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 : 4460520002 : i_symbol_binding (tree node)
256 : : {
257 : 4460520002 : struct lang_identifier *lid
258 : 4460520002 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
259 : :
260 : 4460520002 : if (lid->symbol_binding == NULL
261 : 1641548339 : && c_binding_oracle != NULL
262 : 4460520002 : && !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 : 4460520002 : 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 : 4436809 : i_tag_binding (tree node)
285 : : {
286 : 4436809 : struct lang_identifier *lid
287 : 4436809 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
288 : :
289 : 4436809 : if (lid->tag_binding == NULL
290 : 1228553 : && c_binding_oracle != NULL
291 : 4436809 : && !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 : 4436809 : 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 : 357499 : i_label_binding (tree node)
314 : : {
315 : 357499 : struct lang_identifier *lid
316 : 357499 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
317 : :
318 : 357499 : if (lid->label_binding == NULL
319 : 47308 : && c_binding_oracle != NULL
320 : 357499 : && !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 : 357499 : 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 : 1152807 : 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 : 4226585 : c_struct_hasher::hash (tree type)
658 : : {
659 : 4226585 : inchash::hash hstate;
660 : :
661 : 4226585 : hstate.add_int (TREE_CODE (type));
662 : 4226585 : tree tag = c_type_tag (type);
663 : 4226585 : hstate.add_object (tag);
664 : :
665 : 4226585 : return hstate.end ();
666 : : }
667 : :
668 : : /* Compare two RECORD or UNION types. */
669 : : bool
670 : 14286896 : c_struct_hasher::equal (tree t1, tree t2)
671 : : {
672 : 14286896 : 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 : 91500564 : add_stmt (tree t)
699 : : {
700 : 91500564 : enum tree_code code = TREE_CODE (t);
701 : :
702 : 91500564 : if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
703 : : {
704 : 90802600 : if (!EXPR_HAS_LOCATION (t))
705 : 154704 : 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 : 91500564 : if (!building_stmt_list_p ())
711 : 1 : push_stmt_list ();
712 : :
713 : 91500564 : if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
714 : 1073697 : STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
715 : :
716 : 91500564 : append_to_statement_list_force (t, &cur_stmt_list);
717 : :
718 : 91500564 : 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 : 880188079 : decl_jump_unsafe (tree decl)
730 : : {
731 : 880188079 : 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 : 880186025 : if (VAR_P (decl) && C_DECL_COMPOUND_LITERAL_P (decl))
740 : : return false;
741 : :
742 : 879288019 : if (flag_openmp
743 : 11751609 : && VAR_P (decl)
744 : 879319024 : && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl)))
745 : : return true;
746 : :
747 : : /* Always warn about crossing variably modified types. */
748 : 869428696 : if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
749 : 891121481 : && 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 : 879247638 : if (warn_jump_misses_init
755 : 5948401 : && VAR_P (decl)
756 : 10197 : && !TREE_STATIC (decl)
757 : 879253079 : && 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 : 880038979 : bind (tree name, tree decl, struct c_scope *scope, bool invisible,
804 : : bool nested, location_t locus)
805 : : {
806 : 880038979 : struct c_binding *b, **here;
807 : :
808 : 880038979 : if (binding_freelist)
809 : : {
810 : 224803324 : b = binding_freelist;
811 : 224803324 : binding_freelist = b->prev;
812 : : }
813 : : else
814 : 655235655 : b = ggc_alloc<c_binding> ();
815 : :
816 : 880038979 : b->shadowed = 0;
817 : 880038979 : b->decl = decl;
818 : 880038979 : b->id = name;
819 : 880038979 : b->depth = scope->depth;
820 : 880038979 : b->invisible = invisible;
821 : 880038979 : b->nested = nested;
822 : 880038979 : b->inner_comp = 0;
823 : 880038979 : b->in_struct = 0;
824 : 880038979 : b->locus = locus;
825 : :
826 : 880038979 : b->u.type = NULL;
827 : :
828 : 880038979 : b->prev = scope->bindings;
829 : 880038979 : scope->bindings = b;
830 : :
831 : 880038979 : if (decl_jump_unsafe (decl))
832 : 24089 : scope->has_jump_unsafe_decl = 1;
833 : :
834 : 880038979 : if (!name)
835 : : return;
836 : :
837 : 871594937 : switch (TREE_CODE (decl))
838 : : {
839 : 23679 : case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
840 : 613570 : case ENUMERAL_TYPE:
841 : 613570 : case UNION_TYPE:
842 : 613570 : case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
843 : 870957688 : case VAR_DECL:
844 : 870957688 : case FUNCTION_DECL:
845 : 870957688 : case TYPE_DECL:
846 : 870957688 : case CONST_DECL:
847 : 870957688 : case PARM_DECL:
848 : 870957688 : 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 : 871594981 : while (*here && (*here)->depth > scope->depth)
858 : 44 : here = &(*here)->shadowed;
859 : :
860 : 871594937 : b->shadowed = *here;
861 : 871594937 : *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 : 863232830 : free_binding_and_advance (struct c_binding *b)
870 : : {
871 : 863232830 : struct c_binding *prev = b->prev;
872 : :
873 : 863232830 : memset (b, 0, sizeof (struct c_binding));
874 : 863232830 : b->prev = binding_freelist;
875 : 863232830 : binding_freelist = b;
876 : :
877 : 863232830 : 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 : 23679 : bind_label (tree name, tree label, struct c_scope *scope,
884 : : struct c_label_vars *label_vars)
885 : : {
886 : 23679 : struct c_binding *b;
887 : :
888 : 23679 : bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
889 : : UNKNOWN_LOCATION);
890 : :
891 : 23679 : scope->has_label_bindings = true;
892 : :
893 : 23679 : b = scope->bindings;
894 : 23679 : gcc_assert (b->decl == label);
895 : 23679 : label_vars->shadowed = b->u.label;
896 : 23679 : b->u.label = label_vars;
897 : 23679 : }
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 : 7446 : c_finish_incomplete_decl (tree decl)
904 : : {
905 : 7446 : if (VAR_P (decl))
906 : : {
907 : 7446 : tree type = TREE_TYPE (decl);
908 : 7446 : if (type != error_mark_node
909 : 7372 : && TREE_CODE (type) == ARRAY_TYPE
910 : 7197 : && !DECL_EXTERNAL (decl)
911 : 7524 : && 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 : 7446 : }
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 : 104332 : check_inline_statics (void)
946 : : {
947 : 104332 : struct c_inline_static *csi;
948 : 104368 : 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 : 104332 : c_inline_statics = NULL;
968 : 104332 : }
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 : 133525 : set_spot_bindings (struct c_spot_bindings *p, bool defining)
975 : : {
976 : 0 : if (defining)
977 : : {
978 : 126535 : p->scope = current_scope;
979 : 16689 : p->bindings_in_scope = current_scope->bindings;
980 : : }
981 : : else
982 : : {
983 : 6990 : p->scope = NULL;
984 : 6990 : p->bindings_in_scope = NULL;
985 : : }
986 : 133525 : p->stmt_exprs = 0;
987 : 133525 : 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 : 470605277 : 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 : 204791 : p->scope = scope->outer;
1007 : 204791 : p->bindings_in_scope = p->scope->bindings;
1008 : :
1009 : 151150 : 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 : 2065582 : global_bindings_p (void)
1046 : : {
1047 : 2065582 : 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 : 34746 : 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 : 34746 : return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
1059 : : }
1060 : :
1061 : : void
1062 : 51924 : keep_next_level (void)
1063 : : {
1064 : 51924 : keep_next_level_flag = true;
1065 : 51924 : }
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 : 589902 : float_const_decimal64_p (void)
1087 : : {
1088 : 589902 : return current_scope->float_const_decimal64;
1089 : : }
1090 : :
1091 : : /* Identify this scope as currently being filled with parameters. */
1092 : :
1093 : : void
1094 : 84797714 : declare_parm_level (void)
1095 : : {
1096 : 84797714 : current_scope->parm_flag = true;
1097 : 84797714 : }
1098 : :
1099 : : void
1100 : 125047044 : push_scope (void)
1101 : : {
1102 : 125047044 : 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 : 35380161 : current_scope->parm_flag = false;
1115 : 35380161 : current_scope->function_body = true;
1116 : 35380161 : current_scope->keep = true;
1117 : 35380161 : current_scope->outer_function = current_function_scope;
1118 : 35380161 : current_function_scope = current_scope;
1119 : :
1120 : 35380161 : keep_next_level_flag = false;
1121 : 35380161 : next_is_function_body = false;
1122 : :
1123 : : /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1124 : 35380161 : if (current_scope->outer)
1125 : 35380161 : current_scope->float_const_decimal64
1126 : 35380161 : = current_scope->outer->float_const_decimal64;
1127 : : else
1128 : 0 : current_scope->float_const_decimal64 = false;
1129 : : }
1130 : : else
1131 : : {
1132 : 89666883 : struct c_scope *scope;
1133 : 89666883 : if (scope_freelist)
1134 : : {
1135 : 88833359 : scope = scope_freelist;
1136 : 88833359 : scope_freelist = scope->outer;
1137 : : }
1138 : : else
1139 : 833524 : scope = ggc_cleared_alloc<c_scope> ();
1140 : :
1141 : : /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1142 : 89666883 : if (current_scope)
1143 : 89557028 : scope->float_const_decimal64 = current_scope->float_const_decimal64;
1144 : : else
1145 : 109855 : scope->float_const_decimal64 = false;
1146 : :
1147 : 89666883 : scope->keep = keep_next_level_flag;
1148 : 89666883 : scope->outer = current_scope;
1149 : 89666883 : 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 : 89666883 : 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 : 89666883 : current_scope = scope;
1160 : 89666883 : keep_next_level_flag = false;
1161 : : }
1162 : 125047044 : }
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 : 89660518 : update_label_decls (struct c_scope *scope)
1171 : : {
1172 : 89660518 : struct c_scope *s;
1173 : :
1174 : 89660518 : s = scope;
1175 : 325249325 : while (s != NULL)
1176 : : {
1177 : 275639103 : if (s->has_label_bindings)
1178 : : {
1179 : 231164 : struct c_binding *b;
1180 : :
1181 : 2133761 : for (b = s->bindings; b != NULL; b = b->prev)
1182 : : {
1183 : 1902597 : struct c_label_vars *label_vars;
1184 : 1902597 : struct c_binding *b1;
1185 : 1902597 : bool hjud;
1186 : 1902597 : unsigned int ix;
1187 : 1902597 : struct c_goto_bindings *g;
1188 : :
1189 : 1902597 : if (TREE_CODE (b->decl) != LABEL_DECL)
1190 : 1400691 : continue;
1191 : 501906 : label_vars = b->u.label;
1192 : :
1193 : 501906 : b1 = label_vars->label_bindings.bindings_in_scope;
1194 : 501906 : if (label_vars->label_bindings.scope == NULL)
1195 : : hjud = false;
1196 : : else
1197 : 193195 : hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1198 : 501906 : if (update_spot_bindings (scope, &label_vars->label_bindings))
1199 : : {
1200 : : /* This label is defined in this scope. */
1201 : 53641 : 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 : 472298405 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1218 : 470254521 : update_spot_bindings (scope, &g->goto_bindings);
1219 : : }
1220 : : }
1221 : :
1222 : : /* Don't search beyond the current function. */
1223 : 275639103 : if (s == current_function_scope)
1224 : : break;
1225 : :
1226 : 235588807 : s = s->outer;
1227 : : }
1228 : 89660518 : }
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 : 89660518 : pop_scope (void)
1237 : : {
1238 : 89660518 : struct c_scope *scope = current_scope;
1239 : 89660518 : tree block, context, p;
1240 : 89660518 : struct c_binding *b;
1241 : :
1242 : 89660518 : bool functionbody = scope->function_body;
1243 : 89660518 : bool keep = functionbody || scope->keep || scope->bindings;
1244 : :
1245 : 89660518 : update_label_decls (scope);
1246 : :
1247 : : /* If appropriate, create a BLOCK to record the decls for the life
1248 : : of this function. */
1249 : 89660518 : block = NULL_TREE;
1250 : 89660518 : if (keep)
1251 : : {
1252 : 35863382 : block = make_node (BLOCK);
1253 : 35863382 : BLOCK_SUBBLOCKS (block) = scope->blocks;
1254 : 35863382 : TREE_USED (block) = 1;
1255 : :
1256 : : /* In each subblock, record that this is its superior. */
1257 : 36138624 : for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1258 : 275242 : BLOCK_SUPERCONTEXT (p) = block;
1259 : :
1260 : 35863382 : 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 : 89660518 : if (scope->function_body)
1277 : 35380160 : context = current_function_decl;
1278 : 54280358 : else if (scope == file_scope)
1279 : : {
1280 : 103991 : tree file_decl
1281 : 103991 : = build_translation_unit_decl (get_identifier (main_input_filename));
1282 : 103991 : context = file_decl;
1283 : 103991 : debug_hooks->register_main_translation_unit (file_decl);
1284 : : }
1285 : : else
1286 : : context = block;
1287 : :
1288 : : /* Clear all bindings in this scope. */
1289 : 832715243 : for (b = scope->bindings; b; b = free_binding_and_advance (b))
1290 : : {
1291 : 743054725 : p = b->decl;
1292 : 743054725 : switch (TREE_CODE (p))
1293 : : {
1294 : 23679 : case LABEL_DECL:
1295 : : /* Warnings for unused labels, errors for undefined labels. */
1296 : 23679 : 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 : 23616 : warn_for_unused_label (p);
1303 : :
1304 : : /* Labels go in BLOCK_VARS. */
1305 : 23679 : DECL_CHAIN (p) = BLOCK_VARS (block);
1306 : 23679 : BLOCK_VARS (block) = p;
1307 : 23679 : gcc_assert (I_LABEL_BINDING (b->id) == b);
1308 : 23679 : I_LABEL_BINDING (b->id) = b->shadowed;
1309 : :
1310 : : /* Also pop back to the shadowed label_vars. */
1311 : 23679 : release_tree_vector (b->u.label->decls_in_scope);
1312 : 23679 : b->u.label = b->u.label->shadowed;
1313 : 23679 : break;
1314 : :
1315 : 1391733 : case ENUMERAL_TYPE:
1316 : 1391733 : case UNION_TYPE:
1317 : 1391733 : 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 : 1391733 : if (b->id)
1322 : : {
1323 : 611749 : gcc_assert (I_TAG_BINDING (b->id) == b);
1324 : 611749 : I_TAG_BINDING (b->id) = b->shadowed;
1325 : : }
1326 : : break;
1327 : :
1328 : 616379149 : case FUNCTION_DECL:
1329 : : /* Propagate TREE_ADDRESSABLE from nested functions to their
1330 : : containing functions. */
1331 : 616379149 : if (!TREE_ASM_WRITTEN (p)
1332 : 616379149 : && DECL_INITIAL (p) != NULL_TREE
1333 : 70414882 : && TREE_ADDRESSABLE (p)
1334 : 3608387 : && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1335 : 616379149 : && DECL_ABSTRACT_ORIGIN (p) != p)
1336 : 0 : TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1337 : 616379149 : if (!TREE_PUBLIC (p)
1338 : 344770 : && !DECL_INITIAL (p)
1339 : 141 : && !b->nested
1340 : 110 : && scope != file_scope
1341 : 616379157 : && scope != external_scope)
1342 : : {
1343 : 8 : error ("nested function %q+D declared but never defined", p);
1344 : 8 : undef_nested_function = true;
1345 : : }
1346 : 616379141 : else if (DECL_DECLARED_INLINE_P (p)
1347 : 69250747 : && TREE_PUBLIC (p)
1348 : 685474577 : && !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 : 76 : if (!flag_gnu89_inline
1354 : 51 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1355 : 111 : && scope == external_scope)
1356 : 13 : pedwarn (input_location, 0,
1357 : : "inline function %q+D declared but never defined", p);
1358 : 76 : DECL_EXTERNAL (p) = 1;
1359 : : }
1360 : :
1361 : 616379149 : goto common_symbol;
1362 : :
1363 : 10749180 : case VAR_DECL:
1364 : : /* Warnings for unused variables. */
1365 : 8188626 : if ((!TREE_USED (p) || !DECL_READ_P (p))
1366 : 2588812 : && !warning_suppressed_p (p, OPT_Wunused_but_set_variable_)
1367 : 2588306 : && !DECL_IN_SYSTEM_HEADER (p)
1368 : 2510907 : && DECL_NAME (p)
1369 : 2510907 : && !DECL_ARTIFICIAL (p)
1370 : 2510542 : && scope != file_scope
1371 : 12372535 : && scope != external_scope)
1372 : : {
1373 : 737777 : if (!TREE_USED (p))
1374 : : {
1375 : 732477 : warning (OPT_Wunused_variable, "unused variable %q+D", p);
1376 : 732477 : suppress_warning (p, OPT_Wunused_variable);
1377 : : }
1378 : 5300 : else if (DECL_CONTEXT (p) == current_function_decl)
1379 : 5281 : warning_at (DECL_SOURCE_LOCATION (p),
1380 : 5281 : OPT_Wunused_but_set_variable_,
1381 : : "variable %qD set but not used", p);
1382 : : }
1383 : :
1384 : 10749180 : 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 : 644128238 : case TYPE_DECL:
1392 : 644128238 : case CONST_DECL:
1393 : 10749178 : common_symbol:
1394 : : /* All of these go in BLOCK_VARS, but only if this is the
1395 : : binding in the home scope. */
1396 : 644128238 : if (!b->nested)
1397 : : {
1398 : 387773865 : DECL_CHAIN (p) = BLOCK_VARS (block);
1399 : 387773865 : BLOCK_VARS (block) = p;
1400 : : }
1401 : 256354373 : 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 : 14578 : tree extp = copy_node (p);
1406 : :
1407 : 14578 : DECL_EXTERNAL (extp) = 1;
1408 : 14578 : TREE_STATIC (extp) = 0;
1409 : 14578 : TREE_PUBLIC (extp) = 1;
1410 : 14578 : DECL_INITIAL (extp) = NULL_TREE;
1411 : 14578 : DECL_LANG_SPECIFIC (extp) = NULL;
1412 : 14578 : DECL_CONTEXT (extp) = current_function_decl;
1413 : 14578 : if (TREE_CODE (p) == FUNCTION_DECL)
1414 : : {
1415 : 13041 : DECL_RESULT (extp) = NULL_TREE;
1416 : 13041 : DECL_SAVED_TREE (extp) = NULL_TREE;
1417 : 13041 : DECL_STRUCT_FUNCTION (extp) = NULL;
1418 : : }
1419 : 14578 : if (b->locus != UNKNOWN_LOCATION)
1420 : 14578 : DECL_SOURCE_LOCATION (extp) = b->locus;
1421 : 14578 : DECL_CHAIN (extp) = BLOCK_VARS (block);
1422 : 14578 : 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 : 644128238 : if (scope == file_scope)
1428 : 267410183 : DECL_CONTEXT (p) = context;
1429 : :
1430 : 741639313 : 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 : 741639313 : case PARM_DECL:
1437 : 741639313 : 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 : 741639313 : if (b->id)
1441 : : {
1442 : 738510866 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1443 : 738510866 : I_SYMBOL_BINDING (b->id) = b->shadowed;
1444 : 738510866 : if (b->shadowed && b->shadowed->u.type)
1445 : 4586578 : 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 : 89660518 : if ((scope->function_body || scope == file_scope) && context)
1457 : : {
1458 : 35484149 : DECL_INITIAL (context) = block;
1459 : 35484149 : BLOCK_SUPERCONTEXT (block) = context;
1460 : : }
1461 : 54176369 : else if (scope->outer)
1462 : : {
1463 : 54072378 : if (block)
1464 : 275242 : 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 : 53797136 : else if (scope->blocks)
1469 : 351633 : SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1470 : : }
1471 : :
1472 : : /* Pop the current scope, and free the structure for reuse. */
1473 : 89660518 : current_scope = scope->outer;
1474 : 89660518 : if (scope->function_body)
1475 : 35380160 : current_function_scope = scope->outer_function;
1476 : :
1477 : 89660518 : memset (scope, 0, sizeof (struct c_scope));
1478 : 89660518 : scope->outer = scope_freelist;
1479 : 89660518 : scope_freelist = scope;
1480 : :
1481 : 89660518 : return block;
1482 : : }
1483 : :
1484 : : void
1485 : 104490 : push_file_scope (void)
1486 : : {
1487 : 104490 : tree decl;
1488 : :
1489 : 104490 : if (file_scope)
1490 : : return;
1491 : :
1492 : 104490 : push_scope ();
1493 : 104490 : file_scope = current_scope;
1494 : :
1495 : 104490 : start_fname_decls ();
1496 : :
1497 : 208143587 : for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1498 : 208039097 : bind (DECL_NAME (decl), decl, file_scope,
1499 : 208039097 : /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1500 : : }
1501 : :
1502 : : void
1503 : 104332 : pop_file_scope (void)
1504 : : {
1505 : : /* In case there were missing closebraces, get us back to the global
1506 : : binding level. */
1507 : 104337 : while (current_scope != file_scope)
1508 : 5 : 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 : 104332 : finish_fname_decls ();
1514 : :
1515 : 104332 : 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 : 104332 : if (pch_file)
1520 : : {
1521 : 341 : c_common_write_pch ();
1522 : : /* Ensure even the callers don't try to finalize the CU. */
1523 : 341 : flag_syntax_only = 1;
1524 : 341 : return;
1525 : : }
1526 : :
1527 : : /* Pop off the file scope and close this translation unit. */
1528 : 103991 : pop_scope ();
1529 : 103991 : file_scope = 0;
1530 : :
1531 : 103991 : 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 : 655 : start_underspecified_init (location_t loc, tree name)
1548 : : {
1549 : 655 : bool prev = in_underspecified_init;
1550 : 655 : bool ok;
1551 : 655 : if (name == NULL_TREE)
1552 : : ok = true;
1553 : : else
1554 : : {
1555 : 423 : tree decl = build_decl (loc, VAR_DECL, name, error_mark_node);
1556 : 423 : C_DECL_UNDERSPECIFIED (decl) = 1;
1557 : 423 : struct c_scope *scope = current_scope;
1558 : 423 : struct c_binding *b = I_SYMBOL_BINDING (name);
1559 : 423 : 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 : 419 : bind (name, decl, scope, false, false, loc);
1568 : 419 : ok = true;
1569 : : }
1570 : : }
1571 : 655 : in_underspecified_init = true;
1572 : 655 : 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 : 655 : finish_underspecified_init (tree name, unsigned int prev_state)
1582 : : {
1583 : 655 : 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 : 419 : struct c_scope *scope = current_scope;
1588 : 419 : struct c_binding *b = I_SYMBOL_BINDING (name);
1589 : 419 : gcc_assert (b);
1590 : 419 : gcc_assert (B_IN_SCOPE (b, scope));
1591 : 419 : gcc_assert (VAR_P (b->decl));
1592 : 419 : gcc_assert (C_DECL_UNDERSPECIFIED (b->decl));
1593 : 419 : I_SYMBOL_BINDING (name) = b->shadowed;
1594 : : /* In erroneous cases there may be other bindings added to this
1595 : : scope during the initializer. */
1596 : 419 : struct c_binding **p = &scope->bindings;
1597 : 474 : while (*p != b)
1598 : 55 : p = &((*p)->prev);
1599 : 419 : *p = free_binding_and_advance (*p);
1600 : : }
1601 : 655 : in_underspecified_init = (prev_state & (1u << 1)) >> 1;
1602 : 655 : }
1603 : :
1604 : : /* Adjust the bindings for the start of a statement expression. */
1605 : :
1606 : : void
1607 : 34725 : c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1608 : : {
1609 : 34725 : struct c_scope *scope;
1610 : :
1611 : 234616 : for (scope = current_scope; scope != NULL; scope = scope->outer)
1612 : : {
1613 : 199891 : struct c_binding *b;
1614 : :
1615 : 199891 : if (!scope->has_label_bindings)
1616 : 197492 : 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 : 34725 : if (switch_bindings != NULL)
1634 : 173 : ++switch_bindings->stmt_exprs;
1635 : 34725 : }
1636 : :
1637 : : /* Adjust the bindings for the end of a statement expression. */
1638 : :
1639 : : void
1640 : 34725 : c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1641 : : {
1642 : 34725 : struct c_scope *scope;
1643 : :
1644 : 199891 : for (scope = current_scope; scope != NULL; scope = scope->outer)
1645 : : {
1646 : 165166 : struct c_binding *b;
1647 : :
1648 : 165166 : if (!scope->has_label_bindings)
1649 : 162532 : 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 : 34725 : if (switch_bindings != NULL)
1679 : : {
1680 : 173 : --switch_bindings->stmt_exprs;
1681 : 173 : gcc_assert (switch_bindings->stmt_exprs >= 0);
1682 : : }
1683 : 34725 : }
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 : 1395687 : pushtag (location_t loc, tree name, tree type)
1695 : : {
1696 : : /* Record the identifier as the type's name if it has none. */
1697 : 1395687 : if (name && !TYPE_NAME (type))
1698 : 613547 : TYPE_NAME (type) = name;
1699 : 1395687 : 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 : 1395687 : 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 : 1395687 : TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1714 : :
1715 : 1395687 : 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 : 1395687 : }
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 : 615388 : types_close_enough_to_match (tree t1, tree t2)
1804 : : {
1805 : 615388 : return (TYPE_MODE (t1) == TYPE_MODE (t2)
1806 : 614933 : && POINTER_TYPE_P (t1) == POINTER_TYPE_P (t2)
1807 : 2460034 : && 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 : 142758 : match_builtin_function_types (tree newtype, tree oldtype,
1818 : : tree *strict, unsigned *argno)
1819 : : {
1820 : 142758 : *argno = 0;
1821 : 142758 : *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 : 142758 : tree oldrettype = TREE_TYPE (oldtype);
1826 : 142758 : tree newrettype = TREE_TYPE (newtype);
1827 : :
1828 : 142758 : 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 : 142396 : if (!comptypes (TYPE_MAIN_VARIANT (oldrettype),
1834 : 142396 : TYPE_MAIN_VARIANT (newrettype)))
1835 : 39 : *strict = oldrettype;
1836 : :
1837 : 142396 : tree oldargs = TYPE_ARG_TYPES (oldtype);
1838 : 142396 : tree newargs = TYPE_ARG_TYPES (newtype);
1839 : 142396 : tree tryargs = newargs;
1840 : :
1841 : 142396 : const unsigned nlst = ARRAY_SIZE (last_structptr_types);
1842 : 142396 : const unsigned nbst = ARRAY_SIZE (builtin_structptr_types);
1843 : :
1844 : 142396 : gcc_checking_assert (nlst == nbst);
1845 : :
1846 : 614829 : for (unsigned i = 1; oldargs || newargs; ++i)
1847 : : {
1848 : 472689 : if (!oldargs
1849 : 472689 : || !newargs
1850 : 472634 : || !TREE_VALUE (oldargs)
1851 : 945323 : || !TREE_VALUE (newargs))
1852 : : return NULL_TREE;
1853 : :
1854 : 472634 : tree oldtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1855 : 472634 : tree newtype = TREE_VALUE (newargs);
1856 : 472634 : if (newtype == error_mark_node)
1857 : : return NULL_TREE;
1858 : 472630 : newtype = TYPE_MAIN_VARIANT (newtype);
1859 : :
1860 : 472630 : if (!types_close_enough_to_match (oldtype, newtype))
1861 : : return NULL_TREE;
1862 : :
1863 : 472486 : unsigned j = nbst;
1864 : 472486 : 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 : 965106 : for (j = 0; j < nbst; ++j)
1869 : : {
1870 : 850272 : if (TREE_VALUE (oldargs) != builtin_structptr_types[j].node)
1871 : 708340 : 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 : 141932 : if (last_structptr_types[j])
1877 : : {
1878 : 124675 : if (!comptypes (last_structptr_types[j], newtype))
1879 : : {
1880 : 2 : *argno = i;
1881 : 2 : *strict = last_structptr_types[j];
1882 : : }
1883 : : }
1884 : : else
1885 : 17257 : last_structptr_types[j] = newtype;
1886 : : break;
1887 : : }
1888 : :
1889 : 472486 : if (j == nbst && !comptypes (oldtype, newtype))
1890 : : {
1891 : 243 : 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 : 190 : if (!*strict)
1904 : : {
1905 : 186 : *argno = i;
1906 : 186 : *strict = oldtype;
1907 : : }
1908 : : }
1909 : :
1910 : 472433 : oldargs = TREE_CHAIN (oldargs);
1911 : 472433 : newargs = TREE_CHAIN (newargs);
1912 : : }
1913 : :
1914 : 142140 : tree trytype = c_build_function_type (newrettype, tryargs);
1915 : :
1916 : : /* Allow declaration to change transaction_safe attribute. */
1917 : 142140 : tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1918 : 142140 : tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1919 : 142140 : tree newattrs = TYPE_ATTRIBUTES (newtype);
1920 : 142140 : tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1921 : 142140 : if (oldtsafe && !newtsafe)
1922 : 0 : oldattrs = remove_attribute ("transaction_safe", oldattrs);
1923 : 142140 : else if (newtsafe && !oldtsafe)
1924 : 7 : oldattrs = tree_cons (get_identifier ("transaction_safe"),
1925 : : NULL_TREE, oldattrs);
1926 : :
1927 : 142140 : 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 : 170 : diagnose_arglist_conflict (tree newdecl, tree olddecl,
1935 : : tree newtype, tree oldtype)
1936 : : {
1937 : 170 : tree t;
1938 : :
1939 : 170 : if (TREE_CODE (olddecl) != FUNCTION_DECL
1940 : 95 : || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1941 : 291 : || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1942 : 64 : || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1943 : 153 : 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 : 409 : locate_old_decl (tree decl)
2050 : : {
2051 : 409 : if (TREE_CODE (decl) == FUNCTION_DECL
2052 : 195 : && fndecl_built_in_p (decl)
2053 : 412 : && !C_DECL_DECLARED_BUILTIN (decl))
2054 : : ;
2055 : 409 : 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 : 291 : 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 : 275 : inform (input_location,
2065 : : "previous declaration of %q+D with type %qT",
2066 : 275 : decl, TREE_TYPE (decl));
2067 : 409 : }
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 : 1106874 : previous_tag (tree type)
2075 : : {
2076 : 1106874 : struct c_binding *b = NULL;
2077 : 1106874 : tree name = c_type_tag (type);
2078 : :
2079 : 1106874 : if (name)
2080 : 553363 : b = I_TAG_BINDING (name);
2081 : :
2082 : 553363 : if (b)
2083 : 553363 : b = b->shadowed;
2084 : :
2085 : 1106874 : if (b && B_IN_CURRENT_SCOPE (b))
2086 : 173 : 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 : 4907840 : diagnose_mismatched_decls (tree newdecl, tree olddecl,
2101 : : tree *newtypep, tree *oldtypep)
2102 : : {
2103 : 4907840 : tree newtype, oldtype;
2104 : 4907840 : 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 : 4907840 : if (olddecl == error_mark_node || newdecl == error_mark_node)
2112 : : return false;
2113 : 4907820 : *oldtypep = oldtype = TREE_TYPE (olddecl);
2114 : 4907820 : *newtypep = newtype = TREE_TYPE (newdecl);
2115 : 4907820 : 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 : 4907812 : 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 : 4907783 : 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 : 4907737 : bool pedwarned = false;
2166 : 4907737 : bool warned = false;
2167 : 4907737 : bool enum_and_int_p = false;
2168 : 4907737 : auto_diagnostic_group d;
2169 : :
2170 : 4907737 : int comptypes_result = comptypes_check_enum_int (oldtype, newtype,
2171 : : &enum_and_int_p);
2172 : 4907737 : if (!comptypes_result)
2173 : : {
2174 : 142952 : if (TREE_CODE (olddecl) == FUNCTION_DECL
2175 : 142877 : && fndecl_built_in_p (olddecl, BUILT_IN_NORMAL)
2176 : 285711 : && !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 : 142758 : tree mismatch_expect;
2183 : 142758 : unsigned mismatch_argno;
2184 : :
2185 : 142758 : tree trytype = match_builtin_function_types (newtype, oldtype,
2186 : : &mismatch_expect,
2187 : : &mismatch_argno);
2188 : :
2189 : 142758 : if (trytype && comptypes (newtype, trytype))
2190 : 142140 : *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 : 142140 : 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 : 194 : else if (TREE_CODE (olddecl) == FUNCTION_DECL
2239 : 194 : && 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 : 119 : else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
2253 : 59 : && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
2254 : 28 : && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
2255 : 200 : && 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 : 189 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2266 : 114 : && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
2267 : 83 : && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
2268 : 212 : && 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 : 170 : int new_quals = TYPE_QUALS (newtype);
2279 : 170 : int old_quals = TYPE_QUALS (oldtype);
2280 : :
2281 : 170 : 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 : 150 : error ("conflicting types for %q+D; have %qT", newdecl, newtype);
2309 : 170 : diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
2310 : 170 : locate_old_decl (olddecl);
2311 : 170 : 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 : 4764785 : else if (enum_and_int_p
2317 : 195 : && 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 : 4764976 : && !(TREE_CODE (olddecl) == FUNCTION_DECL
2322 : 154 : && fndecl_built_in_p (olddecl, BUILT_IN_ACC_ON_DEVICE)
2323 : 124 : && !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 : 4906949 : if (TREE_CODE (newdecl) == TYPE_DECL)
2335 : : {
2336 : 44520 : bool types_different = false;
2337 : :
2338 : 44520 : comptypes_result
2339 : 44520 : = comptypes_check_different_types (oldtype, newtype, &types_different);
2340 : :
2341 : 44520 : 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 : 44509 : if (DECL_IN_SYSTEM_HEADER (newdecl)
2349 : 2217 : || DECL_IN_SYSTEM_HEADER (olddecl)
2350 : 1998 : || warning_suppressed_p (newdecl, OPT_Wpedantic)
2351 : 46507 : || warning_suppressed_p (olddecl, OPT_Wpedantic))
2352 : 42511 : return true; /* Allow OLDDECL to continue in use. */
2353 : :
2354 : 1998 : 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 : 1995 : else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2361 : : "redefinition of typedef %q+D", newdecl))
2362 : 8 : locate_old_decl (olddecl);
2363 : :
2364 : 1998 : 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 : 4862429 : 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 : 4843973 : if (fndecl_built_in_p (olddecl)
2382 : 8660701 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2383 : : {
2384 : 3561707 : if (!TREE_PUBLIC (newdecl)
2385 : 3561707 : || (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 : 3561604 : 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 : 4843870 : if (DECL_INITIAL (newdecl))
2412 : : {
2413 : 228967 : 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 : 4614903 : else if (DECL_INITIAL (olddecl)
2438 : 806 : && !prototype_p (oldtype) && prototype_p (newtype)
2439 : 4614923 : && 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 : 4843834 : 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 : 19 : if (!DECL_IS_UNDECLARED_BUILTIN (olddecl)
2465 : 38 : && !DECL_EXTERN_INLINE (olddecl))
2466 : : {
2467 : 5 : auto_diagnostic_group d;
2468 : 5 : error ("static declaration of %q+D follows "
2469 : : "non-static declaration", newdecl);
2470 : 5 : locate_old_decl (olddecl);
2471 : 5 : }
2472 : 19 : return false;
2473 : : }
2474 : 4843815 : 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 : 4843815 : if (DECL_DECLARED_INLINE_P (olddecl)
2495 : 4844878 : && 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 : 18456 : else if (VAR_P (newdecl))
2512 : : {
2513 : : /* Only variables can be thread-local, and all declarations must
2514 : : agree on this property. */
2515 : 18425 : 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 : 55062 : 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 : 18972 : if ((DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2546 : 18971 : || (flag_isoc23
2547 : 6269 : && 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 : 18429 : if (DECL_FILE_SCOPE_P (newdecl)
2566 : 18412 : && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2567 : : {
2568 : 142 : 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 : 3 : auto_diagnostic_group d;
2588 : 3 : if (TREE_PUBLIC (newdecl))
2589 : 2 : error ("non-static declaration of %q+D follows "
2590 : : "static declaration", newdecl);
2591 : : else
2592 : 1 : error ("static declaration of %q+D follows "
2593 : : "non-static declaration", newdecl);
2594 : :
2595 : 3 : locate_old_decl (olddecl);
2596 : 3 : return false;
2597 : 3 : }
2598 : : }
2599 : : /* Two objects with the same name declared at the same block
2600 : : scope must both be external references (6.7p3). */
2601 : 18270 : 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 : 18390 : if (warn_cxx_compat
2628 : 68 : && DECL_FILE_SCOPE_P (newdecl)
2629 : 68 : && !DECL_EXTERNAL (newdecl)
2630 : 18408 : && !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++", newdecl);
2635 : : }
2636 : :
2637 : : /* warnings */
2638 : : /* All decls must agree on a visibility. */
2639 : 4862236 : if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2640 : 4862205 : && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2641 : 4862971 : && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2642 : : {
2643 : 1 : warned |= warning (0, "redeclaration of %q+D with different visibility "
2644 : : "(old visibility preserved)", newdecl);
2645 : : }
2646 : :
2647 : 4862236 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2648 : 4843815 : warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2649 : : else /* PARM_DECL, VAR_DECL */
2650 : : {
2651 : : /* Redeclaration of a parameter is a constraint violation (this is
2652 : : not explicitly stated, but follows from C99 6.7p3 [no more than
2653 : : one declaration of the same identifier with no linkage in the
2654 : : same scope, except type tags] and 6.2.2p6 [parameters have no
2655 : : linkage]). We must check for a forward parameter declaration,
2656 : : indicated by TREE_ASM_WRITTEN on the old declaration - this is
2657 : : an extension, the mandatory diagnostic for which is handled by
2658 : : mark_forward_parm_decls. */
2659 : :
2660 : 18421 : if (TREE_CODE (newdecl) == PARM_DECL
2661 : 31 : && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2662 : : {
2663 : 2 : auto_diagnostic_group d;
2664 : 2 : error ("redefinition of parameter %q+D", newdecl);
2665 : 2 : locate_old_decl (olddecl);
2666 : 2 : return false;
2667 : 2 : }
2668 : : }
2669 : :
2670 : : /* Optional warning for completely redundant decls. */
2671 : 4862234 : if (!warned && !pedwarned
2672 : 4862160 : && warn_redundant_decls
2673 : : /* Don't warn about a function declaration followed by a
2674 : : definition. */
2675 : 18 : && !(TREE_CODE (newdecl) == FUNCTION_DECL
2676 : 2 : && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2677 : : /* Don't warn about redundant redeclarations of builtins. */
2678 : 18 : && !(TREE_CODE (newdecl) == FUNCTION_DECL
2679 : 2 : && !fndecl_built_in_p (newdecl)
2680 : 2 : && fndecl_built_in_p (olddecl)
2681 : 2 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2682 : : /* Don't warn about an extern followed by a definition. */
2683 : 17 : && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2684 : : /* Don't warn about forward parameter decls. */
2685 : 17 : && !(TREE_CODE (newdecl) == PARM_DECL
2686 : 6 : && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2687 : : /* Don't warn about a variable definition following a declaration. */
2688 : 4862245 : && !(VAR_P (newdecl)
2689 : 10 : && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2690 : : {
2691 : 6 : warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2692 : : newdecl);
2693 : : }
2694 : :
2695 : : /* Report location of previous decl/defn. */
2696 : 4862234 : if (warned || pedwarned)
2697 : 80 : locate_old_decl (olddecl);
2698 : :
2699 : : #undef DECL_EXTERN_INLINE
2700 : :
2701 : : return retval;
2702 : 4907737 : }
2703 : :
2704 : : /* Subroutine of duplicate_decls. NEWDECL has been found to be
2705 : : consistent with OLDDECL, but carries new information. Merge the
2706 : : new information into OLDDECL. This function issues no
2707 : : diagnostics. */
2708 : :
2709 : : static void
2710 : 4906743 : merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2711 : : {
2712 : 4906743 : bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2713 : 4906743 : && DECL_INITIAL (newdecl) != NULL_TREE);
2714 : 4906743 : bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2715 : 4906743 : && prototype_p (TREE_TYPE (newdecl)));
2716 : 4906743 : bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2717 : 4906743 : && prototype_p (TREE_TYPE (olddecl)));
2718 : :
2719 : : /* For real parm decl following a forward decl, rechain the old decl
2720 : : in its new location and clear TREE_ASM_WRITTEN (it's not a
2721 : : forward decl anymore). */
2722 : 4906743 : if (TREE_CODE (newdecl) == PARM_DECL
2723 : 29 : && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2724 : : {
2725 : 29 : struct c_binding *b, **here;
2726 : :
2727 : 44 : for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
2728 : 44 : if ((*here)->decl == olddecl)
2729 : 29 : goto found;
2730 : 0 : gcc_unreachable ();
2731 : :
2732 : 29 : found:
2733 : 29 : b = *here;
2734 : 29 : *here = b->prev;
2735 : 29 : b->prev = current_scope->bindings;
2736 : 29 : current_scope->bindings = b;
2737 : :
2738 : 29 : TREE_ASM_WRITTEN (olddecl) = 0;
2739 : : }
2740 : :
2741 : 4906743 : DECL_ATTRIBUTES (newdecl)
2742 : 4906743 : = targetm.merge_decl_attributes (olddecl, newdecl);
2743 : :
2744 : : /* For typedefs use the old type, as the new type's DECL_NAME points
2745 : : at newdecl, which will be ggc_freed. */
2746 : 4906743 : if (TREE_CODE (newdecl) == TYPE_DECL)
2747 : : {
2748 : : /* But NEWTYPE might have an attribute, honor that. */
2749 : 44509 : tree tem = newtype;
2750 : 44509 : newtype = oldtype;
2751 : :
2752 : 44509 : if (TYPE_USER_ALIGN (tem))
2753 : : {
2754 : 12 : if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2755 : 6 : SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2756 : 12 : TYPE_USER_ALIGN (newtype) = true;
2757 : : }
2758 : :
2759 : : /* And remove the new type from the variants list. */
2760 : 44509 : if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2761 : : {
2762 : 12 : tree remove = TREE_TYPE (newdecl);
2763 : 12 : if (TYPE_MAIN_VARIANT (remove) == remove)
2764 : : {
2765 : 2 : gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2766 : : /* If remove is the main variant, no need to remove that
2767 : : from the list. One of the DECL_ORIGINAL_TYPE
2768 : : variants, e.g. created for aligned attribute, might still
2769 : : refer to the newdecl TYPE_DECL though, so remove that one
2770 : : in that case. */
2771 : 2 : if (DECL_ORIGINAL_TYPE (newdecl)
2772 : 2 : && DECL_ORIGINAL_TYPE (newdecl) != remove)
2773 : 2 : for (tree t = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl));
2774 : 2 : t; t = TYPE_MAIN_VARIANT (t))
2775 : 2 : if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2776 : : {
2777 : 4 : TYPE_NEXT_VARIANT (t)
2778 : 2 : = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2779 : 2 : break;
2780 : : }
2781 : : }
2782 : : else
2783 : 10 : for (tree t = TYPE_MAIN_VARIANT (remove); ;
2784 : 0 : t = TYPE_NEXT_VARIANT (t))
2785 : 10 : if (TYPE_NEXT_VARIANT (t) == remove)
2786 : : {
2787 : 10 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2788 : 10 : break;
2789 : : }
2790 : : }
2791 : :
2792 : : /* Make sure we refer to the same type as the olddecl. */
2793 : 44509 : DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2794 : : }
2795 : :
2796 : : /* Merge the data types specified in the two decls. */
2797 : 9813486 : TREE_TYPE (newdecl)
2798 : 4906743 : = TREE_TYPE (olddecl)
2799 : 9813486 : = composite_type (newtype, oldtype);
2800 : :
2801 : : /* Lay the type out, unless already done. */
2802 : 4906743 : if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2803 : : {
2804 : 0 : if (TREE_TYPE (newdecl) != error_mark_node)
2805 : 0 : layout_type (TREE_TYPE (newdecl));
2806 : 0 : if (TREE_CODE (newdecl) != FUNCTION_DECL
2807 : : && TREE_CODE (newdecl) != TYPE_DECL
2808 : : && TREE_CODE (newdecl) != CONST_DECL)
2809 : 0 : layout_decl (newdecl, 0);
2810 : : }
2811 : : else
2812 : : {
2813 : : /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2814 : 4906743 : DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2815 : 4906743 : DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2816 : 4906743 : SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2817 : 4906743 : if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2818 : : {
2819 : 44 : SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2820 : 44 : DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2821 : : }
2822 : 4906699 : else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2823 : 4906699 : && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2824 : 3 : DECL_USER_ALIGN (newdecl) = 1;
2825 : 9813486 : if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2826 : 4906743 : > DECL_WARN_IF_NOT_ALIGN (newdecl))
2827 : 0 : SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2828 : : DECL_WARN_IF_NOT_ALIGN (olddecl));
2829 : : }
2830 : :
2831 : : /* Keep the old rtl since we can safely use it. */
2832 : 4906743 : if (HAS_RTL_P (olddecl))
2833 : 4906743 : COPY_DECL_RTL (olddecl, newdecl);
2834 : :
2835 : : /* Merge the type qualifiers. */
2836 : 4906743 : if (TREE_READONLY (newdecl))
2837 : 387152 : TREE_READONLY (olddecl) = 1;
2838 : :
2839 : 4906743 : if (TREE_THIS_VOLATILE (newdecl))
2840 : 48617 : TREE_THIS_VOLATILE (olddecl) = 1;
2841 : :
2842 : : /* Merge deprecatedness. */
2843 : 4906743 : if (TREE_DEPRECATED (newdecl))
2844 : 331 : TREE_DEPRECATED (olddecl) = 1;
2845 : :
2846 : : /* Merge unavailability. */
2847 : 4906743 : if (TREE_UNAVAILABLE (newdecl))
2848 : 2 : TREE_UNAVAILABLE (olddecl) = 1;
2849 : :
2850 : : /* If a decl is in a system header and the other isn't, keep the one on the
2851 : : system header. Otherwise, keep source location of definition rather than
2852 : : declaration and of prototype rather than non-prototype unless that
2853 : : prototype is built-in. */
2854 : 4906743 : if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2855 : 4906714 : && DECL_IN_SYSTEM_HEADER (olddecl)
2856 : 5580377 : && !DECL_IN_SYSTEM_HEADER (newdecl) )
2857 : 1035 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2858 : 4905708 : else if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2859 : 4905679 : && DECL_IN_SYSTEM_HEADER (newdecl)
2860 : 9087897 : && !DECL_IN_SYSTEM_HEADER (olddecl))
2861 : 3509590 : DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2862 : 1396118 : else if ((DECL_INITIAL (newdecl) == NULL_TREE
2863 : 1166699 : && DECL_INITIAL (olddecl) != NULL_TREE)
2864 : 2561913 : || (old_is_prototype && !new_is_prototype
2865 : 372 : && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2866 : 932 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2867 : :
2868 : : /* Merge the initialization information. */
2869 : 4906743 : if (DECL_INITIAL (newdecl) == NULL_TREE)
2870 : 4677252 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2871 : :
2872 : : /* Merge 'constexpr' information. */
2873 : 4906743 : if (VAR_P (olddecl) && VAR_P (newdecl))
2874 : : {
2875 : 18390 : if (C_DECL_DECLARED_CONSTEXPR (olddecl))
2876 : 2 : C_DECL_DECLARED_CONSTEXPR (newdecl) = 1;
2877 : 18388 : else if (C_DECL_DECLARED_CONSTEXPR (newdecl))
2878 : 1 : C_DECL_DECLARED_CONSTEXPR (olddecl) = 1;
2879 : : }
2880 : :
2881 : : /* Merge the threadprivate attribute. */
2882 : 4906743 : if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2883 : 7 : C_DECL_THREADPRIVATE_P (newdecl) = 1;
2884 : :
2885 : 4906743 : if (HAS_DECL_ASSEMBLER_NAME_P (olddecl))
2886 : : {
2887 : : /* Copy the assembler name.
2888 : : Currently, it can only be defined in the prototype. */
2889 : 4906714 : COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2890 : :
2891 : : /* Use visibility of whichever declaration had it specified */
2892 : 4906714 : if (DECL_VISIBILITY_SPECIFIED (olddecl))
2893 : : {
2894 : 4741 : DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2895 : 4741 : DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2896 : : }
2897 : :
2898 : 4906714 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2899 : : {
2900 : 4843815 : DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2901 : 4843815 : DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2902 : 4843815 : DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2903 : 4843815 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2904 : 4843815 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2905 : 4843815 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2906 : 4843815 : DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2907 : 4843815 : if (DECL_IS_OPERATOR_NEW_P (olddecl))
2908 : 0 : DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2909 : 4843815 : if (DECL_IS_OPERATOR_DELETE_P (olddecl))
2910 : 0 : DECL_SET_IS_OPERATOR_DELETE (newdecl, true);
2911 : 4843815 : TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2912 : 4843815 : DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2913 : 4843815 : DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2914 : : }
2915 : :
2916 : : /* Merge the storage class information. */
2917 : 4906714 : merge_weak (newdecl, olddecl);
2918 : :
2919 : : /* For functions, static overrides non-static. */
2920 : 4906714 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2921 : : {
2922 : 4843815 : TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2923 : : /* This is since we don't automatically
2924 : : copy the attributes of NEWDECL into OLDDECL. */
2925 : 4843815 : TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2926 : : /* If this clears `static', clear it in the identifier too. */
2927 : 4843815 : if (!TREE_PUBLIC (olddecl))
2928 : 7568 : TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2929 : : }
2930 : : }
2931 : :
2932 : : /* In c99, 'extern' declaration before (or after) 'inline' means this
2933 : : function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2934 : : is present. */
2935 : 4906743 : if (TREE_CODE (newdecl) == FUNCTION_DECL
2936 : 4843815 : && !flag_gnu89_inline
2937 : 4819928 : && (DECL_DECLARED_INLINE_P (newdecl)
2938 : 4632987 : || DECL_DECLARED_INLINE_P (olddecl))
2939 : 187135 : && (!DECL_DECLARED_INLINE_P (newdecl)
2940 : 186941 : || !DECL_DECLARED_INLINE_P (olddecl)
2941 : 795 : || !DECL_EXTERNAL (olddecl))
2942 : 186358 : && DECL_EXTERNAL (newdecl)
2943 : 186150 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2944 : 4906841 : && !current_function_decl)
2945 : 81 : DECL_EXTERNAL (newdecl) = 0;
2946 : :
2947 : : /* An inline definition following a static declaration is not
2948 : : DECL_EXTERNAL. */
2949 : 4906743 : if (new_is_definition
2950 : 228924 : && (DECL_DECLARED_INLINE_P (newdecl)
2951 : 41761 : || DECL_DECLARED_INLINE_P (olddecl))
2952 : 5094120 : && !TREE_PUBLIC (olddecl))
2953 : 888 : DECL_EXTERNAL (newdecl) = 0;
2954 : :
2955 : 4906743 : if (DECL_EXTERNAL (newdecl))
2956 : : {
2957 : 4817299 : TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2958 : 4817299 : DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2959 : :
2960 : : /* An extern decl does not override previous storage class. */
2961 : 4817299 : TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2962 : 4817299 : if (!DECL_EXTERNAL (newdecl))
2963 : : {
2964 : 1409 : DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2965 : 1409 : DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2966 : : }
2967 : : }
2968 : : else
2969 : : {
2970 : 89444 : TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2971 : 89444 : TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2972 : : }
2973 : :
2974 : 4906743 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2975 : : {
2976 : : /* If we're redefining a function previously defined as extern
2977 : : inline, make sure we emit debug info for the inline before we
2978 : : throw it away, in case it was inlined into a function that
2979 : : hasn't been written out yet. */
2980 : 4843815 : if (new_is_definition && DECL_INITIAL (olddecl))
2981 : : /* The new defn must not be inline. */
2982 : 75 : DECL_UNINLINABLE (newdecl) = 1;
2983 : : else
2984 : : {
2985 : : /* If either decl says `inline', this fn is inline, unless
2986 : : its definition was passed already. */
2987 : 4843740 : if (DECL_DECLARED_INLINE_P (newdecl)
2988 : 9500265 : || DECL_DECLARED_INLINE_P (olddecl))
2989 : 187400 : DECL_DECLARED_INLINE_P (newdecl) = 1;
2990 : :
2991 : 14531220 : DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2992 : 9689114 : = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2993 : :
2994 : 9687480 : DECL_DISREGARD_INLINE_LIMITS (newdecl)
2995 : 4843740 : = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2996 : 4843740 : = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2997 : 9687342 : || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2998 : : }
2999 : :
3000 : 4843815 : if (fndecl_built_in_p (olddecl))
3001 : : {
3002 : : /* If redeclaring a builtin function, it stays built in.
3003 : : But it gets tagged as having been declared. */
3004 : 3816625 : copy_decl_built_in_function (newdecl, olddecl);
3005 : 3816625 : C_DECL_DECLARED_BUILTIN (newdecl) = 1;
3006 : 3816625 : if (new_is_prototype)
3007 : : {
3008 : 3816266 : C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
3009 : 3816266 : if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3010 : : {
3011 : 3816266 : enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3012 : 3816266 : switch (fncode)
3013 : : {
3014 : : /* If a compatible prototype of these builtin functions
3015 : : is seen, assume the runtime implements it with the
3016 : : expected semantics. */
3017 : 6426 : case BUILT_IN_STPCPY:
3018 : 6426 : if (builtin_decl_explicit_p (fncode))
3019 : 6426 : set_builtin_decl_implicit_p (fncode, true);
3020 : : break;
3021 : 3809840 : default:
3022 : 3809840 : if (builtin_decl_explicit_p (fncode))
3023 : 3809840 : set_builtin_decl_declared_p (fncode, true);
3024 : : break;
3025 : : }
3026 : :
3027 : 3816266 : copy_attributes_to_builtin (newdecl);
3028 : : }
3029 : : }
3030 : : else
3031 : 718 : C_DECL_BUILTIN_PROTOTYPE (newdecl)
3032 : 718 : = C_DECL_BUILTIN_PROTOTYPE (olddecl);
3033 : : }
3034 : :
3035 : : /* Preserve function specific target and optimization options */
3036 : 4843815 : if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
3037 : 4844320 : && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
3038 : 469 : DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
3039 : 469 : = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
3040 : :
3041 : 4843815 : if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
3042 : 4867237 : && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
3043 : 9 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
3044 : 9 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
3045 : :
3046 : : /* Also preserve various other info from the definition. */
3047 : 4843815 : if (!new_is_definition)
3048 : : {
3049 : 4614891 : tree t;
3050 : 4614891 : DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3051 : 4614891 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3052 : 4614891 : DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
3053 : 4614891 : DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3054 : 4614891 : DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
3055 : 7230118 : for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
3056 : 2615227 : DECL_CONTEXT (t) = newdecl;
3057 : :
3058 : : /* See if we've got a function to instantiate from. */
3059 : 4614891 : if (DECL_SAVED_TREE (olddecl))
3060 : 1584 : DECL_ABSTRACT_ORIGIN (newdecl)
3061 : 792 : = DECL_ABSTRACT_ORIGIN (olddecl);
3062 : : }
3063 : : }
3064 : :
3065 : : /* Merge the USED information. */
3066 : 4906743 : if (TREE_USED (olddecl))
3067 : 702995 : TREE_USED (newdecl) = 1;
3068 : 4203748 : else if (TREE_USED (newdecl))
3069 : 4 : TREE_USED (olddecl) = 1;
3070 : 4906743 : if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
3071 : 18419 : DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
3072 : 4906743 : if (DECL_PRESERVE_P (olddecl))
3073 : 25 : DECL_PRESERVE_P (newdecl) = 1;
3074 : 4906718 : else if (DECL_PRESERVE_P (newdecl))
3075 : 4 : DECL_PRESERVE_P (olddecl) = 1;
3076 : :
3077 : : /* Merge DECL_COMMON */
3078 : 18390 : if (VAR_P (olddecl) && VAR_P (newdecl)
3079 : 18390 : && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
3080 : 4925131 : && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
3081 : 36772 : DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
3082 : :
3083 : : /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
3084 : : But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
3085 : : DECL_ARGUMENTS (if appropriate). */
3086 : 4906743 : {
3087 : 4906743 : unsigned olddecl_uid = DECL_UID (olddecl);
3088 : 4906743 : tree olddecl_context = DECL_CONTEXT (olddecl);
3089 : 4906743 : tree olddecl_arguments = NULL;
3090 : 4906743 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3091 : 4843815 : olddecl_arguments = DECL_ARGUMENTS (olddecl);
3092 : :
3093 : 4906743 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3094 : : (char *) newdecl + sizeof (struct tree_common),
3095 : : sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3096 : 4906743 : DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3097 : 4906743 : switch (TREE_CODE (olddecl))
3098 : : {
3099 : 4862205 : case FUNCTION_DECL:
3100 : 4862205 : case VAR_DECL:
3101 : 4862205 : {
3102 : 4862205 : struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
3103 : :
3104 : 9724410 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3105 : : (char *) newdecl + sizeof (struct tree_decl_common),
3106 : 4862205 : tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3107 : 4862205 : olddecl->decl_with_vis.symtab_node = snode;
3108 : :
3109 : 4862205 : if ((DECL_EXTERNAL (olddecl)
3110 : 46315 : || TREE_PUBLIC (olddecl)
3111 : 7830 : || TREE_STATIC (olddecl))
3112 : 4908513 : && DECL_SECTION_NAME (newdecl) != NULL)
3113 : 8 : set_decl_section_name (olddecl, newdecl);
3114 : :
3115 : : /* This isn't quite correct for something like
3116 : : int __thread x attribute ((tls_model ("local-exec")));
3117 : : extern int __thread x;
3118 : : as we'll lose the "local-exec" model. */
3119 : 4862205 : if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
3120 : 89 : set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3121 : : break;
3122 : : }
3123 : :
3124 : 44538 : case FIELD_DECL:
3125 : 44538 : case PARM_DECL:
3126 : 44538 : case LABEL_DECL:
3127 : 44538 : case RESULT_DECL:
3128 : 44538 : case CONST_DECL:
3129 : 44538 : case TYPE_DECL:
3130 : 89076 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3131 : : (char *) newdecl + sizeof (struct tree_decl_common),
3132 : 44538 : tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3133 : 44538 : break;
3134 : :
3135 : 0 : default:
3136 : :
3137 : 0 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3138 : : (char *) newdecl + sizeof (struct tree_decl_common),
3139 : : sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
3140 : : }
3141 : 4906743 : DECL_UID (olddecl) = olddecl_uid;
3142 : 4906743 : DECL_CONTEXT (olddecl) = olddecl_context;
3143 : 4906743 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3144 : 4843815 : DECL_ARGUMENTS (olddecl) = olddecl_arguments;
3145 : : }
3146 : :
3147 : : /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3148 : : so that encode_section_info has a chance to look at the new decl
3149 : : flags and attributes. */
3150 : 4906743 : if (DECL_RTL_SET_P (olddecl)
3151 : 4906743 : && (TREE_CODE (olddecl) == FUNCTION_DECL
3152 : 0 : || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
3153 : 0 : make_decl_rtl (olddecl);
3154 : 4906743 : }
3155 : :
3156 : : /* Handle when a new declaration NEWDECL has the same name as an old
3157 : : one OLDDECL in the same binding contour. Prints an error message
3158 : : if appropriate.
3159 : :
3160 : : If safely possible, alter OLDDECL to look like NEWDECL, and return
3161 : : true. Otherwise, return false. */
3162 : :
3163 : : static bool
3164 : 4907840 : duplicate_decls (tree newdecl, tree olddecl)
3165 : : {
3166 : 4907840 : tree newtype = NULL, oldtype = NULL;
3167 : :
3168 : 4907840 : if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
3169 : : {
3170 : : /* Avoid `unused variable' and other warnings for OLDDECL. */
3171 : 1097 : suppress_warning (olddecl, OPT_Wunused);
3172 : : /* If the types are completely different, poison them both with
3173 : : error_mark_node. */
3174 : 1097 : if (TREE_CODE (TREE_TYPE (newdecl)) != TREE_CODE (TREE_TYPE (olddecl))
3175 : 114 : && olddecl != error_mark_node
3176 : 1191 : && seen_error ())
3177 : : {
3178 : 60 : if (TREE_CODE (olddecl) != FUNCTION_DECL)
3179 : 48 : TREE_TYPE (olddecl) = error_mark_node;
3180 : 60 : if (TREE_CODE (newdecl) != FUNCTION_DECL)
3181 : 57 : TREE_TYPE (newdecl) = error_mark_node;
3182 : : }
3183 : 1097 : return false;
3184 : : }
3185 : :
3186 : 4906743 : merge_decls (newdecl, olddecl, newtype, oldtype);
3187 : :
3188 : : /* The NEWDECL will no longer be needed.
3189 : :
3190 : : Before releasing the node, be sure to remove function from symbol
3191 : : table that might have been inserted there to record comdat group.
3192 : : Be sure to however do not free DECL_STRUCT_FUNCTION because this
3193 : : structure is shared in between NEWDECL and OLDECL. */
3194 : 4906743 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3195 : 4843815 : DECL_STRUCT_FUNCTION (newdecl) = NULL;
3196 : 4906743 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3197 : : {
3198 : 4862205 : struct symtab_node *snode = symtab_node::get (newdecl);
3199 : 4862205 : if (snode)
3200 : 104 : snode->remove ();
3201 : : }
3202 : 4906743 : ggc_free (newdecl);
3203 : 4906743 : return true;
3204 : : }
3205 : :
3206 : :
3207 : : /* Check whether decl-node NEW_DECL shadows an existing declaration. */
3208 : : static void
3209 : 166332812 : warn_if_shadowing (tree new_decl)
3210 : : {
3211 : 166332812 : struct c_binding *b;
3212 : :
3213 : : /* Shadow warnings wanted? */
3214 : 332664708 : if (!(warn_shadow
3215 : 166332076 : || warn_shadow_local
3216 : 166331896 : || warn_shadow_compatible_local)
3217 : : /* No shadow warnings for internally generated vars. */
3218 : 166333055 : || DECL_IS_UNDECLARED_BUILTIN (new_decl))
3219 : : return;
3220 : :
3221 : : /* Is anything being shadowed? Invisible decls do not count. */
3222 : 253 : for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
3223 : 157 : if (b->decl && b->decl != new_decl && !b->invisible
3224 : 214 : && (b->decl == error_mark_node
3225 : 43 : || diagnostic_report_warnings_p (global_dc,
3226 : : DECL_SOURCE_LOCATION (b->decl))))
3227 : : {
3228 : 57 : tree old_decl = b->decl;
3229 : :
3230 : 57 : if (old_decl == error_mark_node)
3231 : : {
3232 : 14 : warning (OPT_Wshadow, "declaration of %q+D shadows previous "
3233 : : "non-variable", new_decl);
3234 : 50 : break;
3235 : : }
3236 : :
3237 : 43 : bool warned = false;
3238 : 43 : auto_diagnostic_group d;
3239 : 43 : if (TREE_CODE (old_decl) == PARM_DECL)
3240 : : {
3241 : 5 : enum opt_code warning_code;
3242 : :
3243 : : /* If '-Wshadow=compatible-local' is specified without other
3244 : : -Wshadow= flags, we will warn only when the types of the
3245 : : shadowing variable (i.e. new_decl) and the shadowed variable
3246 : : (old_decl) are compatible. */
3247 : 5 : if (warn_shadow)
3248 : : warning_code = OPT_Wshadow;
3249 : 2 : else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3250 : : warning_code = OPT_Wshadow_compatible_local;
3251 : : else
3252 : 2 : warning_code = OPT_Wshadow_local;
3253 : 5 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3254 : : "declaration of %qD shadows a parameter",
3255 : : new_decl);
3256 : : }
3257 : 38 : else if (DECL_FILE_SCOPE_P (old_decl))
3258 : : {
3259 : : /* Do not warn if a variable shadows a function, unless
3260 : : the variable is a function or a pointer-to-function. */
3261 : 34 : if (TREE_CODE (old_decl) == FUNCTION_DECL
3262 : 11 : && TREE_CODE (new_decl) != FUNCTION_DECL
3263 : 36 : && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
3264 : 7 : continue;
3265 : :
3266 : 20 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
3267 : : "declaration of %qD shadows a global "
3268 : : "declaration",
3269 : : new_decl);
3270 : : }
3271 : 11 : else if (TREE_CODE (old_decl) == FUNCTION_DECL
3272 : 11 : && fndecl_built_in_p (old_decl))
3273 : : {
3274 : 0 : warning (OPT_Wshadow, "declaration of %q+D shadows "
3275 : : "a built-in function", new_decl);
3276 : 0 : break;
3277 : : }
3278 : : else
3279 : : {
3280 : 11 : enum opt_code warning_code;
3281 : :
3282 : : /* If '-Wshadow=compatible-local' is specified without other
3283 : : -Wshadow= flags, we will warn only when the types of the
3284 : : shadowing variable (i.e. new_decl) and the shadowed variable
3285 : : (old_decl) are compatible. */
3286 : 11 : if (warn_shadow)
3287 : : warning_code = OPT_Wshadow;
3288 : 8 : else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3289 : : warning_code = OPT_Wshadow_compatible_local;
3290 : : else
3291 : 2 : warning_code = OPT_Wshadow_local;
3292 : 11 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3293 : : "declaration of %qD shadows a previous local",
3294 : : new_decl);
3295 : : }
3296 : :
3297 : 36 : if (warned)
3298 : 26 : inform (DECL_SOURCE_LOCATION (old_decl),
3299 : : "shadowed declaration is here");
3300 : :
3301 : : break;
3302 : 43 : }
3303 : : }
3304 : :
3305 : : /* Record a decl-node X as belonging to the current lexical scope.
3306 : : Check for errors (such as an incompatible declaration for the same
3307 : : name already seen in the same scope).
3308 : :
3309 : : Returns either X or an old decl for the same name.
3310 : : If an old decl is returned, it may have been smashed
3311 : : to agree with what X says. */
3312 : :
3313 : : tree
3314 : 198054745 : pushdecl (tree x)
3315 : : {
3316 : 198054745 : tree name = DECL_NAME (x);
3317 : 198054745 : struct c_scope *scope = current_scope;
3318 : 198054745 : struct c_binding *b;
3319 : 198054745 : bool nested = false;
3320 : 198054745 : location_t locus = DECL_SOURCE_LOCATION (x);
3321 : :
3322 : : /* Must set DECL_CONTEXT for everything not at file scope or
3323 : : DECL_FILE_SCOPE_P won't work. Local externs don't count
3324 : : unless they have initializers (which generate code). We
3325 : : also exclude CONST_DECLs because enumerators will get the
3326 : : type of the enum as context. */
3327 : 198054745 : if (current_function_decl
3328 : 8940594 : && TREE_CODE (x) != CONST_DECL
3329 : 206842017 : && (!VAR_OR_FUNCTION_DECL_P (x)
3330 : 8511247 : || DECL_INITIAL (x) || !TREE_PUBLIC (x)))
3331 : 8775262 : DECL_CONTEXT (x) = current_function_decl;
3332 : :
3333 : : /* Anonymous decls are just inserted in the scope. */
3334 : 198054745 : if (!name)
3335 : : {
3336 : 7661902 : bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
3337 : : locus);
3338 : 7661902 : return x;
3339 : : }
3340 : :
3341 : : /* First, see if there is another declaration with the same name in
3342 : : the current scope. If there is, duplicate_decls may do all the
3343 : : work for us. If duplicate_decls returns false, that indicates
3344 : : two incompatible decls in the same scope; we are to silently
3345 : : replace the old one (duplicate_decls has issued all appropriate
3346 : : diagnostics). In particular, we should not consider possible
3347 : : duplicates in the external scope, or shadowing. */
3348 : 190392843 : b = I_SYMBOL_BINDING (name);
3349 : 190392843 : if (b && B_IN_SCOPE (b, scope))
3350 : : {
3351 : 1365258 : struct c_binding *b_ext, *b_use;
3352 : 1365258 : tree type = TREE_TYPE (x);
3353 : 1365258 : tree visdecl = b->decl;
3354 : 1365258 : tree vistype = TREE_TYPE (visdecl);
3355 : 1365258 : if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3356 : 1365258 : && COMPLETE_TYPE_P (TREE_TYPE (x)))
3357 : 1355 : b->inner_comp = false;
3358 : 1365258 : b_use = b;
3359 : 1365258 : b_ext = b;
3360 : : /* If this is an external linkage declaration, we should check
3361 : : for compatibility with the type in the external scope before
3362 : : setting the type at this scope based on the visible
3363 : : information only. */
3364 : 1365258 : if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
3365 : : {
3366 : 2625583 : while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3367 : 1312801 : b_ext = b_ext->shadowed;
3368 : 1312782 : if (b_ext)
3369 : : {
3370 : 1312781 : b_use = b_ext;
3371 : 1312781 : if (b_use->u.type)
3372 : 266748 : TREE_TYPE (b_use->decl) = b_use->u.type;
3373 : : }
3374 : : }
3375 : 1365258 : if (duplicate_decls (x, b_use->decl))
3376 : : {
3377 : 1364895 : if (b_use != b)
3378 : : {
3379 : : /* Save the updated type in the external scope and
3380 : : restore the proper type for this scope. */
3381 : 1312580 : tree thistype;
3382 : 1312580 : if (comptypes (vistype, type))
3383 : 1312546 : thistype = composite_type (vistype, type);
3384 : : else
3385 : 34 : thistype = TREE_TYPE (b_use->decl);
3386 : 1312580 : b_use->u.type = TREE_TYPE (b_use->decl);
3387 : 1312580 : if (TREE_CODE (b_use->decl) == FUNCTION_DECL
3388 : 1312580 : && fndecl_built_in_p (b_use->decl))
3389 : 276500 : thistype
3390 : 276500 : = c_build_type_attribute_variant (thistype,
3391 : 276500 : TYPE_ATTRIBUTES
3392 : : (b_use->u.type));
3393 : 1312580 : TREE_TYPE (b_use->decl) = thistype;
3394 : : }
3395 : 1364895 : return b_use->decl;
3396 : : }
3397 : : else
3398 : 363 : goto skip_external_and_shadow_checks;
3399 : : }
3400 : :
3401 : : /* All declarations with external linkage, and all external
3402 : : references, go in the external scope, no matter what scope is
3403 : : current. However, the binding in that scope is ignored for
3404 : : purposes of normal name lookup. A separate binding structure is
3405 : : created in the requested scope; this governs the normal
3406 : : visibility of the symbol.
3407 : :
3408 : : The binding in the externals scope is used exclusively for
3409 : : detecting duplicate declarations of the same object, no matter
3410 : : what scope they are in; this is what we do here. (C99 6.2.7p2:
3411 : : All declarations that refer to the same object or function shall
3412 : : have compatible type; otherwise, the behavior is undefined.)
3413 : : However, in Objective-C, we also want to detect declarations
3414 : : conflicting with those of the basic types. */
3415 : 330157702 : if ((DECL_EXTERNAL (x) || scope == file_scope)
3416 : 200247838 : && (VAR_OR_FUNCTION_DECL_P (x) || c_dialect_objc ()))
3417 : : {
3418 : 49667385 : tree type = TREE_TYPE (x);
3419 : 49667385 : tree vistype = NULL_TREE;
3420 : 49667385 : tree visdecl = NULL_TREE;
3421 : 49667385 : bool type_saved = false;
3422 : 3542609 : if (b && !B_IN_EXTERNAL_SCOPE (b)
3423 : 1024 : && VAR_OR_FUNCTION_DECL_P (b->decl)
3424 : 49668400 : && DECL_FILE_SCOPE_P (b->decl))
3425 : : {
3426 : 789 : visdecl = b->decl;
3427 : 789 : vistype = TREE_TYPE (visdecl);
3428 : : }
3429 : 49667385 : if (scope != file_scope
3430 : 49667385 : && !DECL_IN_SYSTEM_HEADER (x))
3431 : 11460 : warning_at (locus, OPT_Wnested_externs,
3432 : : "nested extern declaration of %qD", x);
3433 : :
3434 : 49668795 : while (b && !B_IN_EXTERNAL_SCOPE (b))
3435 : : {
3436 : : /* If this decl might be modified, save its type. This is
3437 : : done here rather than when the decl is first bound
3438 : : because the type may change after first binding, through
3439 : : being completed or through attributes being added. If we
3440 : : encounter multiple such decls, only the first should have
3441 : : its type saved; the others will already have had their
3442 : : proper types saved and the types will not have changed as
3443 : : their scopes will not have been re-entered. */
3444 : 1410 : if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3445 : : {
3446 : 1008 : b->u.type = TREE_TYPE (b->decl);
3447 : 1008 : type_saved = true;
3448 : : }
3449 : 1410 : if (B_IN_FILE_SCOPE (b)
3450 : 998 : && VAR_P (b->decl)
3451 : 572 : && TREE_STATIC (b->decl)
3452 : 281 : && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3453 : 138 : && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3454 : 47 : && TREE_CODE (type) == ARRAY_TYPE
3455 : 47 : && TYPE_DOMAIN (type)
3456 : 28 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3457 : 1438 : && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3458 : : {
3459 : : /* Array type completed in inner scope, which should be
3460 : : diagnosed if the completion does not have size 1 and
3461 : : it does not get completed in the file scope. */
3462 : 6 : b->inner_comp = true;
3463 : : }
3464 : 1410 : b = b->shadowed;
3465 : : }
3466 : :
3467 : : /* If a matching external declaration has been found, set its
3468 : : type to the composite of all the types of that declaration.
3469 : : After the consistency checks, it will be reset to the
3470 : : composite of the visible types only. */
3471 : 49667385 : if (b && b->u.type)
3472 : 761 : TREE_TYPE (b->decl) = b->u.type;
3473 : :
3474 : : /* the static does not go in the externals scope. */
3475 : 3542465 : if (b && duplicate_decls (x, b->decl))
3476 : : {
3477 : 3541731 : tree thistype;
3478 : 3541731 : if (vistype)
3479 : : {
3480 : 661 : if (comptypes (vistype, type))
3481 : 624 : thistype = composite_type (vistype, type);
3482 : : else
3483 : 37 : thistype = TREE_TYPE (b->decl);
3484 : : }
3485 : : else
3486 : : thistype = type;
3487 : 3541731 : b->u.type = TREE_TYPE (b->decl);
3488 : : /* Propagate the type attributes to the decl. */
3489 : 3541731 : thistype
3490 : 3541731 : = c_build_type_attribute_variant (thistype,
3491 : 3541731 : TYPE_ATTRIBUTES (b->u.type));
3492 : 3541731 : TREE_TYPE (b->decl) = thistype;
3493 : 3541731 : bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3494 : : locus);
3495 : 3541731 : return b->decl;
3496 : : }
3497 : 46125654 : else if (TREE_PUBLIC (x))
3498 : : {
3499 : 45765266 : if (visdecl && !b && duplicate_decls (x, visdecl))
3500 : : {
3501 : : /* An external declaration at block scope referring to a
3502 : : visible entity with internal linkage. The composite
3503 : : type will already be correct for this scope, so we
3504 : : just need to fall through to make the declaration in
3505 : : this scope. */
3506 : : nested = true;
3507 : : x = visdecl;
3508 : : }
3509 : : else
3510 : : {
3511 : 45765149 : bind (name, x, external_scope, /*invisible=*/true,
3512 : : /*nested=*/false, locus);
3513 : 45765149 : nested = true;
3514 : : }
3515 : : }
3516 : : }
3517 : :
3518 : 185485854 : if (TREE_CODE (x) != PARM_DECL)
3519 : 68822873 : warn_if_shadowing (x);
3520 : :
3521 : 116662981 : skip_external_and_shadow_checks:
3522 : 185486217 : if (TREE_CODE (x) == TYPE_DECL)
3523 : : {
3524 : : /* So this is a typedef, set its underlying type. */
3525 : 9538498 : set_underlying_type (x);
3526 : :
3527 : : /* If X is a typedef defined in the current function, record it
3528 : : for the purpose of implementing the -Wunused-local-typedefs
3529 : : warning. */
3530 : 9538498 : record_locally_defined_typedef (x);
3531 : : }
3532 : :
3533 : 185486217 : bind (name, x, scope, /*invisible=*/false, nested, locus);
3534 : :
3535 : : /* If x's type is incomplete because it's based on a
3536 : : structure or union which has not yet been fully declared,
3537 : : attach it to that structure or union type, so we can go
3538 : : back and complete the variable declaration later, if the
3539 : : structure or union gets fully declared.
3540 : :
3541 : : If the input is erroneous, we can have error_mark in the type
3542 : : slot (e.g. "f(void a, ...)") - that doesn't count as an
3543 : : incomplete type. */
3544 : 185486217 : if (TREE_TYPE (x) != error_mark_node
3545 : 185486217 : && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3546 : : {
3547 : 181092 : tree element = TREE_TYPE (x);
3548 : :
3549 : 200927 : while (TREE_CODE (element) == ARRAY_TYPE)
3550 : 19835 : element = TREE_TYPE (element);
3551 : 181092 : element = TYPE_MAIN_VARIANT (element);
3552 : :
3553 : 181092 : if ((RECORD_OR_UNION_TYPE_P (element)
3554 : 138272 : || TREE_CODE (element) == ENUMERAL_TYPE)
3555 : 42891 : && (TREE_CODE (x) != TYPE_DECL
3556 : 39475 : || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3557 : 184516 : && !COMPLETE_TYPE_P (element))
3558 : 281 : C_TYPE_INCOMPLETE_VARS (element)
3559 : 562 : = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3560 : : }
3561 : : return x;
3562 : : }
3563 : :
3564 : :
3565 : : /* Issue a permerror about implicit function declaration. ID is the function
3566 : : identifier, OLDDECL is a declaration of the function in a different scope,
3567 : : or NULL_TREE. */
3568 : :
3569 : : static void
3570 : 3776 : implicit_decl_permerror (location_t loc, tree id, tree olddecl)
3571 : : {
3572 : 3776 : if (!warn_implicit_function_declaration)
3573 : 2617 : return;
3574 : :
3575 : 1159 : bool warned;
3576 : 1159 : auto_diagnostic_group d;
3577 : 1159 : name_hint hint;
3578 : 1159 : if (!olddecl)
3579 : 553 : hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3580 : :
3581 : 1159 : if (flag_isoc99)
3582 : : {
3583 : 1153 : if (const char *suggestion = hint.suggestion ())
3584 : : {
3585 : 107 : gcc_rich_location richloc (loc);
3586 : 107 : richloc.add_fixit_replace (suggestion);
3587 : 107 : warned = permerror_opt (&richloc, OPT_Wimplicit_function_declaration,
3588 : : "implicit declaration of function %qE;"
3589 : : " did you mean %qs?",
3590 : : id, suggestion);
3591 : 107 : }
3592 : : else
3593 : 1046 : warned = permerror_opt (loc, OPT_Wimplicit_function_declaration,
3594 : : "implicit declaration of function %qE", id);
3595 : : }
3596 : 6 : else if (const char *suggestion = hint.suggestion ())
3597 : : {
3598 : 2 : gcc_rich_location richloc (loc);
3599 : 2 : richloc.add_fixit_replace (suggestion);
3600 : 2 : warned = warning_at
3601 : 2 : (&richloc, OPT_Wimplicit_function_declaration,
3602 : : G_("implicit declaration of function %qE; did you mean %qs?"),
3603 : : id, suggestion);
3604 : 2 : }
3605 : : else
3606 : 4 : warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3607 : : G_("implicit declaration of function %qE"), id);
3608 : :
3609 : 1159 : if (warned)
3610 : : {
3611 : : /* Whether the olddecl is an undeclared builtin function.
3612 : : locate_old_decl will not generate a diagnostic for those,
3613 : : so in that case we want to look elsewhere. */
3614 : 90 : bool undeclared_builtin = (olddecl
3615 : 28 : && TREE_CODE (olddecl) == FUNCTION_DECL
3616 : 28 : && fndecl_built_in_p (olddecl)
3617 : 117 : && !C_DECL_DECLARED_BUILTIN (olddecl));
3618 : 90 : if (undeclared_builtin)
3619 : : {
3620 : 27 : const char *header = header_for_builtin_fn (olddecl);
3621 : 27 : if (header)
3622 : : {
3623 : 20 : rich_location richloc (line_table, loc);
3624 : 20 : maybe_add_include_fixit (&richloc, header, true);
3625 : 20 : inform (&richloc,
3626 : : "include %qs or provide a declaration of %qE",
3627 : : header, id);
3628 : 20 : }
3629 : : }
3630 : 63 : else if (olddecl)
3631 : 1 : locate_old_decl (olddecl);
3632 : : }
3633 : :
3634 : 1069 : if (!warned)
3635 : 1159 : hint.suppress ();
3636 : 1159 : }
3637 : :
3638 : : /* Return the name of the header file that declares built-in function
3639 : : FNDECL, or null if either we don't know or don't expect to see an
3640 : : explicit declaration. */
3641 : :
3642 : : static const char *
3643 : 3178 : header_for_builtin_fn (tree fndecl)
3644 : : {
3645 : 3178 : if (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3646 : : return NULL;
3647 : :
3648 : 3178 : switch (DECL_FUNCTION_CODE (fndecl))
3649 : : {
3650 : : CASE_FLT_FN (BUILT_IN_ACOS):
3651 : : CASE_FLT_FN (BUILT_IN_ACOSH):
3652 : : CASE_FLT_FN (BUILT_IN_ASIN):
3653 : : CASE_FLT_FN (BUILT_IN_ASINH):
3654 : : CASE_FLT_FN (BUILT_IN_ATAN):
3655 : : CASE_FLT_FN (BUILT_IN_ATANH):
3656 : : CASE_FLT_FN (BUILT_IN_ATAN2):
3657 : : CASE_FLT_FN (BUILT_IN_CBRT):
3658 : : CASE_FLT_FN (BUILT_IN_CEIL):
3659 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3660 : : CASE_FLT_FN (BUILT_IN_COPYSIGN):
3661 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3662 : : CASE_FLT_FN (BUILT_IN_COS):
3663 : : CASE_FLT_FN (BUILT_IN_COSH):
3664 : : CASE_FLT_FN (BUILT_IN_ERF):
3665 : : CASE_FLT_FN (BUILT_IN_ERFC):
3666 : : CASE_FLT_FN (BUILT_IN_EXP):
3667 : : CASE_FLT_FN (BUILT_IN_EXP2):
3668 : : CASE_FLT_FN (BUILT_IN_EXPM1):
3669 : : CASE_FLT_FN (BUILT_IN_FABS):
3670 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3671 : : CASE_FLT_FN (BUILT_IN_FDIM):
3672 : : CASE_FLT_FN (BUILT_IN_FLOOR):
3673 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3674 : : CASE_FLT_FN (BUILT_IN_FMA):
3675 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3676 : : CASE_FLT_FN (BUILT_IN_FMAX):
3677 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3678 : : CASE_FLT_FN (BUILT_IN_FMIN):
3679 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3680 : : CASE_FLT_FN (BUILT_IN_FMOD):
3681 : : CASE_FLT_FN (BUILT_IN_FREXP):
3682 : : CASE_FLT_FN (BUILT_IN_HYPOT):
3683 : : CASE_FLT_FN (BUILT_IN_ILOGB):
3684 : : CASE_FLT_FN (BUILT_IN_LDEXP):
3685 : : CASE_FLT_FN (BUILT_IN_LGAMMA):
3686 : : CASE_FLT_FN (BUILT_IN_LLRINT):
3687 : : CASE_FLT_FN (BUILT_IN_LLROUND):
3688 : : CASE_FLT_FN (BUILT_IN_LOG):
3689 : : CASE_FLT_FN (BUILT_IN_LOG10):
3690 : : CASE_FLT_FN (BUILT_IN_LOG1P):
3691 : : CASE_FLT_FN (BUILT_IN_LOG2):
3692 : : CASE_FLT_FN (BUILT_IN_LOGB):
3693 : : CASE_FLT_FN (BUILT_IN_LRINT):
3694 : : CASE_FLT_FN (BUILT_IN_LROUND):
3695 : : CASE_FLT_FN (BUILT_IN_MODF):
3696 : : CASE_FLT_FN (BUILT_IN_NAN):
3697 : : CASE_FLT_FN (BUILT_IN_NEARBYINT):
3698 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3699 : : CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3700 : : CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3701 : : CASE_FLT_FN (BUILT_IN_POW):
3702 : : CASE_FLT_FN (BUILT_IN_REMAINDER):
3703 : : CASE_FLT_FN (BUILT_IN_REMQUO):
3704 : : CASE_FLT_FN (BUILT_IN_RINT):
3705 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3706 : : CASE_FLT_FN (BUILT_IN_ROUND):
3707 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3708 : : CASE_FLT_FN (BUILT_IN_SCALBLN):
3709 : : CASE_FLT_FN (BUILT_IN_SCALBN):
3710 : : CASE_FLT_FN (BUILT_IN_SIN):
3711 : : CASE_FLT_FN (BUILT_IN_SINH):
3712 : : CASE_FLT_FN (BUILT_IN_SINCOS):
3713 : : CASE_FLT_FN (BUILT_IN_SQRT):
3714 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3715 : : CASE_FLT_FN (BUILT_IN_TAN):
3716 : : CASE_FLT_FN (BUILT_IN_TANH):
3717 : : CASE_FLT_FN (BUILT_IN_TGAMMA):
3718 : : CASE_FLT_FN (BUILT_IN_TRUNC):
3719 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3720 : : case BUILT_IN_ISINF:
3721 : : case BUILT_IN_ISNAN:
3722 : : return "<math.h>";
3723 : 29 : CASE_FLT_FN (BUILT_IN_CABS):
3724 : 29 : CASE_FLT_FN (BUILT_IN_CACOS):
3725 : 29 : CASE_FLT_FN (BUILT_IN_CACOSH):
3726 : 29 : CASE_FLT_FN (BUILT_IN_CARG):
3727 : 29 : CASE_FLT_FN (BUILT_IN_CASIN):
3728 : 29 : CASE_FLT_FN (BUILT_IN_CASINH):
3729 : 29 : CASE_FLT_FN (BUILT_IN_CATAN):
3730 : 29 : CASE_FLT_FN (BUILT_IN_CATANH):
3731 : 29 : CASE_FLT_FN (BUILT_IN_CCOS):
3732 : 29 : CASE_FLT_FN (BUILT_IN_CCOSH):
3733 : 29 : CASE_FLT_FN (BUILT_IN_CEXP):
3734 : 29 : CASE_FLT_FN (BUILT_IN_CIMAG):
3735 : 29 : CASE_FLT_FN (BUILT_IN_CLOG):
3736 : 29 : CASE_FLT_FN (BUILT_IN_CONJ):
3737 : 29 : CASE_FLT_FN (BUILT_IN_CPOW):
3738 : 29 : CASE_FLT_FN (BUILT_IN_CPROJ):
3739 : 29 : CASE_FLT_FN (BUILT_IN_CREAL):
3740 : 29 : CASE_FLT_FN (BUILT_IN_CSIN):
3741 : 29 : CASE_FLT_FN (BUILT_IN_CSINH):
3742 : 29 : CASE_FLT_FN (BUILT_IN_CSQRT):
3743 : 29 : CASE_FLT_FN (BUILT_IN_CTAN):
3744 : 29 : CASE_FLT_FN (BUILT_IN_CTANH):
3745 : 29 : return "<complex.h>";
3746 : 228 : case BUILT_IN_MEMCHR:
3747 : 228 : case BUILT_IN_MEMCMP:
3748 : 228 : case BUILT_IN_MEMCPY:
3749 : 228 : case BUILT_IN_MEMMOVE:
3750 : 228 : case BUILT_IN_MEMSET:
3751 : 228 : case BUILT_IN_STRCAT:
3752 : 228 : case BUILT_IN_STRCHR:
3753 : 228 : case BUILT_IN_STRCMP:
3754 : 228 : case BUILT_IN_STRCPY:
3755 : 228 : case BUILT_IN_STRCSPN:
3756 : 228 : case BUILT_IN_STRLEN:
3757 : 228 : case BUILT_IN_STRNCAT:
3758 : 228 : case BUILT_IN_STRNCMP:
3759 : 228 : case BUILT_IN_STRNCPY:
3760 : 228 : case BUILT_IN_STRPBRK:
3761 : 228 : case BUILT_IN_STRRCHR:
3762 : 228 : case BUILT_IN_STRSPN:
3763 : 228 : case BUILT_IN_STRSTR:
3764 : 228 : return "<string.h>";
3765 : 544 : case BUILT_IN_FPRINTF:
3766 : 544 : case BUILT_IN_PUTC:
3767 : 544 : case BUILT_IN_FPUTC:
3768 : 544 : case BUILT_IN_FPUTS:
3769 : 544 : case BUILT_IN_FSCANF:
3770 : 544 : case BUILT_IN_FWRITE:
3771 : 544 : case BUILT_IN_PRINTF:
3772 : 544 : case BUILT_IN_PUTCHAR:
3773 : 544 : case BUILT_IN_PUTS:
3774 : 544 : case BUILT_IN_SCANF:
3775 : 544 : case BUILT_IN_SNPRINTF:
3776 : 544 : case BUILT_IN_SPRINTF:
3777 : 544 : case BUILT_IN_SSCANF:
3778 : 544 : case BUILT_IN_VFPRINTF:
3779 : 544 : case BUILT_IN_VFSCANF:
3780 : 544 : case BUILT_IN_VPRINTF:
3781 : 544 : case BUILT_IN_VSCANF:
3782 : 544 : case BUILT_IN_VSNPRINTF:
3783 : 544 : case BUILT_IN_VSPRINTF:
3784 : 544 : case BUILT_IN_VSSCANF:
3785 : 544 : return "<stdio.h>";
3786 : 2 : case BUILT_IN_ISALNUM:
3787 : 2 : case BUILT_IN_ISALPHA:
3788 : 2 : case BUILT_IN_ISBLANK:
3789 : 2 : case BUILT_IN_ISCNTRL:
3790 : 2 : case BUILT_IN_ISDIGIT:
3791 : 2 : case BUILT_IN_ISGRAPH:
3792 : 2 : case BUILT_IN_ISLOWER:
3793 : 2 : case BUILT_IN_ISPRINT:
3794 : 2 : case BUILT_IN_ISPUNCT:
3795 : 2 : case BUILT_IN_ISSPACE:
3796 : 2 : case BUILT_IN_ISUPPER:
3797 : 2 : case BUILT_IN_ISXDIGIT:
3798 : 2 : case BUILT_IN_TOLOWER:
3799 : 2 : case BUILT_IN_TOUPPER:
3800 : 2 : return "<ctype.h>";
3801 : 0 : case BUILT_IN_ISWALNUM:
3802 : 0 : case BUILT_IN_ISWALPHA:
3803 : 0 : case BUILT_IN_ISWBLANK:
3804 : 0 : case BUILT_IN_ISWCNTRL:
3805 : 0 : case BUILT_IN_ISWDIGIT:
3806 : 0 : case BUILT_IN_ISWGRAPH:
3807 : 0 : case BUILT_IN_ISWLOWER:
3808 : 0 : case BUILT_IN_ISWPRINT:
3809 : 0 : case BUILT_IN_ISWPUNCT:
3810 : 0 : case BUILT_IN_ISWSPACE:
3811 : 0 : case BUILT_IN_ISWUPPER:
3812 : 0 : case BUILT_IN_ISWXDIGIT:
3813 : 0 : case BUILT_IN_TOWLOWER:
3814 : 0 : case BUILT_IN_TOWUPPER:
3815 : 0 : return "<wctype.h>";
3816 : 1818 : case BUILT_IN_ABORT:
3817 : 1818 : case BUILT_IN_ABS:
3818 : 1818 : case BUILT_IN_CALLOC:
3819 : 1818 : case BUILT_IN_EXIT:
3820 : 1818 : case BUILT_IN_FREE:
3821 : 1818 : case BUILT_IN_LABS:
3822 : 1818 : case BUILT_IN_LLABS:
3823 : 1818 : case BUILT_IN_MALLOC:
3824 : 1818 : case BUILT_IN_REALLOC:
3825 : 1818 : case BUILT_IN__EXIT2:
3826 : 1818 : case BUILT_IN_ALIGNED_ALLOC:
3827 : 1818 : return "<stdlib.h>";
3828 : 1 : case BUILT_IN_IMAXABS:
3829 : 1 : return "<inttypes.h>";
3830 : 3 : case BUILT_IN_STRFTIME:
3831 : 3 : return "<time.h>";
3832 : : default:
3833 : : return NULL;
3834 : : }
3835 : : }
3836 : :
3837 : : /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3838 : : function of type int (). */
3839 : :
3840 : : tree
3841 : 4729 : implicitly_declare (location_t loc, tree functionid)
3842 : : {
3843 : 4729 : struct c_binding *b;
3844 : 4729 : tree decl = NULL_TREE;
3845 : 4729 : tree asmspec_tree;
3846 : :
3847 : 4743 : for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3848 : : {
3849 : 3158 : if (B_IN_SCOPE (b, external_scope))
3850 : : {
3851 : 3144 : decl = b->decl;
3852 : 3144 : break;
3853 : : }
3854 : : }
3855 : :
3856 : 4729 : if (decl)
3857 : : {
3858 : 3144 : if (TREE_CODE (decl) != FUNCTION_DECL)
3859 : : return decl;
3860 : :
3861 : : /* FIXME: Objective-C has weird not-really-builtin functions
3862 : : which are supposed to be visible automatically. They wind up
3863 : : in the external scope because they're pushed before the file
3864 : : scope gets created. Catch this here and rebind them into the
3865 : : file scope. */
3866 : 3137 : if (!fndecl_built_in_p (decl) && DECL_IS_UNDECLARED_BUILTIN (decl))
3867 : : {
3868 : 0 : bind (functionid, decl, file_scope,
3869 : : /*invisible=*/false, /*nested=*/true,
3870 : 0 : DECL_SOURCE_LOCATION (decl));
3871 : 0 : return decl;
3872 : : }
3873 : : else
3874 : : {
3875 : 3137 : tree newtype = default_function_type;
3876 : 3137 : if (b->u.type)
3877 : 753 : TREE_TYPE (decl) = b->u.type;
3878 : : /* Implicit declaration of a function already declared
3879 : : (somehow) in a different scope, or as a built-in.
3880 : : If this is the first time this has happened, warn;
3881 : : then recycle the old declaration but with the new type. */
3882 : 3137 : if (!C_DECL_IMPLICIT (decl))
3883 : : {
3884 : 2191 : implicit_decl_permerror (loc, functionid, decl);
3885 : 2191 : C_DECL_IMPLICIT (decl) = 1;
3886 : : }
3887 : 3137 : if (fndecl_built_in_p (decl))
3888 : : {
3889 : 2705 : newtype = c_build_type_attribute_variant (newtype,
3890 : 2705 : TYPE_ATTRIBUTES
3891 : : (TREE_TYPE (decl)));
3892 : 2705 : if (!comptypes (newtype, TREE_TYPE (decl)))
3893 : : {
3894 : 2528 : auto_diagnostic_group d;
3895 : 2528 : bool warned = warning_at (loc,
3896 : 2528 : OPT_Wbuiltin_declaration_mismatch,
3897 : : "incompatible implicit "
3898 : : "declaration of built-in "
3899 : : "function %qD", decl);
3900 : : /* See if we can hint which header to include. */
3901 : 2528 : const char *header = header_for_builtin_fn (decl);
3902 : 2528 : if (header != NULL && warned)
3903 : : {
3904 : 133 : rich_location richloc (line_table, loc);
3905 : 133 : maybe_add_include_fixit (&richloc, header, true);
3906 : 133 : inform (&richloc,
3907 : : "include %qs or provide a declaration of %qD",
3908 : : header, decl);
3909 : 133 : }
3910 : 2528 : newtype = TREE_TYPE (decl);
3911 : 2528 : }
3912 : : }
3913 : : else
3914 : : {
3915 : 432 : if (!comptypes (newtype, TREE_TYPE (decl)))
3916 : : {
3917 : 2 : auto_diagnostic_group d;
3918 : 2 : error_at (loc, "incompatible implicit declaration of "
3919 : : "function %qD", decl);
3920 : 2 : locate_old_decl (decl);
3921 : 2 : }
3922 : : }
3923 : 3137 : b->u.type = TREE_TYPE (decl);
3924 : 3137 : TREE_TYPE (decl) = newtype;
3925 : 3137 : bind (functionid, decl, current_scope,
3926 : : /*invisible=*/false, /*nested=*/true,
3927 : 3137 : DECL_SOURCE_LOCATION (decl));
3928 : 3137 : return decl;
3929 : : }
3930 : : }
3931 : :
3932 : : /* Not seen before. */
3933 : 1585 : decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3934 : 1585 : DECL_EXTERNAL (decl) = 1;
3935 : 1585 : TREE_PUBLIC (decl) = 1;
3936 : 1585 : C_DECL_IMPLICIT (decl) = 1;
3937 : 1585 : implicit_decl_permerror (loc, functionid, 0);
3938 : 1585 : asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3939 : 1585 : if (asmspec_tree)
3940 : 1 : set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3941 : :
3942 : : /* C89 says implicit declarations are in the innermost block.
3943 : : So we record the decl in the standard fashion. */
3944 : 1585 : decl = pushdecl (decl);
3945 : :
3946 : : /* No need to call objc_check_decl here - it's a function type. */
3947 : 1585 : rest_of_decl_compilation (decl, 0, 0);
3948 : :
3949 : : /* Write a record describing this implicit function declaration
3950 : : to the prototypes file (if requested). */
3951 : 1585 : gen_aux_info_record (decl, 0, 1, 0);
3952 : :
3953 : : /* Possibly apply some default attributes to this implicit declaration. */
3954 : 1585 : decl_attributes (&decl, NULL_TREE, 0);
3955 : :
3956 : 1585 : return decl;
3957 : : }
3958 : :
3959 : : /* Issue an error message for a reference to an undeclared variable
3960 : : ID, including a reference to a builtin outside of function-call
3961 : : context. Establish a binding of the identifier to error_mark_node
3962 : : in an appropriate scope, which will suppress further errors for the
3963 : : same identifier. The error message should be given location LOC. */
3964 : : void
3965 : 1290 : undeclared_variable (location_t loc, tree id)
3966 : : {
3967 : 1290 : static bool already = false;
3968 : 1290 : struct c_scope *scope;
3969 : :
3970 : 1290 : auto_diagnostic_group d;
3971 : 1290 : if (current_function_decl == NULL_TREE)
3972 : : {
3973 : 476 : name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3974 : 476 : if (const char *suggestion = guessed_id.suggestion ())
3975 : : {
3976 : 86 : gcc_rich_location richloc (loc);
3977 : 86 : richloc.add_fixit_replace (suggestion);
3978 : 86 : error_at (&richloc,
3979 : : "%qE undeclared here (not in a function);"
3980 : : " did you mean %qs?",
3981 : : id, suggestion);
3982 : 86 : }
3983 : : else
3984 : 390 : error_at (loc, "%qE undeclared here (not in a function)", id);
3985 : 476 : scope = current_scope;
3986 : 476 : }
3987 : : else
3988 : : {
3989 : 814 : if (!objc_diagnose_private_ivar (id))
3990 : : {
3991 : 814 : name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3992 : 814 : if (const char *suggestion = guessed_id.suggestion ())
3993 : : {
3994 : 36 : gcc_rich_location richloc (loc);
3995 : 36 : richloc.add_fixit_replace (suggestion);
3996 : 36 : error_at (&richloc,
3997 : : "%qE undeclared (first use in this function);"
3998 : : " did you mean %qs?",
3999 : : id, suggestion);
4000 : 36 : }
4001 : : else
4002 : 778 : error_at (loc, "%qE undeclared (first use in this function)", id);
4003 : 814 : }
4004 : 814 : if (!already)
4005 : : {
4006 : 197 : inform (loc, "each undeclared identifier is reported only"
4007 : : " once for each function it appears in");
4008 : 197 : already = true;
4009 : : }
4010 : :
4011 : : /* If we are parsing old-style parameter decls, current_function_decl
4012 : : will be nonnull but current_function_scope will be null. */
4013 : 814 : scope = current_function_scope ? current_function_scope : current_scope;
4014 : : }
4015 : 1290 : bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
4016 : : UNKNOWN_LOCATION);
4017 : 1290 : }
4018 : :
4019 : : /* Subroutine of lookup_label, declare_label, define_label: construct a
4020 : : LABEL_DECL with all the proper frills. Also create a struct
4021 : : c_label_vars initialized for the current scope. */
4022 : :
4023 : : static tree
4024 : 23679 : make_label (location_t location, tree name, bool defining,
4025 : : struct c_label_vars **p_label_vars)
4026 : : {
4027 : 23679 : tree label = build_decl (location, LABEL_DECL, name, void_type_node);
4028 : 23679 : DECL_CONTEXT (label) = current_function_decl;
4029 : 23679 : SET_DECL_MODE (label, VOIDmode);
4030 : :
4031 : 23679 : c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
4032 : 23679 : label_vars->shadowed = NULL;
4033 : 23679 : set_spot_bindings (&label_vars->label_bindings, defining);
4034 : 23679 : label_vars->decls_in_scope = make_tree_vector ();
4035 : 23679 : label_vars->gotos = NULL;
4036 : 23679 : *p_label_vars = label_vars;
4037 : :
4038 : 23679 : return label;
4039 : : }
4040 : :
4041 : : /* Get the LABEL_DECL corresponding to identifier NAME as a label.
4042 : : Create one if none exists so far for the current function.
4043 : : This is called when a label is used in a goto expression or
4044 : : has its address taken. */
4045 : :
4046 : : tree
4047 : 85122 : lookup_label (tree name)
4048 : : {
4049 : 85122 : tree label;
4050 : 85122 : struct c_label_vars *label_vars;
4051 : :
4052 : 85122 : if (current_function_scope == 0)
4053 : : {
4054 : 2 : error ("label %qE referenced outside of any function", name);
4055 : 2 : return NULL_TREE;
4056 : : }
4057 : :
4058 : : /* Use a label already defined or ref'd with this name, but not if
4059 : : it is inherited from a containing function and wasn't declared
4060 : : using __label__. */
4061 : 85120 : label = I_LABEL_DECL (name);
4062 : 79316 : if (label && (DECL_CONTEXT (label) == current_function_decl
4063 : 606 : || C_DECLARED_LABEL_FLAG (label)))
4064 : : {
4065 : : /* If the label has only been declared, update its apparent
4066 : : location to point here, for better diagnostics if it
4067 : : turns out not to have been defined. */
4068 : 79309 : if (DECL_INITIAL (label) == NULL_TREE)
4069 : 62648 : DECL_SOURCE_LOCATION (label) = input_location;
4070 : 79309 : return label;
4071 : : }
4072 : :
4073 : : /* No label binding for that identifier; make one. */
4074 : 5811 : label = make_label (input_location, name, false, &label_vars);
4075 : :
4076 : : /* Ordinary labels go in the current function scope. */
4077 : 5811 : bind_label (name, label, current_function_scope, label_vars);
4078 : :
4079 : 5811 : return label;
4080 : : }
4081 : :
4082 : : /* Issue a warning about DECL for a goto statement at GOTO_LOC going
4083 : : to LABEL. */
4084 : :
4085 : : static void
4086 : 1482 : warn_about_goto (location_t goto_loc, tree label, tree decl)
4087 : : {
4088 : 1482 : auto_diagnostic_group d;
4089 : 1482 : if (c_type_variably_modified_p (TREE_TYPE (decl)))
4090 : 1476 : error_at (goto_loc,
4091 : : "jump into scope of identifier with variably modified type");
4092 : 6 : else if (flag_openmp
4093 : 6 : && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl)))
4094 : 2 : error_at (goto_loc, "jump skips OpenMP %<allocate%> allocation");
4095 : : else
4096 : 4 : if (!warning_at (goto_loc, OPT_Wjump_misses_init,
4097 : : "jump skips variable initialization"))
4098 : 0 : return;
4099 : 1482 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4100 : 1482 : inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
4101 : 1482 : }
4102 : :
4103 : : /* Look up a label because of a goto statement. This is like
4104 : : lookup_label, but also issues any appropriate warnings. */
4105 : :
4106 : : tree
4107 : 83295 : lookup_label_for_goto (location_t loc, tree name)
4108 : : {
4109 : 83295 : tree label;
4110 : 83295 : struct c_label_vars *label_vars;
4111 : 83295 : unsigned int ix;
4112 : 83295 : tree decl;
4113 : :
4114 : 83295 : label = lookup_label (name);
4115 : 83295 : if (label == NULL_TREE)
4116 : : return NULL_TREE;
4117 : :
4118 : : /* If we are jumping to a different function, we can't issue any
4119 : : useful warnings. */
4120 : 83295 : if (DECL_CONTEXT (label) != current_function_decl)
4121 : : {
4122 : 520 : gcc_assert (C_DECLARED_LABEL_FLAG (label));
4123 : : return label;
4124 : : }
4125 : :
4126 : 82775 : label_vars = I_LABEL_BINDING (name)->u.label;
4127 : :
4128 : : /* If the label has not yet been defined, then push this goto on a
4129 : : list for possible later warnings. */
4130 : 82775 : if (label_vars->label_bindings.scope == NULL)
4131 : : {
4132 : 66339 : c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
4133 : :
4134 : 66339 : g->loc = loc;
4135 : 66339 : set_spot_bindings (&g->goto_bindings, true);
4136 : 66339 : vec_safe_push (label_vars->gotos, g);
4137 : 66339 : return label;
4138 : : }
4139 : :
4140 : : /* If there are any decls in label_vars->decls_in_scope, then this
4141 : : goto has missed the declaration of the decl. This happens for a
4142 : : case like
4143 : : int i = 1;
4144 : : lab:
4145 : : ...
4146 : : goto lab;
4147 : : Issue a warning or error. */
4148 : 17211 : FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
4149 : 775 : warn_about_goto (loc, label, decl);
4150 : :
4151 : 16436 : if (label_vars->label_bindings.left_stmt_expr)
4152 : : {
4153 : 120 : auto_diagnostic_group d;
4154 : 120 : error_at (loc, "jump into statement expression");
4155 : 120 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4156 : 120 : }
4157 : :
4158 : : return label;
4159 : : }
4160 : :
4161 : : /* Make a label named NAME in the current function, shadowing silently
4162 : : any that may be inherited from containing functions or containing
4163 : : scopes. This is called for __label__ declarations. */
4164 : :
4165 : : tree
4166 : 1181 : declare_label (tree name)
4167 : : {
4168 : 1181 : struct c_binding *b = I_LABEL_BINDING (name);
4169 : 1181 : tree label;
4170 : 1181 : struct c_label_vars *label_vars;
4171 : :
4172 : : /* Check to make sure that the label hasn't already been declared
4173 : : at this scope */
4174 : 1181 : if (b && B_IN_CURRENT_SCOPE (b))
4175 : : {
4176 : 2 : auto_diagnostic_group d;
4177 : 2 : error ("duplicate label declaration %qE", name);
4178 : 2 : locate_old_decl (b->decl);
4179 : :
4180 : : /* Just use the previous declaration. */
4181 : 2 : return b->decl;
4182 : 2 : }
4183 : :
4184 : 1179 : label = make_label (input_location, name, false, &label_vars);
4185 : 1179 : C_DECLARED_LABEL_FLAG (label) = 1;
4186 : :
4187 : : /* Declared labels go in the current scope. */
4188 : 1179 : bind_label (name, label, current_scope, label_vars);
4189 : :
4190 : 1179 : return label;
4191 : : }
4192 : :
4193 : : /* When we define a label, issue any appropriate warnings if there are
4194 : : any gotos earlier in the function which jump to this label. */
4195 : :
4196 : : static void
4197 : 6910 : check_earlier_gotos (tree label, struct c_label_vars* label_vars)
4198 : : {
4199 : 6910 : unsigned int ix;
4200 : 6910 : struct c_goto_bindings *g;
4201 : :
4202 : 73183 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
4203 : : {
4204 : 66273 : struct c_binding *b;
4205 : 66273 : struct c_scope *scope;
4206 : :
4207 : : /* We have a goto to this label. The goto is going forward. In
4208 : : g->scope, the goto is going to skip any binding which was
4209 : : defined after g->bindings_in_scope. */
4210 : 66273 : if (g->goto_bindings.scope->has_jump_unsafe_decl)
4211 : : {
4212 : 251 : for (b = g->goto_bindings.scope->bindings;
4213 : 581 : b != g->goto_bindings.bindings_in_scope;
4214 : 330 : b = b->prev)
4215 : : {
4216 : 330 : if (decl_jump_unsafe (b->decl))
4217 : 168 : warn_about_goto (g->loc, label, b->decl);
4218 : : }
4219 : : }
4220 : :
4221 : : /* We also need to warn about decls defined in any scopes
4222 : : between the scope of the label and the scope of the goto. */
4223 : 66273 : for (scope = label_vars->label_bindings.scope;
4224 : 68892 : scope != g->goto_bindings.scope;
4225 : 2619 : scope = scope->outer)
4226 : : {
4227 : 2619 : gcc_assert (scope != NULL);
4228 : 2619 : if (scope->has_jump_unsafe_decl)
4229 : : {
4230 : 325 : if (scope == label_vars->label_bindings.scope)
4231 : 249 : b = label_vars->label_bindings.bindings_in_scope;
4232 : : else
4233 : 76 : b = scope->bindings;
4234 : 864 : for (; b != NULL; b = b->prev)
4235 : : {
4236 : 539 : if (decl_jump_unsafe (b->decl))
4237 : 539 : warn_about_goto (g->loc, label, b->decl);
4238 : : }
4239 : : }
4240 : : }
4241 : :
4242 : 66273 : if (g->goto_bindings.stmt_exprs > 0)
4243 : : {
4244 : 100 : auto_diagnostic_group d;
4245 : 100 : error_at (g->loc, "jump into statement expression");
4246 : 100 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
4247 : : label);
4248 : 100 : }
4249 : : }
4250 : :
4251 : : /* Now that the label is defined, we will issue warnings about
4252 : : subsequent gotos to this label when we see them. */
4253 : 6910 : vec_safe_truncate (label_vars->gotos, 0);
4254 : 6910 : label_vars->gotos = NULL;
4255 : 6910 : }
4256 : :
4257 : : /* Define a label, specifying the location in the source file.
4258 : : Return the LABEL_DECL node for the label, if the definition is valid.
4259 : : Otherwise return NULL_TREE. */
4260 : :
4261 : : tree
4262 : 23628 : define_label (location_t location, tree name)
4263 : : {
4264 : : /* Find any preexisting label with this name. It is an error
4265 : : if that label has already been defined in this function, or
4266 : : if there is a containing function with a declared label with
4267 : : the same name. */
4268 : 23628 : tree label = I_LABEL_DECL (name);
4269 : :
4270 : 6946 : if (label
4271 : 6946 : && ((DECL_CONTEXT (label) == current_function_decl
4272 : 6932 : && DECL_INITIAL (label) != NULL_TREE)
4273 : 6924 : || (DECL_CONTEXT (label) != current_function_decl
4274 : 14 : && C_DECLARED_LABEL_FLAG (label))))
4275 : : {
4276 : 29 : auto_diagnostic_group d;
4277 : 29 : error_at (location, "duplicate label %qD", label);
4278 : 29 : locate_old_decl (label);
4279 : 29 : return NULL_TREE;
4280 : 29 : }
4281 : 23599 : else if (label && DECL_CONTEXT (label) == current_function_decl)
4282 : : {
4283 : 6910 : struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
4284 : :
4285 : : /* The label has been used or declared already in this function,
4286 : : but not defined. Update its location to point to this
4287 : : definition. */
4288 : 6910 : DECL_SOURCE_LOCATION (label) = location;
4289 : 6910 : set_spot_bindings (&label_vars->label_bindings, true);
4290 : :
4291 : : /* Issue warnings as required about any goto statements from
4292 : : earlier in the function. */
4293 : 6910 : check_earlier_gotos (label, label_vars);
4294 : : }
4295 : : else
4296 : : {
4297 : 16689 : struct c_label_vars *label_vars;
4298 : :
4299 : : /* No label binding for that identifier; make one. */
4300 : 16689 : label = make_label (location, name, true, &label_vars);
4301 : :
4302 : : /* Ordinary labels go in the current function scope. */
4303 : 16689 : bind_label (name, label, current_function_scope, label_vars);
4304 : : }
4305 : :
4306 : 23599 : if (!in_system_header_at (input_location) && lookup_name (name))
4307 : 131 : warning_at (location, OPT_Wtraditional,
4308 : : "traditional C lacks a separate namespace "
4309 : : "for labels, identifier %qE conflicts", name);
4310 : :
4311 : : /* Mark label as having been defined. */
4312 : 23599 : DECL_INITIAL (label) = error_mark_node;
4313 : 23599 : return label;
4314 : : }
4315 : :
4316 : : /* Get the bindings for a new switch statement. This is used to issue
4317 : : warnings as appropriate for jumps from the switch to case or
4318 : : default labels. */
4319 : :
4320 : : struct c_spot_bindings *
4321 : 36597 : c_get_switch_bindings (void)
4322 : : {
4323 : 36597 : struct c_spot_bindings *switch_bindings;
4324 : :
4325 : 36597 : switch_bindings = XNEW (struct c_spot_bindings);
4326 : 36597 : set_spot_bindings (switch_bindings, true);
4327 : 36597 : return switch_bindings;
4328 : : }
4329 : :
4330 : : void
4331 : 36597 : c_release_switch_bindings (struct c_spot_bindings *bindings)
4332 : : {
4333 : 36597 : gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
4334 : 36597 : XDELETE (bindings);
4335 : 36597 : }
4336 : :
4337 : : /* This is called at the point of a case or default label to issue
4338 : : warnings about decls as needed. It returns true if it found an
4339 : : error, not just a warning. */
4340 : :
4341 : : bool
4342 : 1024603 : c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
4343 : : location_t switch_loc, location_t case_loc)
4344 : : {
4345 : 1024603 : bool saw_error;
4346 : 1024603 : struct c_scope *scope;
4347 : :
4348 : 1024603 : saw_error = false;
4349 : 1024603 : for (scope = current_scope;
4350 : 3073841 : scope != switch_bindings->scope;
4351 : 2049238 : scope = scope->outer)
4352 : : {
4353 : 2049238 : struct c_binding *b;
4354 : :
4355 : 2049238 : gcc_assert (scope != NULL);
4356 : :
4357 : 2049238 : if (!scope->has_jump_unsafe_decl)
4358 : 2049227 : continue;
4359 : :
4360 : 22 : for (b = scope->bindings; b != NULL; b = b->prev)
4361 : : {
4362 : 11 : if (decl_jump_unsafe (b->decl))
4363 : : {
4364 : 11 : auto_diagnostic_group d;
4365 : 11 : bool emitted;
4366 : 11 : if (c_type_variably_modified_p (TREE_TYPE (b->decl)))
4367 : : {
4368 : 6 : saw_error = true;
4369 : 6 : error_at (case_loc,
4370 : : "switch jumps into scope of identifier with "
4371 : : "variably modified type");
4372 : 6 : emitted = true;
4373 : : }
4374 : 5 : else if (flag_openmp
4375 : 7 : && lookup_attribute ("omp allocate",
4376 : 2 : DECL_ATTRIBUTES (b->decl)))
4377 : : {
4378 : 2 : saw_error = true;
4379 : 2 : error_at (case_loc,
4380 : : "switch jumps over OpenMP %<allocate%> allocation");
4381 : 2 : emitted = true;
4382 : : }
4383 : : else
4384 : 3 : emitted
4385 : 3 : = warning_at (case_loc, OPT_Wjump_misses_init,
4386 : : "switch jumps over variable initialization");
4387 : 11 : if (emitted)
4388 : : {
4389 : 11 : inform (switch_loc, "switch starts here");
4390 : 11 : inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
4391 : : b->decl);
4392 : : }
4393 : 11 : }
4394 : : }
4395 : : }
4396 : :
4397 : 1024603 : if (switch_bindings->stmt_exprs > 0)
4398 : : {
4399 : 4 : saw_error = true;
4400 : 4 : auto_diagnostic_group d;
4401 : 4 : error_at (case_loc, "switch jumps into statement expression");
4402 : 4 : inform (switch_loc, "switch starts here");
4403 : 4 : }
4404 : :
4405 : 1024603 : return saw_error;
4406 : : }
4407 : :
4408 : : /* Given NAME, an IDENTIFIER_NODE,
4409 : : return the structure (or union or enum) definition for that name.
4410 : : If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4411 : : CODE says which kind of type the caller wants;
4412 : : it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4413 : : If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4414 : : location where the tag was defined.
4415 : : If the wrong kind of type is found, an error is reported. */
4416 : :
4417 : : static tree
4418 : 2043973 : lookup_tag (enum tree_code code, tree name, bool thislevel_only,
4419 : : location_t *ploc)
4420 : : {
4421 : 2043973 : struct c_binding *b = I_TAG_BINDING (name);
4422 : 2043973 : bool thislevel = false;
4423 : :
4424 : 2043973 : if (!b || !b->decl)
4425 : : return NULL_TREE;
4426 : :
4427 : : /* We only care about whether it's in this level if
4428 : : thislevel_only was set or it might be a type clash. */
4429 : 1430784 : if (thislevel_only || TREE_CODE (b->decl) != code)
4430 : : {
4431 : : /* For our purposes, a tag in the external scope is the same as
4432 : : a tag in the file scope. (Primarily relevant to Objective-C
4433 : : and its builtin structure tags, which get pushed before the
4434 : : file scope is created.) */
4435 : 458572 : if (B_IN_CURRENT_SCOPE (b)
4436 : 179 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
4437 : 1430784 : thislevel = true;
4438 : : }
4439 : :
4440 : 1430784 : if (thislevel_only && !thislevel)
4441 : : return NULL_TREE;
4442 : :
4443 : 1430626 : if (TREE_CODE (b->decl) != code)
4444 : : {
4445 : : /* Definition isn't the kind we were looking for. */
4446 : 39 : pending_invalid_xref = name;
4447 : 39 : pending_invalid_xref_location = input_location;
4448 : :
4449 : : /* If in the same binding level as a declaration as a tag
4450 : : of a different type, this must not be allowed to
4451 : : shadow that tag, so give the error immediately.
4452 : : (For example, "struct foo; union foo;" is invalid.) */
4453 : 39 : if (thislevel)
4454 : 18 : pending_xref_error ();
4455 : : }
4456 : :
4457 : 1430626 : if (ploc != NULL)
4458 : 997230 : *ploc = b->locus;
4459 : :
4460 : 1430626 : return b->decl;
4461 : : }
4462 : :
4463 : : /* Return true if a definition exists for NAME with code CODE. */
4464 : :
4465 : : bool
4466 : 384 : tag_exists_p (enum tree_code code, tree name)
4467 : : {
4468 : 384 : struct c_binding *b = I_TAG_BINDING (name);
4469 : :
4470 : 384 : if (b == NULL || b->decl == NULL_TREE)
4471 : : return false;
4472 : 18 : return TREE_CODE (b->decl) == code;
4473 : : }
4474 : :
4475 : : /* Print an error message now
4476 : : for a recent invalid struct, union or enum cross reference.
4477 : : We don't print them immediately because they are not invalid
4478 : : when used in the `struct foo;' construct for shadowing. */
4479 : :
4480 : : void
4481 : 306054387 : pending_xref_error (void)
4482 : : {
4483 : 306054387 : if (pending_invalid_xref != NULL_TREE)
4484 : 27 : error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4485 : : pending_invalid_xref);
4486 : 306054387 : pending_invalid_xref = NULL_TREE;
4487 : 306054387 : }
4488 : :
4489 : :
4490 : : /* Look up NAME in the current scope and its superiors
4491 : : in the namespace of variables, functions and typedefs.
4492 : : Return a ..._DECL node of some kind representing its definition,
4493 : : or return NULL_TREE if it is undefined. */
4494 : :
4495 : : tree
4496 : 1236236516 : lookup_name (tree name)
4497 : : {
4498 : 1236236516 : struct c_binding *b = I_SYMBOL_BINDING (name);
4499 : 1236236516 : if (b && !b->invisible)
4500 : : {
4501 : 824531795 : maybe_record_typedef_use (b->decl);
4502 : 824531795 : return b->decl;
4503 : : }
4504 : : return NULL_TREE;
4505 : : }
4506 : :
4507 : : /* Similar to `lookup_name' but look only at the indicated scope. */
4508 : :
4509 : : static tree
4510 : 128274063 : lookup_name_in_scope (tree name, struct c_scope *scope)
4511 : : {
4512 : 128274063 : struct c_binding *b;
4513 : :
4514 : 128277230 : for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4515 : 7311061 : if (B_IN_SCOPE (b, scope))
4516 : 7307894 : return b->decl;
4517 : : return NULL_TREE;
4518 : : }
4519 : :
4520 : : /* Look for the closest match for NAME within the currently valid
4521 : : scopes.
4522 : :
4523 : : This finds the identifier with the lowest Levenshtein distance to
4524 : : NAME. If there are multiple candidates with equal minimal distance,
4525 : : the first one found is returned. Scopes are searched from innermost
4526 : : outwards, and within a scope in reverse order of declaration, thus
4527 : : benefiting candidates "near" to the current scope.
4528 : :
4529 : : The function also looks for similar macro names to NAME, since a
4530 : : misspelled macro name will not be expanded, and hence looks like an
4531 : : identifier to the C frontend.
4532 : :
4533 : : It also looks for start_typename keywords, to detect "singed" vs "signed"
4534 : : typos.
4535 : :
4536 : : Use LOC for any deferred diagnostics. */
4537 : :
4538 : : name_hint
4539 : 2095 : lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4540 : : {
4541 : 2095 : gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4542 : :
4543 : : /* First, try some well-known names in the C standard library, in case
4544 : : the user forgot a #include. */
4545 : 2095 : const char *header_hint
4546 : 2095 : = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4547 : :
4548 : 2095 : if (header_hint)
4549 : 56 : return name_hint
4550 : : (nullptr,
4551 : 56 : std::make_unique<suggest_missing_header> (loc,
4552 : 112 : IDENTIFIER_POINTER (name),
4553 : 56 : header_hint));
4554 : :
4555 : : /* Next, look for exact matches for builtin defines that would have been
4556 : : defined if the user had passed a command-line option (e.g. -fopenmp
4557 : : for "_OPENMP"). */
4558 : 2039 : diagnostics::option_id option_id
4559 : 2039 : = get_option_for_builtin_define (IDENTIFIER_POINTER (name));
4560 : 2039 : if (option_id.m_idx > 0)
4561 : 2 : return name_hint
4562 : : (nullptr,
4563 : 2 : std::make_unique<suggest_missing_option> (loc,
4564 : 4 : IDENTIFIER_POINTER (name),
4565 : 2 : option_id));
4566 : :
4567 : : /* Only suggest names reserved for the implementation if NAME begins
4568 : : with an underscore. */
4569 : 2037 : bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4570 : :
4571 : 2037 : best_match<tree, tree> bm (name);
4572 : :
4573 : : /* Look within currently valid scopes. */
4574 : 9204 : for (c_scope *scope = current_scope; scope; scope = scope->outer)
4575 : 11524511 : for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4576 : : {
4577 : 11517344 : if (!binding->id || binding->invisible)
4578 : 6517269 : continue;
4579 : 5000075 : if (binding->decl == error_mark_node)
4580 : 358 : continue;
4581 : : /* Don't use bindings from implicitly declared functions,
4582 : : as they were likely misspellings themselves. */
4583 : 4999717 : if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4584 : 4594381 : if (C_DECL_IMPLICIT (binding->decl))
4585 : 229 : continue;
4586 : : /* Don't suggest names that are reserved for use by the
4587 : : implementation, unless NAME began with an underscore. */
4588 : 4999488 : if (!consider_implementation_names)
4589 : : {
4590 : 2326036 : const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4591 : 2326036 : if (name_reserved_for_implementation_p (suggestion_str))
4592 : 2268161 : continue;
4593 : : }
4594 : 2731327 : switch (kind)
4595 : : {
4596 : 24310 : case FUZZY_LOOKUP_TYPENAME:
4597 : 24310 : if (TREE_CODE (binding->decl) != TYPE_DECL)
4598 : 17005 : continue;
4599 : : break;
4600 : :
4601 : 128872 : case FUZZY_LOOKUP_FUNCTION_NAME:
4602 : 128872 : if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4603 : : {
4604 : : /* Allow function pointers. */
4605 : 26502 : if ((VAR_P (binding->decl)
4606 : 21977 : || TREE_CODE (binding->decl) == PARM_DECL)
4607 : 5400 : && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4608 : 27782 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4609 : : == FUNCTION_TYPE))
4610 : : break;
4611 : 26483 : continue;
4612 : : }
4613 : : break;
4614 : :
4615 : : default:
4616 : : break;
4617 : : }
4618 : 2687839 : bm.consider (binding->id);
4619 : : }
4620 : :
4621 : : /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4622 : : as:
4623 : : x = SOME_OTHER_MACRO (y);
4624 : : then "SOME_OTHER_MACRO" will survive to the frontend and show up
4625 : : as a misspelled identifier.
4626 : :
4627 : : Use the best distance so far so that a candidate is only set if
4628 : : a macro is better than anything so far. This allows early rejection
4629 : : (without calculating the edit distance) of macro names that must have
4630 : : distance >= bm.get_best_distance (), and means that we only get a
4631 : : non-NULL result for best_macro_match if it's better than any of
4632 : : the identifiers already checked, which avoids needless creation
4633 : : of identifiers for macro hashnodes. */
4634 : 2037 : best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4635 : 2037 : cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4636 : : /* If a macro is the closest so far to NAME, use it, creating an
4637 : : identifier tree node for it. */
4638 : 2037 : if (best_macro)
4639 : : {
4640 : 11 : const char *id = (const char *)best_macro->ident.str;
4641 : 11 : tree macro_as_identifier
4642 : 11 : = get_identifier_with_length (id, best_macro->ident.len);
4643 : 11 : bm.set_best_so_far (macro_as_identifier,
4644 : : bmm.get_best_distance (),
4645 : : bmm.get_best_candidate_length ());
4646 : : }
4647 : :
4648 : : /* Try the "start_typename" keywords to detect
4649 : : "singed" vs "signed" typos. */
4650 : 2037 : if (kind == FUZZY_LOOKUP_TYPENAME)
4651 : : {
4652 : 54731 : for (unsigned i = 0; i < num_c_common_reswords; i++)
4653 : : {
4654 : 54492 : const c_common_resword *resword = &c_common_reswords[i];
4655 : 54492 : if (!c_keyword_starts_typename (resword->rid))
4656 : 41347 : continue;
4657 : 13145 : tree resword_identifier = ridpointers [resword->rid];
4658 : 13145 : if (!resword_identifier)
4659 : 30 : continue;
4660 : 13115 : gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4661 : 13115 : bm.consider (resword_identifier);
4662 : : }
4663 : : }
4664 : :
4665 : 2037 : tree best = bm.get_best_meaningful_candidate ();
4666 : 2037 : if (best)
4667 : 275 : return name_hint (IDENTIFIER_POINTER (best), NULL);
4668 : : else
4669 : 1762 : return name_hint (NULL, NULL);
4670 : : }
4671 : :
4672 : :
4673 : : /* Handle the standard [[nodiscard]] attribute. */
4674 : :
4675 : : static tree
4676 : 34 : handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4677 : : int /*flags*/, bool *no_add_attrs)
4678 : : {
4679 : 34 : if (TREE_CODE (*node) == FUNCTION_DECL)
4680 : : {
4681 : 18 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4682 : 1 : warning_at (DECL_SOURCE_LOCATION (*node),
4683 : 1 : OPT_Wattributes, "%qE attribute applied to %qD with void "
4684 : : "return type", name, *node);
4685 : : }
4686 : 16 : else if (RECORD_OR_UNION_TYPE_P (*node)
4687 : 12 : || TREE_CODE (*node) == ENUMERAL_TYPE)
4688 : : /* OK */;
4689 : : else
4690 : : {
4691 : 10 : pedwarn (input_location,
4692 : 10 : OPT_Wattributes, "%qE attribute can only be applied to "
4693 : : "functions or to structure, union or enumeration types", name);
4694 : 10 : *no_add_attrs = true;
4695 : : }
4696 : 34 : return NULL_TREE;
4697 : : }
4698 : :
4699 : : /* Handle the standard [[noreturn]] attribute. */
4700 : :
4701 : : static tree
4702 : 44 : handle_std_noreturn_attribute (tree *node, tree name, tree args,
4703 : : int flags, bool *no_add_attrs)
4704 : : {
4705 : : /* Unlike GNU __attribute__ ((noreturn)), the standard [[noreturn]]
4706 : : only applies to functions, not function pointers. */
4707 : 44 : if (TREE_CODE (*node) == FUNCTION_DECL)
4708 : 22 : return handle_noreturn_attribute (node, name, args, flags, no_add_attrs);
4709 : : else
4710 : : {
4711 : 22 : pedwarn (input_location, OPT_Wattributes,
4712 : : "standard %qE attribute can only be applied to functions",
4713 : : name);
4714 : 22 : *no_add_attrs = true;
4715 : 22 : return NULL_TREE;
4716 : : }
4717 : : }
4718 : :
4719 : : /* Handle the standard [[unsequenced]] attribute. */
4720 : :
4721 : : static tree
4722 : 68 : handle_std_unsequenced_attribute (tree *node, tree name, tree args,
4723 : : int flags, bool *no_add_attrs)
4724 : : {
4725 : : /* Unlike GNU __attribute__ ((unsequenced)), the standard [[unsequenced]]
4726 : : should be only applied to function declarators or type specifiers which
4727 : : have function type. */
4728 : 68 : if (node[2])
4729 : : {
4730 : 8 : auto_diagnostic_group d;
4731 : 8 : if (pedwarn (input_location, OPT_Wattributes,
4732 : : "standard %qE attribute can only be applied to function "
4733 : : "declarators or type specifiers with function type", name))
4734 : 8 : inform (input_location, "did you mean to specify it after %<)%> "
4735 : : "following function parameters?");
4736 : 8 : *no_add_attrs = true;
4737 : 8 : return NULL_TREE;
4738 : 8 : }
4739 : 60 : return handle_unsequenced_attribute (node, name, args, flags, no_add_attrs);
4740 : : }
4741 : :
4742 : : /* Handle the standard [[reproducible]] attribute. */
4743 : :
4744 : : static tree
4745 : 34 : handle_std_reproducible_attribute (tree *node, tree name, tree args,
4746 : : int flags, bool *no_add_attrs)
4747 : : {
4748 : 34 : return handle_std_unsequenced_attribute (node, name, args, flags,
4749 : 34 : no_add_attrs);
4750 : : }
4751 : :
4752 : : /* Table of supported standard (C23) attributes. */
4753 : : static const attribute_spec std_attributes[] =
4754 : : {
4755 : : /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4756 : : affects_type_identity, handler, exclude } */
4757 : : { "_Noreturn", 0, 0, false, false, false, false,
4758 : : handle_std_noreturn_attribute, NULL },
4759 : : { "deprecated", 0, 1, false, false, false, false,
4760 : : handle_deprecated_attribute, NULL },
4761 : : { "fallthrough", 0, 0, false, false, false, false,
4762 : : handle_fallthrough_attribute, NULL },
4763 : : { "maybe_unused", 0, 0, false, false, false, false,
4764 : : handle_unused_attribute, NULL },
4765 : : { "nodiscard", 0, 1, false, false, false, false,
4766 : : handle_nodiscard_attribute, NULL },
4767 : : { "noreturn", 0, 0, false, false, false, false,
4768 : : handle_std_noreturn_attribute, NULL },
4769 : : { "reproducible", 0, 0, false, true, true, false,
4770 : : handle_std_reproducible_attribute, NULL },
4771 : : { "unsequenced", 0, 0, false, true, true, false,
4772 : : handle_std_unsequenced_attribute, NULL }
4773 : : };
4774 : :
4775 : : const scoped_attribute_specs std_attribute_table =
4776 : : {
4777 : : nullptr, { std_attributes }
4778 : : };
4779 : :
4780 : : /* Create the predefined scalar types of C,
4781 : : and some nodes representing standard constants (0, 1, (void *) 0).
4782 : : Initialize the global scope.
4783 : : Make definitions for built-in primitive functions. */
4784 : :
4785 : : void
4786 : 109855 : c_init_decl_processing (void)
4787 : : {
4788 : 109855 : location_t save_loc = input_location;
4789 : :
4790 : : /* Initialize reserved words for parser. */
4791 : 109855 : c_parse_init ();
4792 : :
4793 : 109855 : current_function_decl = NULL_TREE;
4794 : :
4795 : 109855 : gcc_obstack_init (&parser_obstack);
4796 : :
4797 : : /* Make the externals scope. */
4798 : 109855 : push_scope ();
4799 : 109855 : external_scope = current_scope;
4800 : :
4801 : : /* Declarations from c_common_nodes_and_builtins must not be associated
4802 : : with this input file, lest we get differences between using and not
4803 : : using preprocessed headers. */
4804 : 109855 : input_location = BUILTINS_LOCATION;
4805 : :
4806 : 109855 : c_common_nodes_and_builtins ();
4807 : :
4808 : : /* In C, comparisons and TRUTH_* expressions have type int. */
4809 : 109855 : truthvalue_type_node = integer_type_node;
4810 : 109855 : truthvalue_true_node = integer_one_node;
4811 : 109855 : truthvalue_false_node = integer_zero_node;
4812 : :
4813 : : /* Even in C99, which has a real boolean type. */
4814 : 109855 : pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4815 : : boolean_type_node));
4816 : :
4817 : : /* C-specific nullptr initialization. */
4818 : 109855 : record_builtin_type (RID_MAX, "typeof (nullptr)", nullptr_type_node);
4819 : : /* The size and alignment of nullptr_t is the same as for a pointer to
4820 : : character type. */
4821 : 109855 : SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4822 : :
4823 : 109855 : input_location = save_loc;
4824 : :
4825 : 109855 : make_fname_decl = c_make_fname_decl;
4826 : 109855 : start_fname_decls ();
4827 : :
4828 : 109855 : if (warn_keyword_macro)
4829 : : {
4830 : 1145 : for (unsigned int i = 0; i < num_c_common_reswords; ++i)
4831 : : /* For C register keywords which don't start with underscore
4832 : : or start with just single underscore. Don't complain about
4833 : : ObjC or Transactional Memory keywords. */
4834 : 1140 : if (c_common_reswords[i].word[0] == '_'
4835 : 515 : && c_common_reswords[i].word[1] == '_')
4836 : 385 : continue;
4837 : 980 : else if (c_common_reswords[i].disable
4838 : 755 : & (D_TRANSMEM | D_OBJC | D_CXX_OBJC))
4839 : 225 : continue;
4840 : : else
4841 : : {
4842 : 530 : tree id = get_identifier (c_common_reswords[i].word);
4843 : 530 : if (C_IS_RESERVED_WORD (id)
4844 : 530 : && C_RID_CODE (id) != RID_CXX_COMPAT_WARN)
4845 : 322 : cpp_warn (parse_in, IDENTIFIER_POINTER (id),
4846 : 322 : IDENTIFIER_LENGTH (id));
4847 : : }
4848 : : }
4849 : 109855 : }
4850 : :
4851 : : /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4852 : : give the decl, NAME is the initialization string and TYPE_DEP
4853 : : indicates whether NAME depended on the type of the function. As we
4854 : : don't yet implement delayed emission of static data, we mark the
4855 : : decl as emitted so it is not placed in the output. Anything using
4856 : : it must therefore pull out the STRING_CST initializer directly.
4857 : : FIXME. */
4858 : :
4859 : : static tree
4860 : 2861 : c_make_fname_decl (location_t loc, tree id, int type_dep)
4861 : : {
4862 : 2861 : const char *name = fname_as_string (type_dep);
4863 : 2861 : tree decl, type, init;
4864 : 2861 : size_t length = strlen (name);
4865 : :
4866 : 2861 : type = c_build_array_type (char_type_node,
4867 : 2861 : build_index_type (size_int (length)));
4868 : 2861 : type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4869 : :
4870 : 2861 : decl = build_decl (loc, VAR_DECL, id, type);
4871 : :
4872 : 2861 : TREE_STATIC (decl) = 1;
4873 : 2861 : TREE_READONLY (decl) = 1;
4874 : 2861 : DECL_ARTIFICIAL (decl) = 1;
4875 : :
4876 : 2861 : init = build_string (length + 1, name);
4877 : 2861 : free (CONST_CAST (char *, name));
4878 : 2861 : TREE_TYPE (init) = type;
4879 : 2861 : DECL_INITIAL (decl) = init;
4880 : :
4881 : 2861 : TREE_USED (decl) = 1;
4882 : :
4883 : 2861 : if (current_function_decl
4884 : : /* For invalid programs like this:
4885 : :
4886 : : void foo()
4887 : : const char* p = __FUNCTION__;
4888 : :
4889 : : the __FUNCTION__ is believed to appear in K&R style function
4890 : : parameter declarator. In that case we still don't have
4891 : : function_scope. */
4892 : 2854 : && current_function_scope)
4893 : : {
4894 : 2846 : DECL_CONTEXT (decl) = current_function_decl;
4895 : 2846 : bind (id, decl, current_function_scope,
4896 : : /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4897 : : }
4898 : :
4899 : 2861 : finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4900 : :
4901 : 2861 : return decl;
4902 : : }
4903 : :
4904 : : tree
4905 : 320436597 : c_builtin_function (tree decl)
4906 : : {
4907 : 320436597 : tree type = TREE_TYPE (decl);
4908 : 320436597 : tree id = DECL_NAME (decl);
4909 : :
4910 : 320436597 : const char *name = IDENTIFIER_POINTER (id);
4911 : 320436597 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4912 : :
4913 : : /* Should never be called on a symbol with a preexisting meaning. */
4914 : 320436597 : gcc_assert (!I_SYMBOL_BINDING (id));
4915 : :
4916 : 320436597 : bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4917 : : UNKNOWN_LOCATION);
4918 : :
4919 : : /* Builtins in the implementation namespace are made visible without
4920 : : needing to be explicitly declared. See push_file_scope. */
4921 : 320436597 : if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4922 : : {
4923 : 218515343 : DECL_CHAIN (decl) = visible_builtins;
4924 : 218515343 : visible_builtins = decl;
4925 : : }
4926 : :
4927 : 320436597 : return decl;
4928 : : }
4929 : :
4930 : : tree
4931 : 10390404 : c_builtin_function_ext_scope (tree decl)
4932 : : {
4933 : 10390404 : tree type = TREE_TYPE (decl);
4934 : 10390404 : tree id = DECL_NAME (decl);
4935 : :
4936 : 10390404 : const char *name = IDENTIFIER_POINTER (id);
4937 : 10390404 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4938 : :
4939 : 10390404 : if (external_scope)
4940 : 10205309 : bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4941 : : UNKNOWN_LOCATION);
4942 : :
4943 : : /* Builtins in the implementation namespace are made visible without
4944 : : needing to be explicitly declared. See push_file_scope. */
4945 : 10390404 : if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4946 : : {
4947 : 10390404 : DECL_CHAIN (decl) = visible_builtins;
4948 : 10390404 : visible_builtins = decl;
4949 : : }
4950 : :
4951 : 10390404 : return decl;
4952 : : }
4953 : :
4954 : : /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4955 : :
4956 : : tree
4957 : 0 : c_simulate_builtin_function_decl (tree decl)
4958 : : {
4959 : 0 : tree type = TREE_TYPE (decl);
4960 : 0 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4961 : 0 : return pushdecl (decl);
4962 : : }
4963 : :
4964 : : /* Warn about attributes in a context where they are unused
4965 : : (attribute-declarations, except for the "fallthrough" case, and
4966 : : attributes on statements). */
4967 : :
4968 : : void
4969 : 40568081 : c_warn_unused_attributes (tree attrs)
4970 : : {
4971 : 40568126 : for (tree t = attrs; t != NULL_TREE; t = TREE_CHAIN (t))
4972 : 45 : if (get_attribute_namespace (t) == NULL_TREE)
4973 : : /* The specifications of standard attributes mean this is a
4974 : : constraint violation. */
4975 : 29 : pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4976 : : get_attribute_name (t));
4977 : 16 : else if (!attribute_ignored_p (t))
4978 : 12 : warning (OPT_Wattributes, "%qE attribute ignored",
4979 : : get_attribute_name (t));
4980 : 40568081 : }
4981 : :
4982 : : /* Warn for standard attributes being applied to a type that is not
4983 : : being defined, where that is a constraint violation, and return a
4984 : : list of attributes with them removed. */
4985 : :
4986 : : tree
4987 : 424937367 : c_warn_type_attributes (tree type, tree attrs)
4988 : : {
4989 : 424937367 : tree *attr_ptr = &attrs;
4990 : 424959989 : while (*attr_ptr)
4991 : 22622 : if (get_attribute_namespace (*attr_ptr) == NULL_TREE)
4992 : : {
4993 : 89 : if (TREE_CODE (type) == FUNCTION_TYPE)
4994 : : {
4995 : 65 : tree name = get_attribute_name (*attr_ptr);
4996 : : /* [[unsequenced]] and [[reproducible]] is fine on function
4997 : : types that aren't being defined. */
4998 : 65 : if (is_attribute_p ("unsequenced", name)
4999 : 65 : || is_attribute_p ("reproducible", name))
5000 : : {
5001 : 60 : attr_ptr = &TREE_CHAIN (*attr_ptr);
5002 : 60 : continue;
5003 : : }
5004 : : }
5005 : 29 : pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
5006 : : get_attribute_name (*attr_ptr));
5007 : 29 : *attr_ptr = TREE_CHAIN (*attr_ptr);
5008 : : }
5009 : : else
5010 : 22533 : attr_ptr = &TREE_CHAIN (*attr_ptr);
5011 : 424937367 : return attrs;
5012 : : }
5013 : :
5014 : : /* Called when a declaration is seen that contains no names to declare.
5015 : : If its type is a reference to a structure, union or enum inherited
5016 : : from a containing scope, shadow that tag name for the current scope
5017 : : with a forward reference.
5018 : : If its type defines a new named structure or union
5019 : : or defines an enum, it is valid but we need not do anything here.
5020 : : Otherwise, it is an error. */
5021 : :
5022 : : void
5023 : 490900 : shadow_tag (const struct c_declspecs *declspecs)
5024 : : {
5025 : 490900 : shadow_tag_warned (declspecs, 0);
5026 : 490900 : }
5027 : :
5028 : : /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
5029 : : but no pedwarn. */
5030 : : void
5031 : 490970 : shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
5032 : : {
5033 : 490970 : bool found_tag = false;
5034 : :
5035 : 490970 : if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
5036 : : {
5037 : 490856 : tree value = declspecs->type;
5038 : 490856 : enum tree_code code = TREE_CODE (value);
5039 : :
5040 : 490856 : if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
5041 : : /* Used to test also that TYPE_SIZE (value) != 0.
5042 : : That caused warning for `struct foo;' at top level in the file. */
5043 : : {
5044 : 490805 : tree name = TYPE_NAME (value);
5045 : 490805 : tree t;
5046 : :
5047 : 490805 : found_tag = true;
5048 : :
5049 : 490805 : if (declspecs->restrict_p)
5050 : : {
5051 : 2 : error ("invalid use of %<restrict%>");
5052 : 2 : warned = 1;
5053 : : }
5054 : :
5055 : 490805 : if (in_underspecified_init)
5056 : : {
5057 : : /* This can only occur with extensions such as statement
5058 : : expressions, but is still appropriate as an error to
5059 : : avoid types declared in such a context escaping to
5060 : : the type of an auto variable. */
5061 : 1 : error ("%qT declared in underspecified object initializer",
5062 : : value);
5063 : 1 : warned = 1;
5064 : : }
5065 : :
5066 : 490805 : if (name == NULL_TREE)
5067 : : {
5068 : 57398 : if (warned != 1 && code != ENUMERAL_TYPE)
5069 : : /* Empty unnamed enum OK */
5070 : : {
5071 : 19 : pedwarn (input_location, 0,
5072 : : "unnamed struct/union that defines no instances");
5073 : 19 : warned = 1;
5074 : : }
5075 : : }
5076 : 433407 : else if (declspecs->typespec_kind != ctsk_tagdef
5077 : 86999 : && declspecs->typespec_kind != ctsk_tagfirstref
5078 : 25894 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5079 : 25886 : && declspecs->storage_class != csc_none)
5080 : : {
5081 : 2 : if (warned != 1)
5082 : 2 : pedwarn (input_location, 0,
5083 : : "empty declaration with storage class specifier "
5084 : : "does not redeclare tag");
5085 : 2 : warned = 1;
5086 : 2 : pending_xref_error ();
5087 : : }
5088 : 433405 : else if (declspecs->typespec_kind != ctsk_tagdef
5089 : 86997 : && declspecs->typespec_kind != ctsk_tagfirstref
5090 : 25892 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5091 : 25884 : && (declspecs->const_p
5092 : 25881 : || declspecs->volatile_p
5093 : 25881 : || declspecs->atomic_p
5094 : 25881 : || declspecs->restrict_p
5095 : 25881 : || declspecs->address_space))
5096 : : {
5097 : 3 : if (warned != 1)
5098 : 3 : pedwarn (input_location, 0,
5099 : : "empty declaration with type qualifier "
5100 : : "does not redeclare tag");
5101 : 3 : warned = 1;
5102 : 3 : pending_xref_error ();
5103 : : }
5104 : 433402 : else if (declspecs->typespec_kind != ctsk_tagdef
5105 : 86994 : && declspecs->typespec_kind != ctsk_tagfirstref
5106 : 25889 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5107 : 25881 : && declspecs->alignas_p)
5108 : : {
5109 : 1 : if (warned != 1)
5110 : 1 : pedwarn (input_location, 0,
5111 : : "empty declaration with %<_Alignas%> "
5112 : : "does not redeclare tag");
5113 : 1 : warned = 1;
5114 : 1 : pending_xref_error ();
5115 : : }
5116 : 433401 : else if (declspecs->typespec_kind != ctsk_tagdef
5117 : 86993 : && declspecs->typespec_kind != ctsk_tagfirstref
5118 : 25888 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5119 : 25880 : && code == ENUMERAL_TYPE
5120 : 15 : && !declspecs->enum_type_specifier_ref_p)
5121 : : {
5122 : 3 : bool warned_enum = false;
5123 : 3 : if (warned != 1)
5124 : 3 : warned_enum = pedwarn (input_location, OPT_Wpedantic,
5125 : : "empty declaration of %<enum%> type "
5126 : : "does not redeclare tag");
5127 : 3 : if (warned_enum)
5128 : 2 : warned = 1;
5129 : 3 : pending_xref_error ();
5130 : 3 : }
5131 : : else
5132 : : {
5133 : 433398 : pending_invalid_xref = NULL_TREE;
5134 : 433398 : t = lookup_tag (code, name, true, NULL);
5135 : :
5136 : 433398 : if (t == NULL_TREE)
5137 : : {
5138 : 2 : t = make_node (code);
5139 : 2 : if (flag_isoc23 || code == ENUMERAL_TYPE)
5140 : 1 : SET_TYPE_STRUCTURAL_EQUALITY (t);
5141 : 2 : pushtag (input_location, name, t);
5142 : : }
5143 : : }
5144 : : }
5145 : : else
5146 : : {
5147 : 51 : if (warned != 1 && !in_system_header_at (input_location))
5148 : : {
5149 : 44 : pedwarn (input_location, 0,
5150 : : "useless type name in empty declaration");
5151 : 44 : warned = 1;
5152 : : }
5153 : : }
5154 : : }
5155 : 71 : else if (warned != 1 && !in_system_header_at (input_location)
5156 : 185 : && declspecs->typedef_p)
5157 : : {
5158 : 11 : pedwarn (input_location, 0, "useless type name in empty declaration");
5159 : 11 : warned = 1;
5160 : : }
5161 : :
5162 : 490970 : pending_invalid_xref = NULL_TREE;
5163 : :
5164 : 490970 : if (declspecs->inline_p)
5165 : : {
5166 : 5 : error ("%<inline%> in empty declaration");
5167 : 5 : warned = 1;
5168 : : }
5169 : :
5170 : 490970 : if (declspecs->noreturn_p)
5171 : : {
5172 : 2 : error ("%<_Noreturn%> in empty declaration");
5173 : 2 : warned = 1;
5174 : : }
5175 : :
5176 : 490970 : if (declspecs->constexpr_p)
5177 : : {
5178 : 7 : error ("%<constexpr%> in empty declaration");
5179 : 7 : warned = 1;
5180 : : }
5181 : :
5182 : 490970 : if (current_scope == file_scope && declspecs->storage_class == csc_auto)
5183 : : {
5184 : 2 : error ("%<auto%> in file-scope empty declaration");
5185 : 2 : warned = 1;
5186 : : }
5187 : :
5188 : 490970 : if (current_scope == file_scope && declspecs->storage_class == csc_register)
5189 : : {
5190 : 2 : error ("%<register%> in file-scope empty declaration");
5191 : 2 : warned = 1;
5192 : : }
5193 : :
5194 : 490970 : if (declspecs->enum_type_specifier_ref_p && !warned)
5195 : : {
5196 : 39 : if (declspecs->storage_class != csc_none)
5197 : : {
5198 : 1 : error ("storage class specifier in empty declaration with %<enum%> "
5199 : : "underlying type");
5200 : 1 : warned = 1;
5201 : : }
5202 : 38 : else if (declspecs->thread_p)
5203 : : {
5204 : 2 : error ("%qs in empty declaration with %<enum%> underlying type",
5205 : 1 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5206 : 1 : warned = 1;
5207 : : }
5208 : 37 : else if (declspecs->const_p
5209 : 36 : || declspecs->volatile_p
5210 : 36 : || declspecs->atomic_p
5211 : 36 : || declspecs->restrict_p
5212 : 36 : || declspecs->address_space)
5213 : : {
5214 : 1 : error ("type qualifier in empty declaration with %<enum%> "
5215 : : "underlying type");
5216 : 1 : warned = 1;
5217 : : }
5218 : 36 : else if (declspecs->alignas_p)
5219 : : {
5220 : 1 : error ("%<alignas%> in empty declaration with %<enum%> "
5221 : : "underlying type");
5222 : 1 : warned = 1;
5223 : : }
5224 : : }
5225 : :
5226 : 490802 : if (!warned && !in_system_header_at (input_location)
5227 : 608174 : && declspecs->storage_class != csc_none)
5228 : : {
5229 : 12 : warning (0, "useless storage class specifier in empty declaration");
5230 : 12 : warned = 2;
5231 : : }
5232 : :
5233 : 490970 : if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
5234 : : {
5235 : 3 : warning (0, "useless %qs in empty declaration",
5236 : 3 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5237 : 3 : warned = 2;
5238 : : }
5239 : :
5240 : 3 : if (!warned
5241 : 490783 : && !in_system_header_at (input_location)
5242 : 608164 : && (declspecs->const_p
5243 : 117189 : || declspecs->volatile_p
5244 : 117189 : || declspecs->atomic_p
5245 : 117189 : || declspecs->restrict_p
5246 : 117189 : || declspecs->address_space))
5247 : : {
5248 : 8 : warning (0, "useless type qualifier in empty declaration");
5249 : 8 : warned = 2;
5250 : : }
5251 : :
5252 : 490783 : if (!warned && !in_system_header_at (input_location)
5253 : 608151 : && declspecs->alignas_p)
5254 : : {
5255 : 4 : warning (0, "useless %<_Alignas%> in empty declaration");
5256 : 4 : warned = 2;
5257 : : }
5258 : :
5259 : 490970 : if (found_tag
5260 : 490970 : && warned == 2
5261 : 23 : && (declspecs->typespec_kind == ctsk_tagref_attrs
5262 : 23 : || declspecs->typespec_kind == ctsk_tagfirstref_attrs))
5263 : : {
5264 : : /* Standard attributes after the "struct" or "union" keyword are
5265 : : only permitted when the contents of the type are defined, or
5266 : : in the form "struct-or-union attribute-specifier-sequence
5267 : : identifier;". If the ';' was not present, attributes were
5268 : : diagnosed in the parser. Here, ensure that any other useless
5269 : : elements of the declaration result in a pedwarn, not just a
5270 : : warning. Forward declarations of enum types are not part of
5271 : : standard C, but handle them the same. */
5272 : 2 : pedwarn (input_location, 0,
5273 : : "invalid use of attributes in empty declaration");
5274 : 2 : warned = 1;
5275 : : }
5276 : :
5277 : 490970 : if (warned != 1)
5278 : : {
5279 : 490796 : if (declspecs->declspecs_seen_p
5280 : 490796 : && !declspecs->non_std_attrs_seen_p)
5281 : : /* An attribute declaration (but not a fallthrough attribute
5282 : : declaration, which was handled separately); warn if there
5283 : : are any attributes being ignored (but not if the attributes
5284 : : were empty). */
5285 : 48 : c_warn_unused_attributes (declspecs->attrs);
5286 : 490748 : else if (!found_tag)
5287 : 10 : pedwarn (input_location, 0, "empty declaration");
5288 : : }
5289 : 490970 : }
5290 : :
5291 : :
5292 : : /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
5293 : : bits. SPECS represents declaration specifiers that the grammar
5294 : : only permits to contain type qualifiers and attributes. */
5295 : :
5296 : : int
5297 : 17892273 : quals_from_declspecs (const struct c_declspecs *specs)
5298 : : {
5299 : 17892273 : int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
5300 : : | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
5301 : : | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
5302 : 17892273 : | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
5303 : 17892273 : | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
5304 : 17892273 : gcc_assert (!specs->type
5305 : : && !specs->decl_attr
5306 : : && specs->typespec_word == cts_none
5307 : : && specs->storage_class == csc_none
5308 : : && !specs->typedef_p
5309 : : && !specs->explicit_signed_p
5310 : : && !specs->deprecated_p
5311 : : && !specs->unavailable_p
5312 : : && !specs->long_p
5313 : : && !specs->long_long_p
5314 : : && !specs->short_p
5315 : : && !specs->signed_p
5316 : : && !specs->unsigned_p
5317 : : && !specs->complex_p
5318 : : && !specs->inline_p
5319 : : && !specs->noreturn_p
5320 : : && !specs->thread_p);
5321 : 17892273 : return quals;
5322 : : }
5323 : :
5324 : : /* Construct an array declarator. LOC is the location of the
5325 : : beginning of the array (usually the opening brace). EXPR is the
5326 : : expression inside [], or NULL_TREE. QUALS are the type qualifiers
5327 : : inside the [] (to be applied to the pointer to which a parameter
5328 : : array is converted). STATIC_P is true if "static" is inside the
5329 : : [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
5330 : : VLA of unspecified length which is nevertheless a complete type,
5331 : : false otherwise. The field for the contained declarator is left to
5332 : : be filled in by set_array_declarator_inner. */
5333 : :
5334 : : struct c_declarator *
5335 : 1128748 : build_array_declarator (location_t loc,
5336 : : tree expr, struct c_declspecs *quals, bool static_p,
5337 : : bool vla_unspec_p)
5338 : : {
5339 : 1128748 : struct c_declarator *declarator = XOBNEW (&parser_obstack,
5340 : : struct c_declarator);
5341 : 1128748 : declarator->id_loc = loc;
5342 : 1128748 : declarator->kind = cdk_array;
5343 : 1128748 : declarator->declarator = 0;
5344 : 1128748 : declarator->u.array.dimen = expr;
5345 : 1128748 : if (quals)
5346 : : {
5347 : 933 : declarator->u.array.attrs = quals->attrs;
5348 : 933 : declarator->u.array.quals = quals_from_declspecs (quals);
5349 : : }
5350 : : else
5351 : : {
5352 : 1127815 : declarator->u.array.attrs = NULL_TREE;
5353 : 1127815 : declarator->u.array.quals = 0;
5354 : : }
5355 : 1128748 : declarator->u.array.static_p = static_p;
5356 : 1128748 : declarator->u.array.vla_unspec_p = vla_unspec_p;
5357 : 1128748 : if (static_p || quals != NULL)
5358 : 1036 : pedwarn_c90 (loc, OPT_Wpedantic,
5359 : : "ISO C90 does not support %<static%> or type "
5360 : : "qualifiers in parameter array declarators");
5361 : 1128748 : if (vla_unspec_p)
5362 : 147 : pedwarn_c90 (loc, OPT_Wpedantic,
5363 : : "ISO C90 does not support %<[*]%> array declarators");
5364 : 147 : if (vla_unspec_p)
5365 : : {
5366 : 147 : if (!current_scope->parm_flag)
5367 : : {
5368 : : /* C99 6.7.5.2p4 */
5369 : 4 : error_at (loc, "%<[*]%> not allowed in other than "
5370 : : "function prototype scope");
5371 : 4 : declarator->u.array.vla_unspec_p = false;
5372 : 4 : return NULL;
5373 : : }
5374 : 143 : current_scope->had_vla_unspec = true;
5375 : : }
5376 : : return declarator;
5377 : : }
5378 : :
5379 : : /* Set the contained declarator of an array declarator. DECL is the
5380 : : declarator, as constructed by build_array_declarator; INNER is what
5381 : : appears on the left of the []. */
5382 : :
5383 : : struct c_declarator *
5384 : 1128744 : set_array_declarator_inner (struct c_declarator *decl,
5385 : : struct c_declarator *inner)
5386 : : {
5387 : 1128744 : decl->declarator = inner;
5388 : 1128744 : return decl;
5389 : : }
5390 : :
5391 : : /* Determine whether TYPE is a one-element array type "[1]". */
5392 : : static bool
5393 : 520300 : one_element_array_type_p (const_tree type)
5394 : : {
5395 : 520300 : if (TREE_CODE (type) != ARRAY_TYPE)
5396 : : return false;
5397 : 520300 : return integer_zerop (array_type_nelts_minus_one (type));
5398 : : }
5399 : :
5400 : : /* Determine whether TYPE is a zero-length array type "[0]". */
5401 : : static bool
5402 : 520300 : zero_length_array_type_p (const_tree type)
5403 : : {
5404 : 520300 : if (TREE_CODE (type) == ARRAY_TYPE)
5405 : 520300 : if (tree type_size = TYPE_SIZE_UNIT (type))
5406 : 435721 : if ((integer_zerop (type_size))
5407 : 1816 : && TYPE_DOMAIN (type) != NULL_TREE
5408 : 437537 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5409 : : return true;
5410 : : return false;
5411 : : }
5412 : :
5413 : : /* INIT is a constructor that forms DECL's initializer. If the final
5414 : : element initializes a flexible array field, adjust the size of the
5415 : : DECL with the initializer based on whether the DECL is a union or
5416 : : a structure. */
5417 : :
5418 : : static void
5419 : 103591 : add_flexible_array_elts_to_size (tree decl, tree init)
5420 : : {
5421 : 103591 : tree elt, type;
5422 : :
5423 : 103591 : if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
5424 : 1412 : return;
5425 : :
5426 : 102179 : elt = CONSTRUCTOR_ELTS (init)->last ().value;
5427 : 102179 : type = TREE_TYPE (elt);
5428 : 102179 : if (c_flexible_array_member_type_p (type))
5429 : : {
5430 : 220 : complete_array_type (&type, elt, false);
5431 : : /* For a structure, add the size of the initializer to the DECL's
5432 : : size. */
5433 : 220 : if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
5434 : : {
5435 : 210 : DECL_SIZE (decl)
5436 : 210 : = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5437 : 210 : DECL_SIZE_UNIT (decl)
5438 : 420 : = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
5439 : : TYPE_SIZE_UNIT (type));
5440 : : }
5441 : : /* For a union, the DECL's size is the maximum of the current size
5442 : : and the size of the initializer. */
5443 : : else
5444 : : {
5445 : 10 : DECL_SIZE (decl)
5446 : 10 : = size_binop (MAX_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5447 : 10 : DECL_SIZE_UNIT (decl)
5448 : 20 : = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
5449 : : TYPE_SIZE_UNIT (type));
5450 : : }
5451 : : }
5452 : : }
5453 : :
5454 : : /* Decode a "typename", such as "int **", returning a ..._TYPE node.
5455 : : Set *EXPR, if EXPR not NULL, to any expression to be evaluated
5456 : : before the type name, and set *EXPR_CONST_OPERANDS, if
5457 : : EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
5458 : : appear in a constant expression. */
5459 : :
5460 : : tree
5461 : 118374987 : groktypename (struct c_type_name *type_name, tree *expr,
5462 : : bool *expr_const_operands)
5463 : : {
5464 : 118374987 : tree type;
5465 : 118374987 : tree attrs = type_name->specs->attrs;
5466 : :
5467 : 118374987 : type_name->specs->attrs = NULL_TREE;
5468 : :
5469 : 118374987 : type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
5470 : : false, NULL, &attrs, expr, expr_const_operands,
5471 : : DEPRECATED_NORMAL);
5472 : :
5473 : : /* Apply attributes. */
5474 : 118374987 : attrs = c_warn_type_attributes (type, attrs);
5475 : 118374987 : decl_attributes (&type, attrs, 0);
5476 : :
5477 : 118374987 : return type;
5478 : : }
5479 : :
5480 : : /* Looks up the most recent pushed declaration corresponding to DECL. */
5481 : :
5482 : : static tree
5483 : 90266018 : lookup_last_decl (tree decl)
5484 : : {
5485 : 90266018 : tree last_decl = lookup_name (DECL_NAME (decl));
5486 : 90266018 : if (!last_decl)
5487 : 87749063 : last_decl = lookup_name_in_scope (DECL_NAME (decl), external_scope);
5488 : 90266018 : return last_decl;
5489 : : }
5490 : :
5491 : : /* Wrapper for decl_attributes that adds some implicit attributes
5492 : : to VAR_DECLs or FUNCTION_DECLs. */
5493 : :
5494 : : static tree
5495 : 62823090 : c_decl_attributes (tree *node, tree attributes, int flags)
5496 : : {
5497 : : /* Add implicit "omp declare target" attribute if requested. */
5498 : 62823090 : if (vec_safe_length (current_omp_declare_target_attribute)
5499 : 9158 : && ((VAR_P (*node) && is_global_var (*node))
5500 : 2710 : || TREE_CODE (*node) == FUNCTION_DECL))
5501 : : {
5502 : 1112 : if (VAR_P (*node) && !omp_mappable_type (TREE_TYPE (*node)))
5503 : 13 : attributes = tree_cons (get_identifier ("omp declare target implicit"),
5504 : : NULL_TREE, attributes);
5505 : : else
5506 : 1099 : attributes = tree_cons (get_identifier ("omp declare target"),
5507 : : NULL_TREE, attributes);
5508 : 1112 : if (TREE_CODE (*node) == FUNCTION_DECL)
5509 : : {
5510 : 982 : int device_type
5511 : 982 : = current_omp_declare_target_attribute->last ().device_type;
5512 : 982 : device_type = MAX (device_type, 0);
5513 : 982 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
5514 : 982 : && !lookup_attribute ("omp declare target host", attributes))
5515 : 2 : attributes
5516 : 2 : = tree_cons (get_identifier ("omp declare target host"),
5517 : : NULL_TREE, attributes);
5518 : 982 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
5519 : 982 : && !lookup_attribute ("omp declare target nohost", attributes))
5520 : 2 : attributes
5521 : 2 : = tree_cons (get_identifier ("omp declare target nohost"),
5522 : : NULL_TREE, attributes);
5523 : :
5524 : 982 : int indirect
5525 : 982 : = current_omp_declare_target_attribute->last ().indirect;
5526 : 982 : if (indirect && !lookup_attribute ("omp declare target indirect",
5527 : : attributes))
5528 : 10 : attributes
5529 : 10 : = tree_cons (get_identifier ("omp declare target indirect"),
5530 : : NULL_TREE, attributes);
5531 : : }
5532 : : }
5533 : :
5534 : 62823090 : if (flag_openmp || flag_openmp_simd)
5535 : : {
5536 : : bool diagnosed = false;
5537 : 1340786 : for (tree *pa = &attributes; *pa; )
5538 : : {
5539 : 867795 : if (is_attribute_namespace_p ("omp", *pa))
5540 : : {
5541 : 60 : tree name = get_attribute_name (*pa);
5542 : 60 : if (is_attribute_p ("directive", name)
5543 : 0 : || is_attribute_p ("sequence", name)
5544 : 60 : || is_attribute_p ("decl", name))
5545 : : {
5546 : 60 : const char *p = NULL;
5547 : 60 : if (TREE_VALUE (*pa) == NULL_TREE)
5548 : 12 : p = IDENTIFIER_POINTER (name);
5549 : 108 : for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
5550 : : {
5551 : 48 : tree d = TREE_VALUE (a);
5552 : 48 : gcc_assert (TREE_CODE (d) == C_TOKEN_VEC);
5553 : 86 : if (TREE_PUBLIC (d)
5554 : 38 : && (VAR_P (*node)
5555 : 2 : || TREE_CODE (*node) == FUNCTION_DECL)
5556 : 86 : && c_maybe_parse_omp_decl (*node, d))
5557 : 38 : continue;
5558 : 10 : p = TREE_PUBLIC (d) ? "decl" : "directive";
5559 : : }
5560 : 60 : if (p && !diagnosed)
5561 : : {
5562 : 22 : error ("%<omp::%s%> not allowed to be specified in "
5563 : : "this context", p);
5564 : 22 : diagnosed = true;
5565 : : }
5566 : 60 : if (p)
5567 : : {
5568 : 22 : *pa = TREE_CHAIN (*pa);
5569 : 22 : continue;
5570 : : }
5571 : : }
5572 : : }
5573 : 867773 : pa = &TREE_CHAIN (*pa);
5574 : : }
5575 : : }
5576 : :
5577 : : /* Look up the current declaration with all the attributes merged
5578 : : so far so that attributes on the current declaration that's
5579 : : about to be pushed that conflict with the former can be detected,
5580 : : diagnosed, and rejected as appropriate. To match the C++ FE, do
5581 : : not pass an error_mark_node when we found an undeclared variable. */
5582 : 62823090 : tree last_decl = lookup_last_decl (*node);
5583 : 62823090 : if (last_decl == error_mark_node)
5584 : 20 : last_decl = NULL_TREE;
5585 : 62823090 : tree attr = decl_attributes (node, attributes, flags, last_decl);
5586 : 62823089 : if (VAR_P (*node) && DECL_THREAD_LOCAL_P (*node))
5587 : : {
5588 : : // tls_model attribute can set a stronger TLS access model.
5589 : 2827 : tls_model model = DECL_TLS_MODEL (*node);
5590 : 2827 : tls_model default_model = decl_default_tls_model (*node);
5591 : 2827 : if (default_model > model)
5592 : 1573 : set_decl_tls_model (*node, default_model);
5593 : : }
5594 : 62823089 : return attr;
5595 : : }
5596 : :
5597 : :
5598 : : /* Decode a declarator in an ordinary declaration or data definition.
5599 : : This is called as soon as the type information and variable name
5600 : : have been parsed, before parsing the initializer if any.
5601 : : Here we create the ..._DECL node, fill in its type,
5602 : : and (if DO_PUSH) put it on the list of decls for the current context.
5603 : : When nonnull, set *LASTLOC to the location of the prior declaration
5604 : : of the same entity if one exists.
5605 : : The ..._DECL node is returned as the value.
5606 : :
5607 : : Exception: for arrays where the length is not specified,
5608 : : the type is left null, to be filled in by `finish_decl'.
5609 : :
5610 : : Function definitions do not come here; they go to start_function
5611 : : instead. However, external and forward declarations of functions
5612 : : do go through here. Structure field declarations are done by
5613 : : grokfield and not through here. */
5614 : :
5615 : : tree
5616 : 27442952 : start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5617 : : bool initialized, tree attributes, bool do_push /* = true */,
5618 : : location_t *lastloc /* = NULL */)
5619 : : {
5620 : 27442952 : tree decl;
5621 : 27442952 : tree old_decl;
5622 : 27442952 : tree tem;
5623 : 27442952 : tree expr = NULL_TREE;
5624 : 27442952 : enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5625 : :
5626 : : /* An object declared as __attribute__((unavailable)) suppresses
5627 : : warnings and errors from __attribute__((deprecated/unavailable))
5628 : : components.
5629 : : An object declared as __attribute__((deprecated)) suppresses
5630 : : warnings of uses of other deprecated items. */
5631 : 27442952 : if (lookup_attribute ("unavailable", attributes))
5632 : : deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
5633 : 27442928 : else if (lookup_attribute ("deprecated", attributes))
5634 : 29596 : deprecated_state = DEPRECATED_SUPPRESS;
5635 : :
5636 : 27442952 : decl = grokdeclarator (declarator, declspecs,
5637 : : NORMAL, initialized, NULL, &attributes, &expr, NULL,
5638 : : deprecated_state);
5639 : 27442952 : if (!decl || decl == error_mark_node)
5640 : : return NULL_TREE;
5641 : :
5642 : 27442928 : old_decl = lookup_last_decl (decl);
5643 : :
5644 : 27442928 : if (tree lastdecl = lastloc ? old_decl : NULL_TREE)
5645 : 4681373 : if (lastdecl != error_mark_node)
5646 : 4681364 : *lastloc = DECL_SOURCE_LOCATION (lastdecl);
5647 : :
5648 : : /* Make sure the size expression is evaluated at this point. */
5649 : 27442928 : if (expr && !current_scope->parm_flag)
5650 : 12922 : add_stmt (fold_convert (void_type_node, expr));
5651 : :
5652 : 12986565 : if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5653 : 27442930 : && TREE_PUBLIC (decl))
5654 : 1 : warning (OPT_Wmain, "%q+D is usually a function", decl);
5655 : :
5656 : 14 : if (warn_missing_variable_declarations && VAR_P (decl)
5657 : 27442941 : && !DECL_EXTERNAL (decl) && TREE_PUBLIC (decl) && old_decl == NULL_TREE)
5658 : 5 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_variable_declarations,
5659 : : "no previous declaration for %qD", decl);
5660 : :
5661 : 27442928 : if (initialized)
5662 : : /* Is it valid for this decl to have an initializer at all?
5663 : : If not, set INITIALIZED to zero, which will indirectly
5664 : : tell 'finish_decl' to ignore the initializer once it is parsed. */
5665 : 6196768 : switch (TREE_CODE (decl))
5666 : : {
5667 : 6 : case TYPE_DECL:
5668 : 6 : error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5669 : 6 : initialized = false;
5670 : 6 : break;
5671 : :
5672 : 9 : case FUNCTION_DECL:
5673 : 9 : error ("function %qD is initialized like a variable", decl);
5674 : 9 : initialized = false;
5675 : 9 : break;
5676 : :
5677 : 49 : case PARM_DECL:
5678 : : /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5679 : 49 : error ("parameter %qD is initialized", decl);
5680 : 49 : initialized = false;
5681 : 49 : break;
5682 : :
5683 : 6196704 : default:
5684 : : /* Don't allow initializations for incomplete types except for
5685 : : arrays which might be completed by the initialization. */
5686 : :
5687 : : /* This can happen if the array size is an undefined macro.
5688 : : We already gave a warning, so we don't need another one. */
5689 : 6196704 : if (TREE_TYPE (decl) == error_mark_node)
5690 : : initialized = false;
5691 : 6196682 : else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5692 : : {
5693 : : /* A complete type is ok if size is fixed. If the size is
5694 : : variable, an empty initializer is OK and nonempty
5695 : : initializers will be diagnosed in the parser. */
5696 : : }
5697 : 12093 : else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5698 : : {
5699 : 3 : error ("variable %qD has initializer but incomplete type", decl);
5700 : 3 : initialized = false;
5701 : : }
5702 : : }
5703 : :
5704 : 67 : if (initialized)
5705 : : {
5706 : 6196679 : if (current_scope == file_scope)
5707 : 155526 : TREE_STATIC (decl) = 1;
5708 : :
5709 : : /* Tell 'pushdecl' this is an initialized decl
5710 : : even though we don't yet have the initializer expression.
5711 : : Also tell 'finish_decl' it may store the real initializer. */
5712 : 6196679 : DECL_INITIAL (decl) = error_mark_node;
5713 : : }
5714 : :
5715 : : /* If this is a function declaration, write a record describing it to the
5716 : : prototypes file (if requested). */
5717 : :
5718 : 27442928 : if (TREE_CODE (decl) == FUNCTION_DECL)
5719 : 14456363 : gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5720 : :
5721 : : /* ANSI specifies that a tentative definition which is not merged with
5722 : : a non-tentative definition behaves exactly like a definition with an
5723 : : initializer equal to zero. (Section 3.7.2)
5724 : :
5725 : : -fno-common gives strict ANSI behavior, though this tends to break
5726 : : a large body of code that grew up without this rule.
5727 : :
5728 : : Thread-local variables are never common, since there's no entrenched
5729 : : body of code to break, and it allows more efficient variable references
5730 : : in the presence of dynamic linking. */
5731 : :
5732 : 27442928 : if (VAR_P (decl)
5733 : 8745717 : && !initialized
5734 : 2549038 : && TREE_PUBLIC (decl)
5735 : 987236 : && !DECL_THREAD_LOCAL_P (decl)
5736 : 28427817 : && !flag_no_common)
5737 : 128 : DECL_COMMON (decl) = 1;
5738 : :
5739 : : /* Set attributes here so if duplicate decl, will have proper attributes. */
5740 : 27442928 : c_decl_attributes (&decl, attributes, 0);
5741 : :
5742 : : /* Handle gnu_inline attribute. */
5743 : 27442927 : if (declspecs->inline_p
5744 : 1017 : && !flag_gnu89_inline
5745 : 985 : && TREE_CODE (decl) == FUNCTION_DECL
5746 : 27443904 : && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
5747 : 954 : || current_function_decl))
5748 : : {
5749 : 46 : if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5750 : : ;
5751 : 37 : else if (declspecs->storage_class != csc_static)
5752 : 37 : DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5753 : : }
5754 : :
5755 : 27442927 : if (TREE_CODE (decl) == FUNCTION_DECL
5756 : 14456362 : && DECL_DECLARED_INLINE_P (decl)
5757 : 1007 : && DECL_UNINLINABLE (decl)
5758 : 27442929 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5759 : : {
5760 : 2 : auto_urlify_attributes sentinel;
5761 : 2 : warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5762 : : decl, "noinline");
5763 : 2 : }
5764 : :
5765 : : /* C99 6.7.4p3: An inline definition of a function with external
5766 : : linkage shall not contain a definition of a modifiable object
5767 : : with static storage duration... */
5768 : 27442927 : if (VAR_P (decl)
5769 : 8745717 : && current_scope != file_scope
5770 : 7596063 : && TREE_STATIC (decl)
5771 : 78738 : && !TREE_READONLY (decl)
5772 : 76173 : && DECL_DECLARED_INLINE_P (current_function_decl)
5773 : 27442997 : && DECL_EXTERNAL (current_function_decl))
5774 : 7 : record_inline_static (input_location, current_function_decl,
5775 : : decl, csi_modifiable);
5776 : :
5777 : 27442927 : if (c_dialect_objc ()
5778 : 0 : && VAR_OR_FUNCTION_DECL_P (decl))
5779 : 0 : objc_check_global_decl (decl);
5780 : :
5781 : : /* Add this decl to the current scope.
5782 : : TEM may equal DECL or it may be a previous decl of the same name. */
5783 : 27442927 : if (do_push)
5784 : : {
5785 : 27442583 : tem = pushdecl (decl);
5786 : :
5787 : 27442583 : if (initialized && DECL_EXTERNAL (tem))
5788 : : {
5789 : 27 : DECL_EXTERNAL (tem) = 0;
5790 : 27 : TREE_STATIC (tem) = 1;
5791 : : }
5792 : :
5793 : 27442583 : return tem;
5794 : : }
5795 : : else
5796 : 344 : return decl;
5797 : : }
5798 : :
5799 : : /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5800 : : DECL or the non-array element type if DECL is an uninitialized array.
5801 : : If that type has a const member, diagnose this. */
5802 : :
5803 : : static void
5804 : 7 : diagnose_uninitialized_cst_member (tree decl, tree type)
5805 : : {
5806 : 7 : tree field;
5807 : 17 : for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5808 : : {
5809 : 10 : tree field_type;
5810 : 10 : if (TREE_CODE (field) != FIELD_DECL)
5811 : 0 : continue;
5812 : 10 : field_type = strip_array_types (TREE_TYPE (field));
5813 : :
5814 : 10 : if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5815 : : {
5816 : 5 : auto_diagnostic_group d;
5817 : 5 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5818 : : "uninitialized const member in %qT is invalid in C++",
5819 : 5 : strip_array_types (TREE_TYPE (decl))))
5820 : 5 : inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5821 : 5 : }
5822 : :
5823 : 10 : if (RECORD_OR_UNION_TYPE_P (field_type))
5824 : 2 : diagnose_uninitialized_cst_member (decl, field_type);
5825 : : }
5826 : 7 : }
5827 : :
5828 : : /* Finish processing of a declaration;
5829 : : install its initial value.
5830 : : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5831 : : If the length of an array type is not known before,
5832 : : it must be determined now, from the initial value, or it is an error.
5833 : :
5834 : : INIT_LOC is the location of the initial value. */
5835 : :
5836 : : void
5837 : 152788904 : finish_decl (tree decl, location_t init_loc, tree init,
5838 : : tree origtype, tree asmspec_tree)
5839 : : {
5840 : 152788904 : tree type;
5841 : 152788904 : bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5842 : 152788904 : const char *asmspec = 0;
5843 : :
5844 : : /* If a name was specified, get the string. */
5845 : 144040273 : if (VAR_OR_FUNCTION_DECL_P (decl)
5846 : 167245266 : && DECL_FILE_SCOPE_P (decl))
5847 : 15607531 : asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5848 : 152788904 : if (asmspec_tree)
5849 : 855720 : asmspec = TREE_STRING_POINTER (asmspec_tree);
5850 : :
5851 : 152788904 : if (VAR_P (decl)
5852 : 8748631 : && TREE_STATIC (decl)
5853 : 153848653 : && global_bindings_p ())
5854 : : /* So decl is a global variable. Record the types it uses
5855 : : so that we can decide later to emit debug info for them. */
5856 : 977880 : record_types_used_by_current_var_decl (decl);
5857 : :
5858 : : /* If `start_decl' didn't like having an initialization, ignore it now. */
5859 : 152788904 : if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5860 : : init = NULL_TREE;
5861 : :
5862 : : /* Don't crash if parm is initialized. */
5863 : 152788904 : if (TREE_CODE (decl) == PARM_DECL)
5864 : : init = NULL_TREE;
5865 : :
5866 : 31654927 : if (init)
5867 : 6199594 : store_init_value (init_loc, decl, init, origtype);
5868 : :
5869 : 152788904 : if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
5870 : 0 : || TREE_CODE (decl) == FIELD_DECL))
5871 : 0 : objc_check_decl (decl);
5872 : :
5873 : 152788904 : type = TREE_TYPE (decl);
5874 : :
5875 : : /* Deduce size of array from initialization, if not already known.
5876 : : This is only needed for an initialization in the current scope;
5877 : : it must not be done for a file-scope initialization of a
5878 : : declaration with external linkage, redeclared in an inner scope
5879 : : with the outer declaration shadowed in an intermediate scope. */
5880 : 152788904 : if (TREE_CODE (type) == ARRAY_TYPE
5881 : 848776 : && TYPE_DOMAIN (type) == NULL_TREE
5882 : 19680 : && TREE_CODE (decl) != TYPE_DECL
5883 : 152808488 : && !(TREE_PUBLIC (decl) && current_scope != file_scope))
5884 : : {
5885 : 19398 : bool do_default
5886 : 19398 : = (TREE_STATIC (decl)
5887 : : /* Even if pedantic, an external linkage array
5888 : : may have incomplete type at first. */
5889 : 19398 : ? pedantic && !TREE_PUBLIC (decl)
5890 : 12304 : : !DECL_EXTERNAL (decl));
5891 : 19398 : int failure
5892 : 19398 : = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5893 : : do_default);
5894 : :
5895 : : /* Get the completed type made by complete_array_type. */
5896 : 19398 : type = TREE_TYPE (decl);
5897 : :
5898 : 19398 : switch (failure)
5899 : : {
5900 : 0 : case 1:
5901 : 0 : error ("initializer fails to determine size of %q+D", decl);
5902 : 0 : break;
5903 : :
5904 : 7333 : case 2:
5905 : 7333 : if (do_default)
5906 : 3 : error ("array size missing in %q+D", decl);
5907 : : break;
5908 : :
5909 : 3 : case 3:
5910 : 3 : error ("zero or negative size array %q+D", decl);
5911 : 3 : break;
5912 : :
5913 : 12062 : case 0:
5914 : : /* For global variables, update the copy of the type that
5915 : : exists in the binding. */
5916 : 12062 : if (TREE_PUBLIC (decl))
5917 : : {
5918 : 3755 : struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5919 : 7510 : while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5920 : 3755 : b_ext = b_ext->shadowed;
5921 : 3755 : if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5922 : : {
5923 : 3755 : if (b_ext->u.type && comptypes (b_ext->u.type, type))
5924 : 176 : b_ext->u.type = composite_type (b_ext->u.type, type);
5925 : : else
5926 : 3579 : b_ext->u.type = type;
5927 : : }
5928 : : }
5929 : : break;
5930 : :
5931 : 0 : default:
5932 : 0 : gcc_unreachable ();
5933 : : }
5934 : :
5935 : 19398 : if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
5936 : 11225 : TREE_TYPE (DECL_INITIAL (decl)) = type;
5937 : :
5938 : 19398 : relayout_decl (decl);
5939 : : }
5940 : :
5941 : : /* Look for braced array initializers for character arrays and
5942 : : recursively convert them into STRING_CSTs. */
5943 : 152788904 : if (tree init = DECL_INITIAL (decl))
5944 : 127335962 : DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
5945 : :
5946 : 152788904 : if (VAR_P (decl))
5947 : : {
5948 : 8748631 : if (init && TREE_CODE (init) == CONSTRUCTOR)
5949 : 103591 : add_flexible_array_elts_to_size (decl, init);
5950 : :
5951 : 8748631 : complete_flexible_array_elts (DECL_INITIAL (decl));
5952 : :
5953 : 8748631 : if (is_global_var (decl))
5954 : : {
5955 : 1232829 : type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5956 : 1232829 : ? TCTX_THREAD_STORAGE
5957 : : : TCTX_STATIC_STORAGE);
5958 : 1232829 : if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
5959 : 0 : TREE_TYPE (decl) = error_mark_node;
5960 : : }
5961 : :
5962 : 8756649 : if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5963 : 8756550 : && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5964 : 97 : layout_decl (decl, 0);
5965 : :
5966 : 8748631 : if (DECL_SIZE (decl) == NULL_TREE
5967 : : /* Don't give an error if we already gave one earlier. */
5968 : 7921 : && TREE_TYPE (decl) != error_mark_node
5969 : 8756453 : && (TREE_STATIC (decl)
5970 : : /* A static variable with an incomplete type
5971 : : is an error if it is initialized.
5972 : : Also if it is not file scope.
5973 : : Also if it is thread-local (in C23).
5974 : : Otherwise, let it through, but if it is not `extern'
5975 : : then it may cause an error message later. */
5976 : 246 : ? (DECL_INITIAL (decl) != NULL_TREE
5977 : 246 : || !DECL_FILE_SCOPE_P (decl)
5978 : 244 : || (flag_isoc23 && DECL_THREAD_LOCAL_P (decl)))
5979 : : /* An automatic variable with an incomplete type
5980 : : is an error. */
5981 : 7576 : : !DECL_EXTERNAL (decl)))
5982 : : {
5983 : 17 : error ("storage size of %q+D isn%'t known", decl);
5984 : 17 : TREE_TYPE (decl) = error_mark_node;
5985 : : }
5986 : :
5987 : 17308298 : if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5988 : 8479272 : || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5989 : 282645 : && DECL_SIZE (decl) == NULL_TREE
5990 : 8748879 : && TREE_STATIC (decl))
5991 : 95 : incomplete_record_decls.safe_push (decl);
5992 : :
5993 : 8748631 : if (is_global_var (decl)
5994 : 1232829 : && DECL_SIZE (decl) != NULL_TREE
5995 : 9973580 : && TREE_TYPE (decl) != error_mark_node)
5996 : : {
5997 : 1224931 : if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5998 : 1224928 : constant_expression_warning (DECL_SIZE (decl));
5999 : : else
6000 : : {
6001 : 3 : error ("storage size of %q+D isn%'t constant", decl);
6002 : 3 : TREE_TYPE (decl) = error_mark_node;
6003 : : }
6004 : : }
6005 : :
6006 : 8748631 : if (TREE_USED (type))
6007 : : {
6008 : 4 : TREE_USED (decl) = 1;
6009 : 4 : DECL_READ_P (decl) = 1;
6010 : : }
6011 : : }
6012 : :
6013 : : /* If this is a function and an assembler name is specified, reset DECL_RTL
6014 : : so we can give it its new name. Also, update builtin_decl if it
6015 : : was a normal built-in. */
6016 : 152788904 : if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
6017 : : {
6018 : 842710 : if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6019 : 71038 : set_builtin_user_assembler_name (decl, asmspec);
6020 : 842710 : set_user_assembler_name (decl, asmspec);
6021 : : }
6022 : :
6023 : : /* If #pragma weak was used, mark the decl weak now. */
6024 : 152788904 : maybe_apply_pragma_weak (decl);
6025 : :
6026 : : /* Output the assembler code and/or RTL code for variables and functions,
6027 : : unless the type is an undefined structure or union.
6028 : : If not, it will get done when the type is completed. */
6029 : :
6030 : 152788904 : if (VAR_OR_FUNCTION_DECL_P (decl))
6031 : : {
6032 : : /* Determine the ELF visibility. */
6033 : 23204993 : if (TREE_PUBLIC (decl))
6034 : 15582987 : c_determine_visibility (decl);
6035 : :
6036 : : /* This is a no-op in c-lang.cc or something real in objc-act.cc. */
6037 : 23204993 : if (c_dialect_objc ())
6038 : 0 : objc_check_decl (decl);
6039 : :
6040 : 23204993 : if (asmspec)
6041 : : {
6042 : : /* If this is not a static variable, issue a warning.
6043 : : It doesn't make any sense to give an ASMSPEC for an
6044 : : ordinary, non-register local variable. Historically,
6045 : : GCC has accepted -- but ignored -- the ASMSPEC in
6046 : : this case. */
6047 : 856757 : if (!DECL_FILE_SCOPE_P (decl)
6048 : 1037 : && VAR_P (decl)
6049 : 1037 : && !C_DECL_REGISTER (decl)
6050 : 855728 : && !TREE_STATIC (decl))
6051 : 1 : warning (0, "ignoring %<asm%> specifier for non-static local "
6052 : : "variable %q+D", decl);
6053 : : else
6054 : 855719 : set_user_assembler_name (decl, asmspec);
6055 : : }
6056 : :
6057 : 23204993 : if (DECL_FILE_SCOPE_P (decl))
6058 : : {
6059 : 15607531 : if (DECL_INITIAL (decl) == NULL_TREE
6060 : 15607531 : || DECL_INITIAL (decl) == error_mark_node)
6061 : : /* Don't output anything
6062 : : when a tentative file-scope definition is seen.
6063 : : But at end of compilation, do output code for them. */
6064 : 15451838 : DECL_DEFER_OUTPUT (decl) = 1;
6065 : 15607531 : if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
6066 : 68 : DECL_HARD_REGISTER (decl) = 1;
6067 : 15607531 : rest_of_decl_compilation (decl, true, 0);
6068 : :
6069 : 15607531 : if (TREE_CODE (decl) == FUNCTION_DECL)
6070 : : {
6071 : 14456295 : tree parms = DECL_ARGUMENTS (decl);
6072 : 14456295 : const bool builtin = fndecl_built_in_p (decl);
6073 : 14456295 : if (tree access = build_attr_access_from_parms (parms, !builtin))
6074 : 339419 : decl_attributes (&decl, access, 0);
6075 : : }
6076 : : }
6077 : : else
6078 : : {
6079 : : /* In conjunction with an ASMSPEC, the `register'
6080 : : keyword indicates that we should place the variable
6081 : : in a particular register. */
6082 : 7597462 : if (asmspec && C_DECL_REGISTER (decl))
6083 : : {
6084 : 1029 : DECL_HARD_REGISTER (decl) = 1;
6085 : : /* This cannot be done for a structure with volatile
6086 : : fields, on which DECL_REGISTER will have been
6087 : : reset. */
6088 : 1029 : if (!DECL_REGISTER (decl))
6089 : 1 : error ("cannot put object with volatile field into register");
6090 : : }
6091 : :
6092 : 7597462 : if (TREE_CODE (decl) != FUNCTION_DECL)
6093 : : {
6094 : : /* If we're building a variable sized type, and we might be
6095 : : reachable other than via the top of the current binding
6096 : : level, then create a new BIND_EXPR so that we deallocate
6097 : : the object at the right time. */
6098 : : /* Note that DECL_SIZE can be null due to errors. */
6099 : 7597395 : if (DECL_SIZE (decl)
6100 : 7597352 : && !TREE_CONSTANT (DECL_SIZE (decl))
6101 : 7604746 : && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
6102 : : {
6103 : 1336 : tree bind;
6104 : 1336 : bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6105 : 1336 : TREE_SIDE_EFFECTS (bind) = 1;
6106 : 1336 : add_stmt (bind);
6107 : 1336 : BIND_EXPR_BODY (bind) = push_stmt_list ();
6108 : : }
6109 : 7597395 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
6110 : : DECL_EXPR, decl));
6111 : : }
6112 : : }
6113 : :
6114 : :
6115 : 23204993 : if (!DECL_FILE_SCOPE_P (decl))
6116 : : {
6117 : : /* Recompute the RTL of a local array now
6118 : : if it used to be an incomplete type. */
6119 : 7597462 : if (was_incomplete && !is_global_var (decl))
6120 : : {
6121 : : /* If we used it already as memory, it must stay in memory. */
6122 : 5204 : TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6123 : : /* If it's still incomplete now, no init will save it. */
6124 : 5204 : if (DECL_SIZE (decl) == NULL_TREE)
6125 : 107 : DECL_INITIAL (decl) = NULL_TREE;
6126 : : }
6127 : : }
6128 : : }
6129 : :
6130 : 152788904 : if (TREE_CODE (decl) == TYPE_DECL)
6131 : : {
6132 : 4346146 : if (!DECL_FILE_SCOPE_P (decl)
6133 : 4346146 : && c_type_variably_modified_p (TREE_TYPE (decl)))
6134 : 5321 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6135 : :
6136 : 8308430 : rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
6137 : : }
6138 : :
6139 : : /* Install a cleanup (aka destructor) if one was given. */
6140 : 152788904 : if (VAR_P (decl) && !TREE_STATIC (decl))
6141 : : {
6142 : 7688879 : tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
6143 : 7688879 : if (attr)
6144 : : {
6145 : 95 : tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
6146 : 95 : tree cleanup_decl = lookup_name (cleanup_id);
6147 : 95 : tree cleanup;
6148 : 95 : vec<tree, va_gc> *v;
6149 : :
6150 : : /* Build "cleanup(&decl)" for the destructor. */
6151 : 95 : cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
6152 : 95 : vec_alloc (v, 1);
6153 : 95 : v->quick_push (cleanup);
6154 : 95 : cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
6155 : 95 : vNULL, cleanup_decl, v, NULL);
6156 : 95 : vec_free (v);
6157 : :
6158 : : /* Don't warn about decl unused; the cleanup uses it. */
6159 : 95 : TREE_USED (decl) = 1;
6160 : 95 : TREE_USED (cleanup_decl) = 1;
6161 : 95 : DECL_READ_P (decl) = 1;
6162 : :
6163 : 95 : push_cleanup (decl, cleanup, false);
6164 : : }
6165 : : }
6166 : :
6167 : 152788904 : if (warn_cxx_compat
6168 : 22378 : && VAR_P (decl)
6169 : 7542 : && !DECL_EXTERNAL (decl)
6170 : 152796005 : && DECL_INITIAL (decl) == NULL_TREE)
6171 : : {
6172 : 2328 : type = strip_array_types (type);
6173 : 2328 : if (TREE_READONLY (decl))
6174 : 6 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6175 : : "uninitialized %<const %D%> is invalid in C++", decl);
6176 : 2322 : else if (RECORD_OR_UNION_TYPE_P (type)
6177 : 2322 : && C_TYPE_FIELDS_READONLY (type))
6178 : 5 : diagnose_uninitialized_cst_member (decl, type);
6179 : : }
6180 : :
6181 : 152788904 : if (flag_openmp
6182 : 430660 : && VAR_P (decl)
6183 : 152812609 : && lookup_attribute ("omp declare target implicit",
6184 : 23705 : DECL_ATTRIBUTES (decl)))
6185 : : {
6186 : 13 : DECL_ATTRIBUTES (decl)
6187 : 13 : = remove_attribute ("omp declare target implicit",
6188 : 13 : DECL_ATTRIBUTES (decl));
6189 : 13 : if (!omp_mappable_type (TREE_TYPE (decl)))
6190 : 9 : error ("%q+D in declare target directive does not have mappable type",
6191 : : decl);
6192 : 4 : else if (!lookup_attribute ("omp declare target",
6193 : 4 : DECL_ATTRIBUTES (decl))
6194 : 8 : && !lookup_attribute ("omp declare target link",
6195 : 4 : DECL_ATTRIBUTES (decl)))
6196 : : {
6197 : 4 : DECL_ATTRIBUTES (decl)
6198 : 4 : = tree_cons (get_identifier ("omp declare target"),
6199 : 4 : NULL_TREE, DECL_ATTRIBUTES (decl));
6200 : 4 : symtab_node *node = symtab_node::get (decl);
6201 : 4 : if (node != NULL)
6202 : : {
6203 : 4 : node->offloadable = 1;
6204 : 4 : if (ENABLE_OFFLOADING)
6205 : : {
6206 : : g->have_offload = true;
6207 : : if (is_a <varpool_node *> (node))
6208 : : vec_safe_push (offload_vars, decl);
6209 : : }
6210 : : }
6211 : : }
6212 : : }
6213 : :
6214 : : /* This is the last point we can lower alignment so give the target the
6215 : : chance to do so. */
6216 : 152788904 : if (VAR_P (decl)
6217 : 8748631 : && !is_global_var (decl)
6218 : 160304706 : && !DECL_HARD_REGISTER (decl))
6219 : 7514773 : targetm.lower_local_decl_alignment (decl);
6220 : :
6221 : 152788904 : invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
6222 : 152788904 : }
6223 : :
6224 : : /* Given a parsed parameter declaration, decode it into a PARM_DECL.
6225 : : EXPR is NULL or a pointer to an expression that needs to be
6226 : : evaluated for the side effects of array size expressions in the
6227 : : parameters. */
6228 : :
6229 : : tree
6230 : 0 : grokparm (const struct c_parm *parm, tree *expr)
6231 : : {
6232 : 0 : tree attrs = parm->attrs;
6233 : 0 : tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
6234 : 0 : NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
6235 : :
6236 : 0 : decl_attributes (&decl, attrs, 0);
6237 : :
6238 : 0 : return decl;
6239 : : }
6240 : :
6241 : :
6242 : :
6243 : : /* Given a parsed parameter declaration, decode it into a PARM_DECL
6244 : : and push that on the current scope. EXPR is a pointer to an
6245 : : expression that needs to be evaluated for the side effects of array
6246 : : size expressions in the parameters. */
6247 : :
6248 : : void
6249 : 121111321 : push_parm_decl (const struct c_parm *parm, tree *expr)
6250 : : {
6251 : 121111321 : tree attrs = parm->attrs;
6252 : 121111321 : tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
6253 : 121111321 : &attrs, expr, NULL, DEPRECATED_NORMAL);
6254 : 121111321 : if (decl && DECL_P (decl))
6255 : 121111321 : DECL_SOURCE_LOCATION (decl) = parm->loc;
6256 : :
6257 : 121111321 : decl_attributes (&decl, attrs, 0);
6258 : :
6259 : 121111321 : decl = pushdecl (decl);
6260 : :
6261 : 121111321 : finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
6262 : 121111321 : }
6263 : :
6264 : : /* Mark all the parameter declarations to date as forward decls.
6265 : : Also diagnose use of this extension. */
6266 : :
6267 : : void
6268 : 37 : mark_forward_parm_decls (void)
6269 : : {
6270 : 37 : struct c_binding *b;
6271 : :
6272 : 37 : if (pedantic && !current_scope->warned_forward_parm_decls)
6273 : : {
6274 : 4 : pedwarn (input_location, OPT_Wpedantic,
6275 : : "ISO C forbids forward parameter declarations");
6276 : 4 : current_scope->warned_forward_parm_decls = true;
6277 : : }
6278 : :
6279 : 83 : for (b = current_scope->bindings; b; b = b->prev)
6280 : 46 : if (TREE_CODE (b->decl) == PARM_DECL)
6281 : 46 : TREE_ASM_WRITTEN (b->decl) = 1;
6282 : 37 : }
6283 : :
6284 : : /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
6285 : : literal, which may be an incomplete array type completed by the
6286 : : initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
6287 : : literal. NON_CONST is true if the initializers contain something
6288 : : that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
6289 : : it is the (valid) alignment for this compound literal, as specified
6290 : : with _Alignas. SCSPECS are the storage class specifiers (C23) from the
6291 : : compound literal. */
6292 : :
6293 : : tree
6294 : 898098 : build_compound_literal (location_t loc, tree type, tree init, bool non_const,
6295 : : unsigned int alignas_align,
6296 : : struct c_declspecs *scspecs)
6297 : : {
6298 : : /* We do not use start_decl here because we have a type, not a declarator;
6299 : : and do not use finish_decl because the decl should be stored inside
6300 : : the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
6301 : 898098 : tree decl;
6302 : 898098 : tree complit;
6303 : 898098 : tree stmt;
6304 : 898098 : bool threadp = scspecs ? scspecs->thread_p : false;
6305 : 298 : enum c_storage_class storage_class = (scspecs
6306 : : ? scspecs->storage_class
6307 : : : csc_none);
6308 : :
6309 : 898098 : if (type == error_mark_node
6310 : 898082 : || init == error_mark_node)
6311 : : return error_mark_node;
6312 : :
6313 : 898029 : if (current_scope == file_scope && storage_class == csc_register)
6314 : : {
6315 : 1 : error_at (loc, "file-scope compound literal specifies %<register%>");
6316 : 1 : storage_class = csc_none;
6317 : : }
6318 : :
6319 : 898029 : if (current_scope != file_scope && threadp && storage_class == csc_none)
6320 : : {
6321 : 2 : error_at (loc, "compound literal implicitly auto and declared %qs",
6322 : 1 : scspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6323 : 1 : threadp = false;
6324 : : }
6325 : :
6326 : 898029 : decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
6327 : 898029 : DECL_EXTERNAL (decl) = 0;
6328 : 898029 : TREE_PUBLIC (decl) = 0;
6329 : 1796058 : TREE_STATIC (decl) = (current_scope == file_scope
6330 : 898029 : || storage_class == csc_static);
6331 : 898029 : DECL_CONTEXT (decl) = current_function_decl;
6332 : 898029 : TREE_USED (decl) = 1;
6333 : 898029 : DECL_READ_P (decl) = 1;
6334 : 898029 : DECL_ARTIFICIAL (decl) = 1;
6335 : 898029 : DECL_IGNORED_P (decl) = 1;
6336 : 898029 : C_DECL_COMPOUND_LITERAL_P (decl) = 1;
6337 : 1795829 : C_DECL_DECLARED_CONSTEXPR (decl) = scspecs && scspecs->constexpr_p;
6338 : 898029 : TREE_TYPE (decl) = type;
6339 : 898029 : if (threadp)
6340 : 16 : set_decl_tls_model (decl, decl_default_tls_model (decl));
6341 : 898029 : if (storage_class == csc_register)
6342 : : {
6343 : 22 : C_DECL_REGISTER (decl) = 1;
6344 : 22 : DECL_REGISTER (decl) = 1;
6345 : : }
6346 : 898029 : c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
6347 : 898029 : if (alignas_align)
6348 : : {
6349 : 3 : SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6350 : 3 : DECL_USER_ALIGN (decl) = 1;
6351 : : }
6352 : 898029 : store_init_value (loc, decl, init, NULL_TREE);
6353 : 898029 : if (current_scope != file_scope
6354 : 897782 : && TREE_STATIC (decl)
6355 : 26 : && !TREE_READONLY (decl)
6356 : 15 : && DECL_DECLARED_INLINE_P (current_function_decl)
6357 : 898035 : && DECL_EXTERNAL (current_function_decl))
6358 : 4 : record_inline_static (input_location, current_function_decl,
6359 : : decl, csi_modifiable);
6360 : :
6361 : 898029 : if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
6362 : : {
6363 : 251 : int failure = complete_array_type (&TREE_TYPE (decl),
6364 : 251 : DECL_INITIAL (decl), true);
6365 : : /* If complete_array_type returns 3, it means that the initial value of
6366 : : the compound literal is empty. Allow it with a pedwarn; in pre-C23
6367 : : modes, the empty initializer itself has been diagnosed if pedantic so
6368 : : does not need to be diagnosed again here. */
6369 : 251 : gcc_assert (failure == 0 || failure == 3);
6370 : 251 : if (failure == 3 && flag_isoc23)
6371 : 1 : pedwarn (loc, OPT_Wpedantic,
6372 : : "array of unknown size with empty initializer");
6373 : :
6374 : 251 : type = TREE_TYPE (decl);
6375 : 251 : TREE_TYPE (DECL_INITIAL (decl)) = type;
6376 : : }
6377 : :
6378 : 898029 : if (type == error_mark_node || !COMPLETE_TYPE_P (type))
6379 : : {
6380 : 19 : c_incomplete_type_error (loc, NULL_TREE, type);
6381 : 19 : return error_mark_node;
6382 : : }
6383 : :
6384 : 898010 : if (TREE_STATIC (decl)
6385 : 898010 : && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
6386 : 0 : return error_mark_node;
6387 : :
6388 : 898010 : stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
6389 : 898010 : complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
6390 : 898010 : TREE_SIDE_EFFECTS (complit) = 1;
6391 : :
6392 : 898010 : layout_decl (decl, 0);
6393 : :
6394 : 898010 : if (TREE_STATIC (decl))
6395 : : {
6396 : : /* This decl needs a name for the assembler output. */
6397 : 273 : set_compound_literal_name (decl);
6398 : 273 : DECL_DEFER_OUTPUT (decl) = 1;
6399 : 273 : DECL_COMDAT (decl) = 1;
6400 : 273 : pushdecl (decl);
6401 : 273 : rest_of_decl_compilation (decl, 1, 0);
6402 : : }
6403 : 897737 : else if (current_function_decl && !current_scope->parm_flag)
6404 : 897728 : pushdecl (decl);
6405 : :
6406 : 898010 : if (non_const)
6407 : : {
6408 : 193 : complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
6409 : 193 : C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
6410 : : }
6411 : :
6412 : : return complit;
6413 : : }
6414 : :
6415 : : /* Check the type of a compound literal. Here we just check that it
6416 : : is valid for C++. */
6417 : :
6418 : : void
6419 : 898098 : check_compound_literal_type (location_t loc, struct c_type_name *type_name)
6420 : : {
6421 : 898098 : if (warn_cxx_compat
6422 : 165 : && (type_name->specs->typespec_kind == ctsk_tagdef
6423 : 164 : || type_name->specs->typespec_kind == ctsk_tagfirstref
6424 : 163 : || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
6425 : 2 : warning_at (loc, OPT_Wc___compat,
6426 : : "defining a type in a compound literal is invalid in C++");
6427 : 898098 : }
6428 : :
6429 : : /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6430 : : replacing with appropriate values if they are invalid. */
6431 : :
6432 : : static void
6433 : 51871 : check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
6434 : : tree orig_name)
6435 : : {
6436 : 51871 : tree type_mv;
6437 : 51871 : unsigned int max_width;
6438 : 51871 : unsigned HOST_WIDE_INT w;
6439 : 51871 : const char *name = (orig_name
6440 : 94554 : ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
6441 : 9188 : : _("<anonymous>"));
6442 : :
6443 : : /* Detect and ignore out of range field width and process valid
6444 : : field widths. */
6445 : 51871 : if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
6446 : : {
6447 : 8 : error_at (loc, "bit-field %qs width not an integer constant", name);
6448 : 8 : *width = integer_one_node;
6449 : : }
6450 : : else
6451 : : {
6452 : 51863 : if (TREE_CODE (*width) != INTEGER_CST)
6453 : : {
6454 : 12 : *width = c_fully_fold (*width, false, NULL);
6455 : 12 : if (TREE_CODE (*width) == INTEGER_CST)
6456 : 6 : pedwarn (loc, OPT_Wpedantic,
6457 : : "bit-field %qs width not an integer constant expression",
6458 : : name);
6459 : : }
6460 : 51863 : if (TREE_CODE (*width) != INTEGER_CST)
6461 : : {
6462 : 6 : error_at (loc, "bit-field %qs width not an integer constant", name);
6463 : 6 : *width = integer_one_node;
6464 : : }
6465 : 51863 : constant_expression_warning (*width);
6466 : 51863 : if (tree_int_cst_sgn (*width) < 0)
6467 : : {
6468 : 2 : error_at (loc, "negative width in bit-field %qs", name);
6469 : 2 : *width = integer_one_node;
6470 : : }
6471 : 51861 : else if (integer_zerop (*width) && orig_name)
6472 : : {
6473 : 4 : error_at (loc, "zero width for bit-field %qs", name);
6474 : 4 : *width = integer_one_node;
6475 : : }
6476 : : }
6477 : :
6478 : : /* Detect invalid bit-field type. */
6479 : 51871 : if (TREE_CODE (*type) != INTEGER_TYPE
6480 : : && TREE_CODE (*type) != BOOLEAN_TYPE
6481 : : && TREE_CODE (*type) != ENUMERAL_TYPE
6482 : 51871 : && TREE_CODE (*type) != BITINT_TYPE)
6483 : : {
6484 : 6 : error_at (loc, "bit-field %qs has invalid type", name);
6485 : 6 : *type = unsigned_type_node;
6486 : : }
6487 : :
6488 : 51871 : if (TYPE_WARN_IF_NOT_ALIGN (*type))
6489 : : {
6490 : 1 : error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6491 : : name);
6492 : 1 : *type = unsigned_type_node;
6493 : : }
6494 : :
6495 : 51871 : type_mv = TYPE_MAIN_VARIANT (*type);
6496 : 51871 : if (!in_system_header_at (input_location)
6497 : 29369 : && type_mv != integer_type_node
6498 : 26633 : && type_mv != unsigned_type_node
6499 : 64476 : && type_mv != boolean_type_node)
6500 : 12023 : pedwarn_c90 (loc, OPT_Wpedantic,
6501 : : "type of bit-field %qs is a GCC extension", name);
6502 : :
6503 : 51871 : max_width = TYPE_PRECISION (*type);
6504 : :
6505 : 51871 : if (compare_tree_int (*width, max_width) > 0)
6506 : : {
6507 : 4 : error_at (loc, "width of %qs exceeds its type", name);
6508 : 4 : w = max_width;
6509 : 4 : *width = build_int_cst (integer_type_node, w);
6510 : : }
6511 : : else
6512 : 51867 : w = tree_to_uhwi (*width);
6513 : :
6514 : : /* Truncation of hardbool false and true representation values is always safe:
6515 : : either the values remain different, or we'll report a problem when creating
6516 : : the narrower type. */
6517 : 51871 : if (c_hardbool_type_attr (*type))
6518 : 51871 : return;
6519 : :
6520 : 51590 : if (TREE_CODE (*type) == ENUMERAL_TYPE)
6521 : : {
6522 : 4130 : struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
6523 : 4130 : if (!lt
6524 : 4120 : || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
6525 : 8246 : || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
6526 : 15 : warning_at (loc, 0, "%qs is narrower than values of its type", name);
6527 : : }
6528 : : }
6529 : :
6530 : :
6531 : :
6532 : : /* Print warning about variable length array if necessary. */
6533 : :
6534 : : static void
6535 : 22427 : warn_variable_length_array (tree name, tree size)
6536 : : {
6537 : 22427 : if (TREE_CONSTANT (size))
6538 : : {
6539 : 180 : if (name)
6540 : 168 : pedwarn_c90 (input_location, OPT_Wvla,
6541 : : "ISO C90 forbids array %qE whose size "
6542 : : "cannot be evaluated", name);
6543 : : else
6544 : 12 : pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
6545 : : "whose size cannot be evaluated");
6546 : : }
6547 : : else
6548 : : {
6549 : 22247 : if (name)
6550 : 13780 : pedwarn_c90 (input_location, OPT_Wvla,
6551 : : "ISO C90 forbids variable length array %qE", name);
6552 : : else
6553 : 8467 : pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
6554 : : "length array");
6555 : : }
6556 : 22427 : }
6557 : :
6558 : : /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6559 : : considering only those c_declspec_words found in LIST, which
6560 : : must be terminated by cdw_number_of_elements. */
6561 : :
6562 : : static location_t
6563 : 235 : smallest_type_quals_location (const location_t *locations,
6564 : : const c_declspec_word *list)
6565 : : {
6566 : 235 : location_t loc = UNKNOWN_LOCATION;
6567 : 1410 : while (*list != cdw_number_of_elements)
6568 : : {
6569 : 1175 : location_t newloc = locations[*list];
6570 : 1175 : if (loc == UNKNOWN_LOCATION
6571 : 288 : || (newloc != UNKNOWN_LOCATION && newloc < loc))
6572 : 887 : loc = newloc;
6573 : 1175 : list++;
6574 : : }
6575 : :
6576 : 235 : return loc;
6577 : : }
6578 : :
6579 : :
6580 : : /* We attach an artificial TYPE_DECL to pointed-to type
6581 : : and arrange for it to be included in a DECL_EXPR. This
6582 : : forces the sizes evaluation at a safe point and ensures it
6583 : : is not deferred until e.g. within a deeper conditional context.
6584 : :
6585 : : PARM contexts have no enclosing statement list that
6586 : : can hold the DECL_EXPR, so we need to use a BIND_EXPR
6587 : : instead, and add it to the list of expressions that
6588 : : need to be evaluated.
6589 : :
6590 : : TYPENAME contexts do have an enclosing statement list,
6591 : : but it would be incorrect to use it, as the size should
6592 : : only be evaluated if the containing expression is
6593 : : evaluated. We might also be in the middle of an
6594 : : expression with side effects on the pointed-to type size
6595 : : "arguments" prior to the pointer declaration point and
6596 : : the fake TYPE_DECL in the enclosing context would force
6597 : : the size evaluation prior to the side effects. We therefore
6598 : : use BIND_EXPRs in TYPENAME contexts too. */
6599 : : static void
6600 : 6976 : add_decl_expr (location_t loc, tree type, tree *expr, bool set_name_p)
6601 : : {
6602 : 6976 : tree bind = NULL_TREE;
6603 : 6976 : if (expr)
6604 : : {
6605 : 1543 : bind = build3 (BIND_EXPR, void_type_node, NULL_TREE, NULL_TREE,
6606 : : NULL_TREE);
6607 : 1543 : TREE_SIDE_EFFECTS (bind) = 1;
6608 : 1543 : BIND_EXPR_BODY (bind) = push_stmt_list ();
6609 : 1543 : push_scope ();
6610 : : }
6611 : :
6612 : 6976 : tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6613 : 6976 : pushdecl (decl);
6614 : 6976 : DECL_ARTIFICIAL (decl) = 1;
6615 : 6976 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6616 : 6976 : if (set_name_p)
6617 : 6311 : TYPE_NAME (type) = decl;
6618 : :
6619 : 6976 : if (bind)
6620 : : {
6621 : 1543 : pop_scope ();
6622 : 1543 : BIND_EXPR_BODY (bind) = pop_stmt_list (BIND_EXPR_BODY (bind));
6623 : 1543 : if (*expr)
6624 : 1449 : *expr = build2 (COMPOUND_EXPR, void_type_node, *expr, bind);
6625 : : else
6626 : 94 : *expr = bind;
6627 : : }
6628 : 6976 : }
6629 : :
6630 : :
6631 : : /* Add attribute "arg spec" to ATTRS corresponding to an array/VLA parameter
6632 : : declared with type TYPE. The attribute has two arguments. The first is
6633 : : a string that encodes the presence of the static keyword. The second is
6634 : : the declared type of the array before adjustment, i.e. as an array type
6635 : : including the outermost bound. */
6636 : :
6637 : : static tree
6638 : 429405 : build_arg_spec_attribute (tree type, bool static_p, tree attrs)
6639 : : {
6640 : 429405 : tree vbchain = tree_cons (NULL_TREE, type, NULL_TREE);
6641 : 429405 : tree acsstr = static_p ? build_string (7, "static") :
6642 : 429283 : build_string (1, "");
6643 : 429405 : tree args = tree_cons (NULL_TREE, acsstr, vbchain);
6644 : 429405 : tree name = get_identifier ("arg spec");
6645 : 429405 : return tree_cons (name, args, attrs);
6646 : : }
6647 : :
6648 : :
6649 : : /* Given declspecs and a declarator,
6650 : : determine the name and type of the object declared
6651 : : and construct a ..._DECL node for it.
6652 : : (In one case we can return a ..._TYPE node instead.
6653 : : For invalid input we sometimes return NULL_TREE.)
6654 : :
6655 : : DECLSPECS is a c_declspecs structure for the declaration specifiers.
6656 : :
6657 : : DECL_CONTEXT says which syntactic context this declaration is in:
6658 : : NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6659 : : FUNCDEF for a function definition. Like NORMAL but a few different
6660 : : error messages in each case. Return value may be zero meaning
6661 : : this definition is too screwy to try to parse.
6662 : : PARM for a parameter declaration (either within a function prototype
6663 : : or before a function body). Make a PARM_DECL, or return void_type_node.
6664 : : TYPENAME if for a typename (in a cast or sizeof).
6665 : : Don't make a DECL node; just return the ..._TYPE node.
6666 : : FIELD for a struct or union field; make a FIELD_DECL.
6667 : : INITIALIZED is true if the decl has an initializer.
6668 : : WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6669 : : representing the width of the bit-field.
6670 : : DECL_ATTRS points to the list of attributes that should be added to this
6671 : : decl. Any nested attributes that belong on the decl itself will be
6672 : : added to this list.
6673 : : If EXPR is not NULL, any expressions that need to be evaluated as
6674 : : part of evaluating variably modified types will be stored in *EXPR.
6675 : : If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6676 : : set to indicate whether operands in *EXPR can be used in constant
6677 : : expressions.
6678 : : DEPRECATED_STATE is a deprecated_states value indicating whether
6679 : : deprecation/unavailability warnings should be suppressed.
6680 : :
6681 : : In the TYPENAME case, DECLARATOR is really an absolute declarator.
6682 : : It may also be so in the PARM case, for a prototype where the
6683 : : argument type is specified but not the name.
6684 : :
6685 : : This function is where the complicated C meanings of `static'
6686 : : and `extern' are interpreted. */
6687 : :
6688 : : static tree
6689 : 306541082 : grokdeclarator (const struct c_declarator *declarator,
6690 : : struct c_declspecs *declspecs,
6691 : : enum decl_context decl_context, bool initialized, tree *width,
6692 : : tree *decl_attrs, tree *expr, bool *expr_const_operands,
6693 : : enum deprecated_states deprecated_state)
6694 : : {
6695 : 306541082 : tree type = declspecs->type;
6696 : 306541082 : bool threadp = declspecs->thread_p;
6697 : 306541082 : bool constexprp = declspecs->constexpr_p;
6698 : 306541082 : enum c_storage_class storage_class = declspecs->storage_class;
6699 : 306541082 : int constp;
6700 : 306541082 : int restrictp;
6701 : 306541082 : int volatilep;
6702 : 306541082 : int atomicp;
6703 : 306541082 : int type_quals = TYPE_UNQUALIFIED;
6704 : 306541082 : tree name = NULL_TREE;
6705 : 306541082 : bool funcdef_flag = false;
6706 : 306541082 : bool funcdef_syntax = false;
6707 : 306541082 : bool size_varies = false;
6708 : 306541082 : bool size_error = false;
6709 : 306541082 : tree decl_attr = declspecs->decl_attr;
6710 : 306541082 : int array_ptr_quals = TYPE_UNQUALIFIED;
6711 : 306541082 : tree array_ptr_attrs = NULL_TREE;
6712 : 306541082 : bool array_parm_static = false;
6713 : 306541082 : bool array_parm_vla_unspec_p = false;
6714 : 306541082 : tree returned_attrs = NULL_TREE;
6715 : 306541082 : tree decl_id_attrs = NULL_TREE;
6716 : 306541082 : bool bitfield = width != NULL;
6717 : 306541082 : tree element_type;
6718 : 306541082 : tree orig_qual_type = NULL;
6719 : 306541082 : size_t orig_qual_indirect = 0;
6720 : 306541082 : struct c_arg_info *arg_info = 0;
6721 : 306541082 : addr_space_t as1, as2, address_space;
6722 : 306541082 : location_t loc = UNKNOWN_LOCATION;
6723 : 306541082 : tree expr_dummy;
6724 : 306541082 : bool expr_const_operands_dummy;
6725 : 306541082 : enum c_declarator_kind first_non_attr_kind;
6726 : 306541082 : unsigned int alignas_align = 0;
6727 : :
6728 : 306541082 : if (type == NULL_TREE)
6729 : : {
6730 : : /* This can occur for auto on a parameter in C23 mode. Set a
6731 : : dummy type here so subsequent code can give diagnostics for
6732 : : this case. */
6733 : 2 : gcc_assert (declspecs->c23_auto_p);
6734 : 2 : gcc_assert (decl_context == PARM);
6735 : 2 : type = declspecs->type = integer_type_node;
6736 : : }
6737 : 306541082 : if (TREE_CODE (type) == ERROR_MARK)
6738 : 29 : return error_mark_node;
6739 : 306541053 : if (expr == NULL)
6740 : : {
6741 : 37712 : expr = &expr_dummy;
6742 : 37712 : expr_dummy = NULL_TREE;
6743 : : }
6744 : 306541053 : if (expr_const_operands == NULL)
6745 : 188224572 : expr_const_operands = &expr_const_operands_dummy;
6746 : :
6747 : 306541053 : if (declspecs->expr)
6748 : : {
6749 : 914 : if (*expr)
6750 : 7 : *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6751 : : declspecs->expr);
6752 : : else
6753 : 907 : *expr = declspecs->expr;
6754 : : }
6755 : 306541053 : *expr_const_operands = declspecs->expr_const_operands;
6756 : :
6757 : 306541053 : if (decl_context == FUNCDEF)
6758 : 35380200 : funcdef_flag = true, decl_context = NORMAL;
6759 : :
6760 : : /* Look inside a declarator for the name being declared
6761 : : and get it as an IDENTIFIER_NODE, for an error message. */
6762 : 306541053 : {
6763 : 306541053 : const struct c_declarator *decl = declarator;
6764 : :
6765 : 306541053 : first_non_attr_kind = cdk_attrs;
6766 : 374985167 : while (decl)
6767 : 374985167 : switch (decl->kind)
6768 : : {
6769 : 1128744 : case cdk_array:
6770 : 1128744 : loc = decl->id_loc;
6771 : : /* FALL THRU. */
6772 : :
6773 : 68437459 : case cdk_function:
6774 : 68437459 : case cdk_pointer:
6775 : 68437459 : funcdef_syntax = (decl->kind == cdk_function);
6776 : 68437459 : if (first_non_attr_kind == cdk_attrs)
6777 : 66362545 : first_non_attr_kind = decl->kind;
6778 : 68437459 : decl = decl->declarator;
6779 : 68437459 : break;
6780 : :
6781 : 6655 : case cdk_attrs:
6782 : 6655 : decl = decl->declarator;
6783 : 6655 : break;
6784 : :
6785 : 306541053 : case cdk_id:
6786 : 306541053 : loc = decl->id_loc;
6787 : 306541053 : if (decl->u.id.id)
6788 : : name = decl->u.id.id;
6789 : 306541053 : decl_id_attrs = decl->u.id.attrs;
6790 : 306541053 : if (first_non_attr_kind == cdk_attrs)
6791 : 240178508 : first_non_attr_kind = decl->kind;
6792 : : decl = 0;
6793 : : break;
6794 : :
6795 : 0 : default:
6796 : 0 : gcc_unreachable ();
6797 : : }
6798 : 306541053 : if (name == NULL_TREE)
6799 : : {
6800 : 122877267 : gcc_assert (decl_context == PARM
6801 : : || decl_context == TYPENAME
6802 : : || (decl_context == FIELD
6803 : : && declarator->kind == cdk_id));
6804 : 122877267 : gcc_assert (!initialized);
6805 : : }
6806 : : }
6807 : :
6808 : : /* An enum type specifier (": specifier-qualifier-list") may only be
6809 : : specified when the enum is being defined or in an empty
6810 : : declaration of the form "enum identifier enum-type-specifier;".
6811 : : Except for the case of an empty declaration that has additional
6812 : : declaration specifiers, all invalid contexts (declarations that
6813 : : aren't empty, type names, parameter declarations, member
6814 : : declarations) pass through grokdeclarator. */
6815 : 306541053 : if (declspecs->enum_type_specifier_ref_p)
6816 : 6 : error_at (loc, "%<enum%> underlying type may not be specified here");
6817 : :
6818 : : /* A function definition's declarator must have the form of
6819 : : a function declarator. */
6820 : :
6821 : 306541053 : if (funcdef_flag && !funcdef_syntax)
6822 : : return NULL_TREE;
6823 : :
6824 : : /* If this looks like a function definition, make it one,
6825 : : even if it occurs where parms are expected.
6826 : : Then store_parm_decls will reject it and not use it as a parm. */
6827 : 306541022 : if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
6828 : 22656 : decl_context = PARM;
6829 : :
6830 : 306541022 : if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
6831 : : {
6832 : 306540998 : if (declspecs->unavailable_p)
6833 : 28 : error_unavailable_use (declspecs->type, declspecs->decl_attr);
6834 : 306540970 : else if (declspecs->deprecated_p
6835 : 46 : && deprecated_state != DEPRECATED_SUPPRESS)
6836 : 38 : warn_deprecated_use (declspecs->type, declspecs->decl_attr);
6837 : : }
6838 : :
6839 : 306541022 : if ((decl_context == NORMAL || decl_context == FIELD)
6840 : 67032075 : && current_scope == file_scope
6841 : 365679034 : && c_type_variably_modified_p (type))
6842 : : {
6843 : 3 : if (name)
6844 : 3 : error_at (loc, "variably modified %qE at file scope", name);
6845 : : else
6846 : 0 : error_at (loc, "variably modified field at file scope");
6847 : 3 : type = integer_type_node;
6848 : : }
6849 : :
6850 : 306541022 : size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
6851 : :
6852 : : /* Diagnose defaulting to "int". */
6853 : :
6854 : 306541022 : if (declspecs->default_int_p)
6855 : : {
6856 : : /* Issue a warning if this is an ISO C 99 program or if
6857 : : -Wreturn-type and this is a function, or if -Wimplicit;
6858 : : prefer the former warning since it is more explicit. */
6859 : 9763 : if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
6860 : 1228 : && funcdef_flag)
6861 : 702 : warn_about_return_type = 1;
6862 : : else
6863 : : {
6864 : 9061 : if (name)
6865 : 9051 : permerror_opt (loc, OPT_Wimplicit_int,
6866 : : "type defaults to %<int%> in declaration "
6867 : : "of %qE", name);
6868 : : else
6869 : 10 : permerror_opt (loc, OPT_Wimplicit_int,
6870 : : "type defaults to %<int%> in type name");
6871 : : }
6872 : : }
6873 : :
6874 : : /* Adjust the type if a bit-field is being declared,
6875 : : -funsigned-bitfields applied and the type is not explicitly
6876 : : "signed". */
6877 : 306541022 : if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
6878 : 40 : && TREE_CODE (type) == INTEGER_TYPE)
6879 : 38 : type = c_common_unsigned_type (type);
6880 : :
6881 : : /* Figure out the type qualifiers for the declaration. There are
6882 : : two ways a declaration can become qualified. One is something
6883 : : like `const int i' where the `const' is explicit. Another is
6884 : : something like `typedef const int CI; CI i' where the type of the
6885 : : declaration contains the `const'. A third possibility is that
6886 : : there is a type qualifier on the element type of a typedefed
6887 : : array type, in which case we should extract that qualifier so
6888 : : that c_apply_type_quals_to_decl receives the full list of
6889 : : qualifiers to work with (C90 is not entirely clear about whether
6890 : : duplicate qualifiers should be diagnosed in this case, but it
6891 : : seems most appropriate to do so). */
6892 : 306541022 : element_type = strip_array_types (type);
6893 : 306541022 : constp = declspecs->const_p + TYPE_READONLY (element_type);
6894 : 306541022 : restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
6895 : 306541022 : volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
6896 : 306541022 : atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
6897 : 306541022 : as1 = declspecs->address_space;
6898 : 306541022 : as2 = TYPE_ADDR_SPACE (element_type);
6899 : 306541022 : address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
6900 : :
6901 : 306541022 : if (constp > 1)
6902 : 25 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
6903 : 306541022 : if (restrictp > 1)
6904 : 6 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
6905 : 306541022 : if (volatilep > 1)
6906 : 15 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
6907 : 306541022 : if (atomicp > 1)
6908 : 9 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
6909 : :
6910 : 306541022 : if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
6911 : 0 : error_at (loc, "conflicting named address spaces (%s vs %s)",
6912 : : c_addr_space_name (as1), c_addr_space_name (as2));
6913 : :
6914 : 306541022 : if ((TREE_CODE (type) == ARRAY_TYPE
6915 : 306367894 : || first_non_attr_kind == cdk_array)
6916 : 307602793 : && TYPE_QUALS (element_type))
6917 : : {
6918 : 73 : orig_qual_type = type;
6919 : 73 : type = c_build_qualified_type (type, TYPE_UNQUALIFIED);
6920 : : }
6921 : 306541022 : type_quals = ((constp ? TYPE_QUAL_CONST : 0)
6922 : 306541022 : | (restrictp ? TYPE_QUAL_RESTRICT : 0)
6923 : 306541022 : | (volatilep ? TYPE_QUAL_VOLATILE : 0)
6924 : 306541022 : | (atomicp ? TYPE_QUAL_ATOMIC : 0)
6925 : 306541022 : | ENCODE_QUAL_ADDR_SPACE (address_space));
6926 : 306541022 : if (type_quals != TYPE_QUALS (element_type))
6927 : 12535044 : orig_qual_type = NULL_TREE;
6928 : :
6929 : : /* Applying the _Atomic qualifier to an array type (through the use
6930 : : of typedefs or typeof) must be detected here. If the qualifier
6931 : : is introduced later, any appearance of applying it to an array is
6932 : : actually applying it to an element of that array. */
6933 : 306541022 : if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
6934 : 6 : error_at (loc, "%<_Atomic%>-qualified array type");
6935 : :
6936 : : /* Warn about storage classes that are invalid for certain
6937 : : kinds of declarations (parameters, typenames, etc.). */
6938 : :
6939 : 306541022 : if (funcdef_flag
6940 : 35380169 : && (threadp
6941 : : || constexprp
6942 : 35380167 : || storage_class == csc_auto
6943 : 35380167 : || storage_class == csc_register
6944 : 35380122 : || storage_class == csc_typedef))
6945 : : {
6946 : 47 : if (storage_class == csc_auto)
6947 : 42 : pedwarn (loc,
6948 : 81 : (current_scope == file_scope) ? 0 : OPT_Wpedantic,
6949 : : "function definition declared %<auto%>");
6950 : 50 : if (storage_class == csc_register)
6951 : 3 : error_at (loc, "function definition declared %<register%>");
6952 : 50 : if (storage_class == csc_typedef)
6953 : 3 : error_at (loc, "function definition declared %<typedef%>");
6954 : 50 : if (threadp)
6955 : 2 : error_at (loc, "function definition declared %qs",
6956 : 2 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6957 : 50 : threadp = false;
6958 : : /* The parser ensures a constexpr function definition never
6959 : : reaches here. */
6960 : 50 : gcc_assert (!constexprp);
6961 : 50 : if (storage_class == csc_auto
6962 : 50 : || storage_class == csc_register
6963 : : || storage_class == csc_typedef)
6964 : 55 : storage_class = csc_none;
6965 : : }
6966 : 306540972 : else if (decl_context != NORMAL && (storage_class != csc_none
6967 : 243740569 : || threadp
6968 : 243739986 : || constexprp
6969 : 243739984 : || declspecs->c23_auto_p))
6970 : : {
6971 : 587 : if (decl_context == PARM
6972 : 587 : && storage_class == csc_register
6973 : 568 : && !constexprp
6974 : 566 : && !declspecs->c23_auto_p)
6975 : : ;
6976 : : else
6977 : : {
6978 : 21 : switch (decl_context)
6979 : : {
6980 : 0 : case FIELD:
6981 : 0 : if (name)
6982 : 0 : error_at (loc, "storage class specified for structure "
6983 : : "field %qE", name);
6984 : : else
6985 : 0 : error_at (loc, "storage class specified for structure field");
6986 : : break;
6987 : 21 : case PARM:
6988 : 21 : if (name)
6989 : 7 : error_at (loc, "storage class specified for parameter %qE",
6990 : : name);
6991 : : else
6992 : 14 : error_at (loc, "storage class specified for unnamed parameter");
6993 : : break;
6994 : 0 : default:
6995 : 0 : error_at (loc, "storage class specified for typename");
6996 : 0 : break;
6997 : : }
6998 : 306541022 : storage_class = csc_none;
6999 : 306541022 : threadp = false;
7000 : 306541022 : constexprp = false;
7001 : : }
7002 : : }
7003 : 306540385 : else if (storage_class == csc_extern
7004 : 306540385 : && initialized
7005 : 34545597 : && !funcdef_flag)
7006 : : {
7007 : : /* 'extern' with initialization is invalid if not at file scope. */
7008 : 28 : if (current_scope == file_scope)
7009 : : {
7010 : : /* It is fine to have 'extern const' when compiling at C
7011 : : and C++ intersection. */
7012 : 19 : if (!(warn_cxx_compat && constp))
7013 : 18 : warning_at (loc, 0, "%qE initialized and declared %<extern%>",
7014 : : name);
7015 : : }
7016 : : else
7017 : 9 : error_at (loc, "%qE has both %<extern%> and initializer", name);
7018 : : }
7019 : 306540357 : else if (current_scope == file_scope)
7020 : : {
7021 : 60010014 : if (storage_class == csc_auto)
7022 : 7 : error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
7023 : : name);
7024 : 60010014 : if (pedantic && storage_class == csc_register)
7025 : 4 : pedwarn (input_location, OPT_Wpedantic,
7026 : : "file-scope declaration of %qE specifies %<register%>", name);
7027 : : }
7028 : : else
7029 : : {
7030 : 246530343 : if (storage_class == csc_extern && funcdef_flag)
7031 : 3 : error_at (loc, "nested function %qE declared %<extern%>", name);
7032 : 246530340 : else if (threadp && storage_class == csc_none)
7033 : : {
7034 : 14 : error_at (loc, "function-scope %qE implicitly auto and declared "
7035 : : "%qs", name,
7036 : 7 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7037 : 7 : threadp = false;
7038 : : }
7039 : : }
7040 : :
7041 : : /* Now figure out the structure of the declarator proper.
7042 : : Descend through it, creating more complex types, until we reach
7043 : : the declared identifier (or NULL_TREE, in an absolute declarator).
7044 : : At each stage we maintain an unqualified version of the type
7045 : : together with any qualifiers that should be applied to it with
7046 : : c_build_qualified_type; this way, array types including
7047 : : multidimensional array types are first built up in unqualified
7048 : : form and then the qualified form is created with
7049 : : TYPE_MAIN_VARIANT pointing to the unqualified form. */
7050 : :
7051 : 374985135 : while (declarator && declarator->kind != cdk_id)
7052 : : {
7053 : 68444113 : if (type == error_mark_node)
7054 : : {
7055 : 38 : declarator = declarator->declarator;
7056 : 38 : continue;
7057 : : }
7058 : :
7059 : : /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
7060 : : a cdk_pointer (for *...),
7061 : : a cdk_function (for ...(...)),
7062 : : a cdk_attrs (for nested attributes),
7063 : : or a cdk_id (for the name being declared
7064 : : or the place in an absolute declarator
7065 : : where the name was omitted).
7066 : : For the last case, we have just exited the loop.
7067 : :
7068 : : At this point, TYPE is the type of elements of an array,
7069 : : or for a function to return, or for a pointer to point to.
7070 : : After this sequence of ifs, TYPE is the type of the
7071 : : array or function or pointer, and DECLARATOR has had its
7072 : : outermost layer removed. */
7073 : :
7074 : 68444075 : if (array_ptr_quals != TYPE_UNQUALIFIED
7075 : 68444075 : || array_ptr_attrs != NULL_TREE
7076 : 68444075 : || array_parm_static)
7077 : : {
7078 : : /* Only the innermost declarator (making a parameter be of
7079 : : array type which is converted to pointer type)
7080 : : may have static or type qualifiers. */
7081 : 1 : error_at (loc, "static or type qualifiers in non-parameter array declarator");
7082 : 1 : array_ptr_quals = TYPE_UNQUALIFIED;
7083 : 1 : array_ptr_attrs = NULL_TREE;
7084 : 1 : array_parm_static = false;
7085 : : }
7086 : :
7087 : 68444075 : switch (declarator->kind)
7088 : : {
7089 : 6655 : case cdk_attrs:
7090 : 6655 : {
7091 : : /* A declarator with embedded attributes. */
7092 : 6655 : tree attrs = declarator->u.attrs;
7093 : 6655 : const struct c_declarator *inner_decl;
7094 : 6655 : int attr_flags = 0;
7095 : 6655 : declarator = declarator->declarator;
7096 : : /* Standard attribute syntax precisely defines what entity
7097 : : an attribute in each position appertains to, so only
7098 : : apply laxity about positioning to GNU attribute syntax.
7099 : : Standard attributes applied to a function or array
7100 : : declarator apply exactly to that type; standard
7101 : : attributes applied to the identifier apply to the
7102 : : declaration rather than to the type, and are specified
7103 : : using a cdk_id declarator rather than using
7104 : : cdk_attrs. */
7105 : 6655 : inner_decl = declarator;
7106 : 6655 : while (inner_decl->kind == cdk_attrs)
7107 : 0 : inner_decl = inner_decl->declarator;
7108 : 6655 : if (!cxx11_attribute_p (attrs))
7109 : : {
7110 : 6560 : if (inner_decl->kind == cdk_id)
7111 : : attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
7112 : : else if (inner_decl->kind == cdk_function)
7113 : : attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
7114 : : else if (inner_decl->kind == cdk_array)
7115 : 6655 : attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
7116 : : }
7117 : 6655 : attrs = c_warn_type_attributes (type, attrs);
7118 : 6655 : returned_attrs = decl_attributes (&type,
7119 : : chainon (returned_attrs, attrs),
7120 : : attr_flags);
7121 : 6655 : break;
7122 : : }
7123 : 1128734 : case cdk_array:
7124 : 1128734 : {
7125 : 1128734 : tree itype = NULL_TREE;
7126 : 1128734 : tree size = declarator->u.array.dimen;
7127 : : /* The index is a signed object `sizetype' bits wide. */
7128 : 1128734 : tree index_type = c_common_signed_type (sizetype);
7129 : :
7130 : 1128734 : array_ptr_quals = declarator->u.array.quals;
7131 : 1128734 : array_ptr_attrs = declarator->u.array.attrs;
7132 : 1128734 : array_parm_static = declarator->u.array.static_p;
7133 : 1128734 : array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
7134 : :
7135 : 1128734 : declarator = declarator->declarator;
7136 : :
7137 : : /* Check for some types that there cannot be arrays of. */
7138 : :
7139 : 1128734 : if (VOID_TYPE_P (type))
7140 : : {
7141 : 11 : if (name)
7142 : 9 : error_at (loc, "declaration of %qE as array of voids", name);
7143 : : else
7144 : 2 : error_at (loc, "declaration of type name as array of voids");
7145 : 11 : type = error_mark_node;
7146 : : }
7147 : :
7148 : 1128734 : if (TREE_CODE (type) == FUNCTION_TYPE)
7149 : : {
7150 : 3 : if (name)
7151 : 2 : error_at (loc, "declaration of %qE as array of functions",
7152 : : name);
7153 : : else
7154 : 1 : error_at (loc, "declaration of type name as array of "
7155 : : "functions");
7156 : 3 : type = error_mark_node;
7157 : : }
7158 : :
7159 : 21893 : if (pedantic && !in_system_header_at (input_location)
7160 : 1143457 : && flexible_array_type_p (type))
7161 : 20 : pedwarn (loc, OPT_Wpedantic,
7162 : : "invalid use of structure with flexible array member");
7163 : :
7164 : 1128734 : if (size == error_mark_node)
7165 : 119 : type = error_mark_node;
7166 : :
7167 : 1128734 : if (type == error_mark_node)
7168 : 133 : continue;
7169 : :
7170 : 1128601 : if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
7171 : : {
7172 : 0 : type = error_mark_node;
7173 : 0 : continue;
7174 : : }
7175 : :
7176 : : /* If size was specified, set ITYPE to a range-type for
7177 : : that size. Otherwise, ITYPE remains null. finish_decl
7178 : : may figure it out from an initial value. */
7179 : :
7180 : 1128601 : if (size)
7181 : : {
7182 : 960334 : bool size_maybe_const = true;
7183 : 960334 : bool size_int_const = (TREE_CODE (size) == INTEGER_CST
7184 : 960334 : && !TREE_OVERFLOW (size));
7185 : 960334 : bool this_size_varies = false;
7186 : :
7187 : : /* Strip NON_LVALUE_EXPRs since we aren't using as an
7188 : : lvalue. */
7189 : 960343 : STRIP_TYPE_NOPS (size);
7190 : :
7191 : 960334 : if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
7192 : : {
7193 : 16 : if (name)
7194 : 14 : error_at (loc, "size of array %qE has non-integer type",
7195 : : name);
7196 : : else
7197 : 2 : error_at (loc,
7198 : : "size of unnamed array has non-integer type");
7199 : 16 : size = integer_one_node;
7200 : 16 : size_int_const = true;
7201 : 16 : size_error = true;
7202 : : }
7203 : : /* This can happen with enum forward declaration. */
7204 : 960318 : else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
7205 : : {
7206 : 0 : if (name)
7207 : 0 : error_at (loc, "size of array %qE has incomplete type",
7208 : : name);
7209 : : else
7210 : 0 : error_at (loc, "size of unnamed array has incomplete "
7211 : : "type");
7212 : 0 : size = integer_one_node;
7213 : 0 : size_int_const = true;
7214 : 0 : size_error = true;
7215 : : }
7216 : :
7217 : 960334 : size = c_fully_fold (size, false, &size_maybe_const);
7218 : :
7219 : 960334 : if (pedantic && size_maybe_const && integer_zerop (size))
7220 : : {
7221 : 3 : if (name)
7222 : 3 : pedwarn (loc, OPT_Wpedantic,
7223 : : "ISO C forbids zero-size array %qE", name);
7224 : : else
7225 : 0 : pedwarn (loc, OPT_Wpedantic,
7226 : : "ISO C forbids zero-size array");
7227 : : }
7228 : :
7229 : 960334 : if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
7230 : : {
7231 : 938040 : constant_expression_warning (size);
7232 : 938040 : if (tree_int_cst_sgn (size) < 0)
7233 : : {
7234 : 521 : if (name)
7235 : 518 : error_at (loc, "size of array %qE is negative", name);
7236 : : else
7237 : 3 : error_at (loc, "size of unnamed array is negative");
7238 : 521 : size = integer_one_node;
7239 : 521 : size_int_const = true;
7240 : 521 : size_error = true;
7241 : : }
7242 : : /* Handle a size folded to an integer constant but
7243 : : not an integer constant expression. */
7244 : 938040 : if (!size_int_const)
7245 : : {
7246 : : /* If this is a file scope declaration of an
7247 : : ordinary identifier, this is invalid code;
7248 : : diagnosing it here and not subsequently
7249 : : treating the type as variable-length avoids
7250 : : more confusing diagnostics later. */
7251 : 154 : if ((decl_context == NORMAL || decl_context == FIELD)
7252 : 142 : && current_scope == file_scope)
7253 : 14 : pedwarn (input_location, 0,
7254 : : "variably modified %qE at file scope",
7255 : : name);
7256 : : else
7257 : : this_size_varies = size_varies = true;
7258 : 154 : warn_variable_length_array (name, size);
7259 : : }
7260 : : }
7261 : 22294 : else if ((decl_context == NORMAL || decl_context == FIELD)
7262 : 13215 : && current_scope == file_scope)
7263 : : {
7264 : 21 : error_at (loc, "variably modified %qE at file scope", name);
7265 : 21 : size = integer_one_node;
7266 : : }
7267 : : else
7268 : : {
7269 : : /* Make sure the array size remains visibly
7270 : : nonconstant even if it is (eg) a const variable
7271 : : with known value. */
7272 : 22273 : this_size_varies = size_varies = true;
7273 : 22273 : warn_variable_length_array (name, size);
7274 : 22273 : if (sanitize_flags_p (SANITIZE_VLA)
7275 : 178 : && current_function_decl != NULL_TREE
7276 : 22433 : && decl_context == NORMAL)
7277 : : {
7278 : : /* Evaluate the array size only once. */
7279 : 152 : size = save_expr (size);
7280 : 152 : size = c_fully_fold (size, false, NULL);
7281 : 152 : size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
7282 : : ubsan_instrument_vla (loc, size),
7283 : : size);
7284 : : }
7285 : : }
7286 : :
7287 : 960334 : if (integer_zerop (size) && !this_size_varies)
7288 : : {
7289 : : /* A zero-length array cannot be represented with
7290 : : an unsigned index type, which is what we'll
7291 : : get with build_index_type. Create an
7292 : : open-ended range instead. */
7293 : 2595 : itype = build_range_type (sizetype, size, NULL_TREE);
7294 : : }
7295 : : else
7296 : : {
7297 : : /* Arrange for the SAVE_EXPR on the inside of the
7298 : : MINUS_EXPR, which allows the -1 to get folded
7299 : : with the +1 that happens when building TYPE_SIZE. */
7300 : 957739 : if (size_varies)
7301 : 22783 : size = save_expr (size);
7302 : 957739 : if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
7303 : 140 : size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7304 : : integer_zero_node, size);
7305 : :
7306 : : /* Compute the maximum valid index, that is, size
7307 : : - 1. Do the calculation in index_type, so that
7308 : : if it is a variable the computations will be
7309 : : done in the proper mode. */
7310 : 957739 : itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
7311 : : convert (index_type, size),
7312 : : convert (index_type,
7313 : : size_one_node));
7314 : :
7315 : : /* The above overflows when size does not fit
7316 : : in index_type.
7317 : : ??? While a size of INT_MAX+1 technically shouldn't
7318 : : cause an overflow (because we subtract 1), handling
7319 : : this case seems like an unnecessary complication. */
7320 : 957739 : if (TREE_CODE (size) == INTEGER_CST
7321 : 935326 : && !int_fits_type_p (size, index_type))
7322 : : {
7323 : 6 : if (name)
7324 : 5 : error_at (loc, "size of array %qE is too large",
7325 : : name);
7326 : : else
7327 : 1 : error_at (loc, "size of unnamed array is too large");
7328 : 6 : type = error_mark_node;
7329 : 6 : continue;
7330 : : }
7331 : :
7332 : 957733 : itype = build_index_type (itype);
7333 : : }
7334 : 960328 : if (this_size_varies)
7335 : : {
7336 : 22413 : if (TREE_SIDE_EFFECTS (size))
7337 : : {
7338 : 22052 : if (*expr)
7339 : 8173 : *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7340 : : *expr, size);
7341 : : else
7342 : 13879 : *expr = size;
7343 : : }
7344 : 22413 : *expr_const_operands &= size_maybe_const;
7345 : : }
7346 : : }
7347 : 168267 : else if (decl_context == FIELD)
7348 : : {
7349 : 84591 : bool flexible_array_member = false;
7350 : 84591 : if (array_parm_vla_unspec_p)
7351 : : /* Field names can in fact have function prototype
7352 : : scope so [*] is disallowed here through making
7353 : : the field variably modified, not through being
7354 : : something other than a declaration with function
7355 : : prototype scope. */
7356 : : size_varies = true;
7357 : : else
7358 : : {
7359 : : const struct c_declarator *t = declarator;
7360 : 84589 : while (t->kind == cdk_attrs)
7361 : 0 : t = t->declarator;
7362 : 84589 : flexible_array_member = (t->kind == cdk_id);
7363 : : }
7364 : 84589 : if (flexible_array_member
7365 : 84589 : && !in_system_header_at (input_location))
7366 : 83558 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7367 : : "support flexible array members");
7368 : :
7369 : : /* ISO C99 Flexible array members are effectively
7370 : : identical to GCC's zero-length array extension. */
7371 : 84591 : if (flexible_array_member)
7372 : 84569 : itype = build_index_type (NULL_TREE);
7373 : : }
7374 : :
7375 : 1128595 : if (array_parm_vla_unspec_p)
7376 : : {
7377 : : /* C99 6.7.5.2p4 */
7378 : 142 : if (decl_context == TYPENAME)
7379 : 5 : warning (0, "%<[*]%> not in a declaration");
7380 : : size_varies = true;
7381 : : }
7382 : :
7383 : : /* Complain about arrays of incomplete types. */
7384 : 1128595 : if (!COMPLETE_TYPE_P (type))
7385 : : {
7386 : 57 : auto_diagnostic_group d;
7387 : 57 : error_at (loc, "array type has incomplete element type %qT",
7388 : : type);
7389 : : /* See if we can be more helpful. */
7390 : 57 : if (TREE_CODE (type) == ARRAY_TYPE)
7391 : : {
7392 : 29 : if (name)
7393 : 24 : inform (loc, "declaration of %qE as multidimensional "
7394 : : "array must have bounds for all dimensions "
7395 : : "except the first", name);
7396 : : else
7397 : 5 : inform (loc, "declaration of multidimensional array "
7398 : : "must have bounds for all dimensions except "
7399 : : "the first");
7400 : : }
7401 : 57 : type = error_mark_node;
7402 : 57 : }
7403 : : else
7404 : : /* When itype is NULL, a shared incomplete array type is
7405 : : returned for all array of a given type. Elsewhere we
7406 : : make sure we don't complete that type before copying
7407 : : it, but here we want to make sure we don't ever
7408 : : modify the shared type, so we gcc_assert (itype)
7409 : : below. */
7410 : : {
7411 : 1128538 : addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
7412 : 1128538 : if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
7413 : 0 : type = c_build_qualified_type (type,
7414 : : ENCODE_QUAL_ADDR_SPACE (as));
7415 : 1128538 : if (array_parm_vla_unspec_p)
7416 : 142 : type = c_build_array_type_unspecified (type);
7417 : : else
7418 : 1128396 : type = c_build_array_type (type, itype);
7419 : : }
7420 : :
7421 : 1128595 : if (type != error_mark_node)
7422 : : {
7423 : : /* The GCC extension for zero-length arrays differs from
7424 : : ISO flexible array members in that sizeof yields
7425 : : zero. */
7426 : 1128538 : if (size && integer_zerop (size))
7427 : : {
7428 : 2588 : gcc_assert (itype);
7429 : 2588 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7430 : 2588 : TYPE_SIZE (type) = bitsize_zero_node;
7431 : 2588 : TYPE_SIZE_UNIT (type) = size_zero_node;
7432 : 2588 : SET_TYPE_STRUCTURAL_EQUALITY (type);
7433 : : }
7434 : :
7435 : 1128538 : if (!valid_array_size_p (loc, type, name))
7436 : 33 : type = error_mark_node;
7437 : : }
7438 : :
7439 : 1128595 : if (decl_context != PARM
7440 : 815465 : && (array_ptr_quals != TYPE_UNQUALIFIED
7441 : 815465 : || array_ptr_attrs != NULL_TREE
7442 : 815464 : || array_parm_static))
7443 : : {
7444 : 2 : error_at (loc, "static or type qualifiers in non-parameter "
7445 : : "array declarator");
7446 : 2 : array_ptr_quals = TYPE_UNQUALIFIED;
7447 : 2 : array_ptr_attrs = NULL_TREE;
7448 : 2 : array_parm_static = false;
7449 : : }
7450 : 1128595 : orig_qual_indirect++;
7451 : 1128595 : break;
7452 : : }
7453 : 49417426 : case cdk_function:
7454 : 49417426 : {
7455 : : /* Say it's a definition only for the declarator closest
7456 : : to the identifier, apart possibly from some
7457 : : attributes. */
7458 : 49417426 : bool really_funcdef = false;
7459 : 49417426 : tree arg_types;
7460 : 49417426 : orig_qual_type = NULL_TREE;
7461 : 49417426 : if (funcdef_flag)
7462 : : {
7463 : 35380191 : const struct c_declarator *t = declarator->declarator;
7464 : 35380209 : while (t->kind == cdk_attrs)
7465 : 18 : t = t->declarator;
7466 : 35380191 : really_funcdef = (t->kind == cdk_id);
7467 : : }
7468 : :
7469 : : /* Declaring a function type. Make sure we have a valid
7470 : : type for the function to return. */
7471 : 49417426 : if (type == error_mark_node)
7472 : 0 : continue;
7473 : :
7474 : 49417426 : size_varies = false;
7475 : :
7476 : : /* Warn about some types functions can't return. */
7477 : 49417426 : if (TREE_CODE (type) == FUNCTION_TYPE)
7478 : : {
7479 : 0 : if (name)
7480 : 0 : error_at (loc, "%qE declared as function returning a "
7481 : : "function", name);
7482 : : else
7483 : 0 : error_at (loc, "type name declared as function "
7484 : : "returning a function");
7485 : 0 : type = integer_type_node;
7486 : : }
7487 : 49417426 : if (TREE_CODE (type) == ARRAY_TYPE)
7488 : : {
7489 : 0 : if (name)
7490 : 0 : error_at (loc, "%qE declared as function returning an array",
7491 : : name);
7492 : : else
7493 : 0 : error_at (loc, "type name declared as function returning "
7494 : : "an array");
7495 : 0 : type = integer_type_node;
7496 : : }
7497 : :
7498 : : /* Construct the function type and go to the next
7499 : : inner layer of declarator. */
7500 : 49417426 : arg_info = declarator->u.arg_info;
7501 : 49417426 : arg_types = grokparms (arg_info, really_funcdef);
7502 : :
7503 : : /* Type qualifiers before the return type of the function
7504 : : qualify the return type, not the function type. */
7505 : 49417426 : if (type_quals)
7506 : : {
7507 : 235 : const enum c_declspec_word ignored_quals_list[] =
7508 : : {
7509 : : cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
7510 : : cdw_atomic, cdw_number_of_elements
7511 : : };
7512 : 235 : location_t specs_loc
7513 : 235 : = smallest_type_quals_location (declspecs->locations,
7514 : : ignored_quals_list);
7515 : 235 : if (specs_loc == UNKNOWN_LOCATION)
7516 : 139 : specs_loc = declspecs->locations[cdw_typedef];
7517 : 139 : if (specs_loc == UNKNOWN_LOCATION)
7518 : 15 : specs_loc = loc;
7519 : :
7520 : : /* Type qualifiers on a function return type are
7521 : : normally permitted by the standard but have no
7522 : : effect, so give a warning at -Wreturn-type.
7523 : : Qualifiers on a void return type are banned on
7524 : : function definitions in ISO C; GCC used to used
7525 : : them for noreturn functions. The resolution of C11
7526 : : DR#423 means qualifiers (other than _Atomic) are
7527 : : actually removed from the return type when
7528 : : determining the function type. For C23, _Atomic is
7529 : : removed as well. */
7530 : 235 : int quals_used = type_quals;
7531 : 235 : if (flag_isoc23)
7532 : : quals_used = 0;
7533 : 65 : else if (flag_isoc11)
7534 : 31 : quals_used &= TYPE_QUAL_ATOMIC;
7535 : 65 : if (quals_used && VOID_TYPE_P (type) && really_funcdef)
7536 : 5 : pedwarn (specs_loc, 0,
7537 : : "function definition has qualified void "
7538 : : "return type");
7539 : : else
7540 : 230 : warning_at (specs_loc, OPT_Wignored_qualifiers,
7541 : : "type qualifiers ignored on function "
7542 : : "return type");
7543 : :
7544 : : /* Ensure an error for restrict on invalid types; the
7545 : : DR#423 resolution is not entirely clear about
7546 : : this. */
7547 : 235 : if (flag_isoc11
7548 : 201 : && (type_quals & TYPE_QUAL_RESTRICT)
7549 : 241 : && (!POINTER_TYPE_P (type)
7550 : 4 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
7551 : 4 : error_at (loc, "invalid use of %<restrict%>");
7552 : 235 : type = c_build_qualified_type (type, quals_used);
7553 : : }
7554 : 49417426 : type_quals = TYPE_UNQUALIFIED;
7555 : :
7556 : 98834852 : type = c_build_function_type (type, arg_types,
7557 : 49417426 : arg_info->no_named_args_stdarg_p);
7558 : 49417426 : declarator = declarator->declarator;
7559 : :
7560 : : /* Set the TYPE_CONTEXTs for each tagged type which is local to
7561 : : the formal parameter list of this FUNCTION_TYPE to point to
7562 : : the FUNCTION_TYPE node itself. */
7563 : 49417426 : {
7564 : 49417426 : c_arg_tag *tag;
7565 : 49417426 : unsigned ix;
7566 : :
7567 : 424402827 : FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7568 : 134 : TYPE_CONTEXT (tag->type) = type;
7569 : : }
7570 : : break;
7571 : : }
7572 : 17891260 : case cdk_pointer:
7573 : 17891260 : {
7574 : : /* Merge any constancy or volatility into the target type
7575 : : for the pointer. */
7576 : 17891260 : if ((type_quals & TYPE_QUAL_ATOMIC)
7577 : 1839 : && TREE_CODE (type) == FUNCTION_TYPE)
7578 : : {
7579 : 2 : error_at (loc,
7580 : : "%<_Atomic%>-qualified function type");
7581 : 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
7582 : : }
7583 : 17891258 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7584 : 1701 : && type_quals)
7585 : 0 : pedwarn (loc, OPT_Wpedantic,
7586 : : "ISO C forbids qualified function types");
7587 : 17889559 : if (type_quals)
7588 : 5886623 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7589 : : orig_qual_indirect);
7590 : 17891260 : orig_qual_type = NULL_TREE;
7591 : 17891260 : size_varies = false;
7592 : :
7593 : : /* When the pointed-to type involves components of variable size,
7594 : : care must be taken to ensure that the size evaluation code is
7595 : : emitted early enough to dominate all the possible later uses
7596 : : and late enough for the variables on which it depends to have
7597 : : been assigned.
7598 : :
7599 : : This is expected to happen automatically when the pointed-to
7600 : : type has a name/declaration of it's own, but special attention
7601 : : is required if the type is anonymous. */
7602 : 17891260 : if (!TYPE_NAME (type) && c_type_variably_modified_p (type))
7603 : : {
7604 : 5938 : bool bind_p = decl_context == TYPENAME
7605 : : || decl_context == FIELD
7606 : 5938 : || decl_context == PARM;
7607 : 11371 : add_decl_expr (loc, type, bind_p ? expr : NULL, true);
7608 : : }
7609 : :
7610 : 17891260 : type = c_build_pointer_type (type);
7611 : :
7612 : : /* Process type qualifiers (such as const or volatile)
7613 : : that were given inside the `*'. */
7614 : 17891260 : type_quals = declarator->u.pointer_quals;
7615 : :
7616 : 17891260 : declarator = declarator->declarator;
7617 : 17891260 : break;
7618 : : }
7619 : 0 : default:
7620 : 0 : gcc_unreachable ();
7621 : : }
7622 : : }
7623 : 306541022 : *decl_attrs = chainon (returned_attrs, *decl_attrs);
7624 : 306541022 : *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
7625 : :
7626 : : /* Now TYPE has the actual type, apart from any qualifiers in
7627 : : TYPE_QUALS. */
7628 : :
7629 : : /* Warn about address space used for things other than static memory or
7630 : : pointers. */
7631 : 306541022 : address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
7632 : 306541022 : if (!ADDR_SPACE_GENERIC_P (address_space))
7633 : : {
7634 : 10 : if (decl_context == NORMAL)
7635 : : {
7636 : 10 : switch (storage_class)
7637 : : {
7638 : 0 : case csc_auto:
7639 : 0 : error ("%qs combined with %<auto%> qualifier for %qE",
7640 : : c_addr_space_name (address_space), name);
7641 : 0 : break;
7642 : 0 : case csc_register:
7643 : 0 : error ("%qs combined with %<register%> qualifier for %qE",
7644 : : c_addr_space_name (address_space), name);
7645 : 0 : break;
7646 : 6 : case csc_none:
7647 : 6 : if (current_function_scope)
7648 : : {
7649 : 0 : error ("%qs specified for auto variable %qE",
7650 : : c_addr_space_name (address_space), name);
7651 : 0 : break;
7652 : : }
7653 : : break;
7654 : : case csc_static:
7655 : : case csc_extern:
7656 : : case csc_typedef:
7657 : : break;
7658 : 0 : default:
7659 : 0 : gcc_unreachable ();
7660 : : }
7661 : : }
7662 : 0 : else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
7663 : : {
7664 : 0 : if (name)
7665 : 0 : error ("%qs specified for parameter %qE",
7666 : : c_addr_space_name (address_space), name);
7667 : : else
7668 : 0 : error ("%qs specified for unnamed parameter",
7669 : : c_addr_space_name (address_space));
7670 : : }
7671 : 0 : else if (decl_context == FIELD)
7672 : : {
7673 : 0 : if (name)
7674 : 0 : error ("%qs specified for structure field %qE",
7675 : : c_addr_space_name (address_space), name);
7676 : : else
7677 : 0 : error ("%qs specified for structure field",
7678 : : c_addr_space_name (address_space));
7679 : : }
7680 : : }
7681 : :
7682 : : /* Check the type and width of a bit-field. */
7683 : 306541022 : if (bitfield)
7684 : : {
7685 : 51871 : check_bitfield_type_and_width (loc, &type, width, name);
7686 : : /* C11 makes it implementation-defined (6.7.2.1#5) whether
7687 : : atomic types are permitted for bit-fields; we have no code to
7688 : : make bit-field accesses atomic, so disallow them. */
7689 : 51871 : if (type_quals & TYPE_QUAL_ATOMIC)
7690 : : {
7691 : 2 : if (name)
7692 : 1 : error_at (loc, "bit-field %qE has atomic type", name);
7693 : : else
7694 : 1 : error_at (loc, "bit-field has atomic type");
7695 : 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
7696 : : }
7697 : : }
7698 : :
7699 : : /* Reject invalid uses of _Alignas. */
7700 : 306541022 : if (declspecs->alignas_p)
7701 : : {
7702 : 188 : if (storage_class == csc_typedef)
7703 : 1 : error_at (loc, "alignment specified for typedef %qE", name);
7704 : 187 : else if (storage_class == csc_register)
7705 : 1 : error_at (loc, "alignment specified for %<register%> object %qE",
7706 : : name);
7707 : 186 : else if (decl_context == PARM)
7708 : : {
7709 : 2 : if (name)
7710 : 1 : error_at (loc, "alignment specified for parameter %qE", name);
7711 : : else
7712 : 1 : error_at (loc, "alignment specified for unnamed parameter");
7713 : : }
7714 : 184 : else if (bitfield)
7715 : : {
7716 : 0 : if (name)
7717 : 0 : error_at (loc, "alignment specified for bit-field %qE", name);
7718 : : else
7719 : 0 : error_at (loc, "alignment specified for unnamed bit-field");
7720 : : }
7721 : 184 : else if (TREE_CODE (type) == FUNCTION_TYPE)
7722 : 1 : error_at (loc, "alignment specified for function %qE", name);
7723 : 183 : else if (declspecs->align_log != -1 && TYPE_P (type))
7724 : : {
7725 : 156 : alignas_align = 1U << declspecs->align_log;
7726 : 156 : if (alignas_align < min_align_of_type (type))
7727 : : {
7728 : 26 : if (name)
7729 : 25 : error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7730 : : "alignment of %qE", name);
7731 : : else
7732 : 1 : error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7733 : : "alignment of unnamed field");
7734 : : alignas_align = 0;
7735 : : }
7736 : : }
7737 : : }
7738 : :
7739 : : /* If this is declaring a typedef name, return a TYPE_DECL. */
7740 : :
7741 : 306541022 : if (storage_class == csc_typedef)
7742 : : {
7743 : 4218192 : tree decl;
7744 : 4218192 : if ((type_quals & TYPE_QUAL_ATOMIC)
7745 : 11187 : && TREE_CODE (type) == FUNCTION_TYPE)
7746 : : {
7747 : 0 : error_at (loc,
7748 : : "%<_Atomic%>-qualified function type");
7749 : 0 : type_quals &= ~TYPE_QUAL_ATOMIC;
7750 : : }
7751 : 4218192 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7752 : 609 : && type_quals)
7753 : 0 : pedwarn (loc, OPT_Wpedantic,
7754 : : "ISO C forbids qualified function types");
7755 : 4217583 : if (type_quals)
7756 : 29738 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7757 : : orig_qual_indirect);
7758 : 8436384 : decl = build_decl (declarator->id_loc,
7759 : 4218192 : TYPE_DECL, declarator->u.id.id, type);
7760 : 4218192 : if (declspecs->explicit_signed_p)
7761 : 347532 : C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7762 : 4218192 : if (declspecs->inline_p)
7763 : 4 : pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7764 : 4218192 : if (declspecs->noreturn_p)
7765 : 1 : pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7766 : :
7767 : 4218192 : if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7768 : : {
7769 : 1825 : struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7770 : :
7771 : 1825 : if (b != NULL
7772 : 40 : && b->decl != NULL_TREE
7773 : 40 : && (B_IN_CURRENT_SCOPE (b)
7774 : 4 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7775 : 1861 : && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7776 : : {
7777 : 7 : auto_diagnostic_group d;
7778 : 8 : if (warning_at (declarator->id_loc, OPT_Wc___compat,
7779 : : "using %qD as both a typedef and a tag is "
7780 : : "invalid in C++", decl)
7781 : 7 : && b->locus != UNKNOWN_LOCATION)
7782 : 6 : inform (b->locus, "originally defined here");
7783 : 7 : }
7784 : : }
7785 : :
7786 : 4218192 : return decl;
7787 : : }
7788 : :
7789 : : /* If this is a type name (such as, in a cast or sizeof),
7790 : : compute the type and return it now. */
7791 : :
7792 : 302322830 : if (decl_context == TYPENAME)
7793 : : {
7794 : : /* Note that the grammar rejects storage classes in typenames
7795 : : and fields. */
7796 : 118374970 : gcc_assert (storage_class == csc_none && !threadp
7797 : : && !declspecs->inline_p && !declspecs->noreturn_p);
7798 : 118374970 : if ((type_quals & TYPE_QUAL_ATOMIC)
7799 : 256 : && TREE_CODE (type) == FUNCTION_TYPE)
7800 : : {
7801 : 0 : error_at (loc,
7802 : : "%<_Atomic%>-qualified function type");
7803 : 0 : type_quals &= ~TYPE_QUAL_ATOMIC;
7804 : : }
7805 : 118374970 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7806 : 22 : && type_quals)
7807 : 0 : pedwarn (loc, OPT_Wpedantic,
7808 : : "ISO C forbids const or volatile function types");
7809 : 118374948 : if (type_quals)
7810 : 856 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7811 : : orig_qual_indirect);
7812 : 118374970 : return type;
7813 : : }
7814 : :
7815 : 374808 : if (pedantic && decl_context == FIELD
7816 : 183972617 : && c_type_variably_modified_p (type))
7817 : : {
7818 : : /* C99 6.7.2.1p8 */
7819 : 6 : pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
7820 : : "have a variably modified type");
7821 : : }
7822 : :
7823 : : /* Aside from typedefs and type names (handle above),
7824 : : `void' at top level (not within pointer)
7825 : : is allowed only in public variables.
7826 : : We don't complain about parms either, but that is because
7827 : : a better error message can be made later. */
7828 : :
7829 : 183947860 : if (VOID_TYPE_P (type) && decl_context != PARM
7830 : 64 : && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
7831 : : && (storage_class == csc_extern
7832 : 27 : || (current_scope == file_scope
7833 : 18 : && !(storage_class == csc_static
7834 : : || storage_class == csc_register)))))
7835 : : {
7836 : 15 : error_at (loc, "variable or field %qE declared void", name);
7837 : 15 : type = integer_type_node;
7838 : : }
7839 : :
7840 : : /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7841 : : or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
7842 : :
7843 : 183013620 : {
7844 : 183013620 : tree decl;
7845 : :
7846 : 183013620 : if (decl_context == PARM)
7847 : : {
7848 : 121133977 : tree promoted_type;
7849 : 121133977 : bool array_parameter_p = false;
7850 : :
7851 : : /* A parameter declared as an array of T is really a pointer to T.
7852 : : One declared as a function is really a pointer to a function. */
7853 : :
7854 : 121133977 : if (TREE_CODE (type) == ARRAY_TYPE)
7855 : : {
7856 : 429419 : if (!size_error)
7857 : 429405 : *decl_attrs = build_arg_spec_attribute (type, array_parm_static,
7858 : : *decl_attrs);
7859 : :
7860 : : /* Transfer const-ness of array into that of type pointed to. */
7861 : 429419 : type = TREE_TYPE (type);
7862 : 429419 : if (orig_qual_type != NULL_TREE)
7863 : : {
7864 : 7 : if (orig_qual_indirect == 0)
7865 : 5 : orig_qual_type = TREE_TYPE (orig_qual_type);
7866 : : else
7867 : 2 : orig_qual_indirect--;
7868 : : }
7869 : 429419 : if (type_quals)
7870 : 47117 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7871 : : orig_qual_indirect);
7872 : :
7873 : : /* The pointed-to type may need a decl expr (see above). */
7874 : 429419 : if (!TYPE_NAME (type) && c_type_variably_modified_p (type))
7875 : : {
7876 : 373 : bool bind_p = decl_context == TYPENAME
7877 : : || decl_context == FIELD
7878 : : || decl_context == PARM;
7879 : 373 : add_decl_expr (loc, type, bind_p ? expr : NULL, true);
7880 : : }
7881 : :
7882 : 429419 : type = c_build_pointer_type (type);
7883 : 429419 : type_quals = array_ptr_quals;
7884 : 429419 : if (type_quals)
7885 : 932 : type = c_build_qualified_type (type, type_quals);
7886 : :
7887 : : /* We don't yet implement attributes in this context. */
7888 : 429419 : if (array_ptr_attrs != NULL_TREE)
7889 : 0 : warning_at (loc, OPT_Wattributes,
7890 : : "attributes in parameter array declarator ignored");
7891 : :
7892 : : size_varies = false;
7893 : : array_parameter_p = true;
7894 : : }
7895 : 120704558 : else if (TREE_CODE (type) == FUNCTION_TYPE)
7896 : : {
7897 : 301 : if (type_quals & TYPE_QUAL_ATOMIC)
7898 : : {
7899 : 1 : error_at (loc,
7900 : : "%<_Atomic%>-qualified function type");
7901 : 1 : type_quals &= ~TYPE_QUAL_ATOMIC;
7902 : : }
7903 : 300 : else if (type_quals)
7904 : 0 : pedwarn (loc, OPT_Wpedantic,
7905 : : "ISO C forbids qualified function types");
7906 : 1 : if (type_quals)
7907 : 0 : type = c_build_qualified_type (type, type_quals);
7908 : 301 : type = c_build_pointer_type (type);
7909 : 301 : type_quals = TYPE_UNQUALIFIED;
7910 : : }
7911 : 120704257 : else if (type_quals)
7912 : 9728354 : type = c_build_qualified_type (type, type_quals);
7913 : :
7914 : 242267954 : decl = build_decl (declarator->id_loc,
7915 : 121133977 : PARM_DECL, declarator->u.id.id, type);
7916 : 121133977 : if (size_varies)
7917 : 42 : C_DECL_VARIABLE_SIZE (decl) = 1;
7918 : 121133977 : C_ARRAY_PARAMETER (decl) = array_parameter_p;
7919 : :
7920 : : /* Compute the type actually passed in the parmlist,
7921 : : for the case where there is no prototype.
7922 : : (For example, shorts and chars are passed as ints.)
7923 : : When there is a prototype, this is overridden later. */
7924 : :
7925 : 121133977 : if (type == error_mark_node)
7926 : : promoted_type = type;
7927 : : else
7928 : 121133893 : promoted_type = c_type_promotes_to (type);
7929 : :
7930 : 121133977 : DECL_ARG_TYPE (decl) = promoted_type;
7931 : 121133977 : if (declspecs->inline_p)
7932 : 4 : pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
7933 : 121133977 : if (declspecs->noreturn_p)
7934 : 1 : pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
7935 : : }
7936 : 62813883 : else if (decl_context == FIELD)
7937 : : {
7938 : : /* Note that the grammar rejects storage classes in typenames
7939 : : and fields. */
7940 : 4231622 : gcc_assert (storage_class == csc_none && !threadp
7941 : : && !declspecs->inline_p && !declspecs->noreturn_p);
7942 : :
7943 : : /* Structure field. It may not be a function. */
7944 : :
7945 : 4231622 : if (TREE_CODE (type) == FUNCTION_TYPE)
7946 : : {
7947 : 0 : error_at (loc, "field %qE declared as a function", name);
7948 : 0 : type = c_build_pointer_type (type);
7949 : : }
7950 : 4231622 : else if (TREE_CODE (type) != ERROR_MARK
7951 : 4231622 : && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
7952 : : {
7953 : 24 : if (name)
7954 : 17 : error_at (loc, "field %qE has incomplete type", name);
7955 : : else
7956 : 7 : error_at (loc, "unnamed field has incomplete type");
7957 : 24 : type = error_mark_node;
7958 : : }
7959 : 4231598 : else if (TREE_CODE (type) == ARRAY_TYPE
7960 : 4231598 : && TYPE_DOMAIN (type) == NULL_TREE)
7961 : : {
7962 : : /* We have a flexible array member through a typedef.
7963 : : Set suitable range. Whether this is a correct position
7964 : : for a flexible array member will be determined elsewhere. */
7965 : 14 : if (!in_system_header_at (input_location))
7966 : 14 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7967 : : "support flexible array members");
7968 : 14 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7969 : 14 : TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
7970 : : NULL_TREE);
7971 : 14 : if (orig_qual_indirect == 0)
7972 : 4231622 : orig_qual_type = NULL_TREE;
7973 : : }
7974 : 4231622 : if (type != error_mark_node
7975 : 4231622 : && !verify_type_context (loc, TCTX_FIELD, type))
7976 : 0 : type = error_mark_node;
7977 : :
7978 : 4231622 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7979 : : orig_qual_indirect);
7980 : 8463244 : decl = build_decl (declarator->id_loc,
7981 : 4231622 : FIELD_DECL, declarator->u.id.id, type);
7982 : 4231622 : DECL_NONADDRESSABLE_P (decl) = bitfield;
7983 : 4231622 : if (bitfield && !declarator->u.id.id)
7984 : 9188 : DECL_PADDING_P (decl) = 1;
7985 : :
7986 : 4231622 : if (size_varies)
7987 : 646 : C_DECL_VARIABLE_SIZE (decl) = 1;
7988 : : }
7989 : 58582261 : else if (TREE_CODE (type) == FUNCTION_TYPE)
7990 : : {
7991 : 49836537 : if (storage_class == csc_register || threadp || constexprp)
7992 : : {
7993 : 12 : error_at (loc, "invalid storage class for function %qE", name);
7994 : : }
7995 : 49836525 : else if (current_scope != file_scope)
7996 : : {
7997 : : /* Function declaration not at file scope. Storage
7998 : : classes other than `extern' are not allowed, C99
7999 : : 6.7.1p5, and `extern' makes no difference. However,
8000 : : GCC allows 'auto', perhaps with 'inline', to support
8001 : : nested functions. */
8002 : 10528 : if (storage_class == csc_auto)
8003 : 66 : pedwarn (loc, OPT_Wpedantic,
8004 : : "invalid storage class for function %qE", name);
8005 : 10462 : else if (storage_class == csc_static)
8006 : : {
8007 : 20 : error_at (loc, "invalid storage class for function %qE", name);
8008 : 20 : if (funcdef_flag)
8009 : 8 : storage_class = declspecs->storage_class = csc_none;
8010 : : else
8011 : : return NULL_TREE;
8012 : : }
8013 : : }
8014 : :
8015 : 99673050 : decl = build_decl (declarator->id_loc,
8016 : 49836525 : FUNCTION_DECL, declarator->u.id.id, type);
8017 : 49836525 : decl = build_decl_attribute_variant (decl, decl_attr);
8018 : :
8019 : 49836525 : if (type_quals & TYPE_QUAL_ATOMIC)
8020 : : {
8021 : 2 : error_at (loc,
8022 : : "%<_Atomic%>-qualified function type");
8023 : 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
8024 : : }
8025 : 49836523 : else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
8026 : 9 : pedwarn (loc, OPT_Wpedantic,
8027 : : "ISO C forbids qualified function types");
8028 : :
8029 : : /* Every function declaration is an external reference
8030 : : (DECL_EXTERNAL) except for those which are not at file
8031 : : scope and are explicitly declared "auto". This is
8032 : : forbidden by standard C (C99 6.7.1p5) and is interpreted by
8033 : : GCC to signify a forward declaration of a nested function. */
8034 : 49836525 : if (storage_class == csc_auto && current_scope != file_scope)
8035 : 66 : DECL_EXTERNAL (decl) = 0;
8036 : : /* In C99, a function which is declared 'inline' with 'extern'
8037 : : is not an external reference (which is confusing). It
8038 : : means that the later definition of the function must be output
8039 : : in this file, C99 6.7.4p6. In GNU C89, a function declared
8040 : : 'extern inline' is an external reference. */
8041 : 49836459 : else if (declspecs->inline_p && storage_class != csc_static)
8042 : 34549267 : DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
8043 : 34549267 : == flag_gnu89_inline);
8044 : : else
8045 : 15287192 : DECL_EXTERNAL (decl) = !initialized;
8046 : :
8047 : : /* Record absence of global scope for `static' or `auto'. */
8048 : 49836525 : TREE_PUBLIC (decl)
8049 : 49836525 : = !(storage_class == csc_static || storage_class == csc_auto);
8050 : :
8051 : : /* For a function definition, record the argument information
8052 : : block where store_parm_decls will look for it. */
8053 : 49836525 : if (funcdef_flag)
8054 : 35380162 : current_function_arg_info = arg_info;
8055 : :
8056 : 49836525 : if (declspecs->default_int_p)
8057 : 9219 : C_FUNCTION_IMPLICIT_INT (decl) = 1;
8058 : :
8059 : : /* Record presence of `inline' and `_Noreturn', if it is
8060 : : reasonable. */
8061 : 49836525 : if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
8062 : : {
8063 : 47175 : if (declspecs->inline_p)
8064 : 5 : pedwarn (loc, 0, "cannot inline function %<main%>");
8065 : 47175 : if (declspecs->noreturn_p)
8066 : 1 : pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
8067 : : }
8068 : : else
8069 : : {
8070 : 49789350 : if (declspecs->inline_p)
8071 : : /* Record that the function is declared `inline'. */
8072 : 34705707 : DECL_DECLARED_INLINE_P (decl) = 1;
8073 : 49789350 : if (declspecs->noreturn_p)
8074 : : {
8075 : 22955 : if (flag_isoc99)
8076 : 22952 : pedwarn_c99 (loc, OPT_Wpedantic,
8077 : : "ISO C99 does not support %<_Noreturn%>");
8078 : : else
8079 : 3 : pedwarn_c99 (loc, OPT_Wpedantic,
8080 : : "ISO C90 does not support %<_Noreturn%>");
8081 : 22955 : TREE_THIS_VOLATILE (decl) = 1;
8082 : : }
8083 : : }
8084 : :
8085 : : /* C99 6.2.2p7: It is invalid (compile-time undefined
8086 : : behavior) to create an 'extern' declaration for a
8087 : : function if there is a global declaration that is
8088 : : 'static' and the global declaration is not visible.
8089 : : (If the static declaration _is_ currently visible,
8090 : : the 'extern' declaration is taken to refer to that decl.) */
8091 : 49836525 : if (!initialized
8092 : 14456354 : && TREE_PUBLIC (decl)
8093 : 14448685 : && current_scope != file_scope)
8094 : : {
8095 : 8874 : tree global_decl = identifier_global_value (declarator->u.id.id);
8096 : 8874 : tree visible_decl = lookup_name (declarator->u.id.id);
8097 : :
8098 : 8874 : if (global_decl
8099 : 8874 : && global_decl != visible_decl
8100 : 1713 : && VAR_OR_FUNCTION_DECL_P (global_decl)
8101 : 1713 : && !TREE_PUBLIC (global_decl))
8102 : 2 : error_at (loc, "function previously declared %<static%> "
8103 : : "redeclared %<extern%>");
8104 : : }
8105 : : }
8106 : : else
8107 : : {
8108 : : /* It's a variable. */
8109 : : /* An uninitialized decl with `extern' is a reference. */
8110 : 8745724 : int extern_ref = !initialized && storage_class == csc_extern;
8111 : :
8112 : 8745724 : if (constexprp)
8113 : : {
8114 : : /* The type of a constexpr variable must not be variably
8115 : : modified, volatile, atomic or restrict qualified or
8116 : : have a member with such a qualifier. const
8117 : : qualification is implicitly added, and, at file scope,
8118 : : has internal linkage. */
8119 : 352 : if (c_type_variably_modified_p (type))
8120 : 1 : error_at (loc, "%<constexpr%> object has variably modified "
8121 : : "type");
8122 : 352 : if (type_quals
8123 : 352 : & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
8124 : 9 : error_at (loc, "invalid qualifiers for %<constexpr%> object");
8125 : : else
8126 : : {
8127 : 343 : tree type_no_array = strip_array_types (type);
8128 : 343 : if (RECORD_OR_UNION_TYPE_P (type_no_array)
8129 : 343 : && C_TYPE_FIELDS_NON_CONSTEXPR (type_no_array))
8130 : 8 : error_at (loc, "invalid qualifiers for field of "
8131 : : "%<constexpr%> object");
8132 : : }
8133 : 352 : type_quals |= TYPE_QUAL_CONST;
8134 : 352 : if (current_scope == file_scope)
8135 : 294 : storage_class = csc_static;
8136 : : }
8137 : :
8138 : 8745724 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
8139 : : orig_qual_indirect);
8140 : :
8141 : : /* C99 6.2.2p7: It is invalid (compile-time undefined
8142 : : behavior) to create an 'extern' declaration for a
8143 : : variable if there is a global declaration that is
8144 : : 'static' and the global declaration is not visible.
8145 : : (If the static declaration _is_ currently visible,
8146 : : the 'extern' declaration is taken to refer to that decl.) */
8147 : 8745724 : if (extern_ref && current_scope != file_scope)
8148 : : {
8149 : 1569 : tree global_decl = identifier_global_value (declarator->u.id.id);
8150 : 1569 : tree visible_decl = lookup_name (declarator->u.id.id);
8151 : :
8152 : 1569 : if (global_decl
8153 : 1569 : && global_decl != visible_decl
8154 : 282 : && VAR_P (global_decl)
8155 : 282 : && !TREE_PUBLIC (global_decl))
8156 : 8 : error_at (loc, "variable previously declared %<static%> "
8157 : : "redeclared %<extern%>");
8158 : : }
8159 : :
8160 : 17491448 : decl = build_decl (declarator->id_loc,
8161 : 8745724 : VAR_DECL, declarator->u.id.id, type);
8162 : 8745724 : if (size_varies)
8163 : 7407 : C_DECL_VARIABLE_SIZE (decl) = 1;
8164 : 8745724 : if (constexprp)
8165 : 352 : C_DECL_DECLARED_CONSTEXPR (decl) = 1;
8166 : :
8167 : 8745724 : if (declspecs->inline_p)
8168 : 4 : pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
8169 : 8745724 : if (declspecs->noreturn_p)
8170 : 1 : pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
8171 : :
8172 : : /* At file scope, an initialized extern declaration may follow
8173 : : a static declaration. In that case, DECL_EXTERNAL will be
8174 : : reset later in start_decl. */
8175 : 8745724 : DECL_EXTERNAL (decl) = (storage_class == csc_extern);
8176 : :
8177 : : /* At file scope, the presence of a `static' or `register' storage
8178 : : class specifier, or the absence of all storage class specifiers
8179 : : makes this declaration a definition (perhaps tentative). Also,
8180 : : the absence of `static' makes it public. */
8181 : 8745724 : if (current_scope == file_scope)
8182 : : {
8183 : 1149661 : TREE_PUBLIC (decl) = storage_class != csc_static;
8184 : 1149661 : TREE_STATIC (decl) = !extern_ref;
8185 : : }
8186 : : /* Not at file scope, only `static' makes a static definition. */
8187 : : else
8188 : : {
8189 : 7596063 : TREE_STATIC (decl) = (storage_class == csc_static);
8190 : 7596063 : TREE_PUBLIC (decl) = extern_ref;
8191 : : }
8192 : :
8193 : : // NB: Set a tentative TLS model to avoid tls_model attribute
8194 : : // warnings due to lack of thread storage duration. It will
8195 : : // be updated by c_decl_attributes later.
8196 : 8745724 : if (threadp)
8197 : 2812 : set_decl_tls_model (decl, TLS_MODEL_REAL);
8198 : : }
8199 : :
8200 : 183947848 : if ((storage_class == csc_extern
8201 : 135083453 : || (storage_class == csc_none
8202 : 134635256 : && TREE_CODE (type) == FUNCTION_TYPE
8203 : 796881 : && !funcdef_flag))
8204 : 184251589 : && c_type_variably_modified_p (type))
8205 : : {
8206 : : /* C99 6.7.5.2p2 */
8207 : 6 : if (TREE_CODE (type) == FUNCTION_TYPE)
8208 : 4 : error_at (loc, "non-nested function with variably modified type");
8209 : : else
8210 : 2 : error_at (loc, "object with variably modified type must have "
8211 : : "no linkage");
8212 : : }
8213 : :
8214 : : /* For nested functions disqualify ones taking VLAs by value
8215 : : from inlining since the middle-end cannot deal with this.
8216 : : ??? We should arrange for those to be passed by reference
8217 : : with emitting the copy on the caller side in the frontend. */
8218 : 183947848 : if (storage_class == csc_none
8219 : 134635256 : && TREE_CODE (type) == FUNCTION_TYPE)
8220 : 4145440 : for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
8221 : : {
8222 : 3348599 : tree arg = TREE_VALUE (al);
8223 : 3348599 : if (arg != error_mark_node
8224 : 3348599 : && C_TYPE_VARIABLE_SIZE (arg))
8225 : : {
8226 : 40 : DECL_UNINLINABLE (decl) = 1;
8227 : 40 : break;
8228 : : }
8229 : : }
8230 : :
8231 : : /* Record `register' declaration for warnings on &
8232 : : and in case doing stupid register allocation. */
8233 : :
8234 : 183947848 : if (storage_class == csc_register
8235 : 3593 : && TREE_CODE (type) != FUNCTION_TYPE)
8236 : : {
8237 : 3587 : C_DECL_REGISTER (decl) = 1;
8238 : 3587 : DECL_REGISTER (decl) = 1;
8239 : : }
8240 : :
8241 : : /* Record constancy and volatility. */
8242 : 183947848 : c_apply_type_quals_to_decl (type_quals, decl);
8243 : :
8244 : : /* Apply _Alignas specifiers. */
8245 : 183947848 : if (alignas_align)
8246 : : {
8247 : 123 : SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
8248 : 123 : DECL_USER_ALIGN (decl) = 1;
8249 : : }
8250 : :
8251 : : /* If a type has volatile components, it should be stored in memory.
8252 : : Otherwise, the fact that those components are volatile
8253 : : will be ignored, and would even crash the compiler.
8254 : : Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
8255 : 183947848 : if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
8256 : 183947848 : && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
8257 : : || TREE_CODE (decl) == RESULT_DECL))
8258 : : {
8259 : : /* It is not an error for a structure with volatile fields to
8260 : : be declared register, but reset DECL_REGISTER since it
8261 : : cannot actually go in a register. */
8262 : 184 : int was_reg = C_DECL_REGISTER (decl);
8263 : 184 : C_DECL_REGISTER (decl) = 0;
8264 : 184 : DECL_REGISTER (decl) = 0;
8265 : 184 : c_mark_addressable (decl);
8266 : 184 : C_DECL_REGISTER (decl) = was_reg;
8267 : : }
8268 : :
8269 : : /* This is the earliest point at which we might know the assembler
8270 : : name of a variable. Thus, if it's known before this, die horribly. */
8271 : 183947848 : gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
8272 : : || !DECL_ASSEMBLER_NAME_SET_P (decl));
8273 : :
8274 : 183947848 : if (warn_cxx_compat
8275 : 24507 : && VAR_P (decl)
8276 : 7541 : && TREE_PUBLIC (decl)
8277 : 2798 : && TREE_STATIC (decl)
8278 : 2351 : && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
8279 : 2219 : || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
8280 : 183947995 : && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
8281 : 4 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
8282 : : "non-local variable %qD with anonymous type is "
8283 : : "questionable in C++", decl);
8284 : :
8285 : : return decl;
8286 : : }
8287 : : }
8288 : :
8289 : : /* Decode the parameter-list info for a function type or function definition.
8290 : : The argument is the value returned by `get_parm_info' (or made in c-parse.c
8291 : : if there is an identifier list instead of a parameter decl list).
8292 : : These two functions are separate because when a function returns
8293 : : or receives functions then each is called multiple times but the order
8294 : : of calls is different. The last call to `grokparms' is always the one
8295 : : that contains the formal parameter names of a function definition.
8296 : :
8297 : : Return a list of arg types to use in the FUNCTION_TYPE for this function.
8298 : :
8299 : : FUNCDEF_FLAG is true for a function definition, false for
8300 : : a mere declaration. A nonempty identifier-list gets an error message
8301 : : when FUNCDEF_FLAG is false. */
8302 : :
8303 : : static tree
8304 : 49417426 : grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
8305 : : {
8306 : 49417426 : tree arg_types = arg_info->types;
8307 : :
8308 : 49417426 : if (funcdef_flag && arg_info->had_vla_unspec)
8309 : : {
8310 : : /* A function definition isn't function prototype scope C99 6.2.1p4. */
8311 : : /* C99 6.7.5.2p4 */
8312 : 2 : error ("%<[*]%> not allowed in other than function prototype scope");
8313 : : }
8314 : :
8315 : 715265 : if (arg_types == NULL_TREE && !funcdef_flag && !flag_isoc23
8316 : 49420451 : && !in_system_header_at (input_location))
8317 : 3025 : warning (OPT_Wstrict_prototypes,
8318 : : "function declaration isn%'t a prototype");
8319 : :
8320 : 49417426 : if (arg_types == error_mark_node)
8321 : : /* Don't set TYPE_ARG_TYPES in this case. */
8322 : : return NULL_TREE;
8323 : :
8324 : 98063788 : else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
8325 : : {
8326 : 8684 : if (!funcdef_flag)
8327 : : {
8328 : 13 : permerror_opt (input_location,
8329 : 13 : OPT_Wdeclaration_missing_parameter_type,
8330 : : "parameter names (without types) in "
8331 : : "function declaration");
8332 : 13 : arg_info->parms = NULL_TREE;
8333 : : }
8334 : : else
8335 : 8671 : arg_info->parms = arg_info->types;
8336 : :
8337 : 8684 : arg_info->types = NULL_TREE;
8338 : 8684 : return NULL_TREE;
8339 : : }
8340 : : else
8341 : : {
8342 : 49408742 : tree parm, type, typelt;
8343 : 49408742 : unsigned int parmno;
8344 : :
8345 : : /* In C23, convert () to (void). */
8346 : 49408742 : if (flag_isoc23
8347 : 40381288 : && !arg_types
8348 : 763609 : && !arg_info->parms
8349 : 763609 : && !arg_info->no_named_args_stdarg_p)
8350 : : {
8351 : 763468 : arg_types = arg_info->types = void_list_node;
8352 : 763468 : arg_info->c23_empty_parens = 1;
8353 : : }
8354 : :
8355 : : /* If there is a parameter of incomplete type in a definition,
8356 : : this is an error. In a declaration this is valid, and a
8357 : : struct or union type may be completed later, before any calls
8358 : : or definition of the function. In the case where the tag was
8359 : : first declared within the parameter list, a warning has
8360 : : already been given. If a parameter has void type, then
8361 : : this has already received an error (constraint violation in C2Y,
8362 : : previously implicitly undefined behavior). */
8363 : :
8364 : 49408742 : for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
8365 : 169585657 : parm;
8366 : 120176915 : parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
8367 : : {
8368 : 120176915 : type = TREE_VALUE (typelt);
8369 : 120176915 : if (type == error_mark_node)
8370 : 61 : continue;
8371 : :
8372 : 120176854 : if (!COMPLETE_TYPE_P (type))
8373 : : {
8374 : 40 : if (funcdef_flag)
8375 : : {
8376 : 13 : if (DECL_NAME (parm))
8377 : 13 : error_at (input_location,
8378 : : "parameter %u (%q+D) has incomplete type",
8379 : : parmno, parm);
8380 : : else
8381 : 0 : error_at (DECL_SOURCE_LOCATION (parm),
8382 : : "parameter %u has incomplete type",
8383 : : parmno);
8384 : :
8385 : 13 : TREE_VALUE (typelt) = error_mark_node;
8386 : 13 : TREE_TYPE (parm) = error_mark_node;
8387 : 13 : arg_types = NULL_TREE;
8388 : : }
8389 : : }
8390 : :
8391 : 120176854 : if (DECL_NAME (parm) && TREE_USED (parm))
8392 : 17501 : warn_if_shadowing (parm);
8393 : : }
8394 : : return arg_types;
8395 : : }
8396 : : }
8397 : :
8398 : : /* Allocate and initialize a c_arg_info structure from the parser's
8399 : : obstack. */
8400 : :
8401 : : struct c_arg_info *
8402 : 49417444 : build_arg_info (void)
8403 : : {
8404 : 49417444 : struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
8405 : 49417444 : ret->parms = NULL_TREE;
8406 : 49417444 : ret->tags = NULL;
8407 : 49417444 : ret->types = NULL_TREE;
8408 : 49417444 : ret->others = NULL_TREE;
8409 : 49417444 : ret->pending_sizes = NULL;
8410 : 49417444 : ret->had_vla_unspec = 0;
8411 : 49417444 : ret->no_named_args_stdarg_p = 0;
8412 : 49417444 : ret->c23_empty_parens = 0;
8413 : 49417444 : return ret;
8414 : : }
8415 : :
8416 : : /* Take apart the current scope and return a c_arg_info structure with
8417 : : info on a parameter list just parsed.
8418 : :
8419 : : This structure is later fed to 'grokparms' and 'store_parm_decls'.
8420 : :
8421 : : ELLIPSIS being true means the argument list ended in '...' so don't
8422 : : append a sentinel (void_list_node) to the end of the type-list.
8423 : :
8424 : : EXPR is NULL or an expression that needs to be evaluated for the
8425 : : side effects of array size expressions in the parameters. */
8426 : :
8427 : : struct c_arg_info *
8428 : 48637707 : get_parm_info (bool ellipsis, tree expr)
8429 : : {
8430 : 48637707 : struct c_binding *b = current_scope->bindings;
8431 : 48637707 : struct c_arg_info *arg_info = build_arg_info ();
8432 : :
8433 : 48637707 : tree parms = NULL_TREE;
8434 : 48637707 : vec<c_arg_tag, va_gc> *tags = NULL;
8435 : 48637707 : tree types = NULL_TREE;
8436 : 48637707 : tree others = NULL_TREE;
8437 : :
8438 : 48637707 : bool gave_void_only_once_err = false;
8439 : :
8440 : 48637707 : arg_info->had_vla_unspec = current_scope->had_vla_unspec;
8441 : :
8442 : : /* The bindings in this scope must not get put into a block.
8443 : : We will take care of deleting the binding nodes. */
8444 : 48637707 : current_scope->bindings = 0;
8445 : :
8446 : : /* This function is only called if there was *something* on the
8447 : : parameter list. */
8448 : 48637707 : gcc_assert (b);
8449 : :
8450 : : /* A parameter list consisting solely of 'void' indicates that the
8451 : : function takes no arguments. But if the 'void' is qualified
8452 : : (by 'const' or 'volatile'), or has a storage class specifier
8453 : : ('register'), then the behavior is undefined; issue an error.
8454 : : Typedefs for 'void' are OK (see DR#157). */
8455 : 48637707 : if (b->prev == 0 /* one binding */
8456 : 12983354 : && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
8457 : 12983354 : && !DECL_NAME (b->decl) /* anonymous */
8458 : 50415204 : && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
8459 : : {
8460 : 934126 : if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
8461 : 934126 : || C_DECL_REGISTER (b->decl))
8462 : 96 : error_at (b->locus, "%<void%> as only parameter may not be qualified");
8463 : :
8464 : : /* There cannot be an ellipsis. */
8465 : 934126 : if (ellipsis)
8466 : 17 : error_at (b->locus, "%<void%> must be the only parameter");
8467 : :
8468 : 934126 : arg_info->types = void_list_node;
8469 : 934126 : return arg_info;
8470 : : }
8471 : :
8472 : 47703581 : if (!ellipsis)
8473 : 47493427 : types = void_list_node;
8474 : :
8475 : : /* Break up the bindings list into parms, tags, types, and others;
8476 : : apply sanity checks; purge the name-to-decl bindings. */
8477 : 167880943 : while (b)
8478 : : {
8479 : 120177362 : tree decl = b->decl;
8480 : 120177362 : tree type = TREE_TYPE (decl);
8481 : 120177362 : c_arg_tag tag;
8482 : 120177362 : const char *keyword;
8483 : :
8484 : 120177362 : switch (TREE_CODE (decl))
8485 : : {
8486 : 120177038 : case PARM_DECL:
8487 : 120177038 : if (b->id)
8488 : : {
8489 : 116628598 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8490 : 116628598 : I_SYMBOL_BINDING (b->id) = b->shadowed;
8491 : : }
8492 : :
8493 : : /* Check for forward decls that never got their actual decl. */
8494 : 120177038 : if (TREE_ASM_WRITTEN (decl))
8495 : 4 : error_at (b->locus,
8496 : : "parameter %q+D has just a forward declaration", decl);
8497 : : /* Check for (..., void, ...) and named void parameters and issue an
8498 : : error. */
8499 : 120177034 : else if (VOID_TYPE_P (type))
8500 : : {
8501 : 114 : if (!gave_void_only_once_err)
8502 : : {
8503 : 114 : error_at (b->locus,
8504 : : "%<void%> must be the only parameter and unnamed");
8505 : 114 : gave_void_only_once_err = true;
8506 : : }
8507 : : }
8508 : : else
8509 : : {
8510 : : /* Valid parameter, add it to the list. */
8511 : 120176920 : DECL_CHAIN (decl) = parms;
8512 : 120176920 : parms = decl;
8513 : :
8514 : : /* Since there is a prototype, args are passed in their
8515 : : declared types. The back end may override this later. */
8516 : 120176920 : DECL_ARG_TYPE (decl) = type;
8517 : 120176920 : types = tree_cons (0, type, types);
8518 : : }
8519 : : break;
8520 : :
8521 : 21 : case ENUMERAL_TYPE: keyword = "enum"; goto tag;
8522 : 19 : case UNION_TYPE: keyword = "union"; goto tag;
8523 : 94 : case RECORD_TYPE: keyword = "struct"; goto tag;
8524 : 134 : tag:
8525 : : /* Types may not have tag-names, in which case the type
8526 : : appears in the bindings list with b->id NULL. */
8527 : 134 : if (b->id)
8528 : : {
8529 : 92 : gcc_assert (I_TAG_BINDING (b->id) == b);
8530 : 92 : I_TAG_BINDING (b->id) = b->shadowed;
8531 : : }
8532 : :
8533 : : /* Warn about any struct, union or enum tags defined in a
8534 : : parameter list. The scope of such types is limited to
8535 : : the parameter list, which is rarely if ever desirable
8536 : : (it's impossible to call such a function with type-
8537 : : correct arguments). An anonymous union parm type is
8538 : : meaningful as a GNU extension, so don't warn for that. */
8539 : 134 : if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
8540 : : {
8541 : 115 : if (b->id)
8542 : : {
8543 : : /* The %s will be one of 'struct', 'union', or 'enum'. */
8544 : 92 : if (!flag_isoc23 || !COMPLETE_TYPE_P (decl))
8545 : 72 : warning_at (b->locus, 0,
8546 : : "%<%s %E%> declared inside parameter list"
8547 : : " will not be visible outside of this definition or"
8548 : : " declaration", keyword, b->id);
8549 : : }
8550 : : else
8551 : : /* The %s will be one of 'struct', 'union', or 'enum'. */
8552 : 23 : warning_at (b->locus, 0,
8553 : : "anonymous %s declared inside parameter list"
8554 : : " will not be visible outside of this definition or"
8555 : : " declaration", keyword);
8556 : : }
8557 : :
8558 : 134 : tag.id = b->id;
8559 : 134 : tag.type = decl;
8560 : 134 : vec_safe_push (tags, tag);
8561 : 134 : break;
8562 : :
8563 : 20 : case FUNCTION_DECL:
8564 : : /* FUNCTION_DECLs appear when there is an implicit function
8565 : : declaration in the parameter list. */
8566 : 20 : gcc_assert (b->nested || seen_error ());
8567 : 20 : goto set_shadowed;
8568 : :
8569 : 136 : case CONST_DECL:
8570 : 136 : case TYPE_DECL:
8571 : : /* CONST_DECLs appear here when we have an embedded enum,
8572 : : and TYPE_DECLs appear here when we have an embedded struct
8573 : : or union. No warnings for this - we already warned about the
8574 : : type itself. */
8575 : :
8576 : : /* When we reinsert this decl in the function body, we need
8577 : : to reconstruct whether it was marked as nested. */
8578 : 136 : gcc_assert (!b->nested);
8579 : 136 : DECL_CHAIN (decl) = others;
8580 : 136 : others = decl;
8581 : : /* fall through */
8582 : :
8583 : 190 : case ERROR_MARK:
8584 : 190 : set_shadowed:
8585 : : /* error_mark_node appears here when we have an undeclared
8586 : : variable. Just throw it away. */
8587 : 190 : if (b->id)
8588 : : {
8589 : 56 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8590 : 56 : I_SYMBOL_BINDING (b->id) = b->shadowed;
8591 : : }
8592 : : break;
8593 : :
8594 : : /* Other things that might be encountered. */
8595 : 0 : case LABEL_DECL:
8596 : 0 : case VAR_DECL:
8597 : 0 : default:
8598 : 0 : gcc_unreachable ();
8599 : : }
8600 : :
8601 : 120177362 : b = free_binding_and_advance (b);
8602 : : }
8603 : :
8604 : 47703581 : arg_info->parms = parms;
8605 : 47703581 : arg_info->tags = tags;
8606 : 47703581 : arg_info->types = types;
8607 : 47703581 : arg_info->others = others;
8608 : 47703581 : arg_info->pending_sizes = expr;
8609 : 47703581 : arg_info->no_named_args_stdarg_p = ellipsis && !types;
8610 : 47703581 : return arg_info;
8611 : : }
8612 : :
8613 : : /* Get the struct, enum or union (CODE says which) with tag NAME.
8614 : : Define the tag as a forward-reference with location LOC if it is
8615 : : not defined. HAVE_STD_ATTRS says whether any standard attributes
8616 : : were present after the struct, union or enum keyword; ATTRS are the
8617 : : standard attributes present there. HAS_ENUM_TYPE_SPECIFIER says
8618 : : whether an enum type specifier (": specifier-qualifier-list") is
8619 : : present; if so, this is called before that specifier is parsed, so
8620 : : that the tag is in scope for that specifier. Return a c_typespec
8621 : : structure for the type specifier. */
8622 : :
8623 : : struct c_typespec
8624 : 1065027 : parser_xref_tag (location_t loc, enum tree_code code, tree name,
8625 : : bool have_std_attrs, tree attrs, bool has_enum_type_specifier)
8626 : : {
8627 : 1065027 : struct c_typespec ret;
8628 : 1065027 : tree ref;
8629 : 1065027 : location_t refloc;
8630 : :
8631 : 1065027 : ret.expr = NULL_TREE;
8632 : 1065027 : ret.expr_const_operands = true;
8633 : 1065027 : ret.has_enum_type_specifier = has_enum_type_specifier;
8634 : :
8635 : : /* If a cross reference is requested, look up the type already
8636 : : defined for this tag and return it. If an enum type specifier is
8637 : : present, only a definition in the current scope is relevant. */
8638 : :
8639 : 1065027 : ref = lookup_tag (code, name, has_enum_type_specifier, &refloc);
8640 : :
8641 : : /* If the visble type is still being defined, see if there is
8642 : : an earlier definition (which may be complete). We do not
8643 : : have to loop because nested redefinitions are not allowed. */
8644 : 1065027 : if (flag_isoc23 && ref && C_TYPE_BEING_DEFINED (ref))
8645 : : {
8646 : 84027 : tree vis = previous_tag (ref);
8647 : 84027 : if (vis)
8648 : 16 : ref = vis;
8649 : : }
8650 : :
8651 : : /* If this is the right type of tag, return what we found.
8652 : : (This reference will be shadowed by shadow_tag later if appropriate.)
8653 : : If this is the wrong type of tag, do not return it. If it was the
8654 : : wrong type in the same scope, we will have had an error
8655 : : message already; if in a different scope and declaring
8656 : : a name, pending_xref_error will give an error message; but if in a
8657 : : different scope and not declaring a name, this tag should
8658 : : shadow the previous declaration of a different type of tag, and
8659 : : this would not work properly if we return the reference found.
8660 : : (For example, with "struct foo" in an outer scope, "union foo;"
8661 : : must shadow that tag with a new one of union type.) */
8662 : 2130054 : ret.kind = (ref
8663 : 1065027 : ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
8664 : 92762 : : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
8665 : 1065027 : if (ref && TREE_CODE (ref) == code)
8666 : : {
8667 : 972231 : decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8668 : 972231 : if (C_TYPE_DEFINED_IN_STRUCT (ref)
8669 : 5 : && loc != UNKNOWN_LOCATION
8670 : 972236 : && warn_cxx_compat)
8671 : : {
8672 : 5 : auto_diagnostic_group d;
8673 : 5 : switch (code)
8674 : : {
8675 : 2 : case ENUMERAL_TYPE:
8676 : 2 : if (warning_at (loc, OPT_Wc___compat,
8677 : : ("enum type defined in struct or union "
8678 : : "is not visible in C++")))
8679 : 2 : inform (refloc, "enum type defined here");
8680 : : break;
8681 : 2 : case RECORD_TYPE:
8682 : 2 : if (warning_at (loc, OPT_Wc___compat,
8683 : : ("struct defined in struct or union "
8684 : : "is not visible in C++")))
8685 : 2 : inform (refloc, "struct defined here");
8686 : : break;
8687 : 1 : case UNION_TYPE:
8688 : 1 : if (warning_at (loc, OPT_Wc___compat,
8689 : : ("union defined in struct or union "
8690 : : "is not visible in C++")))
8691 : 1 : inform (refloc, "union defined here");
8692 : : break;
8693 : 0 : default:
8694 : 0 : gcc_unreachable();
8695 : : }
8696 : 5 : }
8697 : :
8698 : 972231 : ret.spec = ref;
8699 : 972231 : return ret;
8700 : : }
8701 : :
8702 : : /* If no such tag is yet defined, create a forward-reference node
8703 : : and record it as the "definition".
8704 : : When a real declaration of this type is found,
8705 : : the forward-reference will be altered into a real type. */
8706 : :
8707 : 92796 : ref = make_node (code);
8708 : 92796 : if (flag_isoc23 || code == ENUMERAL_TYPE)
8709 : 70610 : SET_TYPE_STRUCTURAL_EQUALITY (ref);
8710 : 70610 : if (code == ENUMERAL_TYPE)
8711 : : {
8712 : : /* Give the type a default layout like unsigned int
8713 : : to avoid crashing if it does not get defined. */
8714 : 249 : SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
8715 : 249 : SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
8716 : 249 : TYPE_USER_ALIGN (ref) = 0;
8717 : 249 : TYPE_UNSIGNED (ref) = 1;
8718 : 249 : TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
8719 : 249 : TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
8720 : 249 : TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
8721 : 249 : ENUM_FIXED_UNDERLYING_TYPE_P (ref) = has_enum_type_specifier;
8722 : : }
8723 : :
8724 : 92796 : pushtag (loc, name, ref);
8725 : 92796 : decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8726 : 92796 : if (in_underspecified_init)
8727 : 9 : error_at (loc, "%qT declared in underspecified object initializer",
8728 : : ref);
8729 : :
8730 : 92796 : ret.spec = ref;
8731 : 92796 : return ret;
8732 : : }
8733 : :
8734 : : /* Get the struct, enum or union (CODE says which) with tag NAME.
8735 : : Define the tag as a forward-reference if it is not defined.
8736 : : Return a tree for the type. */
8737 : :
8738 : : tree
8739 : 0 : xref_tag (enum tree_code code, tree name)
8740 : : {
8741 : 0 : return parser_xref_tag (input_location, code, name, false, NULL_TREE,
8742 : 0 : false).spec;
8743 : : }
8744 : :
8745 : : /* Make sure that the tag NAME is defined *in the current scope*
8746 : : at least as a forward reference.
8747 : : LOC is the location of the struct's definition.
8748 : : CODE says which kind of tag NAME ought to be.
8749 : :
8750 : : This stores the current value of the file static STRUCT_PARSE_INFO
8751 : : in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8752 : : new c_struct_parse_info structure. The old value of
8753 : : STRUCT_PARSE_INFO is restored in finish_struct. */
8754 : :
8755 : : tree
8756 : 1152807 : start_struct (location_t loc, enum tree_code code, tree name,
8757 : : class c_struct_parse_info **enclosing_struct_parse_info)
8758 : : {
8759 : : /* If there is already a tag defined at this scope
8760 : : (as a forward reference), just return it. */
8761 : :
8762 : 1152807 : tree ref = NULL_TREE;
8763 : 1152807 : location_t refloc = UNKNOWN_LOCATION;
8764 : :
8765 : 1152807 : if (name != NULL_TREE)
8766 : 475852 : ref = lookup_tag (code, name, true, &refloc);
8767 : :
8768 : : /* For C23, even if we already have a completed definition,
8769 : : we do not use it. We will check for consistency later.
8770 : : If we are in a nested redefinition the type is not
8771 : : complete. We will then detect this below. */
8772 : 1152807 : if (flag_isoc23 && ref && TYPE_SIZE (ref))
8773 : : ref = NULL_TREE;
8774 : :
8775 : 1152702 : if (ref && TREE_CODE (ref) == code)
8776 : : {
8777 : 24678 : if (TYPE_STUB_DECL (ref))
8778 : 24678 : refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8779 : :
8780 : 24678 : if (TYPE_SIZE (ref))
8781 : : {
8782 : 11 : auto_diagnostic_group d;
8783 : 11 : if (code == UNION_TYPE)
8784 : 2 : error_at (loc, "redefinition of %<union %E%>", name);
8785 : : else
8786 : 9 : error_at (loc, "redefinition of %<struct %E%>", name);
8787 : 11 : if (refloc != UNKNOWN_LOCATION)
8788 : 11 : inform (refloc, "originally defined here");
8789 : : /* Don't create structures using a name already in use. */
8790 : 11 : ref = NULL_TREE;
8791 : 11 : }
8792 : 24667 : else if (C_TYPE_BEING_DEFINED (ref))
8793 : : {
8794 : 17 : if (code == UNION_TYPE)
8795 : 1 : error_at (loc, "nested redefinition of %<union %E%>", name);
8796 : : else
8797 : 16 : error_at (loc, "nested redefinition of %<struct %E%>", name);
8798 : : /* Don't bother to report "originally defined here" for a
8799 : : nested redefinition; the original definition should be
8800 : : obvious. */
8801 : : /* Don't create structures that contain themselves. */
8802 : : ref = NULL_TREE;
8803 : : }
8804 : : }
8805 : :
8806 : : /* Otherwise create a forward-reference just so the tag is in scope. */
8807 : :
8808 : 24664 : if (ref == NULL_TREE || TREE_CODE (ref) != code)
8809 : : {
8810 : 1128157 : ref = make_node (code);
8811 : 1128157 : if (flag_isoc23)
8812 : 900095 : SET_TYPE_STRUCTURAL_EQUALITY (ref);
8813 : 1128157 : pushtag (loc, name, ref);
8814 : : }
8815 : :
8816 : 1152807 : C_TYPE_BEING_DEFINED (ref) = 1;
8817 : 2335676 : for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
8818 : 1182869 : TYPE_PACKED (v) = flag_pack_struct;
8819 : :
8820 : 1152807 : *enclosing_struct_parse_info = struct_parse_info;
8821 : 1152807 : struct_parse_info = new c_struct_parse_info ();
8822 : 1152807 : struct_parse_info->refloc = refloc;
8823 : :
8824 : : /* FIXME: This will issue a warning for a use of a type defined
8825 : : within a statement expr used within sizeof, et. al. This is not
8826 : : terribly serious as C++ doesn't permit statement exprs within
8827 : : sizeof anyhow. */
8828 : 1152807 : if (warn_cxx_compat
8829 : 574 : && (in_sizeof || in_typeof || in_alignof || in_countof))
8830 : 10 : warning_at (loc, OPT_Wc___compat,
8831 : : "defining type in %qs expression is invalid in C++",
8832 : : (in_sizeof ? "sizeof"
8833 : 4 : : in_typeof ? "typeof"
8834 : 1 : : in_alignof ? "alignof"
8835 : : : "_Countof"));
8836 : :
8837 : 1152807 : if (in_underspecified_init)
8838 : 18 : error_at (loc, "%qT defined in underspecified object initializer", ref);
8839 : :
8840 : 1152807 : return ref;
8841 : : }
8842 : :
8843 : : /* Process the specs, declarator and width (NULL if omitted)
8844 : : of a structure component, returning a FIELD_DECL node.
8845 : : WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
8846 : : DECL_ATTRS is as for grokdeclarator.
8847 : :
8848 : : LOC is the location of the structure component.
8849 : :
8850 : : This is done during the parsing of the struct declaration.
8851 : : The FIELD_DECL nodes are chained together and the lot of them
8852 : : are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
8853 : :
8854 : : tree
8855 : 4231651 : grokfield (location_t loc,
8856 : : struct c_declarator *declarator, struct c_declspecs *declspecs,
8857 : : tree width, tree *decl_attrs, tree *expr)
8858 : : {
8859 : 4231651 : tree value;
8860 : :
8861 : 4231651 : if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
8862 : 19749 : && width == NULL_TREE)
8863 : : {
8864 : : /* This is an unnamed decl.
8865 : :
8866 : : If we have something of the form "union { list } ;" then this
8867 : : is the anonymous union extension. Similarly for struct.
8868 : :
8869 : : If this is something of the form "struct foo;", then
8870 : : If MS or Plan 9 extensions are enabled, this is handled as
8871 : : an anonymous struct.
8872 : : Otherwise this is a forward declaration of a structure tag.
8873 : :
8874 : : If this is something of the form "foo;" and foo is a TYPE_DECL, then
8875 : : If foo names a structure or union without a tag, then this
8876 : : is an anonymous struct (this is permitted by C11).
8877 : : If MS or Plan 9 extensions are enabled and foo names a
8878 : : structure, then again this is an anonymous struct.
8879 : : Otherwise this is an error.
8880 : :
8881 : : Oh what a horrid tangled web we weave. I wonder if MS consciously
8882 : : took this from Plan 9 or if it was an accident of implementation
8883 : : that took root before someone noticed the bug... */
8884 : :
8885 : 10561 : tree type = declspecs->type;
8886 : 10561 : bool ok = false;
8887 : :
8888 : 10561 : if (RECORD_OR_UNION_TYPE_P (type)
8889 : 10555 : && (flag_ms_extensions
8890 : 10545 : || flag_plan9_extensions
8891 : 10530 : || !declspecs->typedef_p))
8892 : : {
8893 : 10548 : if (flag_ms_extensions || flag_plan9_extensions)
8894 : : ok = true;
8895 : 10523 : else if (TYPE_NAME (type) == NULL)
8896 : : ok = true;
8897 : : else
8898 : : ok = false;
8899 : : }
8900 : : if (!ok)
8901 : : {
8902 : 29 : pedwarn (loc, 0, "declaration does not declare anything");
8903 : 29 : return NULL_TREE;
8904 : : }
8905 : 10532 : if (flag_isoc99)
8906 : 10510 : pedwarn_c99 (loc, OPT_Wpedantic,
8907 : : "ISO C99 doesn%'t support unnamed structs/unions");
8908 : : else
8909 : 22 : pedwarn_c99 (loc, OPT_Wpedantic,
8910 : : "ISO C90 doesn%'t support unnamed structs/unions");
8911 : : }
8912 : :
8913 : 4231622 : value = grokdeclarator (declarator, declspecs, FIELD, false,
8914 : 4231622 : width ? &width : NULL, decl_attrs, expr, NULL,
8915 : : DEPRECATED_NORMAL);
8916 : :
8917 : 4231622 : finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
8918 : 4231622 : DECL_INITIAL (value) = width;
8919 : 4231622 : if (width)
8920 : 51871 : SET_DECL_C_BIT_FIELD (value);
8921 : :
8922 : 4231622 : if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
8923 : : {
8924 : : /* If we currently have a binding for this field, set the
8925 : : in_struct field in the binding, so that we warn about lookups
8926 : : which find it. */
8927 : 1312 : struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
8928 : 1312 : if (b != NULL)
8929 : : {
8930 : : /* If the in_struct field is not yet set, push it on a list
8931 : : to be cleared when this struct is finished. */
8932 : 93 : if (!b->in_struct)
8933 : : {
8934 : 91 : struct_parse_info->fields.safe_push (b);
8935 : 91 : b->in_struct = 1;
8936 : : }
8937 : : }
8938 : : }
8939 : :
8940 : : return value;
8941 : : }
8942 : :
8943 : : /* Subroutine of detect_field_duplicates: return whether X and Y,
8944 : : which are both fields in the same struct, have duplicate field
8945 : : names. */
8946 : :
8947 : : static bool
8948 : 4220278 : is_duplicate_field (tree x, tree y)
8949 : : {
8950 : 4220278 : if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
8951 : : return true;
8952 : :
8953 : : /* When using -fplan9-extensions, an anonymous field whose name is a
8954 : : typedef can duplicate a field name. */
8955 : 4220277 : if (flag_plan9_extensions
8956 : 4220277 : && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
8957 : : {
8958 : 0 : tree xt, xn, yt, yn;
8959 : :
8960 : 0 : xt = TREE_TYPE (x);
8961 : 0 : if (DECL_NAME (x) != NULL_TREE)
8962 : 0 : xn = DECL_NAME (x);
8963 : 0 : else if (RECORD_OR_UNION_TYPE_P (xt)
8964 : 0 : && TYPE_NAME (xt) != NULL_TREE
8965 : 0 : && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
8966 : 0 : xn = DECL_NAME (TYPE_NAME (xt));
8967 : : else
8968 : : xn = NULL_TREE;
8969 : :
8970 : 0 : yt = TREE_TYPE (y);
8971 : 0 : if (DECL_NAME (y) != NULL_TREE)
8972 : 0 : yn = DECL_NAME (y);
8973 : 0 : else if (RECORD_OR_UNION_TYPE_P (yt)
8974 : 0 : && TYPE_NAME (yt) != NULL_TREE
8975 : 0 : && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
8976 : 0 : yn = DECL_NAME (TYPE_NAME (yt));
8977 : : else
8978 : : yn = NULL_TREE;
8979 : :
8980 : 0 : if (xn != NULL_TREE && xn == yn)
8981 : 0 : return true;
8982 : : }
8983 : :
8984 : : return false;
8985 : : }
8986 : :
8987 : : /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
8988 : : to HTAB, giving errors for any duplicates. */
8989 : :
8990 : : static void
8991 : 78846 : detect_field_duplicates_hash (tree fieldlist,
8992 : : hash_table<nofree_ptr_hash <tree_node> > *htab)
8993 : : {
8994 : 78846 : tree x, y;
8995 : 78846 : tree_node **slot;
8996 : :
8997 : 1388573 : for (x = fieldlist; x ; x = DECL_CHAIN (x))
8998 : 1309727 : if ((y = DECL_NAME (x)) != NULL_TREE)
8999 : : {
9000 : 1290018 : slot = htab->find_slot (y, INSERT);
9001 : 1290018 : if (*slot)
9002 : : {
9003 : 15 : error ("duplicate member %q+D", x);
9004 : 15 : DECL_NAME (x) = NULL_TREE;
9005 : : }
9006 : 1290018 : *slot = y;
9007 : : }
9008 : 19709 : else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9009 : : {
9010 : 11235 : detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
9011 : :
9012 : : /* When using -fplan9-extensions, an anonymous field whose
9013 : : name is a typedef can duplicate a field name. */
9014 : 11235 : if (flag_plan9_extensions
9015 : 15 : && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9016 : 11249 : && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
9017 : : {
9018 : 2 : tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
9019 : 2 : slot = htab->find_slot (xn, INSERT);
9020 : 2 : if (*slot)
9021 : 0 : error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
9022 : 2 : *slot = xn;
9023 : : }
9024 : : }
9025 : 78846 : }
9026 : :
9027 : : /* Generate an error for any duplicate field names in FIELDLIST. Munge
9028 : : the list such that this does not present a problem later. */
9029 : :
9030 : : static void
9031 : 1152901 : detect_field_duplicates (tree fieldlist)
9032 : : {
9033 : 1152901 : tree x, y;
9034 : 1152901 : int timeout = 10;
9035 : :
9036 : : /* If the struct is the list of instance variables of an Objective-C
9037 : : class, then we need to check all the instance variables of
9038 : : superclasses when checking for duplicates (since you can't have
9039 : : an instance variable in a subclass with the same name as an
9040 : : instance variable in a superclass). We pass on this job to the
9041 : : Objective-C compiler. objc_detect_field_duplicates() will return
9042 : : false if we are not checking the list of instance variables and
9043 : : the C frontend should proceed with the standard field duplicate
9044 : : checks. If we are checking the list of instance variables, the
9045 : : ObjC frontend will do the check, emit the errors if needed, and
9046 : : then return true. */
9047 : 1152901 : if (c_dialect_objc ())
9048 : 0 : if (objc_detect_field_duplicates (false))
9049 : : return;
9050 : :
9051 : : /* First, see if there are more than "a few" fields.
9052 : : This is trivially true if there are zero or one fields. */
9053 : 1152901 : if (!fieldlist || !DECL_CHAIN (fieldlist))
9054 : : return;
9055 : : x = fieldlist;
9056 : 3397496 : do {
9057 : 3397496 : timeout--;
9058 : 3397496 : if (DECL_NAME (x) == NULL_TREE
9059 : 3397496 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9060 : : timeout = 0;
9061 : 3397496 : x = DECL_CHAIN (x);
9062 : 3397496 : } while (timeout > 0 && x);
9063 : :
9064 : : /* If there were "few" fields and no anonymous structures or unions,
9065 : : avoid the overhead of allocating a hash table. Instead just do
9066 : : the nested traversal thing. */
9067 : 950364 : if (timeout > 0)
9068 : : {
9069 : 2757145 : for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
9070 : : /* When using -fplan9-extensions, we can have duplicates
9071 : : between typedef names and fields. */
9072 : 1874392 : if (DECL_NAME (x)
9073 : 1874392 : || (flag_plan9_extensions
9074 : 0 : && DECL_NAME (x) == NULL_TREE
9075 : 0 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9076 : 0 : && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9077 : 0 : && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
9078 : : {
9079 : 6094134 : for (y = fieldlist; y != x; y = TREE_CHAIN (y))
9080 : 4220278 : if (is_duplicate_field (y, x))
9081 : : {
9082 : 1 : error ("duplicate member %q+D", x);
9083 : 1 : DECL_NAME (x) = NULL_TREE;
9084 : : }
9085 : : }
9086 : : }
9087 : : else
9088 : : {
9089 : 67611 : hash_table<nofree_ptr_hash <tree_node> > htab (37);
9090 : 67611 : detect_field_duplicates_hash (fieldlist, &htab);
9091 : 67611 : }
9092 : : }
9093 : :
9094 : : /* Finish up struct info used by -Wc++-compat. */
9095 : :
9096 : : static void
9097 : 575 : warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
9098 : : location_t record_loc)
9099 : : {
9100 : 575 : unsigned int ix;
9101 : 575 : tree x;
9102 : 575 : struct c_binding *b;
9103 : :
9104 : 575 : if (fieldlist == NULL_TREE)
9105 : : {
9106 : 9 : if (code == RECORD_TYPE)
9107 : 6 : warning_at (record_loc, OPT_Wc___compat,
9108 : : "empty struct has size 0 in C, size 1 in C++");
9109 : : else
9110 : 3 : warning_at (record_loc, OPT_Wc___compat,
9111 : : "empty union has size 0 in C, size 1 in C++");
9112 : : }
9113 : :
9114 : : /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
9115 : : the current struct. We do this now at the end of the struct
9116 : : because the flag is used to issue visibility warnings, and we
9117 : : only want to issue those warnings if the type is referenced
9118 : : outside of the struct declaration. */
9119 : 623 : FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
9120 : 48 : C_TYPE_DEFINED_IN_STRUCT (x) = 1;
9121 : :
9122 : : /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
9123 : : typedefs used when declaring fields in this struct. If the name
9124 : : of any of the fields is also a typedef name then the struct would
9125 : : not parse in C++, because the C++ lookup rules say that the
9126 : : typedef name would be looked up in the context of the struct, and
9127 : : would thus be the field rather than the typedef. */
9128 : 575 : if (!struct_parse_info->typedefs_seen.is_empty ()
9129 : 67 : && fieldlist != NULL_TREE)
9130 : : {
9131 : : /* Use a hash_set<tree> using the name of the typedef. We can use
9132 : : a hash_set<tree> because identifiers are interned. */
9133 : 67 : hash_set<tree> tset;
9134 : :
9135 : 254 : FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
9136 : 120 : tset.add (DECL_NAME (x));
9137 : :
9138 : 387 : for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
9139 : : {
9140 : 320 : if (DECL_NAME (x) != NULL_TREE
9141 : 320 : && tset.contains (DECL_NAME (x)))
9142 : : {
9143 : 2 : warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
9144 : : "using %qD as both field and typedef name is "
9145 : : "invalid in C++", x);
9146 : : /* FIXME: It would be nice to report the location where
9147 : : the typedef name is used. */
9148 : : }
9149 : : }
9150 : 67 : }
9151 : :
9152 : : /* For each field which has a binding and which was not defined in
9153 : : an enclosing struct, clear the in_struct field. */
9154 : 666 : FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
9155 : 91 : b->in_struct = 0;
9156 : 575 : }
9157 : :
9158 : : /* Function to help qsort sort FIELD_DECLs by name order. */
9159 : :
9160 : : static int
9161 : 19945993 : field_decl_cmp (const void *x_p, const void *y_p)
9162 : : {
9163 : 19945993 : const tree *const x = (const tree *) x_p;
9164 : 19945993 : const tree *const y = (const tree *) y_p;
9165 : :
9166 : 19945993 : if (DECL_NAME (*x) == DECL_NAME (*y))
9167 : : /* A nontype is "greater" than a type. */
9168 : 0 : return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9169 : 19945993 : if (DECL_NAME (*x) == NULL_TREE)
9170 : : return -1;
9171 : 19945993 : if (DECL_NAME (*y) == NULL_TREE)
9172 : : return 1;
9173 : 19945993 : if (DECL_NAME (*x) < DECL_NAME (*y))
9174 : 9823660 : return -1;
9175 : : return 1;
9176 : : }
9177 : :
9178 : : /* If this structure or union completes the type of any previous
9179 : : variable declaration, lay it out and output its rtl. */
9180 : : static void
9181 : 1327782 : finish_incomplete_vars (tree incomplete_vars, bool toplevel)
9182 : : {
9183 : 1327911 : for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
9184 : : {
9185 : 129 : tree decl = TREE_VALUE (x);
9186 : 129 : if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9187 : 0 : layout_array_type (TREE_TYPE (decl));
9188 : 129 : if (TREE_CODE (decl) != TYPE_DECL)
9189 : : {
9190 : 129 : relayout_decl (decl);
9191 : 129 : if (c_dialect_objc ())
9192 : 0 : objc_check_decl (decl);
9193 : 129 : rest_of_decl_compilation (decl, toplevel, 0);
9194 : : }
9195 : : }
9196 : 1327782 : }
9197 : :
9198 : : /* Determine whether the FIELD_DECL X is a flexible array member according to
9199 : : the following info:
9200 : : A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT;
9201 : : B. whether the FIELD_DECL is an array that is declared as "[]", "[0]",
9202 : : or "[1]";
9203 : : C. flag_strict_flex_arrays;
9204 : : D. the attribute strict_flex_array that is attached to the field
9205 : : if presenting.
9206 : : Return TRUE when it's a flexible array member, FALSE otherwise. */
9207 : :
9208 : : static bool
9209 : 4231702 : is_flexible_array_member_p (bool is_last_field,
9210 : : tree x)
9211 : : {
9212 : : /* If not the last field, return false. */
9213 : 4231702 : if (!is_last_field)
9214 : : return false;
9215 : :
9216 : : /* If not an array field, return false. */
9217 : 1614929 : if (TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
9218 : : return false;
9219 : :
9220 : 520300 : bool is_zero_length_array = zero_length_array_type_p (TREE_TYPE (x));
9221 : 520300 : bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x));
9222 : 520300 : bool is_flexible_array = c_flexible_array_member_type_p (TREE_TYPE (x));
9223 : :
9224 : 520300 : unsigned int strict_flex_array_level = c_strict_flex_array_level_of (x);
9225 : :
9226 : 520300 : switch (strict_flex_array_level)
9227 : : {
9228 : : case 0:
9229 : : /* Default, all trailing arrays are flexible array members. */
9230 : : return true;
9231 : 77 : case 1:
9232 : : /* Level 1: all "[1]", "[0]", and "[]" are flexible array members. */
9233 : 77 : if (is_one_element_array)
9234 : : return true;
9235 : : /* FALLTHROUGH. */
9236 : 141 : case 2:
9237 : : /* Level 2: all "[0]", and "[]" are flexible array members. */
9238 : 141 : if (is_zero_length_array)
9239 : : return true;
9240 : : /* FALLTHROUGH. */
9241 : 201 : case 3:
9242 : : /* Level 3: Only "[]" are flexible array members. */
9243 : 201 : if (is_flexible_array)
9244 : : return true;
9245 : : break;
9246 : 0 : default:
9247 : 0 : gcc_unreachable ();
9248 : : }
9249 : : return false;
9250 : : }
9251 : :
9252 : : /* Recompute TYPE_CANONICAL for variants of the type including qualified
9253 : : versions of the type and related pointer types after an aggregate type
9254 : : has been finalized.
9255 : : Will not update array types, pointers to array types, function
9256 : : types and other derived types created while the type was still
9257 : : incomplete, those will remain TYPE_STRUCTURAL_EQUALITY_P. */
9258 : :
9259 : : static void
9260 : 1163011 : c_update_type_canonical (tree t)
9261 : : {
9262 : 1163011 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t && !TYPE_QUALS (t));
9263 : 2352155 : for (tree x = t, l = NULL_TREE; x; l = x, x = TYPE_NEXT_VARIANT (x))
9264 : : {
9265 : 1189144 : if (x != t && TYPE_STRUCTURAL_EQUALITY_P (x))
9266 : : {
9267 : 26133 : if (!TYPE_QUALS (x))
9268 : 25511 : TYPE_CANONICAL (x) = TYPE_CANONICAL (t);
9269 : : else
9270 : : {
9271 : 622 : tree
9272 : 622 : c = c_build_qualified_type (TYPE_CANONICAL (t), TYPE_QUALS (x));
9273 : 622 : if (TYPE_STRUCTURAL_EQUALITY_P (c))
9274 : : {
9275 : 610 : gcc_checking_assert (TYPE_CANONICAL (t) == t);
9276 : 610 : if (c == x)
9277 : 610 : TYPE_CANONICAL (x) = x;
9278 : : else
9279 : : {
9280 : : /* build_qualified_type for this function unhelpfully
9281 : : moved c from some later spot in TYPE_MAIN_VARIANT (t)
9282 : : chain to right after t (or created it there). Move
9283 : : it right before x and process c and then x. */
9284 : 0 : gcc_checking_assert (TYPE_NEXT_VARIANT (t) == c);
9285 : 0 : if (l != t)
9286 : : {
9287 : 0 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (c);
9288 : 0 : TYPE_NEXT_VARIANT (l) = c;
9289 : 0 : TYPE_NEXT_VARIANT (c) = x;
9290 : : }
9291 : 0 : TYPE_CANONICAL (c) = c;
9292 : 0 : x = c;
9293 : : }
9294 : : }
9295 : : else
9296 : 12 : TYPE_CANONICAL (x) = TYPE_CANONICAL (c);
9297 : : }
9298 : : }
9299 : 1163011 : else if (x != t)
9300 : 0 : continue;
9301 : 1256555 : for (tree p = TYPE_POINTER_TO (x); p; p = TYPE_NEXT_PTR_TO (p))
9302 : : {
9303 : 67411 : if (!TYPE_STRUCTURAL_EQUALITY_P (p))
9304 : 0 : continue;
9305 : 67411 : if (TYPE_CANONICAL (x) != x || TYPE_REF_CAN_ALIAS_ALL (p))
9306 : 2434 : TYPE_CANONICAL (p)
9307 : 4868 : = c_build_pointer_type_for_mode (TYPE_CANONICAL (x), TYPE_MODE (p),
9308 : : false);
9309 : : else
9310 : 64977 : TYPE_CANONICAL (p) = p;
9311 : 67411 : c_update_type_canonical (p);
9312 : : }
9313 : : }
9314 : 1163011 : }
9315 : :
9316 : : /* Verify the argument of the counted_by attribute of each of the
9317 : : FIELDS_WITH_COUNTED_BY is a valid field of the containing structure,
9318 : : STRUCT_TYPE, Report error and remove the corresponding attribute
9319 : : when it's not. */
9320 : :
9321 : : static void
9322 : 157 : verify_counted_by_attribute (tree struct_type,
9323 : : auto_vec<tree> *fields_with_counted_by)
9324 : : {
9325 : 642 : for (tree field_decl : *fields_with_counted_by)
9326 : : {
9327 : 171 : tree attr_counted_by = lookup_attribute ("counted_by",
9328 : 171 : DECL_ATTRIBUTES (field_decl));
9329 : :
9330 : 171 : if (!attr_counted_by)
9331 : 0 : continue;
9332 : :
9333 : : /* If there is an counted_by attribute attached to the field,
9334 : : verify it. */
9335 : :
9336 : 171 : tree fieldname = TREE_VALUE (TREE_VALUE (attr_counted_by));
9337 : :
9338 : : /* Verify the argument of the attrbute is a valid field of the
9339 : : containing structure. */
9340 : :
9341 : 171 : tree counted_by_field = lookup_field (struct_type, fieldname);
9342 : :
9343 : : /* Error when the field is not found in the containing structure and
9344 : : remove the corresponding counted_by attribute from the field_decl. */
9345 : 171 : if (!counted_by_field)
9346 : : {
9347 : 4 : error_at (DECL_SOURCE_LOCATION (field_decl),
9348 : : "argument %qE to the %<counted_by%> attribute"
9349 : : " is not a field declaration in the same structure"
9350 : : " as %qD", fieldname, field_decl);
9351 : 4 : DECL_ATTRIBUTES (field_decl)
9352 : 8 : = remove_attribute ("counted_by", DECL_ATTRIBUTES (field_decl));
9353 : : }
9354 : : else
9355 : : /* Error when the field is not with an integer type. */
9356 : : {
9357 : 209 : while (TREE_CHAIN (counted_by_field))
9358 : 42 : counted_by_field = TREE_CHAIN (counted_by_field);
9359 : 167 : tree real_field = TREE_VALUE (counted_by_field);
9360 : :
9361 : 167 : if (!INTEGRAL_TYPE_P (TREE_TYPE (real_field)))
9362 : : {
9363 : 4 : error_at (DECL_SOURCE_LOCATION (field_decl),
9364 : : "argument %qE to the %<counted_by%> attribute"
9365 : : " is not a field declaration with an integer type",
9366 : : fieldname);
9367 : 4 : DECL_ATTRIBUTES (field_decl)
9368 : 8 : = remove_attribute ("counted_by",
9369 : 4 : DECL_ATTRIBUTES (field_decl));
9370 : : }
9371 : : }
9372 : : }
9373 : 157 : }
9374 : :
9375 : : /* TYPE is a struct or union that we're applying may_alias to after the body is
9376 : : parsed. Fixup any POINTER_TO types. */
9377 : :
9378 : : static void
9379 : 363 : c_fixup_may_alias (tree type)
9380 : : {
9381 : 428 : for (tree t = TYPE_POINTER_TO (type); t; t = TYPE_NEXT_PTR_TO (t))
9382 : 190 : for (tree v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
9383 : 125 : TYPE_REF_CAN_ALIAS_ALL (v) = true;
9384 : 363 : }
9385 : :
9386 : : /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
9387 : : LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
9388 : : FIELDLIST is a chain of FIELD_DECL nodes for the fields.
9389 : : ATTRIBUTES are attributes to be applied to the structure.
9390 : :
9391 : : ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
9392 : : the struct was started. */
9393 : :
9394 : : tree
9395 : 1152901 : finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
9396 : : class c_struct_parse_info *enclosing_struct_parse_info,
9397 : : tree *expr)
9398 : : {
9399 : 1152901 : tree x;
9400 : 1152901 : bool toplevel = file_scope == current_scope;
9401 : :
9402 : : /* If this type was previously laid out as a forward reference,
9403 : : make sure we lay it out again. */
9404 : :
9405 : 1152901 : TYPE_SIZE (t) = NULL_TREE;
9406 : :
9407 : 1152901 : decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9408 : :
9409 : 1152901 : if (pedantic)
9410 : : {
9411 : 7349 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9412 : : {
9413 : 7330 : if (DECL_NAME (x) != NULL_TREE)
9414 : : break;
9415 : 23 : if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9416 : : break;
9417 : : }
9418 : :
9419 : 7333 : if (x == NULL_TREE)
9420 : : {
9421 : 19 : if (TREE_CODE (t) == UNION_TYPE)
9422 : : {
9423 : 5 : if (fieldlist)
9424 : 2 : pedwarn (loc, OPT_Wpedantic, "union has no named members");
9425 : : else
9426 : 3 : pedwarn (loc, OPT_Wpedantic, "union has no members");
9427 : : }
9428 : : else
9429 : : {
9430 : 14 : if (fieldlist)
9431 : 3 : pedwarn (loc, OPT_Wpedantic, "struct has no named members");
9432 : : else
9433 : 11 : pedwarn (loc, OPT_Wpedantic, "struct has no members");
9434 : : }
9435 : : }
9436 : : }
9437 : :
9438 : : /* Install struct as DECL_CONTEXT of each field decl.
9439 : : Also process specified field sizes, found in the DECL_INITIAL,
9440 : : storing 0 there after the type has been changed to precision equal
9441 : : to its width, rather than the precision of the specified standard
9442 : : type. (Correct layout requires the original type to have been preserved
9443 : : until now.) */
9444 : :
9445 : 1152901 : bool saw_named_field = false;
9446 : 1152901 : auto_vec<tree> fields_with_counted_by;
9447 : 5384643 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9448 : : {
9449 : : /* Whether this field is the last field of the structure or union.
9450 : : for UNION, any field is the last field of it. */
9451 : 4231742 : bool is_last_field = (DECL_CHAIN (x) == NULL_TREE)
9452 : 4231742 : || (TREE_CODE (t) == UNION_TYPE);
9453 : :
9454 : 4231742 : if (TREE_TYPE (x) == error_mark_node)
9455 : 40 : continue;
9456 : :
9457 : 4231702 : DECL_CONTEXT (x) = t;
9458 : :
9459 : 4231702 : tree t1 = strip_array_types (TREE_TYPE (x));
9460 : : /* If any field is const, the structure type is pseudo-const. */
9461 : 4231702 : if (TREE_READONLY (x))
9462 : 64244 : C_TYPE_FIELDS_READONLY (t) = 1;
9463 : : else
9464 : : {
9465 : : /* A field that is pseudo-const makes the structure likewise. */
9466 : 4167458 : if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
9467 : 641 : C_TYPE_FIELDS_READONLY (t) = 1;
9468 : : }
9469 : :
9470 : : /* Any field that is volatile means variables of this type must be
9471 : : treated in some ways as volatile. */
9472 : 4231702 : if (TREE_THIS_VOLATILE (x))
9473 : : {
9474 : 367 : C_TYPE_FIELDS_VOLATILE (t) = 1;
9475 : 367 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9476 : : }
9477 : :
9478 : : /* Any field that is volatile, restrict-qualified or atomic
9479 : : means the type cannot be used for a constexpr object. */
9480 : 4231702 : if (TYPE_QUALS (t1)
9481 : 4231702 : & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
9482 : 1279 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9483 : 4230423 : else if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_NON_CONSTEXPR (t1))
9484 : 31 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9485 : :
9486 : : /* Any field of nominal variable size implies structure is too. */
9487 : 4231702 : if (C_DECL_VARIABLE_SIZE (x))
9488 : 649 : C_TYPE_VARIABLE_SIZE (t) = 1;
9489 : :
9490 : : /* If any field is variably modified, record this fact. */
9491 : 4231702 : if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (x)))
9492 : 707 : C_TYPE_VARIABLY_MODIFIED (t) = 1;
9493 : :
9494 : 4231702 : if (DECL_C_BIT_FIELD (x))
9495 : : {
9496 : 51880 : unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
9497 : 51880 : DECL_SIZE (x) = bitsize_int (width);
9498 : 51880 : DECL_BIT_FIELD (x) = 1;
9499 : : }
9500 : :
9501 : 4231702 : if (TYPE_PACKED (t)
9502 : 4231702 : && (DECL_BIT_FIELD (x)
9503 : 3235 : || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
9504 : 4442 : DECL_PACKED (x) = 1;
9505 : :
9506 : : /* Detect flexible array member in an invalid context. */
9507 : 4231702 : if (c_flexible_array_member_type_p (TREE_TYPE (x)))
9508 : : {
9509 : 84582 : if (TREE_CODE (t) == UNION_TYPE)
9510 : 29 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9511 : : "flexible array member in union is a GCC extension");
9512 : 84553 : else if (!is_last_field)
9513 : : {
9514 : 3 : error_at (DECL_SOURCE_LOCATION (x),
9515 : : "flexible array member not at end of struct");
9516 : 3 : TREE_TYPE (x) = error_mark_node;
9517 : : }
9518 : 84550 : else if (!saw_named_field)
9519 : 18 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9520 : : "flexible array member in a struct with no named "
9521 : : "members is a GCC extension");
9522 : :
9523 : : /* If there is a counted_by attribute attached to this field,
9524 : : record it here and do more verification later after the
9525 : : whole structure is complete. */
9526 : 84582 : if (lookup_attribute ("counted_by", DECL_ATTRIBUTES (x)))
9527 : 77 : fields_with_counted_by.safe_push (x);
9528 : : }
9529 : :
9530 : 4231702 : if (TREE_CODE (TREE_TYPE (x)) == POINTER_TYPE)
9531 : : /* If there is a counted_by attribute attached to this field,
9532 : : record it here and do more verification later after the
9533 : : whole structure is complete. */
9534 : 699223 : if (lookup_attribute ("counted_by", DECL_ATTRIBUTES (x)))
9535 : 94 : fields_with_counted_by.safe_push (x);
9536 : :
9537 : 25302 : if (pedantic && TREE_CODE (t) == RECORD_TYPE
9538 : 4252751 : && flexible_array_type_p (TREE_TYPE (x)))
9539 : 18 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9540 : : "invalid use of structure with flexible array member");
9541 : :
9542 : : /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */
9543 : 4231702 : DECL_NOT_FLEXARRAY (x) = !is_flexible_array_member_p (is_last_field, x);
9544 : :
9545 : : /* Set TYPE_INCLUDES_FLEXARRAY for the context of x, t.
9546 : : when x is an array and is the last field.
9547 : : There is only one last_field for a structure type, but there might
9548 : : be multiple last_fields for a union type, therefore we should ORed
9549 : : the result for multiple last_fields. */
9550 : 4231702 : if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
9551 : 689284 : TYPE_INCLUDES_FLEXARRAY (t)
9552 : 1378568 : |= is_last_field && c_flexible_array_member_type_p (TREE_TYPE (x));
9553 : : /* Recursively set TYPE_INCLUDES_FLEXARRAY for the context of x, t
9554 : : when x is an union or record and is the last field. */
9555 : 3542418 : else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9556 : 455914 : TYPE_INCLUDES_FLEXARRAY (t)
9557 : 911828 : |= is_last_field && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x));
9558 : :
9559 : 4231702 : if (warn_flex_array_member_not_at_end
9560 : 56 : && !is_last_field
9561 : 22 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9562 : 4231714 : && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x)))
9563 : 5 : warning_at (DECL_SOURCE_LOCATION (x),
9564 : 5 : OPT_Wflex_array_member_not_at_end,
9565 : : "structure containing a flexible array member"
9566 : : " is not at the end of another structure");
9567 : :
9568 : 4231702 : if (DECL_NAME (x)
9569 : 4231702 : || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9570 : : saw_named_field = true;
9571 : :
9572 : 7774120 : if (AGGREGATE_TYPE_P (TREE_TYPE (x))
9573 : 4687616 : && TYPE_TYPELESS_STORAGE (TREE_TYPE (x)))
9574 : 438742 : TYPE_TYPELESS_STORAGE (t) = true;
9575 : : }
9576 : :
9577 : 1152901 : detect_field_duplicates (fieldlist);
9578 : :
9579 : : /* Now we have the nearly final fieldlist. Record it,
9580 : : then lay out the structure or union (including the fields). */
9581 : :
9582 : 1152901 : TYPE_FIELDS (t) = fieldlist;
9583 : :
9584 : 1152901 : maybe_apply_pragma_scalar_storage_order (t);
9585 : :
9586 : 1152901 : layout_type (t);
9587 : :
9588 : 1152901 : if (TYPE_SIZE_UNIT (t)
9589 : 1152901 : && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
9590 : 1152307 : && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
9591 : 2305208 : && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
9592 : 1 : error ("type %qT is too large", t);
9593 : :
9594 : : /* Give bit-fields their proper types and rewrite the type of array fields
9595 : : with scalar component if the enclosing type has reverse storage order. */
9596 : 5384643 : for (tree field = fieldlist; field; field = DECL_CHAIN (field))
9597 : : {
9598 : 4231742 : if (TREE_CODE (field) == FIELD_DECL
9599 : 4231742 : && DECL_INITIAL (field)
9600 : 4283632 : && TREE_TYPE (field) != error_mark_node)
9601 : : {
9602 : 51880 : unsigned HOST_WIDE_INT width
9603 : 51880 : = tree_to_uhwi (DECL_INITIAL (field));
9604 : 51880 : tree type = TREE_TYPE (field);
9605 : 51880 : if (width != TYPE_PRECISION (type))
9606 : : {
9607 : 37730 : if (TREE_CODE (type) == BITINT_TYPE
9608 : 37800 : && width >= (TYPE_UNSIGNED (type) ? 1 : 2))
9609 : 161 : TREE_TYPE (field)
9610 : 322 : = build_bitint_type (width, TYPE_UNSIGNED (type));
9611 : : else
9612 : 37569 : TREE_TYPE (field)
9613 : 37569 : = c_build_bitfield_integer_type (width,
9614 : 37569 : TYPE_UNSIGNED (type));
9615 : 37730 : if (tree attr = c_hardbool_type_attr (type))
9616 : 281 : decl_attributes (&TREE_TYPE (field),
9617 : : copy_list (attr),
9618 : : 0, NULL_TREE);
9619 : 37730 : SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
9620 : : }
9621 : 51880 : DECL_INITIAL (field) = NULL_TREE;
9622 : : }
9623 : 4179862 : else if (TYPE_REVERSE_STORAGE_ORDER (t)
9624 : 693 : && TREE_CODE (field) == FIELD_DECL
9625 : 4180555 : && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
9626 : : {
9627 : 106 : tree ftype = TREE_TYPE (field);
9628 : 106 : tree ctype = strip_array_types (ftype);
9629 : 106 : if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
9630 : : {
9631 : 93 : tree fmain_type = TYPE_MAIN_VARIANT (ftype);
9632 : 93 : tree *typep = &fmain_type;
9633 : 95 : do {
9634 : 95 : *typep = build_distinct_type_copy (*typep);
9635 : 95 : TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
9636 : 95 : typep = &TREE_TYPE (*typep);
9637 : 95 : } while (TREE_CODE (*typep) == ARRAY_TYPE);
9638 : 93 : TREE_TYPE (field)
9639 : 186 : = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
9640 : : }
9641 : : }
9642 : :
9643 : : /* Warn on problematic type punning for storage order purposes. */
9644 : 4231742 : if (TREE_CODE (t) == UNION_TYPE
9645 : 831519 : && TREE_CODE (field) == FIELD_DECL
9646 : 5063261 : && AGGREGATE_TYPE_P (TREE_TYPE (field)))
9647 : : {
9648 : 412911 : tree ftype = TREE_TYPE (field);
9649 : 412911 : if (TREE_CODE (ftype) == ARRAY_TYPE)
9650 : 304464 : ftype = strip_array_types (ftype);
9651 : 412911 : if (RECORD_OR_UNION_TYPE_P (ftype)
9652 : 412911 : && TYPE_REVERSE_STORAGE_ORDER (ftype)
9653 : 108941 : != TYPE_REVERSE_STORAGE_ORDER (t))
9654 : 1 : warning_at (DECL_SOURCE_LOCATION (field),
9655 : 1 : OPT_Wscalar_storage_order,
9656 : : "type punning toggles scalar storage order");
9657 : : }
9658 : : }
9659 : :
9660 : : /* Now we have the truly final field list.
9661 : : Store it in this type and in the variants. */
9662 : :
9663 : 1152901 : TYPE_FIELDS (t) = fieldlist;
9664 : :
9665 : : /* If there are lots of fields, sort so we can look through them fast.
9666 : : We arbitrarily consider 16 or more elts to be "a lot". */
9667 : :
9668 : 1152901 : {
9669 : 1152901 : int len = 0;
9670 : :
9671 : 4949808 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9672 : : {
9673 : 3824201 : if (len > 15 || DECL_NAME (x) == NULL)
9674 : : break;
9675 : 3796907 : len += 1;
9676 : : }
9677 : :
9678 : 1152901 : if (len > 15)
9679 : : {
9680 : 22232 : tree *field_array;
9681 : 22232 : struct lang_type *space;
9682 : 22232 : struct sorted_fields_type *space2;
9683 : :
9684 : 22232 : len += list_length (x);
9685 : :
9686 : : /* Use the same allocation policy here that make_node uses, to
9687 : : ensure that this lives as long as the rest of the struct decl.
9688 : : All decls in an inline function need to be saved. */
9689 : :
9690 : 22232 : space = ((struct lang_type *)
9691 : 22232 : ggc_internal_cleared_alloc (c_dialect_objc ()
9692 : : ? sizeof (struct lang_type)
9693 : : : offsetof (struct lang_type,
9694 : : info)));
9695 : 22232 : space2 = ((sorted_fields_type *)
9696 : 44464 : ggc_internal_alloc (sizeof (struct sorted_fields_type)
9697 : 22232 : + len * sizeof (tree)));
9698 : :
9699 : 22232 : len = 0;
9700 : 22232 : space->s = space2;
9701 : 22232 : field_array = &space2->elts[0];
9702 : 779328 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9703 : : {
9704 : 760232 : field_array[len++] = x;
9705 : :
9706 : : /* If there is anonymous struct or union, break out of the loop. */
9707 : 760232 : if (DECL_NAME (x) == NULL)
9708 : : break;
9709 : : }
9710 : : /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
9711 : 22232 : if (x == NULL)
9712 : : {
9713 : 19096 : TYPE_LANG_SPECIFIC (t) = space;
9714 : 19096 : TYPE_LANG_SPECIFIC (t)->s->len = len;
9715 : 19096 : field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
9716 : 19096 : qsort (field_array, len, sizeof (tree), field_decl_cmp);
9717 : : }
9718 : : }
9719 : : }
9720 : :
9721 : : /* If this was supposed to be a transparent union, but we can't
9722 : : make it one, warn and turn off the flag. */
9723 : 1152901 : if (TREE_CODE (t) == UNION_TYPE
9724 : 367271 : && TYPE_TRANSPARENT_AGGR (t)
9725 : 1152944 : && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
9726 : : {
9727 : 13 : TYPE_TRANSPARENT_AGGR (t) = 0;
9728 : 13 : warning_at (loc, 0, "union cannot be made transparent");
9729 : : }
9730 : :
9731 : : /* Check for consistency with previous definition. */
9732 : 1152901 : if (flag_isoc23 && NULL != enclosing_struct_parse_info)
9733 : : {
9734 : 885932 : tree vistype = previous_tag (t);
9735 : 885932 : if (vistype
9736 : 124 : && TREE_CODE (vistype) == TREE_CODE (t)
9737 : 886056 : && !C_TYPE_BEING_DEFINED (vistype))
9738 : : {
9739 : 104 : TYPE_STUB_DECL (t) = TYPE_STUB_DECL (vistype);
9740 : 104 : if (c_type_variably_modified_p (t))
9741 : : {
9742 : 7 : error ("redefinition of struct or union %qT with variably "
9743 : : "modified type", t);
9744 : 7 : if (struct_parse_info->refloc != UNKNOWN_LOCATION)
9745 : 7 : inform (struct_parse_info->refloc, "originally defined here");
9746 : : }
9747 : 97 : else if (!comptypes_same_p (t, vistype))
9748 : : {
9749 : 38 : error ("redefinition of struct or union %qT", t);
9750 : 38 : if (struct_parse_info->refloc != UNKNOWN_LOCATION)
9751 : 38 : inform (struct_parse_info->refloc, "originally defined here");
9752 : : }
9753 : : }
9754 : : }
9755 : :
9756 : 1152901 : C_TYPE_BEING_DEFINED (t) = 0;
9757 : :
9758 : 1152901 : if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (t)))
9759 : 694 : for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9760 : 363 : c_fixup_may_alias (x);
9761 : :
9762 : : /* Set type canonical based on equivalence class. */
9763 : 1152901 : if (flag_isoc23 && !C_TYPE_VARIABLE_SIZE (t))
9764 : : {
9765 : 920825 : if (c_struct_htab == NULL)
9766 : 35120 : c_struct_htab = hash_table<c_struct_hasher>::create_ggc (61);
9767 : :
9768 : 920825 : hashval_t hash = c_struct_hasher::hash (t);
9769 : :
9770 : 920825 : gcc_checking_assert (TYPE_STRUCTURAL_EQUALITY_P (t));
9771 : 920825 : gcc_checking_assert (!TYPE_NAME (t)
9772 : : || TREE_CODE (TYPE_NAME (t)) == IDENTIFIER_NODE);
9773 : :
9774 : 920825 : tree *e = c_struct_htab->find_slot_with_hash (t, hash, INSERT);
9775 : 920825 : if (*e)
9776 : 61067 : TYPE_CANONICAL (t) = *e;
9777 : : else
9778 : : {
9779 : 859758 : TYPE_CANONICAL (t) = t;
9780 : 859758 : *e = t;
9781 : : }
9782 : 920825 : c_update_type_canonical (t);
9783 : : }
9784 : :
9785 : 1152901 : tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
9786 : 2336049 : for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9787 : : {
9788 : 1183148 : TYPE_FIELDS (x) = TYPE_FIELDS (t);
9789 : 1183148 : TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
9790 : 1183148 : TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
9791 : 1183148 : TYPE_TYPELESS_STORAGE (x) = TYPE_TYPELESS_STORAGE (t);
9792 : 1183148 : C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
9793 : 1183148 : C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
9794 : 1183148 : C_TYPE_FIELDS_NON_CONSTEXPR (x) = C_TYPE_FIELDS_NON_CONSTEXPR (t);
9795 : 1183148 : C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
9796 : 1183148 : C_TYPE_VARIABLY_MODIFIED (x) = C_TYPE_VARIABLY_MODIFIED (t);
9797 : 1183148 : C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
9798 : 1183148 : TYPE_INCLUDES_FLEXARRAY (x) = TYPE_INCLUDES_FLEXARRAY (t);
9799 : : }
9800 : :
9801 : : /* Update type location to the one of the definition, instead of e.g.
9802 : : a forward declaration. */
9803 : 1152901 : if (TYPE_STUB_DECL (t))
9804 : 1152901 : DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
9805 : :
9806 : : /* Finish debugging output for this type. */
9807 : 1152901 : rest_of_type_compilation (t, toplevel);
9808 : :
9809 : 1152901 : finish_incomplete_vars (incomplete_vars, toplevel);
9810 : :
9811 : : /* Make sure a DECL_EXPR is created for structs with VLA members.
9812 : : Because we do not know the context, we always pass expr
9813 : : to force creation of a BIND_EXPR which is required in some
9814 : : contexts. */
9815 : 1152901 : if (c_type_variably_modified_p (t))
9816 : 665 : add_decl_expr (loc, t, expr, false);
9817 : :
9818 : 1152901 : if (warn_cxx_compat)
9819 : 575 : warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
9820 : :
9821 : 1152901 : if (NULL != enclosing_struct_parse_info)
9822 : : {
9823 : 1112371 : delete struct_parse_info;
9824 : :
9825 : 1112371 : struct_parse_info = enclosing_struct_parse_info;
9826 : :
9827 : : /* If this struct is defined inside a struct, add it to
9828 : : struct_types. */
9829 : 1112371 : if (warn_cxx_compat
9830 : : && struct_parse_info != NULL
9831 : 350 : && !in_sizeof && !in_typeof && !in_alignof && !in_countof)
9832 : 345 : struct_parse_info->struct_types.safe_push (t);
9833 : : }
9834 : :
9835 : 1152901 : if (fields_with_counted_by.length () > 0)
9836 : 157 : verify_counted_by_attribute (t, &fields_with_counted_by);
9837 : :
9838 : 1152901 : return t;
9839 : 1152901 : }
9840 : :
9841 : : static struct {
9842 : : gt_pointer_operator new_value;
9843 : : void *cookie;
9844 : : } resort_data;
9845 : :
9846 : : /* This routine compares two fields like field_decl_cmp but using the
9847 : : pointer operator in resort_data. */
9848 : :
9849 : : static int
9850 : 5460 : resort_field_decl_cmp (const void *x_p, const void *y_p)
9851 : : {
9852 : 5460 : const tree *const x = (const tree *) x_p;
9853 : 5460 : const tree *const y = (const tree *) y_p;
9854 : :
9855 : 5460 : if (DECL_NAME (*x) == DECL_NAME (*y))
9856 : : /* A nontype is "greater" than a type. */
9857 : 0 : return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9858 : 5460 : if (DECL_NAME (*x) == NULL_TREE)
9859 : : return -1;
9860 : 5460 : if (DECL_NAME (*y) == NULL_TREE)
9861 : : return 1;
9862 : 5460 : {
9863 : 5460 : tree d1 = DECL_NAME (*x);
9864 : 5460 : tree d2 = DECL_NAME (*y);
9865 : 5460 : resort_data.new_value (&d1, &d1, resort_data.cookie);
9866 : 5460 : resort_data.new_value (&d2, &d2, resort_data.cookie);
9867 : 5460 : if (d1 < d2)
9868 : 2765 : return -1;
9869 : : }
9870 : 2695 : return 1;
9871 : : }
9872 : :
9873 : : /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9874 : :
9875 : : void
9876 : 6 : resort_sorted_fields (void *obj,
9877 : : void * ARG_UNUSED (orig_obj),
9878 : : gt_pointer_operator new_value,
9879 : : void *cookie)
9880 : : {
9881 : 6 : struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9882 : 6 : resort_data.new_value = new_value;
9883 : 6 : resort_data.cookie = cookie;
9884 : 6 : qsort (&sf->elts[0], sf->len, sizeof (tree),
9885 : : resort_field_decl_cmp);
9886 : 6 : }
9887 : :
9888 : : /* Lay out the type T, and its element type, and so on. */
9889 : :
9890 : : static void
9891 : 0 : layout_array_type (tree t)
9892 : : {
9893 : 0 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
9894 : 0 : layout_array_type (TREE_TYPE (t));
9895 : 0 : layout_type (t);
9896 : 0 : }
9897 : :
9898 : : /* Begin compiling the definition of an enumeration type.
9899 : : NAME is its name (or null if anonymous).
9900 : : LOC is the enum's location.
9901 : : FIXED_UNDERLYING_TYPE is the (C23) underlying type specified in the
9902 : : definition.
9903 : : Returns the type object, as yet incomplete.
9904 : : Also records info about it so that build_enumerator
9905 : : may be used to declare the individual values as they are read. */
9906 : :
9907 : : tree
9908 : 174881 : start_enum (location_t loc, struct c_enum_contents *the_enum, tree name,
9909 : : tree fixed_underlying_type, bool potential_nesting_p)
9910 : : {
9911 : 174881 : tree enumtype = NULL_TREE;
9912 : 174881 : location_t enumloc = UNKNOWN_LOCATION;
9913 : :
9914 : : /* If this is the real definition for a previous forward reference,
9915 : : fill in the contents in the same object that used to be the
9916 : : forward reference. */
9917 : :
9918 : 174881 : if (name != NULL_TREE)
9919 : 69696 : enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
9920 : :
9921 : 69696 : if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
9922 : : {
9923 : : /* If the type is currently being defined or if we have seen an
9924 : : incomplete version which is now complete, this is a nested
9925 : : redefinition. The later happens if the redefinition occurs
9926 : : inside the enum specifier itself. */
9927 : 177 : if (C_TYPE_BEING_DEFINED (enumtype)
9928 : 177 : || (potential_nesting_p && TYPE_VALUES (enumtype) != NULL_TREE))
9929 : 9 : error_at (loc, "nested redefinition of %<enum %E%>", name);
9930 : :
9931 : : /* For C23 we allow redefinitions. We set to zero and check for
9932 : : consistency later. */
9933 : 337 : if (flag_isoc23 && TYPE_VALUES (enumtype) != NULL_TREE)
9934 : : enumtype = NULL_TREE;
9935 : : }
9936 : :
9937 : 105336 : if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
9938 : : {
9939 : 174732 : enumtype = make_node (ENUMERAL_TYPE);
9940 : 174732 : TYPE_SIZE (enumtype) = NULL_TREE;
9941 : 174732 : SET_TYPE_STRUCTURAL_EQUALITY (enumtype);
9942 : 174732 : pushtag (loc, name, enumtype);
9943 : 174732 : if (fixed_underlying_type != NULL_TREE)
9944 : : {
9945 : : /* For an enum definition with a fixed underlying type, the
9946 : : type is complete during the definition and the
9947 : : enumeration constants have that type. If there was a
9948 : : tag, the type was completed in c_parser_enum_specifier.
9949 : : If not, it must be completed here. */
9950 : 12 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = true;
9951 : 12 : TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (fixed_underlying_type);
9952 : 12 : TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (fixed_underlying_type);
9953 : 12 : TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (fixed_underlying_type);
9954 : 12 : SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (fixed_underlying_type));
9955 : 12 : TYPE_SIZE (enumtype) = NULL_TREE;
9956 : 12 : TYPE_PRECISION (enumtype) = TYPE_PRECISION (fixed_underlying_type);
9957 : 12 : ENUM_UNDERLYING_TYPE (enumtype) = fixed_underlying_type;
9958 : 12 : TYPE_CANONICAL (enumtype) = TYPE_CANONICAL (fixed_underlying_type);
9959 : 12 : c_update_type_canonical (enumtype);
9960 : 12 : layout_type (enumtype);
9961 : : }
9962 : : }
9963 : : /* Update type location to the one of the definition, instead of e.g.
9964 : : a forward declaration. */
9965 : 149 : else if (TYPE_STUB_DECL (enumtype))
9966 : : {
9967 : 149 : enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
9968 : 149 : DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
9969 : : }
9970 : :
9971 : 174881 : C_TYPE_BEING_DEFINED (enumtype) = 1;
9972 : :
9973 : 174881 : if (TYPE_VALUES (enumtype) != NULL_TREE)
9974 : : {
9975 : : /* This enum is a named one that has been declared already. */
9976 : 6 : auto_diagnostic_group d;
9977 : 6 : error_at (loc, "redeclaration of %<enum %E%>", name);
9978 : 6 : if (enumloc != UNKNOWN_LOCATION)
9979 : 6 : inform (enumloc, "originally defined here");
9980 : :
9981 : : /* Completely replace its old definition.
9982 : : The old enumerators remain defined, however. */
9983 : 6 : TYPE_VALUES (enumtype) = NULL_TREE;
9984 : 6 : }
9985 : :
9986 : 174881 : if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype)
9987 : 174881 : && fixed_underlying_type == NULL_TREE)
9988 : : {
9989 : 3 : error_at (loc, "%<enum%> declared with but defined without "
9990 : : "fixed underlying type");
9991 : 3 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = false;
9992 : : }
9993 : :
9994 : 174881 : the_enum->enum_next_value = integer_zero_node;
9995 : 174881 : the_enum->enum_type = enumtype;
9996 : 174881 : the_enum->enum_overflow = 0;
9997 : :
9998 : 174881 : if (flag_short_enums && !ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
9999 : 106 : for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
10000 : 53 : TYPE_PACKED (v) = 1;
10001 : :
10002 : : /* FIXME: This will issue a warning for a use of a type defined
10003 : : within sizeof in a statement expr. This is not terribly serious
10004 : : as C++ doesn't permit statement exprs within sizeof anyhow. */
10005 : 174881 : if (warn_cxx_compat
10006 : 126 : && (in_sizeof || in_typeof || in_alignof || in_countof))
10007 : 0 : warning_at (loc, OPT_Wc___compat,
10008 : : "defining type in %qs expression is invalid in C++",
10009 : : (in_sizeof ? "sizeof"
10010 : 0 : : in_typeof ? "typeof"
10011 : 0 : : in_alignof ? "alignof"
10012 : : : "_Countof"));
10013 : :
10014 : 174881 : if (in_underspecified_init)
10015 : 6 : error_at (loc, "%qT defined in underspecified object initializer",
10016 : : enumtype);
10017 : :
10018 : 174881 : return enumtype;
10019 : : }
10020 : :
10021 : : /* After processing and defining all the values of an enumeration type,
10022 : : install their decls in the enumeration type and finish it off.
10023 : : ENUMTYPE is the type object, VALUES a list of decl-value pairs,
10024 : : and ATTRIBUTES are the specified attributes.
10025 : : Returns ENUMTYPE. */
10026 : :
10027 : : tree
10028 : 174881 : finish_enum (tree enumtype, tree values, tree attributes)
10029 : : {
10030 : 174881 : tree pair, tem;
10031 : 174881 : tree minnode = NULL_TREE, maxnode = NULL_TREE;
10032 : 174881 : int precision;
10033 : 174881 : signop sign;
10034 : 174881 : bool toplevel = (file_scope == current_scope);
10035 : 174881 : struct lang_type *lt;
10036 : :
10037 : 174881 : decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10038 : :
10039 : : /* Calculate the maximum value of any enumerator in this type. */
10040 : :
10041 : 174881 : if (values == error_mark_node)
10042 : 26 : minnode = maxnode = integer_zero_node;
10043 : : else
10044 : : {
10045 : 174855 : minnode = maxnode = TREE_VALUE (values);
10046 : 5557326 : for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
10047 : : {
10048 : 5382471 : tree value = TREE_VALUE (pair);
10049 : 5382471 : if (tree_int_cst_lt (maxnode, value))
10050 : 5240479 : maxnode = value;
10051 : 5382471 : if (tree_int_cst_lt (value, minnode))
10052 : 61917 : minnode = value;
10053 : : }
10054 : : }
10055 : :
10056 : : /* Construct the final type of this enumeration. It is the same
10057 : : as one of the integral types - the narrowest one that fits, except
10058 : : that normally we only go as narrow as int - and signed iff any of
10059 : : the values are negative. */
10060 : 174881 : sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
10061 : 174881 : precision = MAX (tree_int_cst_min_precision (minnode, sign),
10062 : : tree_int_cst_min_precision (maxnode, sign));
10063 : :
10064 : 174881 : bool wider_than_int =
10065 : 174881 : (tree_int_cst_lt (minnode, TYPE_MIN_VALUE (integer_type_node))
10066 : 174881 : || tree_int_cst_lt (TYPE_MAX_VALUE (integer_type_node), maxnode));
10067 : :
10068 : :
10069 : 174881 : if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
10070 : : {
10071 : : /* If the precision of the type was specified with an attribute and it
10072 : : was too small, give an error. Otherwise, use it. */
10073 : 174763 : if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
10074 : : {
10075 : 23 : if (precision > TYPE_PRECISION (enumtype))
10076 : : {
10077 : 6 : TYPE_PRECISION (enumtype) = 0;
10078 : 6 : error ("specified mode too small for enumerated values");
10079 : : }
10080 : : else
10081 : 17 : precision = TYPE_PRECISION (enumtype);
10082 : : }
10083 : : else
10084 : 174740 : TYPE_PRECISION (enumtype) = 0;
10085 : :
10086 : 174763 : if (TYPE_PACKED (enumtype)
10087 : 174684 : || precision > TYPE_PRECISION (integer_type_node)
10088 : 346870 : || TYPE_PRECISION (enumtype))
10089 : : {
10090 : 2783 : tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
10091 : 2668 : if (tem == NULL)
10092 : : {
10093 : : /* This should only occur when both signed and unsigned
10094 : : values of maximum precision occur among the
10095 : : enumerators. */
10096 : 3 : pedwarn (input_location, 0,
10097 : : "enumeration values exceed range of largest integer");
10098 : 3 : tem = widest_integer_literal_type_node;
10099 : : }
10100 : 2665 : else if (precision > TYPE_PRECISION (intmax_type_node)
10101 : 4 : && !tree_int_cst_lt (minnode,
10102 : 4 : TYPE_MIN_VALUE (intmax_type_node))
10103 : 2668 : && !tree_int_cst_lt (TYPE_MAX_VALUE (uintmax_type_node),
10104 : : maxnode))
10105 : 2 : pedwarn (input_location, OPT_Wpedantic,
10106 : : "enumeration values exceed range of %qs",
10107 : : sign == UNSIGNED ? "uintmax_t" : "intmax_t");
10108 : : }
10109 : : else
10110 : 172095 : tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
10111 : :
10112 : 174763 : TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
10113 : 174763 : TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
10114 : 174763 : TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
10115 : 174763 : SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
10116 : 174763 : TYPE_SIZE (enumtype) = NULL_TREE;
10117 : 174763 : TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
10118 : 174763 : ENUM_UNDERLYING_TYPE (enumtype) =
10119 : 174763 : c_common_type_for_size (TYPE_PRECISION (tem), TYPE_UNSIGNED (tem));
10120 : :
10121 : 524289 : TYPE_CANONICAL (enumtype) =
10122 : 174763 : TYPE_CANONICAL (ENUM_UNDERLYING_TYPE (enumtype));
10123 : 174763 : c_update_type_canonical (enumtype);
10124 : :
10125 : 174763 : layout_type (enumtype);
10126 : : }
10127 : :
10128 : 174881 : if (values != error_mark_node)
10129 : : {
10130 : : /* Change the type of the enumerators to be the enum type. We
10131 : : need to do this irrespective of the size of the enum, for
10132 : : proper type checking. Replace the DECL_INITIALs of the
10133 : : enumerators, and the value slots of the list, with copies
10134 : : that have the enum type; they cannot be modified in place
10135 : : because they may be shared (e.g. integer_zero_node) Finally,
10136 : : change the purpose slots to point to the names of the decls. */
10137 : 5732181 : for (pair = values; pair; pair = TREE_CHAIN (pair))
10138 : : {
10139 : 5557326 : tree enu = TREE_PURPOSE (pair);
10140 : 5557326 : tree ini = DECL_INITIAL (enu);
10141 : :
10142 : 5557326 : TREE_TYPE (enu) = enumtype;
10143 : :
10144 : : /* Before C23, the ISO C Standard mandates enumerators to
10145 : : have type int, even though the underlying type of an enum
10146 : : type is unspecified. However, C23 allows enumerators of
10147 : : any integer type, and if an enumeration has any
10148 : : enumerators wider than int, all enumerators have the
10149 : : enumerated type after it is parsed. Any enumerators that
10150 : : fit in int are given type int in build_enumerator (which
10151 : : is the correct type while the enumeration is being
10152 : : parsed), so no conversions are needed here if all
10153 : : enumerators fit in int. If the enum has a fixed
10154 : : underlying type, the correct type was also given in
10155 : : build_enumerator. */
10156 : 5557326 : if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) && wider_than_int)
10157 : 28521 : ini = convert (enumtype, ini);
10158 : :
10159 : 5557326 : DECL_INITIAL (enu) = ini;
10160 : 5557326 : TREE_PURPOSE (pair) = DECL_NAME (enu);
10161 : : /* To match the C++ FE, store the CONST_DECL rather than just its
10162 : : value. */
10163 : 5557326 : TREE_VALUE (pair) = enu;
10164 : : }
10165 : :
10166 : 174855 : TYPE_VALUES (enumtype) = values;
10167 : : }
10168 : :
10169 : : /* Record the min/max values so that we can warn about bit-field
10170 : : enumerations that are too small for the values. */
10171 : 174881 : lt = ((struct lang_type *)
10172 : 174881 : ggc_internal_cleared_alloc (c_dialect_objc ()
10173 : : ? sizeof (struct lang_type)
10174 : : : offsetof (struct lang_type, info)));
10175 : 174881 : lt->enum_min = minnode;
10176 : 174881 : lt->enum_max = maxnode;
10177 : 174881 : TYPE_LANG_SPECIFIC (enumtype) = lt;
10178 : :
10179 : : /* Fix up all variant types of this enum type. */
10180 : 174881 : tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
10181 : 349772 : for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
10182 : : {
10183 : 174891 : C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
10184 : 174891 : if (tem == enumtype)
10185 : 174881 : continue;
10186 : 10 : TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
10187 : 10 : TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
10188 : 10 : TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
10189 : 10 : TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
10190 : 10 : TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
10191 : 10 : SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
10192 : 10 : TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
10193 : 10 : SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
10194 : 10 : TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
10195 : 10 : TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
10196 : 10 : TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
10197 : 10 : ENUM_UNDERLYING_TYPE (tem) = ENUM_UNDERLYING_TYPE (enumtype);
10198 : 10 : TYPE_PACKED (tem) = TYPE_PACKED (enumtype);
10199 : : }
10200 : :
10201 : : /* Finish debugging output for this type. */
10202 : 174881 : rest_of_type_compilation (enumtype, toplevel);
10203 : :
10204 : 174881 : finish_incomplete_vars (incomplete_vars, toplevel);
10205 : :
10206 : : /* If this enum is defined inside a struct, add it to
10207 : : struct_types. */
10208 : 174881 : if (warn_cxx_compat
10209 : 126 : && struct_parse_info != NULL
10210 : 42 : && !in_sizeof && !in_typeof && !in_alignof && !in_countof)
10211 : 42 : struct_parse_info->struct_types.safe_push (enumtype);
10212 : :
10213 : : /* Check for consistency with previous definition */
10214 : 174881 : if (flag_isoc23)
10215 : : {
10216 : 136915 : tree vistype = previous_tag (enumtype);
10217 : 136915 : if (vistype
10218 : 33 : && TREE_CODE (vistype) == TREE_CODE (enumtype)
10219 : 136944 : && !C_TYPE_BEING_DEFINED (vistype))
10220 : : {
10221 : 29 : TYPE_STUB_DECL (enumtype) = TYPE_STUB_DECL (vistype);
10222 : 29 : if (!comptypes_same_p (enumtype, vistype))
10223 : 3 : error("conflicting redefinition of enum %qT", enumtype);
10224 : : }
10225 : : }
10226 : :
10227 : 174881 : C_TYPE_BEING_DEFINED (enumtype) = 0;
10228 : :
10229 : 174881 : return enumtype;
10230 : : }
10231 : :
10232 : : /* Build and install a CONST_DECL for one value of the
10233 : : current enumeration type (one that was begun with start_enum).
10234 : : DECL_LOC is the location of the enumerator.
10235 : : LOC is the location of the '=' operator if any, DECL_LOC otherwise.
10236 : : Return a tree-list containing the CONST_DECL and its value.
10237 : : Assignment of sequential values by default is handled here. */
10238 : :
10239 : : tree
10240 : 5557343 : build_enumerator (location_t decl_loc, location_t loc,
10241 : : struct c_enum_contents *the_enum, tree name, tree value)
10242 : : {
10243 : 5557343 : tree decl;
10244 : 5557343 : tree old_decl;
10245 : :
10246 : : /* Validate and default VALUE. */
10247 : :
10248 : 5557343 : if (value != NULL_TREE)
10249 : : {
10250 : : /* Don't issue more errors for error_mark_node (i.e. an
10251 : : undeclared identifier) - just ignore the value expression. */
10252 : 3438615 : if (value == error_mark_node)
10253 : : value = NULL_TREE;
10254 : 3437950 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
10255 : : {
10256 : 6 : error_at (loc, "enumerator value for %qE is not an integer constant",
10257 : : name);
10258 : 6 : value = NULL_TREE;
10259 : : }
10260 : : else
10261 : : {
10262 : 3437944 : if (TREE_CODE (value) != INTEGER_CST)
10263 : : {
10264 : 97 : value = c_fully_fold (value, false, NULL);
10265 : 97 : if (TREE_CODE (value) == INTEGER_CST)
10266 : 48 : pedwarn (loc, OPT_Wpedantic,
10267 : : "enumerator value for %qE is not an integer "
10268 : : "constant expression", name);
10269 : : }
10270 : 3437944 : if (TREE_CODE (value) != INTEGER_CST)
10271 : : {
10272 : 49 : error ("enumerator value for %qE is not an integer constant",
10273 : : name);
10274 : 49 : value = NULL_TREE;
10275 : : }
10276 : : else
10277 : : {
10278 : 3437895 : value = default_conversion (value);
10279 : 3437895 : constant_expression_warning (value);
10280 : : }
10281 : : }
10282 : : }
10283 : :
10284 : : /* Default based on previous value. */
10285 : : /* It should no longer be possible to have NON_LVALUE_EXPR
10286 : : in the default. */
10287 : 3437950 : if (value == NULL_TREE)
10288 : : {
10289 : 2119448 : value = the_enum->enum_next_value;
10290 : 2119448 : if (the_enum->enum_overflow)
10291 : 8 : error_at (loc, "overflow in enumeration values");
10292 : : }
10293 : 5557343 : if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10294 : : {
10295 : : /* Enumeration constants must fit in the fixed underlying type. */
10296 : 163 : if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (the_enum->enum_type)))
10297 : 4 : error_at (loc,
10298 : : "enumerator value outside the range of underlying type");
10299 : : /* Enumeration constants for an enum with fixed underlying type
10300 : : have the enum type, both inside and outside the
10301 : : definition. */
10302 : 163 : value = convert (the_enum->enum_type, value);
10303 : : }
10304 : 5557180 : else if (flag_isoc23
10305 : 5144838 : && (old_decl = lookup_name_in_scope (name, current_scope))
10306 : 35 : && old_decl != error_mark_node
10307 : 35 : && TREE_TYPE (old_decl)
10308 : 35 : && TREE_TYPE (TREE_TYPE (old_decl))
10309 : 5557214 : && TREE_CODE (old_decl) == CONST_DECL)
10310 : : {
10311 : : /* Enumeration constants in a redeclaration have the previous type. */
10312 : 34 : tree previous_type = TREE_TYPE (DECL_INITIAL (old_decl));
10313 : 34 : if (!int_fits_type_p (value, previous_type))
10314 : : {
10315 : 2 : error_at (loc, "value of redeclared enumerator outside the range "
10316 : : "of %qT", previous_type);
10317 : 2 : locate_old_decl (old_decl);
10318 : : }
10319 : 34 : value = convert (previous_type, value);
10320 : : }
10321 : : else
10322 : : {
10323 : : /* Even though the underlying type of an enum is unspecified, the
10324 : : type of enumeration constants is explicitly defined as int
10325 : : (6.4.4.3/2 in the C99 Standard). C23 allows any integer type, and
10326 : : GCC allows such types for older standards as an extension. */
10327 : 5557146 : bool warned_range = false;
10328 : 5557146 : if (!int_fits_type_p (value,
10329 : 5557146 : (TYPE_UNSIGNED (TREE_TYPE (value))
10330 : : ? uintmax_type_node
10331 : : : intmax_type_node)))
10332 : : /* GCC does not consider its types larger than intmax_t to be
10333 : : extended integer types (although C23 would permit such types to
10334 : : be considered extended integer types if all the features
10335 : : required by <stdint.h> and <inttypes.h> macros, such as support
10336 : : for integer constants and I/O, were present), so diagnose if
10337 : : such a wider type is used. (If the wider type arose from a
10338 : : constant of such a type, that will also have been diagnosed,
10339 : : but this is the only diagnostic in the case where it arises
10340 : : from choosing a wider type automatically when adding 1
10341 : : overflows.) */
10342 : 11 : warned_range = pedwarn (loc, OPT_Wpedantic,
10343 : : "enumerator value outside the range of %qs",
10344 : 11 : TYPE_UNSIGNED (TREE_TYPE (value))
10345 : : ? "uintmax_t"
10346 : : : "intmax_t");
10347 : 5557146 : if (!warned_range && !int_fits_type_p (value, integer_type_node))
10348 : 4662 : pedwarn_c11 (loc, OPT_Wpedantic,
10349 : : "ISO C restricts enumerator values to range of %<int%> "
10350 : : "before C23");
10351 : :
10352 : : /* The ISO C Standard mandates enumerators to have type int before
10353 : : C23, even though the underlying type of an enum type is
10354 : : unspecified. C23 allows enumerators of any integer type. During
10355 : : the parsing of the enumeration, C23 specifies that constants
10356 : : representable in int have type int, constants not representable
10357 : : in int have the type of the given expression if any, and
10358 : : constants not representable in int and derived by adding 1 to the
10359 : : previous constant have the type of that constant unless the
10360 : : addition would overflow or wraparound, in which case a wider type
10361 : : of the same signedness is chosen automatically; after the
10362 : : enumeration is parsed, all the constants have the type of the
10363 : : enumeration if any do not fit in int. */
10364 : 5557146 : if (int_fits_type_p (value, integer_type_node))
10365 : 5552480 : value = convert (integer_type_node, value);
10366 : : }
10367 : :
10368 : : /* Set basis for default for next value. */
10369 : 5557343 : if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10370 : : {
10371 : 163 : tree underlying_type = ENUM_UNDERLYING_TYPE (the_enum->enum_type);
10372 : 163 : if (TREE_CODE (underlying_type) == BOOLEAN_TYPE)
10373 : : /* A value of 2 following a value of 1 overflows bool, but we
10374 : : cannot carry out addition directly on bool without
10375 : : promotion, and converting the result of arithmetic in a
10376 : : wider type back to bool would not produce the right result
10377 : : for this overflow check. */
10378 : 61 : the_enum->enum_next_value = invert_truthvalue_loc (loc, value);
10379 : : else
10380 : 102 : the_enum->enum_next_value
10381 : 102 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10382 : : PLUS_EXPR, convert (underlying_type, value),
10383 : : convert (underlying_type, integer_one_node),
10384 : : false);
10385 : : }
10386 : : else
10387 : : {
10388 : : /* In a redeclaration the type can already be the enumeral type. */
10389 : 5557180 : if (TREE_CODE (TREE_TYPE (value)) == ENUMERAL_TYPE)
10390 : 12 : value = convert (ENUM_UNDERLYING_TYPE (TREE_TYPE (value)), value);
10391 : 5557180 : the_enum->enum_next_value
10392 : 5557180 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10393 : : PLUS_EXPR, value, integer_one_node, false);
10394 : : }
10395 : 5557343 : the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
10396 : 5557343 : if (the_enum->enum_overflow
10397 : 5557343 : && !ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10398 : : {
10399 : : /* Choose a wider type with the same signedness if
10400 : : available. */
10401 : 3293 : int prec = TYPE_PRECISION (TREE_TYPE (value)) + 1;
10402 : 3293 : bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (value));
10403 : 3293 : tree new_type = (unsignedp
10404 : 3293 : ? long_unsigned_type_node
10405 : : : long_integer_type_node);
10406 : 3293 : if (prec > TYPE_PRECISION (new_type))
10407 : 2922 : new_type = (unsignedp
10408 : 2922 : ? long_long_unsigned_type_node
10409 : : : long_long_integer_type_node);
10410 : 3293 : if (prec > TYPE_PRECISION (new_type))
10411 : 2460 : new_type = (unsignedp
10412 : 2460 : ? widest_unsigned_literal_type_node
10413 : : : widest_integer_literal_type_node);
10414 : 3293 : if (prec <= TYPE_PRECISION (new_type))
10415 : : {
10416 : 3288 : the_enum->enum_overflow = false;
10417 : 3288 : the_enum->enum_next_value
10418 : 3288 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10419 : : PLUS_EXPR, convert (new_type, value),
10420 : : integer_one_node, false);
10421 : 3288 : gcc_assert (!tree_int_cst_lt (the_enum->enum_next_value, value));
10422 : : }
10423 : : }
10424 : :
10425 : : /* Now create a declaration for the enum value name. */
10426 : :
10427 : 5557343 : decl = build_decl (decl_loc, CONST_DECL, name, TREE_TYPE (value));
10428 : 5557343 : DECL_INITIAL (decl) = value;
10429 : 5557343 : DECL_CONTEXT (decl) = the_enum->enum_type;
10430 : 5557343 : pushdecl (decl);
10431 : :
10432 : 5557343 : return tree_cons (decl, value, NULL_TREE);
10433 : : }
10434 : :
10435 : : /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
10436 : :
10437 : : tree
10438 : 0 : c_simulate_enum_decl (location_t loc, const char *name,
10439 : : vec<string_int_pair> *values_ptr)
10440 : : {
10441 : 0 : location_t saved_loc = input_location;
10442 : 0 : input_location = loc;
10443 : :
10444 : 0 : struct c_enum_contents the_enum;
10445 : 0 : tree enumtype = start_enum (loc, &the_enum, get_identifier (name),
10446 : : NULL_TREE, false);
10447 : :
10448 : 0 : tree value_chain = NULL_TREE;
10449 : 0 : string_int_pair *value;
10450 : 0 : vec<string_int_pair> values = *values_ptr;
10451 : 0 : unsigned int i;
10452 : 0 : FOR_EACH_VEC_ELT (values, i, value)
10453 : : {
10454 : 0 : tree decl = build_enumerator (loc, loc, &the_enum,
10455 : : get_identifier (value->first),
10456 : : build_int_cst (integer_type_node,
10457 : 0 : value->second));
10458 : 0 : TREE_CHAIN (decl) = value_chain;
10459 : 0 : value_chain = decl;
10460 : : }
10461 : :
10462 : 0 : finish_enum (enumtype, nreverse (value_chain), NULL_TREE);
10463 : :
10464 : 0 : input_location = saved_loc;
10465 : 0 : return enumtype;
10466 : : }
10467 : :
10468 : : /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
10469 : :
10470 : : tree
10471 : 0 : c_simulate_record_decl (location_t loc, const char *name,
10472 : : array_slice<const tree> fields)
10473 : : {
10474 : 0 : location_t saved_loc = input_location;
10475 : 0 : input_location = loc;
10476 : :
10477 : 0 : class c_struct_parse_info *struct_info;
10478 : 0 : tree ident = get_identifier (name);
10479 : 0 : tree type = start_struct (loc, RECORD_TYPE, ident, &struct_info);
10480 : :
10481 : 0 : for (unsigned int i = 0; i < fields.size (); ++i)
10482 : : {
10483 : 0 : DECL_FIELD_CONTEXT (fields[i]) = type;
10484 : 0 : if (i > 0)
10485 : 0 : DECL_CHAIN (fields[i - 1]) = fields[i];
10486 : : }
10487 : :
10488 : 0 : finish_struct (loc, type, fields[0], NULL_TREE, struct_info);
10489 : :
10490 : 0 : tree decl = build_decl (loc, TYPE_DECL, ident, type);
10491 : 0 : set_underlying_type (decl);
10492 : 0 : lang_hooks.decls.pushdecl (decl);
10493 : :
10494 : 0 : input_location = saved_loc;
10495 : 0 : return type;
10496 : : }
10497 : :
10498 : : /* Create the FUNCTION_DECL for a function definition.
10499 : : DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
10500 : : the declaration; they describe the function's name and the type it returns,
10501 : : but twisted together in a fashion that parallels the syntax of C.
10502 : :
10503 : : This function creates a binding context for the function body
10504 : : as well as setting up the FUNCTION_DECL in current_function_decl.
10505 : :
10506 : : Returns true on success. If the DECLARATOR is not suitable for a function
10507 : : (it defines a datum instead), we return false to report a parse error. */
10508 : :
10509 : : bool
10510 : 35380200 : start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
10511 : : tree attributes)
10512 : : {
10513 : 35380200 : tree decl1, old_decl;
10514 : 35380200 : tree restype, resdecl;
10515 : 35380200 : location_t loc;
10516 : 35380200 : location_t result_loc;
10517 : 35380200 : tree expr = NULL;
10518 : :
10519 : 35380200 : current_function_returns_value = 0; /* Assume, until we see it does. */
10520 : 35380200 : current_function_returns_null = 0;
10521 : 35380200 : current_function_returns_abnormally = 0;
10522 : 35380200 : warn_about_return_type = 0;
10523 : 35380200 : c_switch_stack = NULL;
10524 : :
10525 : : /* Indicate no valid break/continue context. */
10526 : 35380200 : in_statement = 0;
10527 : :
10528 : 35380200 : decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
10529 : : &attributes, &expr, NULL, DEPRECATED_NORMAL);
10530 : 35380200 : invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
10531 : :
10532 : : /* If the declarator is not suitable for a function definition,
10533 : : cause a syntax error. */
10534 : 35380200 : if (decl1 == NULL_TREE
10535 : 35380169 : || TREE_CODE (decl1) != FUNCTION_DECL)
10536 : : return false;
10537 : :
10538 : : /* Nested functions may have variably modified (return) type.
10539 : : Make sure the size expression is evaluated at this point. */
10540 : 35380162 : if (expr && !current_scope->parm_flag)
10541 : 11 : add_stmt (fold_convert (void_type_node, expr));
10542 : :
10543 : 35380162 : loc = DECL_SOURCE_LOCATION (decl1);
10544 : :
10545 : : /* A nested function is not global. */
10546 : 35380162 : if (current_function_decl != NULL_TREE)
10547 : 1575 : TREE_PUBLIC (decl1) = 0;
10548 : :
10549 : 35380162 : c_decl_attributes (&decl1, attributes, 0);
10550 : :
10551 : 35380162 : if (DECL_DECLARED_INLINE_P (decl1)
10552 : 34704700 : && DECL_UNINLINABLE (decl1)
10553 : 35380172 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
10554 : : {
10555 : 3 : auto_urlify_attributes sentinel;
10556 : 3 : warning_at (loc, OPT_Wattributes,
10557 : : "inline function %qD given attribute %qs",
10558 : : decl1, "noinline");
10559 : 3 : }
10560 : :
10561 : : /* Handle gnu_inline attribute. */
10562 : 35380162 : if (declspecs->inline_p
10563 : 34704703 : && !flag_gnu89_inline
10564 : 34670741 : && TREE_CODE (decl1) == FUNCTION_DECL
10565 : 70050903 : && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
10566 : 155078 : || current_function_decl))
10567 : : {
10568 : 34515735 : if (declspecs->storage_class != csc_static)
10569 : 34515729 : DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
10570 : : }
10571 : :
10572 : 35380162 : announce_function (decl1);
10573 : :
10574 : 35380162 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
10575 : : {
10576 : 1 : error_at (loc, "return type is an incomplete type");
10577 : : /* Make it return void instead. */
10578 : 1 : TREE_TYPE (decl1)
10579 : 2 : = c_build_function_type (void_type_node,
10580 : 1 : TYPE_ARG_TYPES (TREE_TYPE (decl1)),
10581 : 1 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl1)));
10582 : : }
10583 : :
10584 : 35380162 : if (warn_about_return_type)
10585 : 1404 : permerror_opt (loc, flag_isoc99 ? OPT_Wimplicit_int
10586 : 0 : : (warn_return_type > 0 ? OPT_Wreturn_type
10587 : : : OPT_Wimplicit_int),
10588 : : "return type defaults to %<int%>");
10589 : :
10590 : : /* Make the init_value nonzero so pushdecl knows this is not tentative.
10591 : : error_mark_node is replaced below (in pop_scope) with the BLOCK. */
10592 : 35380162 : DECL_INITIAL (decl1) = error_mark_node;
10593 : :
10594 : : /* If this definition isn't a prototype and we had a prototype declaration
10595 : : before, copy the arg type info from that prototype. */
10596 : 35380162 : old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
10597 : 35380162 : if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
10598 : 35154346 : old_decl = NULL_TREE;
10599 : :
10600 : 35380162 : current_function_prototype_locus = UNKNOWN_LOCATION;
10601 : 35380162 : current_function_prototype_built_in = false;
10602 : 35380162 : current_function_prototype_arg_types = NULL_TREE;
10603 : 35380162 : tree newtype = TREE_TYPE (decl1);
10604 : 35380162 : tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype;
10605 : 35380162 : if (!prototype_p (newtype))
10606 : : {
10607 : 13111 : tree oldrt = TREE_TYPE (oldtype);
10608 : 13111 : tree newrt = TREE_TYPE (newtype);
10609 : 13111 : if (old_decl != NULL_TREE
10610 : 315 : && TREE_CODE (oldtype) == FUNCTION_TYPE
10611 : 13426 : && comptypes (oldrt, newrt))
10612 : : {
10613 : 296 : if (stdarg_p (oldtype))
10614 : : {
10615 : 1 : auto_diagnostic_group d;
10616 : 1 : warning_at (loc, 0, "%q+D defined as variadic function "
10617 : : "without prototype", decl1);
10618 : 1 : locate_old_decl (old_decl);
10619 : 1 : }
10620 : 296 : TREE_TYPE (decl1) = composite_type (oldtype, newtype);
10621 : 296 : current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
10622 : 296 : current_function_prototype_built_in
10623 : 296 : = C_DECL_BUILTIN_PROTOTYPE (old_decl);
10624 : 296 : current_function_prototype_arg_types
10625 : 296 : = TYPE_ARG_TYPES (newtype);
10626 : : }
10627 : 13111 : if (TREE_PUBLIC (decl1))
10628 : : {
10629 : : /* If there is an external prototype declaration of this
10630 : : function, record its location but do not copy information
10631 : : to this decl. This may be an invisible declaration
10632 : : (built-in or in a scope which has finished) or simply
10633 : : have more refined argument types than any declaration
10634 : : found above. */
10635 : 12697 : struct c_binding *b;
10636 : 12972 : for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
10637 : 711 : if (B_IN_SCOPE (b, external_scope))
10638 : : break;
10639 : 12697 : if (b)
10640 : : {
10641 : 436 : tree ext_decl, ext_type;
10642 : 436 : ext_decl = b->decl;
10643 : 436 : ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
10644 : 436 : if (TREE_CODE (ext_type) == FUNCTION_TYPE
10645 : 872 : && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
10646 : 436 : TREE_TYPE (ext_type)))
10647 : : {
10648 : 416 : current_function_prototype_locus
10649 : 416 : = DECL_SOURCE_LOCATION (ext_decl);
10650 : 416 : current_function_prototype_built_in
10651 : 416 : = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
10652 : 416 : current_function_prototype_arg_types
10653 : 416 : = TYPE_ARG_TYPES (ext_type);
10654 : : }
10655 : : }
10656 : : }
10657 : : }
10658 : :
10659 : : /* Optionally warn about C23 compatibility. */
10660 : 35380162 : if (warn_deprecated_non_prototype
10661 : 34 : && old_decl != NULL_TREE
10662 : 5 : && TREE_CODE (oldtype) == FUNCTION_TYPE
10663 : 5 : && !TYPE_ARG_TYPES (oldtype)
10664 : 5 : && !TYPE_NO_NAMED_ARGS_STDARG_P (oldtype)
10665 : 35380167 : && (TYPE_ARG_TYPES (newtype)
10666 : 3 : && TREE_VALUE (TYPE_ARG_TYPES (newtype)) != void_type_node))
10667 : : {
10668 : 3 : bool warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
10669 : : "ISO C23 does not allow defining"
10670 : : " parameters for function %qE declared"
10671 : : " without parameters",
10672 : : decl1);
10673 : 3 : if (warned)
10674 : 3 : inform (DECL_SOURCE_LOCATION (old_decl), "declared here");
10675 : : }
10676 : :
10677 : : /* Optionally warn of old-fashioned def with no previous prototype. */
10678 : 35380162 : if (warn_strict_prototypes
10679 : 107308 : && old_decl != error_mark_node
10680 : 107308 : && !prototype_p (TREE_TYPE (decl1))
10681 : 35380163 : && C_DECL_ISNT_PROTOTYPE (old_decl))
10682 : 1 : warning_at (loc, OPT_Wstrict_prototypes,
10683 : : "function declaration isn%'t a prototype");
10684 : : /* Optionally warn of any global def with no previous prototype. */
10685 : 35380161 : else if (warn_missing_prototypes
10686 : 107009 : && old_decl != error_mark_node
10687 : 107009 : && TREE_PUBLIC (decl1)
10688 : 73916 : && !MAIN_NAME_P (DECL_NAME (decl1))
10689 : 73880 : && C_DECL_ISNT_PROTOTYPE (old_decl)
10690 : 35380802 : && !DECL_DECLARED_INLINE_P (decl1))
10691 : 4 : warning_at (loc, OPT_Wmissing_prototypes,
10692 : : "no previous prototype for %qD", decl1);
10693 : : /* Optionally warn of any def with no previous prototype
10694 : : if the function has already been used. */
10695 : 35380157 : else if (warn_missing_prototypes
10696 : 107005 : && old_decl != NULL_TREE
10697 : 74243 : && old_decl != error_mark_node
10698 : 74243 : && TREE_USED (old_decl)
10699 : 35387246 : && !prototype_p (TREE_TYPE (old_decl)))
10700 : 0 : warning_at (loc, OPT_Wmissing_prototypes,
10701 : : "%qD was used with no prototype before its definition", decl1);
10702 : : /* Optionally warn of any global def with no previous declaration. */
10703 : 35380157 : else if (warn_missing_declarations
10704 : 4 : && TREE_PUBLIC (decl1)
10705 : 3 : && old_decl == NULL_TREE
10706 : 3 : && !MAIN_NAME_P (DECL_NAME (decl1))
10707 : 35380160 : && !DECL_DECLARED_INLINE_P (decl1))
10708 : 2 : warning_at (loc, OPT_Wmissing_declarations,
10709 : : "no previous declaration for %qD",
10710 : : decl1);
10711 : : /* Optionally warn of any def with no previous declaration
10712 : : if the function has already been used. */
10713 : 35380155 : else if (warn_missing_declarations
10714 : 2 : && old_decl != NULL_TREE
10715 : 0 : && old_decl != error_mark_node
10716 : 0 : && TREE_USED (old_decl)
10717 : 35380155 : && C_DECL_IMPLICIT (old_decl))
10718 : 0 : warning_at (loc, OPT_Wmissing_declarations,
10719 : : "%qD was used with no declaration before its definition", decl1);
10720 : :
10721 : : /* This function exists in static storage.
10722 : : (This does not mean `static' in the C sense!) */
10723 : 35380162 : TREE_STATIC (decl1) = 1;
10724 : :
10725 : : /* This is the earliest point at which we might know the assembler
10726 : : name of the function. Thus, if it's set before this, die horribly. */
10727 : 35380162 : gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
10728 : :
10729 : : /* If #pragma weak was used, mark the decl weak now. */
10730 : 35380162 : if (current_scope == file_scope)
10731 : 35378587 : maybe_apply_pragma_weak (decl1);
10732 : :
10733 : : /* Warn for unlikely, improbable, or stupid declarations of `main'. */
10734 : 35380162 : if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
10735 : : {
10736 : 3197 : if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
10737 : 3197 : != integer_type_node)
10738 : 5 : pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
10739 : 3192 : else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
10740 : 1 : pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
10741 : : decl1);
10742 : :
10743 : 3197 : check_main_parameter_types (decl1);
10744 : :
10745 : 3197 : if (!TREE_PUBLIC (decl1))
10746 : 0 : pedwarn (loc, OPT_Wmain,
10747 : : "%qD is normally a non-static function", decl1);
10748 : : }
10749 : :
10750 : 35380162 : tree parms = current_function_arg_info->parms;
10751 : 35380162 : if (old_decl)
10752 : : {
10753 : 225816 : location_t origloc = DECL_SOURCE_LOCATION (old_decl);
10754 : 225816 : warn_parm_array_mismatch (origloc, old_decl, parms);
10755 : : }
10756 : :
10757 : : /* Record the decl so that the function name is defined.
10758 : : If we already have a decl for this name, and it is a FUNCTION_DECL,
10759 : : use the old decl. */
10760 : :
10761 : 35380162 : current_function_decl = pushdecl (decl1);
10762 : :
10763 : 35380162 : if (tree access = build_attr_access_from_parms (parms, false))
10764 : 56223 : decl_attributes (¤t_function_decl, access, ATTR_FLAG_INTERNAL,
10765 : : old_decl);
10766 : :
10767 : 35380162 : push_scope ();
10768 : 35380162 : declare_parm_level ();
10769 : :
10770 : : /* Set the result decl source location to the location of the typespec. */
10771 : 4002336 : result_loc = (declspecs->locations[cdw_typespec] == UNKNOWN_LOCATION
10772 : 35380162 : ? loc : declspecs->locations[cdw_typespec]);
10773 : 35380162 : restype = TREE_TYPE (TREE_TYPE (current_function_decl));
10774 : 35380162 : resdecl = build_decl (result_loc, RESULT_DECL, NULL_TREE, restype);
10775 : 35380162 : DECL_ARTIFICIAL (resdecl) = 1;
10776 : 35380162 : DECL_IGNORED_P (resdecl) = 1;
10777 : 35380162 : DECL_RESULT (current_function_decl) = resdecl;
10778 : :
10779 : 35380162 : start_fname_decls ();
10780 : :
10781 : 35380162 : return true;
10782 : : }
10783 : :
10784 : : /* Subroutine of store_parm_decls which handles new-style function
10785 : : definitions (prototype format). The parms already have decls, so we
10786 : : need only record them as in effect and complain if any redundant
10787 : : old-style parm decls were written. */
10788 : : static void
10789 : 35367064 : store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
10790 : : {
10791 : 35367064 : tree decl;
10792 : 35367064 : c_arg_tag *tag;
10793 : 35367064 : unsigned ix;
10794 : :
10795 : 35367064 : if (current_scope->bindings)
10796 : : {
10797 : 7 : error_at (DECL_SOURCE_LOCATION (fndecl),
10798 : : "old-style parameter declarations in prototyped "
10799 : : "function definition");
10800 : :
10801 : : /* Get rid of the old-style declarations. */
10802 : 7 : pop_scope ();
10803 : 7 : push_scope ();
10804 : : }
10805 : : /* Don't issue this warning for nested functions, and don't issue this
10806 : : warning if we got here because ARG_INFO_TYPES was error_mark_node
10807 : : (this happens when a function definition has just an ellipsis in
10808 : : its parameter list). */
10809 : 35367057 : else if (!in_system_header_at (input_location)
10810 : 625107 : && !current_function_scope
10811 : 623679 : && arg_info->types != error_mark_node
10812 : 35990736 : && !arg_info->c23_empty_parens)
10813 : 572715 : warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
10814 : : "traditional C rejects ISO C style function definitions");
10815 : :
10816 : : /* Now make all the parameter declarations visible in the function body.
10817 : : We can bypass most of the grunt work of pushdecl. */
10818 : 132843109 : for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
10819 : : {
10820 : 97476045 : DECL_CONTEXT (decl) = current_function_decl;
10821 : 97476045 : if (DECL_NAME (decl))
10822 : : {
10823 : 97475423 : bind (DECL_NAME (decl), decl, current_scope,
10824 : : /*invisible=*/false, /*nested=*/false,
10825 : : UNKNOWN_LOCATION);
10826 : 97475423 : if (!TREE_USED (decl))
10827 : 97458178 : warn_if_shadowing (decl);
10828 : : }
10829 : : else
10830 : 622 : pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_parameter_name,
10831 : : "ISO C does not support omitting parameter names in "
10832 : : "function definitions before C23");
10833 : : }
10834 : :
10835 : : /* Record the parameter list in the function declaration. */
10836 : 35367064 : DECL_ARGUMENTS (fndecl) = arg_info->parms;
10837 : :
10838 : : /* Now make all the ancillary declarations visible, likewise. */
10839 : 35367120 : for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
10840 : : {
10841 : 56 : DECL_CONTEXT (decl) = current_function_decl;
10842 : 56 : if (DECL_NAME (decl))
10843 : 0 : bind (DECL_NAME (decl), decl, current_scope,
10844 : : /*invisible=*/false,
10845 : 0 : /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
10846 : : UNKNOWN_LOCATION);
10847 : : }
10848 : :
10849 : : /* And all the tag declarations. */
10850 : 35367174 : FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
10851 : 56 : if (tag->id)
10852 : 23 : bind (tag->id, tag->type, current_scope,
10853 : : /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
10854 : 35367064 : }
10855 : :
10856 : : /* Subroutine of store_parm_decls which handles old-style function
10857 : : definitions (separate parameter list and declarations). */
10858 : :
10859 : : static void
10860 : 13098 : store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
10861 : : {
10862 : 13098 : struct c_binding *b;
10863 : 13098 : tree parm, decl, last;
10864 : 13098 : tree parmids = arg_info->parms;
10865 : 13098 : hash_set<tree> seen_args;
10866 : :
10867 : 13098 : if (!in_system_header_at (input_location))
10868 : : {
10869 : 13095 : if (flag_isoc23)
10870 : 1349 : pedwarn (DECL_SOURCE_LOCATION (fndecl),
10871 : 1349 : OPT_Wold_style_definition, "old-style function definition");
10872 : : else
10873 : 11746 : warning_at (DECL_SOURCE_LOCATION (fndecl),
10874 : 11746 : OPT_Wold_style_definition,
10875 : : "old-style function definition");
10876 : : }
10877 : :
10878 : 13098 : if (current_scope->had_vla_unspec)
10879 : 1 : error ("%<[*]%> not allowed in other than function prototype scope");
10880 : :
10881 : : /* Match each formal parameter name with its declaration. Save each
10882 : : decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
10883 : 47367 : for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10884 : : {
10885 : 34269 : if (TREE_VALUE (parm) == NULL_TREE)
10886 : : {
10887 : 0 : error_at (DECL_SOURCE_LOCATION (fndecl),
10888 : : "parameter name missing from parameter list");
10889 : 0 : TREE_PURPOSE (parm) = NULL_TREE;
10890 : 0 : continue;
10891 : : }
10892 : :
10893 : 34269 : b = I_SYMBOL_BINDING (TREE_VALUE (parm));
10894 : 34269 : if (b && B_IN_CURRENT_SCOPE (b))
10895 : : {
10896 : 22649 : decl = b->decl;
10897 : : /* Skip erroneous parameters. */
10898 : 22649 : if (decl == error_mark_node)
10899 : 2 : continue;
10900 : : /* If we got something other than a PARM_DECL it is an error. */
10901 : 22647 : if (TREE_CODE (decl) != PARM_DECL)
10902 : : {
10903 : 7 : error_at (DECL_SOURCE_LOCATION (decl),
10904 : : "%qD declared as a non-parameter", decl);
10905 : 7 : continue;
10906 : : }
10907 : : /* If the declaration is already marked, we have a duplicate
10908 : : name. Complain and ignore the duplicate. */
10909 : 22640 : else if (seen_args.contains (decl))
10910 : : {
10911 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
10912 : : "multiple parameters named %qD", decl);
10913 : 0 : TREE_PURPOSE (parm) = NULL_TREE;
10914 : 0 : continue;
10915 : : }
10916 : : /* If the declaration says "void", complain and turn it into
10917 : : an int. */
10918 : 22640 : else if (VOID_TYPE_P (TREE_TYPE (decl)))
10919 : : {
10920 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
10921 : : "parameter %qD declared with void type", decl);
10922 : 0 : TREE_TYPE (decl) = integer_type_node;
10923 : 0 : DECL_ARG_TYPE (decl) = integer_type_node;
10924 : 0 : layout_decl (decl, 0);
10925 : : }
10926 : 22640 : warn_if_shadowing (decl);
10927 : : }
10928 : : /* If no declaration found, default to int. */
10929 : : else
10930 : : {
10931 : : /* FIXME diagnostics: This should be the location of the argument,
10932 : : not the FNDECL. E.g., for an old-style declaration
10933 : :
10934 : : int f10(v) { blah; }
10935 : :
10936 : : We should use the location of the V, not the F10.
10937 : : Unfortunately, the V is an IDENTIFIER_NODE which has no
10938 : : location. In the future we need locations for c_arg_info
10939 : : entries.
10940 : :
10941 : : See gcc.dg/Wshadow-3.c for an example of this problem. */
10942 : 11620 : decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
10943 : 11620 : PARM_DECL, TREE_VALUE (parm), integer_type_node);
10944 : 11620 : DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
10945 : 11620 : pushdecl (decl);
10946 : 11620 : warn_if_shadowing (decl);
10947 : :
10948 : 11620 : if (flag_isoc99)
10949 : 118 : permerror_opt (DECL_SOURCE_LOCATION (decl),
10950 : 118 : OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
10951 : : decl);
10952 : : else
10953 : 11502 : warning_at (DECL_SOURCE_LOCATION (decl),
10954 : 11502 : OPT_Wmissing_parameter_type,
10955 : : "type of %qD defaults to %<int%>", decl);
10956 : : }
10957 : :
10958 : 34260 : TREE_PURPOSE (parm) = decl;
10959 : 34260 : seen_args.add (decl);
10960 : : }
10961 : :
10962 : : /* Now examine the parms chain for incomplete declarations
10963 : : and declarations with no corresponding names. */
10964 : :
10965 : 47446 : for (b = current_scope->bindings; b; b = b->prev)
10966 : : {
10967 : 34348 : parm = b->decl;
10968 : 34348 : if (TREE_CODE (parm) != PARM_DECL)
10969 : 79 : continue;
10970 : :
10971 : 34269 : if (TREE_TYPE (parm) != error_mark_node
10972 : 34269 : && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
10973 : : {
10974 : 0 : error_at (DECL_SOURCE_LOCATION (parm),
10975 : : "parameter %qD has incomplete type", parm);
10976 : 0 : TREE_TYPE (parm) = error_mark_node;
10977 : : }
10978 : :
10979 : 34269 : if (!seen_args.contains (parm))
10980 : : {
10981 : 9 : error_at (DECL_SOURCE_LOCATION (parm),
10982 : : "declaration for parameter %qD but no such parameter",
10983 : : parm);
10984 : :
10985 : : /* Pretend the parameter was not missing.
10986 : : This gets us to a standard state and minimizes
10987 : : further error messages. */
10988 : 9 : parmids = chainon (parmids, tree_cons (parm, 0, 0));
10989 : : }
10990 : : }
10991 : :
10992 : : /* Chain the declarations together in the order of the list of
10993 : : names. Store that chain in the function decl, replacing the
10994 : : list of names. Update the current scope to match. */
10995 : 13098 : DECL_ARGUMENTS (fndecl) = NULL_TREE;
10996 : :
10997 : 13106 : for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10998 : 8680 : if (TREE_PURPOSE (parm))
10999 : : break;
11000 : 13098 : if (parm && TREE_PURPOSE (parm))
11001 : : {
11002 : 8672 : last = TREE_PURPOSE (parm);
11003 : 8672 : DECL_ARGUMENTS (fndecl) = last;
11004 : :
11005 : 34270 : for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
11006 : 25598 : if (TREE_PURPOSE (parm))
11007 : : {
11008 : 25597 : DECL_CHAIN (last) = TREE_PURPOSE (parm);
11009 : 25598 : last = TREE_PURPOSE (parm);
11010 : : }
11011 : 8672 : DECL_CHAIN (last) = NULL_TREE;
11012 : : }
11013 : :
11014 : : /* If there was a previous prototype,
11015 : : set the DECL_ARG_TYPE of each argument according to
11016 : : the type previously specified, and report any mismatches. */
11017 : :
11018 : 13098 : if (current_function_prototype_arg_types)
11019 : : {
11020 : 171 : tree type;
11021 : 171 : for (parm = DECL_ARGUMENTS (fndecl),
11022 : 171 : type = current_function_prototype_arg_types;
11023 : 373 : parm || (type != NULL_TREE
11024 : 166 : && TREE_VALUE (type) != error_mark_node
11025 : 166 : && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
11026 : 202 : parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
11027 : : {
11028 : 212 : if (parm == NULL_TREE
11029 : 206 : || type == NULL_TREE
11030 : 418 : || (TREE_VALUE (type) != error_mark_node
11031 : 192 : && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
11032 : : {
11033 : 10 : if (current_function_prototype_built_in)
11034 : 6 : warning_at (DECL_SOURCE_LOCATION (fndecl),
11035 : 6 : 0, "number of arguments doesn%'t match "
11036 : : "built-in prototype");
11037 : : else
11038 : : {
11039 : : /* FIXME diagnostics: This should be the location of
11040 : : FNDECL, but there is bug when a prototype is
11041 : : declared inside function context, but defined
11042 : : outside of it (e.g., gcc.dg/pr15698-2.c). In
11043 : : which case FNDECL gets the location of the
11044 : : prototype, not the definition. */
11045 : 4 : error_at (input_location,
11046 : : "number of arguments doesn%'t match prototype");
11047 : :
11048 : 4 : error_at (current_function_prototype_locus,
11049 : : "prototype declaration");
11050 : : }
11051 : : break;
11052 : : }
11053 : : /* Type for passing arg must be consistent with that
11054 : : declared for the arg. ISO C says we take the unqualified
11055 : : type for parameters declared with qualified type. */
11056 : 202 : if (TREE_TYPE (parm) != error_mark_node
11057 : 201 : && TREE_VALUE (type) != error_mark_node
11058 : 389 : && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
11059 : 187 : != TYPE_ATOMIC (TREE_VALUE (type)))
11060 : 185 : || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
11061 : 185 : TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
11062 : : {
11063 : 36 : if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
11064 : 36 : == TYPE_ATOMIC (TREE_VALUE (type)))
11065 : 36 : && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
11066 : 34 : == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
11067 : : {
11068 : : /* Adjust argument to match prototype. E.g. a previous
11069 : : `int foo(float);' prototype causes
11070 : : `int foo(x) float x; {...}' to be treated like
11071 : : `int foo(float x) {...}'. This is particularly
11072 : : useful for argument types like uid_t. */
11073 : 17 : DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
11074 : :
11075 : : /* ??? Is it possible to get here with a
11076 : : built-in prototype or will it always have
11077 : : been diagnosed as conflicting with an
11078 : : old-style definition and discarded? */
11079 : 17 : if (current_function_prototype_built_in)
11080 : 2 : warning_at (DECL_SOURCE_LOCATION (parm),
11081 : 2 : OPT_Wpedantic, "promoted argument %qD "
11082 : : "doesn%'t match built-in prototype", parm);
11083 : : else
11084 : : {
11085 : 15 : pedwarn (DECL_SOURCE_LOCATION (parm),
11086 : 15 : OPT_Wpedantic, "promoted argument %qD "
11087 : : "doesn%'t match prototype", parm);
11088 : 15 : pedwarn (current_function_prototype_locus, OPT_Wpedantic,
11089 : : "prototype declaration");
11090 : : }
11091 : : }
11092 : : else
11093 : : {
11094 : 19 : if (current_function_prototype_built_in)
11095 : 10 : warning_at (DECL_SOURCE_LOCATION (parm),
11096 : 10 : 0, "argument %qD doesn%'t match "
11097 : : "built-in prototype", parm);
11098 : : else
11099 : : {
11100 : 9 : error_at (DECL_SOURCE_LOCATION (parm),
11101 : : "argument %qD doesn%'t match prototype", parm);
11102 : 9 : error_at (current_function_prototype_locus,
11103 : : "prototype declaration");
11104 : : }
11105 : : }
11106 : : }
11107 : : }
11108 : 171 : TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
11109 : : }
11110 : :
11111 : : /* Otherwise, create a prototype that would match. */
11112 : :
11113 : : else
11114 : : {
11115 : 12927 : tree actual = NULL_TREE, last = NULL_TREE, type;
11116 : :
11117 : 46990 : for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
11118 : : {
11119 : 34063 : type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
11120 : 34063 : if (last)
11121 : 25547 : TREE_CHAIN (last) = type;
11122 : : else
11123 : : actual = type;
11124 : 34063 : last = type;
11125 : : }
11126 : 12927 : type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
11127 : 12927 : if (last)
11128 : 8516 : TREE_CHAIN (last) = type;
11129 : : else
11130 : : actual = type;
11131 : :
11132 : : /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
11133 : : of the type of this function, but we need to avoid having this
11134 : : affect the types of other similarly-typed functions, so we must
11135 : : first force the generation of an identical (but separate) type
11136 : : node for the relevant function type. The new node we create
11137 : : will be a variant of the main variant of the original function
11138 : : type. */
11139 : :
11140 : 12927 : TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
11141 : :
11142 : 12927 : TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
11143 : : }
11144 : 13098 : }
11145 : :
11146 : : /* Store parameter declarations passed in ARG_INFO into the current
11147 : : function declaration. */
11148 : :
11149 : : void
11150 : 0 : store_parm_decls_from (struct c_arg_info *arg_info)
11151 : : {
11152 : 0 : current_function_arg_info = arg_info;
11153 : 0 : store_parm_decls ();
11154 : 0 : }
11155 : :
11156 : : /* Called by walk_tree to look for and update context-less labels
11157 : : or labels with context in the parent function. */
11158 : :
11159 : : static tree
11160 : 8127 : set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
11161 : : {
11162 : 8127 : tree ctx = static_cast<tree>(data);
11163 : 8127 : if (TREE_CODE (*tp) == LABEL_EXPR
11164 : 8127 : && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
11165 : 2 : || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
11166 : : {
11167 : 72 : DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
11168 : 72 : *walk_subtrees = 0;
11169 : : }
11170 : :
11171 : 8127 : return NULL_TREE;
11172 : : }
11173 : :
11174 : : /* Store the parameter declarations into the current function declaration.
11175 : : This is called after parsing the parameter declarations, before
11176 : : digesting the body of the function.
11177 : :
11178 : : For an old-style definition, construct a prototype out of the old-style
11179 : : parameter declarations and inject it into the function's type. */
11180 : :
11181 : : void
11182 : 35380162 : store_parm_decls (void)
11183 : : {
11184 : 35380162 : tree fndecl = current_function_decl;
11185 : 35380162 : bool proto;
11186 : :
11187 : : /* The argument information block for FNDECL. */
11188 : 35380162 : struct c_arg_info *arg_info = current_function_arg_info;
11189 : 35380162 : current_function_arg_info = 0;
11190 : :
11191 : : /* True if this definition is written with a prototype. In C23, an
11192 : : empty argument list was converted to (void) in grokparms; in
11193 : : older C standard versions, it does not give the function a type
11194 : : with a prototype for future calls. */
11195 : 35380162 : proto = arg_info->types != 0 || arg_info->no_named_args_stdarg_p;
11196 : :
11197 : 13098 : if (proto)
11198 : 35367064 : store_parm_decls_newstyle (fndecl, arg_info);
11199 : : else
11200 : 13098 : store_parm_decls_oldstyle (fndecl, arg_info);
11201 : :
11202 : : /* The next call to push_scope will be a function body. */
11203 : :
11204 : 35380162 : next_is_function_body = true;
11205 : :
11206 : : /* Write a record describing this function definition to the prototypes
11207 : : file (if requested). */
11208 : :
11209 : 35380162 : gen_aux_info_record (fndecl, 1, 0, proto);
11210 : :
11211 : : /* Initialize the RTL code for the function. */
11212 : 35380162 : allocate_struct_function (fndecl, false);
11213 : :
11214 : 35380162 : if (warn_unused_local_typedefs)
11215 : 3108762 : cfun->language = ggc_cleared_alloc<language_function> ();
11216 : :
11217 : : /* Begin the statement tree for this function. */
11218 : 35380162 : DECL_SAVED_TREE (fndecl) = push_stmt_list ();
11219 : :
11220 : : /* ??? Insert the contents of the pending sizes list into the function
11221 : : to be evaluated. The only reason left to have this is
11222 : : void foo(int n, int array[n++])
11223 : : because we throw away the array type in favor of a pointer type, and
11224 : : thus won't naturally see the SAVE_EXPR containing the increment. All
11225 : : other pending sizes would be handled by gimplify_parameters. */
11226 : 35380162 : if (arg_info->pending_sizes)
11227 : : {
11228 : : /* In very special circumstances, e.g. for code like
11229 : : _Atomic int i = 5;
11230 : : void f (int a[i += 2]) {}
11231 : : we need to execute the atomic assignment on function entry.
11232 : : But in this case, it is not just a straight store, it has the
11233 : : op= form, which means that build_atomic_assign has generated
11234 : : gotos, labels, etc. Because at that time the function decl
11235 : : for F has not been created yet, those labels do not have any
11236 : : function context. But we have the fndecl now, so update the
11237 : : labels accordingly. gimplify_expr would crash otherwise.
11238 : : Or with nested functions the labels could be created with parent
11239 : : function's context, while when the statement is emitted at the
11240 : : start of the nested function, it needs the nested function's
11241 : : context. */
11242 : 295 : walk_tree_without_duplicates (&arg_info->pending_sizes,
11243 : : set_labels_context_r, fndecl);
11244 : 295 : add_stmt (arg_info->pending_sizes);
11245 : : }
11246 : 35380162 : }
11247 : :
11248 : : /* Store PARM_DECLs in PARMS into scope temporarily. Used for
11249 : : c_finish_omp_declare_simd for function prototypes. No diagnostics
11250 : : should be done. */
11251 : :
11252 : : void
11253 : 240 : temp_store_parm_decls (tree fndecl, tree parms)
11254 : : {
11255 : 240 : push_scope ();
11256 : 678 : for (tree p = parms; p; p = DECL_CHAIN (p))
11257 : : {
11258 : 438 : DECL_CONTEXT (p) = fndecl;
11259 : 438 : if (DECL_NAME (p))
11260 : 319 : bind (DECL_NAME (p), p, current_scope,
11261 : : /*invisible=*/false, /*nested=*/false,
11262 : : UNKNOWN_LOCATION);
11263 : : }
11264 : 240 : }
11265 : :
11266 : : /* Undo what temp_store_parm_decls did. */
11267 : :
11268 : : void
11269 : 240 : temp_pop_parm_decls (void)
11270 : : {
11271 : : /* Clear all bindings in this temporary scope, so that
11272 : : pop_scope doesn't create a BLOCK. */
11273 : 240 : struct c_binding *b = current_scope->bindings;
11274 : 240 : current_scope->bindings = NULL;
11275 : 564 : for (; b; b = free_binding_and_advance (b))
11276 : : {
11277 : 324 : gcc_assert (TREE_CODE (b->decl) == PARM_DECL
11278 : : || b->decl == error_mark_node);
11279 : 324 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
11280 : 324 : I_SYMBOL_BINDING (b->id) = b->shadowed;
11281 : 324 : if (b->shadowed && b->shadowed->u.type)
11282 : 0 : TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
11283 : : }
11284 : 240 : pop_scope ();
11285 : 240 : }
11286 : :
11287 : :
11288 : : /* Finish up a function declaration and compile that function
11289 : : all the way to assembler language output. Then free the storage
11290 : : for the function definition.
11291 : :
11292 : : This is called after parsing the body of the function definition. */
11293 : :
11294 : : void
11295 : 35380160 : finish_function (location_t end_loc)
11296 : : {
11297 : 35380160 : tree fndecl = current_function_decl;
11298 : :
11299 : 35380160 : if (c_dialect_objc ())
11300 : 0 : objc_finish_function ();
11301 : :
11302 : 35380160 : if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
11303 : 35380158 : BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
11304 : :
11305 : : /* Must mark the RESULT_DECL as being in this function. */
11306 : :
11307 : 35380160 : if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
11308 : 35380160 : DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
11309 : :
11310 : 35427306 : if (MAIN_NAME_P (DECL_NAME (fndecl)) && !TREE_THIS_VOLATILE (fndecl)
11311 : 47145 : && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
11312 : 35427305 : == integer_type_node && flag_isoc99)
11313 : : {
11314 : : /* Hack. We don't want the middle-end to warn that this return
11315 : : is unreachable, so we mark its location as special. Using
11316 : : UNKNOWN_LOCATION has the problem that it gets clobbered in
11317 : : annotate_one_with_locus. A cleaner solution might be to
11318 : : ensure ! should_carry_locus_p (stmt), but that needs a flag.
11319 : : */
11320 : 45781 : c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
11321 : : }
11322 : :
11323 : : /* Tie off the statement tree for this function. */
11324 : 35380160 : DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
11325 : :
11326 : 35380160 : finish_fname_decls ();
11327 : :
11328 : : /* Complain if there's no return statement only if option specified on
11329 : : command line. */
11330 : 35380160 : if (warn_return_type > 0
11331 : 3108785 : && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
11332 : 2913204 : && !current_function_returns_value && !current_function_returns_null
11333 : : /* Don't complain if we are no-return. */
11334 : 72 : && !current_function_returns_abnormally
11335 : : /* Don't complain if we are declared noreturn. */
11336 : 21 : && !TREE_THIS_VOLATILE (fndecl)
11337 : : /* Don't warn for main(). */
11338 : 18 : && !MAIN_NAME_P (DECL_NAME (fndecl))
11339 : : /* Or if they didn't actually specify a return type. */
11340 : 17 : && !C_FUNCTION_IMPLICIT_INT (fndecl)
11341 : : /* Normally, with -Wreturn-type, flow will complain, but we might
11342 : : optimize out static functions. */
11343 : 17 : && !TREE_PUBLIC (fndecl)
11344 : 6 : && targetm.warn_func_return (fndecl)
11345 : 38488945 : && warning (OPT_Wreturn_type,
11346 : : "no return statement in function returning non-void"))
11347 : 5 : suppress_warning (fndecl, OPT_Wreturn_type);
11348 : :
11349 : : /* Complain about parameters that are only set, but never otherwise used. */
11350 : 35380160 : if (warn_unused_but_set_parameter)
11351 : : {
11352 : 3044926 : tree decl;
11353 : :
11354 : 3044926 : for (decl = DECL_ARGUMENTS (fndecl);
11355 : 11380568 : decl;
11356 : 8335642 : decl = DECL_CHAIN (decl))
11357 : 8335642 : if (TREE_USED (decl)
11358 : 8274939 : && TREE_CODE (decl) == PARM_DECL
11359 : 8274939 : && !DECL_READ_P (decl)
11360 : 47 : && DECL_NAME (decl)
11361 : 47 : && !DECL_ARTIFICIAL (decl)
11362 : 8335689 : && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter_))
11363 : 47 : warning_at (DECL_SOURCE_LOCATION (decl),
11364 : 47 : OPT_Wunused_but_set_parameter_,
11365 : : "parameter %qD set but not used", decl);
11366 : : }
11367 : :
11368 : : /* Complain about locally defined typedefs that are not used in this
11369 : : function. */
11370 : 35380160 : maybe_warn_unused_local_typedefs ();
11371 : :
11372 : : /* Possibly warn about unused parameters. */
11373 : 35380160 : if (warn_unused_parameter)
11374 : 2951037 : do_warn_unused_parameter (fndecl);
11375 : :
11376 : : /* Store the end of the function, so that we get good line number
11377 : : info for the epilogue. */
11378 : 35380160 : cfun->function_end_locus = end_loc;
11379 : :
11380 : : /* Finalize the ELF visibility for the function. */
11381 : 35380160 : c_determine_visibility (fndecl);
11382 : :
11383 : : /* For GNU C extern inline functions disregard inline limits. */
11384 : 35380160 : if (DECL_EXTERNAL (fndecl)
11385 : 34544250 : && DECL_DECLARED_INLINE_P (fndecl)
11386 : 69924407 : && (flag_gnu89_inline
11387 : 34513308 : || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
11388 : 34543778 : DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
11389 : :
11390 : : /* Genericize before inlining. Delay genericizing nested functions
11391 : : until their parent function is genericized. Since finalizing
11392 : : requires GENERIC, delay that as well. */
11393 : :
11394 : 70760320 : if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
11395 : 70760318 : && !undef_nested_function)
11396 : : {
11397 : 35380152 : if (!decl_function_context (fndecl))
11398 : : {
11399 : 35378577 : invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
11400 : 35378577 : c_genericize (fndecl);
11401 : :
11402 : : /* ??? Objc emits functions after finalizing the compilation unit.
11403 : : This should be cleaned up later and this conditional removed. */
11404 : 35378577 : if (symtab->global_info_ready)
11405 : : {
11406 : 0 : cgraph_node::add_new_function (fndecl, false);
11407 : 0 : return;
11408 : : }
11409 : 35378577 : cgraph_node::finalize_function (fndecl, false);
11410 : : }
11411 : : else
11412 : : {
11413 : : /* Register this function with cgraph just far enough to get it
11414 : : added to our parent's nested function list. Handy, since the
11415 : : C front end doesn't have such a list. */
11416 : 1575 : (void) cgraph_node::get_create (fndecl);
11417 : : }
11418 : : }
11419 : :
11420 : 35380160 : if (!decl_function_context (fndecl))
11421 : 35378585 : undef_nested_function = false;
11422 : :
11423 : 35380160 : if (cfun->language != NULL)
11424 : : {
11425 : 3108761 : ggc_free (cfun->language);
11426 : 3108761 : cfun->language = NULL;
11427 : : }
11428 : :
11429 : : /* We're leaving the context of this function, so zap cfun.
11430 : : It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
11431 : : tree_rest_of_compilation. */
11432 : 35380160 : set_cfun (NULL);
11433 : 35380160 : invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
11434 : 35380160 : current_function_decl = NULL;
11435 : : }
11436 : :
11437 : : /* Check the declarations given in a for-loop for satisfying the C99
11438 : : constraints. If exactly one such decl is found, return it. LOC is
11439 : : the location of the opening parenthesis of the for loop. The last
11440 : : parameter allows you to control the "for loop initial declarations
11441 : : are only allowed in C99 mode". Normally, you should pass
11442 : : flag_isoc99 as that parameter. But in some cases (Objective-C
11443 : : foreach loop, for example) we want to run the checks in this
11444 : : function even if not in C99 mode, so we allow the caller to turn
11445 : : off the error about not being in C99 mode.
11446 : : */
11447 : :
11448 : : tree
11449 : 24722 : check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
11450 : : {
11451 : 24722 : struct c_binding *b;
11452 : 24722 : tree one_decl = NULL_TREE;
11453 : 24722 : int n_decls = 0;
11454 : :
11455 : 24722 : if (!turn_off_iso_c99_error)
11456 : : {
11457 : 1 : static bool hint = true;
11458 : : /* If we get here, declarations have been used in a for loop without
11459 : : the C99 for loop scope. This doesn't make much sense, so don't
11460 : : allow it. */
11461 : 1 : auto_diagnostic_group d;
11462 : 1 : error_at (loc, "%<for%> loop initial declarations "
11463 : : "are only allowed in C99 or C11 mode");
11464 : 1 : if (hint)
11465 : : {
11466 : 1 : inform (loc,
11467 : : "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
11468 : : "%<-std=gnu11%> to compile your code");
11469 : 1 : hint = false;
11470 : : }
11471 : 1 : return NULL_TREE;
11472 : 1 : }
11473 : : else
11474 : 24721 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
11475 : : "initial declarations");
11476 : :
11477 : : /* C99 subclause 6.8.5 paragraph 3:
11478 : :
11479 : : [#3] The declaration part of a for statement shall only
11480 : : declare identifiers for objects having storage class auto or
11481 : : register.
11482 : :
11483 : : It isn't clear whether, in this sentence, "identifiers" binds to
11484 : : "shall only declare" or to "objects" - that is, whether all identifiers
11485 : : declared must be identifiers for objects, or whether the restriction
11486 : : only applies to those that are. (A question on this in comp.std.c
11487 : : in November 2000 received no answer.) We implement the strictest
11488 : : interpretation, to avoid creating an extension which later causes
11489 : : problems.
11490 : :
11491 : : This constraint was removed in C23. */
11492 : :
11493 : 49522 : for (b = current_scope->bindings; b; b = b->prev)
11494 : : {
11495 : 24801 : tree id = b->id;
11496 : 24801 : tree decl = b->decl;
11497 : :
11498 : 24801 : if (!id)
11499 : 27 : continue;
11500 : :
11501 : 24774 : switch (TREE_CODE (decl))
11502 : : {
11503 : 24726 : case VAR_DECL:
11504 : 24726 : {
11505 : 24726 : location_t decl_loc = DECL_SOURCE_LOCATION (decl);
11506 : 24726 : if (TREE_STATIC (decl))
11507 : 5 : pedwarn_c11 (decl_loc, OPT_Wpedantic,
11508 : : "declaration of static variable %qD in %<for%> "
11509 : : "loop initial declaration", decl);
11510 : 24721 : else if (DECL_EXTERNAL (decl))
11511 : 5 : pedwarn_c11 (decl_loc, OPT_Wpedantic,
11512 : : "declaration of %<extern%> variable %qD in %<for%> "
11513 : : "loop initial declaration", decl);
11514 : : }
11515 : : break;
11516 : :
11517 : 6 : case RECORD_TYPE:
11518 : 6 : pedwarn_c11 (loc, OPT_Wpedantic,
11519 : : "%<struct %E%> declared in %<for%> loop initial "
11520 : : "declaration", id);
11521 : 6 : break;
11522 : 5 : case UNION_TYPE:
11523 : 5 : pedwarn_c11 (loc, OPT_Wpedantic,
11524 : : "%<union %E%> declared in %<for%> loop initial "
11525 : : "declaration",
11526 : : id);
11527 : 5 : break;
11528 : 5 : case ENUMERAL_TYPE:
11529 : 5 : pedwarn_c11 (loc, OPT_Wpedantic,
11530 : : "%<enum %E%> declared in %<for%> loop "
11531 : : "initial declaration", id);
11532 : 5 : break;
11533 : 32 : default:
11534 : 32 : pedwarn_c11 (loc, OPT_Wpedantic, "declaration of non-variable "
11535 : : "%qD in %<for%> loop initial declaration", decl);
11536 : : }
11537 : :
11538 : 24774 : n_decls++;
11539 : 24774 : one_decl = decl;
11540 : : }
11541 : :
11542 : 24721 : return n_decls == 1 ? one_decl : NULL_TREE;
11543 : : }
11544 : :
11545 : : /* Save and reinitialize the variables
11546 : : used during compilation of a C function. */
11547 : :
11548 : : void
11549 : 1603 : c_push_function_context (void)
11550 : : {
11551 : 1603 : struct language_function *p = cfun->language;
11552 : : /* cfun->language might have been already allocated by the use of
11553 : : -Wunused-local-typedefs. In that case, just re-use it. */
11554 : 1603 : if (p == NULL)
11555 : 1547 : cfun->language = p = ggc_cleared_alloc<language_function> ();
11556 : :
11557 : 1603 : p->base.x_stmt_tree = c_stmt_tree;
11558 : 1603 : c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
11559 : 1603 : p->x_in_statement = in_statement;
11560 : 1603 : p->x_switch_stack = c_switch_stack;
11561 : 1603 : p->loop_names = loop_names;
11562 : 1603 : loop_names = vNULL;
11563 : 1603 : p->loop_names_hash = loop_names_hash;
11564 : 1603 : loop_names_hash = NULL;
11565 : 1603 : p->arg_info = current_function_arg_info;
11566 : 1603 : p->returns_value = current_function_returns_value;
11567 : 1603 : p->returns_null = current_function_returns_null;
11568 : 1603 : p->returns_abnormally = current_function_returns_abnormally;
11569 : 1603 : p->warn_about_return_type = warn_about_return_type;
11570 : :
11571 : 1603 : push_function_context ();
11572 : 1603 : }
11573 : :
11574 : : /* Restore the variables used during compilation of a C function. */
11575 : :
11576 : : void
11577 : 1603 : c_pop_function_context (void)
11578 : : {
11579 : 1603 : struct language_function *p;
11580 : :
11581 : 1603 : pop_function_context ();
11582 : 1603 : p = cfun->language;
11583 : :
11584 : : /* When -Wunused-local-typedefs is in effect, cfun->languages is
11585 : : used to store data throughout the life time of the current cfun,
11586 : : So don't deallocate it. */
11587 : 1603 : if (!warn_unused_local_typedefs)
11588 : 1547 : cfun->language = NULL;
11589 : :
11590 : 1603 : if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
11591 : 1603 : && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
11592 : : {
11593 : : /* Stop pointing to the local nodes about to be freed. */
11594 : : /* But DECL_INITIAL must remain nonzero so we know this
11595 : : was an actual function definition. */
11596 : 0 : DECL_INITIAL (current_function_decl) = error_mark_node;
11597 : 0 : DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
11598 : : }
11599 : :
11600 : 1603 : c_stmt_tree = p->base.x_stmt_tree;
11601 : 1603 : p->base.x_stmt_tree.x_cur_stmt_list = NULL;
11602 : 1603 : in_statement = p->x_in_statement;
11603 : 1603 : c_switch_stack = p->x_switch_stack;
11604 : 1603 : loop_names.release ();
11605 : 1603 : loop_names = p->loop_names;
11606 : 1603 : p->loop_names = vNULL;
11607 : 1603 : delete loop_names_hash;
11608 : 1603 : loop_names_hash = p->loop_names_hash;
11609 : 1603 : p->loop_names_hash = NULL;
11610 : 1603 : current_function_arg_info = p->arg_info;
11611 : 1603 : current_function_returns_value = p->returns_value;
11612 : 1603 : current_function_returns_null = p->returns_null;
11613 : 1603 : current_function_returns_abnormally = p->returns_abnormally;
11614 : 1603 : warn_about_return_type = p->warn_about_return_type;
11615 : 1603 : }
11616 : :
11617 : : /* The functions below are required for functionality of doing
11618 : : function at once processing in the C front end. Currently these
11619 : : functions are not called from anywhere in the C front end, but as
11620 : : these changes continue, that will change. */
11621 : :
11622 : : /* Returns the stmt_tree (if any) to which statements are currently
11623 : : being added. If there is no active statement-tree, NULL is
11624 : : returned. */
11625 : :
11626 : : stmt_tree
11627 : 961668028 : current_stmt_tree (void)
11628 : : {
11629 : 961668028 : return &c_stmt_tree;
11630 : : }
11631 : :
11632 : : /* Return the global value of T as a symbol. */
11633 : :
11634 : : tree
11635 : 3880090 : identifier_global_value (tree t)
11636 : : {
11637 : 3880090 : struct c_binding *b;
11638 : :
11639 : 3881057 : for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
11640 : 3873829 : if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11641 : 3872862 : return b->decl;
11642 : :
11643 : : return NULL_TREE;
11644 : : }
11645 : :
11646 : : /* Return the global value of tag T as a symbol. */
11647 : :
11648 : : tree
11649 : 12 : identifier_global_tag (tree t)
11650 : : {
11651 : 12 : struct c_binding *b;
11652 : :
11653 : 12 : for (b = I_TAG_BINDING (t); b; b = b->shadowed)
11654 : 11 : if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11655 : 11 : return b->decl;
11656 : :
11657 : : return NULL_TREE;
11658 : : }
11659 : :
11660 : : /* Returns non-zero (result of __has_builtin) if NAME refers to a built-in
11661 : : function or function-like operator. */
11662 : :
11663 : : int
11664 : 24554 : names_builtin_p (const char *name)
11665 : : {
11666 : 24554 : tree id = get_identifier (name);
11667 : 24554 : if (tree decl = identifier_global_value (id))
11668 : 24466 : return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_UNDECLARED_BUILTIN (decl);
11669 : :
11670 : : /* Also detect common reserved C words that aren't strictly built-in
11671 : : functions. */
11672 : 88 : switch (C_RID_CODE (id))
11673 : : {
11674 : : case RID_BUILTIN_ASSOC_BARRIER:
11675 : : case RID_BUILTIN_CONVERTVECTOR:
11676 : : case RID_BUILTIN_HAS_ATTRIBUTE:
11677 : : case RID_BUILTIN_SHUFFLE:
11678 : : case RID_BUILTIN_SHUFFLEVECTOR:
11679 : : case RID_BUILTIN_STDC:
11680 : : case RID_BUILTIN_COUNTED_BY_REF:
11681 : : case RID_CHOOSE_EXPR:
11682 : : case RID_OFFSETOF:
11683 : : case RID_TYPES_COMPATIBLE_P:
11684 : : case RID_C23_VA_START:
11685 : : case RID_VA_ARG:
11686 : : return 1;
11687 : 67 : default:
11688 : 67 : break;
11689 : : }
11690 : :
11691 : 67 : return 0;
11692 : : }
11693 : :
11694 : : /* In C, the only C-linkage public declaration is at file scope. */
11695 : :
11696 : : tree
11697 : 5 : c_linkage_bindings (tree name)
11698 : : {
11699 : 5 : return identifier_global_value (name);
11700 : : }
11701 : :
11702 : : /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
11703 : : otherwise the name is found in ridpointers from RID_INDEX. */
11704 : :
11705 : : void
11706 : 3295650 : record_builtin_type (enum rid rid_index, const char *name, tree type)
11707 : : {
11708 : 3295650 : tree id, decl;
11709 : 3295650 : if (name == 0)
11710 : 1537970 : id = ridpointers[(int) rid_index];
11711 : : else
11712 : 1757680 : id = get_identifier (name);
11713 : 3295650 : decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
11714 : 3295650 : pushdecl (decl);
11715 : 3295650 : if (debug_hooks->type_decl)
11716 : 3295650 : debug_hooks->type_decl (decl, false);
11717 : 3295650 : }
11718 : :
11719 : : /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
11720 : :
11721 : : struct c_parm *
11722 : 121111321 : build_c_parm (struct c_declspecs *specs, tree attrs,
11723 : : struct c_declarator *declarator,
11724 : : location_t loc)
11725 : : {
11726 : 121111321 : struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
11727 : 121111321 : ret->specs = specs;
11728 : 121111321 : ret->attrs = attrs;
11729 : 121111321 : ret->declarator = declarator;
11730 : 121111321 : ret->loc = loc;
11731 : 121111321 : return ret;
11732 : : }
11733 : :
11734 : : /* Return a declarator with nested attributes. TARGET is the inner
11735 : : declarator to which these attributes apply. ATTRS are the
11736 : : attributes. */
11737 : :
11738 : : struct c_declarator *
11739 : 6655 : build_attrs_declarator (tree attrs, struct c_declarator *target)
11740 : : {
11741 : 6655 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11742 : 6655 : ret->kind = cdk_attrs;
11743 : 6655 : ret->declarator = target;
11744 : 6655 : ret->u.attrs = attrs;
11745 : 6655 : return ret;
11746 : : }
11747 : :
11748 : : /* Return a declarator for a function with arguments specified by ARGS
11749 : : and return type specified by TARGET. */
11750 : :
11751 : : struct c_declarator *
11752 : 49417444 : build_function_declarator (struct c_arg_info *args,
11753 : : struct c_declarator *target)
11754 : : {
11755 : 49417444 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11756 : 49417444 : ret->kind = cdk_function;
11757 : 49417444 : ret->declarator = target;
11758 : 49417444 : ret->u.arg_info = args;
11759 : 49417444 : return ret;
11760 : : }
11761 : :
11762 : : /* Return a declarator for the identifier IDENT (which may be
11763 : : NULL_TREE for an abstract declarator). */
11764 : :
11765 : : struct c_declarator *
11766 : 306542127 : build_id_declarator (tree ident)
11767 : : {
11768 : 306542127 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11769 : 306542127 : ret->kind = cdk_id;
11770 : 306542127 : ret->declarator = 0;
11771 : 306542127 : ret->u.id.id = ident;
11772 : 306542127 : ret->u.id.attrs = NULL_TREE;
11773 : : /* Default value - may get reset to a more precise location. */
11774 : 306542127 : ret->id_loc = input_location;
11775 : 306542127 : return ret;
11776 : : }
11777 : :
11778 : : /* Return something to represent absolute declarators containing a *.
11779 : : TARGET is the absolute declarator that the * contains.
11780 : : TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
11781 : : to apply to the pointer type. */
11782 : :
11783 : : struct c_declarator *
11784 : 17891340 : make_pointer_declarator (struct c_declspecs *type_quals_attrs,
11785 : : struct c_declarator *target)
11786 : : {
11787 : 17891340 : tree attrs;
11788 : 17891340 : int quals = 0;
11789 : 17891340 : struct c_declarator *itarget = target;
11790 : 17891340 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11791 : 17891340 : if (type_quals_attrs)
11792 : : {
11793 : 17891340 : attrs = type_quals_attrs->attrs;
11794 : 17891340 : quals = quals_from_declspecs (type_quals_attrs);
11795 : 17891340 : if (attrs != NULL_TREE)
11796 : 6383 : itarget = build_attrs_declarator (attrs, target);
11797 : : }
11798 : 17891340 : ret->kind = cdk_pointer;
11799 : 17891340 : ret->declarator = itarget;
11800 : 17891340 : ret->u.pointer_quals = quals;
11801 : 17891340 : return ret;
11802 : : }
11803 : :
11804 : : /* Return a pointer to a structure for an empty list of declaration
11805 : : specifiers. */
11806 : :
11807 : : struct c_declspecs *
11808 : 443911738 : build_null_declspecs (void)
11809 : : {
11810 : 443911738 : struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
11811 : 443911738 : memset (ret, 0, sizeof *ret);
11812 : 443911738 : ret->align_log = -1;
11813 : 443911738 : ret->typespec_word = cts_none;
11814 : 443911738 : ret->storage_class = csc_none;
11815 : 443911738 : ret->expr_const_operands = true;
11816 : 443911738 : ret->typespec_kind = ctsk_none;
11817 : 443911738 : ret->address_space = ADDR_SPACE_GENERIC;
11818 : 443911738 : return ret;
11819 : : }
11820 : :
11821 : : /* Add the address space ADDRSPACE to the declaration specifiers
11822 : : SPECS, returning SPECS. */
11823 : :
11824 : : struct c_declspecs *
11825 : 174 : declspecs_add_addrspace (location_t location,
11826 : : struct c_declspecs *specs, addr_space_t as)
11827 : : {
11828 : 174 : specs->non_sc_seen_p = true;
11829 : 174 : specs->declspecs_seen_p = true;
11830 : 174 : specs->non_std_attrs_seen_p = true;
11831 : :
11832 : 174 : if (!ADDR_SPACE_GENERIC_P (specs->address_space)
11833 : 0 : && specs->address_space != as)
11834 : 0 : error ("incompatible address space qualifiers %qs and %qs",
11835 : : c_addr_space_name (as),
11836 : : c_addr_space_name (specs->address_space));
11837 : : else
11838 : : {
11839 : 174 : specs->address_space = as;
11840 : 174 : specs->locations[cdw_address_space] = location;
11841 : : }
11842 : 174 : return specs;
11843 : : }
11844 : :
11845 : : /* Add the type qualifier QUAL to the declaration specifiers SPECS,
11846 : : returning SPECS. */
11847 : :
11848 : : struct c_declspecs *
11849 : 16248393 : declspecs_add_qual (location_t loc,
11850 : : struct c_declspecs *specs, tree qual)
11851 : : {
11852 : 16248393 : enum rid i;
11853 : 16248393 : bool dupe = false;
11854 : 16248393 : specs->non_sc_seen_p = true;
11855 : 16248393 : specs->declspecs_seen_p = true;
11856 : 16248393 : specs->non_std_attrs_seen_p = true;
11857 : 16248393 : gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
11858 : : && C_IS_RESERVED_WORD (qual));
11859 : 16248393 : i = C_RID_CODE (qual);
11860 : 16248393 : location_t prev_loc = UNKNOWN_LOCATION;
11861 : 16248393 : switch (i)
11862 : : {
11863 : 12803068 : case RID_CONST:
11864 : 12803068 : dupe = specs->const_p;
11865 : 12803068 : specs->const_p = true;
11866 : 12803068 : prev_loc = specs->locations[cdw_const];
11867 : 12803068 : specs->locations[cdw_const] = loc;
11868 : 12803068 : break;
11869 : 95339 : case RID_VOLATILE:
11870 : 95339 : dupe = specs->volatile_p;
11871 : 95339 : specs->volatile_p = true;
11872 : 95339 : prev_loc = specs->locations[cdw_volatile];
11873 : 95339 : specs->locations[cdw_volatile] = loc;
11874 : 95339 : break;
11875 : 3331043 : case RID_RESTRICT:
11876 : 3331043 : dupe = specs->restrict_p;
11877 : 3331043 : specs->restrict_p = true;
11878 : 3331043 : prev_loc = specs->locations[cdw_restrict];
11879 : 3331043 : specs->locations[cdw_restrict] = loc;
11880 : 3331043 : break;
11881 : 18943 : case RID_ATOMIC:
11882 : 18943 : dupe = specs->atomic_p;
11883 : 18943 : specs->atomic_p = true;
11884 : 18943 : prev_loc = specs->locations[cdw_atomic];
11885 : 18943 : specs->locations[cdw_atomic] = loc;
11886 : 18943 : break;
11887 : 0 : default:
11888 : 0 : gcc_unreachable ();
11889 : : }
11890 : 16248393 : if (dupe)
11891 : : {
11892 : 56 : bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
11893 : : "duplicate %qE declaration specifier", qual);
11894 : 56 : if (!warned
11895 : 52 : && warn_duplicate_decl_specifier
11896 : 33 : && prev_loc >= RESERVED_LOCATION_COUNT
11897 : 33 : && !from_macro_expansion_at (prev_loc)
11898 : 77 : && !from_macro_expansion_at (loc))
11899 : 12 : warning_at (loc, OPT_Wduplicate_decl_specifier,
11900 : : "duplicate %qE declaration specifier", qual);
11901 : : }
11902 : 16248393 : return specs;
11903 : : }
11904 : :
11905 : : /* Add the type specifier TYPE to the declaration specifiers SPECS,
11906 : : returning SPECS. */
11907 : :
11908 : : struct c_declspecs *
11909 : 323813197 : declspecs_add_type (location_t loc, struct c_declspecs *specs,
11910 : : struct c_typespec spec)
11911 : : {
11912 : 323813197 : tree type = spec.spec;
11913 : 323813197 : specs->non_sc_seen_p = true;
11914 : 323813197 : specs->declspecs_seen_p = true;
11915 : 323813197 : specs->non_std_attrs_seen_p = true;
11916 : 323813197 : specs->typespec_kind = spec.kind;
11917 : 323813197 : if (TREE_DEPRECATED (type))
11918 : 56 : specs->deprecated_p = true;
11919 : 323813197 : if (TREE_UNAVAILABLE (type))
11920 : 40 : specs->unavailable_p = true;
11921 : :
11922 : : /* As a type specifier is present, "auto" must be used as a storage
11923 : : class specifier, not for type deduction. */
11924 : 323813197 : if (specs->c23_auto_p)
11925 : : {
11926 : 115 : specs->c23_auto_p = false;
11927 : 115 : if (specs->storage_class != csc_none)
11928 : 1 : error ("multiple storage classes in declaration specifiers");
11929 : 114 : else if (specs->thread_p)
11930 : 1 : error ("%qs used with %<auto%>",
11931 : 1 : specs->thread_gnu_p ? "__thread" : "_Thread_local");
11932 : 113 : else if (specs->constexpr_p)
11933 : : /* auto may only be used with another storage class specifier,
11934 : : such as constexpr, if the type is inferred. */
11935 : 2 : error ("%<auto%> used with %<constexpr%>");
11936 : : else
11937 : 111 : specs->storage_class = csc_auto;
11938 : : }
11939 : :
11940 : : /* Handle type specifier keywords. */
11941 : 323813197 : if (TREE_CODE (type) == IDENTIFIER_NODE
11942 : 81038773 : && C_IS_RESERVED_WORD (type)
11943 : 404851970 : && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
11944 : : {
11945 : 81038773 : enum rid i = C_RID_CODE (type);
11946 : 81038773 : if (specs->type)
11947 : : {
11948 : 58 : error_at (loc, "two or more data types in declaration specifiers");
11949 : 58 : return specs;
11950 : : }
11951 : 81038715 : if ((int) i <= (int) RID_LAST_MODIFIER)
11952 : : {
11953 : : /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
11954 : 21351162 : bool dupe = false;
11955 : 21351162 : switch (i)
11956 : : {
11957 : 10529074 : case RID_LONG:
11958 : 10529074 : if (specs->long_long_p)
11959 : : {
11960 : 104 : error_at (loc, "%<long long long%> is too long for GCC");
11961 : 104 : break;
11962 : : }
11963 : 10528970 : if (specs->long_p)
11964 : : {
11965 : 2889293 : if (specs->typespec_word == cts_double)
11966 : : {
11967 : 15 : error_at (loc,
11968 : : "both %qs and %qs in declaration specifiers",
11969 : : "long long", "double");
11970 : 15 : break;
11971 : : }
11972 : 2889278 : pedwarn_c90 (loc, OPT_Wlong_long,
11973 : : "ISO C90 does not support %<long long%>");
11974 : 2889278 : specs->long_long_p = 1;
11975 : 2889278 : specs->locations[cdw_long_long] = loc;
11976 : 2889278 : break;
11977 : : }
11978 : 7639677 : if (specs->short_p)
11979 : 77 : error_at (loc,
11980 : : "both %qs and %qs in declaration specifiers",
11981 : : "long", "short");
11982 : 7639600 : else if (specs->typespec_word == cts_auto_type)
11983 : 1 : error_at (loc,
11984 : : "both %qs and %qs in declaration specifiers",
11985 : : "long", "__auto_type");
11986 : : else if (specs->typespec_word == cts_void)
11987 : 5 : error_at (loc,
11988 : : "both %qs and %qs in declaration specifiers",
11989 : : "long", "void");
11990 : : else if (specs->typespec_word == cts_int_n)
11991 : 19 : error_at (loc,
11992 : : "both %qs and %<__int%d%> in declaration specifiers",
11993 : 19 : "long", int_n_data[specs->u.int_n_idx].bitsize);
11994 : : else if (specs->typespec_word == cts_bool)
11995 : 3 : error_at (loc,
11996 : : "both %qs and %qs in declaration specifiers",
11997 : : "long", "_Bool");
11998 : : else if (specs->typespec_word == cts_bitint)
11999 : 3 : error_at (loc,
12000 : : "both %qs and %qs in declaration specifiers",
12001 : : "long", "_BitInt");
12002 : : else if (specs->typespec_word == cts_char)
12003 : 21 : error_at (loc,
12004 : : "both %qs and %qs in declaration specifiers",
12005 : : "long", "char");
12006 : : else if (specs->typespec_word == cts_float)
12007 : 7 : error_at (loc,
12008 : : "both %qs and %qs in declaration specifiers",
12009 : : "long", "float");
12010 : : else if (specs->typespec_word == cts_floatn_nx)
12011 : 0 : error_at (loc,
12012 : : "both %qs and %<_Float%d%s%> in declaration "
12013 : : "specifiers", "long",
12014 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12015 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12016 : : ? "x"
12017 : : : "");
12018 : : else if (specs->typespec_word == cts_dfloat32)
12019 : 4 : error_at (loc,
12020 : : "both %qs and %qs in declaration specifiers",
12021 : : "long", "_Decimal32");
12022 : : else if (specs->typespec_word == cts_dfloat64)
12023 : 4 : error_at (loc,
12024 : : "both %qs and %qs in declaration specifiers",
12025 : : "long", "_Decimal64");
12026 : : else if (specs->typespec_word == cts_dfloat128)
12027 : 4 : error_at (loc,
12028 : : "both %qs and %qs in declaration specifiers",
12029 : : "long", "_Decimal128");
12030 : : else if (specs->typespec_word == cts_dfloat64x)
12031 : 0 : error_at (loc,
12032 : : "both %qs and %qs in declaration specifiers",
12033 : : "long", "_Decimal64x");
12034 : : else
12035 : : {
12036 : 7639529 : specs->long_p = true;
12037 : 7639529 : specs->locations[cdw_long] = loc;
12038 : : }
12039 : : break;
12040 : 1665282 : case RID_SHORT:
12041 : 1665282 : dupe = specs->short_p;
12042 : 1665282 : if (specs->long_p)
12043 : 197 : error_at (loc,
12044 : : "both %qs and %qs in declaration specifiers",
12045 : : "long", "short");
12046 : 1665085 : else if (specs->typespec_word == cts_auto_type)
12047 : 1 : error_at (loc,
12048 : : "both %qs and %qs in declaration specifiers",
12049 : : "short", "__auto_type");
12050 : : else if (specs->typespec_word == cts_void)
12051 : 5 : error_at (loc,
12052 : : "both %qs and %qs in declaration specifiers",
12053 : : "short", "void");
12054 : : else if (specs->typespec_word == cts_int_n)
12055 : 19 : error_at (loc,
12056 : : "both %qs and %<__int%d%> in declaration specifiers",
12057 : 19 : "short", int_n_data[specs->u.int_n_idx].bitsize);
12058 : : else if (specs->typespec_word == cts_bool)
12059 : 3 : error_at (loc,
12060 : : "both %qs and %qs in declaration specifiers",
12061 : : "short", "_Bool");
12062 : : else if (specs->typespec_word == cts_bitint)
12063 : 1 : error_at (loc,
12064 : : "both %qs and %qs in declaration specifiers",
12065 : : "short", "_BitInt");
12066 : : else if (specs->typespec_word == cts_char)
12067 : 21 : error_at (loc,
12068 : : "both %qs and %qs in declaration specifiers",
12069 : : "short", "char");
12070 : : else if (specs->typespec_word == cts_float)
12071 : 7 : error_at (loc,
12072 : : "both %qs and %qs in declaration specifiers",
12073 : : "short", "float");
12074 : : else if (specs->typespec_word == cts_double)
12075 : 7 : error_at (loc,
12076 : : "both %qs and %qs in declaration specifiers",
12077 : : "short", "double");
12078 : : else if (specs->typespec_word == cts_floatn_nx)
12079 : 0 : error_at (loc,
12080 : : "both %qs and %<_Float%d%s%> in declaration "
12081 : : "specifiers", "short",
12082 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12083 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12084 : : ? "x"
12085 : : : "");
12086 : : else if (specs->typespec_word == cts_dfloat32)
12087 : 4 : error_at (loc,
12088 : : "both %qs and %qs in declaration specifiers",
12089 : : "short", "_Decimal32");
12090 : : else if (specs->typespec_word == cts_dfloat64)
12091 : 4 : error_at (loc,
12092 : : "both %qs and %qs in declaration specifiers",
12093 : : "short", "_Decimal64");
12094 : : else if (specs->typespec_word == cts_dfloat128)
12095 : 4 : error_at (loc,
12096 : : "both %qs and %qs in declaration specifiers",
12097 : : "short", "_Decimal128");
12098 : : else if (specs->typespec_word == cts_dfloat64x)
12099 : 0 : error_at (loc,
12100 : : "both %qs and %qs in declaration specifiers",
12101 : : "short", "_Decimal64x");
12102 : : else
12103 : : {
12104 : 1665009 : specs->short_p = true;
12105 : 1665009 : specs->locations[cdw_short] = loc;
12106 : : }
12107 : : break;
12108 : 569461 : case RID_SIGNED:
12109 : 569461 : dupe = specs->signed_p;
12110 : 569461 : if (specs->unsigned_p)
12111 : 138 : error_at (loc,
12112 : : "both %qs and %qs in declaration specifiers",
12113 : : "signed", "unsigned");
12114 : 569323 : else if (specs->typespec_word == cts_auto_type)
12115 : 1 : error_at (loc,
12116 : : "both %qs and %qs in declaration specifiers",
12117 : : "signed", "__auto_type");
12118 : : else if (specs->typespec_word == cts_void)
12119 : 5 : error_at (loc,
12120 : : "both %qs and %qs in declaration specifiers",
12121 : : "signed", "void");
12122 : : else if (specs->typespec_word == cts_bool)
12123 : 3 : error_at (loc,
12124 : : "both %qs and %qs in declaration specifiers",
12125 : : "signed", "_Bool");
12126 : : else if (specs->typespec_word == cts_float)
12127 : 7 : error_at (loc,
12128 : : "both %qs and %qs in declaration specifiers",
12129 : : "signed", "float");
12130 : : else if (specs->typespec_word == cts_double)
12131 : 21 : error_at (loc,
12132 : : "both %qs and %qs in declaration specifiers",
12133 : : "signed", "double");
12134 : : else if (specs->typespec_word == cts_floatn_nx)
12135 : 0 : error_at (loc,
12136 : : "both %qs and %<_Float%d%s%> in declaration "
12137 : : "specifiers", "signed",
12138 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12139 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12140 : : ? "x"
12141 : : : "");
12142 : : else if (specs->typespec_word == cts_dfloat32)
12143 : 4 : error_at (loc,
12144 : : "both %qs and %qs in declaration specifiers",
12145 : : "signed", "_Decimal32");
12146 : : else if (specs->typespec_word == cts_dfloat64)
12147 : 4 : error_at (loc,
12148 : : "both %qs and %qs in declaration specifiers",
12149 : : "signed", "_Decimal64");
12150 : : else if (specs->typespec_word == cts_dfloat128)
12151 : 4 : error_at (loc,
12152 : : "both %qs and %qs in declaration specifiers",
12153 : : "signed", "_Decimal128");
12154 : : else if (specs->typespec_word == cts_dfloat64x)
12155 : 0 : error_at (loc,
12156 : : "both %qs and %qs in declaration specifiers",
12157 : : "signed", "_Decimal64x");
12158 : : else
12159 : : {
12160 : 569274 : specs->signed_p = true;
12161 : 569274 : specs->locations[cdw_signed] = loc;
12162 : : }
12163 : : break;
12164 : 6040546 : case RID_UNSIGNED:
12165 : 6040546 : dupe = specs->unsigned_p;
12166 : 6040546 : if (specs->signed_p)
12167 : 139 : error_at (loc,
12168 : : "both %qs and %qs in declaration specifiers",
12169 : : "signed", "unsigned");
12170 : 6040407 : else if (specs->typespec_word == cts_auto_type)
12171 : 1 : error_at (loc,
12172 : : "both %qs and %qs in declaration specifiers",
12173 : : "unsigned", "__auto_type");
12174 : : else if (specs->typespec_word == cts_void)
12175 : 5 : error_at (loc,
12176 : : "both %qs and %qs in declaration specifiers",
12177 : : "unsigned", "void");
12178 : : else if (specs->typespec_word == cts_bool)
12179 : 3 : error_at (loc,
12180 : : "both %qs and %qs in declaration specifiers",
12181 : : "unsigned", "_Bool");
12182 : : else if (specs->typespec_word == cts_float)
12183 : 7 : error_at (loc,
12184 : : "both %qs and %qs in declaration specifiers",
12185 : : "unsigned", "float");
12186 : : else if (specs->typespec_word == cts_double)
12187 : 21 : error_at (loc,
12188 : : "both %qs and %qs in declaration specifiers",
12189 : : "unsigned", "double");
12190 : : else if (specs->typespec_word == cts_floatn_nx)
12191 : 0 : error_at (loc,
12192 : : "both %qs and %<_Float%d%s%> in declaration "
12193 : : "specifiers", "unsigned",
12194 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12195 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12196 : : ? "x"
12197 : : : "");
12198 : : else if (specs->typespec_word == cts_dfloat32)
12199 : 2 : error_at (loc,
12200 : : "both %qs and %qs in declaration specifiers",
12201 : : "unsigned", "_Decimal32");
12202 : : else if (specs->typespec_word == cts_dfloat64)
12203 : 2 : error_at (loc,
12204 : : "both %qs and %qs in declaration specifiers",
12205 : : "unsigned", "_Decimal64");
12206 : : else if (specs->typespec_word == cts_dfloat128)
12207 : 2 : error_at (loc,
12208 : : "both %qs and %qs in declaration specifiers",
12209 : : "unsigned", "_Decimal128");
12210 : : else if (specs->typespec_word == cts_dfloat64x)
12211 : 0 : error_at (loc,
12212 : : "both %qs and %qs in declaration specifiers",
12213 : : "unsigned", "_Decimal64x");
12214 : : else
12215 : : {
12216 : 6040364 : specs->unsigned_p = true;
12217 : 6040364 : specs->locations[cdw_unsigned] = loc;
12218 : : }
12219 : : break;
12220 : 2546726 : case RID_COMPLEX:
12221 : 2546726 : dupe = specs->complex_p;
12222 : 2546726 : if (!in_system_header_at (loc))
12223 : 68932 : pedwarn_c90 (loc, OPT_Wpedantic,
12224 : : "ISO C90 does not support complex types");
12225 : 2546726 : if (specs->typespec_word == cts_auto_type)
12226 : 1 : error_at (loc,
12227 : : "both %qs and %qs in declaration specifiers",
12228 : : "complex", "__auto_type");
12229 : : else if (specs->typespec_word == cts_void)
12230 : 2 : error_at (loc,
12231 : : "both %qs and %qs in declaration specifiers",
12232 : : "complex", "void");
12233 : : else if (specs->typespec_word == cts_bool)
12234 : 2 : error_at (loc,
12235 : : "both %qs and %qs in declaration specifiers",
12236 : : "complex", "_Bool");
12237 : : else if (specs->typespec_word == cts_bitint)
12238 : 1 : error_at (loc,
12239 : : "both %qs and %qs in declaration specifiers",
12240 : : "complex", "_BitInt");
12241 : : else if (specs->typespec_word == cts_dfloat32)
12242 : 1 : error_at (loc,
12243 : : "both %qs and %qs in declaration specifiers",
12244 : : "complex", "_Decimal32");
12245 : : else if (specs->typespec_word == cts_dfloat64)
12246 : 1 : error_at (loc,
12247 : : "both %qs and %qs in declaration specifiers",
12248 : : "complex", "_Decimal64");
12249 : : else if (specs->typespec_word == cts_dfloat128)
12250 : 1 : error_at (loc,
12251 : : "both %qs and %qs in declaration specifiers",
12252 : : "complex", "_Decimal128");
12253 : : else if (specs->typespec_word == cts_dfloat64x)
12254 : 0 : error_at (loc,
12255 : : "both %qs and %qs in declaration specifiers",
12256 : : "complex", "_Decimal64x");
12257 : : else if (specs->typespec_word == cts_fract)
12258 : 0 : error_at (loc,
12259 : : "both %qs and %qs in declaration specifiers",
12260 : : "complex", "_Fract");
12261 : : else if (specs->typespec_word == cts_accum)
12262 : 0 : error_at (loc,
12263 : : "both %qs and %qs in declaration specifiers",
12264 : : "complex", "_Accum");
12265 : 2546717 : else if (specs->saturating_p)
12266 : 0 : error_at (loc,
12267 : : "both %qs and %qs in declaration specifiers",
12268 : : "complex", "_Sat");
12269 : : else
12270 : : {
12271 : 2546717 : specs->complex_p = true;
12272 : 2546717 : specs->locations[cdw_complex] = loc;
12273 : : }
12274 : : break;
12275 : 73 : case RID_SAT:
12276 : 73 : dupe = specs->saturating_p;
12277 : 73 : pedwarn (loc, OPT_Wpedantic,
12278 : : "ISO C does not support saturating types");
12279 : 73 : if (specs->typespec_word == cts_int_n)
12280 : 0 : error_at (loc,
12281 : : "both %qs and %<__int%d%> in declaration specifiers",
12282 : 0 : "_Sat", int_n_data[specs->u.int_n_idx].bitsize);
12283 : : else if (specs->typespec_word == cts_auto_type)
12284 : 0 : error_at (loc,
12285 : : "both %qs and %qs in declaration specifiers",
12286 : : "_Sat", "__auto_type");
12287 : : else if (specs->typespec_word == cts_void)
12288 : 0 : error_at (loc,
12289 : : "both %qs and %qs in declaration specifiers",
12290 : : "_Sat", "void");
12291 : : else if (specs->typespec_word == cts_bool)
12292 : 0 : error_at (loc,
12293 : : "both %qs and %qs in declaration specifiers",
12294 : : "_Sat", "_Bool");
12295 : : else if (specs->typespec_word == cts_bitint)
12296 : 0 : error_at (loc,
12297 : : "both %qs and %qs in declaration specifiers",
12298 : : "_Sat", "_BitInt");
12299 : : else if (specs->typespec_word == cts_char)
12300 : 0 : error_at (loc,
12301 : : "both %qs and %qs in declaration specifiers",
12302 : : "_Sat", "char");
12303 : : else if (specs->typespec_word == cts_int)
12304 : 0 : error_at (loc,
12305 : : "both %qs and %qs in declaration specifiers",
12306 : : "_Sat", "int");
12307 : : else if (specs->typespec_word == cts_float)
12308 : 0 : error_at (loc,
12309 : : "both %qs and %qs in declaration specifiers",
12310 : : "_Sat", "float");
12311 : : else if (specs->typespec_word == cts_double)
12312 : 0 : error_at (loc,
12313 : : "both %qs and %qs in declaration specifiers",
12314 : : "_Sat", "double");
12315 : : else if (specs->typespec_word == cts_floatn_nx)
12316 : 0 : error_at (loc,
12317 : : "both %qs and %<_Float%d%s%> in declaration "
12318 : : "specifiers", "_Sat",
12319 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12320 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12321 : : ? "x"
12322 : : : "");
12323 : : else if (specs->typespec_word == cts_dfloat32)
12324 : 0 : error_at (loc,
12325 : : "both %qs and %qs in declaration specifiers",
12326 : : "_Sat", "_Decimal32");
12327 : : else if (specs->typespec_word == cts_dfloat64)
12328 : 0 : error_at (loc,
12329 : : "both %qs and %qs in declaration specifiers",
12330 : : "_Sat", "_Decimal64");
12331 : : else if (specs->typespec_word == cts_dfloat128)
12332 : 0 : error_at (loc,
12333 : : "both %qs and %qs in declaration specifiers",
12334 : : "_Sat", "_Decimal128");
12335 : : else if (specs->typespec_word == cts_dfloat64x)
12336 : 0 : error_at (loc,
12337 : : "both %qs and %qs in declaration specifiers",
12338 : : "_Sat", "_Decimal64x");
12339 : 73 : else if (specs->complex_p)
12340 : 0 : error_at (loc,
12341 : : "both %qs and %qs in declaration specifiers",
12342 : : "_Sat", "complex");
12343 : : else
12344 : : {
12345 : 73 : specs->saturating_p = true;
12346 : 73 : specs->locations[cdw_saturating] = loc;
12347 : : }
12348 : : break;
12349 : 0 : default:
12350 : 0 : gcc_unreachable ();
12351 : : }
12352 : :
12353 : 21351162 : if (dupe)
12354 : 378 : error_at (loc, "duplicate %qE", type);
12355 : :
12356 : 21351162 : return specs;
12357 : : }
12358 : : else
12359 : : {
12360 : : /* "void", "_Bool", "char", "int", "float", "double",
12361 : : "_FloatN", "_FloatNx", "_Decimal32", "__intN",
12362 : : "_Decimal64", "_Decimal128", "_Fract", "_Accum", "_BitInt(N)" or
12363 : : "__auto_type". */
12364 : 59687553 : if (specs->typespec_word != cts_none)
12365 : : {
12366 : 2171 : if (i == RID_BOOL)
12367 : : {
12368 : 175 : auto_diagnostic_group d;
12369 : 175 : if (specs->storage_class == csc_typedef)
12370 : 4 : error_at (loc,
12371 : : "%qs cannot be defined via %<typedef%>",
12372 : 2 : IDENTIFIER_POINTER (type));
12373 : : else
12374 : 346 : error_at (loc,
12375 : : "%qs cannot be used here",
12376 : 173 : IDENTIFIER_POINTER (type));
12377 : 175 : add_note_about_new_keyword (loc, type);
12378 : 175 : }
12379 : : else
12380 : 1996 : error_at (loc,
12381 : : "two or more data types in declaration specifiers");
12382 : 2171 : return specs;
12383 : : }
12384 : 59685382 : switch (i)
12385 : : {
12386 : 1838 : case RID_AUTO_TYPE:
12387 : 1838 : if (specs->long_p)
12388 : 1 : error_at (loc,
12389 : : "both %qs and %qs in declaration specifiers",
12390 : : "long", "__auto_type");
12391 : 1837 : else if (specs->short_p)
12392 : 1 : error_at (loc,
12393 : : "both %qs and %qs in declaration specifiers",
12394 : : "short", "__auto_type");
12395 : 1836 : else if (specs->signed_p)
12396 : 1 : error_at (loc,
12397 : : "both %qs and %qs in declaration specifiers",
12398 : : "signed", "__auto_type");
12399 : 1835 : else if (specs->unsigned_p)
12400 : 1 : error_at (loc,
12401 : : "both %qs and %qs in declaration specifiers",
12402 : : "unsigned", "__auto_type");
12403 : 1834 : else if (specs->complex_p)
12404 : 1 : error_at (loc,
12405 : : "both %qs and %qs in declaration specifiers",
12406 : : "complex", "__auto_type");
12407 : 1833 : else if (specs->saturating_p)
12408 : 0 : error_at (loc,
12409 : : "both %qs and %qs in declaration specifiers",
12410 : : "_Sat", "__auto_type");
12411 : : else
12412 : : {
12413 : 1833 : specs->typespec_word = cts_auto_type;
12414 : 1833 : specs->locations[cdw_typespec] = loc;
12415 : : }
12416 : 1838 : return specs;
12417 : 51306 : case RID_INT_N_0:
12418 : 51306 : case RID_INT_N_1:
12419 : 51306 : case RID_INT_N_2:
12420 : 51306 : case RID_INT_N_3:
12421 : 51306 : specs->u.int_n_idx = i - RID_INT_N_0;
12422 : 51306 : if (!in_system_header_at (input_location)
12423 : : /* If the INT_N type ends in "__", and so is of the format
12424 : : "__intN__", don't pedwarn. */
12425 : 51306 : && (strncmp (IDENTIFIER_POINTER (type)
12426 : 40948 : + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
12427 : 40948 : pedwarn (loc, OPT_Wpedantic,
12428 : : "ISO C does not support %<__int%d%> types",
12429 : 40948 : int_n_data[specs->u.int_n_idx].bitsize);
12430 : :
12431 : 51306 : if (specs->long_p)
12432 : 53 : error_at (loc,
12433 : : "both %<__int%d%> and %qs in declaration specifiers",
12434 : 53 : int_n_data[specs->u.int_n_idx].bitsize, "long");
12435 : 51253 : else if (specs->saturating_p)
12436 : 0 : error_at (loc,
12437 : : "both %qs and %<__int%d%> in declaration specifiers",
12438 : 0 : "_Sat", int_n_data[specs->u.int_n_idx].bitsize);
12439 : 51253 : else if (specs->short_p)
12440 : 19 : error_at (loc,
12441 : : "both %<__int%d%> and %qs in declaration specifiers",
12442 : 19 : int_n_data[specs->u.int_n_idx].bitsize, "short");
12443 : 51234 : else if (! int_n_enabled_p[specs->u.int_n_idx])
12444 : : {
12445 : 0 : specs->typespec_word = cts_int_n;
12446 : 0 : error_at (loc,
12447 : : "%<__int%d%> is not supported on this target",
12448 : 0 : int_n_data[specs->u.int_n_idx].bitsize);
12449 : : }
12450 : : else
12451 : : {
12452 : 51234 : specs->typespec_word = cts_int_n;
12453 : 51234 : specs->locations[cdw_typespec] = loc;
12454 : : }
12455 : 51306 : return specs;
12456 : 7674343 : case RID_VOID:
12457 : 7674343 : if (specs->long_p)
12458 : 44 : error_at (loc,
12459 : : "both %qs and %qs in declaration specifiers",
12460 : : "long", "void");
12461 : 7674299 : else if (specs->short_p)
12462 : 21 : error_at (loc,
12463 : : "both %qs and %qs in declaration specifiers",
12464 : : "short", "void");
12465 : 7674278 : else if (specs->signed_p)
12466 : 5 : error_at (loc,
12467 : : "both %qs and %qs in declaration specifiers",
12468 : : "signed", "void");
12469 : 7674273 : else if (specs->unsigned_p)
12470 : 5 : error_at (loc,
12471 : : "both %qs and %qs in declaration specifiers",
12472 : : "unsigned", "void");
12473 : 7674268 : else if (specs->complex_p)
12474 : 2 : error_at (loc,
12475 : : "both %qs and %qs in declaration specifiers",
12476 : : "complex", "void");
12477 : 7674266 : else if (specs->saturating_p)
12478 : 0 : error_at (loc,
12479 : : "both %qs and %qs in declaration specifiers",
12480 : : "_Sat", "void");
12481 : : else
12482 : : {
12483 : 7674266 : specs->typespec_word = cts_void;
12484 : 7674266 : specs->locations[cdw_typespec] = loc;
12485 : : }
12486 : 7674343 : return specs;
12487 : 82557 : case RID_BOOL:
12488 : 82557 : if (!in_system_header_at (loc))
12489 : 68238 : pedwarn_c90 (loc, OPT_Wpedantic,
12490 : : "ISO C90 does not support boolean types");
12491 : 82557 : if (specs->long_p)
12492 : 27 : error_at (loc,
12493 : : "both %qs and %qs in declaration specifiers",
12494 : : "long", "_Bool");
12495 : 82530 : else if (specs->short_p)
12496 : 11 : error_at (loc,
12497 : : "both %qs and %qs in declaration specifiers",
12498 : : "short", "_Bool");
12499 : 82519 : else if (specs->signed_p)
12500 : 3 : error_at (loc,
12501 : : "both %qs and %qs in declaration specifiers",
12502 : : "signed", "_Bool");
12503 : 82516 : else if (specs->unsigned_p)
12504 : 3 : error_at (loc,
12505 : : "both %qs and %qs in declaration specifiers",
12506 : : "unsigned", "_Bool");
12507 : 82513 : else if (specs->complex_p)
12508 : 2 : error_at (loc,
12509 : : "both %qs and %qs in declaration specifiers",
12510 : : "complex", "_Bool");
12511 : 82511 : else if (specs->saturating_p)
12512 : 0 : error_at (loc,
12513 : : "both %qs and %qs in declaration specifiers",
12514 : : "_Sat", "_Bool");
12515 : : else
12516 : : {
12517 : 82511 : specs->typespec_word = cts_bool;
12518 : 82511 : specs->locations[cdw_typespec] = loc;
12519 : : }
12520 : 82557 : return specs;
12521 : 8934420 : case RID_CHAR:
12522 : 8934420 : if (specs->long_p)
12523 : 44 : error_at (loc,
12524 : : "both %qs and %qs in declaration specifiers",
12525 : : "long", "char");
12526 : 8934376 : else if (specs->short_p)
12527 : 21 : error_at (loc,
12528 : : "both %qs and %qs in declaration specifiers",
12529 : : "short", "char");
12530 : 8934355 : else if (specs->saturating_p)
12531 : 0 : error_at (loc,
12532 : : "both %qs and %qs in declaration specifiers",
12533 : : "_Sat", "char");
12534 : : else
12535 : : {
12536 : 8934355 : specs->typespec_word = cts_char;
12537 : 8934355 : specs->locations[cdw_typespec] = loc;
12538 : : }
12539 : 8934420 : return specs;
12540 : 23354210 : case RID_INT:
12541 : 23354210 : if (specs->saturating_p)
12542 : 0 : error_at (loc,
12543 : : "both %qs and %qs in declaration specifiers",
12544 : : "_Sat", "int");
12545 : : else
12546 : : {
12547 : 23354210 : specs->typespec_word = cts_int;
12548 : 23354210 : specs->locations[cdw_typespec] = loc;
12549 : : }
12550 : 23354210 : return specs;
12551 : 3336935 : case RID_FLOAT:
12552 : 3336935 : if (specs->long_p)
12553 : 44 : error_at (loc,
12554 : : "both %qs and %qs in declaration specifiers",
12555 : : "long", "float");
12556 : 3336891 : else if (specs->short_p)
12557 : 21 : error_at (loc,
12558 : : "both %qs and %qs in declaration specifiers",
12559 : : "short", "float");
12560 : 3336870 : else if (specs->signed_p)
12561 : 5 : error_at (loc,
12562 : : "both %qs and %qs in declaration specifiers",
12563 : : "signed", "float");
12564 : 3336865 : else if (specs->unsigned_p)
12565 : 5 : error_at (loc,
12566 : : "both %qs and %qs in declaration specifiers",
12567 : : "unsigned", "float");
12568 : 3336860 : else if (specs->saturating_p)
12569 : 0 : error_at (loc,
12570 : : "both %qs and %qs in declaration specifiers",
12571 : : "_Sat", "float");
12572 : : else
12573 : : {
12574 : 3336860 : specs->typespec_word = cts_float;
12575 : 3336860 : specs->locations[cdw_typespec] = loc;
12576 : : }
12577 : 3336935 : return specs;
12578 : 6015953 : case RID_DOUBLE:
12579 : 6015953 : if (specs->long_long_p)
12580 : 22 : error_at (loc,
12581 : : "both %qs and %qs in declaration specifiers",
12582 : : "long long", "double");
12583 : 6015931 : else if (specs->short_p)
12584 : 21 : error_at (loc,
12585 : : "both %qs and %qs in declaration specifiers",
12586 : : "short", "double");
12587 : 6015910 : else if (specs->signed_p)
12588 : 13 : error_at (loc,
12589 : : "both %qs and %qs in declaration specifiers",
12590 : : "signed", "double");
12591 : 6015897 : else if (specs->unsigned_p)
12592 : 13 : error_at (loc,
12593 : : "both %qs and %qs in declaration specifiers",
12594 : : "unsigned", "double");
12595 : 6015884 : else if (specs->saturating_p)
12596 : 0 : error_at (loc,
12597 : : "both %qs and %qs in declaration specifiers",
12598 : : "_Sat", "double");
12599 : : else
12600 : : {
12601 : 6015884 : specs->typespec_word = cts_double;
12602 : 6015884 : specs->locations[cdw_typespec] = loc;
12603 : : }
12604 : 6015953 : return specs;
12605 : 10140528 : CASE_RID_FLOATN_NX:
12606 : 10140528 : specs->u.floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
12607 : 10140528 : if (!in_system_header_at (input_location))
12608 : 51467 : pedwarn_c11 (loc, OPT_Wpedantic,
12609 : : "ISO C does not support the %<_Float%d%s%> type"
12610 : : " before C23",
12611 : 51467 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12612 : 51467 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12613 : : ? "x"
12614 : : : "");
12615 : :
12616 : 10140528 : if (specs->long_p)
12617 : 0 : error_at (loc,
12618 : : "both %qs and %<_Float%d%s%> in declaration "
12619 : : "specifiers", "long",
12620 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12621 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12622 : : ? "x"
12623 : : : "");
12624 : 10140528 : else if (specs->short_p)
12625 : 0 : error_at (loc,
12626 : : "both %qs and %<_Float%d%s%> in declaration "
12627 : : "specifiers", "short",
12628 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12629 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12630 : : ? "x"
12631 : : : "");
12632 : 10140528 : else if (specs->signed_p)
12633 : 0 : error_at (loc,
12634 : : "both %qs and %<_Float%d%s%> in declaration "
12635 : : "specifiers", "signed",
12636 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12637 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12638 : : ? "x"
12639 : : : "");
12640 : 10140528 : else if (specs->unsigned_p)
12641 : 0 : error_at (loc,
12642 : : "both %qs and %<_Float%d%s%> in declaration "
12643 : : "specifiers", "unsigned",
12644 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12645 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12646 : : ? "x"
12647 : : : "");
12648 : 10140528 : else if (specs->saturating_p)
12649 : 0 : error_at (loc,
12650 : : "both %qs and %<_Float%d%s%> in declaration "
12651 : : "specifiers", "_Sat",
12652 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12653 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12654 : : ? "x"
12655 : : : "");
12656 : 10140528 : else if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
12657 : : {
12658 : 88 : specs->typespec_word = cts_floatn_nx;
12659 : 176 : error_at (loc,
12660 : : "%<_Float%d%s%> is not supported on this target",
12661 : 88 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12662 : 88 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12663 : : ? "x"
12664 : : : "");
12665 : : }
12666 : : else
12667 : : {
12668 : 10140440 : specs->typespec_word = cts_floatn_nx;
12669 : 10140440 : specs->locations[cdw_typespec] = loc;
12670 : : }
12671 : 10140528 : return specs;
12672 : 47631 : case RID_DFLOAT32:
12673 : 47631 : case RID_DFLOAT64:
12674 : 47631 : case RID_DFLOAT128:
12675 : 47631 : case RID_DFLOAT64X:
12676 : 47631 : {
12677 : 47631 : const char *str;
12678 : 47631 : if (i == RID_DFLOAT32)
12679 : : str = "_Decimal32";
12680 : : else if (i == RID_DFLOAT64)
12681 : : str = "_Decimal64";
12682 : : else if (i == RID_DFLOAT128)
12683 : : str = "_Decimal128";
12684 : : else
12685 : 47631 : str = "_Decimal64x";
12686 : 47631 : if (specs->long_long_p)
12687 : 18 : error_at (loc,
12688 : : "both %qs and %qs in declaration specifiers",
12689 : : "long long", str);
12690 : 47631 : if (specs->long_p)
12691 : 33 : error_at (loc,
12692 : : "both %qs and %qs in declaration specifiers",
12693 : : "long", str);
12694 : 47598 : else if (specs->short_p)
12695 : 18 : error_at (loc,
12696 : : "both %qs and %qs in declaration specifiers",
12697 : : "short", str);
12698 : 47580 : else if (specs->signed_p)
12699 : 6 : error_at (loc,
12700 : : "both %qs and %qs in declaration specifiers",
12701 : : "signed", str);
12702 : 47574 : else if (specs->unsigned_p)
12703 : 3 : error_at (loc,
12704 : : "both %qs and %qs in declaration specifiers",
12705 : : "unsigned", str);
12706 : 47571 : else if (specs->complex_p)
12707 : 3 : error_at (loc,
12708 : : "both %qs and %qs in declaration specifiers",
12709 : : "complex", str);
12710 : 47568 : else if (specs->saturating_p)
12711 : 0 : error_at (loc,
12712 : : "both %qs and %qs in declaration specifiers",
12713 : : "_Sat", str);
12714 : 47568 : else if (i == RID_DFLOAT32)
12715 : 15957 : specs->typespec_word = cts_dfloat32;
12716 : 31611 : else if (i == RID_DFLOAT64)
12717 : 15831 : specs->typespec_word = cts_dfloat64;
12718 : 15780 : else if (i == RID_DFLOAT128)
12719 : 15733 : specs->typespec_word = cts_dfloat128;
12720 : : else
12721 : 47 : specs->typespec_word = cts_dfloat64x;
12722 : 47631 : specs->locations[cdw_typespec] = loc;
12723 : : }
12724 : 47631 : if (!targetm.decimal_float_supported_p ())
12725 : 0 : error_at (loc,
12726 : : "decimal floating-point not supported "
12727 : : "for this target");
12728 : 47631 : pedwarn_c11 (loc, OPT_Wpedantic,
12729 : : "ISO C does not support decimal floating-point "
12730 : : "before C23");
12731 : 47631 : return specs;
12732 : 75 : case RID_FRACT:
12733 : 75 : case RID_ACCUM:
12734 : 75 : {
12735 : 75 : const char *str;
12736 : 75 : if (i == RID_FRACT)
12737 : : str = "_Fract";
12738 : : else
12739 : 37 : str = "_Accum";
12740 : 75 : if (specs->complex_p)
12741 : 0 : error_at (loc,
12742 : : "both %qs and %qs in declaration specifiers",
12743 : : "complex", str);
12744 : 75 : else if (i == RID_FRACT)
12745 : 38 : specs->typespec_word = cts_fract;
12746 : : else
12747 : 37 : specs->typespec_word = cts_accum;
12748 : 75 : specs->locations[cdw_typespec] = loc;
12749 : : }
12750 : 75 : if (!targetm.fixed_point_supported_p ())
12751 : 75 : error_at (loc,
12752 : : "fixed-point types not supported for this target");
12753 : 75 : pedwarn (loc, OPT_Wpedantic,
12754 : : "ISO C does not support fixed-point types");
12755 : 75 : return specs;
12756 : 45586 : case RID_BITINT:
12757 : 45586 : if (specs->long_p)
12758 : 2 : error_at (loc,
12759 : : "both %qs and %qs in declaration specifiers",
12760 : : "long", "_BitInt");
12761 : 45584 : else if (specs->short_p)
12762 : 1 : error_at (loc,
12763 : : "both %qs and %qs in declaration specifiers",
12764 : : "short", "_BitInt");
12765 : 45583 : else if (specs->complex_p)
12766 : 1 : error_at (loc,
12767 : : "both %qs and %qs in declaration specifiers",
12768 : : "complex", "_BitInt");
12769 : 45582 : else if (specs->saturating_p)
12770 : 0 : error_at (loc,
12771 : : "both %qs and %qs in declaration specifiers",
12772 : : "_Sat", "_BitInt");
12773 : : else
12774 : : {
12775 : 45582 : specs->typespec_word = cts_bitint;
12776 : 45582 : specs->locations[cdw_typespec] = loc;
12777 : 45582 : specs->u.bitint_prec = -1;
12778 : 45582 : if (error_operand_p (spec.expr))
12779 : 7 : return specs;
12780 : 45582 : if (TREE_CODE (spec.expr) != INTEGER_CST
12781 : 45582 : || !INTEGRAL_TYPE_P (TREE_TYPE (spec.expr)))
12782 : : {
12783 : 1 : error_at (loc, "%<_BitInt%> argument is not an integer "
12784 : : "constant expression");
12785 : 1 : return specs;
12786 : : }
12787 : 45581 : if (tree_int_cst_sgn (spec.expr) <= 0)
12788 : : {
12789 : 4 : error_at (loc, "%<_BitInt%> argument %qE is not a "
12790 : : "positive integer constant expression",
12791 : : spec.expr);
12792 : 4 : return specs;
12793 : : }
12794 : 45577 : if (wi::to_widest (spec.expr) > WIDE_INT_MAX_PRECISION - 1)
12795 : : {
12796 : 2 : error_at (loc, "%<_BitInt%> argument %qE is larger than "
12797 : : "%<BITINT_MAXWIDTH%> %qd",
12798 : : spec.expr, (int) WIDE_INT_MAX_PRECISION - 1);
12799 : 2 : return specs;
12800 : : }
12801 : 45575 : specs->u.bitint_prec = tree_to_uhwi (spec.expr);
12802 : 45575 : struct bitint_info info;
12803 : 45575 : if (!targetm.c.bitint_type_info (specs->u.bitint_prec,
12804 : : &info))
12805 : : {
12806 : 0 : sorry_at (loc, "%<_BitInt(%d)%> is not supported on "
12807 : : "this target", specs->u.bitint_prec);
12808 : 0 : specs->u.bitint_prec = -1;
12809 : 0 : return specs;
12810 : : }
12811 : : }
12812 : 45579 : return specs;
12813 : : default:
12814 : : /* ObjC reserved word "id", handled below. */
12815 : : break;
12816 : : }
12817 : : }
12818 : : }
12819 : :
12820 : : /* Now we have a typedef (a TYPE_DECL node), an identifier (some
12821 : : form of ObjC type, cases such as "int" and "long" being handled
12822 : : above), a TYPE (struct, union, enum and typeof specifiers) or an
12823 : : ERROR_MARK. In none of these cases may there have previously
12824 : : been any type specifiers. */
12825 : 242774424 : if (specs->type || specs->typespec_word != cts_none
12826 : 242774412 : || specs->long_p || specs->short_p || specs->signed_p
12827 : 242774409 : || specs->unsigned_p || specs->complex_p)
12828 : 21 : error_at (loc, "two or more data types in declaration specifiers");
12829 : 242774403 : else if (TREE_CODE (type) == TYPE_DECL)
12830 : : {
12831 : 239561888 : mark_decl_used (type, false);
12832 : 239561888 : specs->type = TREE_TYPE (type);
12833 : 239561888 : if (TREE_TYPE (type) != error_mark_node)
12834 : : {
12835 : 239561875 : specs->decl_attr = DECL_ATTRIBUTES (type);
12836 : 239561875 : specs->typedef_p = true;
12837 : 239561875 : specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
12838 : 239561875 : specs->locations[cdw_typedef] = loc;
12839 : :
12840 : : /* If this typedef name is defined in a struct, then a C++
12841 : : lookup would return a different value. */
12842 : 239561875 : if (warn_cxx_compat
12843 : 239561875 : && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
12844 : 2 : warning_at (loc, OPT_Wc___compat,
12845 : : "C++ lookup of %qD would return a field, not a type",
12846 : : type);
12847 : :
12848 : : /* If we are parsing a struct, record that a struct field
12849 : : used a typedef. */
12850 : 239561875 : if (warn_cxx_compat && struct_parse_info != NULL)
12851 : 1751 : struct_parse_info->typedefs_seen.safe_push (type);
12852 : : }
12853 : : }
12854 : 3212515 : else if (TREE_CODE (type) == IDENTIFIER_NODE)
12855 : : {
12856 : 0 : tree t = lookup_name (type);
12857 : 0 : if (!t || TREE_CODE (t) != TYPE_DECL)
12858 : 0 : error_at (loc, "%qE fails to be a typedef or built in type", type);
12859 : 0 : else if (TREE_TYPE (t) == error_mark_node)
12860 : : ;
12861 : : else
12862 : : {
12863 : 0 : specs->type = TREE_TYPE (t);
12864 : 0 : specs->locations[cdw_typespec] = loc;
12865 : : }
12866 : : }
12867 : : else
12868 : : {
12869 : 3212515 : if (TREE_CODE (type) != ERROR_MARK)
12870 : : {
12871 : 3212310 : if (spec.kind == ctsk_typeof)
12872 : : {
12873 : 819731 : specs->typedef_p = true;
12874 : 819731 : specs->locations[cdw_typedef] = loc;
12875 : : }
12876 : 3212310 : if (spec.expr)
12877 : : {
12878 : 909 : if (specs->expr)
12879 : 0 : specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
12880 : : specs->expr, spec.expr);
12881 : : else
12882 : 909 : specs->expr = spec.expr;
12883 : 909 : specs->expr_const_operands &= spec.expr_const_operands;
12884 : : }
12885 : : }
12886 : 3212515 : specs->type = type;
12887 : 3212515 : if (spec.has_enum_type_specifier
12888 : 168 : && spec.kind != ctsk_tagdef)
12889 : 49 : specs->enum_type_specifier_ref_p = true;
12890 : : }
12891 : :
12892 : : return specs;
12893 : : }
12894 : :
12895 : : /* Add the storage class specifier or function specifier SCSPEC to the
12896 : : declaration specifiers SPECS, returning SPECS. */
12897 : :
12898 : : struct c_declspecs *
12899 : 88215237 : declspecs_add_scspec (location_t loc,
12900 : : struct c_declspecs *specs,
12901 : : tree scspec)
12902 : : {
12903 : 88215237 : enum rid i;
12904 : 88215237 : enum c_storage_class n = csc_none;
12905 : 88215237 : bool dupe = false;
12906 : 88215237 : specs->declspecs_seen_p = true;
12907 : 88215237 : specs->non_std_attrs_seen_p = true;
12908 : 88215237 : gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
12909 : : && C_IS_RESERVED_WORD (scspec));
12910 : 88215237 : i = C_RID_CODE (scspec);
12911 : 88215237 : if (specs->non_sc_seen_p)
12912 : 2074 : warning (OPT_Wold_style_declaration,
12913 : : "%qE is not at beginning of declaration", scspec);
12914 : 88215237 : switch (i)
12915 : : {
12916 : 34705731 : case RID_INLINE:
12917 : : /* C99 permits duplicate inline. Although of doubtful utility,
12918 : : it seems simplest to permit it in gnu89 mode as well, as
12919 : : there is also little utility in maintaining this as a
12920 : : difference between gnu89 and C99 inline. */
12921 : 34705731 : dupe = false;
12922 : 34705731 : specs->inline_p = true;
12923 : 34705731 : specs->locations[cdw_inline] = loc;
12924 : 34705731 : break;
12925 : 22965 : case RID_NORETURN:
12926 : : /* Duplicate _Noreturn is permitted. */
12927 : 22965 : dupe = false;
12928 : 22965 : specs->noreturn_p = true;
12929 : 22965 : specs->locations[cdw_noreturn] = loc;
12930 : 22965 : break;
12931 : 2852 : case RID_THREAD:
12932 : 2852 : dupe = specs->thread_p;
12933 : 2852 : if (specs->storage_class == csc_auto)
12934 : 2 : error ("%qE used with %<auto%>", scspec);
12935 : 2850 : else if (specs->storage_class == csc_register)
12936 : 3 : error ("%qE used with %<register%>", scspec);
12937 : 2847 : else if (specs->storage_class == csc_typedef)
12938 : 2 : error ("%qE used with %<typedef%>", scspec);
12939 : 2845 : else if (specs->constexpr_p)
12940 : 2 : error ("%qE used with %<constexpr%>", scspec);
12941 : : else
12942 : : {
12943 : 2843 : specs->thread_p = true;
12944 : 2843 : specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
12945 : 2843 : "__thread") == 0);
12946 : : /* A diagnostic is not required for the use of this
12947 : : identifier in the implementation namespace; only diagnose
12948 : : it for the C11 spelling because of existing code using
12949 : : the other spelling. */
12950 : 2843 : if (!specs->thread_gnu_p)
12951 : : {
12952 : 114 : if (flag_isoc99)
12953 : 111 : pedwarn_c99 (loc, OPT_Wpedantic,
12954 : : "ISO C99 does not support %qE", scspec);
12955 : : else
12956 : 3 : pedwarn_c99 (loc, OPT_Wpedantic,
12957 : : "ISO C90 does not support %qE", scspec);
12958 : : }
12959 : 2843 : specs->locations[cdw_thread] = loc;
12960 : : }
12961 : : break;
12962 : 262 : case RID_AUTO:
12963 : 262 : if (flag_isoc23
12964 : 223 : && specs->typespec_kind == ctsk_none
12965 : 212 : && specs->storage_class != csc_typedef)
12966 : : {
12967 : : /* "auto" potentially used for type deduction. */
12968 : 211 : if (specs->c23_auto_p)
12969 : 2 : error ("duplicate %qE", scspec);
12970 : 211 : specs->c23_auto_p = true;
12971 : 211 : return specs;
12972 : : }
12973 : 51 : n = csc_auto;
12974 : : /* auto may only be used with another storage class specifier,
12975 : : such as constexpr, if the type is inferred. */
12976 : 51 : if (specs->constexpr_p)
12977 : 2 : error ("%qE used with %<constexpr%>", scspec);
12978 : : break;
12979 : 48849072 : case RID_EXTERN:
12980 : 48849072 : n = csc_extern;
12981 : : /* Diagnose "__thread extern". */
12982 : 48849072 : if (specs->thread_p && specs->thread_gnu_p)
12983 : 2 : error ("%<__thread%> before %<extern%>");
12984 : : break;
12985 : : case RID_REGISTER:
12986 : : n = csc_register;
12987 : : break;
12988 : 412666 : case RID_STATIC:
12989 : 412666 : n = csc_static;
12990 : : /* Diagnose "__thread static". */
12991 : 412666 : if (specs->thread_p && specs->thread_gnu_p)
12992 : 1 : error ("%<__thread%> before %<static%>");
12993 : : break;
12994 : 4217954 : case RID_TYPEDEF:
12995 : 4217954 : n = csc_typedef;
12996 : 4217954 : if (specs->c23_auto_p)
12997 : : {
12998 : 1 : error ("%<typedef%> used with %<auto%>");
12999 : 1 : specs->c23_auto_p = false;
13000 : : }
13001 : : break;
13002 : 610 : case RID_CONSTEXPR:
13003 : 610 : dupe = specs->constexpr_p;
13004 : 610 : if (specs->storage_class == csc_extern)
13005 : 1 : error ("%qE used with %<extern%>", scspec);
13006 : 609 : else if (specs->storage_class == csc_typedef)
13007 : 1 : error ("%qE used with %<typedef%>", scspec);
13008 : 608 : else if (specs->storage_class == csc_auto)
13009 : : /* auto may only be used with another storage class specifier,
13010 : : such as constexpr, if the type is inferred. */
13011 : 2 : error ("%qE used with %<auto%>", scspec);
13012 : 606 : else if (specs->thread_p)
13013 : 4 : error ("%qE used with %qs", scspec,
13014 : 2 : specs->thread_gnu_p ? "__thread" : "_Thread_local");
13015 : : else
13016 : 604 : specs->constexpr_p = true;
13017 : : break;
13018 : 0 : default:
13019 : 0 : gcc_unreachable ();
13020 : : }
13021 : 88215032 : if (n != csc_none && n == specs->storage_class)
13022 : : dupe = true;
13023 : 88215018 : if (dupe)
13024 : : {
13025 : 12 : if (i == RID_THREAD)
13026 : 2 : error ("duplicate %<_Thread_local%> or %<__thread%>");
13027 : : else
13028 : 10 : error ("duplicate %qE", scspec);
13029 : : }
13030 : 88215026 : if (n != csc_none)
13031 : : {
13032 : 53482868 : if (specs->storage_class != csc_none && n != specs->storage_class)
13033 : : {
13034 : 7 : error ("multiple storage classes in declaration specifiers");
13035 : : }
13036 : : else
13037 : : {
13038 : 53482861 : specs->storage_class = n;
13039 : 53482861 : specs->locations[cdw_storage_class] = loc;
13040 : 53482861 : if (n != csc_extern && n != csc_static && specs->thread_p)
13041 : : {
13042 : 8 : error ("%qs used with %qE",
13043 : 8 : specs->thread_gnu_p ? "__thread" : "_Thread_local",
13044 : : scspec);
13045 : 8 : specs->thread_p = false;
13046 : : }
13047 : 53482861 : if (n != csc_auto && n != csc_register && n != csc_static
13048 : 53067023 : && specs->constexpr_p)
13049 : : {
13050 : 2 : error ("%<constexpr%> used with %qE", scspec);
13051 : 2 : specs->constexpr_p = false;
13052 : : }
13053 : : }
13054 : : }
13055 : : return specs;
13056 : : }
13057 : :
13058 : : /* Add the attributes ATTRS to the declaration specifiers SPECS,
13059 : : returning SPECS. */
13060 : :
13061 : : struct c_declspecs *
13062 : 35008893 : declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
13063 : : {
13064 : 35008893 : specs->attrs = chainon (attrs, specs->attrs);
13065 : 35008893 : specs->locations[cdw_attributes] = loc;
13066 : 35008893 : specs->declspecs_seen_p = true;
13067 : : /* In the case of standard attributes at the start of the
13068 : : declaration, the caller will reset this. */
13069 : 35008893 : specs->non_std_attrs_seen_p = true;
13070 : 35008893 : return specs;
13071 : : }
13072 : :
13073 : : /* Add an _Alignas specifier (expression ALIGN, or type whose
13074 : : alignment is ALIGN) to the declaration specifiers SPECS, returning
13075 : : SPECS. */
13076 : : struct c_declspecs *
13077 : 200 : declspecs_add_alignas (location_t loc,
13078 : : struct c_declspecs *specs, tree align)
13079 : : {
13080 : 200 : specs->alignas_p = true;
13081 : 200 : specs->locations[cdw_alignas] = loc;
13082 : 200 : if (align == error_mark_node)
13083 : : return specs;
13084 : :
13085 : : /* Only accept the alignment if it's valid and greater than
13086 : : the current one. Zero is invalid but by C11 required to
13087 : : be silently ignored. */
13088 : 198 : int align_log = check_user_alignment (align, false, /* warn_zero = */false);
13089 : 198 : if (align_log > specs->align_log)
13090 : 165 : specs->align_log = align_log;
13091 : : return specs;
13092 : : }
13093 : :
13094 : : /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
13095 : : specifiers with any other type specifier to determine the resulting
13096 : : type. This is where ISO C checks on complex types are made, since
13097 : : "_Complex long" is a prefix of the valid ISO C type "_Complex long
13098 : : double". Also apply postfix standard attributes to modify the type. */
13099 : :
13100 : : struct c_declspecs *
13101 : 306557649 : finish_declspecs (struct c_declspecs *specs)
13102 : : {
13103 : : /* If a type was specified as a whole, we have no modifiers and are
13104 : : done. */
13105 : 306557649 : if (specs->type != NULL_TREE)
13106 : : {
13107 : 242774357 : gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
13108 : : && !specs->signed_p && !specs->unsigned_p
13109 : : && !specs->complex_p && !specs->c23_auto_p);
13110 : :
13111 : : /* Set a dummy type. */
13112 : 242774357 : if (TREE_CODE (specs->type) == ERROR_MARK)
13113 : 181 : specs->type = integer_type_node;
13114 : 242774357 : goto handle_postfix_attrs;
13115 : : }
13116 : :
13117 : : /* If none of "void", "_Bool", "char", "int", "float" or "double"
13118 : : has been specified, treat it as "int" unless "_Complex" is
13119 : : present and there are no other specifiers. If we just have
13120 : : "_Complex", it is equivalent to "_Complex double", but e.g.
13121 : : "_Complex short" is equivalent to "_Complex short int". */
13122 : 63783292 : if (specs->typespec_word == cts_none)
13123 : : {
13124 : 4098388 : if (specs->saturating_p)
13125 : : {
13126 : 1 : error_at (specs->locations[cdw_saturating],
13127 : : "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
13128 : 1 : if (!targetm.fixed_point_supported_p ())
13129 : 1 : error_at (specs->locations[cdw_saturating],
13130 : : "fixed-point types not supported for this target");
13131 : 1 : specs->typespec_word = cts_fract;
13132 : : }
13133 : 4098387 : else if (specs->long_p || specs->short_p
13134 : 189788 : || specs->signed_p || specs->unsigned_p)
13135 : : {
13136 : 4087954 : specs->typespec_word = cts_int;
13137 : : }
13138 : 10433 : else if (specs->complex_p)
13139 : : {
13140 : 130 : specs->typespec_word = cts_double;
13141 : 130 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13142 : : "ISO C does not support plain %<complex%> meaning "
13143 : : "%<double complex%>");
13144 : : }
13145 : 10303 : else if (specs->c23_auto_p)
13146 : : {
13147 : : /* Type to be filled in later, including applying postfix
13148 : : attributes. This warning only actually appears for
13149 : : -Wc11-c23-compat in C23 mode; in older modes, there may
13150 : : be a warning or pedwarn for implicit "int" instead, or
13151 : : other errors for use of auto at file scope. */
13152 : 93 : pedwarn_c11 (input_location, OPT_Wpedantic,
13153 : : "ISO C does not support %<auto%> type deduction "
13154 : : "before C23");
13155 : 93 : return specs;
13156 : : }
13157 : : else
13158 : : {
13159 : 10210 : specs->typespec_word = cts_int;
13160 : 10210 : specs->default_int_p = true;
13161 : : /* We don't diagnose this here because grokdeclarator will
13162 : : give more specific diagnostics according to whether it is
13163 : : a function definition. */
13164 : : }
13165 : : }
13166 : :
13167 : : /* If "signed" was specified, record this to distinguish "int" and
13168 : : "signed int" in the case of a bit-field with
13169 : : -funsigned-bitfields. */
13170 : 63783199 : specs->explicit_signed_p = specs->signed_p;
13171 : :
13172 : : /* Now compute the actual type. */
13173 : 63783199 : gcc_assert (!specs->c23_auto_p);
13174 : 63783199 : switch (specs->typespec_word)
13175 : : {
13176 : 1833 : case cts_auto_type:
13177 : 1833 : gcc_assert (!specs->long_p && !specs->short_p
13178 : : && !specs->signed_p && !specs->unsigned_p
13179 : : && !specs->complex_p);
13180 : : /* Type to be filled in later. */
13181 : 1833 : if (specs->postfix_attrs)
13182 : 2 : error ("%<__auto_type%> followed by %<[[]]%> attributes");
13183 : : break;
13184 : 7674266 : case cts_void:
13185 : 7674266 : gcc_assert (!specs->long_p && !specs->short_p
13186 : : && !specs->signed_p && !specs->unsigned_p
13187 : : && !specs->complex_p);
13188 : 7674266 : specs->type = void_type_node;
13189 : 7674266 : break;
13190 : 82511 : case cts_bool:
13191 : 82511 : gcc_assert (!specs->long_p && !specs->short_p
13192 : : && !specs->signed_p && !specs->unsigned_p
13193 : : && !specs->complex_p);
13194 : 82511 : specs->type = boolean_type_node;
13195 : 82511 : break;
13196 : 8934355 : case cts_char:
13197 : 8934355 : gcc_assert (!specs->long_p && !specs->short_p);
13198 : 8934355 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13199 : 8934355 : if (specs->signed_p)
13200 : 188955 : specs->type = signed_char_type_node;
13201 : 8745400 : else if (specs->unsigned_p)
13202 : 877088 : specs->type = unsigned_char_type_node;
13203 : : else
13204 : 7868312 : specs->type = char_type_node;
13205 : 8934355 : if (specs->complex_p)
13206 : : {
13207 : 3853 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13208 : : "ISO C does not support complex integer types");
13209 : 3853 : specs->type = build_complex_type (specs->type);
13210 : : }
13211 : : break;
13212 : 51234 : case cts_int_n:
13213 : 51234 : gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
13214 : 51234 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13215 : 51234 : if (! int_n_enabled_p[specs->u.int_n_idx])
13216 : 0 : specs->type = integer_type_node;
13217 : : else
13218 : 51234 : specs->type = (specs->unsigned_p
13219 : 51234 : ? int_n_trees[specs->u.int_n_idx].unsigned_type
13220 : : : int_n_trees[specs->u.int_n_idx].signed_type);
13221 : 51234 : if (specs->complex_p)
13222 : : {
13223 : 199 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13224 : : "ISO C does not support complex integer types");
13225 : 199 : specs->type = build_complex_type (specs->type);
13226 : : }
13227 : : break;
13228 : 27452372 : case cts_int:
13229 : 27452372 : gcc_assert (!(specs->long_p && specs->short_p));
13230 : 27452372 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13231 : 27452372 : if (specs->long_long_p)
13232 : 2889278 : specs->type = (specs->unsigned_p
13233 : 2889278 : ? long_long_unsigned_type_node
13234 : : : long_long_integer_type_node);
13235 : 24563094 : else if (specs->long_p)
13236 : 2161384 : specs->type = (specs->unsigned_p
13237 : 2161384 : ? long_unsigned_type_node
13238 : : : long_integer_type_node);
13239 : 22401710 : else if (specs->short_p)
13240 : 1664932 : specs->type = (specs->unsigned_p
13241 : 1664932 : ? short_unsigned_type_node
13242 : : : short_integer_type_node);
13243 : : else
13244 : 20736778 : specs->type = (specs->unsigned_p
13245 : 20736778 : ? unsigned_type_node
13246 : : : integer_type_node);
13247 : 27452372 : if (specs->complex_p)
13248 : : {
13249 : 14910 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13250 : : "ISO C does not support complex integer types");
13251 : 14910 : specs->type = build_complex_type (specs->type);
13252 : : }
13253 : : break;
13254 : 3336860 : case cts_float:
13255 : 3336860 : gcc_assert (!specs->long_p && !specs->short_p
13256 : : && !specs->signed_p && !specs->unsigned_p);
13257 : 6673720 : specs->type = (specs->complex_p
13258 : 3336860 : ? complex_float_type_node
13259 : : : float_type_node);
13260 : 3336860 : break;
13261 : 6016014 : case cts_double:
13262 : 6016014 : gcc_assert (!specs->long_long_p && !specs->short_p
13263 : : && !specs->signed_p && !specs->unsigned_p);
13264 : 6016014 : if (specs->long_p)
13265 : : {
13266 : 2588867 : specs->type = (specs->complex_p
13267 : 2588867 : ? complex_long_double_type_node
13268 : : : long_double_type_node);
13269 : : }
13270 : : else
13271 : : {
13272 : 3427147 : specs->type = (specs->complex_p
13273 : 3427147 : ? complex_double_type_node
13274 : : : double_type_node);
13275 : : }
13276 : : break;
13277 : 10140528 : case cts_floatn_nx:
13278 : 10140528 : gcc_assert (!specs->long_p && !specs->short_p
13279 : : && !specs->signed_p && !specs->unsigned_p);
13280 : 10140528 : if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
13281 : 88 : specs->type = integer_type_node;
13282 : 10140440 : else if (specs->complex_p)
13283 : 1535829 : specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13284 : : else
13285 : 8604611 : specs->type = FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13286 : : break;
13287 : 47568 : case cts_dfloat32:
13288 : 47568 : case cts_dfloat64:
13289 : 47568 : case cts_dfloat128:
13290 : 47568 : case cts_dfloat64x:
13291 : 47568 : gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
13292 : : && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
13293 : 47568 : if (!targetm.decimal_float_supported_p ())
13294 : 0 : specs->type = integer_type_node;
13295 : 47568 : else if (specs->typespec_word == cts_dfloat32)
13296 : 15957 : specs->type = dfloat32_type_node;
13297 : 31611 : else if (specs->typespec_word == cts_dfloat64)
13298 : 15831 : specs->type = dfloat64_type_node;
13299 : 15780 : else if (specs->typespec_word == cts_dfloat128)
13300 : 15733 : specs->type = dfloat128_type_node;
13301 : : else
13302 : 47 : specs->type = dfloat64x_type_node;
13303 : : break;
13304 : 39 : case cts_fract:
13305 : 39 : gcc_assert (!specs->complex_p);
13306 : 39 : if (!targetm.fixed_point_supported_p ())
13307 : 39 : specs->type = integer_type_node;
13308 : 0 : else if (specs->saturating_p)
13309 : : {
13310 : 0 : if (specs->long_long_p)
13311 : 0 : specs->type = specs->unsigned_p
13312 : 0 : ? sat_unsigned_long_long_fract_type_node
13313 : : : sat_long_long_fract_type_node;
13314 : 0 : else if (specs->long_p)
13315 : 0 : specs->type = specs->unsigned_p
13316 : 0 : ? sat_unsigned_long_fract_type_node
13317 : : : sat_long_fract_type_node;
13318 : 0 : else if (specs->short_p)
13319 : 0 : specs->type = specs->unsigned_p
13320 : 0 : ? sat_unsigned_short_fract_type_node
13321 : : : sat_short_fract_type_node;
13322 : : else
13323 : 0 : specs->type = specs->unsigned_p
13324 : 0 : ? sat_unsigned_fract_type_node
13325 : : : sat_fract_type_node;
13326 : : }
13327 : : else
13328 : : {
13329 : 0 : if (specs->long_long_p)
13330 : 0 : specs->type = specs->unsigned_p
13331 : 0 : ? unsigned_long_long_fract_type_node
13332 : : : long_long_fract_type_node;
13333 : 0 : else if (specs->long_p)
13334 : 0 : specs->type = specs->unsigned_p
13335 : 0 : ? unsigned_long_fract_type_node
13336 : : : long_fract_type_node;
13337 : 0 : else if (specs->short_p)
13338 : 0 : specs->type = specs->unsigned_p
13339 : 0 : ? unsigned_short_fract_type_node
13340 : : : short_fract_type_node;
13341 : : else
13342 : 0 : specs->type = specs->unsigned_p
13343 : 0 : ? unsigned_fract_type_node
13344 : : : fract_type_node;
13345 : : }
13346 : : break;
13347 : 37 : case cts_accum:
13348 : 37 : gcc_assert (!specs->complex_p);
13349 : 37 : if (!targetm.fixed_point_supported_p ())
13350 : 37 : specs->type = integer_type_node;
13351 : 0 : else if (specs->saturating_p)
13352 : : {
13353 : 0 : if (specs->long_long_p)
13354 : 0 : specs->type = specs->unsigned_p
13355 : 0 : ? sat_unsigned_long_long_accum_type_node
13356 : : : sat_long_long_accum_type_node;
13357 : 0 : else if (specs->long_p)
13358 : 0 : specs->type = specs->unsigned_p
13359 : 0 : ? sat_unsigned_long_accum_type_node
13360 : : : sat_long_accum_type_node;
13361 : 0 : else if (specs->short_p)
13362 : 0 : specs->type = specs->unsigned_p
13363 : 0 : ? sat_unsigned_short_accum_type_node
13364 : : : sat_short_accum_type_node;
13365 : : else
13366 : 0 : specs->type = specs->unsigned_p
13367 : 0 : ? sat_unsigned_accum_type_node
13368 : : : sat_accum_type_node;
13369 : : }
13370 : : else
13371 : : {
13372 : 0 : if (specs->long_long_p)
13373 : 0 : specs->type = specs->unsigned_p
13374 : 0 : ? unsigned_long_long_accum_type_node
13375 : : : long_long_accum_type_node;
13376 : 0 : else if (specs->long_p)
13377 : 0 : specs->type = specs->unsigned_p
13378 : 0 : ? unsigned_long_accum_type_node
13379 : : : long_accum_type_node;
13380 : 0 : else if (specs->short_p)
13381 : 0 : specs->type = specs->unsigned_p
13382 : 0 : ? unsigned_short_accum_type_node
13383 : : : short_accum_type_node;
13384 : : else
13385 : 0 : specs->type = specs->unsigned_p
13386 : 0 : ? unsigned_accum_type_node
13387 : : : accum_type_node;
13388 : : }
13389 : : break;
13390 : 45582 : case cts_bitint:
13391 : 45582 : gcc_assert (!specs->long_p && !specs->short_p
13392 : : && !specs->complex_p);
13393 : 45582 : if (!specs->unsigned_p && specs->u.bitint_prec == 1)
13394 : : {
13395 : 2 : error_at (specs->locations[cdw_typespec],
13396 : : "%<signed _BitInt%> argument must be at least 2");
13397 : 2 : specs->type = integer_type_node;
13398 : 2 : break;
13399 : : }
13400 : 45580 : if (specs->u.bitint_prec == -1)
13401 : 7 : specs->type = integer_type_node;
13402 : : else
13403 : : {
13404 : 67188 : pedwarn_c11 (specs->locations[cdw_typespec], OPT_Wpedantic,
13405 : : "ISO C does not support %<%s_BitInt(%d)%> before C23",
13406 : : specs->unsigned_p ? "unsigned "
13407 : 21615 : : specs->signed_p ? "signed " : "",
13408 : : specs->u.bitint_prec);
13409 : 45573 : specs->type = build_bitint_type (specs->u.bitint_prec,
13410 : 45573 : specs->unsigned_p);
13411 : : }
13412 : : break;
13413 : 0 : default:
13414 : 0 : gcc_unreachable ();
13415 : : }
13416 : 306557556 : handle_postfix_attrs:
13417 : 306557556 : if (specs->type != NULL)
13418 : : {
13419 : 306555723 : specs->postfix_attrs
13420 : 306555723 : = c_warn_type_attributes (specs->type, specs->postfix_attrs);
13421 : 306555723 : decl_attributes (&specs->type, specs->postfix_attrs, 0);
13422 : 306555723 : specs->postfix_attrs = NULL_TREE;
13423 : : }
13424 : :
13425 : : return specs;
13426 : : }
13427 : :
13428 : : /* Perform final processing on one file scope's declarations (or the
13429 : : external scope's declarations), GLOBALS. */
13430 : :
13431 : : static void
13432 : 207982 : c_write_global_declarations_1 (tree globals)
13433 : : {
13434 : 207982 : tree decl;
13435 : 207982 : bool reconsider;
13436 : :
13437 : : /* Process the decls in the order they were written. */
13438 : 379100843 : for (decl = globals; decl; decl = DECL_CHAIN (decl))
13439 : : {
13440 : : /* Check for used but undefined static functions using the C
13441 : : standard's definition of "used", and set TREE_NO_WARNING so
13442 : : that check_global_declaration doesn't repeat the check. */
13443 : 378892861 : if (TREE_CODE (decl) == FUNCTION_DECL
13444 : 361139391 : && DECL_INITIAL (decl) == NULL_TREE
13445 : 325761198 : && DECL_EXTERNAL (decl)
13446 : 704654054 : && !TREE_PUBLIC (decl))
13447 : : {
13448 : 102 : if (C_DECL_USED (decl))
13449 : : {
13450 : : /* TODO: Add OPT_Wundefined-inline. */
13451 : 20 : if (pedwarn (input_location, 0, "%q+F used but never defined",
13452 : : decl))
13453 : 19 : suppress_warning (decl /* OPT_Wundefined-inline. */);
13454 : : }
13455 : : /* For -Wunused-function warn about unused static prototypes. */
13456 : 82 : else if (warn_unused_function
13457 : 2 : && ! DECL_ARTIFICIAL (decl)
13458 : 84 : && ! warning_suppressed_p (decl, OPT_Wunused_function))
13459 : : {
13460 : 2 : if (warning (OPT_Wunused_function,
13461 : : "%q+F declared %<static%> but never defined",
13462 : : decl))
13463 : 2 : suppress_warning (decl, OPT_Wunused_function);
13464 : : }
13465 : : }
13466 : :
13467 : 378892861 : wrapup_global_declaration_1 (decl);
13468 : : }
13469 : :
13470 : 241150 : do
13471 : : {
13472 : 241150 : reconsider = false;
13473 : 489976240 : for (decl = globals; decl; decl = DECL_CHAIN (decl))
13474 : 489735090 : reconsider |= wrapup_global_declaration_2 (decl);
13475 : : }
13476 : : while (reconsider);
13477 : 207982 : }
13478 : :
13479 : : /* Preserve the external declarations scope across a garbage collect. */
13480 : : static GTY(()) tree ext_block;
13481 : :
13482 : : /* Collect all references relevant to SOURCE_FILE. */
13483 : :
13484 : : static void
13485 : 15 : collect_all_refs (const char *source_file)
13486 : : {
13487 : 15 : tree t;
13488 : 15 : unsigned i;
13489 : :
13490 : 30 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13491 : 15 : collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
13492 : :
13493 : 15 : collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
13494 : 15 : }
13495 : :
13496 : : /* Collect source file references at global level. */
13497 : :
13498 : : static void
13499 : 15 : collect_source_refs (void)
13500 : : {
13501 : 15 : tree t;
13502 : 15 : tree decls;
13503 : 15 : tree decl;
13504 : 15 : unsigned i;
13505 : :
13506 : 30 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13507 : : {
13508 : 15 : decls = DECL_INITIAL (t);
13509 : 82 : for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
13510 : 67 : if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13511 : 67 : collect_source_ref (DECL_SOURCE_FILE (decl));
13512 : : }
13513 : :
13514 : 44171 : for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
13515 : 44156 : if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13516 : 11 : collect_source_ref (DECL_SOURCE_FILE (decl));
13517 : 15 : }
13518 : :
13519 : : /* Free attribute access data that are not needed by the middle end. */
13520 : :
13521 : : static void
13522 : 103991 : free_attr_access_data ()
13523 : : {
13524 : 103991 : struct cgraph_node *n;
13525 : :
13526 : : /* Iterate over all functions declared in the translation unit. */
13527 : 70967610 : FOR_EACH_FUNCTION (n)
13528 : : {
13529 : 132888807 : for (tree parm = DECL_ARGUMENTS (n->decl); parm; parm = TREE_CHAIN (parm))
13530 : 97508993 : if (tree attrs = DECL_ATTRIBUTES (parm))
13531 : 95482 : attr_access::free_lang_data (attrs);
13532 : :
13533 : 35379814 : tree fntype = TREE_TYPE (n->decl);
13534 : 35379814 : if (!fntype || fntype == error_mark_node)
13535 : 0 : continue;
13536 : 35379814 : tree attrs = TYPE_ATTRIBUTES (fntype);
13537 : 35379814 : if (!attrs)
13538 : 35148215 : continue;
13539 : :
13540 : 231599 : attr_access::free_lang_data (attrs);
13541 : : }
13542 : 103991 : }
13543 : :
13544 : : /* Perform any final parser cleanups and generate initial debugging
13545 : : information. */
13546 : :
13547 : : void
13548 : 104332 : c_parse_final_cleanups (void)
13549 : : {
13550 : 104332 : tree t;
13551 : 104332 : unsigned i;
13552 : :
13553 : : /* We don't want to do this if generating a PCH. */
13554 : 104332 : if (pch_file)
13555 : 104332 : return;
13556 : :
13557 : 103991 : timevar_stop (TV_PHASE_PARSING);
13558 : 103991 : timevar_start (TV_PHASE_DEFERRED);
13559 : :
13560 : : /* Do the Objective-C stuff. This is where all the Objective-C
13561 : : module stuff gets generated (symtab, class/protocol/selector
13562 : : lists etc). */
13563 : 103991 : if (c_dialect_objc ())
13564 : 0 : objc_write_global_declarations ();
13565 : :
13566 : : /* Close the external scope. */
13567 : 103991 : ext_block = pop_scope ();
13568 : 103991 : external_scope = 0;
13569 : 103991 : gcc_assert (!current_scope);
13570 : :
13571 : : /* Handle -fdump-ada-spec[-slim]. */
13572 : 103991 : if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
13573 : : {
13574 : : /* Build a table of files to generate specs for */
13575 : 15 : collect_source_ref (main_input_filename);
13576 : 15 : if (!flag_dump_ada_spec_slim)
13577 : 15 : collect_source_refs ();
13578 : :
13579 : 15 : dump_ada_specs (collect_all_refs, NULL);
13580 : : }
13581 : :
13582 : : /* Process all file scopes in this compilation, and the external_scope,
13583 : : through wrapup_global_declarations. */
13584 : 207982 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13585 : 103991 : c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
13586 : 103991 : c_write_global_declarations_1 (BLOCK_VARS (ext_block));
13587 : :
13588 : 103991 : if (!in_lto_p)
13589 : 103991 : free_attr_access_data ();
13590 : :
13591 : 103991 : timevar_stop (TV_PHASE_DEFERRED);
13592 : 103991 : timevar_start (TV_PHASE_PARSING);
13593 : :
13594 : 103991 : ext_block = NULL;
13595 : : }
13596 : :
13597 : : /* Register reserved keyword WORD as qualifier for address space AS. */
13598 : :
13599 : : void
13600 : 219710 : c_register_addr_space (const char *word, addr_space_t as)
13601 : : {
13602 : 219710 : int rid = RID_FIRST_ADDR_SPACE + as;
13603 : 219710 : tree id;
13604 : :
13605 : : /* Address space qualifiers are only supported
13606 : : in C with GNU extensions enabled. */
13607 : 219710 : if (c_dialect_objc () || flag_no_asm)
13608 : : return;
13609 : :
13610 : 207484 : id = get_identifier (word);
13611 : 207484 : C_SET_RID_CODE (id, rid);
13612 : 207484 : C_IS_RESERVED_WORD (id) = 1;
13613 : 207484 : ridpointers [rid] = id;
13614 : : }
13615 : :
13616 : : /* Return identifier to look up for omp declare reduction. */
13617 : :
13618 : : tree
13619 : 3250 : c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
13620 : : {
13621 : 3250 : const char *p = NULL;
13622 : 3250 : switch (reduction_code)
13623 : : {
13624 : : case PLUS_EXPR: p = "+"; break;
13625 : 267 : case MULT_EXPR: p = "*"; break;
13626 : 155 : case MINUS_EXPR: p = "-"; break;
13627 : 31 : case BIT_AND_EXPR: p = "&"; break;
13628 : 14 : case BIT_XOR_EXPR: p = "^"; break;
13629 : 92 : case BIT_IOR_EXPR: p = "|"; break;
13630 : 67 : case TRUTH_ANDIF_EXPR: p = "&&"; break;
13631 : 78 : case TRUTH_ORIF_EXPR: p = "||"; break;
13632 : 46 : case MIN_EXPR: p = "min"; break;
13633 : 83 : case MAX_EXPR: p = "max"; break;
13634 : : default:
13635 : : break;
13636 : : }
13637 : :
13638 : 833 : if (p == NULL)
13639 : : {
13640 : 213 : if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
13641 : 0 : return error_mark_node;
13642 : 213 : p = IDENTIFIER_POINTER (reduction_id);
13643 : : }
13644 : :
13645 : 3250 : const char prefix[] = "omp declare reduction ";
13646 : 3250 : size_t lenp = sizeof (prefix);
13647 : 3250 : size_t len = strlen (p);
13648 : 3250 : char *name = XALLOCAVEC (char, lenp + len);
13649 : 3250 : memcpy (name, prefix, lenp - 1);
13650 : 3250 : memcpy (name + lenp - 1, p, len + 1);
13651 : 3250 : return get_identifier (name);
13652 : : }
13653 : :
13654 : : /* Lookup REDUCTION_ID in the current scope, or create an artificial
13655 : : VAR_DECL, bind it into the current scope and return it. */
13656 : :
13657 : : tree
13658 : 161 : c_omp_reduction_decl (tree reduction_id)
13659 : : {
13660 : 161 : struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13661 : 161 : if (b != NULL && B_IN_CURRENT_SCOPE (b))
13662 : 47 : return b->decl;
13663 : :
13664 : 114 : tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
13665 : : reduction_id, integer_type_node);
13666 : 114 : DECL_ARTIFICIAL (decl) = 1;
13667 : 114 : DECL_EXTERNAL (decl) = 1;
13668 : 114 : TREE_STATIC (decl) = 1;
13669 : 114 : TREE_PUBLIC (decl) = 0;
13670 : 114 : bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
13671 : 114 : return decl;
13672 : : }
13673 : :
13674 : : /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
13675 : :
13676 : : tree
13677 : 273 : c_omp_reduction_lookup (tree reduction_id, tree type)
13678 : : {
13679 : 273 : struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13680 : 276 : while (b)
13681 : : {
13682 : 274 : tree t;
13683 : 294 : for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
13684 : 291 : if (comptypes (TREE_PURPOSE (t), type))
13685 : 271 : return TREE_VALUE (t);
13686 : 3 : b = b->shadowed;
13687 : : }
13688 : 2 : return error_mark_node;
13689 : : }
13690 : :
13691 : : /* Helper function called via walk_tree, to diagnose invalid
13692 : : #pragma omp declare reduction combiners or initializers. */
13693 : :
13694 : : tree
13695 : 1401 : c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
13696 : : {
13697 : 1401 : tree *vars = (tree *) data;
13698 : 1401 : if (SSA_VAR_P (*tp)
13699 : 413 : && !DECL_ARTIFICIAL (*tp)
13700 : 24 : && *tp != vars[0]
13701 : 24 : && *tp != vars[1])
13702 : : {
13703 : 24 : location_t loc = DECL_SOURCE_LOCATION (vars[0]);
13704 : 24 : if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
13705 : 10 : error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
13706 : : "variable %qD which is not %<omp_out%> nor %<omp_in%>",
13707 : : *tp);
13708 : : else
13709 : 14 : error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
13710 : : "to variable %qD which is not %<omp_priv%> nor "
13711 : : "%<omp_orig%>",
13712 : : *tp);
13713 : 24 : return *tp;
13714 : : }
13715 : : return NULL_TREE;
13716 : : }
13717 : :
13718 : : /* Return identifier to look up for omp declare mapper. */
13719 : :
13720 : : tree
13721 : 607 : c_omp_mapper_id (tree mapper_id)
13722 : : {
13723 : 607 : const char *p = NULL;
13724 : :
13725 : 607 : const char prefix[] = "omp declare mapper ";
13726 : :
13727 : 607 : if (mapper_id == NULL_TREE)
13728 : : p = "<default>";
13729 : 19 : else if (TREE_CODE (mapper_id) == IDENTIFIER_NODE)
13730 : 19 : p = IDENTIFIER_POINTER (mapper_id);
13731 : : else
13732 : 0 : return error_mark_node;
13733 : :
13734 : 607 : size_t lenp = sizeof (prefix);
13735 : 607 : size_t len = strlen (p);
13736 : 607 : char *name = XALLOCAVEC (char, lenp + len);
13737 : 607 : memcpy (name, prefix, lenp - 1);
13738 : 607 : memcpy (name + lenp - 1, p, len + 1);
13739 : 607 : return get_identifier (name);
13740 : : }
13741 : :
13742 : : /* Lookup MAPPER_ID in the current scope, or create an artificial
13743 : : VAR_DECL, bind it into the current scope and return it. */
13744 : :
13745 : : tree
13746 : 62 : c_omp_mapper_decl (tree mapper_id)
13747 : : {
13748 : 62 : struct c_binding *b = I_SYMBOL_BINDING (mapper_id);
13749 : 62 : if (b != NULL && B_IN_CURRENT_SCOPE (b))
13750 : 22 : return b->decl;
13751 : :
13752 : 40 : tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
13753 : : mapper_id, integer_type_node);
13754 : 40 : DECL_ARTIFICIAL (decl) = 1;
13755 : 40 : DECL_EXTERNAL (decl) = 1;
13756 : 40 : TREE_STATIC (decl) = 1;
13757 : 40 : TREE_PUBLIC (decl) = 0;
13758 : 40 : bind (mapper_id, decl, current_scope, true, false, BUILTINS_LOCATION);
13759 : 40 : return decl;
13760 : : }
13761 : :
13762 : : /* Lookup MAPPER_ID in the first scope where it has entry for TYPE. */
13763 : :
13764 : : tree
13765 : 1696 : c_omp_mapper_lookup (tree mapper_id, tree type)
13766 : : {
13767 : 1696 : if (!RECORD_OR_UNION_TYPE_P (type))
13768 : : return NULL_TREE;
13769 : :
13770 : 545 : mapper_id = c_omp_mapper_id (mapper_id);
13771 : :
13772 : 545 : struct c_binding *b = I_SYMBOL_BINDING (mapper_id);
13773 : 551 : while (b)
13774 : : {
13775 : 91 : tree t;
13776 : 110 : for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
13777 : 104 : if (comptypes (TREE_PURPOSE (t), type))
13778 : 85 : return TREE_VALUE (t);
13779 : 6 : b = b->shadowed;
13780 : : }
13781 : : return NULL_TREE;
13782 : : }
13783 : :
13784 : : /* For C, we record a pointer to the mapper itself without wrapping it in an
13785 : : artificial function or similar. So, just return it. */
13786 : :
13787 : : tree
13788 : 82 : c_omp_extract_mapper_directive (tree mapper)
13789 : : {
13790 : 82 : return mapper;
13791 : : }
13792 : :
13793 : : /* For now we can handle singleton OMP_ARRAY_SECTIONs with custom mappers, but
13794 : : nothing more complicated. */
13795 : :
13796 : : tree
13797 : 364 : c_omp_map_array_section (location_t loc, tree t)
13798 : : {
13799 : 364 : tree low = TREE_OPERAND (t, 1);
13800 : 364 : tree len = TREE_OPERAND (t, 2);
13801 : :
13802 : 364 : if (len && integer_onep (len))
13803 : : {
13804 : 63 : t = TREE_OPERAND (t, 0);
13805 : :
13806 : 63 : if (!low)
13807 : 0 : low = integer_zero_node;
13808 : :
13809 : 63 : t = build_array_ref (loc, t, low);
13810 : : }
13811 : :
13812 : 364 : return t;
13813 : : }
13814 : :
13815 : : /* Helper function for below function. */
13816 : :
13817 : : static tree
13818 : 50786 : c_omp_scan_mapper_bindings_r (tree *tp, int *walk_subtrees, void *ptr)
13819 : : {
13820 : 50786 : tree t = *tp;
13821 : 50786 : omp_mapper_list<tree> *mlist = (omp_mapper_list<tree> *) ptr;
13822 : 50786 : tree aggr_type = NULL_TREE;
13823 : :
13824 : 50786 : if (TREE_CODE (t) == SIZEOF_EXPR
13825 : 50786 : || TREE_CODE (t) == ALIGNOF_EXPR)
13826 : : {
13827 : 0 : *walk_subtrees = 0;
13828 : 0 : return NULL_TREE;
13829 : : }
13830 : :
13831 : 50786 : if (TREE_CODE (t) == OMP_CLAUSE)
13832 : : return NULL_TREE;
13833 : :
13834 : 47502 : if (TREE_CODE (t) == COMPONENT_REF
13835 : 47502 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
13836 : 356 : aggr_type = TREE_TYPE (TREE_OPERAND (t, 0));
13837 : 47146 : else if ((TREE_CODE (t) == VAR_DECL
13838 : : || TREE_CODE (t) == PARM_DECL
13839 : : || TREE_CODE (t) == RESULT_DECL)
13840 : 5254 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t)))
13841 : 116 : aggr_type = TREE_TYPE (t);
13842 : :
13843 : 472 : if (aggr_type)
13844 : : {
13845 : 472 : tree mapper_fn = c_omp_mapper_lookup (NULL_TREE, aggr_type);
13846 : 472 : if (mapper_fn)
13847 : 65 : mlist->add_mapper (NULL_TREE, aggr_type, mapper_fn);
13848 : : }
13849 : :
13850 : : return NULL_TREE;
13851 : : }
13852 : :
13853 : : /* Scan an offload region's body, and record uses of struct- or union-typed
13854 : : variables. Add _mapper_binding_ fake clauses to *CLAUSES_PTR. */
13855 : :
13856 : : void
13857 : 1890 : c_omp_scan_mapper_bindings (location_t loc, tree *clauses_ptr, tree body)
13858 : : {
13859 : 1890 : hash_set<omp_name_type<tree>> seen_types;
13860 : 1890 : auto_vec<tree> mappers;
13861 : 1890 : omp_mapper_list<tree> mlist (&seen_types, &mappers);
13862 : :
13863 : 1890 : walk_tree_without_duplicates (&body, c_omp_scan_mapper_bindings_r, &mlist);
13864 : :
13865 : 1890 : unsigned int i;
13866 : 1890 : tree mapper;
13867 : 3819 : FOR_EACH_VEC_ELT (mappers, i, mapper)
13868 : 39 : c_omp_find_nested_mappers (&mlist, mapper);
13869 : :
13870 : 1955 : FOR_EACH_VEC_ELT (mappers, i, mapper)
13871 : : {
13872 : 39 : if (mapper == error_mark_node)
13873 : 0 : continue;
13874 : 39 : tree mapper_name = OMP_DECLARE_MAPPER_ID (mapper);
13875 : 39 : tree decl = OMP_DECLARE_MAPPER_DECL (mapper);
13876 : :
13877 : 39 : tree c = build_omp_clause (loc, OMP_CLAUSE__MAPPER_BINDING_);
13878 : 39 : OMP_CLAUSE__MAPPER_BINDING__ID (c) = mapper_name;
13879 : 39 : OMP_CLAUSE__MAPPER_BINDING__DECL (c) = decl;
13880 : 39 : OMP_CLAUSE__MAPPER_BINDING__MAPPER (c) = mapper;
13881 : :
13882 : 39 : OMP_CLAUSE_CHAIN (c) = *clauses_ptr;
13883 : 39 : *clauses_ptr = c;
13884 : : }
13885 : 1890 : }
13886 : :
13887 : : bool
13888 : 223 : c_check_in_current_scope (tree decl)
13889 : : {
13890 : 223 : struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
13891 : 223 : return b != NULL && B_IN_CURRENT_SCOPE (b);
13892 : : }
13893 : :
13894 : : /* Search for loop or switch names. BEFORE_LABELS is last statement before
13895 : : possible labels and SWITCH_P true for a switch, false for loops.
13896 : : Searches through last statements in cur_stmt_list, stops when seeing
13897 : : BEFORE_LABELs, or statement other than LABEL_EXPR or CASE_LABEL_EXPR.
13898 : : Returns number of loop/switch names found and if any are found, sets
13899 : : *LAST_P to the canonical loop/switch name LABEL_DECL. */
13900 : :
13901 : : int
13902 : 498835 : c_get_loop_names (tree before_labels, bool switch_p, tree *last_p)
13903 : : {
13904 : 498835 : *last_p = NULL_TREE;
13905 : 997670 : if (!building_stmt_list_p ()
13906 : 498835 : || !STATEMENT_LIST_HAS_LABEL (cur_stmt_list)
13907 : 519341 : || before_labels == void_list_node)
13908 : 478329 : return 0;
13909 : :
13910 : 20506 : int ret = 0;
13911 : 20506 : tree last = NULL_TREE;
13912 : 20506 : for (tree_stmt_iterator tsi = tsi_last (cur_stmt_list);
13913 : 28615 : !tsi_end_p (tsi); tsi_prev (&tsi))
13914 : : {
13915 : 27769 : tree stmt = tsi_stmt (tsi);
13916 : 27769 : if (stmt == before_labels)
13917 : : break;
13918 : 8109 : else if (TREE_CODE (stmt) == LABEL_EXPR)
13919 : : {
13920 : 1093 : if (last == NULL_TREE)
13921 : 899 : last = LABEL_EXPR_LABEL (stmt);
13922 : : else
13923 : : {
13924 : 194 : loop_names.safe_push (LABEL_EXPR_LABEL (stmt));
13925 : 194 : ++ret;
13926 : : }
13927 : : }
13928 : 7016 : else if (TREE_CODE (stmt) != CASE_LABEL_EXPR
13929 : 4712 : && TREE_CODE (stmt) != DEBUG_BEGIN_STMT)
13930 : : break;
13931 : : }
13932 : 20506 : if (last)
13933 : : {
13934 : 899 : if (switch_p)
13935 : 129 : C_DECL_SWITCH_NAME (last) = 1;
13936 : : else
13937 : 770 : C_DECL_LOOP_NAME (last) = 1;
13938 : 899 : loop_names.safe_push (last);
13939 : 899 : ++ret;
13940 : 899 : if (loop_names.length () > 16)
13941 : : {
13942 : 20 : unsigned int first = 0, i;
13943 : 20 : tree l, c = NULL_TREE;
13944 : 20 : if (loop_names_hash == NULL)
13945 : 8 : loop_names_hash = new decl_tree_map (ret);
13946 : : else
13947 : 12 : first = loop_names.length () - ret;
13948 : 210 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
13949 : : {
13950 : 170 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
13951 : 48 : c = l;
13952 : 170 : gcc_checking_assert (c);
13953 : 170 : loop_names_hash->put (l, c);
13954 : 170 : if (i == first)
13955 : : break;
13956 : : }
13957 : : }
13958 : 899 : *last_p = last;
13959 : : }
13960 : : return ret;
13961 : : }
13962 : :
13963 : : /* Undoes what get_loop_names did when it returned NUM_NAMES. */
13964 : :
13965 : : void
13966 : 899 : c_release_loop_names (int num_names)
13967 : : {
13968 : 899 : unsigned len = loop_names.length () - num_names;
13969 : 899 : if (loop_names_hash)
13970 : : {
13971 : 20 : if (len <= 16)
13972 : : {
13973 : 8 : delete loop_names_hash;
13974 : 8 : loop_names_hash = NULL;
13975 : : }
13976 : : else
13977 : : {
13978 : 12 : unsigned int i;
13979 : 12 : tree l;
13980 : 42 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
13981 : : {
13982 : 30 : loop_names_hash->remove (l);
13983 : 30 : if (i == len)
13984 : : break;
13985 : : }
13986 : : }
13987 : : }
13988 : 899 : loop_names.truncate (len);
13989 : 899 : }
13990 : :
13991 : : /* Finish processing of break or continue identifier operand.
13992 : : NAME is the identifier operand of break or continue and
13993 : : IS_BREAK is true iff it is break stmt. Returns the operand
13994 : : to use for BREAK_STMT or CONTINUE_STMT, either NULL_TREE or
13995 : : canonical loop/switch name LABEL_DECL. */
13996 : :
13997 : : tree
13998 : 316 : c_finish_bc_name (location_t loc, tree name, bool is_break)
13999 : : {
14000 : 316 : tree label = NULL_TREE, lab;
14001 : 457 : pedwarn_c23 (loc, OPT_Wpedantic,
14002 : : "ISO C does not support %qs statement with an identifier "
14003 : : "operand before C2Y", is_break ? "break" : "continue");
14004 : :
14005 : : /* If I_LABEL_DECL is NULL or not from current function, don't waste time
14006 : : trying to find it among loop_names, it can't be there. */
14007 : 316 : if (!loop_names.is_empty ()
14008 : 296 : && current_function_scope
14009 : 296 : && (lab = I_LABEL_DECL (name))
14010 : 290 : && DECL_CONTEXT (lab) == current_function_decl)
14011 : : {
14012 : 280 : unsigned int i;
14013 : 280 : tree l, c = NULL_TREE;
14014 : 280 : if (loop_names_hash)
14015 : : {
14016 : 86 : if (tree *val = loop_names_hash->get (lab))
14017 : 86 : label = *val;
14018 : : }
14019 : : else
14020 : 565 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14021 : : {
14022 : 363 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14023 : : c = l;
14024 : 363 : gcc_checking_assert (c);
14025 : 363 : if (l == lab)
14026 : : {
14027 : : label = c;
14028 : : break;
14029 : : }
14030 : : }
14031 : 280 : if (label)
14032 : 272 : TREE_USED (lab) = 1;
14033 : : }
14034 : 272 : if (label == NULL_TREE)
14035 : : {
14036 : 44 : auto_vec<const char *> candidates;
14037 : 44 : unsigned int i;
14038 : 44 : tree l, c = NULL_TREE;
14039 : 115 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14040 : : {
14041 : 28 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14042 : : c = l;
14043 : 28 : gcc_checking_assert (c);
14044 : 42 : if (is_break || C_DECL_LOOP_NAME (c))
14045 : 25 : candidates.safe_push (IDENTIFIER_POINTER (DECL_NAME (l)));
14046 : : }
14047 : 44 : const char *hint = find_closest_string (IDENTIFIER_POINTER (name),
14048 : : &candidates);
14049 : 44 : if (hint)
14050 : : {
14051 : 22 : gcc_rich_location richloc (loc);
14052 : 22 : richloc.add_fixit_replace (hint);
14053 : 22 : if (is_break)
14054 : 11 : error_at (&richloc, "%<break%> statement operand %qE does not "
14055 : : "refer to a named loop or %<switch%>; "
14056 : : "did you mean %qs?", name, hint);
14057 : : else
14058 : 11 : error_at (&richloc, "%<continue%> statement operand %qE does not "
14059 : : "refer to a named loop; did you mean %qs?",
14060 : : name, hint);
14061 : 22 : }
14062 : 22 : else if (is_break)
14063 : 12 : error_at (loc, "%<break%> statement operand %qE does not refer to a "
14064 : : "named loop or %<switch%>", name);
14065 : : else
14066 : 10 : error_at (loc, "%<continue%> statement operand %qE does not refer to "
14067 : : "a named loop", name);
14068 : 44 : }
14069 : 272 : else if (!C_DECL_LOOP_NAME (label) && !is_break)
14070 : : {
14071 : 2 : auto_diagnostic_group d;
14072 : 2 : error_at (loc, "%<continue%> statement operand %qE refers to a named "
14073 : : "%<switch%>", name);
14074 : 2 : inform (DECL_SOURCE_LOCATION (label), "%<switch%> name defined here");
14075 : 2 : label = NULL_TREE;
14076 : 2 : }
14077 : 270 : else if (!C_DECL_LOOP_SWITCH_NAME_VALID (label))
14078 : : {
14079 : 18 : auto_diagnostic_group d;
14080 : 18 : if (C_DECL_LOOP_NAME (label))
14081 : : {
14082 : 16 : error_at (loc, "%qs statement operand %qE refers to a loop outside "
14083 : : "of its body", is_break ? "break" : "continue", name);
14084 : 16 : inform (DECL_SOURCE_LOCATION (label), "loop name defined here");
14085 : : }
14086 : : else
14087 : : {
14088 : 2 : error_at (loc, "%<break%> statement operand %qE refers to a "
14089 : : "%<switch%> outside of its body", name);
14090 : 2 : inform (DECL_SOURCE_LOCATION (label),
14091 : : "%<switch%> name defined here");
14092 : : }
14093 : 18 : label = NULL_TREE;
14094 : 18 : }
14095 : 252 : else if (label == loop_names.last () && (in_statement & IN_NAMED_STMT) != 0)
14096 : : /* If it is just a fancy reference to the innermost construct, handle it
14097 : : just like break; or continue; though tracking cheaply what is the
14098 : : innermost loop for continue when nested in switches would require
14099 : : another global variable and updating it. */
14100 : : label = NULL_TREE;
14101 : : else
14102 : 98 : C_DECL_LOOP_SWITCH_NAME_USED (label) = 1;
14103 : 316 : return label;
14104 : : }
14105 : :
14106 : : #include "gt-c-c-decl.h"
|