Line data Source code
1 : /* Process declarations and variables for C compiler.
2 : Copyright (C) 1988-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify it under
7 : the terms of the GNU General Public License as published by the Free
8 : Software Foundation; either version 3, or (at your option) any later
9 : version.
10 :
11 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with GCC; see the file COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 : /* Process declarations and symbol lookup for C front end.
21 : Also constructs types; the standard scalar types at initialization,
22 : and structure, union, array and enum types when they are declared. */
23 :
24 : /* ??? not all decl nodes are given the most useful possible
25 : line numbers. For example, the CONST_DECLs for enum values. */
26 :
27 : #include "config.h"
28 : #define INCLUDE_STRING
29 : #include "system.h"
30 : #include "coretypes.h"
31 : #include "target.h"
32 : #include "function.h"
33 : #include "c-tree.h"
34 : #include "timevar.h"
35 : #include "stringpool.h"
36 : #include "cgraph.h"
37 : #include "intl.h"
38 : #include "print-tree.h"
39 : #include "stor-layout.h"
40 : #include "varasm.h"
41 : #include "attribs.h"
42 : #include "toplev.h"
43 : #include "debug.h"
44 : #include "c-family/c-objc.h"
45 : #include "c-family/c-pragma.h"
46 : #include "c-family/c-ubsan.h"
47 : #include "c-lang.h"
48 : #include "langhooks.h"
49 : #include "tree-iterator.h"
50 : #include "dumpfile.h"
51 : #include "plugin.h"
52 : #include "c-family/c-ada-spec.h"
53 : #include "builtins.h"
54 : #include "spellcheck-tree.h"
55 : #include "gcc-rich-location.h"
56 : #include "asan.h"
57 : #include "c-family/name-hint.h"
58 : #include "c-family/known-headers.h"
59 : #include "c-family/c-spellcheck.h"
60 : #include "context.h" /* For 'g'. */
61 : #include "omp-general.h"
62 : #include "omp-offload.h" /* For offload_vars. */
63 : #include "c-parser.h"
64 : #include "gcc-urlifier.h"
65 :
66 : #include "tree-pretty-print.h"
67 :
68 : /* In grokdeclarator, distinguish syntactic contexts of declarators. */
69 : enum decl_context
70 : { NORMAL, /* Ordinary declaration */
71 : FUNCDEF, /* Function definition */
72 : PARM, /* Declaration of parm before function body */
73 : FIELD, /* Declaration inside struct or union */
74 : TYPENAME, /* Typename (inside cast or sizeof) */
75 : GENERIC_ASSOC }; /* Typename in generic association */
76 :
77 : /* States indicating how grokdeclarator() should handle declspecs marked
78 : with __attribute__((deprecated)) or __attribute__((unavailable)).
79 : An object declared as __attribute__((unavailable)) should suppress
80 : any reports of being declared with unavailable or deprecated items.
81 : An object declared as __attribute__((deprecated)) should suppress
82 : warnings of uses of other deprecated items. */
83 :
84 : enum deprecated_states {
85 : DEPRECATED_NORMAL,
86 : DEPRECATED_SUPPRESS,
87 : UNAVAILABLE_DEPRECATED_SUPPRESS
88 : };
89 :
90 :
91 : /* Nonzero if we have seen an invalid cross reference
92 : to a struct, union, or enum, but not yet printed the message. */
93 : tree pending_invalid_xref;
94 :
95 : /* File and line to appear in the eventual error message. */
96 : location_t pending_invalid_xref_location;
97 :
98 : /* The file and line that the prototype came from if this is an
99 : old-style definition; used for diagnostics in
100 : store_parm_decls_oldstyle. */
101 :
102 : static location_t current_function_prototype_locus;
103 :
104 : /* Whether this prototype was built-in. */
105 :
106 : static bool current_function_prototype_built_in;
107 :
108 : /* The argument type information of this prototype. */
109 :
110 : static tree current_function_prototype_arg_types;
111 :
112 : /* The argument information structure for the function currently being
113 : defined. */
114 :
115 : static struct c_arg_info *current_function_arg_info;
116 :
117 : /* The obstack on which parser and related data structures, which are
118 : not live beyond their top-level declaration or definition, are
119 : allocated. */
120 : struct obstack parser_obstack;
121 :
122 : /* The current statement tree. */
123 :
124 : static GTY(()) struct stmt_tree_s c_stmt_tree;
125 :
126 : /* Zero if we are not in an iteration or switch statement, otherwise
127 : a bitmask. See bitmask definitions in c-tree.h. */
128 : unsigned char in_statement;
129 :
130 : /* A list of decls to be made automatically visible in each file scope. */
131 : static GTY(()) tree visible_builtins;
132 :
133 : /* Set to 0 at beginning of a function definition, set to 1 if
134 : a return statement that specifies a return value is seen. */
135 :
136 : int current_function_returns_value;
137 :
138 : /* Set to 0 at beginning of a function definition, set to 1 if
139 : a return statement with no argument is seen. */
140 :
141 : int current_function_returns_null;
142 :
143 : /* Set to 0 at beginning of a function definition, set to 1 if
144 : a call to a noreturn function is seen. */
145 :
146 : int current_function_returns_abnormally;
147 :
148 : /* Set to nonzero by `grokdeclarator' for a function
149 : whose return type is defaulted, if warnings for this are desired. */
150 :
151 : static int warn_about_return_type;
152 :
153 : /* Nonzero when the current toplevel function contains a declaration
154 : of a nested function which is never defined. */
155 :
156 : static bool undef_nested_function;
157 :
158 : /* Vector of implicit "omp declare target" attributes to be added into
159 : the attribute lists. */
160 : vec<c_omp_declare_target_attr, va_gc> *current_omp_declare_target_attribute;
161 :
162 : /* Vector of
163 : #pragma omp begin assumes ... #pragma omp end assumes regions
164 : we are in. */
165 : vec<c_omp_begin_assumes_data, va_gc> *current_omp_begin_assumes;
166 :
167 : /* Vector of "omp begin/end declare variant" blocks we are in. */
168 : vec<c_omp_declare_variant_attr, va_gc> *current_omp_declare_variant_attribute;
169 :
170 : /* Vector of loop names with C_DECL_LOOP_NAME or C_DECL_SWITCH_NAME marked
171 : LABEL_DECL as the last and canonical for each loop or switch. */
172 : static vec<tree> loop_names;
173 :
174 : /* Hash table mapping LABEL_DECLs to the canonical LABEL_DECLs if LOOP_NAMES
175 : vector becomes too long. */
176 : static decl_tree_map *loop_names_hash;
177 :
178 : /* Each c_binding structure describes one binding of an identifier to
179 : a decl. All the decls in a scope - irrespective of namespace - are
180 : chained together by the ->prev field, which (as the name implies)
181 : runs in reverse order. All the decls in a given namespace bound to
182 : a given identifier are chained by the ->shadowed field, which runs
183 : from inner to outer scopes.
184 :
185 : The ->decl field usually points to a DECL node, but there are two
186 : exceptions. In the namespace of type tags, the bound entity is a
187 : RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
188 : identifier is encountered, it is bound to error_mark_node to
189 : suppress further errors about that identifier in the current
190 : function.
191 :
192 : The ->u.type field stores the type of the declaration in this scope;
193 : if NULL, the type is the type of the ->decl field. This is only of
194 : relevance for objects with external or internal linkage which may
195 : be redeclared in inner scopes, forming composite types that only
196 : persist for the duration of those scopes. In the external scope,
197 : this stores the composite of all the types declared for this
198 : object, visible or not. The ->inner_comp field (used only at file
199 : scope) stores whether an incomplete array type at file scope was
200 : completed at an inner scope to an array size other than 1.
201 :
202 : The ->u.label field is used for labels. It points to a structure
203 : which stores additional information used for warnings.
204 :
205 : The depth field is copied from the scope structure that holds this
206 : decl. It is used to preserve the proper ordering of the ->shadowed
207 : field (see bind()) and also for a handful of special-case checks.
208 : Finally, the invisible bit is true for a decl which should be
209 : ignored for purposes of normal name lookup, and the nested bit is
210 : true for a decl that's been bound a second time in an inner scope;
211 : in all such cases, the binding in the outer scope will have its
212 : invisible bit true. */
213 :
214 : struct GTY((chain_next ("%h.prev"))) c_binding {
215 : union GTY(()) { /* first so GTY desc can use decl */
216 : tree GTY((tag ("0"))) type; /* the type in this scope */
217 : struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
218 : } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
219 : tree decl; /* the decl bound */
220 : tree id; /* the identifier it's bound to */
221 : struct c_binding *prev; /* the previous decl in this scope */
222 : struct c_binding *shadowed; /* the innermost decl shadowed by this one */
223 : unsigned int depth : 28; /* depth of this scope */
224 : bool invisible : 1; /* normal lookup should ignore this binding */
225 : bool nested : 1; /* do not set DECL_CONTEXT when popping */
226 : bool inner_comp : 1; /* incomplete array completed in inner scope */
227 : bool in_struct : 1; /* currently defined as struct field */
228 : location_t locus; /* location for nested bindings */
229 : };
230 : #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
231 : #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
232 : #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
233 : #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
234 :
235 : /* Each C symbol points to three linked lists of c_binding structures.
236 : These describe the values of the identifier in the three different
237 : namespaces defined by the language. */
238 :
239 : struct GTY(()) lang_identifier {
240 : struct c_common_identifier common_id;
241 : struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
242 : struct c_binding *tag_binding; /* struct/union/enum tags */
243 : struct c_binding *label_binding; /* labels */
244 : };
245 :
246 : /* Validate c-lang.cc's assumptions. */
247 : extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
248 : [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
249 :
250 : /* The binding oracle; see c-tree.h. */
251 : void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
252 :
253 : /* This flag is set on an identifier if we have previously asked the
254 : binding oracle for this identifier's symbol binding. */
255 : #define I_SYMBOL_CHECKED(node) \
256 : (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
257 :
258 : static inline struct c_binding* *
259 4572044965 : i_symbol_binding (tree node)
260 : {
261 4572044965 : struct lang_identifier *lid
262 4572044965 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
263 :
264 4572044965 : if (lid->symbol_binding == NULL
265 1684886006 : && c_binding_oracle != NULL
266 4572044965 : && !I_SYMBOL_CHECKED (node))
267 : {
268 : /* Set the "checked" flag first, to avoid infinite recursion
269 : when the binding oracle calls back into gcc. */
270 0 : I_SYMBOL_CHECKED (node) = 1;
271 0 : c_binding_oracle (C_ORACLE_SYMBOL, node);
272 : }
273 :
274 4572044965 : return &lid->symbol_binding;
275 : }
276 :
277 : #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
278 :
279 : #define I_SYMBOL_DECL(node) \
280 : (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
281 :
282 : /* This flag is set on an identifier if we have previously asked the
283 : binding oracle for this identifier's tag binding. */
284 : #define I_TAG_CHECKED(node) \
285 : (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
286 :
287 : static inline struct c_binding **
288 4570410 : i_tag_binding (tree node)
289 : {
290 4570410 : struct lang_identifier *lid
291 4570410 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
292 :
293 4570410 : if (lid->tag_binding == NULL
294 1265149 : && c_binding_oracle != NULL
295 4570410 : && !I_TAG_CHECKED (node))
296 : {
297 : /* Set the "checked" flag first, to avoid infinite recursion
298 : when the binding oracle calls back into gcc. */
299 0 : I_TAG_CHECKED (node) = 1;
300 0 : c_binding_oracle (C_ORACLE_TAG, node);
301 : }
302 :
303 4570410 : return &lid->tag_binding;
304 : }
305 :
306 : #define I_TAG_BINDING(node) (*i_tag_binding (node))
307 :
308 : #define I_TAG_DECL(node) \
309 : (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
310 :
311 : /* This flag is set on an identifier if we have previously asked the
312 : binding oracle for this identifier's label binding. */
313 : #define I_LABEL_CHECKED(node) \
314 : (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
315 :
316 : static inline struct c_binding **
317 362098 : i_label_binding (tree node)
318 : {
319 362098 : struct lang_identifier *lid
320 362098 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
321 :
322 362098 : if (lid->label_binding == NULL
323 48394 : && c_binding_oracle != NULL
324 362098 : && !I_LABEL_CHECKED (node))
325 : {
326 : /* Set the "checked" flag first, to avoid infinite recursion
327 : when the binding oracle calls back into gcc. */
328 0 : I_LABEL_CHECKED (node) = 1;
329 0 : c_binding_oracle (C_ORACLE_LABEL, node);
330 : }
331 :
332 362098 : return &lid->label_binding;
333 : }
334 :
335 : #define I_LABEL_BINDING(node) (*i_label_binding (node))
336 :
337 : #define I_LABEL_DECL(node) \
338 : (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
339 :
340 : /* Used by C_TOKEN_VEC tree. */
341 : struct GTY (()) c_tree_token_vec {
342 : struct tree_base base;
343 : vec<c_token, va_gc> *tokens;
344 : };
345 :
346 : STATIC_ASSERT (sizeof (c_tree_token_vec) == sizeof (c_tree_token_vec_struct));
347 : STATIC_ASSERT (offsetof (c_tree_token_vec, tokens)
348 : == offsetof (c_tree_token_vec_struct, tokens));
349 :
350 : /* The resulting tree type. */
351 :
352 : union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE + 2 * (TREE_CODE (&%h.generic) == C_TOKEN_VEC)"),
353 : chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
354 : {
355 : union tree_node GTY ((tag ("0"),
356 : desc ("tree_node_structure (&%h)")))
357 : generic;
358 : struct lang_identifier GTY ((tag ("1"))) identifier;
359 : struct c_tree_token_vec GTY ((tag ("2"))) c_token_vec;
360 : };
361 :
362 : /* Langhook for tree_size. */
363 : size_t
364 911 : c_tree_size (enum tree_code code)
365 : {
366 911 : gcc_checking_assert (code >= NUM_TREE_CODES);
367 911 : switch (code)
368 : {
369 : case C_TOKEN_VEC: return sizeof (c_tree_token_vec);
370 0 : default:
371 0 : switch (TREE_CODE_CLASS (code))
372 : {
373 : case tcc_declaration: return sizeof (tree_decl_non_common);
374 0 : case tcc_type: return sizeof (tree_type_non_common);
375 0 : default: gcc_unreachable ();
376 : }
377 : }
378 : }
379 :
380 : /* Track bindings and other things that matter for goto warnings. For
381 : efficiency, we do not gather all the decls at the point of
382 : definition. Instead, we point into the bindings structure. As
383 : scopes are popped, we update these structures and gather the decls
384 : that matter at that time. */
385 :
386 : struct GTY(()) c_spot_bindings {
387 : /* The currently open scope which holds bindings defined when the
388 : label was defined or the goto statement was found. */
389 : struct c_scope *scope;
390 : /* The bindings in the scope field which were defined at the point
391 : of the label or goto. This lets us look at older or newer
392 : bindings in the scope, as appropriate. */
393 : struct c_binding *bindings_in_scope;
394 : /* The number of statement expressions that have started since this
395 : label or goto statement was defined. This is zero if we are at
396 : the same statement expression level. It is positive if we are in
397 : a statement expression started since this spot. It is negative
398 : if this spot was in a statement expression and we have left
399 : it. */
400 : int stmt_exprs;
401 : /* Whether we started in a statement expression but are no longer in
402 : it. This is set to true if stmt_exprs ever goes negative. */
403 : bool left_stmt_expr;
404 : };
405 :
406 : /* This structure is used to keep track of bindings seen when a goto
407 : statement is defined. This is only used if we see the goto
408 : statement before we see the label. */
409 :
410 : struct GTY(()) c_goto_bindings {
411 : /* The location of the goto statement. */
412 : location_t loc;
413 : /* The bindings of the goto statement. */
414 : struct c_spot_bindings goto_bindings;
415 : };
416 :
417 : typedef struct c_goto_bindings *c_goto_bindings_p;
418 :
419 : /* The additional information we keep track of for a label binding.
420 : These fields are updated as scopes are popped. */
421 :
422 : struct GTY(()) c_label_vars {
423 : /* The shadowed c_label_vars, when one label shadows another (which
424 : can only happen using a __label__ declaration). */
425 : struct c_label_vars *shadowed;
426 : /* The bindings when the label was defined. */
427 : struct c_spot_bindings label_bindings;
428 : /* A list of decls that we care about: decls about which we should
429 : warn if a goto branches to this label from later in the function.
430 : Decls are added to this list as scopes are popped. We only add
431 : the decls that matter. */
432 : vec<tree, va_gc> *decls_in_scope;
433 : /* A list of goto statements to this label. This is only used for
434 : goto statements seen before the label was defined, so that we can
435 : issue appropriate warnings for them. */
436 : vec<c_goto_bindings_p, va_gc> *gotos;
437 : };
438 :
439 : /* Each c_scope structure describes the complete contents of one
440 : scope. Four scopes are distinguished specially: the innermost or
441 : current scope, the innermost function scope, the file scope (always
442 : the second to outermost) and the outermost or external scope.
443 :
444 : Most declarations are recorded in the current scope.
445 :
446 : All normal label declarations are recorded in the innermost
447 : function scope, as are bindings of undeclared identifiers to
448 : error_mark_node. (GCC permits nested functions as an extension,
449 : hence the 'innermost' qualifier.) Explicitly declared labels
450 : (using the __label__ extension) appear in the current scope.
451 :
452 : Being in the file scope (current_scope == file_scope) causes
453 : special behavior in several places below. Also, under some
454 : conditions the Objective-C front end records declarations in the
455 : file scope even though that isn't the current scope.
456 :
457 : All declarations with external linkage are recorded in the external
458 : scope, even if they aren't visible there; this models the fact that
459 : such declarations are visible to the entire program, and (with a
460 : bit of cleverness, see pushdecl) allows diagnosis of some violations
461 : of C99 6.2.2p7 and 6.2.7p2:
462 :
463 : If, within the same translation unit, the same identifier appears
464 : with both internal and external linkage, the behavior is
465 : undefined.
466 :
467 : All declarations that refer to the same object or function shall
468 : have compatible type; otherwise, the behavior is undefined.
469 :
470 : Initially only the built-in declarations, which describe compiler
471 : intrinsic functions plus a subset of the standard library, are in
472 : this scope.
473 :
474 : The order of the blocks list matters, and it is frequently appended
475 : to. To avoid having to walk all the way to the end of the list on
476 : each insertion, or reverse the list later, we maintain a pointer to
477 : the last list entry. (FIXME: It should be feasible to use a reversed
478 : list here.)
479 :
480 : The bindings list is strictly in reverse order of declarations;
481 : pop_scope relies on this. */
482 :
483 :
484 : struct GTY((chain_next ("%h.outer"))) c_scope {
485 : /* The scope containing this one. */
486 : struct c_scope *outer;
487 :
488 : /* The next outermost function scope. */
489 : struct c_scope *outer_function;
490 :
491 : /* All bindings in this scope. */
492 : struct c_binding *bindings;
493 :
494 : /* For each scope (except the global one), a chain of BLOCK nodes
495 : for all the scopes that were entered and exited one level down. */
496 : tree blocks;
497 : tree blocks_last;
498 :
499 : /* The depth of this scope. Used to keep the ->shadowed chain of
500 : bindings sorted innermost to outermost. */
501 : unsigned int depth : 28;
502 :
503 : /* True if we are currently filling this scope with parameter
504 : declarations. */
505 : bool parm_flag : 1;
506 :
507 : /* True if we saw [*] in this scope. Used to give an error messages
508 : if these appears in a function definition. */
509 : bool had_vla_unspec : 1;
510 :
511 : /* True if we parsed a list of forward parameter decls in this scope. */
512 : bool had_forward_parm_decls : 1;
513 :
514 : /* True if this is the outermost block scope of a function body.
515 : This scope contains the parameters, the local variables declared
516 : in the outermost block, and all the labels (except those in
517 : nested functions, or declared at block scope with __label__). */
518 : bool function_body : 1;
519 :
520 : /* True means make a BLOCK for this scope no matter what. */
521 : bool keep : 1;
522 :
523 : /* True means that an unsuffixed float constant is _Decimal64. */
524 : bool float_const_decimal64 : 1;
525 :
526 : /* True if this scope has any label bindings. This is used to speed
527 : up searching for labels when popping scopes, particularly since
528 : labels are normally only found at function scope. */
529 : bool has_label_bindings : 1;
530 :
531 : /* True if we should issue a warning if a goto statement crosses any
532 : of the bindings. We still need to check the list of bindings to
533 : find the specific ones we need to warn about. This is true if
534 : decl_jump_unsafe would return true for any of the bindings. This
535 : is used to avoid looping over all the bindings unnecessarily. */
536 : bool has_jump_unsafe_decl : 1;
537 : };
538 :
539 : /* The scope currently in effect. */
540 :
541 : static GTY(()) struct c_scope *current_scope;
542 :
543 : /* The innermost function scope. Ordinary (not explicitly declared)
544 : labels, bindings to error_mark_node, and the lazily-created
545 : bindings of __func__ and its friends get this scope. */
546 :
547 : static GTY(()) struct c_scope *current_function_scope;
548 :
549 : /* The C file scope. This is reset for each input translation unit. */
550 :
551 : static GTY(()) struct c_scope *file_scope;
552 :
553 : /* The outermost scope. This is used for all declarations with
554 : external linkage, and only these, hence the name. */
555 :
556 : static GTY(()) struct c_scope *external_scope;
557 :
558 : /* A chain of c_scope structures awaiting reuse. */
559 :
560 : static GTY((deletable)) struct c_scope *scope_freelist;
561 :
562 : /* A chain of c_binding structures awaiting reuse. */
563 :
564 : static GTY((deletable)) struct c_binding *binding_freelist;
565 :
566 : /* Append VAR to LIST in scope SCOPE. */
567 : #define SCOPE_LIST_APPEND(scope, list, decl) do { \
568 : struct c_scope *s_ = (scope); \
569 : tree d_ = (decl); \
570 : if (s_->list##_last) \
571 : BLOCK_CHAIN (s_->list##_last) = d_; \
572 : else \
573 : s_->list = d_; \
574 : s_->list##_last = d_; \
575 : } while (0)
576 :
577 : /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
578 : #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
579 : struct c_scope *t_ = (tscope); \
580 : struct c_scope *f_ = (fscope); \
581 : if (t_->to##_last) \
582 : BLOCK_CHAIN (t_->to##_last) = f_->from; \
583 : else \
584 : t_->to = f_->from; \
585 : t_->to##_last = f_->from##_last; \
586 : } while (0)
587 :
588 : /* A c_inline_static structure stores details of a static identifier
589 : referenced in a definition of a function that may be an inline
590 : definition if no subsequent declaration of that function uses
591 : "extern" or does not use "inline". */
592 :
593 : struct GTY((chain_next ("%h.next"))) c_inline_static {
594 : /* The location for a diagnostic. */
595 : location_t location;
596 :
597 : /* The function that may be an inline definition. */
598 : tree function;
599 :
600 : /* The object or function referenced. */
601 : tree static_decl;
602 :
603 : /* What sort of reference this is. */
604 : enum c_inline_static_type type;
605 :
606 : /* The next such structure or NULL. */
607 : struct c_inline_static *next;
608 : };
609 :
610 : /* List of static identifiers used or referenced in functions that may
611 : be inline definitions. */
612 : static GTY(()) struct c_inline_static *c_inline_statics;
613 :
614 : /* True means unconditionally make a BLOCK for the next scope pushed. */
615 :
616 : static bool keep_next_level_flag;
617 :
618 : /* True means the next call to push_scope will be the outermost scope
619 : of a function body, so do not push a new scope, merely cease
620 : expecting parameter decls. */
621 :
622 : static bool next_is_function_body;
623 :
624 : /* A vector of pointers to c_binding structures. */
625 :
626 : typedef struct c_binding *c_binding_ptr;
627 :
628 : /* Information that we keep for a struct or union while it is being
629 : parsed. */
630 :
631 1183107 : class c_struct_parse_info
632 : {
633 : public:
634 : /* If warn_cxx_compat, a list of types defined within this
635 : struct. */
636 : auto_vec<tree> struct_types;
637 : /* If warn_cxx_compat, a list of field names which have bindings,
638 : and which are defined in this struct, but which are not defined
639 : in any enclosing struct. This is used to clear the in_struct
640 : field of the c_bindings structure. */
641 : auto_vec<c_binding_ptr> fields;
642 : /* If warn_cxx_compat, a list of typedef names used when defining
643 : fields in this struct. */
644 : auto_vec<tree> typedefs_seen;
645 : /* The location of a previous definition of this struct. */
646 : location_t refloc;
647 : };
648 :
649 :
650 : /* Hash table for structs and unions. */
651 : struct c_struct_hasher : ggc_ptr_hash<tree_node>
652 : {
653 : static hashval_t hash (tree t);
654 : static bool equal (tree, tree);
655 : };
656 :
657 : /* Hash an RECORD OR UNION. */
658 : hashval_t
659 4346633 : c_struct_hasher::hash (tree type)
660 : {
661 4346633 : inchash::hash hstate;
662 :
663 4346633 : hstate.add_int (TREE_CODE (type));
664 4346633 : tree tag = c_type_tag (type);
665 4346633 : hstate.add_object (tag);
666 :
667 4346633 : return hstate.end ();
668 : }
669 :
670 : /* Compare two RECORD or UNION types. */
671 : bool
672 14674831 : c_struct_hasher::equal (tree t1, tree t2)
673 : {
674 14674831 : return comptypes_equiv_p (t1, t2);
675 : }
676 :
677 : /* All tagged typed so that TYPE_CANONICAL can be set correctly. */
678 : static GTY (()) hash_table<c_struct_hasher> *c_struct_htab;
679 :
680 : /* Information for the struct or union currently being parsed, or
681 : NULL if not parsing a struct or union. */
682 : static class c_struct_parse_info *struct_parse_info;
683 :
684 : /* Forward declarations. */
685 : static tree lookup_name_in_scope (tree, struct c_scope *);
686 : static tree c_make_fname_decl (location_t, tree, int);
687 : static tree grokdeclarator (const struct c_declarator *,
688 : struct c_declspecs *,
689 : enum decl_context, bool, tree *, tree *, tree *,
690 : bool *, enum deprecated_states);
691 : static tree grokparms (struct c_arg_info *, bool);
692 : static void layout_array_type (tree);
693 : static const char *header_for_builtin_fn (tree);
694 :
695 : /* T is a statement. Add it to the statement-tree. This is the
696 : C/ObjC version--C++ has a slightly different version of this
697 : function. */
698 :
699 : tree
700 93906266 : add_stmt (tree t)
701 : {
702 93906266 : enum tree_code code = TREE_CODE (t);
703 :
704 93906266 : if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
705 : {
706 93182662 : if (!EXPR_HAS_LOCATION (t))
707 155561 : SET_EXPR_LOCATION (t, input_location);
708 : }
709 :
710 : /* Add T to the statement-tree. Non-side-effect statements need to be
711 : recorded during statement expressions. */
712 93906266 : if (!building_stmt_list_p ())
713 1 : push_stmt_list ();
714 :
715 93906266 : if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
716 1080627 : STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
717 :
718 93906266 : append_to_statement_list_force (t, &cur_stmt_list);
719 :
720 93906266 : return t;
721 : }
722 :
723 :
724 :
725 :
726 :
727 : /* Return true if we will want to say something if a goto statement
728 : crosses DECL. */
729 :
730 : static bool
731 901395350 : decl_jump_unsafe (tree decl)
732 : {
733 901395350 : if (error_operand_p (decl))
734 : return false;
735 :
736 : /* Don't warn for compound literals. If a goto statement crosses
737 : their initialization, it should cross also all the places where
738 : the complit is used or where the complit address might be saved
739 : into some variable, so code after the label to which goto jumps
740 : should not be able to refer to the compound literal. */
741 901393309 : if (VAR_P (decl) && C_DECL_COMPOUND_LITERAL_P (decl))
742 : return false;
743 :
744 900473102 : if (flag_openmp
745 12826142 : && VAR_P (decl)
746 900504642 : && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl)))
747 : return true;
748 :
749 : /* Always warn about crossing variably modified types. */
750 890428590 : if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
751 912571619 : && c_type_variably_modified_p (TREE_TYPE (decl)))
752 : return true;
753 :
754 : /* Otherwise, only warn if -Wgoto-misses-init and this is an
755 : initialized automatic decl. */
756 900432575 : if (warn_jump_misses_init
757 6103821 : && VAR_P (decl)
758 10269 : && !TREE_STATIC (decl)
759 900438074 : && DECL_INITIAL (decl) != NULL_TREE)
760 : return true;
761 :
762 : return false;
763 : }
764 :
765 :
766 : void
767 0 : c_print_identifier (FILE *file, tree node, int indent)
768 : {
769 0 : void (*save) (enum c_oracle_request, tree identifier);
770 :
771 : /* Temporarily hide any binding oracle. Without this, calls to
772 : debug_tree from the debugger will end up calling into the oracle,
773 : making for a confusing debug session. As the oracle isn't needed
774 : here for normal operation, it's simplest to suppress it. */
775 0 : save = c_binding_oracle;
776 0 : c_binding_oracle = NULL;
777 :
778 0 : print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
779 0 : print_node (file, "tag", I_TAG_DECL (node), indent + 4);
780 0 : print_node (file, "label", I_LABEL_DECL (node), indent + 4);
781 0 : if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
782 : {
783 0 : tree rid = ridpointers[C_RID_CODE (node)];
784 0 : indent_to (file, indent + 4);
785 0 : fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
786 0 : (void *) rid, IDENTIFIER_POINTER (rid));
787 : }
788 :
789 0 : c_binding_oracle = save;
790 0 : }
791 :
792 : /* Establish that the scope contains declarations that are sensitive to
793 : jumps that cross a binding. Together with decl_jump_unsafe, this is
794 : used to diagnose such jumps. */
795 : void
796 93 : c_mark_decl_jump_unsafe_in_current_scope ()
797 : {
798 93 : current_scope->has_jump_unsafe_decl = 1;
799 93 : }
800 :
801 : /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
802 : which may be any of several kinds of DECL or TYPE or error_mark_node,
803 : in the scope SCOPE. */
804 : static void
805 901246195 : bind (tree name, tree decl, struct c_scope *scope, bool invisible,
806 : bool nested, location_t locus)
807 : {
808 901246195 : struct c_binding *b, **here;
809 :
810 901246195 : if (binding_freelist)
811 : {
812 231278345 : b = binding_freelist;
813 231278345 : binding_freelist = b->prev;
814 : }
815 : else
816 669967850 : b = ggc_alloc<c_binding> ();
817 :
818 901246195 : b->shadowed = 0;
819 901246195 : b->decl = decl;
820 901246195 : b->id = name;
821 901246195 : b->depth = scope->depth;
822 901246195 : b->invisible = invisible;
823 901246195 : b->nested = nested;
824 901246195 : b->inner_comp = 0;
825 901246195 : b->in_struct = 0;
826 901246195 : b->locus = locus;
827 :
828 901246195 : b->u.type = NULL;
829 :
830 901246195 : b->prev = scope->bindings;
831 901246195 : scope->bindings = b;
832 :
833 901246195 : if (decl_jump_unsafe (decl))
834 24211 : scope->has_jump_unsafe_decl = 1;
835 :
836 901246195 : if (!name)
837 : return;
838 :
839 892524848 : switch (TREE_CODE (decl))
840 : {
841 24226 : case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
842 631892 : case ENUMERAL_TYPE:
843 631892 : case UNION_TYPE:
844 631892 : case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
845 891868730 : case VAR_DECL:
846 891868730 : case FUNCTION_DECL:
847 891868730 : case TYPE_DECL:
848 891868730 : case CONST_DECL:
849 891868730 : case PARM_DECL:
850 891868730 : case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
851 :
852 0 : default:
853 0 : gcc_unreachable ();
854 : }
855 :
856 : /* Locate the appropriate place in the chain of shadowed decls
857 : to insert this binding. Normally, scope == current_scope and
858 : this does nothing. */
859 892524892 : while (*here && (*here)->depth > scope->depth)
860 44 : here = &(*here)->shadowed;
861 :
862 892524848 : b->shadowed = *here;
863 892524848 : *here = b;
864 : }
865 :
866 : /* Clear the binding structure B, stick it on the binding_freelist,
867 : and return the former value of b->prev. This is used by pop_scope
868 : and get_parm_info to iterate destructively over all the bindings
869 : from a given scope. */
870 : static struct c_binding *
871 883007038 : free_binding_and_advance (struct c_binding *b)
872 : {
873 883007038 : struct c_binding *prev = b->prev;
874 :
875 883007038 : memset (b, 0, sizeof (struct c_binding));
876 883007038 : b->prev = binding_freelist;
877 883007038 : binding_freelist = b;
878 :
879 883007038 : return prev;
880 : }
881 :
882 : /* Bind a label. Like bind, but skip fields which aren't used for
883 : labels, and add the LABEL_VARS value. */
884 : static void
885 24226 : bind_label (tree name, tree label, struct c_scope *scope,
886 : struct c_label_vars *label_vars)
887 : {
888 24226 : struct c_binding *b;
889 :
890 24226 : bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
891 : UNKNOWN_LOCATION);
892 :
893 24226 : scope->has_label_bindings = true;
894 :
895 24226 : b = scope->bindings;
896 24226 : gcc_assert (b->decl == label);
897 24226 : label_vars->shadowed = b->u.label;
898 24226 : b->u.label = label_vars;
899 24226 : }
900 :
901 : /* Hook called at end of compilation to assume 1 elt
902 : for a file-scope tentative array defn that wasn't complete before. */
903 :
904 : void
905 7498 : c_finish_incomplete_decl (tree decl)
906 : {
907 7498 : if (VAR_P (decl))
908 : {
909 7498 : tree type = TREE_TYPE (decl);
910 7498 : if (type != error_mark_node
911 7421 : && TREE_CODE (type) == ARRAY_TYPE
912 7226 : && !DECL_EXTERNAL (decl)
913 7587 : && TYPE_DOMAIN (type) == NULL_TREE)
914 : {
915 89 : if (flag_isoc2y && !TREE_PUBLIC (decl))
916 2 : error_at (DECL_SOURCE_LOCATION (decl),
917 : "array size missing in %q+D", decl);
918 : else
919 87 : warning_at (DECL_SOURCE_LOCATION (decl),
920 87 : 0, "array %q+D assumed to have one element", decl);
921 :
922 89 : complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
923 :
924 89 : relayout_decl (decl);
925 : }
926 : }
927 7498 : }
928 :
929 : /* Record that inline function FUNC contains a reference (location
930 : LOC) to static DECL (file-scope or function-local according to
931 : TYPE). */
932 :
933 : void
934 36 : record_inline_static (location_t loc, tree func, tree decl,
935 : enum c_inline_static_type type)
936 : {
937 36 : c_inline_static *csi = ggc_alloc<c_inline_static> ();
938 36 : csi->location = loc;
939 36 : csi->function = func;
940 36 : csi->static_decl = decl;
941 36 : csi->type = type;
942 36 : csi->next = c_inline_statics;
943 36 : c_inline_statics = csi;
944 36 : }
945 :
946 : /* Check for references to static declarations in inline functions at
947 : the end of the translation unit and diagnose them if the functions
948 : are still inline definitions. */
949 :
950 : static void
951 105818 : check_inline_statics (void)
952 : {
953 105818 : struct c_inline_static *csi;
954 105854 : for (csi = c_inline_statics; csi; csi = csi->next)
955 : {
956 36 : if (DECL_EXTERNAL (csi->function))
957 30 : switch (csi->type)
958 : {
959 23 : case csi_internal:
960 23 : pedwarn (csi->location, 0,
961 : "%qD is static but used in inline function %qD "
962 : "which is not static", csi->static_decl, csi->function);
963 23 : break;
964 7 : case csi_modifiable:
965 7 : pedwarn (csi->location, 0,
966 : "%q+D is static but declared in inline function %qD "
967 : "which is not static", csi->static_decl, csi->function);
968 7 : break;
969 0 : default:
970 0 : gcc_unreachable ();
971 : }
972 : }
973 105818 : c_inline_statics = NULL;
974 105818 : }
975 :
976 : /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
977 : for the current state, otherwise set it to uninitialized. */
978 :
979 : static void
980 135557 : set_spot_bindings (struct c_spot_bindings *p, bool defining)
981 : {
982 0 : if (defining)
983 : {
984 128277 : p->scope = current_scope;
985 16946 : p->bindings_in_scope = current_scope->bindings;
986 : }
987 : else
988 : {
989 7280 : p->scope = NULL;
990 7280 : p->bindings_in_scope = NULL;
991 : }
992 135557 : p->stmt_exprs = 0;
993 135557 : p->left_stmt_expr = false;
994 0 : }
995 :
996 : /* Update spot bindings P as we pop out of SCOPE. Return true if we
997 : should push decls for a label. */
998 :
999 : static bool
1000 470625326 : update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
1001 : {
1002 0 : if (p->scope != scope)
1003 : {
1004 : /* This label or goto is defined in some other scope, or it is a
1005 : label which is not yet defined. There is nothing to
1006 : update. */
1007 : return false;
1008 : }
1009 :
1010 : /* Adjust the spot bindings to refer to the bindings already defined
1011 : in the enclosing scope. */
1012 207032 : p->scope = scope->outer;
1013 207032 : p->bindings_in_scope = p->scope->bindings;
1014 :
1015 152421 : return true;
1016 : }
1017 :
1018 : /* The Objective-C front-end often needs to determine the current scope. */
1019 :
1020 : void *
1021 0 : objc_get_current_scope (void)
1022 : {
1023 0 : return current_scope;
1024 : }
1025 :
1026 : /* The following function is used only by Objective-C. It needs to live here
1027 : because it accesses the innards of c_scope. */
1028 :
1029 : void
1030 0 : objc_mark_locals_volatile (void *enclosing_blk)
1031 : {
1032 0 : struct c_scope *scope;
1033 0 : struct c_binding *b;
1034 :
1035 0 : for (scope = current_scope;
1036 0 : scope && scope != enclosing_blk;
1037 0 : scope = scope->outer)
1038 : {
1039 0 : for (b = scope->bindings; b; b = b->prev)
1040 0 : objc_volatilize_decl (b->decl);
1041 :
1042 : /* Do not climb up past the current function. */
1043 0 : if (scope->function_body)
1044 : break;
1045 : }
1046 0 : }
1047 :
1048 : /* Return true if we are in the global binding level. */
1049 :
1050 : bool
1051 2093837 : global_bindings_p (void)
1052 : {
1053 2093837 : return current_scope == file_scope;
1054 : }
1055 :
1056 : /* Return true if we're declaring parameters in an old-style function
1057 : declaration. */
1058 :
1059 : bool
1060 34964 : old_style_parameter_scope (void)
1061 : {
1062 : /* If processing parameters and there is no function statement list, we
1063 : * have an old-style function declaration. */
1064 34964 : return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
1065 : }
1066 :
1067 : void
1068 52415 : keep_next_level (void)
1069 : {
1070 52415 : keep_next_level_flag = true;
1071 52415 : }
1072 :
1073 : /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
1074 :
1075 : void
1076 20 : set_float_const_decimal64 (void)
1077 : {
1078 20 : current_scope->float_const_decimal64 = true;
1079 20 : }
1080 :
1081 : /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
1082 :
1083 : void
1084 28 : clear_float_const_decimal64 (void)
1085 : {
1086 28 : current_scope->float_const_decimal64 = false;
1087 28 : }
1088 :
1089 : /* Return nonzero if an unsuffixed float constant is _Decimal64. */
1090 :
1091 : bool
1092 602745 : float_const_decimal64_p (void)
1093 : {
1094 602745 : return current_scope->float_const_decimal64;
1095 : }
1096 :
1097 : /* Identify this scope as currently being filled with parameters. */
1098 :
1099 : void
1100 87384434 : declare_parm_level (void)
1101 : {
1102 87384434 : current_scope->parm_flag = true;
1103 87384434 : }
1104 :
1105 : void
1106 128755062 : push_scope (void)
1107 : {
1108 128755062 : if (next_is_function_body)
1109 : {
1110 : /* This is the transition from the parameters to the top level
1111 : of the function body. These are the same scope
1112 : (C99 6.2.1p4,6) so we do not push another scope structure.
1113 : next_is_function_body is set only by store_parm_decls, which
1114 : in turn is called when and only when we are about to
1115 : encounter the opening curly brace for the function body.
1116 :
1117 : The outermost block of a function always gets a BLOCK node,
1118 : because the debugging output routines expect that each
1119 : function has at least one BLOCK. */
1120 36350916 : current_scope->parm_flag = false;
1121 36350916 : current_scope->function_body = true;
1122 36350916 : current_scope->keep = true;
1123 36350916 : current_scope->outer_function = current_function_scope;
1124 36350916 : current_function_scope = current_scope;
1125 :
1126 36350916 : keep_next_level_flag = false;
1127 36350916 : next_is_function_body = false;
1128 :
1129 : /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1130 36350916 : if (current_scope->outer)
1131 36350916 : current_scope->float_const_decimal64
1132 36350916 : = current_scope->outer->float_const_decimal64;
1133 : else
1134 0 : current_scope->float_const_decimal64 = false;
1135 : }
1136 : else
1137 : {
1138 92404146 : struct c_scope *scope;
1139 92404146 : if (scope_freelist)
1140 : {
1141 91549998 : scope = scope_freelist;
1142 91549998 : scope_freelist = scope->outer;
1143 : }
1144 : else
1145 854148 : scope = ggc_cleared_alloc<c_scope> ();
1146 :
1147 : /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1148 92404146 : if (current_scope)
1149 92292336 : scope->float_const_decimal64 = current_scope->float_const_decimal64;
1150 : else
1151 111810 : scope->float_const_decimal64 = false;
1152 :
1153 92404146 : scope->keep = keep_next_level_flag;
1154 92404146 : scope->outer = current_scope;
1155 92404146 : scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1156 :
1157 : /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1158 : possible. */
1159 92404146 : if (current_scope && scope->depth == 0)
1160 : {
1161 0 : scope->depth--;
1162 0 : sorry ("GCC supports only %u nested scopes", scope->depth);
1163 : }
1164 :
1165 92404146 : current_scope = scope;
1166 92404146 : keep_next_level_flag = false;
1167 : }
1168 128755062 : }
1169 :
1170 : /* This is called when we are leaving SCOPE. For each label defined
1171 : in SCOPE, add any appropriate decls to its decls_in_scope fields.
1172 : These are the decls whose initialization will be skipped by a goto
1173 : later in the function. */
1174 :
1175 : static void
1176 92397331 : update_label_decls (struct c_scope *scope)
1177 : {
1178 92397331 : struct c_scope *s;
1179 :
1180 92397331 : s = scope;
1181 333534150 : while (s != NULL)
1182 : {
1183 282305158 : if (s->has_label_bindings)
1184 : {
1185 234777 : struct c_binding *b;
1186 :
1187 2182345 : for (b = s->bindings; b != NULL; b = b->prev)
1188 : {
1189 1947568 : struct c_label_vars *label_vars;
1190 1947568 : struct c_binding *b1;
1191 1947568 : bool hjud;
1192 1947568 : unsigned int ix;
1193 1947568 : struct c_goto_bindings *g;
1194 :
1195 1947568 : if (TREE_CODE (b->decl) != LABEL_DECL)
1196 1436472 : continue;
1197 511096 : label_vars = b->u.label;
1198 :
1199 511096 : b1 = label_vars->label_bindings.bindings_in_scope;
1200 511096 : if (label_vars->label_bindings.scope == NULL)
1201 : hjud = false;
1202 : else
1203 199373 : hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1204 511096 : if (update_spot_bindings (scope, &label_vars->label_bindings))
1205 : {
1206 : /* This label is defined in this scope. */
1207 54611 : if (hjud)
1208 : {
1209 169690 : for (; b1 != NULL; b1 = b1->prev)
1210 : {
1211 : /* A goto from later in the function to this
1212 : label will never see the initialization
1213 : of B1, if any. Save it to issue a
1214 : warning if needed. */
1215 148267 : if (decl_jump_unsafe (b1->decl))
1216 19464 : vec_safe_push(label_vars->decls_in_scope, b1->decl);
1217 : }
1218 : }
1219 : }
1220 :
1221 : /* Update the bindings of any goto statements associated
1222 : with this label. */
1223 472357224 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1224 470266651 : update_spot_bindings (scope, &g->goto_bindings);
1225 : }
1226 : }
1227 :
1228 : /* Don't search beyond the current function. */
1229 282305158 : if (s == current_function_scope)
1230 : break;
1231 :
1232 241136819 : s = s->outer;
1233 : }
1234 92397331 : }
1235 :
1236 : /* Exit a scope. Restore the state of the identifier-decl mappings
1237 : that were in effect when this scope was entered. Return a BLOCK
1238 : node containing all the DECLs in this scope that are of interest
1239 : to debug info generation. */
1240 :
1241 : tree
1242 92397331 : pop_scope (void)
1243 : {
1244 92397331 : struct c_scope *scope = current_scope;
1245 92397331 : tree block, context, p;
1246 92397331 : struct c_binding *b;
1247 :
1248 92397331 : bool functionbody = scope->function_body;
1249 92397331 : bool keep = functionbody || scope->keep || scope->bindings;
1250 :
1251 92397331 : update_label_decls (scope);
1252 :
1253 : /* If appropriate, create a BLOCK to record the decls for the life
1254 : of this function. */
1255 92397331 : block = NULL_TREE;
1256 92397331 : if (keep)
1257 : {
1258 36842705 : block = make_node (BLOCK);
1259 36842705 : BLOCK_SUBBLOCKS (block) = scope->blocks;
1260 36842705 : TREE_USED (block) = 1;
1261 :
1262 : /* In each subblock, record that this is its superior. */
1263 37123521 : for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1264 280816 : BLOCK_SUPERCONTEXT (p) = block;
1265 :
1266 36842705 : BLOCK_VARS (block) = NULL_TREE;
1267 : }
1268 :
1269 : /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1270 : scope must be set so that they point to the appropriate
1271 : construct, i.e. either to the current FUNCTION_DECL node, or
1272 : else to the BLOCK node we just constructed.
1273 :
1274 : Note that for tagged types whose scope is just the formal
1275 : parameter list for some function type specification, we can't
1276 : properly set their TYPE_CONTEXTs here, because we don't have a
1277 : pointer to the appropriate FUNCTION_TYPE node readily available
1278 : to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1279 : type nodes get set in `grokdeclarator' as soon as we have created
1280 : the FUNCTION_TYPE node which will represent the "scope" for these
1281 : "parameter list local" tagged types. */
1282 92397331 : if (scope->function_body)
1283 36350915 : context = current_function_decl;
1284 56046416 : else if (scope == file_scope)
1285 : {
1286 105488 : tree file_decl
1287 105488 : = build_translation_unit_decl (get_identifier (main_input_filename));
1288 105488 : context = file_decl;
1289 105488 : debug_hooks->register_main_translation_unit (file_decl);
1290 : }
1291 : else
1292 : context = block;
1293 :
1294 : /* Clear all bindings in this scope. */
1295 851582318 : for (b = scope->bindings; b; b = free_binding_and_advance (b))
1296 : {
1297 759184987 : p = b->decl;
1298 759184987 : switch (TREE_CODE (p))
1299 : {
1300 24226 : case LABEL_DECL:
1301 : /* Warnings for unused labels, errors for undefined labels. */
1302 24226 : if (TREE_USED (p) && !DECL_INITIAL (p))
1303 : {
1304 63 : error ("label %q+D used but not defined", p);
1305 63 : DECL_INITIAL (p) = error_mark_node;
1306 : }
1307 : else
1308 24163 : warn_for_unused_label (p);
1309 :
1310 : /* Labels go in BLOCK_VARS. */
1311 24226 : DECL_CHAIN (p) = BLOCK_VARS (block);
1312 24226 : BLOCK_VARS (block) = p;
1313 24226 : gcc_assert (I_LABEL_BINDING (b->id) == b);
1314 24226 : I_LABEL_BINDING (b->id) = b->shadowed;
1315 :
1316 : /* Also pop back to the shadowed label_vars. */
1317 24226 : release_tree_vector (b->u.label->decls_in_scope);
1318 24226 : b->u.label = b->u.label->shadowed;
1319 24226 : break;
1320 :
1321 1432587 : case ENUMERAL_TYPE:
1322 1432587 : case UNION_TYPE:
1323 1432587 : case RECORD_TYPE:
1324 :
1325 : /* Types may not have tag-names, in which case the type
1326 : appears in the bindings list with b->id NULL. */
1327 1432587 : if (b->id)
1328 : {
1329 630066 : gcc_assert (I_TAG_BINDING (b->id) == b);
1330 630066 : I_TAG_BINDING (b->id) = b->shadowed;
1331 : }
1332 : break;
1333 :
1334 629191377 : case FUNCTION_DECL:
1335 : /* Propagate TREE_ADDRESSABLE from nested functions to their
1336 : containing functions. */
1337 629191377 : if (!TREE_ASM_WRITTEN (p)
1338 629191377 : && DECL_INITIAL (p) != NULL_TREE
1339 72341501 : && TREE_ADDRESSABLE (p)
1340 3694852 : && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1341 629191377 : && DECL_ABSTRACT_ORIGIN (p) != p)
1342 0 : TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1343 629191377 : if (!TREE_PUBLIC (p)
1344 359769 : && !DECL_INITIAL (p)
1345 174 : && !b->nested
1346 143 : && scope != file_scope
1347 629191385 : && scope != external_scope)
1348 : {
1349 8 : error ("nested function %q+D declared but never defined", p);
1350 8 : undef_nested_function = true;
1351 : }
1352 629191369 : else if (DECL_DECLARED_INLINE_P (p)
1353 71152422 : && TREE_PUBLIC (p)
1354 700183883 : && !DECL_INITIAL (p))
1355 : {
1356 : /* C99 6.7.4p6: "a function with external linkage... declared
1357 : with an inline function specifier ... shall also be defined
1358 : in the same translation unit." */
1359 76 : if (!flag_gnu89_inline
1360 51 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1361 111 : && scope == external_scope)
1362 13 : pedwarn (input_location, 0,
1363 : "inline function %q+D declared but never defined", p);
1364 76 : DECL_EXTERNAL (p) = 1;
1365 : }
1366 :
1367 629191377 : goto common_symbol;
1368 :
1369 10956581 : case VAR_DECL:
1370 : /* Warnings for unused variables. */
1371 8401227 : if ((!TREE_USED (p) || !DECL_READ_P (p))
1372 2584778 : && !warning_suppressed_p (p, OPT_Wunused_but_set_variable_)
1373 2584268 : && !DECL_IN_SYSTEM_HEADER (p)
1374 2503507 : && DECL_NAME (p)
1375 2503507 : && !DECL_ARTIFICIAL (p)
1376 2503117 : && scope != file_scope
1377 12576974 : && scope != external_scope)
1378 : {
1379 739335 : if (!TREE_USED (p))
1380 : {
1381 733556 : warning (OPT_Wunused_variable, "unused variable %q+D", p);
1382 733556 : suppress_warning (p, OPT_Wunused_variable);
1383 : }
1384 5779 : else if (DECL_CONTEXT (p) == current_function_decl)
1385 5758 : warning_at (DECL_SOURCE_LOCATION (p),
1386 5758 : OPT_Wunused_but_set_variable_,
1387 : "variable %qD set but not used", p);
1388 : }
1389 :
1390 10956581 : if (b->inner_comp)
1391 : {
1392 2 : error ("type of array %q+D completed incompatibly with"
1393 : " implicit initialization", p);
1394 : }
1395 :
1396 : /* Fall through. */
1397 657578839 : case TYPE_DECL:
1398 657578839 : case CONST_DECL:
1399 10956579 : common_symbol:
1400 : /* All of these go in BLOCK_VARS, but only if this is the
1401 : binding in the home scope. */
1402 657578839 : if (!b->nested)
1403 : {
1404 395476494 : DECL_CHAIN (p) = BLOCK_VARS (block);
1405 395476494 : BLOCK_VARS (block) = p;
1406 : }
1407 262102345 : else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1408 : {
1409 : /* For block local externs add a special
1410 : DECL_EXTERNAL decl for debug info generation. */
1411 14596 : tree extp = copy_node (p);
1412 :
1413 14596 : DECL_EXTERNAL (extp) = 1;
1414 14596 : TREE_STATIC (extp) = 0;
1415 14596 : TREE_PUBLIC (extp) = 1;
1416 14596 : DECL_INITIAL (extp) = NULL_TREE;
1417 14596 : DECL_LANG_SPECIFIC (extp) = NULL;
1418 14596 : DECL_CONTEXT (extp) = current_function_decl;
1419 14596 : if (TREE_CODE (p) == FUNCTION_DECL)
1420 : {
1421 13057 : DECL_RESULT (extp) = NULL_TREE;
1422 13057 : DECL_SAVED_TREE (extp) = NULL_TREE;
1423 13057 : DECL_STRUCT_FUNCTION (extp) = NULL;
1424 : }
1425 14596 : if (b->locus != UNKNOWN_LOCATION)
1426 14596 : DECL_SOURCE_LOCATION (extp) = b->locus;
1427 14596 : DECL_CHAIN (extp) = BLOCK_VARS (block);
1428 14596 : BLOCK_VARS (block) = extp;
1429 : }
1430 : /* If this is the file scope set DECL_CONTEXT of each decl to
1431 : the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1432 : work. */
1433 657578839 : if (scope == file_scope)
1434 273512967 : DECL_CONTEXT (p) = context;
1435 :
1436 757728174 : gcc_fallthrough ();
1437 : /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1438 : already been put there by store_parm_decls. Unused-
1439 : parameter warnings are handled by function.cc.
1440 : error_mark_node obviously does not go in BLOCK_VARS and
1441 : does not get unused-variable warnings. */
1442 757728174 : case PARM_DECL:
1443 757728174 : case ERROR_MARK:
1444 : /* It is possible for a decl not to have a name. We get
1445 : here with b->id NULL in this case. */
1446 757728174 : if (b->id)
1447 : {
1448 754524612 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1449 754524612 : I_SYMBOL_BINDING (b->id) = b->shadowed;
1450 754524612 : if (b->shadowed && b->shadowed->u.type)
1451 4780467 : TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1452 : }
1453 : break;
1454 :
1455 0 : default:
1456 0 : gcc_unreachable ();
1457 : }
1458 : }
1459 :
1460 :
1461 : /* Dispose of the block that we just made inside some higher level. */
1462 92397331 : if ((scope->function_body || scope == file_scope) && context)
1463 : {
1464 36456401 : DECL_INITIAL (context) = block;
1465 36456401 : BLOCK_SUPERCONTEXT (block) = context;
1466 : }
1467 55940930 : else if (scope->outer)
1468 : {
1469 55835442 : if (block)
1470 280816 : SCOPE_LIST_APPEND (scope->outer, blocks, block);
1471 : /* If we did not make a block for the scope just exited, any
1472 : blocks made for inner scopes must be carried forward so they
1473 : will later become subblocks of something else. */
1474 55554626 : else if (scope->blocks)
1475 360795 : SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1476 : }
1477 :
1478 : /* Pop the current scope, and free the structure for reuse. */
1479 92397331 : current_scope = scope->outer;
1480 92397331 : if (scope->function_body)
1481 36350915 : current_function_scope = scope->outer_function;
1482 :
1483 92397331 : memset (scope, 0, sizeof (struct c_scope));
1484 92397331 : scope->outer = scope_freelist;
1485 92397331 : scope_freelist = scope;
1486 :
1487 92397331 : return block;
1488 : }
1489 :
1490 : void
1491 105979 : push_file_scope (void)
1492 : {
1493 105979 : tree decl;
1494 :
1495 105979 : if (file_scope)
1496 : return;
1497 :
1498 : /* Call the target stack_protect_guard hook if the stack protection
1499 : guard is declared as a global symbol. */
1500 105979 : if (targetm.stack_protect_guard_symbol_p ())
1501 : {
1502 16 : tree decl = targetm.stack_protect_guard ();
1503 16 : DECL_CHAIN (decl) = visible_builtins;
1504 16 : visible_builtins = decl;
1505 : }
1506 :
1507 105979 : push_scope ();
1508 105979 : file_scope = current_scope;
1509 :
1510 105979 : start_fname_decls ();
1511 :
1512 212322278 : for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1513 212216299 : bind (DECL_NAME (decl), decl, file_scope,
1514 212216299 : /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1515 : }
1516 :
1517 : void
1518 105818 : pop_file_scope (void)
1519 : {
1520 : /* In case there were missing closebraces, get us back to the global
1521 : binding level. */
1522 105823 : while (current_scope != file_scope)
1523 5 : pop_scope ();
1524 :
1525 : /* __FUNCTION__ is defined at file scope (""). This
1526 : call may not be necessary as my tests indicate it
1527 : still works without it. */
1528 105818 : finish_fname_decls ();
1529 :
1530 105818 : check_inline_statics ();
1531 :
1532 : /* This is the point to write out a PCH if we're doing that.
1533 : In that case we do not want to do anything else. */
1534 105818 : if (pch_file)
1535 : {
1536 330 : c_common_write_pch ();
1537 : /* Ensure even the callers don't try to finalize the CU. */
1538 330 : flag_syntax_only = 1;
1539 330 : return;
1540 : }
1541 :
1542 : /* Pop off the file scope and close this translation unit. */
1543 105488 : pop_scope ();
1544 105488 : file_scope = 0;
1545 :
1546 105488 : maybe_apply_pending_pragma_weaks ();
1547 : }
1548 :
1549 : /* Whether we are currently inside the initializer for an
1550 : underspecified object definition (C23 auto or constexpr). */
1551 : static bool in_underspecified_init;
1552 :
1553 : /* Start an underspecified object definition for NAME at LOC. This
1554 : means that NAME is shadowed inside its initializer, so neither the
1555 : definition being initialized, nor any definition from an outer
1556 : scope, may be referenced during that initializer. Return state to
1557 : be passed to finish_underspecified_init. If NAME is NULL_TREE, the
1558 : underspecified object is a (constexpr) compound literal; there is
1559 : no shadowing in that case, but all the other restrictions on
1560 : underspecified object definitions still apply. */
1561 : unsigned int
1562 667 : start_underspecified_init (location_t loc, tree name)
1563 : {
1564 667 : bool prev = in_underspecified_init;
1565 667 : bool ok;
1566 667 : if (name == NULL_TREE)
1567 : ok = true;
1568 : else
1569 : {
1570 433 : tree decl = build_decl (loc, VAR_DECL, name, error_mark_node);
1571 433 : C_DECL_UNDERSPECIFIED (decl) = 1;
1572 433 : struct c_scope *scope = current_scope;
1573 433 : struct c_binding *b = I_SYMBOL_BINDING (name);
1574 433 : if (b && B_IN_SCOPE (b, scope))
1575 : {
1576 4 : error_at (loc, "underspecified declaration of %qE, which is already "
1577 : "declared in this scope", name);
1578 4 : ok = false;
1579 : }
1580 : else
1581 : {
1582 429 : bind (name, decl, scope, false, false, loc);
1583 429 : ok = true;
1584 : }
1585 : }
1586 667 : in_underspecified_init = true;
1587 667 : return ok | (prev << 1);
1588 : }
1589 :
1590 : /* Finish an underspecified object definition for NAME, before that
1591 : name is bound to the real declaration instead of a placeholder.
1592 : PREV_STATE is the value returned by the call to
1593 : start_underspecified_init. If NAME is NULL_TREE, this means a
1594 : compound literal, as for start_underspecified_init. */
1595 : void
1596 667 : finish_underspecified_init (tree name, unsigned int prev_state)
1597 : {
1598 667 : if (name != NULL_TREE && (prev_state & 1))
1599 : {
1600 : /* A VAR_DECL was bound to the name to shadow any previous
1601 : declarations for the name; remove that binding now. */
1602 429 : struct c_scope *scope = current_scope;
1603 429 : struct c_binding *b = I_SYMBOL_BINDING (name);
1604 429 : gcc_assert (b);
1605 429 : gcc_assert (B_IN_SCOPE (b, scope));
1606 429 : gcc_assert (VAR_P (b->decl));
1607 429 : gcc_assert (C_DECL_UNDERSPECIFIED (b->decl));
1608 429 : I_SYMBOL_BINDING (name) = b->shadowed;
1609 : /* In erroneous cases there may be other bindings added to this
1610 : scope during the initializer. */
1611 429 : struct c_binding **p = &scope->bindings;
1612 484 : while (*p != b)
1613 55 : p = &((*p)->prev);
1614 429 : *p = free_binding_and_advance (*p);
1615 : }
1616 667 : in_underspecified_init = (prev_state & (1u << 1)) >> 1;
1617 667 : }
1618 :
1619 : /* Adjust the bindings for the start of a statement expression. */
1620 :
1621 : void
1622 34943 : c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1623 : {
1624 34943 : struct c_scope *scope;
1625 :
1626 236050 : for (scope = current_scope; scope != NULL; scope = scope->outer)
1627 : {
1628 201107 : struct c_binding *b;
1629 :
1630 201107 : if (!scope->has_label_bindings)
1631 198680 : continue;
1632 :
1633 13176 : for (b = scope->bindings; b != NULL; b = b->prev)
1634 : {
1635 10749 : struct c_label_vars *label_vars;
1636 10749 : unsigned int ix;
1637 10749 : struct c_goto_bindings *g;
1638 :
1639 10749 : if (TREE_CODE (b->decl) != LABEL_DECL)
1640 1948 : continue;
1641 8801 : label_vars = b->u.label;
1642 8801 : ++label_vars->label_bindings.stmt_exprs;
1643 13405 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1644 1891 : ++g->goto_bindings.stmt_exprs;
1645 : }
1646 : }
1647 :
1648 34943 : if (switch_bindings != NULL)
1649 173 : ++switch_bindings->stmt_exprs;
1650 34943 : }
1651 :
1652 : /* Adjust the bindings for the end of a statement expression. */
1653 :
1654 : void
1655 34943 : c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1656 : {
1657 34943 : struct c_scope *scope;
1658 :
1659 201107 : for (scope = current_scope; scope != NULL; scope = scope->outer)
1660 : {
1661 166164 : struct c_binding *b;
1662 :
1663 166164 : if (!scope->has_label_bindings)
1664 163502 : continue;
1665 :
1666 16656 : for (b = scope->bindings; b != NULL; b = b->prev)
1667 : {
1668 13994 : struct c_label_vars *label_vars;
1669 13994 : unsigned int ix;
1670 13994 : struct c_goto_bindings *g;
1671 :
1672 13994 : if (TREE_CODE (b->decl) != LABEL_DECL)
1673 2012 : continue;
1674 11982 : label_vars = b->u.label;
1675 11982 : --label_vars->label_bindings.stmt_exprs;
1676 11982 : if (label_vars->label_bindings.stmt_exprs < 0)
1677 : {
1678 3322 : label_vars->label_bindings.left_stmt_expr = true;
1679 3322 : label_vars->label_bindings.stmt_exprs = 0;
1680 : }
1681 16642 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1682 : {
1683 1887 : --g->goto_bindings.stmt_exprs;
1684 1887 : if (g->goto_bindings.stmt_exprs < 0)
1685 : {
1686 128 : g->goto_bindings.left_stmt_expr = true;
1687 128 : g->goto_bindings.stmt_exprs = 0;
1688 : }
1689 : }
1690 : }
1691 : }
1692 :
1693 34943 : if (switch_bindings != NULL)
1694 : {
1695 173 : --switch_bindings->stmt_exprs;
1696 173 : gcc_assert (switch_bindings->stmt_exprs >= 0);
1697 : }
1698 34943 : }
1699 :
1700 : /* Push a definition or a declaration of struct, union or enum tag "name".
1701 : "type" should be the type node.
1702 : We assume that the tag "name" is not already defined, and has a location
1703 : of LOC.
1704 :
1705 : Note that the definition may really be just a forward reference.
1706 : In that case, the TYPE_SIZE will be zero. */
1707 :
1708 : static void
1709 1436544 : pushtag (location_t loc, tree name, tree type)
1710 : {
1711 : /* Record the identifier as the type's name if it has none. */
1712 1436544 : if (name && !TYPE_NAME (type))
1713 631864 : TYPE_NAME (type) = name;
1714 1436544 : bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1715 :
1716 : /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1717 : tagged type we just added to the current scope. This fake
1718 : NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1719 : to output a representation of a tagged type, and it also gives
1720 : us a convenient place to record the "scope start" address for the
1721 : tagged type, and it is used to track whether the type is used
1722 : in a non-local context via mark_decl_used. */
1723 :
1724 1436544 : TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1725 : TYPE_DECL, NULL_TREE, type));
1726 :
1727 : /* An approximation for now, so we can tell this is a function-scope tag.
1728 : This will be updated in pop_scope. */
1729 1436544 : TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1730 :
1731 1436544 : if (warn_cxx_compat && name != NULL_TREE)
1732 : {
1733 640 : struct c_binding *b = I_SYMBOL_BINDING (name);
1734 :
1735 640 : if (b != NULL
1736 12 : && b->decl != NULL_TREE
1737 12 : && TREE_CODE (b->decl) == TYPE_DECL
1738 11 : && (B_IN_CURRENT_SCOPE (b)
1739 6 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1740 645 : && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1741 5 : != TYPE_MAIN_VARIANT (type)))
1742 : {
1743 5 : auto_diagnostic_group d;
1744 6 : if (warning_at (loc, OPT_Wc___compat,
1745 : "using %qD as both a typedef and a tag is "
1746 : "invalid in C++", b->decl)
1747 5 : && b->locus != UNKNOWN_LOCATION)
1748 4 : inform (b->locus, "originally defined here");
1749 5 : }
1750 : }
1751 1436544 : }
1752 :
1753 : /* An exported interface to pushtag. This is used by the gdb plugin's
1754 : binding oracle to introduce a new tag binding. */
1755 :
1756 : void
1757 0 : c_pushtag (location_t loc, tree name, tree type)
1758 : {
1759 0 : pushtag (loc, name, type);
1760 0 : }
1761 :
1762 : /* An exported interface to bind a declaration. LOC is the location
1763 : to use. DECL is the declaration to bind. The decl's name is used
1764 : to determine how it is bound. If DECL is a VAR_DECL, then
1765 : IS_GLOBAL determines whether the decl is put into the global (file
1766 : and external) scope or the current function's scope; if DECL is not
1767 : a VAR_DECL then it is always put into the file scope. */
1768 :
1769 : void
1770 0 : c_bind (location_t loc, tree decl, bool is_global)
1771 : {
1772 0 : struct c_scope *scope;
1773 0 : bool nested = false;
1774 :
1775 0 : if (!VAR_P (decl) || current_function_scope == NULL)
1776 : {
1777 : /* Types and functions are always considered to be global. */
1778 0 : scope = file_scope;
1779 0 : DECL_EXTERNAL (decl) = 1;
1780 0 : TREE_PUBLIC (decl) = 1;
1781 : }
1782 0 : else if (is_global)
1783 : {
1784 : /* Also bind it into the external scope. */
1785 0 : bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1786 0 : nested = true;
1787 0 : scope = file_scope;
1788 0 : DECL_EXTERNAL (decl) = 1;
1789 0 : TREE_PUBLIC (decl) = 1;
1790 : }
1791 : else
1792 : {
1793 0 : DECL_CONTEXT (decl) = current_function_decl;
1794 0 : TREE_PUBLIC (decl) = 0;
1795 0 : scope = current_function_scope;
1796 : }
1797 :
1798 0 : bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1799 0 : }
1800 :
1801 :
1802 : /* Stores the first FILE*, const struct tm* etc. argument type (whatever
1803 : it is) seen in a declaration of a file I/O etc. built-in, corresponding
1804 : to the builtin_structptr_types array. Subsequent declarations of such
1805 : built-ins are expected to refer to it rather than to fileptr_type_node,
1806 : etc. which is just void* (or to any other type).
1807 : Used only by match_builtin_function_types. */
1808 :
1809 : static const unsigned builtin_structptr_type_count
1810 : = ARRAY_SIZE (builtin_structptr_types);
1811 :
1812 : static GTY(()) tree last_structptr_types[builtin_structptr_type_count];
1813 :
1814 : /* Returns true if types T1 and T2 representing return types or types
1815 : of function arguments are close enough to be considered interchangeable
1816 : in redeclarations of built-in functions. */
1817 :
1818 : static bool
1819 628331 : types_close_enough_to_match (tree t1, tree t2)
1820 : {
1821 628331 : return (TYPE_MODE (t1) == TYPE_MODE (t2)
1822 627877 : && POINTER_TYPE_P (t1) == POINTER_TYPE_P (t2)
1823 2511809 : && FUNCTION_POINTER_TYPE_P (t1) == FUNCTION_POINTER_TYPE_P (t2));
1824 : }
1825 :
1826 : /* Subroutine of compare_decls. Allow harmless mismatches in return
1827 : and argument types provided that the type modes match. Set *STRICT
1828 : and *ARGNO to the expected argument type and number in case of
1829 : an argument type mismatch or null and zero otherwise. Return
1830 : a unified type given a suitable match, and 0 otherwise. */
1831 :
1832 : static tree
1833 145712 : match_builtin_function_types (tree newtype, tree oldtype,
1834 : tree *strict, unsigned *argno)
1835 : {
1836 145712 : *argno = 0;
1837 145712 : *strict = NULL_TREE;
1838 :
1839 : /* Accept the return type of the new declaration if it has the same
1840 : mode and if they're both pointers or if neither is. */
1841 145712 : tree oldrettype = TREE_TYPE (oldtype);
1842 145712 : tree newrettype = TREE_TYPE (newtype);
1843 :
1844 145712 : if (!types_close_enough_to_match (oldrettype, newrettype))
1845 : return NULL_TREE;
1846 :
1847 : /* Check that the return types are compatible but don't fail if they
1848 : are not (e.g., int vs long in ILP32) and just let the caller know. */
1849 145350 : if (!comptypes (TYPE_MAIN_VARIANT (oldrettype),
1850 145350 : TYPE_MAIN_VARIANT (newrettype)))
1851 46 : *strict = oldrettype;
1852 :
1853 145350 : tree oldargs = TYPE_ARG_TYPES (oldtype);
1854 145350 : tree newargs = TYPE_ARG_TYPES (newtype);
1855 145350 : tree tryargs = newargs;
1856 :
1857 145350 : const unsigned nlst = ARRAY_SIZE (last_structptr_types);
1858 145350 : const unsigned nbst = ARRAY_SIZE (builtin_structptr_types);
1859 :
1860 145350 : gcc_checking_assert (nlst == nbst);
1861 :
1862 627773 : for (unsigned i = 1; oldargs || newargs; ++i)
1863 : {
1864 482678 : if (!oldargs
1865 482678 : || !newargs
1866 482623 : || !TREE_VALUE (oldargs)
1867 965301 : || !TREE_VALUE (newargs))
1868 : return NULL_TREE;
1869 :
1870 482623 : tree oldtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1871 482623 : tree newtype = TREE_VALUE (newargs);
1872 482623 : if (newtype == error_mark_node)
1873 : return NULL_TREE;
1874 482619 : newtype = TYPE_MAIN_VARIANT (newtype);
1875 :
1876 482619 : if (!types_close_enough_to_match (oldtype, newtype))
1877 : return NULL_TREE;
1878 :
1879 482476 : unsigned j = nbst;
1880 482476 : if (POINTER_TYPE_P (oldtype))
1881 : /* Iterate over well-known struct types like FILE (whose types
1882 : aren't known to us) and compare the pointer to each to
1883 : the pointer argument. */
1884 985517 : for (j = 0; j < nbst; ++j)
1885 : {
1886 868205 : if (TREE_VALUE (oldargs) != builtin_structptr_types[j].node)
1887 723325 : continue;
1888 : /* Store the first FILE* etc. argument type (whatever it is), and
1889 : expect any subsequent declarations of file I/O etc. built-ins
1890 : to refer to it rather than to fileptr_type_node etc. which is
1891 : just void* (or const void*). */
1892 144880 : if (last_structptr_types[j])
1893 : {
1894 127295 : if (!comptypes (last_structptr_types[j], newtype))
1895 : {
1896 2 : *argno = i;
1897 2 : *strict = last_structptr_types[j];
1898 : }
1899 : }
1900 : else
1901 17585 : last_structptr_types[j] = newtype;
1902 : break;
1903 : }
1904 :
1905 482476 : if (j == nbst && !comptypes (oldtype, newtype))
1906 : {
1907 243 : if (POINTER_TYPE_P (oldtype))
1908 : {
1909 : /* For incompatible pointers, only reject differences in
1910 : the unqualified variants of the referenced types but
1911 : consider differences in qualifiers as benign (report
1912 : those to caller via *STRICT below). */
1913 204 : tree oldref = TYPE_MAIN_VARIANT (TREE_TYPE (oldtype));
1914 204 : tree newref = TYPE_MAIN_VARIANT (TREE_TYPE (newtype));
1915 204 : if (!comptypes (oldref, newref))
1916 : return NULL_TREE;
1917 : }
1918 :
1919 190 : if (!*strict)
1920 : {
1921 186 : *argno = i;
1922 186 : *strict = oldtype;
1923 : }
1924 : }
1925 :
1926 482423 : oldargs = TREE_CHAIN (oldargs);
1927 482423 : newargs = TREE_CHAIN (newargs);
1928 : }
1929 :
1930 145095 : tree trytype = c_build_function_type (newrettype, tryargs);
1931 :
1932 : /* Allow declaration to change transaction_safe attribute. */
1933 145095 : tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1934 145095 : tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1935 145095 : tree newattrs = TYPE_ATTRIBUTES (newtype);
1936 145095 : tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1937 145095 : if (oldtsafe && !newtsafe)
1938 0 : oldattrs = remove_attribute ("transaction_safe", oldattrs);
1939 145095 : else if (newtsafe && !oldtsafe)
1940 7 : oldattrs = tree_cons (get_identifier ("transaction_safe"),
1941 : NULL_TREE, oldattrs);
1942 :
1943 145095 : return c_build_type_attribute_variant (trytype, oldattrs);
1944 : }
1945 :
1946 : /* Subroutine of diagnose_mismatched_decls. Check for function type
1947 : mismatch involving an empty arglist vs a nonempty one and give clearer
1948 : diagnostics. */
1949 : static void
1950 178 : diagnose_arglist_conflict (tree newdecl, tree olddecl,
1951 : tree newtype, tree oldtype)
1952 : {
1953 178 : tree t;
1954 :
1955 178 : if (TREE_CODE (olddecl) != FUNCTION_DECL
1956 98 : || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1957 305 : || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1958 68 : || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1959 161 : return;
1960 :
1961 17 : t = TYPE_ARG_TYPES (oldtype);
1962 17 : if (t == NULL_TREE)
1963 9 : t = TYPE_ARG_TYPES (newtype);
1964 18 : for (; t; t = TREE_CHAIN (t))
1965 : {
1966 18 : tree type = TREE_VALUE (t);
1967 :
1968 18 : if (TREE_CHAIN (t) == NULL_TREE
1969 18 : && TYPE_MAIN_VARIANT (type) != void_type_node)
1970 : {
1971 10 : inform (input_location, "a parameter list with an ellipsis "
1972 : "cannot match an empty parameter name list declaration");
1973 10 : break;
1974 : }
1975 :
1976 8 : if (!error_operand_p (type)
1977 8 : && c_type_promotes_to (type) != type)
1978 : {
1979 7 : inform (input_location, "an argument type that has a default "
1980 : "promotion cannot match an empty parameter name list "
1981 : "declaration");
1982 7 : break;
1983 : }
1984 : }
1985 : }
1986 :
1987 : /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1988 : old-style function definition, NEWDECL is a prototype declaration.
1989 : Diagnose inconsistencies in the argument list. Returns TRUE if
1990 : the prototype is compatible, FALSE if not. */
1991 : static bool
1992 19 : validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1993 : {
1994 19 : tree newargs, oldargs;
1995 19 : int i;
1996 :
1997 : #define END_OF_ARGLIST(t) ((t) == void_type_node)
1998 :
1999 19 : oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
2000 19 : newargs = TYPE_ARG_TYPES (newtype);
2001 19 : i = 1;
2002 :
2003 45 : for (;;)
2004 : {
2005 32 : tree oldargtype = TREE_VALUE (oldargs);
2006 32 : tree newargtype = TREE_VALUE (newargs);
2007 :
2008 32 : if (oldargtype == error_mark_node || newargtype == error_mark_node)
2009 : return false;
2010 :
2011 58 : oldargtype = (TYPE_ATOMIC (oldargtype)
2012 31 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
2013 : TYPE_QUAL_ATOMIC)
2014 27 : : TYPE_MAIN_VARIANT (oldargtype));
2015 60 : newargtype = (TYPE_ATOMIC (newargtype)
2016 31 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
2017 : TYPE_QUAL_ATOMIC)
2018 29 : : TYPE_MAIN_VARIANT (newargtype));
2019 :
2020 31 : if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
2021 : break;
2022 :
2023 : /* Reaching the end of just one list means the two decls don't
2024 : agree on the number of arguments. */
2025 22 : if (END_OF_ARGLIST (oldargtype))
2026 : {
2027 2 : error ("prototype for %q+D declares more arguments "
2028 : "than previous old-style definition", newdecl);
2029 2 : return false;
2030 : }
2031 20 : else if (END_OF_ARGLIST (newargtype))
2032 : {
2033 2 : error ("prototype for %q+D declares fewer arguments "
2034 : "than previous old-style definition", newdecl);
2035 2 : return false;
2036 : }
2037 :
2038 : /* Type for passing arg must be consistent with that declared
2039 : for the arg. */
2040 18 : else if (!comptypes (oldargtype, newargtype))
2041 : {
2042 5 : error ("prototype for %q+D declares argument %d"
2043 : " with incompatible type",
2044 : newdecl, i);
2045 5 : return false;
2046 : }
2047 :
2048 13 : oldargs = TREE_CHAIN (oldargs);
2049 13 : newargs = TREE_CHAIN (newargs);
2050 13 : i++;
2051 13 : }
2052 :
2053 : /* If we get here, no errors were found, but do issue a warning
2054 : for this poor-style construct. */
2055 9 : warning (0, "prototype for %q+D follows non-prototype definition",
2056 : newdecl);
2057 9 : return true;
2058 : #undef END_OF_ARGLIST
2059 : }
2060 :
2061 : /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
2062 : first in a pair of mismatched declarations, using the diagnostic
2063 : function DIAG. */
2064 : static void
2065 418 : locate_old_decl (tree decl)
2066 : {
2067 418 : if (TREE_CODE (decl) == FUNCTION_DECL
2068 198 : && fndecl_built_in_p (decl)
2069 421 : && !C_DECL_DECLARED_BUILTIN (decl))
2070 : ;
2071 418 : else if (DECL_INITIAL (decl))
2072 119 : inform (input_location,
2073 : "previous definition of %q+D with type %qT",
2074 119 : decl, TREE_TYPE (decl));
2075 299 : else if (C_DECL_IMPLICIT (decl))
2076 16 : inform (input_location,
2077 : "previous implicit declaration of %q+D with type %qT",
2078 16 : decl, TREE_TYPE (decl));
2079 : else
2080 283 : inform (input_location,
2081 : "previous declaration of %q+D with type %qT",
2082 283 : decl, TREE_TYPE (decl));
2083 418 : }
2084 :
2085 :
2086 : /* Helper function. For a tagged type, it finds the declaration
2087 : for a visible tag declared in the same scope if such a
2088 : declaration exists. */
2089 : static tree
2090 1146862 : previous_tag (tree type)
2091 : {
2092 1146862 : struct c_binding *b = NULL;
2093 1146862 : tree name = c_type_tag (type);
2094 :
2095 1146862 : if (name)
2096 571238 : b = I_TAG_BINDING (name);
2097 :
2098 571238 : if (b)
2099 571238 : b = b->shadowed;
2100 :
2101 1146862 : if (b && B_IN_CURRENT_SCOPE (b))
2102 177 : return b->decl;
2103 :
2104 : return NULL_TREE;
2105 : }
2106 :
2107 : /* Subroutine to mark functions as versioned when using the attribute
2108 : 'target_version'. */
2109 :
2110 : static void
2111 0 : maybe_mark_function_versioned (tree decl)
2112 : {
2113 0 : if (!DECL_FUNCTION_VERSIONED (decl))
2114 : {
2115 : /* Check if the name of the function has been overridden. */
2116 0 : if (DECL_ASSEMBLER_NAME_SET_P (decl)
2117 0 : && IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))[0] == '*')
2118 0 : error_at (DECL_SOURCE_LOCATION (decl),
2119 : "cannot use function multiversioning on a renamed function");
2120 :
2121 : /* We need to insert function version now to make sure the correct
2122 : pre-mangled assembler name is recorded. */
2123 0 : cgraph_node *node = cgraph_node::get_create (decl);
2124 :
2125 0 : if (!node->function_version ())
2126 0 : node->insert_new_function_version ();
2127 :
2128 0 : DECL_FUNCTION_VERSIONED (decl) = 1;
2129 :
2130 0 : tree mangled_name
2131 0 : = targetm.mangle_decl_assembler_name (decl, DECL_NAME (decl));
2132 0 : SET_DECL_ASSEMBLER_NAME (decl, mangled_name);
2133 : }
2134 0 : }
2135 :
2136 : /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
2137 : Returns true if the caller should proceed to merge the two, false
2138 : if OLDDECL should simply be discarded. As a side effect, issues
2139 : all necessary diagnostics for invalid or poor-style combinations.
2140 : If it returns true, writes the types of NEWDECL and OLDDECL to
2141 : *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
2142 : TREE_TYPE (NEWDECL, OLDDECL) respectively. */
2143 :
2144 : static bool
2145 5113454 : diagnose_mismatched_decls (tree newdecl, tree olddecl,
2146 : tree *newtypep, tree *oldtypep)
2147 : {
2148 5113454 : tree newtype, oldtype;
2149 5113454 : bool retval = true;
2150 :
2151 : #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
2152 : && DECL_EXTERNAL (DECL))
2153 :
2154 : /* If we have error_mark_node for either decl or type, just discard
2155 : the previous decl - we're in an error cascade already. */
2156 5113454 : if (olddecl == error_mark_node || newdecl == error_mark_node)
2157 : return false;
2158 5113434 : *oldtypep = oldtype = TREE_TYPE (olddecl);
2159 5113434 : *newtypep = newtype = TREE_TYPE (newdecl);
2160 5113434 : if (oldtype == error_mark_node || newtype == error_mark_node)
2161 : return false;
2162 :
2163 : /* Two different categories of symbol altogether. This is an error
2164 : unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
2165 5113426 : if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2166 : {
2167 29 : if (!(TREE_CODE (olddecl) == FUNCTION_DECL
2168 17 : && fndecl_built_in_p (olddecl)
2169 14 : && !C_DECL_DECLARED_BUILTIN (olddecl)))
2170 : {
2171 16 : auto_diagnostic_group d;
2172 16 : error ("%q+D redeclared as different kind of symbol", newdecl);
2173 16 : locate_old_decl (olddecl);
2174 16 : }
2175 13 : else if (TREE_PUBLIC (newdecl))
2176 3 : warning (OPT_Wbuiltin_declaration_mismatch,
2177 : "built-in function %q+D declared as non-function",
2178 : newdecl);
2179 : else
2180 10 : warning (OPT_Wshadow, "declaration of %q+D shadows "
2181 : "a built-in function", newdecl);
2182 29 : return false;
2183 : }
2184 :
2185 : /* Enumerators have no linkage, so may only be declared once in a
2186 : given scope. */
2187 5113397 : if (TREE_CODE (olddecl) == CONST_DECL)
2188 : {
2189 46 : if (flag_isoc23
2190 45 : && TYPE_NAME (DECL_CONTEXT (newdecl))
2191 43 : && DECL_CONTEXT (newdecl) != DECL_CONTEXT (olddecl)
2192 87 : && TYPE_NAME (DECL_CONTEXT (newdecl)) == TYPE_NAME (DECL_CONTEXT (olddecl)))
2193 : {
2194 38 : if (!simple_cst_equal (DECL_INITIAL (olddecl), DECL_INITIAL (newdecl)))
2195 : {
2196 1 : auto_diagnostic_group d;
2197 1 : error ("conflicting redeclaration of enumerator %q+D", newdecl);
2198 1 : locate_old_decl (olddecl);
2199 1 : }
2200 : }
2201 : else
2202 : {
2203 8 : auto_diagnostic_group d;
2204 8 : error ("redeclaration of enumerator %q+D", newdecl);
2205 8 : locate_old_decl (olddecl);
2206 8 : }
2207 46 : return false;
2208 : }
2209 :
2210 5113351 : bool pedwarned = false;
2211 5113351 : bool warned = false;
2212 5113351 : bool enum_and_int_p = false;
2213 5113351 : auto_diagnostic_group d;
2214 :
2215 5113351 : bool comptypes_result = comptypes_check_enum_int (oldtype, newtype,
2216 : &enum_and_int_p);
2217 5113351 : if (!comptypes_result)
2218 : {
2219 145921 : if (TREE_CODE (olddecl) == FUNCTION_DECL
2220 145841 : && fndecl_built_in_p (olddecl, BUILT_IN_NORMAL)
2221 291634 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2222 : {
2223 : /* Accept "harmless" mismatches in function types such
2224 : as missing qualifiers or int vs long when they're the same
2225 : size. However, diagnose return and argument types that are
2226 : incompatible according to language rules. */
2227 145712 : tree mismatch_expect;
2228 145712 : unsigned mismatch_argno;
2229 :
2230 145712 : tree trytype = match_builtin_function_types (newtype, oldtype,
2231 : &mismatch_expect,
2232 : &mismatch_argno);
2233 :
2234 145712 : if (trytype && comptypes (newtype, trytype))
2235 145095 : *oldtypep = oldtype = trytype;
2236 : else
2237 : {
2238 : /* If types don't match for a built-in, throw away the
2239 : built-in. No point in calling locate_old_decl here, it
2240 : won't print anything. */
2241 617 : const char *header = header_for_builtin_fn (olddecl);
2242 617 : location_t loc = DECL_SOURCE_LOCATION (newdecl);
2243 1017 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
2244 : "conflicting types for built-in function %q+D; "
2245 : "expected %qT",
2246 : newdecl, oldtype)
2247 617 : && header)
2248 : {
2249 : /* Suggest the right header to include as the preferred
2250 : solution rather than the spelling of the declaration. */
2251 217 : rich_location richloc (line_table, loc);
2252 217 : maybe_add_include_fixit (&richloc, header, true);
2253 217 : inform (&richloc,
2254 : "%qD is declared in header %qs", olddecl, header);
2255 217 : }
2256 617 : return false;
2257 : }
2258 :
2259 145095 : if (mismatch_expect && extra_warnings)
2260 : {
2261 5 : location_t newloc = DECL_SOURCE_LOCATION (newdecl);
2262 5 : bool warned = false;
2263 5 : if (mismatch_argno)
2264 5 : warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2265 : "mismatch in argument %u type of built-in "
2266 : "function %qD; expected %qT",
2267 : mismatch_argno, newdecl, mismatch_expect);
2268 : else
2269 0 : warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2270 : "mismatch in return type of built-in "
2271 : "function %qD; expected %qT",
2272 : newdecl, mismatch_expect);
2273 5 : const char *header = header_for_builtin_fn (olddecl);
2274 5 : if (warned && header)
2275 : {
2276 5 : rich_location richloc (line_table, newloc);
2277 5 : maybe_add_include_fixit (&richloc, header, true);
2278 5 : inform (&richloc,
2279 : "%qD is declared in header %qs", olddecl, header);
2280 5 : }
2281 : }
2282 : }
2283 209 : else if (TREE_CODE (olddecl) == FUNCTION_DECL
2284 209 : && DECL_IS_UNDECLARED_BUILTIN (olddecl))
2285 : {
2286 : /* A conflicting function declaration for a predeclared
2287 : function that isn't actually built in. Objective C uses
2288 : these. The new declaration silently overrides everything
2289 : but the volatility (i.e. noreturn) indication. See also
2290 : below. FIXME: Make Objective C use normal builtins. */
2291 0 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2292 0 : return false;
2293 : }
2294 : /* Permit void foo (...) to match int foo (...) if the latter is
2295 : the definition and implicit int was used. See
2296 : c-torture/compile/920625-2.c. */
2297 129 : else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
2298 64 : && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
2299 32 : && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
2300 215 : && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
2301 : {
2302 5 : pedwarned = pedwarn (input_location, 0,
2303 : "conflicting types for %q+D", newdecl);
2304 : /* Make sure we keep void as the return type. */
2305 5 : TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
2306 5 : C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
2307 : }
2308 : /* Permit void foo (...) to match an earlier call to foo (...) with
2309 : no declared type (thus, implicitly int). */
2310 204 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2311 124 : && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
2312 92 : && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
2313 227 : && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
2314 : {
2315 19 : pedwarned = pedwarn (input_location, 0,
2316 : "conflicting types for %q+D; have %qT",
2317 : newdecl, newtype);
2318 : /* Make sure we keep void as the return type. */
2319 19 : TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
2320 : }
2321 : else
2322 : {
2323 185 : int new_quals = TYPE_QUALS (newtype);
2324 185 : int old_quals = TYPE_QUALS (oldtype);
2325 :
2326 185 : if (new_quals != old_quals)
2327 : {
2328 21 : addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
2329 21 : addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
2330 21 : if (new_addr != old_addr)
2331 : {
2332 0 : if (ADDR_SPACE_GENERIC_P (new_addr))
2333 0 : error ("conflicting named address spaces (generic vs %s) "
2334 : "for %q+D",
2335 : c_addr_space_name (old_addr), newdecl);
2336 0 : else if (ADDR_SPACE_GENERIC_P (old_addr))
2337 0 : error ("conflicting named address spaces (%s vs generic) "
2338 : "for %q+D",
2339 : c_addr_space_name (new_addr), newdecl);
2340 : else
2341 0 : error ("conflicting named address spaces (%s vs %s) "
2342 : "for %q+D",
2343 : c_addr_space_name (new_addr),
2344 : c_addr_space_name (old_addr),
2345 : newdecl);
2346 : }
2347 :
2348 21 : if (CLEAR_QUAL_ADDR_SPACE (new_quals)
2349 21 : != CLEAR_QUAL_ADDR_SPACE (old_quals))
2350 21 : error ("conflicting type qualifiers for %q+D", newdecl);
2351 : }
2352 : else
2353 : {
2354 164 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
2355 : {
2356 93 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (olddecl));
2357 93 : if (attrs && !TYPE_ATTRIBUTES (TREE_TYPE (newdecl)))
2358 : {
2359 : /* Similar to the C++ front-end, for FUNCTION_DECL,
2360 : if OLDDECL has attributes and NEWDECL doesn't,
2361 : try the type with OLDDECL attributes. */
2362 12 : tree rettype = TREE_TYPE (newtype);
2363 12 : tree tryargs = TYPE_ARG_TYPES (newtype);
2364 12 : tree trytype = c_build_function_type (rettype,
2365 : tryargs);
2366 12 : trytype = c_build_type_attribute_variant (trytype,
2367 : attrs);
2368 12 : if (comptypes (oldtype, trytype))
2369 : {
2370 7 : *newtypep = newtype = trytype;
2371 7 : comptypes_result = true;
2372 : }
2373 : }
2374 : }
2375 :
2376 164 : if (!comptypes_result)
2377 157 : error ("conflicting types for %q+D; have %qT", newdecl,
2378 : newtype);
2379 : }
2380 7 : if (!comptypes_result)
2381 : {
2382 178 : diagnose_arglist_conflict (newdecl, olddecl, newtype,
2383 : oldtype);
2384 178 : locate_old_decl (olddecl);
2385 178 : return false;
2386 : }
2387 : }
2388 : }
2389 : /* Warn about enum/integer type mismatches. They are compatible types
2390 : (C23 6.7.2.2/5), but may pose portability problems. */
2391 4967430 : else if (enum_and_int_p
2392 191 : && TREE_CODE (newdecl) != TYPE_DECL
2393 : /* Don't warn about acc_on_device built-in redeclaration,
2394 : the built-in is declared with int rather than enum because
2395 : the enum isn't intrinsic. */
2396 4967617 : && !(TREE_CODE (olddecl) == FUNCTION_DECL
2397 150 : && fndecl_built_in_p (olddecl, BUILT_IN_ACC_ON_DEVICE)
2398 120 : && !C_DECL_DECLARED_BUILTIN (olddecl)))
2399 67 : warned = warning_at (DECL_SOURCE_LOCATION (newdecl),
2400 67 : OPT_Wenum_int_mismatch,
2401 : "conflicting types for %q+D due to enum/integer "
2402 : "mismatch; have %qT", newdecl, newtype);
2403 :
2404 : /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2405 : but silently ignore the redeclaration if either is in a system
2406 : header. (Conflicting redeclarations were handled above.) This
2407 : is allowed for C11 if the types are the same, not just
2408 : compatible. */
2409 5112556 : if (TREE_CODE (newdecl) == TYPE_DECL)
2410 : {
2411 45898 : if (!comptypes_same_p (oldtype, newtype))
2412 : {
2413 11 : error ("redefinition of typedef %q+D with different type", newdecl);
2414 11 : locate_old_decl (olddecl);
2415 11 : return false;
2416 : }
2417 :
2418 45887 : if (DECL_IN_SYSTEM_HEADER (newdecl)
2419 2497 : || DECL_IN_SYSTEM_HEADER (olddecl)
2420 2275 : || warning_suppressed_p (newdecl, OPT_Wpedantic)
2421 48162 : || warning_suppressed_p (olddecl, OPT_Wpedantic))
2422 43612 : return true; /* Allow OLDDECL to continue in use. */
2423 :
2424 2275 : if (c_type_variably_modified_p (newtype))
2425 : {
2426 3 : error ("redefinition of typedef %q+D with variably modified type",
2427 : newdecl);
2428 3 : locate_old_decl (olddecl);
2429 : }
2430 2272 : else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2431 : "redefinition of typedef %q+D", newdecl))
2432 8 : locate_old_decl (olddecl);
2433 :
2434 2275 : return true;
2435 : }
2436 :
2437 : /* Function declarations can either be 'static' or 'extern' (no
2438 : qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2439 : can never conflict with each other on account of linkage
2440 : (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2441 : gnu89 mode permits two definitions if one is 'extern inline' and
2442 : one is not. The non- extern-inline definition supersedes the
2443 : extern-inline definition. */
2444 :
2445 5066658 : else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2446 : {
2447 : /* If you declare a built-in function name as static, or
2448 : define the built-in with an old-style definition (so we
2449 : can't validate the argument list) the built-in definition is
2450 : overridden, but optionally warn this was a bad choice of name. */
2451 5047963 : if (fndecl_built_in_p (olddecl)
2452 9042450 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2453 : {
2454 3729410 : if (!TREE_PUBLIC (newdecl)
2455 3729410 : || (DECL_INITIAL (newdecl)
2456 4861 : && !prototype_p (TREE_TYPE (newdecl))))
2457 : {
2458 104 : warning_at (DECL_SOURCE_LOCATION (newdecl),
2459 104 : OPT_Wshadow, "declaration of %qD shadows "
2460 : "a built-in function", newdecl);
2461 : /* Discard the old built-in function. */
2462 104 : return false;
2463 : }
2464 :
2465 3729306 : if (!prototype_p (TREE_TYPE (newdecl)))
2466 : {
2467 : /* Set for built-ins that take no arguments. */
2468 350 : bool func_void_args = false;
2469 350 : if (tree at = TYPE_ARG_TYPES (oldtype))
2470 350 : func_void_args = VOID_TYPE_P (TREE_VALUE (at));
2471 :
2472 350 : if (extra_warnings && !func_void_args)
2473 47 : warning_at (DECL_SOURCE_LOCATION (newdecl),
2474 47 : OPT_Wbuiltin_declaration_mismatch,
2475 : "declaration of built-in function %qD without "
2476 : "a prototype; expected %qT",
2477 47 : newdecl, TREE_TYPE (olddecl));
2478 : }
2479 : }
2480 :
2481 5047859 : if (DECL_INITIAL (newdecl))
2482 : {
2483 233872 : if (DECL_INITIAL (olddecl))
2484 : {
2485 : /* If the new declaration isn't overriding an extern inline
2486 : reject the new decl. In c99, no overriding is allowed
2487 : in the same translation unit. */
2488 209 : if (!DECL_EXTERN_INLINE (olddecl)
2489 91 : || DECL_EXTERN_INLINE (newdecl)
2490 204 : || (!flag_gnu89_inline
2491 15 : && (!DECL_DECLARED_INLINE_P (olddecl)
2492 15 : || !lookup_attribute ("gnu_inline",
2493 15 : DECL_ATTRIBUTES (olddecl)))
2494 0 : && (!DECL_DECLARED_INLINE_P (newdecl)
2495 0 : || !lookup_attribute ("gnu_inline",
2496 0 : DECL_ATTRIBUTES (newdecl)))))
2497 : {
2498 26 : auto_diagnostic_group d;
2499 26 : error ("redefinition of %q+D", newdecl);
2500 26 : locate_old_decl (olddecl);
2501 26 : return false;
2502 26 : }
2503 : }
2504 : }
2505 : /* If we have a prototype after an old-style function definition,
2506 : the argument types must be checked specially. */
2507 4813987 : else if (DECL_INITIAL (olddecl)
2508 824 : && !prototype_p (oldtype) && prototype_p (newtype)
2509 4814007 : && TYPE_ACTUAL_ARG_TYPES (oldtype))
2510 : {
2511 19 : auto_diagnostic_group d;
2512 19 : if (!validate_proto_after_old_defn (newdecl, newtype, oldtype))
2513 : {
2514 10 : locate_old_decl (olddecl);
2515 10 : return false;
2516 : }
2517 19 : }
2518 : /* A non-static declaration (even an "extern") followed by a
2519 : static declaration is undefined behavior per C99 6.2.2p3-5,7.
2520 : The same is true for a static forward declaration at block
2521 : scope followed by a non-static declaration/definition at file
2522 : scope. Static followed by non-static at the same scope is
2523 : not undefined behavior, and is the most convenient way to get
2524 : some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2525 : the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2526 : we do diagnose it if -Wtraditional. */
2527 5047823 : if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2528 : {
2529 : /* Two exceptions to the rule. If olddecl is an extern
2530 : inline, or a predeclared function that isn't actually
2531 : built in, newdecl silently overrides olddecl. The latter
2532 : occur only in Objective C; see also above. (FIXME: Make
2533 : Objective C use normal builtins.) */
2534 19 : if (!DECL_IS_UNDECLARED_BUILTIN (olddecl)
2535 38 : && !DECL_EXTERN_INLINE (olddecl))
2536 : {
2537 5 : auto_diagnostic_group d;
2538 5 : error ("static declaration of %q+D follows "
2539 : "non-static declaration", newdecl);
2540 5 : locate_old_decl (olddecl);
2541 5 : }
2542 19 : return false;
2543 : }
2544 5047804 : else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2545 : {
2546 2329 : if (DECL_CONTEXT (olddecl))
2547 : {
2548 0 : auto_diagnostic_group d;
2549 0 : error ("non-static declaration of %q+D follows "
2550 : "static declaration", newdecl);
2551 0 : locate_old_decl (olddecl);
2552 0 : return false;
2553 0 : }
2554 2329 : else if (warn_traditional)
2555 : {
2556 2 : warned |= warning (OPT_Wtraditional,
2557 : "non-static declaration of %q+D "
2558 : "follows static declaration", newdecl);
2559 : }
2560 : }
2561 :
2562 : /* Make sure gnu_inline attribute is either not present, or
2563 : present on all inline decls. */
2564 5047804 : if (DECL_DECLARED_INLINE_P (olddecl)
2565 5048906 : && DECL_DECLARED_INLINE_P (newdecl))
2566 : {
2567 847 : bool newa = lookup_attribute ("gnu_inline",
2568 847 : DECL_ATTRIBUTES (newdecl)) != NULL;
2569 847 : bool olda = lookup_attribute ("gnu_inline",
2570 847 : DECL_ATTRIBUTES (olddecl)) != NULL;
2571 847 : if (newa != olda)
2572 : {
2573 0 : auto_diagnostic_group d;
2574 0 : error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2575 : newa ? newdecl : olddecl);
2576 0 : error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2577 : "but not here");
2578 0 : }
2579 : }
2580 : /* Check if these are unmergeable overlapping FMV declarations. */
2581 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
2582 : && diagnose_versioned_decls (olddecl, newdecl))
2583 : return false;
2584 : }
2585 18695 : else if (VAR_P (newdecl))
2586 : {
2587 : /* Only variables can be thread-local, and all declarations must
2588 : agree on this property. */
2589 18658 : if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2590 : {
2591 : /* Nothing to check. Since OLDDECL is marked threadprivate
2592 : and NEWDECL does not have a thread-local attribute, we
2593 : will merge the threadprivate attribute into NEWDECL. */
2594 : ;
2595 : }
2596 55761 : else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2597 : {
2598 6 : auto_diagnostic_group d;
2599 6 : if (DECL_THREAD_LOCAL_P (newdecl))
2600 3 : error ("thread-local declaration of %q+D follows "
2601 : "non-thread-local declaration", newdecl);
2602 : else
2603 3 : error ("non-thread-local declaration of %q+D follows "
2604 : "thread-local declaration", newdecl);
2605 :
2606 6 : locate_old_decl (olddecl);
2607 6 : return false;
2608 6 : }
2609 :
2610 : /* Multiple initialized definitions are not allowed (6.9p3,5).
2611 : For this purpose, C23 makes it clear that thread-local
2612 : declarations without extern are definitions, not tentative
2613 : definitions, whether or not they have initializers. The
2614 : wording before C23 was unclear; literally it would have made
2615 : uninitialized thread-local declarations into tentative
2616 : definitions only if they also used static, but without saying
2617 : explicitly whether or not other cases count as
2618 : definitions at all. */
2619 19225 : if ((DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2620 19224 : || (flag_isoc23
2621 6484 : && DECL_THREAD_LOCAL_P (newdecl)
2622 25 : && !DECL_EXTERNAL (newdecl)
2623 21 : && !DECL_EXTERNAL (olddecl)))
2624 : {
2625 7 : auto_diagnostic_group d;
2626 7 : error ("redefinition of %q+D", newdecl);
2627 7 : locate_old_decl (olddecl);
2628 7 : return false;
2629 7 : }
2630 :
2631 : /* Objects declared at file scope: if the first declaration had
2632 : external linkage (even if it was an external reference) the
2633 : second must have external linkage as well, or the behavior is
2634 : undefined. If the first declaration had internal linkage, then
2635 : the second must too, or else be an external reference (in which
2636 : case the composite declaration still has internal linkage).
2637 : As for function declarations, we warn about the static-then-
2638 : extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2639 18662 : if (DECL_FILE_SCOPE_P (newdecl)
2640 18645 : && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2641 : {
2642 147 : if (DECL_EXTERNAL (newdecl))
2643 : {
2644 144 : if (!DECL_FILE_SCOPE_P (olddecl))
2645 : {
2646 2 : auto_diagnostic_group d;
2647 2 : error ("extern declaration of %q+D follows "
2648 : "declaration with no linkage", newdecl);
2649 2 : locate_old_decl (olddecl);
2650 2 : return false;
2651 2 : }
2652 142 : else if (warn_traditional)
2653 : {
2654 0 : warned |= warning (OPT_Wtraditional,
2655 : "non-static declaration of %q+D "
2656 : "follows static declaration", newdecl);
2657 : }
2658 : }
2659 : else
2660 : {
2661 3 : auto_diagnostic_group d;
2662 3 : if (TREE_PUBLIC (newdecl))
2663 2 : error ("non-static declaration of %q+D follows "
2664 : "static declaration", newdecl);
2665 : else
2666 1 : error ("static declaration of %q+D follows "
2667 : "non-static declaration", newdecl);
2668 :
2669 3 : locate_old_decl (olddecl);
2670 3 : return false;
2671 3 : }
2672 : }
2673 : /* Two objects with the same name declared at the same block
2674 : scope must both be external references (6.7p3). */
2675 18498 : else if (!DECL_FILE_SCOPE_P (newdecl))
2676 : {
2677 17 : if (DECL_EXTERNAL (newdecl))
2678 : {
2679 : /* Extern with initializer at block scope, which will
2680 : already have received an error. */
2681 : }
2682 15 : else if (DECL_EXTERNAL (olddecl))
2683 : {
2684 4 : auto_diagnostic_group d;
2685 4 : error ("declaration of %q+D with no linkage follows "
2686 : "extern declaration", newdecl);
2687 4 : locate_old_decl (olddecl);
2688 4 : }
2689 : else
2690 : {
2691 11 : auto_diagnostic_group d;
2692 11 : error ("redeclaration of %q+D with no linkage", newdecl);
2693 11 : locate_old_decl (olddecl);
2694 11 : }
2695 :
2696 17 : return false;
2697 : }
2698 :
2699 : /* C++ does not permit a decl to appear multiple times at file
2700 : scope. */
2701 18623 : if (warn_cxx_compat
2702 68 : && DECL_FILE_SCOPE_P (newdecl)
2703 68 : && !DECL_EXTERNAL (newdecl)
2704 18641 : && !DECL_EXTERNAL (olddecl))
2705 5 : warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2706 5 : OPT_Wc___compat,
2707 : "duplicate declaration of %qD is "
2708 : "invalid in C++", newdecl);
2709 : }
2710 :
2711 : /* warnings */
2712 : /* All decls must agree on a visibility. */
2713 5066464 : if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2714 5066427 : && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2715 5067215 : && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2716 : {
2717 1 : warned |= warning (0, "redeclaration of %q+D with different visibility "
2718 : "(old visibility preserved)", newdecl);
2719 : }
2720 :
2721 5066464 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2722 5047804 : warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2723 : else /* PARM_DECL, VAR_DECL */
2724 : {
2725 : /* Redeclaration of a parameter is a constraint violation (this is
2726 : not explicitly stated, but follows from C99 6.7p3 [no more than
2727 : one declaration of the same identifier with no linkage in the
2728 : same scope, except type tags] and 6.2.2p6 [parameters have no
2729 : linkage]). We must check for a forward parameter declaration,
2730 : indicated by TREE_ASM_WRITTEN on the old declaration - this is
2731 : an extension, the mandatory diagnostic for which is handled by
2732 : mark_forward_parm_decls. */
2733 :
2734 18660 : if (TREE_CODE (newdecl) == PARM_DECL
2735 37 : && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2736 : {
2737 2 : auto_diagnostic_group d;
2738 2 : error ("redefinition of parameter %q+D", newdecl);
2739 2 : locate_old_decl (olddecl);
2740 2 : return false;
2741 2 : }
2742 : }
2743 :
2744 : /* Optional warning for completely redundant decls. */
2745 5066462 : if (!warned && !pedwarned
2746 5066388 : && warn_redundant_decls
2747 : /* Don't warn about a function declaration followed by a
2748 : definition. */
2749 18 : && !(TREE_CODE (newdecl) == FUNCTION_DECL
2750 2 : && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2751 : /* Don't warn about redundant redeclarations of builtins. */
2752 18 : && !(TREE_CODE (newdecl) == FUNCTION_DECL
2753 2 : && !fndecl_built_in_p (newdecl)
2754 2 : && fndecl_built_in_p (olddecl)
2755 2 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2756 : /* Don't warn about an extern followed by a definition. */
2757 17 : && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2758 : /* Don't warn about forward parameter decls. */
2759 17 : && !(TREE_CODE (newdecl) == PARM_DECL
2760 6 : && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2761 : /* Don't warn about a variable definition following a declaration. */
2762 5066473 : && !(VAR_P (newdecl)
2763 10 : && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2764 : {
2765 6 : warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2766 : newdecl);
2767 : }
2768 :
2769 : /* Report location of previous decl/defn. */
2770 5066462 : if (warned || pedwarned)
2771 80 : locate_old_decl (olddecl);
2772 :
2773 : #undef DECL_EXTERN_INLINE
2774 :
2775 : return retval;
2776 5113351 : }
2777 :
2778 : /* Subroutine of duplicate_decls. NEWDECL has been found to be
2779 : consistent with OLDDECL, but carries new information. Merge the
2780 : new information into OLDDECL. This function issues no
2781 : diagnostics. */
2782 :
2783 : static void
2784 5112349 : merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2785 : {
2786 5112349 : bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2787 5112349 : && DECL_INITIAL (newdecl) != NULL_TREE);
2788 5112349 : bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2789 5112349 : && prototype_p (TREE_TYPE (newdecl)));
2790 5112349 : bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2791 5112349 : && prototype_p (TREE_TYPE (olddecl)));
2792 :
2793 : /* For real parm decl following a forward decl, rechain the old decl
2794 : in its new location and clear TREE_ASM_WRITTEN (it's not a
2795 : forward decl anymore). */
2796 5112349 : if (TREE_CODE (newdecl) == PARM_DECL
2797 35 : && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2798 : {
2799 35 : struct c_binding *b, **here;
2800 :
2801 54 : for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
2802 54 : if ((*here)->decl == olddecl)
2803 35 : goto found;
2804 0 : gcc_unreachable ();
2805 :
2806 35 : found:
2807 35 : b = *here;
2808 35 : *here = b->prev;
2809 35 : b->prev = current_scope->bindings;
2810 35 : current_scope->bindings = b;
2811 :
2812 35 : TREE_ASM_WRITTEN (olddecl) = 0;
2813 : }
2814 :
2815 5112349 : DECL_ATTRIBUTES (newdecl)
2816 5112349 : = targetm.merge_decl_attributes (olddecl, newdecl);
2817 :
2818 : /* For typedefs use the old type, as the new type's DECL_NAME points
2819 : at newdecl, which will be ggc_freed. */
2820 5112349 : if (TREE_CODE (newdecl) == TYPE_DECL)
2821 : {
2822 : /* But NEWTYPE might have an attribute, honor that. */
2823 45887 : tree tem = newtype;
2824 45887 : newtype = oldtype;
2825 :
2826 45887 : if (TYPE_USER_ALIGN (tem))
2827 : {
2828 15 : if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2829 6 : SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2830 15 : TYPE_USER_ALIGN (newtype) = true;
2831 : }
2832 :
2833 : /* And remove the new type from the variants list. */
2834 45887 : if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2835 : {
2836 15 : tree remove = TREE_TYPE (newdecl);
2837 15 : if (TYPE_MAIN_VARIANT (remove) == remove)
2838 : {
2839 2 : gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2840 : /* If remove is the main variant, no need to remove that
2841 : from the list. One of the DECL_ORIGINAL_TYPE
2842 : variants, e.g. created for aligned attribute, might still
2843 : refer to the newdecl TYPE_DECL though, so remove that one
2844 : in that case. */
2845 2 : if (DECL_ORIGINAL_TYPE (newdecl)
2846 2 : && DECL_ORIGINAL_TYPE (newdecl) != remove)
2847 2 : for (tree t = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl));
2848 2 : t; t = TYPE_MAIN_VARIANT (t))
2849 2 : if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2850 : {
2851 4 : TYPE_NEXT_VARIANT (t)
2852 2 : = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2853 2 : break;
2854 : }
2855 : }
2856 : else
2857 13 : for (tree t = TYPE_MAIN_VARIANT (remove); ;
2858 0 : t = TYPE_NEXT_VARIANT (t))
2859 13 : if (TYPE_NEXT_VARIANT (t) == remove)
2860 : {
2861 13 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2862 13 : break;
2863 : }
2864 : }
2865 :
2866 : /* Make sure we refer to the same type as the olddecl. */
2867 45887 : DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2868 : }
2869 :
2870 : /* Merge the data types specified in the two decls. */
2871 10224698 : TREE_TYPE (newdecl)
2872 5112349 : = TREE_TYPE (olddecl)
2873 10224698 : = composite_type (newtype, oldtype);
2874 :
2875 : /* Lay the type out, unless already done. */
2876 5112349 : if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2877 : {
2878 0 : if (TREE_TYPE (newdecl) != error_mark_node)
2879 0 : layout_type (TREE_TYPE (newdecl));
2880 0 : if (TREE_CODE (newdecl) != FUNCTION_DECL
2881 : && TREE_CODE (newdecl) != TYPE_DECL
2882 : && TREE_CODE (newdecl) != CONST_DECL)
2883 0 : layout_decl (newdecl, 0);
2884 : }
2885 : else
2886 : {
2887 : /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2888 5112349 : DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2889 5112349 : DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2890 5112349 : SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2891 5112349 : if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2892 : {
2893 44 : SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2894 44 : DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2895 : }
2896 5112305 : else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2897 5112305 : && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2898 3 : DECL_USER_ALIGN (newdecl) = 1;
2899 10224698 : if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2900 5112349 : > DECL_WARN_IF_NOT_ALIGN (newdecl))
2901 0 : SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2902 : DECL_WARN_IF_NOT_ALIGN (olddecl));
2903 : }
2904 :
2905 : /* Keep the old rtl since we can safely use it. */
2906 5112349 : if (HAS_RTL_P (olddecl))
2907 5112349 : COPY_DECL_RTL (olddecl, newdecl);
2908 :
2909 : /* Merge the type qualifiers. */
2910 5112349 : if (TREE_READONLY (newdecl))
2911 410255 : TREE_READONLY (olddecl) = 1;
2912 :
2913 5112349 : if (TREE_THIS_VOLATILE (newdecl))
2914 49750 : TREE_THIS_VOLATILE (olddecl) = 1;
2915 :
2916 : /* Merge deprecatedness. */
2917 5112349 : if (TREE_DEPRECATED (newdecl))
2918 387 : TREE_DEPRECATED (olddecl) = 1;
2919 :
2920 : /* Merge unavailability. */
2921 5112349 : if (TREE_UNAVAILABLE (newdecl))
2922 2 : TREE_UNAVAILABLE (olddecl) = 1;
2923 :
2924 : /* If a decl is in a system header and the other isn't, keep the one on the
2925 : system header. Otherwise, keep source location of definition rather than
2926 : declaration and of prototype rather than non-prototype unless that
2927 : prototype is built-in. */
2928 5112349 : if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2929 5112314 : && DECL_IN_SYSTEM_HEADER (olddecl)
2930 5810199 : && !DECL_IN_SYSTEM_HEADER (newdecl) )
2931 1046 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2932 5111303 : else if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2933 5111268 : && DECL_IN_SYSTEM_HEADER (newdecl)
2934 9484644 : && !DECL_IN_SYSTEM_HEADER (olddecl))
2935 3676537 : DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2936 1434766 : else if ((DECL_INITIAL (newdecl) == NULL_TREE
2937 1200416 : && DECL_INITIAL (olddecl) != NULL_TREE)
2938 2634260 : || (old_is_prototype && !new_is_prototype
2939 380 : && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2940 950 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2941 :
2942 : /* Merge the initialization information. */
2943 5112349 : if (DECL_INITIAL (newdecl) == NULL_TREE)
2944 4877927 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2945 :
2946 : /* Merge 'constexpr' information. */
2947 5112349 : if (VAR_P (olddecl) && VAR_P (newdecl))
2948 : {
2949 18623 : if (C_DECL_DECLARED_CONSTEXPR (olddecl))
2950 2 : C_DECL_DECLARED_CONSTEXPR (newdecl) = 1;
2951 18621 : else if (C_DECL_DECLARED_CONSTEXPR (newdecl))
2952 1 : C_DECL_DECLARED_CONSTEXPR (olddecl) = 1;
2953 : }
2954 :
2955 : /* Merge the threadprivate attribute. */
2956 5112349 : if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2957 7 : C_DECL_THREADPRIVATE_P (newdecl) = 1;
2958 :
2959 5112349 : if (HAS_DECL_ASSEMBLER_NAME_P (olddecl))
2960 : {
2961 : /* Copy the assembler name.
2962 : Currently, it can only be defined in the prototype. */
2963 5112314 : COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2964 :
2965 : /* Use visibility of whichever declaration had it specified */
2966 5112314 : if (DECL_VISIBILITY_SPECIFIED (olddecl))
2967 : {
2968 4782 : DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2969 4782 : DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2970 : }
2971 :
2972 5112314 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2973 : {
2974 5047804 : DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2975 5047804 : DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2976 5047804 : DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2977 5047804 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2978 5047804 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2979 5047804 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2980 5047804 : DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2981 5047804 : if (DECL_IS_OPERATOR_NEW_P (olddecl))
2982 0 : DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2983 5047804 : if (DECL_IS_OPERATOR_DELETE_P (olddecl))
2984 0 : DECL_SET_IS_OPERATOR_DELETE (newdecl, true);
2985 5047804 : TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2986 5047804 : DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2987 5047804 : DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2988 : }
2989 :
2990 : /* Merge the storage class information. */
2991 5112314 : merge_weak (newdecl, olddecl);
2992 :
2993 : /* For functions, static overrides non-static. */
2994 5112314 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2995 : {
2996 5047804 : TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2997 : /* This is since we don't automatically
2998 : copy the attributes of NEWDECL into OLDDECL. */
2999 5047804 : TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
3000 : /* If this clears `static', clear it in the identifier too. */
3001 5047804 : if (!TREE_PUBLIC (olddecl))
3002 7843 : TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
3003 : }
3004 : }
3005 :
3006 : /* In c99, 'extern' declaration before (or after) 'inline' means this
3007 : function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
3008 : is present. */
3009 5112349 : if (TREE_CODE (newdecl) == FUNCTION_DECL
3010 5047804 : && !flag_gnu89_inline
3011 5023303 : && (DECL_DECLARED_INLINE_P (newdecl)
3012 4832415 : || DECL_DECLARED_INLINE_P (olddecl))
3013 191082 : && (!DECL_DECLARED_INLINE_P (newdecl)
3014 190888 : || !DECL_DECLARED_INLINE_P (olddecl)
3015 834 : || !DECL_EXTERNAL (olddecl))
3016 190266 : && DECL_EXTERNAL (newdecl)
3017 190058 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
3018 5112447 : && !current_function_decl)
3019 81 : DECL_EXTERNAL (newdecl) = 0;
3020 :
3021 : /* An inline definition following a static declaration is not
3022 : DECL_EXTERNAL. */
3023 5112349 : if (new_is_definition
3024 233829 : && (DECL_DECLARED_INLINE_P (newdecl)
3025 42719 : || DECL_DECLARED_INLINE_P (olddecl))
3026 5303673 : && !TREE_PUBLIC (olddecl))
3027 927 : DECL_EXTERNAL (newdecl) = 0;
3028 :
3029 5112349 : if (DECL_EXTERNAL (newdecl))
3030 : {
3031 5020483 : TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
3032 5020483 : DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
3033 :
3034 : /* An extern decl does not override previous storage class. */
3035 5020483 : TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3036 5020483 : if (!DECL_EXTERNAL (newdecl))
3037 : {
3038 1437 : DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3039 1437 : DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3040 : }
3041 : }
3042 : else
3043 : {
3044 91866 : TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
3045 91866 : TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
3046 : }
3047 :
3048 5112349 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3049 : {
3050 5047804 : if (DECL_FUNCTION_VERSIONED (olddecl)
3051 5047804 : || DECL_FUNCTION_VERSIONED (newdecl))
3052 : {
3053 0 : maybe_mark_function_versioned (olddecl);
3054 0 : maybe_mark_function_versioned (newdecl);
3055 : }
3056 : /* If we're redefining a function previously defined as extern
3057 : inline, make sure we emit debug info for the inline before we
3058 : throw it away, in case it was inlined into a function that
3059 : hasn't been written out yet. */
3060 5047804 : if (new_is_definition && DECL_INITIAL (olddecl))
3061 : /* The new defn must not be inline. */
3062 75 : DECL_UNINLINABLE (newdecl) = 1;
3063 : else
3064 : {
3065 : /* If either decl says `inline', this fn is inline, unless
3066 : its definition was passed already. */
3067 5047729 : if (DECL_DECLARED_INLINE_P (newdecl)
3068 9904296 : || DECL_DECLARED_INLINE_P (olddecl))
3069 191347 : DECL_DECLARED_INLINE_P (newdecl) = 1;
3070 :
3071 15143187 : DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3072 10097102 : = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3073 :
3074 10095458 : DECL_DISREGARD_INLINE_LIMITS (newdecl)
3075 5047729 : = DECL_DISREGARD_INLINE_LIMITS (olddecl)
3076 5047729 : = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
3077 10095320 : || DECL_DISREGARD_INLINE_LIMITS (olddecl));
3078 : }
3079 :
3080 5047804 : if (fndecl_built_in_p (olddecl))
3081 : {
3082 : /* If redeclaring a builtin function, it stays built in.
3083 : But it gets tagged as having been declared. */
3084 3994383 : copy_decl_built_in_function (newdecl, olddecl);
3085 3994383 : C_DECL_DECLARED_BUILTIN (newdecl) = 1;
3086 3994383 : if (new_is_prototype)
3087 : {
3088 3994016 : C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
3089 3994016 : if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3090 : {
3091 3994016 : enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3092 3994016 : switch (fncode)
3093 : {
3094 : /* If a compatible prototype of these builtin functions
3095 : is seen, assume the runtime implements it with the
3096 : expected semantics. */
3097 6593 : case BUILT_IN_STPCPY:
3098 6593 : if (builtin_decl_explicit_p (fncode))
3099 6593 : set_builtin_decl_implicit_p (fncode, true);
3100 : break;
3101 3987423 : default:
3102 3987423 : if (builtin_decl_explicit_p (fncode))
3103 3987423 : set_builtin_decl_declared_p (fncode, true);
3104 : break;
3105 : }
3106 :
3107 3994016 : copy_attributes_to_builtin (newdecl);
3108 : }
3109 : }
3110 : else
3111 734 : C_DECL_BUILTIN_PROTOTYPE (newdecl)
3112 734 : = C_DECL_BUILTIN_PROTOTYPE (olddecl);
3113 : }
3114 :
3115 : /* Preserve function specific target and optimization options */
3116 5047804 : if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
3117 5048310 : && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
3118 470 : DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
3119 470 : = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
3120 :
3121 5047804 : if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
3122 5071226 : && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
3123 9 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
3124 9 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
3125 :
3126 : /* Also preserve various other info from the definition. */
3127 5047804 : if (!new_is_definition)
3128 : {
3129 4813975 : tree t;
3130 4813975 : DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3131 4813975 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3132 4813975 : DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
3133 4813975 : DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3134 4813975 : DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
3135 7495761 : for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
3136 2681786 : DECL_CONTEXT (t) = newdecl;
3137 :
3138 : /* See if we've got a function to instantiate from. */
3139 4813975 : if (DECL_SAVED_TREE (olddecl))
3140 1620 : DECL_ABSTRACT_ORIGIN (newdecl)
3141 810 : = DECL_ABSTRACT_ORIGIN (olddecl);
3142 : }
3143 : }
3144 :
3145 : /* Merge the USED information. */
3146 5112349 : if (TREE_USED (olddecl))
3147 717424 : TREE_USED (newdecl) = 1;
3148 4394925 : else if (TREE_USED (newdecl))
3149 4 : TREE_USED (olddecl) = 1;
3150 5112349 : if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
3151 18658 : DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
3152 5112349 : if (DECL_PRESERVE_P (olddecl))
3153 25 : DECL_PRESERVE_P (newdecl) = 1;
3154 5112324 : else if (DECL_PRESERVE_P (newdecl))
3155 4 : DECL_PRESERVE_P (olddecl) = 1;
3156 :
3157 : /* Merge DECL_COMMON */
3158 18623 : if (VAR_P (olddecl) && VAR_P (newdecl)
3159 18623 : && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
3160 5130970 : && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
3161 37238 : DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
3162 :
3163 : /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
3164 : But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
3165 : DECL_ARGUMENTS (if appropriate). */
3166 5112349 : {
3167 5112349 : unsigned olddecl_uid = DECL_UID (olddecl);
3168 5112349 : tree olddecl_context = DECL_CONTEXT (olddecl);
3169 5112349 : tree olddecl_arguments = NULL;
3170 5112349 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3171 5047804 : olddecl_arguments = DECL_ARGUMENTS (olddecl);
3172 :
3173 5112349 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3174 : (char *) newdecl + sizeof (struct tree_common),
3175 : sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3176 5112349 : DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3177 5112349 : switch (TREE_CODE (olddecl))
3178 : {
3179 5066427 : case FUNCTION_DECL:
3180 5066427 : case VAR_DECL:
3181 5066427 : {
3182 5066427 : struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
3183 :
3184 10132854 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3185 : (char *) newdecl + sizeof (struct tree_decl_common),
3186 5066427 : tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3187 5066427 : olddecl->decl_with_vis.symtab_node = snode;
3188 :
3189 5066427 : if ((DECL_EXTERNAL (olddecl)
3190 47381 : || TREE_PUBLIC (olddecl)
3191 8116 : || TREE_STATIC (olddecl))
3192 5113801 : && DECL_SECTION_NAME (newdecl) != NULL)
3193 8 : set_decl_section_name (olddecl, newdecl);
3194 :
3195 : /* This isn't quite correct for something like
3196 : int __thread x attribute ((tls_model ("local-exec")));
3197 : extern int __thread x;
3198 : as we'll lose the "local-exec" model. */
3199 5066427 : if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
3200 89 : set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3201 : break;
3202 : }
3203 :
3204 45922 : case FIELD_DECL:
3205 45922 : case PARM_DECL:
3206 45922 : case LABEL_DECL:
3207 45922 : case RESULT_DECL:
3208 45922 : case CONST_DECL:
3209 45922 : case TYPE_DECL:
3210 91844 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3211 : (char *) newdecl + sizeof (struct tree_decl_common),
3212 45922 : tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3213 45922 : break;
3214 :
3215 0 : default:
3216 :
3217 0 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3218 : (char *) newdecl + sizeof (struct tree_decl_common),
3219 : sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
3220 : }
3221 5112349 : DECL_UID (olddecl) = olddecl_uid;
3222 5112349 : DECL_CONTEXT (olddecl) = olddecl_context;
3223 5112349 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3224 5047804 : DECL_ARGUMENTS (olddecl) = olddecl_arguments;
3225 : }
3226 :
3227 : /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3228 : so that encode_section_info has a chance to look at the new decl
3229 : flags and attributes. */
3230 5112349 : if (DECL_RTL_SET_P (olddecl)
3231 5112355 : && (TREE_CODE (olddecl) == FUNCTION_DECL
3232 6 : || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
3233 6 : make_decl_rtl (olddecl);
3234 5112349 : }
3235 :
3236 : /* Handle when a new declaration NEWDECL has the same name as an old
3237 : one OLDDECL in the same binding contour. Prints an error message
3238 : if appropriate.
3239 :
3240 : If safely possible, alter OLDDECL to look like NEWDECL, and return
3241 : true. Otherwise, return false. */
3242 :
3243 : static bool
3244 5113454 : duplicate_decls (tree newdecl, tree olddecl)
3245 : {
3246 5113454 : tree newtype = NULL, oldtype = NULL;
3247 :
3248 5113454 : if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
3249 : {
3250 : /* Avoid `unused variable' and other warnings for OLDDECL. */
3251 1105 : suppress_warning (olddecl, OPT_Wunused);
3252 : /* If the types are completely different, poison them both with
3253 : error_mark_node. */
3254 1105 : if (TREE_CODE (TREE_TYPE (newdecl)) != TREE_CODE (TREE_TYPE (olddecl))
3255 118 : && olddecl != error_mark_node
3256 1203 : && seen_error ())
3257 : {
3258 65 : if (TREE_CODE (olddecl) != FUNCTION_DECL)
3259 53 : TREE_TYPE (olddecl) = error_mark_node;
3260 65 : if (TREE_CODE (newdecl) != FUNCTION_DECL)
3261 61 : TREE_TYPE (newdecl) = error_mark_node;
3262 : }
3263 1105 : return false;
3264 : }
3265 :
3266 5112349 : merge_decls (newdecl, olddecl, newtype, oldtype);
3267 :
3268 : /* The NEWDECL will no longer be needed.
3269 :
3270 : Before releasing the node, be sure to remove function from symbol
3271 : table that might have been inserted there to record comdat group.
3272 : Be sure to however do not free DECL_STRUCT_FUNCTION because this
3273 : structure is shared in between NEWDECL and OLDECL. */
3274 5112349 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3275 5047804 : DECL_STRUCT_FUNCTION (newdecl) = NULL;
3276 5112349 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3277 : {
3278 5066427 : struct symtab_node *snode = symtab_node::get (newdecl);
3279 5066427 : if (snode)
3280 104 : snode->remove ();
3281 : }
3282 5112349 : ggc_free (newdecl);
3283 5112349 : return true;
3284 : }
3285 :
3286 :
3287 : /* Check whether decl-node NEW_DECL shadows an existing declaration. */
3288 : static void
3289 170962526 : warn_if_shadowing (tree new_decl)
3290 : {
3291 170962526 : struct c_binding *b;
3292 :
3293 : /* Shadow warnings wanted? */
3294 341924136 : if (!(warn_shadow
3295 170961790 : || warn_shadow_local
3296 170961610 : || warn_shadow_compatible_local)
3297 : /* No shadow warnings for internally generated vars. */
3298 170962769 : || DECL_IS_UNDECLARED_BUILTIN (new_decl))
3299 : return;
3300 :
3301 : /* Is anything being shadowed? Invisible decls do not count. */
3302 253 : for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
3303 157 : if (b->decl && b->decl != new_decl && !b->invisible
3304 214 : && (b->decl == error_mark_node
3305 43 : || diagnostic_report_warnings_p (global_dc,
3306 : DECL_SOURCE_LOCATION (b->decl))))
3307 : {
3308 57 : tree old_decl = b->decl;
3309 :
3310 57 : if (old_decl == error_mark_node)
3311 : {
3312 14 : warning (OPT_Wshadow, "declaration of %q+D shadows previous "
3313 : "non-variable", new_decl);
3314 50 : break;
3315 : }
3316 :
3317 43 : bool warned = false;
3318 43 : auto_diagnostic_group d;
3319 43 : if (TREE_CODE (old_decl) == PARM_DECL)
3320 : {
3321 5 : enum opt_code warning_code;
3322 :
3323 : /* If '-Wshadow=compatible-local' is specified without other
3324 : -Wshadow= flags, we will warn only when the types of the
3325 : shadowing variable (i.e. new_decl) and the shadowed variable
3326 : (old_decl) are compatible. */
3327 5 : if (warn_shadow)
3328 : warning_code = OPT_Wshadow;
3329 2 : else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3330 : warning_code = OPT_Wshadow_compatible_local;
3331 : else
3332 2 : warning_code = OPT_Wshadow_local;
3333 5 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3334 : "declaration of %qD shadows a parameter",
3335 : new_decl);
3336 : }
3337 38 : else if (DECL_FILE_SCOPE_P (old_decl))
3338 : {
3339 : /* Do not warn if a variable shadows a function, unless
3340 : the variable is a function or a pointer-to-function. */
3341 34 : if (TREE_CODE (old_decl) == FUNCTION_DECL
3342 11 : && TREE_CODE (new_decl) != FUNCTION_DECL
3343 36 : && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
3344 7 : continue;
3345 :
3346 20 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
3347 : "declaration of %qD shadows a global "
3348 : "declaration",
3349 : new_decl);
3350 : }
3351 11 : else if (TREE_CODE (old_decl) == FUNCTION_DECL
3352 11 : && fndecl_built_in_p (old_decl))
3353 : {
3354 0 : warning (OPT_Wshadow, "declaration of %q+D shadows "
3355 : "a built-in function", new_decl);
3356 0 : break;
3357 : }
3358 : else
3359 : {
3360 11 : enum opt_code warning_code;
3361 :
3362 : /* If '-Wshadow=compatible-local' is specified without other
3363 : -Wshadow= flags, we will warn only when the types of the
3364 : shadowing variable (i.e. new_decl) and the shadowed variable
3365 : (old_decl) are compatible. */
3366 11 : if (warn_shadow)
3367 : warning_code = OPT_Wshadow;
3368 8 : else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3369 : warning_code = OPT_Wshadow_compatible_local;
3370 : else
3371 2 : warning_code = OPT_Wshadow_local;
3372 11 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3373 : "declaration of %qD shadows a previous local",
3374 : new_decl);
3375 : }
3376 :
3377 36 : if (warned)
3378 26 : inform (DECL_SOURCE_LOCATION (old_decl),
3379 : "shadowed declaration is here");
3380 :
3381 : break;
3382 43 : }
3383 : }
3384 :
3385 : /* Record a decl-node X as belonging to the current lexical scope.
3386 : Check for errors (such as an incompatible declaration for the same
3387 : name already seen in the same scope).
3388 :
3389 : Returns either X or an old decl for the same name.
3390 : If an old decl is returned, it may have been smashed
3391 : to agree with what X says. */
3392 :
3393 : tree
3394 204019887 : pushdecl (tree x)
3395 : {
3396 204019887 : tree name = DECL_NAME (x);
3397 204019887 : struct c_scope *scope = current_scope;
3398 204019887 : struct c_binding *b;
3399 204019887 : bool nested = false;
3400 204019887 : location_t locus = DECL_SOURCE_LOCATION (x);
3401 :
3402 : /* Must set DECL_CONTEXT for everything not at file scope or
3403 : DECL_FILE_SCOPE_P won't work. Local externs don't count
3404 : unless they have initializers (which generate code). We
3405 : also exclude CONST_DECLs because enumerators will get the
3406 : type of the enum as context. */
3407 204019887 : if (current_function_decl
3408 9152254 : && TREE_CODE (x) != CONST_DECL
3409 213014871 : && (!VAR_OR_FUNCTION_DECL_P (x)
3410 8716579 : || DECL_INITIAL (x) || !TREE_PUBLIC (x)))
3411 8982956 : DECL_CONTEXT (x) = current_function_decl;
3412 :
3413 : /* Anonymous decls are just inserted in the scope. */
3414 204019887 : if (!name)
3415 : {
3416 7916667 : bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
3417 : locus);
3418 7916667 : return x;
3419 : }
3420 :
3421 : /* First, see if there is another declaration with the same name in
3422 : the current scope. If there is, duplicate_decls may do all the
3423 : work for us. If duplicate_decls returns false, that indicates
3424 : two incompatible decls in the same scope; we are to silently
3425 : replace the old one (duplicate_decls has issued all appropriate
3426 : diagnostics). In particular, we should not consider possible
3427 : duplicates in the external scope, or shadowing. */
3428 196103220 : b = I_SYMBOL_BINDING (name);
3429 196103220 : if (b && B_IN_SCOPE (b, scope))
3430 : {
3431 1403510 : struct c_binding *b_ext, *b_use;
3432 1403510 : tree type = TREE_TYPE (x);
3433 1403510 : tree visdecl = b->decl;
3434 1403510 : tree vistype = TREE_TYPE (visdecl);
3435 1403510 : if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3436 1403510 : && COMPLETE_TYPE_P (TREE_TYPE (x)))
3437 1372 : b->inner_comp = false;
3438 1403510 : b_use = b;
3439 1403510 : b_ext = b;
3440 : /* If this is an external linkage declaration, we should check
3441 : for compatibility with the type in the external scope before
3442 : setting the type at this scope based on the visible
3443 : information only. */
3444 1403510 : if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
3445 : {
3446 2698744 : while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3447 1349382 : b_ext = b_ext->shadowed;
3448 1349362 : if (b_ext)
3449 : {
3450 1349353 : b_use = b_ext;
3451 1349353 : if (b_use->u.type)
3452 276847 : TREE_TYPE (b_use->decl) = b_use->u.type;
3453 : }
3454 : }
3455 :
3456 : /* Check if x is part of a FMV set with b_use.
3457 : FMV is only supported in c for targets with target_version
3458 : attributes. */
3459 1403510 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
3460 : && b_use && TREE_CODE (b_use->decl) == FUNCTION_DECL
3461 : && TREE_CODE (x) == FUNCTION_DECL && DECL_FILE_SCOPE_P (b_use->decl)
3462 : && DECL_FILE_SCOPE_P (x)
3463 : && disjoint_version_decls (x, b_use->decl)
3464 : && comptypes (vistype, type))
3465 : {
3466 : maybe_mark_function_versioned (b_use->decl);
3467 : maybe_mark_function_versioned (b->decl);
3468 : maybe_mark_function_versioned (x);
3469 :
3470 : cgraph_node *b_node = cgraph_node::get_create (b_use->decl);
3471 : cgraph_function_version_info *b_v = b_node->function_version ();
3472 : if (!b_v)
3473 : b_v = b_node->insert_new_function_version ();
3474 :
3475 : /* Check if this new node conflicts with any previous functions
3476 : in the set. */
3477 : cgraph_function_version_info *version = b_v;
3478 : for (; version; version = version->next)
3479 : if (!disjoint_version_decls (version->this_node->decl, x))
3480 : {
3481 : /* The decls define overlapping version, so attempt to merge
3482 : or diagnose the conflict. */
3483 : if (duplicate_decls (x, version->this_node->decl))
3484 : return version->this_node->decl;
3485 : else
3486 : return error_mark_node;
3487 : }
3488 :
3489 : /* This is a new version to be added to FMV structure. */
3490 : cgraph_node::add_function_version (b_v, x);
3491 :
3492 : /* Get the first node from the structure. */
3493 : cgraph_function_version_info *default_v = b_v;
3494 : while (default_v->prev)
3495 : default_v = default_v->prev;
3496 : /* Always use the default node for the bindings. */
3497 : b_use->decl = default_v->this_node->decl;
3498 : b->decl = default_v->this_node->decl;
3499 :
3500 : /* Node is not a duplicate, so no need to do the rest of the
3501 : checks. */
3502 : return x;
3503 : }
3504 :
3505 1403510 : if (duplicate_decls (x, b_use->decl))
3506 : {
3507 1403138 : if (b_use != b)
3508 : {
3509 : /* Save the updated type in the external scope and
3510 : restore the proper type for this scope. */
3511 1349147 : tree thistype;
3512 1349147 : if (comptypes (vistype, type))
3513 1349105 : thistype = composite_type (vistype, type);
3514 : else
3515 42 : thistype = TREE_TYPE (b_use->decl);
3516 1349147 : b_use->u.type = TREE_TYPE (b_use->decl);
3517 1349147 : if (TREE_CODE (b_use->decl) == FUNCTION_DECL
3518 1349147 : && fndecl_built_in_p (b_use->decl))
3519 286909 : thistype
3520 286909 : = c_build_type_attribute_variant (thistype,
3521 286909 : TYPE_ATTRIBUTES
3522 : (b_use->u.type));
3523 1349147 : TREE_TYPE (b_use->decl) = thistype;
3524 : }
3525 1403138 : return b_use->decl;
3526 : }
3527 : else
3528 372 : goto skip_external_and_shadow_checks;
3529 : }
3530 :
3531 : /* All declarations with external linkage, and all external
3532 : references, go in the external scope, no matter what scope is
3533 : current. However, the binding in that scope is ignored for
3534 : purposes of normal name lookup. A separate binding structure is
3535 : created in the requested scope; this governs the normal
3536 : visibility of the symbol.
3537 :
3538 : The binding in the externals scope is used exclusively for
3539 : detecting duplicate declarations of the same object, no matter
3540 : what scope they are in; this is what we do here. (C99 6.2.7p2:
3541 : All declarations that refer to the same object or function shall
3542 : have compatible type; otherwise, the behavior is undefined.)
3543 : However, in Objective-C, we also want to detect declarations
3544 : conflicting with those of the basic types. */
3545 339951586 : if ((DECL_EXTERNAL (x) || scope == file_scope)
3546 206245452 : && (VAR_OR_FUNCTION_DECL_P (x) || c_dialect_objc ()))
3547 : {
3548 51242503 : tree type = TREE_TYPE (x);
3549 51242503 : tree vistype = NULL_TREE;
3550 51242503 : tree visdecl = NULL_TREE;
3551 51242503 : bool type_saved = false;
3552 3709971 : if (b && !B_IN_EXTERNAL_SCOPE (b)
3553 1038 : && VAR_OR_FUNCTION_DECL_P (b->decl)
3554 51243532 : && DECL_FILE_SCOPE_P (b->decl))
3555 : {
3556 803 : visdecl = b->decl;
3557 803 : vistype = TREE_TYPE (visdecl);
3558 : }
3559 51242503 : if (scope != file_scope
3560 51242503 : && !DECL_IN_SYSTEM_HEADER (x))
3561 11478 : warning_at (locus, OPT_Wnested_externs,
3562 : "nested extern declaration of %qD", x);
3563 :
3564 51243927 : while (b && !B_IN_EXTERNAL_SCOPE (b))
3565 : {
3566 : /* If this decl might be modified, save its type. This is
3567 : done here rather than when the decl is first bound
3568 : because the type may change after first binding, through
3569 : being completed or through attributes being added. If we
3570 : encounter multiple such decls, only the first should have
3571 : its type saved; the others will already have had their
3572 : proper types saved and the types will not have changed as
3573 : their scopes will not have been re-entered. */
3574 1424 : if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3575 : {
3576 1022 : b->u.type = TREE_TYPE (b->decl);
3577 1022 : type_saved = true;
3578 : }
3579 1424 : if (B_IN_FILE_SCOPE (b)
3580 1012 : && VAR_P (b->decl)
3581 572 : && TREE_STATIC (b->decl)
3582 281 : && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3583 138 : && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3584 47 : && TREE_CODE (type) == ARRAY_TYPE
3585 47 : && TYPE_DOMAIN (type)
3586 28 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3587 1452 : && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3588 : {
3589 : /* Array type completed in inner scope, which should be
3590 : diagnosed if the completion does not have size 1 and
3591 : it does not get completed in the file scope. */
3592 6 : b->inner_comp = true;
3593 : }
3594 1424 : b = b->shadowed;
3595 : }
3596 :
3597 : /* If a matching external declaration has been found, set its
3598 : type to the composite of all the types of that declaration.
3599 : After the consistency checks, it will be reset to the
3600 : composite of the visible types only. */
3601 51242503 : if (b && b->u.type)
3602 768 : TREE_TYPE (b->decl) = b->u.type;
3603 :
3604 : /* the static does not go in the externals scope. */
3605 3709827 : if (b && duplicate_decls (x, b->decl))
3606 : {
3607 3709094 : tree thistype;
3608 3709094 : if (vistype)
3609 : {
3610 675 : if (comptypes (vistype, type))
3611 638 : thistype = composite_type (vistype, type);
3612 : else
3613 37 : thistype = TREE_TYPE (b->decl);
3614 : }
3615 : else
3616 : thistype = type;
3617 3709094 : b->u.type = TREE_TYPE (b->decl);
3618 : /* Propagate the type attributes to the decl. */
3619 3709094 : thistype
3620 3709094 : = c_build_type_attribute_variant (thistype,
3621 3709094 : TYPE_ATTRIBUTES (b->u.type));
3622 3709094 : TREE_TYPE (b->decl) = thistype;
3623 3709094 : bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3624 : locus);
3625 3709094 : return b->decl;
3626 : }
3627 47533409 : else if (TREE_PUBLIC (x))
3628 : {
3629 47157896 : if (visdecl && !b && duplicate_decls (x, visdecl))
3630 : {
3631 : /* An external declaration at block scope referring to a
3632 : visible entity with internal linkage. The composite
3633 : type will already be correct for this scope, so we
3634 : just need to fall through to make the declaration in
3635 : this scope. */
3636 : nested = true;
3637 : x = visdecl;
3638 : }
3639 : else
3640 : {
3641 47157779 : bind (name, x, external_scope, /*invisible=*/true,
3642 : /*nested=*/false, locus);
3643 47157779 : nested = true;
3644 : }
3645 : }
3646 : }
3647 :
3648 190990616 : if (TREE_CODE (x) != PARM_DECL)
3649 70814292 : warn_if_shadowing (x);
3650 :
3651 120176324 : skip_external_and_shadow_checks:
3652 190990988 : if (TREE_CODE (x) == TYPE_DECL)
3653 : {
3654 : /* So this is a typedef, set its underlying type. */
3655 9746945 : set_underlying_type (x);
3656 :
3657 : /* If X is a typedef defined in the current function, record it
3658 : for the purpose of implementing the -Wunused-local-typedefs
3659 : warning. */
3660 9746945 : record_locally_defined_typedef (x);
3661 : }
3662 :
3663 190990988 : bind (name, x, scope, /*invisible=*/false, nested, locus);
3664 :
3665 : /* If x's type is incomplete because it's based on a
3666 : structure or union which has not yet been fully declared,
3667 : attach it to that structure or union type, so we can go
3668 : back and complete the variable declaration later, if the
3669 : structure or union gets fully declared.
3670 :
3671 : If the input is erroneous, we can have error_mark in the type
3672 : slot (e.g. "f(void a, ...)") - that doesn't count as an
3673 : incomplete type. */
3674 190990988 : if (TREE_TYPE (x) != error_mark_node
3675 190990988 : && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3676 : {
3677 184918 : tree element = TREE_TYPE (x);
3678 :
3679 205044 : while (TREE_CODE (element) == ARRAY_TYPE)
3680 20126 : element = TREE_TYPE (element);
3681 184918 : element = TYPE_MAIN_VARIANT (element);
3682 :
3683 184918 : if ((RECORD_OR_UNION_TYPE_P (element)
3684 140767 : || TREE_CODE (element) == ENUMERAL_TYPE)
3685 44222 : && (TREE_CODE (x) != TYPE_DECL
3686 40768 : || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3687 188382 : && !COMPLETE_TYPE_P (element))
3688 321 : C_TYPE_INCOMPLETE_VARS (element)
3689 642 : = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3690 : }
3691 : return x;
3692 : }
3693 :
3694 :
3695 : /* Issue a permerror about implicit function declaration. ID is the function
3696 : identifier, OLDDECL is a declaration of the function in a different scope,
3697 : or NULL_TREE. */
3698 :
3699 : static void
3700 3776 : implicit_decl_permerror (location_t loc, tree id, tree olddecl)
3701 : {
3702 3776 : if (!warn_implicit_function_declaration)
3703 2617 : return;
3704 :
3705 1159 : bool warned;
3706 1159 : auto_diagnostic_group d;
3707 1159 : name_hint hint;
3708 1159 : if (!olddecl)
3709 553 : hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3710 :
3711 1159 : if (flag_isoc99)
3712 : {
3713 1153 : if (const char *suggestion = hint.suggestion ())
3714 : {
3715 107 : gcc_rich_location richloc (loc);
3716 107 : richloc.add_fixit_replace (suggestion);
3717 107 : warned = permerror_opt (&richloc, OPT_Wimplicit_function_declaration,
3718 : "implicit declaration of function %qE;"
3719 : " did you mean %qs?",
3720 : id, suggestion);
3721 107 : }
3722 : else
3723 1046 : warned = permerror_opt (loc, OPT_Wimplicit_function_declaration,
3724 : "implicit declaration of function %qE", id);
3725 : }
3726 6 : else if (const char *suggestion = hint.suggestion ())
3727 : {
3728 2 : gcc_rich_location richloc (loc);
3729 2 : richloc.add_fixit_replace (suggestion);
3730 2 : warned = warning_at
3731 2 : (&richloc, OPT_Wimplicit_function_declaration,
3732 : G_("implicit declaration of function %qE; did you mean %qs?"),
3733 : id, suggestion);
3734 2 : }
3735 : else
3736 4 : warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3737 : G_("implicit declaration of function %qE"), id);
3738 :
3739 1159 : if (warned)
3740 : {
3741 : /* Whether the olddecl is an undeclared builtin function.
3742 : locate_old_decl will not generate a diagnostic for those,
3743 : so in that case we want to look elsewhere. */
3744 90 : bool undeclared_builtin = (olddecl
3745 28 : && TREE_CODE (olddecl) == FUNCTION_DECL
3746 28 : && fndecl_built_in_p (olddecl)
3747 117 : && !C_DECL_DECLARED_BUILTIN (olddecl));
3748 90 : if (undeclared_builtin)
3749 : {
3750 27 : const char *header = header_for_builtin_fn (olddecl);
3751 27 : if (header)
3752 : {
3753 20 : rich_location richloc (line_table, loc);
3754 20 : maybe_add_include_fixit (&richloc, header, true);
3755 20 : inform (&richloc,
3756 : "include %qs or provide a declaration of %qE",
3757 : header, id);
3758 20 : }
3759 : }
3760 63 : else if (olddecl)
3761 1 : locate_old_decl (olddecl);
3762 : }
3763 :
3764 1069 : if (!warned)
3765 1159 : hint.suppress ();
3766 1159 : }
3767 :
3768 : /* Return the name of the header file that declares built-in function
3769 : FNDECL, or null if either we don't know or don't expect to see an
3770 : explicit declaration. */
3771 :
3772 : static const char *
3773 3177 : header_for_builtin_fn (tree fndecl)
3774 : {
3775 3177 : if (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3776 : return NULL;
3777 :
3778 3177 : switch (DECL_FUNCTION_CODE (fndecl))
3779 : {
3780 : CASE_FLT_FN (BUILT_IN_ACOS):
3781 : CASE_FLT_FN (BUILT_IN_ACOSH):
3782 : CASE_FLT_FN (BUILT_IN_ASIN):
3783 : CASE_FLT_FN (BUILT_IN_ASINH):
3784 : CASE_FLT_FN (BUILT_IN_ATAN):
3785 : CASE_FLT_FN (BUILT_IN_ATANH):
3786 : CASE_FLT_FN (BUILT_IN_ATAN2):
3787 : CASE_FLT_FN (BUILT_IN_CBRT):
3788 : CASE_FLT_FN (BUILT_IN_CEIL):
3789 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3790 : CASE_FLT_FN (BUILT_IN_COPYSIGN):
3791 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3792 : CASE_FLT_FN (BUILT_IN_COS):
3793 : CASE_FLT_FN (BUILT_IN_COSH):
3794 : CASE_FLT_FN (BUILT_IN_ERF):
3795 : CASE_FLT_FN (BUILT_IN_ERFC):
3796 : CASE_FLT_FN (BUILT_IN_EXP):
3797 : CASE_FLT_FN (BUILT_IN_EXP2):
3798 : CASE_FLT_FN (BUILT_IN_EXPM1):
3799 : CASE_FLT_FN (BUILT_IN_FABS):
3800 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3801 : CASE_FLT_FN (BUILT_IN_FDIM):
3802 : CASE_FLT_FN (BUILT_IN_FLOOR):
3803 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3804 : CASE_FLT_FN (BUILT_IN_FMA):
3805 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3806 : CASE_FLT_FN (BUILT_IN_FMAX):
3807 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3808 : CASE_FLT_FN (BUILT_IN_FMIN):
3809 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3810 : CASE_FLT_FN (BUILT_IN_FMOD):
3811 : CASE_FLT_FN (BUILT_IN_FREXP):
3812 : CASE_FLT_FN (BUILT_IN_HYPOT):
3813 : CASE_FLT_FN (BUILT_IN_ILOGB):
3814 : CASE_FLT_FN (BUILT_IN_LDEXP):
3815 : CASE_FLT_FN (BUILT_IN_LGAMMA):
3816 : CASE_FLT_FN (BUILT_IN_LLRINT):
3817 : CASE_FLT_FN (BUILT_IN_LLROUND):
3818 : CASE_FLT_FN (BUILT_IN_LOG):
3819 : CASE_FLT_FN (BUILT_IN_LOG10):
3820 : CASE_FLT_FN (BUILT_IN_LOG1P):
3821 : CASE_FLT_FN (BUILT_IN_LOG2):
3822 : CASE_FLT_FN (BUILT_IN_LOGB):
3823 : CASE_FLT_FN (BUILT_IN_LRINT):
3824 : CASE_FLT_FN (BUILT_IN_LROUND):
3825 : CASE_FLT_FN (BUILT_IN_MODF):
3826 : CASE_FLT_FN (BUILT_IN_NAN):
3827 : CASE_FLT_FN (BUILT_IN_NEARBYINT):
3828 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3829 : CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3830 : CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3831 : CASE_FLT_FN (BUILT_IN_POW):
3832 : CASE_FLT_FN (BUILT_IN_REMAINDER):
3833 : CASE_FLT_FN (BUILT_IN_REMQUO):
3834 : CASE_FLT_FN (BUILT_IN_RINT):
3835 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3836 : CASE_FLT_FN (BUILT_IN_ROUND):
3837 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3838 : CASE_FLT_FN (BUILT_IN_SCALBLN):
3839 : CASE_FLT_FN (BUILT_IN_SCALBN):
3840 : CASE_FLT_FN (BUILT_IN_SIN):
3841 : CASE_FLT_FN (BUILT_IN_SINH):
3842 : CASE_FLT_FN (BUILT_IN_SINCOS):
3843 : CASE_FLT_FN (BUILT_IN_SQRT):
3844 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3845 : CASE_FLT_FN (BUILT_IN_TAN):
3846 : CASE_FLT_FN (BUILT_IN_TANH):
3847 : CASE_FLT_FN (BUILT_IN_TGAMMA):
3848 : CASE_FLT_FN (BUILT_IN_TRUNC):
3849 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3850 : case BUILT_IN_ISINF:
3851 : case BUILT_IN_ISNAN:
3852 : return "<math.h>";
3853 29 : CASE_FLT_FN (BUILT_IN_CABS):
3854 29 : CASE_FLT_FN (BUILT_IN_CACOS):
3855 29 : CASE_FLT_FN (BUILT_IN_CACOSH):
3856 29 : CASE_FLT_FN (BUILT_IN_CARG):
3857 29 : CASE_FLT_FN (BUILT_IN_CASIN):
3858 29 : CASE_FLT_FN (BUILT_IN_CASINH):
3859 29 : CASE_FLT_FN (BUILT_IN_CATAN):
3860 29 : CASE_FLT_FN (BUILT_IN_CATANH):
3861 29 : CASE_FLT_FN (BUILT_IN_CCOS):
3862 29 : CASE_FLT_FN (BUILT_IN_CCOSH):
3863 29 : CASE_FLT_FN (BUILT_IN_CEXP):
3864 29 : CASE_FLT_FN (BUILT_IN_CIMAG):
3865 29 : CASE_FLT_FN (BUILT_IN_CLOG):
3866 29 : CASE_FLT_FN (BUILT_IN_CONJ):
3867 29 : CASE_FLT_FN (BUILT_IN_CPOW):
3868 29 : CASE_FLT_FN (BUILT_IN_CPROJ):
3869 29 : CASE_FLT_FN (BUILT_IN_CREAL):
3870 29 : CASE_FLT_FN (BUILT_IN_CSIN):
3871 29 : CASE_FLT_FN (BUILT_IN_CSINH):
3872 29 : CASE_FLT_FN (BUILT_IN_CSQRT):
3873 29 : CASE_FLT_FN (BUILT_IN_CTAN):
3874 29 : CASE_FLT_FN (BUILT_IN_CTANH):
3875 29 : return "<complex.h>";
3876 228 : case BUILT_IN_MEMCHR:
3877 228 : case BUILT_IN_MEMCMP:
3878 228 : case BUILT_IN_MEMCPY:
3879 228 : case BUILT_IN_MEMMOVE:
3880 228 : case BUILT_IN_MEMSET:
3881 228 : case BUILT_IN_STRCAT:
3882 228 : case BUILT_IN_STRCHR:
3883 228 : case BUILT_IN_STRCMP:
3884 228 : case BUILT_IN_STRCPY:
3885 228 : case BUILT_IN_STRCSPN:
3886 228 : case BUILT_IN_STRLEN:
3887 228 : case BUILT_IN_STRNCAT:
3888 228 : case BUILT_IN_STRNCMP:
3889 228 : case BUILT_IN_STRNCPY:
3890 228 : case BUILT_IN_STRPBRK:
3891 228 : case BUILT_IN_STRRCHR:
3892 228 : case BUILT_IN_STRSPN:
3893 228 : case BUILT_IN_STRSTR:
3894 228 : return "<string.h>";
3895 544 : case BUILT_IN_FPRINTF:
3896 544 : case BUILT_IN_PUTC:
3897 544 : case BUILT_IN_FPUTC:
3898 544 : case BUILT_IN_FPUTS:
3899 544 : case BUILT_IN_FSCANF:
3900 544 : case BUILT_IN_FWRITE:
3901 544 : case BUILT_IN_PRINTF:
3902 544 : case BUILT_IN_PUTCHAR:
3903 544 : case BUILT_IN_PUTS:
3904 544 : case BUILT_IN_SCANF:
3905 544 : case BUILT_IN_SNPRINTF:
3906 544 : case BUILT_IN_SPRINTF:
3907 544 : case BUILT_IN_SSCANF:
3908 544 : case BUILT_IN_VFPRINTF:
3909 544 : case BUILT_IN_VFSCANF:
3910 544 : case BUILT_IN_VPRINTF:
3911 544 : case BUILT_IN_VSCANF:
3912 544 : case BUILT_IN_VSNPRINTF:
3913 544 : case BUILT_IN_VSPRINTF:
3914 544 : case BUILT_IN_VSSCANF:
3915 544 : return "<stdio.h>";
3916 2 : case BUILT_IN_ISALNUM:
3917 2 : case BUILT_IN_ISALPHA:
3918 2 : case BUILT_IN_ISBLANK:
3919 2 : case BUILT_IN_ISCNTRL:
3920 2 : case BUILT_IN_ISDIGIT:
3921 2 : case BUILT_IN_ISGRAPH:
3922 2 : case BUILT_IN_ISLOWER:
3923 2 : case BUILT_IN_ISPRINT:
3924 2 : case BUILT_IN_ISPUNCT:
3925 2 : case BUILT_IN_ISSPACE:
3926 2 : case BUILT_IN_ISUPPER:
3927 2 : case BUILT_IN_ISXDIGIT:
3928 2 : case BUILT_IN_TOLOWER:
3929 2 : case BUILT_IN_TOUPPER:
3930 2 : return "<ctype.h>";
3931 0 : case BUILT_IN_ISWALNUM:
3932 0 : case BUILT_IN_ISWALPHA:
3933 0 : case BUILT_IN_ISWBLANK:
3934 0 : case BUILT_IN_ISWCNTRL:
3935 0 : case BUILT_IN_ISWDIGIT:
3936 0 : case BUILT_IN_ISWGRAPH:
3937 0 : case BUILT_IN_ISWLOWER:
3938 0 : case BUILT_IN_ISWPRINT:
3939 0 : case BUILT_IN_ISWPUNCT:
3940 0 : case BUILT_IN_ISWSPACE:
3941 0 : case BUILT_IN_ISWUPPER:
3942 0 : case BUILT_IN_ISWXDIGIT:
3943 0 : case BUILT_IN_TOWLOWER:
3944 0 : case BUILT_IN_TOWUPPER:
3945 0 : return "<wctype.h>";
3946 1818 : case BUILT_IN_ABORT:
3947 1818 : case BUILT_IN_ABS:
3948 1818 : case BUILT_IN_CALLOC:
3949 1818 : case BUILT_IN_EXIT:
3950 1818 : case BUILT_IN_FREE:
3951 1818 : case BUILT_IN_LABS:
3952 1818 : case BUILT_IN_LLABS:
3953 1818 : case BUILT_IN_MALLOC:
3954 1818 : case BUILT_IN_REALLOC:
3955 1818 : case BUILT_IN__EXIT2:
3956 1818 : case BUILT_IN_ALIGNED_ALLOC:
3957 1818 : return "<stdlib.h>";
3958 1 : case BUILT_IN_IMAXABS:
3959 1 : return "<inttypes.h>";
3960 3 : case BUILT_IN_STRFTIME:
3961 3 : return "<time.h>";
3962 : default:
3963 : return NULL;
3964 : }
3965 : }
3966 :
3967 : /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3968 : function of type int (). */
3969 :
3970 : tree
3971 4729 : implicitly_declare (location_t loc, tree functionid)
3972 : {
3973 4729 : struct c_binding *b;
3974 4729 : tree decl = NULL_TREE;
3975 4729 : tree asmspec_tree;
3976 :
3977 4743 : for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3978 : {
3979 3158 : if (B_IN_SCOPE (b, external_scope))
3980 : {
3981 3144 : decl = b->decl;
3982 3144 : break;
3983 : }
3984 : }
3985 :
3986 4729 : if (decl)
3987 : {
3988 3144 : if (TREE_CODE (decl) != FUNCTION_DECL)
3989 : return decl;
3990 :
3991 : /* FIXME: Objective-C has weird not-really-builtin functions
3992 : which are supposed to be visible automatically. They wind up
3993 : in the external scope because they're pushed before the file
3994 : scope gets created. Catch this here and rebind them into the
3995 : file scope. */
3996 3137 : if (!fndecl_built_in_p (decl) && DECL_IS_UNDECLARED_BUILTIN (decl))
3997 : {
3998 0 : bind (functionid, decl, file_scope,
3999 : /*invisible=*/false, /*nested=*/true,
4000 0 : DECL_SOURCE_LOCATION (decl));
4001 0 : return decl;
4002 : }
4003 : else
4004 : {
4005 3137 : tree newtype = default_function_type;
4006 3137 : if (b->u.type)
4007 753 : TREE_TYPE (decl) = b->u.type;
4008 : /* Implicit declaration of a function already declared
4009 : (somehow) in a different scope, or as a built-in.
4010 : If this is the first time this has happened, warn;
4011 : then recycle the old declaration but with the new type. */
4012 3137 : if (!C_DECL_IMPLICIT (decl))
4013 : {
4014 2191 : implicit_decl_permerror (loc, functionid, decl);
4015 2191 : C_DECL_IMPLICIT (decl) = 1;
4016 : }
4017 3137 : if (fndecl_built_in_p (decl))
4018 : {
4019 2705 : newtype = c_build_type_attribute_variant (newtype,
4020 2705 : TYPE_ATTRIBUTES
4021 : (TREE_TYPE (decl)));
4022 2705 : if (!comptypes (newtype, TREE_TYPE (decl)))
4023 : {
4024 2528 : auto_diagnostic_group d;
4025 2528 : bool warned = warning_at (loc,
4026 2528 : OPT_Wbuiltin_declaration_mismatch,
4027 : "incompatible implicit "
4028 : "declaration of built-in "
4029 : "function %qD", decl);
4030 : /* See if we can hint which header to include. */
4031 2528 : const char *header = header_for_builtin_fn (decl);
4032 2528 : if (header != NULL && warned)
4033 : {
4034 133 : rich_location richloc (line_table, loc);
4035 133 : maybe_add_include_fixit (&richloc, header, true);
4036 133 : inform (&richloc,
4037 : "include %qs or provide a declaration of %qD",
4038 : header, decl);
4039 133 : }
4040 2528 : newtype = TREE_TYPE (decl);
4041 2528 : }
4042 : }
4043 : else
4044 : {
4045 432 : if (!comptypes (newtype, TREE_TYPE (decl)))
4046 : {
4047 2 : auto_diagnostic_group d;
4048 2 : error_at (loc, "incompatible implicit declaration of "
4049 : "function %qD", decl);
4050 2 : locate_old_decl (decl);
4051 2 : }
4052 : }
4053 3137 : b->u.type = TREE_TYPE (decl);
4054 3137 : TREE_TYPE (decl) = newtype;
4055 3137 : bind (functionid, decl, current_scope,
4056 : /*invisible=*/false, /*nested=*/true,
4057 3137 : DECL_SOURCE_LOCATION (decl));
4058 3137 : return decl;
4059 : }
4060 : }
4061 :
4062 : /* Not seen before. */
4063 1585 : decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
4064 1585 : DECL_EXTERNAL (decl) = 1;
4065 1585 : TREE_PUBLIC (decl) = 1;
4066 1585 : C_DECL_IMPLICIT (decl) = 1;
4067 1585 : implicit_decl_permerror (loc, functionid, 0);
4068 1585 : asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
4069 1585 : if (asmspec_tree)
4070 1 : set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
4071 :
4072 : /* C89 says implicit declarations are in the innermost block.
4073 : So we record the decl in the standard fashion. */
4074 1585 : decl = pushdecl (decl);
4075 :
4076 : /* No need to call objc_check_decl here - it's a function type. */
4077 1585 : rest_of_decl_compilation (decl, 0, 0);
4078 :
4079 : /* Write a record describing this implicit function declaration
4080 : to the prototypes file (if requested). */
4081 1585 : gen_aux_info_record (decl, 0, 1, 0);
4082 :
4083 : /* Possibly apply some default attributes to this implicit declaration. */
4084 1585 : decl_attributes (&decl, NULL_TREE, 0);
4085 :
4086 1585 : return decl;
4087 : }
4088 :
4089 : /* Issue an error message for a reference to an undeclared variable
4090 : ID, including a reference to a builtin outside of function-call
4091 : context. Establish a binding of the identifier to error_mark_node
4092 : in an appropriate scope, which will suppress further errors for the
4093 : same identifier. The error message should be given location LOC. */
4094 : void
4095 1256 : undeclared_variable (location_t loc, tree id)
4096 : {
4097 1256 : static bool already = false;
4098 1256 : struct c_scope *scope;
4099 :
4100 1256 : auto_diagnostic_group d;
4101 1256 : if (current_function_decl == NULL_TREE)
4102 : {
4103 432 : name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
4104 432 : if (const char *suggestion = guessed_id.suggestion ())
4105 : {
4106 86 : gcc_rich_location richloc (loc);
4107 86 : richloc.add_fixit_replace (suggestion);
4108 86 : error_at (&richloc,
4109 : "%qE undeclared here (not in a function);"
4110 : " did you mean %qs?",
4111 : id, suggestion);
4112 86 : }
4113 : else
4114 346 : error_at (loc, "%qE undeclared here (not in a function)", id);
4115 432 : scope = current_scope;
4116 432 : }
4117 : else
4118 : {
4119 824 : if (!objc_diagnose_private_ivar (id))
4120 : {
4121 824 : name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
4122 824 : if (const char *suggestion = guessed_id.suggestion ())
4123 : {
4124 39 : gcc_rich_location richloc (loc);
4125 39 : richloc.add_fixit_replace (suggestion);
4126 39 : error_at (&richloc,
4127 : "%qE undeclared (first use in this function);"
4128 : " did you mean %qs?",
4129 : id, suggestion);
4130 39 : }
4131 : else
4132 785 : error_at (loc, "%qE undeclared (first use in this function)", id);
4133 824 : }
4134 824 : if (!already)
4135 : {
4136 201 : inform (loc, "each undeclared identifier is reported only"
4137 : " once for each function it appears in");
4138 201 : already = true;
4139 : }
4140 :
4141 : /* If we are parsing old-style parameter decls, current_function_decl
4142 : will be nonnull but current_function_scope will be null. */
4143 824 : scope = current_function_scope ? current_function_scope : current_scope;
4144 : }
4145 1256 : bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
4146 : UNKNOWN_LOCATION);
4147 1256 : }
4148 :
4149 : /* Subroutine of lookup_label, declare_label, define_label: construct a
4150 : LABEL_DECL with all the proper frills. Also create a struct
4151 : c_label_vars initialized for the current scope. */
4152 :
4153 : static tree
4154 24226 : make_label (location_t location, tree name, bool defining,
4155 : struct c_label_vars **p_label_vars)
4156 : {
4157 24226 : tree label = build_decl (location, LABEL_DECL, name, void_type_node);
4158 24226 : DECL_CONTEXT (label) = current_function_decl;
4159 24226 : SET_DECL_MODE (label, VOIDmode);
4160 :
4161 24226 : c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
4162 24226 : label_vars->shadowed = NULL;
4163 24226 : set_spot_bindings (&label_vars->label_bindings, defining);
4164 24226 : label_vars->decls_in_scope = make_tree_vector ();
4165 24226 : label_vars->gotos = NULL;
4166 24226 : *p_label_vars = label_vars;
4167 :
4168 24226 : return label;
4169 : }
4170 :
4171 : /* Get the LABEL_DECL corresponding to identifier NAME as a label.
4172 : Create one if none exists so far for the current function.
4173 : This is called when a label is used in a goto expression or
4174 : has its address taken. */
4175 :
4176 : tree
4177 85820 : lookup_label (tree name)
4178 : {
4179 85820 : tree label;
4180 85820 : struct c_label_vars *label_vars;
4181 :
4182 85820 : if (current_function_scope == 0)
4183 : {
4184 2 : error ("label %qE referenced outside of any function", name);
4185 2 : return NULL_TREE;
4186 : }
4187 :
4188 : /* Use a label already defined or ref'd with this name, but not if
4189 : it is inherited from a containing function and wasn't declared
4190 : using __label__. */
4191 85818 : label = I_LABEL_DECL (name);
4192 79756 : if (label && (DECL_CONTEXT (label) == current_function_decl
4193 608 : || C_DECLARED_LABEL_FLAG (label)))
4194 : {
4195 : /* If the label has only been declared, update its apparent
4196 : location to point here, for better diagnostics if it
4197 : turns out not to have been defined. */
4198 79749 : if (DECL_INITIAL (label) == NULL_TREE)
4199 62801 : DECL_SOURCE_LOCATION (label) = input_location;
4200 79749 : return label;
4201 : }
4202 :
4203 : /* No label binding for that identifier; make one. */
4204 6069 : label = make_label (input_location, name, false, &label_vars);
4205 :
4206 : /* Ordinary labels go in the current function scope. */
4207 6069 : bind_label (name, label, current_function_scope, label_vars);
4208 :
4209 6069 : return label;
4210 : }
4211 :
4212 : /* Issue a warning about DECL for a goto statement at GOTO_LOC going
4213 : to LABEL. */
4214 :
4215 : static void
4216 1490 : warn_about_goto (location_t goto_loc, tree label, tree decl)
4217 : {
4218 1490 : auto_diagnostic_group d;
4219 1490 : if (c_type_variably_modified_p (TREE_TYPE (decl)))
4220 1484 : error_at (goto_loc,
4221 : "jump into scope of identifier with variably modified type");
4222 6 : else if (flag_openmp
4223 6 : && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl)))
4224 2 : error_at (goto_loc, "jump skips OpenMP %<allocate%> allocation");
4225 : else
4226 4 : if (!warning_at (goto_loc, OPT_Wjump_misses_init,
4227 : "jump skips variable initialization"))
4228 0 : return;
4229 1490 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4230 1490 : inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
4231 1490 : }
4232 :
4233 : /* Look up a label because of a goto statement. This is like
4234 : lookup_label, but also issues any appropriate warnings. */
4235 :
4236 : tree
4237 83958 : lookup_label_for_goto (location_t loc, tree name)
4238 : {
4239 83958 : tree label;
4240 83958 : struct c_label_vars *label_vars;
4241 83958 : unsigned int ix;
4242 83958 : tree decl;
4243 :
4244 83958 : label = lookup_label (name);
4245 83958 : if (label == NULL_TREE)
4246 : return NULL_TREE;
4247 :
4248 : /* If we are jumping to a different function, we can't issue any
4249 : useful warnings. */
4250 83958 : if (DECL_CONTEXT (label) != current_function_decl)
4251 : {
4252 522 : gcc_assert (C_DECLARED_LABEL_FLAG (label));
4253 : return label;
4254 : }
4255 :
4256 83436 : label_vars = I_LABEL_BINDING (name)->u.label;
4257 :
4258 : /* If the label has not yet been defined, then push this goto on a
4259 : list for possible later warnings. */
4260 83436 : if (label_vars->label_bindings.scope == NULL)
4261 : {
4262 66713 : c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
4263 :
4264 66713 : g->loc = loc;
4265 66713 : set_spot_bindings (&g->goto_bindings, true);
4266 66713 : vec_safe_push (label_vars->gotos, g);
4267 66713 : return label;
4268 : }
4269 :
4270 : /* If there are any decls in label_vars->decls_in_scope, then this
4271 : goto has missed the declaration of the decl. This happens for a
4272 : case like
4273 : int i = 1;
4274 : lab:
4275 : ...
4276 : goto lab;
4277 : Issue a warning or error. */
4278 17498 : FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
4279 775 : warn_about_goto (loc, label, decl);
4280 :
4281 16723 : if (label_vars->label_bindings.left_stmt_expr)
4282 : {
4283 120 : auto_diagnostic_group d;
4284 120 : error_at (loc, "jump into statement expression");
4285 120 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4286 120 : }
4287 :
4288 : return label;
4289 : }
4290 :
4291 : /* Make a label named NAME in the current function, shadowing silently
4292 : any that may be inherited from containing functions or containing
4293 : scopes. This is called for __label__ declarations. */
4294 :
4295 : tree
4296 1213 : declare_label (tree name)
4297 : {
4298 1213 : struct c_binding *b = I_LABEL_BINDING (name);
4299 1213 : tree label;
4300 1213 : struct c_label_vars *label_vars;
4301 :
4302 : /* Check to make sure that the label hasn't already been declared
4303 : at this scope */
4304 1213 : if (b && B_IN_CURRENT_SCOPE (b))
4305 : {
4306 2 : auto_diagnostic_group d;
4307 2 : error ("duplicate label declaration %qE", name);
4308 2 : locate_old_decl (b->decl);
4309 :
4310 : /* Just use the previous declaration. */
4311 2 : return b->decl;
4312 2 : }
4313 :
4314 1211 : label = make_label (input_location, name, false, &label_vars);
4315 1211 : C_DECLARED_LABEL_FLAG (label) = 1;
4316 :
4317 : /* Declared labels go in the current scope. */
4318 1211 : bind_label (name, label, current_scope, label_vars);
4319 :
4320 1211 : return label;
4321 : }
4322 :
4323 : /* When we define a label, issue any appropriate warnings if there are
4324 : any gotos earlier in the function which jump to this label. */
4325 :
4326 : static void
4327 7200 : check_earlier_gotos (tree label, struct c_label_vars* label_vars)
4328 : {
4329 7200 : unsigned int ix;
4330 7200 : struct c_goto_bindings *g;
4331 :
4332 73847 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
4333 : {
4334 66647 : struct c_binding *b;
4335 66647 : struct c_scope *scope;
4336 :
4337 : /* We have a goto to this label. The goto is going forward. In
4338 : g->scope, the goto is going to skip any binding which was
4339 : defined after g->bindings_in_scope. */
4340 66647 : if (g->goto_bindings.scope->has_jump_unsafe_decl)
4341 : {
4342 255 : for (b = g->goto_bindings.scope->bindings;
4343 593 : b != g->goto_bindings.bindings_in_scope;
4344 338 : b = b->prev)
4345 : {
4346 338 : if (decl_jump_unsafe (b->decl))
4347 176 : warn_about_goto (g->loc, label, b->decl);
4348 : }
4349 : }
4350 :
4351 : /* We also need to warn about decls defined in any scopes
4352 : between the scope of the label and the scope of the goto. */
4353 66647 : for (scope = label_vars->label_bindings.scope;
4354 69344 : scope != g->goto_bindings.scope;
4355 2697 : scope = scope->outer)
4356 : {
4357 2697 : gcc_assert (scope != NULL);
4358 2697 : if (scope->has_jump_unsafe_decl)
4359 : {
4360 325 : if (scope == label_vars->label_bindings.scope)
4361 249 : b = label_vars->label_bindings.bindings_in_scope;
4362 : else
4363 76 : b = scope->bindings;
4364 864 : for (; b != NULL; b = b->prev)
4365 : {
4366 539 : if (decl_jump_unsafe (b->decl))
4367 539 : warn_about_goto (g->loc, label, b->decl);
4368 : }
4369 : }
4370 : }
4371 :
4372 66647 : if (g->goto_bindings.stmt_exprs > 0)
4373 : {
4374 100 : auto_diagnostic_group d;
4375 100 : error_at (g->loc, "jump into statement expression");
4376 100 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
4377 : label);
4378 100 : }
4379 : }
4380 :
4381 : /* Now that the label is defined, we will issue warnings about
4382 : subsequent gotos to this label when we see them. */
4383 7200 : vec_safe_truncate (label_vars->gotos, 0);
4384 7200 : label_vars->gotos = NULL;
4385 7200 : }
4386 :
4387 : /* Define a label, specifying the location in the source file.
4388 : Return the LABEL_DECL node for the label, if the definition is valid.
4389 : Otherwise return NULL_TREE. */
4390 :
4391 : tree
4392 24175 : define_label (location_t location, tree name)
4393 : {
4394 : /* Find any preexisting label with this name. It is an error
4395 : if that label has already been defined in this function, or
4396 : if there is a containing function with a declared label with
4397 : the same name. */
4398 24175 : tree label = I_LABEL_DECL (name);
4399 :
4400 7236 : if (label
4401 7236 : && ((DECL_CONTEXT (label) == current_function_decl
4402 7222 : && DECL_INITIAL (label) != NULL_TREE)
4403 7214 : || (DECL_CONTEXT (label) != current_function_decl
4404 14 : && C_DECLARED_LABEL_FLAG (label))))
4405 : {
4406 29 : auto_diagnostic_group d;
4407 29 : error_at (location, "duplicate label %qD", label);
4408 29 : locate_old_decl (label);
4409 29 : return NULL_TREE;
4410 29 : }
4411 24146 : else if (label && DECL_CONTEXT (label) == current_function_decl)
4412 : {
4413 7200 : struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
4414 :
4415 : /* The label has been used or declared already in this function,
4416 : but not defined. Update its location to point to this
4417 : definition. */
4418 7200 : DECL_SOURCE_LOCATION (label) = location;
4419 7200 : set_spot_bindings (&label_vars->label_bindings, true);
4420 :
4421 : /* Issue warnings as required about any goto statements from
4422 : earlier in the function. */
4423 7200 : check_earlier_gotos (label, label_vars);
4424 : }
4425 : else
4426 : {
4427 16946 : struct c_label_vars *label_vars;
4428 :
4429 : /* No label binding for that identifier; make one. */
4430 16946 : label = make_label (location, name, true, &label_vars);
4431 :
4432 : /* Ordinary labels go in the current function scope. */
4433 16946 : bind_label (name, label, current_function_scope, label_vars);
4434 : }
4435 :
4436 24146 : if (!in_system_header_at (input_location) && lookup_name (name))
4437 143 : warning_at (location, OPT_Wtraditional,
4438 : "traditional C lacks a separate namespace "
4439 : "for labels, identifier %qE conflicts", name);
4440 :
4441 : /* Mark label as having been defined. */
4442 24146 : DECL_INITIAL (label) = error_mark_node;
4443 24146 : return label;
4444 : }
4445 :
4446 : /* Get the bindings for a new switch statement. This is used to issue
4447 : warnings as appropriate for jumps from the switch to case or
4448 : default labels. */
4449 :
4450 : struct c_spot_bindings *
4451 37418 : c_get_switch_bindings (void)
4452 : {
4453 37418 : struct c_spot_bindings *switch_bindings;
4454 :
4455 37418 : switch_bindings = XNEW (struct c_spot_bindings);
4456 37418 : set_spot_bindings (switch_bindings, true);
4457 37418 : return switch_bindings;
4458 : }
4459 :
4460 : void
4461 37418 : c_release_switch_bindings (struct c_spot_bindings *bindings)
4462 : {
4463 37418 : gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
4464 37418 : XDELETE (bindings);
4465 37418 : }
4466 :
4467 : /* This is called at the point of a case or default label to issue
4468 : warnings about decls as needed. It returns true if it found an
4469 : error, not just a warning. */
4470 :
4471 : bool
4472 1030991 : c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
4473 : location_t switch_loc, location_t case_loc)
4474 : {
4475 1030991 : bool saw_error;
4476 1030991 : struct c_scope *scope;
4477 :
4478 1030991 : saw_error = false;
4479 1030991 : for (scope = current_scope;
4480 3092970 : scope != switch_bindings->scope;
4481 2061979 : scope = scope->outer)
4482 : {
4483 2061979 : struct c_binding *b;
4484 :
4485 2061979 : gcc_assert (scope != NULL);
4486 :
4487 2061979 : if (!scope->has_jump_unsafe_decl)
4488 2061968 : continue;
4489 :
4490 22 : for (b = scope->bindings; b != NULL; b = b->prev)
4491 : {
4492 11 : if (decl_jump_unsafe (b->decl))
4493 : {
4494 11 : auto_diagnostic_group d;
4495 11 : bool emitted;
4496 11 : if (c_type_variably_modified_p (TREE_TYPE (b->decl)))
4497 : {
4498 6 : saw_error = true;
4499 6 : error_at (case_loc,
4500 : "switch jumps into scope of identifier with "
4501 : "variably modified type");
4502 6 : emitted = true;
4503 : }
4504 5 : else if (flag_openmp
4505 7 : && lookup_attribute ("omp allocate",
4506 2 : DECL_ATTRIBUTES (b->decl)))
4507 : {
4508 2 : saw_error = true;
4509 2 : error_at (case_loc,
4510 : "switch jumps over OpenMP %<allocate%> allocation");
4511 2 : emitted = true;
4512 : }
4513 : else
4514 3 : emitted
4515 3 : = warning_at (case_loc, OPT_Wjump_misses_init,
4516 : "switch jumps over variable initialization");
4517 11 : if (emitted)
4518 : {
4519 11 : inform (switch_loc, "switch starts here");
4520 11 : inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
4521 : b->decl);
4522 : }
4523 11 : }
4524 : }
4525 : }
4526 :
4527 1030991 : if (switch_bindings->stmt_exprs > 0)
4528 : {
4529 4 : saw_error = true;
4530 4 : auto_diagnostic_group d;
4531 4 : error_at (case_loc, "switch jumps into statement expression");
4532 4 : inform (switch_loc, "switch starts here");
4533 4 : }
4534 :
4535 1030991 : return saw_error;
4536 : }
4537 :
4538 : /* Given NAME, an IDENTIFIER_NODE,
4539 : return the structure (or union or enum) definition for that name.
4540 : If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4541 : CODE says which kind of type the caller wants;
4542 : it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4543 : If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4544 : location where the tag was defined.
4545 : If the wrong kind of type is found, an error is reported. */
4546 :
4547 : static tree
4548 2104730 : lookup_tag (enum tree_code code, tree name, bool thislevel_only,
4549 : location_t *ploc)
4550 : {
4551 2104730 : struct c_binding *b = I_TAG_BINDING (name);
4552 2104730 : bool thislevel = false;
4553 :
4554 2104730 : if (!b || !b->decl)
4555 : return NULL_TREE;
4556 :
4557 : /* We only care about whether it's in this level if
4558 : thislevel_only was set or it might be a type clash. */
4559 1473245 : if (thislevel_only || TREE_CODE (b->decl) != code)
4560 : {
4561 : /* For our purposes, a tag in the external scope is the same as
4562 : a tag in the file scope. (Primarily relevant to Objective-C
4563 : and its builtin structure tags, which get pushed before the
4564 : file scope is created.) */
4565 472176 : if (B_IN_CURRENT_SCOPE (b)
4566 196 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
4567 1473245 : thislevel = true;
4568 : }
4569 :
4570 1473245 : if (thislevel_only && !thislevel)
4571 : return NULL_TREE;
4572 :
4573 1473070 : if (TREE_CODE (b->decl) != code)
4574 : {
4575 : /* Definition isn't the kind we were looking for. */
4576 39 : pending_invalid_xref = name;
4577 39 : pending_invalid_xref_location = input_location;
4578 :
4579 : /* If in the same binding level as a declaration as a tag
4580 : of a different type, this must not be allowed to
4581 : shadow that tag, so give the error immediately.
4582 : (For example, "struct foo; union foo;" is invalid.) */
4583 39 : if (thislevel)
4584 18 : pending_xref_error ();
4585 : }
4586 :
4587 1473070 : if (ploc != NULL)
4588 1026846 : *ploc = b->locus;
4589 :
4590 1473070 : return b->decl;
4591 : }
4592 :
4593 : /* Return true if a definition exists for NAME with code CODE. */
4594 :
4595 : bool
4596 384 : tag_exists_p (enum tree_code code, tree name)
4597 : {
4598 384 : struct c_binding *b = I_TAG_BINDING (name);
4599 :
4600 384 : if (b == NULL || b->decl == NULL_TREE)
4601 : return false;
4602 18 : return TREE_CODE (b->decl) == code;
4603 : }
4604 :
4605 : /* Print an error message now
4606 : for a recent invalid struct, union or enum cross reference.
4607 : We don't print them immediately because they are not invalid
4608 : when used in the `struct foo;' construct for shadowing. */
4609 :
4610 : void
4611 315021505 : pending_xref_error (void)
4612 : {
4613 315021505 : if (pending_invalid_xref != NULL_TREE)
4614 27 : error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4615 : pending_invalid_xref);
4616 315021505 : pending_invalid_xref = NULL_TREE;
4617 315021505 : }
4618 :
4619 :
4620 : /* Look up NAME in the current scope and its superiors
4621 : in the namespace of variables, functions and typedefs.
4622 : Return a ..._DECL node of some kind representing its definition,
4623 : or return NULL_TREE if it is undefined. */
4624 :
4625 : tree
4626 1271167129 : lookup_name (tree name)
4627 : {
4628 1271167129 : struct c_binding *b = I_SYMBOL_BINDING (name);
4629 : /* Do not resolve non-default function versions. */
4630 1271167129 : if (b
4631 857959430 : && TREE_CODE (b->decl) == FUNCTION_DECL
4632 117728050 : && DECL_FUNCTION_VERSIONED (b->decl)
4633 1271167129 : && !is_function_default_version (b->decl))
4634 : return NULL_TREE;
4635 1271167129 : if (b && !b->invisible)
4636 : {
4637 846809263 : maybe_record_typedef_use (b->decl);
4638 846809263 : return b->decl;
4639 : }
4640 : return NULL_TREE;
4641 : }
4642 :
4643 : /* Similar to `lookup_name' but look only at the indicated scope. */
4644 :
4645 : static tree
4646 132207750 : lookup_name_in_scope (tree name, struct c_scope *scope)
4647 : {
4648 132207750 : struct c_binding *b;
4649 :
4650 132210982 : for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4651 7650651 : if (B_IN_SCOPE (b, scope))
4652 7647419 : return b->decl;
4653 : return NULL_TREE;
4654 : }
4655 :
4656 : /* Look for the closest match for NAME within the currently valid
4657 : scopes.
4658 :
4659 : This finds the identifier with the lowest Levenshtein distance to
4660 : NAME. If there are multiple candidates with equal minimal distance,
4661 : the first one found is returned. Scopes are searched from innermost
4662 : outwards, and within a scope in reverse order of declaration, thus
4663 : benefiting candidates "near" to the current scope.
4664 :
4665 : The function also looks for similar macro names to NAME, since a
4666 : misspelled macro name will not be expanded, and hence looks like an
4667 : identifier to the C frontend.
4668 :
4669 : It also looks for start_typename keywords, to detect "singed" vs "signed"
4670 : typos.
4671 :
4672 : Use LOC for any deferred diagnostics. */
4673 :
4674 : name_hint
4675 2061 : lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4676 : {
4677 2061 : gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4678 :
4679 : /* Look up function-like macros first; maybe misusing them. */
4680 4122 : auto cpp_node = cpp_lookup (parse_in,
4681 2061 : (const unsigned char*)IDENTIFIER_POINTER (name),
4682 2061 : IDENTIFIER_LENGTH (name));
4683 2061 : if (cpp_node && cpp_fun_like_macro_p (cpp_node))
4684 5 : return name_hint
4685 : (nullptr,
4686 5 : std::make_unique<macro_like_function_used> (loc,
4687 10 : IDENTIFIER_POINTER (name)));
4688 :
4689 : /* Next, try some well-known names in the C standard library, in case
4690 : the user forgot a #include. */
4691 2056 : const char *header_hint
4692 2056 : = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4693 :
4694 2056 : if (header_hint)
4695 56 : return name_hint
4696 : (nullptr,
4697 56 : std::make_unique<suggest_missing_header> (loc,
4698 112 : IDENTIFIER_POINTER (name),
4699 56 : header_hint));
4700 :
4701 : /* Next, look for exact matches for builtin defines that would have been
4702 : defined if the user had passed a command-line option (e.g. -fopenmp
4703 : for "_OPENMP"). */
4704 2000 : diagnostics::option_id option_id
4705 2000 : = get_option_for_builtin_define (IDENTIFIER_POINTER (name));
4706 2000 : if (option_id.m_idx > 0)
4707 2 : return name_hint
4708 : (nullptr,
4709 2 : std::make_unique<suggest_missing_option> (loc,
4710 4 : IDENTIFIER_POINTER (name),
4711 2 : option_id));
4712 :
4713 : /* Only suggest names reserved for the implementation if NAME begins
4714 : with an underscore. */
4715 1998 : bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4716 :
4717 1998 : best_match<tree, tree> bm (name);
4718 :
4719 : /* Look within currently valid scopes. */
4720 9094 : for (c_scope *scope = current_scope; scope; scope = scope->outer)
4721 11376288 : for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4722 : {
4723 11369192 : if (!binding->id || binding->invisible)
4724 6424784 : continue;
4725 4944408 : if (binding->decl == error_mark_node)
4726 374 : continue;
4727 : /* Don't use bindings from implicitly declared functions,
4728 : as they were likely misspellings themselves. */
4729 4944034 : if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4730 4540069 : if (C_DECL_IMPLICIT (binding->decl))
4731 229 : continue;
4732 : /* Don't suggest names that are reserved for use by the
4733 : implementation, unless NAME began with an underscore. */
4734 4943805 : if (!consider_implementation_names)
4735 : {
4736 2348183 : const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4737 2348183 : if (name_reserved_for_implementation_p (suggestion_str))
4738 2287774 : continue;
4739 : }
4740 2656031 : switch (kind)
4741 : {
4742 24499 : case FUZZY_LOOKUP_TYPENAME:
4743 24499 : if (TREE_CODE (binding->decl) != TYPE_DECL)
4744 17062 : continue;
4745 : break;
4746 :
4747 129747 : case FUZZY_LOOKUP_FUNCTION_NAME:
4748 129747 : if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4749 : {
4750 : /* Allow function pointers. */
4751 26969 : if ((VAR_P (binding->decl)
4752 22444 : || TREE_CODE (binding->decl) == PARM_DECL)
4753 5400 : && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4754 28249 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4755 : == FUNCTION_TYPE))
4756 : break;
4757 26950 : continue;
4758 : }
4759 : break;
4760 :
4761 : default:
4762 : break;
4763 : }
4764 2612019 : bm.consider (binding->id);
4765 : }
4766 :
4767 : /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4768 : as:
4769 : x = SOME_OTHER_MACRO (y);
4770 : then "SOME_OTHER_MACRO" will survive to the frontend and show up
4771 : as a misspelled identifier.
4772 :
4773 : Use the best distance so far so that a candidate is only set if
4774 : a macro is better than anything so far. This allows early rejection
4775 : (without calculating the edit distance) of macro names that must have
4776 : distance >= bm.get_best_distance (), and means that we only get a
4777 : non-NULL result for best_macro_match if it's better than any of
4778 : the identifiers already checked, which avoids needless creation
4779 : of identifiers for macro hashnodes. */
4780 1998 : best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4781 1998 : cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4782 : /* If a macro is the closest so far to NAME, use it, creating an
4783 : identifier tree node for it. */
4784 1998 : if (best_macro)
4785 : {
4786 11 : const char *id = (const char *)best_macro->ident.str;
4787 11 : tree macro_as_identifier
4788 11 : = get_identifier_with_length (id, best_macro->ident.len);
4789 11 : bm.set_best_so_far (macro_as_identifier,
4790 : bmm.get_best_distance (),
4791 : bmm.get_best_candidate_length ());
4792 : }
4793 :
4794 : /* Try the "start_typename" keywords to detect
4795 : "singed" vs "signed" typos. */
4796 1998 : if (kind == FUZZY_LOOKUP_TYPENAME)
4797 : {
4798 56165 : for (unsigned i = 0; i < num_c_common_reswords; i++)
4799 : {
4800 55926 : const c_common_resword *resword = &c_common_reswords[i];
4801 55926 : if (!c_keyword_starts_typename (resword->rid))
4802 42781 : continue;
4803 13145 : tree resword_identifier = ridpointers [resword->rid];
4804 13145 : if (!resword_identifier)
4805 30 : continue;
4806 13115 : gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4807 13115 : bm.consider (resword_identifier);
4808 : }
4809 : }
4810 :
4811 1998 : tree best = bm.get_best_meaningful_candidate ();
4812 1998 : if (best)
4813 278 : return name_hint (IDENTIFIER_POINTER (best), NULL);
4814 : else
4815 1720 : return name_hint (NULL, NULL);
4816 : }
4817 :
4818 :
4819 : /* Handle the standard [[nodiscard]] attribute. */
4820 :
4821 : static tree
4822 34 : handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4823 : int /*flags*/, bool *no_add_attrs)
4824 : {
4825 34 : if (TREE_CODE (*node) == FUNCTION_DECL)
4826 : {
4827 18 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4828 1 : warning_at (DECL_SOURCE_LOCATION (*node),
4829 1 : OPT_Wattributes, "%qE attribute applied to %qD with void "
4830 : "return type", name, *node);
4831 : }
4832 16 : else if (RECORD_OR_UNION_TYPE_P (*node)
4833 12 : || TREE_CODE (*node) == ENUMERAL_TYPE)
4834 : /* OK */;
4835 : else
4836 : {
4837 10 : pedwarn (input_location,
4838 10 : OPT_Wattributes, "%qE attribute can only be applied to "
4839 : "functions or to structure, union or enumeration types", name);
4840 10 : *no_add_attrs = true;
4841 : }
4842 34 : return NULL_TREE;
4843 : }
4844 :
4845 : /* Handle the standard [[noreturn]] attribute. */
4846 :
4847 : static tree
4848 44 : handle_std_noreturn_attribute (tree *node, tree name, tree args,
4849 : int flags, bool *no_add_attrs)
4850 : {
4851 : /* Unlike GNU __attribute__ ((noreturn)), the standard [[noreturn]]
4852 : only applies to functions, not function pointers. */
4853 44 : if (TREE_CODE (*node) == FUNCTION_DECL)
4854 22 : return handle_noreturn_attribute (node, name, args, flags, no_add_attrs);
4855 : else
4856 : {
4857 22 : pedwarn (input_location, OPT_Wattributes,
4858 : "standard %qE attribute can only be applied to functions",
4859 : name);
4860 22 : *no_add_attrs = true;
4861 22 : return NULL_TREE;
4862 : }
4863 : }
4864 :
4865 : /* Handle the standard [[unsequenced]] attribute. */
4866 :
4867 : static tree
4868 68 : handle_std_unsequenced_attribute (tree *node, tree name, tree args,
4869 : int flags, bool *no_add_attrs)
4870 : {
4871 : /* Unlike GNU __attribute__ ((unsequenced)), the standard [[unsequenced]]
4872 : should be only applied to function declarators or type specifiers which
4873 : have function type. */
4874 68 : if (node[2])
4875 : {
4876 8 : auto_diagnostic_group d;
4877 8 : if (pedwarn (input_location, OPT_Wattributes,
4878 : "standard %qE attribute can only be applied to function "
4879 : "declarators or type specifiers with function type", name))
4880 8 : inform (input_location, "did you mean to specify it after %<)%> "
4881 : "following function parameters?");
4882 8 : *no_add_attrs = true;
4883 8 : return NULL_TREE;
4884 8 : }
4885 60 : return handle_unsequenced_attribute (node, name, args, flags, no_add_attrs);
4886 : }
4887 :
4888 : /* Handle the standard [[reproducible]] attribute. */
4889 :
4890 : static tree
4891 34 : handle_std_reproducible_attribute (tree *node, tree name, tree args,
4892 : int flags, bool *no_add_attrs)
4893 : {
4894 34 : return handle_std_unsequenced_attribute (node, name, args, flags,
4895 34 : no_add_attrs);
4896 : }
4897 :
4898 : /* Table of supported standard (C23) attributes. */
4899 : static const attribute_spec std_attributes[] =
4900 : {
4901 : /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4902 : affects_type_identity, handler, exclude } */
4903 : { "_Noreturn", 0, 0, false, false, false, false,
4904 : handle_std_noreturn_attribute, NULL },
4905 : { "deprecated", 0, 1, false, false, false, false,
4906 : handle_deprecated_attribute, NULL },
4907 : { "fallthrough", 0, 0, false, false, false, false,
4908 : handle_fallthrough_attribute, NULL },
4909 : { "maybe_unused", 0, 0, false, false, false, false,
4910 : handle_unused_attribute, NULL },
4911 : { "nodiscard", 0, 1, false, false, false, false,
4912 : handle_nodiscard_attribute, NULL },
4913 : { "noreturn", 0, 0, false, false, false, false,
4914 : handle_std_noreturn_attribute, NULL },
4915 : { "reproducible", 0, 0, false, true, true, false,
4916 : handle_std_reproducible_attribute, NULL },
4917 : { "unsequenced", 0, 0, false, true, true, false,
4918 : handle_std_unsequenced_attribute, NULL }
4919 : };
4920 :
4921 : const scoped_attribute_specs std_attribute_table =
4922 : {
4923 : nullptr, { std_attributes }
4924 : };
4925 :
4926 : /* Create the predefined scalar types of C,
4927 : and some nodes representing standard constants (0, 1, (void *) 0).
4928 : Initialize the global scope.
4929 : Make definitions for built-in primitive functions. */
4930 :
4931 : void
4932 111810 : c_init_decl_processing (void)
4933 : {
4934 111810 : location_t save_loc = input_location;
4935 :
4936 : /* Initialize reserved words for parser. */
4937 111810 : c_parse_init ();
4938 :
4939 111810 : current_function_decl = NULL_TREE;
4940 :
4941 111810 : gcc_obstack_init (&parser_obstack);
4942 :
4943 : /* Make the externals scope. */
4944 111810 : push_scope ();
4945 111810 : external_scope = current_scope;
4946 :
4947 : /* Declarations from c_common_nodes_and_builtins must not be associated
4948 : with this input file, lest we get differences between using and not
4949 : using preprocessed headers. */
4950 111810 : input_location = BUILTINS_LOCATION;
4951 :
4952 111810 : c_common_nodes_and_builtins ();
4953 :
4954 : /* In C, comparisons and TRUTH_* expressions have type int. */
4955 111810 : truthvalue_type_node = integer_type_node;
4956 111810 : truthvalue_true_node = integer_one_node;
4957 111810 : truthvalue_false_node = integer_zero_node;
4958 :
4959 : /* Even in C99, which has a real boolean type. */
4960 111810 : pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL,
4961 111810 : get_identifier (flag_isoc23 ? "bool" : "_Bool"),
4962 : boolean_type_node));
4963 :
4964 : /* C-specific nullptr initialization. */
4965 111810 : record_builtin_type (RID_MAX, "typeof (nullptr)", nullptr_type_node);
4966 : /* The size and alignment of nullptr_t is the same as for a pointer to
4967 : character type. */
4968 111810 : SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4969 :
4970 111810 : input_location = save_loc;
4971 :
4972 111810 : make_fname_decl = c_make_fname_decl;
4973 111810 : start_fname_decls ();
4974 :
4975 111810 : if (warn_keyword_macro)
4976 : {
4977 1175 : for (unsigned int i = 0; i < num_c_common_reswords; ++i)
4978 : /* For C register keywords which don't start with underscore
4979 : or start with just single underscore. Don't complain about
4980 : ObjC or Transactional Memory keywords. */
4981 1170 : if (c_common_reswords[i].word[0] == '_'
4982 540 : && c_common_reswords[i].word[1] == '_')
4983 400 : continue;
4984 995 : else if (c_common_reswords[i].disable
4985 770 : & (D_TRANSMEM | D_OBJC | D_CXX_OBJC))
4986 225 : continue;
4987 : else
4988 : {
4989 545 : tree id = get_identifier (c_common_reswords[i].word);
4990 545 : if (C_IS_RESERVED_WORD (id)
4991 545 : && C_RID_CODE (id) != RID_CXX_COMPAT_WARN)
4992 332 : cpp_warn (parse_in, IDENTIFIER_POINTER (id),
4993 332 : IDENTIFIER_LENGTH (id));
4994 : }
4995 : }
4996 111810 : }
4997 :
4998 : /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4999 : give the decl, NAME is the initialization string and TYPE_DEP
5000 : indicates whether NAME depended on the type of the function. As we
5001 : don't yet implement delayed emission of static data, we mark the
5002 : decl as emitted so it is not placed in the output. Anything using
5003 : it must therefore pull out the STRING_CST initializer directly.
5004 : FIXME. */
5005 :
5006 : static tree
5007 2961 : c_make_fname_decl (location_t loc, tree id, int type_dep)
5008 : {
5009 2961 : const char *name = fname_as_string (type_dep);
5010 2961 : tree decl, type, init;
5011 2961 : size_t length = strlen (name);
5012 :
5013 2961 : type = c_build_array_type (char_type_node,
5014 2961 : build_index_type (size_int (length)));
5015 2961 : type = c_build_qualified_type (type, TYPE_QUAL_CONST);
5016 :
5017 2961 : decl = build_decl (loc, VAR_DECL, id, type);
5018 :
5019 2961 : TREE_STATIC (decl) = 1;
5020 2961 : TREE_READONLY (decl) = 1;
5021 2961 : DECL_ARTIFICIAL (decl) = 1;
5022 :
5023 2961 : init = build_string (length + 1, name);
5024 2961 : free (const_cast<char *> (name));
5025 2961 : TREE_TYPE (init) = type;
5026 2961 : DECL_INITIAL (decl) = init;
5027 :
5028 2961 : TREE_USED (decl) = 1;
5029 :
5030 2961 : if (current_function_decl
5031 : /* For invalid programs like this:
5032 :
5033 : void foo()
5034 : const char* p = __FUNCTION__;
5035 :
5036 : the __FUNCTION__ is believed to appear in K&R style function
5037 : parameter declarator. In that case we still don't have
5038 : function_scope. */
5039 2954 : && current_function_scope)
5040 : {
5041 2946 : DECL_CONTEXT (decl) = current_function_decl;
5042 2946 : bind (id, decl, current_function_scope,
5043 : /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
5044 : }
5045 :
5046 2961 : finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
5047 :
5048 2961 : return decl;
5049 : }
5050 :
5051 : tree
5052 327352285 : c_builtin_function (tree decl)
5053 : {
5054 327352285 : tree type = TREE_TYPE (decl);
5055 327352285 : tree id = DECL_NAME (decl);
5056 :
5057 327352285 : const char *name = IDENTIFIER_POINTER (id);
5058 327352285 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
5059 :
5060 : /* Should never be called on a symbol with a preexisting meaning. */
5061 327352285 : gcc_assert (!I_SYMBOL_BINDING (id));
5062 :
5063 327352285 : bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
5064 : UNKNOWN_LOCATION);
5065 :
5066 : /* Builtins in the implementation namespace are made visible without
5067 : needing to be explicitly declared. See push_file_scope. */
5068 327352285 : if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
5069 : {
5070 223629759 : DECL_CHAIN (decl) = visible_builtins;
5071 223629759 : visible_builtins = decl;
5072 : }
5073 :
5074 327352285 : return decl;
5075 : }
5076 :
5077 : tree
5078 10510318 : c_builtin_function_ext_scope (tree decl)
5079 : {
5080 10510318 : tree type = TREE_TYPE (decl);
5081 10510318 : tree id = DECL_NAME (decl);
5082 :
5083 10510318 : const char *name = IDENTIFIER_POINTER (id);
5084 10510318 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
5085 :
5086 10510318 : if (external_scope)
5087 10320311 : bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
5088 : UNKNOWN_LOCATION);
5089 :
5090 : /* Builtins in the implementation namespace are made visible without
5091 : needing to be explicitly declared. See push_file_scope. */
5092 10510318 : if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
5093 : {
5094 10510318 : DECL_CHAIN (decl) = visible_builtins;
5095 10510318 : visible_builtins = decl;
5096 : }
5097 :
5098 10510318 : return decl;
5099 : }
5100 :
5101 : /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5102 :
5103 : tree
5104 0 : c_simulate_builtin_function_decl (tree decl)
5105 : {
5106 0 : tree type = TREE_TYPE (decl);
5107 0 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
5108 0 : return pushdecl (decl);
5109 : }
5110 :
5111 : /* Warn about attributes in a context where they are unused
5112 : (attribute-declarations, except for the "fallthrough" case, and
5113 : attributes on statements). */
5114 :
5115 : void
5116 41661606 : c_warn_unused_attributes (tree attrs)
5117 : {
5118 41661651 : for (tree t = attrs; t != NULL_TREE; t = TREE_CHAIN (t))
5119 45 : if (get_attribute_namespace (t) == NULL_TREE)
5120 : /* The specifications of standard attributes mean this is a
5121 : constraint violation. */
5122 29 : pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
5123 : get_attribute_name (t));
5124 16 : else if (!attribute_ignored_p (t))
5125 12 : warning (OPT_Wattributes, "%qE attribute ignored",
5126 : get_attribute_name (t));
5127 41661606 : }
5128 :
5129 : /* Warn for standard attributes being applied to a type that is not
5130 : being defined, where that is a constraint violation, and return a
5131 : list of attributes with them removed. */
5132 :
5133 : tree
5134 437207975 : c_warn_type_attributes (tree type, tree attrs)
5135 : {
5136 437207975 : tree *attr_ptr = &attrs;
5137 437231291 : while (*attr_ptr)
5138 23316 : if (get_attribute_namespace (*attr_ptr) == NULL_TREE)
5139 : {
5140 89 : if (TREE_CODE (type) == FUNCTION_TYPE)
5141 : {
5142 65 : tree name = get_attribute_name (*attr_ptr);
5143 : /* [[unsequenced]] and [[reproducible]] is fine on function
5144 : types that aren't being defined. */
5145 65 : if (is_attribute_p ("unsequenced", name)
5146 65 : || is_attribute_p ("reproducible", name))
5147 : {
5148 60 : attr_ptr = &TREE_CHAIN (*attr_ptr);
5149 60 : continue;
5150 : }
5151 : }
5152 29 : pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
5153 : get_attribute_name (*attr_ptr));
5154 29 : *attr_ptr = TREE_CHAIN (*attr_ptr);
5155 : }
5156 : else
5157 23227 : attr_ptr = &TREE_CHAIN (*attr_ptr);
5158 437207975 : return attrs;
5159 : }
5160 :
5161 : /* Called when a declaration is seen that contains no names to declare.
5162 : If its type is a reference to a structure, union or enum inherited
5163 : from a containing scope, shadow that tag name for the current scope
5164 : with a forward reference.
5165 : If its type defines a new named structure or union
5166 : or defines an enum, it is valid but we need not do anything here.
5167 : Otherwise, it is an error. */
5168 :
5169 : void
5170 507839 : shadow_tag (const struct c_declspecs *declspecs)
5171 : {
5172 507839 : shadow_tag_warned (declspecs, 0);
5173 507839 : }
5174 :
5175 : /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
5176 : but no pedwarn. */
5177 : void
5178 507909 : shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
5179 : {
5180 507909 : bool found_tag = false;
5181 :
5182 507909 : if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
5183 : {
5184 507795 : tree value = declspecs->type;
5185 507795 : enum tree_code code = TREE_CODE (value);
5186 :
5187 507795 : if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
5188 : /* Used to test also that TYPE_SIZE (value) != 0.
5189 : That caused warning for `struct foo;' at top level in the file. */
5190 : {
5191 507744 : tree name = TYPE_NAME (value);
5192 507744 : tree t;
5193 :
5194 507744 : found_tag = true;
5195 :
5196 507744 : if (declspecs->restrict_p)
5197 : {
5198 2 : error ("invalid use of %<restrict%>");
5199 2 : warned = 1;
5200 : }
5201 :
5202 507744 : if (in_underspecified_init)
5203 : {
5204 : /* This can only occur with extensions such as statement
5205 : expressions, but is still appropriate as an error to
5206 : avoid types declared in such a context escaping to
5207 : the type of an auto variable. */
5208 1 : error ("%qT declared in underspecified object initializer",
5209 : value);
5210 1 : warned = 1;
5211 : }
5212 :
5213 507744 : if (name == NULL_TREE)
5214 : {
5215 61509 : if (warned != 1 && code != ENUMERAL_TYPE)
5216 : /* Empty unnamed enum OK */
5217 : {
5218 19 : pedwarn (input_location, 0,
5219 : "unnamed struct/union that defines no instances");
5220 19 : warned = 1;
5221 : }
5222 : }
5223 446235 : else if (declspecs->typespec_kind != ctsk_tagdef
5224 89013 : && declspecs->typespec_kind != ctsk_tagfirstref
5225 26431 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5226 26423 : && declspecs->storage_class != csc_none)
5227 : {
5228 2 : if (warned != 1)
5229 2 : pedwarn (input_location, 0,
5230 : "empty declaration with storage class specifier "
5231 : "does not redeclare tag");
5232 2 : warned = 1;
5233 2 : pending_xref_error ();
5234 : }
5235 446233 : else if (declspecs->typespec_kind != ctsk_tagdef
5236 89011 : && declspecs->typespec_kind != ctsk_tagfirstref
5237 26429 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5238 26421 : && (declspecs->const_p
5239 26418 : || declspecs->volatile_p
5240 26418 : || declspecs->atomic_p
5241 26418 : || declspecs->restrict_p
5242 26418 : || declspecs->address_space))
5243 : {
5244 3 : if (warned != 1)
5245 3 : pedwarn (input_location, 0,
5246 : "empty declaration with type qualifier "
5247 : "does not redeclare tag");
5248 3 : warned = 1;
5249 3 : pending_xref_error ();
5250 : }
5251 446230 : else if (declspecs->typespec_kind != ctsk_tagdef
5252 89008 : && declspecs->typespec_kind != ctsk_tagfirstref
5253 26426 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5254 26418 : && declspecs->alignas_p)
5255 : {
5256 1 : if (warned != 1)
5257 1 : pedwarn (input_location, 0,
5258 : "empty declaration with %<_Alignas%> "
5259 : "does not redeclare tag");
5260 1 : warned = 1;
5261 1 : pending_xref_error ();
5262 : }
5263 446229 : else if (declspecs->typespec_kind != ctsk_tagdef
5264 89007 : && declspecs->typespec_kind != ctsk_tagfirstref
5265 26425 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5266 26417 : && code == ENUMERAL_TYPE
5267 15 : && !declspecs->enum_type_specifier_ref_p)
5268 : {
5269 3 : bool warned_enum = false;
5270 3 : if (warned != 1)
5271 3 : warned_enum = pedwarn (input_location, OPT_Wpedantic,
5272 : "empty declaration of %<enum%> type "
5273 : "does not redeclare tag");
5274 3 : if (warned_enum)
5275 2 : warned = 1;
5276 3 : pending_xref_error ();
5277 3 : }
5278 : else
5279 : {
5280 446226 : pending_invalid_xref = NULL_TREE;
5281 446226 : t = lookup_tag (code, name, true, NULL);
5282 :
5283 446226 : if (t == NULL_TREE)
5284 : {
5285 2 : t = make_node (code);
5286 2 : if (flag_isoc23 || code == ENUMERAL_TYPE)
5287 1 : SET_TYPE_STRUCTURAL_EQUALITY (t);
5288 2 : pushtag (input_location, name, t);
5289 : }
5290 : }
5291 : }
5292 : else
5293 : {
5294 51 : if (warned != 1 && !in_system_header_at (input_location))
5295 : {
5296 44 : pedwarn (input_location, 0,
5297 : "useless type name in empty declaration");
5298 44 : warned = 1;
5299 : }
5300 : }
5301 : }
5302 71 : else if (warned != 1 && !in_system_header_at (input_location)
5303 185 : && declspecs->typedef_p)
5304 : {
5305 11 : pedwarn (input_location, 0, "useless type name in empty declaration");
5306 11 : warned = 1;
5307 : }
5308 :
5309 507909 : pending_invalid_xref = NULL_TREE;
5310 :
5311 507909 : if (declspecs->inline_p)
5312 : {
5313 5 : error ("%<inline%> in empty declaration");
5314 5 : warned = 1;
5315 : }
5316 :
5317 507909 : if (declspecs->noreturn_p)
5318 : {
5319 2 : error ("%<_Noreturn%> in empty declaration");
5320 2 : warned = 1;
5321 : }
5322 :
5323 507909 : if (declspecs->constexpr_p)
5324 : {
5325 7 : error ("%<constexpr%> in empty declaration");
5326 7 : warned = 1;
5327 : }
5328 :
5329 507909 : if (current_scope == file_scope && declspecs->storage_class == csc_auto)
5330 : {
5331 2 : error ("%<auto%> in file-scope empty declaration");
5332 2 : warned = 1;
5333 : }
5334 :
5335 507909 : if (current_scope == file_scope && declspecs->storage_class == csc_register)
5336 : {
5337 2 : error ("%<register%> in file-scope empty declaration");
5338 2 : warned = 1;
5339 : }
5340 :
5341 507909 : if (declspecs->enum_type_specifier_ref_p && !warned)
5342 : {
5343 39 : if (declspecs->storage_class != csc_none)
5344 : {
5345 1 : error ("storage class specifier in empty declaration with %<enum%> "
5346 : "underlying type");
5347 1 : warned = 1;
5348 : }
5349 38 : else if (declspecs->thread_p)
5350 : {
5351 2 : error ("%qs in empty declaration with %<enum%> underlying type",
5352 1 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5353 1 : warned = 1;
5354 : }
5355 37 : else if (declspecs->const_p
5356 36 : || declspecs->volatile_p
5357 36 : || declspecs->atomic_p
5358 36 : || declspecs->restrict_p
5359 36 : || declspecs->address_space)
5360 : {
5361 1 : error ("type qualifier in empty declaration with %<enum%> "
5362 : "underlying type");
5363 1 : warned = 1;
5364 : }
5365 36 : else if (declspecs->alignas_p)
5366 : {
5367 1 : error ("%<alignas%> in empty declaration with %<enum%> "
5368 : "underlying type");
5369 1 : warned = 1;
5370 : }
5371 : }
5372 :
5373 507741 : if (!warned && !in_system_header_at (input_location)
5374 627532 : && declspecs->storage_class != csc_none)
5375 : {
5376 12 : warning (0, "useless storage class specifier in empty declaration");
5377 12 : warned = 2;
5378 : }
5379 :
5380 507909 : if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
5381 : {
5382 3 : warning (0, "useless %qs in empty declaration",
5383 3 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5384 3 : warned = 2;
5385 : }
5386 :
5387 3 : if (!warned
5388 507722 : && !in_system_header_at (input_location)
5389 627522 : && (declspecs->const_p
5390 119608 : || declspecs->volatile_p
5391 119608 : || declspecs->atomic_p
5392 119608 : || declspecs->restrict_p
5393 119608 : || declspecs->address_space))
5394 : {
5395 8 : warning (0, "useless type qualifier in empty declaration");
5396 8 : warned = 2;
5397 : }
5398 :
5399 507722 : if (!warned && !in_system_header_at (input_location)
5400 627509 : && declspecs->alignas_p)
5401 : {
5402 4 : warning (0, "useless %<_Alignas%> in empty declaration");
5403 4 : warned = 2;
5404 : }
5405 :
5406 507909 : if (found_tag
5407 507909 : && warned == 2
5408 23 : && (declspecs->typespec_kind == ctsk_tagref_attrs
5409 23 : || declspecs->typespec_kind == ctsk_tagfirstref_attrs))
5410 : {
5411 : /* Standard attributes after the "struct" or "union" keyword are
5412 : only permitted when the contents of the type are defined, or
5413 : in the form "struct-or-union attribute-specifier-sequence
5414 : identifier;". If the ';' was not present, attributes were
5415 : diagnosed in the parser. Here, ensure that any other useless
5416 : elements of the declaration result in a pedwarn, not just a
5417 : warning. Forward declarations of enum types are not part of
5418 : standard C, but handle them the same. */
5419 2 : pedwarn (input_location, 0,
5420 : "invalid use of attributes in empty declaration");
5421 2 : warned = 1;
5422 : }
5423 :
5424 507909 : if (warned != 1)
5425 : {
5426 507735 : if (declspecs->declspecs_seen_p
5427 507735 : && !declspecs->non_std_attrs_seen_p)
5428 : /* An attribute declaration (but not a fallthrough attribute
5429 : declaration, which was handled separately); warn if there
5430 : are any attributes being ignored (but not if the attributes
5431 : were empty). */
5432 48 : c_warn_unused_attributes (declspecs->attrs);
5433 507687 : else if (!found_tag)
5434 10 : pedwarn (input_location, 0, "empty declaration");
5435 : }
5436 507909 : }
5437 :
5438 :
5439 : /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
5440 : bits. SPECS represents declaration specifiers that the grammar
5441 : only permits to contain type qualifiers and attributes. */
5442 :
5443 : int
5444 18384754 : quals_from_declspecs (const struct c_declspecs *specs)
5445 : {
5446 18384754 : int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
5447 18384754 : | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
5448 18384754 : | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
5449 18384754 : | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
5450 18384754 : | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
5451 18384754 : gcc_assert (!specs->type
5452 : && !specs->decl_attr
5453 : && specs->typespec_word == cts_none
5454 : && specs->storage_class == csc_none
5455 : && !specs->typedef_p
5456 : && !specs->explicit_signed_p
5457 : && !specs->deprecated_p
5458 : && !specs->unavailable_p
5459 : && !specs->long_p
5460 : && !specs->long_long_p
5461 : && !specs->short_p
5462 : && !specs->signed_p
5463 : && !specs->unsigned_p
5464 : && !specs->complex_p
5465 : && !specs->inline_p
5466 : && !specs->noreturn_p
5467 : && !specs->thread_p);
5468 18384754 : return quals;
5469 : }
5470 :
5471 : /* Construct an array declarator. LOC is the location of the
5472 : beginning of the array (usually the opening brace). EXPR is the
5473 : expression inside [], or NULL_TREE. QUALS are the type qualifiers
5474 : inside the [] (to be applied to the pointer to which a parameter
5475 : array is converted). STATIC_P is true if "static" is inside the
5476 : [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
5477 : VLA of unspecified length which is nevertheless a complete type,
5478 : false otherwise. The field for the contained declarator is left to
5479 : be filled in by set_array_declarator_inner. */
5480 :
5481 : struct c_declarator *
5482 1141970 : build_array_declarator (location_t loc,
5483 : tree expr, struct c_declspecs *quals, bool static_p,
5484 : bool vla_unspec_p)
5485 : {
5486 1141970 : struct c_declarator *declarator = XOBNEW (&parser_obstack,
5487 : struct c_declarator);
5488 1141970 : declarator->id_loc = loc;
5489 1141970 : declarator->kind = cdk_array;
5490 1141970 : declarator->declarator = 0;
5491 1141970 : declarator->u.array.dimen = expr;
5492 1141970 : if (quals)
5493 : {
5494 966 : declarator->u.array.attrs = quals->attrs;
5495 966 : declarator->u.array.quals = quals_from_declspecs (quals);
5496 : }
5497 : else
5498 : {
5499 1141004 : declarator->u.array.attrs = NULL_TREE;
5500 1141004 : declarator->u.array.quals = 0;
5501 : }
5502 1141970 : declarator->u.array.static_p = static_p;
5503 1141970 : declarator->u.array.vla_unspec_p = vla_unspec_p;
5504 1141970 : if (static_p || quals != NULL)
5505 1069 : pedwarn_c90 (loc, OPT_Wpedantic,
5506 : "ISO C90 does not support %<static%> or type "
5507 : "qualifiers in parameter array declarators");
5508 1141970 : if (vla_unspec_p)
5509 : {
5510 152 : pedwarn_c90 (loc, OPT_Wpedantic,
5511 : "ISO C90 does not support %<[*]%> array declarators");
5512 152 : if (current_scope->parm_flag)
5513 144 : current_scope->had_vla_unspec = true;
5514 : }
5515 1141970 : return declarator;
5516 : }
5517 :
5518 : /* Set the contained declarator of an array declarator. DECL is the
5519 : declarator, as constructed by build_array_declarator; INNER is what
5520 : appears on the left of the []. */
5521 :
5522 : struct c_declarator *
5523 1141970 : set_array_declarator_inner (struct c_declarator *decl,
5524 : struct c_declarator *inner)
5525 : {
5526 1141970 : decl->declarator = inner;
5527 1141970 : return decl;
5528 : }
5529 :
5530 : /* Determine whether TYPE is a one-element array type "[1]". */
5531 : static bool
5532 535582 : one_element_array_type_p (const_tree type)
5533 : {
5534 535582 : if (TREE_CODE (type) != ARRAY_TYPE)
5535 : return false;
5536 535582 : return integer_zerop (array_type_nelts_minus_one (type));
5537 : }
5538 :
5539 : /* Determine whether TYPE is a zero-length array type "[0]". */
5540 : bool
5541 1477237 : zero_length_array_type_p (const_tree type)
5542 : {
5543 1477237 : if (TREE_CODE (type) == ARRAY_TYPE
5544 1477237 : && COMPLETE_TYPE_P (type)
5545 1390198 : && TYPE_DOMAIN (type) != NULL_TREE
5546 2867435 : && (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE
5547 1387648 : || integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))))
5548 2687 : return true;
5549 : return false;
5550 : }
5551 :
5552 : /* INIT is a constructor that forms DECL's initializer. If the final
5553 : element initializes a flexible array field, adjust the size of the
5554 : DECL with the initializer based on whether the DECL is a union or
5555 : a structure. */
5556 :
5557 : static void
5558 105905 : add_flexible_array_elts_to_size (tree decl, tree init)
5559 : {
5560 105905 : tree elt, type;
5561 :
5562 105905 : if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
5563 1601 : return;
5564 :
5565 104304 : elt = CONSTRUCTOR_ELTS (init)->last ().value;
5566 104304 : type = TREE_TYPE (elt);
5567 104304 : if (flexible_array_member_type_p (type))
5568 : {
5569 221 : complete_array_type (&type, elt, false);
5570 : /* For a structure, add the size of the initializer to the DECL's
5571 : size. */
5572 221 : if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
5573 : {
5574 211 : DECL_SIZE (decl)
5575 211 : = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5576 211 : DECL_SIZE_UNIT (decl)
5577 422 : = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
5578 : TYPE_SIZE_UNIT (type));
5579 : }
5580 : /* For a union, the DECL's size is the maximum of the current size
5581 : and the size of the initializer. */
5582 : else
5583 : {
5584 10 : DECL_SIZE (decl)
5585 10 : = size_binop (MAX_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5586 10 : DECL_SIZE_UNIT (decl)
5587 20 : = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
5588 : TYPE_SIZE_UNIT (type));
5589 : }
5590 : }
5591 : }
5592 :
5593 : /* Decode a "typename", such as "int **", returning a ..._TYPE node.
5594 : Set *EXPR, if EXPR not NULL, to any expression to be evaluated
5595 : before the type name, and set *EXPR_CONST_OPERANDS, if
5596 : EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
5597 : appear in a constant expression. */
5598 :
5599 : tree
5600 121658664 : groktypename (struct c_type_name *type_name, tree *expr,
5601 : bool *expr_const_operands)
5602 : {
5603 121658664 : tree type;
5604 121658664 : tree attrs = type_name->specs->attrs;
5605 :
5606 121658664 : type_name->specs->attrs = NULL_TREE;
5607 :
5608 121658664 : type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
5609 : false, NULL, &attrs, expr, expr_const_operands,
5610 : DEPRECATED_NORMAL);
5611 :
5612 : /* Apply attributes. */
5613 121658664 : attrs = c_warn_type_attributes (type, attrs);
5614 121658664 : decl_attributes (&type, attrs, 0);
5615 :
5616 121658664 : return type;
5617 : }
5618 :
5619 :
5620 : /* Decode a "typename", such as "int **", returning a ..._TYPE node,
5621 : as for groktypename but setting the context to GENERIC_ASSOC. */
5622 :
5623 : tree
5624 1444 : grokgenassoc (struct c_type_name *type_name)
5625 : {
5626 1444 : tree type;
5627 1444 : tree attrs = type_name->specs->attrs;
5628 :
5629 1444 : type_name->specs->attrs = NULL_TREE;
5630 :
5631 1444 : type = grokdeclarator (type_name->declarator, type_name->specs, GENERIC_ASSOC,
5632 : false, NULL, &attrs, NULL, NULL, DEPRECATED_NORMAL);
5633 :
5634 : /* Apply attributes. */
5635 1444 : attrs = c_warn_type_attributes (type, attrs);
5636 1444 : decl_attributes (&type, attrs, 0);
5637 :
5638 1444 : return type;
5639 : }
5640 :
5641 :
5642 : /* Looks up the most recent pushed declaration corresponding to DECL. */
5643 :
5644 : static tree
5645 93113891 : lookup_last_decl (tree decl)
5646 : {
5647 93113891 : tree last_decl = lookup_name (DECL_NAME (decl));
5648 93113891 : if (!last_decl)
5649 90525024 : last_decl = lookup_name_in_scope (DECL_NAME (decl), external_scope);
5650 93113891 : return last_decl;
5651 : }
5652 :
5653 : /* Wrapper for decl_attributes that adds some implicit attributes
5654 : to VAR_DECLs or FUNCTION_DECLs. */
5655 :
5656 : static tree
5657 64732404 : c_decl_attributes (tree *node, tree attributes, int flags)
5658 : {
5659 : /* Add implicit "omp declare target" attribute if requested. */
5660 64732404 : if (vec_safe_length (current_omp_declare_target_attribute)
5661 9167 : && ((VAR_P (*node) && is_global_var (*node))
5662 2729 : || TREE_CODE (*node) == FUNCTION_DECL))
5663 : {
5664 1131 : if (VAR_P (*node) && !omp_mappable_type (TREE_TYPE (*node)))
5665 13 : attributes = tree_cons (get_identifier ("omp declare target implicit"),
5666 : NULL_TREE, attributes);
5667 : else
5668 1118 : attributes = tree_cons (get_identifier ("omp declare target"),
5669 : NULL_TREE, attributes);
5670 1131 : if (TREE_CODE (*node) == FUNCTION_DECL)
5671 : {
5672 1001 : int device_type
5673 1001 : = current_omp_declare_target_attribute->last ().device_type;
5674 1001 : device_type = MAX (device_type, 0);
5675 1001 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
5676 1001 : && !lookup_attribute ("omp declare target host", attributes))
5677 2 : attributes
5678 2 : = tree_cons (get_identifier ("omp declare target host"),
5679 : NULL_TREE, attributes);
5680 1001 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
5681 1001 : && !lookup_attribute ("omp declare target nohost", attributes))
5682 2 : attributes
5683 2 : = tree_cons (get_identifier ("omp declare target nohost"),
5684 : NULL_TREE, attributes);
5685 :
5686 1001 : int indirect
5687 1001 : = current_omp_declare_target_attribute->last ().indirect;
5688 1001 : if (indirect && !lookup_attribute ("omp declare target indirect",
5689 : attributes))
5690 10 : attributes
5691 10 : = tree_cons (get_identifier ("omp declare target indirect"),
5692 : NULL_TREE, attributes);
5693 : }
5694 : }
5695 :
5696 64732404 : if (flag_openmp || flag_openmp_simd)
5697 : {
5698 : bool diagnosed = false;
5699 1384701 : for (tree *pa = &attributes; *pa; )
5700 : {
5701 889527 : if (is_attribute_namespace_p ("omp", *pa))
5702 : {
5703 65 : tree name = get_attribute_name (*pa);
5704 65 : if (is_attribute_p ("directive", name)
5705 0 : || is_attribute_p ("sequence", name)
5706 65 : || is_attribute_p ("decl", name))
5707 : {
5708 65 : const char *p = NULL;
5709 65 : if (TREE_VALUE (*pa) == NULL_TREE)
5710 12 : p = IDENTIFIER_POINTER (name);
5711 118 : for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
5712 : {
5713 53 : tree d = TREE_VALUE (a);
5714 53 : gcc_assert (TREE_CODE (d) == C_TOKEN_VEC);
5715 96 : if (TREE_PUBLIC (d)
5716 43 : && (VAR_P (*node)
5717 2 : || TREE_CODE (*node) == FUNCTION_DECL)
5718 96 : && c_maybe_parse_omp_decl (*node, d))
5719 43 : continue;
5720 10 : p = TREE_PUBLIC (d) ? "decl" : "directive";
5721 : }
5722 65 : if (p && !diagnosed)
5723 : {
5724 22 : error ("%<omp::%s%> not allowed to be specified in "
5725 : "this context", p);
5726 22 : diagnosed = true;
5727 : }
5728 65 : if (p)
5729 : {
5730 22 : *pa = TREE_CHAIN (*pa);
5731 22 : continue;
5732 : }
5733 : }
5734 : }
5735 889505 : pa = &TREE_CHAIN (*pa);
5736 : }
5737 : }
5738 :
5739 : /* Look up the current declaration with all the attributes merged
5740 : so far so that attributes on the current declaration that's
5741 : about to be pushed that conflict with the former can be detected,
5742 : diagnosed, and rejected as appropriate. To match the C++ FE, do
5743 : not pass an error_mark_node when we found an undeclared variable. */
5744 64732404 : tree last_decl = lookup_last_decl (*node);
5745 64732404 : if (last_decl == error_mark_node)
5746 20 : last_decl = NULL_TREE;
5747 64732404 : tree attr = decl_attributes (node, attributes, flags, last_decl);
5748 64732403 : if (VAR_P (*node) && DECL_THREAD_LOCAL_P (*node))
5749 : {
5750 : // tls_model attribute can set a stronger TLS access model.
5751 2848 : tls_model model = DECL_TLS_MODEL (*node);
5752 2848 : tls_model default_model = decl_default_tls_model (*node);
5753 2848 : if (default_model > model)
5754 1587 : set_decl_tls_model (*node, default_model);
5755 : }
5756 64732403 : return attr;
5757 : }
5758 :
5759 :
5760 : /* Decode a declarator in an ordinary declaration or data definition.
5761 : This is called as soon as the type information and variable name
5762 : have been parsed, before parsing the initializer if any.
5763 : Here we create the ..._DECL node, fill in its type,
5764 : and (if DO_PUSH) put it on the list of decls for the current context.
5765 : When nonnull, set *LASTLOC to the location of the prior declaration
5766 : of the same entity if one exists.
5767 : The ..._DECL node is returned as the value.
5768 :
5769 : Exception: for arrays where the length is not specified,
5770 : the type is left null, to be filled in by `finish_decl'.
5771 :
5772 : Function definitions do not come here; they go to start_function
5773 : instead. However, external and forward declarations of functions
5774 : do go through here. Structure field declarations are done by
5775 : grokfield and not through here. */
5776 :
5777 : tree
5778 28381511 : start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5779 : bool initialized, tree attributes, bool do_push /* = true */,
5780 : location_t *lastloc /* = NULL */)
5781 : {
5782 28381511 : tree decl;
5783 28381511 : tree old_decl;
5784 28381511 : tree tem;
5785 28381511 : tree expr = NULL_TREE;
5786 28381511 : enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5787 :
5788 : /* An object declared as __attribute__((unavailable)) suppresses
5789 : warnings and errors from __attribute__((deprecated/unavailable))
5790 : components.
5791 : An object declared as __attribute__((deprecated)) suppresses
5792 : warnings of uses of other deprecated items. */
5793 28381511 : if (lookup_attribute ("unavailable", attributes))
5794 : deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
5795 28381487 : else if (lookup_attribute ("deprecated", attributes))
5796 30043 : deprecated_state = DEPRECATED_SUPPRESS;
5797 :
5798 28381511 : decl = grokdeclarator (declarator, declspecs,
5799 : NORMAL, initialized, NULL, &attributes, &expr, NULL,
5800 : deprecated_state);
5801 28381511 : if (!decl || decl == error_mark_node)
5802 : return NULL_TREE;
5803 :
5804 28381487 : old_decl = lookup_last_decl (decl);
5805 :
5806 28381487 : if (tree lastdecl = lastloc ? old_decl : NULL_TREE)
5807 4882082 : if (lastdecl != error_mark_node)
5808 4882073 : *lastloc = DECL_SOURCE_LOCATION (lastdecl);
5809 :
5810 : /* Make sure the size expression is evaluated at this point. */
5811 28381487 : if (expr && !current_scope->parm_flag)
5812 12973 : add_stmt (fold_convert (void_type_node, expr));
5813 :
5814 13285179 : if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5815 28381489 : && TREE_PUBLIC (decl))
5816 1 : warning (OPT_Wmain, "%q+D is usually a function", decl);
5817 :
5818 14 : if (warn_missing_variable_declarations && VAR_P (decl)
5819 28381500 : && !DECL_EXTERNAL (decl) && TREE_PUBLIC (decl) && old_decl == NULL_TREE)
5820 5 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_variable_declarations,
5821 : "no previous declaration for %qD", decl);
5822 :
5823 28381487 : if (initialized)
5824 : /* Is it valid for this decl to have an initializer at all?
5825 : If not, set INITIALIZED to zero, which will indirectly
5826 : tell 'finish_decl' to ignore the initializer once it is parsed. */
5827 6356321 : switch (TREE_CODE (decl))
5828 : {
5829 6 : case TYPE_DECL:
5830 6 : error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5831 6 : initialized = false;
5832 6 : break;
5833 :
5834 9 : case FUNCTION_DECL:
5835 9 : error ("function %qD is initialized like a variable", decl);
5836 9 : initialized = false;
5837 9 : break;
5838 :
5839 49 : case PARM_DECL:
5840 : /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5841 49 : error ("parameter %qD is initialized", decl);
5842 49 : initialized = false;
5843 49 : break;
5844 :
5845 6356257 : default:
5846 : /* Don't allow initializations for incomplete types except for
5847 : arrays which might be completed by the initialization. */
5848 :
5849 : /* This can happen if the array size is an undefined macro.
5850 : We already gave a warning, so we don't need another one. */
5851 6356257 : if (TREE_TYPE (decl) == error_mark_node)
5852 : initialized = false;
5853 6356235 : else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5854 : {
5855 : /* A complete type is ok if size is fixed. If the size is
5856 : variable, an empty initializer is OK and nonempty
5857 : initializers will be diagnosed in the parser. */
5858 : }
5859 12335 : else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5860 : {
5861 3 : error ("variable %qD has initializer but incomplete type", decl);
5862 3 : initialized = false;
5863 : }
5864 : }
5865 :
5866 67 : if (initialized)
5867 : {
5868 6356232 : if (current_scope == file_scope)
5869 156513 : TREE_STATIC (decl) = 1;
5870 :
5871 : /* Tell 'pushdecl' this is an initialized decl
5872 : even though we don't yet have the initializer expression.
5873 : Also tell 'finish_decl' it may store the real initializer. */
5874 6356232 : DECL_INITIAL (decl) = error_mark_node;
5875 : }
5876 :
5877 : /* If this is a function declaration, write a record describing it to the
5878 : prototypes file (if requested). */
5879 :
5880 28381487 : if (TREE_CODE (decl) == FUNCTION_DECL)
5881 15096308 : gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5882 :
5883 : /* ANSI specifies that a tentative definition which is not merged with
5884 : a non-tentative definition behaves exactly like a definition with an
5885 : initializer equal to zero. (Section 3.7.2)
5886 :
5887 : -fno-common gives strict ANSI behavior, though this tends to break
5888 : a large body of code that grew up without this rule.
5889 :
5890 : Thread-local variables are never common, since there's no entrenched
5891 : body of code to break, and it allows more efficient variable references
5892 : in the presence of dynamic linking. */
5893 :
5894 28381487 : if (VAR_P (decl)
5895 8930091 : && !initialized
5896 2573859 : && TREE_PUBLIC (decl)
5897 987435 : && !DECL_THREAD_LOCAL_P (decl)
5898 29366550 : && !flag_no_common)
5899 128 : DECL_COMMON (decl) = 1;
5900 :
5901 : /* Set attributes here so if duplicate decl, will have proper attributes. */
5902 28381487 : c_decl_attributes (&decl, attributes, 0);
5903 :
5904 : /* Handle gnu_inline attribute. */
5905 28381486 : if (declspecs->inline_p
5906 1056 : && !flag_gnu89_inline
5907 1024 : && TREE_CODE (decl) == FUNCTION_DECL
5908 28382502 : && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
5909 993 : || current_function_decl))
5910 : {
5911 46 : if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5912 : ;
5913 37 : else if (declspecs->storage_class != csc_static)
5914 37 : DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5915 : }
5916 :
5917 28381486 : if (TREE_CODE (decl) == FUNCTION_DECL
5918 15096307 : && DECL_DECLARED_INLINE_P (decl)
5919 1046 : && DECL_UNINLINABLE (decl)
5920 28381488 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5921 : {
5922 2 : auto_urlify_attributes sentinel;
5923 2 : warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5924 : decl, "noinline");
5925 2 : }
5926 :
5927 : /* C99 6.7.4p3: An inline definition of a function with external
5928 : linkage shall not contain a definition of a modifiable object
5929 : with static storage duration... */
5930 28381486 : if (VAR_P (decl)
5931 8930091 : && current_scope != file_scope
5932 7779136 : && TREE_STATIC (decl)
5933 78848 : && !TREE_READONLY (decl)
5934 76290 : && DECL_DECLARED_INLINE_P (current_function_decl)
5935 28381556 : && DECL_EXTERNAL (current_function_decl))
5936 7 : record_inline_static (input_location, current_function_decl,
5937 : decl, csi_modifiable);
5938 :
5939 28381486 : if (c_dialect_objc ()
5940 0 : && VAR_OR_FUNCTION_DECL_P (decl))
5941 0 : objc_check_global_decl (decl);
5942 :
5943 : /* To enable versions to be created across TU's we mark and mangle all
5944 : non-default versioned functions. */
5945 28381486 : if (TREE_CODE (decl) == FUNCTION_DECL
5946 : && !TARGET_HAS_FMV_TARGET_ATTRIBUTE
5947 : && get_target_version (decl).is_valid ())
5948 : {
5949 : maybe_mark_function_versioned (decl);
5950 : if (current_scope != file_scope)
5951 : error ("versioned declarations are only allowed at file scope");
5952 : }
5953 :
5954 : /* Add this decl to the current scope.
5955 : TEM may equal DECL or it may be a previous decl of the same name. */
5956 28381486 : if (do_push)
5957 : {
5958 28381134 : tem = pushdecl (decl);
5959 :
5960 28381134 : if (initialized && DECL_EXTERNAL (tem))
5961 : {
5962 27 : DECL_EXTERNAL (tem) = 0;
5963 27 : TREE_STATIC (tem) = 1;
5964 : }
5965 :
5966 28381134 : return tem;
5967 : }
5968 : else
5969 352 : return decl;
5970 : }
5971 :
5972 : /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5973 : DECL or the non-array element type if DECL is an uninitialized array.
5974 : If that type has a const member, diagnose this. */
5975 :
5976 : static void
5977 7 : diagnose_uninitialized_cst_member (tree decl, tree type)
5978 : {
5979 7 : tree field;
5980 17 : for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5981 : {
5982 10 : tree field_type;
5983 10 : if (TREE_CODE (field) != FIELD_DECL)
5984 0 : continue;
5985 10 : field_type = strip_array_types (TREE_TYPE (field));
5986 :
5987 10 : if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5988 : {
5989 5 : auto_diagnostic_group d;
5990 5 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5991 : "uninitialized const member in %qT is invalid in C++",
5992 5 : strip_array_types (TREE_TYPE (decl))))
5993 5 : inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5994 5 : }
5995 :
5996 10 : if (RECORD_OR_UNION_TYPE_P (field_type))
5997 2 : diagnose_uninitialized_cst_member (decl, field_type);
5998 : }
5999 7 : }
6000 :
6001 : /* Finish processing of a declaration;
6002 : install its initial value.
6003 : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6004 : If the length of an array type is not known before,
6005 : it must be determined now, from the initial value, or it is an error.
6006 :
6007 : INIT_LOC is the location of the initial value. */
6008 :
6009 : void
6010 157509735 : finish_decl (tree decl, location_t init_loc, tree init,
6011 : tree origtype, tree asmspec_tree)
6012 : {
6013 157509735 : tree type;
6014 157509735 : bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
6015 157509735 : const char *asmspec = 0;
6016 :
6017 : /* If a name was specified, get the string. */
6018 148576629 : if (VAR_OR_FUNCTION_DECL_P (decl)
6019 172606042 : && DECL_FILE_SCOPE_P (decl))
6020 16248779 : asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
6021 157509735 : if (asmspec_tree)
6022 873123 : asmspec = TREE_STRING_POINTER (asmspec_tree);
6023 :
6024 157509735 : if (VAR_P (decl)
6025 8933106 : && TREE_STATIC (decl)
6026 158573185 : && global_bindings_p ())
6027 : /* So decl is a global variable. Record the types it uses
6028 : so that we can decide later to emit debug info for them. */
6029 981371 : record_types_used_by_current_var_decl (decl);
6030 :
6031 : /* If `start_decl' didn't like having an initialization, ignore it now. */
6032 157509735 : if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
6033 : init = NULL_TREE;
6034 :
6035 : /* Don't crash if parm is initialized. */
6036 157509735 : if (TREE_CODE (decl) == PARM_DECL)
6037 : init = NULL_TREE;
6038 :
6039 32686431 : if (init)
6040 6359248 : store_init_value (init_loc, decl, init, origtype);
6041 :
6042 157509735 : if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
6043 0 : || TREE_CODE (decl) == FIELD_DECL))
6044 0 : objc_check_decl (decl);
6045 :
6046 157509735 : type = TREE_TYPE (decl);
6047 :
6048 : /* Deduce size of array from initialization, if not already known.
6049 : This is only needed for an initialization in the current scope;
6050 : it must not be done for a file-scope initialization of a
6051 : declaration with external linkage, redeclared in an inner scope
6052 : with the outer declaration shadowed in an intermediate scope. */
6053 157509735 : if (TREE_CODE (type) == ARRAY_TYPE
6054 855556 : && TYPE_DOMAIN (type) == NULL_TREE
6055 19963 : && TREE_CODE (decl) != TYPE_DECL
6056 157529600 : && !(TREE_PUBLIC (decl) && current_scope != file_scope))
6057 : {
6058 19678 : bool do_default
6059 19678 : = !TREE_STATIC (decl) && !DECL_EXTERNAL (decl);
6060 19678 : int failure
6061 19678 : = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
6062 : do_default);
6063 :
6064 : /* Get the completed type made by complete_array_type. */
6065 19678 : type = TREE_TYPE (decl);
6066 :
6067 19678 : switch (failure)
6068 : {
6069 0 : case 1:
6070 0 : error ("initializer fails to determine size of %q+D", decl);
6071 0 : break;
6072 :
6073 7371 : case 2:
6074 7371 : if (do_default)
6075 1 : error ("array size missing in %q+D", decl);
6076 7370 : else if (!TREE_PUBLIC (decl))
6077 37 : pedwarn_c23 (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
6078 : "array size missing in %q+D", decl);
6079 : break;
6080 :
6081 3 : case 3:
6082 3 : error ("zero or negative size array %q+D", decl);
6083 3 : break;
6084 :
6085 12304 : case 0:
6086 : /* For global variables, update the copy of the type that
6087 : exists in the binding. */
6088 12304 : if (TREE_PUBLIC (decl))
6089 : {
6090 3830 : struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
6091 7660 : while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
6092 3830 : b_ext = b_ext->shadowed;
6093 3830 : if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
6094 : {
6095 3830 : if (b_ext->u.type && comptypes (b_ext->u.type, type))
6096 176 : b_ext->u.type = composite_type (b_ext->u.type, type);
6097 : else
6098 3654 : b_ext->u.type = type;
6099 : }
6100 : }
6101 : break;
6102 :
6103 0 : default:
6104 0 : gcc_unreachable ();
6105 : }
6106 :
6107 19678 : if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
6108 11435 : TREE_TYPE (DECL_INITIAL (decl)) = type;
6109 :
6110 19678 : relayout_decl (decl);
6111 : }
6112 :
6113 : /* Look for braced array initializers for character arrays and
6114 : recursively convert them into STRING_CSTs. */
6115 157509735 : if (tree init = DECL_INITIAL (decl))
6116 131184971 : DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
6117 :
6118 157509735 : if (VAR_P (decl))
6119 : {
6120 8933106 : if (init && TREE_CODE (init) == CONSTRUCTOR)
6121 105905 : add_flexible_array_elts_to_size (decl, init);
6122 :
6123 8933106 : complete_flexible_array_elts (DECL_INITIAL (decl));
6124 :
6125 8933106 : if (is_global_var (decl))
6126 : {
6127 1234342 : type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
6128 1234342 : ? TCTX_THREAD_STORAGE
6129 : : TCTX_STATIC_STORAGE);
6130 1234342 : if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
6131 0 : TREE_TYPE (decl) = error_mark_node;
6132 : }
6133 :
6134 8941219 : if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
6135 8941116 : && COMPLETE_TYPE_P (TREE_TYPE (decl)))
6136 107 : layout_decl (decl, 0);
6137 :
6138 8933106 : if (DECL_SIZE (decl) == NULL_TREE
6139 : /* Don't give an error if we already gave one earlier. */
6140 8006 : && TREE_TYPE (decl) != error_mark_node
6141 8941009 : && (TREE_STATIC (decl)
6142 : /* A static variable with an incomplete type
6143 : is an error if it is initialized.
6144 : Also if it is not file scope.
6145 : Also if it is thread-local (in C23).
6146 : Otherwise, let it through, but if it is not `extern'
6147 : then it may cause an error message later. */
6148 309 : ? (DECL_INITIAL (decl) != NULL_TREE
6149 309 : || !DECL_FILE_SCOPE_P (decl)
6150 306 : || (flag_isoc23 && DECL_THREAD_LOCAL_P (decl)))
6151 : /* An automatic variable with an incomplete type
6152 : is an error. */
6153 7594 : : !DECL_EXTERNAL (decl)))
6154 : {
6155 19 : error ("storage size of %q+D isn%'t known", decl);
6156 19 : TREE_TYPE (decl) = error_mark_node;
6157 : }
6158 :
6159 17681264 : if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
6160 8663769 : || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6161 282971 : && DECL_SIZE (decl) == NULL_TREE
6162 8933394 : && TREE_STATIC (decl))
6163 135 : incomplete_record_decls.safe_push (decl);
6164 :
6165 8933106 : if (is_global_var (decl)
6166 1234342 : && DECL_SIZE (decl) != NULL_TREE
6167 10159485 : && TREE_TYPE (decl) != error_mark_node)
6168 : {
6169 1226359 : if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
6170 1226356 : constant_expression_warning (DECL_SIZE (decl));
6171 : else
6172 : {
6173 3 : error ("storage size of %q+D isn%'t constant", decl);
6174 3 : TREE_TYPE (decl) = error_mark_node;
6175 : }
6176 : }
6177 :
6178 8933106 : if (TREE_USED (type))
6179 : {
6180 4 : TREE_USED (decl) = 1;
6181 4 : DECL_READ_P (decl) = 1;
6182 : }
6183 : }
6184 :
6185 : /* If this is a function and an assembler name is specified, reset DECL_RTL
6186 : so we can give it its new name. Also, update builtin_decl if it
6187 : was a normal built-in. */
6188 157509735 : if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
6189 : {
6190 859933 : if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6191 72574 : set_builtin_user_assembler_name (decl, asmspec);
6192 859933 : set_user_assembler_name (decl, asmspec);
6193 : }
6194 :
6195 : /* If #pragma weak was used, mark the decl weak now. */
6196 157509735 : maybe_apply_pragma_weak (decl);
6197 :
6198 : /* Output the assembler code and/or RTL code for variables and functions,
6199 : unless the type is an undefined structure or union.
6200 : If not, it will get done when the type is completed. */
6201 :
6202 157509735 : if (VAR_OR_FUNCTION_DECL_P (decl))
6203 : {
6204 : /* Determine the ELF visibility. */
6205 24029413 : if (TREE_PUBLIC (decl))
6206 16223729 : c_determine_visibility (decl);
6207 :
6208 : /* This is a no-op in c-lang.cc or something real in objc-act.cc. */
6209 24029413 : if (c_dialect_objc ())
6210 0 : objc_check_decl (decl);
6211 :
6212 24029413 : if (asmspec)
6213 : {
6214 : /* If this is not a static variable, issue a warning.
6215 : It doesn't make any sense to give an ASMSPEC for an
6216 : ordinary, non-register local variable. Historically,
6217 : GCC has accepted -- but ignored -- the ASMSPEC in
6218 : this case. */
6219 874164 : if (!DECL_FILE_SCOPE_P (decl)
6220 1041 : && VAR_P (decl)
6221 1041 : && !C_DECL_REGISTER (decl)
6222 873131 : && !TREE_STATIC (decl))
6223 1 : warning (0, "ignoring %<asm%> specifier for non-static local "
6224 : "variable %q+D", decl);
6225 : else
6226 873122 : set_user_assembler_name (decl, asmspec);
6227 : }
6228 :
6229 24029413 : if (DECL_FILE_SCOPE_P (decl))
6230 : {
6231 16248779 : if (DECL_INITIAL (decl) == NULL_TREE
6232 16248779 : || DECL_INITIAL (decl) == error_mark_node)
6233 : /* Don't output anything
6234 : when a tentative file-scope definition is seen.
6235 : But at end of compilation, do output code for them. */
6236 16092068 : DECL_DEFER_OUTPUT (decl) = 1;
6237 16248779 : if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
6238 68 : DECL_HARD_REGISTER (decl) = 1;
6239 16248779 : rest_of_decl_compilation (decl, true, 0);
6240 :
6241 16248779 : if (TREE_CODE (decl) == FUNCTION_DECL)
6242 : {
6243 15096240 : tree parms = DECL_ARGUMENTS (decl);
6244 15096240 : const bool builtin = fndecl_built_in_p (decl);
6245 15096240 : if (tree access = build_attr_access_from_parms (parms, !builtin))
6246 347299 : decl_attributes (&decl, access, 0);
6247 : }
6248 : }
6249 : else
6250 : {
6251 : /* In conjunction with an ASMSPEC, the `register'
6252 : keyword indicates that we should place the variable
6253 : in a particular register. */
6254 7780634 : if (asmspec && C_DECL_REGISTER (decl))
6255 : {
6256 1033 : DECL_HARD_REGISTER (decl) = 1;
6257 : /* This cannot be done for a structure with volatile
6258 : fields, on which DECL_REGISTER will have been
6259 : reset. */
6260 1033 : if (!DECL_REGISTER (decl))
6261 1 : error ("cannot put object with volatile field into register");
6262 : }
6263 :
6264 7780634 : if (TREE_CODE (decl) != FUNCTION_DECL)
6265 : {
6266 : /* If we're building a variable sized type, and we might be
6267 : reachable other than via the top of the current binding
6268 : level, then create a new BIND_EXPR so that we deallocate
6269 : the object at the right time. */
6270 : /* Note that DECL_SIZE can be null due to errors. */
6271 7780567 : if (DECL_SIZE (decl)
6272 7780521 : && !TREE_CONSTANT (DECL_SIZE (decl))
6273 7787950 : && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
6274 : {
6275 1337 : tree bind;
6276 1337 : bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6277 1337 : TREE_SIDE_EFFECTS (bind) = 1;
6278 1337 : add_stmt (bind);
6279 1337 : BIND_EXPR_BODY (bind) = push_stmt_list ();
6280 : }
6281 7780567 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
6282 : DECL_EXPR, decl));
6283 : }
6284 : }
6285 :
6286 :
6287 24029413 : if (!DECL_FILE_SCOPE_P (decl))
6288 : {
6289 : /* Recompute the RTL of a local array now
6290 : if it used to be an incomplete type. */
6291 7780634 : if (was_incomplete && !is_global_var (decl))
6292 : {
6293 : /* If we used it already as memory, it must stay in memory. */
6294 5395 : TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6295 : /* If it's still incomplete now, no init will save it. */
6296 5395 : if (DECL_SIZE (decl) == NULL_TREE)
6297 109 : DECL_INITIAL (decl) = NULL_TREE;
6298 : }
6299 : }
6300 : }
6301 :
6302 157509735 : if (TREE_CODE (decl) == TYPE_DECL)
6303 : {
6304 4460641 : if (!DECL_FILE_SCOPE_P (decl)
6305 4460641 : && c_type_variably_modified_p (TREE_TYPE (decl)))
6306 5321 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6307 :
6308 8536637 : rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
6309 : }
6310 :
6311 : /* Install a cleanup (aka destructor) if one was given. */
6312 157509735 : if (VAR_P (decl) && !TREE_STATIC (decl))
6313 : {
6314 7869653 : tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
6315 7869653 : if (attr)
6316 : {
6317 131 : tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
6318 131 : tree cleanup_decl = lookup_name (cleanup_id);
6319 131 : tree cleanup;
6320 131 : vec<tree, va_gc> *v;
6321 :
6322 : /* Build "cleanup(&decl)" for the destructor. */
6323 131 : cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
6324 131 : vec_alloc (v, 1);
6325 131 : v->quick_push (cleanup);
6326 131 : cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
6327 131 : vNULL, cleanup_decl, v, NULL);
6328 131 : vec_free (v);
6329 :
6330 : /* Don't warn about decl unused; the cleanup uses it. */
6331 131 : TREE_USED (decl) = 1;
6332 131 : TREE_USED (cleanup_decl) = 1;
6333 131 : DECL_READ_P (decl) = 1;
6334 :
6335 131 : push_cleanup (decl, cleanup, false);
6336 : }
6337 : }
6338 :
6339 157509735 : if (warn_cxx_compat
6340 22593 : && VAR_P (decl)
6341 7605 : && !DECL_EXTERNAL (decl)
6342 157516897 : && DECL_INITIAL (decl) == NULL_TREE)
6343 : {
6344 2389 : type = strip_array_types (type);
6345 2389 : if (TREE_READONLY (decl))
6346 6 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6347 : "uninitialized %<const %D%> is invalid in C++", decl);
6348 2383 : else if (RECORD_OR_UNION_TYPE_P (type)
6349 2383 : && C_TYPE_FIELDS_READONLY (type))
6350 5 : diagnose_uninitialized_cst_member (decl, type);
6351 : }
6352 :
6353 157509735 : if (flag_openmp
6354 482858 : && VAR_P (decl)
6355 157533882 : && lookup_attribute ("omp declare target implicit",
6356 24147 : DECL_ATTRIBUTES (decl)))
6357 : {
6358 13 : DECL_ATTRIBUTES (decl)
6359 13 : = remove_attribute ("omp declare target implicit",
6360 13 : DECL_ATTRIBUTES (decl));
6361 13 : if (!omp_mappable_type (TREE_TYPE (decl)))
6362 9 : error ("%q+D in declare target directive does not have mappable type",
6363 : decl);
6364 4 : else if (!lookup_attribute ("omp declare target",
6365 4 : DECL_ATTRIBUTES (decl))
6366 8 : && !lookup_attribute ("omp declare target link",
6367 4 : DECL_ATTRIBUTES (decl)))
6368 : {
6369 4 : DECL_ATTRIBUTES (decl)
6370 4 : = tree_cons (get_identifier ("omp declare target"),
6371 4 : NULL_TREE, DECL_ATTRIBUTES (decl));
6372 4 : symtab_node *node = symtab_node::get (decl);
6373 4 : if (node != NULL)
6374 : {
6375 4 : node->offloadable = 1;
6376 4 : if (ENABLE_OFFLOADING)
6377 : {
6378 : g->have_offload = true;
6379 : if (is_a <varpool_node *> (node))
6380 : vec_safe_push (offload_vars, decl);
6381 : }
6382 : }
6383 : }
6384 : }
6385 :
6386 : /* This is the last point we can lower alignment so give the target the
6387 : chance to do so. */
6388 157509735 : if (VAR_P (decl)
6389 8933106 : && !is_global_var (decl)
6390 165208499 : && !DECL_HARD_REGISTER (decl))
6391 7697731 : targetm.lower_local_decl_alignment (decl);
6392 :
6393 157509735 : invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
6394 157509735 : }
6395 :
6396 : /* Given a parsed parameter declaration, decode it into a PARM_DECL.
6397 : EXPR is NULL or a pointer to an expression that needs to be
6398 : evaluated for the side effects of array size expressions in the
6399 : parameters. */
6400 :
6401 : tree
6402 0 : grokparm (const struct c_parm *parm, tree *expr)
6403 : {
6404 0 : tree attrs = parm->attrs;
6405 0 : tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
6406 0 : NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
6407 :
6408 0 : decl_attributes (&decl, attrs, 0);
6409 :
6410 0 : return decl;
6411 : }
6412 :
6413 :
6414 :
6415 : /* Given a parsed parameter declaration, decode it into a PARM_DECL
6416 : and push that on the current scope. EXPR is a pointer to an
6417 : expression that needs to be evaluated for the side effects of array
6418 : size expressions in the parameters. */
6419 :
6420 : void
6421 124800642 : push_parm_decl (const struct c_parm *parm, tree *expr)
6422 : {
6423 124800642 : tree attrs = parm->attrs;
6424 124800642 : tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
6425 124800642 : &attrs, expr, NULL, DEPRECATED_NORMAL);
6426 124800642 : if (decl && DECL_P (decl))
6427 124800642 : DECL_SOURCE_LOCATION (decl) = parm->loc;
6428 :
6429 124800642 : decl_attributes (&decl, attrs, 0);
6430 :
6431 124800642 : decl = pushdecl (decl);
6432 :
6433 124800642 : finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
6434 124800642 : }
6435 :
6436 : /* Mark all the parameter declarations to date as forward decls.
6437 : Also diagnose use of this extension. */
6438 :
6439 : void
6440 42 : mark_forward_parm_decls (void)
6441 : {
6442 42 : struct c_binding *b;
6443 :
6444 42 : if (current_scope->had_forward_parm_decls)
6445 12 : warning_at (input_location, OPT_Wmultiple_parameter_fwd_decl_lists,
6446 : "more than one list of forward declarations of parameters");
6447 42 : if (pedantic && !current_scope->had_forward_parm_decls)
6448 4 : pedwarn (input_location, OPT_Wpedantic,
6449 : "ISO C forbids forward parameter declarations");
6450 :
6451 42 : current_scope->had_forward_parm_decls = true;
6452 :
6453 95 : for (b = current_scope->bindings; b; b = b->prev)
6454 53 : if (TREE_CODE (b->decl) == PARM_DECL)
6455 53 : TREE_ASM_WRITTEN (b->decl) = 1;
6456 42 : }
6457 :
6458 : /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
6459 : literal, which may be an incomplete array type completed by the
6460 : initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
6461 : literal. NON_CONST is true if the initializers contain something
6462 : that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
6463 : it is the (valid) alignment for this compound literal, as specified
6464 : with _Alignas. SCSPECS are the storage class specifiers (C23) from the
6465 : compound literal. */
6466 :
6467 : tree
6468 920285 : build_compound_literal (location_t loc, tree type, tree init, bool non_const,
6469 : unsigned int alignas_align,
6470 : struct c_declspecs *scspecs)
6471 : {
6472 : /* We do not use start_decl here because we have a type, not a declarator;
6473 : and do not use finish_decl because the decl should be stored inside
6474 : the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
6475 920285 : tree decl;
6476 920285 : tree complit;
6477 920285 : tree stmt;
6478 920285 : bool threadp = scspecs ? scspecs->thread_p : false;
6479 307 : enum c_storage_class storage_class = (scspecs
6480 : ? scspecs->storage_class
6481 : : csc_none);
6482 :
6483 920285 : if (type == error_mark_node
6484 920278 : || init == error_mark_node)
6485 : return error_mark_node;
6486 :
6487 920225 : if (current_scope == file_scope && storage_class == csc_register)
6488 : {
6489 1 : error_at (loc, "file-scope compound literal specifies %<register%>");
6490 1 : storage_class = csc_none;
6491 : }
6492 :
6493 920225 : if (current_scope != file_scope && threadp && storage_class == csc_none)
6494 : {
6495 6 : error_at (loc, "compound literal implicitly auto and declared %qs",
6496 3 : scspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6497 3 : threadp = false;
6498 : }
6499 :
6500 920225 : decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
6501 920225 : DECL_EXTERNAL (decl) = 0;
6502 920225 : TREE_PUBLIC (decl) = 0;
6503 1840450 : TREE_STATIC (decl) = (current_scope == file_scope
6504 920225 : || storage_class == csc_static);
6505 920225 : DECL_CONTEXT (decl) = current_function_decl;
6506 920225 : TREE_USED (decl) = 1;
6507 920225 : DECL_READ_P (decl) = 1;
6508 920225 : DECL_ARTIFICIAL (decl) = 1;
6509 920225 : DECL_IGNORED_P (decl) = 1;
6510 920225 : C_DECL_COMPOUND_LITERAL_P (decl) = 1;
6511 920225 : C_DECL_DECLARED_CONSTEXPR (decl) = scspecs && scspecs->constexpr_p;
6512 920225 : TREE_TYPE (decl) = type;
6513 920225 : if (threadp)
6514 16 : set_decl_tls_model (decl, decl_default_tls_model (decl));
6515 920225 : if (storage_class == csc_register)
6516 : {
6517 25 : C_DECL_REGISTER (decl) = 1;
6518 25 : DECL_REGISTER (decl) = 1;
6519 : }
6520 920225 : c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
6521 920225 : if (alignas_align)
6522 : {
6523 3 : SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6524 3 : DECL_USER_ALIGN (decl) = 1;
6525 : }
6526 920225 : store_init_value (loc, decl, init, NULL_TREE);
6527 920225 : if (current_scope != file_scope
6528 919974 : && TREE_STATIC (decl)
6529 28 : && !TREE_READONLY (decl)
6530 17 : && DECL_DECLARED_INLINE_P (current_function_decl)
6531 920231 : && DECL_EXTERNAL (current_function_decl))
6532 4 : record_inline_static (input_location, current_function_decl,
6533 : decl, csi_modifiable);
6534 :
6535 920225 : if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
6536 : {
6537 267 : int failure = complete_array_type (&TREE_TYPE (decl),
6538 267 : DECL_INITIAL (decl), true);
6539 : /* If complete_array_type returns 3, it means that the initial value of
6540 : the compound literal is empty. Allow it with a pedwarn; in pre-C23
6541 : modes, the empty initializer itself has been diagnosed if pedantic so
6542 : does not need to be diagnosed again here. */
6543 267 : gcc_assert (failure == 0 || failure == 3);
6544 267 : if (failure == 3 && flag_isoc23)
6545 1 : pedwarn (loc, OPT_Wpedantic,
6546 : "array of unknown size with empty initializer");
6547 :
6548 267 : type = TREE_TYPE (decl);
6549 267 : TREE_TYPE (DECL_INITIAL (decl)) = type;
6550 267 : relayout_decl (decl);
6551 : }
6552 :
6553 920225 : if (type == error_mark_node || !COMPLETE_TYPE_P (type))
6554 : {
6555 19 : c_incomplete_type_error (loc, NULL_TREE, type);
6556 19 : return error_mark_node;
6557 : }
6558 :
6559 919927 : if ((TREE_STATIC (decl) || C_DECL_DECLARED_CONSTEXPR (decl))
6560 920427 : && C_TYPE_VARIABLE_SIZE (type))
6561 2 : error_at (loc, "storage size isn%'t constant");
6562 :
6563 920206 : if (TREE_STATIC (decl)
6564 920206 : && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
6565 0 : return error_mark_node;
6566 :
6567 920206 : stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
6568 920206 : complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
6569 920206 : TREE_SIDE_EFFECTS (complit) = 1;
6570 :
6571 920206 : layout_decl (decl, 0);
6572 :
6573 920206 : if (TREE_STATIC (decl))
6574 : {
6575 : /* This decl needs a name for the assembler output. */
6576 279 : set_compound_literal_name (decl);
6577 279 : DECL_DEFER_OUTPUT (decl) = 1;
6578 279 : DECL_COMDAT (decl) = 1;
6579 279 : pushdecl (decl);
6580 279 : rest_of_decl_compilation (decl, 1, 0);
6581 : }
6582 919927 : else if (current_function_decl && !current_scope->parm_flag)
6583 919918 : pushdecl (decl);
6584 :
6585 920206 : if (non_const)
6586 : {
6587 221 : complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
6588 221 : C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
6589 : }
6590 :
6591 : return complit;
6592 : }
6593 :
6594 : /* Check the type of a compound literal. Here we just check that it
6595 : is valid for C++. */
6596 :
6597 : void
6598 920282 : check_compound_literal_type (location_t loc, struct c_type_name *type_name)
6599 : {
6600 920282 : if (warn_cxx_compat
6601 167 : && (type_name->specs->typespec_kind == ctsk_tagdef
6602 166 : || type_name->specs->typespec_kind == ctsk_tagfirstref
6603 165 : || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
6604 2 : warning_at (loc, OPT_Wc___compat,
6605 : "defining a type in a compound literal is invalid in C++");
6606 920282 : }
6607 :
6608 : /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6609 : replacing with appropriate values if they are invalid. */
6610 :
6611 : static void
6612 52817 : check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
6613 : tree orig_name)
6614 : {
6615 52817 : tree type_mv;
6616 52817 : unsigned int max_width;
6617 52817 : unsigned HOST_WIDE_INT w;
6618 52817 : const char *name = (orig_name
6619 95827 : ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
6620 9807 : : _("<anonymous>"));
6621 :
6622 : /* Detect and ignore out of range field width and process valid
6623 : field widths. */
6624 52817 : if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
6625 : {
6626 8 : error_at (loc, "bit-field %qs width not an integer constant", name);
6627 8 : *width = integer_one_node;
6628 : }
6629 : else
6630 : {
6631 52809 : if (TREE_CODE (*width) != INTEGER_CST)
6632 : {
6633 12 : *width = c_fully_fold (*width, false, NULL);
6634 12 : if (TREE_CODE (*width) == INTEGER_CST)
6635 6 : pedwarn (loc, OPT_Wpedantic,
6636 : "bit-field %qs width not an integer constant expression",
6637 : name);
6638 : }
6639 52809 : if (TREE_CODE (*width) != INTEGER_CST)
6640 : {
6641 6 : error_at (loc, "bit-field %qs width not an integer constant", name);
6642 6 : *width = integer_one_node;
6643 : }
6644 52809 : constant_expression_warning (*width);
6645 52809 : if (tree_int_cst_sgn (*width) < 0)
6646 : {
6647 2 : error_at (loc, "negative width in bit-field %qs", name);
6648 2 : *width = integer_one_node;
6649 : }
6650 52807 : else if (integer_zerop (*width) && orig_name)
6651 : {
6652 4 : error_at (loc, "zero width for bit-field %qs", name);
6653 4 : *width = integer_one_node;
6654 : }
6655 : }
6656 :
6657 : /* Detect invalid bit-field type. */
6658 52817 : if (TREE_CODE (*type) != INTEGER_TYPE
6659 : && TREE_CODE (*type) != BOOLEAN_TYPE
6660 : && TREE_CODE (*type) != ENUMERAL_TYPE
6661 52817 : && TREE_CODE (*type) != BITINT_TYPE)
6662 : {
6663 8 : error_at (loc, "bit-field %qs has invalid type", name);
6664 8 : *type = unsigned_type_node;
6665 : }
6666 :
6667 52817 : if (TYPE_WARN_IF_NOT_ALIGN (*type))
6668 : {
6669 1 : error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6670 : name);
6671 1 : *type = unsigned_type_node;
6672 : }
6673 :
6674 52817 : type_mv = TYPE_MAIN_VARIANT (*type);
6675 52817 : if (!in_system_header_at (input_location)
6676 29369 : && type_mv != integer_type_node
6677 26630 : && type_mv != unsigned_type_node
6678 65433 : && type_mv != boolean_type_node)
6679 12033 : pedwarn_c90 (loc, OPT_Wpedantic,
6680 : "type of bit-field %qs is a GCC extension", name);
6681 :
6682 52817 : max_width = TYPE_PRECISION (*type);
6683 :
6684 52817 : if (compare_tree_int (*width, max_width) > 0)
6685 : {
6686 4 : error_at (loc, "width of %qs exceeds its type", name);
6687 4 : w = max_width;
6688 4 : *width = build_int_cst (integer_type_node, w);
6689 : }
6690 : else
6691 52813 : w = tree_to_uhwi (*width);
6692 :
6693 : /* Truncation of hardbool false and true representation values is always safe:
6694 : either the values remain different, or we'll report a problem when creating
6695 : the narrower type. */
6696 52817 : if (c_hardbool_type_attr (*type))
6697 52817 : return;
6698 :
6699 52536 : if (TREE_CODE (*type) == ENUMERAL_TYPE)
6700 : {
6701 4140 : struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
6702 4140 : if (!lt
6703 4130 : || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
6704 8266 : || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
6705 15 : warning_at (loc, 0, "%qs is narrower than values of its type", name);
6706 : }
6707 : }
6708 :
6709 :
6710 :
6711 : /* Print warning about variable length array if necessary. */
6712 :
6713 : static void
6714 22493 : warn_variable_length_array (tree name, tree size)
6715 : {
6716 22493 : if (TREE_CONSTANT (size))
6717 : {
6718 181 : if (name)
6719 168 : pedwarn_c90 (input_location, OPT_Wvla,
6720 : "ISO C90 forbids array %qE whose size "
6721 : "cannot be evaluated", name);
6722 : else
6723 13 : pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
6724 : "whose size cannot be evaluated");
6725 : }
6726 : else
6727 : {
6728 22312 : if (name)
6729 13828 : pedwarn_c90 (input_location, OPT_Wvla,
6730 : "ISO C90 forbids variable length array %qE", name);
6731 : else
6732 8484 : pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
6733 : "length array");
6734 : }
6735 22493 : }
6736 :
6737 : /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6738 : considering only those c_declspec_words found in LIST, which
6739 : must be terminated by cdw_number_of_elements. */
6740 :
6741 : static location_t
6742 235 : smallest_type_quals_location (const location_t *locations,
6743 : const c_declspec_word *list)
6744 : {
6745 235 : location_t loc = UNKNOWN_LOCATION;
6746 1410 : while (*list != cdw_number_of_elements)
6747 : {
6748 1175 : location_t newloc = locations[*list];
6749 1175 : if (loc == UNKNOWN_LOCATION
6750 288 : || (newloc != UNKNOWN_LOCATION && newloc < loc))
6751 887 : loc = newloc;
6752 1175 : list++;
6753 : }
6754 :
6755 235 : return loc;
6756 : }
6757 :
6758 :
6759 : /* We attach an artificial TYPE_DECL to pointed-to type
6760 : and arrange for it to be included in a DECL_EXPR. This
6761 : forces the sizes evaluation at a safe point and ensures it
6762 : is not deferred until e.g. within a deeper conditional context.
6763 :
6764 : PARM contexts have no enclosing statement list that
6765 : can hold the DECL_EXPR, so we need to use a BIND_EXPR
6766 : instead, and add it to the list of expressions that
6767 : need to be evaluated.
6768 :
6769 : TYPENAME contexts do have an enclosing statement list,
6770 : but it would be incorrect to use it, as the size should
6771 : only be evaluated if the containing expression is
6772 : evaluated. We might also be in the middle of an
6773 : expression with side effects on the pointed-to type size
6774 : "arguments" prior to the pointer declaration point and
6775 : the fake TYPE_DECL in the enclosing context would force
6776 : the size evaluation prior to the side effects. We therefore
6777 : use BIND_EXPRs in TYPENAME contexts too. */
6778 : static void
6779 7040 : add_decl_expr (location_t loc, tree type, tree *expr, bool set_name_p)
6780 : {
6781 7040 : tree bind = NULL_TREE;
6782 7040 : if (expr)
6783 : {
6784 1598 : bind = build3 (BIND_EXPR, void_type_node, NULL_TREE, NULL_TREE,
6785 : NULL_TREE);
6786 1598 : TREE_SIDE_EFFECTS (bind) = 1;
6787 1598 : BIND_EXPR_BODY (bind) = push_stmt_list ();
6788 1598 : push_scope ();
6789 : }
6790 :
6791 7040 : tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6792 7040 : pushdecl (decl);
6793 7040 : DECL_ARTIFICIAL (decl) = 1;
6794 7040 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6795 7040 : if (set_name_p)
6796 6324 : TYPE_NAME (type) = decl;
6797 :
6798 7040 : if (bind)
6799 : {
6800 1598 : pop_scope ();
6801 1598 : BIND_EXPR_BODY (bind) = pop_stmt_list (BIND_EXPR_BODY (bind));
6802 1598 : if (*expr)
6803 1476 : *expr = build2 (COMPOUND_EXPR, void_type_node, *expr, bind);
6804 : else
6805 122 : *expr = bind;
6806 : }
6807 7040 : }
6808 :
6809 :
6810 : /* Add attribute "arg spec" to ATTRS corresponding to an array/VLA parameter
6811 : declared with type TYPE. The attribute has two arguments. The first is
6812 : a string that encodes the presence of the static keyword. The second is
6813 : the declared type of the array before adjustment, i.e. as an array type
6814 : including the outermost bound. */
6815 :
6816 : static tree
6817 439712 : build_arg_spec_attribute (tree type, bool static_p, tree attrs)
6818 : {
6819 439712 : tree vbchain = tree_cons (NULL_TREE, type, NULL_TREE);
6820 439712 : tree acsstr = static_p ? build_string (7, "static") :
6821 439590 : build_string (1, "");
6822 439712 : tree args = tree_cons (NULL_TREE, acsstr, vbchain);
6823 439712 : tree name = get_identifier ("arg spec");
6824 439712 : return tree_cons (name, args, attrs);
6825 : }
6826 :
6827 :
6828 : /* Given declspecs and a declarator,
6829 : determine the name and type of the object declared
6830 : and construct a ..._DECL node for it.
6831 : (In one case we can return a ..._TYPE node instead.
6832 : For invalid input we sometimes return NULL_TREE.)
6833 :
6834 : DECLSPECS is a c_declspecs structure for the declaration specifiers.
6835 :
6836 : DECL_CONTEXT says which syntactic context this declaration is in:
6837 : NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6838 : FUNCDEF for a function definition. Like NORMAL but a few different
6839 : error messages in each case. Return value may be zero meaning
6840 : this definition is too screwy to try to parse.
6841 : PARM for a parameter declaration (either within a function prototype
6842 : or before a function body). Make a PARM_DECL, or return void_type_node.
6843 : TYPENAME if for a typename (in a cast or sizeof).
6844 : Don't make a DECL node; just return the ..._TYPE node.
6845 : GENERIC_ASSOC for typenames in a generic association.
6846 : FIELD for a struct or union field; make a FIELD_DECL.
6847 : INITIALIZED is true if the decl has an initializer.
6848 : WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6849 : representing the width of the bit-field.
6850 : DECL_ATTRS points to the list of attributes that should be added to this
6851 : decl. Any nested attributes that belong on the decl itself will be
6852 : added to this list.
6853 : If EXPR is not NULL, any expressions that need to be evaluated as
6854 : part of evaluating variably modified types will be stored in *EXPR.
6855 : If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6856 : set to indicate whether operands in *EXPR can be used in constant
6857 : expressions.
6858 : DEPRECATED_STATE is a deprecated_states value indicating whether
6859 : deprecation/unavailability warnings should be suppressed.
6860 :
6861 : In the TYPENAME case, DECLARATOR is really an absolute declarator.
6862 : It may also be so in the PARM case, for a prototype where the
6863 : argument type is specified but not the name.
6864 :
6865 : This function is where the complicated C meanings of `static'
6866 : and `extern' are interpreted. */
6867 :
6868 : static tree
6869 315517617 : grokdeclarator (const struct c_declarator *declarator,
6870 : struct c_declspecs *declspecs,
6871 : enum decl_context decl_context, bool initialized, tree *width,
6872 : tree *decl_attrs, tree *expr, bool *expr_const_operands,
6873 : enum deprecated_states deprecated_state)
6874 : {
6875 315517617 : tree type = declspecs->type;
6876 315517617 : bool threadp = declspecs->thread_p;
6877 315517617 : bool constexprp = declspecs->constexpr_p;
6878 315517617 : enum c_storage_class storage_class = declspecs->storage_class;
6879 315517617 : int constp;
6880 315517617 : int restrictp;
6881 315517617 : int volatilep;
6882 315517617 : int atomicp;
6883 315517617 : int type_quals = TYPE_UNQUALIFIED;
6884 315517617 : tree name = NULL_TREE;
6885 315517617 : bool funcdef_flag = false;
6886 315517617 : bool funcdef_syntax = false;
6887 315517617 : bool size_varies = false;
6888 315517617 : bool size_error = false;
6889 315517617 : tree decl_attr = declspecs->decl_attr;
6890 315517617 : int array_ptr_quals = TYPE_UNQUALIFIED;
6891 315517617 : tree array_ptr_attrs = NULL_TREE;
6892 315517617 : bool array_parm_static = false;
6893 315517617 : bool array_parm_vla_unspec_p = false;
6894 315517617 : tree returned_attrs = NULL_TREE;
6895 315517617 : tree decl_id_attrs = NULL_TREE;
6896 315517617 : bool bitfield = width != NULL;
6897 315517617 : tree element_type;
6898 315517617 : tree orig_qual_type = NULL;
6899 315517617 : size_t orig_qual_indirect = 0;
6900 315517617 : struct c_arg_info *arg_info = 0;
6901 315517617 : addr_space_t as1, as2, address_space;
6902 315517617 : location_t loc = UNKNOWN_LOCATION;
6903 315517617 : tree expr_dummy;
6904 315517617 : bool expr_const_operands_dummy;
6905 315517617 : enum c_declarator_kind first_non_attr_kind;
6906 315517617 : unsigned int alignas_align = 0;
6907 :
6908 315517617 : if (type == NULL_TREE)
6909 : {
6910 : /* This can occur for auto on a parameter in C23 mode. Set a
6911 : dummy type here so subsequent code can give diagnostics for
6912 : this case. */
6913 2 : gcc_assert (declspecs->c23_auto_p);
6914 2 : gcc_assert (decl_context == PARM);
6915 2 : type = declspecs->type = integer_type_node;
6916 : }
6917 315517617 : if (TREE_CODE (type) == ERROR_MARK)
6918 29 : return error_mark_node;
6919 315517588 : if (expr == NULL)
6920 : {
6921 38250 : expr = &expr_dummy;
6922 38250 : expr_dummy = NULL_TREE;
6923 : }
6924 315517588 : if (expr_const_operands == NULL)
6925 193916950 : expr_const_operands = &expr_const_operands_dummy;
6926 :
6927 315517588 : if (declspecs->expr)
6928 : {
6929 953 : if (*expr)
6930 7 : *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6931 : declspecs->expr);
6932 : else
6933 946 : *expr = declspecs->expr;
6934 : }
6935 315517588 : *expr_const_operands = declspecs->expr_const_operands;
6936 :
6937 315517588 : if (decl_context == FUNCDEF)
6938 36350955 : funcdef_flag = true, decl_context = NORMAL;
6939 :
6940 : /* Look inside a declarator for the name being declared
6941 : and get it as an IDENTIFIER_NODE, for an error message. */
6942 315517588 : {
6943 315517588 : const struct c_declarator *decl = declarator;
6944 :
6945 315517588 : first_non_attr_kind = cdk_attrs;
6946 386083447 : while (decl)
6947 386083447 : switch (decl->kind)
6948 : {
6949 1141970 : case cdk_array:
6950 1141970 : loc = decl->id_loc;
6951 : /* FALL THRU. */
6952 :
6953 70559027 : case cdk_function:
6954 70559027 : case cdk_pointer:
6955 70559027 : funcdef_syntax = (decl->kind == cdk_function);
6956 70559027 : if (first_non_attr_kind == cdk_attrs)
6957 68423425 : first_non_attr_kind = decl->kind;
6958 70559027 : decl = decl->declarator;
6959 70559027 : break;
6960 :
6961 6832 : case cdk_attrs:
6962 6832 : decl = decl->declarator;
6963 6832 : break;
6964 :
6965 315517588 : case cdk_id:
6966 315517588 : loc = decl->id_loc;
6967 315517588 : if (decl->u.id.id)
6968 : name = decl->u.id.id;
6969 315517588 : decl_id_attrs = decl->u.id.attrs;
6970 315517588 : if (first_non_attr_kind == cdk_attrs)
6971 247094163 : first_non_attr_kind = decl->kind;
6972 : decl = 0;
6973 : break;
6974 :
6975 0 : default:
6976 0 : gcc_unreachable ();
6977 : }
6978 315517588 : if (name == NULL_TREE)
6979 : {
6980 126339204 : gcc_assert (decl_context == PARM
6981 : || decl_context == TYPENAME
6982 : || decl_context == GENERIC_ASSOC
6983 : || (decl_context == FIELD
6984 : && declarator->kind == cdk_id));
6985 126339204 : gcc_assert (!initialized);
6986 : }
6987 : }
6988 :
6989 : /* An enum type specifier (": specifier-qualifier-list") may only be
6990 : specified when the enum is being defined or in an empty
6991 : declaration of the form "enum identifier enum-type-specifier;".
6992 : Except for the case of an empty declaration that has additional
6993 : declaration specifiers, all invalid contexts (declarations that
6994 : aren't empty, type names, parameter declarations, member
6995 : declarations) pass through grokdeclarator. */
6996 315517588 : if (declspecs->enum_type_specifier_ref_p)
6997 6 : error_at (loc, "%<enum%> underlying type may not be specified here");
6998 :
6999 : /* A function definition's declarator must have the form of
7000 : a function declarator. */
7001 :
7002 315517588 : if (funcdef_flag && !funcdef_syntax)
7003 : return NULL_TREE;
7004 :
7005 : /* If this looks like a function definition, make it one,
7006 : even if it occurs where parms are expected.
7007 : Then store_parm_decls will reject it and not use it as a parm. */
7008 315517557 : if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
7009 22662 : decl_context = PARM;
7010 :
7011 315517557 : if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
7012 : {
7013 315517533 : if (declspecs->unavailable_p)
7014 28 : error_unavailable_use (declspecs->type, declspecs->decl_attr);
7015 315517505 : else if (declspecs->deprecated_p
7016 46 : && deprecated_state != DEPRECATED_SUPPRESS)
7017 38 : warn_deprecated_use (declspecs->type, declspecs->decl_attr);
7018 : }
7019 :
7020 315517557 : if ((decl_context == NORMAL || decl_context == FIELD)
7021 69034162 : && current_scope == file_scope
7022 376471714 : && c_type_variably_modified_p (type))
7023 : {
7024 3 : if (name)
7025 3 : error_at (loc, "variably modified %qE at file scope", name);
7026 : else
7027 0 : error_at (loc, "variably modified field at file scope");
7028 3 : type = integer_type_node;
7029 : }
7030 :
7031 315517557 : size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
7032 :
7033 : /* Diagnose defaulting to "int". */
7034 :
7035 315517557 : if (declspecs->default_int_p)
7036 : {
7037 : /* Issue a warning if this is an ISO C 99 program or if
7038 : -Wreturn-type and this is a function, or if -Wimplicit;
7039 : prefer the former warning since it is more explicit. */
7040 9764 : if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
7041 1229 : && funcdef_flag)
7042 702 : warn_about_return_type = 1;
7043 : else
7044 : {
7045 9062 : if (name)
7046 9052 : permerror_opt (loc, OPT_Wimplicit_int,
7047 : "type defaults to %<int%> in declaration "
7048 : "of %qE", name);
7049 : else
7050 10 : permerror_opt (loc, OPT_Wimplicit_int,
7051 : "type defaults to %<int%> in type name");
7052 : }
7053 : }
7054 :
7055 : /* Adjust the type if a bit-field is being declared,
7056 : -funsigned-bitfields applied and the type is not explicitly
7057 : "signed". */
7058 315517557 : if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
7059 40 : && TREE_CODE (type) == INTEGER_TYPE)
7060 38 : type = c_common_unsigned_type (type);
7061 :
7062 : /* Figure out the type qualifiers for the declaration. There are
7063 : two ways a declaration can become qualified. One is something
7064 : like `const int i' where the `const' is explicit. Another is
7065 : something like `typedef const int CI; CI i' where the type of the
7066 : declaration contains the `const'. A third possibility is that
7067 : there is a type qualifier on the element type of a typedefed
7068 : array type, in which case we should extract that qualifier so
7069 : that c_apply_type_quals_to_decl receives the full list of
7070 : qualifiers to work with (C90 is not entirely clear about whether
7071 : duplicate qualifiers should be diagnosed in this case, but it
7072 : seems most appropriate to do so). */
7073 315517557 : element_type = strip_array_types (type);
7074 315517557 : constp = declspecs->const_p + TYPE_READONLY (element_type);
7075 315517557 : restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
7076 315517557 : volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
7077 315517557 : atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
7078 315517557 : as1 = declspecs->address_space;
7079 315517557 : as2 = TYPE_ADDR_SPACE (element_type);
7080 315517557 : address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
7081 :
7082 315517557 : if (constp > 1)
7083 25 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
7084 315517557 : if (restrictp > 1)
7085 6 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
7086 315517557 : if (volatilep > 1)
7087 15 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
7088 315517557 : if (atomicp > 1)
7089 9 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
7090 :
7091 315517557 : if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
7092 0 : error_at (loc, "conflicting named address spaces (%s vs %s)",
7093 : c_addr_space_name (as1), c_addr_space_name (as2));
7094 :
7095 315517557 : if ((TREE_CODE (type) == ARRAY_TYPE
7096 315340495 : || first_non_attr_kind == cdk_array)
7097 316590863 : && TYPE_QUALS (element_type))
7098 : {
7099 73 : orig_qual_type = type;
7100 73 : type = c_build_qualified_type (type, TYPE_UNQUALIFIED);
7101 : }
7102 315517557 : type_quals = ((constp ? TYPE_QUAL_CONST : 0)
7103 315517557 : | (restrictp ? TYPE_QUAL_RESTRICT : 0)
7104 315517557 : | (volatilep ? TYPE_QUAL_VOLATILE : 0)
7105 315517557 : | (atomicp ? TYPE_QUAL_ATOMIC : 0)
7106 315517557 : | ENCODE_QUAL_ADDR_SPACE (address_space));
7107 315517557 : if (type_quals != TYPE_QUALS (element_type))
7108 12877946 : orig_qual_type = NULL_TREE;
7109 :
7110 : /* Applying the _Atomic qualifier to an array type (through the use
7111 : of typedefs or typeof) must be detected here. If the qualifier
7112 : is introduced later, any appearance of applying it to an array is
7113 : actually applying it to an element of that array. */
7114 315517557 : if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
7115 6 : error_at (loc, "%<_Atomic%>-qualified array type");
7116 :
7117 : /* Warn about storage classes that are invalid for certain
7118 : kinds of declarations (parameters, typenames, etc.). */
7119 :
7120 315517557 : if (funcdef_flag
7121 36350924 : && (threadp
7122 36350924 : || constexprp
7123 36350922 : || storage_class == csc_auto
7124 36350922 : || storage_class == csc_register
7125 36350877 : || storage_class == csc_typedef))
7126 : {
7127 47 : if (storage_class == csc_auto)
7128 42 : pedwarn (loc,
7129 81 : (current_scope == file_scope) ? 0 : OPT_Wpedantic,
7130 : "function definition declared %<auto%>");
7131 50 : if (storage_class == csc_register)
7132 3 : error_at (loc, "function definition declared %<register%>");
7133 50 : if (storage_class == csc_typedef)
7134 3 : error_at (loc, "function definition declared %<typedef%>");
7135 50 : if (threadp)
7136 2 : error_at (loc, "function definition declared %qs",
7137 2 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7138 50 : threadp = false;
7139 : /* The parser ensures a constexpr function definition never
7140 : reaches here. */
7141 50 : gcc_assert (!constexprp);
7142 50 : if (storage_class == csc_auto
7143 50 : || storage_class == csc_register
7144 : || storage_class == csc_typedef)
7145 55 : storage_class = csc_none;
7146 : }
7147 315517507 : else if (decl_context != NORMAL && (storage_class != csc_none
7148 250807796 : || threadp
7149 250807213 : || constexprp
7150 250807211 : || declspecs->c23_auto_p))
7151 : {
7152 587 : if (decl_context == PARM
7153 587 : && storage_class == csc_register
7154 568 : && !constexprp
7155 566 : && !declspecs->c23_auto_p)
7156 : ;
7157 : else
7158 : {
7159 21 : switch (decl_context)
7160 : {
7161 0 : case FIELD:
7162 0 : if (name)
7163 0 : error_at (loc, "storage class specified for structure "
7164 : "field %qE", name);
7165 : else
7166 0 : error_at (loc, "storage class specified for structure field");
7167 : break;
7168 21 : case PARM:
7169 21 : if (name)
7170 7 : error_at (loc, "storage class specified for parameter %qE",
7171 : name);
7172 : else
7173 14 : error_at (loc, "storage class specified for unnamed parameter");
7174 : break;
7175 0 : default:
7176 0 : error_at (loc, "storage class specified for typename");
7177 0 : break;
7178 : }
7179 315517557 : storage_class = csc_none;
7180 315517557 : threadp = false;
7181 315517557 : constexprp = false;
7182 : }
7183 : }
7184 315516920 : else if (storage_class == csc_extern
7185 315516920 : && initialized
7186 35494078 : && !funcdef_flag)
7187 : {
7188 : /* 'extern' with initialization is invalid if not at file scope. */
7189 28 : if (current_scope == file_scope)
7190 : {
7191 : /* It is fine to have 'extern const' when compiling at C
7192 : and C++ intersection. */
7193 19 : if (!(warn_cxx_compat && constp))
7194 18 : warning_at (loc, 0, "%qE initialized and declared %<extern%>",
7195 : name);
7196 : }
7197 : else
7198 9 : error_at (loc, "%qE has both %<extern%> and initializer", name);
7199 : }
7200 315516892 : else if (current_scope == file_scope)
7201 : {
7202 61829098 : if (storage_class == csc_auto)
7203 7 : error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
7204 : name);
7205 61829098 : if (pedantic && storage_class == csc_register)
7206 4 : pedwarn (input_location, OPT_Wpedantic,
7207 : "file-scope declaration of %qE specifies %<register%>", name);
7208 : }
7209 : else
7210 : {
7211 253687794 : if (storage_class == csc_extern && funcdef_flag)
7212 3 : error_at (loc, "nested function %qE declared %<extern%>", name);
7213 253687791 : else if (threadp && storage_class == csc_none)
7214 : {
7215 14 : error_at (loc, "function-scope %qE implicitly auto and declared "
7216 : "%qs", name,
7217 7 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7218 7 : threadp = false;
7219 : }
7220 : }
7221 :
7222 : /* Now figure out the structure of the declarator proper.
7223 : Descend through it, creating more complex types, until we reach
7224 : the declared identifier (or NULL_TREE, in an absolute declarator).
7225 : At each stage we maintain an unqualified version of the type
7226 : together with any qualifiers that should be applied to it with
7227 : c_build_qualified_type; this way, array types including
7228 : multidimensional array types are first built up in unqualified
7229 : form and then the qualified form is created with
7230 : TYPE_MAIN_VARIANT pointing to the unqualified form. */
7231 :
7232 386083415 : while (declarator && declarator->kind != cdk_id)
7233 : {
7234 70565858 : if (type == error_mark_node)
7235 : {
7236 39 : declarator = declarator->declarator;
7237 39 : continue;
7238 : }
7239 :
7240 : /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
7241 : a cdk_pointer (for *...),
7242 : a cdk_function (for ...(...)),
7243 : a cdk_attrs (for nested attributes),
7244 : or a cdk_id (for the name being declared
7245 : or the place in an absolute declarator
7246 : where the name was omitted).
7247 : For the last case, we have just exited the loop.
7248 :
7249 : At this point, TYPE is the type of elements of an array,
7250 : or for a function to return, or for a pointer to point to.
7251 : After this sequence of ifs, TYPE is the type of the
7252 : array or function or pointer, and DECLARATOR has had its
7253 : outermost layer removed. */
7254 :
7255 70565819 : if (array_ptr_quals != TYPE_UNQUALIFIED
7256 70565819 : || array_ptr_attrs != NULL_TREE
7257 70565819 : || array_parm_static)
7258 : {
7259 : /* Only the innermost declarator (making a parameter be of
7260 : array type which is converted to pointer type)
7261 : may have static or type qualifiers. */
7262 1 : error_at (loc, "static or type qualifiers in non-parameter array declarator");
7263 1 : array_ptr_quals = TYPE_UNQUALIFIED;
7264 1 : array_ptr_attrs = NULL_TREE;
7265 1 : array_parm_static = false;
7266 : }
7267 :
7268 70565819 : switch (declarator->kind)
7269 : {
7270 6832 : case cdk_attrs:
7271 6832 : {
7272 : /* A declarator with embedded attributes. */
7273 6832 : tree attrs = declarator->u.attrs;
7274 6832 : const struct c_declarator *inner_decl;
7275 6832 : int attr_flags = 0;
7276 6832 : declarator = declarator->declarator;
7277 : /* Standard attribute syntax precisely defines what entity
7278 : an attribute in each position appertains to, so only
7279 : apply laxity about positioning to GNU attribute syntax.
7280 : Standard attributes applied to a function or array
7281 : declarator apply exactly to that type; standard
7282 : attributes applied to the identifier apply to the
7283 : declaration rather than to the type, and are specified
7284 : using a cdk_id declarator rather than using
7285 : cdk_attrs. */
7286 6832 : inner_decl = declarator;
7287 6832 : while (inner_decl->kind == cdk_attrs)
7288 0 : inner_decl = inner_decl->declarator;
7289 6832 : if (!cxx11_attribute_p (attrs))
7290 : {
7291 6694 : if (inner_decl->kind == cdk_id)
7292 : attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
7293 : else if (inner_decl->kind == cdk_function)
7294 : attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
7295 : else if (inner_decl->kind == cdk_array)
7296 6832 : attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
7297 : }
7298 6832 : attrs = c_warn_type_attributes (type, attrs);
7299 6832 : returned_attrs = decl_attributes (&type,
7300 : chainon (returned_attrs, attrs),
7301 : attr_flags);
7302 6832 : break;
7303 : }
7304 1141960 : case cdk_array:
7305 1141960 : {
7306 1141960 : tree itype = NULL_TREE;
7307 1141960 : tree size = declarator->u.array.dimen;
7308 : /* The index is a signed object `sizetype' bits wide. */
7309 1141960 : tree index_type = c_common_signed_type (sizetype);
7310 :
7311 1141960 : array_ptr_quals = declarator->u.array.quals;
7312 1141960 : array_ptr_attrs = declarator->u.array.attrs;
7313 1141960 : array_parm_static = declarator->u.array.static_p;
7314 1141960 : array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
7315 :
7316 1141960 : declarator = declarator->declarator;
7317 :
7318 1141960 : bool flexible_array_member = false;
7319 :
7320 : /* Check for some types that there cannot be arrays of. */
7321 :
7322 1141960 : if (VOID_TYPE_P (type))
7323 : {
7324 11 : if (name)
7325 9 : error_at (loc, "declaration of %qE as array of voids", name);
7326 : else
7327 2 : error_at (loc, "declaration of type name as array of voids");
7328 11 : type = error_mark_node;
7329 : }
7330 :
7331 1141960 : if (TREE_CODE (type) == FUNCTION_TYPE)
7332 : {
7333 3 : if (name)
7334 2 : error_at (loc, "declaration of %qE as array of functions",
7335 : name);
7336 : else
7337 1 : error_at (loc, "declaration of type name as array of "
7338 : "functions");
7339 3 : type = error_mark_node;
7340 : }
7341 :
7342 21330 : if (pedantic && !in_system_header_at (input_location)
7343 1157001 : && flexible_array_type_p (type))
7344 20 : pedwarn (loc, OPT_Wpedantic,
7345 : "invalid use of structure with flexible array member");
7346 :
7347 1141960 : if (size == error_mark_node)
7348 119 : type = error_mark_node;
7349 :
7350 1141960 : if (type == error_mark_node)
7351 133 : continue;
7352 :
7353 1141827 : if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
7354 : {
7355 0 : type = error_mark_node;
7356 0 : continue;
7357 : }
7358 :
7359 : /* If size was specified, set ITYPE to a range-type for
7360 : that size. Otherwise, ITYPE remains null. finish_decl
7361 : may figure it out from an initial value. */
7362 :
7363 1141827 : if (size)
7364 : {
7365 970031 : bool size_maybe_const = true;
7366 970031 : bool size_int_const = (TREE_CODE (size) == INTEGER_CST
7367 970031 : && !TREE_OVERFLOW (size));
7368 970031 : bool this_size_varies = false;
7369 :
7370 : /* Strip NON_LVALUE_EXPRs since we aren't using as an
7371 : lvalue. */
7372 970040 : STRIP_TYPE_NOPS (size);
7373 :
7374 970031 : if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
7375 : {
7376 16 : if (name)
7377 14 : error_at (loc, "size of array %qE has non-integer type",
7378 : name);
7379 : else
7380 2 : error_at (loc,
7381 : "size of unnamed array has non-integer type");
7382 16 : size = integer_one_node;
7383 16 : size_int_const = true;
7384 16 : size_error = true;
7385 : }
7386 : /* This can happen with enum forward declaration. */
7387 970015 : else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
7388 : {
7389 0 : if (name)
7390 0 : error_at (loc, "size of array %qE has incomplete type",
7391 : name);
7392 : else
7393 0 : error_at (loc, "size of unnamed array has incomplete "
7394 : "type");
7395 0 : size = integer_one_node;
7396 0 : size_int_const = true;
7397 0 : size_error = true;
7398 : }
7399 :
7400 970031 : size = c_fully_fold (size, false, &size_maybe_const);
7401 :
7402 970031 : if (pedantic && size_maybe_const && integer_zerop (size))
7403 : {
7404 3 : if (name)
7405 3 : pedwarn (loc, OPT_Wpedantic,
7406 : "ISO C forbids zero-size array %qE", name);
7407 : else
7408 0 : pedwarn (loc, OPT_Wpedantic,
7409 : "ISO C forbids zero-size array");
7410 : }
7411 :
7412 970031 : if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
7413 : {
7414 947672 : constant_expression_warning (size);
7415 947672 : if (tree_int_cst_sgn (size) < 0)
7416 : {
7417 463 : if (name)
7418 460 : error_at (loc, "size of array %qE is negative", name);
7419 : else
7420 3 : error_at (loc, "size of unnamed array is negative");
7421 463 : size = integer_one_node;
7422 463 : size_int_const = true;
7423 463 : size_error = true;
7424 : }
7425 : /* Handle a size folded to an integer constant but
7426 : not an integer constant expression. */
7427 947672 : if (!size_int_const)
7428 : {
7429 : /* If this is a file scope declaration of an
7430 : ordinary identifier, this is invalid code;
7431 : diagnosing it here and not subsequently
7432 : treating the type as variable-length avoids
7433 : more confusing diagnostics later. */
7434 155 : if ((decl_context == NORMAL || decl_context == FIELD)
7435 142 : && current_scope == file_scope)
7436 14 : pedwarn (input_location, 0,
7437 : "variably modified %qE at file scope",
7438 : name);
7439 : else
7440 : this_size_varies = size_varies = true;
7441 155 : warn_variable_length_array (name, size);
7442 : }
7443 : }
7444 22359 : else if ((decl_context == NORMAL || decl_context == FIELD)
7445 13257 : && current_scope == file_scope)
7446 : {
7447 21 : error_at (loc, "variably modified %qE at file scope", name);
7448 21 : size = integer_one_node;
7449 : }
7450 : else
7451 : {
7452 : /* Make sure the array size remains visibly
7453 : nonconstant even if it is (eg) a const variable
7454 : with known value. */
7455 22338 : this_size_varies = size_varies = true;
7456 22338 : warn_variable_length_array (name, size);
7457 22338 : if (sanitize_flags_p (SANITIZE_VLA)
7458 181 : && current_function_decl != NULL_TREE
7459 22501 : && decl_context == NORMAL)
7460 : {
7461 : /* Evaluate the array size only once. */
7462 155 : size = save_expr (size);
7463 155 : size = c_fully_fold (size, false, NULL);
7464 155 : size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
7465 : ubsan_instrument_vla (loc, size),
7466 : size);
7467 : }
7468 : }
7469 :
7470 970031 : if (integer_zerop (size) && !this_size_varies)
7471 : {
7472 : /* A zero-length array cannot be represented with
7473 : an unsigned index type, which is what we'll
7474 : get with build_index_type. Create an
7475 : open-ended range instead. */
7476 2615 : itype = build_index_type (NULL_TREE);
7477 : }
7478 : else
7479 : {
7480 : /* Arrange for the SAVE_EXPR on the inside of the
7481 : MINUS_EXPR, which allows the -1 to get folded
7482 : with the +1 that happens when building TYPE_SIZE. */
7483 967416 : if (size_varies)
7484 22854 : size = save_expr (size);
7485 967416 : if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
7486 141 : size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7487 : integer_zero_node, size);
7488 :
7489 : /* Compute the maximum valid index, that is, size
7490 : - 1. Do the calculation in index_type, so that
7491 : if it is a variable the computations will be
7492 : done in the proper mode. */
7493 967416 : itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
7494 : convert (index_type, size),
7495 : convert (index_type,
7496 : size_one_node));
7497 :
7498 : /* The above overflows when size does not fit
7499 : in index_type.
7500 : ??? While a size of INT_MAX+1 technically shouldn't
7501 : cause an overflow (because we subtract 1), handling
7502 : this case seems like an unnecessary complication. */
7503 967416 : if (TREE_CODE (size) == INTEGER_CST
7504 944937 : && !int_fits_type_p (size, index_type))
7505 : {
7506 6 : if (name)
7507 5 : error_at (loc, "size of array %qE is too large",
7508 : name);
7509 : else
7510 1 : error_at (loc, "size of unnamed array is too large");
7511 6 : type = error_mark_node;
7512 6 : continue;
7513 : }
7514 :
7515 967410 : itype = build_index_type (itype);
7516 : }
7517 970025 : if (this_size_varies)
7518 : {
7519 22479 : if (TREE_SIDE_EFFECTS (size))
7520 : {
7521 22116 : if (*expr)
7522 8178 : *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7523 : *expr, size);
7524 : else
7525 13938 : *expr = size;
7526 : }
7527 22479 : *expr_const_operands &= size_maybe_const;
7528 : }
7529 : }
7530 171796 : else if (decl_context == FIELD)
7531 : {
7532 86056 : if (array_parm_vla_unspec_p)
7533 : /* Field names can in fact have function prototype
7534 : scope so [*] is disallowed here through making
7535 : the field variably modified, not through being
7536 : something other than a declaration with function
7537 : prototype scope. */
7538 : size_varies = true;
7539 : else
7540 : {
7541 : const struct c_declarator *t = declarator;
7542 86053 : while (t->kind == cdk_attrs)
7543 0 : t = t->declarator;
7544 86053 : flexible_array_member = (t->kind == cdk_id);
7545 : }
7546 86053 : if (flexible_array_member
7547 86053 : && !in_system_header_at (input_location))
7548 85023 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7549 : "support flexible array members");
7550 :
7551 : /* ISO C99 Flexible array members are effectively
7552 : identical to GCC's zero-length array extension. */
7553 86056 : if (flexible_array_member)
7554 86033 : itype = build_index_type (NULL_TREE);
7555 : }
7556 :
7557 : /* Complain about arrays of incomplete types. */
7558 1141821 : if (!COMPLETE_TYPE_P (type))
7559 : {
7560 58 : auto_diagnostic_group d;
7561 58 : error_at (loc, "array type has incomplete element type %qT",
7562 : type);
7563 : /* See if we can be more helpful. */
7564 58 : if (TREE_CODE (type) == ARRAY_TYPE)
7565 : {
7566 29 : if (name)
7567 24 : inform (loc, "declaration of %qE as multidimensional "
7568 : "array must have bounds for all dimensions "
7569 : "except the first", name);
7570 : else
7571 5 : inform (loc, "declaration of multidimensional array "
7572 : "must have bounds for all dimensions except "
7573 : "the first");
7574 : }
7575 58 : type = error_mark_node;
7576 58 : }
7577 : else
7578 : /* When itype is NULL, a shared incomplete array type is
7579 : returned for all array of a given type. Elsewhere we
7580 : make sure we don't complete that type before copying
7581 : it, but here we want to make sure we don't ever
7582 : modify the shared type, so we gcc_assert (itype)
7583 : below. */
7584 : {
7585 1141763 : addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
7586 1141763 : if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
7587 0 : type = c_build_qualified_type (type,
7588 : ENCODE_QUAL_ADDR_SPACE (as));
7589 1141763 : if (array_parm_vla_unspec_p)
7590 151 : type = c_build_array_type_unspecified (type);
7591 : /* The GCC extension for zero-length arrays differs from
7592 : ISO flexible array members in that sizeof yields
7593 : zero. */
7594 1141612 : else if (size && integer_zerop (size))
7595 2608 : type = c_build_array_type_zero_size (type);
7596 : else
7597 1139004 : type = c_build_array_type (type, itype);
7598 :
7599 1141763 : if (!valid_array_size_p (loc, type, name))
7600 33 : type = error_mark_node;
7601 : }
7602 :
7603 1141821 : if (array_parm_vla_unspec_p)
7604 : {
7605 : /* C99 6.7.5.2p4 */
7606 151 : if (decl_context == TYPENAME)
7607 6 : warning (0, "%<[*]%> not in a declaration");
7608 145 : else if (decl_context != GENERIC_ASSOC
7609 145 : && decl_context != PARM
7610 7 : && decl_context != FIELD)
7611 : {
7612 4 : error ("%<[*]%> not allowed in other than function prototype scope "
7613 : "or generic association");
7614 4 : type = error_mark_node;
7615 : }
7616 : size_varies = true;
7617 : }
7618 :
7619 :
7620 1141821 : gcc_assert (type == error_mark_node
7621 : || flexible_array_member
7622 : == flexible_array_member_type_p (type));
7623 :
7624 1141821 : if (decl_context != PARM
7625 821292 : && (array_ptr_quals != TYPE_UNQUALIFIED
7626 821292 : || array_ptr_attrs != NULL_TREE
7627 821291 : || array_parm_static))
7628 : {
7629 2 : error_at (loc, "static or type qualifiers in non-parameter "
7630 : "array declarator");
7631 2 : array_ptr_quals = TYPE_UNQUALIFIED;
7632 2 : array_ptr_attrs = NULL_TREE;
7633 2 : array_parm_static = false;
7634 : }
7635 1141821 : orig_qual_indirect++;
7636 1141821 : break;
7637 : }
7638 51033391 : case cdk_function:
7639 51033391 : {
7640 : /* Say it's a definition only for the declarator closest
7641 : to the identifier, apart possibly from some
7642 : attributes. */
7643 51033391 : bool really_funcdef = false;
7644 51033391 : tree arg_types;
7645 51033391 : orig_qual_type = NULL_TREE;
7646 51033391 : if (funcdef_flag)
7647 : {
7648 36350947 : const struct c_declarator *t = declarator->declarator;
7649 36350997 : while (t->kind == cdk_attrs)
7650 50 : t = t->declarator;
7651 36350947 : really_funcdef = (t->kind == cdk_id);
7652 : }
7653 :
7654 : /* Declaring a function type. Make sure we have a valid
7655 : type for the function to return. */
7656 51033391 : if (type == error_mark_node)
7657 0 : continue;
7658 :
7659 51033391 : size_varies = false;
7660 :
7661 : /* Warn about some types functions can't return. */
7662 51033391 : if (TREE_CODE (type) == FUNCTION_TYPE)
7663 : {
7664 0 : if (name)
7665 0 : error_at (loc, "%qE declared as function returning a "
7666 : "function", name);
7667 : else
7668 0 : error_at (loc, "type name declared as function "
7669 : "returning a function");
7670 0 : type = integer_type_node;
7671 : }
7672 51033391 : if (TREE_CODE (type) == ARRAY_TYPE)
7673 : {
7674 0 : if (name)
7675 0 : error_at (loc, "%qE declared as function returning an array",
7676 : name);
7677 : else
7678 0 : error_at (loc, "type name declared as function returning "
7679 : "an array");
7680 0 : type = integer_type_node;
7681 : }
7682 :
7683 : /* Construct the function type and go to the next
7684 : inner layer of declarator. */
7685 51033391 : arg_info = declarator->u.arg_info;
7686 51033391 : arg_types = grokparms (arg_info, really_funcdef);
7687 :
7688 : /* Type qualifiers before the return type of the function
7689 : qualify the return type, not the function type. */
7690 51033391 : if (type_quals)
7691 : {
7692 235 : const enum c_declspec_word ignored_quals_list[] =
7693 : {
7694 : cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
7695 : cdw_atomic, cdw_number_of_elements
7696 : };
7697 235 : location_t specs_loc
7698 235 : = smallest_type_quals_location (declspecs->locations,
7699 : ignored_quals_list);
7700 235 : if (specs_loc == UNKNOWN_LOCATION)
7701 139 : specs_loc = declspecs->locations[cdw_typedef];
7702 139 : if (specs_loc == UNKNOWN_LOCATION)
7703 15 : specs_loc = loc;
7704 :
7705 : /* Type qualifiers on a function return type are
7706 : normally permitted by the standard but have no
7707 : effect, so give a warning at -Wreturn-type.
7708 : Qualifiers on a void return type are banned on
7709 : function definitions in ISO C; GCC used to used
7710 : them for noreturn functions. The resolution of C11
7711 : DR#423 means qualifiers (other than _Atomic) are
7712 : actually removed from the return type when
7713 : determining the function type. For C23, _Atomic is
7714 : removed as well. */
7715 235 : int quals_used = type_quals;
7716 235 : if (flag_isoc23)
7717 : quals_used = 0;
7718 65 : else if (flag_isoc11)
7719 31 : quals_used &= TYPE_QUAL_ATOMIC;
7720 65 : if (quals_used && VOID_TYPE_P (type) && really_funcdef)
7721 5 : pedwarn (specs_loc, 0,
7722 : "function definition has qualified void "
7723 : "return type");
7724 : else
7725 230 : warning_at (specs_loc, OPT_Wignored_qualifiers,
7726 : "type qualifiers ignored on function "
7727 : "return type");
7728 :
7729 : /* Ensure an error for restrict on invalid types; the
7730 : DR#423 resolution is not entirely clear about
7731 : this. */
7732 235 : if (flag_isoc11
7733 201 : && (type_quals & TYPE_QUAL_RESTRICT)
7734 241 : && (!POINTER_TYPE_P (type)
7735 4 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
7736 4 : error_at (loc, "invalid use of %<restrict%>");
7737 235 : type = c_build_qualified_type (type, quals_used);
7738 : }
7739 51033391 : type_quals = TYPE_UNQUALIFIED;
7740 :
7741 102066782 : type = c_build_function_type (type, arg_types,
7742 51033391 : arg_info->no_named_args_stdarg_p);
7743 51033391 : declarator = declarator->declarator;
7744 :
7745 : /* Set the TYPE_CONTEXTs for each tagged type which is local to
7746 : the formal parameter list of this FUNCTION_TYPE to point to
7747 : the FUNCTION_TYPE node itself. */
7748 51033391 : {
7749 51033391 : c_arg_tag *tag;
7750 51033391 : unsigned ix;
7751 :
7752 437117085 : FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7753 142 : TYPE_CONTEXT (tag->type) = type;
7754 : }
7755 : break;
7756 : }
7757 18383636 : case cdk_pointer:
7758 18383636 : {
7759 : /* Merge any constancy or volatility into the target type
7760 : for the pointer. */
7761 18383636 : if ((type_quals & TYPE_QUAL_ATOMIC)
7762 1988 : && TREE_CODE (type) == FUNCTION_TYPE)
7763 : {
7764 2 : error_at (loc,
7765 : "%<_Atomic%>-qualified function type");
7766 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
7767 : }
7768 18383634 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7769 1708 : && type_quals)
7770 0 : pedwarn (loc, OPT_Wpedantic,
7771 : "ISO C forbids qualified function types");
7772 18381928 : if (type_quals)
7773 6060957 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7774 : orig_qual_indirect);
7775 18383636 : orig_qual_type = NULL_TREE;
7776 18383636 : size_varies = false;
7777 :
7778 : /* When the pointed-to type involves components of variable size,
7779 : care must be taken to ensure that the size evaluation code is
7780 : emitted early enough to dominate all the possible later uses
7781 : and late enough for the variables on which it depends to have
7782 : been assigned.
7783 :
7784 : This is expected to happen automatically when the pointed-to
7785 : type has a name/declaration of it's own, but special attention
7786 : is required if the type is anonymous. */
7787 18383636 : if (!TYPE_NAME (type) && c_type_variably_modified_p (type))
7788 : {
7789 5949 : bool bind_p = decl_context == TYPENAME
7790 : || decl_context == FIELD
7791 5949 : || decl_context == PARM;
7792 11391 : add_decl_expr (loc, type, bind_p ? expr : NULL, true);
7793 : }
7794 :
7795 18383636 : type = c_build_pointer_type (type);
7796 :
7797 : /* Process type qualifiers (such as const or volatile)
7798 : that were given inside the `*'. */
7799 18383636 : type_quals = declarator->u.pointer_quals;
7800 :
7801 18383636 : declarator = declarator->declarator;
7802 18383636 : break;
7803 : }
7804 0 : default:
7805 0 : gcc_unreachable ();
7806 : }
7807 : }
7808 315517557 : *decl_attrs = chainon (returned_attrs, *decl_attrs);
7809 315517557 : *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
7810 :
7811 : /* Now TYPE has the actual type, apart from any qualifiers in
7812 : TYPE_QUALS. */
7813 :
7814 : /* Warn about address space used for things other than static memory or
7815 : pointers. */
7816 315517557 : address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
7817 315517557 : if (!ADDR_SPACE_GENERIC_P (address_space))
7818 : {
7819 10 : if (decl_context == NORMAL)
7820 : {
7821 10 : switch (storage_class)
7822 : {
7823 0 : case csc_auto:
7824 0 : error ("%qs combined with %<auto%> qualifier for %qE",
7825 : c_addr_space_name (address_space), name);
7826 0 : break;
7827 0 : case csc_register:
7828 0 : error ("%qs combined with %<register%> qualifier for %qE",
7829 : c_addr_space_name (address_space), name);
7830 0 : break;
7831 6 : case csc_none:
7832 6 : if (current_function_scope)
7833 : {
7834 0 : error ("%qs specified for auto variable %qE",
7835 : c_addr_space_name (address_space), name);
7836 0 : break;
7837 : }
7838 : break;
7839 : case csc_static:
7840 : case csc_extern:
7841 : case csc_typedef:
7842 : break;
7843 0 : default:
7844 0 : gcc_unreachable ();
7845 : }
7846 : }
7847 0 : else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
7848 : {
7849 0 : if (name)
7850 0 : error ("%qs specified for parameter %qE",
7851 : c_addr_space_name (address_space), name);
7852 : else
7853 0 : error ("%qs specified for unnamed parameter",
7854 : c_addr_space_name (address_space));
7855 : }
7856 0 : else if (decl_context == FIELD)
7857 : {
7858 0 : if (name)
7859 0 : error ("%qs specified for structure field %qE",
7860 : c_addr_space_name (address_space), name);
7861 : else
7862 0 : error ("%qs specified for structure field",
7863 : c_addr_space_name (address_space));
7864 : }
7865 : }
7866 :
7867 : /* Check the type and width of a bit-field. */
7868 315517557 : if (bitfield)
7869 : {
7870 52817 : check_bitfield_type_and_width (loc, &type, width, name);
7871 : /* C11 makes it implementation-defined (6.7.2.1#5) whether
7872 : atomic types are permitted for bit-fields; we have no code to
7873 : make bit-field accesses atomic, so disallow them. */
7874 52817 : if (type_quals & TYPE_QUAL_ATOMIC)
7875 : {
7876 2 : if (name)
7877 1 : error_at (loc, "bit-field %qE has atomic type", name);
7878 : else
7879 1 : error_at (loc, "bit-field has atomic type");
7880 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
7881 : }
7882 : }
7883 :
7884 : /* Reject invalid uses of _Alignas. */
7885 315517557 : if (declspecs->alignas_p)
7886 : {
7887 190 : if (storage_class == csc_typedef)
7888 1 : error_at (loc, "alignment specified for typedef %qE", name);
7889 189 : else if (storage_class == csc_register)
7890 1 : error_at (loc, "alignment specified for %<register%> object %qE",
7891 : name);
7892 188 : else if (decl_context == PARM)
7893 : {
7894 2 : if (name)
7895 1 : error_at (loc, "alignment specified for parameter %qE", name);
7896 : else
7897 1 : error_at (loc, "alignment specified for unnamed parameter");
7898 : }
7899 186 : else if (bitfield)
7900 : {
7901 0 : if (name)
7902 0 : error_at (loc, "alignment specified for bit-field %qE", name);
7903 : else
7904 0 : error_at (loc, "alignment specified for unnamed bit-field");
7905 : }
7906 186 : else if (TREE_CODE (type) == FUNCTION_TYPE)
7907 1 : error_at (loc, "alignment specified for function %qE", name);
7908 185 : else if (declspecs->align_log != -1 && TYPE_P (type))
7909 : {
7910 158 : alignas_align = 1U << declspecs->align_log;
7911 158 : if (alignas_align < min_align_of_type (type))
7912 : {
7913 26 : if (name)
7914 25 : error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7915 : "alignment of %qE", name);
7916 : else
7917 1 : error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7918 : "alignment of unnamed field");
7919 : alignas_align = 0;
7920 : }
7921 : }
7922 : }
7923 :
7924 : /* If this is declaring a typedef name, return a TYPE_DECL. */
7925 :
7926 315517557 : if (storage_class == csc_typedef)
7927 : {
7928 4332426 : tree decl;
7929 4332426 : if ((type_quals & TYPE_QUAL_ATOMIC)
7930 12630 : && TREE_CODE (type) == FUNCTION_TYPE)
7931 : {
7932 0 : error_at (loc,
7933 : "%<_Atomic%>-qualified function type");
7934 0 : type_quals &= ~TYPE_QUAL_ATOMIC;
7935 : }
7936 4332426 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7937 537 : && type_quals)
7938 0 : pedwarn (loc, OPT_Wpedantic,
7939 : "ISO C forbids qualified function types");
7940 4331889 : if (type_quals)
7941 31724 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7942 : orig_qual_indirect);
7943 8664852 : decl = build_decl (declarator->id_loc,
7944 4332426 : TYPE_DECL, declarator->u.id.id, type);
7945 4332426 : if (declspecs->explicit_signed_p)
7946 352737 : C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7947 4332426 : if (declspecs->inline_p)
7948 4 : pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7949 4332426 : if (declspecs->noreturn_p)
7950 1 : pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7951 :
7952 4332426 : if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7953 : {
7954 1828 : struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7955 :
7956 1828 : if (b != NULL
7957 40 : && b->decl != NULL_TREE
7958 40 : && (B_IN_CURRENT_SCOPE (b)
7959 4 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7960 1864 : && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7961 : {
7962 7 : auto_diagnostic_group d;
7963 8 : if (warning_at (declarator->id_loc, OPT_Wc___compat,
7964 : "using %qD as both a typedef and a tag is "
7965 : "invalid in C++", decl)
7966 7 : && b->locus != UNKNOWN_LOCATION)
7967 6 : inform (b->locus, "originally defined here");
7968 7 : }
7969 : }
7970 :
7971 4332426 : return decl;
7972 : }
7973 :
7974 : /* If this is a type name (such as, in a cast or sizeof),
7975 : compute the type and return it now. */
7976 :
7977 311185131 : if (decl_context == TYPENAME || decl_context == GENERIC_ASSOC)
7978 : {
7979 : /* Note that the grammar rejects storage classes in typenames
7980 : and fields. */
7981 121660091 : gcc_assert (storage_class == csc_none && !threadp
7982 : && !declspecs->inline_p && !declspecs->noreturn_p);
7983 121660091 : if ((type_quals & TYPE_QUAL_ATOMIC)
7984 257 : && TREE_CODE (type) == FUNCTION_TYPE)
7985 : {
7986 0 : error_at (loc,
7987 : "%<_Atomic%>-qualified function type");
7988 0 : type_quals &= ~TYPE_QUAL_ATOMIC;
7989 : }
7990 121660091 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7991 22 : && type_quals)
7992 0 : pedwarn (loc, OPT_Wpedantic,
7993 : "ISO C forbids const or volatile function types");
7994 121660069 : if (type_quals)
7995 856 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7996 : orig_qual_indirect);
7997 121660091 : return type;
7998 : }
7999 :
8000 364125 : if (pedantic && decl_context == FIELD
8001 189547638 : && c_type_variably_modified_p (type))
8002 : {
8003 : /* C99 6.7.2.1p8 */
8004 7 : pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
8005 : "have a variably modified type");
8006 : }
8007 :
8008 : /* Aside from typedefs and type names (handle above),
8009 : `void' at top level (not within pointer)
8010 : is allowed only in public variables.
8011 : We don't complain about parms either, but that is because
8012 : a better error message can be made later. */
8013 :
8014 189525040 : if (VOID_TYPE_P (type) && decl_context != PARM
8015 64 : && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
8016 : && (storage_class == csc_extern
8017 27 : || (current_scope == file_scope
8018 18 : && !(storage_class == csc_static
8019 : || storage_class == csc_register)))))
8020 : {
8021 15 : error_at (loc, "variable or field %qE declared void", name);
8022 15 : type = integer_type_node;
8023 : }
8024 :
8025 : /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
8026 : or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
8027 :
8028 188545396 : {
8029 188545396 : tree decl;
8030 :
8031 188545396 : if (decl_context == PARM)
8032 : {
8033 124823304 : tree promoted_type;
8034 124823304 : bool array_parameter_p = false;
8035 :
8036 : /* A parameter declared as an array of T is really a pointer to T.
8037 : One declared as a function is really a pointer to a function. */
8038 :
8039 124823304 : if (TREE_CODE (type) == ARRAY_TYPE)
8040 : {
8041 439726 : if (!size_error)
8042 439712 : *decl_attrs = build_arg_spec_attribute (type, array_parm_static,
8043 : *decl_attrs);
8044 :
8045 : /* Transfer const-ness of array into that of type pointed to. */
8046 439726 : type = TREE_TYPE (type);
8047 439726 : if (orig_qual_type != NULL_TREE)
8048 : {
8049 7 : if (orig_qual_indirect == 0)
8050 5 : orig_qual_type = TREE_TYPE (orig_qual_type);
8051 : else
8052 2 : orig_qual_indirect--;
8053 : }
8054 439726 : if (type_quals)
8055 48342 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
8056 : orig_qual_indirect);
8057 :
8058 : /* The pointed-to type may need a decl expr (see above). */
8059 439726 : if (!TYPE_NAME (type) && c_type_variably_modified_p (type))
8060 : {
8061 375 : bool bind_p = decl_context == TYPENAME
8062 : || decl_context == FIELD
8063 : || decl_context == PARM;
8064 375 : add_decl_expr (loc, type, bind_p ? expr : NULL, true);
8065 : }
8066 :
8067 439726 : type = c_build_pointer_type (type);
8068 439726 : type_quals = array_ptr_quals;
8069 439726 : if (type_quals)
8070 965 : type = c_build_qualified_type (type, type_quals);
8071 :
8072 : /* We don't yet implement attributes in this context. */
8073 439726 : if (array_ptr_attrs != NULL_TREE)
8074 0 : warning_at (loc, OPT_Wattributes,
8075 : "attributes in parameter array declarator ignored");
8076 :
8077 : size_varies = false;
8078 : array_parameter_p = true;
8079 : }
8080 124383578 : else if (TREE_CODE (type) == FUNCTION_TYPE)
8081 : {
8082 301 : if (type_quals & TYPE_QUAL_ATOMIC)
8083 : {
8084 1 : error_at (loc,
8085 : "%<_Atomic%>-qualified function type");
8086 1 : type_quals &= ~TYPE_QUAL_ATOMIC;
8087 : }
8088 300 : else if (type_quals)
8089 0 : pedwarn (loc, OPT_Wpedantic,
8090 : "ISO C forbids qualified function types");
8091 1 : if (type_quals)
8092 0 : type = c_build_qualified_type (type, type_quals);
8093 301 : type = c_build_pointer_type (type);
8094 301 : type_quals = TYPE_UNQUALIFIED;
8095 : }
8096 124383277 : else if (type_quals)
8097 9971111 : type = c_build_qualified_type (type, type_quals);
8098 :
8099 249646608 : decl = build_decl (declarator->id_loc,
8100 124823304 : PARM_DECL, declarator->u.id.id, type);
8101 124823304 : if (size_varies)
8102 42 : C_DECL_VARIABLE_SIZE (decl) = 1;
8103 124823304 : C_ARRAY_PARAMETER (decl) = array_parameter_p;
8104 :
8105 : /* Compute the type actually passed in the parmlist,
8106 : for the case where there is no prototype.
8107 : (For example, shorts and chars are passed as ints.)
8108 : When there is a prototype, this is overridden later. */
8109 :
8110 124823304 : if (type == error_mark_node)
8111 : promoted_type = type;
8112 : else
8113 124823220 : promoted_type = c_type_promotes_to (type);
8114 :
8115 124823304 : DECL_ARG_TYPE (decl) = promoted_type;
8116 124823304 : if (declspecs->inline_p)
8117 4 : pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
8118 124823304 : if (declspecs->noreturn_p)
8119 1 : pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
8120 : }
8121 64701736 : else if (decl_context == FIELD)
8122 : {
8123 : /* Note that the grammar rejects storage classes in typenames
8124 : and fields. */
8125 4324401 : gcc_assert (storage_class == csc_none && !threadp
8126 : && !declspecs->inline_p && !declspecs->noreturn_p);
8127 :
8128 : /* Structure field. It may not be a function. */
8129 :
8130 4324401 : if (TREE_CODE (type) == FUNCTION_TYPE)
8131 : {
8132 0 : error_at (loc, "field %qE declared as a function", name);
8133 0 : type = c_build_pointer_type (type);
8134 : }
8135 4324401 : else if (TREE_CODE (type) != ERROR_MARK
8136 4324401 : && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
8137 : {
8138 24 : if (name)
8139 17 : error_at (loc, "field %qE has incomplete type", name);
8140 : else
8141 7 : error_at (loc, "unnamed field has incomplete type");
8142 24 : type = error_mark_node;
8143 : }
8144 4324377 : else if (TREE_CODE (type) == ARRAY_TYPE
8145 4324377 : && TYPE_DOMAIN (type) == NULL_TREE)
8146 : {
8147 : /* We have a flexible array member through a typedef.
8148 : Set suitable range. Whether this is a correct position
8149 : for a flexible array member will be determined elsewhere. */
8150 14 : if (!in_system_header_at (input_location))
8151 14 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
8152 : "support flexible array members");
8153 14 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8154 14 : TYPE_DOMAIN (type) = build_index_type (NULL_TREE);
8155 14 : if (orig_qual_indirect == 0)
8156 4324401 : orig_qual_type = NULL_TREE;
8157 : }
8158 4324401 : if (type != error_mark_node
8159 4324401 : && !verify_type_context (loc, TCTX_FIELD, type))
8160 0 : type = error_mark_node;
8161 :
8162 4324401 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
8163 : orig_qual_indirect);
8164 8648802 : decl = build_decl (declarator->id_loc,
8165 4324401 : FIELD_DECL, declarator->u.id.id, type);
8166 4324401 : DECL_NONADDRESSABLE_P (decl) = bitfield;
8167 4324401 : if (bitfield && !declarator->u.id.id)
8168 9807 : DECL_PADDING_P (decl) = 1;
8169 :
8170 4324401 : if (size_varies)
8171 669 : C_DECL_VARIABLE_SIZE (decl) = 1;
8172 : }
8173 60377335 : else if (TREE_CODE (type) == FUNCTION_TYPE)
8174 : {
8175 51447237 : if (storage_class == csc_register || threadp || constexprp)
8176 : {
8177 12 : error_at (loc, "invalid storage class for function %qE", name);
8178 : }
8179 51447225 : else if (current_scope != file_scope)
8180 : {
8181 : /* Function declaration not at file scope. Storage
8182 : classes other than `extern' are not allowed, C99
8183 : 6.7.1p5, and `extern' makes no difference. However,
8184 : GCC allows 'auto', perhaps with 'inline', to support
8185 : nested functions. */
8186 10567 : if (storage_class == csc_auto)
8187 66 : pedwarn (loc, OPT_Wpedantic,
8188 : "invalid storage class for function %qE", name);
8189 10501 : else if (storage_class == csc_static)
8190 : {
8191 20 : error_at (loc, "invalid storage class for function %qE", name);
8192 20 : if (funcdef_flag)
8193 8 : storage_class = declspecs->storage_class = csc_none;
8194 : else
8195 : return NULL_TREE;
8196 : }
8197 : }
8198 :
8199 102894450 : decl = build_decl (declarator->id_loc,
8200 51447225 : FUNCTION_DECL, declarator->u.id.id, type);
8201 51447225 : decl = build_decl_attribute_variant (decl, decl_attr);
8202 :
8203 51447225 : if (type_quals & TYPE_QUAL_ATOMIC)
8204 : {
8205 2 : error_at (loc,
8206 : "%<_Atomic%>-qualified function type");
8207 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
8208 : }
8209 51447223 : else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
8210 9 : pedwarn (loc, OPT_Wpedantic,
8211 : "ISO C forbids qualified function types");
8212 :
8213 : /* Every function declaration is an external reference
8214 : (DECL_EXTERNAL) except for those which are not at file
8215 : scope and are explicitly declared "auto". This is
8216 : forbidden by standard C (C99 6.7.1p5) and is interpreted by
8217 : GCC to signify a forward declaration of a nested function. */
8218 51447225 : if (storage_class == csc_auto && current_scope != file_scope)
8219 66 : DECL_EXTERNAL (decl) = 0;
8220 : /* In C99, a function which is declared 'inline' with 'extern'
8221 : is not an external reference (which is confusing). It
8222 : means that the later definition of the function must be output
8223 : in this file, C99 6.7.4p6. In GNU C89, a function declared
8224 : 'extern inline' is an external reference. */
8225 51447159 : else if (declspecs->inline_p && storage_class != csc_static)
8226 35497806 : DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
8227 35497806 : == flag_gnu89_inline);
8228 : else
8229 15949353 : DECL_EXTERNAL (decl) = !initialized;
8230 :
8231 : /* Record absence of global scope for `static' or `auto'. */
8232 51447225 : TREE_PUBLIC (decl)
8233 51447225 : = !(storage_class == csc_static || storage_class == csc_auto);
8234 :
8235 : /* For a function definition, record the argument information
8236 : block where store_parm_decls will look for it. */
8237 51447225 : if (funcdef_flag)
8238 36350917 : current_function_arg_info = arg_info;
8239 :
8240 51447225 : if (declspecs->default_int_p)
8241 9220 : C_FUNCTION_IMPLICIT_INT (decl) = 1;
8242 :
8243 : /* Record presence of `inline' and `_Noreturn', if it is
8244 : reasonable. */
8245 51447225 : if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
8246 : {
8247 47845 : if (declspecs->inline_p)
8248 5 : pedwarn (loc, 0, "cannot inline function %<main%>");
8249 47845 : if (declspecs->noreturn_p)
8250 1 : pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
8251 : }
8252 : else
8253 : {
8254 51399380 : if (declspecs->inline_p)
8255 : /* Record that the function is declared `inline'. */
8256 35658882 : DECL_DECLARED_INLINE_P (decl) = 1;
8257 51399380 : if (declspecs->noreturn_p)
8258 : {
8259 23307 : if (flag_isoc99)
8260 23304 : pedwarn_c99 (loc, OPT_Wpedantic,
8261 : "ISO C99 does not support %<_Noreturn%>");
8262 : else
8263 3 : pedwarn_c99 (loc, OPT_Wpedantic,
8264 : "ISO C90 does not support %<_Noreturn%>");
8265 23307 : TREE_THIS_VOLATILE (decl) = 1;
8266 : }
8267 : }
8268 :
8269 : /* C99 6.2.2p7: It is invalid (compile-time undefined
8270 : behavior) to create an 'extern' declaration for a
8271 : function if there is a global declaration that is
8272 : 'static' and the global declaration is not visible.
8273 : (If the static declaration _is_ currently visible,
8274 : the 'extern' declaration is taken to refer to that decl.) */
8275 51447225 : if (!initialized
8276 15096299 : && TREE_PUBLIC (decl)
8277 15088322 : && current_scope != file_scope)
8278 : {
8279 8890 : tree global_decl = identifier_global_value (declarator->u.id.id);
8280 8890 : tree visible_decl = lookup_name (declarator->u.id.id);
8281 :
8282 8890 : if (global_decl
8283 8890 : && global_decl != visible_decl
8284 1713 : && VAR_OR_FUNCTION_DECL_P (global_decl)
8285 1713 : && !TREE_PUBLIC (global_decl))
8286 2 : error_at (loc, "function previously declared %<static%> "
8287 : "redeclared %<extern%>");
8288 : }
8289 : }
8290 : else
8291 : {
8292 : /* It's a variable. */
8293 : /* An uninitialized decl with `extern' is a reference. */
8294 8930098 : int extern_ref = !initialized && storage_class == csc_extern;
8295 :
8296 8930098 : if (constexprp)
8297 : {
8298 : /* The type of a constexpr variable must not be variably
8299 : modified, volatile, atomic or restrict qualified or
8300 : have a member with such a qualifier. const
8301 : qualification is implicitly added, and, at file scope,
8302 : has internal linkage. */
8303 360 : if (c_type_variably_modified_p (type))
8304 1 : error_at (loc, "%<constexpr%> object has variably modified "
8305 : "type");
8306 360 : if (type_quals
8307 360 : & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
8308 9 : error_at (loc, "invalid qualifiers for %<constexpr%> object");
8309 : else
8310 : {
8311 351 : tree type_no_array = strip_array_types (type);
8312 351 : if (RECORD_OR_UNION_TYPE_P (type_no_array)
8313 351 : && C_TYPE_FIELDS_NON_CONSTEXPR (type_no_array))
8314 8 : error_at (loc, "invalid qualifiers for field of "
8315 : "%<constexpr%> object");
8316 : }
8317 360 : type_quals |= TYPE_QUAL_CONST;
8318 360 : if (current_scope == file_scope)
8319 294 : storage_class = csc_static;
8320 : }
8321 :
8322 8930098 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
8323 : orig_qual_indirect);
8324 :
8325 : /* C99 6.2.2p7: It is invalid (compile-time undefined
8326 : behavior) to create an 'extern' declaration for a
8327 : variable if there is a global declaration that is
8328 : 'static' and the global declaration is not visible.
8329 : (If the static declaration _is_ currently visible,
8330 : the 'extern' declaration is taken to refer to that decl.) */
8331 8930098 : if (extern_ref && current_scope != file_scope)
8332 : {
8333 1571 : tree global_decl = identifier_global_value (declarator->u.id.id);
8334 1571 : tree visible_decl = lookup_name (declarator->u.id.id);
8335 :
8336 1571 : if (global_decl
8337 1571 : && global_decl != visible_decl
8338 282 : && VAR_P (global_decl)
8339 282 : && !TREE_PUBLIC (global_decl))
8340 8 : error_at (loc, "variable previously declared %<static%> "
8341 : "redeclared %<extern%>");
8342 : }
8343 :
8344 17860196 : decl = build_decl (declarator->id_loc,
8345 8930098 : VAR_DECL, declarator->u.id.id, type);
8346 8930098 : if (size_varies)
8347 7443 : C_DECL_VARIABLE_SIZE (decl) = 1;
8348 8930098 : if (constexprp)
8349 360 : C_DECL_DECLARED_CONSTEXPR (decl) = 1;
8350 :
8351 8930098 : if (declspecs->inline_p)
8352 4 : pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
8353 8930098 : if (declspecs->noreturn_p)
8354 1 : pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
8355 :
8356 : /* At file scope, an initialized extern declaration may follow
8357 : a static declaration. In that case, DECL_EXTERNAL will be
8358 : reset later in start_decl. */
8359 8930098 : DECL_EXTERNAL (decl) = (storage_class == csc_extern);
8360 :
8361 : /* At file scope, the presence of a `static' or `register' storage
8362 : class specifier, or the absence of all storage class specifiers
8363 : makes this declaration a definition (perhaps tentative). Also,
8364 : the absence of `static' makes it public. */
8365 8930098 : if (current_scope == file_scope)
8366 : {
8367 1150962 : TREE_PUBLIC (decl) = storage_class != csc_static;
8368 1150962 : TREE_STATIC (decl) = !extern_ref;
8369 : }
8370 : /* Not at file scope, only `static' makes a static definition. */
8371 : else
8372 : {
8373 7779136 : TREE_STATIC (decl) = (storage_class == csc_static);
8374 7779136 : TREE_PUBLIC (decl) = extern_ref;
8375 : }
8376 :
8377 : // NB: Set a tentative TLS model to avoid tls_model attribute
8378 : // warnings due to lack of thread storage duration. It will
8379 : // be updated by c_decl_attributes later.
8380 8930098 : if (threadp)
8381 2833 : set_decl_tls_model (decl, TLS_MODEL_REAL);
8382 : }
8383 :
8384 189525028 : if ((storage_class == csc_extern
8385 139079886 : || (storage_class == csc_none
8386 138616356 : && TREE_CODE (type) == FUNCTION_TYPE
8387 809679 : && !funcdef_flag))
8388 189833964 : && c_type_variably_modified_p (type))
8389 : {
8390 : /* C99 6.7.5.2p2 */
8391 6 : if (TREE_CODE (type) == FUNCTION_TYPE)
8392 4 : error_at (loc, "non-nested function with variably modified type");
8393 : else
8394 2 : error_at (loc, "object with variably modified type must have "
8395 : "no linkage");
8396 : }
8397 :
8398 : /* For nested functions disqualify ones taking VLAs by value
8399 : from inlining since the middle-end cannot deal with this.
8400 : ??? We should arrange for those to be passed by reference
8401 : with emitting the copy on the caller side in the frontend. */
8402 189525028 : if (storage_class == csc_none
8403 138616356 : && TREE_CODE (type) == FUNCTION_TYPE)
8404 4179771 : for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
8405 : {
8406 3370132 : tree arg = TREE_VALUE (al);
8407 3370132 : if (arg != error_mark_node
8408 3370132 : && C_TYPE_VARIABLE_SIZE (arg))
8409 : {
8410 40 : DECL_UNINLINABLE (decl) = 1;
8411 40 : break;
8412 : }
8413 : }
8414 :
8415 : /* Record `register' declaration for warnings on &
8416 : and in case doing stupid register allocation. */
8417 :
8418 189525028 : if (storage_class == csc_register
8419 3644 : && TREE_CODE (type) != FUNCTION_TYPE)
8420 : {
8421 3638 : C_DECL_REGISTER (decl) = 1;
8422 3638 : DECL_REGISTER (decl) = 1;
8423 : }
8424 :
8425 : /* Record constancy and volatility. */
8426 189525028 : c_apply_type_quals_to_decl (type_quals, decl);
8427 :
8428 : /* Apply _Alignas specifiers. */
8429 189525028 : if (alignas_align)
8430 : {
8431 123 : SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
8432 123 : DECL_USER_ALIGN (decl) = 1;
8433 : }
8434 :
8435 : /* If a type has volatile components, it should be stored in memory.
8436 : Otherwise, the fact that those components are volatile
8437 : will be ignored, and would even crash the compiler.
8438 : Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
8439 189525028 : if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
8440 189525028 : && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
8441 : || TREE_CODE (decl) == RESULT_DECL))
8442 : {
8443 : /* It is not an error for a structure with volatile fields to
8444 : be declared register, but reset DECL_REGISTER since it
8445 : cannot actually go in a register. */
8446 184 : int was_reg = C_DECL_REGISTER (decl);
8447 184 : C_DECL_REGISTER (decl) = 0;
8448 184 : DECL_REGISTER (decl) = 0;
8449 184 : c_mark_addressable (decl);
8450 184 : C_DECL_REGISTER (decl) = was_reg;
8451 : }
8452 :
8453 : /* This is the earliest point at which we might know the assembler
8454 : name of a variable. Thus, if it's known before this, die horribly. */
8455 189525028 : gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
8456 : || !DECL_ASSEMBLER_NAME_SET_P (decl));
8457 :
8458 189525028 : if (warn_cxx_compat
8459 24794 : && VAR_P (decl)
8460 7604 : && TREE_PUBLIC (decl)
8461 2807 : && TREE_STATIC (decl)
8462 2358 : && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
8463 2226 : || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
8464 189525175 : && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
8465 4 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
8466 : "non-local variable %qD with anonymous type is "
8467 : "questionable in C++", decl);
8468 :
8469 : return decl;
8470 : }
8471 : }
8472 :
8473 : /* Decode the parameter-list info for a function type or function definition.
8474 : The argument is the value returned by `get_parm_info' (or made in c-parse.c
8475 : if there is an identifier list instead of a parameter decl list).
8476 : These two functions are separate because when a function returns
8477 : or receives functions then each is called multiple times but the order
8478 : of calls is different. The last call to `grokparms' is always the one
8479 : that contains the formal parameter names of a function definition.
8480 :
8481 : Return a list of arg types to use in the FUNCTION_TYPE for this function.
8482 :
8483 : FUNCDEF_FLAG is true for a function definition, false for
8484 : a mere declaration. A nonempty identifier-list gets an error message
8485 : when FUNCDEF_FLAG is false. */
8486 :
8487 : static tree
8488 51033391 : grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
8489 : {
8490 51033391 : tree arg_types = arg_info->types;
8491 :
8492 51033391 : if (funcdef_flag && arg_info->had_vla_unspec)
8493 : {
8494 : /* A function definition isn't function prototype scope C99 6.2.1p4. */
8495 : /* C99 6.7.5.2p4 */
8496 2 : error ("%<[*]%> not allowed in other than function prototype scope");
8497 : }
8498 :
8499 715840 : if (arg_types == NULL_TREE && !funcdef_flag && !flag_isoc23
8500 51036442 : && !in_system_header_at (input_location))
8501 3051 : warning (OPT_Wstrict_prototypes,
8502 : "function declaration isn%'t a prototype");
8503 :
8504 51033391 : if (arg_types == error_mark_node)
8505 : /* Don't set TYPE_ARG_TYPES in this case. */
8506 : return NULL_TREE;
8507 :
8508 101293799 : else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
8509 : {
8510 8684 : if (!funcdef_flag)
8511 : {
8512 13 : permerror_opt (input_location,
8513 13 : OPT_Wdeclaration_missing_parameter_type,
8514 : "parameter names (without types) in "
8515 : "function declaration");
8516 13 : arg_info->parms = NULL_TREE;
8517 : }
8518 : else
8519 8671 : arg_info->parms = arg_info->types;
8520 :
8521 8684 : arg_info->types = NULL_TREE;
8522 8684 : return NULL_TREE;
8523 : }
8524 : else
8525 : {
8526 51024707 : tree parm, type, typelt;
8527 51024707 : unsigned int parmno;
8528 :
8529 : /* In C23, convert () to (void). */
8530 51024707 : if (flag_isoc23
8531 41953949 : && !arg_types
8532 765447 : && !arg_info->parms
8533 765447 : && !arg_info->no_named_args_stdarg_p)
8534 : {
8535 765304 : arg_types = arg_info->types = void_list_node;
8536 765304 : arg_info->c23_empty_parens = 1;
8537 : }
8538 :
8539 : /* If there is a parameter of incomplete type in a definition,
8540 : this is an error. In a declaration this is valid, and a
8541 : struct or union type may be completed later, before any calls
8542 : or definition of the function. In the case where the tag was
8543 : first declared within the parameter list, a warning has
8544 : already been given. If a parameter has void type, then
8545 : this has already received an error (constraint violation in C2Y,
8546 : previously implicitly undefined behavior). */
8547 :
8548 51024707 : for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
8549 174845533 : parm;
8550 123820826 : parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
8551 : {
8552 123820826 : type = TREE_VALUE (typelt);
8553 123820826 : if (type == error_mark_node)
8554 61 : continue;
8555 :
8556 123820765 : if (!COMPLETE_TYPE_P (type))
8557 : {
8558 40 : if (funcdef_flag)
8559 : {
8560 13 : if (DECL_NAME (parm))
8561 13 : error_at (input_location,
8562 : "parameter %u (%q+D) has incomplete type",
8563 : parmno, parm);
8564 : else
8565 0 : error_at (DECL_SOURCE_LOCATION (parm),
8566 : "parameter %u has incomplete type",
8567 : parmno);
8568 :
8569 13 : TREE_VALUE (typelt) = error_mark_node;
8570 13 : TREE_TYPE (parm) = error_mark_node;
8571 13 : arg_types = NULL_TREE;
8572 : }
8573 : }
8574 :
8575 123820765 : if (DECL_NAME (parm) && TREE_USED (parm))
8576 17694 : warn_if_shadowing (parm);
8577 : }
8578 : return arg_types;
8579 : }
8580 : }
8581 :
8582 : /* Allocate and initialize a c_arg_info structure from the parser's
8583 : obstack. */
8584 :
8585 : struct c_arg_info *
8586 51033409 : build_arg_info (void)
8587 : {
8588 51033409 : struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
8589 51033409 : ret->parms = NULL_TREE;
8590 51033409 : ret->tags = NULL;
8591 51033409 : ret->types = NULL_TREE;
8592 51033409 : ret->others = NULL_TREE;
8593 51033409 : ret->pending_sizes = NULL;
8594 51033409 : ret->had_vla_unspec = 0;
8595 51033409 : ret->no_named_args_stdarg_p = 0;
8596 51033409 : ret->c23_empty_parens = 0;
8597 51033409 : return ret;
8598 : }
8599 :
8600 : /* Take apart the current scope and return a c_arg_info structure with
8601 : info on a parameter list just parsed.
8602 :
8603 : This structure is later fed to 'grokparms' and 'store_parm_decls'.
8604 :
8605 : ELLIPSIS being true means the argument list ended in '...' so don't
8606 : append a sentinel (void_list_node) to the end of the type-list.
8607 :
8608 : EXPR is NULL or an expression that needs to be evaluated for the
8609 : side effects of array size expressions in the parameters. */
8610 :
8611 : struct c_arg_info *
8612 50251753 : get_parm_info (bool ellipsis, tree expr)
8613 : {
8614 50251753 : struct c_binding *b = current_scope->bindings;
8615 50251753 : struct c_arg_info *arg_info = build_arg_info ();
8616 :
8617 50251753 : tree parms = NULL_TREE;
8618 50251753 : vec<c_arg_tag, va_gc> *tags = NULL;
8619 50251753 : tree types = NULL_TREE;
8620 50251753 : tree others = NULL_TREE;
8621 :
8622 50251753 : bool gave_void_only_once_err = false;
8623 :
8624 50251753 : arg_info->had_vla_unspec = current_scope->had_vla_unspec;
8625 :
8626 : /* The bindings in this scope must not get put into a block.
8627 : We will take care of deleting the binding nodes. */
8628 50251753 : current_scope->bindings = 0;
8629 :
8630 : /* This function is only called if there was *something* on the
8631 : parameter list. */
8632 50251753 : gcc_assert (b);
8633 :
8634 : /* A parameter list consisting solely of 'void' indicates that the
8635 : function takes no arguments. But if the 'void' is qualified
8636 : (by 'const' or 'volatile'), or has a storage class specifier
8637 : ('register'), then the behavior is undefined; issue an error.
8638 : Typedefs for 'void' are OK (see DR#157). */
8639 50251753 : if (b->prev == 0 /* one binding */
8640 13500145 : && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
8641 13500145 : && !DECL_NAME (b->decl) /* anonymous */
8642 52123343 : && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
8643 : {
8644 979530 : if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
8645 979530 : || C_DECL_REGISTER (b->decl))
8646 96 : error_at (b->locus, "%<void%> as only parameter may not be qualified");
8647 :
8648 : /* There cannot be an ellipsis. */
8649 979530 : if (ellipsis)
8650 17 : error_at (b->locus, "%<void%> must be the only parameter");
8651 :
8652 979530 : arg_info->types = void_list_node;
8653 979530 : return arg_info;
8654 : }
8655 :
8656 49272223 : if (!ellipsis)
8657 49057589 : types = void_list_node;
8658 :
8659 : /* Break up the bindings list into parms, tags, types, and others;
8660 : apply sanity checks; purge the name-to-decl bindings. */
8661 173093512 : while (b)
8662 : {
8663 123821289 : tree decl = b->decl;
8664 123821289 : tree type = TREE_TYPE (decl);
8665 123821289 : c_arg_tag tag;
8666 123821289 : const char *keyword;
8667 :
8668 123821289 : switch (TREE_CODE (decl))
8669 : {
8670 123820949 : case PARM_DECL:
8671 123820949 : if (b->id)
8672 : {
8673 120141935 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8674 120141935 : I_SYMBOL_BINDING (b->id) = b->shadowed;
8675 : }
8676 :
8677 : /* Check for forward decls that never got their actual decl. */
8678 123820949 : if (TREE_ASM_WRITTEN (decl))
8679 4 : error_at (b->locus,
8680 : "parameter %q+D has just a forward declaration", decl);
8681 : /* Check for (..., void, ...) and named void parameters and issue an
8682 : error. */
8683 123820945 : else if (VOID_TYPE_P (type))
8684 : {
8685 114 : if (!gave_void_only_once_err)
8686 : {
8687 114 : error_at (b->locus,
8688 : "%<void%> must be the only parameter and unnamed");
8689 114 : gave_void_only_once_err = true;
8690 : }
8691 : }
8692 : else
8693 : {
8694 : /* Valid parameter, add it to the list. */
8695 123820831 : DECL_CHAIN (decl) = parms;
8696 123820831 : parms = decl;
8697 :
8698 : /* Since there is a prototype, args are passed in their
8699 : declared types. The back end may override this later. */
8700 123820831 : DECL_ARG_TYPE (decl) = type;
8701 123820831 : types = tree_cons (0, type, types);
8702 : }
8703 : break;
8704 :
8705 21 : case ENUMERAL_TYPE: keyword = "enum"; goto tag;
8706 20 : case UNION_TYPE: keyword = "union"; goto tag;
8707 101 : case RECORD_TYPE: keyword = "struct"; goto tag;
8708 142 : tag:
8709 : /* Types may not have tag-names, in which case the type
8710 : appears in the bindings list with b->id NULL. */
8711 142 : if (b->id)
8712 : {
8713 97 : gcc_assert (I_TAG_BINDING (b->id) == b);
8714 97 : I_TAG_BINDING (b->id) = b->shadowed;
8715 : }
8716 :
8717 : /* Warn about any struct, union or enum tags defined in a
8718 : parameter list. The scope of such types is limited to
8719 : the parameter list, which is rarely if ever desirable
8720 : (it's impossible to call such a function with type-
8721 : correct arguments). An anonymous union parm type is
8722 : meaningful as a GNU extension, so don't warn for that. */
8723 142 : if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
8724 : {
8725 122 : if (b->id)
8726 : {
8727 : /* The %s will be one of 'struct', 'union', or 'enum'. */
8728 97 : if (!flag_isoc23 || !COMPLETE_TYPE_P (decl))
8729 73 : warning_at (b->locus, 0,
8730 : "%<%s %E%> declared inside parameter list"
8731 : " will not be visible outside of this definition or"
8732 : " declaration", keyword, b->id);
8733 : }
8734 : else
8735 : /* The %s will be one of 'struct', 'union', or 'enum'. */
8736 25 : warning_at (b->locus, 0,
8737 : "anonymous %s declared inside parameter list"
8738 : " will not be visible outside of this definition or"
8739 : " declaration", keyword);
8740 : }
8741 :
8742 142 : tag.id = b->id;
8743 142 : tag.type = decl;
8744 142 : vec_safe_push (tags, tag);
8745 142 : break;
8746 :
8747 20 : case FUNCTION_DECL:
8748 : /* FUNCTION_DECLs appear when there is an implicit function
8749 : declaration in the parameter list. */
8750 20 : gcc_assert (b->nested || seen_error ());
8751 20 : goto set_shadowed;
8752 :
8753 144 : case CONST_DECL:
8754 144 : case TYPE_DECL:
8755 : /* CONST_DECLs appear here when we have an embedded enum,
8756 : and TYPE_DECLs appear here when we have an embedded struct
8757 : or union. No warnings for this - we already warned about the
8758 : type itself. */
8759 :
8760 : /* When we reinsert this decl in the function body, we need
8761 : to reconstruct whether it was marked as nested. */
8762 144 : gcc_assert (!b->nested);
8763 144 : DECL_CHAIN (decl) = others;
8764 144 : others = decl;
8765 : /* fall through */
8766 :
8767 198 : case ERROR_MARK:
8768 198 : set_shadowed:
8769 : /* error_mark_node appears here when we have an undeclared
8770 : variable. Just throw it away. */
8771 198 : if (b->id)
8772 : {
8773 56 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8774 56 : I_SYMBOL_BINDING (b->id) = b->shadowed;
8775 : }
8776 : break;
8777 :
8778 : /* Other things that might be encountered. */
8779 0 : case LABEL_DECL:
8780 0 : case VAR_DECL:
8781 0 : default:
8782 0 : gcc_unreachable ();
8783 : }
8784 :
8785 123821289 : b = free_binding_and_advance (b);
8786 : }
8787 :
8788 49272223 : arg_info->parms = parms;
8789 49272223 : arg_info->tags = tags;
8790 49272223 : arg_info->types = types;
8791 49272223 : arg_info->others = others;
8792 49272223 : arg_info->pending_sizes = expr;
8793 49272223 : arg_info->no_named_args_stdarg_p = ellipsis && !types;
8794 49272223 : return arg_info;
8795 : }
8796 :
8797 : /* Get the struct, enum or union (CODE says which) with tag NAME.
8798 : Define the tag as a forward-reference with location LOC if it is
8799 : not defined. HAVE_STD_ATTRS says whether any standard attributes
8800 : were present after the struct, union or enum keyword; ATTRS are the
8801 : standard attributes present there. HAS_ENUM_TYPE_SPECIFIER says
8802 : whether an enum type specifier (": specifier-qualifier-list") is
8803 : present; if so, this is called before that specifier is parsed, so
8804 : that the tag is in scope for that specifier. Return a c_typespec
8805 : structure for the type specifier. */
8806 :
8807 : struct c_typespec
8808 1096779 : parser_xref_tag (location_t loc, enum tree_code code, tree name,
8809 : bool have_std_attrs, tree attrs, bool has_enum_type_specifier)
8810 : {
8811 1096779 : struct c_typespec ret;
8812 1096779 : tree ref;
8813 1096779 : location_t refloc;
8814 :
8815 1096779 : ret.expr = NULL_TREE;
8816 1096779 : ret.expr_const_operands = true;
8817 1096779 : ret.has_enum_type_specifier = has_enum_type_specifier;
8818 :
8819 : /* If a cross reference is requested, look up the type already
8820 : defined for this tag and return it. If an enum type specifier is
8821 : present, only a definition in the current scope is relevant. */
8822 :
8823 1096779 : ref = lookup_tag (code, name, has_enum_type_specifier, &refloc);
8824 :
8825 : /* If the visible type is still being defined, see if there is
8826 : an earlier definition (which may be complete). We do not
8827 : have to loop because nested redefinitions are not allowed. */
8828 1096779 : if (flag_isoc23 && ref && C_TYPE_BEING_DEFINED (ref))
8829 : {
8830 86474 : tree vis = previous_tag (ref);
8831 86474 : if (vis)
8832 16 : ref = vis;
8833 : }
8834 :
8835 : /* If this is the right type of tag, return what we found.
8836 : (This reference will be shadowed by shadow_tag later if appropriate.)
8837 : If this is the wrong type of tag, do not return it. If it was the
8838 : wrong type in the same scope, we will have had an error
8839 : message already; if in a different scope and declaring
8840 : a name, pending_xref_error will give an error message; but if in a
8841 : different scope and not declaring a name, this tag should
8842 : shadow the previous declaration of a different type of tag, and
8843 : this would not work properly if we return the reference found.
8844 : (For example, with "struct foo" in an outer scope, "union foo;"
8845 : must shadow that tag with a new one of union type.) */
8846 2193558 : ret.kind = (ref
8847 1096779 : ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
8848 95657 : : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
8849 1096779 : if (ref && TREE_CODE (ref) == code)
8850 : {
8851 1001088 : decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8852 1001088 : if (C_TYPE_DEFINED_IN_STRUCT (ref)
8853 5 : && loc != UNKNOWN_LOCATION
8854 1001093 : && warn_cxx_compat)
8855 : {
8856 5 : auto_diagnostic_group d;
8857 5 : switch (code)
8858 : {
8859 2 : case ENUMERAL_TYPE:
8860 2 : if (warning_at (loc, OPT_Wc___compat,
8861 : ("enum type defined in struct or union "
8862 : "is not visible in C++")))
8863 2 : inform (refloc, "enum type defined here");
8864 : break;
8865 2 : case RECORD_TYPE:
8866 2 : if (warning_at (loc, OPT_Wc___compat,
8867 : ("struct defined in struct or union "
8868 : "is not visible in C++")))
8869 2 : inform (refloc, "struct defined here");
8870 : break;
8871 1 : case UNION_TYPE:
8872 1 : if (warning_at (loc, OPT_Wc___compat,
8873 : ("union defined in struct or union "
8874 : "is not visible in C++")))
8875 1 : inform (refloc, "union defined here");
8876 : break;
8877 0 : default:
8878 0 : gcc_unreachable();
8879 : }
8880 5 : }
8881 :
8882 1001088 : ret.spec = ref;
8883 1001088 : return ret;
8884 : }
8885 :
8886 : /* If no such tag is yet defined, create a forward-reference node
8887 : and record it as the "definition".
8888 : When a real declaration of this type is found,
8889 : the forward-reference will be altered into a real type. */
8890 :
8891 95691 : ref = make_node (code);
8892 95691 : if (flag_isoc23 || code == ENUMERAL_TYPE)
8893 73423 : SET_TYPE_STRUCTURAL_EQUALITY (ref);
8894 73423 : if (code == ENUMERAL_TYPE)
8895 : {
8896 : /* Give the type a default layout like unsigned int
8897 : to avoid crashing if it does not get defined. */
8898 273 : SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
8899 273 : SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
8900 273 : TYPE_USER_ALIGN (ref) = 0;
8901 273 : TYPE_UNSIGNED (ref) = 1;
8902 273 : TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
8903 273 : TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
8904 273 : TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
8905 273 : ENUM_FIXED_UNDERLYING_TYPE_P (ref) = has_enum_type_specifier;
8906 : }
8907 :
8908 95691 : pushtag (loc, name, ref);
8909 95691 : decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8910 95691 : if (in_underspecified_init)
8911 9 : error_at (loc, "%qT declared in underspecified object initializer",
8912 : ref);
8913 :
8914 95691 : ret.spec = ref;
8915 95691 : return ret;
8916 : }
8917 :
8918 : /* Get the struct, enum or union (CODE says which) with tag NAME.
8919 : Define the tag as a forward-reference if it is not defined.
8920 : Return a tree for the type. */
8921 :
8922 : tree
8923 0 : xref_tag (enum tree_code code, tree name)
8924 : {
8925 0 : return parser_xref_tag (input_location, code, name, false, NULL_TREE,
8926 0 : false).spec;
8927 : }
8928 :
8929 : /* Make sure that the tag NAME is defined *in the current scope*
8930 : at least as a forward reference.
8931 : LOC is the location of the struct's definition.
8932 : CODE says which kind of tag NAME ought to be.
8933 :
8934 : This stores the current value of the file static STRUCT_PARSE_INFO
8935 : in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8936 : new c_struct_parse_info structure. The old value of
8937 : STRUCT_PARSE_INFO is restored in finish_struct. */
8938 :
8939 : tree
8940 1183107 : start_struct (location_t loc, enum tree_code code, tree name,
8941 : class c_struct_parse_info **enclosing_struct_parse_info)
8942 : {
8943 : /* If there is already a tag defined at this scope
8944 : (as a forward reference), just return it. */
8945 :
8946 1183107 : tree ref = NULL_TREE;
8947 1183107 : location_t refloc = UNKNOWN_LOCATION;
8948 :
8949 1183107 : if (name != NULL_TREE)
8950 488457 : ref = lookup_tag (code, name, true, &refloc);
8951 :
8952 : /* For C23, even if we already have a completed definition,
8953 : we do not use it. We will check for consistency later.
8954 : If we are in a nested redefinition the type is not
8955 : complete. We will then detect this below. */
8956 1183107 : if (flag_isoc23 && ref && TYPE_SIZE (ref))
8957 : ref = NULL_TREE;
8958 :
8959 1182998 : if (ref && TREE_CODE (ref) == code)
8960 : {
8961 25411 : if (TYPE_STUB_DECL (ref))
8962 25411 : refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8963 :
8964 25411 : if (TYPE_SIZE (ref))
8965 : {
8966 11 : auto_diagnostic_group d;
8967 11 : if (code == UNION_TYPE)
8968 2 : error_at (loc, "redefinition of %<union %E%>", name);
8969 : else
8970 9 : error_at (loc, "redefinition of %<struct %E%>", name);
8971 11 : if (refloc != UNKNOWN_LOCATION)
8972 11 : inform (refloc, "originally defined here");
8973 : /* Don't create structures using a name already in use. */
8974 11 : ref = NULL_TREE;
8975 11 : }
8976 25400 : else if (C_TYPE_BEING_DEFINED (ref))
8977 : {
8978 17 : if (code == UNION_TYPE)
8979 1 : error_at (loc, "nested redefinition of %<union %E%>", name);
8980 : else
8981 16 : error_at (loc, "nested redefinition of %<struct %E%>", name);
8982 : /* Don't bother to report "originally defined here" for a
8983 : nested redefinition; the original definition should be
8984 : obvious. */
8985 : /* Don't create structures that contain themselves. */
8986 : ref = NULL_TREE;
8987 : }
8988 : }
8989 :
8990 : /* Otherwise create a forward-reference just so the tag is in scope. */
8991 :
8992 25397 : if (ref == NULL_TREE || TREE_CODE (ref) != code)
8993 : {
8994 1157724 : ref = make_node (code);
8995 1157724 : if (flag_isoc23)
8996 929315 : SET_TYPE_STRUCTURAL_EQUALITY (ref);
8997 1157724 : pushtag (loc, name, ref);
8998 : }
8999 :
9000 1183107 : C_TYPE_BEING_DEFINED (ref) = 1;
9001 2396993 : for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
9002 1213886 : TYPE_PACKED (v) = flag_pack_struct;
9003 :
9004 1183107 : *enclosing_struct_parse_info = struct_parse_info;
9005 1183107 : struct_parse_info = new c_struct_parse_info ();
9006 1183107 : struct_parse_info->refloc = refloc;
9007 :
9008 : /* FIXME: This will issue a warning for a use of a type defined
9009 : within a statement expr used within sizeof, et. al. This is not
9010 : terribly serious as C++ doesn't permit statement exprs within
9011 : sizeof anyhow. */
9012 1183107 : if (warn_cxx_compat
9013 576 : && (in_sizeof || in_typeof || in_alignof || in_countof))
9014 10 : warning_at (loc, OPT_Wc___compat,
9015 : "defining type in %qs expression is invalid in C++",
9016 : (in_sizeof ? "sizeof"
9017 4 : : in_typeof ? "typeof"
9018 1 : : in_alignof ? "alignof"
9019 : : "_Countof"));
9020 :
9021 1183107 : if (in_underspecified_init)
9022 18 : error_at (loc, "%qT defined in underspecified object initializer", ref);
9023 :
9024 1183107 : return ref;
9025 : }
9026 :
9027 : /* Process the specs, declarator and width (NULL if omitted)
9028 : of a structure component, returning a FIELD_DECL node.
9029 : WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
9030 : DECL_ATTRS is as for grokdeclarator.
9031 :
9032 : LOC is the location of the structure component.
9033 :
9034 : This is done during the parsing of the struct declaration.
9035 : The FIELD_DECL nodes are chained together and the lot of them
9036 : are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
9037 :
9038 : tree
9039 4324430 : grokfield (location_t loc,
9040 : struct c_declarator *declarator, struct c_declspecs *declspecs,
9041 : tree width, tree *decl_attrs, tree *expr)
9042 : {
9043 4324430 : tree value;
9044 :
9045 4324430 : if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
9046 20587 : && width == NULL_TREE)
9047 : {
9048 : /* This is an unnamed decl.
9049 :
9050 : If we have something of the form "union { list } ;" then this
9051 : is the anonymous union extension. Similarly for struct.
9052 :
9053 : If this is something of the form "struct foo;", then
9054 : If MS or Plan 9 extensions are enabled, this is handled as
9055 : an anonymous struct.
9056 : Otherwise this is a forward declaration of a structure tag.
9057 :
9058 : If this is something of the form "foo;" and foo is a TYPE_DECL, then
9059 : If foo names a structure or union without a tag, then this
9060 : is an anonymous struct (this is permitted by C11).
9061 : If MS or Plan 9 extensions are enabled and foo names a
9062 : structure, then again this is an anonymous struct.
9063 : Otherwise this is an error.
9064 :
9065 : Oh what a horrid tangled web we weave. I wonder if MS consciously
9066 : took this from Plan 9 or if it was an accident of implementation
9067 : that took root before someone noticed the bug... */
9068 :
9069 10780 : tree type = declspecs->type;
9070 10780 : bool ok = false;
9071 :
9072 10780 : if (RECORD_OR_UNION_TYPE_P (type)
9073 10774 : && (flag_ms_extensions
9074 10745 : || flag_plan9_extensions
9075 10714 : || !declspecs->typedef_p))
9076 : {
9077 10767 : if (flag_ms_extensions || flag_plan9_extensions)
9078 : ok = true;
9079 10707 : else if (TYPE_NAME (type) == NULL)
9080 : ok = true;
9081 : else
9082 : ok = false;
9083 : }
9084 : if (!ok)
9085 : {
9086 29 : pedwarn (loc, 0, "declaration does not declare anything");
9087 29 : return NULL_TREE;
9088 : }
9089 10751 : if (flag_isoc99)
9090 10729 : pedwarn_c99 (loc, OPT_Wpedantic,
9091 : "ISO C99 doesn%'t support unnamed structs/unions");
9092 : else
9093 22 : pedwarn_c99 (loc, OPT_Wpedantic,
9094 : "ISO C90 doesn%'t support unnamed structs/unions");
9095 : }
9096 :
9097 4324401 : value = grokdeclarator (declarator, declspecs, FIELD, false,
9098 4324401 : width ? &width : NULL, decl_attrs, expr, NULL,
9099 : DEPRECATED_NORMAL);
9100 :
9101 : /* When this field has name, its type is a top level type, we should
9102 : call verify_counted_by_for_top_anonymous_type. */
9103 4324401 : if (DECL_NAME (value) != NULL_TREE
9104 4324401 : && declspecs->typespec_kind == ctsk_tagdef)
9105 153806 : verify_counted_by_for_top_anonymous_type (declspecs->type);
9106 :
9107 4324401 : finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
9108 4324401 : DECL_INITIAL (value) = width;
9109 4324401 : if (width)
9110 52817 : SET_DECL_C_BIT_FIELD (value);
9111 :
9112 4324401 : if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
9113 : {
9114 : /* If we currently have a binding for this field, set the
9115 : in_struct field in the binding, so that we warn about lookups
9116 : which find it. */
9117 1312 : struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
9118 1312 : if (b != NULL)
9119 : {
9120 : /* If the in_struct field is not yet set, push it on a list
9121 : to be cleared when this struct is finished. */
9122 93 : if (!b->in_struct)
9123 : {
9124 91 : struct_parse_info->fields.safe_push (b);
9125 91 : b->in_struct = 1;
9126 : }
9127 : }
9128 : }
9129 :
9130 : return value;
9131 : }
9132 :
9133 : /* Subroutine of detect_field_duplicates: return whether X and Y,
9134 : which are both fields in the same struct, have duplicate field
9135 : names. */
9136 :
9137 : static bool
9138 4315427 : is_duplicate_field (tree x, tree y)
9139 : {
9140 4315427 : if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
9141 : return true;
9142 :
9143 : /* When using -fplan9-extensions, an anonymous field whose name is a
9144 : typedef can duplicate a field name. */
9145 4315426 : if (flag_plan9_extensions
9146 4315426 : && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
9147 : {
9148 0 : tree xt, xn, yt, yn;
9149 :
9150 0 : xt = TREE_TYPE (x);
9151 0 : if (DECL_NAME (x) != NULL_TREE)
9152 0 : xn = DECL_NAME (x);
9153 0 : else if (RECORD_OR_UNION_TYPE_P (xt)
9154 0 : && TYPE_NAME (xt) != NULL_TREE
9155 0 : && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
9156 0 : xn = DECL_NAME (TYPE_NAME (xt));
9157 : else
9158 : xn = NULL_TREE;
9159 :
9160 0 : yt = TREE_TYPE (y);
9161 0 : if (DECL_NAME (y) != NULL_TREE)
9162 0 : yn = DECL_NAME (y);
9163 0 : else if (RECORD_OR_UNION_TYPE_P (yt)
9164 0 : && TYPE_NAME (yt) != NULL_TREE
9165 0 : && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
9166 0 : yn = DECL_NAME (TYPE_NAME (yt));
9167 : else
9168 : yn = NULL_TREE;
9169 :
9170 0 : if (xn != NULL_TREE && xn == yn)
9171 0 : return true;
9172 : }
9173 :
9174 : return false;
9175 : }
9176 :
9177 : /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
9178 : to HTAB, giving errors for any duplicates. */
9179 :
9180 : static void
9181 80964 : detect_field_duplicates_hash (tree fieldlist,
9182 : hash_table<nofree_ptr_hash <tree_node> > *htab)
9183 : {
9184 80964 : tree x, y;
9185 80964 : tree_node **slot;
9186 :
9187 1410142 : for (x = fieldlist; x ; x = DECL_CHAIN (x))
9188 1329178 : if ((y = DECL_NAME (x)) != NULL_TREE)
9189 : {
9190 1308539 : slot = htab->find_slot (y, INSERT);
9191 1308539 : if (*slot)
9192 : {
9193 15 : error ("duplicate member %q+D", x);
9194 15 : DECL_NAME (x) = NULL_TREE;
9195 : }
9196 1308539 : *slot = y;
9197 : }
9198 20639 : else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9199 : {
9200 11549 : detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
9201 :
9202 : /* When using -fplan9-extensions, an anonymous field whose
9203 : name is a typedef can duplicate a field name. */
9204 11549 : if (flag_plan9_extensions
9205 31 : && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9206 11579 : && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
9207 : {
9208 10 : tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
9209 10 : slot = htab->find_slot (xn, INSERT);
9210 10 : if (*slot)
9211 0 : error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
9212 10 : *slot = xn;
9213 : }
9214 : }
9215 80964 : }
9216 :
9217 : /* Generate an error for any duplicate field names in FIELDLIST. Munge
9218 : the list such that this does not present a problem later. */
9219 :
9220 : static void
9221 1183237 : detect_field_duplicates (tree fieldlist)
9222 : {
9223 1183237 : tree x, y;
9224 1183237 : int timeout = 10;
9225 :
9226 : /* If the struct is the list of instance variables of an Objective-C
9227 : class, then we need to check all the instance variables of
9228 : superclasses when checking for duplicates (since you can't have
9229 : an instance variable in a subclass with the same name as an
9230 : instance variable in a superclass). We pass on this job to the
9231 : Objective-C compiler. objc_detect_field_duplicates() will return
9232 : false if we are not checking the list of instance variables and
9233 : the C frontend should proceed with the standard field duplicate
9234 : checks. If we are checking the list of instance variables, the
9235 : ObjC frontend will do the check, emit the errors if needed, and
9236 : then return true. */
9237 1183237 : if (c_dialect_objc ())
9238 0 : if (objc_detect_field_duplicates (false))
9239 : return;
9240 :
9241 : /* First, see if there are more than "a few" fields.
9242 : This is trivially true if there are zero or one fields. */
9243 1183237 : if (!fieldlist || !DECL_CHAIN (fieldlist))
9244 : return;
9245 : x = fieldlist;
9246 3487460 : do {
9247 3487460 : timeout--;
9248 3487460 : if (DECL_NAME (x) == NULL_TREE
9249 3487460 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9250 : timeout = 0;
9251 3487460 : x = DECL_CHAIN (x);
9252 3487460 : } while (timeout > 0 && x);
9253 :
9254 : /* If there were "few" fields and no anonymous structures or unions,
9255 : avoid the overhead of allocating a hash table. Instead just do
9256 : the nested traversal thing. */
9257 977023 : if (timeout > 0)
9258 : {
9259 2827673 : for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
9260 : /* When using -fplan9-extensions, we can have duplicates
9261 : between typedef names and fields. */
9262 1920065 : if (DECL_NAME (x)
9263 1920065 : || (flag_plan9_extensions
9264 0 : && DECL_NAME (x) == NULL_TREE
9265 0 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9266 0 : && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9267 0 : && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
9268 : {
9269 6234956 : for (y = fieldlist; y != x; y = TREE_CHAIN (y))
9270 4315427 : if (is_duplicate_field (y, x))
9271 : {
9272 1 : error ("duplicate member %q+D", x);
9273 1 : DECL_NAME (x) = NULL_TREE;
9274 : }
9275 : }
9276 : }
9277 : else
9278 : {
9279 69415 : hash_table<nofree_ptr_hash <tree_node> > htab (37);
9280 69415 : detect_field_duplicates_hash (fieldlist, &htab);
9281 69415 : }
9282 : }
9283 :
9284 : /* Finish up struct info used by -Wc++-compat. */
9285 :
9286 : static void
9287 577 : warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
9288 : location_t record_loc)
9289 : {
9290 577 : unsigned int ix;
9291 577 : tree x;
9292 577 : struct c_binding *b;
9293 :
9294 577 : if (fieldlist == NULL_TREE)
9295 : {
9296 9 : if (code == RECORD_TYPE)
9297 6 : warning_at (record_loc, OPT_Wc___compat,
9298 : "empty struct has size 0 in C, size 1 in C++");
9299 : else
9300 3 : warning_at (record_loc, OPT_Wc___compat,
9301 : "empty union has size 0 in C, size 1 in C++");
9302 : }
9303 :
9304 : /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
9305 : the current struct. We do this now at the end of the struct
9306 : because the flag is used to issue visibility warnings, and we
9307 : only want to issue those warnings if the type is referenced
9308 : outside of the struct declaration. */
9309 625 : FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
9310 48 : C_TYPE_DEFINED_IN_STRUCT (x) = 1;
9311 :
9312 : /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
9313 : typedefs used when declaring fields in this struct. If the name
9314 : of any of the fields is also a typedef name then the struct would
9315 : not parse in C++, because the C++ lookup rules say that the
9316 : typedef name would be looked up in the context of the struct, and
9317 : would thus be the field rather than the typedef. */
9318 577 : if (!struct_parse_info->typedefs_seen.is_empty ()
9319 67 : && fieldlist != NULL_TREE)
9320 : {
9321 : /* Use a hash_set<tree> using the name of the typedef. We can use
9322 : a hash_set<tree> because identifiers are interned. */
9323 67 : hash_set<tree> tset;
9324 :
9325 254 : FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
9326 120 : tset.add (DECL_NAME (x));
9327 :
9328 387 : for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
9329 : {
9330 320 : if (DECL_NAME (x) != NULL_TREE
9331 320 : && tset.contains (DECL_NAME (x)))
9332 : {
9333 2 : warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
9334 : "using %qD as both field and typedef name is "
9335 : "invalid in C++", x);
9336 : /* FIXME: It would be nice to report the location where
9337 : the typedef name is used. */
9338 : }
9339 : }
9340 67 : }
9341 :
9342 : /* For each field which has a binding and which was not defined in
9343 : an enclosing struct, clear the in_struct field. */
9344 668 : FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
9345 91 : b->in_struct = 0;
9346 577 : }
9347 :
9348 : /* Function to help qsort sort FIELD_DECLs by name order. */
9349 :
9350 : static int
9351 20274473 : field_decl_cmp (const void *x_p, const void *y_p)
9352 : {
9353 20274473 : const tree *const x = (const tree *) x_p;
9354 20274473 : const tree *const y = (const tree *) y_p;
9355 :
9356 20274473 : if (DECL_NAME (*x) == DECL_NAME (*y))
9357 : /* A nontype is "greater" than a type. */
9358 0 : return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9359 20274473 : if (DECL_NAME (*x) == NULL_TREE)
9360 : return -1;
9361 20274473 : if (DECL_NAME (*y) == NULL_TREE)
9362 : return 1;
9363 20274473 : if (DECL_NAME (*x) < DECL_NAME (*y))
9364 9988319 : return -1;
9365 : return 1;
9366 : }
9367 :
9368 : /* If this structure or union completes the type of any previous
9369 : variable declaration, lay it out and output its rtl. */
9370 : static void
9371 1366535 : finish_incomplete_vars (tree incomplete_vars, bool toplevel)
9372 : {
9373 1366684 : for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
9374 : {
9375 149 : tree decl = TREE_VALUE (x);
9376 149 : if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9377 0 : layout_array_type (TREE_TYPE (decl));
9378 149 : if (TREE_CODE (decl) != TYPE_DECL)
9379 : {
9380 149 : relayout_decl (decl);
9381 149 : if (c_dialect_objc ())
9382 0 : objc_check_decl (decl);
9383 149 : rest_of_decl_compilation (decl, toplevel, 0);
9384 : }
9385 : }
9386 1366535 : }
9387 :
9388 : /* Determine whether the FIELD_DECL X is a flexible array member according to
9389 : the following info:
9390 : A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT;
9391 : B. whether the FIELD_DECL is an array that is declared as "[]", "[0]",
9392 : or "[1]";
9393 : C. flag_strict_flex_arrays;
9394 : D. the attribute strict_flex_array that is attached to the field
9395 : if presenting.
9396 : Return TRUE when it's a flexible array member, FALSE otherwise. */
9397 :
9398 : static bool
9399 4324552 : is_flexible_array_member_p (bool is_last_field,
9400 : tree x)
9401 : {
9402 : /* If not the last field, return false. */
9403 4324552 : if (!is_last_field)
9404 : return false;
9405 :
9406 : /* If not an array field, return false. */
9407 1658795 : if (TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
9408 : return false;
9409 :
9410 535582 : bool is_zero_length_array = zero_length_array_type_p (TREE_TYPE (x));
9411 535582 : bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x));
9412 535582 : bool is_flexible_array = flexible_array_member_type_p (TREE_TYPE (x));
9413 :
9414 535582 : unsigned int strict_flex_array_level = c_strict_flex_array_level_of (x);
9415 :
9416 535582 : switch (strict_flex_array_level)
9417 : {
9418 : case 0:
9419 : /* Default, all trailing arrays are flexible array members. */
9420 : return true;
9421 77 : case 1:
9422 : /* Level 1: all "[1]", "[0]", and "[]" are flexible array members. */
9423 77 : if (is_one_element_array)
9424 : return true;
9425 : /* FALLTHROUGH. */
9426 141 : case 2:
9427 : /* Level 2: all "[0]", and "[]" are flexible array members. */
9428 141 : if (is_zero_length_array)
9429 : return true;
9430 : /* FALLTHROUGH. */
9431 201 : case 3:
9432 : /* Level 3: Only "[]" are flexible array members. */
9433 201 : if (is_flexible_array)
9434 : return true;
9435 : break;
9436 0 : default:
9437 0 : gcc_unreachable ();
9438 : }
9439 : return false;
9440 : }
9441 :
9442 : /* Recompute TYPE_CANONICAL for variants of the type including qualified
9443 : versions of the type and related pointer types after an aggregate type
9444 : has been finalized.
9445 : Will not update array types, pointers to array types, function
9446 : types and other derived types created while the type was still
9447 : incomplete, those will remain TYPE_STRUCTURAL_EQUALITY_P. */
9448 :
9449 : static void
9450 1203016 : c_update_type_canonical (tree t)
9451 : {
9452 1203016 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t && !TYPE_QUALS (t));
9453 2433024 : for (tree x = t, l = NULL_TREE; x; l = x, x = TYPE_NEXT_VARIANT (x))
9454 : {
9455 1230008 : if (x != t && TYPE_STRUCTURAL_EQUALITY_P (x))
9456 : {
9457 26992 : if (!TYPE_QUALS (x))
9458 26364 : TYPE_CANONICAL (x) = TYPE_CANONICAL (t);
9459 : else
9460 : {
9461 628 : tree
9462 628 : c = c_build_qualified_type (TYPE_CANONICAL (t), TYPE_QUALS (x));
9463 628 : if (TYPE_STRUCTURAL_EQUALITY_P (c))
9464 : {
9465 206 : gcc_checking_assert (TYPE_CANONICAL (t) == t);
9466 206 : if (c == x)
9467 206 : TYPE_CANONICAL (x) = x;
9468 : else
9469 : {
9470 : /* build_qualified_type for this function unhelpfully
9471 : moved c from some later spot in TYPE_MAIN_VARIANT (t)
9472 : chain to right after t (or created it there). Move
9473 : it right before x and process c and then x. */
9474 0 : gcc_checking_assert (TYPE_NEXT_VARIANT (t) == c);
9475 0 : if (l != t)
9476 : {
9477 0 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (c);
9478 0 : TYPE_NEXT_VARIANT (l) = c;
9479 0 : TYPE_NEXT_VARIANT (c) = x;
9480 : }
9481 0 : TYPE_CANONICAL (c) = c;
9482 0 : x = c;
9483 : }
9484 : }
9485 : else
9486 422 : TYPE_CANONICAL (x) = TYPE_CANONICAL (c);
9487 : }
9488 : }
9489 1203016 : else if (x != t)
9490 0 : continue;
9491 1299101 : for (tree p = TYPE_POINTER_TO (x); p; p = TYPE_NEXT_PTR_TO (p))
9492 : {
9493 69093 : if (!TYPE_STRUCTURAL_EQUALITY_P (p))
9494 0 : continue;
9495 69093 : if (TYPE_CANONICAL (x) != x || TYPE_REF_CAN_ALIAS_ALL (p))
9496 66963 : TYPE_CANONICAL (p)
9497 133926 : = c_build_pointer_type_for_mode (TYPE_CANONICAL (x), TYPE_MODE (p),
9498 : false);
9499 : else
9500 2130 : TYPE_CANONICAL (p) = p;
9501 69093 : c_update_type_canonical (p);
9502 : }
9503 : }
9504 1203016 : }
9505 :
9506 :
9507 : /* We set C_TYPE_VARIABLY_MODIFIED for derived types. We will not update
9508 : array types, pointers to array types, function types and other derived
9509 : types created while the type was still incomplete. We need to update
9510 : at least all types for which TYPE_CANONICAL will bet set, because for
9511 : those we later assume (in c_variably_modified_p) that the bit is
9512 : up-to-date. */
9513 :
9514 : static void
9515 734 : c_update_variably_modified (tree t)
9516 : {
9517 1477 : for (tree x = t; x; x = TYPE_NEXT_VARIANT (x))
9518 : {
9519 743 : C_TYPE_VARIABLY_MODIFIED (x) = 1;
9520 761 : for (tree p = TYPE_POINTER_TO (x); p; p = TYPE_NEXT_PTR_TO (p))
9521 18 : c_update_variably_modified (p);
9522 : }
9523 734 : }
9524 :
9525 :
9526 : /* Verify the argument of the counted_by attribute of each field of
9527 : the containing structure, OUTMOST_STRUCT_TYPE, including its inner
9528 : anonymous struct/union, Report error and remove the corresponding
9529 : attribute when it's not. */
9530 :
9531 : static void
9532 531 : verify_counted_by_attribute (tree outmost_struct_type,
9533 : tree cur_struct_type)
9534 : {
9535 1709 : for (tree field = TYPE_FIELDS (cur_struct_type); field;
9536 1178 : field = TREE_CHAIN (field))
9537 : {
9538 1178 : if (flexible_array_member_type_p (TREE_TYPE (field))
9539 1178 : || TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
9540 : {
9541 401 : tree attr_counted_by = lookup_attribute ("counted_by",
9542 401 : DECL_ATTRIBUTES (field));
9543 :
9544 401 : if (!attr_counted_by)
9545 1 : continue;
9546 :
9547 : /* If there is an counted_by attribute attached to the field,
9548 : verify it. */
9549 :
9550 400 : tree fieldname = TREE_VALUE (TREE_VALUE (attr_counted_by));
9551 :
9552 : /* Verify the argument of the attribute is a valid field of the
9553 : containing structure. */
9554 :
9555 400 : tree counted_by_field = lookup_field (outmost_struct_type,
9556 : fieldname);
9557 :
9558 : /* Error when the field is not found in the containing structure
9559 : and remove the corresponding counted_by attribute from the
9560 : field_decl. */
9561 400 : if (!counted_by_field)
9562 : {
9563 24 : error_at (DECL_SOURCE_LOCATION (field),
9564 : "argument %qE to the %<counted_by%> attribute"
9565 : " is not a field declaration in the same structure"
9566 : " as %qD", fieldname, field);
9567 24 : DECL_ATTRIBUTES (field)
9568 48 : = remove_attribute ("counted_by", DECL_ATTRIBUTES (field));
9569 : }
9570 : else
9571 : /* Error when the field is not with an integer type. */
9572 : {
9573 577 : while (TREE_CHAIN (counted_by_field))
9574 201 : counted_by_field = TREE_CHAIN (counted_by_field);
9575 376 : tree real_field = TREE_VALUE (counted_by_field);
9576 :
9577 376 : if (!INTEGRAL_TYPE_P (TREE_TYPE (real_field)))
9578 : {
9579 6 : error_at (DECL_SOURCE_LOCATION (field),
9580 : "argument %qE to the %<counted_by%> attribute"
9581 : " is not a field declaration with an integer type",
9582 : fieldname);
9583 6 : DECL_ATTRIBUTES (field)
9584 12 : = remove_attribute ("counted_by",
9585 6 : DECL_ATTRIBUTES (field));
9586 : }
9587 : }
9588 : }
9589 1457 : else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
9590 868 : && (DECL_NAME (field) == NULL_TREE))
9591 188 : verify_counted_by_attribute (outmost_struct_type, TREE_TYPE (field));
9592 : }
9593 531 : }
9594 :
9595 : /* Caller should make sure the TYPE is a top-level type (i.e. not being
9596 : nested in other structure/uniona). For such type, verify its counted_by
9597 : if it is an anonymous structure/union. */
9598 :
9599 : void
9600 1355687 : verify_counted_by_for_top_anonymous_type (tree type)
9601 : {
9602 1355687 : if (!RECORD_OR_UNION_TYPE_P (type))
9603 : return;
9604 :
9605 1172396 : if (C_TYPE_FIELDS_HAS_COUNTED_BY (type)
9606 1172396 : && c_type_tag (type) == NULL_TREE)
9607 48 : verify_counted_by_attribute (type, type);
9608 : }
9609 :
9610 : /* TYPE is a struct or union that we're applying may_alias to after the body is
9611 : parsed. Fixup any POINTER_TO types. */
9612 :
9613 : static void
9614 353 : c_fixup_may_alias (tree type)
9615 : {
9616 418 : for (tree t = TYPE_POINTER_TO (type); t; t = TYPE_NEXT_PTR_TO (t))
9617 190 : for (tree v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
9618 125 : TYPE_REF_CAN_ALIAS_ALL (v) = true;
9619 353 : }
9620 :
9621 : /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
9622 : LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
9623 : FIELDLIST is a chain of FIELD_DECL nodes for the fields.
9624 : ATTRIBUTES are attributes to be applied to the structure.
9625 :
9626 : ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
9627 : the struct was started. */
9628 :
9629 : tree
9630 1183237 : finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
9631 : class c_struct_parse_info *enclosing_struct_parse_info,
9632 : tree *expr)
9633 : {
9634 1183237 : tree x;
9635 1183237 : bool toplevel = file_scope == current_scope;
9636 :
9637 : /* If this type was previously laid out as a forward reference,
9638 : make sure we lay it out again. */
9639 :
9640 1183237 : TYPE_SIZE (t) = NULL_TREE;
9641 :
9642 1183237 : decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9643 :
9644 1183237 : if (pedantic)
9645 : {
9646 6871 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9647 : {
9648 6852 : if (DECL_NAME (x) != NULL_TREE)
9649 : break;
9650 25 : if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9651 : break;
9652 : }
9653 :
9654 6853 : if (x == NULL_TREE)
9655 : {
9656 19 : if (TREE_CODE (t) == UNION_TYPE)
9657 : {
9658 5 : if (fieldlist)
9659 2 : pedwarn (loc, OPT_Wpedantic, "union has no named members");
9660 : else
9661 3 : pedwarn (loc, OPT_Wpedantic, "union has no members");
9662 : }
9663 : else
9664 : {
9665 14 : if (fieldlist)
9666 3 : pedwarn (loc, OPT_Wpedantic, "struct has no named members");
9667 : else
9668 11 : pedwarn (loc, OPT_Wpedantic, "struct has no members");
9669 : }
9670 : }
9671 : }
9672 :
9673 : /* Install struct as DECL_CONTEXT of each field decl.
9674 : Also process specified field sizes, found in the DECL_INITIAL,
9675 : storing 0 there after the type has been changed to precision equal
9676 : to its width, rather than the precision of the specified standard
9677 : type. (Correct layout requires the original type to have been preserved
9678 : until now.) */
9679 :
9680 : bool saw_named_field = false;
9681 5507829 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9682 : {
9683 : /* Whether this field is the last field of the structure or union.
9684 : for UNION, any field is the last field of it. */
9685 4324592 : bool is_last_field = (DECL_CHAIN (x) == NULL_TREE)
9686 4324592 : || (TREE_CODE (t) == UNION_TYPE);
9687 :
9688 4324592 : if (TREE_TYPE (x) == error_mark_node)
9689 40 : continue;
9690 :
9691 4324552 : DECL_CONTEXT (x) = t;
9692 :
9693 4324552 : tree t1 = strip_array_types (TREE_TYPE (x));
9694 : /* If any field is const, the structure type is pseudo-const. */
9695 4324552 : if (TREE_READONLY (x))
9696 64244 : C_TYPE_FIELDS_READONLY (t) = 1;
9697 : else
9698 : {
9699 : /* A field that is pseudo-const makes the structure likewise. */
9700 4260308 : if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
9701 641 : C_TYPE_FIELDS_READONLY (t) = 1;
9702 : }
9703 :
9704 : /* Any field that is volatile means variables of this type must be
9705 : treated in some ways as volatile. */
9706 4324552 : if (TREE_THIS_VOLATILE (x))
9707 : {
9708 475 : C_TYPE_FIELDS_VOLATILE (t) = 1;
9709 475 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9710 : }
9711 :
9712 : /* Any field that is volatile, restrict-qualified or atomic
9713 : means the type cannot be used for a constexpr object. */
9714 4324552 : if (TYPE_QUALS (t1)
9715 4324552 : & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
9716 1461 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9717 4323091 : else if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_NON_CONSTEXPR (t1))
9718 179 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9719 :
9720 : /* Any field of nominal variable size implies structure is too. */
9721 4324552 : if (C_DECL_VARIABLE_SIZE (x))
9722 698 : C_TYPE_VARIABLE_SIZE (t) = 1;
9723 :
9724 : /* If any field is variably modified, record this fact. */
9725 4324552 : if (c_type_variably_modified_p (TREE_TYPE (x)))
9726 777 : C_TYPE_VARIABLY_MODIFIED (t) = 1;
9727 :
9728 4324552 : if (DECL_C_BIT_FIELD (x))
9729 : {
9730 52826 : unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
9731 52826 : DECL_SIZE (x) = bitsize_int (width);
9732 52826 : DECL_BIT_FIELD (x) = 1;
9733 : }
9734 :
9735 4324552 : if (TYPE_PACKED (t)
9736 4330108 : && (DECL_BIT_FIELD (x)
9737 3236 : || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
9738 4445 : DECL_PACKED (x) = 1;
9739 :
9740 : /* Detect flexible array member in an invalid context. */
9741 4324552 : if (flexible_array_member_type_p (TREE_TYPE (x)))
9742 : {
9743 86048 : if (TREE_CODE (t) == UNION_TYPE)
9744 31 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9745 : "flexible array member in union is a GCC extension");
9746 86017 : else if (!is_last_field)
9747 : {
9748 3 : error_at (DECL_SOURCE_LOCATION (x),
9749 : "flexible array member not at end of struct");
9750 3 : TREE_TYPE (x) = error_mark_node;
9751 : }
9752 86014 : else if (!saw_named_field)
9753 37 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9754 : "flexible array member in a struct with no named "
9755 : "members is a GCC extension");
9756 86048 : if (lookup_attribute ("counted_by", DECL_ATTRIBUTES (x)))
9757 133 : C_TYPE_FIELDS_HAS_COUNTED_BY (t) = 1;
9758 : }
9759 :
9760 4324552 : if (TREE_CODE (TREE_TYPE (x)) == POINTER_TYPE
9761 4324552 : && lookup_attribute ("counted_by", DECL_ATTRIBUTES (x)))
9762 267 : C_TYPE_FIELDS_HAS_COUNTED_BY (t) = 1;
9763 :
9764 : /* If the field is an anonymous structure that includes a field
9765 : with counted_by attribute, this structure should also be marked
9766 : too. */
9767 8270933 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9768 469023 : && C_TYPE_FIELDS_HAS_COUNTED_BY (TREE_TYPE (x))
9769 18 : && DECL_NAME (x) == NULL_TREE
9770 4324565 : && c_type_tag (TREE_TYPE (x)) == NULL_TREE)
9771 12 : C_TYPE_FIELDS_HAS_COUNTED_BY (t) = 1;
9772 :
9773 23108 : if (pedantic && TREE_CODE (t) == RECORD_TYPE
9774 4343705 : && flexible_array_type_p (TREE_TYPE (x)))
9775 18 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9776 : "invalid use of structure with flexible array member");
9777 :
9778 : /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */
9779 4324552 : DECL_NOT_FLEXARRAY (x) = !is_flexible_array_member_p (is_last_field, x);
9780 :
9781 : /* Set TYPE_INCLUDES_FLEXARRAY for the context of x, t.
9782 : when x is an array and is the last field.
9783 : There is only one last_field for a structure type, but there might
9784 : be multiple last_fields for a union type, therefore we should ORed
9785 : the result for multiple last_fields. */
9786 4324552 : if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
9787 692774 : TYPE_INCLUDES_FLEXARRAY (t)
9788 1385548 : |= is_last_field && flexible_array_member_type_p (TREE_TYPE (x));
9789 : /* Recursively set TYPE_INCLUDES_FLEXARRAY for the context of x, t
9790 : when x is an union or record and is the last field. */
9791 3631778 : else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9792 469023 : TYPE_INCLUDES_FLEXARRAY (t)
9793 938046 : |= is_last_field && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x));
9794 :
9795 4324552 : if (warn_flex_array_member_not_at_end
9796 56 : && !is_last_field
9797 22 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9798 4324564 : && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x)))
9799 5 : warning_at (DECL_SOURCE_LOCATION (x),
9800 5 : OPT_Wflex_array_member_not_at_end,
9801 : "structure containing a flexible array member"
9802 : " is not at the end of another structure");
9803 :
9804 4324552 : if (DECL_NAME (x)
9805 4324552 : || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9806 : saw_named_field = true;
9807 :
9808 7956330 : if (AGGREGATE_TYPE_P (TREE_TYPE (x))
9809 4793575 : && TYPE_TYPELESS_STORAGE (TREE_TYPE (x)))
9810 418735 : TYPE_TYPELESS_STORAGE (t) = true;
9811 : }
9812 :
9813 1183237 : detect_field_duplicates (fieldlist);
9814 :
9815 : /* Now we have the nearly final fieldlist. Record it,
9816 : then lay out the structure or union (including the fields). */
9817 :
9818 1183237 : TYPE_FIELDS (t) = fieldlist;
9819 :
9820 1183237 : maybe_apply_pragma_scalar_storage_order (t);
9821 :
9822 1183237 : layout_type (t);
9823 :
9824 1183237 : if (TYPE_SIZE_UNIT (t)
9825 1183237 : && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
9826 1182594 : && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
9827 2365831 : && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
9828 1 : error ("type %qT is too large", t);
9829 :
9830 : /* Give bit-fields their proper types and rewrite the type of array fields
9831 : with scalar component if the enclosing type has reverse storage order. */
9832 5507829 : for (tree field = fieldlist; field; field = DECL_CHAIN (field))
9833 : {
9834 4324592 : if (TREE_CODE (field) == FIELD_DECL
9835 4324592 : && DECL_INITIAL (field)
9836 4377428 : && TREE_TYPE (field) != error_mark_node)
9837 : {
9838 52826 : unsigned HOST_WIDE_INT width
9839 52826 : = tree_to_uhwi (DECL_INITIAL (field));
9840 52826 : tree type = TREE_TYPE (field);
9841 52826 : if (VECTOR_TYPE_P (type))
9842 : {
9843 2 : error_at (DECL_SOURCE_LOCATION (field),
9844 : "bit-field %qD has invalid type", field);
9845 2 : type = TREE_TYPE (type);
9846 2 : TREE_TYPE (field) = type;
9847 : }
9848 52826 : if (width != TYPE_PRECISION (type))
9849 : {
9850 37894 : if (BITINT_TYPE_P (type)
9851 38174 : && width >= ((TYPE_UNSIGNED (type) || flag_isoc2y) ? 1 : 2))
9852 171 : TREE_TYPE (field)
9853 342 : = build_bitint_type (width, TYPE_UNSIGNED (type));
9854 : else
9855 37888 : TREE_TYPE (field)
9856 37888 : = c_build_bitfield_integer_type (width,
9857 37888 : TYPE_UNSIGNED (type));
9858 38059 : if (tree attr = c_hardbool_type_attr (type))
9859 281 : decl_attributes (&TREE_TYPE (field),
9860 : copy_list (attr),
9861 : 0, NULL_TREE);
9862 38059 : SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
9863 : }
9864 52826 : DECL_INITIAL (field) = NULL_TREE;
9865 : }
9866 4271766 : else if (TYPE_REVERSE_STORAGE_ORDER (t)
9867 695 : && TREE_CODE (field) == FIELD_DECL
9868 4272461 : && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
9869 : {
9870 107 : tree ftype = TREE_TYPE (field);
9871 107 : tree ctype = strip_array_types (ftype);
9872 107 : if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
9873 : {
9874 94 : tree fmain_type = TYPE_MAIN_VARIANT (ftype);
9875 94 : tree *typep = &fmain_type;
9876 96 : do {
9877 96 : *typep = build_distinct_type_copy (*typep);
9878 96 : TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
9879 96 : typep = &TREE_TYPE (*typep);
9880 96 : } while (TREE_CODE (*typep) == ARRAY_TYPE);
9881 94 : TREE_TYPE (field)
9882 188 : = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
9883 : }
9884 : }
9885 :
9886 : /* Warn on problematic type punning for storage order purposes. */
9887 4324592 : if (TREE_CODE (t) == UNION_TYPE
9888 856643 : && TREE_CODE (field) == FIELD_DECL
9889 5181235 : && AGGREGATE_TYPE_P (TREE_TYPE (field)))
9890 : {
9891 426119 : tree ftype = TREE_TYPE (field);
9892 426119 : if (TREE_CODE (ftype) == ARRAY_TYPE)
9893 313582 : ftype = strip_array_types (ftype);
9894 426119 : if (RECORD_OR_UNION_TYPE_P (ftype)
9895 426119 : && TYPE_REVERSE_STORAGE_ORDER (ftype)
9896 113031 : != TYPE_REVERSE_STORAGE_ORDER (t))
9897 1 : warning_at (DECL_SOURCE_LOCATION (field),
9898 1 : OPT_Wscalar_storage_order,
9899 : "type punning toggles scalar storage order");
9900 : }
9901 : }
9902 :
9903 : /* Now we have the truly final field list.
9904 : Store it in this type and in the variants. */
9905 :
9906 1183237 : TYPE_FIELDS (t) = fieldlist;
9907 :
9908 : /* If there are lots of fields, sort so we can look through them fast.
9909 : We arbitrarily consider 16 or more elts to be "a lot". */
9910 :
9911 1183237 : {
9912 1183237 : int len = 0;
9913 :
9914 5063536 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9915 : {
9916 3908041 : if (len > 15 || DECL_NAME (x) == NULL)
9917 : break;
9918 3880299 : len += 1;
9919 : }
9920 :
9921 1183237 : if (len > 15)
9922 : {
9923 22795 : tree *field_array;
9924 22795 : struct lang_type *space;
9925 22795 : struct sorted_fields_type *space2;
9926 :
9927 22795 : len += list_length (x);
9928 :
9929 : /* Use the same allocation policy here that make_node uses, to
9930 : ensure that this lives as long as the rest of the struct decl.
9931 : All decls in an inline function need to be saved. */
9932 :
9933 22795 : space = ((struct lang_type *)
9934 22795 : ggc_internal_cleared_alloc (c_dialect_objc ()
9935 : ? sizeof (struct lang_type)
9936 : : offsetof (struct lang_type,
9937 : info)));
9938 22795 : space2 = ((sorted_fields_type *)
9939 45590 : ggc_internal_alloc (sizeof (struct sorted_fields_type)
9940 22795 : + len * sizeof (tree)));
9941 :
9942 22795 : len = 0;
9943 22795 : space->s = space2;
9944 22795 : field_array = &space2->elts[0];
9945 797852 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9946 : {
9947 778446 : field_array[len++] = x;
9948 :
9949 : /* If there is anonymous struct or union, break out of the loop. */
9950 778446 : if (DECL_NAME (x) == NULL)
9951 : break;
9952 : }
9953 : /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
9954 22795 : if (x == NULL)
9955 : {
9956 19406 : TYPE_LANG_SPECIFIC (t) = space;
9957 19406 : TYPE_LANG_SPECIFIC (t)->s->len = len;
9958 19406 : field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
9959 19406 : qsort (field_array, len, sizeof (tree), field_decl_cmp);
9960 : }
9961 : }
9962 : }
9963 :
9964 : /* If this was supposed to be a transparent union, but we can't
9965 : make it one, warn and turn off the flag. */
9966 1183237 : if (TREE_CODE (t) == UNION_TYPE
9967 378832 : && TYPE_TRANSPARENT_AGGR (t)
9968 1183282 : && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
9969 : {
9970 13 : TYPE_TRANSPARENT_AGGR (t) = 0;
9971 13 : warning_at (loc, 0, "union cannot be made transparent");
9972 : }
9973 :
9974 1183237 : tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
9975 2397430 : for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9976 : {
9977 1214193 : TYPE_FIELDS (x) = TYPE_FIELDS (t);
9978 1214193 : TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
9979 1214193 : TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
9980 1214193 : TYPE_TYPELESS_STORAGE (x) = TYPE_TYPELESS_STORAGE (t);
9981 1214193 : C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
9982 1214193 : C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
9983 1214193 : C_TYPE_FIELDS_NON_CONSTEXPR (x) = C_TYPE_FIELDS_NON_CONSTEXPR (t);
9984 1214193 : C_TYPE_FIELDS_HAS_COUNTED_BY (x) = C_TYPE_FIELDS_HAS_COUNTED_BY (t);
9985 1214193 : C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
9986 1214193 : C_TYPE_VARIABLY_MODIFIED (x) = C_TYPE_VARIABLY_MODIFIED (t);
9987 1214193 : C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
9988 1214193 : TYPE_INCLUDES_FLEXARRAY (x) = TYPE_INCLUDES_FLEXARRAY (t);
9989 : }
9990 :
9991 : /* Check for consistency with previous definition. */
9992 1183237 : if (flag_isoc23 && NULL != enclosing_struct_parse_info)
9993 : {
9994 915285 : tree vistype = previous_tag (t);
9995 915285 : if (vistype
9996 128 : && TREE_CODE (vistype) == TREE_CODE (t)
9997 915413 : && !C_TYPE_BEING_DEFINED (vistype))
9998 : {
9999 108 : TYPE_STUB_DECL (t) = TYPE_STUB_DECL (vistype);
10000 108 : if (c_type_variably_modified_p (t))
10001 : {
10002 7 : error ("redefinition of struct or union %qT with variably "
10003 : "modified type", t);
10004 7 : if (struct_parse_info->refloc != UNKNOWN_LOCATION)
10005 7 : inform (struct_parse_info->refloc, "originally defined here");
10006 : }
10007 101 : else if (!comptypes_same_p (t, vistype))
10008 : {
10009 38 : error ("redefinition of struct or union %qT", t);
10010 38 : if (struct_parse_info->refloc != UNKNOWN_LOCATION)
10011 38 : inform (struct_parse_info->refloc, "originally defined here");
10012 : }
10013 : }
10014 : }
10015 :
10016 1183237 : C_TYPE_BEING_DEFINED (t) = 0;
10017 :
10018 1183237 : if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (t)))
10019 674 : for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
10020 353 : c_fixup_may_alias (x);
10021 :
10022 : /* Set type canonical based on equivalence class. */
10023 1183237 : if (flag_isoc23 && !C_TYPE_VARIABLE_SIZE (t))
10024 : {
10025 950753 : if (c_struct_htab == NULL)
10026 35702 : c_struct_htab = hash_table<c_struct_hasher>::create_ggc (61);
10027 :
10028 950753 : hashval_t hash = c_struct_hasher::hash (t);
10029 :
10030 950753 : gcc_checking_assert (TYPE_STRUCTURAL_EQUALITY_P (t));
10031 950753 : gcc_checking_assert (!TYPE_NAME (t)
10032 : || TREE_CODE (TYPE_NAME (t)) == IDENTIFIER_NODE);
10033 :
10034 950753 : tree *e = c_struct_htab->find_slot_with_hash (t, hash, INSERT);
10035 950753 : if (*e)
10036 62994 : TYPE_CANONICAL (t) = TYPE_CANONICAL (*e);
10037 : else
10038 : {
10039 887759 : TYPE_CANONICAL (t) = c_type_canonical (t);
10040 887759 : *e = t;
10041 : }
10042 950753 : c_update_type_canonical (t);
10043 : }
10044 :
10045 : /* Update type location to the one of the definition, instead of e.g.
10046 : a forward declaration. */
10047 1183237 : if (TYPE_STUB_DECL (t))
10048 1183237 : DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
10049 :
10050 : /* Finish debugging output for this type. */
10051 1183237 : rest_of_type_compilation (t, toplevel);
10052 :
10053 1183237 : finish_incomplete_vars (incomplete_vars, toplevel);
10054 :
10055 :
10056 1183237 : if (c_type_variably_modified_p (t))
10057 : {
10058 716 : c_update_variably_modified (t);
10059 : /* Make sure a DECL_EXPR is created for structs with VLA members.
10060 : Because we do not know the context, we always pass expr
10061 : to force creation of a BIND_EXPR which is required in some
10062 : contexts. */
10063 716 : add_decl_expr (loc, t, expr, false);
10064 : }
10065 :
10066 1183237 : if (warn_cxx_compat)
10067 577 : warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
10068 :
10069 1183237 : if (NULL != enclosing_struct_parse_info)
10070 : {
10071 1142546 : delete struct_parse_info;
10072 :
10073 1142546 : struct_parse_info = enclosing_struct_parse_info;
10074 :
10075 : /* If this struct is defined inside a struct, add it to
10076 : struct_types. */
10077 1142546 : if (warn_cxx_compat
10078 : && struct_parse_info != NULL
10079 350 : && !in_sizeof && !in_typeof && !in_alignof && !in_countof)
10080 345 : struct_parse_info->struct_types.safe_push (t);
10081 : }
10082 :
10083 : /* Only when the enclosing struct/union type is not anonymous, do more
10084 : verification on the fields with counted_by attributes. */
10085 1183237 : if (C_TYPE_FIELDS_HAS_COUNTED_BY (t) && c_type_tag (t) != NULL_TREE)
10086 295 : verify_counted_by_attribute (t, t);
10087 :
10088 1183237 : return t;
10089 : }
10090 :
10091 : static struct {
10092 : gt_pointer_operator new_value;
10093 : void *cookie;
10094 : } resort_data;
10095 :
10096 : /* This routine compares two fields like field_decl_cmp but using the
10097 : pointer operator in resort_data. */
10098 :
10099 : static int
10100 5445 : resort_field_decl_cmp (const void *x_p, const void *y_p)
10101 : {
10102 5445 : const tree *const x = (const tree *) x_p;
10103 5445 : const tree *const y = (const tree *) y_p;
10104 :
10105 5445 : if (DECL_NAME (*x) == DECL_NAME (*y))
10106 : /* A nontype is "greater" than a type. */
10107 0 : return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10108 5445 : if (DECL_NAME (*x) == NULL_TREE)
10109 : return -1;
10110 5445 : if (DECL_NAME (*y) == NULL_TREE)
10111 : return 1;
10112 5445 : {
10113 5445 : tree d1 = DECL_NAME (*x);
10114 5445 : tree d2 = DECL_NAME (*y);
10115 5445 : resort_data.new_value (&d1, &d1, resort_data.cookie);
10116 5445 : resort_data.new_value (&d2, &d2, resort_data.cookie);
10117 5445 : if (d1 < d2)
10118 2786 : return -1;
10119 : }
10120 2659 : return 1;
10121 : }
10122 :
10123 : /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10124 :
10125 : void
10126 6 : resort_sorted_fields (void *obj,
10127 : void * ARG_UNUSED (orig_obj),
10128 : gt_pointer_operator new_value,
10129 : void *cookie)
10130 : {
10131 6 : struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
10132 6 : resort_data.new_value = new_value;
10133 6 : resort_data.cookie = cookie;
10134 6 : qsort (&sf->elts[0], sf->len, sizeof (tree),
10135 : resort_field_decl_cmp);
10136 6 : }
10137 :
10138 : /* Lay out the type T, and its element type, and so on. */
10139 :
10140 : static void
10141 0 : layout_array_type (tree t)
10142 : {
10143 0 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
10144 0 : layout_array_type (TREE_TYPE (t));
10145 0 : layout_type (t);
10146 0 : }
10147 :
10148 : /* Begin compiling the definition of an enumeration type.
10149 : NAME is its name (or null if anonymous).
10150 : LOC is the enum's location.
10151 : FIXED_UNDERLYING_TYPE is the (C23) underlying type specified in the
10152 : definition.
10153 : Returns the type object, as yet incomplete.
10154 : Also records info about it so that build_enumerator
10155 : may be used to declare the individual values as they are read. */
10156 :
10157 : tree
10158 183298 : start_enum (location_t loc, struct c_enum_contents *the_enum, tree name,
10159 : tree fixed_underlying_type, bool potential_nesting_p)
10160 : {
10161 183298 : tree enumtype = NULL_TREE;
10162 183298 : location_t enumloc = UNKNOWN_LOCATION;
10163 :
10164 : /* If this is the real definition for a previous forward reference,
10165 : fill in the contents in the same object that used to be the
10166 : forward reference. */
10167 :
10168 183298 : if (name != NULL_TREE)
10169 73268 : enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
10170 :
10171 73268 : if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
10172 : {
10173 : /* If the type is currently being defined or if we have seen an
10174 : incomplete version which is now complete, this is a nested
10175 : redefinition. The later happens if the redefinition occurs
10176 : inside the enum specifier itself. */
10177 199 : if (C_TYPE_BEING_DEFINED (enumtype)
10178 199 : || (potential_nesting_p && TYPE_VALUES (enumtype) != NULL_TREE))
10179 9 : error_at (loc, "nested redefinition of %<enum %E%>", name);
10180 :
10181 : /* For C23 we allow redefinitions. We set to zero and check for
10182 : consistency later. */
10183 381 : if (flag_isoc23 && TYPE_VALUES (enumtype) != NULL_TREE)
10184 : enumtype = NULL_TREE;
10185 : }
10186 :
10187 110203 : if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
10188 : {
10189 183127 : enumtype = make_node (ENUMERAL_TYPE);
10190 183127 : TYPE_SIZE (enumtype) = NULL_TREE;
10191 183127 : SET_TYPE_STRUCTURAL_EQUALITY (enumtype);
10192 183127 : pushtag (loc, name, enumtype);
10193 183127 : if (fixed_underlying_type != NULL_TREE)
10194 : {
10195 : /* For an enum definition with a fixed underlying type, the
10196 : type is complete during the definition and the
10197 : enumeration constants have that type. If there was a
10198 : tag, the type was completed in c_parser_enum_specifier.
10199 : If not, it must be completed here. */
10200 12 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = true;
10201 12 : TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (fixed_underlying_type);
10202 12 : TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (fixed_underlying_type);
10203 12 : TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (fixed_underlying_type);
10204 12 : SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (fixed_underlying_type));
10205 12 : TYPE_SIZE (enumtype) = NULL_TREE;
10206 12 : TYPE_PRECISION (enumtype) = TYPE_PRECISION (fixed_underlying_type);
10207 12 : ENUM_UNDERLYING_TYPE (enumtype) = fixed_underlying_type;
10208 12 : TYPE_CANONICAL (enumtype) = TYPE_CANONICAL (fixed_underlying_type);
10209 12 : c_update_type_canonical (enumtype);
10210 12 : layout_type (enumtype);
10211 : }
10212 : }
10213 : /* Update type location to the one of the definition, instead of e.g.
10214 : a forward declaration. */
10215 171 : else if (TYPE_STUB_DECL (enumtype))
10216 : {
10217 171 : enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
10218 171 : DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
10219 : }
10220 :
10221 183298 : C_TYPE_BEING_DEFINED (enumtype) = 1;
10222 :
10223 183298 : if (TYPE_VALUES (enumtype) != NULL_TREE)
10224 : {
10225 : /* This enum is a named one that has been declared already. */
10226 6 : auto_diagnostic_group d;
10227 6 : error_at (loc, "redeclaration of %<enum %E%>", name);
10228 6 : if (enumloc != UNKNOWN_LOCATION)
10229 6 : inform (enumloc, "originally defined here");
10230 :
10231 : /* Completely replace its old definition.
10232 : The old enumerators remain defined, however. */
10233 6 : TYPE_VALUES (enumtype) = NULL_TREE;
10234 6 : }
10235 :
10236 183298 : if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype)
10237 183298 : && fixed_underlying_type == NULL_TREE)
10238 : {
10239 3 : error_at (loc, "%<enum%> declared with but defined without "
10240 : "fixed underlying type");
10241 3 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = false;
10242 : }
10243 :
10244 183298 : the_enum->enum_next_value = integer_zero_node;
10245 183298 : the_enum->enum_type = enumtype;
10246 183298 : the_enum->enum_overflow = 0;
10247 :
10248 183298 : if (flag_short_enums && !ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
10249 106 : for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
10250 53 : TYPE_PACKED (v) = 1;
10251 :
10252 : /* FIXME: This will issue a warning for a use of a type defined
10253 : within sizeof in a statement expr. This is not terribly serious
10254 : as C++ doesn't permit statement exprs within sizeof anyhow. */
10255 183298 : if (warn_cxx_compat
10256 136 : && (in_sizeof || in_typeof || in_alignof || in_countof))
10257 0 : warning_at (loc, OPT_Wc___compat,
10258 : "defining type in %qs expression is invalid in C++",
10259 : (in_sizeof ? "sizeof"
10260 0 : : in_typeof ? "typeof"
10261 0 : : in_alignof ? "alignof"
10262 : : "_Countof"));
10263 :
10264 183298 : if (in_underspecified_init)
10265 6 : error_at (loc, "%qT defined in underspecified object initializer",
10266 : enumtype);
10267 :
10268 183298 : return enumtype;
10269 : }
10270 :
10271 : /* After processing and defining all the values of an enumeration type,
10272 : install their decls in the enumeration type and finish it off.
10273 : ENUMTYPE is the type object, VALUES a list of decl-value pairs,
10274 : and ATTRIBUTES are the specified attributes.
10275 : Returns ENUMTYPE. */
10276 :
10277 : tree
10278 183298 : finish_enum (tree enumtype, tree values, tree attributes)
10279 : {
10280 183298 : tree pair, tem;
10281 183298 : tree minnode = NULL_TREE, maxnode = NULL_TREE;
10282 183298 : int precision;
10283 183298 : signop sign;
10284 183298 : bool toplevel = (file_scope == current_scope);
10285 183298 : struct lang_type *lt;
10286 :
10287 183298 : decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10288 :
10289 : /* Calculate the maximum value of any enumerator in this type. */
10290 :
10291 183298 : if (values == error_mark_node)
10292 26 : minnode = maxnode = integer_zero_node;
10293 : else
10294 : {
10295 183272 : minnode = maxnode = TREE_VALUE (values);
10296 5749416 : for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
10297 : {
10298 5566144 : tree value = TREE_VALUE (pair);
10299 5566144 : if (tree_int_cst_lt (maxnode, value))
10300 5416079 : maxnode = value;
10301 5566144 : if (tree_int_cst_lt (value, minnode))
10302 65616 : minnode = value;
10303 : }
10304 : }
10305 :
10306 : /* Construct the final type of this enumeration. It is the same
10307 : as one of the integral types - the narrowest one that fits, except
10308 : that normally we only go as narrow as int - and signed iff any of
10309 : the values are negative. */
10310 183298 : sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
10311 183298 : precision = MAX (tree_int_cst_min_precision (minnode, sign),
10312 : tree_int_cst_min_precision (maxnode, sign));
10313 :
10314 183298 : bool wider_than_int =
10315 183298 : (tree_int_cst_lt (minnode, TYPE_MIN_VALUE (integer_type_node))
10316 183298 : || tree_int_cst_lt (TYPE_MAX_VALUE (integer_type_node), maxnode));
10317 :
10318 :
10319 183298 : if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
10320 : {
10321 : /* If the precision of the type was specified with an attribute and it
10322 : was too small, give an error. Otherwise, use it. */
10323 183158 : if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
10324 : {
10325 23 : if (precision > TYPE_PRECISION (enumtype))
10326 : {
10327 6 : TYPE_PRECISION (enumtype) = 0;
10328 6 : error ("specified mode too small for enumerated values");
10329 : }
10330 : else
10331 17 : precision = TYPE_PRECISION (enumtype);
10332 : }
10333 : else
10334 183135 : TYPE_PRECISION (enumtype) = 0;
10335 :
10336 183158 : if (TYPE_PACKED (enumtype)
10337 183079 : || precision > TYPE_PRECISION (integer_type_node)
10338 363141 : || TYPE_PRECISION (enumtype))
10339 : {
10340 3302 : tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
10341 3187 : if (tem == NULL)
10342 : {
10343 : /* This should only occur when both signed and unsigned
10344 : values of maximum precision occur among the
10345 : enumerators. */
10346 3 : pedwarn (input_location, 0,
10347 : "enumeration values exceed range of largest integer");
10348 3 : tem = widest_integer_literal_type_node;
10349 : }
10350 3184 : else if (precision > TYPE_PRECISION (intmax_type_node)
10351 4 : && !tree_int_cst_lt (minnode,
10352 4 : TYPE_MIN_VALUE (intmax_type_node))
10353 3187 : && !tree_int_cst_lt (TYPE_MAX_VALUE (uintmax_type_node),
10354 : maxnode))
10355 2 : pedwarn (input_location, OPT_Wpedantic,
10356 : "enumeration values exceed range of %qs",
10357 : sign == UNSIGNED ? "uintmax_t" : "intmax_t");
10358 : }
10359 : else
10360 179971 : tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
10361 :
10362 183158 : TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
10363 183158 : TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
10364 183158 : TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
10365 183158 : SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
10366 183158 : TYPE_SIZE (enumtype) = NULL_TREE;
10367 183158 : TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
10368 183158 : ENUM_UNDERLYING_TYPE (enumtype) =
10369 183158 : c_common_type_for_size (TYPE_PRECISION (tem), TYPE_UNSIGNED (tem));
10370 :
10371 549474 : TYPE_CANONICAL (enumtype) =
10372 183158 : TYPE_CANONICAL (ENUM_UNDERLYING_TYPE (enumtype));
10373 183158 : c_update_type_canonical (enumtype);
10374 :
10375 183158 : layout_type (enumtype);
10376 : }
10377 :
10378 183298 : if (values != error_mark_node)
10379 : {
10380 : /* Change the type of the enumerators to be the enum type. We
10381 : need to do this irrespective of the size of the enum, for
10382 : proper type checking. Replace the DECL_INITIALs of the
10383 : enumerators, and the value slots of the list, with copies
10384 : that have the enum type; they cannot be modified in place
10385 : because they may be shared (e.g. integer_zero_node) Finally,
10386 : change the purpose slots to point to the names of the decls. */
10387 5932688 : for (pair = values; pair; pair = TREE_CHAIN (pair))
10388 : {
10389 5749416 : tree enu = TREE_PURPOSE (pair);
10390 5749416 : tree ini = DECL_INITIAL (enu);
10391 :
10392 5749416 : TREE_TYPE (enu) = enumtype;
10393 :
10394 : /* Before C23, the ISO C Standard mandates enumerators to
10395 : have type int, even though the underlying type of an enum
10396 : type is unspecified. However, C23 allows enumerators of
10397 : any integer type, and if an enumeration has any
10398 : enumerators wider than int, all enumerators have the
10399 : enumerated type after it is parsed. Any enumerators that
10400 : fit in int are given type int in build_enumerator (which
10401 : is the correct type while the enumeration is being
10402 : parsed), so no conversions are needed here if all
10403 : enumerators fit in int. If the enum has a fixed
10404 : underlying type, the correct type was also given in
10405 : build_enumerator. */
10406 5749416 : if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) && wider_than_int)
10407 34887 : ini = convert (enumtype, ini);
10408 :
10409 5749416 : DECL_INITIAL (enu) = ini;
10410 5749416 : TREE_PURPOSE (pair) = DECL_NAME (enu);
10411 : /* To match the C++ FE, store the CONST_DECL rather than just its
10412 : value. */
10413 5749416 : TREE_VALUE (pair) = enu;
10414 : }
10415 :
10416 183272 : TYPE_VALUES (enumtype) = values;
10417 : }
10418 :
10419 : /* Record the min/max values so that we can warn about bit-field
10420 : enumerations that are too small for the values. */
10421 183298 : lt = ((struct lang_type *)
10422 183298 : ggc_internal_cleared_alloc (c_dialect_objc ()
10423 : ? sizeof (struct lang_type)
10424 : : offsetof (struct lang_type, info)));
10425 183298 : lt->enum_min = minnode;
10426 183298 : lt->enum_max = maxnode;
10427 183298 : TYPE_LANG_SPECIFIC (enumtype) = lt;
10428 :
10429 : /* Fix up all variant types of this enum type. */
10430 183298 : tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
10431 366606 : for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
10432 : {
10433 183308 : C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
10434 183308 : if (tem == enumtype)
10435 183298 : continue;
10436 10 : TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
10437 10 : TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
10438 10 : TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
10439 10 : TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
10440 10 : TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
10441 10 : SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
10442 10 : TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
10443 10 : SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
10444 10 : TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
10445 10 : TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
10446 10 : TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
10447 10 : ENUM_UNDERLYING_TYPE (tem) = ENUM_UNDERLYING_TYPE (enumtype);
10448 10 : TYPE_PACKED (tem) = TYPE_PACKED (enumtype);
10449 : }
10450 :
10451 : /* Finish debugging output for this type. */
10452 183298 : rest_of_type_compilation (enumtype, toplevel);
10453 :
10454 183298 : finish_incomplete_vars (incomplete_vars, toplevel);
10455 :
10456 : /* If this enum is defined inside a struct, add it to
10457 : struct_types. */
10458 183298 : if (warn_cxx_compat
10459 136 : && struct_parse_info != NULL
10460 46 : && !in_sizeof && !in_typeof && !in_alignof && !in_countof)
10461 46 : struct_parse_info->struct_types.safe_push (enumtype);
10462 :
10463 : /* Check for consistency with previous definition */
10464 183298 : if (flag_isoc23)
10465 : {
10466 145103 : tree vistype = previous_tag (enumtype);
10467 145103 : if (vistype
10468 33 : && TREE_CODE (vistype) == TREE_CODE (enumtype)
10469 145132 : && !C_TYPE_BEING_DEFINED (vistype))
10470 : {
10471 29 : TYPE_STUB_DECL (enumtype) = TYPE_STUB_DECL (vistype);
10472 29 : if (!comptypes_same_p (enumtype, vistype))
10473 3 : error("conflicting redefinition of enum %qT", enumtype);
10474 : }
10475 : }
10476 :
10477 183298 : C_TYPE_BEING_DEFINED (enumtype) = 0;
10478 :
10479 183298 : return enumtype;
10480 : }
10481 :
10482 : /* Build and install a CONST_DECL for one value of the
10483 : current enumeration type (one that was begun with start_enum).
10484 : DECL_LOC is the location of the enumerator.
10485 : LOC is the location of the '=' operator if any, DECL_LOC otherwise.
10486 : Return a tree-list containing the CONST_DECL and its value.
10487 : Assignment of sequential values by default is handled here. */
10488 :
10489 : tree
10490 5749433 : build_enumerator (location_t decl_loc, location_t loc,
10491 : struct c_enum_contents *the_enum, tree name, tree value)
10492 : {
10493 5749433 : tree decl;
10494 5749433 : tree old_decl;
10495 :
10496 : /* Validate and default VALUE. */
10497 :
10498 5749433 : if (value != NULL_TREE)
10499 : {
10500 : /* Don't issue more errors for error_mark_node (i.e. an
10501 : undeclared identifier) - just ignore the value expression. */
10502 3578394 : if (value == error_mark_node)
10503 : value = NULL_TREE;
10504 3577729 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
10505 : {
10506 6 : error_at (loc, "enumerator value for %qE is not an integer constant",
10507 : name);
10508 6 : value = NULL_TREE;
10509 : }
10510 : else
10511 : {
10512 3577723 : if (TREE_CODE (value) != INTEGER_CST)
10513 : {
10514 97 : value = c_fully_fold (value, false, NULL);
10515 97 : if (TREE_CODE (value) == INTEGER_CST)
10516 48 : pedwarn (loc, OPT_Wpedantic,
10517 : "enumerator value for %qE is not an integer "
10518 : "constant expression", name);
10519 : }
10520 3577723 : if (TREE_CODE (value) != INTEGER_CST)
10521 : {
10522 49 : error ("enumerator value for %qE is not an integer constant",
10523 : name);
10524 49 : value = NULL_TREE;
10525 : }
10526 : else
10527 : {
10528 3577674 : value = default_conversion (value);
10529 3577674 : constant_expression_warning (value);
10530 : }
10531 : }
10532 : }
10533 :
10534 : /* Default based on previous value. */
10535 : /* It should no longer be possible to have NON_LVALUE_EXPR
10536 : in the default. */
10537 3577729 : if (value == NULL_TREE)
10538 : {
10539 2171759 : value = the_enum->enum_next_value;
10540 2171759 : if (the_enum->enum_overflow)
10541 8 : error_at (loc, "overflow in enumeration values");
10542 : }
10543 5749433 : if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10544 : {
10545 : /* Enumeration constants must fit in the fixed underlying type. */
10546 205 : if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (the_enum->enum_type)))
10547 4 : error_at (loc,
10548 : "enumerator value outside the range of underlying type");
10549 : /* Enumeration constants for an enum with fixed underlying type
10550 : have the enum type, both inside and outside the
10551 : definition. */
10552 205 : value = convert (the_enum->enum_type, value);
10553 : }
10554 5749228 : else if (flag_isoc23
10555 5331809 : && (old_decl = lookup_name_in_scope (name, current_scope))
10556 35 : && old_decl != error_mark_node
10557 35 : && TREE_TYPE (old_decl)
10558 35 : && TREE_TYPE (TREE_TYPE (old_decl))
10559 5749262 : && TREE_CODE (old_decl) == CONST_DECL)
10560 : {
10561 : /* Enumeration constants in a redeclaration have the previous type. */
10562 34 : tree previous_type = TREE_TYPE (DECL_INITIAL (old_decl));
10563 34 : if (!int_fits_type_p (value, previous_type))
10564 : {
10565 2 : error_at (loc, "value of redeclared enumerator outside the range "
10566 : "of %qT", previous_type);
10567 2 : locate_old_decl (old_decl);
10568 : }
10569 34 : value = convert (previous_type, value);
10570 : }
10571 : else
10572 : {
10573 : /* Even though the underlying type of an enum is unspecified, the
10574 : type of enumeration constants is explicitly defined as int
10575 : (6.4.4.3/2 in the C99 Standard). C23 allows any integer type, and
10576 : GCC allows such types for older standards as an extension. */
10577 5749194 : bool warned_range = false;
10578 5749194 : if (!int_fits_type_p (value,
10579 5749194 : (TYPE_UNSIGNED (TREE_TYPE (value))
10580 : ? uintmax_type_node
10581 : : intmax_type_node)))
10582 : /* GCC does not consider its types larger than intmax_t to be
10583 : extended integer types (although C23 would permit such types to
10584 : be considered extended integer types if all the features
10585 : required by <stdint.h> and <inttypes.h> macros, such as support
10586 : for integer constants and I/O, were present), so diagnose if
10587 : such a wider type is used. (If the wider type arose from a
10588 : constant of such a type, that will also have been diagnosed,
10589 : but this is the only diagnostic in the case where it arises
10590 : from choosing a wider type automatically when adding 1
10591 : overflows.) */
10592 11 : warned_range = pedwarn (loc, OPT_Wpedantic,
10593 : "enumerator value outside the range of %qs",
10594 11 : TYPE_UNSIGNED (TREE_TYPE (value))
10595 : ? "uintmax_t"
10596 : : "intmax_t");
10597 5749194 : if (!warned_range && !int_fits_type_p (value, integer_type_node))
10598 5332 : pedwarn_c11 (loc, OPT_Wpedantic,
10599 : "ISO C restricts enumerator values to range of %<int%> "
10600 : "before C23");
10601 :
10602 : /* The ISO C Standard mandates enumerators to have type int before
10603 : C23, even though the underlying type of an enum type is
10604 : unspecified. C23 allows enumerators of any integer type. During
10605 : the parsing of the enumeration, C23 specifies that constants
10606 : representable in int have type int, constants not representable
10607 : in int have the type of the given expression if any, and
10608 : constants not representable in int and derived by adding 1 to the
10609 : previous constant have the type of that constant unless the
10610 : addition would overflow or wraparound, in which case a wider type
10611 : of the same signedness is chosen automatically; after the
10612 : enumeration is parsed, all the constants have the type of the
10613 : enumeration if any do not fit in int. */
10614 5749194 : if (int_fits_type_p (value, integer_type_node))
10615 5743858 : value = convert (integer_type_node, value);
10616 : }
10617 :
10618 : /* Set basis for default for next value. */
10619 5749433 : if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10620 : {
10621 205 : tree underlying_type = ENUM_UNDERLYING_TYPE (the_enum->enum_type);
10622 205 : if (TREE_CODE (underlying_type) == BOOLEAN_TYPE)
10623 : /* A value of 2 following a value of 1 overflows bool, but we
10624 : cannot carry out addition directly on bool without
10625 : promotion, and converting the result of arithmetic in a
10626 : wider type back to bool would not produce the right result
10627 : for this overflow check. */
10628 61 : the_enum->enum_next_value = invert_truthvalue_loc (loc, value);
10629 : else
10630 144 : the_enum->enum_next_value
10631 144 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10632 : PLUS_EXPR, convert (underlying_type, value),
10633 : convert (underlying_type, integer_one_node),
10634 : false);
10635 : }
10636 : else
10637 : {
10638 : /* In a redeclaration the type can already be the enumeral type. */
10639 5749228 : if (TREE_CODE (TREE_TYPE (value)) == ENUMERAL_TYPE)
10640 12 : value = convert (ENUM_UNDERLYING_TYPE (TREE_TYPE (value)), value);
10641 5749228 : the_enum->enum_next_value
10642 5749228 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10643 : PLUS_EXPR, value, integer_one_node, false);
10644 : }
10645 5749433 : the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
10646 5749433 : if (the_enum->enum_overflow
10647 5749433 : && !ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10648 : {
10649 : /* Choose a wider type with the same signedness if
10650 : available. */
10651 4515 : int prec = TYPE_PRECISION (TREE_TYPE (value)) + 1;
10652 4515 : bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (value));
10653 4515 : tree new_type = (unsignedp
10654 4515 : ? long_unsigned_type_node
10655 : : long_integer_type_node);
10656 4515 : if (prec > TYPE_PRECISION (new_type))
10657 3565 : new_type = (unsignedp
10658 3565 : ? long_long_unsigned_type_node
10659 : : long_long_integer_type_node);
10660 4515 : if (prec > TYPE_PRECISION (new_type))
10661 2979 : new_type = (unsignedp
10662 2979 : ? widest_unsigned_literal_type_node
10663 : : widest_integer_literal_type_node);
10664 4515 : if (prec <= TYPE_PRECISION (new_type))
10665 : {
10666 4510 : the_enum->enum_overflow = false;
10667 4510 : the_enum->enum_next_value
10668 4510 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10669 : PLUS_EXPR, convert (new_type, value),
10670 : integer_one_node, false);
10671 4510 : gcc_assert (!tree_int_cst_lt (the_enum->enum_next_value, value));
10672 : }
10673 : }
10674 :
10675 : /* Now create a declaration for the enum value name. */
10676 :
10677 5749433 : decl = build_decl (decl_loc, CONST_DECL, name, TREE_TYPE (value));
10678 5749433 : DECL_INITIAL (decl) = value;
10679 5749433 : DECL_CONTEXT (decl) = the_enum->enum_type;
10680 5749433 : pushdecl (decl);
10681 :
10682 5749433 : return tree_cons (decl, value, NULL_TREE);
10683 : }
10684 :
10685 : /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
10686 :
10687 : tree
10688 0 : c_simulate_enum_decl (location_t loc, const char *name,
10689 : vec<string_int_pair> *values_ptr)
10690 : {
10691 0 : location_t saved_loc = input_location;
10692 0 : input_location = loc;
10693 :
10694 0 : struct c_enum_contents the_enum;
10695 0 : tree enumtype = start_enum (loc, &the_enum, get_identifier (name),
10696 : NULL_TREE, false);
10697 :
10698 0 : tree value_chain = NULL_TREE;
10699 0 : string_int_pair *value;
10700 0 : vec<string_int_pair> values = *values_ptr;
10701 0 : unsigned int i;
10702 0 : FOR_EACH_VEC_ELT (values, i, value)
10703 : {
10704 0 : tree decl = build_enumerator (loc, loc, &the_enum,
10705 : get_identifier (value->first),
10706 : build_int_cst (integer_type_node,
10707 0 : value->second));
10708 0 : TREE_CHAIN (decl) = value_chain;
10709 0 : value_chain = decl;
10710 : }
10711 :
10712 0 : finish_enum (enumtype, nreverse (value_chain), NULL_TREE);
10713 :
10714 0 : input_location = saved_loc;
10715 0 : return enumtype;
10716 : }
10717 :
10718 : /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
10719 :
10720 : tree
10721 0 : c_simulate_record_decl (location_t loc, const char *name,
10722 : array_slice<const tree> fields)
10723 : {
10724 0 : location_t saved_loc = input_location;
10725 0 : input_location = loc;
10726 :
10727 0 : class c_struct_parse_info *struct_info;
10728 0 : tree ident = get_identifier (name);
10729 0 : tree type = start_struct (loc, RECORD_TYPE, ident, &struct_info);
10730 :
10731 0 : for (unsigned int i = 0; i < fields.size (); ++i)
10732 : {
10733 0 : DECL_FIELD_CONTEXT (fields[i]) = type;
10734 0 : if (i > 0)
10735 0 : DECL_CHAIN (fields[i - 1]) = fields[i];
10736 : }
10737 :
10738 0 : finish_struct (loc, type, fields[0], NULL_TREE, struct_info);
10739 :
10740 0 : tree decl = build_decl (loc, TYPE_DECL, ident, type);
10741 0 : set_underlying_type (decl);
10742 0 : lang_hooks.decls.pushdecl (decl);
10743 :
10744 0 : input_location = saved_loc;
10745 0 : return type;
10746 : }
10747 :
10748 : /* Create the FUNCTION_DECL for a function definition.
10749 : DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
10750 : the declaration; they describe the function's name and the type it returns,
10751 : but twisted together in a fashion that parallels the syntax of C.
10752 :
10753 : This function creates a binding context for the function body
10754 : as well as setting up the FUNCTION_DECL in current_function_decl.
10755 :
10756 : Returns true on success. If the DECLARATOR is not suitable for a function
10757 : (it defines a datum instead), we return false to report a parse error. */
10758 :
10759 : bool
10760 36350955 : start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
10761 : tree attributes)
10762 : {
10763 36350955 : tree decl1, old_decl;
10764 36350955 : tree restype, resdecl;
10765 36350955 : location_t loc;
10766 36350955 : location_t result_loc;
10767 36350955 : tree expr = NULL;
10768 :
10769 36350955 : current_function_returns_value = 0; /* Assume, until we see it does. */
10770 36350955 : current_function_returns_null = 0;
10771 36350955 : current_function_returns_abnormally = 0;
10772 36350955 : warn_about_return_type = 0;
10773 36350955 : c_switch_stack = NULL;
10774 :
10775 : /* Indicate no valid break/continue context. */
10776 36350955 : in_statement = 0;
10777 :
10778 36350955 : decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
10779 : &attributes, &expr, NULL, DEPRECATED_NORMAL);
10780 36350955 : invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
10781 :
10782 : /* If the declarator is not suitable for a function definition,
10783 : cause a syntax error. */
10784 36350955 : if (decl1 == NULL_TREE
10785 36350924 : || TREE_CODE (decl1) != FUNCTION_DECL)
10786 : return false;
10787 :
10788 : /* Nested functions may have variably modified (return) type.
10789 : Make sure the size expression is evaluated at this point. */
10790 36350917 : if (expr && !current_scope->parm_flag)
10791 11 : add_stmt (fold_convert (void_type_node, expr));
10792 :
10793 36350917 : loc = DECL_SOURCE_LOCATION (decl1);
10794 :
10795 : /* A nested function is not global. */
10796 36350917 : if (current_function_decl != NULL_TREE)
10797 1598 : TREE_PUBLIC (decl1) = 0;
10798 :
10799 36350917 : c_decl_attributes (&decl1, attributes, 0);
10800 :
10801 36350917 : if (DECL_DECLARED_INLINE_P (decl1)
10802 35657836 : && DECL_UNINLINABLE (decl1)
10803 36350928 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
10804 : {
10805 3 : auto_urlify_attributes sentinel;
10806 3 : warning_at (loc, OPT_Wattributes,
10807 : "inline function %qD given attribute %qs",
10808 : decl1, "noinline");
10809 3 : }
10810 :
10811 : /* Handle gnu_inline attribute. */
10812 36350917 : if (declspecs->inline_p
10813 35657839 : && !flag_gnu89_inline
10814 35623728 : && TREE_CODE (decl1) == FUNCTION_DECL
10815 71974645 : && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
10816 159653 : || current_function_decl))
10817 : {
10818 35464147 : if (declspecs->storage_class != csc_static)
10819 35464141 : DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
10820 : }
10821 :
10822 36350917 : announce_function (decl1);
10823 :
10824 36350917 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
10825 : {
10826 1 : error_at (loc, "return type is an incomplete type");
10827 : /* Make it return void instead. */
10828 1 : TREE_TYPE (decl1)
10829 2 : = c_build_function_type (void_type_node,
10830 1 : TYPE_ARG_TYPES (TREE_TYPE (decl1)),
10831 1 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl1)));
10832 : }
10833 :
10834 36350917 : if (warn_about_return_type)
10835 1404 : permerror_opt (loc, flag_isoc99 ? OPT_Wimplicit_int
10836 0 : : (warn_return_type > 0 ? OPT_Wreturn_type
10837 : : OPT_Wimplicit_int),
10838 : "return type defaults to %<int%>");
10839 :
10840 : /* Make the init_value nonzero so pushdecl knows this is not tentative.
10841 : error_mark_node is replaced below (in pop_scope) with the BLOCK. */
10842 36350917 : DECL_INITIAL (decl1) = error_mark_node;
10843 :
10844 : /* If this definition isn't a prototype and we had a prototype declaration
10845 : before, copy the arg type info from that prototype. */
10846 36350917 : old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
10847 36350917 : if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
10848 36120262 : old_decl = NULL_TREE;
10849 :
10850 36350917 : current_function_prototype_locus = UNKNOWN_LOCATION;
10851 36350917 : current_function_prototype_built_in = false;
10852 36350917 : current_function_prototype_arg_types = NULL_TREE;
10853 36350917 : tree newtype = TREE_TYPE (decl1);
10854 36350917 : tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype;
10855 36350917 : if (!prototype_p (newtype))
10856 : {
10857 13166 : tree oldrt = TREE_TYPE (oldtype);
10858 13166 : tree newrt = TREE_TYPE (newtype);
10859 13166 : if (old_decl != NULL_TREE
10860 317 : && TREE_CODE (oldtype) == FUNCTION_TYPE
10861 317 : && comptypes (oldrt, newrt)
10862 13464 : && !TYPE_NO_NAMED_ARGS_STDARG_P (oldtype))
10863 : {
10864 297 : if (stdarg_p (oldtype))
10865 : {
10866 1 : auto_diagnostic_group d;
10867 1 : warning_at (loc, 0, "%q+D defined as variadic function "
10868 : "without prototype", decl1);
10869 1 : locate_old_decl (old_decl);
10870 1 : }
10871 297 : TREE_TYPE (decl1) = composite_type (oldtype, newtype);
10872 297 : current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
10873 297 : current_function_prototype_built_in
10874 297 : = C_DECL_BUILTIN_PROTOTYPE (old_decl);
10875 297 : current_function_prototype_arg_types
10876 297 : = TYPE_ARG_TYPES (newtype);
10877 : }
10878 13166 : if (TREE_PUBLIC (decl1))
10879 : {
10880 : /* If there is an external prototype declaration of this
10881 : function, record its location but do not copy information
10882 : to this decl. This may be an invisible declaration
10883 : (built-in or in a scope which has finished) or simply
10884 : have more refined argument types than any declaration
10885 : found above. */
10886 12751 : struct c_binding *b;
10887 13027 : for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
10888 713 : if (B_IN_SCOPE (b, external_scope))
10889 : break;
10890 12751 : if (b)
10891 : {
10892 437 : tree ext_decl, ext_type;
10893 437 : ext_decl = b->decl;
10894 437 : ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
10895 437 : if (TREE_CODE (ext_type) == FUNCTION_TYPE
10896 874 : && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
10897 437 : TREE_TYPE (ext_type)))
10898 : {
10899 417 : current_function_prototype_locus
10900 417 : = DECL_SOURCE_LOCATION (ext_decl);
10901 417 : current_function_prototype_built_in
10902 417 : = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
10903 417 : current_function_prototype_arg_types
10904 417 : = TYPE_ARG_TYPES (ext_type);
10905 : }
10906 : }
10907 : }
10908 : }
10909 :
10910 : /* Optionally warn about C23 compatibility. */
10911 36350917 : if (warn_deprecated_non_prototype
10912 34 : && old_decl != NULL_TREE
10913 5 : && TREE_CODE (oldtype) == FUNCTION_TYPE
10914 5 : && !TYPE_ARG_TYPES (oldtype)
10915 5 : && !TYPE_NO_NAMED_ARGS_STDARG_P (oldtype)
10916 36350922 : && (TYPE_ARG_TYPES (newtype)
10917 3 : && TREE_VALUE (TYPE_ARG_TYPES (newtype)) != void_type_node))
10918 : {
10919 3 : bool warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
10920 : "ISO C23 does not allow defining"
10921 : " parameters for function %qE declared"
10922 : " without parameters",
10923 : decl1);
10924 3 : if (warned)
10925 3 : inform (DECL_SOURCE_LOCATION (old_decl), "declared here");
10926 : }
10927 :
10928 : /* Optionally warn of old-fashioned def with no previous prototype. */
10929 36350917 : if (warn_strict_prototypes
10930 107632 : && old_decl != error_mark_node
10931 107632 : && !prototype_p (TREE_TYPE (decl1))
10932 36350918 : && C_DECL_ISNT_PROTOTYPE (old_decl))
10933 1 : warning_at (loc, OPT_Wstrict_prototypes,
10934 : "function declaration isn%'t a prototype");
10935 : /* Optionally warn of any global def with no previous prototype. */
10936 36350916 : else if (warn_missing_prototypes
10937 107333 : && old_decl != error_mark_node
10938 107333 : && TREE_PUBLIC (decl1)
10939 74122 : && !MAIN_NAME_P (DECL_NAME (decl1))
10940 74085 : && C_DECL_ISNT_PROTOTYPE (old_decl)
10941 36351557 : && !DECL_DECLARED_INLINE_P (decl1))
10942 4 : warning_at (loc, OPT_Wmissing_prototypes,
10943 : "no previous prototype for %qD", decl1);
10944 : /* Optionally warn of any def with no previous prototype
10945 : if the function has already been used. */
10946 36350912 : else if (warn_missing_prototypes
10947 107329 : && old_decl != NULL_TREE
10948 74459 : && old_decl != error_mark_node
10949 74459 : && TREE_USED (old_decl)
10950 36358028 : && !prototype_p (TREE_TYPE (old_decl)))
10951 0 : warning_at (loc, OPT_Wmissing_prototypes,
10952 : "%qD was used with no prototype before its definition", decl1);
10953 : /* Optionally warn of any global def with no previous declaration. */
10954 36350912 : else if (warn_missing_declarations
10955 4 : && TREE_PUBLIC (decl1)
10956 3 : && old_decl == NULL_TREE
10957 3 : && !MAIN_NAME_P (DECL_NAME (decl1))
10958 36350915 : && !DECL_DECLARED_INLINE_P (decl1))
10959 2 : warning_at (loc, OPT_Wmissing_declarations,
10960 : "no previous declaration for %qD",
10961 : decl1);
10962 : /* Optionally warn of any def with no previous declaration
10963 : if the function has already been used. */
10964 36350910 : else if (warn_missing_declarations
10965 2 : && old_decl != NULL_TREE
10966 0 : && old_decl != error_mark_node
10967 0 : && TREE_USED (old_decl)
10968 36350910 : && C_DECL_IMPLICIT (old_decl))
10969 0 : warning_at (loc, OPT_Wmissing_declarations,
10970 : "%qD was used with no declaration before its definition", decl1);
10971 :
10972 : /* This function exists in static storage.
10973 : (This does not mean `static' in the C sense!) */
10974 36350917 : TREE_STATIC (decl1) = 1;
10975 :
10976 : /* This is the earliest point at which we might know the assembler
10977 : name of the function. Thus, if it's set before this, die horribly. */
10978 36350917 : gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
10979 :
10980 : /* If #pragma weak was used, mark the decl weak now. */
10981 36350917 : if (current_scope == file_scope)
10982 36349319 : maybe_apply_pragma_weak (decl1);
10983 :
10984 : /* Warn for unlikely, improbable, or stupid declarations of `main'. */
10985 36350917 : if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
10986 : {
10987 3339 : if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
10988 3339 : != integer_type_node)
10989 5 : pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
10990 3334 : else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
10991 1 : pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
10992 : decl1);
10993 :
10994 3339 : check_main_parameter_types (decl1);
10995 :
10996 3339 : if (!TREE_PUBLIC (decl1))
10997 0 : pedwarn (loc, OPT_Wmain,
10998 : "%qD is normally a non-static function", decl1);
10999 : }
11000 :
11001 36350917 : tree parms = current_function_arg_info->parms;
11002 36350917 : if (old_decl)
11003 : {
11004 230655 : location_t origloc = DECL_SOURCE_LOCATION (old_decl);
11005 230655 : warn_parms_array_mismatch (origloc, old_decl, parms);
11006 : }
11007 :
11008 : /* To enable versions to be created across TU's we mark and mangle all
11009 : non-default versioned functions. */
11010 36350917 : if (TREE_CODE (decl1) == FUNCTION_DECL
11011 : && !TARGET_HAS_FMV_TARGET_ATTRIBUTE
11012 : && get_target_version (decl1).is_valid ())
11013 : {
11014 : maybe_mark_function_versioned (decl1);
11015 : if (current_scope != file_scope)
11016 : error ("versioned definitions are only allowed at file scope");
11017 : }
11018 :
11019 : /* Record the decl so that the function name is defined.
11020 : If we already have a decl for this name, and it is a FUNCTION_DECL,
11021 : use the old decl. */
11022 :
11023 36350917 : current_function_decl = pushdecl (decl1);
11024 :
11025 36350917 : if (tree access = build_attr_access_from_parms (parms, false))
11026 57829 : decl_attributes (¤t_function_decl, access, ATTR_FLAG_INTERNAL,
11027 : old_decl);
11028 :
11029 36350917 : push_scope ();
11030 36350917 : declare_parm_level ();
11031 :
11032 : /* Set the result decl source location to the location of the typespec. */
11033 4107475 : result_loc = (declspecs->locations[cdw_typespec] == UNKNOWN_LOCATION
11034 36350917 : ? loc : declspecs->locations[cdw_typespec]);
11035 36350917 : restype = TREE_TYPE (TREE_TYPE (current_function_decl));
11036 36350917 : resdecl = build_decl (result_loc, RESULT_DECL, NULL_TREE, restype);
11037 36350917 : DECL_ARTIFICIAL (resdecl) = 1;
11038 36350917 : DECL_IGNORED_P (resdecl) = 1;
11039 36350917 : DECL_RESULT (current_function_decl) = resdecl;
11040 :
11041 36350917 : start_fname_decls ();
11042 :
11043 36350917 : return true;
11044 : }
11045 :
11046 : /* Subroutine of store_parm_decls which handles new-style function
11047 : definitions (prototype format). The parms already have decls, so we
11048 : need only record them as in effect and complain if any redundant
11049 : old-style parm decls were written. */
11050 : static void
11051 36337764 : store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
11052 : {
11053 36337764 : tree decl;
11054 36337764 : c_arg_tag *tag;
11055 36337764 : unsigned ix;
11056 :
11057 36337764 : if (current_scope->bindings)
11058 : {
11059 8 : error_at (DECL_SOURCE_LOCATION (fndecl),
11060 : "old-style parameter declarations in prototyped "
11061 : "function definition");
11062 :
11063 : /* Get rid of the old-style declarations. */
11064 8 : pop_scope ();
11065 8 : push_scope ();
11066 : }
11067 : /* Don't issue this warning for nested functions, and don't issue this
11068 : warning if we got here because ARG_INFO_TYPES was error_mark_node
11069 : (this happens when a function definition has just an ellipsis in
11070 : its parameter list). */
11071 36337756 : else if (!in_system_header_at (input_location)
11072 643769 : && !current_function_scope
11073 642319 : && arg_info->types != error_mark_node
11074 36980075 : && !arg_info->c23_empty_parens)
11075 590080 : warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
11076 : "traditional C rejects ISO C style function definitions");
11077 :
11078 : /* Now make all the parameter declarations visible in the function body.
11079 : We can bypass most of the grunt work of pushdecl. */
11080 136453046 : for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
11081 : {
11082 100115282 : DECL_CONTEXT (decl) = current_function_decl;
11083 100115282 : if (DECL_NAME (decl))
11084 : {
11085 100113711 : bind (DECL_NAME (decl), decl, current_scope,
11086 : /*invisible=*/false, /*nested=*/false,
11087 : UNKNOWN_LOCATION);
11088 100113711 : if (!TREE_USED (decl))
11089 100096275 : warn_if_shadowing (decl);
11090 : }
11091 : else
11092 1571 : pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_parameter_name,
11093 : "ISO C does not support omitting parameter names in "
11094 : "function definitions before C23");
11095 : }
11096 :
11097 : /* Record the parameter list in the function declaration. */
11098 36337764 : DECL_ARGUMENTS (fndecl) = arg_info->parms;
11099 :
11100 : /* Now make all the ancillary declarations visible, likewise. */
11101 36337825 : for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
11102 : {
11103 61 : DECL_CONTEXT (decl) = current_function_decl;
11104 61 : if (DECL_NAME (decl))
11105 0 : bind (DECL_NAME (decl), decl, current_scope,
11106 : /*invisible=*/false,
11107 0 : /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
11108 : UNKNOWN_LOCATION);
11109 : }
11110 :
11111 : /* And all the tag declarations. */
11112 36337881 : FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
11113 61 : if (tag->id)
11114 28 : bind (tag->id, tag->type, current_scope,
11115 : /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
11116 36337764 : }
11117 :
11118 : /* Subroutine of store_parm_decls which handles old-style function
11119 : definitions (separate parameter list and declarations). */
11120 :
11121 : static void
11122 13153 : store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
11123 : {
11124 13153 : struct c_binding *b;
11125 13153 : tree parm, decl, last;
11126 13153 : tree parmids = arg_info->parms;
11127 13153 : hash_set<tree> seen_args;
11128 :
11129 13153 : if (!in_system_header_at (input_location))
11130 : {
11131 13150 : if (flag_isoc23)
11132 1348 : pedwarn (DECL_SOURCE_LOCATION (fndecl),
11133 1348 : OPT_Wold_style_definition, "old-style function definition");
11134 : else
11135 11802 : warning_at (DECL_SOURCE_LOCATION (fndecl),
11136 11802 : OPT_Wold_style_definition,
11137 : "old-style function definition");
11138 : }
11139 :
11140 13153 : if (current_scope->had_vla_unspec)
11141 1 : error ("%<[*]%> not allowed in other than function prototype scope");
11142 :
11143 : /* Match each formal parameter name with its declaration. Save each
11144 : decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
11145 47427 : for (parm = parmids; parm; parm = TREE_CHAIN (parm))
11146 : {
11147 34274 : if (TREE_VALUE (parm) == NULL_TREE)
11148 : {
11149 0 : error_at (DECL_SOURCE_LOCATION (fndecl),
11150 : "parameter name missing from parameter list");
11151 0 : TREE_PURPOSE (parm) = NULL_TREE;
11152 0 : continue;
11153 : }
11154 :
11155 34274 : b = I_SYMBOL_BINDING (TREE_VALUE (parm));
11156 34274 : if (b && B_IN_CURRENT_SCOPE (b))
11157 : {
11158 22654 : decl = b->decl;
11159 : /* Skip erroneous parameters. */
11160 22654 : if (decl == error_mark_node)
11161 2 : continue;
11162 : /* If we got something other than a PARM_DECL it is an error. */
11163 22652 : if (TREE_CODE (decl) != PARM_DECL)
11164 : {
11165 7 : error_at (DECL_SOURCE_LOCATION (decl),
11166 : "%qD declared as a non-parameter", decl);
11167 7 : continue;
11168 : }
11169 : /* If the declaration is already marked, we have a duplicate
11170 : name. Complain and ignore the duplicate. */
11171 22645 : else if (seen_args.contains (decl))
11172 : {
11173 0 : error_at (DECL_SOURCE_LOCATION (decl),
11174 : "multiple parameters named %qD", decl);
11175 0 : TREE_PURPOSE (parm) = NULL_TREE;
11176 0 : continue;
11177 : }
11178 : /* If the declaration says "void", complain and turn it into
11179 : an int. */
11180 22645 : else if (VOID_TYPE_P (TREE_TYPE (decl)))
11181 : {
11182 0 : error_at (DECL_SOURCE_LOCATION (decl),
11183 : "parameter %qD declared with void type", decl);
11184 0 : TREE_TYPE (decl) = integer_type_node;
11185 0 : DECL_ARG_TYPE (decl) = integer_type_node;
11186 0 : layout_decl (decl, 0);
11187 : }
11188 22645 : warn_if_shadowing (decl);
11189 : }
11190 : /* If no declaration found, default to int. */
11191 : else
11192 : {
11193 : /* FIXME diagnostics: This should be the location of the argument,
11194 : not the FNDECL. E.g., for an old-style declaration
11195 :
11196 : int f10(v) { blah; }
11197 :
11198 : We should use the location of the V, not the F10.
11199 : Unfortunately, the V is an IDENTIFIER_NODE which has no
11200 : location. In the future we need locations for c_arg_info
11201 : entries.
11202 :
11203 : See gcc.dg/Wshadow-3.c for an example of this problem. */
11204 11620 : decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
11205 11620 : PARM_DECL, TREE_VALUE (parm), integer_type_node);
11206 11620 : DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
11207 11620 : pushdecl (decl);
11208 11620 : warn_if_shadowing (decl);
11209 :
11210 11620 : if (flag_isoc99)
11211 118 : permerror_opt (DECL_SOURCE_LOCATION (decl),
11212 118 : OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
11213 : decl);
11214 : else
11215 11502 : warning_at (DECL_SOURCE_LOCATION (decl),
11216 11502 : OPT_Wmissing_parameter_type,
11217 : "type of %qD defaults to %<int%>", decl);
11218 : }
11219 :
11220 34265 : TREE_PURPOSE (parm) = decl;
11221 34265 : seen_args.add (decl);
11222 : }
11223 :
11224 : /* Now examine the parms chain for incomplete declarations
11225 : and declarations with no corresponding names. */
11226 :
11227 47506 : for (b = current_scope->bindings; b; b = b->prev)
11228 : {
11229 34353 : parm = b->decl;
11230 34353 : if (TREE_CODE (parm) != PARM_DECL)
11231 79 : continue;
11232 :
11233 34274 : if (TREE_TYPE (parm) != error_mark_node
11234 34274 : && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
11235 : {
11236 0 : error_at (DECL_SOURCE_LOCATION (parm),
11237 : "parameter %qD has incomplete type", parm);
11238 0 : TREE_TYPE (parm) = error_mark_node;
11239 : }
11240 :
11241 34274 : if (!seen_args.contains (parm))
11242 : {
11243 9 : error_at (DECL_SOURCE_LOCATION (parm),
11244 : "declaration for parameter %qD but no such parameter",
11245 : parm);
11246 :
11247 : /* Pretend the parameter was not missing.
11248 : This gets us to a standard state and minimizes
11249 : further error messages. */
11250 9 : parmids = chainon (parmids, tree_cons (parm, 0, 0));
11251 : }
11252 : }
11253 :
11254 : /* Chain the declarations together in the order of the list of
11255 : names. Store that chain in the function decl, replacing the
11256 : list of names. Update the current scope to match. */
11257 13153 : DECL_ARGUMENTS (fndecl) = NULL_TREE;
11258 :
11259 13161 : for (parm = parmids; parm; parm = TREE_CHAIN (parm))
11260 8680 : if (TREE_PURPOSE (parm))
11261 : break;
11262 13153 : if (parm && TREE_PURPOSE (parm))
11263 : {
11264 8672 : last = TREE_PURPOSE (parm);
11265 8672 : DECL_ARGUMENTS (fndecl) = last;
11266 :
11267 34275 : for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
11268 25603 : if (TREE_PURPOSE (parm))
11269 : {
11270 25602 : DECL_CHAIN (last) = TREE_PURPOSE (parm);
11271 25603 : last = TREE_PURPOSE (parm);
11272 : }
11273 8672 : DECL_CHAIN (last) = NULL_TREE;
11274 : }
11275 :
11276 : /* If there was a previous prototype,
11277 : set the DECL_ARG_TYPE of each argument according to
11278 : the type previously specified, and report any mismatches. */
11279 :
11280 13153 : if (current_function_prototype_arg_types)
11281 : {
11282 171 : tree type;
11283 171 : for (parm = DECL_ARGUMENTS (fndecl),
11284 171 : type = current_function_prototype_arg_types;
11285 373 : parm || (type != NULL_TREE
11286 166 : && TREE_VALUE (type) != error_mark_node
11287 166 : && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
11288 202 : parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
11289 : {
11290 212 : if (parm == NULL_TREE
11291 206 : || type == NULL_TREE
11292 418 : || (TREE_VALUE (type) != error_mark_node
11293 192 : && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
11294 : {
11295 10 : if (current_function_prototype_built_in)
11296 6 : warning_at (DECL_SOURCE_LOCATION (fndecl),
11297 6 : 0, "number of arguments doesn%'t match "
11298 : "built-in prototype");
11299 : else
11300 : {
11301 : /* FIXME diagnostics: This should be the location of
11302 : FNDECL, but there is bug when a prototype is
11303 : declared inside function context, but defined
11304 : outside of it (e.g., gcc.dg/pr15698-2.c). In
11305 : which case FNDECL gets the location of the
11306 : prototype, not the definition. */
11307 4 : error_at (input_location,
11308 : "number of arguments doesn%'t match prototype");
11309 :
11310 4 : error_at (current_function_prototype_locus,
11311 : "prototype declaration");
11312 : }
11313 : break;
11314 : }
11315 : /* Type for passing arg must be consistent with that
11316 : declared for the arg. ISO C says we take the unqualified
11317 : type for parameters declared with qualified type. */
11318 202 : if (TREE_TYPE (parm) != error_mark_node
11319 201 : && TREE_VALUE (type) != error_mark_node
11320 389 : && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
11321 187 : != TYPE_ATOMIC (TREE_VALUE (type)))
11322 185 : || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
11323 185 : TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
11324 : {
11325 36 : if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
11326 36 : == TYPE_ATOMIC (TREE_VALUE (type)))
11327 36 : && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
11328 34 : == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
11329 : {
11330 : /* Adjust argument to match prototype. E.g. a previous
11331 : `int foo(float);' prototype causes
11332 : `int foo(x) float x; {...}' to be treated like
11333 : `int foo(float x) {...}'. This is particularly
11334 : useful for argument types like uid_t. */
11335 17 : DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
11336 :
11337 : /* ??? Is it possible to get here with a
11338 : built-in prototype or will it always have
11339 : been diagnosed as conflicting with an
11340 : old-style definition and discarded? */
11341 17 : if (current_function_prototype_built_in)
11342 2 : warning_at (DECL_SOURCE_LOCATION (parm),
11343 2 : OPT_Wpedantic, "promoted argument %qD "
11344 : "doesn%'t match built-in prototype", parm);
11345 : else
11346 : {
11347 15 : pedwarn (DECL_SOURCE_LOCATION (parm),
11348 15 : OPT_Wpedantic, "promoted argument %qD "
11349 : "doesn%'t match prototype", parm);
11350 15 : pedwarn (current_function_prototype_locus, OPT_Wpedantic,
11351 : "prototype declaration");
11352 : }
11353 : }
11354 : else
11355 : {
11356 19 : if (current_function_prototype_built_in)
11357 10 : warning_at (DECL_SOURCE_LOCATION (parm),
11358 10 : 0, "argument %qD doesn%'t match "
11359 : "built-in prototype", parm);
11360 : else
11361 : {
11362 9 : error_at (DECL_SOURCE_LOCATION (parm),
11363 : "argument %qD doesn%'t match prototype", parm);
11364 9 : error_at (current_function_prototype_locus,
11365 : "prototype declaration");
11366 : }
11367 : }
11368 : }
11369 : }
11370 171 : TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
11371 : }
11372 :
11373 : /* Otherwise, create a prototype that would match. */
11374 :
11375 : else
11376 : {
11377 12982 : tree actual = NULL_TREE, last = NULL_TREE, type;
11378 :
11379 47050 : for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
11380 : {
11381 34068 : type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
11382 34068 : if (last)
11383 25552 : TREE_CHAIN (last) = type;
11384 : else
11385 : actual = type;
11386 34068 : last = type;
11387 : }
11388 12982 : type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
11389 12982 : if (last)
11390 8516 : TREE_CHAIN (last) = type;
11391 : else
11392 : actual = type;
11393 :
11394 : /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
11395 : of the type of this function, but we need to avoid having this
11396 : affect the types of other similarly-typed functions, so we must
11397 : first force the generation of an identical (but separate) type
11398 : node for the relevant function type. The new node we create
11399 : will be a variant of the main variant of the original function
11400 : type. */
11401 :
11402 12982 : TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
11403 :
11404 12982 : TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
11405 : }
11406 13153 : }
11407 :
11408 : /* Store parameter declarations passed in ARG_INFO into the current
11409 : function declaration. */
11410 :
11411 : void
11412 0 : store_parm_decls_from (struct c_arg_info *arg_info)
11413 : {
11414 0 : current_function_arg_info = arg_info;
11415 0 : store_parm_decls ();
11416 0 : }
11417 :
11418 : /* Called by walk_tree to look for and update context-less labels
11419 : or labels with context in the parent function. */
11420 :
11421 : static tree
11422 8175 : set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
11423 : {
11424 8175 : tree ctx = static_cast<tree>(data);
11425 8175 : if (TREE_CODE (*tp) == LABEL_EXPR
11426 8175 : && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
11427 2 : || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
11428 : {
11429 72 : DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
11430 72 : *walk_subtrees = 0;
11431 : }
11432 :
11433 8175 : return NULL_TREE;
11434 : }
11435 :
11436 : /* Store the parameter declarations into the current function declaration.
11437 : This is called after parsing the parameter declarations, before
11438 : digesting the body of the function.
11439 :
11440 : For an old-style definition, construct a prototype out of the old-style
11441 : parameter declarations and inject it into the function's type. */
11442 :
11443 : void
11444 36350917 : store_parm_decls (void)
11445 : {
11446 36350917 : tree fndecl = current_function_decl;
11447 36350917 : bool proto;
11448 :
11449 : /* The argument information block for FNDECL. */
11450 36350917 : struct c_arg_info *arg_info = current_function_arg_info;
11451 36350917 : current_function_arg_info = 0;
11452 :
11453 : /* True if this definition is written with a prototype. In C23, an
11454 : empty argument list was converted to (void) in grokparms; in
11455 : older C standard versions, it does not give the function a type
11456 : with a prototype for future calls. */
11457 36350917 : proto = arg_info->types != 0 || arg_info->no_named_args_stdarg_p;
11458 :
11459 13153 : if (proto)
11460 36337764 : store_parm_decls_newstyle (fndecl, arg_info);
11461 : else
11462 13153 : store_parm_decls_oldstyle (fndecl, arg_info);
11463 :
11464 : /* The next call to push_scope will be a function body. */
11465 :
11466 36350917 : next_is_function_body = true;
11467 :
11468 : /* Write a record describing this function definition to the prototypes
11469 : file (if requested). */
11470 :
11471 36350917 : gen_aux_info_record (fndecl, 1, 0, proto);
11472 :
11473 : /* Initialize the RTL code for the function. */
11474 36350917 : allocate_struct_function (fndecl, false);
11475 :
11476 36350917 : if (warn_unused_local_typedefs)
11477 3117334 : cfun->language = ggc_cleared_alloc<language_function> ();
11478 :
11479 : /* Begin the statement tree for this function. */
11480 36350917 : DECL_SAVED_TREE (fndecl) = push_stmt_list ();
11481 :
11482 : /* ??? Insert the contents of the pending sizes list into the function
11483 : to be evaluated. The only reason left to have this is
11484 : void foo(int n, int array[n++])
11485 : because we throw away the array type in favor of a pointer type, and
11486 : thus won't naturally see the SAVE_EXPR containing the increment. All
11487 : other pending sizes would be handled by gimplify_parameters. */
11488 36350917 : if (arg_info->pending_sizes)
11489 : {
11490 : /* In very special circumstances, e.g. for code like
11491 : _Atomic int i = 5;
11492 : void f (int a[i += 2]) {}
11493 : we need to execute the atomic assignment on function entry.
11494 : But in this case, it is not just a straight store, it has the
11495 : op= form, which means that build_atomic_assign has generated
11496 : gotos, labels, etc. Because at that time the function decl
11497 : for F has not been created yet, those labels do not have any
11498 : function context. But we have the fndecl now, so update the
11499 : labels accordingly. gimplify_expr would crash otherwise.
11500 : Or with nested functions the labels could be created with parent
11501 : function's context, while when the statement is emitted at the
11502 : start of the nested function, it needs the nested function's
11503 : context. */
11504 298 : walk_tree_without_duplicates (&arg_info->pending_sizes,
11505 : set_labels_context_r, fndecl);
11506 298 : add_stmt (arg_info->pending_sizes);
11507 : }
11508 36350917 : }
11509 :
11510 : /* Store PARM_DECLs in PARMS into scope temporarily. Used for
11511 : c_finish_omp_declare_simd for function prototypes. No diagnostics
11512 : should be done. */
11513 :
11514 : void
11515 249 : temp_store_parm_decls (tree fndecl, tree parms)
11516 : {
11517 249 : push_scope ();
11518 699 : for (tree p = parms; p; p = DECL_CHAIN (p))
11519 : {
11520 450 : DECL_CONTEXT (p) = fndecl;
11521 450 : if (DECL_NAME (p))
11522 328 : bind (DECL_NAME (p), p, current_scope,
11523 : /*invisible=*/false, /*nested=*/false,
11524 : UNKNOWN_LOCATION);
11525 : }
11526 249 : }
11527 :
11528 : /* Undo what temp_store_parm_decls did. */
11529 :
11530 : void
11531 249 : temp_pop_parm_decls (void)
11532 : {
11533 : /* Clear all bindings in this temporary scope, so that
11534 : pop_scope doesn't create a BLOCK. */
11535 249 : struct c_binding *b = current_scope->bindings;
11536 249 : current_scope->bindings = NULL;
11537 582 : for (; b; b = free_binding_and_advance (b))
11538 : {
11539 333 : gcc_assert (TREE_CODE (b->decl) == PARM_DECL
11540 : || b->decl == error_mark_node);
11541 333 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
11542 333 : I_SYMBOL_BINDING (b->id) = b->shadowed;
11543 333 : if (b->shadowed && b->shadowed->u.type)
11544 0 : TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
11545 : }
11546 249 : pop_scope ();
11547 249 : }
11548 :
11549 :
11550 : /* Finish up a function declaration and compile that function
11551 : all the way to assembler language output. Then free the storage
11552 : for the function definition.
11553 :
11554 : This is called after parsing the body of the function definition. */
11555 :
11556 : void
11557 36350915 : finish_function (location_t end_loc)
11558 : {
11559 36350915 : tree fndecl = current_function_decl;
11560 :
11561 36350915 : if (c_dialect_objc ())
11562 0 : objc_finish_function ();
11563 :
11564 36350915 : if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
11565 36350913 : BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
11566 :
11567 : /* Must mark the RESULT_DECL as being in this function. */
11568 :
11569 36350915 : if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
11570 36350915 : DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
11571 :
11572 36398731 : if (MAIN_NAME_P (DECL_NAME (fndecl)) && !TREE_THIS_VOLATILE (fndecl)
11573 47815 : && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
11574 36398730 : == integer_type_node && flag_isoc99)
11575 : {
11576 : /* Hack. We don't want the middle-end to warn that this return
11577 : is unreachable, so we mark its location as special. Using
11578 : UNKNOWN_LOCATION has the problem that it gets clobbered in
11579 : annotate_one_with_locus. A cleaner solution might be to
11580 : ensure ! should_carry_locus_p (stmt), but that needs a flag.
11581 : */
11582 46426 : c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
11583 : }
11584 :
11585 : /* Tie off the statement tree for this function. */
11586 36350915 : DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
11587 :
11588 36350915 : finish_fname_decls ();
11589 :
11590 : /* Complain if there's no return statement only if option specified on
11591 : command line. */
11592 36350915 : if (warn_return_type > 0
11593 3117357 : && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
11594 2921086 : && !current_function_returns_value && !current_function_returns_null
11595 : /* Don't complain if we are no-return. */
11596 72 : && !current_function_returns_abnormally
11597 : /* Don't complain if we are declared noreturn. */
11598 21 : && !TREE_THIS_VOLATILE (fndecl)
11599 : /* Don't warn for main(). */
11600 18 : && !MAIN_NAME_P (DECL_NAME (fndecl))
11601 : /* Or if they didn't actually specify a return type. */
11602 17 : && !C_FUNCTION_IMPLICIT_INT (fndecl)
11603 : /* Normally, with -Wreturn-type, flow will complain, but we might
11604 : optimize out static functions. */
11605 17 : && !TREE_PUBLIC (fndecl)
11606 6 : && targetm.warn_func_return (fndecl)
11607 39468272 : && warning (OPT_Wreturn_type,
11608 : "no return statement in function returning non-void"))
11609 5 : suppress_warning (fndecl, OPT_Wreturn_type);
11610 :
11611 : /* Complain about parameters that are only set, but never otherwise used. */
11612 36350915 : if (warn_unused_but_set_parameter)
11613 : {
11614 3051647 : tree decl;
11615 :
11616 3051647 : for (decl = DECL_ARGUMENTS (fndecl);
11617 11401197 : decl;
11618 8349550 : decl = DECL_CHAIN (decl))
11619 8349550 : if (TREE_USED (decl)
11620 8288847 : && TREE_CODE (decl) == PARM_DECL
11621 8288847 : && !DECL_READ_P (decl)
11622 47 : && DECL_NAME (decl)
11623 47 : && !DECL_ARTIFICIAL (decl)
11624 8349597 : && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter_))
11625 47 : warning_at (DECL_SOURCE_LOCATION (decl),
11626 47 : OPT_Wunused_but_set_parameter_,
11627 : "parameter %qD set but not used", decl);
11628 : }
11629 :
11630 : /* Complain about locally defined typedefs that are not used in this
11631 : function. */
11632 36350915 : maybe_warn_unused_local_typedefs ();
11633 :
11634 : /* Possibly warn about unused parameters. */
11635 36350915 : if (warn_unused_parameter)
11636 2957758 : do_warn_unused_parameter (fndecl);
11637 :
11638 : /* Store the end of the function, so that we get good line number
11639 : info for the epilogue. */
11640 36350915 : cfun->function_end_locus = end_loc;
11641 :
11642 : /* Finalize the ELF visibility for the function. */
11643 36350915 : c_determine_visibility (fndecl);
11644 :
11645 : /* For GNU C extern inline functions disregard inline limits. */
11646 36350915 : if (DECL_EXTERNAL (fndecl)
11647 35492740 : && DECL_DECLARED_INLINE_P (fndecl)
11648 71843652 : && (flag_gnu89_inline
11649 35461733 : || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
11650 35492255 : DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
11651 :
11652 : /* Genericize before inlining. Delay genericizing nested functions
11653 : until their parent function is genericized. Since finalizing
11654 : requires GENERIC, delay that as well. */
11655 :
11656 72701830 : if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
11657 72701828 : && !undef_nested_function)
11658 : {
11659 36350907 : if (!decl_function_context (fndecl))
11660 : {
11661 36349309 : invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
11662 36349309 : c_genericize (fndecl);
11663 :
11664 : /* ??? Objc emits functions after finalizing the compilation unit.
11665 : This should be cleaned up later and this conditional removed. */
11666 36349309 : if (symtab->global_info_ready)
11667 : {
11668 0 : cgraph_node::add_new_function (fndecl, false);
11669 0 : return;
11670 : }
11671 36349309 : cgraph_node::finalize_function (fndecl, false);
11672 : }
11673 : else
11674 : {
11675 : /* Register this function with cgraph just far enough to get it
11676 : added to our parent's nested function list. Handy, since the
11677 : C front end doesn't have such a list. */
11678 1598 : (void) cgraph_node::get_create (fndecl);
11679 : }
11680 : }
11681 :
11682 36350915 : if (!decl_function_context (fndecl))
11683 36349317 : undef_nested_function = false;
11684 :
11685 36350915 : if (cfun->language != NULL)
11686 : {
11687 3117333 : ggc_free (cfun->language);
11688 3117333 : cfun->language = NULL;
11689 : }
11690 :
11691 : /* We're leaving the context of this function, so zap cfun.
11692 : It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
11693 : tree_rest_of_compilation. */
11694 36350915 : set_cfun (NULL);
11695 36350915 : invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
11696 36350915 : current_function_decl = NULL;
11697 : }
11698 :
11699 : /* Check the declarations given in a for-loop for satisfying the C99
11700 : constraints. If exactly one such decl is found, return it. LOC is
11701 : the location of the opening parenthesis of the for loop. The last
11702 : parameter allows you to control the "for loop initial declarations
11703 : are only allowed in C99 mode". Normally, you should pass
11704 : flag_isoc99 as that parameter. But in some cases (Objective-C
11705 : foreach loop, for example) we want to run the checks in this
11706 : function even if not in C99 mode, so we allow the caller to turn
11707 : off the error about not being in C99 mode.
11708 : */
11709 :
11710 : tree
11711 26096 : check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
11712 : {
11713 26096 : struct c_binding *b;
11714 26096 : tree one_decl = NULL_TREE;
11715 26096 : int n_decls = 0;
11716 :
11717 26096 : if (!turn_off_iso_c99_error)
11718 : {
11719 1 : static bool hint = true;
11720 : /* If we get here, declarations have been used in a for loop without
11721 : the C99 for loop scope. This doesn't make much sense, so don't
11722 : allow it. */
11723 1 : auto_diagnostic_group d;
11724 1 : error_at (loc, "%<for%> loop initial declarations "
11725 : "are only allowed in C99 or C11 mode");
11726 1 : if (hint)
11727 : {
11728 1 : inform (loc,
11729 : "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
11730 : "%<-std=gnu11%> to compile your code");
11731 1 : hint = false;
11732 : }
11733 1 : return NULL_TREE;
11734 1 : }
11735 : else
11736 26095 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
11737 : "initial declarations");
11738 :
11739 : /* C99 subclause 6.8.5 paragraph 3:
11740 :
11741 : [#3] The declaration part of a for statement shall only
11742 : declare identifiers for objects having storage class auto or
11743 : register.
11744 :
11745 : It isn't clear whether, in this sentence, "identifiers" binds to
11746 : "shall only declare" or to "objects" - that is, whether all identifiers
11747 : declared must be identifiers for objects, or whether the restriction
11748 : only applies to those that are. (A question on this in comp.std.c
11749 : in November 2000 received no answer.) We implement the strictest
11750 : interpretation, to avoid creating an extension which later causes
11751 : problems.
11752 :
11753 : This constraint was removed in C23. */
11754 :
11755 52270 : for (b = current_scope->bindings; b; b = b->prev)
11756 : {
11757 26175 : tree id = b->id;
11758 26175 : tree decl = b->decl;
11759 :
11760 26175 : if (!id)
11761 27 : continue;
11762 :
11763 26148 : switch (TREE_CODE (decl))
11764 : {
11765 26100 : case VAR_DECL:
11766 26100 : {
11767 26100 : location_t decl_loc = DECL_SOURCE_LOCATION (decl);
11768 26100 : if (TREE_STATIC (decl))
11769 5 : pedwarn_c11 (decl_loc, OPT_Wpedantic,
11770 : "declaration of static variable %qD in %<for%> "
11771 : "loop initial declaration", decl);
11772 26095 : else if (DECL_EXTERNAL (decl))
11773 5 : pedwarn_c11 (decl_loc, OPT_Wpedantic,
11774 : "declaration of %<extern%> variable %qD in %<for%> "
11775 : "loop initial declaration", decl);
11776 : }
11777 : break;
11778 :
11779 6 : case RECORD_TYPE:
11780 6 : pedwarn_c11 (loc, OPT_Wpedantic,
11781 : "%<struct %E%> declared in %<for%> loop initial "
11782 : "declaration", id);
11783 6 : break;
11784 5 : case UNION_TYPE:
11785 5 : pedwarn_c11 (loc, OPT_Wpedantic,
11786 : "%<union %E%> declared in %<for%> loop initial "
11787 : "declaration",
11788 : id);
11789 5 : break;
11790 5 : case ENUMERAL_TYPE:
11791 5 : pedwarn_c11 (loc, OPT_Wpedantic,
11792 : "%<enum %E%> declared in %<for%> loop "
11793 : "initial declaration", id);
11794 5 : break;
11795 32 : default:
11796 32 : pedwarn_c11 (loc, OPT_Wpedantic, "declaration of non-variable "
11797 : "%qD in %<for%> loop initial declaration", decl);
11798 : }
11799 :
11800 26148 : n_decls++;
11801 26148 : one_decl = decl;
11802 : }
11803 :
11804 26095 : return n_decls == 1 ? one_decl : NULL_TREE;
11805 : }
11806 :
11807 : /* Save and reinitialize the variables
11808 : used during compilation of a C function. */
11809 :
11810 : void
11811 1626 : c_push_function_context (void)
11812 : {
11813 1626 : struct language_function *p = cfun->language;
11814 : /* cfun->language might have been already allocated by the use of
11815 : -Wunused-local-typedefs. In that case, just re-use it. */
11816 1626 : if (p == NULL)
11817 1570 : cfun->language = p = ggc_cleared_alloc<language_function> ();
11818 :
11819 1626 : p->base.x_stmt_tree = c_stmt_tree;
11820 1626 : c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
11821 1626 : p->x_in_statement = in_statement;
11822 1626 : p->x_switch_stack = c_switch_stack;
11823 1626 : p->loop_names = loop_names;
11824 1626 : loop_names = vNULL;
11825 1626 : p->loop_names_hash = loop_names_hash;
11826 1626 : loop_names_hash = NULL;
11827 1626 : p->arg_info = current_function_arg_info;
11828 1626 : p->returns_value = current_function_returns_value;
11829 1626 : p->returns_null = current_function_returns_null;
11830 1626 : p->returns_abnormally = current_function_returns_abnormally;
11831 1626 : p->warn_about_return_type = warn_about_return_type;
11832 :
11833 1626 : push_function_context ();
11834 1626 : }
11835 :
11836 : /* Restore the variables used during compilation of a C function. */
11837 :
11838 : void
11839 1626 : c_pop_function_context (void)
11840 : {
11841 1626 : struct language_function *p;
11842 :
11843 1626 : pop_function_context ();
11844 1626 : p = cfun->language;
11845 :
11846 : /* When -Wunused-local-typedefs is in effect, cfun->languages is
11847 : used to store data throughout the life time of the current cfun,
11848 : So don't deallocate it. */
11849 1626 : if (!warn_unused_local_typedefs)
11850 1570 : cfun->language = NULL;
11851 :
11852 1626 : if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
11853 1626 : && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
11854 : {
11855 : /* Stop pointing to the local nodes about to be freed. */
11856 : /* But DECL_INITIAL must remain nonzero so we know this
11857 : was an actual function definition. */
11858 0 : DECL_INITIAL (current_function_decl) = error_mark_node;
11859 0 : DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
11860 : }
11861 :
11862 1626 : c_stmt_tree = p->base.x_stmt_tree;
11863 1626 : p->base.x_stmt_tree.x_cur_stmt_list = NULL;
11864 1626 : in_statement = p->x_in_statement;
11865 1626 : c_switch_stack = p->x_switch_stack;
11866 1626 : loop_names.release ();
11867 1626 : loop_names = p->loop_names;
11868 1626 : p->loop_names = vNULL;
11869 1626 : delete loop_names_hash;
11870 1626 : loop_names_hash = p->loop_names_hash;
11871 1626 : p->loop_names_hash = NULL;
11872 1626 : current_function_arg_info = p->arg_info;
11873 1626 : current_function_returns_value = p->returns_value;
11874 1626 : current_function_returns_null = p->returns_null;
11875 1626 : current_function_returns_abnormally = p->returns_abnormally;
11876 1626 : warn_about_return_type = p->warn_about_return_type;
11877 1626 : }
11878 :
11879 : /* The functions below are required for functionality of doing
11880 : function at once processing in the C front end. Currently these
11881 : functions are not called from anywhere in the C front end, but as
11882 : these changes continue, that will change. */
11883 :
11884 : /* Returns the stmt_tree (if any) to which statements are currently
11885 : being added. If there is no active statement-tree, NULL is
11886 : returned. */
11887 :
11888 : stmt_tree
11889 987403961 : current_stmt_tree (void)
11890 : {
11891 987403961 : return &c_stmt_tree;
11892 : }
11893 :
11894 : /* Return the global value of T as a symbol. */
11895 :
11896 : tree
11897 3949147 : identifier_global_value (tree t)
11898 : {
11899 3949147 : struct c_binding *b;
11900 :
11901 3950114 : for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
11902 3942885 : if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11903 3941918 : return b->decl;
11904 :
11905 : return NULL_TREE;
11906 : }
11907 :
11908 : /* Return the global value of tag T as a symbol. */
11909 :
11910 : tree
11911 12 : identifier_global_tag (tree t)
11912 : {
11913 12 : struct c_binding *b;
11914 :
11915 12 : for (b = I_TAG_BINDING (t); b; b = b->shadowed)
11916 11 : if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11917 11 : return b->decl;
11918 :
11919 : return NULL_TREE;
11920 : }
11921 :
11922 : /* Returns non-zero (result of __has_builtin) if NAME refers to a built-in
11923 : function or function-like operator. */
11924 :
11925 : int
11926 25168 : names_builtin_p (const char *name)
11927 : {
11928 25168 : tree id = get_identifier (name);
11929 25168 : if (tree decl = identifier_global_value (id))
11930 25083 : return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_UNDECLARED_BUILTIN (decl);
11931 :
11932 : /* Also detect common reserved C words that aren't strictly built-in
11933 : functions. */
11934 85 : switch (C_RID_CODE (id))
11935 : {
11936 : case RID_BUILTIN_ASSOC_BARRIER:
11937 : case RID_BUILTIN_CONVERTVECTOR:
11938 : case RID_BUILTIN_HAS_ATTRIBUTE:
11939 : case RID_BUILTIN_SHUFFLE:
11940 : case RID_BUILTIN_SHUFFLEVECTOR:
11941 : case RID_BUILTIN_STDC:
11942 : case RID_BUILTIN_COUNTED_BY_REF:
11943 : case RID_CHOOSE_EXPR:
11944 : case RID_OFFSETOF:
11945 : case RID_TYPES_COMPATIBLE_P:
11946 : case RID_C23_VA_START:
11947 : case RID_VA_ARG:
11948 : return 1;
11949 64 : default:
11950 64 : break;
11951 : }
11952 :
11953 64 : return 0;
11954 : }
11955 :
11956 : /* In C, the only C-linkage public declaration is at file scope. */
11957 :
11958 : tree
11959 5 : c_linkage_bindings (tree name)
11960 : {
11961 5 : return identifier_global_value (name);
11962 : }
11963 :
11964 : /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
11965 : otherwise the name is found in ridpointers from RID_INDEX. */
11966 :
11967 : void
11968 3354300 : record_builtin_type (enum rid rid_index, const char *name, tree type)
11969 : {
11970 3354300 : tree id, decl;
11971 3354300 : if (name == 0)
11972 1565340 : id = ridpointers[(int) rid_index];
11973 : else
11974 1788960 : id = get_identifier (name);
11975 3354300 : decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
11976 3354300 : pushdecl (decl);
11977 3354300 : if (debug_hooks->type_decl)
11978 3354300 : debug_hooks->type_decl (decl, false);
11979 3354300 : }
11980 :
11981 : /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
11982 :
11983 : struct c_parm *
11984 124800642 : build_c_parm (struct c_declspecs *specs, tree attrs,
11985 : struct c_declarator *declarator,
11986 : location_t loc)
11987 : {
11988 124800642 : struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
11989 124800642 : ret->specs = specs;
11990 124800642 : ret->attrs = attrs;
11991 124800642 : ret->declarator = declarator;
11992 124800642 : ret->loc = loc;
11993 124800642 : return ret;
11994 : }
11995 :
11996 : /* Return a declarator with nested attributes. TARGET is the inner
11997 : declarator to which these attributes apply. ATTRS are the
11998 : attributes. */
11999 :
12000 : struct c_declarator *
12001 6832 : build_attrs_declarator (tree attrs, struct c_declarator *target)
12002 : {
12003 6832 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
12004 6832 : ret->kind = cdk_attrs;
12005 6832 : ret->declarator = target;
12006 6832 : ret->u.attrs = attrs;
12007 6832 : return ret;
12008 : }
12009 :
12010 : /* Return a declarator for a function with arguments specified by ARGS
12011 : and return type specified by TARGET. */
12012 :
12013 : struct c_declarator *
12014 51033409 : build_function_declarator (struct c_arg_info *args,
12015 : struct c_declarator *target)
12016 : {
12017 51033409 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
12018 51033409 : ret->kind = cdk_function;
12019 51033409 : ret->declarator = target;
12020 51033409 : ret->u.arg_info = args;
12021 51033409 : return ret;
12022 : }
12023 :
12024 : /* Return a declarator for the identifier IDENT (which may be
12025 : NULL_TREE for an abstract declarator). */
12026 :
12027 : struct c_declarator *
12028 315519327 : build_id_declarator (tree ident)
12029 : {
12030 315519327 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
12031 315519327 : ret->kind = cdk_id;
12032 315519327 : ret->declarator = 0;
12033 315519327 : ret->u.id.id = ident;
12034 315519327 : ret->u.id.attrs = NULL_TREE;
12035 : /* Default value - may get reset to a more precise location. */
12036 315519327 : ret->id_loc = input_location;
12037 315519327 : return ret;
12038 : }
12039 :
12040 : /* Return something to represent absolute declarators containing a *.
12041 : TARGET is the absolute declarator that the * contains.
12042 : TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
12043 : to apply to the pointer type. */
12044 :
12045 : struct c_declarator *
12046 18383788 : make_pointer_declarator (struct c_declspecs *type_quals_attrs,
12047 : struct c_declarator *target)
12048 : {
12049 18383788 : tree attrs;
12050 18383788 : int quals = 0;
12051 18383788 : struct c_declarator *itarget = target;
12052 18383788 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
12053 18383788 : if (type_quals_attrs)
12054 : {
12055 18383788 : attrs = type_quals_attrs->attrs;
12056 18383788 : quals = quals_from_declspecs (type_quals_attrs);
12057 18383788 : if (attrs != NULL_TREE)
12058 6520 : itarget = build_attrs_declarator (attrs, target);
12059 : }
12060 18383788 : ret->kind = cdk_pointer;
12061 18383788 : ret->declarator = itarget;
12062 18383788 : ret->u.pointer_quals = quals;
12063 18383788 : return ret;
12064 : }
12065 :
12066 : /* Return a pointer to a structure for an empty list of declaration
12067 : specifiers. */
12068 :
12069 : struct c_declspecs *
12070 456686574 : build_null_declspecs (void)
12071 : {
12072 456686574 : struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
12073 456686574 : memset (ret, 0, sizeof *ret);
12074 456686574 : ret->align_log = -1;
12075 456686574 : ret->typespec_word = cts_none;
12076 456686574 : ret->storage_class = csc_none;
12077 456686574 : ret->expr_const_operands = true;
12078 456686574 : ret->typespec_kind = ctsk_none;
12079 456686574 : ret->address_space = ADDR_SPACE_GENERIC;
12080 456686574 : return ret;
12081 : }
12082 :
12083 : /* Add the address space ADDRSPACE to the declaration specifiers
12084 : SPECS, returning SPECS. */
12085 :
12086 : struct c_declspecs *
12087 177 : declspecs_add_addrspace (location_t location,
12088 : struct c_declspecs *specs, addr_space_t as)
12089 : {
12090 177 : specs->non_sc_seen_p = true;
12091 177 : specs->declspecs_seen_p = true;
12092 177 : specs->non_std_attrs_seen_p = true;
12093 :
12094 177 : if (!ADDR_SPACE_GENERIC_P (specs->address_space)
12095 0 : && specs->address_space != as)
12096 0 : error ("incompatible address space qualifiers %qs and %qs",
12097 : c_addr_space_name (as),
12098 : c_addr_space_name (specs->address_space));
12099 : else
12100 : {
12101 177 : specs->address_space = as;
12102 177 : specs->locations[cdw_address_space] = location;
12103 : }
12104 177 : return specs;
12105 : }
12106 :
12107 : /* Add the type qualifier QUAL to the declaration specifiers SPECS,
12108 : returning SPECS. */
12109 :
12110 : struct c_declspecs *
12111 16679465 : declspecs_add_qual (location_t loc,
12112 : struct c_declspecs *specs, tree qual)
12113 : {
12114 16679465 : enum rid i;
12115 16679465 : bool dupe = false;
12116 16679465 : specs->non_sc_seen_p = true;
12117 16679465 : specs->declspecs_seen_p = true;
12118 16679465 : specs->non_std_attrs_seen_p = true;
12119 16679465 : gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
12120 : && C_IS_RESERVED_WORD (qual));
12121 16679465 : i = C_RID_CODE (qual);
12122 16679465 : location_t prev_loc = UNKNOWN_LOCATION;
12123 16679465 : switch (i)
12124 : {
12125 13150157 : case RID_CONST:
12126 13150157 : dupe = specs->const_p;
12127 13150157 : specs->const_p = true;
12128 13150157 : prev_loc = specs->locations[cdw_const];
12129 13150157 : specs->locations[cdw_const] = loc;
12130 13150157 : break;
12131 101124 : case RID_VOLATILE:
12132 101124 : dupe = specs->volatile_p;
12133 101124 : specs->volatile_p = true;
12134 101124 : prev_loc = specs->locations[cdw_volatile];
12135 101124 : specs->locations[cdw_volatile] = loc;
12136 101124 : break;
12137 3407784 : case RID_RESTRICT:
12138 3407784 : dupe = specs->restrict_p;
12139 3407784 : specs->restrict_p = true;
12140 3407784 : prev_loc = specs->locations[cdw_restrict];
12141 3407784 : specs->locations[cdw_restrict] = loc;
12142 3407784 : break;
12143 20400 : case RID_ATOMIC:
12144 20400 : dupe = specs->atomic_p;
12145 20400 : specs->atomic_p = true;
12146 20400 : prev_loc = specs->locations[cdw_atomic];
12147 20400 : specs->locations[cdw_atomic] = loc;
12148 20400 : break;
12149 0 : default:
12150 0 : gcc_unreachable ();
12151 : }
12152 16679465 : if (dupe)
12153 : {
12154 56 : bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
12155 : "duplicate %qE declaration specifier", qual);
12156 56 : if (!warned
12157 52 : && warn_duplicate_decl_specifier
12158 33 : && prev_loc >= RESERVED_LOCATION_COUNT
12159 33 : && !from_macro_expansion_at (prev_loc)
12160 77 : && !from_macro_expansion_at (loc))
12161 12 : warning_at (loc, OPT_Wduplicate_decl_specifier,
12162 : "duplicate %qE declaration specifier", qual);
12163 : }
12164 16679465 : return specs;
12165 : }
12166 :
12167 : /* Add the type specifier TYPE to the declaration specifiers SPECS,
12168 : returning SPECS. */
12169 :
12170 : struct c_declspecs *
12171 333447208 : declspecs_add_type (location_t loc, struct c_declspecs *specs,
12172 : struct c_typespec spec)
12173 : {
12174 333447208 : tree type = spec.spec;
12175 333447208 : specs->non_sc_seen_p = true;
12176 333447208 : specs->declspecs_seen_p = true;
12177 333447208 : specs->non_std_attrs_seen_p = true;
12178 333447208 : specs->typespec_kind = spec.kind;
12179 333447208 : if (TREE_DEPRECATED (type))
12180 56 : specs->deprecated_p = true;
12181 333447208 : if (TREE_UNAVAILABLE (type))
12182 40 : specs->unavailable_p = true;
12183 :
12184 : /* As a type specifier is present, "auto" must be used as a storage
12185 : class specifier, not for type deduction. */
12186 333447208 : if (specs->c23_auto_p)
12187 : {
12188 115 : specs->c23_auto_p = false;
12189 115 : if (specs->storage_class != csc_none)
12190 1 : error ("multiple storage classes in declaration specifiers");
12191 114 : else if (specs->thread_p)
12192 1 : error ("%qs used with %<auto%>",
12193 1 : specs->thread_gnu_p ? "__thread" : "_Thread_local");
12194 113 : else if (specs->constexpr_p)
12195 : /* auto may only be used with another storage class specifier,
12196 : such as constexpr, if the type is inferred. */
12197 2 : error ("%<auto%> used with %<constexpr%>");
12198 : else
12199 111 : specs->storage_class = csc_auto;
12200 : }
12201 :
12202 : /* Handle type specifier keywords. */
12203 333447208 : if (TREE_CODE (type) == IDENTIFIER_NODE
12204 84112856 : && C_IS_RESERVED_WORD (type)
12205 417560064 : && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
12206 : {
12207 84112856 : enum rid i = C_RID_CODE (type);
12208 84112856 : if (specs->type)
12209 : {
12210 58 : error_at (loc, "two or more data types in declaration specifiers");
12211 58 : return specs;
12212 : }
12213 84112798 : if ((int) i <= (int) RID_LAST_MODIFIER)
12214 : {
12215 : /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
12216 22095477 : bool dupe = false;
12217 22095477 : switch (i)
12218 : {
12219 11051999 : case RID_LONG:
12220 11051999 : if (specs->long_long_p)
12221 : {
12222 104 : error_at (loc, "%<long long long%> is too long for GCC");
12223 104 : break;
12224 : }
12225 11051895 : if (specs->long_p)
12226 : {
12227 2969677 : if (specs->typespec_word == cts_double)
12228 : {
12229 15 : error_at (loc,
12230 : "both %qs and %qs in declaration specifiers",
12231 : "long long", "double");
12232 15 : break;
12233 : }
12234 2969662 : pedwarn_c90 (loc, OPT_Wlong_long,
12235 : "ISO C90 does not support %<long long%>");
12236 2969662 : specs->long_long_p = 1;
12237 2969662 : specs->locations[cdw_long_long] = loc;
12238 2969662 : break;
12239 : }
12240 8082218 : if (specs->short_p)
12241 77 : error_at (loc,
12242 : "both %qs and %qs in declaration specifiers",
12243 : "long", "short");
12244 8082141 : else if (specs->typespec_word == cts_auto_type)
12245 1 : error_at (loc,
12246 : "both %qs and %qs in declaration specifiers",
12247 : "long", "__auto_type");
12248 : else if (specs->typespec_word == cts_void)
12249 5 : error_at (loc,
12250 : "both %qs and %qs in declaration specifiers",
12251 : "long", "void");
12252 : else if (specs->typespec_word == cts_int_n)
12253 19 : error_at (loc,
12254 : "both %qs and %<__int%d%> in declaration specifiers",
12255 19 : "long", int_n_data[specs->u.int_n_idx].bitsize);
12256 : else if (specs->typespec_word == cts_bool)
12257 3 : error_at (loc,
12258 : "both %qs and %qs in declaration specifiers",
12259 : "long", "_Bool");
12260 : else if (specs->typespec_word == cts_bitint)
12261 3 : error_at (loc,
12262 : "both %qs and %qs in declaration specifiers",
12263 : "long", "_BitInt");
12264 : else if (specs->typespec_word == cts_char)
12265 21 : error_at (loc,
12266 : "both %qs and %qs in declaration specifiers",
12267 : "long", "char");
12268 : else if (specs->typespec_word == cts_float)
12269 7 : error_at (loc,
12270 : "both %qs and %qs in declaration specifiers",
12271 : "long", "float");
12272 : else if (specs->typespec_word == cts_floatn_nx)
12273 0 : error_at (loc,
12274 : "both %qs and %<_Float%d%s%> in declaration "
12275 : "specifiers", "long",
12276 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12277 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12278 : ? "x"
12279 : : "");
12280 : else if (specs->typespec_word == cts_dfloat32)
12281 4 : error_at (loc,
12282 : "both %qs and %qs in declaration specifiers",
12283 : "long", "_Decimal32");
12284 : else if (specs->typespec_word == cts_dfloat64)
12285 4 : error_at (loc,
12286 : "both %qs and %qs in declaration specifiers",
12287 : "long", "_Decimal64");
12288 : else if (specs->typespec_word == cts_dfloat128)
12289 4 : error_at (loc,
12290 : "both %qs and %qs in declaration specifiers",
12291 : "long", "_Decimal128");
12292 : else if (specs->typespec_word == cts_dfloat64x)
12293 0 : error_at (loc,
12294 : "both %qs and %qs in declaration specifiers",
12295 : "long", "_Decimal64x");
12296 : else
12297 : {
12298 8082070 : specs->long_p = true;
12299 8082070 : specs->locations[cdw_long] = loc;
12300 : }
12301 : break;
12302 1705254 : case RID_SHORT:
12303 1705254 : dupe = specs->short_p;
12304 1705254 : if (specs->long_p)
12305 197 : error_at (loc,
12306 : "both %qs and %qs in declaration specifiers",
12307 : "long", "short");
12308 1705057 : else if (specs->typespec_word == cts_auto_type)
12309 1 : error_at (loc,
12310 : "both %qs and %qs in declaration specifiers",
12311 : "short", "__auto_type");
12312 : else if (specs->typespec_word == cts_void)
12313 5 : error_at (loc,
12314 : "both %qs and %qs in declaration specifiers",
12315 : "short", "void");
12316 : else if (specs->typespec_word == cts_int_n)
12317 19 : error_at (loc,
12318 : "both %qs and %<__int%d%> in declaration specifiers",
12319 19 : "short", int_n_data[specs->u.int_n_idx].bitsize);
12320 : else if (specs->typespec_word == cts_bool)
12321 3 : error_at (loc,
12322 : "both %qs and %qs in declaration specifiers",
12323 : "short", "_Bool");
12324 : else if (specs->typespec_word == cts_bitint)
12325 1 : error_at (loc,
12326 : "both %qs and %qs in declaration specifiers",
12327 : "short", "_BitInt");
12328 : else if (specs->typespec_word == cts_char)
12329 21 : error_at (loc,
12330 : "both %qs and %qs in declaration specifiers",
12331 : "short", "char");
12332 : else if (specs->typespec_word == cts_float)
12333 7 : error_at (loc,
12334 : "both %qs and %qs in declaration specifiers",
12335 : "short", "float");
12336 : else if (specs->typespec_word == cts_double)
12337 7 : error_at (loc,
12338 : "both %qs and %qs in declaration specifiers",
12339 : "short", "double");
12340 : else if (specs->typespec_word == cts_floatn_nx)
12341 0 : error_at (loc,
12342 : "both %qs and %<_Float%d%s%> in declaration "
12343 : "specifiers", "short",
12344 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12345 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12346 : ? "x"
12347 : : "");
12348 : else if (specs->typespec_word == cts_dfloat32)
12349 4 : error_at (loc,
12350 : "both %qs and %qs in declaration specifiers",
12351 : "short", "_Decimal32");
12352 : else if (specs->typespec_word == cts_dfloat64)
12353 4 : error_at (loc,
12354 : "both %qs and %qs in declaration specifiers",
12355 : "short", "_Decimal64");
12356 : else if (specs->typespec_word == cts_dfloat128)
12357 4 : error_at (loc,
12358 : "both %qs and %qs in declaration specifiers",
12359 : "short", "_Decimal128");
12360 : else if (specs->typespec_word == cts_dfloat64x)
12361 0 : error_at (loc,
12362 : "both %qs and %qs in declaration specifiers",
12363 : "short", "_Decimal64x");
12364 : else
12365 : {
12366 1704981 : specs->short_p = true;
12367 1704981 : specs->locations[cdw_short] = loc;
12368 : }
12369 : break;
12370 553945 : case RID_SIGNED:
12371 553945 : dupe = specs->signed_p;
12372 553945 : if (specs->unsigned_p)
12373 138 : error_at (loc,
12374 : "both %qs and %qs in declaration specifiers",
12375 : "signed", "unsigned");
12376 553807 : else if (specs->typespec_word == cts_auto_type)
12377 1 : error_at (loc,
12378 : "both %qs and %qs in declaration specifiers",
12379 : "signed", "__auto_type");
12380 : else if (specs->typespec_word == cts_void)
12381 5 : error_at (loc,
12382 : "both %qs and %qs in declaration specifiers",
12383 : "signed", "void");
12384 : else if (specs->typespec_word == cts_bool)
12385 3 : error_at (loc,
12386 : "both %qs and %qs in declaration specifiers",
12387 : "signed", "_Bool");
12388 : else if (specs->typespec_word == cts_float)
12389 7 : error_at (loc,
12390 : "both %qs and %qs in declaration specifiers",
12391 : "signed", "float");
12392 : else if (specs->typespec_word == cts_double)
12393 21 : error_at (loc,
12394 : "both %qs and %qs in declaration specifiers",
12395 : "signed", "double");
12396 : else if (specs->typespec_word == cts_floatn_nx)
12397 0 : error_at (loc,
12398 : "both %qs and %<_Float%d%s%> in declaration "
12399 : "specifiers", "signed",
12400 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12401 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12402 : ? "x"
12403 : : "");
12404 : else if (specs->typespec_word == cts_dfloat32)
12405 4 : error_at (loc,
12406 : "both %qs and %qs in declaration specifiers",
12407 : "signed", "_Decimal32");
12408 : else if (specs->typespec_word == cts_dfloat64)
12409 4 : error_at (loc,
12410 : "both %qs and %qs in declaration specifiers",
12411 : "signed", "_Decimal64");
12412 : else if (specs->typespec_word == cts_dfloat128)
12413 4 : error_at (loc,
12414 : "both %qs and %qs in declaration specifiers",
12415 : "signed", "_Decimal128");
12416 : else if (specs->typespec_word == cts_dfloat64x)
12417 0 : error_at (loc,
12418 : "both %qs and %qs in declaration specifiers",
12419 : "signed", "_Decimal64x");
12420 : else
12421 : {
12422 553758 : specs->signed_p = true;
12423 553758 : specs->locations[cdw_signed] = loc;
12424 : }
12425 : break;
12426 6205451 : case RID_UNSIGNED:
12427 6205451 : dupe = specs->unsigned_p;
12428 6205451 : if (specs->signed_p)
12429 139 : error_at (loc,
12430 : "both %qs and %qs in declaration specifiers",
12431 : "signed", "unsigned");
12432 6205312 : else if (specs->typespec_word == cts_auto_type)
12433 1 : error_at (loc,
12434 : "both %qs and %qs in declaration specifiers",
12435 : "unsigned", "__auto_type");
12436 : else if (specs->typespec_word == cts_void)
12437 5 : error_at (loc,
12438 : "both %qs and %qs in declaration specifiers",
12439 : "unsigned", "void");
12440 : else if (specs->typespec_word == cts_bool)
12441 3 : error_at (loc,
12442 : "both %qs and %qs in declaration specifiers",
12443 : "unsigned", "_Bool");
12444 : else if (specs->typespec_word == cts_float)
12445 7 : error_at (loc,
12446 : "both %qs and %qs in declaration specifiers",
12447 : "unsigned", "float");
12448 : else if (specs->typespec_word == cts_double)
12449 21 : error_at (loc,
12450 : "both %qs and %qs in declaration specifiers",
12451 : "unsigned", "double");
12452 : else if (specs->typespec_word == cts_floatn_nx)
12453 0 : error_at (loc,
12454 : "both %qs and %<_Float%d%s%> in declaration "
12455 : "specifiers", "unsigned",
12456 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12457 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12458 : ? "x"
12459 : : "");
12460 : else if (specs->typespec_word == cts_dfloat32)
12461 2 : error_at (loc,
12462 : "both %qs and %qs in declaration specifiers",
12463 : "unsigned", "_Decimal32");
12464 : else if (specs->typespec_word == cts_dfloat64)
12465 2 : error_at (loc,
12466 : "both %qs and %qs in declaration specifiers",
12467 : "unsigned", "_Decimal64");
12468 : else if (specs->typespec_word == cts_dfloat128)
12469 2 : error_at (loc,
12470 : "both %qs and %qs in declaration specifiers",
12471 : "unsigned", "_Decimal128");
12472 : else if (specs->typespec_word == cts_dfloat64x)
12473 0 : error_at (loc,
12474 : "both %qs and %qs in declaration specifiers",
12475 : "unsigned", "_Decimal64x");
12476 : else
12477 : {
12478 6205269 : specs->unsigned_p = true;
12479 6205269 : specs->locations[cdw_unsigned] = loc;
12480 : }
12481 : break;
12482 2578769 : case RID_COMPLEX:
12483 2578769 : dupe = specs->complex_p;
12484 2578769 : if (!in_system_header_at (loc))
12485 69839 : pedwarn_c90 (loc, OPT_Wpedantic,
12486 : "ISO C90 does not support complex types");
12487 2578769 : if (specs->typespec_word == cts_auto_type)
12488 1 : error_at (loc,
12489 : "both %qs and %qs in declaration specifiers",
12490 : "complex", "__auto_type");
12491 : else if (specs->typespec_word == cts_void)
12492 2 : error_at (loc,
12493 : "both %qs and %qs in declaration specifiers",
12494 : "complex", "void");
12495 : else if (specs->typespec_word == cts_bool)
12496 2 : error_at (loc,
12497 : "both %qs and %qs in declaration specifiers",
12498 : "complex", "_Bool");
12499 : else if (specs->typespec_word == cts_bitint)
12500 1 : error_at (loc,
12501 : "both %qs and %qs in declaration specifiers",
12502 : "complex", "_BitInt");
12503 : else if (specs->typespec_word == cts_dfloat32)
12504 1 : error_at (loc,
12505 : "both %qs and %qs in declaration specifiers",
12506 : "complex", "_Decimal32");
12507 : else if (specs->typespec_word == cts_dfloat64)
12508 1 : error_at (loc,
12509 : "both %qs and %qs in declaration specifiers",
12510 : "complex", "_Decimal64");
12511 : else if (specs->typespec_word == cts_dfloat128)
12512 1 : error_at (loc,
12513 : "both %qs and %qs in declaration specifiers",
12514 : "complex", "_Decimal128");
12515 : else if (specs->typespec_word == cts_dfloat64x)
12516 0 : error_at (loc,
12517 : "both %qs and %qs in declaration specifiers",
12518 : "complex", "_Decimal64x");
12519 : else if (specs->typespec_word == cts_fract)
12520 0 : error_at (loc,
12521 : "both %qs and %qs in declaration specifiers",
12522 : "complex", "_Fract");
12523 : else if (specs->typespec_word == cts_accum)
12524 0 : error_at (loc,
12525 : "both %qs and %qs in declaration specifiers",
12526 : "complex", "_Accum");
12527 2578760 : else if (specs->saturating_p)
12528 0 : error_at (loc,
12529 : "both %qs and %qs in declaration specifiers",
12530 : "complex", "_Sat");
12531 : else
12532 : {
12533 2578760 : specs->complex_p = true;
12534 2578760 : specs->locations[cdw_complex] = loc;
12535 : }
12536 : break;
12537 59 : case RID_SAT:
12538 59 : dupe = specs->saturating_p;
12539 59 : pedwarn (loc, OPT_Wpedantic,
12540 : "ISO C does not support saturating types");
12541 59 : if (specs->typespec_word == cts_int_n)
12542 0 : error_at (loc,
12543 : "both %qs and %<__int%d%> in declaration specifiers",
12544 0 : "_Sat", int_n_data[specs->u.int_n_idx].bitsize);
12545 : else if (specs->typespec_word == cts_auto_type)
12546 0 : error_at (loc,
12547 : "both %qs and %qs in declaration specifiers",
12548 : "_Sat", "__auto_type");
12549 : else if (specs->typespec_word == cts_void)
12550 0 : error_at (loc,
12551 : "both %qs and %qs in declaration specifiers",
12552 : "_Sat", "void");
12553 : else if (specs->typespec_word == cts_bool)
12554 0 : error_at (loc,
12555 : "both %qs and %qs in declaration specifiers",
12556 : "_Sat", "_Bool");
12557 : else if (specs->typespec_word == cts_bitint)
12558 0 : error_at (loc,
12559 : "both %qs and %qs in declaration specifiers",
12560 : "_Sat", "_BitInt");
12561 : else if (specs->typespec_word == cts_char)
12562 0 : error_at (loc,
12563 : "both %qs and %qs in declaration specifiers",
12564 : "_Sat", "char");
12565 : else if (specs->typespec_word == cts_int)
12566 0 : error_at (loc,
12567 : "both %qs and %qs in declaration specifiers",
12568 : "_Sat", "int");
12569 : else if (specs->typespec_word == cts_float)
12570 0 : error_at (loc,
12571 : "both %qs and %qs in declaration specifiers",
12572 : "_Sat", "float");
12573 : else if (specs->typespec_word == cts_double)
12574 0 : error_at (loc,
12575 : "both %qs and %qs in declaration specifiers",
12576 : "_Sat", "double");
12577 : else if (specs->typespec_word == cts_floatn_nx)
12578 0 : error_at (loc,
12579 : "both %qs and %<_Float%d%s%> in declaration "
12580 : "specifiers", "_Sat",
12581 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12582 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12583 : ? "x"
12584 : : "");
12585 : else if (specs->typespec_word == cts_dfloat32)
12586 0 : error_at (loc,
12587 : "both %qs and %qs in declaration specifiers",
12588 : "_Sat", "_Decimal32");
12589 : else if (specs->typespec_word == cts_dfloat64)
12590 0 : error_at (loc,
12591 : "both %qs and %qs in declaration specifiers",
12592 : "_Sat", "_Decimal64");
12593 : else if (specs->typespec_word == cts_dfloat128)
12594 0 : error_at (loc,
12595 : "both %qs and %qs in declaration specifiers",
12596 : "_Sat", "_Decimal128");
12597 : else if (specs->typespec_word == cts_dfloat64x)
12598 0 : error_at (loc,
12599 : "both %qs and %qs in declaration specifiers",
12600 : "_Sat", "_Decimal64x");
12601 59 : else if (specs->complex_p)
12602 0 : error_at (loc,
12603 : "both %qs and %qs in declaration specifiers",
12604 : "_Sat", "complex");
12605 : else
12606 : {
12607 59 : specs->saturating_p = true;
12608 59 : specs->locations[cdw_saturating] = loc;
12609 : }
12610 : break;
12611 0 : default:
12612 0 : gcc_unreachable ();
12613 : }
12614 :
12615 22095477 : if (dupe)
12616 378 : error_at (loc, "duplicate %qE", type);
12617 :
12618 22095477 : return specs;
12619 : }
12620 : else
12621 : {
12622 : /* "void", "_Bool", "char", "int", "float", "double",
12623 : "_FloatN", "_FloatNx", "_Decimal32", "__intN",
12624 : "_Decimal64", "_Decimal128", "_Fract", "_Accum", "_BitInt(N)" or
12625 : "__auto_type". */
12626 62017321 : if (specs->typespec_word != cts_none)
12627 : {
12628 2171 : if (i == RID_BOOL)
12629 : {
12630 175 : auto_diagnostic_group d;
12631 175 : if (specs->storage_class == csc_typedef)
12632 4 : error_at (loc,
12633 : "%qs cannot be defined via %<typedef%>",
12634 2 : IDENTIFIER_POINTER (type));
12635 : else
12636 346 : error_at (loc,
12637 : "%qs cannot be used here",
12638 173 : IDENTIFIER_POINTER (type));
12639 175 : add_note_about_new_keyword (loc, type);
12640 175 : }
12641 : else
12642 1996 : error_at (loc,
12643 : "two or more data types in declaration specifiers");
12644 2171 : return specs;
12645 : }
12646 62015150 : switch (i)
12647 : {
12648 1890 : case RID_AUTO_TYPE:
12649 1890 : if (specs->long_p)
12650 1 : error_at (loc,
12651 : "both %qs and %qs in declaration specifiers",
12652 : "long", "__auto_type");
12653 1889 : else if (specs->short_p)
12654 1 : error_at (loc,
12655 : "both %qs and %qs in declaration specifiers",
12656 : "short", "__auto_type");
12657 1888 : else if (specs->signed_p)
12658 1 : error_at (loc,
12659 : "both %qs and %qs in declaration specifiers",
12660 : "signed", "__auto_type");
12661 1887 : else if (specs->unsigned_p)
12662 1 : error_at (loc,
12663 : "both %qs and %qs in declaration specifiers",
12664 : "unsigned", "__auto_type");
12665 1886 : else if (specs->complex_p)
12666 1 : error_at (loc,
12667 : "both %qs and %qs in declaration specifiers",
12668 : "complex", "__auto_type");
12669 1885 : else if (specs->saturating_p)
12670 0 : error_at (loc,
12671 : "both %qs and %qs in declaration specifiers",
12672 : "_Sat", "__auto_type");
12673 : else
12674 : {
12675 1885 : specs->typespec_word = cts_auto_type;
12676 1885 : specs->locations[cdw_typespec] = loc;
12677 : }
12678 1890 : return specs;
12679 51888 : case RID_INT_N_0:
12680 51888 : case RID_INT_N_1:
12681 51888 : case RID_INT_N_2:
12682 51888 : case RID_INT_N_3:
12683 51888 : specs->u.int_n_idx = i - RID_INT_N_0;
12684 51888 : if (!in_system_header_at (input_location)
12685 : /* If the INT_N type ends in "__", and so is of the format
12686 : "__intN__", don't pedwarn. */
12687 51888 : && (strncmp (IDENTIFIER_POINTER (type)
12688 41232 : + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
12689 41232 : pedwarn (loc, OPT_Wpedantic,
12690 : "ISO C does not support %<__int%d%> types",
12691 41232 : int_n_data[specs->u.int_n_idx].bitsize);
12692 :
12693 51888 : if (specs->long_p)
12694 53 : error_at (loc,
12695 : "both %<__int%d%> and %qs in declaration specifiers",
12696 53 : int_n_data[specs->u.int_n_idx].bitsize, "long");
12697 51835 : else if (specs->saturating_p)
12698 0 : error_at (loc,
12699 : "both %qs and %<__int%d%> in declaration specifiers",
12700 0 : "_Sat", int_n_data[specs->u.int_n_idx].bitsize);
12701 51835 : else if (specs->short_p)
12702 19 : error_at (loc,
12703 : "both %<__int%d%> and %qs in declaration specifiers",
12704 19 : int_n_data[specs->u.int_n_idx].bitsize, "short");
12705 51816 : else if (! int_n_enabled_p[specs->u.int_n_idx])
12706 : {
12707 0 : specs->typespec_word = cts_int_n;
12708 0 : error_at (loc,
12709 : "%<__int%d%> is not supported on this target",
12710 0 : int_n_data[specs->u.int_n_idx].bitsize);
12711 : }
12712 : else
12713 : {
12714 51816 : specs->typespec_word = cts_int_n;
12715 51816 : specs->locations[cdw_typespec] = loc;
12716 : }
12717 51888 : return specs;
12718 8000308 : case RID_VOID:
12719 8000308 : if (specs->long_p)
12720 44 : error_at (loc,
12721 : "both %qs and %qs in declaration specifiers",
12722 : "long", "void");
12723 8000264 : else if (specs->short_p)
12724 21 : error_at (loc,
12725 : "both %qs and %qs in declaration specifiers",
12726 : "short", "void");
12727 8000243 : else if (specs->signed_p)
12728 5 : error_at (loc,
12729 : "both %qs and %qs in declaration specifiers",
12730 : "signed", "void");
12731 8000238 : else if (specs->unsigned_p)
12732 5 : error_at (loc,
12733 : "both %qs and %qs in declaration specifiers",
12734 : "unsigned", "void");
12735 8000233 : else if (specs->complex_p)
12736 2 : error_at (loc,
12737 : "both %qs and %qs in declaration specifiers",
12738 : "complex", "void");
12739 8000231 : else if (specs->saturating_p)
12740 0 : error_at (loc,
12741 : "both %qs and %qs in declaration specifiers",
12742 : "_Sat", "void");
12743 : else
12744 : {
12745 8000231 : specs->typespec_word = cts_void;
12746 8000231 : specs->locations[cdw_typespec] = loc;
12747 : }
12748 8000308 : return specs;
12749 91990 : case RID_BOOL:
12750 91990 : if (!in_system_header_at (loc))
12751 73807 : pedwarn_c90 (loc, OPT_Wpedantic,
12752 : "ISO C90 does not support boolean types");
12753 91990 : if (specs->long_p)
12754 27 : error_at (loc,
12755 : "both %qs and %qs in declaration specifiers",
12756 : "long", "_Bool");
12757 91963 : else if (specs->short_p)
12758 11 : error_at (loc,
12759 : "both %qs and %qs in declaration specifiers",
12760 : "short", "_Bool");
12761 91952 : else if (specs->signed_p)
12762 3 : error_at (loc,
12763 : "both %qs and %qs in declaration specifiers",
12764 : "signed", "_Bool");
12765 91949 : else if (specs->unsigned_p)
12766 3 : error_at (loc,
12767 : "both %qs and %qs in declaration specifiers",
12768 : "unsigned", "_Bool");
12769 91946 : else if (specs->complex_p)
12770 2 : error_at (loc,
12771 : "both %qs and %qs in declaration specifiers",
12772 : "complex", "_Bool");
12773 91944 : else if (specs->saturating_p)
12774 0 : error_at (loc,
12775 : "both %qs and %qs in declaration specifiers",
12776 : "_Sat", "_Bool");
12777 : else
12778 : {
12779 91944 : specs->typespec_word = cts_bool;
12780 91944 : specs->locations[cdw_typespec] = loc;
12781 : }
12782 91990 : return specs;
12783 9063526 : case RID_CHAR:
12784 9063526 : if (specs->long_p)
12785 44 : error_at (loc,
12786 : "both %qs and %qs in declaration specifiers",
12787 : "long", "char");
12788 9063482 : else if (specs->short_p)
12789 21 : error_at (loc,
12790 : "both %qs and %qs in declaration specifiers",
12791 : "short", "char");
12792 9063461 : else if (specs->saturating_p)
12793 0 : error_at (loc,
12794 : "both %qs and %qs in declaration specifiers",
12795 : "_Sat", "char");
12796 : else
12797 : {
12798 9063461 : specs->typespec_word = cts_char;
12799 9063461 : specs->locations[cdw_typespec] = loc;
12800 : }
12801 9063526 : return specs;
12802 24037709 : case RID_INT:
12803 24037709 : if (specs->saturating_p)
12804 0 : error_at (loc,
12805 : "both %qs and %qs in declaration specifiers",
12806 : "_Sat", "int");
12807 : else
12808 : {
12809 24037709 : specs->typespec_word = cts_int;
12810 24037709 : specs->locations[cdw_typespec] = loc;
12811 : }
12812 24037709 : return specs;
12813 3633242 : case RID_FLOAT:
12814 3633242 : if (specs->long_p)
12815 44 : error_at (loc,
12816 : "both %qs and %qs in declaration specifiers",
12817 : "long", "float");
12818 3633198 : else if (specs->short_p)
12819 21 : error_at (loc,
12820 : "both %qs and %qs in declaration specifiers",
12821 : "short", "float");
12822 3633177 : else if (specs->signed_p)
12823 5 : error_at (loc,
12824 : "both %qs and %qs in declaration specifiers",
12825 : "signed", "float");
12826 3633172 : else if (specs->unsigned_p)
12827 5 : error_at (loc,
12828 : "both %qs and %qs in declaration specifiers",
12829 : "unsigned", "float");
12830 3633167 : else if (specs->saturating_p)
12831 0 : error_at (loc,
12832 : "both %qs and %qs in declaration specifiers",
12833 : "_Sat", "float");
12834 : else
12835 : {
12836 3633167 : specs->typespec_word = cts_float;
12837 3633167 : specs->locations[cdw_typespec] = loc;
12838 : }
12839 3633242 : return specs;
12840 6609397 : case RID_DOUBLE:
12841 6609397 : if (specs->long_long_p)
12842 22 : error_at (loc,
12843 : "both %qs and %qs in declaration specifiers",
12844 : "long long", "double");
12845 6609375 : else if (specs->short_p)
12846 21 : error_at (loc,
12847 : "both %qs and %qs in declaration specifiers",
12848 : "short", "double");
12849 6609354 : else if (specs->signed_p)
12850 13 : error_at (loc,
12851 : "both %qs and %qs in declaration specifiers",
12852 : "signed", "double");
12853 6609341 : else if (specs->unsigned_p)
12854 13 : error_at (loc,
12855 : "both %qs and %qs in declaration specifiers",
12856 : "unsigned", "double");
12857 6609328 : else if (specs->saturating_p)
12858 0 : error_at (loc,
12859 : "both %qs and %qs in declaration specifiers",
12860 : "_Sat", "double");
12861 : else
12862 : {
12863 6609328 : specs->typespec_word = cts_double;
12864 6609328 : specs->locations[cdw_typespec] = loc;
12865 : }
12866 6609397 : return specs;
12867 10430779 : CASE_RID_FLOATN_NX:
12868 10430779 : specs->u.floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
12869 10430779 : if (!in_system_header_at (input_location))
12870 53768 : pedwarn_c11 (loc, OPT_Wpedantic,
12871 : "ISO C does not support the %<_Float%d%s%> type"
12872 : " before C23",
12873 53768 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12874 53768 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12875 : ? "x"
12876 : : "");
12877 :
12878 10430779 : if (specs->long_p)
12879 0 : error_at (loc,
12880 : "both %qs and %<_Float%d%s%> in declaration "
12881 : "specifiers", "long",
12882 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12883 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12884 : ? "x"
12885 : : "");
12886 10430779 : else if (specs->short_p)
12887 0 : error_at (loc,
12888 : "both %qs and %<_Float%d%s%> in declaration "
12889 : "specifiers", "short",
12890 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12891 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12892 : ? "x"
12893 : : "");
12894 10430779 : else if (specs->signed_p)
12895 0 : error_at (loc,
12896 : "both %qs and %<_Float%d%s%> in declaration "
12897 : "specifiers", "signed",
12898 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12899 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12900 : ? "x"
12901 : : "");
12902 10430779 : else if (specs->unsigned_p)
12903 0 : error_at (loc,
12904 : "both %qs and %<_Float%d%s%> in declaration "
12905 : "specifiers", "unsigned",
12906 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12907 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12908 : ? "x"
12909 : : "");
12910 10430779 : else if (specs->saturating_p)
12911 0 : error_at (loc,
12912 : "both %qs and %<_Float%d%s%> in declaration "
12913 : "specifiers", "_Sat",
12914 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12915 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12916 : ? "x"
12917 : : "");
12918 10430779 : else if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
12919 : {
12920 88 : specs->typespec_word = cts_floatn_nx;
12921 176 : error_at (loc,
12922 : "%<_Float%d%s%> is not supported on this target",
12923 88 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12924 88 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12925 : ? "x"
12926 : : "");
12927 : }
12928 : else
12929 : {
12930 10430691 : specs->typespec_word = cts_floatn_nx;
12931 10430691 : specs->locations[cdw_typespec] = loc;
12932 : }
12933 10430779 : return specs;
12934 47639 : case RID_DFLOAT32:
12935 47639 : case RID_DFLOAT64:
12936 47639 : case RID_DFLOAT128:
12937 47639 : case RID_DFLOAT64X:
12938 47639 : {
12939 47639 : const char *str;
12940 47639 : if (i == RID_DFLOAT32)
12941 : str = "_Decimal32";
12942 : else if (i == RID_DFLOAT64)
12943 : str = "_Decimal64";
12944 : else if (i == RID_DFLOAT128)
12945 : str = "_Decimal128";
12946 : else
12947 47639 : str = "_Decimal64x";
12948 47639 : if (specs->long_long_p)
12949 18 : error_at (loc,
12950 : "both %qs and %qs in declaration specifiers",
12951 : "long long", str);
12952 47639 : if (specs->long_p)
12953 33 : error_at (loc,
12954 : "both %qs and %qs in declaration specifiers",
12955 : "long", str);
12956 47606 : else if (specs->short_p)
12957 18 : error_at (loc,
12958 : "both %qs and %qs in declaration specifiers",
12959 : "short", str);
12960 47588 : else if (specs->signed_p)
12961 6 : error_at (loc,
12962 : "both %qs and %qs in declaration specifiers",
12963 : "signed", str);
12964 47582 : else if (specs->unsigned_p)
12965 3 : error_at (loc,
12966 : "both %qs and %qs in declaration specifiers",
12967 : "unsigned", str);
12968 47579 : else if (specs->complex_p)
12969 3 : error_at (loc,
12970 : "both %qs and %qs in declaration specifiers",
12971 : "complex", str);
12972 47576 : else if (specs->saturating_p)
12973 0 : error_at (loc,
12974 : "both %qs and %qs in declaration specifiers",
12975 : "_Sat", str);
12976 47576 : else if (i == RID_DFLOAT32)
12977 15957 : specs->typespec_word = cts_dfloat32;
12978 31619 : else if (i == RID_DFLOAT64)
12979 15838 : specs->typespec_word = cts_dfloat64;
12980 15781 : else if (i == RID_DFLOAT128)
12981 15734 : specs->typespec_word = cts_dfloat128;
12982 : else
12983 47 : specs->typespec_word = cts_dfloat64x;
12984 47639 : specs->locations[cdw_typespec] = loc;
12985 : }
12986 47639 : if (!targetm.decimal_float_supported_p ())
12987 0 : error_at (loc,
12988 : "decimal floating-point not supported "
12989 : "for this target");
12990 47639 : pedwarn_c11 (loc, OPT_Wpedantic,
12991 : "ISO C does not support decimal floating-point "
12992 : "before C23");
12993 47639 : return specs;
12994 61 : case RID_FRACT:
12995 61 : case RID_ACCUM:
12996 61 : {
12997 61 : const char *str;
12998 61 : if (i == RID_FRACT)
12999 : str = "_Fract";
13000 : else
13001 30 : str = "_Accum";
13002 61 : if (specs->complex_p)
13003 0 : error_at (loc,
13004 : "both %qs and %qs in declaration specifiers",
13005 : "complex", str);
13006 61 : else if (i == RID_FRACT)
13007 31 : specs->typespec_word = cts_fract;
13008 : else
13009 30 : specs->typespec_word = cts_accum;
13010 61 : specs->locations[cdw_typespec] = loc;
13011 : }
13012 61 : if (!targetm.fixed_point_supported_p ())
13013 61 : error_at (loc,
13014 : "fixed-point types not supported for this target");
13015 61 : pedwarn (loc, OPT_Wpedantic,
13016 : "ISO C does not support fixed-point types");
13017 61 : return specs;
13018 46721 : case RID_BITINT:
13019 46721 : if (specs->long_p)
13020 2 : error_at (loc,
13021 : "both %qs and %qs in declaration specifiers",
13022 : "long", "_BitInt");
13023 46719 : else if (specs->short_p)
13024 1 : error_at (loc,
13025 : "both %qs and %qs in declaration specifiers",
13026 : "short", "_BitInt");
13027 46718 : else if (specs->complex_p)
13028 1 : error_at (loc,
13029 : "both %qs and %qs in declaration specifiers",
13030 : "complex", "_BitInt");
13031 46717 : else if (specs->saturating_p)
13032 0 : error_at (loc,
13033 : "both %qs and %qs in declaration specifiers",
13034 : "_Sat", "_BitInt");
13035 : else
13036 : {
13037 46717 : specs->typespec_word = cts_bitint;
13038 46717 : specs->locations[cdw_typespec] = loc;
13039 46717 : specs->u.bitint_prec = -1;
13040 46717 : if (error_operand_p (spec.expr))
13041 7 : return specs;
13042 46717 : if (TREE_CODE (spec.expr) != INTEGER_CST
13043 46717 : || !INTEGRAL_TYPE_P (TREE_TYPE (spec.expr)))
13044 : {
13045 1 : error_at (loc, "%<_BitInt%> argument is not an integer "
13046 : "constant expression");
13047 1 : return specs;
13048 : }
13049 46716 : if (tree_int_cst_sgn (spec.expr) <= 0)
13050 : {
13051 4 : error_at (loc, "%<_BitInt%> argument %qE is not a "
13052 : "positive integer constant expression",
13053 : spec.expr);
13054 4 : return specs;
13055 : }
13056 46712 : if (wi::to_widest (spec.expr) > WIDE_INT_MAX_PRECISION - 1)
13057 : {
13058 2 : error_at (loc, "%<_BitInt%> argument %qE is larger than "
13059 : "%<BITINT_MAXWIDTH%> %qd",
13060 : spec.expr, (int) WIDE_INT_MAX_PRECISION - 1);
13061 2 : return specs;
13062 : }
13063 46710 : specs->u.bitint_prec = tree_to_uhwi (spec.expr);
13064 46710 : struct bitint_info info;
13065 46710 : if (!targetm.c.bitint_type_info (specs->u.bitint_prec,
13066 : &info))
13067 : {
13068 0 : sorry_at (loc, "%<_BitInt(%d)%> is not supported on "
13069 : "this target", specs->u.bitint_prec);
13070 0 : specs->u.bitint_prec = -1;
13071 0 : return specs;
13072 : }
13073 : }
13074 46714 : return specs;
13075 : default:
13076 : /* ObjC reserved word "id", handled below. */
13077 : break;
13078 : }
13079 : }
13080 : }
13081 :
13082 : /* Now we have a typedef (a TYPE_DECL node), an identifier (some
13083 : form of ObjC type, cases such as "int" and "long" being handled
13084 : above), a TYPE (struct, union, enum and typeof specifiers) or an
13085 : ERROR_MARK. In none of these cases may there have previously
13086 : been any type specifiers. */
13087 249334352 : if (specs->type || specs->typespec_word != cts_none
13088 249334340 : || specs->long_p || specs->short_p || specs->signed_p
13089 249334337 : || specs->unsigned_p || specs->complex_p)
13090 21 : error_at (loc, "two or more data types in declaration specifiers");
13091 249334331 : else if (TREE_CODE (type) == TYPE_DECL)
13092 : {
13093 246036270 : specs->type = TREE_TYPE (type);
13094 246036270 : if (TREE_TYPE (type) != error_mark_node)
13095 : {
13096 246036255 : mark_decl_used (type, false);
13097 246036255 : specs->decl_attr = DECL_ATTRIBUTES (type);
13098 246036255 : specs->typedef_p = true;
13099 246036255 : specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
13100 246036255 : specs->locations[cdw_typedef] = loc;
13101 :
13102 : /* If this typedef name is defined in a struct, then a C++
13103 : lookup would return a different value. */
13104 246036255 : if (warn_cxx_compat
13105 246036255 : && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
13106 2 : warning_at (loc, OPT_Wc___compat,
13107 : "C++ lookup of %qD would return a field, not a type",
13108 : type);
13109 :
13110 : /* If we are parsing a struct, record that a struct field
13111 : used a typedef. */
13112 246036255 : if (warn_cxx_compat && struct_parse_info != NULL)
13113 1766 : struct_parse_info->typedefs_seen.safe_push (type);
13114 : }
13115 : }
13116 3298061 : else if (TREE_CODE (type) == IDENTIFIER_NODE)
13117 : {
13118 0 : tree t = lookup_name (type);
13119 0 : if (!t || TREE_CODE (t) != TYPE_DECL)
13120 0 : error_at (loc, "%qE fails to be a typedef or built in type", type);
13121 0 : else if (TREE_TYPE (t) == error_mark_node)
13122 : ;
13123 : else
13124 : {
13125 0 : specs->type = TREE_TYPE (t);
13126 0 : specs->locations[cdw_typespec] = loc;
13127 : }
13128 : }
13129 : else
13130 : {
13131 3298061 : if (TREE_CODE (type) != ERROR_MARK)
13132 : {
13133 3297856 : if (spec.kind == ctsk_typeof)
13134 : {
13135 834830 : specs->typedef_p = true;
13136 834830 : specs->locations[cdw_typedef] = loc;
13137 : }
13138 :
13139 3297856 : if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
13140 1069903 : || TREE_CODE (type) == ENUMERAL_TYPE)
13141 2471621 : mark_decl_used (TYPE_STUB_DECL (type), false);
13142 :
13143 3297856 : if (spec.expr)
13144 : {
13145 957 : tree expr = save_expr (fold_convert (void_type_node, spec.expr));
13146 957 : if (specs->expr)
13147 0 : specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr),
13148 : specs->expr, expr);
13149 : else
13150 957 : specs->expr = expr;
13151 957 : specs->expr_const_operands &= spec.expr_const_operands;
13152 : }
13153 : }
13154 3298061 : specs->type = type;
13155 3298061 : if (spec.has_enum_type_specifier
13156 190 : && spec.kind != ctsk_tagdef)
13157 49 : specs->enum_type_specifier_ref_p = true;
13158 : }
13159 :
13160 : return specs;
13161 : }
13162 :
13163 : /* Add the storage class specifier or function specifier SCSPEC to the
13164 : declaration specifiers SPECS, returning SPECS. */
13165 :
13166 : struct c_declspecs *
13167 90879045 : declspecs_add_scspec (location_t loc,
13168 : struct c_declspecs *specs,
13169 : tree scspec)
13170 : {
13171 90879045 : enum rid i;
13172 90879045 : enum c_storage_class n = csc_none;
13173 90879045 : bool dupe = false;
13174 90879045 : specs->declspecs_seen_p = true;
13175 90879045 : specs->non_std_attrs_seen_p = true;
13176 90879045 : gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
13177 : && C_IS_RESERVED_WORD (scspec));
13178 90879045 : i = C_RID_CODE (scspec);
13179 90879045 : if (specs->non_sc_seen_p)
13180 2111 : warning (OPT_Wold_style_declaration,
13181 : "%qE is not at beginning of declaration", scspec);
13182 90879045 : switch (i)
13183 : {
13184 35658906 : case RID_INLINE:
13185 : /* C99 permits duplicate inline. Although of doubtful utility,
13186 : it seems simplest to permit it in gnu89 mode as well, as
13187 : there is also little utility in maintaining this as a
13188 : difference between gnu89 and C99 inline. */
13189 35658906 : dupe = false;
13190 35658906 : specs->inline_p = true;
13191 35658906 : specs->locations[cdw_inline] = loc;
13192 35658906 : break;
13193 23317 : case RID_NORETURN:
13194 : /* Duplicate _Noreturn is permitted. */
13195 23317 : dupe = false;
13196 23317 : specs->noreturn_p = true;
13197 23317 : specs->locations[cdw_noreturn] = loc;
13198 23317 : break;
13199 2875 : case RID_THREAD:
13200 2875 : dupe = specs->thread_p;
13201 2875 : if (specs->storage_class == csc_auto)
13202 2 : error ("%qE used with %<auto%>", scspec);
13203 2873 : else if (specs->storage_class == csc_register)
13204 3 : error ("%qE used with %<register%>", scspec);
13205 2870 : else if (specs->storage_class == csc_typedef)
13206 2 : error ("%qE used with %<typedef%>", scspec);
13207 2868 : else if (specs->constexpr_p)
13208 2 : error ("%qE used with %<constexpr%>", scspec);
13209 : else
13210 : {
13211 2866 : specs->thread_p = true;
13212 2866 : specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
13213 2866 : "__thread") == 0);
13214 : /* A diagnostic is not required for the use of this
13215 : identifier in the implementation namespace; only diagnose
13216 : it for the C11 spelling because of existing code using
13217 : the other spelling. */
13218 2866 : if (!specs->thread_gnu_p)
13219 : {
13220 116 : if (flag_isoc99)
13221 113 : pedwarn_c99 (loc, OPT_Wpedantic,
13222 : "ISO C99 does not support %qE", scspec);
13223 : else
13224 3 : pedwarn_c99 (loc, OPT_Wpedantic,
13225 : "ISO C90 does not support %qE", scspec);
13226 : }
13227 2866 : specs->locations[cdw_thread] = loc;
13228 : }
13229 : break;
13230 264 : case RID_AUTO:
13231 264 : if (flag_isoc23
13232 225 : && specs->typespec_kind == ctsk_none
13233 214 : && specs->storage_class != csc_typedef)
13234 : {
13235 : /* "auto" potentially used for type deduction. */
13236 213 : if (specs->c23_auto_p)
13237 2 : error ("duplicate %qE", scspec);
13238 213 : specs->c23_auto_p = true;
13239 213 : return specs;
13240 : }
13241 51 : n = csc_auto;
13242 : /* auto may only be used with another storage class specifier,
13243 : such as constexpr, if the type is inferred. */
13244 51 : if (specs->constexpr_p)
13245 2 : error ("%qE used with %<constexpr%>", scspec);
13246 : break;
13247 50429770 : case RID_EXTERN:
13248 50429770 : n = csc_extern;
13249 : /* Diagnose "__thread extern". */
13250 50429770 : if (specs->thread_p && specs->thread_gnu_p)
13251 2 : error ("%<__thread%> before %<extern%>");
13252 : break;
13253 : case RID_REGISTER:
13254 : n = csc_register;
13255 : break;
13256 427945 : case RID_STATIC:
13257 427945 : n = csc_static;
13258 : /* Diagnose "__thread static". */
13259 427945 : if (specs->thread_p && specs->thread_gnu_p)
13260 1 : error ("%<__thread%> before %<static%>");
13261 : break;
13262 4332180 : case RID_TYPEDEF:
13263 4332180 : n = csc_typedef;
13264 4332180 : if (specs->c23_auto_p)
13265 : {
13266 1 : error ("%<typedef%> used with %<auto%>");
13267 1 : specs->c23_auto_p = false;
13268 : }
13269 : break;
13270 620 : case RID_CONSTEXPR:
13271 620 : dupe = specs->constexpr_p;
13272 620 : if (specs->storage_class == csc_extern)
13273 1 : error ("%qE used with %<extern%>", scspec);
13274 619 : else if (specs->storage_class == csc_typedef)
13275 1 : error ("%qE used with %<typedef%>", scspec);
13276 618 : else if (specs->storage_class == csc_auto)
13277 : /* auto may only be used with another storage class specifier,
13278 : such as constexpr, if the type is inferred. */
13279 2 : error ("%qE used with %<auto%>", scspec);
13280 616 : else if (specs->thread_p)
13281 4 : error ("%qE used with %qs", scspec,
13282 2 : specs->thread_gnu_p ? "__thread" : "_Thread_local");
13283 : else
13284 614 : specs->constexpr_p = true;
13285 : break;
13286 0 : default:
13287 0 : gcc_unreachable ();
13288 : }
13289 90878838 : if (n != csc_none && n == specs->storage_class)
13290 : dupe = true;
13291 90878824 : if (dupe)
13292 : {
13293 12 : if (i == RID_THREAD)
13294 2 : error ("duplicate %<_Thread_local%> or %<__thread%>");
13295 : else
13296 10 : error ("duplicate %qE", scspec);
13297 : }
13298 90878832 : if (n != csc_none)
13299 : {
13300 55193114 : if (specs->storage_class != csc_none && n != specs->storage_class)
13301 : {
13302 7 : error ("multiple storage classes in declaration specifiers");
13303 : }
13304 : else
13305 : {
13306 55193107 : specs->storage_class = n;
13307 55193107 : specs->locations[cdw_storage_class] = loc;
13308 55193107 : if (n != csc_extern && n != csc_static && specs->thread_p)
13309 : {
13310 8 : error ("%qs used with %qE",
13311 8 : specs->thread_gnu_p ? "__thread" : "_Thread_local",
13312 : scspec);
13313 8 : specs->thread_p = false;
13314 : }
13315 55193107 : if (n != csc_auto && n != csc_register && n != csc_static
13316 54761947 : && specs->constexpr_p)
13317 : {
13318 2 : error ("%<constexpr%> used with %qE", scspec);
13319 2 : specs->constexpr_p = false;
13320 : }
13321 : }
13322 : }
13323 : return specs;
13324 : }
13325 :
13326 : /* Add the attributes ATTRS to the declaration specifiers SPECS,
13327 : returning SPECS. */
13328 :
13329 : struct c_declspecs *
13330 35970890 : declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
13331 : {
13332 35970890 : specs->attrs = chainon (attrs, specs->attrs);
13333 35970890 : specs->locations[cdw_attributes] = loc;
13334 35970890 : specs->declspecs_seen_p = true;
13335 : /* In the case of standard attributes at the start of the
13336 : declaration, the caller will reset this. */
13337 35970890 : specs->non_std_attrs_seen_p = true;
13338 35970890 : return specs;
13339 : }
13340 :
13341 : /* Add an _Alignas specifier (expression ALIGN, or type whose
13342 : alignment is ALIGN) to the declaration specifiers SPECS, returning
13343 : SPECS. */
13344 : struct c_declspecs *
13345 202 : declspecs_add_alignas (location_t loc,
13346 : struct c_declspecs *specs, tree align)
13347 : {
13348 202 : specs->alignas_p = true;
13349 202 : specs->locations[cdw_alignas] = loc;
13350 202 : if (align == error_mark_node)
13351 : return specs;
13352 :
13353 : /* Only accept the alignment if it's valid and greater than
13354 : the current one. Zero is invalid but by C11 required to
13355 : be silently ignored. */
13356 200 : int align_log = check_user_alignment (align, false, /* warn_zero = */false);
13357 200 : if (align_log > specs->align_log)
13358 167 : specs->align_log = align_log;
13359 : return specs;
13360 : }
13361 :
13362 : /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
13363 : specifiers with any other type specifier to determine the resulting
13364 : type. This is where ISO C checks on complex types are made, since
13365 : "_Complex long" is a prefix of the valid ISO C type "_Complex long
13366 : double". Also apply postfix standard attributes to modify the type. */
13367 :
13368 : struct c_declspecs *
13369 315543013 : finish_declspecs (struct c_declspecs *specs)
13370 : {
13371 : /* If a type was specified as a whole, we have no modifiers and are
13372 : done. */
13373 315543013 : if (specs->type != NULL_TREE)
13374 : {
13375 249334285 : gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
13376 : && !specs->signed_p && !specs->unsigned_p
13377 : && !specs->complex_p && !specs->c23_auto_p);
13378 :
13379 : /* Set a dummy type. */
13380 249334285 : if (TREE_CODE (specs->type) == ERROR_MARK)
13381 183 : specs->type = integer_type_node;
13382 249334285 : goto handle_postfix_attrs;
13383 : }
13384 :
13385 : /* If none of "void", "_Bool", "char", "int", "float" or "double"
13386 : has been specified, treat it as "int" unless "_Complex" is
13387 : present and there are no other specifiers. If we just have
13388 : "_Complex", it is equivalent to "_Complex double", but e.g.
13389 : "_Complex short" is equivalent to "_Complex short int". */
13390 66208728 : if (specs->typespec_word == cts_none)
13391 : {
13392 4194056 : if (specs->saturating_p)
13393 : {
13394 1 : error_at (specs->locations[cdw_saturating],
13395 : "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
13396 1 : if (!targetm.fixed_point_supported_p ())
13397 1 : error_at (specs->locations[cdw_saturating],
13398 : "fixed-point types not supported for this target");
13399 1 : specs->typespec_word = cts_fract;
13400 : }
13401 4194055 : else if (specs->long_p || specs->short_p
13402 193958 : || specs->signed_p || specs->unsigned_p)
13403 : {
13404 4183610 : specs->typespec_word = cts_int;
13405 : }
13406 10445 : else if (specs->complex_p)
13407 : {
13408 131 : specs->typespec_word = cts_double;
13409 131 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13410 : "ISO C does not support plain %<complex%> meaning "
13411 : "%<double complex%>");
13412 : }
13413 10314 : else if (specs->c23_auto_p)
13414 : {
13415 : /* Type to be filled in later, including applying postfix
13416 : attributes. This warning only actually appears for
13417 : -Wc11-c23-compat in C23 mode; in older modes, there may
13418 : be a warning or pedwarn for implicit "int" instead, or
13419 : other errors for use of auto at file scope. */
13420 95 : pedwarn_c11 (input_location, OPT_Wpedantic,
13421 : "ISO C does not support %<auto%> type deduction "
13422 : "before C23");
13423 95 : return specs;
13424 : }
13425 : else
13426 : {
13427 10219 : specs->typespec_word = cts_int;
13428 10219 : specs->default_int_p = true;
13429 : /* We don't diagnose this here because grokdeclarator will
13430 : give more specific diagnostics according to whether it is
13431 : a function definition. */
13432 : }
13433 : }
13434 :
13435 : /* If "signed" was specified, record this to distinguish "int" and
13436 : "signed int" in the case of a bit-field with
13437 : -funsigned-bitfields. */
13438 66208633 : specs->explicit_signed_p = specs->signed_p;
13439 :
13440 : /* Now compute the actual type. */
13441 66208633 : gcc_assert (!specs->c23_auto_p);
13442 66208633 : switch (specs->typespec_word)
13443 : {
13444 1885 : case cts_auto_type:
13445 1885 : gcc_assert (!specs->long_p && !specs->short_p
13446 : && !specs->signed_p && !specs->unsigned_p
13447 : && !specs->complex_p);
13448 : /* Type to be filled in later. */
13449 1885 : if (specs->postfix_attrs)
13450 2 : error ("%<__auto_type%> followed by %<[[]]%> attributes");
13451 : break;
13452 8000231 : case cts_void:
13453 8000231 : gcc_assert (!specs->long_p && !specs->short_p
13454 : && !specs->signed_p && !specs->unsigned_p
13455 : && !specs->complex_p);
13456 8000231 : specs->type = void_type_node;
13457 8000231 : break;
13458 91944 : case cts_bool:
13459 91944 : gcc_assert (!specs->long_p && !specs->short_p
13460 : && !specs->signed_p && !specs->unsigned_p
13461 : && !specs->complex_p);
13462 91944 : specs->type = boolean_type_node;
13463 91944 : break;
13464 9063461 : case cts_char:
13465 9063461 : gcc_assert (!specs->long_p && !specs->short_p);
13466 9063461 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13467 9063461 : if (specs->signed_p)
13468 173286 : specs->type = signed_char_type_node;
13469 8890175 : else if (specs->unsigned_p)
13470 884555 : specs->type = unsigned_char_type_node;
13471 : else
13472 8005620 : specs->type = char_type_node;
13473 9063461 : if (specs->complex_p)
13474 : {
13475 3856 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13476 : "ISO C does not support complex integer types");
13477 3856 : specs->type = build_complex_type (specs->type);
13478 : }
13479 : break;
13480 51816 : case cts_int_n:
13481 51816 : gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
13482 51816 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13483 51816 : if (! int_n_enabled_p[specs->u.int_n_idx])
13484 0 : specs->type = integer_type_node;
13485 : else
13486 51816 : specs->type = (specs->unsigned_p
13487 51816 : ? int_n_trees[specs->u.int_n_idx].unsigned_type
13488 : : int_n_trees[specs->u.int_n_idx].signed_type);
13489 51816 : if (specs->complex_p)
13490 : {
13491 199 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13492 : "ISO C does not support complex integer types");
13493 199 : specs->type = build_complex_type (specs->type);
13494 : }
13495 : break;
13496 28231536 : case cts_int:
13497 28231536 : gcc_assert (!(specs->long_p && specs->short_p));
13498 28231536 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13499 28231536 : if (specs->long_long_p)
13500 2969662 : specs->type = (specs->unsigned_p
13501 2969662 : ? long_long_unsigned_type_node
13502 : : long_long_integer_type_node);
13503 25261874 : else if (specs->long_p)
13504 2229363 : specs->type = (specs->unsigned_p
13505 2229363 : ? long_unsigned_type_node
13506 : : long_integer_type_node);
13507 23032511 : else if (specs->short_p)
13508 1704904 : specs->type = (specs->unsigned_p
13509 1704904 : ? short_unsigned_type_node
13510 : : short_integer_type_node);
13511 : else
13512 21327607 : specs->type = (specs->unsigned_p
13513 21327607 : ? unsigned_type_node
13514 : : integer_type_node);
13515 28231536 : if (specs->complex_p)
13516 : {
13517 15738 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13518 : "ISO C does not support complex integer types");
13519 15738 : specs->type = build_complex_type (specs->type);
13520 : }
13521 : break;
13522 3633167 : case cts_float:
13523 3633167 : gcc_assert (!specs->long_p && !specs->short_p
13524 : && !specs->signed_p && !specs->unsigned_p);
13525 7266334 : specs->type = (specs->complex_p
13526 3633167 : ? complex_float_type_node
13527 : : float_type_node);
13528 3633167 : break;
13529 6609459 : case cts_double:
13530 6609459 : gcc_assert (!specs->long_long_p && !specs->short_p
13531 : && !specs->signed_p && !specs->unsigned_p);
13532 6609459 : if (specs->long_p)
13533 : {
13534 2883045 : specs->type = (specs->complex_p
13535 2883045 : ? complex_long_double_type_node
13536 : : long_double_type_node);
13537 : }
13538 : else
13539 : {
13540 3726414 : specs->type = (specs->complex_p
13541 3726414 : ? complex_double_type_node
13542 : : double_type_node);
13543 : }
13544 : break;
13545 10430779 : case cts_floatn_nx:
13546 10430779 : gcc_assert (!specs->long_p && !specs->short_p
13547 : && !specs->signed_p && !specs->unsigned_p);
13548 10430779 : if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
13549 88 : specs->type = integer_type_node;
13550 10430691 : else if (specs->complex_p)
13551 1555525 : specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13552 : else
13553 8875166 : specs->type = FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13554 : break;
13555 47576 : case cts_dfloat32:
13556 47576 : case cts_dfloat64:
13557 47576 : case cts_dfloat128:
13558 47576 : case cts_dfloat64x:
13559 47576 : gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
13560 : && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
13561 47576 : if (!targetm.decimal_float_supported_p ())
13562 0 : specs->type = integer_type_node;
13563 47576 : else if (specs->typespec_word == cts_dfloat32)
13564 15957 : specs->type = dfloat32_type_node;
13565 31619 : else if (specs->typespec_word == cts_dfloat64)
13566 15838 : specs->type = dfloat64_type_node;
13567 15781 : else if (specs->typespec_word == cts_dfloat128)
13568 15734 : specs->type = dfloat128_type_node;
13569 : else
13570 47 : specs->type = dfloat64x_type_node;
13571 : break;
13572 32 : case cts_fract:
13573 32 : gcc_assert (!specs->complex_p);
13574 32 : if (!targetm.fixed_point_supported_p ())
13575 32 : specs->type = integer_type_node;
13576 0 : else if (specs->saturating_p)
13577 : {
13578 0 : if (specs->long_long_p)
13579 0 : specs->type = specs->unsigned_p
13580 0 : ? sat_unsigned_long_long_fract_type_node
13581 : : sat_long_long_fract_type_node;
13582 0 : else if (specs->long_p)
13583 0 : specs->type = specs->unsigned_p
13584 0 : ? sat_unsigned_long_fract_type_node
13585 : : sat_long_fract_type_node;
13586 0 : else if (specs->short_p)
13587 0 : specs->type = specs->unsigned_p
13588 0 : ? sat_unsigned_short_fract_type_node
13589 : : sat_short_fract_type_node;
13590 : else
13591 0 : specs->type = specs->unsigned_p
13592 0 : ? sat_unsigned_fract_type_node
13593 : : sat_fract_type_node;
13594 : }
13595 : else
13596 : {
13597 0 : if (specs->long_long_p)
13598 0 : specs->type = specs->unsigned_p
13599 0 : ? unsigned_long_long_fract_type_node
13600 : : long_long_fract_type_node;
13601 0 : else if (specs->long_p)
13602 0 : specs->type = specs->unsigned_p
13603 0 : ? unsigned_long_fract_type_node
13604 : : long_fract_type_node;
13605 0 : else if (specs->short_p)
13606 0 : specs->type = specs->unsigned_p
13607 0 : ? unsigned_short_fract_type_node
13608 : : short_fract_type_node;
13609 : else
13610 0 : specs->type = specs->unsigned_p
13611 0 : ? unsigned_fract_type_node
13612 : : fract_type_node;
13613 : }
13614 : break;
13615 30 : case cts_accum:
13616 30 : gcc_assert (!specs->complex_p);
13617 30 : if (!targetm.fixed_point_supported_p ())
13618 30 : specs->type = integer_type_node;
13619 0 : else if (specs->saturating_p)
13620 : {
13621 0 : if (specs->long_long_p)
13622 0 : specs->type = specs->unsigned_p
13623 0 : ? sat_unsigned_long_long_accum_type_node
13624 : : sat_long_long_accum_type_node;
13625 0 : else if (specs->long_p)
13626 0 : specs->type = specs->unsigned_p
13627 0 : ? sat_unsigned_long_accum_type_node
13628 : : sat_long_accum_type_node;
13629 0 : else if (specs->short_p)
13630 0 : specs->type = specs->unsigned_p
13631 0 : ? sat_unsigned_short_accum_type_node
13632 : : sat_short_accum_type_node;
13633 : else
13634 0 : specs->type = specs->unsigned_p
13635 0 : ? sat_unsigned_accum_type_node
13636 : : sat_accum_type_node;
13637 : }
13638 : else
13639 : {
13640 0 : if (specs->long_long_p)
13641 0 : specs->type = specs->unsigned_p
13642 0 : ? unsigned_long_long_accum_type_node
13643 : : long_long_accum_type_node;
13644 0 : else if (specs->long_p)
13645 0 : specs->type = specs->unsigned_p
13646 0 : ? unsigned_long_accum_type_node
13647 : : long_accum_type_node;
13648 0 : else if (specs->short_p)
13649 0 : specs->type = specs->unsigned_p
13650 0 : ? unsigned_short_accum_type_node
13651 : : short_accum_type_node;
13652 : else
13653 0 : specs->type = specs->unsigned_p
13654 0 : ? unsigned_accum_type_node
13655 : : accum_type_node;
13656 : }
13657 : break;
13658 46717 : case cts_bitint:
13659 46717 : gcc_assert (!specs->long_p && !specs->short_p
13660 : && !specs->complex_p);
13661 46717 : if (!specs->unsigned_p && specs->u.bitint_prec == 1 && !flag_isoc2y)
13662 : {
13663 2 : error_at (specs->locations[cdw_typespec],
13664 : "%<signed _BitInt%> argument must be at least 2 "
13665 : "before C2Y");
13666 2 : specs->type = integer_type_node;
13667 2 : break;
13668 : }
13669 46715 : if (specs->u.bitint_prec == -1)
13670 7 : specs->type = integer_type_node;
13671 : else
13672 : {
13673 69103 : pedwarn_c11 (specs->locations[cdw_typespec], OPT_Wpedantic,
13674 : "ISO C does not support %<%s_BitInt(%d)%> before C23",
13675 : specs->unsigned_p ? "unsigned "
13676 22395 : : specs->signed_p ? "signed " : "",
13677 : specs->u.bitint_prec);
13678 46708 : specs->type = build_bitint_type (specs->u.bitint_prec,
13679 46708 : specs->unsigned_p);
13680 : }
13681 : break;
13682 0 : default:
13683 0 : gcc_unreachable ();
13684 : }
13685 315542918 : handle_postfix_attrs:
13686 315542918 : if (specs->type != NULL)
13687 : {
13688 315541033 : specs->postfix_attrs
13689 315541033 : = c_warn_type_attributes (specs->type, specs->postfix_attrs);
13690 315541033 : decl_attributes (&specs->type, specs->postfix_attrs, 0);
13691 315541033 : specs->postfix_attrs = NULL_TREE;
13692 : }
13693 :
13694 : return specs;
13695 : }
13696 :
13697 : /* Perform final processing on one file scope's declarations (or the
13698 : external scope's declarations), GLOBALS. */
13699 :
13700 : static void
13701 210976 : c_write_global_declarations_1 (tree globals)
13702 : {
13703 210976 : tree decl;
13704 210976 : bool reconsider;
13705 :
13706 : /* Process the decls in the order they were written. */
13707 386594941 : for (decl = globals; decl; decl = DECL_CHAIN (decl))
13708 : {
13709 : /* Check for used but undefined static functions using the C
13710 : standard's definition of "used", and set TREE_NO_WARNING so
13711 : that check_global_declaration doesn't repeat the check. */
13712 386383965 : if (TREE_CODE (decl) == FUNCTION_DECL
13713 368204531 : && DECL_INITIAL (decl) == NULL_TREE
13714 331855596 : && DECL_EXTERNAL (decl)
13715 331855591 : && !TREE_PUBLIC (decl)
13716 386384100 : && !warning_suppressed_p (decl, OPT_Wunused))
13717 : {
13718 130 : if (C_DECL_USED (decl))
13719 : {
13720 31 : if (pedwarn (input_location, 0, "%q+F used but never defined",
13721 : decl))
13722 30 : suppress_warning (decl, OPT_Wunused);
13723 : }
13724 : /* For -Wunused-function warn about unused static prototypes. */
13725 99 : else if (warn_unused_function
13726 2 : && ! DECL_ARTIFICIAL (decl)
13727 101 : && warning (OPT_Wunused_function,
13728 : "%q+F declared %<static%> but never defined",
13729 : decl))
13730 2 : suppress_warning (decl, OPT_Wunused);
13731 : }
13732 :
13733 386383965 : wrapup_global_declaration_1 (decl);
13734 : }
13735 :
13736 245299 : do
13737 : {
13738 245299 : reconsider = false;
13739 501336029 : for (decl = globals; decl; decl = DECL_CHAIN (decl))
13740 501090730 : reconsider |= wrapup_global_declaration_2 (decl);
13741 : }
13742 : while (reconsider);
13743 210976 : }
13744 :
13745 : /* Preserve the external declarations scope across a garbage collect. */
13746 : static GTY(()) tree ext_block;
13747 :
13748 : /* Collect all references relevant to SOURCE_FILE. */
13749 :
13750 : static void
13751 15 : collect_all_refs (const char *source_file)
13752 : {
13753 15 : tree t;
13754 15 : unsigned i;
13755 :
13756 30 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13757 15 : collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
13758 :
13759 15 : collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
13760 15 : }
13761 :
13762 : /* Collect source file references at global level. */
13763 :
13764 : static void
13765 15 : collect_source_refs (void)
13766 : {
13767 15 : tree t;
13768 15 : tree decls;
13769 15 : tree decl;
13770 15 : unsigned i;
13771 :
13772 30 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13773 : {
13774 15 : decls = DECL_INITIAL (t);
13775 82 : for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
13776 67 : if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13777 67 : collect_source_ref (DECL_SOURCE_FILE (decl));
13778 : }
13779 :
13780 44291 : for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
13781 44276 : if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13782 11 : collect_source_ref (DECL_SOURCE_FILE (decl));
13783 15 : }
13784 :
13785 : /* Free attribute access data that are not needed by the middle end. */
13786 :
13787 : static void
13788 105488 : free_attr_access_data ()
13789 : {
13790 105488 : struct cgraph_node *n;
13791 :
13792 : /* Iterate over all functions declared in the translation unit. */
13793 36456070 : FOR_EACH_FUNCTION (n)
13794 : {
13795 136498817 : for (tree parm = DECL_ARGUMENTS (n->decl); parm; parm = TREE_CHAIN (parm))
13796 100148235 : if (tree attrs = DECL_ATTRIBUTES (parm))
13797 97868 : attr_access::free_lang_data (attrs);
13798 :
13799 36350582 : tree fntype = TREE_TYPE (n->decl);
13800 36350582 : if (!fntype || fntype == error_mark_node)
13801 0 : continue;
13802 36350582 : tree attrs = TYPE_ATTRIBUTES (fntype);
13803 36350582 : if (!attrs)
13804 36114530 : continue;
13805 :
13806 236052 : attr_access::free_lang_data (attrs);
13807 : }
13808 105488 : }
13809 :
13810 : /* Perform any final parser cleanups and generate initial debugging
13811 : information. */
13812 :
13813 : void
13814 105818 : c_parse_final_cleanups (void)
13815 : {
13816 105818 : tree t;
13817 105818 : unsigned i;
13818 :
13819 : /* We don't want to do this if generating a PCH. */
13820 105818 : if (pch_file)
13821 105818 : return;
13822 :
13823 105488 : timevar_stop (TV_PHASE_PARSING);
13824 105488 : timevar_start (TV_PHASE_DEFERRED);
13825 :
13826 : /* Do the Objective-C stuff. This is where all the Objective-C
13827 : module stuff gets generated (symtab, class/protocol/selector
13828 : lists etc). */
13829 105488 : if (c_dialect_objc ())
13830 0 : objc_write_global_declarations ();
13831 :
13832 : /* Close the external scope. */
13833 105488 : ext_block = pop_scope ();
13834 105488 : external_scope = 0;
13835 105488 : gcc_assert (!current_scope);
13836 :
13837 : /* Handle -fdump-ada-spec[-slim]. */
13838 105488 : if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
13839 : {
13840 : /* Build a table of files to generate specs for */
13841 15 : collect_source_ref (main_input_filename);
13842 15 : if (!flag_dump_ada_spec_slim)
13843 15 : collect_source_refs ();
13844 :
13845 15 : dump_ada_specs (collect_all_refs, NULL);
13846 : }
13847 :
13848 : /* Process all file scopes in this compilation, and the external_scope,
13849 : through wrapup_global_declarations. */
13850 210976 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13851 105488 : c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
13852 105488 : c_write_global_declarations_1 (BLOCK_VARS (ext_block));
13853 :
13854 : /* Call this to set cpp_implicit_aliases_done on all nodes. This is
13855 : important for function multiversioning aliases to get resolved. */
13856 105488 : symtab->process_same_body_aliases ();
13857 :
13858 105488 : if (!in_lto_p)
13859 105488 : free_attr_access_data ();
13860 :
13861 105488 : timevar_stop (TV_PHASE_DEFERRED);
13862 105488 : timevar_start (TV_PHASE_PARSING);
13863 :
13864 105488 : ext_block = NULL;
13865 : }
13866 :
13867 : /* Register reserved keyword WORD as qualifier for address space AS. */
13868 :
13869 : void
13870 223620 : c_register_addr_space (const char *word, addr_space_t as)
13871 : {
13872 223620 : int rid = RID_FIRST_ADDR_SPACE + as;
13873 223620 : tree id;
13874 :
13875 : /* Address space qualifiers are only supported
13876 : in C with GNU extensions enabled. */
13877 223620 : if (c_dialect_objc () || flag_no_asm)
13878 : return;
13879 :
13880 211108 : id = get_identifier (word);
13881 211108 : C_SET_RID_CODE (id, rid);
13882 211108 : C_IS_RESERVED_WORD (id) = 1;
13883 211108 : ridpointers [rid] = id;
13884 : }
13885 :
13886 : /* Return identifier to look up for omp declare reduction. */
13887 :
13888 : tree
13889 3264 : c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
13890 : {
13891 3264 : const char *p = NULL;
13892 3264 : switch (reduction_code)
13893 : {
13894 : case PLUS_EXPR: p = "+"; break;
13895 267 : case MULT_EXPR: p = "*"; break;
13896 157 : case MINUS_EXPR: p = "-"; break;
13897 31 : case BIT_AND_EXPR: p = "&"; break;
13898 14 : case BIT_XOR_EXPR: p = "^"; break;
13899 92 : case BIT_IOR_EXPR: p = "|"; break;
13900 67 : case TRUTH_ANDIF_EXPR: p = "&&"; break;
13901 78 : case TRUTH_ORIF_EXPR: p = "||"; break;
13902 46 : case MIN_EXPR: p = "min"; break;
13903 83 : case MAX_EXPR: p = "max"; break;
13904 : default:
13905 : break;
13906 : }
13907 :
13908 835 : if (p == NULL)
13909 : {
13910 213 : if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
13911 0 : return error_mark_node;
13912 213 : p = IDENTIFIER_POINTER (reduction_id);
13913 : }
13914 :
13915 3264 : const char prefix[] = "omp declare reduction ";
13916 3264 : size_t lenp = sizeof (prefix);
13917 3264 : size_t len = strlen (p);
13918 3264 : char *name = XALLOCAVEC (char, lenp + len);
13919 3264 : memcpy (name, prefix, lenp - 1);
13920 3264 : memcpy (name + lenp - 1, p, len + 1);
13921 3264 : return get_identifier (name);
13922 : }
13923 :
13924 : /* Lookup REDUCTION_ID in the current scope, or create an artificial
13925 : VAR_DECL, bind it into the current scope and return it. */
13926 :
13927 : tree
13928 162 : c_omp_reduction_decl (tree reduction_id)
13929 : {
13930 162 : struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13931 162 : if (b != NULL && B_IN_CURRENT_SCOPE (b))
13932 47 : return b->decl;
13933 :
13934 115 : tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
13935 : reduction_id, integer_type_node);
13936 115 : DECL_ARTIFICIAL (decl) = 1;
13937 115 : DECL_EXTERNAL (decl) = 1;
13938 115 : TREE_STATIC (decl) = 1;
13939 115 : TREE_PUBLIC (decl) = 0;
13940 115 : bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
13941 115 : return decl;
13942 : }
13943 :
13944 : /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
13945 :
13946 : tree
13947 276 : c_omp_reduction_lookup (tree reduction_id, tree type)
13948 : {
13949 276 : struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13950 279 : while (b)
13951 : {
13952 277 : tree t;
13953 297 : for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
13954 294 : if (comptypes (TREE_PURPOSE (t), type))
13955 274 : return TREE_VALUE (t);
13956 3 : b = b->shadowed;
13957 : }
13958 2 : return error_mark_node;
13959 : }
13960 :
13961 : /* Helper function called via walk_tree, to diagnose invalid
13962 : #pragma omp declare reduction combiners or initializers. */
13963 :
13964 : tree
13965 1416 : c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
13966 : {
13967 1416 : tree *vars = (tree *) data;
13968 1416 : if (SSA_VAR_P (*tp)
13969 416 : && !DECL_ARTIFICIAL (*tp)
13970 24 : && *tp != vars[0]
13971 24 : && *tp != vars[1])
13972 : {
13973 24 : location_t loc = DECL_SOURCE_LOCATION (vars[0]);
13974 24 : if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
13975 10 : error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
13976 : "variable %qD which is not %<omp_out%> nor %<omp_in%>",
13977 : *tp);
13978 : else
13979 14 : error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
13980 : "to variable %qD which is not %<omp_priv%> nor "
13981 : "%<omp_orig%>",
13982 : *tp);
13983 24 : return *tp;
13984 : }
13985 : return NULL_TREE;
13986 : }
13987 :
13988 : /* Return identifier to look up for omp declare mapper. */
13989 :
13990 : tree
13991 700 : c_omp_mapper_id (tree mapper_id)
13992 : {
13993 700 : const char *p = NULL;
13994 :
13995 700 : const char prefix[] = "omp declare mapper ";
13996 :
13997 700 : if (mapper_id == NULL_TREE)
13998 : p = "<default>";
13999 27 : else if (TREE_CODE (mapper_id) == IDENTIFIER_NODE)
14000 27 : p = IDENTIFIER_POINTER (mapper_id);
14001 : else
14002 0 : return error_mark_node;
14003 :
14004 700 : size_t lenp = sizeof (prefix);
14005 700 : size_t len = strlen (p);
14006 700 : char *name = XALLOCAVEC (char, lenp + len);
14007 700 : memcpy (name, prefix, lenp - 1);
14008 700 : memcpy (name + lenp - 1, p, len + 1);
14009 700 : return get_identifier (name);
14010 : }
14011 :
14012 : /* Lookup MAPPER_ID in the current scope, or create an artificial
14013 : VAR_DECL, bind it into the current scope and return it. */
14014 :
14015 : tree
14016 74 : c_omp_mapper_decl (tree mapper_id)
14017 : {
14018 74 : struct c_binding *b = I_SYMBOL_BINDING (mapper_id);
14019 74 : if (b != NULL && B_IN_CURRENT_SCOPE (b))
14020 22 : return b->decl;
14021 :
14022 52 : tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
14023 : mapper_id, integer_type_node);
14024 52 : DECL_ARTIFICIAL (decl) = 1;
14025 52 : DECL_EXTERNAL (decl) = 1;
14026 52 : TREE_STATIC (decl) = 1;
14027 52 : TREE_PUBLIC (decl) = 0;
14028 52 : bind (mapper_id, decl, current_scope, true, false, BUILTINS_LOCATION);
14029 52 : return decl;
14030 : }
14031 :
14032 : /* Lookup MAPPER_ID in the first scope where it has entry for TYPE. */
14033 :
14034 : tree
14035 5437 : c_omp_mapper_lookup (tree mapper_id, tree type)
14036 : {
14037 5437 : if (!RECORD_OR_UNION_TYPE_P (type))
14038 : return NULL_TREE;
14039 :
14040 626 : mapper_id = c_omp_mapper_id (mapper_id);
14041 :
14042 626 : struct c_binding *b = I_SYMBOL_BINDING (mapper_id);
14043 632 : while (b)
14044 : {
14045 129 : tree t;
14046 148 : for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
14047 142 : if (comptypes (TREE_PURPOSE (t), type))
14048 123 : return TREE_VALUE (t);
14049 6 : b = b->shadowed;
14050 : }
14051 : return NULL_TREE;
14052 : }
14053 :
14054 : /* For C, we record a pointer to the mapper itself without wrapping it in an
14055 : artificial function or similar. So, just return it. */
14056 :
14057 : tree
14058 115 : c_omp_extract_mapper_directive (tree mapper)
14059 : {
14060 115 : return mapper;
14061 : }
14062 :
14063 : /* For now we can handle singleton OMP_ARRAY_SECTIONs with custom mappers, but
14064 : nothing more complicated. */
14065 :
14066 : tree
14067 571 : c_omp_map_array_section (location_t loc, tree t)
14068 : {
14069 571 : tree low = TREE_OPERAND (t, 1);
14070 571 : tree len = TREE_OPERAND (t, 2);
14071 :
14072 571 : if (len && integer_onep (len))
14073 : {
14074 92 : t = TREE_OPERAND (t, 0);
14075 :
14076 92 : if (!low)
14077 14 : low = integer_zero_node;
14078 :
14079 92 : t = build_array_ref (loc, t, low);
14080 : }
14081 :
14082 571 : return t;
14083 : }
14084 :
14085 : /* Helper function for below function. */
14086 :
14087 : static tree
14088 52015 : c_omp_scan_mapper_bindings_r (tree *tp, int *walk_subtrees, void *ptr)
14089 : {
14090 52015 : tree t = *tp;
14091 52015 : omp_mapper_list<tree> *mlist = (omp_mapper_list<tree> *) ptr;
14092 52015 : tree aggr_type = NULL_TREE;
14093 :
14094 52015 : if (TREE_CODE (t) == SIZEOF_EXPR
14095 52015 : || TREE_CODE (t) == ALIGNOF_EXPR)
14096 : {
14097 0 : *walk_subtrees = 0;
14098 0 : return NULL_TREE;
14099 : }
14100 :
14101 52015 : if (TREE_CODE (t) == OMP_CLAUSE)
14102 : return NULL_TREE;
14103 :
14104 48711 : if (TREE_CODE (t) == COMPONENT_REF
14105 48711 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
14106 370 : aggr_type = TREE_TYPE (TREE_OPERAND (t, 0));
14107 48341 : else if ((TREE_CODE (t) == VAR_DECL
14108 : || TREE_CODE (t) == PARM_DECL
14109 : || TREE_CODE (t) == RESULT_DECL)
14110 5420 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t)))
14111 121 : aggr_type = TREE_TYPE (t);
14112 :
14113 491 : if (aggr_type)
14114 : {
14115 491 : tree mapper_fn = c_omp_mapper_lookup (NULL_TREE, aggr_type);
14116 491 : if (mapper_fn)
14117 76 : mlist->add_mapper (NULL_TREE, aggr_type, mapper_fn);
14118 : }
14119 :
14120 : return NULL_TREE;
14121 : }
14122 :
14123 : /* Scan an offload region's body, and record uses of struct- or union-typed
14124 : variables. Add _mapper_binding_ fake clauses to *CLAUSES_PTR. */
14125 :
14126 : void
14127 2110 : c_omp_scan_mapper_bindings (location_t loc, tree *clauses_ptr, tree body)
14128 : {
14129 2110 : hash_set<omp_name_type<tree>> seen_types;
14130 2110 : auto_vec<tree> mappers;
14131 2110 : omp_mapper_list<tree> mlist (&seen_types, &mappers);
14132 :
14133 2110 : walk_tree_without_duplicates (&body, c_omp_scan_mapper_bindings_r, &mlist);
14134 :
14135 2110 : unsigned int i;
14136 2110 : tree mapper;
14137 4262 : FOR_EACH_VEC_ELT (mappers, i, mapper)
14138 42 : c_omp_find_nested_mappers (&mlist, mapper);
14139 :
14140 2181 : FOR_EACH_VEC_ELT (mappers, i, mapper)
14141 : {
14142 42 : if (mapper == error_mark_node)
14143 0 : continue;
14144 42 : tree mapper_name = OMP_DECLARE_MAPPER_ID (mapper);
14145 42 : tree decl = OMP_DECLARE_MAPPER_DECL (mapper);
14146 :
14147 42 : tree c = build_omp_clause (loc, OMP_CLAUSE__MAPPER_BINDING_);
14148 42 : OMP_CLAUSE__MAPPER_BINDING__ID (c) = mapper_name;
14149 42 : OMP_CLAUSE__MAPPER_BINDING__DECL (c) = decl;
14150 42 : OMP_CLAUSE__MAPPER_BINDING__MAPPER (c) = mapper;
14151 :
14152 42 : OMP_CLAUSE_CHAIN (c) = *clauses_ptr;
14153 42 : *clauses_ptr = c;
14154 : }
14155 2110 : }
14156 :
14157 : bool
14158 247 : c_check_in_current_scope (tree decl)
14159 : {
14160 247 : struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
14161 247 : return b != NULL && B_IN_CURRENT_SCOPE (b);
14162 : }
14163 :
14164 : /* Search for loop or switch names. BEFORE_LABELS is last statement before
14165 : possible labels and SWITCH_P true for a switch, false for loops.
14166 : Searches through last statements in cur_stmt_list, stops when seeing
14167 : BEFORE_LABELs, or statement other than LABEL_EXPR or CASE_LABEL_EXPR.
14168 : Returns number of loop/switch names found and if any are found, sets
14169 : *LAST_P to the canonical loop/switch name LABEL_DECL. */
14170 :
14171 : int
14172 521721 : c_get_loop_names (tree before_labels, bool switch_p, tree *last_p)
14173 : {
14174 521721 : *last_p = NULL_TREE;
14175 1043442 : if (!building_stmt_list_p ()
14176 521721 : || !STATEMENT_LIST_HAS_LABEL (cur_stmt_list)
14177 542393 : || before_labels == void_list_node)
14178 501049 : return 0;
14179 :
14180 20672 : int ret = 0;
14181 20672 : tree last = NULL_TREE;
14182 20672 : for (tree_stmt_iterator tsi = tsi_last (cur_stmt_list);
14183 28908 : !tsi_end_p (tsi); tsi_prev (&tsi))
14184 : {
14185 28051 : tree stmt = tsi_stmt (tsi);
14186 28051 : if (stmt == before_labels)
14187 : break;
14188 8236 : else if (TREE_CODE (stmt) == LABEL_EXPR)
14189 : {
14190 1111 : if (last == NULL_TREE)
14191 917 : last = LABEL_EXPR_LABEL (stmt);
14192 : else
14193 : {
14194 194 : loop_names.safe_push (LABEL_EXPR_LABEL (stmt));
14195 194 : ++ret;
14196 : }
14197 : }
14198 7125 : else if (TREE_CODE (stmt) != CASE_LABEL_EXPR
14199 4799 : && TREE_CODE (stmt) != DEBUG_BEGIN_STMT)
14200 : break;
14201 : }
14202 20672 : if (last)
14203 : {
14204 917 : if (switch_p)
14205 139 : C_DECL_SWITCH_NAME (last) = 1;
14206 : else
14207 778 : C_DECL_LOOP_NAME (last) = 1;
14208 917 : loop_names.safe_push (last);
14209 917 : ++ret;
14210 917 : if (loop_names.length () > 16)
14211 : {
14212 20 : unsigned int first = 0, i;
14213 20 : tree l, c = NULL_TREE;
14214 20 : if (loop_names_hash == NULL)
14215 8 : loop_names_hash = new decl_tree_map (ret);
14216 : else
14217 12 : first = loop_names.length () - ret;
14218 210 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14219 : {
14220 170 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14221 48 : c = l;
14222 170 : gcc_checking_assert (c);
14223 170 : loop_names_hash->put (l, c);
14224 170 : if (i == first)
14225 : break;
14226 : }
14227 : }
14228 917 : *last_p = last;
14229 : }
14230 : return ret;
14231 : }
14232 :
14233 : /* Undoes what get_loop_names did when it returned NUM_NAMES. */
14234 :
14235 : void
14236 917 : c_release_loop_names (int num_names)
14237 : {
14238 917 : unsigned len = loop_names.length () - num_names;
14239 917 : if (loop_names_hash)
14240 : {
14241 20 : if (len <= 16)
14242 : {
14243 8 : delete loop_names_hash;
14244 8 : loop_names_hash = NULL;
14245 : }
14246 : else
14247 : {
14248 12 : unsigned int i;
14249 12 : tree l;
14250 42 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14251 : {
14252 30 : loop_names_hash->remove (l);
14253 30 : if (i == len)
14254 : break;
14255 : }
14256 : }
14257 : }
14258 917 : loop_names.truncate (len);
14259 917 : }
14260 :
14261 : /* Finish processing of break or continue identifier operand.
14262 : NAME is the identifier operand of break or continue and
14263 : IS_BREAK is true iff it is break stmt. Returns the operand
14264 : to use for BREAK_STMT or CONTINUE_STMT, either NULL_TREE or
14265 : canonical loop/switch name LABEL_DECL. */
14266 :
14267 : tree
14268 316 : c_finish_bc_name (location_t loc, tree name, bool is_break)
14269 : {
14270 316 : tree label = NULL_TREE, lab;
14271 457 : pedwarn_c23 (loc, OPT_Wpedantic,
14272 : "ISO C does not support %qs statement with an identifier "
14273 : "operand before C2Y", is_break ? "break" : "continue");
14274 :
14275 : /* If I_LABEL_DECL is NULL or not from current function, don't waste time
14276 : trying to find it among loop_names, it can't be there. */
14277 316 : if (!loop_names.is_empty ()
14278 296 : && current_function_scope
14279 296 : && (lab = I_LABEL_DECL (name))
14280 290 : && DECL_CONTEXT (lab) == current_function_decl)
14281 : {
14282 280 : unsigned int i;
14283 280 : tree l, c = NULL_TREE;
14284 280 : if (loop_names_hash)
14285 : {
14286 86 : if (tree *val = loop_names_hash->get (lab))
14287 86 : label = *val;
14288 : }
14289 : else
14290 565 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14291 : {
14292 363 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14293 : c = l;
14294 363 : gcc_checking_assert (c);
14295 363 : if (l == lab)
14296 : {
14297 : label = c;
14298 : break;
14299 : }
14300 : }
14301 280 : if (label)
14302 272 : TREE_USED (lab) = 1;
14303 : }
14304 272 : if (label == NULL_TREE)
14305 : {
14306 44 : auto_vec<const char *> candidates;
14307 44 : unsigned int i;
14308 44 : tree l, c = NULL_TREE;
14309 115 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14310 : {
14311 28 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14312 : c = l;
14313 28 : gcc_checking_assert (c);
14314 42 : if (is_break || C_DECL_LOOP_NAME (c))
14315 25 : candidates.safe_push (IDENTIFIER_POINTER (DECL_NAME (l)));
14316 : }
14317 44 : const char *hint = find_closest_string (IDENTIFIER_POINTER (name),
14318 : &candidates);
14319 44 : if (hint)
14320 : {
14321 22 : gcc_rich_location richloc (loc);
14322 22 : richloc.add_fixit_replace (hint);
14323 22 : if (is_break)
14324 11 : error_at (&richloc, "%<break%> statement operand %qE does not "
14325 : "refer to a named loop or %<switch%>; "
14326 : "did you mean %qs?", name, hint);
14327 : else
14328 11 : error_at (&richloc, "%<continue%> statement operand %qE does not "
14329 : "refer to a named loop; did you mean %qs?",
14330 : name, hint);
14331 22 : }
14332 22 : else if (is_break)
14333 12 : error_at (loc, "%<break%> statement operand %qE does not refer to a "
14334 : "named loop or %<switch%>", name);
14335 : else
14336 10 : error_at (loc, "%<continue%> statement operand %qE does not refer to "
14337 : "a named loop", name);
14338 44 : }
14339 272 : else if (!C_DECL_LOOP_NAME (label) && !is_break)
14340 : {
14341 2 : auto_diagnostic_group d;
14342 2 : error_at (loc, "%<continue%> statement operand %qE refers to a named "
14343 : "%<switch%>", name);
14344 2 : inform (DECL_SOURCE_LOCATION (label), "%<switch%> name defined here");
14345 2 : label = NULL_TREE;
14346 2 : }
14347 270 : else if (!C_DECL_LOOP_SWITCH_NAME_VALID (label))
14348 : {
14349 18 : auto_diagnostic_group d;
14350 18 : if (C_DECL_LOOP_NAME (label))
14351 : {
14352 16 : error_at (loc, "%qs statement operand %qE refers to a loop outside "
14353 : "of its body", is_break ? "break" : "continue", name);
14354 16 : inform (DECL_SOURCE_LOCATION (label), "loop name defined here");
14355 : }
14356 : else
14357 : {
14358 2 : error_at (loc, "%<break%> statement operand %qE refers to a "
14359 : "%<switch%> outside of its body", name);
14360 2 : inform (DECL_SOURCE_LOCATION (label),
14361 : "%<switch%> name defined here");
14362 : }
14363 18 : label = NULL_TREE;
14364 18 : }
14365 252 : else if (label == loop_names.last () && (in_statement & IN_NAMED_STMT) != 0)
14366 : /* If it is just a fancy reference to the innermost construct, handle it
14367 : just like break; or continue; though tracking cheaply what is the
14368 : innermost loop for continue when nested in switches would require
14369 : another global variable and updating it. */
14370 : label = NULL_TREE;
14371 : else
14372 98 : C_DECL_LOOP_SWITCH_NAME_USED (label) = 1;
14373 316 : return label;
14374 : }
14375 :
14376 : #include "gt-c-c-decl.h"
|