Branch data Line data Source code
1 : : /* Process declarations and variables for C compiler.
2 : : Copyright (C) 1988-2025 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify it under
7 : : the terms of the GNU General Public License as published by the Free
8 : : Software Foundation; either version 3, or (at your option) any later
9 : : version.
10 : :
11 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : : for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along with GCC; see the file COPYING3. If not see
18 : : <http://www.gnu.org/licenses/>. */
19 : :
20 : : /* Process declarations and symbol lookup for C front end.
21 : : Also constructs types; the standard scalar types at initialization,
22 : : and structure, union, array and enum types when they are declared. */
23 : :
24 : : /* ??? not all decl nodes are given the most useful possible
25 : : line numbers. For example, the CONST_DECLs for enum values. */
26 : :
27 : : #include "config.h"
28 : : #define INCLUDE_STRING
29 : : #include "system.h"
30 : : #include "coretypes.h"
31 : : #include "target.h"
32 : : #include "function.h"
33 : : #include "c-tree.h"
34 : : #include "timevar.h"
35 : : #include "stringpool.h"
36 : : #include "cgraph.h"
37 : : #include "intl.h"
38 : : #include "print-tree.h"
39 : : #include "stor-layout.h"
40 : : #include "varasm.h"
41 : : #include "attribs.h"
42 : : #include "toplev.h"
43 : : #include "debug.h"
44 : : #include "c-family/c-objc.h"
45 : : #include "c-family/c-pragma.h"
46 : : #include "c-family/c-ubsan.h"
47 : : #include "c-lang.h"
48 : : #include "langhooks.h"
49 : : #include "tree-iterator.h"
50 : : #include "dumpfile.h"
51 : : #include "plugin.h"
52 : : #include "c-family/c-ada-spec.h"
53 : : #include "builtins.h"
54 : : #include "spellcheck-tree.h"
55 : : #include "gcc-rich-location.h"
56 : : #include "asan.h"
57 : : #include "c-family/name-hint.h"
58 : : #include "c-family/known-headers.h"
59 : : #include "c-family/c-spellcheck.h"
60 : : #include "context.h" /* For 'g'. */
61 : : #include "omp-general.h"
62 : : #include "omp-offload.h" /* For offload_vars. */
63 : : #include "c-parser.h"
64 : : #include "gcc-urlifier.h"
65 : :
66 : : #include "tree-pretty-print.h"
67 : :
68 : : /* In grokdeclarator, distinguish syntactic contexts of declarators. */
69 : : enum decl_context
70 : : { NORMAL, /* Ordinary declaration */
71 : : FUNCDEF, /* Function definition */
72 : : PARM, /* Declaration of parm before function body */
73 : : FIELD, /* Declaration inside struct or union */
74 : : TYPENAME}; /* Typename (inside cast or sizeof) */
75 : :
76 : : /* States indicating how grokdeclarator() should handle declspecs marked
77 : : with __attribute__((deprecated)) or __attribute__((unavailable)).
78 : : An object declared as __attribute__((unavailable)) should suppress
79 : : any reports of being declared with unavailable or deprecated items.
80 : : An object declared as __attribute__((deprecated)) should suppress
81 : : warnings of uses of other deprecated items. */
82 : :
83 : : enum deprecated_states {
84 : : DEPRECATED_NORMAL,
85 : : DEPRECATED_SUPPRESS,
86 : : UNAVAILABLE_DEPRECATED_SUPPRESS
87 : : };
88 : :
89 : :
90 : : /* Nonzero if we have seen an invalid cross reference
91 : : to a struct, union, or enum, but not yet printed the message. */
92 : : tree pending_invalid_xref;
93 : :
94 : : /* File and line to appear in the eventual error message. */
95 : : location_t pending_invalid_xref_location;
96 : :
97 : : /* The file and line that the prototype came from if this is an
98 : : old-style definition; used for diagnostics in
99 : : store_parm_decls_oldstyle. */
100 : :
101 : : static location_t current_function_prototype_locus;
102 : :
103 : : /* Whether this prototype was built-in. */
104 : :
105 : : static bool current_function_prototype_built_in;
106 : :
107 : : /* The argument type information of this prototype. */
108 : :
109 : : static tree current_function_prototype_arg_types;
110 : :
111 : : /* The argument information structure for the function currently being
112 : : defined. */
113 : :
114 : : static struct c_arg_info *current_function_arg_info;
115 : :
116 : : /* The obstack on which parser and related data structures, which are
117 : : not live beyond their top-level declaration or definition, are
118 : : allocated. */
119 : : struct obstack parser_obstack;
120 : :
121 : : /* The current statement tree. */
122 : :
123 : : static GTY(()) struct stmt_tree_s c_stmt_tree;
124 : :
125 : : /* Zero if we are not in an iteration or switch statement, otherwise
126 : : a bitmask. See bitmask definitions in c-tree.h. */
127 : : unsigned char in_statement;
128 : :
129 : : /* A list of decls to be made automatically visible in each file scope. */
130 : : static GTY(()) tree visible_builtins;
131 : :
132 : : /* Set to 0 at beginning of a function definition, set to 1 if
133 : : a return statement that specifies a return value is seen. */
134 : :
135 : : int current_function_returns_value;
136 : :
137 : : /* Set to 0 at beginning of a function definition, set to 1 if
138 : : a return statement with no argument is seen. */
139 : :
140 : : int current_function_returns_null;
141 : :
142 : : /* Set to 0 at beginning of a function definition, set to 1 if
143 : : a call to a noreturn function is seen. */
144 : :
145 : : int current_function_returns_abnormally;
146 : :
147 : : /* Set to nonzero by `grokdeclarator' for a function
148 : : whose return type is defaulted, if warnings for this are desired. */
149 : :
150 : : static int warn_about_return_type;
151 : :
152 : : /* Nonzero when the current toplevel function contains a declaration
153 : : of a nested function which is never defined. */
154 : :
155 : : static bool undef_nested_function;
156 : :
157 : : /* Vector of implicit "omp declare target" attributes to be added into
158 : : the attribute lists. */
159 : : vec<c_omp_declare_target_attr, va_gc> *current_omp_declare_target_attribute;
160 : :
161 : : /* Vector of
162 : : #pragma omp begin assumes ... #pragma omp end assumes regions
163 : : we are in. */
164 : : vec<c_omp_begin_assumes_data, va_gc> *current_omp_begin_assumes;
165 : :
166 : : /* Vector of loop names with C_DECL_LOOP_NAME or C_DECL_SWITCH_NAME marked
167 : : LABEL_DECL as the last and canonical for each loop or switch. */
168 : : static vec<tree> loop_names;
169 : :
170 : : /* Hash table mapping LABEL_DECLs to the canonical LABEL_DECLs if LOOP_NAMES
171 : : vector becomes too long. */
172 : : static decl_tree_map *loop_names_hash;
173 : :
174 : : /* Each c_binding structure describes one binding of an identifier to
175 : : a decl. All the decls in a scope - irrespective of namespace - are
176 : : chained together by the ->prev field, which (as the name implies)
177 : : runs in reverse order. All the decls in a given namespace bound to
178 : : a given identifier are chained by the ->shadowed field, which runs
179 : : from inner to outer scopes.
180 : :
181 : : The ->decl field usually points to a DECL node, but there are two
182 : : exceptions. In the namespace of type tags, the bound entity is a
183 : : RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
184 : : identifier is encountered, it is bound to error_mark_node to
185 : : suppress further errors about that identifier in the current
186 : : function.
187 : :
188 : : The ->u.type field stores the type of the declaration in this scope;
189 : : if NULL, the type is the type of the ->decl field. This is only of
190 : : relevance for objects with external or internal linkage which may
191 : : be redeclared in inner scopes, forming composite types that only
192 : : persist for the duration of those scopes. In the external scope,
193 : : this stores the composite of all the types declared for this
194 : : object, visible or not. The ->inner_comp field (used only at file
195 : : scope) stores whether an incomplete array type at file scope was
196 : : completed at an inner scope to an array size other than 1.
197 : :
198 : : The ->u.label field is used for labels. It points to a structure
199 : : which stores additional information used for warnings.
200 : :
201 : : The depth field is copied from the scope structure that holds this
202 : : decl. It is used to preserve the proper ordering of the ->shadowed
203 : : field (see bind()) and also for a handful of special-case checks.
204 : : Finally, the invisible bit is true for a decl which should be
205 : : ignored for purposes of normal name lookup, and the nested bit is
206 : : true for a decl that's been bound a second time in an inner scope;
207 : : in all such cases, the binding in the outer scope will have its
208 : : invisible bit true. */
209 : :
210 : : struct GTY((chain_next ("%h.prev"))) c_binding {
211 : : union GTY(()) { /* first so GTY desc can use decl */
212 : : tree GTY((tag ("0"))) type; /* the type in this scope */
213 : : struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
214 : : } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
215 : : tree decl; /* the decl bound */
216 : : tree id; /* the identifier it's bound to */
217 : : struct c_binding *prev; /* the previous decl in this scope */
218 : : struct c_binding *shadowed; /* the innermost decl shadowed by this one */
219 : : unsigned int depth : 28; /* depth of this scope */
220 : : BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
221 : : BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
222 : : BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
223 : : BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
224 : : location_t locus; /* location for nested bindings */
225 : : };
226 : : #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
227 : : #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
228 : : #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
229 : : #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
230 : :
231 : : /* Each C symbol points to three linked lists of c_binding structures.
232 : : These describe the values of the identifier in the three different
233 : : namespaces defined by the language. */
234 : :
235 : : struct GTY(()) lang_identifier {
236 : : struct c_common_identifier common_id;
237 : : struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
238 : : struct c_binding *tag_binding; /* struct/union/enum tags */
239 : : struct c_binding *label_binding; /* labels */
240 : : };
241 : :
242 : : /* Validate c-lang.cc's assumptions. */
243 : : extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
244 : : [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
245 : :
246 : : /* The binding oracle; see c-tree.h. */
247 : : void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
248 : :
249 : : /* This flag is set on an identifier if we have previously asked the
250 : : binding oracle for this identifier's symbol binding. */
251 : : #define I_SYMBOL_CHECKED(node) \
252 : : (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
253 : :
254 : : static inline struct c_binding* *
255 : 4476168917 : i_symbol_binding (tree node)
256 : : {
257 : 4476168917 : struct lang_identifier *lid
258 : 4476168917 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
259 : :
260 : 4476168917 : if (lid->symbol_binding == NULL
261 : 1642849529 : && c_binding_oracle != NULL
262 : 4476168917 : && !I_SYMBOL_CHECKED (node))
263 : : {
264 : : /* Set the "checked" flag first, to avoid infinite recursion
265 : : when the binding oracle calls back into gcc. */
266 : 0 : I_SYMBOL_CHECKED (node) = 1;
267 : 0 : c_binding_oracle (C_ORACLE_SYMBOL, node);
268 : : }
269 : :
270 : 4476168917 : return &lid->symbol_binding;
271 : : }
272 : :
273 : : #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
274 : :
275 : : #define I_SYMBOL_DECL(node) \
276 : : (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
277 : :
278 : : /* This flag is set on an identifier if we have previously asked the
279 : : binding oracle for this identifier's tag binding. */
280 : : #define I_TAG_CHECKED(node) \
281 : : (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
282 : :
283 : : static inline struct c_binding **
284 : 4424116 : i_tag_binding (tree node)
285 : : {
286 : 4424116 : struct lang_identifier *lid
287 : 4424116 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
288 : :
289 : 4424116 : if (lid->tag_binding == NULL
290 : 1224836 : && c_binding_oracle != NULL
291 : 4424116 : && !I_TAG_CHECKED (node))
292 : : {
293 : : /* Set the "checked" flag first, to avoid infinite recursion
294 : : when the binding oracle calls back into gcc. */
295 : 0 : I_TAG_CHECKED (node) = 1;
296 : 0 : c_binding_oracle (C_ORACLE_TAG, node);
297 : : }
298 : :
299 : 4424116 : return &lid->tag_binding;
300 : : }
301 : :
302 : : #define I_TAG_BINDING(node) (*i_tag_binding (node))
303 : :
304 : : #define I_TAG_DECL(node) \
305 : : (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
306 : :
307 : : /* This flag is set on an identifier if we have previously asked the
308 : : binding oracle for this identifier's label binding. */
309 : : #define I_LABEL_CHECKED(node) \
310 : : (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
311 : :
312 : : static inline struct c_binding **
313 : 354225 : i_label_binding (tree node)
314 : : {
315 : 354225 : struct lang_identifier *lid
316 : 354225 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
317 : :
318 : 354225 : if (lid->label_binding == NULL
319 : 46496 : && c_binding_oracle != NULL
320 : 354225 : && !I_LABEL_CHECKED (node))
321 : : {
322 : : /* Set the "checked" flag first, to avoid infinite recursion
323 : : when the binding oracle calls back into gcc. */
324 : 0 : I_LABEL_CHECKED (node) = 1;
325 : 0 : c_binding_oracle (C_ORACLE_LABEL, node);
326 : : }
327 : :
328 : 354225 : return &lid->label_binding;
329 : : }
330 : :
331 : : #define I_LABEL_BINDING(node) (*i_label_binding (node))
332 : :
333 : : #define I_LABEL_DECL(node) \
334 : : (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
335 : :
336 : : /* Used by C_TOKEN_VEC tree. */
337 : : struct GTY (()) c_tree_token_vec {
338 : : struct tree_base base;
339 : : vec<c_token, va_gc> *tokens;
340 : : };
341 : :
342 : : STATIC_ASSERT (sizeof (c_tree_token_vec) == sizeof (c_tree_token_vec_struct));
343 : : STATIC_ASSERT (offsetof (c_tree_token_vec, tokens)
344 : : == offsetof (c_tree_token_vec_struct, tokens));
345 : :
346 : : /* The resulting tree type. */
347 : :
348 : : union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE + 2 * (TREE_CODE (&%h.generic) == C_TOKEN_VEC)"),
349 : : chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
350 : : {
351 : : union tree_node GTY ((tag ("0"),
352 : : desc ("tree_node_structure (&%h)")))
353 : : generic;
354 : : struct lang_identifier GTY ((tag ("1"))) identifier;
355 : : struct c_tree_token_vec GTY ((tag ("2"))) c_token_vec;
356 : : };
357 : :
358 : : /* Langhook for tree_size. */
359 : : size_t
360 : 901 : c_tree_size (enum tree_code code)
361 : : {
362 : 901 : gcc_checking_assert (code >= NUM_TREE_CODES);
363 : 901 : switch (code)
364 : : {
365 : : case C_TOKEN_VEC: return sizeof (c_tree_token_vec);
366 : 0 : default:
367 : 0 : switch (TREE_CODE_CLASS (code))
368 : : {
369 : : case tcc_declaration: return sizeof (tree_decl_non_common);
370 : 0 : case tcc_type: return sizeof (tree_type_non_common);
371 : 0 : default: gcc_unreachable ();
372 : : }
373 : : }
374 : : }
375 : :
376 : : /* Track bindings and other things that matter for goto warnings. For
377 : : efficiency, we do not gather all the decls at the point of
378 : : definition. Instead, we point into the bindings structure. As
379 : : scopes are popped, we update these structures and gather the decls
380 : : that matter at that time. */
381 : :
382 : : struct GTY(()) c_spot_bindings {
383 : : /* The currently open scope which holds bindings defined when the
384 : : label was defined or the goto statement was found. */
385 : : struct c_scope *scope;
386 : : /* The bindings in the scope field which were defined at the point
387 : : of the label or goto. This lets us look at older or newer
388 : : bindings in the scope, as appropriate. */
389 : : struct c_binding *bindings_in_scope;
390 : : /* The number of statement expressions that have started since this
391 : : label or goto statement was defined. This is zero if we are at
392 : : the same statement expression level. It is positive if we are in
393 : : a statement expression started since this spot. It is negative
394 : : if this spot was in a statement expression and we have left
395 : : it. */
396 : : int stmt_exprs;
397 : : /* Whether we started in a statement expression but are no longer in
398 : : it. This is set to true if stmt_exprs ever goes negative. */
399 : : bool left_stmt_expr;
400 : : };
401 : :
402 : : /* This structure is used to keep track of bindings seen when a goto
403 : : statement is defined. This is only used if we see the goto
404 : : statement before we see the label. */
405 : :
406 : : struct GTY(()) c_goto_bindings {
407 : : /* The location of the goto statement. */
408 : : location_t loc;
409 : : /* The bindings of the goto statement. */
410 : : struct c_spot_bindings goto_bindings;
411 : : };
412 : :
413 : : typedef struct c_goto_bindings *c_goto_bindings_p;
414 : :
415 : : /* The additional information we keep track of for a label binding.
416 : : These fields are updated as scopes are popped. */
417 : :
418 : : struct GTY(()) c_label_vars {
419 : : /* The shadowed c_label_vars, when one label shadows another (which
420 : : can only happen using a __label__ declaration). */
421 : : struct c_label_vars *shadowed;
422 : : /* The bindings when the label was defined. */
423 : : struct c_spot_bindings label_bindings;
424 : : /* A list of decls that we care about: decls about which we should
425 : : warn if a goto branches to this label from later in the function.
426 : : Decls are added to this list as scopes are popped. We only add
427 : : the decls that matter. */
428 : : vec<tree, va_gc> *decls_in_scope;
429 : : /* A list of goto statements to this label. This is only used for
430 : : goto statements seen before the label was defined, so that we can
431 : : issue appropriate warnings for them. */
432 : : vec<c_goto_bindings_p, va_gc> *gotos;
433 : : };
434 : :
435 : : /* Each c_scope structure describes the complete contents of one
436 : : scope. Four scopes are distinguished specially: the innermost or
437 : : current scope, the innermost function scope, the file scope (always
438 : : the second to outermost) and the outermost or external scope.
439 : :
440 : : Most declarations are recorded in the current scope.
441 : :
442 : : All normal label declarations are recorded in the innermost
443 : : function scope, as are bindings of undeclared identifiers to
444 : : error_mark_node. (GCC permits nested functions as an extension,
445 : : hence the 'innermost' qualifier.) Explicitly declared labels
446 : : (using the __label__ extension) appear in the current scope.
447 : :
448 : : Being in the file scope (current_scope == file_scope) causes
449 : : special behavior in several places below. Also, under some
450 : : conditions the Objective-C front end records declarations in the
451 : : file scope even though that isn't the current scope.
452 : :
453 : : All declarations with external linkage are recorded in the external
454 : : scope, even if they aren't visible there; this models the fact that
455 : : such declarations are visible to the entire program, and (with a
456 : : bit of cleverness, see pushdecl) allows diagnosis of some violations
457 : : of C99 6.2.2p7 and 6.2.7p2:
458 : :
459 : : If, within the same translation unit, the same identifier appears
460 : : with both internal and external linkage, the behavior is
461 : : undefined.
462 : :
463 : : All declarations that refer to the same object or function shall
464 : : have compatible type; otherwise, the behavior is undefined.
465 : :
466 : : Initially only the built-in declarations, which describe compiler
467 : : intrinsic functions plus a subset of the standard library, are in
468 : : this scope.
469 : :
470 : : The order of the blocks list matters, and it is frequently appended
471 : : to. To avoid having to walk all the way to the end of the list on
472 : : each insertion, or reverse the list later, we maintain a pointer to
473 : : the last list entry. (FIXME: It should be feasible to use a reversed
474 : : list here.)
475 : :
476 : : The bindings list is strictly in reverse order of declarations;
477 : : pop_scope relies on this. */
478 : :
479 : :
480 : : struct GTY((chain_next ("%h.outer"))) c_scope {
481 : : /* The scope containing this one. */
482 : : struct c_scope *outer;
483 : :
484 : : /* The next outermost function scope. */
485 : : struct c_scope *outer_function;
486 : :
487 : : /* All bindings in this scope. */
488 : : struct c_binding *bindings;
489 : :
490 : : /* For each scope (except the global one), a chain of BLOCK nodes
491 : : for all the scopes that were entered and exited one level down. */
492 : : tree blocks;
493 : : tree blocks_last;
494 : :
495 : : /* The depth of this scope. Used to keep the ->shadowed chain of
496 : : bindings sorted innermost to outermost. */
497 : : unsigned int depth : 28;
498 : :
499 : : /* True if we are currently filling this scope with parameter
500 : : declarations. */
501 : : BOOL_BITFIELD parm_flag : 1;
502 : :
503 : : /* True if we saw [*] in this scope. Used to give an error messages
504 : : if these appears in a function definition. */
505 : : BOOL_BITFIELD had_vla_unspec : 1;
506 : :
507 : : /* True if we already complained about forward parameter decls
508 : : in this scope. This prevents double warnings on
509 : : foo (int a; int b; ...) */
510 : : BOOL_BITFIELD warned_forward_parm_decls : 1;
511 : :
512 : : /* True if this is the outermost block scope of a function body.
513 : : This scope contains the parameters, the local variables declared
514 : : in the outermost block, and all the labels (except those in
515 : : nested functions, or declared at block scope with __label__). */
516 : : BOOL_BITFIELD function_body : 1;
517 : :
518 : : /* True means make a BLOCK for this scope no matter what. */
519 : : BOOL_BITFIELD keep : 1;
520 : :
521 : : /* True means that an unsuffixed float constant is _Decimal64. */
522 : : BOOL_BITFIELD float_const_decimal64 : 1;
523 : :
524 : : /* True if this scope has any label bindings. This is used to speed
525 : : up searching for labels when popping scopes, particularly since
526 : : labels are normally only found at function scope. */
527 : : BOOL_BITFIELD has_label_bindings : 1;
528 : :
529 : : /* True if we should issue a warning if a goto statement crosses any
530 : : of the bindings. We still need to check the list of bindings to
531 : : find the specific ones we need to warn about. This is true if
532 : : decl_jump_unsafe would return true for any of the bindings. This
533 : : is used to avoid looping over all the bindings unnecessarily. */
534 : : BOOL_BITFIELD has_jump_unsafe_decl : 1;
535 : : };
536 : :
537 : : /* The scope currently in effect. */
538 : :
539 : : static GTY(()) struct c_scope *current_scope;
540 : :
541 : : /* The innermost function scope. Ordinary (not explicitly declared)
542 : : labels, bindings to error_mark_node, and the lazily-created
543 : : bindings of __func__ and its friends get this scope. */
544 : :
545 : : static GTY(()) struct c_scope *current_function_scope;
546 : :
547 : : /* The C file scope. This is reset for each input translation unit. */
548 : :
549 : : static GTY(()) struct c_scope *file_scope;
550 : :
551 : : /* The outermost scope. This is used for all declarations with
552 : : external linkage, and only these, hence the name. */
553 : :
554 : : static GTY(()) struct c_scope *external_scope;
555 : :
556 : : /* A chain of c_scope structures awaiting reuse. */
557 : :
558 : : static GTY((deletable)) struct c_scope *scope_freelist;
559 : :
560 : : /* A chain of c_binding structures awaiting reuse. */
561 : :
562 : : static GTY((deletable)) struct c_binding *binding_freelist;
563 : :
564 : : /* Append VAR to LIST in scope SCOPE. */
565 : : #define SCOPE_LIST_APPEND(scope, list, decl) do { \
566 : : struct c_scope *s_ = (scope); \
567 : : tree d_ = (decl); \
568 : : if (s_->list##_last) \
569 : : BLOCK_CHAIN (s_->list##_last) = d_; \
570 : : else \
571 : : s_->list = d_; \
572 : : s_->list##_last = d_; \
573 : : } while (0)
574 : :
575 : : /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
576 : : #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
577 : : struct c_scope *t_ = (tscope); \
578 : : struct c_scope *f_ = (fscope); \
579 : : if (t_->to##_last) \
580 : : BLOCK_CHAIN (t_->to##_last) = f_->from; \
581 : : else \
582 : : t_->to = f_->from; \
583 : : t_->to##_last = f_->from##_last; \
584 : : } while (0)
585 : :
586 : : /* A c_inline_static structure stores details of a static identifier
587 : : referenced in a definition of a function that may be an inline
588 : : definition if no subsequent declaration of that function uses
589 : : "extern" or does not use "inline". */
590 : :
591 : : struct GTY((chain_next ("%h.next"))) c_inline_static {
592 : : /* The location for a diagnostic. */
593 : : location_t location;
594 : :
595 : : /* The function that may be an inline definition. */
596 : : tree function;
597 : :
598 : : /* The object or function referenced. */
599 : : tree static_decl;
600 : :
601 : : /* What sort of reference this is. */
602 : : enum c_inline_static_type type;
603 : :
604 : : /* The next such structure or NULL. */
605 : : struct c_inline_static *next;
606 : : };
607 : :
608 : : /* List of static identifiers used or referenced in functions that may
609 : : be inline definitions. */
610 : : static GTY(()) struct c_inline_static *c_inline_statics;
611 : :
612 : : /* True means unconditionally make a BLOCK for the next scope pushed. */
613 : :
614 : : static bool keep_next_level_flag;
615 : :
616 : : /* True means the next call to push_scope will be the outermost scope
617 : : of a function body, so do not push a new scope, merely cease
618 : : expecting parameter decls. */
619 : :
620 : : static bool next_is_function_body;
621 : :
622 : : /* A vector of pointers to c_binding structures. */
623 : :
624 : : typedef struct c_binding *c_binding_ptr;
625 : :
626 : : /* Information that we keep for a struct or union while it is being
627 : : parsed. */
628 : :
629 : 1149636 : class c_struct_parse_info
630 : : {
631 : : public:
632 : : /* If warn_cxx_compat, a list of types defined within this
633 : : struct. */
634 : : auto_vec<tree> struct_types;
635 : : /* If warn_cxx_compat, a list of field names which have bindings,
636 : : and which are defined in this struct, but which are not defined
637 : : in any enclosing struct. This is used to clear the in_struct
638 : : field of the c_bindings structure. */
639 : : auto_vec<c_binding_ptr> fields;
640 : : /* If warn_cxx_compat, a list of typedef names used when defining
641 : : fields in this struct. */
642 : : auto_vec<tree> typedefs_seen;
643 : : /* The location of a previous definition of this struct. */
644 : : location_t refloc;
645 : : };
646 : :
647 : :
648 : : /* Hash table for structs and unions. */
649 : : struct c_struct_hasher : ggc_ptr_hash<tree_node>
650 : : {
651 : : static hashval_t hash (tree t);
652 : : static bool equal (tree, tree);
653 : : };
654 : :
655 : : /* Hash an RECORD OR UNION. */
656 : : hashval_t
657 : 4192868 : c_struct_hasher::hash (tree type)
658 : : {
659 : 4192868 : inchash::hash hstate;
660 : :
661 : 4192868 : hstate.add_int (TREE_CODE (type));
662 : 4192868 : tree tag = c_type_tag (type);
663 : 4192868 : hstate.add_object (tag);
664 : :
665 : 4192868 : return hstate.end ();
666 : : }
667 : :
668 : : /* Compare two RECORD or UNION types. */
669 : : bool
670 : 14258060 : c_struct_hasher::equal (tree t1, tree t2)
671 : : {
672 : 14258060 : return comptypes_equiv_p (t1, t2);
673 : : }
674 : :
675 : : /* All tagged typed so that TYPE_CANONICAL can be set correctly. */
676 : : static GTY (()) hash_table<c_struct_hasher> *c_struct_htab;
677 : :
678 : : /* Information for the struct or union currently being parsed, or
679 : : NULL if not parsing a struct or union. */
680 : : static class c_struct_parse_info *struct_parse_info;
681 : :
682 : : /* Forward declarations. */
683 : : static tree lookup_name_in_scope (tree, struct c_scope *);
684 : : static tree c_make_fname_decl (location_t, tree, int);
685 : : static tree grokdeclarator (const struct c_declarator *,
686 : : struct c_declspecs *,
687 : : enum decl_context, bool, tree *, tree *, tree *,
688 : : bool *, enum deprecated_states);
689 : : static tree grokparms (struct c_arg_info *, bool);
690 : : static void layout_array_type (tree);
691 : : static const char *header_for_builtin_fn (tree);
692 : :
693 : : /* T is a statement. Add it to the statement-tree. This is the
694 : : C/ObjC version--C++ has a slightly different version of this
695 : : function. */
696 : :
697 : : tree
698 : 91402641 : add_stmt (tree t)
699 : : {
700 : 91402641 : enum tree_code code = TREE_CODE (t);
701 : :
702 : 91402641 : if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
703 : : {
704 : 90706336 : if (!EXPR_HAS_LOCATION (t))
705 : 154187 : SET_EXPR_LOCATION (t, input_location);
706 : : }
707 : :
708 : : /* Add T to the statement-tree. Non-side-effect statements need to be
709 : : recorded during statement expressions. */
710 : 91402641 : if (!building_stmt_list_p ())
711 : 1 : push_stmt_list ();
712 : :
713 : 91402641 : if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
714 : 1072526 : STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
715 : :
716 : 91402641 : append_to_statement_list_force (t, &cur_stmt_list);
717 : :
718 : 91402641 : return t;
719 : : }
720 : :
721 : :
722 : :
723 : :
724 : :
725 : : /* Return true if we will want to say something if a goto statement
726 : : crosses DECL. */
727 : :
728 : : static bool
729 : 885768651 : decl_jump_unsafe (tree decl)
730 : : {
731 : 885768651 : if (error_operand_p (decl))
732 : : return false;
733 : :
734 : : /* Don't warn for compound literals. If a goto statement crosses
735 : : their initialization, it should cross also all the places where
736 : : the complit is used or where the complit address might be saved
737 : : into some variable, so code after the label to which goto jumps
738 : : should not be able to refer to the compound literal. */
739 : 885766473 : if (VAR_P (decl) && C_DECL_COMPOUND_LITERAL_P (decl))
740 : : return false;
741 : :
742 : 884869278 : if (flag_openmp
743 : 11791623 : && VAR_P (decl)
744 : 884900233 : && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl)))
745 : : return true;
746 : :
747 : : /* Always warn about crossing variably modified types. */
748 : 875017362 : if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
749 : 896754571 : && c_type_variably_modified_p (TREE_TYPE (decl)))
750 : : return true;
751 : :
752 : : /* Otherwise, only warn if -Wgoto-misses-init and this is an
753 : : initialized automatic decl. */
754 : 884829030 : if (warn_jump_misses_init
755 : 5887562 : && VAR_P (decl)
756 : 9996 : && !TREE_STATIC (decl)
757 : 884834294 : && DECL_INITIAL (decl) != NULL_TREE)
758 : : return true;
759 : :
760 : : return false;
761 : : }
762 : :
763 : :
764 : : void
765 : 0 : c_print_identifier (FILE *file, tree node, int indent)
766 : : {
767 : 0 : void (*save) (enum c_oracle_request, tree identifier);
768 : :
769 : : /* Temporarily hide any binding oracle. Without this, calls to
770 : : debug_tree from the debugger will end up calling into the oracle,
771 : : making for a confusing debug session. As the oracle isn't needed
772 : : here for normal operation, it's simplest to suppress it. */
773 : 0 : save = c_binding_oracle;
774 : 0 : c_binding_oracle = NULL;
775 : :
776 : 0 : print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
777 : 0 : print_node (file, "tag", I_TAG_DECL (node), indent + 4);
778 : 0 : print_node (file, "label", I_LABEL_DECL (node), indent + 4);
779 : 0 : if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
780 : : {
781 : 0 : tree rid = ridpointers[C_RID_CODE (node)];
782 : 0 : indent_to (file, indent + 4);
783 : 0 : fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
784 : 0 : (void *) rid, IDENTIFIER_POINTER (rid));
785 : : }
786 : :
787 : 0 : c_binding_oracle = save;
788 : 0 : }
789 : :
790 : : /* Establish that the scope contains declarations that are sensitive to
791 : : jumps that cross a binding. Together with decl_jump_unsafe, this is
792 : : used to diagnose such jumps. */
793 : : void
794 : 69 : c_mark_decl_jump_unsafe_in_current_scope ()
795 : : {
796 : 69 : current_scope->has_jump_unsafe_decl = 1;
797 : 69 : }
798 : :
799 : : /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
800 : : which may be any of several kinds of DECL or TYPE or error_mark_node,
801 : : in the scope SCOPE. */
802 : : static void
803 : 885619551 : bind (tree name, tree decl, struct c_scope *scope, bool invisible,
804 : : bool nested, location_t locus)
805 : : {
806 : 885619551 : struct c_binding *b, **here;
807 : :
808 : 885619551 : if (binding_freelist)
809 : : {
810 : 223772084 : b = binding_freelist;
811 : 223772084 : binding_freelist = b->prev;
812 : : }
813 : : else
814 : 661847467 : b = ggc_alloc<c_binding> ();
815 : :
816 : 885619551 : b->shadowed = 0;
817 : 885619551 : b->decl = decl;
818 : 885619551 : b->id = name;
819 : 885619551 : b->depth = scope->depth;
820 : 885619551 : b->invisible = invisible;
821 : 885619551 : b->nested = nested;
822 : 885619551 : b->inner_comp = 0;
823 : 885619551 : b->in_struct = 0;
824 : 885619551 : b->locus = locus;
825 : :
826 : 885619551 : b->u.type = NULL;
827 : :
828 : 885619551 : b->prev = scope->bindings;
829 : 885619551 : scope->bindings = b;
830 : :
831 : 885619551 : if (decl_jump_unsafe (decl))
832 : 23799 : scope->has_jump_unsafe_decl = 1;
833 : :
834 : 885619551 : if (!name)
835 : : return;
836 : :
837 : 877175962 : switch (TREE_CODE (decl))
838 : : {
839 : 23273 : case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
840 : 611704 : case ENUMERAL_TYPE:
841 : 611704 : case UNION_TYPE:
842 : 611704 : case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
843 : 876540985 : case VAR_DECL:
844 : 876540985 : case FUNCTION_DECL:
845 : 876540985 : case TYPE_DECL:
846 : 876540985 : case CONST_DECL:
847 : 876540985 : case PARM_DECL:
848 : 876540985 : case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
849 : :
850 : 0 : default:
851 : 0 : gcc_unreachable ();
852 : : }
853 : :
854 : : /* Locate the appropriate place in the chain of shadowed decls
855 : : to insert this binding. Normally, scope == current_scope and
856 : : this does nothing. */
857 : 877176002 : while (*here && (*here)->depth > scope->depth)
858 : 40 : here = &(*here)->shadowed;
859 : :
860 : 877175962 : b->shadowed = *here;
861 : 877175962 : *here = b;
862 : : }
863 : :
864 : : /* Clear the binding structure B, stick it on the binding_freelist,
865 : : and return the former value of b->prev. This is used by pop_scope
866 : : and get_parm_info to iterate destructively over all the bindings
867 : : from a given scope. */
868 : : static struct c_binding *
869 : 869796728 : free_binding_and_advance (struct c_binding *b)
870 : : {
871 : 869796728 : struct c_binding *prev = b->prev;
872 : :
873 : 869796728 : memset (b, 0, sizeof (struct c_binding));
874 : 869796728 : b->prev = binding_freelist;
875 : 869796728 : binding_freelist = b;
876 : :
877 : 869796728 : return prev;
878 : : }
879 : :
880 : : /* Bind a label. Like bind, but skip fields which aren't used for
881 : : labels, and add the LABEL_VARS value. */
882 : : static void
883 : 23273 : bind_label (tree name, tree label, struct c_scope *scope,
884 : : struct c_label_vars *label_vars)
885 : : {
886 : 23273 : struct c_binding *b;
887 : :
888 : 23273 : bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
889 : : UNKNOWN_LOCATION);
890 : :
891 : 23273 : scope->has_label_bindings = true;
892 : :
893 : 23273 : b = scope->bindings;
894 : 23273 : gcc_assert (b->decl == label);
895 : 23273 : label_vars->shadowed = b->u.label;
896 : 23273 : b->u.label = label_vars;
897 : 23273 : }
898 : :
899 : : /* Hook called at end of compilation to assume 1 elt
900 : : for a file-scope tentative array defn that wasn't complete before. */
901 : :
902 : : void
903 : 7432 : c_finish_incomplete_decl (tree decl)
904 : : {
905 : 7432 : if (VAR_P (decl))
906 : : {
907 : 7432 : tree type = TREE_TYPE (decl);
908 : 7432 : if (type != error_mark_node
909 : 7358 : && TREE_CODE (type) == ARRAY_TYPE
910 : 7196 : && !DECL_EXTERNAL (decl)
911 : 7510 : && TYPE_DOMAIN (type) == NULL_TREE)
912 : : {
913 : 78 : warning_at (DECL_SOURCE_LOCATION (decl),
914 : 78 : 0, "array %q+D assumed to have one element", decl);
915 : :
916 : 78 : complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
917 : :
918 : 78 : relayout_decl (decl);
919 : : }
920 : : }
921 : 7432 : }
922 : :
923 : : /* Record that inline function FUNC contains a reference (location
924 : : LOC) to static DECL (file-scope or function-local according to
925 : : TYPE). */
926 : :
927 : : void
928 : 36 : record_inline_static (location_t loc, tree func, tree decl,
929 : : enum c_inline_static_type type)
930 : : {
931 : 36 : c_inline_static *csi = ggc_alloc<c_inline_static> ();
932 : 36 : csi->location = loc;
933 : 36 : csi->function = func;
934 : 36 : csi->static_decl = decl;
935 : 36 : csi->type = type;
936 : 36 : csi->next = c_inline_statics;
937 : 36 : c_inline_statics = csi;
938 : 36 : }
939 : :
940 : : /* Check for references to static declarations in inline functions at
941 : : the end of the translation unit and diagnose them if the functions
942 : : are still inline definitions. */
943 : :
944 : : static void
945 : 106012 : check_inline_statics (void)
946 : : {
947 : 106012 : struct c_inline_static *csi;
948 : 106048 : for (csi = c_inline_statics; csi; csi = csi->next)
949 : : {
950 : 36 : if (DECL_EXTERNAL (csi->function))
951 : 30 : switch (csi->type)
952 : : {
953 : 23 : case csi_internal:
954 : 23 : pedwarn (csi->location, 0,
955 : : "%qD is static but used in inline function %qD "
956 : : "which is not static", csi->static_decl, csi->function);
957 : 23 : break;
958 : 7 : case csi_modifiable:
959 : 7 : pedwarn (csi->location, 0,
960 : : "%q+D is static but declared in inline function %qD "
961 : : "which is not static", csi->static_decl, csi->function);
962 : 7 : break;
963 : 0 : default:
964 : 0 : gcc_unreachable ();
965 : : }
966 : : }
967 : 106012 : c_inline_statics = NULL;
968 : 106012 : }
969 : :
970 : : /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
971 : : for the current state, otherwise set it to uninitialized. */
972 : :
973 : : static void
974 : 132381 : set_spot_bindings (struct c_spot_bindings *p, bool defining)
975 : : {
976 : 0 : if (defining)
977 : : {
978 : 125739 : p->scope = current_scope;
979 : 16631 : p->bindings_in_scope = current_scope->bindings;
980 : : }
981 : : else
982 : : {
983 : 6642 : p->scope = NULL;
984 : 6642 : p->bindings_in_scope = NULL;
985 : : }
986 : 132381 : p->stmt_exprs = 0;
987 : 132381 : p->left_stmt_expr = false;
988 : 0 : }
989 : :
990 : : /* Update spot bindings P as we pop out of SCOPE. Return true if we
991 : : should push decls for a label. */
992 : :
993 : : static bool
994 : 470595754 : update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
995 : : {
996 : 0 : if (p->scope != scope)
997 : : {
998 : : /* This label or goto is defined in some other scope, or it is a
999 : : label which is not yet defined. There is nothing to
1000 : : update. */
1001 : : return false;
1002 : : }
1003 : :
1004 : : /* Adjust the spot bindings to refer to the bindings already defined
1005 : : in the enclosing scope. */
1006 : 200295 : p->scope = scope->outer;
1007 : 200295 : p->bindings_in_scope = p->scope->bindings;
1008 : :
1009 : 148201 : return true;
1010 : : }
1011 : :
1012 : : /* The Objective-C front-end often needs to determine the current scope. */
1013 : :
1014 : : void *
1015 : 0 : objc_get_current_scope (void)
1016 : : {
1017 : 0 : return current_scope;
1018 : : }
1019 : :
1020 : : /* The following function is used only by Objective-C. It needs to live here
1021 : : because it accesses the innards of c_scope. */
1022 : :
1023 : : void
1024 : 0 : objc_mark_locals_volatile (void *enclosing_blk)
1025 : : {
1026 : 0 : struct c_scope *scope;
1027 : 0 : struct c_binding *b;
1028 : :
1029 : 0 : for (scope = current_scope;
1030 : 0 : scope && scope != enclosing_blk;
1031 : 0 : scope = scope->outer)
1032 : : {
1033 : 0 : for (b = scope->bindings; b; b = b->prev)
1034 : 0 : objc_volatilize_decl (b->decl);
1035 : :
1036 : : /* Do not climb up past the current function. */
1037 : 0 : if (scope->function_body)
1038 : : break;
1039 : : }
1040 : 0 : }
1041 : :
1042 : : /* Return true if we are in the global binding level. */
1043 : :
1044 : : bool
1045 : 2063755 : global_bindings_p (void)
1046 : : {
1047 : 2063755 : return current_scope == file_scope;
1048 : : }
1049 : :
1050 : : /* Return true if we're declaring parameters in an old-style function
1051 : : declaration. */
1052 : :
1053 : : bool
1054 : 33486 : old_style_parameter_scope (void)
1055 : : {
1056 : : /* If processing parameters and there is no function statement list, we
1057 : : * have an old-style function declaration. */
1058 : 33486 : return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
1059 : : }
1060 : :
1061 : : void
1062 : 50643 : keep_next_level (void)
1063 : : {
1064 : 50643 : keep_next_level_flag = true;
1065 : 50643 : }
1066 : :
1067 : : /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
1068 : :
1069 : : void
1070 : 20 : set_float_const_decimal64 (void)
1071 : : {
1072 : 20 : current_scope->float_const_decimal64 = true;
1073 : 20 : }
1074 : :
1075 : : /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
1076 : :
1077 : : void
1078 : 28 : clear_float_const_decimal64 (void)
1079 : : {
1080 : 28 : current_scope->float_const_decimal64 = false;
1081 : 28 : }
1082 : :
1083 : : /* Return nonzero if an unsuffixed float constant is _Decimal64. */
1084 : :
1085 : : bool
1086 : 588777 : float_const_decimal64_p (void)
1087 : : {
1088 : 588777 : return current_scope->float_const_decimal64;
1089 : : }
1090 : :
1091 : : /* Identify this scope as currently being filled with parameters. */
1092 : :
1093 : : void
1094 : 84178431 : declare_parm_level (void)
1095 : : {
1096 : 84178431 : current_scope->parm_flag = true;
1097 : 84178431 : }
1098 : :
1099 : : void
1100 : 124387678 : push_scope (void)
1101 : : {
1102 : 124387678 : if (next_is_function_body)
1103 : : {
1104 : : /* This is the transition from the parameters to the top level
1105 : : of the function body. These are the same scope
1106 : : (C99 6.2.1p4,6) so we do not push another scope structure.
1107 : : next_is_function_body is set only by store_parm_decls, which
1108 : : in turn is called when and only when we are about to
1109 : : encounter the opening curly brace for the function body.
1110 : :
1111 : : The outermost block of a function always gets a BLOCK node,
1112 : : because the debugging output routines expect that each
1113 : : function has at least one BLOCK. */
1114 : 35349684 : current_scope->parm_flag = false;
1115 : 35349684 : current_scope->function_body = true;
1116 : 35349684 : current_scope->keep = true;
1117 : 35349684 : current_scope->outer_function = current_function_scope;
1118 : 35349684 : current_function_scope = current_scope;
1119 : :
1120 : 35349684 : keep_next_level_flag = false;
1121 : 35349684 : next_is_function_body = false;
1122 : :
1123 : : /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1124 : 35349684 : if (current_scope->outer)
1125 : 35349684 : current_scope->float_const_decimal64
1126 : 35349684 : = current_scope->outer->float_const_decimal64;
1127 : : else
1128 : 0 : current_scope->float_const_decimal64 = false;
1129 : : }
1130 : : else
1131 : : {
1132 : 89037994 : struct c_scope *scope;
1133 : 89037994 : if (scope_freelist)
1134 : : {
1135 : 88204444 : scope = scope_freelist;
1136 : 88204444 : scope_freelist = scope->outer;
1137 : : }
1138 : : else
1139 : 833550 : scope = ggc_cleared_alloc<c_scope> ();
1140 : :
1141 : : /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1142 : 89037994 : if (current_scope)
1143 : 88927004 : scope->float_const_decimal64 = current_scope->float_const_decimal64;
1144 : : else
1145 : 110990 : scope->float_const_decimal64 = false;
1146 : :
1147 : 89037994 : scope->keep = keep_next_level_flag;
1148 : 89037994 : scope->outer = current_scope;
1149 : 89037994 : scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1150 : :
1151 : : /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1152 : : possible. */
1153 : 89037994 : if (current_scope && scope->depth == 0)
1154 : : {
1155 : 0 : scope->depth--;
1156 : 0 : sorry ("GCC supports only %u nested scopes", scope->depth);
1157 : : }
1158 : :
1159 : 89037994 : current_scope = scope;
1160 : 89037994 : keep_next_level_flag = false;
1161 : : }
1162 : 124387678 : }
1163 : :
1164 : : /* This is called when we are leaving SCOPE. For each label defined
1165 : : in SCOPE, add any appropriate decls to its decls_in_scope fields.
1166 : : These are the decls whose initialization will be skipped by a goto
1167 : : later in the function. */
1168 : :
1169 : : static void
1170 : 89032121 : update_label_decls (struct c_scope *scope)
1171 : : {
1172 : 89032121 : struct c_scope *s;
1173 : :
1174 : 89032121 : s = scope;
1175 : 322823565 : while (s != NULL)
1176 : : {
1177 : 273798750 : if (s->has_label_bindings)
1178 : : {
1179 : 230026 : struct c_binding *b;
1180 : :
1181 : 2124076 : for (b = s->bindings; b != NULL; b = b->prev)
1182 : : {
1183 : 1894050 : struct c_label_vars *label_vars;
1184 : 1894050 : struct c_binding *b1;
1185 : 1894050 : bool hjud;
1186 : 1894050 : unsigned int ix;
1187 : 1894050 : struct c_goto_bindings *g;
1188 : :
1189 : 1894050 : if (TREE_CODE (b->decl) != LABEL_DECL)
1190 : 1398172 : continue;
1191 : 495878 : label_vars = b->u.label;
1192 : :
1193 : 495878 : b1 = label_vars->label_bindings.bindings_in_scope;
1194 : 495878 : if (label_vars->label_bindings.scope == NULL)
1195 : : hjud = false;
1196 : : else
1197 : 190402 : hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1198 : 495878 : if (update_spot_bindings (scope, &label_vars->label_bindings))
1199 : : {
1200 : : /* This label is defined in this scope. */
1201 : 52094 : if (hjud)
1202 : : {
1203 : 169637 : for (; b1 != NULL; b1 = b1->prev)
1204 : : {
1205 : : /* A goto from later in the function to this
1206 : : label will never see the initialization
1207 : : of B1, if any. Save it to issue a
1208 : : warning if needed. */
1209 : 148220 : if (decl_jump_unsafe (b1->decl))
1210 : 19448 : vec_safe_push(label_vars->decls_in_scope, b1->decl);
1211 : : }
1212 : : }
1213 : : }
1214 : :
1215 : : /* Update the bindings of any goto statements associated
1216 : : with this label. */
1217 : 472283128 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1218 : 470248077 : update_spot_bindings (scope, &g->goto_bindings);
1219 : : }
1220 : : }
1221 : :
1222 : : /* Don't search beyond the current function. */
1223 : 273798750 : if (s == current_function_scope)
1224 : : break;
1225 : :
1226 : 233791444 : s = s->outer;
1227 : : }
1228 : 89032121 : }
1229 : :
1230 : : /* Exit a scope. Restore the state of the identifier-decl mappings
1231 : : that were in effect when this scope was entered. Return a BLOCK
1232 : : node containing all the DECLs in this scope that are of interest
1233 : : to debug info generation. */
1234 : :
1235 : : tree
1236 : 89032121 : pop_scope (void)
1237 : : {
1238 : 89032121 : struct c_scope *scope = current_scope;
1239 : 89032121 : tree block, context, p;
1240 : 89032121 : struct c_binding *b;
1241 : :
1242 : 89032121 : bool functionbody = scope->function_body;
1243 : 89032121 : bool keep = functionbody || scope->keep || scope->bindings;
1244 : :
1245 : 89032121 : update_label_decls (scope);
1246 : :
1247 : : /* If appropriate, create a BLOCK to record the decls for the life
1248 : : of this function. */
1249 : 89032121 : block = NULL_TREE;
1250 : 89032121 : if (keep)
1251 : : {
1252 : 35834371 : block = make_node (BLOCK);
1253 : 35834371 : BLOCK_SUBBLOCKS (block) = scope->blocks;
1254 : 35834371 : TREE_USED (block) = 1;
1255 : :
1256 : : /* In each subblock, record that this is its superior. */
1257 : 36107775 : for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1258 : 273404 : BLOCK_SUPERCONTEXT (p) = block;
1259 : :
1260 : 35834371 : BLOCK_VARS (block) = NULL_TREE;
1261 : : }
1262 : :
1263 : : /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1264 : : scope must be set so that they point to the appropriate
1265 : : construct, i.e. either to the current FUNCTION_DECL node, or
1266 : : else to the BLOCK node we just constructed.
1267 : :
1268 : : Note that for tagged types whose scope is just the formal
1269 : : parameter list for some function type specification, we can't
1270 : : properly set their TYPE_CONTEXTs here, because we don't have a
1271 : : pointer to the appropriate FUNCTION_TYPE node readily available
1272 : : to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1273 : : type nodes get set in `grokdeclarator' as soon as we have created
1274 : : the FUNCTION_TYPE node which will represent the "scope" for these
1275 : : "parameter list local" tagged types. */
1276 : 89032121 : if (scope->function_body)
1277 : 35349683 : context = current_function_decl;
1278 : 53682438 : else if (scope == file_scope)
1279 : : {
1280 : 105642 : tree file_decl
1281 : 105642 : = build_translation_unit_decl (get_identifier (main_input_filename));
1282 : 105642 : context = file_decl;
1283 : 105642 : debug_hooks->register_main_translation_unit (file_decl);
1284 : : }
1285 : : else
1286 : : context = block;
1287 : :
1288 : : /* Clear all bindings in this scope. */
1289 : 839598367 : for (b = scope->bindings; b; b = free_binding_and_advance (b))
1290 : : {
1291 : 750566246 : p = b->decl;
1292 : 750566246 : switch (TREE_CODE (p))
1293 : : {
1294 : 23273 : case LABEL_DECL:
1295 : : /* Warnings for unused labels, errors for undefined labels. */
1296 : 23273 : if (TREE_USED (p) && !DECL_INITIAL (p))
1297 : : {
1298 : 63 : error ("label %q+D used but not defined", p);
1299 : 63 : DECL_INITIAL (p) = error_mark_node;
1300 : : }
1301 : : else
1302 : 23210 : warn_for_unused_label (p);
1303 : :
1304 : : /* Labels go in BLOCK_VARS. */
1305 : 23273 : DECL_CHAIN (p) = BLOCK_VARS (block);
1306 : 23273 : BLOCK_VARS (block) = p;
1307 : 23273 : gcc_assert (I_LABEL_BINDING (b->id) == b);
1308 : 23273 : I_LABEL_BINDING (b->id) = b->shadowed;
1309 : :
1310 : : /* Also pop back to the shadowed label_vars. */
1311 : 23273 : release_tree_vector (b->u.label->decls_in_scope);
1312 : 23273 : b->u.label = b->u.label->shadowed;
1313 : 23273 : break;
1314 : :
1315 : 1387924 : case ENUMERAL_TYPE:
1316 : 1387924 : case UNION_TYPE:
1317 : 1387924 : case RECORD_TYPE:
1318 : :
1319 : : /* Types may not have tag-names, in which case the type
1320 : : appears in the bindings list with b->id NULL. */
1321 : 1387924 : if (b->id)
1322 : : {
1323 : 609883 : gcc_assert (I_TAG_BINDING (b->id) == b);
1324 : 609883 : I_TAG_BINDING (b->id) = b->shadowed;
1325 : : }
1326 : : break;
1327 : :
1328 : 623916233 : case FUNCTION_DECL:
1329 : : /* Propagate TREE_ADDRESSABLE from nested functions to their
1330 : : containing functions. */
1331 : 623916233 : if (!TREE_ASM_WRITTEN (p)
1332 : 623916233 : && DECL_INITIAL (p) != NULL_TREE
1333 : 70354459 : && TREE_ADDRESSABLE (p)
1334 : 3602605 : && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1335 : 623916233 : && DECL_ABSTRACT_ORIGIN (p) != p)
1336 : 0 : TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1337 : 623916233 : if (!TREE_PUBLIC (p)
1338 : 344230 : && !DECL_INITIAL (p)
1339 : 127 : && !b->nested
1340 : 96 : && scope != file_scope
1341 : 623916239 : && scope != external_scope)
1342 : : {
1343 : 6 : error ("nested function %q+D declared but never defined", p);
1344 : 6 : undef_nested_function = true;
1345 : : }
1346 : 623916227 : else if (DECL_DECLARED_INLINE_P (p)
1347 : 69191647 : && TREE_PUBLIC (p)
1348 : 692952839 : && !DECL_INITIAL (p))
1349 : : {
1350 : : /* C99 6.7.4p6: "a function with external linkage... declared
1351 : : with an inline function specifier ... shall also be defined
1352 : : in the same translation unit." */
1353 : 72 : if (!flag_gnu89_inline
1354 : 47 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1355 : 103 : && scope == external_scope)
1356 : 11 : pedwarn (input_location, 0,
1357 : : "inline function %q+D declared but never defined", p);
1358 : 72 : DECL_EXTERNAL (p) = 1;
1359 : : }
1360 : :
1361 : 623916233 : goto common_symbol;
1362 : :
1363 : 10740903 : case VAR_DECL:
1364 : : /* Warnings for unused variables. */
1365 : 8179316 : if ((!TREE_USED (p) || !DECL_READ_P (p))
1366 : 2589471 : && !warning_suppressed_p (p, OPT_Wunused_but_set_variable)
1367 : 2588967 : && !DECL_IN_SYSTEM_HEADER (p)
1368 : 2511118 : && DECL_NAME (p)
1369 : 2511118 : && !DECL_ARTIFICIAL (p)
1370 : 2510760 : && scope != file_scope
1371 : 12364293 : && scope != external_scope)
1372 : : {
1373 : 737627 : if (!TREE_USED (p))
1374 : : {
1375 : 732383 : warning (OPT_Wunused_variable, "unused variable %q+D", p);
1376 : 732383 : suppress_warning (p, OPT_Wunused_variable);
1377 : : }
1378 : 5244 : else if (DECL_CONTEXT (p) == current_function_decl)
1379 : 5225 : warning_at (DECL_SOURCE_LOCATION (p),
1380 : 5225 : OPT_Wunused_but_set_variable,
1381 : : "variable %qD set but not used", p);
1382 : : }
1383 : :
1384 : 10740903 : if (b->inner_comp)
1385 : : {
1386 : 2 : error ("type of array %q+D completed incompatibly with"
1387 : : " implicit initialization", p);
1388 : : }
1389 : :
1390 : : /* Fall through. */
1391 : 651728032 : case TYPE_DECL:
1392 : 651728032 : case CONST_DECL:
1393 : 10740901 : common_symbol:
1394 : : /* All of these go in BLOCK_VARS, but only if this is the
1395 : : binding in the home scope. */
1396 : 651728032 : if (!b->nested)
1397 : : {
1398 : 392430069 : DECL_CHAIN (p) = BLOCK_VARS (block);
1399 : 392430069 : BLOCK_VARS (block) = p;
1400 : : }
1401 : 259297963 : else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1402 : : {
1403 : : /* For block local externs add a special
1404 : : DECL_EXTERNAL decl for debug info generation. */
1405 : 14570 : tree extp = copy_node (p);
1406 : :
1407 : 14570 : DECL_EXTERNAL (extp) = 1;
1408 : 14570 : TREE_STATIC (extp) = 0;
1409 : 14570 : TREE_PUBLIC (extp) = 1;
1410 : 14570 : DECL_INITIAL (extp) = NULL_TREE;
1411 : 14570 : DECL_LANG_SPECIFIC (extp) = NULL;
1412 : 14570 : DECL_CONTEXT (extp) = current_function_decl;
1413 : 14570 : if (TREE_CODE (p) == FUNCTION_DECL)
1414 : : {
1415 : 13035 : DECL_RESULT (extp) = NULL_TREE;
1416 : 13035 : DECL_SAVED_TREE (extp) = NULL_TREE;
1417 : 13035 : DECL_STRUCT_FUNCTION (extp) = NULL;
1418 : : }
1419 : 14570 : if (b->locus != UNKNOWN_LOCATION)
1420 : 14570 : DECL_SOURCE_LOCATION (extp) = b->locus;
1421 : 14570 : DECL_CHAIN (extp) = BLOCK_VARS (block);
1422 : 14570 : BLOCK_VARS (block) = extp;
1423 : : }
1424 : : /* If this is the file scope set DECL_CONTEXT of each decl to
1425 : : the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1426 : : work. */
1427 : 651728032 : if (scope == file_scope)
1428 : 270330380 : DECL_CONTEXT (p) = context;
1429 : :
1430 : 749155049 : gcc_fallthrough ();
1431 : : /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1432 : : already been put there by store_parm_decls. Unused-
1433 : : parameter warnings are handled by function.cc.
1434 : : error_mark_node obviously does not go in BLOCK_VARS and
1435 : : does not get unused-variable warnings. */
1436 : 749155049 : case PARM_DECL:
1437 : 749155049 : case ERROR_MARK:
1438 : : /* It is possible for a decl not to have a name. We get
1439 : : here with b->id NULL in this case. */
1440 : 749155049 : if (b->id)
1441 : : {
1442 : 746018098 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1443 : 746018098 : I_SYMBOL_BINDING (b->id) = b->shadowed;
1444 : 746018098 : if (b->shadowed && b->shadowed->u.type)
1445 : 4452632 : TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1446 : : }
1447 : : break;
1448 : :
1449 : 0 : default:
1450 : 0 : gcc_unreachable ();
1451 : : }
1452 : : }
1453 : :
1454 : :
1455 : : /* Dispose of the block that we just made inside some higher level. */
1456 : 89032121 : if ((scope->function_body || scope == file_scope) && context)
1457 : : {
1458 : 35455325 : DECL_INITIAL (context) = block;
1459 : 35455325 : BLOCK_SUPERCONTEXT (block) = context;
1460 : : }
1461 : 53576796 : else if (scope->outer)
1462 : : {
1463 : 53471154 : if (block)
1464 : 273404 : SCOPE_LIST_APPEND (scope->outer, blocks, block);
1465 : : /* If we did not make a block for the scope just exited, any
1466 : : blocks made for inner scopes must be carried forward so they
1467 : : will later become subblocks of something else. */
1468 : 53197750 : else if (scope->blocks)
1469 : 351454 : SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1470 : : }
1471 : :
1472 : : /* Pop the current scope, and free the structure for reuse. */
1473 : 89032121 : current_scope = scope->outer;
1474 : 89032121 : if (scope->function_body)
1475 : 35349683 : current_function_scope = scope->outer_function;
1476 : :
1477 : 89032121 : memset (scope, 0, sizeof (struct c_scope));
1478 : 89032121 : scope->outer = scope_freelist;
1479 : 89032121 : scope_freelist = scope;
1480 : :
1481 : 89032121 : return block;
1482 : : }
1483 : :
1484 : : void
1485 : 106165 : push_file_scope (void)
1486 : : {
1487 : 106165 : tree decl;
1488 : :
1489 : 106165 : if (file_scope)
1490 : : return;
1491 : :
1492 : 106165 : push_scope ();
1493 : 106165 : file_scope = current_scope;
1494 : :
1495 : 106165 : start_fname_decls ();
1496 : :
1497 : 211482184 : for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1498 : 211376019 : bind (DECL_NAME (decl), decl, file_scope,
1499 : 211376019 : /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1500 : : }
1501 : :
1502 : : void
1503 : 106012 : pop_file_scope (void)
1504 : : {
1505 : : /* In case there were missing closebraces, get us back to the global
1506 : : binding level. */
1507 : 106014 : while (current_scope != file_scope)
1508 : 2 : pop_scope ();
1509 : :
1510 : : /* __FUNCTION__ is defined at file scope (""). This
1511 : : call may not be necessary as my tests indicate it
1512 : : still works without it. */
1513 : 106012 : finish_fname_decls ();
1514 : :
1515 : 106012 : check_inline_statics ();
1516 : :
1517 : : /* This is the point to write out a PCH if we're doing that.
1518 : : In that case we do not want to do anything else. */
1519 : 106012 : if (pch_file)
1520 : : {
1521 : 370 : c_common_write_pch ();
1522 : : /* Ensure even the callers don't try to finalize the CU. */
1523 : 370 : flag_syntax_only = 1;
1524 : 370 : return;
1525 : : }
1526 : :
1527 : : /* Pop off the file scope and close this translation unit. */
1528 : 105642 : pop_scope ();
1529 : 105642 : file_scope = 0;
1530 : :
1531 : 105642 : maybe_apply_pending_pragma_weaks ();
1532 : : }
1533 : :
1534 : : /* Whether we are curently inside the initializer for an
1535 : : underspecified object definition (C23 auto or constexpr). */
1536 : : static bool in_underspecified_init;
1537 : :
1538 : : /* Start an underspecified object definition for NAME at LOC. This
1539 : : means that NAME is shadowed inside its initializer, so neither the
1540 : : definition being initialized, nor any definition from an outer
1541 : : scope, may be referenced during that initializer. Return state to
1542 : : be passed to finish_underspecified_init. If NAME is NULL_TREE, the
1543 : : underspecified object is a (constexpr) compound literal; there is
1544 : : no shadowing in that case, but all the other restrictions on
1545 : : underspecified object definitions still apply. */
1546 : : unsigned int
1547 : 648 : start_underspecified_init (location_t loc, tree name)
1548 : : {
1549 : 648 : bool prev = in_underspecified_init;
1550 : 648 : bool ok;
1551 : 648 : if (name == NULL_TREE)
1552 : : ok = true;
1553 : : else
1554 : : {
1555 : 416 : tree decl = build_decl (loc, VAR_DECL, name, error_mark_node);
1556 : 416 : C_DECL_UNDERSPECIFIED (decl) = 1;
1557 : 416 : struct c_scope *scope = current_scope;
1558 : 416 : struct c_binding *b = I_SYMBOL_BINDING (name);
1559 : 416 : if (b && B_IN_SCOPE (b, scope))
1560 : : {
1561 : 4 : error_at (loc, "underspecified declaration of %qE, which is already "
1562 : : "declared in this scope", name);
1563 : 4 : ok = false;
1564 : : }
1565 : : else
1566 : : {
1567 : 412 : bind (name, decl, scope, false, false, loc);
1568 : 412 : ok = true;
1569 : : }
1570 : : }
1571 : 648 : in_underspecified_init = true;
1572 : 648 : return ok | (prev << 1);
1573 : : }
1574 : :
1575 : : /* Finish an underspecified object definition for NAME, before that
1576 : : name is bound to the real declaration instead of a placeholder.
1577 : : PREV_STATE is the value returned by the call to
1578 : : start_underspecified_init. If NAME is NULL_TREE, this means a
1579 : : compound literal, as for start_underspecified_init. */
1580 : : void
1581 : 648 : finish_underspecified_init (tree name, unsigned int prev_state)
1582 : : {
1583 : 648 : if (name != NULL_TREE && (prev_state & 1))
1584 : : {
1585 : : /* A VAR_DECL was bound to the name to shadow any previous
1586 : : declarations for the name; remove that binding now. */
1587 : 412 : struct c_scope *scope = current_scope;
1588 : 412 : struct c_binding *b = I_SYMBOL_BINDING (name);
1589 : 412 : gcc_assert (b);
1590 : 412 : gcc_assert (B_IN_SCOPE (b, scope));
1591 : 412 : gcc_assert (VAR_P (b->decl));
1592 : 412 : gcc_assert (C_DECL_UNDERSPECIFIED (b->decl));
1593 : 412 : I_SYMBOL_BINDING (name) = b->shadowed;
1594 : : /* In erroneous cases there may be other bindings added to this
1595 : : scope during the initializer. */
1596 : 412 : struct c_binding **p = &scope->bindings;
1597 : 467 : while (*p != b)
1598 : 55 : p = &((*p)->prev);
1599 : 412 : *p = free_binding_and_advance (*p);
1600 : : }
1601 : 648 : in_underspecified_init = (prev_state & (1u << 1)) >> 1;
1602 : 648 : }
1603 : :
1604 : : /* Adjust the bindings for the start of a statement expression. */
1605 : :
1606 : : void
1607 : 33465 : c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1608 : : {
1609 : 33465 : struct c_scope *scope;
1610 : :
1611 : 228316 : for (scope = current_scope; scope != NULL; scope = scope->outer)
1612 : : {
1613 : 194851 : struct c_binding *b;
1614 : :
1615 : 194851 : if (!scope->has_label_bindings)
1616 : 192452 : continue;
1617 : :
1618 : 12608 : for (b = scope->bindings; b != NULL; b = b->prev)
1619 : : {
1620 : 10209 : struct c_label_vars *label_vars;
1621 : 10209 : unsigned int ix;
1622 : 10209 : struct c_goto_bindings *g;
1623 : :
1624 : 10209 : if (TREE_CODE (b->decl) != LABEL_DECL)
1625 : 1460 : continue;
1626 : 8749 : label_vars = b->u.label;
1627 : 8749 : ++label_vars->label_bindings.stmt_exprs;
1628 : 12745 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1629 : 1795 : ++g->goto_bindings.stmt_exprs;
1630 : : }
1631 : : }
1632 : :
1633 : 33465 : if (switch_bindings != NULL)
1634 : 173 : ++switch_bindings->stmt_exprs;
1635 : 33465 : }
1636 : :
1637 : : /* Adjust the bindings for the end of a statement expression. */
1638 : :
1639 : : void
1640 : 33465 : c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1641 : : {
1642 : 33465 : struct c_scope *scope;
1643 : :
1644 : 194851 : for (scope = current_scope; scope != NULL; scope = scope->outer)
1645 : : {
1646 : 161386 : struct c_binding *b;
1647 : :
1648 : 161386 : if (!scope->has_label_bindings)
1649 : 158752 : continue;
1650 : :
1651 : 16084 : for (b = scope->bindings; b != NULL; b = b->prev)
1652 : : {
1653 : 13450 : struct c_label_vars *label_vars;
1654 : 13450 : unsigned int ix;
1655 : 13450 : struct c_goto_bindings *g;
1656 : :
1657 : 13450 : if (TREE_CODE (b->decl) != LABEL_DECL)
1658 : 1520 : continue;
1659 : 11930 : label_vars = b->u.label;
1660 : 11930 : --label_vars->label_bindings.stmt_exprs;
1661 : 11930 : if (label_vars->label_bindings.stmt_exprs < 0)
1662 : : {
1663 : 3322 : label_vars->label_bindings.left_stmt_expr = true;
1664 : 3322 : label_vars->label_bindings.stmt_exprs = 0;
1665 : : }
1666 : 15978 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1667 : : {
1668 : 1791 : --g->goto_bindings.stmt_exprs;
1669 : 1791 : if (g->goto_bindings.stmt_exprs < 0)
1670 : : {
1671 : 128 : g->goto_bindings.left_stmt_expr = true;
1672 : 128 : g->goto_bindings.stmt_exprs = 0;
1673 : : }
1674 : : }
1675 : : }
1676 : : }
1677 : :
1678 : 33465 : if (switch_bindings != NULL)
1679 : : {
1680 : 173 : --switch_bindings->stmt_exprs;
1681 : 173 : gcc_assert (switch_bindings->stmt_exprs >= 0);
1682 : : }
1683 : 33465 : }
1684 : :
1685 : : /* Push a definition or a declaration of struct, union or enum tag "name".
1686 : : "type" should be the type node.
1687 : : We assume that the tag "name" is not already defined, and has a location
1688 : : of LOC.
1689 : :
1690 : : Note that the definition may really be just a forward reference.
1691 : : In that case, the TYPE_SIZE will be zero. */
1692 : :
1693 : : static void
1694 : 1391878 : pushtag (location_t loc, tree name, tree type)
1695 : : {
1696 : : /* Record the identifier as the type's name if it has none. */
1697 : 1391878 : if (name && !TYPE_NAME (type))
1698 : 611681 : TYPE_NAME (type) = name;
1699 : 1391878 : bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1700 : :
1701 : : /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1702 : : tagged type we just added to the current scope. This fake
1703 : : NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1704 : : to output a representation of a tagged type, and it also gives
1705 : : us a convenient place to record the "scope start" address for the
1706 : : tagged type. */
1707 : :
1708 : 1391878 : TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1709 : : TYPE_DECL, NULL_TREE, type));
1710 : :
1711 : : /* An approximation for now, so we can tell this is a function-scope tag.
1712 : : This will be updated in pop_scope. */
1713 : 1391878 : TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1714 : :
1715 : 1391878 : if (warn_cxx_compat && name != NULL_TREE)
1716 : : {
1717 : 628 : struct c_binding *b = I_SYMBOL_BINDING (name);
1718 : :
1719 : 628 : if (b != NULL
1720 : 12 : && b->decl != NULL_TREE
1721 : 12 : && TREE_CODE (b->decl) == TYPE_DECL
1722 : 11 : && (B_IN_CURRENT_SCOPE (b)
1723 : 6 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1724 : 633 : && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1725 : 5 : != TYPE_MAIN_VARIANT (type)))
1726 : : {
1727 : 5 : auto_diagnostic_group d;
1728 : 6 : if (warning_at (loc, OPT_Wc___compat,
1729 : : ("using %qD as both a typedef and a tag is "
1730 : : "invalid in C++"), b->decl)
1731 : 5 : && b->locus != UNKNOWN_LOCATION)
1732 : 4 : inform (b->locus, "originally defined here");
1733 : 5 : }
1734 : : }
1735 : 1391878 : }
1736 : :
1737 : : /* An exported interface to pushtag. This is used by the gdb plugin's
1738 : : binding oracle to introduce a new tag binding. */
1739 : :
1740 : : void
1741 : 0 : c_pushtag (location_t loc, tree name, tree type)
1742 : : {
1743 : 0 : pushtag (loc, name, type);
1744 : 0 : }
1745 : :
1746 : : /* An exported interface to bind a declaration. LOC is the location
1747 : : to use. DECL is the declaration to bind. The decl's name is used
1748 : : to determine how it is bound. If DECL is a VAR_DECL, then
1749 : : IS_GLOBAL determines whether the decl is put into the global (file
1750 : : and external) scope or the current function's scope; if DECL is not
1751 : : a VAR_DECL then it is always put into the file scope. */
1752 : :
1753 : : void
1754 : 0 : c_bind (location_t loc, tree decl, bool is_global)
1755 : : {
1756 : 0 : struct c_scope *scope;
1757 : 0 : bool nested = false;
1758 : :
1759 : 0 : if (!VAR_P (decl) || current_function_scope == NULL)
1760 : : {
1761 : : /* Types and functions are always considered to be global. */
1762 : 0 : scope = file_scope;
1763 : 0 : DECL_EXTERNAL (decl) = 1;
1764 : 0 : TREE_PUBLIC (decl) = 1;
1765 : : }
1766 : 0 : else if (is_global)
1767 : : {
1768 : : /* Also bind it into the external scope. */
1769 : 0 : bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1770 : 0 : nested = true;
1771 : 0 : scope = file_scope;
1772 : 0 : DECL_EXTERNAL (decl) = 1;
1773 : 0 : TREE_PUBLIC (decl) = 1;
1774 : : }
1775 : : else
1776 : : {
1777 : 0 : DECL_CONTEXT (decl) = current_function_decl;
1778 : 0 : TREE_PUBLIC (decl) = 0;
1779 : 0 : scope = current_function_scope;
1780 : : }
1781 : :
1782 : 0 : bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1783 : 0 : }
1784 : :
1785 : :
1786 : : /* Stores the first FILE*, const struct tm* etc. argument type (whatever
1787 : : it is) seen in a declaration of a file I/O etc. built-in, corresponding
1788 : : to the builtin_structptr_types array. Subsequent declarations of such
1789 : : built-ins are expected to refer to it rather than to fileptr_type_node,
1790 : : etc. which is just void* (or to any other type).
1791 : : Used only by match_builtin_function_types. */
1792 : :
1793 : : static const unsigned builtin_structptr_type_count
1794 : : = ARRAY_SIZE (builtin_structptr_types);
1795 : :
1796 : : static GTY(()) tree last_structptr_types[builtin_structptr_type_count];
1797 : :
1798 : : /* Returns true if types T1 and T2 representing return types or types
1799 : : of function arguments are close enough to be considered interchangeable
1800 : : in redeclarations of built-in functions. */
1801 : :
1802 : : static bool
1803 : 615860 : types_close_enough_to_match (tree t1, tree t2)
1804 : : {
1805 : 615860 : return (TYPE_MODE (t1) == TYPE_MODE (t2)
1806 : 615405 : && POINTER_TYPE_P (t1) == POINTER_TYPE_P (t2)
1807 : 2461922 : && FUNCTION_POINTER_TYPE_P (t1) == FUNCTION_POINTER_TYPE_P (t2));
1808 : : }
1809 : :
1810 : : /* Subroutine of compare_decls. Allow harmless mismatches in return
1811 : : and argument types provided that the type modes match. Set *STRICT
1812 : : and *ARGNO to the expected argument type and number in case of
1813 : : an argument type mismatch or null and zero otherwise. Return
1814 : : a unified type given a suitable match, and 0 otherwise. */
1815 : :
1816 : : static tree
1817 : 142854 : match_builtin_function_types (tree newtype, tree oldtype,
1818 : : tree *strict, unsigned *argno)
1819 : : {
1820 : 142854 : *argno = 0;
1821 : 142854 : *strict = NULL_TREE;
1822 : :
1823 : : /* Accept the return type of the new declaration if it has the same
1824 : : mode and if they're both pointers or if neither is. */
1825 : 142854 : tree oldrettype = TREE_TYPE (oldtype);
1826 : 142854 : tree newrettype = TREE_TYPE (newtype);
1827 : :
1828 : 142854 : if (!types_close_enough_to_match (oldrettype, newrettype))
1829 : : return NULL_TREE;
1830 : :
1831 : : /* Check that the return types are compatible but don't fail if they
1832 : : are not (e.g., int vs long in ILP32) and just let the caller know. */
1833 : 142492 : if (!comptypes (TYPE_MAIN_VARIANT (oldrettype),
1834 : 142492 : TYPE_MAIN_VARIANT (newrettype)))
1835 : 39 : *strict = oldrettype;
1836 : :
1837 : 142492 : tree oldargs = TYPE_ARG_TYPES (oldtype);
1838 : 142492 : tree newargs = TYPE_ARG_TYPES (newtype);
1839 : 142492 : tree tryargs = newargs;
1840 : :
1841 : 142492 : const unsigned nlst = ARRAY_SIZE (last_structptr_types);
1842 : 142492 : const unsigned nbst = ARRAY_SIZE (builtin_structptr_types);
1843 : :
1844 : 142492 : gcc_checking_assert (nlst == nbst);
1845 : :
1846 : 615301 : for (unsigned i = 1; oldargs || newargs; ++i)
1847 : : {
1848 : 473065 : if (!oldargs
1849 : 473065 : || !newargs
1850 : 473010 : || !TREE_VALUE (oldargs)
1851 : 946075 : || !TREE_VALUE (newargs))
1852 : : return NULL_TREE;
1853 : :
1854 : 473010 : tree oldtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1855 : 473010 : tree newtype = TREE_VALUE (newargs);
1856 : 473010 : if (newtype == error_mark_node)
1857 : : return NULL_TREE;
1858 : 473006 : newtype = TYPE_MAIN_VARIANT (newtype);
1859 : :
1860 : 473006 : if (!types_close_enough_to_match (oldtype, newtype))
1861 : : return NULL_TREE;
1862 : :
1863 : 472862 : unsigned j = nbst;
1864 : 472862 : if (POINTER_TYPE_P (oldtype))
1865 : : /* Iterate over well-known struct types like FILE (whose types
1866 : : aren't known to us) and compare the pointer to each to
1867 : : the pointer argument. */
1868 : 965982 : for (j = 0; j < nbst; ++j)
1869 : : {
1870 : 851028 : if (TREE_VALUE (oldargs) != builtin_structptr_types[j].node)
1871 : 709008 : continue;
1872 : : /* Store the first FILE* etc. argument type (whatever it is), and
1873 : : expect any subsequent declarations of file I/O etc. built-ins
1874 : : to refer to it rather than to fileptr_type_node etc. which is
1875 : : just void* (or const void*). */
1876 : 142020 : if (last_structptr_types[j])
1877 : : {
1878 : 124747 : if (!comptypes (last_structptr_types[j], newtype))
1879 : : {
1880 : 2 : *argno = i;
1881 : 2 : *strict = last_structptr_types[j];
1882 : : }
1883 : : }
1884 : : else
1885 : 17273 : last_structptr_types[j] = newtype;
1886 : : break;
1887 : : }
1888 : :
1889 : 472862 : if (j == nbst && !comptypes (oldtype, newtype))
1890 : : {
1891 : 251 : if (POINTER_TYPE_P (oldtype))
1892 : : {
1893 : : /* For incompatible pointers, only reject differences in
1894 : : the unqualified variants of the referenced types but
1895 : : consider differences in qualifiers as benign (report
1896 : : those to caller via *STRICT below). */
1897 : 204 : tree oldref = TYPE_MAIN_VARIANT (TREE_TYPE (oldtype));
1898 : 204 : tree newref = TYPE_MAIN_VARIANT (TREE_TYPE (newtype));
1899 : 204 : if (!comptypes (oldref, newref))
1900 : : return NULL_TREE;
1901 : : }
1902 : :
1903 : 198 : if (!*strict)
1904 : : {
1905 : 194 : *argno = i;
1906 : 194 : *strict = oldtype;
1907 : : }
1908 : : }
1909 : :
1910 : 472809 : oldargs = TREE_CHAIN (oldargs);
1911 : 472809 : newargs = TREE_CHAIN (newargs);
1912 : : }
1913 : :
1914 : 142236 : tree trytype = c_build_function_type (newrettype, tryargs);
1915 : :
1916 : : /* Allow declaration to change transaction_safe attribute. */
1917 : 142236 : tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1918 : 142236 : tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1919 : 142236 : tree newattrs = TYPE_ATTRIBUTES (newtype);
1920 : 142236 : tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1921 : 142236 : if (oldtsafe && !newtsafe)
1922 : 0 : oldattrs = remove_attribute ("transaction_safe", oldattrs);
1923 : 142236 : else if (newtsafe && !oldtsafe)
1924 : 7 : oldattrs = tree_cons (get_identifier ("transaction_safe"),
1925 : : NULL_TREE, oldattrs);
1926 : :
1927 : 142236 : return c_build_type_attribute_variant (trytype, oldattrs);
1928 : : }
1929 : :
1930 : : /* Subroutine of diagnose_mismatched_decls. Check for function type
1931 : : mismatch involving an empty arglist vs a nonempty one and give clearer
1932 : : diagnostics. */
1933 : : static void
1934 : 167 : diagnose_arglist_conflict (tree newdecl, tree olddecl,
1935 : : tree newtype, tree oldtype)
1936 : : {
1937 : 167 : tree t;
1938 : :
1939 : 167 : if (TREE_CODE (olddecl) != FUNCTION_DECL
1940 : 93 : || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1941 : 284 : || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1942 : 62 : || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1943 : 150 : return;
1944 : :
1945 : 17 : t = TYPE_ARG_TYPES (oldtype);
1946 : 17 : if (t == NULL_TREE)
1947 : 9 : t = TYPE_ARG_TYPES (newtype);
1948 : 18 : for (; t; t = TREE_CHAIN (t))
1949 : : {
1950 : 18 : tree type = TREE_VALUE (t);
1951 : :
1952 : 18 : if (TREE_CHAIN (t) == NULL_TREE
1953 : 18 : && TYPE_MAIN_VARIANT (type) != void_type_node)
1954 : : {
1955 : 10 : inform (input_location, "a parameter list with an ellipsis "
1956 : : "cannot match an empty parameter name list declaration");
1957 : 10 : break;
1958 : : }
1959 : :
1960 : 8 : if (!error_operand_p (type)
1961 : 8 : && c_type_promotes_to (type) != type)
1962 : : {
1963 : 7 : inform (input_location, "an argument type that has a default "
1964 : : "promotion cannot match an empty parameter name list "
1965 : : "declaration");
1966 : 7 : break;
1967 : : }
1968 : : }
1969 : : }
1970 : :
1971 : : /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1972 : : old-style function definition, NEWDECL is a prototype declaration.
1973 : : Diagnose inconsistencies in the argument list. Returns TRUE if
1974 : : the prototype is compatible, FALSE if not. */
1975 : : static bool
1976 : 19 : validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1977 : : {
1978 : 19 : tree newargs, oldargs;
1979 : 19 : int i;
1980 : :
1981 : : #define END_OF_ARGLIST(t) ((t) == void_type_node)
1982 : :
1983 : 19 : oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1984 : 19 : newargs = TYPE_ARG_TYPES (newtype);
1985 : 19 : i = 1;
1986 : :
1987 : 45 : for (;;)
1988 : : {
1989 : 32 : tree oldargtype = TREE_VALUE (oldargs);
1990 : 32 : tree newargtype = TREE_VALUE (newargs);
1991 : :
1992 : 32 : if (oldargtype == error_mark_node || newargtype == error_mark_node)
1993 : : return false;
1994 : :
1995 : 58 : oldargtype = (TYPE_ATOMIC (oldargtype)
1996 : 31 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1997 : : TYPE_QUAL_ATOMIC)
1998 : 27 : : TYPE_MAIN_VARIANT (oldargtype));
1999 : 60 : newargtype = (TYPE_ATOMIC (newargtype)
2000 : 31 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
2001 : : TYPE_QUAL_ATOMIC)
2002 : 29 : : TYPE_MAIN_VARIANT (newargtype));
2003 : :
2004 : 31 : if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
2005 : : break;
2006 : :
2007 : : /* Reaching the end of just one list means the two decls don't
2008 : : agree on the number of arguments. */
2009 : 22 : if (END_OF_ARGLIST (oldargtype))
2010 : : {
2011 : 2 : error ("prototype for %q+D declares more arguments "
2012 : : "than previous old-style definition", newdecl);
2013 : 2 : return false;
2014 : : }
2015 : 20 : else if (END_OF_ARGLIST (newargtype))
2016 : : {
2017 : 2 : error ("prototype for %q+D declares fewer arguments "
2018 : : "than previous old-style definition", newdecl);
2019 : 2 : return false;
2020 : : }
2021 : :
2022 : : /* Type for passing arg must be consistent with that declared
2023 : : for the arg. */
2024 : 18 : else if (!comptypes (oldargtype, newargtype))
2025 : : {
2026 : 5 : error ("prototype for %q+D declares argument %d"
2027 : : " with incompatible type",
2028 : : newdecl, i);
2029 : 5 : return false;
2030 : : }
2031 : :
2032 : 13 : oldargs = TREE_CHAIN (oldargs);
2033 : 13 : newargs = TREE_CHAIN (newargs);
2034 : 13 : i++;
2035 : 13 : }
2036 : :
2037 : : /* If we get here, no errors were found, but do issue a warning
2038 : : for this poor-style construct. */
2039 : 9 : warning (0, "prototype for %q+D follows non-prototype definition",
2040 : : newdecl);
2041 : 9 : return true;
2042 : : #undef END_OF_ARGLIST
2043 : : }
2044 : :
2045 : : /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
2046 : : first in a pair of mismatched declarations, using the diagnostic
2047 : : function DIAG. */
2048 : : static void
2049 : 404 : locate_old_decl (tree decl)
2050 : : {
2051 : 404 : if (TREE_CODE (decl) == FUNCTION_DECL
2052 : 192 : && fndecl_built_in_p (decl)
2053 : 407 : && !C_DECL_DECLARED_BUILTIN (decl))
2054 : : ;
2055 : 404 : else if (DECL_INITIAL (decl))
2056 : 118 : inform (input_location,
2057 : : "previous definition of %q+D with type %qT",
2058 : 118 : decl, TREE_TYPE (decl));
2059 : 286 : else if (C_DECL_IMPLICIT (decl))
2060 : 16 : inform (input_location,
2061 : : "previous implicit declaration of %q+D with type %qT",
2062 : 16 : decl, TREE_TYPE (decl));
2063 : : else
2064 : 270 : inform (input_location,
2065 : : "previous declaration of %q+D with type %qT",
2066 : 270 : decl, TREE_TYPE (decl));
2067 : 404 : }
2068 : :
2069 : :
2070 : : /* Helper function. For a tagged type, it finds the declaration
2071 : : for a visible tag declared in the same scope if such a
2072 : : declaration exists. */
2073 : : static tree
2074 : 1103159 : previous_tag (tree type)
2075 : : {
2076 : 1103159 : struct c_binding *b = NULL;
2077 : 1103159 : tree name = c_type_tag (type);
2078 : :
2079 : 1103159 : if (name)
2080 : 551405 : b = I_TAG_BINDING (name);
2081 : :
2082 : 551405 : if (b)
2083 : 551405 : b = b->shadowed;
2084 : :
2085 : 1103159 : if (b && B_IN_CURRENT_SCOPE (b))
2086 : 168 : return b->decl;
2087 : :
2088 : : return NULL_TREE;
2089 : : }
2090 : :
2091 : : /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
2092 : : Returns true if the caller should proceed to merge the two, false
2093 : : if OLDDECL should simply be discarded. As a side effect, issues
2094 : : all necessary diagnostics for invalid or poor-style combinations.
2095 : : If it returns true, writes the types of NEWDECL and OLDDECL to
2096 : : *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
2097 : : TREE_TYPE (NEWDECL, OLDDECL) respectively. */
2098 : :
2099 : : static bool
2100 : 4653934 : diagnose_mismatched_decls (tree newdecl, tree olddecl,
2101 : : tree *newtypep, tree *oldtypep)
2102 : : {
2103 : 4653934 : tree newtype, oldtype;
2104 : 4653934 : bool retval = true;
2105 : :
2106 : : #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
2107 : : && DECL_EXTERNAL (DECL))
2108 : :
2109 : : /* If we have error_mark_node for either decl or type, just discard
2110 : : the previous decl - we're in an error cascade already. */
2111 : 4653934 : if (olddecl == error_mark_node || newdecl == error_mark_node)
2112 : : return false;
2113 : 4653915 : *oldtypep = oldtype = TREE_TYPE (olddecl);
2114 : 4653915 : *newtypep = newtype = TREE_TYPE (newdecl);
2115 : 4653915 : if (oldtype == error_mark_node || newtype == error_mark_node)
2116 : : return false;
2117 : :
2118 : : /* Two different categories of symbol altogether. This is an error
2119 : : unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
2120 : 4653907 : if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2121 : : {
2122 : 29 : if (!(TREE_CODE (olddecl) == FUNCTION_DECL
2123 : 18 : && fndecl_built_in_p (olddecl)
2124 : 15 : && !C_DECL_DECLARED_BUILTIN (olddecl)))
2125 : : {
2126 : 15 : auto_diagnostic_group d;
2127 : 15 : error ("%q+D redeclared as different kind of symbol", newdecl);
2128 : 15 : locate_old_decl (olddecl);
2129 : 15 : }
2130 : 14 : else if (TREE_PUBLIC (newdecl))
2131 : 3 : warning (OPT_Wbuiltin_declaration_mismatch,
2132 : : "built-in function %q+D declared as non-function",
2133 : : newdecl);
2134 : : else
2135 : 11 : warning (OPT_Wshadow, "declaration of %q+D shadows "
2136 : : "a built-in function", newdecl);
2137 : 29 : return false;
2138 : : }
2139 : :
2140 : : /* Enumerators have no linkage, so may only be declared once in a
2141 : : given scope. */
2142 : 4653878 : if (TREE_CODE (olddecl) == CONST_DECL)
2143 : : {
2144 : 46 : if (flag_isoc23
2145 : 45 : && TYPE_NAME (DECL_CONTEXT (newdecl))
2146 : 43 : && DECL_CONTEXT (newdecl) != DECL_CONTEXT (olddecl)
2147 : 87 : && TYPE_NAME (DECL_CONTEXT (newdecl)) == TYPE_NAME (DECL_CONTEXT (olddecl)))
2148 : : {
2149 : 38 : if (!simple_cst_equal (DECL_INITIAL (olddecl), DECL_INITIAL (newdecl)))
2150 : : {
2151 : 1 : auto_diagnostic_group d;
2152 : 1 : error ("conflicting redeclaration of enumerator %q+D", newdecl);
2153 : 1 : locate_old_decl (olddecl);
2154 : 1 : }
2155 : : }
2156 : : else
2157 : : {
2158 : 8 : auto_diagnostic_group d;
2159 : 8 : error ("redeclaration of enumerator %q+D", newdecl);
2160 : 8 : locate_old_decl (olddecl);
2161 : 8 : }
2162 : 46 : return false;
2163 : : }
2164 : :
2165 : 4653832 : bool pedwarned = false;
2166 : 4653832 : bool warned = false;
2167 : 4653832 : bool enum_and_int_p = false;
2168 : 4653832 : auto_diagnostic_group d;
2169 : :
2170 : 4653832 : int comptypes_result = comptypes_check_enum_int (oldtype, newtype,
2171 : : &enum_and_int_p);
2172 : 4653832 : if (!comptypes_result)
2173 : : {
2174 : 143045 : if (TREE_CODE (olddecl) == FUNCTION_DECL
2175 : 142971 : && fndecl_built_in_p (olddecl, BUILT_IN_NORMAL)
2176 : 285900 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2177 : : {
2178 : : /* Accept "harmless" mismatches in function types such
2179 : : as missing qualifiers or int vs long when they're the same
2180 : : size. However, diagnose return and argument types that are
2181 : : incompatible according to language rules. */
2182 : 142854 : tree mismatch_expect;
2183 : 142854 : unsigned mismatch_argno;
2184 : :
2185 : 142854 : tree trytype = match_builtin_function_types (newtype, oldtype,
2186 : : &mismatch_expect,
2187 : : &mismatch_argno);
2188 : :
2189 : 142854 : if (trytype && comptypes (newtype, trytype))
2190 : 142236 : *oldtypep = oldtype = trytype;
2191 : : else
2192 : : {
2193 : : /* If types don't match for a built-in, throw away the
2194 : : built-in. No point in calling locate_old_decl here, it
2195 : : won't print anything. */
2196 : 618 : const char *header = header_for_builtin_fn (olddecl);
2197 : 618 : location_t loc = DECL_SOURCE_LOCATION (newdecl);
2198 : 1019 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
2199 : : "conflicting types for built-in function %q+D; "
2200 : : "expected %qT",
2201 : : newdecl, oldtype)
2202 : 618 : && header)
2203 : : {
2204 : : /* Suggest the right header to include as the preferred
2205 : : solution rather than the spelling of the declaration. */
2206 : 217 : rich_location richloc (line_table, loc);
2207 : 217 : maybe_add_include_fixit (&richloc, header, true);
2208 : 217 : inform (&richloc,
2209 : : "%qD is declared in header %qs", olddecl, header);
2210 : 217 : }
2211 : 618 : return false;
2212 : : }
2213 : :
2214 : 142236 : if (mismatch_expect && extra_warnings)
2215 : : {
2216 : 5 : location_t newloc = DECL_SOURCE_LOCATION (newdecl);
2217 : 5 : bool warned = false;
2218 : 5 : if (mismatch_argno)
2219 : 5 : warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2220 : : "mismatch in argument %u type of built-in "
2221 : : "function %qD; expected %qT",
2222 : : mismatch_argno, newdecl, mismatch_expect);
2223 : : else
2224 : 0 : warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2225 : : "mismatch in return type of built-in "
2226 : : "function %qD; expected %qT",
2227 : : newdecl, mismatch_expect);
2228 : 5 : const char *header = header_for_builtin_fn (olddecl);
2229 : 5 : if (warned && header)
2230 : : {
2231 : 5 : rich_location richloc (line_table, newloc);
2232 : 5 : maybe_add_include_fixit (&richloc, header, true);
2233 : 5 : inform (&richloc,
2234 : : "%qD is declared in header %qs", olddecl, header);
2235 : 5 : }
2236 : : }
2237 : : }
2238 : 191 : else if (TREE_CODE (olddecl) == FUNCTION_DECL
2239 : 191 : && DECL_IS_UNDECLARED_BUILTIN (olddecl))
2240 : : {
2241 : : /* A conflicting function declaration for a predeclared
2242 : : function that isn't actually built in. Objective C uses
2243 : : these. The new declaration silently overrides everything
2244 : : but the volatility (i.e. noreturn) indication. See also
2245 : : below. FIXME: Make Objective C use normal builtins. */
2246 : 0 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2247 : 0 : return false;
2248 : : }
2249 : : /* Permit void foo (...) to match int foo (...) if the latter is
2250 : : the definition and implicit int was used. See
2251 : : c-torture/compile/920625-2.c. */
2252 : 117 : else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
2253 : 57 : && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
2254 : 26 : && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
2255 : 197 : && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
2256 : : {
2257 : 5 : pedwarned = pedwarn (input_location, 0,
2258 : : "conflicting types for %q+D", newdecl);
2259 : : /* Make sure we keep void as the return type. */
2260 : 5 : TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
2261 : 5 : C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
2262 : : }
2263 : : /* Permit void foo (...) to match an earlier call to foo (...) with
2264 : : no declared type (thus, implicitly int). */
2265 : 186 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2266 : 112 : && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
2267 : 81 : && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
2268 : 209 : && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
2269 : : {
2270 : 19 : pedwarned = pedwarn (input_location, 0,
2271 : : "conflicting types for %q+D; have %qT",
2272 : : newdecl, newtype);
2273 : : /* Make sure we keep void as the return type. */
2274 : 19 : TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
2275 : : }
2276 : : else
2277 : : {
2278 : 167 : int new_quals = TYPE_QUALS (newtype);
2279 : 167 : int old_quals = TYPE_QUALS (oldtype);
2280 : :
2281 : 167 : if (new_quals != old_quals)
2282 : : {
2283 : 20 : addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
2284 : 20 : addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
2285 : 20 : if (new_addr != old_addr)
2286 : : {
2287 : 0 : if (ADDR_SPACE_GENERIC_P (new_addr))
2288 : 0 : error ("conflicting named address spaces (generic vs %s) "
2289 : : "for %q+D",
2290 : : c_addr_space_name (old_addr), newdecl);
2291 : 0 : else if (ADDR_SPACE_GENERIC_P (old_addr))
2292 : 0 : error ("conflicting named address spaces (%s vs generic) "
2293 : : "for %q+D",
2294 : : c_addr_space_name (new_addr), newdecl);
2295 : : else
2296 : 0 : error ("conflicting named address spaces (%s vs %s) "
2297 : : "for %q+D",
2298 : : c_addr_space_name (new_addr),
2299 : : c_addr_space_name (old_addr),
2300 : : newdecl);
2301 : : }
2302 : :
2303 : 20 : if (CLEAR_QUAL_ADDR_SPACE (new_quals)
2304 : 20 : != CLEAR_QUAL_ADDR_SPACE (old_quals))
2305 : 20 : error ("conflicting type qualifiers for %q+D", newdecl);
2306 : : }
2307 : : else
2308 : 147 : error ("conflicting types for %q+D; have %qT", newdecl, newtype);
2309 : 167 : diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
2310 : 167 : locate_old_decl (olddecl);
2311 : 167 : return false;
2312 : : }
2313 : : }
2314 : : /* Warn about enum/integer type mismatches. They are compatible types
2315 : : (C23 6.7.2.2/5), but may pose portability problems. */
2316 : 4510787 : else if (enum_and_int_p
2317 : 193 : && TREE_CODE (newdecl) != TYPE_DECL
2318 : : /* Don't warn about acc_on_device built-in redeclaration,
2319 : : the built-in is declared with int rather than enum because
2320 : : the enum isn't intrinsic. */
2321 : 4510976 : && !(TREE_CODE (olddecl) == FUNCTION_DECL
2322 : 152 : && fndecl_built_in_p (olddecl, BUILT_IN_ACC_ON_DEVICE)
2323 : 122 : && !C_DECL_DECLARED_BUILTIN (olddecl)))
2324 : 67 : warned = warning_at (DECL_SOURCE_LOCATION (newdecl),
2325 : 67 : OPT_Wenum_int_mismatch,
2326 : : "conflicting types for %q+D due to enum/integer "
2327 : : "mismatch; have %qT", newdecl, newtype);
2328 : :
2329 : : /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2330 : : but silently ignore the redeclaration if either is in a system
2331 : : header. (Conflicting redeclarations were handled above.) This
2332 : : is allowed for C11 if the types are the same, not just
2333 : : compatible. */
2334 : 4653047 : if (TREE_CODE (newdecl) == TYPE_DECL)
2335 : : {
2336 : 44481 : bool types_different = false;
2337 : :
2338 : 44481 : comptypes_result
2339 : 44481 : = comptypes_check_different_types (oldtype, newtype, &types_different);
2340 : :
2341 : 44481 : if (comptypes_result != 1 || types_different)
2342 : : {
2343 : 11 : error ("redefinition of typedef %q+D with different type", newdecl);
2344 : 11 : locate_old_decl (olddecl);
2345 : 11 : return false;
2346 : : }
2347 : :
2348 : 44470 : if (DECL_IN_SYSTEM_HEADER (newdecl)
2349 : 2214 : || DECL_IN_SYSTEM_HEADER (olddecl)
2350 : 1995 : || warning_suppressed_p (newdecl, OPT_Wpedantic)
2351 : 46465 : || warning_suppressed_p (olddecl, OPT_Wpedantic))
2352 : 42475 : return true; /* Allow OLDDECL to continue in use. */
2353 : :
2354 : 1995 : if (c_type_variably_modified_p (newtype))
2355 : : {
2356 : 3 : error ("redefinition of typedef %q+D with variably modified type",
2357 : : newdecl);
2358 : 3 : locate_old_decl (olddecl);
2359 : : }
2360 : 1992 : else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2361 : : "redefinition of typedef %q+D", newdecl))
2362 : 8 : locate_old_decl (olddecl);
2363 : :
2364 : 1995 : return true;
2365 : : }
2366 : :
2367 : : /* Function declarations can either be 'static' or 'extern' (no
2368 : : qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2369 : : can never conflict with each other on account of linkage
2370 : : (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2371 : : gnu89 mode permits two definitions if one is 'extern inline' and
2372 : : one is not. The non- extern-inline definition supersedes the
2373 : : extern-inline definition. */
2374 : :
2375 : 4608566 : else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2376 : : {
2377 : : /* If you declare a built-in function name as static, or
2378 : : define the built-in with an old-style definition (so we
2379 : : can't validate the argument list) the built-in definition is
2380 : : overridden, but optionally warn this was a bad choice of name. */
2381 : 4590142 : if (fndecl_built_in_p (olddecl)
2382 : 8273873 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2383 : : {
2384 : 3548494 : if (!TREE_PUBLIC (newdecl)
2385 : 3548494 : || (DECL_INITIAL (newdecl)
2386 : 4795 : && !prototype_p (TREE_TYPE (newdecl))))
2387 : : {
2388 : 103 : warning_at (DECL_SOURCE_LOCATION (newdecl),
2389 : 103 : OPT_Wshadow, "declaration of %qD shadows "
2390 : : "a built-in function", newdecl);
2391 : : /* Discard the old built-in function. */
2392 : 103 : return false;
2393 : : }
2394 : :
2395 : 3548391 : if (!prototype_p (TREE_TYPE (newdecl)))
2396 : : {
2397 : : /* Set for built-ins that take no arguments. */
2398 : 342 : bool func_void_args = false;
2399 : 342 : if (tree at = TYPE_ARG_TYPES (oldtype))
2400 : 342 : func_void_args = VOID_TYPE_P (TREE_VALUE (at));
2401 : :
2402 : 342 : if (extra_warnings && !func_void_args)
2403 : 47 : warning_at (DECL_SOURCE_LOCATION (newdecl),
2404 : 47 : OPT_Wbuiltin_declaration_mismatch,
2405 : : "declaration of built-in function %qD without "
2406 : : "a prototype; expected %qT",
2407 : 47 : newdecl, TREE_TYPE (olddecl));
2408 : : }
2409 : : }
2410 : :
2411 : 4590039 : if (DECL_INITIAL (newdecl))
2412 : : {
2413 : 228377 : if (DECL_INITIAL (olddecl))
2414 : : {
2415 : : /* If the new declaration isn't overriding an extern inline
2416 : : reject the new decl. In c99, no overriding is allowed
2417 : : in the same translation unit. */
2418 : 209 : if (!DECL_EXTERN_INLINE (olddecl)
2419 : 91 : || DECL_EXTERN_INLINE (newdecl)
2420 : 204 : || (!flag_gnu89_inline
2421 : 15 : && (!DECL_DECLARED_INLINE_P (olddecl)
2422 : 15 : || !lookup_attribute ("gnu_inline",
2423 : 15 : DECL_ATTRIBUTES (olddecl)))
2424 : 0 : && (!DECL_DECLARED_INLINE_P (newdecl)
2425 : 0 : || !lookup_attribute ("gnu_inline",
2426 : 0 : DECL_ATTRIBUTES (newdecl)))))
2427 : : {
2428 : 26 : auto_diagnostic_group d;
2429 : 26 : error ("redefinition of %q+D", newdecl);
2430 : 26 : locate_old_decl (olddecl);
2431 : 26 : return false;
2432 : 26 : }
2433 : : }
2434 : : }
2435 : : /* If we have a prototype after an old-style function definition,
2436 : : the argument types must be checked specially. */
2437 : 4361662 : else if (DECL_INITIAL (olddecl)
2438 : 805 : && !prototype_p (oldtype) && prototype_p (newtype)
2439 : 4361682 : && TYPE_ACTUAL_ARG_TYPES (oldtype))
2440 : : {
2441 : 19 : auto_diagnostic_group d;
2442 : 19 : if (!validate_proto_after_old_defn (newdecl, newtype, oldtype))
2443 : : {
2444 : 10 : locate_old_decl (olddecl);
2445 : 10 : return false;
2446 : : }
2447 : 19 : }
2448 : : /* A non-static declaration (even an "extern") followed by a
2449 : : static declaration is undefined behavior per C99 6.2.2p3-5,7.
2450 : : The same is true for a static forward declaration at block
2451 : : scope followed by a non-static declaration/definition at file
2452 : : scope. Static followed by non-static at the same scope is
2453 : : not undefined behavior, and is the most convenient way to get
2454 : : some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2455 : : the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2456 : : we do diagnose it if -Wtraditional. */
2457 : 4590003 : if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2458 : : {
2459 : : /* Two exceptions to the rule. If olddecl is an extern
2460 : : inline, or a predeclared function that isn't actually
2461 : : built in, newdecl silently overrides olddecl. The latter
2462 : : occur only in Objective C; see also above. (FIXME: Make
2463 : : Objective C use normal builtins.) */
2464 : 18 : if (!DECL_IS_UNDECLARED_BUILTIN (olddecl)
2465 : 36 : && !DECL_EXTERN_INLINE (olddecl))
2466 : : {
2467 : 4 : auto_diagnostic_group d;
2468 : 4 : error ("static declaration of %q+D follows "
2469 : : "non-static declaration", newdecl);
2470 : 4 : locate_old_decl (olddecl);
2471 : 4 : }
2472 : 18 : return false;
2473 : : }
2474 : 4589985 : else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2475 : : {
2476 : 2099 : if (DECL_CONTEXT (olddecl))
2477 : : {
2478 : 0 : auto_diagnostic_group d;
2479 : 0 : error ("non-static declaration of %q+D follows "
2480 : : "static declaration", newdecl);
2481 : 0 : locate_old_decl (olddecl);
2482 : 0 : return false;
2483 : 0 : }
2484 : 2099 : else if (warn_traditional)
2485 : : {
2486 : 2 : warned |= warning (OPT_Wtraditional,
2487 : : "non-static declaration of %q+D "
2488 : : "follows static declaration", newdecl);
2489 : : }
2490 : : }
2491 : :
2492 : : /* Make sure gnu_inline attribute is either not present, or
2493 : : present on all inline decls. */
2494 : 4589985 : if (DECL_DECLARED_INLINE_P (olddecl)
2495 : 4591048 : && DECL_DECLARED_INLINE_P (newdecl))
2496 : : {
2497 : 808 : bool newa = lookup_attribute ("gnu_inline",
2498 : 808 : DECL_ATTRIBUTES (newdecl)) != NULL;
2499 : 808 : bool olda = lookup_attribute ("gnu_inline",
2500 : 808 : DECL_ATTRIBUTES (olddecl)) != NULL;
2501 : 808 : if (newa != olda)
2502 : : {
2503 : 0 : auto_diagnostic_group d;
2504 : 0 : error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2505 : : newa ? newdecl : olddecl);
2506 : 0 : error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2507 : : "but not here");
2508 : 0 : }
2509 : : }
2510 : : }
2511 : 18424 : else if (VAR_P (newdecl))
2512 : : {
2513 : : /* Only variables can be thread-local, and all declarations must
2514 : : agree on this property. */
2515 : 18393 : if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2516 : : {
2517 : : /* Nothing to check. Since OLDDECL is marked threadprivate
2518 : : and NEWDECL does not have a thread-local attribute, we
2519 : : will merge the threadprivate attribute into NEWDECL. */
2520 : : ;
2521 : : }
2522 : 54966 : else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2523 : : {
2524 : 6 : auto_diagnostic_group d;
2525 : 6 : if (DECL_THREAD_LOCAL_P (newdecl))
2526 : 3 : error ("thread-local declaration of %q+D follows "
2527 : : "non-thread-local declaration", newdecl);
2528 : : else
2529 : 3 : error ("non-thread-local declaration of %q+D follows "
2530 : : "thread-local declaration", newdecl);
2531 : :
2532 : 6 : locate_old_decl (olddecl);
2533 : 6 : return false;
2534 : 6 : }
2535 : :
2536 : : /* Multiple initialized definitions are not allowed (6.9p3,5).
2537 : : For this purpose, C23 makes it clear that thread-local
2538 : : declarations without extern are definitions, not tentative
2539 : : definitions, whether or not they have initializers. The
2540 : : wording before C23 was unclear; literally it would have made
2541 : : uninitialized thread-local declarations into tentative
2542 : : definitions only if they also used static, but without saying
2543 : : explicitly whether or not other cases count as
2544 : : definitions at all. */
2545 : 18919 : if ((DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2546 : 18918 : || (flag_isoc23
2547 : 6237 : && DECL_THREAD_LOCAL_P (newdecl)
2548 : 25 : && !DECL_EXTERNAL (newdecl)
2549 : 21 : && !DECL_EXTERNAL (olddecl)))
2550 : : {
2551 : 7 : auto_diagnostic_group d;
2552 : 7 : error ("redefinition of %q+D", newdecl);
2553 : 7 : locate_old_decl (olddecl);
2554 : 7 : return false;
2555 : 7 : }
2556 : :
2557 : : /* Objects declared at file scope: if the first declaration had
2558 : : external linkage (even if it was an external reference) the
2559 : : second must have external linkage as well, or the behavior is
2560 : : undefined. If the first declaration had internal linkage, then
2561 : : the second must too, or else be an external reference (in which
2562 : : case the composite declaration still has internal linkage).
2563 : : As for function declarations, we warn about the static-then-
2564 : : extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2565 : 18397 : if (DECL_FILE_SCOPE_P (newdecl)
2566 : 18380 : && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2567 : : {
2568 : 141 : if (DECL_EXTERNAL (newdecl))
2569 : : {
2570 : 139 : if (!DECL_FILE_SCOPE_P (olddecl))
2571 : : {
2572 : 2 : auto_diagnostic_group d;
2573 : 2 : error ("extern declaration of %q+D follows "
2574 : : "declaration with no linkage", newdecl);
2575 : 2 : locate_old_decl (olddecl);
2576 : 2 : return false;
2577 : 2 : }
2578 : 137 : else if (warn_traditional)
2579 : : {
2580 : 0 : warned |= warning (OPT_Wtraditional,
2581 : : "non-static declaration of %q+D "
2582 : : "follows static declaration", newdecl);
2583 : : }
2584 : : }
2585 : : else
2586 : : {
2587 : 2 : auto_diagnostic_group d;
2588 : 2 : if (TREE_PUBLIC (newdecl))
2589 : 2 : error ("non-static declaration of %q+D follows "
2590 : : "static declaration", newdecl);
2591 : : else
2592 : 0 : error ("static declaration of %q+D follows "
2593 : : "non-static declaration", newdecl);
2594 : :
2595 : 2 : locate_old_decl (olddecl);
2596 : 2 : return false;
2597 : 2 : }
2598 : : }
2599 : : /* Two objects with the same name declared at the same block
2600 : : scope must both be external references (6.7p3). */
2601 : 18239 : else if (!DECL_FILE_SCOPE_P (newdecl))
2602 : : {
2603 : 17 : if (DECL_EXTERNAL (newdecl))
2604 : : {
2605 : : /* Extern with initializer at block scope, which will
2606 : : already have received an error. */
2607 : : }
2608 : 15 : else if (DECL_EXTERNAL (olddecl))
2609 : : {
2610 : 4 : auto_diagnostic_group d;
2611 : 4 : error ("declaration of %q+D with no linkage follows "
2612 : : "extern declaration", newdecl);
2613 : 4 : locate_old_decl (olddecl);
2614 : 4 : }
2615 : : else
2616 : : {
2617 : 11 : auto_diagnostic_group d;
2618 : 11 : error ("redeclaration of %q+D with no linkage", newdecl);
2619 : 11 : locate_old_decl (olddecl);
2620 : 11 : }
2621 : :
2622 : 17 : return false;
2623 : : }
2624 : :
2625 : : /* C++ does not permit a decl to appear multiple times at file
2626 : : scope. */
2627 : 18359 : if (warn_cxx_compat
2628 : 68 : && DECL_FILE_SCOPE_P (newdecl)
2629 : 68 : && !DECL_EXTERNAL (newdecl)
2630 : 18377 : && !DECL_EXTERNAL (olddecl))
2631 : 5 : warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2632 : 5 : OPT_Wc___compat,
2633 : : ("duplicate declaration of %qD is "
2634 : : "invalid in C++"),
2635 : : newdecl);
2636 : : }
2637 : :
2638 : : /* warnings */
2639 : : /* All decls must agree on a visibility. */
2640 : 4608375 : if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2641 : 4608344 : && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2642 : 4609110 : && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2643 : : {
2644 : 1 : warned |= warning (0, "redeclaration of %q+D with different visibility "
2645 : : "(old visibility preserved)", newdecl);
2646 : : }
2647 : :
2648 : 4608375 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2649 : 4589985 : warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2650 : : else /* PARM_DECL, VAR_DECL */
2651 : : {
2652 : : /* Redeclaration of a parameter is a constraint violation (this is
2653 : : not explicitly stated, but follows from C99 6.7p3 [no more than
2654 : : one declaration of the same identifier with no linkage in the
2655 : : same scope, except type tags] and 6.2.2p6 [parameters have no
2656 : : linkage]). We must check for a forward parameter declaration,
2657 : : indicated by TREE_ASM_WRITTEN on the old declaration - this is
2658 : : an extension, the mandatory diagnostic for which is handled by
2659 : : mark_forward_parm_decls. */
2660 : :
2661 : 18390 : if (TREE_CODE (newdecl) == PARM_DECL
2662 : 31 : && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2663 : : {
2664 : 2 : auto_diagnostic_group d;
2665 : 2 : error ("redefinition of parameter %q+D", newdecl);
2666 : 2 : locate_old_decl (olddecl);
2667 : 2 : return false;
2668 : 2 : }
2669 : : }
2670 : :
2671 : : /* Optional warning for completely redundant decls. */
2672 : 4608373 : if (!warned && !pedwarned
2673 : 4608299 : && warn_redundant_decls
2674 : : /* Don't warn about a function declaration followed by a
2675 : : definition. */
2676 : 18 : && !(TREE_CODE (newdecl) == FUNCTION_DECL
2677 : 2 : && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2678 : : /* Don't warn about redundant redeclarations of builtins. */
2679 : 18 : && !(TREE_CODE (newdecl) == FUNCTION_DECL
2680 : 2 : && !fndecl_built_in_p (newdecl)
2681 : 2 : && fndecl_built_in_p (olddecl)
2682 : 2 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2683 : : /* Don't warn about an extern followed by a definition. */
2684 : 17 : && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2685 : : /* Don't warn about forward parameter decls. */
2686 : 17 : && !(TREE_CODE (newdecl) == PARM_DECL
2687 : 6 : && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2688 : : /* Don't warn about a variable definition following a declaration. */
2689 : 4608384 : && !(VAR_P (newdecl)
2690 : 10 : && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2691 : : {
2692 : 6 : warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2693 : : newdecl);
2694 : : }
2695 : :
2696 : : /* Report location of previous decl/defn. */
2697 : 4608373 : if (warned || pedwarned)
2698 : 80 : locate_old_decl (olddecl);
2699 : :
2700 : : #undef DECL_EXTERN_INLINE
2701 : :
2702 : : return retval;
2703 : 4653832 : }
2704 : :
2705 : : /* Subroutine of duplicate_decls. NEWDECL has been found to be
2706 : : consistent with OLDDECL, but carries new information. Merge the
2707 : : new information into OLDDECL. This function issues no
2708 : : diagnostics. */
2709 : :
2710 : : static void
2711 : 4652843 : merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2712 : : {
2713 : 4652843 : bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2714 : 4652843 : && DECL_INITIAL (newdecl) != NULL_TREE);
2715 : 4652843 : bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2716 : 4652843 : && prototype_p (TREE_TYPE (newdecl)));
2717 : 4652843 : bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2718 : 4652843 : && prototype_p (TREE_TYPE (olddecl)));
2719 : :
2720 : : /* For real parm decl following a forward decl, rechain the old decl
2721 : : in its new location and clear TREE_ASM_WRITTEN (it's not a
2722 : : forward decl anymore). */
2723 : 4652843 : if (TREE_CODE (newdecl) == PARM_DECL
2724 : 29 : && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2725 : : {
2726 : 29 : struct c_binding *b, **here;
2727 : :
2728 : 44 : for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
2729 : 44 : if ((*here)->decl == olddecl)
2730 : 29 : goto found;
2731 : 0 : gcc_unreachable ();
2732 : :
2733 : 29 : found:
2734 : 29 : b = *here;
2735 : 29 : *here = b->prev;
2736 : 29 : b->prev = current_scope->bindings;
2737 : 29 : current_scope->bindings = b;
2738 : :
2739 : 29 : TREE_ASM_WRITTEN (olddecl) = 0;
2740 : : }
2741 : :
2742 : 4652843 : DECL_ATTRIBUTES (newdecl)
2743 : 4652843 : = targetm.merge_decl_attributes (olddecl, newdecl);
2744 : :
2745 : : /* For typedefs use the old type, as the new type's DECL_NAME points
2746 : : at newdecl, which will be ggc_freed. */
2747 : 4652843 : if (TREE_CODE (newdecl) == TYPE_DECL)
2748 : : {
2749 : : /* But NEWTYPE might have an attribute, honor that. */
2750 : 44470 : tree tem = newtype;
2751 : 44470 : newtype = oldtype;
2752 : :
2753 : 44470 : if (TYPE_USER_ALIGN (tem))
2754 : : {
2755 : 12 : if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2756 : 6 : SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2757 : 12 : TYPE_USER_ALIGN (newtype) = true;
2758 : : }
2759 : :
2760 : : /* And remove the new type from the variants list. */
2761 : 44470 : if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2762 : : {
2763 : 12 : tree remove = TREE_TYPE (newdecl);
2764 : 12 : if (TYPE_MAIN_VARIANT (remove) == remove)
2765 : : {
2766 : 2 : gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2767 : : /* If remove is the main variant, no need to remove that
2768 : : from the list. One of the DECL_ORIGINAL_TYPE
2769 : : variants, e.g. created for aligned attribute, might still
2770 : : refer to the newdecl TYPE_DECL though, so remove that one
2771 : : in that case. */
2772 : 2 : if (DECL_ORIGINAL_TYPE (newdecl)
2773 : 2 : && DECL_ORIGINAL_TYPE (newdecl) != remove)
2774 : 2 : for (tree t = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl));
2775 : 2 : t; t = TYPE_MAIN_VARIANT (t))
2776 : 2 : if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2777 : : {
2778 : 4 : TYPE_NEXT_VARIANT (t)
2779 : 2 : = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2780 : 2 : break;
2781 : : }
2782 : : }
2783 : : else
2784 : 10 : for (tree t = TYPE_MAIN_VARIANT (remove); ;
2785 : 0 : t = TYPE_NEXT_VARIANT (t))
2786 : 10 : if (TYPE_NEXT_VARIANT (t) == remove)
2787 : : {
2788 : 10 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2789 : 10 : break;
2790 : : }
2791 : : }
2792 : :
2793 : : /* Make sure we refer to the same type as the olddecl. */
2794 : 44470 : DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2795 : : }
2796 : :
2797 : : /* Merge the data types specified in the two decls. */
2798 : 9305686 : TREE_TYPE (newdecl)
2799 : 4652843 : = TREE_TYPE (olddecl)
2800 : 9305686 : = composite_type (newtype, oldtype);
2801 : :
2802 : : /* Lay the type out, unless already done. */
2803 : 4652843 : if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2804 : : {
2805 : 0 : if (TREE_TYPE (newdecl) != error_mark_node)
2806 : 0 : layout_type (TREE_TYPE (newdecl));
2807 : 0 : if (TREE_CODE (newdecl) != FUNCTION_DECL
2808 : : && TREE_CODE (newdecl) != TYPE_DECL
2809 : : && TREE_CODE (newdecl) != CONST_DECL)
2810 : 0 : layout_decl (newdecl, 0);
2811 : : }
2812 : : else
2813 : : {
2814 : : /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2815 : 4652843 : DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2816 : 4652843 : DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2817 : 4652843 : SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2818 : 4652843 : if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2819 : : {
2820 : 44 : SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2821 : 44 : DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2822 : : }
2823 : 4652799 : else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2824 : 4652799 : && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2825 : 3 : DECL_USER_ALIGN (newdecl) = 1;
2826 : 9305686 : if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2827 : 4652843 : > DECL_WARN_IF_NOT_ALIGN (newdecl))
2828 : 0 : SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2829 : : DECL_WARN_IF_NOT_ALIGN (olddecl));
2830 : : }
2831 : :
2832 : : /* Keep the old rtl since we can safely use it. */
2833 : 4652843 : if (HAS_RTL_P (olddecl))
2834 : 4652843 : COPY_DECL_RTL (olddecl, newdecl);
2835 : :
2836 : : /* Merge the type qualifiers. */
2837 : 4652843 : if (TREE_READONLY (newdecl))
2838 : 375237 : TREE_READONLY (olddecl) = 1;
2839 : :
2840 : 4652843 : if (TREE_THIS_VOLATILE (newdecl))
2841 : 48437 : TREE_THIS_VOLATILE (olddecl) = 1;
2842 : :
2843 : : /* Merge deprecatedness. */
2844 : 4652843 : if (TREE_DEPRECATED (newdecl))
2845 : 331 : TREE_DEPRECATED (olddecl) = 1;
2846 : :
2847 : : /* Merge unavailability. */
2848 : 4652843 : if (TREE_UNAVAILABLE (newdecl))
2849 : 2 : TREE_UNAVAILABLE (olddecl) = 1;
2850 : :
2851 : : /* If a decl is in a system header and the other isn't, keep the one on the
2852 : : system header. Otherwise, keep source location of definition rather than
2853 : : declaration and of prototype rather than non-prototype unless that
2854 : : prototype is built-in. */
2855 : 4652843 : if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2856 : 4652814 : && DECL_IN_SYSTEM_HEADER (olddecl)
2857 : 5086099 : && !DECL_IN_SYSTEM_HEADER (newdecl) )
2858 : 1031 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2859 : 4651812 : else if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2860 : 4651783 : && DECL_IN_SYSTEM_HEADER (newdecl)
2861 : 8580642 : && !DECL_IN_SYSTEM_HEADER (olddecl))
2862 : 3496605 : DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2863 : 1155207 : else if ((DECL_INITIAL (newdecl) == NULL_TREE
2864 : 926399 : && DECL_INITIAL (olddecl) != NULL_TREE)
2865 : 2080703 : || (old_is_prototype && !new_is_prototype
2866 : 372 : && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2867 : 931 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2868 : :
2869 : : /* Merge the initialization information. */
2870 : 4652843 : if (DECL_INITIAL (newdecl) == NULL_TREE)
2871 : 4423963 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2872 : :
2873 : : /* Merge 'constexpr' information. */
2874 : 4652843 : if (VAR_P (olddecl) && VAR_P (newdecl))
2875 : : {
2876 : 18359 : if (C_DECL_DECLARED_CONSTEXPR (olddecl))
2877 : 2 : C_DECL_DECLARED_CONSTEXPR (newdecl) = 1;
2878 : 18357 : else if (C_DECL_DECLARED_CONSTEXPR (newdecl))
2879 : 1 : C_DECL_DECLARED_CONSTEXPR (olddecl) = 1;
2880 : : }
2881 : :
2882 : : /* Merge the threadprivate attribute. */
2883 : 4652843 : if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2884 : 7 : C_DECL_THREADPRIVATE_P (newdecl) = 1;
2885 : :
2886 : 4652843 : if (HAS_DECL_ASSEMBLER_NAME_P (olddecl))
2887 : : {
2888 : : /* Copy the assembler name.
2889 : : Currently, it can only be defined in the prototype. */
2890 : 4652814 : COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2891 : :
2892 : : /* Use visibility of whichever declaration had it specified */
2893 : 4652814 : if (DECL_VISIBILITY_SPECIFIED (olddecl))
2894 : : {
2895 : 4737 : DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2896 : 4737 : DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2897 : : }
2898 : :
2899 : 4652814 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2900 : : {
2901 : 4589985 : DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2902 : 4589985 : DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2903 : 4589985 : DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2904 : 4589985 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2905 : 4589985 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2906 : 4589985 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2907 : 4589985 : DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2908 : 4589985 : if (DECL_IS_OPERATOR_NEW_P (olddecl))
2909 : 0 : DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2910 : 4589985 : if (DECL_IS_OPERATOR_DELETE_P (olddecl))
2911 : 0 : DECL_SET_IS_OPERATOR_DELETE (newdecl, true);
2912 : 4589985 : TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2913 : 4589985 : DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2914 : 4589985 : DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2915 : : }
2916 : :
2917 : : /* Merge the storage class information. */
2918 : 4652814 : merge_weak (newdecl, olddecl);
2919 : :
2920 : : /* For functions, static overrides non-static. */
2921 : 4652814 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2922 : : {
2923 : 4589985 : TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2924 : : /* This is since we don't automatically
2925 : : copy the attributes of NEWDECL into OLDDECL. */
2926 : 4589985 : TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2927 : : /* If this clears `static', clear it in the identifier too. */
2928 : 4589985 : if (!TREE_PUBLIC (olddecl))
2929 : 7501 : TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2930 : : }
2931 : : }
2932 : :
2933 : : /* In c99, 'extern' declaration before (or after) 'inline' means this
2934 : : function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2935 : : is present. */
2936 : 4652843 : if (TREE_CODE (newdecl) == FUNCTION_DECL
2937 : 4589985 : && !flag_gnu89_inline
2938 : 4566290 : && (DECL_DECLARED_INLINE_P (newdecl)
2939 : 4379673 : || DECL_DECLARED_INLINE_P (olddecl))
2940 : 186811 : && (!DECL_DECLARED_INLINE_P (newdecl)
2941 : 186617 : || !DECL_DECLARED_INLINE_P (olddecl)
2942 : 795 : || !DECL_EXTERNAL (olddecl))
2943 : 186034 : && DECL_EXTERNAL (newdecl)
2944 : 185826 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2945 : 4652941 : && !current_function_decl)
2946 : 81 : DECL_EXTERNAL (newdecl) = 0;
2947 : :
2948 : : /* An inline definition following a static declaration is not
2949 : : DECL_EXTERNAL. */
2950 : 4652843 : if (new_is_definition
2951 : 228334 : && (DECL_DECLARED_INLINE_P (newdecl)
2952 : 41495 : || DECL_DECLARED_INLINE_P (olddecl))
2953 : 4839896 : && !TREE_PUBLIC (olddecl))
2954 : 888 : DECL_EXTERNAL (newdecl) = 0;
2955 : :
2956 : 4652843 : if (DECL_EXTERNAL (newdecl))
2957 : : {
2958 : 4563735 : TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2959 : 4563735 : DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2960 : :
2961 : : /* An extern decl does not override previous storage class. */
2962 : 4563735 : TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2963 : 4563735 : if (!DECL_EXTERNAL (newdecl))
2964 : : {
2965 : 1408 : DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2966 : 1408 : DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2967 : : }
2968 : : }
2969 : : else
2970 : : {
2971 : 89108 : TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2972 : 89108 : TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2973 : : }
2974 : :
2975 : 4652843 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2976 : : {
2977 : : /* If we're redefining a function previously defined as extern
2978 : : inline, make sure we emit debug info for the inline before we
2979 : : throw it away, in case it was inlined into a function that
2980 : : hasn't been written out yet. */
2981 : 4589985 : if (new_is_definition && DECL_INITIAL (olddecl))
2982 : : /* The new defn must not be inline. */
2983 : 75 : DECL_UNINLINABLE (newdecl) = 1;
2984 : : else
2985 : : {
2986 : : /* If either decl says `inline', this fn is inline, unless
2987 : : its definition was passed already. */
2988 : 4589910 : if (DECL_DECLARED_INLINE_P (newdecl)
2989 : 8992929 : || DECL_DECLARED_INLINE_P (olddecl))
2990 : 187076 : DECL_DECLARED_INLINE_P (newdecl) = 1;
2991 : :
2992 : 13769730 : DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2993 : 9181355 : = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2994 : :
2995 : 9179820 : DECL_DISREGARD_INLINE_LIMITS (newdecl)
2996 : 4589910 : = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2997 : 4589910 : = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2998 : 9179682 : || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2999 : : }
3000 : :
3001 : 4589985 : if (fndecl_built_in_p (olddecl))
3002 : : {
3003 : : /* If redeclaring a builtin function, it stays built in.
3004 : : But it gets tagged as having been declared. */
3005 : 3683628 : copy_decl_built_in_function (newdecl, olddecl);
3006 : 3683628 : C_DECL_DECLARED_BUILTIN (newdecl) = 1;
3007 : 3683628 : if (new_is_prototype)
3008 : : {
3009 : 3683269 : C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
3010 : 3683269 : if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3011 : : {
3012 : 3683269 : enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3013 : 3683269 : switch (fncode)
3014 : : {
3015 : : /* If a compatible prototype of these builtin functions
3016 : : is seen, assume the runtime implements it with the
3017 : : expected semantics. */
3018 : 6423 : case BUILT_IN_STPCPY:
3019 : 6423 : if (builtin_decl_explicit_p (fncode))
3020 : 6423 : set_builtin_decl_implicit_p (fncode, true);
3021 : : break;
3022 : 3676846 : default:
3023 : 3676846 : if (builtin_decl_explicit_p (fncode))
3024 : 3676846 : set_builtin_decl_declared_p (fncode, true);
3025 : : break;
3026 : : }
3027 : :
3028 : 3683269 : copy_attributes_to_builtin (newdecl);
3029 : : }
3030 : : }
3031 : : else
3032 : 718 : C_DECL_BUILTIN_PROTOTYPE (newdecl)
3033 : 718 : = C_DECL_BUILTIN_PROTOTYPE (olddecl);
3034 : : }
3035 : :
3036 : : /* Preserve function specific target and optimization options */
3037 : 4589985 : if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
3038 : 4590490 : && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
3039 : 469 : DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
3040 : 469 : = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
3041 : :
3042 : 4589985 : if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
3043 : 4606159 : && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
3044 : 9 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
3045 : 9 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
3046 : :
3047 : : /* Also preserve various other info from the definition. */
3048 : 4589985 : if (!new_is_definition)
3049 : : {
3050 : 4361651 : tree t;
3051 : 4361651 : DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3052 : 4361651 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3053 : 4361651 : DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
3054 : 4361651 : DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3055 : 4361651 : DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
3056 : 6676096 : for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
3057 : 2314445 : DECL_CONTEXT (t) = newdecl;
3058 : :
3059 : : /* See if we've got a function to instantiate from. */
3060 : 4361651 : if (DECL_SAVED_TREE (olddecl))
3061 : 1582 : DECL_ABSTRACT_ORIGIN (newdecl)
3062 : 791 : = DECL_ABSTRACT_ORIGIN (olddecl);
3063 : : }
3064 : : }
3065 : :
3066 : : /* Merge the USED information. */
3067 : 4652843 : if (TREE_USED (olddecl))
3068 : 702815 : TREE_USED (newdecl) = 1;
3069 : 3950028 : else if (TREE_USED (newdecl))
3070 : 4 : TREE_USED (olddecl) = 1;
3071 : 4652843 : if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
3072 : 18388 : DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
3073 : 4652843 : if (DECL_PRESERVE_P (olddecl))
3074 : 25 : DECL_PRESERVE_P (newdecl) = 1;
3075 : 4652818 : else if (DECL_PRESERVE_P (newdecl))
3076 : 4 : DECL_PRESERVE_P (olddecl) = 1;
3077 : :
3078 : : /* Merge DECL_COMMON */
3079 : 18359 : if (VAR_P (olddecl) && VAR_P (newdecl)
3080 : 18359 : && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
3081 : 4671200 : && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
3082 : 36710 : DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
3083 : :
3084 : : /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
3085 : : But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
3086 : : DECL_ARGUMENTS (if appropriate). */
3087 : 4652843 : {
3088 : 4652843 : unsigned olddecl_uid = DECL_UID (olddecl);
3089 : 4652843 : tree olddecl_context = DECL_CONTEXT (olddecl);
3090 : 4652843 : tree olddecl_arguments = NULL;
3091 : 4652843 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3092 : 4589985 : olddecl_arguments = DECL_ARGUMENTS (olddecl);
3093 : :
3094 : 4652843 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3095 : : (char *) newdecl + sizeof (struct tree_common),
3096 : : sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3097 : 4652843 : DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3098 : 4652843 : switch (TREE_CODE (olddecl))
3099 : : {
3100 : 4608344 : case FUNCTION_DECL:
3101 : 4608344 : case VAR_DECL:
3102 : 4608344 : {
3103 : 4608344 : struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
3104 : :
3105 : 9216688 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3106 : : (char *) newdecl + sizeof (struct tree_decl_common),
3107 : 4608344 : tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3108 : 4608344 : olddecl->decl_with_vis.symtab_node = snode;
3109 : :
3110 : 4608344 : if ((DECL_EXTERNAL (olddecl)
3111 : 46017 : || TREE_PUBLIC (olddecl)
3112 : 7763 : || TREE_STATIC (olddecl))
3113 : 4654354 : && DECL_SECTION_NAME (newdecl) != NULL)
3114 : 8 : set_decl_section_name (olddecl, newdecl);
3115 : :
3116 : : /* This isn't quite correct for something like
3117 : : int __thread x attribute ((tls_model ("local-exec")));
3118 : : extern int __thread x;
3119 : : as we'll lose the "local-exec" model. */
3120 : 4608344 : if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
3121 : 89 : set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3122 : : break;
3123 : : }
3124 : :
3125 : 44499 : case FIELD_DECL:
3126 : 44499 : case PARM_DECL:
3127 : 44499 : case LABEL_DECL:
3128 : 44499 : case RESULT_DECL:
3129 : 44499 : case CONST_DECL:
3130 : 44499 : case TYPE_DECL:
3131 : 88998 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3132 : : (char *) newdecl + sizeof (struct tree_decl_common),
3133 : 44499 : tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3134 : 44499 : break;
3135 : :
3136 : 0 : default:
3137 : :
3138 : 0 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3139 : : (char *) newdecl + sizeof (struct tree_decl_common),
3140 : : sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
3141 : : }
3142 : 4652843 : DECL_UID (olddecl) = olddecl_uid;
3143 : 4652843 : DECL_CONTEXT (olddecl) = olddecl_context;
3144 : 4652843 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3145 : 4589985 : DECL_ARGUMENTS (olddecl) = olddecl_arguments;
3146 : : }
3147 : :
3148 : : /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3149 : : so that encode_section_info has a chance to look at the new decl
3150 : : flags and attributes. */
3151 : 4652843 : if (DECL_RTL_SET_P (olddecl)
3152 : 4652843 : && (TREE_CODE (olddecl) == FUNCTION_DECL
3153 : 0 : || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
3154 : 0 : make_decl_rtl (olddecl);
3155 : 4652843 : }
3156 : :
3157 : : /* Handle when a new declaration NEWDECL has the same name as an old
3158 : : one OLDDECL in the same binding contour. Prints an error message
3159 : : if appropriate.
3160 : :
3161 : : If safely possible, alter OLDDECL to look like NEWDECL, and return
3162 : : true. Otherwise, return false. */
3163 : :
3164 : : static bool
3165 : 4653934 : duplicate_decls (tree newdecl, tree olddecl)
3166 : : {
3167 : 4653934 : tree newtype = NULL, oldtype = NULL;
3168 : :
3169 : 4653934 : if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
3170 : : {
3171 : : /* Avoid `unused variable' and other warnings for OLDDECL. */
3172 : 1091 : suppress_warning (olddecl, OPT_Wunused);
3173 : : /* If the types are completely different, poison them both with
3174 : : error_mark_node. */
3175 : 1091 : if (TREE_CODE (TREE_TYPE (newdecl)) != TREE_CODE (TREE_TYPE (olddecl))
3176 : 112 : && olddecl != error_mark_node
3177 : 1184 : && seen_error ())
3178 : : {
3179 : 59 : if (TREE_CODE (olddecl) != FUNCTION_DECL)
3180 : 47 : TREE_TYPE (olddecl) = error_mark_node;
3181 : 59 : if (TREE_CODE (newdecl) != FUNCTION_DECL)
3182 : 56 : TREE_TYPE (newdecl) = error_mark_node;
3183 : : }
3184 : 1091 : return false;
3185 : : }
3186 : :
3187 : 4652843 : merge_decls (newdecl, olddecl, newtype, oldtype);
3188 : :
3189 : : /* The NEWDECL will no longer be needed.
3190 : :
3191 : : Before releasing the node, be sure to remove function from symbol
3192 : : table that might have been inserted there to record comdat group.
3193 : : Be sure to however do not free DECL_STRUCT_FUNCTION because this
3194 : : structure is shared in between NEWDECL and OLDECL. */
3195 : 4652843 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3196 : 4589985 : DECL_STRUCT_FUNCTION (newdecl) = NULL;
3197 : 4652843 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3198 : : {
3199 : 4608344 : struct symtab_node *snode = symtab_node::get (newdecl);
3200 : 4608344 : if (snode)
3201 : 104 : snode->remove ();
3202 : : }
3203 : 4652843 : ggc_free (newdecl);
3204 : 4652843 : return true;
3205 : : }
3206 : :
3207 : :
3208 : : /* Check whether decl-node NEW_DECL shadows an existing declaration. */
3209 : : static void
3210 : 165943005 : warn_if_shadowing (tree new_decl)
3211 : : {
3212 : 165943005 : struct c_binding *b;
3213 : :
3214 : : /* Shadow warnings wanted? */
3215 : 331885094 : if (!(warn_shadow
3216 : 165942269 : || warn_shadow_local
3217 : 165942089 : || warn_shadow_compatible_local)
3218 : : /* No shadow warnings for internally generated vars. */
3219 : 165943248 : || DECL_IS_UNDECLARED_BUILTIN (new_decl))
3220 : : return;
3221 : :
3222 : : /* Is anything being shadowed? Invisible decls do not count. */
3223 : 253 : for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
3224 : 157 : if (b->decl && b->decl != new_decl && !b->invisible
3225 : 214 : && (b->decl == error_mark_node
3226 : 43 : || diagnostic_report_warnings_p (global_dc,
3227 : : DECL_SOURCE_LOCATION (b->decl))))
3228 : : {
3229 : 57 : tree old_decl = b->decl;
3230 : :
3231 : 57 : if (old_decl == error_mark_node)
3232 : : {
3233 : 14 : warning (OPT_Wshadow, "declaration of %q+D shadows previous "
3234 : : "non-variable", new_decl);
3235 : 50 : break;
3236 : : }
3237 : :
3238 : 43 : bool warned = false;
3239 : 43 : auto_diagnostic_group d;
3240 : 43 : if (TREE_CODE (old_decl) == PARM_DECL)
3241 : : {
3242 : 5 : enum opt_code warning_code;
3243 : :
3244 : : /* If '-Wshadow=compatible-local' is specified without other
3245 : : -Wshadow= flags, we will warn only when the types of the
3246 : : shadowing variable (i.e. new_decl) and the shadowed variable
3247 : : (old_decl) are compatible. */
3248 : 5 : if (warn_shadow)
3249 : : warning_code = OPT_Wshadow;
3250 : 2 : else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3251 : : warning_code = OPT_Wshadow_compatible_local;
3252 : : else
3253 : 2 : warning_code = OPT_Wshadow_local;
3254 : 5 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3255 : : "declaration of %qD shadows a parameter",
3256 : : new_decl);
3257 : : }
3258 : 38 : else if (DECL_FILE_SCOPE_P (old_decl))
3259 : : {
3260 : : /* Do not warn if a variable shadows a function, unless
3261 : : the variable is a function or a pointer-to-function. */
3262 : 34 : if (TREE_CODE (old_decl) == FUNCTION_DECL
3263 : 11 : && TREE_CODE (new_decl) != FUNCTION_DECL
3264 : 36 : && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
3265 : 7 : continue;
3266 : :
3267 : 20 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
3268 : : "declaration of %qD shadows a global "
3269 : : "declaration",
3270 : : new_decl);
3271 : : }
3272 : 11 : else if (TREE_CODE (old_decl) == FUNCTION_DECL
3273 : 11 : && fndecl_built_in_p (old_decl))
3274 : : {
3275 : 0 : warning (OPT_Wshadow, "declaration of %q+D shadows "
3276 : : "a built-in function", new_decl);
3277 : 0 : break;
3278 : : }
3279 : : else
3280 : : {
3281 : 11 : enum opt_code warning_code;
3282 : :
3283 : : /* If '-Wshadow=compatible-local' is specified without other
3284 : : -Wshadow= flags, we will warn only when the types of the
3285 : : shadowing variable (i.e. new_decl) and the shadowed variable
3286 : : (old_decl) are compatible. */
3287 : 11 : if (warn_shadow)
3288 : : warning_code = OPT_Wshadow;
3289 : 8 : else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3290 : : warning_code = OPT_Wshadow_compatible_local;
3291 : : else
3292 : 2 : warning_code = OPT_Wshadow_local;
3293 : 11 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3294 : : "declaration of %qD shadows a previous local",
3295 : : new_decl);
3296 : : }
3297 : :
3298 : 36 : if (warned)
3299 : 26 : inform (DECL_SOURCE_LOCATION (old_decl),
3300 : : "shadowed declaration is here");
3301 : :
3302 : : break;
3303 : 43 : }
3304 : : }
3305 : :
3306 : : /* Record a decl-node X as belonging to the current lexical scope.
3307 : : Check for errors (such as an incompatible declaration for the same
3308 : : name already seen in the same scope).
3309 : :
3310 : : Returns either X or an old decl for the same name.
3311 : : If an old decl is returned, it may have been smashed
3312 : : to agree with what X says. */
3313 : :
3314 : : tree
3315 : 196550574 : pushdecl (tree x)
3316 : : {
3317 : 196550574 : tree name = DECL_NAME (x);
3318 : 196550574 : struct c_scope *scope = current_scope;
3319 : 196550574 : struct c_binding *b;
3320 : 196550574 : bool nested = false;
3321 : 196550574 : location_t locus = DECL_SOURCE_LOCATION (x);
3322 : :
3323 : : /* Must set DECL_CONTEXT for everything not at file scope or
3324 : : DECL_FILE_SCOPE_P won't work. Local externs don't count
3325 : : unless they have initializers (which generate code). We
3326 : : also exclude CONST_DECLs because enumerators will get the
3327 : : type of the enum as context. */
3328 : 196550574 : if (current_function_decl
3329 : 8932317 : && TREE_CODE (x) != CONST_DECL
3330 : 205329699 : && (!VAR_OR_FUNCTION_DECL_P (x)
3331 : 8503327 : || DECL_INITIAL (x) || !TREE_PUBLIC (x)))
3332 : 8767123 : DECL_CONTEXT (x) = current_function_decl;
3333 : :
3334 : : /* Anonymous decls are just inserted in the scope. */
3335 : 196550574 : if (!name)
3336 : : {
3337 : 7663392 : bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
3338 : : locus);
3339 : 7663392 : return x;
3340 : : }
3341 : :
3342 : : /* First, see if there is another declaration with the same name in
3343 : : the current scope. If there is, duplicate_decls may do all the
3344 : : work for us. If duplicate_decls returns false, that indicates
3345 : : two incompatible decls in the same scope; we are to silently
3346 : : replace the old one (duplicate_decls has issued all appropriate
3347 : : diagnostics). In particular, we should not consider possible
3348 : : duplicates in the external scope, or shadowing. */
3349 : 188887182 : b = I_SYMBOL_BINDING (name);
3350 : 188887182 : if (b && B_IN_SCOPE (b, scope))
3351 : : {
3352 : 1124505 : struct c_binding *b_ext, *b_use;
3353 : 1124505 : tree type = TREE_TYPE (x);
3354 : 1124505 : tree visdecl = b->decl;
3355 : 1124505 : tree vistype = TREE_TYPE (visdecl);
3356 : 1124505 : if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3357 : 1124505 : && COMPLETE_TYPE_P (TREE_TYPE (x)))
3358 : 1355 : b->inner_comp = false;
3359 : 1124505 : b_use = b;
3360 : 1124505 : b_ext = b;
3361 : : /* If this is an external linkage declaration, we should check
3362 : : for compatibility with the type in the external scope before
3363 : : setting the type at this scope based on the visible
3364 : : information only. */
3365 : 1124505 : if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
3366 : : {
3367 : 2144293 : while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3368 : 1072156 : b_ext = b_ext->shadowed;
3369 : 1072137 : if (b_ext)
3370 : : {
3371 : 1072136 : b_use = b_ext;
3372 : 1072136 : if (b_use->u.type)
3373 : 146956 : TREE_TYPE (b_use->decl) = b_use->u.type;
3374 : : }
3375 : : }
3376 : 1124505 : if (duplicate_decls (x, b_use->decl))
3377 : : {
3378 : 1124146 : if (b_use != b)
3379 : : {
3380 : : /* Save the updated type in the external scope and
3381 : : restore the proper type for this scope. */
3382 : 1071937 : tree thistype;
3383 : 1071937 : if (comptypes (vistype, type))
3384 : 1071903 : thistype = composite_type (vistype, type);
3385 : : else
3386 : 34 : thistype = TREE_TYPE (b_use->decl);
3387 : 1071937 : b_use->u.type = TREE_TYPE (b_use->decl);
3388 : 1071937 : if (TREE_CODE (b_use->decl) == FUNCTION_DECL
3389 : 1071937 : && fndecl_built_in_p (b_use->decl))
3390 : 156654 : thistype
3391 : 156654 : = c_build_type_attribute_variant (thistype,
3392 : 156654 : TYPE_ATTRIBUTES
3393 : : (b_use->u.type));
3394 : 1071937 : TREE_TYPE (b_use->decl) = thistype;
3395 : : }
3396 : 1124146 : return b_use->decl;
3397 : : }
3398 : : else
3399 : 359 : goto skip_external_and_shadow_checks;
3400 : : }
3401 : :
3402 : : /* All declarations with external linkage, and all external
3403 : : references, go in the external scope, no matter what scope is
3404 : : current. However, the binding in that scope is ignored for
3405 : : purposes of normal name lookup. A separate binding structure is
3406 : : created in the requested scope; this governs the normal
3407 : : visibility of the symbol.
3408 : :
3409 : : The binding in the externals scope is used exclusively for
3410 : : detecting duplicate declarations of the same object, no matter
3411 : : what scope they are in; this is what we do here. (C99 6.2.7p2:
3412 : : All declarations that refer to the same object or function shall
3413 : : have compatible type; otherwise, the behavior is undefined.)
3414 : : However, in Objective-C, we also want to detect declarations
3415 : : conflicting with those of the basic types. */
3416 : 327974985 : if ((DECL_EXTERNAL (x) || scope == file_scope)
3417 : 198962889 : && (VAR_OR_FUNCTION_DECL_P (x) || c_dialect_objc ()))
3418 : : {
3419 : 49319326 : tree type = TREE_TYPE (x);
3420 : 49319326 : tree vistype = NULL_TREE;
3421 : 49319326 : tree visdecl = NULL_TREE;
3422 : 49319326 : bool type_saved = false;
3423 : 3529454 : if (b && !B_IN_EXTERNAL_SCOPE (b)
3424 : 1022 : && VAR_OR_FUNCTION_DECL_P (b->decl)
3425 : 49320339 : && DECL_FILE_SCOPE_P (b->decl))
3426 : : {
3427 : 789 : visdecl = b->decl;
3428 : 789 : vistype = TREE_TYPE (visdecl);
3429 : : }
3430 : 49319326 : if (scope != file_scope
3431 : 49319326 : && !DECL_IN_SYSTEM_HEADER (x))
3432 : 11451 : warning_at (locus, OPT_Wnested_externs,
3433 : : "nested extern declaration of %qD", x);
3434 : :
3435 : 49320732 : while (b && !B_IN_EXTERNAL_SCOPE (b))
3436 : : {
3437 : : /* If this decl might be modified, save its type. This is
3438 : : done here rather than when the decl is first bound
3439 : : because the type may change after first binding, through
3440 : : being completed or through attributes being added. If we
3441 : : encounter multiple such decls, only the first should have
3442 : : its type saved; the others will already have had their
3443 : : proper types saved and the types will not have changed as
3444 : : their scopes will not have been re-entered. */
3445 : 1406 : if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3446 : : {
3447 : 1006 : b->u.type = TREE_TYPE (b->decl);
3448 : 1006 : type_saved = true;
3449 : : }
3450 : 1406 : if (B_IN_FILE_SCOPE (b)
3451 : 996 : && VAR_P (b->decl)
3452 : 571 : && TREE_STATIC (b->decl)
3453 : 280 : && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3454 : 138 : && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3455 : 47 : && TREE_CODE (type) == ARRAY_TYPE
3456 : 47 : && TYPE_DOMAIN (type)
3457 : 28 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3458 : 1434 : && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3459 : : {
3460 : : /* Array type completed in inner scope, which should be
3461 : : diagnosed if the completion does not have size 1 and
3462 : : it does not get completed in the file scope. */
3463 : 6 : b->inner_comp = true;
3464 : : }
3465 : 1406 : b = b->shadowed;
3466 : : }
3467 : :
3468 : : /* If a matching external declaration has been found, set its
3469 : : type to the composite of all the types of that declaration.
3470 : : After the consistency checks, it will be reset to the
3471 : : composite of the visible types only. */
3472 : 49319326 : if (b && b->u.type)
3473 : 761 : TREE_TYPE (b->decl) = b->u.type;
3474 : :
3475 : : /* the static does not go in the externals scope. */
3476 : 3529312 : if (b && duplicate_decls (x, b->decl))
3477 : : {
3478 : 3528580 : tree thistype;
3479 : 3528580 : if (vistype)
3480 : : {
3481 : 661 : if (comptypes (vistype, type))
3482 : 624 : thistype = composite_type (vistype, type);
3483 : : else
3484 : 37 : thistype = TREE_TYPE (b->decl);
3485 : : }
3486 : : else
3487 : : thistype = type;
3488 : 3528580 : b->u.type = TREE_TYPE (b->decl);
3489 : : /* Propagate the type attributes to the decl. */
3490 : 3528580 : thistype
3491 : 3528580 : = c_build_type_attribute_variant (thistype,
3492 : 3528580 : TYPE_ATTRIBUTES (b->u.type));
3493 : 3528580 : TREE_TYPE (b->decl) = thistype;
3494 : 3528580 : bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3495 : : locus);
3496 : 3528580 : return b->decl;
3497 : : }
3498 : 45790746 : else if (TREE_PUBLIC (x))
3499 : : {
3500 : 45430952 : if (visdecl && !b && duplicate_decls (x, visdecl))
3501 : : {
3502 : : /* An external declaration at block scope referring to a
3503 : : visible entity with internal linkage. The composite
3504 : : type will already be correct for this scope, so we
3505 : : just need to fall through to make the declaration in
3506 : : this scope. */
3507 : : nested = true;
3508 : : x = visdecl;
3509 : : }
3510 : : else
3511 : : {
3512 : 45430835 : bind (name, x, external_scope, /*invisible=*/true,
3513 : : /*nested=*/false, locus);
3514 : 45430835 : nested = true;
3515 : : }
3516 : : }
3517 : : }
3518 : :
3519 : 184234097 : if (TREE_CODE (x) != PARM_DECL)
3520 : 68517256 : warn_if_shadowing (x);
3521 : :
3522 : 115716841 : skip_external_and_shadow_checks:
3523 : 184234456 : if (TREE_CODE (x) == TYPE_DECL)
3524 : : {
3525 : : /* So this is a typedef, set its underlying type. */
3526 : 9585142 : set_underlying_type (x);
3527 : :
3528 : : /* If X is a typedef defined in the current function, record it
3529 : : for the purpose of implementing the -Wunused-local-typedefs
3530 : : warning. */
3531 : 9585142 : record_locally_defined_typedef (x);
3532 : : }
3533 : :
3534 : 184234456 : bind (name, x, scope, /*invisible=*/false, nested, locus);
3535 : :
3536 : : /* If x's type is incomplete because it's based on a
3537 : : structure or union which has not yet been fully declared,
3538 : : attach it to that structure or union type, so we can go
3539 : : back and complete the variable declaration later, if the
3540 : : structure or union gets fully declared.
3541 : :
3542 : : If the input is erroneous, we can have error_mark in the type
3543 : : slot (e.g. "f(void a, ...)") - that doesn't count as an
3544 : : incomplete type. */
3545 : 184234456 : if (TREE_TYPE (x) != error_mark_node
3546 : 184234456 : && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3547 : : {
3548 : 182168 : tree element = TREE_TYPE (x);
3549 : :
3550 : 201972 : while (TREE_CODE (element) == ARRAY_TYPE)
3551 : 19804 : element = TREE_TYPE (element);
3552 : 182168 : element = TYPE_MAIN_VARIANT (element);
3553 : :
3554 : 182168 : if ((RECORD_OR_UNION_TYPE_P (element)
3555 : 139380 : || TREE_CODE (element) == ENUMERAL_TYPE)
3556 : 42859 : && (TREE_CODE (x) != TYPE_DECL
3557 : 39458 : || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3558 : 185577 : && !COMPLETE_TYPE_P (element))
3559 : 267 : C_TYPE_INCOMPLETE_VARS (element)
3560 : 534 : = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3561 : : }
3562 : : return x;
3563 : : }
3564 : :
3565 : :
3566 : : /* Issue a permerror about implicit function declaration. ID is the function
3567 : : identifier, OLDDECL is a declaration of the function in a different scope,
3568 : : or NULL_TREE. */
3569 : :
3570 : : static void
3571 : 3776 : implicit_decl_permerror (location_t loc, tree id, tree olddecl)
3572 : : {
3573 : 3776 : if (!warn_implicit_function_declaration)
3574 : 2617 : return;
3575 : :
3576 : 1159 : bool warned;
3577 : 1159 : auto_diagnostic_group d;
3578 : 1159 : name_hint hint;
3579 : 1159 : if (!olddecl)
3580 : 553 : hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3581 : :
3582 : 1159 : if (flag_isoc99)
3583 : : {
3584 : 1153 : if (const char *suggestion = hint.suggestion ())
3585 : : {
3586 : 107 : gcc_rich_location richloc (loc);
3587 : 107 : richloc.add_fixit_replace (suggestion);
3588 : 107 : warned = permerror_opt (&richloc, OPT_Wimplicit_function_declaration,
3589 : : "implicit declaration of function %qE;"
3590 : : " did you mean %qs?",
3591 : : id, suggestion);
3592 : 107 : }
3593 : : else
3594 : 1046 : warned = permerror_opt (loc, OPT_Wimplicit_function_declaration,
3595 : : "implicit declaration of function %qE", id);
3596 : : }
3597 : 6 : else if (const char *suggestion = hint.suggestion ())
3598 : : {
3599 : 2 : gcc_rich_location richloc (loc);
3600 : 2 : richloc.add_fixit_replace (suggestion);
3601 : 2 : warned = warning_at
3602 : 2 : (&richloc, OPT_Wimplicit_function_declaration,
3603 : : G_("implicit declaration of function %qE; did you mean %qs?"),
3604 : : id, suggestion);
3605 : 2 : }
3606 : : else
3607 : 4 : warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3608 : : G_("implicit declaration of function %qE"), id);
3609 : :
3610 : 1159 : if (warned)
3611 : : {
3612 : : /* Whether the olddecl is an undeclared builtin function.
3613 : : locate_old_decl will not generate a diagnostic for those,
3614 : : so in that case we want to look elsewhere. */
3615 : 90 : bool undeclared_builtin = (olddecl
3616 : 28 : && TREE_CODE (olddecl) == FUNCTION_DECL
3617 : 28 : && fndecl_built_in_p (olddecl)
3618 : 117 : && !C_DECL_DECLARED_BUILTIN (olddecl));
3619 : 90 : if (undeclared_builtin)
3620 : : {
3621 : 27 : const char *header = header_for_builtin_fn (olddecl);
3622 : 27 : if (header)
3623 : : {
3624 : 20 : rich_location richloc (line_table, loc);
3625 : 20 : maybe_add_include_fixit (&richloc, header, true);
3626 : 20 : inform (&richloc,
3627 : : "include %qs or provide a declaration of %qE",
3628 : : header, id);
3629 : 20 : }
3630 : : }
3631 : 63 : else if (olddecl)
3632 : 1 : locate_old_decl (olddecl);
3633 : : }
3634 : :
3635 : 1069 : if (!warned)
3636 : 1159 : hint.suppress ();
3637 : 1159 : }
3638 : :
3639 : : /* Return the name of the header file that declares built-in function
3640 : : FNDECL, or null if either we don't know or don't expect to see an
3641 : : explicit declaration. */
3642 : :
3643 : : static const char *
3644 : 3178 : header_for_builtin_fn (tree fndecl)
3645 : : {
3646 : 3178 : if (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3647 : : return NULL;
3648 : :
3649 : 3178 : switch (DECL_FUNCTION_CODE (fndecl))
3650 : : {
3651 : : CASE_FLT_FN (BUILT_IN_ACOS):
3652 : : CASE_FLT_FN (BUILT_IN_ACOSH):
3653 : : CASE_FLT_FN (BUILT_IN_ASIN):
3654 : : CASE_FLT_FN (BUILT_IN_ASINH):
3655 : : CASE_FLT_FN (BUILT_IN_ATAN):
3656 : : CASE_FLT_FN (BUILT_IN_ATANH):
3657 : : CASE_FLT_FN (BUILT_IN_ATAN2):
3658 : : CASE_FLT_FN (BUILT_IN_CBRT):
3659 : : CASE_FLT_FN (BUILT_IN_CEIL):
3660 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3661 : : CASE_FLT_FN (BUILT_IN_COPYSIGN):
3662 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3663 : : CASE_FLT_FN (BUILT_IN_COS):
3664 : : CASE_FLT_FN (BUILT_IN_COSH):
3665 : : CASE_FLT_FN (BUILT_IN_ERF):
3666 : : CASE_FLT_FN (BUILT_IN_ERFC):
3667 : : CASE_FLT_FN (BUILT_IN_EXP):
3668 : : CASE_FLT_FN (BUILT_IN_EXP2):
3669 : : CASE_FLT_FN (BUILT_IN_EXPM1):
3670 : : CASE_FLT_FN (BUILT_IN_FABS):
3671 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3672 : : CASE_FLT_FN (BUILT_IN_FDIM):
3673 : : CASE_FLT_FN (BUILT_IN_FLOOR):
3674 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3675 : : CASE_FLT_FN (BUILT_IN_FMA):
3676 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3677 : : CASE_FLT_FN (BUILT_IN_FMAX):
3678 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3679 : : CASE_FLT_FN (BUILT_IN_FMIN):
3680 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3681 : : CASE_FLT_FN (BUILT_IN_FMOD):
3682 : : CASE_FLT_FN (BUILT_IN_FREXP):
3683 : : CASE_FLT_FN (BUILT_IN_HYPOT):
3684 : : CASE_FLT_FN (BUILT_IN_ILOGB):
3685 : : CASE_FLT_FN (BUILT_IN_LDEXP):
3686 : : CASE_FLT_FN (BUILT_IN_LGAMMA):
3687 : : CASE_FLT_FN (BUILT_IN_LLRINT):
3688 : : CASE_FLT_FN (BUILT_IN_LLROUND):
3689 : : CASE_FLT_FN (BUILT_IN_LOG):
3690 : : CASE_FLT_FN (BUILT_IN_LOG10):
3691 : : CASE_FLT_FN (BUILT_IN_LOG1P):
3692 : : CASE_FLT_FN (BUILT_IN_LOG2):
3693 : : CASE_FLT_FN (BUILT_IN_LOGB):
3694 : : CASE_FLT_FN (BUILT_IN_LRINT):
3695 : : CASE_FLT_FN (BUILT_IN_LROUND):
3696 : : CASE_FLT_FN (BUILT_IN_MODF):
3697 : : CASE_FLT_FN (BUILT_IN_NAN):
3698 : : CASE_FLT_FN (BUILT_IN_NEARBYINT):
3699 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3700 : : CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3701 : : CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3702 : : CASE_FLT_FN (BUILT_IN_POW):
3703 : : CASE_FLT_FN (BUILT_IN_REMAINDER):
3704 : : CASE_FLT_FN (BUILT_IN_REMQUO):
3705 : : CASE_FLT_FN (BUILT_IN_RINT):
3706 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3707 : : CASE_FLT_FN (BUILT_IN_ROUND):
3708 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3709 : : CASE_FLT_FN (BUILT_IN_SCALBLN):
3710 : : CASE_FLT_FN (BUILT_IN_SCALBN):
3711 : : CASE_FLT_FN (BUILT_IN_SIN):
3712 : : CASE_FLT_FN (BUILT_IN_SINH):
3713 : : CASE_FLT_FN (BUILT_IN_SINCOS):
3714 : : CASE_FLT_FN (BUILT_IN_SQRT):
3715 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3716 : : CASE_FLT_FN (BUILT_IN_TAN):
3717 : : CASE_FLT_FN (BUILT_IN_TANH):
3718 : : CASE_FLT_FN (BUILT_IN_TGAMMA):
3719 : : CASE_FLT_FN (BUILT_IN_TRUNC):
3720 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3721 : : case BUILT_IN_ISINF:
3722 : : case BUILT_IN_ISNAN:
3723 : : return "<math.h>";
3724 : 29 : CASE_FLT_FN (BUILT_IN_CABS):
3725 : 29 : CASE_FLT_FN (BUILT_IN_CACOS):
3726 : 29 : CASE_FLT_FN (BUILT_IN_CACOSH):
3727 : 29 : CASE_FLT_FN (BUILT_IN_CARG):
3728 : 29 : CASE_FLT_FN (BUILT_IN_CASIN):
3729 : 29 : CASE_FLT_FN (BUILT_IN_CASINH):
3730 : 29 : CASE_FLT_FN (BUILT_IN_CATAN):
3731 : 29 : CASE_FLT_FN (BUILT_IN_CATANH):
3732 : 29 : CASE_FLT_FN (BUILT_IN_CCOS):
3733 : 29 : CASE_FLT_FN (BUILT_IN_CCOSH):
3734 : 29 : CASE_FLT_FN (BUILT_IN_CEXP):
3735 : 29 : CASE_FLT_FN (BUILT_IN_CIMAG):
3736 : 29 : CASE_FLT_FN (BUILT_IN_CLOG):
3737 : 29 : CASE_FLT_FN (BUILT_IN_CONJ):
3738 : 29 : CASE_FLT_FN (BUILT_IN_CPOW):
3739 : 29 : CASE_FLT_FN (BUILT_IN_CPROJ):
3740 : 29 : CASE_FLT_FN (BUILT_IN_CREAL):
3741 : 29 : CASE_FLT_FN (BUILT_IN_CSIN):
3742 : 29 : CASE_FLT_FN (BUILT_IN_CSINH):
3743 : 29 : CASE_FLT_FN (BUILT_IN_CSQRT):
3744 : 29 : CASE_FLT_FN (BUILT_IN_CTAN):
3745 : 29 : CASE_FLT_FN (BUILT_IN_CTANH):
3746 : 29 : return "<complex.h>";
3747 : 228 : case BUILT_IN_MEMCHR:
3748 : 228 : case BUILT_IN_MEMCMP:
3749 : 228 : case BUILT_IN_MEMCPY:
3750 : 228 : case BUILT_IN_MEMMOVE:
3751 : 228 : case BUILT_IN_MEMSET:
3752 : 228 : case BUILT_IN_STRCAT:
3753 : 228 : case BUILT_IN_STRCHR:
3754 : 228 : case BUILT_IN_STRCMP:
3755 : 228 : case BUILT_IN_STRCPY:
3756 : 228 : case BUILT_IN_STRCSPN:
3757 : 228 : case BUILT_IN_STRLEN:
3758 : 228 : case BUILT_IN_STRNCAT:
3759 : 228 : case BUILT_IN_STRNCMP:
3760 : 228 : case BUILT_IN_STRNCPY:
3761 : 228 : case BUILT_IN_STRPBRK:
3762 : 228 : case BUILT_IN_STRRCHR:
3763 : 228 : case BUILT_IN_STRSPN:
3764 : 228 : case BUILT_IN_STRSTR:
3765 : 228 : return "<string.h>";
3766 : 544 : case BUILT_IN_FPRINTF:
3767 : 544 : case BUILT_IN_PUTC:
3768 : 544 : case BUILT_IN_FPUTC:
3769 : 544 : case BUILT_IN_FPUTS:
3770 : 544 : case BUILT_IN_FSCANF:
3771 : 544 : case BUILT_IN_FWRITE:
3772 : 544 : case BUILT_IN_PRINTF:
3773 : 544 : case BUILT_IN_PUTCHAR:
3774 : 544 : case BUILT_IN_PUTS:
3775 : 544 : case BUILT_IN_SCANF:
3776 : 544 : case BUILT_IN_SNPRINTF:
3777 : 544 : case BUILT_IN_SPRINTF:
3778 : 544 : case BUILT_IN_SSCANF:
3779 : 544 : case BUILT_IN_VFPRINTF:
3780 : 544 : case BUILT_IN_VFSCANF:
3781 : 544 : case BUILT_IN_VPRINTF:
3782 : 544 : case BUILT_IN_VSCANF:
3783 : 544 : case BUILT_IN_VSNPRINTF:
3784 : 544 : case BUILT_IN_VSPRINTF:
3785 : 544 : case BUILT_IN_VSSCANF:
3786 : 544 : return "<stdio.h>";
3787 : 2 : case BUILT_IN_ISALNUM:
3788 : 2 : case BUILT_IN_ISALPHA:
3789 : 2 : case BUILT_IN_ISBLANK:
3790 : 2 : case BUILT_IN_ISCNTRL:
3791 : 2 : case BUILT_IN_ISDIGIT:
3792 : 2 : case BUILT_IN_ISGRAPH:
3793 : 2 : case BUILT_IN_ISLOWER:
3794 : 2 : case BUILT_IN_ISPRINT:
3795 : 2 : case BUILT_IN_ISPUNCT:
3796 : 2 : case BUILT_IN_ISSPACE:
3797 : 2 : case BUILT_IN_ISUPPER:
3798 : 2 : case BUILT_IN_ISXDIGIT:
3799 : 2 : case BUILT_IN_TOLOWER:
3800 : 2 : case BUILT_IN_TOUPPER:
3801 : 2 : return "<ctype.h>";
3802 : 0 : case BUILT_IN_ISWALNUM:
3803 : 0 : case BUILT_IN_ISWALPHA:
3804 : 0 : case BUILT_IN_ISWBLANK:
3805 : 0 : case BUILT_IN_ISWCNTRL:
3806 : 0 : case BUILT_IN_ISWDIGIT:
3807 : 0 : case BUILT_IN_ISWGRAPH:
3808 : 0 : case BUILT_IN_ISWLOWER:
3809 : 0 : case BUILT_IN_ISWPRINT:
3810 : 0 : case BUILT_IN_ISWPUNCT:
3811 : 0 : case BUILT_IN_ISWSPACE:
3812 : 0 : case BUILT_IN_ISWUPPER:
3813 : 0 : case BUILT_IN_ISWXDIGIT:
3814 : 0 : case BUILT_IN_TOWLOWER:
3815 : 0 : case BUILT_IN_TOWUPPER:
3816 : 0 : return "<wctype.h>";
3817 : 1818 : case BUILT_IN_ABORT:
3818 : 1818 : case BUILT_IN_ABS:
3819 : 1818 : case BUILT_IN_CALLOC:
3820 : 1818 : case BUILT_IN_EXIT:
3821 : 1818 : case BUILT_IN_FREE:
3822 : 1818 : case BUILT_IN_LABS:
3823 : 1818 : case BUILT_IN_LLABS:
3824 : 1818 : case BUILT_IN_MALLOC:
3825 : 1818 : case BUILT_IN_REALLOC:
3826 : 1818 : case BUILT_IN__EXIT2:
3827 : 1818 : case BUILT_IN_ALIGNED_ALLOC:
3828 : 1818 : return "<stdlib.h>";
3829 : 1 : case BUILT_IN_IMAXABS:
3830 : 1 : return "<inttypes.h>";
3831 : 3 : case BUILT_IN_STRFTIME:
3832 : 3 : return "<time.h>";
3833 : : default:
3834 : : return NULL;
3835 : : }
3836 : : }
3837 : :
3838 : : /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3839 : : function of type int (). */
3840 : :
3841 : : tree
3842 : 4729 : implicitly_declare (location_t loc, tree functionid)
3843 : : {
3844 : 4729 : struct c_binding *b;
3845 : 4729 : tree decl = NULL_TREE;
3846 : 4729 : tree asmspec_tree;
3847 : :
3848 : 4743 : for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3849 : : {
3850 : 3158 : if (B_IN_SCOPE (b, external_scope))
3851 : : {
3852 : 3144 : decl = b->decl;
3853 : 3144 : break;
3854 : : }
3855 : : }
3856 : :
3857 : 4729 : if (decl)
3858 : : {
3859 : 3144 : if (TREE_CODE (decl) != FUNCTION_DECL)
3860 : : return decl;
3861 : :
3862 : : /* FIXME: Objective-C has weird not-really-builtin functions
3863 : : which are supposed to be visible automatically. They wind up
3864 : : in the external scope because they're pushed before the file
3865 : : scope gets created. Catch this here and rebind them into the
3866 : : file scope. */
3867 : 3137 : if (!fndecl_built_in_p (decl) && DECL_IS_UNDECLARED_BUILTIN (decl))
3868 : : {
3869 : 0 : bind (functionid, decl, file_scope,
3870 : : /*invisible=*/false, /*nested=*/true,
3871 : 0 : DECL_SOURCE_LOCATION (decl));
3872 : 0 : return decl;
3873 : : }
3874 : : else
3875 : : {
3876 : 3137 : tree newtype = default_function_type;
3877 : 3137 : if (b->u.type)
3878 : 753 : TREE_TYPE (decl) = b->u.type;
3879 : : /* Implicit declaration of a function already declared
3880 : : (somehow) in a different scope, or as a built-in.
3881 : : If this is the first time this has happened, warn;
3882 : : then recycle the old declaration but with the new type. */
3883 : 3137 : if (!C_DECL_IMPLICIT (decl))
3884 : : {
3885 : 2191 : implicit_decl_permerror (loc, functionid, decl);
3886 : 2191 : C_DECL_IMPLICIT (decl) = 1;
3887 : : }
3888 : 3137 : if (fndecl_built_in_p (decl))
3889 : : {
3890 : 2705 : newtype = c_build_type_attribute_variant (newtype,
3891 : 2705 : TYPE_ATTRIBUTES
3892 : : (TREE_TYPE (decl)));
3893 : 2705 : if (!comptypes (newtype, TREE_TYPE (decl)))
3894 : : {
3895 : 2528 : auto_diagnostic_group d;
3896 : 2528 : bool warned = warning_at (loc,
3897 : 2528 : OPT_Wbuiltin_declaration_mismatch,
3898 : : "incompatible implicit "
3899 : : "declaration of built-in "
3900 : : "function %qD", decl);
3901 : : /* See if we can hint which header to include. */
3902 : 2528 : const char *header = header_for_builtin_fn (decl);
3903 : 2528 : if (header != NULL && warned)
3904 : : {
3905 : 133 : rich_location richloc (line_table, loc);
3906 : 133 : maybe_add_include_fixit (&richloc, header, true);
3907 : 133 : inform (&richloc,
3908 : : "include %qs or provide a declaration of %qD",
3909 : : header, decl);
3910 : 133 : }
3911 : 2528 : newtype = TREE_TYPE (decl);
3912 : 2528 : }
3913 : : }
3914 : : else
3915 : : {
3916 : 432 : if (!comptypes (newtype, TREE_TYPE (decl)))
3917 : : {
3918 : 2 : auto_diagnostic_group d;
3919 : 2 : error_at (loc, "incompatible implicit declaration of "
3920 : : "function %qD", decl);
3921 : 2 : locate_old_decl (decl);
3922 : 2 : }
3923 : : }
3924 : 3137 : b->u.type = TREE_TYPE (decl);
3925 : 3137 : TREE_TYPE (decl) = newtype;
3926 : 3137 : bind (functionid, decl, current_scope,
3927 : : /*invisible=*/false, /*nested=*/true,
3928 : 3137 : DECL_SOURCE_LOCATION (decl));
3929 : 3137 : return decl;
3930 : : }
3931 : : }
3932 : :
3933 : : /* Not seen before. */
3934 : 1585 : decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3935 : 1585 : DECL_EXTERNAL (decl) = 1;
3936 : 1585 : TREE_PUBLIC (decl) = 1;
3937 : 1585 : C_DECL_IMPLICIT (decl) = 1;
3938 : 1585 : implicit_decl_permerror (loc, functionid, 0);
3939 : 1585 : asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3940 : 1585 : if (asmspec_tree)
3941 : 1 : set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3942 : :
3943 : : /* C89 says implicit declarations are in the innermost block.
3944 : : So we record the decl in the standard fashion. */
3945 : 1585 : decl = pushdecl (decl);
3946 : :
3947 : : /* No need to call objc_check_decl here - it's a function type. */
3948 : 1585 : rest_of_decl_compilation (decl, 0, 0);
3949 : :
3950 : : /* Write a record describing this implicit function declaration
3951 : : to the prototypes file (if requested). */
3952 : 1585 : gen_aux_info_record (decl, 0, 1, 0);
3953 : :
3954 : : /* Possibly apply some default attributes to this implicit declaration. */
3955 : 1585 : decl_attributes (&decl, NULL_TREE, 0);
3956 : :
3957 : 1585 : return decl;
3958 : : }
3959 : :
3960 : : /* Issue an error message for a reference to an undeclared variable
3961 : : ID, including a reference to a builtin outside of function-call
3962 : : context. Establish a binding of the identifier to error_mark_node
3963 : : in an appropriate scope, which will suppress further errors for the
3964 : : same identifier. The error message should be given location LOC. */
3965 : : void
3966 : 1422 : undeclared_variable (location_t loc, tree id)
3967 : : {
3968 : 1422 : static bool already = false;
3969 : 1422 : struct c_scope *scope;
3970 : :
3971 : 1422 : auto_diagnostic_group d;
3972 : 1422 : if (current_function_decl == NULL_TREE)
3973 : : {
3974 : 610 : name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3975 : 610 : if (const char *suggestion = guessed_id.suggestion ())
3976 : : {
3977 : 86 : gcc_rich_location richloc (loc);
3978 : 86 : richloc.add_fixit_replace (suggestion);
3979 : 86 : error_at (&richloc,
3980 : : "%qE undeclared here (not in a function);"
3981 : : " did you mean %qs?",
3982 : : id, suggestion);
3983 : 86 : }
3984 : : else
3985 : 524 : error_at (loc, "%qE undeclared here (not in a function)", id);
3986 : 610 : scope = current_scope;
3987 : 610 : }
3988 : : else
3989 : : {
3990 : 812 : if (!objc_diagnose_private_ivar (id))
3991 : : {
3992 : 812 : name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3993 : 812 : if (const char *suggestion = guessed_id.suggestion ())
3994 : : {
3995 : 36 : gcc_rich_location richloc (loc);
3996 : 36 : richloc.add_fixit_replace (suggestion);
3997 : 36 : error_at (&richloc,
3998 : : "%qE undeclared (first use in this function);"
3999 : : " did you mean %qs?",
4000 : : id, suggestion);
4001 : 36 : }
4002 : : else
4003 : 776 : error_at (loc, "%qE undeclared (first use in this function)", id);
4004 : 812 : }
4005 : 812 : if (!already)
4006 : : {
4007 : 195 : inform (loc, "each undeclared identifier is reported only"
4008 : : " once for each function it appears in");
4009 : 195 : already = true;
4010 : : }
4011 : :
4012 : : /* If we are parsing old-style parameter decls, current_function_decl
4013 : : will be nonnull but current_function_scope will be null. */
4014 : 812 : scope = current_function_scope ? current_function_scope : current_scope;
4015 : : }
4016 : 1422 : bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
4017 : : UNKNOWN_LOCATION);
4018 : 1422 : }
4019 : :
4020 : : /* Subroutine of lookup_label, declare_label, define_label: construct a
4021 : : LABEL_DECL with all the proper frills. Also create a struct
4022 : : c_label_vars initialized for the current scope. */
4023 : :
4024 : : static tree
4025 : 23273 : make_label (location_t location, tree name, bool defining,
4026 : : struct c_label_vars **p_label_vars)
4027 : : {
4028 : 23273 : tree label = build_decl (location, LABEL_DECL, name, void_type_node);
4029 : 23273 : DECL_CONTEXT (label) = current_function_decl;
4030 : 23273 : SET_DECL_MODE (label, VOIDmode);
4031 : :
4032 : 23273 : c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
4033 : 23273 : label_vars->shadowed = NULL;
4034 : 23273 : set_spot_bindings (&label_vars->label_bindings, defining);
4035 : 23273 : label_vars->decls_in_scope = make_tree_vector ();
4036 : 23273 : label_vars->gotos = NULL;
4037 : 23273 : *p_label_vars = label_vars;
4038 : :
4039 : 23273 : return label;
4040 : : }
4041 : :
4042 : : /* Get the LABEL_DECL corresponding to identifier NAME as a label.
4043 : : Create one if none exists so far for the current function.
4044 : : This is called when a label is used in a goto expression or
4045 : : has its address taken. */
4046 : :
4047 : : tree
4048 : 84688 : lookup_label (tree name)
4049 : : {
4050 : 84688 : tree label;
4051 : 84688 : struct c_label_vars *label_vars;
4052 : :
4053 : 84688 : if (current_function_scope == 0)
4054 : : {
4055 : 2 : error ("label %qE referenced outside of any function", name);
4056 : 2 : return NULL_TREE;
4057 : : }
4058 : :
4059 : : /* Use a label already defined or ref'd with this name, but not if
4060 : : it is inherited from a containing function and wasn't declared
4061 : : using __label__. */
4062 : 84686 : label = I_LABEL_DECL (name);
4063 : 79230 : if (label && (DECL_CONTEXT (label) == current_function_decl
4064 : 606 : || C_DECLARED_LABEL_FLAG (label)))
4065 : : {
4066 : : /* If the label has only been declared, update its apparent
4067 : : location to point here, for better diagnostics if it
4068 : : turns out not to have been defined. */
4069 : 79223 : if (DECL_INITIAL (label) == NULL_TREE)
4070 : 62628 : DECL_SOURCE_LOCATION (label) = input_location;
4071 : 79223 : return label;
4072 : : }
4073 : :
4074 : : /* No label binding for that identifier; make one. */
4075 : 5463 : label = make_label (input_location, name, false, &label_vars);
4076 : :
4077 : : /* Ordinary labels go in the current function scope. */
4078 : 5463 : bind_label (name, label, current_function_scope, label_vars);
4079 : :
4080 : 5463 : return label;
4081 : : }
4082 : :
4083 : : /* Issue a warning about DECL for a goto statement at GOTO_LOC going
4084 : : to LABEL. */
4085 : :
4086 : : static void
4087 : 1482 : warn_about_goto (location_t goto_loc, tree label, tree decl)
4088 : : {
4089 : 1482 : auto_diagnostic_group d;
4090 : 1482 : if (c_type_variably_modified_p (TREE_TYPE (decl)))
4091 : 1476 : error_at (goto_loc,
4092 : : "jump into scope of identifier with variably modified type");
4093 : 6 : else if (flag_openmp
4094 : 6 : && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl)))
4095 : 2 : error_at (goto_loc, "jump skips OpenMP %<allocate%> allocation");
4096 : : else
4097 : 4 : if (!warning_at (goto_loc, OPT_Wjump_misses_init,
4098 : : "jump skips variable initialization"))
4099 : 0 : return;
4100 : 1482 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4101 : 1482 : inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
4102 : 1482 : }
4103 : :
4104 : : /* Look up a label because of a goto statement. This is like
4105 : : lookup_label, but also issues any appropriate warnings. */
4106 : :
4107 : : tree
4108 : 82861 : lookup_label_for_goto (location_t loc, tree name)
4109 : : {
4110 : 82861 : tree label;
4111 : 82861 : struct c_label_vars *label_vars;
4112 : 82861 : unsigned int ix;
4113 : 82861 : tree decl;
4114 : :
4115 : 82861 : label = lookup_label (name);
4116 : 82861 : if (label == NULL_TREE)
4117 : : return NULL_TREE;
4118 : :
4119 : : /* If we are jumping to a different function, we can't issue any
4120 : : useful warnings. */
4121 : 82861 : if (DECL_CONTEXT (label) != current_function_decl)
4122 : : {
4123 : 520 : gcc_assert (C_DECLARED_LABEL_FLAG (label));
4124 : : return label;
4125 : : }
4126 : :
4127 : 82341 : label_vars = I_LABEL_BINDING (name)->u.label;
4128 : :
4129 : : /* If the label has not yet been defined, then push this goto on a
4130 : : list for possible later warnings. */
4131 : 82341 : if (label_vars->label_bindings.scope == NULL)
4132 : : {
4133 : 65971 : c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
4134 : :
4135 : 65971 : g->loc = loc;
4136 : 65971 : set_spot_bindings (&g->goto_bindings, true);
4137 : 65971 : vec_safe_push (label_vars->gotos, g);
4138 : 65971 : return label;
4139 : : }
4140 : :
4141 : : /* If there are any decls in label_vars->decls_in_scope, then this
4142 : : goto has missed the declaration of the decl. This happens for a
4143 : : case like
4144 : : int i = 1;
4145 : : lab:
4146 : : ...
4147 : : goto lab;
4148 : : Issue a warning or error. */
4149 : 17145 : FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
4150 : 775 : warn_about_goto (loc, label, decl);
4151 : :
4152 : 16370 : if (label_vars->label_bindings.left_stmt_expr)
4153 : : {
4154 : 120 : auto_diagnostic_group d;
4155 : 120 : error_at (loc, "jump into statement expression");
4156 : 120 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4157 : 120 : }
4158 : :
4159 : : return label;
4160 : : }
4161 : :
4162 : : /* Make a label named NAME in the current function, shadowing silently
4163 : : any that may be inherited from containing functions or containing
4164 : : scopes. This is called for __label__ declarations. */
4165 : :
4166 : : tree
4167 : 1181 : declare_label (tree name)
4168 : : {
4169 : 1181 : struct c_binding *b = I_LABEL_BINDING (name);
4170 : 1181 : tree label;
4171 : 1181 : struct c_label_vars *label_vars;
4172 : :
4173 : : /* Check to make sure that the label hasn't already been declared
4174 : : at this scope */
4175 : 1181 : if (b && B_IN_CURRENT_SCOPE (b))
4176 : : {
4177 : 2 : auto_diagnostic_group d;
4178 : 2 : error ("duplicate label declaration %qE", name);
4179 : 2 : locate_old_decl (b->decl);
4180 : :
4181 : : /* Just use the previous declaration. */
4182 : 2 : return b->decl;
4183 : 2 : }
4184 : :
4185 : 1179 : label = make_label (input_location, name, false, &label_vars);
4186 : 1179 : C_DECLARED_LABEL_FLAG (label) = 1;
4187 : :
4188 : : /* Declared labels go in the current scope. */
4189 : 1179 : bind_label (name, label, current_scope, label_vars);
4190 : :
4191 : 1179 : return label;
4192 : : }
4193 : :
4194 : : /* When we define a label, issue any appropriate warnings if there are
4195 : : any gotos earlier in the function which jump to this label. */
4196 : :
4197 : : static void
4198 : 6562 : check_earlier_gotos (tree label, struct c_label_vars* label_vars)
4199 : : {
4200 : 6562 : unsigned int ix;
4201 : 6562 : struct c_goto_bindings *g;
4202 : :
4203 : 72467 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
4204 : : {
4205 : 65905 : struct c_binding *b;
4206 : 65905 : struct c_scope *scope;
4207 : :
4208 : : /* We have a goto to this label. The goto is going forward. In
4209 : : g->scope, the goto is going to skip any binding which was
4210 : : defined after g->bindings_in_scope. */
4211 : 65905 : if (g->goto_bindings.scope->has_jump_unsafe_decl)
4212 : : {
4213 : 251 : for (b = g->goto_bindings.scope->bindings;
4214 : 581 : b != g->goto_bindings.bindings_in_scope;
4215 : 330 : b = b->prev)
4216 : : {
4217 : 330 : if (decl_jump_unsafe (b->decl))
4218 : 168 : warn_about_goto (g->loc, label, b->decl);
4219 : : }
4220 : : }
4221 : :
4222 : : /* We also need to warn about decls defined in any scopes
4223 : : between the scope of the label and the scope of the goto. */
4224 : 65905 : for (scope = label_vars->label_bindings.scope;
4225 : 68452 : scope != g->goto_bindings.scope;
4226 : 2547 : scope = scope->outer)
4227 : : {
4228 : 2547 : gcc_assert (scope != NULL);
4229 : 2547 : if (scope->has_jump_unsafe_decl)
4230 : : {
4231 : 325 : if (scope == label_vars->label_bindings.scope)
4232 : 249 : b = label_vars->label_bindings.bindings_in_scope;
4233 : : else
4234 : 76 : b = scope->bindings;
4235 : 864 : for (; b != NULL; b = b->prev)
4236 : : {
4237 : 539 : if (decl_jump_unsafe (b->decl))
4238 : 539 : warn_about_goto (g->loc, label, b->decl);
4239 : : }
4240 : : }
4241 : : }
4242 : :
4243 : 65905 : if (g->goto_bindings.stmt_exprs > 0)
4244 : : {
4245 : 100 : auto_diagnostic_group d;
4246 : 100 : error_at (g->loc, "jump into statement expression");
4247 : 100 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
4248 : : label);
4249 : 100 : }
4250 : : }
4251 : :
4252 : : /* Now that the label is defined, we will issue warnings about
4253 : : subsequent gotos to this label when we see them. */
4254 : 6562 : vec_safe_truncate (label_vars->gotos, 0);
4255 : 6562 : label_vars->gotos = NULL;
4256 : 6562 : }
4257 : :
4258 : : /* Define a label, specifying the location in the source file.
4259 : : Return the LABEL_DECL node for the label, if the definition is valid.
4260 : : Otherwise return NULL_TREE. */
4261 : :
4262 : : tree
4263 : 23222 : define_label (location_t location, tree name)
4264 : : {
4265 : : /* Find any preexisting label with this name. It is an error
4266 : : if that label has already been defined in this function, or
4267 : : if there is a containing function with a declared label with
4268 : : the same name. */
4269 : 23222 : tree label = I_LABEL_DECL (name);
4270 : :
4271 : 6598 : if (label
4272 : 6598 : && ((DECL_CONTEXT (label) == current_function_decl
4273 : 6584 : && DECL_INITIAL (label) != NULL_TREE)
4274 : 6576 : || (DECL_CONTEXT (label) != current_function_decl
4275 : 14 : && C_DECLARED_LABEL_FLAG (label))))
4276 : : {
4277 : 29 : auto_diagnostic_group d;
4278 : 29 : error_at (location, "duplicate label %qD", label);
4279 : 29 : locate_old_decl (label);
4280 : 29 : return NULL_TREE;
4281 : 29 : }
4282 : 23193 : else if (label && DECL_CONTEXT (label) == current_function_decl)
4283 : : {
4284 : 6562 : struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
4285 : :
4286 : : /* The label has been used or declared already in this function,
4287 : : but not defined. Update its location to point to this
4288 : : definition. */
4289 : 6562 : DECL_SOURCE_LOCATION (label) = location;
4290 : 6562 : set_spot_bindings (&label_vars->label_bindings, true);
4291 : :
4292 : : /* Issue warnings as required about any goto statements from
4293 : : earlier in the function. */
4294 : 6562 : check_earlier_gotos (label, label_vars);
4295 : : }
4296 : : else
4297 : : {
4298 : 16631 : struct c_label_vars *label_vars;
4299 : :
4300 : : /* No label binding for that identifier; make one. */
4301 : 16631 : label = make_label (location, name, true, &label_vars);
4302 : :
4303 : : /* Ordinary labels go in the current function scope. */
4304 : 16631 : bind_label (name, label, current_function_scope, label_vars);
4305 : : }
4306 : :
4307 : 23193 : if (!in_system_header_at (input_location) && lookup_name (name))
4308 : 131 : warning_at (location, OPT_Wtraditional,
4309 : : "traditional C lacks a separate namespace "
4310 : : "for labels, identifier %qE conflicts", name);
4311 : :
4312 : : /* Mark label as having been defined. */
4313 : 23193 : DECL_INITIAL (label) = error_mark_node;
4314 : 23193 : return label;
4315 : : }
4316 : :
4317 : : /* Get the bindings for a new switch statement. This is used to issue
4318 : : warnings as appropriate for jumps from the switch to case or
4319 : : default labels. */
4320 : :
4321 : : struct c_spot_bindings *
4322 : 36575 : c_get_switch_bindings (void)
4323 : : {
4324 : 36575 : struct c_spot_bindings *switch_bindings;
4325 : :
4326 : 36575 : switch_bindings = XNEW (struct c_spot_bindings);
4327 : 36575 : set_spot_bindings (switch_bindings, true);
4328 : 36575 : return switch_bindings;
4329 : : }
4330 : :
4331 : : void
4332 : 36575 : c_release_switch_bindings (struct c_spot_bindings *bindings)
4333 : : {
4334 : 36575 : gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
4335 : 36575 : XDELETE (bindings);
4336 : 36575 : }
4337 : :
4338 : : /* This is called at the point of a case or default label to issue
4339 : : warnings about decls as needed. It returns true if it found an
4340 : : error, not just a warning. */
4341 : :
4342 : : bool
4343 : 1023926 : c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
4344 : : location_t switch_loc, location_t case_loc)
4345 : : {
4346 : 1023926 : bool saw_error;
4347 : 1023926 : struct c_scope *scope;
4348 : :
4349 : 1023926 : saw_error = false;
4350 : 1023926 : for (scope = current_scope;
4351 : 3071810 : scope != switch_bindings->scope;
4352 : 2047884 : scope = scope->outer)
4353 : : {
4354 : 2047884 : struct c_binding *b;
4355 : :
4356 : 2047884 : gcc_assert (scope != NULL);
4357 : :
4358 : 2047884 : if (!scope->has_jump_unsafe_decl)
4359 : 2047873 : continue;
4360 : :
4361 : 22 : for (b = scope->bindings; b != NULL; b = b->prev)
4362 : : {
4363 : 11 : if (decl_jump_unsafe (b->decl))
4364 : : {
4365 : 11 : auto_diagnostic_group d;
4366 : 11 : bool emitted;
4367 : 11 : if (c_type_variably_modified_p (TREE_TYPE (b->decl)))
4368 : : {
4369 : 6 : saw_error = true;
4370 : 6 : error_at (case_loc,
4371 : : ("switch jumps into scope of identifier with "
4372 : : "variably modified type"));
4373 : 6 : emitted = true;
4374 : : }
4375 : 5 : else if (flag_openmp
4376 : 7 : && lookup_attribute ("omp allocate",
4377 : 2 : DECL_ATTRIBUTES (b->decl)))
4378 : : {
4379 : 2 : saw_error = true;
4380 : 2 : error_at (case_loc,
4381 : : "switch jumps over OpenMP %<allocate%> allocation");
4382 : 2 : emitted = true;
4383 : : }
4384 : : else
4385 : 3 : emitted
4386 : 3 : = warning_at (case_loc, OPT_Wjump_misses_init,
4387 : : "switch jumps over variable initialization");
4388 : 11 : if (emitted)
4389 : : {
4390 : 11 : inform (switch_loc, "switch starts here");
4391 : 11 : inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
4392 : : b->decl);
4393 : : }
4394 : 11 : }
4395 : : }
4396 : : }
4397 : :
4398 : 1023926 : if (switch_bindings->stmt_exprs > 0)
4399 : : {
4400 : 4 : saw_error = true;
4401 : 4 : auto_diagnostic_group d;
4402 : 4 : error_at (case_loc, "switch jumps into statement expression");
4403 : 4 : inform (switch_loc, "switch starts here");
4404 : 4 : }
4405 : :
4406 : 1023926 : return saw_error;
4407 : : }
4408 : :
4409 : : /* Given NAME, an IDENTIFIER_NODE,
4410 : : return the structure (or union or enum) definition for that name.
4411 : : If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4412 : : CODE says which kind of type the caller wants;
4413 : : it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4414 : : If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4415 : : location where the tag was defined.
4416 : : If the wrong kind of type is found, an error is reported. */
4417 : :
4418 : : static tree
4419 : 2038839 : lookup_tag (enum tree_code code, tree name, bool thislevel_only,
4420 : : location_t *ploc)
4421 : : {
4422 : 2038839 : struct c_binding *b = I_TAG_BINDING (name);
4423 : 2038839 : bool thislevel = false;
4424 : :
4425 : 2038839 : if (!b || !b->decl)
4426 : : return NULL_TREE;
4427 : :
4428 : : /* We only care about whether it's in this level if
4429 : : thislevel_only was set or it might be a type clash. */
4430 : 1427507 : if (thislevel_only || TREE_CODE (b->decl) != code)
4431 : : {
4432 : : /* For our purposes, a tag in the external scope is the same as
4433 : : a tag in the file scope. (Primarily relevant to Objective-C
4434 : : and its builtin structure tags, which get pushed before the
4435 : : file scope is created.) */
4436 : 456950 : if (B_IN_CURRENT_SCOPE (b)
4437 : 175 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
4438 : 1427507 : thislevel = true;
4439 : : }
4440 : :
4441 : 1427507 : if (thislevel_only && !thislevel)
4442 : : return NULL_TREE;
4443 : :
4444 : 1427353 : if (TREE_CODE (b->decl) != code)
4445 : : {
4446 : : /* Definition isn't the kind we were looking for. */
4447 : 39 : pending_invalid_xref = name;
4448 : 39 : pending_invalid_xref_location = input_location;
4449 : :
4450 : : /* If in the same binding level as a declaration as a tag
4451 : : of a different type, this must not be allowed to
4452 : : shadow that tag, so give the error immediately.
4453 : : (For example, "struct foo; union foo;" is invalid.) */
4454 : 39 : if (thislevel)
4455 : 18 : pending_xref_error ();
4456 : : }
4457 : :
4458 : 1427353 : if (ploc != NULL)
4459 : 995532 : *ploc = b->locus;
4460 : :
4461 : 1427353 : return b->decl;
4462 : : }
4463 : :
4464 : : /* Return true if a definition exists for NAME with code CODE. */
4465 : :
4466 : : bool
4467 : 381 : tag_exists_p (enum tree_code code, tree name)
4468 : : {
4469 : 381 : struct c_binding *b = I_TAG_BINDING (name);
4470 : :
4471 : 381 : if (b == NULL || b->decl == NULL_TREE)
4472 : : return false;
4473 : 18 : return TREE_CODE (b->decl) == code;
4474 : : }
4475 : :
4476 : : /* Print an error message now
4477 : : for a recent invalid struct, union or enum cross reference.
4478 : : We don't print them immediately because they are not invalid
4479 : : when used in the `struct foo;' construct for shadowing. */
4480 : :
4481 : : void
4482 : 304326409 : pending_xref_error (void)
4483 : : {
4484 : 304326409 : if (pending_invalid_xref != NULL_TREE)
4485 : 27 : error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4486 : : pending_invalid_xref);
4487 : 304326409 : pending_invalid_xref = NULL_TREE;
4488 : 304326409 : }
4489 : :
4490 : :
4491 : : /* Look up NAME in the current scope and its superiors
4492 : : in the namespace of variables, functions and typedefs.
4493 : : Return a ..._DECL node of some kind representing its definition,
4494 : : or return NULL_TREE if it is undefined. */
4495 : :
4496 : : tree
4497 : 1231447131 : lookup_name (tree name)
4498 : : {
4499 : 1231447131 : struct c_binding *b = I_SYMBOL_BINDING (name);
4500 : 1231447131 : if (b && !b->invisible)
4501 : : {
4502 : 823048813 : maybe_record_typedef_use (b->decl);
4503 : 823048813 : return b->decl;
4504 : : }
4505 : : return NULL_TREE;
4506 : : }
4507 : :
4508 : : /* Similar to `lookup_name' but look only at the indicated scope. */
4509 : :
4510 : : static tree
4511 : 127535136 : lookup_name_in_scope (tree name, struct c_scope *scope)
4512 : : {
4513 : 127535136 : struct c_binding *b;
4514 : :
4515 : 127538299 : for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4516 : 7284150 : if (B_IN_SCOPE (b, scope))
4517 : 7280987 : return b->decl;
4518 : : return NULL_TREE;
4519 : : }
4520 : :
4521 : : /* Look for the closest match for NAME within the currently valid
4522 : : scopes.
4523 : :
4524 : : This finds the identifier with the lowest Levenshtein distance to
4525 : : NAME. If there are multiple candidates with equal minimal distance,
4526 : : the first one found is returned. Scopes are searched from innermost
4527 : : outwards, and within a scope in reverse order of declaration, thus
4528 : : benefiting candidates "near" to the current scope.
4529 : :
4530 : : The function also looks for similar macro names to NAME, since a
4531 : : misspelled macro name will not be expanded, and hence looks like an
4532 : : identifier to the C frontend.
4533 : :
4534 : : It also looks for start_typename keywords, to detect "singed" vs "signed"
4535 : : typos.
4536 : :
4537 : : Use LOC for any deferred diagnostics. */
4538 : :
4539 : : name_hint
4540 : 2224 : lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4541 : : {
4542 : 2224 : gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4543 : :
4544 : : /* First, try some well-known names in the C standard library, in case
4545 : : the user forgot a #include. */
4546 : 2224 : const char *header_hint
4547 : 2224 : = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4548 : :
4549 : 2224 : if (header_hint)
4550 : 56 : return name_hint
4551 : : (nullptr,
4552 : 56 : std::make_unique<suggest_missing_header> (loc,
4553 : 112 : IDENTIFIER_POINTER (name),
4554 : 56 : header_hint));
4555 : :
4556 : : /* Next, look for exact matches for builtin defines that would have been
4557 : : defined if the user had passed a command-line option (e.g. -fopenmp
4558 : : for "_OPENMP"). */
4559 : 2168 : diagnostic_option_id option_id
4560 : 2168 : = get_option_for_builtin_define (IDENTIFIER_POINTER (name));
4561 : 2168 : if (option_id.m_idx > 0)
4562 : 2 : return name_hint
4563 : : (nullptr,
4564 : 2 : std::make_unique<suggest_missing_option> (loc,
4565 : 4 : IDENTIFIER_POINTER (name),
4566 : 2 : option_id));
4567 : :
4568 : : /* Only suggest names reserved for the implementation if NAME begins
4569 : : with an underscore. */
4570 : 2166 : bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4571 : :
4572 : 2166 : best_match<tree, tree> bm (name);
4573 : :
4574 : : /* Look within currently valid scopes. */
4575 : 9587 : for (c_scope *scope = current_scope; scope; scope = scope->outer)
4576 : 12131578 : for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4577 : : {
4578 : 12124157 : if (!binding->id || binding->invisible)
4579 : 6881691 : continue;
4580 : 5242466 : if (binding->decl == error_mark_node)
4581 : 356 : continue;
4582 : : /* Don't use bindings from implicitly declared functions,
4583 : : as they were likely misspellings themselves. */
4584 : 5242110 : if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4585 : 4830321 : if (C_DECL_IMPLICIT (binding->decl))
4586 : 229 : continue;
4587 : : /* Don't suggest names that are reserved for use by the
4588 : : implementation, unless NAME began with an underscore. */
4589 : 5241881 : if (!consider_implementation_names)
4590 : : {
4591 : 2319542 : const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4592 : 2319542 : if (name_reserved_for_implementation_p (suggestion_str))
4593 : 2262077 : continue;
4594 : : }
4595 : 2979804 : switch (kind)
4596 : : {
4597 : 17650 : case FUZZY_LOOKUP_TYPENAME:
4598 : 17650 : if (TREE_CODE (binding->decl) != TYPE_DECL)
4599 : 10495 : continue;
4600 : : break;
4601 : :
4602 : 128872 : case FUZZY_LOOKUP_FUNCTION_NAME:
4603 : 128872 : if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4604 : : {
4605 : : /* Allow function pointers. */
4606 : 26502 : if ((VAR_P (binding->decl)
4607 : 21977 : || TREE_CODE (binding->decl) == PARM_DECL)
4608 : 5400 : && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4609 : 27782 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4610 : : == FUNCTION_TYPE))
4611 : : break;
4612 : 26483 : continue;
4613 : : }
4614 : : break;
4615 : :
4616 : : default:
4617 : : break;
4618 : : }
4619 : 2942826 : bm.consider (binding->id);
4620 : : }
4621 : :
4622 : : /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4623 : : as:
4624 : : x = SOME_OTHER_MACRO (y);
4625 : : then "SOME_OTHER_MACRO" will survive to the frontend and show up
4626 : : as a misspelled identifier.
4627 : :
4628 : : Use the best distance so far so that a candidate is only set if
4629 : : a macro is better than anything so far. This allows early rejection
4630 : : (without calculating the edit distance) of macro names that must have
4631 : : distance >= bm.get_best_distance (), and means that we only get a
4632 : : non-NULL result for best_macro_match if it's better than any of
4633 : : the identifiers already checked, which avoids needless creation
4634 : : of identifiers for macro hashnodes. */
4635 : 2166 : best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4636 : 2166 : cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4637 : : /* If a macro is the closest so far to NAME, use it, creating an
4638 : : identifier tree node for it. */
4639 : 2166 : if (best_macro)
4640 : : {
4641 : 11 : const char *id = (const char *)best_macro->ident.str;
4642 : 11 : tree macro_as_identifier
4643 : 11 : = get_identifier_with_length (id, best_macro->ident.len);
4644 : 11 : bm.set_best_so_far (macro_as_identifier,
4645 : : bmm.get_best_distance (),
4646 : : bmm.get_best_candidate_length ());
4647 : : }
4648 : :
4649 : : /* Try the "start_typename" keywords to detect
4650 : : "singed" vs "signed" typos. */
4651 : 2166 : if (kind == FUZZY_LOOKUP_TYPENAME)
4652 : : {
4653 : 54044 : for (unsigned i = 0; i < num_c_common_reswords; i++)
4654 : : {
4655 : 53808 : const c_common_resword *resword = &c_common_reswords[i];
4656 : 53808 : if (!c_keyword_starts_typename (resword->rid))
4657 : 40828 : continue;
4658 : 12980 : tree resword_identifier = ridpointers [resword->rid];
4659 : 12980 : if (!resword_identifier)
4660 : 30 : continue;
4661 : 12950 : gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4662 : 12950 : bm.consider (resword_identifier);
4663 : : }
4664 : : }
4665 : :
4666 : 2166 : tree best = bm.get_best_meaningful_candidate ();
4667 : 2166 : if (best)
4668 : 275 : return name_hint (IDENTIFIER_POINTER (best), NULL);
4669 : : else
4670 : 1891 : return name_hint (NULL, NULL);
4671 : : }
4672 : :
4673 : :
4674 : : /* Handle the standard [[nodiscard]] attribute. */
4675 : :
4676 : : static tree
4677 : 26 : handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4678 : : int /*flags*/, bool *no_add_attrs)
4679 : : {
4680 : 26 : if (TREE_CODE (*node) == FUNCTION_DECL)
4681 : : {
4682 : 10 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4683 : 1 : warning_at (DECL_SOURCE_LOCATION (*node),
4684 : 1 : OPT_Wattributes, "%qE attribute applied to %qD with void "
4685 : : "return type", name, *node);
4686 : : }
4687 : 16 : else if (RECORD_OR_UNION_TYPE_P (*node)
4688 : 12 : || TREE_CODE (*node) == ENUMERAL_TYPE)
4689 : : /* OK */;
4690 : : else
4691 : : {
4692 : 10 : pedwarn (input_location,
4693 : 10 : OPT_Wattributes, "%qE attribute can only be applied to "
4694 : : "functions or to structure, union or enumeration types", name);
4695 : 10 : *no_add_attrs = true;
4696 : : }
4697 : 26 : return NULL_TREE;
4698 : : }
4699 : :
4700 : : /* Handle the standard [[noreturn]] attribute. */
4701 : :
4702 : : static tree
4703 : 43 : handle_std_noreturn_attribute (tree *node, tree name, tree args,
4704 : : int flags, bool *no_add_attrs)
4705 : : {
4706 : : /* Unlike GNU __attribute__ ((noreturn)), the standard [[noreturn]]
4707 : : only applies to functions, not function pointers. */
4708 : 43 : if (TREE_CODE (*node) == FUNCTION_DECL)
4709 : 21 : return handle_noreturn_attribute (node, name, args, flags, no_add_attrs);
4710 : : else
4711 : : {
4712 : 22 : pedwarn (input_location, OPT_Wattributes,
4713 : : "standard %qE attribute can only be applied to functions",
4714 : : name);
4715 : 22 : *no_add_attrs = true;
4716 : 22 : return NULL_TREE;
4717 : : }
4718 : : }
4719 : :
4720 : : /* Handle the standard [[unsequenced]] attribute. */
4721 : :
4722 : : static tree
4723 : 68 : handle_std_unsequenced_attribute (tree *node, tree name, tree args,
4724 : : int flags, bool *no_add_attrs)
4725 : : {
4726 : : /* Unlike GNU __attribute__ ((unsequenced)), the standard [[unsequenced]]
4727 : : should be only applied to function declarators or type specifiers which
4728 : : have function type. */
4729 : 68 : if (node[2])
4730 : : {
4731 : 8 : auto_diagnostic_group d;
4732 : 8 : if (pedwarn (input_location, OPT_Wattributes,
4733 : : "standard %qE attribute can only be applied to function "
4734 : : "declarators or type specifiers with function type", name))
4735 : 8 : inform (input_location, "did you mean to specify it after %<)%> "
4736 : : "following function parameters?");
4737 : 8 : *no_add_attrs = true;
4738 : 8 : return NULL_TREE;
4739 : 8 : }
4740 : 60 : return handle_unsequenced_attribute (node, name, args, flags, no_add_attrs);
4741 : : }
4742 : :
4743 : : /* Handle the standard [[reproducible]] attribute. */
4744 : :
4745 : : static tree
4746 : 34 : handle_std_reproducible_attribute (tree *node, tree name, tree args,
4747 : : int flags, bool *no_add_attrs)
4748 : : {
4749 : 34 : return handle_std_unsequenced_attribute (node, name, args, flags,
4750 : 34 : no_add_attrs);
4751 : : }
4752 : :
4753 : : /* Table of supported standard (C23) attributes. */
4754 : : static const attribute_spec std_attributes[] =
4755 : : {
4756 : : /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4757 : : affects_type_identity, handler, exclude } */
4758 : : { "_Noreturn", 0, 0, false, false, false, false,
4759 : : handle_std_noreturn_attribute, NULL },
4760 : : { "deprecated", 0, 1, false, false, false, false,
4761 : : handle_deprecated_attribute, NULL },
4762 : : { "fallthrough", 0, 0, false, false, false, false,
4763 : : handle_fallthrough_attribute, NULL },
4764 : : { "maybe_unused", 0, 0, false, false, false, false,
4765 : : handle_unused_attribute, NULL },
4766 : : { "nodiscard", 0, 1, false, false, false, false,
4767 : : handle_nodiscard_attribute, NULL },
4768 : : { "noreturn", 0, 0, false, false, false, false,
4769 : : handle_std_noreturn_attribute, NULL },
4770 : : { "reproducible", 0, 0, false, true, true, false,
4771 : : handle_std_reproducible_attribute, NULL },
4772 : : { "unsequenced", 0, 0, false, true, true, false,
4773 : : handle_std_unsequenced_attribute, NULL }
4774 : : };
4775 : :
4776 : : const scoped_attribute_specs std_attribute_table =
4777 : : {
4778 : : nullptr, { std_attributes }
4779 : : };
4780 : :
4781 : : /* Create the predefined scalar types of C,
4782 : : and some nodes representing standard constants (0, 1, (void *) 0).
4783 : : Initialize the global scope.
4784 : : Make definitions for built-in primitive functions. */
4785 : :
4786 : : void
4787 : 110990 : c_init_decl_processing (void)
4788 : : {
4789 : 110990 : location_t save_loc = input_location;
4790 : :
4791 : : /* Initialize reserved words for parser. */
4792 : 110990 : c_parse_init ();
4793 : :
4794 : 110990 : current_function_decl = NULL_TREE;
4795 : :
4796 : 110990 : gcc_obstack_init (&parser_obstack);
4797 : :
4798 : : /* Make the externals scope. */
4799 : 110990 : push_scope ();
4800 : 110990 : external_scope = current_scope;
4801 : :
4802 : : /* Declarations from c_common_nodes_and_builtins must not be associated
4803 : : with this input file, lest we get differences between using and not
4804 : : using preprocessed headers. */
4805 : 110990 : input_location = BUILTINS_LOCATION;
4806 : :
4807 : 110990 : c_common_nodes_and_builtins ();
4808 : :
4809 : : /* In C, comparisons and TRUTH_* expressions have type int. */
4810 : 110990 : truthvalue_type_node = integer_type_node;
4811 : 110990 : truthvalue_true_node = integer_one_node;
4812 : 110990 : truthvalue_false_node = integer_zero_node;
4813 : :
4814 : : /* Even in C99, which has a real boolean type. */
4815 : 110990 : pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4816 : : boolean_type_node));
4817 : :
4818 : : /* C-specific nullptr initialization. */
4819 : 110990 : record_builtin_type (RID_MAX, "typeof (nullptr)", nullptr_type_node);
4820 : : /* The size and alignment of nullptr_t is the same as for a pointer to
4821 : : character type. */
4822 : 110990 : SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4823 : :
4824 : 110990 : input_location = save_loc;
4825 : :
4826 : 110990 : make_fname_decl = c_make_fname_decl;
4827 : 110990 : start_fname_decls ();
4828 : 110990 : }
4829 : :
4830 : : /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4831 : : give the decl, NAME is the initialization string and TYPE_DEP
4832 : : indicates whether NAME depended on the type of the function. As we
4833 : : don't yet implement delayed emission of static data, we mark the
4834 : : decl as emitted so it is not placed in the output. Anything using
4835 : : it must therefore pull out the STRING_CST initializer directly.
4836 : : FIXME. */
4837 : :
4838 : : static tree
4839 : 2735 : c_make_fname_decl (location_t loc, tree id, int type_dep)
4840 : : {
4841 : 2735 : const char *name = fname_as_string (type_dep);
4842 : 2735 : tree decl, type, init;
4843 : 2735 : size_t length = strlen (name);
4844 : :
4845 : 2735 : type = c_build_array_type (char_type_node,
4846 : 2735 : build_index_type (size_int (length)));
4847 : 2735 : type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4848 : :
4849 : 2735 : decl = build_decl (loc, VAR_DECL, id, type);
4850 : :
4851 : 2735 : TREE_STATIC (decl) = 1;
4852 : 2735 : TREE_READONLY (decl) = 1;
4853 : 2735 : DECL_ARTIFICIAL (decl) = 1;
4854 : :
4855 : 2735 : init = build_string (length + 1, name);
4856 : 2735 : free (CONST_CAST (char *, name));
4857 : 2735 : TREE_TYPE (init) = type;
4858 : 2735 : DECL_INITIAL (decl) = init;
4859 : :
4860 : 2735 : TREE_USED (decl) = 1;
4861 : :
4862 : 2735 : if (current_function_decl
4863 : : /* For invalid programs like this:
4864 : :
4865 : : void foo()
4866 : : const char* p = __FUNCTION__;
4867 : :
4868 : : the __FUNCTION__ is believed to appear in K&R style function
4869 : : parameter declarator. In that case we still don't have
4870 : : function_scope. */
4871 : 2728 : && current_function_scope)
4872 : : {
4873 : 2720 : DECL_CONTEXT (decl) = current_function_decl;
4874 : 2720 : bind (id, decl, current_function_scope,
4875 : : /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4876 : : }
4877 : :
4878 : 2735 : finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4879 : :
4880 : 2735 : return decl;
4881 : : }
4882 : :
4883 : : tree
4884 : 324374402 : c_builtin_function (tree decl)
4885 : : {
4886 : 324374402 : tree type = TREE_TYPE (decl);
4887 : 324374402 : tree id = DECL_NAME (decl);
4888 : :
4889 : 324374402 : const char *name = IDENTIFIER_POINTER (id);
4890 : 324374402 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4891 : :
4892 : : /* Should never be called on a symbol with a preexisting meaning. */
4893 : 324374402 : gcc_assert (!I_SYMBOL_BINDING (id));
4894 : :
4895 : 324374402 : bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4896 : : UNKNOWN_LOCATION);
4897 : :
4898 : : /* Builtins in the implementation namespace are made visible without
4899 : : needing to be explicitly declared. See push_file_scope. */
4900 : 324374402 : if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4901 : : {
4902 : 220831029 : DECL_CHAIN (decl) = visible_builtins;
4903 : 220831029 : visible_builtins = decl;
4904 : : }
4905 : :
4906 : 324374402 : return decl;
4907 : : }
4908 : :
4909 : : tree
4910 : 10380606 : c_builtin_function_ext_scope (tree decl)
4911 : : {
4912 : 10380606 : tree type = TREE_TYPE (decl);
4913 : 10380606 : tree id = DECL_NAME (decl);
4914 : :
4915 : 10380606 : const char *name = IDENTIFIER_POINTER (id);
4916 : 10380606 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4917 : :
4918 : 10380606 : if (external_scope)
4919 : 10197298 : bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4920 : : UNKNOWN_LOCATION);
4921 : :
4922 : : /* Builtins in the implementation namespace are made visible without
4923 : : needing to be explicitly declared. See push_file_scope. */
4924 : 10380606 : if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4925 : : {
4926 : 10380606 : DECL_CHAIN (decl) = visible_builtins;
4927 : 10380606 : visible_builtins = decl;
4928 : : }
4929 : :
4930 : 10380606 : return decl;
4931 : : }
4932 : :
4933 : : /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4934 : :
4935 : : tree
4936 : 0 : c_simulate_builtin_function_decl (tree decl)
4937 : : {
4938 : 0 : tree type = TREE_TYPE (decl);
4939 : 0 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4940 : 0 : return pushdecl (decl);
4941 : : }
4942 : :
4943 : : /* Warn about attributes in a context where they are unused
4944 : : (attribute-declarations, except for the "fallthrough" case, and
4945 : : attributes on statements). */
4946 : :
4947 : : void
4948 : 40524063 : c_warn_unused_attributes (tree attrs)
4949 : : {
4950 : 40524108 : for (tree t = attrs; t != NULL_TREE; t = TREE_CHAIN (t))
4951 : 45 : if (get_attribute_namespace (t) == NULL_TREE)
4952 : : /* The specifications of standard attributes mean this is a
4953 : : constraint violation. */
4954 : 29 : pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4955 : : get_attribute_name (t));
4956 : 16 : else if (!attribute_ignored_p (t))
4957 : 12 : warning (OPT_Wattributes, "%qE attribute ignored",
4958 : : get_attribute_name (t));
4959 : 40524063 : }
4960 : :
4961 : : /* Warn for standard attributes being applied to a type that is not
4962 : : being defined, where that is a constraint violation, and return a
4963 : : list of attributes with them removed. */
4964 : :
4965 : : tree
4966 : 423106385 : c_warn_type_attributes (tree type, tree attrs)
4967 : : {
4968 : 423106385 : tree *attr_ptr = &attrs;
4969 : 423128992 : while (*attr_ptr)
4970 : 22607 : if (get_attribute_namespace (*attr_ptr) == NULL_TREE)
4971 : : {
4972 : 89 : if (TREE_CODE (type) == FUNCTION_TYPE)
4973 : : {
4974 : 65 : tree name = get_attribute_name (*attr_ptr);
4975 : : /* [[unsequenced]] and [[reproducible]] is fine on function
4976 : : types that aren't being defined. */
4977 : 65 : if (is_attribute_p ("unsequenced", name)
4978 : 65 : || is_attribute_p ("reproducible", name))
4979 : : {
4980 : 60 : attr_ptr = &TREE_CHAIN (*attr_ptr);
4981 : 60 : continue;
4982 : : }
4983 : : }
4984 : 29 : pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4985 : : get_attribute_name (*attr_ptr));
4986 : 29 : *attr_ptr = TREE_CHAIN (*attr_ptr);
4987 : : }
4988 : : else
4989 : 22518 : attr_ptr = &TREE_CHAIN (*attr_ptr);
4990 : 423106385 : return attrs;
4991 : : }
4992 : :
4993 : : /* Called when a declaration is seen that contains no names to declare.
4994 : : If its type is a reference to a structure, union or enum inherited
4995 : : from a containing scope, shadow that tag name for the current scope
4996 : : with a forward reference.
4997 : : If its type defines a new named structure or union
4998 : : or defines an enum, it is valid but we need not do anything here.
4999 : : Otherwise, it is an error. */
5000 : :
5001 : : void
5002 : 489281 : shadow_tag (const struct c_declspecs *declspecs)
5003 : : {
5004 : 489281 : shadow_tag_warned (declspecs, 0);
5005 : 489281 : }
5006 : :
5007 : : /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
5008 : : but no pedwarn. */
5009 : : void
5010 : 489351 : shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
5011 : : {
5012 : 489351 : bool found_tag = false;
5013 : :
5014 : 489351 : if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
5015 : : {
5016 : 489237 : tree value = declspecs->type;
5017 : 489237 : enum tree_code code = TREE_CODE (value);
5018 : :
5019 : 489237 : if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
5020 : : /* Used to test also that TYPE_SIZE (value) != 0.
5021 : : That caused warning for `struct foo;' at top level in the file. */
5022 : : {
5023 : 489186 : tree name = TYPE_NAME (value);
5024 : 489186 : tree t;
5025 : :
5026 : 489186 : found_tag = true;
5027 : :
5028 : 489186 : if (declspecs->restrict_p)
5029 : : {
5030 : 2 : error ("invalid use of %<restrict%>");
5031 : 2 : warned = 1;
5032 : : }
5033 : :
5034 : 489186 : if (in_underspecified_init)
5035 : : {
5036 : : /* This can only occur with extensions such as statement
5037 : : expressions, but is still appropriate as an error to
5038 : : avoid types declared in such a context escaping to
5039 : : the type of an auto variable. */
5040 : 1 : error ("%qT declared in underspecified object initializer",
5041 : : value);
5042 : 1 : warned = 1;
5043 : : }
5044 : :
5045 : 489186 : if (name == NULL_TREE)
5046 : : {
5047 : 57354 : if (warned != 1 && code != ENUMERAL_TYPE)
5048 : : /* Empty unnamed enum OK */
5049 : : {
5050 : 19 : pedwarn (input_location, 0,
5051 : : "unnamed struct/union that defines no instances");
5052 : 19 : warned = 1;
5053 : : }
5054 : : }
5055 : 431832 : else if (declspecs->typespec_kind != ctsk_tagdef
5056 : 87064 : && declspecs->typespec_kind != ctsk_tagfirstref
5057 : 25911 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5058 : 25903 : && declspecs->storage_class != csc_none)
5059 : : {
5060 : 2 : if (warned != 1)
5061 : 2 : pedwarn (input_location, 0,
5062 : : "empty declaration with storage class specifier "
5063 : : "does not redeclare tag");
5064 : 2 : warned = 1;
5065 : 2 : pending_xref_error ();
5066 : : }
5067 : 431830 : else if (declspecs->typespec_kind != ctsk_tagdef
5068 : 87062 : && declspecs->typespec_kind != ctsk_tagfirstref
5069 : 25909 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5070 : 25901 : && (declspecs->const_p
5071 : 25898 : || declspecs->volatile_p
5072 : 25898 : || declspecs->atomic_p
5073 : 25898 : || declspecs->restrict_p
5074 : 25898 : || declspecs->address_space))
5075 : : {
5076 : 3 : if (warned != 1)
5077 : 3 : pedwarn (input_location, 0,
5078 : : "empty declaration with type qualifier "
5079 : : "does not redeclare tag");
5080 : 3 : warned = 1;
5081 : 3 : pending_xref_error ();
5082 : : }
5083 : 431827 : else if (declspecs->typespec_kind != ctsk_tagdef
5084 : 87059 : && declspecs->typespec_kind != ctsk_tagfirstref
5085 : 25906 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5086 : 25898 : && declspecs->alignas_p)
5087 : : {
5088 : 1 : if (warned != 1)
5089 : 1 : pedwarn (input_location, 0,
5090 : : "empty declaration with %<_Alignas%> "
5091 : : "does not redeclare tag");
5092 : 1 : warned = 1;
5093 : 1 : pending_xref_error ();
5094 : : }
5095 : 431826 : else if (declspecs->typespec_kind != ctsk_tagdef
5096 : 87058 : && declspecs->typespec_kind != ctsk_tagfirstref
5097 : 25905 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5098 : 25897 : && code == ENUMERAL_TYPE
5099 : 15 : && !declspecs->enum_type_specifier_ref_p)
5100 : : {
5101 : 3 : bool warned_enum = false;
5102 : 3 : if (warned != 1)
5103 : 3 : warned_enum = pedwarn (input_location, OPT_Wpedantic,
5104 : : "empty declaration of %<enum%> type "
5105 : : "does not redeclare tag");
5106 : 3 : if (warned_enum)
5107 : 2 : warned = 1;
5108 : 3 : pending_xref_error ();
5109 : 3 : }
5110 : : else
5111 : : {
5112 : 431823 : pending_invalid_xref = NULL_TREE;
5113 : 431823 : t = lookup_tag (code, name, true, NULL);
5114 : :
5115 : 431823 : if (t == NULL_TREE)
5116 : : {
5117 : 2 : t = make_node (code);
5118 : 2 : if (flag_isoc23 || code == ENUMERAL_TYPE)
5119 : 1 : SET_TYPE_STRUCTURAL_EQUALITY (t);
5120 : 2 : pushtag (input_location, name, t);
5121 : : }
5122 : : }
5123 : : }
5124 : : else
5125 : : {
5126 : 51 : if (warned != 1 && !in_system_header_at (input_location))
5127 : : {
5128 : 44 : pedwarn (input_location, 0,
5129 : : "useless type name in empty declaration");
5130 : 44 : warned = 1;
5131 : : }
5132 : : }
5133 : : }
5134 : 71 : else if (warned != 1 && !in_system_header_at (input_location)
5135 : 185 : && declspecs->typedef_p)
5136 : : {
5137 : 11 : pedwarn (input_location, 0, "useless type name in empty declaration");
5138 : 11 : warned = 1;
5139 : : }
5140 : :
5141 : 489351 : pending_invalid_xref = NULL_TREE;
5142 : :
5143 : 489351 : if (declspecs->inline_p)
5144 : : {
5145 : 5 : error ("%<inline%> in empty declaration");
5146 : 5 : warned = 1;
5147 : : }
5148 : :
5149 : 489351 : if (declspecs->noreturn_p)
5150 : : {
5151 : 2 : error ("%<_Noreturn%> in empty declaration");
5152 : 2 : warned = 1;
5153 : : }
5154 : :
5155 : 489351 : if (declspecs->constexpr_p)
5156 : : {
5157 : 7 : error ("%<constexpr%> in empty declaration");
5158 : 7 : warned = 1;
5159 : : }
5160 : :
5161 : 489351 : if (current_scope == file_scope && declspecs->storage_class == csc_auto)
5162 : : {
5163 : 2 : error ("%<auto%> in file-scope empty declaration");
5164 : 2 : warned = 1;
5165 : : }
5166 : :
5167 : 489351 : if (current_scope == file_scope && declspecs->storage_class == csc_register)
5168 : : {
5169 : 2 : error ("%<register%> in file-scope empty declaration");
5170 : 2 : warned = 1;
5171 : : }
5172 : :
5173 : 489351 : if (declspecs->enum_type_specifier_ref_p && !warned)
5174 : : {
5175 : 39 : if (declspecs->storage_class != csc_none)
5176 : : {
5177 : 1 : error ("storage class specifier in empty declaration with %<enum%> "
5178 : : "underlying type");
5179 : 1 : warned = 1;
5180 : : }
5181 : 38 : else if (declspecs->thread_p)
5182 : : {
5183 : 2 : error ("%qs in empty declaration with %<enum%> underlying type",
5184 : 1 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5185 : 1 : warned = 1;
5186 : : }
5187 : 37 : else if (declspecs->const_p
5188 : 36 : || declspecs->volatile_p
5189 : 36 : || declspecs->atomic_p
5190 : 36 : || declspecs->restrict_p
5191 : 36 : || declspecs->address_space)
5192 : : {
5193 : 1 : error ("type qualifier in empty declaration with %<enum%> "
5194 : : "underlying type");
5195 : 1 : warned = 1;
5196 : : }
5197 : 36 : else if (declspecs->alignas_p)
5198 : : {
5199 : 1 : error ("%<alignas%> in empty declaration with %<enum%> "
5200 : : "underlying type");
5201 : 1 : warned = 1;
5202 : : }
5203 : : }
5204 : :
5205 : 489183 : if (!warned && !in_system_header_at (input_location)
5206 : 605748 : && declspecs->storage_class != csc_none)
5207 : : {
5208 : 12 : warning (0, "useless storage class specifier in empty declaration");
5209 : 12 : warned = 2;
5210 : : }
5211 : :
5212 : 489351 : if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
5213 : : {
5214 : 3 : warning (0, "useless %qs in empty declaration",
5215 : 3 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5216 : 3 : warned = 2;
5217 : : }
5218 : :
5219 : 3 : if (!warned
5220 : 489164 : && !in_system_header_at (input_location)
5221 : 605738 : && (declspecs->const_p
5222 : 116382 : || declspecs->volatile_p
5223 : 116382 : || declspecs->atomic_p
5224 : 116382 : || declspecs->restrict_p
5225 : 116382 : || declspecs->address_space))
5226 : : {
5227 : 8 : warning (0, "useless type qualifier in empty declaration");
5228 : 8 : warned = 2;
5229 : : }
5230 : :
5231 : 489164 : if (!warned && !in_system_header_at (input_location)
5232 : 605725 : && declspecs->alignas_p)
5233 : : {
5234 : 4 : warning (0, "useless %<_Alignas%> in empty declaration");
5235 : 4 : warned = 2;
5236 : : }
5237 : :
5238 : 489351 : if (found_tag
5239 : 489351 : && warned == 2
5240 : 23 : && (declspecs->typespec_kind == ctsk_tagref_attrs
5241 : 23 : || declspecs->typespec_kind == ctsk_tagfirstref_attrs))
5242 : : {
5243 : : /* Standard attributes after the "struct" or "union" keyword are
5244 : : only permitted when the contents of the type are defined, or
5245 : : in the form "struct-or-union attribute-specifier-sequence
5246 : : identifier;". If the ';' was not present, attributes were
5247 : : diagnosed in the parser. Here, ensure that any other useless
5248 : : elements of the declaration result in a pedwarn, not just a
5249 : : warning. Forward declarations of enum types are not part of
5250 : : standard C, but handle them the same. */
5251 : 2 : pedwarn (input_location, 0,
5252 : : "invalid use of attributes in empty declaration");
5253 : 2 : warned = 1;
5254 : : }
5255 : :
5256 : 489351 : if (warned != 1)
5257 : : {
5258 : 489177 : if (declspecs->declspecs_seen_p
5259 : 489177 : && !declspecs->non_std_attrs_seen_p)
5260 : : /* An attribute declaration (but not a fallthrough attribute
5261 : : declaration, which was handled separately); warn if there
5262 : : are any attributes being ignored (but not if the attributes
5263 : : were empty). */
5264 : 48 : c_warn_unused_attributes (declspecs->attrs);
5265 : 489129 : else if (!found_tag)
5266 : 10 : pedwarn (input_location, 0, "empty declaration");
5267 : : }
5268 : 489351 : }
5269 : :
5270 : :
5271 : : /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
5272 : : bits. SPECS represents declaration specifiers that the grammar
5273 : : only permits to contain type qualifiers and attributes. */
5274 : :
5275 : : int
5276 : 17876699 : quals_from_declspecs (const struct c_declspecs *specs)
5277 : : {
5278 : 17876699 : int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
5279 : : | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
5280 : : | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
5281 : 17876699 : | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
5282 : 17876699 : | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
5283 : 17876699 : gcc_assert (!specs->type
5284 : : && !specs->decl_attr
5285 : : && specs->typespec_word == cts_none
5286 : : && specs->storage_class == csc_none
5287 : : && !specs->typedef_p
5288 : : && !specs->explicit_signed_p
5289 : : && !specs->deprecated_p
5290 : : && !specs->unavailable_p
5291 : : && !specs->long_p
5292 : : && !specs->long_long_p
5293 : : && !specs->short_p
5294 : : && !specs->signed_p
5295 : : && !specs->unsigned_p
5296 : : && !specs->complex_p
5297 : : && !specs->inline_p
5298 : : && !specs->noreturn_p
5299 : : && !specs->thread_p);
5300 : 17876699 : return quals;
5301 : : }
5302 : :
5303 : : /* Construct an array declarator. LOC is the location of the
5304 : : beginning of the array (usually the opening brace). EXPR is the
5305 : : expression inside [], or NULL_TREE. QUALS are the type qualifiers
5306 : : inside the [] (to be applied to the pointer to which a parameter
5307 : : array is converted). STATIC_P is true if "static" is inside the
5308 : : [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
5309 : : VLA of unspecified length which is nevertheless a complete type,
5310 : : false otherwise. The field for the contained declarator is left to
5311 : : be filled in by set_array_declarator_inner. */
5312 : :
5313 : : struct c_declarator *
5314 : 1125136 : build_array_declarator (location_t loc,
5315 : : tree expr, struct c_declspecs *quals, bool static_p,
5316 : : bool vla_unspec_p)
5317 : : {
5318 : 1125136 : struct c_declarator *declarator = XOBNEW (&parser_obstack,
5319 : : struct c_declarator);
5320 : 1125136 : declarator->id_loc = loc;
5321 : 1125136 : declarator->kind = cdk_array;
5322 : 1125136 : declarator->declarator = 0;
5323 : 1125136 : declarator->u.array.dimen = expr;
5324 : 1125136 : if (quals)
5325 : : {
5326 : 931 : declarator->u.array.attrs = quals->attrs;
5327 : 931 : declarator->u.array.quals = quals_from_declspecs (quals);
5328 : : }
5329 : : else
5330 : : {
5331 : 1124205 : declarator->u.array.attrs = NULL_TREE;
5332 : 1124205 : declarator->u.array.quals = 0;
5333 : : }
5334 : 1125136 : declarator->u.array.static_p = static_p;
5335 : 1125136 : declarator->u.array.vla_unspec_p = vla_unspec_p;
5336 : 1125136 : if (static_p || quals != NULL)
5337 : 1034 : pedwarn_c90 (loc, OPT_Wpedantic,
5338 : : "ISO C90 does not support %<static%> or type "
5339 : : "qualifiers in parameter array declarators");
5340 : 1125136 : if (vla_unspec_p)
5341 : 147 : pedwarn_c90 (loc, OPT_Wpedantic,
5342 : : "ISO C90 does not support %<[*]%> array declarators");
5343 : 147 : if (vla_unspec_p)
5344 : : {
5345 : 147 : if (!current_scope->parm_flag)
5346 : : {
5347 : : /* C99 6.7.5.2p4 */
5348 : 4 : error_at (loc, "%<[*]%> not allowed in other than "
5349 : : "function prototype scope");
5350 : 4 : declarator->u.array.vla_unspec_p = false;
5351 : 4 : return NULL;
5352 : : }
5353 : 143 : current_scope->had_vla_unspec = true;
5354 : : }
5355 : : return declarator;
5356 : : }
5357 : :
5358 : : /* Set the contained declarator of an array declarator. DECL is the
5359 : : declarator, as constructed by build_array_declarator; INNER is what
5360 : : appears on the left of the []. */
5361 : :
5362 : : struct c_declarator *
5363 : 1125132 : set_array_declarator_inner (struct c_declarator *decl,
5364 : : struct c_declarator *inner)
5365 : : {
5366 : 1125132 : decl->declarator = inner;
5367 : 1125132 : return decl;
5368 : : }
5369 : :
5370 : : /* Determine whether TYPE is a one-element array type "[1]". */
5371 : : static bool
5372 : 537289 : one_element_array_type_p (const_tree type)
5373 : : {
5374 : 537289 : if (TREE_CODE (type) != ARRAY_TYPE)
5375 : : return false;
5376 : 537289 : return integer_zerop (array_type_nelts_minus_one (type));
5377 : : }
5378 : :
5379 : : /* Determine whether TYPE is a zero-length array type "[0]". */
5380 : : static bool
5381 : 537289 : zero_length_array_type_p (const_tree type)
5382 : : {
5383 : 537289 : if (TREE_CODE (type) == ARRAY_TYPE)
5384 : 537289 : if (tree type_size = TYPE_SIZE_UNIT (type))
5385 : 452722 : if ((integer_zerop (type_size))
5386 : 1816 : && TYPE_DOMAIN (type) != NULL_TREE
5387 : 454538 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5388 : : return true;
5389 : : return false;
5390 : : }
5391 : :
5392 : : /* INIT is a constructor that forms DECL's initializer. If the final
5393 : : element initializes a flexible array field, adjust the size of the
5394 : : DECL with the initializer based on whether the DECL is a union or
5395 : : a structure. */
5396 : :
5397 : : static void
5398 : 103416 : add_flexible_array_elts_to_size (tree decl, tree init)
5399 : : {
5400 : 103416 : tree elt, type;
5401 : :
5402 : 103416 : if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
5403 : 1400 : return;
5404 : :
5405 : 102016 : elt = CONSTRUCTOR_ELTS (init)->last ().value;
5406 : 102016 : type = TREE_TYPE (elt);
5407 : 102016 : if (c_flexible_array_member_type_p (type))
5408 : : {
5409 : 220 : complete_array_type (&type, elt, false);
5410 : : /* For a structure, add the size of the initializer to the DECL's
5411 : : size. */
5412 : 220 : if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
5413 : : {
5414 : 210 : DECL_SIZE (decl)
5415 : 210 : = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5416 : 210 : DECL_SIZE_UNIT (decl)
5417 : 420 : = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
5418 : : TYPE_SIZE_UNIT (type));
5419 : : }
5420 : : /* For a union, the DECL's size is the maximum of the current size
5421 : : and the size of the initializer. */
5422 : : else
5423 : : {
5424 : 10 : DECL_SIZE (decl)
5425 : 10 : = size_binop (MAX_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5426 : 10 : DECL_SIZE_UNIT (decl)
5427 : 20 : = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
5428 : : TYPE_SIZE_UNIT (type));
5429 : : }
5430 : : }
5431 : : }
5432 : :
5433 : : /* Decode a "typename", such as "int **", returning a ..._TYPE node.
5434 : : Set *EXPR, if EXPR not NULL, to any expression to be evaluated
5435 : : before the type name, and set *EXPR_CONST_OPERANDS, if
5436 : : EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
5437 : : appear in a constant expression. */
5438 : :
5439 : : tree
5440 : 118273894 : groktypename (struct c_type_name *type_name, tree *expr,
5441 : : bool *expr_const_operands)
5442 : : {
5443 : 118273894 : tree type;
5444 : 118273894 : tree attrs = type_name->specs->attrs;
5445 : :
5446 : 118273894 : type_name->specs->attrs = NULL_TREE;
5447 : :
5448 : 118273894 : type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
5449 : : false, NULL, &attrs, expr, expr_const_operands,
5450 : : DEPRECATED_NORMAL);
5451 : :
5452 : : /* Apply attributes. */
5453 : 118273894 : attrs = c_warn_type_attributes (type, attrs);
5454 : 118273894 : decl_attributes (&type, attrs, 0);
5455 : :
5456 : 118273894 : return type;
5457 : : }
5458 : :
5459 : : /* Looks up the most recent pushed declaration corresponding to DECL. */
5460 : :
5461 : : static tree
5462 : 89086803 : lookup_last_decl (tree decl)
5463 : : {
5464 : 89086803 : tree last_decl = lookup_name (DECL_NAME (decl));
5465 : 89086803 : if (!last_decl)
5466 : 87050815 : last_decl = lookup_name_in_scope (DECL_NAME (decl), external_scope);
5467 : 89086803 : return last_decl;
5468 : : }
5469 : :
5470 : : /* Wrapper for decl_attributes that adds some implicit attributes
5471 : : to VAR_DECLs or FUNCTION_DECLs. */
5472 : :
5473 : : static tree
5474 : 62218244 : c_decl_attributes (tree *node, tree attributes, int flags)
5475 : : {
5476 : : /* Add implicit "omp declare target" attribute if requested. */
5477 : 62218244 : if (vec_safe_length (current_omp_declare_target_attribute)
5478 : 9158 : && ((VAR_P (*node) && is_global_var (*node))
5479 : 2710 : || TREE_CODE (*node) == FUNCTION_DECL))
5480 : : {
5481 : 1112 : if (VAR_P (*node) && !omp_mappable_type (TREE_TYPE (*node)))
5482 : 13 : attributes = tree_cons (get_identifier ("omp declare target implicit"),
5483 : : NULL_TREE, attributes);
5484 : : else
5485 : 1099 : attributes = tree_cons (get_identifier ("omp declare target"),
5486 : : NULL_TREE, attributes);
5487 : 1112 : if (TREE_CODE (*node) == FUNCTION_DECL)
5488 : : {
5489 : 982 : int device_type
5490 : 982 : = current_omp_declare_target_attribute->last ().device_type;
5491 : 982 : device_type = MAX (device_type, 0);
5492 : 982 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
5493 : 982 : && !lookup_attribute ("omp declare target host", attributes))
5494 : 2 : attributes
5495 : 2 : = tree_cons (get_identifier ("omp declare target host"),
5496 : : NULL_TREE, attributes);
5497 : 982 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
5498 : 982 : && !lookup_attribute ("omp declare target nohost", attributes))
5499 : 2 : attributes
5500 : 2 : = tree_cons (get_identifier ("omp declare target nohost"),
5501 : : NULL_TREE, attributes);
5502 : :
5503 : 982 : int indirect
5504 : 982 : = current_omp_declare_target_attribute->last ().indirect;
5505 : 982 : if (indirect && !lookup_attribute ("omp declare target indirect",
5506 : : attributes))
5507 : 10 : attributes
5508 : 10 : = tree_cons (get_identifier ("omp declare target indirect"),
5509 : : NULL_TREE, attributes);
5510 : : }
5511 : : }
5512 : :
5513 : 62218244 : if (flag_openmp || flag_openmp_simd)
5514 : : {
5515 : : bool diagnosed = false;
5516 : 1337721 : for (tree *pa = &attributes; *pa; )
5517 : : {
5518 : 865824 : if (is_attribute_namespace_p ("omp", *pa))
5519 : : {
5520 : 60 : tree name = get_attribute_name (*pa);
5521 : 60 : if (is_attribute_p ("directive", name)
5522 : 0 : || is_attribute_p ("sequence", name)
5523 : 60 : || is_attribute_p ("decl", name))
5524 : : {
5525 : 60 : const char *p = NULL;
5526 : 60 : if (TREE_VALUE (*pa) == NULL_TREE)
5527 : 12 : p = IDENTIFIER_POINTER (name);
5528 : 108 : for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
5529 : : {
5530 : 48 : tree d = TREE_VALUE (a);
5531 : 48 : gcc_assert (TREE_CODE (d) == C_TOKEN_VEC);
5532 : 86 : if (TREE_PUBLIC (d)
5533 : 38 : && (VAR_P (*node)
5534 : 2 : || TREE_CODE (*node) == FUNCTION_DECL)
5535 : 86 : && c_maybe_parse_omp_decl (*node, d))
5536 : 38 : continue;
5537 : 10 : p = TREE_PUBLIC (d) ? "decl" : "directive";
5538 : : }
5539 : 60 : if (p && !diagnosed)
5540 : : {
5541 : 22 : error ("%<omp::%s%> not allowed to be specified in "
5542 : : "this context", p);
5543 : 22 : diagnosed = true;
5544 : : }
5545 : 60 : if (p)
5546 : : {
5547 : 22 : *pa = TREE_CHAIN (*pa);
5548 : 22 : continue;
5549 : : }
5550 : : }
5551 : : }
5552 : 865802 : pa = &TREE_CHAIN (*pa);
5553 : : }
5554 : : }
5555 : :
5556 : : /* Look up the current declaration with all the attributes merged
5557 : : so far so that attributes on the current declaration that's
5558 : : about to be pushed that conflict with the former can be detected,
5559 : : diagnosed, and rejected as appropriate. To match the C++ FE, do
5560 : : not pass an error_mark_node when we found an undeclared variable. */
5561 : 62218244 : tree last_decl = lookup_last_decl (*node);
5562 : 62218244 : if (last_decl == error_mark_node)
5563 : 19 : last_decl = NULL_TREE;
5564 : 62218244 : return decl_attributes (node, attributes, flags, last_decl);
5565 : : }
5566 : :
5567 : :
5568 : : /* Decode a declarator in an ordinary declaration or data definition.
5569 : : This is called as soon as the type information and variable name
5570 : : have been parsed, before parsing the initializer if any.
5571 : : Here we create the ..._DECL node, fill in its type,
5572 : : and (if DO_PUSH) put it on the list of decls for the current context.
5573 : : When nonnull, set *LASTLOC to the location of the prior declaration
5574 : : of the same entity if one exists.
5575 : : The ..._DECL node is returned as the value.
5576 : :
5577 : : Exception: for arrays where the length is not specified,
5578 : : the type is left null, to be filled in by `finish_decl'.
5579 : :
5580 : : Function definitions do not come here; they go to start_function
5581 : : instead. However, external and forward declarations of functions
5582 : : do go through here. Structure field declarations are done by
5583 : : grokfield and not through here. */
5584 : :
5585 : : tree
5586 : 26868582 : start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5587 : : bool initialized, tree attributes, bool do_push /* = true */,
5588 : : location_t *lastloc /* = NULL */)
5589 : : {
5590 : 26868582 : tree decl;
5591 : 26868582 : tree old_decl;
5592 : 26868582 : tree tem;
5593 : 26868582 : tree expr = NULL_TREE;
5594 : 26868582 : enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5595 : :
5596 : : /* An object declared as __attribute__((unavailable)) suppresses
5597 : : warnings and errors from __attribute__((deprecated/unavailable))
5598 : : components.
5599 : : An object declared as __attribute__((deprecated)) suppresses
5600 : : warnings of uses of other deprecated items. */
5601 : 26868582 : if (lookup_attribute ("unavailable", attributes))
5602 : : deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
5603 : 26868558 : else if (lookup_attribute ("deprecated", attributes))
5604 : 29490 : deprecated_state = DEPRECATED_SUPPRESS;
5605 : :
5606 : 26868582 : decl = grokdeclarator (declarator, declspecs,
5607 : : NORMAL, initialized, NULL, &attributes, &expr, NULL,
5608 : : deprecated_state);
5609 : 26868582 : if (!decl || decl == error_mark_node)
5610 : : return NULL_TREE;
5611 : :
5612 : 26868559 : old_decl = lookup_last_decl (decl);
5613 : :
5614 : 26868559 : if (tree lastdecl = lastloc ? old_decl : NULL_TREE)
5615 : 4428073 : if (lastdecl != error_mark_node)
5616 : 4428064 : *lastloc = DECL_SOURCE_LOCATION (lastdecl);
5617 : :
5618 : : /* Make sure the size expression is evaluated at this point. */
5619 : 26868559 : if (expr && !current_scope->parm_flag)
5620 : 12885 : add_stmt (fold_convert (void_type_node, expr));
5621 : :
5622 : 12970341 : if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5623 : 26868561 : && TREE_PUBLIC (decl))
5624 : 1 : warning (OPT_Wmain, "%q+D is usually a function", decl);
5625 : :
5626 : 14 : if (warn_missing_variable_declarations && VAR_P (decl)
5627 : 26868572 : && !DECL_EXTERNAL (decl) && TREE_PUBLIC (decl) && old_decl == NULL_TREE)
5628 : 5 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_variable_declarations,
5629 : : "no previous declaration for %qD", decl);
5630 : :
5631 : 26868559 : if (initialized)
5632 : : /* Is it valid for this decl to have an initializer at all?
5633 : : If not, set INITIALIZED to zero, which will indirectly
5634 : : tell 'finish_decl' to ignore the initializer once it is parsed. */
5635 : 6190744 : switch (TREE_CODE (decl))
5636 : : {
5637 : 6 : case TYPE_DECL:
5638 : 6 : error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5639 : 6 : initialized = false;
5640 : 6 : break;
5641 : :
5642 : 9 : case FUNCTION_DECL:
5643 : 9 : error ("function %qD is initialized like a variable", decl);
5644 : 9 : initialized = false;
5645 : 9 : break;
5646 : :
5647 : 49 : case PARM_DECL:
5648 : : /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5649 : 49 : error ("parameter %qD is initialized", decl);
5650 : 49 : initialized = false;
5651 : 49 : break;
5652 : :
5653 : 6190680 : default:
5654 : : /* Don't allow initializations for incomplete types except for
5655 : : arrays which might be completed by the initialization. */
5656 : :
5657 : : /* This can happen if the array size is an undefined macro.
5658 : : We already gave a warning, so we don't need another one. */
5659 : 6190680 : if (TREE_TYPE (decl) == error_mark_node)
5660 : : initialized = false;
5661 : 6190658 : else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5662 : : {
5663 : : /* A complete type is ok if size is fixed. If the size is
5664 : : variable, an empty initializer is OK and nonempty
5665 : : initializers will be diagnosed in the parser. */
5666 : : }
5667 : 12065 : else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5668 : : {
5669 : 3 : error ("variable %qD has initializer but incomplete type", decl);
5670 : 3 : initialized = false;
5671 : : }
5672 : : }
5673 : :
5674 : 67 : if (initialized)
5675 : : {
5676 : 6190655 : if (current_scope == file_scope)
5677 : 155493 : TREE_STATIC (decl) = 1;
5678 : :
5679 : : /* Tell 'pushdecl' this is an initialized decl
5680 : : even though we don't yet have the initializer expression.
5681 : : Also tell 'finish_decl' it may store the real initializer. */
5682 : 6190655 : DECL_INITIAL (decl) = error_mark_node;
5683 : : }
5684 : :
5685 : : /* If this is a function declaration, write a record describing it to the
5686 : : prototypes file (if requested). */
5687 : :
5688 : 26868559 : if (TREE_CODE (decl) == FUNCTION_DECL)
5689 : 13898218 : gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5690 : :
5691 : : /* ANSI specifies that a tentative definition which is not merged with
5692 : : a non-tentative definition behaves exactly like a definition with an
5693 : : initializer equal to zero. (Section 3.7.2)
5694 : :
5695 : : -fno-common gives strict ANSI behavior, though this tends to break
5696 : : a large body of code that grew up without this rule.
5697 : :
5698 : : Thread-local variables are never common, since there's no entrenched
5699 : : body of code to break, and it allows more efficient variable references
5700 : : in the presence of dynamic linking. */
5701 : :
5702 : 26868559 : if (VAR_P (decl)
5703 : 8738503 : && !initialized
5704 : 2547848 : && TREE_PUBLIC (decl)
5705 : 987059 : && !DECL_THREAD_LOCAL_P (decl)
5706 : 27853305 : && !flag_no_common)
5707 : 128 : DECL_COMMON (decl) = 1;
5708 : :
5709 : : /* Set attributes here so if duplicate decl, will have proper attributes. */
5710 : 26868559 : c_decl_attributes (&decl, attributes, 0);
5711 : :
5712 : : /* Handle gnu_inline attribute. */
5713 : 26868558 : if (declspecs->inline_p
5714 : 1015 : && !flag_gnu89_inline
5715 : 983 : && TREE_CODE (decl) == FUNCTION_DECL
5716 : 26869533 : && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
5717 : 952 : || current_function_decl))
5718 : : {
5719 : 46 : if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5720 : : ;
5721 : 37 : else if (declspecs->storage_class != csc_static)
5722 : 37 : DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5723 : : }
5724 : :
5725 : 26868558 : if (TREE_CODE (decl) == FUNCTION_DECL
5726 : 13898217 : && DECL_DECLARED_INLINE_P (decl)
5727 : 1005 : && DECL_UNINLINABLE (decl)
5728 : 26868560 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5729 : : {
5730 : 2 : auto_urlify_attributes sentinel;
5731 : 2 : warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5732 : : decl, "noinline");
5733 : 2 : }
5734 : :
5735 : : /* C99 6.7.4p3: An inline definition of a function with external
5736 : : linkage shall not contain a definition of a modifiable object
5737 : : with static storage duration... */
5738 : 26868558 : if (VAR_P (decl)
5739 : 8738503 : && current_scope != file_scope
5740 : 7589043 : && TREE_STATIC (decl)
5741 : 78729 : && !TREE_READONLY (decl)
5742 : 76164 : && DECL_DECLARED_INLINE_P (current_function_decl)
5743 : 26868628 : && DECL_EXTERNAL (current_function_decl))
5744 : 7 : record_inline_static (input_location, current_function_decl,
5745 : : decl, csi_modifiable);
5746 : :
5747 : 26868558 : if (c_dialect_objc ()
5748 : 0 : && VAR_OR_FUNCTION_DECL_P (decl))
5749 : 0 : objc_check_global_decl (decl);
5750 : :
5751 : : /* Add this decl to the current scope.
5752 : : TEM may equal DECL or it may be a previous decl of the same name. */
5753 : 26868558 : if (do_push)
5754 : : {
5755 : 26868221 : tem = pushdecl (decl);
5756 : :
5757 : 26868221 : if (initialized && DECL_EXTERNAL (tem))
5758 : : {
5759 : 27 : DECL_EXTERNAL (tem) = 0;
5760 : 27 : TREE_STATIC (tem) = 1;
5761 : : }
5762 : :
5763 : 26868221 : return tem;
5764 : : }
5765 : : else
5766 : 337 : return decl;
5767 : : }
5768 : :
5769 : : /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5770 : : DECL or the non-array element type if DECL is an uninitialized array.
5771 : : If that type has a const member, diagnose this. */
5772 : :
5773 : : static void
5774 : 7 : diagnose_uninitialized_cst_member (tree decl, tree type)
5775 : : {
5776 : 7 : tree field;
5777 : 17 : for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5778 : : {
5779 : 10 : tree field_type;
5780 : 10 : if (TREE_CODE (field) != FIELD_DECL)
5781 : 0 : continue;
5782 : 10 : field_type = strip_array_types (TREE_TYPE (field));
5783 : :
5784 : 10 : if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5785 : : {
5786 : 5 : auto_diagnostic_group d;
5787 : 5 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5788 : : "uninitialized const member in %qT is invalid in C++",
5789 : 5 : strip_array_types (TREE_TYPE (decl))))
5790 : 5 : inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5791 : 5 : }
5792 : :
5793 : 10 : if (RECORD_OR_UNION_TYPE_P (field_type))
5794 : 2 : diagnose_uninitialized_cst_member (decl, field_type);
5795 : : }
5796 : 7 : }
5797 : :
5798 : : /* Finish processing of a declaration;
5799 : : install its initial value.
5800 : : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5801 : : If the length of an array type is not known before,
5802 : : it must be determined now, from the initial value, or it is an error.
5803 : :
5804 : : INIT_LOC is the location of the initial value. */
5805 : :
5806 : : void
5807 : 151192045 : finish_decl (tree decl, location_t init_loc, tree init,
5808 : : tree origtype, tree asmspec_tree)
5809 : : {
5810 : 151192045 : tree type;
5811 : 151192045 : bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5812 : 151192045 : const char *asmspec = 0;
5813 : :
5814 : : /* If a name was specified, get the string. */
5815 : 142450754 : if (VAR_OR_FUNCTION_DECL_P (decl)
5816 : 165090262 : && DECL_FILE_SCOPE_P (decl))
5817 : 15049194 : asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5818 : 151192045 : if (asmspec_tree)
5819 : 855415 : asmspec = TREE_STRING_POINTER (asmspec_tree);
5820 : :
5821 : 151192045 : if (VAR_P (decl)
5822 : 8741291 : && TREE_STATIC (decl)
5823 : 152251424 : && global_bindings_p ())
5824 : : /* So decl is a global variable. Record the types it uses
5825 : : so that we can decide later to emit debug info for them. */
5826 : 977645 : record_types_used_by_current_var_decl (decl);
5827 : :
5828 : : /* If `start_decl' didn't like having an initialization, ignore it now. */
5829 : 151192045 : if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5830 : : init = NULL_TREE;
5831 : :
5832 : : /* Don't crash if parm is initialized. */
5833 : 151192045 : if (TREE_CODE (decl) == PARM_DECL)
5834 : : init = NULL_TREE;
5835 : :
5836 : 31007094 : if (init)
5837 : 6193444 : store_init_value (init_loc, decl, init, origtype);
5838 : :
5839 : 151192045 : if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
5840 : 0 : || TREE_CODE (decl) == FIELD_DECL))
5841 : 0 : objc_check_decl (decl);
5842 : :
5843 : 151192045 : type = TREE_TYPE (decl);
5844 : :
5845 : : /* Deduce size of array from initialization, if not already known.
5846 : : This is only needed for an initialization in the current scope;
5847 : : it must not be done for a file-scope initialization of a
5848 : : declaration with external linkage, redeclared in an inner scope
5849 : : with the outer declaration shadowed in an intermediate scope. */
5850 : 151192045 : if (TREE_CODE (type) == ARRAY_TYPE
5851 : 845783 : && TYPE_DOMAIN (type) == NULL_TREE
5852 : 19649 : && TREE_CODE (decl) != TYPE_DECL
5853 : 151211598 : && !(TREE_PUBLIC (decl) && current_scope != file_scope))
5854 : : {
5855 : 19367 : bool do_default
5856 : 19367 : = (TREE_STATIC (decl)
5857 : : /* Even if pedantic, an external linkage array
5858 : : may have incomplete type at first. */
5859 : 19367 : ? pedantic && !TREE_PUBLIC (decl)
5860 : 12292 : : !DECL_EXTERNAL (decl));
5861 : 19367 : int failure
5862 : 19367 : = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5863 : : do_default);
5864 : :
5865 : : /* Get the completed type made by complete_array_type. */
5866 : 19367 : type = TREE_TYPE (decl);
5867 : :
5868 : 19367 : switch (failure)
5869 : : {
5870 : 0 : case 1:
5871 : 0 : error ("initializer fails to determine size of %q+D", decl);
5872 : 0 : break;
5873 : :
5874 : 7330 : case 2:
5875 : 7330 : if (do_default)
5876 : 1 : error ("array size missing in %q+D", decl);
5877 : : break;
5878 : :
5879 : 3 : case 3:
5880 : 3 : error ("zero or negative size array %q+D", decl);
5881 : 3 : break;
5882 : :
5883 : 12034 : case 0:
5884 : : /* For global variables, update the copy of the type that
5885 : : exists in the binding. */
5886 : 12034 : if (TREE_PUBLIC (decl))
5887 : : {
5888 : 3741 : struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5889 : 7482 : while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5890 : 3741 : b_ext = b_ext->shadowed;
5891 : 3741 : if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5892 : : {
5893 : 3741 : if (b_ext->u.type && comptypes (b_ext->u.type, type))
5894 : 176 : b_ext->u.type = composite_type (b_ext->u.type, type);
5895 : : else
5896 : 3565 : b_ext->u.type = type;
5897 : : }
5898 : : }
5899 : : break;
5900 : :
5901 : 0 : default:
5902 : 0 : gcc_unreachable ();
5903 : : }
5904 : :
5905 : 19367 : if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
5906 : 11197 : TREE_TYPE (DECL_INITIAL (decl)) = type;
5907 : :
5908 : 19367 : relayout_decl (decl);
5909 : : }
5910 : :
5911 : : /* Look for braced array initializers for character arrays and
5912 : : recursively convert them into STRING_CSTs. */
5913 : 151192045 : if (tree init = DECL_INITIAL (decl))
5914 : 126380786 : DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
5915 : :
5916 : 151192045 : if (VAR_P (decl))
5917 : : {
5918 : 8741291 : if (init && TREE_CODE (init) == CONSTRUCTOR)
5919 : 103416 : add_flexible_array_elts_to_size (decl, init);
5920 : :
5921 : 8741291 : complete_flexible_array_elts (DECL_INITIAL (decl));
5922 : :
5923 : 8741291 : if (is_global_var (decl))
5924 : : {
5925 : 1232498 : type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5926 : 1232498 : ? TCTX_THREAD_STORAGE
5927 : : : TCTX_STATIC_STORAGE);
5928 : 1232498 : if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
5929 : 0 : TREE_TYPE (decl) = error_mark_node;
5930 : : }
5931 : :
5932 : 8749289 : if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5933 : 8749190 : && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5934 : 97 : layout_decl (decl, 0);
5935 : :
5936 : 8741291 : if (DECL_SIZE (decl) == NULL_TREE
5937 : : /* Don't give an error if we already gave one earlier. */
5938 : 7901 : && TREE_TYPE (decl) != error_mark_node
5939 : 8749093 : && (TREE_STATIC (decl)
5940 : : /* A static variable with an incomplete type
5941 : : is an error if it is initialized.
5942 : : Also if it is not file scope.
5943 : : Also if it is thread-local (in C23).
5944 : : Otherwise, let it through, but if it is not `extern'
5945 : : then it may cause an error message later. */
5946 : 244 : ? (DECL_INITIAL (decl) != NULL_TREE
5947 : 244 : || !DECL_FILE_SCOPE_P (decl)
5948 : 244 : || (flag_isoc23 && DECL_THREAD_LOCAL_P (decl)))
5949 : : /* An automatic variable with an incomplete type
5950 : : is an error. */
5951 : 7558 : : !DECL_EXTERNAL (decl)))
5952 : : {
5953 : 13 : error ("storage size of %q+D isn%'t known", decl);
5954 : 13 : TREE_TYPE (decl) = error_mark_node;
5955 : : }
5956 : :
5957 : 17293840 : if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5958 : 8472214 : || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5959 : 282223 : && DECL_SIZE (decl) == NULL_TREE
5960 : 8741529 : && TREE_STATIC (decl))
5961 : 95 : incomplete_record_decls.safe_push (decl);
5962 : :
5963 : 8741291 : if (is_global_var (decl)
5964 : 1232498 : && DECL_SIZE (decl) != NULL_TREE
5965 : 9965927 : && TREE_TYPE (decl) != error_mark_node)
5966 : : {
5967 : 1224618 : if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5968 : 1224615 : constant_expression_warning (DECL_SIZE (decl));
5969 : : else
5970 : : {
5971 : 3 : error ("storage size of %q+D isn%'t constant", decl);
5972 : 3 : TREE_TYPE (decl) = error_mark_node;
5973 : : }
5974 : : }
5975 : :
5976 : 8741291 : if (TREE_USED (type))
5977 : : {
5978 : 4 : TREE_USED (decl) = 1;
5979 : 4 : DECL_READ_P (decl) = 1;
5980 : : }
5981 : : }
5982 : :
5983 : : /* If this is a function and an assembler name is specified, reset DECL_RTL
5984 : : so we can give it its new name. Also, update builtin_decl if it
5985 : : was a normal built-in. */
5986 : 151192045 : if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5987 : : {
5988 : 842407 : if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5989 : 71086 : set_builtin_user_assembler_name (decl, asmspec);
5990 : 842407 : set_user_assembler_name (decl, asmspec);
5991 : : }
5992 : :
5993 : : /* If #pragma weak was used, mark the decl weak now. */
5994 : 151192045 : maybe_apply_pragma_weak (decl);
5995 : :
5996 : : /* Output the assembler code and/or RTL code for variables and functions,
5997 : : unless the type is an undefined structure or union.
5998 : : If not, it will get done when the type is completed. */
5999 : :
6000 : 151192045 : if (VAR_OR_FUNCTION_DECL_P (decl))
6001 : : {
6002 : : /* Determine the ELF visibility. */
6003 : 22639508 : if (TREE_PUBLIC (decl))
6004 : 15024827 : c_determine_visibility (decl);
6005 : :
6006 : : /* This is a no-op in c-lang.cc or something real in objc-act.cc. */
6007 : 22639508 : if (c_dialect_objc ())
6008 : 0 : objc_check_decl (decl);
6009 : :
6010 : 22639508 : if (asmspec)
6011 : : {
6012 : : /* If this is not a static variable, issue a warning.
6013 : : It doesn't make any sense to give an ASMSPEC for an
6014 : : ordinary, non-register local variable. Historically,
6015 : : GCC has accepted -- but ignored -- the ASMSPEC in
6016 : : this case. */
6017 : 856450 : if (!DECL_FILE_SCOPE_P (decl)
6018 : 1035 : && VAR_P (decl)
6019 : 1035 : && !C_DECL_REGISTER (decl)
6020 : 855423 : && !TREE_STATIC (decl))
6021 : 1 : warning (0, "ignoring %<asm%> specifier for non-static local "
6022 : : "variable %q+D", decl);
6023 : : else
6024 : 855414 : set_user_assembler_name (decl, asmspec);
6025 : : }
6026 : :
6027 : 22639508 : if (DECL_FILE_SCOPE_P (decl))
6028 : : {
6029 : 15049194 : if (DECL_INITIAL (decl) == NULL_TREE
6030 : 15049194 : || DECL_INITIAL (decl) == error_mark_node)
6031 : : /* Don't output anything
6032 : : when a tentative file-scope definition is seen.
6033 : : But at end of compilation, do output code for them. */
6034 : 14893670 : DECL_DEFER_OUTPUT (decl) = 1;
6035 : 15049194 : if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
6036 : 68 : DECL_HARD_REGISTER (decl) = 1;
6037 : 15049194 : rest_of_decl_compilation (decl, true, 0);
6038 : :
6039 : 15049194 : if (TREE_CODE (decl) == FUNCTION_DECL)
6040 : : {
6041 : 13898154 : tree parms = DECL_ARGUMENTS (decl);
6042 : 13898154 : const bool builtin = fndecl_built_in_p (decl);
6043 : 13898154 : if (tree access = build_attr_access_from_parms (parms, !builtin))
6044 : 338937 : decl_attributes (&decl, access, 0);
6045 : : }
6046 : : }
6047 : : else
6048 : : {
6049 : : /* In conjunction with an ASMSPEC, the `register'
6050 : : keyword indicates that we should place the variable
6051 : : in a particular register. */
6052 : 7590314 : if (asmspec && C_DECL_REGISTER (decl))
6053 : : {
6054 : 1027 : DECL_HARD_REGISTER (decl) = 1;
6055 : : /* This cannot be done for a structure with volatile
6056 : : fields, on which DECL_REGISTER will have been
6057 : : reset. */
6058 : 1027 : if (!DECL_REGISTER (decl))
6059 : 1 : error ("cannot put object with volatile field into register");
6060 : : }
6061 : :
6062 : 7590314 : if (TREE_CODE (decl) != FUNCTION_DECL)
6063 : : {
6064 : : /* If we're building a variable sized type, and we might be
6065 : : reachable other than via the top of the current binding
6066 : : level, then create a new BIND_EXPR so that we deallocate
6067 : : the object at the right time. */
6068 : : /* Note that DECL_SIZE can be null due to errors. */
6069 : 7590251 : if (DECL_SIZE (decl)
6070 : 7590212 : && !TREE_CONSTANT (DECL_SIZE (decl))
6071 : 7597583 : && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
6072 : : {
6073 : 1336 : tree bind;
6074 : 1336 : bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6075 : 1336 : TREE_SIDE_EFFECTS (bind) = 1;
6076 : 1336 : add_stmt (bind);
6077 : 1336 : BIND_EXPR_BODY (bind) = push_stmt_list ();
6078 : : }
6079 : 7590251 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
6080 : : DECL_EXPR, decl));
6081 : : }
6082 : : }
6083 : :
6084 : :
6085 : 22639508 : if (!DECL_FILE_SCOPE_P (decl))
6086 : : {
6087 : : /* Recompute the RTL of a local array now
6088 : : if it used to be an incomplete type. */
6089 : 7590314 : if (was_incomplete && !is_global_var (decl))
6090 : : {
6091 : : /* If we used it already as memory, it must stay in memory. */
6092 : 5188 : TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6093 : : /* If it's still incomplete now, no init will save it. */
6094 : 5188 : if (DECL_SIZE (decl) == NULL_TREE)
6095 : 102 : DECL_INITIAL (decl) = NULL_TREE;
6096 : : }
6097 : : }
6098 : : }
6099 : :
6100 : 151192045 : if (TREE_CODE (decl) == TYPE_DECL)
6101 : : {
6102 : 4337120 : if (!DECL_FILE_SCOPE_P (decl)
6103 : 4337120 : && c_type_variably_modified_p (TREE_TYPE (decl)))
6104 : 5313 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6105 : :
6106 : 8290426 : rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
6107 : : }
6108 : :
6109 : : /* Install a cleanup (aka destructor) if one was given. */
6110 : 151192045 : if (VAR_P (decl) && !TREE_STATIC (decl))
6111 : : {
6112 : 7681909 : tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
6113 : 7681909 : if (attr)
6114 : : {
6115 : 95 : tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
6116 : 95 : tree cleanup_decl = lookup_name (cleanup_id);
6117 : 95 : tree cleanup;
6118 : 95 : vec<tree, va_gc> *v;
6119 : :
6120 : : /* Build "cleanup(&decl)" for the destructor. */
6121 : 95 : cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
6122 : 95 : vec_alloc (v, 1);
6123 : 95 : v->quick_push (cleanup);
6124 : 95 : cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
6125 : 95 : vNULL, cleanup_decl, v, NULL);
6126 : 95 : vec_free (v);
6127 : :
6128 : : /* Don't warn about decl unused; the cleanup uses it. */
6129 : 95 : TREE_USED (decl) = 1;
6130 : 95 : TREE_USED (cleanup_decl) = 1;
6131 : 95 : DECL_READ_P (decl) = 1;
6132 : :
6133 : 95 : push_cleanup (decl, cleanup, false);
6134 : : }
6135 : : }
6136 : :
6137 : 151192045 : if (warn_cxx_compat
6138 : 21962 : && VAR_P (decl)
6139 : 7351 : && !DECL_EXTERNAL (decl)
6140 : 151198956 : && DECL_INITIAL (decl) == NULL_TREE)
6141 : : {
6142 : 2302 : type = strip_array_types (type);
6143 : 2302 : if (TREE_READONLY (decl))
6144 : 6 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6145 : : "uninitialized %<const %D%> is invalid in C++", decl);
6146 : 2296 : else if (RECORD_OR_UNION_TYPE_P (type)
6147 : 2296 : && C_TYPE_FIELDS_READONLY (type))
6148 : 5 : diagnose_uninitialized_cst_member (decl, type);
6149 : : }
6150 : :
6151 : 151192045 : if (flag_openmp
6152 : 427215 : && VAR_P (decl)
6153 : 151215734 : && lookup_attribute ("omp declare target implicit",
6154 : 23689 : DECL_ATTRIBUTES (decl)))
6155 : : {
6156 : 13 : DECL_ATTRIBUTES (decl)
6157 : 13 : = remove_attribute ("omp declare target implicit",
6158 : 13 : DECL_ATTRIBUTES (decl));
6159 : 13 : if (!omp_mappable_type (TREE_TYPE (decl)))
6160 : 9 : error ("%q+D in declare target directive does not have mappable type",
6161 : : decl);
6162 : 4 : else if (!lookup_attribute ("omp declare target",
6163 : 4 : DECL_ATTRIBUTES (decl))
6164 : 8 : && !lookup_attribute ("omp declare target link",
6165 : 4 : DECL_ATTRIBUTES (decl)))
6166 : : {
6167 : 4 : DECL_ATTRIBUTES (decl)
6168 : 4 : = tree_cons (get_identifier ("omp declare target"),
6169 : 4 : NULL_TREE, DECL_ATTRIBUTES (decl));
6170 : 4 : symtab_node *node = symtab_node::get (decl);
6171 : 4 : if (node != NULL)
6172 : : {
6173 : 4 : node->offloadable = 1;
6174 : 4 : if (ENABLE_OFFLOADING)
6175 : : {
6176 : : g->have_offload = true;
6177 : : if (is_a <varpool_node *> (node))
6178 : : vec_safe_push (offload_vars, decl);
6179 : : }
6180 : : }
6181 : : }
6182 : : }
6183 : :
6184 : : /* This is the last point we can lower alignment so give the target the
6185 : : chance to do so. */
6186 : 151192045 : if (VAR_P (decl)
6187 : 8741291 : && !is_global_var (decl)
6188 : 158700838 : && !DECL_HARD_REGISTER (decl))
6189 : 7507766 : targetm.lower_local_decl_alignment (decl);
6190 : :
6191 : 151192045 : invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
6192 : 151192045 : }
6193 : :
6194 : : /* Given a parsed parameter declaration, decode it into a PARM_DECL.
6195 : : EXPR is NULL or a pointer to an expression that needs to be
6196 : : evaluated for the side effects of array size expressions in the
6197 : : parameters. */
6198 : :
6199 : : tree
6200 : 0 : grokparm (const struct c_parm *parm, tree *expr)
6201 : : {
6202 : 0 : tree attrs = parm->attrs;
6203 : 0 : tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
6204 : 0 : NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
6205 : :
6206 : 0 : decl_attributes (&decl, attrs, 0);
6207 : :
6208 : 0 : return decl;
6209 : : }
6210 : :
6211 : : /* Return attribute "arg spec" corresponding to an array/VLA parameter
6212 : : described by PARM, concatenated onto attributes ATTRS.
6213 : : The spec consists of one dollar symbol for each specified variable
6214 : : bound, one asterisk for each unspecified variable bound, followed
6215 : : by at most one specification of the most significant bound of
6216 : : an ordinary array parameter. For ordinary arrays the specification
6217 : : is either the constant bound itself, or the space character for
6218 : : an array with an unspecified bound (the [] form). Finally, a chain
6219 : : of specified variable bounds is appended to the spec, starting with
6220 : : the most significant bound. For example, the PARM T a[2][m][3][n]
6221 : : will produce __attribute__((arg spec ("[$$2]", m, n)).
6222 : : For T a typedef for an array with variable bounds, the bounds are
6223 : : included in the specification in the expected order.
6224 : : No "arg spec" is created for parameters of pointer types, making
6225 : : a distinction between T(*)[N] (or, equivalently, T[][N]) and
6226 : : the T[M][N] form, all of which have the same type and are represented
6227 : : the same, but only the last of which gets an "arg spec" describing
6228 : : the most significant bound M. */
6229 : :
6230 : : static tree
6231 : 120162295 : get_parm_array_spec (const struct c_parm *parm, tree attrs)
6232 : : {
6233 : : /* The attribute specification string, minor bound first. */
6234 : 120162295 : std::string spec;
6235 : :
6236 : : /* A list of VLA variable bounds, major first, or null if unspecified
6237 : : or not a VLA. */
6238 : 120162295 : tree vbchain = NULL_TREE;
6239 : : /* True for a pointer parameter. */
6240 : 120162295 : bool pointer = false;
6241 : : /* True for an ordinary array with an unpecified bound. */
6242 : 120162295 : bool nobound = false;
6243 : :
6244 : : /* Create a string representation for the bounds of the array/VLA. */
6245 : 229198151 : for (c_declarator *pd = parm->declarator, *next; pd; pd = next)
6246 : : {
6247 : 120916375 : next = pd->declarator;
6248 : 120916404 : while (next && next->kind == cdk_attrs)
6249 : 29 : next = next->declarator;
6250 : :
6251 : : /* Remember if a pointer has been seen to avoid storing the constant
6252 : : bound. */
6253 : 120916375 : if (pd->kind == cdk_pointer)
6254 : 12245901 : pointer = true;
6255 : :
6256 : 120916375 : if ((pd->kind == cdk_pointer || pd->kind == cdk_function)
6257 : 12322091 : && (!next || next->kind == cdk_id))
6258 : : {
6259 : : /* Do nothing for the common case of a pointer. The fact that
6260 : : the parameter is one can be deduced from the absence of
6261 : : an arg spec for it. */
6262 : : return attrs;
6263 : : }
6264 : :
6265 : 109272161 : if (pd->kind == cdk_id)
6266 : : {
6267 : 216435000 : if (pointer
6268 : 108263778 : || !parm->specs->type
6269 : 108263778 : || TREE_CODE (parm->specs->type) != ARRAY_TYPE
6270 : 128571 : || !TYPE_DOMAIN (parm->specs->type)
6271 : 108410330 : || !TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type)))
6272 : 108153224 : continue;
6273 : :
6274 : 128552 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type));
6275 : 128552 : if (!vbchain
6276 : 128538 : && TREE_CODE (max) == INTEGER_CST)
6277 : : {
6278 : : /* Extract the upper bound from a parameter of an array type
6279 : : unless the parameter is an ordinary array of unspecified
6280 : : bound in which case a next iteration of the loop will
6281 : : exit. */
6282 : 128304 : if (spec.empty () || spec.end ()[-1] != ' ')
6283 : : {
6284 : 128304 : if (!tree_fits_shwi_p (max))
6285 : 0 : continue;
6286 : :
6287 : : /* The upper bound is the value of the largest valid
6288 : : index. */
6289 : 128304 : HOST_WIDE_INT n = tree_to_shwi (max) + 1;
6290 : 128304 : char buf[40];
6291 : 128304 : sprintf (buf, HOST_WIDE_INT_PRINT_UNSIGNED, n);
6292 : 128304 : spec += buf;
6293 : : }
6294 : 128304 : continue;
6295 : 128304 : }
6296 : :
6297 : : /* For a VLA typedef, create a list of its variable bounds and
6298 : : append it in the expected order to VBCHAIN. */
6299 : : tree tpbnds = NULL_TREE;
6300 : 1188 : for (tree type = parm->specs->type; TREE_CODE (type) == ARRAY_TYPE;
6301 : 940 : type = TREE_TYPE (type))
6302 : : {
6303 : 940 : tree nelts_minus_one = array_type_nelts_minus_one (type);
6304 : 940 : if (error_operand_p (nelts_minus_one))
6305 : : return attrs;
6306 : 940 : if (TREE_CODE (nelts_minus_one) != INTEGER_CST)
6307 : : {
6308 : : /* Each variable VLA bound is represented by the dollar
6309 : : sign. */
6310 : 908 : spec += "$";
6311 : 908 : tpbnds = tree_cons (NULL_TREE, nelts_minus_one, tpbnds);
6312 : : }
6313 : : }
6314 : 248 : tpbnds = nreverse (tpbnds);
6315 : 248 : vbchain = chainon (vbchain, tpbnds);
6316 : 248 : continue;
6317 : 248 : }
6318 : :
6319 : 990385 : if (pd->kind != cdk_array)
6320 : 677877 : continue;
6321 : :
6322 : 312508 : if (pd->u.array.vla_unspec_p)
6323 : : {
6324 : : /* Each unspecified bound is represented by a star. There
6325 : : can be any number of these in a declaration (but none in
6326 : : a definition). */
6327 : 134 : spec += '*';
6328 : 134 : continue;
6329 : : }
6330 : :
6331 : 312374 : tree nelts = pd->u.array.dimen;
6332 : 312374 : if (!nelts)
6333 : : {
6334 : : /* Ordinary array of unspecified size. There can be at most
6335 : : one for the most significant bound. Exit on the next
6336 : : iteration which determines whether or not PARM is declared
6337 : : as a pointer or an array. */
6338 : 63373 : nobound = true;
6339 : 63373 : continue;
6340 : : }
6341 : :
6342 : 249001 : if (pd->u.array.static_p)
6343 : 121 : spec += 's';
6344 : :
6345 : 249001 : if (!INTEGRAL_TYPE_P (TREE_TYPE (nelts)))
6346 : : /* Avoid invalid NELTS. */
6347 : : return attrs;
6348 : :
6349 : 248955 : STRIP_NOPS (nelts);
6350 : 248955 : nelts = c_fully_fold (nelts, false, nullptr);
6351 : 248955 : if (TREE_CODE (nelts) == INTEGER_CST)
6352 : : {
6353 : : /* Skip all constant bounds except the most significant one.
6354 : : The interior ones are included in the array type. */
6355 : 240251 : if (next && (next->kind == cdk_array || next->kind == cdk_pointer))
6356 : 3992 : continue;
6357 : :
6358 : 236259 : if (!tree_fits_uhwi_p (nelts))
6359 : : /* Bail completely on invalid bounds. */
6360 : 2 : return attrs;
6361 : :
6362 : 236257 : char buf[40];
6363 : 236257 : unsigned HOST_WIDE_INT n = tree_to_uhwi (nelts);
6364 : 236257 : sprintf (buf, HOST_WIDE_INT_PRINT_UNSIGNED, n);
6365 : 236257 : spec += buf;
6366 : 236257 : break;
6367 : : }
6368 : :
6369 : : /* Each variable VLA bound is represented by a dollar sign. */
6370 : 8704 : spec += "$";
6371 : 8704 : vbchain = tree_cons (NULL_TREE, nelts, vbchain);
6372 : : }
6373 : :
6374 : 108518033 : if (spec.empty () && !nobound)
6375 : : return attrs;
6376 : :
6377 : 428862 : spec.insert (0, "[");
6378 : 428862 : if (nobound)
6379 : : /* Ordinary array of unspecified bound is represented by a space.
6380 : : It must be last in the spec. */
6381 : 63318 : spec += ' ';
6382 : 428862 : spec += ']';
6383 : :
6384 : 428862 : tree acsstr = build_string (spec.length () + 1, spec.c_str ());
6385 : 428862 : tree args = tree_cons (NULL_TREE, acsstr, vbchain);
6386 : 428862 : tree name = get_identifier ("arg spec");
6387 : 428862 : return tree_cons (name, args, attrs);
6388 : 120162295 : }
6389 : :
6390 : : /* Given a parsed parameter declaration, decode it into a PARM_DECL
6391 : : and push that on the current scope. EXPR is a pointer to an
6392 : : expression that needs to be evaluated for the side effects of array
6393 : : size expressions in the parameters. */
6394 : :
6395 : : void
6396 : 120162295 : push_parm_decl (const struct c_parm *parm, tree *expr)
6397 : : {
6398 : 120162295 : tree attrs = parm->attrs;
6399 : 120162295 : tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
6400 : 120162295 : &attrs, expr, NULL, DEPRECATED_NORMAL);
6401 : 120162295 : if (decl && DECL_P (decl))
6402 : 120162295 : DECL_SOURCE_LOCATION (decl) = parm->loc;
6403 : :
6404 : 120162295 : attrs = get_parm_array_spec (parm, attrs);
6405 : 120162295 : decl_attributes (&decl, attrs, 0);
6406 : :
6407 : 120162295 : decl = pushdecl (decl);
6408 : :
6409 : 120162295 : finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
6410 : 120162295 : }
6411 : :
6412 : : /* Mark all the parameter declarations to date as forward decls.
6413 : : Also diagnose use of this extension. */
6414 : :
6415 : : void
6416 : 37 : mark_forward_parm_decls (void)
6417 : : {
6418 : 37 : struct c_binding *b;
6419 : :
6420 : 37 : if (pedantic && !current_scope->warned_forward_parm_decls)
6421 : : {
6422 : 4 : pedwarn (input_location, OPT_Wpedantic,
6423 : : "ISO C forbids forward parameter declarations");
6424 : 4 : current_scope->warned_forward_parm_decls = true;
6425 : : }
6426 : :
6427 : 83 : for (b = current_scope->bindings; b; b = b->prev)
6428 : 46 : if (TREE_CODE (b->decl) == PARM_DECL)
6429 : 46 : TREE_ASM_WRITTEN (b->decl) = 1;
6430 : 37 : }
6431 : :
6432 : : /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
6433 : : literal, which may be an incomplete array type completed by the
6434 : : initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
6435 : : literal. NON_CONST is true if the initializers contain something
6436 : : that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
6437 : : it is the (valid) alignment for this compound literal, as specified
6438 : : with _Alignas. SCSPECS are the storage class specifiers (C23) from the
6439 : : compound literal. */
6440 : :
6441 : : tree
6442 : 897287 : build_compound_literal (location_t loc, tree type, tree init, bool non_const,
6443 : : unsigned int alignas_align,
6444 : : struct c_declspecs *scspecs)
6445 : : {
6446 : : /* We do not use start_decl here because we have a type, not a declarator;
6447 : : and do not use finish_decl because the decl should be stored inside
6448 : : the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
6449 : 897287 : tree decl;
6450 : 897287 : tree complit;
6451 : 897287 : tree stmt;
6452 : 897287 : bool threadp = scspecs ? scspecs->thread_p : false;
6453 : 286 : enum c_storage_class storage_class = (scspecs
6454 : : ? scspecs->storage_class
6455 : : : csc_none);
6456 : :
6457 : 897287 : if (type == error_mark_node
6458 : 897271 : || init == error_mark_node)
6459 : : return error_mark_node;
6460 : :
6461 : 897218 : if (current_scope == file_scope && storage_class == csc_register)
6462 : : {
6463 : 1 : error_at (loc, "file-scope compound literal specifies %<register%>");
6464 : 1 : storage_class = csc_none;
6465 : : }
6466 : :
6467 : 897218 : if (current_scope != file_scope && threadp && storage_class == csc_none)
6468 : : {
6469 : 2 : error_at (loc, "compound literal implicitly auto and declared %qs",
6470 : 1 : scspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6471 : 1 : threadp = false;
6472 : : }
6473 : :
6474 : 897218 : decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
6475 : 897218 : DECL_EXTERNAL (decl) = 0;
6476 : 897218 : TREE_PUBLIC (decl) = 0;
6477 : 1794436 : TREE_STATIC (decl) = (current_scope == file_scope
6478 : 897218 : || storage_class == csc_static);
6479 : 897218 : DECL_CONTEXT (decl) = current_function_decl;
6480 : 897218 : TREE_USED (decl) = 1;
6481 : 897218 : DECL_READ_P (decl) = 1;
6482 : 897218 : DECL_ARTIFICIAL (decl) = 1;
6483 : 897218 : DECL_IGNORED_P (decl) = 1;
6484 : 897218 : C_DECL_COMPOUND_LITERAL_P (decl) = 1;
6485 : 1794207 : C_DECL_DECLARED_CONSTEXPR (decl) = scspecs && scspecs->constexpr_p;
6486 : 897218 : TREE_TYPE (decl) = type;
6487 : 897218 : if (threadp)
6488 : 16 : set_decl_tls_model (decl, decl_default_tls_model (decl));
6489 : 897218 : if (storage_class == csc_register)
6490 : : {
6491 : 10 : C_DECL_REGISTER (decl) = 1;
6492 : 10 : DECL_REGISTER (decl) = 1;
6493 : : }
6494 : 897218 : c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
6495 : 897218 : if (alignas_align)
6496 : : {
6497 : 3 : SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6498 : 3 : DECL_USER_ALIGN (decl) = 1;
6499 : : }
6500 : 897218 : store_init_value (loc, decl, init, NULL_TREE);
6501 : 897218 : if (current_scope != file_scope
6502 : 896971 : && TREE_STATIC (decl)
6503 : 26 : && !TREE_READONLY (decl)
6504 : 15 : && DECL_DECLARED_INLINE_P (current_function_decl)
6505 : 897224 : && DECL_EXTERNAL (current_function_decl))
6506 : 4 : record_inline_static (input_location, current_function_decl,
6507 : : decl, csi_modifiable);
6508 : :
6509 : 897218 : if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
6510 : : {
6511 : 251 : int failure = complete_array_type (&TREE_TYPE (decl),
6512 : 251 : DECL_INITIAL (decl), true);
6513 : : /* If complete_array_type returns 3, it means that the initial value of
6514 : : the compound literal is empty. Allow it with a pedwarn; in pre-C23
6515 : : modes, the empty initializer itself has been diagnosed if pedantic so
6516 : : does not need to be diagnosed again here. */
6517 : 251 : gcc_assert (failure == 0 || failure == 3);
6518 : 251 : if (failure == 3 && flag_isoc23)
6519 : 1 : pedwarn (loc, OPT_Wpedantic,
6520 : : "array of unknown size with empty initializer");
6521 : :
6522 : 251 : type = TREE_TYPE (decl);
6523 : 251 : TREE_TYPE (DECL_INITIAL (decl)) = type;
6524 : : }
6525 : :
6526 : 897218 : if (type == error_mark_node || !COMPLETE_TYPE_P (type))
6527 : : {
6528 : 19 : c_incomplete_type_error (loc, NULL_TREE, type);
6529 : 19 : return error_mark_node;
6530 : : }
6531 : :
6532 : 897199 : if (TREE_STATIC (decl)
6533 : 897199 : && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
6534 : 0 : return error_mark_node;
6535 : :
6536 : 897199 : stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
6537 : 897199 : complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
6538 : 897199 : TREE_SIDE_EFFECTS (complit) = 1;
6539 : :
6540 : 897199 : layout_decl (decl, 0);
6541 : :
6542 : 897199 : if (TREE_STATIC (decl))
6543 : : {
6544 : : /* This decl needs a name for the assembler output. */
6545 : 273 : set_compound_literal_name (decl);
6546 : 273 : DECL_DEFER_OUTPUT (decl) = 1;
6547 : 273 : DECL_COMDAT (decl) = 1;
6548 : 273 : pushdecl (decl);
6549 : 273 : rest_of_decl_compilation (decl, 1, 0);
6550 : : }
6551 : 896926 : else if (current_function_decl && !current_scope->parm_flag)
6552 : 896917 : pushdecl (decl);
6553 : :
6554 : 897199 : if (non_const)
6555 : : {
6556 : 193 : complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
6557 : 193 : C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
6558 : : }
6559 : :
6560 : : return complit;
6561 : : }
6562 : :
6563 : : /* Check the type of a compound literal. Here we just check that it
6564 : : is valid for C++. */
6565 : :
6566 : : void
6567 : 897287 : check_compound_literal_type (location_t loc, struct c_type_name *type_name)
6568 : : {
6569 : 897287 : if (warn_cxx_compat
6570 : 162 : && (type_name->specs->typespec_kind == ctsk_tagdef
6571 : 161 : || type_name->specs->typespec_kind == ctsk_tagfirstref
6572 : 160 : || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
6573 : 2 : warning_at (loc, OPT_Wc___compat,
6574 : : "defining a type in a compound literal is invalid in C++");
6575 : 897287 : }
6576 : :
6577 : : /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6578 : : replacing with appropriate values if they are invalid. */
6579 : :
6580 : : static void
6581 : 51791 : check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
6582 : : tree orig_name)
6583 : : {
6584 : 51791 : tree type_mv;
6585 : 51791 : unsigned int max_width;
6586 : 51791 : unsigned HOST_WIDE_INT w;
6587 : 51791 : const char *name = (orig_name
6588 : 94396 : ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
6589 : 9186 : : _("<anonymous>"));
6590 : :
6591 : : /* Detect and ignore out of range field width and process valid
6592 : : field widths. */
6593 : 51791 : if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
6594 : : {
6595 : 4 : error_at (loc, "bit-field %qs width not an integer constant", name);
6596 : 4 : *width = integer_one_node;
6597 : : }
6598 : : else
6599 : : {
6600 : 51787 : if (TREE_CODE (*width) != INTEGER_CST)
6601 : : {
6602 : 12 : *width = c_fully_fold (*width, false, NULL);
6603 : 12 : if (TREE_CODE (*width) == INTEGER_CST)
6604 : 6 : pedwarn (loc, OPT_Wpedantic,
6605 : : "bit-field %qs width not an integer constant expression",
6606 : : name);
6607 : : }
6608 : 51787 : if (TREE_CODE (*width) != INTEGER_CST)
6609 : : {
6610 : 6 : error_at (loc, "bit-field %qs width not an integer constant", name);
6611 : 6 : *width = integer_one_node;
6612 : : }
6613 : 51787 : constant_expression_warning (*width);
6614 : 51787 : if (tree_int_cst_sgn (*width) < 0)
6615 : : {
6616 : 2 : error_at (loc, "negative width in bit-field %qs", name);
6617 : 2 : *width = integer_one_node;
6618 : : }
6619 : 51785 : else if (integer_zerop (*width) && orig_name)
6620 : : {
6621 : 4 : error_at (loc, "zero width for bit-field %qs", name);
6622 : 4 : *width = integer_one_node;
6623 : : }
6624 : : }
6625 : :
6626 : : /* Detect invalid bit-field type. */
6627 : 51791 : if (TREE_CODE (*type) != INTEGER_TYPE
6628 : : && TREE_CODE (*type) != BOOLEAN_TYPE
6629 : : && TREE_CODE (*type) != ENUMERAL_TYPE
6630 : 51791 : && TREE_CODE (*type) != BITINT_TYPE)
6631 : : {
6632 : 6 : error_at (loc, "bit-field %qs has invalid type", name);
6633 : 6 : *type = unsigned_type_node;
6634 : : }
6635 : :
6636 : 51791 : if (TYPE_WARN_IF_NOT_ALIGN (*type))
6637 : : {
6638 : 1 : error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6639 : : name);
6640 : 1 : *type = unsigned_type_node;
6641 : : }
6642 : :
6643 : 51791 : type_mv = TYPE_MAIN_VARIANT (*type);
6644 : 51791 : if (!in_system_header_at (input_location)
6645 : 29289 : && type_mv != integer_type_node
6646 : 26574 : && type_mv != unsigned_type_node
6647 : 64337 : && type_mv != boolean_type_node)
6648 : 11964 : pedwarn_c90 (loc, OPT_Wpedantic,
6649 : : "type of bit-field %qs is a GCC extension", name);
6650 : :
6651 : 51791 : max_width = TYPE_PRECISION (*type);
6652 : :
6653 : 51791 : if (compare_tree_int (*width, max_width) > 0)
6654 : : {
6655 : 4 : error_at (loc, "width of %qs exceeds its type", name);
6656 : 4 : w = max_width;
6657 : 4 : *width = build_int_cst (integer_type_node, w);
6658 : : }
6659 : : else
6660 : 51787 : w = tree_to_uhwi (*width);
6661 : :
6662 : : /* Truncation of hardbool false and true representation values is always safe:
6663 : : either the values remain different, or we'll report a problem when creating
6664 : : the narrower type. */
6665 : 51791 : if (c_hardbool_type_attr (*type))
6666 : 51791 : return;
6667 : :
6668 : 51538 : if (TREE_CODE (*type) == ENUMERAL_TYPE)
6669 : : {
6670 : 4128 : struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
6671 : 4128 : if (!lt
6672 : 4120 : || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
6673 : 8244 : || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
6674 : 13 : warning_at (loc, 0, "%qs is narrower than values of its type", name);
6675 : : }
6676 : : }
6677 : :
6678 : :
6679 : :
6680 : : /* Print warning about variable length array if necessary. */
6681 : :
6682 : : static void
6683 : 22295 : warn_variable_length_array (tree name, tree size)
6684 : : {
6685 : 22295 : if (TREE_CONSTANT (size))
6686 : : {
6687 : 172 : if (name)
6688 : 160 : pedwarn_c90 (input_location, OPT_Wvla,
6689 : : "ISO C90 forbids array %qE whose size "
6690 : : "cannot be evaluated", name);
6691 : : else
6692 : 12 : pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
6693 : : "whose size cannot be evaluated");
6694 : : }
6695 : : else
6696 : : {
6697 : 22123 : if (name)
6698 : 13734 : pedwarn_c90 (input_location, OPT_Wvla,
6699 : : "ISO C90 forbids variable length array %qE", name);
6700 : : else
6701 : 8389 : pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
6702 : : "length array");
6703 : : }
6704 : 22295 : }
6705 : :
6706 : : /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6707 : : considering only those c_declspec_words found in LIST, which
6708 : : must be terminated by cdw_number_of_elements. */
6709 : :
6710 : : static location_t
6711 : 123 : smallest_type_quals_location (const location_t *locations,
6712 : : const c_declspec_word *list)
6713 : : {
6714 : 123 : location_t loc = UNKNOWN_LOCATION;
6715 : 738 : while (*list != cdw_number_of_elements)
6716 : : {
6717 : 615 : location_t newloc = locations[*list];
6718 : 615 : if (loc == UNKNOWN_LOCATION
6719 : 288 : || (newloc != UNKNOWN_LOCATION && newloc < loc))
6720 : 327 : loc = newloc;
6721 : 615 : list++;
6722 : : }
6723 : :
6724 : 123 : return loc;
6725 : : }
6726 : :
6727 : :
6728 : : /* We attach an artificial TYPE_DECL to pointed-to type
6729 : : and arrange for it to be included in a DECL_EXPR. This
6730 : : forces the sizes evaluation at a safe point and ensures it
6731 : : is not deferred until e.g. within a deeper conditional context.
6732 : :
6733 : : PARM contexts have no enclosing statement list that
6734 : : can hold the DECL_EXPR, so we need to use a BIND_EXPR
6735 : : instead, and add it to the list of expressions that
6736 : : need to be evaluated.
6737 : :
6738 : : TYPENAME contexts do have an enclosing statement list,
6739 : : but it would be incorrect to use it, as the size should
6740 : : only be evaluated if the containing expression is
6741 : : evaluated. We might also be in the middle of an
6742 : : expression with side effects on the pointed-to type size
6743 : : "arguments" prior to the pointer declaration point and
6744 : : the fake TYPE_DECL in the enclosing context would force
6745 : : the size evaluation prior to the side effects. We therefore
6746 : : use BIND_EXPRs in TYPENAME contexts too. */
6747 : : static void
6748 : 6892 : add_decl_expr (location_t loc, tree type, tree *expr, bool set_name_p)
6749 : : {
6750 : 6892 : tree bind = NULL_TREE;
6751 : 6892 : if (expr)
6752 : : {
6753 : 1463 : bind = build3 (BIND_EXPR, void_type_node, NULL_TREE, NULL_TREE,
6754 : : NULL_TREE);
6755 : 1463 : TREE_SIDE_EFFECTS (bind) = 1;
6756 : 1463 : BIND_EXPR_BODY (bind) = push_stmt_list ();
6757 : 1463 : push_scope ();
6758 : : }
6759 : :
6760 : 6892 : tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6761 : 6892 : pushdecl (decl);
6762 : 6892 : DECL_ARTIFICIAL (decl) = 1;
6763 : 6892 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6764 : 6892 : if (set_name_p)
6765 : 6245 : TYPE_NAME (type) = decl;
6766 : :
6767 : 6892 : if (bind)
6768 : : {
6769 : 1463 : pop_scope ();
6770 : 1463 : BIND_EXPR_BODY (bind) = pop_stmt_list (BIND_EXPR_BODY (bind));
6771 : 1463 : if (*expr)
6772 : 1403 : *expr = build2 (COMPOUND_EXPR, void_type_node, *expr, bind);
6773 : : else
6774 : 60 : *expr = bind;
6775 : : }
6776 : 6892 : }
6777 : :
6778 : : /* Given declspecs and a declarator,
6779 : : determine the name and type of the object declared
6780 : : and construct a ..._DECL node for it.
6781 : : (In one case we can return a ..._TYPE node instead.
6782 : : For invalid input we sometimes return NULL_TREE.)
6783 : :
6784 : : DECLSPECS is a c_declspecs structure for the declaration specifiers.
6785 : :
6786 : : DECL_CONTEXT says which syntactic context this declaration is in:
6787 : : NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6788 : : FUNCDEF for a function definition. Like NORMAL but a few different
6789 : : error messages in each case. Return value may be zero meaning
6790 : : this definition is too screwy to try to parse.
6791 : : PARM for a parameter declaration (either within a function prototype
6792 : : or before a function body). Make a PARM_DECL, or return void_type_node.
6793 : : TYPENAME if for a typename (in a cast or sizeof).
6794 : : Don't make a DECL node; just return the ..._TYPE node.
6795 : : FIELD for a struct or union field; make a FIELD_DECL.
6796 : : INITIALIZED is true if the decl has an initializer.
6797 : : WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6798 : : representing the width of the bit-field.
6799 : : DECL_ATTRS points to the list of attributes that should be added to this
6800 : : decl. Any nested attributes that belong on the decl itself will be
6801 : : added to this list.
6802 : : If EXPR is not NULL, any expressions that need to be evaluated as
6803 : : part of evaluating variably modified types will be stored in *EXPR.
6804 : : If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6805 : : set to indicate whether operands in *EXPR can be used in constant
6806 : : expressions.
6807 : : DEPRECATED_STATE is a deprecated_states value indicating whether
6808 : : deprecation/unavailability warnings should be suppressed.
6809 : :
6810 : : In the TYPENAME case, DECLARATOR is really an absolute declarator.
6811 : : It may also be so in the PARM case, for a prototype where the
6812 : : argument type is specified but not the name.
6813 : :
6814 : : This function is where the complicated C meanings of `static'
6815 : : and `extern' are interpreted. */
6816 : :
6817 : : static tree
6818 : 304812778 : grokdeclarator (const struct c_declarator *declarator,
6819 : : struct c_declspecs *declspecs,
6820 : : enum decl_context decl_context, bool initialized, tree *width,
6821 : : tree *decl_attrs, tree *expr, bool *expr_const_operands,
6822 : : enum deprecated_states deprecated_state)
6823 : : {
6824 : 304812778 : tree type = declspecs->type;
6825 : 304812778 : bool threadp = declspecs->thread_p;
6826 : 304812778 : bool constexprp = declspecs->constexpr_p;
6827 : 304812778 : enum c_storage_class storage_class = declspecs->storage_class;
6828 : 304812778 : int constp;
6829 : 304812778 : int restrictp;
6830 : 304812778 : int volatilep;
6831 : 304812778 : int atomicp;
6832 : 304812778 : int type_quals = TYPE_UNQUALIFIED;
6833 : 304812778 : tree name = NULL_TREE;
6834 : 304812778 : bool funcdef_flag = false;
6835 : 304812778 : bool funcdef_syntax = false;
6836 : 304812778 : bool size_varies = false;
6837 : 304812778 : tree decl_attr = declspecs->decl_attr;
6838 : 304812778 : int array_ptr_quals = TYPE_UNQUALIFIED;
6839 : 304812778 : tree array_ptr_attrs = NULL_TREE;
6840 : 304812778 : bool array_parm_static = false;
6841 : 304812778 : bool array_parm_vla_unspec_p = false;
6842 : 304812778 : tree returned_attrs = NULL_TREE;
6843 : 304812778 : tree decl_id_attrs = NULL_TREE;
6844 : 304812778 : bool bitfield = width != NULL;
6845 : 304812778 : tree element_type;
6846 : 304812778 : tree orig_qual_type = NULL;
6847 : 304812778 : size_t orig_qual_indirect = 0;
6848 : 304812778 : struct c_arg_info *arg_info = 0;
6849 : 304812778 : addr_space_t as1, as2, address_space;
6850 : 304812778 : location_t loc = UNKNOWN_LOCATION;
6851 : 304812778 : tree expr_dummy;
6852 : 304812778 : bool expr_const_operands_dummy;
6853 : 304812778 : enum c_declarator_kind first_non_attr_kind;
6854 : 304812778 : unsigned int alignas_align = 0;
6855 : :
6856 : 304812778 : if (type == NULL_TREE)
6857 : : {
6858 : : /* This can occur for auto on a parameter in C23 mode. Set a
6859 : : dummy type here so subsequent code can give diagnostics for
6860 : : this case. */
6861 : 2 : gcc_assert (declspecs->c23_auto_p);
6862 : 2 : gcc_assert (decl_context == PARM);
6863 : 2 : type = declspecs->type = integer_type_node;
6864 : : }
6865 : 304812778 : if (TREE_CODE (type) == ERROR_MARK)
6866 : 29 : return error_mark_node;
6867 : 304812749 : if (expr == NULL)
6868 : : {
6869 : 37694 : expr = &expr_dummy;
6870 : 37694 : expr_dummy = NULL_TREE;
6871 : : }
6872 : 304812749 : if (expr_const_operands == NULL)
6873 : 186597273 : expr_const_operands = &expr_const_operands_dummy;
6874 : :
6875 : 304812749 : if (declspecs->expr)
6876 : : {
6877 : 894 : if (*expr)
6878 : 7 : *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6879 : : declspecs->expr);
6880 : : else
6881 : 887 : *expr = declspecs->expr;
6882 : : }
6883 : 304812749 : *expr_const_operands = declspecs->expr_const_operands;
6884 : :
6885 : 304812749 : if (decl_context == FUNCDEF)
6886 : 35349723 : funcdef_flag = true, decl_context = NORMAL;
6887 : :
6888 : : /* Look inside a declarator for the name being declared
6889 : : and get it as an IDENTIFIER_NODE, for an error message. */
6890 : 304812749 : {
6891 : 304812749 : const struct c_declarator *decl = declarator;
6892 : :
6893 : 304812749 : first_non_attr_kind = cdk_attrs;
6894 : 372648876 : while (decl)
6895 : 372648876 : switch (decl->kind)
6896 : : {
6897 : 1125132 : case cdk_array:
6898 : 1125132 : loc = decl->id_loc;
6899 : : /* FALL THRU. */
6900 : :
6901 : 67829473 : case cdk_function:
6902 : 67829473 : case cdk_pointer:
6903 : 67829473 : funcdef_syntax = (decl->kind == cdk_function);
6904 : 67829473 : if (first_non_attr_kind == cdk_attrs)
6905 : 65757547 : first_non_attr_kind = decl->kind;
6906 : 67829473 : decl = decl->declarator;
6907 : 67829473 : break;
6908 : :
6909 : 6654 : case cdk_attrs:
6910 : 6654 : decl = decl->declarator;
6911 : 6654 : break;
6912 : :
6913 : 304812749 : case cdk_id:
6914 : 304812749 : loc = decl->id_loc;
6915 : 304812749 : if (decl->u.id.id)
6916 : : name = decl->u.id.id;
6917 : 304812749 : decl_id_attrs = decl->u.id.attrs;
6918 : 304812749 : if (first_non_attr_kind == cdk_attrs)
6919 : 239055202 : first_non_attr_kind = decl->kind;
6920 : : decl = 0;
6921 : : break;
6922 : :
6923 : 0 : default:
6924 : 0 : gcc_unreachable ();
6925 : : }
6926 : 304812749 : if (name == NULL_TREE)
6927 : : {
6928 : 122773195 : gcc_assert (decl_context == PARM
6929 : : || decl_context == TYPENAME
6930 : : || (decl_context == FIELD
6931 : : && declarator->kind == cdk_id));
6932 : 122773195 : gcc_assert (!initialized);
6933 : : }
6934 : : }
6935 : :
6936 : : /* An enum type specifier (": specifier-qualifier-list") may only be
6937 : : specified when the enum is being defined or in an empty
6938 : : declaration of the form "enum identifier enum-type-specifier;".
6939 : : Except for the case of an empty declaration that has additional
6940 : : declaration specifiers, all invalid contexts (declarations that
6941 : : aren't empty, type names, parameter declarations, member
6942 : : declarations) pass through grokdeclarator. */
6943 : 304812749 : if (declspecs->enum_type_specifier_ref_p)
6944 : 6 : error_at (loc, "%<enum%> underlying type may not be specified here");
6945 : :
6946 : : /* A function definition's declarator must have the form of
6947 : : a function declarator. */
6948 : :
6949 : 304812749 : if (funcdef_flag && !funcdef_syntax)
6950 : : return NULL_TREE;
6951 : :
6952 : : /* If this looks like a function definition, make it one,
6953 : : even if it occurs where parms are expected.
6954 : : Then store_parm_decls will reject it and not use it as a parm. */
6955 : 304812718 : if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
6956 : 22656 : decl_context = PARM;
6957 : :
6958 : 304812718 : if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
6959 : : {
6960 : 304812694 : if (declspecs->unavailable_p)
6961 : 28 : error_unavailable_use (declspecs->type, declspecs->decl_attr);
6962 : 304812666 : else if (declspecs->deprecated_p
6963 : 46 : && deprecated_state != DEPRECATED_SUPPRESS)
6964 : 38 : warn_deprecated_use (declspecs->type, declspecs->decl_attr);
6965 : : }
6966 : :
6967 : 304812718 : if ((decl_context == NORMAL || decl_context == FIELD)
6968 : 66353890 : && current_scope == file_scope
6969 : 363279775 : && c_type_variably_modified_p (type))
6970 : : {
6971 : 3 : if (name)
6972 : 3 : error_at (loc, "variably modified %qE at file scope", name);
6973 : : else
6974 : 0 : error_at (loc, "variably modified field at file scope");
6975 : 3 : type = integer_type_node;
6976 : : }
6977 : :
6978 : 304812718 : size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
6979 : :
6980 : : /* Diagnose defaulting to "int". */
6981 : :
6982 : 304812718 : if (declspecs->default_int_p)
6983 : : {
6984 : : /* Issue a warning if this is an ISO C 99 program or if
6985 : : -Wreturn-type and this is a function, or if -Wimplicit;
6986 : : prefer the former warning since it is more explicit. */
6987 : 9763 : if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
6988 : 1228 : && funcdef_flag)
6989 : 702 : warn_about_return_type = 1;
6990 : : else
6991 : : {
6992 : 9061 : if (name)
6993 : 9051 : permerror_opt (loc, OPT_Wimplicit_int,
6994 : : "type defaults to %<int%> in declaration "
6995 : : "of %qE", name);
6996 : : else
6997 : 10 : permerror_opt (loc, OPT_Wimplicit_int,
6998 : : "type defaults to %<int%> in type name");
6999 : : }
7000 : : }
7001 : :
7002 : : /* Adjust the type if a bit-field is being declared,
7003 : : -funsigned-bitfields applied and the type is not explicitly
7004 : : "signed". */
7005 : 304812718 : if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
7006 : 40 : && TREE_CODE (type) == INTEGER_TYPE)
7007 : 38 : type = c_common_unsigned_type (type);
7008 : :
7009 : : /* Figure out the type qualifiers for the declaration. There are
7010 : : two ways a declaration can become qualified. One is something
7011 : : like `const int i' where the `const' is explicit. Another is
7012 : : something like `typedef const int CI; CI i' where the type of the
7013 : : declaration contains the `const'. A third possibility is that
7014 : : there is a type qualifier on the element type of a typedefed
7015 : : array type, in which case we should extract that qualifier so
7016 : : that c_apply_type_quals_to_decl receives the full list of
7017 : : qualifiers to work with (C90 is not entirely clear about whether
7018 : : duplicate qualifiers should be diagnosed in this case, but it
7019 : : seems most appropriate to do so). */
7020 : 304812718 : element_type = strip_array_types (type);
7021 : 304812718 : constp = declspecs->const_p + TYPE_READONLY (element_type);
7022 : 304812718 : restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
7023 : 304812718 : volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
7024 : 304812718 : atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
7025 : 304812718 : as1 = declspecs->address_space;
7026 : 304812718 : as2 = TYPE_ADDR_SPACE (element_type);
7027 : 304812718 : address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
7028 : :
7029 : 304812718 : if (constp > 1)
7030 : 25 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
7031 : 304812718 : if (restrictp > 1)
7032 : 6 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
7033 : 304812718 : if (volatilep > 1)
7034 : 15 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
7035 : 304812718 : if (atomicp > 1)
7036 : 9 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
7037 : :
7038 : 304812718 : if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
7039 : 0 : error_at (loc, "conflicting named address spaces (%s vs %s)",
7040 : : c_addr_space_name (as1), c_addr_space_name (as2));
7041 : :
7042 : 304812718 : if ((TREE_CODE (type) == ARRAY_TYPE
7043 : 304639511 : || first_non_attr_kind == cdk_array)
7044 : 305870786 : && TYPE_QUALS (element_type))
7045 : : {
7046 : 59 : orig_qual_type = type;
7047 : 59 : type = c_build_qualified_type (type, TYPE_UNQUALIFIED);
7048 : : }
7049 : 304812718 : type_quals = ((constp ? TYPE_QUAL_CONST : 0)
7050 : 304812718 : | (restrictp ? TYPE_QUAL_RESTRICT : 0)
7051 : 304812718 : | (volatilep ? TYPE_QUAL_VOLATILE : 0)
7052 : 304812718 : | (atomicp ? TYPE_QUAL_ATOMIC : 0)
7053 : 304812718 : | ENCODE_QUAL_ADDR_SPACE (address_space));
7054 : 304812718 : if (type_quals != TYPE_QUALS (element_type))
7055 : 12524434 : orig_qual_type = NULL_TREE;
7056 : :
7057 : : /* Applying the _Atomic qualifier to an array type (through the use
7058 : : of typedefs or typeof) must be detected here. If the qualifier
7059 : : is introduced later, any appearance of applying it to an array is
7060 : : actually applying it to an element of that array. */
7061 : 304812718 : if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
7062 : 6 : error_at (loc, "%<_Atomic%>-qualified array type");
7063 : :
7064 : : /* Warn about storage classes that are invalid for certain
7065 : : kinds of declarations (parameters, typenames, etc.). */
7066 : :
7067 : 304812718 : if (funcdef_flag
7068 : 35349692 : && (threadp
7069 : : || constexprp
7070 : 35349690 : || storage_class == csc_auto
7071 : 35349690 : || storage_class == csc_register
7072 : 35349645 : || storage_class == csc_typedef))
7073 : : {
7074 : 47 : if (storage_class == csc_auto)
7075 : 42 : pedwarn (loc,
7076 : 81 : (current_scope == file_scope) ? 0 : OPT_Wpedantic,
7077 : : "function definition declared %<auto%>");
7078 : 50 : if (storage_class == csc_register)
7079 : 3 : error_at (loc, "function definition declared %<register%>");
7080 : 50 : if (storage_class == csc_typedef)
7081 : 3 : error_at (loc, "function definition declared %<typedef%>");
7082 : 50 : if (threadp)
7083 : 2 : error_at (loc, "function definition declared %qs",
7084 : 2 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7085 : 50 : threadp = false;
7086 : : /* The parser ensures a constexpr function definition never
7087 : : reaches here. */
7088 : 50 : gcc_assert (!constexprp);
7089 : 50 : if (storage_class == csc_auto
7090 : 50 : || storage_class == csc_register
7091 : : || storage_class == csc_typedef)
7092 : 55 : storage_class = csc_none;
7093 : : }
7094 : 304812668 : else if (decl_context != NORMAL && (storage_class != csc_none
7095 : 242617112 : || threadp
7096 : 242616529 : || constexprp
7097 : 242616527 : || declspecs->c23_auto_p))
7098 : : {
7099 : 587 : if (decl_context == PARM
7100 : 587 : && storage_class == csc_register
7101 : 568 : && !constexprp
7102 : 566 : && !declspecs->c23_auto_p)
7103 : : ;
7104 : : else
7105 : : {
7106 : 21 : switch (decl_context)
7107 : : {
7108 : 0 : case FIELD:
7109 : 0 : if (name)
7110 : 0 : error_at (loc, "storage class specified for structure "
7111 : : "field %qE", name);
7112 : : else
7113 : 0 : error_at (loc, "storage class specified for structure field");
7114 : : break;
7115 : 21 : case PARM:
7116 : 21 : if (name)
7117 : 7 : error_at (loc, "storage class specified for parameter %qE",
7118 : : name);
7119 : : else
7120 : 14 : error_at (loc, "storage class specified for unnamed parameter");
7121 : : break;
7122 : 0 : default:
7123 : 0 : error_at (loc, "storage class specified for typename");
7124 : 0 : break;
7125 : : }
7126 : 304812718 : storage_class = csc_none;
7127 : 304812718 : threadp = false;
7128 : 304812718 : constexprp = false;
7129 : : }
7130 : : }
7131 : 304812081 : else if (storage_class == csc_extern
7132 : 304812081 : && initialized
7133 : 34516189 : && !funcdef_flag)
7134 : : {
7135 : : /* 'extern' with initialization is invalid if not at file scope. */
7136 : 28 : if (current_scope == file_scope)
7137 : : {
7138 : : /* It is fine to have 'extern const' when compiling at C
7139 : : and C++ intersection. */
7140 : 19 : if (!(warn_cxx_compat && constp))
7141 : 18 : warning_at (loc, 0, "%qE initialized and declared %<extern%>",
7142 : : name);
7143 : : }
7144 : : else
7145 : 9 : error_at (loc, "%qE has both %<extern%> and initializer", name);
7146 : : }
7147 : 304812053 : else if (current_scope == file_scope)
7148 : : {
7149 : 59339698 : if (storage_class == csc_auto)
7150 : 7 : error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
7151 : : name);
7152 : 59339698 : if (pedantic && storage_class == csc_register)
7153 : 4 : pedwarn (input_location, OPT_Wpedantic,
7154 : : "file-scope declaration of %qE specifies %<register%>", name);
7155 : : }
7156 : : else
7157 : : {
7158 : 245472355 : if (storage_class == csc_extern && funcdef_flag)
7159 : 3 : error_at (loc, "nested function %qE declared %<extern%>", name);
7160 : 245472352 : else if (threadp && storage_class == csc_none)
7161 : : {
7162 : 12 : error_at (loc, "function-scope %qE implicitly auto and declared "
7163 : : "%qs", name,
7164 : 6 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7165 : 6 : threadp = false;
7166 : : }
7167 : : }
7168 : :
7169 : : /* Now figure out the structure of the declarator proper.
7170 : : Descend through it, creating more complex types, until we reach
7171 : : the declared identifier (or NULL_TREE, in an absolute declarator).
7172 : : At each stage we maintain an unqualified version of the type
7173 : : together with any qualifiers that should be applied to it with
7174 : : c_build_qualified_type; this way, array types including
7175 : : multidimensional array types are first built up in unqualified
7176 : : form and then the qualified form is created with
7177 : : TYPE_MAIN_VARIANT pointing to the unqualified form. */
7178 : :
7179 : 372648844 : while (declarator && declarator->kind != cdk_id)
7180 : : {
7181 : 67836126 : if (type == error_mark_node)
7182 : : {
7183 : 38 : declarator = declarator->declarator;
7184 : 38 : continue;
7185 : : }
7186 : :
7187 : : /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
7188 : : a cdk_pointer (for *...),
7189 : : a cdk_function (for ...(...)),
7190 : : a cdk_attrs (for nested attributes),
7191 : : or a cdk_id (for the name being declared
7192 : : or the place in an absolute declarator
7193 : : where the name was omitted).
7194 : : For the last case, we have just exited the loop.
7195 : :
7196 : : At this point, TYPE is the type of elements of an array,
7197 : : or for a function to return, or for a pointer to point to.
7198 : : After this sequence of ifs, TYPE is the type of the
7199 : : array or function or pointer, and DECLARATOR has had its
7200 : : outermost layer removed. */
7201 : :
7202 : 67836088 : if (array_ptr_quals != TYPE_UNQUALIFIED
7203 : 67836088 : || array_ptr_attrs != NULL_TREE
7204 : 67836088 : || array_parm_static)
7205 : : {
7206 : : /* Only the innermost declarator (making a parameter be of
7207 : : array type which is converted to pointer type)
7208 : : may have static or type qualifiers. */
7209 : 1 : error_at (loc, "static or type qualifiers in non-parameter array declarator");
7210 : 1 : array_ptr_quals = TYPE_UNQUALIFIED;
7211 : 1 : array_ptr_attrs = NULL_TREE;
7212 : 1 : array_parm_static = false;
7213 : : }
7214 : :
7215 : 67836088 : switch (declarator->kind)
7216 : : {
7217 : 6654 : case cdk_attrs:
7218 : 6654 : {
7219 : : /* A declarator with embedded attributes. */
7220 : 6654 : tree attrs = declarator->u.attrs;
7221 : 6654 : const struct c_declarator *inner_decl;
7222 : 6654 : int attr_flags = 0;
7223 : 6654 : declarator = declarator->declarator;
7224 : : /* Standard attribute syntax precisely defines what entity
7225 : : an attribute in each position appertains to, so only
7226 : : apply laxity about positioning to GNU attribute syntax.
7227 : : Standard attributes applied to a function or array
7228 : : declarator apply exactly to that type; standard
7229 : : attributes applied to the identifier apply to the
7230 : : declaration rather than to the type, and are specified
7231 : : using a cdk_id declarator rather than using
7232 : : cdk_attrs. */
7233 : 6654 : inner_decl = declarator;
7234 : 6654 : while (inner_decl->kind == cdk_attrs)
7235 : 0 : inner_decl = inner_decl->declarator;
7236 : 6654 : if (!cxx11_attribute_p (attrs))
7237 : : {
7238 : 6559 : if (inner_decl->kind == cdk_id)
7239 : : attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
7240 : : else if (inner_decl->kind == cdk_function)
7241 : : attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
7242 : : else if (inner_decl->kind == cdk_array)
7243 : 6654 : attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
7244 : : }
7245 : 6654 : attrs = c_warn_type_attributes (type, attrs);
7246 : 6654 : returned_attrs = decl_attributes (&type,
7247 : : chainon (returned_attrs, attrs),
7248 : : attr_flags);
7249 : 6654 : break;
7250 : : }
7251 : 1125122 : case cdk_array:
7252 : 1125122 : {
7253 : 1125122 : tree itype = NULL_TREE;
7254 : 1125122 : tree size = declarator->u.array.dimen;
7255 : : /* The index is a signed object `sizetype' bits wide. */
7256 : 1125122 : tree index_type = c_common_signed_type (sizetype);
7257 : :
7258 : 1125122 : array_ptr_quals = declarator->u.array.quals;
7259 : 1125122 : array_ptr_attrs = declarator->u.array.attrs;
7260 : 1125122 : array_parm_static = declarator->u.array.static_p;
7261 : 1125122 : array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
7262 : :
7263 : 1125122 : declarator = declarator->declarator;
7264 : :
7265 : : /* Check for some types that there cannot be arrays of. */
7266 : :
7267 : 1125122 : if (VOID_TYPE_P (type))
7268 : : {
7269 : 11 : if (name)
7270 : 9 : error_at (loc, "declaration of %qE as array of voids", name);
7271 : : else
7272 : 2 : error_at (loc, "declaration of type name as array of voids");
7273 : 11 : type = error_mark_node;
7274 : : }
7275 : :
7276 : 1125122 : if (TREE_CODE (type) == FUNCTION_TYPE)
7277 : : {
7278 : 3 : if (name)
7279 : 2 : error_at (loc, "declaration of %qE as array of functions",
7280 : : name);
7281 : : else
7282 : 1 : error_at (loc, "declaration of type name as array of "
7283 : : "functions");
7284 : 3 : type = error_mark_node;
7285 : : }
7286 : :
7287 : 21844 : if (pedantic && !in_system_header_at (input_location)
7288 : 1139797 : && flexible_array_type_p (type))
7289 : 20 : pedwarn (loc, OPT_Wpedantic,
7290 : : "invalid use of structure with flexible array member");
7291 : :
7292 : 1125122 : if (size == error_mark_node)
7293 : 119 : type = error_mark_node;
7294 : :
7295 : 1125122 : if (type == error_mark_node)
7296 : 133 : continue;
7297 : :
7298 : 1124989 : if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
7299 : : {
7300 : 0 : type = error_mark_node;
7301 : 0 : continue;
7302 : : }
7303 : :
7304 : : /* If size was specified, set ITYPE to a range-type for
7305 : : that size. Otherwise, ITYPE remains null. finish_decl
7306 : : may figure it out from an initial value. */
7307 : :
7308 : 1124989 : if (size)
7309 : : {
7310 : 956790 : bool size_maybe_const = true;
7311 : 956790 : bool size_int_const = (TREE_CODE (size) == INTEGER_CST
7312 : 956790 : && !TREE_OVERFLOW (size));
7313 : 956790 : bool this_size_varies = false;
7314 : :
7315 : : /* Strip NON_LVALUE_EXPRs since we aren't using as an
7316 : : lvalue. */
7317 : 956799 : STRIP_TYPE_NOPS (size);
7318 : :
7319 : 956790 : if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
7320 : : {
7321 : 16 : if (name)
7322 : 14 : error_at (loc, "size of array %qE has non-integer type",
7323 : : name);
7324 : : else
7325 : 2 : error_at (loc,
7326 : : "size of unnamed array has non-integer type");
7327 : 16 : size = integer_one_node;
7328 : 16 : size_int_const = true;
7329 : : }
7330 : : /* This can happen with enum forward declaration. */
7331 : 956774 : else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
7332 : : {
7333 : 0 : if (name)
7334 : 0 : error_at (loc, "size of array %qE has incomplete type",
7335 : : name);
7336 : : else
7337 : 0 : error_at (loc, "size of unnamed array has incomplete "
7338 : : "type");
7339 : 0 : size = integer_one_node;
7340 : 0 : size_int_const = true;
7341 : : }
7342 : :
7343 : 956790 : size = c_fully_fold (size, false, &size_maybe_const);
7344 : :
7345 : 956790 : if (pedantic && size_maybe_const && integer_zerop (size))
7346 : : {
7347 : 3 : if (name)
7348 : 3 : pedwarn (loc, OPT_Wpedantic,
7349 : : "ISO C forbids zero-size array %qE", name);
7350 : : else
7351 : 0 : pedwarn (loc, OPT_Wpedantic,
7352 : : "ISO C forbids zero-size array");
7353 : : }
7354 : :
7355 : 956790 : if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
7356 : : {
7357 : 934620 : constant_expression_warning (size);
7358 : 934620 : if (tree_int_cst_sgn (size) < 0)
7359 : : {
7360 : 693 : if (name)
7361 : 690 : error_at (loc, "size of array %qE is negative", name);
7362 : : else
7363 : 3 : error_at (loc, "size of unnamed array is negative");
7364 : 693 : size = integer_one_node;
7365 : 693 : size_int_const = true;
7366 : : }
7367 : : /* Handle a size folded to an integer constant but
7368 : : not an integer constant expression. */
7369 : 934620 : if (!size_int_const)
7370 : : {
7371 : : /* If this is a file scope declaration of an
7372 : : ordinary identifier, this is invalid code;
7373 : : diagnosing it here and not subsequently
7374 : : treating the type as variable-length avoids
7375 : : more confusing diagnostics later. */
7376 : 146 : if ((decl_context == NORMAL || decl_context == FIELD)
7377 : 134 : && current_scope == file_scope)
7378 : 14 : pedwarn (input_location, 0,
7379 : : "variably modified %qE at file scope",
7380 : : name);
7381 : : else
7382 : : this_size_varies = size_varies = true;
7383 : 146 : warn_variable_length_array (name, size);
7384 : : }
7385 : : }
7386 : 22170 : else if ((decl_context == NORMAL || decl_context == FIELD)
7387 : 13169 : && current_scope == file_scope)
7388 : : {
7389 : 21 : error_at (loc, "variably modified %qE at file scope", name);
7390 : 21 : size = integer_one_node;
7391 : : }
7392 : : else
7393 : : {
7394 : : /* Make sure the array size remains visibly
7395 : : nonconstant even if it is (eg) a const variable
7396 : : with known value. */
7397 : 22149 : this_size_varies = size_varies = true;
7398 : 22149 : warn_variable_length_array (name, size);
7399 : 22149 : if (sanitize_flags_p (SANITIZE_VLA)
7400 : 178 : && current_function_decl != NULL_TREE
7401 : 22309 : && decl_context == NORMAL)
7402 : : {
7403 : : /* Evaluate the array size only once. */
7404 : 152 : size = save_expr (size);
7405 : 152 : size = c_fully_fold (size, false, NULL);
7406 : 152 : size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
7407 : : ubsan_instrument_vla (loc, size),
7408 : : size);
7409 : : }
7410 : : }
7411 : :
7412 : 956790 : if (integer_zerop (size) && !this_size_varies)
7413 : : {
7414 : : /* A zero-length array cannot be represented with
7415 : : an unsigned index type, which is what we'll
7416 : : get with build_index_type. Create an
7417 : : open-ended range instead. */
7418 : 2595 : itype = build_range_type (sizetype, size, NULL_TREE);
7419 : : }
7420 : : else
7421 : : {
7422 : : /* Arrange for the SAVE_EXPR on the inside of the
7423 : : MINUS_EXPR, which allows the -1 to get folded
7424 : : with the +1 that happens when building TYPE_SIZE. */
7425 : 954195 : if (size_varies)
7426 : 22649 : size = save_expr (size);
7427 : 954195 : if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
7428 : 132 : size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7429 : : integer_zero_node, size);
7430 : :
7431 : : /* Compute the maximum valid index, that is, size
7432 : : - 1. Do the calculation in index_type, so that
7433 : : if it is a variable the computations will be
7434 : : done in the proper mode. */
7435 : 954195 : itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
7436 : : convert (index_type, size),
7437 : : convert (index_type,
7438 : : size_one_node));
7439 : :
7440 : : /* The above overflows when size does not fit
7441 : : in index_type.
7442 : : ??? While a size of INT_MAX+1 technically shouldn't
7443 : : cause an overflow (because we subtract 1), handling
7444 : : this case seems like an unnecessary complication. */
7445 : 954195 : if (TREE_CODE (size) == INTEGER_CST
7446 : 931914 : && !int_fits_type_p (size, index_type))
7447 : : {
7448 : 6 : if (name)
7449 : 5 : error_at (loc, "size of array %qE is too large",
7450 : : name);
7451 : : else
7452 : 1 : error_at (loc, "size of unnamed array is too large");
7453 : 6 : type = error_mark_node;
7454 : 6 : continue;
7455 : : }
7456 : :
7457 : 954189 : itype = build_index_type (itype);
7458 : : }
7459 : 956784 : if (this_size_varies)
7460 : : {
7461 : 22281 : if (TREE_SIDE_EFFECTS (size))
7462 : : {
7463 : 22003 : if (*expr)
7464 : 8173 : *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7465 : : *expr, size);
7466 : : else
7467 : 13830 : *expr = size;
7468 : : }
7469 : 22281 : *expr_const_operands &= size_maybe_const;
7470 : : }
7471 : : }
7472 : 168199 : else if (decl_context == FIELD)
7473 : : {
7474 : 84579 : bool flexible_array_member = false;
7475 : 84579 : if (array_parm_vla_unspec_p)
7476 : : /* Field names can in fact have function prototype
7477 : : scope so [*] is disallowed here through making
7478 : : the field variably modified, not through being
7479 : : something other than a declaration with function
7480 : : prototype scope. */
7481 : : size_varies = true;
7482 : : else
7483 : : {
7484 : : const struct c_declarator *t = declarator;
7485 : 84577 : while (t->kind == cdk_attrs)
7486 : 0 : t = t->declarator;
7487 : 84577 : flexible_array_member = (t->kind == cdk_id);
7488 : : }
7489 : 84577 : if (flexible_array_member
7490 : 84577 : && !in_system_header_at (input_location))
7491 : 83546 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7492 : : "support flexible array members");
7493 : :
7494 : : /* ISO C99 Flexible array members are effectively
7495 : : identical to GCC's zero-length array extension. */
7496 : 84579 : if (flexible_array_member)
7497 : 84557 : itype = build_index_type (NULL_TREE);
7498 : : }
7499 : :
7500 : 1124983 : if (array_parm_vla_unspec_p)
7501 : : {
7502 : : /* C99 6.7.5.2p4 */
7503 : 142 : if (decl_context == TYPENAME)
7504 : 5 : warning (0, "%<[*]%> not in a declaration");
7505 : : size_varies = true;
7506 : : }
7507 : :
7508 : : /* Complain about arrays of incomplete types. */
7509 : 1124983 : if (!COMPLETE_TYPE_P (type))
7510 : : {
7511 : 57 : auto_diagnostic_group d;
7512 : 57 : error_at (loc, "array type has incomplete element type %qT",
7513 : : type);
7514 : : /* See if we can be more helpful. */
7515 : 57 : if (TREE_CODE (type) == ARRAY_TYPE)
7516 : : {
7517 : 29 : if (name)
7518 : 24 : inform (loc, "declaration of %qE as multidimensional "
7519 : : "array must have bounds for all dimensions "
7520 : : "except the first", name);
7521 : : else
7522 : 5 : inform (loc, "declaration of multidimensional array "
7523 : : "must have bounds for all dimensions except "
7524 : : "the first");
7525 : : }
7526 : 57 : type = error_mark_node;
7527 : 57 : }
7528 : : else
7529 : : /* When itype is NULL, a shared incomplete array type is
7530 : : returned for all array of a given type. Elsewhere we
7531 : : make sure we don't complete that type before copying
7532 : : it, but here we want to make sure we don't ever
7533 : : modify the shared type, so we gcc_assert (itype)
7534 : : below. */
7535 : : {
7536 : 1124926 : addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
7537 : 1124926 : if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
7538 : 0 : type = c_build_qualified_type (type,
7539 : : ENCODE_QUAL_ADDR_SPACE (as));
7540 : 1124926 : if (array_parm_vla_unspec_p)
7541 : 142 : type = c_build_array_type_unspecified (type);
7542 : : else
7543 : 1124784 : type = c_build_array_type (type, itype);
7544 : : }
7545 : :
7546 : 1124983 : if (type != error_mark_node)
7547 : : {
7548 : : /* The GCC extension for zero-length arrays differs from
7549 : : ISO flexible array members in that sizeof yields
7550 : : zero. */
7551 : 1124926 : if (size && integer_zerop (size))
7552 : : {
7553 : 2588 : gcc_assert (itype);
7554 : 2588 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7555 : 2588 : TYPE_SIZE (type) = bitsize_zero_node;
7556 : 2588 : TYPE_SIZE_UNIT (type) = size_zero_node;
7557 : 2588 : SET_TYPE_STRUCTURAL_EQUALITY (type);
7558 : : }
7559 : :
7560 : 1124926 : if (!valid_array_size_p (loc, type, name))
7561 : 33 : type = error_mark_node;
7562 : : }
7563 : :
7564 : 1124983 : if (decl_context != PARM
7565 : 812448 : && (array_ptr_quals != TYPE_UNQUALIFIED
7566 : 812448 : || array_ptr_attrs != NULL_TREE
7567 : 812447 : || array_parm_static))
7568 : : {
7569 : 2 : error_at (loc, "static or type qualifiers in non-parameter "
7570 : : "array declarator");
7571 : 2 : array_ptr_quals = TYPE_UNQUALIFIED;
7572 : 2 : array_ptr_attrs = NULL_TREE;
7573 : 2 : array_parm_static = false;
7574 : : }
7575 : 1124983 : orig_qual_indirect++;
7576 : 1124983 : break;
7577 : : }
7578 : 48828622 : case cdk_function:
7579 : 48828622 : {
7580 : : /* Say it's a definition only for the declarator closest
7581 : : to the identifier, apart possibly from some
7582 : : attributes. */
7583 : 48828622 : bool really_funcdef = false;
7584 : 48828622 : tree arg_types;
7585 : 48828622 : orig_qual_type = NULL_TREE;
7586 : 48828622 : if (funcdef_flag)
7587 : : {
7588 : 35349714 : const struct c_declarator *t = declarator->declarator;
7589 : 35349732 : while (t->kind == cdk_attrs)
7590 : 18 : t = t->declarator;
7591 : 35349714 : really_funcdef = (t->kind == cdk_id);
7592 : : }
7593 : :
7594 : : /* Declaring a function type. Make sure we have a valid
7595 : : type for the function to return. */
7596 : 48828622 : if (type == error_mark_node)
7597 : 0 : continue;
7598 : :
7599 : 48828622 : size_varies = false;
7600 : :
7601 : : /* Warn about some types functions can't return. */
7602 : 48828622 : if (TREE_CODE (type) == FUNCTION_TYPE)
7603 : : {
7604 : 0 : if (name)
7605 : 0 : error_at (loc, "%qE declared as function returning a "
7606 : : "function", name);
7607 : : else
7608 : 0 : error_at (loc, "type name declared as function "
7609 : : "returning a function");
7610 : 0 : type = integer_type_node;
7611 : : }
7612 : 48828622 : if (TREE_CODE (type) == ARRAY_TYPE)
7613 : : {
7614 : 0 : if (name)
7615 : 0 : error_at (loc, "%qE declared as function returning an array",
7616 : : name);
7617 : : else
7618 : 0 : error_at (loc, "type name declared as function returning "
7619 : : "an array");
7620 : 0 : type = integer_type_node;
7621 : : }
7622 : :
7623 : : /* Construct the function type and go to the next
7624 : : inner layer of declarator. */
7625 : 48828622 : arg_info = declarator->u.arg_info;
7626 : 48828622 : arg_types = grokparms (arg_info, really_funcdef);
7627 : :
7628 : : /* Type qualifiers before the return type of the function
7629 : : qualify the return type, not the function type. */
7630 : 48828622 : if (type_quals)
7631 : : {
7632 : 123 : const enum c_declspec_word ignored_quals_list[] =
7633 : : {
7634 : : cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
7635 : : cdw_atomic, cdw_number_of_elements
7636 : : };
7637 : 123 : location_t specs_loc
7638 : 123 : = smallest_type_quals_location (declspecs->locations,
7639 : : ignored_quals_list);
7640 : 123 : if (specs_loc == UNKNOWN_LOCATION)
7641 : 27 : specs_loc = declspecs->locations[cdw_typedef];
7642 : 27 : if (specs_loc == UNKNOWN_LOCATION)
7643 : 15 : specs_loc = loc;
7644 : :
7645 : : /* Type qualifiers on a function return type are
7646 : : normally permitted by the standard but have no
7647 : : effect, so give a warning at -Wreturn-type.
7648 : : Qualifiers on a void return type are banned on
7649 : : function definitions in ISO C; GCC used to used
7650 : : them for noreturn functions. The resolution of C11
7651 : : DR#423 means qualifiers (other than _Atomic) are
7652 : : actually removed from the return type when
7653 : : determining the function type. For C23, _Atomic is
7654 : : removed as well. */
7655 : 123 : int quals_used = type_quals;
7656 : 123 : if (flag_isoc23)
7657 : : quals_used = 0;
7658 : 65 : else if (flag_isoc11)
7659 : 31 : quals_used &= TYPE_QUAL_ATOMIC;
7660 : 65 : if (quals_used && VOID_TYPE_P (type) && really_funcdef)
7661 : 5 : pedwarn (specs_loc, 0,
7662 : : "function definition has qualified void "
7663 : : "return type");
7664 : : else
7665 : 118 : warning_at (specs_loc, OPT_Wignored_qualifiers,
7666 : : "type qualifiers ignored on function "
7667 : : "return type");
7668 : :
7669 : : /* Ensure an error for restrict on invalid types; the
7670 : : DR#423 resolution is not entirely clear about
7671 : : this. */
7672 : 123 : if (flag_isoc11
7673 : 89 : && (type_quals & TYPE_QUAL_RESTRICT)
7674 : 129 : && (!POINTER_TYPE_P (type)
7675 : 4 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
7676 : 4 : error_at (loc, "invalid use of %<restrict%>");
7677 : 123 : type = c_build_qualified_type (type, quals_used);
7678 : : }
7679 : 48828622 : type_quals = TYPE_UNQUALIFIED;
7680 : :
7681 : 97657244 : type = c_build_function_type (type, arg_types,
7682 : 48828622 : arg_info->no_named_args_stdarg_p);
7683 : 48828622 : declarator = declarator->declarator;
7684 : :
7685 : : /* Set the TYPE_CONTEXTs for each tagged type which is local to
7686 : : the formal parameter list of this FUNCTION_TYPE to point to
7687 : : the FUNCTION_TYPE node itself. */
7688 : 48828622 : {
7689 : 48828622 : c_arg_tag *tag;
7690 : 48828622 : unsigned ix;
7691 : :
7692 : 421477732 : FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7693 : 134 : TYPE_CONTEXT (tag->type) = type;
7694 : : }
7695 : : break;
7696 : : }
7697 : 17875690 : case cdk_pointer:
7698 : 17875690 : {
7699 : : /* Merge any constancy or volatility into the target type
7700 : : for the pointer. */
7701 : 17875690 : if ((type_quals & TYPE_QUAL_ATOMIC)
7702 : 1749 : && TREE_CODE (type) == FUNCTION_TYPE)
7703 : : {
7704 : 2 : error_at (loc,
7705 : : "%<_Atomic%>-qualified function type");
7706 : 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
7707 : : }
7708 : 17875688 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7709 : 1701 : && type_quals)
7710 : 0 : pedwarn (loc, OPT_Wpedantic,
7711 : : "ISO C forbids qualified function types");
7712 : 17873989 : if (type_quals)
7713 : 5882965 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7714 : : orig_qual_indirect);
7715 : 17875690 : orig_qual_type = NULL_TREE;
7716 : 17875690 : size_varies = false;
7717 : :
7718 : : /* When the pointed-to type involves components of variable size,
7719 : : care must be taken to ensure that the size evaluation code is
7720 : : emitted early enough to dominate all the possible later uses
7721 : : and late enough for the variables on which it depends to have
7722 : : been assigned.
7723 : :
7724 : : This is expected to happen automatically when the pointed-to
7725 : : type has a name/declaration of it's own, but special attention
7726 : : is required if the type is anonymous. */
7727 : 17875690 : if (!TYPE_NAME (type) && c_type_variably_modified_p (type))
7728 : : {
7729 : 5872 : bool bind_p = decl_context == TYPENAME
7730 : : || decl_context == FIELD
7731 : 5872 : || decl_context == PARM;
7732 : 11301 : add_decl_expr (loc, type, bind_p ? expr : NULL, true);
7733 : : }
7734 : :
7735 : 17875690 : type = c_build_pointer_type (type);
7736 : :
7737 : : /* Process type qualifiers (such as const or volatile)
7738 : : that were given inside the `*'. */
7739 : 17875690 : type_quals = declarator->u.pointer_quals;
7740 : :
7741 : 17875690 : declarator = declarator->declarator;
7742 : 17875690 : break;
7743 : : }
7744 : 0 : default:
7745 : 0 : gcc_unreachable ();
7746 : : }
7747 : : }
7748 : 304812718 : *decl_attrs = chainon (returned_attrs, *decl_attrs);
7749 : 304812718 : *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
7750 : :
7751 : : /* Now TYPE has the actual type, apart from any qualifiers in
7752 : : TYPE_QUALS. */
7753 : :
7754 : : /* Warn about address space used for things other than static memory or
7755 : : pointers. */
7756 : 304812718 : address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
7757 : 304812718 : if (!ADDR_SPACE_GENERIC_P (address_space))
7758 : : {
7759 : 10 : if (decl_context == NORMAL)
7760 : : {
7761 : 10 : switch (storage_class)
7762 : : {
7763 : 0 : case csc_auto:
7764 : 0 : error ("%qs combined with %<auto%> qualifier for %qE",
7765 : : c_addr_space_name (address_space), name);
7766 : 0 : break;
7767 : 0 : case csc_register:
7768 : 0 : error ("%qs combined with %<register%> qualifier for %qE",
7769 : : c_addr_space_name (address_space), name);
7770 : 0 : break;
7771 : 6 : case csc_none:
7772 : 6 : if (current_function_scope)
7773 : : {
7774 : 0 : error ("%qs specified for auto variable %qE",
7775 : : c_addr_space_name (address_space), name);
7776 : 0 : break;
7777 : : }
7778 : : break;
7779 : : case csc_static:
7780 : : case csc_extern:
7781 : : case csc_typedef:
7782 : : break;
7783 : 0 : default:
7784 : 0 : gcc_unreachable ();
7785 : : }
7786 : : }
7787 : 0 : else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
7788 : : {
7789 : 0 : if (name)
7790 : 0 : error ("%qs specified for parameter %qE",
7791 : : c_addr_space_name (address_space), name);
7792 : : else
7793 : 0 : error ("%qs specified for unnamed parameter",
7794 : : c_addr_space_name (address_space));
7795 : : }
7796 : 0 : else if (decl_context == FIELD)
7797 : : {
7798 : 0 : if (name)
7799 : 0 : error ("%qs specified for structure field %qE",
7800 : : c_addr_space_name (address_space), name);
7801 : : else
7802 : 0 : error ("%qs specified for structure field",
7803 : : c_addr_space_name (address_space));
7804 : : }
7805 : : }
7806 : :
7807 : : /* Check the type and width of a bit-field. */
7808 : 304812718 : if (bitfield)
7809 : : {
7810 : 51791 : check_bitfield_type_and_width (loc, &type, width, name);
7811 : : /* C11 makes it implementation-defined (6.7.2.1#5) whether
7812 : : atomic types are permitted for bit-fields; we have no code to
7813 : : make bit-field accesses atomic, so disallow them. */
7814 : 51791 : if (type_quals & TYPE_QUAL_ATOMIC)
7815 : : {
7816 : 2 : if (name)
7817 : 1 : error_at (loc, "bit-field %qE has atomic type", name);
7818 : : else
7819 : 1 : error_at (loc, "bit-field has atomic type");
7820 : 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
7821 : : }
7822 : : }
7823 : :
7824 : : /* Reject invalid uses of _Alignas. */
7825 : 304812718 : if (declspecs->alignas_p)
7826 : : {
7827 : 188 : if (storage_class == csc_typedef)
7828 : 1 : error_at (loc, "alignment specified for typedef %qE", name);
7829 : 187 : else if (storage_class == csc_register)
7830 : 1 : error_at (loc, "alignment specified for %<register%> object %qE",
7831 : : name);
7832 : 186 : else if (decl_context == PARM)
7833 : : {
7834 : 2 : if (name)
7835 : 1 : error_at (loc, "alignment specified for parameter %qE", name);
7836 : : else
7837 : 1 : error_at (loc, "alignment specified for unnamed parameter");
7838 : : }
7839 : 184 : else if (bitfield)
7840 : : {
7841 : 0 : if (name)
7842 : 0 : error_at (loc, "alignment specified for bit-field %qE", name);
7843 : : else
7844 : 0 : error_at (loc, "alignment specified for unnamed bit-field");
7845 : : }
7846 : 184 : else if (TREE_CODE (type) == FUNCTION_TYPE)
7847 : 1 : error_at (loc, "alignment specified for function %qE", name);
7848 : 183 : else if (declspecs->align_log != -1 && TYPE_P (type))
7849 : : {
7850 : 156 : alignas_align = 1U << declspecs->align_log;
7851 : 156 : if (alignas_align < min_align_of_type (type))
7852 : : {
7853 : 26 : if (name)
7854 : 25 : error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7855 : : "alignment of %qE", name);
7856 : : else
7857 : 1 : error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7858 : : "alignment of unnamed field");
7859 : : alignas_align = 0;
7860 : : }
7861 : : }
7862 : : }
7863 : :
7864 : : /* If this is declaring a typedef name, return a TYPE_DECL. */
7865 : :
7866 : 304812718 : if (storage_class == csc_typedef)
7867 : : {
7868 : 4209182 : tree decl;
7869 : 4209182 : if ((type_quals & TYPE_QUAL_ATOMIC)
7870 : 11180 : && TREE_CODE (type) == FUNCTION_TYPE)
7871 : : {
7872 : 0 : error_at (loc,
7873 : : "%<_Atomic%>-qualified function type");
7874 : 0 : type_quals &= ~TYPE_QUAL_ATOMIC;
7875 : : }
7876 : 4209182 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7877 : 608 : && type_quals)
7878 : 0 : pedwarn (loc, OPT_Wpedantic,
7879 : : "ISO C forbids qualified function types");
7880 : 4208574 : if (type_quals)
7881 : 29651 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7882 : : orig_qual_indirect);
7883 : 8418364 : decl = build_decl (declarator->id_loc,
7884 : 4209182 : TYPE_DECL, declarator->u.id.id, type);
7885 : 4209182 : if (declspecs->explicit_signed_p)
7886 : 346767 : C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7887 : 4209182 : if (declspecs->inline_p)
7888 : 4 : pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7889 : 4209182 : if (declspecs->noreturn_p)
7890 : 1 : pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7891 : :
7892 : 4209182 : if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7893 : : {
7894 : 1825 : struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7895 : :
7896 : 1825 : if (b != NULL
7897 : 40 : && b->decl != NULL_TREE
7898 : 40 : && (B_IN_CURRENT_SCOPE (b)
7899 : 4 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7900 : 1861 : && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7901 : : {
7902 : 7 : auto_diagnostic_group d;
7903 : 8 : if (warning_at (declarator->id_loc, OPT_Wc___compat,
7904 : : ("using %qD as both a typedef and a tag is "
7905 : : "invalid in C++"), decl)
7906 : 7 : && b->locus != UNKNOWN_LOCATION)
7907 : 6 : inform (b->locus, "originally defined here");
7908 : 7 : }
7909 : : }
7910 : :
7911 : 4209182 : return decl;
7912 : : }
7913 : :
7914 : : /* If this is a type name (such as, in a cast or sizeof),
7915 : : compute the type and return it now. */
7916 : :
7917 : 300603536 : if (decl_context == TYPENAME)
7918 : : {
7919 : : /* Note that the grammar rejects storage classes in typenames
7920 : : and fields. */
7921 : 118273877 : gcc_assert (storage_class == csc_none && !threadp
7922 : : && !declspecs->inline_p && !declspecs->noreturn_p);
7923 : 118273877 : if ((type_quals & TYPE_QUAL_ATOMIC)
7924 : 186 : && TREE_CODE (type) == FUNCTION_TYPE)
7925 : : {
7926 : 0 : error_at (loc,
7927 : : "%<_Atomic%>-qualified function type");
7928 : 0 : type_quals &= ~TYPE_QUAL_ATOMIC;
7929 : : }
7930 : 118273877 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7931 : 22 : && type_quals)
7932 : 0 : pedwarn (loc, OPT_Wpedantic,
7933 : : "ISO C forbids const or volatile function types");
7934 : 118273855 : if (type_quals)
7935 : 739 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7936 : : orig_qual_indirect);
7937 : 118273877 : return type;
7938 : : }
7939 : :
7940 : 370152 : if (pedantic && decl_context == FIELD
7941 : 182353754 : && c_type_variably_modified_p (type))
7942 : : {
7943 : : /* C99 6.7.2.1p8 */
7944 : 6 : pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
7945 : : "have a variably modified type");
7946 : : }
7947 : :
7948 : : /* Aside from typedefs and type names (handle above),
7949 : : `void' at top level (not within pointer)
7950 : : is allowed only in public variables.
7951 : : We don't complain about parms either, but that is because
7952 : : a better error message can be made later. */
7953 : :
7954 : 182329659 : if (VOID_TYPE_P (type) && decl_context != PARM
7955 : 59 : && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
7956 : : && (storage_class == csc_extern
7957 : 27 : || (current_scope == file_scope
7958 : 18 : && !(storage_class == csc_static
7959 : : || storage_class == csc_register)))))
7960 : : {
7961 : 15 : error_at (loc, "variable or field %qE declared void", name);
7962 : 15 : type = integer_type_node;
7963 : : }
7964 : :
7965 : : /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7966 : : or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
7967 : :
7968 : 181396831 : {
7969 : 181396831 : tree decl;
7970 : :
7971 : 181396831 : if (decl_context == PARM)
7972 : : {
7973 : 120184951 : tree promoted_type;
7974 : 120184951 : bool array_parameter_p = false;
7975 : :
7976 : : /* A parameter declared as an array of T is really a pointer to T.
7977 : : One declared as a function is really a pointer to a function. */
7978 : :
7979 : 120184951 : if (TREE_CODE (type) == ARRAY_TYPE)
7980 : : {
7981 : : /* Transfer const-ness of array into that of type pointed to. */
7982 : 428931 : type = TREE_TYPE (type);
7983 : 428931 : if (orig_qual_type != NULL_TREE)
7984 : : {
7985 : 7 : if (orig_qual_indirect == 0)
7986 : 5 : orig_qual_type = TREE_TYPE (orig_qual_type);
7987 : : else
7988 : 2 : orig_qual_indirect--;
7989 : : }
7990 : 428931 : if (type_quals)
7991 : 47167 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7992 : : orig_qual_indirect);
7993 : :
7994 : : /* The pointed-to type may need a decl expr (see above). */
7995 : 428931 : if (!TYPE_NAME (type) && c_type_variably_modified_p (type))
7996 : : {
7997 : 373 : bool bind_p = decl_context == TYPENAME
7998 : : || decl_context == FIELD
7999 : : || decl_context == PARM;
8000 : 373 : add_decl_expr (loc, type, bind_p ? expr : NULL, true);
8001 : : }
8002 : :
8003 : 428931 : type = c_build_pointer_type (type);
8004 : 428931 : type_quals = array_ptr_quals;
8005 : 428931 : if (type_quals)
8006 : 930 : type = c_build_qualified_type (type, type_quals);
8007 : :
8008 : : /* We don't yet implement attributes in this context. */
8009 : 428931 : if (array_ptr_attrs != NULL_TREE)
8010 : 0 : warning_at (loc, OPT_Wattributes,
8011 : : "attributes in parameter array declarator ignored");
8012 : :
8013 : : size_varies = false;
8014 : : array_parameter_p = true;
8015 : : }
8016 : 119756020 : else if (TREE_CODE (type) == FUNCTION_TYPE)
8017 : : {
8018 : 301 : if (type_quals & TYPE_QUAL_ATOMIC)
8019 : : {
8020 : 1 : error_at (loc,
8021 : : "%<_Atomic%>-qualified function type");
8022 : 1 : type_quals &= ~TYPE_QUAL_ATOMIC;
8023 : : }
8024 : 300 : else if (type_quals)
8025 : 0 : pedwarn (loc, OPT_Wpedantic,
8026 : : "ISO C forbids qualified function types");
8027 : 1 : if (type_quals)
8028 : 0 : type = c_build_qualified_type (type, type_quals);
8029 : 301 : type = c_build_pointer_type (type);
8030 : 301 : type_quals = TYPE_UNQUALIFIED;
8031 : : }
8032 : 119755719 : else if (type_quals)
8033 : 9719219 : type = c_build_qualified_type (type, type_quals);
8034 : :
8035 : 240369902 : decl = build_decl (declarator->id_loc,
8036 : 120184951 : PARM_DECL, declarator->u.id.id, type);
8037 : 120184951 : if (size_varies)
8038 : 42 : C_DECL_VARIABLE_SIZE (decl) = 1;
8039 : 120184951 : C_ARRAY_PARAMETER (decl) = array_parameter_p;
8040 : :
8041 : : /* Compute the type actually passed in the parmlist,
8042 : : for the case where there is no prototype.
8043 : : (For example, shorts and chars are passed as ints.)
8044 : : When there is a prototype, this is overridden later. */
8045 : :
8046 : 120184951 : if (type == error_mark_node)
8047 : : promoted_type = type;
8048 : : else
8049 : 120184867 : promoted_type = c_type_promotes_to (type);
8050 : :
8051 : 120184951 : DECL_ARG_TYPE (decl) = promoted_type;
8052 : 120184951 : if (declspecs->inline_p)
8053 : 4 : pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
8054 : 120184951 : if (declspecs->noreturn_p)
8055 : 1 : pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
8056 : : }
8057 : 62144708 : else if (decl_context == FIELD)
8058 : : {
8059 : : /* Note that the grammar rejects storage classes in typenames
8060 : : and fields. */
8061 : 4158284 : gcc_assert (storage_class == csc_none && !threadp
8062 : : && !declspecs->inline_p && !declspecs->noreturn_p);
8063 : :
8064 : : /* Structure field. It may not be a function. */
8065 : :
8066 : 4158284 : if (TREE_CODE (type) == FUNCTION_TYPE)
8067 : : {
8068 : 0 : error_at (loc, "field %qE declared as a function", name);
8069 : 0 : type = c_build_pointer_type (type);
8070 : : }
8071 : 4158284 : else if (TREE_CODE (type) != ERROR_MARK
8072 : 4158284 : && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
8073 : : {
8074 : 22 : if (name)
8075 : 15 : error_at (loc, "field %qE has incomplete type", name);
8076 : : else
8077 : 7 : error_at (loc, "unnamed field has incomplete type");
8078 : 22 : type = error_mark_node;
8079 : : }
8080 : 4158262 : else if (TREE_CODE (type) == ARRAY_TYPE
8081 : 4158262 : && TYPE_DOMAIN (type) == NULL_TREE)
8082 : : {
8083 : : /* We have a flexible array member through a typedef.
8084 : : Set suitable range. Whether this is a correct position
8085 : : for a flexible array member will be determined elsewhere. */
8086 : 14 : if (!in_system_header_at (input_location))
8087 : 14 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
8088 : : "support flexible array members");
8089 : 14 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8090 : 14 : TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
8091 : : NULL_TREE);
8092 : 14 : if (orig_qual_indirect == 0)
8093 : 4158284 : orig_qual_type = NULL_TREE;
8094 : : }
8095 : 4158284 : if (type != error_mark_node
8096 : 4158284 : && !verify_type_context (loc, TCTX_FIELD, type))
8097 : 0 : type = error_mark_node;
8098 : :
8099 : 4158284 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
8100 : : orig_qual_indirect);
8101 : 8316568 : decl = build_decl (declarator->id_loc,
8102 : 4158284 : FIELD_DECL, declarator->u.id.id, type);
8103 : 4158284 : DECL_NONADDRESSABLE_P (decl) = bitfield;
8104 : 4158284 : if (bitfield && !declarator->u.id.id)
8105 : 9186 : DECL_PADDING_P (decl) = 1;
8106 : :
8107 : 4158284 : if (size_varies)
8108 : 642 : C_DECL_VARIABLE_SIZE (decl) = 1;
8109 : : }
8110 : 57986424 : else if (TREE_CODE (type) == FUNCTION_TYPE)
8111 : : {
8112 : 49247914 : if (storage_class == csc_register || threadp || constexprp)
8113 : : {
8114 : 10 : error_at (loc, "invalid storage class for function %qE", name);
8115 : : }
8116 : 49247904 : else if (current_scope != file_scope)
8117 : : {
8118 : : /* Function declaration not at file scope. Storage
8119 : : classes other than `extern' are not allowed, C99
8120 : : 6.7.1p5, and `extern' makes no difference. However,
8121 : : GCC allows 'auto', perhaps with 'inline', to support
8122 : : nested functions. */
8123 : 10474 : if (storage_class == csc_auto)
8124 : 63 : pedwarn (loc, OPT_Wpedantic,
8125 : : "invalid storage class for function %qE", name);
8126 : 10411 : else if (storage_class == csc_static)
8127 : : {
8128 : 19 : error_at (loc, "invalid storage class for function %qE", name);
8129 : 19 : if (funcdef_flag)
8130 : 8 : storage_class = declspecs->storage_class = csc_none;
8131 : : else
8132 : : return NULL_TREE;
8133 : : }
8134 : : }
8135 : :
8136 : 98495806 : decl = build_decl (declarator->id_loc,
8137 : 49247903 : FUNCTION_DECL, declarator->u.id.id, type);
8138 : 49247903 : decl = build_decl_attribute_variant (decl, decl_attr);
8139 : :
8140 : 49247903 : if (type_quals & TYPE_QUAL_ATOMIC)
8141 : : {
8142 : 1 : error_at (loc,
8143 : : "%<_Atomic%>-qualified function type");
8144 : 1 : type_quals &= ~TYPE_QUAL_ATOMIC;
8145 : : }
8146 : 49247902 : else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
8147 : 6 : pedwarn (loc, OPT_Wpedantic,
8148 : : "ISO C forbids qualified function types");
8149 : :
8150 : : /* Every function declaration is an external reference
8151 : : (DECL_EXTERNAL) except for those which are not at file
8152 : : scope and are explicitly declared "auto". This is
8153 : : forbidden by standard C (C99 6.7.1p5) and is interpreted by
8154 : : GCC to signify a forward declaration of a nested function. */
8155 : 49247903 : if (storage_class == csc_auto && current_scope != file_scope)
8156 : 63 : DECL_EXTERNAL (decl) = 0;
8157 : : /* In C99, a function which is declared 'inline' with 'extern'
8158 : : is not an external reference (which is confusing). It
8159 : : means that the later definition of the function must be output
8160 : : in this file, C99 6.7.4p6. In GNU C89, a function declared
8161 : : 'extern inline' is an external reference. */
8162 : 49247840 : else if (declspecs->inline_p && storage_class != csc_static)
8163 : 34519855 : DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
8164 : 34519855 : == flag_gnu89_inline);
8165 : : else
8166 : 14727985 : DECL_EXTERNAL (decl) = !initialized;
8167 : :
8168 : : /* Record absence of global scope for `static' or `auto'. */
8169 : 49247903 : TREE_PUBLIC (decl)
8170 : 49247903 : = !(storage_class == csc_static || storage_class == csc_auto);
8171 : :
8172 : : /* For a function definition, record the argument information
8173 : : block where store_parm_decls will look for it. */
8174 : 49247903 : if (funcdef_flag)
8175 : 35349685 : current_function_arg_info = arg_info;
8176 : :
8177 : 49247903 : if (declspecs->default_int_p)
8178 : 9219 : C_FUNCTION_IMPLICIT_INT (decl) = 1;
8179 : :
8180 : : /* Record presence of `inline' and `_Noreturn', if it is
8181 : : reasonable. */
8182 : 49247903 : if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
8183 : : {
8184 : 48084 : if (declspecs->inline_p)
8185 : 5 : pedwarn (loc, 0, "cannot inline function %<main%>");
8186 : 48084 : if (declspecs->noreturn_p)
8187 : 1 : pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
8188 : : }
8189 : : else
8190 : : {
8191 : 49199819 : if (declspecs->inline_p)
8192 : : /* Record that the function is declared `inline'. */
8193 : 34676019 : DECL_DECLARED_INLINE_P (decl) = 1;
8194 : 49199819 : if (declspecs->noreturn_p)
8195 : : {
8196 : 22955 : if (flag_isoc99)
8197 : 22952 : pedwarn_c99 (loc, OPT_Wpedantic,
8198 : : "ISO C99 does not support %<_Noreturn%>");
8199 : : else
8200 : 3 : pedwarn_c99 (loc, OPT_Wpedantic,
8201 : : "ISO C90 does not support %<_Noreturn%>");
8202 : 22955 : TREE_THIS_VOLATILE (decl) = 1;
8203 : : }
8204 : : }
8205 : :
8206 : : /* C99 6.2.2p7: It is invalid (compile-time undefined
8207 : : behavior) to create an 'extern' declaration for a
8208 : : function if there is a global declaration that is
8209 : : 'static' and the global declaration is not visible.
8210 : : (If the static declaration _is_ currently visible,
8211 : : the 'extern' declaration is taken to refer to that decl.) */
8212 : 49247903 : if (!initialized
8213 : 13898209 : && TREE_PUBLIC (decl)
8214 : 13890621 : && current_scope != file_scope)
8215 : : {
8216 : 8868 : tree global_decl = identifier_global_value (declarator->u.id.id);
8217 : 8868 : tree visible_decl = lookup_name (declarator->u.id.id);
8218 : :
8219 : 8868 : if (global_decl
8220 : 8868 : && global_decl != visible_decl
8221 : 1712 : && VAR_OR_FUNCTION_DECL_P (global_decl)
8222 : 1712 : && !TREE_PUBLIC (global_decl))
8223 : 1 : error_at (loc, "function previously declared %<static%> "
8224 : : "redeclared %<extern%>");
8225 : : }
8226 : : }
8227 : : else
8228 : : {
8229 : : /* It's a variable. */
8230 : : /* An uninitialized decl with `extern' is a reference. */
8231 : 8738510 : int extern_ref = !initialized && storage_class == csc_extern;
8232 : :
8233 : 8738510 : if (constexprp)
8234 : : {
8235 : : /* The type of a constexpr variable must not be variably
8236 : : modified, volatile, atomic or restrict qualified or
8237 : : have a member with such a qualifier. const
8238 : : qualification is implicitly added, and, at file scope,
8239 : : has internal linkage. */
8240 : 345 : if (c_type_variably_modified_p (type))
8241 : 1 : error_at (loc, "%<constexpr%> object has variably modified "
8242 : : "type");
8243 : 345 : if (type_quals
8244 : 345 : & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
8245 : 9 : error_at (loc, "invalid qualifiers for %<constexpr%> object");
8246 : : else
8247 : : {
8248 : 336 : tree type_no_array = strip_array_types (type);
8249 : 336 : if (RECORD_OR_UNION_TYPE_P (type_no_array)
8250 : 336 : && C_TYPE_FIELDS_NON_CONSTEXPR (type_no_array))
8251 : 8 : error_at (loc, "invalid qualifiers for field of "
8252 : : "%<constexpr%> object");
8253 : : }
8254 : 345 : type_quals |= TYPE_QUAL_CONST;
8255 : 345 : if (current_scope == file_scope)
8256 : 292 : storage_class = csc_static;
8257 : : }
8258 : :
8259 : 8738510 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
8260 : : orig_qual_indirect);
8261 : :
8262 : : /* C99 6.2.2p7: It is invalid (compile-time undefined
8263 : : behavior) to create an 'extern' declaration for a
8264 : : variable if there is a global declaration that is
8265 : : 'static' and the global declaration is not visible.
8266 : : (If the static declaration _is_ currently visible,
8267 : : the 'extern' declaration is taken to refer to that decl.) */
8268 : 8738510 : if (extern_ref && current_scope != file_scope)
8269 : : {
8270 : 1567 : tree global_decl = identifier_global_value (declarator->u.id.id);
8271 : 1567 : tree visible_decl = lookup_name (declarator->u.id.id);
8272 : :
8273 : 1567 : if (global_decl
8274 : 1567 : && global_decl != visible_decl
8275 : 281 : && VAR_P (global_decl)
8276 : 281 : && !TREE_PUBLIC (global_decl))
8277 : 7 : error_at (loc, "variable previously declared %<static%> "
8278 : : "redeclared %<extern%>");
8279 : : }
8280 : :
8281 : 17477020 : decl = build_decl (declarator->id_loc,
8282 : 8738510 : VAR_DECL, declarator->u.id.id, type);
8283 : 8738510 : if (size_varies)
8284 : 7388 : C_DECL_VARIABLE_SIZE (decl) = 1;
8285 : 8738510 : if (constexprp)
8286 : 345 : C_DECL_DECLARED_CONSTEXPR (decl) = 1;
8287 : :
8288 : 8738510 : if (declspecs->inline_p)
8289 : 4 : pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
8290 : 8738510 : if (declspecs->noreturn_p)
8291 : 1 : pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
8292 : :
8293 : : /* At file scope, an initialized extern declaration may follow
8294 : : a static declaration. In that case, DECL_EXTERNAL will be
8295 : : reset later in start_decl. */
8296 : 8738510 : DECL_EXTERNAL (decl) = (storage_class == csc_extern);
8297 : :
8298 : : /* At file scope, the presence of a `static' or `register' storage
8299 : : class specifier, or the absence of all storage class specifiers
8300 : : makes this declaration a definition (perhaps tentative). Also,
8301 : : the absence of `static' makes it public. */
8302 : 8738510 : if (current_scope == file_scope)
8303 : : {
8304 : 1149467 : TREE_PUBLIC (decl) = storage_class != csc_static;
8305 : 1149467 : TREE_STATIC (decl) = !extern_ref;
8306 : : }
8307 : : /* Not at file scope, only `static' makes a static definition. */
8308 : : else
8309 : : {
8310 : 7589043 : TREE_STATIC (decl) = (storage_class == csc_static);
8311 : 7589043 : TREE_PUBLIC (decl) = extern_ref;
8312 : : }
8313 : :
8314 : 8738510 : if (threadp)
8315 : 2783 : set_decl_tls_model (decl, decl_default_tls_model (decl));
8316 : : }
8317 : :
8318 : 182329648 : if ((storage_class == csc_extern
8319 : 134052216 : || (storage_class == csc_none
8320 : 133604719 : && TREE_CODE (type) == FUNCTION_TYPE
8321 : 795824 : && !funcdef_flag))
8322 : 182632920 : && c_type_variably_modified_p (type))
8323 : : {
8324 : : /* C99 6.7.5.2p2 */
8325 : 6 : if (TREE_CODE (type) == FUNCTION_TYPE)
8326 : 4 : error_at (loc, "non-nested function with variably modified type");
8327 : : else
8328 : 2 : error_at (loc, "object with variably modified type must have "
8329 : : "no linkage");
8330 : : }
8331 : :
8332 : : /* For nested functions disqualify ones taking VLAs by value
8333 : : from inlining since the middle-end cannot deal with this.
8334 : : ??? We should arrange for those to be passed by reference
8335 : : with emitting the copy on the caller side in the frontend. */
8336 : 182329648 : if (storage_class == csc_none
8337 : 133604719 : && TREE_CODE (type) == FUNCTION_TYPE)
8338 : 4140063 : for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
8339 : : {
8340 : 3344279 : tree arg = TREE_VALUE (al);
8341 : 3344279 : if (arg != error_mark_node
8342 : 3344279 : && C_TYPE_VARIABLE_SIZE (arg))
8343 : : {
8344 : 40 : DECL_UNINLINABLE (decl) = 1;
8345 : 40 : break;
8346 : : }
8347 : : }
8348 : :
8349 : : /* Record `register' declaration for warnings on &
8350 : : and in case doing stupid register allocation. */
8351 : :
8352 : 182329648 : if (storage_class == csc_register)
8353 : : {
8354 : 3564 : C_DECL_REGISTER (decl) = 1;
8355 : 3564 : DECL_REGISTER (decl) = 1;
8356 : : }
8357 : :
8358 : : /* Record constancy and volatility. */
8359 : 182329648 : c_apply_type_quals_to_decl (type_quals, decl);
8360 : :
8361 : : /* Apply _Alignas specifiers. */
8362 : 182329648 : if (alignas_align)
8363 : : {
8364 : 123 : SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
8365 : 123 : DECL_USER_ALIGN (decl) = 1;
8366 : : }
8367 : :
8368 : : /* If a type has volatile components, it should be stored in memory.
8369 : : Otherwise, the fact that those components are volatile
8370 : : will be ignored, and would even crash the compiler.
8371 : : Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
8372 : 182329648 : if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
8373 : 182329648 : && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
8374 : : || TREE_CODE (decl) == RESULT_DECL))
8375 : : {
8376 : : /* It is not an error for a structure with volatile fields to
8377 : : be declared register, but reset DECL_REGISTER since it
8378 : : cannot actually go in a register. */
8379 : 163 : int was_reg = C_DECL_REGISTER (decl);
8380 : 163 : C_DECL_REGISTER (decl) = 0;
8381 : 163 : DECL_REGISTER (decl) = 0;
8382 : 163 : c_mark_addressable (decl);
8383 : 163 : C_DECL_REGISTER (decl) = was_reg;
8384 : : }
8385 : :
8386 : : /* This is the earliest point at which we might know the assembler
8387 : : name of a variable. Thus, if it's known before this, die horribly. */
8388 : 182329648 : gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
8389 : : || !DECL_ASSEMBLER_NAME_SET_P (decl));
8390 : :
8391 : 182329648 : if (warn_cxx_compat
8392 : 24060 : && VAR_P (decl)
8393 : 7350 : && TREE_PUBLIC (decl)
8394 : 2788 : && TREE_STATIC (decl)
8395 : 2342 : && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
8396 : 2210 : || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
8397 : 182329795 : && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
8398 : 4 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
8399 : : ("non-local variable %qD with anonymous type is "
8400 : : "questionable in C++"),
8401 : : decl);
8402 : :
8403 : : return decl;
8404 : : }
8405 : : }
8406 : :
8407 : : /* Decode the parameter-list info for a function type or function definition.
8408 : : The argument is the value returned by `get_parm_info' (or made in c-parse.c
8409 : : if there is an identifier list instead of a parameter decl list).
8410 : : These two functions are separate because when a function returns
8411 : : or receives functions then each is called multiple times but the order
8412 : : of calls is different. The last call to `grokparms' is always the one
8413 : : that contains the formal parameter names of a function definition.
8414 : :
8415 : : Return a list of arg types to use in the FUNCTION_TYPE for this function.
8416 : :
8417 : : FUNCDEF_FLAG is true for a function definition, false for
8418 : : a mere declaration. A nonempty identifier-list gets an error message
8419 : : when FUNCDEF_FLAG is false. */
8420 : :
8421 : : static tree
8422 : 48828622 : grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
8423 : : {
8424 : 48828622 : tree arg_types = arg_info->types;
8425 : :
8426 : 48828622 : if (funcdef_flag && arg_info->had_vla_unspec)
8427 : : {
8428 : : /* A function definition isn't function prototype scope C99 6.2.1p4. */
8429 : : /* C99 6.7.5.2p4 */
8430 : 2 : error ("%<[*]%> not allowed in other than function prototype scope");
8431 : : }
8432 : :
8433 : 715213 : if (arg_types == NULL_TREE && !funcdef_flag && !flag_isoc23
8434 : 48831645 : && !in_system_header_at (input_location))
8435 : 3023 : warning (OPT_Wstrict_prototypes,
8436 : : "function declaration isn%'t a prototype");
8437 : :
8438 : 48828622 : if (arg_types == error_mark_node)
8439 : : /* Don't set TYPE_ARG_TYPES in this case. */
8440 : : return NULL_TREE;
8441 : :
8442 : 96885749 : else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
8443 : : {
8444 : 8684 : if (!funcdef_flag)
8445 : : {
8446 : 13 : permerror_opt (input_location,
8447 : 13 : OPT_Wdeclaration_missing_parameter_type,
8448 : : "parameter names (without types) in "
8449 : : "function declaration");
8450 : 13 : arg_info->parms = NULL_TREE;
8451 : : }
8452 : : else
8453 : 8671 : arg_info->parms = arg_info->types;
8454 : :
8455 : 8684 : arg_info->types = NULL_TREE;
8456 : 8684 : return NULL_TREE;
8457 : : }
8458 : : else
8459 : : {
8460 : 48819938 : tree parm, type, typelt;
8461 : 48819938 : unsigned int parmno;
8462 : :
8463 : : /* In C23, convert () to (void). */
8464 : 48819938 : if (flag_isoc23
8465 : 40284261 : && !arg_types
8466 : 764044 : && !arg_info->parms
8467 : 764044 : && !arg_info->no_named_args_stdarg_p)
8468 : : {
8469 : 763903 : arg_types = arg_info->types = void_list_node;
8470 : 763903 : arg_info->c23_empty_parens = 1;
8471 : : }
8472 : :
8473 : : /* If there is a parameter of incomplete type in a definition,
8474 : : this is an error. In a declaration this is valid, and a
8475 : : struct or union type may be completed later, before any calls
8476 : : or definition of the function. In the case where the tag was
8477 : : first declared within the parameter list, a warning has
8478 : : already been given. If a parameter has void type, then
8479 : : this has already received an error (constraint violation in C2Y,
8480 : : previously implicitly undefined behavior). */
8481 : :
8482 : 48819938 : for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
8483 : 168049239 : parm;
8484 : 119229301 : parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
8485 : : {
8486 : 119229301 : type = TREE_VALUE (typelt);
8487 : 119229301 : if (type == error_mark_node)
8488 : 61 : continue;
8489 : :
8490 : 119229240 : if (!COMPLETE_TYPE_P (type))
8491 : : {
8492 : 40 : if (funcdef_flag)
8493 : : {
8494 : 13 : if (DECL_NAME (parm))
8495 : 13 : error_at (input_location,
8496 : : "parameter %u (%q+D) has incomplete type",
8497 : : parmno, parm);
8498 : : else
8499 : 0 : error_at (DECL_SOURCE_LOCATION (parm),
8500 : : "parameter %u has incomplete type",
8501 : : parmno);
8502 : :
8503 : 13 : TREE_VALUE (typelt) = error_mark_node;
8504 : 13 : TREE_TYPE (parm) = error_mark_node;
8505 : 13 : arg_types = NULL_TREE;
8506 : : }
8507 : : }
8508 : :
8509 : 119229240 : if (DECL_NAME (parm) && TREE_USED (parm))
8510 : 17501 : warn_if_shadowing (parm);
8511 : : }
8512 : : return arg_types;
8513 : : }
8514 : : }
8515 : :
8516 : : /* Allocate and initialize a c_arg_info structure from the parser's
8517 : : obstack. */
8518 : :
8519 : : struct c_arg_info *
8520 : 48828639 : build_arg_info (void)
8521 : : {
8522 : 48828639 : struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
8523 : 48828639 : ret->parms = NULL_TREE;
8524 : 48828639 : ret->tags = NULL;
8525 : 48828639 : ret->types = NULL_TREE;
8526 : 48828639 : ret->others = NULL_TREE;
8527 : 48828639 : ret->pending_sizes = NULL;
8528 : 48828639 : ret->had_vla_unspec = 0;
8529 : 48828639 : ret->no_named_args_stdarg_p = 0;
8530 : 48828639 : ret->c23_empty_parens = 0;
8531 : 48828639 : return ret;
8532 : : }
8533 : :
8534 : : /* Take apart the current scope and return a c_arg_info structure with
8535 : : info on a parameter list just parsed.
8536 : :
8537 : : This structure is later fed to 'grokparms' and 'store_parm_decls'.
8538 : :
8539 : : ELLIPSIS being true means the argument list ended in '...' so don't
8540 : : append a sentinel (void_list_node) to the end of the type-list.
8541 : :
8542 : : EXPR is NULL or an expression that needs to be evaluated for the
8543 : : side effects of array size expressions in the parameters. */
8544 : :
8545 : : struct c_arg_info *
8546 : 48048472 : get_parm_info (bool ellipsis, tree expr)
8547 : : {
8548 : 48048472 : struct c_binding *b = current_scope->bindings;
8549 : 48048472 : struct c_arg_info *arg_info = build_arg_info ();
8550 : :
8551 : 48048472 : tree parms = NULL_TREE;
8552 : 48048472 : vec<c_arg_tag, va_gc> *tags = NULL;
8553 : 48048472 : tree types = NULL_TREE;
8554 : 48048472 : tree others = NULL_TREE;
8555 : :
8556 : 48048472 : bool gave_void_only_once_err = false;
8557 : :
8558 : 48048472 : arg_info->had_vla_unspec = current_scope->had_vla_unspec;
8559 : :
8560 : : /* The bindings in this scope must not get put into a block.
8561 : : We will take care of deleting the binding nodes. */
8562 : 48048472 : current_scope->bindings = 0;
8563 : :
8564 : : /* This function is only called if there was *something* on the
8565 : : parameter list. */
8566 : 48048472 : gcc_assert (b);
8567 : :
8568 : : /* A parameter list consisting solely of 'void' indicates that the
8569 : : function takes no arguments. But if the 'void' is qualified
8570 : : (by 'const' or 'volatile'), or has a storage class specifier
8571 : : ('register'), then the behavior is undefined; issue an error.
8572 : : Typedefs for 'void' are OK (see DR#157). */
8573 : 48048472 : if (b->prev == 0 /* one binding */
8574 : 12722170 : && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
8575 : 12722170 : && !DECL_NAME (b->decl) /* anonymous */
8576 : 49824089 : && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
8577 : : {
8578 : 932714 : if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
8579 : 932714 : || C_DECL_REGISTER (b->decl))
8580 : 96 : error_at (b->locus, "%<void%> as only parameter may not be qualified");
8581 : :
8582 : : /* There cannot be an ellipsis. */
8583 : 932714 : if (ellipsis)
8584 : 17 : error_at (b->locus, "%<void%> must be the only parameter");
8585 : :
8586 : 932714 : arg_info->types = void_list_node;
8587 : 932714 : return arg_info;
8588 : : }
8589 : :
8590 : 47115758 : if (!ellipsis)
8591 : 46905473 : types = void_list_node;
8592 : :
8593 : : /* Break up the bindings list into parms, tags, types, and others;
8594 : : apply sanity checks; purge the name-to-decl bindings. */
8595 : 166345506 : while (b)
8596 : : {
8597 : 119229748 : tree decl = b->decl;
8598 : 119229748 : tree type = TREE_TYPE (decl);
8599 : 119229748 : c_arg_tag tag;
8600 : 119229748 : const char *keyword;
8601 : :
8602 : 119229748 : switch (TREE_CODE (decl))
8603 : : {
8604 : 119229424 : case PARM_DECL:
8605 : 119229424 : if (b->id)
8606 : : {
8607 : 115682458 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8608 : 115682458 : I_SYMBOL_BINDING (b->id) = b->shadowed;
8609 : : }
8610 : :
8611 : : /* Check for forward decls that never got their actual decl. */
8612 : 119229424 : if (TREE_ASM_WRITTEN (decl))
8613 : 4 : error_at (b->locus,
8614 : : "parameter %q+D has just a forward declaration", decl);
8615 : : /* Check for (..., void, ...) and named void parameters and issue an
8616 : : error. */
8617 : 119229420 : else if (VOID_TYPE_P (type))
8618 : : {
8619 : 114 : if (!gave_void_only_once_err)
8620 : : {
8621 : 114 : error_at (b->locus,
8622 : : "%<void%> must be the only parameter and unnamed");
8623 : 114 : gave_void_only_once_err = true;
8624 : : }
8625 : : }
8626 : : else
8627 : : {
8628 : : /* Valid parameter, add it to the list. */
8629 : 119229306 : DECL_CHAIN (decl) = parms;
8630 : 119229306 : parms = decl;
8631 : :
8632 : : /* Since there is a prototype, args are passed in their
8633 : : declared types. The back end may override this later. */
8634 : 119229306 : DECL_ARG_TYPE (decl) = type;
8635 : 119229306 : types = tree_cons (0, type, types);
8636 : : }
8637 : : break;
8638 : :
8639 : 21 : case ENUMERAL_TYPE: keyword = "enum"; goto tag;
8640 : 19 : case UNION_TYPE: keyword = "union"; goto tag;
8641 : 94 : case RECORD_TYPE: keyword = "struct"; goto tag;
8642 : 134 : tag:
8643 : : /* Types may not have tag-names, in which case the type
8644 : : appears in the bindings list with b->id NULL. */
8645 : 134 : if (b->id)
8646 : : {
8647 : 92 : gcc_assert (I_TAG_BINDING (b->id) == b);
8648 : 92 : I_TAG_BINDING (b->id) = b->shadowed;
8649 : : }
8650 : :
8651 : : /* Warn about any struct, union or enum tags defined in a
8652 : : parameter list. The scope of such types is limited to
8653 : : the parameter list, which is rarely if ever desirable
8654 : : (it's impossible to call such a function with type-
8655 : : correct arguments). An anonymous union parm type is
8656 : : meaningful as a GNU extension, so don't warn for that. */
8657 : 134 : if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
8658 : : {
8659 : 115 : if (b->id)
8660 : : {
8661 : : /* The %s will be one of 'struct', 'union', or 'enum'. */
8662 : 92 : if (!flag_isoc23 || !COMPLETE_TYPE_P (decl))
8663 : 72 : warning_at (b->locus, 0,
8664 : : "%<%s %E%> declared inside parameter list"
8665 : : " will not be visible outside of this definition or"
8666 : : " declaration", keyword, b->id);
8667 : : }
8668 : : else
8669 : : /* The %s will be one of 'struct', 'union', or 'enum'. */
8670 : 23 : warning_at (b->locus, 0,
8671 : : "anonymous %s declared inside parameter list"
8672 : : " will not be visible outside of this definition or"
8673 : : " declaration", keyword);
8674 : : }
8675 : :
8676 : 134 : tag.id = b->id;
8677 : 134 : tag.type = decl;
8678 : 134 : vec_safe_push (tags, tag);
8679 : 134 : break;
8680 : :
8681 : 20 : case FUNCTION_DECL:
8682 : : /* FUNCTION_DECLs appear when there is an implicit function
8683 : : declaration in the parameter list. */
8684 : 20 : gcc_assert (b->nested || seen_error ());
8685 : 20 : goto set_shadowed;
8686 : :
8687 : 136 : case CONST_DECL:
8688 : 136 : case TYPE_DECL:
8689 : : /* CONST_DECLs appear here when we have an embedded enum,
8690 : : and TYPE_DECLs appear here when we have an embedded struct
8691 : : or union. No warnings for this - we already warned about the
8692 : : type itself. */
8693 : :
8694 : : /* When we reinsert this decl in the function body, we need
8695 : : to reconstruct whether it was marked as nested. */
8696 : 136 : gcc_assert (!b->nested);
8697 : 136 : DECL_CHAIN (decl) = others;
8698 : 136 : others = decl;
8699 : : /* fall through */
8700 : :
8701 : 190 : case ERROR_MARK:
8702 : 190 : set_shadowed:
8703 : : /* error_mark_node appears here when we have an undeclared
8704 : : variable. Just throw it away. */
8705 : 190 : if (b->id)
8706 : : {
8707 : 56 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8708 : 56 : I_SYMBOL_BINDING (b->id) = b->shadowed;
8709 : : }
8710 : : break;
8711 : :
8712 : : /* Other things that might be encountered. */
8713 : 0 : case LABEL_DECL:
8714 : 0 : case VAR_DECL:
8715 : 0 : default:
8716 : 0 : gcc_unreachable ();
8717 : : }
8718 : :
8719 : 119229748 : b = free_binding_and_advance (b);
8720 : : }
8721 : :
8722 : 47115758 : arg_info->parms = parms;
8723 : 47115758 : arg_info->tags = tags;
8724 : 47115758 : arg_info->types = types;
8725 : 47115758 : arg_info->others = others;
8726 : 47115758 : arg_info->pending_sizes = expr;
8727 : 47115758 : arg_info->no_named_args_stdarg_p = ellipsis && !types;
8728 : 47115758 : return arg_info;
8729 : : }
8730 : :
8731 : : /* Get the struct, enum or union (CODE says which) with tag NAME.
8732 : : Define the tag as a forward-reference with location LOC if it is
8733 : : not defined. HAVE_STD_ATTRS says whether any standard attributes
8734 : : were present after the struct, union or enum keyword; ATTRS are the
8735 : : standard attributes present there. HAS_ENUM_TYPE_SPECIFIER says
8736 : : whether an enum type specifier (": specifier-qualifier-list") is
8737 : : present; if so, this is called before that specifier is parsed, so
8738 : : that the tag is in scope for that specifier. Return a c_typespec
8739 : : structure for the type specifier. */
8740 : :
8741 : : struct c_typespec
8742 : 1063399 : parser_xref_tag (location_t loc, enum tree_code code, tree name,
8743 : : bool have_std_attrs, tree attrs, bool has_enum_type_specifier)
8744 : : {
8745 : 1063399 : struct c_typespec ret;
8746 : 1063399 : tree ref;
8747 : 1063399 : location_t refloc;
8748 : :
8749 : 1063399 : ret.expr = NULL_TREE;
8750 : 1063399 : ret.expr_const_operands = true;
8751 : 1063399 : ret.has_enum_type_specifier = has_enum_type_specifier;
8752 : :
8753 : : /* If a cross reference is requested, look up the type already
8754 : : defined for this tag and return it. If an enum type specifier is
8755 : : present, only a definition in the current scope is relevant. */
8756 : :
8757 : 1063399 : ref = lookup_tag (code, name, has_enum_type_specifier, &refloc);
8758 : :
8759 : : /* If the visble type is still being defined, see if there is
8760 : : an earlier definition (which may be complete). We do not
8761 : : have to loop because nested redefinitions are not allowed. */
8762 : 1063399 : if (flag_isoc23 && ref && C_TYPE_BEING_DEFINED (ref))
8763 : : {
8764 : 83831 : tree vis = previous_tag (ref);
8765 : 83831 : if (vis)
8766 : 16 : ref = vis;
8767 : : }
8768 : :
8769 : : /* If this is the right type of tag, return what we found.
8770 : : (This reference will be shadowed by shadow_tag later if appropriate.)
8771 : : If this is the wrong type of tag, do not return it. If it was the
8772 : : wrong type in the same scope, we will have had an error
8773 : : message already; if in a different scope and declaring
8774 : : a name, pending_xref_error will give an error message; but if in a
8775 : : different scope and not declaring a name, this tag should
8776 : : shadow the previous declaration of a different type of tag, and
8777 : : this would not work properly if we return the reference found.
8778 : : (For example, with "struct foo" in an outer scope, "union foo;"
8779 : : must shadow that tag with a new one of union type.) */
8780 : 2126798 : ret.kind = (ref
8781 : 1063399 : ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
8782 : 92789 : : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
8783 : 1063399 : if (ref && TREE_CODE (ref) == code)
8784 : : {
8785 : 970576 : decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8786 : 970576 : if (C_TYPE_DEFINED_IN_STRUCT (ref)
8787 : 5 : && loc != UNKNOWN_LOCATION
8788 : 970581 : && warn_cxx_compat)
8789 : : {
8790 : 5 : auto_diagnostic_group d;
8791 : 5 : switch (code)
8792 : : {
8793 : 2 : case ENUMERAL_TYPE:
8794 : 2 : if (warning_at (loc, OPT_Wc___compat,
8795 : : ("enum type defined in struct or union "
8796 : : "is not visible in C++")))
8797 : 2 : inform (refloc, "enum type defined here");
8798 : : break;
8799 : 2 : case RECORD_TYPE:
8800 : 2 : if (warning_at (loc, OPT_Wc___compat,
8801 : : ("struct defined in struct or union "
8802 : : "is not visible in C++")))
8803 : 2 : inform (refloc, "struct defined here");
8804 : : break;
8805 : 1 : case UNION_TYPE:
8806 : 1 : if (warning_at (loc, OPT_Wc___compat,
8807 : : ("union defined in struct or union "
8808 : : "is not visible in C++")))
8809 : 1 : inform (refloc, "union defined here");
8810 : : break;
8811 : 0 : default:
8812 : 0 : gcc_unreachable();
8813 : : }
8814 : 5 : }
8815 : :
8816 : 970576 : ret.spec = ref;
8817 : 970576 : return ret;
8818 : : }
8819 : :
8820 : : /* If no such tag is yet defined, create a forward-reference node
8821 : : and record it as the "definition".
8822 : : When a real declaration of this type is found,
8823 : : the forward-reference will be altered into a real type. */
8824 : :
8825 : 92823 : ref = make_node (code);
8826 : 92823 : if (flag_isoc23 || code == ENUMERAL_TYPE)
8827 : 70637 : SET_TYPE_STRUCTURAL_EQUALITY (ref);
8828 : 70637 : if (code == ENUMERAL_TYPE)
8829 : : {
8830 : : /* Give the type a default layout like unsigned int
8831 : : to avoid crashing if it does not get defined. */
8832 : 246 : SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
8833 : 246 : SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
8834 : 246 : TYPE_USER_ALIGN (ref) = 0;
8835 : 246 : TYPE_UNSIGNED (ref) = 1;
8836 : 246 : TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
8837 : 246 : TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
8838 : 246 : TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
8839 : 246 : ENUM_FIXED_UNDERLYING_TYPE_P (ref) = has_enum_type_specifier;
8840 : : }
8841 : :
8842 : 92823 : pushtag (loc, name, ref);
8843 : 92823 : decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8844 : 92823 : if (in_underspecified_init)
8845 : 9 : error_at (loc, "%qT declared in underspecified object initializer",
8846 : : ref);
8847 : :
8848 : 92823 : ret.spec = ref;
8849 : 92823 : return ret;
8850 : : }
8851 : :
8852 : : /* Get the struct, enum or union (CODE says which) with tag NAME.
8853 : : Define the tag as a forward-reference if it is not defined.
8854 : : Return a tree for the type. */
8855 : :
8856 : : tree
8857 : 0 : xref_tag (enum tree_code code, tree name)
8858 : : {
8859 : 0 : return parser_xref_tag (input_location, code, name, false, NULL_TREE,
8860 : 0 : false).spec;
8861 : : }
8862 : :
8863 : : /* Make sure that the tag NAME is defined *in the current scope*
8864 : : at least as a forward reference.
8865 : : LOC is the location of the struct's definition.
8866 : : CODE says which kind of tag NAME ought to be.
8867 : :
8868 : : This stores the current value of the file static STRUCT_PARSE_INFO
8869 : : in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8870 : : new c_struct_parse_info structure. The old value of
8871 : : STRUCT_PARSE_INFO is restored in finish_struct. */
8872 : :
8873 : : tree
8874 : 1149636 : start_struct (location_t loc, enum tree_code code, tree name,
8875 : : class c_struct_parse_info **enclosing_struct_parse_info)
8876 : : {
8877 : : /* If there is already a tag defined at this scope
8878 : : (as a forward reference), just return it. */
8879 : :
8880 : 1149636 : tree ref = NULL_TREE;
8881 : 1149636 : location_t refloc = UNKNOWN_LOCATION;
8882 : :
8883 : 1149636 : if (name != NULL_TREE)
8884 : 474547 : ref = lookup_tag (code, name, true, &refloc);
8885 : :
8886 : : /* For C23, even if we already have a completed definition,
8887 : : we do not use it. We will check for consistency later.
8888 : : If we are in a nested redefinition the type is not
8889 : : complete. We will then detect this below. */
8890 : 1149636 : if (flag_isoc23 && ref && TYPE_SIZE (ref))
8891 : : ref = NULL_TREE;
8892 : :
8893 : 1149536 : if (ref && TREE_CODE (ref) == code)
8894 : : {
8895 : 24642 : if (TYPE_STUB_DECL (ref))
8896 : 24642 : refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8897 : :
8898 : 24642 : if (TYPE_SIZE (ref))
8899 : : {
8900 : 11 : auto_diagnostic_group d;
8901 : 11 : if (code == UNION_TYPE)
8902 : 2 : error_at (loc, "redefinition of %<union %E%>", name);
8903 : : else
8904 : 9 : error_at (loc, "redefinition of %<struct %E%>", name);
8905 : 11 : if (refloc != UNKNOWN_LOCATION)
8906 : 11 : inform (refloc, "originally defined here");
8907 : : /* Don't create structures using a name already in use. */
8908 : 11 : ref = NULL_TREE;
8909 : 11 : }
8910 : 24631 : else if (C_TYPE_BEING_DEFINED (ref))
8911 : : {
8912 : 17 : if (code == UNION_TYPE)
8913 : 1 : error_at (loc, "nested redefinition of %<union %E%>", name);
8914 : : else
8915 : 16 : error_at (loc, "nested redefinition of %<struct %E%>", name);
8916 : : /* Don't bother to report "originally defined here" for a
8917 : : nested redefinition; the original definition should be
8918 : : obvious. */
8919 : : /* Don't create structures that contain themselves. */
8920 : : ref = NULL_TREE;
8921 : : }
8922 : : }
8923 : :
8924 : : /* Otherwise create a forward-reference just so the tag is in scope. */
8925 : :
8926 : 24628 : if (ref == NULL_TREE || TREE_CODE (ref) != code)
8927 : : {
8928 : 1125022 : ref = make_node (code);
8929 : 1125022 : if (flag_isoc23)
8930 : 897051 : SET_TYPE_STRUCTURAL_EQUALITY (ref);
8931 : 1125022 : pushtag (loc, name, ref);
8932 : : }
8933 : :
8934 : 1149636 : C_TYPE_BEING_DEFINED (ref) = 1;
8935 : 2329317 : for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
8936 : 1179681 : TYPE_PACKED (v) = flag_pack_struct;
8937 : :
8938 : 1149636 : *enclosing_struct_parse_info = struct_parse_info;
8939 : 1149636 : struct_parse_info = new c_struct_parse_info ();
8940 : 1149636 : struct_parse_info->refloc = refloc;
8941 : :
8942 : : /* FIXME: This will issue a warning for a use of a type defined
8943 : : within a statement expr used within sizeof, et. al. This is not
8944 : : terribly serious as C++ doesn't permit statement exprs within
8945 : : sizeof anyhow. */
8946 : 1149636 : if (warn_cxx_compat
8947 : 574 : && (in_sizeof || in_typeof || in_alignof || in_countof))
8948 : 10 : warning_at (loc, OPT_Wc___compat,
8949 : : "defining type in %qs expression is invalid in C++",
8950 : : (in_sizeof ? "sizeof"
8951 : 4 : : in_typeof ? "typeof"
8952 : 1 : : in_alignof ? "alignof"
8953 : : : "_Countof"));
8954 : :
8955 : 1149636 : if (in_underspecified_init)
8956 : 18 : error_at (loc, "%qT defined in underspecified object initializer", ref);
8957 : :
8958 : 1149636 : return ref;
8959 : : }
8960 : :
8961 : : /* Process the specs, declarator and width (NULL if omitted)
8962 : : of a structure component, returning a FIELD_DECL node.
8963 : : WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
8964 : : DECL_ATTRS is as for grokdeclarator.
8965 : :
8966 : : LOC is the location of the structure component.
8967 : :
8968 : : This is done during the parsing of the struct declaration.
8969 : : The FIELD_DECL nodes are chained together and the lot of them
8970 : : are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
8971 : :
8972 : : tree
8973 : 4158313 : grokfield (location_t loc,
8974 : : struct c_declarator *declarator, struct c_declspecs *declspecs,
8975 : : tree width, tree *decl_attrs, tree *expr)
8976 : : {
8977 : 4158313 : tree value;
8978 : :
8979 : 4158313 : if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
8980 : 19656 : && width == NULL_TREE)
8981 : : {
8982 : : /* This is an unnamed decl.
8983 : :
8984 : : If we have something of the form "union { list } ;" then this
8985 : : is the anonymous union extension. Similarly for struct.
8986 : :
8987 : : If this is something of the form "struct foo;", then
8988 : : If MS or Plan 9 extensions are enabled, this is handled as
8989 : : an anonymous struct.
8990 : : Otherwise this is a forward declaration of a structure tag.
8991 : :
8992 : : If this is something of the form "foo;" and foo is a TYPE_DECL, then
8993 : : If foo names a structure or union without a tag, then this
8994 : : is an anonymous struct (this is permitted by C11).
8995 : : If MS or Plan 9 extensions are enabled and foo names a
8996 : : structure, then again this is an anonymous struct.
8997 : : Otherwise this is an error.
8998 : :
8999 : : Oh what a horrid tangled web we weave. I wonder if MS consciously
9000 : : took this from Plan 9 or if it was an accident of implementation
9001 : : that took root before someone noticed the bug... */
9002 : :
9003 : 10470 : tree type = declspecs->type;
9004 : 10470 : bool ok = false;
9005 : :
9006 : 10470 : if (RECORD_OR_UNION_TYPE_P (type)
9007 : 10464 : && (flag_ms_extensions
9008 : 10454 : || flag_plan9_extensions
9009 : 10439 : || !declspecs->typedef_p))
9010 : : {
9011 : 10457 : if (flag_ms_extensions || flag_plan9_extensions)
9012 : : ok = true;
9013 : 10432 : else if (TYPE_NAME (type) == NULL)
9014 : : ok = true;
9015 : : else
9016 : : ok = false;
9017 : : }
9018 : : if (!ok)
9019 : : {
9020 : 29 : pedwarn (loc, 0, "declaration does not declare anything");
9021 : 29 : return NULL_TREE;
9022 : : }
9023 : 10441 : if (flag_isoc99)
9024 : 10419 : pedwarn_c99 (loc, OPT_Wpedantic,
9025 : : "ISO C99 doesn%'t support unnamed structs/unions");
9026 : : else
9027 : 22 : pedwarn_c99 (loc, OPT_Wpedantic,
9028 : : "ISO C90 doesn%'t support unnamed structs/unions");
9029 : : }
9030 : :
9031 : 4158284 : value = grokdeclarator (declarator, declspecs, FIELD, false,
9032 : 4158284 : width ? &width : NULL, decl_attrs, expr, NULL,
9033 : : DEPRECATED_NORMAL);
9034 : :
9035 : 4158284 : finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
9036 : 4158284 : DECL_INITIAL (value) = width;
9037 : 4158284 : if (width)
9038 : 51791 : SET_DECL_C_BIT_FIELD (value);
9039 : :
9040 : 4158284 : if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
9041 : : {
9042 : : /* If we currently have a binding for this field, set the
9043 : : in_struct field in the binding, so that we warn about lookups
9044 : : which find it. */
9045 : 1297 : struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
9046 : 1297 : if (b != NULL)
9047 : : {
9048 : : /* If the in_struct field is not yet set, push it on a list
9049 : : to be cleared when this struct is finished. */
9050 : 93 : if (!b->in_struct)
9051 : : {
9052 : 91 : struct_parse_info->fields.safe_push (b);
9053 : 91 : b->in_struct = 1;
9054 : : }
9055 : : }
9056 : : }
9057 : :
9058 : : return value;
9059 : : }
9060 : :
9061 : : /* Subroutine of detect_field_duplicates: return whether X and Y,
9062 : : which are both fields in the same struct, have duplicate field
9063 : : names. */
9064 : :
9065 : : static bool
9066 : 3971451 : is_duplicate_field (tree x, tree y)
9067 : : {
9068 : 3971451 : if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
9069 : : return true;
9070 : :
9071 : : /* When using -fplan9-extensions, an anonymous field whose name is a
9072 : : typedef can duplicate a field name. */
9073 : 3971450 : if (flag_plan9_extensions
9074 : 3971450 : && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
9075 : : {
9076 : 0 : tree xt, xn, yt, yn;
9077 : :
9078 : 0 : xt = TREE_TYPE (x);
9079 : 0 : if (DECL_NAME (x) != NULL_TREE)
9080 : 0 : xn = DECL_NAME (x);
9081 : 0 : else if (RECORD_OR_UNION_TYPE_P (xt)
9082 : 0 : && TYPE_NAME (xt) != NULL_TREE
9083 : 0 : && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
9084 : 0 : xn = DECL_NAME (TYPE_NAME (xt));
9085 : : else
9086 : : xn = NULL_TREE;
9087 : :
9088 : 0 : yt = TREE_TYPE (y);
9089 : 0 : if (DECL_NAME (y) != NULL_TREE)
9090 : 0 : yn = DECL_NAME (y);
9091 : 0 : else if (RECORD_OR_UNION_TYPE_P (yt)
9092 : 0 : && TYPE_NAME (yt) != NULL_TREE
9093 : 0 : && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
9094 : 0 : yn = DECL_NAME (TYPE_NAME (yt));
9095 : : else
9096 : : yn = NULL_TREE;
9097 : :
9098 : 0 : if (xn != NULL_TREE && xn == yn)
9099 : 0 : return true;
9100 : : }
9101 : :
9102 : : return false;
9103 : : }
9104 : :
9105 : : /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
9106 : : to HTAB, giving errors for any duplicates. */
9107 : :
9108 : : static void
9109 : 78527 : detect_field_duplicates_hash (tree fieldlist,
9110 : : hash_table<nofree_ptr_hash <tree_node> > *htab)
9111 : : {
9112 : 78527 : tree x, y;
9113 : 78527 : tree_node **slot;
9114 : :
9115 : 1359208 : for (x = fieldlist; x ; x = DECL_CHAIN (x))
9116 : 1280681 : if ((y = DECL_NAME (x)) != NULL_TREE)
9117 : : {
9118 : 1261075 : slot = htab->find_slot (y, INSERT);
9119 : 1261075 : if (*slot)
9120 : : {
9121 : 15 : error ("duplicate member %q+D", x);
9122 : 15 : DECL_NAME (x) = NULL_TREE;
9123 : : }
9124 : 1261075 : *slot = y;
9125 : : }
9126 : 19606 : else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9127 : : {
9128 : 11132 : detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
9129 : :
9130 : : /* When using -fplan9-extensions, an anonymous field whose
9131 : : name is a typedef can duplicate a field name. */
9132 : 11132 : if (flag_plan9_extensions
9133 : 15 : && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9134 : 11146 : && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
9135 : : {
9136 : 2 : tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
9137 : 2 : slot = htab->find_slot (xn, INSERT);
9138 : 2 : if (*slot)
9139 : 0 : error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
9140 : 2 : *slot = xn;
9141 : : }
9142 : : }
9143 : 78527 : }
9144 : :
9145 : : /* Generate an error for any duplicate field names in FIELDLIST. Munge
9146 : : the list such that this does not present a problem later. */
9147 : :
9148 : : static void
9149 : 1149730 : detect_field_duplicates (tree fieldlist)
9150 : : {
9151 : 1149730 : tree x, y;
9152 : 1149730 : int timeout = 10;
9153 : :
9154 : : /* If the struct is the list of instance variables of an Objective-C
9155 : : class, then we need to check all the instance variables of
9156 : : superclasses when checking for duplicates (since you can't have
9157 : : an instance variable in a subclass with the same name as an
9158 : : instance variable in a superclass). We pass on this job to the
9159 : : Objective-C compiler. objc_detect_field_duplicates() will return
9160 : : false if we are not checking the list of instance variables and
9161 : : the C frontend should proceed with the standard field duplicate
9162 : : checks. If we are checking the list of instance variables, the
9163 : : ObjC frontend will do the check, emit the errors if needed, and
9164 : : then return true. */
9165 : 1149730 : if (c_dialect_objc ())
9166 : 0 : if (objc_detect_field_duplicates (false))
9167 : : return;
9168 : :
9169 : : /* First, see if there are more than "a few" fields.
9170 : : This is trivially true if there are zero or one fields. */
9171 : 1149730 : if (!fieldlist || !DECL_CHAIN (fieldlist))
9172 : : return;
9173 : : x = fieldlist;
9174 : 3351699 : do {
9175 : 3351699 : timeout--;
9176 : 3351699 : if (DECL_NAME (x) == NULL_TREE
9177 : 3351699 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9178 : : timeout = 0;
9179 : 3351699 : x = DECL_CHAIN (x);
9180 : 3351699 : } while (timeout > 0 && x);
9181 : :
9182 : : /* If there were "few" fields and no anonymous structures or unions,
9183 : : avoid the overhead of allocating a hash table. Instead just do
9184 : : the nested traversal thing. */
9185 : 947659 : if (timeout > 0)
9186 : : {
9187 : 2713101 : for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
9188 : : /* When using -fplan9-extensions, we can have duplicates
9189 : : between typedef names and fields. */
9190 : 1832837 : if (DECL_NAME (x)
9191 : 1832837 : || (flag_plan9_extensions
9192 : 0 : && DECL_NAME (x) == NULL_TREE
9193 : 0 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9194 : 0 : && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9195 : 0 : && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
9196 : : {
9197 : 5803753 : for (y = fieldlist; y != x; y = TREE_CHAIN (y))
9198 : 3971451 : if (is_duplicate_field (y, x))
9199 : : {
9200 : 1 : error ("duplicate member %q+D", x);
9201 : 1 : DECL_NAME (x) = NULL_TREE;
9202 : : }
9203 : : }
9204 : : }
9205 : : else
9206 : : {
9207 : 67395 : hash_table<nofree_ptr_hash <tree_node> > htab (37);
9208 : 67395 : detect_field_duplicates_hash (fieldlist, &htab);
9209 : 67395 : }
9210 : : }
9211 : :
9212 : : /* Finish up struct info used by -Wc++-compat. */
9213 : :
9214 : : static void
9215 : 575 : warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
9216 : : location_t record_loc)
9217 : : {
9218 : 575 : unsigned int ix;
9219 : 575 : tree x;
9220 : 575 : struct c_binding *b;
9221 : :
9222 : 575 : if (fieldlist == NULL_TREE)
9223 : : {
9224 : 9 : if (code == RECORD_TYPE)
9225 : 6 : warning_at (record_loc, OPT_Wc___compat,
9226 : : "empty struct has size 0 in C, size 1 in C++");
9227 : : else
9228 : 3 : warning_at (record_loc, OPT_Wc___compat,
9229 : : "empty union has size 0 in C, size 1 in C++");
9230 : : }
9231 : :
9232 : : /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
9233 : : the current struct. We do this now at the end of the struct
9234 : : because the flag is used to issue visibility warnings, and we
9235 : : only want to issue those warnings if the type is referenced
9236 : : outside of the struct declaration. */
9237 : 623 : FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
9238 : 48 : C_TYPE_DEFINED_IN_STRUCT (x) = 1;
9239 : :
9240 : : /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
9241 : : typedefs used when declaring fields in this struct. If the name
9242 : : of any of the fields is also a typedef name then the struct would
9243 : : not parse in C++, because the C++ lookup rules say that the
9244 : : typedef name would be looked up in the context of the struct, and
9245 : : would thus be the field rather than the typedef. */
9246 : 575 : if (!struct_parse_info->typedefs_seen.is_empty ()
9247 : 67 : && fieldlist != NULL_TREE)
9248 : : {
9249 : : /* Use a hash_set<tree> using the name of the typedef. We can use
9250 : : a hash_set<tree> because identifiers are interned. */
9251 : 67 : hash_set<tree> tset;
9252 : :
9253 : 249 : FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
9254 : 115 : tset.add (DECL_NAME (x));
9255 : :
9256 : 373 : for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
9257 : : {
9258 : 306 : if (DECL_NAME (x) != NULL_TREE
9259 : 306 : && tset.contains (DECL_NAME (x)))
9260 : : {
9261 : 2 : warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
9262 : : ("using %qD as both field and typedef name is "
9263 : : "invalid in C++"),
9264 : : x);
9265 : : /* FIXME: It would be nice to report the location where
9266 : : the typedef name is used. */
9267 : : }
9268 : : }
9269 : 67 : }
9270 : :
9271 : : /* For each field which has a binding and which was not defined in
9272 : : an enclosing struct, clear the in_struct field. */
9273 : 666 : FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
9274 : 91 : b->in_struct = 0;
9275 : 575 : }
9276 : :
9277 : : /* Function to help qsort sort FIELD_DECLs by name order. */
9278 : :
9279 : : static int
9280 : 19013701 : field_decl_cmp (const void *x_p, const void *y_p)
9281 : : {
9282 : 19013701 : const tree *const x = (const tree *) x_p;
9283 : 19013701 : const tree *const y = (const tree *) y_p;
9284 : :
9285 : 19013701 : if (DECL_NAME (*x) == DECL_NAME (*y))
9286 : : /* A nontype is "greater" than a type. */
9287 : 0 : return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9288 : 19013701 : if (DECL_NAME (*x) == NULL_TREE)
9289 : : return -1;
9290 : 19013701 : if (DECL_NAME (*y) == NULL_TREE)
9291 : : return 1;
9292 : 19013701 : if (DECL_NAME (*x) < DECL_NAME (*y))
9293 : 9368794 : return -1;
9294 : : return 1;
9295 : : }
9296 : :
9297 : : /* If this structure or union completes the type of any previous
9298 : : variable declaration, lay it out and output its rtl. */
9299 : : static void
9300 : 1323908 : finish_incomplete_vars (tree incomplete_vars, bool toplevel)
9301 : : {
9302 : 1324031 : for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
9303 : : {
9304 : 123 : tree decl = TREE_VALUE (x);
9305 : 123 : if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9306 : 0 : layout_array_type (TREE_TYPE (decl));
9307 : 123 : if (TREE_CODE (decl) != TYPE_DECL)
9308 : : {
9309 : 123 : relayout_decl (decl);
9310 : 123 : if (c_dialect_objc ())
9311 : 0 : objc_check_decl (decl);
9312 : 123 : rest_of_decl_compilation (decl, toplevel, 0);
9313 : : }
9314 : : }
9315 : 1323908 : }
9316 : :
9317 : : /* Determine whether the FIELD_DECL X is a flexible array member according to
9318 : : the following info:
9319 : : A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT;
9320 : : B. whether the FIELD_DECL is an array that is declared as "[]", "[0]",
9321 : : or "[1]";
9322 : : C. flag_strict_flex_arrays;
9323 : : D. the attribute strict_flex_array that is attached to the field
9324 : : if presenting.
9325 : : Return TRUE when it's a flexible array member, FALSE otherwise. */
9326 : :
9327 : : static bool
9328 : 4158366 : is_flexible_array_member_p (bool is_last_field,
9329 : : tree x)
9330 : : {
9331 : : /* If not the last field, return false. */
9332 : 4158366 : if (!is_last_field)
9333 : : return false;
9334 : :
9335 : : /* If not an array field, return false. */
9336 : 1610285 : if (TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
9337 : : return false;
9338 : :
9339 : 537289 : bool is_zero_length_array = zero_length_array_type_p (TREE_TYPE (x));
9340 : 537289 : bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x));
9341 : 537289 : bool is_flexible_array = c_flexible_array_member_type_p (TREE_TYPE (x));
9342 : :
9343 : 537289 : unsigned int strict_flex_array_level = c_strict_flex_array_level_of (x);
9344 : :
9345 : 537289 : switch (strict_flex_array_level)
9346 : : {
9347 : : case 0:
9348 : : /* Default, all trailing arrays are flexible array members. */
9349 : : return true;
9350 : 77 : case 1:
9351 : : /* Level 1: all "[1]", "[0]", and "[]" are flexible array members. */
9352 : 77 : if (is_one_element_array)
9353 : : return true;
9354 : : /* FALLTHROUGH. */
9355 : 141 : case 2:
9356 : : /* Level 2: all "[0]", and "[]" are flexible array members. */
9357 : 141 : if (is_zero_length_array)
9358 : : return true;
9359 : : /* FALLTHROUGH. */
9360 : 201 : case 3:
9361 : : /* Level 3: Only "[]" are flexible array members. */
9362 : 201 : if (is_flexible_array)
9363 : : return true;
9364 : : break;
9365 : 0 : default:
9366 : 0 : gcc_unreachable ();
9367 : : }
9368 : : return false;
9369 : : }
9370 : :
9371 : : /* Recompute TYPE_CANONICAL for variants of the type including qualified
9372 : : versions of the type and related pointer types after an aggregate type
9373 : : has been finalized.
9374 : : Will not update array types, pointers to array types, function
9375 : : types and other derived types created while the type was still
9376 : : incomplete, those will remain TYPE_STRUCTURAL_EQUALITY_P. */
9377 : :
9378 : : static void
9379 : 1159058 : c_update_type_canonical (tree t)
9380 : : {
9381 : 1159058 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t && !TYPE_QUALS (t));
9382 : 2344227 : for (tree x = t, l = NULL_TREE; x; l = x, x = TYPE_NEXT_VARIANT (x))
9383 : : {
9384 : 1185169 : if (x != t && TYPE_STRUCTURAL_EQUALITY_P (x))
9385 : : {
9386 : 26111 : if (!TYPE_QUALS (x))
9387 : 25489 : TYPE_CANONICAL (x) = TYPE_CANONICAL (t);
9388 : : else
9389 : : {
9390 : 622 : tree
9391 : 622 : c = c_build_qualified_type (TYPE_CANONICAL (t), TYPE_QUALS (x));
9392 : 622 : if (TYPE_STRUCTURAL_EQUALITY_P (c))
9393 : : {
9394 : 610 : gcc_checking_assert (TYPE_CANONICAL (t) == t);
9395 : 610 : if (c == x)
9396 : 610 : TYPE_CANONICAL (x) = x;
9397 : : else
9398 : : {
9399 : : /* build_qualified_type for this function unhelpfully
9400 : : moved c from some later spot in TYPE_MAIN_VARIANT (t)
9401 : : chain to right after t (or created it there). Move
9402 : : it right before x and process c and then x. */
9403 : 0 : gcc_checking_assert (TYPE_NEXT_VARIANT (t) == c);
9404 : 0 : if (l != t)
9405 : : {
9406 : 0 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (c);
9407 : 0 : TYPE_NEXT_VARIANT (l) = c;
9408 : 0 : TYPE_NEXT_VARIANT (c) = x;
9409 : : }
9410 : 0 : TYPE_CANONICAL (c) = c;
9411 : 0 : x = c;
9412 : : }
9413 : : }
9414 : : else
9415 : 12 : TYPE_CANONICAL (x) = TYPE_CANONICAL (c);
9416 : : }
9417 : : }
9418 : 1159058 : else if (x != t)
9419 : 0 : continue;
9420 : 1252406 : for (tree p = TYPE_POINTER_TO (x); p; p = TYPE_NEXT_PTR_TO (p))
9421 : : {
9422 : 67237 : if (!TYPE_STRUCTURAL_EQUALITY_P (p))
9423 : 0 : continue;
9424 : 67237 : if (TYPE_CANONICAL (x) != x || TYPE_REF_CAN_ALIAS_ALL (p))
9425 : 2401 : TYPE_CANONICAL (p)
9426 : 4802 : = c_build_pointer_type_for_mode (TYPE_CANONICAL (x), TYPE_MODE (p),
9427 : : false);
9428 : : else
9429 : 64836 : TYPE_CANONICAL (p) = p;
9430 : 67237 : c_update_type_canonical (p);
9431 : : }
9432 : : }
9433 : 1159058 : }
9434 : :
9435 : : /* Verify the argument of the counted_by attribute of the flexible array
9436 : : member FIELD_DECL is a valid field of the containing structure,
9437 : : STRUCT_TYPE, Report error and remove this attribute when it's not. */
9438 : :
9439 : : static void
9440 : 70 : verify_counted_by_attribute (tree struct_type, tree field_decl)
9441 : : {
9442 : 70 : tree attr_counted_by = lookup_attribute ("counted_by",
9443 : 70 : DECL_ATTRIBUTES (field_decl));
9444 : :
9445 : 70 : if (!attr_counted_by)
9446 : : return;
9447 : :
9448 : : /* If there is an counted_by attribute attached to the field,
9449 : : verify it. */
9450 : :
9451 : 70 : tree fieldname = TREE_VALUE (TREE_VALUE (attr_counted_by));
9452 : :
9453 : : /* Verify the argument of the attrbute is a valid field of the
9454 : : containing structure. */
9455 : :
9456 : 70 : tree counted_by_field = lookup_field (struct_type, fieldname);
9457 : :
9458 : : /* Error when the field is not found in the containing structure and
9459 : : remove the corresponding counted_by attribute from the field_decl. */
9460 : 70 : if (!counted_by_field)
9461 : : {
9462 : 2 : error_at (DECL_SOURCE_LOCATION (field_decl),
9463 : : "argument %qE to the %<counted_by%> attribute"
9464 : : " is not a field declaration in the same structure"
9465 : : " as %qD", fieldname, field_decl);
9466 : 2 : DECL_ATTRIBUTES (field_decl)
9467 : 4 : = remove_attribute ("counted_by", DECL_ATTRIBUTES (field_decl));
9468 : : }
9469 : : else
9470 : : /* Error when the field is not with an integer type. */
9471 : : {
9472 : 86 : while (TREE_CHAIN (counted_by_field))
9473 : 18 : counted_by_field = TREE_CHAIN (counted_by_field);
9474 : 68 : tree real_field = TREE_VALUE (counted_by_field);
9475 : :
9476 : 68 : if (!INTEGRAL_TYPE_P (TREE_TYPE (real_field)))
9477 : : {
9478 : 2 : error_at (DECL_SOURCE_LOCATION (field_decl),
9479 : : "argument %qE to the %<counted_by%> attribute"
9480 : : " is not a field declaration with an integer type",
9481 : : fieldname);
9482 : 2 : DECL_ATTRIBUTES (field_decl)
9483 : 4 : = remove_attribute ("counted_by", DECL_ATTRIBUTES (field_decl));
9484 : : }
9485 : : }
9486 : : }
9487 : :
9488 : : /* TYPE is a struct or union that we're applying may_alias to after the body is
9489 : : parsed. Fixup any POINTER_TO types. */
9490 : :
9491 : : static void
9492 : 363 : c_fixup_may_alias (tree type)
9493 : : {
9494 : 428 : for (tree t = TYPE_POINTER_TO (type); t; t = TYPE_NEXT_PTR_TO (t))
9495 : 190 : for (tree v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
9496 : 125 : TYPE_REF_CAN_ALIAS_ALL (v) = true;
9497 : 363 : }
9498 : :
9499 : : /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
9500 : : LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
9501 : : FIELDLIST is a chain of FIELD_DECL nodes for the fields.
9502 : : ATTRIBUTES are attributes to be applied to the structure.
9503 : :
9504 : : ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
9505 : : the struct was started. */
9506 : :
9507 : : tree
9508 : 1149730 : finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
9509 : : class c_struct_parse_info *enclosing_struct_parse_info,
9510 : : tree *expr)
9511 : : {
9512 : 1149730 : tree x;
9513 : 1149730 : bool toplevel = file_scope == current_scope;
9514 : :
9515 : : /* If this type was previously laid out as a forward reference,
9516 : : make sure we lay it out again. */
9517 : :
9518 : 1149730 : TYPE_SIZE (t) = NULL_TREE;
9519 : :
9520 : 1149730 : decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9521 : :
9522 : 1149730 : if (pedantic)
9523 : : {
9524 : 7317 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9525 : : {
9526 : 7302 : if (DECL_NAME (x) != NULL_TREE)
9527 : : break;
9528 : 18 : if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9529 : : break;
9530 : : }
9531 : :
9532 : 7303 : if (x == NULL_TREE)
9533 : : {
9534 : 15 : if (TREE_CODE (t) == UNION_TYPE)
9535 : : {
9536 : 4 : if (fieldlist)
9537 : 2 : pedwarn (loc, OPT_Wpedantic, "union has no named members");
9538 : : else
9539 : 2 : pedwarn (loc, OPT_Wpedantic, "union has no members");
9540 : : }
9541 : : else
9542 : : {
9543 : 11 : if (fieldlist)
9544 : 2 : pedwarn (loc, OPT_Wpedantic, "struct has no named members");
9545 : : else
9546 : 9 : pedwarn (loc, OPT_Wpedantic, "struct has no members");
9547 : : }
9548 : : }
9549 : : }
9550 : :
9551 : : /* Install struct as DECL_CONTEXT of each field decl.
9552 : : Also process specified field sizes, found in the DECL_INITIAL,
9553 : : storing 0 there after the type has been changed to precision equal
9554 : : to its width, rather than the precision of the specified standard
9555 : : type. (Correct layout requires the original type to have been preserved
9556 : : until now.) */
9557 : :
9558 : : bool saw_named_field = false;
9559 : : tree counted_by_fam_field = NULL_TREE;
9560 : 5308134 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9561 : : {
9562 : : /* Whether this field is the last field of the structure or union.
9563 : : for UNION, any field is the last field of it. */
9564 : 4158404 : bool is_last_field = (DECL_CHAIN (x) == NULL_TREE)
9565 : 4158404 : || (TREE_CODE (t) == UNION_TYPE);
9566 : :
9567 : 4158404 : if (TREE_TYPE (x) == error_mark_node)
9568 : 38 : continue;
9569 : :
9570 : 4158366 : DECL_CONTEXT (x) = t;
9571 : :
9572 : 4158366 : tree t1 = strip_array_types (TREE_TYPE (x));
9573 : : /* If any field is const, the structure type is pseudo-const. */
9574 : 4158366 : if (TREE_READONLY (x))
9575 : 62968 : C_TYPE_FIELDS_READONLY (t) = 1;
9576 : : else
9577 : : {
9578 : : /* A field that is pseudo-const makes the structure likewise. */
9579 : 4095398 : if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
9580 : 641 : C_TYPE_FIELDS_READONLY (t) = 1;
9581 : : }
9582 : :
9583 : : /* Any field that is volatile means variables of this type must be
9584 : : treated in some ways as volatile. */
9585 : 4158366 : if (TREE_THIS_VOLATILE (x))
9586 : : {
9587 : 336 : C_TYPE_FIELDS_VOLATILE (t) = 1;
9588 : 336 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9589 : : }
9590 : :
9591 : : /* Any field that is volatile, restrict-qualified or atomic
9592 : : means the type cannot be used for a constexpr object. */
9593 : 4158366 : if (TYPE_QUALS (t1)
9594 : 4158366 : & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
9595 : 1227 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9596 : 4157139 : else if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_NON_CONSTEXPR (t1))
9597 : 31 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9598 : :
9599 : : /* Any field of nominal variable size implies structure is too. */
9600 : 4158366 : if (C_DECL_VARIABLE_SIZE (x))
9601 : 645 : C_TYPE_VARIABLE_SIZE (t) = 1;
9602 : :
9603 : : /* If any field is variably modified, record this fact. */
9604 : 4158366 : if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (x)))
9605 : 687 : C_TYPE_VARIABLY_MODIFIED (t) = 1;
9606 : :
9607 : 4158366 : if (DECL_C_BIT_FIELD (x))
9608 : : {
9609 : 51802 : unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
9610 : 51802 : DECL_SIZE (x) = bitsize_int (width);
9611 : 51802 : DECL_BIT_FIELD (x) = 1;
9612 : : }
9613 : :
9614 : 4158366 : if (TYPE_PACKED (t)
9615 : 4163919 : && (DECL_BIT_FIELD (x)
9616 : 3235 : || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
9617 : 4442 : DECL_PACKED (x) = 1;
9618 : :
9619 : : /* Detect flexible array member in an invalid context. */
9620 : 4158366 : if (c_flexible_array_member_type_p (TREE_TYPE (x)))
9621 : : {
9622 : 84570 : if (TREE_CODE (t) == UNION_TYPE)
9623 : 28 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9624 : : "flexible array member in union is a GCC extension");
9625 : 84542 : else if (!is_last_field)
9626 : : {
9627 : 3 : error_at (DECL_SOURCE_LOCATION (x),
9628 : : "flexible array member not at end of struct");
9629 : 3 : TREE_TYPE (x) = error_mark_node;
9630 : : }
9631 : 84539 : else if (!saw_named_field)
9632 : 18 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9633 : : "flexible array member in a struct with no named "
9634 : : "members is a GCC extension");
9635 : :
9636 : : /* If there is a counted_by attribute attached to this field,
9637 : : record it here and do more verification later after the
9638 : : whole structure is complete. */
9639 : 84570 : if (lookup_attribute ("counted_by", DECL_ATTRIBUTES (x)))
9640 : 4158366 : counted_by_fam_field = x;
9641 : : }
9642 : :
9643 : 24640 : if (pedantic && TREE_CODE (t) == RECORD_TYPE
9644 : 4178761 : && flexible_array_type_p (TREE_TYPE (x)))
9645 : 18 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9646 : : "invalid use of structure with flexible array member");
9647 : :
9648 : : /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */
9649 : 4158366 : DECL_NOT_FLEXARRAY (x) = !is_flexible_array_member_p (is_last_field, x);
9650 : :
9651 : : /* Set TYPE_INCLUDES_FLEXARRAY for the context of x, t.
9652 : : when x is an array and is the last field.
9653 : : There is only one last_field for a structure type, but there might
9654 : : be multiple last_fields for a union type, therefore we should ORed
9655 : : the result for multiple last_fields. */
9656 : 4158366 : if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
9657 : 686332 : TYPE_INCLUDES_FLEXARRAY (t)
9658 : 1372664 : |= is_last_field && c_flexible_array_member_type_p (TREE_TYPE (x));
9659 : : /* Recursively set TYPE_INCLUDES_FLEXARRAY for the context of x, t
9660 : : when x is an union or record and is the last field. */
9661 : 3472034 : else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9662 : 454546 : TYPE_INCLUDES_FLEXARRAY (t)
9663 : 909092 : |= is_last_field && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x));
9664 : :
9665 : 4158366 : if (warn_flex_array_member_not_at_end
9666 : 56 : && !is_last_field
9667 : 22 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9668 : 4158378 : && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x)))
9669 : 5 : warning_at (DECL_SOURCE_LOCATION (x),
9670 : 5 : OPT_Wflex_array_member_not_at_end,
9671 : : "structure containing a flexible array member"
9672 : : " is not at the end of another structure");
9673 : :
9674 : 4158366 : if (DECL_NAME (x)
9675 : 4158366 : || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9676 : : saw_named_field = true;
9677 : :
9678 : 7630400 : if (AGGREGATE_TYPE_P (TREE_TYPE (x))
9679 : 4612912 : && TYPE_TYPELESS_STORAGE (TREE_TYPE (x)))
9680 : 436698 : TYPE_TYPELESS_STORAGE (t) = true;
9681 : : }
9682 : :
9683 : 1149730 : detect_field_duplicates (fieldlist);
9684 : :
9685 : : /* Now we have the nearly final fieldlist. Record it,
9686 : : then lay out the structure or union (including the fields). */
9687 : :
9688 : 1149730 : TYPE_FIELDS (t) = fieldlist;
9689 : :
9690 : 1149730 : maybe_apply_pragma_scalar_storage_order (t);
9691 : :
9692 : 1149730 : layout_type (t);
9693 : :
9694 : 1149730 : if (TYPE_SIZE_UNIT (t)
9695 : 1149730 : && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
9696 : 1149138 : && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
9697 : 2298868 : && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
9698 : 1 : error ("type %qT is too large", t);
9699 : :
9700 : : /* Give bit-fields their proper types and rewrite the type of array fields
9701 : : with scalar component if the enclosing type has reverse storage order. */
9702 : 5308134 : for (tree field = fieldlist; field; field = DECL_CHAIN (field))
9703 : : {
9704 : 4158404 : if (TREE_CODE (field) == FIELD_DECL
9705 : 4158404 : && DECL_INITIAL (field)
9706 : 4210214 : && TREE_TYPE (field) != error_mark_node)
9707 : : {
9708 : 51802 : unsigned HOST_WIDE_INT width
9709 : 51802 : = tree_to_uhwi (DECL_INITIAL (field));
9710 : 51802 : tree type = TREE_TYPE (field);
9711 : 51802 : if (width != TYPE_PRECISION (type))
9712 : : {
9713 : 37652 : if (TREE_CODE (type) == BITINT_TYPE
9714 : 37721 : && width >= (TYPE_UNSIGNED (type) ? 1 : 2))
9715 : 160 : TREE_TYPE (field)
9716 : 320 : = build_bitint_type (width, TYPE_UNSIGNED (type));
9717 : : else
9718 : 37492 : TREE_TYPE (field)
9719 : 37492 : = c_build_bitfield_integer_type (width,
9720 : 37492 : TYPE_UNSIGNED (type));
9721 : 37652 : if (tree attr = c_hardbool_type_attr (type))
9722 : 253 : decl_attributes (&TREE_TYPE (field),
9723 : : copy_list (attr),
9724 : : 0, NULL_TREE);
9725 : 37652 : SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
9726 : : }
9727 : 51802 : DECL_INITIAL (field) = NULL_TREE;
9728 : : }
9729 : 4106602 : else if (TYPE_REVERSE_STORAGE_ORDER (t)
9730 : 693 : && TREE_CODE (field) == FIELD_DECL
9731 : 4107295 : && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
9732 : : {
9733 : 106 : tree ftype = TREE_TYPE (field);
9734 : 106 : tree ctype = strip_array_types (ftype);
9735 : 106 : if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
9736 : : {
9737 : 93 : tree fmain_type = TYPE_MAIN_VARIANT (ftype);
9738 : 93 : tree *typep = &fmain_type;
9739 : 95 : do {
9740 : 95 : *typep = build_distinct_type_copy (*typep);
9741 : 95 : TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
9742 : 95 : typep = &TREE_TYPE (*typep);
9743 : 95 : } while (TREE_CODE (*typep) == ARRAY_TYPE);
9744 : 93 : TREE_TYPE (field)
9745 : 186 : = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
9746 : : }
9747 : : }
9748 : :
9749 : : /* Warn on problematic type punning for storage order purposes. */
9750 : 4158404 : if (TREE_CODE (t) == UNION_TYPE
9751 : 829014 : && TREE_CODE (field) == FIELD_DECL
9752 : 4987418 : && AGGREGATE_TYPE_P (TREE_TYPE (field)))
9753 : : {
9754 : 411556 : tree ftype = TREE_TYPE (field);
9755 : 411556 : if (TREE_CODE (ftype) == ARRAY_TYPE)
9756 : 303720 : ftype = strip_array_types (ftype);
9757 : 411556 : if (RECORD_OR_UNION_TYPE_P (ftype)
9758 : 411556 : && TYPE_REVERSE_STORAGE_ORDER (ftype)
9759 : 108330 : != TYPE_REVERSE_STORAGE_ORDER (t))
9760 : 1 : warning_at (DECL_SOURCE_LOCATION (field),
9761 : 1 : OPT_Wscalar_storage_order,
9762 : : "type punning toggles scalar storage order");
9763 : : }
9764 : : }
9765 : :
9766 : : /* Now we have the truly final field list.
9767 : : Store it in this type and in the variants. */
9768 : :
9769 : 1149730 : TYPE_FIELDS (t) = fieldlist;
9770 : :
9771 : : /* If there are lots of fields, sort so we can look through them fast.
9772 : : We arbitrarily consider 16 or more elts to be "a lot". */
9773 : :
9774 : 1149730 : {
9775 : 1149730 : int len = 0;
9776 : :
9777 : 4900135 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9778 : : {
9779 : 3777620 : if (len > 15 || DECL_NAME (x) == NULL)
9780 : : break;
9781 : 3750405 : len += 1;
9782 : : }
9783 : :
9784 : 1149730 : if (len > 15)
9785 : : {
9786 : 22207 : tree *field_array;
9787 : 22207 : struct lang_type *space;
9788 : 22207 : struct sorted_fields_type *space2;
9789 : :
9790 : 22207 : len += list_length (x);
9791 : :
9792 : : /* Use the same allocation policy here that make_node uses, to
9793 : : ensure that this lives as long as the rest of the struct decl.
9794 : : All decls in an inline function need to be saved. */
9795 : :
9796 : 22207 : space = ((struct lang_type *)
9797 : 22207 : ggc_internal_cleared_alloc (c_dialect_objc ()
9798 : : ? sizeof (struct lang_type)
9799 : : : offsetof (struct lang_type,
9800 : : info)));
9801 : 22207 : space2 = ((sorted_fields_type *)
9802 : 44414 : ggc_internal_alloc (sizeof (struct sorted_fields_type)
9803 : 22207 : + len * sizeof (tree)));
9804 : :
9805 : 22207 : len = 0;
9806 : 22207 : space->s = space2;
9807 : 22207 : field_array = &space2->elts[0];
9808 : 752215 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9809 : : {
9810 : 733111 : field_array[len++] = x;
9811 : :
9812 : : /* If there is anonymous struct or union, break out of the loop. */
9813 : 733111 : if (DECL_NAME (x) == NULL)
9814 : : break;
9815 : : }
9816 : : /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
9817 : 22207 : if (x == NULL)
9818 : : {
9819 : 19104 : TYPE_LANG_SPECIFIC (t) = space;
9820 : 19104 : TYPE_LANG_SPECIFIC (t)->s->len = len;
9821 : 19104 : field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
9822 : 19104 : qsort (field_array, len, sizeof (tree), field_decl_cmp);
9823 : : }
9824 : : }
9825 : : }
9826 : :
9827 : : /* If this was supposed to be a transparent union, but we can't
9828 : : make it one, warn and turn off the flag. */
9829 : 1149730 : if (TREE_CODE (t) == UNION_TYPE
9830 : 366243 : && TYPE_TRANSPARENT_AGGR (t)
9831 : 1149773 : && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
9832 : : {
9833 : 13 : TYPE_TRANSPARENT_AGGR (t) = 0;
9834 : 13 : warning_at (loc, 0, "union cannot be made transparent");
9835 : : }
9836 : :
9837 : : /* Check for consistency with previous definition. */
9838 : 1149730 : if (flag_isoc23 && NULL != enclosing_struct_parse_info)
9839 : : {
9840 : 883103 : tree vistype = previous_tag (t);
9841 : 883103 : if (vistype
9842 : 119 : && TREE_CODE (vistype) == TREE_CODE (t)
9843 : 883222 : && !C_TYPE_BEING_DEFINED (vistype))
9844 : : {
9845 : 99 : TYPE_STUB_DECL (t) = TYPE_STUB_DECL (vistype);
9846 : 99 : if (c_type_variably_modified_p (t))
9847 : : {
9848 : 7 : error ("redefinition of struct or union %qT with variably "
9849 : : "modified type", t);
9850 : 7 : if (struct_parse_info->refloc != UNKNOWN_LOCATION)
9851 : 7 : inform (struct_parse_info->refloc, "originally defined here");
9852 : : }
9853 : 92 : else if (!comptypes_same_p (t, vistype))
9854 : : {
9855 : 35 : error ("redefinition of struct or union %qT", t);
9856 : 35 : if (struct_parse_info->refloc != UNKNOWN_LOCATION)
9857 : 35 : inform (struct_parse_info->refloc, "originally defined here");
9858 : : }
9859 : : }
9860 : : }
9861 : :
9862 : 1149730 : C_TYPE_BEING_DEFINED (t) = 0;
9863 : :
9864 : 1149730 : if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (t)))
9865 : 694 : for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9866 : 363 : c_fixup_may_alias (x);
9867 : :
9868 : : /* Set type canonical based on equivalence class. */
9869 : 1149730 : if (flag_isoc23 && !C_TYPE_VARIABLE_SIZE (t))
9870 : : {
9871 : 917747 : if (c_struct_htab == NULL)
9872 : 34870 : c_struct_htab = hash_table<c_struct_hasher>::create_ggc (61);
9873 : :
9874 : 917747 : hashval_t hash = c_struct_hasher::hash (t);
9875 : :
9876 : 917747 : gcc_checking_assert (TYPE_STRUCTURAL_EQUALITY_P (t));
9877 : 917747 : gcc_checking_assert (!TYPE_NAME (t)
9878 : : || TREE_CODE (TYPE_NAME (t)) == IDENTIFIER_NODE);
9879 : :
9880 : 917747 : tree *e = c_struct_htab->find_slot_with_hash (t, hash, INSERT);
9881 : 917747 : if (*e)
9882 : 60886 : TYPE_CANONICAL (t) = *e;
9883 : : else
9884 : : {
9885 : 856861 : TYPE_CANONICAL (t) = t;
9886 : 856861 : *e = t;
9887 : : }
9888 : 917747 : c_update_type_canonical (t);
9889 : : }
9890 : :
9891 : 1149730 : tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
9892 : 2329690 : for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9893 : : {
9894 : 1179960 : TYPE_FIELDS (x) = TYPE_FIELDS (t);
9895 : 1179960 : TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
9896 : 1179960 : TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
9897 : 1179960 : TYPE_TYPELESS_STORAGE (x) = TYPE_TYPELESS_STORAGE (t);
9898 : 1179960 : C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
9899 : 1179960 : C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
9900 : 1179960 : C_TYPE_FIELDS_NON_CONSTEXPR (x) = C_TYPE_FIELDS_NON_CONSTEXPR (t);
9901 : 1179960 : C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
9902 : 1179960 : C_TYPE_VARIABLY_MODIFIED (x) = C_TYPE_VARIABLY_MODIFIED (t);
9903 : 1179960 : C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
9904 : 1179960 : TYPE_INCLUDES_FLEXARRAY (x) = TYPE_INCLUDES_FLEXARRAY (t);
9905 : : }
9906 : :
9907 : : /* Update type location to the one of the definition, instead of e.g.
9908 : : a forward declaration. */
9909 : 1149730 : if (TYPE_STUB_DECL (t))
9910 : 1149730 : DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
9911 : :
9912 : : /* Finish debugging output for this type. */
9913 : 1149730 : rest_of_type_compilation (t, toplevel);
9914 : :
9915 : 1149730 : finish_incomplete_vars (incomplete_vars, toplevel);
9916 : :
9917 : : /* Make sure a DECL_EXPR is created for structs with VLA members.
9918 : : Because we do not know the context, we always pass expr
9919 : : to force creation of a BIND_EXPR which is required in some
9920 : : contexts. */
9921 : 1149730 : if (c_type_variably_modified_p (t))
9922 : 647 : add_decl_expr (loc, t, expr, false);
9923 : :
9924 : 1149730 : if (warn_cxx_compat)
9925 : 575 : warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
9926 : :
9927 : 1149730 : if (NULL != enclosing_struct_parse_info)
9928 : : {
9929 : 1109457 : delete struct_parse_info;
9930 : :
9931 : 1109457 : struct_parse_info = enclosing_struct_parse_info;
9932 : :
9933 : : /* If this struct is defined inside a struct, add it to
9934 : : struct_types. */
9935 : 1109457 : if (warn_cxx_compat
9936 : : && struct_parse_info != NULL
9937 : 350 : && !in_sizeof && !in_typeof && !in_alignof && !in_countof)
9938 : 345 : struct_parse_info->struct_types.safe_push (t);
9939 : : }
9940 : :
9941 : 1149730 : if (counted_by_fam_field)
9942 : 70 : verify_counted_by_attribute (t, counted_by_fam_field);
9943 : :
9944 : 1149730 : return t;
9945 : : }
9946 : :
9947 : : static struct {
9948 : : gt_pointer_operator new_value;
9949 : : void *cookie;
9950 : : } resort_data;
9951 : :
9952 : : /* This routine compares two fields like field_decl_cmp but using the
9953 : : pointer operator in resort_data. */
9954 : :
9955 : : static int
9956 : 4990 : resort_field_decl_cmp (const void *x_p, const void *y_p)
9957 : : {
9958 : 4990 : const tree *const x = (const tree *) x_p;
9959 : 4990 : const tree *const y = (const tree *) y_p;
9960 : :
9961 : 4990 : if (DECL_NAME (*x) == DECL_NAME (*y))
9962 : : /* A nontype is "greater" than a type. */
9963 : 0 : return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9964 : 4990 : if (DECL_NAME (*x) == NULL_TREE)
9965 : : return -1;
9966 : 4990 : if (DECL_NAME (*y) == NULL_TREE)
9967 : : return 1;
9968 : 4990 : {
9969 : 4990 : tree d1 = DECL_NAME (*x);
9970 : 4990 : tree d2 = DECL_NAME (*y);
9971 : 4990 : resort_data.new_value (&d1, &d1, resort_data.cookie);
9972 : 4990 : resort_data.new_value (&d2, &d2, resort_data.cookie);
9973 : 4990 : if (d1 < d2)
9974 : 2556 : return -1;
9975 : : }
9976 : 2434 : return 1;
9977 : : }
9978 : :
9979 : : /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9980 : :
9981 : : void
9982 : 6 : resort_sorted_fields (void *obj,
9983 : : void * ARG_UNUSED (orig_obj),
9984 : : gt_pointer_operator new_value,
9985 : : void *cookie)
9986 : : {
9987 : 6 : struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9988 : 6 : resort_data.new_value = new_value;
9989 : 6 : resort_data.cookie = cookie;
9990 : 6 : qsort (&sf->elts[0], sf->len, sizeof (tree),
9991 : : resort_field_decl_cmp);
9992 : 6 : }
9993 : :
9994 : : /* Lay out the type T, and its element type, and so on. */
9995 : :
9996 : : static void
9997 : 0 : layout_array_type (tree t)
9998 : : {
9999 : 0 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
10000 : 0 : layout_array_type (TREE_TYPE (t));
10001 : 0 : layout_type (t);
10002 : 0 : }
10003 : :
10004 : : /* Begin compiling the definition of an enumeration type.
10005 : : NAME is its name (or null if anonymous).
10006 : : LOC is the enum's location.
10007 : : FIXED_UNDERLYING_TYPE is the (C23) underlying type specified in the
10008 : : definition.
10009 : : Returns the type object, as yet incomplete.
10010 : : Also records info about it so that build_enumerator
10011 : : may be used to declare the individual values as they are read. */
10012 : :
10013 : : tree
10014 : 174178 : start_enum (location_t loc, struct c_enum_contents *the_enum, tree name,
10015 : : tree fixed_underlying_type, bool potential_nesting_p)
10016 : : {
10017 : 174178 : tree enumtype = NULL_TREE;
10018 : 174178 : location_t enumloc = UNKNOWN_LOCATION;
10019 : :
10020 : : /* If this is the real definition for a previous forward reference,
10021 : : fill in the contents in the same object that used to be the
10022 : : forward reference. */
10023 : :
10024 : 174178 : if (name != NULL_TREE)
10025 : 69070 : enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
10026 : :
10027 : 69070 : if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
10028 : : {
10029 : : /* If the type is currently being defined or if we have seen an
10030 : : incomplete version which is now complete, this is a nested
10031 : : redefinition. The later happens if the redefinition occurs
10032 : : inside the enum specifier itself. */
10033 : 175 : if (C_TYPE_BEING_DEFINED (enumtype)
10034 : 175 : || (potential_nesting_p && TYPE_VALUES (enumtype) != NULL_TREE))
10035 : 9 : error_at (loc, "nested redefinition of %<enum %E%>", name);
10036 : :
10037 : : /* For C23 we allow redefinitions. We set to zero and check for
10038 : : consistency later. */
10039 : 333 : if (flag_isoc23 && TYPE_VALUES (enumtype) != NULL_TREE)
10040 : : enumtype = NULL_TREE;
10041 : : }
10042 : :
10043 : 105257 : if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
10044 : : {
10045 : 174031 : enumtype = make_node (ENUMERAL_TYPE);
10046 : 174031 : TYPE_SIZE (enumtype) = NULL_TREE;
10047 : 174031 : SET_TYPE_STRUCTURAL_EQUALITY (enumtype);
10048 : 174031 : pushtag (loc, name, enumtype);
10049 : 174031 : if (fixed_underlying_type != NULL_TREE)
10050 : : {
10051 : : /* For an enum definition with a fixed underlying type, the
10052 : : type is complete during the definition and the
10053 : : enumeration constants have that type. If there was a
10054 : : tag, the type was completed in c_parser_enum_specifier.
10055 : : If not, it must be completed here. */
10056 : 12 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = true;
10057 : 12 : TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (fixed_underlying_type);
10058 : 12 : TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (fixed_underlying_type);
10059 : 12 : TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (fixed_underlying_type);
10060 : 12 : SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (fixed_underlying_type));
10061 : 12 : TYPE_SIZE (enumtype) = NULL_TREE;
10062 : 12 : TYPE_PRECISION (enumtype) = TYPE_PRECISION (fixed_underlying_type);
10063 : 12 : ENUM_UNDERLYING_TYPE (enumtype) = fixed_underlying_type;
10064 : 12 : TYPE_CANONICAL (enumtype) = TYPE_CANONICAL (fixed_underlying_type);
10065 : 12 : c_update_type_canonical (enumtype);
10066 : 12 : layout_type (enumtype);
10067 : : }
10068 : : }
10069 : : /* Update type location to the one of the definition, instead of e.g.
10070 : : a forward declaration. */
10071 : 147 : else if (TYPE_STUB_DECL (enumtype))
10072 : : {
10073 : 147 : enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
10074 : 147 : DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
10075 : : }
10076 : :
10077 : 174178 : C_TYPE_BEING_DEFINED (enumtype) = 1;
10078 : :
10079 : 174178 : if (TYPE_VALUES (enumtype) != NULL_TREE)
10080 : : {
10081 : : /* This enum is a named one that has been declared already. */
10082 : 6 : auto_diagnostic_group d;
10083 : 6 : error_at (loc, "redeclaration of %<enum %E%>", name);
10084 : 6 : if (enumloc != UNKNOWN_LOCATION)
10085 : 6 : inform (enumloc, "originally defined here");
10086 : :
10087 : : /* Completely replace its old definition.
10088 : : The old enumerators remain defined, however. */
10089 : 6 : TYPE_VALUES (enumtype) = NULL_TREE;
10090 : 6 : }
10091 : :
10092 : 174178 : if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype)
10093 : 174178 : && fixed_underlying_type == NULL_TREE)
10094 : : {
10095 : 3 : error_at (loc, "%<enum%> declared with but defined without "
10096 : : "fixed underlying type");
10097 : 3 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = false;
10098 : : }
10099 : :
10100 : 174178 : the_enum->enum_next_value = integer_zero_node;
10101 : 174178 : the_enum->enum_type = enumtype;
10102 : 174178 : the_enum->enum_overflow = 0;
10103 : :
10104 : 174178 : if (flag_short_enums && !ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
10105 : 106 : for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
10106 : 53 : TYPE_PACKED (v) = 1;
10107 : :
10108 : : /* FIXME: This will issue a warning for a use of a type defined
10109 : : within sizeof in a statement expr. This is not terribly serious
10110 : : as C++ doesn't permit statement exprs within sizeof anyhow. */
10111 : 174178 : if (warn_cxx_compat
10112 : 126 : && (in_sizeof || in_typeof || in_alignof || in_countof))
10113 : 0 : warning_at (loc, OPT_Wc___compat,
10114 : : "defining type in %qs expression is invalid in C++",
10115 : : (in_sizeof ? "sizeof"
10116 : 0 : : in_typeof ? "typeof"
10117 : 0 : : in_alignof ? "alignof"
10118 : : : "_Countof"));
10119 : :
10120 : 174178 : if (in_underspecified_init)
10121 : 6 : error_at (loc, "%qT defined in underspecified object initializer",
10122 : : enumtype);
10123 : :
10124 : 174178 : return enumtype;
10125 : : }
10126 : :
10127 : : /* After processing and defining all the values of an enumeration type,
10128 : : install their decls in the enumeration type and finish it off.
10129 : : ENUMTYPE is the type object, VALUES a list of decl-value pairs,
10130 : : and ATTRIBUTES are the specified attributes.
10131 : : Returns ENUMTYPE. */
10132 : :
10133 : : tree
10134 : 174178 : finish_enum (tree enumtype, tree values, tree attributes)
10135 : : {
10136 : 174178 : tree pair, tem;
10137 : 174178 : tree minnode = NULL_TREE, maxnode = NULL_TREE;
10138 : 174178 : int precision;
10139 : 174178 : signop sign;
10140 : 174178 : bool toplevel = (file_scope == current_scope);
10141 : 174178 : struct lang_type *lt;
10142 : :
10143 : 174178 : decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10144 : :
10145 : : /* Calculate the maximum value of any enumerator in this type. */
10146 : :
10147 : 174178 : if (values == error_mark_node)
10148 : 26 : minnode = maxnode = integer_zero_node;
10149 : : else
10150 : : {
10151 : 174152 : minnode = maxnode = TREE_VALUE (values);
10152 : 5547070 : for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
10153 : : {
10154 : 5372918 : tree value = TREE_VALUE (pair);
10155 : 5372918 : if (tree_int_cst_lt (maxnode, value))
10156 : 5230783 : maxnode = value;
10157 : 5372918 : if (tree_int_cst_lt (value, minnode))
10158 : 61883 : minnode = value;
10159 : : }
10160 : : }
10161 : :
10162 : : /* Construct the final type of this enumeration. It is the same
10163 : : as one of the integral types - the narrowest one that fits, except
10164 : : that normally we only go as narrow as int - and signed iff any of
10165 : : the values are negative. */
10166 : 174178 : sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
10167 : 174178 : precision = MAX (tree_int_cst_min_precision (minnode, sign),
10168 : : tree_int_cst_min_precision (maxnode, sign));
10169 : :
10170 : 174178 : bool wider_than_int =
10171 : 174178 : (tree_int_cst_lt (minnode, TYPE_MIN_VALUE (integer_type_node))
10172 : 174178 : || tree_int_cst_lt (TYPE_MAX_VALUE (integer_type_node), maxnode));
10173 : :
10174 : :
10175 : 174178 : if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
10176 : : {
10177 : : /* If the precision of the type was specified with an attribute and it
10178 : : was too small, give an error. Otherwise, use it. */
10179 : 174062 : if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
10180 : : {
10181 : 23 : if (precision > TYPE_PRECISION (enumtype))
10182 : : {
10183 : 6 : TYPE_PRECISION (enumtype) = 0;
10184 : 6 : error ("specified mode too small for enumerated values");
10185 : : }
10186 : : else
10187 : 17 : precision = TYPE_PRECISION (enumtype);
10188 : : }
10189 : : else
10190 : 174039 : TYPE_PRECISION (enumtype) = 0;
10191 : :
10192 : 174062 : if (TYPE_PACKED (enumtype)
10193 : 173983 : || precision > TYPE_PRECISION (integer_type_node)
10194 : 345469 : || TYPE_PRECISION (enumtype))
10195 : : {
10196 : 2782 : tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
10197 : 2667 : if (tem == NULL)
10198 : : {
10199 : : /* This should only occur when both signed and unsigned
10200 : : values of maximum precision occur among the
10201 : : enumerators. */
10202 : 3 : pedwarn (input_location, 0,
10203 : : "enumeration values exceed range of largest integer");
10204 : 3 : tem = widest_integer_literal_type_node;
10205 : : }
10206 : 2664 : else if (precision > TYPE_PRECISION (intmax_type_node)
10207 : 4 : && !tree_int_cst_lt (minnode,
10208 : 4 : TYPE_MIN_VALUE (intmax_type_node))
10209 : 2667 : && !tree_int_cst_lt (TYPE_MAX_VALUE (uintmax_type_node),
10210 : : maxnode))
10211 : 2 : pedwarn (input_location, OPT_Wpedantic,
10212 : : "enumeration values exceed range of %qs",
10213 : : sign == UNSIGNED ? "uintmax_t" : "intmax_t");
10214 : : }
10215 : : else
10216 : 171395 : tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
10217 : :
10218 : 174062 : TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
10219 : 174062 : TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
10220 : 174062 : TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
10221 : 174062 : SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
10222 : 174062 : TYPE_SIZE (enumtype) = NULL_TREE;
10223 : 174062 : TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
10224 : 174062 : ENUM_UNDERLYING_TYPE (enumtype) =
10225 : 174062 : c_common_type_for_size (TYPE_PRECISION (tem), TYPE_UNSIGNED (tem));
10226 : :
10227 : 522186 : TYPE_CANONICAL (enumtype) =
10228 : 174062 : TYPE_CANONICAL (ENUM_UNDERLYING_TYPE (enumtype));
10229 : 174062 : c_update_type_canonical (enumtype);
10230 : :
10231 : 174062 : layout_type (enumtype);
10232 : : }
10233 : :
10234 : 174178 : if (values != error_mark_node)
10235 : : {
10236 : : /* Change the type of the enumerators to be the enum type. We
10237 : : need to do this irrespective of the size of the enum, for
10238 : : proper type checking. Replace the DECL_INITIALs of the
10239 : : enumerators, and the value slots of the list, with copies
10240 : : that have the enum type; they cannot be modified in place
10241 : : because they may be shared (e.g. integer_zero_node) Finally,
10242 : : change the purpose slots to point to the names of the decls. */
10243 : 5721222 : for (pair = values; pair; pair = TREE_CHAIN (pair))
10244 : : {
10245 : 5547070 : tree enu = TREE_PURPOSE (pair);
10246 : 5547070 : tree ini = DECL_INITIAL (enu);
10247 : :
10248 : 5547070 : TREE_TYPE (enu) = enumtype;
10249 : :
10250 : : /* Before C23, the ISO C Standard mandates enumerators to
10251 : : have type int, even though the underlying type of an enum
10252 : : type is unspecified. However, C23 allows enumerators of
10253 : : any integer type, and if an enumeration has any
10254 : : enumerators wider than int, all enumerators have the
10255 : : enumerated type after it is parsed. Any enumerators that
10256 : : fit in int are given type int in build_enumerator (which
10257 : : is the correct type while the enumeration is being
10258 : : parsed), so no conversions are needed here if all
10259 : : enumerators fit in int. If the enum has a fixed
10260 : : underlying type, the correct type was also given in
10261 : : build_enumerator. */
10262 : 5547070 : if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) && wider_than_int)
10263 : 28411 : ini = convert (enumtype, ini);
10264 : :
10265 : 5547070 : DECL_INITIAL (enu) = ini;
10266 : 5547070 : TREE_PURPOSE (pair) = DECL_NAME (enu);
10267 : : /* To match the C++ FE, store the CONST_DECL rather than just its
10268 : : value. */
10269 : 5547070 : TREE_VALUE (pair) = enu;
10270 : : }
10271 : :
10272 : 174152 : TYPE_VALUES (enumtype) = values;
10273 : : }
10274 : :
10275 : : /* Record the min/max values so that we can warn about bit-field
10276 : : enumerations that are too small for the values. */
10277 : 174178 : lt = ((struct lang_type *)
10278 : 174178 : ggc_internal_cleared_alloc (c_dialect_objc ()
10279 : : ? sizeof (struct lang_type)
10280 : : : offsetof (struct lang_type, info)));
10281 : 174178 : lt->enum_min = minnode;
10282 : 174178 : lt->enum_max = maxnode;
10283 : 174178 : TYPE_LANG_SPECIFIC (enumtype) = lt;
10284 : :
10285 : : /* Fix up all variant types of this enum type. */
10286 : 174178 : tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
10287 : 348366 : for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
10288 : : {
10289 : 174188 : C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
10290 : 174188 : if (tem == enumtype)
10291 : 174178 : continue;
10292 : 10 : TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
10293 : 10 : TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
10294 : 10 : TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
10295 : 10 : TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
10296 : 10 : TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
10297 : 10 : SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
10298 : 10 : TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
10299 : 10 : SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
10300 : 10 : TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
10301 : 10 : TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
10302 : 10 : TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
10303 : 10 : ENUM_UNDERLYING_TYPE (tem) = ENUM_UNDERLYING_TYPE (enumtype);
10304 : 10 : TYPE_PACKED (tem) = TYPE_PACKED (enumtype);
10305 : : }
10306 : :
10307 : : /* Finish debugging output for this type. */
10308 : 174178 : rest_of_type_compilation (enumtype, toplevel);
10309 : :
10310 : 174178 : finish_incomplete_vars (incomplete_vars, toplevel);
10311 : :
10312 : : /* If this enum is defined inside a struct, add it to
10313 : : struct_types. */
10314 : 174178 : if (warn_cxx_compat
10315 : 126 : && struct_parse_info != NULL
10316 : 42 : && !in_sizeof && !in_typeof && !in_alignof && !in_countof)
10317 : 42 : struct_parse_info->struct_types.safe_push (enumtype);
10318 : :
10319 : : /* Check for consistency with previous definition */
10320 : 174178 : if (flag_isoc23)
10321 : : {
10322 : 136225 : tree vistype = previous_tag (enumtype);
10323 : 136225 : if (vistype
10324 : 33 : && TREE_CODE (vistype) == TREE_CODE (enumtype)
10325 : 136254 : && !C_TYPE_BEING_DEFINED (vistype))
10326 : : {
10327 : 29 : TYPE_STUB_DECL (enumtype) = TYPE_STUB_DECL (vistype);
10328 : 29 : if (!comptypes_same_p (enumtype, vistype))
10329 : 3 : error("conflicting redefinition of enum %qT", enumtype);
10330 : : }
10331 : : }
10332 : :
10333 : 174178 : C_TYPE_BEING_DEFINED (enumtype) = 0;
10334 : :
10335 : 174178 : return enumtype;
10336 : : }
10337 : :
10338 : : /* Build and install a CONST_DECL for one value of the
10339 : : current enumeration type (one that was begun with start_enum).
10340 : : DECL_LOC is the location of the enumerator.
10341 : : LOC is the location of the '=' operator if any, DECL_LOC otherwise.
10342 : : Return a tree-list containing the CONST_DECL and its value.
10343 : : Assignment of sequential values by default is handled here. */
10344 : :
10345 : : tree
10346 : 5547087 : build_enumerator (location_t decl_loc, location_t loc,
10347 : : struct c_enum_contents *the_enum, tree name, tree value)
10348 : : {
10349 : 5547087 : tree decl;
10350 : 5547087 : tree old_decl;
10351 : :
10352 : : /* Validate and default VALUE. */
10353 : :
10354 : 5547087 : if (value != NULL_TREE)
10355 : : {
10356 : : /* Don't issue more errors for error_mark_node (i.e. an
10357 : : undeclared identifier) - just ignore the value expression. */
10358 : 3425615 : if (value == error_mark_node)
10359 : : value = NULL_TREE;
10360 : 3424950 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
10361 : : {
10362 : 6 : error_at (loc, "enumerator value for %qE is not an integer constant",
10363 : : name);
10364 : 6 : value = NULL_TREE;
10365 : : }
10366 : : else
10367 : : {
10368 : 3424944 : if (TREE_CODE (value) != INTEGER_CST)
10369 : : {
10370 : 101 : value = c_fully_fold (value, false, NULL);
10371 : 101 : if (TREE_CODE (value) == INTEGER_CST)
10372 : 52 : pedwarn (loc, OPT_Wpedantic,
10373 : : "enumerator value for %qE is not an integer "
10374 : : "constant expression", name);
10375 : : }
10376 : 3424944 : if (TREE_CODE (value) != INTEGER_CST)
10377 : : {
10378 : 49 : error ("enumerator value for %qE is not an integer constant",
10379 : : name);
10380 : 49 : value = NULL_TREE;
10381 : : }
10382 : : else
10383 : : {
10384 : 3424895 : value = default_conversion (value);
10385 : 3424895 : constant_expression_warning (value);
10386 : : }
10387 : : }
10388 : : }
10389 : :
10390 : : /* Default based on previous value. */
10391 : : /* It should no longer be possible to have NON_LVALUE_EXPR
10392 : : in the default. */
10393 : 3424950 : if (value == NULL_TREE)
10394 : : {
10395 : 2122192 : value = the_enum->enum_next_value;
10396 : 2122192 : if (the_enum->enum_overflow)
10397 : 8 : error_at (loc, "overflow in enumeration values");
10398 : : }
10399 : 5547087 : if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10400 : : {
10401 : : /* Enumeration constants must fit in the fixed underlying type. */
10402 : 161 : if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (the_enum->enum_type)))
10403 : 4 : error_at (loc,
10404 : : "enumerator value outside the range of underlying type");
10405 : : /* Enumeration constants for an enum with fixed underlying type
10406 : : have the enum type, both inside and outside the
10407 : : definition. */
10408 : 161 : value = convert (the_enum->enum_type, value);
10409 : : }
10410 : 5546926 : else if (flag_isoc23
10411 : 5134636 : && (old_decl = lookup_name_in_scope (name, current_scope))
10412 : 35 : && old_decl != error_mark_node
10413 : 35 : && TREE_TYPE (old_decl)
10414 : 35 : && TREE_TYPE (TREE_TYPE (old_decl))
10415 : 5546960 : && TREE_CODE (old_decl) == CONST_DECL)
10416 : : {
10417 : : /* Enumeration constants in a redeclaration have the previous type. */
10418 : 34 : tree previous_type = TREE_TYPE (DECL_INITIAL (old_decl));
10419 : 34 : if (!int_fits_type_p (value, previous_type))
10420 : : {
10421 : 2 : error_at (loc, "value of redeclared enumerator outside the range "
10422 : : "of %qT", previous_type);
10423 : 2 : locate_old_decl (old_decl);
10424 : : }
10425 : 34 : value = convert (previous_type, value);
10426 : : }
10427 : : else
10428 : : {
10429 : : /* Even though the underlying type of an enum is unspecified, the
10430 : : type of enumeration constants is explicitly defined as int
10431 : : (6.4.4.3/2 in the C99 Standard). C23 allows any integer type, and
10432 : : GCC allows such types for older standards as an extension. */
10433 : 5546892 : bool warned_range = false;
10434 : 5546892 : if (!int_fits_type_p (value,
10435 : 5546892 : (TYPE_UNSIGNED (TREE_TYPE (value))
10436 : : ? uintmax_type_node
10437 : : : intmax_type_node)))
10438 : : /* GCC does not consider its types larger than intmax_t to be
10439 : : extended integer types (although C23 would permit such types to
10440 : : be considered extended integer types if all the features
10441 : : required by <stdint.h> and <inttypes.h> macros, such as support
10442 : : for integer constants and I/O, were present), so diagnose if
10443 : : such a wider type is used. (If the wider type arose from a
10444 : : constant of such a type, that will also have been diagnosed,
10445 : : but this is the only diagnostic in the case where it arises
10446 : : from choosing a wider type automatically when adding 1
10447 : : overflows.) */
10448 : 11 : warned_range = pedwarn (loc, OPT_Wpedantic,
10449 : : "enumerator value outside the range of %qs",
10450 : 11 : (TYPE_UNSIGNED (TREE_TYPE (value))
10451 : : ? "uintmax_t"
10452 : : : "intmax_t"));
10453 : 5546892 : if (!warned_range && !int_fits_type_p (value, integer_type_node))
10454 : 4657 : pedwarn_c11 (loc, OPT_Wpedantic,
10455 : : "ISO C restricts enumerator values to range of %<int%> "
10456 : : "before C23");
10457 : :
10458 : : /* The ISO C Standard mandates enumerators to have type int before
10459 : : C23, even though the underlying type of an enum type is
10460 : : unspecified. C23 allows enumerators of any integer type. During
10461 : : the parsing of the enumeration, C23 specifies that constants
10462 : : representable in int have type int, constants not representable
10463 : : in int have the type of the given expression if any, and
10464 : : constants not representable in int and derived by adding 1 to the
10465 : : previous constant have the type of that constant unless the
10466 : : addition would overflow or wraparound, in which case a wider type
10467 : : of the same signedness is chosen automatically; after the
10468 : : enumeration is parsed, all the constants have the type of the
10469 : : enumeration if any do not fit in int. */
10470 : 5546892 : if (int_fits_type_p (value, integer_type_node))
10471 : 5542231 : value = convert (integer_type_node, value);
10472 : : }
10473 : :
10474 : : /* Set basis for default for next value. */
10475 : 5547087 : if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10476 : : {
10477 : 161 : tree underlying_type = ENUM_UNDERLYING_TYPE (the_enum->enum_type);
10478 : 161 : if (TREE_CODE (underlying_type) == BOOLEAN_TYPE)
10479 : : /* A value of 2 following a value of 1 overflows bool, but we
10480 : : cannot carry out addition directly on bool without
10481 : : promotion, and converting the result of arithmetic in a
10482 : : wider type back to bool would not produce the right result
10483 : : for this overflow check. */
10484 : 61 : the_enum->enum_next_value = invert_truthvalue_loc (loc, value);
10485 : : else
10486 : 100 : the_enum->enum_next_value
10487 : 100 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10488 : : PLUS_EXPR, convert (underlying_type, value),
10489 : : convert (underlying_type, integer_one_node),
10490 : : false);
10491 : : }
10492 : : else
10493 : : {
10494 : : /* In a redeclaration the type can already be the enumeral type. */
10495 : 5546926 : if (TREE_CODE (TREE_TYPE (value)) == ENUMERAL_TYPE)
10496 : 12 : value = convert (ENUM_UNDERLYING_TYPE (TREE_TYPE (value)), value);
10497 : 5546926 : the_enum->enum_next_value
10498 : 5546926 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10499 : : PLUS_EXPR, value, integer_one_node, false);
10500 : : }
10501 : 5547087 : the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
10502 : 5547087 : if (the_enum->enum_overflow
10503 : 5547087 : && !ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10504 : : {
10505 : : /* Choose a wider type with the same signedness if
10506 : : available. */
10507 : 3289 : int prec = TYPE_PRECISION (TREE_TYPE (value)) + 1;
10508 : 3289 : bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (value));
10509 : 3289 : tree new_type = (unsignedp
10510 : 3289 : ? long_unsigned_type_node
10511 : : : long_integer_type_node);
10512 : 3289 : if (prec > TYPE_PRECISION (new_type))
10513 : 2921 : new_type = (unsignedp
10514 : 2921 : ? long_long_unsigned_type_node
10515 : : : long_long_integer_type_node);
10516 : 3289 : if (prec > TYPE_PRECISION (new_type))
10517 : 2459 : new_type = (unsignedp
10518 : 2459 : ? widest_unsigned_literal_type_node
10519 : : : widest_integer_literal_type_node);
10520 : 3289 : if (prec <= TYPE_PRECISION (new_type))
10521 : : {
10522 : 3284 : the_enum->enum_overflow = false;
10523 : 3284 : the_enum->enum_next_value
10524 : 3284 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10525 : : PLUS_EXPR, convert (new_type, value),
10526 : : integer_one_node, false);
10527 : 3284 : gcc_assert (!tree_int_cst_lt (the_enum->enum_next_value, value));
10528 : : }
10529 : : }
10530 : :
10531 : : /* Now create a declaration for the enum value name. */
10532 : :
10533 : 5547087 : decl = build_decl (decl_loc, CONST_DECL, name, TREE_TYPE (value));
10534 : 5547087 : DECL_INITIAL (decl) = value;
10535 : 5547087 : DECL_CONTEXT (decl) = the_enum->enum_type;
10536 : 5547087 : pushdecl (decl);
10537 : :
10538 : 5547087 : return tree_cons (decl, value, NULL_TREE);
10539 : : }
10540 : :
10541 : : /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
10542 : :
10543 : : tree
10544 : 0 : c_simulate_enum_decl (location_t loc, const char *name,
10545 : : vec<string_int_pair> *values_ptr)
10546 : : {
10547 : 0 : location_t saved_loc = input_location;
10548 : 0 : input_location = loc;
10549 : :
10550 : 0 : struct c_enum_contents the_enum;
10551 : 0 : tree enumtype = start_enum (loc, &the_enum, get_identifier (name),
10552 : : NULL_TREE, false);
10553 : :
10554 : 0 : tree value_chain = NULL_TREE;
10555 : 0 : string_int_pair *value;
10556 : 0 : vec<string_int_pair> values = *values_ptr;
10557 : 0 : unsigned int i;
10558 : 0 : FOR_EACH_VEC_ELT (values, i, value)
10559 : : {
10560 : 0 : tree decl = build_enumerator (loc, loc, &the_enum,
10561 : : get_identifier (value->first),
10562 : : build_int_cst (integer_type_node,
10563 : 0 : value->second));
10564 : 0 : TREE_CHAIN (decl) = value_chain;
10565 : 0 : value_chain = decl;
10566 : : }
10567 : :
10568 : 0 : finish_enum (enumtype, nreverse (value_chain), NULL_TREE);
10569 : :
10570 : 0 : input_location = saved_loc;
10571 : 0 : return enumtype;
10572 : : }
10573 : :
10574 : : /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
10575 : :
10576 : : tree
10577 : 0 : c_simulate_record_decl (location_t loc, const char *name,
10578 : : array_slice<const tree> fields)
10579 : : {
10580 : 0 : location_t saved_loc = input_location;
10581 : 0 : input_location = loc;
10582 : :
10583 : 0 : class c_struct_parse_info *struct_info;
10584 : 0 : tree ident = get_identifier (name);
10585 : 0 : tree type = start_struct (loc, RECORD_TYPE, ident, &struct_info);
10586 : :
10587 : 0 : for (unsigned int i = 0; i < fields.size (); ++i)
10588 : : {
10589 : 0 : DECL_FIELD_CONTEXT (fields[i]) = type;
10590 : 0 : if (i > 0)
10591 : 0 : DECL_CHAIN (fields[i - 1]) = fields[i];
10592 : : }
10593 : :
10594 : 0 : finish_struct (loc, type, fields[0], NULL_TREE, struct_info);
10595 : :
10596 : 0 : tree decl = build_decl (loc, TYPE_DECL, ident, type);
10597 : 0 : set_underlying_type (decl);
10598 : 0 : lang_hooks.decls.pushdecl (decl);
10599 : :
10600 : 0 : input_location = saved_loc;
10601 : 0 : return type;
10602 : : }
10603 : :
10604 : : /* Create the FUNCTION_DECL for a function definition.
10605 : : DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
10606 : : the declaration; they describe the function's name and the type it returns,
10607 : : but twisted together in a fashion that parallels the syntax of C.
10608 : :
10609 : : This function creates a binding context for the function body
10610 : : as well as setting up the FUNCTION_DECL in current_function_decl.
10611 : :
10612 : : Returns true on success. If the DECLARATOR is not suitable for a function
10613 : : (it defines a datum instead), we return false to report a parse error. */
10614 : :
10615 : : bool
10616 : 35349723 : start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
10617 : : tree attributes)
10618 : : {
10619 : 35349723 : tree decl1, old_decl;
10620 : 35349723 : tree restype, resdecl;
10621 : 35349723 : location_t loc;
10622 : 35349723 : location_t result_loc;
10623 : 35349723 : tree expr = NULL;
10624 : :
10625 : 35349723 : current_function_returns_value = 0; /* Assume, until we see it does. */
10626 : 35349723 : current_function_returns_null = 0;
10627 : 35349723 : current_function_returns_abnormally = 0;
10628 : 35349723 : warn_about_return_type = 0;
10629 : 35349723 : c_switch_stack = NULL;
10630 : :
10631 : : /* Indicate no valid break/continue context. */
10632 : 35349723 : in_statement = 0;
10633 : :
10634 : 35349723 : decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
10635 : : &attributes, &expr, NULL, DEPRECATED_NORMAL);
10636 : 35349723 : invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
10637 : :
10638 : : /* If the declarator is not suitable for a function definition,
10639 : : cause a syntax error. */
10640 : 35349723 : if (decl1 == NULL_TREE
10641 : 35349692 : || TREE_CODE (decl1) != FUNCTION_DECL)
10642 : : return false;
10643 : :
10644 : : /* Nested functions may have variably modified (return) type.
10645 : : Make sure the size expression is evaluated at this point. */
10646 : 35349685 : if (expr && !current_scope->parm_flag)
10647 : 9 : add_stmt (fold_convert (void_type_node, expr));
10648 : :
10649 : 35349685 : loc = DECL_SOURCE_LOCATION (decl1);
10650 : :
10651 : : /* A nested function is not global. */
10652 : 35349685 : if (current_function_decl != NULL_TREE)
10653 : 1530 : TREE_PUBLIC (decl1) = 0;
10654 : :
10655 : 35349685 : c_decl_attributes (&decl1, attributes, 0);
10656 : :
10657 : 35349685 : if (DECL_DECLARED_INLINE_P (decl1)
10658 : 34675014 : && DECL_UNINLINABLE (decl1)
10659 : 35349695 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
10660 : : {
10661 : 3 : auto_urlify_attributes sentinel;
10662 : 3 : warning_at (loc, OPT_Wattributes,
10663 : : "inline function %qD given attribute %qs",
10664 : : decl1, "noinline");
10665 : 3 : }
10666 : :
10667 : : /* Handle gnu_inline attribute. */
10668 : 35349685 : if (declspecs->inline_p
10669 : 34675017 : && !flag_gnu89_inline
10670 : 34641055 : && TREE_CODE (decl1) == FUNCTION_DECL
10671 : 69990740 : && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
10672 : 154800 : || current_function_decl))
10673 : : {
10674 : 34486327 : if (declspecs->storage_class != csc_static)
10675 : 34486321 : DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
10676 : : }
10677 : :
10678 : 35349685 : announce_function (decl1);
10679 : :
10680 : 35349685 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
10681 : : {
10682 : 1 : error_at (loc, "return type is an incomplete type");
10683 : : /* Make it return void instead. */
10684 : 1 : TREE_TYPE (decl1)
10685 : 2 : = c_build_function_type (void_type_node,
10686 : 1 : TYPE_ARG_TYPES (TREE_TYPE (decl1)),
10687 : 1 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl1)));
10688 : : }
10689 : :
10690 : 35349685 : if (warn_about_return_type)
10691 : 1404 : permerror_opt (loc, flag_isoc99 ? OPT_Wimplicit_int
10692 : 0 : : (warn_return_type > 0 ? OPT_Wreturn_type
10693 : : : OPT_Wimplicit_int),
10694 : : "return type defaults to %<int%>");
10695 : :
10696 : : /* Make the init_value nonzero so pushdecl knows this is not tentative.
10697 : : error_mark_node is replaced below (in pop_scope) with the BLOCK. */
10698 : 35349685 : DECL_INITIAL (decl1) = error_mark_node;
10699 : :
10700 : : /* If this definition isn't a prototype and we had a prototype declaration
10701 : : before, copy the arg type info from that prototype. */
10702 : 35349685 : old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
10703 : 35349685 : if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
10704 : 35124461 : old_decl = NULL_TREE;
10705 : :
10706 : 35349685 : current_function_prototype_locus = UNKNOWN_LOCATION;
10707 : 35349685 : current_function_prototype_built_in = false;
10708 : 35349685 : current_function_prototype_arg_types = NULL_TREE;
10709 : 35349685 : tree newtype = TREE_TYPE (decl1);
10710 : 35349685 : tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype;
10711 : 35349685 : if (!prototype_p (newtype))
10712 : : {
10713 : 13109 : tree oldrt = TREE_TYPE (oldtype);
10714 : 13109 : tree newrt = TREE_TYPE (newtype);
10715 : 13109 : if (old_decl != NULL_TREE
10716 : 315 : && TREE_CODE (oldtype) == FUNCTION_TYPE
10717 : 13424 : && comptypes (oldrt, newrt))
10718 : : {
10719 : 296 : if (stdarg_p (oldtype))
10720 : : {
10721 : 1 : auto_diagnostic_group d;
10722 : 1 : warning_at (loc, 0, "%q+D defined as variadic function "
10723 : : "without prototype", decl1);
10724 : 1 : locate_old_decl (old_decl);
10725 : 1 : }
10726 : 296 : TREE_TYPE (decl1) = composite_type (oldtype, newtype);
10727 : 296 : current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
10728 : 296 : current_function_prototype_built_in
10729 : 296 : = C_DECL_BUILTIN_PROTOTYPE (old_decl);
10730 : 296 : current_function_prototype_arg_types
10731 : 296 : = TYPE_ARG_TYPES (newtype);
10732 : : }
10733 : 13109 : if (TREE_PUBLIC (decl1))
10734 : : {
10735 : : /* If there is an external prototype declaration of this
10736 : : function, record its location but do not copy information
10737 : : to this decl. This may be an invisible declaration
10738 : : (built-in or in a scope which has finished) or simply
10739 : : have more refined argument types than any declaration
10740 : : found above. */
10741 : 12695 : struct c_binding *b;
10742 : 12970 : for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
10743 : 711 : if (B_IN_SCOPE (b, external_scope))
10744 : : break;
10745 : 12695 : if (b)
10746 : : {
10747 : 436 : tree ext_decl, ext_type;
10748 : 436 : ext_decl = b->decl;
10749 : 436 : ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
10750 : 436 : if (TREE_CODE (ext_type) == FUNCTION_TYPE
10751 : 872 : && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
10752 : 436 : TREE_TYPE (ext_type)))
10753 : : {
10754 : 416 : current_function_prototype_locus
10755 : 416 : = DECL_SOURCE_LOCATION (ext_decl);
10756 : 416 : current_function_prototype_built_in
10757 : 416 : = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
10758 : 416 : current_function_prototype_arg_types
10759 : 416 : = TYPE_ARG_TYPES (ext_type);
10760 : : }
10761 : : }
10762 : : }
10763 : : }
10764 : :
10765 : : /* Optionally warn about C23 compatibility. */
10766 : 35349685 : if (warn_deprecated_non_prototype
10767 : 34 : && old_decl != NULL_TREE
10768 : 5 : && TREE_CODE (oldtype) == FUNCTION_TYPE
10769 : 5 : && !TYPE_ARG_TYPES (oldtype)
10770 : 5 : && !TYPE_NO_NAMED_ARGS_STDARG_P (oldtype)
10771 : 35349690 : && (TYPE_ARG_TYPES (newtype)
10772 : 3 : && TREE_VALUE (TYPE_ARG_TYPES (newtype)) != void_type_node))
10773 : : {
10774 : 3 : bool warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
10775 : : "ISO C23 does not allow defining"
10776 : : " parameters for function %qE declared"
10777 : : " without parameters",
10778 : : decl1);
10779 : 3 : if (warned)
10780 : 3 : inform (DECL_SOURCE_LOCATION (old_decl), "declared here");
10781 : : }
10782 : :
10783 : : /* Optionally warn of old-fashioned def with no previous prototype. */
10784 : 35349685 : if (warn_strict_prototypes
10785 : 107292 : && old_decl != error_mark_node
10786 : 107292 : && !prototype_p (TREE_TYPE (decl1))
10787 : 35349686 : && C_DECL_ISNT_PROTOTYPE (old_decl))
10788 : 1 : warning_at (loc, OPT_Wstrict_prototypes,
10789 : : "function declaration isn%'t a prototype");
10790 : : /* Optionally warn of any global def with no previous prototype. */
10791 : 35349684 : else if (warn_missing_prototypes
10792 : 106993 : && old_decl != error_mark_node
10793 : 106993 : && TREE_PUBLIC (decl1)
10794 : 73906 : && !MAIN_NAME_P (DECL_NAME (decl1))
10795 : 73870 : && C_DECL_ISNT_PROTOTYPE (old_decl)
10796 : 35350325 : && !DECL_DECLARED_INLINE_P (decl1))
10797 : 4 : warning_at (loc, OPT_Wmissing_prototypes,
10798 : : "no previous prototype for %qD", decl1);
10799 : : /* Optionally warn of any def with no previous prototype
10800 : : if the function has already been used. */
10801 : 35349680 : else if (warn_missing_prototypes
10802 : 106989 : && old_decl != NULL_TREE
10803 : 74225 : && old_decl != error_mark_node
10804 : 74225 : && TREE_USED (old_decl)
10805 : 35356753 : && !prototype_p (TREE_TYPE (old_decl)))
10806 : 0 : warning_at (loc, OPT_Wmissing_prototypes,
10807 : : "%qD was used with no prototype before its definition", decl1);
10808 : : /* Optionally warn of any global def with no previous declaration. */
10809 : 35349680 : else if (warn_missing_declarations
10810 : 4 : && TREE_PUBLIC (decl1)
10811 : 3 : && old_decl == NULL_TREE
10812 : 3 : && !MAIN_NAME_P (DECL_NAME (decl1))
10813 : 35349683 : && !DECL_DECLARED_INLINE_P (decl1))
10814 : 2 : warning_at (loc, OPT_Wmissing_declarations,
10815 : : "no previous declaration for %qD",
10816 : : decl1);
10817 : : /* Optionally warn of any def with no previous declaration
10818 : : if the function has already been used. */
10819 : 35349678 : else if (warn_missing_declarations
10820 : 2 : && old_decl != NULL_TREE
10821 : 0 : && old_decl != error_mark_node
10822 : 0 : && TREE_USED (old_decl)
10823 : 35349678 : && C_DECL_IMPLICIT (old_decl))
10824 : 0 : warning_at (loc, OPT_Wmissing_declarations,
10825 : : "%qD was used with no declaration before its definition", decl1);
10826 : :
10827 : : /* This function exists in static storage.
10828 : : (This does not mean `static' in the C sense!) */
10829 : 35349685 : TREE_STATIC (decl1) = 1;
10830 : :
10831 : : /* This is the earliest point at which we might know the assembler
10832 : : name of the function. Thus, if it's set before this, die horribly. */
10833 : 35349685 : gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
10834 : :
10835 : : /* If #pragma weak was used, mark the decl weak now. */
10836 : 35349685 : if (current_scope == file_scope)
10837 : 35348155 : maybe_apply_pragma_weak (decl1);
10838 : :
10839 : : /* Warn for unlikely, improbable, or stupid declarations of `main'. */
10840 : 35349685 : if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
10841 : : {
10842 : 3186 : if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
10843 : 3186 : != integer_type_node)
10844 : 5 : pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
10845 : 3181 : else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
10846 : 1 : pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
10847 : : decl1);
10848 : :
10849 : 3186 : check_main_parameter_types (decl1);
10850 : :
10851 : 3186 : if (!TREE_PUBLIC (decl1))
10852 : 0 : pedwarn (loc, OPT_Wmain,
10853 : : "%qD is normally a non-static function", decl1);
10854 : : }
10855 : :
10856 : 35349685 : tree parms = current_function_arg_info->parms;
10857 : 35349685 : if (old_decl)
10858 : : {
10859 : 225224 : location_t origloc = DECL_SOURCE_LOCATION (old_decl);
10860 : 225224 : warn_parm_array_mismatch (origloc, old_decl, parms);
10861 : : }
10862 : :
10863 : : /* Record the decl so that the function name is defined.
10864 : : If we already have a decl for this name, and it is a FUNCTION_DECL,
10865 : : use the old decl. */
10866 : :
10867 : 35349685 : current_function_decl = pushdecl (decl1);
10868 : :
10869 : 35349685 : if (tree access = build_attr_access_from_parms (parms, false))
10870 : 56196 : decl_attributes (¤t_function_decl, access, ATTR_FLAG_INTERNAL,
10871 : : old_decl);
10872 : :
10873 : 35349685 : push_scope ();
10874 : 35349685 : declare_parm_level ();
10875 : :
10876 : : /* Set the result decl source location to the location of the typespec. */
10877 : 3999041 : result_loc = (declspecs->locations[cdw_typespec] == UNKNOWN_LOCATION
10878 : 35349685 : ? loc : declspecs->locations[cdw_typespec]);
10879 : 35349685 : restype = TREE_TYPE (TREE_TYPE (current_function_decl));
10880 : 35349685 : resdecl = build_decl (result_loc, RESULT_DECL, NULL_TREE, restype);
10881 : 35349685 : DECL_ARTIFICIAL (resdecl) = 1;
10882 : 35349685 : DECL_IGNORED_P (resdecl) = 1;
10883 : 35349685 : DECL_RESULT (current_function_decl) = resdecl;
10884 : :
10885 : 35349685 : start_fname_decls ();
10886 : :
10887 : 35349685 : return true;
10888 : : }
10889 : :
10890 : : /* Subroutine of store_parm_decls which handles new-style function
10891 : : definitions (prototype format). The parms already have decls, so we
10892 : : need only record them as in effect and complain if any redundant
10893 : : old-style parm decls were written. */
10894 : : static void
10895 : 35336589 : store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
10896 : : {
10897 : 35336589 : tree decl;
10898 : 35336589 : c_arg_tag *tag;
10899 : 35336589 : unsigned ix;
10900 : :
10901 : 35336589 : if (current_scope->bindings)
10902 : : {
10903 : 7 : error_at (DECL_SOURCE_LOCATION (fndecl),
10904 : : "old-style parameter declarations in prototyped "
10905 : : "function definition");
10906 : :
10907 : : /* Get rid of the old-style declarations. */
10908 : 7 : pop_scope ();
10909 : 7 : push_scope ();
10910 : : }
10911 : : /* Don't issue this warning for nested functions, and don't issue this
10912 : : warning if we got here because ARG_INFO_TYPES was error_mark_node
10913 : : (this happens when a function definition has just an ellipsis in
10914 : : its parameter list). */
10915 : 35336582 : else if (!in_system_header_at (input_location)
10916 : 624308 : && !current_function_scope
10917 : 622925 : && arg_info->types != error_mark_node
10918 : 35959507 : && !arg_info->c23_empty_parens)
10919 : 571431 : warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
10920 : : "traditional C rejects ISO C style function definitions");
10921 : :
10922 : : /* Now make all the parameter declarations visible in the function body.
10923 : : We can bypass most of the grunt work of pushdecl. */
10924 : 132728384 : for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
10925 : : {
10926 : 97391795 : DECL_CONTEXT (decl) = current_function_decl;
10927 : 97391795 : if (DECL_NAME (decl))
10928 : : {
10929 : 97391233 : bind (DECL_NAME (decl), decl, current_scope,
10930 : : /*invisible=*/false, /*nested=*/false,
10931 : : UNKNOWN_LOCATION);
10932 : 97391233 : if (!TREE_USED (decl))
10933 : 97373988 : warn_if_shadowing (decl);
10934 : : }
10935 : : else
10936 : 562 : pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_parameter_name,
10937 : : "ISO C does not support omitting parameter names in "
10938 : : "function definitions before C23");
10939 : : }
10940 : :
10941 : : /* Record the parameter list in the function declaration. */
10942 : 35336589 : DECL_ARGUMENTS (fndecl) = arg_info->parms;
10943 : :
10944 : : /* Now make all the ancillary declarations visible, likewise. */
10945 : 35336645 : for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
10946 : : {
10947 : 56 : DECL_CONTEXT (decl) = current_function_decl;
10948 : 56 : if (DECL_NAME (decl))
10949 : 0 : bind (DECL_NAME (decl), decl, current_scope,
10950 : : /*invisible=*/false,
10951 : 0 : /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
10952 : : UNKNOWN_LOCATION);
10953 : : }
10954 : :
10955 : : /* And all the tag declarations. */
10956 : 35336699 : FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
10957 : 56 : if (tag->id)
10958 : 23 : bind (tag->id, tag->type, current_scope,
10959 : : /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
10960 : 35336589 : }
10961 : :
10962 : : /* Subroutine of store_parm_decls which handles old-style function
10963 : : definitions (separate parameter list and declarations). */
10964 : :
10965 : : static void
10966 : 13096 : store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
10967 : : {
10968 : 13096 : struct c_binding *b;
10969 : 13096 : tree parm, decl, last;
10970 : 13096 : tree parmids = arg_info->parms;
10971 : 13096 : hash_set<tree> seen_args;
10972 : :
10973 : 13096 : if (!in_system_header_at (input_location))
10974 : : {
10975 : 13093 : if (flag_isoc23)
10976 : 1349 : pedwarn (DECL_SOURCE_LOCATION (fndecl),
10977 : 1349 : OPT_Wold_style_definition, "old-style function definition");
10978 : : else
10979 : 11744 : warning_at (DECL_SOURCE_LOCATION (fndecl),
10980 : 11744 : OPT_Wold_style_definition,
10981 : : "old-style function definition");
10982 : : }
10983 : :
10984 : 13096 : if (current_scope->had_vla_unspec)
10985 : 1 : error ("%<[*]%> not allowed in other than function prototype scope");
10986 : :
10987 : : /* Match each formal parameter name with its declaration. Save each
10988 : : decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
10989 : 47365 : for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10990 : : {
10991 : 34269 : if (TREE_VALUE (parm) == NULL_TREE)
10992 : : {
10993 : 0 : error_at (DECL_SOURCE_LOCATION (fndecl),
10994 : : "parameter name missing from parameter list");
10995 : 0 : TREE_PURPOSE (parm) = NULL_TREE;
10996 : 0 : continue;
10997 : : }
10998 : :
10999 : 34269 : b = I_SYMBOL_BINDING (TREE_VALUE (parm));
11000 : 34269 : if (b && B_IN_CURRENT_SCOPE (b))
11001 : : {
11002 : 22649 : decl = b->decl;
11003 : : /* Skip erroneous parameters. */
11004 : 22649 : if (decl == error_mark_node)
11005 : 2 : continue;
11006 : : /* If we got something other than a PARM_DECL it is an error. */
11007 : 22647 : if (TREE_CODE (decl) != PARM_DECL)
11008 : : {
11009 : 7 : error_at (DECL_SOURCE_LOCATION (decl),
11010 : : "%qD declared as a non-parameter", decl);
11011 : 7 : continue;
11012 : : }
11013 : : /* If the declaration is already marked, we have a duplicate
11014 : : name. Complain and ignore the duplicate. */
11015 : 22640 : else if (seen_args.contains (decl))
11016 : : {
11017 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
11018 : : "multiple parameters named %qD", decl);
11019 : 0 : TREE_PURPOSE (parm) = NULL_TREE;
11020 : 0 : continue;
11021 : : }
11022 : : /* If the declaration says "void", complain and turn it into
11023 : : an int. */
11024 : 22640 : else if (VOID_TYPE_P (TREE_TYPE (decl)))
11025 : : {
11026 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
11027 : : "parameter %qD declared with void type", decl);
11028 : 0 : TREE_TYPE (decl) = integer_type_node;
11029 : 0 : DECL_ARG_TYPE (decl) = integer_type_node;
11030 : 0 : layout_decl (decl, 0);
11031 : : }
11032 : 22640 : warn_if_shadowing (decl);
11033 : : }
11034 : : /* If no declaration found, default to int. */
11035 : : else
11036 : : {
11037 : : /* FIXME diagnostics: This should be the location of the argument,
11038 : : not the FNDECL. E.g., for an old-style declaration
11039 : :
11040 : : int f10(v) { blah; }
11041 : :
11042 : : We should use the location of the V, not the F10.
11043 : : Unfortunately, the V is an IDENTIFIER_NODE which has no
11044 : : location. In the future we need locations for c_arg_info
11045 : : entries.
11046 : :
11047 : : See gcc.dg/Wshadow-3.c for an example of this problem. */
11048 : 11620 : decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
11049 : 11620 : PARM_DECL, TREE_VALUE (parm), integer_type_node);
11050 : 11620 : DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
11051 : 11620 : pushdecl (decl);
11052 : 11620 : warn_if_shadowing (decl);
11053 : :
11054 : 11620 : if (flag_isoc99)
11055 : 118 : permerror_opt (DECL_SOURCE_LOCATION (decl),
11056 : 118 : OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
11057 : : decl);
11058 : : else
11059 : 11502 : warning_at (DECL_SOURCE_LOCATION (decl),
11060 : 11502 : OPT_Wmissing_parameter_type,
11061 : : "type of %qD defaults to %<int%>", decl);
11062 : : }
11063 : :
11064 : 34260 : TREE_PURPOSE (parm) = decl;
11065 : 34260 : seen_args.add (decl);
11066 : : }
11067 : :
11068 : : /* Now examine the parms chain for incomplete declarations
11069 : : and declarations with no corresponding names. */
11070 : :
11071 : 47444 : for (b = current_scope->bindings; b; b = b->prev)
11072 : : {
11073 : 34348 : parm = b->decl;
11074 : 34348 : if (TREE_CODE (parm) != PARM_DECL)
11075 : 79 : continue;
11076 : :
11077 : 34269 : if (TREE_TYPE (parm) != error_mark_node
11078 : 34269 : && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
11079 : : {
11080 : 0 : error_at (DECL_SOURCE_LOCATION (parm),
11081 : : "parameter %qD has incomplete type", parm);
11082 : 0 : TREE_TYPE (parm) = error_mark_node;
11083 : : }
11084 : :
11085 : 34269 : if (!seen_args.contains (parm))
11086 : : {
11087 : 9 : error_at (DECL_SOURCE_LOCATION (parm),
11088 : : "declaration for parameter %qD but no such parameter",
11089 : : parm);
11090 : :
11091 : : /* Pretend the parameter was not missing.
11092 : : This gets us to a standard state and minimizes
11093 : : further error messages. */
11094 : 9 : parmids = chainon (parmids, tree_cons (parm, 0, 0));
11095 : : }
11096 : : }
11097 : :
11098 : : /* Chain the declarations together in the order of the list of
11099 : : names. Store that chain in the function decl, replacing the
11100 : : list of names. Update the current scope to match. */
11101 : 13096 : DECL_ARGUMENTS (fndecl) = NULL_TREE;
11102 : :
11103 : 13104 : for (parm = parmids; parm; parm = TREE_CHAIN (parm))
11104 : 8680 : if (TREE_PURPOSE (parm))
11105 : : break;
11106 : 13096 : if (parm && TREE_PURPOSE (parm))
11107 : : {
11108 : 8672 : last = TREE_PURPOSE (parm);
11109 : 8672 : DECL_ARGUMENTS (fndecl) = last;
11110 : :
11111 : 34270 : for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
11112 : 25598 : if (TREE_PURPOSE (parm))
11113 : : {
11114 : 25597 : DECL_CHAIN (last) = TREE_PURPOSE (parm);
11115 : 25598 : last = TREE_PURPOSE (parm);
11116 : : }
11117 : 8672 : DECL_CHAIN (last) = NULL_TREE;
11118 : : }
11119 : :
11120 : : /* If there was a previous prototype,
11121 : : set the DECL_ARG_TYPE of each argument according to
11122 : : the type previously specified, and report any mismatches. */
11123 : :
11124 : 13096 : if (current_function_prototype_arg_types)
11125 : : {
11126 : 171 : tree type;
11127 : 171 : for (parm = DECL_ARGUMENTS (fndecl),
11128 : 171 : type = current_function_prototype_arg_types;
11129 : 373 : parm || (type != NULL_TREE
11130 : 166 : && TREE_VALUE (type) != error_mark_node
11131 : 166 : && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
11132 : 202 : parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
11133 : : {
11134 : 212 : if (parm == NULL_TREE
11135 : 206 : || type == NULL_TREE
11136 : 418 : || (TREE_VALUE (type) != error_mark_node
11137 : 192 : && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
11138 : : {
11139 : 10 : if (current_function_prototype_built_in)
11140 : 6 : warning_at (DECL_SOURCE_LOCATION (fndecl),
11141 : 6 : 0, "number of arguments doesn%'t match "
11142 : : "built-in prototype");
11143 : : else
11144 : : {
11145 : : /* FIXME diagnostics: This should be the location of
11146 : : FNDECL, but there is bug when a prototype is
11147 : : declared inside function context, but defined
11148 : : outside of it (e.g., gcc.dg/pr15698-2.c). In
11149 : : which case FNDECL gets the location of the
11150 : : prototype, not the definition. */
11151 : 4 : error_at (input_location,
11152 : : "number of arguments doesn%'t match prototype");
11153 : :
11154 : 4 : error_at (current_function_prototype_locus,
11155 : : "prototype declaration");
11156 : : }
11157 : : break;
11158 : : }
11159 : : /* Type for passing arg must be consistent with that
11160 : : declared for the arg. ISO C says we take the unqualified
11161 : : type for parameters declared with qualified type. */
11162 : 202 : if (TREE_TYPE (parm) != error_mark_node
11163 : 201 : && TREE_VALUE (type) != error_mark_node
11164 : 389 : && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
11165 : 187 : != TYPE_ATOMIC (TREE_VALUE (type)))
11166 : 185 : || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
11167 : 185 : TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
11168 : : {
11169 : 36 : if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
11170 : 36 : == TYPE_ATOMIC (TREE_VALUE (type)))
11171 : 36 : && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
11172 : 34 : == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
11173 : : {
11174 : : /* Adjust argument to match prototype. E.g. a previous
11175 : : `int foo(float);' prototype causes
11176 : : `int foo(x) float x; {...}' to be treated like
11177 : : `int foo(float x) {...}'. This is particularly
11178 : : useful for argument types like uid_t. */
11179 : 17 : DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
11180 : :
11181 : : /* ??? Is it possible to get here with a
11182 : : built-in prototype or will it always have
11183 : : been diagnosed as conflicting with an
11184 : : old-style definition and discarded? */
11185 : 17 : if (current_function_prototype_built_in)
11186 : 2 : warning_at (DECL_SOURCE_LOCATION (parm),
11187 : 2 : OPT_Wpedantic, "promoted argument %qD "
11188 : : "doesn%'t match built-in prototype", parm);
11189 : : else
11190 : : {
11191 : 15 : pedwarn (DECL_SOURCE_LOCATION (parm),
11192 : 15 : OPT_Wpedantic, "promoted argument %qD "
11193 : : "doesn%'t match prototype", parm);
11194 : 15 : pedwarn (current_function_prototype_locus, OPT_Wpedantic,
11195 : : "prototype declaration");
11196 : : }
11197 : : }
11198 : : else
11199 : : {
11200 : 19 : if (current_function_prototype_built_in)
11201 : 10 : warning_at (DECL_SOURCE_LOCATION (parm),
11202 : 10 : 0, "argument %qD doesn%'t match "
11203 : : "built-in prototype", parm);
11204 : : else
11205 : : {
11206 : 9 : error_at (DECL_SOURCE_LOCATION (parm),
11207 : : "argument %qD doesn%'t match prototype", parm);
11208 : 9 : error_at (current_function_prototype_locus,
11209 : : "prototype declaration");
11210 : : }
11211 : : }
11212 : : }
11213 : : }
11214 : 171 : TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
11215 : : }
11216 : :
11217 : : /* Otherwise, create a prototype that would match. */
11218 : :
11219 : : else
11220 : : {
11221 : 12925 : tree actual = NULL_TREE, last = NULL_TREE, type;
11222 : :
11223 : 46988 : for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
11224 : : {
11225 : 34063 : type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
11226 : 34063 : if (last)
11227 : 25547 : TREE_CHAIN (last) = type;
11228 : : else
11229 : : actual = type;
11230 : 34063 : last = type;
11231 : : }
11232 : 12925 : type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
11233 : 12925 : if (last)
11234 : 8516 : TREE_CHAIN (last) = type;
11235 : : else
11236 : : actual = type;
11237 : :
11238 : : /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
11239 : : of the type of this function, but we need to avoid having this
11240 : : affect the types of other similarly-typed functions, so we must
11241 : : first force the generation of an identical (but separate) type
11242 : : node for the relevant function type. The new node we create
11243 : : will be a variant of the main variant of the original function
11244 : : type. */
11245 : :
11246 : 12925 : TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
11247 : :
11248 : 12925 : TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
11249 : : }
11250 : 13096 : }
11251 : :
11252 : : /* Store parameter declarations passed in ARG_INFO into the current
11253 : : function declaration. */
11254 : :
11255 : : void
11256 : 0 : store_parm_decls_from (struct c_arg_info *arg_info)
11257 : : {
11258 : 0 : current_function_arg_info = arg_info;
11259 : 0 : store_parm_decls ();
11260 : 0 : }
11261 : :
11262 : : /* Called by walk_tree to look for and update context-less labels
11263 : : or labels with context in the parent function. */
11264 : :
11265 : : static tree
11266 : 8127 : set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
11267 : : {
11268 : 8127 : tree ctx = static_cast<tree>(data);
11269 : 8127 : if (TREE_CODE (*tp) == LABEL_EXPR
11270 : 8127 : && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
11271 : 2 : || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
11272 : : {
11273 : 72 : DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
11274 : 72 : *walk_subtrees = 0;
11275 : : }
11276 : :
11277 : 8127 : return NULL_TREE;
11278 : : }
11279 : :
11280 : : /* Store the parameter declarations into the current function declaration.
11281 : : This is called after parsing the parameter declarations, before
11282 : : digesting the body of the function.
11283 : :
11284 : : For an old-style definition, construct a prototype out of the old-style
11285 : : parameter declarations and inject it into the function's type. */
11286 : :
11287 : : void
11288 : 35349685 : store_parm_decls (void)
11289 : : {
11290 : 35349685 : tree fndecl = current_function_decl;
11291 : 35349685 : bool proto;
11292 : :
11293 : : /* The argument information block for FNDECL. */
11294 : 35349685 : struct c_arg_info *arg_info = current_function_arg_info;
11295 : 35349685 : current_function_arg_info = 0;
11296 : :
11297 : : /* True if this definition is written with a prototype. In C23, an
11298 : : empty argument list was converted to (void) in grokparms; in
11299 : : older C standard versions, it does not give the function a type
11300 : : with a prototype for future calls. */
11301 : 35349685 : proto = arg_info->types != 0 || arg_info->no_named_args_stdarg_p;
11302 : :
11303 : 13096 : if (proto)
11304 : 35336589 : store_parm_decls_newstyle (fndecl, arg_info);
11305 : : else
11306 : 13096 : store_parm_decls_oldstyle (fndecl, arg_info);
11307 : :
11308 : : /* The next call to push_scope will be a function body. */
11309 : :
11310 : 35349685 : next_is_function_body = true;
11311 : :
11312 : : /* Write a record describing this function definition to the prototypes
11313 : : file (if requested). */
11314 : :
11315 : 35349685 : gen_aux_info_record (fndecl, 1, 0, proto);
11316 : :
11317 : : /* Initialize the RTL code for the function. */
11318 : 35349685 : allocate_struct_function (fndecl, false);
11319 : :
11320 : 35349685 : if (warn_unused_local_typedefs)
11321 : 3108729 : cfun->language = ggc_cleared_alloc<language_function> ();
11322 : :
11323 : : /* Begin the statement tree for this function. */
11324 : 35349685 : DECL_SAVED_TREE (fndecl) = push_stmt_list ();
11325 : :
11326 : : /* ??? Insert the contents of the pending sizes list into the function
11327 : : to be evaluated. The only reason left to have this is
11328 : : void foo(int n, int array[n++])
11329 : : because we throw away the array type in favor of a pointer type, and
11330 : : thus won't naturally see the SAVE_EXPR containing the increment. All
11331 : : other pending sizes would be handled by gimplify_parameters. */
11332 : 35349685 : if (arg_info->pending_sizes)
11333 : : {
11334 : : /* In very special circumstances, e.g. for code like
11335 : : _Atomic int i = 5;
11336 : : void f (int a[i += 2]) {}
11337 : : we need to execute the atomic assignment on function entry.
11338 : : But in this case, it is not just a straight store, it has the
11339 : : op= form, which means that build_atomic_assign has generated
11340 : : gotos, labels, etc. Because at that time the function decl
11341 : : for F has not been created yet, those labels do not have any
11342 : : function context. But we have the fndecl now, so update the
11343 : : labels accordingly. gimplify_expr would crash otherwise.
11344 : : Or with nested functions the labels could be created with parent
11345 : : function's context, while when the statement is emitted at the
11346 : : start of the nested function, it needs the nested function's
11347 : : context. */
11348 : 295 : walk_tree_without_duplicates (&arg_info->pending_sizes,
11349 : : set_labels_context_r, fndecl);
11350 : 295 : add_stmt (arg_info->pending_sizes);
11351 : : }
11352 : 35349685 : }
11353 : :
11354 : : /* Store PARM_DECLs in PARMS into scope temporarily. Used for
11355 : : c_finish_omp_declare_simd for function prototypes. No diagnostics
11356 : : should be done. */
11357 : :
11358 : : void
11359 : 238 : temp_store_parm_decls (tree fndecl, tree parms)
11360 : : {
11361 : 238 : push_scope ();
11362 : 674 : for (tree p = parms; p; p = DECL_CHAIN (p))
11363 : : {
11364 : 436 : DECL_CONTEXT (p) = fndecl;
11365 : 436 : if (DECL_NAME (p))
11366 : 317 : bind (DECL_NAME (p), p, current_scope,
11367 : : /*invisible=*/false, /*nested=*/false,
11368 : : UNKNOWN_LOCATION);
11369 : : }
11370 : 238 : }
11371 : :
11372 : : /* Undo what temp_store_parm_decls did. */
11373 : :
11374 : : void
11375 : 238 : temp_pop_parm_decls (void)
11376 : : {
11377 : : /* Clear all bindings in this temporary scope, so that
11378 : : pop_scope doesn't create a BLOCK. */
11379 : 238 : struct c_binding *b = current_scope->bindings;
11380 : 238 : current_scope->bindings = NULL;
11381 : 560 : for (; b; b = free_binding_and_advance (b))
11382 : : {
11383 : 322 : gcc_assert (TREE_CODE (b->decl) == PARM_DECL
11384 : : || b->decl == error_mark_node);
11385 : 322 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
11386 : 322 : I_SYMBOL_BINDING (b->id) = b->shadowed;
11387 : 322 : if (b->shadowed && b->shadowed->u.type)
11388 : 0 : TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
11389 : : }
11390 : 238 : pop_scope ();
11391 : 238 : }
11392 : :
11393 : :
11394 : : /* Finish up a function declaration and compile that function
11395 : : all the way to assembler language output. Then free the storage
11396 : : for the function definition.
11397 : :
11398 : : This is called after parsing the body of the function definition. */
11399 : :
11400 : : void
11401 : 35349683 : finish_function (location_t end_loc)
11402 : : {
11403 : 35349683 : tree fndecl = current_function_decl;
11404 : :
11405 : 35349683 : if (c_dialect_objc ())
11406 : 0 : objc_finish_function ();
11407 : :
11408 : 35349683 : if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
11409 : 35349683 : BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
11410 : :
11411 : : /* Must mark the RESULT_DECL as being in this function. */
11412 : :
11413 : 35349683 : if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
11414 : 35349683 : DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
11415 : :
11416 : 35397738 : if (MAIN_NAME_P (DECL_NAME (fndecl)) && !TREE_THIS_VOLATILE (fndecl)
11417 : 48054 : && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
11418 : 35397737 : == integer_type_node && flag_isoc99)
11419 : : {
11420 : : /* Hack. We don't want the middle-end to warn that this return
11421 : : is unreachable, so we mark its location as special. Using
11422 : : UNKNOWN_LOCATION has the problem that it gets clobbered in
11423 : : annotate_one_with_locus. A cleaner solution might be to
11424 : : ensure ! should_carry_locus_p (stmt), but that needs a flag.
11425 : : */
11426 : 46694 : c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
11427 : : }
11428 : :
11429 : : /* Tie off the statement tree for this function. */
11430 : 35349683 : DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
11431 : :
11432 : 35349683 : finish_fname_decls ();
11433 : :
11434 : : /* Complain if there's no return statement only if option specified on
11435 : : command line. */
11436 : 35349683 : if (warn_return_type > 0
11437 : 3108756 : && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
11438 : 2913179 : && !current_function_returns_value && !current_function_returns_null
11439 : : /* Don't complain if we are no-return. */
11440 : 72 : && !current_function_returns_abnormally
11441 : : /* Don't complain if we are declared noreturn. */
11442 : 21 : && !TREE_THIS_VOLATILE (fndecl)
11443 : : /* Don't warn for main(). */
11444 : 18 : && !MAIN_NAME_P (DECL_NAME (fndecl))
11445 : : /* Or if they didn't actually specify a return type. */
11446 : 17 : && !C_FUNCTION_IMPLICIT_INT (fndecl)
11447 : : /* Normally, with -Wreturn-type, flow will complain, but we might
11448 : : optimize out static functions. */
11449 : 17 : && !TREE_PUBLIC (fndecl)
11450 : 6 : && targetm.warn_func_return (fndecl)
11451 : 38458439 : && warning (OPT_Wreturn_type,
11452 : : "no return statement in function returning non-void"))
11453 : 5 : suppress_warning (fndecl, OPT_Wreturn_type);
11454 : :
11455 : : /* Complain about parameters that are only set, but never otherwise used. */
11456 : 35349683 : if (warn_unused_but_set_parameter)
11457 : : {
11458 : 3044900 : tree decl;
11459 : :
11460 : 3044900 : for (decl = DECL_ARGUMENTS (fndecl);
11461 : 11380351 : decl;
11462 : 8335451 : decl = DECL_CHAIN (decl))
11463 : 8335451 : if (TREE_USED (decl)
11464 : 8274748 : && TREE_CODE (decl) == PARM_DECL
11465 : 8274748 : && !DECL_READ_P (decl)
11466 : 1 : && DECL_NAME (decl)
11467 : 1 : && !DECL_ARTIFICIAL (decl)
11468 : 8335452 : && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter))
11469 : 1 : warning_at (DECL_SOURCE_LOCATION (decl),
11470 : 1 : OPT_Wunused_but_set_parameter,
11471 : : "parameter %qD set but not used", decl);
11472 : : }
11473 : :
11474 : : /* Complain about locally defined typedefs that are not used in this
11475 : : function. */
11476 : 35349683 : maybe_warn_unused_local_typedefs ();
11477 : :
11478 : : /* Possibly warn about unused parameters. */
11479 : 35349683 : if (warn_unused_parameter)
11480 : 2951019 : do_warn_unused_parameter (fndecl);
11481 : :
11482 : : /* Store the end of the function, so that we get good line number
11483 : : info for the epilogue. */
11484 : 35349683 : cfun->function_end_locus = end_loc;
11485 : :
11486 : : /* Finalize the ELF visibility for the function. */
11487 : 35349683 : c_determine_visibility (fndecl);
11488 : :
11489 : : /* For GNU C extern inline functions disregard inline limits. */
11490 : 35349683 : if (DECL_EXTERNAL (fndecl)
11491 : 34514840 : && DECL_DECLARED_INLINE_P (fndecl)
11492 : 69864520 : && (flag_gnu89_inline
11493 : 34483898 : || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
11494 : 34514370 : DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
11495 : :
11496 : : /* Genericize before inlining. Delay genericizing nested functions
11497 : : until their parent function is genericized. Since finalizing
11498 : : requires GENERIC, delay that as well. */
11499 : :
11500 : 70699366 : if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
11501 : 70699366 : && !undef_nested_function)
11502 : : {
11503 : 35349678 : if (!decl_function_context (fndecl))
11504 : : {
11505 : 35348148 : invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
11506 : 35348148 : c_genericize (fndecl);
11507 : :
11508 : : /* ??? Objc emits functions after finalizing the compilation unit.
11509 : : This should be cleaned up later and this conditional removed. */
11510 : 35348148 : if (symtab->global_info_ready)
11511 : : {
11512 : 0 : cgraph_node::add_new_function (fndecl, false);
11513 : 0 : return;
11514 : : }
11515 : 35348148 : cgraph_node::finalize_function (fndecl, false);
11516 : : }
11517 : : else
11518 : : {
11519 : : /* Register this function with cgraph just far enough to get it
11520 : : added to our parent's nested function list. Handy, since the
11521 : : C front end doesn't have such a list. */
11522 : 1530 : (void) cgraph_node::get_create (fndecl);
11523 : : }
11524 : : }
11525 : :
11526 : 35349683 : if (!decl_function_context (fndecl))
11527 : 35348153 : undef_nested_function = false;
11528 : :
11529 : 35349683 : if (cfun->language != NULL)
11530 : : {
11531 : 3108728 : ggc_free (cfun->language);
11532 : 3108728 : cfun->language = NULL;
11533 : : }
11534 : :
11535 : : /* We're leaving the context of this function, so zap cfun.
11536 : : It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
11537 : : tree_rest_of_compilation. */
11538 : 35349683 : set_cfun (NULL);
11539 : 35349683 : invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
11540 : 35349683 : current_function_decl = NULL;
11541 : : }
11542 : :
11543 : : /* Check the declarations given in a for-loop for satisfying the C99
11544 : : constraints. If exactly one such decl is found, return it. LOC is
11545 : : the location of the opening parenthesis of the for loop. The last
11546 : : parameter allows you to control the "for loop initial declarations
11547 : : are only allowed in C99 mode". Normally, you should pass
11548 : : flag_isoc99 as that parameter. But in some cases (Objective-C
11549 : : foreach loop, for example) we want to run the checks in this
11550 : : function even if not in C99 mode, so we allow the caller to turn
11551 : : off the error about not being in C99 mode.
11552 : : */
11553 : :
11554 : : tree
11555 : 24465 : check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
11556 : : {
11557 : 24465 : struct c_binding *b;
11558 : 24465 : tree one_decl = NULL_TREE;
11559 : 24465 : int n_decls = 0;
11560 : :
11561 : 24465 : if (!turn_off_iso_c99_error)
11562 : : {
11563 : 1 : static bool hint = true;
11564 : : /* If we get here, declarations have been used in a for loop without
11565 : : the C99 for loop scope. This doesn't make much sense, so don't
11566 : : allow it. */
11567 : 1 : auto_diagnostic_group d;
11568 : 1 : error_at (loc, "%<for%> loop initial declarations "
11569 : : "are only allowed in C99 or C11 mode");
11570 : 1 : if (hint)
11571 : : {
11572 : 1 : inform (loc,
11573 : : "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
11574 : : "%<-std=gnu11%> to compile your code");
11575 : 1 : hint = false;
11576 : : }
11577 : 1 : return NULL_TREE;
11578 : 1 : }
11579 : : else
11580 : 24464 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
11581 : : "initial declarations");
11582 : :
11583 : : /* C99 subclause 6.8.5 paragraph 3:
11584 : :
11585 : : [#3] The declaration part of a for statement shall only
11586 : : declare identifiers for objects having storage class auto or
11587 : : register.
11588 : :
11589 : : It isn't clear whether, in this sentence, "identifiers" binds to
11590 : : "shall only declare" or to "objects" - that is, whether all identifiers
11591 : : declared must be identifiers for objects, or whether the restriction
11592 : : only applies to those that are. (A question on this in comp.std.c
11593 : : in November 2000 received no answer.) We implement the strictest
11594 : : interpretation, to avoid creating an extension which later causes
11595 : : problems.
11596 : :
11597 : : This constraint was removed in C23. */
11598 : :
11599 : 49008 : for (b = current_scope->bindings; b; b = b->prev)
11600 : : {
11601 : 24544 : tree id = b->id;
11602 : 24544 : tree decl = b->decl;
11603 : :
11604 : 24544 : if (!id)
11605 : 27 : continue;
11606 : :
11607 : 24517 : switch (TREE_CODE (decl))
11608 : : {
11609 : 24469 : case VAR_DECL:
11610 : 24469 : {
11611 : 24469 : location_t decl_loc = DECL_SOURCE_LOCATION (decl);
11612 : 24469 : if (TREE_STATIC (decl))
11613 : 5 : pedwarn_c11 (decl_loc, OPT_Wpedantic,
11614 : : "declaration of static variable %qD in %<for%> "
11615 : : "loop initial declaration", decl);
11616 : 24464 : else if (DECL_EXTERNAL (decl))
11617 : 5 : pedwarn_c11 (decl_loc, OPT_Wpedantic,
11618 : : "declaration of %<extern%> variable %qD in %<for%> "
11619 : : "loop initial declaration", decl);
11620 : : }
11621 : : break;
11622 : :
11623 : 6 : case RECORD_TYPE:
11624 : 6 : pedwarn_c11 (loc, OPT_Wpedantic,
11625 : : "%<struct %E%> declared in %<for%> loop initial "
11626 : : "declaration", id);
11627 : 6 : break;
11628 : 5 : case UNION_TYPE:
11629 : 5 : pedwarn_c11 (loc, OPT_Wpedantic,
11630 : : "%<union %E%> declared in %<for%> loop initial "
11631 : : "declaration",
11632 : : id);
11633 : 5 : break;
11634 : 5 : case ENUMERAL_TYPE:
11635 : 5 : pedwarn_c11 (loc, OPT_Wpedantic,
11636 : : "%<enum %E%> declared in %<for%> loop "
11637 : : "initial declaration", id);
11638 : 5 : break;
11639 : 32 : default:
11640 : 32 : pedwarn_c11 (loc, OPT_Wpedantic, "declaration of non-variable "
11641 : : "%qD in %<for%> loop initial declaration", decl);
11642 : : }
11643 : :
11644 : 24517 : n_decls++;
11645 : 24517 : one_decl = decl;
11646 : : }
11647 : :
11648 : 24464 : return n_decls == 1 ? one_decl : NULL_TREE;
11649 : : }
11650 : :
11651 : : /* Save and reinitialize the variables
11652 : : used during compilation of a C function. */
11653 : :
11654 : : void
11655 : 1558 : c_push_function_context (void)
11656 : : {
11657 : 1558 : struct language_function *p = cfun->language;
11658 : : /* cfun->language might have been already allocated by the use of
11659 : : -Wunused-local-typedefs. In that case, just re-use it. */
11660 : 1558 : if (p == NULL)
11661 : 1502 : cfun->language = p = ggc_cleared_alloc<language_function> ();
11662 : :
11663 : 1558 : p->base.x_stmt_tree = c_stmt_tree;
11664 : 1558 : c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
11665 : 1558 : p->x_in_statement = in_statement;
11666 : 1558 : p->x_switch_stack = c_switch_stack;
11667 : 1558 : p->loop_names = loop_names;
11668 : 1558 : loop_names = vNULL;
11669 : 1558 : p->loop_names_hash = loop_names_hash;
11670 : 1558 : loop_names_hash = NULL;
11671 : 1558 : p->arg_info = current_function_arg_info;
11672 : 1558 : p->returns_value = current_function_returns_value;
11673 : 1558 : p->returns_null = current_function_returns_null;
11674 : 1558 : p->returns_abnormally = current_function_returns_abnormally;
11675 : 1558 : p->warn_about_return_type = warn_about_return_type;
11676 : :
11677 : 1558 : push_function_context ();
11678 : 1558 : }
11679 : :
11680 : : /* Restore the variables used during compilation of a C function. */
11681 : :
11682 : : void
11683 : 1558 : c_pop_function_context (void)
11684 : : {
11685 : 1558 : struct language_function *p;
11686 : :
11687 : 1558 : pop_function_context ();
11688 : 1558 : p = cfun->language;
11689 : :
11690 : : /* When -Wunused-local-typedefs is in effect, cfun->languages is
11691 : : used to store data throughout the life time of the current cfun,
11692 : : So don't deallocate it. */
11693 : 1558 : if (!warn_unused_local_typedefs)
11694 : 1502 : cfun->language = NULL;
11695 : :
11696 : 1558 : if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
11697 : 1558 : && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
11698 : : {
11699 : : /* Stop pointing to the local nodes about to be freed. */
11700 : : /* But DECL_INITIAL must remain nonzero so we know this
11701 : : was an actual function definition. */
11702 : 0 : DECL_INITIAL (current_function_decl) = error_mark_node;
11703 : 0 : DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
11704 : : }
11705 : :
11706 : 1558 : c_stmt_tree = p->base.x_stmt_tree;
11707 : 1558 : p->base.x_stmt_tree.x_cur_stmt_list = NULL;
11708 : 1558 : in_statement = p->x_in_statement;
11709 : 1558 : c_switch_stack = p->x_switch_stack;
11710 : 1558 : loop_names.release ();
11711 : 1558 : loop_names = p->loop_names;
11712 : 1558 : p->loop_names = vNULL;
11713 : 1558 : delete loop_names_hash;
11714 : 1558 : loop_names_hash = p->loop_names_hash;
11715 : 1558 : p->loop_names_hash = NULL;
11716 : 1558 : current_function_arg_info = p->arg_info;
11717 : 1558 : current_function_returns_value = p->returns_value;
11718 : 1558 : current_function_returns_null = p->returns_null;
11719 : 1558 : current_function_returns_abnormally = p->returns_abnormally;
11720 : 1558 : warn_about_return_type = p->warn_about_return_type;
11721 : 1558 : }
11722 : :
11723 : : /* The functions below are required for functionality of doing
11724 : : function at once processing in the C front end. Currently these
11725 : : functions are not called from anywhere in the C front end, but as
11726 : : these changes continue, that will change. */
11727 : :
11728 : : /* Returns the stmt_tree (if any) to which statements are currently
11729 : : being added. If there is no active statement-tree, NULL is
11730 : : returned. */
11731 : :
11732 : : stmt_tree
11733 : 959742572 : current_stmt_tree (void)
11734 : : {
11735 : 959742572 : return &c_stmt_tree;
11736 : : }
11737 : :
11738 : : /* Return the global value of T as a symbol. */
11739 : :
11740 : : tree
11741 : 3919753 : identifier_global_value (tree t)
11742 : : {
11743 : 3919753 : struct c_binding *b;
11744 : :
11745 : 3920718 : for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
11746 : 3913495 : if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11747 : 3912530 : return b->decl;
11748 : :
11749 : : return NULL_TREE;
11750 : : }
11751 : :
11752 : : /* Return the global value of tag T as a symbol. */
11753 : :
11754 : : tree
11755 : 12 : identifier_global_tag (tree t)
11756 : : {
11757 : 12 : struct c_binding *b;
11758 : :
11759 : 12 : for (b = I_TAG_BINDING (t); b; b = b->shadowed)
11760 : 11 : if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11761 : 11 : return b->decl;
11762 : :
11763 : : return NULL_TREE;
11764 : : }
11765 : :
11766 : : /* Returns non-zero (result of __has_builtin) if NAME refers to a built-in
11767 : : function or function-like operator. */
11768 : :
11769 : : int
11770 : 24500 : names_builtin_p (const char *name)
11771 : : {
11772 : 24500 : tree id = get_identifier (name);
11773 : 24500 : if (tree decl = identifier_global_value (id))
11774 : 24411 : return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_UNDECLARED_BUILTIN (decl);
11775 : :
11776 : : /* Also detect common reserved C words that aren't strictly built-in
11777 : : functions. */
11778 : 89 : switch (C_RID_CODE (id))
11779 : : {
11780 : : case RID_BUILTIN_ASSOC_BARRIER:
11781 : : case RID_BUILTIN_CONVERTVECTOR:
11782 : : case RID_BUILTIN_HAS_ATTRIBUTE:
11783 : : case RID_BUILTIN_SHUFFLE:
11784 : : case RID_BUILTIN_SHUFFLEVECTOR:
11785 : : case RID_BUILTIN_STDC:
11786 : : case RID_BUILTIN_COUNTED_BY_REF:
11787 : : case RID_CHOOSE_EXPR:
11788 : : case RID_OFFSETOF:
11789 : : case RID_TYPES_COMPATIBLE_P:
11790 : : case RID_C23_VA_START:
11791 : : case RID_VA_ARG:
11792 : : return 1;
11793 : 68 : default:
11794 : 68 : break;
11795 : : }
11796 : :
11797 : 68 : return 0;
11798 : : }
11799 : :
11800 : : /* In C, the only C-linkage public declaration is at file scope. */
11801 : :
11802 : : tree
11803 : 5 : c_linkage_bindings (tree name)
11804 : : {
11805 : 5 : return identifier_global_value (name);
11806 : : }
11807 : :
11808 : : /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
11809 : : otherwise the name is found in ridpointers from RID_INDEX. */
11810 : :
11811 : : void
11812 : 3329700 : record_builtin_type (enum rid rid_index, const char *name, tree type)
11813 : : {
11814 : 3329700 : tree id, decl;
11815 : 3329700 : if (name == 0)
11816 : 1553860 : id = ridpointers[(int) rid_index];
11817 : : else
11818 : 1775840 : id = get_identifier (name);
11819 : 3329700 : decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
11820 : 3329700 : pushdecl (decl);
11821 : 3329700 : if (debug_hooks->type_decl)
11822 : 3329700 : debug_hooks->type_decl (decl, false);
11823 : 3329700 : }
11824 : :
11825 : : /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
11826 : :
11827 : : struct c_parm *
11828 : 120162295 : build_c_parm (struct c_declspecs *specs, tree attrs,
11829 : : struct c_declarator *declarator,
11830 : : location_t loc)
11831 : : {
11832 : 120162295 : struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
11833 : 120162295 : ret->specs = specs;
11834 : 120162295 : ret->attrs = attrs;
11835 : 120162295 : ret->declarator = declarator;
11836 : 120162295 : ret->loc = loc;
11837 : 120162295 : return ret;
11838 : : }
11839 : :
11840 : : /* Return a declarator with nested attributes. TARGET is the inner
11841 : : declarator to which these attributes apply. ATTRS are the
11842 : : attributes. */
11843 : :
11844 : : struct c_declarator *
11845 : 6654 : build_attrs_declarator (tree attrs, struct c_declarator *target)
11846 : : {
11847 : 6654 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11848 : 6654 : ret->kind = cdk_attrs;
11849 : 6654 : ret->declarator = target;
11850 : 6654 : ret->u.attrs = attrs;
11851 : 6654 : return ret;
11852 : : }
11853 : :
11854 : : /* Return a declarator for a function with arguments specified by ARGS
11855 : : and return type specified by TARGET. */
11856 : :
11857 : : struct c_declarator *
11858 : 48828639 : build_function_declarator (struct c_arg_info *args,
11859 : : struct c_declarator *target)
11860 : : {
11861 : 48828639 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11862 : 48828639 : ret->kind = cdk_function;
11863 : 48828639 : ret->declarator = target;
11864 : 48828639 : ret->u.arg_info = args;
11865 : 48828639 : return ret;
11866 : : }
11867 : :
11868 : : /* Return a declarator for the identifier IDENT (which may be
11869 : : NULL_TREE for an abstract declarator). */
11870 : :
11871 : : struct c_declarator *
11872 : 304813664 : build_id_declarator (tree ident)
11873 : : {
11874 : 304813664 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11875 : 304813664 : ret->kind = cdk_id;
11876 : 304813664 : ret->declarator = 0;
11877 : 304813664 : ret->u.id.id = ident;
11878 : 304813664 : ret->u.id.attrs = NULL_TREE;
11879 : : /* Default value - may get reset to a more precise location. */
11880 : 304813664 : ret->id_loc = input_location;
11881 : 304813664 : return ret;
11882 : : }
11883 : :
11884 : : /* Return something to represent absolute declarators containing a *.
11885 : : TARGET is the absolute declarator that the * contains.
11886 : : TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
11887 : : to apply to the pointer type. */
11888 : :
11889 : : struct c_declarator *
11890 : 17875768 : make_pointer_declarator (struct c_declspecs *type_quals_attrs,
11891 : : struct c_declarator *target)
11892 : : {
11893 : 17875768 : tree attrs;
11894 : 17875768 : int quals = 0;
11895 : 17875768 : struct c_declarator *itarget = target;
11896 : 17875768 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11897 : 17875768 : if (type_quals_attrs)
11898 : : {
11899 : 17875768 : attrs = type_quals_attrs->attrs;
11900 : 17875768 : quals = quals_from_declspecs (type_quals_attrs);
11901 : 17875768 : if (attrs != NULL_TREE)
11902 : 6380 : itarget = build_attrs_declarator (attrs, target);
11903 : : }
11904 : 17875768 : ret->kind = cdk_pointer;
11905 : 17875768 : ret->declarator = itarget;
11906 : 17875768 : ret->u.pointer_quals = quals;
11907 : 17875768 : return ret;
11908 : : }
11909 : :
11910 : : /* Return a pointer to a structure for an empty list of declaration
11911 : : specifiers. */
11912 : :
11913 : : struct c_declspecs *
11914 : 442061611 : build_null_declspecs (void)
11915 : : {
11916 : 442061611 : struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
11917 : 442061611 : memset (ret, 0, sizeof *ret);
11918 : 442061611 : ret->align_log = -1;
11919 : 442061611 : ret->typespec_word = cts_none;
11920 : 442061611 : ret->storage_class = csc_none;
11921 : 442061611 : ret->expr_const_operands = true;
11922 : 442061611 : ret->typespec_kind = ctsk_none;
11923 : 442061611 : ret->address_space = ADDR_SPACE_GENERIC;
11924 : 442061611 : return ret;
11925 : : }
11926 : :
11927 : : /* Add the address space ADDRSPACE to the declaration specifiers
11928 : : SPECS, returning SPECS. */
11929 : :
11930 : : struct c_declspecs *
11931 : 174 : declspecs_add_addrspace (location_t location,
11932 : : struct c_declspecs *specs, addr_space_t as)
11933 : : {
11934 : 174 : specs->non_sc_seen_p = true;
11935 : 174 : specs->declspecs_seen_p = true;
11936 : 174 : specs->non_std_attrs_seen_p = true;
11937 : :
11938 : 174 : if (!ADDR_SPACE_GENERIC_P (specs->address_space)
11939 : 0 : && specs->address_space != as)
11940 : 0 : error ("incompatible address space qualifiers %qs and %qs",
11941 : : c_addr_space_name (as),
11942 : : c_addr_space_name (specs->address_space));
11943 : : else
11944 : : {
11945 : 174 : specs->address_space = as;
11946 : 174 : specs->locations[cdw_address_space] = location;
11947 : : }
11948 : 174 : return specs;
11949 : : }
11950 : :
11951 : : /* Add the type qualifier QUAL to the declaration specifiers SPECS,
11952 : : returning SPECS. */
11953 : :
11954 : : struct c_declspecs *
11955 : 16234298 : declspecs_add_qual (location_t loc,
11956 : : struct c_declspecs *specs, tree qual)
11957 : : {
11958 : 16234298 : enum rid i;
11959 : 16234298 : bool dupe = false;
11960 : 16234298 : specs->non_sc_seen_p = true;
11961 : 16234298 : specs->declspecs_seen_p = true;
11962 : 16234298 : specs->non_std_attrs_seen_p = true;
11963 : 16234298 : gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
11964 : : && C_IS_RESERVED_WORD (qual));
11965 : 16234298 : i = C_RID_CODE (qual);
11966 : 16234298 : location_t prev_loc = UNKNOWN_LOCATION;
11967 : 16234298 : switch (i)
11968 : : {
11969 : 12792637 : case RID_CONST:
11970 : 12792637 : dupe = specs->const_p;
11971 : 12792637 : specs->const_p = true;
11972 : 12792637 : prev_loc = specs->locations[cdw_const];
11973 : 12792637 : specs->locations[cdw_const] = loc;
11974 : 12792637 : break;
11975 : 95258 : case RID_VOLATILE:
11976 : 95258 : dupe = specs->volatile_p;
11977 : 95258 : specs->volatile_p = true;
11978 : 95258 : prev_loc = specs->locations[cdw_volatile];
11979 : 95258 : specs->locations[cdw_volatile] = loc;
11980 : 95258 : break;
11981 : 3327533 : case RID_RESTRICT:
11982 : 3327533 : dupe = specs->restrict_p;
11983 : 3327533 : specs->restrict_p = true;
11984 : 3327533 : prev_loc = specs->locations[cdw_restrict];
11985 : 3327533 : specs->locations[cdw_restrict] = loc;
11986 : 3327533 : break;
11987 : 18870 : case RID_ATOMIC:
11988 : 18870 : dupe = specs->atomic_p;
11989 : 18870 : specs->atomic_p = true;
11990 : 18870 : prev_loc = specs->locations[cdw_atomic];
11991 : 18870 : specs->locations[cdw_atomic] = loc;
11992 : 18870 : break;
11993 : 0 : default:
11994 : 0 : gcc_unreachable ();
11995 : : }
11996 : 16234298 : if (dupe)
11997 : : {
11998 : 56 : bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
11999 : : "duplicate %qE declaration specifier", qual);
12000 : 56 : if (!warned
12001 : 52 : && warn_duplicate_decl_specifier
12002 : 33 : && prev_loc >= RESERVED_LOCATION_COUNT
12003 : 33 : && !from_macro_expansion_at (prev_loc)
12004 : 77 : && !from_macro_expansion_at (loc))
12005 : 12 : warning_at (loc, OPT_Wduplicate_decl_specifier,
12006 : : "duplicate %qE declaration specifier", qual);
12007 : : }
12008 : 16234298 : return specs;
12009 : : }
12010 : :
12011 : : /* Add the type specifier TYPE to the declaration specifiers SPECS,
12012 : : returning SPECS. */
12013 : :
12014 : : struct c_declspecs *
12015 : 321459244 : declspecs_add_type (location_t loc, struct c_declspecs *specs,
12016 : : struct c_typespec spec)
12017 : : {
12018 : 321459244 : tree type = spec.spec;
12019 : 321459244 : specs->non_sc_seen_p = true;
12020 : 321459244 : specs->declspecs_seen_p = true;
12021 : 321459244 : specs->non_std_attrs_seen_p = true;
12022 : 321459244 : specs->typespec_kind = spec.kind;
12023 : 321459244 : if (TREE_DEPRECATED (type))
12024 : 56 : specs->deprecated_p = true;
12025 : 321459244 : if (TREE_UNAVAILABLE (type))
12026 : 40 : specs->unavailable_p = true;
12027 : :
12028 : : /* As a type specifier is present, "auto" must be used as a storage
12029 : : class specifier, not for type deduction. */
12030 : 321459244 : if (specs->c23_auto_p)
12031 : : {
12032 : 112 : specs->c23_auto_p = false;
12033 : 112 : if (specs->storage_class != csc_none)
12034 : 1 : error ("multiple storage classes in declaration specifiers");
12035 : 111 : else if (specs->thread_p)
12036 : 1 : error ("%qs used with %<auto%>",
12037 : 1 : specs->thread_gnu_p ? "__thread" : "_Thread_local");
12038 : 110 : else if (specs->constexpr_p)
12039 : : /* auto may only be used with another storage class specifier,
12040 : : such as constexpr, if the type is inferred. */
12041 : 2 : error ("%<auto%> used with %<constexpr%>");
12042 : : else
12043 : 108 : specs->storage_class = csc_auto;
12044 : : }
12045 : :
12046 : : /* Handle type specifier keywords. */
12047 : 321459244 : if (TREE_CODE (type) == IDENTIFIER_NODE
12048 : 78907101 : && C_IS_RESERVED_WORD (type)
12049 : 400366345 : && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
12050 : : {
12051 : 78907101 : enum rid i = C_RID_CODE (type);
12052 : 78907101 : if (specs->type)
12053 : : {
12054 : 58 : error_at (loc, "two or more data types in declaration specifiers");
12055 : 58 : return specs;
12056 : : }
12057 : 78907043 : if ((int) i <= (int) RID_LAST_MODIFIER)
12058 : : {
12059 : : /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
12060 : 20723144 : bool dupe = false;
12061 : 20723144 : switch (i)
12062 : : {
12063 : 9962611 : case RID_LONG:
12064 : 9962611 : if (specs->long_long_p)
12065 : : {
12066 : 104 : error_at (loc, "%<long long long%> is too long for GCC");
12067 : 104 : break;
12068 : : }
12069 : 9962507 : if (specs->long_p)
12070 : : {
12071 : 2699225 : if (specs->typespec_word == cts_double)
12072 : : {
12073 : 15 : error_at (loc,
12074 : : ("both %<long long%> and %<double%> in "
12075 : : "declaration specifiers"));
12076 : 15 : break;
12077 : : }
12078 : 2699210 : pedwarn_c90 (loc, OPT_Wlong_long,
12079 : : "ISO C90 does not support %<long long%>");
12080 : 2699210 : specs->long_long_p = 1;
12081 : 2699210 : specs->locations[cdw_long_long] = loc;
12082 : 2699210 : break;
12083 : : }
12084 : 7263282 : if (specs->short_p)
12085 : 77 : error_at (loc,
12086 : : ("both %<long%> and %<short%> in "
12087 : : "declaration specifiers"));
12088 : 7263205 : else if (specs->typespec_word == cts_auto_type)
12089 : 1 : error_at (loc,
12090 : : ("both %<long%> and %<__auto_type%> in "
12091 : : "declaration specifiers"));
12092 : : else if (specs->typespec_word == cts_void)
12093 : 5 : error_at (loc,
12094 : : ("both %<long%> and %<void%> in "
12095 : : "declaration specifiers"));
12096 : : else if (specs->typespec_word == cts_int_n)
12097 : 19 : error_at (loc,
12098 : : ("both %<long%> and %<__int%d%> in "
12099 : : "declaration specifiers"),
12100 : 19 : int_n_data[specs->u.int_n_idx].bitsize);
12101 : : else if (specs->typespec_word == cts_bool)
12102 : 3 : error_at (loc,
12103 : : ("both %<long%> and %<_Bool%> in "
12104 : : "declaration specifiers"));
12105 : : else if (specs->typespec_word == cts_bitint)
12106 : 3 : error_at (loc,
12107 : : ("both %<long%> and %<_BitInt%> in "
12108 : : "declaration specifiers"));
12109 : : else if (specs->typespec_word == cts_char)
12110 : 21 : error_at (loc,
12111 : : ("both %<long%> and %<char%> in "
12112 : : "declaration specifiers"));
12113 : : else if (specs->typespec_word == cts_float)
12114 : 7 : error_at (loc,
12115 : : ("both %<long%> and %<float%> in "
12116 : : "declaration specifiers"));
12117 : : else if (specs->typespec_word == cts_floatn_nx)
12118 : 0 : error_at (loc,
12119 : : ("both %<long%> and %<_Float%d%s%> in "
12120 : : "declaration specifiers"),
12121 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12122 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12123 : : ? "x"
12124 : : : ""));
12125 : : else if (specs->typespec_word == cts_dfloat32)
12126 : 4 : error_at (loc,
12127 : : ("both %<long%> and %<_Decimal32%> in "
12128 : : "declaration specifiers"));
12129 : : else if (specs->typespec_word == cts_dfloat64)
12130 : 4 : error_at (loc,
12131 : : ("both %<long%> and %<_Decimal64%> in "
12132 : : "declaration specifiers"));
12133 : : else if (specs->typespec_word == cts_dfloat128)
12134 : 4 : error_at (loc,
12135 : : ("both %<long%> and %<_Decimal128%> in "
12136 : : "declaration specifiers"));
12137 : : else if (specs->typespec_word == cts_dfloat64x)
12138 : 0 : error_at (loc,
12139 : : ("both %<long%> and %<_Decimal64x%> in "
12140 : : "declaration specifiers"));
12141 : : else
12142 : : {
12143 : 7263134 : specs->long_p = true;
12144 : 7263134 : specs->locations[cdw_long] = loc;
12145 : : }
12146 : : break;
12147 : 1662723 : case RID_SHORT:
12148 : 1662723 : dupe = specs->short_p;
12149 : 1662723 : if (specs->long_p)
12150 : 197 : error_at (loc,
12151 : : ("both %<long%> and %<short%> in "
12152 : : "declaration specifiers"));
12153 : 1662526 : else if (specs->typespec_word == cts_auto_type)
12154 : 1 : error_at (loc,
12155 : : ("both %<short%> and %<__auto_type%> in "
12156 : : "declaration specifiers"));
12157 : : else if (specs->typespec_word == cts_void)
12158 : 5 : error_at (loc,
12159 : : ("both %<short%> and %<void%> in "
12160 : : "declaration specifiers"));
12161 : : else if (specs->typespec_word == cts_int_n)
12162 : 19 : error_at (loc,
12163 : : ("both %<short%> and %<__int%d%> in "
12164 : : "declaration specifiers"),
12165 : 19 : int_n_data[specs->u.int_n_idx].bitsize);
12166 : : else if (specs->typespec_word == cts_bool)
12167 : 3 : error_at (loc,
12168 : : ("both %<short%> and %<_Bool%> in "
12169 : : "declaration specifiers"));
12170 : : else if (specs->typespec_word == cts_bitint)
12171 : 1 : error_at (loc,
12172 : : ("both %<short%> and %<_BitInt%> in "
12173 : : "declaration specifiers"));
12174 : : else if (specs->typespec_word == cts_char)
12175 : 21 : error_at (loc,
12176 : : ("both %<short%> and %<char%> in "
12177 : : "declaration specifiers"));
12178 : : else if (specs->typespec_word == cts_float)
12179 : 7 : error_at (loc,
12180 : : ("both %<short%> and %<float%> in "
12181 : : "declaration specifiers"));
12182 : : else if (specs->typespec_word == cts_double)
12183 : 7 : error_at (loc,
12184 : : ("both %<short%> and %<double%> in "
12185 : : "declaration specifiers"));
12186 : : else if (specs->typespec_word == cts_floatn_nx)
12187 : 0 : error_at (loc,
12188 : : ("both %<short%> and %<_Float%d%s%> in "
12189 : : "declaration specifiers"),
12190 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12191 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12192 : : ? "x"
12193 : : : ""));
12194 : : else if (specs->typespec_word == cts_dfloat32)
12195 : 4 : error_at (loc,
12196 : : ("both %<short%> and %<_Decimal32%> in "
12197 : : "declaration specifiers"));
12198 : : else if (specs->typespec_word == cts_dfloat64)
12199 : 4 : error_at (loc,
12200 : : ("both %<short%> and %<_Decimal64%> in "
12201 : : "declaration specifiers"));
12202 : : else if (specs->typespec_word == cts_dfloat128)
12203 : 4 : error_at (loc,
12204 : : ("both %<short%> and %<_Decimal128%> in "
12205 : : "declaration specifiers"));
12206 : : else if (specs->typespec_word == cts_dfloat64x)
12207 : 0 : error_at (loc,
12208 : : ("both %<short%> and %<_Decimal64x%> in "
12209 : : "declaration specifiers"));
12210 : : else
12211 : : {
12212 : 1662450 : specs->short_p = true;
12213 : 1662450 : specs->locations[cdw_short] = loc;
12214 : : }
12215 : : break;
12216 : 568745 : case RID_SIGNED:
12217 : 568745 : dupe = specs->signed_p;
12218 : 568745 : if (specs->unsigned_p)
12219 : 138 : error_at (loc,
12220 : : ("both %<signed%> and %<unsigned%> in "
12221 : : "declaration specifiers"));
12222 : 568607 : else if (specs->typespec_word == cts_auto_type)
12223 : 1 : error_at (loc,
12224 : : ("both %<signed%> and %<__auto_type%> in "
12225 : : "declaration specifiers"));
12226 : : else if (specs->typespec_word == cts_void)
12227 : 5 : error_at (loc,
12228 : : ("both %<signed%> and %<void%> in "
12229 : : "declaration specifiers"));
12230 : : else if (specs->typespec_word == cts_bool)
12231 : 3 : error_at (loc,
12232 : : ("both %<signed%> and %<_Bool%> in "
12233 : : "declaration specifiers"));
12234 : : else if (specs->typespec_word == cts_float)
12235 : 7 : error_at (loc,
12236 : : ("both %<signed%> and %<float%> in "
12237 : : "declaration specifiers"));
12238 : : else if (specs->typespec_word == cts_double)
12239 : 21 : error_at (loc,
12240 : : ("both %<signed%> and %<double%> in "
12241 : : "declaration specifiers"));
12242 : : else if (specs->typespec_word == cts_floatn_nx)
12243 : 0 : error_at (loc,
12244 : : ("both %<signed%> and %<_Float%d%s%> in "
12245 : : "declaration specifiers"),
12246 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12247 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12248 : : ? "x"
12249 : : : ""));
12250 : : else if (specs->typespec_word == cts_dfloat32)
12251 : 4 : error_at (loc,
12252 : : ("both %<signed%> and %<_Decimal32%> in "
12253 : : "declaration specifiers"));
12254 : : else if (specs->typespec_word == cts_dfloat64)
12255 : 4 : error_at (loc,
12256 : : ("both %<signed%> and %<_Decimal64%> in "
12257 : : "declaration specifiers"));
12258 : : else if (specs->typespec_word == cts_dfloat128)
12259 : 4 : error_at (loc,
12260 : : ("both %<signed%> and %<_Decimal128%> in "
12261 : : "declaration specifiers"));
12262 : : else if (specs->typespec_word == cts_dfloat64x)
12263 : 0 : error_at (loc,
12264 : : ("both %<signed%> and %<_Decimal64x%> in "
12265 : : "declaration specifiers"));
12266 : : else
12267 : : {
12268 : 568558 : specs->signed_p = true;
12269 : 568558 : specs->locations[cdw_signed] = loc;
12270 : : }
12271 : : break;
12272 : 5982000 : case RID_UNSIGNED:
12273 : 5982000 : dupe = specs->unsigned_p;
12274 : 5982000 : if (specs->signed_p)
12275 : 139 : error_at (loc,
12276 : : ("both %<signed%> and %<unsigned%> in "
12277 : : "declaration specifiers"));
12278 : 5981861 : else if (specs->typespec_word == cts_auto_type)
12279 : 1 : error_at (loc,
12280 : : ("both %<unsigned%> and %<__auto_type%> in "
12281 : : "declaration specifiers"));
12282 : : else if (specs->typespec_word == cts_void)
12283 : 5 : error_at (loc,
12284 : : ("both %<unsigned%> and %<void%> in "
12285 : : "declaration specifiers"));
12286 : : else if (specs->typespec_word == cts_bool)
12287 : 3 : error_at (loc,
12288 : : ("both %<unsigned%> and %<_Bool%> in "
12289 : : "declaration specifiers"));
12290 : : else if (specs->typespec_word == cts_float)
12291 : 7 : error_at (loc,
12292 : : ("both %<unsigned%> and %<float%> in "
12293 : : "declaration specifiers"));
12294 : : else if (specs->typespec_word == cts_double)
12295 : 21 : error_at (loc,
12296 : : ("both %<unsigned%> and %<double%> in "
12297 : : "declaration specifiers"));
12298 : : else if (specs->typespec_word == cts_floatn_nx)
12299 : 0 : error_at (loc,
12300 : : ("both %<unsigned%> and %<_Float%d%s%> in "
12301 : : "declaration specifiers"),
12302 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12303 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12304 : : ? "x"
12305 : : : ""));
12306 : : else if (specs->typespec_word == cts_dfloat32)
12307 : 2 : error_at (loc,
12308 : : ("both %<unsigned%> and %<_Decimal32%> in "
12309 : : "declaration specifiers"));
12310 : : else if (specs->typespec_word == cts_dfloat64)
12311 : 2 : error_at (loc,
12312 : : ("both %<unsigned%> and %<_Decimal64%> in "
12313 : : "declaration specifiers"));
12314 : : else if (specs->typespec_word == cts_dfloat128)
12315 : 2 : error_at (loc,
12316 : : ("both %<unsigned%> and %<_Decimal128%> in "
12317 : : "declaration specifiers"));
12318 : : else if (specs->typespec_word == cts_dfloat64x)
12319 : 0 : error_at (loc,
12320 : : ("both %<unsigned%> and %<_Decimal64x%> in "
12321 : : "declaration specifiers"));
12322 : : else
12323 : : {
12324 : 5981818 : specs->unsigned_p = true;
12325 : 5981818 : specs->locations[cdw_unsigned] = loc;
12326 : : }
12327 : : break;
12328 : 2546946 : case RID_COMPLEX:
12329 : 2546946 : dupe = specs->complex_p;
12330 : 2546946 : if (!in_system_header_at (loc))
12331 : 68930 : pedwarn_c90 (loc, OPT_Wpedantic,
12332 : : "ISO C90 does not support complex types");
12333 : 2546946 : if (specs->typespec_word == cts_auto_type)
12334 : 1 : error_at (loc,
12335 : : ("both %<complex%> and %<__auto_type%> in "
12336 : : "declaration specifiers"));
12337 : : else if (specs->typespec_word == cts_void)
12338 : 2 : error_at (loc,
12339 : : ("both %<complex%> and %<void%> in "
12340 : : "declaration specifiers"));
12341 : : else if (specs->typespec_word == cts_bool)
12342 : 2 : error_at (loc,
12343 : : ("both %<complex%> and %<_Bool%> in "
12344 : : "declaration specifiers"));
12345 : : else if (specs->typespec_word == cts_bitint)
12346 : 1 : error_at (loc,
12347 : : ("both %<complex%> and %<_BitInt%> in "
12348 : : "declaration specifiers"));
12349 : : else if (specs->typespec_word == cts_dfloat32)
12350 : 1 : error_at (loc,
12351 : : ("both %<complex%> and %<_Decimal32%> in "
12352 : : "declaration specifiers"));
12353 : : else if (specs->typespec_word == cts_dfloat64)
12354 : 1 : error_at (loc,
12355 : : ("both %<complex%> and %<_Decimal64%> in "
12356 : : "declaration specifiers"));
12357 : : else if (specs->typespec_word == cts_dfloat128)
12358 : 1 : error_at (loc,
12359 : : ("both %<complex%> and %<_Decimal128%> in "
12360 : : "declaration specifiers"));
12361 : : else if (specs->typespec_word == cts_dfloat64x)
12362 : 0 : error_at (loc,
12363 : : ("both %<complex%> and %<_Decimal64x%> in "
12364 : : "declaration specifiers"));
12365 : : else if (specs->typespec_word == cts_fract)
12366 : 0 : error_at (loc,
12367 : : ("both %<complex%> and %<_Fract%> in "
12368 : : "declaration specifiers"));
12369 : : else if (specs->typespec_word == cts_accum)
12370 : 0 : error_at (loc,
12371 : : ("both %<complex%> and %<_Accum%> in "
12372 : : "declaration specifiers"));
12373 : 2546937 : else if (specs->saturating_p)
12374 : 0 : error_at (loc,
12375 : : ("both %<complex%> and %<_Sat%> in "
12376 : : "declaration specifiers"));
12377 : : else
12378 : : {
12379 : 2546937 : specs->complex_p = true;
12380 : 2546937 : specs->locations[cdw_complex] = loc;
12381 : : }
12382 : : break;
12383 : 119 : case RID_SAT:
12384 : 119 : dupe = specs->saturating_p;
12385 : 119 : pedwarn (loc, OPT_Wpedantic,
12386 : : "ISO C does not support saturating types");
12387 : 119 : if (specs->typespec_word == cts_int_n)
12388 : : {
12389 : 0 : error_at (loc,
12390 : : ("both %<_Sat%> and %<__int%d%> in "
12391 : : "declaration specifiers"),
12392 : 0 : int_n_data[specs->u.int_n_idx].bitsize);
12393 : : }
12394 : : else if (specs->typespec_word == cts_auto_type)
12395 : 0 : error_at (loc,
12396 : : ("both %<_Sat%> and %<__auto_type%> in "
12397 : : "declaration specifiers"));
12398 : : else if (specs->typespec_word == cts_void)
12399 : 0 : error_at (loc,
12400 : : ("both %<_Sat%> and %<void%> in "
12401 : : "declaration specifiers"));
12402 : : else if (specs->typespec_word == cts_bool)
12403 : 0 : error_at (loc,
12404 : : ("both %<_Sat%> and %<_Bool%> in "
12405 : : "declaration specifiers"));
12406 : : else if (specs->typespec_word == cts_bitint)
12407 : 0 : error_at (loc,
12408 : : ("both %<_Sat%> and %<_BitInt%> in "
12409 : : "declaration specifiers"));
12410 : : else if (specs->typespec_word == cts_char)
12411 : 0 : error_at (loc,
12412 : : ("both %<_Sat%> and %<char%> in "
12413 : : "declaration specifiers"));
12414 : : else if (specs->typespec_word == cts_int)
12415 : 0 : error_at (loc,
12416 : : ("both %<_Sat%> and %<int%> in "
12417 : : "declaration specifiers"));
12418 : : else if (specs->typespec_word == cts_float)
12419 : 0 : error_at (loc,
12420 : : ("both %<_Sat%> and %<float%> in "
12421 : : "declaration specifiers"));
12422 : : else if (specs->typespec_word == cts_double)
12423 : 0 : error_at (loc,
12424 : : ("both %<_Sat%> and %<double%> in "
12425 : : "declaration specifiers"));
12426 : : else if (specs->typespec_word == cts_floatn_nx)
12427 : 0 : error_at (loc,
12428 : : ("both %<_Sat%> and %<_Float%d%s%> in "
12429 : : "declaration specifiers"),
12430 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12431 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12432 : : ? "x"
12433 : : : ""));
12434 : : else if (specs->typespec_word == cts_dfloat32)
12435 : 0 : error_at (loc,
12436 : : ("both %<_Sat%> and %<_Decimal32%> in "
12437 : : "declaration specifiers"));
12438 : : else if (specs->typespec_word == cts_dfloat64)
12439 : 0 : error_at (loc,
12440 : : ("both %<_Sat%> and %<_Decimal64%> in "
12441 : : "declaration specifiers"));
12442 : : else if (specs->typespec_word == cts_dfloat128)
12443 : 0 : error_at (loc,
12444 : : ("both %<_Sat%> and %<_Decimal128%> in "
12445 : : "declaration specifiers"));
12446 : : else if (specs->typespec_word == cts_dfloat64x)
12447 : 0 : error_at (loc,
12448 : : ("both %<_Sat%> and %<_Decimal64x%> in "
12449 : : "declaration specifiers"));
12450 : 119 : else if (specs->complex_p)
12451 : 0 : error_at (loc,
12452 : : ("both %<_Sat%> and %<complex%> in "
12453 : : "declaration specifiers"));
12454 : : else
12455 : : {
12456 : 119 : specs->saturating_p = true;
12457 : 119 : specs->locations[cdw_saturating] = loc;
12458 : : }
12459 : : break;
12460 : 0 : default:
12461 : 0 : gcc_unreachable ();
12462 : : }
12463 : :
12464 : 20723144 : if (dupe)
12465 : 378 : error_at (loc, "duplicate %qE", type);
12466 : :
12467 : 20723144 : return specs;
12468 : : }
12469 : : else
12470 : : {
12471 : : /* "void", "_Bool", "char", "int", "float", "double",
12472 : : "_FloatN", "_FloatNx", "_Decimal32", "__intN",
12473 : : "_Decimal64", "_Decimal128", "_Fract", "_Accum", "_BitInt(N)" or
12474 : : "__auto_type". */
12475 : 58183899 : if (specs->typespec_word != cts_none)
12476 : : {
12477 : 2171 : if (i == RID_BOOL)
12478 : : {
12479 : 175 : auto_diagnostic_group d;
12480 : 175 : if (specs->storage_class == csc_typedef)
12481 : 4 : error_at (loc,
12482 : : "%qs cannot be defined via %<typedef%>",
12483 : 2 : IDENTIFIER_POINTER (type));
12484 : : else
12485 : 346 : error_at (loc,
12486 : : "%qs cannot be used here",
12487 : 173 : IDENTIFIER_POINTER (type));
12488 : 175 : add_note_about_new_keyword (loc, type);
12489 : 175 : }
12490 : : else
12491 : 1996 : error_at (loc,
12492 : : "two or more data types in declaration specifiers");
12493 : 2171 : return specs;
12494 : : }
12495 : 58181728 : switch (i)
12496 : : {
12497 : 1838 : case RID_AUTO_TYPE:
12498 : 1838 : if (specs->long_p)
12499 : 1 : error_at (loc,
12500 : : ("both %<long%> and %<__auto_type%> in "
12501 : : "declaration specifiers"));
12502 : 1837 : else if (specs->short_p)
12503 : 1 : error_at (loc,
12504 : : ("both %<short%> and %<__auto_type%> in "
12505 : : "declaration specifiers"));
12506 : 1836 : else if (specs->signed_p)
12507 : 1 : error_at (loc,
12508 : : ("both %<signed%> and %<__auto_type%> in "
12509 : : "declaration specifiers"));
12510 : 1835 : else if (specs->unsigned_p)
12511 : 1 : error_at (loc,
12512 : : ("both %<unsigned%> and %<__auto_type%> in "
12513 : : "declaration specifiers"));
12514 : 1834 : else if (specs->complex_p)
12515 : 1 : error_at (loc,
12516 : : ("both %<complex%> and %<__auto_type%> in "
12517 : : "declaration specifiers"));
12518 : 1833 : else if (specs->saturating_p)
12519 : 0 : error_at (loc,
12520 : : ("both %<_Sat%> and %<__auto_type%> in "
12521 : : "declaration specifiers"));
12522 : : else
12523 : : {
12524 : 1833 : specs->typespec_word = cts_auto_type;
12525 : 1833 : specs->locations[cdw_typespec] = loc;
12526 : : }
12527 : 1838 : return specs;
12528 : 51334 : case RID_INT_N_0:
12529 : 51334 : case RID_INT_N_1:
12530 : 51334 : case RID_INT_N_2:
12531 : 51334 : case RID_INT_N_3:
12532 : 51334 : specs->u.int_n_idx = i - RID_INT_N_0;
12533 : 51334 : if (!in_system_header_at (input_location)
12534 : : /* If the INT_N type ends in "__", and so is of the format
12535 : : "__intN__", don't pedwarn. */
12536 : 51334 : && (strncmp (IDENTIFIER_POINTER (type)
12537 : 40938 : + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
12538 : 40938 : pedwarn (loc, OPT_Wpedantic,
12539 : : "ISO C does not support %<__int%d%> types",
12540 : 40938 : int_n_data[specs->u.int_n_idx].bitsize);
12541 : :
12542 : 51334 : if (specs->long_p)
12543 : 53 : error_at (loc,
12544 : : ("both %<__int%d%> and %<long%> in "
12545 : : "declaration specifiers"),
12546 : 53 : int_n_data[specs->u.int_n_idx].bitsize);
12547 : 51281 : else if (specs->saturating_p)
12548 : 0 : error_at (loc,
12549 : : ("both %<_Sat%> and %<__int%d%> in "
12550 : : "declaration specifiers"),
12551 : 0 : int_n_data[specs->u.int_n_idx].bitsize);
12552 : 51281 : else if (specs->short_p)
12553 : 19 : error_at (loc,
12554 : : ("both %<__int%d%> and %<short%> in "
12555 : : "declaration specifiers"),
12556 : 19 : int_n_data[specs->u.int_n_idx].bitsize);
12557 : 51262 : else if (! int_n_enabled_p[specs->u.int_n_idx])
12558 : : {
12559 : 0 : specs->typespec_word = cts_int_n;
12560 : 0 : error_at (loc,
12561 : : "%<__int%d%> is not supported on this target",
12562 : 0 : int_n_data[specs->u.int_n_idx].bitsize);
12563 : : }
12564 : : else
12565 : : {
12566 : 51262 : specs->typespec_word = cts_int_n;
12567 : 51262 : specs->locations[cdw_typespec] = loc;
12568 : : }
12569 : 51334 : return specs;
12570 : 7662845 : case RID_VOID:
12571 : 7662845 : if (specs->long_p)
12572 : 44 : error_at (loc,
12573 : : ("both %<long%> and %<void%> in "
12574 : : "declaration specifiers"));
12575 : 7662801 : else if (specs->short_p)
12576 : 21 : error_at (loc,
12577 : : ("both %<short%> and %<void%> in "
12578 : : "declaration specifiers"));
12579 : 7662780 : else if (specs->signed_p)
12580 : 5 : error_at (loc,
12581 : : ("both %<signed%> and %<void%> in "
12582 : : "declaration specifiers"));
12583 : 7662775 : else if (specs->unsigned_p)
12584 : 5 : error_at (loc,
12585 : : ("both %<unsigned%> and %<void%> in "
12586 : : "declaration specifiers"));
12587 : 7662770 : else if (specs->complex_p)
12588 : 2 : error_at (loc,
12589 : : ("both %<complex%> and %<void%> in "
12590 : : "declaration specifiers"));
12591 : 7662768 : else if (specs->saturating_p)
12592 : 0 : error_at (loc,
12593 : : ("both %<_Sat%> and %<void%> in "
12594 : : "declaration specifiers"));
12595 : : else
12596 : : {
12597 : 7662768 : specs->typespec_word = cts_void;
12598 : 7662768 : specs->locations[cdw_typespec] = loc;
12599 : : }
12600 : 7662845 : return specs;
12601 : 81741 : case RID_BOOL:
12602 : 81741 : if (!in_system_header_at (loc))
12603 : 67430 : pedwarn_c90 (loc, OPT_Wpedantic,
12604 : : "ISO C90 does not support boolean types");
12605 : 81741 : if (specs->long_p)
12606 : 27 : error_at (loc,
12607 : : ("both %<long%> and %<_Bool%> in "
12608 : : "declaration specifiers"));
12609 : 81714 : else if (specs->short_p)
12610 : 11 : error_at (loc,
12611 : : ("both %<short%> and %<_Bool%> in "
12612 : : "declaration specifiers"));
12613 : 81703 : else if (specs->signed_p)
12614 : 3 : error_at (loc,
12615 : : ("both %<signed%> and %<_Bool%> in "
12616 : : "declaration specifiers"));
12617 : 81700 : else if (specs->unsigned_p)
12618 : 3 : error_at (loc,
12619 : : ("both %<unsigned%> and %<_Bool%> in "
12620 : : "declaration specifiers"));
12621 : 81697 : else if (specs->complex_p)
12622 : 2 : error_at (loc,
12623 : : ("both %<complex%> and %<_Bool%> in "
12624 : : "declaration specifiers"));
12625 : 81695 : else if (specs->saturating_p)
12626 : 0 : error_at (loc,
12627 : : ("both %<_Sat%> and %<_Bool%> in "
12628 : : "declaration specifiers"));
12629 : : else
12630 : : {
12631 : 81695 : specs->typespec_word = cts_bool;
12632 : 81695 : specs->locations[cdw_typespec] = loc;
12633 : : }
12634 : 81741 : return specs;
12635 : 8925463 : case RID_CHAR:
12636 : 8925463 : if (specs->long_p)
12637 : 44 : error_at (loc,
12638 : : ("both %<long%> and %<char%> in "
12639 : : "declaration specifiers"));
12640 : 8925419 : else if (specs->short_p)
12641 : 21 : error_at (loc,
12642 : : ("both %<short%> and %<char%> in "
12643 : : "declaration specifiers"));
12644 : 8925398 : else if (specs->saturating_p)
12645 : 0 : error_at (loc,
12646 : : ("both %<_Sat%> and %<char%> in "
12647 : : "declaration specifiers"));
12648 : : else
12649 : : {
12650 : 8925398 : specs->typespec_word = cts_char;
12651 : 8925398 : specs->locations[cdw_typespec] = loc;
12652 : : }
12653 : 8925463 : return specs;
12654 : 23077612 : case RID_INT:
12655 : 23077612 : if (specs->saturating_p)
12656 : 0 : error_at (loc,
12657 : : ("both %<_Sat%> and %<int%> in "
12658 : : "declaration specifiers"));
12659 : : else
12660 : : {
12661 : 23077612 : specs->typespec_word = cts_int;
12662 : 23077612 : specs->locations[cdw_typespec] = loc;
12663 : : }
12664 : 23077612 : return specs;
12665 : 3162829 : case RID_FLOAT:
12666 : 3162829 : if (specs->long_p)
12667 : 44 : error_at (loc,
12668 : : ("both %<long%> and %<float%> in "
12669 : : "declaration specifiers"));
12670 : 3162785 : else if (specs->short_p)
12671 : 21 : error_at (loc,
12672 : : ("both %<short%> and %<float%> in "
12673 : : "declaration specifiers"));
12674 : 3162764 : else if (specs->signed_p)
12675 : 5 : error_at (loc,
12676 : : ("both %<signed%> and %<float%> in "
12677 : : "declaration specifiers"));
12678 : 3162759 : else if (specs->unsigned_p)
12679 : 5 : error_at (loc,
12680 : : ("both %<unsigned%> and %<float%> in "
12681 : : "declaration specifiers"));
12682 : 3162754 : else if (specs->saturating_p)
12683 : 0 : error_at (loc,
12684 : : ("both %<_Sat%> and %<float%> in "
12685 : : "declaration specifiers"));
12686 : : else
12687 : : {
12688 : 3162754 : specs->typespec_word = cts_float;
12689 : 3162754 : specs->locations[cdw_typespec] = loc;
12690 : : }
12691 : 3162829 : return specs;
12692 : 5666998 : case RID_DOUBLE:
12693 : 5666998 : if (specs->long_long_p)
12694 : 22 : error_at (loc,
12695 : : ("both %<long long%> and %<double%> in "
12696 : : "declaration specifiers"));
12697 : 5666976 : else if (specs->short_p)
12698 : 21 : error_at (loc,
12699 : : ("both %<short%> and %<double%> in "
12700 : : "declaration specifiers"));
12701 : 5666955 : else if (specs->signed_p)
12702 : 13 : error_at (loc,
12703 : : ("both %<signed%> and %<double%> in "
12704 : : "declaration specifiers"));
12705 : 5666942 : else if (specs->unsigned_p)
12706 : 13 : error_at (loc,
12707 : : ("both %<unsigned%> and %<double%> in "
12708 : : "declaration specifiers"));
12709 : 5666929 : else if (specs->saturating_p)
12710 : 0 : error_at (loc,
12711 : : ("both %<_Sat%> and %<double%> in "
12712 : : "declaration specifiers"));
12713 : : else
12714 : : {
12715 : 5666929 : specs->typespec_word = cts_double;
12716 : 5666929 : specs->locations[cdw_typespec] = loc;
12717 : : }
12718 : 5666998 : return specs;
12719 : 9457850 : CASE_RID_FLOATN_NX:
12720 : 9457850 : specs->u.floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
12721 : 9457850 : if (!in_system_header_at (input_location))
12722 : 51462 : pedwarn_c11 (loc, OPT_Wpedantic,
12723 : : "ISO C does not support the %<_Float%d%s%> type"
12724 : : " before C23",
12725 : 51462 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12726 : 51462 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12727 : : ? "x"
12728 : : : ""));
12729 : :
12730 : 9457850 : if (specs->long_p)
12731 : 0 : error_at (loc,
12732 : : ("both %<long%> and %<_Float%d%s%> in "
12733 : : "declaration specifiers"),
12734 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12735 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12736 : : ? "x"
12737 : : : ""));
12738 : 9457850 : else if (specs->short_p)
12739 : 0 : error_at (loc,
12740 : : ("both %<short%> and %<_Float%d%s%> in "
12741 : : "declaration specifiers"),
12742 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12743 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12744 : : ? "x"
12745 : : : ""));
12746 : 9457850 : else if (specs->signed_p)
12747 : 0 : error_at (loc,
12748 : : ("both %<signed%> and %<_Float%d%s%> in "
12749 : : "declaration specifiers"),
12750 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12751 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12752 : : ? "x"
12753 : : : ""));
12754 : 9457850 : else if (specs->unsigned_p)
12755 : 0 : error_at (loc,
12756 : : ("both %<unsigned%> and %<_Float%d%s%> in "
12757 : : "declaration specifiers"),
12758 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12759 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12760 : : ? "x"
12761 : : : ""));
12762 : 9457850 : else if (specs->saturating_p)
12763 : 0 : error_at (loc,
12764 : : ("both %<_Sat%> and %<_Float%d%s%> in "
12765 : : "declaration specifiers"),
12766 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12767 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12768 : : ? "x"
12769 : : : ""));
12770 : 9457850 : else if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
12771 : : {
12772 : 88 : specs->typespec_word = cts_floatn_nx;
12773 : 176 : error_at (loc,
12774 : : "%<_Float%d%s%> is not supported on this target",
12775 : 88 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12776 : 88 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12777 : : ? "x"
12778 : : : ""));
12779 : : }
12780 : : else
12781 : : {
12782 : 9457762 : specs->typespec_word = cts_floatn_nx;
12783 : 9457762 : specs->locations[cdw_typespec] = loc;
12784 : : }
12785 : 9457850 : return specs;
12786 : 47632 : case RID_DFLOAT32:
12787 : 47632 : case RID_DFLOAT64:
12788 : 47632 : case RID_DFLOAT128:
12789 : 47632 : case RID_DFLOAT64X:
12790 : 47632 : {
12791 : 47632 : const char *str;
12792 : 47632 : if (i == RID_DFLOAT32)
12793 : : str = "_Decimal32";
12794 : : else if (i == RID_DFLOAT64)
12795 : : str = "_Decimal64";
12796 : : else if (i == RID_DFLOAT128)
12797 : : str = "_Decimal128";
12798 : : else
12799 : 47632 : str = "_Decimal64x";
12800 : 47632 : if (specs->long_long_p)
12801 : 18 : error_at (loc,
12802 : : ("both %<long long%> and %qs in "
12803 : : "declaration specifiers"),
12804 : : str);
12805 : 47632 : if (specs->long_p)
12806 : 33 : error_at (loc,
12807 : : ("both %<long%> and %qs in "
12808 : : "declaration specifiers"),
12809 : : str);
12810 : 47599 : else if (specs->short_p)
12811 : 18 : error_at (loc,
12812 : : ("both %<short%> and %qs in "
12813 : : "declaration specifiers"),
12814 : : str);
12815 : 47581 : else if (specs->signed_p)
12816 : 6 : error_at (loc,
12817 : : ("both %<signed%> and %qs in "
12818 : : "declaration specifiers"),
12819 : : str);
12820 : 47575 : else if (specs->unsigned_p)
12821 : 3 : error_at (loc,
12822 : : ("both %<unsigned%> and %qs in "
12823 : : "declaration specifiers"),
12824 : : str);
12825 : 47572 : else if (specs->complex_p)
12826 : 3 : error_at (loc,
12827 : : ("both %<complex%> and %qs in "
12828 : : "declaration specifiers"),
12829 : : str);
12830 : 47569 : else if (specs->saturating_p)
12831 : 0 : error_at (loc,
12832 : : ("both %<_Sat%> and %qs in "
12833 : : "declaration specifiers"),
12834 : : str);
12835 : 47569 : else if (i == RID_DFLOAT32)
12836 : 15957 : specs->typespec_word = cts_dfloat32;
12837 : 31612 : else if (i == RID_DFLOAT64)
12838 : 15832 : specs->typespec_word = cts_dfloat64;
12839 : 15780 : else if (i == RID_DFLOAT128)
12840 : 15733 : specs->typespec_word = cts_dfloat128;
12841 : : else
12842 : 47 : specs->typespec_word = cts_dfloat64x;
12843 : 47632 : specs->locations[cdw_typespec] = loc;
12844 : : }
12845 : 47632 : if (!targetm.decimal_float_supported_p ())
12846 : 0 : error_at (loc,
12847 : : ("decimal floating-point not supported "
12848 : : "for this target"));
12849 : 47632 : pedwarn_c11 (loc, OPT_Wpedantic,
12850 : : "ISO C does not support decimal floating-point "
12851 : : "before C23");
12852 : 47632 : return specs;
12853 : 121 : case RID_FRACT:
12854 : 121 : case RID_ACCUM:
12855 : 121 : {
12856 : 121 : const char *str;
12857 : 121 : if (i == RID_FRACT)
12858 : : str = "_Fract";
12859 : : else
12860 : 60 : str = "_Accum";
12861 : 121 : if (specs->complex_p)
12862 : 0 : error_at (loc,
12863 : : ("both %<complex%> and %qs in "
12864 : : "declaration specifiers"),
12865 : : str);
12866 : 121 : else if (i == RID_FRACT)
12867 : 61 : specs->typespec_word = cts_fract;
12868 : : else
12869 : 60 : specs->typespec_word = cts_accum;
12870 : 121 : specs->locations[cdw_typespec] = loc;
12871 : : }
12872 : 121 : if (!targetm.fixed_point_supported_p ())
12873 : 121 : error_at (loc,
12874 : : "fixed-point types not supported for this target");
12875 : 121 : pedwarn (loc, OPT_Wpedantic,
12876 : : "ISO C does not support fixed-point types");
12877 : 121 : return specs;
12878 : 45465 : case RID_BITINT:
12879 : 45465 : if (specs->long_p)
12880 : 2 : error_at (loc,
12881 : : ("both %<long%> and %<_BitInt%> in "
12882 : : "declaration specifiers"));
12883 : 45463 : else if (specs->short_p)
12884 : 1 : error_at (loc,
12885 : : ("both %<short%> and %<_BitInt%> in "
12886 : : "declaration specifiers"));
12887 : 45462 : else if (specs->complex_p)
12888 : 1 : error_at (loc,
12889 : : ("both %<complex%> and %<_BitInt%> in "
12890 : : "declaration specifiers"));
12891 : 45461 : else if (specs->saturating_p)
12892 : 0 : error_at (loc,
12893 : : ("both %<_Sat%> and %<_BitInt%> in "
12894 : : "declaration specifiers"));
12895 : : else
12896 : : {
12897 : 45461 : specs->typespec_word = cts_bitint;
12898 : 45461 : specs->locations[cdw_typespec] = loc;
12899 : 45461 : specs->u.bitint_prec = -1;
12900 : 45461 : if (error_operand_p (spec.expr))
12901 : 7 : return specs;
12902 : 45461 : if (TREE_CODE (spec.expr) != INTEGER_CST
12903 : 45461 : || !INTEGRAL_TYPE_P (TREE_TYPE (spec.expr)))
12904 : : {
12905 : 1 : error_at (loc, "%<_BitInt%> argument is not an integer "
12906 : : "constant expression");
12907 : 1 : return specs;
12908 : : }
12909 : 45460 : if (tree_int_cst_sgn (spec.expr) <= 0)
12910 : : {
12911 : 4 : error_at (loc, "%<_BitInt%> argument %qE is not a "
12912 : : "positive integer constant expression",
12913 : : spec.expr);
12914 : 4 : return specs;
12915 : : }
12916 : 45456 : if (wi::to_widest (spec.expr) > WIDE_INT_MAX_PRECISION - 1)
12917 : : {
12918 : 2 : error_at (loc, "%<_BitInt%> argument %qE is larger than "
12919 : : "%<BITINT_MAXWIDTH%> %qd",
12920 : : spec.expr, (int) WIDE_INT_MAX_PRECISION - 1);
12921 : 2 : return specs;
12922 : : }
12923 : 45454 : specs->u.bitint_prec = tree_to_uhwi (spec.expr);
12924 : 45454 : struct bitint_info info;
12925 : 45454 : if (!targetm.c.bitint_type_info (specs->u.bitint_prec,
12926 : : &info))
12927 : : {
12928 : 0 : sorry_at (loc, "%<_BitInt(%d)%> is not supported on "
12929 : : "this target", specs->u.bitint_prec);
12930 : 0 : specs->u.bitint_prec = -1;
12931 : 0 : return specs;
12932 : : }
12933 : : }
12934 : 45458 : return specs;
12935 : : default:
12936 : : /* ObjC reserved word "id", handled below. */
12937 : : break;
12938 : : }
12939 : : }
12940 : : }
12941 : :
12942 : : /* Now we have a typedef (a TYPE_DECL node), an identifier (some
12943 : : form of ObjC type, cases such as "int" and "long" being handled
12944 : : above), a TYPE (struct, union, enum and typeof specifiers) or an
12945 : : ERROR_MARK. In none of these cases may there have previously
12946 : : been any type specifiers. */
12947 : 242552143 : if (specs->type || specs->typespec_word != cts_none
12948 : 242552131 : || specs->long_p || specs->short_p || specs->signed_p
12949 : 242552128 : || specs->unsigned_p || specs->complex_p)
12950 : 21 : error_at (loc, "two or more data types in declaration specifiers");
12951 : 242552122 : else if (TREE_CODE (type) == TYPE_DECL)
12952 : : {
12953 : 239345119 : specs->type = TREE_TYPE (type);
12954 : 239345119 : if (TREE_TYPE (type) != error_mark_node)
12955 : : {
12956 : 239345106 : specs->decl_attr = DECL_ATTRIBUTES (type);
12957 : 239345106 : specs->typedef_p = true;
12958 : 239345106 : specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
12959 : 239345106 : specs->locations[cdw_typedef] = loc;
12960 : :
12961 : : /* If this typedef name is defined in a struct, then a C++
12962 : : lookup would return a different value. */
12963 : 239345106 : if (warn_cxx_compat
12964 : 239345106 : && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
12965 : 2 : warning_at (loc, OPT_Wc___compat,
12966 : : "C++ lookup of %qD would return a field, not a type",
12967 : : type);
12968 : :
12969 : : /* If we are parsing a struct, record that a struct field
12970 : : used a typedef. */
12971 : 239345106 : if (warn_cxx_compat && struct_parse_info != NULL)
12972 : 1746 : struct_parse_info->typedefs_seen.safe_push (type);
12973 : : }
12974 : : }
12975 : 3207003 : else if (TREE_CODE (type) == IDENTIFIER_NODE)
12976 : : {
12977 : 0 : tree t = lookup_name (type);
12978 : 0 : if (!t || TREE_CODE (t) != TYPE_DECL)
12979 : 0 : error_at (loc, "%qE fails to be a typedef or built in type", type);
12980 : 0 : else if (TREE_TYPE (t) == error_mark_node)
12981 : : ;
12982 : : else
12983 : : {
12984 : 0 : specs->type = TREE_TYPE (t);
12985 : 0 : specs->locations[cdw_typespec] = loc;
12986 : : }
12987 : : }
12988 : : else
12989 : : {
12990 : 3207003 : if (TREE_CODE (type) != ERROR_MARK)
12991 : : {
12992 : 3206799 : if (spec.kind == ctsk_typeof)
12993 : : {
12994 : 819720 : specs->typedef_p = true;
12995 : 819720 : specs->locations[cdw_typedef] = loc;
12996 : : }
12997 : 3206799 : if (spec.expr)
12998 : : {
12999 : 889 : if (specs->expr)
13000 : 0 : specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
13001 : : specs->expr, spec.expr);
13002 : : else
13003 : 889 : specs->expr = spec.expr;
13004 : 889 : specs->expr_const_operands &= spec.expr_const_operands;
13005 : : }
13006 : : }
13007 : 3207003 : specs->type = type;
13008 : 3207003 : if (spec.has_enum_type_specifier
13009 : 166 : && spec.kind != ctsk_tagdef)
13010 : 49 : specs->enum_type_specifier_ref_p = true;
13011 : : }
13012 : :
13013 : : return specs;
13014 : : }
13015 : :
13016 : : /* Add the storage class specifier or function specifier SCSPEC to the
13017 : : declaration specifiers SPECS, returning SPECS. */
13018 : :
13019 : : struct c_declspecs *
13020 : 87588861 : declspecs_add_scspec (location_t loc,
13021 : : struct c_declspecs *specs,
13022 : : tree scspec)
13023 : : {
13024 : 87588861 : enum rid i;
13025 : 87588861 : enum c_storage_class n = csc_none;
13026 : 87588861 : bool dupe = false;
13027 : 87588861 : specs->declspecs_seen_p = true;
13028 : 87588861 : specs->non_std_attrs_seen_p = true;
13029 : 87588861 : gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
13030 : : && C_IS_RESERVED_WORD (scspec));
13031 : 87588861 : i = C_RID_CODE (scspec);
13032 : 87588861 : if (specs->non_sc_seen_p)
13033 : 2072 : warning (OPT_Wold_style_declaration,
13034 : : "%qE is not at beginning of declaration", scspec);
13035 : 87588861 : switch (i)
13036 : : {
13037 : 34676043 : case RID_INLINE:
13038 : : /* C99 permits duplicate inline. Although of doubtful utility,
13039 : : it seems simplest to permit it in gnu89 mode as well, as
13040 : : there is also little utility in maintaining this as a
13041 : : difference between gnu89 and C99 inline. */
13042 : 34676043 : dupe = false;
13043 : 34676043 : specs->inline_p = true;
13044 : 34676043 : specs->locations[cdw_inline] = loc;
13045 : 34676043 : break;
13046 : 22965 : case RID_NORETURN:
13047 : : /* Duplicate _Noreturn is permitted. */
13048 : 22965 : dupe = false;
13049 : 22965 : specs->noreturn_p = true;
13050 : 22965 : specs->locations[cdw_noreturn] = loc;
13051 : 22965 : break;
13052 : 2823 : case RID_THREAD:
13053 : 2823 : dupe = specs->thread_p;
13054 : 2823 : if (specs->storage_class == csc_auto)
13055 : 2 : error ("%qE used with %<auto%>", scspec);
13056 : 2821 : else if (specs->storage_class == csc_register)
13057 : 3 : error ("%qE used with %<register%>", scspec);
13058 : 2818 : else if (specs->storage_class == csc_typedef)
13059 : 2 : error ("%qE used with %<typedef%>", scspec);
13060 : 2816 : else if (specs->constexpr_p)
13061 : 2 : error ("%qE used with %<constexpr%>", scspec);
13062 : : else
13063 : : {
13064 : 2814 : specs->thread_p = true;
13065 : 2814 : specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
13066 : 2814 : "__thread") == 0);
13067 : : /* A diagnostic is not required for the use of this
13068 : : identifier in the implementation namespace; only diagnose
13069 : : it for the C11 spelling because of existing code using
13070 : : the other spelling. */
13071 : 2814 : if (!specs->thread_gnu_p)
13072 : : {
13073 : 102 : if (flag_isoc99)
13074 : 99 : pedwarn_c99 (loc, OPT_Wpedantic,
13075 : : "ISO C99 does not support %qE", scspec);
13076 : : else
13077 : 3 : pedwarn_c99 (loc, OPT_Wpedantic,
13078 : : "ISO C90 does not support %qE", scspec);
13079 : : }
13080 : 2814 : specs->locations[cdw_thread] = loc;
13081 : : }
13082 : : break;
13083 : 259 : case RID_AUTO:
13084 : 259 : if (flag_isoc23
13085 : 220 : && specs->typespec_kind == ctsk_none
13086 : 209 : && specs->storage_class != csc_typedef)
13087 : : {
13088 : : /* "auto" potentially used for type deduction. */
13089 : 208 : if (specs->c23_auto_p)
13090 : 2 : error ("duplicate %qE", scspec);
13091 : 208 : specs->c23_auto_p = true;
13092 : 208 : return specs;
13093 : : }
13094 : 51 : n = csc_auto;
13095 : : /* auto may only be used with another storage class specifier,
13096 : : such as constexpr, if the type is inferred. */
13097 : 51 : if (specs->constexpr_p)
13098 : 2 : error ("%qE used with %<constexpr%>", scspec);
13099 : : break;
13100 : 48262131 : case RID_EXTERN:
13101 : 48262131 : n = csc_extern;
13102 : : /* Diagnose "__thread extern". */
13103 : 48262131 : if (specs->thread_p && specs->thread_gnu_p)
13104 : 2 : error ("%<__thread%> before %<extern%>");
13105 : : break;
13106 : : case RID_REGISTER:
13107 : : n = csc_register;
13108 : : break;
13109 : 411999 : case RID_STATIC:
13110 : 411999 : n = csc_static;
13111 : : /* Diagnose "__thread static". */
13112 : 411999 : if (specs->thread_p && specs->thread_gnu_p)
13113 : 1 : error ("%<__thread%> before %<static%>");
13114 : : break;
13115 : 4208943 : case RID_TYPEDEF:
13116 : 4208943 : n = csc_typedef;
13117 : 4208943 : if (specs->c23_auto_p)
13118 : : {
13119 : 1 : error ("%<typedef%> used with %<auto%>");
13120 : 1 : specs->c23_auto_p = false;
13121 : : }
13122 : : break;
13123 : 602 : case RID_CONSTEXPR:
13124 : 602 : dupe = specs->constexpr_p;
13125 : 602 : if (specs->storage_class == csc_extern)
13126 : 1 : error ("%qE used with %<extern%>", scspec);
13127 : 601 : else if (specs->storage_class == csc_typedef)
13128 : 1 : error ("%qE used with %<typedef%>", scspec);
13129 : 600 : else if (specs->storage_class == csc_auto)
13130 : : /* auto may only be used with another storage class specifier,
13131 : : such as constexpr, if the type is inferred. */
13132 : 2 : error ("%qE used with %<auto%>", scspec);
13133 : 598 : else if (specs->thread_p)
13134 : 4 : error ("%qE used with %qs", scspec,
13135 : 2 : specs->thread_gnu_p ? "__thread" : "_Thread_local");
13136 : : else
13137 : 596 : specs->constexpr_p = true;
13138 : : break;
13139 : 0 : default:
13140 : 0 : gcc_unreachable ();
13141 : : }
13142 : 87588659 : if (n != csc_none && n == specs->storage_class)
13143 : : dupe = true;
13144 : 87588645 : if (dupe)
13145 : : {
13146 : 12 : if (i == RID_THREAD)
13147 : 2 : error ("duplicate %<_Thread_local%> or %<__thread%>");
13148 : : else
13149 : 10 : error ("duplicate %qE", scspec);
13150 : : }
13151 : 87588653 : if (n != csc_none)
13152 : : {
13153 : 52886220 : if (specs->storage_class != csc_none && n != specs->storage_class)
13154 : : {
13155 : 7 : error ("multiple storage classes in declaration specifiers");
13156 : : }
13157 : : else
13158 : : {
13159 : 52886213 : specs->storage_class = n;
13160 : 52886213 : specs->locations[cdw_storage_class] = loc;
13161 : 52886213 : if (n != csc_extern && n != csc_static && specs->thread_p)
13162 : : {
13163 : 8 : error ("%qs used with %qE",
13164 : 8 : specs->thread_gnu_p ? "__thread" : "_Thread_local",
13165 : : scspec);
13166 : 8 : specs->thread_p = false;
13167 : : }
13168 : 52886213 : if (n != csc_auto && n != csc_register && n != csc_static
13169 : 52471071 : && specs->constexpr_p)
13170 : : {
13171 : 2 : error ("%<constexpr%> used with %qE", scspec);
13172 : 2 : specs->constexpr_p = false;
13173 : : }
13174 : : }
13175 : : }
13176 : : return specs;
13177 : : }
13178 : :
13179 : : /* Add the attributes ATTRS to the declaration specifiers SPECS,
13180 : : returning SPECS. */
13181 : :
13182 : : struct c_declspecs *
13183 : 34978596 : declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
13184 : : {
13185 : 34978596 : specs->attrs = chainon (attrs, specs->attrs);
13186 : 34978596 : specs->locations[cdw_attributes] = loc;
13187 : 34978596 : specs->declspecs_seen_p = true;
13188 : : /* In the case of standard attributes at the start of the
13189 : : declaration, the caller will reset this. */
13190 : 34978596 : specs->non_std_attrs_seen_p = true;
13191 : 34978596 : return specs;
13192 : : }
13193 : :
13194 : : /* Add an _Alignas specifier (expression ALIGN, or type whose
13195 : : alignment is ALIGN) to the declaration specifiers SPECS, returning
13196 : : SPECS. */
13197 : : struct c_declspecs *
13198 : 200 : declspecs_add_alignas (location_t loc,
13199 : : struct c_declspecs *specs, tree align)
13200 : : {
13201 : 200 : specs->alignas_p = true;
13202 : 200 : specs->locations[cdw_alignas] = loc;
13203 : 200 : if (align == error_mark_node)
13204 : : return specs;
13205 : :
13206 : : /* Only accept the alignment if it's valid and greater than
13207 : : the current one. Zero is invalid but by C11 required to
13208 : : be silently ignored. */
13209 : 198 : int align_log = check_user_alignment (align, false, /* warn_zero = */false);
13210 : 198 : if (align_log > specs->align_log)
13211 : 165 : specs->align_log = align_log;
13212 : : return specs;
13213 : : }
13214 : :
13215 : : /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
13216 : : specifiers with any other type specifier to determine the resulting
13217 : : type. This is where ISO C checks on complex types are made, since
13218 : : "_Complex long" is a prefix of the valid ISO C type "_Complex long
13219 : : double". Also apply postfix standard attributes to modify the type. */
13220 : :
13221 : : struct c_declspecs *
13222 : 304827761 : finish_declspecs (struct c_declspecs *specs)
13223 : : {
13224 : : /* If a type was specified as a whole, we have no modifiers and are
13225 : : done. */
13226 : 304827761 : if (specs->type != NULL_TREE)
13227 : : {
13228 : 242552076 : gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
13229 : : && !specs->signed_p && !specs->unsigned_p
13230 : : && !specs->complex_p && !specs->c23_auto_p);
13231 : :
13232 : : /* Set a dummy type. */
13233 : 242552076 : if (TREE_CODE (specs->type) == ERROR_MARK)
13234 : 180 : specs->type = integer_type_node;
13235 : 242552076 : goto handle_postfix_attrs;
13236 : : }
13237 : :
13238 : : /* If none of "void", "_Bool", "char", "int", "float" or "double"
13239 : : has been specified, treat it as "int" unless "_Complex" is
13240 : : present and there are no other specifiers. If we just have
13241 : : "_Complex", it is equivalent to "_Complex double", but e.g.
13242 : : "_Complex short" is equivalent to "_Complex short int". */
13243 : 62275685 : if (specs->typespec_word == cts_none)
13244 : : {
13245 : 4094435 : if (specs->saturating_p)
13246 : : {
13247 : 1 : error_at (specs->locations[cdw_saturating],
13248 : : "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
13249 : 1 : if (!targetm.fixed_point_supported_p ())
13250 : 1 : error_at (specs->locations[cdw_saturating],
13251 : : "fixed-point types not supported for this target");
13252 : 1 : specs->typespec_word = cts_fract;
13253 : : }
13254 : 4094434 : else if (specs->long_p || specs->short_p
13255 : 188218 : || specs->signed_p || specs->unsigned_p)
13256 : : {
13257 : 4084009 : specs->typespec_word = cts_int;
13258 : : }
13259 : 10425 : else if (specs->complex_p)
13260 : : {
13261 : 130 : specs->typespec_word = cts_double;
13262 : 130 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13263 : : "ISO C does not support plain %<complex%> meaning "
13264 : : "%<double complex%>");
13265 : : }
13266 : 10295 : else if (specs->c23_auto_p)
13267 : : {
13268 : : /* Type to be filled in later, including applying postfix
13269 : : attributes. This warning only actually appears for
13270 : : -Wc11-c23-compat in C23 mode; in older modes, there may
13271 : : be a warning or pedwarn for implicit "int" instead, or
13272 : : other errors for use of auto at file scope. */
13273 : 93 : pedwarn_c11 (input_location, OPT_Wpedantic,
13274 : : "ISO C does not support %<auto%> type deduction "
13275 : : "before C23");
13276 : 93 : return specs;
13277 : : }
13278 : : else
13279 : : {
13280 : 10202 : specs->typespec_word = cts_int;
13281 : 10202 : specs->default_int_p = true;
13282 : : /* We don't diagnose this here because grokdeclarator will
13283 : : give more specific diagnostics according to whether it is
13284 : : a function definition. */
13285 : : }
13286 : : }
13287 : :
13288 : : /* If "signed" was specified, record this to distinguish "int" and
13289 : : "signed int" in the case of a bit-field with
13290 : : -funsigned-bitfields. */
13291 : 62275592 : specs->explicit_signed_p = specs->signed_p;
13292 : :
13293 : : /* Now compute the actual type. */
13294 : 62275592 : gcc_assert (!specs->c23_auto_p);
13295 : 62275592 : switch (specs->typespec_word)
13296 : : {
13297 : 1833 : case cts_auto_type:
13298 : 1833 : gcc_assert (!specs->long_p && !specs->short_p
13299 : : && !specs->signed_p && !specs->unsigned_p
13300 : : && !specs->complex_p);
13301 : : /* Type to be filled in later. */
13302 : 1833 : if (specs->postfix_attrs)
13303 : 2 : error ("%<__auto_type%> followed by %<[[]]%> attributes");
13304 : : break;
13305 : 7662768 : case cts_void:
13306 : 7662768 : gcc_assert (!specs->long_p && !specs->short_p
13307 : : && !specs->signed_p && !specs->unsigned_p
13308 : : && !specs->complex_p);
13309 : 7662768 : specs->type = void_type_node;
13310 : 7662768 : break;
13311 : 81695 : case cts_bool:
13312 : 81695 : gcc_assert (!specs->long_p && !specs->short_p
13313 : : && !specs->signed_p && !specs->unsigned_p
13314 : : && !specs->complex_p);
13315 : 81695 : specs->type = boolean_type_node;
13316 : 81695 : break;
13317 : 8925398 : case cts_char:
13318 : 8925398 : gcc_assert (!specs->long_p && !specs->short_p);
13319 : 8925398 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13320 : 8925398 : if (specs->signed_p)
13321 : 188381 : specs->type = signed_char_type_node;
13322 : 8737017 : else if (specs->unsigned_p)
13323 : 875775 : specs->type = unsigned_char_type_node;
13324 : : else
13325 : 7861242 : specs->type = char_type_node;
13326 : 8925398 : if (specs->complex_p)
13327 : : {
13328 : 3853 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13329 : : "ISO C does not support complex integer types");
13330 : 3853 : specs->type = build_complex_type (specs->type);
13331 : : }
13332 : : break;
13333 : 51262 : case cts_int_n:
13334 : 51262 : gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
13335 : 51262 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13336 : 51262 : if (! int_n_enabled_p[specs->u.int_n_idx])
13337 : 0 : specs->type = integer_type_node;
13338 : : else
13339 : 51262 : specs->type = (specs->unsigned_p
13340 : 51262 : ? int_n_trees[specs->u.int_n_idx].unsigned_type
13341 : : : int_n_trees[specs->u.int_n_idx].signed_type);
13342 : 51262 : if (specs->complex_p)
13343 : : {
13344 : 199 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13345 : : "ISO C does not support complex integer types");
13346 : 199 : specs->type = build_complex_type (specs->type);
13347 : : }
13348 : : break;
13349 : 27171821 : case cts_int:
13350 : 27171821 : gcc_assert (!(specs->long_p && specs->short_p));
13351 : 27171821 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13352 : 27171821 : if (specs->long_long_p)
13353 : 2699210 : specs->type = (specs->unsigned_p
13354 : 2699210 : ? long_long_unsigned_type_node
13355 : : : long_long_integer_type_node);
13356 : 24472611 : else if (specs->long_p)
13357 : 2149031 : specs->type = (specs->unsigned_p
13358 : 2149031 : ? long_unsigned_type_node
13359 : : : long_integer_type_node);
13360 : 22323580 : else if (specs->short_p)
13361 : 1662373 : specs->type = (specs->unsigned_p
13362 : 1662373 : ? short_unsigned_type_node
13363 : : : short_integer_type_node);
13364 : : else
13365 : 20661207 : specs->type = (specs->unsigned_p
13366 : 20661207 : ? unsigned_type_node
13367 : : : integer_type_node);
13368 : 27171821 : if (specs->complex_p)
13369 : : {
13370 : 14909 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13371 : : "ISO C does not support complex integer types");
13372 : 14909 : specs->type = build_complex_type (specs->type);
13373 : : }
13374 : : break;
13375 : 3162754 : case cts_float:
13376 : 3162754 : gcc_assert (!specs->long_p && !specs->short_p
13377 : : && !specs->signed_p && !specs->unsigned_p);
13378 : 6325508 : specs->type = (specs->complex_p
13379 : 3162754 : ? complex_float_type_node
13380 : : : float_type_node);
13381 : 3162754 : break;
13382 : 5667059 : case cts_double:
13383 : 5667059 : gcc_assert (!specs->long_long_p && !specs->short_p
13384 : : && !specs->signed_p && !specs->unsigned_p);
13385 : 5667059 : if (specs->long_p)
13386 : : {
13387 : 2414893 : specs->type = (specs->complex_p
13388 : 2414893 : ? complex_long_double_type_node
13389 : : : long_double_type_node);
13390 : : }
13391 : : else
13392 : : {
13393 : 3252166 : specs->type = (specs->complex_p
13394 : 3252166 : ? complex_double_type_node
13395 : : : double_type_node);
13396 : : }
13397 : : break;
13398 : 9457850 : case cts_floatn_nx:
13399 : 9457850 : gcc_assert (!specs->long_p && !specs->short_p
13400 : : && !specs->signed_p && !specs->unsigned_p);
13401 : 9457850 : if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
13402 : 88 : specs->type = integer_type_node;
13403 : 9457762 : else if (specs->complex_p)
13404 : 1535805 : specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13405 : : else
13406 : 7921957 : specs->type = FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13407 : : break;
13408 : 47569 : case cts_dfloat32:
13409 : 47569 : case cts_dfloat64:
13410 : 47569 : case cts_dfloat128:
13411 : 47569 : case cts_dfloat64x:
13412 : 47569 : gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
13413 : : && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
13414 : 47569 : if (!targetm.decimal_float_supported_p ())
13415 : 0 : specs->type = integer_type_node;
13416 : 47569 : else if (specs->typespec_word == cts_dfloat32)
13417 : 15957 : specs->type = dfloat32_type_node;
13418 : 31612 : else if (specs->typespec_word == cts_dfloat64)
13419 : 15832 : specs->type = dfloat64_type_node;
13420 : 15780 : else if (specs->typespec_word == cts_dfloat128)
13421 : 15733 : specs->type = dfloat128_type_node;
13422 : : else
13423 : 47 : specs->type = dfloat64x_type_node;
13424 : : break;
13425 : 62 : case cts_fract:
13426 : 62 : gcc_assert (!specs->complex_p);
13427 : 62 : if (!targetm.fixed_point_supported_p ())
13428 : 62 : specs->type = integer_type_node;
13429 : 0 : else if (specs->saturating_p)
13430 : : {
13431 : 0 : if (specs->long_long_p)
13432 : 0 : specs->type = specs->unsigned_p
13433 : 0 : ? sat_unsigned_long_long_fract_type_node
13434 : : : sat_long_long_fract_type_node;
13435 : 0 : else if (specs->long_p)
13436 : 0 : specs->type = specs->unsigned_p
13437 : 0 : ? sat_unsigned_long_fract_type_node
13438 : : : sat_long_fract_type_node;
13439 : 0 : else if (specs->short_p)
13440 : 0 : specs->type = specs->unsigned_p
13441 : 0 : ? sat_unsigned_short_fract_type_node
13442 : : : sat_short_fract_type_node;
13443 : : else
13444 : 0 : specs->type = specs->unsigned_p
13445 : 0 : ? sat_unsigned_fract_type_node
13446 : : : sat_fract_type_node;
13447 : : }
13448 : : else
13449 : : {
13450 : 0 : if (specs->long_long_p)
13451 : 0 : specs->type = specs->unsigned_p
13452 : 0 : ? unsigned_long_long_fract_type_node
13453 : : : long_long_fract_type_node;
13454 : 0 : else if (specs->long_p)
13455 : 0 : specs->type = specs->unsigned_p
13456 : 0 : ? unsigned_long_fract_type_node
13457 : : : long_fract_type_node;
13458 : 0 : else if (specs->short_p)
13459 : 0 : specs->type = specs->unsigned_p
13460 : 0 : ? unsigned_short_fract_type_node
13461 : : : short_fract_type_node;
13462 : : else
13463 : 0 : specs->type = specs->unsigned_p
13464 : 0 : ? unsigned_fract_type_node
13465 : : : fract_type_node;
13466 : : }
13467 : : break;
13468 : 60 : case cts_accum:
13469 : 60 : gcc_assert (!specs->complex_p);
13470 : 60 : if (!targetm.fixed_point_supported_p ())
13471 : 60 : specs->type = integer_type_node;
13472 : 0 : else if (specs->saturating_p)
13473 : : {
13474 : 0 : if (specs->long_long_p)
13475 : 0 : specs->type = specs->unsigned_p
13476 : 0 : ? sat_unsigned_long_long_accum_type_node
13477 : : : sat_long_long_accum_type_node;
13478 : 0 : else if (specs->long_p)
13479 : 0 : specs->type = specs->unsigned_p
13480 : 0 : ? sat_unsigned_long_accum_type_node
13481 : : : sat_long_accum_type_node;
13482 : 0 : else if (specs->short_p)
13483 : 0 : specs->type = specs->unsigned_p
13484 : 0 : ? sat_unsigned_short_accum_type_node
13485 : : : sat_short_accum_type_node;
13486 : : else
13487 : 0 : specs->type = specs->unsigned_p
13488 : 0 : ? sat_unsigned_accum_type_node
13489 : : : sat_accum_type_node;
13490 : : }
13491 : : else
13492 : : {
13493 : 0 : if (specs->long_long_p)
13494 : 0 : specs->type = specs->unsigned_p
13495 : 0 : ? unsigned_long_long_accum_type_node
13496 : : : long_long_accum_type_node;
13497 : 0 : else if (specs->long_p)
13498 : 0 : specs->type = specs->unsigned_p
13499 : 0 : ? unsigned_long_accum_type_node
13500 : : : long_accum_type_node;
13501 : 0 : else if (specs->short_p)
13502 : 0 : specs->type = specs->unsigned_p
13503 : 0 : ? unsigned_short_accum_type_node
13504 : : : short_accum_type_node;
13505 : : else
13506 : 0 : specs->type = specs->unsigned_p
13507 : 0 : ? unsigned_accum_type_node
13508 : : : accum_type_node;
13509 : : }
13510 : : break;
13511 : 45461 : case cts_bitint:
13512 : 45461 : gcc_assert (!specs->long_p && !specs->short_p
13513 : : && !specs->complex_p);
13514 : 45461 : if (!specs->unsigned_p && specs->u.bitint_prec == 1)
13515 : : {
13516 : 2 : error_at (specs->locations[cdw_typespec],
13517 : : "%<signed _BitInt%> argument must be at least 2");
13518 : 2 : specs->type = integer_type_node;
13519 : 2 : break;
13520 : : }
13521 : 45459 : if (specs->u.bitint_prec == -1)
13522 : 7 : specs->type = integer_type_node;
13523 : : else
13524 : : {
13525 : 66990 : pedwarn_c11 (specs->locations[cdw_typespec], OPT_Wpedantic,
13526 : : "ISO C does not support %<%s_BitInt(%d)%> before C23",
13527 : : specs->unsigned_p ? "unsigned "
13528 : 21538 : : specs->signed_p ? "signed " : "",
13529 : : specs->u.bitint_prec);
13530 : 45452 : specs->type = build_bitint_type (specs->u.bitint_prec,
13531 : 45452 : specs->unsigned_p);
13532 : : }
13533 : : break;
13534 : 0 : default:
13535 : 0 : gcc_unreachable ();
13536 : : }
13537 : 304827668 : handle_postfix_attrs:
13538 : 304827668 : if (specs->type != NULL)
13539 : : {
13540 : 304825835 : specs->postfix_attrs
13541 : 304825835 : = c_warn_type_attributes (specs->type, specs->postfix_attrs);
13542 : 304825835 : decl_attributes (&specs->type, specs->postfix_attrs, 0);
13543 : 304825835 : specs->postfix_attrs = NULL_TREE;
13544 : : }
13545 : :
13546 : : return specs;
13547 : : }
13548 : :
13549 : : /* Perform final processing on one file scope's declarations (or the
13550 : : external scope's declarations), GLOBALS. */
13551 : :
13552 : : static void
13553 : 211284 : c_write_global_declarations_1 (tree globals)
13554 : : {
13555 : 211284 : tree decl;
13556 : 211284 : bool reconsider;
13557 : :
13558 : : /* Process the decls in the order they were written. */
13559 : 383768700 : for (decl = globals; decl; decl = DECL_CHAIN (decl))
13560 : : {
13561 : : /* Check for used but undefined static functions using the C
13562 : : standard's definition of "used", and set TREE_NO_WARNING so
13563 : : that check_global_declaration doesn't repeat the check. */
13564 : 383557416 : if (TREE_CODE (decl) == FUNCTION_DECL
13565 : 365732838 : && DECL_INITIAL (decl) == NULL_TREE
13566 : 330385076 : && DECL_EXTERNAL (decl)
13567 : 713942487 : && !TREE_PUBLIC (decl))
13568 : : {
13569 : 90 : if (C_DECL_USED (decl))
13570 : : {
13571 : : /* TODO: Add OPT_Wundefined-inline. */
13572 : 20 : if (pedwarn (input_location, 0, "%q+F used but never defined",
13573 : : decl))
13574 : 19 : suppress_warning (decl /* OPT_Wundefined-inline. */);
13575 : : }
13576 : : /* For -Wunused-function warn about unused static prototypes. */
13577 : 70 : else if (warn_unused_function
13578 : 2 : && ! DECL_ARTIFICIAL (decl)
13579 : 72 : && ! warning_suppressed_p (decl, OPT_Wunused_function))
13580 : : {
13581 : 2 : if (warning (OPT_Wunused_function,
13582 : : "%q+F declared %<static%> but never defined",
13583 : : decl))
13584 : 2 : suppress_warning (decl, OPT_Wunused_function);
13585 : : }
13586 : : }
13587 : :
13588 : 383557416 : wrapup_global_declaration_1 (decl);
13589 : : }
13590 : :
13591 : 244511 : do
13592 : : {
13593 : 244511 : reconsider = false;
13594 : 494813930 : for (decl = globals; decl; decl = DECL_CHAIN (decl))
13595 : 494569419 : reconsider |= wrapup_global_declaration_2 (decl);
13596 : : }
13597 : : while (reconsider);
13598 : 211284 : }
13599 : :
13600 : : /* Preserve the external declarations scope across a garbage collect. */
13601 : : static GTY(()) tree ext_block;
13602 : :
13603 : : /* Collect all references relevant to SOURCE_FILE. */
13604 : :
13605 : : static void
13606 : 15 : collect_all_refs (const char *source_file)
13607 : : {
13608 : 15 : tree t;
13609 : 15 : unsigned i;
13610 : :
13611 : 30 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13612 : 15 : collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
13613 : :
13614 : 15 : collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
13615 : 15 : }
13616 : :
13617 : : /* Collect source file references at global level. */
13618 : :
13619 : : static void
13620 : 15 : collect_source_refs (void)
13621 : : {
13622 : 15 : tree t;
13623 : 15 : tree decls;
13624 : 15 : tree decl;
13625 : 15 : unsigned i;
13626 : :
13627 : 30 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13628 : : {
13629 : 15 : decls = DECL_INITIAL (t);
13630 : 82 : for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
13631 : 67 : if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13632 : 67 : collect_source_ref (DECL_SOURCE_FILE (decl));
13633 : : }
13634 : :
13635 : 44171 : for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
13636 : 44156 : if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13637 : 11 : collect_source_ref (DECL_SOURCE_FILE (decl));
13638 : 15 : }
13639 : :
13640 : : /* Free attribute access data that are not needed by the middle end. */
13641 : :
13642 : : static void
13643 : 105642 : free_attr_access_data ()
13644 : : {
13645 : 105642 : struct cgraph_node *n;
13646 : :
13647 : : /* Iterate over all functions declared in the translation unit. */
13648 : 70909952 : FOR_EACH_FUNCTION (n)
13649 : : {
13650 : 132774081 : for (tree parm = DECL_ARGUMENTS (n->decl); parm; parm = TREE_CHAIN (parm))
13651 : 97424747 : if (tree attrs = DECL_ATTRIBUTES (parm))
13652 : 95386 : attr_access::free_lang_data (attrs);
13653 : :
13654 : 35349334 : tree fntype = TREE_TYPE (n->decl);
13655 : 35349334 : if (!fntype || fntype == error_mark_node)
13656 : 0 : continue;
13657 : 35349334 : tree attrs = TYPE_ATTRIBUTES (fntype);
13658 : 35349334 : if (!attrs)
13659 : 35118131 : continue;
13660 : :
13661 : 231203 : attr_access::free_lang_data (attrs);
13662 : : }
13663 : 105642 : }
13664 : :
13665 : : /* Perform any final parser cleanups and generate initial debugging
13666 : : information. */
13667 : :
13668 : : void
13669 : 106012 : c_parse_final_cleanups (void)
13670 : : {
13671 : 106012 : tree t;
13672 : 106012 : unsigned i;
13673 : :
13674 : : /* We don't want to do this if generating a PCH. */
13675 : 106012 : if (pch_file)
13676 : 106012 : return;
13677 : :
13678 : 105642 : timevar_stop (TV_PHASE_PARSING);
13679 : 105642 : timevar_start (TV_PHASE_DEFERRED);
13680 : :
13681 : : /* Do the Objective-C stuff. This is where all the Objective-C
13682 : : module stuff gets generated (symtab, class/protocol/selector
13683 : : lists etc). */
13684 : 105642 : if (c_dialect_objc ())
13685 : 0 : objc_write_global_declarations ();
13686 : :
13687 : : /* Close the external scope. */
13688 : 105642 : ext_block = pop_scope ();
13689 : 105642 : external_scope = 0;
13690 : 105642 : gcc_assert (!current_scope);
13691 : :
13692 : : /* Handle -fdump-ada-spec[-slim]. */
13693 : 105642 : if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
13694 : : {
13695 : : /* Build a table of files to generate specs for */
13696 : 15 : collect_source_ref (main_input_filename);
13697 : 15 : if (!flag_dump_ada_spec_slim)
13698 : 15 : collect_source_refs ();
13699 : :
13700 : 15 : dump_ada_specs (collect_all_refs, NULL);
13701 : : }
13702 : :
13703 : : /* Process all file scopes in this compilation, and the external_scope,
13704 : : through wrapup_global_declarations. */
13705 : 211284 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13706 : 105642 : c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
13707 : 105642 : c_write_global_declarations_1 (BLOCK_VARS (ext_block));
13708 : :
13709 : 105642 : if (!in_lto_p)
13710 : 105642 : free_attr_access_data ();
13711 : :
13712 : 105642 : timevar_stop (TV_PHASE_DEFERRED);
13713 : 105642 : timevar_start (TV_PHASE_PARSING);
13714 : :
13715 : 105642 : ext_block = NULL;
13716 : : }
13717 : :
13718 : : /* Register reserved keyword WORD as qualifier for address space AS. */
13719 : :
13720 : : void
13721 : 221980 : c_register_addr_space (const char *word, addr_space_t as)
13722 : : {
13723 : 221980 : int rid = RID_FIRST_ADDR_SPACE + as;
13724 : 221980 : tree id;
13725 : :
13726 : : /* Address space qualifiers are only supported
13727 : : in C with GNU extensions enabled. */
13728 : 221980 : if (c_dialect_objc () || flag_no_asm)
13729 : : return;
13730 : :
13731 : 210942 : id = get_identifier (word);
13732 : 210942 : C_SET_RID_CODE (id, rid);
13733 : 210942 : C_IS_RESERVED_WORD (id) = 1;
13734 : 210942 : ridpointers [rid] = id;
13735 : : }
13736 : :
13737 : : /* Return identifier to look up for omp declare reduction. */
13738 : :
13739 : : tree
13740 : 3250 : c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
13741 : : {
13742 : 3250 : const char *p = NULL;
13743 : 3250 : switch (reduction_code)
13744 : : {
13745 : : case PLUS_EXPR: p = "+"; break;
13746 : 267 : case MULT_EXPR: p = "*"; break;
13747 : 155 : case MINUS_EXPR: p = "-"; break;
13748 : 31 : case BIT_AND_EXPR: p = "&"; break;
13749 : 14 : case BIT_XOR_EXPR: p = "^"; break;
13750 : 92 : case BIT_IOR_EXPR: p = "|"; break;
13751 : 67 : case TRUTH_ANDIF_EXPR: p = "&&"; break;
13752 : 78 : case TRUTH_ORIF_EXPR: p = "||"; break;
13753 : 46 : case MIN_EXPR: p = "min"; break;
13754 : 83 : case MAX_EXPR: p = "max"; break;
13755 : : default:
13756 : : break;
13757 : : }
13758 : :
13759 : 833 : if (p == NULL)
13760 : : {
13761 : 213 : if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
13762 : 0 : return error_mark_node;
13763 : 213 : p = IDENTIFIER_POINTER (reduction_id);
13764 : : }
13765 : :
13766 : 3250 : const char prefix[] = "omp declare reduction ";
13767 : 3250 : size_t lenp = sizeof (prefix);
13768 : 3250 : size_t len = strlen (p);
13769 : 3250 : char *name = XALLOCAVEC (char, lenp + len);
13770 : 3250 : memcpy (name, prefix, lenp - 1);
13771 : 3250 : memcpy (name + lenp - 1, p, len + 1);
13772 : 3250 : return get_identifier (name);
13773 : : }
13774 : :
13775 : : /* Lookup REDUCTION_ID in the current scope, or create an artificial
13776 : : VAR_DECL, bind it into the current scope and return it. */
13777 : :
13778 : : tree
13779 : 161 : c_omp_reduction_decl (tree reduction_id)
13780 : : {
13781 : 161 : struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13782 : 161 : if (b != NULL && B_IN_CURRENT_SCOPE (b))
13783 : 47 : return b->decl;
13784 : :
13785 : 114 : tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
13786 : : reduction_id, integer_type_node);
13787 : 114 : DECL_ARTIFICIAL (decl) = 1;
13788 : 114 : DECL_EXTERNAL (decl) = 1;
13789 : 114 : TREE_STATIC (decl) = 1;
13790 : 114 : TREE_PUBLIC (decl) = 0;
13791 : 114 : bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
13792 : 114 : return decl;
13793 : : }
13794 : :
13795 : : /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
13796 : :
13797 : : tree
13798 : 273 : c_omp_reduction_lookup (tree reduction_id, tree type)
13799 : : {
13800 : 273 : struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13801 : 276 : while (b)
13802 : : {
13803 : 274 : tree t;
13804 : 294 : for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
13805 : 291 : if (comptypes (TREE_PURPOSE (t), type))
13806 : 271 : return TREE_VALUE (t);
13807 : 3 : b = b->shadowed;
13808 : : }
13809 : 2 : return error_mark_node;
13810 : : }
13811 : :
13812 : : /* Helper function called via walk_tree, to diagnose invalid
13813 : : #pragma omp declare reduction combiners or initializers. */
13814 : :
13815 : : tree
13816 : 1401 : c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
13817 : : {
13818 : 1401 : tree *vars = (tree *) data;
13819 : 1401 : if (SSA_VAR_P (*tp)
13820 : 413 : && !DECL_ARTIFICIAL (*tp)
13821 : 24 : && *tp != vars[0]
13822 : 24 : && *tp != vars[1])
13823 : : {
13824 : 24 : location_t loc = DECL_SOURCE_LOCATION (vars[0]);
13825 : 24 : if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
13826 : 10 : error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
13827 : : "variable %qD which is not %<omp_out%> nor %<omp_in%>",
13828 : : *tp);
13829 : : else
13830 : 14 : error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
13831 : : "to variable %qD which is not %<omp_priv%> nor "
13832 : : "%<omp_orig%>",
13833 : : *tp);
13834 : 24 : return *tp;
13835 : : }
13836 : : return NULL_TREE;
13837 : : }
13838 : :
13839 : : /* Return identifier to look up for omp declare mapper. */
13840 : :
13841 : : tree
13842 : 607 : c_omp_mapper_id (tree mapper_id)
13843 : : {
13844 : 607 : const char *p = NULL;
13845 : :
13846 : 607 : const char prefix[] = "omp declare mapper ";
13847 : :
13848 : 607 : if (mapper_id == NULL_TREE)
13849 : : p = "<default>";
13850 : 19 : else if (TREE_CODE (mapper_id) == IDENTIFIER_NODE)
13851 : 19 : p = IDENTIFIER_POINTER (mapper_id);
13852 : : else
13853 : 0 : return error_mark_node;
13854 : :
13855 : 607 : size_t lenp = sizeof (prefix);
13856 : 607 : size_t len = strlen (p);
13857 : 607 : char *name = XALLOCAVEC (char, lenp + len);
13858 : 607 : memcpy (name, prefix, lenp - 1);
13859 : 607 : memcpy (name + lenp - 1, p, len + 1);
13860 : 607 : return get_identifier (name);
13861 : : }
13862 : :
13863 : : /* Lookup MAPPER_ID in the current scope, or create an artificial
13864 : : VAR_DECL, bind it into the current scope and return it. */
13865 : :
13866 : : tree
13867 : 62 : c_omp_mapper_decl (tree mapper_id)
13868 : : {
13869 : 62 : struct c_binding *b = I_SYMBOL_BINDING (mapper_id);
13870 : 62 : if (b != NULL && B_IN_CURRENT_SCOPE (b))
13871 : 22 : return b->decl;
13872 : :
13873 : 40 : tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
13874 : : mapper_id, integer_type_node);
13875 : 40 : DECL_ARTIFICIAL (decl) = 1;
13876 : 40 : DECL_EXTERNAL (decl) = 1;
13877 : 40 : TREE_STATIC (decl) = 1;
13878 : 40 : TREE_PUBLIC (decl) = 0;
13879 : 40 : bind (mapper_id, decl, current_scope, true, false, BUILTINS_LOCATION);
13880 : 40 : return decl;
13881 : : }
13882 : :
13883 : : /* Lookup MAPPER_ID in the first scope where it has entry for TYPE. */
13884 : :
13885 : : tree
13886 : 1669 : c_omp_mapper_lookup (tree mapper_id, tree type)
13887 : : {
13888 : 1669 : if (!RECORD_OR_UNION_TYPE_P (type))
13889 : : return NULL_TREE;
13890 : :
13891 : 545 : mapper_id = c_omp_mapper_id (mapper_id);
13892 : :
13893 : 545 : struct c_binding *b = I_SYMBOL_BINDING (mapper_id);
13894 : 551 : while (b)
13895 : : {
13896 : 91 : tree t;
13897 : 110 : for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
13898 : 104 : if (comptypes (TREE_PURPOSE (t), type))
13899 : 85 : return TREE_VALUE (t);
13900 : 6 : b = b->shadowed;
13901 : : }
13902 : : return NULL_TREE;
13903 : : }
13904 : :
13905 : : /* For C, we record a pointer to the mapper itself without wrapping it in an
13906 : : artificial function or similar. So, just return it. */
13907 : :
13908 : : tree
13909 : 82 : c_omp_extract_mapper_directive (tree mapper)
13910 : : {
13911 : 82 : return mapper;
13912 : : }
13913 : :
13914 : : /* For now we can handle singleton OMP_ARRAY_SECTIONs with custom mappers, but
13915 : : nothing more complicated. */
13916 : :
13917 : : tree
13918 : 350 : c_omp_map_array_section (location_t loc, tree t)
13919 : : {
13920 : 350 : tree low = TREE_OPERAND (t, 1);
13921 : 350 : tree len = TREE_OPERAND (t, 2);
13922 : :
13923 : 350 : if (len && integer_onep (len))
13924 : : {
13925 : 55 : t = TREE_OPERAND (t, 0);
13926 : :
13927 : 55 : if (!low)
13928 : 0 : low = integer_zero_node;
13929 : :
13930 : 55 : t = build_array_ref (loc, t, low);
13931 : : }
13932 : :
13933 : 350 : return t;
13934 : : }
13935 : :
13936 : : /* Helper function for below function. */
13937 : :
13938 : : static tree
13939 : 50684 : c_omp_scan_mapper_bindings_r (tree *tp, int *walk_subtrees, void *ptr)
13940 : : {
13941 : 50684 : tree t = *tp;
13942 : 50684 : omp_mapper_list<tree> *mlist = (omp_mapper_list<tree> *) ptr;
13943 : 50684 : tree aggr_type = NULL_TREE;
13944 : :
13945 : 50684 : if (TREE_CODE (t) == SIZEOF_EXPR
13946 : 50684 : || TREE_CODE (t) == ALIGNOF_EXPR)
13947 : : {
13948 : 0 : *walk_subtrees = 0;
13949 : 0 : return NULL_TREE;
13950 : : }
13951 : :
13952 : 50684 : if (TREE_CODE (t) == OMP_CLAUSE)
13953 : : return NULL_TREE;
13954 : :
13955 : 47400 : if (TREE_CODE (t) == COMPONENT_REF
13956 : 47400 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
13957 : 356 : aggr_type = TREE_TYPE (TREE_OPERAND (t, 0));
13958 : 47044 : else if ((TREE_CODE (t) == VAR_DECL
13959 : : || TREE_CODE (t) == PARM_DECL
13960 : : || TREE_CODE (t) == RESULT_DECL)
13961 : 5238 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t)))
13962 : 116 : aggr_type = TREE_TYPE (t);
13963 : :
13964 : 472 : if (aggr_type)
13965 : : {
13966 : 472 : tree mapper_fn = c_omp_mapper_lookup (NULL_TREE, aggr_type);
13967 : 472 : if (mapper_fn)
13968 : 65 : mlist->add_mapper (NULL_TREE, aggr_type, mapper_fn);
13969 : : }
13970 : :
13971 : : return NULL_TREE;
13972 : : }
13973 : :
13974 : : /* Scan an offload region's body, and record uses of struct- or union-typed
13975 : : variables. Add _mapper_binding_ fake clauses to *CLAUSES_PTR. */
13976 : :
13977 : : void
13978 : 1870 : c_omp_scan_mapper_bindings (location_t loc, tree *clauses_ptr, tree body)
13979 : : {
13980 : 1870 : hash_set<omp_name_type<tree>> seen_types;
13981 : 1870 : auto_vec<tree> mappers;
13982 : 1870 : omp_mapper_list<tree> mlist (&seen_types, &mappers);
13983 : :
13984 : 1870 : walk_tree_without_duplicates (&body, c_omp_scan_mapper_bindings_r, &mlist);
13985 : :
13986 : 1870 : unsigned int i;
13987 : 1870 : tree mapper;
13988 : 3779 : FOR_EACH_VEC_ELT (mappers, i, mapper)
13989 : 39 : c_omp_find_nested_mappers (&mlist, mapper);
13990 : :
13991 : 1935 : FOR_EACH_VEC_ELT (mappers, i, mapper)
13992 : : {
13993 : 39 : if (mapper == error_mark_node)
13994 : 0 : continue;
13995 : 39 : tree mapper_name = OMP_DECLARE_MAPPER_ID (mapper);
13996 : 39 : tree decl = OMP_DECLARE_MAPPER_DECL (mapper);
13997 : :
13998 : 39 : tree c = build_omp_clause (loc, OMP_CLAUSE__MAPPER_BINDING_);
13999 : 39 : OMP_CLAUSE__MAPPER_BINDING__ID (c) = mapper_name;
14000 : 39 : OMP_CLAUSE__MAPPER_BINDING__DECL (c) = decl;
14001 : 39 : OMP_CLAUSE__MAPPER_BINDING__MAPPER (c) = mapper;
14002 : :
14003 : 39 : OMP_CLAUSE_CHAIN (c) = *clauses_ptr;
14004 : 39 : *clauses_ptr = c;
14005 : : }
14006 : 1870 : }
14007 : :
14008 : : bool
14009 : 223 : c_check_in_current_scope (tree decl)
14010 : : {
14011 : 223 : struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
14012 : 223 : return b != NULL && B_IN_CURRENT_SCOPE (b);
14013 : : }
14014 : :
14015 : : /* Search for loop or switch names. BEFORE_LABELS is last statement before
14016 : : possible labels and SWITCH_P true for a switch, false for loops.
14017 : : Searches through last statements in cur_stmt_list, stops when seeing
14018 : : BEFORE_LABELs, or statement other than LABEL_EXPR or CASE_LABEL_EXPR.
14019 : : Returns number of loop/switch names found and if any are found, sets
14020 : : *LAST_P to the canonical loop/switch name LABEL_DECL. */
14021 : :
14022 : : int
14023 : 497811 : c_get_loop_names (tree before_labels, bool switch_p, tree *last_p)
14024 : : {
14025 : 497811 : *last_p = NULL_TREE;
14026 : 995622 : if (!building_stmt_list_p ()
14027 : 497811 : || !STATEMENT_LIST_HAS_LABEL (cur_stmt_list)
14028 : 518297 : || before_labels == void_list_node)
14029 : 477325 : return 0;
14030 : :
14031 : 20486 : int ret = 0;
14032 : 20486 : tree last = NULL_TREE;
14033 : 20486 : for (tree_stmt_iterator tsi = tsi_last (cur_stmt_list);
14034 : 28578 : !tsi_end_p (tsi); tsi_prev (&tsi))
14035 : : {
14036 : 27732 : tree stmt = tsi_stmt (tsi);
14037 : 27732 : if (stmt == before_labels)
14038 : : break;
14039 : 8092 : else if (TREE_CODE (stmt) == LABEL_EXPR)
14040 : : {
14041 : 1083 : if (last == NULL_TREE)
14042 : 889 : last = LABEL_EXPR_LABEL (stmt);
14043 : : else
14044 : : {
14045 : 194 : loop_names.safe_push (LABEL_EXPR_LABEL (stmt));
14046 : 194 : ++ret;
14047 : : }
14048 : : }
14049 : 7009 : else if (TREE_CODE (stmt) != CASE_LABEL_EXPR
14050 : 4710 : && TREE_CODE (stmt) != DEBUG_BEGIN_STMT)
14051 : : break;
14052 : : }
14053 : 20486 : if (last)
14054 : : {
14055 : 889 : if (switch_p)
14056 : 124 : C_DECL_SWITCH_NAME (last) = 1;
14057 : : else
14058 : 765 : C_DECL_LOOP_NAME (last) = 1;
14059 : 889 : loop_names.safe_push (last);
14060 : 889 : ++ret;
14061 : 889 : if (loop_names.length () > 16)
14062 : : {
14063 : 20 : unsigned int first = 0, i;
14064 : 20 : tree l, c = NULL_TREE;
14065 : 20 : if (loop_names_hash == NULL)
14066 : 8 : loop_names_hash = new decl_tree_map (ret);
14067 : : else
14068 : 12 : first = loop_names.length () - ret;
14069 : 210 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14070 : : {
14071 : 170 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14072 : 48 : c = l;
14073 : 170 : gcc_checking_assert (c);
14074 : 170 : loop_names_hash->put (l, c);
14075 : 170 : if (i == first)
14076 : : break;
14077 : : }
14078 : : }
14079 : 889 : *last_p = last;
14080 : : }
14081 : : return ret;
14082 : : }
14083 : :
14084 : : /* Undoes what get_loop_names did when it returned NUM_NAMES. */
14085 : :
14086 : : void
14087 : 889 : c_release_loop_names (int num_names)
14088 : : {
14089 : 889 : unsigned len = loop_names.length () - num_names;
14090 : 889 : if (loop_names_hash)
14091 : : {
14092 : 20 : if (len <= 16)
14093 : : {
14094 : 8 : delete loop_names_hash;
14095 : 8 : loop_names_hash = NULL;
14096 : : }
14097 : : else
14098 : : {
14099 : 12 : unsigned int i;
14100 : 12 : tree l;
14101 : 42 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14102 : : {
14103 : 30 : loop_names_hash->remove (l);
14104 : 30 : if (i == len)
14105 : : break;
14106 : : }
14107 : : }
14108 : : }
14109 : 889 : loop_names.truncate (len);
14110 : 889 : }
14111 : :
14112 : : /* Finish processing of break or continue identifier operand.
14113 : : NAME is the identifier operand of break or continue and
14114 : : IS_BREAK is true iff it is break stmt. Returns the operand
14115 : : to use for BREAK_STMT or CONTINUE_STMT, either NULL_TREE or
14116 : : canonical loop/switch name LABEL_DECL. */
14117 : :
14118 : : tree
14119 : 316 : c_finish_bc_name (location_t loc, tree name, bool is_break)
14120 : : {
14121 : 316 : tree label = NULL_TREE, lab;
14122 : 457 : pedwarn_c23 (loc, OPT_Wpedantic,
14123 : : "ISO C does not support %qs statement with an identifier "
14124 : : "operand before C2Y", is_break ? "break" : "continue");
14125 : :
14126 : : /* If I_LABEL_DECL is NULL or not from current function, don't waste time
14127 : : trying to find it among loop_names, it can't be there. */
14128 : 316 : if (!loop_names.is_empty ()
14129 : 296 : && current_function_scope
14130 : 296 : && (lab = I_LABEL_DECL (name))
14131 : 290 : && DECL_CONTEXT (lab) == current_function_decl)
14132 : : {
14133 : 280 : unsigned int i;
14134 : 280 : tree l, c = NULL_TREE;
14135 : 280 : if (loop_names_hash)
14136 : : {
14137 : 86 : if (tree *val = loop_names_hash->get (lab))
14138 : 86 : label = *val;
14139 : : }
14140 : : else
14141 : 565 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14142 : : {
14143 : 363 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14144 : : c = l;
14145 : 363 : gcc_checking_assert (c);
14146 : 363 : if (l == lab)
14147 : : {
14148 : : label = c;
14149 : : break;
14150 : : }
14151 : : }
14152 : 280 : if (label)
14153 : 272 : TREE_USED (lab) = 1;
14154 : : }
14155 : 272 : if (label == NULL_TREE)
14156 : : {
14157 : 44 : auto_vec<const char *> candidates;
14158 : 44 : unsigned int i;
14159 : 44 : tree l, c = NULL_TREE;
14160 : 115 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14161 : : {
14162 : 28 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14163 : : c = l;
14164 : 28 : gcc_checking_assert (c);
14165 : 42 : if (is_break || C_DECL_LOOP_NAME (c))
14166 : 25 : candidates.safe_push (IDENTIFIER_POINTER (DECL_NAME (l)));
14167 : : }
14168 : 44 : const char *hint = find_closest_string (IDENTIFIER_POINTER (name),
14169 : : &candidates);
14170 : 44 : if (hint)
14171 : : {
14172 : 22 : gcc_rich_location richloc (loc);
14173 : 22 : richloc.add_fixit_replace (hint);
14174 : 22 : if (is_break)
14175 : 11 : error_at (&richloc, "%<break%> statement operand %qE does not "
14176 : : "refer to a named loop or %<switch%>; "
14177 : : "did you mean %qs?", name, hint);
14178 : : else
14179 : 11 : error_at (&richloc, "%<continue%> statement operand %qE does not "
14180 : : "refer to a named loop; did you mean %qs?",
14181 : : name, hint);
14182 : 22 : }
14183 : 22 : else if (is_break)
14184 : 12 : error_at (loc, "%<break%> statement operand %qE does not refer to a "
14185 : : "named loop or %<switch%>", name);
14186 : : else
14187 : 10 : error_at (loc, "%<continue%> statement operand %qE does not refer to "
14188 : : "a named loop", name);
14189 : 44 : }
14190 : 272 : else if (!C_DECL_LOOP_NAME (label) && !is_break)
14191 : : {
14192 : 2 : auto_diagnostic_group d;
14193 : 2 : error_at (loc, "%<continue%> statement operand %qE refers to a named "
14194 : : "%<switch%>", name);
14195 : 2 : inform (DECL_SOURCE_LOCATION (label), "%<switch%> name defined here");
14196 : 2 : label = NULL_TREE;
14197 : 2 : }
14198 : 270 : else if (!C_DECL_LOOP_SWITCH_NAME_VALID (label))
14199 : : {
14200 : 18 : auto_diagnostic_group d;
14201 : 18 : if (C_DECL_LOOP_NAME (label))
14202 : : {
14203 : 16 : error_at (loc, "%qs statement operand %qE refers to a loop outside "
14204 : : "of its body", is_break ? "break" : "continue", name);
14205 : 16 : inform (DECL_SOURCE_LOCATION (label), "loop name defined here");
14206 : : }
14207 : : else
14208 : : {
14209 : 2 : error_at (loc, "%<break%> statement operand %qE refers to a "
14210 : : "%<switch%> outside of its body", name);
14211 : 2 : inform (DECL_SOURCE_LOCATION (label),
14212 : : "%<switch%> name defined here");
14213 : : }
14214 : 18 : label = NULL_TREE;
14215 : 18 : }
14216 : 252 : else if (label == loop_names.last () && (in_statement & IN_NAMED_STMT) != 0)
14217 : : /* If it is just a fancy reference to the innermost construct, handle it
14218 : : just like break; or continue; though tracking cheaply what is the
14219 : : innermost loop for continue when nested in switches would require
14220 : : another global variable and updating it. */
14221 : : label = NULL_TREE;
14222 : : else
14223 : 98 : C_DECL_LOOP_SWITCH_NAME_USED (label) = 1;
14224 : 316 : return label;
14225 : : }
14226 : :
14227 : : #include "gt-c-c-decl.h"
|