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 : : GENERIC_ASSOC }; /* Typename in generic association */
76 : :
77 : : /* States indicating how grokdeclarator() should handle declspecs marked
78 : : with __attribute__((deprecated)) or __attribute__((unavailable)).
79 : : An object declared as __attribute__((unavailable)) should suppress
80 : : any reports of being declared with unavailable or deprecated items.
81 : : An object declared as __attribute__((deprecated)) should suppress
82 : : warnings of uses of other deprecated items. */
83 : :
84 : : enum deprecated_states {
85 : : DEPRECATED_NORMAL,
86 : : DEPRECATED_SUPPRESS,
87 : : UNAVAILABLE_DEPRECATED_SUPPRESS
88 : : };
89 : :
90 : :
91 : : /* Nonzero if we have seen an invalid cross reference
92 : : to a struct, union, or enum, but not yet printed the message. */
93 : : tree pending_invalid_xref;
94 : :
95 : : /* File and line to appear in the eventual error message. */
96 : : location_t pending_invalid_xref_location;
97 : :
98 : : /* The file and line that the prototype came from if this is an
99 : : old-style definition; used for diagnostics in
100 : : store_parm_decls_oldstyle. */
101 : :
102 : : static location_t current_function_prototype_locus;
103 : :
104 : : /* Whether this prototype was built-in. */
105 : :
106 : : static bool current_function_prototype_built_in;
107 : :
108 : : /* The argument type information of this prototype. */
109 : :
110 : : static tree current_function_prototype_arg_types;
111 : :
112 : : /* The argument information structure for the function currently being
113 : : defined. */
114 : :
115 : : static struct c_arg_info *current_function_arg_info;
116 : :
117 : : /* The obstack on which parser and related data structures, which are
118 : : not live beyond their top-level declaration or definition, are
119 : : allocated. */
120 : : struct obstack parser_obstack;
121 : :
122 : : /* The current statement tree. */
123 : :
124 : : static GTY(()) struct stmt_tree_s c_stmt_tree;
125 : :
126 : : /* Zero if we are not in an iteration or switch statement, otherwise
127 : : a bitmask. See bitmask definitions in c-tree.h. */
128 : : unsigned char in_statement;
129 : :
130 : : /* A list of decls to be made automatically visible in each file scope. */
131 : : static GTY(()) tree visible_builtins;
132 : :
133 : : /* Set to 0 at beginning of a function definition, set to 1 if
134 : : a return statement that specifies a return value is seen. */
135 : :
136 : : int current_function_returns_value;
137 : :
138 : : /* Set to 0 at beginning of a function definition, set to 1 if
139 : : a return statement with no argument is seen. */
140 : :
141 : : int current_function_returns_null;
142 : :
143 : : /* Set to 0 at beginning of a function definition, set to 1 if
144 : : a call to a noreturn function is seen. */
145 : :
146 : : int current_function_returns_abnormally;
147 : :
148 : : /* Set to nonzero by `grokdeclarator' for a function
149 : : whose return type is defaulted, if warnings for this are desired. */
150 : :
151 : : static int warn_about_return_type;
152 : :
153 : : /* Nonzero when the current toplevel function contains a declaration
154 : : of a nested function which is never defined. */
155 : :
156 : : static bool undef_nested_function;
157 : :
158 : : /* Vector of implicit "omp declare target" attributes to be added into
159 : : the attribute lists. */
160 : : vec<c_omp_declare_target_attr, va_gc> *current_omp_declare_target_attribute;
161 : :
162 : : /* Vector of
163 : : #pragma omp begin assumes ... #pragma omp end assumes regions
164 : : we are in. */
165 : : vec<c_omp_begin_assumes_data, va_gc> *current_omp_begin_assumes;
166 : :
167 : : /* Vector of loop names with C_DECL_LOOP_NAME or C_DECL_SWITCH_NAME marked
168 : : LABEL_DECL as the last and canonical for each loop or switch. */
169 : : static vec<tree> loop_names;
170 : :
171 : : /* Hash table mapping LABEL_DECLs to the canonical LABEL_DECLs if LOOP_NAMES
172 : : vector becomes too long. */
173 : : static decl_tree_map *loop_names_hash;
174 : :
175 : : /* Each c_binding structure describes one binding of an identifier to
176 : : a decl. All the decls in a scope - irrespective of namespace - are
177 : : chained together by the ->prev field, which (as the name implies)
178 : : runs in reverse order. All the decls in a given namespace bound to
179 : : a given identifier are chained by the ->shadowed field, which runs
180 : : from inner to outer scopes.
181 : :
182 : : The ->decl field usually points to a DECL node, but there are two
183 : : exceptions. In the namespace of type tags, the bound entity is a
184 : : RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
185 : : identifier is encountered, it is bound to error_mark_node to
186 : : suppress further errors about that identifier in the current
187 : : function.
188 : :
189 : : The ->u.type field stores the type of the declaration in this scope;
190 : : if NULL, the type is the type of the ->decl field. This is only of
191 : : relevance for objects with external or internal linkage which may
192 : : be redeclared in inner scopes, forming composite types that only
193 : : persist for the duration of those scopes. In the external scope,
194 : : this stores the composite of all the types declared for this
195 : : object, visible or not. The ->inner_comp field (used only at file
196 : : scope) stores whether an incomplete array type at file scope was
197 : : completed at an inner scope to an array size other than 1.
198 : :
199 : : The ->u.label field is used for labels. It points to a structure
200 : : which stores additional information used for warnings.
201 : :
202 : : The depth field is copied from the scope structure that holds this
203 : : decl. It is used to preserve the proper ordering of the ->shadowed
204 : : field (see bind()) and also for a handful of special-case checks.
205 : : Finally, the invisible bit is true for a decl which should be
206 : : ignored for purposes of normal name lookup, and the nested bit is
207 : : true for a decl that's been bound a second time in an inner scope;
208 : : in all such cases, the binding in the outer scope will have its
209 : : invisible bit true. */
210 : :
211 : : struct GTY((chain_next ("%h.prev"))) c_binding {
212 : : union GTY(()) { /* first so GTY desc can use decl */
213 : : tree GTY((tag ("0"))) type; /* the type in this scope */
214 : : struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
215 : : } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
216 : : tree decl; /* the decl bound */
217 : : tree id; /* the identifier it's bound to */
218 : : struct c_binding *prev; /* the previous decl in this scope */
219 : : struct c_binding *shadowed; /* the innermost decl shadowed by this one */
220 : : unsigned int depth : 28; /* depth of this scope */
221 : : BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
222 : : BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
223 : : BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
224 : : BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
225 : : location_t locus; /* location for nested bindings */
226 : : };
227 : : #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
228 : : #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
229 : : #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
230 : : #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
231 : :
232 : : /* Each C symbol points to three linked lists of c_binding structures.
233 : : These describe the values of the identifier in the three different
234 : : namespaces defined by the language. */
235 : :
236 : : struct GTY(()) lang_identifier {
237 : : struct c_common_identifier common_id;
238 : : struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
239 : : struct c_binding *tag_binding; /* struct/union/enum tags */
240 : : struct c_binding *label_binding; /* labels */
241 : : };
242 : :
243 : : /* Validate c-lang.cc's assumptions. */
244 : : extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
245 : : [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
246 : :
247 : : /* The binding oracle; see c-tree.h. */
248 : : void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
249 : :
250 : : /* This flag is set on an identifier if we have previously asked the
251 : : binding oracle for this identifier's symbol binding. */
252 : : #define I_SYMBOL_CHECKED(node) \
253 : : (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
254 : :
255 : : static inline struct c_binding* *
256 : 4572966436 : i_symbol_binding (tree node)
257 : : {
258 : 4572966436 : struct lang_identifier *lid
259 : 4572966436 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
260 : :
261 : 4572966436 : if (lid->symbol_binding == NULL
262 : 1680785852 : && c_binding_oracle != NULL
263 : 4572966436 : && !I_SYMBOL_CHECKED (node))
264 : : {
265 : : /* Set the "checked" flag first, to avoid infinite recursion
266 : : when the binding oracle calls back into gcc. */
267 : 0 : I_SYMBOL_CHECKED (node) = 1;
268 : 0 : c_binding_oracle (C_ORACLE_SYMBOL, node);
269 : : }
270 : :
271 : 4572966436 : return &lid->symbol_binding;
272 : : }
273 : :
274 : : #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
275 : :
276 : : #define I_SYMBOL_DECL(node) \
277 : : (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
278 : :
279 : : /* This flag is set on an identifier if we have previously asked the
280 : : binding oracle for this identifier's tag binding. */
281 : : #define I_TAG_CHECKED(node) \
282 : : (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
283 : :
284 : : static inline struct c_binding **
285 : 4486936 : i_tag_binding (tree node)
286 : : {
287 : 4486936 : struct lang_identifier *lid
288 : 4486936 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
289 : :
290 : 4486936 : if (lid->tag_binding == NULL
291 : 1242571 : && c_binding_oracle != NULL
292 : 4486936 : && !I_TAG_CHECKED (node))
293 : : {
294 : : /* Set the "checked" flag first, to avoid infinite recursion
295 : : when the binding oracle calls back into gcc. */
296 : 0 : I_TAG_CHECKED (node) = 1;
297 : 0 : c_binding_oracle (C_ORACLE_TAG, node);
298 : : }
299 : :
300 : 4486936 : return &lid->tag_binding;
301 : : }
302 : :
303 : : #define I_TAG_BINDING(node) (*i_tag_binding (node))
304 : :
305 : : #define I_TAG_DECL(node) \
306 : : (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
307 : :
308 : : /* This flag is set on an identifier if we have previously asked the
309 : : binding oracle for this identifier's label binding. */
310 : : #define I_LABEL_CHECKED(node) \
311 : : (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
312 : :
313 : : static inline struct c_binding **
314 : 358296 : i_label_binding (tree node)
315 : : {
316 : 358296 : struct lang_identifier *lid
317 : 358296 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
318 : :
319 : 358296 : if (lid->label_binding == NULL
320 : 47516 : && c_binding_oracle != NULL
321 : 358296 : && !I_LABEL_CHECKED (node))
322 : : {
323 : : /* Set the "checked" flag first, to avoid infinite recursion
324 : : when the binding oracle calls back into gcc. */
325 : 0 : I_LABEL_CHECKED (node) = 1;
326 : 0 : c_binding_oracle (C_ORACLE_LABEL, node);
327 : : }
328 : :
329 : 358296 : return &lid->label_binding;
330 : : }
331 : :
332 : : #define I_LABEL_BINDING(node) (*i_label_binding (node))
333 : :
334 : : #define I_LABEL_DECL(node) \
335 : : (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
336 : :
337 : : /* Used by C_TOKEN_VEC tree. */
338 : : struct GTY (()) c_tree_token_vec {
339 : : struct tree_base base;
340 : : vec<c_token, va_gc> *tokens;
341 : : };
342 : :
343 : : STATIC_ASSERT (sizeof (c_tree_token_vec) == sizeof (c_tree_token_vec_struct));
344 : : STATIC_ASSERT (offsetof (c_tree_token_vec, tokens)
345 : : == offsetof (c_tree_token_vec_struct, tokens));
346 : :
347 : : /* The resulting tree type. */
348 : :
349 : : union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE + 2 * (TREE_CODE (&%h.generic) == C_TOKEN_VEC)"),
350 : : chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
351 : : {
352 : : union tree_node GTY ((tag ("0"),
353 : : desc ("tree_node_structure (&%h)")))
354 : : generic;
355 : : struct lang_identifier GTY ((tag ("1"))) identifier;
356 : : struct c_tree_token_vec GTY ((tag ("2"))) c_token_vec;
357 : : };
358 : :
359 : : /* Langhook for tree_size. */
360 : : size_t
361 : 901 : c_tree_size (enum tree_code code)
362 : : {
363 : 901 : gcc_checking_assert (code >= NUM_TREE_CODES);
364 : 901 : switch (code)
365 : : {
366 : : case C_TOKEN_VEC: return sizeof (c_tree_token_vec);
367 : 0 : default:
368 : 0 : switch (TREE_CODE_CLASS (code))
369 : : {
370 : : case tcc_declaration: return sizeof (tree_decl_non_common);
371 : 0 : case tcc_type: return sizeof (tree_type_non_common);
372 : 0 : default: gcc_unreachable ();
373 : : }
374 : : }
375 : : }
376 : :
377 : : /* Track bindings and other things that matter for goto warnings. For
378 : : efficiency, we do not gather all the decls at the point of
379 : : definition. Instead, we point into the bindings structure. As
380 : : scopes are popped, we update these structures and gather the decls
381 : : that matter at that time. */
382 : :
383 : : struct GTY(()) c_spot_bindings {
384 : : /* The currently open scope which holds bindings defined when the
385 : : label was defined or the goto statement was found. */
386 : : struct c_scope *scope;
387 : : /* The bindings in the scope field which were defined at the point
388 : : of the label or goto. This lets us look at older or newer
389 : : bindings in the scope, as appropriate. */
390 : : struct c_binding *bindings_in_scope;
391 : : /* The number of statement expressions that have started since this
392 : : label or goto statement was defined. This is zero if we are at
393 : : the same statement expression level. It is positive if we are in
394 : : a statement expression started since this spot. It is negative
395 : : if this spot was in a statement expression and we have left
396 : : it. */
397 : : int stmt_exprs;
398 : : /* Whether we started in a statement expression but are no longer in
399 : : it. This is set to true if stmt_exprs ever goes negative. */
400 : : bool left_stmt_expr;
401 : : };
402 : :
403 : : /* This structure is used to keep track of bindings seen when a goto
404 : : statement is defined. This is only used if we see the goto
405 : : statement before we see the label. */
406 : :
407 : : struct GTY(()) c_goto_bindings {
408 : : /* The location of the goto statement. */
409 : : location_t loc;
410 : : /* The bindings of the goto statement. */
411 : : struct c_spot_bindings goto_bindings;
412 : : };
413 : :
414 : : typedef struct c_goto_bindings *c_goto_bindings_p;
415 : :
416 : : /* The additional information we keep track of for a label binding.
417 : : These fields are updated as scopes are popped. */
418 : :
419 : : struct GTY(()) c_label_vars {
420 : : /* The shadowed c_label_vars, when one label shadows another (which
421 : : can only happen using a __label__ declaration). */
422 : : struct c_label_vars *shadowed;
423 : : /* The bindings when the label was defined. */
424 : : struct c_spot_bindings label_bindings;
425 : : /* A list of decls that we care about: decls about which we should
426 : : warn if a goto branches to this label from later in the function.
427 : : Decls are added to this list as scopes are popped. We only add
428 : : the decls that matter. */
429 : : vec<tree, va_gc> *decls_in_scope;
430 : : /* A list of goto statements to this label. This is only used for
431 : : goto statements seen before the label was defined, so that we can
432 : : issue appropriate warnings for them. */
433 : : vec<c_goto_bindings_p, va_gc> *gotos;
434 : : };
435 : :
436 : : /* Each c_scope structure describes the complete contents of one
437 : : scope. Four scopes are distinguished specially: the innermost or
438 : : current scope, the innermost function scope, the file scope (always
439 : : the second to outermost) and the outermost or external scope.
440 : :
441 : : Most declarations are recorded in the current scope.
442 : :
443 : : All normal label declarations are recorded in the innermost
444 : : function scope, as are bindings of undeclared identifiers to
445 : : error_mark_node. (GCC permits nested functions as an extension,
446 : : hence the 'innermost' qualifier.) Explicitly declared labels
447 : : (using the __label__ extension) appear in the current scope.
448 : :
449 : : Being in the file scope (current_scope == file_scope) causes
450 : : special behavior in several places below. Also, under some
451 : : conditions the Objective-C front end records declarations in the
452 : : file scope even though that isn't the current scope.
453 : :
454 : : All declarations with external linkage are recorded in the external
455 : : scope, even if they aren't visible there; this models the fact that
456 : : such declarations are visible to the entire program, and (with a
457 : : bit of cleverness, see pushdecl) allows diagnosis of some violations
458 : : of C99 6.2.2p7 and 6.2.7p2:
459 : :
460 : : If, within the same translation unit, the same identifier appears
461 : : with both internal and external linkage, the behavior is
462 : : undefined.
463 : :
464 : : All declarations that refer to the same object or function shall
465 : : have compatible type; otherwise, the behavior is undefined.
466 : :
467 : : Initially only the built-in declarations, which describe compiler
468 : : intrinsic functions plus a subset of the standard library, are in
469 : : this scope.
470 : :
471 : : The order of the blocks list matters, and it is frequently appended
472 : : to. To avoid having to walk all the way to the end of the list on
473 : : each insertion, or reverse the list later, we maintain a pointer to
474 : : the last list entry. (FIXME: It should be feasible to use a reversed
475 : : list here.)
476 : :
477 : : The bindings list is strictly in reverse order of declarations;
478 : : pop_scope relies on this. */
479 : :
480 : :
481 : : struct GTY((chain_next ("%h.outer"))) c_scope {
482 : : /* The scope containing this one. */
483 : : struct c_scope *outer;
484 : :
485 : : /* The next outermost function scope. */
486 : : struct c_scope *outer_function;
487 : :
488 : : /* All bindings in this scope. */
489 : : struct c_binding *bindings;
490 : :
491 : : /* For each scope (except the global one), a chain of BLOCK nodes
492 : : for all the scopes that were entered and exited one level down. */
493 : : tree blocks;
494 : : tree blocks_last;
495 : :
496 : : /* The depth of this scope. Used to keep the ->shadowed chain of
497 : : bindings sorted innermost to outermost. */
498 : : unsigned int depth : 28;
499 : :
500 : : /* True if we are currently filling this scope with parameter
501 : : declarations. */
502 : : BOOL_BITFIELD parm_flag : 1;
503 : :
504 : : /* True if we saw [*] in this scope. Used to give an error messages
505 : : if these appears in a function definition. */
506 : : BOOL_BITFIELD had_vla_unspec : 1;
507 : :
508 : : /* True if we parsed a list of forward parameter decls in this scope. */
509 : : BOOL_BITFIELD had_forward_parm_decls : 1;
510 : :
511 : : /* True if this is the outermost block scope of a function body.
512 : : This scope contains the parameters, the local variables declared
513 : : in the outermost block, and all the labels (except those in
514 : : nested functions, or declared at block scope with __label__). */
515 : : BOOL_BITFIELD function_body : 1;
516 : :
517 : : /* True means make a BLOCK for this scope no matter what. */
518 : : BOOL_BITFIELD keep : 1;
519 : :
520 : : /* True means that an unsuffixed float constant is _Decimal64. */
521 : : BOOL_BITFIELD float_const_decimal64 : 1;
522 : :
523 : : /* True if this scope has any label bindings. This is used to speed
524 : : up searching for labels when popping scopes, particularly since
525 : : labels are normally only found at function scope. */
526 : : BOOL_BITFIELD has_label_bindings : 1;
527 : :
528 : : /* True if we should issue a warning if a goto statement crosses any
529 : : of the bindings. We still need to check the list of bindings to
530 : : find the specific ones we need to warn about. This is true if
531 : : decl_jump_unsafe would return true for any of the bindings. This
532 : : is used to avoid looping over all the bindings unnecessarily. */
533 : : BOOL_BITFIELD has_jump_unsafe_decl : 1;
534 : : };
535 : :
536 : : /* The scope currently in effect. */
537 : :
538 : : static GTY(()) struct c_scope *current_scope;
539 : :
540 : : /* The innermost function scope. Ordinary (not explicitly declared)
541 : : labels, bindings to error_mark_node, and the lazily-created
542 : : bindings of __func__ and its friends get this scope. */
543 : :
544 : : static GTY(()) struct c_scope *current_function_scope;
545 : :
546 : : /* The C file scope. This is reset for each input translation unit. */
547 : :
548 : : static GTY(()) struct c_scope *file_scope;
549 : :
550 : : /* The outermost scope. This is used for all declarations with
551 : : external linkage, and only these, hence the name. */
552 : :
553 : : static GTY(()) struct c_scope *external_scope;
554 : :
555 : : /* A chain of c_scope structures awaiting reuse. */
556 : :
557 : : static GTY((deletable)) struct c_scope *scope_freelist;
558 : :
559 : : /* A chain of c_binding structures awaiting reuse. */
560 : :
561 : : static GTY((deletable)) struct c_binding *binding_freelist;
562 : :
563 : : /* Append VAR to LIST in scope SCOPE. */
564 : : #define SCOPE_LIST_APPEND(scope, list, decl) do { \
565 : : struct c_scope *s_ = (scope); \
566 : : tree d_ = (decl); \
567 : : if (s_->list##_last) \
568 : : BLOCK_CHAIN (s_->list##_last) = d_; \
569 : : else \
570 : : s_->list = d_; \
571 : : s_->list##_last = d_; \
572 : : } while (0)
573 : :
574 : : /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
575 : : #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
576 : : struct c_scope *t_ = (tscope); \
577 : : struct c_scope *f_ = (fscope); \
578 : : if (t_->to##_last) \
579 : : BLOCK_CHAIN (t_->to##_last) = f_->from; \
580 : : else \
581 : : t_->to = f_->from; \
582 : : t_->to##_last = f_->from##_last; \
583 : : } while (0)
584 : :
585 : : /* A c_inline_static structure stores details of a static identifier
586 : : referenced in a definition of a function that may be an inline
587 : : definition if no subsequent declaration of that function uses
588 : : "extern" or does not use "inline". */
589 : :
590 : : struct GTY((chain_next ("%h.next"))) c_inline_static {
591 : : /* The location for a diagnostic. */
592 : : location_t location;
593 : :
594 : : /* The function that may be an inline definition. */
595 : : tree function;
596 : :
597 : : /* The object or function referenced. */
598 : : tree static_decl;
599 : :
600 : : /* What sort of reference this is. */
601 : : enum c_inline_static_type type;
602 : :
603 : : /* The next such structure or NULL. */
604 : : struct c_inline_static *next;
605 : : };
606 : :
607 : : /* List of static identifiers used or referenced in functions that may
608 : : be inline definitions. */
609 : : static GTY(()) struct c_inline_static *c_inline_statics;
610 : :
611 : : /* True means unconditionally make a BLOCK for the next scope pushed. */
612 : :
613 : : static bool keep_next_level_flag;
614 : :
615 : : /* True means the next call to push_scope will be the outermost scope
616 : : of a function body, so do not push a new scope, merely cease
617 : : expecting parameter decls. */
618 : :
619 : : static bool next_is_function_body;
620 : :
621 : : /* A vector of pointers to c_binding structures. */
622 : :
623 : : typedef struct c_binding *c_binding_ptr;
624 : :
625 : : /* Information that we keep for a struct or union while it is being
626 : : parsed. */
627 : :
628 : 1168127 : class c_struct_parse_info
629 : : {
630 : : public:
631 : : /* If warn_cxx_compat, a list of types defined within this
632 : : struct. */
633 : : auto_vec<tree> struct_types;
634 : : /* If warn_cxx_compat, a list of field names which have bindings,
635 : : and which are defined in this struct, but which are not defined
636 : : in any enclosing struct. This is used to clear the in_struct
637 : : field of the c_bindings structure. */
638 : : auto_vec<c_binding_ptr> fields;
639 : : /* If warn_cxx_compat, a list of typedef names used when defining
640 : : fields in this struct. */
641 : : auto_vec<tree> typedefs_seen;
642 : : /* The location of a previous definition of this struct. */
643 : : location_t refloc;
644 : : };
645 : :
646 : :
647 : : /* Hash table for structs and unions. */
648 : : struct c_struct_hasher : ggc_ptr_hash<tree_node>
649 : : {
650 : : static hashval_t hash (tree t);
651 : : static bool equal (tree, tree);
652 : : };
653 : :
654 : : /* Hash an RECORD OR UNION. */
655 : : hashval_t
656 : 4303706 : c_struct_hasher::hash (tree type)
657 : : {
658 : 4303706 : inchash::hash hstate;
659 : :
660 : 4303706 : hstate.add_int (TREE_CODE (type));
661 : 4303706 : tree tag = c_type_tag (type);
662 : 4303706 : hstate.add_object (tag);
663 : :
664 : 4303706 : return hstate.end ();
665 : : }
666 : :
667 : : /* Compare two RECORD or UNION types. */
668 : : bool
669 : 14518402 : c_struct_hasher::equal (tree t1, tree t2)
670 : : {
671 : 14518402 : return comptypes_equiv_p (t1, t2);
672 : : }
673 : :
674 : : /* All tagged typed so that TYPE_CANONICAL can be set correctly. */
675 : : static GTY (()) hash_table<c_struct_hasher> *c_struct_htab;
676 : :
677 : : /* Information for the struct or union currently being parsed, or
678 : : NULL if not parsing a struct or union. */
679 : : static class c_struct_parse_info *struct_parse_info;
680 : :
681 : : /* Forward declarations. */
682 : : static tree lookup_name_in_scope (tree, struct c_scope *);
683 : : static tree c_make_fname_decl (location_t, tree, int);
684 : : static tree grokdeclarator (const struct c_declarator *,
685 : : struct c_declspecs *,
686 : : enum decl_context, bool, tree *, tree *, tree *,
687 : : bool *, enum deprecated_states);
688 : : static tree grokparms (struct c_arg_info *, bool);
689 : : static void layout_array_type (tree);
690 : : static const char *header_for_builtin_fn (tree);
691 : :
692 : : /* T is a statement. Add it to the statement-tree. This is the
693 : : C/ObjC version--C++ has a slightly different version of this
694 : : function. */
695 : :
696 : : tree
697 : 93289589 : add_stmt (tree t)
698 : : {
699 : 93289589 : enum tree_code code = TREE_CODE (t);
700 : :
701 : 93289589 : if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
702 : : {
703 : 92579280 : if (!EXPR_HAS_LOCATION (t))
704 : 154810 : SET_EXPR_LOCATION (t, input_location);
705 : : }
706 : :
707 : : /* Add T to the statement-tree. Non-side-effect statements need to be
708 : : recorded during statement expressions. */
709 : 93289589 : if (!building_stmt_list_p ())
710 : 1 : push_stmt_list ();
711 : :
712 : 93289589 : if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
713 : 1078357 : STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
714 : :
715 : 93289589 : append_to_statement_list_force (t, &cur_stmt_list);
716 : :
717 : 93289589 : return t;
718 : : }
719 : :
720 : :
721 : :
722 : :
723 : :
724 : : /* Return true if we will want to say something if a goto statement
725 : : crosses DECL. */
726 : :
727 : : static bool
728 : 904425414 : decl_jump_unsafe (tree decl)
729 : : {
730 : 904425414 : if (error_operand_p (decl))
731 : : return false;
732 : :
733 : : /* Don't warn for compound literals. If a goto statement crosses
734 : : their initialization, it should cross also all the places where
735 : : the complit is used or where the complit address might be saved
736 : : into some variable, so code after the label to which goto jumps
737 : : should not be able to refer to the compound literal. */
738 : 904423220 : if (VAR_P (decl) && C_DECL_COMPOUND_LITERAL_P (decl))
739 : : return false;
740 : :
741 : 903507250 : if (flag_openmp
742 : 12001982 : && VAR_P (decl)
743 : 903538315 : && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl)))
744 : : return true;
745 : :
746 : : /* Always warn about crossing variably modified types. */
747 : 893497530 : if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
748 : 915614629 : && c_type_variably_modified_p (TREE_TYPE (decl)))
749 : : return true;
750 : :
751 : : /* Otherwise, only warn if -Wgoto-misses-init and this is an
752 : : initialized automatic decl. */
753 : 903466856 : if (warn_jump_misses_init
754 : 5960559 : && VAR_P (decl)
755 : 10205 : && !TREE_STATIC (decl)
756 : 903472301 : && DECL_INITIAL (decl) != NULL_TREE)
757 : : return true;
758 : :
759 : : return false;
760 : : }
761 : :
762 : :
763 : : void
764 : 0 : c_print_identifier (FILE *file, tree node, int indent)
765 : : {
766 : 0 : void (*save) (enum c_oracle_request, tree identifier);
767 : :
768 : : /* Temporarily hide any binding oracle. Without this, calls to
769 : : debug_tree from the debugger will end up calling into the oracle,
770 : : making for a confusing debug session. As the oracle isn't needed
771 : : here for normal operation, it's simplest to suppress it. */
772 : 0 : save = c_binding_oracle;
773 : 0 : c_binding_oracle = NULL;
774 : :
775 : 0 : print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
776 : 0 : print_node (file, "tag", I_TAG_DECL (node), indent + 4);
777 : 0 : print_node (file, "label", I_LABEL_DECL (node), indent + 4);
778 : 0 : if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
779 : : {
780 : 0 : tree rid = ridpointers[C_RID_CODE (node)];
781 : 0 : indent_to (file, indent + 4);
782 : 0 : fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
783 : 0 : (void *) rid, IDENTIFIER_POINTER (rid));
784 : : }
785 : :
786 : 0 : c_binding_oracle = save;
787 : 0 : }
788 : :
789 : : /* Establish that the scope contains declarations that are sensitive to
790 : : jumps that cross a binding. Together with decl_jump_unsafe, this is
791 : : used to diagnose such jumps. */
792 : : void
793 : 69 : c_mark_decl_jump_unsafe_in_current_scope ()
794 : : {
795 : 69 : current_scope->has_jump_unsafe_decl = 1;
796 : 69 : }
797 : :
798 : : /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
799 : : which may be any of several kinds of DECL or TYPE or error_mark_node,
800 : : in the scope SCOPE. */
801 : : static void
802 : 904276295 : bind (tree name, tree decl, struct c_scope *scope, bool invisible,
803 : : bool nested, location_t locus)
804 : : {
805 : 904276295 : struct c_binding *b, **here;
806 : :
807 : 904276295 : if (binding_freelist)
808 : : {
809 : 229262896 : b = binding_freelist;
810 : 229262896 : binding_freelist = b->prev;
811 : : }
812 : : else
813 : 675013399 : b = ggc_alloc<c_binding> ();
814 : :
815 : 904276295 : b->shadowed = 0;
816 : 904276295 : b->decl = decl;
817 : 904276295 : b->id = name;
818 : 904276295 : b->depth = scope->depth;
819 : 904276295 : b->invisible = invisible;
820 : 904276295 : b->nested = nested;
821 : 904276295 : b->inner_comp = 0;
822 : 904276295 : b->in_struct = 0;
823 : 904276295 : b->locus = locus;
824 : :
825 : 904276295 : b->u.type = NULL;
826 : :
827 : 904276295 : b->prev = scope->bindings;
828 : 904276295 : scope->bindings = b;
829 : :
830 : 904276295 : if (decl_jump_unsafe (decl))
831 : 24101 : scope->has_jump_unsafe_decl = 1;
832 : :
833 : 904276295 : if (!name)
834 : : return;
835 : :
836 : 895713860 : switch (TREE_CODE (decl))
837 : : {
838 : 23787 : case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
839 : 620576 : case ENUMERAL_TYPE:
840 : 620576 : case UNION_TYPE:
841 : 620576 : case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
842 : 895069497 : case VAR_DECL:
843 : 895069497 : case FUNCTION_DECL:
844 : 895069497 : case TYPE_DECL:
845 : 895069497 : case CONST_DECL:
846 : 895069497 : case PARM_DECL:
847 : 895069497 : case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
848 : :
849 : 0 : default:
850 : 0 : gcc_unreachable ();
851 : : }
852 : :
853 : : /* Locate the appropriate place in the chain of shadowed decls
854 : : to insert this binding. Normally, scope == current_scope and
855 : : this does nothing. */
856 : 895713904 : while (*here && (*here)->depth > scope->depth)
857 : 44 : here = &(*here)->shadowed;
858 : :
859 : 895713860 : b->shadowed = *here;
860 : 895713860 : *here = b;
861 : : }
862 : :
863 : : /* Clear the binding structure B, stick it on the binding_freelist,
864 : : and return the former value of b->prev. This is used by pop_scope
865 : : and get_parm_info to iterate destructively over all the bindings
866 : : from a given scope. */
867 : : static struct c_binding *
868 : 887280203 : free_binding_and_advance (struct c_binding *b)
869 : : {
870 : 887280203 : struct c_binding *prev = b->prev;
871 : :
872 : 887280203 : memset (b, 0, sizeof (struct c_binding));
873 : 887280203 : b->prev = binding_freelist;
874 : 887280203 : binding_freelist = b;
875 : :
876 : 887280203 : return prev;
877 : : }
878 : :
879 : : /* Bind a label. Like bind, but skip fields which aren't used for
880 : : labels, and add the LABEL_VARS value. */
881 : : static void
882 : 23787 : bind_label (tree name, tree label, struct c_scope *scope,
883 : : struct c_label_vars *label_vars)
884 : : {
885 : 23787 : struct c_binding *b;
886 : :
887 : 23787 : bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
888 : : UNKNOWN_LOCATION);
889 : :
890 : 23787 : scope->has_label_bindings = true;
891 : :
892 : 23787 : b = scope->bindings;
893 : 23787 : gcc_assert (b->decl == label);
894 : 23787 : label_vars->shadowed = b->u.label;
895 : 23787 : b->u.label = label_vars;
896 : 23787 : }
897 : :
898 : : /* Hook called at end of compilation to assume 1 elt
899 : : for a file-scope tentative array defn that wasn't complete before. */
900 : :
901 : : void
902 : 7480 : c_finish_incomplete_decl (tree decl)
903 : : {
904 : 7480 : if (VAR_P (decl))
905 : : {
906 : 7480 : tree type = TREE_TYPE (decl);
907 : 7480 : if (type != error_mark_node
908 : 7403 : && TREE_CODE (type) == ARRAY_TYPE
909 : 7208 : && !DECL_EXTERNAL (decl)
910 : 7568 : && TYPE_DOMAIN (type) == NULL_TREE)
911 : : {
912 : 88 : if (flag_isoc2y && !TREE_PUBLIC (decl))
913 : 2 : error_at (DECL_SOURCE_LOCATION (decl),
914 : : "array size missing in %q+D", decl);
915 : : else
916 : 86 : warning_at (DECL_SOURCE_LOCATION (decl),
917 : 86 : 0, "array %q+D assumed to have one element", decl);
918 : :
919 : 88 : complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
920 : :
921 : 88 : relayout_decl (decl);
922 : : }
923 : : }
924 : 7480 : }
925 : :
926 : : /* Record that inline function FUNC contains a reference (location
927 : : LOC) to static DECL (file-scope or function-local according to
928 : : TYPE). */
929 : :
930 : : void
931 : 36 : record_inline_static (location_t loc, tree func, tree decl,
932 : : enum c_inline_static_type type)
933 : : {
934 : 36 : c_inline_static *csi = ggc_alloc<c_inline_static> ();
935 : 36 : csi->location = loc;
936 : 36 : csi->function = func;
937 : 36 : csi->static_decl = decl;
938 : 36 : csi->type = type;
939 : 36 : csi->next = c_inline_statics;
940 : 36 : c_inline_statics = csi;
941 : 36 : }
942 : :
943 : : /* Check for references to static declarations in inline functions at
944 : : the end of the translation unit and diagnose them if the functions
945 : : are still inline definitions. */
946 : :
947 : : static void
948 : 107717 : check_inline_statics (void)
949 : : {
950 : 107717 : struct c_inline_static *csi;
951 : 107753 : for (csi = c_inline_statics; csi; csi = csi->next)
952 : : {
953 : 36 : if (DECL_EXTERNAL (csi->function))
954 : 30 : switch (csi->type)
955 : : {
956 : 23 : case csi_internal:
957 : 23 : pedwarn (csi->location, 0,
958 : : "%qD is static but used in inline function %qD "
959 : : "which is not static", csi->static_decl, csi->function);
960 : 23 : break;
961 : 7 : case csi_modifiable:
962 : 7 : pedwarn (csi->location, 0,
963 : : "%q+D is static but declared in inline function %qD "
964 : : "which is not static", csi->static_decl, csi->function);
965 : 7 : break;
966 : 0 : default:
967 : 0 : gcc_unreachable ();
968 : : }
969 : : }
970 : 107717 : c_inline_statics = NULL;
971 : 107717 : }
972 : :
973 : : /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
974 : : for the current state, otherwise set it to uninitialized. */
975 : :
976 : : static void
977 : 134295 : set_spot_bindings (struct c_spot_bindings *p, bool defining)
978 : : {
979 : 0 : if (defining)
980 : : {
981 : 127246 : p->scope = current_scope;
982 : 16738 : p->bindings_in_scope = current_scope->bindings;
983 : : }
984 : : else
985 : : {
986 : 7049 : p->scope = NULL;
987 : 7049 : p->bindings_in_scope = NULL;
988 : : }
989 : 134295 : p->stmt_exprs = 0;
990 : 134295 : p->left_stmt_expr = false;
991 : 0 : }
992 : :
993 : : /* Update spot bindings P as we pop out of SCOPE. Return true if we
994 : : should push decls for a label. */
995 : :
996 : : static bool
997 : 470605630 : update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
998 : : {
999 : 0 : if (p->scope != scope)
1000 : : {
1001 : : /* This label or goto is defined in some other scope, or it is a
1002 : : label which is not yet defined. There is nothing to
1003 : : update. */
1004 : : return false;
1005 : : }
1006 : :
1007 : : /* Adjust the spot bindings to refer to the bindings already defined
1008 : : in the enclosing scope. */
1009 : 204964 : p->scope = scope->outer;
1010 : 204964 : p->bindings_in_scope = p->scope->bindings;
1011 : :
1012 : 151272 : return true;
1013 : : }
1014 : :
1015 : : /* The Objective-C front-end often needs to determine the current scope. */
1016 : :
1017 : : void *
1018 : 0 : objc_get_current_scope (void)
1019 : : {
1020 : 0 : return current_scope;
1021 : : }
1022 : :
1023 : : /* The following function is used only by Objective-C. It needs to live here
1024 : : because it accesses the innards of c_scope. */
1025 : :
1026 : : void
1027 : 0 : objc_mark_locals_volatile (void *enclosing_blk)
1028 : : {
1029 : 0 : struct c_scope *scope;
1030 : 0 : struct c_binding *b;
1031 : :
1032 : 0 : for (scope = current_scope;
1033 : 0 : scope && scope != enclosing_blk;
1034 : 0 : scope = scope->outer)
1035 : : {
1036 : 0 : for (b = scope->bindings; b; b = b->prev)
1037 : 0 : objc_volatilize_decl (b->decl);
1038 : :
1039 : : /* Do not climb up past the current function. */
1040 : 0 : if (scope->function_body)
1041 : : break;
1042 : : }
1043 : 0 : }
1044 : :
1045 : : /* Return true if we are in the global binding level. */
1046 : :
1047 : : bool
1048 : 2085398 : global_bindings_p (void)
1049 : : {
1050 : 2085398 : return current_scope == file_scope;
1051 : : }
1052 : :
1053 : : /* Return true if we're declaring parameters in an old-style function
1054 : : declaration. */
1055 : :
1056 : : bool
1057 : 34790 : old_style_parameter_scope (void)
1058 : : {
1059 : : /* If processing parameters and there is no function statement list, we
1060 : : * have an old-style function declaration. */
1061 : 34790 : return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
1062 : : }
1063 : :
1064 : : void
1065 : 51981 : keep_next_level (void)
1066 : : {
1067 : 51981 : keep_next_level_flag = true;
1068 : 51981 : }
1069 : :
1070 : : /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
1071 : :
1072 : : void
1073 : 20 : set_float_const_decimal64 (void)
1074 : : {
1075 : 20 : current_scope->float_const_decimal64 = true;
1076 : 20 : }
1077 : :
1078 : : /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
1079 : :
1080 : : void
1081 : 28 : clear_float_const_decimal64 (void)
1082 : : {
1083 : 28 : current_scope->float_const_decimal64 = false;
1084 : 28 : }
1085 : :
1086 : : /* Return nonzero if an unsuffixed float constant is _Decimal64. */
1087 : :
1088 : : bool
1089 : 601488 : float_const_decimal64_p (void)
1090 : : {
1091 : 601488 : return current_scope->float_const_decimal64;
1092 : : }
1093 : :
1094 : : /* Identify this scope as currently being filled with parameters. */
1095 : :
1096 : : void
1097 : 86380631 : declare_parm_level (void)
1098 : : {
1099 : 86380631 : current_scope->parm_flag = true;
1100 : 86380631 : }
1101 : :
1102 : : void
1103 : 127466710 : push_scope (void)
1104 : : {
1105 : 127466710 : if (next_is_function_body)
1106 : : {
1107 : : /* This is the transition from the parameters to the top level
1108 : : of the function body. These are the same scope
1109 : : (C99 6.2.1p4,6) so we do not push another scope structure.
1110 : : next_is_function_body is set only by store_parm_decls, which
1111 : : in turn is called when and only when we are about to
1112 : : encounter the opening curly brace for the function body.
1113 : :
1114 : : The outermost block of a function always gets a BLOCK node,
1115 : : because the debugging output routines expect that each
1116 : : function has at least one BLOCK. */
1117 : 36132629 : current_scope->parm_flag = false;
1118 : 36132629 : current_scope->function_body = true;
1119 : 36132629 : current_scope->keep = true;
1120 : 36132629 : current_scope->outer_function = current_function_scope;
1121 : 36132629 : current_function_scope = current_scope;
1122 : :
1123 : 36132629 : keep_next_level_flag = false;
1124 : 36132629 : next_is_function_body = false;
1125 : :
1126 : : /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1127 : 36132629 : if (current_scope->outer)
1128 : 36132629 : current_scope->float_const_decimal64
1129 : 36132629 : = current_scope->outer->float_const_decimal64;
1130 : : else
1131 : 0 : current_scope->float_const_decimal64 = false;
1132 : : }
1133 : : else
1134 : : {
1135 : 91334081 : struct c_scope *scope;
1136 : 91334081 : if (scope_freelist)
1137 : : {
1138 : 90485063 : scope = scope_freelist;
1139 : 90485063 : scope_freelist = scope->outer;
1140 : : }
1141 : : else
1142 : 849018 : scope = ggc_cleared_alloc<c_scope> ();
1143 : :
1144 : : /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1145 : 91334081 : if (current_scope)
1146 : 91220817 : scope->float_const_decimal64 = current_scope->float_const_decimal64;
1147 : : else
1148 : 113264 : scope->float_const_decimal64 = false;
1149 : :
1150 : 91334081 : scope->keep = keep_next_level_flag;
1151 : 91334081 : scope->outer = current_scope;
1152 : 91334081 : scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1153 : :
1154 : : /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1155 : : possible. */
1156 : 91334081 : if (current_scope && scope->depth == 0)
1157 : : {
1158 : 0 : scope->depth--;
1159 : 0 : sorry ("GCC supports only %u nested scopes", scope->depth);
1160 : : }
1161 : :
1162 : 91334081 : current_scope = scope;
1163 : 91334081 : keep_next_level_flag = false;
1164 : : }
1165 : 127466710 : }
1166 : :
1167 : : /* This is called when we are leaving SCOPE. For each label defined
1168 : : in SCOPE, add any appropriate decls to its decls_in_scope fields.
1169 : : These are the decls whose initialization will be skipped by a goto
1170 : : later in the function. */
1171 : :
1172 : : static void
1173 : 91327651 : update_label_decls (struct c_scope *scope)
1174 : : {
1175 : 91327651 : struct c_scope *s;
1176 : :
1177 : 91327651 : s = scope;
1178 : 329818051 : while (s != NULL)
1179 : : {
1180 : 279370671 : if (s->has_label_bindings)
1181 : : {
1182 : 231147 : struct c_binding *b;
1183 : :
1184 : 2132798 : for (b = s->bindings; b != NULL; b = b->prev)
1185 : : {
1186 : 1901651 : struct c_label_vars *label_vars;
1187 : 1901651 : struct c_binding *b1;
1188 : 1901651 : bool hjud;
1189 : 1901651 : unsigned int ix;
1190 : 1901651 : struct c_goto_bindings *g;
1191 : :
1192 : 1901651 : if (TREE_CODE (b->decl) != LABEL_DECL)
1193 : 1399747 : continue;
1194 : 501904 : label_vars = b->u.label;
1195 : :
1196 : 501904 : b1 = label_vars->label_bindings.bindings_in_scope;
1197 : 501904 : if (label_vars->label_bindings.scope == NULL)
1198 : : hjud = false;
1199 : : else
1200 : 192871 : hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1201 : 501904 : if (update_spot_bindings (scope, &label_vars->label_bindings))
1202 : : {
1203 : : /* This label is defined in this scope. */
1204 : 53692 : if (hjud)
1205 : : {
1206 : 169658 : for (; b1 != NULL; b1 = b1->prev)
1207 : : {
1208 : : /* A goto from later in the function to this
1209 : : label will never see the initialization
1210 : : of B1, if any. Save it to issue a
1211 : : warning if needed. */
1212 : 148239 : if (decl_jump_unsafe (b1->decl))
1213 : 19449 : vec_safe_push(label_vars->decls_in_scope, b1->decl);
1214 : : }
1215 : : }
1216 : : }
1217 : :
1218 : : /* Update the bindings of any goto statements associated
1219 : : with this label. */
1220 : 472298113 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1221 : 470254998 : update_spot_bindings (scope, &g->goto_bindings);
1222 : : }
1223 : : }
1224 : :
1225 : : /* Don't search beyond the current function. */
1226 : 279370671 : if (s == current_function_scope)
1227 : : break;
1228 : :
1229 : 238490400 : s = s->outer;
1230 : : }
1231 : 91327651 : }
1232 : :
1233 : : /* Exit a scope. Restore the state of the identifier-decl mappings
1234 : : that were in effect when this scope was entered. Return a BLOCK
1235 : : node containing all the DECLs in this scope that are of interest
1236 : : to debug info generation. */
1237 : :
1238 : : tree
1239 : 91327651 : pop_scope (void)
1240 : : {
1241 : 91327651 : struct c_scope *scope = current_scope;
1242 : 91327651 : tree block, context, p;
1243 : 91327651 : struct c_binding *b;
1244 : :
1245 : 91327651 : bool functionbody = scope->function_body;
1246 : 91327651 : bool keep = functionbody || scope->keep || scope->bindings;
1247 : :
1248 : 91327651 : update_label_decls (scope);
1249 : :
1250 : : /* If appropriate, create a BLOCK to record the decls for the life
1251 : : of this function. */
1252 : 91327651 : block = NULL_TREE;
1253 : 91327651 : if (keep)
1254 : : {
1255 : 36625245 : block = make_node (BLOCK);
1256 : 36625245 : BLOCK_SUBBLOCKS (block) = scope->blocks;
1257 : 36625245 : TREE_USED (block) = 1;
1258 : :
1259 : : /* In each subblock, record that this is its superior. */
1260 : 36903154 : for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1261 : 277909 : BLOCK_SUPERCONTEXT (p) = block;
1262 : :
1263 : 36625245 : BLOCK_VARS (block) = NULL_TREE;
1264 : : }
1265 : :
1266 : : /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1267 : : scope must be set so that they point to the appropriate
1268 : : construct, i.e. either to the current FUNCTION_DECL node, or
1269 : : else to the BLOCK node we just constructed.
1270 : :
1271 : : Note that for tagged types whose scope is just the formal
1272 : : parameter list for some function type specification, we can't
1273 : : properly set their TYPE_CONTEXTs here, because we don't have a
1274 : : pointer to the appropriate FUNCTION_TYPE node readily available
1275 : : to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1276 : : type nodes get set in `grokdeclarator' as soon as we have created
1277 : : the FUNCTION_TYPE node which will represent the "scope" for these
1278 : : "parameter list local" tagged types. */
1279 : 91327651 : if (scope->function_body)
1280 : 36132628 : context = current_function_decl;
1281 : 55195023 : else if (scope == file_scope)
1282 : : {
1283 : 107355 : tree file_decl
1284 : 107355 : = build_translation_unit_decl (get_identifier (main_input_filename));
1285 : 107355 : context = file_decl;
1286 : 107355 : debug_hooks->register_main_translation_unit (file_decl);
1287 : : }
1288 : : else
1289 : : context = block;
1290 : :
1291 : : /* Clear all bindings in this scope. */
1292 : 856209391 : for (b = scope->bindings; b; b = free_binding_and_advance (b))
1293 : : {
1294 : 764881740 : p = b->decl;
1295 : 764881740 : switch (TREE_CODE (p))
1296 : : {
1297 : 23787 : case LABEL_DECL:
1298 : : /* Warnings for unused labels, errors for undefined labels. */
1299 : 23787 : if (TREE_USED (p) && !DECL_INITIAL (p))
1300 : : {
1301 : 63 : error ("label %q+D used but not defined", p);
1302 : 63 : DECL_INITIAL (p) = error_mark_node;
1303 : : }
1304 : : else
1305 : 23724 : warn_for_unused_label (p);
1306 : :
1307 : : /* Labels go in BLOCK_VARS. */
1308 : 23787 : DECL_CHAIN (p) = BLOCK_VARS (block);
1309 : 23787 : BLOCK_VARS (block) = p;
1310 : 23787 : gcc_assert (I_LABEL_BINDING (b->id) == b);
1311 : 23787 : I_LABEL_BINDING (b->id) = b->shadowed;
1312 : :
1313 : : /* Also pop back to the shadowed label_vars. */
1314 : 23787 : release_tree_vector (b->u.label->decls_in_scope);
1315 : 23787 : b->u.label = b->u.label->shadowed;
1316 : 23787 : break;
1317 : :
1318 : 1411027 : case ENUMERAL_TYPE:
1319 : 1411027 : case UNION_TYPE:
1320 : 1411027 : case RECORD_TYPE:
1321 : :
1322 : : /* Types may not have tag-names, in which case the type
1323 : : appears in the bindings list with b->id NULL. */
1324 : 1411027 : if (b->id)
1325 : : {
1326 : 618755 : gcc_assert (I_TAG_BINDING (b->id) == b);
1327 : 618755 : I_TAG_BINDING (b->id) = b->shadowed;
1328 : : }
1329 : : break;
1330 : :
1331 : 635607340 : case FUNCTION_DECL:
1332 : : /* Propagate TREE_ADDRESSABLE from nested functions to their
1333 : : containing functions. */
1334 : 635607340 : if (!TREE_ASM_WRITTEN (p)
1335 : 635607340 : && DECL_INITIAL (p) != NULL_TREE
1336 : 71908467 : && TREE_ADDRESSABLE (p)
1337 : 3677495 : && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1338 : 635607340 : && DECL_ABSTRACT_ORIGIN (p) != p)
1339 : 0 : TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1340 : 635607340 : if (!TREE_PUBLIC (p)
1341 : 356159 : && !DECL_INITIAL (p)
1342 : 158 : && !b->nested
1343 : 127 : && scope != file_scope
1344 : 635607348 : && scope != external_scope)
1345 : : {
1346 : 8 : error ("nested function %q+D declared but never defined", p);
1347 : 8 : undef_nested_function = true;
1348 : : }
1349 : 635607332 : else if (DECL_DECLARED_INLINE_P (p)
1350 : 70725175 : && TREE_PUBLIC (p)
1351 : 706174822 : && !DECL_INITIAL (p))
1352 : : {
1353 : : /* C99 6.7.4p6: "a function with external linkage... declared
1354 : : with an inline function specifier ... shall also be defined
1355 : : in the same translation unit." */
1356 : 76 : if (!flag_gnu89_inline
1357 : 51 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1358 : 111 : && scope == external_scope)
1359 : 13 : pedwarn (input_location, 0,
1360 : : "inline function %q+D declared but never defined", p);
1361 : 76 : DECL_EXTERNAL (p) = 1;
1362 : : }
1363 : :
1364 : 635607340 : goto common_symbol;
1365 : :
1366 : 10917495 : case VAR_DECL:
1367 : : /* Warnings for unused variables. */
1368 : 8354156 : if ((!TREE_USED (p) || !DECL_READ_P (p))
1369 : 2591814 : && !warning_suppressed_p (p, OPT_Wunused_but_set_variable_)
1370 : 2591308 : && !DECL_IN_SYSTEM_HEADER (p)
1371 : 2512989 : && DECL_NAME (p)
1372 : 2512989 : && !DECL_ARTIFICIAL (p)
1373 : 2512624 : && scope != file_scope
1374 : 12542002 : && scope != external_scope)
1375 : : {
1376 : 738029 : if (!TREE_USED (p))
1377 : : {
1378 : 732728 : warning (OPT_Wunused_variable, "unused variable %q+D", p);
1379 : 732728 : suppress_warning (p, OPT_Wunused_variable);
1380 : : }
1381 : 5301 : else if (DECL_CONTEXT (p) == current_function_decl)
1382 : 5282 : warning_at (DECL_SOURCE_LOCATION (p),
1383 : 5282 : OPT_Wunused_but_set_variable_,
1384 : : "variable %qD set but not used", p);
1385 : : }
1386 : :
1387 : 10917495 : if (b->inner_comp)
1388 : : {
1389 : 2 : error ("type of array %q+D completed incompatibly with"
1390 : : " implicit initialization", p);
1391 : : }
1392 : :
1393 : : /* Fall through. */
1394 : 663860069 : case TYPE_DECL:
1395 : 663860069 : case CONST_DECL:
1396 : 10917493 : common_symbol:
1397 : : /* All of these go in BLOCK_VARS, but only if this is the
1398 : : binding in the home scope. */
1399 : 663860069 : if (!b->nested)
1400 : : {
1401 : 399537203 : DECL_CHAIN (p) = BLOCK_VARS (block);
1402 : 399537203 : BLOCK_VARS (block) = p;
1403 : : }
1404 : 264322866 : else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1405 : : {
1406 : : /* For block local externs add a special
1407 : : DECL_EXTERNAL decl for debug info generation. */
1408 : 14592 : tree extp = copy_node (p);
1409 : :
1410 : 14592 : DECL_EXTERNAL (extp) = 1;
1411 : 14592 : TREE_STATIC (extp) = 0;
1412 : 14592 : TREE_PUBLIC (extp) = 1;
1413 : 14592 : DECL_INITIAL (extp) = NULL_TREE;
1414 : 14592 : DECL_LANG_SPECIFIC (extp) = NULL;
1415 : 14592 : DECL_CONTEXT (extp) = current_function_decl;
1416 : 14592 : if (TREE_CODE (p) == FUNCTION_DECL)
1417 : : {
1418 : 13055 : DECL_RESULT (extp) = NULL_TREE;
1419 : 13055 : DECL_SAVED_TREE (extp) = NULL_TREE;
1420 : 13055 : DECL_STRUCT_FUNCTION (extp) = NULL;
1421 : : }
1422 : 14592 : if (b->locus != UNKNOWN_LOCATION)
1423 : 14592 : DECL_SOURCE_LOCATION (extp) = b->locus;
1424 : 14592 : DECL_CHAIN (extp) = BLOCK_VARS (block);
1425 : 14592 : BLOCK_VARS (block) = extp;
1426 : : }
1427 : : /* If this is the file scope set DECL_CONTEXT of each decl to
1428 : : the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1429 : : work. */
1430 : 663860069 : if (scope == file_scope)
1431 : 275528814 : DECL_CONTEXT (p) = context;
1432 : :
1433 : 763446926 : gcc_fallthrough ();
1434 : : /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1435 : : already been put there by store_parm_decls. Unused-
1436 : : parameter warnings are handled by function.cc.
1437 : : error_mark_node obviously does not go in BLOCK_VARS and
1438 : : does not get unused-variable warnings. */
1439 : 763446926 : case PARM_DECL:
1440 : 763446926 : case ERROR_MARK:
1441 : : /* It is possible for a decl not to have a name. We get
1442 : : here with b->id NULL in this case. */
1443 : 763446926 : if (b->id)
1444 : : {
1445 : 760254314 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1446 : 760254314 : I_SYMBOL_BINDING (b->id) = b->shadowed;
1447 : 760254314 : if (b->shadowed && b->shadowed->u.type)
1448 : 4606887 : TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1449 : : }
1450 : : break;
1451 : :
1452 : 0 : default:
1453 : 0 : gcc_unreachable ();
1454 : : }
1455 : : }
1456 : :
1457 : :
1458 : : /* Dispose of the block that we just made inside some higher level. */
1459 : 91327651 : if ((scope->function_body || scope == file_scope) && context)
1460 : : {
1461 : 36239981 : DECL_INITIAL (context) = block;
1462 : 36239981 : BLOCK_SUPERCONTEXT (block) = context;
1463 : : }
1464 : 55087670 : else if (scope->outer)
1465 : : {
1466 : 54980315 : if (block)
1467 : 277909 : SCOPE_LIST_APPEND (scope->outer, blocks, block);
1468 : : /* If we did not make a block for the scope just exited, any
1469 : : blocks made for inner scopes must be carried forward so they
1470 : : will later become subblocks of something else. */
1471 : 54702406 : else if (scope->blocks)
1472 : 357212 : SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1473 : : }
1474 : :
1475 : : /* Pop the current scope, and free the structure for reuse. */
1476 : 91327651 : current_scope = scope->outer;
1477 : 91327651 : if (scope->function_body)
1478 : 36132628 : current_function_scope = scope->outer_function;
1479 : :
1480 : 91327651 : memset (scope, 0, sizeof (struct c_scope));
1481 : 91327651 : scope->outer = scope_freelist;
1482 : 91327651 : scope_freelist = scope;
1483 : :
1484 : 91327651 : return block;
1485 : : }
1486 : :
1487 : : void
1488 : 107874 : push_file_scope (void)
1489 : : {
1490 : 107874 : tree decl;
1491 : :
1492 : 107874 : if (file_scope)
1493 : : return;
1494 : :
1495 : 107874 : push_scope ();
1496 : 107874 : file_scope = current_scope;
1497 : :
1498 : 107874 : start_fname_decls ();
1499 : :
1500 : 215341783 : for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1501 : 215233909 : bind (DECL_NAME (decl), decl, file_scope,
1502 : 215233909 : /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1503 : : }
1504 : :
1505 : : void
1506 : 107717 : pop_file_scope (void)
1507 : : {
1508 : : /* In case there were missing closebraces, get us back to the global
1509 : : binding level. */
1510 : 107722 : while (current_scope != file_scope)
1511 : 5 : pop_scope ();
1512 : :
1513 : : /* __FUNCTION__ is defined at file scope (""). This
1514 : : call may not be necessary as my tests indicate it
1515 : : still works without it. */
1516 : 107717 : finish_fname_decls ();
1517 : :
1518 : 107717 : check_inline_statics ();
1519 : :
1520 : : /* This is the point to write out a PCH if we're doing that.
1521 : : In that case we do not want to do anything else. */
1522 : 107717 : if (pch_file)
1523 : : {
1524 : 362 : c_common_write_pch ();
1525 : : /* Ensure even the callers don't try to finalize the CU. */
1526 : 362 : flag_syntax_only = 1;
1527 : 362 : return;
1528 : : }
1529 : :
1530 : : /* Pop off the file scope and close this translation unit. */
1531 : 107355 : pop_scope ();
1532 : 107355 : file_scope = 0;
1533 : :
1534 : 107355 : maybe_apply_pending_pragma_weaks ();
1535 : : }
1536 : :
1537 : : /* Whether we are curently inside the initializer for an
1538 : : underspecified object definition (C23 auto or constexpr). */
1539 : : static bool in_underspecified_init;
1540 : :
1541 : : /* Start an underspecified object definition for NAME at LOC. This
1542 : : means that NAME is shadowed inside its initializer, so neither the
1543 : : definition being initialized, nor any definition from an outer
1544 : : scope, may be referenced during that initializer. Return state to
1545 : : be passed to finish_underspecified_init. If NAME is NULL_TREE, the
1546 : : underspecified object is a (constexpr) compound literal; there is
1547 : : no shadowing in that case, but all the other restrictions on
1548 : : underspecified object definitions still apply. */
1549 : : unsigned int
1550 : 657 : start_underspecified_init (location_t loc, tree name)
1551 : : {
1552 : 657 : bool prev = in_underspecified_init;
1553 : 657 : bool ok;
1554 : 657 : if (name == NULL_TREE)
1555 : : ok = true;
1556 : : else
1557 : : {
1558 : 423 : tree decl = build_decl (loc, VAR_DECL, name, error_mark_node);
1559 : 423 : C_DECL_UNDERSPECIFIED (decl) = 1;
1560 : 423 : struct c_scope *scope = current_scope;
1561 : 423 : struct c_binding *b = I_SYMBOL_BINDING (name);
1562 : 423 : if (b && B_IN_SCOPE (b, scope))
1563 : : {
1564 : 4 : error_at (loc, "underspecified declaration of %qE, which is already "
1565 : : "declared in this scope", name);
1566 : 4 : ok = false;
1567 : : }
1568 : : else
1569 : : {
1570 : 419 : bind (name, decl, scope, false, false, loc);
1571 : 419 : ok = true;
1572 : : }
1573 : : }
1574 : 657 : in_underspecified_init = true;
1575 : 657 : return ok | (prev << 1);
1576 : : }
1577 : :
1578 : : /* Finish an underspecified object definition for NAME, before that
1579 : : name is bound to the real declaration instead of a placeholder.
1580 : : PREV_STATE is the value returned by the call to
1581 : : start_underspecified_init. If NAME is NULL_TREE, this means a
1582 : : compound literal, as for start_underspecified_init. */
1583 : : void
1584 : 657 : finish_underspecified_init (tree name, unsigned int prev_state)
1585 : : {
1586 : 657 : if (name != NULL_TREE && (prev_state & 1))
1587 : : {
1588 : : /* A VAR_DECL was bound to the name to shadow any previous
1589 : : declarations for the name; remove that binding now. */
1590 : 419 : struct c_scope *scope = current_scope;
1591 : 419 : struct c_binding *b = I_SYMBOL_BINDING (name);
1592 : 419 : gcc_assert (b);
1593 : 419 : gcc_assert (B_IN_SCOPE (b, scope));
1594 : 419 : gcc_assert (VAR_P (b->decl));
1595 : 419 : gcc_assert (C_DECL_UNDERSPECIFIED (b->decl));
1596 : 419 : I_SYMBOL_BINDING (name) = b->shadowed;
1597 : : /* In erroneous cases there may be other bindings added to this
1598 : : scope during the initializer. */
1599 : 419 : struct c_binding **p = &scope->bindings;
1600 : 474 : while (*p != b)
1601 : 55 : p = &((*p)->prev);
1602 : 419 : *p = free_binding_and_advance (*p);
1603 : : }
1604 : 657 : in_underspecified_init = (prev_state & (1u << 1)) >> 1;
1605 : 657 : }
1606 : :
1607 : : /* Adjust the bindings for the start of a statement expression. */
1608 : :
1609 : : void
1610 : 34769 : c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1611 : : {
1612 : 34769 : struct c_scope *scope;
1613 : :
1614 : 234880 : for (scope = current_scope; scope != NULL; scope = scope->outer)
1615 : : {
1616 : 200111 : struct c_binding *b;
1617 : :
1618 : 200111 : if (!scope->has_label_bindings)
1619 : 197712 : continue;
1620 : :
1621 : 12608 : for (b = scope->bindings; b != NULL; b = b->prev)
1622 : : {
1623 : 10209 : struct c_label_vars *label_vars;
1624 : 10209 : unsigned int ix;
1625 : 10209 : struct c_goto_bindings *g;
1626 : :
1627 : 10209 : if (TREE_CODE (b->decl) != LABEL_DECL)
1628 : 1460 : continue;
1629 : 8749 : label_vars = b->u.label;
1630 : 8749 : ++label_vars->label_bindings.stmt_exprs;
1631 : 12745 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1632 : 1795 : ++g->goto_bindings.stmt_exprs;
1633 : : }
1634 : : }
1635 : :
1636 : 34769 : if (switch_bindings != NULL)
1637 : 173 : ++switch_bindings->stmt_exprs;
1638 : 34769 : }
1639 : :
1640 : : /* Adjust the bindings for the end of a statement expression. */
1641 : :
1642 : : void
1643 : 34769 : c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1644 : : {
1645 : 34769 : struct c_scope *scope;
1646 : :
1647 : 200111 : for (scope = current_scope; scope != NULL; scope = scope->outer)
1648 : : {
1649 : 165342 : struct c_binding *b;
1650 : :
1651 : 165342 : if (!scope->has_label_bindings)
1652 : 162708 : continue;
1653 : :
1654 : 16084 : for (b = scope->bindings; b != NULL; b = b->prev)
1655 : : {
1656 : 13450 : struct c_label_vars *label_vars;
1657 : 13450 : unsigned int ix;
1658 : 13450 : struct c_goto_bindings *g;
1659 : :
1660 : 13450 : if (TREE_CODE (b->decl) != LABEL_DECL)
1661 : 1520 : continue;
1662 : 11930 : label_vars = b->u.label;
1663 : 11930 : --label_vars->label_bindings.stmt_exprs;
1664 : 11930 : if (label_vars->label_bindings.stmt_exprs < 0)
1665 : : {
1666 : 3322 : label_vars->label_bindings.left_stmt_expr = true;
1667 : 3322 : label_vars->label_bindings.stmt_exprs = 0;
1668 : : }
1669 : 15978 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1670 : : {
1671 : 1791 : --g->goto_bindings.stmt_exprs;
1672 : 1791 : if (g->goto_bindings.stmt_exprs < 0)
1673 : : {
1674 : 128 : g->goto_bindings.left_stmt_expr = true;
1675 : 128 : g->goto_bindings.stmt_exprs = 0;
1676 : : }
1677 : : }
1678 : : }
1679 : : }
1680 : :
1681 : 34769 : if (switch_bindings != NULL)
1682 : : {
1683 : 173 : --switch_bindings->stmt_exprs;
1684 : 173 : gcc_assert (switch_bindings->stmt_exprs >= 0);
1685 : : }
1686 : 34769 : }
1687 : :
1688 : : /* Push a definition or a declaration of struct, union or enum tag "name".
1689 : : "type" should be the type node.
1690 : : We assume that the tag "name" is not already defined, and has a location
1691 : : of LOC.
1692 : :
1693 : : Note that the definition may really be just a forward reference.
1694 : : In that case, the TYPE_SIZE will be zero. */
1695 : :
1696 : : static void
1697 : 1414981 : pushtag (location_t loc, tree name, tree type)
1698 : : {
1699 : : /* Record the identifier as the type's name if it has none. */
1700 : 1414981 : if (name && !TYPE_NAME (type))
1701 : 620553 : TYPE_NAME (type) = name;
1702 : 1414981 : bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1703 : :
1704 : : /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1705 : : tagged type we just added to the current scope. This fake
1706 : : NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1707 : : to output a representation of a tagged type, and it also gives
1708 : : us a convenient place to record the "scope start" address for the
1709 : : tagged type. */
1710 : :
1711 : 1414981 : TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1712 : : TYPE_DECL, NULL_TREE, type));
1713 : :
1714 : : /* An approximation for now, so we can tell this is a function-scope tag.
1715 : : This will be updated in pop_scope. */
1716 : 1414981 : TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1717 : :
1718 : 1414981 : if (warn_cxx_compat && name != NULL_TREE)
1719 : : {
1720 : 628 : struct c_binding *b = I_SYMBOL_BINDING (name);
1721 : :
1722 : 628 : if (b != NULL
1723 : 12 : && b->decl != NULL_TREE
1724 : 12 : && TREE_CODE (b->decl) == TYPE_DECL
1725 : 11 : && (B_IN_CURRENT_SCOPE (b)
1726 : 6 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1727 : 633 : && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1728 : 5 : != TYPE_MAIN_VARIANT (type)))
1729 : : {
1730 : 5 : auto_diagnostic_group d;
1731 : 6 : if (warning_at (loc, OPT_Wc___compat,
1732 : : "using %qD as both a typedef and a tag is "
1733 : : "invalid in C++", b->decl)
1734 : 5 : && b->locus != UNKNOWN_LOCATION)
1735 : 4 : inform (b->locus, "originally defined here");
1736 : 5 : }
1737 : : }
1738 : 1414981 : }
1739 : :
1740 : : /* An exported interface to pushtag. This is used by the gdb plugin's
1741 : : binding oracle to introduce a new tag binding. */
1742 : :
1743 : : void
1744 : 0 : c_pushtag (location_t loc, tree name, tree type)
1745 : : {
1746 : 0 : pushtag (loc, name, type);
1747 : 0 : }
1748 : :
1749 : : /* An exported interface to bind a declaration. LOC is the location
1750 : : to use. DECL is the declaration to bind. The decl's name is used
1751 : : to determine how it is bound. If DECL is a VAR_DECL, then
1752 : : IS_GLOBAL determines whether the decl is put into the global (file
1753 : : and external) scope or the current function's scope; if DECL is not
1754 : : a VAR_DECL then it is always put into the file scope. */
1755 : :
1756 : : void
1757 : 0 : c_bind (location_t loc, tree decl, bool is_global)
1758 : : {
1759 : 0 : struct c_scope *scope;
1760 : 0 : bool nested = false;
1761 : :
1762 : 0 : if (!VAR_P (decl) || current_function_scope == NULL)
1763 : : {
1764 : : /* Types and functions are always considered to be global. */
1765 : 0 : scope = file_scope;
1766 : 0 : DECL_EXTERNAL (decl) = 1;
1767 : 0 : TREE_PUBLIC (decl) = 1;
1768 : : }
1769 : 0 : else if (is_global)
1770 : : {
1771 : : /* Also bind it into the external scope. */
1772 : 0 : bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1773 : 0 : nested = true;
1774 : 0 : scope = file_scope;
1775 : 0 : DECL_EXTERNAL (decl) = 1;
1776 : 0 : TREE_PUBLIC (decl) = 1;
1777 : : }
1778 : : else
1779 : : {
1780 : 0 : DECL_CONTEXT (decl) = current_function_decl;
1781 : 0 : TREE_PUBLIC (decl) = 0;
1782 : 0 : scope = current_function_scope;
1783 : : }
1784 : :
1785 : 0 : bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1786 : 0 : }
1787 : :
1788 : :
1789 : : /* Stores the first FILE*, const struct tm* etc. argument type (whatever
1790 : : it is) seen in a declaration of a file I/O etc. built-in, corresponding
1791 : : to the builtin_structptr_types array. Subsequent declarations of such
1792 : : built-ins are expected to refer to it rather than to fileptr_type_node,
1793 : : etc. which is just void* (or to any other type).
1794 : : Used only by match_builtin_function_types. */
1795 : :
1796 : : static const unsigned builtin_structptr_type_count
1797 : : = ARRAY_SIZE (builtin_structptr_types);
1798 : :
1799 : : static GTY(()) tree last_structptr_types[builtin_structptr_type_count];
1800 : :
1801 : : /* Returns true if types T1 and T2 representing return types or types
1802 : : of function arguments are close enough to be considered interchangeable
1803 : : in redeclarations of built-in functions. */
1804 : :
1805 : : static bool
1806 : 620970 : types_close_enough_to_match (tree t1, tree t2)
1807 : : {
1808 : 620970 : return (TYPE_MODE (t1) == TYPE_MODE (t2)
1809 : 620515 : && POINTER_TYPE_P (t1) == POINTER_TYPE_P (t2)
1810 : 2482362 : && FUNCTION_POINTER_TYPE_P (t1) == FUNCTION_POINTER_TYPE_P (t2));
1811 : : }
1812 : :
1813 : : /* Subroutine of compare_decls. Allow harmless mismatches in return
1814 : : and argument types provided that the type modes match. Set *STRICT
1815 : : and *ARGNO to the expected argument type and number in case of
1816 : : an argument type mismatch or null and zero otherwise. Return
1817 : : a unified type given a suitable match, and 0 otherwise. */
1818 : :
1819 : : static tree
1820 : 144036 : match_builtin_function_types (tree newtype, tree oldtype,
1821 : : tree *strict, unsigned *argno)
1822 : : {
1823 : 144036 : *argno = 0;
1824 : 144036 : *strict = NULL_TREE;
1825 : :
1826 : : /* Accept the return type of the new declaration if it has the same
1827 : : mode and if they're both pointers or if neither is. */
1828 : 144036 : tree oldrettype = TREE_TYPE (oldtype);
1829 : 144036 : tree newrettype = TREE_TYPE (newtype);
1830 : :
1831 : 144036 : if (!types_close_enough_to_match (oldrettype, newrettype))
1832 : : return NULL_TREE;
1833 : :
1834 : : /* Check that the return types are compatible but don't fail if they
1835 : : are not (e.g., int vs long in ILP32) and just let the caller know. */
1836 : 143674 : if (!comptypes (TYPE_MAIN_VARIANT (oldrettype),
1837 : 143674 : TYPE_MAIN_VARIANT (newrettype)))
1838 : 39 : *strict = oldrettype;
1839 : :
1840 : 143674 : tree oldargs = TYPE_ARG_TYPES (oldtype);
1841 : 143674 : tree newargs = TYPE_ARG_TYPES (newtype);
1842 : 143674 : tree tryargs = newargs;
1843 : :
1844 : 143674 : const unsigned nlst = ARRAY_SIZE (last_structptr_types);
1845 : 143674 : const unsigned nbst = ARRAY_SIZE (builtin_structptr_types);
1846 : :
1847 : 143674 : gcc_checking_assert (nlst == nbst);
1848 : :
1849 : 620411 : for (unsigned i = 1; oldargs || newargs; ++i)
1850 : : {
1851 : 476993 : if (!oldargs
1852 : 476993 : || !newargs
1853 : 476938 : || !TREE_VALUE (oldargs)
1854 : 953931 : || !TREE_VALUE (newargs))
1855 : : return NULL_TREE;
1856 : :
1857 : 476938 : tree oldtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1858 : 476938 : tree newtype = TREE_VALUE (newargs);
1859 : 476938 : if (newtype == error_mark_node)
1860 : : return NULL_TREE;
1861 : 476934 : newtype = TYPE_MAIN_VARIANT (newtype);
1862 : :
1863 : 476934 : if (!types_close_enough_to_match (oldtype, newtype))
1864 : : return NULL_TREE;
1865 : :
1866 : 476790 : unsigned j = nbst;
1867 : 476790 : if (POINTER_TYPE_P (oldtype))
1868 : : /* Iterate over well-known struct types like FILE (whose types
1869 : : aren't known to us) and compare the pointer to each to
1870 : : the pointer argument. */
1871 : 973845 : for (j = 0; j < nbst; ++j)
1872 : : {
1873 : 857962 : if (TREE_VALUE (oldargs) != builtin_structptr_types[j].node)
1874 : 714752 : continue;
1875 : : /* Store the first FILE* etc. argument type (whatever it is), and
1876 : : expect any subsequent declarations of file I/O etc. built-ins
1877 : : to refer to it rather than to fileptr_type_node etc. which is
1878 : : just void* (or const void*). */
1879 : 143210 : if (last_structptr_types[j])
1880 : : {
1881 : 125799 : if (!comptypes (last_structptr_types[j], newtype))
1882 : : {
1883 : 2 : *argno = i;
1884 : 2 : *strict = last_structptr_types[j];
1885 : : }
1886 : : }
1887 : : else
1888 : 17411 : last_structptr_types[j] = newtype;
1889 : : break;
1890 : : }
1891 : :
1892 : 476790 : if (j == nbst && !comptypes (oldtype, newtype))
1893 : : {
1894 : 243 : if (POINTER_TYPE_P (oldtype))
1895 : : {
1896 : : /* For incompatible pointers, only reject differences in
1897 : : the unqualified variants of the referenced types but
1898 : : consider differences in qualifiers as benign (report
1899 : : those to caller via *STRICT below). */
1900 : 204 : tree oldref = TYPE_MAIN_VARIANT (TREE_TYPE (oldtype));
1901 : 204 : tree newref = TYPE_MAIN_VARIANT (TREE_TYPE (newtype));
1902 : 204 : if (!comptypes (oldref, newref))
1903 : : return NULL_TREE;
1904 : : }
1905 : :
1906 : 190 : if (!*strict)
1907 : : {
1908 : 186 : *argno = i;
1909 : 186 : *strict = oldtype;
1910 : : }
1911 : : }
1912 : :
1913 : 476737 : oldargs = TREE_CHAIN (oldargs);
1914 : 476737 : newargs = TREE_CHAIN (newargs);
1915 : : }
1916 : :
1917 : 143418 : tree trytype = c_build_function_type (newrettype, tryargs);
1918 : :
1919 : : /* Allow declaration to change transaction_safe attribute. */
1920 : 143418 : tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1921 : 143418 : tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1922 : 143418 : tree newattrs = TYPE_ATTRIBUTES (newtype);
1923 : 143418 : tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1924 : 143418 : if (oldtsafe && !newtsafe)
1925 : 0 : oldattrs = remove_attribute ("transaction_safe", oldattrs);
1926 : 143418 : else if (newtsafe && !oldtsafe)
1927 : 7 : oldattrs = tree_cons (get_identifier ("transaction_safe"),
1928 : : NULL_TREE, oldattrs);
1929 : :
1930 : 143418 : return c_build_type_attribute_variant (trytype, oldattrs);
1931 : : }
1932 : :
1933 : : /* Subroutine of diagnose_mismatched_decls. Check for function type
1934 : : mismatch involving an empty arglist vs a nonempty one and give clearer
1935 : : diagnostics. */
1936 : : static void
1937 : 171 : diagnose_arglist_conflict (tree newdecl, tree olddecl,
1938 : : tree newtype, tree oldtype)
1939 : : {
1940 : 171 : tree t;
1941 : :
1942 : 171 : if (TREE_CODE (olddecl) != FUNCTION_DECL
1943 : 96 : || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1944 : 294 : || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1945 : 65 : || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1946 : 154 : return;
1947 : :
1948 : 17 : t = TYPE_ARG_TYPES (oldtype);
1949 : 17 : if (t == NULL_TREE)
1950 : 9 : t = TYPE_ARG_TYPES (newtype);
1951 : 18 : for (; t; t = TREE_CHAIN (t))
1952 : : {
1953 : 18 : tree type = TREE_VALUE (t);
1954 : :
1955 : 18 : if (TREE_CHAIN (t) == NULL_TREE
1956 : 18 : && TYPE_MAIN_VARIANT (type) != void_type_node)
1957 : : {
1958 : 10 : inform (input_location, "a parameter list with an ellipsis "
1959 : : "cannot match an empty parameter name list declaration");
1960 : 10 : break;
1961 : : }
1962 : :
1963 : 8 : if (!error_operand_p (type)
1964 : 8 : && c_type_promotes_to (type) != type)
1965 : : {
1966 : 7 : inform (input_location, "an argument type that has a default "
1967 : : "promotion cannot match an empty parameter name list "
1968 : : "declaration");
1969 : 7 : break;
1970 : : }
1971 : : }
1972 : : }
1973 : :
1974 : : /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1975 : : old-style function definition, NEWDECL is a prototype declaration.
1976 : : Diagnose inconsistencies in the argument list. Returns TRUE if
1977 : : the prototype is compatible, FALSE if not. */
1978 : : static bool
1979 : 19 : validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1980 : : {
1981 : 19 : tree newargs, oldargs;
1982 : 19 : int i;
1983 : :
1984 : : #define END_OF_ARGLIST(t) ((t) == void_type_node)
1985 : :
1986 : 19 : oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1987 : 19 : newargs = TYPE_ARG_TYPES (newtype);
1988 : 19 : i = 1;
1989 : :
1990 : 45 : for (;;)
1991 : : {
1992 : 32 : tree oldargtype = TREE_VALUE (oldargs);
1993 : 32 : tree newargtype = TREE_VALUE (newargs);
1994 : :
1995 : 32 : if (oldargtype == error_mark_node || newargtype == error_mark_node)
1996 : : return false;
1997 : :
1998 : 58 : oldargtype = (TYPE_ATOMIC (oldargtype)
1999 : 31 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
2000 : : TYPE_QUAL_ATOMIC)
2001 : 27 : : TYPE_MAIN_VARIANT (oldargtype));
2002 : 60 : newargtype = (TYPE_ATOMIC (newargtype)
2003 : 31 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
2004 : : TYPE_QUAL_ATOMIC)
2005 : 29 : : TYPE_MAIN_VARIANT (newargtype));
2006 : :
2007 : 31 : if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
2008 : : break;
2009 : :
2010 : : /* Reaching the end of just one list means the two decls don't
2011 : : agree on the number of arguments. */
2012 : 22 : if (END_OF_ARGLIST (oldargtype))
2013 : : {
2014 : 2 : error ("prototype for %q+D declares more arguments "
2015 : : "than previous old-style definition", newdecl);
2016 : 2 : return false;
2017 : : }
2018 : 20 : else if (END_OF_ARGLIST (newargtype))
2019 : : {
2020 : 2 : error ("prototype for %q+D declares fewer arguments "
2021 : : "than previous old-style definition", newdecl);
2022 : 2 : return false;
2023 : : }
2024 : :
2025 : : /* Type for passing arg must be consistent with that declared
2026 : : for the arg. */
2027 : 18 : else if (!comptypes (oldargtype, newargtype))
2028 : : {
2029 : 5 : error ("prototype for %q+D declares argument %d"
2030 : : " with incompatible type",
2031 : : newdecl, i);
2032 : 5 : return false;
2033 : : }
2034 : :
2035 : 13 : oldargs = TREE_CHAIN (oldargs);
2036 : 13 : newargs = TREE_CHAIN (newargs);
2037 : 13 : i++;
2038 : 13 : }
2039 : :
2040 : : /* If we get here, no errors were found, but do issue a warning
2041 : : for this poor-style construct. */
2042 : 9 : warning (0, "prototype for %q+D follows non-prototype definition",
2043 : : newdecl);
2044 : 9 : return true;
2045 : : #undef END_OF_ARGLIST
2046 : : }
2047 : :
2048 : : /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
2049 : : first in a pair of mismatched declarations, using the diagnostic
2050 : : function DIAG. */
2051 : : static void
2052 : 410 : locate_old_decl (tree decl)
2053 : : {
2054 : 410 : if (TREE_CODE (decl) == FUNCTION_DECL
2055 : 196 : && fndecl_built_in_p (decl)
2056 : 413 : && !C_DECL_DECLARED_BUILTIN (decl))
2057 : : ;
2058 : 410 : else if (DECL_INITIAL (decl))
2059 : 118 : inform (input_location,
2060 : : "previous definition of %q+D with type %qT",
2061 : 118 : decl, TREE_TYPE (decl));
2062 : 292 : else if (C_DECL_IMPLICIT (decl))
2063 : 16 : inform (input_location,
2064 : : "previous implicit declaration of %q+D with type %qT",
2065 : 16 : decl, TREE_TYPE (decl));
2066 : : else
2067 : 276 : inform (input_location,
2068 : : "previous declaration of %q+D with type %qT",
2069 : 276 : decl, TREE_TYPE (decl));
2070 : 410 : }
2071 : :
2072 : :
2073 : : /* Helper function. For a tagged type, it finds the declaration
2074 : : for a visible tag declared in the same scope if such a
2075 : : declaration exists. */
2076 : : static tree
2077 : 1125486 : previous_tag (tree type)
2078 : : {
2079 : 1125486 : struct c_binding *b = NULL;
2080 : 1125486 : tree name = c_type_tag (type);
2081 : :
2082 : 1125486 : if (name)
2083 : 560272 : b = I_TAG_BINDING (name);
2084 : :
2085 : 560272 : if (b)
2086 : 560272 : b = b->shadowed;
2087 : :
2088 : 1125486 : if (b && B_IN_CURRENT_SCOPE (b))
2089 : 173 : return b->decl;
2090 : :
2091 : : return NULL_TREE;
2092 : : }
2093 : :
2094 : : /* Subroutine to mark functions as versioned when using the attribute
2095 : : 'target_version'. */
2096 : :
2097 : : static void
2098 : 0 : maybe_mark_function_versioned (tree decl)
2099 : : {
2100 : 0 : if (!DECL_FUNCTION_VERSIONED (decl))
2101 : : {
2102 : : /* Check if the name of the function has been overridden. */
2103 : 0 : if (DECL_ASSEMBLER_NAME_SET_P (decl)
2104 : 0 : && IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))[0] == '*')
2105 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
2106 : : "cannot use function multiversioning on a renamed function");
2107 : :
2108 : : /* We need to insert function version now to make sure the correct
2109 : : pre-mangled assembler name is recorded. */
2110 : 0 : cgraph_node *node = cgraph_node::get_create (decl);
2111 : :
2112 : 0 : if (!node->function_version ())
2113 : 0 : node->insert_new_function_version ();
2114 : :
2115 : 0 : DECL_FUNCTION_VERSIONED (decl) = 1;
2116 : :
2117 : 0 : tree mangled_name
2118 : 0 : = targetm.mangle_decl_assembler_name (decl, DECL_NAME (decl));
2119 : 0 : SET_DECL_ASSEMBLER_NAME (decl, mangled_name);
2120 : : }
2121 : 0 : }
2122 : :
2123 : : /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
2124 : : Returns true if the caller should proceed to merge the two, false
2125 : : if OLDDECL should simply be discarded. As a side effect, issues
2126 : : all necessary diagnostics for invalid or poor-style combinations.
2127 : : If it returns true, writes the types of NEWDECL and OLDDECL to
2128 : : *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
2129 : : TREE_TYPE (NEWDECL, OLDDECL) respectively. */
2130 : :
2131 : : static bool
2132 : 4931042 : diagnose_mismatched_decls (tree newdecl, tree olddecl,
2133 : : tree *newtypep, tree *oldtypep)
2134 : : {
2135 : 4931042 : tree newtype, oldtype;
2136 : 4931042 : bool retval = true;
2137 : :
2138 : : #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
2139 : : && DECL_EXTERNAL (DECL))
2140 : :
2141 : : /* If we have error_mark_node for either decl or type, just discard
2142 : : the previous decl - we're in an error cascade already. */
2143 : 4931042 : if (olddecl == error_mark_node || newdecl == error_mark_node)
2144 : : return false;
2145 : 4931022 : *oldtypep = oldtype = TREE_TYPE (olddecl);
2146 : 4931022 : *newtypep = newtype = TREE_TYPE (newdecl);
2147 : 4931022 : if (oldtype == error_mark_node || newtype == error_mark_node)
2148 : : return false;
2149 : :
2150 : : /* Two different categories of symbol altogether. This is an error
2151 : : unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
2152 : 4931014 : if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2153 : : {
2154 : 29 : if (!(TREE_CODE (olddecl) == FUNCTION_DECL
2155 : 18 : && fndecl_built_in_p (olddecl)
2156 : 15 : && !C_DECL_DECLARED_BUILTIN (olddecl)))
2157 : : {
2158 : 15 : auto_diagnostic_group d;
2159 : 15 : error ("%q+D redeclared as different kind of symbol", newdecl);
2160 : 15 : locate_old_decl (olddecl);
2161 : 15 : }
2162 : 14 : else if (TREE_PUBLIC (newdecl))
2163 : 3 : warning (OPT_Wbuiltin_declaration_mismatch,
2164 : : "built-in function %q+D declared as non-function",
2165 : : newdecl);
2166 : : else
2167 : 11 : warning (OPT_Wshadow, "declaration of %q+D shadows "
2168 : : "a built-in function", newdecl);
2169 : 29 : return false;
2170 : : }
2171 : :
2172 : : /* Enumerators have no linkage, so may only be declared once in a
2173 : : given scope. */
2174 : 4930985 : if (TREE_CODE (olddecl) == CONST_DECL)
2175 : : {
2176 : 46 : if (flag_isoc23
2177 : 45 : && TYPE_NAME (DECL_CONTEXT (newdecl))
2178 : 43 : && DECL_CONTEXT (newdecl) != DECL_CONTEXT (olddecl)
2179 : 87 : && TYPE_NAME (DECL_CONTEXT (newdecl)) == TYPE_NAME (DECL_CONTEXT (olddecl)))
2180 : : {
2181 : 38 : if (!simple_cst_equal (DECL_INITIAL (olddecl), DECL_INITIAL (newdecl)))
2182 : : {
2183 : 1 : auto_diagnostic_group d;
2184 : 1 : error ("conflicting redeclaration of enumerator %q+D", newdecl);
2185 : 1 : locate_old_decl (olddecl);
2186 : 1 : }
2187 : : }
2188 : : else
2189 : : {
2190 : 8 : auto_diagnostic_group d;
2191 : 8 : error ("redeclaration of enumerator %q+D", newdecl);
2192 : 8 : locate_old_decl (olddecl);
2193 : 8 : }
2194 : 46 : return false;
2195 : : }
2196 : :
2197 : 4930939 : bool pedwarned = false;
2198 : 4930939 : bool warned = false;
2199 : 4930939 : bool enum_and_int_p = false;
2200 : 4930939 : auto_diagnostic_group d;
2201 : :
2202 : 4930939 : int comptypes_result = comptypes_check_enum_int (oldtype, newtype,
2203 : : &enum_and_int_p);
2204 : 4930939 : if (!comptypes_result)
2205 : : {
2206 : 144238 : if (TREE_CODE (olddecl) == FUNCTION_DECL
2207 : 144163 : && fndecl_built_in_p (olddecl, BUILT_IN_NORMAL)
2208 : 288275 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2209 : : {
2210 : : /* Accept "harmless" mismatches in function types such
2211 : : as missing qualifiers or int vs long when they're the same
2212 : : size. However, diagnose return and argument types that are
2213 : : incompatible according to language rules. */
2214 : 144036 : tree mismatch_expect;
2215 : 144036 : unsigned mismatch_argno;
2216 : :
2217 : 144036 : tree trytype = match_builtin_function_types (newtype, oldtype,
2218 : : &mismatch_expect,
2219 : : &mismatch_argno);
2220 : :
2221 : 144036 : if (trytype && comptypes (newtype, trytype))
2222 : 143418 : *oldtypep = oldtype = trytype;
2223 : : else
2224 : : {
2225 : : /* If types don't match for a built-in, throw away the
2226 : : built-in. No point in calling locate_old_decl here, it
2227 : : won't print anything. */
2228 : 618 : const char *header = header_for_builtin_fn (olddecl);
2229 : 618 : location_t loc = DECL_SOURCE_LOCATION (newdecl);
2230 : 1019 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
2231 : : "conflicting types for built-in function %q+D; "
2232 : : "expected %qT",
2233 : : newdecl, oldtype)
2234 : 618 : && header)
2235 : : {
2236 : : /* Suggest the right header to include as the preferred
2237 : : solution rather than the spelling of the declaration. */
2238 : 217 : rich_location richloc (line_table, loc);
2239 : 217 : maybe_add_include_fixit (&richloc, header, true);
2240 : 217 : inform (&richloc,
2241 : : "%qD is declared in header %qs", olddecl, header);
2242 : 217 : }
2243 : 618 : return false;
2244 : : }
2245 : :
2246 : 143418 : if (mismatch_expect && extra_warnings)
2247 : : {
2248 : 5 : location_t newloc = DECL_SOURCE_LOCATION (newdecl);
2249 : 5 : bool warned = false;
2250 : 5 : if (mismatch_argno)
2251 : 5 : warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2252 : : "mismatch in argument %u type of built-in "
2253 : : "function %qD; expected %qT",
2254 : : mismatch_argno, newdecl, mismatch_expect);
2255 : : else
2256 : 0 : warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2257 : : "mismatch in return type of built-in "
2258 : : "function %qD; expected %qT",
2259 : : newdecl, mismatch_expect);
2260 : 5 : const char *header = header_for_builtin_fn (olddecl);
2261 : 5 : if (warned && header)
2262 : : {
2263 : 5 : rich_location richloc (line_table, newloc);
2264 : 5 : maybe_add_include_fixit (&richloc, header, true);
2265 : 5 : inform (&richloc,
2266 : : "%qD is declared in header %qs", olddecl, header);
2267 : 5 : }
2268 : : }
2269 : : }
2270 : 202 : else if (TREE_CODE (olddecl) == FUNCTION_DECL
2271 : 202 : && DECL_IS_UNDECLARED_BUILTIN (olddecl))
2272 : : {
2273 : : /* A conflicting function declaration for a predeclared
2274 : : function that isn't actually built in. Objective C uses
2275 : : these. The new declaration silently overrides everything
2276 : : but the volatility (i.e. noreturn) indication. See also
2277 : : below. FIXME: Make Objective C use normal builtins. */
2278 : 0 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2279 : 0 : return false;
2280 : : }
2281 : : /* Permit void foo (...) to match int foo (...) if the latter is
2282 : : the definition and implicit int was used. See
2283 : : c-torture/compile/920625-2.c. */
2284 : 127 : else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
2285 : 63 : && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
2286 : 32 : && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
2287 : 208 : && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
2288 : : {
2289 : 5 : pedwarned = pedwarn (input_location, 0,
2290 : : "conflicting types for %q+D", newdecl);
2291 : : /* Make sure we keep void as the return type. */
2292 : 5 : TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
2293 : 5 : C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
2294 : : }
2295 : : /* Permit void foo (...) to match an earlier call to foo (...) with
2296 : : no declared type (thus, implicitly int). */
2297 : 197 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2298 : 122 : && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
2299 : 91 : && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
2300 : 220 : && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
2301 : : {
2302 : 19 : pedwarned = pedwarn (input_location, 0,
2303 : : "conflicting types for %q+D; have %qT",
2304 : : newdecl, newtype);
2305 : : /* Make sure we keep void as the return type. */
2306 : 19 : TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
2307 : : }
2308 : : else
2309 : : {
2310 : 178 : int new_quals = TYPE_QUALS (newtype);
2311 : 178 : int old_quals = TYPE_QUALS (oldtype);
2312 : :
2313 : 178 : if (new_quals != old_quals)
2314 : : {
2315 : 20 : addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
2316 : 20 : addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
2317 : 20 : if (new_addr != old_addr)
2318 : : {
2319 : 0 : if (ADDR_SPACE_GENERIC_P (new_addr))
2320 : 0 : error ("conflicting named address spaces (generic vs %s) "
2321 : : "for %q+D",
2322 : : c_addr_space_name (old_addr), newdecl);
2323 : 0 : else if (ADDR_SPACE_GENERIC_P (old_addr))
2324 : 0 : error ("conflicting named address spaces (%s vs generic) "
2325 : : "for %q+D",
2326 : : c_addr_space_name (new_addr), newdecl);
2327 : : else
2328 : 0 : error ("conflicting named address spaces (%s vs %s) "
2329 : : "for %q+D",
2330 : : c_addr_space_name (new_addr),
2331 : : c_addr_space_name (old_addr),
2332 : : newdecl);
2333 : : }
2334 : :
2335 : 20 : if (CLEAR_QUAL_ADDR_SPACE (new_quals)
2336 : 20 : != CLEAR_QUAL_ADDR_SPACE (old_quals))
2337 : 20 : error ("conflicting type qualifiers for %q+D", newdecl);
2338 : : }
2339 : : else
2340 : : {
2341 : 158 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
2342 : : {
2343 : 91 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (olddecl));
2344 : 91 : if (attrs && !TYPE_ATTRIBUTES (TREE_TYPE (newdecl)))
2345 : : {
2346 : : /* Similar to the C++ front-end, for FUNCTION_DECL,
2347 : : if OLDDECL has attributes and NEWDECL doesn't,
2348 : : try the type with OLDDECL attributes. */
2349 : 12 : tree rettype = TREE_TYPE (newtype);
2350 : 12 : tree tryargs = TYPE_ARG_TYPES (newtype);
2351 : 12 : tree trytype = c_build_function_type (rettype,
2352 : : tryargs);
2353 : 12 : trytype = c_build_type_attribute_variant (trytype,
2354 : : attrs);
2355 : 12 : if (comptypes (oldtype, trytype))
2356 : : {
2357 : 7 : *newtypep = newtype = trytype;
2358 : 7 : comptypes_result = 1;
2359 : : }
2360 : : }
2361 : : }
2362 : :
2363 : 158 : if (!comptypes_result)
2364 : 151 : error ("conflicting types for %q+D; have %qT", newdecl,
2365 : : newtype);
2366 : : }
2367 : 7 : if (!comptypes_result)
2368 : : {
2369 : 171 : diagnose_arglist_conflict (newdecl, olddecl, newtype,
2370 : : oldtype);
2371 : 171 : locate_old_decl (olddecl);
2372 : 171 : return false;
2373 : : }
2374 : : }
2375 : : }
2376 : : /* Warn about enum/integer type mismatches. They are compatible types
2377 : : (C23 6.7.2.2/5), but may pose portability problems. */
2378 : 4786701 : else if (enum_and_int_p
2379 : 193 : && TREE_CODE (newdecl) != TYPE_DECL
2380 : : /* Don't warn about acc_on_device built-in redeclaration,
2381 : : the built-in is declared with int rather than enum because
2382 : : the enum isn't intrinsic. */
2383 : 4786890 : && !(TREE_CODE (olddecl) == FUNCTION_DECL
2384 : 152 : && fndecl_built_in_p (olddecl, BUILT_IN_ACC_ON_DEVICE)
2385 : 122 : && !C_DECL_DECLARED_BUILTIN (olddecl)))
2386 : 67 : warned = warning_at (DECL_SOURCE_LOCATION (newdecl),
2387 : 67 : OPT_Wenum_int_mismatch,
2388 : : "conflicting types for %q+D due to enum/integer "
2389 : : "mismatch; have %qT", newdecl, newtype);
2390 : :
2391 : : /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2392 : : but silently ignore the redeclaration if either is in a system
2393 : : header. (Conflicting redeclarations were handled above.) This
2394 : : is allowed for C11 if the types are the same, not just
2395 : : compatible. */
2396 : 4930150 : if (TREE_CODE (newdecl) == TYPE_DECL)
2397 : : {
2398 : 45673 : bool types_different = false;
2399 : :
2400 : 45673 : comptypes_result
2401 : 45673 : = comptypes_check_different_types (oldtype, newtype, &types_different);
2402 : :
2403 : 45673 : if (comptypes_result != 1 || types_different)
2404 : : {
2405 : 11 : error ("redefinition of typedef %q+D with different type", newdecl);
2406 : 11 : locate_old_decl (olddecl);
2407 : 11 : return false;
2408 : : }
2409 : :
2410 : 45662 : if (DECL_IN_SYSTEM_HEADER (newdecl)
2411 : 2461 : || DECL_IN_SYSTEM_HEADER (olddecl)
2412 : 2242 : || warning_suppressed_p (newdecl, OPT_Wpedantic)
2413 : 47904 : || warning_suppressed_p (olddecl, OPT_Wpedantic))
2414 : 43420 : return true; /* Allow OLDDECL to continue in use. */
2415 : :
2416 : 2242 : if (c_type_variably_modified_p (newtype))
2417 : : {
2418 : 3 : error ("redefinition of typedef %q+D with variably modified type",
2419 : : newdecl);
2420 : 3 : locate_old_decl (olddecl);
2421 : : }
2422 : 2239 : else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2423 : : "redefinition of typedef %q+D", newdecl))
2424 : 8 : locate_old_decl (olddecl);
2425 : :
2426 : 2242 : return true;
2427 : : }
2428 : :
2429 : : /* Function declarations can either be 'static' or 'extern' (no
2430 : : qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2431 : : can never conflict with each other on account of linkage
2432 : : (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2433 : : gnu89 mode permits two definitions if one is 'extern inline' and
2434 : : one is not. The non- extern-inline definition supersedes the
2435 : : extern-inline definition. */
2436 : :
2437 : 4884477 : else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2438 : : {
2439 : : /* If you declare a built-in function name as static, or
2440 : : define the built-in with an old-style definition (so we
2441 : : can't validate the argument list) the built-in definition is
2442 : : overridden, but optionally warn this was a bad choice of name. */
2443 : 4866016 : if (fndecl_built_in_p (olddecl)
2444 : 8698468 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2445 : : {
2446 : 3575848 : if (!TREE_PUBLIC (newdecl)
2447 : 3575848 : || (DECL_INITIAL (newdecl)
2448 : 4855 : && !prototype_p (TREE_TYPE (newdecl))))
2449 : : {
2450 : 103 : warning_at (DECL_SOURCE_LOCATION (newdecl),
2451 : 103 : OPT_Wshadow, "declaration of %qD shadows "
2452 : : "a built-in function", newdecl);
2453 : : /* Discard the old built-in function. */
2454 : 103 : return false;
2455 : : }
2456 : :
2457 : 3575745 : if (!prototype_p (TREE_TYPE (newdecl)))
2458 : : {
2459 : : /* Set for built-ins that take no arguments. */
2460 : 342 : bool func_void_args = false;
2461 : 342 : if (tree at = TYPE_ARG_TYPES (oldtype))
2462 : 342 : func_void_args = VOID_TYPE_P (TREE_VALUE (at));
2463 : :
2464 : 342 : if (extra_warnings && !func_void_args)
2465 : 47 : warning_at (DECL_SOURCE_LOCATION (newdecl),
2466 : 47 : OPT_Wbuiltin_declaration_mismatch,
2467 : : "declaration of built-in function %qD without "
2468 : : "a prototype; expected %qT",
2469 : 47 : newdecl, TREE_TYPE (olddecl));
2470 : : }
2471 : : }
2472 : :
2473 : 4865913 : if (DECL_INITIAL (newdecl))
2474 : : {
2475 : 230760 : if (DECL_INITIAL (olddecl))
2476 : : {
2477 : : /* If the new declaration isn't overriding an extern inline
2478 : : reject the new decl. In c99, no overriding is allowed
2479 : : in the same translation unit. */
2480 : 209 : if (!DECL_EXTERN_INLINE (olddecl)
2481 : 91 : || DECL_EXTERN_INLINE (newdecl)
2482 : 204 : || (!flag_gnu89_inline
2483 : 15 : && (!DECL_DECLARED_INLINE_P (olddecl)
2484 : 15 : || !lookup_attribute ("gnu_inline",
2485 : 15 : DECL_ATTRIBUTES (olddecl)))
2486 : 0 : && (!DECL_DECLARED_INLINE_P (newdecl)
2487 : 0 : || !lookup_attribute ("gnu_inline",
2488 : 0 : DECL_ATTRIBUTES (newdecl)))))
2489 : : {
2490 : 26 : auto_diagnostic_group d;
2491 : 26 : error ("redefinition of %q+D", newdecl);
2492 : 26 : locate_old_decl (olddecl);
2493 : 26 : return false;
2494 : 26 : }
2495 : : }
2496 : : }
2497 : : /* If we have a prototype after an old-style function definition,
2498 : : the argument types must be checked specially. */
2499 : 4635153 : else if (DECL_INITIAL (olddecl)
2500 : 824 : && !prototype_p (oldtype) && prototype_p (newtype)
2501 : 4635173 : && TYPE_ACTUAL_ARG_TYPES (oldtype))
2502 : : {
2503 : 19 : auto_diagnostic_group d;
2504 : 19 : if (!validate_proto_after_old_defn (newdecl, newtype, oldtype))
2505 : : {
2506 : 10 : locate_old_decl (olddecl);
2507 : 10 : return false;
2508 : : }
2509 : 19 : }
2510 : : /* A non-static declaration (even an "extern") followed by a
2511 : : static declaration is undefined behavior per C99 6.2.2p3-5,7.
2512 : : The same is true for a static forward declaration at block
2513 : : scope followed by a non-static declaration/definition at file
2514 : : scope. Static followed by non-static at the same scope is
2515 : : not undefined behavior, and is the most convenient way to get
2516 : : some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2517 : : the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2518 : : we do diagnose it if -Wtraditional. */
2519 : 4865877 : if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2520 : : {
2521 : : /* Two exceptions to the rule. If olddecl is an extern
2522 : : inline, or a predeclared function that isn't actually
2523 : : built in, newdecl silently overrides olddecl. The latter
2524 : : occur only in Objective C; see also above. (FIXME: Make
2525 : : Objective C use normal builtins.) */
2526 : 19 : if (!DECL_IS_UNDECLARED_BUILTIN (olddecl)
2527 : 38 : && !DECL_EXTERN_INLINE (olddecl))
2528 : : {
2529 : 5 : auto_diagnostic_group d;
2530 : 5 : error ("static declaration of %q+D follows "
2531 : : "non-static declaration", newdecl);
2532 : 5 : locate_old_decl (olddecl);
2533 : 5 : }
2534 : 19 : return false;
2535 : : }
2536 : 4865858 : else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2537 : : {
2538 : 2326 : if (DECL_CONTEXT (olddecl))
2539 : : {
2540 : 0 : auto_diagnostic_group d;
2541 : 0 : error ("non-static declaration of %q+D follows "
2542 : : "static declaration", newdecl);
2543 : 0 : locate_old_decl (olddecl);
2544 : 0 : return false;
2545 : 0 : }
2546 : 2326 : else if (warn_traditional)
2547 : : {
2548 : 2 : warned |= warning (OPT_Wtraditional,
2549 : : "non-static declaration of %q+D "
2550 : : "follows static declaration", newdecl);
2551 : : }
2552 : : }
2553 : :
2554 : : /* Make sure gnu_inline attribute is either not present, or
2555 : : present on all inline decls. */
2556 : 4865858 : if (DECL_DECLARED_INLINE_P (olddecl)
2557 : 4866933 : && DECL_DECLARED_INLINE_P (newdecl))
2558 : : {
2559 : 820 : bool newa = lookup_attribute ("gnu_inline",
2560 : 820 : DECL_ATTRIBUTES (newdecl)) != NULL;
2561 : 820 : bool olda = lookup_attribute ("gnu_inline",
2562 : 820 : DECL_ATTRIBUTES (olddecl)) != NULL;
2563 : 820 : if (newa != olda)
2564 : : {
2565 : 0 : auto_diagnostic_group d;
2566 : 0 : error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2567 : : newa ? newdecl : olddecl);
2568 : 0 : error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2569 : : "but not here");
2570 : 0 : }
2571 : : }
2572 : : /* Check if these are unmergable overlapping FMV declarations. */
2573 : : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
2574 : : && diagnose_versioned_decls (olddecl, newdecl))
2575 : : return false;
2576 : : }
2577 : 18461 : else if (VAR_P (newdecl))
2578 : : {
2579 : : /* Only variables can be thread-local, and all declarations must
2580 : : agree on this property. */
2581 : 18424 : if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2582 : : {
2583 : : /* Nothing to check. Since OLDDECL is marked threadprivate
2584 : : and NEWDECL does not have a thread-local attribute, we
2585 : : will merge the threadprivate attribute into NEWDECL. */
2586 : : ;
2587 : : }
2588 : 55059 : else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2589 : : {
2590 : 6 : auto_diagnostic_group d;
2591 : 6 : if (DECL_THREAD_LOCAL_P (newdecl))
2592 : 3 : error ("thread-local declaration of %q+D follows "
2593 : : "non-thread-local declaration", newdecl);
2594 : : else
2595 : 3 : error ("non-thread-local declaration of %q+D follows "
2596 : : "thread-local declaration", newdecl);
2597 : :
2598 : 6 : locate_old_decl (olddecl);
2599 : 6 : return false;
2600 : 6 : }
2601 : :
2602 : : /* Multiple initialized definitions are not allowed (6.9p3,5).
2603 : : For this purpose, C23 makes it clear that thread-local
2604 : : declarations without extern are definitions, not tentative
2605 : : definitions, whether or not they have initializers. The
2606 : : wording before C23 was unclear; literally it would have made
2607 : : uninitialized thread-local declarations into tentative
2608 : : definitions only if they also used static, but without saying
2609 : : explicitly whether or not other cases count as
2610 : : definitions at all. */
2611 : 18947 : if ((DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2612 : 18946 : || (flag_isoc23
2613 : 6252 : && DECL_THREAD_LOCAL_P (newdecl)
2614 : 25 : && !DECL_EXTERNAL (newdecl)
2615 : 21 : && !DECL_EXTERNAL (olddecl)))
2616 : : {
2617 : 7 : auto_diagnostic_group d;
2618 : 7 : error ("redefinition of %q+D", newdecl);
2619 : 7 : locate_old_decl (olddecl);
2620 : 7 : return false;
2621 : 7 : }
2622 : :
2623 : : /* Objects declared at file scope: if the first declaration had
2624 : : external linkage (even if it was an external reference) the
2625 : : second must have external linkage as well, or the behavior is
2626 : : undefined. If the first declaration had internal linkage, then
2627 : : the second must too, or else be an external reference (in which
2628 : : case the composite declaration still has internal linkage).
2629 : : As for function declarations, we warn about the static-then-
2630 : : extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2631 : 18428 : if (DECL_FILE_SCOPE_P (newdecl)
2632 : 18411 : && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2633 : : {
2634 : 147 : if (DECL_EXTERNAL (newdecl))
2635 : : {
2636 : 144 : if (!DECL_FILE_SCOPE_P (olddecl))
2637 : : {
2638 : 2 : auto_diagnostic_group d;
2639 : 2 : error ("extern declaration of %q+D follows "
2640 : : "declaration with no linkage", newdecl);
2641 : 2 : locate_old_decl (olddecl);
2642 : 2 : return false;
2643 : 2 : }
2644 : 142 : else if (warn_traditional)
2645 : : {
2646 : 0 : warned |= warning (OPT_Wtraditional,
2647 : : "non-static declaration of %q+D "
2648 : : "follows static declaration", newdecl);
2649 : : }
2650 : : }
2651 : : else
2652 : : {
2653 : 3 : auto_diagnostic_group d;
2654 : 3 : if (TREE_PUBLIC (newdecl))
2655 : 2 : error ("non-static declaration of %q+D follows "
2656 : : "static declaration", newdecl);
2657 : : else
2658 : 1 : error ("static declaration of %q+D follows "
2659 : : "non-static declaration", newdecl);
2660 : :
2661 : 3 : locate_old_decl (olddecl);
2662 : 3 : return false;
2663 : 3 : }
2664 : : }
2665 : : /* Two objects with the same name declared at the same block
2666 : : scope must both be external references (6.7p3). */
2667 : 18264 : else if (!DECL_FILE_SCOPE_P (newdecl))
2668 : : {
2669 : 17 : if (DECL_EXTERNAL (newdecl))
2670 : : {
2671 : : /* Extern with initializer at block scope, which will
2672 : : already have received an error. */
2673 : : }
2674 : 15 : else if (DECL_EXTERNAL (olddecl))
2675 : : {
2676 : 4 : auto_diagnostic_group d;
2677 : 4 : error ("declaration of %q+D with no linkage follows "
2678 : : "extern declaration", newdecl);
2679 : 4 : locate_old_decl (olddecl);
2680 : 4 : }
2681 : : else
2682 : : {
2683 : 11 : auto_diagnostic_group d;
2684 : 11 : error ("redeclaration of %q+D with no linkage", newdecl);
2685 : 11 : locate_old_decl (olddecl);
2686 : 11 : }
2687 : :
2688 : 17 : return false;
2689 : : }
2690 : :
2691 : : /* C++ does not permit a decl to appear multiple times at file
2692 : : scope. */
2693 : 18389 : if (warn_cxx_compat
2694 : 68 : && DECL_FILE_SCOPE_P (newdecl)
2695 : 68 : && !DECL_EXTERNAL (newdecl)
2696 : 18407 : && !DECL_EXTERNAL (olddecl))
2697 : 5 : warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2698 : 5 : OPT_Wc___compat,
2699 : : "duplicate declaration of %qD is "
2700 : : "invalid in C++", newdecl);
2701 : : }
2702 : :
2703 : : /* warnings */
2704 : : /* All decls must agree on a visibility. */
2705 : 4884284 : if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2706 : 4884247 : && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2707 : 4885027 : && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2708 : : {
2709 : 1 : warned |= warning (0, "redeclaration of %q+D with different visibility "
2710 : : "(old visibility preserved)", newdecl);
2711 : : }
2712 : :
2713 : 4884284 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2714 : 4865858 : warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2715 : : else /* PARM_DECL, VAR_DECL */
2716 : : {
2717 : : /* Redeclaration of a parameter is a constraint violation (this is
2718 : : not explicitly stated, but follows from C99 6.7p3 [no more than
2719 : : one declaration of the same identifier with no linkage in the
2720 : : same scope, except type tags] and 6.2.2p6 [parameters have no
2721 : : linkage]). We must check for a forward parameter declaration,
2722 : : indicated by TREE_ASM_WRITTEN on the old declaration - this is
2723 : : an extension, the mandatory diagnostic for which is handled by
2724 : : mark_forward_parm_decls. */
2725 : :
2726 : 18426 : if (TREE_CODE (newdecl) == PARM_DECL
2727 : 37 : && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2728 : : {
2729 : 2 : auto_diagnostic_group d;
2730 : 2 : error ("redefinition of parameter %q+D", newdecl);
2731 : 2 : locate_old_decl (olddecl);
2732 : 2 : return false;
2733 : 2 : }
2734 : : }
2735 : :
2736 : : /* Optional warning for completely redundant decls. */
2737 : 4884282 : if (!warned && !pedwarned
2738 : 4884208 : && warn_redundant_decls
2739 : : /* Don't warn about a function declaration followed by a
2740 : : definition. */
2741 : 18 : && !(TREE_CODE (newdecl) == FUNCTION_DECL
2742 : 2 : && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2743 : : /* Don't warn about redundant redeclarations of builtins. */
2744 : 18 : && !(TREE_CODE (newdecl) == FUNCTION_DECL
2745 : 2 : && !fndecl_built_in_p (newdecl)
2746 : 2 : && fndecl_built_in_p (olddecl)
2747 : 2 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2748 : : /* Don't warn about an extern followed by a definition. */
2749 : 17 : && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2750 : : /* Don't warn about forward parameter decls. */
2751 : 17 : && !(TREE_CODE (newdecl) == PARM_DECL
2752 : 6 : && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2753 : : /* Don't warn about a variable definition following a declaration. */
2754 : 4884293 : && !(VAR_P (newdecl)
2755 : 10 : && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2756 : : {
2757 : 6 : warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2758 : : newdecl);
2759 : : }
2760 : :
2761 : : /* Report location of previous decl/defn. */
2762 : 4884282 : if (warned || pedwarned)
2763 : 80 : locate_old_decl (olddecl);
2764 : :
2765 : : #undef DECL_EXTERN_INLINE
2766 : :
2767 : : return retval;
2768 : 4930939 : }
2769 : :
2770 : : /* Subroutine of duplicate_decls. NEWDECL has been found to be
2771 : : consistent with OLDDECL, but carries new information. Merge the
2772 : : new information into OLDDECL. This function issues no
2773 : : diagnostics. */
2774 : :
2775 : : static void
2776 : 4929944 : merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2777 : : {
2778 : 4929944 : bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2779 : 4929944 : && DECL_INITIAL (newdecl) != NULL_TREE);
2780 : 4929944 : bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2781 : 4929944 : && prototype_p (TREE_TYPE (newdecl)));
2782 : 4929944 : bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2783 : 4929944 : && prototype_p (TREE_TYPE (olddecl)));
2784 : :
2785 : : /* For real parm decl following a forward decl, rechain the old decl
2786 : : in its new location and clear TREE_ASM_WRITTEN (it's not a
2787 : : forward decl anymore). */
2788 : 4929944 : if (TREE_CODE (newdecl) == PARM_DECL
2789 : 35 : && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2790 : : {
2791 : 35 : struct c_binding *b, **here;
2792 : :
2793 : 54 : for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
2794 : 54 : if ((*here)->decl == olddecl)
2795 : 35 : goto found;
2796 : 0 : gcc_unreachable ();
2797 : :
2798 : 35 : found:
2799 : 35 : b = *here;
2800 : 35 : *here = b->prev;
2801 : 35 : b->prev = current_scope->bindings;
2802 : 35 : current_scope->bindings = b;
2803 : :
2804 : 35 : TREE_ASM_WRITTEN (olddecl) = 0;
2805 : : }
2806 : :
2807 : 4929944 : DECL_ATTRIBUTES (newdecl)
2808 : 4929944 : = targetm.merge_decl_attributes (olddecl, newdecl);
2809 : :
2810 : : /* For typedefs use the old type, as the new type's DECL_NAME points
2811 : : at newdecl, which will be ggc_freed. */
2812 : 4929944 : if (TREE_CODE (newdecl) == TYPE_DECL)
2813 : : {
2814 : : /* But NEWTYPE might have an attribute, honor that. */
2815 : 45662 : tree tem = newtype;
2816 : 45662 : newtype = oldtype;
2817 : :
2818 : 45662 : if (TYPE_USER_ALIGN (tem))
2819 : : {
2820 : 12 : if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2821 : 6 : SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2822 : 12 : TYPE_USER_ALIGN (newtype) = true;
2823 : : }
2824 : :
2825 : : /* And remove the new type from the variants list. */
2826 : 45662 : if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2827 : : {
2828 : 12 : tree remove = TREE_TYPE (newdecl);
2829 : 12 : if (TYPE_MAIN_VARIANT (remove) == remove)
2830 : : {
2831 : 2 : gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2832 : : /* If remove is the main variant, no need to remove that
2833 : : from the list. One of the DECL_ORIGINAL_TYPE
2834 : : variants, e.g. created for aligned attribute, might still
2835 : : refer to the newdecl TYPE_DECL though, so remove that one
2836 : : in that case. */
2837 : 2 : if (DECL_ORIGINAL_TYPE (newdecl)
2838 : 2 : && DECL_ORIGINAL_TYPE (newdecl) != remove)
2839 : 2 : for (tree t = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl));
2840 : 2 : t; t = TYPE_MAIN_VARIANT (t))
2841 : 2 : if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2842 : : {
2843 : 4 : TYPE_NEXT_VARIANT (t)
2844 : 2 : = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2845 : 2 : break;
2846 : : }
2847 : : }
2848 : : else
2849 : 10 : for (tree t = TYPE_MAIN_VARIANT (remove); ;
2850 : 0 : t = TYPE_NEXT_VARIANT (t))
2851 : 10 : if (TYPE_NEXT_VARIANT (t) == remove)
2852 : : {
2853 : 10 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2854 : 10 : break;
2855 : : }
2856 : : }
2857 : :
2858 : : /* Make sure we refer to the same type as the olddecl. */
2859 : 45662 : DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2860 : : }
2861 : :
2862 : : /* Merge the data types specified in the two decls. */
2863 : 9859888 : TREE_TYPE (newdecl)
2864 : 4929944 : = TREE_TYPE (olddecl)
2865 : 9859888 : = composite_type (newtype, oldtype);
2866 : :
2867 : : /* Lay the type out, unless already done. */
2868 : 4929944 : if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2869 : : {
2870 : 0 : if (TREE_TYPE (newdecl) != error_mark_node)
2871 : 0 : layout_type (TREE_TYPE (newdecl));
2872 : 0 : if (TREE_CODE (newdecl) != FUNCTION_DECL
2873 : : && TREE_CODE (newdecl) != TYPE_DECL
2874 : : && TREE_CODE (newdecl) != CONST_DECL)
2875 : 0 : layout_decl (newdecl, 0);
2876 : : }
2877 : : else
2878 : : {
2879 : : /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2880 : 4929944 : DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2881 : 4929944 : DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2882 : 4929944 : SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2883 : 4929944 : if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2884 : : {
2885 : 44 : SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2886 : 44 : DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2887 : : }
2888 : 4929900 : else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2889 : 4929900 : && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2890 : 3 : DECL_USER_ALIGN (newdecl) = 1;
2891 : 9859888 : if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2892 : 4929944 : > DECL_WARN_IF_NOT_ALIGN (newdecl))
2893 : 0 : SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2894 : : DECL_WARN_IF_NOT_ALIGN (olddecl));
2895 : : }
2896 : :
2897 : : /* Keep the old rtl since we can safely use it. */
2898 : 4929944 : if (HAS_RTL_P (olddecl))
2899 : 4929944 : COPY_DECL_RTL (olddecl, newdecl);
2900 : :
2901 : : /* Merge the type qualifiers. */
2902 : 4929944 : if (TREE_READONLY (newdecl))
2903 : 388723 : TREE_READONLY (olddecl) = 1;
2904 : :
2905 : 4929944 : if (TREE_THIS_VOLATILE (newdecl))
2906 : 49050 : TREE_THIS_VOLATILE (olddecl) = 1;
2907 : :
2908 : : /* Merge deprecatedness. */
2909 : 4929944 : if (TREE_DEPRECATED (newdecl))
2910 : 331 : TREE_DEPRECATED (olddecl) = 1;
2911 : :
2912 : : /* Merge unavailability. */
2913 : 4929944 : if (TREE_UNAVAILABLE (newdecl))
2914 : 2 : TREE_UNAVAILABLE (olddecl) = 1;
2915 : :
2916 : : /* If a decl is in a system header and the other isn't, keep the one on the
2917 : : system header. Otherwise, keep source location of definition rather than
2918 : : declaration and of prototype rather than non-prototype unless that
2919 : : prototype is built-in. */
2920 : 4929944 : if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2921 : 4929909 : && DECL_IN_SYSTEM_HEADER (olddecl)
2922 : 5608543 : && !DECL_IN_SYSTEM_HEADER (newdecl) )
2923 : 1035 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2924 : 4928909 : else if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2925 : 4928874 : && DECL_IN_SYSTEM_HEADER (newdecl)
2926 : 9129893 : && !DECL_IN_SYSTEM_HEADER (olddecl))
2927 : 3523420 : DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2928 : 1405489 : else if ((DECL_INITIAL (newdecl) == NULL_TREE
2929 : 1174295 : && DECL_INITIAL (olddecl) != NULL_TREE)
2930 : 2578862 : || (old_is_prototype && !new_is_prototype
2931 : 372 : && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2932 : 950 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2933 : :
2934 : : /* Merge the initialization information. */
2935 : 4929944 : if (DECL_INITIAL (newdecl) == NULL_TREE)
2936 : 4698678 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2937 : :
2938 : : /* Merge 'constexpr' information. */
2939 : 4929944 : if (VAR_P (olddecl) && VAR_P (newdecl))
2940 : : {
2941 : 18389 : if (C_DECL_DECLARED_CONSTEXPR (olddecl))
2942 : 2 : C_DECL_DECLARED_CONSTEXPR (newdecl) = 1;
2943 : 18387 : else if (C_DECL_DECLARED_CONSTEXPR (newdecl))
2944 : 1 : C_DECL_DECLARED_CONSTEXPR (olddecl) = 1;
2945 : : }
2946 : :
2947 : : /* Merge the threadprivate attribute. */
2948 : 4929944 : if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2949 : 7 : C_DECL_THREADPRIVATE_P (newdecl) = 1;
2950 : :
2951 : 4929944 : if (HAS_DECL_ASSEMBLER_NAME_P (olddecl))
2952 : : {
2953 : : /* Copy the assembler name.
2954 : : Currently, it can only be defined in the prototype. */
2955 : 4929909 : COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2956 : :
2957 : : /* Use visibility of whichever declaration had it specified */
2958 : 4929909 : if (DECL_VISIBILITY_SPECIFIED (olddecl))
2959 : : {
2960 : 4754 : DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2961 : 4754 : DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2962 : : }
2963 : :
2964 : 4929909 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2965 : : {
2966 : 4865858 : DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2967 : 4865858 : DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2968 : 4865858 : DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2969 : 4865858 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2970 : 4865858 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2971 : 4865858 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2972 : 4865858 : DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2973 : 4865858 : if (DECL_IS_OPERATOR_NEW_P (olddecl))
2974 : 0 : DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2975 : 4865858 : if (DECL_IS_OPERATOR_DELETE_P (olddecl))
2976 : 0 : DECL_SET_IS_OPERATOR_DELETE (newdecl, true);
2977 : 4865858 : TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2978 : 4865858 : DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2979 : 4865858 : DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2980 : : }
2981 : :
2982 : : /* Merge the storage class information. */
2983 : 4929909 : merge_weak (newdecl, olddecl);
2984 : :
2985 : : /* For functions, static overrides non-static. */
2986 : 4929909 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2987 : : {
2988 : 4865858 : TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2989 : : /* This is since we don't automatically
2990 : : copy the attributes of NEWDECL into OLDDECL. */
2991 : 4865858 : TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2992 : : /* If this clears `static', clear it in the identifier too. */
2993 : 4865858 : if (!TREE_PUBLIC (olddecl))
2994 : 7760 : TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2995 : : }
2996 : : }
2997 : :
2998 : : /* In c99, 'extern' declaration before (or after) 'inline' means this
2999 : : function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
3000 : : is present. */
3001 : 4929944 : if (TREE_CODE (newdecl) == FUNCTION_DECL
3002 : 4865858 : && !flag_gnu89_inline
3003 : 4841466 : && (DECL_DECLARED_INLINE_P (newdecl)
3004 : 4652900 : || DECL_DECLARED_INLINE_P (olddecl))
3005 : 188760 : && (!DECL_DECLARED_INLINE_P (newdecl)
3006 : 188566 : || !DECL_DECLARED_INLINE_P (olddecl)
3007 : 807 : || !DECL_EXTERNAL (olddecl))
3008 : 187971 : && DECL_EXTERNAL (newdecl)
3009 : 187763 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
3010 : 4930042 : && !current_function_decl)
3011 : 81 : DECL_EXTERNAL (newdecl) = 0;
3012 : :
3013 : : /* An inline definition following a static declaration is not
3014 : : DECL_EXTERNAL. */
3015 : 4929944 : if (new_is_definition
3016 : 230717 : && (DECL_DECLARED_INLINE_P (newdecl)
3017 : 41929 : || DECL_DECLARED_INLINE_P (olddecl))
3018 : 5118946 : && !TREE_PUBLIC (olddecl))
3019 : 900 : DECL_EXTERNAL (newdecl) = 0;
3020 : :
3021 : 4929944 : if (DECL_EXTERNAL (newdecl))
3022 : : {
3023 : 4839188 : TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
3024 : 4839188 : DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
3025 : :
3026 : : /* An extern decl does not override previous storage class. */
3027 : 4839188 : TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3028 : 4839188 : if (!DECL_EXTERNAL (newdecl))
3029 : : {
3030 : 1437 : DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3031 : 1437 : DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3032 : : }
3033 : : }
3034 : : else
3035 : : {
3036 : 90756 : TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
3037 : 90756 : TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
3038 : : }
3039 : :
3040 : 4929944 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3041 : : {
3042 : 4865858 : if (DECL_FUNCTION_VERSIONED (olddecl)
3043 : 4865858 : || DECL_FUNCTION_VERSIONED (newdecl))
3044 : : {
3045 : 0 : maybe_mark_function_versioned (olddecl);
3046 : 0 : maybe_mark_function_versioned (newdecl);
3047 : : }
3048 : : /* If we're redefining a function previously defined as extern
3049 : : inline, make sure we emit debug info for the inline before we
3050 : : throw it away, in case it was inlined into a function that
3051 : : hasn't been written out yet. */
3052 : 4865858 : if (new_is_definition && DECL_INITIAL (olddecl))
3053 : : /* The new defn must not be inline. */
3054 : 75 : DECL_UNINLINABLE (newdecl) = 1;
3055 : : else
3056 : : {
3057 : : /* If either decl says `inline', this fn is inline, unless
3058 : : its definition was passed already. */
3059 : 4865783 : if (DECL_DECLARED_INLINE_P (newdecl)
3060 : 9542726 : || DECL_DECLARED_INLINE_P (olddecl))
3061 : 189025 : DECL_DECLARED_INLINE_P (newdecl) = 1;
3062 : :
3063 : 14597349 : DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3064 : 9733222 : = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3065 : :
3066 : 9731566 : DECL_DISREGARD_INLINE_LIMITS (newdecl)
3067 : 4865783 : = DECL_DISREGARD_INLINE_LIMITS (olddecl)
3068 : 4865783 : = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
3069 : 9731428 : || DECL_DISREGARD_INLINE_LIMITS (olddecl));
3070 : : }
3071 : :
3072 : 4865858 : if (fndecl_built_in_p (olddecl))
3073 : : {
3074 : : /* If redeclaring a builtin function, it stays built in.
3075 : : But it gets tagged as having been declared. */
3076 : 3832349 : copy_decl_built_in_function (newdecl, olddecl);
3077 : 3832349 : C_DECL_DECLARED_BUILTIN (newdecl) = 1;
3078 : 3832349 : if (new_is_prototype)
3079 : : {
3080 : 3831990 : C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
3081 : 3831990 : if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3082 : : {
3083 : 3831990 : enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3084 : 3831990 : switch (fncode)
3085 : : {
3086 : : /* If a compatible prototype of these builtin functions
3087 : : is seen, assume the runtime implements it with the
3088 : : expected semantics. */
3089 : 6473 : case BUILT_IN_STPCPY:
3090 : 6473 : if (builtin_decl_explicit_p (fncode))
3091 : 6473 : set_builtin_decl_implicit_p (fncode, true);
3092 : : break;
3093 : 3825517 : default:
3094 : 3825517 : if (builtin_decl_explicit_p (fncode))
3095 : 3825517 : set_builtin_decl_declared_p (fncode, true);
3096 : : break;
3097 : : }
3098 : :
3099 : 3831990 : copy_attributes_to_builtin (newdecl);
3100 : : }
3101 : : }
3102 : : else
3103 : 718 : C_DECL_BUILTIN_PROTOTYPE (newdecl)
3104 : 718 : = C_DECL_BUILTIN_PROTOTYPE (olddecl);
3105 : : }
3106 : :
3107 : : /* Preserve function specific target and optimization options */
3108 : 4865858 : if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
3109 : 4866364 : && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
3110 : 470 : DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
3111 : 470 : = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
3112 : :
3113 : 4865858 : if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
3114 : 4889280 : && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
3115 : 9 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
3116 : 9 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
3117 : :
3118 : : /* Also preserve various other info from the definition. */
3119 : 4865858 : if (!new_is_definition)
3120 : : {
3121 : 4635141 : tree t;
3122 : 4635141 : DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3123 : 4635141 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3124 : 4635141 : DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
3125 : 4635141 : DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3126 : 4635141 : DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
3127 : 7266972 : for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
3128 : 2631831 : DECL_CONTEXT (t) = newdecl;
3129 : :
3130 : : /* See if we've got a function to instantiate from. */
3131 : 4635141 : if (DECL_SAVED_TREE (olddecl))
3132 : 1620 : DECL_ABSTRACT_ORIGIN (newdecl)
3133 : 810 : = DECL_ABSTRACT_ORIGIN (olddecl);
3134 : : }
3135 : : }
3136 : :
3137 : : /* Merge the USED information. */
3138 : 4929944 : if (TREE_USED (olddecl))
3139 : 708238 : TREE_USED (newdecl) = 1;
3140 : 4221706 : else if (TREE_USED (newdecl))
3141 : 4 : TREE_USED (olddecl) = 1;
3142 : 4929944 : if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
3143 : 18424 : DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
3144 : 4929944 : if (DECL_PRESERVE_P (olddecl))
3145 : 25 : DECL_PRESERVE_P (newdecl) = 1;
3146 : 4929919 : else if (DECL_PRESERVE_P (newdecl))
3147 : 4 : DECL_PRESERVE_P (olddecl) = 1;
3148 : :
3149 : : /* Merge DECL_COMMON */
3150 : 18389 : if (VAR_P (olddecl) && VAR_P (newdecl)
3151 : 18389 : && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
3152 : 4948331 : && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
3153 : 36770 : DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
3154 : :
3155 : : /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
3156 : : But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
3157 : : DECL_ARGUMENTS (if appropriate). */
3158 : 4929944 : {
3159 : 4929944 : unsigned olddecl_uid = DECL_UID (olddecl);
3160 : 4929944 : tree olddecl_context = DECL_CONTEXT (olddecl);
3161 : 4929944 : tree olddecl_arguments = NULL;
3162 : 4929944 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3163 : 4865858 : olddecl_arguments = DECL_ARGUMENTS (olddecl);
3164 : :
3165 : 4929944 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3166 : : (char *) newdecl + sizeof (struct tree_common),
3167 : : sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3168 : 4929944 : DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3169 : 4929944 : switch (TREE_CODE (olddecl))
3170 : : {
3171 : 4884247 : case FUNCTION_DECL:
3172 : 4884247 : case VAR_DECL:
3173 : 4884247 : {
3174 : 4884247 : struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
3175 : :
3176 : 9768494 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3177 : : (char *) newdecl + sizeof (struct tree_decl_common),
3178 : 4884247 : tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3179 : 4884247 : olddecl->decl_with_vis.symtab_node = snode;
3180 : :
3181 : 4884247 : if ((DECL_EXTERNAL (olddecl)
3182 : 46496 : || TREE_PUBLIC (olddecl)
3183 : 8031 : || TREE_STATIC (olddecl))
3184 : 4930736 : && DECL_SECTION_NAME (newdecl) != NULL)
3185 : 8 : set_decl_section_name (olddecl, newdecl);
3186 : :
3187 : : /* This isn't quite correct for something like
3188 : : int __thread x attribute ((tls_model ("local-exec")));
3189 : : extern int __thread x;
3190 : : as we'll lose the "local-exec" model. */
3191 : 4884247 : if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
3192 : 89 : set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3193 : : break;
3194 : : }
3195 : :
3196 : 45697 : case FIELD_DECL:
3197 : 45697 : case PARM_DECL:
3198 : 45697 : case LABEL_DECL:
3199 : 45697 : case RESULT_DECL:
3200 : 45697 : case CONST_DECL:
3201 : 45697 : case TYPE_DECL:
3202 : 91394 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3203 : : (char *) newdecl + sizeof (struct tree_decl_common),
3204 : 45697 : tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3205 : 45697 : break;
3206 : :
3207 : 0 : default:
3208 : :
3209 : 0 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3210 : : (char *) newdecl + sizeof (struct tree_decl_common),
3211 : : sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
3212 : : }
3213 : 4929944 : DECL_UID (olddecl) = olddecl_uid;
3214 : 4929944 : DECL_CONTEXT (olddecl) = olddecl_context;
3215 : 4929944 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3216 : 4865858 : DECL_ARGUMENTS (olddecl) = olddecl_arguments;
3217 : : }
3218 : :
3219 : : /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3220 : : so that encode_section_info has a chance to look at the new decl
3221 : : flags and attributes. */
3222 : 4929944 : if (DECL_RTL_SET_P (olddecl)
3223 : 4929944 : && (TREE_CODE (olddecl) == FUNCTION_DECL
3224 : 0 : || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
3225 : 0 : make_decl_rtl (olddecl);
3226 : 4929944 : }
3227 : :
3228 : : /* Handle when a new declaration NEWDECL has the same name as an old
3229 : : one OLDDECL in the same binding contour. Prints an error message
3230 : : if appropriate.
3231 : :
3232 : : If safely possible, alter OLDDECL to look like NEWDECL, and return
3233 : : true. Otherwise, return false. */
3234 : :
3235 : : static bool
3236 : 4931042 : duplicate_decls (tree newdecl, tree olddecl)
3237 : : {
3238 : 4931042 : tree newtype = NULL, oldtype = NULL;
3239 : :
3240 : 4931042 : if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
3241 : : {
3242 : : /* Avoid `unused variable' and other warnings for OLDDECL. */
3243 : 1098 : suppress_warning (olddecl, OPT_Wunused);
3244 : : /* If the types are completely different, poison them both with
3245 : : error_mark_node. */
3246 : 1098 : if (TREE_CODE (TREE_TYPE (newdecl)) != TREE_CODE (TREE_TYPE (olddecl))
3247 : 114 : && olddecl != error_mark_node
3248 : 1192 : && seen_error ())
3249 : : {
3250 : 60 : if (TREE_CODE (olddecl) != FUNCTION_DECL)
3251 : 48 : TREE_TYPE (olddecl) = error_mark_node;
3252 : 60 : if (TREE_CODE (newdecl) != FUNCTION_DECL)
3253 : 57 : TREE_TYPE (newdecl) = error_mark_node;
3254 : : }
3255 : 1098 : return false;
3256 : : }
3257 : :
3258 : 4929944 : merge_decls (newdecl, olddecl, newtype, oldtype);
3259 : :
3260 : : /* The NEWDECL will no longer be needed.
3261 : :
3262 : : Before releasing the node, be sure to remove function from symbol
3263 : : table that might have been inserted there to record comdat group.
3264 : : Be sure to however do not free DECL_STRUCT_FUNCTION because this
3265 : : structure is shared in between NEWDECL and OLDECL. */
3266 : 4929944 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3267 : 4865858 : DECL_STRUCT_FUNCTION (newdecl) = NULL;
3268 : 4929944 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3269 : : {
3270 : 4884247 : struct symtab_node *snode = symtab_node::get (newdecl);
3271 : 4884247 : if (snode)
3272 : 104 : snode->remove ();
3273 : : }
3274 : 4929944 : ggc_free (newdecl);
3275 : 4929944 : return true;
3276 : : }
3277 : :
3278 : :
3279 : : /* Check whether decl-node NEW_DECL shadows an existing declaration. */
3280 : : static void
3281 : 169655364 : warn_if_shadowing (tree new_decl)
3282 : : {
3283 : 169655364 : struct c_binding *b;
3284 : :
3285 : : /* Shadow warnings wanted? */
3286 : 339309812 : if (!(warn_shadow
3287 : 169654628 : || warn_shadow_local
3288 : 169654448 : || warn_shadow_compatible_local)
3289 : : /* No shadow warnings for internally generated vars. */
3290 : 169655607 : || DECL_IS_UNDECLARED_BUILTIN (new_decl))
3291 : : return;
3292 : :
3293 : : /* Is anything being shadowed? Invisible decls do not count. */
3294 : 253 : for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
3295 : 157 : if (b->decl && b->decl != new_decl && !b->invisible
3296 : 214 : && (b->decl == error_mark_node
3297 : 43 : || diagnostic_report_warnings_p (global_dc,
3298 : : DECL_SOURCE_LOCATION (b->decl))))
3299 : : {
3300 : 57 : tree old_decl = b->decl;
3301 : :
3302 : 57 : if (old_decl == error_mark_node)
3303 : : {
3304 : 14 : warning (OPT_Wshadow, "declaration of %q+D shadows previous "
3305 : : "non-variable", new_decl);
3306 : 50 : break;
3307 : : }
3308 : :
3309 : 43 : bool warned = false;
3310 : 43 : auto_diagnostic_group d;
3311 : 43 : if (TREE_CODE (old_decl) == PARM_DECL)
3312 : : {
3313 : 5 : enum opt_code warning_code;
3314 : :
3315 : : /* If '-Wshadow=compatible-local' is specified without other
3316 : : -Wshadow= flags, we will warn only when the types of the
3317 : : shadowing variable (i.e. new_decl) and the shadowed variable
3318 : : (old_decl) are compatible. */
3319 : 5 : if (warn_shadow)
3320 : : warning_code = OPT_Wshadow;
3321 : 2 : else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3322 : : warning_code = OPT_Wshadow_compatible_local;
3323 : : else
3324 : 2 : warning_code = OPT_Wshadow_local;
3325 : 5 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3326 : : "declaration of %qD shadows a parameter",
3327 : : new_decl);
3328 : : }
3329 : 38 : else if (DECL_FILE_SCOPE_P (old_decl))
3330 : : {
3331 : : /* Do not warn if a variable shadows a function, unless
3332 : : the variable is a function or a pointer-to-function. */
3333 : 34 : if (TREE_CODE (old_decl) == FUNCTION_DECL
3334 : 11 : && TREE_CODE (new_decl) != FUNCTION_DECL
3335 : 36 : && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
3336 : 7 : continue;
3337 : :
3338 : 20 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
3339 : : "declaration of %qD shadows a global "
3340 : : "declaration",
3341 : : new_decl);
3342 : : }
3343 : 11 : else if (TREE_CODE (old_decl) == FUNCTION_DECL
3344 : 11 : && fndecl_built_in_p (old_decl))
3345 : : {
3346 : 0 : warning (OPT_Wshadow, "declaration of %q+D shadows "
3347 : : "a built-in function", new_decl);
3348 : 0 : break;
3349 : : }
3350 : : else
3351 : : {
3352 : 11 : enum opt_code warning_code;
3353 : :
3354 : : /* If '-Wshadow=compatible-local' is specified without other
3355 : : -Wshadow= flags, we will warn only when the types of the
3356 : : shadowing variable (i.e. new_decl) and the shadowed variable
3357 : : (old_decl) are compatible. */
3358 : 11 : if (warn_shadow)
3359 : : warning_code = OPT_Wshadow;
3360 : 8 : else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3361 : : warning_code = OPT_Wshadow_compatible_local;
3362 : : else
3363 : 2 : warning_code = OPT_Wshadow_local;
3364 : 11 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3365 : : "declaration of %qD shadows a previous local",
3366 : : new_decl);
3367 : : }
3368 : :
3369 : 36 : if (warned)
3370 : 26 : inform (DECL_SOURCE_LOCATION (old_decl),
3371 : : "shadowed declaration is here");
3372 : :
3373 : : break;
3374 : 43 : }
3375 : : }
3376 : :
3377 : : /* Record a decl-node X as belonging to the current lexical scope.
3378 : : Check for errors (such as an incompatible declaration for the same
3379 : : name already seen in the same scope).
3380 : :
3381 : : Returns either X or an old decl for the same name.
3382 : : If an old decl is returned, it may have been smashed
3383 : : to agree with what X says. */
3384 : :
3385 : : tree
3386 : 201623507 : pushdecl (tree x)
3387 : : {
3388 : 201623507 : tree name = DECL_NAME (x);
3389 : 201623507 : struct c_scope *scope = current_scope;
3390 : 201623507 : struct c_binding *b;
3391 : 201623507 : bool nested = false;
3392 : 201623507 : location_t locus = DECL_SOURCE_LOCATION (x);
3393 : :
3394 : : /* Must set DECL_CONTEXT for everything not at file scope or
3395 : : DECL_FILE_SCOPE_P won't work. Local externs don't count
3396 : : unless they have initializers (which generate code). We
3397 : : also exclude CONST_DECLs because enumerators will get the
3398 : : type of the enum as context. */
3399 : 201623507 : if (current_function_decl
3400 : 9109343 : && TREE_CODE (x) != CONST_DECL
3401 : 210576296 : && (!VAR_OR_FUNCTION_DECL_P (x)
3402 : 8675121 : || DECL_INITIAL (x) || !TREE_PUBLIC (x)))
3403 : 8940765 : DECL_CONTEXT (x) = current_function_decl;
3404 : :
3405 : : /* Anonymous decls are just inserted in the scope. */
3406 : 201623507 : if (!name)
3407 : : {
3408 : 7768007 : bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
3409 : : locus);
3410 : 7768007 : return x;
3411 : : }
3412 : :
3413 : : /* First, see if there is another declaration with the same name in
3414 : : the current scope. If there is, duplicate_decls may do all the
3415 : : work for us. If duplicate_decls returns false, that indicates
3416 : : two incompatible decls in the same scope; we are to silently
3417 : : replace the old one (duplicate_decls has issued all appropriate
3418 : : diagnostics). In particular, we should not consider possible
3419 : : duplicates in the external scope, or shadowing. */
3420 : 193855500 : b = I_SYMBOL_BINDING (name);
3421 : 193855500 : if (b && B_IN_SCOPE (b, scope))
3422 : : {
3423 : 1374441 : struct c_binding *b_ext, *b_use;
3424 : 1374441 : tree type = TREE_TYPE (x);
3425 : 1374441 : tree visdecl = b->decl;
3426 : 1374441 : tree vistype = TREE_TYPE (visdecl);
3427 : 1374441 : if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3428 : 1374441 : && COMPLETE_TYPE_P (TREE_TYPE (x)))
3429 : 1369 : b->inner_comp = false;
3430 : 1374441 : b_use = b;
3431 : 1374441 : b_ext = b;
3432 : : /* If this is an external linkage declaration, we should check
3433 : : for compatibility with the type in the external scope before
3434 : : setting the type at this scope based on the visible
3435 : : information only. */
3436 : 1374441 : if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
3437 : : {
3438 : 2641230 : while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3439 : 1320625 : b_ext = b_ext->shadowed;
3440 : 1320605 : if (b_ext)
3441 : : {
3442 : 1320604 : b_use = b_ext;
3443 : 1320604 : if (b_use->u.type)
3444 : 268352 : TREE_TYPE (b_use->decl) = b_use->u.type;
3445 : : }
3446 : : }
3447 : :
3448 : : /* Check if x is part of a FMV set with b_use.
3449 : : FMV is only supported in c for targets with target_version
3450 : : attributes. */
3451 : 1374441 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
3452 : : && b_use && TREE_CODE (b_use->decl) == FUNCTION_DECL
3453 : : && TREE_CODE (x) == FUNCTION_DECL && DECL_FILE_SCOPE_P (b_use->decl)
3454 : : && DECL_FILE_SCOPE_P (x)
3455 : : && disjoint_version_decls (x, b_use->decl)
3456 : : && comptypes (vistype, type) != 0)
3457 : : {
3458 : : maybe_mark_function_versioned (b_use->decl);
3459 : : maybe_mark_function_versioned (b->decl);
3460 : : maybe_mark_function_versioned (x);
3461 : :
3462 : : cgraph_node *b_node = cgraph_node::get_create (b_use->decl);
3463 : : cgraph_function_version_info *b_v = b_node->function_version ();
3464 : : if (!b_v)
3465 : : b_v = b_node->insert_new_function_version ();
3466 : :
3467 : : /* Check if this new node conflicts with any previous functions
3468 : : in the set. */
3469 : : cgraph_function_version_info *version = b_v;
3470 : : for (; version; version = version->next)
3471 : : if (!disjoint_version_decls (version->this_node->decl, x))
3472 : : {
3473 : : /* The decls define overlapping version, so attempt to merge
3474 : : or diagnose the conflict. */
3475 : : if (duplicate_decls (x, version->this_node->decl))
3476 : : return version->this_node->decl;
3477 : : else
3478 : : return error_mark_node;
3479 : : }
3480 : :
3481 : : /* This is a new version to be added to FMV structure. */
3482 : : cgraph_node::add_function_version (b_v, x);
3483 : :
3484 : : /* Get the first node from the structure. */
3485 : : cgraph_function_version_info *default_v = b_v;
3486 : : while (default_v->prev)
3487 : : default_v = default_v->prev;
3488 : : /* Always use the default node for the bindings. */
3489 : : b_use->decl = default_v->this_node->decl;
3490 : : b->decl = default_v->this_node->decl;
3491 : :
3492 : : /* Node is not a duplicate, so no need to do the rest of the
3493 : : checks. */
3494 : : return x;
3495 : : }
3496 : :
3497 : 1374441 : if (duplicate_decls (x, b_use->decl))
3498 : : {
3499 : 1374077 : if (b_use != b)
3500 : : {
3501 : : /* Save the updated type in the external scope and
3502 : : restore the proper type for this scope. */
3503 : 1320402 : tree thistype;
3504 : 1320402 : if (comptypes (vistype, type))
3505 : 1320360 : thistype = composite_type (vistype, type);
3506 : : else
3507 : 42 : thistype = TREE_TYPE (b_use->decl);
3508 : 1320402 : b_use->u.type = TREE_TYPE (b_use->decl);
3509 : 1320402 : if (TREE_CODE (b_use->decl) == FUNCTION_DECL
3510 : 1320402 : && fndecl_built_in_p (b_use->decl))
3511 : 278219 : thistype
3512 : 278219 : = c_build_type_attribute_variant (thistype,
3513 : 278219 : TYPE_ATTRIBUTES
3514 : : (b_use->u.type));
3515 : 1320402 : TREE_TYPE (b_use->decl) = thistype;
3516 : : }
3517 : 1374077 : return b_use->decl;
3518 : : }
3519 : : else
3520 : 364 : goto skip_external_and_shadow_checks;
3521 : : }
3522 : :
3523 : : /* All declarations with external linkage, and all external
3524 : : references, go in the external scope, no matter what scope is
3525 : : current. However, the binding in that scope is ignored for
3526 : : purposes of normal name lookup. A separate binding structure is
3527 : : created in the requested scope; this governs the normal
3528 : : visibility of the symbol.
3529 : :
3530 : : The binding in the externals scope is used exclusively for
3531 : : detecting duplicate declarations of the same object, no matter
3532 : : what scope they are in; this is what we do here. (C99 6.2.7p2:
3533 : : All declarations that refer to the same object or function shall
3534 : : have compatible type; otherwise, the behavior is undefined.)
3535 : : However, in Objective-C, we also want to detect declarations
3536 : : conflicting with those of the basic types. */
3537 : 336258718 : if ((DECL_EXTERNAL (x) || scope == file_scope)
3538 : 203839994 : && (VAR_OR_FUNCTION_DECL_P (x) || c_dialect_objc ()))
3539 : : {
3540 : 50491134 : tree type = TREE_TYPE (x);
3541 : 50491134 : tree vistype = NULL_TREE;
3542 : 50491134 : tree visdecl = NULL_TREE;
3543 : 50491134 : bool type_saved = false;
3544 : 3556628 : if (b && !B_IN_EXTERNAL_SCOPE (b)
3545 : 1038 : && VAR_OR_FUNCTION_DECL_P (b->decl)
3546 : 50492163 : && DECL_FILE_SCOPE_P (b->decl))
3547 : : {
3548 : 803 : visdecl = b->decl;
3549 : 803 : vistype = TREE_TYPE (visdecl);
3550 : : }
3551 : 50491134 : if (scope != file_scope
3552 : 50491134 : && !DECL_IN_SYSTEM_HEADER (x))
3553 : 11474 : warning_at (locus, OPT_Wnested_externs,
3554 : : "nested extern declaration of %qD", x);
3555 : :
3556 : 50492558 : while (b && !B_IN_EXTERNAL_SCOPE (b))
3557 : : {
3558 : : /* If this decl might be modified, save its type. This is
3559 : : done here rather than when the decl is first bound
3560 : : because the type may change after first binding, through
3561 : : being completed or through attributes being added. If we
3562 : : encounter multiple such decls, only the first should have
3563 : : its type saved; the others will already have had their
3564 : : proper types saved and the types will not have changed as
3565 : : their scopes will not have been re-entered. */
3566 : 1424 : if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3567 : : {
3568 : 1022 : b->u.type = TREE_TYPE (b->decl);
3569 : 1022 : type_saved = true;
3570 : : }
3571 : 1424 : if (B_IN_FILE_SCOPE (b)
3572 : 1012 : && VAR_P (b->decl)
3573 : 572 : && TREE_STATIC (b->decl)
3574 : 281 : && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3575 : 138 : && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3576 : 47 : && TREE_CODE (type) == ARRAY_TYPE
3577 : 47 : && TYPE_DOMAIN (type)
3578 : 28 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3579 : 1452 : && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3580 : : {
3581 : : /* Array type completed in inner scope, which should be
3582 : : diagnosed if the completion does not have size 1 and
3583 : : it does not get completed in the file scope. */
3584 : 6 : b->inner_comp = true;
3585 : : }
3586 : 1424 : b = b->shadowed;
3587 : : }
3588 : :
3589 : : /* If a matching external declaration has been found, set its
3590 : : type to the composite of all the types of that declaration.
3591 : : After the consistency checks, it will be reset to the
3592 : : composite of the visible types only. */
3593 : 50491134 : if (b && b->u.type)
3594 : 768 : TREE_TYPE (b->decl) = b->u.type;
3595 : :
3596 : : /* the static does not go in the externals scope. */
3597 : 3556484 : if (b && duplicate_decls (x, b->decl))
3598 : : {
3599 : 3555750 : tree thistype;
3600 : 3555750 : if (vistype)
3601 : : {
3602 : 675 : if (comptypes (vistype, type))
3603 : 638 : thistype = composite_type (vistype, type);
3604 : : else
3605 : 37 : thistype = TREE_TYPE (b->decl);
3606 : : }
3607 : : else
3608 : : thistype = type;
3609 : 3555750 : b->u.type = TREE_TYPE (b->decl);
3610 : : /* Propagate the type attributes to the decl. */
3611 : 3555750 : thistype
3612 : 3555750 : = c_build_type_attribute_variant (thistype,
3613 : 3555750 : TYPE_ATTRIBUTES (b->u.type));
3614 : 3555750 : TREE_TYPE (b->decl) = thistype;
3615 : 3555750 : bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3616 : : locus);
3617 : 3555750 : return b->decl;
3618 : : }
3619 : 46935384 : else if (TREE_PUBLIC (x))
3620 : : {
3621 : 46563586 : if (visdecl && !b && duplicate_decls (x, visdecl))
3622 : : {
3623 : : /* An external declaration at block scope referring to a
3624 : : visible entity with internal linkage. The composite
3625 : : type will already be correct for this scope, so we
3626 : : just need to fall through to make the declaration in
3627 : : this scope. */
3628 : : nested = true;
3629 : : x = visdecl;
3630 : : }
3631 : : else
3632 : : {
3633 : 46563469 : bind (name, x, external_scope, /*invisible=*/true,
3634 : : /*nested=*/false, locus);
3635 : 46563469 : nested = true;
3636 : : }
3637 : : }
3638 : : }
3639 : :
3640 : 188925309 : if (TREE_CODE (x) != PARM_DECL)
3641 : 70069776 : warn_if_shadowing (x);
3642 : :
3643 : 118855533 : skip_external_and_shadow_checks:
3644 : 188925673 : if (TREE_CODE (x) == TYPE_DECL)
3645 : : {
3646 : : /* So this is a typedef, set its underlying type. */
3647 : 9765843 : set_underlying_type (x);
3648 : :
3649 : : /* If X is a typedef defined in the current function, record it
3650 : : for the purpose of implementing the -Wunused-local-typedefs
3651 : : warning. */
3652 : 9765843 : record_locally_defined_typedef (x);
3653 : : }
3654 : :
3655 : 188925673 : bind (name, x, scope, /*invisible=*/false, nested, locus);
3656 : :
3657 : : /* If x's type is incomplete because it's based on a
3658 : : structure or union which has not yet been fully declared,
3659 : : attach it to that structure or union type, so we can go
3660 : : back and complete the variable declaration later, if the
3661 : : structure or union gets fully declared.
3662 : :
3663 : : If the input is erroneous, we can have error_mark in the type
3664 : : slot (e.g. "f(void a, ...)") - that doesn't count as an
3665 : : incomplete type. */
3666 : 188925673 : if (TREE_TYPE (x) != error_mark_node
3667 : 188925673 : && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3668 : : {
3669 : 185617 : tree element = TREE_TYPE (x);
3670 : :
3671 : 205571 : while (TREE_CODE (element) == ARRAY_TYPE)
3672 : 19954 : element = TREE_TYPE (element);
3673 : 185617 : element = TYPE_MAIN_VARIANT (element);
3674 : :
3675 : 185617 : if ((RECORD_OR_UNION_TYPE_P (element)
3676 : 141909 : || TREE_CODE (element) == ENUMERAL_TYPE)
3677 : 43779 : && (TREE_CODE (x) != TYPE_DECL
3678 : 40321 : || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3679 : 189083 : && !COMPLETE_TYPE_P (element))
3680 : 321 : C_TYPE_INCOMPLETE_VARS (element)
3681 : 642 : = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3682 : : }
3683 : : return x;
3684 : : }
3685 : :
3686 : :
3687 : : /* Issue a permerror about implicit function declaration. ID is the function
3688 : : identifier, OLDDECL is a declaration of the function in a different scope,
3689 : : or NULL_TREE. */
3690 : :
3691 : : static void
3692 : 3776 : implicit_decl_permerror (location_t loc, tree id, tree olddecl)
3693 : : {
3694 : 3776 : if (!warn_implicit_function_declaration)
3695 : 2617 : return;
3696 : :
3697 : 1159 : bool warned;
3698 : 1159 : auto_diagnostic_group d;
3699 : 1159 : name_hint hint;
3700 : 1159 : if (!olddecl)
3701 : 553 : hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3702 : :
3703 : 1159 : if (flag_isoc99)
3704 : : {
3705 : 1153 : if (const char *suggestion = hint.suggestion ())
3706 : : {
3707 : 107 : gcc_rich_location richloc (loc);
3708 : 107 : richloc.add_fixit_replace (suggestion);
3709 : 107 : warned = permerror_opt (&richloc, OPT_Wimplicit_function_declaration,
3710 : : "implicit declaration of function %qE;"
3711 : : " did you mean %qs?",
3712 : : id, suggestion);
3713 : 107 : }
3714 : : else
3715 : 1046 : warned = permerror_opt (loc, OPT_Wimplicit_function_declaration,
3716 : : "implicit declaration of function %qE", id);
3717 : : }
3718 : 6 : else if (const char *suggestion = hint.suggestion ())
3719 : : {
3720 : 2 : gcc_rich_location richloc (loc);
3721 : 2 : richloc.add_fixit_replace (suggestion);
3722 : 2 : warned = warning_at
3723 : 2 : (&richloc, OPT_Wimplicit_function_declaration,
3724 : : G_("implicit declaration of function %qE; did you mean %qs?"),
3725 : : id, suggestion);
3726 : 2 : }
3727 : : else
3728 : 4 : warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3729 : : G_("implicit declaration of function %qE"), id);
3730 : :
3731 : 1159 : if (warned)
3732 : : {
3733 : : /* Whether the olddecl is an undeclared builtin function.
3734 : : locate_old_decl will not generate a diagnostic for those,
3735 : : so in that case we want to look elsewhere. */
3736 : 90 : bool undeclared_builtin = (olddecl
3737 : 28 : && TREE_CODE (olddecl) == FUNCTION_DECL
3738 : 28 : && fndecl_built_in_p (olddecl)
3739 : 117 : && !C_DECL_DECLARED_BUILTIN (olddecl));
3740 : 90 : if (undeclared_builtin)
3741 : : {
3742 : 27 : const char *header = header_for_builtin_fn (olddecl);
3743 : 27 : if (header)
3744 : : {
3745 : 20 : rich_location richloc (line_table, loc);
3746 : 20 : maybe_add_include_fixit (&richloc, header, true);
3747 : 20 : inform (&richloc,
3748 : : "include %qs or provide a declaration of %qE",
3749 : : header, id);
3750 : 20 : }
3751 : : }
3752 : 63 : else if (olddecl)
3753 : 1 : locate_old_decl (olddecl);
3754 : : }
3755 : :
3756 : 1069 : if (!warned)
3757 : 1159 : hint.suppress ();
3758 : 1159 : }
3759 : :
3760 : : /* Return the name of the header file that declares built-in function
3761 : : FNDECL, or null if either we don't know or don't expect to see an
3762 : : explicit declaration. */
3763 : :
3764 : : static const char *
3765 : 3178 : header_for_builtin_fn (tree fndecl)
3766 : : {
3767 : 3178 : if (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3768 : : return NULL;
3769 : :
3770 : 3178 : switch (DECL_FUNCTION_CODE (fndecl))
3771 : : {
3772 : : CASE_FLT_FN (BUILT_IN_ACOS):
3773 : : CASE_FLT_FN (BUILT_IN_ACOSH):
3774 : : CASE_FLT_FN (BUILT_IN_ASIN):
3775 : : CASE_FLT_FN (BUILT_IN_ASINH):
3776 : : CASE_FLT_FN (BUILT_IN_ATAN):
3777 : : CASE_FLT_FN (BUILT_IN_ATANH):
3778 : : CASE_FLT_FN (BUILT_IN_ATAN2):
3779 : : CASE_FLT_FN (BUILT_IN_CBRT):
3780 : : CASE_FLT_FN (BUILT_IN_CEIL):
3781 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3782 : : CASE_FLT_FN (BUILT_IN_COPYSIGN):
3783 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3784 : : CASE_FLT_FN (BUILT_IN_COS):
3785 : : CASE_FLT_FN (BUILT_IN_COSH):
3786 : : CASE_FLT_FN (BUILT_IN_ERF):
3787 : : CASE_FLT_FN (BUILT_IN_ERFC):
3788 : : CASE_FLT_FN (BUILT_IN_EXP):
3789 : : CASE_FLT_FN (BUILT_IN_EXP2):
3790 : : CASE_FLT_FN (BUILT_IN_EXPM1):
3791 : : CASE_FLT_FN (BUILT_IN_FABS):
3792 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3793 : : CASE_FLT_FN (BUILT_IN_FDIM):
3794 : : CASE_FLT_FN (BUILT_IN_FLOOR):
3795 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3796 : : CASE_FLT_FN (BUILT_IN_FMA):
3797 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3798 : : CASE_FLT_FN (BUILT_IN_FMAX):
3799 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3800 : : CASE_FLT_FN (BUILT_IN_FMIN):
3801 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3802 : : CASE_FLT_FN (BUILT_IN_FMOD):
3803 : : CASE_FLT_FN (BUILT_IN_FREXP):
3804 : : CASE_FLT_FN (BUILT_IN_HYPOT):
3805 : : CASE_FLT_FN (BUILT_IN_ILOGB):
3806 : : CASE_FLT_FN (BUILT_IN_LDEXP):
3807 : : CASE_FLT_FN (BUILT_IN_LGAMMA):
3808 : : CASE_FLT_FN (BUILT_IN_LLRINT):
3809 : : CASE_FLT_FN (BUILT_IN_LLROUND):
3810 : : CASE_FLT_FN (BUILT_IN_LOG):
3811 : : CASE_FLT_FN (BUILT_IN_LOG10):
3812 : : CASE_FLT_FN (BUILT_IN_LOG1P):
3813 : : CASE_FLT_FN (BUILT_IN_LOG2):
3814 : : CASE_FLT_FN (BUILT_IN_LOGB):
3815 : : CASE_FLT_FN (BUILT_IN_LRINT):
3816 : : CASE_FLT_FN (BUILT_IN_LROUND):
3817 : : CASE_FLT_FN (BUILT_IN_MODF):
3818 : : CASE_FLT_FN (BUILT_IN_NAN):
3819 : : CASE_FLT_FN (BUILT_IN_NEARBYINT):
3820 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3821 : : CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3822 : : CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3823 : : CASE_FLT_FN (BUILT_IN_POW):
3824 : : CASE_FLT_FN (BUILT_IN_REMAINDER):
3825 : : CASE_FLT_FN (BUILT_IN_REMQUO):
3826 : : CASE_FLT_FN (BUILT_IN_RINT):
3827 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3828 : : CASE_FLT_FN (BUILT_IN_ROUND):
3829 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3830 : : CASE_FLT_FN (BUILT_IN_SCALBLN):
3831 : : CASE_FLT_FN (BUILT_IN_SCALBN):
3832 : : CASE_FLT_FN (BUILT_IN_SIN):
3833 : : CASE_FLT_FN (BUILT_IN_SINH):
3834 : : CASE_FLT_FN (BUILT_IN_SINCOS):
3835 : : CASE_FLT_FN (BUILT_IN_SQRT):
3836 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3837 : : CASE_FLT_FN (BUILT_IN_TAN):
3838 : : CASE_FLT_FN (BUILT_IN_TANH):
3839 : : CASE_FLT_FN (BUILT_IN_TGAMMA):
3840 : : CASE_FLT_FN (BUILT_IN_TRUNC):
3841 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3842 : : case BUILT_IN_ISINF:
3843 : : case BUILT_IN_ISNAN:
3844 : : return "<math.h>";
3845 : 29 : CASE_FLT_FN (BUILT_IN_CABS):
3846 : 29 : CASE_FLT_FN (BUILT_IN_CACOS):
3847 : 29 : CASE_FLT_FN (BUILT_IN_CACOSH):
3848 : 29 : CASE_FLT_FN (BUILT_IN_CARG):
3849 : 29 : CASE_FLT_FN (BUILT_IN_CASIN):
3850 : 29 : CASE_FLT_FN (BUILT_IN_CASINH):
3851 : 29 : CASE_FLT_FN (BUILT_IN_CATAN):
3852 : 29 : CASE_FLT_FN (BUILT_IN_CATANH):
3853 : 29 : CASE_FLT_FN (BUILT_IN_CCOS):
3854 : 29 : CASE_FLT_FN (BUILT_IN_CCOSH):
3855 : 29 : CASE_FLT_FN (BUILT_IN_CEXP):
3856 : 29 : CASE_FLT_FN (BUILT_IN_CIMAG):
3857 : 29 : CASE_FLT_FN (BUILT_IN_CLOG):
3858 : 29 : CASE_FLT_FN (BUILT_IN_CONJ):
3859 : 29 : CASE_FLT_FN (BUILT_IN_CPOW):
3860 : 29 : CASE_FLT_FN (BUILT_IN_CPROJ):
3861 : 29 : CASE_FLT_FN (BUILT_IN_CREAL):
3862 : 29 : CASE_FLT_FN (BUILT_IN_CSIN):
3863 : 29 : CASE_FLT_FN (BUILT_IN_CSINH):
3864 : 29 : CASE_FLT_FN (BUILT_IN_CSQRT):
3865 : 29 : CASE_FLT_FN (BUILT_IN_CTAN):
3866 : 29 : CASE_FLT_FN (BUILT_IN_CTANH):
3867 : 29 : return "<complex.h>";
3868 : 228 : case BUILT_IN_MEMCHR:
3869 : 228 : case BUILT_IN_MEMCMP:
3870 : 228 : case BUILT_IN_MEMCPY:
3871 : 228 : case BUILT_IN_MEMMOVE:
3872 : 228 : case BUILT_IN_MEMSET:
3873 : 228 : case BUILT_IN_STRCAT:
3874 : 228 : case BUILT_IN_STRCHR:
3875 : 228 : case BUILT_IN_STRCMP:
3876 : 228 : case BUILT_IN_STRCPY:
3877 : 228 : case BUILT_IN_STRCSPN:
3878 : 228 : case BUILT_IN_STRLEN:
3879 : 228 : case BUILT_IN_STRNCAT:
3880 : 228 : case BUILT_IN_STRNCMP:
3881 : 228 : case BUILT_IN_STRNCPY:
3882 : 228 : case BUILT_IN_STRPBRK:
3883 : 228 : case BUILT_IN_STRRCHR:
3884 : 228 : case BUILT_IN_STRSPN:
3885 : 228 : case BUILT_IN_STRSTR:
3886 : 228 : return "<string.h>";
3887 : 544 : case BUILT_IN_FPRINTF:
3888 : 544 : case BUILT_IN_PUTC:
3889 : 544 : case BUILT_IN_FPUTC:
3890 : 544 : case BUILT_IN_FPUTS:
3891 : 544 : case BUILT_IN_FSCANF:
3892 : 544 : case BUILT_IN_FWRITE:
3893 : 544 : case BUILT_IN_PRINTF:
3894 : 544 : case BUILT_IN_PUTCHAR:
3895 : 544 : case BUILT_IN_PUTS:
3896 : 544 : case BUILT_IN_SCANF:
3897 : 544 : case BUILT_IN_SNPRINTF:
3898 : 544 : case BUILT_IN_SPRINTF:
3899 : 544 : case BUILT_IN_SSCANF:
3900 : 544 : case BUILT_IN_VFPRINTF:
3901 : 544 : case BUILT_IN_VFSCANF:
3902 : 544 : case BUILT_IN_VPRINTF:
3903 : 544 : case BUILT_IN_VSCANF:
3904 : 544 : case BUILT_IN_VSNPRINTF:
3905 : 544 : case BUILT_IN_VSPRINTF:
3906 : 544 : case BUILT_IN_VSSCANF:
3907 : 544 : return "<stdio.h>";
3908 : 2 : case BUILT_IN_ISALNUM:
3909 : 2 : case BUILT_IN_ISALPHA:
3910 : 2 : case BUILT_IN_ISBLANK:
3911 : 2 : case BUILT_IN_ISCNTRL:
3912 : 2 : case BUILT_IN_ISDIGIT:
3913 : 2 : case BUILT_IN_ISGRAPH:
3914 : 2 : case BUILT_IN_ISLOWER:
3915 : 2 : case BUILT_IN_ISPRINT:
3916 : 2 : case BUILT_IN_ISPUNCT:
3917 : 2 : case BUILT_IN_ISSPACE:
3918 : 2 : case BUILT_IN_ISUPPER:
3919 : 2 : case BUILT_IN_ISXDIGIT:
3920 : 2 : case BUILT_IN_TOLOWER:
3921 : 2 : case BUILT_IN_TOUPPER:
3922 : 2 : return "<ctype.h>";
3923 : 0 : case BUILT_IN_ISWALNUM:
3924 : 0 : case BUILT_IN_ISWALPHA:
3925 : 0 : case BUILT_IN_ISWBLANK:
3926 : 0 : case BUILT_IN_ISWCNTRL:
3927 : 0 : case BUILT_IN_ISWDIGIT:
3928 : 0 : case BUILT_IN_ISWGRAPH:
3929 : 0 : case BUILT_IN_ISWLOWER:
3930 : 0 : case BUILT_IN_ISWPRINT:
3931 : 0 : case BUILT_IN_ISWPUNCT:
3932 : 0 : case BUILT_IN_ISWSPACE:
3933 : 0 : case BUILT_IN_ISWUPPER:
3934 : 0 : case BUILT_IN_ISWXDIGIT:
3935 : 0 : case BUILT_IN_TOWLOWER:
3936 : 0 : case BUILT_IN_TOWUPPER:
3937 : 0 : return "<wctype.h>";
3938 : 1818 : case BUILT_IN_ABORT:
3939 : 1818 : case BUILT_IN_ABS:
3940 : 1818 : case BUILT_IN_CALLOC:
3941 : 1818 : case BUILT_IN_EXIT:
3942 : 1818 : case BUILT_IN_FREE:
3943 : 1818 : case BUILT_IN_LABS:
3944 : 1818 : case BUILT_IN_LLABS:
3945 : 1818 : case BUILT_IN_MALLOC:
3946 : 1818 : case BUILT_IN_REALLOC:
3947 : 1818 : case BUILT_IN__EXIT2:
3948 : 1818 : case BUILT_IN_ALIGNED_ALLOC:
3949 : 1818 : return "<stdlib.h>";
3950 : 1 : case BUILT_IN_IMAXABS:
3951 : 1 : return "<inttypes.h>";
3952 : 3 : case BUILT_IN_STRFTIME:
3953 : 3 : return "<time.h>";
3954 : : default:
3955 : : return NULL;
3956 : : }
3957 : : }
3958 : :
3959 : : /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3960 : : function of type int (). */
3961 : :
3962 : : tree
3963 : 4729 : implicitly_declare (location_t loc, tree functionid)
3964 : : {
3965 : 4729 : struct c_binding *b;
3966 : 4729 : tree decl = NULL_TREE;
3967 : 4729 : tree asmspec_tree;
3968 : :
3969 : 4743 : for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3970 : : {
3971 : 3158 : if (B_IN_SCOPE (b, external_scope))
3972 : : {
3973 : 3144 : decl = b->decl;
3974 : 3144 : break;
3975 : : }
3976 : : }
3977 : :
3978 : 4729 : if (decl)
3979 : : {
3980 : 3144 : if (TREE_CODE (decl) != FUNCTION_DECL)
3981 : : return decl;
3982 : :
3983 : : /* FIXME: Objective-C has weird not-really-builtin functions
3984 : : which are supposed to be visible automatically. They wind up
3985 : : in the external scope because they're pushed before the file
3986 : : scope gets created. Catch this here and rebind them into the
3987 : : file scope. */
3988 : 3137 : if (!fndecl_built_in_p (decl) && DECL_IS_UNDECLARED_BUILTIN (decl))
3989 : : {
3990 : 0 : bind (functionid, decl, file_scope,
3991 : : /*invisible=*/false, /*nested=*/true,
3992 : 0 : DECL_SOURCE_LOCATION (decl));
3993 : 0 : return decl;
3994 : : }
3995 : : else
3996 : : {
3997 : 3137 : tree newtype = default_function_type;
3998 : 3137 : if (b->u.type)
3999 : 753 : TREE_TYPE (decl) = b->u.type;
4000 : : /* Implicit declaration of a function already declared
4001 : : (somehow) in a different scope, or as a built-in.
4002 : : If this is the first time this has happened, warn;
4003 : : then recycle the old declaration but with the new type. */
4004 : 3137 : if (!C_DECL_IMPLICIT (decl))
4005 : : {
4006 : 2191 : implicit_decl_permerror (loc, functionid, decl);
4007 : 2191 : C_DECL_IMPLICIT (decl) = 1;
4008 : : }
4009 : 3137 : if (fndecl_built_in_p (decl))
4010 : : {
4011 : 2705 : newtype = c_build_type_attribute_variant (newtype,
4012 : 2705 : TYPE_ATTRIBUTES
4013 : : (TREE_TYPE (decl)));
4014 : 2705 : if (!comptypes (newtype, TREE_TYPE (decl)))
4015 : : {
4016 : 2528 : auto_diagnostic_group d;
4017 : 2528 : bool warned = warning_at (loc,
4018 : 2528 : OPT_Wbuiltin_declaration_mismatch,
4019 : : "incompatible implicit "
4020 : : "declaration of built-in "
4021 : : "function %qD", decl);
4022 : : /* See if we can hint which header to include. */
4023 : 2528 : const char *header = header_for_builtin_fn (decl);
4024 : 2528 : if (header != NULL && warned)
4025 : : {
4026 : 133 : rich_location richloc (line_table, loc);
4027 : 133 : maybe_add_include_fixit (&richloc, header, true);
4028 : 133 : inform (&richloc,
4029 : : "include %qs or provide a declaration of %qD",
4030 : : header, decl);
4031 : 133 : }
4032 : 2528 : newtype = TREE_TYPE (decl);
4033 : 2528 : }
4034 : : }
4035 : : else
4036 : : {
4037 : 432 : if (!comptypes (newtype, TREE_TYPE (decl)))
4038 : : {
4039 : 2 : auto_diagnostic_group d;
4040 : 2 : error_at (loc, "incompatible implicit declaration of "
4041 : : "function %qD", decl);
4042 : 2 : locate_old_decl (decl);
4043 : 2 : }
4044 : : }
4045 : 3137 : b->u.type = TREE_TYPE (decl);
4046 : 3137 : TREE_TYPE (decl) = newtype;
4047 : 3137 : bind (functionid, decl, current_scope,
4048 : : /*invisible=*/false, /*nested=*/true,
4049 : 3137 : DECL_SOURCE_LOCATION (decl));
4050 : 3137 : return decl;
4051 : : }
4052 : : }
4053 : :
4054 : : /* Not seen before. */
4055 : 1585 : decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
4056 : 1585 : DECL_EXTERNAL (decl) = 1;
4057 : 1585 : TREE_PUBLIC (decl) = 1;
4058 : 1585 : C_DECL_IMPLICIT (decl) = 1;
4059 : 1585 : implicit_decl_permerror (loc, functionid, 0);
4060 : 1585 : asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
4061 : 1585 : if (asmspec_tree)
4062 : 1 : set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
4063 : :
4064 : : /* C89 says implicit declarations are in the innermost block.
4065 : : So we record the decl in the standard fashion. */
4066 : 1585 : decl = pushdecl (decl);
4067 : :
4068 : : /* No need to call objc_check_decl here - it's a function type. */
4069 : 1585 : rest_of_decl_compilation (decl, 0, 0);
4070 : :
4071 : : /* Write a record describing this implicit function declaration
4072 : : to the prototypes file (if requested). */
4073 : 1585 : gen_aux_info_record (decl, 0, 1, 0);
4074 : :
4075 : : /* Possibly apply some default attributes to this implicit declaration. */
4076 : 1585 : decl_attributes (&decl, NULL_TREE, 0);
4077 : :
4078 : 1585 : return decl;
4079 : : }
4080 : :
4081 : : /* Issue an error message for a reference to an undeclared variable
4082 : : ID, including a reference to a builtin outside of function-call
4083 : : context. Establish a binding of the identifier to error_mark_node
4084 : : in an appropriate scope, which will suppress further errors for the
4085 : : same identifier. The error message should be given location LOC. */
4086 : : void
4087 : 1424 : undeclared_variable (location_t loc, tree id)
4088 : : {
4089 : 1424 : static bool already = false;
4090 : 1424 : struct c_scope *scope;
4091 : :
4092 : 1424 : auto_diagnostic_group d;
4093 : 1424 : if (current_function_decl == NULL_TREE)
4094 : : {
4095 : 610 : name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
4096 : 610 : if (const char *suggestion = guessed_id.suggestion ())
4097 : : {
4098 : 86 : gcc_rich_location richloc (loc);
4099 : 86 : richloc.add_fixit_replace (suggestion);
4100 : 86 : error_at (&richloc,
4101 : : "%qE undeclared here (not in a function);"
4102 : : " did you mean %qs?",
4103 : : id, suggestion);
4104 : 86 : }
4105 : : else
4106 : 524 : error_at (loc, "%qE undeclared here (not in a function)", id);
4107 : 610 : scope = current_scope;
4108 : 610 : }
4109 : : else
4110 : : {
4111 : 814 : if (!objc_diagnose_private_ivar (id))
4112 : : {
4113 : 814 : name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
4114 : 814 : if (const char *suggestion = guessed_id.suggestion ())
4115 : : {
4116 : 36 : gcc_rich_location richloc (loc);
4117 : 36 : richloc.add_fixit_replace (suggestion);
4118 : 36 : error_at (&richloc,
4119 : : "%qE undeclared (first use in this function);"
4120 : : " did you mean %qs?",
4121 : : id, suggestion);
4122 : 36 : }
4123 : : else
4124 : 778 : error_at (loc, "%qE undeclared (first use in this function)", id);
4125 : 814 : }
4126 : 814 : if (!already)
4127 : : {
4128 : 197 : inform (loc, "each undeclared identifier is reported only"
4129 : : " once for each function it appears in");
4130 : 197 : already = true;
4131 : : }
4132 : :
4133 : : /* If we are parsing old-style parameter decls, current_function_decl
4134 : : will be nonnull but current_function_scope will be null. */
4135 : 814 : scope = current_function_scope ? current_function_scope : current_scope;
4136 : : }
4137 : 1424 : bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
4138 : : UNKNOWN_LOCATION);
4139 : 1424 : }
4140 : :
4141 : : /* Subroutine of lookup_label, declare_label, define_label: construct a
4142 : : LABEL_DECL with all the proper frills. Also create a struct
4143 : : c_label_vars initialized for the current scope. */
4144 : :
4145 : : static tree
4146 : 23787 : make_label (location_t location, tree name, bool defining,
4147 : : struct c_label_vars **p_label_vars)
4148 : : {
4149 : 23787 : tree label = build_decl (location, LABEL_DECL, name, void_type_node);
4150 : 23787 : DECL_CONTEXT (label) = current_function_decl;
4151 : 23787 : SET_DECL_MODE (label, VOIDmode);
4152 : :
4153 : 23787 : c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
4154 : 23787 : label_vars->shadowed = NULL;
4155 : 23787 : set_spot_bindings (&label_vars->label_bindings, defining);
4156 : 23787 : label_vars->decls_in_scope = make_tree_vector ();
4157 : 23787 : label_vars->gotos = NULL;
4158 : 23787 : *p_label_vars = label_vars;
4159 : :
4160 : 23787 : return label;
4161 : : }
4162 : :
4163 : : /* Get the LABEL_DECL corresponding to identifier NAME as a label.
4164 : : Create one if none exists so far for the current function.
4165 : : This is called when a label is used in a goto expression or
4166 : : has its address taken. */
4167 : :
4168 : : tree
4169 : 85219 : lookup_label (tree name)
4170 : : {
4171 : 85219 : tree label;
4172 : 85219 : struct c_label_vars *label_vars;
4173 : :
4174 : 85219 : if (current_function_scope == 0)
4175 : : {
4176 : 2 : error ("label %qE referenced outside of any function", name);
4177 : 2 : return NULL_TREE;
4178 : : }
4179 : :
4180 : : /* Use a label already defined or ref'd with this name, but not if
4181 : : it is inherited from a containing function and wasn't declared
4182 : : using __label__. */
4183 : 85217 : label = I_LABEL_DECL (name);
4184 : 79365 : if (label && (DECL_CONTEXT (label) == current_function_decl
4185 : 608 : || C_DECLARED_LABEL_FLAG (label)))
4186 : : {
4187 : : /* If the label has only been declared, update its apparent
4188 : : location to point here, for better diagnostics if it
4189 : : turns out not to have been defined. */
4190 : 79358 : if (DECL_INITIAL (label) == NULL_TREE)
4191 : 62666 : DECL_SOURCE_LOCATION (label) = input_location;
4192 : 79358 : return label;
4193 : : }
4194 : :
4195 : : /* No label binding for that identifier; make one. */
4196 : 5859 : label = make_label (input_location, name, false, &label_vars);
4197 : :
4198 : : /* Ordinary labels go in the current function scope. */
4199 : 5859 : bind_label (name, label, current_function_scope, label_vars);
4200 : :
4201 : 5859 : return label;
4202 : : }
4203 : :
4204 : : /* Issue a warning about DECL for a goto statement at GOTO_LOC going
4205 : : to LABEL. */
4206 : :
4207 : : static void
4208 : 1482 : warn_about_goto (location_t goto_loc, tree label, tree decl)
4209 : : {
4210 : 1482 : auto_diagnostic_group d;
4211 : 1482 : if (c_type_variably_modified_p (TREE_TYPE (decl)))
4212 : 1476 : error_at (goto_loc,
4213 : : "jump into scope of identifier with variably modified type");
4214 : 6 : else if (flag_openmp
4215 : 6 : && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl)))
4216 : 2 : error_at (goto_loc, "jump skips OpenMP %<allocate%> allocation");
4217 : : else
4218 : 4 : if (!warning_at (goto_loc, OPT_Wjump_misses_init,
4219 : : "jump skips variable initialization"))
4220 : 0 : return;
4221 : 1482 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4222 : 1482 : inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
4223 : 1482 : }
4224 : :
4225 : : /* Look up a label because of a goto statement. This is like
4226 : : lookup_label, but also issues any appropriate warnings. */
4227 : :
4228 : : tree
4229 : 83387 : lookup_label_for_goto (location_t loc, tree name)
4230 : : {
4231 : 83387 : tree label;
4232 : 83387 : struct c_label_vars *label_vars;
4233 : 83387 : unsigned int ix;
4234 : 83387 : tree decl;
4235 : :
4236 : 83387 : label = lookup_label (name);
4237 : 83387 : if (label == NULL_TREE)
4238 : : return NULL_TREE;
4239 : :
4240 : : /* If we are jumping to a different function, we can't issue any
4241 : : useful warnings. */
4242 : 83387 : if (DECL_CONTEXT (label) != current_function_decl)
4243 : : {
4244 : 522 : gcc_assert (C_DECLARED_LABEL_FLAG (label));
4245 : : return label;
4246 : : }
4247 : :
4248 : 82865 : label_vars = I_LABEL_BINDING (name)->u.label;
4249 : :
4250 : : /* If the label has not yet been defined, then push this goto on a
4251 : : list for possible later warnings. */
4252 : 82865 : if (label_vars->label_bindings.scope == NULL)
4253 : : {
4254 : 66398 : c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
4255 : :
4256 : 66398 : g->loc = loc;
4257 : 66398 : set_spot_bindings (&g->goto_bindings, true);
4258 : 66398 : vec_safe_push (label_vars->gotos, g);
4259 : 66398 : return label;
4260 : : }
4261 : :
4262 : : /* If there are any decls in label_vars->decls_in_scope, then this
4263 : : goto has missed the declaration of the decl. This happens for a
4264 : : case like
4265 : : int i = 1;
4266 : : lab:
4267 : : ...
4268 : : goto lab;
4269 : : Issue a warning or error. */
4270 : 17242 : FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
4271 : 775 : warn_about_goto (loc, label, decl);
4272 : :
4273 : 16467 : if (label_vars->label_bindings.left_stmt_expr)
4274 : : {
4275 : 120 : auto_diagnostic_group d;
4276 : 120 : error_at (loc, "jump into statement expression");
4277 : 120 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4278 : 120 : }
4279 : :
4280 : : return label;
4281 : : }
4282 : :
4283 : : /* Make a label named NAME in the current function, shadowing silently
4284 : : any that may be inherited from containing functions or containing
4285 : : scopes. This is called for __label__ declarations. */
4286 : :
4287 : : tree
4288 : 1192 : declare_label (tree name)
4289 : : {
4290 : 1192 : struct c_binding *b = I_LABEL_BINDING (name);
4291 : 1192 : tree label;
4292 : 1192 : struct c_label_vars *label_vars;
4293 : :
4294 : : /* Check to make sure that the label hasn't already been declared
4295 : : at this scope */
4296 : 1192 : if (b && B_IN_CURRENT_SCOPE (b))
4297 : : {
4298 : 2 : auto_diagnostic_group d;
4299 : 2 : error ("duplicate label declaration %qE", name);
4300 : 2 : locate_old_decl (b->decl);
4301 : :
4302 : : /* Just use the previous declaration. */
4303 : 2 : return b->decl;
4304 : 2 : }
4305 : :
4306 : 1190 : label = make_label (input_location, name, false, &label_vars);
4307 : 1190 : C_DECLARED_LABEL_FLAG (label) = 1;
4308 : :
4309 : : /* Declared labels go in the current scope. */
4310 : 1190 : bind_label (name, label, current_scope, label_vars);
4311 : :
4312 : 1190 : return label;
4313 : : }
4314 : :
4315 : : /* When we define a label, issue any appropriate warnings if there are
4316 : : any gotos earlier in the function which jump to this label. */
4317 : :
4318 : : static void
4319 : 6969 : check_earlier_gotos (tree label, struct c_label_vars* label_vars)
4320 : : {
4321 : 6969 : unsigned int ix;
4322 : 6969 : struct c_goto_bindings *g;
4323 : :
4324 : 73301 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
4325 : : {
4326 : 66332 : struct c_binding *b;
4327 : 66332 : struct c_scope *scope;
4328 : :
4329 : : /* We have a goto to this label. The goto is going forward. In
4330 : : g->scope, the goto is going to skip any binding which was
4331 : : defined after g->bindings_in_scope. */
4332 : 66332 : if (g->goto_bindings.scope->has_jump_unsafe_decl)
4333 : : {
4334 : 251 : for (b = g->goto_bindings.scope->bindings;
4335 : 581 : b != g->goto_bindings.bindings_in_scope;
4336 : 330 : b = b->prev)
4337 : : {
4338 : 330 : if (decl_jump_unsafe (b->decl))
4339 : 168 : warn_about_goto (g->loc, label, b->decl);
4340 : : }
4341 : : }
4342 : :
4343 : : /* We also need to warn about decls defined in any scopes
4344 : : between the scope of the label and the scope of the goto. */
4345 : 66332 : for (scope = label_vars->label_bindings.scope;
4346 : 68978 : scope != g->goto_bindings.scope;
4347 : 2646 : scope = scope->outer)
4348 : : {
4349 : 2646 : gcc_assert (scope != NULL);
4350 : 2646 : if (scope->has_jump_unsafe_decl)
4351 : : {
4352 : 325 : if (scope == label_vars->label_bindings.scope)
4353 : 249 : b = label_vars->label_bindings.bindings_in_scope;
4354 : : else
4355 : 76 : b = scope->bindings;
4356 : 864 : for (; b != NULL; b = b->prev)
4357 : : {
4358 : 539 : if (decl_jump_unsafe (b->decl))
4359 : 539 : warn_about_goto (g->loc, label, b->decl);
4360 : : }
4361 : : }
4362 : : }
4363 : :
4364 : 66332 : if (g->goto_bindings.stmt_exprs > 0)
4365 : : {
4366 : 100 : auto_diagnostic_group d;
4367 : 100 : error_at (g->loc, "jump into statement expression");
4368 : 100 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
4369 : : label);
4370 : 100 : }
4371 : : }
4372 : :
4373 : : /* Now that the label is defined, we will issue warnings about
4374 : : subsequent gotos to this label when we see them. */
4375 : 6969 : vec_safe_truncate (label_vars->gotos, 0);
4376 : 6969 : label_vars->gotos = NULL;
4377 : 6969 : }
4378 : :
4379 : : /* Define a label, specifying the location in the source file.
4380 : : Return the LABEL_DECL node for the label, if the definition is valid.
4381 : : Otherwise return NULL_TREE. */
4382 : :
4383 : : tree
4384 : 23736 : define_label (location_t location, tree name)
4385 : : {
4386 : : /* Find any preexisting label with this name. It is an error
4387 : : if that label has already been defined in this function, or
4388 : : if there is a containing function with a declared label with
4389 : : the same name. */
4390 : 23736 : tree label = I_LABEL_DECL (name);
4391 : :
4392 : 7005 : if (label
4393 : 7005 : && ((DECL_CONTEXT (label) == current_function_decl
4394 : 6991 : && DECL_INITIAL (label) != NULL_TREE)
4395 : 6983 : || (DECL_CONTEXT (label) != current_function_decl
4396 : 14 : && C_DECLARED_LABEL_FLAG (label))))
4397 : : {
4398 : 29 : auto_diagnostic_group d;
4399 : 29 : error_at (location, "duplicate label %qD", label);
4400 : 29 : locate_old_decl (label);
4401 : 29 : return NULL_TREE;
4402 : 29 : }
4403 : 23707 : else if (label && DECL_CONTEXT (label) == current_function_decl)
4404 : : {
4405 : 6969 : struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
4406 : :
4407 : : /* The label has been used or declared already in this function,
4408 : : but not defined. Update its location to point to this
4409 : : definition. */
4410 : 6969 : DECL_SOURCE_LOCATION (label) = location;
4411 : 6969 : set_spot_bindings (&label_vars->label_bindings, true);
4412 : :
4413 : : /* Issue warnings as required about any goto statements from
4414 : : earlier in the function. */
4415 : 6969 : check_earlier_gotos (label, label_vars);
4416 : : }
4417 : : else
4418 : : {
4419 : 16738 : struct c_label_vars *label_vars;
4420 : :
4421 : : /* No label binding for that identifier; make one. */
4422 : 16738 : label = make_label (location, name, true, &label_vars);
4423 : :
4424 : : /* Ordinary labels go in the current function scope. */
4425 : 16738 : bind_label (name, label, current_function_scope, label_vars);
4426 : : }
4427 : :
4428 : 23707 : if (!in_system_header_at (input_location) && lookup_name (name))
4429 : 131 : warning_at (location, OPT_Wtraditional,
4430 : : "traditional C lacks a separate namespace "
4431 : : "for labels, identifier %qE conflicts", name);
4432 : :
4433 : : /* Mark label as having been defined. */
4434 : 23707 : DECL_INITIAL (label) = error_mark_node;
4435 : 23707 : return label;
4436 : : }
4437 : :
4438 : : /* Get the bindings for a new switch statement. This is used to issue
4439 : : warnings as appropriate for jumps from the switch to case or
4440 : : default labels. */
4441 : :
4442 : : struct c_spot_bindings *
4443 : 37141 : c_get_switch_bindings (void)
4444 : : {
4445 : 37141 : struct c_spot_bindings *switch_bindings;
4446 : :
4447 : 37141 : switch_bindings = XNEW (struct c_spot_bindings);
4448 : 37141 : set_spot_bindings (switch_bindings, true);
4449 : 37141 : return switch_bindings;
4450 : : }
4451 : :
4452 : : void
4453 : 37141 : c_release_switch_bindings (struct c_spot_bindings *bindings)
4454 : : {
4455 : 37141 : gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
4456 : 37141 : XDELETE (bindings);
4457 : 37141 : }
4458 : :
4459 : : /* This is called at the point of a case or default label to issue
4460 : : warnings about decls as needed. It returns true if it found an
4461 : : error, not just a warning. */
4462 : :
4463 : : bool
4464 : 1029160 : c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
4465 : : location_t switch_loc, location_t case_loc)
4466 : : {
4467 : 1029160 : bool saw_error;
4468 : 1029160 : struct c_scope *scope;
4469 : :
4470 : 1029160 : saw_error = false;
4471 : 1029160 : for (scope = current_scope;
4472 : 3087508 : scope != switch_bindings->scope;
4473 : 2058348 : scope = scope->outer)
4474 : : {
4475 : 2058348 : struct c_binding *b;
4476 : :
4477 : 2058348 : gcc_assert (scope != NULL);
4478 : :
4479 : 2058348 : if (!scope->has_jump_unsafe_decl)
4480 : 2058337 : continue;
4481 : :
4482 : 22 : for (b = scope->bindings; b != NULL; b = b->prev)
4483 : : {
4484 : 11 : if (decl_jump_unsafe (b->decl))
4485 : : {
4486 : 11 : auto_diagnostic_group d;
4487 : 11 : bool emitted;
4488 : 11 : if (c_type_variably_modified_p (TREE_TYPE (b->decl)))
4489 : : {
4490 : 6 : saw_error = true;
4491 : 6 : error_at (case_loc,
4492 : : "switch jumps into scope of identifier with "
4493 : : "variably modified type");
4494 : 6 : emitted = true;
4495 : : }
4496 : 5 : else if (flag_openmp
4497 : 7 : && lookup_attribute ("omp allocate",
4498 : 2 : DECL_ATTRIBUTES (b->decl)))
4499 : : {
4500 : 2 : saw_error = true;
4501 : 2 : error_at (case_loc,
4502 : : "switch jumps over OpenMP %<allocate%> allocation");
4503 : 2 : emitted = true;
4504 : : }
4505 : : else
4506 : 3 : emitted
4507 : 3 : = warning_at (case_loc, OPT_Wjump_misses_init,
4508 : : "switch jumps over variable initialization");
4509 : 11 : if (emitted)
4510 : : {
4511 : 11 : inform (switch_loc, "switch starts here");
4512 : 11 : inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
4513 : : b->decl);
4514 : : }
4515 : 11 : }
4516 : : }
4517 : : }
4518 : :
4519 : 1029160 : if (switch_bindings->stmt_exprs > 0)
4520 : : {
4521 : 4 : saw_error = true;
4522 : 4 : auto_diagnostic_group d;
4523 : 4 : error_at (case_loc, "switch jumps into statement expression");
4524 : 4 : inform (switch_loc, "switch starts here");
4525 : 4 : }
4526 : :
4527 : 1029160 : return saw_error;
4528 : : }
4529 : :
4530 : : /* Given NAME, an IDENTIFIER_NODE,
4531 : : return the structure (or union or enum) definition for that name.
4532 : : If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4533 : : CODE says which kind of type the caller wants;
4534 : : it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4535 : : If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4536 : : location where the tag was defined.
4537 : : If the wrong kind of type is found, an error is reported. */
4538 : :
4539 : : static tree
4540 : 2066167 : lookup_tag (enum tree_code code, tree name, bool thislevel_only,
4541 : : location_t *ploc)
4542 : : {
4543 : 2066167 : struct c_binding *b = I_TAG_BINDING (name);
4544 : 2066167 : bool thislevel = false;
4545 : :
4546 : 2066167 : if (!b || !b->decl)
4547 : : return NULL_TREE;
4548 : :
4549 : : /* We only care about whether it's in this level if
4550 : : thislevel_only was set or it might be a type clash. */
4551 : 1445972 : if (thislevel_only || TREE_CODE (b->decl) != code)
4552 : : {
4553 : : /* For our purposes, a tag in the external scope is the same as
4554 : : a tag in the file scope. (Primarily relevant to Objective-C
4555 : : and its builtin structure tags, which get pushed before the
4556 : : file scope is created.) */
4557 : 464024 : if (B_IN_CURRENT_SCOPE (b)
4558 : 179 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
4559 : 1445972 : thislevel = true;
4560 : : }
4561 : :
4562 : 1445972 : if (thislevel_only && !thislevel)
4563 : : return NULL_TREE;
4564 : :
4565 : 1445814 : if (TREE_CODE (b->decl) != code)
4566 : : {
4567 : : /* Definition isn't the kind we were looking for. */
4568 : 39 : pending_invalid_xref = name;
4569 : 39 : pending_invalid_xref_location = input_location;
4570 : :
4571 : : /* If in the same binding level as a declaration as a tag
4572 : : of a different type, this must not be allowed to
4573 : : shadow that tag, so give the error immediately.
4574 : : (For example, "struct foo; union foo;" is invalid.) */
4575 : 39 : if (thislevel)
4576 : 18 : pending_xref_error ();
4577 : : }
4578 : :
4579 : 1445814 : if (ploc != NULL)
4580 : 1007252 : *ploc = b->locus;
4581 : :
4582 : 1445814 : return b->decl;
4583 : : }
4584 : :
4585 : : /* Return true if a definition exists for NAME with code CODE. */
4586 : :
4587 : : bool
4588 : 390 : tag_exists_p (enum tree_code code, tree name)
4589 : : {
4590 : 390 : struct c_binding *b = I_TAG_BINDING (name);
4591 : :
4592 : 390 : if (b == NULL || b->decl == NULL_TREE)
4593 : : return false;
4594 : 18 : return TREE_CODE (b->decl) == code;
4595 : : }
4596 : :
4597 : : /* Print an error message now
4598 : : for a recent invalid struct, union or enum cross reference.
4599 : : We don't print them immediately because they are not invalid
4600 : : when used in the `struct foo;' construct for shadowing. */
4601 : :
4602 : : void
4603 : 311858350 : pending_xref_error (void)
4604 : : {
4605 : 311858350 : if (pending_invalid_xref != NULL_TREE)
4606 : 27 : error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4607 : : pending_invalid_xref);
4608 : 311858350 : pending_invalid_xref = NULL_TREE;
4609 : 311858350 : }
4610 : :
4611 : :
4612 : : /* Look up NAME in the current scope and its superiors
4613 : : in the namespace of variables, functions and typedefs.
4614 : : Return a ..._DECL node of some kind representing its definition,
4615 : : or return NULL_TREE if it is undefined. */
4616 : :
4617 : : tree
4618 : 1260444355 : lookup_name (tree name)
4619 : : {
4620 : 1260444355 : struct c_binding *b = I_SYMBOL_BINDING (name);
4621 : : /* Do not resolve non-default function versions. */
4622 : 1260444355 : if (b
4623 : 852335929 : && TREE_CODE (b->decl) == FUNCTION_DECL
4624 : 116512569 : && DECL_FUNCTION_VERSIONED (b->decl)
4625 : 1260444355 : && !is_function_default_version (b->decl))
4626 : : return NULL_TREE;
4627 : 1260444355 : if (b && !b->invisible)
4628 : : {
4629 : 841646175 : maybe_record_typedef_use (b->decl);
4630 : 841646175 : return b->decl;
4631 : : }
4632 : : return NULL_TREE;
4633 : : }
4634 : :
4635 : : /* Similar to `lookup_name' but look only at the indicated scope. */
4636 : :
4637 : : static tree
4638 : 130399292 : lookup_name_in_scope (tree name, struct c_scope *scope)
4639 : : {
4640 : 130399292 : struct c_binding *b;
4641 : :
4642 : 130402519 : for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4643 : 7340889 : if (B_IN_SCOPE (b, scope))
4644 : 7337662 : return b->decl;
4645 : : return NULL_TREE;
4646 : : }
4647 : :
4648 : : /* Look for the closest match for NAME within the currently valid
4649 : : scopes.
4650 : :
4651 : : This finds the identifier with the lowest Levenshtein distance to
4652 : : NAME. If there are multiple candidates with equal minimal distance,
4653 : : the first one found is returned. Scopes are searched from innermost
4654 : : outwards, and within a scope in reverse order of declaration, thus
4655 : : benefiting candidates "near" to the current scope.
4656 : :
4657 : : The function also looks for similar macro names to NAME, since a
4658 : : misspelled macro name will not be expanded, and hence looks like an
4659 : : identifier to the C frontend.
4660 : :
4661 : : It also looks for start_typename keywords, to detect "singed" vs "signed"
4662 : : typos.
4663 : :
4664 : : Use LOC for any deferred diagnostics. */
4665 : :
4666 : : name_hint
4667 : 2235 : lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4668 : : {
4669 : 2235 : gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4670 : :
4671 : : /* First, try some well-known names in the C standard library, in case
4672 : : the user forgot a #include. */
4673 : 2235 : const char *header_hint
4674 : 2235 : = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4675 : :
4676 : 2235 : if (header_hint)
4677 : 56 : return name_hint
4678 : : (nullptr,
4679 : 56 : std::make_unique<suggest_missing_header> (loc,
4680 : 112 : IDENTIFIER_POINTER (name),
4681 : 56 : header_hint));
4682 : :
4683 : : /* Next, look for exact matches for builtin defines that would have been
4684 : : defined if the user had passed a command-line option (e.g. -fopenmp
4685 : : for "_OPENMP"). */
4686 : 2179 : diagnostics::option_id option_id
4687 : 2179 : = get_option_for_builtin_define (IDENTIFIER_POINTER (name));
4688 : 2179 : if (option_id.m_idx > 0)
4689 : 2 : return name_hint
4690 : : (nullptr,
4691 : 2 : std::make_unique<suggest_missing_option> (loc,
4692 : 4 : IDENTIFIER_POINTER (name),
4693 : 2 : option_id));
4694 : :
4695 : : /* Only suggest names reserved for the implementation if NAME begins
4696 : : with an underscore. */
4697 : 2177 : bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4698 : :
4699 : 2177 : best_match<tree, tree> bm (name);
4700 : :
4701 : : /* Look within currently valid scopes. */
4702 : 9628 : for (c_scope *scope = current_scope; scope; scope = scope->outer)
4703 : 12212347 : for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4704 : : {
4705 : 12204896 : if (!binding->id || binding->invisible)
4706 : 6926282 : continue;
4707 : 5278614 : if (binding->decl == error_mark_node)
4708 : 358 : continue;
4709 : : /* Don't use bindings from implicitly declared functions,
4710 : : as they were likely misspellings themselves. */
4711 : 5278256 : if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4712 : 4865920 : if (C_DECL_IMPLICIT (binding->decl))
4713 : 229 : continue;
4714 : : /* Don't suggest names that are reserved for use by the
4715 : : implementation, unless NAME began with an underscore. */
4716 : 5278027 : if (!consider_implementation_names)
4717 : : {
4718 : 2327189 : const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4719 : 2327189 : if (name_reserved_for_implementation_p (suggestion_str))
4720 : 2269314 : continue;
4721 : : }
4722 : 3008713 : switch (kind)
4723 : : {
4724 : 37643 : case FUZZY_LOOKUP_TYPENAME:
4725 : 37643 : if (TREE_CODE (binding->decl) != TYPE_DECL)
4726 : 30038 : continue;
4727 : : break;
4728 : :
4729 : 128923 : case FUZZY_LOOKUP_FUNCTION_NAME:
4730 : 128923 : if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4731 : : {
4732 : : /* Allow function pointers. */
4733 : 26502 : if ((VAR_P (binding->decl)
4734 : 21977 : || TREE_CODE (binding->decl) == PARM_DECL)
4735 : 5400 : && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4736 : 27782 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4737 : : == FUNCTION_TYPE))
4738 : : break;
4739 : 26483 : continue;
4740 : : }
4741 : : break;
4742 : :
4743 : : default:
4744 : : break;
4745 : : }
4746 : 2952192 : bm.consider (binding->id);
4747 : : }
4748 : :
4749 : : /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4750 : : as:
4751 : : x = SOME_OTHER_MACRO (y);
4752 : : then "SOME_OTHER_MACRO" will survive to the frontend and show up
4753 : : as a misspelled identifier.
4754 : :
4755 : : Use the best distance so far so that a candidate is only set if
4756 : : a macro is better than anything so far. This allows early rejection
4757 : : (without calculating the edit distance) of macro names that must have
4758 : : distance >= bm.get_best_distance (), and means that we only get a
4759 : : non-NULL result for best_macro_match if it's better than any of
4760 : : the identifiers already checked, which avoids needless creation
4761 : : of identifiers for macro hashnodes. */
4762 : 2177 : best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4763 : 2177 : cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4764 : : /* If a macro is the closest so far to NAME, use it, creating an
4765 : : identifier tree node for it. */
4766 : 2177 : if (best_macro)
4767 : : {
4768 : 11 : const char *id = (const char *)best_macro->ident.str;
4769 : 11 : tree macro_as_identifier
4770 : 11 : = get_identifier_with_length (id, best_macro->ident.len);
4771 : 11 : bm.set_best_so_far (macro_as_identifier,
4772 : : bmm.get_best_distance (),
4773 : : bmm.get_best_candidate_length ());
4774 : : }
4775 : :
4776 : : /* Try the "start_typename" keywords to detect
4777 : : "singed" vs "signed" typos. */
4778 : 2177 : if (kind == FUZZY_LOOKUP_TYPENAME)
4779 : : {
4780 : 56105 : for (unsigned i = 0; i < num_c_common_reswords; i++)
4781 : : {
4782 : 55860 : const c_common_resword *resword = &c_common_reswords[i];
4783 : 55860 : if (!c_keyword_starts_typename (resword->rid))
4784 : 42385 : continue;
4785 : 13475 : tree resword_identifier = ridpointers [resword->rid];
4786 : 13475 : if (!resword_identifier)
4787 : 30 : continue;
4788 : 13445 : gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4789 : 13445 : bm.consider (resword_identifier);
4790 : : }
4791 : : }
4792 : :
4793 : 2177 : tree best = bm.get_best_meaningful_candidate ();
4794 : 2177 : if (best)
4795 : 275 : return name_hint (IDENTIFIER_POINTER (best), NULL);
4796 : : else
4797 : 1902 : return name_hint (NULL, NULL);
4798 : : }
4799 : :
4800 : :
4801 : : /* Handle the standard [[nodiscard]] attribute. */
4802 : :
4803 : : static tree
4804 : 34 : handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4805 : : int /*flags*/, bool *no_add_attrs)
4806 : : {
4807 : 34 : if (TREE_CODE (*node) == FUNCTION_DECL)
4808 : : {
4809 : 18 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4810 : 1 : warning_at (DECL_SOURCE_LOCATION (*node),
4811 : 1 : OPT_Wattributes, "%qE attribute applied to %qD with void "
4812 : : "return type", name, *node);
4813 : : }
4814 : 16 : else if (RECORD_OR_UNION_TYPE_P (*node)
4815 : 12 : || TREE_CODE (*node) == ENUMERAL_TYPE)
4816 : : /* OK */;
4817 : : else
4818 : : {
4819 : 10 : pedwarn (input_location,
4820 : 10 : OPT_Wattributes, "%qE attribute can only be applied to "
4821 : : "functions or to structure, union or enumeration types", name);
4822 : 10 : *no_add_attrs = true;
4823 : : }
4824 : 34 : return NULL_TREE;
4825 : : }
4826 : :
4827 : : /* Handle the standard [[noreturn]] attribute. */
4828 : :
4829 : : static tree
4830 : 44 : handle_std_noreturn_attribute (tree *node, tree name, tree args,
4831 : : int flags, bool *no_add_attrs)
4832 : : {
4833 : : /* Unlike GNU __attribute__ ((noreturn)), the standard [[noreturn]]
4834 : : only applies to functions, not function pointers. */
4835 : 44 : if (TREE_CODE (*node) == FUNCTION_DECL)
4836 : 22 : return handle_noreturn_attribute (node, name, args, flags, no_add_attrs);
4837 : : else
4838 : : {
4839 : 22 : pedwarn (input_location, OPT_Wattributes,
4840 : : "standard %qE attribute can only be applied to functions",
4841 : : name);
4842 : 22 : *no_add_attrs = true;
4843 : 22 : return NULL_TREE;
4844 : : }
4845 : : }
4846 : :
4847 : : /* Handle the standard [[unsequenced]] attribute. */
4848 : :
4849 : : static tree
4850 : 68 : handle_std_unsequenced_attribute (tree *node, tree name, tree args,
4851 : : int flags, bool *no_add_attrs)
4852 : : {
4853 : : /* Unlike GNU __attribute__ ((unsequenced)), the standard [[unsequenced]]
4854 : : should be only applied to function declarators or type specifiers which
4855 : : have function type. */
4856 : 68 : if (node[2])
4857 : : {
4858 : 8 : auto_diagnostic_group d;
4859 : 8 : if (pedwarn (input_location, OPT_Wattributes,
4860 : : "standard %qE attribute can only be applied to function "
4861 : : "declarators or type specifiers with function type", name))
4862 : 8 : inform (input_location, "did you mean to specify it after %<)%> "
4863 : : "following function parameters?");
4864 : 8 : *no_add_attrs = true;
4865 : 8 : return NULL_TREE;
4866 : 8 : }
4867 : 60 : return handle_unsequenced_attribute (node, name, args, flags, no_add_attrs);
4868 : : }
4869 : :
4870 : : /* Handle the standard [[reproducible]] attribute. */
4871 : :
4872 : : static tree
4873 : 34 : handle_std_reproducible_attribute (tree *node, tree name, tree args,
4874 : : int flags, bool *no_add_attrs)
4875 : : {
4876 : 34 : return handle_std_unsequenced_attribute (node, name, args, flags,
4877 : 34 : no_add_attrs);
4878 : : }
4879 : :
4880 : : /* Table of supported standard (C23) attributes. */
4881 : : static const attribute_spec std_attributes[] =
4882 : : {
4883 : : /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4884 : : affects_type_identity, handler, exclude } */
4885 : : { "_Noreturn", 0, 0, false, false, false, false,
4886 : : handle_std_noreturn_attribute, NULL },
4887 : : { "deprecated", 0, 1, false, false, false, false,
4888 : : handle_deprecated_attribute, NULL },
4889 : : { "fallthrough", 0, 0, false, false, false, false,
4890 : : handle_fallthrough_attribute, NULL },
4891 : : { "maybe_unused", 0, 0, false, false, false, false,
4892 : : handle_unused_attribute, NULL },
4893 : : { "nodiscard", 0, 1, false, false, false, false,
4894 : : handle_nodiscard_attribute, NULL },
4895 : : { "noreturn", 0, 0, false, false, false, false,
4896 : : handle_std_noreturn_attribute, NULL },
4897 : : { "reproducible", 0, 0, false, true, true, false,
4898 : : handle_std_reproducible_attribute, NULL },
4899 : : { "unsequenced", 0, 0, false, true, true, false,
4900 : : handle_std_unsequenced_attribute, NULL }
4901 : : };
4902 : :
4903 : : const scoped_attribute_specs std_attribute_table =
4904 : : {
4905 : : nullptr, { std_attributes }
4906 : : };
4907 : :
4908 : : /* Create the predefined scalar types of C,
4909 : : and some nodes representing standard constants (0, 1, (void *) 0).
4910 : : Initialize the global scope.
4911 : : Make definitions for built-in primitive functions. */
4912 : :
4913 : : void
4914 : 113264 : c_init_decl_processing (void)
4915 : : {
4916 : 113264 : location_t save_loc = input_location;
4917 : :
4918 : : /* Initialize reserved words for parser. */
4919 : 113264 : c_parse_init ();
4920 : :
4921 : 113264 : current_function_decl = NULL_TREE;
4922 : :
4923 : 113264 : gcc_obstack_init (&parser_obstack);
4924 : :
4925 : : /* Make the externals scope. */
4926 : 113264 : push_scope ();
4927 : 113264 : external_scope = current_scope;
4928 : :
4929 : : /* Declarations from c_common_nodes_and_builtins must not be associated
4930 : : with this input file, lest we get differences between using and not
4931 : : using preprocessed headers. */
4932 : 113264 : input_location = BUILTINS_LOCATION;
4933 : :
4934 : 113264 : c_common_nodes_and_builtins ();
4935 : :
4936 : : /* In C, comparisons and TRUTH_* expressions have type int. */
4937 : 113264 : truthvalue_type_node = integer_type_node;
4938 : 113264 : truthvalue_true_node = integer_one_node;
4939 : 113264 : truthvalue_false_node = integer_zero_node;
4940 : :
4941 : : /* Even in C99, which has a real boolean type. */
4942 : 113264 : pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4943 : : boolean_type_node));
4944 : :
4945 : : /* C-specific nullptr initialization. */
4946 : 113264 : record_builtin_type (RID_MAX, "typeof (nullptr)", nullptr_type_node);
4947 : : /* The size and alignment of nullptr_t is the same as for a pointer to
4948 : : character type. */
4949 : 113264 : SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4950 : :
4951 : 113264 : input_location = save_loc;
4952 : :
4953 : 113264 : make_fname_decl = c_make_fname_decl;
4954 : 113264 : start_fname_decls ();
4955 : :
4956 : 113264 : if (warn_keyword_macro)
4957 : : {
4958 : 1145 : for (unsigned int i = 0; i < num_c_common_reswords; ++i)
4959 : : /* For C register keywords which don't start with underscore
4960 : : or start with just single underscore. Don't complain about
4961 : : ObjC or Transactional Memory keywords. */
4962 : 1140 : if (c_common_reswords[i].word[0] == '_'
4963 : 515 : && c_common_reswords[i].word[1] == '_')
4964 : 385 : continue;
4965 : 980 : else if (c_common_reswords[i].disable
4966 : 755 : & (D_TRANSMEM | D_OBJC | D_CXX_OBJC))
4967 : 225 : continue;
4968 : : else
4969 : : {
4970 : 530 : tree id = get_identifier (c_common_reswords[i].word);
4971 : 530 : if (C_IS_RESERVED_WORD (id)
4972 : 530 : && C_RID_CODE (id) != RID_CXX_COMPAT_WARN)
4973 : 322 : cpp_warn (parse_in, IDENTIFIER_POINTER (id),
4974 : 322 : IDENTIFIER_LENGTH (id));
4975 : : }
4976 : : }
4977 : 113264 : }
4978 : :
4979 : : /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4980 : : give the decl, NAME is the initialization string and TYPE_DEP
4981 : : indicates whether NAME depended on the type of the function. As we
4982 : : don't yet implement delayed emission of static data, we mark the
4983 : : decl as emitted so it is not placed in the output. Anything using
4984 : : it must therefore pull out the STRING_CST initializer directly.
4985 : : FIXME. */
4986 : :
4987 : : static tree
4988 : 2887 : c_make_fname_decl (location_t loc, tree id, int type_dep)
4989 : : {
4990 : 2887 : const char *name = fname_as_string (type_dep);
4991 : 2887 : tree decl, type, init;
4992 : 2887 : size_t length = strlen (name);
4993 : :
4994 : 2887 : type = c_build_array_type (char_type_node,
4995 : 2887 : build_index_type (size_int (length)));
4996 : 2887 : type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4997 : :
4998 : 2887 : decl = build_decl (loc, VAR_DECL, id, type);
4999 : :
5000 : 2887 : TREE_STATIC (decl) = 1;
5001 : 2887 : TREE_READONLY (decl) = 1;
5002 : 2887 : DECL_ARTIFICIAL (decl) = 1;
5003 : :
5004 : 2887 : init = build_string (length + 1, name);
5005 : 2887 : free (CONST_CAST (char *, name));
5006 : 2887 : TREE_TYPE (init) = type;
5007 : 2887 : DECL_INITIAL (decl) = init;
5008 : :
5009 : 2887 : TREE_USED (decl) = 1;
5010 : :
5011 : 2887 : if (current_function_decl
5012 : : /* For invalid programs like this:
5013 : :
5014 : : void foo()
5015 : : const char* p = __FUNCTION__;
5016 : :
5017 : : the __FUNCTION__ is believed to appear in K&R style function
5018 : : parameter declarator. In that case we still don't have
5019 : : function_scope. */
5020 : 2880 : && current_function_scope)
5021 : : {
5022 : 2872 : DECL_CONTEXT (decl) = current_function_decl;
5023 : 2872 : bind (id, decl, current_function_scope,
5024 : : /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
5025 : : }
5026 : :
5027 : 2887 : finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
5028 : :
5029 : 2887 : return decl;
5030 : : }
5031 : :
5032 : : tree
5033 : 330983803 : c_builtin_function (tree decl)
5034 : : {
5035 : 330983803 : tree type = TREE_TYPE (decl);
5036 : 330983803 : tree id = DECL_NAME (decl);
5037 : :
5038 : 330983803 : const char *name = IDENTIFIER_POINTER (id);
5039 : 330983803 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
5040 : :
5041 : : /* Should never be called on a symbol with a preexisting meaning. */
5042 : 330983803 : gcc_assert (!I_SYMBOL_BINDING (id));
5043 : :
5044 : 330983803 : bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
5045 : : UNKNOWN_LOCATION);
5046 : :
5047 : : /* Builtins in the implementation namespace are made visible without
5048 : : needing to be explicitly declared. See push_file_scope. */
5049 : 330983803 : if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
5050 : : {
5051 : 225762707 : DECL_CHAIN (decl) = visible_builtins;
5052 : 225762707 : visible_builtins = decl;
5053 : : }
5054 : :
5055 : 330983803 : return decl;
5056 : : }
5057 : :
5058 : : tree
5059 : 10434376 : c_builtin_function_ext_scope (tree decl)
5060 : : {
5061 : 10434376 : tree type = TREE_TYPE (decl);
5062 : 10434376 : tree id = DECL_NAME (decl);
5063 : :
5064 : 10434376 : const char *name = IDENTIFIER_POINTER (id);
5065 : 10434376 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
5066 : :
5067 : 10434376 : if (external_scope)
5068 : 10247491 : bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
5069 : : UNKNOWN_LOCATION);
5070 : :
5071 : : /* Builtins in the implementation namespace are made visible without
5072 : : needing to be explicitly declared. See push_file_scope. */
5073 : 10434376 : if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
5074 : : {
5075 : 10434376 : DECL_CHAIN (decl) = visible_builtins;
5076 : 10434376 : visible_builtins = decl;
5077 : : }
5078 : :
5079 : 10434376 : return decl;
5080 : : }
5081 : :
5082 : : /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5083 : :
5084 : : tree
5085 : 0 : c_simulate_builtin_function_decl (tree decl)
5086 : : {
5087 : 0 : tree type = TREE_TYPE (decl);
5088 : 0 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
5089 : 0 : return pushdecl (decl);
5090 : : }
5091 : :
5092 : : /* Warn about attributes in a context where they are unused
5093 : : (attribute-declarations, except for the "fallthrough" case, and
5094 : : attributes on statements). */
5095 : :
5096 : : void
5097 : 41383131 : c_warn_unused_attributes (tree attrs)
5098 : : {
5099 : 41383176 : for (tree t = attrs; t != NULL_TREE; t = TREE_CHAIN (t))
5100 : 45 : if (get_attribute_namespace (t) == NULL_TREE)
5101 : : /* The specifications of standard attributes mean this is a
5102 : : constraint violation. */
5103 : 29 : pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
5104 : : get_attribute_name (t));
5105 : 16 : else if (!attribute_ignored_p (t))
5106 : 12 : warning (OPT_Wattributes, "%qE attribute ignored",
5107 : : get_attribute_name (t));
5108 : 41383131 : }
5109 : :
5110 : : /* Warn for standard attributes being applied to a type that is not
5111 : : being defined, where that is a constraint violation, and return a
5112 : : list of attributes with them removed. */
5113 : :
5114 : : tree
5115 : 433234593 : c_warn_type_attributes (tree type, tree attrs)
5116 : : {
5117 : 433234593 : tree *attr_ptr = &attrs;
5118 : 433257648 : while (*attr_ptr)
5119 : 23055 : if (get_attribute_namespace (*attr_ptr) == NULL_TREE)
5120 : : {
5121 : 89 : if (TREE_CODE (type) == FUNCTION_TYPE)
5122 : : {
5123 : 65 : tree name = get_attribute_name (*attr_ptr);
5124 : : /* [[unsequenced]] and [[reproducible]] is fine on function
5125 : : types that aren't being defined. */
5126 : 65 : if (is_attribute_p ("unsequenced", name)
5127 : 65 : || is_attribute_p ("reproducible", name))
5128 : : {
5129 : 60 : attr_ptr = &TREE_CHAIN (*attr_ptr);
5130 : 60 : continue;
5131 : : }
5132 : : }
5133 : 29 : pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
5134 : : get_attribute_name (*attr_ptr));
5135 : 29 : *attr_ptr = TREE_CHAIN (*attr_ptr);
5136 : : }
5137 : : else
5138 : 22966 : attr_ptr = &TREE_CHAIN (*attr_ptr);
5139 : 433234593 : return attrs;
5140 : : }
5141 : :
5142 : : /* Called when a declaration is seen that contains no names to declare.
5143 : : If its type is a reference to a structure, union or enum inherited
5144 : : from a containing scope, shadow that tag name for the current scope
5145 : : with a forward reference.
5146 : : If its type defines a new named structure or union
5147 : : or defines an enum, it is valid but we need not do anything here.
5148 : : Otherwise, it is an error. */
5149 : :
5150 : : void
5151 : 497261 : shadow_tag (const struct c_declspecs *declspecs)
5152 : : {
5153 : 497261 : shadow_tag_warned (declspecs, 0);
5154 : 497261 : }
5155 : :
5156 : : /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
5157 : : but no pedwarn. */
5158 : : void
5159 : 497331 : shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
5160 : : {
5161 : 497331 : bool found_tag = false;
5162 : :
5163 : 497331 : if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
5164 : : {
5165 : 497217 : tree value = declspecs->type;
5166 : 497217 : enum tree_code code = TREE_CODE (value);
5167 : :
5168 : 497217 : if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
5169 : : /* Used to test also that TYPE_SIZE (value) != 0.
5170 : : That caused warning for `struct foo;' at top level in the file. */
5171 : : {
5172 : 497166 : tree name = TYPE_NAME (value);
5173 : 497166 : tree t;
5174 : :
5175 : 497166 : found_tag = true;
5176 : :
5177 : 497166 : if (declspecs->restrict_p)
5178 : : {
5179 : 2 : error ("invalid use of %<restrict%>");
5180 : 2 : warned = 1;
5181 : : }
5182 : :
5183 : 497166 : if (in_underspecified_init)
5184 : : {
5185 : : /* This can only occur with extensions such as statement
5186 : : expressions, but is still appropriate as an error to
5187 : : avoid types declared in such a context escaping to
5188 : : the type of an auto variable. */
5189 : 1 : error ("%qT declared in underspecified object initializer",
5190 : : value);
5191 : 1 : warned = 1;
5192 : : }
5193 : :
5194 : 497166 : if (name == NULL_TREE)
5195 : : {
5196 : 58593 : if (warned != 1 && code != ENUMERAL_TYPE)
5197 : : /* Empty unnamed enum OK */
5198 : : {
5199 : 19 : pedwarn (input_location, 0,
5200 : : "unnamed struct/union that defines no instances");
5201 : 19 : warned = 1;
5202 : : }
5203 : : }
5204 : 438573 : else if (declspecs->typespec_kind != ctsk_tagdef
5205 : 87742 : && declspecs->typespec_kind != ctsk_tagfirstref
5206 : 26122 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5207 : 26114 : && declspecs->storage_class != csc_none)
5208 : : {
5209 : 2 : if (warned != 1)
5210 : 2 : pedwarn (input_location, 0,
5211 : : "empty declaration with storage class specifier "
5212 : : "does not redeclare tag");
5213 : 2 : warned = 1;
5214 : 2 : pending_xref_error ();
5215 : : }
5216 : 438571 : else if (declspecs->typespec_kind != ctsk_tagdef
5217 : 87740 : && declspecs->typespec_kind != ctsk_tagfirstref
5218 : 26120 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5219 : 26112 : && (declspecs->const_p
5220 : 26109 : || declspecs->volatile_p
5221 : 26109 : || declspecs->atomic_p
5222 : 26109 : || declspecs->restrict_p
5223 : 26109 : || declspecs->address_space))
5224 : : {
5225 : 3 : if (warned != 1)
5226 : 3 : pedwarn (input_location, 0,
5227 : : "empty declaration with type qualifier "
5228 : : "does not redeclare tag");
5229 : 3 : warned = 1;
5230 : 3 : pending_xref_error ();
5231 : : }
5232 : 438568 : else if (declspecs->typespec_kind != ctsk_tagdef
5233 : 87737 : && declspecs->typespec_kind != ctsk_tagfirstref
5234 : 26117 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5235 : 26109 : && declspecs->alignas_p)
5236 : : {
5237 : 1 : if (warned != 1)
5238 : 1 : pedwarn (input_location, 0,
5239 : : "empty declaration with %<_Alignas%> "
5240 : : "does not redeclare tag");
5241 : 1 : warned = 1;
5242 : 1 : pending_xref_error ();
5243 : : }
5244 : 438567 : else if (declspecs->typespec_kind != ctsk_tagdef
5245 : 87736 : && declspecs->typespec_kind != ctsk_tagfirstref
5246 : 26116 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5247 : 26108 : && code == ENUMERAL_TYPE
5248 : 15 : && !declspecs->enum_type_specifier_ref_p)
5249 : : {
5250 : 3 : bool warned_enum = false;
5251 : 3 : if (warned != 1)
5252 : 3 : warned_enum = pedwarn (input_location, OPT_Wpedantic,
5253 : : "empty declaration of %<enum%> type "
5254 : : "does not redeclare tag");
5255 : 3 : if (warned_enum)
5256 : 2 : warned = 1;
5257 : 3 : pending_xref_error ();
5258 : 3 : }
5259 : : else
5260 : : {
5261 : 438564 : pending_invalid_xref = NULL_TREE;
5262 : 438564 : t = lookup_tag (code, name, true, NULL);
5263 : :
5264 : 438564 : if (t == NULL_TREE)
5265 : : {
5266 : 2 : t = make_node (code);
5267 : 2 : if (flag_isoc23 || code == ENUMERAL_TYPE)
5268 : 1 : SET_TYPE_STRUCTURAL_EQUALITY (t);
5269 : 2 : pushtag (input_location, name, t);
5270 : : }
5271 : : }
5272 : : }
5273 : : else
5274 : : {
5275 : 51 : if (warned != 1 && !in_system_header_at (input_location))
5276 : : {
5277 : 44 : pedwarn (input_location, 0,
5278 : : "useless type name in empty declaration");
5279 : 44 : warned = 1;
5280 : : }
5281 : : }
5282 : : }
5283 : 71 : else if (warned != 1 && !in_system_header_at (input_location)
5284 : 185 : && declspecs->typedef_p)
5285 : : {
5286 : 11 : pedwarn (input_location, 0, "useless type name in empty declaration");
5287 : 11 : warned = 1;
5288 : : }
5289 : :
5290 : 497331 : pending_invalid_xref = NULL_TREE;
5291 : :
5292 : 497331 : if (declspecs->inline_p)
5293 : : {
5294 : 5 : error ("%<inline%> in empty declaration");
5295 : 5 : warned = 1;
5296 : : }
5297 : :
5298 : 497331 : if (declspecs->noreturn_p)
5299 : : {
5300 : 2 : error ("%<_Noreturn%> in empty declaration");
5301 : 2 : warned = 1;
5302 : : }
5303 : :
5304 : 497331 : if (declspecs->constexpr_p)
5305 : : {
5306 : 7 : error ("%<constexpr%> in empty declaration");
5307 : 7 : warned = 1;
5308 : : }
5309 : :
5310 : 497331 : if (current_scope == file_scope && declspecs->storage_class == csc_auto)
5311 : : {
5312 : 2 : error ("%<auto%> in file-scope empty declaration");
5313 : 2 : warned = 1;
5314 : : }
5315 : :
5316 : 497331 : if (current_scope == file_scope && declspecs->storage_class == csc_register)
5317 : : {
5318 : 2 : error ("%<register%> in file-scope empty declaration");
5319 : 2 : warned = 1;
5320 : : }
5321 : :
5322 : 497331 : if (declspecs->enum_type_specifier_ref_p && !warned)
5323 : : {
5324 : 39 : if (declspecs->storage_class != csc_none)
5325 : : {
5326 : 1 : error ("storage class specifier in empty declaration with %<enum%> "
5327 : : "underlying type");
5328 : 1 : warned = 1;
5329 : : }
5330 : 38 : else if (declspecs->thread_p)
5331 : : {
5332 : 2 : error ("%qs in empty declaration with %<enum%> underlying type",
5333 : 1 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5334 : 1 : warned = 1;
5335 : : }
5336 : 37 : else if (declspecs->const_p
5337 : 36 : || declspecs->volatile_p
5338 : 36 : || declspecs->atomic_p
5339 : 36 : || declspecs->restrict_p
5340 : 36 : || declspecs->address_space)
5341 : : {
5342 : 1 : error ("type qualifier in empty declaration with %<enum%> "
5343 : : "underlying type");
5344 : 1 : warned = 1;
5345 : : }
5346 : 36 : else if (declspecs->alignas_p)
5347 : : {
5348 : 1 : error ("%<alignas%> in empty declaration with %<enum%> "
5349 : : "underlying type");
5350 : 1 : warned = 1;
5351 : : }
5352 : : }
5353 : :
5354 : 497163 : if (!warned && !in_system_header_at (input_location)
5355 : 615114 : && declspecs->storage_class != csc_none)
5356 : : {
5357 : 12 : warning (0, "useless storage class specifier in empty declaration");
5358 : 12 : warned = 2;
5359 : : }
5360 : :
5361 : 497331 : if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
5362 : : {
5363 : 3 : warning (0, "useless %qs in empty declaration",
5364 : 3 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5365 : 3 : warned = 2;
5366 : : }
5367 : :
5368 : 3 : if (!warned
5369 : 497144 : && !in_system_header_at (input_location)
5370 : 615104 : && (declspecs->const_p
5371 : 117768 : || declspecs->volatile_p
5372 : 117768 : || declspecs->atomic_p
5373 : 117768 : || declspecs->restrict_p
5374 : 117768 : || declspecs->address_space))
5375 : : {
5376 : 8 : warning (0, "useless type qualifier in empty declaration");
5377 : 8 : warned = 2;
5378 : : }
5379 : :
5380 : 497144 : if (!warned && !in_system_header_at (input_location)
5381 : 615091 : && declspecs->alignas_p)
5382 : : {
5383 : 4 : warning (0, "useless %<_Alignas%> in empty declaration");
5384 : 4 : warned = 2;
5385 : : }
5386 : :
5387 : 497331 : if (found_tag
5388 : 497331 : && warned == 2
5389 : 23 : && (declspecs->typespec_kind == ctsk_tagref_attrs
5390 : 23 : || declspecs->typespec_kind == ctsk_tagfirstref_attrs))
5391 : : {
5392 : : /* Standard attributes after the "struct" or "union" keyword are
5393 : : only permitted when the contents of the type are defined, or
5394 : : in the form "struct-or-union attribute-specifier-sequence
5395 : : identifier;". If the ';' was not present, attributes were
5396 : : diagnosed in the parser. Here, ensure that any other useless
5397 : : elements of the declaration result in a pedwarn, not just a
5398 : : warning. Forward declarations of enum types are not part of
5399 : : standard C, but handle them the same. */
5400 : 2 : pedwarn (input_location, 0,
5401 : : "invalid use of attributes in empty declaration");
5402 : 2 : warned = 1;
5403 : : }
5404 : :
5405 : 497331 : if (warned != 1)
5406 : : {
5407 : 497157 : if (declspecs->declspecs_seen_p
5408 : 497157 : && !declspecs->non_std_attrs_seen_p)
5409 : : /* An attribute declaration (but not a fallthrough attribute
5410 : : declaration, which was handled separately); warn if there
5411 : : are any attributes being ignored (but not if the attributes
5412 : : were empty). */
5413 : 48 : c_warn_unused_attributes (declspecs->attrs);
5414 : 497109 : else if (!found_tag)
5415 : 10 : pedwarn (input_location, 0, "empty declaration");
5416 : : }
5417 : 497331 : }
5418 : :
5419 : :
5420 : : /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
5421 : : bits. SPECS represents declaration specifiers that the grammar
5422 : : only permits to contain type qualifiers and attributes. */
5423 : :
5424 : : int
5425 : 18119083 : quals_from_declspecs (const struct c_declspecs *specs)
5426 : : {
5427 : 18119083 : int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
5428 : : | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
5429 : : | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
5430 : 18119083 : | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
5431 : 18119083 : | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
5432 : 18119083 : gcc_assert (!specs->type
5433 : : && !specs->decl_attr
5434 : : && specs->typespec_word == cts_none
5435 : : && specs->storage_class == csc_none
5436 : : && !specs->typedef_p
5437 : : && !specs->explicit_signed_p
5438 : : && !specs->deprecated_p
5439 : : && !specs->unavailable_p
5440 : : && !specs->long_p
5441 : : && !specs->long_long_p
5442 : : && !specs->short_p
5443 : : && !specs->signed_p
5444 : : && !specs->unsigned_p
5445 : : && !specs->complex_p
5446 : : && !specs->inline_p
5447 : : && !specs->noreturn_p
5448 : : && !specs->thread_p);
5449 : 18119083 : return quals;
5450 : : }
5451 : :
5452 : : /* Construct an array declarator. LOC is the location of the
5453 : : beginning of the array (usually the opening brace). EXPR is the
5454 : : expression inside [], or NULL_TREE. QUALS are the type qualifiers
5455 : : inside the [] (to be applied to the pointer to which a parameter
5456 : : array is converted). STATIC_P is true if "static" is inside the
5457 : : [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
5458 : : VLA of unspecified length which is nevertheless a complete type,
5459 : : false otherwise. The field for the contained declarator is left to
5460 : : be filled in by set_array_declarator_inner. */
5461 : :
5462 : : struct c_declarator *
5463 : 1144559 : build_array_declarator (location_t loc,
5464 : : tree expr, struct c_declspecs *quals, bool static_p,
5465 : : bool vla_unspec_p)
5466 : : {
5467 : 1144559 : struct c_declarator *declarator = XOBNEW (&parser_obstack,
5468 : : struct c_declarator);
5469 : 1144559 : declarator->id_loc = loc;
5470 : 1144559 : declarator->kind = cdk_array;
5471 : 1144559 : declarator->declarator = 0;
5472 : 1144559 : declarator->u.array.dimen = expr;
5473 : 1144559 : if (quals)
5474 : : {
5475 : 933 : declarator->u.array.attrs = quals->attrs;
5476 : 933 : declarator->u.array.quals = quals_from_declspecs (quals);
5477 : : }
5478 : : else
5479 : : {
5480 : 1143626 : declarator->u.array.attrs = NULL_TREE;
5481 : 1143626 : declarator->u.array.quals = 0;
5482 : : }
5483 : 1144559 : declarator->u.array.static_p = static_p;
5484 : 1144559 : declarator->u.array.vla_unspec_p = vla_unspec_p;
5485 : 1144559 : if (static_p || quals != NULL)
5486 : 1036 : pedwarn_c90 (loc, OPT_Wpedantic,
5487 : : "ISO C90 does not support %<static%> or type "
5488 : : "qualifiers in parameter array declarators");
5489 : 1144559 : if (vla_unspec_p)
5490 : : {
5491 : 151 : pedwarn_c90 (loc, OPT_Wpedantic,
5492 : : "ISO C90 does not support %<[*]%> array declarators");
5493 : 151 : if (current_scope->parm_flag)
5494 : 143 : current_scope->had_vla_unspec = true;
5495 : : }
5496 : 1144559 : return declarator;
5497 : : }
5498 : :
5499 : : /* Set the contained declarator of an array declarator. DECL is the
5500 : : declarator, as constructed by build_array_declarator; INNER is what
5501 : : appears on the left of the []. */
5502 : :
5503 : : struct c_declarator *
5504 : 1144559 : set_array_declarator_inner (struct c_declarator *decl,
5505 : : struct c_declarator *inner)
5506 : : {
5507 : 1144559 : decl->declarator = inner;
5508 : 1144559 : return decl;
5509 : : }
5510 : :
5511 : : /* Determine whether TYPE is a one-element array type "[1]". */
5512 : : static bool
5513 : 529015 : one_element_array_type_p (const_tree type)
5514 : : {
5515 : 529015 : if (TREE_CODE (type) != ARRAY_TYPE)
5516 : : return false;
5517 : 529015 : return integer_zerop (array_type_nelts_minus_one (type));
5518 : : }
5519 : :
5520 : : /* Determine whether TYPE is a zero-length array type "[0]". */
5521 : : static bool
5522 : 529015 : zero_length_array_type_p (const_tree type)
5523 : : {
5524 : 529015 : if (TREE_CODE (type) == ARRAY_TYPE)
5525 : 529015 : if (tree type_size = TYPE_SIZE_UNIT (type))
5526 : 444291 : if ((integer_zerop (type_size))
5527 : 1820 : && TYPE_DOMAIN (type) != NULL_TREE
5528 : 446111 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5529 : : return true;
5530 : : return false;
5531 : : }
5532 : :
5533 : : /* INIT is a constructor that forms DECL's initializer. If the final
5534 : : element initializes a flexible array field, adjust the size of the
5535 : : DECL with the initializer based on whether the DECL is a union or
5536 : : a structure. */
5537 : :
5538 : : static void
5539 : 104639 : add_flexible_array_elts_to_size (tree decl, tree init)
5540 : : {
5541 : 104639 : tree elt, type;
5542 : :
5543 : 104639 : if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
5544 : 1429 : return;
5545 : :
5546 : 103210 : elt = CONSTRUCTOR_ELTS (init)->last ().value;
5547 : 103210 : type = TREE_TYPE (elt);
5548 : 103210 : if (c_flexible_array_member_type_p (type))
5549 : : {
5550 : 220 : complete_array_type (&type, elt, false);
5551 : : /* For a structure, add the size of the initializer to the DECL's
5552 : : size. */
5553 : 220 : if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
5554 : : {
5555 : 210 : DECL_SIZE (decl)
5556 : 210 : = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5557 : 210 : DECL_SIZE_UNIT (decl)
5558 : 420 : = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
5559 : : TYPE_SIZE_UNIT (type));
5560 : : }
5561 : : /* For a union, the DECL's size is the maximum of the current size
5562 : : and the size of the initializer. */
5563 : : else
5564 : : {
5565 : 10 : DECL_SIZE (decl)
5566 : 10 : = size_binop (MAX_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5567 : 10 : DECL_SIZE_UNIT (decl)
5568 : 20 : = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
5569 : : TYPE_SIZE_UNIT (type));
5570 : : }
5571 : : }
5572 : : }
5573 : :
5574 : : /* Decode a "typename", such as "int **", returning a ..._TYPE node.
5575 : : Set *EXPR, if EXPR not NULL, to any expression to be evaluated
5576 : : before the type name, and set *EXPR_CONST_OPERANDS, if
5577 : : EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
5578 : : appear in a constant expression. */
5579 : :
5580 : : tree
5581 : 120860293 : groktypename (struct c_type_name *type_name, tree *expr,
5582 : : bool *expr_const_operands)
5583 : : {
5584 : 120860293 : tree type;
5585 : 120860293 : tree attrs = type_name->specs->attrs;
5586 : :
5587 : 120860293 : type_name->specs->attrs = NULL_TREE;
5588 : :
5589 : 120860293 : type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
5590 : : false, NULL, &attrs, expr, expr_const_operands,
5591 : : DEPRECATED_NORMAL);
5592 : :
5593 : : /* Apply attributes. */
5594 : 120860293 : attrs = c_warn_type_attributes (type, attrs);
5595 : 120860293 : decl_attributes (&type, attrs, 0);
5596 : :
5597 : 120860293 : return type;
5598 : : }
5599 : :
5600 : :
5601 : : /* Decode a "typename", such as "int **", returning a ..._TYPE node,
5602 : : as for groktypename but setting the context to GENERIC_ASSOC. */
5603 : :
5604 : : tree
5605 : 763 : grokgenassoc (struct c_type_name *type_name)
5606 : : {
5607 : 763 : tree type;
5608 : 763 : tree attrs = type_name->specs->attrs;
5609 : :
5610 : 763 : type_name->specs->attrs = NULL_TREE;
5611 : :
5612 : 763 : type = grokdeclarator (type_name->declarator, type_name->specs, GENERIC_ASSOC,
5613 : : false, NULL, &attrs, NULL, NULL, DEPRECATED_NORMAL);
5614 : :
5615 : : /* Apply attributes. */
5616 : 763 : attrs = c_warn_type_attributes (type, attrs);
5617 : 763 : decl_attributes (&type, attrs, 0);
5618 : :
5619 : 763 : return type;
5620 : : }
5621 : :
5622 : :
5623 : : /* Looks up the most recent pushed declaration corresponding to DECL. */
5624 : :
5625 : : static tree
5626 : 91591962 : lookup_last_decl (tree decl)
5627 : : {
5628 : 91591962 : tree last_decl = lookup_name (DECL_NAME (decl));
5629 : 91591962 : if (!last_decl)
5630 : 89058347 : last_decl = lookup_name_in_scope (DECL_NAME (decl), external_scope);
5631 : 91591962 : return last_decl;
5632 : : }
5633 : :
5634 : : /* Wrapper for decl_attributes that adds some implicit attributes
5635 : : to VAR_DECLs or FUNCTION_DECLs. */
5636 : :
5637 : : static tree
5638 : 63862296 : c_decl_attributes (tree *node, tree attributes, int flags)
5639 : : {
5640 : : /* Add implicit "omp declare target" attribute if requested. */
5641 : 63862296 : if (vec_safe_length (current_omp_declare_target_attribute)
5642 : 9177 : && ((VAR_P (*node) && is_global_var (*node))
5643 : 2729 : || TREE_CODE (*node) == FUNCTION_DECL))
5644 : : {
5645 : 1131 : if (VAR_P (*node) && !omp_mappable_type (TREE_TYPE (*node)))
5646 : 13 : attributes = tree_cons (get_identifier ("omp declare target implicit"),
5647 : : NULL_TREE, attributes);
5648 : : else
5649 : 1118 : attributes = tree_cons (get_identifier ("omp declare target"),
5650 : : NULL_TREE, attributes);
5651 : 1131 : if (TREE_CODE (*node) == FUNCTION_DECL)
5652 : : {
5653 : 1001 : int device_type
5654 : 1001 : = current_omp_declare_target_attribute->last ().device_type;
5655 : 1001 : device_type = MAX (device_type, 0);
5656 : 1001 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
5657 : 1001 : && !lookup_attribute ("omp declare target host", attributes))
5658 : 2 : attributes
5659 : 2 : = tree_cons (get_identifier ("omp declare target host"),
5660 : : NULL_TREE, attributes);
5661 : 1001 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
5662 : 1001 : && !lookup_attribute ("omp declare target nohost", attributes))
5663 : 2 : attributes
5664 : 2 : = tree_cons (get_identifier ("omp declare target nohost"),
5665 : : NULL_TREE, attributes);
5666 : :
5667 : 1001 : int indirect
5668 : 1001 : = current_omp_declare_target_attribute->last ().indirect;
5669 : 1001 : if (indirect && !lookup_attribute ("omp declare target indirect",
5670 : : attributes))
5671 : 10 : attributes
5672 : 10 : = tree_cons (get_identifier ("omp declare target indirect"),
5673 : : NULL_TREE, attributes);
5674 : : }
5675 : : }
5676 : :
5677 : 63862296 : if (flag_openmp || flag_openmp_simd)
5678 : : {
5679 : : bool diagnosed = false;
5680 : 1347511 : for (tree *pa = &attributes; *pa; )
5681 : : {
5682 : 871075 : if (is_attribute_namespace_p ("omp", *pa))
5683 : : {
5684 : 60 : tree name = get_attribute_name (*pa);
5685 : 60 : if (is_attribute_p ("directive", name)
5686 : 0 : || is_attribute_p ("sequence", name)
5687 : 60 : || is_attribute_p ("decl", name))
5688 : : {
5689 : 60 : const char *p = NULL;
5690 : 60 : if (TREE_VALUE (*pa) == NULL_TREE)
5691 : 12 : p = IDENTIFIER_POINTER (name);
5692 : 108 : for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
5693 : : {
5694 : 48 : tree d = TREE_VALUE (a);
5695 : 48 : gcc_assert (TREE_CODE (d) == C_TOKEN_VEC);
5696 : 86 : if (TREE_PUBLIC (d)
5697 : 38 : && (VAR_P (*node)
5698 : 2 : || TREE_CODE (*node) == FUNCTION_DECL)
5699 : 86 : && c_maybe_parse_omp_decl (*node, d))
5700 : 38 : continue;
5701 : 10 : p = TREE_PUBLIC (d) ? "decl" : "directive";
5702 : : }
5703 : 60 : if (p && !diagnosed)
5704 : : {
5705 : 22 : error ("%<omp::%s%> not allowed to be specified in "
5706 : : "this context", p);
5707 : 22 : diagnosed = true;
5708 : : }
5709 : 60 : if (p)
5710 : : {
5711 : 22 : *pa = TREE_CHAIN (*pa);
5712 : 22 : continue;
5713 : : }
5714 : : }
5715 : : }
5716 : 871053 : pa = &TREE_CHAIN (*pa);
5717 : : }
5718 : : }
5719 : :
5720 : : /* Look up the current declaration with all the attributes merged
5721 : : so far so that attributes on the current declaration that's
5722 : : about to be pushed that conflict with the former can be detected,
5723 : : diagnosed, and rejected as appropriate. To match the C++ FE, do
5724 : : not pass an error_mark_node when we found an undeclared variable. */
5725 : 63862296 : tree last_decl = lookup_last_decl (*node);
5726 : 63862296 : if (last_decl == error_mark_node)
5727 : 20 : last_decl = NULL_TREE;
5728 : 63862296 : tree attr = decl_attributes (node, attributes, flags, last_decl);
5729 : 63862295 : if (VAR_P (*node) && DECL_THREAD_LOCAL_P (*node))
5730 : : {
5731 : : // tls_model attribute can set a stronger TLS access model.
5732 : 2855 : tls_model model = DECL_TLS_MODEL (*node);
5733 : 2855 : tls_model default_model = decl_default_tls_model (*node);
5734 : 2855 : if (default_model > model)
5735 : 1601 : set_decl_tls_model (*node, default_model);
5736 : : }
5737 : 63862295 : return attr;
5738 : : }
5739 : :
5740 : :
5741 : : /* Decode a declarator in an ordinary declaration or data definition.
5742 : : This is called as soon as the type information and variable name
5743 : : have been parsed, before parsing the initializer if any.
5744 : : Here we create the ..._DECL node, fill in its type,
5745 : : and (if DO_PUSH) put it on the list of decls for the current context.
5746 : : When nonnull, set *LASTLOC to the location of the prior declaration
5747 : : of the same entity if one exists.
5748 : : The ..._DECL node is returned as the value.
5749 : :
5750 : : Exception: for arrays where the length is not specified,
5751 : : the type is left null, to be filled in by `finish_decl'.
5752 : :
5753 : : Function definitions do not come here; they go to start_function
5754 : : instead. However, external and forward declarations of functions
5755 : : do go through here. Structure field declarations are done by
5756 : : grokfield and not through here. */
5757 : :
5758 : : tree
5759 : 27729690 : start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5760 : : bool initialized, tree attributes, bool do_push /* = true */,
5761 : : location_t *lastloc /* = NULL */)
5762 : : {
5763 : 27729690 : tree decl;
5764 : 27729690 : tree old_decl;
5765 : 27729690 : tree tem;
5766 : 27729690 : tree expr = NULL_TREE;
5767 : 27729690 : enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5768 : :
5769 : : /* An object declared as __attribute__((unavailable)) suppresses
5770 : : warnings and errors from __attribute__((deprecated/unavailable))
5771 : : components.
5772 : : An object declared as __attribute__((deprecated)) suppresses
5773 : : warnings of uses of other deprecated items. */
5774 : 27729690 : if (lookup_attribute ("unavailable", attributes))
5775 : : deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
5776 : 27729666 : else if (lookup_attribute ("deprecated", attributes))
5777 : 30108 : deprecated_state = DEPRECATED_SUPPRESS;
5778 : :
5779 : 27729690 : decl = grokdeclarator (declarator, declspecs,
5780 : : NORMAL, initialized, NULL, &attributes, &expr, NULL,
5781 : : deprecated_state);
5782 : 27729690 : if (!decl || decl == error_mark_node)
5783 : : return NULL_TREE;
5784 : :
5785 : 27729666 : old_decl = lookup_last_decl (decl);
5786 : :
5787 : 27729666 : if (tree lastdecl = lastloc ? old_decl : NULL_TREE)
5788 : 4702804 : if (lastdecl != error_mark_node)
5789 : 4702795 : *lastloc = DECL_SOURCE_LOCATION (lastdecl);
5790 : :
5791 : : /* Make sure the size expression is evaluated at this point. */
5792 : 27729666 : if (expr && !current_scope->parm_flag)
5793 : 12927 : add_stmt (fold_convert (void_type_node, expr));
5794 : :
5795 : 13196243 : if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5796 : 27729668 : && TREE_PUBLIC (decl))
5797 : 1 : warning (OPT_Wmain, "%q+D is usually a function", decl);
5798 : :
5799 : 14 : if (warn_missing_variable_declarations && VAR_P (decl)
5800 : 27729679 : && !DECL_EXTERNAL (decl) && TREE_PUBLIC (decl) && old_decl == NULL_TREE)
5801 : 5 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_variable_declarations,
5802 : : "no previous declaration for %qD", decl);
5803 : :
5804 : 27729666 : if (initialized)
5805 : : /* Is it valid for this decl to have an initializer at all?
5806 : : If not, set INITIALIZED to zero, which will indirectly
5807 : : tell 'finish_decl' to ignore the initializer once it is parsed. */
5808 : 6325926 : switch (TREE_CODE (decl))
5809 : : {
5810 : 6 : case TYPE_DECL:
5811 : 6 : error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5812 : 6 : initialized = false;
5813 : 6 : break;
5814 : :
5815 : 9 : case FUNCTION_DECL:
5816 : 9 : error ("function %qD is initialized like a variable", decl);
5817 : 9 : initialized = false;
5818 : 9 : break;
5819 : :
5820 : 49 : case PARM_DECL:
5821 : : /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5822 : 49 : error ("parameter %qD is initialized", decl);
5823 : 49 : initialized = false;
5824 : 49 : break;
5825 : :
5826 : 6325862 : default:
5827 : : /* Don't allow initializations for incomplete types except for
5828 : : arrays which might be completed by the initialization. */
5829 : :
5830 : : /* This can happen if the array size is an undefined macro.
5831 : : We already gave a warning, so we don't need another one. */
5832 : 6325862 : if (TREE_TYPE (decl) == error_mark_node)
5833 : : initialized = false;
5834 : 6325840 : else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5835 : : {
5836 : : /* A complete type is ok if size is fixed. If the size is
5837 : : variable, an empty initializer is OK and nonempty
5838 : : initializers will be diagnosed in the parser. */
5839 : : }
5840 : 12191 : else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5841 : : {
5842 : 3 : error ("variable %qD has initializer but incomplete type", decl);
5843 : 3 : initialized = false;
5844 : : }
5845 : : }
5846 : :
5847 : 67 : if (initialized)
5848 : : {
5849 : 6325837 : if (current_scope == file_scope)
5850 : 155847 : TREE_STATIC (decl) = 1;
5851 : :
5852 : : /* Tell 'pushdecl' this is an initialized decl
5853 : : even though we don't yet have the initializer expression.
5854 : : Also tell 'finish_decl' it may store the real initializer. */
5855 : 6325837 : DECL_INITIAL (decl) = error_mark_node;
5856 : : }
5857 : :
5858 : : /* If this is a function declaration, write a record describing it to the
5859 : : prototypes file (if requested). */
5860 : :
5861 : 27729666 : if (TREE_CODE (decl) == FUNCTION_DECL)
5862 : 14533423 : gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5863 : :
5864 : : /* ANSI specifies that a tentative definition which is not merged with
5865 : : a non-tentative definition behaves exactly like a definition with an
5866 : : initializer equal to zero. (Section 3.7.2)
5867 : :
5868 : : -fno-common gives strict ANSI behavior, though this tends to break
5869 : : a large body of code that grew up without this rule.
5870 : :
5871 : : Thread-local variables are never common, since there's no entrenched
5872 : : body of code to break, and it allows more efficient variable references
5873 : : in the presence of dynamic linking. */
5874 : :
5875 : 27729666 : if (VAR_P (decl)
5876 : 8893858 : && !initialized
5877 : 2568021 : && TREE_PUBLIC (decl)
5878 : 989055 : && !DECL_THREAD_LOCAL_P (decl)
5879 : 28716369 : && !flag_no_common)
5880 : 128 : DECL_COMMON (decl) = 1;
5881 : :
5882 : : /* Set attributes here so if duplicate decl, will have proper attributes. */
5883 : 27729666 : c_decl_attributes (&decl, attributes, 0);
5884 : :
5885 : : /* Handle gnu_inline attribute. */
5886 : 27729665 : if (declspecs->inline_p
5887 : 1029 : && !flag_gnu89_inline
5888 : 997 : && TREE_CODE (decl) == FUNCTION_DECL
5889 : 27730654 : && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
5890 : 966 : || current_function_decl))
5891 : : {
5892 : 46 : if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5893 : : ;
5894 : 37 : else if (declspecs->storage_class != csc_static)
5895 : 37 : DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5896 : : }
5897 : :
5898 : 27729665 : if (TREE_CODE (decl) == FUNCTION_DECL
5899 : 14533422 : && DECL_DECLARED_INLINE_P (decl)
5900 : 1019 : && DECL_UNINLINABLE (decl)
5901 : 27729667 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5902 : : {
5903 : 2 : auto_urlify_attributes sentinel;
5904 : 2 : warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5905 : : decl, "noinline");
5906 : 2 : }
5907 : :
5908 : : /* C99 6.7.4p3: An inline definition of a function with external
5909 : : linkage shall not contain a definition of a modifiable object
5910 : : with static storage duration... */
5911 : 27729665 : if (VAR_P (decl)
5912 : 8893858 : && current_scope != file_scope
5913 : 7741959 : && TREE_STATIC (decl)
5914 : 78755 : && !TREE_READONLY (decl)
5915 : 76183 : && DECL_DECLARED_INLINE_P (current_function_decl)
5916 : 27729735 : && DECL_EXTERNAL (current_function_decl))
5917 : 7 : record_inline_static (input_location, current_function_decl,
5918 : : decl, csi_modifiable);
5919 : :
5920 : 27729665 : if (c_dialect_objc ()
5921 : 0 : && VAR_OR_FUNCTION_DECL_P (decl))
5922 : 0 : objc_check_global_decl (decl);
5923 : :
5924 : : /* To enable versions to be created across TU's we mark and mangle all
5925 : : non-default versioned functions. */
5926 : 27729665 : if (TREE_CODE (decl) == FUNCTION_DECL
5927 : : && !TARGET_HAS_FMV_TARGET_ATTRIBUTE
5928 : : && get_target_version (decl).is_valid ())
5929 : : {
5930 : : maybe_mark_function_versioned (decl);
5931 : : if (current_scope != file_scope)
5932 : : error ("versioned declarations are only allowed at file scope");
5933 : : }
5934 : :
5935 : : /* Add this decl to the current scope.
5936 : : TEM may equal DECL or it may be a previous decl of the same name. */
5937 : 27729665 : if (do_push)
5938 : : {
5939 : 27729321 : tem = pushdecl (decl);
5940 : :
5941 : 27729321 : if (initialized && DECL_EXTERNAL (tem))
5942 : : {
5943 : 27 : DECL_EXTERNAL (tem) = 0;
5944 : 27 : TREE_STATIC (tem) = 1;
5945 : : }
5946 : :
5947 : 27729321 : return tem;
5948 : : }
5949 : : else
5950 : 344 : return decl;
5951 : : }
5952 : :
5953 : : /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5954 : : DECL or the non-array element type if DECL is an uninitialized array.
5955 : : If that type has a const member, diagnose this. */
5956 : :
5957 : : static void
5958 : 7 : diagnose_uninitialized_cst_member (tree decl, tree type)
5959 : : {
5960 : 7 : tree field;
5961 : 17 : for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5962 : : {
5963 : 10 : tree field_type;
5964 : 10 : if (TREE_CODE (field) != FIELD_DECL)
5965 : 0 : continue;
5966 : 10 : field_type = strip_array_types (TREE_TYPE (field));
5967 : :
5968 : 10 : if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5969 : : {
5970 : 5 : auto_diagnostic_group d;
5971 : 5 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5972 : : "uninitialized const member in %qT is invalid in C++",
5973 : 5 : strip_array_types (TREE_TYPE (decl))))
5974 : 5 : inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5975 : 5 : }
5976 : :
5977 : 10 : if (RECORD_OR_UNION_TYPE_P (field_type))
5978 : 2 : diagnose_uninitialized_cst_member (decl, field_type);
5979 : : }
5980 : 7 : }
5981 : :
5982 : : /* Finish processing of a declaration;
5983 : : install its initial value.
5984 : : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5985 : : If the length of an array type is not known before,
5986 : : it must be determined now, from the initial value, or it is an error.
5987 : :
5988 : : INIT_LOC is the location of the initial value. */
5989 : :
5990 : : void
5991 : 155359565 : finish_decl (tree decl, location_t init_loc, tree init,
5992 : : tree origtype, tree asmspec_tree)
5993 : : {
5994 : 155359565 : tree type;
5995 : 155359565 : bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5996 : 155359565 : const char *asmspec = 0;
5997 : :
5998 : : /* If a name was specified, get the string. */
5999 : 146462767 : if (VAR_OR_FUNCTION_DECL_P (decl)
6000 : 169892987 : && DECL_FILE_SCOPE_P (decl))
6001 : 15686836 : asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
6002 : 155359565 : if (asmspec_tree)
6003 : 861036 : asmspec = TREE_STRING_POINTER (asmspec_tree);
6004 : :
6005 : 155359565 : if (VAR_P (decl)
6006 : 8896798 : && TREE_STATIC (decl)
6007 : 156420795 : && global_bindings_p ())
6008 : : /* So decl is a global variable. Record the types it uses
6009 : : so that we can decide later to emit debug info for them. */
6010 : 979318 : record_types_used_by_current_var_decl (decl);
6011 : :
6012 : : /* If `start_decl' didn't like having an initialization, ignore it now. */
6013 : 155359565 : if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
6014 : : init = NULL_TREE;
6015 : :
6016 : : /* Don't crash if parm is initialized. */
6017 : 155359565 : if (TREE_CODE (decl) == PARM_DECL)
6018 : : init = NULL_TREE;
6019 : :
6020 : 31991450 : if (init)
6021 : 6328778 : store_init_value (init_loc, decl, init, origtype);
6022 : :
6023 : 155359565 : if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
6024 : 0 : || TREE_CODE (decl) == FIELD_DECL))
6025 : 0 : objc_check_decl (decl);
6026 : :
6027 : 155359565 : type = TREE_TYPE (decl);
6028 : :
6029 : : /* Deduce size of array from initialization, if not already known.
6030 : : This is only needed for an initialization in the current scope;
6031 : : it must not be done for a file-scope initialization of a
6032 : : declaration with external linkage, redeclared in an inner scope
6033 : : with the outer declaration shadowed in an intermediate scope. */
6034 : 155359565 : if (TREE_CODE (type) == ARRAY_TYPE
6035 : 861367 : && TYPE_DOMAIN (type) == NULL_TREE
6036 : 19799 : && TREE_CODE (decl) != TYPE_DECL
6037 : 155379268 : && !(TREE_PUBLIC (decl) && current_scope != file_scope))
6038 : : {
6039 : 19517 : bool do_default
6040 : 19517 : = !TREE_STATIC (decl) && !DECL_EXTERNAL (decl);
6041 : 19517 : int failure
6042 : 19517 : = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
6043 : : do_default);
6044 : :
6045 : : /* Get the completed type made by complete_array_type. */
6046 : 19517 : type = TREE_TYPE (decl);
6047 : :
6048 : 19517 : switch (failure)
6049 : : {
6050 : 0 : case 1:
6051 : 0 : error ("initializer fails to determine size of %q+D", decl);
6052 : 0 : break;
6053 : :
6054 : 7354 : case 2:
6055 : 7354 : if (do_default)
6056 : 1 : error ("array size missing in %q+D", decl);
6057 : 7353 : else if (!TREE_PUBLIC (decl))
6058 : 37 : pedwarn_c23 (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
6059 : : "array size missing in %q+D", decl);
6060 : : break;
6061 : :
6062 : 3 : case 3:
6063 : 3 : error ("zero or negative size array %q+D", decl);
6064 : 3 : break;
6065 : :
6066 : 12160 : case 0:
6067 : : /* For global variables, update the copy of the type that
6068 : : exists in the binding. */
6069 : 12160 : if (TREE_PUBLIC (decl))
6070 : : {
6071 : 3820 : struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
6072 : 7640 : while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
6073 : 3820 : b_ext = b_ext->shadowed;
6074 : 3820 : if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
6075 : : {
6076 : 3820 : if (b_ext->u.type && comptypes (b_ext->u.type, type))
6077 : 176 : b_ext->u.type = composite_type (b_ext->u.type, type);
6078 : : else
6079 : 3644 : b_ext->u.type = type;
6080 : : }
6081 : : }
6082 : : break;
6083 : :
6084 : 0 : default:
6085 : 0 : gcc_unreachable ();
6086 : : }
6087 : :
6088 : 19517 : if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
6089 : 11291 : TREE_TYPE (DECL_INITIAL (decl)) = type;
6090 : :
6091 : 19517 : relayout_decl (decl);
6092 : : }
6093 : :
6094 : : /* Look for braced array initializers for character arrays and
6095 : : recursively convert them into STRING_CSTs. */
6096 : 155359565 : if (tree init = DECL_INITIAL (decl))
6097 : 129699305 : DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
6098 : :
6099 : 155359565 : if (VAR_P (decl))
6100 : : {
6101 : 8896798 : if (init && TREE_CODE (init) == CONSTRUCTOR)
6102 : 104639 : add_flexible_array_elts_to_size (decl, init);
6103 : :
6104 : 8896798 : complete_flexible_array_elts (DECL_INITIAL (decl));
6105 : :
6106 : 8896798 : if (is_global_var (decl))
6107 : : {
6108 : 1235117 : type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
6109 : 1235117 : ? TCTX_THREAD_STORAGE
6110 : : : TCTX_STATIC_STORAGE);
6111 : 1235117 : if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
6112 : 0 : TREE_TYPE (decl) = error_mark_node;
6113 : : }
6114 : :
6115 : 8904893 : if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
6116 : 8904790 : && COMPLETE_TYPE_P (TREE_TYPE (decl)))
6117 : 107 : layout_decl (decl, 0);
6118 : :
6119 : 8896798 : if (DECL_SIZE (decl) == NULL_TREE
6120 : : /* Don't give an error if we already gave one earlier. */
6121 : 7988 : && TREE_TYPE (decl) != error_mark_node
6122 : 8904683 : && (TREE_STATIC (decl)
6123 : : /* A static variable with an incomplete type
6124 : : is an error if it is initialized.
6125 : : Also if it is not file scope.
6126 : : Also if it is thread-local (in C23).
6127 : : Otherwise, let it through, but if it is not `extern'
6128 : : then it may cause an error message later. */
6129 : 308 : ? (DECL_INITIAL (decl) != NULL_TREE
6130 : 308 : || !DECL_FILE_SCOPE_P (decl)
6131 : 305 : || (flag_isoc23 && DECL_THREAD_LOCAL_P (decl)))
6132 : : /* An automatic variable with an incomplete type
6133 : : is an error. */
6134 : 7577 : : !DECL_EXTERNAL (decl)))
6135 : : {
6136 : 19 : error ("storage size of %q+D isn%'t known", decl);
6137 : 19 : TREE_TYPE (decl) = error_mark_node;
6138 : : }
6139 : :
6140 : 17604439 : if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
6141 : 8623672 : || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6142 : 286634 : && DECL_SIZE (decl) == NULL_TREE
6143 : 8897086 : && TREE_STATIC (decl))
6144 : 135 : incomplete_record_decls.safe_push (decl);
6145 : :
6146 : 8896798 : if (is_global_var (decl)
6147 : 1235117 : && DECL_SIZE (decl) != NULL_TREE
6148 : 10123970 : && TREE_TYPE (decl) != error_mark_node)
6149 : : {
6150 : 1227154 : if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
6151 : 1227151 : constant_expression_warning (DECL_SIZE (decl));
6152 : : else
6153 : : {
6154 : 3 : error ("storage size of %q+D isn%'t constant", decl);
6155 : 3 : TREE_TYPE (decl) = error_mark_node;
6156 : : }
6157 : : }
6158 : :
6159 : 8896798 : if (TREE_USED (type))
6160 : : {
6161 : 4 : TREE_USED (decl) = 1;
6162 : 4 : DECL_READ_P (decl) = 1;
6163 : : }
6164 : : }
6165 : :
6166 : : /* If this is a function and an assembler name is specified, reset DECL_RTL
6167 : : so we can give it its new name. Also, update builtin_decl if it
6168 : : was a normal built-in. */
6169 : 155359565 : if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
6170 : : {
6171 : 848010 : if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6172 : 71704 : set_builtin_user_assembler_name (decl, asmspec);
6173 : 848010 : set_user_assembler_name (decl, asmspec);
6174 : : }
6175 : :
6176 : : /* If #pragma weak was used, mark the decl weak now. */
6177 : 155359565 : maybe_apply_pragma_weak (decl);
6178 : :
6179 : : /* Output the assembler code and/or RTL code for variables and functions,
6180 : : unless the type is an undefined structure or union.
6181 : : If not, it will get done when the type is completed. */
6182 : :
6183 : 155359565 : if (VAR_OR_FUNCTION_DECL_P (decl))
6184 : : {
6185 : : /* Determine the ELF visibility. */
6186 : 23430220 : if (TREE_PUBLIC (decl))
6187 : 15662053 : c_determine_visibility (decl);
6188 : :
6189 : : /* This is a no-op in c-lang.cc or something real in objc-act.cc. */
6190 : 23430220 : if (c_dialect_objc ())
6191 : 0 : objc_check_decl (decl);
6192 : :
6193 : 23430220 : if (asmspec)
6194 : : {
6195 : : /* If this is not a static variable, issue a warning.
6196 : : It doesn't make any sense to give an ASMSPEC for an
6197 : : ordinary, non-register local variable. Historically,
6198 : : GCC has accepted -- but ignored -- the ASMSPEC in
6199 : : this case. */
6200 : 862073 : if (!DECL_FILE_SCOPE_P (decl)
6201 : 1037 : && VAR_P (decl)
6202 : 1037 : && !C_DECL_REGISTER (decl)
6203 : 861044 : && !TREE_STATIC (decl))
6204 : 1 : warning (0, "ignoring %<asm%> specifier for non-static local "
6205 : : "variable %q+D", decl);
6206 : : else
6207 : 861035 : set_user_assembler_name (decl, asmspec);
6208 : : }
6209 : :
6210 : 23430220 : if (DECL_FILE_SCOPE_P (decl))
6211 : : {
6212 : 15686836 : if (DECL_INITIAL (decl) == NULL_TREE
6213 : 15686836 : || DECL_INITIAL (decl) == error_mark_node)
6214 : : /* Don't output anything
6215 : : when a tentative file-scope definition is seen.
6216 : : But at end of compilation, do output code for them. */
6217 : 15530969 : DECL_DEFER_OUTPUT (decl) = 1;
6218 : 15686836 : if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
6219 : 68 : DECL_HARD_REGISTER (decl) = 1;
6220 : 15686836 : rest_of_decl_compilation (decl, true, 0);
6221 : :
6222 : 15686836 : if (TREE_CODE (decl) == FUNCTION_DECL)
6223 : : {
6224 : 14533355 : tree parms = DECL_ARGUMENTS (decl);
6225 : 14533355 : const bool builtin = fndecl_built_in_p (decl);
6226 : 14533355 : if (tree access = build_attr_access_from_parms (parms, !builtin))
6227 : 342690 : decl_attributes (&decl, access, 0);
6228 : : }
6229 : : }
6230 : : else
6231 : : {
6232 : : /* In conjunction with an ASMSPEC, the `register'
6233 : : keyword indicates that we should place the variable
6234 : : in a particular register. */
6235 : 7743384 : if (asmspec && C_DECL_REGISTER (decl))
6236 : : {
6237 : 1029 : DECL_HARD_REGISTER (decl) = 1;
6238 : : /* This cannot be done for a structure with volatile
6239 : : fields, on which DECL_REGISTER will have been
6240 : : reset. */
6241 : 1029 : if (!DECL_REGISTER (decl))
6242 : 1 : error ("cannot put object with volatile field into register");
6243 : : }
6244 : :
6245 : 7743384 : if (TREE_CODE (decl) != FUNCTION_DECL)
6246 : : {
6247 : : /* If we're building a variable sized type, and we might be
6248 : : reachable other than via the top of the current binding
6249 : : level, then create a new BIND_EXPR so that we deallocate
6250 : : the object at the right time. */
6251 : : /* Note that DECL_SIZE can be null due to errors. */
6252 : 7743317 : if (DECL_SIZE (decl)
6253 : 7743271 : && !TREE_CONSTANT (DECL_SIZE (decl))
6254 : 7750674 : && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
6255 : : {
6256 : 1337 : tree bind;
6257 : 1337 : bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6258 : 1337 : TREE_SIDE_EFFECTS (bind) = 1;
6259 : 1337 : add_stmt (bind);
6260 : 1337 : BIND_EXPR_BODY (bind) = push_stmt_list ();
6261 : : }
6262 : 7743317 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
6263 : : DECL_EXPR, decl));
6264 : : }
6265 : : }
6266 : :
6267 : :
6268 : 23430220 : if (!DECL_FILE_SCOPE_P (decl))
6269 : : {
6270 : : /* Recompute the RTL of a local array now
6271 : : if it used to be an incomplete type. */
6272 : 7743384 : if (was_incomplete && !is_global_var (decl))
6273 : : {
6274 : : /* If we used it already as memory, it must stay in memory. */
6275 : 5238 : TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6276 : : /* If it's still incomplete now, no init will save it. */
6277 : 5238 : if (DECL_SIZE (decl) == NULL_TREE)
6278 : 109 : DECL_INITIAL (decl) = NULL_TREE;
6279 : : }
6280 : : }
6281 : : }
6282 : :
6283 : 155359565 : if (TREE_CODE (decl) == TYPE_DECL)
6284 : : {
6285 : 4407891 : if (!DECL_FILE_SCOPE_P (decl)
6286 : 4407891 : && c_type_variably_modified_p (TREE_TYPE (decl)))
6287 : 5321 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6288 : :
6289 : 8431296 : rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
6290 : : }
6291 : :
6292 : : /* Install a cleanup (aka destructor) if one was given. */
6293 : 155359565 : if (VAR_P (decl) && !TREE_STATIC (decl))
6294 : : {
6295 : 7835565 : tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
6296 : 7835565 : if (attr)
6297 : : {
6298 : 95 : tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
6299 : 95 : tree cleanup_decl = lookup_name (cleanup_id);
6300 : 95 : tree cleanup;
6301 : 95 : vec<tree, va_gc> *v;
6302 : :
6303 : : /* Build "cleanup(&decl)" for the destructor. */
6304 : 95 : cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
6305 : 95 : vec_alloc (v, 1);
6306 : 95 : v->quick_push (cleanup);
6307 : 95 : cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
6308 : 95 : vNULL, cleanup_decl, v, NULL);
6309 : 95 : vec_free (v);
6310 : :
6311 : : /* Don't warn about decl unused; the cleanup uses it. */
6312 : 95 : TREE_USED (decl) = 1;
6313 : 95 : TREE_USED (cleanup_decl) = 1;
6314 : 95 : DECL_READ_P (decl) = 1;
6315 : :
6316 : 95 : push_cleanup (decl, cleanup, false);
6317 : : }
6318 : : }
6319 : :
6320 : 155359565 : if (warn_cxx_compat
6321 : 22389 : && VAR_P (decl)
6322 : 7546 : && !DECL_EXTERNAL (decl)
6323 : 155366668 : && DECL_INITIAL (decl) == NULL_TREE)
6324 : : {
6325 : 2330 : type = strip_array_types (type);
6326 : 2330 : if (TREE_READONLY (decl))
6327 : 6 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6328 : : "uninitialized %<const %D%> is invalid in C++", decl);
6329 : 2324 : else if (RECORD_OR_UNION_TYPE_P (type)
6330 : 2324 : && C_TYPE_FIELDS_READONLY (type))
6331 : 5 : diagnose_uninitialized_cst_member (decl, type);
6332 : : }
6333 : :
6334 : 155359565 : if (flag_openmp
6335 : 438272 : && VAR_P (decl)
6336 : 155383315 : && lookup_attribute ("omp declare target implicit",
6337 : 23750 : DECL_ATTRIBUTES (decl)))
6338 : : {
6339 : 13 : DECL_ATTRIBUTES (decl)
6340 : 13 : = remove_attribute ("omp declare target implicit",
6341 : 13 : DECL_ATTRIBUTES (decl));
6342 : 13 : if (!omp_mappable_type (TREE_TYPE (decl)))
6343 : 9 : error ("%q+D in declare target directive does not have mappable type",
6344 : : decl);
6345 : 4 : else if (!lookup_attribute ("omp declare target",
6346 : 4 : DECL_ATTRIBUTES (decl))
6347 : 8 : && !lookup_attribute ("omp declare target link",
6348 : 4 : DECL_ATTRIBUTES (decl)))
6349 : : {
6350 : 4 : DECL_ATTRIBUTES (decl)
6351 : 4 : = tree_cons (get_identifier ("omp declare target"),
6352 : 4 : NULL_TREE, DECL_ATTRIBUTES (decl));
6353 : 4 : symtab_node *node = symtab_node::get (decl);
6354 : 4 : if (node != NULL)
6355 : : {
6356 : 4 : node->offloadable = 1;
6357 : 4 : if (ENABLE_OFFLOADING)
6358 : : {
6359 : : g->have_offload = true;
6360 : : if (is_a <varpool_node *> (node))
6361 : : vec_safe_push (offload_vars, decl);
6362 : : }
6363 : : }
6364 : : }
6365 : : }
6366 : :
6367 : : /* This is the last point we can lower alignment so give the target the
6368 : : chance to do so. */
6369 : 155359565 : if (VAR_P (decl)
6370 : 8896798 : && !is_global_var (decl)
6371 : 163021246 : && !DECL_HARD_REGISTER (decl))
6372 : 7660652 : targetm.lower_local_decl_alignment (decl);
6373 : :
6374 : 155359565 : invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
6375 : 155359565 : }
6376 : :
6377 : : /* Given a parsed parameter declaration, decode it into a PARM_DECL.
6378 : : EXPR is NULL or a pointer to an expression that needs to be
6379 : : evaluated for the side effects of array size expressions in the
6380 : : parameters. */
6381 : :
6382 : : tree
6383 : 0 : grokparm (const struct c_parm *parm, tree *expr)
6384 : : {
6385 : 0 : tree attrs = parm->attrs;
6386 : 0 : tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
6387 : 0 : NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
6388 : :
6389 : 0 : decl_attributes (&decl, attrs, 0);
6390 : :
6391 : 0 : return decl;
6392 : : }
6393 : :
6394 : :
6395 : :
6396 : : /* Given a parsed parameter declaration, decode it into a PARM_DECL
6397 : : and push that on the current scope. EXPR is a pointer to an
6398 : : expression that needs to be evaluated for the side effects of array
6399 : : size expressions in the parameters. */
6400 : :
6401 : : void
6402 : 123345459 : push_parm_decl (const struct c_parm *parm, tree *expr)
6403 : : {
6404 : 123345459 : tree attrs = parm->attrs;
6405 : 123345459 : tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
6406 : 123345459 : &attrs, expr, NULL, DEPRECATED_NORMAL);
6407 : 123345459 : if (decl && DECL_P (decl))
6408 : 123345459 : DECL_SOURCE_LOCATION (decl) = parm->loc;
6409 : :
6410 : 123345459 : decl_attributes (&decl, attrs, 0);
6411 : :
6412 : 123345459 : decl = pushdecl (decl);
6413 : :
6414 : 123345459 : finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
6415 : 123345459 : }
6416 : :
6417 : : /* Mark all the parameter declarations to date as forward decls.
6418 : : Also diagnose use of this extension. */
6419 : :
6420 : : void
6421 : 42 : mark_forward_parm_decls (void)
6422 : : {
6423 : 42 : struct c_binding *b;
6424 : :
6425 : 42 : if (current_scope->had_forward_parm_decls)
6426 : 12 : warning_at (input_location, OPT_Wmultiple_parameter_fwd_decl_lists,
6427 : : "more than one list of forward declarations of parameters");
6428 : 42 : if (pedantic && !current_scope->had_forward_parm_decls)
6429 : 4 : pedwarn (input_location, OPT_Wpedantic,
6430 : : "ISO C forbids forward parameter declarations");
6431 : :
6432 : 42 : current_scope->had_forward_parm_decls = true;
6433 : :
6434 : 95 : for (b = current_scope->bindings; b; b = b->prev)
6435 : 53 : if (TREE_CODE (b->decl) == PARM_DECL)
6436 : 53 : TREE_ASM_WRITTEN (b->decl) = 1;
6437 : 42 : }
6438 : :
6439 : : /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
6440 : : literal, which may be an incomplete array type completed by the
6441 : : initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
6442 : : literal. NON_CONST is true if the initializers contain something
6443 : : that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
6444 : : it is the (valid) alignment for this compound literal, as specified
6445 : : with _Alignas. SCSPECS are the storage class specifiers (C23) from the
6446 : : compound literal. */
6447 : :
6448 : : tree
6449 : 916048 : build_compound_literal (location_t loc, tree type, tree init, bool non_const,
6450 : : unsigned int alignas_align,
6451 : : struct c_declspecs *scspecs)
6452 : : {
6453 : : /* We do not use start_decl here because we have a type, not a declarator;
6454 : : and do not use finish_decl because the decl should be stored inside
6455 : : the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
6456 : 916048 : tree decl;
6457 : 916048 : tree complit;
6458 : 916048 : tree stmt;
6459 : 916048 : bool threadp = scspecs ? scspecs->thread_p : false;
6460 : 307 : enum c_storage_class storage_class = (scspecs
6461 : : ? scspecs->storage_class
6462 : : : csc_none);
6463 : :
6464 : 916048 : if (type == error_mark_node
6465 : 916041 : || init == error_mark_node)
6466 : : return error_mark_node;
6467 : :
6468 : 915988 : if (current_scope == file_scope && storage_class == csc_register)
6469 : : {
6470 : 1 : error_at (loc, "file-scope compound literal specifies %<register%>");
6471 : 1 : storage_class = csc_none;
6472 : : }
6473 : :
6474 : 915988 : if (current_scope != file_scope && threadp && storage_class == csc_none)
6475 : : {
6476 : 6 : error_at (loc, "compound literal implicitly auto and declared %qs",
6477 : 3 : scspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6478 : 3 : threadp = false;
6479 : : }
6480 : :
6481 : 915988 : decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
6482 : 915988 : DECL_EXTERNAL (decl) = 0;
6483 : 915988 : TREE_PUBLIC (decl) = 0;
6484 : 1831976 : TREE_STATIC (decl) = (current_scope == file_scope
6485 : 915988 : || storage_class == csc_static);
6486 : 915988 : DECL_CONTEXT (decl) = current_function_decl;
6487 : 915988 : TREE_USED (decl) = 1;
6488 : 915988 : DECL_READ_P (decl) = 1;
6489 : 915988 : DECL_ARTIFICIAL (decl) = 1;
6490 : 915988 : DECL_IGNORED_P (decl) = 1;
6491 : 915988 : C_DECL_COMPOUND_LITERAL_P (decl) = 1;
6492 : 1831745 : C_DECL_DECLARED_CONSTEXPR (decl) = scspecs && scspecs->constexpr_p;
6493 : 915988 : TREE_TYPE (decl) = type;
6494 : 915988 : if (threadp)
6495 : 16 : set_decl_tls_model (decl, decl_default_tls_model (decl));
6496 : 915988 : if (storage_class == csc_register)
6497 : : {
6498 : 25 : C_DECL_REGISTER (decl) = 1;
6499 : 25 : DECL_REGISTER (decl) = 1;
6500 : : }
6501 : 915988 : c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
6502 : 915988 : if (alignas_align)
6503 : : {
6504 : 3 : SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6505 : 3 : DECL_USER_ALIGN (decl) = 1;
6506 : : }
6507 : 915988 : store_init_value (loc, decl, init, NULL_TREE);
6508 : 915988 : if (current_scope != file_scope
6509 : 915737 : && TREE_STATIC (decl)
6510 : 28 : && !TREE_READONLY (decl)
6511 : 17 : && DECL_DECLARED_INLINE_P (current_function_decl)
6512 : 915994 : && DECL_EXTERNAL (current_function_decl))
6513 : 4 : record_inline_static (input_location, current_function_decl,
6514 : : decl, csi_modifiable);
6515 : :
6516 : 915988 : if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
6517 : : {
6518 : 258 : int failure = complete_array_type (&TREE_TYPE (decl),
6519 : 258 : DECL_INITIAL (decl), true);
6520 : : /* If complete_array_type returns 3, it means that the initial value of
6521 : : the compound literal is empty. Allow it with a pedwarn; in pre-C23
6522 : : modes, the empty initializer itself has been diagnosed if pedantic so
6523 : : does not need to be diagnosed again here. */
6524 : 258 : gcc_assert (failure == 0 || failure == 3);
6525 : 258 : if (failure == 3 && flag_isoc23)
6526 : 1 : pedwarn (loc, OPT_Wpedantic,
6527 : : "array of unknown size with empty initializer");
6528 : :
6529 : 258 : type = TREE_TYPE (decl);
6530 : 258 : TREE_TYPE (DECL_INITIAL (decl)) = type;
6531 : : }
6532 : :
6533 : 915988 : if (type == error_mark_node || !COMPLETE_TYPE_P (type))
6534 : : {
6535 : 19 : c_incomplete_type_error (loc, NULL_TREE, type);
6536 : 19 : return error_mark_node;
6537 : : }
6538 : :
6539 : 915690 : if ((TREE_STATIC (decl) || C_DECL_DECLARED_CONSTEXPR (decl))
6540 : 916190 : && C_TYPE_VARIABLE_SIZE (type))
6541 : 2 : error_at (loc, "storage size isn%'t constant");
6542 : :
6543 : 915969 : if (TREE_STATIC (decl)
6544 : 915969 : && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
6545 : 0 : return error_mark_node;
6546 : :
6547 : 915969 : stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
6548 : 915969 : complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
6549 : 915969 : TREE_SIDE_EFFECTS (complit) = 1;
6550 : :
6551 : 915969 : layout_decl (decl, 0);
6552 : :
6553 : 915969 : if (TREE_STATIC (decl))
6554 : : {
6555 : : /* This decl needs a name for the assembler output. */
6556 : 279 : set_compound_literal_name (decl);
6557 : 279 : DECL_DEFER_OUTPUT (decl) = 1;
6558 : 279 : DECL_COMDAT (decl) = 1;
6559 : 279 : pushdecl (decl);
6560 : 279 : rest_of_decl_compilation (decl, 1, 0);
6561 : : }
6562 : 915690 : else if (current_function_decl && !current_scope->parm_flag)
6563 : 915681 : pushdecl (decl);
6564 : :
6565 : 915969 : if (non_const)
6566 : : {
6567 : 199 : complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
6568 : 199 : C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
6569 : : }
6570 : :
6571 : : return complit;
6572 : : }
6573 : :
6574 : : /* Check the type of a compound literal. Here we just check that it
6575 : : is valid for C++. */
6576 : :
6577 : : void
6578 : 916045 : check_compound_literal_type (location_t loc, struct c_type_name *type_name)
6579 : : {
6580 : 916045 : if (warn_cxx_compat
6581 : 165 : && (type_name->specs->typespec_kind == ctsk_tagdef
6582 : 164 : || type_name->specs->typespec_kind == ctsk_tagfirstref
6583 : 163 : || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
6584 : 2 : warning_at (loc, OPT_Wc___compat,
6585 : : "defining a type in a compound literal is invalid in C++");
6586 : 916045 : }
6587 : :
6588 : : /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6589 : : replacing with appropriate values if they are invalid. */
6590 : :
6591 : : static void
6592 : 51992 : check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
6593 : : tree orig_name)
6594 : : {
6595 : 51992 : tree type_mv;
6596 : 51992 : unsigned int max_width;
6597 : 51992 : unsigned HOST_WIDE_INT w;
6598 : 51992 : const char *name = (orig_name
6599 : 94796 : ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
6600 : 9188 : : _("<anonymous>"));
6601 : :
6602 : : /* Detect and ignore out of range field width and process valid
6603 : : field widths. */
6604 : 51992 : if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
6605 : : {
6606 : 8 : error_at (loc, "bit-field %qs width not an integer constant", name);
6607 : 8 : *width = integer_one_node;
6608 : : }
6609 : : else
6610 : : {
6611 : 51984 : if (TREE_CODE (*width) != INTEGER_CST)
6612 : : {
6613 : 12 : *width = c_fully_fold (*width, false, NULL);
6614 : 12 : if (TREE_CODE (*width) == INTEGER_CST)
6615 : 6 : pedwarn (loc, OPT_Wpedantic,
6616 : : "bit-field %qs width not an integer constant expression",
6617 : : name);
6618 : : }
6619 : 51984 : if (TREE_CODE (*width) != INTEGER_CST)
6620 : : {
6621 : 6 : error_at (loc, "bit-field %qs width not an integer constant", name);
6622 : 6 : *width = integer_one_node;
6623 : : }
6624 : 51984 : constant_expression_warning (*width);
6625 : 51984 : if (tree_int_cst_sgn (*width) < 0)
6626 : : {
6627 : 2 : error_at (loc, "negative width in bit-field %qs", name);
6628 : 2 : *width = integer_one_node;
6629 : : }
6630 : 51982 : else if (integer_zerop (*width) && orig_name)
6631 : : {
6632 : 4 : error_at (loc, "zero width for bit-field %qs", name);
6633 : 4 : *width = integer_one_node;
6634 : : }
6635 : : }
6636 : :
6637 : : /* Detect invalid bit-field type. */
6638 : 51992 : if (TREE_CODE (*type) != INTEGER_TYPE
6639 : : && TREE_CODE (*type) != BOOLEAN_TYPE
6640 : : && TREE_CODE (*type) != ENUMERAL_TYPE
6641 : 51992 : && TREE_CODE (*type) != BITINT_TYPE)
6642 : : {
6643 : 6 : error_at (loc, "bit-field %qs has invalid type", name);
6644 : 6 : *type = unsigned_type_node;
6645 : : }
6646 : :
6647 : 51992 : if (TYPE_WARN_IF_NOT_ALIGN (*type))
6648 : : {
6649 : 1 : error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6650 : : name);
6651 : 1 : *type = unsigned_type_node;
6652 : : }
6653 : :
6654 : 51992 : type_mv = TYPE_MAIN_VARIANT (*type);
6655 : 51992 : if (!in_system_header_at (input_location)
6656 : 29369 : && type_mv != integer_type_node
6657 : 26633 : && type_mv != unsigned_type_node
6658 : 64597 : && type_mv != boolean_type_node)
6659 : 12023 : pedwarn_c90 (loc, OPT_Wpedantic,
6660 : : "type of bit-field %qs is a GCC extension", name);
6661 : :
6662 : 51992 : max_width = TYPE_PRECISION (*type);
6663 : :
6664 : 51992 : if (compare_tree_int (*width, max_width) > 0)
6665 : : {
6666 : 4 : error_at (loc, "width of %qs exceeds its type", name);
6667 : 4 : w = max_width;
6668 : 4 : *width = build_int_cst (integer_type_node, w);
6669 : : }
6670 : : else
6671 : 51988 : w = tree_to_uhwi (*width);
6672 : :
6673 : : /* Truncation of hardbool false and true representation values is always safe:
6674 : : either the values remain different, or we'll report a problem when creating
6675 : : the narrower type. */
6676 : 51992 : if (c_hardbool_type_attr (*type))
6677 : 51992 : return;
6678 : :
6679 : 51711 : if (TREE_CODE (*type) == ENUMERAL_TYPE)
6680 : : {
6681 : 4130 : struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
6682 : 4130 : if (!lt
6683 : 4120 : || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
6684 : 8246 : || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
6685 : 15 : warning_at (loc, 0, "%qs is narrower than values of its type", name);
6686 : : }
6687 : : }
6688 : :
6689 : :
6690 : :
6691 : : /* Print warning about variable length array if necessary. */
6692 : :
6693 : : static void
6694 : 22447 : warn_variable_length_array (tree name, tree size)
6695 : : {
6696 : 22447 : if (TREE_CONSTANT (size))
6697 : : {
6698 : 180 : if (name)
6699 : 168 : pedwarn_c90 (input_location, OPT_Wvla,
6700 : : "ISO C90 forbids array %qE whose size "
6701 : : "cannot be evaluated", name);
6702 : : else
6703 : 12 : pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
6704 : : "whose size cannot be evaluated");
6705 : : }
6706 : : else
6707 : : {
6708 : 22267 : if (name)
6709 : 13786 : pedwarn_c90 (input_location, OPT_Wvla,
6710 : : "ISO C90 forbids variable length array %qE", name);
6711 : : else
6712 : 8481 : pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
6713 : : "length array");
6714 : : }
6715 : 22447 : }
6716 : :
6717 : : /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6718 : : considering only those c_declspec_words found in LIST, which
6719 : : must be terminated by cdw_number_of_elements. */
6720 : :
6721 : : static location_t
6722 : 235 : smallest_type_quals_location (const location_t *locations,
6723 : : const c_declspec_word *list)
6724 : : {
6725 : 235 : location_t loc = UNKNOWN_LOCATION;
6726 : 1410 : while (*list != cdw_number_of_elements)
6727 : : {
6728 : 1175 : location_t newloc = locations[*list];
6729 : 1175 : if (loc == UNKNOWN_LOCATION
6730 : 288 : || (newloc != UNKNOWN_LOCATION && newloc < loc))
6731 : 887 : loc = newloc;
6732 : 1175 : list++;
6733 : : }
6734 : :
6735 : 235 : return loc;
6736 : : }
6737 : :
6738 : :
6739 : : /* We attach an artificial TYPE_DECL to pointed-to type
6740 : : and arrange for it to be included in a DECL_EXPR. This
6741 : : forces the sizes evaluation at a safe point and ensures it
6742 : : is not deferred until e.g. within a deeper conditional context.
6743 : :
6744 : : PARM contexts have no enclosing statement list that
6745 : : can hold the DECL_EXPR, so we need to use a BIND_EXPR
6746 : : instead, and add it to the list of expressions that
6747 : : need to be evaluated.
6748 : :
6749 : : TYPENAME contexts do have an enclosing statement list,
6750 : : but it would be incorrect to use it, as the size should
6751 : : only be evaluated if the containing expression is
6752 : : evaluated. We might also be in the middle of an
6753 : : expression with side effects on the pointed-to type size
6754 : : "arguments" prior to the pointer declaration point and
6755 : : the fake TYPE_DECL in the enclosing context would force
6756 : : the size evaluation prior to the side effects. We therefore
6757 : : use BIND_EXPRs in TYPENAME contexts too. */
6758 : : static void
6759 : 6982 : add_decl_expr (location_t loc, tree type, tree *expr, bool set_name_p)
6760 : : {
6761 : 6982 : tree bind = NULL_TREE;
6762 : 6982 : if (expr)
6763 : : {
6764 : 1546 : bind = build3 (BIND_EXPR, void_type_node, NULL_TREE, NULL_TREE,
6765 : : NULL_TREE);
6766 : 1546 : TREE_SIDE_EFFECTS (bind) = 1;
6767 : 1546 : BIND_EXPR_BODY (bind) = push_stmt_list ();
6768 : 1546 : push_scope ();
6769 : : }
6770 : :
6771 : 6982 : tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6772 : 6982 : pushdecl (decl);
6773 : 6982 : DECL_ARTIFICIAL (decl) = 1;
6774 : 6982 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6775 : 6982 : if (set_name_p)
6776 : 6315 : TYPE_NAME (type) = decl;
6777 : :
6778 : 6982 : if (bind)
6779 : : {
6780 : 1546 : pop_scope ();
6781 : 1546 : BIND_EXPR_BODY (bind) = pop_stmt_list (BIND_EXPR_BODY (bind));
6782 : 1546 : if (*expr)
6783 : 1451 : *expr = build2 (COMPOUND_EXPR, void_type_node, *expr, bind);
6784 : : else
6785 : 95 : *expr = bind;
6786 : : }
6787 : 6982 : }
6788 : :
6789 : :
6790 : : /* Add attribute "arg spec" to ATTRS corresponding to an array/VLA parameter
6791 : : declared with type TYPE. The attribute has two arguments. The first is
6792 : : a string that encodes the presence of the static keyword. The second is
6793 : : the declared type of the array before adjustment, i.e. as an array type
6794 : : including the outermost bound. */
6795 : :
6796 : : static tree
6797 : 434365 : build_arg_spec_attribute (tree type, bool static_p, tree attrs)
6798 : : {
6799 : 434365 : tree vbchain = tree_cons (NULL_TREE, type, NULL_TREE);
6800 : 434365 : tree acsstr = static_p ? build_string (7, "static") :
6801 : 434243 : build_string (1, "");
6802 : 434365 : tree args = tree_cons (NULL_TREE, acsstr, vbchain);
6803 : 434365 : tree name = get_identifier ("arg spec");
6804 : 434365 : return tree_cons (name, args, attrs);
6805 : : }
6806 : :
6807 : :
6808 : : /* Given declspecs and a declarator,
6809 : : determine the name and type of the object declared
6810 : : and construct a ..._DECL node for it.
6811 : : (In one case we can return a ..._TYPE node instead.
6812 : : For invalid input we sometimes return NULL_TREE.)
6813 : :
6814 : : DECLSPECS is a c_declspecs structure for the declaration specifiers.
6815 : :
6816 : : DECL_CONTEXT says which syntactic context this declaration is in:
6817 : : NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6818 : : FUNCDEF for a function definition. Like NORMAL but a few different
6819 : : error messages in each case. Return value may be zero meaning
6820 : : this definition is too screwy to try to parse.
6821 : : PARM for a parameter declaration (either within a function prototype
6822 : : or before a function body). Make a PARM_DECL, or return void_type_node.
6823 : : TYPENAME if for a typename (in a cast or sizeof).
6824 : : Don't make a DECL node; just return the ..._TYPE node.
6825 : : GENERIC_ASSOC for typenames in a generic association.
6826 : : FIELD for a struct or union field; make a FIELD_DECL.
6827 : : INITIALIZED is true if the decl has an initializer.
6828 : : WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6829 : : representing the width of the bit-field.
6830 : : DECL_ATTRS points to the list of attributes that should be added to this
6831 : : decl. Any nested attributes that belong on the decl itself will be
6832 : : added to this list.
6833 : : If EXPR is not NULL, any expressions that need to be evaluated as
6834 : : part of evaluating variably modified types will be stored in *EXPR.
6835 : : If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6836 : : set to indicate whether operands in *EXPR can be used in constant
6837 : : expressions.
6838 : : DEPRECATED_STATE is a deprecated_states value indicating whether
6839 : : deprecation/unavailability warnings should be suppressed.
6840 : :
6841 : : In the TYPENAME case, DECLARATOR is really an absolute declarator.
6842 : : It may also be so in the PARM case, for a prototype where the
6843 : : argument type is specified but not the name.
6844 : :
6845 : : This function is where the complicated C meanings of `static'
6846 : : and `extern' are interpreted. */
6847 : :
6848 : : static tree
6849 : 312350254 : grokdeclarator (const struct c_declarator *declarator,
6850 : : struct c_declspecs *declspecs,
6851 : : enum decl_context decl_context, bool initialized, tree *width,
6852 : : tree *decl_attrs, tree *expr, bool *expr_const_operands,
6853 : : enum deprecated_states deprecated_state)
6854 : : {
6855 : 312350254 : tree type = declspecs->type;
6856 : 312350254 : bool threadp = declspecs->thread_p;
6857 : 312350254 : bool constexprp = declspecs->constexpr_p;
6858 : 312350254 : enum c_storage_class storage_class = declspecs->storage_class;
6859 : 312350254 : int constp;
6860 : 312350254 : int restrictp;
6861 : 312350254 : int volatilep;
6862 : 312350254 : int atomicp;
6863 : 312350254 : int type_quals = TYPE_UNQUALIFIED;
6864 : 312350254 : tree name = NULL_TREE;
6865 : 312350254 : bool funcdef_flag = false;
6866 : 312350254 : bool funcdef_syntax = false;
6867 : 312350254 : bool size_varies = false;
6868 : 312350254 : bool size_error = false;
6869 : 312350254 : tree decl_attr = declspecs->decl_attr;
6870 : 312350254 : int array_ptr_quals = TYPE_UNQUALIFIED;
6871 : 312350254 : tree array_ptr_attrs = NULL_TREE;
6872 : 312350254 : bool array_parm_static = false;
6873 : 312350254 : bool array_parm_vla_unspec_p = false;
6874 : 312350254 : tree returned_attrs = NULL_TREE;
6875 : 312350254 : tree decl_id_attrs = NULL_TREE;
6876 : 312350254 : bool bitfield = width != NULL;
6877 : 312350254 : tree element_type;
6878 : 312350254 : tree orig_qual_type = NULL;
6879 : 312350254 : size_t orig_qual_indirect = 0;
6880 : 312350254 : struct c_arg_info *arg_info = 0;
6881 : 312350254 : addr_space_t as1, as2, address_space;
6882 : 312350254 : location_t loc = UNKNOWN_LOCATION;
6883 : 312350254 : tree expr_dummy;
6884 : 312350254 : bool expr_const_operands_dummy;
6885 : 312350254 : enum c_declarator_kind first_non_attr_kind;
6886 : 312350254 : unsigned int alignas_align = 0;
6887 : :
6888 : 312350254 : if (type == NULL_TREE)
6889 : : {
6890 : : /* This can occur for auto on a parameter in C23 mode. Set a
6891 : : dummy type here so subsequent code can give diagnostics for
6892 : : this case. */
6893 : 2 : gcc_assert (declspecs->c23_auto_p);
6894 : 2 : gcc_assert (decl_context == PARM);
6895 : 2 : type = declspecs->type = integer_type_node;
6896 : : }
6897 : 312350254 : if (TREE_CODE (type) == ERROR_MARK)
6898 : 29 : return error_mark_node;
6899 : 312350225 : if (expr == NULL)
6900 : : {
6901 : 38048 : expr = &expr_dummy;
6902 : 38048 : expr_dummy = NULL_TREE;
6903 : : }
6904 : 312350225 : if (expr_const_operands == NULL)
6905 : 191548157 : expr_const_operands = &expr_const_operands_dummy;
6906 : :
6907 : 312350225 : if (declspecs->expr)
6908 : : {
6909 : 916 : if (*expr)
6910 : 7 : *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6911 : : declspecs->expr);
6912 : : else
6913 : 909 : *expr = declspecs->expr;
6914 : : }
6915 : 312350225 : *expr_const_operands = declspecs->expr_const_operands;
6916 : :
6917 : 312350225 : if (decl_context == FUNCDEF)
6918 : 36132668 : funcdef_flag = true, decl_context = NORMAL;
6919 : :
6920 : : /* Look inside a declarator for the name being declared
6921 : : and get it as an IDENTIFIER_NODE, for an error message. */
6922 : 312350225 : {
6923 : 312350225 : const struct c_declarator *decl = declarator;
6924 : :
6925 : 312350225 : first_non_attr_kind = cdk_attrs;
6926 : 381867467 : while (decl)
6927 : 381867467 : switch (decl->kind)
6928 : : {
6929 : 1144559 : case cdk_array:
6930 : 1144559 : loc = decl->id_loc;
6931 : : /* FALL THRU. */
6932 : :
6933 : 69510468 : case cdk_function:
6934 : 69510468 : case cdk_pointer:
6935 : 69510468 : funcdef_syntax = (decl->kind == cdk_function);
6936 : 69510468 : if (first_non_attr_kind == cdk_attrs)
6937 : 67415320 : first_non_attr_kind = decl->kind;
6938 : 69510468 : decl = decl->declarator;
6939 : 69510468 : break;
6940 : :
6941 : 6774 : case cdk_attrs:
6942 : 6774 : decl = decl->declarator;
6943 : 6774 : break;
6944 : :
6945 : 312350225 : case cdk_id:
6946 : 312350225 : loc = decl->id_loc;
6947 : 312350225 : if (decl->u.id.id)
6948 : : name = decl->u.id.id;
6949 : 312350225 : decl_id_attrs = decl->u.id.attrs;
6950 : 312350225 : if (first_non_attr_kind == cdk_attrs)
6951 : 244934905 : first_non_attr_kind = decl->kind;
6952 : : decl = 0;
6953 : : break;
6954 : :
6955 : 0 : default:
6956 : 0 : gcc_unreachable ();
6957 : : }
6958 : 312350225 : if (name == NULL_TREE)
6959 : : {
6960 : 125405079 : gcc_assert (decl_context == PARM
6961 : : || decl_context == TYPENAME
6962 : : || decl_context == GENERIC_ASSOC
6963 : : || (decl_context == FIELD
6964 : : && declarator->kind == cdk_id));
6965 : 125405079 : gcc_assert (!initialized);
6966 : : }
6967 : : }
6968 : :
6969 : : /* An enum type specifier (": specifier-qualifier-list") may only be
6970 : : specified when the enum is being defined or in an empty
6971 : : declaration of the form "enum identifier enum-type-specifier;".
6972 : : Except for the case of an empty declaration that has additional
6973 : : declaration specifiers, all invalid contexts (declarations that
6974 : : aren't empty, type names, parameter declarations, member
6975 : : declarations) pass through grokdeclarator. */
6976 : 312350225 : if (declspecs->enum_type_specifier_ref_p)
6977 : 6 : error_at (loc, "%<enum%> underlying type may not be specified here");
6978 : :
6979 : : /* A function definition's declarator must have the form of
6980 : : a function declarator. */
6981 : :
6982 : 312350225 : if (funcdef_flag && !funcdef_syntax)
6983 : : return NULL_TREE;
6984 : :
6985 : : /* If this looks like a function definition, make it one,
6986 : : even if it occurs where parms are expected.
6987 : : Then store_parm_decls will reject it and not use it as a parm. */
6988 : 312350194 : if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
6989 : 22656 : decl_context = PARM;
6990 : :
6991 : 312350194 : if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
6992 : : {
6993 : 312350170 : if (declspecs->unavailable_p)
6994 : 28 : error_unavailable_use (declspecs->type, declspecs->decl_attr);
6995 : 312350142 : else if (declspecs->deprecated_p
6996 : 46 : && deprecated_state != DEPRECATED_SUPPRESS)
6997 : 38 : warn_deprecated_use (declspecs->type, declspecs->decl_attr);
6998 : : }
6999 : :
7000 : 312350194 : if ((decl_context == NORMAL || decl_context == FIELD)
7001 : 68121040 : && current_scope == file_scope
7002 : 372429023 : && c_type_variably_modified_p (type))
7003 : : {
7004 : 3 : if (name)
7005 : 3 : error_at (loc, "variably modified %qE at file scope", name);
7006 : : else
7007 : 0 : error_at (loc, "variably modified field at file scope");
7008 : 3 : type = integer_type_node;
7009 : : }
7010 : :
7011 : 312350194 : size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
7012 : :
7013 : : /* Diagnose defaulting to "int". */
7014 : :
7015 : 312350194 : if (declspecs->default_int_p)
7016 : : {
7017 : : /* Issue a warning if this is an ISO C 99 program or if
7018 : : -Wreturn-type and this is a function, or if -Wimplicit;
7019 : : prefer the former warning since it is more explicit. */
7020 : 9764 : if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
7021 : 1229 : && funcdef_flag)
7022 : 702 : warn_about_return_type = 1;
7023 : : else
7024 : : {
7025 : 9062 : if (name)
7026 : 9052 : permerror_opt (loc, OPT_Wimplicit_int,
7027 : : "type defaults to %<int%> in declaration "
7028 : : "of %qE", name);
7029 : : else
7030 : 10 : permerror_opt (loc, OPT_Wimplicit_int,
7031 : : "type defaults to %<int%> in type name");
7032 : : }
7033 : : }
7034 : :
7035 : : /* Adjust the type if a bit-field is being declared,
7036 : : -funsigned-bitfields applied and the type is not explicitly
7037 : : "signed". */
7038 : 312350194 : if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
7039 : 40 : && TREE_CODE (type) == INTEGER_TYPE)
7040 : 38 : type = c_common_unsigned_type (type);
7041 : :
7042 : : /* Figure out the type qualifiers for the declaration. There are
7043 : : two ways a declaration can become qualified. One is something
7044 : : like `const int i' where the `const' is explicit. Another is
7045 : : something like `typedef const int CI; CI i' where the type of the
7046 : : declaration contains the `const'. A third possibility is that
7047 : : there is a type qualifier on the element type of a typedefed
7048 : : array type, in which case we should extract that qualifier so
7049 : : that c_apply_type_quals_to_decl receives the full list of
7050 : : qualifiers to work with (C90 is not entirely clear about whether
7051 : : duplicate qualifiers should be diagnosed in this case, but it
7052 : : seems most appropriate to do so). */
7053 : 312350194 : element_type = strip_array_types (type);
7054 : 312350194 : constp = declspecs->const_p + TYPE_READONLY (element_type);
7055 : 312350194 : restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
7056 : 312350194 : volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
7057 : 312350194 : atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
7058 : 312350194 : as1 = declspecs->address_space;
7059 : 312350194 : as2 = TYPE_ADDR_SPACE (element_type);
7060 : 312350194 : address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
7061 : :
7062 : 312350194 : if (constp > 1)
7063 : 25 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
7064 : 312350194 : if (restrictp > 1)
7065 : 6 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
7066 : 312350194 : if (volatilep > 1)
7067 : 15 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
7068 : 312350194 : if (atomicp > 1)
7069 : 9 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
7070 : :
7071 : 312350194 : if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
7072 : 0 : error_at (loc, "conflicting named address spaces (%s vs %s)",
7073 : : c_addr_space_name (as1), c_addr_space_name (as2));
7074 : :
7075 : 312350194 : if ((TREE_CODE (type) == ARRAY_TYPE
7076 : 312175301 : || first_non_attr_kind == cdk_array)
7077 : 313427297 : && TYPE_QUALS (element_type))
7078 : : {
7079 : 73 : orig_qual_type = type;
7080 : 73 : type = c_build_qualified_type (type, TYPE_UNQUALIFIED);
7081 : : }
7082 : 312350194 : type_quals = ((constp ? TYPE_QUAL_CONST : 0)
7083 : 312350194 : | (restrictp ? TYPE_QUAL_RESTRICT : 0)
7084 : 312350194 : | (volatilep ? TYPE_QUAL_VOLATILE : 0)
7085 : 312350194 : | (atomicp ? TYPE_QUAL_ATOMIC : 0)
7086 : 312350194 : | ENCODE_QUAL_ADDR_SPACE (address_space));
7087 : 312350194 : if (type_quals != TYPE_QUALS (element_type))
7088 : 12750804 : orig_qual_type = NULL_TREE;
7089 : :
7090 : : /* Applying the _Atomic qualifier to an array type (through the use
7091 : : of typedefs or typeof) must be detected here. If the qualifier
7092 : : is introduced later, any appearance of applying it to an array is
7093 : : actually applying it to an element of that array. */
7094 : 312350194 : if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
7095 : 6 : error_at (loc, "%<_Atomic%>-qualified array type");
7096 : :
7097 : : /* Warn about storage classes that are invalid for certain
7098 : : kinds of declarations (parameters, typenames, etc.). */
7099 : :
7100 : 312350194 : if (funcdef_flag
7101 : 36132637 : && (threadp
7102 : : || constexprp
7103 : 36132635 : || storage_class == csc_auto
7104 : 36132635 : || storage_class == csc_register
7105 : 36132590 : || storage_class == csc_typedef))
7106 : : {
7107 : 47 : if (storage_class == csc_auto)
7108 : 42 : pedwarn (loc,
7109 : 81 : (current_scope == file_scope) ? 0 : OPT_Wpedantic,
7110 : : "function definition declared %<auto%>");
7111 : 50 : if (storage_class == csc_register)
7112 : 3 : error_at (loc, "function definition declared %<register%>");
7113 : 50 : if (storage_class == csc_typedef)
7114 : 3 : error_at (loc, "function definition declared %<typedef%>");
7115 : 50 : if (threadp)
7116 : 2 : error_at (loc, "function definition declared %qs",
7117 : 2 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7118 : 50 : threadp = false;
7119 : : /* The parser ensures a constexpr function definition never
7120 : : reaches here. */
7121 : 50 : gcc_assert (!constexprp);
7122 : 50 : if (storage_class == csc_auto
7123 : 50 : || storage_class == csc_register
7124 : : || storage_class == csc_typedef)
7125 : 55 : storage_class = csc_none;
7126 : : }
7127 : 312350144 : else if (decl_context != NORMAL && (storage_class != csc_none
7128 : 248510535 : || threadp
7129 : 248509952 : || constexprp
7130 : 248509950 : || declspecs->c23_auto_p))
7131 : : {
7132 : 587 : if (decl_context == PARM
7133 : 587 : && storage_class == csc_register
7134 : 568 : && !constexprp
7135 : 566 : && !declspecs->c23_auto_p)
7136 : : ;
7137 : : else
7138 : : {
7139 : 21 : switch (decl_context)
7140 : : {
7141 : 0 : case FIELD:
7142 : 0 : if (name)
7143 : 0 : error_at (loc, "storage class specified for structure "
7144 : : "field %qE", name);
7145 : : else
7146 : 0 : error_at (loc, "storage class specified for structure field");
7147 : : break;
7148 : 21 : case PARM:
7149 : 21 : if (name)
7150 : 7 : error_at (loc, "storage class specified for parameter %qE",
7151 : : name);
7152 : : else
7153 : 14 : error_at (loc, "storage class specified for unnamed parameter");
7154 : : break;
7155 : 0 : default:
7156 : 0 : error_at (loc, "storage class specified for typename");
7157 : 0 : break;
7158 : : }
7159 : 312350194 : storage_class = csc_none;
7160 : 312350194 : threadp = false;
7161 : 312350194 : constexprp = false;
7162 : : }
7163 : : }
7164 : 312349557 : else if (storage_class == csc_extern
7165 : 312349557 : && initialized
7166 : 35281582 : && !funcdef_flag)
7167 : : {
7168 : : /* 'extern' with initialization is invalid if not at file scope. */
7169 : 28 : if (current_scope == file_scope)
7170 : : {
7171 : : /* It is fine to have 'extern const' when compiling at C
7172 : : and C++ intersection. */
7173 : 19 : if (!(warn_cxx_compat && constp))
7174 : 18 : warning_at (loc, 0, "%qE initialized and declared %<extern%>",
7175 : : name);
7176 : : }
7177 : : else
7178 : 9 : error_at (loc, "%qE has both %<extern%> and initializer", name);
7179 : : }
7180 : 312349529 : else if (current_scope == file_scope)
7181 : : {
7182 : 60953093 : if (storage_class == csc_auto)
7183 : 7 : error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
7184 : : name);
7185 : 60953093 : if (pedantic && storage_class == csc_register)
7186 : 4 : pedwarn (input_location, OPT_Wpedantic,
7187 : : "file-scope declaration of %qE specifies %<register%>", name);
7188 : : }
7189 : : else
7190 : : {
7191 : 251396436 : if (storage_class == csc_extern && funcdef_flag)
7192 : 3 : error_at (loc, "nested function %qE declared %<extern%>", name);
7193 : 251396433 : else if (threadp && storage_class == csc_none)
7194 : : {
7195 : 14 : error_at (loc, "function-scope %qE implicitly auto and declared "
7196 : : "%qs", name,
7197 : 7 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7198 : 7 : threadp = false;
7199 : : }
7200 : : }
7201 : :
7202 : : /* Now figure out the structure of the declarator proper.
7203 : : Descend through it, creating more complex types, until we reach
7204 : : the declared identifier (or NULL_TREE, in an absolute declarator).
7205 : : At each stage we maintain an unqualified version of the type
7206 : : together with any qualifiers that should be applied to it with
7207 : : c_build_qualified_type; this way, array types including
7208 : : multidimensional array types are first built up in unqualified
7209 : : form and then the qualified form is created with
7210 : : TYPE_MAIN_VARIANT pointing to the unqualified form. */
7211 : :
7212 : 381867435 : while (declarator && declarator->kind != cdk_id)
7213 : : {
7214 : 69517241 : if (type == error_mark_node)
7215 : : {
7216 : 39 : declarator = declarator->declarator;
7217 : 39 : continue;
7218 : : }
7219 : :
7220 : : /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
7221 : : a cdk_pointer (for *...),
7222 : : a cdk_function (for ...(...)),
7223 : : a cdk_attrs (for nested attributes),
7224 : : or a cdk_id (for the name being declared
7225 : : or the place in an absolute declarator
7226 : : where the name was omitted).
7227 : : For the last case, we have just exited the loop.
7228 : :
7229 : : At this point, TYPE is the type of elements of an array,
7230 : : or for a function to return, or for a pointer to point to.
7231 : : After this sequence of ifs, TYPE is the type of the
7232 : : array or function or pointer, and DECLARATOR has had its
7233 : : outermost layer removed. */
7234 : :
7235 : 69517202 : if (array_ptr_quals != TYPE_UNQUALIFIED
7236 : 69517202 : || array_ptr_attrs != NULL_TREE
7237 : 69517202 : || array_parm_static)
7238 : : {
7239 : : /* Only the innermost declarator (making a parameter be of
7240 : : array type which is converted to pointer type)
7241 : : may have static or type qualifiers. */
7242 : 1 : error_at (loc, "static or type qualifiers in non-parameter array declarator");
7243 : 1 : array_ptr_quals = TYPE_UNQUALIFIED;
7244 : 1 : array_ptr_attrs = NULL_TREE;
7245 : 1 : array_parm_static = false;
7246 : : }
7247 : :
7248 : 69517202 : switch (declarator->kind)
7249 : : {
7250 : 6774 : case cdk_attrs:
7251 : 6774 : {
7252 : : /* A declarator with embedded attributes. */
7253 : 6774 : tree attrs = declarator->u.attrs;
7254 : 6774 : const struct c_declarator *inner_decl;
7255 : 6774 : int attr_flags = 0;
7256 : 6774 : declarator = declarator->declarator;
7257 : : /* Standard attribute syntax precisely defines what entity
7258 : : an attribute in each position appertains to, so only
7259 : : apply laxity about positioning to GNU attribute syntax.
7260 : : Standard attributes applied to a function or array
7261 : : declarator apply exactly to that type; standard
7262 : : attributes applied to the identifier apply to the
7263 : : declaration rather than to the type, and are specified
7264 : : using a cdk_id declarator rather than using
7265 : : cdk_attrs. */
7266 : 6774 : inner_decl = declarator;
7267 : 6774 : while (inner_decl->kind == cdk_attrs)
7268 : 0 : inner_decl = inner_decl->declarator;
7269 : 6774 : if (!cxx11_attribute_p (attrs))
7270 : : {
7271 : 6676 : if (inner_decl->kind == cdk_id)
7272 : : attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
7273 : : else if (inner_decl->kind == cdk_function)
7274 : : attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
7275 : : else if (inner_decl->kind == cdk_array)
7276 : 6774 : attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
7277 : : }
7278 : 6774 : attrs = c_warn_type_attributes (type, attrs);
7279 : 6774 : returned_attrs = decl_attributes (&type,
7280 : : chainon (returned_attrs, attrs),
7281 : : attr_flags);
7282 : 6774 : break;
7283 : : }
7284 : 1144549 : case cdk_array:
7285 : 1144549 : {
7286 : 1144549 : tree itype = NULL_TREE;
7287 : 1144549 : tree size = declarator->u.array.dimen;
7288 : : /* The index is a signed object `sizetype' bits wide. */
7289 : 1144549 : tree index_type = c_common_signed_type (sizetype);
7290 : :
7291 : 1144549 : array_ptr_quals = declarator->u.array.quals;
7292 : 1144549 : array_ptr_attrs = declarator->u.array.attrs;
7293 : 1144549 : array_parm_static = declarator->u.array.static_p;
7294 : 1144549 : array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
7295 : :
7296 : 1144549 : declarator = declarator->declarator;
7297 : :
7298 : : /* Check for some types that there cannot be arrays of. */
7299 : :
7300 : 1144549 : if (VOID_TYPE_P (type))
7301 : : {
7302 : 11 : if (name)
7303 : 9 : error_at (loc, "declaration of %qE as array of voids", name);
7304 : : else
7305 : 2 : error_at (loc, "declaration of type name as array of voids");
7306 : 11 : type = error_mark_node;
7307 : : }
7308 : :
7309 : 1144549 : if (TREE_CODE (type) == FUNCTION_TYPE)
7310 : : {
7311 : 3 : if (name)
7312 : 2 : error_at (loc, "declaration of %qE as array of functions",
7313 : : name);
7314 : : else
7315 : 1 : error_at (loc, "declaration of type name as array of "
7316 : : "functions");
7317 : 3 : type = error_mark_node;
7318 : : }
7319 : :
7320 : 22010 : if (pedantic && !in_system_header_at (input_location)
7321 : 1159389 : && flexible_array_type_p (type))
7322 : 20 : pedwarn (loc, OPT_Wpedantic,
7323 : : "invalid use of structure with flexible array member");
7324 : :
7325 : 1144549 : if (size == error_mark_node)
7326 : 119 : type = error_mark_node;
7327 : :
7328 : 1144549 : if (type == error_mark_node)
7329 : 133 : continue;
7330 : :
7331 : 1144416 : if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
7332 : : {
7333 : 0 : type = error_mark_node;
7334 : 0 : continue;
7335 : : }
7336 : :
7337 : : /* If size was specified, set ITYPE to a range-type for
7338 : : that size. Otherwise, ITYPE remains null. finish_decl
7339 : : may figure it out from an initial value. */
7340 : :
7341 : 1144416 : if (size)
7342 : : {
7343 : 975019 : bool size_maybe_const = true;
7344 : 975019 : bool size_int_const = (TREE_CODE (size) == INTEGER_CST
7345 : 975019 : && !TREE_OVERFLOW (size));
7346 : 975019 : bool this_size_varies = false;
7347 : :
7348 : : /* Strip NON_LVALUE_EXPRs since we aren't using as an
7349 : : lvalue. */
7350 : 975028 : STRIP_TYPE_NOPS (size);
7351 : :
7352 : 975019 : if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
7353 : : {
7354 : 16 : if (name)
7355 : 14 : error_at (loc, "size of array %qE has non-integer type",
7356 : : name);
7357 : : else
7358 : 2 : error_at (loc,
7359 : : "size of unnamed array has non-integer type");
7360 : 16 : size = integer_one_node;
7361 : 16 : size_int_const = true;
7362 : 16 : size_error = true;
7363 : : }
7364 : : /* This can happen with enum forward declaration. */
7365 : 975003 : else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
7366 : : {
7367 : 0 : if (name)
7368 : 0 : error_at (loc, "size of array %qE has incomplete type",
7369 : : name);
7370 : : else
7371 : 0 : error_at (loc, "size of unnamed array has incomplete "
7372 : : "type");
7373 : 0 : size = integer_one_node;
7374 : 0 : size_int_const = true;
7375 : 0 : size_error = true;
7376 : : }
7377 : :
7378 : 975019 : size = c_fully_fold (size, false, &size_maybe_const);
7379 : :
7380 : 975019 : if (pedantic && size_maybe_const && integer_zerop (size))
7381 : : {
7382 : 3 : if (name)
7383 : 3 : pedwarn (loc, OPT_Wpedantic,
7384 : : "ISO C forbids zero-size array %qE", name);
7385 : : else
7386 : 0 : pedwarn (loc, OPT_Wpedantic,
7387 : : "ISO C forbids zero-size array");
7388 : : }
7389 : :
7390 : 975019 : if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
7391 : : {
7392 : 952705 : constant_expression_warning (size);
7393 : 952705 : if (tree_int_cst_sgn (size) < 0)
7394 : : {
7395 : 700 : if (name)
7396 : 697 : error_at (loc, "size of array %qE is negative", name);
7397 : : else
7398 : 3 : error_at (loc, "size of unnamed array is negative");
7399 : 700 : size = integer_one_node;
7400 : 700 : size_int_const = true;
7401 : 700 : size_error = true;
7402 : : }
7403 : : /* Handle a size folded to an integer constant but
7404 : : not an integer constant expression. */
7405 : 952705 : if (!size_int_const)
7406 : : {
7407 : : /* If this is a file scope declaration of an
7408 : : ordinary identifier, this is invalid code;
7409 : : diagnosing it here and not subsequently
7410 : : treating the type as variable-length avoids
7411 : : more confusing diagnostics later. */
7412 : 154 : if ((decl_context == NORMAL || decl_context == FIELD)
7413 : 142 : && current_scope == file_scope)
7414 : 14 : pedwarn (input_location, 0,
7415 : : "variably modified %qE at file scope",
7416 : : name);
7417 : : else
7418 : : this_size_varies = size_varies = true;
7419 : 154 : warn_variable_length_array (name, size);
7420 : : }
7421 : : }
7422 : 22314 : else if ((decl_context == NORMAL || decl_context == FIELD)
7423 : 13220 : && current_scope == file_scope)
7424 : : {
7425 : 21 : error_at (loc, "variably modified %qE at file scope", name);
7426 : 21 : size = integer_one_node;
7427 : : }
7428 : : else
7429 : : {
7430 : : /* Make sure the array size remains visibly
7431 : : nonconstant even if it is (eg) a const variable
7432 : : with known value. */
7433 : 22293 : this_size_varies = size_varies = true;
7434 : 22293 : warn_variable_length_array (name, size);
7435 : 22293 : if (sanitize_flags_p (SANITIZE_VLA)
7436 : 178 : && current_function_decl != NULL_TREE
7437 : 22453 : && decl_context == NORMAL)
7438 : : {
7439 : : /* Evaluate the array size only once. */
7440 : 152 : size = save_expr (size);
7441 : 152 : size = c_fully_fold (size, false, NULL);
7442 : 152 : size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
7443 : : ubsan_instrument_vla (loc, size),
7444 : : size);
7445 : : }
7446 : : }
7447 : :
7448 : 975019 : if (integer_zerop (size) && !this_size_varies)
7449 : : {
7450 : : /* A zero-length array cannot be represented with
7451 : : an unsigned index type, which is what we'll
7452 : : get with build_index_type. Create an
7453 : : open-ended range instead. */
7454 : 2599 : itype = build_range_type (sizetype, size, NULL_TREE);
7455 : : }
7456 : : else
7457 : : {
7458 : : /* Arrange for the SAVE_EXPR on the inside of the
7459 : : MINUS_EXPR, which allows the -1 to get folded
7460 : : with the +1 that happens when building TYPE_SIZE. */
7461 : 972420 : if (size_varies)
7462 : 22803 : size = save_expr (size);
7463 : 972420 : if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
7464 : 140 : size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7465 : : integer_zero_node, size);
7466 : :
7467 : : /* Compute the maximum valid index, that is, size
7468 : : - 1. Do the calculation in index_type, so that
7469 : : if it is a variable the computations will be
7470 : : done in the proper mode. */
7471 : 972420 : itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
7472 : : convert (index_type, size),
7473 : : convert (index_type,
7474 : : size_one_node));
7475 : :
7476 : : /* The above overflows when size does not fit
7477 : : in index_type.
7478 : : ??? While a size of INT_MAX+1 technically shouldn't
7479 : : cause an overflow (because we subtract 1), handling
7480 : : this case seems like an unnecessary complication. */
7481 : 972420 : if (TREE_CODE (size) == INTEGER_CST
7482 : 949987 : && !int_fits_type_p (size, index_type))
7483 : : {
7484 : 6 : if (name)
7485 : 5 : error_at (loc, "size of array %qE is too large",
7486 : : name);
7487 : : else
7488 : 1 : error_at (loc, "size of unnamed array is too large");
7489 : 6 : type = error_mark_node;
7490 : 6 : continue;
7491 : : }
7492 : :
7493 : 972414 : itype = build_index_type (itype);
7494 : : }
7495 : 975013 : if (this_size_varies)
7496 : : {
7497 : 22433 : if (TREE_SIDE_EFFECTS (size))
7498 : : {
7499 : 22071 : if (*expr)
7500 : 8173 : *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7501 : : *expr, size);
7502 : : else
7503 : 13898 : *expr = size;
7504 : : }
7505 : 22433 : *expr_const_operands &= size_maybe_const;
7506 : : }
7507 : : }
7508 : 169397 : else if (decl_context == FIELD)
7509 : : {
7510 : 84737 : bool flexible_array_member = false;
7511 : 84737 : if (array_parm_vla_unspec_p)
7512 : : /* Field names can in fact have function prototype
7513 : : scope so [*] is disallowed here through making
7514 : : the field variably modified, not through being
7515 : : something other than a declaration with function
7516 : : prototype scope. */
7517 : : size_varies = true;
7518 : : else
7519 : : {
7520 : : const struct c_declarator *t = declarator;
7521 : 84734 : while (t->kind == cdk_attrs)
7522 : 0 : t = t->declarator;
7523 : 84734 : flexible_array_member = (t->kind == cdk_id);
7524 : : }
7525 : 84734 : if (flexible_array_member
7526 : 84734 : && !in_system_header_at (input_location))
7527 : 83702 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7528 : : "support flexible array members");
7529 : :
7530 : : /* ISO C99 Flexible array members are effectively
7531 : : identical to GCC's zero-length array extension. */
7532 : 84737 : if (flexible_array_member)
7533 : 84714 : itype = build_index_type (NULL_TREE);
7534 : : }
7535 : :
7536 : : /* Complain about arrays of incomplete types. */
7537 : 1144410 : if (!COMPLETE_TYPE_P (type))
7538 : : {
7539 : 57 : auto_diagnostic_group d;
7540 : 57 : error_at (loc, "array type has incomplete element type %qT",
7541 : : type);
7542 : : /* See if we can be more helpful. */
7543 : 57 : if (TREE_CODE (type) == ARRAY_TYPE)
7544 : : {
7545 : 29 : if (name)
7546 : 24 : inform (loc, "declaration of %qE as multidimensional "
7547 : : "array must have bounds for all dimensions "
7548 : : "except the first", name);
7549 : : else
7550 : 5 : inform (loc, "declaration of multidimensional array "
7551 : : "must have bounds for all dimensions except "
7552 : : "the first");
7553 : : }
7554 : 57 : type = error_mark_node;
7555 : 57 : }
7556 : : else
7557 : : /* When itype is NULL, a shared incomplete array type is
7558 : : returned for all array of a given type. Elsewhere we
7559 : : make sure we don't complete that type before copying
7560 : : it, but here we want to make sure we don't ever
7561 : : modify the shared type, so we gcc_assert (itype)
7562 : : below. */
7563 : : {
7564 : 1144353 : addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
7565 : 1144353 : if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
7566 : 0 : type = c_build_qualified_type (type,
7567 : : ENCODE_QUAL_ADDR_SPACE (as));
7568 : 1144353 : if (array_parm_vla_unspec_p)
7569 : 150 : type = c_build_array_type_unspecified (type);
7570 : : else
7571 : 1144203 : type = c_build_array_type (type, itype);
7572 : : }
7573 : :
7574 : 1144410 : if (array_parm_vla_unspec_p)
7575 : : {
7576 : : /* C99 6.7.5.2p4 */
7577 : 150 : if (decl_context == TYPENAME)
7578 : 6 : warning (0, "%<[*]%> not in a declaration");
7579 : 144 : else if (decl_context != GENERIC_ASSOC
7580 : 144 : && decl_context != PARM
7581 : 7 : && decl_context != FIELD)
7582 : : {
7583 : 4 : error ("%<[*]%> not allowed in other than function prototype scope "
7584 : : "or generic association");
7585 : 4 : type = error_mark_node;
7586 : : }
7587 : : size_varies = true;
7588 : : }
7589 : :
7590 : 1144410 : if (type != error_mark_node)
7591 : : {
7592 : : /* The GCC extension for zero-length arrays differs from
7593 : : ISO flexible array members in that sizeof yields
7594 : : zero. */
7595 : 1144349 : if (size && integer_zerop (size))
7596 : : {
7597 : 2592 : gcc_assert (itype);
7598 : 2592 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7599 : 2592 : TYPE_SIZE (type) = bitsize_zero_node;
7600 : 2592 : TYPE_SIZE_UNIT (type) = size_zero_node;
7601 : 2592 : SET_TYPE_STRUCTURAL_EQUALITY (type);
7602 : : }
7603 : :
7604 : 1144349 : if (!valid_array_size_p (loc, type, name))
7605 : 33 : type = error_mark_node;
7606 : : }
7607 : :
7608 : 1144410 : if (decl_context != PARM
7609 : 827699 : && (array_ptr_quals != TYPE_UNQUALIFIED
7610 : 827699 : || array_ptr_attrs != NULL_TREE
7611 : 827698 : || array_parm_static))
7612 : : {
7613 : 2 : error_at (loc, "static or type qualifiers in non-parameter "
7614 : : "array declarator");
7615 : 2 : array_ptr_quals = TYPE_UNQUALIFIED;
7616 : 2 : array_ptr_attrs = NULL_TREE;
7617 : 2 : array_parm_static = false;
7618 : : }
7619 : 1144410 : orig_qual_indirect++;
7620 : 1144410 : break;
7621 : : }
7622 : 50247873 : case cdk_function:
7623 : 50247873 : {
7624 : : /* Say it's a definition only for the declarator closest
7625 : : to the identifier, apart possibly from some
7626 : : attributes. */
7627 : 50247873 : bool really_funcdef = false;
7628 : 50247873 : tree arg_types;
7629 : 50247873 : orig_qual_type = NULL_TREE;
7630 : 50247873 : if (funcdef_flag)
7631 : : {
7632 : 36132659 : const struct c_declarator *t = declarator->declarator;
7633 : 36132677 : while (t->kind == cdk_attrs)
7634 : 18 : t = t->declarator;
7635 : 36132659 : really_funcdef = (t->kind == cdk_id);
7636 : : }
7637 : :
7638 : : /* Declaring a function type. Make sure we have a valid
7639 : : type for the function to return. */
7640 : 50247873 : if (type == error_mark_node)
7641 : 0 : continue;
7642 : :
7643 : 50247873 : size_varies = false;
7644 : :
7645 : : /* Warn about some types functions can't return. */
7646 : 50247873 : if (TREE_CODE (type) == FUNCTION_TYPE)
7647 : : {
7648 : 0 : if (name)
7649 : 0 : error_at (loc, "%qE declared as function returning a "
7650 : : "function", name);
7651 : : else
7652 : 0 : error_at (loc, "type name declared as function "
7653 : : "returning a function");
7654 : 0 : type = integer_type_node;
7655 : : }
7656 : 50247873 : if (TREE_CODE (type) == ARRAY_TYPE)
7657 : : {
7658 : 0 : if (name)
7659 : 0 : error_at (loc, "%qE declared as function returning an array",
7660 : : name);
7661 : : else
7662 : 0 : error_at (loc, "type name declared as function returning "
7663 : : "an array");
7664 : 0 : type = integer_type_node;
7665 : : }
7666 : :
7667 : : /* Construct the function type and go to the next
7668 : : inner layer of declarator. */
7669 : 50247873 : arg_info = declarator->u.arg_info;
7670 : 50247873 : arg_types = grokparms (arg_info, really_funcdef);
7671 : :
7672 : : /* Type qualifiers before the return type of the function
7673 : : qualify the return type, not the function type. */
7674 : 50247873 : if (type_quals)
7675 : : {
7676 : 235 : const enum c_declspec_word ignored_quals_list[] =
7677 : : {
7678 : : cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
7679 : : cdw_atomic, cdw_number_of_elements
7680 : : };
7681 : 235 : location_t specs_loc
7682 : 235 : = smallest_type_quals_location (declspecs->locations,
7683 : : ignored_quals_list);
7684 : 235 : if (specs_loc == UNKNOWN_LOCATION)
7685 : 139 : specs_loc = declspecs->locations[cdw_typedef];
7686 : 139 : if (specs_loc == UNKNOWN_LOCATION)
7687 : 15 : specs_loc = loc;
7688 : :
7689 : : /* Type qualifiers on a function return type are
7690 : : normally permitted by the standard but have no
7691 : : effect, so give a warning at -Wreturn-type.
7692 : : Qualifiers on a void return type are banned on
7693 : : function definitions in ISO C; GCC used to used
7694 : : them for noreturn functions. The resolution of C11
7695 : : DR#423 means qualifiers (other than _Atomic) are
7696 : : actually removed from the return type when
7697 : : determining the function type. For C23, _Atomic is
7698 : : removed as well. */
7699 : 235 : int quals_used = type_quals;
7700 : 235 : if (flag_isoc23)
7701 : : quals_used = 0;
7702 : 65 : else if (flag_isoc11)
7703 : 31 : quals_used &= TYPE_QUAL_ATOMIC;
7704 : 65 : if (quals_used && VOID_TYPE_P (type) && really_funcdef)
7705 : 5 : pedwarn (specs_loc, 0,
7706 : : "function definition has qualified void "
7707 : : "return type");
7708 : : else
7709 : 230 : warning_at (specs_loc, OPT_Wignored_qualifiers,
7710 : : "type qualifiers ignored on function "
7711 : : "return type");
7712 : :
7713 : : /* Ensure an error for restrict on invalid types; the
7714 : : DR#423 resolution is not entirely clear about
7715 : : this. */
7716 : 235 : if (flag_isoc11
7717 : 201 : && (type_quals & TYPE_QUAL_RESTRICT)
7718 : 241 : && (!POINTER_TYPE_P (type)
7719 : 4 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
7720 : 4 : error_at (loc, "invalid use of %<restrict%>");
7721 : 235 : type = c_build_qualified_type (type, quals_used);
7722 : : }
7723 : 50247873 : type_quals = TYPE_UNQUALIFIED;
7724 : :
7725 : 100495746 : type = c_build_function_type (type, arg_types,
7726 : 50247873 : arg_info->no_named_args_stdarg_p);
7727 : 50247873 : declarator = declarator->declarator;
7728 : :
7729 : : /* Set the TYPE_CONTEXTs for each tagged type which is local to
7730 : : the formal parameter list of this FUNCTION_TYPE to point to
7731 : : the FUNCTION_TYPE node itself. */
7732 : 50247873 : {
7733 : 50247873 : c_arg_tag *tag;
7734 : 50247873 : unsigned ix;
7735 : :
7736 : 432115574 : FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7737 : 134 : TYPE_CONTEXT (tag->type) = type;
7738 : : }
7739 : : break;
7740 : : }
7741 : 18118006 : case cdk_pointer:
7742 : 18118006 : {
7743 : : /* Merge any constancy or volatility into the target type
7744 : : for the pointer. */
7745 : 18118006 : if ((type_quals & TYPE_QUAL_ATOMIC)
7746 : 1839 : && TREE_CODE (type) == FUNCTION_TYPE)
7747 : : {
7748 : 2 : error_at (loc,
7749 : : "%<_Atomic%>-qualified function type");
7750 : 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
7751 : : }
7752 : 18118004 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7753 : 1701 : && type_quals)
7754 : 0 : pedwarn (loc, OPT_Wpedantic,
7755 : : "ISO C forbids qualified function types");
7756 : 18116305 : if (type_quals)
7757 : 5962881 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7758 : : orig_qual_indirect);
7759 : 18118006 : orig_qual_type = NULL_TREE;
7760 : 18118006 : size_varies = false;
7761 : :
7762 : : /* When the pointed-to type involves components of variable size,
7763 : : care must be taken to ensure that the size evaluation code is
7764 : : emitted early enough to dominate all the possible later uses
7765 : : and late enough for the variables on which it depends to have
7766 : : been assigned.
7767 : :
7768 : : This is expected to happen automatically when the pointed-to
7769 : : type has a name/declaration of it's own, but special attention
7770 : : is required if the type is anonymous. */
7771 : 18118006 : if (!TYPE_NAME (type) && c_type_variably_modified_p (type))
7772 : : {
7773 : 5942 : bool bind_p = decl_context == TYPENAME
7774 : : || decl_context == FIELD
7775 : 5942 : || decl_context == PARM;
7776 : 11378 : add_decl_expr (loc, type, bind_p ? expr : NULL, true);
7777 : : }
7778 : :
7779 : 18118006 : type = c_build_pointer_type (type);
7780 : :
7781 : : /* Process type qualifiers (such as const or volatile)
7782 : : that were given inside the `*'. */
7783 : 18118006 : type_quals = declarator->u.pointer_quals;
7784 : :
7785 : 18118006 : declarator = declarator->declarator;
7786 : 18118006 : break;
7787 : : }
7788 : 0 : default:
7789 : 0 : gcc_unreachable ();
7790 : : }
7791 : : }
7792 : 312350194 : *decl_attrs = chainon (returned_attrs, *decl_attrs);
7793 : 312350194 : *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
7794 : :
7795 : : /* Now TYPE has the actual type, apart from any qualifiers in
7796 : : TYPE_QUALS. */
7797 : :
7798 : : /* Warn about address space used for things other than static memory or
7799 : : pointers. */
7800 : 312350194 : address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
7801 : 312350194 : if (!ADDR_SPACE_GENERIC_P (address_space))
7802 : : {
7803 : 10 : if (decl_context == NORMAL)
7804 : : {
7805 : 10 : switch (storage_class)
7806 : : {
7807 : 0 : case csc_auto:
7808 : 0 : error ("%qs combined with %<auto%> qualifier for %qE",
7809 : : c_addr_space_name (address_space), name);
7810 : 0 : break;
7811 : 0 : case csc_register:
7812 : 0 : error ("%qs combined with %<register%> qualifier for %qE",
7813 : : c_addr_space_name (address_space), name);
7814 : 0 : break;
7815 : 6 : case csc_none:
7816 : 6 : if (current_function_scope)
7817 : : {
7818 : 0 : error ("%qs specified for auto variable %qE",
7819 : : c_addr_space_name (address_space), name);
7820 : 0 : break;
7821 : : }
7822 : : break;
7823 : : case csc_static:
7824 : : case csc_extern:
7825 : : case csc_typedef:
7826 : : break;
7827 : 0 : default:
7828 : 0 : gcc_unreachable ();
7829 : : }
7830 : : }
7831 : 0 : else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
7832 : : {
7833 : 0 : if (name)
7834 : 0 : error ("%qs specified for parameter %qE",
7835 : : c_addr_space_name (address_space), name);
7836 : : else
7837 : 0 : error ("%qs specified for unnamed parameter",
7838 : : c_addr_space_name (address_space));
7839 : : }
7840 : 0 : else if (decl_context == FIELD)
7841 : : {
7842 : 0 : if (name)
7843 : 0 : error ("%qs specified for structure field %qE",
7844 : : c_addr_space_name (address_space), name);
7845 : : else
7846 : 0 : error ("%qs specified for structure field",
7847 : : c_addr_space_name (address_space));
7848 : : }
7849 : : }
7850 : :
7851 : : /* Check the type and width of a bit-field. */
7852 : 312350194 : if (bitfield)
7853 : : {
7854 : 51992 : check_bitfield_type_and_width (loc, &type, width, name);
7855 : : /* C11 makes it implementation-defined (6.7.2.1#5) whether
7856 : : atomic types are permitted for bit-fields; we have no code to
7857 : : make bit-field accesses atomic, so disallow them. */
7858 : 51992 : if (type_quals & TYPE_QUAL_ATOMIC)
7859 : : {
7860 : 2 : if (name)
7861 : 1 : error_at (loc, "bit-field %qE has atomic type", name);
7862 : : else
7863 : 1 : error_at (loc, "bit-field has atomic type");
7864 : 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
7865 : : }
7866 : : }
7867 : :
7868 : : /* Reject invalid uses of _Alignas. */
7869 : 312350194 : if (declspecs->alignas_p)
7870 : : {
7871 : 188 : if (storage_class == csc_typedef)
7872 : 1 : error_at (loc, "alignment specified for typedef %qE", name);
7873 : 187 : else if (storage_class == csc_register)
7874 : 1 : error_at (loc, "alignment specified for %<register%> object %qE",
7875 : : name);
7876 : 186 : else if (decl_context == PARM)
7877 : : {
7878 : 2 : if (name)
7879 : 1 : error_at (loc, "alignment specified for parameter %qE", name);
7880 : : else
7881 : 1 : error_at (loc, "alignment specified for unnamed parameter");
7882 : : }
7883 : 184 : else if (bitfield)
7884 : : {
7885 : 0 : if (name)
7886 : 0 : error_at (loc, "alignment specified for bit-field %qE", name);
7887 : : else
7888 : 0 : error_at (loc, "alignment specified for unnamed bit-field");
7889 : : }
7890 : 184 : else if (TREE_CODE (type) == FUNCTION_TYPE)
7891 : 1 : error_at (loc, "alignment specified for function %qE", name);
7892 : 183 : else if (declspecs->align_log != -1 && TYPE_P (type))
7893 : : {
7894 : 156 : alignas_align = 1U << declspecs->align_log;
7895 : 156 : if (alignas_align < min_align_of_type (type))
7896 : : {
7897 : 26 : if (name)
7898 : 25 : error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7899 : : "alignment of %qE", name);
7900 : : else
7901 : 1 : error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7902 : : "alignment of unnamed field");
7903 : : alignas_align = 0;
7904 : : }
7905 : : }
7906 : : }
7907 : :
7908 : : /* If this is declaring a typedef name, return a TYPE_DECL. */
7909 : :
7910 : 312350194 : if (storage_class == csc_typedef)
7911 : : {
7912 : 4279729 : tree decl;
7913 : 4279729 : if ((type_quals & TYPE_QUAL_ATOMIC)
7914 : 11187 : && TREE_CODE (type) == FUNCTION_TYPE)
7915 : : {
7916 : 0 : error_at (loc,
7917 : : "%<_Atomic%>-qualified function type");
7918 : 0 : type_quals &= ~TYPE_QUAL_ATOMIC;
7919 : : }
7920 : 4279729 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7921 : 609 : && type_quals)
7922 : 0 : pedwarn (loc, OPT_Wpedantic,
7923 : : "ISO C forbids qualified function types");
7924 : 4279120 : if (type_quals)
7925 : 29989 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7926 : : orig_qual_indirect);
7927 : 8559458 : decl = build_decl (declarator->id_loc,
7928 : 4279729 : TYPE_DECL, declarator->u.id.id, type);
7929 : 4279729 : if (declspecs->explicit_signed_p)
7930 : 352842 : C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7931 : 4279729 : if (declspecs->inline_p)
7932 : 4 : pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7933 : 4279729 : if (declspecs->noreturn_p)
7934 : 1 : pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7935 : :
7936 : 4279729 : if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7937 : : {
7938 : 1825 : struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7939 : :
7940 : 1825 : if (b != NULL
7941 : 40 : && b->decl != NULL_TREE
7942 : 40 : && (B_IN_CURRENT_SCOPE (b)
7943 : 4 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7944 : 1861 : && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7945 : : {
7946 : 7 : auto_diagnostic_group d;
7947 : 8 : if (warning_at (declarator->id_loc, OPT_Wc___compat,
7948 : : "using %qD as both a typedef and a tag is "
7949 : : "invalid in C++", decl)
7950 : 7 : && b->locus != UNKNOWN_LOCATION)
7951 : 6 : inform (b->locus, "originally defined here");
7952 : 7 : }
7953 : : }
7954 : :
7955 : 4279729 : return decl;
7956 : : }
7957 : :
7958 : : /* If this is a type name (such as, in a cast or sizeof),
7959 : : compute the type and return it now. */
7960 : :
7961 : 308070465 : if (decl_context == TYPENAME || decl_context == GENERIC_ASSOC)
7962 : : {
7963 : : /* Note that the grammar rejects storage classes in typenames
7964 : : and fields. */
7965 : 120861039 : gcc_assert (storage_class == csc_none && !threadp
7966 : : && !declspecs->inline_p && !declspecs->noreturn_p);
7967 : 120861039 : if ((type_quals & TYPE_QUAL_ATOMIC)
7968 : 257 : && TREE_CODE (type) == FUNCTION_TYPE)
7969 : : {
7970 : 0 : error_at (loc,
7971 : : "%<_Atomic%>-qualified function type");
7972 : 0 : type_quals &= ~TYPE_QUAL_ATOMIC;
7973 : : }
7974 : 120861039 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7975 : 22 : && type_quals)
7976 : 0 : pedwarn (loc, OPT_Wpedantic,
7977 : : "ISO C forbids const or volatile function types");
7978 : 120861017 : if (type_quals)
7979 : 879 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7980 : : orig_qual_indirect);
7981 : 120861039 : return type;
7982 : : }
7983 : :
7984 : 375115 : if (pedantic && decl_context == FIELD
7985 : 187234212 : && c_type_variably_modified_p (type))
7986 : : {
7987 : : /* C99 6.7.2.1p8 */
7988 : 7 : pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
7989 : : "have a variably modified type");
7990 : : }
7991 : :
7992 : : /* Aside from typedefs and type names (handle above),
7993 : : `void' at top level (not within pointer)
7994 : : is allowed only in public variables.
7995 : : We don't complain about parms either, but that is because
7996 : : a better error message can be made later. */
7997 : :
7998 : 187209426 : if (VOID_TYPE_P (type) && decl_context != PARM
7999 : 64 : && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
8000 : : && (storage_class == csc_extern
8001 : 27 : || (current_scope == file_scope
8002 : 18 : && !(storage_class == csc_static
8003 : : || storage_class == csc_register)))))
8004 : : {
8005 : 15 : error_at (loc, "variable or field %qE declared void", name);
8006 : 15 : type = integer_type_node;
8007 : : }
8008 : :
8009 : : /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
8010 : : or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
8011 : :
8012 : 186261406 : {
8013 : 186261406 : tree decl;
8014 : :
8015 : 186261406 : if (decl_context == PARM)
8016 : : {
8017 : 123368115 : tree promoted_type;
8018 : 123368115 : bool array_parameter_p = false;
8019 : :
8020 : : /* A parameter declared as an array of T is really a pointer to T.
8021 : : One declared as a function is really a pointer to a function. */
8022 : :
8023 : 123368115 : if (TREE_CODE (type) == ARRAY_TYPE)
8024 : : {
8025 : 434379 : if (!size_error)
8026 : 434365 : *decl_attrs = build_arg_spec_attribute (type, array_parm_static,
8027 : : *decl_attrs);
8028 : :
8029 : : /* Transfer const-ness of array into that of type pointed to. */
8030 : 434379 : type = TREE_TYPE (type);
8031 : 434379 : if (orig_qual_type != NULL_TREE)
8032 : : {
8033 : 7 : if (orig_qual_indirect == 0)
8034 : 5 : orig_qual_type = TREE_TYPE (orig_qual_type);
8035 : : else
8036 : 2 : orig_qual_indirect--;
8037 : : }
8038 : 434379 : if (type_quals)
8039 : 47755 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
8040 : : orig_qual_indirect);
8041 : :
8042 : : /* The pointed-to type may need a decl expr (see above). */
8043 : 434379 : if (!TYPE_NAME (type) && c_type_variably_modified_p (type))
8044 : : {
8045 : 373 : bool bind_p = decl_context == TYPENAME
8046 : : || decl_context == FIELD
8047 : : || decl_context == PARM;
8048 : 373 : add_decl_expr (loc, type, bind_p ? expr : NULL, true);
8049 : : }
8050 : :
8051 : 434379 : type = c_build_pointer_type (type);
8052 : 434379 : type_quals = array_ptr_quals;
8053 : 434379 : if (type_quals)
8054 : 932 : type = c_build_qualified_type (type, type_quals);
8055 : :
8056 : : /* We don't yet implement attributes in this context. */
8057 : 434379 : if (array_ptr_attrs != NULL_TREE)
8058 : 0 : warning_at (loc, OPT_Wattributes,
8059 : : "attributes in parameter array declarator ignored");
8060 : :
8061 : : size_varies = false;
8062 : : array_parameter_p = true;
8063 : : }
8064 : 122933736 : else if (TREE_CODE (type) == FUNCTION_TYPE)
8065 : : {
8066 : 301 : if (type_quals & TYPE_QUAL_ATOMIC)
8067 : : {
8068 : 1 : error_at (loc,
8069 : : "%<_Atomic%>-qualified function type");
8070 : 1 : type_quals &= ~TYPE_QUAL_ATOMIC;
8071 : : }
8072 : 300 : else if (type_quals)
8073 : 0 : pedwarn (loc, OPT_Wpedantic,
8074 : : "ISO C forbids qualified function types");
8075 : 1 : if (type_quals)
8076 : 0 : type = c_build_qualified_type (type, type_quals);
8077 : 301 : type = c_build_pointer_type (type);
8078 : 301 : type_quals = TYPE_UNQUALIFIED;
8079 : : }
8080 : 122933435 : else if (type_quals)
8081 : 9893929 : type = c_build_qualified_type (type, type_quals);
8082 : :
8083 : 246736230 : decl = build_decl (declarator->id_loc,
8084 : 123368115 : PARM_DECL, declarator->u.id.id, type);
8085 : 123368115 : if (size_varies)
8086 : 42 : C_DECL_VARIABLE_SIZE (decl) = 1;
8087 : 123368115 : C_ARRAY_PARAMETER (decl) = array_parameter_p;
8088 : :
8089 : : /* Compute the type actually passed in the parmlist,
8090 : : for the case where there is no prototype.
8091 : : (For example, shorts and chars are passed as ints.)
8092 : : When there is a prototype, this is overridden later. */
8093 : :
8094 : 123368115 : if (type == error_mark_node)
8095 : : promoted_type = type;
8096 : : else
8097 : 123368031 : promoted_type = c_type_promotes_to (type);
8098 : :
8099 : 123368115 : DECL_ARG_TYPE (decl) = promoted_type;
8100 : 123368115 : if (declspecs->inline_p)
8101 : 4 : pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
8102 : 123368115 : if (declspecs->noreturn_p)
8103 : 1 : pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
8104 : : }
8105 : 63841311 : else if (decl_context == FIELD)
8106 : : {
8107 : : /* Note that the grammar rejects storage classes in typenames
8108 : : and fields. */
8109 : 4281381 : gcc_assert (storage_class == csc_none && !threadp
8110 : : && !declspecs->inline_p && !declspecs->noreturn_p);
8111 : :
8112 : : /* Structure field. It may not be a function. */
8113 : :
8114 : 4281381 : if (TREE_CODE (type) == FUNCTION_TYPE)
8115 : : {
8116 : 0 : error_at (loc, "field %qE declared as a function", name);
8117 : 0 : type = c_build_pointer_type (type);
8118 : : }
8119 : 4281381 : else if (TREE_CODE (type) != ERROR_MARK
8120 : 4281381 : && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
8121 : : {
8122 : 24 : if (name)
8123 : 17 : error_at (loc, "field %qE has incomplete type", name);
8124 : : else
8125 : 7 : error_at (loc, "unnamed field has incomplete type");
8126 : 24 : type = error_mark_node;
8127 : : }
8128 : 4281357 : else if (TREE_CODE (type) == ARRAY_TYPE
8129 : 4281357 : && TYPE_DOMAIN (type) == NULL_TREE)
8130 : : {
8131 : : /* We have a flexible array member through a typedef.
8132 : : Set suitable range. Whether this is a correct position
8133 : : for a flexible array member will be determined elsewhere. */
8134 : 14 : if (!in_system_header_at (input_location))
8135 : 14 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
8136 : : "support flexible array members");
8137 : 14 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8138 : 14 : TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
8139 : : NULL_TREE);
8140 : 14 : if (orig_qual_indirect == 0)
8141 : 4281381 : orig_qual_type = NULL_TREE;
8142 : : }
8143 : 4281381 : if (type != error_mark_node
8144 : 4281381 : && !verify_type_context (loc, TCTX_FIELD, type))
8145 : 0 : type = error_mark_node;
8146 : :
8147 : 4281381 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
8148 : : orig_qual_indirect);
8149 : 8562762 : decl = build_decl (declarator->id_loc,
8150 : 4281381 : FIELD_DECL, declarator->u.id.id, type);
8151 : 4281381 : DECL_NONADDRESSABLE_P (decl) = bitfield;
8152 : 4281381 : if (bitfield && !declarator->u.id.id)
8153 : 9188 : DECL_PADDING_P (decl) = 1;
8154 : :
8155 : 4281381 : if (size_varies)
8156 : 648 : C_DECL_VARIABLE_SIZE (decl) = 1;
8157 : : }
8158 : 59559930 : else if (TREE_CODE (type) == FUNCTION_TYPE)
8159 : : {
8160 : 50666065 : if (storage_class == csc_register || threadp || constexprp)
8161 : : {
8162 : 12 : error_at (loc, "invalid storage class for function %qE", name);
8163 : : }
8164 : 50666053 : else if (current_scope != file_scope)
8165 : : {
8166 : : /* Function declaration not at file scope. Storage
8167 : : classes other than `extern' are not allowed, C99
8168 : : 6.7.1p5, and `extern' makes no difference. However,
8169 : : GCC allows 'auto', perhaps with 'inline', to support
8170 : : nested functions. */
8171 : 10546 : if (storage_class == csc_auto)
8172 : 66 : pedwarn (loc, OPT_Wpedantic,
8173 : : "invalid storage class for function %qE", name);
8174 : 10480 : else if (storage_class == csc_static)
8175 : : {
8176 : 20 : error_at (loc, "invalid storage class for function %qE", name);
8177 : 20 : if (funcdef_flag)
8178 : 8 : storage_class = declspecs->storage_class = csc_none;
8179 : : else
8180 : : return NULL_TREE;
8181 : : }
8182 : : }
8183 : :
8184 : 101332106 : decl = build_decl (declarator->id_loc,
8185 : 50666053 : FUNCTION_DECL, declarator->u.id.id, type);
8186 : 50666053 : decl = build_decl_attribute_variant (decl, decl_attr);
8187 : :
8188 : 50666053 : if (type_quals & TYPE_QUAL_ATOMIC)
8189 : : {
8190 : 2 : error_at (loc,
8191 : : "%<_Atomic%>-qualified function type");
8192 : 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
8193 : : }
8194 : 50666051 : else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
8195 : 9 : pedwarn (loc, OPT_Wpedantic,
8196 : : "ISO C forbids qualified function types");
8197 : :
8198 : : /* Every function declaration is an external reference
8199 : : (DECL_EXTERNAL) except for those which are not at file
8200 : : scope and are explicitly declared "auto". This is
8201 : : forbidden by standard C (C99 6.7.1p5) and is interpreted by
8202 : : GCC to signify a forward declaration of a nested function. */
8203 : 50666053 : if (storage_class == csc_auto && current_scope != file_scope)
8204 : 66 : DECL_EXTERNAL (decl) = 0;
8205 : : /* In C99, a function which is declared 'inline' with 'extern'
8206 : : is not an external reference (which is confusing). It
8207 : : means that the later definition of the function must be output
8208 : : in this file, C99 6.7.4p6. In GNU C89, a function declared
8209 : : 'extern inline' is an external reference. */
8210 : 50665987 : else if (declspecs->inline_p && storage_class != csc_static)
8211 : 35285294 : DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
8212 : 35285294 : == flag_gnu89_inline);
8213 : : else
8214 : 15380693 : DECL_EXTERNAL (decl) = !initialized;
8215 : :
8216 : : /* Record absence of global scope for `static' or `auto'. */
8217 : 50666053 : TREE_PUBLIC (decl)
8218 : 50666053 : = !(storage_class == csc_static || storage_class == csc_auto);
8219 : :
8220 : : /* For a function definition, record the argument information
8221 : : block where store_parm_decls will look for it. */
8222 : 50666053 : if (funcdef_flag)
8223 : 36132630 : current_function_arg_info = arg_info;
8224 : :
8225 : 50666053 : if (declspecs->default_int_p)
8226 : 9220 : C_FUNCTION_IMPLICIT_INT (decl) = 1;
8227 : :
8228 : : /* Record presence of `inline' and `_Noreturn', if it is
8229 : : reasonable. */
8230 : 50666053 : if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
8231 : : {
8232 : 48824 : if (declspecs->inline_p)
8233 : 5 : pedwarn (loc, 0, "cannot inline function %<main%>");
8234 : 48824 : if (declspecs->noreturn_p)
8235 : 1 : pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
8236 : : }
8237 : : else
8238 : : {
8239 : 50617229 : if (declspecs->inline_p)
8240 : : /* Record that the function is declared `inline'. */
8241 : 35444120 : DECL_DECLARED_INLINE_P (decl) = 1;
8242 : 50617229 : if (declspecs->noreturn_p)
8243 : : {
8244 : 22987 : if (flag_isoc99)
8245 : 22984 : pedwarn_c99 (loc, OPT_Wpedantic,
8246 : : "ISO C99 does not support %<_Noreturn%>");
8247 : : else
8248 : 3 : pedwarn_c99 (loc, OPT_Wpedantic,
8249 : : "ISO C90 does not support %<_Noreturn%>");
8250 : 22987 : TREE_THIS_VOLATILE (decl) = 1;
8251 : : }
8252 : : }
8253 : :
8254 : : /* C99 6.2.2p7: It is invalid (compile-time undefined
8255 : : behavior) to create an 'extern' declaration for a
8256 : : function if there is a global declaration that is
8257 : : 'static' and the global declaration is not visible.
8258 : : (If the static declaration _is_ currently visible,
8259 : : the 'extern' declaration is taken to refer to that decl.) */
8260 : 50666053 : if (!initialized
8261 : 14533414 : && TREE_PUBLIC (decl)
8262 : 14525536 : && current_scope != file_scope)
8263 : : {
8264 : 8888 : tree global_decl = identifier_global_value (declarator->u.id.id);
8265 : 8888 : tree visible_decl = lookup_name (declarator->u.id.id);
8266 : :
8267 : 8888 : if (global_decl
8268 : 8888 : && global_decl != visible_decl
8269 : 1713 : && VAR_OR_FUNCTION_DECL_P (global_decl)
8270 : 1713 : && !TREE_PUBLIC (global_decl))
8271 : 2 : error_at (loc, "function previously declared %<static%> "
8272 : : "redeclared %<extern%>");
8273 : : }
8274 : : }
8275 : : else
8276 : : {
8277 : : /* It's a variable. */
8278 : : /* An uninitialized decl with `extern' is a reference. */
8279 : 8893865 : int extern_ref = !initialized && storage_class == csc_extern;
8280 : :
8281 : 8893865 : if (constexprp)
8282 : : {
8283 : : /* The type of a constexpr variable must not be variably
8284 : : modified, volatile, atomic or restrict qualified or
8285 : : have a member with such a qualifier. const
8286 : : qualification is implicitly added, and, at file scope,
8287 : : has internal linkage. */
8288 : 352 : if (c_type_variably_modified_p (type))
8289 : 1 : error_at (loc, "%<constexpr%> object has variably modified "
8290 : : "type");
8291 : 352 : if (type_quals
8292 : 352 : & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
8293 : 9 : error_at (loc, "invalid qualifiers for %<constexpr%> object");
8294 : : else
8295 : : {
8296 : 343 : tree type_no_array = strip_array_types (type);
8297 : 343 : if (RECORD_OR_UNION_TYPE_P (type_no_array)
8298 : 343 : && C_TYPE_FIELDS_NON_CONSTEXPR (type_no_array))
8299 : 8 : error_at (loc, "invalid qualifiers for field of "
8300 : : "%<constexpr%> object");
8301 : : }
8302 : 352 : type_quals |= TYPE_QUAL_CONST;
8303 : 352 : if (current_scope == file_scope)
8304 : 294 : storage_class = csc_static;
8305 : : }
8306 : :
8307 : 8893865 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
8308 : : orig_qual_indirect);
8309 : :
8310 : : /* C99 6.2.2p7: It is invalid (compile-time undefined
8311 : : behavior) to create an 'extern' declaration for a
8312 : : variable if there is a global declaration that is
8313 : : 'static' and the global declaration is not visible.
8314 : : (If the static declaration _is_ currently visible,
8315 : : the 'extern' declaration is taken to refer to that decl.) */
8316 : 8893865 : if (extern_ref && current_scope != file_scope)
8317 : : {
8318 : 1569 : tree global_decl = identifier_global_value (declarator->u.id.id);
8319 : 1569 : tree visible_decl = lookup_name (declarator->u.id.id);
8320 : :
8321 : 1569 : if (global_decl
8322 : 1569 : && global_decl != visible_decl
8323 : 282 : && VAR_P (global_decl)
8324 : 282 : && !TREE_PUBLIC (global_decl))
8325 : 8 : error_at (loc, "variable previously declared %<static%> "
8326 : : "redeclared %<extern%>");
8327 : : }
8328 : :
8329 : 17787730 : decl = build_decl (declarator->id_loc,
8330 : 8893865 : VAR_DECL, declarator->u.id.id, type);
8331 : 8893865 : if (size_varies)
8332 : 7417 : C_DECL_VARIABLE_SIZE (decl) = 1;
8333 : 8893865 : if (constexprp)
8334 : 352 : C_DECL_DECLARED_CONSTEXPR (decl) = 1;
8335 : :
8336 : 8893865 : if (declspecs->inline_p)
8337 : 4 : pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
8338 : 8893865 : if (declspecs->noreturn_p)
8339 : 1 : pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
8340 : :
8341 : : /* At file scope, an initialized extern declaration may follow
8342 : : a static declaration. In that case, DECL_EXTERNAL will be
8343 : : reset later in start_decl. */
8344 : 8893865 : DECL_EXTERNAL (decl) = (storage_class == csc_extern);
8345 : :
8346 : : /* At file scope, the presence of a `static' or `register' storage
8347 : : class specifier, or the absence of all storage class specifiers
8348 : : makes this declaration a definition (perhaps tentative). Also,
8349 : : the absence of `static' makes it public. */
8350 : 8893865 : if (current_scope == file_scope)
8351 : : {
8352 : 1151906 : TREE_PUBLIC (decl) = storage_class != csc_static;
8353 : 1151906 : TREE_STATIC (decl) = !extern_ref;
8354 : : }
8355 : : /* Not at file scope, only `static' makes a static definition. */
8356 : : else
8357 : : {
8358 : 7741959 : TREE_STATIC (decl) = (storage_class == csc_static);
8359 : 7741959 : TREE_PUBLIC (decl) = extern_ref;
8360 : : }
8361 : :
8362 : : // NB: Set a tentative TLS model to avoid tls_model attribute
8363 : : // warnings due to lack of thread storage duration. It will
8364 : : // be updated by c_decl_attributes later.
8365 : 8893865 : if (threadp)
8366 : 2840 : set_decl_tls_model (decl, TLS_MODEL_REAL);
8367 : : }
8368 : :
8369 : 187209414 : if ((storage_class == csc_extern
8370 : 137532540 : || (storage_class == csc_none
8371 : 137072951 : && TREE_CODE (type) == FUNCTION_TYPE
8372 : 803397 : && !funcdef_flag))
8373 : 187514329 : && c_type_variably_modified_p (type))
8374 : : {
8375 : : /* C99 6.7.5.2p2 */
8376 : 6 : if (TREE_CODE (type) == FUNCTION_TYPE)
8377 : 4 : error_at (loc, "non-nested function with variably modified type");
8378 : : else
8379 : 2 : error_at (loc, "object with variably modified type must have "
8380 : : "no linkage");
8381 : : }
8382 : :
8383 : : /* For nested functions disqualify ones taking VLAs by value
8384 : : from inlining since the middle-end cannot deal with this.
8385 : : ??? We should arrange for those to be passed by reference
8386 : : with emitting the copy on the caller side in the frontend. */
8387 : 187209414 : if (storage_class == csc_none
8388 : 137072951 : && TREE_CODE (type) == FUNCTION_TYPE)
8389 : 4165716 : for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
8390 : : {
8391 : 3362359 : tree arg = TREE_VALUE (al);
8392 : 3362359 : if (arg != error_mark_node
8393 : 3362359 : && C_TYPE_VARIABLE_SIZE (arg))
8394 : : {
8395 : 40 : DECL_UNINLINABLE (decl) = 1;
8396 : 40 : break;
8397 : : }
8398 : : }
8399 : :
8400 : : /* Record `register' declaration for warnings on &
8401 : : and in case doing stupid register allocation. */
8402 : :
8403 : 187209414 : if (storage_class == csc_register
8404 : 3593 : && TREE_CODE (type) != FUNCTION_TYPE)
8405 : : {
8406 : 3587 : C_DECL_REGISTER (decl) = 1;
8407 : 3587 : DECL_REGISTER (decl) = 1;
8408 : : }
8409 : :
8410 : : /* Record constancy and volatility. */
8411 : 187209414 : c_apply_type_quals_to_decl (type_quals, decl);
8412 : :
8413 : : /* Apply _Alignas specifiers. */
8414 : 187209414 : if (alignas_align)
8415 : : {
8416 : 123 : SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
8417 : 123 : DECL_USER_ALIGN (decl) = 1;
8418 : : }
8419 : :
8420 : : /* If a type has volatile components, it should be stored in memory.
8421 : : Otherwise, the fact that those components are volatile
8422 : : will be ignored, and would even crash the compiler.
8423 : : Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
8424 : 187209414 : if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
8425 : 187209414 : && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
8426 : : || TREE_CODE (decl) == RESULT_DECL))
8427 : : {
8428 : : /* It is not an error for a structure with volatile fields to
8429 : : be declared register, but reset DECL_REGISTER since it
8430 : : cannot actually go in a register. */
8431 : 184 : int was_reg = C_DECL_REGISTER (decl);
8432 : 184 : C_DECL_REGISTER (decl) = 0;
8433 : 184 : DECL_REGISTER (decl) = 0;
8434 : 184 : c_mark_addressable (decl);
8435 : 184 : C_DECL_REGISTER (decl) = was_reg;
8436 : : }
8437 : :
8438 : : /* This is the earliest point at which we might know the assembler
8439 : : name of a variable. Thus, if it's known before this, die horribly. */
8440 : 187209414 : gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
8441 : : || !DECL_ASSEMBLER_NAME_SET_P (decl));
8442 : :
8443 : 187209414 : if (warn_cxx_compat
8444 : 24521 : && VAR_P (decl)
8445 : 7545 : && TREE_PUBLIC (decl)
8446 : 2802 : && TREE_STATIC (decl)
8447 : 2353 : && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
8448 : 2221 : || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
8449 : 187209561 : && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
8450 : 4 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
8451 : : "non-local variable %qD with anonymous type is "
8452 : : "questionable in C++", decl);
8453 : :
8454 : : return decl;
8455 : : }
8456 : : }
8457 : :
8458 : : /* Decode the parameter-list info for a function type or function definition.
8459 : : The argument is the value returned by `get_parm_info' (or made in c-parse.c
8460 : : if there is an identifier list instead of a parameter decl list).
8461 : : These two functions are separate because when a function returns
8462 : : or receives functions then each is called multiple times but the order
8463 : : of calls is different. The last call to `grokparms' is always the one
8464 : : that contains the formal parameter names of a function definition.
8465 : :
8466 : : Return a list of arg types to use in the FUNCTION_TYPE for this function.
8467 : :
8468 : : FUNCDEF_FLAG is true for a function definition, false for
8469 : : a mere declaration. A nonempty identifier-list gets an error message
8470 : : when FUNCDEF_FLAG is false. */
8471 : :
8472 : : static tree
8473 : 50247873 : grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
8474 : : {
8475 : 50247873 : tree arg_types = arg_info->types;
8476 : :
8477 : 50247873 : if (funcdef_flag && arg_info->had_vla_unspec)
8478 : : {
8479 : : /* A function definition isn't function prototype scope C99 6.2.1p4. */
8480 : : /* C99 6.7.5.2p4 */
8481 : 2 : error ("%<[*]%> not allowed in other than function prototype scope");
8482 : : }
8483 : :
8484 : 715512 : if (arg_types == NULL_TREE && !funcdef_flag && !flag_isoc23
8485 : 50250913 : && !in_system_header_at (input_location))
8486 : 3040 : warning (OPT_Wstrict_prototypes,
8487 : : "function declaration isn%'t a prototype");
8488 : :
8489 : 50247873 : if (arg_types == error_mark_node)
8490 : : /* Don't set TYPE_ARG_TYPES in this case. */
8491 : : return NULL_TREE;
8492 : :
8493 : 99723014 : else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
8494 : : {
8495 : 8684 : if (!funcdef_flag)
8496 : : {
8497 : 13 : permerror_opt (input_location,
8498 : 13 : OPT_Wdeclaration_missing_parameter_type,
8499 : : "parameter names (without types) in "
8500 : : "function declaration");
8501 : 13 : arg_info->parms = NULL_TREE;
8502 : : }
8503 : : else
8504 : 8671 : arg_info->parms = arg_info->types;
8505 : :
8506 : 8684 : arg_info->types = NULL_TREE;
8507 : 8684 : return NULL_TREE;
8508 : : }
8509 : : else
8510 : : {
8511 : 50239189 : tree parm, type, typelt;
8512 : 50239189 : unsigned int parmno;
8513 : :
8514 : : /* In C23, convert () to (void). */
8515 : 50239189 : if (flag_isoc23
8516 : 41194885 : && !arg_types
8517 : 765256 : && !arg_info->parms
8518 : 765256 : && !arg_info->no_named_args_stdarg_p)
8519 : : {
8520 : 765115 : arg_types = arg_info->types = void_list_node;
8521 : 765115 : arg_info->c23_empty_parens = 1;
8522 : : }
8523 : :
8524 : : /* If there is a parameter of incomplete type in a definition,
8525 : : this is an error. In a declaration this is valid, and a
8526 : : struct or union type may be completed later, before any calls
8527 : : or definition of the function. In the case where the tag was
8528 : : first declared within the parameter list, a warning has
8529 : : already been given. If a parameter has void type, then
8530 : : this has already received an error (constraint violation in C2Y,
8531 : : previously implicitly undefined behavior). */
8532 : :
8533 : 50239189 : for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
8534 : 172636456 : parm;
8535 : 122397267 : parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
8536 : : {
8537 : 122397267 : type = TREE_VALUE (typelt);
8538 : 122397267 : if (type == error_mark_node)
8539 : 61 : continue;
8540 : :
8541 : 122397206 : if (!COMPLETE_TYPE_P (type))
8542 : : {
8543 : 40 : if (funcdef_flag)
8544 : : {
8545 : 13 : if (DECL_NAME (parm))
8546 : 13 : error_at (input_location,
8547 : : "parameter %u (%q+D) has incomplete type",
8548 : : parmno, parm);
8549 : : else
8550 : 0 : error_at (DECL_SOURCE_LOCATION (parm),
8551 : : "parameter %u has incomplete type",
8552 : : parmno);
8553 : :
8554 : 13 : TREE_VALUE (typelt) = error_mark_node;
8555 : 13 : TREE_TYPE (parm) = error_mark_node;
8556 : 13 : arg_types = NULL_TREE;
8557 : : }
8558 : : }
8559 : :
8560 : 122397206 : if (DECL_NAME (parm) && TREE_USED (parm))
8561 : 17502 : warn_if_shadowing (parm);
8562 : : }
8563 : : return arg_types;
8564 : : }
8565 : : }
8566 : :
8567 : : /* Allocate and initialize a c_arg_info structure from the parser's
8568 : : obstack. */
8569 : :
8570 : : struct c_arg_info *
8571 : 50247891 : build_arg_info (void)
8572 : : {
8573 : 50247891 : struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
8574 : 50247891 : ret->parms = NULL_TREE;
8575 : 50247891 : ret->tags = NULL;
8576 : 50247891 : ret->types = NULL_TREE;
8577 : 50247891 : ret->others = NULL_TREE;
8578 : 50247891 : ret->pending_sizes = NULL;
8579 : 50247891 : ret->had_vla_unspec = 0;
8580 : 50247891 : ret->no_named_args_stdarg_p = 0;
8581 : 50247891 : ret->c23_empty_parens = 0;
8582 : 50247891 : return ret;
8583 : : }
8584 : :
8585 : : /* Take apart the current scope and return a c_arg_info structure with
8586 : : info on a parameter list just parsed.
8587 : :
8588 : : This structure is later fed to 'grokparms' and 'store_parm_decls'.
8589 : :
8590 : : ELLIPSIS being true means the argument list ended in '...' so don't
8591 : : append a sentinel (void_list_node) to the end of the type-list.
8592 : :
8593 : : EXPR is NULL or an expression that needs to be evaluated for the
8594 : : side effects of array size expressions in the parameters. */
8595 : :
8596 : : struct c_arg_info *
8597 : 49466486 : get_parm_info (bool ellipsis, tree expr)
8598 : : {
8599 : 49466486 : struct c_binding *b = current_scope->bindings;
8600 : 49466486 : struct c_arg_info *arg_info = build_arg_info ();
8601 : :
8602 : 49466486 : tree parms = NULL_TREE;
8603 : 49466486 : vec<c_arg_tag, va_gc> *tags = NULL;
8604 : 49466486 : tree types = NULL_TREE;
8605 : 49466486 : tree others = NULL_TREE;
8606 : :
8607 : 49466486 : bool gave_void_only_once_err = false;
8608 : :
8609 : 49466486 : arg_info->had_vla_unspec = current_scope->had_vla_unspec;
8610 : :
8611 : : /* The bindings in this scope must not get put into a block.
8612 : : We will take care of deleting the binding nodes. */
8613 : 49466486 : current_scope->bindings = 0;
8614 : :
8615 : : /* This function is only called if there was *something* on the
8616 : : parameter list. */
8617 : 49466486 : gcc_assert (b);
8618 : :
8619 : : /* A parameter list consisting solely of 'void' indicates that the
8620 : : function takes no arguments. But if the 'void' is qualified
8621 : : (by 'const' or 'volatile'), or has a storage class specifier
8622 : : ('register'), then the behavior is undefined; issue an error.
8623 : : Typedefs for 'void' are OK (see DR#157). */
8624 : 49466486 : if (b->prev == 0 /* one binding */
8625 : 13132500 : && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
8626 : 13132500 : && !DECL_NAME (b->decl) /* anonymous */
8627 : 51261826 : && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
8628 : : {
8629 : 947906 : if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
8630 : 947906 : || C_DECL_REGISTER (b->decl))
8631 : 96 : error_at (b->locus, "%<void%> as only parameter may not be qualified");
8632 : :
8633 : : /* There cannot be an ellipsis. */
8634 : 947906 : if (ellipsis)
8635 : 17 : error_at (b->locus, "%<void%> must be the only parameter");
8636 : :
8637 : 947906 : arg_info->types = void_list_node;
8638 : 947906 : return arg_info;
8639 : : }
8640 : :
8641 : 48518580 : if (!ellipsis)
8642 : 48306770 : types = void_list_node;
8643 : :
8644 : : /* Break up the bindings list into parms, tags, types, and others;
8645 : : apply sanity checks; purge the name-to-decl bindings. */
8646 : 170916294 : while (b)
8647 : : {
8648 : 122397714 : tree decl = b->decl;
8649 : 122397714 : tree type = TREE_TYPE (decl);
8650 : 122397714 : c_arg_tag tag;
8651 : 122397714 : const char *keyword;
8652 : :
8653 : 122397714 : switch (TREE_CODE (decl))
8654 : : {
8655 : 122397390 : case PARM_DECL:
8656 : 122397390 : if (b->id)
8657 : : {
8658 : 118821150 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8659 : 118821150 : I_SYMBOL_BINDING (b->id) = b->shadowed;
8660 : : }
8661 : :
8662 : : /* Check for forward decls that never got their actual decl. */
8663 : 122397390 : if (TREE_ASM_WRITTEN (decl))
8664 : 4 : error_at (b->locus,
8665 : : "parameter %q+D has just a forward declaration", decl);
8666 : : /* Check for (..., void, ...) and named void parameters and issue an
8667 : : error. */
8668 : 122397386 : else if (VOID_TYPE_P (type))
8669 : : {
8670 : 114 : if (!gave_void_only_once_err)
8671 : : {
8672 : 114 : error_at (b->locus,
8673 : : "%<void%> must be the only parameter and unnamed");
8674 : 114 : gave_void_only_once_err = true;
8675 : : }
8676 : : }
8677 : : else
8678 : : {
8679 : : /* Valid parameter, add it to the list. */
8680 : 122397272 : DECL_CHAIN (decl) = parms;
8681 : 122397272 : parms = decl;
8682 : :
8683 : : /* Since there is a prototype, args are passed in their
8684 : : declared types. The back end may override this later. */
8685 : 122397272 : DECL_ARG_TYPE (decl) = type;
8686 : 122397272 : types = tree_cons (0, type, types);
8687 : : }
8688 : : break;
8689 : :
8690 : 21 : case ENUMERAL_TYPE: keyword = "enum"; goto tag;
8691 : 19 : case UNION_TYPE: keyword = "union"; goto tag;
8692 : 94 : case RECORD_TYPE: keyword = "struct"; goto tag;
8693 : 134 : tag:
8694 : : /* Types may not have tag-names, in which case the type
8695 : : appears in the bindings list with b->id NULL. */
8696 : 134 : if (b->id)
8697 : : {
8698 : 92 : gcc_assert (I_TAG_BINDING (b->id) == b);
8699 : 92 : I_TAG_BINDING (b->id) = b->shadowed;
8700 : : }
8701 : :
8702 : : /* Warn about any struct, union or enum tags defined in a
8703 : : parameter list. The scope of such types is limited to
8704 : : the parameter list, which is rarely if ever desirable
8705 : : (it's impossible to call such a function with type-
8706 : : correct arguments). An anonymous union parm type is
8707 : : meaningful as a GNU extension, so don't warn for that. */
8708 : 134 : if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
8709 : : {
8710 : 115 : if (b->id)
8711 : : {
8712 : : /* The %s will be one of 'struct', 'union', or 'enum'. */
8713 : 92 : if (!flag_isoc23 || !COMPLETE_TYPE_P (decl))
8714 : 72 : warning_at (b->locus, 0,
8715 : : "%<%s %E%> declared inside parameter list"
8716 : : " will not be visible outside of this definition or"
8717 : : " declaration", keyword, b->id);
8718 : : }
8719 : : else
8720 : : /* The %s will be one of 'struct', 'union', or 'enum'. */
8721 : 23 : warning_at (b->locus, 0,
8722 : : "anonymous %s declared inside parameter list"
8723 : : " will not be visible outside of this definition or"
8724 : : " declaration", keyword);
8725 : : }
8726 : :
8727 : 134 : tag.id = b->id;
8728 : 134 : tag.type = decl;
8729 : 134 : vec_safe_push (tags, tag);
8730 : 134 : break;
8731 : :
8732 : 20 : case FUNCTION_DECL:
8733 : : /* FUNCTION_DECLs appear when there is an implicit function
8734 : : declaration in the parameter list. */
8735 : 20 : gcc_assert (b->nested || seen_error ());
8736 : 20 : goto set_shadowed;
8737 : :
8738 : 136 : case CONST_DECL:
8739 : 136 : case TYPE_DECL:
8740 : : /* CONST_DECLs appear here when we have an embedded enum,
8741 : : and TYPE_DECLs appear here when we have an embedded struct
8742 : : or union. No warnings for this - we already warned about the
8743 : : type itself. */
8744 : :
8745 : : /* When we reinsert this decl in the function body, we need
8746 : : to reconstruct whether it was marked as nested. */
8747 : 136 : gcc_assert (!b->nested);
8748 : 136 : DECL_CHAIN (decl) = others;
8749 : 136 : others = decl;
8750 : : /* fall through */
8751 : :
8752 : 190 : case ERROR_MARK:
8753 : 190 : set_shadowed:
8754 : : /* error_mark_node appears here when we have an undeclared
8755 : : variable. Just throw it away. */
8756 : 190 : if (b->id)
8757 : : {
8758 : 56 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8759 : 56 : I_SYMBOL_BINDING (b->id) = b->shadowed;
8760 : : }
8761 : : break;
8762 : :
8763 : : /* Other things that might be encountered. */
8764 : 0 : case LABEL_DECL:
8765 : 0 : case VAR_DECL:
8766 : 0 : default:
8767 : 0 : gcc_unreachable ();
8768 : : }
8769 : :
8770 : 122397714 : b = free_binding_and_advance (b);
8771 : : }
8772 : :
8773 : 48518580 : arg_info->parms = parms;
8774 : 48518580 : arg_info->tags = tags;
8775 : 48518580 : arg_info->types = types;
8776 : 48518580 : arg_info->others = others;
8777 : 48518580 : arg_info->pending_sizes = expr;
8778 : 48518580 : arg_info->no_named_args_stdarg_p = ellipsis && !types;
8779 : 48518580 : return arg_info;
8780 : : }
8781 : :
8782 : : /* Get the struct, enum or union (CODE says which) with tag NAME.
8783 : : Define the tag as a forward-reference with location LOC if it is
8784 : : not defined. HAVE_STD_ATTRS says whether any standard attributes
8785 : : were present after the struct, union or enum keyword; ATTRS are the
8786 : : standard attributes present there. HAS_ENUM_TYPE_SPECIFIER says
8787 : : whether an enum type specifier (": specifier-qualifier-list") is
8788 : : present; if so, this is called before that specifier is parsed, so
8789 : : that the tag is in scope for that specifier. Return a c_typespec
8790 : : structure for the type specifier. */
8791 : :
8792 : : struct c_typespec
8793 : 1076212 : parser_xref_tag (location_t loc, enum tree_code code, tree name,
8794 : : bool have_std_attrs, tree attrs, bool has_enum_type_specifier)
8795 : : {
8796 : 1076212 : struct c_typespec ret;
8797 : 1076212 : tree ref;
8798 : 1076212 : location_t refloc;
8799 : :
8800 : 1076212 : ret.expr = NULL_TREE;
8801 : 1076212 : ret.expr_const_operands = true;
8802 : 1076212 : ret.has_enum_type_specifier = has_enum_type_specifier;
8803 : :
8804 : : /* If a cross reference is requested, look up the type already
8805 : : defined for this tag and return it. If an enum type specifier is
8806 : : present, only a definition in the current scope is relevant. */
8807 : :
8808 : 1076212 : ref = lookup_tag (code, name, has_enum_type_specifier, &refloc);
8809 : :
8810 : : /* If the visble type is still being defined, see if there is
8811 : : an earlier definition (which may be complete). We do not
8812 : : have to loop because nested redefinitions are not allowed. */
8813 : 1076212 : if (flag_isoc23 && ref && C_TYPE_BEING_DEFINED (ref))
8814 : : {
8815 : 85232 : tree vis = previous_tag (ref);
8816 : 85232 : if (vis)
8817 : 16 : ref = vis;
8818 : : }
8819 : :
8820 : : /* If this is the right type of tag, return what we found.
8821 : : (This reference will be shadowed by shadow_tag later if appropriate.)
8822 : : If this is the wrong type of tag, do not return it. If it was the
8823 : : wrong type in the same scope, we will have had an error
8824 : : message already; if in a different scope and declaring
8825 : : a name, pending_xref_error will give an error message; but if in a
8826 : : different scope and not declaring a name, this tag should
8827 : : shadow the previous declaration of a different type of tag, and
8828 : : this would not work properly if we return the reference found.
8829 : : (For example, with "struct foo" in an outer scope, "union foo;"
8830 : : must shadow that tag with a new one of union type.) */
8831 : 2152424 : ret.kind = (ref
8832 : 1076212 : ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
8833 : 94211 : : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
8834 : 1076212 : if (ref && TREE_CODE (ref) == code)
8835 : : {
8836 : 981967 : decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8837 : 981967 : if (C_TYPE_DEFINED_IN_STRUCT (ref)
8838 : 5 : && loc != UNKNOWN_LOCATION
8839 : 981972 : && warn_cxx_compat)
8840 : : {
8841 : 5 : auto_diagnostic_group d;
8842 : 5 : switch (code)
8843 : : {
8844 : 2 : case ENUMERAL_TYPE:
8845 : 2 : if (warning_at (loc, OPT_Wc___compat,
8846 : : ("enum type defined in struct or union "
8847 : : "is not visible in C++")))
8848 : 2 : inform (refloc, "enum type defined here");
8849 : : break;
8850 : 2 : case RECORD_TYPE:
8851 : 2 : if (warning_at (loc, OPT_Wc___compat,
8852 : : ("struct defined in struct or union "
8853 : : "is not visible in C++")))
8854 : 2 : inform (refloc, "struct defined here");
8855 : : break;
8856 : 1 : case UNION_TYPE:
8857 : 1 : if (warning_at (loc, OPT_Wc___compat,
8858 : : ("union defined in struct or union "
8859 : : "is not visible in C++")))
8860 : 1 : inform (refloc, "union defined here");
8861 : : break;
8862 : 0 : default:
8863 : 0 : gcc_unreachable();
8864 : : }
8865 : 5 : }
8866 : :
8867 : 981967 : ret.spec = ref;
8868 : 981967 : return ret;
8869 : : }
8870 : :
8871 : : /* If no such tag is yet defined, create a forward-reference node
8872 : : and record it as the "definition".
8873 : : When a real declaration of this type is found,
8874 : : the forward-reference will be altered into a real type. */
8875 : :
8876 : 94245 : ref = make_node (code);
8877 : 94245 : if (flag_isoc23 || code == ENUMERAL_TYPE)
8878 : 72031 : SET_TYPE_STRUCTURAL_EQUALITY (ref);
8879 : 72031 : if (code == ENUMERAL_TYPE)
8880 : : {
8881 : : /* Give the type a default layout like unsigned int
8882 : : to avoid crashing if it does not get defined. */
8883 : 249 : SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
8884 : 249 : SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
8885 : 249 : TYPE_USER_ALIGN (ref) = 0;
8886 : 249 : TYPE_UNSIGNED (ref) = 1;
8887 : 249 : TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
8888 : 249 : TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
8889 : 249 : TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
8890 : 249 : ENUM_FIXED_UNDERLYING_TYPE_P (ref) = has_enum_type_specifier;
8891 : : }
8892 : :
8893 : 94245 : pushtag (loc, name, ref);
8894 : 94245 : decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8895 : 94245 : if (in_underspecified_init)
8896 : 9 : error_at (loc, "%qT declared in underspecified object initializer",
8897 : : ref);
8898 : :
8899 : 94245 : ret.spec = ref;
8900 : 94245 : return ret;
8901 : : }
8902 : :
8903 : : /* Get the struct, enum or union (CODE says which) with tag NAME.
8904 : : Define the tag as a forward-reference if it is not defined.
8905 : : Return a tree for the type. */
8906 : :
8907 : : tree
8908 : 0 : xref_tag (enum tree_code code, tree name)
8909 : : {
8910 : 0 : return parser_xref_tag (input_location, code, name, false, NULL_TREE,
8911 : 0 : false).spec;
8912 : : }
8913 : :
8914 : : /* Make sure that the tag NAME is defined *in the current scope*
8915 : : at least as a forward reference.
8916 : : LOC is the location of the struct's definition.
8917 : : CODE says which kind of tag NAME ought to be.
8918 : :
8919 : : This stores the current value of the file static STRUCT_PARSE_INFO
8920 : : in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8921 : : new c_struct_parse_info structure. The old value of
8922 : : STRUCT_PARSE_INFO is restored in finish_struct. */
8923 : :
8924 : : tree
8925 : 1168127 : start_struct (location_t loc, enum tree_code code, tree name,
8926 : : class c_struct_parse_info **enclosing_struct_parse_info)
8927 : : {
8928 : : /* If there is already a tag defined at this scope
8929 : : (as a forward reference), just return it. */
8930 : :
8931 : 1168127 : tree ref = NULL_TREE;
8932 : 1168127 : location_t refloc = UNKNOWN_LOCATION;
8933 : :
8934 : 1168127 : if (name != NULL_TREE)
8935 : 480618 : ref = lookup_tag (code, name, true, &refloc);
8936 : :
8937 : : /* For C23, even if we already have a completed definition,
8938 : : we do not use it. We will check for consistency later.
8939 : : If we are in a nested redefinition the type is not
8940 : : complete. We will then detect this below. */
8941 : 1168127 : if (flag_isoc23 && ref && TYPE_SIZE (ref))
8942 : : ref = NULL_TREE;
8943 : :
8944 : 1168022 : if (ref && TREE_CODE (ref) == code)
8945 : : {
8946 : 24964 : if (TYPE_STUB_DECL (ref))
8947 : 24964 : refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8948 : :
8949 : 24964 : if (TYPE_SIZE (ref))
8950 : : {
8951 : 11 : auto_diagnostic_group d;
8952 : 11 : if (code == UNION_TYPE)
8953 : 2 : error_at (loc, "redefinition of %<union %E%>", name);
8954 : : else
8955 : 9 : error_at (loc, "redefinition of %<struct %E%>", name);
8956 : 11 : if (refloc != UNKNOWN_LOCATION)
8957 : 11 : inform (refloc, "originally defined here");
8958 : : /* Don't create structures using a name already in use. */
8959 : 11 : ref = NULL_TREE;
8960 : 11 : }
8961 : 24953 : else if (C_TYPE_BEING_DEFINED (ref))
8962 : : {
8963 : 17 : if (code == UNION_TYPE)
8964 : 1 : error_at (loc, "nested redefinition of %<union %E%>", name);
8965 : : else
8966 : 16 : error_at (loc, "nested redefinition of %<struct %E%>", name);
8967 : : /* Don't bother to report "originally defined here" for a
8968 : : nested redefinition; the original definition should be
8969 : : obvious. */
8970 : : /* Don't create structures that contain themselves. */
8971 : : ref = NULL_TREE;
8972 : : }
8973 : : }
8974 : :
8975 : : /* Otherwise create a forward-reference just so the tag is in scope. */
8976 : :
8977 : 24950 : if (ref == NULL_TREE || TREE_CODE (ref) != code)
8978 : : {
8979 : 1143191 : ref = make_node (code);
8980 : 1143191 : if (flag_isoc23)
8981 : 914777 : SET_TYPE_STRUCTURAL_EQUALITY (ref);
8982 : 1143191 : pushtag (loc, name, ref);
8983 : : }
8984 : :
8985 : 1168127 : C_TYPE_BEING_DEFINED (ref) = 1;
8986 : 2366625 : for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
8987 : 1198498 : TYPE_PACKED (v) = flag_pack_struct;
8988 : :
8989 : 1168127 : *enclosing_struct_parse_info = struct_parse_info;
8990 : 1168127 : struct_parse_info = new c_struct_parse_info ();
8991 : 1168127 : struct_parse_info->refloc = refloc;
8992 : :
8993 : : /* FIXME: This will issue a warning for a use of a type defined
8994 : : within a statement expr used within sizeof, et. al. This is not
8995 : : terribly serious as C++ doesn't permit statement exprs within
8996 : : sizeof anyhow. */
8997 : 1168127 : if (warn_cxx_compat
8998 : 574 : && (in_sizeof || in_typeof || in_alignof || in_countof))
8999 : 10 : warning_at (loc, OPT_Wc___compat,
9000 : : "defining type in %qs expression is invalid in C++",
9001 : : (in_sizeof ? "sizeof"
9002 : 4 : : in_typeof ? "typeof"
9003 : 1 : : in_alignof ? "alignof"
9004 : : : "_Countof"));
9005 : :
9006 : 1168127 : if (in_underspecified_init)
9007 : 18 : error_at (loc, "%qT defined in underspecified object initializer", ref);
9008 : :
9009 : 1168127 : return ref;
9010 : : }
9011 : :
9012 : : /* Process the specs, declarator and width (NULL if omitted)
9013 : : of a structure component, returning a FIELD_DECL node.
9014 : : WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
9015 : : DECL_ATTRS is as for grokdeclarator.
9016 : :
9017 : : LOC is the location of the structure component.
9018 : :
9019 : : This is done during the parsing of the struct declaration.
9020 : : The FIELD_DECL nodes are chained together and the lot of them
9021 : : are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
9022 : :
9023 : : tree
9024 : 4281410 : grokfield (location_t loc,
9025 : : struct c_declarator *declarator, struct c_declspecs *declspecs,
9026 : : tree width, tree *decl_attrs, tree *expr)
9027 : : {
9028 : 4281410 : tree value;
9029 : :
9030 : 4281410 : if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
9031 : 19912 : && width == NULL_TREE)
9032 : : {
9033 : : /* This is an unnamed decl.
9034 : :
9035 : : If we have something of the form "union { list } ;" then this
9036 : : is the anonymous union extension. Similarly for struct.
9037 : :
9038 : : If this is something of the form "struct foo;", then
9039 : : If MS or Plan 9 extensions are enabled, this is handled as
9040 : : an anonymous struct.
9041 : : Otherwise this is a forward declaration of a structure tag.
9042 : :
9043 : : If this is something of the form "foo;" and foo is a TYPE_DECL, then
9044 : : If foo names a structure or union without a tag, then this
9045 : : is an anonymous struct (this is permitted by C11).
9046 : : If MS or Plan 9 extensions are enabled and foo names a
9047 : : structure, then again this is an anonymous struct.
9048 : : Otherwise this is an error.
9049 : :
9050 : : Oh what a horrid tangled web we weave. I wonder if MS consciously
9051 : : took this from Plan 9 or if it was an accident of implementation
9052 : : that took root before someone noticed the bug... */
9053 : :
9054 : 10724 : tree type = declspecs->type;
9055 : 10724 : bool ok = false;
9056 : :
9057 : 10724 : if (RECORD_OR_UNION_TYPE_P (type)
9058 : 10718 : && (flag_ms_extensions
9059 : 10708 : || flag_plan9_extensions
9060 : 10693 : || !declspecs->typedef_p))
9061 : : {
9062 : 10711 : if (flag_ms_extensions || flag_plan9_extensions)
9063 : : ok = true;
9064 : 10686 : else if (TYPE_NAME (type) == NULL)
9065 : : ok = true;
9066 : : else
9067 : : ok = false;
9068 : : }
9069 : : if (!ok)
9070 : : {
9071 : 29 : pedwarn (loc, 0, "declaration does not declare anything");
9072 : 29 : return NULL_TREE;
9073 : : }
9074 : 10695 : if (flag_isoc99)
9075 : 10673 : pedwarn_c99 (loc, OPT_Wpedantic,
9076 : : "ISO C99 doesn%'t support unnamed structs/unions");
9077 : : else
9078 : 22 : pedwarn_c99 (loc, OPT_Wpedantic,
9079 : : "ISO C90 doesn%'t support unnamed structs/unions");
9080 : : }
9081 : :
9082 : 4281381 : value = grokdeclarator (declarator, declspecs, FIELD, false,
9083 : 4281381 : width ? &width : NULL, decl_attrs, expr, NULL,
9084 : : DEPRECATED_NORMAL);
9085 : :
9086 : 4281381 : finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
9087 : 4281381 : DECL_INITIAL (value) = width;
9088 : 4281381 : if (width)
9089 : 51992 : SET_DECL_C_BIT_FIELD (value);
9090 : :
9091 : 4281381 : if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
9092 : : {
9093 : : /* If we currently have a binding for this field, set the
9094 : : in_struct field in the binding, so that we warn about lookups
9095 : : which find it. */
9096 : 1312 : struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
9097 : 1312 : if (b != NULL)
9098 : : {
9099 : : /* If the in_struct field is not yet set, push it on a list
9100 : : to be cleared when this struct is finished. */
9101 : 93 : if (!b->in_struct)
9102 : : {
9103 : 91 : struct_parse_info->fields.safe_push (b);
9104 : 91 : b->in_struct = 1;
9105 : : }
9106 : : }
9107 : : }
9108 : :
9109 : : return value;
9110 : : }
9111 : :
9112 : : /* Subroutine of detect_field_duplicates: return whether X and Y,
9113 : : which are both fields in the same struct, have duplicate field
9114 : : names. */
9115 : :
9116 : : static bool
9117 : 4265183 : is_duplicate_field (tree x, tree y)
9118 : : {
9119 : 4265183 : if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
9120 : : return true;
9121 : :
9122 : : /* When using -fplan9-extensions, an anonymous field whose name is a
9123 : : typedef can duplicate a field name. */
9124 : 4265182 : if (flag_plan9_extensions
9125 : 4265182 : && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
9126 : : {
9127 : 0 : tree xt, xn, yt, yn;
9128 : :
9129 : 0 : xt = TREE_TYPE (x);
9130 : 0 : if (DECL_NAME (x) != NULL_TREE)
9131 : 0 : xn = DECL_NAME (x);
9132 : 0 : else if (RECORD_OR_UNION_TYPE_P (xt)
9133 : 0 : && TYPE_NAME (xt) != NULL_TREE
9134 : 0 : && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
9135 : 0 : xn = DECL_NAME (TYPE_NAME (xt));
9136 : : else
9137 : : xn = NULL_TREE;
9138 : :
9139 : 0 : yt = TREE_TYPE (y);
9140 : 0 : if (DECL_NAME (y) != NULL_TREE)
9141 : 0 : yn = DECL_NAME (y);
9142 : 0 : else if (RECORD_OR_UNION_TYPE_P (yt)
9143 : 0 : && TYPE_NAME (yt) != NULL_TREE
9144 : 0 : && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
9145 : 0 : yn = DECL_NAME (TYPE_NAME (yt));
9146 : : else
9147 : : yn = NULL_TREE;
9148 : :
9149 : 0 : if (xn != NULL_TREE && xn == yn)
9150 : 0 : return true;
9151 : : }
9152 : :
9153 : : return false;
9154 : : }
9155 : :
9156 : : /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
9157 : : to HTAB, giving errors for any duplicates. */
9158 : :
9159 : : static void
9160 : 79761 : detect_field_duplicates_hash (tree fieldlist,
9161 : : hash_table<nofree_ptr_hash <tree_node> > *htab)
9162 : : {
9163 : 79761 : tree x, y;
9164 : 79761 : tree_node **slot;
9165 : :
9166 : 1402895 : for (x = fieldlist; x ; x = DECL_CHAIN (x))
9167 : 1323134 : if ((y = DECL_NAME (x)) != NULL_TREE)
9168 : : {
9169 : 1303245 : slot = htab->find_slot (y, INSERT);
9170 : 1303245 : if (*slot)
9171 : : {
9172 : 15 : error ("duplicate member %q+D", x);
9173 : 15 : DECL_NAME (x) = NULL_TREE;
9174 : : }
9175 : 1303245 : *slot = y;
9176 : : }
9177 : 19889 : else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9178 : : {
9179 : 11415 : detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
9180 : :
9181 : : /* When using -fplan9-extensions, an anonymous field whose
9182 : : name is a typedef can duplicate a field name. */
9183 : 11415 : if (flag_plan9_extensions
9184 : 15 : && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9185 : 11429 : && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
9186 : : {
9187 : 2 : tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
9188 : 2 : slot = htab->find_slot (xn, INSERT);
9189 : 2 : if (*slot)
9190 : 0 : error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
9191 : 2 : *slot = xn;
9192 : : }
9193 : : }
9194 : 79761 : }
9195 : :
9196 : : /* Generate an error for any duplicate field names in FIELDLIST. Munge
9197 : : the list such that this does not present a problem later. */
9198 : :
9199 : : static void
9200 : 1168221 : detect_field_duplicates (tree fieldlist)
9201 : : {
9202 : 1168221 : tree x, y;
9203 : 1168221 : int timeout = 10;
9204 : :
9205 : : /* If the struct is the list of instance variables of an Objective-C
9206 : : class, then we need to check all the instance variables of
9207 : : superclasses when checking for duplicates (since you can't have
9208 : : an instance variable in a subclass with the same name as an
9209 : : instance variable in a superclass). We pass on this job to the
9210 : : Objective-C compiler. objc_detect_field_duplicates() will return
9211 : : false if we are not checking the list of instance variables and
9212 : : the C frontend should proceed with the standard field duplicate
9213 : : checks. If we are checking the list of instance variables, the
9214 : : ObjC frontend will do the check, emit the errors if needed, and
9215 : : then return true. */
9216 : 1168221 : if (c_dialect_objc ())
9217 : 0 : if (objc_detect_field_duplicates (false))
9218 : : return;
9219 : :
9220 : : /* First, see if there are more than "a few" fields.
9221 : : This is trivially true if there are zero or one fields. */
9222 : 1168221 : if (!fieldlist || !DECL_CHAIN (fieldlist))
9223 : : return;
9224 : : x = fieldlist;
9225 : 3439773 : do {
9226 : 3439773 : timeout--;
9227 : 3439773 : if (DECL_NAME (x) == NULL_TREE
9228 : 3439773 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9229 : : timeout = 0;
9230 : 3439773 : x = DECL_CHAIN (x);
9231 : 3439773 : } while (timeout > 0 && x);
9232 : :
9233 : : /* If there were "few" fields and no anonymous structures or unions,
9234 : : avoid the overhead of allocating a hash table. Instead just do
9235 : : the nested traversal thing. */
9236 : 964071 : if (timeout > 0)
9237 : : {
9238 : 2792292 : for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
9239 : : /* When using -fplan9-extensions, we can have duplicates
9240 : : between typedef names and fields. */
9241 : 1896567 : if (DECL_NAME (x)
9242 : 1896567 : || (flag_plan9_extensions
9243 : 0 : && DECL_NAME (x) == NULL_TREE
9244 : 0 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9245 : 0 : && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9246 : 0 : && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
9247 : : {
9248 : 6161214 : for (y = fieldlist; y != x; y = TREE_CHAIN (y))
9249 : 4265183 : if (is_duplicate_field (y, x))
9250 : : {
9251 : 1 : error ("duplicate member %q+D", x);
9252 : 1 : DECL_NAME (x) = NULL_TREE;
9253 : : }
9254 : : }
9255 : : }
9256 : : else
9257 : : {
9258 : 68346 : hash_table<nofree_ptr_hash <tree_node> > htab (37);
9259 : 68346 : detect_field_duplicates_hash (fieldlist, &htab);
9260 : 68346 : }
9261 : : }
9262 : :
9263 : : /* Finish up struct info used by -Wc++-compat. */
9264 : :
9265 : : static void
9266 : 575 : warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
9267 : : location_t record_loc)
9268 : : {
9269 : 575 : unsigned int ix;
9270 : 575 : tree x;
9271 : 575 : struct c_binding *b;
9272 : :
9273 : 575 : if (fieldlist == NULL_TREE)
9274 : : {
9275 : 9 : if (code == RECORD_TYPE)
9276 : 6 : warning_at (record_loc, OPT_Wc___compat,
9277 : : "empty struct has size 0 in C, size 1 in C++");
9278 : : else
9279 : 3 : warning_at (record_loc, OPT_Wc___compat,
9280 : : "empty union has size 0 in C, size 1 in C++");
9281 : : }
9282 : :
9283 : : /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
9284 : : the current struct. We do this now at the end of the struct
9285 : : because the flag is used to issue visibility warnings, and we
9286 : : only want to issue those warnings if the type is referenced
9287 : : outside of the struct declaration. */
9288 : 623 : FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
9289 : 48 : C_TYPE_DEFINED_IN_STRUCT (x) = 1;
9290 : :
9291 : : /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
9292 : : typedefs used when declaring fields in this struct. If the name
9293 : : of any of the fields is also a typedef name then the struct would
9294 : : not parse in C++, because the C++ lookup rules say that the
9295 : : typedef name would be looked up in the context of the struct, and
9296 : : would thus be the field rather than the typedef. */
9297 : 575 : if (!struct_parse_info->typedefs_seen.is_empty ()
9298 : 67 : && fieldlist != NULL_TREE)
9299 : : {
9300 : : /* Use a hash_set<tree> using the name of the typedef. We can use
9301 : : a hash_set<tree> because identifiers are interned. */
9302 : 67 : hash_set<tree> tset;
9303 : :
9304 : 254 : FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
9305 : 120 : tset.add (DECL_NAME (x));
9306 : :
9307 : 387 : for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
9308 : : {
9309 : 320 : if (DECL_NAME (x) != NULL_TREE
9310 : 320 : && tset.contains (DECL_NAME (x)))
9311 : : {
9312 : 2 : warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
9313 : : "using %qD as both field and typedef name is "
9314 : : "invalid in C++", x);
9315 : : /* FIXME: It would be nice to report the location where
9316 : : the typedef name is used. */
9317 : : }
9318 : : }
9319 : 67 : }
9320 : :
9321 : : /* For each field which has a binding and which was not defined in
9322 : : an enclosing struct, clear the in_struct field. */
9323 : 666 : FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
9324 : 91 : b->in_struct = 0;
9325 : 575 : }
9326 : :
9327 : : /* Function to help qsort sort FIELD_DECLs by name order. */
9328 : :
9329 : : static int
9330 : 20026403 : field_decl_cmp (const void *x_p, const void *y_p)
9331 : : {
9332 : 20026403 : const tree *const x = (const tree *) x_p;
9333 : 20026403 : const tree *const y = (const tree *) y_p;
9334 : :
9335 : 20026403 : if (DECL_NAME (*x) == DECL_NAME (*y))
9336 : : /* A nontype is "greater" than a type. */
9337 : 0 : return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9338 : 20026403 : if (DECL_NAME (*x) == NULL_TREE)
9339 : : return -1;
9340 : 20026403 : if (DECL_NAME (*y) == NULL_TREE)
9341 : : return 1;
9342 : 20026403 : if (DECL_NAME (*x) < DECL_NAME (*y))
9343 : 9854808 : return -1;
9344 : : return 1;
9345 : : }
9346 : :
9347 : : /* If this structure or union completes the type of any previous
9348 : : variable declaration, lay it out and output its rtl. */
9349 : : static void
9350 : 1345913 : finish_incomplete_vars (tree incomplete_vars, bool toplevel)
9351 : : {
9352 : 1346062 : for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
9353 : : {
9354 : 149 : tree decl = TREE_VALUE (x);
9355 : 149 : if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9356 : 0 : layout_array_type (TREE_TYPE (decl));
9357 : 149 : if (TREE_CODE (decl) != TYPE_DECL)
9358 : : {
9359 : 149 : relayout_decl (decl);
9360 : 149 : if (c_dialect_objc ())
9361 : 0 : objc_check_decl (decl);
9362 : 149 : rest_of_decl_compilation (decl, toplevel, 0);
9363 : : }
9364 : : }
9365 : 1345913 : }
9366 : :
9367 : : /* Determine whether the FIELD_DECL X is a flexible array member according to
9368 : : the following info:
9369 : : A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT;
9370 : : B. whether the FIELD_DECL is an array that is declared as "[]", "[0]",
9371 : : or "[1]";
9372 : : C. flag_strict_flex_arrays;
9373 : : D. the attribute strict_flex_array that is attached to the field
9374 : : if presenting.
9375 : : Return TRUE when it's a flexible array member, FALSE otherwise. */
9376 : :
9377 : : static bool
9378 : 4281461 : is_flexible_array_member_p (bool is_last_field,
9379 : : tree x)
9380 : : {
9381 : : /* If not the last field, return false. */
9382 : 4281461 : if (!is_last_field)
9383 : : return false;
9384 : :
9385 : : /* If not an array field, return false. */
9386 : 1639367 : if (TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
9387 : : return false;
9388 : :
9389 : 529015 : bool is_zero_length_array = zero_length_array_type_p (TREE_TYPE (x));
9390 : 529015 : bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x));
9391 : 529015 : bool is_flexible_array = c_flexible_array_member_type_p (TREE_TYPE (x));
9392 : :
9393 : 529015 : unsigned int strict_flex_array_level = c_strict_flex_array_level_of (x);
9394 : :
9395 : 529015 : switch (strict_flex_array_level)
9396 : : {
9397 : : case 0:
9398 : : /* Default, all trailing arrays are flexible array members. */
9399 : : return true;
9400 : 77 : case 1:
9401 : : /* Level 1: all "[1]", "[0]", and "[]" are flexible array members. */
9402 : 77 : if (is_one_element_array)
9403 : : return true;
9404 : : /* FALLTHROUGH. */
9405 : 141 : case 2:
9406 : : /* Level 2: all "[0]", and "[]" are flexible array members. */
9407 : 141 : if (is_zero_length_array)
9408 : : return true;
9409 : : /* FALLTHROUGH. */
9410 : 201 : case 3:
9411 : : /* Level 3: Only "[]" are flexible array members. */
9412 : 201 : if (is_flexible_array)
9413 : : return true;
9414 : : break;
9415 : 0 : default:
9416 : 0 : gcc_unreachable ();
9417 : : }
9418 : : return false;
9419 : : }
9420 : :
9421 : : /* Recompute TYPE_CANONICAL for variants of the type including qualified
9422 : : versions of the type and related pointer types after an aggregate type
9423 : : has been finalized.
9424 : : Will not update array types, pointers to array types, function
9425 : : types and other derived types created while the type was still
9426 : : incomplete, those will remain TYPE_STRUCTURAL_EQUALITY_P. */
9427 : :
9428 : : static void
9429 : 1181777 : c_update_type_canonical (tree t)
9430 : : {
9431 : 1181777 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t && !TYPE_QUALS (t));
9432 : 2390038 : for (tree x = t, l = NULL_TREE; x; l = x, x = TYPE_NEXT_VARIANT (x))
9433 : : {
9434 : 1208261 : if (x != t && TYPE_STRUCTURAL_EQUALITY_P (x))
9435 : : {
9436 : 26484 : if (!TYPE_QUALS (x))
9437 : 25862 : TYPE_CANONICAL (x) = TYPE_CANONICAL (t);
9438 : : else
9439 : : {
9440 : 622 : tree
9441 : 622 : c = c_build_qualified_type (TYPE_CANONICAL (t), TYPE_QUALS (x));
9442 : 622 : if (TYPE_STRUCTURAL_EQUALITY_P (c))
9443 : : {
9444 : 610 : gcc_checking_assert (TYPE_CANONICAL (t) == t);
9445 : 610 : if (c == x)
9446 : 610 : TYPE_CANONICAL (x) = x;
9447 : : else
9448 : : {
9449 : : /* build_qualified_type for this function unhelpfully
9450 : : moved c from some later spot in TYPE_MAIN_VARIANT (t)
9451 : : chain to right after t (or created it there). Move
9452 : : it right before x and process c and then x. */
9453 : 0 : gcc_checking_assert (TYPE_NEXT_VARIANT (t) == c);
9454 : 0 : if (l != t)
9455 : : {
9456 : 0 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (c);
9457 : 0 : TYPE_NEXT_VARIANT (l) = c;
9458 : 0 : TYPE_NEXT_VARIANT (c) = x;
9459 : : }
9460 : 0 : TYPE_CANONICAL (c) = c;
9461 : 0 : x = c;
9462 : : }
9463 : : }
9464 : : else
9465 : 12 : TYPE_CANONICAL (x) = TYPE_CANONICAL (c);
9466 : : }
9467 : : }
9468 : 1181777 : else if (x != t)
9469 : 0 : continue;
9470 : 1276665 : for (tree p = TYPE_POINTER_TO (x); p; p = TYPE_NEXT_PTR_TO (p))
9471 : : {
9472 : 68404 : if (!TYPE_STRUCTURAL_EQUALITY_P (p))
9473 : 0 : continue;
9474 : 68404 : if (TYPE_CANONICAL (x) != x || TYPE_REF_CAN_ALIAS_ALL (p))
9475 : 2521 : TYPE_CANONICAL (p)
9476 : 5042 : = c_build_pointer_type_for_mode (TYPE_CANONICAL (x), TYPE_MODE (p),
9477 : : false);
9478 : : else
9479 : 65883 : TYPE_CANONICAL (p) = p;
9480 : 68404 : c_update_type_canonical (p);
9481 : : }
9482 : : }
9483 : 1181777 : }
9484 : :
9485 : : /* Verify the argument of the counted_by attribute of each of the
9486 : : FIELDS_WITH_COUNTED_BY is a valid field of the containing structure,
9487 : : STRUCT_TYPE, Report error and remove the corresponding attribute
9488 : : when it's not. */
9489 : :
9490 : : static void
9491 : 161 : verify_counted_by_attribute (tree struct_type,
9492 : : auto_vec<tree> *fields_with_counted_by)
9493 : : {
9494 : 658 : for (tree field_decl : *fields_with_counted_by)
9495 : : {
9496 : 175 : tree attr_counted_by = lookup_attribute ("counted_by",
9497 : 175 : DECL_ATTRIBUTES (field_decl));
9498 : :
9499 : 175 : if (!attr_counted_by)
9500 : 0 : continue;
9501 : :
9502 : : /* If there is an counted_by attribute attached to the field,
9503 : : verify it. */
9504 : :
9505 : 175 : tree fieldname = TREE_VALUE (TREE_VALUE (attr_counted_by));
9506 : :
9507 : : /* Verify the argument of the attrbute is a valid field of the
9508 : : containing structure. */
9509 : :
9510 : 175 : tree counted_by_field = lookup_field (struct_type, fieldname);
9511 : :
9512 : : /* Error when the field is not found in the containing structure and
9513 : : remove the corresponding counted_by attribute from the field_decl. */
9514 : 175 : if (!counted_by_field)
9515 : : {
9516 : 4 : error_at (DECL_SOURCE_LOCATION (field_decl),
9517 : : "argument %qE to the %<counted_by%> attribute"
9518 : : " is not a field declaration in the same structure"
9519 : : " as %qD", fieldname, field_decl);
9520 : 4 : DECL_ATTRIBUTES (field_decl)
9521 : 8 : = remove_attribute ("counted_by", DECL_ATTRIBUTES (field_decl));
9522 : : }
9523 : : else
9524 : : /* Error when the field is not with an integer type. */
9525 : : {
9526 : 215 : while (TREE_CHAIN (counted_by_field))
9527 : 44 : counted_by_field = TREE_CHAIN (counted_by_field);
9528 : 171 : tree real_field = TREE_VALUE (counted_by_field);
9529 : :
9530 : 171 : if (!INTEGRAL_TYPE_P (TREE_TYPE (real_field)))
9531 : : {
9532 : 4 : error_at (DECL_SOURCE_LOCATION (field_decl),
9533 : : "argument %qE to the %<counted_by%> attribute"
9534 : : " is not a field declaration with an integer type",
9535 : : fieldname);
9536 : 4 : DECL_ATTRIBUTES (field_decl)
9537 : 8 : = remove_attribute ("counted_by",
9538 : 4 : DECL_ATTRIBUTES (field_decl));
9539 : : }
9540 : : }
9541 : : }
9542 : 161 : }
9543 : :
9544 : : /* TYPE is a struct or union that we're applying may_alias to after the body is
9545 : : parsed. Fixup any POINTER_TO types. */
9546 : :
9547 : : static void
9548 : 363 : c_fixup_may_alias (tree type)
9549 : : {
9550 : 428 : for (tree t = TYPE_POINTER_TO (type); t; t = TYPE_NEXT_PTR_TO (t))
9551 : 190 : for (tree v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
9552 : 125 : TYPE_REF_CAN_ALIAS_ALL (v) = true;
9553 : 363 : }
9554 : :
9555 : : /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
9556 : : LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
9557 : : FIELDLIST is a chain of FIELD_DECL nodes for the fields.
9558 : : ATTRIBUTES are attributes to be applied to the structure.
9559 : :
9560 : : ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
9561 : : the struct was started. */
9562 : :
9563 : : tree
9564 : 1168221 : finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
9565 : : class c_struct_parse_info *enclosing_struct_parse_info,
9566 : : tree *expr)
9567 : : {
9568 : 1168221 : tree x;
9569 : 1168221 : bool toplevel = file_scope == current_scope;
9570 : :
9571 : : /* If this type was previously laid out as a forward reference,
9572 : : make sure we lay it out again. */
9573 : :
9574 : 1168221 : TYPE_SIZE (t) = NULL_TREE;
9575 : :
9576 : 1168221 : decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9577 : :
9578 : 1168221 : if (pedantic)
9579 : : {
9580 : 7376 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9581 : : {
9582 : 7357 : if (DECL_NAME (x) != NULL_TREE)
9583 : : break;
9584 : 23 : if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9585 : : break;
9586 : : }
9587 : :
9588 : 7360 : if (x == NULL_TREE)
9589 : : {
9590 : 19 : if (TREE_CODE (t) == UNION_TYPE)
9591 : : {
9592 : 5 : if (fieldlist)
9593 : 2 : pedwarn (loc, OPT_Wpedantic, "union has no named members");
9594 : : else
9595 : 3 : pedwarn (loc, OPT_Wpedantic, "union has no members");
9596 : : }
9597 : : else
9598 : : {
9599 : 14 : if (fieldlist)
9600 : 3 : pedwarn (loc, OPT_Wpedantic, "struct has no named members");
9601 : : else
9602 : 11 : pedwarn (loc, OPT_Wpedantic, "struct has no members");
9603 : : }
9604 : : }
9605 : : }
9606 : :
9607 : : /* Install struct as DECL_CONTEXT of each field decl.
9608 : : Also process specified field sizes, found in the DECL_INITIAL,
9609 : : storing 0 there after the type has been changed to precision equal
9610 : : to its width, rather than the precision of the specified standard
9611 : : type. (Correct layout requires the original type to have been preserved
9612 : : until now.) */
9613 : :
9614 : 1168221 : bool saw_named_field = false;
9615 : 1168221 : auto_vec<tree> fields_with_counted_by;
9616 : 5449722 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9617 : : {
9618 : : /* Whether this field is the last field of the structure or union.
9619 : : for UNION, any field is the last field of it. */
9620 : 4281501 : bool is_last_field = (DECL_CHAIN (x) == NULL_TREE)
9621 : 4281501 : || (TREE_CODE (t) == UNION_TYPE);
9622 : :
9623 : 4281501 : if (TREE_TYPE (x) == error_mark_node)
9624 : 40 : continue;
9625 : :
9626 : 4281461 : DECL_CONTEXT (x) = t;
9627 : :
9628 : 4281461 : tree t1 = strip_array_types (TREE_TYPE (x));
9629 : : /* If any field is const, the structure type is pseudo-const. */
9630 : 4281461 : if (TREE_READONLY (x))
9631 : 64244 : C_TYPE_FIELDS_READONLY (t) = 1;
9632 : : else
9633 : : {
9634 : : /* A field that is pseudo-const makes the structure likewise. */
9635 : 4217217 : if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
9636 : 641 : C_TYPE_FIELDS_READONLY (t) = 1;
9637 : : }
9638 : :
9639 : : /* Any field that is volatile means variables of this type must be
9640 : : treated in some ways as volatile. */
9641 : 4281461 : if (TREE_THIS_VOLATILE (x))
9642 : : {
9643 : 367 : C_TYPE_FIELDS_VOLATILE (t) = 1;
9644 : 367 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9645 : : }
9646 : :
9647 : : /* Any field that is volatile, restrict-qualified or atomic
9648 : : means the type cannot be used for a constexpr object. */
9649 : 4281461 : if (TYPE_QUALS (t1)
9650 : 4281461 : & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
9651 : 1279 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9652 : 4280182 : else if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_NON_CONSTEXPR (t1))
9653 : 31 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9654 : :
9655 : : /* Any field of nominal variable size implies structure is too. */
9656 : 4281461 : if (C_DECL_VARIABLE_SIZE (x))
9657 : 651 : C_TYPE_VARIABLE_SIZE (t) = 1;
9658 : :
9659 : : /* If any field is variably modified, record this fact. */
9660 : 4281461 : if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (x)))
9661 : 709 : C_TYPE_VARIABLY_MODIFIED (t) = 1;
9662 : :
9663 : 4281461 : if (DECL_C_BIT_FIELD (x))
9664 : : {
9665 : 52001 : unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
9666 : 52001 : DECL_SIZE (x) = bitsize_int (width);
9667 : 52001 : DECL_BIT_FIELD (x) = 1;
9668 : : }
9669 : :
9670 : 4281461 : if (TYPE_PACKED (t)
9671 : 4281461 : && (DECL_BIT_FIELD (x)
9672 : 3235 : || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
9673 : 4442 : DECL_PACKED (x) = 1;
9674 : :
9675 : : /* Detect flexible array member in an invalid context. */
9676 : 4281461 : if (c_flexible_array_member_type_p (TREE_TYPE (x)))
9677 : : {
9678 : 84727 : if (TREE_CODE (t) == UNION_TYPE)
9679 : 29 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9680 : : "flexible array member in union is a GCC extension");
9681 : 84698 : else if (!is_last_field)
9682 : : {
9683 : 3 : error_at (DECL_SOURCE_LOCATION (x),
9684 : : "flexible array member not at end of struct");
9685 : 3 : TREE_TYPE (x) = error_mark_node;
9686 : : }
9687 : 84695 : else if (!saw_named_field)
9688 : 20 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9689 : : "flexible array member in a struct with no named "
9690 : : "members is a GCC extension");
9691 : :
9692 : : /* If there is a counted_by attribute attached to this field,
9693 : : record it here and do more verification later after the
9694 : : whole structure is complete. */
9695 : 84727 : if (lookup_attribute ("counted_by", DECL_ATTRIBUTES (x)))
9696 : 77 : fields_with_counted_by.safe_push (x);
9697 : : }
9698 : :
9699 : 4281461 : if (TREE_CODE (TREE_TYPE (x)) == POINTER_TYPE)
9700 : : /* If there is a counted_by attribute attached to this field,
9701 : : record it here and do more verification later after the
9702 : : whole structure is complete. */
9703 : 706163 : if (lookup_attribute ("counted_by", DECL_ATTRIBUTES (x)))
9704 : 98 : fields_with_counted_by.safe_push (x);
9705 : :
9706 : 25331 : if (pedantic && TREE_CODE (t) == RECORD_TYPE
9707 : 4302530 : && flexible_array_type_p (TREE_TYPE (x)))
9708 : 18 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9709 : : "invalid use of structure with flexible array member");
9710 : :
9711 : : /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */
9712 : 4281461 : DECL_NOT_FLEXARRAY (x) = !is_flexible_array_member_p (is_last_field, x);
9713 : :
9714 : : /* Set TYPE_INCLUDES_FLEXARRAY for the context of x, t.
9715 : : when x is an array and is the last field.
9716 : : There is only one last_field for a structure type, but there might
9717 : : be multiple last_fields for a union type, therefore we should ORed
9718 : : the result for multiple last_fields. */
9719 : 4281461 : if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
9720 : 700238 : TYPE_INCLUDES_FLEXARRAY (t)
9721 : 1400476 : |= is_last_field && c_flexible_array_member_type_p (TREE_TYPE (x));
9722 : : /* Recursively set TYPE_INCLUDES_FLEXARRAY for the context of x, t
9723 : : when x is an union or record and is the last field. */
9724 : 3581223 : else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9725 : 460512 : TYPE_INCLUDES_FLEXARRAY (t)
9726 : 921024 : |= is_last_field && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x));
9727 : :
9728 : 4281461 : if (warn_flex_array_member_not_at_end
9729 : 56 : && !is_last_field
9730 : 22 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9731 : 4281473 : && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x)))
9732 : 5 : warning_at (DECL_SOURCE_LOCATION (x),
9733 : 5 : OPT_Wflex_array_member_not_at_end,
9734 : : "structure containing a flexible array member"
9735 : : " is not at the end of another structure");
9736 : :
9737 : 4281461 : if (DECL_NAME (x)
9738 : 4281461 : || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9739 : : saw_named_field = true;
9740 : :
9741 : 7862684 : if (AGGREGATE_TYPE_P (TREE_TYPE (x))
9742 : 4741973 : && TYPE_TYPELESS_STORAGE (TREE_TYPE (x)))
9743 : 445420 : TYPE_TYPELESS_STORAGE (t) = true;
9744 : : }
9745 : :
9746 : 1168221 : detect_field_duplicates (fieldlist);
9747 : :
9748 : : /* Now we have the nearly final fieldlist. Record it,
9749 : : then lay out the structure or union (including the fields). */
9750 : :
9751 : 1168221 : TYPE_FIELDS (t) = fieldlist;
9752 : :
9753 : 1168221 : maybe_apply_pragma_scalar_storage_order (t);
9754 : :
9755 : 1168221 : layout_type (t);
9756 : :
9757 : 1168221 : if (TYPE_SIZE_UNIT (t)
9758 : 1168221 : && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
9759 : 1167625 : && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
9760 : 2335846 : && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
9761 : 1 : error ("type %qT is too large", t);
9762 : :
9763 : : /* Give bit-fields their proper types and rewrite the type of array fields
9764 : : with scalar component if the enclosing type has reverse storage order. */
9765 : 5449722 : for (tree field = fieldlist; field; field = DECL_CHAIN (field))
9766 : : {
9767 : 4281501 : if (TREE_CODE (field) == FIELD_DECL
9768 : 4281501 : && DECL_INITIAL (field)
9769 : 4333512 : && TREE_TYPE (field) != error_mark_node)
9770 : : {
9771 : 52001 : unsigned HOST_WIDE_INT width
9772 : 52001 : = tree_to_uhwi (DECL_INITIAL (field));
9773 : 52001 : tree type = TREE_TYPE (field);
9774 : 52001 : if (width != TYPE_PRECISION (type))
9775 : : {
9776 : 37851 : if (TREE_CODE (type) == BITINT_TYPE
9777 : 37921 : && width >= (TYPE_UNSIGNED (type) ? 1 : 2))
9778 : 161 : TREE_TYPE (field)
9779 : 322 : = build_bitint_type (width, TYPE_UNSIGNED (type));
9780 : : else
9781 : 37690 : TREE_TYPE (field)
9782 : 37690 : = c_build_bitfield_integer_type (width,
9783 : 37690 : TYPE_UNSIGNED (type));
9784 : 37851 : if (tree attr = c_hardbool_type_attr (type))
9785 : 281 : decl_attributes (&TREE_TYPE (field),
9786 : : copy_list (attr),
9787 : : 0, NULL_TREE);
9788 : 37851 : SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
9789 : : }
9790 : 52001 : DECL_INITIAL (field) = NULL_TREE;
9791 : : }
9792 : 4229500 : else if (TYPE_REVERSE_STORAGE_ORDER (t)
9793 : 693 : && TREE_CODE (field) == FIELD_DECL
9794 : 4230193 : && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
9795 : : {
9796 : 106 : tree ftype = TREE_TYPE (field);
9797 : 106 : tree ctype = strip_array_types (ftype);
9798 : 106 : if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
9799 : : {
9800 : 93 : tree fmain_type = TYPE_MAIN_VARIANT (ftype);
9801 : 93 : tree *typep = &fmain_type;
9802 : 95 : do {
9803 : 95 : *typep = build_distinct_type_copy (*typep);
9804 : 95 : TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
9805 : 95 : typep = &TREE_TYPE (*typep);
9806 : 95 : } while (TREE_CODE (*typep) == ARRAY_TYPE);
9807 : 93 : TREE_TYPE (field)
9808 : 186 : = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
9809 : : }
9810 : : }
9811 : :
9812 : : /* Warn on problematic type punning for storage order purposes. */
9813 : 4281501 : if (TREE_CODE (t) == UNION_TYPE
9814 : 848395 : && TREE_CODE (field) == FIELD_DECL
9815 : 5129896 : && AGGREGATE_TYPE_P (TREE_TYPE (field)))
9816 : : {
9817 : 421235 : tree ftype = TREE_TYPE (field);
9818 : 421235 : if (TREE_CODE (ftype) == ARRAY_TYPE)
9819 : 310983 : ftype = strip_array_types (ftype);
9820 : 421235 : if (RECORD_OR_UNION_TYPE_P (ftype)
9821 : 421235 : && TYPE_REVERSE_STORAGE_ORDER (ftype)
9822 : 110746 : != TYPE_REVERSE_STORAGE_ORDER (t))
9823 : 1 : warning_at (DECL_SOURCE_LOCATION (field),
9824 : 1 : OPT_Wscalar_storage_order,
9825 : : "type punning toggles scalar storage order");
9826 : : }
9827 : : }
9828 : :
9829 : : /* Now we have the truly final field list.
9830 : : Store it in this type and in the variants. */
9831 : :
9832 : 1168221 : TYPE_FIELDS (t) = fieldlist;
9833 : :
9834 : : /* If there are lots of fields, sort so we can look through them fast.
9835 : : We arbitrarily consider 16 or more elts to be "a lot". */
9836 : :
9837 : 1168221 : {
9838 : 1168221 : int len = 0;
9839 : :
9840 : 5011042 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9841 : : {
9842 : 3870356 : if (len > 15 || DECL_NAME (x) == NULL)
9843 : : break;
9844 : 3842821 : len += 1;
9845 : : }
9846 : :
9847 : 1168221 : if (len > 15)
9848 : : {
9849 : 22442 : tree *field_array;
9850 : 22442 : struct lang_type *space;
9851 : 22442 : struct sorted_fields_type *space2;
9852 : :
9853 : 22442 : len += list_length (x);
9854 : :
9855 : : /* Use the same allocation policy here that make_node uses, to
9856 : : ensure that this lives as long as the rest of the struct decl.
9857 : : All decls in an inline function need to be saved. */
9858 : :
9859 : 22442 : space = ((struct lang_type *)
9860 : 22442 : ggc_internal_cleared_alloc (c_dialect_objc ()
9861 : : ? sizeof (struct lang_type)
9862 : : : offsetof (struct lang_type,
9863 : : info)));
9864 : 22442 : space2 = ((sorted_fields_type *)
9865 : 44884 : ggc_internal_alloc (sizeof (struct sorted_fields_type)
9866 : 22442 : + len * sizeof (tree)));
9867 : :
9868 : 22442 : len = 0;
9869 : 22442 : space->s = space2;
9870 : 22442 : field_array = &space2->elts[0];
9871 : 786360 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9872 : : {
9873 : 767141 : field_array[len++] = x;
9874 : :
9875 : : /* If there is anonymous struct or union, break out of the loop. */
9876 : 767141 : if (DECL_NAME (x) == NULL)
9877 : : break;
9878 : : }
9879 : : /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
9880 : 22442 : if (x == NULL)
9881 : : {
9882 : 19219 : TYPE_LANG_SPECIFIC (t) = space;
9883 : 19219 : TYPE_LANG_SPECIFIC (t)->s->len = len;
9884 : 19219 : field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
9885 : 19219 : qsort (field_array, len, sizeof (tree), field_decl_cmp);
9886 : : }
9887 : : }
9888 : : }
9889 : :
9890 : : /* If this was supposed to be a transparent union, but we can't
9891 : : make it one, warn and turn off the flag. */
9892 : 1168221 : if (TREE_CODE (t) == UNION_TYPE
9893 : 375029 : && TYPE_TRANSPARENT_AGGR (t)
9894 : 1168264 : && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
9895 : : {
9896 : 13 : TYPE_TRANSPARENT_AGGR (t) = 0;
9897 : 13 : warning_at (loc, 0, "union cannot be made transparent");
9898 : : }
9899 : :
9900 : : /* Check for consistency with previous definition. */
9901 : 1168221 : if (flag_isoc23 && NULL != enclosing_struct_parse_info)
9902 : : {
9903 : 900576 : tree vistype = previous_tag (t);
9904 : 900576 : if (vistype
9905 : 124 : && TREE_CODE (vistype) == TREE_CODE (t)
9906 : 900700 : && !C_TYPE_BEING_DEFINED (vistype))
9907 : : {
9908 : 104 : TYPE_STUB_DECL (t) = TYPE_STUB_DECL (vistype);
9909 : 104 : if (c_type_variably_modified_p (t))
9910 : : {
9911 : 7 : error ("redefinition of struct or union %qT with variably "
9912 : : "modified type", t);
9913 : 7 : if (struct_parse_info->refloc != UNKNOWN_LOCATION)
9914 : 7 : inform (struct_parse_info->refloc, "originally defined here");
9915 : : }
9916 : 97 : else if (!comptypes_same_p (t, vistype))
9917 : : {
9918 : 38 : error ("redefinition of struct or union %qT", t);
9919 : 38 : if (struct_parse_info->refloc != UNKNOWN_LOCATION)
9920 : 38 : inform (struct_parse_info->refloc, "originally defined here");
9921 : : }
9922 : : }
9923 : : }
9924 : :
9925 : 1168221 : C_TYPE_BEING_DEFINED (t) = 0;
9926 : :
9927 : 1168221 : if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (t)))
9928 : 694 : for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9929 : 363 : c_fixup_may_alias (x);
9930 : :
9931 : : /* Set type canonical based on equivalence class. */
9932 : 1168221 : if (flag_isoc23 && !C_TYPE_VARIABLE_SIZE (t))
9933 : : {
9934 : 935787 : if (c_struct_htab == NULL)
9935 : 35438 : c_struct_htab = hash_table<c_struct_hasher>::create_ggc (61);
9936 : :
9937 : 935787 : hashval_t hash = c_struct_hasher::hash (t);
9938 : :
9939 : 935787 : gcc_checking_assert (TYPE_STRUCTURAL_EQUALITY_P (t));
9940 : 935787 : gcc_checking_assert (!TYPE_NAME (t)
9941 : : || TREE_CODE (TYPE_NAME (t)) == IDENTIFIER_NODE);
9942 : :
9943 : 935787 : tree *e = c_struct_htab->find_slot_with_hash (t, hash, INSERT);
9944 : 935787 : if (*e)
9945 : 62087 : TYPE_CANONICAL (t) = *e;
9946 : : else
9947 : : {
9948 : 873700 : TYPE_CANONICAL (t) = t;
9949 : 873700 : *e = t;
9950 : : }
9951 : 935787 : c_update_type_canonical (t);
9952 : : }
9953 : :
9954 : 1168221 : tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
9955 : 2366998 : for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9956 : : {
9957 : 1198777 : TYPE_FIELDS (x) = TYPE_FIELDS (t);
9958 : 1198777 : TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
9959 : 1198777 : TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
9960 : 1198777 : TYPE_TYPELESS_STORAGE (x) = TYPE_TYPELESS_STORAGE (t);
9961 : 1198777 : C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
9962 : 1198777 : C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
9963 : 1198777 : C_TYPE_FIELDS_NON_CONSTEXPR (x) = C_TYPE_FIELDS_NON_CONSTEXPR (t);
9964 : 1198777 : C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
9965 : 1198777 : C_TYPE_VARIABLY_MODIFIED (x) = C_TYPE_VARIABLY_MODIFIED (t);
9966 : 1198777 : C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
9967 : 1198777 : TYPE_INCLUDES_FLEXARRAY (x) = TYPE_INCLUDES_FLEXARRAY (t);
9968 : : }
9969 : :
9970 : : /* Update type location to the one of the definition, instead of e.g.
9971 : : a forward declaration. */
9972 : 1168221 : if (TYPE_STUB_DECL (t))
9973 : 1168221 : DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
9974 : :
9975 : : /* Finish debugging output for this type. */
9976 : 1168221 : rest_of_type_compilation (t, toplevel);
9977 : :
9978 : 1168221 : finish_incomplete_vars (incomplete_vars, toplevel);
9979 : :
9980 : : /* Make sure a DECL_EXPR is created for structs with VLA members.
9981 : : Because we do not know the context, we always pass expr
9982 : : to force creation of a BIND_EXPR which is required in some
9983 : : contexts. */
9984 : 1168221 : if (c_type_variably_modified_p (t))
9985 : 667 : add_decl_expr (loc, t, expr, false);
9986 : :
9987 : 1168221 : if (warn_cxx_compat)
9988 : 575 : warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
9989 : :
9990 : 1168221 : if (NULL != enclosing_struct_parse_info)
9991 : : {
9992 : 1127363 : delete struct_parse_info;
9993 : :
9994 : 1127363 : struct_parse_info = enclosing_struct_parse_info;
9995 : :
9996 : : /* If this struct is defined inside a struct, add it to
9997 : : struct_types. */
9998 : 1127363 : if (warn_cxx_compat
9999 : : && struct_parse_info != NULL
10000 : 350 : && !in_sizeof && !in_typeof && !in_alignof && !in_countof)
10001 : 345 : struct_parse_info->struct_types.safe_push (t);
10002 : : }
10003 : :
10004 : 1168221 : if (fields_with_counted_by.length () > 0)
10005 : 161 : verify_counted_by_attribute (t, &fields_with_counted_by);
10006 : :
10007 : 1168221 : return t;
10008 : 1168221 : }
10009 : :
10010 : : static struct {
10011 : : gt_pointer_operator new_value;
10012 : : void *cookie;
10013 : : } resort_data;
10014 : :
10015 : : /* This routine compares two fields like field_decl_cmp but using the
10016 : : pointer operator in resort_data. */
10017 : :
10018 : : static int
10019 : 5397 : resort_field_decl_cmp (const void *x_p, const void *y_p)
10020 : : {
10021 : 5397 : const tree *const x = (const tree *) x_p;
10022 : 5397 : const tree *const y = (const tree *) y_p;
10023 : :
10024 : 5397 : if (DECL_NAME (*x) == DECL_NAME (*y))
10025 : : /* A nontype is "greater" than a type. */
10026 : 0 : return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10027 : 5397 : if (DECL_NAME (*x) == NULL_TREE)
10028 : : return -1;
10029 : 5397 : if (DECL_NAME (*y) == NULL_TREE)
10030 : : return 1;
10031 : 5397 : {
10032 : 5397 : tree d1 = DECL_NAME (*x);
10033 : 5397 : tree d2 = DECL_NAME (*y);
10034 : 5397 : resort_data.new_value (&d1, &d1, resort_data.cookie);
10035 : 5397 : resort_data.new_value (&d2, &d2, resort_data.cookie);
10036 : 5397 : if (d1 < d2)
10037 : 2731 : return -1;
10038 : : }
10039 : 2666 : return 1;
10040 : : }
10041 : :
10042 : : /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10043 : :
10044 : : void
10045 : 6 : resort_sorted_fields (void *obj,
10046 : : void * ARG_UNUSED (orig_obj),
10047 : : gt_pointer_operator new_value,
10048 : : void *cookie)
10049 : : {
10050 : 6 : struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
10051 : 6 : resort_data.new_value = new_value;
10052 : 6 : resort_data.cookie = cookie;
10053 : 6 : qsort (&sf->elts[0], sf->len, sizeof (tree),
10054 : : resort_field_decl_cmp);
10055 : 6 : }
10056 : :
10057 : : /* Lay out the type T, and its element type, and so on. */
10058 : :
10059 : : static void
10060 : 0 : layout_array_type (tree t)
10061 : : {
10062 : 0 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
10063 : 0 : layout_array_type (TREE_TYPE (t));
10064 : 0 : layout_type (t);
10065 : 0 : }
10066 : :
10067 : : /* Begin compiling the definition of an enumeration type.
10068 : : NAME is its name (or null if anonymous).
10069 : : LOC is the enum's location.
10070 : : FIXED_UNDERLYING_TYPE is the (C23) underlying type specified in the
10071 : : definition.
10072 : : Returns the type object, as yet incomplete.
10073 : : Also records info about it so that build_enumerator
10074 : : may be used to declare the individual values as they are read. */
10075 : :
10076 : : tree
10077 : 177692 : start_enum (location_t loc, struct c_enum_contents *the_enum, tree name,
10078 : : tree fixed_underlying_type, bool potential_nesting_p)
10079 : : {
10080 : 177692 : tree enumtype = NULL_TREE;
10081 : 177692 : location_t enumloc = UNKNOWN_LOCATION;
10082 : :
10083 : : /* If this is the real definition for a previous forward reference,
10084 : : fill in the contents in the same object that used to be the
10085 : : forward reference. */
10086 : :
10087 : 177692 : if (name != NULL_TREE)
10088 : 70773 : enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
10089 : :
10090 : 70773 : if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
10091 : : {
10092 : : /* If the type is currently being defined or if we have seen an
10093 : : incomplete version which is now complete, this is a nested
10094 : : redefinition. The later happens if the redefinition occurs
10095 : : inside the enum specifier itself. */
10096 : 177 : if (C_TYPE_BEING_DEFINED (enumtype)
10097 : 177 : || (potential_nesting_p && TYPE_VALUES (enumtype) != NULL_TREE))
10098 : 9 : error_at (loc, "nested redefinition of %<enum %E%>", name);
10099 : :
10100 : : /* For C23 we allow redefinitions. We set to zero and check for
10101 : : consistency later. */
10102 : 337 : if (flag_isoc23 && TYPE_VALUES (enumtype) != NULL_TREE)
10103 : : enumtype = NULL_TREE;
10104 : : }
10105 : :
10106 : 107070 : if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
10107 : : {
10108 : 177543 : enumtype = make_node (ENUMERAL_TYPE);
10109 : 177543 : TYPE_SIZE (enumtype) = NULL_TREE;
10110 : 177543 : SET_TYPE_STRUCTURAL_EQUALITY (enumtype);
10111 : 177543 : pushtag (loc, name, enumtype);
10112 : 177543 : if (fixed_underlying_type != NULL_TREE)
10113 : : {
10114 : : /* For an enum definition with a fixed underlying type, the
10115 : : type is complete during the definition and the
10116 : : enumeration constants have that type. If there was a
10117 : : tag, the type was completed in c_parser_enum_specifier.
10118 : : If not, it must be completed here. */
10119 : 12 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = true;
10120 : 12 : TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (fixed_underlying_type);
10121 : 12 : TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (fixed_underlying_type);
10122 : 12 : TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (fixed_underlying_type);
10123 : 12 : SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (fixed_underlying_type));
10124 : 12 : TYPE_SIZE (enumtype) = NULL_TREE;
10125 : 12 : TYPE_PRECISION (enumtype) = TYPE_PRECISION (fixed_underlying_type);
10126 : 12 : ENUM_UNDERLYING_TYPE (enumtype) = fixed_underlying_type;
10127 : 12 : TYPE_CANONICAL (enumtype) = TYPE_CANONICAL (fixed_underlying_type);
10128 : 12 : c_update_type_canonical (enumtype);
10129 : 12 : layout_type (enumtype);
10130 : : }
10131 : : }
10132 : : /* Update type location to the one of the definition, instead of e.g.
10133 : : a forward declaration. */
10134 : 149 : else if (TYPE_STUB_DECL (enumtype))
10135 : : {
10136 : 149 : enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
10137 : 149 : DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
10138 : : }
10139 : :
10140 : 177692 : C_TYPE_BEING_DEFINED (enumtype) = 1;
10141 : :
10142 : 177692 : if (TYPE_VALUES (enumtype) != NULL_TREE)
10143 : : {
10144 : : /* This enum is a named one that has been declared already. */
10145 : 6 : auto_diagnostic_group d;
10146 : 6 : error_at (loc, "redeclaration of %<enum %E%>", name);
10147 : 6 : if (enumloc != UNKNOWN_LOCATION)
10148 : 6 : inform (enumloc, "originally defined here");
10149 : :
10150 : : /* Completely replace its old definition.
10151 : : The old enumerators remain defined, however. */
10152 : 6 : TYPE_VALUES (enumtype) = NULL_TREE;
10153 : 6 : }
10154 : :
10155 : 177692 : if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype)
10156 : 177692 : && fixed_underlying_type == NULL_TREE)
10157 : : {
10158 : 3 : error_at (loc, "%<enum%> declared with but defined without "
10159 : : "fixed underlying type");
10160 : 3 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = false;
10161 : : }
10162 : :
10163 : 177692 : the_enum->enum_next_value = integer_zero_node;
10164 : 177692 : the_enum->enum_type = enumtype;
10165 : 177692 : the_enum->enum_overflow = 0;
10166 : :
10167 : 177692 : if (flag_short_enums && !ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
10168 : 106 : for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
10169 : 53 : TYPE_PACKED (v) = 1;
10170 : :
10171 : : /* FIXME: This will issue a warning for a use of a type defined
10172 : : within sizeof in a statement expr. This is not terribly serious
10173 : : as C++ doesn't permit statement exprs within sizeof anyhow. */
10174 : 177692 : if (warn_cxx_compat
10175 : 126 : && (in_sizeof || in_typeof || in_alignof || in_countof))
10176 : 0 : warning_at (loc, OPT_Wc___compat,
10177 : : "defining type in %qs expression is invalid in C++",
10178 : : (in_sizeof ? "sizeof"
10179 : 0 : : in_typeof ? "typeof"
10180 : 0 : : in_alignof ? "alignof"
10181 : : : "_Countof"));
10182 : :
10183 : 177692 : if (in_underspecified_init)
10184 : 6 : error_at (loc, "%qT defined in underspecified object initializer",
10185 : : enumtype);
10186 : :
10187 : 177692 : return enumtype;
10188 : : }
10189 : :
10190 : : /* After processing and defining all the values of an enumeration type,
10191 : : install their decls in the enumeration type and finish it off.
10192 : : ENUMTYPE is the type object, VALUES a list of decl-value pairs,
10193 : : and ATTRIBUTES are the specified attributes.
10194 : : Returns ENUMTYPE. */
10195 : :
10196 : : tree
10197 : 177692 : finish_enum (tree enumtype, tree values, tree attributes)
10198 : : {
10199 : 177692 : tree pair, tem;
10200 : 177692 : tree minnode = NULL_TREE, maxnode = NULL_TREE;
10201 : 177692 : int precision;
10202 : 177692 : signop sign;
10203 : 177692 : bool toplevel = (file_scope == current_scope);
10204 : 177692 : struct lang_type *lt;
10205 : :
10206 : 177692 : decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10207 : :
10208 : : /* Calculate the maximum value of any enumerator in this type. */
10209 : :
10210 : 177692 : if (values == error_mark_node)
10211 : 26 : minnode = maxnode = integer_zero_node;
10212 : : else
10213 : : {
10214 : 177666 : minnode = maxnode = TREE_VALUE (values);
10215 : 5621247 : for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
10216 : : {
10217 : 5443581 : tree value = TREE_VALUE (pair);
10218 : 5443581 : if (tree_int_cst_lt (maxnode, value))
10219 : 5298511 : maxnode = value;
10220 : 5443581 : if (tree_int_cst_lt (value, minnode))
10221 : 63836 : minnode = value;
10222 : : }
10223 : : }
10224 : :
10225 : : /* Construct the final type of this enumeration. It is the same
10226 : : as one of the integral types - the narrowest one that fits, except
10227 : : that normally we only go as narrow as int - and signed iff any of
10228 : : the values are negative. */
10229 : 177692 : sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
10230 : 177692 : precision = MAX (tree_int_cst_min_precision (minnode, sign),
10231 : : tree_int_cst_min_precision (maxnode, sign));
10232 : :
10233 : 177692 : bool wider_than_int =
10234 : 177692 : (tree_int_cst_lt (minnode, TYPE_MIN_VALUE (integer_type_node))
10235 : 177692 : || tree_int_cst_lt (TYPE_MAX_VALUE (integer_type_node), maxnode));
10236 : :
10237 : :
10238 : 177692 : if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
10239 : : {
10240 : : /* If the precision of the type was specified with an attribute and it
10241 : : was too small, give an error. Otherwise, use it. */
10242 : 177574 : if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
10243 : : {
10244 : 23 : if (precision > TYPE_PRECISION (enumtype))
10245 : : {
10246 : 6 : TYPE_PRECISION (enumtype) = 0;
10247 : 6 : error ("specified mode too small for enumerated values");
10248 : : }
10249 : : else
10250 : 17 : precision = TYPE_PRECISION (enumtype);
10251 : : }
10252 : : else
10253 : 177551 : TYPE_PRECISION (enumtype) = 0;
10254 : :
10255 : 177574 : if (TYPE_PACKED (enumtype)
10256 : 177495 : || precision > TYPE_PRECISION (integer_type_node)
10257 : 352382 : || TYPE_PRECISION (enumtype))
10258 : : {
10259 : 2893 : tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
10260 : 2778 : if (tem == NULL)
10261 : : {
10262 : : /* This should only occur when both signed and unsigned
10263 : : values of maximum precision occur among the
10264 : : enumerators. */
10265 : 3 : pedwarn (input_location, 0,
10266 : : "enumeration values exceed range of largest integer");
10267 : 3 : tem = widest_integer_literal_type_node;
10268 : : }
10269 : 2775 : else if (precision > TYPE_PRECISION (intmax_type_node)
10270 : 4 : && !tree_int_cst_lt (minnode,
10271 : 4 : TYPE_MIN_VALUE (intmax_type_node))
10272 : 2778 : && !tree_int_cst_lt (TYPE_MAX_VALUE (uintmax_type_node),
10273 : : maxnode))
10274 : 2 : pedwarn (input_location, OPT_Wpedantic,
10275 : : "enumeration values exceed range of %qs",
10276 : : sign == UNSIGNED ? "uintmax_t" : "intmax_t");
10277 : : }
10278 : : else
10279 : 174796 : tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
10280 : :
10281 : 177574 : TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
10282 : 177574 : TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
10283 : 177574 : TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
10284 : 177574 : SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
10285 : 177574 : TYPE_SIZE (enumtype) = NULL_TREE;
10286 : 177574 : TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
10287 : 177574 : ENUM_UNDERLYING_TYPE (enumtype) =
10288 : 177574 : c_common_type_for_size (TYPE_PRECISION (tem), TYPE_UNSIGNED (tem));
10289 : :
10290 : 532722 : TYPE_CANONICAL (enumtype) =
10291 : 177574 : TYPE_CANONICAL (ENUM_UNDERLYING_TYPE (enumtype));
10292 : 177574 : c_update_type_canonical (enumtype);
10293 : :
10294 : 177574 : layout_type (enumtype);
10295 : : }
10296 : :
10297 : 177692 : if (values != error_mark_node)
10298 : : {
10299 : : /* Change the type of the enumerators to be the enum type. We
10300 : : need to do this irrespective of the size of the enum, for
10301 : : proper type checking. Replace the DECL_INITIALs of the
10302 : : enumerators, and the value slots of the list, with copies
10303 : : that have the enum type; they cannot be modified in place
10304 : : because they may be shared (e.g. integer_zero_node) Finally,
10305 : : change the purpose slots to point to the names of the decls. */
10306 : 5798913 : for (pair = values; pair; pair = TREE_CHAIN (pair))
10307 : : {
10308 : 5621247 : tree enu = TREE_PURPOSE (pair);
10309 : 5621247 : tree ini = DECL_INITIAL (enu);
10310 : :
10311 : 5621247 : TREE_TYPE (enu) = enumtype;
10312 : :
10313 : : /* Before C23, the ISO C Standard mandates enumerators to
10314 : : have type int, even though the underlying type of an enum
10315 : : type is unspecified. However, C23 allows enumerators of
10316 : : any integer type, and if an enumeration has any
10317 : : enumerators wider than int, all enumerators have the
10318 : : enumerated type after it is parsed. Any enumerators that
10319 : : fit in int are given type int in build_enumerator (which
10320 : : is the correct type while the enumeration is being
10321 : : parsed), so no conversions are needed here if all
10322 : : enumerators fit in int. If the enum has a fixed
10323 : : underlying type, the correct type was also given in
10324 : : build_enumerator. */
10325 : 5621247 : if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) && wider_than_int)
10326 : 30807 : ini = convert (enumtype, ini);
10327 : :
10328 : 5621247 : DECL_INITIAL (enu) = ini;
10329 : 5621247 : TREE_PURPOSE (pair) = DECL_NAME (enu);
10330 : : /* To match the C++ FE, store the CONST_DECL rather than just its
10331 : : value. */
10332 : 5621247 : TREE_VALUE (pair) = enu;
10333 : : }
10334 : :
10335 : 177666 : TYPE_VALUES (enumtype) = values;
10336 : : }
10337 : :
10338 : : /* Record the min/max values so that we can warn about bit-field
10339 : : enumerations that are too small for the values. */
10340 : 177692 : lt = ((struct lang_type *)
10341 : 177692 : ggc_internal_cleared_alloc (c_dialect_objc ()
10342 : : ? sizeof (struct lang_type)
10343 : : : offsetof (struct lang_type, info)));
10344 : 177692 : lt->enum_min = minnode;
10345 : 177692 : lt->enum_max = maxnode;
10346 : 177692 : TYPE_LANG_SPECIFIC (enumtype) = lt;
10347 : :
10348 : : /* Fix up all variant types of this enum type. */
10349 : 177692 : tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
10350 : 355394 : for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
10351 : : {
10352 : 177702 : C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
10353 : 177702 : if (tem == enumtype)
10354 : 177692 : continue;
10355 : 10 : TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
10356 : 10 : TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
10357 : 10 : TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
10358 : 10 : TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
10359 : 10 : TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
10360 : 10 : SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
10361 : 10 : TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
10362 : 10 : SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
10363 : 10 : TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
10364 : 10 : TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
10365 : 10 : TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
10366 : 10 : ENUM_UNDERLYING_TYPE (tem) = ENUM_UNDERLYING_TYPE (enumtype);
10367 : 10 : TYPE_PACKED (tem) = TYPE_PACKED (enumtype);
10368 : : }
10369 : :
10370 : : /* Finish debugging output for this type. */
10371 : 177692 : rest_of_type_compilation (enumtype, toplevel);
10372 : :
10373 : 177692 : finish_incomplete_vars (incomplete_vars, toplevel);
10374 : :
10375 : : /* If this enum is defined inside a struct, add it to
10376 : : struct_types. */
10377 : 177692 : if (warn_cxx_compat
10378 : 126 : && struct_parse_info != NULL
10379 : 42 : && !in_sizeof && !in_typeof && !in_alignof && !in_countof)
10380 : 42 : struct_parse_info->struct_types.safe_push (enumtype);
10381 : :
10382 : : /* Check for consistency with previous definition */
10383 : 177692 : if (flag_isoc23)
10384 : : {
10385 : 139678 : tree vistype = previous_tag (enumtype);
10386 : 139678 : if (vistype
10387 : 33 : && TREE_CODE (vistype) == TREE_CODE (enumtype)
10388 : 139707 : && !C_TYPE_BEING_DEFINED (vistype))
10389 : : {
10390 : 29 : TYPE_STUB_DECL (enumtype) = TYPE_STUB_DECL (vistype);
10391 : 29 : if (!comptypes_same_p (enumtype, vistype))
10392 : 3 : error("conflicting redefinition of enum %qT", enumtype);
10393 : : }
10394 : : }
10395 : :
10396 : 177692 : C_TYPE_BEING_DEFINED (enumtype) = 0;
10397 : :
10398 : 177692 : return enumtype;
10399 : : }
10400 : :
10401 : : /* Build and install a CONST_DECL for one value of the
10402 : : current enumeration type (one that was begun with start_enum).
10403 : : DECL_LOC is the location of the enumerator.
10404 : : LOC is the location of the '=' operator if any, DECL_LOC otherwise.
10405 : : Return a tree-list containing the CONST_DECL and its value.
10406 : : Assignment of sequential values by default is handled here. */
10407 : :
10408 : : tree
10409 : 5621264 : build_enumerator (location_t decl_loc, location_t loc,
10410 : : struct c_enum_contents *the_enum, tree name, tree value)
10411 : : {
10412 : 5621264 : tree decl;
10413 : 5621264 : tree old_decl;
10414 : :
10415 : : /* Validate and default VALUE. */
10416 : :
10417 : 5621264 : if (value != NULL_TREE)
10418 : : {
10419 : : /* Don't issue more errors for error_mark_node (i.e. an
10420 : : undeclared identifier) - just ignore the value expression. */
10421 : 3477973 : if (value == error_mark_node)
10422 : : value = NULL_TREE;
10423 : 3477308 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
10424 : : {
10425 : 6 : error_at (loc, "enumerator value for %qE is not an integer constant",
10426 : : name);
10427 : 6 : value = NULL_TREE;
10428 : : }
10429 : : else
10430 : : {
10431 : 3477302 : if (TREE_CODE (value) != INTEGER_CST)
10432 : : {
10433 : 97 : value = c_fully_fold (value, false, NULL);
10434 : 97 : if (TREE_CODE (value) == INTEGER_CST)
10435 : 48 : pedwarn (loc, OPT_Wpedantic,
10436 : : "enumerator value for %qE is not an integer "
10437 : : "constant expression", name);
10438 : : }
10439 : 3477302 : if (TREE_CODE (value) != INTEGER_CST)
10440 : : {
10441 : 49 : error ("enumerator value for %qE is not an integer constant",
10442 : : name);
10443 : 49 : value = NULL_TREE;
10444 : : }
10445 : : else
10446 : : {
10447 : 3477253 : value = default_conversion (value);
10448 : 3477253 : constant_expression_warning (value);
10449 : : }
10450 : : }
10451 : : }
10452 : :
10453 : : /* Default based on previous value. */
10454 : : /* It should no longer be possible to have NON_LVALUE_EXPR
10455 : : in the default. */
10456 : 3477308 : if (value == NULL_TREE)
10457 : : {
10458 : 2144011 : value = the_enum->enum_next_value;
10459 : 2144011 : if (the_enum->enum_overflow)
10460 : 8 : error_at (loc, "overflow in enumeration values");
10461 : : }
10462 : 5621264 : if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10463 : : {
10464 : : /* Enumeration constants must fit in the fixed underlying type. */
10465 : 163 : if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (the_enum->enum_type)))
10466 : 4 : error_at (loc,
10467 : : "enumerator value outside the range of underlying type");
10468 : : /* Enumeration constants for an enum with fixed underlying type
10469 : : have the enum type, both inside and outside the
10470 : : definition. */
10471 : 163 : value = convert (the_enum->enum_type, value);
10472 : : }
10473 : 5621101 : else if (flag_isoc23
10474 : 5208315 : && (old_decl = lookup_name_in_scope (name, current_scope))
10475 : 35 : && old_decl != error_mark_node
10476 : 35 : && TREE_TYPE (old_decl)
10477 : 35 : && TREE_TYPE (TREE_TYPE (old_decl))
10478 : 5621135 : && TREE_CODE (old_decl) == CONST_DECL)
10479 : : {
10480 : : /* Enumeration constants in a redeclaration have the previous type. */
10481 : 34 : tree previous_type = TREE_TYPE (DECL_INITIAL (old_decl));
10482 : 34 : if (!int_fits_type_p (value, previous_type))
10483 : : {
10484 : 2 : error_at (loc, "value of redeclared enumerator outside the range "
10485 : : "of %qT", previous_type);
10486 : 2 : locate_old_decl (old_decl);
10487 : : }
10488 : 34 : value = convert (previous_type, value);
10489 : : }
10490 : : else
10491 : : {
10492 : : /* Even though the underlying type of an enum is unspecified, the
10493 : : type of enumeration constants is explicitly defined as int
10494 : : (6.4.4.3/2 in the C99 Standard). C23 allows any integer type, and
10495 : : GCC allows such types for older standards as an extension. */
10496 : 5621067 : bool warned_range = false;
10497 : 5621067 : if (!int_fits_type_p (value,
10498 : 5621067 : (TYPE_UNSIGNED (TREE_TYPE (value))
10499 : : ? uintmax_type_node
10500 : : : intmax_type_node)))
10501 : : /* GCC does not consider its types larger than intmax_t to be
10502 : : extended integer types (although C23 would permit such types to
10503 : : be considered extended integer types if all the features
10504 : : required by <stdint.h> and <inttypes.h> macros, such as support
10505 : : for integer constants and I/O, were present), so diagnose if
10506 : : such a wider type is used. (If the wider type arose from a
10507 : : constant of such a type, that will also have been diagnosed,
10508 : : but this is the only diagnostic in the case where it arises
10509 : : from choosing a wider type automatically when adding 1
10510 : : overflows.) */
10511 : 11 : warned_range = pedwarn (loc, OPT_Wpedantic,
10512 : : "enumerator value outside the range of %qs",
10513 : 11 : TYPE_UNSIGNED (TREE_TYPE (value))
10514 : : ? "uintmax_t"
10515 : : : "intmax_t");
10516 : 5621067 : if (!warned_range && !int_fits_type_p (value, integer_type_node))
10517 : 4810 : pedwarn_c11 (loc, OPT_Wpedantic,
10518 : : "ISO C restricts enumerator values to range of %<int%> "
10519 : : "before C23");
10520 : :
10521 : : /* The ISO C Standard mandates enumerators to have type int before
10522 : : C23, even though the underlying type of an enum type is
10523 : : unspecified. C23 allows enumerators of any integer type. During
10524 : : the parsing of the enumeration, C23 specifies that constants
10525 : : representable in int have type int, constants not representable
10526 : : in int have the type of the given expression if any, and
10527 : : constants not representable in int and derived by adding 1 to the
10528 : : previous constant have the type of that constant unless the
10529 : : addition would overflow or wraparound, in which case a wider type
10530 : : of the same signedness is chosen automatically; after the
10531 : : enumeration is parsed, all the constants have the type of the
10532 : : enumeration if any do not fit in int. */
10533 : 5621067 : if (int_fits_type_p (value, integer_type_node))
10534 : 5616253 : value = convert (integer_type_node, value);
10535 : : }
10536 : :
10537 : : /* Set basis for default for next value. */
10538 : 5621264 : if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10539 : : {
10540 : 163 : tree underlying_type = ENUM_UNDERLYING_TYPE (the_enum->enum_type);
10541 : 163 : if (TREE_CODE (underlying_type) == BOOLEAN_TYPE)
10542 : : /* A value of 2 following a value of 1 overflows bool, but we
10543 : : cannot carry out addition directly on bool without
10544 : : promotion, and converting the result of arithmetic in a
10545 : : wider type back to bool would not produce the right result
10546 : : for this overflow check. */
10547 : 61 : the_enum->enum_next_value = invert_truthvalue_loc (loc, value);
10548 : : else
10549 : 102 : the_enum->enum_next_value
10550 : 102 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10551 : : PLUS_EXPR, convert (underlying_type, value),
10552 : : convert (underlying_type, integer_one_node),
10553 : : false);
10554 : : }
10555 : : else
10556 : : {
10557 : : /* In a redeclaration the type can already be the enumeral type. */
10558 : 5621101 : if (TREE_CODE (TREE_TYPE (value)) == ENUMERAL_TYPE)
10559 : 12 : value = convert (ENUM_UNDERLYING_TYPE (TREE_TYPE (value)), value);
10560 : 5621101 : the_enum->enum_next_value
10561 : 5621101 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10562 : : PLUS_EXPR, value, integer_one_node, false);
10563 : : }
10564 : 5621264 : the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
10565 : 5621264 : if (the_enum->enum_overflow
10566 : 5621264 : && !ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10567 : : {
10568 : : /* Choose a wider type with the same signedness if
10569 : : available. */
10570 : 3411 : int prec = TYPE_PRECISION (TREE_TYPE (value)) + 1;
10571 : 3411 : bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (value));
10572 : 3411 : tree new_type = (unsignedp
10573 : 3411 : ? long_unsigned_type_node
10574 : : : long_integer_type_node);
10575 : 3411 : if (prec > TYPE_PRECISION (new_type))
10576 : 3042 : new_type = (unsignedp
10577 : 3042 : ? long_long_unsigned_type_node
10578 : : : long_long_integer_type_node);
10579 : 3411 : if (prec > TYPE_PRECISION (new_type))
10580 : 2570 : new_type = (unsignedp
10581 : 2570 : ? widest_unsigned_literal_type_node
10582 : : : widest_integer_literal_type_node);
10583 : 3411 : if (prec <= TYPE_PRECISION (new_type))
10584 : : {
10585 : 3406 : the_enum->enum_overflow = false;
10586 : 3406 : the_enum->enum_next_value
10587 : 3406 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10588 : : PLUS_EXPR, convert (new_type, value),
10589 : : integer_one_node, false);
10590 : 3406 : gcc_assert (!tree_int_cst_lt (the_enum->enum_next_value, value));
10591 : : }
10592 : : }
10593 : :
10594 : : /* Now create a declaration for the enum value name. */
10595 : :
10596 : 5621264 : decl = build_decl (decl_loc, CONST_DECL, name, TREE_TYPE (value));
10597 : 5621264 : DECL_INITIAL (decl) = value;
10598 : 5621264 : DECL_CONTEXT (decl) = the_enum->enum_type;
10599 : 5621264 : pushdecl (decl);
10600 : :
10601 : 5621264 : return tree_cons (decl, value, NULL_TREE);
10602 : : }
10603 : :
10604 : : /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
10605 : :
10606 : : tree
10607 : 0 : c_simulate_enum_decl (location_t loc, const char *name,
10608 : : vec<string_int_pair> *values_ptr)
10609 : : {
10610 : 0 : location_t saved_loc = input_location;
10611 : 0 : input_location = loc;
10612 : :
10613 : 0 : struct c_enum_contents the_enum;
10614 : 0 : tree enumtype = start_enum (loc, &the_enum, get_identifier (name),
10615 : : NULL_TREE, false);
10616 : :
10617 : 0 : tree value_chain = NULL_TREE;
10618 : 0 : string_int_pair *value;
10619 : 0 : vec<string_int_pair> values = *values_ptr;
10620 : 0 : unsigned int i;
10621 : 0 : FOR_EACH_VEC_ELT (values, i, value)
10622 : : {
10623 : 0 : tree decl = build_enumerator (loc, loc, &the_enum,
10624 : : get_identifier (value->first),
10625 : : build_int_cst (integer_type_node,
10626 : 0 : value->second));
10627 : 0 : TREE_CHAIN (decl) = value_chain;
10628 : 0 : value_chain = decl;
10629 : : }
10630 : :
10631 : 0 : finish_enum (enumtype, nreverse (value_chain), NULL_TREE);
10632 : :
10633 : 0 : input_location = saved_loc;
10634 : 0 : return enumtype;
10635 : : }
10636 : :
10637 : : /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
10638 : :
10639 : : tree
10640 : 0 : c_simulate_record_decl (location_t loc, const char *name,
10641 : : array_slice<const tree> fields)
10642 : : {
10643 : 0 : location_t saved_loc = input_location;
10644 : 0 : input_location = loc;
10645 : :
10646 : 0 : class c_struct_parse_info *struct_info;
10647 : 0 : tree ident = get_identifier (name);
10648 : 0 : tree type = start_struct (loc, RECORD_TYPE, ident, &struct_info);
10649 : :
10650 : 0 : for (unsigned int i = 0; i < fields.size (); ++i)
10651 : : {
10652 : 0 : DECL_FIELD_CONTEXT (fields[i]) = type;
10653 : 0 : if (i > 0)
10654 : 0 : DECL_CHAIN (fields[i - 1]) = fields[i];
10655 : : }
10656 : :
10657 : 0 : finish_struct (loc, type, fields[0], NULL_TREE, struct_info);
10658 : :
10659 : 0 : tree decl = build_decl (loc, TYPE_DECL, ident, type);
10660 : 0 : set_underlying_type (decl);
10661 : 0 : lang_hooks.decls.pushdecl (decl);
10662 : :
10663 : 0 : input_location = saved_loc;
10664 : 0 : return type;
10665 : : }
10666 : :
10667 : : /* Create the FUNCTION_DECL for a function definition.
10668 : : DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
10669 : : the declaration; they describe the function's name and the type it returns,
10670 : : but twisted together in a fashion that parallels the syntax of C.
10671 : :
10672 : : This function creates a binding context for the function body
10673 : : as well as setting up the FUNCTION_DECL in current_function_decl.
10674 : :
10675 : : Returns true on success. If the DECLARATOR is not suitable for a function
10676 : : (it defines a datum instead), we return false to report a parse error. */
10677 : :
10678 : : bool
10679 : 36132668 : start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
10680 : : tree attributes)
10681 : : {
10682 : 36132668 : tree decl1, old_decl;
10683 : 36132668 : tree restype, resdecl;
10684 : 36132668 : location_t loc;
10685 : 36132668 : location_t result_loc;
10686 : 36132668 : tree expr = NULL;
10687 : :
10688 : 36132668 : current_function_returns_value = 0; /* Assume, until we see it does. */
10689 : 36132668 : current_function_returns_null = 0;
10690 : 36132668 : current_function_returns_abnormally = 0;
10691 : 36132668 : warn_about_return_type = 0;
10692 : 36132668 : c_switch_stack = NULL;
10693 : :
10694 : : /* Indicate no valid break/continue context. */
10695 : 36132668 : in_statement = 0;
10696 : :
10697 : 36132668 : decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
10698 : : &attributes, &expr, NULL, DEPRECATED_NORMAL);
10699 : 36132668 : invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
10700 : :
10701 : : /* If the declarator is not suitable for a function definition,
10702 : : cause a syntax error. */
10703 : 36132668 : if (decl1 == NULL_TREE
10704 : 36132637 : || TREE_CODE (decl1) != FUNCTION_DECL)
10705 : : return false;
10706 : :
10707 : : /* Nested functions may have variably modified (return) type.
10708 : : Make sure the size expression is evaluated at this point. */
10709 : 36132630 : if (expr && !current_scope->parm_flag)
10710 : 11 : add_stmt (fold_convert (void_type_node, expr));
10711 : :
10712 : 36132630 : loc = DECL_SOURCE_LOCATION (decl1);
10713 : :
10714 : : /* A nested function is not global. */
10715 : 36132630 : if (current_function_decl != NULL_TREE)
10716 : 1579 : TREE_PUBLIC (decl1) = 0;
10717 : :
10718 : 36132630 : c_decl_attributes (&decl1, attributes, 0);
10719 : :
10720 : 36132630 : if (DECL_DECLARED_INLINE_P (decl1)
10721 : 35443101 : && DECL_UNINLINABLE (decl1)
10722 : 36132640 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
10723 : : {
10724 : 3 : auto_urlify_attributes sentinel;
10725 : 3 : warning_at (loc, OPT_Wattributes,
10726 : : "inline function %qD given attribute %qs",
10727 : : decl1, "noinline");
10728 : 3 : }
10729 : :
10730 : : /* Handle gnu_inline attribute. */
10731 : 36132630 : if (declspecs->inline_p
10732 : 35443104 : && !flag_gnu89_inline
10733 : 35409072 : && TREE_CODE (decl1) == FUNCTION_DECL
10734 : 71541702 : && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
10735 : 157425 : || current_function_decl))
10736 : : {
10737 : 35251719 : if (declspecs->storage_class != csc_static)
10738 : 35251713 : DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
10739 : : }
10740 : :
10741 : 36132630 : announce_function (decl1);
10742 : :
10743 : 36132630 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
10744 : : {
10745 : 1 : error_at (loc, "return type is an incomplete type");
10746 : : /* Make it return void instead. */
10747 : 1 : TREE_TYPE (decl1)
10748 : 2 : = c_build_function_type (void_type_node,
10749 : 1 : TYPE_ARG_TYPES (TREE_TYPE (decl1)),
10750 : 1 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl1)));
10751 : : }
10752 : :
10753 : 36132630 : if (warn_about_return_type)
10754 : 1404 : permerror_opt (loc, flag_isoc99 ? OPT_Wimplicit_int
10755 : 0 : : (warn_return_type > 0 ? OPT_Wreturn_type
10756 : : : OPT_Wimplicit_int),
10757 : : "return type defaults to %<int%>");
10758 : :
10759 : : /* Make the init_value nonzero so pushdecl knows this is not tentative.
10760 : : error_mark_node is replaced below (in pop_scope) with the BLOCK. */
10761 : 36132630 : DECL_INITIAL (decl1) = error_mark_node;
10762 : :
10763 : : /* If this definition isn't a prototype and we had a prototype declaration
10764 : : before, copy the arg type info from that prototype. */
10765 : 36132630 : old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
10766 : 36132630 : if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
10767 : 35905082 : old_decl = NULL_TREE;
10768 : :
10769 : 36132630 : current_function_prototype_locus = UNKNOWN_LOCATION;
10770 : 36132630 : current_function_prototype_built_in = false;
10771 : 36132630 : current_function_prototype_arg_types = NULL_TREE;
10772 : 36132630 : tree newtype = TREE_TYPE (decl1);
10773 : 36132630 : tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype;
10774 : 36132630 : if (!prototype_p (newtype))
10775 : : {
10776 : 13117 : tree oldrt = TREE_TYPE (oldtype);
10777 : 13117 : tree newrt = TREE_TYPE (newtype);
10778 : 13117 : if (old_decl != NULL_TREE
10779 : 316 : && TREE_CODE (oldtype) == FUNCTION_TYPE
10780 : 13433 : && comptypes (oldrt, newrt))
10781 : : {
10782 : 297 : if (stdarg_p (oldtype))
10783 : : {
10784 : 1 : auto_diagnostic_group d;
10785 : 1 : warning_at (loc, 0, "%q+D defined as variadic function "
10786 : : "without prototype", decl1);
10787 : 1 : locate_old_decl (old_decl);
10788 : 1 : }
10789 : 297 : TREE_TYPE (decl1) = composite_type (oldtype, newtype);
10790 : 297 : current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
10791 : 297 : current_function_prototype_built_in
10792 : 297 : = C_DECL_BUILTIN_PROTOTYPE (old_decl);
10793 : 297 : current_function_prototype_arg_types
10794 : 297 : = TYPE_ARG_TYPES (newtype);
10795 : : }
10796 : 13117 : if (TREE_PUBLIC (decl1))
10797 : : {
10798 : : /* If there is an external prototype declaration of this
10799 : : function, record its location but do not copy information
10800 : : to this decl. This may be an invisible declaration
10801 : : (built-in or in a scope which has finished) or simply
10802 : : have more refined argument types than any declaration
10803 : : found above. */
10804 : 12702 : struct c_binding *b;
10805 : 12977 : for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
10806 : 711 : if (B_IN_SCOPE (b, external_scope))
10807 : : break;
10808 : 12702 : if (b)
10809 : : {
10810 : 436 : tree ext_decl, ext_type;
10811 : 436 : ext_decl = b->decl;
10812 : 436 : ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
10813 : 436 : if (TREE_CODE (ext_type) == FUNCTION_TYPE
10814 : 872 : && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
10815 : 436 : TREE_TYPE (ext_type)))
10816 : : {
10817 : 416 : current_function_prototype_locus
10818 : 416 : = DECL_SOURCE_LOCATION (ext_decl);
10819 : 416 : current_function_prototype_built_in
10820 : 416 : = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
10821 : 416 : current_function_prototype_arg_types
10822 : 416 : = TYPE_ARG_TYPES (ext_type);
10823 : : }
10824 : : }
10825 : : }
10826 : : }
10827 : :
10828 : : /* Optionally warn about C23 compatibility. */
10829 : 36132630 : if (warn_deprecated_non_prototype
10830 : 34 : && old_decl != NULL_TREE
10831 : 5 : && TREE_CODE (oldtype) == FUNCTION_TYPE
10832 : 5 : && !TYPE_ARG_TYPES (oldtype)
10833 : 5 : && !TYPE_NO_NAMED_ARGS_STDARG_P (oldtype)
10834 : 36132635 : && (TYPE_ARG_TYPES (newtype)
10835 : 3 : && TREE_VALUE (TYPE_ARG_TYPES (newtype)) != void_type_node))
10836 : : {
10837 : 3 : bool warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
10838 : : "ISO C23 does not allow defining"
10839 : : " parameters for function %qE declared"
10840 : : " without parameters",
10841 : : decl1);
10842 : 3 : if (warned)
10843 : 3 : inform (DECL_SOURCE_LOCATION (old_decl), "declared here");
10844 : : }
10845 : :
10846 : : /* Optionally warn of old-fashioned def with no previous prototype. */
10847 : 36132630 : if (warn_strict_prototypes
10848 : 107416 : && old_decl != error_mark_node
10849 : 107416 : && !prototype_p (TREE_TYPE (decl1))
10850 : 36132631 : && C_DECL_ISNT_PROTOTYPE (old_decl))
10851 : 1 : warning_at (loc, OPT_Wstrict_prototypes,
10852 : : "function declaration isn%'t a prototype");
10853 : : /* Optionally warn of any global def with no previous prototype. */
10854 : 36132629 : else if (warn_missing_prototypes
10855 : 107117 : && old_decl != error_mark_node
10856 : 107117 : && TREE_PUBLIC (decl1)
10857 : 73996 : && !MAIN_NAME_P (DECL_NAME (decl1))
10858 : 73960 : && C_DECL_ISNT_PROTOTYPE (old_decl)
10859 : 36133270 : && !DECL_DECLARED_INLINE_P (decl1))
10860 : 4 : warning_at (loc, OPT_Wmissing_prototypes,
10861 : : "no previous prototype for %qD", decl1);
10862 : : /* Optionally warn of any def with no previous prototype
10863 : : if the function has already been used. */
10864 : 36132625 : else if (warn_missing_prototypes
10865 : 107113 : && old_decl != NULL_TREE
10866 : 74323 : && old_decl != error_mark_node
10867 : 74323 : && TREE_USED (old_decl)
10868 : 36139718 : && !prototype_p (TREE_TYPE (old_decl)))
10869 : 0 : warning_at (loc, OPT_Wmissing_prototypes,
10870 : : "%qD was used with no prototype before its definition", decl1);
10871 : : /* Optionally warn of any global def with no previous declaration. */
10872 : 36132625 : else if (warn_missing_declarations
10873 : 4 : && TREE_PUBLIC (decl1)
10874 : 3 : && old_decl == NULL_TREE
10875 : 3 : && !MAIN_NAME_P (DECL_NAME (decl1))
10876 : 36132628 : && !DECL_DECLARED_INLINE_P (decl1))
10877 : 2 : warning_at (loc, OPT_Wmissing_declarations,
10878 : : "no previous declaration for %qD",
10879 : : decl1);
10880 : : /* Optionally warn of any def with no previous declaration
10881 : : if the function has already been used. */
10882 : 36132623 : else if (warn_missing_declarations
10883 : 2 : && old_decl != NULL_TREE
10884 : 0 : && old_decl != error_mark_node
10885 : 0 : && TREE_USED (old_decl)
10886 : 36132623 : && C_DECL_IMPLICIT (old_decl))
10887 : 0 : warning_at (loc, OPT_Wmissing_declarations,
10888 : : "%qD was used with no declaration before its definition", decl1);
10889 : :
10890 : : /* This function exists in static storage.
10891 : : (This does not mean `static' in the C sense!) */
10892 : 36132630 : TREE_STATIC (decl1) = 1;
10893 : :
10894 : : /* This is the earliest point at which we might know the assembler
10895 : : name of the function. Thus, if it's set before this, die horribly. */
10896 : 36132630 : gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
10897 : :
10898 : : /* If #pragma weak was used, mark the decl weak now. */
10899 : 36132630 : if (current_scope == file_scope)
10900 : 36131051 : maybe_apply_pragma_weak (decl1);
10901 : :
10902 : : /* Warn for unlikely, improbable, or stupid declarations of `main'. */
10903 : 36132630 : if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
10904 : : {
10905 : 3208 : if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
10906 : 3208 : != integer_type_node)
10907 : 5 : pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
10908 : 3203 : else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
10909 : 1 : pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
10910 : : decl1);
10911 : :
10912 : 3208 : check_main_parameter_types (decl1);
10913 : :
10914 : 3208 : if (!TREE_PUBLIC (decl1))
10915 : 0 : pedwarn (loc, OPT_Wmain,
10916 : : "%qD is normally a non-static function", decl1);
10917 : : }
10918 : :
10919 : 36132630 : tree parms = current_function_arg_info->parms;
10920 : 36132630 : if (old_decl)
10921 : : {
10922 : 227548 : location_t origloc = DECL_SOURCE_LOCATION (old_decl);
10923 : 227548 : warn_parms_array_mismatch (origloc, old_decl, parms);
10924 : : }
10925 : :
10926 : : /* To enable versions to be created across TU's we mark and mangle all
10927 : : non-default versioned functions. */
10928 : 36132630 : if (TREE_CODE (decl1) == FUNCTION_DECL
10929 : : && !TARGET_HAS_FMV_TARGET_ATTRIBUTE
10930 : : && get_target_version (decl1).is_valid ())
10931 : : {
10932 : : maybe_mark_function_versioned (decl1);
10933 : : if (current_scope != file_scope)
10934 : : error ("versioned definitions are only allowed at file scope");
10935 : : }
10936 : :
10937 : : /* Record the decl so that the function name is defined.
10938 : : If we already have a decl for this name, and it is a FUNCTION_DECL,
10939 : : use the old decl. */
10940 : :
10941 : 36132630 : current_function_decl = pushdecl (decl1);
10942 : :
10943 : 36132630 : if (tree access = build_attr_access_from_parms (parms, false))
10944 : 57435 : decl_attributes (¤t_function_decl, access, ATTR_FLAG_INTERNAL,
10945 : : old_decl);
10946 : :
10947 : 36132630 : push_scope ();
10948 : 36132630 : declare_parm_level ();
10949 : :
10950 : : /* Set the result decl source location to the location of the typespec. */
10951 : 4087224 : result_loc = (declspecs->locations[cdw_typespec] == UNKNOWN_LOCATION
10952 : 36132630 : ? loc : declspecs->locations[cdw_typespec]);
10953 : 36132630 : restype = TREE_TYPE (TREE_TYPE (current_function_decl));
10954 : 36132630 : resdecl = build_decl (result_loc, RESULT_DECL, NULL_TREE, restype);
10955 : 36132630 : DECL_ARTIFICIAL (resdecl) = 1;
10956 : 36132630 : DECL_IGNORED_P (resdecl) = 1;
10957 : 36132630 : DECL_RESULT (current_function_decl) = resdecl;
10958 : :
10959 : 36132630 : start_fname_decls ();
10960 : :
10961 : 36132630 : return true;
10962 : : }
10963 : :
10964 : : /* Subroutine of store_parm_decls which handles new-style function
10965 : : definitions (prototype format). The parms already have decls, so we
10966 : : need only record them as in effect and complain if any redundant
10967 : : old-style parm decls were written. */
10968 : : static void
10969 : 36119526 : store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
10970 : : {
10971 : 36119526 : tree decl;
10972 : 36119526 : c_arg_tag *tag;
10973 : 36119526 : unsigned ix;
10974 : :
10975 : 36119526 : if (current_scope->bindings)
10976 : : {
10977 : 7 : error_at (DECL_SOURCE_LOCATION (fndecl),
10978 : : "old-style parameter declarations in prototyped "
10979 : : "function definition");
10980 : :
10981 : : /* Get rid of the old-style declarations. */
10982 : 7 : pop_scope ();
10983 : 7 : push_scope ();
10984 : : }
10985 : : /* Don't issue this warning for nested functions, and don't issue this
10986 : : warning if we got here because ARG_INFO_TYPES was error_mark_node
10987 : : (this happens when a function definition has just an ellipsis in
10988 : : its parameter list). */
10989 : 36119519 : else if (!in_system_header_at (input_location)
10990 : 639453 : && !current_function_scope
10991 : 638021 : && arg_info->types != error_mark_node
10992 : 36757540 : && !arg_info->c23_empty_parens)
10993 : 585642 : warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
10994 : : "traditional C rejects ISO C style function definitions");
10995 : :
10996 : : /* Now make all the parameter declarations visible in the function body.
10997 : : We can bypass most of the grunt work of pushdecl. */
10998 : 135671147 : for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
10999 : : {
11000 : 99551621 : DECL_CONTEXT (decl) = current_function_decl;
11001 : 99551621 : if (DECL_NAME (decl))
11002 : : {
11003 : 99551071 : bind (DECL_NAME (decl), decl, current_scope,
11004 : : /*invisible=*/false, /*nested=*/false,
11005 : : UNKNOWN_LOCATION);
11006 : 99551071 : if (!TREE_USED (decl))
11007 : 99533826 : warn_if_shadowing (decl);
11008 : : }
11009 : : else
11010 : 550 : pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_parameter_name,
11011 : : "ISO C does not support omitting parameter names in "
11012 : : "function definitions before C23");
11013 : : }
11014 : :
11015 : : /* Record the parameter list in the function declaration. */
11016 : 36119526 : DECL_ARGUMENTS (fndecl) = arg_info->parms;
11017 : :
11018 : : /* Now make all the ancillary declarations visible, likewise. */
11019 : 36119582 : for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
11020 : : {
11021 : 56 : DECL_CONTEXT (decl) = current_function_decl;
11022 : 56 : if (DECL_NAME (decl))
11023 : 0 : bind (DECL_NAME (decl), decl, current_scope,
11024 : : /*invisible=*/false,
11025 : 0 : /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
11026 : : UNKNOWN_LOCATION);
11027 : : }
11028 : :
11029 : : /* And all the tag declarations. */
11030 : 36119636 : FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
11031 : 56 : if (tag->id)
11032 : 23 : bind (tag->id, tag->type, current_scope,
11033 : : /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
11034 : 36119526 : }
11035 : :
11036 : : /* Subroutine of store_parm_decls which handles old-style function
11037 : : definitions (separate parameter list and declarations). */
11038 : :
11039 : : static void
11040 : 13104 : store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
11041 : : {
11042 : 13104 : struct c_binding *b;
11043 : 13104 : tree parm, decl, last;
11044 : 13104 : tree parmids = arg_info->parms;
11045 : 13104 : hash_set<tree> seen_args;
11046 : :
11047 : 13104 : if (!in_system_header_at (input_location))
11048 : : {
11049 : 13101 : if (flag_isoc23)
11050 : 1349 : pedwarn (DECL_SOURCE_LOCATION (fndecl),
11051 : 1349 : OPT_Wold_style_definition, "old-style function definition");
11052 : : else
11053 : 11752 : warning_at (DECL_SOURCE_LOCATION (fndecl),
11054 : 11752 : OPT_Wold_style_definition,
11055 : : "old-style function definition");
11056 : : }
11057 : :
11058 : 13104 : if (current_scope->had_vla_unspec)
11059 : 1 : error ("%<[*]%> not allowed in other than function prototype scope");
11060 : :
11061 : : /* Match each formal parameter name with its declaration. Save each
11062 : : decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
11063 : 47373 : for (parm = parmids; parm; parm = TREE_CHAIN (parm))
11064 : : {
11065 : 34269 : if (TREE_VALUE (parm) == NULL_TREE)
11066 : : {
11067 : 0 : error_at (DECL_SOURCE_LOCATION (fndecl),
11068 : : "parameter name missing from parameter list");
11069 : 0 : TREE_PURPOSE (parm) = NULL_TREE;
11070 : 0 : continue;
11071 : : }
11072 : :
11073 : 34269 : b = I_SYMBOL_BINDING (TREE_VALUE (parm));
11074 : 34269 : if (b && B_IN_CURRENT_SCOPE (b))
11075 : : {
11076 : 22649 : decl = b->decl;
11077 : : /* Skip erroneous parameters. */
11078 : 22649 : if (decl == error_mark_node)
11079 : 2 : continue;
11080 : : /* If we got something other than a PARM_DECL it is an error. */
11081 : 22647 : if (TREE_CODE (decl) != PARM_DECL)
11082 : : {
11083 : 7 : error_at (DECL_SOURCE_LOCATION (decl),
11084 : : "%qD declared as a non-parameter", decl);
11085 : 7 : continue;
11086 : : }
11087 : : /* If the declaration is already marked, we have a duplicate
11088 : : name. Complain and ignore the duplicate. */
11089 : 22640 : else if (seen_args.contains (decl))
11090 : : {
11091 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
11092 : : "multiple parameters named %qD", decl);
11093 : 0 : TREE_PURPOSE (parm) = NULL_TREE;
11094 : 0 : continue;
11095 : : }
11096 : : /* If the declaration says "void", complain and turn it into
11097 : : an int. */
11098 : 22640 : else if (VOID_TYPE_P (TREE_TYPE (decl)))
11099 : : {
11100 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
11101 : : "parameter %qD declared with void type", decl);
11102 : 0 : TREE_TYPE (decl) = integer_type_node;
11103 : 0 : DECL_ARG_TYPE (decl) = integer_type_node;
11104 : 0 : layout_decl (decl, 0);
11105 : : }
11106 : 22640 : warn_if_shadowing (decl);
11107 : : }
11108 : : /* If no declaration found, default to int. */
11109 : : else
11110 : : {
11111 : : /* FIXME diagnostics: This should be the location of the argument,
11112 : : not the FNDECL. E.g., for an old-style declaration
11113 : :
11114 : : int f10(v) { blah; }
11115 : :
11116 : : We should use the location of the V, not the F10.
11117 : : Unfortunately, the V is an IDENTIFIER_NODE which has no
11118 : : location. In the future we need locations for c_arg_info
11119 : : entries.
11120 : :
11121 : : See gcc.dg/Wshadow-3.c for an example of this problem. */
11122 : 11620 : decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
11123 : 11620 : PARM_DECL, TREE_VALUE (parm), integer_type_node);
11124 : 11620 : DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
11125 : 11620 : pushdecl (decl);
11126 : 11620 : warn_if_shadowing (decl);
11127 : :
11128 : 11620 : if (flag_isoc99)
11129 : 118 : permerror_opt (DECL_SOURCE_LOCATION (decl),
11130 : 118 : OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
11131 : : decl);
11132 : : else
11133 : 11502 : warning_at (DECL_SOURCE_LOCATION (decl),
11134 : 11502 : OPT_Wmissing_parameter_type,
11135 : : "type of %qD defaults to %<int%>", decl);
11136 : : }
11137 : :
11138 : 34260 : TREE_PURPOSE (parm) = decl;
11139 : 34260 : seen_args.add (decl);
11140 : : }
11141 : :
11142 : : /* Now examine the parms chain for incomplete declarations
11143 : : and declarations with no corresponding names. */
11144 : :
11145 : 47452 : for (b = current_scope->bindings; b; b = b->prev)
11146 : : {
11147 : 34348 : parm = b->decl;
11148 : 34348 : if (TREE_CODE (parm) != PARM_DECL)
11149 : 79 : continue;
11150 : :
11151 : 34269 : if (TREE_TYPE (parm) != error_mark_node
11152 : 34269 : && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
11153 : : {
11154 : 0 : error_at (DECL_SOURCE_LOCATION (parm),
11155 : : "parameter %qD has incomplete type", parm);
11156 : 0 : TREE_TYPE (parm) = error_mark_node;
11157 : : }
11158 : :
11159 : 34269 : if (!seen_args.contains (parm))
11160 : : {
11161 : 9 : error_at (DECL_SOURCE_LOCATION (parm),
11162 : : "declaration for parameter %qD but no such parameter",
11163 : : parm);
11164 : :
11165 : : /* Pretend the parameter was not missing.
11166 : : This gets us to a standard state and minimizes
11167 : : further error messages. */
11168 : 9 : parmids = chainon (parmids, tree_cons (parm, 0, 0));
11169 : : }
11170 : : }
11171 : :
11172 : : /* Chain the declarations together in the order of the list of
11173 : : names. Store that chain in the function decl, replacing the
11174 : : list of names. Update the current scope to match. */
11175 : 13104 : DECL_ARGUMENTS (fndecl) = NULL_TREE;
11176 : :
11177 : 13112 : for (parm = parmids; parm; parm = TREE_CHAIN (parm))
11178 : 8680 : if (TREE_PURPOSE (parm))
11179 : : break;
11180 : 13104 : if (parm && TREE_PURPOSE (parm))
11181 : : {
11182 : 8672 : last = TREE_PURPOSE (parm);
11183 : 8672 : DECL_ARGUMENTS (fndecl) = last;
11184 : :
11185 : 34270 : for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
11186 : 25598 : if (TREE_PURPOSE (parm))
11187 : : {
11188 : 25597 : DECL_CHAIN (last) = TREE_PURPOSE (parm);
11189 : 25598 : last = TREE_PURPOSE (parm);
11190 : : }
11191 : 8672 : DECL_CHAIN (last) = NULL_TREE;
11192 : : }
11193 : :
11194 : : /* If there was a previous prototype,
11195 : : set the DECL_ARG_TYPE of each argument according to
11196 : : the type previously specified, and report any mismatches. */
11197 : :
11198 : 13104 : if (current_function_prototype_arg_types)
11199 : : {
11200 : 171 : tree type;
11201 : 171 : for (parm = DECL_ARGUMENTS (fndecl),
11202 : 171 : type = current_function_prototype_arg_types;
11203 : 373 : parm || (type != NULL_TREE
11204 : 166 : && TREE_VALUE (type) != error_mark_node
11205 : 166 : && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
11206 : 202 : parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
11207 : : {
11208 : 212 : if (parm == NULL_TREE
11209 : 206 : || type == NULL_TREE
11210 : 418 : || (TREE_VALUE (type) != error_mark_node
11211 : 192 : && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
11212 : : {
11213 : 10 : if (current_function_prototype_built_in)
11214 : 6 : warning_at (DECL_SOURCE_LOCATION (fndecl),
11215 : 6 : 0, "number of arguments doesn%'t match "
11216 : : "built-in prototype");
11217 : : else
11218 : : {
11219 : : /* FIXME diagnostics: This should be the location of
11220 : : FNDECL, but there is bug when a prototype is
11221 : : declared inside function context, but defined
11222 : : outside of it (e.g., gcc.dg/pr15698-2.c). In
11223 : : which case FNDECL gets the location of the
11224 : : prototype, not the definition. */
11225 : 4 : error_at (input_location,
11226 : : "number of arguments doesn%'t match prototype");
11227 : :
11228 : 4 : error_at (current_function_prototype_locus,
11229 : : "prototype declaration");
11230 : : }
11231 : : break;
11232 : : }
11233 : : /* Type for passing arg must be consistent with that
11234 : : declared for the arg. ISO C says we take the unqualified
11235 : : type for parameters declared with qualified type. */
11236 : 202 : if (TREE_TYPE (parm) != error_mark_node
11237 : 201 : && TREE_VALUE (type) != error_mark_node
11238 : 389 : && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
11239 : 187 : != TYPE_ATOMIC (TREE_VALUE (type)))
11240 : 185 : || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
11241 : 185 : TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
11242 : : {
11243 : 36 : if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
11244 : 36 : == TYPE_ATOMIC (TREE_VALUE (type)))
11245 : 36 : && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
11246 : 34 : == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
11247 : : {
11248 : : /* Adjust argument to match prototype. E.g. a previous
11249 : : `int foo(float);' prototype causes
11250 : : `int foo(x) float x; {...}' to be treated like
11251 : : `int foo(float x) {...}'. This is particularly
11252 : : useful for argument types like uid_t. */
11253 : 17 : DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
11254 : :
11255 : : /* ??? Is it possible to get here with a
11256 : : built-in prototype or will it always have
11257 : : been diagnosed as conflicting with an
11258 : : old-style definition and discarded? */
11259 : 17 : if (current_function_prototype_built_in)
11260 : 2 : warning_at (DECL_SOURCE_LOCATION (parm),
11261 : 2 : OPT_Wpedantic, "promoted argument %qD "
11262 : : "doesn%'t match built-in prototype", parm);
11263 : : else
11264 : : {
11265 : 15 : pedwarn (DECL_SOURCE_LOCATION (parm),
11266 : 15 : OPT_Wpedantic, "promoted argument %qD "
11267 : : "doesn%'t match prototype", parm);
11268 : 15 : pedwarn (current_function_prototype_locus, OPT_Wpedantic,
11269 : : "prototype declaration");
11270 : : }
11271 : : }
11272 : : else
11273 : : {
11274 : 19 : if (current_function_prototype_built_in)
11275 : 10 : warning_at (DECL_SOURCE_LOCATION (parm),
11276 : 10 : 0, "argument %qD doesn%'t match "
11277 : : "built-in prototype", parm);
11278 : : else
11279 : : {
11280 : 9 : error_at (DECL_SOURCE_LOCATION (parm),
11281 : : "argument %qD doesn%'t match prototype", parm);
11282 : 9 : error_at (current_function_prototype_locus,
11283 : : "prototype declaration");
11284 : : }
11285 : : }
11286 : : }
11287 : : }
11288 : 171 : TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
11289 : : }
11290 : :
11291 : : /* Otherwise, create a prototype that would match. */
11292 : :
11293 : : else
11294 : : {
11295 : 12933 : tree actual = NULL_TREE, last = NULL_TREE, type;
11296 : :
11297 : 46996 : for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
11298 : : {
11299 : 34063 : type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
11300 : 34063 : if (last)
11301 : 25547 : TREE_CHAIN (last) = type;
11302 : : else
11303 : : actual = type;
11304 : 34063 : last = type;
11305 : : }
11306 : 12933 : type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
11307 : 12933 : if (last)
11308 : 8516 : TREE_CHAIN (last) = type;
11309 : : else
11310 : : actual = type;
11311 : :
11312 : : /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
11313 : : of the type of this function, but we need to avoid having this
11314 : : affect the types of other similarly-typed functions, so we must
11315 : : first force the generation of an identical (but separate) type
11316 : : node for the relevant function type. The new node we create
11317 : : will be a variant of the main variant of the original function
11318 : : type. */
11319 : :
11320 : 12933 : TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
11321 : :
11322 : 12933 : TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
11323 : : }
11324 : 13104 : }
11325 : :
11326 : : /* Store parameter declarations passed in ARG_INFO into the current
11327 : : function declaration. */
11328 : :
11329 : : void
11330 : 0 : store_parm_decls_from (struct c_arg_info *arg_info)
11331 : : {
11332 : 0 : current_function_arg_info = arg_info;
11333 : 0 : store_parm_decls ();
11334 : 0 : }
11335 : :
11336 : : /* Called by walk_tree to look for and update context-less labels
11337 : : or labels with context in the parent function. */
11338 : :
11339 : : static tree
11340 : 8127 : set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
11341 : : {
11342 : 8127 : tree ctx = static_cast<tree>(data);
11343 : 8127 : if (TREE_CODE (*tp) == LABEL_EXPR
11344 : 8127 : && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
11345 : 2 : || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
11346 : : {
11347 : 72 : DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
11348 : 72 : *walk_subtrees = 0;
11349 : : }
11350 : :
11351 : 8127 : return NULL_TREE;
11352 : : }
11353 : :
11354 : : /* Store the parameter declarations into the current function declaration.
11355 : : This is called after parsing the parameter declarations, before
11356 : : digesting the body of the function.
11357 : :
11358 : : For an old-style definition, construct a prototype out of the old-style
11359 : : parameter declarations and inject it into the function's type. */
11360 : :
11361 : : void
11362 : 36132630 : store_parm_decls (void)
11363 : : {
11364 : 36132630 : tree fndecl = current_function_decl;
11365 : 36132630 : bool proto;
11366 : :
11367 : : /* The argument information block for FNDECL. */
11368 : 36132630 : struct c_arg_info *arg_info = current_function_arg_info;
11369 : 36132630 : current_function_arg_info = 0;
11370 : :
11371 : : /* True if this definition is written with a prototype. In C23, an
11372 : : empty argument list was converted to (void) in grokparms; in
11373 : : older C standard versions, it does not give the function a type
11374 : : with a prototype for future calls. */
11375 : 36132630 : proto = arg_info->types != 0 || arg_info->no_named_args_stdarg_p;
11376 : :
11377 : 13104 : if (proto)
11378 : 36119526 : store_parm_decls_newstyle (fndecl, arg_info);
11379 : : else
11380 : 13104 : store_parm_decls_oldstyle (fndecl, arg_info);
11381 : :
11382 : : /* The next call to push_scope will be a function body. */
11383 : :
11384 : 36132630 : next_is_function_body = true;
11385 : :
11386 : : /* Write a record describing this function definition to the prototypes
11387 : : file (if requested). */
11388 : :
11389 : 36132630 : gen_aux_info_record (fndecl, 1, 0, proto);
11390 : :
11391 : : /* Initialize the RTL code for the function. */
11392 : 36132630 : allocate_struct_function (fndecl, false);
11393 : :
11394 : 36132630 : if (warn_unused_local_typedefs)
11395 : 3109422 : cfun->language = ggc_cleared_alloc<language_function> ();
11396 : :
11397 : : /* Begin the statement tree for this function. */
11398 : 36132630 : DECL_SAVED_TREE (fndecl) = push_stmt_list ();
11399 : :
11400 : : /* ??? Insert the contents of the pending sizes list into the function
11401 : : to be evaluated. The only reason left to have this is
11402 : : void foo(int n, int array[n++])
11403 : : because we throw away the array type in favor of a pointer type, and
11404 : : thus won't naturally see the SAVE_EXPR containing the increment. All
11405 : : other pending sizes would be handled by gimplify_parameters. */
11406 : 36132630 : if (arg_info->pending_sizes)
11407 : : {
11408 : : /* In very special circumstances, e.g. for code like
11409 : : _Atomic int i = 5;
11410 : : void f (int a[i += 2]) {}
11411 : : we need to execute the atomic assignment on function entry.
11412 : : But in this case, it is not just a straight store, it has the
11413 : : op= form, which means that build_atomic_assign has generated
11414 : : gotos, labels, etc. Because at that time the function decl
11415 : : for F has not been created yet, those labels do not have any
11416 : : function context. But we have the fndecl now, so update the
11417 : : labels accordingly. gimplify_expr would crash otherwise.
11418 : : Or with nested functions the labels could be created with parent
11419 : : function's context, while when the statement is emitted at the
11420 : : start of the nested function, it needs the nested function's
11421 : : context. */
11422 : 295 : walk_tree_without_duplicates (&arg_info->pending_sizes,
11423 : : set_labels_context_r, fndecl);
11424 : 295 : add_stmt (arg_info->pending_sizes);
11425 : : }
11426 : 36132630 : }
11427 : :
11428 : : /* Store PARM_DECLs in PARMS into scope temporarily. Used for
11429 : : c_finish_omp_declare_simd for function prototypes. No diagnostics
11430 : : should be done. */
11431 : :
11432 : : void
11433 : 244 : temp_store_parm_decls (tree fndecl, tree parms)
11434 : : {
11435 : 244 : push_scope ();
11436 : 688 : for (tree p = parms; p; p = DECL_CHAIN (p))
11437 : : {
11438 : 444 : DECL_CONTEXT (p) = fndecl;
11439 : 444 : if (DECL_NAME (p))
11440 : 325 : bind (DECL_NAME (p), p, current_scope,
11441 : : /*invisible=*/false, /*nested=*/false,
11442 : : UNKNOWN_LOCATION);
11443 : : }
11444 : 244 : }
11445 : :
11446 : : /* Undo what temp_store_parm_decls did. */
11447 : :
11448 : : void
11449 : 244 : temp_pop_parm_decls (void)
11450 : : {
11451 : : /* Clear all bindings in this temporary scope, so that
11452 : : pop_scope doesn't create a BLOCK. */
11453 : 244 : struct c_binding *b = current_scope->bindings;
11454 : 244 : current_scope->bindings = NULL;
11455 : 574 : for (; b; b = free_binding_and_advance (b))
11456 : : {
11457 : 330 : gcc_assert (TREE_CODE (b->decl) == PARM_DECL
11458 : : || b->decl == error_mark_node);
11459 : 330 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
11460 : 330 : I_SYMBOL_BINDING (b->id) = b->shadowed;
11461 : 330 : if (b->shadowed && b->shadowed->u.type)
11462 : 0 : TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
11463 : : }
11464 : 244 : pop_scope ();
11465 : 244 : }
11466 : :
11467 : :
11468 : : /* Finish up a function declaration and compile that function
11469 : : all the way to assembler language output. Then free the storage
11470 : : for the function definition.
11471 : :
11472 : : This is called after parsing the body of the function definition. */
11473 : :
11474 : : void
11475 : 36132628 : finish_function (location_t end_loc)
11476 : : {
11477 : 36132628 : tree fndecl = current_function_decl;
11478 : :
11479 : 36132628 : if (c_dialect_objc ())
11480 : 0 : objc_finish_function ();
11481 : :
11482 : 36132628 : if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
11483 : 36132626 : BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
11484 : :
11485 : : /* Must mark the RESULT_DECL as being in this function. */
11486 : :
11487 : 36132628 : if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
11488 : 36132628 : DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
11489 : :
11490 : 36181423 : if (MAIN_NAME_P (DECL_NAME (fndecl)) && !TREE_THIS_VOLATILE (fndecl)
11491 : 48794 : && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
11492 : 36181422 : == integer_type_node && flag_isoc99)
11493 : : {
11494 : : /* Hack. We don't want the middle-end to warn that this return
11495 : : is unreachable, so we mark its location as special. Using
11496 : : UNKNOWN_LOCATION has the problem that it gets clobbered in
11497 : : annotate_one_with_locus. A cleaner solution might be to
11498 : : ensure ! should_carry_locus_p (stmt), but that needs a flag.
11499 : : */
11500 : 47414 : c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
11501 : : }
11502 : :
11503 : : /* Tie off the statement tree for this function. */
11504 : 36132628 : DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
11505 : :
11506 : 36132628 : finish_fname_decls ();
11507 : :
11508 : : /* Complain if there's no return statement only if option specified on
11509 : : command line. */
11510 : 36132628 : if (warn_return_type > 0
11511 : 3109445 : && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
11512 : 2913587 : && !current_function_returns_value && !current_function_returns_null
11513 : : /* Don't complain if we are no-return. */
11514 : 72 : && !current_function_returns_abnormally
11515 : : /* Don't complain if we are declared noreturn. */
11516 : 21 : && !TREE_THIS_VOLATILE (fndecl)
11517 : : /* Don't warn for main(). */
11518 : 18 : && !MAIN_NAME_P (DECL_NAME (fndecl))
11519 : : /* Or if they didn't actually specify a return type. */
11520 : 17 : && !C_FUNCTION_IMPLICIT_INT (fndecl)
11521 : : /* Normally, with -Wreturn-type, flow will complain, but we might
11522 : : optimize out static functions. */
11523 : 17 : && !TREE_PUBLIC (fndecl)
11524 : 6 : && targetm.warn_func_return (fndecl)
11525 : 39242073 : && warning (OPT_Wreturn_type,
11526 : : "no return statement in function returning non-void"))
11527 : 5 : suppress_warning (fndecl, OPT_Wreturn_type);
11528 : :
11529 : : /* Complain about parameters that are only set, but never otherwise used. */
11530 : 36132628 : if (warn_unused_but_set_parameter)
11531 : : {
11532 : 3045034 : tree decl;
11533 : :
11534 : 3045034 : for (decl = DECL_ARGUMENTS (fndecl);
11535 : 11380820 : decl;
11536 : 8335786 : decl = DECL_CHAIN (decl))
11537 : 8335786 : if (TREE_USED (decl)
11538 : 8275083 : && TREE_CODE (decl) == PARM_DECL
11539 : 8275083 : && !DECL_READ_P (decl)
11540 : 47 : && DECL_NAME (decl)
11541 : 47 : && !DECL_ARTIFICIAL (decl)
11542 : 8335833 : && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter_))
11543 : 47 : warning_at (DECL_SOURCE_LOCATION (decl),
11544 : 47 : OPT_Wunused_but_set_parameter_,
11545 : : "parameter %qD set but not used", decl);
11546 : : }
11547 : :
11548 : : /* Complain about locally defined typedefs that are not used in this
11549 : : function. */
11550 : 36132628 : maybe_warn_unused_local_typedefs ();
11551 : :
11552 : : /* Possibly warn about unused parameters. */
11553 : 36132628 : if (warn_unused_parameter)
11554 : 2951145 : do_warn_unused_parameter (fndecl);
11555 : :
11556 : : /* Store the end of the function, so that we get good line number
11557 : : info for the epilogue. */
11558 : 36132628 : cfun->function_end_locus = end_loc;
11559 : :
11560 : : /* Finalize the ELF visibility for the function. */
11561 : 36132628 : c_determine_visibility (fndecl);
11562 : :
11563 : : /* For GNU C extern inline functions disregard inline limits. */
11564 : 36132628 : if (DECL_EXTERNAL (fndecl)
11565 : 35280237 : && DECL_DECLARED_INLINE_P (fndecl)
11566 : 71412862 : && (flag_gnu89_inline
11567 : 35249295 : || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
11568 : 35279762 : DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
11569 : :
11570 : : /* Genericize before inlining. Delay genericizing nested functions
11571 : : until their parent function is genericized. Since finalizing
11572 : : requires GENERIC, delay that as well. */
11573 : :
11574 : 72265256 : if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
11575 : 72265254 : && !undef_nested_function)
11576 : : {
11577 : 36132620 : if (!decl_function_context (fndecl))
11578 : : {
11579 : 36131041 : invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
11580 : 36131041 : c_genericize (fndecl);
11581 : :
11582 : : /* ??? Objc emits functions after finalizing the compilation unit.
11583 : : This should be cleaned up later and this conditional removed. */
11584 : 36131041 : if (symtab->global_info_ready)
11585 : : {
11586 : 0 : cgraph_node::add_new_function (fndecl, false);
11587 : 0 : return;
11588 : : }
11589 : 36131041 : cgraph_node::finalize_function (fndecl, false);
11590 : : }
11591 : : else
11592 : : {
11593 : : /* Register this function with cgraph just far enough to get it
11594 : : added to our parent's nested function list. Handy, since the
11595 : : C front end doesn't have such a list. */
11596 : 1579 : (void) cgraph_node::get_create (fndecl);
11597 : : }
11598 : : }
11599 : :
11600 : 36132628 : if (!decl_function_context (fndecl))
11601 : 36131049 : undef_nested_function = false;
11602 : :
11603 : 36132628 : if (cfun->language != NULL)
11604 : : {
11605 : 3109421 : ggc_free (cfun->language);
11606 : 3109421 : cfun->language = NULL;
11607 : : }
11608 : :
11609 : : /* We're leaving the context of this function, so zap cfun.
11610 : : It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
11611 : : tree_rest_of_compilation. */
11612 : 36132628 : set_cfun (NULL);
11613 : 36132628 : invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
11614 : 36132628 : current_function_decl = NULL;
11615 : : }
11616 : :
11617 : : /* Check the declarations given in a for-loop for satisfying the C99
11618 : : constraints. If exactly one such decl is found, return it. LOC is
11619 : : the location of the opening parenthesis of the for loop. The last
11620 : : parameter allows you to control the "for loop initial declarations
11621 : : are only allowed in C99 mode". Normally, you should pass
11622 : : flag_isoc99 as that parameter. But in some cases (Objective-C
11623 : : foreach loop, for example) we want to run the checks in this
11624 : : function even if not in C99 mode, so we allow the caller to turn
11625 : : off the error about not being in C99 mode.
11626 : : */
11627 : :
11628 : : tree
11629 : 25346 : check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
11630 : : {
11631 : 25346 : struct c_binding *b;
11632 : 25346 : tree one_decl = NULL_TREE;
11633 : 25346 : int n_decls = 0;
11634 : :
11635 : 25346 : if (!turn_off_iso_c99_error)
11636 : : {
11637 : 1 : static bool hint = true;
11638 : : /* If we get here, declarations have been used in a for loop without
11639 : : the C99 for loop scope. This doesn't make much sense, so don't
11640 : : allow it. */
11641 : 1 : auto_diagnostic_group d;
11642 : 1 : error_at (loc, "%<for%> loop initial declarations "
11643 : : "are only allowed in C99 or C11 mode");
11644 : 1 : if (hint)
11645 : : {
11646 : 1 : inform (loc,
11647 : : "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
11648 : : "%<-std=gnu11%> to compile your code");
11649 : 1 : hint = false;
11650 : : }
11651 : 1 : return NULL_TREE;
11652 : 1 : }
11653 : : else
11654 : 25345 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
11655 : : "initial declarations");
11656 : :
11657 : : /* C99 subclause 6.8.5 paragraph 3:
11658 : :
11659 : : [#3] The declaration part of a for statement shall only
11660 : : declare identifiers for objects having storage class auto or
11661 : : register.
11662 : :
11663 : : It isn't clear whether, in this sentence, "identifiers" binds to
11664 : : "shall only declare" or to "objects" - that is, whether all identifiers
11665 : : declared must be identifiers for objects, or whether the restriction
11666 : : only applies to those that are. (A question on this in comp.std.c
11667 : : in November 2000 received no answer.) We implement the strictest
11668 : : interpretation, to avoid creating an extension which later causes
11669 : : problems.
11670 : :
11671 : : This constraint was removed in C23. */
11672 : :
11673 : 50765 : for (b = current_scope->bindings; b; b = b->prev)
11674 : : {
11675 : 25420 : tree id = b->id;
11676 : 25420 : tree decl = b->decl;
11677 : :
11678 : 25420 : if (!id)
11679 : 27 : continue;
11680 : :
11681 : 25393 : switch (TREE_CODE (decl))
11682 : : {
11683 : 25345 : case VAR_DECL:
11684 : 25345 : {
11685 : 25345 : location_t decl_loc = DECL_SOURCE_LOCATION (decl);
11686 : 25345 : if (TREE_STATIC (decl))
11687 : 5 : pedwarn_c11 (decl_loc, OPT_Wpedantic,
11688 : : "declaration of static variable %qD in %<for%> "
11689 : : "loop initial declaration", decl);
11690 : 25340 : else if (DECL_EXTERNAL (decl))
11691 : 5 : pedwarn_c11 (decl_loc, OPT_Wpedantic,
11692 : : "declaration of %<extern%> variable %qD in %<for%> "
11693 : : "loop initial declaration", decl);
11694 : : }
11695 : : break;
11696 : :
11697 : 6 : case RECORD_TYPE:
11698 : 6 : pedwarn_c11 (loc, OPT_Wpedantic,
11699 : : "%<struct %E%> declared in %<for%> loop initial "
11700 : : "declaration", id);
11701 : 6 : break;
11702 : 5 : case UNION_TYPE:
11703 : 5 : pedwarn_c11 (loc, OPT_Wpedantic,
11704 : : "%<union %E%> declared in %<for%> loop initial "
11705 : : "declaration",
11706 : : id);
11707 : 5 : break;
11708 : 5 : case ENUMERAL_TYPE:
11709 : 5 : pedwarn_c11 (loc, OPT_Wpedantic,
11710 : : "%<enum %E%> declared in %<for%> loop "
11711 : : "initial declaration", id);
11712 : 5 : break;
11713 : 32 : default:
11714 : 32 : pedwarn_c11 (loc, OPT_Wpedantic, "declaration of non-variable "
11715 : : "%qD in %<for%> loop initial declaration", decl);
11716 : : }
11717 : :
11718 : 25393 : n_decls++;
11719 : 25393 : one_decl = decl;
11720 : : }
11721 : :
11722 : 25345 : return n_decls == 1 ? one_decl : NULL_TREE;
11723 : : }
11724 : :
11725 : : /* Save and reinitialize the variables
11726 : : used during compilation of a C function. */
11727 : :
11728 : : void
11729 : 1607 : c_push_function_context (void)
11730 : : {
11731 : 1607 : struct language_function *p = cfun->language;
11732 : : /* cfun->language might have been already allocated by the use of
11733 : : -Wunused-local-typedefs. In that case, just re-use it. */
11734 : 1607 : if (p == NULL)
11735 : 1551 : cfun->language = p = ggc_cleared_alloc<language_function> ();
11736 : :
11737 : 1607 : p->base.x_stmt_tree = c_stmt_tree;
11738 : 1607 : c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
11739 : 1607 : p->x_in_statement = in_statement;
11740 : 1607 : p->x_switch_stack = c_switch_stack;
11741 : 1607 : p->loop_names = loop_names;
11742 : 1607 : loop_names = vNULL;
11743 : 1607 : p->loop_names_hash = loop_names_hash;
11744 : 1607 : loop_names_hash = NULL;
11745 : 1607 : p->arg_info = current_function_arg_info;
11746 : 1607 : p->returns_value = current_function_returns_value;
11747 : 1607 : p->returns_null = current_function_returns_null;
11748 : 1607 : p->returns_abnormally = current_function_returns_abnormally;
11749 : 1607 : p->warn_about_return_type = warn_about_return_type;
11750 : :
11751 : 1607 : push_function_context ();
11752 : 1607 : }
11753 : :
11754 : : /* Restore the variables used during compilation of a C function. */
11755 : :
11756 : : void
11757 : 1607 : c_pop_function_context (void)
11758 : : {
11759 : 1607 : struct language_function *p;
11760 : :
11761 : 1607 : pop_function_context ();
11762 : 1607 : p = cfun->language;
11763 : :
11764 : : /* When -Wunused-local-typedefs is in effect, cfun->languages is
11765 : : used to store data throughout the life time of the current cfun,
11766 : : So don't deallocate it. */
11767 : 1607 : if (!warn_unused_local_typedefs)
11768 : 1551 : cfun->language = NULL;
11769 : :
11770 : 1607 : if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
11771 : 1607 : && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
11772 : : {
11773 : : /* Stop pointing to the local nodes about to be freed. */
11774 : : /* But DECL_INITIAL must remain nonzero so we know this
11775 : : was an actual function definition. */
11776 : 0 : DECL_INITIAL (current_function_decl) = error_mark_node;
11777 : 0 : DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
11778 : : }
11779 : :
11780 : 1607 : c_stmt_tree = p->base.x_stmt_tree;
11781 : 1607 : p->base.x_stmt_tree.x_cur_stmt_list = NULL;
11782 : 1607 : in_statement = p->x_in_statement;
11783 : 1607 : c_switch_stack = p->x_switch_stack;
11784 : 1607 : loop_names.release ();
11785 : 1607 : loop_names = p->loop_names;
11786 : 1607 : p->loop_names = vNULL;
11787 : 1607 : delete loop_names_hash;
11788 : 1607 : loop_names_hash = p->loop_names_hash;
11789 : 1607 : p->loop_names_hash = NULL;
11790 : 1607 : current_function_arg_info = p->arg_info;
11791 : 1607 : current_function_returns_value = p->returns_value;
11792 : 1607 : current_function_returns_null = p->returns_null;
11793 : 1607 : current_function_returns_abnormally = p->returns_abnormally;
11794 : 1607 : warn_about_return_type = p->warn_about_return_type;
11795 : 1607 : }
11796 : :
11797 : : /* The functions below are required for functionality of doing
11798 : : function at once processing in the C front end. Currently these
11799 : : functions are not called from anywhere in the C front end, but as
11800 : : these changes continue, that will change. */
11801 : :
11802 : : /* Returns the stmt_tree (if any) to which statements are currently
11803 : : being added. If there is no active statement-tree, NULL is
11804 : : returned. */
11805 : :
11806 : : stmt_tree
11807 : 980588018 : current_stmt_tree (void)
11808 : : {
11809 : 980588018 : return &c_stmt_tree;
11810 : : }
11811 : :
11812 : : /* Return the global value of T as a symbol. */
11813 : :
11814 : : tree
11815 : 3999702 : identifier_global_value (tree t)
11816 : : {
11817 : 3999702 : struct c_binding *b;
11818 : :
11819 : 4000669 : for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
11820 : 3993443 : if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11821 : 3992476 : return b->decl;
11822 : :
11823 : : return NULL_TREE;
11824 : : }
11825 : :
11826 : : /* Return the global value of tag T as a symbol. */
11827 : :
11828 : : tree
11829 : 12 : identifier_global_tag (tree t)
11830 : : {
11831 : 12 : struct c_binding *b;
11832 : :
11833 : 12 : for (b = I_TAG_BINDING (t); b; b = b->shadowed)
11834 : 11 : if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11835 : 11 : return b->decl;
11836 : :
11837 : : return NULL_TREE;
11838 : : }
11839 : :
11840 : : /* Returns non-zero (result of __has_builtin) if NAME refers to a built-in
11841 : : function or function-like operator. */
11842 : :
11843 : : int
11844 : 24837 : names_builtin_p (const char *name)
11845 : : {
11846 : 24837 : tree id = get_identifier (name);
11847 : 24837 : if (tree decl = identifier_global_value (id))
11848 : 24751 : return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_UNDECLARED_BUILTIN (decl);
11849 : :
11850 : : /* Also detect common reserved C words that aren't strictly built-in
11851 : : functions. */
11852 : 86 : switch (C_RID_CODE (id))
11853 : : {
11854 : : case RID_BUILTIN_ASSOC_BARRIER:
11855 : : case RID_BUILTIN_CONVERTVECTOR:
11856 : : case RID_BUILTIN_HAS_ATTRIBUTE:
11857 : : case RID_BUILTIN_SHUFFLE:
11858 : : case RID_BUILTIN_SHUFFLEVECTOR:
11859 : : case RID_BUILTIN_STDC:
11860 : : case RID_BUILTIN_COUNTED_BY_REF:
11861 : : case RID_CHOOSE_EXPR:
11862 : : case RID_OFFSETOF:
11863 : : case RID_TYPES_COMPATIBLE_P:
11864 : : case RID_C23_VA_START:
11865 : : case RID_VA_ARG:
11866 : : return 1;
11867 : 65 : default:
11868 : 65 : break;
11869 : : }
11870 : :
11871 : 65 : return 0;
11872 : : }
11873 : :
11874 : : /* In C, the only C-linkage public declaration is at file scope. */
11875 : :
11876 : : tree
11877 : 5 : c_linkage_bindings (tree name)
11878 : : {
11879 : 5 : return identifier_global_value (name);
11880 : : }
11881 : :
11882 : : /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
11883 : : otherwise the name is found in ridpointers from RID_INDEX. */
11884 : :
11885 : : void
11886 : 3397920 : record_builtin_type (enum rid rid_index, const char *name, tree type)
11887 : : {
11888 : 3397920 : tree id, decl;
11889 : 3397920 : if (name == 0)
11890 : 1585696 : id = ridpointers[(int) rid_index];
11891 : : else
11892 : 1812224 : id = get_identifier (name);
11893 : 3397920 : decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
11894 : 3397920 : pushdecl (decl);
11895 : 3397920 : if (debug_hooks->type_decl)
11896 : 3397920 : debug_hooks->type_decl (decl, false);
11897 : 3397920 : }
11898 : :
11899 : : /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
11900 : :
11901 : : struct c_parm *
11902 : 123345459 : build_c_parm (struct c_declspecs *specs, tree attrs,
11903 : : struct c_declarator *declarator,
11904 : : location_t loc)
11905 : : {
11906 : 123345459 : struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
11907 : 123345459 : ret->specs = specs;
11908 : 123345459 : ret->attrs = attrs;
11909 : 123345459 : ret->declarator = declarator;
11910 : 123345459 : ret->loc = loc;
11911 : 123345459 : return ret;
11912 : : }
11913 : :
11914 : : /* Return a declarator with nested attributes. TARGET is the inner
11915 : : declarator to which these attributes apply. ATTRS are the
11916 : : attributes. */
11917 : :
11918 : : struct c_declarator *
11919 : 6774 : build_attrs_declarator (tree attrs, struct c_declarator *target)
11920 : : {
11921 : 6774 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11922 : 6774 : ret->kind = cdk_attrs;
11923 : 6774 : ret->declarator = target;
11924 : 6774 : ret->u.attrs = attrs;
11925 : 6774 : return ret;
11926 : : }
11927 : :
11928 : : /* Return a declarator for a function with arguments specified by ARGS
11929 : : and return type specified by TARGET. */
11930 : :
11931 : : struct c_declarator *
11932 : 50247891 : build_function_declarator (struct c_arg_info *args,
11933 : : struct c_declarator *target)
11934 : : {
11935 : 50247891 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11936 : 50247891 : ret->kind = cdk_function;
11937 : 50247891 : ret->declarator = target;
11938 : 50247891 : ret->u.arg_info = args;
11939 : 50247891 : return ret;
11940 : : }
11941 : :
11942 : : /* Return a declarator for the identifier IDENT (which may be
11943 : : NULL_TREE for an abstract declarator). */
11944 : :
11945 : : struct c_declarator *
11946 : 312351846 : build_id_declarator (tree ident)
11947 : : {
11948 : 312351846 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11949 : 312351846 : ret->kind = cdk_id;
11950 : 312351846 : ret->declarator = 0;
11951 : 312351846 : ret->u.id.id = ident;
11952 : 312351846 : ret->u.id.attrs = NULL_TREE;
11953 : : /* Default value - may get reset to a more precise location. */
11954 : 312351846 : ret->id_loc = input_location;
11955 : 312351846 : return ret;
11956 : : }
11957 : :
11958 : : /* Return something to represent absolute declarators containing a *.
11959 : : TARGET is the absolute declarator that the * contains.
11960 : : TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
11961 : : to apply to the pointer type. */
11962 : :
11963 : : struct c_declarator *
11964 : 18118150 : make_pointer_declarator (struct c_declspecs *type_quals_attrs,
11965 : : struct c_declarator *target)
11966 : : {
11967 : 18118150 : tree attrs;
11968 : 18118150 : int quals = 0;
11969 : 18118150 : struct c_declarator *itarget = target;
11970 : 18118150 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11971 : 18118150 : if (type_quals_attrs)
11972 : : {
11973 : 18118150 : attrs = type_quals_attrs->attrs;
11974 : 18118150 : quals = quals_from_declspecs (type_quals_attrs);
11975 : 18118150 : if (attrs != NULL_TREE)
11976 : 6502 : itarget = build_attrs_declarator (attrs, target);
11977 : : }
11978 : 18118150 : ret->kind = cdk_pointer;
11979 : 18118150 : ret->declarator = itarget;
11980 : 18118150 : ret->u.pointer_quals = quals;
11981 : 18118150 : return ret;
11982 : : }
11983 : :
11984 : : /* Return a pointer to a structure for an empty list of declaration
11985 : : specifiers. */
11986 : :
11987 : : struct c_declspecs *
11988 : 452451217 : build_null_declspecs (void)
11989 : : {
11990 : 452451217 : struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
11991 : 452451217 : memset (ret, 0, sizeof *ret);
11992 : 452451217 : ret->align_log = -1;
11993 : 452451217 : ret->typespec_word = cts_none;
11994 : 452451217 : ret->storage_class = csc_none;
11995 : 452451217 : ret->expr_const_operands = true;
11996 : 452451217 : ret->typespec_kind = ctsk_none;
11997 : 452451217 : ret->address_space = ADDR_SPACE_GENERIC;
11998 : 452451217 : return ret;
11999 : : }
12000 : :
12001 : : /* Add the address space ADDRSPACE to the declaration specifiers
12002 : : SPECS, returning SPECS. */
12003 : :
12004 : : struct c_declspecs *
12005 : 174 : declspecs_add_addrspace (location_t location,
12006 : : struct c_declspecs *specs, addr_space_t as)
12007 : : {
12008 : 174 : specs->non_sc_seen_p = true;
12009 : 174 : specs->declspecs_seen_p = true;
12010 : 174 : specs->non_std_attrs_seen_p = true;
12011 : :
12012 : 174 : if (!ADDR_SPACE_GENERIC_P (specs->address_space)
12013 : 0 : && specs->address_space != as)
12014 : 0 : error ("incompatible address space qualifiers %qs and %qs",
12015 : : c_addr_space_name (as),
12016 : : c_addr_space_name (specs->address_space));
12017 : : else
12018 : : {
12019 : 174 : specs->address_space = as;
12020 : 174 : specs->locations[cdw_address_space] = location;
12021 : : }
12022 : 174 : return specs;
12023 : : }
12024 : :
12025 : : /* Add the type qualifier QUAL to the declaration specifiers SPECS,
12026 : : returning SPECS. */
12027 : :
12028 : : struct c_declspecs *
12029 : 16491649 : declspecs_add_qual (location_t loc,
12030 : : struct c_declspecs *specs, tree qual)
12031 : : {
12032 : 16491649 : enum rid i;
12033 : 16491649 : bool dupe = false;
12034 : 16491649 : specs->non_sc_seen_p = true;
12035 : 16491649 : specs->declspecs_seen_p = true;
12036 : 16491649 : specs->non_std_attrs_seen_p = true;
12037 : 16491649 : gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
12038 : : && C_IS_RESERVED_WORD (qual));
12039 : 16491649 : i = C_RID_CODE (qual);
12040 : 16491649 : location_t prev_loc = UNKNOWN_LOCATION;
12041 : 16491649 : switch (i)
12042 : : {
12043 : 13019317 : case RID_CONST:
12044 : 13019317 : dupe = specs->const_p;
12045 : 13019317 : specs->const_p = true;
12046 : 13019317 : prev_loc = specs->locations[cdw_const];
12047 : 13019317 : specs->locations[cdw_const] = loc;
12048 : 13019317 : break;
12049 : 95710 : case RID_VOLATILE:
12050 : 95710 : dupe = specs->volatile_p;
12051 : 95710 : specs->volatile_p = true;
12052 : 95710 : prev_loc = specs->locations[cdw_volatile];
12053 : 95710 : specs->locations[cdw_volatile] = loc;
12054 : 95710 : break;
12055 : 3357673 : case RID_RESTRICT:
12056 : 3357673 : dupe = specs->restrict_p;
12057 : 3357673 : specs->restrict_p = true;
12058 : 3357673 : prev_loc = specs->locations[cdw_restrict];
12059 : 3357673 : specs->locations[cdw_restrict] = loc;
12060 : 3357673 : break;
12061 : 18949 : case RID_ATOMIC:
12062 : 18949 : dupe = specs->atomic_p;
12063 : 18949 : specs->atomic_p = true;
12064 : 18949 : prev_loc = specs->locations[cdw_atomic];
12065 : 18949 : specs->locations[cdw_atomic] = loc;
12066 : 18949 : break;
12067 : 0 : default:
12068 : 0 : gcc_unreachable ();
12069 : : }
12070 : 16491649 : if (dupe)
12071 : : {
12072 : 56 : bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
12073 : : "duplicate %qE declaration specifier", qual);
12074 : 56 : if (!warned
12075 : 52 : && warn_duplicate_decl_specifier
12076 : 33 : && prev_loc >= RESERVED_LOCATION_COUNT
12077 : 33 : && !from_macro_expansion_at (prev_loc)
12078 : 77 : && !from_macro_expansion_at (loc))
12079 : 12 : warning_at (loc, OPT_Wduplicate_decl_specifier,
12080 : : "duplicate %qE declaration specifier", qual);
12081 : : }
12082 : 16491649 : return specs;
12083 : : }
12084 : :
12085 : : /* Add the type specifier TYPE to the declaration specifiers SPECS,
12086 : : returning SPECS. */
12087 : :
12088 : : struct c_declspecs *
12089 : 329824649 : declspecs_add_type (location_t loc, struct c_declspecs *specs,
12090 : : struct c_typespec spec)
12091 : : {
12092 : 329824649 : tree type = spec.spec;
12093 : 329824649 : specs->non_sc_seen_p = true;
12094 : 329824649 : specs->declspecs_seen_p = true;
12095 : 329824649 : specs->non_std_attrs_seen_p = true;
12096 : 329824649 : specs->typespec_kind = spec.kind;
12097 : 329824649 : if (TREE_DEPRECATED (type))
12098 : 56 : specs->deprecated_p = true;
12099 : 329824649 : if (TREE_UNAVAILABLE (type))
12100 : 40 : specs->unavailable_p = true;
12101 : :
12102 : : /* As a type specifier is present, "auto" must be used as a storage
12103 : : class specifier, not for type deduction. */
12104 : 329824649 : if (specs->c23_auto_p)
12105 : : {
12106 : 115 : specs->c23_auto_p = false;
12107 : 115 : if (specs->storage_class != csc_none)
12108 : 1 : error ("multiple storage classes in declaration specifiers");
12109 : 114 : else if (specs->thread_p)
12110 : 1 : error ("%qs used with %<auto%>",
12111 : 1 : specs->thread_gnu_p ? "__thread" : "_Thread_local");
12112 : 113 : else if (specs->constexpr_p)
12113 : : /* auto may only be used with another storage class specifier,
12114 : : such as constexpr, if the type is inferred. */
12115 : 2 : error ("%<auto%> used with %<constexpr%>");
12116 : : else
12117 : 111 : specs->storage_class = csc_auto;
12118 : : }
12119 : :
12120 : : /* Handle type specifier keywords. */
12121 : 329824649 : if (TREE_CODE (type) == IDENTIFIER_NODE
12122 : 81988842 : && C_IS_RESERVED_WORD (type)
12123 : 411813491 : && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
12124 : : {
12125 : 81988842 : enum rid i = C_RID_CODE (type);
12126 : 81988842 : if (specs->type)
12127 : : {
12128 : 58 : error_at (loc, "two or more data types in declaration specifiers");
12129 : 58 : return specs;
12130 : : }
12131 : 81988784 : if ((int) i <= (int) RID_LAST_MODIFIER)
12132 : : {
12133 : : /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
12134 : 21625602 : bool dupe = false;
12135 : 21625602 : switch (i)
12136 : : {
12137 : 10660602 : case RID_LONG:
12138 : 10660602 : if (specs->long_long_p)
12139 : : {
12140 : 104 : error_at (loc, "%<long long long%> is too long for GCC");
12141 : 104 : break;
12142 : : }
12143 : 10660498 : if (specs->long_p)
12144 : : {
12145 : 2937633 : if (specs->typespec_word == cts_double)
12146 : : {
12147 : 15 : error_at (loc,
12148 : : "both %qs and %qs in declaration specifiers",
12149 : : "long long", "double");
12150 : 15 : break;
12151 : : }
12152 : 2937618 : pedwarn_c90 (loc, OPT_Wlong_long,
12153 : : "ISO C90 does not support %<long long%>");
12154 : 2937618 : specs->long_long_p = 1;
12155 : 2937618 : specs->locations[cdw_long_long] = loc;
12156 : 2937618 : break;
12157 : : }
12158 : 7722865 : if (specs->short_p)
12159 : 77 : error_at (loc,
12160 : : "both %qs and %qs in declaration specifiers",
12161 : : "long", "short");
12162 : 7722788 : else if (specs->typespec_word == cts_auto_type)
12163 : 1 : error_at (loc,
12164 : : "both %qs and %qs in declaration specifiers",
12165 : : "long", "__auto_type");
12166 : : else if (specs->typespec_word == cts_void)
12167 : 5 : error_at (loc,
12168 : : "both %qs and %qs in declaration specifiers",
12169 : : "long", "void");
12170 : : else if (specs->typespec_word == cts_int_n)
12171 : 19 : error_at (loc,
12172 : : "both %qs and %<__int%d%> in declaration specifiers",
12173 : 19 : "long", int_n_data[specs->u.int_n_idx].bitsize);
12174 : : else if (specs->typespec_word == cts_bool)
12175 : 3 : error_at (loc,
12176 : : "both %qs and %qs in declaration specifiers",
12177 : : "long", "_Bool");
12178 : : else if (specs->typespec_word == cts_bitint)
12179 : 3 : error_at (loc,
12180 : : "both %qs and %qs in declaration specifiers",
12181 : : "long", "_BitInt");
12182 : : else if (specs->typespec_word == cts_char)
12183 : 21 : error_at (loc,
12184 : : "both %qs and %qs in declaration specifiers",
12185 : : "long", "char");
12186 : : else if (specs->typespec_word == cts_float)
12187 : 7 : error_at (loc,
12188 : : "both %qs and %qs in declaration specifiers",
12189 : : "long", "float");
12190 : : else if (specs->typespec_word == cts_floatn_nx)
12191 : 0 : error_at (loc,
12192 : : "both %qs and %<_Float%d%s%> in declaration "
12193 : : "specifiers", "long",
12194 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12195 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12196 : : ? "x"
12197 : : : "");
12198 : : else if (specs->typespec_word == cts_dfloat32)
12199 : 4 : error_at (loc,
12200 : : "both %qs and %qs in declaration specifiers",
12201 : : "long", "_Decimal32");
12202 : : else if (specs->typespec_word == cts_dfloat64)
12203 : 4 : error_at (loc,
12204 : : "both %qs and %qs in declaration specifiers",
12205 : : "long", "_Decimal64");
12206 : : else if (specs->typespec_word == cts_dfloat128)
12207 : 4 : error_at (loc,
12208 : : "both %qs and %qs in declaration specifiers",
12209 : : "long", "_Decimal128");
12210 : : else if (specs->typespec_word == cts_dfloat64x)
12211 : 0 : error_at (loc,
12212 : : "both %qs and %qs in declaration specifiers",
12213 : : "long", "_Decimal64x");
12214 : : else
12215 : : {
12216 : 7722717 : specs->long_p = true;
12217 : 7722717 : specs->locations[cdw_long] = loc;
12218 : : }
12219 : : break;
12220 : 1694725 : case RID_SHORT:
12221 : 1694725 : dupe = specs->short_p;
12222 : 1694725 : if (specs->long_p)
12223 : 197 : error_at (loc,
12224 : : "both %qs and %qs in declaration specifiers",
12225 : : "long", "short");
12226 : 1694528 : else if (specs->typespec_word == cts_auto_type)
12227 : 1 : error_at (loc,
12228 : : "both %qs and %qs in declaration specifiers",
12229 : : "short", "__auto_type");
12230 : : else if (specs->typespec_word == cts_void)
12231 : 5 : error_at (loc,
12232 : : "both %qs and %qs in declaration specifiers",
12233 : : "short", "void");
12234 : : else if (specs->typespec_word == cts_int_n)
12235 : 19 : error_at (loc,
12236 : : "both %qs and %<__int%d%> in declaration specifiers",
12237 : 19 : "short", int_n_data[specs->u.int_n_idx].bitsize);
12238 : : else if (specs->typespec_word == cts_bool)
12239 : 3 : error_at (loc,
12240 : : "both %qs and %qs in declaration specifiers",
12241 : : "short", "_Bool");
12242 : : else if (specs->typespec_word == cts_bitint)
12243 : 1 : error_at (loc,
12244 : : "both %qs and %qs in declaration specifiers",
12245 : : "short", "_BitInt");
12246 : : else if (specs->typespec_word == cts_char)
12247 : 21 : error_at (loc,
12248 : : "both %qs and %qs in declaration specifiers",
12249 : : "short", "char");
12250 : : else if (specs->typespec_word == cts_float)
12251 : 7 : error_at (loc,
12252 : : "both %qs and %qs in declaration specifiers",
12253 : : "short", "float");
12254 : : else if (specs->typespec_word == cts_double)
12255 : 7 : error_at (loc,
12256 : : "both %qs and %qs in declaration specifiers",
12257 : : "short", "double");
12258 : : else if (specs->typespec_word == cts_floatn_nx)
12259 : 0 : error_at (loc,
12260 : : "both %qs and %<_Float%d%s%> in declaration "
12261 : : "specifiers", "short",
12262 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12263 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12264 : : ? "x"
12265 : : : "");
12266 : : else if (specs->typespec_word == cts_dfloat32)
12267 : 4 : error_at (loc,
12268 : : "both %qs and %qs in declaration specifiers",
12269 : : "short", "_Decimal32");
12270 : : else if (specs->typespec_word == cts_dfloat64)
12271 : 4 : error_at (loc,
12272 : : "both %qs and %qs in declaration specifiers",
12273 : : "short", "_Decimal64");
12274 : : else if (specs->typespec_word == cts_dfloat128)
12275 : 4 : error_at (loc,
12276 : : "both %qs and %qs in declaration specifiers",
12277 : : "short", "_Decimal128");
12278 : : else if (specs->typespec_word == cts_dfloat64x)
12279 : 0 : error_at (loc,
12280 : : "both %qs and %qs in declaration specifiers",
12281 : : "short", "_Decimal64x");
12282 : : else
12283 : : {
12284 : 1694452 : specs->short_p = true;
12285 : 1694452 : specs->locations[cdw_short] = loc;
12286 : : }
12287 : : break;
12288 : 572422 : case RID_SIGNED:
12289 : 572422 : dupe = specs->signed_p;
12290 : 572422 : if (specs->unsigned_p)
12291 : 138 : error_at (loc,
12292 : : "both %qs and %qs in declaration specifiers",
12293 : : "signed", "unsigned");
12294 : 572284 : else if (specs->typespec_word == cts_auto_type)
12295 : 1 : error_at (loc,
12296 : : "both %qs and %qs in declaration specifiers",
12297 : : "signed", "__auto_type");
12298 : : else if (specs->typespec_word == cts_void)
12299 : 5 : error_at (loc,
12300 : : "both %qs and %qs in declaration specifiers",
12301 : : "signed", "void");
12302 : : else if (specs->typespec_word == cts_bool)
12303 : 3 : error_at (loc,
12304 : : "both %qs and %qs in declaration specifiers",
12305 : : "signed", "_Bool");
12306 : : else if (specs->typespec_word == cts_float)
12307 : 7 : error_at (loc,
12308 : : "both %qs and %qs in declaration specifiers",
12309 : : "signed", "float");
12310 : : else if (specs->typespec_word == cts_double)
12311 : 21 : error_at (loc,
12312 : : "both %qs and %qs in declaration specifiers",
12313 : : "signed", "double");
12314 : : else if (specs->typespec_word == cts_floatn_nx)
12315 : 0 : error_at (loc,
12316 : : "both %qs and %<_Float%d%s%> in declaration "
12317 : : "specifiers", "signed",
12318 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12319 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12320 : : ? "x"
12321 : : : "");
12322 : : else if (specs->typespec_word == cts_dfloat32)
12323 : 4 : error_at (loc,
12324 : : "both %qs and %qs in declaration specifiers",
12325 : : "signed", "_Decimal32");
12326 : : else if (specs->typespec_word == cts_dfloat64)
12327 : 4 : error_at (loc,
12328 : : "both %qs and %qs in declaration specifiers",
12329 : : "signed", "_Decimal64");
12330 : : else if (specs->typespec_word == cts_dfloat128)
12331 : 4 : error_at (loc,
12332 : : "both %qs and %qs in declaration specifiers",
12333 : : "signed", "_Decimal128");
12334 : : else if (specs->typespec_word == cts_dfloat64x)
12335 : 0 : error_at (loc,
12336 : : "both %qs and %qs in declaration specifiers",
12337 : : "signed", "_Decimal64x");
12338 : : else
12339 : : {
12340 : 572235 : specs->signed_p = true;
12341 : 572235 : specs->locations[cdw_signed] = loc;
12342 : : }
12343 : : break;
12344 : 6147630 : case RID_UNSIGNED:
12345 : 6147630 : dupe = specs->unsigned_p;
12346 : 6147630 : if (specs->signed_p)
12347 : 139 : error_at (loc,
12348 : : "both %qs and %qs in declaration specifiers",
12349 : : "signed", "unsigned");
12350 : 6147491 : else if (specs->typespec_word == cts_auto_type)
12351 : 1 : error_at (loc,
12352 : : "both %qs and %qs in declaration specifiers",
12353 : : "unsigned", "__auto_type");
12354 : : else if (specs->typespec_word == cts_void)
12355 : 5 : error_at (loc,
12356 : : "both %qs and %qs in declaration specifiers",
12357 : : "unsigned", "void");
12358 : : else if (specs->typespec_word == cts_bool)
12359 : 3 : error_at (loc,
12360 : : "both %qs and %qs in declaration specifiers",
12361 : : "unsigned", "_Bool");
12362 : : else if (specs->typespec_word == cts_float)
12363 : 7 : error_at (loc,
12364 : : "both %qs and %qs in declaration specifiers",
12365 : : "unsigned", "float");
12366 : : else if (specs->typespec_word == cts_double)
12367 : 21 : error_at (loc,
12368 : : "both %qs and %qs in declaration specifiers",
12369 : : "unsigned", "double");
12370 : : else if (specs->typespec_word == cts_floatn_nx)
12371 : 0 : error_at (loc,
12372 : : "both %qs and %<_Float%d%s%> in declaration "
12373 : : "specifiers", "unsigned",
12374 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12375 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12376 : : ? "x"
12377 : : : "");
12378 : : else if (specs->typespec_word == cts_dfloat32)
12379 : 2 : error_at (loc,
12380 : : "both %qs and %qs in declaration specifiers",
12381 : : "unsigned", "_Decimal32");
12382 : : else if (specs->typespec_word == cts_dfloat64)
12383 : 2 : error_at (loc,
12384 : : "both %qs and %qs in declaration specifiers",
12385 : : "unsigned", "_Decimal64");
12386 : : else if (specs->typespec_word == cts_dfloat128)
12387 : 2 : error_at (loc,
12388 : : "both %qs and %qs in declaration specifiers",
12389 : : "unsigned", "_Decimal128");
12390 : : else if (specs->typespec_word == cts_dfloat64x)
12391 : 0 : error_at (loc,
12392 : : "both %qs and %qs in declaration specifiers",
12393 : : "unsigned", "_Decimal64x");
12394 : : else
12395 : : {
12396 : 6147448 : specs->unsigned_p = true;
12397 : 6147448 : specs->locations[cdw_unsigned] = loc;
12398 : : }
12399 : : break;
12400 : 2550106 : case RID_COMPLEX:
12401 : 2550106 : dupe = specs->complex_p;
12402 : 2550106 : if (!in_system_header_at (loc))
12403 : 68942 : pedwarn_c90 (loc, OPT_Wpedantic,
12404 : : "ISO C90 does not support complex types");
12405 : 2550106 : if (specs->typespec_word == cts_auto_type)
12406 : 1 : error_at (loc,
12407 : : "both %qs and %qs in declaration specifiers",
12408 : : "complex", "__auto_type");
12409 : : else if (specs->typespec_word == cts_void)
12410 : 2 : error_at (loc,
12411 : : "both %qs and %qs in declaration specifiers",
12412 : : "complex", "void");
12413 : : else if (specs->typespec_word == cts_bool)
12414 : 2 : error_at (loc,
12415 : : "both %qs and %qs in declaration specifiers",
12416 : : "complex", "_Bool");
12417 : : else if (specs->typespec_word == cts_bitint)
12418 : 1 : error_at (loc,
12419 : : "both %qs and %qs in declaration specifiers",
12420 : : "complex", "_BitInt");
12421 : : else if (specs->typespec_word == cts_dfloat32)
12422 : 1 : error_at (loc,
12423 : : "both %qs and %qs in declaration specifiers",
12424 : : "complex", "_Decimal32");
12425 : : else if (specs->typespec_word == cts_dfloat64)
12426 : 1 : error_at (loc,
12427 : : "both %qs and %qs in declaration specifiers",
12428 : : "complex", "_Decimal64");
12429 : : else if (specs->typespec_word == cts_dfloat128)
12430 : 1 : error_at (loc,
12431 : : "both %qs and %qs in declaration specifiers",
12432 : : "complex", "_Decimal128");
12433 : : else if (specs->typespec_word == cts_dfloat64x)
12434 : 0 : error_at (loc,
12435 : : "both %qs and %qs in declaration specifiers",
12436 : : "complex", "_Decimal64x");
12437 : : else if (specs->typespec_word == cts_fract)
12438 : 0 : error_at (loc,
12439 : : "both %qs and %qs in declaration specifiers",
12440 : : "complex", "_Fract");
12441 : : else if (specs->typespec_word == cts_accum)
12442 : 0 : error_at (loc,
12443 : : "both %qs and %qs in declaration specifiers",
12444 : : "complex", "_Accum");
12445 : 2550097 : else if (specs->saturating_p)
12446 : 0 : error_at (loc,
12447 : : "both %qs and %qs in declaration specifiers",
12448 : : "complex", "_Sat");
12449 : : else
12450 : : {
12451 : 2550097 : specs->complex_p = true;
12452 : 2550097 : specs->locations[cdw_complex] = loc;
12453 : : }
12454 : : break;
12455 : 117 : case RID_SAT:
12456 : 117 : dupe = specs->saturating_p;
12457 : 117 : pedwarn (loc, OPT_Wpedantic,
12458 : : "ISO C does not support saturating types");
12459 : 117 : if (specs->typespec_word == cts_int_n)
12460 : 0 : error_at (loc,
12461 : : "both %qs and %<__int%d%> in declaration specifiers",
12462 : 0 : "_Sat", int_n_data[specs->u.int_n_idx].bitsize);
12463 : : else if (specs->typespec_word == cts_auto_type)
12464 : 0 : error_at (loc,
12465 : : "both %qs and %qs in declaration specifiers",
12466 : : "_Sat", "__auto_type");
12467 : : else if (specs->typespec_word == cts_void)
12468 : 0 : error_at (loc,
12469 : : "both %qs and %qs in declaration specifiers",
12470 : : "_Sat", "void");
12471 : : else if (specs->typespec_word == cts_bool)
12472 : 0 : error_at (loc,
12473 : : "both %qs and %qs in declaration specifiers",
12474 : : "_Sat", "_Bool");
12475 : : else if (specs->typespec_word == cts_bitint)
12476 : 0 : error_at (loc,
12477 : : "both %qs and %qs in declaration specifiers",
12478 : : "_Sat", "_BitInt");
12479 : : else if (specs->typespec_word == cts_char)
12480 : 0 : error_at (loc,
12481 : : "both %qs and %qs in declaration specifiers",
12482 : : "_Sat", "char");
12483 : : else if (specs->typespec_word == cts_int)
12484 : 0 : error_at (loc,
12485 : : "both %qs and %qs in declaration specifiers",
12486 : : "_Sat", "int");
12487 : : else if (specs->typespec_word == cts_float)
12488 : 0 : error_at (loc,
12489 : : "both %qs and %qs in declaration specifiers",
12490 : : "_Sat", "float");
12491 : : else if (specs->typespec_word == cts_double)
12492 : 0 : error_at (loc,
12493 : : "both %qs and %qs in declaration specifiers",
12494 : : "_Sat", "double");
12495 : : else if (specs->typespec_word == cts_floatn_nx)
12496 : 0 : error_at (loc,
12497 : : "both %qs and %<_Float%d%s%> in declaration "
12498 : : "specifiers", "_Sat",
12499 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12500 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12501 : : ? "x"
12502 : : : "");
12503 : : else if (specs->typespec_word == cts_dfloat32)
12504 : 0 : error_at (loc,
12505 : : "both %qs and %qs in declaration specifiers",
12506 : : "_Sat", "_Decimal32");
12507 : : else if (specs->typespec_word == cts_dfloat64)
12508 : 0 : error_at (loc,
12509 : : "both %qs and %qs in declaration specifiers",
12510 : : "_Sat", "_Decimal64");
12511 : : else if (specs->typespec_word == cts_dfloat128)
12512 : 0 : error_at (loc,
12513 : : "both %qs and %qs in declaration specifiers",
12514 : : "_Sat", "_Decimal128");
12515 : : else if (specs->typespec_word == cts_dfloat64x)
12516 : 0 : error_at (loc,
12517 : : "both %qs and %qs in declaration specifiers",
12518 : : "_Sat", "_Decimal64x");
12519 : 117 : else if (specs->complex_p)
12520 : 0 : error_at (loc,
12521 : : "both %qs and %qs in declaration specifiers",
12522 : : "_Sat", "complex");
12523 : : else
12524 : : {
12525 : 117 : specs->saturating_p = true;
12526 : 117 : specs->locations[cdw_saturating] = loc;
12527 : : }
12528 : : break;
12529 : 0 : default:
12530 : 0 : gcc_unreachable ();
12531 : : }
12532 : :
12533 : 21625602 : if (dupe)
12534 : 378 : error_at (loc, "duplicate %qE", type);
12535 : :
12536 : 21625602 : return specs;
12537 : : }
12538 : : else
12539 : : {
12540 : : /* "void", "_Bool", "char", "int", "float", "double",
12541 : : "_FloatN", "_FloatNx", "_Decimal32", "__intN",
12542 : : "_Decimal64", "_Decimal128", "_Fract", "_Accum", "_BitInt(N)" or
12543 : : "__auto_type". */
12544 : 60363182 : if (specs->typespec_word != cts_none)
12545 : : {
12546 : 2171 : if (i == RID_BOOL)
12547 : : {
12548 : 175 : auto_diagnostic_group d;
12549 : 175 : if (specs->storage_class == csc_typedef)
12550 : 4 : error_at (loc,
12551 : : "%qs cannot be defined via %<typedef%>",
12552 : 2 : IDENTIFIER_POINTER (type));
12553 : : else
12554 : 346 : error_at (loc,
12555 : : "%qs cannot be used here",
12556 : 173 : IDENTIFIER_POINTER (type));
12557 : 175 : add_note_about_new_keyword (loc, type);
12558 : 175 : }
12559 : : else
12560 : 1996 : error_at (loc,
12561 : : "two or more data types in declaration specifiers");
12562 : 2171 : return specs;
12563 : : }
12564 : 60361011 : switch (i)
12565 : : {
12566 : 1838 : case RID_AUTO_TYPE:
12567 : 1838 : if (specs->long_p)
12568 : 1 : error_at (loc,
12569 : : "both %qs and %qs in declaration specifiers",
12570 : : "long", "__auto_type");
12571 : 1837 : else if (specs->short_p)
12572 : 1 : error_at (loc,
12573 : : "both %qs and %qs in declaration specifiers",
12574 : : "short", "__auto_type");
12575 : 1836 : else if (specs->signed_p)
12576 : 1 : error_at (loc,
12577 : : "both %qs and %qs in declaration specifiers",
12578 : : "signed", "__auto_type");
12579 : 1835 : else if (specs->unsigned_p)
12580 : 1 : error_at (loc,
12581 : : "both %qs and %qs in declaration specifiers",
12582 : : "unsigned", "__auto_type");
12583 : 1834 : else if (specs->complex_p)
12584 : 1 : error_at (loc,
12585 : : "both %qs and %qs in declaration specifiers",
12586 : : "complex", "__auto_type");
12587 : 1833 : else if (specs->saturating_p)
12588 : 0 : error_at (loc,
12589 : : "both %qs and %qs in declaration specifiers",
12590 : : "_Sat", "__auto_type");
12591 : : else
12592 : : {
12593 : 1833 : specs->typespec_word = cts_auto_type;
12594 : 1833 : specs->locations[cdw_typespec] = loc;
12595 : : }
12596 : 1838 : return specs;
12597 : 51573 : case RID_INT_N_0:
12598 : 51573 : case RID_INT_N_1:
12599 : 51573 : case RID_INT_N_2:
12600 : 51573 : case RID_INT_N_3:
12601 : 51573 : specs->u.int_n_idx = i - RID_INT_N_0;
12602 : 51573 : if (!in_system_header_at (input_location)
12603 : : /* If the INT_N type ends in "__", and so is of the format
12604 : : "__intN__", don't pedwarn. */
12605 : 51573 : && (strncmp (IDENTIFIER_POINTER (type)
12606 : 40969 : + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
12607 : 40969 : pedwarn (loc, OPT_Wpedantic,
12608 : : "ISO C does not support %<__int%d%> types",
12609 : 40969 : int_n_data[specs->u.int_n_idx].bitsize);
12610 : :
12611 : 51573 : if (specs->long_p)
12612 : 53 : error_at (loc,
12613 : : "both %<__int%d%> and %qs in declaration specifiers",
12614 : 53 : int_n_data[specs->u.int_n_idx].bitsize, "long");
12615 : 51520 : else if (specs->saturating_p)
12616 : 0 : error_at (loc,
12617 : : "both %qs and %<__int%d%> in declaration specifiers",
12618 : 0 : "_Sat", int_n_data[specs->u.int_n_idx].bitsize);
12619 : 51520 : else if (specs->short_p)
12620 : 19 : error_at (loc,
12621 : : "both %<__int%d%> and %qs in declaration specifiers",
12622 : 19 : int_n_data[specs->u.int_n_idx].bitsize, "short");
12623 : 51501 : else if (! int_n_enabled_p[specs->u.int_n_idx])
12624 : : {
12625 : 0 : specs->typespec_word = cts_int_n;
12626 : 0 : error_at (loc,
12627 : : "%<__int%d%> is not supported on this target",
12628 : 0 : int_n_data[specs->u.int_n_idx].bitsize);
12629 : : }
12630 : : else
12631 : : {
12632 : 51501 : specs->typespec_word = cts_int_n;
12633 : 51501 : specs->locations[cdw_typespec] = loc;
12634 : : }
12635 : 51573 : return specs;
12636 : 7806658 : case RID_VOID:
12637 : 7806658 : if (specs->long_p)
12638 : 44 : error_at (loc,
12639 : : "both %qs and %qs in declaration specifiers",
12640 : : "long", "void");
12641 : 7806614 : else if (specs->short_p)
12642 : 21 : error_at (loc,
12643 : : "both %qs and %qs in declaration specifiers",
12644 : : "short", "void");
12645 : 7806593 : else if (specs->signed_p)
12646 : 5 : error_at (loc,
12647 : : "both %qs and %qs in declaration specifiers",
12648 : : "signed", "void");
12649 : 7806588 : else if (specs->unsigned_p)
12650 : 5 : error_at (loc,
12651 : : "both %qs and %qs in declaration specifiers",
12652 : : "unsigned", "void");
12653 : 7806583 : else if (specs->complex_p)
12654 : 2 : error_at (loc,
12655 : : "both %qs and %qs in declaration specifiers",
12656 : : "complex", "void");
12657 : 7806581 : else if (specs->saturating_p)
12658 : 0 : error_at (loc,
12659 : : "both %qs and %qs in declaration specifiers",
12660 : : "_Sat", "void");
12661 : : else
12662 : : {
12663 : 7806581 : specs->typespec_word = cts_void;
12664 : 7806581 : specs->locations[cdw_typespec] = loc;
12665 : : }
12666 : 7806658 : return specs;
12667 : 84576 : case RID_BOOL:
12668 : 84576 : if (!in_system_header_at (loc))
12669 : 70241 : pedwarn_c90 (loc, OPT_Wpedantic,
12670 : : "ISO C90 does not support boolean types");
12671 : 84576 : if (specs->long_p)
12672 : 27 : error_at (loc,
12673 : : "both %qs and %qs in declaration specifiers",
12674 : : "long", "_Bool");
12675 : 84549 : else if (specs->short_p)
12676 : 11 : error_at (loc,
12677 : : "both %qs and %qs in declaration specifiers",
12678 : : "short", "_Bool");
12679 : 84538 : else if (specs->signed_p)
12680 : 3 : error_at (loc,
12681 : : "both %qs and %qs in declaration specifiers",
12682 : : "signed", "_Bool");
12683 : 84535 : else if (specs->unsigned_p)
12684 : 3 : error_at (loc,
12685 : : "both %qs and %qs in declaration specifiers",
12686 : : "unsigned", "_Bool");
12687 : 84532 : else if (specs->complex_p)
12688 : 2 : error_at (loc,
12689 : : "both %qs and %qs in declaration specifiers",
12690 : : "complex", "_Bool");
12691 : 84530 : else if (specs->saturating_p)
12692 : 0 : error_at (loc,
12693 : : "both %qs and %qs in declaration specifiers",
12694 : : "_Sat", "_Bool");
12695 : : else
12696 : : {
12697 : 84530 : specs->typespec_word = cts_bool;
12698 : 84530 : specs->locations[cdw_typespec] = loc;
12699 : : }
12700 : 84576 : return specs;
12701 : 9016991 : case RID_CHAR:
12702 : 9016991 : if (specs->long_p)
12703 : 44 : error_at (loc,
12704 : : "both %qs and %qs in declaration specifiers",
12705 : : "long", "char");
12706 : 9016947 : else if (specs->short_p)
12707 : 21 : error_at (loc,
12708 : : "both %qs and %qs in declaration specifiers",
12709 : : "short", "char");
12710 : 9016926 : else if (specs->saturating_p)
12711 : 0 : error_at (loc,
12712 : : "both %qs and %qs in declaration specifiers",
12713 : : "_Sat", "char");
12714 : : else
12715 : : {
12716 : 9016926 : specs->typespec_word = cts_char;
12717 : 9016926 : specs->locations[cdw_typespec] = loc;
12718 : : }
12719 : 9016991 : return specs;
12720 : 23719367 : case RID_INT:
12721 : 23719367 : if (specs->saturating_p)
12722 : 0 : error_at (loc,
12723 : : "both %qs and %qs in declaration specifiers",
12724 : : "_Sat", "int");
12725 : : else
12726 : : {
12727 : 23719367 : specs->typespec_word = cts_int;
12728 : 23719367 : specs->locations[cdw_typespec] = loc;
12729 : : }
12730 : 23719367 : return specs;
12731 : 3365260 : case RID_FLOAT:
12732 : 3365260 : if (specs->long_p)
12733 : 44 : error_at (loc,
12734 : : "both %qs and %qs in declaration specifiers",
12735 : : "long", "float");
12736 : 3365216 : else if (specs->short_p)
12737 : 21 : error_at (loc,
12738 : : "both %qs and %qs in declaration specifiers",
12739 : : "short", "float");
12740 : 3365195 : else if (specs->signed_p)
12741 : 5 : error_at (loc,
12742 : : "both %qs and %qs in declaration specifiers",
12743 : : "signed", "float");
12744 : 3365190 : else if (specs->unsigned_p)
12745 : 5 : error_at (loc,
12746 : : "both %qs and %qs in declaration specifiers",
12747 : : "unsigned", "float");
12748 : 3365185 : else if (specs->saturating_p)
12749 : 0 : error_at (loc,
12750 : : "both %qs and %qs in declaration specifiers",
12751 : : "_Sat", "float");
12752 : : else
12753 : : {
12754 : 3365185 : specs->typespec_word = cts_float;
12755 : 3365185 : specs->locations[cdw_typespec] = loc;
12756 : : }
12757 : 3365260 : return specs;
12758 : 6054393 : case RID_DOUBLE:
12759 : 6054393 : if (specs->long_long_p)
12760 : 22 : error_at (loc,
12761 : : "both %qs and %qs in declaration specifiers",
12762 : : "long long", "double");
12763 : 6054371 : else if (specs->short_p)
12764 : 21 : error_at (loc,
12765 : : "both %qs and %qs in declaration specifiers",
12766 : : "short", "double");
12767 : 6054350 : else if (specs->signed_p)
12768 : 13 : error_at (loc,
12769 : : "both %qs and %qs in declaration specifiers",
12770 : : "signed", "double");
12771 : 6054337 : else if (specs->unsigned_p)
12772 : 13 : error_at (loc,
12773 : : "both %qs and %qs in declaration specifiers",
12774 : : "unsigned", "double");
12775 : 6054324 : else if (specs->saturating_p)
12776 : 0 : error_at (loc,
12777 : : "both %qs and %qs in declaration specifiers",
12778 : : "_Sat", "double");
12779 : : else
12780 : : {
12781 : 6054324 : specs->typespec_word = cts_double;
12782 : 6054324 : specs->locations[cdw_typespec] = loc;
12783 : : }
12784 : 6054393 : return specs;
12785 : 10167025 : CASE_RID_FLOATN_NX:
12786 : 10167025 : specs->u.floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
12787 : 10167025 : if (!in_system_header_at (input_location))
12788 : 53644 : pedwarn_c11 (loc, OPT_Wpedantic,
12789 : : "ISO C does not support the %<_Float%d%s%> type"
12790 : : " before C23",
12791 : 53644 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12792 : 53644 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12793 : : ? "x"
12794 : : : "");
12795 : :
12796 : 10167025 : if (specs->long_p)
12797 : 0 : error_at (loc,
12798 : : "both %qs and %<_Float%d%s%> in declaration "
12799 : : "specifiers", "long",
12800 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12801 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12802 : : ? "x"
12803 : : : "");
12804 : 10167025 : else if (specs->short_p)
12805 : 0 : error_at (loc,
12806 : : "both %qs and %<_Float%d%s%> in declaration "
12807 : : "specifiers", "short",
12808 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12809 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12810 : : ? "x"
12811 : : : "");
12812 : 10167025 : else if (specs->signed_p)
12813 : 0 : error_at (loc,
12814 : : "both %qs and %<_Float%d%s%> in declaration "
12815 : : "specifiers", "signed",
12816 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12817 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12818 : : ? "x"
12819 : : : "");
12820 : 10167025 : else if (specs->unsigned_p)
12821 : 0 : error_at (loc,
12822 : : "both %qs and %<_Float%d%s%> in declaration "
12823 : : "specifiers", "unsigned",
12824 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12825 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12826 : : ? "x"
12827 : : : "");
12828 : 10167025 : else if (specs->saturating_p)
12829 : 0 : error_at (loc,
12830 : : "both %qs and %<_Float%d%s%> in declaration "
12831 : : "specifiers", "_Sat",
12832 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12833 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12834 : : ? "x"
12835 : : : "");
12836 : 10167025 : else if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
12837 : : {
12838 : 88 : specs->typespec_word = cts_floatn_nx;
12839 : 176 : error_at (loc,
12840 : : "%<_Float%d%s%> is not supported on this target",
12841 : 88 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12842 : 88 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12843 : : ? "x"
12844 : : : "");
12845 : : }
12846 : : else
12847 : : {
12848 : 10166937 : specs->typespec_word = cts_floatn_nx;
12849 : 10166937 : specs->locations[cdw_typespec] = loc;
12850 : : }
12851 : 10167025 : return specs;
12852 : 47633 : case RID_DFLOAT32:
12853 : 47633 : case RID_DFLOAT64:
12854 : 47633 : case RID_DFLOAT128:
12855 : 47633 : case RID_DFLOAT64X:
12856 : 47633 : {
12857 : 47633 : const char *str;
12858 : 47633 : if (i == RID_DFLOAT32)
12859 : : str = "_Decimal32";
12860 : : else if (i == RID_DFLOAT64)
12861 : : str = "_Decimal64";
12862 : : else if (i == RID_DFLOAT128)
12863 : : str = "_Decimal128";
12864 : : else
12865 : 47633 : str = "_Decimal64x";
12866 : 47633 : if (specs->long_long_p)
12867 : 18 : error_at (loc,
12868 : : "both %qs and %qs in declaration specifiers",
12869 : : "long long", str);
12870 : 47633 : if (specs->long_p)
12871 : 33 : error_at (loc,
12872 : : "both %qs and %qs in declaration specifiers",
12873 : : "long", str);
12874 : 47600 : else if (specs->short_p)
12875 : 18 : error_at (loc,
12876 : : "both %qs and %qs in declaration specifiers",
12877 : : "short", str);
12878 : 47582 : else if (specs->signed_p)
12879 : 6 : error_at (loc,
12880 : : "both %qs and %qs in declaration specifiers",
12881 : : "signed", str);
12882 : 47576 : else if (specs->unsigned_p)
12883 : 3 : error_at (loc,
12884 : : "both %qs and %qs in declaration specifiers",
12885 : : "unsigned", str);
12886 : 47573 : else if (specs->complex_p)
12887 : 3 : error_at (loc,
12888 : : "both %qs and %qs in declaration specifiers",
12889 : : "complex", str);
12890 : 47570 : else if (specs->saturating_p)
12891 : 0 : error_at (loc,
12892 : : "both %qs and %qs in declaration specifiers",
12893 : : "_Sat", str);
12894 : 47570 : else if (i == RID_DFLOAT32)
12895 : 15957 : specs->typespec_word = cts_dfloat32;
12896 : 31613 : else if (i == RID_DFLOAT64)
12897 : 15832 : specs->typespec_word = cts_dfloat64;
12898 : 15781 : else if (i == RID_DFLOAT128)
12899 : 15734 : specs->typespec_word = cts_dfloat128;
12900 : : else
12901 : 47 : specs->typespec_word = cts_dfloat64x;
12902 : 47633 : specs->locations[cdw_typespec] = loc;
12903 : : }
12904 : 47633 : if (!targetm.decimal_float_supported_p ())
12905 : 0 : error_at (loc,
12906 : : "decimal floating-point not supported "
12907 : : "for this target");
12908 : 47633 : pedwarn_c11 (loc, OPT_Wpedantic,
12909 : : "ISO C does not support decimal floating-point "
12910 : : "before C23");
12911 : 47633 : return specs;
12912 : 119 : case RID_FRACT:
12913 : 119 : case RID_ACCUM:
12914 : 119 : {
12915 : 119 : const char *str;
12916 : 119 : if (i == RID_FRACT)
12917 : : str = "_Fract";
12918 : : else
12919 : 59 : str = "_Accum";
12920 : 119 : if (specs->complex_p)
12921 : 0 : error_at (loc,
12922 : : "both %qs and %qs in declaration specifiers",
12923 : : "complex", str);
12924 : 119 : else if (i == RID_FRACT)
12925 : 60 : specs->typespec_word = cts_fract;
12926 : : else
12927 : 59 : specs->typespec_word = cts_accum;
12928 : 119 : specs->locations[cdw_typespec] = loc;
12929 : : }
12930 : 119 : if (!targetm.fixed_point_supported_p ())
12931 : 119 : error_at (loc,
12932 : : "fixed-point types not supported for this target");
12933 : 119 : pedwarn (loc, OPT_Wpedantic,
12934 : : "ISO C does not support fixed-point types");
12935 : 119 : return specs;
12936 : 45578 : case RID_BITINT:
12937 : 45578 : if (specs->long_p)
12938 : 2 : error_at (loc,
12939 : : "both %qs and %qs in declaration specifiers",
12940 : : "long", "_BitInt");
12941 : 45576 : else if (specs->short_p)
12942 : 1 : error_at (loc,
12943 : : "both %qs and %qs in declaration specifiers",
12944 : : "short", "_BitInt");
12945 : 45575 : else if (specs->complex_p)
12946 : 1 : error_at (loc,
12947 : : "both %qs and %qs in declaration specifiers",
12948 : : "complex", "_BitInt");
12949 : 45574 : else if (specs->saturating_p)
12950 : 0 : error_at (loc,
12951 : : "both %qs and %qs in declaration specifiers",
12952 : : "_Sat", "_BitInt");
12953 : : else
12954 : : {
12955 : 45574 : specs->typespec_word = cts_bitint;
12956 : 45574 : specs->locations[cdw_typespec] = loc;
12957 : 45574 : specs->u.bitint_prec = -1;
12958 : 45574 : if (error_operand_p (spec.expr))
12959 : 7 : return specs;
12960 : 45574 : if (TREE_CODE (spec.expr) != INTEGER_CST
12961 : 45574 : || !INTEGRAL_TYPE_P (TREE_TYPE (spec.expr)))
12962 : : {
12963 : 1 : error_at (loc, "%<_BitInt%> argument is not an integer "
12964 : : "constant expression");
12965 : 1 : return specs;
12966 : : }
12967 : 45573 : if (tree_int_cst_sgn (spec.expr) <= 0)
12968 : : {
12969 : 4 : error_at (loc, "%<_BitInt%> argument %qE is not a "
12970 : : "positive integer constant expression",
12971 : : spec.expr);
12972 : 4 : return specs;
12973 : : }
12974 : 45569 : if (wi::to_widest (spec.expr) > WIDE_INT_MAX_PRECISION - 1)
12975 : : {
12976 : 2 : error_at (loc, "%<_BitInt%> argument %qE is larger than "
12977 : : "%<BITINT_MAXWIDTH%> %qd",
12978 : : spec.expr, (int) WIDE_INT_MAX_PRECISION - 1);
12979 : 2 : return specs;
12980 : : }
12981 : 45567 : specs->u.bitint_prec = tree_to_uhwi (spec.expr);
12982 : 45567 : struct bitint_info info;
12983 : 45567 : if (!targetm.c.bitint_type_info (specs->u.bitint_prec,
12984 : : &info))
12985 : : {
12986 : 0 : sorry_at (loc, "%<_BitInt(%d)%> is not supported on "
12987 : : "this target", specs->u.bitint_prec);
12988 : 0 : specs->u.bitint_prec = -1;
12989 : 0 : return specs;
12990 : : }
12991 : : }
12992 : 45571 : return specs;
12993 : : default:
12994 : : /* ObjC reserved word "id", handled below. */
12995 : : break;
12996 : : }
12997 : : }
12998 : : }
12999 : :
13000 : : /* Now we have a typedef (a TYPE_DECL node), an identifier (some
13001 : : form of ObjC type, cases such as "int" and "long" being handled
13002 : : above), a TYPE (struct, union, enum and typeof specifiers) or an
13003 : : ERROR_MARK. In none of these cases may there have previously
13004 : : been any type specifiers. */
13005 : 247835807 : if (specs->type || specs->typespec_word != cts_none
13006 : 247835795 : || specs->long_p || specs->short_p || specs->signed_p
13007 : 247835792 : || specs->unsigned_p || specs->complex_p)
13008 : 21 : error_at (loc, "two or more data types in declaration specifiers");
13009 : 247835786 : else if (TREE_CODE (type) == TYPE_DECL)
13010 : : {
13011 : 244589193 : mark_decl_used (type, false);
13012 : 244589193 : specs->type = TREE_TYPE (type);
13013 : 244589193 : if (TREE_TYPE (type) != error_mark_node)
13014 : : {
13015 : 244589180 : specs->decl_attr = DECL_ATTRIBUTES (type);
13016 : 244589180 : specs->typedef_p = true;
13017 : 244589180 : specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
13018 : 244589180 : specs->locations[cdw_typedef] = loc;
13019 : :
13020 : : /* If this typedef name is defined in a struct, then a C++
13021 : : lookup would return a different value. */
13022 : 244589180 : if (warn_cxx_compat
13023 : 244589180 : && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
13024 : 2 : warning_at (loc, OPT_Wc___compat,
13025 : : "C++ lookup of %qD would return a field, not a type",
13026 : : type);
13027 : :
13028 : : /* If we are parsing a struct, record that a struct field
13029 : : used a typedef. */
13030 : 244589180 : if (warn_cxx_compat && struct_parse_info != NULL)
13031 : 1751 : struct_parse_info->typedefs_seen.safe_push (type);
13032 : : }
13033 : : }
13034 : 3246593 : else if (TREE_CODE (type) == IDENTIFIER_NODE)
13035 : : {
13036 : 0 : tree t = lookup_name (type);
13037 : 0 : if (!t || TREE_CODE (t) != TYPE_DECL)
13038 : 0 : error_at (loc, "%qE fails to be a typedef or built in type", type);
13039 : 0 : else if (TREE_TYPE (t) == error_mark_node)
13040 : : ;
13041 : : else
13042 : : {
13043 : 0 : specs->type = TREE_TYPE (t);
13044 : 0 : specs->locations[cdw_typespec] = loc;
13045 : : }
13046 : : }
13047 : : else
13048 : : {
13049 : 3246593 : if (TREE_CODE (type) != ERROR_MARK)
13050 : : {
13051 : 3246386 : if (spec.kind == ctsk_typeof)
13052 : : {
13053 : 824491 : specs->typedef_p = true;
13054 : 824491 : specs->locations[cdw_typedef] = loc;
13055 : : }
13056 : 3246386 : if (spec.expr)
13057 : : {
13058 : 912 : if (specs->expr)
13059 : 0 : specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
13060 : : specs->expr, spec.expr);
13061 : : else
13062 : 912 : specs->expr = spec.expr;
13063 : 912 : specs->expr_const_operands &= spec.expr_const_operands;
13064 : : }
13065 : : }
13066 : 3246593 : specs->type = type;
13067 : 3246593 : if (spec.has_enum_type_specifier
13068 : 168 : && spec.kind != ctsk_tagdef)
13069 : 49 : specs->enum_type_specifier_ref_p = true;
13070 : : }
13071 : :
13072 : : return specs;
13073 : : }
13074 : :
13075 : : /* Add the storage class specifier or function specifier SCSPEC to the
13076 : : declaration specifiers SPECS, returning SPECS. */
13077 : :
13078 : : struct c_declspecs *
13079 : 89839120 : declspecs_add_scspec (location_t loc,
13080 : : struct c_declspecs *specs,
13081 : : tree scspec)
13082 : : {
13083 : 89839120 : enum rid i;
13084 : 89839120 : enum c_storage_class n = csc_none;
13085 : 89839120 : bool dupe = false;
13086 : 89839120 : specs->declspecs_seen_p = true;
13087 : 89839120 : specs->non_std_attrs_seen_p = true;
13088 : 89839120 : gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
13089 : : && C_IS_RESERVED_WORD (scspec));
13090 : 89839120 : i = C_RID_CODE (scspec);
13091 : 89839120 : if (specs->non_sc_seen_p)
13092 : 2093 : warning (OPT_Wold_style_declaration,
13093 : : "%qE is not at beginning of declaration", scspec);
13094 : 89839120 : switch (i)
13095 : : {
13096 : 35444144 : case RID_INLINE:
13097 : : /* C99 permits duplicate inline. Although of doubtful utility,
13098 : : it seems simplest to permit it in gnu89 mode as well, as
13099 : : there is also little utility in maintaining this as a
13100 : : difference between gnu89 and C99 inline. */
13101 : 35444144 : dupe = false;
13102 : 35444144 : specs->inline_p = true;
13103 : 35444144 : specs->locations[cdw_inline] = loc;
13104 : 35444144 : break;
13105 : 22997 : case RID_NORETURN:
13106 : : /* Duplicate _Noreturn is permitted. */
13107 : 22997 : dupe = false;
13108 : 22997 : specs->noreturn_p = true;
13109 : 22997 : specs->locations[cdw_noreturn] = loc;
13110 : 22997 : break;
13111 : 2882 : case RID_THREAD:
13112 : 2882 : dupe = specs->thread_p;
13113 : 2882 : if (specs->storage_class == csc_auto)
13114 : 2 : error ("%qE used with %<auto%>", scspec);
13115 : 2880 : else if (specs->storage_class == csc_register)
13116 : 3 : error ("%qE used with %<register%>", scspec);
13117 : 2877 : else if (specs->storage_class == csc_typedef)
13118 : 2 : error ("%qE used with %<typedef%>", scspec);
13119 : 2875 : else if (specs->constexpr_p)
13120 : 2 : error ("%qE used with %<constexpr%>", scspec);
13121 : : else
13122 : : {
13123 : 2873 : specs->thread_p = true;
13124 : 2873 : specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
13125 : 2873 : "__thread") == 0);
13126 : : /* A diagnostic is not required for the use of this
13127 : : identifier in the implementation namespace; only diagnose
13128 : : it for the C11 spelling because of existing code using
13129 : : the other spelling. */
13130 : 2873 : if (!specs->thread_gnu_p)
13131 : : {
13132 : 116 : if (flag_isoc99)
13133 : 113 : pedwarn_c99 (loc, OPT_Wpedantic,
13134 : : "ISO C99 does not support %qE", scspec);
13135 : : else
13136 : 3 : pedwarn_c99 (loc, OPT_Wpedantic,
13137 : : "ISO C90 does not support %qE", scspec);
13138 : : }
13139 : 2873 : specs->locations[cdw_thread] = loc;
13140 : : }
13141 : : break;
13142 : 262 : case RID_AUTO:
13143 : 262 : if (flag_isoc23
13144 : 223 : && specs->typespec_kind == ctsk_none
13145 : 212 : && specs->storage_class != csc_typedef)
13146 : : {
13147 : : /* "auto" potentially used for type deduction. */
13148 : 211 : if (specs->c23_auto_p)
13149 : 2 : error ("duplicate %qE", scspec);
13150 : 211 : specs->c23_auto_p = true;
13151 : 211 : return specs;
13152 : : }
13153 : 51 : n = csc_auto;
13154 : : /* auto may only be used with another storage class specifier,
13155 : : such as constexpr, if the type is inferred. */
13156 : 51 : if (specs->constexpr_p)
13157 : 2 : error ("%qE used with %<constexpr%>", scspec);
13158 : : break;
13159 : 49661544 : case RID_EXTERN:
13160 : 49661544 : n = csc_extern;
13161 : : /* Diagnose "__thread extern". */
13162 : 49661544 : if (specs->thread_p && specs->thread_gnu_p)
13163 : 2 : error ("%<__thread%> before %<extern%>");
13164 : : break;
13165 : : case RID_REGISTER:
13166 : : n = csc_register;
13167 : : break;
13168 : 424060 : case RID_STATIC:
13169 : 424060 : n = csc_static;
13170 : : /* Diagnose "__thread static". */
13171 : 424060 : if (specs->thread_p && specs->thread_gnu_p)
13172 : 1 : error ("%<__thread%> before %<static%>");
13173 : : break;
13174 : 4279491 : case RID_TYPEDEF:
13175 : 4279491 : n = csc_typedef;
13176 : 4279491 : if (specs->c23_auto_p)
13177 : : {
13178 : 1 : error ("%<typedef%> used with %<auto%>");
13179 : 1 : specs->c23_auto_p = false;
13180 : : }
13181 : : break;
13182 : 612 : case RID_CONSTEXPR:
13183 : 612 : dupe = specs->constexpr_p;
13184 : 612 : if (specs->storage_class == csc_extern)
13185 : 1 : error ("%qE used with %<extern%>", scspec);
13186 : 611 : else if (specs->storage_class == csc_typedef)
13187 : 1 : error ("%qE used with %<typedef%>", scspec);
13188 : 610 : else if (specs->storage_class == csc_auto)
13189 : : /* auto may only be used with another storage class specifier,
13190 : : such as constexpr, if the type is inferred. */
13191 : 2 : error ("%qE used with %<auto%>", scspec);
13192 : 608 : else if (specs->thread_p)
13193 : 4 : error ("%qE used with %qs", scspec,
13194 : 2 : specs->thread_gnu_p ? "__thread" : "_Thread_local");
13195 : : else
13196 : 606 : specs->constexpr_p = true;
13197 : : break;
13198 : 0 : default:
13199 : 0 : gcc_unreachable ();
13200 : : }
13201 : 89838915 : if (n != csc_none && n == specs->storage_class)
13202 : : dupe = true;
13203 : 89838901 : if (dupe)
13204 : : {
13205 : 12 : if (i == RID_THREAD)
13206 : 2 : error ("duplicate %<_Thread_local%> or %<__thread%>");
13207 : : else
13208 : 10 : error ("duplicate %qE", scspec);
13209 : : }
13210 : 89838909 : if (n != csc_none)
13211 : : {
13212 : 54368274 : if (specs->storage_class != csc_none && n != specs->storage_class)
13213 : : {
13214 : 7 : error ("multiple storage classes in declaration specifiers");
13215 : : }
13216 : : else
13217 : : {
13218 : 54368267 : specs->storage_class = n;
13219 : 54368267 : specs->locations[cdw_storage_class] = loc;
13220 : 54368267 : if (n != csc_extern && n != csc_static && specs->thread_p)
13221 : : {
13222 : 8 : error ("%qs used with %qE",
13223 : 8 : specs->thread_gnu_p ? "__thread" : "_Thread_local",
13224 : : scspec);
13225 : 8 : specs->thread_p = false;
13226 : : }
13227 : 54368267 : if (n != csc_auto && n != csc_register && n != csc_static
13228 : 53941032 : && specs->constexpr_p)
13229 : : {
13230 : 2 : error ("%<constexpr%> used with %qE", scspec);
13231 : 2 : specs->constexpr_p = false;
13232 : : }
13233 : : }
13234 : : }
13235 : : return specs;
13236 : : }
13237 : :
13238 : : /* Add the attributes ATTRS to the declaration specifiers SPECS,
13239 : : returning SPECS. */
13240 : :
13241 : : struct c_declspecs *
13242 : 35754687 : declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
13243 : : {
13244 : 35754687 : specs->attrs = chainon (attrs, specs->attrs);
13245 : 35754687 : specs->locations[cdw_attributes] = loc;
13246 : 35754687 : specs->declspecs_seen_p = true;
13247 : : /* In the case of standard attributes at the start of the
13248 : : declaration, the caller will reset this. */
13249 : 35754687 : specs->non_std_attrs_seen_p = true;
13250 : 35754687 : return specs;
13251 : : }
13252 : :
13253 : : /* Add an _Alignas specifier (expression ALIGN, or type whose
13254 : : alignment is ALIGN) to the declaration specifiers SPECS, returning
13255 : : SPECS. */
13256 : : struct c_declspecs *
13257 : 200 : declspecs_add_alignas (location_t loc,
13258 : : struct c_declspecs *specs, tree align)
13259 : : {
13260 : 200 : specs->alignas_p = true;
13261 : 200 : specs->locations[cdw_alignas] = loc;
13262 : 200 : if (align == error_mark_node)
13263 : : return specs;
13264 : :
13265 : : /* Only accept the alignment if it's valid and greater than
13266 : : the current one. Zero is invalid but by C11 required to
13267 : : be silently ignored. */
13268 : 198 : int align_log = check_user_alignment (align, false, /* warn_zero = */false);
13269 : 198 : if (align_log > specs->align_log)
13270 : 165 : specs->align_log = align_log;
13271 : : return specs;
13272 : : }
13273 : :
13274 : : /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
13275 : : specifiers with any other type specifier to determine the resulting
13276 : : type. This is where ISO C checks on complex types are made, since
13277 : : "_Complex long" is a prefix of the valid ISO C type "_Complex long
13278 : : double". Also apply postfix standard attributes to modify the type. */
13279 : :
13280 : : struct c_declspecs *
13281 : 312368687 : finish_declspecs (struct c_declspecs *specs)
13282 : : {
13283 : : /* If a type was specified as a whole, we have no modifiers and are
13284 : : done. */
13285 : 312368687 : if (specs->type != NULL_TREE)
13286 : : {
13287 : 247835740 : gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
13288 : : && !specs->signed_p && !specs->unsigned_p
13289 : : && !specs->complex_p && !specs->c23_auto_p);
13290 : :
13291 : : /* Set a dummy type. */
13292 : 247835740 : if (TREE_CODE (specs->type) == ERROR_MARK)
13293 : 183 : specs->type = integer_type_node;
13294 : 247835740 : goto handle_postfix_attrs;
13295 : : }
13296 : :
13297 : : /* If none of "void", "_Bool", "char", "int", "float" or "double"
13298 : : has been specified, treat it as "int" unless "_Complex" is
13299 : : present and there are no other specifiers. If we just have
13300 : : "_Complex", it is equivalent to "_Complex double", but e.g.
13301 : : "_Complex short" is equivalent to "_Complex short int". */
13302 : 64532947 : if (specs->typespec_word == cts_none)
13303 : : {
13304 : 4172414 : if (specs->saturating_p)
13305 : : {
13306 : 1 : error_at (specs->locations[cdw_saturating],
13307 : : "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
13308 : 1 : if (!targetm.fixed_point_supported_p ())
13309 : 1 : error_at (specs->locations[cdw_saturating],
13310 : : "fixed-point types not supported for this target");
13311 : 1 : specs->typespec_word = cts_fract;
13312 : : }
13313 : 4172413 : else if (specs->long_p || specs->short_p
13314 : 193067 : || specs->signed_p || specs->unsigned_p)
13315 : : {
13316 : 4161979 : specs->typespec_word = cts_int;
13317 : : }
13318 : 10434 : else if (specs->complex_p)
13319 : : {
13320 : 130 : specs->typespec_word = cts_double;
13321 : 130 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13322 : : "ISO C does not support plain %<complex%> meaning "
13323 : : "%<double complex%>");
13324 : : }
13325 : 10304 : else if (specs->c23_auto_p)
13326 : : {
13327 : : /* Type to be filled in later, including applying postfix
13328 : : attributes. This warning only actually appears for
13329 : : -Wc11-c23-compat in C23 mode; in older modes, there may
13330 : : be a warning or pedwarn for implicit "int" instead, or
13331 : : other errors for use of auto at file scope. */
13332 : 93 : pedwarn_c11 (input_location, OPT_Wpedantic,
13333 : : "ISO C does not support %<auto%> type deduction "
13334 : : "before C23");
13335 : 93 : return specs;
13336 : : }
13337 : : else
13338 : : {
13339 : 10211 : specs->typespec_word = cts_int;
13340 : 10211 : specs->default_int_p = true;
13341 : : /* We don't diagnose this here because grokdeclarator will
13342 : : give more specific diagnostics according to whether it is
13343 : : a function definition. */
13344 : : }
13345 : : }
13346 : :
13347 : : /* If "signed" was specified, record this to distinguish "int" and
13348 : : "signed int" in the case of a bit-field with
13349 : : -funsigned-bitfields. */
13350 : 64532854 : specs->explicit_signed_p = specs->signed_p;
13351 : :
13352 : : /* Now compute the actual type. */
13353 : 64532854 : gcc_assert (!specs->c23_auto_p);
13354 : 64532854 : switch (specs->typespec_word)
13355 : : {
13356 : 1833 : case cts_auto_type:
13357 : 1833 : gcc_assert (!specs->long_p && !specs->short_p
13358 : : && !specs->signed_p && !specs->unsigned_p
13359 : : && !specs->complex_p);
13360 : : /* Type to be filled in later. */
13361 : 1833 : if (specs->postfix_attrs)
13362 : 2 : error ("%<__auto_type%> followed by %<[[]]%> attributes");
13363 : : break;
13364 : 7806581 : case cts_void:
13365 : 7806581 : gcc_assert (!specs->long_p && !specs->short_p
13366 : : && !specs->signed_p && !specs->unsigned_p
13367 : : && !specs->complex_p);
13368 : 7806581 : specs->type = void_type_node;
13369 : 7806581 : break;
13370 : 84530 : case cts_bool:
13371 : 84530 : gcc_assert (!specs->long_p && !specs->short_p
13372 : : && !specs->signed_p && !specs->unsigned_p
13373 : : && !specs->complex_p);
13374 : 84530 : specs->type = boolean_type_node;
13375 : 84530 : break;
13376 : 9016926 : case cts_char:
13377 : 9016926 : gcc_assert (!specs->long_p && !specs->short_p);
13378 : 9016926 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13379 : 9016926 : if (specs->signed_p)
13380 : 191083 : specs->type = signed_char_type_node;
13381 : 8825843 : else if (specs->unsigned_p)
13382 : 893417 : specs->type = unsigned_char_type_node;
13383 : : else
13384 : 7932426 : specs->type = char_type_node;
13385 : 9016926 : if (specs->complex_p)
13386 : : {
13387 : 3853 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13388 : : "ISO C does not support complex integer types");
13389 : 3853 : specs->type = build_complex_type (specs->type);
13390 : : }
13391 : : break;
13392 : 51501 : case cts_int_n:
13393 : 51501 : gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
13394 : 51501 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13395 : 51501 : if (! int_n_enabled_p[specs->u.int_n_idx])
13396 : 0 : specs->type = integer_type_node;
13397 : : else
13398 : 51501 : specs->type = (specs->unsigned_p
13399 : 51501 : ? int_n_trees[specs->u.int_n_idx].unsigned_type
13400 : : : int_n_trees[specs->u.int_n_idx].signed_type);
13401 : 51501 : if (specs->complex_p)
13402 : : {
13403 : 199 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13404 : : "ISO C does not support complex integer types");
13405 : 199 : specs->type = build_complex_type (specs->type);
13406 : : }
13407 : : break;
13408 : 27891555 : case cts_int:
13409 : 27891555 : gcc_assert (!(specs->long_p && specs->short_p));
13410 : 27891555 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13411 : 27891555 : if (specs->long_long_p)
13412 : 2937618 : specs->type = (specs->unsigned_p
13413 : 2937618 : ? long_long_unsigned_type_node
13414 : : : long_long_integer_type_node);
13415 : 24953937 : else if (specs->long_p)
13416 : 2183667 : specs->type = (specs->unsigned_p
13417 : 2183667 : ? long_unsigned_type_node
13418 : : : long_integer_type_node);
13419 : 22770270 : else if (specs->short_p)
13420 : 1694375 : specs->type = (specs->unsigned_p
13421 : 1694375 : ? short_unsigned_type_node
13422 : : : short_integer_type_node);
13423 : : else
13424 : 21075895 : specs->type = (specs->unsigned_p
13425 : 21075895 : ? unsigned_type_node
13426 : : : integer_type_node);
13427 : 27891555 : if (specs->complex_p)
13428 : : {
13429 : 14910 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13430 : : "ISO C does not support complex integer types");
13431 : 14910 : specs->type = build_complex_type (specs->type);
13432 : : }
13433 : : break;
13434 : 3365185 : case cts_float:
13435 : 3365185 : gcc_assert (!specs->long_p && !specs->short_p
13436 : : && !specs->signed_p && !specs->unsigned_p);
13437 : 6730370 : specs->type = (specs->complex_p
13438 : 3365185 : ? complex_float_type_node
13439 : : : float_type_node);
13440 : 3365185 : break;
13441 : 6054454 : case cts_double:
13442 : 6054454 : gcc_assert (!specs->long_long_p && !specs->short_p
13443 : : && !specs->signed_p && !specs->unsigned_p);
13444 : 6054454 : if (specs->long_p)
13445 : : {
13446 : 2601432 : specs->type = (specs->complex_p
13447 : 2601432 : ? complex_long_double_type_node
13448 : : : long_double_type_node);
13449 : : }
13450 : : else
13451 : : {
13452 : 3453022 : specs->type = (specs->complex_p
13453 : 3453022 : ? complex_double_type_node
13454 : : : double_type_node);
13455 : : }
13456 : : break;
13457 : 10167025 : case cts_floatn_nx:
13458 : 10167025 : gcc_assert (!specs->long_p && !specs->short_p
13459 : : && !specs->signed_p && !specs->unsigned_p);
13460 : 10167025 : if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
13461 : 88 : specs->type = integer_type_node;
13462 : 10166937 : else if (specs->complex_p)
13463 : 1538159 : specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13464 : : else
13465 : 8628778 : specs->type = FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13466 : : break;
13467 : 47570 : case cts_dfloat32:
13468 : 47570 : case cts_dfloat64:
13469 : 47570 : case cts_dfloat128:
13470 : 47570 : case cts_dfloat64x:
13471 : 47570 : gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
13472 : : && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
13473 : 47570 : if (!targetm.decimal_float_supported_p ())
13474 : 0 : specs->type = integer_type_node;
13475 : 47570 : else if (specs->typespec_word == cts_dfloat32)
13476 : 15957 : specs->type = dfloat32_type_node;
13477 : 31613 : else if (specs->typespec_word == cts_dfloat64)
13478 : 15832 : specs->type = dfloat64_type_node;
13479 : 15781 : else if (specs->typespec_word == cts_dfloat128)
13480 : 15734 : specs->type = dfloat128_type_node;
13481 : : else
13482 : 47 : specs->type = dfloat64x_type_node;
13483 : : break;
13484 : 61 : case cts_fract:
13485 : 61 : gcc_assert (!specs->complex_p);
13486 : 61 : if (!targetm.fixed_point_supported_p ())
13487 : 61 : specs->type = integer_type_node;
13488 : 0 : else if (specs->saturating_p)
13489 : : {
13490 : 0 : if (specs->long_long_p)
13491 : 0 : specs->type = specs->unsigned_p
13492 : 0 : ? sat_unsigned_long_long_fract_type_node
13493 : : : sat_long_long_fract_type_node;
13494 : 0 : else if (specs->long_p)
13495 : 0 : specs->type = specs->unsigned_p
13496 : 0 : ? sat_unsigned_long_fract_type_node
13497 : : : sat_long_fract_type_node;
13498 : 0 : else if (specs->short_p)
13499 : 0 : specs->type = specs->unsigned_p
13500 : 0 : ? sat_unsigned_short_fract_type_node
13501 : : : sat_short_fract_type_node;
13502 : : else
13503 : 0 : specs->type = specs->unsigned_p
13504 : 0 : ? sat_unsigned_fract_type_node
13505 : : : sat_fract_type_node;
13506 : : }
13507 : : else
13508 : : {
13509 : 0 : if (specs->long_long_p)
13510 : 0 : specs->type = specs->unsigned_p
13511 : 0 : ? unsigned_long_long_fract_type_node
13512 : : : long_long_fract_type_node;
13513 : 0 : else if (specs->long_p)
13514 : 0 : specs->type = specs->unsigned_p
13515 : 0 : ? unsigned_long_fract_type_node
13516 : : : long_fract_type_node;
13517 : 0 : else if (specs->short_p)
13518 : 0 : specs->type = specs->unsigned_p
13519 : 0 : ? unsigned_short_fract_type_node
13520 : : : short_fract_type_node;
13521 : : else
13522 : 0 : specs->type = specs->unsigned_p
13523 : 0 : ? unsigned_fract_type_node
13524 : : : fract_type_node;
13525 : : }
13526 : : break;
13527 : 59 : case cts_accum:
13528 : 59 : gcc_assert (!specs->complex_p);
13529 : 59 : if (!targetm.fixed_point_supported_p ())
13530 : 59 : specs->type = integer_type_node;
13531 : 0 : else if (specs->saturating_p)
13532 : : {
13533 : 0 : if (specs->long_long_p)
13534 : 0 : specs->type = specs->unsigned_p
13535 : 0 : ? sat_unsigned_long_long_accum_type_node
13536 : : : sat_long_long_accum_type_node;
13537 : 0 : else if (specs->long_p)
13538 : 0 : specs->type = specs->unsigned_p
13539 : 0 : ? sat_unsigned_long_accum_type_node
13540 : : : sat_long_accum_type_node;
13541 : 0 : else if (specs->short_p)
13542 : 0 : specs->type = specs->unsigned_p
13543 : 0 : ? sat_unsigned_short_accum_type_node
13544 : : : sat_short_accum_type_node;
13545 : : else
13546 : 0 : specs->type = specs->unsigned_p
13547 : 0 : ? sat_unsigned_accum_type_node
13548 : : : sat_accum_type_node;
13549 : : }
13550 : : else
13551 : : {
13552 : 0 : if (specs->long_long_p)
13553 : 0 : specs->type = specs->unsigned_p
13554 : 0 : ? unsigned_long_long_accum_type_node
13555 : : : long_long_accum_type_node;
13556 : 0 : else if (specs->long_p)
13557 : 0 : specs->type = specs->unsigned_p
13558 : 0 : ? unsigned_long_accum_type_node
13559 : : : long_accum_type_node;
13560 : 0 : else if (specs->short_p)
13561 : 0 : specs->type = specs->unsigned_p
13562 : 0 : ? unsigned_short_accum_type_node
13563 : : : short_accum_type_node;
13564 : : else
13565 : 0 : specs->type = specs->unsigned_p
13566 : 0 : ? unsigned_accum_type_node
13567 : : : accum_type_node;
13568 : : }
13569 : : break;
13570 : 45574 : case cts_bitint:
13571 : 45574 : gcc_assert (!specs->long_p && !specs->short_p
13572 : : && !specs->complex_p);
13573 : 45574 : if (!specs->unsigned_p && specs->u.bitint_prec == 1)
13574 : : {
13575 : 2 : error_at (specs->locations[cdw_typespec],
13576 : : "%<signed _BitInt%> argument must be at least 2");
13577 : 2 : specs->type = integer_type_node;
13578 : 2 : break;
13579 : : }
13580 : 45572 : if (specs->u.bitint_prec == -1)
13581 : 7 : specs->type = integer_type_node;
13582 : : else
13583 : : {
13584 : 67176 : pedwarn_c11 (specs->locations[cdw_typespec], OPT_Wpedantic,
13585 : : "ISO C does not support %<%s_BitInt(%d)%> before C23",
13586 : : specs->unsigned_p ? "unsigned "
13587 : 21611 : : specs->signed_p ? "signed " : "",
13588 : : specs->u.bitint_prec);
13589 : 45565 : specs->type = build_bitint_type (specs->u.bitint_prec,
13590 : 45565 : specs->unsigned_p);
13591 : : }
13592 : : break;
13593 : 0 : default:
13594 : 0 : gcc_unreachable ();
13595 : : }
13596 : 312368594 : handle_postfix_attrs:
13597 : 312368594 : if (specs->type != NULL)
13598 : : {
13599 : 312366761 : specs->postfix_attrs
13600 : 312366761 : = c_warn_type_attributes (specs->type, specs->postfix_attrs);
13601 : 312366761 : decl_attributes (&specs->type, specs->postfix_attrs, 0);
13602 : 312366761 : specs->postfix_attrs = NULL_TREE;
13603 : : }
13604 : :
13605 : : return specs;
13606 : : }
13607 : :
13608 : : /* Perform final processing on one file scope's declarations (or the
13609 : : external scope's declarations), GLOBALS. */
13610 : :
13611 : : static void
13612 : 214710 : c_write_global_declarations_1 (tree globals)
13613 : : {
13614 : 214710 : tree decl;
13615 : 214710 : bool reconsider;
13616 : :
13617 : : /* Process the decls in the order they were written. */
13618 : 390702159 : for (decl = globals; decl; decl = DECL_CHAIN (decl))
13619 : : {
13620 : : /* Check for used but undefined static functions using the C
13621 : : standard's definition of "used", and set TREE_NO_WARNING so
13622 : : that check_global_declaration doesn't repeat the check. */
13623 : 390487449 : if (TREE_CODE (decl) == FUNCTION_DECL
13624 : 372401289 : && DECL_INITIAL (decl) == NULL_TREE
13625 : 336270629 : && DECL_EXTERNAL (decl)
13626 : 726758073 : && !TREE_PUBLIC (decl))
13627 : : {
13628 : 119 : if (C_DECL_USED (decl))
13629 : : {
13630 : : /* TODO: Add OPT_Wundefined-inline. */
13631 : 30 : if (pedwarn (input_location, 0, "%q+F used but never defined",
13632 : : decl))
13633 : 29 : suppress_warning (decl /* OPT_Wundefined-inline. */);
13634 : : }
13635 : : /* For -Wunused-function warn about unused static prototypes. */
13636 : 89 : else if (warn_unused_function
13637 : 2 : && ! DECL_ARTIFICIAL (decl)
13638 : 91 : && ! warning_suppressed_p (decl, OPT_Wunused_function))
13639 : : {
13640 : 2 : if (warning (OPT_Wunused_function,
13641 : : "%q+F declared %<static%> but never defined",
13642 : : decl))
13643 : 2 : suppress_warning (decl, OPT_Wunused_function);
13644 : : }
13645 : : }
13646 : :
13647 : 390487449 : wrapup_global_declaration_1 (decl);
13648 : : }
13649 : :
13650 : 248533 : do
13651 : : {
13652 : 248533 : reconsider = false;
13653 : 503582546 : for (decl = globals; decl; decl = DECL_CHAIN (decl))
13654 : 503334013 : reconsider |= wrapup_global_declaration_2 (decl);
13655 : : }
13656 : : while (reconsider);
13657 : 214710 : }
13658 : :
13659 : : /* Preserve the external declarations scope across a garbage collect. */
13660 : : static GTY(()) tree ext_block;
13661 : :
13662 : : /* Collect all references relevant to SOURCE_FILE. */
13663 : :
13664 : : static void
13665 : 15 : collect_all_refs (const char *source_file)
13666 : : {
13667 : 15 : tree t;
13668 : 15 : unsigned i;
13669 : :
13670 : 30 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13671 : 15 : collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
13672 : :
13673 : 15 : collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
13674 : 15 : }
13675 : :
13676 : : /* Collect source file references at global level. */
13677 : :
13678 : : static void
13679 : 15 : collect_source_refs (void)
13680 : : {
13681 : 15 : tree t;
13682 : 15 : tree decls;
13683 : 15 : tree decl;
13684 : 15 : unsigned i;
13685 : :
13686 : 30 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13687 : : {
13688 : 15 : decls = DECL_INITIAL (t);
13689 : 82 : for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
13690 : 67 : if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13691 : 67 : collect_source_ref (DECL_SOURCE_FILE (decl));
13692 : : }
13693 : :
13694 : 44186 : for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
13695 : 44171 : if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13696 : 11 : collect_source_ref (DECL_SOURCE_FILE (decl));
13697 : 15 : }
13698 : :
13699 : : /* Free attribute access data that are not needed by the middle end. */
13700 : :
13701 : : static void
13702 : 107355 : free_attr_access_data ()
13703 : : {
13704 : 107355 : struct cgraph_node *n;
13705 : :
13706 : : /* Iterate over all functions declared in the translation unit. */
13707 : 72479282 : FOR_EACH_FUNCTION (n)
13708 : : {
13709 : 135716855 : for (tree parm = DECL_ARGUMENTS (n->decl); parm; parm = TREE_CHAIN (parm))
13710 : 99584569 : if (tree attrs = DECL_ATTRIBUTES (parm))
13711 : 97104 : attr_access::free_lang_data (attrs);
13712 : :
13713 : 36132286 : tree fntype = TREE_TYPE (n->decl);
13714 : 36132286 : if (!fntype || fntype == error_mark_node)
13715 : 0 : continue;
13716 : 36132286 : tree attrs = TYPE_ATTRIBUTES (fntype);
13717 : 36132286 : if (!attrs)
13718 : 35898326 : continue;
13719 : :
13720 : 233960 : attr_access::free_lang_data (attrs);
13721 : : }
13722 : 107355 : }
13723 : :
13724 : : /* Perform any final parser cleanups and generate initial debugging
13725 : : information. */
13726 : :
13727 : : void
13728 : 107717 : c_parse_final_cleanups (void)
13729 : : {
13730 : 107717 : tree t;
13731 : 107717 : unsigned i;
13732 : :
13733 : : /* We don't want to do this if generating a PCH. */
13734 : 107717 : if (pch_file)
13735 : 107717 : return;
13736 : :
13737 : 107355 : timevar_stop (TV_PHASE_PARSING);
13738 : 107355 : timevar_start (TV_PHASE_DEFERRED);
13739 : :
13740 : : /* Do the Objective-C stuff. This is where all the Objective-C
13741 : : module stuff gets generated (symtab, class/protocol/selector
13742 : : lists etc). */
13743 : 107355 : if (c_dialect_objc ())
13744 : 0 : objc_write_global_declarations ();
13745 : :
13746 : : /* Close the external scope. */
13747 : 107355 : ext_block = pop_scope ();
13748 : 107355 : external_scope = 0;
13749 : 107355 : gcc_assert (!current_scope);
13750 : :
13751 : : /* Handle -fdump-ada-spec[-slim]. */
13752 : 107355 : if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
13753 : : {
13754 : : /* Build a table of files to generate specs for */
13755 : 15 : collect_source_ref (main_input_filename);
13756 : 15 : if (!flag_dump_ada_spec_slim)
13757 : 15 : collect_source_refs ();
13758 : :
13759 : 15 : dump_ada_specs (collect_all_refs, NULL);
13760 : : }
13761 : :
13762 : : /* Process all file scopes in this compilation, and the external_scope,
13763 : : through wrapup_global_declarations. */
13764 : 214710 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13765 : 107355 : c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
13766 : 107355 : c_write_global_declarations_1 (BLOCK_VARS (ext_block));
13767 : :
13768 : : /* Call this to set cpp_implicit_aliases_done on all nodes. This is
13769 : : important for function multiversioning aliases to get resolved. */
13770 : 107355 : symtab->process_same_body_aliases ();
13771 : :
13772 : 107355 : if (!in_lto_p)
13773 : 107355 : free_attr_access_data ();
13774 : :
13775 : 107355 : timevar_stop (TV_PHASE_DEFERRED);
13776 : 107355 : timevar_start (TV_PHASE_PARSING);
13777 : :
13778 : 107355 : ext_block = NULL;
13779 : : }
13780 : :
13781 : : /* Register reserved keyword WORD as qualifier for address space AS. */
13782 : :
13783 : : void
13784 : 226528 : c_register_addr_space (const char *word, addr_space_t as)
13785 : : {
13786 : 226528 : int rid = RID_FIRST_ADDR_SPACE + as;
13787 : 226528 : tree id;
13788 : :
13789 : : /* Address space qualifiers are only supported
13790 : : in C with GNU extensions enabled. */
13791 : 226528 : if (c_dialect_objc () || flag_no_asm)
13792 : : return;
13793 : :
13794 : 214250 : id = get_identifier (word);
13795 : 214250 : C_SET_RID_CODE (id, rid);
13796 : 214250 : C_IS_RESERVED_WORD (id) = 1;
13797 : 214250 : ridpointers [rid] = id;
13798 : : }
13799 : :
13800 : : /* Return identifier to look up for omp declare reduction. */
13801 : :
13802 : : tree
13803 : 3250 : c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
13804 : : {
13805 : 3250 : const char *p = NULL;
13806 : 3250 : switch (reduction_code)
13807 : : {
13808 : : case PLUS_EXPR: p = "+"; break;
13809 : 267 : case MULT_EXPR: p = "*"; break;
13810 : 155 : case MINUS_EXPR: p = "-"; break;
13811 : 31 : case BIT_AND_EXPR: p = "&"; break;
13812 : 14 : case BIT_XOR_EXPR: p = "^"; break;
13813 : 92 : case BIT_IOR_EXPR: p = "|"; break;
13814 : 67 : case TRUTH_ANDIF_EXPR: p = "&&"; break;
13815 : 78 : case TRUTH_ORIF_EXPR: p = "||"; break;
13816 : 46 : case MIN_EXPR: p = "min"; break;
13817 : 83 : case MAX_EXPR: p = "max"; break;
13818 : : default:
13819 : : break;
13820 : : }
13821 : :
13822 : 833 : if (p == NULL)
13823 : : {
13824 : 213 : if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
13825 : 0 : return error_mark_node;
13826 : 213 : p = IDENTIFIER_POINTER (reduction_id);
13827 : : }
13828 : :
13829 : 3250 : const char prefix[] = "omp declare reduction ";
13830 : 3250 : size_t lenp = sizeof (prefix);
13831 : 3250 : size_t len = strlen (p);
13832 : 3250 : char *name = XALLOCAVEC (char, lenp + len);
13833 : 3250 : memcpy (name, prefix, lenp - 1);
13834 : 3250 : memcpy (name + lenp - 1, p, len + 1);
13835 : 3250 : return get_identifier (name);
13836 : : }
13837 : :
13838 : : /* Lookup REDUCTION_ID in the current scope, or create an artificial
13839 : : VAR_DECL, bind it into the current scope and return it. */
13840 : :
13841 : : tree
13842 : 161 : c_omp_reduction_decl (tree reduction_id)
13843 : : {
13844 : 161 : struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13845 : 161 : if (b != NULL && B_IN_CURRENT_SCOPE (b))
13846 : 47 : return b->decl;
13847 : :
13848 : 114 : tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
13849 : : reduction_id, integer_type_node);
13850 : 114 : DECL_ARTIFICIAL (decl) = 1;
13851 : 114 : DECL_EXTERNAL (decl) = 1;
13852 : 114 : TREE_STATIC (decl) = 1;
13853 : 114 : TREE_PUBLIC (decl) = 0;
13854 : 114 : bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
13855 : 114 : return decl;
13856 : : }
13857 : :
13858 : : /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
13859 : :
13860 : : tree
13861 : 273 : c_omp_reduction_lookup (tree reduction_id, tree type)
13862 : : {
13863 : 273 : struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13864 : 276 : while (b)
13865 : : {
13866 : 274 : tree t;
13867 : 294 : for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
13868 : 291 : if (comptypes (TREE_PURPOSE (t), type))
13869 : 271 : return TREE_VALUE (t);
13870 : 3 : b = b->shadowed;
13871 : : }
13872 : 2 : return error_mark_node;
13873 : : }
13874 : :
13875 : : /* Helper function called via walk_tree, to diagnose invalid
13876 : : #pragma omp declare reduction combiners or initializers. */
13877 : :
13878 : : tree
13879 : 1401 : c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
13880 : : {
13881 : 1401 : tree *vars = (tree *) data;
13882 : 1401 : if (SSA_VAR_P (*tp)
13883 : 413 : && !DECL_ARTIFICIAL (*tp)
13884 : 24 : && *tp != vars[0]
13885 : 24 : && *tp != vars[1])
13886 : : {
13887 : 24 : location_t loc = DECL_SOURCE_LOCATION (vars[0]);
13888 : 24 : if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
13889 : 10 : error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
13890 : : "variable %qD which is not %<omp_out%> nor %<omp_in%>",
13891 : : *tp);
13892 : : else
13893 : 14 : error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
13894 : : "to variable %qD which is not %<omp_priv%> nor "
13895 : : "%<omp_orig%>",
13896 : : *tp);
13897 : 24 : return *tp;
13898 : : }
13899 : : return NULL_TREE;
13900 : : }
13901 : :
13902 : : /* Return identifier to look up for omp declare mapper. */
13903 : :
13904 : : tree
13905 : 607 : c_omp_mapper_id (tree mapper_id)
13906 : : {
13907 : 607 : const char *p = NULL;
13908 : :
13909 : 607 : const char prefix[] = "omp declare mapper ";
13910 : :
13911 : 607 : if (mapper_id == NULL_TREE)
13912 : : p = "<default>";
13913 : 19 : else if (TREE_CODE (mapper_id) == IDENTIFIER_NODE)
13914 : 19 : p = IDENTIFIER_POINTER (mapper_id);
13915 : : else
13916 : 0 : return error_mark_node;
13917 : :
13918 : 607 : size_t lenp = sizeof (prefix);
13919 : 607 : size_t len = strlen (p);
13920 : 607 : char *name = XALLOCAVEC (char, lenp + len);
13921 : 607 : memcpy (name, prefix, lenp - 1);
13922 : 607 : memcpy (name + lenp - 1, p, len + 1);
13923 : 607 : return get_identifier (name);
13924 : : }
13925 : :
13926 : : /* Lookup MAPPER_ID in the current scope, or create an artificial
13927 : : VAR_DECL, bind it into the current scope and return it. */
13928 : :
13929 : : tree
13930 : 62 : c_omp_mapper_decl (tree mapper_id)
13931 : : {
13932 : 62 : struct c_binding *b = I_SYMBOL_BINDING (mapper_id);
13933 : 62 : if (b != NULL && B_IN_CURRENT_SCOPE (b))
13934 : 22 : return b->decl;
13935 : :
13936 : 40 : tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
13937 : : mapper_id, integer_type_node);
13938 : 40 : DECL_ARTIFICIAL (decl) = 1;
13939 : 40 : DECL_EXTERNAL (decl) = 1;
13940 : 40 : TREE_STATIC (decl) = 1;
13941 : 40 : TREE_PUBLIC (decl) = 0;
13942 : 40 : bind (mapper_id, decl, current_scope, true, false, BUILTINS_LOCATION);
13943 : 40 : return decl;
13944 : : }
13945 : :
13946 : : /* Lookup MAPPER_ID in the first scope where it has entry for TYPE. */
13947 : :
13948 : : tree
13949 : 1717 : c_omp_mapper_lookup (tree mapper_id, tree type)
13950 : : {
13951 : 1717 : if (!RECORD_OR_UNION_TYPE_P (type))
13952 : : return NULL_TREE;
13953 : :
13954 : 545 : mapper_id = c_omp_mapper_id (mapper_id);
13955 : :
13956 : 545 : struct c_binding *b = I_SYMBOL_BINDING (mapper_id);
13957 : 551 : while (b)
13958 : : {
13959 : 91 : tree t;
13960 : 110 : for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
13961 : 104 : if (comptypes (TREE_PURPOSE (t), type))
13962 : 85 : return TREE_VALUE (t);
13963 : 6 : b = b->shadowed;
13964 : : }
13965 : : return NULL_TREE;
13966 : : }
13967 : :
13968 : : /* For C, we record a pointer to the mapper itself without wrapping it in an
13969 : : artificial function or similar. So, just return it. */
13970 : :
13971 : : tree
13972 : 82 : c_omp_extract_mapper_directive (tree mapper)
13973 : : {
13974 : 82 : return mapper;
13975 : : }
13976 : :
13977 : : /* For now we can handle singleton OMP_ARRAY_SECTIONs with custom mappers, but
13978 : : nothing more complicated. */
13979 : :
13980 : : tree
13981 : 365 : c_omp_map_array_section (location_t loc, tree t)
13982 : : {
13983 : 365 : tree low = TREE_OPERAND (t, 1);
13984 : 365 : tree len = TREE_OPERAND (t, 2);
13985 : :
13986 : 365 : if (len && integer_onep (len))
13987 : : {
13988 : 63 : t = TREE_OPERAND (t, 0);
13989 : :
13990 : 63 : if (!low)
13991 : 0 : low = integer_zero_node;
13992 : :
13993 : 63 : t = build_array_ref (loc, t, low);
13994 : : }
13995 : :
13996 : 365 : return t;
13997 : : }
13998 : :
13999 : : /* Helper function for below function. */
14000 : :
14001 : : static tree
14002 : 50834 : c_omp_scan_mapper_bindings_r (tree *tp, int *walk_subtrees, void *ptr)
14003 : : {
14004 : 50834 : tree t = *tp;
14005 : 50834 : omp_mapper_list<tree> *mlist = (omp_mapper_list<tree> *) ptr;
14006 : 50834 : tree aggr_type = NULL_TREE;
14007 : :
14008 : 50834 : if (TREE_CODE (t) == SIZEOF_EXPR
14009 : 50834 : || TREE_CODE (t) == ALIGNOF_EXPR)
14010 : : {
14011 : 0 : *walk_subtrees = 0;
14012 : 0 : return NULL_TREE;
14013 : : }
14014 : :
14015 : 50834 : if (TREE_CODE (t) == OMP_CLAUSE)
14016 : : return NULL_TREE;
14017 : :
14018 : 47549 : if (TREE_CODE (t) == COMPONENT_REF
14019 : 47549 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
14020 : 356 : aggr_type = TREE_TYPE (TREE_OPERAND (t, 0));
14021 : 47193 : else if ((TREE_CODE (t) == VAR_DECL
14022 : : || TREE_CODE (t) == PARM_DECL
14023 : : || TREE_CODE (t) == RESULT_DECL)
14024 : 5265 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t)))
14025 : 116 : aggr_type = TREE_TYPE (t);
14026 : :
14027 : 472 : if (aggr_type)
14028 : : {
14029 : 472 : tree mapper_fn = c_omp_mapper_lookup (NULL_TREE, aggr_type);
14030 : 472 : if (mapper_fn)
14031 : 65 : mlist->add_mapper (NULL_TREE, aggr_type, mapper_fn);
14032 : : }
14033 : :
14034 : : return NULL_TREE;
14035 : : }
14036 : :
14037 : : /* Scan an offload region's body, and record uses of struct- or union-typed
14038 : : variables. Add _mapper_binding_ fake clauses to *CLAUSES_PTR. */
14039 : :
14040 : : void
14041 : 1910 : c_omp_scan_mapper_bindings (location_t loc, tree *clauses_ptr, tree body)
14042 : : {
14043 : 1910 : hash_set<omp_name_type<tree>> seen_types;
14044 : 1910 : auto_vec<tree> mappers;
14045 : 1910 : omp_mapper_list<tree> mlist (&seen_types, &mappers);
14046 : :
14047 : 1910 : walk_tree_without_duplicates (&body, c_omp_scan_mapper_bindings_r, &mlist);
14048 : :
14049 : 1910 : unsigned int i;
14050 : 1910 : tree mapper;
14051 : 3859 : FOR_EACH_VEC_ELT (mappers, i, mapper)
14052 : 39 : c_omp_find_nested_mappers (&mlist, mapper);
14053 : :
14054 : 1975 : FOR_EACH_VEC_ELT (mappers, i, mapper)
14055 : : {
14056 : 39 : if (mapper == error_mark_node)
14057 : 0 : continue;
14058 : 39 : tree mapper_name = OMP_DECLARE_MAPPER_ID (mapper);
14059 : 39 : tree decl = OMP_DECLARE_MAPPER_DECL (mapper);
14060 : :
14061 : 39 : tree c = build_omp_clause (loc, OMP_CLAUSE__MAPPER_BINDING_);
14062 : 39 : OMP_CLAUSE__MAPPER_BINDING__ID (c) = mapper_name;
14063 : 39 : OMP_CLAUSE__MAPPER_BINDING__DECL (c) = decl;
14064 : 39 : OMP_CLAUSE__MAPPER_BINDING__MAPPER (c) = mapper;
14065 : :
14066 : 39 : OMP_CLAUSE_CHAIN (c) = *clauses_ptr;
14067 : 39 : *clauses_ptr = c;
14068 : : }
14069 : 1910 : }
14070 : :
14071 : : bool
14072 : 223 : c_check_in_current_scope (tree decl)
14073 : : {
14074 : 223 : struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
14075 : 223 : return b != NULL && B_IN_CURRENT_SCOPE (b);
14076 : : }
14077 : :
14078 : : /* Search for loop or switch names. BEFORE_LABELS is last statement before
14079 : : possible labels and SWITCH_P true for a switch, false for loops.
14080 : : Searches through last statements in cur_stmt_list, stops when seeing
14081 : : BEFORE_LABELs, or statement other than LABEL_EXPR or CASE_LABEL_EXPR.
14082 : : Returns number of loop/switch names found and if any are found, sets
14083 : : *LAST_P to the canonical loop/switch name LABEL_DECL. */
14084 : :
14085 : : int
14086 : 509719 : c_get_loop_names (tree before_labels, bool switch_p, tree *last_p)
14087 : : {
14088 : 509719 : *last_p = NULL_TREE;
14089 : 1019438 : if (!building_stmt_list_p ()
14090 : 509719 : || !STATEMENT_LIST_HAS_LABEL (cur_stmt_list)
14091 : 530263 : || before_labels == void_list_node)
14092 : 489175 : return 0;
14093 : :
14094 : 20544 : int ret = 0;
14095 : 20544 : tree last = NULL_TREE;
14096 : 20544 : for (tree_stmt_iterator tsi = tsi_last (cur_stmt_list);
14097 : 28653 : !tsi_end_p (tsi); tsi_prev (&tsi))
14098 : : {
14099 : 27806 : tree stmt = tsi_stmt (tsi);
14100 : 27806 : if (stmt == before_labels)
14101 : : break;
14102 : 8109 : else if (TREE_CODE (stmt) == LABEL_EXPR)
14103 : : {
14104 : 1081 : if (last == NULL_TREE)
14105 : 887 : last = LABEL_EXPR_LABEL (stmt);
14106 : : else
14107 : : {
14108 : 194 : loop_names.safe_push (LABEL_EXPR_LABEL (stmt));
14109 : 194 : ++ret;
14110 : : }
14111 : : }
14112 : 7028 : else if (TREE_CODE (stmt) != CASE_LABEL_EXPR
14113 : 4729 : && TREE_CODE (stmt) != DEBUG_BEGIN_STMT)
14114 : : break;
14115 : : }
14116 : 20544 : if (last)
14117 : : {
14118 : 887 : if (switch_p)
14119 : 123 : C_DECL_SWITCH_NAME (last) = 1;
14120 : : else
14121 : 764 : C_DECL_LOOP_NAME (last) = 1;
14122 : 887 : loop_names.safe_push (last);
14123 : 887 : ++ret;
14124 : 887 : if (loop_names.length () > 16)
14125 : : {
14126 : 20 : unsigned int first = 0, i;
14127 : 20 : tree l, c = NULL_TREE;
14128 : 20 : if (loop_names_hash == NULL)
14129 : 8 : loop_names_hash = new decl_tree_map (ret);
14130 : : else
14131 : 12 : first = loop_names.length () - ret;
14132 : 210 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14133 : : {
14134 : 170 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14135 : 48 : c = l;
14136 : 170 : gcc_checking_assert (c);
14137 : 170 : loop_names_hash->put (l, c);
14138 : 170 : if (i == first)
14139 : : break;
14140 : : }
14141 : : }
14142 : 887 : *last_p = last;
14143 : : }
14144 : : return ret;
14145 : : }
14146 : :
14147 : : /* Undoes what get_loop_names did when it returned NUM_NAMES. */
14148 : :
14149 : : void
14150 : 887 : c_release_loop_names (int num_names)
14151 : : {
14152 : 887 : unsigned len = loop_names.length () - num_names;
14153 : 887 : if (loop_names_hash)
14154 : : {
14155 : 20 : if (len <= 16)
14156 : : {
14157 : 8 : delete loop_names_hash;
14158 : 8 : loop_names_hash = NULL;
14159 : : }
14160 : : else
14161 : : {
14162 : 12 : unsigned int i;
14163 : 12 : tree l;
14164 : 42 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14165 : : {
14166 : 30 : loop_names_hash->remove (l);
14167 : 30 : if (i == len)
14168 : : break;
14169 : : }
14170 : : }
14171 : : }
14172 : 887 : loop_names.truncate (len);
14173 : 887 : }
14174 : :
14175 : : /* Finish processing of break or continue identifier operand.
14176 : : NAME is the identifier operand of break or continue and
14177 : : IS_BREAK is true iff it is break stmt. Returns the operand
14178 : : to use for BREAK_STMT or CONTINUE_STMT, either NULL_TREE or
14179 : : canonical loop/switch name LABEL_DECL. */
14180 : :
14181 : : tree
14182 : 316 : c_finish_bc_name (location_t loc, tree name, bool is_break)
14183 : : {
14184 : 316 : tree label = NULL_TREE, lab;
14185 : 457 : pedwarn_c23 (loc, OPT_Wpedantic,
14186 : : "ISO C does not support %qs statement with an identifier "
14187 : : "operand before C2Y", is_break ? "break" : "continue");
14188 : :
14189 : : /* If I_LABEL_DECL is NULL or not from current function, don't waste time
14190 : : trying to find it among loop_names, it can't be there. */
14191 : 316 : if (!loop_names.is_empty ()
14192 : 296 : && current_function_scope
14193 : 296 : && (lab = I_LABEL_DECL (name))
14194 : 290 : && DECL_CONTEXT (lab) == current_function_decl)
14195 : : {
14196 : 280 : unsigned int i;
14197 : 280 : tree l, c = NULL_TREE;
14198 : 280 : if (loop_names_hash)
14199 : : {
14200 : 86 : if (tree *val = loop_names_hash->get (lab))
14201 : 86 : label = *val;
14202 : : }
14203 : : else
14204 : 565 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14205 : : {
14206 : 363 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14207 : : c = l;
14208 : 363 : gcc_checking_assert (c);
14209 : 363 : if (l == lab)
14210 : : {
14211 : : label = c;
14212 : : break;
14213 : : }
14214 : : }
14215 : 280 : if (label)
14216 : 272 : TREE_USED (lab) = 1;
14217 : : }
14218 : 272 : if (label == NULL_TREE)
14219 : : {
14220 : 44 : auto_vec<const char *> candidates;
14221 : 44 : unsigned int i;
14222 : 44 : tree l, c = NULL_TREE;
14223 : 115 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14224 : : {
14225 : 28 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14226 : : c = l;
14227 : 28 : gcc_checking_assert (c);
14228 : 42 : if (is_break || C_DECL_LOOP_NAME (c))
14229 : 25 : candidates.safe_push (IDENTIFIER_POINTER (DECL_NAME (l)));
14230 : : }
14231 : 44 : const char *hint = find_closest_string (IDENTIFIER_POINTER (name),
14232 : : &candidates);
14233 : 44 : if (hint)
14234 : : {
14235 : 22 : gcc_rich_location richloc (loc);
14236 : 22 : richloc.add_fixit_replace (hint);
14237 : 22 : if (is_break)
14238 : 11 : error_at (&richloc, "%<break%> statement operand %qE does not "
14239 : : "refer to a named loop or %<switch%>; "
14240 : : "did you mean %qs?", name, hint);
14241 : : else
14242 : 11 : error_at (&richloc, "%<continue%> statement operand %qE does not "
14243 : : "refer to a named loop; did you mean %qs?",
14244 : : name, hint);
14245 : 22 : }
14246 : 22 : else if (is_break)
14247 : 12 : error_at (loc, "%<break%> statement operand %qE does not refer to a "
14248 : : "named loop or %<switch%>", name);
14249 : : else
14250 : 10 : error_at (loc, "%<continue%> statement operand %qE does not refer to "
14251 : : "a named loop", name);
14252 : 44 : }
14253 : 272 : else if (!C_DECL_LOOP_NAME (label) && !is_break)
14254 : : {
14255 : 2 : auto_diagnostic_group d;
14256 : 2 : error_at (loc, "%<continue%> statement operand %qE refers to a named "
14257 : : "%<switch%>", name);
14258 : 2 : inform (DECL_SOURCE_LOCATION (label), "%<switch%> name defined here");
14259 : 2 : label = NULL_TREE;
14260 : 2 : }
14261 : 270 : else if (!C_DECL_LOOP_SWITCH_NAME_VALID (label))
14262 : : {
14263 : 18 : auto_diagnostic_group d;
14264 : 18 : if (C_DECL_LOOP_NAME (label))
14265 : : {
14266 : 16 : error_at (loc, "%qs statement operand %qE refers to a loop outside "
14267 : : "of its body", is_break ? "break" : "continue", name);
14268 : 16 : inform (DECL_SOURCE_LOCATION (label), "loop name defined here");
14269 : : }
14270 : : else
14271 : : {
14272 : 2 : error_at (loc, "%<break%> statement operand %qE refers to a "
14273 : : "%<switch%> outside of its body", name);
14274 : 2 : inform (DECL_SOURCE_LOCATION (label),
14275 : : "%<switch%> name defined here");
14276 : : }
14277 : 18 : label = NULL_TREE;
14278 : 18 : }
14279 : 252 : else if (label == loop_names.last () && (in_statement & IN_NAMED_STMT) != 0)
14280 : : /* If it is just a fancy reference to the innermost construct, handle it
14281 : : just like break; or continue; though tracking cheaply what is the
14282 : : innermost loop for continue when nested in switches would require
14283 : : another global variable and updating it. */
14284 : : label = NULL_TREE;
14285 : : else
14286 : 98 : C_DECL_LOOP_SWITCH_NAME_USED (label) = 1;
14287 : 316 : return label;
14288 : : }
14289 : :
14290 : : #include "gt-c-c-decl.h"
|