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 : 4508724708 : i_symbol_binding (tree node)
256 : : {
257 : 4508724708 : struct lang_identifier *lid
258 : 4508724708 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
259 : :
260 : 4508724708 : if (lid->symbol_binding == NULL
261 : 1656371867 : && c_binding_oracle != NULL
262 : 4508724708 : && !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 : 4508724708 : 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 : 4442380 : i_tag_binding (tree node)
285 : : {
286 : 4442380 : struct lang_identifier *lid
287 : 4442380 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
288 : :
289 : 4442380 : if (lid->tag_binding == NULL
290 : 1229886 : && c_binding_oracle != NULL
291 : 4442380 : && !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 : 4442380 : 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 : 357147 : i_label_binding (tree node)
314 : : {
315 : 357147 : struct lang_identifier *lid
316 : 357147 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
317 : :
318 : 357147 : if (lid->label_binding == NULL
319 : 47222 : && c_binding_oracle != NULL
320 : 357147 : && !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 : 357147 : 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 : 1153934 : 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 : 4166652 : c_struct_hasher::hash (tree type)
658 : : {
659 : 4166652 : inchash::hash hstate;
660 : :
661 : 4166652 : hstate.add_int (TREE_CODE (type));
662 : 4166652 : tree tag = c_type_tag (type);
663 : 4166652 : hstate.add_object (tag);
664 : :
665 : 4166652 : return hstate.end ();
666 : : }
667 : :
668 : : /* Compare two RECORD or UNION types. */
669 : : bool
670 : 14234676 : c_struct_hasher::equal (tree t1, tree t2)
671 : : {
672 : 14234676 : 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 : 91504993 : add_stmt (tree t)
699 : : {
700 : 91504993 : enum tree_code code = TREE_CODE (t);
701 : :
702 : 91504993 : if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
703 : : {
704 : 90807120 : if (!EXPR_HAS_LOCATION (t))
705 : 154653 : 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 : 91504993 : if (!building_stmt_list_p ())
711 : 1 : push_stmt_list ();
712 : :
713 : 91504993 : if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
714 : 1073743 : STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
715 : :
716 : 91504993 : append_to_statement_list_force (t, &cur_stmt_list);
717 : :
718 : 91504993 : 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 : 893099085 : decl_jump_unsafe (tree decl)
730 : : {
731 : 893099085 : 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 : 893096891 : if (VAR_P (decl) && C_DECL_COMPOUND_LITERAL_P (decl))
740 : : return false;
741 : :
742 : 892198901 : if (flag_openmp
743 : 11896303 : && VAR_P (decl)
744 : 892229930 : && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl)))
745 : : return true;
746 : :
747 : : /* Always warn about crossing variably modified types. */
748 : 882337399 : if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
749 : 904166255 : && 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 : 892158544 : if (warn_jump_misses_init
755 : 5933816 : && VAR_P (decl)
756 : 10189 : && !TREE_STATIC (decl)
757 : 892163983 : && 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 : 892949985 : bind (tree name, tree decl, struct c_scope *scope, bool invisible,
804 : : bool nested, location_t locus)
805 : : {
806 : 892949985 : struct c_binding *b, **here;
807 : :
808 : 892949985 : if (binding_freelist)
809 : : {
810 : 224824605 : b = binding_freelist;
811 : 224824605 : binding_freelist = b->prev;
812 : : }
813 : : else
814 : 668125380 : b = ggc_alloc<c_binding> ();
815 : :
816 : 892949985 : b->shadowed = 0;
817 : 892949985 : b->decl = decl;
818 : 892949985 : b->id = name;
819 : 892949985 : b->depth = scope->depth;
820 : 892949985 : b->invisible = invisible;
821 : 892949985 : b->nested = nested;
822 : 892949985 : b->inner_comp = 0;
823 : 892949985 : b->in_struct = 0;
824 : 892949985 : b->locus = locus;
825 : :
826 : 892949985 : b->u.type = NULL;
827 : :
828 : 892949985 : b->prev = scope->bindings;
829 : 892949985 : scope->bindings = b;
830 : :
831 : 892949985 : if (decl_jump_unsafe (decl))
832 : 24065 : scope->has_jump_unsafe_decl = 1;
833 : :
834 : 892949985 : if (!name)
835 : : return;
836 : :
837 : 884484483 : switch (TREE_CODE (decl))
838 : : {
839 : 23636 : case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
840 : 614235 : case ENUMERAL_TYPE:
841 : 614235 : case UNION_TYPE:
842 : 614235 : case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
843 : 883846612 : case VAR_DECL:
844 : 883846612 : case FUNCTION_DECL:
845 : 883846612 : case TYPE_DECL:
846 : 883846612 : case CONST_DECL:
847 : 883846612 : case PARM_DECL:
848 : 883846612 : 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 : 884484523 : while (*here && (*here)->depth > scope->depth)
858 : 40 : here = &(*here)->shadowed;
859 : :
860 : 884484483 : b->shadowed = *here;
861 : 884484483 : *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 : 877113304 : free_binding_and_advance (struct c_binding *b)
870 : : {
871 : 877113304 : struct c_binding *prev = b->prev;
872 : :
873 : 877113304 : memset (b, 0, sizeof (struct c_binding));
874 : 877113304 : b->prev = binding_freelist;
875 : 877113304 : binding_freelist = b;
876 : :
877 : 877113304 : 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 : 23636 : bind_label (tree name, tree label, struct c_scope *scope,
884 : : struct c_label_vars *label_vars)
885 : : {
886 : 23636 : struct c_binding *b;
887 : :
888 : 23636 : bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
889 : : UNKNOWN_LOCATION);
890 : :
891 : 23636 : scope->has_label_bindings = true;
892 : :
893 : 23636 : b = scope->bindings;
894 : 23636 : gcc_assert (b->decl == label);
895 : 23636 : label_vars->shadowed = b->u.label;
896 : 23636 : b->u.label = label_vars;
897 : 23636 : }
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 : 7433 : c_finish_incomplete_decl (tree decl)
904 : : {
905 : 7433 : if (VAR_P (decl))
906 : : {
907 : 7433 : tree type = TREE_TYPE (decl);
908 : 7433 : if (type != error_mark_node
909 : 7359 : && TREE_CODE (type) == ARRAY_TYPE
910 : 7197 : && !DECL_EXTERNAL (decl)
911 : 7511 : && 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 : 7433 : }
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 : 107067 : check_inline_statics (void)
946 : : {
947 : 107067 : struct c_inline_static *csi;
948 : 107103 : 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 : 107067 : c_inline_statics = NULL;
968 : 107067 : }
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 : 133461 : set_spot_bindings (struct c_spot_bindings *p, bool defining)
975 : : {
976 : 0 : if (defining)
977 : : {
978 : 126495 : p->scope = current_scope;
979 : 16670 : p->bindings_in_scope = current_scope->bindings;
980 : : }
981 : : else
982 : : {
983 : 6966 : p->scope = NULL;
984 : 6966 : p->bindings_in_scope = NULL;
985 : : }
986 : 133461 : p->stmt_exprs = 0;
987 : 133461 : 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 : 470604379 : 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 : 204558 : p->scope = scope->outer;
1007 : 204558 : p->bindings_in_scope = p->scope->bindings;
1008 : :
1009 : 151094 : 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 : 2066110 : global_bindings_p (void)
1046 : : {
1047 : 2066110 : 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 : 589489 : float_const_decimal64_p (void)
1087 : : {
1088 : 589489 : return current_scope->float_const_decimal64;
1089 : : }
1090 : :
1091 : : /* Identify this scope as currently being filled with parameters. */
1092 : :
1093 : : void
1094 : 84812192 : declare_parm_level (void)
1095 : : {
1096 : 84812192 : current_scope->parm_flag = true;
1097 : 84812192 : }
1098 : :
1099 : : void
1100 : 125068281 : push_scope (void)
1101 : : {
1102 : 125068281 : 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 : 35382602 : current_scope->parm_flag = false;
1115 : 35382602 : current_scope->function_body = true;
1116 : 35382602 : current_scope->keep = true;
1117 : 35382602 : current_scope->outer_function = current_function_scope;
1118 : 35382602 : current_function_scope = current_scope;
1119 : :
1120 : 35382602 : keep_next_level_flag = false;
1121 : 35382602 : next_is_function_body = false;
1122 : :
1123 : : /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1124 : 35382602 : if (current_scope->outer)
1125 : 35382602 : current_scope->float_const_decimal64
1126 : 35382602 : = current_scope->outer->float_const_decimal64;
1127 : : else
1128 : 0 : current_scope->float_const_decimal64 = false;
1129 : : }
1130 : : else
1131 : : {
1132 : 89685679 : struct c_scope *scope;
1133 : 89685679 : if (scope_freelist)
1134 : : {
1135 : 88844892 : scope = scope_freelist;
1136 : 88844892 : scope_freelist = scope->outer;
1137 : : }
1138 : : else
1139 : 840787 : scope = ggc_cleared_alloc<c_scope> ();
1140 : :
1141 : : /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1142 : 89685679 : if (current_scope)
1143 : 89573618 : scope->float_const_decimal64 = current_scope->float_const_decimal64;
1144 : : else
1145 : 112061 : scope->float_const_decimal64 = false;
1146 : :
1147 : 89685679 : scope->keep = keep_next_level_flag;
1148 : 89685679 : scope->outer = current_scope;
1149 : 89685679 : 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 : 89685679 : 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 : 89685679 : current_scope = scope;
1160 : 89685679 : keep_next_level_flag = false;
1161 : : }
1162 : 125068281 : }
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 : 89679802 : update_label_decls (struct c_scope *scope)
1171 : : {
1172 : 89679802 : struct c_scope *s;
1173 : :
1174 : 89679802 : s = scope;
1175 : 325310629 : while (s != NULL)
1176 : : {
1177 : 275682886 : if (s->has_label_bindings)
1178 : : {
1179 : 230817 : struct c_binding *b;
1180 : :
1181 : 2131346 : for (b = s->bindings; b != NULL; b = b->prev)
1182 : : {
1183 : 1900529 : struct c_label_vars *label_vars;
1184 : 1900529 : struct c_binding *b1;
1185 : 1900529 : bool hjud;
1186 : 1900529 : unsigned int ix;
1187 : 1900529 : struct c_goto_bindings *g;
1188 : :
1189 : 1900529 : if (TREE_CODE (b->decl) != LABEL_DECL)
1190 : 1399449 : continue;
1191 : 501080 : label_vars = b->u.label;
1192 : :
1193 : 501080 : b1 = label_vars->label_bindings.bindings_in_scope;
1194 : 501080 : if (label_vars->label_bindings.scope == NULL)
1195 : : hjud = false;
1196 : : else
1197 : 192441 : hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1198 : 501080 : if (update_spot_bindings (scope, &label_vars->label_bindings))
1199 : : {
1200 : : /* This label is defined in this scope. */
1201 : 53464 : 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 : 472296193 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1218 : 470254393 : update_spot_bindings (scope, &g->goto_bindings);
1219 : : }
1220 : : }
1221 : :
1222 : : /* Don't search beyond the current function. */
1223 : 275682886 : if (s == current_function_scope)
1224 : : break;
1225 : :
1226 : 235630827 : s = s->outer;
1227 : : }
1228 : 89679802 : }
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 : 89679802 : pop_scope (void)
1237 : : {
1238 : 89679802 : struct c_scope *scope = current_scope;
1239 : 89679802 : tree block, context, p;
1240 : 89679802 : struct c_binding *b;
1241 : :
1242 : 89679802 : bool functionbody = scope->function_body;
1243 : 89679802 : bool keep = functionbody || scope->keep || scope->bindings;
1244 : :
1245 : 89679802 : update_label_decls (scope);
1246 : :
1247 : : /* If appropriate, create a BLOCK to record the decls for the life
1248 : : of this function. */
1249 : 89679802 : block = NULL_TREE;
1250 : 89679802 : if (keep)
1251 : : {
1252 : 35871238 : block = make_node (BLOCK);
1253 : 35871238 : BLOCK_SUBBLOCKS (block) = scope->blocks;
1254 : 35871238 : TREE_USED (block) = 1;
1255 : :
1256 : : /* In each subblock, record that this is its superior. */
1257 : 36146467 : for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1258 : 275229 : BLOCK_SUPERCONTEXT (p) = block;
1259 : :
1260 : 35871238 : 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 : 89679802 : if (scope->function_body)
1277 : 35382601 : context = current_function_decl;
1278 : 54297201 : else if (scope == file_scope)
1279 : : {
1280 : 106705 : tree file_decl
1281 : 106705 : = build_translation_unit_decl (get_identifier (main_input_filename));
1282 : 106705 : context = file_decl;
1283 : 106705 : debug_hooks->register_main_translation_unit (file_decl);
1284 : : }
1285 : : else
1286 : : context = block;
1287 : :
1288 : : /* Clear all bindings in this scope. */
1289 : 846595076 : for (b = scope->bindings; b; b = free_binding_and_advance (b))
1290 : : {
1291 : 756915274 : p = b->decl;
1292 : 756915274 : switch (TREE_CODE (p))
1293 : : {
1294 : 23636 : case LABEL_DECL:
1295 : : /* Warnings for unused labels, errors for undefined labels. */
1296 : 23636 : 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 : 23573 : warn_for_unused_label (p);
1303 : :
1304 : : /* Labels go in BLOCK_VARS. */
1305 : 23636 : DECL_CHAIN (p) = BLOCK_VARS (block);
1306 : 23636 : BLOCK_VARS (block) = p;
1307 : 23636 : gcc_assert (I_LABEL_BINDING (b->id) == b);
1308 : 23636 : I_LABEL_BINDING (b->id) = b->shadowed;
1309 : :
1310 : : /* Also pop back to the shadowed label_vars. */
1311 : 23636 : release_tree_vector (b->u.label->decls_in_scope);
1312 : 23636 : b->u.label = b->u.label->shadowed;
1313 : 23636 : break;
1314 : :
1315 : 1393269 : case ENUMERAL_TYPE:
1316 : 1393269 : case UNION_TYPE:
1317 : 1393269 : 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 : 1393269 : if (b->id)
1322 : : {
1323 : 612414 : gcc_assert (I_TAG_BINDING (b->id) == b);
1324 : 612414 : I_TAG_BINDING (b->id) = b->shadowed;
1325 : : }
1326 : : break;
1327 : :
1328 : 630066236 : case FUNCTION_DECL:
1329 : : /* Propagate TREE_ADDRESSABLE from nested functions to their
1330 : : containing functions. */
1331 : 630066236 : if (!TREE_ASM_WRITTEN (p)
1332 : 630066236 : && DECL_INITIAL (p) != NULL_TREE
1333 : 70419416 : && TREE_ADDRESSABLE (p)
1334 : 3608306 : && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1335 : 630066236 : && DECL_ABSTRACT_ORIGIN (p) != p)
1336 : 0 : TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1337 : 630066236 : if (!TREE_PUBLIC (p)
1338 : 345108 : && !DECL_INITIAL (p)
1339 : 131 : && !b->nested
1340 : 100 : && scope != file_scope
1341 : 630066242 : && scope != external_scope)
1342 : : {
1343 : 6 : error ("nested function %q+D declared but never defined", p);
1344 : 6 : undef_nested_function = true;
1345 : : }
1346 : 630066230 : else if (DECL_DECLARED_INLINE_P (p)
1347 : 69251171 : && TREE_PUBLIC (p)
1348 : 699161662 : && !DECL_INITIAL (p))
1349 : : {
1350 : : /* C99 6.7.4p6: "a function with external linkage... declared
1351 : : with an inline function specifier ... shall also be defined
1352 : : in the same translation unit." */
1353 : 72 : if (!flag_gnu89_inline
1354 : 47 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1355 : 103 : && scope == external_scope)
1356 : 11 : pedwarn (input_location, 0,
1357 : : "inline function %q+D declared but never defined", p);
1358 : 72 : DECL_EXTERNAL (p) = 1;
1359 : : }
1360 : :
1361 : 630066236 : goto common_symbol;
1362 : :
1363 : 10751301 : case VAR_DECL:
1364 : : /* Warnings for unused variables. */
1365 : 8189388 : if ((!TREE_USED (p) || !DECL_READ_P (p))
1366 : 2590207 : && !warning_suppressed_p (p, OPT_Wunused_but_set_variable_)
1367 : 2589703 : && !DECL_IN_SYSTEM_HEADER (p)
1368 : 2511750 : && DECL_NAME (p)
1369 : 2511750 : && !DECL_ARTIFICIAL (p)
1370 : 2511385 : && scope != file_scope
1371 : 12375051 : && scope != external_scope)
1372 : : {
1373 : 737720 : if (!TREE_USED (p))
1374 : : {
1375 : 732422 : warning (OPT_Wunused_variable, "unused variable %q+D", p);
1376 : 732422 : suppress_warning (p, OPT_Wunused_variable);
1377 : : }
1378 : 5298 : else if (DECL_CONTEXT (p) == current_function_decl)
1379 : 5279 : warning_at (DECL_SOURCE_LOCATION (p),
1380 : 5279 : OPT_Wunused_but_set_variable_,
1381 : : "variable %qD set but not used", p);
1382 : : }
1383 : :
1384 : 10751301 : 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 : 657985252 : case TYPE_DECL:
1392 : 657985252 : case CONST_DECL:
1393 : 10751299 : common_symbol:
1394 : : /* All of these go in BLOCK_VARS, but only if this is the
1395 : : binding in the home scope. */
1396 : 657985252 : if (!b->nested)
1397 : : {
1398 : 396119619 : DECL_CHAIN (p) = BLOCK_VARS (block);
1399 : 396119619 : BLOCK_VARS (block) = p;
1400 : : }
1401 : 261865633 : 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 : 14571 : tree extp = copy_node (p);
1406 : :
1407 : 14571 : DECL_EXTERNAL (extp) = 1;
1408 : 14571 : TREE_STATIC (extp) = 0;
1409 : 14571 : TREE_PUBLIC (extp) = 1;
1410 : 14571 : DECL_INITIAL (extp) = NULL_TREE;
1411 : 14571 : DECL_LANG_SPECIFIC (extp) = NULL;
1412 : 14571 : DECL_CONTEXT (extp) = current_function_decl;
1413 : 14571 : if (TREE_CODE (p) == FUNCTION_DECL)
1414 : : {
1415 : 13036 : DECL_RESULT (extp) = NULL_TREE;
1416 : 13036 : DECL_SAVED_TREE (extp) = NULL_TREE;
1417 : 13036 : DECL_STRUCT_FUNCTION (extp) = NULL;
1418 : : }
1419 : 14571 : if (b->locus != UNKNOWN_LOCATION)
1420 : 14571 : DECL_SOURCE_LOCATION (extp) = b->locus;
1421 : 14571 : DECL_CHAIN (extp) = BLOCK_VARS (block);
1422 : 14571 : 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 : 657985252 : if (scope == file_scope)
1428 : 272934963 : DECL_CONTEXT (p) = context;
1429 : :
1430 : 755498369 : 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 : 755498369 : case PARM_DECL:
1437 : 755498369 : 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 : 755498369 : if (b->id)
1441 : : {
1442 : 752346694 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1443 : 752346694 : I_SYMBOL_BINDING (b->id) = b->shadowed;
1444 : 752346694 : if (b->shadowed && b->shadowed->u.type)
1445 : 4588059 : 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 : 89679802 : if ((scope->function_body || scope == file_scope) && context)
1457 : : {
1458 : 35489304 : DECL_INITIAL (context) = block;
1459 : 35489304 : BLOCK_SUPERCONTEXT (block) = context;
1460 : : }
1461 : 54190498 : else if (scope->outer)
1462 : : {
1463 : 54083793 : if (block)
1464 : 275229 : 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 : 53808564 : else if (scope->blocks)
1469 : 351841 : SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1470 : : }
1471 : :
1472 : : /* Pop the current scope, and free the structure for reuse. */
1473 : 89679802 : current_scope = scope->outer;
1474 : 89679802 : if (scope->function_body)
1475 : 35382601 : current_function_scope = scope->outer_function;
1476 : :
1477 : 89679802 : memset (scope, 0, sizeof (struct c_scope));
1478 : 89679802 : scope->outer = scope_freelist;
1479 : 89679802 : scope_freelist = scope;
1480 : :
1481 : 89679802 : return block;
1482 : : }
1483 : :
1484 : : void
1485 : 107224 : push_file_scope (void)
1486 : : {
1487 : 107224 : tree decl;
1488 : :
1489 : 107224 : if (file_scope)
1490 : : return;
1491 : :
1492 : 107224 : push_scope ();
1493 : 107224 : file_scope = current_scope;
1494 : :
1495 : 107224 : start_fname_decls ();
1496 : :
1497 : 213683725 : for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1498 : 213576501 : bind (DECL_NAME (decl), decl, file_scope,
1499 : 213576501 : /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1500 : : }
1501 : :
1502 : : void
1503 : 107067 : pop_file_scope (void)
1504 : : {
1505 : : /* In case there were missing closebraces, get us back to the global
1506 : : binding level. */
1507 : 107072 : 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 : 107067 : finish_fname_decls ();
1514 : :
1515 : 107067 : 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 : 107067 : if (pch_file)
1520 : : {
1521 : 362 : c_common_write_pch ();
1522 : : /* Ensure even the callers don't try to finalize the CU. */
1523 : 362 : flag_syntax_only = 1;
1524 : 362 : return;
1525 : : }
1526 : :
1527 : : /* Pop off the file scope and close this translation unit. */
1528 : 106705 : pop_scope ();
1529 : 106705 : file_scope = 0;
1530 : :
1531 : 106705 : 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 : 650 : start_underspecified_init (location_t loc, tree name)
1548 : : {
1549 : 650 : bool prev = in_underspecified_init;
1550 : 650 : bool ok;
1551 : 650 : if (name == NULL_TREE)
1552 : : ok = true;
1553 : : else
1554 : : {
1555 : 418 : tree decl = build_decl (loc, VAR_DECL, name, error_mark_node);
1556 : 418 : C_DECL_UNDERSPECIFIED (decl) = 1;
1557 : 418 : struct c_scope *scope = current_scope;
1558 : 418 : struct c_binding *b = I_SYMBOL_BINDING (name);
1559 : 418 : 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 : 414 : bind (name, decl, scope, false, false, loc);
1568 : 414 : ok = true;
1569 : : }
1570 : : }
1571 : 650 : in_underspecified_init = true;
1572 : 650 : 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 : 650 : finish_underspecified_init (tree name, unsigned int prev_state)
1582 : : {
1583 : 650 : 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 : 414 : struct c_scope *scope = current_scope;
1588 : 414 : struct c_binding *b = I_SYMBOL_BINDING (name);
1589 : 414 : gcc_assert (b);
1590 : 414 : gcc_assert (B_IN_SCOPE (b, scope));
1591 : 414 : gcc_assert (VAR_P (b->decl));
1592 : 414 : gcc_assert (C_DECL_UNDERSPECIFIED (b->decl));
1593 : 414 : I_SYMBOL_BINDING (name) = b->shadowed;
1594 : : /* In erroneous cases there may be other bindings added to this
1595 : : scope during the initializer. */
1596 : 414 : struct c_binding **p = &scope->bindings;
1597 : 469 : while (*p != b)
1598 : 55 : p = &((*p)->prev);
1599 : 414 : *p = free_binding_and_advance (*p);
1600 : : }
1601 : 650 : in_underspecified_init = (prev_state & (1u << 1)) >> 1;
1602 : 650 : }
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 : 1397223 : pushtag (location_t loc, tree name, tree type)
1695 : : {
1696 : : /* Record the identifier as the type's name if it has none. */
1697 : 1397223 : if (name && !TYPE_NAME (type))
1698 : 614212 : TYPE_NAME (type) = name;
1699 : 1397223 : 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 : 1397223 : 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 : 1397223 : TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1714 : :
1715 : 1397223 : 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 : 1397223 : }
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 : 616372 : types_close_enough_to_match (tree t1, tree t2)
1804 : : {
1805 : 616372 : return (TYPE_MODE (t1) == TYPE_MODE (t2)
1806 : 615917 : && POINTER_TYPE_P (t1) == POINTER_TYPE_P (t2)
1807 : 2463970 : && 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 : 142971 : match_builtin_function_types (tree newtype, tree oldtype,
1818 : : tree *strict, unsigned *argno)
1819 : : {
1820 : 142971 : *argno = 0;
1821 : 142971 : *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 : 142971 : tree oldrettype = TREE_TYPE (oldtype);
1826 : 142971 : tree newrettype = TREE_TYPE (newtype);
1827 : :
1828 : 142971 : 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 : 142609 : if (!comptypes (TYPE_MAIN_VARIANT (oldrettype),
1834 : 142609 : TYPE_MAIN_VARIANT (newrettype)))
1835 : 39 : *strict = oldrettype;
1836 : :
1837 : 142609 : tree oldargs = TYPE_ARG_TYPES (oldtype);
1838 : 142609 : tree newargs = TYPE_ARG_TYPES (newtype);
1839 : 142609 : tree tryargs = newargs;
1840 : :
1841 : 142609 : const unsigned nlst = ARRAY_SIZE (last_structptr_types);
1842 : 142609 : const unsigned nbst = ARRAY_SIZE (builtin_structptr_types);
1843 : :
1844 : 142609 : gcc_checking_assert (nlst == nbst);
1845 : :
1846 : 615813 : for (unsigned i = 1; oldargs || newargs; ++i)
1847 : : {
1848 : 473460 : if (!oldargs
1849 : 473460 : || !newargs
1850 : 473405 : || !TREE_VALUE (oldargs)
1851 : 946865 : || !TREE_VALUE (newargs))
1852 : : return NULL_TREE;
1853 : :
1854 : 473405 : tree oldtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1855 : 473405 : tree newtype = TREE_VALUE (newargs);
1856 : 473405 : if (newtype == error_mark_node)
1857 : : return NULL_TREE;
1858 : 473401 : newtype = TYPE_MAIN_VARIANT (newtype);
1859 : :
1860 : 473401 : if (!types_close_enough_to_match (oldtype, newtype))
1861 : : return NULL_TREE;
1862 : :
1863 : 473257 : unsigned j = nbst;
1864 : 473257 : 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 : 966789 : for (j = 0; j < nbst; ++j)
1869 : : {
1870 : 851740 : if (TREE_VALUE (oldargs) != builtin_structptr_types[j].node)
1871 : 709595 : 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 : 142145 : if (last_structptr_types[j])
1877 : : {
1878 : 124859 : if (!comptypes (last_structptr_types[j], newtype))
1879 : : {
1880 : 2 : *argno = i;
1881 : 2 : *strict = last_structptr_types[j];
1882 : : }
1883 : : }
1884 : : else
1885 : 17286 : last_structptr_types[j] = newtype;
1886 : : break;
1887 : : }
1888 : :
1889 : 473257 : 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 : 473204 : oldargs = TREE_CHAIN (oldargs);
1911 : 473204 : newargs = TREE_CHAIN (newargs);
1912 : : }
1913 : :
1914 : 142353 : tree trytype = c_build_function_type (newrettype, tryargs);
1915 : :
1916 : : /* Allow declaration to change transaction_safe attribute. */
1917 : 142353 : tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1918 : 142353 : tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1919 : 142353 : tree newattrs = TYPE_ATTRIBUTES (newtype);
1920 : 142353 : tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1921 : 142353 : if (oldtsafe && !newtsafe)
1922 : 0 : oldattrs = remove_attribute ("transaction_safe", oldattrs);
1923 : 142353 : else if (newtsafe && !oldtsafe)
1924 : 7 : oldattrs = tree_cons (get_identifier ("transaction_safe"),
1925 : : NULL_TREE, oldattrs);
1926 : :
1927 : 142353 : 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 : 407 : locate_old_decl (tree decl)
2050 : : {
2051 : 407 : if (TREE_CODE (decl) == FUNCTION_DECL
2052 : 194 : && fndecl_built_in_p (decl)
2053 : 410 : && !C_DECL_DECLARED_BUILTIN (decl))
2054 : : ;
2055 : 407 : 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 : 289 : 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 : 273 : inform (input_location,
2065 : : "previous declaration of %q+D with type %qT",
2066 : 273 : decl, TREE_TYPE (decl));
2067 : 407 : }
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 : 1108593 : previous_tag (tree type)
2075 : : {
2076 : 1108593 : struct c_binding *b = NULL;
2077 : 1108593 : tree name = c_type_tag (type);
2078 : :
2079 : 1108593 : if (name)
2080 : 554177 : b = I_TAG_BINDING (name);
2081 : :
2082 : 554177 : if (b)
2083 : 554177 : b = b->shadowed;
2084 : :
2085 : 1108593 : 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 : 4909436 : diagnose_mismatched_decls (tree newdecl, tree olddecl,
2101 : : tree *newtypep, tree *oldtypep)
2102 : : {
2103 : 4909436 : tree newtype, oldtype;
2104 : 4909436 : 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 : 4909436 : if (olddecl == error_mark_node || newdecl == error_mark_node)
2112 : : return false;
2113 : 4909416 : *oldtypep = oldtype = TREE_TYPE (olddecl);
2114 : 4909416 : *newtypep = newtype = TREE_TYPE (newdecl);
2115 : 4909416 : 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 : 4909408 : 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 : 4909379 : 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 : 4909333 : bool pedwarned = false;
2166 : 4909333 : bool warned = false;
2167 : 4909333 : bool enum_and_int_p = false;
2168 : 4909333 : auto_diagnostic_group d;
2169 : :
2170 : 4909333 : int comptypes_result = comptypes_check_enum_int (oldtype, newtype,
2171 : : &enum_and_int_p);
2172 : 4909333 : if (!comptypes_result)
2173 : : {
2174 : 143165 : if (TREE_CODE (olddecl) == FUNCTION_DECL
2175 : 143090 : && fndecl_built_in_p (olddecl, BUILT_IN_NORMAL)
2176 : 286137 : && !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 : 142971 : tree mismatch_expect;
2183 : 142971 : unsigned mismatch_argno;
2184 : :
2185 : 142971 : tree trytype = match_builtin_function_types (newtype, oldtype,
2186 : : &mismatch_expect,
2187 : : &mismatch_argno);
2188 : :
2189 : 142971 : if (trytype && comptypes (newtype, trytype))
2190 : 142353 : *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 : 142353 : 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 : 4766168 : else if (enum_and_int_p
2317 : 193 : && TREE_CODE (newdecl) != TYPE_DECL
2318 : : /* Don't warn about acc_on_device built-in redeclaration,
2319 : : the built-in is declared with int rather than enum because
2320 : : the enum isn't intrinsic. */
2321 : 4766357 : && !(TREE_CODE (olddecl) == FUNCTION_DECL
2322 : 152 : && fndecl_built_in_p (olddecl, BUILT_IN_ACC_ON_DEVICE)
2323 : 122 : && !C_DECL_DECLARED_BUILTIN (olddecl)))
2324 : 67 : warned = warning_at (DECL_SOURCE_LOCATION (newdecl),
2325 : 67 : OPT_Wenum_int_mismatch,
2326 : : "conflicting types for %q+D due to enum/integer "
2327 : : "mismatch; have %qT", newdecl, newtype);
2328 : :
2329 : : /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2330 : : but silently ignore the redeclaration if either is in a system
2331 : : header. (Conflicting redeclarations were handled above.) This
2332 : : is allowed for C11 if the types are the same, not just
2333 : : compatible. */
2334 : 4908545 : 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 : 4864025 : 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 : 4845589 : if (fndecl_built_in_p (olddecl)
2382 : 8663951 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2383 : : {
2384 : 3563239 : if (!TREE_PUBLIC (newdecl)
2385 : 3563239 : || (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 : 3563136 : 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 : 4845486 : if (DECL_INITIAL (newdecl))
2412 : : {
2413 : 228858 : 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 : 4616628 : else if (DECL_INITIAL (olddecl)
2438 : 806 : && !prototype_p (oldtype) && prototype_p (newtype)
2439 : 4616648 : && 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 : 4845450 : if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2458 : : {
2459 : : /* Two exceptions to the rule. If olddecl is an extern
2460 : : inline, or a predeclared function that isn't actually
2461 : : built in, newdecl silently overrides olddecl. The latter
2462 : : occur only in Objective C; see also above. (FIXME: Make
2463 : : Objective C use normal builtins.) */
2464 : 18 : if (!DECL_IS_UNDECLARED_BUILTIN (olddecl)
2465 : 36 : && !DECL_EXTERN_INLINE (olddecl))
2466 : : {
2467 : 4 : auto_diagnostic_group d;
2468 : 4 : error ("static declaration of %q+D follows "
2469 : : "non-static declaration", newdecl);
2470 : 4 : locate_old_decl (olddecl);
2471 : 4 : }
2472 : 18 : return false;
2473 : : }
2474 : 4845432 : 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 : 4845432 : if (DECL_DECLARED_INLINE_P (olddecl)
2495 : 4846495 : && 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 : 18436 : else if (VAR_P (newdecl))
2512 : : {
2513 : : /* Only variables can be thread-local, and all declarations must
2514 : : agree on this property. */
2515 : 18405 : 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 : 55002 : 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 : 18939 : if ((DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2546 : 18938 : || (flag_isoc23
2547 : 6249 : && 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 : 18409 : if (DECL_FILE_SCOPE_P (newdecl)
2566 : 18392 : && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2567 : : {
2568 : 141 : if (DECL_EXTERNAL (newdecl))
2569 : : {
2570 : 139 : if (!DECL_FILE_SCOPE_P (olddecl))
2571 : : {
2572 : 2 : auto_diagnostic_group d;
2573 : 2 : error ("extern declaration of %q+D follows "
2574 : : "declaration with no linkage", newdecl);
2575 : 2 : locate_old_decl (olddecl);
2576 : 2 : return false;
2577 : 2 : }
2578 : 137 : else if (warn_traditional)
2579 : : {
2580 : 0 : warned |= warning (OPT_Wtraditional,
2581 : : "non-static declaration of %q+D "
2582 : : "follows static declaration", newdecl);
2583 : : }
2584 : : }
2585 : : else
2586 : : {
2587 : 2 : auto_diagnostic_group d;
2588 : 2 : if (TREE_PUBLIC (newdecl))
2589 : 2 : error ("non-static declaration of %q+D follows "
2590 : : "static declaration", newdecl);
2591 : : else
2592 : 0 : error ("static declaration of %q+D follows "
2593 : : "non-static declaration", newdecl);
2594 : :
2595 : 2 : locate_old_decl (olddecl);
2596 : 2 : return false;
2597 : 2 : }
2598 : : }
2599 : : /* Two objects with the same name declared at the same block
2600 : : scope must both be external references (6.7p3). */
2601 : 18251 : 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 : 18371 : if (warn_cxx_compat
2628 : 68 : && DECL_FILE_SCOPE_P (newdecl)
2629 : 68 : && !DECL_EXTERNAL (newdecl)
2630 : 18389 : && !DECL_EXTERNAL (olddecl))
2631 : 5 : warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2632 : 5 : OPT_Wc___compat,
2633 : : ("duplicate declaration of %qD is "
2634 : : "invalid in C++"),
2635 : : newdecl);
2636 : : }
2637 : :
2638 : : /* warnings */
2639 : : /* All decls must agree on a visibility. */
2640 : 4863834 : if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2641 : 4863803 : && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2642 : 4864569 : && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2643 : : {
2644 : 1 : warned |= warning (0, "redeclaration of %q+D with different visibility "
2645 : : "(old visibility preserved)", newdecl);
2646 : : }
2647 : :
2648 : 4863834 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2649 : 4845432 : warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2650 : : else /* PARM_DECL, VAR_DECL */
2651 : : {
2652 : : /* Redeclaration of a parameter is a constraint violation (this is
2653 : : not explicitly stated, but follows from C99 6.7p3 [no more than
2654 : : one declaration of the same identifier with no linkage in the
2655 : : same scope, except type tags] and 6.2.2p6 [parameters have no
2656 : : linkage]). We must check for a forward parameter declaration,
2657 : : indicated by TREE_ASM_WRITTEN on the old declaration - this is
2658 : : an extension, the mandatory diagnostic for which is handled by
2659 : : mark_forward_parm_decls. */
2660 : :
2661 : 18402 : if (TREE_CODE (newdecl) == PARM_DECL
2662 : 31 : && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2663 : : {
2664 : 2 : auto_diagnostic_group d;
2665 : 2 : error ("redefinition of parameter %q+D", newdecl);
2666 : 2 : locate_old_decl (olddecl);
2667 : 2 : return false;
2668 : 2 : }
2669 : : }
2670 : :
2671 : : /* Optional warning for completely redundant decls. */
2672 : 4863832 : if (!warned && !pedwarned
2673 : 4863758 : && warn_redundant_decls
2674 : : /* Don't warn about a function declaration followed by a
2675 : : definition. */
2676 : 18 : && !(TREE_CODE (newdecl) == FUNCTION_DECL
2677 : 2 : && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2678 : : /* Don't warn about redundant redeclarations of builtins. */
2679 : 18 : && !(TREE_CODE (newdecl) == FUNCTION_DECL
2680 : 2 : && !fndecl_built_in_p (newdecl)
2681 : 2 : && fndecl_built_in_p (olddecl)
2682 : 2 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2683 : : /* Don't warn about an extern followed by a definition. */
2684 : 17 : && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2685 : : /* Don't warn about forward parameter decls. */
2686 : 17 : && !(TREE_CODE (newdecl) == PARM_DECL
2687 : 6 : && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2688 : : /* Don't warn about a variable definition following a declaration. */
2689 : 4863843 : && !(VAR_P (newdecl)
2690 : 10 : && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2691 : : {
2692 : 6 : warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2693 : : newdecl);
2694 : : }
2695 : :
2696 : : /* Report location of previous decl/defn. */
2697 : 4863832 : if (warned || pedwarned)
2698 : 80 : locate_old_decl (olddecl);
2699 : :
2700 : : #undef DECL_EXTERN_INLINE
2701 : :
2702 : : return retval;
2703 : 4909333 : }
2704 : :
2705 : : /* Subroutine of duplicate_decls. NEWDECL has been found to be
2706 : : consistent with OLDDECL, but carries new information. Merge the
2707 : : new information into OLDDECL. This function issues no
2708 : : diagnostics. */
2709 : :
2710 : : static void
2711 : 4908341 : merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2712 : : {
2713 : 4908341 : bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2714 : 4908341 : && DECL_INITIAL (newdecl) != NULL_TREE);
2715 : 4908341 : bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2716 : 4908341 : && prototype_p (TREE_TYPE (newdecl)));
2717 : 4908341 : bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2718 : 4908341 : && prototype_p (TREE_TYPE (olddecl)));
2719 : :
2720 : : /* For real parm decl following a forward decl, rechain the old decl
2721 : : in its new location and clear TREE_ASM_WRITTEN (it's not a
2722 : : forward decl anymore). */
2723 : 4908341 : if (TREE_CODE (newdecl) == PARM_DECL
2724 : 29 : && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2725 : : {
2726 : 29 : struct c_binding *b, **here;
2727 : :
2728 : 44 : for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
2729 : 44 : if ((*here)->decl == olddecl)
2730 : 29 : goto found;
2731 : 0 : gcc_unreachable ();
2732 : :
2733 : 29 : found:
2734 : 29 : b = *here;
2735 : 29 : *here = b->prev;
2736 : 29 : b->prev = current_scope->bindings;
2737 : 29 : current_scope->bindings = b;
2738 : :
2739 : 29 : TREE_ASM_WRITTEN (olddecl) = 0;
2740 : : }
2741 : :
2742 : 4908341 : DECL_ATTRIBUTES (newdecl)
2743 : 4908341 : = targetm.merge_decl_attributes (olddecl, newdecl);
2744 : :
2745 : : /* For typedefs use the old type, as the new type's DECL_NAME points
2746 : : at newdecl, which will be ggc_freed. */
2747 : 4908341 : if (TREE_CODE (newdecl) == TYPE_DECL)
2748 : : {
2749 : : /* But NEWTYPE might have an attribute, honor that. */
2750 : 44509 : tree tem = newtype;
2751 : 44509 : newtype = oldtype;
2752 : :
2753 : 44509 : if (TYPE_USER_ALIGN (tem))
2754 : : {
2755 : 12 : if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2756 : 6 : SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2757 : 12 : TYPE_USER_ALIGN (newtype) = true;
2758 : : }
2759 : :
2760 : : /* And remove the new type from the variants list. */
2761 : 44509 : if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2762 : : {
2763 : 12 : tree remove = TREE_TYPE (newdecl);
2764 : 12 : if (TYPE_MAIN_VARIANT (remove) == remove)
2765 : : {
2766 : 2 : gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2767 : : /* If remove is the main variant, no need to remove that
2768 : : from the list. One of the DECL_ORIGINAL_TYPE
2769 : : variants, e.g. created for aligned attribute, might still
2770 : : refer to the newdecl TYPE_DECL though, so remove that one
2771 : : in that case. */
2772 : 2 : if (DECL_ORIGINAL_TYPE (newdecl)
2773 : 2 : && DECL_ORIGINAL_TYPE (newdecl) != remove)
2774 : 2 : for (tree t = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl));
2775 : 2 : t; t = TYPE_MAIN_VARIANT (t))
2776 : 2 : if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2777 : : {
2778 : 4 : TYPE_NEXT_VARIANT (t)
2779 : 2 : = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2780 : 2 : break;
2781 : : }
2782 : : }
2783 : : else
2784 : 10 : for (tree t = TYPE_MAIN_VARIANT (remove); ;
2785 : 0 : t = TYPE_NEXT_VARIANT (t))
2786 : 10 : if (TYPE_NEXT_VARIANT (t) == remove)
2787 : : {
2788 : 10 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2789 : 10 : break;
2790 : : }
2791 : : }
2792 : :
2793 : : /* Make sure we refer to the same type as the olddecl. */
2794 : 44509 : DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2795 : : }
2796 : :
2797 : : /* Merge the data types specified in the two decls. */
2798 : 9816682 : TREE_TYPE (newdecl)
2799 : 4908341 : = TREE_TYPE (olddecl)
2800 : 9816682 : = composite_type (newtype, oldtype);
2801 : :
2802 : : /* Lay the type out, unless already done. */
2803 : 4908341 : if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2804 : : {
2805 : 0 : if (TREE_TYPE (newdecl) != error_mark_node)
2806 : 0 : layout_type (TREE_TYPE (newdecl));
2807 : 0 : if (TREE_CODE (newdecl) != FUNCTION_DECL
2808 : : && TREE_CODE (newdecl) != TYPE_DECL
2809 : : && TREE_CODE (newdecl) != CONST_DECL)
2810 : 0 : layout_decl (newdecl, 0);
2811 : : }
2812 : : else
2813 : : {
2814 : : /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2815 : 4908341 : DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2816 : 4908341 : DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2817 : 4908341 : SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2818 : 4908341 : if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2819 : : {
2820 : 44 : SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2821 : 44 : DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2822 : : }
2823 : 4908297 : else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2824 : 4908297 : && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2825 : 3 : DECL_USER_ALIGN (newdecl) = 1;
2826 : 9816682 : if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2827 : 4908341 : > DECL_WARN_IF_NOT_ALIGN (newdecl))
2828 : 0 : SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2829 : : DECL_WARN_IF_NOT_ALIGN (olddecl));
2830 : : }
2831 : :
2832 : : /* Keep the old rtl since we can safely use it. */
2833 : 4908341 : if (HAS_RTL_P (olddecl))
2834 : 4908341 : COPY_DECL_RTL (olddecl, newdecl);
2835 : :
2836 : : /* Merge the type qualifiers. */
2837 : 4908341 : if (TREE_READONLY (newdecl))
2838 : 387444 : TREE_READONLY (olddecl) = 1;
2839 : :
2840 : 4908341 : if (TREE_THIS_VOLATILE (newdecl))
2841 : 48683 : TREE_THIS_VOLATILE (olddecl) = 1;
2842 : :
2843 : : /* Merge deprecatedness. */
2844 : 4908341 : if (TREE_DEPRECATED (newdecl))
2845 : 331 : TREE_DEPRECATED (olddecl) = 1;
2846 : :
2847 : : /* Merge unavailability. */
2848 : 4908341 : if (TREE_UNAVAILABLE (newdecl))
2849 : 2 : TREE_UNAVAILABLE (olddecl) = 1;
2850 : :
2851 : : /* If a decl is in a system header and the other isn't, keep the one on the
2852 : : system header. Otherwise, keep source location of definition rather than
2853 : : declaration and of prototype rather than non-prototype unless that
2854 : : prototype is built-in. */
2855 : 4908341 : if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2856 : 4908312 : && DECL_IN_SYSTEM_HEADER (olddecl)
2857 : 5582169 : && !DECL_IN_SYSTEM_HEADER (newdecl) )
2858 : 1035 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2859 : 4907306 : else if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2860 : 4907277 : && DECL_IN_SYSTEM_HEADER (newdecl)
2861 : 9091239 : && !DECL_IN_SYSTEM_HEADER (olddecl))
2862 : 3511140 : DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2863 : 1396166 : else if ((DECL_INITIAL (newdecl) == NULL_TREE
2864 : 1166869 : && DECL_INITIAL (olddecl) != NULL_TREE)
2865 : 2562131 : || (old_is_prototype && !new_is_prototype
2866 : 372 : && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2867 : 932 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2868 : :
2869 : : /* Merge the initialization information. */
2870 : 4908341 : if (DECL_INITIAL (newdecl) == NULL_TREE)
2871 : 4678972 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2872 : :
2873 : : /* Merge 'constexpr' information. */
2874 : 4908341 : if (VAR_P (olddecl) && VAR_P (newdecl))
2875 : : {
2876 : 18371 : if (C_DECL_DECLARED_CONSTEXPR (olddecl))
2877 : 2 : C_DECL_DECLARED_CONSTEXPR (newdecl) = 1;
2878 : 18369 : else if (C_DECL_DECLARED_CONSTEXPR (newdecl))
2879 : 1 : C_DECL_DECLARED_CONSTEXPR (olddecl) = 1;
2880 : : }
2881 : :
2882 : : /* Merge the threadprivate attribute. */
2883 : 4908341 : if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2884 : 7 : C_DECL_THREADPRIVATE_P (newdecl) = 1;
2885 : :
2886 : 4908341 : if (HAS_DECL_ASSEMBLER_NAME_P (olddecl))
2887 : : {
2888 : : /* Copy the assembler name.
2889 : : Currently, it can only be defined in the prototype. */
2890 : 4908312 : COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2891 : :
2892 : : /* Use visibility of whichever declaration had it specified */
2893 : 4908312 : if (DECL_VISIBILITY_SPECIFIED (olddecl))
2894 : : {
2895 : 4738 : DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2896 : 4738 : DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2897 : : }
2898 : :
2899 : 4908312 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2900 : : {
2901 : 4845432 : DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2902 : 4845432 : DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2903 : 4845432 : DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2904 : 4845432 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2905 : 4845432 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2906 : 4845432 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2907 : 4845432 : DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2908 : 4845432 : if (DECL_IS_OPERATOR_NEW_P (olddecl))
2909 : 0 : DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2910 : 4845432 : if (DECL_IS_OPERATOR_DELETE_P (olddecl))
2911 : 0 : DECL_SET_IS_OPERATOR_DELETE (newdecl, true);
2912 : 4845432 : TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2913 : 4845432 : DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2914 : 4845432 : DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2915 : : }
2916 : :
2917 : : /* Merge the storage class information. */
2918 : 4908312 : merge_weak (newdecl, olddecl);
2919 : :
2920 : : /* For functions, static overrides non-static. */
2921 : 4908312 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2922 : : {
2923 : 4845432 : TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2924 : : /* This is since we don't automatically
2925 : : copy the attributes of NEWDECL into OLDDECL. */
2926 : 4845432 : TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2927 : : /* If this clears `static', clear it in the identifier too. */
2928 : 4845432 : if (!TREE_PUBLIC (olddecl))
2929 : 7549 : TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2930 : : }
2931 : : }
2932 : :
2933 : : /* In c99, 'extern' declaration before (or after) 'inline' means this
2934 : : function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2935 : : is present. */
2936 : 4908341 : if (TREE_CODE (newdecl) == FUNCTION_DECL
2937 : 4845432 : && !flag_gnu89_inline
2938 : 4821545 : && (DECL_DECLARED_INLINE_P (newdecl)
2939 : 4634604 : || DECL_DECLARED_INLINE_P (olddecl))
2940 : 187135 : && (!DECL_DECLARED_INLINE_P (newdecl)
2941 : 186941 : || !DECL_DECLARED_INLINE_P (olddecl)
2942 : 795 : || !DECL_EXTERNAL (olddecl))
2943 : 186358 : && DECL_EXTERNAL (newdecl)
2944 : 186150 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2945 : 4908439 : && !current_function_decl)
2946 : 81 : DECL_EXTERNAL (newdecl) = 0;
2947 : :
2948 : : /* An inline definition following a static declaration is not
2949 : : DECL_EXTERNAL. */
2950 : 4908341 : if (new_is_definition
2951 : 228815 : && (DECL_DECLARED_INLINE_P (newdecl)
2952 : 41652 : || DECL_DECLARED_INLINE_P (olddecl))
2953 : 5095718 : && !TREE_PUBLIC (olddecl))
2954 : 888 : DECL_EXTERNAL (newdecl) = 0;
2955 : :
2956 : 4908341 : if (DECL_EXTERNAL (newdecl))
2957 : : {
2958 : 4819025 : TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2959 : 4819025 : DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2960 : :
2961 : : /* An extern decl does not override previous storage class. */
2962 : 4819025 : TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2963 : 4819025 : if (!DECL_EXTERNAL (newdecl))
2964 : : {
2965 : 1409 : DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2966 : 1409 : DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2967 : : }
2968 : : }
2969 : : else
2970 : : {
2971 : 89316 : TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2972 : 89316 : TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2973 : : }
2974 : :
2975 : 4908341 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2976 : : {
2977 : : /* If we're redefining a function previously defined as extern
2978 : : inline, make sure we emit debug info for the inline before we
2979 : : throw it away, in case it was inlined into a function that
2980 : : hasn't been written out yet. */
2981 : 4845432 : if (new_is_definition && DECL_INITIAL (olddecl))
2982 : : /* The new defn must not be inline. */
2983 : 75 : DECL_UNINLINABLE (newdecl) = 1;
2984 : : else
2985 : : {
2986 : : /* If either decl says `inline', this fn is inline, unless
2987 : : its definition was passed already. */
2988 : 4845357 : if (DECL_DECLARED_INLINE_P (newdecl)
2989 : 9503499 : || DECL_DECLARED_INLINE_P (olddecl))
2990 : 187400 : DECL_DECLARED_INLINE_P (newdecl) = 1;
2991 : :
2992 : 14536071 : DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2993 : 9692373 : = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2994 : :
2995 : 9690714 : DECL_DISREGARD_INLINE_LIMITS (newdecl)
2996 : 4845357 : = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2997 : 4845357 : = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2998 : 9690576 : || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2999 : : }
3000 : :
3001 : 4845432 : if (fndecl_built_in_p (olddecl))
3002 : : {
3003 : : /* If redeclaring a builtin function, it stays built in.
3004 : : But it gets tagged as having been declared. */
3005 : 3818259 : copy_decl_built_in_function (newdecl, olddecl);
3006 : 3818259 : C_DECL_DECLARED_BUILTIN (newdecl) = 1;
3007 : 3818259 : if (new_is_prototype)
3008 : : {
3009 : 3817900 : C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
3010 : 3817900 : if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3011 : : {
3012 : 3817900 : enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3013 : 3817900 : switch (fncode)
3014 : : {
3015 : : /* If a compatible prototype of these builtin functions
3016 : : is seen, assume the runtime implements it with the
3017 : : expected semantics. */
3018 : 6446 : case BUILT_IN_STPCPY:
3019 : 6446 : if (builtin_decl_explicit_p (fncode))
3020 : 6446 : set_builtin_decl_implicit_p (fncode, true);
3021 : : break;
3022 : 3811454 : default:
3023 : 3811454 : if (builtin_decl_explicit_p (fncode))
3024 : 3811454 : set_builtin_decl_declared_p (fncode, true);
3025 : : break;
3026 : : }
3027 : :
3028 : 3817900 : copy_attributes_to_builtin (newdecl);
3029 : : }
3030 : : }
3031 : : else
3032 : 718 : C_DECL_BUILTIN_PROTOTYPE (newdecl)
3033 : 718 : = C_DECL_BUILTIN_PROTOTYPE (olddecl);
3034 : : }
3035 : :
3036 : : /* Preserve function specific target and optimization options */
3037 : 4845432 : if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
3038 : 4845937 : && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
3039 : 469 : DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
3040 : 469 : = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
3041 : :
3042 : 4845432 : if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
3043 : 4868854 : && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
3044 : 9 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
3045 : 9 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
3046 : :
3047 : : /* Also preserve various other info from the definition. */
3048 : 4845432 : if (!new_is_definition)
3049 : : {
3050 : 4616617 : tree t;
3051 : 4616617 : DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3052 : 4616617 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3053 : 4616617 : DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
3054 : 4616617 : DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3055 : 4616617 : DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
3056 : 7232378 : for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
3057 : 2615761 : DECL_CONTEXT (t) = newdecl;
3058 : :
3059 : : /* See if we've got a function to instantiate from. */
3060 : 4616617 : if (DECL_SAVED_TREE (olddecl))
3061 : 1584 : DECL_ABSTRACT_ORIGIN (newdecl)
3062 : 792 : = DECL_ABSTRACT_ORIGIN (olddecl);
3063 : : }
3064 : : }
3065 : :
3066 : : /* Merge the USED information. */
3067 : 4908341 : if (TREE_USED (olddecl))
3068 : 702998 : TREE_USED (newdecl) = 1;
3069 : 4205343 : else if (TREE_USED (newdecl))
3070 : 4 : TREE_USED (olddecl) = 1;
3071 : 4908341 : if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
3072 : 18400 : DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
3073 : 4908341 : if (DECL_PRESERVE_P (olddecl))
3074 : 25 : DECL_PRESERVE_P (newdecl) = 1;
3075 : 4908316 : else if (DECL_PRESERVE_P (newdecl))
3076 : 4 : DECL_PRESERVE_P (olddecl) = 1;
3077 : :
3078 : : /* Merge DECL_COMMON */
3079 : 18371 : if (VAR_P (olddecl) && VAR_P (newdecl)
3080 : 18371 : && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
3081 : 4926710 : && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
3082 : 36734 : DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
3083 : :
3084 : : /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
3085 : : But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
3086 : : DECL_ARGUMENTS (if appropriate). */
3087 : 4908341 : {
3088 : 4908341 : unsigned olddecl_uid = DECL_UID (olddecl);
3089 : 4908341 : tree olddecl_context = DECL_CONTEXT (olddecl);
3090 : 4908341 : tree olddecl_arguments = NULL;
3091 : 4908341 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3092 : 4845432 : olddecl_arguments = DECL_ARGUMENTS (olddecl);
3093 : :
3094 : 4908341 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3095 : : (char *) newdecl + sizeof (struct tree_common),
3096 : : sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3097 : 4908341 : DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3098 : 4908341 : switch (TREE_CODE (olddecl))
3099 : : {
3100 : 4863803 : case FUNCTION_DECL:
3101 : 4863803 : case VAR_DECL:
3102 : 4863803 : {
3103 : 4863803 : struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
3104 : :
3105 : 9727606 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3106 : : (char *) newdecl + sizeof (struct tree_decl_common),
3107 : 4863803 : tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3108 : 4863803 : olddecl->decl_with_vis.symtab_node = snode;
3109 : :
3110 : 4863803 : if ((DECL_EXTERNAL (olddecl)
3111 : 46187 : || TREE_PUBLIC (olddecl)
3112 : 7811 : || TREE_STATIC (olddecl))
3113 : 4909983 : && DECL_SECTION_NAME (newdecl) != NULL)
3114 : 8 : set_decl_section_name (olddecl, newdecl);
3115 : :
3116 : : /* This isn't quite correct for something like
3117 : : int __thread x attribute ((tls_model ("local-exec")));
3118 : : extern int __thread x;
3119 : : as we'll lose the "local-exec" model. */
3120 : 4863803 : if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
3121 : 89 : set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3122 : : break;
3123 : : }
3124 : :
3125 : 44538 : case FIELD_DECL:
3126 : 44538 : case PARM_DECL:
3127 : 44538 : case LABEL_DECL:
3128 : 44538 : case RESULT_DECL:
3129 : 44538 : case CONST_DECL:
3130 : 44538 : case TYPE_DECL:
3131 : 89076 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3132 : : (char *) newdecl + sizeof (struct tree_decl_common),
3133 : 44538 : tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3134 : 44538 : break;
3135 : :
3136 : 0 : default:
3137 : :
3138 : 0 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3139 : : (char *) newdecl + sizeof (struct tree_decl_common),
3140 : : sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
3141 : : }
3142 : 4908341 : DECL_UID (olddecl) = olddecl_uid;
3143 : 4908341 : DECL_CONTEXT (olddecl) = olddecl_context;
3144 : 4908341 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3145 : 4845432 : DECL_ARGUMENTS (olddecl) = olddecl_arguments;
3146 : : }
3147 : :
3148 : : /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3149 : : so that encode_section_info has a chance to look at the new decl
3150 : : flags and attributes. */
3151 : 4908341 : if (DECL_RTL_SET_P (olddecl)
3152 : 4908341 : && (TREE_CODE (olddecl) == FUNCTION_DECL
3153 : 0 : || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
3154 : 0 : make_decl_rtl (olddecl);
3155 : 4908341 : }
3156 : :
3157 : : /* Handle when a new declaration NEWDECL has the same name as an old
3158 : : one OLDDECL in the same binding contour. Prints an error message
3159 : : if appropriate.
3160 : :
3161 : : If safely possible, alter OLDDECL to look like NEWDECL, and return
3162 : : true. Otherwise, return false. */
3163 : :
3164 : : static bool
3165 : 4909436 : duplicate_decls (tree newdecl, tree olddecl)
3166 : : {
3167 : 4909436 : tree newtype = NULL, oldtype = NULL;
3168 : :
3169 : 4909436 : if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
3170 : : {
3171 : : /* Avoid `unused variable' and other warnings for OLDDECL. */
3172 : 1095 : suppress_warning (olddecl, OPT_Wunused);
3173 : : /* If the types are completely different, poison them both with
3174 : : error_mark_node. */
3175 : 1095 : if (TREE_CODE (TREE_TYPE (newdecl)) != TREE_CODE (TREE_TYPE (olddecl))
3176 : 114 : && olddecl != error_mark_node
3177 : 1189 : && seen_error ())
3178 : : {
3179 : 60 : if (TREE_CODE (olddecl) != FUNCTION_DECL)
3180 : 48 : TREE_TYPE (olddecl) = error_mark_node;
3181 : 60 : if (TREE_CODE (newdecl) != FUNCTION_DECL)
3182 : 57 : TREE_TYPE (newdecl) = error_mark_node;
3183 : : }
3184 : 1095 : return false;
3185 : : }
3186 : :
3187 : 4908341 : merge_decls (newdecl, olddecl, newtype, oldtype);
3188 : :
3189 : : /* The NEWDECL will no longer be needed.
3190 : :
3191 : : Before releasing the node, be sure to remove function from symbol
3192 : : table that might have been inserted there to record comdat group.
3193 : : Be sure to however do not free DECL_STRUCT_FUNCTION because this
3194 : : structure is shared in between NEWDECL and OLDECL. */
3195 : 4908341 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3196 : 4845432 : DECL_STRUCT_FUNCTION (newdecl) = NULL;
3197 : 4908341 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3198 : : {
3199 : 4863803 : struct symtab_node *snode = symtab_node::get (newdecl);
3200 : 4863803 : if (snode)
3201 : 104 : snode->remove ();
3202 : : }
3203 : 4908341 : ggc_free (newdecl);
3204 : 4908341 : return true;
3205 : : }
3206 : :
3207 : :
3208 : : /* Check whether decl-node NEW_DECL shadows an existing declaration. */
3209 : : static void
3210 : 166465741 : warn_if_shadowing (tree new_decl)
3211 : : {
3212 : 166465741 : struct c_binding *b;
3213 : :
3214 : : /* Shadow warnings wanted? */
3215 : 332930566 : if (!(warn_shadow
3216 : 166465005 : || warn_shadow_local
3217 : 166464825 : || warn_shadow_compatible_local)
3218 : : /* No shadow warnings for internally generated vars. */
3219 : 166465984 : || DECL_IS_UNDECLARED_BUILTIN (new_decl))
3220 : : return;
3221 : :
3222 : : /* Is anything being shadowed? Invisible decls do not count. */
3223 : 253 : for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
3224 : 157 : if (b->decl && b->decl != new_decl && !b->invisible
3225 : 214 : && (b->decl == error_mark_node
3226 : 43 : || diagnostic_report_warnings_p (global_dc,
3227 : : DECL_SOURCE_LOCATION (b->decl))))
3228 : : {
3229 : 57 : tree old_decl = b->decl;
3230 : :
3231 : 57 : if (old_decl == error_mark_node)
3232 : : {
3233 : 14 : warning (OPT_Wshadow, "declaration of %q+D shadows previous "
3234 : : "non-variable", new_decl);
3235 : 50 : break;
3236 : : }
3237 : :
3238 : 43 : bool warned = false;
3239 : 43 : auto_diagnostic_group d;
3240 : 43 : if (TREE_CODE (old_decl) == PARM_DECL)
3241 : : {
3242 : 5 : enum opt_code warning_code;
3243 : :
3244 : : /* If '-Wshadow=compatible-local' is specified without other
3245 : : -Wshadow= flags, we will warn only when the types of the
3246 : : shadowing variable (i.e. new_decl) and the shadowed variable
3247 : : (old_decl) are compatible. */
3248 : 5 : if (warn_shadow)
3249 : : warning_code = OPT_Wshadow;
3250 : 2 : else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3251 : : warning_code = OPT_Wshadow_compatible_local;
3252 : : else
3253 : 2 : warning_code = OPT_Wshadow_local;
3254 : 5 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3255 : : "declaration of %qD shadows a parameter",
3256 : : new_decl);
3257 : : }
3258 : 38 : else if (DECL_FILE_SCOPE_P (old_decl))
3259 : : {
3260 : : /* Do not warn if a variable shadows a function, unless
3261 : : the variable is a function or a pointer-to-function. */
3262 : 34 : if (TREE_CODE (old_decl) == FUNCTION_DECL
3263 : 11 : && TREE_CODE (new_decl) != FUNCTION_DECL
3264 : 36 : && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
3265 : 7 : continue;
3266 : :
3267 : 20 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
3268 : : "declaration of %qD shadows a global "
3269 : : "declaration",
3270 : : new_decl);
3271 : : }
3272 : 11 : else if (TREE_CODE (old_decl) == FUNCTION_DECL
3273 : 11 : && fndecl_built_in_p (old_decl))
3274 : : {
3275 : 0 : warning (OPT_Wshadow, "declaration of %q+D shadows "
3276 : : "a built-in function", new_decl);
3277 : 0 : break;
3278 : : }
3279 : : else
3280 : : {
3281 : 11 : enum opt_code warning_code;
3282 : :
3283 : : /* If '-Wshadow=compatible-local' is specified without other
3284 : : -Wshadow= flags, we will warn only when the types of the
3285 : : shadowing variable (i.e. new_decl) and the shadowed variable
3286 : : (old_decl) are compatible. */
3287 : 11 : if (warn_shadow)
3288 : : warning_code = OPT_Wshadow;
3289 : 8 : else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3290 : : warning_code = OPT_Wshadow_compatible_local;
3291 : : else
3292 : 2 : warning_code = OPT_Wshadow_local;
3293 : 11 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3294 : : "declaration of %qD shadows a previous local",
3295 : : new_decl);
3296 : : }
3297 : :
3298 : 36 : if (warned)
3299 : 26 : inform (DECL_SOURCE_LOCATION (old_decl),
3300 : : "shadowed declaration is here");
3301 : :
3302 : : break;
3303 : 43 : }
3304 : : }
3305 : :
3306 : : /* Record a decl-node X as belonging to the current lexical scope.
3307 : : Check for errors (such as an incompatible declaration for the same
3308 : : name already seen in the same scope).
3309 : :
3310 : : Returns either X or an old decl for the same name.
3311 : : If an old decl is returned, it may have been smashed
3312 : : to agree with what X says. */
3313 : :
3314 : : tree
3315 : 198228123 : pushdecl (tree x)
3316 : : {
3317 : 198228123 : tree name = DECL_NAME (x);
3318 : 198228123 : struct c_scope *scope = current_scope;
3319 : 198228123 : struct c_binding *b;
3320 : 198228123 : bool nested = false;
3321 : 198228123 : location_t locus = DECL_SOURCE_LOCATION (x);
3322 : :
3323 : : /* Must set DECL_CONTEXT for everything not at file scope or
3324 : : DECL_FILE_SCOPE_P won't work. Local externs don't count
3325 : : unless they have initializers (which generate code). We
3326 : : also exclude CONST_DECLs because enumerators will get the
3327 : : type of the enum as context. */
3328 : 198228123 : if (current_function_decl
3329 : 8940883 : && TREE_CODE (x) != CONST_DECL
3330 : 207015686 : && (!VAR_OR_FUNCTION_DECL_P (x)
3331 : 8511551 : || DECL_INITIAL (x) || !TREE_PUBLIC (x)))
3332 : 8775560 : DECL_CONTEXT (x) = current_function_decl;
3333 : :
3334 : : /* Anonymous decls are just inserted in the scope. */
3335 : 198228123 : if (!name)
3336 : : {
3337 : 7682491 : bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
3338 : : locus);
3339 : 7682491 : return x;
3340 : : }
3341 : :
3342 : : /* First, see if there is another declaration with the same name in
3343 : : the current scope. If there is, duplicate_decls may do all the
3344 : : work for us. If duplicate_decls returns false, that indicates
3345 : : two incompatible decls in the same scope; we are to silently
3346 : : replace the old one (duplicate_decls has issued all appropriate
3347 : : diagnostics). In particular, we should not consider possible
3348 : : duplicates in the external scope, or shadowing. */
3349 : 190545632 : b = I_SYMBOL_BINDING (name);
3350 : 190545632 : if (b && B_IN_SCOPE (b, scope))
3351 : : {
3352 : 1365340 : struct c_binding *b_ext, *b_use;
3353 : 1365340 : tree type = TREE_TYPE (x);
3354 : 1365340 : tree visdecl = b->decl;
3355 : 1365340 : tree vistype = TREE_TYPE (visdecl);
3356 : 1365340 : if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3357 : 1365340 : && COMPLETE_TYPE_P (TREE_TYPE (x)))
3358 : 1355 : b->inner_comp = false;
3359 : 1365340 : b_use = b;
3360 : 1365340 : b_ext = b;
3361 : : /* If this is an external linkage declaration, we should check
3362 : : for compatibility with the type in the external scope before
3363 : : setting the type at this scope based on the visible
3364 : : information only. */
3365 : 1365340 : if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
3366 : : {
3367 : 2625785 : while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3368 : 1312902 : b_ext = b_ext->shadowed;
3369 : 1312883 : if (b_ext)
3370 : : {
3371 : 1312882 : b_use = b_ext;
3372 : 1312882 : if (b_use->u.type)
3373 : 266850 : TREE_TYPE (b_use->decl) = b_use->u.type;
3374 : : }
3375 : : }
3376 : 1365340 : if (duplicate_decls (x, b_use->decl))
3377 : : {
3378 : 1364977 : if (b_use != b)
3379 : : {
3380 : : /* Save the updated type in the external scope and
3381 : : restore the proper type for this scope. */
3382 : 1312681 : tree thistype;
3383 : 1312681 : if (comptypes (vistype, type))
3384 : 1312647 : thistype = composite_type (vistype, type);
3385 : : else
3386 : 34 : thistype = TREE_TYPE (b_use->decl);
3387 : 1312681 : b_use->u.type = TREE_TYPE (b_use->decl);
3388 : 1312681 : if (TREE_CODE (b_use->decl) == FUNCTION_DECL
3389 : 1312681 : && fndecl_built_in_p (b_use->decl))
3390 : 276618 : thistype
3391 : 276618 : = c_build_type_attribute_variant (thistype,
3392 : 276618 : TYPE_ATTRIBUTES
3393 : : (b_use->u.type));
3394 : 1312681 : TREE_TYPE (b_use->decl) = thistype;
3395 : : }
3396 : 1364977 : return b_use->decl;
3397 : : }
3398 : : else
3399 : 363 : goto skip_external_and_shadow_checks;
3400 : : }
3401 : :
3402 : : /* All declarations with external linkage, and all external
3403 : : references, go in the external scope, no matter what scope is
3404 : : current. However, the binding in that scope is ignored for
3405 : : purposes of normal name lookup. A separate binding structure is
3406 : : created in the requested scope; this governs the normal
3407 : : visibility of the symbol.
3408 : :
3409 : : The binding in the externals scope is used exclusively for
3410 : : detecting duplicate declarations of the same object, no matter
3411 : : what scope they are in; this is what we do here. (C99 6.2.7p2:
3412 : : All declarations that refer to the same object or function shall
3413 : : have compatible type; otherwise, the behavior is undefined.)
3414 : : However, in Objective-C, we also want to detect declarations
3415 : : conflicting with those of the basic types. */
3416 : 330453806 : if ((DECL_EXTERNAL (x) || scope == file_scope)
3417 : 200415367 : && (VAR_OR_FUNCTION_DECL_P (x) || c_dialect_objc ()))
3418 : : {
3419 : 49679933 : tree type = TREE_TYPE (x);
3420 : 49679933 : tree vistype = NULL_TREE;
3421 : 49679933 : tree visdecl = NULL_TREE;
3422 : 49679933 : bool type_saved = false;
3423 : 3544121 : if (b && !B_IN_EXTERNAL_SCOPE (b)
3424 : 1022 : && VAR_OR_FUNCTION_DECL_P (b->decl)
3425 : 49680946 : && DECL_FILE_SCOPE_P (b->decl))
3426 : : {
3427 : 789 : visdecl = b->decl;
3428 : 789 : vistype = TREE_TYPE (visdecl);
3429 : : }
3430 : 49679933 : if (scope != file_scope
3431 : 49679933 : && !DECL_IN_SYSTEM_HEADER (x))
3432 : 11452 : warning_at (locus, OPT_Wnested_externs,
3433 : : "nested extern declaration of %qD", x);
3434 : :
3435 : 49681339 : while (b && !B_IN_EXTERNAL_SCOPE (b))
3436 : : {
3437 : : /* If this decl might be modified, save its type. This is
3438 : : done here rather than when the decl is first bound
3439 : : because the type may change after first binding, through
3440 : : being completed or through attributes being added. If we
3441 : : encounter multiple such decls, only the first should have
3442 : : its type saved; the others will already have had their
3443 : : proper types saved and the types will not have changed as
3444 : : their scopes will not have been re-entered. */
3445 : 1406 : if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3446 : : {
3447 : 1006 : b->u.type = TREE_TYPE (b->decl);
3448 : 1006 : type_saved = true;
3449 : : }
3450 : 1406 : if (B_IN_FILE_SCOPE (b)
3451 : 996 : && VAR_P (b->decl)
3452 : 571 : && TREE_STATIC (b->decl)
3453 : 280 : && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3454 : 138 : && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3455 : 47 : && TREE_CODE (type) == ARRAY_TYPE
3456 : 47 : && TYPE_DOMAIN (type)
3457 : 28 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3458 : 1434 : && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3459 : : {
3460 : : /* Array type completed in inner scope, which should be
3461 : : diagnosed if the completion does not have size 1 and
3462 : : it does not get completed in the file scope. */
3463 : 6 : b->inner_comp = true;
3464 : : }
3465 : 1406 : b = b->shadowed;
3466 : : }
3467 : :
3468 : : /* If a matching external declaration has been found, set its
3469 : : type to the composite of all the types of that declaration.
3470 : : After the consistency checks, it will be reset to the
3471 : : composite of the visible types only. */
3472 : 49679933 : if (b && b->u.type)
3473 : 761 : TREE_TYPE (b->decl) = b->u.type;
3474 : :
3475 : : /* the static does not go in the externals scope. */
3476 : 3543979 : if (b && duplicate_decls (x, b->decl))
3477 : : {
3478 : 3543247 : tree thistype;
3479 : 3543247 : if (vistype)
3480 : : {
3481 : 661 : if (comptypes (vistype, type))
3482 : 624 : thistype = composite_type (vistype, type);
3483 : : else
3484 : 37 : thistype = TREE_TYPE (b->decl);
3485 : : }
3486 : : else
3487 : : thistype = type;
3488 : 3543247 : b->u.type = TREE_TYPE (b->decl);
3489 : : /* Propagate the type attributes to the decl. */
3490 : 3543247 : thistype
3491 : 3543247 : = c_build_type_attribute_variant (thistype,
3492 : 3543247 : TYPE_ATTRIBUTES (b->u.type));
3493 : 3543247 : TREE_TYPE (b->decl) = thistype;
3494 : 3543247 : bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3495 : : locus);
3496 : 3543247 : return b->decl;
3497 : : }
3498 : 46136686 : else if (TREE_PUBLIC (x))
3499 : : {
3500 : 45775909 : if (visdecl && !b && duplicate_decls (x, visdecl))
3501 : : {
3502 : : /* An external declaration at block scope referring to a
3503 : : visible entity with internal linkage. The composite
3504 : : type will already be correct for this scope, so we
3505 : : just need to fall through to make the declaration in
3506 : : this scope. */
3507 : : nested = true;
3508 : : x = visdecl;
3509 : : }
3510 : : else
3511 : : {
3512 : 45775792 : bind (name, x, external_scope, /*invisible=*/true,
3513 : : /*nested=*/false, locus);
3514 : 45775792 : nested = true;
3515 : : }
3516 : : }
3517 : : }
3518 : :
3519 : 185637045 : if (TREE_CODE (x) != PARM_DECL)
3520 : 68953905 : warn_if_shadowing (x);
3521 : :
3522 : 116683140 : skip_external_and_shadow_checks:
3523 : 185637408 : if (TREE_CODE (x) == TYPE_DECL)
3524 : : {
3525 : : /* So this is a typedef, set its underlying type. */
3526 : 9653210 : set_underlying_type (x);
3527 : :
3528 : : /* If X is a typedef defined in the current function, record it
3529 : : for the purpose of implementing the -Wunused-local-typedefs
3530 : : warning. */
3531 : 9653210 : record_locally_defined_typedef (x);
3532 : : }
3533 : :
3534 : 185637408 : bind (name, x, scope, /*invisible=*/false, nested, locus);
3535 : :
3536 : : /* If x's type is incomplete because it's based on a
3537 : : structure or union which has not yet been fully declared,
3538 : : attach it to that structure or union type, so we can go
3539 : : back and complete the variable declaration later, if the
3540 : : structure or union gets fully declared.
3541 : :
3542 : : If the input is erroneous, we can have error_mark in the type
3543 : : slot (e.g. "f(void a, ...)") - that doesn't count as an
3544 : : incomplete type. */
3545 : 185637408 : if (TREE_TYPE (x) != error_mark_node
3546 : 185637408 : && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3547 : : {
3548 : 183315 : tree element = TREE_TYPE (x);
3549 : :
3550 : 203133 : while (TREE_CODE (element) == ARRAY_TYPE)
3551 : 19818 : element = TREE_TYPE (element);
3552 : 183315 : element = TYPE_MAIN_VARIANT (element);
3553 : :
3554 : 183315 : if ((RECORD_OR_UNION_TYPE_P (element)
3555 : 140475 : || TREE_CODE (element) == ENUMERAL_TYPE)
3556 : 42911 : && (TREE_CODE (x) != TYPE_DECL
3557 : 39509 : || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3558 : 186725 : && !COMPLETE_TYPE_P (element))
3559 : 267 : C_TYPE_INCOMPLETE_VARS (element)
3560 : 534 : = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3561 : : }
3562 : : return x;
3563 : : }
3564 : :
3565 : :
3566 : : /* Issue a permerror about implicit function declaration. ID is the function
3567 : : identifier, OLDDECL is a declaration of the function in a different scope,
3568 : : or NULL_TREE. */
3569 : :
3570 : : static void
3571 : 3776 : implicit_decl_permerror (location_t loc, tree id, tree olddecl)
3572 : : {
3573 : 3776 : if (!warn_implicit_function_declaration)
3574 : 2617 : return;
3575 : :
3576 : 1159 : bool warned;
3577 : 1159 : auto_diagnostic_group d;
3578 : 1159 : name_hint hint;
3579 : 1159 : if (!olddecl)
3580 : 553 : hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3581 : :
3582 : 1159 : if (flag_isoc99)
3583 : : {
3584 : 1153 : if (const char *suggestion = hint.suggestion ())
3585 : : {
3586 : 107 : gcc_rich_location richloc (loc);
3587 : 107 : richloc.add_fixit_replace (suggestion);
3588 : 107 : warned = permerror_opt (&richloc, OPT_Wimplicit_function_declaration,
3589 : : "implicit declaration of function %qE;"
3590 : : " did you mean %qs?",
3591 : : id, suggestion);
3592 : 107 : }
3593 : : else
3594 : 1046 : warned = permerror_opt (loc, OPT_Wimplicit_function_declaration,
3595 : : "implicit declaration of function %qE", id);
3596 : : }
3597 : 6 : else if (const char *suggestion = hint.suggestion ())
3598 : : {
3599 : 2 : gcc_rich_location richloc (loc);
3600 : 2 : richloc.add_fixit_replace (suggestion);
3601 : 2 : warned = warning_at
3602 : 2 : (&richloc, OPT_Wimplicit_function_declaration,
3603 : : G_("implicit declaration of function %qE; did you mean %qs?"),
3604 : : id, suggestion);
3605 : 2 : }
3606 : : else
3607 : 4 : warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3608 : : G_("implicit declaration of function %qE"), id);
3609 : :
3610 : 1159 : if (warned)
3611 : : {
3612 : : /* Whether the olddecl is an undeclared builtin function.
3613 : : locate_old_decl will not generate a diagnostic for those,
3614 : : so in that case we want to look elsewhere. */
3615 : 90 : bool undeclared_builtin = (olddecl
3616 : 28 : && TREE_CODE (olddecl) == FUNCTION_DECL
3617 : 28 : && fndecl_built_in_p (olddecl)
3618 : 117 : && !C_DECL_DECLARED_BUILTIN (olddecl));
3619 : 90 : if (undeclared_builtin)
3620 : : {
3621 : 27 : const char *header = header_for_builtin_fn (olddecl);
3622 : 27 : if (header)
3623 : : {
3624 : 20 : rich_location richloc (line_table, loc);
3625 : 20 : maybe_add_include_fixit (&richloc, header, true);
3626 : 20 : inform (&richloc,
3627 : : "include %qs or provide a declaration of %qE",
3628 : : header, id);
3629 : 20 : }
3630 : : }
3631 : 63 : else if (olddecl)
3632 : 1 : locate_old_decl (olddecl);
3633 : : }
3634 : :
3635 : 1069 : if (!warned)
3636 : 1159 : hint.suppress ();
3637 : 1159 : }
3638 : :
3639 : : /* Return the name of the header file that declares built-in function
3640 : : FNDECL, or null if either we don't know or don't expect to see an
3641 : : explicit declaration. */
3642 : :
3643 : : static const char *
3644 : 3178 : header_for_builtin_fn (tree fndecl)
3645 : : {
3646 : 3178 : if (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3647 : : return NULL;
3648 : :
3649 : 3178 : switch (DECL_FUNCTION_CODE (fndecl))
3650 : : {
3651 : : CASE_FLT_FN (BUILT_IN_ACOS):
3652 : : CASE_FLT_FN (BUILT_IN_ACOSH):
3653 : : CASE_FLT_FN (BUILT_IN_ASIN):
3654 : : CASE_FLT_FN (BUILT_IN_ASINH):
3655 : : CASE_FLT_FN (BUILT_IN_ATAN):
3656 : : CASE_FLT_FN (BUILT_IN_ATANH):
3657 : : CASE_FLT_FN (BUILT_IN_ATAN2):
3658 : : CASE_FLT_FN (BUILT_IN_CBRT):
3659 : : CASE_FLT_FN (BUILT_IN_CEIL):
3660 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3661 : : CASE_FLT_FN (BUILT_IN_COPYSIGN):
3662 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3663 : : CASE_FLT_FN (BUILT_IN_COS):
3664 : : CASE_FLT_FN (BUILT_IN_COSH):
3665 : : CASE_FLT_FN (BUILT_IN_ERF):
3666 : : CASE_FLT_FN (BUILT_IN_ERFC):
3667 : : CASE_FLT_FN (BUILT_IN_EXP):
3668 : : CASE_FLT_FN (BUILT_IN_EXP2):
3669 : : CASE_FLT_FN (BUILT_IN_EXPM1):
3670 : : CASE_FLT_FN (BUILT_IN_FABS):
3671 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3672 : : CASE_FLT_FN (BUILT_IN_FDIM):
3673 : : CASE_FLT_FN (BUILT_IN_FLOOR):
3674 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3675 : : CASE_FLT_FN (BUILT_IN_FMA):
3676 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3677 : : CASE_FLT_FN (BUILT_IN_FMAX):
3678 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3679 : : CASE_FLT_FN (BUILT_IN_FMIN):
3680 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3681 : : CASE_FLT_FN (BUILT_IN_FMOD):
3682 : : CASE_FLT_FN (BUILT_IN_FREXP):
3683 : : CASE_FLT_FN (BUILT_IN_HYPOT):
3684 : : CASE_FLT_FN (BUILT_IN_ILOGB):
3685 : : CASE_FLT_FN (BUILT_IN_LDEXP):
3686 : : CASE_FLT_FN (BUILT_IN_LGAMMA):
3687 : : CASE_FLT_FN (BUILT_IN_LLRINT):
3688 : : CASE_FLT_FN (BUILT_IN_LLROUND):
3689 : : CASE_FLT_FN (BUILT_IN_LOG):
3690 : : CASE_FLT_FN (BUILT_IN_LOG10):
3691 : : CASE_FLT_FN (BUILT_IN_LOG1P):
3692 : : CASE_FLT_FN (BUILT_IN_LOG2):
3693 : : CASE_FLT_FN (BUILT_IN_LOGB):
3694 : : CASE_FLT_FN (BUILT_IN_LRINT):
3695 : : CASE_FLT_FN (BUILT_IN_LROUND):
3696 : : CASE_FLT_FN (BUILT_IN_MODF):
3697 : : CASE_FLT_FN (BUILT_IN_NAN):
3698 : : CASE_FLT_FN (BUILT_IN_NEARBYINT):
3699 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3700 : : CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3701 : : CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3702 : : CASE_FLT_FN (BUILT_IN_POW):
3703 : : CASE_FLT_FN (BUILT_IN_REMAINDER):
3704 : : CASE_FLT_FN (BUILT_IN_REMQUO):
3705 : : CASE_FLT_FN (BUILT_IN_RINT):
3706 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3707 : : CASE_FLT_FN (BUILT_IN_ROUND):
3708 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3709 : : CASE_FLT_FN (BUILT_IN_SCALBLN):
3710 : : CASE_FLT_FN (BUILT_IN_SCALBN):
3711 : : CASE_FLT_FN (BUILT_IN_SIN):
3712 : : CASE_FLT_FN (BUILT_IN_SINH):
3713 : : CASE_FLT_FN (BUILT_IN_SINCOS):
3714 : : CASE_FLT_FN (BUILT_IN_SQRT):
3715 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3716 : : CASE_FLT_FN (BUILT_IN_TAN):
3717 : : CASE_FLT_FN (BUILT_IN_TANH):
3718 : : CASE_FLT_FN (BUILT_IN_TGAMMA):
3719 : : CASE_FLT_FN (BUILT_IN_TRUNC):
3720 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3721 : : case BUILT_IN_ISINF:
3722 : : case BUILT_IN_ISNAN:
3723 : : return "<math.h>";
3724 : 29 : CASE_FLT_FN (BUILT_IN_CABS):
3725 : 29 : CASE_FLT_FN (BUILT_IN_CACOS):
3726 : 29 : CASE_FLT_FN (BUILT_IN_CACOSH):
3727 : 29 : CASE_FLT_FN (BUILT_IN_CARG):
3728 : 29 : CASE_FLT_FN (BUILT_IN_CASIN):
3729 : 29 : CASE_FLT_FN (BUILT_IN_CASINH):
3730 : 29 : CASE_FLT_FN (BUILT_IN_CATAN):
3731 : 29 : CASE_FLT_FN (BUILT_IN_CATANH):
3732 : 29 : CASE_FLT_FN (BUILT_IN_CCOS):
3733 : 29 : CASE_FLT_FN (BUILT_IN_CCOSH):
3734 : 29 : CASE_FLT_FN (BUILT_IN_CEXP):
3735 : 29 : CASE_FLT_FN (BUILT_IN_CIMAG):
3736 : 29 : CASE_FLT_FN (BUILT_IN_CLOG):
3737 : 29 : CASE_FLT_FN (BUILT_IN_CONJ):
3738 : 29 : CASE_FLT_FN (BUILT_IN_CPOW):
3739 : 29 : CASE_FLT_FN (BUILT_IN_CPROJ):
3740 : 29 : CASE_FLT_FN (BUILT_IN_CREAL):
3741 : 29 : CASE_FLT_FN (BUILT_IN_CSIN):
3742 : 29 : CASE_FLT_FN (BUILT_IN_CSINH):
3743 : 29 : CASE_FLT_FN (BUILT_IN_CSQRT):
3744 : 29 : CASE_FLT_FN (BUILT_IN_CTAN):
3745 : 29 : CASE_FLT_FN (BUILT_IN_CTANH):
3746 : 29 : return "<complex.h>";
3747 : 228 : case BUILT_IN_MEMCHR:
3748 : 228 : case BUILT_IN_MEMCMP:
3749 : 228 : case BUILT_IN_MEMCPY:
3750 : 228 : case BUILT_IN_MEMMOVE:
3751 : 228 : case BUILT_IN_MEMSET:
3752 : 228 : case BUILT_IN_STRCAT:
3753 : 228 : case BUILT_IN_STRCHR:
3754 : 228 : case BUILT_IN_STRCMP:
3755 : 228 : case BUILT_IN_STRCPY:
3756 : 228 : case BUILT_IN_STRCSPN:
3757 : 228 : case BUILT_IN_STRLEN:
3758 : 228 : case BUILT_IN_STRNCAT:
3759 : 228 : case BUILT_IN_STRNCMP:
3760 : 228 : case BUILT_IN_STRNCPY:
3761 : 228 : case BUILT_IN_STRPBRK:
3762 : 228 : case BUILT_IN_STRRCHR:
3763 : 228 : case BUILT_IN_STRSPN:
3764 : 228 : case BUILT_IN_STRSTR:
3765 : 228 : return "<string.h>";
3766 : 544 : case BUILT_IN_FPRINTF:
3767 : 544 : case BUILT_IN_PUTC:
3768 : 544 : case BUILT_IN_FPUTC:
3769 : 544 : case BUILT_IN_FPUTS:
3770 : 544 : case BUILT_IN_FSCANF:
3771 : 544 : case BUILT_IN_FWRITE:
3772 : 544 : case BUILT_IN_PRINTF:
3773 : 544 : case BUILT_IN_PUTCHAR:
3774 : 544 : case BUILT_IN_PUTS:
3775 : 544 : case BUILT_IN_SCANF:
3776 : 544 : case BUILT_IN_SNPRINTF:
3777 : 544 : case BUILT_IN_SPRINTF:
3778 : 544 : case BUILT_IN_SSCANF:
3779 : 544 : case BUILT_IN_VFPRINTF:
3780 : 544 : case BUILT_IN_VFSCANF:
3781 : 544 : case BUILT_IN_VPRINTF:
3782 : 544 : case BUILT_IN_VSCANF:
3783 : 544 : case BUILT_IN_VSNPRINTF:
3784 : 544 : case BUILT_IN_VSPRINTF:
3785 : 544 : case BUILT_IN_VSSCANF:
3786 : 544 : return "<stdio.h>";
3787 : 2 : case BUILT_IN_ISALNUM:
3788 : 2 : case BUILT_IN_ISALPHA:
3789 : 2 : case BUILT_IN_ISBLANK:
3790 : 2 : case BUILT_IN_ISCNTRL:
3791 : 2 : case BUILT_IN_ISDIGIT:
3792 : 2 : case BUILT_IN_ISGRAPH:
3793 : 2 : case BUILT_IN_ISLOWER:
3794 : 2 : case BUILT_IN_ISPRINT:
3795 : 2 : case BUILT_IN_ISPUNCT:
3796 : 2 : case BUILT_IN_ISSPACE:
3797 : 2 : case BUILT_IN_ISUPPER:
3798 : 2 : case BUILT_IN_ISXDIGIT:
3799 : 2 : case BUILT_IN_TOLOWER:
3800 : 2 : case BUILT_IN_TOUPPER:
3801 : 2 : return "<ctype.h>";
3802 : 0 : case BUILT_IN_ISWALNUM:
3803 : 0 : case BUILT_IN_ISWALPHA:
3804 : 0 : case BUILT_IN_ISWBLANK:
3805 : 0 : case BUILT_IN_ISWCNTRL:
3806 : 0 : case BUILT_IN_ISWDIGIT:
3807 : 0 : case BUILT_IN_ISWGRAPH:
3808 : 0 : case BUILT_IN_ISWLOWER:
3809 : 0 : case BUILT_IN_ISWPRINT:
3810 : 0 : case BUILT_IN_ISWPUNCT:
3811 : 0 : case BUILT_IN_ISWSPACE:
3812 : 0 : case BUILT_IN_ISWUPPER:
3813 : 0 : case BUILT_IN_ISWXDIGIT:
3814 : 0 : case BUILT_IN_TOWLOWER:
3815 : 0 : case BUILT_IN_TOWUPPER:
3816 : 0 : return "<wctype.h>";
3817 : 1818 : case BUILT_IN_ABORT:
3818 : 1818 : case BUILT_IN_ABS:
3819 : 1818 : case BUILT_IN_CALLOC:
3820 : 1818 : case BUILT_IN_EXIT:
3821 : 1818 : case BUILT_IN_FREE:
3822 : 1818 : case BUILT_IN_LABS:
3823 : 1818 : case BUILT_IN_LLABS:
3824 : 1818 : case BUILT_IN_MALLOC:
3825 : 1818 : case BUILT_IN_REALLOC:
3826 : 1818 : case BUILT_IN__EXIT2:
3827 : 1818 : case BUILT_IN_ALIGNED_ALLOC:
3828 : 1818 : return "<stdlib.h>";
3829 : 1 : case BUILT_IN_IMAXABS:
3830 : 1 : return "<inttypes.h>";
3831 : 3 : case BUILT_IN_STRFTIME:
3832 : 3 : return "<time.h>";
3833 : : default:
3834 : : return NULL;
3835 : : }
3836 : : }
3837 : :
3838 : : /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3839 : : function of type int (). */
3840 : :
3841 : : tree
3842 : 4729 : implicitly_declare (location_t loc, tree functionid)
3843 : : {
3844 : 4729 : struct c_binding *b;
3845 : 4729 : tree decl = NULL_TREE;
3846 : 4729 : tree asmspec_tree;
3847 : :
3848 : 4743 : for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3849 : : {
3850 : 3158 : if (B_IN_SCOPE (b, external_scope))
3851 : : {
3852 : 3144 : decl = b->decl;
3853 : 3144 : break;
3854 : : }
3855 : : }
3856 : :
3857 : 4729 : if (decl)
3858 : : {
3859 : 3144 : if (TREE_CODE (decl) != FUNCTION_DECL)
3860 : : return decl;
3861 : :
3862 : : /* FIXME: Objective-C has weird not-really-builtin functions
3863 : : which are supposed to be visible automatically. They wind up
3864 : : in the external scope because they're pushed before the file
3865 : : scope gets created. Catch this here and rebind them into the
3866 : : file scope. */
3867 : 3137 : if (!fndecl_built_in_p (decl) && DECL_IS_UNDECLARED_BUILTIN (decl))
3868 : : {
3869 : 0 : bind (functionid, decl, file_scope,
3870 : : /*invisible=*/false, /*nested=*/true,
3871 : 0 : DECL_SOURCE_LOCATION (decl));
3872 : 0 : return decl;
3873 : : }
3874 : : else
3875 : : {
3876 : 3137 : tree newtype = default_function_type;
3877 : 3137 : if (b->u.type)
3878 : 753 : TREE_TYPE (decl) = b->u.type;
3879 : : /* Implicit declaration of a function already declared
3880 : : (somehow) in a different scope, or as a built-in.
3881 : : If this is the first time this has happened, warn;
3882 : : then recycle the old declaration but with the new type. */
3883 : 3137 : if (!C_DECL_IMPLICIT (decl))
3884 : : {
3885 : 2191 : implicit_decl_permerror (loc, functionid, decl);
3886 : 2191 : C_DECL_IMPLICIT (decl) = 1;
3887 : : }
3888 : 3137 : if (fndecl_built_in_p (decl))
3889 : : {
3890 : 2705 : newtype = c_build_type_attribute_variant (newtype,
3891 : 2705 : TYPE_ATTRIBUTES
3892 : : (TREE_TYPE (decl)));
3893 : 2705 : if (!comptypes (newtype, TREE_TYPE (decl)))
3894 : : {
3895 : 2528 : auto_diagnostic_group d;
3896 : 2528 : bool warned = warning_at (loc,
3897 : 2528 : OPT_Wbuiltin_declaration_mismatch,
3898 : : "incompatible implicit "
3899 : : "declaration of built-in "
3900 : : "function %qD", decl);
3901 : : /* See if we can hint which header to include. */
3902 : 2528 : const char *header = header_for_builtin_fn (decl);
3903 : 2528 : if (header != NULL && warned)
3904 : : {
3905 : 133 : rich_location richloc (line_table, loc);
3906 : 133 : maybe_add_include_fixit (&richloc, header, true);
3907 : 133 : inform (&richloc,
3908 : : "include %qs or provide a declaration of %qD",
3909 : : header, decl);
3910 : 133 : }
3911 : 2528 : newtype = TREE_TYPE (decl);
3912 : 2528 : }
3913 : : }
3914 : : else
3915 : : {
3916 : 432 : if (!comptypes (newtype, TREE_TYPE (decl)))
3917 : : {
3918 : 2 : auto_diagnostic_group d;
3919 : 2 : error_at (loc, "incompatible implicit declaration of "
3920 : : "function %qD", decl);
3921 : 2 : locate_old_decl (decl);
3922 : 2 : }
3923 : : }
3924 : 3137 : b->u.type = TREE_TYPE (decl);
3925 : 3137 : TREE_TYPE (decl) = newtype;
3926 : 3137 : bind (functionid, decl, current_scope,
3927 : : /*invisible=*/false, /*nested=*/true,
3928 : 3137 : DECL_SOURCE_LOCATION (decl));
3929 : 3137 : return decl;
3930 : : }
3931 : : }
3932 : :
3933 : : /* Not seen before. */
3934 : 1585 : decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3935 : 1585 : DECL_EXTERNAL (decl) = 1;
3936 : 1585 : TREE_PUBLIC (decl) = 1;
3937 : 1585 : C_DECL_IMPLICIT (decl) = 1;
3938 : 1585 : implicit_decl_permerror (loc, functionid, 0);
3939 : 1585 : asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3940 : 1585 : if (asmspec_tree)
3941 : 1 : set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3942 : :
3943 : : /* C89 says implicit declarations are in the innermost block.
3944 : : So we record the decl in the standard fashion. */
3945 : 1585 : decl = pushdecl (decl);
3946 : :
3947 : : /* No need to call objc_check_decl here - it's a function type. */
3948 : 1585 : rest_of_decl_compilation (decl, 0, 0);
3949 : :
3950 : : /* Write a record describing this implicit function declaration
3951 : : to the prototypes file (if requested). */
3952 : 1585 : gen_aux_info_record (decl, 0, 1, 0);
3953 : :
3954 : : /* Possibly apply some default attributes to this implicit declaration. */
3955 : 1585 : decl_attributes (&decl, NULL_TREE, 0);
3956 : :
3957 : 1585 : return decl;
3958 : : }
3959 : :
3960 : : /* Issue an error message for a reference to an undeclared variable
3961 : : ID, including a reference to a builtin outside of function-call
3962 : : context. Establish a binding of the identifier to error_mark_node
3963 : : in an appropriate scope, which will suppress further errors for the
3964 : : same identifier. The error message should be given location LOC. */
3965 : : void
3966 : 1435 : undeclared_variable (location_t loc, tree id)
3967 : : {
3968 : 1435 : static bool already = false;
3969 : 1435 : struct c_scope *scope;
3970 : :
3971 : 1435 : auto_diagnostic_group d;
3972 : 1435 : if (current_function_decl == NULL_TREE)
3973 : : {
3974 : 623 : name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3975 : 623 : if (const char *suggestion = guessed_id.suggestion ())
3976 : : {
3977 : 86 : gcc_rich_location richloc (loc);
3978 : 86 : richloc.add_fixit_replace (suggestion);
3979 : 86 : error_at (&richloc,
3980 : : "%qE undeclared here (not in a function);"
3981 : : " did you mean %qs?",
3982 : : id, suggestion);
3983 : 86 : }
3984 : : else
3985 : 537 : error_at (loc, "%qE undeclared here (not in a function)", id);
3986 : 623 : scope = current_scope;
3987 : 623 : }
3988 : : else
3989 : : {
3990 : 812 : if (!objc_diagnose_private_ivar (id))
3991 : : {
3992 : 812 : name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3993 : 812 : if (const char *suggestion = guessed_id.suggestion ())
3994 : : {
3995 : 36 : gcc_rich_location richloc (loc);
3996 : 36 : richloc.add_fixit_replace (suggestion);
3997 : 36 : error_at (&richloc,
3998 : : "%qE undeclared (first use in this function);"
3999 : : " did you mean %qs?",
4000 : : id, suggestion);
4001 : 36 : }
4002 : : else
4003 : 776 : error_at (loc, "%qE undeclared (first use in this function)", id);
4004 : 812 : }
4005 : 812 : if (!already)
4006 : : {
4007 : 195 : inform (loc, "each undeclared identifier is reported only"
4008 : : " once for each function it appears in");
4009 : 195 : already = true;
4010 : : }
4011 : :
4012 : : /* If we are parsing old-style parameter decls, current_function_decl
4013 : : will be nonnull but current_function_scope will be null. */
4014 : 812 : scope = current_function_scope ? current_function_scope : current_scope;
4015 : : }
4016 : 1435 : bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
4017 : : UNKNOWN_LOCATION);
4018 : 1435 : }
4019 : :
4020 : : /* Subroutine of lookup_label, declare_label, define_label: construct a
4021 : : LABEL_DECL with all the proper frills. Also create a struct
4022 : : c_label_vars initialized for the current scope. */
4023 : :
4024 : : static tree
4025 : 23636 : make_label (location_t location, tree name, bool defining,
4026 : : struct c_label_vars **p_label_vars)
4027 : : {
4028 : 23636 : tree label = build_decl (location, LABEL_DECL, name, void_type_node);
4029 : 23636 : DECL_CONTEXT (label) = current_function_decl;
4030 : 23636 : SET_DECL_MODE (label, VOIDmode);
4031 : :
4032 : 23636 : c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
4033 : 23636 : label_vars->shadowed = NULL;
4034 : 23636 : set_spot_bindings (&label_vars->label_bindings, defining);
4035 : 23636 : label_vars->decls_in_scope = make_tree_vector ();
4036 : 23636 : label_vars->gotos = NULL;
4037 : 23636 : *p_label_vars = label_vars;
4038 : :
4039 : 23636 : return label;
4040 : : }
4041 : :
4042 : : /* Get the LABEL_DECL corresponding to identifier NAME as a label.
4043 : : Create one if none exists so far for the current function.
4044 : : This is called when a label is used in a goto expression or
4045 : : has its address taken. */
4046 : :
4047 : : tree
4048 : 85070 : lookup_label (tree name)
4049 : : {
4050 : 85070 : tree label;
4051 : 85070 : struct c_label_vars *label_vars;
4052 : :
4053 : 85070 : if (current_function_scope == 0)
4054 : : {
4055 : 2 : error ("label %qE referenced outside of any function", name);
4056 : 2 : return NULL_TREE;
4057 : : }
4058 : :
4059 : : /* Use a label already defined or ref'd with this name, but not if
4060 : : it is inherited from a containing function and wasn't declared
4061 : : using __label__. */
4062 : 85068 : label = I_LABEL_DECL (name);
4063 : 79288 : if (label && (DECL_CONTEXT (label) == current_function_decl
4064 : 606 : || C_DECLARED_LABEL_FLAG (label)))
4065 : : {
4066 : : /* If the label has only been declared, update its apparent
4067 : : location to point here, for better diagnostics if it
4068 : : turns out not to have been defined. */
4069 : 79281 : if (DECL_INITIAL (label) == NULL_TREE)
4070 : 62640 : DECL_SOURCE_LOCATION (label) = input_location;
4071 : 79281 : return label;
4072 : : }
4073 : :
4074 : : /* No label binding for that identifier; make one. */
4075 : 5787 : label = make_label (input_location, name, false, &label_vars);
4076 : :
4077 : : /* Ordinary labels go in the current function scope. */
4078 : 5787 : bind_label (name, label, current_function_scope, label_vars);
4079 : :
4080 : 5787 : return label;
4081 : : }
4082 : :
4083 : : /* Issue a warning about DECL for a goto statement at GOTO_LOC going
4084 : : to LABEL. */
4085 : :
4086 : : static void
4087 : 1482 : warn_about_goto (location_t goto_loc, tree label, tree decl)
4088 : : {
4089 : 1482 : auto_diagnostic_group d;
4090 : 1482 : if (c_type_variably_modified_p (TREE_TYPE (decl)))
4091 : 1476 : error_at (goto_loc,
4092 : : "jump into scope of identifier with variably modified type");
4093 : 6 : else if (flag_openmp
4094 : 6 : && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl)))
4095 : 2 : error_at (goto_loc, "jump skips OpenMP %<allocate%> allocation");
4096 : : else
4097 : 4 : if (!warning_at (goto_loc, OPT_Wjump_misses_init,
4098 : : "jump skips variable initialization"))
4099 : 0 : return;
4100 : 1482 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4101 : 1482 : inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
4102 : 1482 : }
4103 : :
4104 : : /* Look up a label because of a goto statement. This is like
4105 : : lookup_label, but also issues any appropriate warnings. */
4106 : :
4107 : : tree
4108 : 83243 : lookup_label_for_goto (location_t loc, tree name)
4109 : : {
4110 : 83243 : tree label;
4111 : 83243 : struct c_label_vars *label_vars;
4112 : 83243 : unsigned int ix;
4113 : 83243 : tree decl;
4114 : :
4115 : 83243 : label = lookup_label (name);
4116 : 83243 : if (label == NULL_TREE)
4117 : : return NULL_TREE;
4118 : :
4119 : : /* If we are jumping to a different function, we can't issue any
4120 : : useful warnings. */
4121 : 83243 : if (DECL_CONTEXT (label) != current_function_decl)
4122 : : {
4123 : 520 : gcc_assert (C_DECLARED_LABEL_FLAG (label));
4124 : : return label;
4125 : : }
4126 : :
4127 : 82723 : label_vars = I_LABEL_BINDING (name)->u.label;
4128 : :
4129 : : /* If the label has not yet been defined, then push this goto on a
4130 : : list for possible later warnings. */
4131 : 82723 : if (label_vars->label_bindings.scope == NULL)
4132 : : {
4133 : 66307 : c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
4134 : :
4135 : 66307 : g->loc = loc;
4136 : 66307 : set_spot_bindings (&g->goto_bindings, true);
4137 : 66307 : vec_safe_push (label_vars->gotos, g);
4138 : 66307 : return label;
4139 : : }
4140 : :
4141 : : /* If there are any decls in label_vars->decls_in_scope, then this
4142 : : goto has missed the declaration of the decl. This happens for a
4143 : : case like
4144 : : int i = 1;
4145 : : lab:
4146 : : ...
4147 : : goto lab;
4148 : : Issue a warning or error. */
4149 : 17191 : FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
4150 : 775 : warn_about_goto (loc, label, decl);
4151 : :
4152 : 16416 : if (label_vars->label_bindings.left_stmt_expr)
4153 : : {
4154 : 120 : auto_diagnostic_group d;
4155 : 120 : error_at (loc, "jump into statement expression");
4156 : 120 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4157 : 120 : }
4158 : :
4159 : : return label;
4160 : : }
4161 : :
4162 : : /* Make a label named NAME in the current function, shadowing silently
4163 : : any that may be inherited from containing functions or containing
4164 : : scopes. This is called for __label__ declarations. */
4165 : :
4166 : : tree
4167 : 1181 : declare_label (tree name)
4168 : : {
4169 : 1181 : struct c_binding *b = I_LABEL_BINDING (name);
4170 : 1181 : tree label;
4171 : 1181 : struct c_label_vars *label_vars;
4172 : :
4173 : : /* Check to make sure that the label hasn't already been declared
4174 : : at this scope */
4175 : 1181 : if (b && B_IN_CURRENT_SCOPE (b))
4176 : : {
4177 : 2 : auto_diagnostic_group d;
4178 : 2 : error ("duplicate label declaration %qE", name);
4179 : 2 : locate_old_decl (b->decl);
4180 : :
4181 : : /* Just use the previous declaration. */
4182 : 2 : return b->decl;
4183 : 2 : }
4184 : :
4185 : 1179 : label = make_label (input_location, name, false, &label_vars);
4186 : 1179 : C_DECLARED_LABEL_FLAG (label) = 1;
4187 : :
4188 : : /* Declared labels go in the current scope. */
4189 : 1179 : bind_label (name, label, current_scope, label_vars);
4190 : :
4191 : 1179 : return label;
4192 : : }
4193 : :
4194 : : /* When we define a label, issue any appropriate warnings if there are
4195 : : any gotos earlier in the function which jump to this label. */
4196 : :
4197 : : static void
4198 : 6886 : check_earlier_gotos (tree label, struct c_label_vars* label_vars)
4199 : : {
4200 : 6886 : unsigned int ix;
4201 : 6886 : struct c_goto_bindings *g;
4202 : :
4203 : 73127 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
4204 : : {
4205 : 66241 : struct c_binding *b;
4206 : 66241 : struct c_scope *scope;
4207 : :
4208 : : /* We have a goto to this label. The goto is going forward. In
4209 : : g->scope, the goto is going to skip any binding which was
4210 : : defined after g->bindings_in_scope. */
4211 : 66241 : if (g->goto_bindings.scope->has_jump_unsafe_decl)
4212 : : {
4213 : 251 : for (b = g->goto_bindings.scope->bindings;
4214 : 581 : b != g->goto_bindings.bindings_in_scope;
4215 : 330 : b = b->prev)
4216 : : {
4217 : 330 : if (decl_jump_unsafe (b->decl))
4218 : 168 : warn_about_goto (g->loc, label, b->decl);
4219 : : }
4220 : : }
4221 : :
4222 : : /* We also need to warn about decls defined in any scopes
4223 : : between the scope of the label and the scope of the goto. */
4224 : 66241 : for (scope = label_vars->label_bindings.scope;
4225 : 68788 : scope != g->goto_bindings.scope;
4226 : 2547 : scope = scope->outer)
4227 : : {
4228 : 2547 : gcc_assert (scope != NULL);
4229 : 2547 : if (scope->has_jump_unsafe_decl)
4230 : : {
4231 : 325 : if (scope == label_vars->label_bindings.scope)
4232 : 249 : b = label_vars->label_bindings.bindings_in_scope;
4233 : : else
4234 : 76 : b = scope->bindings;
4235 : 864 : for (; b != NULL; b = b->prev)
4236 : : {
4237 : 539 : if (decl_jump_unsafe (b->decl))
4238 : 539 : warn_about_goto (g->loc, label, b->decl);
4239 : : }
4240 : : }
4241 : : }
4242 : :
4243 : 66241 : if (g->goto_bindings.stmt_exprs > 0)
4244 : : {
4245 : 100 : auto_diagnostic_group d;
4246 : 100 : error_at (g->loc, "jump into statement expression");
4247 : 100 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
4248 : : label);
4249 : 100 : }
4250 : : }
4251 : :
4252 : : /* Now that the label is defined, we will issue warnings about
4253 : : subsequent gotos to this label when we see them. */
4254 : 6886 : vec_safe_truncate (label_vars->gotos, 0);
4255 : 6886 : label_vars->gotos = NULL;
4256 : 6886 : }
4257 : :
4258 : : /* Define a label, specifying the location in the source file.
4259 : : Return the LABEL_DECL node for the label, if the definition is valid.
4260 : : Otherwise return NULL_TREE. */
4261 : :
4262 : : tree
4263 : 23585 : define_label (location_t location, tree name)
4264 : : {
4265 : : /* Find any preexisting label with this name. It is an error
4266 : : if that label has already been defined in this function, or
4267 : : if there is a containing function with a declared label with
4268 : : the same name. */
4269 : 23585 : tree label = I_LABEL_DECL (name);
4270 : :
4271 : 6922 : if (label
4272 : 6922 : && ((DECL_CONTEXT (label) == current_function_decl
4273 : 6908 : && DECL_INITIAL (label) != NULL_TREE)
4274 : 6900 : || (DECL_CONTEXT (label) != current_function_decl
4275 : 14 : && C_DECLARED_LABEL_FLAG (label))))
4276 : : {
4277 : 29 : auto_diagnostic_group d;
4278 : 29 : error_at (location, "duplicate label %qD", label);
4279 : 29 : locate_old_decl (label);
4280 : 29 : return NULL_TREE;
4281 : 29 : }
4282 : 23556 : else if (label && DECL_CONTEXT (label) == current_function_decl)
4283 : : {
4284 : 6886 : struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
4285 : :
4286 : : /* The label has been used or declared already in this function,
4287 : : but not defined. Update its location to point to this
4288 : : definition. */
4289 : 6886 : DECL_SOURCE_LOCATION (label) = location;
4290 : 6886 : set_spot_bindings (&label_vars->label_bindings, true);
4291 : :
4292 : : /* Issue warnings as required about any goto statements from
4293 : : earlier in the function. */
4294 : 6886 : check_earlier_gotos (label, label_vars);
4295 : : }
4296 : : else
4297 : : {
4298 : 16670 : struct c_label_vars *label_vars;
4299 : :
4300 : : /* No label binding for that identifier; make one. */
4301 : 16670 : label = make_label (location, name, true, &label_vars);
4302 : :
4303 : : /* Ordinary labels go in the current function scope. */
4304 : 16670 : bind_label (name, label, current_function_scope, label_vars);
4305 : : }
4306 : :
4307 : 23556 : if (!in_system_header_at (input_location) && lookup_name (name))
4308 : 131 : warning_at (location, OPT_Wtraditional,
4309 : : "traditional C lacks a separate namespace "
4310 : : "for labels, identifier %qE conflicts", name);
4311 : :
4312 : : /* Mark label as having been defined. */
4313 : 23556 : DECL_INITIAL (label) = error_mark_node;
4314 : 23556 : return label;
4315 : : }
4316 : :
4317 : : /* Get the bindings for a new switch statement. This is used to issue
4318 : : warnings as appropriate for jumps from the switch to case or
4319 : : default labels. */
4320 : :
4321 : : struct c_spot_bindings *
4322 : 36632 : c_get_switch_bindings (void)
4323 : : {
4324 : 36632 : struct c_spot_bindings *switch_bindings;
4325 : :
4326 : 36632 : switch_bindings = XNEW (struct c_spot_bindings);
4327 : 36632 : set_spot_bindings (switch_bindings, true);
4328 : 36632 : return switch_bindings;
4329 : : }
4330 : :
4331 : : void
4332 : 36632 : c_release_switch_bindings (struct c_spot_bindings *bindings)
4333 : : {
4334 : 36632 : gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
4335 : 36632 : XDELETE (bindings);
4336 : 36632 : }
4337 : :
4338 : : /* This is called at the point of a case or default label to issue
4339 : : warnings about decls as needed. It returns true if it found an
4340 : : error, not just a warning. */
4341 : :
4342 : : bool
4343 : 1024692 : c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
4344 : : location_t switch_loc, location_t case_loc)
4345 : : {
4346 : 1024692 : bool saw_error;
4347 : 1024692 : struct c_scope *scope;
4348 : :
4349 : 1024692 : saw_error = false;
4350 : 1024692 : for (scope = current_scope;
4351 : 3074108 : scope != switch_bindings->scope;
4352 : 2049416 : scope = scope->outer)
4353 : : {
4354 : 2049416 : struct c_binding *b;
4355 : :
4356 : 2049416 : gcc_assert (scope != NULL);
4357 : :
4358 : 2049416 : if (!scope->has_jump_unsafe_decl)
4359 : 2049405 : continue;
4360 : :
4361 : 22 : for (b = scope->bindings; b != NULL; b = b->prev)
4362 : : {
4363 : 11 : if (decl_jump_unsafe (b->decl))
4364 : : {
4365 : 11 : auto_diagnostic_group d;
4366 : 11 : bool emitted;
4367 : 11 : if (c_type_variably_modified_p (TREE_TYPE (b->decl)))
4368 : : {
4369 : 6 : saw_error = true;
4370 : 6 : error_at (case_loc,
4371 : : ("switch jumps into scope of identifier with "
4372 : : "variably modified type"));
4373 : 6 : emitted = true;
4374 : : }
4375 : 5 : else if (flag_openmp
4376 : 7 : && lookup_attribute ("omp allocate",
4377 : 2 : DECL_ATTRIBUTES (b->decl)))
4378 : : {
4379 : 2 : saw_error = true;
4380 : 2 : error_at (case_loc,
4381 : : "switch jumps over OpenMP %<allocate%> allocation");
4382 : 2 : emitted = true;
4383 : : }
4384 : : else
4385 : 3 : emitted
4386 : 3 : = warning_at (case_loc, OPT_Wjump_misses_init,
4387 : : "switch jumps over variable initialization");
4388 : 11 : if (emitted)
4389 : : {
4390 : 11 : inform (switch_loc, "switch starts here");
4391 : 11 : inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
4392 : : b->decl);
4393 : : }
4394 : 11 : }
4395 : : }
4396 : : }
4397 : :
4398 : 1024692 : if (switch_bindings->stmt_exprs > 0)
4399 : : {
4400 : 4 : saw_error = true;
4401 : 4 : auto_diagnostic_group d;
4402 : 4 : error_at (case_loc, "switch jumps into statement expression");
4403 : 4 : inform (switch_loc, "switch starts here");
4404 : 4 : }
4405 : :
4406 : 1024692 : return saw_error;
4407 : : }
4408 : :
4409 : : /* Given NAME, an IDENTIFIER_NODE,
4410 : : return the structure (or union or enum) definition for that name.
4411 : : If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4412 : : CODE says which kind of type the caller wants;
4413 : : it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4414 : : If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4415 : : location where the tag was defined.
4416 : : If the wrong kind of type is found, an error is reported. */
4417 : :
4418 : : static tree
4419 : 2046732 : lookup_tag (enum tree_code code, tree name, bool thislevel_only,
4420 : : location_t *ploc)
4421 : : {
4422 : 2046732 : struct c_binding *b = I_TAG_BINDING (name);
4423 : 2046732 : bool thislevel = false;
4424 : :
4425 : 2046732 : if (!b || !b->decl)
4426 : : return NULL_TREE;
4427 : :
4428 : : /* We only care about whether it's in this level if
4429 : : thislevel_only was set or it might be a type clash. */
4430 : 1432878 : if (thislevel_only || TREE_CODE (b->decl) != code)
4431 : : {
4432 : : /* For our purposes, a tag in the external scope is the same as
4433 : : a tag in the file scope. (Primarily relevant to Objective-C
4434 : : and its builtin structure tags, which get pushed before the
4435 : : file scope is created.) */
4436 : 459141 : if (B_IN_CURRENT_SCOPE (b)
4437 : 179 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
4438 : 1432878 : thislevel = true;
4439 : : }
4440 : :
4441 : 1432878 : if (thislevel_only && !thislevel)
4442 : : return NULL_TREE;
4443 : :
4444 : 1432720 : if (TREE_CODE (b->decl) != code)
4445 : : {
4446 : : /* Definition isn't the kind we were looking for. */
4447 : 39 : pending_invalid_xref = name;
4448 : 39 : pending_invalid_xref_location = input_location;
4449 : :
4450 : : /* If in the same binding level as a declaration as a tag
4451 : : of a different type, this must not be allowed to
4452 : : shadow that tag, so give the error immediately.
4453 : : (For example, "struct foo; union foo;" is invalid.) */
4454 : 39 : if (thislevel)
4455 : 18 : pending_xref_error ();
4456 : : }
4457 : :
4458 : 1432720 : if (ploc != NULL)
4459 : 998763 : *ploc = b->locus;
4460 : :
4461 : 1432720 : return b->decl;
4462 : : }
4463 : :
4464 : : /* Return true if a definition exists for NAME with code CODE. */
4465 : :
4466 : : bool
4467 : 387 : tag_exists_p (enum tree_code code, tree name)
4468 : : {
4469 : 387 : struct c_binding *b = I_TAG_BINDING (name);
4470 : :
4471 : 387 : if (b == NULL || b->decl == NULL_TREE)
4472 : : return false;
4473 : 18 : return TREE_CODE (b->decl) == code;
4474 : : }
4475 : :
4476 : : /* Print an error message now
4477 : : for a recent invalid struct, union or enum cross reference.
4478 : : We don't print them immediately because they are not invalid
4479 : : when used in the `struct foo;' construct for shadowing. */
4480 : :
4481 : : void
4482 : 306100578 : pending_xref_error (void)
4483 : : {
4484 : 306100578 : if (pending_invalid_xref != NULL_TREE)
4485 : 27 : error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4486 : : pending_invalid_xref);
4487 : 306100578 : pending_invalid_xref = NULL_TREE;
4488 : 306100578 : }
4489 : :
4490 : :
4491 : : /* Look up NAME in the current scope and its superiors
4492 : : in the namespace of variables, functions and typedefs.
4493 : : Return a ..._DECL node of some kind representing its definition,
4494 : : or return NULL_TREE if it is undefined. */
4495 : :
4496 : : tree
4497 : 1236380255 : lookup_name (tree name)
4498 : : {
4499 : 1236380255 : struct c_binding *b = I_SYMBOL_BINDING (name);
4500 : 1236380255 : if (b && !b->invisible)
4501 : : {
4502 : 824565134 : maybe_record_typedef_use (b->decl);
4503 : 824565134 : return b->decl;
4504 : : }
4505 : : return NULL_TREE;
4506 : : }
4507 : :
4508 : : /* Similar to `lookup_name' but look only at the indicated scope. */
4509 : :
4510 : : static tree
4511 : 128317954 : lookup_name_in_scope (tree name, struct c_scope *scope)
4512 : : {
4513 : 128317954 : struct c_binding *b;
4514 : :
4515 : 128321117 : for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4516 : 7313976 : if (B_IN_SCOPE (b, scope))
4517 : 7310813 : return b->decl;
4518 : : return NULL_TREE;
4519 : : }
4520 : :
4521 : : /* Look for the closest match for NAME within the currently valid
4522 : : scopes.
4523 : :
4524 : : This finds the identifier with the lowest Levenshtein distance to
4525 : : NAME. If there are multiple candidates with equal minimal distance,
4526 : : the first one found is returned. Scopes are searched from innermost
4527 : : outwards, and within a scope in reverse order of declaration, thus
4528 : : benefiting candidates "near" to the current scope.
4529 : :
4530 : : The function also looks for similar macro names to NAME, since a
4531 : : misspelled macro name will not be expanded, and hence looks like an
4532 : : identifier to the C frontend.
4533 : :
4534 : : It also looks for start_typename keywords, to detect "singed" vs "signed"
4535 : : typos.
4536 : :
4537 : : Use LOC for any deferred diagnostics. */
4538 : :
4539 : : name_hint
4540 : 2243 : lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4541 : : {
4542 : 2243 : gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4543 : :
4544 : : /* First, try some well-known names in the C standard library, in case
4545 : : the user forgot a #include. */
4546 : 2243 : const char *header_hint
4547 : 2243 : = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4548 : :
4549 : 2243 : if (header_hint)
4550 : 56 : return name_hint
4551 : : (nullptr,
4552 : 56 : std::make_unique<suggest_missing_header> (loc,
4553 : 112 : IDENTIFIER_POINTER (name),
4554 : 56 : header_hint));
4555 : :
4556 : : /* Next, look for exact matches for builtin defines that would have been
4557 : : defined if the user had passed a command-line option (e.g. -fopenmp
4558 : : for "_OPENMP"). */
4559 : 2187 : diagnostics::option_id option_id
4560 : 2187 : = get_option_for_builtin_define (IDENTIFIER_POINTER (name));
4561 : 2187 : if (option_id.m_idx > 0)
4562 : 2 : return name_hint
4563 : : (nullptr,
4564 : 2 : std::make_unique<suggest_missing_option> (loc,
4565 : 4 : IDENTIFIER_POINTER (name),
4566 : 2 : option_id));
4567 : :
4568 : : /* Only suggest names reserved for the implementation if NAME begins
4569 : : with an underscore. */
4570 : 2185 : bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4571 : :
4572 : 2185 : best_match<tree, tree> bm (name);
4573 : :
4574 : : /* Look within currently valid scopes. */
4575 : 9648 : for (c_scope *scope = current_scope; scope; scope = scope->outer)
4576 : 12247208 : for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4577 : : {
4578 : 12239745 : if (!binding->id || binding->invisible)
4579 : 6948331 : continue;
4580 : 5291414 : if (binding->decl == error_mark_node)
4581 : 358 : continue;
4582 : : /* Don't use bindings from implicitly declared functions,
4583 : : as they were likely misspellings themselves. */
4584 : 5291056 : if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4585 : 4878318 : if (C_DECL_IMPLICIT (binding->decl))
4586 : 229 : continue;
4587 : : /* Don't suggest names that are reserved for use by the
4588 : : implementation, unless NAME began with an underscore. */
4589 : 5290827 : if (!consider_implementation_names)
4590 : : {
4591 : 2322154 : const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4592 : 2322154 : if (name_reserved_for_implementation_p (suggestion_str))
4593 : 2264333 : continue;
4594 : : }
4595 : 3026494 : switch (kind)
4596 : : {
4597 : 30970 : case FUZZY_LOOKUP_TYPENAME:
4598 : 30970 : if (TREE_CODE (binding->decl) != TYPE_DECL)
4599 : 23515 : continue;
4600 : : break;
4601 : :
4602 : 128872 : case FUZZY_LOOKUP_FUNCTION_NAME:
4603 : 128872 : if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4604 : : {
4605 : : /* Allow function pointers. */
4606 : 26502 : if ((VAR_P (binding->decl)
4607 : 21977 : || TREE_CODE (binding->decl) == PARM_DECL)
4608 : 5400 : && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4609 : 27782 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4610 : : == FUNCTION_TYPE))
4611 : : break;
4612 : 26483 : continue;
4613 : : }
4614 : : break;
4615 : :
4616 : : default:
4617 : : break;
4618 : : }
4619 : 2976496 : bm.consider (binding->id);
4620 : : }
4621 : :
4622 : : /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4623 : : as:
4624 : : x = SOME_OTHER_MACRO (y);
4625 : : then "SOME_OTHER_MACRO" will survive to the frontend and show up
4626 : : as a misspelled identifier.
4627 : :
4628 : : Use the best distance so far so that a candidate is only set if
4629 : : a macro is better than anything so far. This allows early rejection
4630 : : (without calculating the edit distance) of macro names that must have
4631 : : distance >= bm.get_best_distance (), and means that we only get a
4632 : : non-NULL result for best_macro_match if it's better than any of
4633 : : the identifiers already checked, which avoids needless creation
4634 : : of identifiers for macro hashnodes. */
4635 : 2185 : best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4636 : 2185 : cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4637 : : /* If a macro is the closest so far to NAME, use it, creating an
4638 : : identifier tree node for it. */
4639 : 2185 : if (best_macro)
4640 : : {
4641 : 11 : const char *id = (const char *)best_macro->ident.str;
4642 : 11 : tree macro_as_identifier
4643 : 11 : = get_identifier_with_length (id, best_macro->ident.len);
4644 : 11 : bm.set_best_so_far (macro_as_identifier,
4645 : : bmm.get_best_distance (),
4646 : : bmm.get_best_candidate_length ());
4647 : : }
4648 : :
4649 : : /* Try the "start_typename" keywords to detect
4650 : : "singed" vs "signed" typos. */
4651 : 2185 : if (kind == FUZZY_LOOKUP_TYPENAME)
4652 : : {
4653 : 55418 : for (unsigned i = 0; i < num_c_common_reswords; i++)
4654 : : {
4655 : 55176 : const c_common_resword *resword = &c_common_reswords[i];
4656 : 55176 : if (!c_keyword_starts_typename (resword->rid))
4657 : 41866 : continue;
4658 : 13310 : tree resword_identifier = ridpointers [resword->rid];
4659 : 13310 : if (!resword_identifier)
4660 : 30 : continue;
4661 : 13280 : gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4662 : 13280 : bm.consider (resword_identifier);
4663 : : }
4664 : : }
4665 : :
4666 : 2185 : tree best = bm.get_best_meaningful_candidate ();
4667 : 2185 : if (best)
4668 : 275 : return name_hint (IDENTIFIER_POINTER (best), NULL);
4669 : : else
4670 : 1910 : return name_hint (NULL, NULL);
4671 : : }
4672 : :
4673 : :
4674 : : /* Handle the standard [[nodiscard]] attribute. */
4675 : :
4676 : : static tree
4677 : 34 : handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4678 : : int /*flags*/, bool *no_add_attrs)
4679 : : {
4680 : 34 : if (TREE_CODE (*node) == FUNCTION_DECL)
4681 : : {
4682 : 18 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4683 : 1 : warning_at (DECL_SOURCE_LOCATION (*node),
4684 : 1 : OPT_Wattributes, "%qE attribute applied to %qD with void "
4685 : : "return type", name, *node);
4686 : : }
4687 : 16 : else if (RECORD_OR_UNION_TYPE_P (*node)
4688 : 12 : || TREE_CODE (*node) == ENUMERAL_TYPE)
4689 : : /* OK */;
4690 : : else
4691 : : {
4692 : 10 : pedwarn (input_location,
4693 : 10 : OPT_Wattributes, "%qE attribute can only be applied to "
4694 : : "functions or to structure, union or enumeration types", name);
4695 : 10 : *no_add_attrs = true;
4696 : : }
4697 : 34 : return NULL_TREE;
4698 : : }
4699 : :
4700 : : /* Handle the standard [[noreturn]] attribute. */
4701 : :
4702 : : static tree
4703 : 44 : handle_std_noreturn_attribute (tree *node, tree name, tree args,
4704 : : int flags, bool *no_add_attrs)
4705 : : {
4706 : : /* Unlike GNU __attribute__ ((noreturn)), the standard [[noreturn]]
4707 : : only applies to functions, not function pointers. */
4708 : 44 : if (TREE_CODE (*node) == FUNCTION_DECL)
4709 : 22 : return handle_noreturn_attribute (node, name, args, flags, no_add_attrs);
4710 : : else
4711 : : {
4712 : 22 : pedwarn (input_location, OPT_Wattributes,
4713 : : "standard %qE attribute can only be applied to functions",
4714 : : name);
4715 : 22 : *no_add_attrs = true;
4716 : 22 : return NULL_TREE;
4717 : : }
4718 : : }
4719 : :
4720 : : /* Handle the standard [[unsequenced]] attribute. */
4721 : :
4722 : : static tree
4723 : 68 : handle_std_unsequenced_attribute (tree *node, tree name, tree args,
4724 : : int flags, bool *no_add_attrs)
4725 : : {
4726 : : /* Unlike GNU __attribute__ ((unsequenced)), the standard [[unsequenced]]
4727 : : should be only applied to function declarators or type specifiers which
4728 : : have function type. */
4729 : 68 : if (node[2])
4730 : : {
4731 : 8 : auto_diagnostic_group d;
4732 : 8 : if (pedwarn (input_location, OPT_Wattributes,
4733 : : "standard %qE attribute can only be applied to function "
4734 : : "declarators or type specifiers with function type", name))
4735 : 8 : inform (input_location, "did you mean to specify it after %<)%> "
4736 : : "following function parameters?");
4737 : 8 : *no_add_attrs = true;
4738 : 8 : return NULL_TREE;
4739 : 8 : }
4740 : 60 : return handle_unsequenced_attribute (node, name, args, flags, no_add_attrs);
4741 : : }
4742 : :
4743 : : /* Handle the standard [[reproducible]] attribute. */
4744 : :
4745 : : static tree
4746 : 34 : handle_std_reproducible_attribute (tree *node, tree name, tree args,
4747 : : int flags, bool *no_add_attrs)
4748 : : {
4749 : 34 : return handle_std_unsequenced_attribute (node, name, args, flags,
4750 : 34 : no_add_attrs);
4751 : : }
4752 : :
4753 : : /* Table of supported standard (C23) attributes. */
4754 : : static const attribute_spec std_attributes[] =
4755 : : {
4756 : : /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4757 : : affects_type_identity, handler, exclude } */
4758 : : { "_Noreturn", 0, 0, false, false, false, false,
4759 : : handle_std_noreturn_attribute, NULL },
4760 : : { "deprecated", 0, 1, false, false, false, false,
4761 : : handle_deprecated_attribute, NULL },
4762 : : { "fallthrough", 0, 0, false, false, false, false,
4763 : : handle_fallthrough_attribute, NULL },
4764 : : { "maybe_unused", 0, 0, false, false, false, false,
4765 : : handle_unused_attribute, NULL },
4766 : : { "nodiscard", 0, 1, false, false, false, false,
4767 : : handle_nodiscard_attribute, NULL },
4768 : : { "noreturn", 0, 0, false, false, false, false,
4769 : : handle_std_noreturn_attribute, NULL },
4770 : : { "reproducible", 0, 0, false, true, true, false,
4771 : : handle_std_reproducible_attribute, NULL },
4772 : : { "unsequenced", 0, 0, false, true, true, false,
4773 : : handle_std_unsequenced_attribute, NULL }
4774 : : };
4775 : :
4776 : : const scoped_attribute_specs std_attribute_table =
4777 : : {
4778 : : nullptr, { std_attributes }
4779 : : };
4780 : :
4781 : : /* Create the predefined scalar types of C,
4782 : : and some nodes representing standard constants (0, 1, (void *) 0).
4783 : : Initialize the global scope.
4784 : : Make definitions for built-in primitive functions. */
4785 : :
4786 : : void
4787 : 112061 : c_init_decl_processing (void)
4788 : : {
4789 : 112061 : location_t save_loc = input_location;
4790 : :
4791 : : /* Initialize reserved words for parser. */
4792 : 112061 : c_parse_init ();
4793 : :
4794 : 112061 : current_function_decl = NULL_TREE;
4795 : :
4796 : 112061 : gcc_obstack_init (&parser_obstack);
4797 : :
4798 : : /* Make the externals scope. */
4799 : 112061 : push_scope ();
4800 : 112061 : external_scope = current_scope;
4801 : :
4802 : : /* Declarations from c_common_nodes_and_builtins must not be associated
4803 : : with this input file, lest we get differences between using and not
4804 : : using preprocessed headers. */
4805 : 112061 : input_location = BUILTINS_LOCATION;
4806 : :
4807 : 112061 : c_common_nodes_and_builtins ();
4808 : :
4809 : : /* In C, comparisons and TRUTH_* expressions have type int. */
4810 : 112061 : truthvalue_type_node = integer_type_node;
4811 : 112061 : truthvalue_true_node = integer_one_node;
4812 : 112061 : truthvalue_false_node = integer_zero_node;
4813 : :
4814 : : /* Even in C99, which has a real boolean type. */
4815 : 112061 : pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4816 : : boolean_type_node));
4817 : :
4818 : : /* C-specific nullptr initialization. */
4819 : 112061 : record_builtin_type (RID_MAX, "typeof (nullptr)", nullptr_type_node);
4820 : : /* The size and alignment of nullptr_t is the same as for a pointer to
4821 : : character type. */
4822 : 112061 : SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4823 : :
4824 : 112061 : input_location = save_loc;
4825 : :
4826 : 112061 : make_fname_decl = c_make_fname_decl;
4827 : 112061 : start_fname_decls ();
4828 : :
4829 : 112061 : if (warn_keyword_macro)
4830 : : {
4831 : 1145 : for (unsigned int i = 0; i < num_c_common_reswords; ++i)
4832 : : /* For C register keywords which don't start with underscore
4833 : : or start with just single underscore. Don't complain about
4834 : : ObjC or Transactional Memory keywords. */
4835 : 1140 : if (c_common_reswords[i].word[0] == '_'
4836 : 515 : && c_common_reswords[i].word[1] == '_')
4837 : 385 : continue;
4838 : 980 : else if (c_common_reswords[i].disable
4839 : 755 : & (D_TRANSMEM | D_OBJC | D_CXX_OBJC))
4840 : 225 : continue;
4841 : : else
4842 : : {
4843 : 530 : tree id = get_identifier (c_common_reswords[i].word);
4844 : 530 : if (C_IS_RESERVED_WORD (id)
4845 : 530 : && C_RID_CODE (id) != RID_CXX_COMPAT_WARN)
4846 : 322 : cpp_warn (parse_in, IDENTIFIER_POINTER (id),
4847 : 322 : IDENTIFIER_LENGTH (id));
4848 : : }
4849 : : }
4850 : 112061 : }
4851 : :
4852 : : /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4853 : : give the decl, NAME is the initialization string and TYPE_DEP
4854 : : indicates whether NAME depended on the type of the function. As we
4855 : : don't yet implement delayed emission of static data, we mark the
4856 : : decl as emitted so it is not placed in the output. Anything using
4857 : : it must therefore pull out the STRING_CST initializer directly.
4858 : : FIXME. */
4859 : :
4860 : : static tree
4861 : 2861 : c_make_fname_decl (location_t loc, tree id, int type_dep)
4862 : : {
4863 : 2861 : const char *name = fname_as_string (type_dep);
4864 : 2861 : tree decl, type, init;
4865 : 2861 : size_t length = strlen (name);
4866 : :
4867 : 2861 : type = c_build_array_type (char_type_node,
4868 : 2861 : build_index_type (size_int (length)));
4869 : 2861 : type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4870 : :
4871 : 2861 : decl = build_decl (loc, VAR_DECL, id, type);
4872 : :
4873 : 2861 : TREE_STATIC (decl) = 1;
4874 : 2861 : TREE_READONLY (decl) = 1;
4875 : 2861 : DECL_ARTIFICIAL (decl) = 1;
4876 : :
4877 : 2861 : init = build_string (length + 1, name);
4878 : 2861 : free (CONST_CAST (char *, name));
4879 : 2861 : TREE_TYPE (init) = type;
4880 : 2861 : DECL_INITIAL (decl) = init;
4881 : :
4882 : 2861 : TREE_USED (decl) = 1;
4883 : :
4884 : 2861 : if (current_function_decl
4885 : : /* For invalid programs like this:
4886 : :
4887 : : void foo()
4888 : : const char* p = __FUNCTION__;
4889 : :
4890 : : the __FUNCTION__ is believed to appear in K&R style function
4891 : : parameter declarator. In that case we still don't have
4892 : : function_scope. */
4893 : 2854 : && current_function_scope)
4894 : : {
4895 : 2846 : DECL_CONTEXT (decl) = current_function_decl;
4896 : 2846 : bind (id, decl, current_function_scope,
4897 : : /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4898 : : }
4899 : :
4900 : 2861 : finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4901 : :
4902 : 2861 : return decl;
4903 : : }
4904 : :
4905 : : tree
4906 : 327622730 : c_builtin_function (tree decl)
4907 : : {
4908 : 327622730 : tree type = TREE_TYPE (decl);
4909 : 327622730 : tree id = DECL_NAME (decl);
4910 : :
4911 : 327622730 : const char *name = IDENTIFIER_POINTER (id);
4912 : 327622730 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4913 : :
4914 : : /* Should never be called on a symbol with a preexisting meaning. */
4915 : 327622730 : gcc_assert (!I_SYMBOL_BINDING (id));
4916 : :
4917 : 327622730 : bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4918 : : UNKNOWN_LOCATION);
4919 : :
4920 : : /* Builtins in the implementation namespace are made visible without
4921 : : needing to be explicitly declared. See push_file_scope. */
4922 : 327622730 : if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4923 : : {
4924 : 223054299 : DECL_CHAIN (decl) = visible_builtins;
4925 : 223054299 : visible_builtins = decl;
4926 : : }
4927 : :
4928 : 327622730 : return decl;
4929 : : }
4930 : :
4931 : : tree
4932 : 10390403 : c_builtin_function_ext_scope (tree decl)
4933 : : {
4934 : 10390403 : tree type = TREE_TYPE (decl);
4935 : 10390403 : tree id = DECL_NAME (decl);
4936 : :
4937 : 10390403 : const char *name = IDENTIFIER_POINTER (id);
4938 : 10390403 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4939 : :
4940 : 10390403 : if (external_scope)
4941 : 10205309 : bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4942 : : UNKNOWN_LOCATION);
4943 : :
4944 : : /* Builtins in the implementation namespace are made visible without
4945 : : needing to be explicitly declared. See push_file_scope. */
4946 : 10390403 : if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4947 : : {
4948 : 10390403 : DECL_CHAIN (decl) = visible_builtins;
4949 : 10390403 : visible_builtins = decl;
4950 : : }
4951 : :
4952 : 10390403 : return decl;
4953 : : }
4954 : :
4955 : : /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4956 : :
4957 : : tree
4958 : 0 : c_simulate_builtin_function_decl (tree decl)
4959 : : {
4960 : 0 : tree type = TREE_TYPE (decl);
4961 : 0 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4962 : 0 : return pushdecl (decl);
4963 : : }
4964 : :
4965 : : /* Warn about attributes in a context where they are unused
4966 : : (attribute-declarations, except for the "fallthrough" case, and
4967 : : attributes on statements). */
4968 : :
4969 : : void
4970 : 40570094 : c_warn_unused_attributes (tree attrs)
4971 : : {
4972 : 40570139 : for (tree t = attrs; t != NULL_TREE; t = TREE_CHAIN (t))
4973 : 45 : if (get_attribute_namespace (t) == NULL_TREE)
4974 : : /* The specifications of standard attributes mean this is a
4975 : : constraint violation. */
4976 : 29 : pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4977 : : get_attribute_name (t));
4978 : 16 : else if (!attribute_ignored_p (t))
4979 : 12 : warning (OPT_Wattributes, "%qE attribute ignored",
4980 : : get_attribute_name (t));
4981 : 40570094 : }
4982 : :
4983 : : /* Warn for standard attributes being applied to a type that is not
4984 : : being defined, where that is a constraint violation, and return a
4985 : : list of attributes with them removed. */
4986 : :
4987 : : tree
4988 : 424985547 : c_warn_type_attributes (tree type, tree attrs)
4989 : : {
4990 : 424985547 : tree *attr_ptr = &attrs;
4991 : 425008169 : while (*attr_ptr)
4992 : 22622 : if (get_attribute_namespace (*attr_ptr) == NULL_TREE)
4993 : : {
4994 : 89 : if (TREE_CODE (type) == FUNCTION_TYPE)
4995 : : {
4996 : 65 : tree name = get_attribute_name (*attr_ptr);
4997 : : /* [[unsequenced]] and [[reproducible]] is fine on function
4998 : : types that aren't being defined. */
4999 : 65 : if (is_attribute_p ("unsequenced", name)
5000 : 65 : || is_attribute_p ("reproducible", name))
5001 : : {
5002 : 60 : attr_ptr = &TREE_CHAIN (*attr_ptr);
5003 : 60 : continue;
5004 : : }
5005 : : }
5006 : 29 : pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
5007 : : get_attribute_name (*attr_ptr));
5008 : 29 : *attr_ptr = TREE_CHAIN (*attr_ptr);
5009 : : }
5010 : : else
5011 : 22533 : attr_ptr = &TREE_CHAIN (*attr_ptr);
5012 : 424985547 : return attrs;
5013 : : }
5014 : :
5015 : : /* Called when a declaration is seen that contains no names to declare.
5016 : : If its type is a reference to a structure, union or enum inherited
5017 : : from a containing scope, shadow that tag name for the current scope
5018 : : with a forward reference.
5019 : : If its type defines a new named structure or union
5020 : : or defines an enum, it is valid but we need not do anything here.
5021 : : Otherwise, it is an error. */
5022 : :
5023 : : void
5024 : 491716 : shadow_tag (const struct c_declspecs *declspecs)
5025 : : {
5026 : 491716 : shadow_tag_warned (declspecs, 0);
5027 : 491716 : }
5028 : :
5029 : : /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
5030 : : but no pedwarn. */
5031 : : void
5032 : 491786 : shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
5033 : : {
5034 : 491786 : bool found_tag = false;
5035 : :
5036 : 491786 : if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
5037 : : {
5038 : 491672 : tree value = declspecs->type;
5039 : 491672 : enum tree_code code = TREE_CODE (value);
5040 : :
5041 : 491672 : if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
5042 : : /* Used to test also that TYPE_SIZE (value) != 0.
5043 : : That caused warning for `struct foo;' at top level in the file. */
5044 : : {
5045 : 491621 : tree name = TYPE_NAME (value);
5046 : 491621 : tree t;
5047 : :
5048 : 491621 : found_tag = true;
5049 : :
5050 : 491621 : if (declspecs->restrict_p)
5051 : : {
5052 : 2 : error ("invalid use of %<restrict%>");
5053 : 2 : warned = 1;
5054 : : }
5055 : :
5056 : 491621 : if (in_underspecified_init)
5057 : : {
5058 : : /* This can only occur with extensions such as statement
5059 : : expressions, but is still appropriate as an error to
5060 : : avoid types declared in such a context escaping to
5061 : : the type of an auto variable. */
5062 : 1 : error ("%qT declared in underspecified object initializer",
5063 : : value);
5064 : 1 : warned = 1;
5065 : : }
5066 : :
5067 : 491621 : if (name == NULL_TREE)
5068 : : {
5069 : 57653 : if (warned != 1 && code != ENUMERAL_TYPE)
5070 : : /* Empty unnamed enum OK */
5071 : : {
5072 : 19 : pedwarn (input_location, 0,
5073 : : "unnamed struct/union that defines no instances");
5074 : 19 : warned = 1;
5075 : : }
5076 : : }
5077 : 433968 : else if (declspecs->typespec_kind != ctsk_tagdef
5078 : 87122 : && declspecs->typespec_kind != ctsk_tagfirstref
5079 : 25931 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5080 : 25923 : && declspecs->storage_class != csc_none)
5081 : : {
5082 : 2 : if (warned != 1)
5083 : 2 : pedwarn (input_location, 0,
5084 : : "empty declaration with storage class specifier "
5085 : : "does not redeclare tag");
5086 : 2 : warned = 1;
5087 : 2 : pending_xref_error ();
5088 : : }
5089 : 433966 : else if (declspecs->typespec_kind != ctsk_tagdef
5090 : 87120 : && declspecs->typespec_kind != ctsk_tagfirstref
5091 : 25929 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5092 : 25921 : && (declspecs->const_p
5093 : 25918 : || declspecs->volatile_p
5094 : 25918 : || declspecs->atomic_p
5095 : 25918 : || declspecs->restrict_p
5096 : 25918 : || declspecs->address_space))
5097 : : {
5098 : 3 : if (warned != 1)
5099 : 3 : pedwarn (input_location, 0,
5100 : : "empty declaration with type qualifier "
5101 : : "does not redeclare tag");
5102 : 3 : warned = 1;
5103 : 3 : pending_xref_error ();
5104 : : }
5105 : 433963 : else if (declspecs->typespec_kind != ctsk_tagdef
5106 : 87117 : && declspecs->typespec_kind != ctsk_tagfirstref
5107 : 25926 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5108 : 25918 : && declspecs->alignas_p)
5109 : : {
5110 : 1 : if (warned != 1)
5111 : 1 : pedwarn (input_location, 0,
5112 : : "empty declaration with %<_Alignas%> "
5113 : : "does not redeclare tag");
5114 : 1 : warned = 1;
5115 : 1 : pending_xref_error ();
5116 : : }
5117 : 433962 : else if (declspecs->typespec_kind != ctsk_tagdef
5118 : 87116 : && declspecs->typespec_kind != ctsk_tagfirstref
5119 : 25925 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5120 : 25917 : && code == ENUMERAL_TYPE
5121 : 15 : && !declspecs->enum_type_specifier_ref_p)
5122 : : {
5123 : 3 : bool warned_enum = false;
5124 : 3 : if (warned != 1)
5125 : 3 : warned_enum = pedwarn (input_location, OPT_Wpedantic,
5126 : : "empty declaration of %<enum%> type "
5127 : : "does not redeclare tag");
5128 : 3 : if (warned_enum)
5129 : 2 : warned = 1;
5130 : 3 : pending_xref_error ();
5131 : 3 : }
5132 : : else
5133 : : {
5134 : 433959 : pending_invalid_xref = NULL_TREE;
5135 : 433959 : t = lookup_tag (code, name, true, NULL);
5136 : :
5137 : 433959 : if (t == NULL_TREE)
5138 : : {
5139 : 2 : t = make_node (code);
5140 : 2 : if (flag_isoc23 || code == ENUMERAL_TYPE)
5141 : 1 : SET_TYPE_STRUCTURAL_EQUALITY (t);
5142 : 2 : pushtag (input_location, name, t);
5143 : : }
5144 : : }
5145 : : }
5146 : : else
5147 : : {
5148 : 51 : if (warned != 1 && !in_system_header_at (input_location))
5149 : : {
5150 : 44 : pedwarn (input_location, 0,
5151 : : "useless type name in empty declaration");
5152 : 44 : warned = 1;
5153 : : }
5154 : : }
5155 : : }
5156 : 71 : else if (warned != 1 && !in_system_header_at (input_location)
5157 : 185 : && declspecs->typedef_p)
5158 : : {
5159 : 11 : pedwarn (input_location, 0, "useless type name in empty declaration");
5160 : 11 : warned = 1;
5161 : : }
5162 : :
5163 : 491786 : pending_invalid_xref = NULL_TREE;
5164 : :
5165 : 491786 : if (declspecs->inline_p)
5166 : : {
5167 : 5 : error ("%<inline%> in empty declaration");
5168 : 5 : warned = 1;
5169 : : }
5170 : :
5171 : 491786 : if (declspecs->noreturn_p)
5172 : : {
5173 : 2 : error ("%<_Noreturn%> in empty declaration");
5174 : 2 : warned = 1;
5175 : : }
5176 : :
5177 : 491786 : if (declspecs->constexpr_p)
5178 : : {
5179 : 7 : error ("%<constexpr%> in empty declaration");
5180 : 7 : warned = 1;
5181 : : }
5182 : :
5183 : 491786 : if (current_scope == file_scope && declspecs->storage_class == csc_auto)
5184 : : {
5185 : 2 : error ("%<auto%> in file-scope empty declaration");
5186 : 2 : warned = 1;
5187 : : }
5188 : :
5189 : 491786 : if (current_scope == file_scope && declspecs->storage_class == csc_register)
5190 : : {
5191 : 2 : error ("%<register%> in file-scope empty declaration");
5192 : 2 : warned = 1;
5193 : : }
5194 : :
5195 : 491786 : if (declspecs->enum_type_specifier_ref_p && !warned)
5196 : : {
5197 : 39 : if (declspecs->storage_class != csc_none)
5198 : : {
5199 : 1 : error ("storage class specifier in empty declaration with %<enum%> "
5200 : : "underlying type");
5201 : 1 : warned = 1;
5202 : : }
5203 : 38 : else if (declspecs->thread_p)
5204 : : {
5205 : 2 : error ("%qs in empty declaration with %<enum%> underlying type",
5206 : 1 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5207 : 1 : warned = 1;
5208 : : }
5209 : 37 : else if (declspecs->const_p
5210 : 36 : || declspecs->volatile_p
5211 : 36 : || declspecs->atomic_p
5212 : 36 : || declspecs->restrict_p
5213 : 36 : || declspecs->address_space)
5214 : : {
5215 : 1 : error ("type qualifier in empty declaration with %<enum%> "
5216 : : "underlying type");
5217 : 1 : warned = 1;
5218 : : }
5219 : 36 : else if (declspecs->alignas_p)
5220 : : {
5221 : 1 : error ("%<alignas%> in empty declaration with %<enum%> "
5222 : : "underlying type");
5223 : 1 : warned = 1;
5224 : : }
5225 : : }
5226 : :
5227 : 491618 : if (!warned && !in_system_header_at (input_location)
5228 : 608972 : && declspecs->storage_class != csc_none)
5229 : : {
5230 : 12 : warning (0, "useless storage class specifier in empty declaration");
5231 : 12 : warned = 2;
5232 : : }
5233 : :
5234 : 491786 : if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
5235 : : {
5236 : 3 : warning (0, "useless %qs in empty declaration",
5237 : 3 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5238 : 3 : warned = 2;
5239 : : }
5240 : :
5241 : 3 : if (!warned
5242 : 491599 : && !in_system_header_at (input_location)
5243 : 608962 : && (declspecs->const_p
5244 : 117171 : || declspecs->volatile_p
5245 : 117171 : || declspecs->atomic_p
5246 : 117171 : || declspecs->restrict_p
5247 : 117171 : || declspecs->address_space))
5248 : : {
5249 : 8 : warning (0, "useless type qualifier in empty declaration");
5250 : 8 : warned = 2;
5251 : : }
5252 : :
5253 : 491599 : if (!warned && !in_system_header_at (input_location)
5254 : 608949 : && declspecs->alignas_p)
5255 : : {
5256 : 4 : warning (0, "useless %<_Alignas%> in empty declaration");
5257 : 4 : warned = 2;
5258 : : }
5259 : :
5260 : 491786 : if (found_tag
5261 : 491786 : && warned == 2
5262 : 23 : && (declspecs->typespec_kind == ctsk_tagref_attrs
5263 : 23 : || declspecs->typespec_kind == ctsk_tagfirstref_attrs))
5264 : : {
5265 : : /* Standard attributes after the "struct" or "union" keyword are
5266 : : only permitted when the contents of the type are defined, or
5267 : : in the form "struct-or-union attribute-specifier-sequence
5268 : : identifier;". If the ';' was not present, attributes were
5269 : : diagnosed in the parser. Here, ensure that any other useless
5270 : : elements of the declaration result in a pedwarn, not just a
5271 : : warning. Forward declarations of enum types are not part of
5272 : : standard C, but handle them the same. */
5273 : 2 : pedwarn (input_location, 0,
5274 : : "invalid use of attributes in empty declaration");
5275 : 2 : warned = 1;
5276 : : }
5277 : :
5278 : 491786 : if (warned != 1)
5279 : : {
5280 : 491612 : if (declspecs->declspecs_seen_p
5281 : 491612 : && !declspecs->non_std_attrs_seen_p)
5282 : : /* An attribute declaration (but not a fallthrough attribute
5283 : : declaration, which was handled separately); warn if there
5284 : : are any attributes being ignored (but not if the attributes
5285 : : were empty). */
5286 : 48 : c_warn_unused_attributes (declspecs->attrs);
5287 : 491564 : else if (!found_tag)
5288 : 10 : pedwarn (input_location, 0, "empty declaration");
5289 : : }
5290 : 491786 : }
5291 : :
5292 : :
5293 : : /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
5294 : : bits. SPECS represents declaration specifiers that the grammar
5295 : : only permits to contain type qualifiers and attributes. */
5296 : :
5297 : : int
5298 : 17908223 : quals_from_declspecs (const struct c_declspecs *specs)
5299 : : {
5300 : 17908223 : int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
5301 : : | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
5302 : : | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
5303 : 17908223 : | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
5304 : 17908223 : | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
5305 : 17908223 : gcc_assert (!specs->type
5306 : : && !specs->decl_attr
5307 : : && specs->typespec_word == cts_none
5308 : : && specs->storage_class == csc_none
5309 : : && !specs->typedef_p
5310 : : && !specs->explicit_signed_p
5311 : : && !specs->deprecated_p
5312 : : && !specs->unavailable_p
5313 : : && !specs->long_p
5314 : : && !specs->long_long_p
5315 : : && !specs->short_p
5316 : : && !specs->signed_p
5317 : : && !specs->unsigned_p
5318 : : && !specs->complex_p
5319 : : && !specs->inline_p
5320 : : && !specs->noreturn_p
5321 : : && !specs->thread_p);
5322 : 17908223 : return quals;
5323 : : }
5324 : :
5325 : : /* Construct an array declarator. LOC is the location of the
5326 : : beginning of the array (usually the opening brace). EXPR is the
5327 : : expression inside [], or NULL_TREE. QUALS are the type qualifiers
5328 : : inside the [] (to be applied to the pointer to which a parameter
5329 : : array is converted). STATIC_P is true if "static" is inside the
5330 : : [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
5331 : : VLA of unspecified length which is nevertheless a complete type,
5332 : : false otherwise. The field for the contained declarator is left to
5333 : : be filled in by set_array_declarator_inner. */
5334 : :
5335 : : struct c_declarator *
5336 : 1130400 : build_array_declarator (location_t loc,
5337 : : tree expr, struct c_declspecs *quals, bool static_p,
5338 : : bool vla_unspec_p)
5339 : : {
5340 : 1130400 : struct c_declarator *declarator = XOBNEW (&parser_obstack,
5341 : : struct c_declarator);
5342 : 1130400 : declarator->id_loc = loc;
5343 : 1130400 : declarator->kind = cdk_array;
5344 : 1130400 : declarator->declarator = 0;
5345 : 1130400 : declarator->u.array.dimen = expr;
5346 : 1130400 : if (quals)
5347 : : {
5348 : 933 : declarator->u.array.attrs = quals->attrs;
5349 : 933 : declarator->u.array.quals = quals_from_declspecs (quals);
5350 : : }
5351 : : else
5352 : : {
5353 : 1129467 : declarator->u.array.attrs = NULL_TREE;
5354 : 1129467 : declarator->u.array.quals = 0;
5355 : : }
5356 : 1130400 : declarator->u.array.static_p = static_p;
5357 : 1130400 : declarator->u.array.vla_unspec_p = vla_unspec_p;
5358 : 1130400 : if (static_p || quals != NULL)
5359 : 1036 : pedwarn_c90 (loc, OPT_Wpedantic,
5360 : : "ISO C90 does not support %<static%> or type "
5361 : : "qualifiers in parameter array declarators");
5362 : 1130400 : if (vla_unspec_p)
5363 : 147 : pedwarn_c90 (loc, OPT_Wpedantic,
5364 : : "ISO C90 does not support %<[*]%> array declarators");
5365 : 147 : if (vla_unspec_p)
5366 : : {
5367 : 147 : if (!current_scope->parm_flag)
5368 : : {
5369 : : /* C99 6.7.5.2p4 */
5370 : 4 : error_at (loc, "%<[*]%> not allowed in other than "
5371 : : "function prototype scope");
5372 : 4 : declarator->u.array.vla_unspec_p = false;
5373 : 4 : return NULL;
5374 : : }
5375 : 143 : current_scope->had_vla_unspec = true;
5376 : : }
5377 : : return declarator;
5378 : : }
5379 : :
5380 : : /* Set the contained declarator of an array declarator. DECL is the
5381 : : declarator, as constructed by build_array_declarator; INNER is what
5382 : : appears on the left of the []. */
5383 : :
5384 : : struct c_declarator *
5385 : 1130396 : set_array_declarator_inner (struct c_declarator *decl,
5386 : : struct c_declarator *inner)
5387 : : {
5388 : 1130396 : decl->declarator = inner;
5389 : 1130396 : return decl;
5390 : : }
5391 : :
5392 : : /* Determine whether TYPE is a one-element array type "[1]". */
5393 : : static bool
5394 : 520858 : one_element_array_type_p (const_tree type)
5395 : : {
5396 : 520858 : if (TREE_CODE (type) != ARRAY_TYPE)
5397 : : return false;
5398 : 520858 : return integer_zerop (array_type_nelts_minus_one (type));
5399 : : }
5400 : :
5401 : : /* Determine whether TYPE is a zero-length array type "[0]". */
5402 : : static bool
5403 : 520858 : zero_length_array_type_p (const_tree type)
5404 : : {
5405 : 520858 : if (TREE_CODE (type) == ARRAY_TYPE)
5406 : 520858 : if (tree type_size = TYPE_SIZE_UNIT (type))
5407 : 436279 : if ((integer_zerop (type_size))
5408 : 1816 : && TYPE_DOMAIN (type) != NULL_TREE
5409 : 438095 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5410 : : return true;
5411 : : return false;
5412 : : }
5413 : :
5414 : : /* INIT is a constructor that forms DECL's initializer. If the final
5415 : : element initializes a flexible array field, adjust the size of the
5416 : : DECL with the initializer based on whether the DECL is a union or
5417 : : a structure. */
5418 : :
5419 : : static void
5420 : 103550 : add_flexible_array_elts_to_size (tree decl, tree init)
5421 : : {
5422 : 103550 : tree elt, type;
5423 : :
5424 : 103550 : if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
5425 : 1403 : return;
5426 : :
5427 : 102147 : elt = CONSTRUCTOR_ELTS (init)->last ().value;
5428 : 102147 : type = TREE_TYPE (elt);
5429 : 102147 : if (c_flexible_array_member_type_p (type))
5430 : : {
5431 : 220 : complete_array_type (&type, elt, false);
5432 : : /* For a structure, add the size of the initializer to the DECL's
5433 : : size. */
5434 : 220 : if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
5435 : : {
5436 : 210 : DECL_SIZE (decl)
5437 : 210 : = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5438 : 210 : DECL_SIZE_UNIT (decl)
5439 : 420 : = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
5440 : : TYPE_SIZE_UNIT (type));
5441 : : }
5442 : : /* For a union, the DECL's size is the maximum of the current size
5443 : : and the size of the initializer. */
5444 : : else
5445 : : {
5446 : 10 : DECL_SIZE (decl)
5447 : 10 : = size_binop (MAX_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5448 : 10 : DECL_SIZE_UNIT (decl)
5449 : 20 : = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
5450 : : TYPE_SIZE_UNIT (type));
5451 : : }
5452 : : }
5453 : : }
5454 : :
5455 : : /* Decode a "typename", such as "int **", returning a ..._TYPE node.
5456 : : Set *EXPR, if EXPR not NULL, to any expression to be evaluated
5457 : : before the type name, and set *EXPR_CONST_OPERANDS, if
5458 : : EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
5459 : : appear in a constant expression. */
5460 : :
5461 : : tree
5462 : 118376313 : groktypename (struct c_type_name *type_name, tree *expr,
5463 : : bool *expr_const_operands)
5464 : : {
5465 : 118376313 : tree type;
5466 : 118376313 : tree attrs = type_name->specs->attrs;
5467 : :
5468 : 118376313 : type_name->specs->attrs = NULL_TREE;
5469 : :
5470 : 118376313 : type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
5471 : : false, NULL, &attrs, expr, expr_const_operands,
5472 : : DEPRECATED_NORMAL);
5473 : :
5474 : : /* Apply attributes. */
5475 : 118376313 : attrs = c_warn_type_attributes (type, attrs);
5476 : 118376313 : decl_attributes (&type, attrs, 0);
5477 : :
5478 : 118376313 : return type;
5479 : : }
5480 : :
5481 : : /* Looks up the most recent pushed declaration corresponding to DECL. */
5482 : :
5483 : : static tree
5484 : 90302733 : lookup_last_decl (tree decl)
5485 : : {
5486 : 90302733 : tree last_decl = lookup_name (DECL_NAME (decl));
5487 : 90302733 : if (!last_decl)
5488 : 87785525 : last_decl = lookup_name_in_scope (DECL_NAME (decl), external_scope);
5489 : 90302733 : return last_decl;
5490 : : }
5491 : :
5492 : : /* Wrapper for decl_attributes that adds some implicit attributes
5493 : : to VAR_DECLs or FUNCTION_DECLs. */
5494 : :
5495 : : static tree
5496 : 62842668 : c_decl_attributes (tree *node, tree attributes, int flags)
5497 : : {
5498 : : /* Add implicit "omp declare target" attribute if requested. */
5499 : 62842668 : if (vec_safe_length (current_omp_declare_target_attribute)
5500 : 9158 : && ((VAR_P (*node) && is_global_var (*node))
5501 : 2710 : || TREE_CODE (*node) == FUNCTION_DECL))
5502 : : {
5503 : 1112 : if (VAR_P (*node) && !omp_mappable_type (TREE_TYPE (*node)))
5504 : 13 : attributes = tree_cons (get_identifier ("omp declare target implicit"),
5505 : : NULL_TREE, attributes);
5506 : : else
5507 : 1099 : attributes = tree_cons (get_identifier ("omp declare target"),
5508 : : NULL_TREE, attributes);
5509 : 1112 : if (TREE_CODE (*node) == FUNCTION_DECL)
5510 : : {
5511 : 982 : int device_type
5512 : 982 : = current_omp_declare_target_attribute->last ().device_type;
5513 : 982 : device_type = MAX (device_type, 0);
5514 : 982 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
5515 : 982 : && !lookup_attribute ("omp declare target host", attributes))
5516 : 2 : attributes
5517 : 2 : = tree_cons (get_identifier ("omp declare target host"),
5518 : : NULL_TREE, attributes);
5519 : 982 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
5520 : 982 : && !lookup_attribute ("omp declare target nohost", attributes))
5521 : 2 : attributes
5522 : 2 : = tree_cons (get_identifier ("omp declare target nohost"),
5523 : : NULL_TREE, attributes);
5524 : :
5525 : 982 : int indirect
5526 : 982 : = current_omp_declare_target_attribute->last ().indirect;
5527 : 982 : if (indirect && !lookup_attribute ("omp declare target indirect",
5528 : : attributes))
5529 : 10 : attributes
5530 : 10 : = tree_cons (get_identifier ("omp declare target indirect"),
5531 : : NULL_TREE, attributes);
5532 : : }
5533 : : }
5534 : :
5535 : 62842668 : if (flag_openmp || flag_openmp_simd)
5536 : : {
5537 : : bool diagnosed = false;
5538 : 1340845 : for (tree *pa = &attributes; *pa; )
5539 : : {
5540 : 867796 : if (is_attribute_namespace_p ("omp", *pa))
5541 : : {
5542 : 60 : tree name = get_attribute_name (*pa);
5543 : 60 : if (is_attribute_p ("directive", name)
5544 : 0 : || is_attribute_p ("sequence", name)
5545 : 60 : || is_attribute_p ("decl", name))
5546 : : {
5547 : 60 : const char *p = NULL;
5548 : 60 : if (TREE_VALUE (*pa) == NULL_TREE)
5549 : 12 : p = IDENTIFIER_POINTER (name);
5550 : 108 : for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
5551 : : {
5552 : 48 : tree d = TREE_VALUE (a);
5553 : 48 : gcc_assert (TREE_CODE (d) == C_TOKEN_VEC);
5554 : 86 : if (TREE_PUBLIC (d)
5555 : 38 : && (VAR_P (*node)
5556 : 2 : || TREE_CODE (*node) == FUNCTION_DECL)
5557 : 86 : && c_maybe_parse_omp_decl (*node, d))
5558 : 38 : continue;
5559 : 10 : p = TREE_PUBLIC (d) ? "decl" : "directive";
5560 : : }
5561 : 60 : if (p && !diagnosed)
5562 : : {
5563 : 22 : error ("%<omp::%s%> not allowed to be specified in "
5564 : : "this context", p);
5565 : 22 : diagnosed = true;
5566 : : }
5567 : 60 : if (p)
5568 : : {
5569 : 22 : *pa = TREE_CHAIN (*pa);
5570 : 22 : continue;
5571 : : }
5572 : : }
5573 : : }
5574 : 867774 : pa = &TREE_CHAIN (*pa);
5575 : : }
5576 : : }
5577 : :
5578 : : /* Look up the current declaration with all the attributes merged
5579 : : so far so that attributes on the current declaration that's
5580 : : about to be pushed that conflict with the former can be detected,
5581 : : diagnosed, and rejected as appropriate. To match the C++ FE, do
5582 : : not pass an error_mark_node when we found an undeclared variable. */
5583 : 62842668 : tree last_decl = lookup_last_decl (*node);
5584 : 62842668 : if (last_decl == error_mark_node)
5585 : 20 : last_decl = NULL_TREE;
5586 : 62842668 : return decl_attributes (node, attributes, flags, last_decl);
5587 : : }
5588 : :
5589 : :
5590 : : /* Decode a declarator in an ordinary declaration or data definition.
5591 : : This is called as soon as the type information and variable name
5592 : : have been parsed, before parsing the initializer if any.
5593 : : Here we create the ..._DECL node, fill in its type,
5594 : : and (if DO_PUSH) put it on the list of decls for the current context.
5595 : : When nonnull, set *LASTLOC to the location of the prior declaration
5596 : : of the same entity if one exists.
5597 : : The ..._DECL node is returned as the value.
5598 : :
5599 : : Exception: for arrays where the length is not specified,
5600 : : the type is left null, to be filled in by `finish_decl'.
5601 : :
5602 : : Function definitions do not come here; they go to start_function
5603 : : instead. However, external and forward declarations of functions
5604 : : do go through here. Structure field declarations are done by
5605 : : grokfield and not through here. */
5606 : :
5607 : : tree
5608 : 27460088 : start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5609 : : bool initialized, tree attributes, bool do_push /* = true */,
5610 : : location_t *lastloc /* = NULL */)
5611 : : {
5612 : 27460088 : tree decl;
5613 : 27460088 : tree old_decl;
5614 : 27460088 : tree tem;
5615 : 27460088 : tree expr = NULL_TREE;
5616 : 27460088 : enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5617 : :
5618 : : /* An object declared as __attribute__((unavailable)) suppresses
5619 : : warnings and errors from __attribute__((deprecated/unavailable))
5620 : : components.
5621 : : An object declared as __attribute__((deprecated)) suppresses
5622 : : warnings of uses of other deprecated items. */
5623 : 27460088 : if (lookup_attribute ("unavailable", attributes))
5624 : : deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
5625 : 27460064 : else if (lookup_attribute ("deprecated", attributes))
5626 : 29651 : deprecated_state = DEPRECATED_SUPPRESS;
5627 : :
5628 : 27460088 : decl = grokdeclarator (declarator, declspecs,
5629 : : NORMAL, initialized, NULL, &attributes, &expr, NULL,
5630 : : deprecated_state);
5631 : 27460088 : if (!decl || decl == error_mark_node)
5632 : : return NULL_TREE;
5633 : :
5634 : 27460065 : old_decl = lookup_last_decl (decl);
5635 : :
5636 : 27460065 : if (tree lastdecl = lastloc ? old_decl : NULL_TREE)
5637 : 4683087 : if (lastdecl != error_mark_node)
5638 : 4683078 : *lastloc = DECL_SOURCE_LOCATION (lastdecl);
5639 : :
5640 : : /* Make sure the size expression is evaluated at this point. */
5641 : 27460065 : if (expr && !current_scope->parm_flag)
5642 : 12904 : add_stmt (fold_convert (void_type_node, expr));
5643 : :
5644 : 12994444 : if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5645 : 27460067 : && TREE_PUBLIC (decl))
5646 : 1 : warning (OPT_Wmain, "%q+D is usually a function", decl);
5647 : :
5648 : 14 : if (warn_missing_variable_declarations && VAR_P (decl)
5649 : 27460078 : && !DECL_EXTERNAL (decl) && TREE_PUBLIC (decl) && old_decl == NULL_TREE)
5650 : 5 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_variable_declarations,
5651 : : "no previous declaration for %qD", decl);
5652 : :
5653 : 27460065 : if (initialized)
5654 : : /* Is it valid for this decl to have an initializer at all?
5655 : : If not, set INITIALIZED to zero, which will indirectly
5656 : : tell 'finish_decl' to ignore the initializer once it is parsed. */
5657 : 6196991 : switch (TREE_CODE (decl))
5658 : : {
5659 : 6 : case TYPE_DECL:
5660 : 6 : error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5661 : 6 : initialized = false;
5662 : 6 : break;
5663 : :
5664 : 9 : case FUNCTION_DECL:
5665 : 9 : error ("function %qD is initialized like a variable", decl);
5666 : 9 : initialized = false;
5667 : 9 : break;
5668 : :
5669 : 49 : case PARM_DECL:
5670 : : /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5671 : 49 : error ("parameter %qD is initialized", decl);
5672 : 49 : initialized = false;
5673 : 49 : break;
5674 : :
5675 : 6196927 : default:
5676 : : /* Don't allow initializations for incomplete types except for
5677 : : arrays which might be completed by the initialization. */
5678 : :
5679 : : /* This can happen if the array size is an undefined macro.
5680 : : We already gave a warning, so we don't need another one. */
5681 : 6196927 : if (TREE_TYPE (decl) == error_mark_node)
5682 : : initialized = false;
5683 : 6196905 : else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5684 : : {
5685 : : /* A complete type is ok if size is fixed. If the size is
5686 : : variable, an empty initializer is OK and nonempty
5687 : : initializers will be diagnosed in the parser. */
5688 : : }
5689 : 12078 : else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5690 : : {
5691 : 3 : error ("variable %qD has initializer but incomplete type", decl);
5692 : 3 : initialized = false;
5693 : : }
5694 : : }
5695 : :
5696 : 67 : if (initialized)
5697 : : {
5698 : 6196902 : if (current_scope == file_scope)
5699 : 155755 : TREE_STATIC (decl) = 1;
5700 : :
5701 : : /* Tell 'pushdecl' this is an initialized decl
5702 : : even though we don't yet have the initializer expression.
5703 : : Also tell 'finish_decl' it may store the real initializer. */
5704 : 6196902 : DECL_INITIAL (decl) = error_mark_node;
5705 : : }
5706 : :
5707 : : /* If this is a function declaration, write a record describing it to the
5708 : : prototypes file (if requested). */
5709 : :
5710 : 27460065 : if (TREE_CODE (decl) == FUNCTION_DECL)
5711 : 14465621 : gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5712 : :
5713 : : /* ANSI specifies that a tentative definition which is not merged with
5714 : : a non-tentative definition behaves exactly like a definition with an
5715 : : initializer equal to zero. (Section 3.7.2)
5716 : :
5717 : : -fno-common gives strict ANSI behavior, though this tends to break
5718 : : a large body of code that grew up without this rule.
5719 : :
5720 : : Thread-local variables are never common, since there's no entrenched
5721 : : body of code to break, and it allows more efficient variable references
5722 : : in the presence of dynamic linking. */
5723 : :
5724 : 27460065 : if (VAR_P (decl)
5725 : 8746978 : && !initialized
5726 : 2550076 : && TREE_PUBLIC (decl)
5727 : 987836 : && !DECL_THREAD_LOCAL_P (decl)
5728 : 28445555 : && !flag_no_common)
5729 : 128 : DECL_COMMON (decl) = 1;
5730 : :
5731 : : /* Set attributes here so if duplicate decl, will have proper attributes. */
5732 : 27460065 : c_decl_attributes (&decl, attributes, 0);
5733 : :
5734 : : /* Handle gnu_inline attribute. */
5735 : 27460064 : if (declspecs->inline_p
5736 : 1015 : && !flag_gnu89_inline
5737 : 983 : && TREE_CODE (decl) == FUNCTION_DECL
5738 : 27461039 : && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
5739 : 952 : || current_function_decl))
5740 : : {
5741 : 46 : if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5742 : : ;
5743 : 37 : else if (declspecs->storage_class != csc_static)
5744 : 37 : DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5745 : : }
5746 : :
5747 : 27460064 : if (TREE_CODE (decl) == FUNCTION_DECL
5748 : 14465620 : && DECL_DECLARED_INLINE_P (decl)
5749 : 1005 : && DECL_UNINLINABLE (decl)
5750 : 27460066 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5751 : : {
5752 : 2 : auto_urlify_attributes sentinel;
5753 : 2 : warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5754 : : decl, "noinline");
5755 : 2 : }
5756 : :
5757 : : /* C99 6.7.4p3: An inline definition of a function with external
5758 : : linkage shall not contain a definition of a modifiable object
5759 : : with static storage duration... */
5760 : 27460064 : if (VAR_P (decl)
5761 : 8746978 : && current_scope != file_scope
5762 : 7596437 : && TREE_STATIC (decl)
5763 : 78729 : && !TREE_READONLY (decl)
5764 : 76164 : && DECL_DECLARED_INLINE_P (current_function_decl)
5765 : 27460134 : && DECL_EXTERNAL (current_function_decl))
5766 : 7 : record_inline_static (input_location, current_function_decl,
5767 : : decl, csi_modifiable);
5768 : :
5769 : 27460064 : if (c_dialect_objc ()
5770 : 0 : && VAR_OR_FUNCTION_DECL_P (decl))
5771 : 0 : objc_check_global_decl (decl);
5772 : :
5773 : : /* Add this decl to the current scope.
5774 : : TEM may equal DECL or it may be a previous decl of the same name. */
5775 : 27460064 : if (do_push)
5776 : : {
5777 : 27459725 : tem = pushdecl (decl);
5778 : :
5779 : 27459725 : if (initialized && DECL_EXTERNAL (tem))
5780 : : {
5781 : 27 : DECL_EXTERNAL (tem) = 0;
5782 : 27 : TREE_STATIC (tem) = 1;
5783 : : }
5784 : :
5785 : 27459725 : return tem;
5786 : : }
5787 : : else
5788 : 339 : return decl;
5789 : : }
5790 : :
5791 : : /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5792 : : DECL or the non-array element type if DECL is an uninitialized array.
5793 : : If that type has a const member, diagnose this. */
5794 : :
5795 : : static void
5796 : 7 : diagnose_uninitialized_cst_member (tree decl, tree type)
5797 : : {
5798 : 7 : tree field;
5799 : 17 : for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5800 : : {
5801 : 10 : tree field_type;
5802 : 10 : if (TREE_CODE (field) != FIELD_DECL)
5803 : 0 : continue;
5804 : 10 : field_type = strip_array_types (TREE_TYPE (field));
5805 : :
5806 : 10 : if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5807 : : {
5808 : 5 : auto_diagnostic_group d;
5809 : 5 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5810 : : "uninitialized const member in %qT is invalid in C++",
5811 : 5 : strip_array_types (TREE_TYPE (decl))))
5812 : 5 : inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5813 : 5 : }
5814 : :
5815 : 10 : if (RECORD_OR_UNION_TYPE_P (field_type))
5816 : 2 : diagnose_uninitialized_cst_member (decl, field_type);
5817 : : }
5818 : 7 : }
5819 : :
5820 : : /* Finish processing of a declaration;
5821 : : install its initial value.
5822 : : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5823 : : If the length of an array type is not known before,
5824 : : it must be determined now, from the initial value, or it is an error.
5825 : :
5826 : : INIT_LOC is the location of the initial value. */
5827 : :
5828 : : void
5829 : 152831793 : finish_decl (tree decl, location_t init_loc, tree init,
5830 : : tree origtype, tree asmspec_tree)
5831 : : {
5832 : 152831793 : tree type;
5833 : 152831793 : bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5834 : 152831793 : const char *asmspec = 0;
5835 : :
5836 : : /* If a name was specified, get the string. */
5837 : 144081901 : if (VAR_OR_FUNCTION_DECL_P (decl)
5838 : 167297413 : && DECL_FILE_SCOPE_P (decl))
5839 : 15617678 : asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5840 : 152831793 : if (asmspec_tree)
5841 : 855979 : asmspec = TREE_STRING_POINTER (asmspec_tree);
5842 : :
5843 : 152831793 : if (VAR_P (decl)
5844 : 8749892 : && TREE_STATIC (decl)
5845 : 153892160 : && global_bindings_p ())
5846 : : /* So decl is a global variable. Record the types it uses
5847 : : so that we can decide later to emit debug info for them. */
5848 : 978507 : record_types_used_by_current_var_decl (decl);
5849 : :
5850 : : /* If `start_decl' didn't like having an initialization, ignore it now. */
5851 : 152831793 : if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5852 : : init = NULL_TREE;
5853 : :
5854 : : /* Don't crash if parm is initialized. */
5855 : 152831793 : if (TREE_CODE (decl) == PARM_DECL)
5856 : : init = NULL_TREE;
5857 : :
5858 : 31676232 : if (init)
5859 : 6199817 : store_init_value (init_loc, decl, init, origtype);
5860 : :
5861 : 152831793 : if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
5862 : 0 : || TREE_CODE (decl) == FIELD_DECL))
5863 : 0 : objc_check_decl (decl);
5864 : :
5865 : 152831793 : type = TREE_TYPE (decl);
5866 : :
5867 : : /* Deduce size of array from initialization, if not already known.
5868 : : This is only needed for an initialization in the current scope;
5869 : : it must not be done for a file-scope initialization of a
5870 : : declaration with external linkage, redeclared in an inner scope
5871 : : with the outer declaration shadowed in an intermediate scope. */
5872 : 152831793 : if (TREE_CODE (type) == ARRAY_TYPE
5873 : 849980 : && TYPE_DOMAIN (type) == NULL_TREE
5874 : 19663 : && TREE_CODE (decl) != TYPE_DECL
5875 : 152851360 : && !(TREE_PUBLIC (decl) && current_scope != file_scope))
5876 : : {
5877 : 19381 : bool do_default
5878 : 19381 : = (TREE_STATIC (decl)
5879 : : /* Even if pedantic, an external linkage array
5880 : : may have incomplete type at first. */
5881 : 19381 : ? pedantic && !TREE_PUBLIC (decl)
5882 : 12296 : : !DECL_EXTERNAL (decl));
5883 : 19381 : int failure
5884 : 19381 : = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5885 : : do_default);
5886 : :
5887 : : /* Get the completed type made by complete_array_type. */
5888 : 19381 : type = TREE_TYPE (decl);
5889 : :
5890 : 19381 : switch (failure)
5891 : : {
5892 : 0 : case 1:
5893 : 0 : error ("initializer fails to determine size of %q+D", decl);
5894 : 0 : break;
5895 : :
5896 : 7331 : case 2:
5897 : 7331 : if (do_default)
5898 : 1 : error ("array size missing in %q+D", decl);
5899 : : break;
5900 : :
5901 : 3 : case 3:
5902 : 3 : error ("zero or negative size array %q+D", decl);
5903 : 3 : break;
5904 : :
5905 : 12047 : case 0:
5906 : : /* For global variables, update the copy of the type that
5907 : : exists in the binding. */
5908 : 12047 : if (TREE_PUBLIC (decl))
5909 : : {
5910 : 3747 : struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5911 : 7494 : while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5912 : 3747 : b_ext = b_ext->shadowed;
5913 : 3747 : if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5914 : : {
5915 : 3747 : if (b_ext->u.type && comptypes (b_ext->u.type, type))
5916 : 176 : b_ext->u.type = composite_type (b_ext->u.type, type);
5917 : : else
5918 : 3571 : b_ext->u.type = type;
5919 : : }
5920 : : }
5921 : : break;
5922 : :
5923 : 0 : default:
5924 : 0 : gcc_unreachable ();
5925 : : }
5926 : :
5927 : 19381 : if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
5928 : 11210 : TREE_TYPE (DECL_INITIAL (decl)) = type;
5929 : :
5930 : 19381 : relayout_decl (decl);
5931 : : }
5932 : :
5933 : : /* Look for braced array initializers for character arrays and
5934 : : recursively convert them into STRING_CSTs. */
5935 : 152831793 : if (tree init = DECL_INITIAL (decl))
5936 : 127357769 : DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
5937 : :
5938 : 152831793 : if (VAR_P (decl))
5939 : : {
5940 : 8749892 : if (init && TREE_CODE (init) == CONSTRUCTOR)
5941 : 103550 : add_flexible_array_elts_to_size (decl, init);
5942 : :
5943 : 8749892 : complete_flexible_array_elts (DECL_INITIAL (decl));
5944 : :
5945 : 8749892 : if (is_global_var (decl))
5946 : : {
5947 : 1233705 : type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5948 : 1233705 : ? TCTX_THREAD_STORAGE
5949 : : : TCTX_STATIC_STORAGE);
5950 : 1233705 : if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
5951 : 0 : TREE_TYPE (decl) = error_mark_node;
5952 : : }
5953 : :
5954 : 8757891 : if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5955 : 8757792 : && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5956 : 97 : layout_decl (decl, 0);
5957 : :
5958 : 8749892 : if (DECL_SIZE (decl) == NULL_TREE
5959 : : /* Don't give an error if we already gave one earlier. */
5960 : 7902 : && TREE_TYPE (decl) != error_mark_node
5961 : 8757695 : && (TREE_STATIC (decl)
5962 : : /* A static variable with an incomplete type
5963 : : is an error if it is initialized.
5964 : : Also if it is not file scope.
5965 : : Also if it is thread-local (in C23).
5966 : : Otherwise, let it through, but if it is not `extern'
5967 : : then it may cause an error message later. */
5968 : 244 : ? (DECL_INITIAL (decl) != NULL_TREE
5969 : 244 : || !DECL_FILE_SCOPE_P (decl)
5970 : 244 : || (flag_isoc23 && DECL_THREAD_LOCAL_P (decl)))
5971 : : /* An automatic variable with an incomplete type
5972 : : is an error. */
5973 : 7559 : : !DECL_EXTERNAL (decl)))
5974 : : {
5975 : 13 : error ("storage size of %q+D isn%'t known", decl);
5976 : 13 : TREE_TYPE (decl) = error_mark_node;
5977 : : }
5978 : :
5979 : 17310856 : if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5980 : 8480578 : || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5981 : 282591 : && DECL_SIZE (decl) == NULL_TREE
5982 : 8750130 : && TREE_STATIC (decl))
5983 : 95 : incomplete_record_decls.safe_push (decl);
5984 : :
5985 : 8749892 : if (is_global_var (decl)
5986 : 1233705 : && DECL_SIZE (decl) != NULL_TREE
5987 : 9975734 : && TREE_TYPE (decl) != error_mark_node)
5988 : : {
5989 : 1225824 : if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5990 : 1225821 : constant_expression_warning (DECL_SIZE (decl));
5991 : : else
5992 : : {
5993 : 3 : error ("storage size of %q+D isn%'t constant", decl);
5994 : 3 : TREE_TYPE (decl) = error_mark_node;
5995 : : }
5996 : : }
5997 : :
5998 : 8749892 : if (TREE_USED (type))
5999 : : {
6000 : 4 : TREE_USED (decl) = 1;
6001 : 4 : DECL_READ_P (decl) = 1;
6002 : : }
6003 : : }
6004 : :
6005 : : /* If this is a function and an assembler name is specified, reset DECL_RTL
6006 : : so we can give it its new name. Also, update builtin_decl if it
6007 : : was a normal built-in. */
6008 : 152831793 : if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
6009 : : {
6010 : 842969 : if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6011 : 71152 : set_builtin_user_assembler_name (decl, asmspec);
6012 : 842969 : set_user_assembler_name (decl, asmspec);
6013 : : }
6014 : :
6015 : : /* If #pragma weak was used, mark the decl weak now. */
6016 : 152831793 : maybe_apply_pragma_weak (decl);
6017 : :
6018 : : /* Output the assembler code and/or RTL code for variables and functions,
6019 : : unless the type is an undefined structure or union.
6020 : : If not, it will get done when the type is completed. */
6021 : :
6022 : 152831793 : if (VAR_OR_FUNCTION_DECL_P (decl))
6023 : : {
6024 : : /* Determine the ELF visibility. */
6025 : 23215512 : if (TREE_PUBLIC (decl))
6026 : 15593154 : c_determine_visibility (decl);
6027 : :
6028 : : /* This is a no-op in c-lang.cc or something real in objc-act.cc. */
6029 : 23215512 : if (c_dialect_objc ())
6030 : 0 : objc_check_decl (decl);
6031 : :
6032 : 23215512 : if (asmspec)
6033 : : {
6034 : : /* If this is not a static variable, issue a warning.
6035 : : It doesn't make any sense to give an ASMSPEC for an
6036 : : ordinary, non-register local variable. Historically,
6037 : : GCC has accepted -- but ignored -- the ASMSPEC in
6038 : : this case. */
6039 : 857016 : if (!DECL_FILE_SCOPE_P (decl)
6040 : 1037 : && VAR_P (decl)
6041 : 1037 : && !C_DECL_REGISTER (decl)
6042 : 855987 : && !TREE_STATIC (decl))
6043 : 1 : warning (0, "ignoring %<asm%> specifier for non-static local "
6044 : : "variable %q+D", decl);
6045 : : else
6046 : 855978 : set_user_assembler_name (decl, asmspec);
6047 : : }
6048 : :
6049 : 23215512 : if (DECL_FILE_SCOPE_P (decl))
6050 : : {
6051 : 15617678 : if (DECL_INITIAL (decl) == NULL_TREE
6052 : 15617678 : || DECL_INITIAL (decl) == error_mark_node)
6053 : : /* Don't output anything
6054 : : when a tentative file-scope definition is seen.
6055 : : But at end of compilation, do output code for them. */
6056 : 15461903 : DECL_DEFER_OUTPUT (decl) = 1;
6057 : 15617678 : if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
6058 : 68 : DECL_HARD_REGISTER (decl) = 1;
6059 : 15617678 : rest_of_decl_compilation (decl, true, 0);
6060 : :
6061 : 15617678 : if (TREE_CODE (decl) == FUNCTION_DECL)
6062 : : {
6063 : 14465557 : tree parms = DECL_ARGUMENTS (decl);
6064 : 14465557 : const bool builtin = fndecl_built_in_p (decl);
6065 : 14465557 : if (tree access = build_attr_access_from_parms (parms, !builtin))
6066 : 340013 : decl_attributes (&decl, access, 0);
6067 : : }
6068 : : }
6069 : : else
6070 : : {
6071 : : /* In conjunction with an ASMSPEC, the `register'
6072 : : keyword indicates that we should place the variable
6073 : : in a particular register. */
6074 : 7597834 : if (asmspec && C_DECL_REGISTER (decl))
6075 : : {
6076 : 1029 : DECL_HARD_REGISTER (decl) = 1;
6077 : : /* This cannot be done for a structure with volatile
6078 : : fields, on which DECL_REGISTER will have been
6079 : : reset. */
6080 : 1029 : if (!DECL_REGISTER (decl))
6081 : 1 : error ("cannot put object with volatile field into register");
6082 : : }
6083 : :
6084 : 7597834 : if (TREE_CODE (decl) != FUNCTION_DECL)
6085 : : {
6086 : : /* If we're building a variable sized type, and we might be
6087 : : reachable other than via the top of the current binding
6088 : : level, then create a new BIND_EXPR so that we deallocate
6089 : : the object at the right time. */
6090 : : /* Note that DECL_SIZE can be null due to errors. */
6091 : 7597771 : if (DECL_SIZE (decl)
6092 : 7597732 : && !TREE_CONSTANT (DECL_SIZE (decl))
6093 : 7605112 : && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
6094 : : {
6095 : 1336 : tree bind;
6096 : 1336 : bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6097 : 1336 : TREE_SIDE_EFFECTS (bind) = 1;
6098 : 1336 : add_stmt (bind);
6099 : 1336 : BIND_EXPR_BODY (bind) = push_stmt_list ();
6100 : : }
6101 : 7597771 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
6102 : : DECL_EXPR, decl));
6103 : : }
6104 : : }
6105 : :
6106 : :
6107 : 23215512 : if (!DECL_FILE_SCOPE_P (decl))
6108 : : {
6109 : : /* Recompute the RTL of a local array now
6110 : : if it used to be an incomplete type. */
6111 : 7597834 : if (was_incomplete && !is_global_var (decl))
6112 : : {
6113 : : /* If we used it already as memory, it must stay in memory. */
6114 : 5191 : TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6115 : : /* If it's still incomplete now, no init will save it. */
6116 : 5191 : if (DECL_SIZE (decl) == NULL_TREE)
6117 : 102 : DECL_INITIAL (decl) = NULL_TREE;
6118 : : }
6119 : : }
6120 : : }
6121 : :
6122 : 152831793 : if (TREE_CODE (decl) == TYPE_DECL)
6123 : : {
6124 : 4352758 : if (!DECL_FILE_SCOPE_P (decl)
6125 : 4352758 : && c_type_variably_modified_p (TREE_TYPE (decl)))
6126 : 5315 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6127 : :
6128 : 8321672 : rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
6129 : : }
6130 : :
6131 : : /* Install a cleanup (aka destructor) if one was given. */
6132 : 152831793 : if (VAR_P (decl) && !TREE_STATIC (decl))
6133 : : {
6134 : 7689522 : tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
6135 : 7689522 : if (attr)
6136 : : {
6137 : 95 : tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
6138 : 95 : tree cleanup_decl = lookup_name (cleanup_id);
6139 : 95 : tree cleanup;
6140 : 95 : vec<tree, va_gc> *v;
6141 : :
6142 : : /* Build "cleanup(&decl)" for the destructor. */
6143 : 95 : cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
6144 : 95 : vec_alloc (v, 1);
6145 : 95 : v->quick_push (cleanup);
6146 : 95 : cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
6147 : 95 : vNULL, cleanup_decl, v, NULL);
6148 : 95 : vec_free (v);
6149 : :
6150 : : /* Don't warn about decl unused; the cleanup uses it. */
6151 : 95 : TREE_USED (decl) = 1;
6152 : 95 : TREE_USED (cleanup_decl) = 1;
6153 : 95 : DECL_READ_P (decl) = 1;
6154 : :
6155 : 95 : push_cleanup (decl, cleanup, false);
6156 : : }
6157 : : }
6158 : :
6159 : 152831793 : if (warn_cxx_compat
6160 : 22371 : && VAR_P (decl)
6161 : 7538 : && !DECL_EXTERNAL (decl)
6162 : 152838891 : && DECL_INITIAL (decl) == NULL_TREE)
6163 : : {
6164 : 2326 : type = strip_array_types (type);
6165 : 2326 : if (TREE_READONLY (decl))
6166 : 6 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6167 : : "uninitialized %<const %D%> is invalid in C++", decl);
6168 : 2320 : else if (RECORD_OR_UNION_TYPE_P (type)
6169 : 2320 : && C_TYPE_FIELDS_READONLY (type))
6170 : 5 : diagnose_uninitialized_cst_member (decl, type);
6171 : : }
6172 : :
6173 : 152831793 : if (flag_openmp
6174 : 430744 : && VAR_P (decl)
6175 : 152855522 : && lookup_attribute ("omp declare target implicit",
6176 : 23729 : DECL_ATTRIBUTES (decl)))
6177 : : {
6178 : 13 : DECL_ATTRIBUTES (decl)
6179 : 13 : = remove_attribute ("omp declare target implicit",
6180 : 13 : DECL_ATTRIBUTES (decl));
6181 : 13 : if (!omp_mappable_type (TREE_TYPE (decl)))
6182 : 9 : error ("%q+D in declare target directive does not have mappable type",
6183 : : decl);
6184 : 4 : else if (!lookup_attribute ("omp declare target",
6185 : 4 : DECL_ATTRIBUTES (decl))
6186 : 8 : && !lookup_attribute ("omp declare target link",
6187 : 4 : DECL_ATTRIBUTES (decl)))
6188 : : {
6189 : 4 : DECL_ATTRIBUTES (decl)
6190 : 4 : = tree_cons (get_identifier ("omp declare target"),
6191 : 4 : NULL_TREE, DECL_ATTRIBUTES (decl));
6192 : 4 : symtab_node *node = symtab_node::get (decl);
6193 : 4 : if (node != NULL)
6194 : : {
6195 : 4 : node->offloadable = 1;
6196 : 4 : if (ENABLE_OFFLOADING)
6197 : : {
6198 : : g->have_offload = true;
6199 : : if (is_a <varpool_node *> (node))
6200 : : vec_safe_push (offload_vars, decl);
6201 : : }
6202 : : }
6203 : : }
6204 : : }
6205 : :
6206 : : /* This is the last point we can lower alignment so give the target the
6207 : : chance to do so. */
6208 : 152831793 : if (VAR_P (decl)
6209 : 8749892 : && !is_global_var (decl)
6210 : 160347980 : && !DECL_HARD_REGISTER (decl))
6211 : 7515158 : targetm.lower_local_decl_alignment (decl);
6212 : :
6213 : 152831793 : invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
6214 : 152831793 : }
6215 : :
6216 : : /* Given a parsed parameter declaration, decode it into a PARM_DECL.
6217 : : EXPR is NULL or a pointer to an expression that needs to be
6218 : : evaluated for the side effects of array size expressions in the
6219 : : parameters. */
6220 : :
6221 : : tree
6222 : 0 : grokparm (const struct c_parm *parm, tree *expr)
6223 : : {
6224 : 0 : tree attrs = parm->attrs;
6225 : 0 : tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
6226 : 0 : NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
6227 : :
6228 : 0 : decl_attributes (&decl, attrs, 0);
6229 : :
6230 : 0 : return decl;
6231 : : }
6232 : :
6233 : :
6234 : :
6235 : : /* Given a parsed parameter declaration, decode it into a PARM_DECL
6236 : : and push that on the current scope. EXPR is a pointer to an
6237 : : expression that needs to be evaluated for the side effects of array
6238 : : size expressions in the parameters. */
6239 : :
6240 : : void
6241 : 121132905 : push_parm_decl (const struct c_parm *parm, tree *expr)
6242 : : {
6243 : 121132905 : tree attrs = parm->attrs;
6244 : 121132905 : tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
6245 : 121132905 : &attrs, expr, NULL, DEPRECATED_NORMAL);
6246 : 121132905 : if (decl && DECL_P (decl))
6247 : 121132905 : DECL_SOURCE_LOCATION (decl) = parm->loc;
6248 : :
6249 : 121132905 : decl_attributes (&decl, attrs, 0);
6250 : :
6251 : 121132905 : decl = pushdecl (decl);
6252 : :
6253 : 121132905 : finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
6254 : 121132905 : }
6255 : :
6256 : : /* Mark all the parameter declarations to date as forward decls.
6257 : : Also diagnose use of this extension. */
6258 : :
6259 : : void
6260 : 37 : mark_forward_parm_decls (void)
6261 : : {
6262 : 37 : struct c_binding *b;
6263 : :
6264 : 37 : if (pedantic && !current_scope->warned_forward_parm_decls)
6265 : : {
6266 : 4 : pedwarn (input_location, OPT_Wpedantic,
6267 : : "ISO C forbids forward parameter declarations");
6268 : 4 : current_scope->warned_forward_parm_decls = true;
6269 : : }
6270 : :
6271 : 83 : for (b = current_scope->bindings; b; b = b->prev)
6272 : 46 : if (TREE_CODE (b->decl) == PARM_DECL)
6273 : 46 : TREE_ASM_WRITTEN (b->decl) = 1;
6274 : 37 : }
6275 : :
6276 : : /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
6277 : : literal, which may be an incomplete array type completed by the
6278 : : initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
6279 : : literal. NON_CONST is true if the initializers contain something
6280 : : that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
6281 : : it is the (valid) alignment for this compound literal, as specified
6282 : : with _Alignas. SCSPECS are the storage class specifiers (C23) from the
6283 : : compound literal. */
6284 : :
6285 : : tree
6286 : 898082 : build_compound_literal (location_t loc, tree type, tree init, bool non_const,
6287 : : unsigned int alignas_align,
6288 : : struct c_declspecs *scspecs)
6289 : : {
6290 : : /* We do not use start_decl here because we have a type, not a declarator;
6291 : : and do not use finish_decl because the decl should be stored inside
6292 : : the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
6293 : 898082 : tree decl;
6294 : 898082 : tree complit;
6295 : 898082 : tree stmt;
6296 : 898082 : bool threadp = scspecs ? scspecs->thread_p : false;
6297 : 286 : enum c_storage_class storage_class = (scspecs
6298 : : ? scspecs->storage_class
6299 : : : csc_none);
6300 : :
6301 : 898082 : if (type == error_mark_node
6302 : 898066 : || init == error_mark_node)
6303 : : return error_mark_node;
6304 : :
6305 : 898013 : if (current_scope == file_scope && storage_class == csc_register)
6306 : : {
6307 : 1 : error_at (loc, "file-scope compound literal specifies %<register%>");
6308 : 1 : storage_class = csc_none;
6309 : : }
6310 : :
6311 : 898013 : if (current_scope != file_scope && threadp && storage_class == csc_none)
6312 : : {
6313 : 2 : error_at (loc, "compound literal implicitly auto and declared %qs",
6314 : 1 : scspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6315 : 1 : threadp = false;
6316 : : }
6317 : :
6318 : 898013 : decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
6319 : 898013 : DECL_EXTERNAL (decl) = 0;
6320 : 898013 : TREE_PUBLIC (decl) = 0;
6321 : 1796026 : TREE_STATIC (decl) = (current_scope == file_scope
6322 : 898013 : || storage_class == csc_static);
6323 : 898013 : DECL_CONTEXT (decl) = current_function_decl;
6324 : 898013 : TREE_USED (decl) = 1;
6325 : 898013 : DECL_READ_P (decl) = 1;
6326 : 898013 : DECL_ARTIFICIAL (decl) = 1;
6327 : 898013 : DECL_IGNORED_P (decl) = 1;
6328 : 898013 : C_DECL_COMPOUND_LITERAL_P (decl) = 1;
6329 : 1795797 : C_DECL_DECLARED_CONSTEXPR (decl) = scspecs && scspecs->constexpr_p;
6330 : 898013 : TREE_TYPE (decl) = type;
6331 : 898013 : if (threadp)
6332 : 16 : set_decl_tls_model (decl, decl_default_tls_model (decl));
6333 : 898013 : if (storage_class == csc_register)
6334 : : {
6335 : 10 : C_DECL_REGISTER (decl) = 1;
6336 : 10 : DECL_REGISTER (decl) = 1;
6337 : : }
6338 : 898013 : c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
6339 : 898013 : if (alignas_align)
6340 : : {
6341 : 3 : SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6342 : 3 : DECL_USER_ALIGN (decl) = 1;
6343 : : }
6344 : 898013 : store_init_value (loc, decl, init, NULL_TREE);
6345 : 898013 : if (current_scope != file_scope
6346 : 897766 : && TREE_STATIC (decl)
6347 : 26 : && !TREE_READONLY (decl)
6348 : 15 : && DECL_DECLARED_INLINE_P (current_function_decl)
6349 : 898019 : && DECL_EXTERNAL (current_function_decl))
6350 : 4 : record_inline_static (input_location, current_function_decl,
6351 : : decl, csi_modifiable);
6352 : :
6353 : 898013 : if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
6354 : : {
6355 : 251 : int failure = complete_array_type (&TREE_TYPE (decl),
6356 : 251 : DECL_INITIAL (decl), true);
6357 : : /* If complete_array_type returns 3, it means that the initial value of
6358 : : the compound literal is empty. Allow it with a pedwarn; in pre-C23
6359 : : modes, the empty initializer itself has been diagnosed if pedantic so
6360 : : does not need to be diagnosed again here. */
6361 : 251 : gcc_assert (failure == 0 || failure == 3);
6362 : 251 : if (failure == 3 && flag_isoc23)
6363 : 1 : pedwarn (loc, OPT_Wpedantic,
6364 : : "array of unknown size with empty initializer");
6365 : :
6366 : 251 : type = TREE_TYPE (decl);
6367 : 251 : TREE_TYPE (DECL_INITIAL (decl)) = type;
6368 : : }
6369 : :
6370 : 898013 : if (type == error_mark_node || !COMPLETE_TYPE_P (type))
6371 : : {
6372 : 19 : c_incomplete_type_error (loc, NULL_TREE, type);
6373 : 19 : return error_mark_node;
6374 : : }
6375 : :
6376 : 897994 : if (TREE_STATIC (decl)
6377 : 897994 : && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
6378 : 0 : return error_mark_node;
6379 : :
6380 : 897994 : stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
6381 : 897994 : complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
6382 : 897994 : TREE_SIDE_EFFECTS (complit) = 1;
6383 : :
6384 : 897994 : layout_decl (decl, 0);
6385 : :
6386 : 897994 : if (TREE_STATIC (decl))
6387 : : {
6388 : : /* This decl needs a name for the assembler output. */
6389 : 273 : set_compound_literal_name (decl);
6390 : 273 : DECL_DEFER_OUTPUT (decl) = 1;
6391 : 273 : DECL_COMDAT (decl) = 1;
6392 : 273 : pushdecl (decl);
6393 : 273 : rest_of_decl_compilation (decl, 1, 0);
6394 : : }
6395 : 897721 : else if (current_function_decl && !current_scope->parm_flag)
6396 : 897712 : pushdecl (decl);
6397 : :
6398 : 897994 : if (non_const)
6399 : : {
6400 : 193 : complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
6401 : 193 : C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
6402 : : }
6403 : :
6404 : : return complit;
6405 : : }
6406 : :
6407 : : /* Check the type of a compound literal. Here we just check that it
6408 : : is valid for C++. */
6409 : :
6410 : : void
6411 : 898082 : check_compound_literal_type (location_t loc, struct c_type_name *type_name)
6412 : : {
6413 : 898082 : if (warn_cxx_compat
6414 : 165 : && (type_name->specs->typespec_kind == ctsk_tagdef
6415 : 164 : || type_name->specs->typespec_kind == ctsk_tagfirstref
6416 : 163 : || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
6417 : 2 : warning_at (loc, OPT_Wc___compat,
6418 : : "defining a type in a compound literal is invalid in C++");
6419 : 898082 : }
6420 : :
6421 : : /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6422 : : replacing with appropriate values if they are invalid. */
6423 : :
6424 : : static void
6425 : 51878 : check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
6426 : : tree orig_name)
6427 : : {
6428 : 51878 : tree type_mv;
6429 : 51878 : unsigned int max_width;
6430 : 51878 : unsigned HOST_WIDE_INT w;
6431 : 51878 : const char *name = (orig_name
6432 : 94570 : ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
6433 : 9186 : : _("<anonymous>"));
6434 : :
6435 : : /* Detect and ignore out of range field width and process valid
6436 : : field widths. */
6437 : 51878 : if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
6438 : : {
6439 : 4 : error_at (loc, "bit-field %qs width not an integer constant", name);
6440 : 4 : *width = integer_one_node;
6441 : : }
6442 : : else
6443 : : {
6444 : 51874 : if (TREE_CODE (*width) != INTEGER_CST)
6445 : : {
6446 : 12 : *width = c_fully_fold (*width, false, NULL);
6447 : 12 : if (TREE_CODE (*width) == INTEGER_CST)
6448 : 6 : pedwarn (loc, OPT_Wpedantic,
6449 : : "bit-field %qs width not an integer constant expression",
6450 : : name);
6451 : : }
6452 : 51874 : if (TREE_CODE (*width) != INTEGER_CST)
6453 : : {
6454 : 6 : error_at (loc, "bit-field %qs width not an integer constant", name);
6455 : 6 : *width = integer_one_node;
6456 : : }
6457 : 51874 : constant_expression_warning (*width);
6458 : 51874 : if (tree_int_cst_sgn (*width) < 0)
6459 : : {
6460 : 2 : error_at (loc, "negative width in bit-field %qs", name);
6461 : 2 : *width = integer_one_node;
6462 : : }
6463 : 51872 : else if (integer_zerop (*width) && orig_name)
6464 : : {
6465 : 4 : error_at (loc, "zero width for bit-field %qs", name);
6466 : 4 : *width = integer_one_node;
6467 : : }
6468 : : }
6469 : :
6470 : : /* Detect invalid bit-field type. */
6471 : 51878 : if (TREE_CODE (*type) != INTEGER_TYPE
6472 : : && TREE_CODE (*type) != BOOLEAN_TYPE
6473 : : && TREE_CODE (*type) != ENUMERAL_TYPE
6474 : 51878 : && TREE_CODE (*type) != BITINT_TYPE)
6475 : : {
6476 : 6 : error_at (loc, "bit-field %qs has invalid type", name);
6477 : 6 : *type = unsigned_type_node;
6478 : : }
6479 : :
6480 : 51878 : if (TYPE_WARN_IF_NOT_ALIGN (*type))
6481 : : {
6482 : 1 : error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6483 : : name);
6484 : 1 : *type = unsigned_type_node;
6485 : : }
6486 : :
6487 : 51878 : type_mv = TYPE_MAIN_VARIANT (*type);
6488 : 51878 : if (!in_system_header_at (input_location)
6489 : 29363 : && type_mv != integer_type_node
6490 : 26633 : && type_mv != unsigned_type_node
6491 : 64483 : && type_mv != boolean_type_node)
6492 : 12023 : pedwarn_c90 (loc, OPT_Wpedantic,
6493 : : "type of bit-field %qs is a GCC extension", name);
6494 : :
6495 : 51878 : max_width = TYPE_PRECISION (*type);
6496 : :
6497 : 51878 : if (compare_tree_int (*width, max_width) > 0)
6498 : : {
6499 : 4 : error_at (loc, "width of %qs exceeds its type", name);
6500 : 4 : w = max_width;
6501 : 4 : *width = build_int_cst (integer_type_node, w);
6502 : : }
6503 : : else
6504 : 51874 : w = tree_to_uhwi (*width);
6505 : :
6506 : : /* Truncation of hardbool false and true representation values is always safe:
6507 : : either the values remain different, or we'll report a problem when creating
6508 : : the narrower type. */
6509 : 51878 : if (c_hardbool_type_attr (*type))
6510 : 51878 : return;
6511 : :
6512 : 51597 : if (TREE_CODE (*type) == ENUMERAL_TYPE)
6513 : : {
6514 : 4130 : struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
6515 : 4130 : if (!lt
6516 : 4120 : || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
6517 : 8246 : || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
6518 : 15 : warning_at (loc, 0, "%qs is narrower than values of its type", name);
6519 : : }
6520 : : }
6521 : :
6522 : :
6523 : :
6524 : : /* Print warning about variable length array if necessary. */
6525 : :
6526 : : static void
6527 : 22409 : warn_variable_length_array (tree name, tree size)
6528 : : {
6529 : 22409 : if (TREE_CONSTANT (size))
6530 : : {
6531 : 180 : if (name)
6532 : 168 : pedwarn_c90 (input_location, OPT_Wvla,
6533 : : "ISO C90 forbids array %qE whose size "
6534 : : "cannot be evaluated", name);
6535 : : else
6536 : 12 : pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
6537 : : "whose size cannot be evaluated");
6538 : : }
6539 : : else
6540 : : {
6541 : 22229 : if (name)
6542 : 13762 : pedwarn_c90 (input_location, OPT_Wvla,
6543 : : "ISO C90 forbids variable length array %qE", name);
6544 : : else
6545 : 8467 : pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
6546 : : "length array");
6547 : : }
6548 : 22409 : }
6549 : :
6550 : : /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6551 : : considering only those c_declspec_words found in LIST, which
6552 : : must be terminated by cdw_number_of_elements. */
6553 : :
6554 : : static location_t
6555 : 235 : smallest_type_quals_location (const location_t *locations,
6556 : : const c_declspec_word *list)
6557 : : {
6558 : 235 : location_t loc = UNKNOWN_LOCATION;
6559 : 1410 : while (*list != cdw_number_of_elements)
6560 : : {
6561 : 1175 : location_t newloc = locations[*list];
6562 : 1175 : if (loc == UNKNOWN_LOCATION
6563 : 288 : || (newloc != UNKNOWN_LOCATION && newloc < loc))
6564 : 887 : loc = newloc;
6565 : 1175 : list++;
6566 : : }
6567 : :
6568 : 235 : return loc;
6569 : : }
6570 : :
6571 : :
6572 : : /* We attach an artificial TYPE_DECL to pointed-to type
6573 : : and arrange for it to be included in a DECL_EXPR. This
6574 : : forces the sizes evaluation at a safe point and ensures it
6575 : : is not deferred until e.g. within a deeper conditional context.
6576 : :
6577 : : PARM contexts have no enclosing statement list that
6578 : : can hold the DECL_EXPR, so we need to use a BIND_EXPR
6579 : : instead, and add it to the list of expressions that
6580 : : need to be evaluated.
6581 : :
6582 : : TYPENAME contexts do have an enclosing statement list,
6583 : : but it would be incorrect to use it, as the size should
6584 : : only be evaluated if the containing expression is
6585 : : evaluated. We might also be in the middle of an
6586 : : expression with side effects on the pointed-to type size
6587 : : "arguments" prior to the pointer declaration point and
6588 : : the fake TYPE_DECL in the enclosing context would force
6589 : : the size evaluation prior to the side effects. We therefore
6590 : : use BIND_EXPRs in TYPENAME contexts too. */
6591 : : static void
6592 : 6972 : add_decl_expr (location_t loc, tree type, tree *expr, bool set_name_p)
6593 : : {
6594 : 6972 : tree bind = NULL_TREE;
6595 : 6972 : if (expr)
6596 : : {
6597 : 1543 : bind = build3 (BIND_EXPR, void_type_node, NULL_TREE, NULL_TREE,
6598 : : NULL_TREE);
6599 : 1543 : TREE_SIDE_EFFECTS (bind) = 1;
6600 : 1543 : BIND_EXPR_BODY (bind) = push_stmt_list ();
6601 : 1543 : push_scope ();
6602 : : }
6603 : :
6604 : 6972 : tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6605 : 6972 : pushdecl (decl);
6606 : 6972 : DECL_ARTIFICIAL (decl) = 1;
6607 : 6972 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6608 : 6972 : if (set_name_p)
6609 : 6307 : TYPE_NAME (type) = decl;
6610 : :
6611 : 6972 : if (bind)
6612 : : {
6613 : 1543 : pop_scope ();
6614 : 1543 : BIND_EXPR_BODY (bind) = pop_stmt_list (BIND_EXPR_BODY (bind));
6615 : 1543 : if (*expr)
6616 : 1449 : *expr = build2 (COMPOUND_EXPR, void_type_node, *expr, bind);
6617 : : else
6618 : 94 : *expr = bind;
6619 : : }
6620 : 6972 : }
6621 : :
6622 : :
6623 : : /* Add attribute "arg spec" to ATTRS corresponding to an array/VLA parameter
6624 : : declared with type TYPE. The attribute has two arguments. The first is
6625 : : a string that encodes the presence of the static keyword. The second is
6626 : : the declared type of the array before adjustment, i.e. as an array type
6627 : : including the outermost bound. */
6628 : :
6629 : : static tree
6630 : 430167 : build_arg_spec_attribute (tree type, bool static_p, tree attrs)
6631 : : {
6632 : 430167 : tree vbchain = tree_cons (NULL_TREE, type, NULL_TREE);
6633 : 430167 : tree acsstr = static_p ? build_string (7, "static") :
6634 : 430045 : build_string (1, "");
6635 : 430167 : tree args = tree_cons (NULL_TREE, acsstr, vbchain);
6636 : 430167 : tree name = get_identifier ("arg spec");
6637 : 430167 : return tree_cons (name, args, attrs);
6638 : : }
6639 : :
6640 : :
6641 : : /* Given declspecs and a declarator,
6642 : : determine the name and type of the object declared
6643 : : and construct a ..._DECL node for it.
6644 : : (In one case we can return a ..._TYPE node instead.
6645 : : For invalid input we sometimes return NULL_TREE.)
6646 : :
6647 : : DECLSPECS is a c_declspecs structure for the declaration specifiers.
6648 : :
6649 : : DECL_CONTEXT says which syntactic context this declaration is in:
6650 : : NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6651 : : FUNCDEF for a function definition. Like NORMAL but a few different
6652 : : error messages in each case. Return value may be zero meaning
6653 : : this definition is too screwy to try to parse.
6654 : : PARM for a parameter declaration (either within a function prototype
6655 : : or before a function body). Make a PARM_DECL, or return void_type_node.
6656 : : TYPENAME if for a typename (in a cast or sizeof).
6657 : : Don't make a DECL node; just return the ..._TYPE node.
6658 : : FIELD for a struct or union field; make a FIELD_DECL.
6659 : : INITIALIZED is true if the decl has an initializer.
6660 : : WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6661 : : representing the width of the bit-field.
6662 : : DECL_ATTRS points to the list of attributes that should be added to this
6663 : : decl. Any nested attributes that belong on the decl itself will be
6664 : : added to this list.
6665 : : If EXPR is not NULL, any expressions that need to be evaluated as
6666 : : part of evaluating variably modified types will be stored in *EXPR.
6667 : : If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6668 : : set to indicate whether operands in *EXPR can be used in constant
6669 : : expressions.
6670 : : DEPRECATED_STATE is a deprecated_states value indicating whether
6671 : : deprecation/unavailability warnings should be suppressed.
6672 : :
6673 : : In the TYPENAME case, DECLARATOR is really an absolute declarator.
6674 : : It may also be so in the PARM case, for a prototype where the
6675 : : argument type is specified but not the name.
6676 : :
6677 : : This function is where the complicated C meanings of `static'
6678 : : and `extern' are interpreted. */
6679 : :
6680 : : static tree
6681 : 306587737 : grokdeclarator (const struct c_declarator *declarator,
6682 : : struct c_declspecs *declspecs,
6683 : : enum decl_context decl_context, bool initialized, tree *width,
6684 : : tree *decl_attrs, tree *expr, bool *expr_const_operands,
6685 : : enum deprecated_states deprecated_state)
6686 : : {
6687 : 306587737 : tree type = declspecs->type;
6688 : 306587737 : bool threadp = declspecs->thread_p;
6689 : 306587737 : bool constexprp = declspecs->constexpr_p;
6690 : 306587737 : enum c_storage_class storage_class = declspecs->storage_class;
6691 : 306587737 : int constp;
6692 : 306587737 : int restrictp;
6693 : 306587737 : int volatilep;
6694 : 306587737 : int atomicp;
6695 : 306587737 : int type_quals = TYPE_UNQUALIFIED;
6696 : 306587737 : tree name = NULL_TREE;
6697 : 306587737 : bool funcdef_flag = false;
6698 : 306587737 : bool funcdef_syntax = false;
6699 : 306587737 : bool size_varies = false;
6700 : 306587737 : bool size_error = false;
6701 : 306587737 : tree decl_attr = declspecs->decl_attr;
6702 : 306587737 : int array_ptr_quals = TYPE_UNQUALIFIED;
6703 : 306587737 : tree array_ptr_attrs = NULL_TREE;
6704 : 306587737 : bool array_parm_static = false;
6705 : 306587737 : bool array_parm_vla_unspec_p = false;
6706 : 306587737 : tree returned_attrs = NULL_TREE;
6707 : 306587737 : tree decl_id_attrs = NULL_TREE;
6708 : 306587737 : bool bitfield = width != NULL;
6709 : 306587737 : tree element_type;
6710 : 306587737 : tree orig_qual_type = NULL;
6711 : 306587737 : size_t orig_qual_indirect = 0;
6712 : 306587737 : struct c_arg_info *arg_info = 0;
6713 : 306587737 : addr_space_t as1, as2, address_space;
6714 : 306587737 : location_t loc = UNKNOWN_LOCATION;
6715 : 306587737 : tree expr_dummy;
6716 : 306587737 : bool expr_const_operands_dummy;
6717 : 306587737 : enum c_declarator_kind first_non_attr_kind;
6718 : 306587737 : unsigned int alignas_align = 0;
6719 : :
6720 : 306587737 : if (type == NULL_TREE)
6721 : : {
6722 : : /* This can occur for auto on a parameter in C23 mode. Set a
6723 : : dummy type here so subsequent code can give diagnostics for
6724 : : this case. */
6725 : 2 : gcc_assert (declspecs->c23_auto_p);
6726 : 2 : gcc_assert (decl_context == PARM);
6727 : 2 : type = declspecs->type = integer_type_node;
6728 : : }
6729 : 306587737 : if (TREE_CODE (type) == ERROR_MARK)
6730 : 29 : return error_mark_node;
6731 : 306587708 : if (expr == NULL)
6732 : : {
6733 : 37750 : expr = &expr_dummy;
6734 : 37750 : expr_dummy = NULL_TREE;
6735 : : }
6736 : 306587708 : if (expr_const_operands == NULL)
6737 : 188269892 : expr_const_operands = &expr_const_operands_dummy;
6738 : :
6739 : 306587708 : if (declspecs->expr)
6740 : : {
6741 : 912 : if (*expr)
6742 : 7 : *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6743 : : declspecs->expr);
6744 : : else
6745 : 905 : *expr = declspecs->expr;
6746 : : }
6747 : 306587708 : *expr_const_operands = declspecs->expr_const_operands;
6748 : :
6749 : 306587708 : if (decl_context == FUNCDEF)
6750 : 35382641 : funcdef_flag = true, decl_context = NORMAL;
6751 : :
6752 : : /* Look inside a declarator for the name being declared
6753 : : and get it as an IDENTIFIER_NODE, for an error message. */
6754 : 306587708 : {
6755 : 306587708 : const struct c_declarator *decl = declarator;
6756 : :
6757 : 306587708 : first_non_attr_kind = cdk_attrs;
6758 : 375061461 : while (decl)
6759 : 375061461 : switch (decl->kind)
6760 : : {
6761 : 1130396 : case cdk_array:
6762 : 1130396 : loc = decl->id_loc;
6763 : : /* FALL THRU. */
6764 : :
6765 : 68467098 : case cdk_function:
6766 : 68467098 : case cdk_pointer:
6767 : 68467098 : funcdef_syntax = (decl->kind == cdk_function);
6768 : 68467098 : if (first_non_attr_kind == cdk_attrs)
6769 : 66389495 : first_non_attr_kind = decl->kind;
6770 : 68467098 : decl = decl->declarator;
6771 : 68467098 : break;
6772 : :
6773 : 6655 : case cdk_attrs:
6774 : 6655 : decl = decl->declarator;
6775 : 6655 : break;
6776 : :
6777 : 306587708 : case cdk_id:
6778 : 306587708 : loc = decl->id_loc;
6779 : 306587708 : if (decl->u.id.id)
6780 : : name = decl->u.id.id;
6781 : 306587708 : decl_id_attrs = decl->u.id.attrs;
6782 : 306587708 : if (first_non_attr_kind == cdk_attrs)
6783 : 240198213 : first_non_attr_kind = decl->kind;
6784 : : decl = 0;
6785 : : break;
6786 : :
6787 : 0 : default:
6788 : 0 : gcc_unreachable ();
6789 : : }
6790 : 306587708 : if (name == NULL_TREE)
6791 : : {
6792 : 122880010 : gcc_assert (decl_context == PARM
6793 : : || decl_context == TYPENAME
6794 : : || (decl_context == FIELD
6795 : : && declarator->kind == cdk_id));
6796 : 122880010 : gcc_assert (!initialized);
6797 : : }
6798 : : }
6799 : :
6800 : : /* An enum type specifier (": specifier-qualifier-list") may only be
6801 : : specified when the enum is being defined or in an empty
6802 : : declaration of the form "enum identifier enum-type-specifier;".
6803 : : Except for the case of an empty declaration that has additional
6804 : : declaration specifiers, all invalid contexts (declarations that
6805 : : aren't empty, type names, parameter declarations, member
6806 : : declarations) pass through grokdeclarator. */
6807 : 306587708 : if (declspecs->enum_type_specifier_ref_p)
6808 : 6 : error_at (loc, "%<enum%> underlying type may not be specified here");
6809 : :
6810 : : /* A function definition's declarator must have the form of
6811 : : a function declarator. */
6812 : :
6813 : 306587708 : if (funcdef_flag && !funcdef_syntax)
6814 : : return NULL_TREE;
6815 : :
6816 : : /* If this looks like a function definition, make it one,
6817 : : even if it occurs where parms are expected.
6818 : : Then store_parm_decls will reject it and not use it as a parm. */
6819 : 306587677 : if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
6820 : 22656 : decl_context = PARM;
6821 : :
6822 : 306587677 : if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
6823 : : {
6824 : 306587653 : if (declspecs->unavailable_p)
6825 : 28 : error_unavailable_use (declspecs->type, declspecs->decl_attr);
6826 : 306587625 : else if (declspecs->deprecated_p
6827 : 46 : && deprecated_state != DEPRECATED_SUPPRESS)
6828 : 38 : warn_deprecated_use (declspecs->type, declspecs->decl_attr);
6829 : : }
6830 : :
6831 : 306587677 : if ((decl_context == NORMAL || decl_context == FIELD)
6832 : 67055820 : && current_scope == file_scope
6833 : 365749122 : && c_type_variably_modified_p (type))
6834 : : {
6835 : 3 : if (name)
6836 : 3 : error_at (loc, "variably modified %qE at file scope", name);
6837 : : else
6838 : 0 : error_at (loc, "variably modified field at file scope");
6839 : 3 : type = integer_type_node;
6840 : : }
6841 : :
6842 : 306587677 : size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
6843 : :
6844 : : /* Diagnose defaulting to "int". */
6845 : :
6846 : 306587677 : if (declspecs->default_int_p)
6847 : : {
6848 : : /* Issue a warning if this is an ISO C 99 program or if
6849 : : -Wreturn-type and this is a function, or if -Wimplicit;
6850 : : prefer the former warning since it is more explicit. */
6851 : 9763 : if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
6852 : 1228 : && funcdef_flag)
6853 : 702 : warn_about_return_type = 1;
6854 : : else
6855 : : {
6856 : 9061 : if (name)
6857 : 9051 : permerror_opt (loc, OPT_Wimplicit_int,
6858 : : "type defaults to %<int%> in declaration "
6859 : : "of %qE", name);
6860 : : else
6861 : 10 : permerror_opt (loc, OPT_Wimplicit_int,
6862 : : "type defaults to %<int%> in type name");
6863 : : }
6864 : : }
6865 : :
6866 : : /* Adjust the type if a bit-field is being declared,
6867 : : -funsigned-bitfields applied and the type is not explicitly
6868 : : "signed". */
6869 : 306587677 : if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
6870 : 40 : && TREE_CODE (type) == INTEGER_TYPE)
6871 : 38 : type = c_common_unsigned_type (type);
6872 : :
6873 : : /* Figure out the type qualifiers for the declaration. There are
6874 : : two ways a declaration can become qualified. One is something
6875 : : like `const int i' where the `const' is explicit. Another is
6876 : : something like `typedef const int CI; CI i' where the type of the
6877 : : declaration contains the `const'. A third possibility is that
6878 : : there is a type qualifier on the element type of a typedefed
6879 : : array type, in which case we should extract that qualifier so
6880 : : that c_apply_type_quals_to_decl receives the full list of
6881 : : qualifiers to work with (C90 is not entirely clear about whether
6882 : : duplicate qualifiers should be diagnosed in this case, but it
6883 : : seems most appropriate to do so). */
6884 : 306587677 : element_type = strip_array_types (type);
6885 : 306587677 : constp = declspecs->const_p + TYPE_READONLY (element_type);
6886 : 306587677 : restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
6887 : 306587677 : volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
6888 : 306587677 : atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
6889 : 306587677 : as1 = declspecs->address_space;
6890 : 306587677 : as2 = TYPE_ADDR_SPACE (element_type);
6891 : 306587677 : address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
6892 : :
6893 : 306587677 : if (constp > 1)
6894 : 25 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
6895 : 306587677 : if (restrictp > 1)
6896 : 6 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
6897 : 306587677 : if (volatilep > 1)
6898 : 15 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
6899 : 306587677 : if (atomicp > 1)
6900 : 9 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
6901 : :
6902 : 306587677 : if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
6903 : 0 : error_at (loc, "conflicting named address spaces (%s vs %s)",
6904 : : c_addr_space_name (as1), c_addr_space_name (as2));
6905 : :
6906 : 306587677 : if ((TREE_CODE (type) == ARRAY_TYPE
6907 : 306414267 : || first_non_attr_kind == cdk_array)
6908 : 307650783 : && TYPE_QUALS (element_type))
6909 : : {
6910 : 73 : orig_qual_type = type;
6911 : 73 : type = c_build_qualified_type (type, TYPE_UNQUALIFIED);
6912 : : }
6913 : 306587677 : type_quals = ((constp ? TYPE_QUAL_CONST : 0)
6914 : 306587677 : | (restrictp ? TYPE_QUAL_RESTRICT : 0)
6915 : 306587677 : | (volatilep ? TYPE_QUAL_VOLATILE : 0)
6916 : 306587677 : | (atomicp ? TYPE_QUAL_ATOMIC : 0)
6917 : 306587677 : | ENCODE_QUAL_ADDR_SPACE (address_space));
6918 : 306587677 : if (type_quals != TYPE_QUALS (element_type))
6919 : 12540165 : orig_qual_type = NULL_TREE;
6920 : :
6921 : : /* Applying the _Atomic qualifier to an array type (through the use
6922 : : of typedefs or typeof) must be detected here. If the qualifier
6923 : : is introduced later, any appearance of applying it to an array is
6924 : : actually applying it to an element of that array. */
6925 : 306587677 : if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
6926 : 6 : error_at (loc, "%<_Atomic%>-qualified array type");
6927 : :
6928 : : /* Warn about storage classes that are invalid for certain
6929 : : kinds of declarations (parameters, typenames, etc.). */
6930 : :
6931 : 306587677 : if (funcdef_flag
6932 : 35382610 : && (threadp
6933 : : || constexprp
6934 : 35382608 : || storage_class == csc_auto
6935 : 35382608 : || storage_class == csc_register
6936 : 35382563 : || storage_class == csc_typedef))
6937 : : {
6938 : 47 : if (storage_class == csc_auto)
6939 : 42 : pedwarn (loc,
6940 : 81 : (current_scope == file_scope) ? 0 : OPT_Wpedantic,
6941 : : "function definition declared %<auto%>");
6942 : 50 : if (storage_class == csc_register)
6943 : 3 : error_at (loc, "function definition declared %<register%>");
6944 : 50 : if (storage_class == csc_typedef)
6945 : 3 : error_at (loc, "function definition declared %<typedef%>");
6946 : 50 : if (threadp)
6947 : 2 : error_at (loc, "function definition declared %qs",
6948 : 2 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6949 : 50 : threadp = false;
6950 : : /* The parser ensures a constexpr function definition never
6951 : : reaches here. */
6952 : 50 : gcc_assert (!constexprp);
6953 : 50 : if (storage_class == csc_auto
6954 : 50 : || storage_class == csc_register
6955 : : || storage_class == csc_typedef)
6956 : 55 : storage_class = csc_none;
6957 : : }
6958 : 306587627 : else if (decl_context != NORMAL && (storage_class != csc_none
6959 : 243767647 : || threadp
6960 : 243767064 : || constexprp
6961 : 243767062 : || declspecs->c23_auto_p))
6962 : : {
6963 : 587 : if (decl_context == PARM
6964 : 587 : && storage_class == csc_register
6965 : 568 : && !constexprp
6966 : 566 : && !declspecs->c23_auto_p)
6967 : : ;
6968 : : else
6969 : : {
6970 : 21 : switch (decl_context)
6971 : : {
6972 : 0 : case FIELD:
6973 : 0 : if (name)
6974 : 0 : error_at (loc, "storage class specified for structure "
6975 : : "field %qE", name);
6976 : : else
6977 : 0 : error_at (loc, "storage class specified for structure field");
6978 : : break;
6979 : 21 : case PARM:
6980 : 21 : if (name)
6981 : 7 : error_at (loc, "storage class specified for parameter %qE",
6982 : : name);
6983 : : else
6984 : 14 : error_at (loc, "storage class specified for unnamed parameter");
6985 : : break;
6986 : 0 : default:
6987 : 0 : error_at (loc, "storage class specified for typename");
6988 : 0 : break;
6989 : : }
6990 : 306587677 : storage_class = csc_none;
6991 : 306587677 : threadp = false;
6992 : 306587677 : constexprp = false;
6993 : : }
6994 : : }
6995 : 306587040 : else if (storage_class == csc_extern
6996 : 306587040 : && initialized
6997 : 34545597 : && !funcdef_flag)
6998 : : {
6999 : : /* 'extern' with initialization is invalid if not at file scope. */
7000 : 28 : if (current_scope == file_scope)
7001 : : {
7002 : : /* It is fine to have 'extern const' when compiling at C
7003 : : and C++ intersection. */
7004 : 19 : if (!(warn_cxx_compat && constp))
7005 : 18 : warning_at (loc, 0, "%qE initialized and declared %<extern%>",
7006 : : name);
7007 : : }
7008 : : else
7009 : 9 : error_at (loc, "%qE has both %<extern%> and initializer", name);
7010 : : }
7011 : 306587012 : else if (current_scope == file_scope)
7012 : : {
7013 : 60034608 : if (storage_class == csc_auto)
7014 : 7 : error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
7015 : : name);
7016 : 60034608 : if (pedantic && storage_class == csc_register)
7017 : 4 : pedwarn (input_location, OPT_Wpedantic,
7018 : : "file-scope declaration of %qE specifies %<register%>", name);
7019 : : }
7020 : : else
7021 : : {
7022 : 246552404 : if (storage_class == csc_extern && funcdef_flag)
7023 : 3 : error_at (loc, "nested function %qE declared %<extern%>", name);
7024 : 246552401 : else if (threadp && storage_class == csc_none)
7025 : : {
7026 : 12 : error_at (loc, "function-scope %qE implicitly auto and declared "
7027 : : "%qs", name,
7028 : 6 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7029 : 6 : threadp = false;
7030 : : }
7031 : : }
7032 : :
7033 : : /* Now figure out the structure of the declarator proper.
7034 : : Descend through it, creating more complex types, until we reach
7035 : : the declared identifier (or NULL_TREE, in an absolute declarator).
7036 : : At each stage we maintain an unqualified version of the type
7037 : : together with any qualifiers that should be applied to it with
7038 : : c_build_qualified_type; this way, array types including
7039 : : multidimensional array types are first built up in unqualified
7040 : : form and then the qualified form is created with
7041 : : TYPE_MAIN_VARIANT pointing to the unqualified form. */
7042 : :
7043 : 375061429 : while (declarator && declarator->kind != cdk_id)
7044 : : {
7045 : 68473752 : if (type == error_mark_node)
7046 : : {
7047 : 38 : declarator = declarator->declarator;
7048 : 38 : continue;
7049 : : }
7050 : :
7051 : : /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
7052 : : a cdk_pointer (for *...),
7053 : : a cdk_function (for ...(...)),
7054 : : a cdk_attrs (for nested attributes),
7055 : : or a cdk_id (for the name being declared
7056 : : or the place in an absolute declarator
7057 : : where the name was omitted).
7058 : : For the last case, we have just exited the loop.
7059 : :
7060 : : At this point, TYPE is the type of elements of an array,
7061 : : or for a function to return, or for a pointer to point to.
7062 : : After this sequence of ifs, TYPE is the type of the
7063 : : array or function or pointer, and DECLARATOR has had its
7064 : : outermost layer removed. */
7065 : :
7066 : 68473714 : if (array_ptr_quals != TYPE_UNQUALIFIED
7067 : 68473714 : || array_ptr_attrs != NULL_TREE
7068 : 68473714 : || array_parm_static)
7069 : : {
7070 : : /* Only the innermost declarator (making a parameter be of
7071 : : array type which is converted to pointer type)
7072 : : may have static or type qualifiers. */
7073 : 1 : error_at (loc, "static or type qualifiers in non-parameter array declarator");
7074 : 1 : array_ptr_quals = TYPE_UNQUALIFIED;
7075 : 1 : array_ptr_attrs = NULL_TREE;
7076 : 1 : array_parm_static = false;
7077 : : }
7078 : :
7079 : 68473714 : switch (declarator->kind)
7080 : : {
7081 : 6655 : case cdk_attrs:
7082 : 6655 : {
7083 : : /* A declarator with embedded attributes. */
7084 : 6655 : tree attrs = declarator->u.attrs;
7085 : 6655 : const struct c_declarator *inner_decl;
7086 : 6655 : int attr_flags = 0;
7087 : 6655 : declarator = declarator->declarator;
7088 : : /* Standard attribute syntax precisely defines what entity
7089 : : an attribute in each position appertains to, so only
7090 : : apply laxity about positioning to GNU attribute syntax.
7091 : : Standard attributes applied to a function or array
7092 : : declarator apply exactly to that type; standard
7093 : : attributes applied to the identifier apply to the
7094 : : declaration rather than to the type, and are specified
7095 : : using a cdk_id declarator rather than using
7096 : : cdk_attrs. */
7097 : 6655 : inner_decl = declarator;
7098 : 6655 : while (inner_decl->kind == cdk_attrs)
7099 : 0 : inner_decl = inner_decl->declarator;
7100 : 6655 : if (!cxx11_attribute_p (attrs))
7101 : : {
7102 : 6560 : if (inner_decl->kind == cdk_id)
7103 : : attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
7104 : : else if (inner_decl->kind == cdk_function)
7105 : : attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
7106 : : else if (inner_decl->kind == cdk_array)
7107 : 6655 : attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
7108 : : }
7109 : 6655 : attrs = c_warn_type_attributes (type, attrs);
7110 : 6655 : returned_attrs = decl_attributes (&type,
7111 : : chainon (returned_attrs, attrs),
7112 : : attr_flags);
7113 : 6655 : break;
7114 : : }
7115 : 1130386 : case cdk_array:
7116 : 1130386 : {
7117 : 1130386 : tree itype = NULL_TREE;
7118 : 1130386 : tree size = declarator->u.array.dimen;
7119 : : /* The index is a signed object `sizetype' bits wide. */
7120 : 1130386 : tree index_type = c_common_signed_type (sizetype);
7121 : :
7122 : 1130386 : array_ptr_quals = declarator->u.array.quals;
7123 : 1130386 : array_ptr_attrs = declarator->u.array.attrs;
7124 : 1130386 : array_parm_static = declarator->u.array.static_p;
7125 : 1130386 : array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
7126 : :
7127 : 1130386 : declarator = declarator->declarator;
7128 : :
7129 : : /* Check for some types that there cannot be arrays of. */
7130 : :
7131 : 1130386 : if (VOID_TYPE_P (type))
7132 : : {
7133 : 11 : if (name)
7134 : 9 : error_at (loc, "declaration of %qE as array of voids", name);
7135 : : else
7136 : 2 : error_at (loc, "declaration of type name as array of voids");
7137 : 11 : type = error_mark_node;
7138 : : }
7139 : :
7140 : 1130386 : if (TREE_CODE (type) == FUNCTION_TYPE)
7141 : : {
7142 : 3 : if (name)
7143 : 2 : error_at (loc, "declaration of %qE as array of functions",
7144 : : name);
7145 : : else
7146 : 1 : error_at (loc, "declaration of type name as array of "
7147 : : "functions");
7148 : 3 : type = error_mark_node;
7149 : : }
7150 : :
7151 : 21845 : if (pedantic && !in_system_header_at (input_location)
7152 : 1145062 : && flexible_array_type_p (type))
7153 : 20 : pedwarn (loc, OPT_Wpedantic,
7154 : : "invalid use of structure with flexible array member");
7155 : :
7156 : 1130386 : if (size == error_mark_node)
7157 : 119 : type = error_mark_node;
7158 : :
7159 : 1130386 : if (type == error_mark_node)
7160 : 133 : continue;
7161 : :
7162 : 1130253 : if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
7163 : : {
7164 : 0 : type = error_mark_node;
7165 : 0 : continue;
7166 : : }
7167 : :
7168 : : /* If size was specified, set ITYPE to a range-type for
7169 : : that size. Otherwise, ITYPE remains null. finish_decl
7170 : : may figure it out from an initial value. */
7171 : :
7172 : 1130253 : if (size)
7173 : : {
7174 : 961780 : bool size_maybe_const = true;
7175 : 961780 : bool size_int_const = (TREE_CODE (size) == INTEGER_CST
7176 : 961780 : && !TREE_OVERFLOW (size));
7177 : 961780 : bool this_size_varies = false;
7178 : :
7179 : : /* Strip NON_LVALUE_EXPRs since we aren't using as an
7180 : : lvalue. */
7181 : 961789 : STRIP_TYPE_NOPS (size);
7182 : :
7183 : 961780 : if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
7184 : : {
7185 : 16 : if (name)
7186 : 14 : error_at (loc, "size of array %qE has non-integer type",
7187 : : name);
7188 : : else
7189 : 2 : error_at (loc,
7190 : : "size of unnamed array has non-integer type");
7191 : 16 : size = integer_one_node;
7192 : 16 : size_int_const = true;
7193 : 16 : size_error = true;
7194 : : }
7195 : : /* This can happen with enum forward declaration. */
7196 : 961764 : else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
7197 : : {
7198 : 0 : if (name)
7199 : 0 : error_at (loc, "size of array %qE has incomplete type",
7200 : : name);
7201 : : else
7202 : 0 : error_at (loc, "size of unnamed array has incomplete "
7203 : : "type");
7204 : 0 : size = integer_one_node;
7205 : 0 : size_int_const = true;
7206 : 0 : size_error = true;
7207 : : }
7208 : :
7209 : 961780 : size = c_fully_fold (size, false, &size_maybe_const);
7210 : :
7211 : 961780 : if (pedantic && size_maybe_const && integer_zerop (size))
7212 : : {
7213 : 3 : if (name)
7214 : 3 : pedwarn (loc, OPT_Wpedantic,
7215 : : "ISO C forbids zero-size array %qE", name);
7216 : : else
7217 : 0 : pedwarn (loc, OPT_Wpedantic,
7218 : : "ISO C forbids zero-size array");
7219 : : }
7220 : :
7221 : 961780 : if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
7222 : : {
7223 : 939504 : constant_expression_warning (size);
7224 : 939504 : if (tree_int_cst_sgn (size) < 0)
7225 : : {
7226 : 712 : if (name)
7227 : 709 : error_at (loc, "size of array %qE is negative", name);
7228 : : else
7229 : 3 : error_at (loc, "size of unnamed array is negative");
7230 : 712 : size = integer_one_node;
7231 : 712 : size_int_const = true;
7232 : 712 : size_error = true;
7233 : : }
7234 : : /* Handle a size folded to an integer constant but
7235 : : not an integer constant expression. */
7236 : 939504 : if (!size_int_const)
7237 : : {
7238 : : /* If this is a file scope declaration of an
7239 : : ordinary identifier, this is invalid code;
7240 : : diagnosing it here and not subsequently
7241 : : treating the type as variable-length avoids
7242 : : more confusing diagnostics later. */
7243 : 154 : if ((decl_context == NORMAL || decl_context == FIELD)
7244 : 142 : && current_scope == file_scope)
7245 : 14 : pedwarn (input_location, 0,
7246 : : "variably modified %qE at file scope",
7247 : : name);
7248 : : else
7249 : : this_size_varies = size_varies = true;
7250 : 154 : warn_variable_length_array (name, size);
7251 : : }
7252 : : }
7253 : 22276 : else if ((decl_context == NORMAL || decl_context == FIELD)
7254 : 13197 : && current_scope == file_scope)
7255 : : {
7256 : 21 : error_at (loc, "variably modified %qE at file scope", name);
7257 : 21 : size = integer_one_node;
7258 : : }
7259 : : else
7260 : : {
7261 : : /* Make sure the array size remains visibly
7262 : : nonconstant even if it is (eg) a const variable
7263 : : with known value. */
7264 : 22255 : this_size_varies = size_varies = true;
7265 : 22255 : warn_variable_length_array (name, size);
7266 : 22255 : if (sanitize_flags_p (SANITIZE_VLA)
7267 : 178 : && current_function_decl != NULL_TREE
7268 : 22415 : && decl_context == NORMAL)
7269 : : {
7270 : : /* Evaluate the array size only once. */
7271 : 152 : size = save_expr (size);
7272 : 152 : size = c_fully_fold (size, false, NULL);
7273 : 152 : size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
7274 : : ubsan_instrument_vla (loc, size),
7275 : : size);
7276 : : }
7277 : : }
7278 : :
7279 : 961780 : if (integer_zerop (size) && !this_size_varies)
7280 : : {
7281 : : /* A zero-length array cannot be represented with
7282 : : an unsigned index type, which is what we'll
7283 : : get with build_index_type. Create an
7284 : : open-ended range instead. */
7285 : 2595 : itype = build_range_type (sizetype, size, NULL_TREE);
7286 : : }
7287 : : else
7288 : : {
7289 : : /* Arrange for the SAVE_EXPR on the inside of the
7290 : : MINUS_EXPR, which allows the -1 to get folded
7291 : : with the +1 that happens when building TYPE_SIZE. */
7292 : 959185 : if (size_varies)
7293 : 22765 : size = save_expr (size);
7294 : 959185 : if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
7295 : 140 : size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7296 : : integer_zero_node, size);
7297 : :
7298 : : /* Compute the maximum valid index, that is, size
7299 : : - 1. Do the calculation in index_type, so that
7300 : : if it is a variable the computations will be
7301 : : done in the proper mode. */
7302 : 959185 : itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
7303 : : convert (index_type, size),
7304 : : convert (index_type,
7305 : : size_one_node));
7306 : :
7307 : : /* The above overflows when size does not fit
7308 : : in index_type.
7309 : : ??? While a size of INT_MAX+1 technically shouldn't
7310 : : cause an overflow (because we subtract 1), handling
7311 : : this case seems like an unnecessary complication. */
7312 : 959185 : if (TREE_CODE (size) == INTEGER_CST
7313 : 936790 : && !int_fits_type_p (size, index_type))
7314 : : {
7315 : 6 : if (name)
7316 : 5 : error_at (loc, "size of array %qE is too large",
7317 : : name);
7318 : : else
7319 : 1 : error_at (loc, "size of unnamed array is too large");
7320 : 6 : type = error_mark_node;
7321 : 6 : continue;
7322 : : }
7323 : :
7324 : 959179 : itype = build_index_type (itype);
7325 : : }
7326 : 961774 : if (this_size_varies)
7327 : : {
7328 : 22395 : if (TREE_SIDE_EFFECTS (size))
7329 : : {
7330 : 22034 : if (*expr)
7331 : 8173 : *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7332 : : *expr, size);
7333 : : else
7334 : 13861 : *expr = size;
7335 : : }
7336 : 22395 : *expr_const_operands &= size_maybe_const;
7337 : : }
7338 : : }
7339 : 168473 : else if (decl_context == FIELD)
7340 : : {
7341 : 84591 : bool flexible_array_member = false;
7342 : 84591 : if (array_parm_vla_unspec_p)
7343 : : /* Field names can in fact have function prototype
7344 : : scope so [*] is disallowed here through making
7345 : : the field variably modified, not through being
7346 : : something other than a declaration with function
7347 : : prototype scope. */
7348 : : size_varies = true;
7349 : : else
7350 : : {
7351 : : const struct c_declarator *t = declarator;
7352 : 84589 : while (t->kind == cdk_attrs)
7353 : 0 : t = t->declarator;
7354 : 84589 : flexible_array_member = (t->kind == cdk_id);
7355 : : }
7356 : 84589 : if (flexible_array_member
7357 : 84589 : && !in_system_header_at (input_location))
7358 : 83558 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7359 : : "support flexible array members");
7360 : :
7361 : : /* ISO C99 Flexible array members are effectively
7362 : : identical to GCC's zero-length array extension. */
7363 : 84591 : if (flexible_array_member)
7364 : 84569 : itype = build_index_type (NULL_TREE);
7365 : : }
7366 : :
7367 : 1130247 : if (array_parm_vla_unspec_p)
7368 : : {
7369 : : /* C99 6.7.5.2p4 */
7370 : 142 : if (decl_context == TYPENAME)
7371 : 5 : warning (0, "%<[*]%> not in a declaration");
7372 : : size_varies = true;
7373 : : }
7374 : :
7375 : : /* Complain about arrays of incomplete types. */
7376 : 1130247 : if (!COMPLETE_TYPE_P (type))
7377 : : {
7378 : 57 : auto_diagnostic_group d;
7379 : 57 : error_at (loc, "array type has incomplete element type %qT",
7380 : : type);
7381 : : /* See if we can be more helpful. */
7382 : 57 : if (TREE_CODE (type) == ARRAY_TYPE)
7383 : : {
7384 : 29 : if (name)
7385 : 24 : inform (loc, "declaration of %qE as multidimensional "
7386 : : "array must have bounds for all dimensions "
7387 : : "except the first", name);
7388 : : else
7389 : 5 : inform (loc, "declaration of multidimensional array "
7390 : : "must have bounds for all dimensions except "
7391 : : "the first");
7392 : : }
7393 : 57 : type = error_mark_node;
7394 : 57 : }
7395 : : else
7396 : : /* When itype is NULL, a shared incomplete array type is
7397 : : returned for all array of a given type. Elsewhere we
7398 : : make sure we don't complete that type before copying
7399 : : it, but here we want to make sure we don't ever
7400 : : modify the shared type, so we gcc_assert (itype)
7401 : : below. */
7402 : : {
7403 : 1130190 : addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
7404 : 1130190 : if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
7405 : 0 : type = c_build_qualified_type (type,
7406 : : ENCODE_QUAL_ADDR_SPACE (as));
7407 : 1130190 : if (array_parm_vla_unspec_p)
7408 : 142 : type = c_build_array_type_unspecified (type);
7409 : : else
7410 : 1130048 : type = c_build_array_type (type, itype);
7411 : : }
7412 : :
7413 : 1130247 : if (type != error_mark_node)
7414 : : {
7415 : : /* The GCC extension for zero-length arrays differs from
7416 : : ISO flexible array members in that sizeof yields
7417 : : zero. */
7418 : 1130190 : if (size && integer_zerop (size))
7419 : : {
7420 : 2588 : gcc_assert (itype);
7421 : 2588 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7422 : 2588 : TYPE_SIZE (type) = bitsize_zero_node;
7423 : 2588 : TYPE_SIZE_UNIT (type) = size_zero_node;
7424 : 2588 : SET_TYPE_STRUCTURAL_EQUALITY (type);
7425 : : }
7426 : :
7427 : 1130190 : if (!valid_array_size_p (loc, type, name))
7428 : 33 : type = error_mark_node;
7429 : : }
7430 : :
7431 : 1130247 : if (decl_context != PARM
7432 : 816574 : && (array_ptr_quals != TYPE_UNQUALIFIED
7433 : 816574 : || array_ptr_attrs != NULL_TREE
7434 : 816573 : || array_parm_static))
7435 : : {
7436 : 2 : error_at (loc, "static or type qualifiers in non-parameter "
7437 : : "array declarator");
7438 : 2 : array_ptr_quals = TYPE_UNQUALIFIED;
7439 : 2 : array_ptr_attrs = NULL_TREE;
7440 : 2 : array_parm_static = false;
7441 : : }
7442 : 1130247 : orig_qual_indirect++;
7443 : 1130247 : break;
7444 : : }
7445 : 49429463 : case cdk_function:
7446 : 49429463 : {
7447 : : /* Say it's a definition only for the declarator closest
7448 : : to the identifier, apart possibly from some
7449 : : attributes. */
7450 : 49429463 : bool really_funcdef = false;
7451 : 49429463 : tree arg_types;
7452 : 49429463 : orig_qual_type = NULL_TREE;
7453 : 49429463 : if (funcdef_flag)
7454 : : {
7455 : 35382632 : const struct c_declarator *t = declarator->declarator;
7456 : 35382650 : while (t->kind == cdk_attrs)
7457 : 18 : t = t->declarator;
7458 : 35382632 : really_funcdef = (t->kind == cdk_id);
7459 : : }
7460 : :
7461 : : /* Declaring a function type. Make sure we have a valid
7462 : : type for the function to return. */
7463 : 49429463 : if (type == error_mark_node)
7464 : 0 : continue;
7465 : :
7466 : 49429463 : size_varies = false;
7467 : :
7468 : : /* Warn about some types functions can't return. */
7469 : 49429463 : if (TREE_CODE (type) == FUNCTION_TYPE)
7470 : : {
7471 : 0 : if (name)
7472 : 0 : error_at (loc, "%qE declared as function returning a "
7473 : : "function", name);
7474 : : else
7475 : 0 : error_at (loc, "type name declared as function "
7476 : : "returning a function");
7477 : 0 : type = integer_type_node;
7478 : : }
7479 : 49429463 : if (TREE_CODE (type) == ARRAY_TYPE)
7480 : : {
7481 : 0 : if (name)
7482 : 0 : error_at (loc, "%qE declared as function returning an array",
7483 : : name);
7484 : : else
7485 : 0 : error_at (loc, "type name declared as function returning "
7486 : : "an array");
7487 : 0 : type = integer_type_node;
7488 : : }
7489 : :
7490 : : /* Construct the function type and go to the next
7491 : : inner layer of declarator. */
7492 : 49429463 : arg_info = declarator->u.arg_info;
7493 : 49429463 : arg_types = grokparms (arg_info, really_funcdef);
7494 : :
7495 : : /* Type qualifiers before the return type of the function
7496 : : qualify the return type, not the function type. */
7497 : 49429463 : if (type_quals)
7498 : : {
7499 : 235 : const enum c_declspec_word ignored_quals_list[] =
7500 : : {
7501 : : cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
7502 : : cdw_atomic, cdw_number_of_elements
7503 : : };
7504 : 235 : location_t specs_loc
7505 : 235 : = smallest_type_quals_location (declspecs->locations,
7506 : : ignored_quals_list);
7507 : 235 : if (specs_loc == UNKNOWN_LOCATION)
7508 : 139 : specs_loc = declspecs->locations[cdw_typedef];
7509 : 139 : if (specs_loc == UNKNOWN_LOCATION)
7510 : 15 : specs_loc = loc;
7511 : :
7512 : : /* Type qualifiers on a function return type are
7513 : : normally permitted by the standard but have no
7514 : : effect, so give a warning at -Wreturn-type.
7515 : : Qualifiers on a void return type are banned on
7516 : : function definitions in ISO C; GCC used to used
7517 : : them for noreturn functions. The resolution of C11
7518 : : DR#423 means qualifiers (other than _Atomic) are
7519 : : actually removed from the return type when
7520 : : determining the function type. For C23, _Atomic is
7521 : : removed as well. */
7522 : 235 : int quals_used = type_quals;
7523 : 235 : if (flag_isoc23)
7524 : : quals_used = 0;
7525 : 65 : else if (flag_isoc11)
7526 : 31 : quals_used &= TYPE_QUAL_ATOMIC;
7527 : 65 : if (quals_used && VOID_TYPE_P (type) && really_funcdef)
7528 : 5 : pedwarn (specs_loc, 0,
7529 : : "function definition has qualified void "
7530 : : "return type");
7531 : : else
7532 : 230 : warning_at (specs_loc, OPT_Wignored_qualifiers,
7533 : : "type qualifiers ignored on function "
7534 : : "return type");
7535 : :
7536 : : /* Ensure an error for restrict on invalid types; the
7537 : : DR#423 resolution is not entirely clear about
7538 : : this. */
7539 : 235 : if (flag_isoc11
7540 : 201 : && (type_quals & TYPE_QUAL_RESTRICT)
7541 : 241 : && (!POINTER_TYPE_P (type)
7542 : 4 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
7543 : 4 : error_at (loc, "invalid use of %<restrict%>");
7544 : 235 : type = c_build_qualified_type (type, quals_used);
7545 : : }
7546 : 49429463 : type_quals = TYPE_UNQUALIFIED;
7547 : :
7548 : 98858926 : type = c_build_function_type (type, arg_types,
7549 : 49429463 : arg_info->no_named_args_stdarg_p);
7550 : 49429463 : declarator = declarator->declarator;
7551 : :
7552 : : /* Set the TYPE_CONTEXTs for each tagged type which is local to
7553 : : the formal parameter list of this FUNCTION_TYPE to point to
7554 : : the FUNCTION_TYPE node itself. */
7555 : 49429463 : {
7556 : 49429463 : c_arg_tag *tag;
7557 : 49429463 : unsigned ix;
7558 : :
7559 : 424491158 : FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7560 : 134 : TYPE_CONTEXT (tag->type) = type;
7561 : : }
7562 : : break;
7563 : : }
7564 : 17907210 : case cdk_pointer:
7565 : 17907210 : {
7566 : : /* Merge any constancy or volatility into the target type
7567 : : for the pointer. */
7568 : 17907210 : if ((type_quals & TYPE_QUAL_ATOMIC)
7569 : 1833 : && TREE_CODE (type) == FUNCTION_TYPE)
7570 : : {
7571 : 2 : error_at (loc,
7572 : : "%<_Atomic%>-qualified function type");
7573 : 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
7574 : : }
7575 : 17907208 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7576 : 1701 : && type_quals)
7577 : 0 : pedwarn (loc, OPT_Wpedantic,
7578 : : "ISO C forbids qualified function types");
7579 : 17905509 : if (type_quals)
7580 : 5891795 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7581 : : orig_qual_indirect);
7582 : 17907210 : orig_qual_type = NULL_TREE;
7583 : 17907210 : size_varies = false;
7584 : :
7585 : : /* When the pointed-to type involves components of variable size,
7586 : : care must be taken to ensure that the size evaluation code is
7587 : : emitted early enough to dominate all the possible later uses
7588 : : and late enough for the variables on which it depends to have
7589 : : been assigned.
7590 : :
7591 : : This is expected to happen automatically when the pointed-to
7592 : : type has a name/declaration of it's own, but special attention
7593 : : is required if the type is anonymous. */
7594 : 17907210 : if (!TYPE_NAME (type) && c_type_variably_modified_p (type))
7595 : : {
7596 : 5934 : bool bind_p = decl_context == TYPENAME
7597 : : || decl_context == FIELD
7598 : 5934 : || decl_context == PARM;
7599 : 11363 : add_decl_expr (loc, type, bind_p ? expr : NULL, true);
7600 : : }
7601 : :
7602 : 17907210 : type = c_build_pointer_type (type);
7603 : :
7604 : : /* Process type qualifiers (such as const or volatile)
7605 : : that were given inside the `*'. */
7606 : 17907210 : type_quals = declarator->u.pointer_quals;
7607 : :
7608 : 17907210 : declarator = declarator->declarator;
7609 : 17907210 : break;
7610 : : }
7611 : 0 : default:
7612 : 0 : gcc_unreachable ();
7613 : : }
7614 : : }
7615 : 306587677 : *decl_attrs = chainon (returned_attrs, *decl_attrs);
7616 : 306587677 : *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
7617 : :
7618 : : /* Now TYPE has the actual type, apart from any qualifiers in
7619 : : TYPE_QUALS. */
7620 : :
7621 : : /* Warn about address space used for things other than static memory or
7622 : : pointers. */
7623 : 306587677 : address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
7624 : 306587677 : if (!ADDR_SPACE_GENERIC_P (address_space))
7625 : : {
7626 : 10 : if (decl_context == NORMAL)
7627 : : {
7628 : 10 : switch (storage_class)
7629 : : {
7630 : 0 : case csc_auto:
7631 : 0 : error ("%qs combined with %<auto%> qualifier for %qE",
7632 : : c_addr_space_name (address_space), name);
7633 : 0 : break;
7634 : 0 : case csc_register:
7635 : 0 : error ("%qs combined with %<register%> qualifier for %qE",
7636 : : c_addr_space_name (address_space), name);
7637 : 0 : break;
7638 : 6 : case csc_none:
7639 : 6 : if (current_function_scope)
7640 : : {
7641 : 0 : error ("%qs specified for auto variable %qE",
7642 : : c_addr_space_name (address_space), name);
7643 : 0 : break;
7644 : : }
7645 : : break;
7646 : : case csc_static:
7647 : : case csc_extern:
7648 : : case csc_typedef:
7649 : : break;
7650 : 0 : default:
7651 : 0 : gcc_unreachable ();
7652 : : }
7653 : : }
7654 : 0 : else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
7655 : : {
7656 : 0 : if (name)
7657 : 0 : error ("%qs specified for parameter %qE",
7658 : : c_addr_space_name (address_space), name);
7659 : : else
7660 : 0 : error ("%qs specified for unnamed parameter",
7661 : : c_addr_space_name (address_space));
7662 : : }
7663 : 0 : else if (decl_context == FIELD)
7664 : : {
7665 : 0 : if (name)
7666 : 0 : error ("%qs specified for structure field %qE",
7667 : : c_addr_space_name (address_space), name);
7668 : : else
7669 : 0 : error ("%qs specified for structure field",
7670 : : c_addr_space_name (address_space));
7671 : : }
7672 : : }
7673 : :
7674 : : /* Check the type and width of a bit-field. */
7675 : 306587677 : if (bitfield)
7676 : : {
7677 : 51878 : check_bitfield_type_and_width (loc, &type, width, name);
7678 : : /* C11 makes it implementation-defined (6.7.2.1#5) whether
7679 : : atomic types are permitted for bit-fields; we have no code to
7680 : : make bit-field accesses atomic, so disallow them. */
7681 : 51878 : if (type_quals & TYPE_QUAL_ATOMIC)
7682 : : {
7683 : 2 : if (name)
7684 : 1 : error_at (loc, "bit-field %qE has atomic type", name);
7685 : : else
7686 : 1 : error_at (loc, "bit-field has atomic type");
7687 : 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
7688 : : }
7689 : : }
7690 : :
7691 : : /* Reject invalid uses of _Alignas. */
7692 : 306587677 : if (declspecs->alignas_p)
7693 : : {
7694 : 188 : if (storage_class == csc_typedef)
7695 : 1 : error_at (loc, "alignment specified for typedef %qE", name);
7696 : 187 : else if (storage_class == csc_register)
7697 : 1 : error_at (loc, "alignment specified for %<register%> object %qE",
7698 : : name);
7699 : 186 : else if (decl_context == PARM)
7700 : : {
7701 : 2 : if (name)
7702 : 1 : error_at (loc, "alignment specified for parameter %qE", name);
7703 : : else
7704 : 1 : error_at (loc, "alignment specified for unnamed parameter");
7705 : : }
7706 : 184 : else if (bitfield)
7707 : : {
7708 : 0 : if (name)
7709 : 0 : error_at (loc, "alignment specified for bit-field %qE", name);
7710 : : else
7711 : 0 : error_at (loc, "alignment specified for unnamed bit-field");
7712 : : }
7713 : 184 : else if (TREE_CODE (type) == FUNCTION_TYPE)
7714 : 1 : error_at (loc, "alignment specified for function %qE", name);
7715 : 183 : else if (declspecs->align_log != -1 && TYPE_P (type))
7716 : : {
7717 : 156 : alignas_align = 1U << declspecs->align_log;
7718 : 156 : if (alignas_align < min_align_of_type (type))
7719 : : {
7720 : 26 : if (name)
7721 : 25 : error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7722 : : "alignment of %qE", name);
7723 : : else
7724 : 1 : error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7725 : : "alignment of unnamed field");
7726 : : alignas_align = 0;
7727 : : }
7728 : : }
7729 : : }
7730 : :
7731 : : /* If this is declaring a typedef name, return a TYPE_DECL. */
7732 : :
7733 : 306587677 : if (storage_class == csc_typedef)
7734 : : {
7735 : 4224810 : tree decl;
7736 : 4224810 : if ((type_quals & TYPE_QUAL_ATOMIC)
7737 : 11187 : && TREE_CODE (type) == FUNCTION_TYPE)
7738 : : {
7739 : 0 : error_at (loc,
7740 : : "%<_Atomic%>-qualified function type");
7741 : 0 : type_quals &= ~TYPE_QUAL_ATOMIC;
7742 : : }
7743 : 4224810 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7744 : 608 : && type_quals)
7745 : 0 : pedwarn (loc, OPT_Wpedantic,
7746 : : "ISO C forbids qualified function types");
7747 : 4224202 : if (type_quals)
7748 : 29776 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7749 : : orig_qual_indirect);
7750 : 8449620 : decl = build_decl (declarator->id_loc,
7751 : 4224810 : TYPE_DECL, declarator->u.id.id, type);
7752 : 4224810 : if (declspecs->explicit_signed_p)
7753 : 348088 : C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7754 : 4224810 : if (declspecs->inline_p)
7755 : 4 : pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7756 : 4224810 : if (declspecs->noreturn_p)
7757 : 1 : pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7758 : :
7759 : 4224810 : if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7760 : : {
7761 : 1825 : struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7762 : :
7763 : 1825 : if (b != NULL
7764 : 40 : && b->decl != NULL_TREE
7765 : 40 : && (B_IN_CURRENT_SCOPE (b)
7766 : 4 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7767 : 1861 : && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7768 : : {
7769 : 7 : auto_diagnostic_group d;
7770 : 8 : if (warning_at (declarator->id_loc, OPT_Wc___compat,
7771 : : ("using %qD as both a typedef and a tag is "
7772 : : "invalid in C++"), decl)
7773 : 7 : && b->locus != UNKNOWN_LOCATION)
7774 : 6 : inform (b->locus, "originally defined here");
7775 : 7 : }
7776 : : }
7777 : :
7778 : 4224810 : return decl;
7779 : : }
7780 : :
7781 : : /* If this is a type name (such as, in a cast or sizeof),
7782 : : compute the type and return it now. */
7783 : :
7784 : 302362867 : if (decl_context == TYPENAME)
7785 : : {
7786 : : /* Note that the grammar rejects storage classes in typenames
7787 : : and fields. */
7788 : 118376296 : gcc_assert (storage_class == csc_none && !threadp
7789 : : && !declspecs->inline_p && !declspecs->noreturn_p);
7790 : 118376296 : if ((type_quals & TYPE_QUAL_ATOMIC)
7791 : 256 : && TREE_CODE (type) == FUNCTION_TYPE)
7792 : : {
7793 : 0 : error_at (loc,
7794 : : "%<_Atomic%>-qualified function type");
7795 : 0 : type_quals &= ~TYPE_QUAL_ATOMIC;
7796 : : }
7797 : 118376296 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7798 : 22 : && type_quals)
7799 : 0 : pedwarn (loc, OPT_Wpedantic,
7800 : : "ISO C forbids const or volatile function types");
7801 : 118376274 : if (type_quals)
7802 : 881 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7803 : : orig_qual_indirect);
7804 : 118376296 : return type;
7805 : : }
7806 : :
7807 : 374580 : if (pedantic && decl_context == FIELD
7808 : 184011293 : && c_type_variably_modified_p (type))
7809 : : {
7810 : : /* C99 6.7.2.1p8 */
7811 : 6 : pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
7812 : : "have a variably modified type");
7813 : : }
7814 : :
7815 : : /* Aside from typedefs and type names (handle above),
7816 : : `void' at top level (not within pointer)
7817 : : is allowed only in public variables.
7818 : : We don't complain about parms either, but that is because
7819 : : a better error message can be made later. */
7820 : :
7821 : 183986571 : if (VOID_TYPE_P (type) && decl_context != PARM
7822 : 59 : && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
7823 : : && (storage_class == csc_extern
7824 : 27 : || (current_scope == file_scope
7825 : 18 : && !(storage_class == csc_static
7826 : : || storage_class == csc_register)))))
7827 : : {
7828 : 15 : error_at (loc, "variable or field %qE declared void", name);
7829 : 15 : type = integer_type_node;
7830 : : }
7831 : :
7832 : : /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7833 : : or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
7834 : :
7835 : 183050677 : {
7836 : 183050677 : tree decl;
7837 : :
7838 : 183050677 : if (decl_context == PARM)
7839 : : {
7840 : 121155561 : tree promoted_type;
7841 : 121155561 : bool array_parameter_p = false;
7842 : :
7843 : : /* A parameter declared as an array of T is really a pointer to T.
7844 : : One declared as a function is really a pointer to a function. */
7845 : :
7846 : 121155561 : if (TREE_CODE (type) == ARRAY_TYPE)
7847 : : {
7848 : 430181 : if (!size_error)
7849 : 430167 : *decl_attrs = build_arg_spec_attribute (type, array_parm_static,
7850 : : *decl_attrs);
7851 : :
7852 : : /* Transfer const-ness of array into that of type pointed to. */
7853 : 430181 : type = TREE_TYPE (type);
7854 : 430181 : if (orig_qual_type != NULL_TREE)
7855 : : {
7856 : 7 : if (orig_qual_indirect == 0)
7857 : 5 : orig_qual_type = TREE_TYPE (orig_qual_type);
7858 : : else
7859 : 2 : orig_qual_indirect--;
7860 : : }
7861 : 430181 : if (type_quals)
7862 : 47243 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7863 : : orig_qual_indirect);
7864 : :
7865 : : /* The pointed-to type may need a decl expr (see above). */
7866 : 430181 : if (!TYPE_NAME (type) && c_type_variably_modified_p (type))
7867 : : {
7868 : 373 : bool bind_p = decl_context == TYPENAME
7869 : : || decl_context == FIELD
7870 : : || decl_context == PARM;
7871 : 373 : add_decl_expr (loc, type, bind_p ? expr : NULL, true);
7872 : : }
7873 : :
7874 : 430181 : type = c_build_pointer_type (type);
7875 : 430181 : type_quals = array_ptr_quals;
7876 : 430181 : if (type_quals)
7877 : 932 : type = c_build_qualified_type (type, type_quals);
7878 : :
7879 : : /* We don't yet implement attributes in this context. */
7880 : 430181 : if (array_ptr_attrs != NULL_TREE)
7881 : 0 : warning_at (loc, OPT_Wattributes,
7882 : : "attributes in parameter array declarator ignored");
7883 : :
7884 : : size_varies = false;
7885 : : array_parameter_p = true;
7886 : : }
7887 : 120725380 : else if (TREE_CODE (type) == FUNCTION_TYPE)
7888 : : {
7889 : 301 : if (type_quals & TYPE_QUAL_ATOMIC)
7890 : : {
7891 : 1 : error_at (loc,
7892 : : "%<_Atomic%>-qualified function type");
7893 : 1 : type_quals &= ~TYPE_QUAL_ATOMIC;
7894 : : }
7895 : 300 : else if (type_quals)
7896 : 0 : pedwarn (loc, OPT_Wpedantic,
7897 : : "ISO C forbids qualified function types");
7898 : 1 : if (type_quals)
7899 : 0 : type = c_build_qualified_type (type, type_quals);
7900 : 301 : type = c_build_pointer_type (type);
7901 : 301 : type_quals = TYPE_UNQUALIFIED;
7902 : : }
7903 : 120725079 : else if (type_quals)
7904 : 9733727 : type = c_build_qualified_type (type, type_quals);
7905 : :
7906 : 242311122 : decl = build_decl (declarator->id_loc,
7907 : 121155561 : PARM_DECL, declarator->u.id.id, type);
7908 : 121155561 : if (size_varies)
7909 : 42 : C_DECL_VARIABLE_SIZE (decl) = 1;
7910 : 121155561 : C_ARRAY_PARAMETER (decl) = array_parameter_p;
7911 : :
7912 : : /* Compute the type actually passed in the parmlist,
7913 : : for the case where there is no prototype.
7914 : : (For example, shorts and chars are passed as ints.)
7915 : : When there is a prototype, this is overridden later. */
7916 : :
7917 : 121155561 : if (type == error_mark_node)
7918 : : promoted_type = type;
7919 : : else
7920 : 121155477 : promoted_type = c_type_promotes_to (type);
7921 : :
7922 : 121155561 : DECL_ARG_TYPE (decl) = promoted_type;
7923 : 121155561 : if (declspecs->inline_p)
7924 : 4 : pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
7925 : 121155561 : if (declspecs->noreturn_p)
7926 : 1 : pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
7927 : : }
7928 : 62831010 : else if (decl_context == FIELD)
7929 : : {
7930 : : /* Note that the grammar rejects storage classes in typenames
7931 : : and fields. */
7932 : 4235790 : gcc_assert (storage_class == csc_none && !threadp
7933 : : && !declspecs->inline_p && !declspecs->noreturn_p);
7934 : :
7935 : : /* Structure field. It may not be a function. */
7936 : :
7937 : 4235790 : if (TREE_CODE (type) == FUNCTION_TYPE)
7938 : : {
7939 : 0 : error_at (loc, "field %qE declared as a function", name);
7940 : 0 : type = c_build_pointer_type (type);
7941 : : }
7942 : 4235790 : else if (TREE_CODE (type) != ERROR_MARK
7943 : 4235790 : && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
7944 : : {
7945 : 24 : if (name)
7946 : 17 : error_at (loc, "field %qE has incomplete type", name);
7947 : : else
7948 : 7 : error_at (loc, "unnamed field has incomplete type");
7949 : 24 : type = error_mark_node;
7950 : : }
7951 : 4235766 : else if (TREE_CODE (type) == ARRAY_TYPE
7952 : 4235766 : && TYPE_DOMAIN (type) == NULL_TREE)
7953 : : {
7954 : : /* We have a flexible array member through a typedef.
7955 : : Set suitable range. Whether this is a correct position
7956 : : for a flexible array member will be determined elsewhere. */
7957 : 14 : if (!in_system_header_at (input_location))
7958 : 14 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7959 : : "support flexible array members");
7960 : 14 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7961 : 14 : TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
7962 : : NULL_TREE);
7963 : 14 : if (orig_qual_indirect == 0)
7964 : 4235790 : orig_qual_type = NULL_TREE;
7965 : : }
7966 : 4235790 : if (type != error_mark_node
7967 : 4235790 : && !verify_type_context (loc, TCTX_FIELD, type))
7968 : 0 : type = error_mark_node;
7969 : :
7970 : 4235790 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7971 : : orig_qual_indirect);
7972 : 8471580 : decl = build_decl (declarator->id_loc,
7973 : 4235790 : FIELD_DECL, declarator->u.id.id, type);
7974 : 4235790 : DECL_NONADDRESSABLE_P (decl) = bitfield;
7975 : 4235790 : if (bitfield && !declarator->u.id.id)
7976 : 9186 : DECL_PADDING_P (decl) = 1;
7977 : :
7978 : 4235790 : if (size_varies)
7979 : 646 : C_DECL_VARIABLE_SIZE (decl) = 1;
7980 : : }
7981 : 58595220 : else if (TREE_CODE (type) == FUNCTION_TYPE)
7982 : : {
7983 : 49848235 : if (storage_class == csc_register || threadp || constexprp)
7984 : : {
7985 : 10 : error_at (loc, "invalid storage class for function %qE", name);
7986 : : }
7987 : 49848225 : else if (current_scope != file_scope)
7988 : : {
7989 : : /* Function declaration not at file scope. Storage
7990 : : classes other than `extern' are not allowed, C99
7991 : : 6.7.1p5, and `extern' makes no difference. However,
7992 : : GCC allows 'auto', perhaps with 'inline', to support
7993 : : nested functions. */
7994 : 10475 : if (storage_class == csc_auto)
7995 : 63 : pedwarn (loc, OPT_Wpedantic,
7996 : : "invalid storage class for function %qE", name);
7997 : 10412 : else if (storage_class == csc_static)
7998 : : {
7999 : 19 : error_at (loc, "invalid storage class for function %qE", name);
8000 : 19 : if (funcdef_flag)
8001 : 8 : storage_class = declspecs->storage_class = csc_none;
8002 : : else
8003 : : return NULL_TREE;
8004 : : }
8005 : : }
8006 : :
8007 : 99696448 : decl = build_decl (declarator->id_loc,
8008 : 49848224 : FUNCTION_DECL, declarator->u.id.id, type);
8009 : 49848224 : decl = build_decl_attribute_variant (decl, decl_attr);
8010 : :
8011 : 49848224 : if (type_quals & TYPE_QUAL_ATOMIC)
8012 : : {
8013 : 1 : error_at (loc,
8014 : : "%<_Atomic%>-qualified function type");
8015 : 1 : type_quals &= ~TYPE_QUAL_ATOMIC;
8016 : : }
8017 : 49848223 : else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
8018 : 6 : pedwarn (loc, OPT_Wpedantic,
8019 : : "ISO C forbids qualified function types");
8020 : :
8021 : : /* Every function declaration is an external reference
8022 : : (DECL_EXTERNAL) except for those which are not at file
8023 : : scope and are explicitly declared "auto". This is
8024 : : forbidden by standard C (C99 6.7.1p5) and is interpreted by
8025 : : GCC to signify a forward declaration of a nested function. */
8026 : 49848224 : if (storage_class == csc_auto && current_scope != file_scope)
8027 : 63 : DECL_EXTERNAL (decl) = 0;
8028 : : /* In C99, a function which is declared 'inline' with 'extern'
8029 : : is not an external reference (which is confusing). It
8030 : : means that the later definition of the function must be output
8031 : : in this file, C99 6.7.4p6. In GNU C89, a function declared
8032 : : 'extern inline' is an external reference. */
8033 : 49848161 : else if (declspecs->inline_p && storage_class != csc_static)
8034 : 34549265 : DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
8035 : 34549265 : == flag_gnu89_inline);
8036 : : else
8037 : 15298896 : DECL_EXTERNAL (decl) = !initialized;
8038 : :
8039 : : /* Record absence of global scope for `static' or `auto'. */
8040 : 49848224 : TREE_PUBLIC (decl)
8041 : 49848224 : = !(storage_class == csc_static || storage_class == csc_auto);
8042 : :
8043 : : /* For a function definition, record the argument information
8044 : : block where store_parm_decls will look for it. */
8045 : 49848224 : if (funcdef_flag)
8046 : 35382603 : current_function_arg_info = arg_info;
8047 : :
8048 : 49848224 : if (declspecs->default_int_p)
8049 : 9219 : C_FUNCTION_IMPLICIT_INT (decl) = 1;
8050 : :
8051 : : /* Record presence of `inline' and `_Noreturn', if it is
8052 : : reasonable. */
8053 : 49848224 : if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
8054 : : {
8055 : 48513 : if (declspecs->inline_p)
8056 : 5 : pedwarn (loc, 0, "cannot inline function %<main%>");
8057 : 48513 : if (declspecs->noreturn_p)
8058 : 1 : pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
8059 : : }
8060 : : else
8061 : : {
8062 : 49799711 : if (declspecs->inline_p)
8063 : : /* Record that the function is declared `inline'. */
8064 : 34706133 : DECL_DECLARED_INLINE_P (decl) = 1;
8065 : 49799711 : if (declspecs->noreturn_p)
8066 : : {
8067 : 22955 : if (flag_isoc99)
8068 : 22952 : pedwarn_c99 (loc, OPT_Wpedantic,
8069 : : "ISO C99 does not support %<_Noreturn%>");
8070 : : else
8071 : 3 : pedwarn_c99 (loc, OPT_Wpedantic,
8072 : : "ISO C90 does not support %<_Noreturn%>");
8073 : 22955 : TREE_THIS_VOLATILE (decl) = 1;
8074 : : }
8075 : : }
8076 : :
8077 : : /* C99 6.2.2p7: It is invalid (compile-time undefined
8078 : : behavior) to create an 'extern' declaration for a
8079 : : function if there is a global declaration that is
8080 : : 'static' and the global declaration is not visible.
8081 : : (If the static declaration _is_ currently visible,
8082 : : the 'extern' declaration is taken to refer to that decl.) */
8083 : 49848224 : if (!initialized
8084 : 14465612 : && TREE_PUBLIC (decl)
8085 : 14457972 : && current_scope != file_scope)
8086 : : {
8087 : 8869 : tree global_decl = identifier_global_value (declarator->u.id.id);
8088 : 8869 : tree visible_decl = lookup_name (declarator->u.id.id);
8089 : :
8090 : 8869 : if (global_decl
8091 : 8869 : && global_decl != visible_decl
8092 : 1712 : && VAR_OR_FUNCTION_DECL_P (global_decl)
8093 : 1712 : && !TREE_PUBLIC (global_decl))
8094 : 1 : error_at (loc, "function previously declared %<static%> "
8095 : : "redeclared %<extern%>");
8096 : : }
8097 : : }
8098 : : else
8099 : : {
8100 : : /* It's a variable. */
8101 : : /* An uninitialized decl with `extern' is a reference. */
8102 : 8746985 : int extern_ref = !initialized && storage_class == csc_extern;
8103 : :
8104 : 8746985 : if (constexprp)
8105 : : {
8106 : : /* The type of a constexpr variable must not be variably
8107 : : modified, volatile, atomic or restrict qualified or
8108 : : have a member with such a qualifier. const
8109 : : qualification is implicitly added, and, at file scope,
8110 : : has internal linkage. */
8111 : 347 : if (c_type_variably_modified_p (type))
8112 : 1 : error_at (loc, "%<constexpr%> object has variably modified "
8113 : : "type");
8114 : 347 : if (type_quals
8115 : 347 : & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
8116 : 9 : error_at (loc, "invalid qualifiers for %<constexpr%> object");
8117 : : else
8118 : : {
8119 : 338 : tree type_no_array = strip_array_types (type);
8120 : 338 : if (RECORD_OR_UNION_TYPE_P (type_no_array)
8121 : 338 : && C_TYPE_FIELDS_NON_CONSTEXPR (type_no_array))
8122 : 8 : error_at (loc, "invalid qualifiers for field of "
8123 : : "%<constexpr%> object");
8124 : : }
8125 : 347 : type_quals |= TYPE_QUAL_CONST;
8126 : 347 : if (current_scope == file_scope)
8127 : 294 : storage_class = csc_static;
8128 : : }
8129 : :
8130 : 8746985 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
8131 : : orig_qual_indirect);
8132 : :
8133 : : /* C99 6.2.2p7: It is invalid (compile-time undefined
8134 : : behavior) to create an 'extern' declaration for a
8135 : : variable if there is a global declaration that is
8136 : : 'static' and the global declaration is not visible.
8137 : : (If the static declaration _is_ currently visible,
8138 : : the 'extern' declaration is taken to refer to that decl.) */
8139 : 8746985 : if (extern_ref && current_scope != file_scope)
8140 : : {
8141 : 1567 : tree global_decl = identifier_global_value (declarator->u.id.id);
8142 : 1567 : tree visible_decl = lookup_name (declarator->u.id.id);
8143 : :
8144 : 1567 : if (global_decl
8145 : 1567 : && global_decl != visible_decl
8146 : 281 : && VAR_P (global_decl)
8147 : 281 : && !TREE_PUBLIC (global_decl))
8148 : 7 : error_at (loc, "variable previously declared %<static%> "
8149 : : "redeclared %<extern%>");
8150 : : }
8151 : :
8152 : 17493970 : decl = build_decl (declarator->id_loc,
8153 : 8746985 : VAR_DECL, declarator->u.id.id, type);
8154 : 8746985 : if (size_varies)
8155 : 7397 : C_DECL_VARIABLE_SIZE (decl) = 1;
8156 : 8746985 : if (constexprp)
8157 : 347 : C_DECL_DECLARED_CONSTEXPR (decl) = 1;
8158 : :
8159 : 8746985 : if (declspecs->inline_p)
8160 : 4 : pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
8161 : 8746985 : if (declspecs->noreturn_p)
8162 : 1 : pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
8163 : :
8164 : : /* At file scope, an initialized extern declaration may follow
8165 : : a static declaration. In that case, DECL_EXTERNAL will be
8166 : : reset later in start_decl. */
8167 : 8746985 : DECL_EXTERNAL (decl) = (storage_class == csc_extern);
8168 : :
8169 : : /* At file scope, the presence of a `static' or `register' storage
8170 : : class specifier, or the absence of all storage class specifiers
8171 : : makes this declaration a definition (perhaps tentative). Also,
8172 : : the absence of `static' makes it public. */
8173 : 8746985 : if (current_scope == file_scope)
8174 : : {
8175 : 1150548 : TREE_PUBLIC (decl) = storage_class != csc_static;
8176 : 1150548 : TREE_STATIC (decl) = !extern_ref;
8177 : : }
8178 : : /* Not at file scope, only `static' makes a static definition. */
8179 : : else
8180 : : {
8181 : 7596437 : TREE_STATIC (decl) = (storage_class == csc_static);
8182 : 7596437 : TREE_PUBLIC (decl) = extern_ref;
8183 : : }
8184 : :
8185 : 8746985 : if (threadp)
8186 : 2828 : set_decl_tls_model (decl, decl_default_tls_model (decl));
8187 : : }
8188 : :
8189 : 183986560 : if ((storage_class == csc_extern
8190 : 135112387 : || (storage_class == csc_none
8191 : 134663857 : && TREE_CODE (type) == FUNCTION_TYPE
8192 : 798697 : && !funcdef_flag))
8193 : 184290069 : && c_type_variably_modified_p (type))
8194 : : {
8195 : : /* C99 6.7.5.2p2 */
8196 : 6 : if (TREE_CODE (type) == FUNCTION_TYPE)
8197 : 4 : error_at (loc, "non-nested function with variably modified type");
8198 : : else
8199 : 2 : error_at (loc, "object with variably modified type must have "
8200 : : "no linkage");
8201 : : }
8202 : :
8203 : : /* For nested functions disqualify ones taking VLAs by value
8204 : : from inlining since the middle-end cannot deal with this.
8205 : : ??? We should arrange for those to be passed by reference
8206 : : with emitting the copy on the caller side in the frontend. */
8207 : 183986560 : if (storage_class == csc_none
8208 : 134663857 : && TREE_CODE (type) == FUNCTION_TYPE)
8209 : 4149241 : for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
8210 : : {
8211 : 3350584 : tree arg = TREE_VALUE (al);
8212 : 3350584 : if (arg != error_mark_node
8213 : 3350584 : && C_TYPE_VARIABLE_SIZE (arg))
8214 : : {
8215 : 40 : DECL_UNINLINABLE (decl) = 1;
8216 : 40 : break;
8217 : : }
8218 : : }
8219 : :
8220 : : /* Record `register' declaration for warnings on &
8221 : : and in case doing stupid register allocation. */
8222 : :
8223 : 183986560 : if (storage_class == csc_register)
8224 : : {
8225 : 3566 : C_DECL_REGISTER (decl) = 1;
8226 : 3566 : DECL_REGISTER (decl) = 1;
8227 : : }
8228 : :
8229 : : /* Record constancy and volatility. */
8230 : 183986560 : c_apply_type_quals_to_decl (type_quals, decl);
8231 : :
8232 : : /* Apply _Alignas specifiers. */
8233 : 183986560 : if (alignas_align)
8234 : : {
8235 : 123 : SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
8236 : 123 : DECL_USER_ALIGN (decl) = 1;
8237 : : }
8238 : :
8239 : : /* If a type has volatile components, it should be stored in memory.
8240 : : Otherwise, the fact that those components are volatile
8241 : : will be ignored, and would even crash the compiler.
8242 : : Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
8243 : 183986560 : if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
8244 : 183986560 : && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
8245 : : || TREE_CODE (decl) == RESULT_DECL))
8246 : : {
8247 : : /* It is not an error for a structure with volatile fields to
8248 : : be declared register, but reset DECL_REGISTER since it
8249 : : cannot actually go in a register. */
8250 : 184 : int was_reg = C_DECL_REGISTER (decl);
8251 : 184 : C_DECL_REGISTER (decl) = 0;
8252 : 184 : DECL_REGISTER (decl) = 0;
8253 : 184 : c_mark_addressable (decl);
8254 : 184 : C_DECL_REGISTER (decl) = was_reg;
8255 : : }
8256 : :
8257 : : /* This is the earliest point at which we might know the assembler
8258 : : name of a variable. Thus, if it's known before this, die horribly. */
8259 : 183986560 : gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
8260 : : || !DECL_ASSEMBLER_NAME_SET_P (decl));
8261 : :
8262 : 183986560 : if (warn_cxx_compat
8263 : 24497 : && VAR_P (decl)
8264 : 7537 : && TREE_PUBLIC (decl)
8265 : 2794 : && TREE_STATIC (decl)
8266 : 2348 : && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
8267 : 2216 : || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
8268 : 183986707 : && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
8269 : 4 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
8270 : : ("non-local variable %qD with anonymous type is "
8271 : : "questionable in C++"),
8272 : : decl);
8273 : :
8274 : : return decl;
8275 : : }
8276 : : }
8277 : :
8278 : : /* Decode the parameter-list info for a function type or function definition.
8279 : : The argument is the value returned by `get_parm_info' (or made in c-parse.c
8280 : : if there is an identifier list instead of a parameter decl list).
8281 : : These two functions are separate because when a function returns
8282 : : or receives functions then each is called multiple times but the order
8283 : : of calls is different. The last call to `grokparms' is always the one
8284 : : that contains the formal parameter names of a function definition.
8285 : :
8286 : : Return a list of arg types to use in the FUNCTION_TYPE for this function.
8287 : :
8288 : : FUNCDEF_FLAG is true for a function definition, false for
8289 : : a mere declaration. A nonempty identifier-list gets an error message
8290 : : when FUNCDEF_FLAG is false. */
8291 : :
8292 : : static tree
8293 : 49429463 : grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
8294 : : {
8295 : 49429463 : tree arg_types = arg_info->types;
8296 : :
8297 : 49429463 : if (funcdef_flag && arg_info->had_vla_unspec)
8298 : : {
8299 : : /* A function definition isn't function prototype scope C99 6.2.1p4. */
8300 : : /* C99 6.7.5.2p4 */
8301 : 2 : error ("%<[*]%> not allowed in other than function prototype scope");
8302 : : }
8303 : :
8304 : 715227 : if (arg_types == NULL_TREE && !funcdef_flag && !flag_isoc23
8305 : 49432488 : && !in_system_header_at (input_location))
8306 : 3025 : warning (OPT_Wstrict_prototypes,
8307 : : "function declaration isn%'t a prototype");
8308 : :
8309 : 49429463 : if (arg_types == error_mark_node)
8310 : : /* Don't set TYPE_ARG_TYPES in this case. */
8311 : : return NULL_TREE;
8312 : :
8313 : 98086987 : else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
8314 : : {
8315 : 8684 : if (!funcdef_flag)
8316 : : {
8317 : 13 : permerror_opt (input_location,
8318 : 13 : OPT_Wdeclaration_missing_parameter_type,
8319 : : "parameter names (without types) in "
8320 : : "function declaration");
8321 : 13 : arg_info->parms = NULL_TREE;
8322 : : }
8323 : : else
8324 : 8671 : arg_info->parms = arg_info->types;
8325 : :
8326 : 8684 : arg_info->types = NULL_TREE;
8327 : 8684 : return NULL_TREE;
8328 : : }
8329 : : else
8330 : : {
8331 : 49420779 : tree parm, type, typelt;
8332 : 49420779 : unsigned int parmno;
8333 : :
8334 : : /* In C23, convert () to (void). */
8335 : 49420779 : if (flag_isoc23
8336 : 40393698 : && !arg_types
8337 : 764484 : && !arg_info->parms
8338 : 764484 : && !arg_info->no_named_args_stdarg_p)
8339 : : {
8340 : 764343 : arg_types = arg_info->types = void_list_node;
8341 : 764343 : arg_info->c23_empty_parens = 1;
8342 : : }
8343 : :
8344 : : /* If there is a parameter of incomplete type in a definition,
8345 : : this is an error. In a declaration this is valid, and a
8346 : : struct or union type may be completed later, before any calls
8347 : : or definition of the function. In the case where the tag was
8348 : : first declared within the parameter list, a warning has
8349 : : already been given. If a parameter has void type, then
8350 : : this has already received an error (constraint violation in C2Y,
8351 : : previously implicitly undefined behavior). */
8352 : :
8353 : 49420779 : for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
8354 : 169617624 : parm;
8355 : 120196845 : parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
8356 : : {
8357 : 120196845 : type = TREE_VALUE (typelt);
8358 : 120196845 : if (type == error_mark_node)
8359 : 61 : continue;
8360 : :
8361 : 120196784 : if (!COMPLETE_TYPE_P (type))
8362 : : {
8363 : 40 : if (funcdef_flag)
8364 : : {
8365 : 13 : if (DECL_NAME (parm))
8366 : 13 : error_at (input_location,
8367 : : "parameter %u (%q+D) has incomplete type",
8368 : : parmno, parm);
8369 : : else
8370 : 0 : error_at (DECL_SOURCE_LOCATION (parm),
8371 : : "parameter %u has incomplete type",
8372 : : parmno);
8373 : :
8374 : 13 : TREE_VALUE (typelt) = error_mark_node;
8375 : 13 : TREE_TYPE (parm) = error_mark_node;
8376 : 13 : arg_types = NULL_TREE;
8377 : : }
8378 : : }
8379 : :
8380 : 120196784 : if (DECL_NAME (parm) && TREE_USED (parm))
8381 : 17501 : warn_if_shadowing (parm);
8382 : : }
8383 : : return arg_types;
8384 : : }
8385 : : }
8386 : :
8387 : : /* Allocate and initialize a c_arg_info structure from the parser's
8388 : : obstack. */
8389 : :
8390 : : struct c_arg_info *
8391 : 49429480 : build_arg_info (void)
8392 : : {
8393 : 49429480 : struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
8394 : 49429480 : ret->parms = NULL_TREE;
8395 : 49429480 : ret->tags = NULL;
8396 : 49429480 : ret->types = NULL_TREE;
8397 : 49429480 : ret->others = NULL_TREE;
8398 : 49429480 : ret->pending_sizes = NULL;
8399 : 49429480 : ret->had_vla_unspec = 0;
8400 : 49429480 : ret->no_named_args_stdarg_p = 0;
8401 : 49429480 : ret->c23_empty_parens = 0;
8402 : 49429480 : return ret;
8403 : : }
8404 : :
8405 : : /* Take apart the current scope and return a c_arg_info structure with
8406 : : info on a parameter list just parsed.
8407 : :
8408 : : This structure is later fed to 'grokparms' and 'store_parm_decls'.
8409 : :
8410 : : ELLIPSIS being true means the argument list ended in '...' so don't
8411 : : append a sentinel (void_list_node) to the end of the type-list.
8412 : :
8413 : : EXPR is NULL or an expression that needs to be evaluated for the
8414 : : side effects of array size expressions in the parameters. */
8415 : :
8416 : : struct c_arg_info *
8417 : 48648869 : get_parm_info (bool ellipsis, tree expr)
8418 : : {
8419 : 48648869 : struct c_binding *b = current_scope->bindings;
8420 : 48648869 : struct c_arg_info *arg_info = build_arg_info ();
8421 : :
8422 : 48648869 : tree parms = NULL_TREE;
8423 : 48648869 : vec<c_arg_tag, va_gc> *tags = NULL;
8424 : 48648869 : tree types = NULL_TREE;
8425 : 48648869 : tree others = NULL_TREE;
8426 : :
8427 : 48648869 : bool gave_void_only_once_err = false;
8428 : :
8429 : 48648869 : arg_info->had_vla_unspec = current_scope->had_vla_unspec;
8430 : :
8431 : : /* The bindings in this scope must not get put into a block.
8432 : : We will take care of deleting the binding nodes. */
8433 : 48648869 : current_scope->bindings = 0;
8434 : :
8435 : : /* This function is only called if there was *something* on the
8436 : : parameter list. */
8437 : 48648869 : gcc_assert (b);
8438 : :
8439 : : /* A parameter list consisting solely of 'void' indicates that the
8440 : : function takes no arguments. But if the 'void' is qualified
8441 : : (by 'const' or 'volatile'), or has a storage class specifier
8442 : : ('register'), then the behavior is undefined; issue an error.
8443 : : Typedefs for 'void' are OK (see DR#157). */
8444 : 48648869 : if (b->prev == 0 /* one binding */
8445 : 12988223 : && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
8446 : 12988223 : && !DECL_NAME (b->decl) /* anonymous */
8447 : 50428035 : && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
8448 : : {
8449 : 935780 : if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
8450 : 935780 : || C_DECL_REGISTER (b->decl))
8451 : 96 : error_at (b->locus, "%<void%> as only parameter may not be qualified");
8452 : :
8453 : : /* There cannot be an ellipsis. */
8454 : 935780 : if (ellipsis)
8455 : 17 : error_at (b->locus, "%<void%> must be the only parameter");
8456 : :
8457 : 935780 : arg_info->types = void_list_node;
8458 : 935780 : return arg_info;
8459 : : }
8460 : :
8461 : 47713089 : if (!ellipsis)
8462 : 47502606 : types = void_list_node;
8463 : :
8464 : : /* Break up the bindings list into parms, tags, types, and others;
8465 : : apply sanity checks; purge the name-to-decl bindings. */
8466 : 167910381 : while (b)
8467 : : {
8468 : 120197292 : tree decl = b->decl;
8469 : 120197292 : tree type = TREE_TYPE (decl);
8470 : 120197292 : c_arg_tag tag;
8471 : 120197292 : const char *keyword;
8472 : :
8473 : 120197292 : switch (TREE_CODE (decl))
8474 : : {
8475 : 120196968 : case PARM_DECL:
8476 : 120196968 : if (b->id)
8477 : : {
8478 : 116648757 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8479 : 116648757 : I_SYMBOL_BINDING (b->id) = b->shadowed;
8480 : : }
8481 : :
8482 : : /* Check for forward decls that never got their actual decl. */
8483 : 120196968 : if (TREE_ASM_WRITTEN (decl))
8484 : 4 : error_at (b->locus,
8485 : : "parameter %q+D has just a forward declaration", decl);
8486 : : /* Check for (..., void, ...) and named void parameters and issue an
8487 : : error. */
8488 : 120196964 : else if (VOID_TYPE_P (type))
8489 : : {
8490 : 114 : if (!gave_void_only_once_err)
8491 : : {
8492 : 114 : error_at (b->locus,
8493 : : "%<void%> must be the only parameter and unnamed");
8494 : 114 : gave_void_only_once_err = true;
8495 : : }
8496 : : }
8497 : : else
8498 : : {
8499 : : /* Valid parameter, add it to the list. */
8500 : 120196850 : DECL_CHAIN (decl) = parms;
8501 : 120196850 : parms = decl;
8502 : :
8503 : : /* Since there is a prototype, args are passed in their
8504 : : declared types. The back end may override this later. */
8505 : 120196850 : DECL_ARG_TYPE (decl) = type;
8506 : 120196850 : types = tree_cons (0, type, types);
8507 : : }
8508 : : break;
8509 : :
8510 : 21 : case ENUMERAL_TYPE: keyword = "enum"; goto tag;
8511 : 19 : case UNION_TYPE: keyword = "union"; goto tag;
8512 : 94 : case RECORD_TYPE: keyword = "struct"; goto tag;
8513 : 134 : tag:
8514 : : /* Types may not have tag-names, in which case the type
8515 : : appears in the bindings list with b->id NULL. */
8516 : 134 : if (b->id)
8517 : : {
8518 : 92 : gcc_assert (I_TAG_BINDING (b->id) == b);
8519 : 92 : I_TAG_BINDING (b->id) = b->shadowed;
8520 : : }
8521 : :
8522 : : /* Warn about any struct, union or enum tags defined in a
8523 : : parameter list. The scope of such types is limited to
8524 : : the parameter list, which is rarely if ever desirable
8525 : : (it's impossible to call such a function with type-
8526 : : correct arguments). An anonymous union parm type is
8527 : : meaningful as a GNU extension, so don't warn for that. */
8528 : 134 : if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
8529 : : {
8530 : 115 : if (b->id)
8531 : : {
8532 : : /* The %s will be one of 'struct', 'union', or 'enum'. */
8533 : 92 : if (!flag_isoc23 || !COMPLETE_TYPE_P (decl))
8534 : 72 : warning_at (b->locus, 0,
8535 : : "%<%s %E%> declared inside parameter list"
8536 : : " will not be visible outside of this definition or"
8537 : : " declaration", keyword, b->id);
8538 : : }
8539 : : else
8540 : : /* The %s will be one of 'struct', 'union', or 'enum'. */
8541 : 23 : warning_at (b->locus, 0,
8542 : : "anonymous %s declared inside parameter list"
8543 : : " will not be visible outside of this definition or"
8544 : : " declaration", keyword);
8545 : : }
8546 : :
8547 : 134 : tag.id = b->id;
8548 : 134 : tag.type = decl;
8549 : 134 : vec_safe_push (tags, tag);
8550 : 134 : break;
8551 : :
8552 : 20 : case FUNCTION_DECL:
8553 : : /* FUNCTION_DECLs appear when there is an implicit function
8554 : : declaration in the parameter list. */
8555 : 20 : gcc_assert (b->nested || seen_error ());
8556 : 20 : goto set_shadowed;
8557 : :
8558 : 136 : case CONST_DECL:
8559 : 136 : case TYPE_DECL:
8560 : : /* CONST_DECLs appear here when we have an embedded enum,
8561 : : and TYPE_DECLs appear here when we have an embedded struct
8562 : : or union. No warnings for this - we already warned about the
8563 : : type itself. */
8564 : :
8565 : : /* When we reinsert this decl in the function body, we need
8566 : : to reconstruct whether it was marked as nested. */
8567 : 136 : gcc_assert (!b->nested);
8568 : 136 : DECL_CHAIN (decl) = others;
8569 : 136 : others = decl;
8570 : : /* fall through */
8571 : :
8572 : 190 : case ERROR_MARK:
8573 : 190 : set_shadowed:
8574 : : /* error_mark_node appears here when we have an undeclared
8575 : : variable. Just throw it away. */
8576 : 190 : if (b->id)
8577 : : {
8578 : 56 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8579 : 56 : I_SYMBOL_BINDING (b->id) = b->shadowed;
8580 : : }
8581 : : break;
8582 : :
8583 : : /* Other things that might be encountered. */
8584 : 0 : case LABEL_DECL:
8585 : 0 : case VAR_DECL:
8586 : 0 : default:
8587 : 0 : gcc_unreachable ();
8588 : : }
8589 : :
8590 : 120197292 : b = free_binding_and_advance (b);
8591 : : }
8592 : :
8593 : 47713089 : arg_info->parms = parms;
8594 : 47713089 : arg_info->tags = tags;
8595 : 47713089 : arg_info->types = types;
8596 : 47713089 : arg_info->others = others;
8597 : 47713089 : arg_info->pending_sizes = expr;
8598 : 47713089 : arg_info->no_named_args_stdarg_p = ellipsis && !types;
8599 : 47713089 : return arg_info;
8600 : : }
8601 : :
8602 : : /* Get the struct, enum or union (CODE says which) with tag NAME.
8603 : : Define the tag as a forward-reference with location LOC if it is
8604 : : not defined. HAVE_STD_ATTRS says whether any standard attributes
8605 : : were present after the struct, union or enum keyword; ATTRS are the
8606 : : standard attributes present there. HAS_ENUM_TYPE_SPECIFIER says
8607 : : whether an enum type specifier (": specifier-qualifier-list") is
8608 : : present; if so, this is called before that specifier is parsed, so
8609 : : that the tag is in scope for that specifier. Return a c_typespec
8610 : : structure for the type specifier. */
8611 : :
8612 : : struct c_typespec
8613 : 1066674 : parser_xref_tag (location_t loc, enum tree_code code, tree name,
8614 : : bool have_std_attrs, tree attrs, bool has_enum_type_specifier)
8615 : : {
8616 : 1066674 : struct c_typespec ret;
8617 : 1066674 : tree ref;
8618 : 1066674 : location_t refloc;
8619 : :
8620 : 1066674 : ret.expr = NULL_TREE;
8621 : 1066674 : ret.expr_const_operands = true;
8622 : 1066674 : ret.has_enum_type_specifier = has_enum_type_specifier;
8623 : :
8624 : : /* If a cross reference is requested, look up the type already
8625 : : defined for this tag and return it. If an enum type specifier is
8626 : : present, only a definition in the current scope is relevant. */
8627 : :
8628 : 1066674 : ref = lookup_tag (code, name, has_enum_type_specifier, &refloc);
8629 : :
8630 : : /* If the visble type is still being defined, see if there is
8631 : : an earlier definition (which may be complete). We do not
8632 : : have to loop because nested redefinitions are not allowed. */
8633 : 1066674 : if (flag_isoc23 && ref && C_TYPE_BEING_DEFINED (ref))
8634 : : {
8635 : 84227 : tree vis = previous_tag (ref);
8636 : 84227 : if (vis)
8637 : 16 : ref = vis;
8638 : : }
8639 : :
8640 : : /* If this is the right type of tag, return what we found.
8641 : : (This reference will be shadowed by shadow_tag later if appropriate.)
8642 : : If this is the wrong type of tag, do not return it. If it was the
8643 : : wrong type in the same scope, we will have had an error
8644 : : message already; if in a different scope and declaring
8645 : : a name, pending_xref_error will give an error message; but if in a
8646 : : different scope and not declaring a name, this tag should
8647 : : shadow the previous declaration of a different type of tag, and
8648 : : this would not work properly if we return the reference found.
8649 : : (For example, with "struct foo" in an outer scope, "union foo;"
8650 : : must shadow that tag with a new one of union type.) */
8651 : 2133348 : ret.kind = (ref
8652 : 1066674 : ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
8653 : 92884 : : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
8654 : 1066674 : if (ref && TREE_CODE (ref) == code)
8655 : : {
8656 : 973756 : decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8657 : 973756 : if (C_TYPE_DEFINED_IN_STRUCT (ref)
8658 : 5 : && loc != UNKNOWN_LOCATION
8659 : 973761 : && warn_cxx_compat)
8660 : : {
8661 : 5 : auto_diagnostic_group d;
8662 : 5 : switch (code)
8663 : : {
8664 : 2 : case ENUMERAL_TYPE:
8665 : 2 : if (warning_at (loc, OPT_Wc___compat,
8666 : : ("enum type defined in struct or union "
8667 : : "is not visible in C++")))
8668 : 2 : inform (refloc, "enum type defined here");
8669 : : break;
8670 : 2 : case RECORD_TYPE:
8671 : 2 : if (warning_at (loc, OPT_Wc___compat,
8672 : : ("struct defined in struct or union "
8673 : : "is not visible in C++")))
8674 : 2 : inform (refloc, "struct defined here");
8675 : : break;
8676 : 1 : case UNION_TYPE:
8677 : 1 : if (warning_at (loc, OPT_Wc___compat,
8678 : : ("union defined in struct or union "
8679 : : "is not visible in C++")))
8680 : 1 : inform (refloc, "union defined here");
8681 : : break;
8682 : 0 : default:
8683 : 0 : gcc_unreachable();
8684 : : }
8685 : 5 : }
8686 : :
8687 : 973756 : ret.spec = ref;
8688 : 973756 : return ret;
8689 : : }
8690 : :
8691 : : /* If no such tag is yet defined, create a forward-reference node
8692 : : and record it as the "definition".
8693 : : When a real declaration of this type is found,
8694 : : the forward-reference will be altered into a real type. */
8695 : :
8696 : 92918 : ref = make_node (code);
8697 : 92918 : if (flag_isoc23 || code == ENUMERAL_TYPE)
8698 : 70732 : SET_TYPE_STRUCTURAL_EQUALITY (ref);
8699 : 70732 : if (code == ENUMERAL_TYPE)
8700 : : {
8701 : : /* Give the type a default layout like unsigned int
8702 : : to avoid crashing if it does not get defined. */
8703 : 249 : SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
8704 : 249 : SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
8705 : 249 : TYPE_USER_ALIGN (ref) = 0;
8706 : 249 : TYPE_UNSIGNED (ref) = 1;
8707 : 249 : TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
8708 : 249 : TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
8709 : 249 : TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
8710 : 249 : ENUM_FIXED_UNDERLYING_TYPE_P (ref) = has_enum_type_specifier;
8711 : : }
8712 : :
8713 : 92918 : pushtag (loc, name, ref);
8714 : 92918 : decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8715 : 92918 : if (in_underspecified_init)
8716 : 9 : error_at (loc, "%qT declared in underspecified object initializer",
8717 : : ref);
8718 : :
8719 : 92918 : ret.spec = ref;
8720 : 92918 : return ret;
8721 : : }
8722 : :
8723 : : /* Get the struct, enum or union (CODE says which) with tag NAME.
8724 : : Define the tag as a forward-reference if it is not defined.
8725 : : Return a tree for the type. */
8726 : :
8727 : : tree
8728 : 0 : xref_tag (enum tree_code code, tree name)
8729 : : {
8730 : 0 : return parser_xref_tag (input_location, code, name, false, NULL_TREE,
8731 : 0 : false).spec;
8732 : : }
8733 : :
8734 : : /* Make sure that the tag NAME is defined *in the current scope*
8735 : : at least as a forward reference.
8736 : : LOC is the location of the struct's definition.
8737 : : CODE says which kind of tag NAME ought to be.
8738 : :
8739 : : This stores the current value of the file static STRUCT_PARSE_INFO
8740 : : in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8741 : : new c_struct_parse_info structure. The old value of
8742 : : STRUCT_PARSE_INFO is restored in finish_struct. */
8743 : :
8744 : : tree
8745 : 1153934 : start_struct (location_t loc, enum tree_code code, tree name,
8746 : : class c_struct_parse_info **enclosing_struct_parse_info)
8747 : : {
8748 : : /* If there is already a tag defined at this scope
8749 : : (as a forward reference), just return it. */
8750 : :
8751 : 1153934 : tree ref = NULL_TREE;
8752 : 1153934 : location_t refloc = UNKNOWN_LOCATION;
8753 : :
8754 : 1153934 : if (name != NULL_TREE)
8755 : 476358 : ref = lookup_tag (code, name, true, &refloc);
8756 : :
8757 : : /* For C23, even if we already have a completed definition,
8758 : : we do not use it. We will check for consistency later.
8759 : : If we are in a nested redefinition the type is not
8760 : : complete. We will then detect this below. */
8761 : 1153934 : if (flag_isoc23 && ref && TYPE_SIZE (ref))
8762 : : ref = NULL_TREE;
8763 : :
8764 : 1153829 : if (ref && TREE_CODE (ref) == code)
8765 : : {
8766 : 24686 : if (TYPE_STUB_DECL (ref))
8767 : 24686 : refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8768 : :
8769 : 24686 : if (TYPE_SIZE (ref))
8770 : : {
8771 : 11 : auto_diagnostic_group d;
8772 : 11 : if (code == UNION_TYPE)
8773 : 2 : error_at (loc, "redefinition of %<union %E%>", name);
8774 : : else
8775 : 9 : error_at (loc, "redefinition of %<struct %E%>", name);
8776 : 11 : if (refloc != UNKNOWN_LOCATION)
8777 : 11 : inform (refloc, "originally defined here");
8778 : : /* Don't create structures using a name already in use. */
8779 : 11 : ref = NULL_TREE;
8780 : 11 : }
8781 : 24675 : else if (C_TYPE_BEING_DEFINED (ref))
8782 : : {
8783 : 17 : if (code == UNION_TYPE)
8784 : 1 : error_at (loc, "nested redefinition of %<union %E%>", name);
8785 : : else
8786 : 16 : error_at (loc, "nested redefinition of %<struct %E%>", name);
8787 : : /* Don't bother to report "originally defined here" for a
8788 : : nested redefinition; the original definition should be
8789 : : obvious. */
8790 : : /* Don't create structures that contain themselves. */
8791 : : ref = NULL_TREE;
8792 : : }
8793 : : }
8794 : :
8795 : : /* Otherwise create a forward-reference just so the tag is in scope. */
8796 : :
8797 : 24672 : if (ref == NULL_TREE || TREE_CODE (ref) != code)
8798 : : {
8799 : 1129276 : ref = make_node (code);
8800 : 1129276 : if (flag_isoc23)
8801 : 901298 : SET_TYPE_STRUCTURAL_EQUALITY (ref);
8802 : 1129276 : pushtag (loc, name, ref);
8803 : : }
8804 : :
8805 : 1153934 : C_TYPE_BEING_DEFINED (ref) = 1;
8806 : 2337966 : for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
8807 : 1184032 : TYPE_PACKED (v) = flag_pack_struct;
8808 : :
8809 : 1153934 : *enclosing_struct_parse_info = struct_parse_info;
8810 : 1153934 : struct_parse_info = new c_struct_parse_info ();
8811 : 1153934 : struct_parse_info->refloc = refloc;
8812 : :
8813 : : /* FIXME: This will issue a warning for a use of a type defined
8814 : : within a statement expr used within sizeof, et. al. This is not
8815 : : terribly serious as C++ doesn't permit statement exprs within
8816 : : sizeof anyhow. */
8817 : 1153934 : if (warn_cxx_compat
8818 : 574 : && (in_sizeof || in_typeof || in_alignof || in_countof))
8819 : 10 : warning_at (loc, OPT_Wc___compat,
8820 : : "defining type in %qs expression is invalid in C++",
8821 : : (in_sizeof ? "sizeof"
8822 : 4 : : in_typeof ? "typeof"
8823 : 1 : : in_alignof ? "alignof"
8824 : : : "_Countof"));
8825 : :
8826 : 1153934 : if (in_underspecified_init)
8827 : 18 : error_at (loc, "%qT defined in underspecified object initializer", ref);
8828 : :
8829 : 1153934 : return ref;
8830 : : }
8831 : :
8832 : : /* Process the specs, declarator and width (NULL if omitted)
8833 : : of a structure component, returning a FIELD_DECL node.
8834 : : WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
8835 : : DECL_ATTRS is as for grokdeclarator.
8836 : :
8837 : : LOC is the location of the structure component.
8838 : :
8839 : : This is done during the parsing of the struct declaration.
8840 : : The FIELD_DECL nodes are chained together and the lot of them
8841 : : are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
8842 : :
8843 : : tree
8844 : 4235819 : grokfield (location_t loc,
8845 : : struct c_declarator *declarator, struct c_declspecs *declspecs,
8846 : : tree width, tree *decl_attrs, tree *expr)
8847 : : {
8848 : 4235819 : tree value;
8849 : :
8850 : 4235819 : if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
8851 : 19741 : && width == NULL_TREE)
8852 : : {
8853 : : /* This is an unnamed decl.
8854 : :
8855 : : If we have something of the form "union { list } ;" then this
8856 : : is the anonymous union extension. Similarly for struct.
8857 : :
8858 : : If this is something of the form "struct foo;", then
8859 : : If MS or Plan 9 extensions are enabled, this is handled as
8860 : : an anonymous struct.
8861 : : Otherwise this is a forward declaration of a structure tag.
8862 : :
8863 : : If this is something of the form "foo;" and foo is a TYPE_DECL, then
8864 : : If foo names a structure or union without a tag, then this
8865 : : is an anonymous struct (this is permitted by C11).
8866 : : If MS or Plan 9 extensions are enabled and foo names a
8867 : : structure, then again this is an anonymous struct.
8868 : : Otherwise this is an error.
8869 : :
8870 : : Oh what a horrid tangled web we weave. I wonder if MS consciously
8871 : : took this from Plan 9 or if it was an accident of implementation
8872 : : that took root before someone noticed the bug... */
8873 : :
8874 : 10555 : tree type = declspecs->type;
8875 : 10555 : bool ok = false;
8876 : :
8877 : 10555 : if (RECORD_OR_UNION_TYPE_P (type)
8878 : 10549 : && (flag_ms_extensions
8879 : 10539 : || flag_plan9_extensions
8880 : 10524 : || !declspecs->typedef_p))
8881 : : {
8882 : 10542 : if (flag_ms_extensions || flag_plan9_extensions)
8883 : : ok = true;
8884 : 10517 : else if (TYPE_NAME (type) == NULL)
8885 : : ok = true;
8886 : : else
8887 : : ok = false;
8888 : : }
8889 : : if (!ok)
8890 : : {
8891 : 29 : pedwarn (loc, 0, "declaration does not declare anything");
8892 : 29 : return NULL_TREE;
8893 : : }
8894 : 10526 : if (flag_isoc99)
8895 : 10504 : pedwarn_c99 (loc, OPT_Wpedantic,
8896 : : "ISO C99 doesn%'t support unnamed structs/unions");
8897 : : else
8898 : 22 : pedwarn_c99 (loc, OPT_Wpedantic,
8899 : : "ISO C90 doesn%'t support unnamed structs/unions");
8900 : : }
8901 : :
8902 : 4235790 : value = grokdeclarator (declarator, declspecs, FIELD, false,
8903 : 4235790 : width ? &width : NULL, decl_attrs, expr, NULL,
8904 : : DEPRECATED_NORMAL);
8905 : :
8906 : 4235790 : finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
8907 : 4235790 : DECL_INITIAL (value) = width;
8908 : 4235790 : if (width)
8909 : 51878 : SET_DECL_C_BIT_FIELD (value);
8910 : :
8911 : 4235790 : if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
8912 : : {
8913 : : /* If we currently have a binding for this field, set the
8914 : : in_struct field in the binding, so that we warn about lookups
8915 : : which find it. */
8916 : 1312 : struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
8917 : 1312 : if (b != NULL)
8918 : : {
8919 : : /* If the in_struct field is not yet set, push it on a list
8920 : : to be cleared when this struct is finished. */
8921 : 93 : if (!b->in_struct)
8922 : : {
8923 : 91 : struct_parse_info->fields.safe_push (b);
8924 : 91 : b->in_struct = 1;
8925 : : }
8926 : : }
8927 : : }
8928 : :
8929 : : return value;
8930 : : }
8931 : :
8932 : : /* Subroutine of detect_field_duplicates: return whether X and Y,
8933 : : which are both fields in the same struct, have duplicate field
8934 : : names. */
8935 : :
8936 : : static bool
8937 : 4224266 : is_duplicate_field (tree x, tree y)
8938 : : {
8939 : 4224266 : if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
8940 : : return true;
8941 : :
8942 : : /* When using -fplan9-extensions, an anonymous field whose name is a
8943 : : typedef can duplicate a field name. */
8944 : 4224265 : if (flag_plan9_extensions
8945 : 4224265 : && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
8946 : : {
8947 : 0 : tree xt, xn, yt, yn;
8948 : :
8949 : 0 : xt = TREE_TYPE (x);
8950 : 0 : if (DECL_NAME (x) != NULL_TREE)
8951 : 0 : xn = DECL_NAME (x);
8952 : 0 : else if (RECORD_OR_UNION_TYPE_P (xt)
8953 : 0 : && TYPE_NAME (xt) != NULL_TREE
8954 : 0 : && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
8955 : 0 : xn = DECL_NAME (TYPE_NAME (xt));
8956 : : else
8957 : : xn = NULL_TREE;
8958 : :
8959 : 0 : yt = TREE_TYPE (y);
8960 : 0 : if (DECL_NAME (y) != NULL_TREE)
8961 : 0 : yn = DECL_NAME (y);
8962 : 0 : else if (RECORD_OR_UNION_TYPE_P (yt)
8963 : 0 : && TYPE_NAME (yt) != NULL_TREE
8964 : 0 : && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
8965 : 0 : yn = DECL_NAME (TYPE_NAME (yt));
8966 : : else
8967 : : yn = NULL_TREE;
8968 : :
8969 : 0 : if (xn != NULL_TREE && xn == yn)
8970 : 0 : return true;
8971 : : }
8972 : :
8973 : : return false;
8974 : : }
8975 : :
8976 : : /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
8977 : : to HTAB, giving errors for any duplicates. */
8978 : :
8979 : : static void
8980 : 78930 : detect_field_duplicates_hash (tree fieldlist,
8981 : : hash_table<nofree_ptr_hash <tree_node> > *htab)
8982 : : {
8983 : 78930 : tree x, y;
8984 : 78930 : tree_node **slot;
8985 : :
8986 : 1390081 : for (x = fieldlist; x ; x = DECL_CHAIN (x))
8987 : 1311151 : if ((y = DECL_NAME (x)) != NULL_TREE)
8988 : : {
8989 : 1291446 : slot = htab->find_slot (y, INSERT);
8990 : 1291446 : if (*slot)
8991 : : {
8992 : 15 : error ("duplicate member %q+D", x);
8993 : 15 : DECL_NAME (x) = NULL_TREE;
8994 : : }
8995 : 1291446 : *slot = y;
8996 : : }
8997 : 19705 : else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8998 : : {
8999 : 11231 : detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
9000 : :
9001 : : /* When using -fplan9-extensions, an anonymous field whose
9002 : : name is a typedef can duplicate a field name. */
9003 : 11231 : if (flag_plan9_extensions
9004 : 15 : && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9005 : 11245 : && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
9006 : : {
9007 : 2 : tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
9008 : 2 : slot = htab->find_slot (xn, INSERT);
9009 : 2 : if (*slot)
9010 : 0 : error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
9011 : 2 : *slot = xn;
9012 : : }
9013 : : }
9014 : 78930 : }
9015 : :
9016 : : /* Generate an error for any duplicate field names in FIELDLIST. Munge
9017 : : the list such that this does not present a problem later. */
9018 : :
9019 : : static void
9020 : 1154028 : detect_field_duplicates (tree fieldlist)
9021 : : {
9022 : 1154028 : tree x, y;
9023 : 1154028 : int timeout = 10;
9024 : :
9025 : : /* If the struct is the list of instance variables of an Objective-C
9026 : : class, then we need to check all the instance variables of
9027 : : superclasses when checking for duplicates (since you can't have
9028 : : an instance variable in a subclass with the same name as an
9029 : : instance variable in a superclass). We pass on this job to the
9030 : : Objective-C compiler. objc_detect_field_duplicates() will return
9031 : : false if we are not checking the list of instance variables and
9032 : : the C frontend should proceed with the standard field duplicate
9033 : : checks. If we are checking the list of instance variables, the
9034 : : ObjC frontend will do the check, emit the errors if needed, and
9035 : : then return true. */
9036 : 1154028 : if (c_dialect_objc ())
9037 : 0 : if (objc_detect_field_duplicates (false))
9038 : : return;
9039 : :
9040 : : /* First, see if there are more than "a few" fields.
9041 : : This is trivially true if there are zero or one fields. */
9042 : 1154028 : if (!fieldlist || !DECL_CHAIN (fieldlist))
9043 : : return;
9044 : : x = fieldlist;
9045 : 3400911 : do {
9046 : 3400911 : timeout--;
9047 : 3400911 : if (DECL_NAME (x) == NULL_TREE
9048 : 3400911 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9049 : : timeout = 0;
9050 : 3400911 : x = DECL_CHAIN (x);
9051 : 3400911 : } while (timeout > 0 && x);
9052 : :
9053 : : /* If there were "few" fields and no anonymous structures or unions,
9054 : : avoid the overhead of allocating a hash table. Instead just do
9055 : : the nested traversal thing. */
9056 : 951283 : if (timeout > 0)
9057 : : {
9058 : 2759671 : for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
9059 : : /* When using -fplan9-extensions, we can have duplicates
9060 : : between typedef names and fields. */
9061 : 1876087 : if (DECL_NAME (x)
9062 : 1876087 : || (flag_plan9_extensions
9063 : 0 : && DECL_NAME (x) == NULL_TREE
9064 : 0 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9065 : 0 : && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9066 : 0 : && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
9067 : : {
9068 : 6099818 : for (y = fieldlist; y != x; y = TREE_CHAIN (y))
9069 : 4224266 : if (is_duplicate_field (y, x))
9070 : : {
9071 : 1 : error ("duplicate member %q+D", x);
9072 : 1 : DECL_NAME (x) = NULL_TREE;
9073 : : }
9074 : : }
9075 : : }
9076 : : else
9077 : : {
9078 : 67699 : hash_table<nofree_ptr_hash <tree_node> > htab (37);
9079 : 67699 : detect_field_duplicates_hash (fieldlist, &htab);
9080 : 67699 : }
9081 : : }
9082 : :
9083 : : /* Finish up struct info used by -Wc++-compat. */
9084 : :
9085 : : static void
9086 : 575 : warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
9087 : : location_t record_loc)
9088 : : {
9089 : 575 : unsigned int ix;
9090 : 575 : tree x;
9091 : 575 : struct c_binding *b;
9092 : :
9093 : 575 : if (fieldlist == NULL_TREE)
9094 : : {
9095 : 9 : if (code == RECORD_TYPE)
9096 : 6 : warning_at (record_loc, OPT_Wc___compat,
9097 : : "empty struct has size 0 in C, size 1 in C++");
9098 : : else
9099 : 3 : warning_at (record_loc, OPT_Wc___compat,
9100 : : "empty union has size 0 in C, size 1 in C++");
9101 : : }
9102 : :
9103 : : /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
9104 : : the current struct. We do this now at the end of the struct
9105 : : because the flag is used to issue visibility warnings, and we
9106 : : only want to issue those warnings if the type is referenced
9107 : : outside of the struct declaration. */
9108 : 623 : FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
9109 : 48 : C_TYPE_DEFINED_IN_STRUCT (x) = 1;
9110 : :
9111 : : /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
9112 : : typedefs used when declaring fields in this struct. If the name
9113 : : of any of the fields is also a typedef name then the struct would
9114 : : not parse in C++, because the C++ lookup rules say that the
9115 : : typedef name would be looked up in the context of the struct, and
9116 : : would thus be the field rather than the typedef. */
9117 : 575 : if (!struct_parse_info->typedefs_seen.is_empty ()
9118 : 67 : && fieldlist != NULL_TREE)
9119 : : {
9120 : : /* Use a hash_set<tree> using the name of the typedef. We can use
9121 : : a hash_set<tree> because identifiers are interned. */
9122 : 67 : hash_set<tree> tset;
9123 : :
9124 : 254 : FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
9125 : 120 : tset.add (DECL_NAME (x));
9126 : :
9127 : 387 : for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
9128 : : {
9129 : 320 : if (DECL_NAME (x) != NULL_TREE
9130 : 320 : && tset.contains (DECL_NAME (x)))
9131 : : {
9132 : 2 : warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
9133 : : ("using %qD as both field and typedef name is "
9134 : : "invalid in C++"),
9135 : : x);
9136 : : /* FIXME: It would be nice to report the location where
9137 : : the typedef name is used. */
9138 : : }
9139 : : }
9140 : 67 : }
9141 : :
9142 : : /* For each field which has a binding and which was not defined in
9143 : : an enclosing struct, clear the in_struct field. */
9144 : 666 : FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
9145 : 91 : b->in_struct = 0;
9146 : 575 : }
9147 : :
9148 : : /* Function to help qsort sort FIELD_DECLs by name order. */
9149 : :
9150 : : static int
9151 : 19961760 : field_decl_cmp (const void *x_p, const void *y_p)
9152 : : {
9153 : 19961760 : const tree *const x = (const tree *) x_p;
9154 : 19961760 : const tree *const y = (const tree *) y_p;
9155 : :
9156 : 19961760 : if (DECL_NAME (*x) == DECL_NAME (*y))
9157 : : /* A nontype is "greater" than a type. */
9158 : 0 : return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9159 : 19961760 : if (DECL_NAME (*x) == NULL_TREE)
9160 : : return -1;
9161 : 19961760 : if (DECL_NAME (*y) == NULL_TREE)
9162 : : return 1;
9163 : 19961760 : if (DECL_NAME (*x) < DECL_NAME (*y))
9164 : 9831380 : return -1;
9165 : : return 1;
9166 : : }
9167 : :
9168 : : /* If this structure or union completes the type of any previous
9169 : : variable declaration, lay it out and output its rtl. */
9170 : : static void
9171 : 1329204 : finish_incomplete_vars (tree incomplete_vars, bool toplevel)
9172 : : {
9173 : 1329327 : for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
9174 : : {
9175 : 123 : tree decl = TREE_VALUE (x);
9176 : 123 : if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9177 : 0 : layout_array_type (TREE_TYPE (decl));
9178 : 123 : if (TREE_CODE (decl) != TYPE_DECL)
9179 : : {
9180 : 123 : relayout_decl (decl);
9181 : 123 : if (c_dialect_objc ())
9182 : 0 : objc_check_decl (decl);
9183 : 123 : rest_of_decl_compilation (decl, toplevel, 0);
9184 : : }
9185 : : }
9186 : 1329204 : }
9187 : :
9188 : : /* Determine whether the FIELD_DECL X is a flexible array member according to
9189 : : the following info:
9190 : : A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT;
9191 : : B. whether the FIELD_DECL is an array that is declared as "[]", "[0]",
9192 : : or "[1]";
9193 : : C. flag_strict_flex_arrays;
9194 : : D. the attribute strict_flex_array that is attached to the field
9195 : : if presenting.
9196 : : Return TRUE when it's a flexible array member, FALSE otherwise. */
9197 : :
9198 : : static bool
9199 : 4235870 : is_flexible_array_member_p (bool is_last_field,
9200 : : tree x)
9201 : : {
9202 : : /* If not the last field, return false. */
9203 : 4235870 : if (!is_last_field)
9204 : : return false;
9205 : :
9206 : : /* If not an array field, return false. */
9207 : 1616536 : if (TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
9208 : : return false;
9209 : :
9210 : 520858 : bool is_zero_length_array = zero_length_array_type_p (TREE_TYPE (x));
9211 : 520858 : bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x));
9212 : 520858 : bool is_flexible_array = c_flexible_array_member_type_p (TREE_TYPE (x));
9213 : :
9214 : 520858 : unsigned int strict_flex_array_level = c_strict_flex_array_level_of (x);
9215 : :
9216 : 520858 : switch (strict_flex_array_level)
9217 : : {
9218 : : case 0:
9219 : : /* Default, all trailing arrays are flexible array members. */
9220 : : return true;
9221 : 77 : case 1:
9222 : : /* Level 1: all "[1]", "[0]", and "[]" are flexible array members. */
9223 : 77 : if (is_one_element_array)
9224 : : return true;
9225 : : /* FALLTHROUGH. */
9226 : 141 : case 2:
9227 : : /* Level 2: all "[0]", and "[]" are flexible array members. */
9228 : 141 : if (is_zero_length_array)
9229 : : return true;
9230 : : /* FALLTHROUGH. */
9231 : 201 : case 3:
9232 : : /* Level 3: Only "[]" are flexible array members. */
9233 : 201 : if (is_flexible_array)
9234 : : return true;
9235 : : break;
9236 : 0 : default:
9237 : 0 : gcc_unreachable ();
9238 : : }
9239 : : return false;
9240 : : }
9241 : :
9242 : : /* Recompute TYPE_CANONICAL for variants of the type including qualified
9243 : : versions of the type and related pointer types after an aggregate type
9244 : : has been finalized.
9245 : : Will not update array types, pointers to array types, function
9246 : : types and other derived types created while the type was still
9247 : : incomplete, those will remain TYPE_STRUCTURAL_EQUALITY_P. */
9248 : :
9249 : : static void
9250 : 1164650 : c_update_type_canonical (tree t)
9251 : : {
9252 : 1164650 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t && !TYPE_QUALS (t));
9253 : 2355466 : for (tree x = t, l = NULL_TREE; x; l = x, x = TYPE_NEXT_VARIANT (x))
9254 : : {
9255 : 1190816 : if (x != t && TYPE_STRUCTURAL_EQUALITY_P (x))
9256 : : {
9257 : 26166 : if (!TYPE_QUALS (x))
9258 : 25544 : TYPE_CANONICAL (x) = TYPE_CANONICAL (t);
9259 : : else
9260 : : {
9261 : 622 : tree
9262 : 622 : c = c_build_qualified_type (TYPE_CANONICAL (t), TYPE_QUALS (x));
9263 : 622 : if (TYPE_STRUCTURAL_EQUALITY_P (c))
9264 : : {
9265 : 610 : gcc_checking_assert (TYPE_CANONICAL (t) == t);
9266 : 610 : if (c == x)
9267 : 610 : TYPE_CANONICAL (x) = x;
9268 : : else
9269 : : {
9270 : : /* build_qualified_type for this function unhelpfully
9271 : : moved c from some later spot in TYPE_MAIN_VARIANT (t)
9272 : : chain to right after t (or created it there). Move
9273 : : it right before x and process c and then x. */
9274 : 0 : gcc_checking_assert (TYPE_NEXT_VARIANT (t) == c);
9275 : 0 : if (l != t)
9276 : : {
9277 : 0 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (c);
9278 : 0 : TYPE_NEXT_VARIANT (l) = c;
9279 : 0 : TYPE_NEXT_VARIANT (c) = x;
9280 : : }
9281 : 0 : TYPE_CANONICAL (c) = c;
9282 : 0 : x = c;
9283 : : }
9284 : : }
9285 : : else
9286 : 12 : TYPE_CANONICAL (x) = TYPE_CANONICAL (c);
9287 : : }
9288 : : }
9289 : 1164650 : else if (x != t)
9290 : 0 : continue;
9291 : 1258360 : for (tree p = TYPE_POINTER_TO (x); p; p = TYPE_NEXT_PTR_TO (p))
9292 : : {
9293 : 67544 : if (!TYPE_STRUCTURAL_EQUALITY_P (p))
9294 : 0 : continue;
9295 : 67544 : if (TYPE_CANONICAL (x) != x || TYPE_REF_CAN_ALIAS_ALL (p))
9296 : 2432 : TYPE_CANONICAL (p)
9297 : 4864 : = c_build_pointer_type_for_mode (TYPE_CANONICAL (x), TYPE_MODE (p),
9298 : : false);
9299 : : else
9300 : 65112 : TYPE_CANONICAL (p) = p;
9301 : 67544 : c_update_type_canonical (p);
9302 : : }
9303 : : }
9304 : 1164650 : }
9305 : :
9306 : : /* Verify the argument of the counted_by attribute of each of the
9307 : : FIELDS_WITH_COUNTED_BY is a valid field of the containing structure,
9308 : : STRUCT_TYPE, Report error and remove the corresponding attribute
9309 : : when it's not. */
9310 : :
9311 : : static void
9312 : 157 : verify_counted_by_attribute (tree struct_type,
9313 : : auto_vec<tree> *fields_with_counted_by)
9314 : : {
9315 : 642 : for (tree field_decl : *fields_with_counted_by)
9316 : : {
9317 : 171 : tree attr_counted_by = lookup_attribute ("counted_by",
9318 : 171 : DECL_ATTRIBUTES (field_decl));
9319 : :
9320 : 171 : if (!attr_counted_by)
9321 : 0 : continue;
9322 : :
9323 : : /* If there is an counted_by attribute attached to the field,
9324 : : verify it. */
9325 : :
9326 : 171 : tree fieldname = TREE_VALUE (TREE_VALUE (attr_counted_by));
9327 : :
9328 : : /* Verify the argument of the attrbute is a valid field of the
9329 : : containing structure. */
9330 : :
9331 : 171 : tree counted_by_field = lookup_field (struct_type, fieldname);
9332 : :
9333 : : /* Error when the field is not found in the containing structure and
9334 : : remove the corresponding counted_by attribute from the field_decl. */
9335 : 171 : if (!counted_by_field)
9336 : : {
9337 : 4 : error_at (DECL_SOURCE_LOCATION (field_decl),
9338 : : "argument %qE to the %<counted_by%> attribute"
9339 : : " is not a field declaration in the same structure"
9340 : : " as %qD", fieldname, field_decl);
9341 : 4 : DECL_ATTRIBUTES (field_decl)
9342 : 8 : = remove_attribute ("counted_by", DECL_ATTRIBUTES (field_decl));
9343 : : }
9344 : : else
9345 : : /* Error when the field is not with an integer type. */
9346 : : {
9347 : 209 : while (TREE_CHAIN (counted_by_field))
9348 : 42 : counted_by_field = TREE_CHAIN (counted_by_field);
9349 : 167 : tree real_field = TREE_VALUE (counted_by_field);
9350 : :
9351 : 167 : if (!INTEGRAL_TYPE_P (TREE_TYPE (real_field)))
9352 : : {
9353 : 4 : error_at (DECL_SOURCE_LOCATION (field_decl),
9354 : : "argument %qE to the %<counted_by%> attribute"
9355 : : " is not a field declaration with an integer type",
9356 : : fieldname);
9357 : 4 : DECL_ATTRIBUTES (field_decl)
9358 : 8 : = remove_attribute ("counted_by",
9359 : 4 : DECL_ATTRIBUTES (field_decl));
9360 : : }
9361 : : }
9362 : : }
9363 : 157 : }
9364 : :
9365 : : /* TYPE is a struct or union that we're applying may_alias to after the body is
9366 : : parsed. Fixup any POINTER_TO types. */
9367 : :
9368 : : static void
9369 : 363 : c_fixup_may_alias (tree type)
9370 : : {
9371 : 428 : for (tree t = TYPE_POINTER_TO (type); t; t = TYPE_NEXT_PTR_TO (t))
9372 : 190 : for (tree v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
9373 : 125 : TYPE_REF_CAN_ALIAS_ALL (v) = true;
9374 : 363 : }
9375 : :
9376 : : /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
9377 : : LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
9378 : : FIELDLIST is a chain of FIELD_DECL nodes for the fields.
9379 : : ATTRIBUTES are attributes to be applied to the structure.
9380 : :
9381 : : ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
9382 : : the struct was started. */
9383 : :
9384 : : tree
9385 : 1154028 : finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
9386 : : class c_struct_parse_info *enclosing_struct_parse_info,
9387 : : tree *expr)
9388 : : {
9389 : 1154028 : tree x;
9390 : 1154028 : bool toplevel = file_scope == current_scope;
9391 : :
9392 : : /* If this type was previously laid out as a forward reference,
9393 : : make sure we lay it out again. */
9394 : :
9395 : 1154028 : TYPE_SIZE (t) = NULL_TREE;
9396 : :
9397 : 1154028 : decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9398 : :
9399 : 1154028 : if (pedantic)
9400 : : {
9401 : 7318 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9402 : : {
9403 : 7303 : if (DECL_NAME (x) != NULL_TREE)
9404 : : break;
9405 : 18 : if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9406 : : break;
9407 : : }
9408 : :
9409 : 7304 : if (x == NULL_TREE)
9410 : : {
9411 : 15 : if (TREE_CODE (t) == UNION_TYPE)
9412 : : {
9413 : 4 : if (fieldlist)
9414 : 2 : pedwarn (loc, OPT_Wpedantic, "union has no named members");
9415 : : else
9416 : 2 : pedwarn (loc, OPT_Wpedantic, "union has no members");
9417 : : }
9418 : : else
9419 : : {
9420 : 11 : if (fieldlist)
9421 : 2 : pedwarn (loc, OPT_Wpedantic, "struct has no named members");
9422 : : else
9423 : 9 : pedwarn (loc, OPT_Wpedantic, "struct has no members");
9424 : : }
9425 : : }
9426 : : }
9427 : :
9428 : : /* Install struct as DECL_CONTEXT of each field decl.
9429 : : Also process specified field sizes, found in the DECL_INITIAL,
9430 : : storing 0 there after the type has been changed to precision equal
9431 : : to its width, rather than the precision of the specified standard
9432 : : type. (Correct layout requires the original type to have been preserved
9433 : : until now.) */
9434 : :
9435 : 1154028 : bool saw_named_field = false;
9436 : 1154028 : auto_vec<tree> fields_with_counted_by;
9437 : 5389938 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9438 : : {
9439 : : /* Whether this field is the last field of the structure or union.
9440 : : for UNION, any field is the last field of it. */
9441 : 4235910 : bool is_last_field = (DECL_CHAIN (x) == NULL_TREE)
9442 : 4235910 : || (TREE_CODE (t) == UNION_TYPE);
9443 : :
9444 : 4235910 : if (TREE_TYPE (x) == error_mark_node)
9445 : 40 : continue;
9446 : :
9447 : 4235870 : DECL_CONTEXT (x) = t;
9448 : :
9449 : 4235870 : tree t1 = strip_array_types (TREE_TYPE (x));
9450 : : /* If any field is const, the structure type is pseudo-const. */
9451 : 4235870 : if (TREE_READONLY (x))
9452 : 64244 : C_TYPE_FIELDS_READONLY (t) = 1;
9453 : : else
9454 : : {
9455 : : /* A field that is pseudo-const makes the structure likewise. */
9456 : 4171626 : if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
9457 : 641 : C_TYPE_FIELDS_READONLY (t) = 1;
9458 : : }
9459 : :
9460 : : /* Any field that is volatile means variables of this type must be
9461 : : treated in some ways as volatile. */
9462 : 4235870 : if (TREE_THIS_VOLATILE (x))
9463 : : {
9464 : 367 : C_TYPE_FIELDS_VOLATILE (t) = 1;
9465 : 367 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9466 : : }
9467 : :
9468 : : /* Any field that is volatile, restrict-qualified or atomic
9469 : : means the type cannot be used for a constexpr object. */
9470 : 4235870 : if (TYPE_QUALS (t1)
9471 : 4235870 : & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
9472 : 1279 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9473 : 4234591 : else if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_NON_CONSTEXPR (t1))
9474 : 31 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9475 : :
9476 : : /* Any field of nominal variable size implies structure is too. */
9477 : 4235870 : if (C_DECL_VARIABLE_SIZE (x))
9478 : 649 : C_TYPE_VARIABLE_SIZE (t) = 1;
9479 : :
9480 : : /* If any field is variably modified, record this fact. */
9481 : 4235870 : if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (x)))
9482 : 707 : C_TYPE_VARIABLY_MODIFIED (t) = 1;
9483 : :
9484 : 4235870 : if (DECL_C_BIT_FIELD (x))
9485 : : {
9486 : 51887 : unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
9487 : 51887 : DECL_SIZE (x) = bitsize_int (width);
9488 : 51887 : DECL_BIT_FIELD (x) = 1;
9489 : : }
9490 : :
9491 : 4235870 : if (TYPE_PACKED (t)
9492 : 4235870 : && (DECL_BIT_FIELD (x)
9493 : 3235 : || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
9494 : 4442 : DECL_PACKED (x) = 1;
9495 : :
9496 : : /* Detect flexible array member in an invalid context. */
9497 : 4235870 : if (c_flexible_array_member_type_p (TREE_TYPE (x)))
9498 : : {
9499 : 84582 : if (TREE_CODE (t) == UNION_TYPE)
9500 : 29 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9501 : : "flexible array member in union is a GCC extension");
9502 : 84553 : else if (!is_last_field)
9503 : : {
9504 : 3 : error_at (DECL_SOURCE_LOCATION (x),
9505 : : "flexible array member not at end of struct");
9506 : 3 : TREE_TYPE (x) = error_mark_node;
9507 : : }
9508 : 84550 : else if (!saw_named_field)
9509 : 18 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9510 : : "flexible array member in a struct with no named "
9511 : : "members is a GCC extension");
9512 : :
9513 : : /* If there is a counted_by attribute attached to this field,
9514 : : record it here and do more verification later after the
9515 : : whole structure is complete. */
9516 : 84582 : if (lookup_attribute ("counted_by", DECL_ATTRIBUTES (x)))
9517 : 77 : fields_with_counted_by.safe_push (x);
9518 : : }
9519 : :
9520 : 4235870 : if (TREE_CODE (TREE_TYPE (x)) == POINTER_TYPE)
9521 : : /* If there is a counted_by attribute attached to this field,
9522 : : record it here and do more verification later after the
9523 : : whole structure is complete. */
9524 : 700073 : if (lookup_attribute ("counted_by", DECL_ATTRIBUTES (x)))
9525 : 94 : fields_with_counted_by.safe_push (x);
9526 : :
9527 : 25267 : if (pedantic && TREE_CODE (t) == RECORD_TYPE
9528 : 4256892 : && flexible_array_type_p (TREE_TYPE (x)))
9529 : 18 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9530 : : "invalid use of structure with flexible array member");
9531 : :
9532 : : /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */
9533 : 4235870 : DECL_NOT_FLEXARRAY (x) = !is_flexible_array_member_p (is_last_field, x);
9534 : :
9535 : : /* Set TYPE_INCLUDES_FLEXARRAY for the context of x, t.
9536 : : when x is an array and is the last field.
9537 : : There is only one last_field for a structure type, but there might
9538 : : be multiple last_fields for a union type, therefore we should ORed
9539 : : the result for multiple last_fields. */
9540 : 4235870 : if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
9541 : 690113 : TYPE_INCLUDES_FLEXARRAY (t)
9542 : 1380226 : |= is_last_field && c_flexible_array_member_type_p (TREE_TYPE (x));
9543 : : /* Recursively set TYPE_INCLUDES_FLEXARRAY for the context of x, t
9544 : : when x is an union or record and is the last field. */
9545 : 3545757 : else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9546 : 456217 : TYPE_INCLUDES_FLEXARRAY (t)
9547 : 912434 : |= is_last_field && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x));
9548 : :
9549 : 4235870 : if (warn_flex_array_member_not_at_end
9550 : 56 : && !is_last_field
9551 : 22 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9552 : 4235882 : && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x)))
9553 : 5 : warning_at (DECL_SOURCE_LOCATION (x),
9554 : 5 : OPT_Wflex_array_member_not_at_end,
9555 : : "structure containing a flexible array member"
9556 : : " is not at the end of another structure");
9557 : :
9558 : 4235870 : if (DECL_NAME (x)
9559 : 4235870 : || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9560 : : saw_named_field = true;
9561 : :
9562 : 7781627 : if (AGGREGATE_TYPE_P (TREE_TYPE (x))
9563 : 4692087 : && TYPE_TYPELESS_STORAGE (TREE_TYPE (x)))
9564 : 439415 : TYPE_TYPELESS_STORAGE (t) = true;
9565 : : }
9566 : :
9567 : 1154028 : detect_field_duplicates (fieldlist);
9568 : :
9569 : : /* Now we have the nearly final fieldlist. Record it,
9570 : : then lay out the structure or union (including the fields). */
9571 : :
9572 : 1154028 : TYPE_FIELDS (t) = fieldlist;
9573 : :
9574 : 1154028 : maybe_apply_pragma_scalar_storage_order (t);
9575 : :
9576 : 1154028 : layout_type (t);
9577 : :
9578 : 1154028 : if (TYPE_SIZE_UNIT (t)
9579 : 1154028 : && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
9580 : 1153434 : && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
9581 : 2307462 : && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
9582 : 1 : error ("type %qT is too large", t);
9583 : :
9584 : : /* Give bit-fields their proper types and rewrite the type of array fields
9585 : : with scalar component if the enclosing type has reverse storage order. */
9586 : 5389938 : for (tree field = fieldlist; field; field = DECL_CHAIN (field))
9587 : : {
9588 : 4235910 : if (TREE_CODE (field) == FIELD_DECL
9589 : 4235910 : && DECL_INITIAL (field)
9590 : 4287807 : && TREE_TYPE (field) != error_mark_node)
9591 : : {
9592 : 51887 : unsigned HOST_WIDE_INT width
9593 : 51887 : = tree_to_uhwi (DECL_INITIAL (field));
9594 : 51887 : tree type = TREE_TYPE (field);
9595 : 51887 : if (width != TYPE_PRECISION (type))
9596 : : {
9597 : 37737 : if (TREE_CODE (type) == BITINT_TYPE
9598 : 37807 : && width >= (TYPE_UNSIGNED (type) ? 1 : 2))
9599 : 161 : TREE_TYPE (field)
9600 : 322 : = build_bitint_type (width, TYPE_UNSIGNED (type));
9601 : : else
9602 : 37576 : TREE_TYPE (field)
9603 : 37576 : = c_build_bitfield_integer_type (width,
9604 : 37576 : TYPE_UNSIGNED (type));
9605 : 37737 : if (tree attr = c_hardbool_type_attr (type))
9606 : 281 : decl_attributes (&TREE_TYPE (field),
9607 : : copy_list (attr),
9608 : : 0, NULL_TREE);
9609 : 37737 : SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
9610 : : }
9611 : 51887 : DECL_INITIAL (field) = NULL_TREE;
9612 : : }
9613 : 4184023 : else if (TYPE_REVERSE_STORAGE_ORDER (t)
9614 : 693 : && TREE_CODE (field) == FIELD_DECL
9615 : 4184716 : && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
9616 : : {
9617 : 106 : tree ftype = TREE_TYPE (field);
9618 : 106 : tree ctype = strip_array_types (ftype);
9619 : 106 : if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
9620 : : {
9621 : 93 : tree fmain_type = TYPE_MAIN_VARIANT (ftype);
9622 : 93 : tree *typep = &fmain_type;
9623 : 95 : do {
9624 : 95 : *typep = build_distinct_type_copy (*typep);
9625 : 95 : TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
9626 : 95 : typep = &TREE_TYPE (*typep);
9627 : 95 : } while (TREE_CODE (*typep) == ARRAY_TYPE);
9628 : 93 : TREE_TYPE (field)
9629 : 186 : = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
9630 : : }
9631 : : }
9632 : :
9633 : : /* Warn on problematic type punning for storage order purposes. */
9634 : 4235910 : if (TREE_CODE (t) == UNION_TYPE
9635 : 832369 : && TREE_CODE (field) == FIELD_DECL
9636 : 5068279 : && AGGREGATE_TYPE_P (TREE_TYPE (field)))
9637 : : {
9638 : 413397 : tree ftype = TREE_TYPE (field);
9639 : 413397 : if (TREE_CODE (ftype) == ARRAY_TYPE)
9640 : 304820 : ftype = strip_array_types (ftype);
9641 : 413397 : if (RECORD_OR_UNION_TYPE_P (ftype)
9642 : 413397 : && TYPE_REVERSE_STORAGE_ORDER (ftype)
9643 : 109071 : != TYPE_REVERSE_STORAGE_ORDER (t))
9644 : 1 : warning_at (DECL_SOURCE_LOCATION (field),
9645 : 1 : OPT_Wscalar_storage_order,
9646 : : "type punning toggles scalar storage order");
9647 : : }
9648 : : }
9649 : :
9650 : : /* Now we have the truly final field list.
9651 : : Store it in this type and in the variants. */
9652 : :
9653 : 1154028 : TYPE_FIELDS (t) = fieldlist;
9654 : :
9655 : : /* If there are lots of fields, sort so we can look through them fast.
9656 : : We arbitrarily consider 16 or more elts to be "a lot". */
9657 : :
9658 : 1154028 : {
9659 : 1154028 : int len = 0;
9660 : :
9661 : 4954829 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9662 : : {
9663 : 3828108 : if (len > 15 || DECL_NAME (x) == NULL)
9664 : : break;
9665 : 3800801 : len += 1;
9666 : : }
9667 : :
9668 : 1154028 : if (len > 15)
9669 : : {
9670 : 22249 : tree *field_array;
9671 : 22249 : struct lang_type *space;
9672 : 22249 : struct sorted_fields_type *space2;
9673 : :
9674 : 22249 : len += list_length (x);
9675 : :
9676 : : /* Use the same allocation policy here that make_node uses, to
9677 : : ensure that this lives as long as the rest of the struct decl.
9678 : : All decls in an inline function need to be saved. */
9679 : :
9680 : 22249 : space = ((struct lang_type *)
9681 : 22249 : ggc_internal_cleared_alloc (c_dialect_objc ()
9682 : : ? sizeof (struct lang_type)
9683 : : : offsetof (struct lang_type,
9684 : : info)));
9685 : 22249 : space2 = ((sorted_fields_type *)
9686 : 44498 : ggc_internal_alloc (sizeof (struct sorted_fields_type)
9687 : 22249 : + len * sizeof (tree)));
9688 : :
9689 : 22249 : len = 0;
9690 : 22249 : space->s = space2;
9691 : 22249 : field_array = &space2->elts[0];
9692 : 779901 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9693 : : {
9694 : 760786 : field_array[len++] = x;
9695 : :
9696 : : /* If there is anonymous struct or union, break out of the loop. */
9697 : 760786 : if (DECL_NAME (x) == NULL)
9698 : : break;
9699 : : }
9700 : : /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
9701 : 22249 : if (x == NULL)
9702 : : {
9703 : 19115 : TYPE_LANG_SPECIFIC (t) = space;
9704 : 19115 : TYPE_LANG_SPECIFIC (t)->s->len = len;
9705 : 19115 : field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
9706 : 19115 : qsort (field_array, len, sizeof (tree), field_decl_cmp);
9707 : : }
9708 : : }
9709 : : }
9710 : :
9711 : : /* If this was supposed to be a transparent union, but we can't
9712 : : make it one, warn and turn off the flag. */
9713 : 1154028 : if (TREE_CODE (t) == UNION_TYPE
9714 : 367643 : && TYPE_TRANSPARENT_AGGR (t)
9715 : 1154071 : && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
9716 : : {
9717 : 13 : TYPE_TRANSPARENT_AGGR (t) = 0;
9718 : 13 : warning_at (loc, 0, "union cannot be made transparent");
9719 : : }
9720 : :
9721 : : /* Check for consistency with previous definition. */
9722 : 1154028 : if (flag_isoc23 && NULL != enclosing_struct_parse_info)
9723 : : {
9724 : 887150 : tree vistype = previous_tag (t);
9725 : 887150 : if (vistype
9726 : 124 : && TREE_CODE (vistype) == TREE_CODE (t)
9727 : 887274 : && !C_TYPE_BEING_DEFINED (vistype))
9728 : : {
9729 : 104 : TYPE_STUB_DECL (t) = TYPE_STUB_DECL (vistype);
9730 : 104 : if (c_type_variably_modified_p (t))
9731 : : {
9732 : 7 : error ("redefinition of struct or union %qT with variably "
9733 : : "modified type", t);
9734 : 7 : if (struct_parse_info->refloc != UNKNOWN_LOCATION)
9735 : 7 : inform (struct_parse_info->refloc, "originally defined here");
9736 : : }
9737 : 97 : else if (!comptypes_same_p (t, vistype))
9738 : : {
9739 : 38 : error ("redefinition of struct or union %qT", t);
9740 : 38 : if (struct_parse_info->refloc != UNKNOWN_LOCATION)
9741 : 38 : inform (struct_parse_info->refloc, "originally defined here");
9742 : : }
9743 : : }
9744 : : }
9745 : :
9746 : 1154028 : C_TYPE_BEING_DEFINED (t) = 0;
9747 : :
9748 : 1154028 : if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (t)))
9749 : 694 : for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9750 : 363 : c_fixup_may_alias (x);
9751 : :
9752 : : /* Set type canonical based on equivalence class. */
9753 : 1154028 : if (flag_isoc23 && !C_TYPE_VARIABLE_SIZE (t))
9754 : : {
9755 : 922036 : if (c_struct_htab == NULL)
9756 : 35113 : c_struct_htab = hash_table<c_struct_hasher>::create_ggc (61);
9757 : :
9758 : 922036 : hashval_t hash = c_struct_hasher::hash (t);
9759 : :
9760 : 922036 : gcc_checking_assert (TYPE_STRUCTURAL_EQUALITY_P (t));
9761 : 922036 : gcc_checking_assert (!TYPE_NAME (t)
9762 : : || TREE_CODE (TYPE_NAME (t)) == IDENTIFIER_NODE);
9763 : :
9764 : 922036 : tree *e = c_struct_htab->find_slot_with_hash (t, hash, INSERT);
9765 : 922036 : if (*e)
9766 : 61146 : TYPE_CANONICAL (t) = *e;
9767 : : else
9768 : : {
9769 : 860890 : TYPE_CANONICAL (t) = t;
9770 : 860890 : *e = t;
9771 : : }
9772 : 922036 : c_update_type_canonical (t);
9773 : : }
9774 : :
9775 : 1154028 : tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
9776 : 2338339 : for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9777 : : {
9778 : 1184311 : TYPE_FIELDS (x) = TYPE_FIELDS (t);
9779 : 1184311 : TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
9780 : 1184311 : TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
9781 : 1184311 : TYPE_TYPELESS_STORAGE (x) = TYPE_TYPELESS_STORAGE (t);
9782 : 1184311 : C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
9783 : 1184311 : C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
9784 : 1184311 : C_TYPE_FIELDS_NON_CONSTEXPR (x) = C_TYPE_FIELDS_NON_CONSTEXPR (t);
9785 : 1184311 : C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
9786 : 1184311 : C_TYPE_VARIABLY_MODIFIED (x) = C_TYPE_VARIABLY_MODIFIED (t);
9787 : 1184311 : C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
9788 : 1184311 : TYPE_INCLUDES_FLEXARRAY (x) = TYPE_INCLUDES_FLEXARRAY (t);
9789 : : }
9790 : :
9791 : : /* Update type location to the one of the definition, instead of e.g.
9792 : : a forward declaration. */
9793 : 1154028 : if (TYPE_STUB_DECL (t))
9794 : 1154028 : DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
9795 : :
9796 : : /* Finish debugging output for this type. */
9797 : 1154028 : rest_of_type_compilation (t, toplevel);
9798 : :
9799 : 1154028 : finish_incomplete_vars (incomplete_vars, toplevel);
9800 : :
9801 : : /* Make sure a DECL_EXPR is created for structs with VLA members.
9802 : : Because we do not know the context, we always pass expr
9803 : : to force creation of a BIND_EXPR which is required in some
9804 : : contexts. */
9805 : 1154028 : if (c_type_variably_modified_p (t))
9806 : 665 : add_decl_expr (loc, t, expr, false);
9807 : :
9808 : 1154028 : if (warn_cxx_compat)
9809 : 575 : warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
9810 : :
9811 : 1154028 : if (NULL != enclosing_struct_parse_info)
9812 : : {
9813 : 1113508 : delete struct_parse_info;
9814 : :
9815 : 1113508 : struct_parse_info = enclosing_struct_parse_info;
9816 : :
9817 : : /* If this struct is defined inside a struct, add it to
9818 : : struct_types. */
9819 : 1113508 : if (warn_cxx_compat
9820 : : && struct_parse_info != NULL
9821 : 350 : && !in_sizeof && !in_typeof && !in_alignof && !in_countof)
9822 : 345 : struct_parse_info->struct_types.safe_push (t);
9823 : : }
9824 : :
9825 : 1154028 : if (fields_with_counted_by.length () > 0)
9826 : 157 : verify_counted_by_attribute (t, &fields_with_counted_by);
9827 : :
9828 : 1154028 : return t;
9829 : 1154028 : }
9830 : :
9831 : : static struct {
9832 : : gt_pointer_operator new_value;
9833 : : void *cookie;
9834 : : } resort_data;
9835 : :
9836 : : /* This routine compares two fields like field_decl_cmp but using the
9837 : : pointer operator in resort_data. */
9838 : :
9839 : : static int
9840 : 5427 : resort_field_decl_cmp (const void *x_p, const void *y_p)
9841 : : {
9842 : 5427 : const tree *const x = (const tree *) x_p;
9843 : 5427 : const tree *const y = (const tree *) y_p;
9844 : :
9845 : 5427 : if (DECL_NAME (*x) == DECL_NAME (*y))
9846 : : /* A nontype is "greater" than a type. */
9847 : 0 : return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9848 : 5427 : if (DECL_NAME (*x) == NULL_TREE)
9849 : : return -1;
9850 : 5427 : if (DECL_NAME (*y) == NULL_TREE)
9851 : : return 1;
9852 : 5427 : {
9853 : 5427 : tree d1 = DECL_NAME (*x);
9854 : 5427 : tree d2 = DECL_NAME (*y);
9855 : 5427 : resort_data.new_value (&d1, &d1, resort_data.cookie);
9856 : 5427 : resort_data.new_value (&d2, &d2, resort_data.cookie);
9857 : 5427 : if (d1 < d2)
9858 : 2746 : return -1;
9859 : : }
9860 : 2681 : return 1;
9861 : : }
9862 : :
9863 : : /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9864 : :
9865 : : void
9866 : 6 : resort_sorted_fields (void *obj,
9867 : : void * ARG_UNUSED (orig_obj),
9868 : : gt_pointer_operator new_value,
9869 : : void *cookie)
9870 : : {
9871 : 6 : struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9872 : 6 : resort_data.new_value = new_value;
9873 : 6 : resort_data.cookie = cookie;
9874 : 6 : qsort (&sf->elts[0], sf->len, sizeof (tree),
9875 : : resort_field_decl_cmp);
9876 : 6 : }
9877 : :
9878 : : /* Lay out the type T, and its element type, and so on. */
9879 : :
9880 : : static void
9881 : 0 : layout_array_type (tree t)
9882 : : {
9883 : 0 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
9884 : 0 : layout_array_type (TREE_TYPE (t));
9885 : 0 : layout_type (t);
9886 : 0 : }
9887 : :
9888 : : /* Begin compiling the definition of an enumeration type.
9889 : : NAME is its name (or null if anonymous).
9890 : : LOC is the enum's location.
9891 : : FIXED_UNDERLYING_TYPE is the (C23) underlying type specified in the
9892 : : definition.
9893 : : Returns the type object, as yet incomplete.
9894 : : Also records info about it so that build_enumerator
9895 : : may be used to declare the individual values as they are read. */
9896 : :
9897 : : tree
9898 : 175176 : start_enum (location_t loc, struct c_enum_contents *the_enum, tree name,
9899 : : tree fixed_underlying_type, bool potential_nesting_p)
9900 : : {
9901 : 175176 : tree enumtype = NULL_TREE;
9902 : 175176 : location_t enumloc = UNKNOWN_LOCATION;
9903 : :
9904 : : /* If this is the real definition for a previous forward reference,
9905 : : fill in the contents in the same object that used to be the
9906 : : forward reference. */
9907 : :
9908 : 175176 : if (name != NULL_TREE)
9909 : 69741 : enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
9910 : :
9911 : 69741 : if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
9912 : : {
9913 : : /* If the type is currently being defined or if we have seen an
9914 : : incomplete version which is now complete, this is a nested
9915 : : redefinition. The later happens if the redefinition occurs
9916 : : inside the enum specifier itself. */
9917 : 177 : if (C_TYPE_BEING_DEFINED (enumtype)
9918 : 177 : || (potential_nesting_p && TYPE_VALUES (enumtype) != NULL_TREE))
9919 : 9 : error_at (loc, "nested redefinition of %<enum %E%>", name);
9920 : :
9921 : : /* For C23 we allow redefinitions. We set to zero and check for
9922 : : consistency later. */
9923 : 337 : if (flag_isoc23 && TYPE_VALUES (enumtype) != NULL_TREE)
9924 : : enumtype = NULL_TREE;
9925 : : }
9926 : :
9927 : 105586 : if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
9928 : : {
9929 : 175027 : enumtype = make_node (ENUMERAL_TYPE);
9930 : 175027 : TYPE_SIZE (enumtype) = NULL_TREE;
9931 : 175027 : SET_TYPE_STRUCTURAL_EQUALITY (enumtype);
9932 : 175027 : pushtag (loc, name, enumtype);
9933 : 175027 : if (fixed_underlying_type != NULL_TREE)
9934 : : {
9935 : : /* For an enum definition with a fixed underlying type, the
9936 : : type is complete during the definition and the
9937 : : enumeration constants have that type. If there was a
9938 : : tag, the type was completed in c_parser_enum_specifier.
9939 : : If not, it must be completed here. */
9940 : 12 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = true;
9941 : 12 : TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (fixed_underlying_type);
9942 : 12 : TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (fixed_underlying_type);
9943 : 12 : TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (fixed_underlying_type);
9944 : 12 : SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (fixed_underlying_type));
9945 : 12 : TYPE_SIZE (enumtype) = NULL_TREE;
9946 : 12 : TYPE_PRECISION (enumtype) = TYPE_PRECISION (fixed_underlying_type);
9947 : 12 : ENUM_UNDERLYING_TYPE (enumtype) = fixed_underlying_type;
9948 : 12 : TYPE_CANONICAL (enumtype) = TYPE_CANONICAL (fixed_underlying_type);
9949 : 12 : c_update_type_canonical (enumtype);
9950 : 12 : layout_type (enumtype);
9951 : : }
9952 : : }
9953 : : /* Update type location to the one of the definition, instead of e.g.
9954 : : a forward declaration. */
9955 : 149 : else if (TYPE_STUB_DECL (enumtype))
9956 : : {
9957 : 149 : enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
9958 : 149 : DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
9959 : : }
9960 : :
9961 : 175176 : C_TYPE_BEING_DEFINED (enumtype) = 1;
9962 : :
9963 : 175176 : if (TYPE_VALUES (enumtype) != NULL_TREE)
9964 : : {
9965 : : /* This enum is a named one that has been declared already. */
9966 : 6 : auto_diagnostic_group d;
9967 : 6 : error_at (loc, "redeclaration of %<enum %E%>", name);
9968 : 6 : if (enumloc != UNKNOWN_LOCATION)
9969 : 6 : inform (enumloc, "originally defined here");
9970 : :
9971 : : /* Completely replace its old definition.
9972 : : The old enumerators remain defined, however. */
9973 : 6 : TYPE_VALUES (enumtype) = NULL_TREE;
9974 : 6 : }
9975 : :
9976 : 175176 : if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype)
9977 : 175176 : && fixed_underlying_type == NULL_TREE)
9978 : : {
9979 : 3 : error_at (loc, "%<enum%> declared with but defined without "
9980 : : "fixed underlying type");
9981 : 3 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = false;
9982 : : }
9983 : :
9984 : 175176 : the_enum->enum_next_value = integer_zero_node;
9985 : 175176 : the_enum->enum_type = enumtype;
9986 : 175176 : the_enum->enum_overflow = 0;
9987 : :
9988 : 175176 : if (flag_short_enums && !ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
9989 : 106 : for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
9990 : 53 : TYPE_PACKED (v) = 1;
9991 : :
9992 : : /* FIXME: This will issue a warning for a use of a type defined
9993 : : within sizeof in a statement expr. This is not terribly serious
9994 : : as C++ doesn't permit statement exprs within sizeof anyhow. */
9995 : 175176 : if (warn_cxx_compat
9996 : 126 : && (in_sizeof || in_typeof || in_alignof || in_countof))
9997 : 0 : warning_at (loc, OPT_Wc___compat,
9998 : : "defining type in %qs expression is invalid in C++",
9999 : : (in_sizeof ? "sizeof"
10000 : 0 : : in_typeof ? "typeof"
10001 : 0 : : in_alignof ? "alignof"
10002 : : : "_Countof"));
10003 : :
10004 : 175176 : if (in_underspecified_init)
10005 : 6 : error_at (loc, "%qT defined in underspecified object initializer",
10006 : : enumtype);
10007 : :
10008 : 175176 : return enumtype;
10009 : : }
10010 : :
10011 : : /* After processing and defining all the values of an enumeration type,
10012 : : install their decls in the enumeration type and finish it off.
10013 : : ENUMTYPE is the type object, VALUES a list of decl-value pairs,
10014 : : and ATTRIBUTES are the specified attributes.
10015 : : Returns ENUMTYPE. */
10016 : :
10017 : : tree
10018 : 175176 : finish_enum (tree enumtype, tree values, tree attributes)
10019 : : {
10020 : 175176 : tree pair, tem;
10021 : 175176 : tree minnode = NULL_TREE, maxnode = NULL_TREE;
10022 : 175176 : int precision;
10023 : 175176 : signop sign;
10024 : 175176 : bool toplevel = (file_scope == current_scope);
10025 : 175176 : struct lang_type *lt;
10026 : :
10027 : 175176 : decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10028 : :
10029 : : /* Calculate the maximum value of any enumerator in this type. */
10030 : :
10031 : 175176 : if (values == error_mark_node)
10032 : 26 : minnode = maxnode = integer_zero_node;
10033 : : else
10034 : : {
10035 : 175150 : minnode = maxnode = TREE_VALUE (values);
10036 : 5562284 : for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
10037 : : {
10038 : 5387134 : tree value = TREE_VALUE (pair);
10039 : 5387134 : if (tree_int_cst_lt (maxnode, value))
10040 : 5244971 : maxnode = value;
10041 : 5387134 : if (tree_int_cst_lt (value, minnode))
10042 : 61915 : minnode = value;
10043 : : }
10044 : : }
10045 : :
10046 : : /* Construct the final type of this enumeration. It is the same
10047 : : as one of the integral types - the narrowest one that fits, except
10048 : : that normally we only go as narrow as int - and signed iff any of
10049 : : the values are negative. */
10050 : 175176 : sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
10051 : 175176 : precision = MAX (tree_int_cst_min_precision (minnode, sign),
10052 : : tree_int_cst_min_precision (maxnode, sign));
10053 : :
10054 : 175176 : bool wider_than_int =
10055 : 175176 : (tree_int_cst_lt (minnode, TYPE_MIN_VALUE (integer_type_node))
10056 : 175176 : || tree_int_cst_lt (TYPE_MAX_VALUE (integer_type_node), maxnode));
10057 : :
10058 : :
10059 : 175176 : if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
10060 : : {
10061 : : /* If the precision of the type was specified with an attribute and it
10062 : : was too small, give an error. Otherwise, use it. */
10063 : 175058 : if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
10064 : : {
10065 : 23 : if (precision > TYPE_PRECISION (enumtype))
10066 : : {
10067 : 6 : TYPE_PRECISION (enumtype) = 0;
10068 : 6 : error ("specified mode too small for enumerated values");
10069 : : }
10070 : : else
10071 : 17 : precision = TYPE_PRECISION (enumtype);
10072 : : }
10073 : : else
10074 : 175035 : TYPE_PRECISION (enumtype) = 0;
10075 : :
10076 : 175058 : if (TYPE_PACKED (enumtype)
10077 : 174979 : || precision > TYPE_PRECISION (integer_type_node)
10078 : 347460 : || TYPE_PRECISION (enumtype))
10079 : : {
10080 : 2783 : tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
10081 : 2668 : if (tem == NULL)
10082 : : {
10083 : : /* This should only occur when both signed and unsigned
10084 : : values of maximum precision occur among the
10085 : : enumerators. */
10086 : 3 : pedwarn (input_location, 0,
10087 : : "enumeration values exceed range of largest integer");
10088 : 3 : tem = widest_integer_literal_type_node;
10089 : : }
10090 : 2665 : else if (precision > TYPE_PRECISION (intmax_type_node)
10091 : 4 : && !tree_int_cst_lt (minnode,
10092 : 4 : TYPE_MIN_VALUE (intmax_type_node))
10093 : 2668 : && !tree_int_cst_lt (TYPE_MAX_VALUE (uintmax_type_node),
10094 : : maxnode))
10095 : 2 : pedwarn (input_location, OPT_Wpedantic,
10096 : : "enumeration values exceed range of %qs",
10097 : : sign == UNSIGNED ? "uintmax_t" : "intmax_t");
10098 : : }
10099 : : else
10100 : 172390 : tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
10101 : :
10102 : 175058 : TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
10103 : 175058 : TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
10104 : 175058 : TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
10105 : 175058 : SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
10106 : 175058 : TYPE_SIZE (enumtype) = NULL_TREE;
10107 : 175058 : TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
10108 : 175058 : ENUM_UNDERLYING_TYPE (enumtype) =
10109 : 175058 : c_common_type_for_size (TYPE_PRECISION (tem), TYPE_UNSIGNED (tem));
10110 : :
10111 : 525174 : TYPE_CANONICAL (enumtype) =
10112 : 175058 : TYPE_CANONICAL (ENUM_UNDERLYING_TYPE (enumtype));
10113 : 175058 : c_update_type_canonical (enumtype);
10114 : :
10115 : 175058 : layout_type (enumtype);
10116 : : }
10117 : :
10118 : 175176 : if (values != error_mark_node)
10119 : : {
10120 : : /* Change the type of the enumerators to be the enum type. We
10121 : : need to do this irrespective of the size of the enum, for
10122 : : proper type checking. Replace the DECL_INITIALs of the
10123 : : enumerators, and the value slots of the list, with copies
10124 : : that have the enum type; they cannot be modified in place
10125 : : because they may be shared (e.g. integer_zero_node) Finally,
10126 : : change the purpose slots to point to the names of the decls. */
10127 : 5737434 : for (pair = values; pair; pair = TREE_CHAIN (pair))
10128 : : {
10129 : 5562284 : tree enu = TREE_PURPOSE (pair);
10130 : 5562284 : tree ini = DECL_INITIAL (enu);
10131 : :
10132 : 5562284 : TREE_TYPE (enu) = enumtype;
10133 : :
10134 : : /* Before C23, the ISO C Standard mandates enumerators to
10135 : : have type int, even though the underlying type of an enum
10136 : : type is unspecified. However, C23 allows enumerators of
10137 : : any integer type, and if an enumeration has any
10138 : : enumerators wider than int, all enumerators have the
10139 : : enumerated type after it is parsed. Any enumerators that
10140 : : fit in int are given type int in build_enumerator (which
10141 : : is the correct type while the enumeration is being
10142 : : parsed), so no conversions are needed here if all
10143 : : enumerators fit in int. If the enum has a fixed
10144 : : underlying type, the correct type was also given in
10145 : : build_enumerator. */
10146 : 5562284 : if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) && wider_than_int)
10147 : 28521 : ini = convert (enumtype, ini);
10148 : :
10149 : 5562284 : DECL_INITIAL (enu) = ini;
10150 : 5562284 : TREE_PURPOSE (pair) = DECL_NAME (enu);
10151 : : /* To match the C++ FE, store the CONST_DECL rather than just its
10152 : : value. */
10153 : 5562284 : TREE_VALUE (pair) = enu;
10154 : : }
10155 : :
10156 : 175150 : TYPE_VALUES (enumtype) = values;
10157 : : }
10158 : :
10159 : : /* Record the min/max values so that we can warn about bit-field
10160 : : enumerations that are too small for the values. */
10161 : 175176 : lt = ((struct lang_type *)
10162 : 175176 : ggc_internal_cleared_alloc (c_dialect_objc ()
10163 : : ? sizeof (struct lang_type)
10164 : : : offsetof (struct lang_type, info)));
10165 : 175176 : lt->enum_min = minnode;
10166 : 175176 : lt->enum_max = maxnode;
10167 : 175176 : TYPE_LANG_SPECIFIC (enumtype) = lt;
10168 : :
10169 : : /* Fix up all variant types of this enum type. */
10170 : 175176 : tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
10171 : 350362 : for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
10172 : : {
10173 : 175186 : C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
10174 : 175186 : if (tem == enumtype)
10175 : 175176 : continue;
10176 : 10 : TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
10177 : 10 : TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
10178 : 10 : TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
10179 : 10 : TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
10180 : 10 : TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
10181 : 10 : SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
10182 : 10 : TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
10183 : 10 : SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
10184 : 10 : TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
10185 : 10 : TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
10186 : 10 : TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
10187 : 10 : ENUM_UNDERLYING_TYPE (tem) = ENUM_UNDERLYING_TYPE (enumtype);
10188 : 10 : TYPE_PACKED (tem) = TYPE_PACKED (enumtype);
10189 : : }
10190 : :
10191 : : /* Finish debugging output for this type. */
10192 : 175176 : rest_of_type_compilation (enumtype, toplevel);
10193 : :
10194 : 175176 : finish_incomplete_vars (incomplete_vars, toplevel);
10195 : :
10196 : : /* If this enum is defined inside a struct, add it to
10197 : : struct_types. */
10198 : 175176 : if (warn_cxx_compat
10199 : 126 : && struct_parse_info != NULL
10200 : 42 : && !in_sizeof && !in_typeof && !in_alignof && !in_countof)
10201 : 42 : struct_parse_info->struct_types.safe_push (enumtype);
10202 : :
10203 : : /* Check for consistency with previous definition */
10204 : 175176 : if (flag_isoc23)
10205 : : {
10206 : 137216 : tree vistype = previous_tag (enumtype);
10207 : 137216 : if (vistype
10208 : 33 : && TREE_CODE (vistype) == TREE_CODE (enumtype)
10209 : 137245 : && !C_TYPE_BEING_DEFINED (vistype))
10210 : : {
10211 : 29 : TYPE_STUB_DECL (enumtype) = TYPE_STUB_DECL (vistype);
10212 : 29 : if (!comptypes_same_p (enumtype, vistype))
10213 : 3 : error("conflicting redefinition of enum %qT", enumtype);
10214 : : }
10215 : : }
10216 : :
10217 : 175176 : C_TYPE_BEING_DEFINED (enumtype) = 0;
10218 : :
10219 : 175176 : return enumtype;
10220 : : }
10221 : :
10222 : : /* Build and install a CONST_DECL for one value of the
10223 : : current enumeration type (one that was begun with start_enum).
10224 : : DECL_LOC is the location of the enumerator.
10225 : : LOC is the location of the '=' operator if any, DECL_LOC otherwise.
10226 : : Return a tree-list containing the CONST_DECL and its value.
10227 : : Assignment of sequential values by default is handled here. */
10228 : :
10229 : : tree
10230 : 5562301 : build_enumerator (location_t decl_loc, location_t loc,
10231 : : struct c_enum_contents *the_enum, tree name, tree value)
10232 : : {
10233 : 5562301 : tree decl;
10234 : 5562301 : tree old_decl;
10235 : :
10236 : : /* Validate and default VALUE. */
10237 : :
10238 : 5562301 : if (value != NULL_TREE)
10239 : : {
10240 : : /* Don't issue more errors for error_mark_node (i.e. an
10241 : : undeclared identifier) - just ignore the value expression. */
10242 : 3436839 : if (value == error_mark_node)
10243 : : value = NULL_TREE;
10244 : 3436174 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
10245 : : {
10246 : 6 : error_at (loc, "enumerator value for %qE is not an integer constant",
10247 : : name);
10248 : 6 : value = NULL_TREE;
10249 : : }
10250 : : else
10251 : : {
10252 : 3436168 : if (TREE_CODE (value) != INTEGER_CST)
10253 : : {
10254 : 97 : value = c_fully_fold (value, false, NULL);
10255 : 97 : if (TREE_CODE (value) == INTEGER_CST)
10256 : 48 : pedwarn (loc, OPT_Wpedantic,
10257 : : "enumerator value for %qE is not an integer "
10258 : : "constant expression", name);
10259 : : }
10260 : 3436168 : if (TREE_CODE (value) != INTEGER_CST)
10261 : : {
10262 : 49 : error ("enumerator value for %qE is not an integer constant",
10263 : : name);
10264 : 49 : value = NULL_TREE;
10265 : : }
10266 : : else
10267 : : {
10268 : 3436119 : value = default_conversion (value);
10269 : 3436119 : constant_expression_warning (value);
10270 : : }
10271 : : }
10272 : : }
10273 : :
10274 : : /* Default based on previous value. */
10275 : : /* It should no longer be possible to have NON_LVALUE_EXPR
10276 : : in the default. */
10277 : 3436174 : if (value == NULL_TREE)
10278 : : {
10279 : 2126182 : value = the_enum->enum_next_value;
10280 : 2126182 : if (the_enum->enum_overflow)
10281 : 8 : error_at (loc, "overflow in enumeration values");
10282 : : }
10283 : 5562301 : if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10284 : : {
10285 : : /* Enumeration constants must fit in the fixed underlying type. */
10286 : 163 : if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (the_enum->enum_type)))
10287 : 4 : error_at (loc,
10288 : : "enumerator value outside the range of underlying type");
10289 : : /* Enumeration constants for an enum with fixed underlying type
10290 : : have the enum type, both inside and outside the
10291 : : definition. */
10292 : 163 : value = convert (the_enum->enum_type, value);
10293 : : }
10294 : 5562138 : else if (flag_isoc23
10295 : 5149826 : && (old_decl = lookup_name_in_scope (name, current_scope))
10296 : 35 : && old_decl != error_mark_node
10297 : 35 : && TREE_TYPE (old_decl)
10298 : 35 : && TREE_TYPE (TREE_TYPE (old_decl))
10299 : 5562172 : && TREE_CODE (old_decl) == CONST_DECL)
10300 : : {
10301 : : /* Enumeration constants in a redeclaration have the previous type. */
10302 : 34 : tree previous_type = TREE_TYPE (DECL_INITIAL (old_decl));
10303 : 34 : if (!int_fits_type_p (value, previous_type))
10304 : : {
10305 : 2 : error_at (loc, "value of redeclared enumerator outside the range "
10306 : : "of %qT", previous_type);
10307 : 2 : locate_old_decl (old_decl);
10308 : : }
10309 : 34 : value = convert (previous_type, value);
10310 : : }
10311 : : else
10312 : : {
10313 : : /* Even though the underlying type of an enum is unspecified, the
10314 : : type of enumeration constants is explicitly defined as int
10315 : : (6.4.4.3/2 in the C99 Standard). C23 allows any integer type, and
10316 : : GCC allows such types for older standards as an extension. */
10317 : 5562104 : bool warned_range = false;
10318 : 5562104 : if (!int_fits_type_p (value,
10319 : 5562104 : (TYPE_UNSIGNED (TREE_TYPE (value))
10320 : : ? uintmax_type_node
10321 : : : intmax_type_node)))
10322 : : /* GCC does not consider its types larger than intmax_t to be
10323 : : extended integer types (although C23 would permit such types to
10324 : : be considered extended integer types if all the features
10325 : : required by <stdint.h> and <inttypes.h> macros, such as support
10326 : : for integer constants and I/O, were present), so diagnose if
10327 : : such a wider type is used. (If the wider type arose from a
10328 : : constant of such a type, that will also have been diagnosed,
10329 : : but this is the only diagnostic in the case where it arises
10330 : : from choosing a wider type automatically when adding 1
10331 : : overflows.) */
10332 : 11 : warned_range = pedwarn (loc, OPT_Wpedantic,
10333 : : "enumerator value outside the range of %qs",
10334 : 11 : (TYPE_UNSIGNED (TREE_TYPE (value))
10335 : : ? "uintmax_t"
10336 : : : "intmax_t"));
10337 : 5562104 : if (!warned_range && !int_fits_type_p (value, integer_type_node))
10338 : 4662 : pedwarn_c11 (loc, OPT_Wpedantic,
10339 : : "ISO C restricts enumerator values to range of %<int%> "
10340 : : "before C23");
10341 : :
10342 : : /* The ISO C Standard mandates enumerators to have type int before
10343 : : C23, even though the underlying type of an enum type is
10344 : : unspecified. C23 allows enumerators of any integer type. During
10345 : : the parsing of the enumeration, C23 specifies that constants
10346 : : representable in int have type int, constants not representable
10347 : : in int have the type of the given expression if any, and
10348 : : constants not representable in int and derived by adding 1 to the
10349 : : previous constant have the type of that constant unless the
10350 : : addition would overflow or wraparound, in which case a wider type
10351 : : of the same signedness is chosen automatically; after the
10352 : : enumeration is parsed, all the constants have the type of the
10353 : : enumeration if any do not fit in int. */
10354 : 5562104 : if (int_fits_type_p (value, integer_type_node))
10355 : 5557438 : value = convert (integer_type_node, value);
10356 : : }
10357 : :
10358 : : /* Set basis for default for next value. */
10359 : 5562301 : if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10360 : : {
10361 : 163 : tree underlying_type = ENUM_UNDERLYING_TYPE (the_enum->enum_type);
10362 : 163 : if (TREE_CODE (underlying_type) == BOOLEAN_TYPE)
10363 : : /* A value of 2 following a value of 1 overflows bool, but we
10364 : : cannot carry out addition directly on bool without
10365 : : promotion, and converting the result of arithmetic in a
10366 : : wider type back to bool would not produce the right result
10367 : : for this overflow check. */
10368 : 61 : the_enum->enum_next_value = invert_truthvalue_loc (loc, value);
10369 : : else
10370 : 102 : the_enum->enum_next_value
10371 : 102 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10372 : : PLUS_EXPR, convert (underlying_type, value),
10373 : : convert (underlying_type, integer_one_node),
10374 : : false);
10375 : : }
10376 : : else
10377 : : {
10378 : : /* In a redeclaration the type can already be the enumeral type. */
10379 : 5562138 : if (TREE_CODE (TREE_TYPE (value)) == ENUMERAL_TYPE)
10380 : 12 : value = convert (ENUM_UNDERLYING_TYPE (TREE_TYPE (value)), value);
10381 : 5562138 : the_enum->enum_next_value
10382 : 5562138 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10383 : : PLUS_EXPR, value, integer_one_node, false);
10384 : : }
10385 : 5562301 : the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
10386 : 5562301 : if (the_enum->enum_overflow
10387 : 5562301 : && !ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10388 : : {
10389 : : /* Choose a wider type with the same signedness if
10390 : : available. */
10391 : 3291 : int prec = TYPE_PRECISION (TREE_TYPE (value)) + 1;
10392 : 3291 : bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (value));
10393 : 3291 : tree new_type = (unsignedp
10394 : 3291 : ? long_unsigned_type_node
10395 : : : long_integer_type_node);
10396 : 3291 : if (prec > TYPE_PRECISION (new_type))
10397 : 2922 : new_type = (unsignedp
10398 : 2922 : ? long_long_unsigned_type_node
10399 : : : long_long_integer_type_node);
10400 : 3291 : if (prec > TYPE_PRECISION (new_type))
10401 : 2460 : new_type = (unsignedp
10402 : 2460 : ? widest_unsigned_literal_type_node
10403 : : : widest_integer_literal_type_node);
10404 : 3291 : if (prec <= TYPE_PRECISION (new_type))
10405 : : {
10406 : 3286 : the_enum->enum_overflow = false;
10407 : 3286 : the_enum->enum_next_value
10408 : 3286 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10409 : : PLUS_EXPR, convert (new_type, value),
10410 : : integer_one_node, false);
10411 : 3286 : gcc_assert (!tree_int_cst_lt (the_enum->enum_next_value, value));
10412 : : }
10413 : : }
10414 : :
10415 : : /* Now create a declaration for the enum value name. */
10416 : :
10417 : 5562301 : decl = build_decl (decl_loc, CONST_DECL, name, TREE_TYPE (value));
10418 : 5562301 : DECL_INITIAL (decl) = value;
10419 : 5562301 : DECL_CONTEXT (decl) = the_enum->enum_type;
10420 : 5562301 : pushdecl (decl);
10421 : :
10422 : 5562301 : return tree_cons (decl, value, NULL_TREE);
10423 : : }
10424 : :
10425 : : /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
10426 : :
10427 : : tree
10428 : 0 : c_simulate_enum_decl (location_t loc, const char *name,
10429 : : vec<string_int_pair> *values_ptr)
10430 : : {
10431 : 0 : location_t saved_loc = input_location;
10432 : 0 : input_location = loc;
10433 : :
10434 : 0 : struct c_enum_contents the_enum;
10435 : 0 : tree enumtype = start_enum (loc, &the_enum, get_identifier (name),
10436 : : NULL_TREE, false);
10437 : :
10438 : 0 : tree value_chain = NULL_TREE;
10439 : 0 : string_int_pair *value;
10440 : 0 : vec<string_int_pair> values = *values_ptr;
10441 : 0 : unsigned int i;
10442 : 0 : FOR_EACH_VEC_ELT (values, i, value)
10443 : : {
10444 : 0 : tree decl = build_enumerator (loc, loc, &the_enum,
10445 : : get_identifier (value->first),
10446 : : build_int_cst (integer_type_node,
10447 : 0 : value->second));
10448 : 0 : TREE_CHAIN (decl) = value_chain;
10449 : 0 : value_chain = decl;
10450 : : }
10451 : :
10452 : 0 : finish_enum (enumtype, nreverse (value_chain), NULL_TREE);
10453 : :
10454 : 0 : input_location = saved_loc;
10455 : 0 : return enumtype;
10456 : : }
10457 : :
10458 : : /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
10459 : :
10460 : : tree
10461 : 0 : c_simulate_record_decl (location_t loc, const char *name,
10462 : : array_slice<const tree> fields)
10463 : : {
10464 : 0 : location_t saved_loc = input_location;
10465 : 0 : input_location = loc;
10466 : :
10467 : 0 : class c_struct_parse_info *struct_info;
10468 : 0 : tree ident = get_identifier (name);
10469 : 0 : tree type = start_struct (loc, RECORD_TYPE, ident, &struct_info);
10470 : :
10471 : 0 : for (unsigned int i = 0; i < fields.size (); ++i)
10472 : : {
10473 : 0 : DECL_FIELD_CONTEXT (fields[i]) = type;
10474 : 0 : if (i > 0)
10475 : 0 : DECL_CHAIN (fields[i - 1]) = fields[i];
10476 : : }
10477 : :
10478 : 0 : finish_struct (loc, type, fields[0], NULL_TREE, struct_info);
10479 : :
10480 : 0 : tree decl = build_decl (loc, TYPE_DECL, ident, type);
10481 : 0 : set_underlying_type (decl);
10482 : 0 : lang_hooks.decls.pushdecl (decl);
10483 : :
10484 : 0 : input_location = saved_loc;
10485 : 0 : return type;
10486 : : }
10487 : :
10488 : : /* Create the FUNCTION_DECL for a function definition.
10489 : : DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
10490 : : the declaration; they describe the function's name and the type it returns,
10491 : : but twisted together in a fashion that parallels the syntax of C.
10492 : :
10493 : : This function creates a binding context for the function body
10494 : : as well as setting up the FUNCTION_DECL in current_function_decl.
10495 : :
10496 : : Returns true on success. If the DECLARATOR is not suitable for a function
10497 : : (it defines a datum instead), we return false to report a parse error. */
10498 : :
10499 : : bool
10500 : 35382641 : start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
10501 : : tree attributes)
10502 : : {
10503 : 35382641 : tree decl1, old_decl;
10504 : 35382641 : tree restype, resdecl;
10505 : 35382641 : location_t loc;
10506 : 35382641 : location_t result_loc;
10507 : 35382641 : tree expr = NULL;
10508 : :
10509 : 35382641 : current_function_returns_value = 0; /* Assume, until we see it does. */
10510 : 35382641 : current_function_returns_null = 0;
10511 : 35382641 : current_function_returns_abnormally = 0;
10512 : 35382641 : warn_about_return_type = 0;
10513 : 35382641 : c_switch_stack = NULL;
10514 : :
10515 : : /* Indicate no valid break/continue context. */
10516 : 35382641 : in_statement = 0;
10517 : :
10518 : 35382641 : decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
10519 : : &attributes, &expr, NULL, DEPRECATED_NORMAL);
10520 : 35382641 : invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
10521 : :
10522 : : /* If the declarator is not suitable for a function definition,
10523 : : cause a syntax error. */
10524 : 35382641 : if (decl1 == NULL_TREE
10525 : 35382610 : || TREE_CODE (decl1) != FUNCTION_DECL)
10526 : : return false;
10527 : :
10528 : : /* Nested functions may have variably modified (return) type.
10529 : : Make sure the size expression is evaluated at this point. */
10530 : 35382603 : if (expr && !current_scope->parm_flag)
10531 : 9 : add_stmt (fold_convert (void_type_node, expr));
10532 : :
10533 : 35382603 : loc = DECL_SOURCE_LOCATION (decl1);
10534 : :
10535 : : /* A nested function is not global. */
10536 : 35382603 : if (current_function_decl != NULL_TREE)
10537 : 1530 : TREE_PUBLIC (decl1) = 0;
10538 : :
10539 : 35382603 : c_decl_attributes (&decl1, attributes, 0);
10540 : :
10541 : 35382603 : if (DECL_DECLARED_INLINE_P (decl1)
10542 : 34705128 : && DECL_UNINLINABLE (decl1)
10543 : 35382613 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
10544 : : {
10545 : 3 : auto_urlify_attributes sentinel;
10546 : 3 : warning_at (loc, OPT_Wattributes,
10547 : : "inline function %qD given attribute %qs",
10548 : : decl1, "noinline");
10549 : 3 : }
10550 : :
10551 : : /* Handle gnu_inline attribute. */
10552 : 35382603 : if (declspecs->inline_p
10553 : 34705131 : && !flag_gnu89_inline
10554 : 34671169 : && TREE_CODE (decl1) == FUNCTION_DECL
10555 : 70053772 : && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
10556 : 155506 : || current_function_decl))
10557 : : {
10558 : 34515735 : if (declspecs->storage_class != csc_static)
10559 : 34515729 : DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
10560 : : }
10561 : :
10562 : 35382603 : announce_function (decl1);
10563 : :
10564 : 35382603 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
10565 : : {
10566 : 1 : error_at (loc, "return type is an incomplete type");
10567 : : /* Make it return void instead. */
10568 : 1 : TREE_TYPE (decl1)
10569 : 2 : = c_build_function_type (void_type_node,
10570 : 1 : TYPE_ARG_TYPES (TREE_TYPE (decl1)),
10571 : 1 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl1)));
10572 : : }
10573 : :
10574 : 35382603 : if (warn_about_return_type)
10575 : 1404 : permerror_opt (loc, flag_isoc99 ? OPT_Wimplicit_int
10576 : 0 : : (warn_return_type > 0 ? OPT_Wreturn_type
10577 : : : OPT_Wimplicit_int),
10578 : : "return type defaults to %<int%>");
10579 : :
10580 : : /* Make the init_value nonzero so pushdecl knows this is not tentative.
10581 : : error_mark_node is replaced below (in pop_scope) with the BLOCK. */
10582 : 35382603 : DECL_INITIAL (decl1) = error_mark_node;
10583 : :
10584 : : /* If this definition isn't a prototype and we had a prototype declaration
10585 : : before, copy the arg type info from that prototype. */
10586 : 35382603 : old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
10587 : 35382603 : if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
10588 : 35156896 : old_decl = NULL_TREE;
10589 : :
10590 : 35382603 : current_function_prototype_locus = UNKNOWN_LOCATION;
10591 : 35382603 : current_function_prototype_built_in = false;
10592 : 35382603 : current_function_prototype_arg_types = NULL_TREE;
10593 : 35382603 : tree newtype = TREE_TYPE (decl1);
10594 : 35382603 : tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype;
10595 : 35382603 : if (!prototype_p (newtype))
10596 : : {
10597 : 13111 : tree oldrt = TREE_TYPE (oldtype);
10598 : 13111 : tree newrt = TREE_TYPE (newtype);
10599 : 13111 : if (old_decl != NULL_TREE
10600 : 315 : && TREE_CODE (oldtype) == FUNCTION_TYPE
10601 : 13426 : && comptypes (oldrt, newrt))
10602 : : {
10603 : 296 : if (stdarg_p (oldtype))
10604 : : {
10605 : 1 : auto_diagnostic_group d;
10606 : 1 : warning_at (loc, 0, "%q+D defined as variadic function "
10607 : : "without prototype", decl1);
10608 : 1 : locate_old_decl (old_decl);
10609 : 1 : }
10610 : 296 : TREE_TYPE (decl1) = composite_type (oldtype, newtype);
10611 : 296 : current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
10612 : 296 : current_function_prototype_built_in
10613 : 296 : = C_DECL_BUILTIN_PROTOTYPE (old_decl);
10614 : 296 : current_function_prototype_arg_types
10615 : 296 : = TYPE_ARG_TYPES (newtype);
10616 : : }
10617 : 13111 : if (TREE_PUBLIC (decl1))
10618 : : {
10619 : : /* If there is an external prototype declaration of this
10620 : : function, record its location but do not copy information
10621 : : to this decl. This may be an invisible declaration
10622 : : (built-in or in a scope which has finished) or simply
10623 : : have more refined argument types than any declaration
10624 : : found above. */
10625 : 12697 : struct c_binding *b;
10626 : 12972 : for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
10627 : 711 : if (B_IN_SCOPE (b, external_scope))
10628 : : break;
10629 : 12697 : if (b)
10630 : : {
10631 : 436 : tree ext_decl, ext_type;
10632 : 436 : ext_decl = b->decl;
10633 : 436 : ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
10634 : 436 : if (TREE_CODE (ext_type) == FUNCTION_TYPE
10635 : 872 : && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
10636 : 436 : TREE_TYPE (ext_type)))
10637 : : {
10638 : 416 : current_function_prototype_locus
10639 : 416 : = DECL_SOURCE_LOCATION (ext_decl);
10640 : 416 : current_function_prototype_built_in
10641 : 416 : = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
10642 : 416 : current_function_prototype_arg_types
10643 : 416 : = TYPE_ARG_TYPES (ext_type);
10644 : : }
10645 : : }
10646 : : }
10647 : : }
10648 : :
10649 : : /* Optionally warn about C23 compatibility. */
10650 : 35382603 : if (warn_deprecated_non_prototype
10651 : 34 : && old_decl != NULL_TREE
10652 : 5 : && TREE_CODE (oldtype) == FUNCTION_TYPE
10653 : 5 : && !TYPE_ARG_TYPES (oldtype)
10654 : 5 : && !TYPE_NO_NAMED_ARGS_STDARG_P (oldtype)
10655 : 35382608 : && (TYPE_ARG_TYPES (newtype)
10656 : 3 : && TREE_VALUE (TYPE_ARG_TYPES (newtype)) != void_type_node))
10657 : : {
10658 : 3 : bool warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
10659 : : "ISO C23 does not allow defining"
10660 : : " parameters for function %qE declared"
10661 : : " without parameters",
10662 : : decl1);
10663 : 3 : if (warned)
10664 : 3 : inform (DECL_SOURCE_LOCATION (old_decl), "declared here");
10665 : : }
10666 : :
10667 : : /* Optionally warn of old-fashioned def with no previous prototype. */
10668 : 35382603 : if (warn_strict_prototypes
10669 : 107308 : && old_decl != error_mark_node
10670 : 107308 : && !prototype_p (TREE_TYPE (decl1))
10671 : 35382604 : && C_DECL_ISNT_PROTOTYPE (old_decl))
10672 : 1 : warning_at (loc, OPT_Wstrict_prototypes,
10673 : : "function declaration isn%'t a prototype");
10674 : : /* Optionally warn of any global def with no previous prototype. */
10675 : 35382602 : else if (warn_missing_prototypes
10676 : 107009 : && old_decl != error_mark_node
10677 : 107009 : && TREE_PUBLIC (decl1)
10678 : 73916 : && !MAIN_NAME_P (DECL_NAME (decl1))
10679 : 73880 : && C_DECL_ISNT_PROTOTYPE (old_decl)
10680 : 35383243 : && !DECL_DECLARED_INLINE_P (decl1))
10681 : 4 : warning_at (loc, OPT_Wmissing_prototypes,
10682 : : "no previous prototype for %qD", decl1);
10683 : : /* Optionally warn of any def with no previous prototype
10684 : : if the function has already been used. */
10685 : 35382598 : else if (warn_missing_prototypes
10686 : 107005 : && old_decl != NULL_TREE
10687 : 74243 : && old_decl != error_mark_node
10688 : 74243 : && TREE_USED (old_decl)
10689 : 35389687 : && !prototype_p (TREE_TYPE (old_decl)))
10690 : 0 : warning_at (loc, OPT_Wmissing_prototypes,
10691 : : "%qD was used with no prototype before its definition", decl1);
10692 : : /* Optionally warn of any global def with no previous declaration. */
10693 : 35382598 : else if (warn_missing_declarations
10694 : 4 : && TREE_PUBLIC (decl1)
10695 : 3 : && old_decl == NULL_TREE
10696 : 3 : && !MAIN_NAME_P (DECL_NAME (decl1))
10697 : 35382601 : && !DECL_DECLARED_INLINE_P (decl1))
10698 : 2 : warning_at (loc, OPT_Wmissing_declarations,
10699 : : "no previous declaration for %qD",
10700 : : decl1);
10701 : : /* Optionally warn of any def with no previous declaration
10702 : : if the function has already been used. */
10703 : 35382596 : else if (warn_missing_declarations
10704 : 2 : && old_decl != NULL_TREE
10705 : 0 : && old_decl != error_mark_node
10706 : 0 : && TREE_USED (old_decl)
10707 : 35382596 : && C_DECL_IMPLICIT (old_decl))
10708 : 0 : warning_at (loc, OPT_Wmissing_declarations,
10709 : : "%qD was used with no declaration before its definition", decl1);
10710 : :
10711 : : /* This function exists in static storage.
10712 : : (This does not mean `static' in the C sense!) */
10713 : 35382603 : TREE_STATIC (decl1) = 1;
10714 : :
10715 : : /* This is the earliest point at which we might know the assembler
10716 : : name of the function. Thus, if it's set before this, die horribly. */
10717 : 35382603 : gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
10718 : :
10719 : : /* If #pragma weak was used, mark the decl weak now. */
10720 : 35382603 : if (current_scope == file_scope)
10721 : 35381073 : maybe_apply_pragma_weak (decl1);
10722 : :
10723 : : /* Warn for unlikely, improbable, or stupid declarations of `main'. */
10724 : 35382603 : if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
10725 : : {
10726 : 3190 : if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
10727 : 3190 : != integer_type_node)
10728 : 5 : pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
10729 : 3185 : else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
10730 : 1 : pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
10731 : : decl1);
10732 : :
10733 : 3190 : check_main_parameter_types (decl1);
10734 : :
10735 : 3190 : if (!TREE_PUBLIC (decl1))
10736 : 0 : pedwarn (loc, OPT_Wmain,
10737 : : "%qD is normally a non-static function", decl1);
10738 : : }
10739 : :
10740 : 35382603 : tree parms = current_function_arg_info->parms;
10741 : 35382603 : if (old_decl)
10742 : : {
10743 : 225707 : location_t origloc = DECL_SOURCE_LOCATION (old_decl);
10744 : 225707 : warn_parm_array_mismatch (origloc, old_decl, parms);
10745 : : }
10746 : :
10747 : : /* Record the decl so that the function name is defined.
10748 : : If we already have a decl for this name, and it is a FUNCTION_DECL,
10749 : : use the old decl. */
10750 : :
10751 : 35382603 : current_function_decl = pushdecl (decl1);
10752 : :
10753 : 35382603 : if (tree access = build_attr_access_from_parms (parms, false))
10754 : 56349 : decl_attributes (¤t_function_decl, access, ATTR_FLAG_INTERNAL,
10755 : : old_decl);
10756 : :
10757 : 35382603 : push_scope ();
10758 : 35382603 : declare_parm_level ();
10759 : :
10760 : : /* Set the result decl source location to the location of the typespec. */
10761 : 4004235 : result_loc = (declspecs->locations[cdw_typespec] == UNKNOWN_LOCATION
10762 : 35382603 : ? loc : declspecs->locations[cdw_typespec]);
10763 : 35382603 : restype = TREE_TYPE (TREE_TYPE (current_function_decl));
10764 : 35382603 : resdecl = build_decl (result_loc, RESULT_DECL, NULL_TREE, restype);
10765 : 35382603 : DECL_ARTIFICIAL (resdecl) = 1;
10766 : 35382603 : DECL_IGNORED_P (resdecl) = 1;
10767 : 35382603 : DECL_RESULT (current_function_decl) = resdecl;
10768 : :
10769 : 35382603 : start_fname_decls ();
10770 : :
10771 : 35382603 : return true;
10772 : : }
10773 : :
10774 : : /* Subroutine of store_parm_decls which handles new-style function
10775 : : definitions (prototype format). The parms already have decls, so we
10776 : : need only record them as in effect and complain if any redundant
10777 : : old-style parm decls were written. */
10778 : : static void
10779 : 35369505 : store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
10780 : : {
10781 : 35369505 : tree decl;
10782 : 35369505 : c_arg_tag *tag;
10783 : 35369505 : unsigned ix;
10784 : :
10785 : 35369505 : if (current_scope->bindings)
10786 : : {
10787 : 7 : error_at (DECL_SOURCE_LOCATION (fndecl),
10788 : : "old-style parameter declarations in prototyped "
10789 : : "function definition");
10790 : :
10791 : : /* Get rid of the old-style declarations. */
10792 : 7 : pop_scope ();
10793 : 7 : push_scope ();
10794 : : }
10795 : : /* Don't issue this warning for nested functions, and don't issue this
10796 : : warning if we got here because ARG_INFO_TYPES was error_mark_node
10797 : : (this happens when a function definition has just an ellipsis in
10798 : : its parameter list). */
10799 : 35369498 : else if (!in_system_header_at (input_location)
10800 : 627118 : && !current_function_scope
10801 : 625735 : && arg_info->types != error_mark_node
10802 : 35995233 : && !arg_info->c23_empty_parens)
10803 : 573813 : warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
10804 : : "traditional C rejects ISO C style function definitions");
10805 : :
10806 : : /* Now make all the parameter declarations visible in the function body.
10807 : : We can bypass most of the grunt work of pushdecl. */
10808 : 132847411 : for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
10809 : : {
10810 : 97477906 : DECL_CONTEXT (decl) = current_function_decl;
10811 : 97477906 : if (DECL_NAME (decl))
10812 : : {
10813 : 97477320 : bind (DECL_NAME (decl), decl, current_scope,
10814 : : /*invisible=*/false, /*nested=*/false,
10815 : : UNKNOWN_LOCATION);
10816 : 97477320 : if (!TREE_USED (decl))
10817 : 97460075 : warn_if_shadowing (decl);
10818 : : }
10819 : : else
10820 : 586 : pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_parameter_name,
10821 : : "ISO C does not support omitting parameter names in "
10822 : : "function definitions before C23");
10823 : : }
10824 : :
10825 : : /* Record the parameter list in the function declaration. */
10826 : 35369505 : DECL_ARGUMENTS (fndecl) = arg_info->parms;
10827 : :
10828 : : /* Now make all the ancillary declarations visible, likewise. */
10829 : 35369561 : for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
10830 : : {
10831 : 56 : DECL_CONTEXT (decl) = current_function_decl;
10832 : 56 : if (DECL_NAME (decl))
10833 : 0 : bind (DECL_NAME (decl), decl, current_scope,
10834 : : /*invisible=*/false,
10835 : 0 : /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
10836 : : UNKNOWN_LOCATION);
10837 : : }
10838 : :
10839 : : /* And all the tag declarations. */
10840 : 35369615 : FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
10841 : 56 : if (tag->id)
10842 : 23 : bind (tag->id, tag->type, current_scope,
10843 : : /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
10844 : 35369505 : }
10845 : :
10846 : : /* Subroutine of store_parm_decls which handles old-style function
10847 : : definitions (separate parameter list and declarations). */
10848 : :
10849 : : static void
10850 : 13098 : store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
10851 : : {
10852 : 13098 : struct c_binding *b;
10853 : 13098 : tree parm, decl, last;
10854 : 13098 : tree parmids = arg_info->parms;
10855 : 13098 : hash_set<tree> seen_args;
10856 : :
10857 : 13098 : if (!in_system_header_at (input_location))
10858 : : {
10859 : 13095 : if (flag_isoc23)
10860 : 1349 : pedwarn (DECL_SOURCE_LOCATION (fndecl),
10861 : 1349 : OPT_Wold_style_definition, "old-style function definition");
10862 : : else
10863 : 11746 : warning_at (DECL_SOURCE_LOCATION (fndecl),
10864 : 11746 : OPT_Wold_style_definition,
10865 : : "old-style function definition");
10866 : : }
10867 : :
10868 : 13098 : if (current_scope->had_vla_unspec)
10869 : 1 : error ("%<[*]%> not allowed in other than function prototype scope");
10870 : :
10871 : : /* Match each formal parameter name with its declaration. Save each
10872 : : decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
10873 : 47367 : for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10874 : : {
10875 : 34269 : if (TREE_VALUE (parm) == NULL_TREE)
10876 : : {
10877 : 0 : error_at (DECL_SOURCE_LOCATION (fndecl),
10878 : : "parameter name missing from parameter list");
10879 : 0 : TREE_PURPOSE (parm) = NULL_TREE;
10880 : 0 : continue;
10881 : : }
10882 : :
10883 : 34269 : b = I_SYMBOL_BINDING (TREE_VALUE (parm));
10884 : 34269 : if (b && B_IN_CURRENT_SCOPE (b))
10885 : : {
10886 : 22649 : decl = b->decl;
10887 : : /* Skip erroneous parameters. */
10888 : 22649 : if (decl == error_mark_node)
10889 : 2 : continue;
10890 : : /* If we got something other than a PARM_DECL it is an error. */
10891 : 22647 : if (TREE_CODE (decl) != PARM_DECL)
10892 : : {
10893 : 7 : error_at (DECL_SOURCE_LOCATION (decl),
10894 : : "%qD declared as a non-parameter", decl);
10895 : 7 : continue;
10896 : : }
10897 : : /* If the declaration is already marked, we have a duplicate
10898 : : name. Complain and ignore the duplicate. */
10899 : 22640 : else if (seen_args.contains (decl))
10900 : : {
10901 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
10902 : : "multiple parameters named %qD", decl);
10903 : 0 : TREE_PURPOSE (parm) = NULL_TREE;
10904 : 0 : continue;
10905 : : }
10906 : : /* If the declaration says "void", complain and turn it into
10907 : : an int. */
10908 : 22640 : else if (VOID_TYPE_P (TREE_TYPE (decl)))
10909 : : {
10910 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
10911 : : "parameter %qD declared with void type", decl);
10912 : 0 : TREE_TYPE (decl) = integer_type_node;
10913 : 0 : DECL_ARG_TYPE (decl) = integer_type_node;
10914 : 0 : layout_decl (decl, 0);
10915 : : }
10916 : 22640 : warn_if_shadowing (decl);
10917 : : }
10918 : : /* If no declaration found, default to int. */
10919 : : else
10920 : : {
10921 : : /* FIXME diagnostics: This should be the location of the argument,
10922 : : not the FNDECL. E.g., for an old-style declaration
10923 : :
10924 : : int f10(v) { blah; }
10925 : :
10926 : : We should use the location of the V, not the F10.
10927 : : Unfortunately, the V is an IDENTIFIER_NODE which has no
10928 : : location. In the future we need locations for c_arg_info
10929 : : entries.
10930 : :
10931 : : See gcc.dg/Wshadow-3.c for an example of this problem. */
10932 : 11620 : decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
10933 : 11620 : PARM_DECL, TREE_VALUE (parm), integer_type_node);
10934 : 11620 : DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
10935 : 11620 : pushdecl (decl);
10936 : 11620 : warn_if_shadowing (decl);
10937 : :
10938 : 11620 : if (flag_isoc99)
10939 : 118 : permerror_opt (DECL_SOURCE_LOCATION (decl),
10940 : 118 : OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
10941 : : decl);
10942 : : else
10943 : 11502 : warning_at (DECL_SOURCE_LOCATION (decl),
10944 : 11502 : OPT_Wmissing_parameter_type,
10945 : : "type of %qD defaults to %<int%>", decl);
10946 : : }
10947 : :
10948 : 34260 : TREE_PURPOSE (parm) = decl;
10949 : 34260 : seen_args.add (decl);
10950 : : }
10951 : :
10952 : : /* Now examine the parms chain for incomplete declarations
10953 : : and declarations with no corresponding names. */
10954 : :
10955 : 47446 : for (b = current_scope->bindings; b; b = b->prev)
10956 : : {
10957 : 34348 : parm = b->decl;
10958 : 34348 : if (TREE_CODE (parm) != PARM_DECL)
10959 : 79 : continue;
10960 : :
10961 : 34269 : if (TREE_TYPE (parm) != error_mark_node
10962 : 34269 : && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
10963 : : {
10964 : 0 : error_at (DECL_SOURCE_LOCATION (parm),
10965 : : "parameter %qD has incomplete type", parm);
10966 : 0 : TREE_TYPE (parm) = error_mark_node;
10967 : : }
10968 : :
10969 : 34269 : if (!seen_args.contains (parm))
10970 : : {
10971 : 9 : error_at (DECL_SOURCE_LOCATION (parm),
10972 : : "declaration for parameter %qD but no such parameter",
10973 : : parm);
10974 : :
10975 : : /* Pretend the parameter was not missing.
10976 : : This gets us to a standard state and minimizes
10977 : : further error messages. */
10978 : 9 : parmids = chainon (parmids, tree_cons (parm, 0, 0));
10979 : : }
10980 : : }
10981 : :
10982 : : /* Chain the declarations together in the order of the list of
10983 : : names. Store that chain in the function decl, replacing the
10984 : : list of names. Update the current scope to match. */
10985 : 13098 : DECL_ARGUMENTS (fndecl) = NULL_TREE;
10986 : :
10987 : 13106 : for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10988 : 8680 : if (TREE_PURPOSE (parm))
10989 : : break;
10990 : 13098 : if (parm && TREE_PURPOSE (parm))
10991 : : {
10992 : 8672 : last = TREE_PURPOSE (parm);
10993 : 8672 : DECL_ARGUMENTS (fndecl) = last;
10994 : :
10995 : 34270 : for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
10996 : 25598 : if (TREE_PURPOSE (parm))
10997 : : {
10998 : 25597 : DECL_CHAIN (last) = TREE_PURPOSE (parm);
10999 : 25598 : last = TREE_PURPOSE (parm);
11000 : : }
11001 : 8672 : DECL_CHAIN (last) = NULL_TREE;
11002 : : }
11003 : :
11004 : : /* If there was a previous prototype,
11005 : : set the DECL_ARG_TYPE of each argument according to
11006 : : the type previously specified, and report any mismatches. */
11007 : :
11008 : 13098 : if (current_function_prototype_arg_types)
11009 : : {
11010 : 171 : tree type;
11011 : 171 : for (parm = DECL_ARGUMENTS (fndecl),
11012 : 171 : type = current_function_prototype_arg_types;
11013 : 373 : parm || (type != NULL_TREE
11014 : 166 : && TREE_VALUE (type) != error_mark_node
11015 : 166 : && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
11016 : 202 : parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
11017 : : {
11018 : 212 : if (parm == NULL_TREE
11019 : 206 : || type == NULL_TREE
11020 : 418 : || (TREE_VALUE (type) != error_mark_node
11021 : 192 : && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
11022 : : {
11023 : 10 : if (current_function_prototype_built_in)
11024 : 6 : warning_at (DECL_SOURCE_LOCATION (fndecl),
11025 : 6 : 0, "number of arguments doesn%'t match "
11026 : : "built-in prototype");
11027 : : else
11028 : : {
11029 : : /* FIXME diagnostics: This should be the location of
11030 : : FNDECL, but there is bug when a prototype is
11031 : : declared inside function context, but defined
11032 : : outside of it (e.g., gcc.dg/pr15698-2.c). In
11033 : : which case FNDECL gets the location of the
11034 : : prototype, not the definition. */
11035 : 4 : error_at (input_location,
11036 : : "number of arguments doesn%'t match prototype");
11037 : :
11038 : 4 : error_at (current_function_prototype_locus,
11039 : : "prototype declaration");
11040 : : }
11041 : : break;
11042 : : }
11043 : : /* Type for passing arg must be consistent with that
11044 : : declared for the arg. ISO C says we take the unqualified
11045 : : type for parameters declared with qualified type. */
11046 : 202 : if (TREE_TYPE (parm) != error_mark_node
11047 : 201 : && TREE_VALUE (type) != error_mark_node
11048 : 389 : && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
11049 : 187 : != TYPE_ATOMIC (TREE_VALUE (type)))
11050 : 185 : || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
11051 : 185 : TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
11052 : : {
11053 : 36 : if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
11054 : 36 : == TYPE_ATOMIC (TREE_VALUE (type)))
11055 : 36 : && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
11056 : 34 : == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
11057 : : {
11058 : : /* Adjust argument to match prototype. E.g. a previous
11059 : : `int foo(float);' prototype causes
11060 : : `int foo(x) float x; {...}' to be treated like
11061 : : `int foo(float x) {...}'. This is particularly
11062 : : useful for argument types like uid_t. */
11063 : 17 : DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
11064 : :
11065 : : /* ??? Is it possible to get here with a
11066 : : built-in prototype or will it always have
11067 : : been diagnosed as conflicting with an
11068 : : old-style definition and discarded? */
11069 : 17 : if (current_function_prototype_built_in)
11070 : 2 : warning_at (DECL_SOURCE_LOCATION (parm),
11071 : 2 : OPT_Wpedantic, "promoted argument %qD "
11072 : : "doesn%'t match built-in prototype", parm);
11073 : : else
11074 : : {
11075 : 15 : pedwarn (DECL_SOURCE_LOCATION (parm),
11076 : 15 : OPT_Wpedantic, "promoted argument %qD "
11077 : : "doesn%'t match prototype", parm);
11078 : 15 : pedwarn (current_function_prototype_locus, OPT_Wpedantic,
11079 : : "prototype declaration");
11080 : : }
11081 : : }
11082 : : else
11083 : : {
11084 : 19 : if (current_function_prototype_built_in)
11085 : 10 : warning_at (DECL_SOURCE_LOCATION (parm),
11086 : 10 : 0, "argument %qD doesn%'t match "
11087 : : "built-in prototype", parm);
11088 : : else
11089 : : {
11090 : 9 : error_at (DECL_SOURCE_LOCATION (parm),
11091 : : "argument %qD doesn%'t match prototype", parm);
11092 : 9 : error_at (current_function_prototype_locus,
11093 : : "prototype declaration");
11094 : : }
11095 : : }
11096 : : }
11097 : : }
11098 : 171 : TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
11099 : : }
11100 : :
11101 : : /* Otherwise, create a prototype that would match. */
11102 : :
11103 : : else
11104 : : {
11105 : 12927 : tree actual = NULL_TREE, last = NULL_TREE, type;
11106 : :
11107 : 46990 : for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
11108 : : {
11109 : 34063 : type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
11110 : 34063 : if (last)
11111 : 25547 : TREE_CHAIN (last) = type;
11112 : : else
11113 : : actual = type;
11114 : 34063 : last = type;
11115 : : }
11116 : 12927 : type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
11117 : 12927 : if (last)
11118 : 8516 : TREE_CHAIN (last) = type;
11119 : : else
11120 : : actual = type;
11121 : :
11122 : : /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
11123 : : of the type of this function, but we need to avoid having this
11124 : : affect the types of other similarly-typed functions, so we must
11125 : : first force the generation of an identical (but separate) type
11126 : : node for the relevant function type. The new node we create
11127 : : will be a variant of the main variant of the original function
11128 : : type. */
11129 : :
11130 : 12927 : TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
11131 : :
11132 : 12927 : TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
11133 : : }
11134 : 13098 : }
11135 : :
11136 : : /* Store parameter declarations passed in ARG_INFO into the current
11137 : : function declaration. */
11138 : :
11139 : : void
11140 : 0 : store_parm_decls_from (struct c_arg_info *arg_info)
11141 : : {
11142 : 0 : current_function_arg_info = arg_info;
11143 : 0 : store_parm_decls ();
11144 : 0 : }
11145 : :
11146 : : /* Called by walk_tree to look for and update context-less labels
11147 : : or labels with context in the parent function. */
11148 : :
11149 : : static tree
11150 : 8127 : set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
11151 : : {
11152 : 8127 : tree ctx = static_cast<tree>(data);
11153 : 8127 : if (TREE_CODE (*tp) == LABEL_EXPR
11154 : 8127 : && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
11155 : 2 : || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
11156 : : {
11157 : 72 : DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
11158 : 72 : *walk_subtrees = 0;
11159 : : }
11160 : :
11161 : 8127 : return NULL_TREE;
11162 : : }
11163 : :
11164 : : /* Store the parameter declarations into the current function declaration.
11165 : : This is called after parsing the parameter declarations, before
11166 : : digesting the body of the function.
11167 : :
11168 : : For an old-style definition, construct a prototype out of the old-style
11169 : : parameter declarations and inject it into the function's type. */
11170 : :
11171 : : void
11172 : 35382603 : store_parm_decls (void)
11173 : : {
11174 : 35382603 : tree fndecl = current_function_decl;
11175 : 35382603 : bool proto;
11176 : :
11177 : : /* The argument information block for FNDECL. */
11178 : 35382603 : struct c_arg_info *arg_info = current_function_arg_info;
11179 : 35382603 : current_function_arg_info = 0;
11180 : :
11181 : : /* True if this definition is written with a prototype. In C23, an
11182 : : empty argument list was converted to (void) in grokparms; in
11183 : : older C standard versions, it does not give the function a type
11184 : : with a prototype for future calls. */
11185 : 35382603 : proto = arg_info->types != 0 || arg_info->no_named_args_stdarg_p;
11186 : :
11187 : 13098 : if (proto)
11188 : 35369505 : store_parm_decls_newstyle (fndecl, arg_info);
11189 : : else
11190 : 13098 : store_parm_decls_oldstyle (fndecl, arg_info);
11191 : :
11192 : : /* The next call to push_scope will be a function body. */
11193 : :
11194 : 35382603 : next_is_function_body = true;
11195 : :
11196 : : /* Write a record describing this function definition to the prototypes
11197 : : file (if requested). */
11198 : :
11199 : 35382603 : gen_aux_info_record (fndecl, 1, 0, proto);
11200 : :
11201 : : /* Initialize the RTL code for the function. */
11202 : 35382603 : allocate_struct_function (fndecl, false);
11203 : :
11204 : 35382603 : if (warn_unused_local_typedefs)
11205 : 3108762 : cfun->language = ggc_cleared_alloc<language_function> ();
11206 : :
11207 : : /* Begin the statement tree for this function. */
11208 : 35382603 : DECL_SAVED_TREE (fndecl) = push_stmt_list ();
11209 : :
11210 : : /* ??? Insert the contents of the pending sizes list into the function
11211 : : to be evaluated. The only reason left to have this is
11212 : : void foo(int n, int array[n++])
11213 : : because we throw away the array type in favor of a pointer type, and
11214 : : thus won't naturally see the SAVE_EXPR containing the increment. All
11215 : : other pending sizes would be handled by gimplify_parameters. */
11216 : 35382603 : if (arg_info->pending_sizes)
11217 : : {
11218 : : /* In very special circumstances, e.g. for code like
11219 : : _Atomic int i = 5;
11220 : : void f (int a[i += 2]) {}
11221 : : we need to execute the atomic assignment on function entry.
11222 : : But in this case, it is not just a straight store, it has the
11223 : : op= form, which means that build_atomic_assign has generated
11224 : : gotos, labels, etc. Because at that time the function decl
11225 : : for F has not been created yet, those labels do not have any
11226 : : function context. But we have the fndecl now, so update the
11227 : : labels accordingly. gimplify_expr would crash otherwise.
11228 : : Or with nested functions the labels could be created with parent
11229 : : function's context, while when the statement is emitted at the
11230 : : start of the nested function, it needs the nested function's
11231 : : context. */
11232 : 295 : walk_tree_without_duplicates (&arg_info->pending_sizes,
11233 : : set_labels_context_r, fndecl);
11234 : 295 : add_stmt (arg_info->pending_sizes);
11235 : : }
11236 : 35382603 : }
11237 : :
11238 : : /* Store PARM_DECLs in PARMS into scope temporarily. Used for
11239 : : c_finish_omp_declare_simd for function prototypes. No diagnostics
11240 : : should be done. */
11241 : :
11242 : : void
11243 : 240 : temp_store_parm_decls (tree fndecl, tree parms)
11244 : : {
11245 : 240 : push_scope ();
11246 : 678 : for (tree p = parms; p; p = DECL_CHAIN (p))
11247 : : {
11248 : 438 : DECL_CONTEXT (p) = fndecl;
11249 : 438 : if (DECL_NAME (p))
11250 : 319 : bind (DECL_NAME (p), p, current_scope,
11251 : : /*invisible=*/false, /*nested=*/false,
11252 : : UNKNOWN_LOCATION);
11253 : : }
11254 : 240 : }
11255 : :
11256 : : /* Undo what temp_store_parm_decls did. */
11257 : :
11258 : : void
11259 : 240 : temp_pop_parm_decls (void)
11260 : : {
11261 : : /* Clear all bindings in this temporary scope, so that
11262 : : pop_scope doesn't create a BLOCK. */
11263 : 240 : struct c_binding *b = current_scope->bindings;
11264 : 240 : current_scope->bindings = NULL;
11265 : 564 : for (; b; b = free_binding_and_advance (b))
11266 : : {
11267 : 324 : gcc_assert (TREE_CODE (b->decl) == PARM_DECL
11268 : : || b->decl == error_mark_node);
11269 : 324 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
11270 : 324 : I_SYMBOL_BINDING (b->id) = b->shadowed;
11271 : 324 : if (b->shadowed && b->shadowed->u.type)
11272 : 0 : TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
11273 : : }
11274 : 240 : pop_scope ();
11275 : 240 : }
11276 : :
11277 : :
11278 : : /* Finish up a function declaration and compile that function
11279 : : all the way to assembler language output. Then free the storage
11280 : : for the function definition.
11281 : :
11282 : : This is called after parsing the body of the function definition. */
11283 : :
11284 : : void
11285 : 35382601 : finish_function (location_t end_loc)
11286 : : {
11287 : 35382601 : tree fndecl = current_function_decl;
11288 : :
11289 : 35382601 : if (c_dialect_objc ())
11290 : 0 : objc_finish_function ();
11291 : :
11292 : 35382601 : if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
11293 : 35382599 : BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
11294 : :
11295 : : /* Must mark the RESULT_DECL as being in this function. */
11296 : :
11297 : 35382601 : if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
11298 : 35382601 : DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
11299 : :
11300 : 35431085 : if (MAIN_NAME_P (DECL_NAME (fndecl)) && !TREE_THIS_VOLATILE (fndecl)
11301 : 48483 : && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
11302 : 35431084 : == integer_type_node && flag_isoc99)
11303 : : {
11304 : : /* Hack. We don't want the middle-end to warn that this return
11305 : : is unreachable, so we mark its location as special. Using
11306 : : UNKNOWN_LOCATION has the problem that it gets clobbered in
11307 : : annotate_one_with_locus. A cleaner solution might be to
11308 : : ensure ! should_carry_locus_p (stmt), but that needs a flag.
11309 : : */
11310 : 47123 : c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
11311 : : }
11312 : :
11313 : : /* Tie off the statement tree for this function. */
11314 : 35382601 : DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
11315 : :
11316 : 35382601 : finish_fname_decls ();
11317 : :
11318 : : /* Complain if there's no return statement only if option specified on
11319 : : command line. */
11320 : 35382601 : if (warn_return_type > 0
11321 : 3108785 : && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
11322 : 2913204 : && !current_function_returns_value && !current_function_returns_null
11323 : : /* Don't complain if we are no-return. */
11324 : 72 : && !current_function_returns_abnormally
11325 : : /* Don't complain if we are declared noreturn. */
11326 : 21 : && !TREE_THIS_VOLATILE (fndecl)
11327 : : /* Don't warn for main(). */
11328 : 18 : && !MAIN_NAME_P (DECL_NAME (fndecl))
11329 : : /* Or if they didn't actually specify a return type. */
11330 : 17 : && !C_FUNCTION_IMPLICIT_INT (fndecl)
11331 : : /* Normally, with -Wreturn-type, flow will complain, but we might
11332 : : optimize out static functions. */
11333 : 17 : && !TREE_PUBLIC (fndecl)
11334 : 6 : && targetm.warn_func_return (fndecl)
11335 : 38491386 : && warning (OPT_Wreturn_type,
11336 : : "no return statement in function returning non-void"))
11337 : 5 : suppress_warning (fndecl, OPT_Wreturn_type);
11338 : :
11339 : : /* Complain about parameters that are only set, but never otherwise used. */
11340 : 35382601 : if (warn_unused_but_set_parameter)
11341 : : {
11342 : 3044926 : tree decl;
11343 : :
11344 : 3044926 : for (decl = DECL_ARGUMENTS (fndecl);
11345 : 11380568 : decl;
11346 : 8335642 : decl = DECL_CHAIN (decl))
11347 : 8335642 : if (TREE_USED (decl)
11348 : 8274939 : && TREE_CODE (decl) == PARM_DECL
11349 : 8274939 : && !DECL_READ_P (decl)
11350 : 47 : && DECL_NAME (decl)
11351 : 47 : && !DECL_ARTIFICIAL (decl)
11352 : 8335689 : && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter_))
11353 : 47 : warning_at (DECL_SOURCE_LOCATION (decl),
11354 : 47 : OPT_Wunused_but_set_parameter_,
11355 : : "parameter %qD set but not used", decl);
11356 : : }
11357 : :
11358 : : /* Complain about locally defined typedefs that are not used in this
11359 : : function. */
11360 : 35382601 : maybe_warn_unused_local_typedefs ();
11361 : :
11362 : : /* Possibly warn about unused parameters. */
11363 : 35382601 : if (warn_unused_parameter)
11364 : 2951037 : do_warn_unused_parameter (fndecl);
11365 : :
11366 : : /* Store the end of the function, so that we get good line number
11367 : : info for the epilogue. */
11368 : 35382601 : cfun->function_end_locus = end_loc;
11369 : :
11370 : : /* Finalize the ELF visibility for the function. */
11371 : 35382601 : c_determine_visibility (fndecl);
11372 : :
11373 : : /* For GNU C extern inline functions disregard inline limits. */
11374 : 35382601 : if (DECL_EXTERNAL (fndecl)
11375 : 34544250 : && DECL_DECLARED_INLINE_P (fndecl)
11376 : 69926848 : && (flag_gnu89_inline
11377 : 34513308 : || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
11378 : 34543778 : DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
11379 : :
11380 : : /* Genericize before inlining. Delay genericizing nested functions
11381 : : until their parent function is genericized. Since finalizing
11382 : : requires GENERIC, delay that as well. */
11383 : :
11384 : 70765202 : if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
11385 : 70765200 : && !undef_nested_function)
11386 : : {
11387 : 35382594 : if (!decl_function_context (fndecl))
11388 : : {
11389 : 35381064 : invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
11390 : 35381064 : c_genericize (fndecl);
11391 : :
11392 : : /* ??? Objc emits functions after finalizing the compilation unit.
11393 : : This should be cleaned up later and this conditional removed. */
11394 : 35381064 : if (symtab->global_info_ready)
11395 : : {
11396 : 0 : cgraph_node::add_new_function (fndecl, false);
11397 : 0 : return;
11398 : : }
11399 : 35381064 : cgraph_node::finalize_function (fndecl, false);
11400 : : }
11401 : : else
11402 : : {
11403 : : /* Register this function with cgraph just far enough to get it
11404 : : added to our parent's nested function list. Handy, since the
11405 : : C front end doesn't have such a list. */
11406 : 1530 : (void) cgraph_node::get_create (fndecl);
11407 : : }
11408 : : }
11409 : :
11410 : 35382601 : if (!decl_function_context (fndecl))
11411 : 35381071 : undef_nested_function = false;
11412 : :
11413 : 35382601 : if (cfun->language != NULL)
11414 : : {
11415 : 3108761 : ggc_free (cfun->language);
11416 : 3108761 : cfun->language = NULL;
11417 : : }
11418 : :
11419 : : /* We're leaving the context of this function, so zap cfun.
11420 : : It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
11421 : : tree_rest_of_compilation. */
11422 : 35382601 : set_cfun (NULL);
11423 : 35382601 : invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
11424 : 35382601 : current_function_decl = NULL;
11425 : : }
11426 : :
11427 : : /* Check the declarations given in a for-loop for satisfying the C99
11428 : : constraints. If exactly one such decl is found, return it. LOC is
11429 : : the location of the opening parenthesis of the for loop. The last
11430 : : parameter allows you to control the "for loop initial declarations
11431 : : are only allowed in C99 mode". Normally, you should pass
11432 : : flag_isoc99 as that parameter. But in some cases (Objective-C
11433 : : foreach loop, for example) we want to run the checks in this
11434 : : function even if not in C99 mode, so we allow the caller to turn
11435 : : off the error about not being in C99 mode.
11436 : : */
11437 : :
11438 : : tree
11439 : 24672 : check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
11440 : : {
11441 : 24672 : struct c_binding *b;
11442 : 24672 : tree one_decl = NULL_TREE;
11443 : 24672 : int n_decls = 0;
11444 : :
11445 : 24672 : if (!turn_off_iso_c99_error)
11446 : : {
11447 : 1 : static bool hint = true;
11448 : : /* If we get here, declarations have been used in a for loop without
11449 : : the C99 for loop scope. This doesn't make much sense, so don't
11450 : : allow it. */
11451 : 1 : auto_diagnostic_group d;
11452 : 1 : error_at (loc, "%<for%> loop initial declarations "
11453 : : "are only allowed in C99 or C11 mode");
11454 : 1 : if (hint)
11455 : : {
11456 : 1 : inform (loc,
11457 : : "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
11458 : : "%<-std=gnu11%> to compile your code");
11459 : 1 : hint = false;
11460 : : }
11461 : 1 : return NULL_TREE;
11462 : 1 : }
11463 : : else
11464 : 24671 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
11465 : : "initial declarations");
11466 : :
11467 : : /* C99 subclause 6.8.5 paragraph 3:
11468 : :
11469 : : [#3] The declaration part of a for statement shall only
11470 : : declare identifiers for objects having storage class auto or
11471 : : register.
11472 : :
11473 : : It isn't clear whether, in this sentence, "identifiers" binds to
11474 : : "shall only declare" or to "objects" - that is, whether all identifiers
11475 : : declared must be identifiers for objects, or whether the restriction
11476 : : only applies to those that are. (A question on this in comp.std.c
11477 : : in November 2000 received no answer.) We implement the strictest
11478 : : interpretation, to avoid creating an extension which later causes
11479 : : problems.
11480 : :
11481 : : This constraint was removed in C23. */
11482 : :
11483 : 49422 : for (b = current_scope->bindings; b; b = b->prev)
11484 : : {
11485 : 24751 : tree id = b->id;
11486 : 24751 : tree decl = b->decl;
11487 : :
11488 : 24751 : if (!id)
11489 : 27 : continue;
11490 : :
11491 : 24724 : switch (TREE_CODE (decl))
11492 : : {
11493 : 24676 : case VAR_DECL:
11494 : 24676 : {
11495 : 24676 : location_t decl_loc = DECL_SOURCE_LOCATION (decl);
11496 : 24676 : if (TREE_STATIC (decl))
11497 : 5 : pedwarn_c11 (decl_loc, OPT_Wpedantic,
11498 : : "declaration of static variable %qD in %<for%> "
11499 : : "loop initial declaration", decl);
11500 : 24671 : else if (DECL_EXTERNAL (decl))
11501 : 5 : pedwarn_c11 (decl_loc, OPT_Wpedantic,
11502 : : "declaration of %<extern%> variable %qD in %<for%> "
11503 : : "loop initial declaration", decl);
11504 : : }
11505 : : break;
11506 : :
11507 : 6 : case RECORD_TYPE:
11508 : 6 : pedwarn_c11 (loc, OPT_Wpedantic,
11509 : : "%<struct %E%> declared in %<for%> loop initial "
11510 : : "declaration", id);
11511 : 6 : break;
11512 : 5 : case UNION_TYPE:
11513 : 5 : pedwarn_c11 (loc, OPT_Wpedantic,
11514 : : "%<union %E%> declared in %<for%> loop initial "
11515 : : "declaration",
11516 : : id);
11517 : 5 : break;
11518 : 5 : case ENUMERAL_TYPE:
11519 : 5 : pedwarn_c11 (loc, OPT_Wpedantic,
11520 : : "%<enum %E%> declared in %<for%> loop "
11521 : : "initial declaration", id);
11522 : 5 : break;
11523 : 32 : default:
11524 : 32 : pedwarn_c11 (loc, OPT_Wpedantic, "declaration of non-variable "
11525 : : "%qD in %<for%> loop initial declaration", decl);
11526 : : }
11527 : :
11528 : 24724 : n_decls++;
11529 : 24724 : one_decl = decl;
11530 : : }
11531 : :
11532 : 24671 : return n_decls == 1 ? one_decl : NULL_TREE;
11533 : : }
11534 : :
11535 : : /* Save and reinitialize the variables
11536 : : used during compilation of a C function. */
11537 : :
11538 : : void
11539 : 1558 : c_push_function_context (void)
11540 : : {
11541 : 1558 : struct language_function *p = cfun->language;
11542 : : /* cfun->language might have been already allocated by the use of
11543 : : -Wunused-local-typedefs. In that case, just re-use it. */
11544 : 1558 : if (p == NULL)
11545 : 1502 : cfun->language = p = ggc_cleared_alloc<language_function> ();
11546 : :
11547 : 1558 : p->base.x_stmt_tree = c_stmt_tree;
11548 : 1558 : c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
11549 : 1558 : p->x_in_statement = in_statement;
11550 : 1558 : p->x_switch_stack = c_switch_stack;
11551 : 1558 : p->loop_names = loop_names;
11552 : 1558 : loop_names = vNULL;
11553 : 1558 : p->loop_names_hash = loop_names_hash;
11554 : 1558 : loop_names_hash = NULL;
11555 : 1558 : p->arg_info = current_function_arg_info;
11556 : 1558 : p->returns_value = current_function_returns_value;
11557 : 1558 : p->returns_null = current_function_returns_null;
11558 : 1558 : p->returns_abnormally = current_function_returns_abnormally;
11559 : 1558 : p->warn_about_return_type = warn_about_return_type;
11560 : :
11561 : 1558 : push_function_context ();
11562 : 1558 : }
11563 : :
11564 : : /* Restore the variables used during compilation of a C function. */
11565 : :
11566 : : void
11567 : 1558 : c_pop_function_context (void)
11568 : : {
11569 : 1558 : struct language_function *p;
11570 : :
11571 : 1558 : pop_function_context ();
11572 : 1558 : p = cfun->language;
11573 : :
11574 : : /* When -Wunused-local-typedefs is in effect, cfun->languages is
11575 : : used to store data throughout the life time of the current cfun,
11576 : : So don't deallocate it. */
11577 : 1558 : if (!warn_unused_local_typedefs)
11578 : 1502 : cfun->language = NULL;
11579 : :
11580 : 1558 : if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
11581 : 1558 : && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
11582 : : {
11583 : : /* Stop pointing to the local nodes about to be freed. */
11584 : : /* But DECL_INITIAL must remain nonzero so we know this
11585 : : was an actual function definition. */
11586 : 0 : DECL_INITIAL (current_function_decl) = error_mark_node;
11587 : 0 : DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
11588 : : }
11589 : :
11590 : 1558 : c_stmt_tree = p->base.x_stmt_tree;
11591 : 1558 : p->base.x_stmt_tree.x_cur_stmt_list = NULL;
11592 : 1558 : in_statement = p->x_in_statement;
11593 : 1558 : c_switch_stack = p->x_switch_stack;
11594 : 1558 : loop_names.release ();
11595 : 1558 : loop_names = p->loop_names;
11596 : 1558 : p->loop_names = vNULL;
11597 : 1558 : delete loop_names_hash;
11598 : 1558 : loop_names_hash = p->loop_names_hash;
11599 : 1558 : p->loop_names_hash = NULL;
11600 : 1558 : current_function_arg_info = p->arg_info;
11601 : 1558 : current_function_returns_value = p->returns_value;
11602 : 1558 : current_function_returns_null = p->returns_null;
11603 : 1558 : current_function_returns_abnormally = p->returns_abnormally;
11604 : 1558 : warn_about_return_type = p->warn_about_return_type;
11605 : 1558 : }
11606 : :
11607 : : /* The functions below are required for functionality of doing
11608 : : function at once processing in the C front end. Currently these
11609 : : functions are not called from anywhere in the C front end, but as
11610 : : these changes continue, that will change. */
11611 : :
11612 : : /* Returns the stmt_tree (if any) to which statements are currently
11613 : : being added. If there is no active statement-tree, NULL is
11614 : : returned. */
11615 : :
11616 : : stmt_tree
11617 : 961708790 : current_stmt_tree (void)
11618 : : {
11619 : 961708790 : return &c_stmt_tree;
11620 : : }
11621 : :
11622 : : /* Return the global value of T as a symbol. */
11623 : :
11624 : : tree
11625 : 3957369 : identifier_global_value (tree t)
11626 : : {
11627 : 3957369 : struct c_binding *b;
11628 : :
11629 : 3958334 : for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
11630 : 3951112 : if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11631 : 3950147 : return b->decl;
11632 : :
11633 : : return NULL_TREE;
11634 : : }
11635 : :
11636 : : /* Return the global value of tag T as a symbol. */
11637 : :
11638 : : tree
11639 : 12 : identifier_global_tag (tree t)
11640 : : {
11641 : 12 : struct c_binding *b;
11642 : :
11643 : 12 : for (b = I_TAG_BINDING (t); b; b = b->shadowed)
11644 : 11 : if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11645 : 11 : return b->decl;
11646 : :
11647 : : return NULL_TREE;
11648 : : }
11649 : :
11650 : : /* Returns non-zero (result of __has_builtin) if NAME refers to a built-in
11651 : : function or function-like operator. */
11652 : :
11653 : : int
11654 : 24630 : names_builtin_p (const char *name)
11655 : : {
11656 : 24630 : tree id = get_identifier (name);
11657 : 24630 : if (tree decl = identifier_global_value (id))
11658 : 24543 : return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_UNDECLARED_BUILTIN (decl);
11659 : :
11660 : : /* Also detect common reserved C words that aren't strictly built-in
11661 : : functions. */
11662 : 87 : switch (C_RID_CODE (id))
11663 : : {
11664 : : case RID_BUILTIN_ASSOC_BARRIER:
11665 : : case RID_BUILTIN_CONVERTVECTOR:
11666 : : case RID_BUILTIN_HAS_ATTRIBUTE:
11667 : : case RID_BUILTIN_SHUFFLE:
11668 : : case RID_BUILTIN_SHUFFLEVECTOR:
11669 : : case RID_BUILTIN_STDC:
11670 : : case RID_BUILTIN_COUNTED_BY_REF:
11671 : : case RID_CHOOSE_EXPR:
11672 : : case RID_OFFSETOF:
11673 : : case RID_TYPES_COMPATIBLE_P:
11674 : : case RID_C23_VA_START:
11675 : : case RID_VA_ARG:
11676 : : return 1;
11677 : 66 : default:
11678 : 66 : break;
11679 : : }
11680 : :
11681 : 66 : return 0;
11682 : : }
11683 : :
11684 : : /* In C, the only C-linkage public declaration is at file scope. */
11685 : :
11686 : : tree
11687 : 5 : c_linkage_bindings (tree name)
11688 : : {
11689 : 5 : return identifier_global_value (name);
11690 : : }
11691 : :
11692 : : /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
11693 : : otherwise the name is found in ridpointers from RID_INDEX. */
11694 : :
11695 : : void
11696 : 3361830 : record_builtin_type (enum rid rid_index, const char *name, tree type)
11697 : : {
11698 : 3361830 : tree id, decl;
11699 : 3361830 : if (name == 0)
11700 : 1568854 : id = ridpointers[(int) rid_index];
11701 : : else
11702 : 1792976 : id = get_identifier (name);
11703 : 3361830 : decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
11704 : 3361830 : pushdecl (decl);
11705 : 3361830 : if (debug_hooks->type_decl)
11706 : 3361830 : debug_hooks->type_decl (decl, false);
11707 : 3361830 : }
11708 : :
11709 : : /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
11710 : :
11711 : : struct c_parm *
11712 : 121132905 : build_c_parm (struct c_declspecs *specs, tree attrs,
11713 : : struct c_declarator *declarator,
11714 : : location_t loc)
11715 : : {
11716 : 121132905 : struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
11717 : 121132905 : ret->specs = specs;
11718 : 121132905 : ret->attrs = attrs;
11719 : 121132905 : ret->declarator = declarator;
11720 : 121132905 : ret->loc = loc;
11721 : 121132905 : return ret;
11722 : : }
11723 : :
11724 : : /* Return a declarator with nested attributes. TARGET is the inner
11725 : : declarator to which these attributes apply. ATTRS are the
11726 : : attributes. */
11727 : :
11728 : : struct c_declarator *
11729 : 6655 : build_attrs_declarator (tree attrs, struct c_declarator *target)
11730 : : {
11731 : 6655 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11732 : 6655 : ret->kind = cdk_attrs;
11733 : 6655 : ret->declarator = target;
11734 : 6655 : ret->u.attrs = attrs;
11735 : 6655 : return ret;
11736 : : }
11737 : :
11738 : : /* Return a declarator for a function with arguments specified by ARGS
11739 : : and return type specified by TARGET. */
11740 : :
11741 : : struct c_declarator *
11742 : 49429480 : build_function_declarator (struct c_arg_info *args,
11743 : : struct c_declarator *target)
11744 : : {
11745 : 49429480 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11746 : 49429480 : ret->kind = cdk_function;
11747 : 49429480 : ret->declarator = target;
11748 : 49429480 : ret->u.arg_info = args;
11749 : 49429480 : return ret;
11750 : : }
11751 : :
11752 : : /* Return a declarator for the identifier IDENT (which may be
11753 : : NULL_TREE for an abstract declarator). */
11754 : :
11755 : : struct c_declarator *
11756 : 306588663 : build_id_declarator (tree ident)
11757 : : {
11758 : 306588663 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11759 : 306588663 : ret->kind = cdk_id;
11760 : 306588663 : ret->declarator = 0;
11761 : 306588663 : ret->u.id.id = ident;
11762 : 306588663 : ret->u.id.attrs = NULL_TREE;
11763 : : /* Default value - may get reset to a more precise location. */
11764 : 306588663 : ret->id_loc = input_location;
11765 : 306588663 : return ret;
11766 : : }
11767 : :
11768 : : /* Return something to represent absolute declarators containing a *.
11769 : : TARGET is the absolute declarator that the * contains.
11770 : : TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
11771 : : to apply to the pointer type. */
11772 : :
11773 : : struct c_declarator *
11774 : 17907290 : make_pointer_declarator (struct c_declspecs *type_quals_attrs,
11775 : : struct c_declarator *target)
11776 : : {
11777 : 17907290 : tree attrs;
11778 : 17907290 : int quals = 0;
11779 : 17907290 : struct c_declarator *itarget = target;
11780 : 17907290 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11781 : 17907290 : if (type_quals_attrs)
11782 : : {
11783 : 17907290 : attrs = type_quals_attrs->attrs;
11784 : 17907290 : quals = quals_from_declspecs (type_quals_attrs);
11785 : 17907290 : if (attrs != NULL_TREE)
11786 : 6383 : itarget = build_attrs_declarator (attrs, target);
11787 : : }
11788 : 17907290 : ret->kind = cdk_pointer;
11789 : 17907290 : ret->declarator = itarget;
11790 : 17907290 : ret->u.pointer_quals = quals;
11791 : 17907290 : return ret;
11792 : : }
11793 : :
11794 : : /* Return a pointer to a structure for an empty list of declaration
11795 : : specifiers. */
11796 : :
11797 : : struct c_declspecs *
11798 : 443977517 : build_null_declspecs (void)
11799 : : {
11800 : 443977517 : struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
11801 : 443977517 : memset (ret, 0, sizeof *ret);
11802 : 443977517 : ret->align_log = -1;
11803 : 443977517 : ret->typespec_word = cts_none;
11804 : 443977517 : ret->storage_class = csc_none;
11805 : 443977517 : ret->expr_const_operands = true;
11806 : 443977517 : ret->typespec_kind = ctsk_none;
11807 : 443977517 : ret->address_space = ADDR_SPACE_GENERIC;
11808 : 443977517 : return ret;
11809 : : }
11810 : :
11811 : : /* Add the address space ADDRSPACE to the declaration specifiers
11812 : : SPECS, returning SPECS. */
11813 : :
11814 : : struct c_declspecs *
11815 : 174 : declspecs_add_addrspace (location_t location,
11816 : : struct c_declspecs *specs, addr_space_t as)
11817 : : {
11818 : 174 : specs->non_sc_seen_p = true;
11819 : 174 : specs->declspecs_seen_p = true;
11820 : 174 : specs->non_std_attrs_seen_p = true;
11821 : :
11822 : 174 : if (!ADDR_SPACE_GENERIC_P (specs->address_space)
11823 : 0 : && specs->address_space != as)
11824 : 0 : error ("incompatible address space qualifiers %qs and %qs",
11825 : : c_addr_space_name (as),
11826 : : c_addr_space_name (specs->address_space));
11827 : : else
11828 : : {
11829 : 174 : specs->address_space = as;
11830 : 174 : specs->locations[cdw_address_space] = location;
11831 : : }
11832 : 174 : return specs;
11833 : : }
11834 : :
11835 : : /* Add the type qualifier QUAL to the declaration specifiers SPECS,
11836 : : returning SPECS. */
11837 : :
11838 : : struct c_declspecs *
11839 : 16259061 : declspecs_add_qual (location_t loc,
11840 : : struct c_declspecs *specs, tree qual)
11841 : : {
11842 : 16259061 : enum rid i;
11843 : 16259061 : bool dupe = false;
11844 : 16259061 : specs->non_sc_seen_p = true;
11845 : 16259061 : specs->declspecs_seen_p = true;
11846 : 16259061 : specs->non_std_attrs_seen_p = true;
11847 : 16259061 : gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
11848 : : && C_IS_RESERVED_WORD (qual));
11849 : 16259061 : i = C_RID_CODE (qual);
11850 : 16259061 : location_t prev_loc = UNKNOWN_LOCATION;
11851 : 16259061 : switch (i)
11852 : : {
11853 : 12808236 : case RID_CONST:
11854 : 12808236 : dupe = specs->const_p;
11855 : 12808236 : specs->const_p = true;
11856 : 12808236 : prev_loc = specs->locations[cdw_const];
11857 : 12808236 : specs->locations[cdw_const] = loc;
11858 : 12808236 : break;
11859 : 95473 : case RID_VOLATILE:
11860 : 95473 : dupe = specs->volatile_p;
11861 : 95473 : specs->volatile_p = true;
11862 : 95473 : prev_loc = specs->locations[cdw_volatile];
11863 : 95473 : specs->locations[cdw_volatile] = loc;
11864 : 95473 : break;
11865 : 3336417 : case RID_RESTRICT:
11866 : 3336417 : dupe = specs->restrict_p;
11867 : 3336417 : specs->restrict_p = true;
11868 : 3336417 : prev_loc = specs->locations[cdw_restrict];
11869 : 3336417 : specs->locations[cdw_restrict] = loc;
11870 : 3336417 : break;
11871 : 18935 : case RID_ATOMIC:
11872 : 18935 : dupe = specs->atomic_p;
11873 : 18935 : specs->atomic_p = true;
11874 : 18935 : prev_loc = specs->locations[cdw_atomic];
11875 : 18935 : specs->locations[cdw_atomic] = loc;
11876 : 18935 : break;
11877 : 0 : default:
11878 : 0 : gcc_unreachable ();
11879 : : }
11880 : 16259061 : if (dupe)
11881 : : {
11882 : 56 : bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
11883 : : "duplicate %qE declaration specifier", qual);
11884 : 56 : if (!warned
11885 : 52 : && warn_duplicate_decl_specifier
11886 : 33 : && prev_loc >= RESERVED_LOCATION_COUNT
11887 : 33 : && !from_macro_expansion_at (prev_loc)
11888 : 77 : && !from_macro_expansion_at (loc))
11889 : 12 : warning_at (loc, OPT_Wduplicate_decl_specifier,
11890 : : "duplicate %qE declaration specifier", qual);
11891 : : }
11892 : 16259061 : return specs;
11893 : : }
11894 : :
11895 : : /* Add the type specifier TYPE to the declaration specifiers SPECS,
11896 : : returning SPECS. */
11897 : :
11898 : : struct c_declspecs *
11899 : 323868422 : declspecs_add_type (location_t loc, struct c_declspecs *specs,
11900 : : struct c_typespec spec)
11901 : : {
11902 : 323868422 : tree type = spec.spec;
11903 : 323868422 : specs->non_sc_seen_p = true;
11904 : 323868422 : specs->declspecs_seen_p = true;
11905 : 323868422 : specs->non_std_attrs_seen_p = true;
11906 : 323868422 : specs->typespec_kind = spec.kind;
11907 : 323868422 : if (TREE_DEPRECATED (type))
11908 : 56 : specs->deprecated_p = true;
11909 : 323868422 : if (TREE_UNAVAILABLE (type))
11910 : 40 : specs->unavailable_p = true;
11911 : :
11912 : : /* As a type specifier is present, "auto" must be used as a storage
11913 : : class specifier, not for type deduction. */
11914 : 323868422 : if (specs->c23_auto_p)
11915 : : {
11916 : 112 : specs->c23_auto_p = false;
11917 : 112 : if (specs->storage_class != csc_none)
11918 : 1 : error ("multiple storage classes in declaration specifiers");
11919 : 111 : else if (specs->thread_p)
11920 : 1 : error ("%qs used with %<auto%>",
11921 : 1 : specs->thread_gnu_p ? "__thread" : "_Thread_local");
11922 : 110 : else if (specs->constexpr_p)
11923 : : /* auto may only be used with another storage class specifier,
11924 : : such as constexpr, if the type is inferred. */
11925 : 2 : error ("%<auto%> used with %<constexpr%>");
11926 : : else
11927 : 108 : specs->storage_class = csc_auto;
11928 : : }
11929 : :
11930 : : /* Handle type specifier keywords. */
11931 : 323868422 : if (TREE_CODE (type) == IDENTIFIER_NODE
11932 : 81078217 : && C_IS_RESERVED_WORD (type)
11933 : 404946639 : && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
11934 : : {
11935 : 81078217 : enum rid i = C_RID_CODE (type);
11936 : 81078217 : if (specs->type)
11937 : : {
11938 : 58 : error_at (loc, "two or more data types in declaration specifiers");
11939 : 58 : return specs;
11940 : : }
11941 : 81078159 : if ((int) i <= (int) RID_LAST_MODIFIER)
11942 : : {
11943 : : /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
11944 : 21360526 : bool dupe = false;
11945 : 21360526 : switch (i)
11946 : : {
11947 : 10533028 : case RID_LONG:
11948 : 10533028 : if (specs->long_long_p)
11949 : : {
11950 : 104 : error_at (loc, "%<long long long%> is too long for GCC");
11951 : 104 : break;
11952 : : }
11953 : 10532924 : if (specs->long_p)
11954 : : {
11955 : 2890017 : if (specs->typespec_word == cts_double)
11956 : : {
11957 : 15 : error_at (loc,
11958 : : ("both %<long long%> and %<double%> in "
11959 : : "declaration specifiers"));
11960 : 15 : break;
11961 : : }
11962 : 2890002 : pedwarn_c90 (loc, OPT_Wlong_long,
11963 : : "ISO C90 does not support %<long long%>");
11964 : 2890002 : specs->long_long_p = 1;
11965 : 2890002 : specs->locations[cdw_long_long] = loc;
11966 : 2890002 : break;
11967 : : }
11968 : 7642907 : if (specs->short_p)
11969 : 77 : error_at (loc,
11970 : : ("both %<long%> and %<short%> in "
11971 : : "declaration specifiers"));
11972 : 7642830 : else if (specs->typespec_word == cts_auto_type)
11973 : 1 : error_at (loc,
11974 : : ("both %<long%> and %<__auto_type%> in "
11975 : : "declaration specifiers"));
11976 : : else if (specs->typespec_word == cts_void)
11977 : 5 : error_at (loc,
11978 : : ("both %<long%> and %<void%> in "
11979 : : "declaration specifiers"));
11980 : : else if (specs->typespec_word == cts_int_n)
11981 : 19 : error_at (loc,
11982 : : ("both %<long%> and %<__int%d%> in "
11983 : : "declaration specifiers"),
11984 : 19 : int_n_data[specs->u.int_n_idx].bitsize);
11985 : : else if (specs->typespec_word == cts_bool)
11986 : 3 : error_at (loc,
11987 : : ("both %<long%> and %<_Bool%> in "
11988 : : "declaration specifiers"));
11989 : : else if (specs->typespec_word == cts_bitint)
11990 : 3 : error_at (loc,
11991 : : ("both %<long%> and %<_BitInt%> in "
11992 : : "declaration specifiers"));
11993 : : else if (specs->typespec_word == cts_char)
11994 : 21 : error_at (loc,
11995 : : ("both %<long%> and %<char%> in "
11996 : : "declaration specifiers"));
11997 : : else if (specs->typespec_word == cts_float)
11998 : 7 : error_at (loc,
11999 : : ("both %<long%> and %<float%> in "
12000 : : "declaration specifiers"));
12001 : : else if (specs->typespec_word == cts_floatn_nx)
12002 : 0 : error_at (loc,
12003 : : ("both %<long%> and %<_Float%d%s%> in "
12004 : : "declaration specifiers"),
12005 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12006 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12007 : : ? "x"
12008 : : : ""));
12009 : : else if (specs->typespec_word == cts_dfloat32)
12010 : 4 : error_at (loc,
12011 : : ("both %<long%> and %<_Decimal32%> in "
12012 : : "declaration specifiers"));
12013 : : else if (specs->typespec_word == cts_dfloat64)
12014 : 4 : error_at (loc,
12015 : : ("both %<long%> and %<_Decimal64%> in "
12016 : : "declaration specifiers"));
12017 : : else if (specs->typespec_word == cts_dfloat128)
12018 : 4 : error_at (loc,
12019 : : ("both %<long%> and %<_Decimal128%> in "
12020 : : "declaration specifiers"));
12021 : : else if (specs->typespec_word == cts_dfloat64x)
12022 : 0 : error_at (loc,
12023 : : ("both %<long%> and %<_Decimal64x%> in "
12024 : : "declaration specifiers"));
12025 : : else
12026 : : {
12027 : 7642759 : specs->long_p = true;
12028 : 7642759 : specs->locations[cdw_long] = loc;
12029 : : }
12030 : : break;
12031 : 1665893 : case RID_SHORT:
12032 : 1665893 : dupe = specs->short_p;
12033 : 1665893 : if (specs->long_p)
12034 : 197 : error_at (loc,
12035 : : ("both %<long%> and %<short%> in "
12036 : : "declaration specifiers"));
12037 : 1665696 : else if (specs->typespec_word == cts_auto_type)
12038 : 1 : error_at (loc,
12039 : : ("both %<short%> and %<__auto_type%> in "
12040 : : "declaration specifiers"));
12041 : : else if (specs->typespec_word == cts_void)
12042 : 5 : error_at (loc,
12043 : : ("both %<short%> and %<void%> in "
12044 : : "declaration specifiers"));
12045 : : else if (specs->typespec_word == cts_int_n)
12046 : 19 : error_at (loc,
12047 : : ("both %<short%> and %<__int%d%> in "
12048 : : "declaration specifiers"),
12049 : 19 : int_n_data[specs->u.int_n_idx].bitsize);
12050 : : else if (specs->typespec_word == cts_bool)
12051 : 3 : error_at (loc,
12052 : : ("both %<short%> and %<_Bool%> in "
12053 : : "declaration specifiers"));
12054 : : else if (specs->typespec_word == cts_bitint)
12055 : 1 : error_at (loc,
12056 : : ("both %<short%> and %<_BitInt%> in "
12057 : : "declaration specifiers"));
12058 : : else if (specs->typespec_word == cts_char)
12059 : 21 : error_at (loc,
12060 : : ("both %<short%> and %<char%> in "
12061 : : "declaration specifiers"));
12062 : : else if (specs->typespec_word == cts_float)
12063 : 7 : error_at (loc,
12064 : : ("both %<short%> and %<float%> in "
12065 : : "declaration specifiers"));
12066 : : else if (specs->typespec_word == cts_double)
12067 : 7 : error_at (loc,
12068 : : ("both %<short%> and %<double%> in "
12069 : : "declaration specifiers"));
12070 : : else if (specs->typespec_word == cts_floatn_nx)
12071 : 0 : error_at (loc,
12072 : : ("both %<short%> and %<_Float%d%s%> in "
12073 : : "declaration specifiers"),
12074 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12075 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12076 : : ? "x"
12077 : : : ""));
12078 : : else if (specs->typespec_word == cts_dfloat32)
12079 : 4 : error_at (loc,
12080 : : ("both %<short%> and %<_Decimal32%> in "
12081 : : "declaration specifiers"));
12082 : : else if (specs->typespec_word == cts_dfloat64)
12083 : 4 : error_at (loc,
12084 : : ("both %<short%> and %<_Decimal64%> in "
12085 : : "declaration specifiers"));
12086 : : else if (specs->typespec_word == cts_dfloat128)
12087 : 4 : error_at (loc,
12088 : : ("both %<short%> and %<_Decimal128%> in "
12089 : : "declaration specifiers"));
12090 : : else if (specs->typespec_word == cts_dfloat64x)
12091 : 0 : error_at (loc,
12092 : : ("both %<short%> and %<_Decimal64x%> in "
12093 : : "declaration specifiers"));
12094 : : else
12095 : : {
12096 : 1665620 : specs->short_p = true;
12097 : 1665620 : specs->locations[cdw_short] = loc;
12098 : : }
12099 : : break;
12100 : 569481 : case RID_SIGNED:
12101 : 569481 : dupe = specs->signed_p;
12102 : 569481 : if (specs->unsigned_p)
12103 : 138 : error_at (loc,
12104 : : ("both %<signed%> and %<unsigned%> in "
12105 : : "declaration specifiers"));
12106 : 569343 : else if (specs->typespec_word == cts_auto_type)
12107 : 1 : error_at (loc,
12108 : : ("both %<signed%> and %<__auto_type%> in "
12109 : : "declaration specifiers"));
12110 : : else if (specs->typespec_word == cts_void)
12111 : 5 : error_at (loc,
12112 : : ("both %<signed%> and %<void%> in "
12113 : : "declaration specifiers"));
12114 : : else if (specs->typespec_word == cts_bool)
12115 : 3 : error_at (loc,
12116 : : ("both %<signed%> and %<_Bool%> in "
12117 : : "declaration specifiers"));
12118 : : else if (specs->typespec_word == cts_float)
12119 : 7 : error_at (loc,
12120 : : ("both %<signed%> and %<float%> in "
12121 : : "declaration specifiers"));
12122 : : else if (specs->typespec_word == cts_double)
12123 : 21 : error_at (loc,
12124 : : ("both %<signed%> and %<double%> in "
12125 : : "declaration specifiers"));
12126 : : else if (specs->typespec_word == cts_floatn_nx)
12127 : 0 : error_at (loc,
12128 : : ("both %<signed%> and %<_Float%d%s%> in "
12129 : : "declaration specifiers"),
12130 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12131 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12132 : : ? "x"
12133 : : : ""));
12134 : : else if (specs->typespec_word == cts_dfloat32)
12135 : 4 : error_at (loc,
12136 : : ("both %<signed%> and %<_Decimal32%> in "
12137 : : "declaration specifiers"));
12138 : : else if (specs->typespec_word == cts_dfloat64)
12139 : 4 : error_at (loc,
12140 : : ("both %<signed%> and %<_Decimal64%> in "
12141 : : "declaration specifiers"));
12142 : : else if (specs->typespec_word == cts_dfloat128)
12143 : 4 : error_at (loc,
12144 : : ("both %<signed%> and %<_Decimal128%> in "
12145 : : "declaration specifiers"));
12146 : : else if (specs->typespec_word == cts_dfloat64x)
12147 : 0 : error_at (loc,
12148 : : ("both %<signed%> and %<_Decimal64x%> in "
12149 : : "declaration specifiers"));
12150 : : else
12151 : : {
12152 : 569294 : specs->signed_p = true;
12153 : 569294 : specs->locations[cdw_signed] = loc;
12154 : : }
12155 : : break;
12156 : 6045030 : case RID_UNSIGNED:
12157 : 6045030 : dupe = specs->unsigned_p;
12158 : 6045030 : if (specs->signed_p)
12159 : 139 : error_at (loc,
12160 : : ("both %<signed%> and %<unsigned%> in "
12161 : : "declaration specifiers"));
12162 : 6044891 : else if (specs->typespec_word == cts_auto_type)
12163 : 1 : error_at (loc,
12164 : : ("both %<unsigned%> and %<__auto_type%> in "
12165 : : "declaration specifiers"));
12166 : : else if (specs->typespec_word == cts_void)
12167 : 5 : error_at (loc,
12168 : : ("both %<unsigned%> and %<void%> in "
12169 : : "declaration specifiers"));
12170 : : else if (specs->typespec_word == cts_bool)
12171 : 3 : error_at (loc,
12172 : : ("both %<unsigned%> and %<_Bool%> in "
12173 : : "declaration specifiers"));
12174 : : else if (specs->typespec_word == cts_float)
12175 : 7 : error_at (loc,
12176 : : ("both %<unsigned%> and %<float%> in "
12177 : : "declaration specifiers"));
12178 : : else if (specs->typespec_word == cts_double)
12179 : 21 : error_at (loc,
12180 : : ("both %<unsigned%> and %<double%> in "
12181 : : "declaration specifiers"));
12182 : : else if (specs->typespec_word == cts_floatn_nx)
12183 : 0 : error_at (loc,
12184 : : ("both %<unsigned%> and %<_Float%d%s%> in "
12185 : : "declaration specifiers"),
12186 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12187 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12188 : : ? "x"
12189 : : : ""));
12190 : : else if (specs->typespec_word == cts_dfloat32)
12191 : 2 : error_at (loc,
12192 : : ("both %<unsigned%> and %<_Decimal32%> in "
12193 : : "declaration specifiers"));
12194 : : else if (specs->typespec_word == cts_dfloat64)
12195 : 2 : error_at (loc,
12196 : : ("both %<unsigned%> and %<_Decimal64%> in "
12197 : : "declaration specifiers"));
12198 : : else if (specs->typespec_word == cts_dfloat128)
12199 : 2 : error_at (loc,
12200 : : ("both %<unsigned%> and %<_Decimal128%> in "
12201 : : "declaration specifiers"));
12202 : : else if (specs->typespec_word == cts_dfloat64x)
12203 : 0 : error_at (loc,
12204 : : ("both %<unsigned%> and %<_Decimal64x%> in "
12205 : : "declaration specifiers"));
12206 : : else
12207 : : {
12208 : 6044848 : specs->unsigned_p = true;
12209 : 6044848 : specs->locations[cdw_unsigned] = loc;
12210 : : }
12211 : : break;
12212 : 2546971 : case RID_COMPLEX:
12213 : 2546971 : dupe = specs->complex_p;
12214 : 2546971 : if (!in_system_header_at (loc))
12215 : 68931 : pedwarn_c90 (loc, OPT_Wpedantic,
12216 : : "ISO C90 does not support complex types");
12217 : 2546971 : if (specs->typespec_word == cts_auto_type)
12218 : 1 : error_at (loc,
12219 : : ("both %<complex%> and %<__auto_type%> in "
12220 : : "declaration specifiers"));
12221 : : else if (specs->typespec_word == cts_void)
12222 : 2 : error_at (loc,
12223 : : ("both %<complex%> and %<void%> in "
12224 : : "declaration specifiers"));
12225 : : else if (specs->typespec_word == cts_bool)
12226 : 2 : error_at (loc,
12227 : : ("both %<complex%> and %<_Bool%> in "
12228 : : "declaration specifiers"));
12229 : : else if (specs->typespec_word == cts_bitint)
12230 : 1 : error_at (loc,
12231 : : ("both %<complex%> and %<_BitInt%> in "
12232 : : "declaration specifiers"));
12233 : : else if (specs->typespec_word == cts_dfloat32)
12234 : 1 : error_at (loc,
12235 : : ("both %<complex%> and %<_Decimal32%> in "
12236 : : "declaration specifiers"));
12237 : : else if (specs->typespec_word == cts_dfloat64)
12238 : 1 : error_at (loc,
12239 : : ("both %<complex%> and %<_Decimal64%> in "
12240 : : "declaration specifiers"));
12241 : : else if (specs->typespec_word == cts_dfloat128)
12242 : 1 : error_at (loc,
12243 : : ("both %<complex%> and %<_Decimal128%> in "
12244 : : "declaration specifiers"));
12245 : : else if (specs->typespec_word == cts_dfloat64x)
12246 : 0 : error_at (loc,
12247 : : ("both %<complex%> and %<_Decimal64x%> in "
12248 : : "declaration specifiers"));
12249 : : else if (specs->typespec_word == cts_fract)
12250 : 0 : error_at (loc,
12251 : : ("both %<complex%> and %<_Fract%> in "
12252 : : "declaration specifiers"));
12253 : : else if (specs->typespec_word == cts_accum)
12254 : 0 : error_at (loc,
12255 : : ("both %<complex%> and %<_Accum%> in "
12256 : : "declaration specifiers"));
12257 : 2546962 : else if (specs->saturating_p)
12258 : 0 : error_at (loc,
12259 : : ("both %<complex%> and %<_Sat%> in "
12260 : : "declaration specifiers"));
12261 : : else
12262 : : {
12263 : 2546962 : specs->complex_p = true;
12264 : 2546962 : specs->locations[cdw_complex] = loc;
12265 : : }
12266 : : break;
12267 : 123 : case RID_SAT:
12268 : 123 : dupe = specs->saturating_p;
12269 : 123 : pedwarn (loc, OPT_Wpedantic,
12270 : : "ISO C does not support saturating types");
12271 : 123 : if (specs->typespec_word == cts_int_n)
12272 : : {
12273 : 0 : error_at (loc,
12274 : : ("both %<_Sat%> and %<__int%d%> in "
12275 : : "declaration specifiers"),
12276 : 0 : int_n_data[specs->u.int_n_idx].bitsize);
12277 : : }
12278 : : else if (specs->typespec_word == cts_auto_type)
12279 : 0 : error_at (loc,
12280 : : ("both %<_Sat%> and %<__auto_type%> in "
12281 : : "declaration specifiers"));
12282 : : else if (specs->typespec_word == cts_void)
12283 : 0 : error_at (loc,
12284 : : ("both %<_Sat%> and %<void%> in "
12285 : : "declaration specifiers"));
12286 : : else if (specs->typespec_word == cts_bool)
12287 : 0 : error_at (loc,
12288 : : ("both %<_Sat%> and %<_Bool%> in "
12289 : : "declaration specifiers"));
12290 : : else if (specs->typespec_word == cts_bitint)
12291 : 0 : error_at (loc,
12292 : : ("both %<_Sat%> and %<_BitInt%> in "
12293 : : "declaration specifiers"));
12294 : : else if (specs->typespec_word == cts_char)
12295 : 0 : error_at (loc,
12296 : : ("both %<_Sat%> and %<char%> in "
12297 : : "declaration specifiers"));
12298 : : else if (specs->typespec_word == cts_int)
12299 : 0 : error_at (loc,
12300 : : ("both %<_Sat%> and %<int%> in "
12301 : : "declaration specifiers"));
12302 : : else if (specs->typespec_word == cts_float)
12303 : 0 : error_at (loc,
12304 : : ("both %<_Sat%> and %<float%> in "
12305 : : "declaration specifiers"));
12306 : : else if (specs->typespec_word == cts_double)
12307 : 0 : error_at (loc,
12308 : : ("both %<_Sat%> and %<double%> in "
12309 : : "declaration specifiers"));
12310 : : else if (specs->typespec_word == cts_floatn_nx)
12311 : 0 : error_at (loc,
12312 : : ("both %<_Sat%> and %<_Float%d%s%> in "
12313 : : "declaration specifiers"),
12314 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12315 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12316 : : ? "x"
12317 : : : ""));
12318 : : else if (specs->typespec_word == cts_dfloat32)
12319 : 0 : error_at (loc,
12320 : : ("both %<_Sat%> and %<_Decimal32%> in "
12321 : : "declaration specifiers"));
12322 : : else if (specs->typespec_word == cts_dfloat64)
12323 : 0 : error_at (loc,
12324 : : ("both %<_Sat%> and %<_Decimal64%> in "
12325 : : "declaration specifiers"));
12326 : : else if (specs->typespec_word == cts_dfloat128)
12327 : 0 : error_at (loc,
12328 : : ("both %<_Sat%> and %<_Decimal128%> in "
12329 : : "declaration specifiers"));
12330 : : else if (specs->typespec_word == cts_dfloat64x)
12331 : 0 : error_at (loc,
12332 : : ("both %<_Sat%> and %<_Decimal64x%> in "
12333 : : "declaration specifiers"));
12334 : 123 : else if (specs->complex_p)
12335 : 0 : error_at (loc,
12336 : : ("both %<_Sat%> and %<complex%> in "
12337 : : "declaration specifiers"));
12338 : : else
12339 : : {
12340 : 123 : specs->saturating_p = true;
12341 : 123 : specs->locations[cdw_saturating] = loc;
12342 : : }
12343 : : break;
12344 : 0 : default:
12345 : 0 : gcc_unreachable ();
12346 : : }
12347 : :
12348 : 21360526 : if (dupe)
12349 : 378 : error_at (loc, "duplicate %qE", type);
12350 : :
12351 : 21360526 : return specs;
12352 : : }
12353 : : else
12354 : : {
12355 : : /* "void", "_Bool", "char", "int", "float", "double",
12356 : : "_FloatN", "_FloatNx", "_Decimal32", "__intN",
12357 : : "_Decimal64", "_Decimal128", "_Fract", "_Accum", "_BitInt(N)" or
12358 : : "__auto_type". */
12359 : 59717633 : if (specs->typespec_word != cts_none)
12360 : : {
12361 : 2171 : if (i == RID_BOOL)
12362 : : {
12363 : 175 : auto_diagnostic_group d;
12364 : 175 : if (specs->storage_class == csc_typedef)
12365 : 4 : error_at (loc,
12366 : : "%qs cannot be defined via %<typedef%>",
12367 : 2 : IDENTIFIER_POINTER (type));
12368 : : else
12369 : 346 : error_at (loc,
12370 : : "%qs cannot be used here",
12371 : 173 : IDENTIFIER_POINTER (type));
12372 : 175 : add_note_about_new_keyword (loc, type);
12373 : 175 : }
12374 : : else
12375 : 1996 : error_at (loc,
12376 : : "two or more data types in declaration specifiers");
12377 : 2171 : return specs;
12378 : : }
12379 : 59715462 : switch (i)
12380 : : {
12381 : 1838 : case RID_AUTO_TYPE:
12382 : 1838 : if (specs->long_p)
12383 : 1 : error_at (loc,
12384 : : ("both %<long%> and %<__auto_type%> in "
12385 : : "declaration specifiers"));
12386 : 1837 : else if (specs->short_p)
12387 : 1 : error_at (loc,
12388 : : ("both %<short%> and %<__auto_type%> in "
12389 : : "declaration specifiers"));
12390 : 1836 : else if (specs->signed_p)
12391 : 1 : error_at (loc,
12392 : : ("both %<signed%> and %<__auto_type%> in "
12393 : : "declaration specifiers"));
12394 : 1835 : else if (specs->unsigned_p)
12395 : 1 : error_at (loc,
12396 : : ("both %<unsigned%> and %<__auto_type%> in "
12397 : : "declaration specifiers"));
12398 : 1834 : else if (specs->complex_p)
12399 : 1 : error_at (loc,
12400 : : ("both %<complex%> and %<__auto_type%> in "
12401 : : "declaration specifiers"));
12402 : 1833 : else if (specs->saturating_p)
12403 : 0 : error_at (loc,
12404 : : ("both %<_Sat%> and %<__auto_type%> in "
12405 : : "declaration specifiers"));
12406 : : else
12407 : : {
12408 : 1833 : specs->typespec_word = cts_auto_type;
12409 : 1833 : specs->locations[cdw_typespec] = loc;
12410 : : }
12411 : 1838 : return specs;
12412 : 51356 : case RID_INT_N_0:
12413 : 51356 : case RID_INT_N_1:
12414 : 51356 : case RID_INT_N_2:
12415 : 51356 : case RID_INT_N_3:
12416 : 51356 : specs->u.int_n_idx = i - RID_INT_N_0;
12417 : 51356 : if (!in_system_header_at (input_location)
12418 : : /* If the INT_N type ends in "__", and so is of the format
12419 : : "__intN__", don't pedwarn. */
12420 : 51356 : && (strncmp (IDENTIFIER_POINTER (type)
12421 : 40948 : + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
12422 : 40948 : pedwarn (loc, OPT_Wpedantic,
12423 : : "ISO C does not support %<__int%d%> types",
12424 : 40948 : int_n_data[specs->u.int_n_idx].bitsize);
12425 : :
12426 : 51356 : if (specs->long_p)
12427 : 53 : error_at (loc,
12428 : : ("both %<__int%d%> and %<long%> in "
12429 : : "declaration specifiers"),
12430 : 53 : int_n_data[specs->u.int_n_idx].bitsize);
12431 : 51303 : else if (specs->saturating_p)
12432 : 0 : error_at (loc,
12433 : : ("both %<_Sat%> and %<__int%d%> in "
12434 : : "declaration specifiers"),
12435 : 0 : int_n_data[specs->u.int_n_idx].bitsize);
12436 : 51303 : else if (specs->short_p)
12437 : 19 : error_at (loc,
12438 : : ("both %<__int%d%> and %<short%> in "
12439 : : "declaration specifiers"),
12440 : 19 : int_n_data[specs->u.int_n_idx].bitsize);
12441 : 51284 : else if (! int_n_enabled_p[specs->u.int_n_idx])
12442 : : {
12443 : 0 : specs->typespec_word = cts_int_n;
12444 : 0 : error_at (loc,
12445 : : "%<__int%d%> is not supported on this target",
12446 : 0 : int_n_data[specs->u.int_n_idx].bitsize);
12447 : : }
12448 : : else
12449 : : {
12450 : 51284 : specs->typespec_word = cts_int_n;
12451 : 51284 : specs->locations[cdw_typespec] = loc;
12452 : : }
12453 : 51356 : return specs;
12454 : 7678970 : case RID_VOID:
12455 : 7678970 : if (specs->long_p)
12456 : 44 : error_at (loc,
12457 : : ("both %<long%> and %<void%> in "
12458 : : "declaration specifiers"));
12459 : 7678926 : else if (specs->short_p)
12460 : 21 : error_at (loc,
12461 : : ("both %<short%> and %<void%> in "
12462 : : "declaration specifiers"));
12463 : 7678905 : else if (specs->signed_p)
12464 : 5 : error_at (loc,
12465 : : ("both %<signed%> and %<void%> in "
12466 : : "declaration specifiers"));
12467 : 7678900 : else if (specs->unsigned_p)
12468 : 5 : error_at (loc,
12469 : : ("both %<unsigned%> and %<void%> in "
12470 : : "declaration specifiers"));
12471 : 7678895 : else if (specs->complex_p)
12472 : 2 : error_at (loc,
12473 : : ("both %<complex%> and %<void%> in "
12474 : : "declaration specifiers"));
12475 : 7678893 : else if (specs->saturating_p)
12476 : 0 : error_at (loc,
12477 : : ("both %<_Sat%> and %<void%> in "
12478 : : "declaration specifiers"));
12479 : : else
12480 : : {
12481 : 7678893 : specs->typespec_word = cts_void;
12482 : 7678893 : specs->locations[cdw_typespec] = loc;
12483 : : }
12484 : 7678970 : return specs;
12485 : 82504 : case RID_BOOL:
12486 : 82504 : if (!in_system_header_at (loc))
12487 : 68185 : pedwarn_c90 (loc, OPT_Wpedantic,
12488 : : "ISO C90 does not support boolean types");
12489 : 82504 : if (specs->long_p)
12490 : 27 : error_at (loc,
12491 : : ("both %<long%> and %<_Bool%> in "
12492 : : "declaration specifiers"));
12493 : 82477 : else if (specs->short_p)
12494 : 11 : error_at (loc,
12495 : : ("both %<short%> and %<_Bool%> in "
12496 : : "declaration specifiers"));
12497 : 82466 : else if (specs->signed_p)
12498 : 3 : error_at (loc,
12499 : : ("both %<signed%> and %<_Bool%> in "
12500 : : "declaration specifiers"));
12501 : 82463 : else if (specs->unsigned_p)
12502 : 3 : error_at (loc,
12503 : : ("both %<unsigned%> and %<_Bool%> in "
12504 : : "declaration specifiers"));
12505 : 82460 : else if (specs->complex_p)
12506 : 2 : error_at (loc,
12507 : : ("both %<complex%> and %<_Bool%> in "
12508 : : "declaration specifiers"));
12509 : 82458 : else if (specs->saturating_p)
12510 : 0 : error_at (loc,
12511 : : ("both %<_Sat%> and %<_Bool%> in "
12512 : : "declaration specifiers"));
12513 : : else
12514 : : {
12515 : 82458 : specs->typespec_word = cts_bool;
12516 : 82458 : specs->locations[cdw_typespec] = loc;
12517 : : }
12518 : 82504 : return specs;
12519 : 8941214 : case RID_CHAR:
12520 : 8941214 : if (specs->long_p)
12521 : 44 : error_at (loc,
12522 : : ("both %<long%> and %<char%> in "
12523 : : "declaration specifiers"));
12524 : 8941170 : else if (specs->short_p)
12525 : 21 : error_at (loc,
12526 : : ("both %<short%> and %<char%> in "
12527 : : "declaration specifiers"));
12528 : 8941149 : else if (specs->saturating_p)
12529 : 0 : error_at (loc,
12530 : : ("both %<_Sat%> and %<char%> in "
12531 : : "declaration specifiers"));
12532 : : else
12533 : : {
12534 : 8941149 : specs->typespec_word = cts_char;
12535 : 8941149 : specs->locations[cdw_typespec] = loc;
12536 : : }
12537 : 8941214 : return specs;
12538 : 23373492 : case RID_INT:
12539 : 23373492 : if (specs->saturating_p)
12540 : 0 : error_at (loc,
12541 : : ("both %<_Sat%> and %<int%> in "
12542 : : "declaration specifiers"));
12543 : : else
12544 : : {
12545 : 23373492 : specs->typespec_word = cts_int;
12546 : 23373492 : specs->locations[cdw_typespec] = loc;
12547 : : }
12548 : 23373492 : return specs;
12549 : 3336726 : case RID_FLOAT:
12550 : 3336726 : if (specs->long_p)
12551 : 44 : error_at (loc,
12552 : : ("both %<long%> and %<float%> in "
12553 : : "declaration specifiers"));
12554 : 3336682 : else if (specs->short_p)
12555 : 21 : error_at (loc,
12556 : : ("both %<short%> and %<float%> in "
12557 : : "declaration specifiers"));
12558 : 3336661 : else if (specs->signed_p)
12559 : 5 : error_at (loc,
12560 : : ("both %<signed%> and %<float%> in "
12561 : : "declaration specifiers"));
12562 : 3336656 : else if (specs->unsigned_p)
12563 : 5 : error_at (loc,
12564 : : ("both %<unsigned%> and %<float%> in "
12565 : : "declaration specifiers"));
12566 : 3336651 : else if (specs->saturating_p)
12567 : 0 : error_at (loc,
12568 : : ("both %<_Sat%> and %<float%> in "
12569 : : "declaration specifiers"));
12570 : : else
12571 : : {
12572 : 3336651 : specs->typespec_word = cts_float;
12573 : 3336651 : specs->locations[cdw_typespec] = loc;
12574 : : }
12575 : 3336726 : return specs;
12576 : 6015536 : case RID_DOUBLE:
12577 : 6015536 : if (specs->long_long_p)
12578 : 22 : error_at (loc,
12579 : : ("both %<long long%> and %<double%> in "
12580 : : "declaration specifiers"));
12581 : 6015514 : else if (specs->short_p)
12582 : 21 : error_at (loc,
12583 : : ("both %<short%> and %<double%> in "
12584 : : "declaration specifiers"));
12585 : 6015493 : else if (specs->signed_p)
12586 : 13 : error_at (loc,
12587 : : ("both %<signed%> and %<double%> in "
12588 : : "declaration specifiers"));
12589 : 6015480 : else if (specs->unsigned_p)
12590 : 13 : error_at (loc,
12591 : : ("both %<unsigned%> and %<double%> in "
12592 : : "declaration specifiers"));
12593 : 6015467 : else if (specs->saturating_p)
12594 : 0 : error_at (loc,
12595 : : ("both %<_Sat%> and %<double%> in "
12596 : : "declaration specifiers"));
12597 : : else
12598 : : {
12599 : 6015467 : specs->typespec_word = cts_double;
12600 : 6015467 : specs->locations[cdw_typespec] = loc;
12601 : : }
12602 : 6015536 : return specs;
12603 : 10140520 : CASE_RID_FLOATN_NX:
12604 : 10140520 : specs->u.floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
12605 : 10140520 : if (!in_system_header_at (input_location))
12606 : 51467 : pedwarn_c11 (loc, OPT_Wpedantic,
12607 : : "ISO C does not support the %<_Float%d%s%> type"
12608 : : " before C23",
12609 : 51467 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12610 : 51467 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12611 : : ? "x"
12612 : : : ""));
12613 : :
12614 : 10140520 : if (specs->long_p)
12615 : 0 : error_at (loc,
12616 : : ("both %<long%> and %<_Float%d%s%> in "
12617 : : "declaration specifiers"),
12618 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12619 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12620 : : ? "x"
12621 : : : ""));
12622 : 10140520 : else if (specs->short_p)
12623 : 0 : error_at (loc,
12624 : : ("both %<short%> and %<_Float%d%s%> in "
12625 : : "declaration specifiers"),
12626 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12627 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12628 : : ? "x"
12629 : : : ""));
12630 : 10140520 : else if (specs->signed_p)
12631 : 0 : error_at (loc,
12632 : : ("both %<signed%> and %<_Float%d%s%> in "
12633 : : "declaration specifiers"),
12634 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12635 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12636 : : ? "x"
12637 : : : ""));
12638 : 10140520 : else if (specs->unsigned_p)
12639 : 0 : error_at (loc,
12640 : : ("both %<unsigned%> and %<_Float%d%s%> in "
12641 : : "declaration specifiers"),
12642 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12643 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12644 : : ? "x"
12645 : : : ""));
12646 : 10140520 : else if (specs->saturating_p)
12647 : 0 : error_at (loc,
12648 : : ("both %<_Sat%> and %<_Float%d%s%> in "
12649 : : "declaration specifiers"),
12650 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12651 : 0 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12652 : : ? "x"
12653 : : : ""));
12654 : 10140520 : else if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
12655 : : {
12656 : 88 : specs->typespec_word = cts_floatn_nx;
12657 : 176 : error_at (loc,
12658 : : "%<_Float%d%s%> is not supported on this target",
12659 : 88 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12660 : 88 : (floatn_nx_types[specs->u.floatn_nx_idx].extended
12661 : : ? "x"
12662 : : : ""));
12663 : : }
12664 : : else
12665 : : {
12666 : 10140432 : specs->typespec_word = cts_floatn_nx;
12667 : 10140432 : specs->locations[cdw_typespec] = loc;
12668 : : }
12669 : 10140520 : return specs;
12670 : 47632 : case RID_DFLOAT32:
12671 : 47632 : case RID_DFLOAT64:
12672 : 47632 : case RID_DFLOAT128:
12673 : 47632 : case RID_DFLOAT64X:
12674 : 47632 : {
12675 : 47632 : const char *str;
12676 : 47632 : if (i == RID_DFLOAT32)
12677 : : str = "_Decimal32";
12678 : : else if (i == RID_DFLOAT64)
12679 : : str = "_Decimal64";
12680 : : else if (i == RID_DFLOAT128)
12681 : : str = "_Decimal128";
12682 : : else
12683 : 47632 : str = "_Decimal64x";
12684 : 47632 : if (specs->long_long_p)
12685 : 18 : error_at (loc,
12686 : : ("both %<long long%> and %qs in "
12687 : : "declaration specifiers"),
12688 : : str);
12689 : 47632 : if (specs->long_p)
12690 : 33 : error_at (loc,
12691 : : ("both %<long%> and %qs in "
12692 : : "declaration specifiers"),
12693 : : str);
12694 : 47599 : else if (specs->short_p)
12695 : 18 : error_at (loc,
12696 : : ("both %<short%> and %qs in "
12697 : : "declaration specifiers"),
12698 : : str);
12699 : 47581 : else if (specs->signed_p)
12700 : 6 : error_at (loc,
12701 : : ("both %<signed%> and %qs in "
12702 : : "declaration specifiers"),
12703 : : str);
12704 : 47575 : else if (specs->unsigned_p)
12705 : 3 : error_at (loc,
12706 : : ("both %<unsigned%> and %qs in "
12707 : : "declaration specifiers"),
12708 : : str);
12709 : 47572 : else if (specs->complex_p)
12710 : 3 : error_at (loc,
12711 : : ("both %<complex%> and %qs in "
12712 : : "declaration specifiers"),
12713 : : str);
12714 : 47569 : else if (specs->saturating_p)
12715 : 0 : error_at (loc,
12716 : : ("both %<_Sat%> and %qs in "
12717 : : "declaration specifiers"),
12718 : : str);
12719 : 47569 : else if (i == RID_DFLOAT32)
12720 : 15957 : specs->typespec_word = cts_dfloat32;
12721 : 31612 : else if (i == RID_DFLOAT64)
12722 : 15832 : specs->typespec_word = cts_dfloat64;
12723 : 15780 : else if (i == RID_DFLOAT128)
12724 : 15733 : specs->typespec_word = cts_dfloat128;
12725 : : else
12726 : 47 : specs->typespec_word = cts_dfloat64x;
12727 : 47632 : specs->locations[cdw_typespec] = loc;
12728 : : }
12729 : 47632 : if (!targetm.decimal_float_supported_p ())
12730 : 0 : error_at (loc,
12731 : : ("decimal floating-point not supported "
12732 : : "for this target"));
12733 : 47632 : pedwarn_c11 (loc, OPT_Wpedantic,
12734 : : "ISO C does not support decimal floating-point "
12735 : : "before C23");
12736 : 47632 : return specs;
12737 : 125 : case RID_FRACT:
12738 : 125 : case RID_ACCUM:
12739 : 125 : {
12740 : 125 : const char *str;
12741 : 125 : if (i == RID_FRACT)
12742 : : str = "_Fract";
12743 : : else
12744 : 62 : str = "_Accum";
12745 : 125 : if (specs->complex_p)
12746 : 0 : error_at (loc,
12747 : : ("both %<complex%> and %qs in "
12748 : : "declaration specifiers"),
12749 : : str);
12750 : 125 : else if (i == RID_FRACT)
12751 : 63 : specs->typespec_word = cts_fract;
12752 : : else
12753 : 62 : specs->typespec_word = cts_accum;
12754 : 125 : specs->locations[cdw_typespec] = loc;
12755 : : }
12756 : 125 : if (!targetm.fixed_point_supported_p ())
12757 : 125 : error_at (loc,
12758 : : "fixed-point types not supported for this target");
12759 : 125 : pedwarn (loc, OPT_Wpedantic,
12760 : : "ISO C does not support fixed-point types");
12761 : 125 : return specs;
12762 : 45549 : case RID_BITINT:
12763 : 45549 : if (specs->long_p)
12764 : 2 : error_at (loc,
12765 : : ("both %<long%> and %<_BitInt%> in "
12766 : : "declaration specifiers"));
12767 : 45547 : else if (specs->short_p)
12768 : 1 : error_at (loc,
12769 : : ("both %<short%> and %<_BitInt%> in "
12770 : : "declaration specifiers"));
12771 : 45546 : else if (specs->complex_p)
12772 : 1 : error_at (loc,
12773 : : ("both %<complex%> and %<_BitInt%> in "
12774 : : "declaration specifiers"));
12775 : 45545 : else if (specs->saturating_p)
12776 : 0 : error_at (loc,
12777 : : ("both %<_Sat%> and %<_BitInt%> in "
12778 : : "declaration specifiers"));
12779 : : else
12780 : : {
12781 : 45545 : specs->typespec_word = cts_bitint;
12782 : 45545 : specs->locations[cdw_typespec] = loc;
12783 : 45545 : specs->u.bitint_prec = -1;
12784 : 45545 : if (error_operand_p (spec.expr))
12785 : 7 : return specs;
12786 : 45545 : if (TREE_CODE (spec.expr) != INTEGER_CST
12787 : 45545 : || !INTEGRAL_TYPE_P (TREE_TYPE (spec.expr)))
12788 : : {
12789 : 1 : error_at (loc, "%<_BitInt%> argument is not an integer "
12790 : : "constant expression");
12791 : 1 : return specs;
12792 : : }
12793 : 45544 : if (tree_int_cst_sgn (spec.expr) <= 0)
12794 : : {
12795 : 4 : error_at (loc, "%<_BitInt%> argument %qE is not a "
12796 : : "positive integer constant expression",
12797 : : spec.expr);
12798 : 4 : return specs;
12799 : : }
12800 : 45540 : if (wi::to_widest (spec.expr) > WIDE_INT_MAX_PRECISION - 1)
12801 : : {
12802 : 2 : error_at (loc, "%<_BitInt%> argument %qE is larger than "
12803 : : "%<BITINT_MAXWIDTH%> %qd",
12804 : : spec.expr, (int) WIDE_INT_MAX_PRECISION - 1);
12805 : 2 : return specs;
12806 : : }
12807 : 45538 : specs->u.bitint_prec = tree_to_uhwi (spec.expr);
12808 : 45538 : struct bitint_info info;
12809 : 45538 : if (!targetm.c.bitint_type_info (specs->u.bitint_prec,
12810 : : &info))
12811 : : {
12812 : 0 : sorry_at (loc, "%<_BitInt(%d)%> is not supported on "
12813 : : "this target", specs->u.bitint_prec);
12814 : 0 : specs->u.bitint_prec = -1;
12815 : 0 : return specs;
12816 : : }
12817 : : }
12818 : 45542 : return specs;
12819 : : default:
12820 : : /* ObjC reserved word "id", handled below. */
12821 : : break;
12822 : : }
12823 : : }
12824 : : }
12825 : :
12826 : : /* Now we have a typedef (a TYPE_DECL node), an identifier (some
12827 : : form of ObjC type, cases such as "int" and "long" being handled
12828 : : above), a TYPE (struct, union, enum and typeof specifiers) or an
12829 : : ERROR_MARK. In none of these cases may there have previously
12830 : : been any type specifiers. */
12831 : 242790205 : if (specs->type || specs->typespec_word != cts_none
12832 : 242790193 : || specs->long_p || specs->short_p || specs->signed_p
12833 : 242790190 : || specs->unsigned_p || specs->complex_p)
12834 : 21 : error_at (loc, "two or more data types in declaration specifiers");
12835 : 242790184 : else if (TREE_CODE (type) == TYPE_DECL)
12836 : : {
12837 : 239574582 : specs->type = TREE_TYPE (type);
12838 : 239574582 : if (TREE_TYPE (type) != error_mark_node)
12839 : : {
12840 : 239574569 : specs->decl_attr = DECL_ATTRIBUTES (type);
12841 : 239574569 : specs->typedef_p = true;
12842 : 239574569 : specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
12843 : 239574569 : specs->locations[cdw_typedef] = loc;
12844 : :
12845 : : /* If this typedef name is defined in a struct, then a C++
12846 : : lookup would return a different value. */
12847 : 239574569 : if (warn_cxx_compat
12848 : 239574569 : && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
12849 : 2 : warning_at (loc, OPT_Wc___compat,
12850 : : "C++ lookup of %qD would return a field, not a type",
12851 : : type);
12852 : :
12853 : : /* If we are parsing a struct, record that a struct field
12854 : : used a typedef. */
12855 : 239574569 : if (warn_cxx_compat && struct_parse_info != NULL)
12856 : 1751 : struct_parse_info->typedefs_seen.safe_push (type);
12857 : : }
12858 : : }
12859 : 3215602 : else if (TREE_CODE (type) == IDENTIFIER_NODE)
12860 : : {
12861 : 0 : tree t = lookup_name (type);
12862 : 0 : if (!t || TREE_CODE (t) != TYPE_DECL)
12863 : 0 : error_at (loc, "%qE fails to be a typedef or built in type", type);
12864 : 0 : else if (TREE_TYPE (t) == error_mark_node)
12865 : : ;
12866 : : else
12867 : : {
12868 : 0 : specs->type = TREE_TYPE (t);
12869 : 0 : specs->locations[cdw_typespec] = loc;
12870 : : }
12871 : : }
12872 : : else
12873 : : {
12874 : 3215602 : if (TREE_CODE (type) != ERROR_MARK)
12875 : : {
12876 : 3215396 : if (spec.kind == ctsk_typeof)
12877 : : {
12878 : 819748 : specs->typedef_p = true;
12879 : 819748 : specs->locations[cdw_typedef] = loc;
12880 : : }
12881 : 3215396 : if (spec.expr)
12882 : : {
12883 : 907 : if (specs->expr)
12884 : 0 : specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
12885 : : specs->expr, spec.expr);
12886 : : else
12887 : 907 : specs->expr = spec.expr;
12888 : 907 : specs->expr_const_operands &= spec.expr_const_operands;
12889 : : }
12890 : : }
12891 : 3215602 : specs->type = type;
12892 : 3215602 : if (spec.has_enum_type_specifier
12893 : 168 : && spec.kind != ctsk_tagdef)
12894 : 49 : specs->enum_type_specifier_ref_p = true;
12895 : : }
12896 : :
12897 : : return specs;
12898 : : }
12899 : :
12900 : : /* Add the storage class specifier or function specifier SCSPEC to the
12901 : : declaration specifiers SPECS, returning SPECS. */
12902 : :
12903 : : struct c_declspecs *
12904 : 88232414 : declspecs_add_scspec (location_t loc,
12905 : : struct c_declspecs *specs,
12906 : : tree scspec)
12907 : : {
12908 : 88232414 : enum rid i;
12909 : 88232414 : enum c_storage_class n = csc_none;
12910 : 88232414 : bool dupe = false;
12911 : 88232414 : specs->declspecs_seen_p = true;
12912 : 88232414 : specs->non_std_attrs_seen_p = true;
12913 : 88232414 : gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
12914 : : && C_IS_RESERVED_WORD (scspec));
12915 : 88232414 : i = C_RID_CODE (scspec);
12916 : 88232414 : if (specs->non_sc_seen_p)
12917 : 2074 : warning (OPT_Wold_style_declaration,
12918 : : "%qE is not at beginning of declaration", scspec);
12919 : 88232414 : switch (i)
12920 : : {
12921 : 34706157 : case RID_INLINE:
12922 : : /* C99 permits duplicate inline. Although of doubtful utility,
12923 : : it seems simplest to permit it in gnu89 mode as well, as
12924 : : there is also little utility in maintaining this as a
12925 : : difference between gnu89 and C99 inline. */
12926 : 34706157 : dupe = false;
12927 : 34706157 : specs->inline_p = true;
12928 : 34706157 : specs->locations[cdw_inline] = loc;
12929 : 34706157 : break;
12930 : 22965 : case RID_NORETURN:
12931 : : /* Duplicate _Noreturn is permitted. */
12932 : 22965 : dupe = false;
12933 : 22965 : specs->noreturn_p = true;
12934 : 22965 : specs->locations[cdw_noreturn] = loc;
12935 : 22965 : break;
12936 : 2866 : case RID_THREAD:
12937 : 2866 : dupe = specs->thread_p;
12938 : 2866 : if (specs->storage_class == csc_auto)
12939 : 2 : error ("%qE used with %<auto%>", scspec);
12940 : 2864 : else if (specs->storage_class == csc_register)
12941 : 3 : error ("%qE used with %<register%>", scspec);
12942 : 2861 : else if (specs->storage_class == csc_typedef)
12943 : 2 : error ("%qE used with %<typedef%>", scspec);
12944 : 2859 : else if (specs->constexpr_p)
12945 : 2 : error ("%qE used with %<constexpr%>", scspec);
12946 : : else
12947 : : {
12948 : 2857 : specs->thread_p = true;
12949 : 2857 : specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
12950 : 2857 : "__thread") == 0);
12951 : : /* A diagnostic is not required for the use of this
12952 : : identifier in the implementation namespace; only diagnose
12953 : : it for the C11 spelling because of existing code using
12954 : : the other spelling. */
12955 : 2857 : if (!specs->thread_gnu_p)
12956 : : {
12957 : 110 : if (flag_isoc99)
12958 : 107 : pedwarn_c99 (loc, OPT_Wpedantic,
12959 : : "ISO C99 does not support %qE", scspec);
12960 : : else
12961 : 3 : pedwarn_c99 (loc, OPT_Wpedantic,
12962 : : "ISO C90 does not support %qE", scspec);
12963 : : }
12964 : 2857 : specs->locations[cdw_thread] = loc;
12965 : : }
12966 : : break;
12967 : 259 : case RID_AUTO:
12968 : 259 : if (flag_isoc23
12969 : 220 : && specs->typespec_kind == ctsk_none
12970 : 209 : && specs->storage_class != csc_typedef)
12971 : : {
12972 : : /* "auto" potentially used for type deduction. */
12973 : 208 : if (specs->c23_auto_p)
12974 : 2 : error ("duplicate %qE", scspec);
12975 : 208 : specs->c23_auto_p = true;
12976 : 208 : return specs;
12977 : : }
12978 : 51 : n = csc_auto;
12979 : : /* auto may only be used with another storage class specifier,
12980 : : such as constexpr, if the type is inferred. */
12981 : 51 : if (specs->constexpr_p)
12982 : 2 : error ("%qE used with %<constexpr%>", scspec);
12983 : : break;
12984 : 48858865 : case RID_EXTERN:
12985 : 48858865 : n = csc_extern;
12986 : : /* Diagnose "__thread extern". */
12987 : 48858865 : if (specs->thread_p && specs->thread_gnu_p)
12988 : 2 : error ("%<__thread%> before %<extern%>");
12989 : : break;
12990 : : case RID_REGISTER:
12991 : : n = csc_register;
12992 : : break;
12993 : 413028 : case RID_STATIC:
12994 : 413028 : n = csc_static;
12995 : : /* Diagnose "__thread static". */
12996 : 413028 : if (specs->thread_p && specs->thread_gnu_p)
12997 : 1 : error ("%<__thread%> before %<static%>");
12998 : : break;
12999 : 4224572 : case RID_TYPEDEF:
13000 : 4224572 : n = csc_typedef;
13001 : 4224572 : if (specs->c23_auto_p)
13002 : : {
13003 : 1 : error ("%<typedef%> used with %<auto%>");
13004 : 1 : specs->c23_auto_p = false;
13005 : : }
13006 : : break;
13007 : 604 : case RID_CONSTEXPR:
13008 : 604 : dupe = specs->constexpr_p;
13009 : 604 : if (specs->storage_class == csc_extern)
13010 : 1 : error ("%qE used with %<extern%>", scspec);
13011 : 603 : else if (specs->storage_class == csc_typedef)
13012 : 1 : error ("%qE used with %<typedef%>", scspec);
13013 : 602 : else if (specs->storage_class == csc_auto)
13014 : : /* auto may only be used with another storage class specifier,
13015 : : such as constexpr, if the type is inferred. */
13016 : 2 : error ("%qE used with %<auto%>", scspec);
13017 : 600 : else if (specs->thread_p)
13018 : 4 : error ("%qE used with %qs", scspec,
13019 : 2 : specs->thread_gnu_p ? "__thread" : "_Thread_local");
13020 : : else
13021 : 598 : specs->constexpr_p = true;
13022 : : break;
13023 : 0 : default:
13024 : 0 : gcc_unreachable ();
13025 : : }
13026 : 88232212 : if (n != csc_none && n == specs->storage_class)
13027 : : dupe = true;
13028 : 88232198 : if (dupe)
13029 : : {
13030 : 12 : if (i == RID_THREAD)
13031 : 2 : error ("duplicate %<_Thread_local%> or %<__thread%>");
13032 : : else
13033 : 10 : error ("duplicate %qE", scspec);
13034 : : }
13035 : 88232206 : if (n != csc_none)
13036 : : {
13037 : 53499614 : if (specs->storage_class != csc_none && n != specs->storage_class)
13038 : : {
13039 : 7 : error ("multiple storage classes in declaration specifiers");
13040 : : }
13041 : : else
13042 : : {
13043 : 53499607 : specs->storage_class = n;
13044 : 53499607 : specs->locations[cdw_storage_class] = loc;
13045 : 53499607 : if (n != csc_extern && n != csc_static && specs->thread_p)
13046 : : {
13047 : 8 : error ("%qs used with %qE",
13048 : 8 : specs->thread_gnu_p ? "__thread" : "_Thread_local",
13049 : : scspec);
13050 : 8 : specs->thread_p = false;
13051 : : }
13052 : 53499607 : if (n != csc_auto && n != csc_register && n != csc_static
13053 : 53083434 : && specs->constexpr_p)
13054 : : {
13055 : 2 : error ("%<constexpr%> used with %qE", scspec);
13056 : 2 : specs->constexpr_p = false;
13057 : : }
13058 : : }
13059 : : }
13060 : : return specs;
13061 : : }
13062 : :
13063 : : /* Add the attributes ATTRS to the declaration specifiers SPECS,
13064 : : returning SPECS. */
13065 : :
13066 : : struct c_declspecs *
13067 : 35008856 : declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
13068 : : {
13069 : 35008856 : specs->attrs = chainon (attrs, specs->attrs);
13070 : 35008856 : specs->locations[cdw_attributes] = loc;
13071 : 35008856 : specs->declspecs_seen_p = true;
13072 : : /* In the case of standard attributes at the start of the
13073 : : declaration, the caller will reset this. */
13074 : 35008856 : specs->non_std_attrs_seen_p = true;
13075 : 35008856 : return specs;
13076 : : }
13077 : :
13078 : : /* Add an _Alignas specifier (expression ALIGN, or type whose
13079 : : alignment is ALIGN) to the declaration specifiers SPECS, returning
13080 : : SPECS. */
13081 : : struct c_declspecs *
13082 : 200 : declspecs_add_alignas (location_t loc,
13083 : : struct c_declspecs *specs, tree align)
13084 : : {
13085 : 200 : specs->alignas_p = true;
13086 : 200 : specs->locations[cdw_alignas] = loc;
13087 : 200 : if (align == error_mark_node)
13088 : : return specs;
13089 : :
13090 : : /* Only accept the alignment if it's valid and greater than
13091 : : the current one. Zero is invalid but by C11 required to
13092 : : be silently ignored. */
13093 : 198 : int align_log = check_user_alignment (align, false, /* warn_zero = */false);
13094 : 198 : if (align_log > specs->align_log)
13095 : 165 : specs->align_log = align_log;
13096 : : return specs;
13097 : : }
13098 : :
13099 : : /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
13100 : : specifiers with any other type specifier to determine the resulting
13101 : : type. This is where ISO C checks on complex types are made, since
13102 : : "_Complex long" is a prefix of the valid ISO C type "_Complex long
13103 : : double". Also apply postfix standard attributes to modify the type. */
13104 : :
13105 : : struct c_declspecs *
13106 : 306604503 : finish_declspecs (struct c_declspecs *specs)
13107 : : {
13108 : : /* If a type was specified as a whole, we have no modifiers and are
13109 : : done. */
13110 : 306604503 : if (specs->type != NULL_TREE)
13111 : : {
13112 : 242790138 : gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
13113 : : && !specs->signed_p && !specs->unsigned_p
13114 : : && !specs->complex_p && !specs->c23_auto_p);
13115 : :
13116 : : /* Set a dummy type. */
13117 : 242790138 : if (TREE_CODE (specs->type) == ERROR_MARK)
13118 : 182 : specs->type = integer_type_node;
13119 : 242790138 : goto handle_postfix_attrs;
13120 : : }
13121 : :
13122 : : /* If none of "void", "_Bool", "char", "int", "float" or "double"
13123 : : has been specified, treat it as "int" unless "_Complex" is
13124 : : present and there are no other specifiers. If we just have
13125 : : "_Complex", it is equivalent to "_Complex double", but e.g.
13126 : : "_Complex short" is equivalent to "_Complex short int". */
13127 : 63814365 : if (specs->typespec_word == cts_none)
13128 : : {
13129 : 4099381 : if (specs->saturating_p)
13130 : : {
13131 : 1 : error_at (specs->locations[cdw_saturating],
13132 : : "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
13133 : 1 : if (!targetm.fixed_point_supported_p ())
13134 : 1 : error_at (specs->locations[cdw_saturating],
13135 : : "fixed-point types not supported for this target");
13136 : 1 : specs->typespec_word = cts_fract;
13137 : : }
13138 : 4099380 : else if (specs->long_p || specs->short_p
13139 : 189816 : || specs->signed_p || specs->unsigned_p)
13140 : : {
13141 : 4088947 : specs->typespec_word = cts_int;
13142 : : }
13143 : 10433 : else if (specs->complex_p)
13144 : : {
13145 : 130 : specs->typespec_word = cts_double;
13146 : 130 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13147 : : "ISO C does not support plain %<complex%> meaning "
13148 : : "%<double complex%>");
13149 : : }
13150 : 10303 : else if (specs->c23_auto_p)
13151 : : {
13152 : : /* Type to be filled in later, including applying postfix
13153 : : attributes. This warning only actually appears for
13154 : : -Wc11-c23-compat in C23 mode; in older modes, there may
13155 : : be a warning or pedwarn for implicit "int" instead, or
13156 : : other errors for use of auto at file scope. */
13157 : 93 : pedwarn_c11 (input_location, OPT_Wpedantic,
13158 : : "ISO C does not support %<auto%> type deduction "
13159 : : "before C23");
13160 : 93 : return specs;
13161 : : }
13162 : : else
13163 : : {
13164 : 10210 : specs->typespec_word = cts_int;
13165 : 10210 : specs->default_int_p = true;
13166 : : /* We don't diagnose this here because grokdeclarator will
13167 : : give more specific diagnostics according to whether it is
13168 : : a function definition. */
13169 : : }
13170 : : }
13171 : :
13172 : : /* If "signed" was specified, record this to distinguish "int" and
13173 : : "signed int" in the case of a bit-field with
13174 : : -funsigned-bitfields. */
13175 : 63814272 : specs->explicit_signed_p = specs->signed_p;
13176 : :
13177 : : /* Now compute the actual type. */
13178 : 63814272 : gcc_assert (!specs->c23_auto_p);
13179 : 63814272 : switch (specs->typespec_word)
13180 : : {
13181 : 1833 : case cts_auto_type:
13182 : 1833 : gcc_assert (!specs->long_p && !specs->short_p
13183 : : && !specs->signed_p && !specs->unsigned_p
13184 : : && !specs->complex_p);
13185 : : /* Type to be filled in later. */
13186 : 1833 : if (specs->postfix_attrs)
13187 : 2 : error ("%<__auto_type%> followed by %<[[]]%> attributes");
13188 : : break;
13189 : 7678893 : case cts_void:
13190 : 7678893 : gcc_assert (!specs->long_p && !specs->short_p
13191 : : && !specs->signed_p && !specs->unsigned_p
13192 : : && !specs->complex_p);
13193 : 7678893 : specs->type = void_type_node;
13194 : 7678893 : break;
13195 : 82458 : case cts_bool:
13196 : 82458 : gcc_assert (!specs->long_p && !specs->short_p
13197 : : && !specs->signed_p && !specs->unsigned_p
13198 : : && !specs->complex_p);
13199 : 82458 : specs->type = boolean_type_node;
13200 : 82458 : break;
13201 : 8941149 : case cts_char:
13202 : 8941149 : gcc_assert (!specs->long_p && !specs->short_p);
13203 : 8941149 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13204 : 8941149 : if (specs->signed_p)
13205 : 188773 : specs->type = signed_char_type_node;
13206 : 8752376 : else if (specs->unsigned_p)
13207 : 877013 : specs->type = unsigned_char_type_node;
13208 : : else
13209 : 7875363 : specs->type = char_type_node;
13210 : 8941149 : if (specs->complex_p)
13211 : : {
13212 : 3853 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13213 : : "ISO C does not support complex integer types");
13214 : 3853 : specs->type = build_complex_type (specs->type);
13215 : : }
13216 : : break;
13217 : 51284 : case cts_int_n:
13218 : 51284 : gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
13219 : 51284 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13220 : 51284 : if (! int_n_enabled_p[specs->u.int_n_idx])
13221 : 0 : specs->type = integer_type_node;
13222 : : else
13223 : 51284 : specs->type = (specs->unsigned_p
13224 : 51284 : ? int_n_trees[specs->u.int_n_idx].unsigned_type
13225 : : : int_n_trees[specs->u.int_n_idx].signed_type);
13226 : 51284 : if (specs->complex_p)
13227 : : {
13228 : 199 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13229 : : "ISO C does not support complex integer types");
13230 : 199 : specs->type = build_complex_type (specs->type);
13231 : : }
13232 : : break;
13233 : 27472647 : case cts_int:
13234 : 27472647 : gcc_assert (!(specs->long_p && specs->short_p));
13235 : 27472647 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13236 : 27472647 : if (specs->long_long_p)
13237 : 2890002 : specs->type = (specs->unsigned_p
13238 : 2890002 : ? long_long_unsigned_type_node
13239 : : : long_long_integer_type_node);
13240 : 24582645 : else if (specs->long_p)
13241 : 2164156 : specs->type = (specs->unsigned_p
13242 : 2164156 : ? long_unsigned_type_node
13243 : : : long_integer_type_node);
13244 : 22418489 : else if (specs->short_p)
13245 : 1665543 : specs->type = (specs->unsigned_p
13246 : 1665543 : ? short_unsigned_type_node
13247 : : : short_integer_type_node);
13248 : : else
13249 : 20752946 : specs->type = (specs->unsigned_p
13250 : 20752946 : ? unsigned_type_node
13251 : : : integer_type_node);
13252 : 27472647 : if (specs->complex_p)
13253 : : {
13254 : 14910 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13255 : : "ISO C does not support complex integer types");
13256 : 14910 : specs->type = build_complex_type (specs->type);
13257 : : }
13258 : : break;
13259 : 3336651 : case cts_float:
13260 : 3336651 : gcc_assert (!specs->long_p && !specs->short_p
13261 : : && !specs->signed_p && !specs->unsigned_p);
13262 : 6673302 : specs->type = (specs->complex_p
13263 : 3336651 : ? complex_float_type_node
13264 : : : float_type_node);
13265 : 3336651 : break;
13266 : 6015597 : case cts_double:
13267 : 6015597 : gcc_assert (!specs->long_long_p && !specs->short_p
13268 : : && !specs->signed_p && !specs->unsigned_p);
13269 : 6015597 : if (specs->long_p)
13270 : : {
13271 : 2588601 : specs->type = (specs->complex_p
13272 : 2588601 : ? complex_long_double_type_node
13273 : : : long_double_type_node);
13274 : : }
13275 : : else
13276 : : {
13277 : 3426996 : specs->type = (specs->complex_p
13278 : 3426996 : ? complex_double_type_node
13279 : : : double_type_node);
13280 : : }
13281 : : break;
13282 : 10140520 : case cts_floatn_nx:
13283 : 10140520 : gcc_assert (!specs->long_p && !specs->short_p
13284 : : && !specs->signed_p && !specs->unsigned_p);
13285 : 10140520 : if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
13286 : 88 : specs->type = integer_type_node;
13287 : 10140432 : else if (specs->complex_p)
13288 : 1535829 : specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13289 : : else
13290 : 8604603 : specs->type = FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13291 : : break;
13292 : 47569 : case cts_dfloat32:
13293 : 47569 : case cts_dfloat64:
13294 : 47569 : case cts_dfloat128:
13295 : 47569 : case cts_dfloat64x:
13296 : 47569 : gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
13297 : : && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
13298 : 47569 : if (!targetm.decimal_float_supported_p ())
13299 : 0 : specs->type = integer_type_node;
13300 : 47569 : else if (specs->typespec_word == cts_dfloat32)
13301 : 15957 : specs->type = dfloat32_type_node;
13302 : 31612 : else if (specs->typespec_word == cts_dfloat64)
13303 : 15832 : specs->type = dfloat64_type_node;
13304 : 15780 : else if (specs->typespec_word == cts_dfloat128)
13305 : 15733 : specs->type = dfloat128_type_node;
13306 : : else
13307 : 47 : specs->type = dfloat64x_type_node;
13308 : : break;
13309 : 64 : case cts_fract:
13310 : 64 : gcc_assert (!specs->complex_p);
13311 : 64 : if (!targetm.fixed_point_supported_p ())
13312 : 64 : specs->type = integer_type_node;
13313 : 0 : else if (specs->saturating_p)
13314 : : {
13315 : 0 : if (specs->long_long_p)
13316 : 0 : specs->type = specs->unsigned_p
13317 : 0 : ? sat_unsigned_long_long_fract_type_node
13318 : : : sat_long_long_fract_type_node;
13319 : 0 : else if (specs->long_p)
13320 : 0 : specs->type = specs->unsigned_p
13321 : 0 : ? sat_unsigned_long_fract_type_node
13322 : : : sat_long_fract_type_node;
13323 : 0 : else if (specs->short_p)
13324 : 0 : specs->type = specs->unsigned_p
13325 : 0 : ? sat_unsigned_short_fract_type_node
13326 : : : sat_short_fract_type_node;
13327 : : else
13328 : 0 : specs->type = specs->unsigned_p
13329 : 0 : ? sat_unsigned_fract_type_node
13330 : : : sat_fract_type_node;
13331 : : }
13332 : : else
13333 : : {
13334 : 0 : if (specs->long_long_p)
13335 : 0 : specs->type = specs->unsigned_p
13336 : 0 : ? unsigned_long_long_fract_type_node
13337 : : : long_long_fract_type_node;
13338 : 0 : else if (specs->long_p)
13339 : 0 : specs->type = specs->unsigned_p
13340 : 0 : ? unsigned_long_fract_type_node
13341 : : : long_fract_type_node;
13342 : 0 : else if (specs->short_p)
13343 : 0 : specs->type = specs->unsigned_p
13344 : 0 : ? unsigned_short_fract_type_node
13345 : : : short_fract_type_node;
13346 : : else
13347 : 0 : specs->type = specs->unsigned_p
13348 : 0 : ? unsigned_fract_type_node
13349 : : : fract_type_node;
13350 : : }
13351 : : break;
13352 : 62 : case cts_accum:
13353 : 62 : gcc_assert (!specs->complex_p);
13354 : 62 : if (!targetm.fixed_point_supported_p ())
13355 : 62 : specs->type = integer_type_node;
13356 : 0 : else if (specs->saturating_p)
13357 : : {
13358 : 0 : if (specs->long_long_p)
13359 : 0 : specs->type = specs->unsigned_p
13360 : 0 : ? sat_unsigned_long_long_accum_type_node
13361 : : : sat_long_long_accum_type_node;
13362 : 0 : else if (specs->long_p)
13363 : 0 : specs->type = specs->unsigned_p
13364 : 0 : ? sat_unsigned_long_accum_type_node
13365 : : : sat_long_accum_type_node;
13366 : 0 : else if (specs->short_p)
13367 : 0 : specs->type = specs->unsigned_p
13368 : 0 : ? sat_unsigned_short_accum_type_node
13369 : : : sat_short_accum_type_node;
13370 : : else
13371 : 0 : specs->type = specs->unsigned_p
13372 : 0 : ? sat_unsigned_accum_type_node
13373 : : : sat_accum_type_node;
13374 : : }
13375 : : else
13376 : : {
13377 : 0 : if (specs->long_long_p)
13378 : 0 : specs->type = specs->unsigned_p
13379 : 0 : ? unsigned_long_long_accum_type_node
13380 : : : long_long_accum_type_node;
13381 : 0 : else if (specs->long_p)
13382 : 0 : specs->type = specs->unsigned_p
13383 : 0 : ? unsigned_long_accum_type_node
13384 : : : long_accum_type_node;
13385 : 0 : else if (specs->short_p)
13386 : 0 : specs->type = specs->unsigned_p
13387 : 0 : ? unsigned_short_accum_type_node
13388 : : : short_accum_type_node;
13389 : : else
13390 : 0 : specs->type = specs->unsigned_p
13391 : 0 : ? unsigned_accum_type_node
13392 : : : accum_type_node;
13393 : : }
13394 : : break;
13395 : 45545 : case cts_bitint:
13396 : 45545 : gcc_assert (!specs->long_p && !specs->short_p
13397 : : && !specs->complex_p);
13398 : 45545 : if (!specs->unsigned_p && specs->u.bitint_prec == 1)
13399 : : {
13400 : 2 : error_at (specs->locations[cdw_typespec],
13401 : : "%<signed _BitInt%> argument must be at least 2");
13402 : 2 : specs->type = integer_type_node;
13403 : 2 : break;
13404 : : }
13405 : 45543 : if (specs->u.bitint_prec == -1)
13406 : 7 : specs->type = integer_type_node;
13407 : : else
13408 : : {
13409 : 67128 : pedwarn_c11 (specs->locations[cdw_typespec], OPT_Wpedantic,
13410 : : "ISO C does not support %<%s_BitInt(%d)%> before C23",
13411 : : specs->unsigned_p ? "unsigned "
13412 : 21592 : : specs->signed_p ? "signed " : "",
13413 : : specs->u.bitint_prec);
13414 : 45536 : specs->type = build_bitint_type (specs->u.bitint_prec,
13415 : 45536 : specs->unsigned_p);
13416 : : }
13417 : : break;
13418 : 0 : default:
13419 : 0 : gcc_unreachable ();
13420 : : }
13421 : 306604410 : handle_postfix_attrs:
13422 : 306604410 : if (specs->type != NULL)
13423 : : {
13424 : 306602577 : specs->postfix_attrs
13425 : 306602577 : = c_warn_type_attributes (specs->type, specs->postfix_attrs);
13426 : 306602577 : decl_attributes (&specs->type, specs->postfix_attrs, 0);
13427 : 306602577 : specs->postfix_attrs = NULL_TREE;
13428 : : }
13429 : :
13430 : : return specs;
13431 : : }
13432 : :
13433 : : /* Perform final processing on one file scope's declarations (or the
13434 : : external scope's declarations), GLOBALS. */
13435 : :
13436 : : static void
13437 : 213410 : c_write_global_declarations_1 (tree globals)
13438 : : {
13439 : 213410 : tree decl;
13440 : 213410 : bool reconsider;
13441 : :
13442 : : /* Process the decls in the order they were written. */
13443 : 387451727 : for (decl = globals; decl; decl = DECL_CHAIN (decl))
13444 : : {
13445 : : /* Check for used but undefined static functions using the C
13446 : : standard's definition of "used", and set TREE_NO_WARNING so
13447 : : that check_global_declaration doesn't repeat the check. */
13448 : 387238317 : if (TREE_CODE (decl) == FUNCTION_DECL
13449 : 369316143 : && DECL_INITIAL (decl) == NULL_TREE
13450 : 333935464 : && DECL_EXTERNAL (decl)
13451 : 721173776 : && !TREE_PUBLIC (decl))
13452 : : {
13453 : 94 : if (C_DECL_USED (decl))
13454 : : {
13455 : : /* TODO: Add OPT_Wundefined-inline. */
13456 : 20 : if (pedwarn (input_location, 0, "%q+F used but never defined",
13457 : : decl))
13458 : 19 : suppress_warning (decl /* OPT_Wundefined-inline. */);
13459 : : }
13460 : : /* For -Wunused-function warn about unused static prototypes. */
13461 : 74 : else if (warn_unused_function
13462 : 2 : && ! DECL_ARTIFICIAL (decl)
13463 : 76 : && ! warning_suppressed_p (decl, OPT_Wunused_function))
13464 : : {
13465 : 2 : if (warning (OPT_Wunused_function,
13466 : : "%q+F declared %<static%> but never defined",
13467 : : decl))
13468 : 2 : suppress_warning (decl, OPT_Wunused_function);
13469 : : }
13470 : : }
13471 : :
13472 : 387238317 : wrapup_global_declaration_1 (decl);
13473 : : }
13474 : :
13475 : 246929 : do
13476 : : {
13477 : 246929 : reconsider = false;
13478 : 499324014 : for (decl = globals; decl; decl = DECL_CHAIN (decl))
13479 : 499077085 : reconsider |= wrapup_global_declaration_2 (decl);
13480 : : }
13481 : : while (reconsider);
13482 : 213410 : }
13483 : :
13484 : : /* Preserve the external declarations scope across a garbage collect. */
13485 : : static GTY(()) tree ext_block;
13486 : :
13487 : : /* Collect all references relevant to SOURCE_FILE. */
13488 : :
13489 : : static void
13490 : 15 : collect_all_refs (const char *source_file)
13491 : : {
13492 : 15 : tree t;
13493 : 15 : unsigned i;
13494 : :
13495 : 30 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13496 : 15 : collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
13497 : :
13498 : 15 : collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
13499 : 15 : }
13500 : :
13501 : : /* Collect source file references at global level. */
13502 : :
13503 : : static void
13504 : 15 : collect_source_refs (void)
13505 : : {
13506 : 15 : tree t;
13507 : 15 : tree decls;
13508 : 15 : tree decl;
13509 : 15 : unsigned i;
13510 : :
13511 : 30 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13512 : : {
13513 : 15 : decls = DECL_INITIAL (t);
13514 : 82 : for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
13515 : 67 : if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13516 : 67 : collect_source_ref (DECL_SOURCE_FILE (decl));
13517 : : }
13518 : :
13519 : 44171 : for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
13520 : 44156 : if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13521 : 11 : collect_source_ref (DECL_SOURCE_FILE (decl));
13522 : 15 : }
13523 : :
13524 : : /* Free attribute access data that are not needed by the middle end. */
13525 : :
13526 : : static void
13527 : 106705 : free_attr_access_data ()
13528 : : {
13529 : 106705 : struct cgraph_node *n;
13530 : :
13531 : : /* Iterate over all functions declared in the translation unit. */
13532 : 70977922 : FOR_EACH_FUNCTION (n)
13533 : : {
13534 : 132893110 : for (tree parm = DECL_ARGUMENTS (n->decl); parm; parm = TREE_CHAIN (parm))
13535 : 97510854 : if (tree attrs = DECL_ATTRIBUTES (parm))
13536 : 95608 : attr_access::free_lang_data (attrs);
13537 : :
13538 : 35382256 : tree fntype = TREE_TYPE (n->decl);
13539 : 35382256 : if (!fntype || fntype == error_mark_node)
13540 : 0 : continue;
13541 : 35382256 : tree attrs = TYPE_ATTRIBUTES (fntype);
13542 : 35382256 : if (!attrs)
13543 : 35150532 : continue;
13544 : :
13545 : 231724 : attr_access::free_lang_data (attrs);
13546 : : }
13547 : 106705 : }
13548 : :
13549 : : /* Perform any final parser cleanups and generate initial debugging
13550 : : information. */
13551 : :
13552 : : void
13553 : 107067 : c_parse_final_cleanups (void)
13554 : : {
13555 : 107067 : tree t;
13556 : 107067 : unsigned i;
13557 : :
13558 : : /* We don't want to do this if generating a PCH. */
13559 : 107067 : if (pch_file)
13560 : 107067 : return;
13561 : :
13562 : 106705 : timevar_stop (TV_PHASE_PARSING);
13563 : 106705 : timevar_start (TV_PHASE_DEFERRED);
13564 : :
13565 : : /* Do the Objective-C stuff. This is where all the Objective-C
13566 : : module stuff gets generated (symtab, class/protocol/selector
13567 : : lists etc). */
13568 : 106705 : if (c_dialect_objc ())
13569 : 0 : objc_write_global_declarations ();
13570 : :
13571 : : /* Close the external scope. */
13572 : 106705 : ext_block = pop_scope ();
13573 : 106705 : external_scope = 0;
13574 : 106705 : gcc_assert (!current_scope);
13575 : :
13576 : : /* Handle -fdump-ada-spec[-slim]. */
13577 : 106705 : if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
13578 : : {
13579 : : /* Build a table of files to generate specs for */
13580 : 15 : collect_source_ref (main_input_filename);
13581 : 15 : if (!flag_dump_ada_spec_slim)
13582 : 15 : collect_source_refs ();
13583 : :
13584 : 15 : dump_ada_specs (collect_all_refs, NULL);
13585 : : }
13586 : :
13587 : : /* Process all file scopes in this compilation, and the external_scope,
13588 : : through wrapup_global_declarations. */
13589 : 213410 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13590 : 106705 : c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
13591 : 106705 : c_write_global_declarations_1 (BLOCK_VARS (ext_block));
13592 : :
13593 : 106705 : if (!in_lto_p)
13594 : 106705 : free_attr_access_data ();
13595 : :
13596 : 106705 : timevar_stop (TV_PHASE_DEFERRED);
13597 : 106705 : timevar_start (TV_PHASE_PARSING);
13598 : :
13599 : 106705 : ext_block = NULL;
13600 : : }
13601 : :
13602 : : /* Register reserved keyword WORD as qualifier for address space AS. */
13603 : :
13604 : : void
13605 : 224122 : c_register_addr_space (const char *word, addr_space_t as)
13606 : : {
13607 : 224122 : int rid = RID_FIRST_ADDR_SPACE + as;
13608 : 224122 : tree id;
13609 : :
13610 : : /* Address space qualifiers are only supported
13611 : : in C with GNU extensions enabled. */
13612 : 224122 : if (c_dialect_objc () || flag_no_asm)
13613 : : return;
13614 : :
13615 : 213038 : id = get_identifier (word);
13616 : 213038 : C_SET_RID_CODE (id, rid);
13617 : 213038 : C_IS_RESERVED_WORD (id) = 1;
13618 : 213038 : ridpointers [rid] = id;
13619 : : }
13620 : :
13621 : : /* Return identifier to look up for omp declare reduction. */
13622 : :
13623 : : tree
13624 : 3250 : c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
13625 : : {
13626 : 3250 : const char *p = NULL;
13627 : 3250 : switch (reduction_code)
13628 : : {
13629 : : case PLUS_EXPR: p = "+"; break;
13630 : 267 : case MULT_EXPR: p = "*"; break;
13631 : 155 : case MINUS_EXPR: p = "-"; break;
13632 : 31 : case BIT_AND_EXPR: p = "&"; break;
13633 : 14 : case BIT_XOR_EXPR: p = "^"; break;
13634 : 92 : case BIT_IOR_EXPR: p = "|"; break;
13635 : 67 : case TRUTH_ANDIF_EXPR: p = "&&"; break;
13636 : 78 : case TRUTH_ORIF_EXPR: p = "||"; break;
13637 : 46 : case MIN_EXPR: p = "min"; break;
13638 : 83 : case MAX_EXPR: p = "max"; break;
13639 : : default:
13640 : : break;
13641 : : }
13642 : :
13643 : 833 : if (p == NULL)
13644 : : {
13645 : 213 : if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
13646 : 0 : return error_mark_node;
13647 : 213 : p = IDENTIFIER_POINTER (reduction_id);
13648 : : }
13649 : :
13650 : 3250 : const char prefix[] = "omp declare reduction ";
13651 : 3250 : size_t lenp = sizeof (prefix);
13652 : 3250 : size_t len = strlen (p);
13653 : 3250 : char *name = XALLOCAVEC (char, lenp + len);
13654 : 3250 : memcpy (name, prefix, lenp - 1);
13655 : 3250 : memcpy (name + lenp - 1, p, len + 1);
13656 : 3250 : return get_identifier (name);
13657 : : }
13658 : :
13659 : : /* Lookup REDUCTION_ID in the current scope, or create an artificial
13660 : : VAR_DECL, bind it into the current scope and return it. */
13661 : :
13662 : : tree
13663 : 161 : c_omp_reduction_decl (tree reduction_id)
13664 : : {
13665 : 161 : struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13666 : 161 : if (b != NULL && B_IN_CURRENT_SCOPE (b))
13667 : 47 : return b->decl;
13668 : :
13669 : 114 : tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
13670 : : reduction_id, integer_type_node);
13671 : 114 : DECL_ARTIFICIAL (decl) = 1;
13672 : 114 : DECL_EXTERNAL (decl) = 1;
13673 : 114 : TREE_STATIC (decl) = 1;
13674 : 114 : TREE_PUBLIC (decl) = 0;
13675 : 114 : bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
13676 : 114 : return decl;
13677 : : }
13678 : :
13679 : : /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
13680 : :
13681 : : tree
13682 : 273 : c_omp_reduction_lookup (tree reduction_id, tree type)
13683 : : {
13684 : 273 : struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13685 : 276 : while (b)
13686 : : {
13687 : 274 : tree t;
13688 : 294 : for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
13689 : 291 : if (comptypes (TREE_PURPOSE (t), type))
13690 : 271 : return TREE_VALUE (t);
13691 : 3 : b = b->shadowed;
13692 : : }
13693 : 2 : return error_mark_node;
13694 : : }
13695 : :
13696 : : /* Helper function called via walk_tree, to diagnose invalid
13697 : : #pragma omp declare reduction combiners or initializers. */
13698 : :
13699 : : tree
13700 : 1401 : c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
13701 : : {
13702 : 1401 : tree *vars = (tree *) data;
13703 : 1401 : if (SSA_VAR_P (*tp)
13704 : 413 : && !DECL_ARTIFICIAL (*tp)
13705 : 24 : && *tp != vars[0]
13706 : 24 : && *tp != vars[1])
13707 : : {
13708 : 24 : location_t loc = DECL_SOURCE_LOCATION (vars[0]);
13709 : 24 : if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
13710 : 10 : error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
13711 : : "variable %qD which is not %<omp_out%> nor %<omp_in%>",
13712 : : *tp);
13713 : : else
13714 : 14 : error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
13715 : : "to variable %qD which is not %<omp_priv%> nor "
13716 : : "%<omp_orig%>",
13717 : : *tp);
13718 : 24 : return *tp;
13719 : : }
13720 : : return NULL_TREE;
13721 : : }
13722 : :
13723 : : /* Return identifier to look up for omp declare mapper. */
13724 : :
13725 : : tree
13726 : 607 : c_omp_mapper_id (tree mapper_id)
13727 : : {
13728 : 607 : const char *p = NULL;
13729 : :
13730 : 607 : const char prefix[] = "omp declare mapper ";
13731 : :
13732 : 607 : if (mapper_id == NULL_TREE)
13733 : : p = "<default>";
13734 : 19 : else if (TREE_CODE (mapper_id) == IDENTIFIER_NODE)
13735 : 19 : p = IDENTIFIER_POINTER (mapper_id);
13736 : : else
13737 : 0 : return error_mark_node;
13738 : :
13739 : 607 : size_t lenp = sizeof (prefix);
13740 : 607 : size_t len = strlen (p);
13741 : 607 : char *name = XALLOCAVEC (char, lenp + len);
13742 : 607 : memcpy (name, prefix, lenp - 1);
13743 : 607 : memcpy (name + lenp - 1, p, len + 1);
13744 : 607 : return get_identifier (name);
13745 : : }
13746 : :
13747 : : /* Lookup MAPPER_ID in the current scope, or create an artificial
13748 : : VAR_DECL, bind it into the current scope and return it. */
13749 : :
13750 : : tree
13751 : 62 : c_omp_mapper_decl (tree mapper_id)
13752 : : {
13753 : 62 : struct c_binding *b = I_SYMBOL_BINDING (mapper_id);
13754 : 62 : if (b != NULL && B_IN_CURRENT_SCOPE (b))
13755 : 22 : return b->decl;
13756 : :
13757 : 40 : tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
13758 : : mapper_id, integer_type_node);
13759 : 40 : DECL_ARTIFICIAL (decl) = 1;
13760 : 40 : DECL_EXTERNAL (decl) = 1;
13761 : 40 : TREE_STATIC (decl) = 1;
13762 : 40 : TREE_PUBLIC (decl) = 0;
13763 : 40 : bind (mapper_id, decl, current_scope, true, false, BUILTINS_LOCATION);
13764 : 40 : return decl;
13765 : : }
13766 : :
13767 : : /* Lookup MAPPER_ID in the first scope where it has entry for TYPE. */
13768 : :
13769 : : tree
13770 : 1696 : c_omp_mapper_lookup (tree mapper_id, tree type)
13771 : : {
13772 : 1696 : if (!RECORD_OR_UNION_TYPE_P (type))
13773 : : return NULL_TREE;
13774 : :
13775 : 545 : mapper_id = c_omp_mapper_id (mapper_id);
13776 : :
13777 : 545 : struct c_binding *b = I_SYMBOL_BINDING (mapper_id);
13778 : 551 : while (b)
13779 : : {
13780 : 91 : tree t;
13781 : 110 : for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
13782 : 104 : if (comptypes (TREE_PURPOSE (t), type))
13783 : 85 : return TREE_VALUE (t);
13784 : 6 : b = b->shadowed;
13785 : : }
13786 : : return NULL_TREE;
13787 : : }
13788 : :
13789 : : /* For C, we record a pointer to the mapper itself without wrapping it in an
13790 : : artificial function or similar. So, just return it. */
13791 : :
13792 : : tree
13793 : 82 : c_omp_extract_mapper_directive (tree mapper)
13794 : : {
13795 : 82 : return mapper;
13796 : : }
13797 : :
13798 : : /* For now we can handle singleton OMP_ARRAY_SECTIONs with custom mappers, but
13799 : : nothing more complicated. */
13800 : :
13801 : : tree
13802 : 364 : c_omp_map_array_section (location_t loc, tree t)
13803 : : {
13804 : 364 : tree low = TREE_OPERAND (t, 1);
13805 : 364 : tree len = TREE_OPERAND (t, 2);
13806 : :
13807 : 364 : if (len && integer_onep (len))
13808 : : {
13809 : 63 : t = TREE_OPERAND (t, 0);
13810 : :
13811 : 63 : if (!low)
13812 : 0 : low = integer_zero_node;
13813 : :
13814 : 63 : t = build_array_ref (loc, t, low);
13815 : : }
13816 : :
13817 : 364 : return t;
13818 : : }
13819 : :
13820 : : /* Helper function for below function. */
13821 : :
13822 : : static tree
13823 : 50786 : c_omp_scan_mapper_bindings_r (tree *tp, int *walk_subtrees, void *ptr)
13824 : : {
13825 : 50786 : tree t = *tp;
13826 : 50786 : omp_mapper_list<tree> *mlist = (omp_mapper_list<tree> *) ptr;
13827 : 50786 : tree aggr_type = NULL_TREE;
13828 : :
13829 : 50786 : if (TREE_CODE (t) == SIZEOF_EXPR
13830 : 50786 : || TREE_CODE (t) == ALIGNOF_EXPR)
13831 : : {
13832 : 0 : *walk_subtrees = 0;
13833 : 0 : return NULL_TREE;
13834 : : }
13835 : :
13836 : 50786 : if (TREE_CODE (t) == OMP_CLAUSE)
13837 : : return NULL_TREE;
13838 : :
13839 : 47502 : if (TREE_CODE (t) == COMPONENT_REF
13840 : 47502 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
13841 : 356 : aggr_type = TREE_TYPE (TREE_OPERAND (t, 0));
13842 : 47146 : else if ((TREE_CODE (t) == VAR_DECL
13843 : : || TREE_CODE (t) == PARM_DECL
13844 : : || TREE_CODE (t) == RESULT_DECL)
13845 : 5254 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t)))
13846 : 116 : aggr_type = TREE_TYPE (t);
13847 : :
13848 : 472 : if (aggr_type)
13849 : : {
13850 : 472 : tree mapper_fn = c_omp_mapper_lookup (NULL_TREE, aggr_type);
13851 : 472 : if (mapper_fn)
13852 : 65 : mlist->add_mapper (NULL_TREE, aggr_type, mapper_fn);
13853 : : }
13854 : :
13855 : : return NULL_TREE;
13856 : : }
13857 : :
13858 : : /* Scan an offload region's body, and record uses of struct- or union-typed
13859 : : variables. Add _mapper_binding_ fake clauses to *CLAUSES_PTR. */
13860 : :
13861 : : void
13862 : 1890 : c_omp_scan_mapper_bindings (location_t loc, tree *clauses_ptr, tree body)
13863 : : {
13864 : 1890 : hash_set<omp_name_type<tree>> seen_types;
13865 : 1890 : auto_vec<tree> mappers;
13866 : 1890 : omp_mapper_list<tree> mlist (&seen_types, &mappers);
13867 : :
13868 : 1890 : walk_tree_without_duplicates (&body, c_omp_scan_mapper_bindings_r, &mlist);
13869 : :
13870 : 1890 : unsigned int i;
13871 : 1890 : tree mapper;
13872 : 3819 : FOR_EACH_VEC_ELT (mappers, i, mapper)
13873 : 39 : c_omp_find_nested_mappers (&mlist, mapper);
13874 : :
13875 : 1955 : FOR_EACH_VEC_ELT (mappers, i, mapper)
13876 : : {
13877 : 39 : if (mapper == error_mark_node)
13878 : 0 : continue;
13879 : 39 : tree mapper_name = OMP_DECLARE_MAPPER_ID (mapper);
13880 : 39 : tree decl = OMP_DECLARE_MAPPER_DECL (mapper);
13881 : :
13882 : 39 : tree c = build_omp_clause (loc, OMP_CLAUSE__MAPPER_BINDING_);
13883 : 39 : OMP_CLAUSE__MAPPER_BINDING__ID (c) = mapper_name;
13884 : 39 : OMP_CLAUSE__MAPPER_BINDING__DECL (c) = decl;
13885 : 39 : OMP_CLAUSE__MAPPER_BINDING__MAPPER (c) = mapper;
13886 : :
13887 : 39 : OMP_CLAUSE_CHAIN (c) = *clauses_ptr;
13888 : 39 : *clauses_ptr = c;
13889 : : }
13890 : 1890 : }
13891 : :
13892 : : bool
13893 : 223 : c_check_in_current_scope (tree decl)
13894 : : {
13895 : 223 : struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
13896 : 223 : return b != NULL && B_IN_CURRENT_SCOPE (b);
13897 : : }
13898 : :
13899 : : /* Search for loop or switch names. BEFORE_LABELS is last statement before
13900 : : possible labels and SWITCH_P true for a switch, false for loops.
13901 : : Searches through last statements in cur_stmt_list, stops when seeing
13902 : : BEFORE_LABELs, or statement other than LABEL_EXPR or CASE_LABEL_EXPR.
13903 : : Returns number of loop/switch names found and if any are found, sets
13904 : : *LAST_P to the canonical loop/switch name LABEL_DECL. */
13905 : :
13906 : : int
13907 : 498750 : c_get_loop_names (tree before_labels, bool switch_p, tree *last_p)
13908 : : {
13909 : 498750 : *last_p = NULL_TREE;
13910 : 997500 : if (!building_stmt_list_p ()
13911 : 498750 : || !STATEMENT_LIST_HAS_LABEL (cur_stmt_list)
13912 : 519246 : || before_labels == void_list_node)
13913 : 478254 : return 0;
13914 : :
13915 : 20496 : int ret = 0;
13916 : 20496 : tree last = NULL_TREE;
13917 : 20496 : for (tree_stmt_iterator tsi = tsi_last (cur_stmt_list);
13918 : 28594 : !tsi_end_p (tsi); tsi_prev (&tsi))
13919 : : {
13920 : 27748 : tree stmt = tsi_stmt (tsi);
13921 : 27748 : if (stmt == before_labels)
13922 : : break;
13923 : 8098 : else if (TREE_CODE (stmt) == LABEL_EXPR)
13924 : : {
13925 : 1087 : if (last == NULL_TREE)
13926 : 893 : last = LABEL_EXPR_LABEL (stmt);
13927 : : else
13928 : : {
13929 : 194 : loop_names.safe_push (LABEL_EXPR_LABEL (stmt));
13930 : 194 : ++ret;
13931 : : }
13932 : : }
13933 : 7011 : else if (TREE_CODE (stmt) != CASE_LABEL_EXPR
13934 : 4710 : && TREE_CODE (stmt) != DEBUG_BEGIN_STMT)
13935 : : break;
13936 : : }
13937 : 20496 : if (last)
13938 : : {
13939 : 893 : if (switch_p)
13940 : 126 : C_DECL_SWITCH_NAME (last) = 1;
13941 : : else
13942 : 767 : C_DECL_LOOP_NAME (last) = 1;
13943 : 893 : loop_names.safe_push (last);
13944 : 893 : ++ret;
13945 : 893 : if (loop_names.length () > 16)
13946 : : {
13947 : 20 : unsigned int first = 0, i;
13948 : 20 : tree l, c = NULL_TREE;
13949 : 20 : if (loop_names_hash == NULL)
13950 : 8 : loop_names_hash = new decl_tree_map (ret);
13951 : : else
13952 : 12 : first = loop_names.length () - ret;
13953 : 210 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
13954 : : {
13955 : 170 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
13956 : 48 : c = l;
13957 : 170 : gcc_checking_assert (c);
13958 : 170 : loop_names_hash->put (l, c);
13959 : 170 : if (i == first)
13960 : : break;
13961 : : }
13962 : : }
13963 : 893 : *last_p = last;
13964 : : }
13965 : : return ret;
13966 : : }
13967 : :
13968 : : /* Undoes what get_loop_names did when it returned NUM_NAMES. */
13969 : :
13970 : : void
13971 : 893 : c_release_loop_names (int num_names)
13972 : : {
13973 : 893 : unsigned len = loop_names.length () - num_names;
13974 : 893 : if (loop_names_hash)
13975 : : {
13976 : 20 : if (len <= 16)
13977 : : {
13978 : 8 : delete loop_names_hash;
13979 : 8 : loop_names_hash = NULL;
13980 : : }
13981 : : else
13982 : : {
13983 : 12 : unsigned int i;
13984 : 12 : tree l;
13985 : 42 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
13986 : : {
13987 : 30 : loop_names_hash->remove (l);
13988 : 30 : if (i == len)
13989 : : break;
13990 : : }
13991 : : }
13992 : : }
13993 : 893 : loop_names.truncate (len);
13994 : 893 : }
13995 : :
13996 : : /* Finish processing of break or continue identifier operand.
13997 : : NAME is the identifier operand of break or continue and
13998 : : IS_BREAK is true iff it is break stmt. Returns the operand
13999 : : to use for BREAK_STMT or CONTINUE_STMT, either NULL_TREE or
14000 : : canonical loop/switch name LABEL_DECL. */
14001 : :
14002 : : tree
14003 : 316 : c_finish_bc_name (location_t loc, tree name, bool is_break)
14004 : : {
14005 : 316 : tree label = NULL_TREE, lab;
14006 : 457 : pedwarn_c23 (loc, OPT_Wpedantic,
14007 : : "ISO C does not support %qs statement with an identifier "
14008 : : "operand before C2Y", is_break ? "break" : "continue");
14009 : :
14010 : : /* If I_LABEL_DECL is NULL or not from current function, don't waste time
14011 : : trying to find it among loop_names, it can't be there. */
14012 : 316 : if (!loop_names.is_empty ()
14013 : 296 : && current_function_scope
14014 : 296 : && (lab = I_LABEL_DECL (name))
14015 : 290 : && DECL_CONTEXT (lab) == current_function_decl)
14016 : : {
14017 : 280 : unsigned int i;
14018 : 280 : tree l, c = NULL_TREE;
14019 : 280 : if (loop_names_hash)
14020 : : {
14021 : 86 : if (tree *val = loop_names_hash->get (lab))
14022 : 86 : label = *val;
14023 : : }
14024 : : else
14025 : 565 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14026 : : {
14027 : 363 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14028 : : c = l;
14029 : 363 : gcc_checking_assert (c);
14030 : 363 : if (l == lab)
14031 : : {
14032 : : label = c;
14033 : : break;
14034 : : }
14035 : : }
14036 : 280 : if (label)
14037 : 272 : TREE_USED (lab) = 1;
14038 : : }
14039 : 272 : if (label == NULL_TREE)
14040 : : {
14041 : 44 : auto_vec<const char *> candidates;
14042 : 44 : unsigned int i;
14043 : 44 : tree l, c = NULL_TREE;
14044 : 115 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14045 : : {
14046 : 28 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14047 : : c = l;
14048 : 28 : gcc_checking_assert (c);
14049 : 42 : if (is_break || C_DECL_LOOP_NAME (c))
14050 : 25 : candidates.safe_push (IDENTIFIER_POINTER (DECL_NAME (l)));
14051 : : }
14052 : 44 : const char *hint = find_closest_string (IDENTIFIER_POINTER (name),
14053 : : &candidates);
14054 : 44 : if (hint)
14055 : : {
14056 : 22 : gcc_rich_location richloc (loc);
14057 : 22 : richloc.add_fixit_replace (hint);
14058 : 22 : if (is_break)
14059 : 11 : error_at (&richloc, "%<break%> statement operand %qE does not "
14060 : : "refer to a named loop or %<switch%>; "
14061 : : "did you mean %qs?", name, hint);
14062 : : else
14063 : 11 : error_at (&richloc, "%<continue%> statement operand %qE does not "
14064 : : "refer to a named loop; did you mean %qs?",
14065 : : name, hint);
14066 : 22 : }
14067 : 22 : else if (is_break)
14068 : 12 : error_at (loc, "%<break%> statement operand %qE does not refer to a "
14069 : : "named loop or %<switch%>", name);
14070 : : else
14071 : 10 : error_at (loc, "%<continue%> statement operand %qE does not refer to "
14072 : : "a named loop", name);
14073 : 44 : }
14074 : 272 : else if (!C_DECL_LOOP_NAME (label) && !is_break)
14075 : : {
14076 : 2 : auto_diagnostic_group d;
14077 : 2 : error_at (loc, "%<continue%> statement operand %qE refers to a named "
14078 : : "%<switch%>", name);
14079 : 2 : inform (DECL_SOURCE_LOCATION (label), "%<switch%> name defined here");
14080 : 2 : label = NULL_TREE;
14081 : 2 : }
14082 : 270 : else if (!C_DECL_LOOP_SWITCH_NAME_VALID (label))
14083 : : {
14084 : 18 : auto_diagnostic_group d;
14085 : 18 : if (C_DECL_LOOP_NAME (label))
14086 : : {
14087 : 16 : error_at (loc, "%qs statement operand %qE refers to a loop outside "
14088 : : "of its body", is_break ? "break" : "continue", name);
14089 : 16 : inform (DECL_SOURCE_LOCATION (label), "loop name defined here");
14090 : : }
14091 : : else
14092 : : {
14093 : 2 : error_at (loc, "%<break%> statement operand %qE refers to a "
14094 : : "%<switch%> outside of its body", name);
14095 : 2 : inform (DECL_SOURCE_LOCATION (label),
14096 : : "%<switch%> name defined here");
14097 : : }
14098 : 18 : label = NULL_TREE;
14099 : 18 : }
14100 : 252 : else if (label == loop_names.last () && (in_statement & IN_NAMED_STMT) != 0)
14101 : : /* If it is just a fancy reference to the innermost construct, handle it
14102 : : just like break; or continue; though tracking cheaply what is the
14103 : : innermost loop for continue when nested in switches would require
14104 : : another global variable and updating it. */
14105 : : label = NULL_TREE;
14106 : : else
14107 : 98 : C_DECL_LOOP_SWITCH_NAME_USED (label) = 1;
14108 : 316 : return label;
14109 : : }
14110 : :
14111 : : #include "gt-c-c-decl.h"
|