1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2024 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
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. */
24 /* ??? not all decl nodes are given the most useful possible
25 line numbers. For example, the CONST_DECLs for enum values. */
28 #define INCLUDE_STRING
29 #define INCLUDE_MEMORY
31 #include "coretypes.h"
36 #include "stringpool.h"
39 #include "print-tree.h"
40 #include "stor-layout.h"
45 #include "c-family/c-objc.h"
46 #include "c-family/c-pragma.h"
47 #include "c-family/c-ubsan.h"
49 #include "langhooks.h"
50 #include "tree-iterator.h"
53 #include "c-family/c-ada-spec.h"
55 #include "spellcheck-tree.h"
56 #include "gcc-rich-location.h"
58 #include "c-family/name-hint.h"
59 #include "c-family/known-headers.h"
60 #include "c-family/c-spellcheck.h"
61 #include "context.h" /* For 'g'. */
62 #include "omp-general.h"
63 #include "omp-offload.h" /* For offload_vars. */
66 #include "tree-pretty-print.h"
68 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
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) */
76 /* States indicating how grokdeclarator() should handle declspecs marked
77 with __attribute__((deprecated)) or __attribute__((unavailable)).
78 An object declared as __attribute__((unavailable)) should suppress
79 any reports of being declared with unavailable or deprecated items.
80 An object declared as __attribute__((deprecated)) should suppress
81 warnings of uses of other deprecated items. */
83 enum deprecated_states
{
86 UNAVAILABLE_DEPRECATED_SUPPRESS
90 /* Nonzero if we have seen an invalid cross reference
91 to a struct, union, or enum, but not yet printed the message. */
92 tree pending_invalid_xref
;
94 /* File and line to appear in the eventual error message. */
95 location_t pending_invalid_xref_location
;
97 /* The file and line that the prototype came from if this is an
98 old-style definition; used for diagnostics in
99 store_parm_decls_oldstyle. */
101 static location_t current_function_prototype_locus
;
103 /* Whether this prototype was built-in. */
105 static bool current_function_prototype_built_in
;
107 /* The argument type information of this prototype. */
109 static tree current_function_prototype_arg_types
;
111 /* The argument information structure for the function currently being
114 static struct c_arg_info
*current_function_arg_info
;
116 /* The obstack on which parser and related data structures, which are
117 not live beyond their top-level declaration or definition, are
119 struct obstack parser_obstack
;
121 /* The current statement tree. */
123 static GTY(()) struct stmt_tree_s c_stmt_tree
;
125 /* Zero if we are not in an iteration or switch statement, otherwise
126 a bitmask. See bitmask definitions in c-tree.h. */
127 unsigned char in_statement
;
129 /* A list of decls to be made automatically visible in each file scope. */
130 static GTY(()) tree visible_builtins
;
132 /* Set to 0 at beginning of a function definition, set to 1 if
133 a return statement that specifies a return value is seen. */
135 int current_function_returns_value
;
137 /* Set to 0 at beginning of a function definition, set to 1 if
138 a return statement with no argument is seen. */
140 int current_function_returns_null
;
142 /* Set to 0 at beginning of a function definition, set to 1 if
143 a call to a noreturn function is seen. */
145 int current_function_returns_abnormally
;
147 /* Set to nonzero by `grokdeclarator' for a function
148 whose return type is defaulted, if warnings for this are desired. */
150 static int warn_about_return_type
;
152 /* Nonzero when the current toplevel function contains a declaration
153 of a nested function which is never defined. */
155 static bool undef_nested_function
;
157 /* Vector of implicit "omp declare target" attributes to be added into
158 the attribute lists. */
159 vec
<c_omp_declare_target_attr
, va_gc
> *current_omp_declare_target_attribute
;
162 #pragma omp begin assumes ... #pragma omp end assumes regions
164 vec
<c_omp_begin_assumes_data
, va_gc
> *current_omp_begin_assumes
;
166 /* Each c_binding structure describes one binding of an identifier to
167 a decl. All the decls in a scope - irrespective of namespace - are
168 chained together by the ->prev field, which (as the name implies)
169 runs in reverse order. All the decls in a given namespace bound to
170 a given identifier are chained by the ->shadowed field, which runs
171 from inner to outer scopes.
173 The ->decl field usually points to a DECL node, but there are two
174 exceptions. In the namespace of type tags, the bound entity is a
175 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
176 identifier is encountered, it is bound to error_mark_node to
177 suppress further errors about that identifier in the current
180 The ->u.type field stores the type of the declaration in this scope;
181 if NULL, the type is the type of the ->decl field. This is only of
182 relevance for objects with external or internal linkage which may
183 be redeclared in inner scopes, forming composite types that only
184 persist for the duration of those scopes. In the external scope,
185 this stores the composite of all the types declared for this
186 object, visible or not. The ->inner_comp field (used only at file
187 scope) stores whether an incomplete array type at file scope was
188 completed at an inner scope to an array size other than 1.
190 The ->u.label field is used for labels. It points to a structure
191 which stores additional information used for warnings.
193 The depth field is copied from the scope structure that holds this
194 decl. It is used to preserve the proper ordering of the ->shadowed
195 field (see bind()) and also for a handful of special-case checks.
196 Finally, the invisible bit is true for a decl which should be
197 ignored for purposes of normal name lookup, and the nested bit is
198 true for a decl that's been bound a second time in an inner scope;
199 in all such cases, the binding in the outer scope will have its
200 invisible bit true. */
202 struct GTY((chain_next ("%h.prev"))) c_binding
{
203 union GTY(()) { /* first so GTY desc can use decl */
204 tree
GTY((tag ("0"))) type
; /* the type in this scope */
205 struct c_label_vars
* GTY((tag ("1"))) label
; /* for warnings */
206 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u
;
207 tree decl
; /* the decl bound */
208 tree id
; /* the identifier it's bound to */
209 struct c_binding
*prev
; /* the previous decl in this scope */
210 struct c_binding
*shadowed
; /* the innermost decl shadowed by this one */
211 unsigned int depth
: 28; /* depth of this scope */
212 BOOL_BITFIELD invisible
: 1; /* normal lookup should ignore this binding */
213 BOOL_BITFIELD nested
: 1; /* do not set DECL_CONTEXT when popping */
214 BOOL_BITFIELD inner_comp
: 1; /* incomplete array completed in inner scope */
215 BOOL_BITFIELD in_struct
: 1; /* currently defined as struct field */
216 location_t locus
; /* location for nested bindings */
218 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
219 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
220 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
221 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
223 /* Each C symbol points to three linked lists of c_binding structures.
224 These describe the values of the identifier in the three different
225 namespaces defined by the language. */
227 struct GTY(()) lang_identifier
{
228 struct c_common_identifier common_id
;
229 struct c_binding
*symbol_binding
; /* vars, funcs, constants, typedefs */
230 struct c_binding
*tag_binding
; /* struct/union/enum tags */
231 struct c_binding
*label_binding
; /* labels */
234 /* Validate c-lang.cc's assumptions. */
235 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
236 [(sizeof(struct lang_identifier
) == C_SIZEOF_STRUCT_LANG_IDENTIFIER
) ? 1 : -1];
238 /* The binding oracle; see c-tree.h. */
239 void (*c_binding_oracle
) (enum c_oracle_request
, tree identifier
);
241 /* This flag is set on an identifier if we have previously asked the
242 binding oracle for this identifier's symbol binding. */
243 #define I_SYMBOL_CHECKED(node) \
244 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
246 static inline struct c_binding
* *
247 i_symbol_binding (tree node
)
249 struct lang_identifier
*lid
250 = (struct lang_identifier
*) IDENTIFIER_NODE_CHECK (node
);
252 if (lid
->symbol_binding
== NULL
253 && c_binding_oracle
!= NULL
254 && !I_SYMBOL_CHECKED (node
))
256 /* Set the "checked" flag first, to avoid infinite recursion
257 when the binding oracle calls back into gcc. */
258 I_SYMBOL_CHECKED (node
) = 1;
259 c_binding_oracle (C_ORACLE_SYMBOL
, node
);
262 return &lid
->symbol_binding
;
265 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
267 #define I_SYMBOL_DECL(node) \
268 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
270 /* This flag is set on an identifier if we have previously asked the
271 binding oracle for this identifier's tag binding. */
272 #define I_TAG_CHECKED(node) \
273 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
275 static inline struct c_binding
**
276 i_tag_binding (tree node
)
278 struct lang_identifier
*lid
279 = (struct lang_identifier
*) IDENTIFIER_NODE_CHECK (node
);
281 if (lid
->tag_binding
== NULL
282 && c_binding_oracle
!= NULL
283 && !I_TAG_CHECKED (node
))
285 /* Set the "checked" flag first, to avoid infinite recursion
286 when the binding oracle calls back into gcc. */
287 I_TAG_CHECKED (node
) = 1;
288 c_binding_oracle (C_ORACLE_TAG
, node
);
291 return &lid
->tag_binding
;
294 #define I_TAG_BINDING(node) (*i_tag_binding (node))
296 #define I_TAG_DECL(node) \
297 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
299 /* This flag is set on an identifier if we have previously asked the
300 binding oracle for this identifier's label binding. */
301 #define I_LABEL_CHECKED(node) \
302 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
304 static inline struct c_binding
**
305 i_label_binding (tree node
)
307 struct lang_identifier
*lid
308 = (struct lang_identifier
*) IDENTIFIER_NODE_CHECK (node
);
310 if (lid
->label_binding
== NULL
311 && c_binding_oracle
!= NULL
312 && !I_LABEL_CHECKED (node
))
314 /* Set the "checked" flag first, to avoid infinite recursion
315 when the binding oracle calls back into gcc. */
316 I_LABEL_CHECKED (node
) = 1;
317 c_binding_oracle (C_ORACLE_LABEL
, node
);
320 return &lid
->label_binding
;
323 #define I_LABEL_BINDING(node) (*i_label_binding (node))
325 #define I_LABEL_DECL(node) \
326 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
328 /* Used by C_TOKEN_VEC tree. */
329 struct GTY (()) c_tree_token_vec
{
330 struct tree_base base
;
331 vec
<c_token
, va_gc
> *tokens
;
334 STATIC_ASSERT (sizeof (c_tree_token_vec
) == sizeof (c_tree_token_vec_struct
));
335 STATIC_ASSERT (offsetof (c_tree_token_vec
, tokens
)
336 == offsetof (c_tree_token_vec_struct
, tokens
));
338 /* The resulting tree type. */
340 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE + 2 * (TREE_CODE (&%h.generic) == C_TOKEN_VEC)"),
341 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
343 union tree_node
GTY ((tag ("0"),
344 desc ("tree_node_structure (&%h)")))
346 struct lang_identifier
GTY ((tag ("1"))) identifier
;
347 struct c_tree_token_vec
GTY ((tag ("2"))) c_token_vec
;
350 /* Langhook for tree_size. */
352 c_tree_size (enum tree_code code
)
354 gcc_checking_assert (code
>= NUM_TREE_CODES
);
357 case C_TOKEN_VEC
: return sizeof (c_tree_token_vec
);
359 switch (TREE_CODE_CLASS (code
))
361 case tcc_declaration
: return sizeof (tree_decl_non_common
);
362 case tcc_type
: return sizeof (tree_type_non_common
);
363 default: gcc_unreachable ();
368 /* Track bindings and other things that matter for goto warnings. For
369 efficiency, we do not gather all the decls at the point of
370 definition. Instead, we point into the bindings structure. As
371 scopes are popped, we update these structures and gather the decls
372 that matter at that time. */
374 struct GTY(()) c_spot_bindings
{
375 /* The currently open scope which holds bindings defined when the
376 label was defined or the goto statement was found. */
377 struct c_scope
*scope
;
378 /* The bindings in the scope field which were defined at the point
379 of the label or goto. This lets us look at older or newer
380 bindings in the scope, as appropriate. */
381 struct c_binding
*bindings_in_scope
;
382 /* The number of statement expressions that have started since this
383 label or goto statement was defined. This is zero if we are at
384 the same statement expression level. It is positive if we are in
385 a statement expression started since this spot. It is negative
386 if this spot was in a statement expression and we have left
389 /* Whether we started in a statement expression but are no longer in
390 it. This is set to true if stmt_exprs ever goes negative. */
394 /* This structure is used to keep track of bindings seen when a goto
395 statement is defined. This is only used if we see the goto
396 statement before we see the label. */
398 struct GTY(()) c_goto_bindings
{
399 /* The location of the goto statement. */
401 /* The bindings of the goto statement. */
402 struct c_spot_bindings goto_bindings
;
405 typedef struct c_goto_bindings
*c_goto_bindings_p
;
407 /* The additional information we keep track of for a label binding.
408 These fields are updated as scopes are popped. */
410 struct GTY(()) c_label_vars
{
411 /* The shadowed c_label_vars, when one label shadows another (which
412 can only happen using a __label__ declaration). */
413 struct c_label_vars
*shadowed
;
414 /* The bindings when the label was defined. */
415 struct c_spot_bindings label_bindings
;
416 /* A list of decls that we care about: decls about which we should
417 warn if a goto branches to this label from later in the function.
418 Decls are added to this list as scopes are popped. We only add
419 the decls that matter. */
420 vec
<tree
, va_gc
> *decls_in_scope
;
421 /* A list of goto statements to this label. This is only used for
422 goto statements seen before the label was defined, so that we can
423 issue appropriate warnings for them. */
424 vec
<c_goto_bindings_p
, va_gc
> *gotos
;
427 /* Each c_scope structure describes the complete contents of one
428 scope. Four scopes are distinguished specially: the innermost or
429 current scope, the innermost function scope, the file scope (always
430 the second to outermost) and the outermost or external scope.
432 Most declarations are recorded in the current scope.
434 All normal label declarations are recorded in the innermost
435 function scope, as are bindings of undeclared identifiers to
436 error_mark_node. (GCC permits nested functions as an extension,
437 hence the 'innermost' qualifier.) Explicitly declared labels
438 (using the __label__ extension) appear in the current scope.
440 Being in the file scope (current_scope == file_scope) causes
441 special behavior in several places below. Also, under some
442 conditions the Objective-C front end records declarations in the
443 file scope even though that isn't the current scope.
445 All declarations with external linkage are recorded in the external
446 scope, even if they aren't visible there; this models the fact that
447 such declarations are visible to the entire program, and (with a
448 bit of cleverness, see pushdecl) allows diagnosis of some violations
449 of C99 6.2.2p7 and 6.2.7p2:
451 If, within the same translation unit, the same identifier appears
452 with both internal and external linkage, the behavior is
455 All declarations that refer to the same object or function shall
456 have compatible type; otherwise, the behavior is undefined.
458 Initially only the built-in declarations, which describe compiler
459 intrinsic functions plus a subset of the standard library, are in
462 The order of the blocks list matters, and it is frequently appended
463 to. To avoid having to walk all the way to the end of the list on
464 each insertion, or reverse the list later, we maintain a pointer to
465 the last list entry. (FIXME: It should be feasible to use a reversed
468 The bindings list is strictly in reverse order of declarations;
469 pop_scope relies on this. */
472 struct GTY((chain_next ("%h.outer"))) c_scope
{
473 /* The scope containing this one. */
474 struct c_scope
*outer
;
476 /* The next outermost function scope. */
477 struct c_scope
*outer_function
;
479 /* All bindings in this scope. */
480 struct c_binding
*bindings
;
482 /* For each scope (except the global one), a chain of BLOCK nodes
483 for all the scopes that were entered and exited one level down. */
487 /* The depth of this scope. Used to keep the ->shadowed chain of
488 bindings sorted innermost to outermost. */
489 unsigned int depth
: 28;
491 /* True if we are currently filling this scope with parameter
493 BOOL_BITFIELD parm_flag
: 1;
495 /* True if we saw [*] in this scope. Used to give an error messages
496 if these appears in a function definition. */
497 BOOL_BITFIELD had_vla_unspec
: 1;
499 /* True if we already complained about forward parameter decls
500 in this scope. This prevents double warnings on
501 foo (int a; int b; ...) */
502 BOOL_BITFIELD warned_forward_parm_decls
: 1;
504 /* True if this is the outermost block scope of a function body.
505 This scope contains the parameters, the local variables declared
506 in the outermost block, and all the labels (except those in
507 nested functions, or declared at block scope with __label__). */
508 BOOL_BITFIELD function_body
: 1;
510 /* True means make a BLOCK for this scope no matter what. */
511 BOOL_BITFIELD keep
: 1;
513 /* True means that an unsuffixed float constant is _Decimal64. */
514 BOOL_BITFIELD float_const_decimal64
: 1;
516 /* True if this scope has any label bindings. This is used to speed
517 up searching for labels when popping scopes, particularly since
518 labels are normally only found at function scope. */
519 BOOL_BITFIELD has_label_bindings
: 1;
521 /* True if we should issue a warning if a goto statement crosses any
522 of the bindings. We still need to check the list of bindings to
523 find the specific ones we need to warn about. This is true if
524 decl_jump_unsafe would return true for any of the bindings. This
525 is used to avoid looping over all the bindings unnecessarily. */
526 BOOL_BITFIELD has_jump_unsafe_decl
: 1;
529 /* The scope currently in effect. */
531 static GTY(()) struct c_scope
*current_scope
;
533 /* The innermost function scope. Ordinary (not explicitly declared)
534 labels, bindings to error_mark_node, and the lazily-created
535 bindings of __func__ and its friends get this scope. */
537 static GTY(()) struct c_scope
*current_function_scope
;
539 /* The C file scope. This is reset for each input translation unit. */
541 static GTY(()) struct c_scope
*file_scope
;
543 /* The outermost scope. This is used for all declarations with
544 external linkage, and only these, hence the name. */
546 static GTY(()) struct c_scope
*external_scope
;
548 /* A chain of c_scope structures awaiting reuse. */
550 static GTY((deletable
)) struct c_scope
*scope_freelist
;
552 /* A chain of c_binding structures awaiting reuse. */
554 static GTY((deletable
)) struct c_binding
*binding_freelist
;
556 /* Append VAR to LIST in scope SCOPE. */
557 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
558 struct c_scope *s_ = (scope); \
560 if (s_->list##_last) \
561 BLOCK_CHAIN (s_->list##_last) = d_; \
564 s_->list##_last = d_; \
567 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
568 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
569 struct c_scope *t_ = (tscope); \
570 struct c_scope *f_ = (fscope); \
572 BLOCK_CHAIN (t_->to##_last) = f_->from; \
575 t_->to##_last = f_->from##_last; \
578 /* A c_inline_static structure stores details of a static identifier
579 referenced in a definition of a function that may be an inline
580 definition if no subsequent declaration of that function uses
581 "extern" or does not use "inline". */
583 struct GTY((chain_next ("%h.next"))) c_inline_static
{
584 /* The location for a diagnostic. */
587 /* The function that may be an inline definition. */
590 /* The object or function referenced. */
593 /* What sort of reference this is. */
594 enum c_inline_static_type type
;
596 /* The next such structure or NULL. */
597 struct c_inline_static
*next
;
600 /* List of static identifiers used or referenced in functions that may
601 be inline definitions. */
602 static GTY(()) struct c_inline_static
*c_inline_statics
;
604 /* True means unconditionally make a BLOCK for the next scope pushed. */
606 static bool keep_next_level_flag
;
608 /* True means the next call to push_scope will be the outermost scope
609 of a function body, so do not push a new scope, merely cease
610 expecting parameter decls. */
612 static bool next_is_function_body
;
614 /* A vector of pointers to c_binding structures. */
616 typedef struct c_binding
*c_binding_ptr
;
618 /* Information that we keep for a struct or union while it is being
621 class c_struct_parse_info
624 /* If warn_cxx_compat, a list of types defined within this
626 auto_vec
<tree
> struct_types
;
627 /* If warn_cxx_compat, a list of field names which have bindings,
628 and which are defined in this struct, but which are not defined
629 in any enclosing struct. This is used to clear the in_struct
630 field of the c_bindings structure. */
631 auto_vec
<c_binding_ptr
> fields
;
632 /* If warn_cxx_compat, a list of typedef names used when defining
633 fields in this struct. */
634 auto_vec
<tree
> typedefs_seen
;
638 /* Hash table for structs and unions. */
639 struct c_struct_hasher
: ggc_ptr_hash
<tree_node
>
641 static hashval_t
hash (tree t
);
642 static bool equal (tree
, tree
);
645 /* Hash an RECORD OR UNION. */
647 c_struct_hasher::hash (tree type
)
649 inchash::hash hstate
;
651 hstate
.add_int (TREE_CODE (type
));
652 hstate
.add_object (TYPE_NAME (type
));
654 return hstate
.end ();
657 /* Compare two RECORD or UNION types. */
659 c_struct_hasher::equal (tree t1
, tree t2
)
661 return comptypes_equiv_p (t1
, t2
);
664 /* All tagged typed so that TYPE_CANONICAL can be set correctly. */
665 static GTY (()) hash_table
<c_struct_hasher
> *c_struct_htab
;
667 /* Information for the struct or union currently being parsed, or
668 NULL if not parsing a struct or union. */
669 static class c_struct_parse_info
*struct_parse_info
;
671 /* Forward declarations. */
672 static tree
lookup_name_in_scope (tree
, struct c_scope
*);
673 static tree
c_make_fname_decl (location_t
, tree
, int);
674 static tree
grokdeclarator (const struct c_declarator
*,
675 struct c_declspecs
*,
676 enum decl_context
, bool, tree
*, tree
*, tree
*,
677 bool *, enum deprecated_states
);
678 static tree
grokparms (struct c_arg_info
*, bool);
679 static void layout_array_type (tree
);
680 static const char *header_for_builtin_fn (tree
);
682 /* T is a statement. Add it to the statement-tree. This is the
683 C/ObjC version--C++ has a slightly different version of this
689 enum tree_code code
= TREE_CODE (t
);
691 if (CAN_HAVE_LOCATION_P (t
) && code
!= LABEL_EXPR
)
693 if (!EXPR_HAS_LOCATION (t
))
694 SET_EXPR_LOCATION (t
, input_location
);
697 if (code
== LABEL_EXPR
|| code
== CASE_LABEL_EXPR
)
698 STATEMENT_LIST_HAS_LABEL (cur_stmt_list
) = 1;
700 /* Add T to the statement-tree. Non-side-effect statements need to be
701 recorded during statement expressions. */
702 if (!building_stmt_list_p ())
704 append_to_statement_list_force (t
, &cur_stmt_list
);
709 /* Build a pointer type using the default pointer mode. */
712 c_build_pointer_type (tree to_type
)
714 addr_space_t as
= to_type
== error_mark_node
? ADDR_SPACE_GENERIC
715 : TYPE_ADDR_SPACE (to_type
);
716 machine_mode pointer_mode
;
718 if (as
!= ADDR_SPACE_GENERIC
|| c_default_pointer_mode
== VOIDmode
)
719 pointer_mode
= targetm
.addr_space
.pointer_mode (as
);
721 pointer_mode
= c_default_pointer_mode
;
722 return build_pointer_type_for_mode (to_type
, pointer_mode
, false);
726 /* Return true if we will want to say something if a goto statement
730 decl_jump_unsafe (tree decl
)
732 if (error_operand_p (decl
))
735 /* Don't warn for compound literals. If a goto statement crosses
736 their initialization, it should cross also all the places where
737 the complit is used or where the complit address might be saved
738 into some variable, so code after the label to which goto jumps
739 should not be able to refer to the compound literal. */
740 if (VAR_P (decl
) && C_DECL_COMPOUND_LITERAL_P (decl
))
745 && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl
)))
748 /* Always warn about crossing variably modified types. */
749 if ((VAR_P (decl
) || TREE_CODE (decl
) == TYPE_DECL
)
750 && c_type_variably_modified_p (TREE_TYPE (decl
)))
753 /* Otherwise, only warn if -Wgoto-misses-init and this is an
754 initialized automatic decl. */
755 if (warn_jump_misses_init
757 && !TREE_STATIC (decl
)
758 && DECL_INITIAL (decl
) != NULL_TREE
)
766 c_print_identifier (FILE *file
, tree node
, int indent
)
768 void (*save
) (enum c_oracle_request
, tree identifier
);
770 /* Temporarily hide any binding oracle. Without this, calls to
771 debug_tree from the debugger will end up calling into the oracle,
772 making for a confusing debug session. As the oracle isn't needed
773 here for normal operation, it's simplest to suppress it. */
774 save
= c_binding_oracle
;
775 c_binding_oracle
= NULL
;
777 print_node (file
, "symbol", I_SYMBOL_DECL (node
), indent
+ 4);
778 print_node (file
, "tag", I_TAG_DECL (node
), indent
+ 4);
779 print_node (file
, "label", I_LABEL_DECL (node
), indent
+ 4);
780 if (C_IS_RESERVED_WORD (node
) && C_RID_CODE (node
) != RID_CXX_COMPAT_WARN
)
782 tree rid
= ridpointers
[C_RID_CODE (node
)];
783 indent_to (file
, indent
+ 4);
784 fprintf (file
, "rid " HOST_PTR_PRINTF
" \"%s\"",
785 (void *) rid
, IDENTIFIER_POINTER (rid
));
788 c_binding_oracle
= save
;
791 /* Establish that the scope contains declarations that are sensitive to
792 jumps that cross a binding. Together with decl_jump_unsafe, this is
793 used to diagnose such jumps. */
795 c_mark_decl_jump_unsafe_in_current_scope ()
797 current_scope
->has_jump_unsafe_decl
= 1;
800 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
801 which may be any of several kinds of DECL or TYPE or error_mark_node,
802 in the scope SCOPE. */
804 bind (tree name
, tree decl
, struct c_scope
*scope
, bool invisible
,
805 bool nested
, location_t locus
)
807 struct c_binding
*b
, **here
;
809 if (binding_freelist
)
811 b
= binding_freelist
;
812 binding_freelist
= b
->prev
;
815 b
= ggc_alloc
<c_binding
> ();
820 b
->depth
= scope
->depth
;
821 b
->invisible
= invisible
;
829 b
->prev
= scope
->bindings
;
832 if (decl_jump_unsafe (decl
))
833 scope
->has_jump_unsafe_decl
= 1;
838 switch (TREE_CODE (decl
))
840 case LABEL_DECL
: here
= &I_LABEL_BINDING (name
); break;
843 case RECORD_TYPE
: here
= &I_TAG_BINDING (name
); break;
849 case ERROR_MARK
: here
= &I_SYMBOL_BINDING (name
); break;
855 /* Locate the appropriate place in the chain of shadowed decls
856 to insert this binding. Normally, scope == current_scope and
857 this does nothing. */
858 while (*here
&& (*here
)->depth
> scope
->depth
)
859 here
= &(*here
)->shadowed
;
865 /* Clear the binding structure B, stick it on the binding_freelist,
866 and return the former value of b->prev. This is used by pop_scope
867 and get_parm_info to iterate destructively over all the bindings
868 from a given scope. */
869 static struct c_binding
*
870 free_binding_and_advance (struct c_binding
*b
)
872 struct c_binding
*prev
= b
->prev
;
874 memset (b
, 0, sizeof (struct c_binding
));
875 b
->prev
= binding_freelist
;
876 binding_freelist
= b
;
881 /* Bind a label. Like bind, but skip fields which aren't used for
882 labels, and add the LABEL_VARS value. */
884 bind_label (tree name
, tree label
, struct c_scope
*scope
,
885 struct c_label_vars
*label_vars
)
889 bind (name
, label
, scope
, /*invisible=*/false, /*nested=*/false,
892 scope
->has_label_bindings
= true;
895 gcc_assert (b
->decl
== label
);
896 label_vars
->shadowed
= b
->u
.label
;
897 b
->u
.label
= label_vars
;
900 /* Hook called at end of compilation to assume 1 elt
901 for a file-scope tentative array defn that wasn't complete before. */
904 c_finish_incomplete_decl (tree decl
)
908 tree type
= TREE_TYPE (decl
);
909 if (type
!= error_mark_node
910 && TREE_CODE (type
) == ARRAY_TYPE
911 && !DECL_EXTERNAL (decl
)
912 && TYPE_DOMAIN (type
) == NULL_TREE
)
914 warning_at (DECL_SOURCE_LOCATION (decl
),
915 0, "array %q+D assumed to have one element", decl
);
917 complete_array_type (&TREE_TYPE (decl
), NULL_TREE
, true);
919 relayout_decl (decl
);
924 /* Record that inline function FUNC contains a reference (location
925 LOC) to static DECL (file-scope or function-local according to
929 record_inline_static (location_t loc
, tree func
, tree decl
,
930 enum c_inline_static_type type
)
932 c_inline_static
*csi
= ggc_alloc
<c_inline_static
> ();
934 csi
->function
= func
;
935 csi
->static_decl
= decl
;
937 csi
->next
= c_inline_statics
;
938 c_inline_statics
= csi
;
941 /* Check for references to static declarations in inline functions at
942 the end of the translation unit and diagnose them if the functions
943 are still inline definitions. */
946 check_inline_statics (void)
948 struct c_inline_static
*csi
;
949 for (csi
= c_inline_statics
; csi
; csi
= csi
->next
)
951 if (DECL_EXTERNAL (csi
->function
))
955 pedwarn (csi
->location
, 0,
956 "%qD is static but used in inline function %qD "
957 "which is not static", csi
->static_decl
, csi
->function
);
960 pedwarn (csi
->location
, 0,
961 "%q+D is static but declared in inline function %qD "
962 "which is not static", csi
->static_decl
, csi
->function
);
968 c_inline_statics
= NULL
;
971 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
972 for the current state, otherwise set it to uninitialized. */
975 set_spot_bindings (struct c_spot_bindings
*p
, bool defining
)
979 p
->scope
= current_scope
;
980 p
->bindings_in_scope
= current_scope
->bindings
;
985 p
->bindings_in_scope
= NULL
;
988 p
->left_stmt_expr
= false;
991 /* Update spot bindings P as we pop out of SCOPE. Return true if we
992 should push decls for a label. */
995 update_spot_bindings (struct c_scope
*scope
, struct c_spot_bindings
*p
)
997 if (p
->scope
!= scope
)
999 /* This label or goto is defined in some other scope, or it is a
1000 label which is not yet defined. There is nothing to
1005 /* Adjust the spot bindings to refer to the bindings already defined
1006 in the enclosing scope. */
1007 p
->scope
= scope
->outer
;
1008 p
->bindings_in_scope
= p
->scope
->bindings
;
1013 /* The Objective-C front-end often needs to determine the current scope. */
1016 objc_get_current_scope (void)
1018 return current_scope
;
1021 /* The following function is used only by Objective-C. It needs to live here
1022 because it accesses the innards of c_scope. */
1025 objc_mark_locals_volatile (void *enclosing_blk
)
1027 struct c_scope
*scope
;
1028 struct c_binding
*b
;
1030 for (scope
= current_scope
;
1031 scope
&& scope
!= enclosing_blk
;
1032 scope
= scope
->outer
)
1034 for (b
= scope
->bindings
; b
; b
= b
->prev
)
1035 objc_volatilize_decl (b
->decl
);
1037 /* Do not climb up past the current function. */
1038 if (scope
->function_body
)
1043 /* Return true if we are in the global binding level. */
1046 global_bindings_p (void)
1048 return current_scope
== file_scope
;
1051 /* Return true if we're declaring parameters in an old-style function
1055 old_style_parameter_scope (void)
1057 /* If processing parameters and there is no function statement list, we
1058 * have an old-style function declaration. */
1059 return (current_scope
->parm_flag
&& !DECL_SAVED_TREE (current_function_decl
));
1063 keep_next_level (void)
1065 keep_next_level_flag
= true;
1068 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
1071 set_float_const_decimal64 (void)
1073 current_scope
->float_const_decimal64
= true;
1076 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
1079 clear_float_const_decimal64 (void)
1081 current_scope
->float_const_decimal64
= false;
1084 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
1087 float_const_decimal64_p (void)
1089 return current_scope
->float_const_decimal64
;
1092 /* Identify this scope as currently being filled with parameters. */
1095 declare_parm_level (void)
1097 current_scope
->parm_flag
= true;
1103 if (next_is_function_body
)
1105 /* This is the transition from the parameters to the top level
1106 of the function body. These are the same scope
1107 (C99 6.2.1p4,6) so we do not push another scope structure.
1108 next_is_function_body is set only by store_parm_decls, which
1109 in turn is called when and only when we are about to
1110 encounter the opening curly brace for the function body.
1112 The outermost block of a function always gets a BLOCK node,
1113 because the debugging output routines expect that each
1114 function has at least one BLOCK. */
1115 current_scope
->parm_flag
= false;
1116 current_scope
->function_body
= true;
1117 current_scope
->keep
= true;
1118 current_scope
->outer_function
= current_function_scope
;
1119 current_function_scope
= current_scope
;
1121 keep_next_level_flag
= false;
1122 next_is_function_body
= false;
1124 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1125 if (current_scope
->outer
)
1126 current_scope
->float_const_decimal64
1127 = current_scope
->outer
->float_const_decimal64
;
1129 current_scope
->float_const_decimal64
= false;
1133 struct c_scope
*scope
;
1136 scope
= scope_freelist
;
1137 scope_freelist
= scope
->outer
;
1140 scope
= ggc_cleared_alloc
<c_scope
> ();
1142 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1144 scope
->float_const_decimal64
= current_scope
->float_const_decimal64
;
1146 scope
->float_const_decimal64
= false;
1148 scope
->keep
= keep_next_level_flag
;
1149 scope
->outer
= current_scope
;
1150 scope
->depth
= current_scope
? (current_scope
->depth
+ 1) : 0;
1152 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1154 if (current_scope
&& scope
->depth
== 0)
1157 sorry ("GCC supports only %u nested scopes", scope
->depth
);
1160 current_scope
= scope
;
1161 keep_next_level_flag
= false;
1165 /* This is called when we are leaving SCOPE. For each label defined
1166 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1167 These are the decls whose initialization will be skipped by a goto
1168 later in the function. */
1171 update_label_decls (struct c_scope
*scope
)
1178 if (s
->has_label_bindings
)
1180 struct c_binding
*b
;
1182 for (b
= s
->bindings
; b
!= NULL
; b
= b
->prev
)
1184 struct c_label_vars
*label_vars
;
1185 struct c_binding
*b1
;
1188 struct c_goto_bindings
*g
;
1190 if (TREE_CODE (b
->decl
) != LABEL_DECL
)
1192 label_vars
= b
->u
.label
;
1194 b1
= label_vars
->label_bindings
.bindings_in_scope
;
1195 if (label_vars
->label_bindings
.scope
== NULL
)
1198 hjud
= label_vars
->label_bindings
.scope
->has_jump_unsafe_decl
;
1199 if (update_spot_bindings (scope
, &label_vars
->label_bindings
))
1201 /* This label is defined in this scope. */
1204 for (; b1
!= NULL
; b1
= b1
->prev
)
1206 /* A goto from later in the function to this
1207 label will never see the initialization
1208 of B1, if any. Save it to issue a
1209 warning if needed. */
1210 if (decl_jump_unsafe (b1
->decl
))
1211 vec_safe_push(label_vars
->decls_in_scope
, b1
->decl
);
1216 /* Update the bindings of any goto statements associated
1218 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
1219 update_spot_bindings (scope
, &g
->goto_bindings
);
1223 /* Don't search beyond the current function. */
1224 if (s
== current_function_scope
)
1231 /* Exit a scope. Restore the state of the identifier-decl mappings
1232 that were in effect when this scope was entered. Return a BLOCK
1233 node containing all the DECLs in this scope that are of interest
1234 to debug info generation. */
1239 struct c_scope
*scope
= current_scope
;
1240 tree block
, context
, p
;
1241 struct c_binding
*b
;
1243 bool functionbody
= scope
->function_body
;
1244 bool keep
= functionbody
|| scope
->keep
|| scope
->bindings
;
1246 update_label_decls (scope
);
1248 /* If appropriate, create a BLOCK to record the decls for the life
1249 of this function. */
1253 block
= make_node (BLOCK
);
1254 BLOCK_SUBBLOCKS (block
) = scope
->blocks
;
1255 TREE_USED (block
) = 1;
1257 /* In each subblock, record that this is its superior. */
1258 for (p
= scope
->blocks
; p
; p
= BLOCK_CHAIN (p
))
1259 BLOCK_SUPERCONTEXT (p
) = block
;
1261 BLOCK_VARS (block
) = NULL_TREE
;
1264 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1265 scope must be set so that they point to the appropriate
1266 construct, i.e. either to the current FUNCTION_DECL node, or
1267 else to the BLOCK node we just constructed.
1269 Note that for tagged types whose scope is just the formal
1270 parameter list for some function type specification, we can't
1271 properly set their TYPE_CONTEXTs here, because we don't have a
1272 pointer to the appropriate FUNCTION_TYPE node readily available
1273 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1274 type nodes get set in `grokdeclarator' as soon as we have created
1275 the FUNCTION_TYPE node which will represent the "scope" for these
1276 "parameter list local" tagged types. */
1277 if (scope
->function_body
)
1278 context
= current_function_decl
;
1279 else if (scope
== file_scope
)
1282 = build_translation_unit_decl (get_identifier (main_input_filename
));
1283 context
= file_decl
;
1284 debug_hooks
->register_main_translation_unit (file_decl
);
1289 /* Clear all bindings in this scope. */
1290 for (b
= scope
->bindings
; b
; b
= free_binding_and_advance (b
))
1293 switch (TREE_CODE (p
))
1296 /* Warnings for unused labels, errors for undefined labels. */
1297 if (TREE_USED (p
) && !DECL_INITIAL (p
))
1299 error ("label %q+D used but not defined", p
);
1300 DECL_INITIAL (p
) = error_mark_node
;
1303 warn_for_unused_label (p
);
1305 /* Labels go in BLOCK_VARS. */
1306 DECL_CHAIN (p
) = BLOCK_VARS (block
);
1307 BLOCK_VARS (block
) = p
;
1308 gcc_assert (I_LABEL_BINDING (b
->id
) == b
);
1309 I_LABEL_BINDING (b
->id
) = b
->shadowed
;
1311 /* Also pop back to the shadowed label_vars. */
1312 release_tree_vector (b
->u
.label
->decls_in_scope
);
1313 b
->u
.label
= b
->u
.label
->shadowed
;
1320 /* Types may not have tag-names, in which case the type
1321 appears in the bindings list with b->id NULL. */
1324 gcc_assert (I_TAG_BINDING (b
->id
) == b
);
1325 I_TAG_BINDING (b
->id
) = b
->shadowed
;
1330 /* Propagate TREE_ADDRESSABLE from nested functions to their
1331 containing functions. */
1332 if (!TREE_ASM_WRITTEN (p
)
1333 && DECL_INITIAL (p
) != NULL_TREE
1334 && TREE_ADDRESSABLE (p
)
1335 && DECL_ABSTRACT_ORIGIN (p
) != NULL_TREE
1336 && DECL_ABSTRACT_ORIGIN (p
) != p
)
1337 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p
)) = 1;
1338 if (!TREE_PUBLIC (p
)
1339 && !DECL_INITIAL (p
)
1341 && scope
!= file_scope
1342 && scope
!= external_scope
)
1344 error ("nested function %q+D declared but never defined", p
);
1345 undef_nested_function
= true;
1347 else if (DECL_DECLARED_INLINE_P (p
)
1349 && !DECL_INITIAL (p
))
1351 /* C99 6.7.4p6: "a function with external linkage... declared
1352 with an inline function specifier ... shall also be defined
1353 in the same translation unit." */
1354 if (!flag_gnu89_inline
1355 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p
))
1356 && scope
== external_scope
)
1357 pedwarn (input_location
, 0,
1358 "inline function %q+D declared but never defined", p
);
1359 DECL_EXTERNAL (p
) = 1;
1365 /* Warnings for unused variables. */
1366 if ((!TREE_USED (p
) || !DECL_READ_P (p
))
1367 && !warning_suppressed_p (p
, OPT_Wunused_but_set_variable
)
1368 && !DECL_IN_SYSTEM_HEADER (p
)
1370 && !DECL_ARTIFICIAL (p
)
1371 && scope
!= file_scope
1372 && scope
!= external_scope
)
1376 warning (OPT_Wunused_variable
, "unused variable %q+D", p
);
1377 suppress_warning (p
, OPT_Wunused_variable
);
1379 else if (DECL_CONTEXT (p
) == current_function_decl
)
1380 warning_at (DECL_SOURCE_LOCATION (p
),
1381 OPT_Wunused_but_set_variable
,
1382 "variable %qD set but not used", p
);
1387 error ("type of array %q+D completed incompatibly with"
1388 " implicit initialization", p
);
1395 /* All of these go in BLOCK_VARS, but only if this is the
1396 binding in the home scope. */
1399 DECL_CHAIN (p
) = BLOCK_VARS (block
);
1400 BLOCK_VARS (block
) = p
;
1402 else if (VAR_OR_FUNCTION_DECL_P (p
) && scope
!= file_scope
)
1404 /* For block local externs add a special
1405 DECL_EXTERNAL decl for debug info generation. */
1406 tree extp
= copy_node (p
);
1408 DECL_EXTERNAL (extp
) = 1;
1409 TREE_STATIC (extp
) = 0;
1410 TREE_PUBLIC (extp
) = 1;
1411 DECL_INITIAL (extp
) = NULL_TREE
;
1412 DECL_LANG_SPECIFIC (extp
) = NULL
;
1413 DECL_CONTEXT (extp
) = current_function_decl
;
1414 if (TREE_CODE (p
) == FUNCTION_DECL
)
1416 DECL_RESULT (extp
) = NULL_TREE
;
1417 DECL_SAVED_TREE (extp
) = NULL_TREE
;
1418 DECL_STRUCT_FUNCTION (extp
) = NULL
;
1420 if (b
->locus
!= UNKNOWN_LOCATION
)
1421 DECL_SOURCE_LOCATION (extp
) = b
->locus
;
1422 DECL_CHAIN (extp
) = BLOCK_VARS (block
);
1423 BLOCK_VARS (block
) = extp
;
1425 /* If this is the file scope set DECL_CONTEXT of each decl to
1426 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1428 if (scope
== file_scope
)
1429 DECL_CONTEXT (p
) = context
;
1432 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1433 already been put there by store_parm_decls. Unused-
1434 parameter warnings are handled by function.cc.
1435 error_mark_node obviously does not go in BLOCK_VARS and
1436 does not get unused-variable warnings. */
1439 /* It is possible for a decl not to have a name. We get
1440 here with b->id NULL in this case. */
1443 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
1444 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
1445 if (b
->shadowed
&& b
->shadowed
->u
.type
)
1446 TREE_TYPE (b
->shadowed
->decl
) = b
->shadowed
->u
.type
;
1456 /* Dispose of the block that we just made inside some higher level. */
1457 if ((scope
->function_body
|| scope
== file_scope
) && context
)
1459 DECL_INITIAL (context
) = block
;
1460 BLOCK_SUPERCONTEXT (block
) = context
;
1462 else if (scope
->outer
)
1465 SCOPE_LIST_APPEND (scope
->outer
, blocks
, block
);
1466 /* If we did not make a block for the scope just exited, any
1467 blocks made for inner scopes must be carried forward so they
1468 will later become subblocks of something else. */
1469 else if (scope
->blocks
)
1470 SCOPE_LIST_CONCAT (scope
->outer
, blocks
, scope
, blocks
);
1473 /* Pop the current scope, and free the structure for reuse. */
1474 current_scope
= scope
->outer
;
1475 if (scope
->function_body
)
1476 current_function_scope
= scope
->outer_function
;
1478 memset (scope
, 0, sizeof (struct c_scope
));
1479 scope
->outer
= scope_freelist
;
1480 scope_freelist
= scope
;
1486 push_file_scope (void)
1494 file_scope
= current_scope
;
1496 start_fname_decls ();
1498 for (decl
= visible_builtins
; decl
; decl
= DECL_CHAIN (decl
))
1499 bind (DECL_NAME (decl
), decl
, file_scope
,
1500 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl
));
1504 pop_file_scope (void)
1506 /* In case there were missing closebraces, get us back to the global
1508 while (current_scope
!= file_scope
)
1511 /* __FUNCTION__ is defined at file scope (""). This
1512 call may not be necessary as my tests indicate it
1513 still works without it. */
1514 finish_fname_decls ();
1516 check_inline_statics ();
1518 /* This is the point to write out a PCH if we're doing that.
1519 In that case we do not want to do anything else. */
1522 c_common_write_pch ();
1523 /* Ensure even the callers don't try to finalize the CU. */
1524 flag_syntax_only
= 1;
1528 /* Pop off the file scope and close this translation unit. */
1532 maybe_apply_pending_pragma_weaks ();
1535 /* Whether we are curently inside the initializer for an
1536 underspecified object definition (C23 auto or constexpr). */
1537 static bool in_underspecified_init
;
1539 /* Start an underspecified object definition for NAME at LOC. This
1540 means that NAME is shadowed inside its initializer, so neither the
1541 definition being initialized, nor any definition from an outer
1542 scope, may be referenced during that initializer. Return state to
1543 be passed to finish_underspecified_init. If NAME is NULL_TREE, the
1544 underspecified object is a (constexpr) compound literal; there is
1545 no shadowing in that case, but all the other restrictions on
1546 underspecified object definitions still apply. */
1548 start_underspecified_init (location_t loc
, tree name
)
1550 bool prev
= in_underspecified_init
;
1552 if (name
== NULL_TREE
)
1556 tree decl
= build_decl (loc
, VAR_DECL
, name
, error_mark_node
);
1557 C_DECL_UNDERSPECIFIED (decl
) = 1;
1558 struct c_scope
*scope
= current_scope
;
1559 struct c_binding
*b
= I_SYMBOL_BINDING (name
);
1560 if (b
&& B_IN_SCOPE (b
, scope
))
1562 error_at (loc
, "underspecified declaration of %qE, which is already "
1563 "declared in this scope", name
);
1568 bind (name
, decl
, scope
, false, false, loc
);
1572 in_underspecified_init
= true;
1573 return ok
| (prev
<< 1);
1576 /* Finish an underspecified object definition for NAME, before that
1577 name is bound to the real declaration instead of a placeholder.
1578 PREV_STATE is the value returned by the call to
1579 start_underspecified_init. If NAME is NULL_TREE, this means a
1580 compound literal, as for start_underspecified_init. */
1582 finish_underspecified_init (tree name
, unsigned int prev_state
)
1584 if (name
!= NULL_TREE
&& (prev_state
& 1))
1586 /* A VAR_DECL was bound to the name to shadow any previous
1587 declarations for the name; remove that binding now. */
1588 struct c_scope
*scope
= current_scope
;
1589 struct c_binding
*b
= I_SYMBOL_BINDING (name
);
1591 gcc_assert (B_IN_SCOPE (b
, scope
));
1592 gcc_assert (VAR_P (b
->decl
));
1593 gcc_assert (C_DECL_UNDERSPECIFIED (b
->decl
));
1594 I_SYMBOL_BINDING (name
) = b
->shadowed
;
1595 /* In erroneous cases there may be other bindings added to this
1596 scope during the initializer. */
1597 struct c_binding
**p
= &scope
->bindings
;
1600 *p
= free_binding_and_advance (*p
);
1602 in_underspecified_init
= (prev_state
& (1u << 1)) >> 1;
1605 /* Adjust the bindings for the start of a statement expression. */
1608 c_bindings_start_stmt_expr (struct c_spot_bindings
* switch_bindings
)
1610 struct c_scope
*scope
;
1612 for (scope
= current_scope
; scope
!= NULL
; scope
= scope
->outer
)
1614 struct c_binding
*b
;
1616 if (!scope
->has_label_bindings
)
1619 for (b
= scope
->bindings
; b
!= NULL
; b
= b
->prev
)
1621 struct c_label_vars
*label_vars
;
1623 struct c_goto_bindings
*g
;
1625 if (TREE_CODE (b
->decl
) != LABEL_DECL
)
1627 label_vars
= b
->u
.label
;
1628 ++label_vars
->label_bindings
.stmt_exprs
;
1629 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
1630 ++g
->goto_bindings
.stmt_exprs
;
1634 if (switch_bindings
!= NULL
)
1635 ++switch_bindings
->stmt_exprs
;
1638 /* Adjust the bindings for the end of a statement expression. */
1641 c_bindings_end_stmt_expr (struct c_spot_bindings
*switch_bindings
)
1643 struct c_scope
*scope
;
1645 for (scope
= current_scope
; scope
!= NULL
; scope
= scope
->outer
)
1647 struct c_binding
*b
;
1649 if (!scope
->has_label_bindings
)
1652 for (b
= scope
->bindings
; b
!= NULL
; b
= b
->prev
)
1654 struct c_label_vars
*label_vars
;
1656 struct c_goto_bindings
*g
;
1658 if (TREE_CODE (b
->decl
) != LABEL_DECL
)
1660 label_vars
= b
->u
.label
;
1661 --label_vars
->label_bindings
.stmt_exprs
;
1662 if (label_vars
->label_bindings
.stmt_exprs
< 0)
1664 label_vars
->label_bindings
.left_stmt_expr
= true;
1665 label_vars
->label_bindings
.stmt_exprs
= 0;
1667 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
1669 --g
->goto_bindings
.stmt_exprs
;
1670 if (g
->goto_bindings
.stmt_exprs
< 0)
1672 g
->goto_bindings
.left_stmt_expr
= true;
1673 g
->goto_bindings
.stmt_exprs
= 0;
1679 if (switch_bindings
!= NULL
)
1681 --switch_bindings
->stmt_exprs
;
1682 gcc_assert (switch_bindings
->stmt_exprs
>= 0);
1686 /* Push a definition or a declaration of struct, union or enum tag "name".
1687 "type" should be the type node.
1688 We assume that the tag "name" is not already defined, and has a location
1691 Note that the definition may really be just a forward reference.
1692 In that case, the TYPE_SIZE will be zero. */
1695 pushtag (location_t loc
, tree name
, tree type
)
1697 /* Record the identifier as the type's name if it has none. */
1698 if (name
&& !TYPE_NAME (type
))
1699 TYPE_NAME (type
) = name
;
1700 bind (name
, type
, current_scope
, /*invisible=*/false, /*nested=*/false, loc
);
1702 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1703 tagged type we just added to the current scope. This fake
1704 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1705 to output a representation of a tagged type, and it also gives
1706 us a convenient place to record the "scope start" address for the
1709 TYPE_STUB_DECL (type
) = pushdecl (build_decl (loc
,
1710 TYPE_DECL
, NULL_TREE
, type
));
1712 /* An approximation for now, so we can tell this is a function-scope tag.
1713 This will be updated in pop_scope. */
1714 TYPE_CONTEXT (type
) = DECL_CONTEXT (TYPE_STUB_DECL (type
));
1716 if (warn_cxx_compat
&& name
!= NULL_TREE
)
1718 struct c_binding
*b
= I_SYMBOL_BINDING (name
);
1721 && b
->decl
!= NULL_TREE
1722 && TREE_CODE (b
->decl
) == TYPE_DECL
1723 && (B_IN_CURRENT_SCOPE (b
)
1724 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
1725 && (TYPE_MAIN_VARIANT (TREE_TYPE (b
->decl
))
1726 != TYPE_MAIN_VARIANT (type
)))
1728 auto_diagnostic_group d
;
1729 if (warning_at (loc
, OPT_Wc___compat
,
1730 ("using %qD as both a typedef and a tag is "
1731 "invalid in C++"), b
->decl
)
1732 && b
->locus
!= UNKNOWN_LOCATION
)
1733 inform (b
->locus
, "originally defined here");
1738 /* An exported interface to pushtag. This is used by the gdb plugin's
1739 binding oracle to introduce a new tag binding. */
1742 c_pushtag (location_t loc
, tree name
, tree type
)
1744 pushtag (loc
, name
, type
);
1747 /* An exported interface to bind a declaration. LOC is the location
1748 to use. DECL is the declaration to bind. The decl's name is used
1749 to determine how it is bound. If DECL is a VAR_DECL, then
1750 IS_GLOBAL determines whether the decl is put into the global (file
1751 and external) scope or the current function's scope; if DECL is not
1752 a VAR_DECL then it is always put into the file scope. */
1755 c_bind (location_t loc
, tree decl
, bool is_global
)
1757 struct c_scope
*scope
;
1758 bool nested
= false;
1760 if (!VAR_P (decl
) || current_function_scope
== NULL
)
1762 /* Types and functions are always considered to be global. */
1764 DECL_EXTERNAL (decl
) = 1;
1765 TREE_PUBLIC (decl
) = 1;
1769 /* Also bind it into the external scope. */
1770 bind (DECL_NAME (decl
), decl
, external_scope
, true, false, loc
);
1773 DECL_EXTERNAL (decl
) = 1;
1774 TREE_PUBLIC (decl
) = 1;
1778 DECL_CONTEXT (decl
) = current_function_decl
;
1779 TREE_PUBLIC (decl
) = 0;
1780 scope
= current_function_scope
;
1783 bind (DECL_NAME (decl
), decl
, scope
, false, nested
, loc
);
1787 /* Stores the first FILE*, const struct tm* etc. argument type (whatever
1788 it is) seen in a declaration of a file I/O etc. built-in, corresponding
1789 to the builtin_structptr_types array. Subsequent declarations of such
1790 built-ins are expected to refer to it rather than to fileptr_type_node,
1791 etc. which is just void* (or to any other type).
1792 Used only by match_builtin_function_types. */
1794 static const unsigned builtin_structptr_type_count
1795 = ARRAY_SIZE (builtin_structptr_types
);
1797 static GTY(()) tree last_structptr_types
[builtin_structptr_type_count
];
1799 /* Returns true if types T1 and T2 representing return types or types
1800 of function arguments are close enough to be considered interchangeable
1801 in redeclarations of built-in functions. */
1804 types_close_enough_to_match (tree t1
, tree t2
)
1806 return (TYPE_MODE (t1
) == TYPE_MODE (t2
)
1807 && POINTER_TYPE_P (t1
) == POINTER_TYPE_P (t2
)
1808 && FUNCTION_POINTER_TYPE_P (t1
) == FUNCTION_POINTER_TYPE_P (t2
));
1811 /* Subroutine of compare_decls. Allow harmless mismatches in return
1812 and argument types provided that the type modes match. Set *STRICT
1813 and *ARGNO to the expected argument type and number in case of
1814 an argument type mismatch or null and zero otherwise. Return
1815 a unified type given a suitable match, and 0 otherwise. */
1818 match_builtin_function_types (tree newtype
, tree oldtype
,
1819 tree
*strict
, unsigned *argno
)
1822 *strict
= NULL_TREE
;
1824 /* Accept the return type of the new declaration if it has the same
1825 mode and if they're both pointers or if neither is. */
1826 tree oldrettype
= TREE_TYPE (oldtype
);
1827 tree newrettype
= TREE_TYPE (newtype
);
1829 if (!types_close_enough_to_match (oldrettype
, newrettype
))
1832 /* Check that the return types are compatible but don't fail if they
1833 are not (e.g., int vs long in ILP32) and just let the caller know. */
1834 if (!comptypes (TYPE_MAIN_VARIANT (oldrettype
),
1835 TYPE_MAIN_VARIANT (newrettype
)))
1836 *strict
= oldrettype
;
1838 tree oldargs
= TYPE_ARG_TYPES (oldtype
);
1839 tree newargs
= TYPE_ARG_TYPES (newtype
);
1840 tree tryargs
= newargs
;
1842 const unsigned nlst
= ARRAY_SIZE (last_structptr_types
);
1843 const unsigned nbst
= ARRAY_SIZE (builtin_structptr_types
);
1845 gcc_checking_assert (nlst
== nbst
);
1847 for (unsigned i
= 1; oldargs
|| newargs
; ++i
)
1851 || !TREE_VALUE (oldargs
)
1852 || !TREE_VALUE (newargs
))
1855 tree oldtype
= TYPE_MAIN_VARIANT (TREE_VALUE (oldargs
));
1856 tree newtype
= TREE_VALUE (newargs
);
1857 if (newtype
== error_mark_node
)
1859 newtype
= TYPE_MAIN_VARIANT (newtype
);
1861 if (!types_close_enough_to_match (oldtype
, newtype
))
1865 if (POINTER_TYPE_P (oldtype
))
1866 /* Iterate over well-known struct types like FILE (whose types
1867 aren't known to us) and compare the pointer to each to
1868 the pointer argument. */
1869 for (j
= 0; j
< nbst
; ++j
)
1871 if (TREE_VALUE (oldargs
) != builtin_structptr_types
[j
].node
)
1873 /* Store the first FILE* etc. argument type (whatever it is), and
1874 expect any subsequent declarations of file I/O etc. built-ins
1875 to refer to it rather than to fileptr_type_node etc. which is
1876 just void* (or const void*). */
1877 if (last_structptr_types
[j
])
1879 if (!comptypes (last_structptr_types
[j
], newtype
))
1882 *strict
= last_structptr_types
[j
];
1886 last_structptr_types
[j
] = newtype
;
1890 if (j
== nbst
&& !comptypes (oldtype
, newtype
))
1892 if (POINTER_TYPE_P (oldtype
))
1894 /* For incompatible pointers, only reject differences in
1895 the unqualified variants of the referenced types but
1896 consider differences in qualifiers as benign (report
1897 those to caller via *STRICT below). */
1898 tree oldref
= TYPE_MAIN_VARIANT (TREE_TYPE (oldtype
));
1899 tree newref
= TYPE_MAIN_VARIANT (TREE_TYPE (newtype
));
1900 if (!comptypes (oldref
, newref
))
1911 oldargs
= TREE_CHAIN (oldargs
);
1912 newargs
= TREE_CHAIN (newargs
);
1915 tree trytype
= build_function_type (newrettype
, tryargs
);
1917 /* Allow declaration to change transaction_safe attribute. */
1918 tree oldattrs
= TYPE_ATTRIBUTES (oldtype
);
1919 tree oldtsafe
= lookup_attribute ("transaction_safe", oldattrs
);
1920 tree newattrs
= TYPE_ATTRIBUTES (newtype
);
1921 tree newtsafe
= lookup_attribute ("transaction_safe", newattrs
);
1922 if (oldtsafe
&& !newtsafe
)
1923 oldattrs
= remove_attribute ("transaction_safe", oldattrs
);
1924 else if (newtsafe
&& !oldtsafe
)
1925 oldattrs
= tree_cons (get_identifier ("transaction_safe"),
1926 NULL_TREE
, oldattrs
);
1928 return build_type_attribute_variant (trytype
, oldattrs
);
1931 /* Subroutine of diagnose_mismatched_decls. Check for function type
1932 mismatch involving an empty arglist vs a nonempty one and give clearer
1935 diagnose_arglist_conflict (tree newdecl
, tree olddecl
,
1936 tree newtype
, tree oldtype
)
1940 if (TREE_CODE (olddecl
) != FUNCTION_DECL
1941 || !comptypes (TREE_TYPE (oldtype
), TREE_TYPE (newtype
))
1942 || !((!prototype_p (oldtype
) && DECL_INITIAL (olddecl
) == NULL_TREE
)
1943 || (!prototype_p (newtype
) && DECL_INITIAL (newdecl
) == NULL_TREE
)))
1946 t
= TYPE_ARG_TYPES (oldtype
);
1948 t
= TYPE_ARG_TYPES (newtype
);
1949 for (; t
; t
= TREE_CHAIN (t
))
1951 tree type
= TREE_VALUE (t
);
1953 if (TREE_CHAIN (t
) == NULL_TREE
1954 && TYPE_MAIN_VARIANT (type
) != void_type_node
)
1956 inform (input_location
, "a parameter list with an ellipsis "
1957 "cannot match an empty parameter name list declaration");
1961 if (!error_operand_p (type
)
1962 && c_type_promotes_to (type
) != type
)
1964 inform (input_location
, "an argument type that has a default "
1965 "promotion cannot match an empty parameter name list "
1972 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1973 old-style function definition, NEWDECL is a prototype declaration.
1974 Diagnose inconsistencies in the argument list. Returns TRUE if
1975 the prototype is compatible, FALSE if not. */
1977 validate_proto_after_old_defn (tree newdecl
, tree newtype
, tree oldtype
)
1979 tree newargs
, oldargs
;
1982 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1984 oldargs
= TYPE_ACTUAL_ARG_TYPES (oldtype
);
1985 newargs
= TYPE_ARG_TYPES (newtype
);
1990 tree oldargtype
= TREE_VALUE (oldargs
);
1991 tree newargtype
= TREE_VALUE (newargs
);
1993 if (oldargtype
== error_mark_node
|| newargtype
== error_mark_node
)
1996 oldargtype
= (TYPE_ATOMIC (oldargtype
)
1997 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype
),
1999 : TYPE_MAIN_VARIANT (oldargtype
));
2000 newargtype
= (TYPE_ATOMIC (newargtype
)
2001 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype
),
2003 : TYPE_MAIN_VARIANT (newargtype
));
2005 if (END_OF_ARGLIST (oldargtype
) && END_OF_ARGLIST (newargtype
))
2008 /* Reaching the end of just one list means the two decls don't
2009 agree on the number of arguments. */
2010 if (END_OF_ARGLIST (oldargtype
))
2012 error ("prototype for %q+D declares more arguments "
2013 "than previous old-style definition", newdecl
);
2016 else if (END_OF_ARGLIST (newargtype
))
2018 error ("prototype for %q+D declares fewer arguments "
2019 "than previous old-style definition", newdecl
);
2023 /* Type for passing arg must be consistent with that declared
2025 else if (!comptypes (oldargtype
, newargtype
))
2027 error ("prototype for %q+D declares argument %d"
2028 " with incompatible type",
2033 oldargs
= TREE_CHAIN (oldargs
);
2034 newargs
= TREE_CHAIN (newargs
);
2038 /* If we get here, no errors were found, but do issue a warning
2039 for this poor-style construct. */
2040 warning (0, "prototype for %q+D follows non-prototype definition",
2043 #undef END_OF_ARGLIST
2046 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
2047 first in a pair of mismatched declarations, using the diagnostic
2050 locate_old_decl (tree decl
)
2052 if (TREE_CODE (decl
) == FUNCTION_DECL
2053 && fndecl_built_in_p (decl
)
2054 && !C_DECL_DECLARED_BUILTIN (decl
))
2056 else if (DECL_INITIAL (decl
))
2057 inform (input_location
,
2058 "previous definition of %q+D with type %qT",
2059 decl
, TREE_TYPE (decl
));
2060 else if (C_DECL_IMPLICIT (decl
))
2061 inform (input_location
,
2062 "previous implicit declaration of %q+D with type %qT",
2063 decl
, TREE_TYPE (decl
));
2065 inform (input_location
,
2066 "previous declaration of %q+D with type %qT",
2067 decl
, TREE_TYPE (decl
));
2071 /* Helper function. For a tagged type, it finds the declaration
2072 for a visible tag declared in the the same scope if such a
2073 declaration exists. */
2075 previous_tag (tree type
)
2077 struct c_binding
*b
= NULL
;
2078 tree name
= TYPE_NAME (type
);
2081 b
= I_TAG_BINDING (name
);
2086 if (b
&& B_IN_CURRENT_SCOPE (b
))
2092 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
2093 Returns true if the caller should proceed to merge the two, false
2094 if OLDDECL should simply be discarded. As a side effect, issues
2095 all necessary diagnostics for invalid or poor-style combinations.
2096 If it returns true, writes the types of NEWDECL and OLDDECL to
2097 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
2098 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
2101 diagnose_mismatched_decls (tree newdecl
, tree olddecl
,
2102 tree
*newtypep
, tree
*oldtypep
)
2104 tree newtype
, oldtype
;
2107 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
2108 && DECL_EXTERNAL (DECL))
2110 /* If we have error_mark_node for either decl or type, just discard
2111 the previous decl - we're in an error cascade already. */
2112 if (olddecl
== error_mark_node
|| newdecl
== error_mark_node
)
2114 *oldtypep
= oldtype
= TREE_TYPE (olddecl
);
2115 *newtypep
= newtype
= TREE_TYPE (newdecl
);
2116 if (oldtype
== error_mark_node
|| newtype
== error_mark_node
)
2119 /* Two different categories of symbol altogether. This is an error
2120 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
2121 if (TREE_CODE (olddecl
) != TREE_CODE (newdecl
))
2123 if (!(TREE_CODE (olddecl
) == FUNCTION_DECL
2124 && fndecl_built_in_p (olddecl
)
2125 && !C_DECL_DECLARED_BUILTIN (olddecl
)))
2127 auto_diagnostic_group d
;
2128 error ("%q+D redeclared as different kind of symbol", newdecl
);
2129 locate_old_decl (olddecl
);
2131 else if (TREE_PUBLIC (newdecl
))
2132 warning (OPT_Wbuiltin_declaration_mismatch
,
2133 "built-in function %q+D declared as non-function",
2136 warning (OPT_Wshadow
, "declaration of %q+D shadows "
2137 "a built-in function", newdecl
);
2141 /* Enumerators have no linkage, so may only be declared once in a
2143 if (TREE_CODE (olddecl
) == CONST_DECL
)
2146 && TYPE_NAME (DECL_CONTEXT (newdecl
))
2147 && DECL_CONTEXT (newdecl
) != DECL_CONTEXT (olddecl
)
2148 && TYPE_NAME (DECL_CONTEXT (newdecl
)) == TYPE_NAME (DECL_CONTEXT (olddecl
)))
2150 if (!simple_cst_equal (DECL_INITIAL (olddecl
), DECL_INITIAL (newdecl
)))
2152 auto_diagnostic_group d
;
2153 error ("conflicting redeclaration of enumerator %q+D", newdecl
);
2154 locate_old_decl (olddecl
);
2159 auto_diagnostic_group d
;
2160 error ("redeclaration of enumerator %q+D", newdecl
);
2161 locate_old_decl (olddecl
);
2166 bool pedwarned
= false;
2167 bool warned
= false;
2168 bool enum_and_int_p
= false;
2169 auto_diagnostic_group d
;
2171 int comptypes_result
= comptypes_check_enum_int (oldtype
, newtype
,
2173 if (!comptypes_result
)
2175 if (TREE_CODE (olddecl
) == FUNCTION_DECL
2176 && fndecl_built_in_p (olddecl
, BUILT_IN_NORMAL
)
2177 && !C_DECL_DECLARED_BUILTIN (olddecl
))
2179 /* Accept "harmless" mismatches in function types such
2180 as missing qualifiers or int vs long when they're the same
2181 size. However, diagnose return and argument types that are
2182 incompatible according to language rules. */
2183 tree mismatch_expect
;
2184 unsigned mismatch_argno
;
2186 tree trytype
= match_builtin_function_types (newtype
, oldtype
,
2190 if (trytype
&& comptypes (newtype
, trytype
))
2191 *oldtypep
= oldtype
= trytype
;
2194 /* If types don't match for a built-in, throw away the
2195 built-in. No point in calling locate_old_decl here, it
2196 won't print anything. */
2197 const char *header
= header_for_builtin_fn (olddecl
);
2198 location_t loc
= DECL_SOURCE_LOCATION (newdecl
);
2199 if (warning_at (loc
, OPT_Wbuiltin_declaration_mismatch
,
2200 "conflicting types for built-in function %q+D; "
2205 /* Suggest the right header to include as the preferred
2206 solution rather than the spelling of the declaration. */
2207 rich_location
richloc (line_table
, loc
);
2208 maybe_add_include_fixit (&richloc
, header
, true);
2210 "%qD is declared in header %qs", olddecl
, header
);
2215 if (mismatch_expect
&& extra_warnings
)
2217 location_t newloc
= DECL_SOURCE_LOCATION (newdecl
);
2218 bool warned
= false;
2220 warned
= warning_at (newloc
, OPT_Wbuiltin_declaration_mismatch
,
2221 "mismatch in argument %u type of built-in "
2222 "function %qD; expected %qT",
2223 mismatch_argno
, newdecl
, mismatch_expect
);
2225 warned
= warning_at (newloc
, OPT_Wbuiltin_declaration_mismatch
,
2226 "mismatch in return type of built-in "
2227 "function %qD; expected %qT",
2228 newdecl
, mismatch_expect
);
2229 const char *header
= header_for_builtin_fn (olddecl
);
2230 if (warned
&& header
)
2232 rich_location
richloc (line_table
, newloc
);
2233 maybe_add_include_fixit (&richloc
, header
, true);
2235 "%qD is declared in header %qs", olddecl
, header
);
2239 else if (TREE_CODE (olddecl
) == FUNCTION_DECL
2240 && DECL_IS_UNDECLARED_BUILTIN (olddecl
))
2242 /* A conflicting function declaration for a predeclared
2243 function that isn't actually built in. Objective C uses
2244 these. The new declaration silently overrides everything
2245 but the volatility (i.e. noreturn) indication. See also
2246 below. FIXME: Make Objective C use normal builtins. */
2247 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
2250 /* Permit void foo (...) to match int foo (...) if the latter is
2251 the definition and implicit int was used. See
2252 c-torture/compile/920625-2.c. */
2253 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
&& DECL_INITIAL (newdecl
)
2254 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype
)) == void_type_node
2255 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype
)) == integer_type_node
2256 && C_FUNCTION_IMPLICIT_INT (newdecl
) && !DECL_INITIAL (olddecl
))
2258 pedwarned
= pedwarn (input_location
, 0,
2259 "conflicting types for %q+D", newdecl
);
2260 /* Make sure we keep void as the return type. */
2261 TREE_TYPE (newdecl
) = *newtypep
= newtype
= oldtype
;
2262 C_FUNCTION_IMPLICIT_INT (newdecl
) = 0;
2264 /* Permit void foo (...) to match an earlier call to foo (...) with
2265 no declared type (thus, implicitly int). */
2266 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
2267 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype
)) == void_type_node
2268 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype
)) == integer_type_node
2269 && C_DECL_IMPLICIT (olddecl
) && !DECL_INITIAL (olddecl
))
2271 pedwarned
= pedwarn (input_location
, 0,
2272 "conflicting types for %q+D; have %qT",
2274 /* Make sure we keep void as the return type. */
2275 TREE_TYPE (olddecl
) = *oldtypep
= oldtype
= newtype
;
2279 int new_quals
= TYPE_QUALS (newtype
);
2280 int old_quals
= TYPE_QUALS (oldtype
);
2282 if (new_quals
!= old_quals
)
2284 addr_space_t new_addr
= DECODE_QUAL_ADDR_SPACE (new_quals
);
2285 addr_space_t old_addr
= DECODE_QUAL_ADDR_SPACE (old_quals
);
2286 if (new_addr
!= old_addr
)
2288 if (ADDR_SPACE_GENERIC_P (new_addr
))
2289 error ("conflicting named address spaces (generic vs %s) "
2291 c_addr_space_name (old_addr
), newdecl
);
2292 else if (ADDR_SPACE_GENERIC_P (old_addr
))
2293 error ("conflicting named address spaces (%s vs generic) "
2295 c_addr_space_name (new_addr
), newdecl
);
2297 error ("conflicting named address spaces (%s vs %s) "
2299 c_addr_space_name (new_addr
),
2300 c_addr_space_name (old_addr
),
2304 if (CLEAR_QUAL_ADDR_SPACE (new_quals
)
2305 != CLEAR_QUAL_ADDR_SPACE (old_quals
))
2306 error ("conflicting type qualifiers for %q+D", newdecl
);
2309 error ("conflicting types for %q+D; have %qT", newdecl
, newtype
);
2310 diagnose_arglist_conflict (newdecl
, olddecl
, newtype
, oldtype
);
2311 locate_old_decl (olddecl
);
2315 /* Warn about enum/integer type mismatches. They are compatible types
2316 (C23 6.7.2.2/5), but may pose portability problems. */
2317 else if (enum_and_int_p
2318 && TREE_CODE (newdecl
) != TYPE_DECL
2319 /* Don't warn about about acc_on_device built-in redeclaration,
2320 the built-in is declared with int rather than enum because
2321 the enum isn't intrinsic. */
2322 && !(TREE_CODE (olddecl
) == FUNCTION_DECL
2323 && fndecl_built_in_p (olddecl
, BUILT_IN_ACC_ON_DEVICE
)
2324 && !C_DECL_DECLARED_BUILTIN (olddecl
)))
2325 warned
= warning_at (DECL_SOURCE_LOCATION (newdecl
),
2326 OPT_Wenum_int_mismatch
,
2327 "conflicting types for %q+D due to enum/integer "
2328 "mismatch; have %qT", newdecl
, newtype
);
2330 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2331 but silently ignore the redeclaration if either is in a system
2332 header. (Conflicting redeclarations were handled above.) This
2333 is allowed for C11 if the types are the same, not just
2335 if (TREE_CODE (newdecl
) == TYPE_DECL
)
2337 bool types_different
= false;
2340 = comptypes_check_different_types (oldtype
, newtype
, &types_different
);
2342 if (comptypes_result
!= 1 || types_different
)
2344 error ("redefinition of typedef %q+D with different type", newdecl
);
2345 locate_old_decl (olddecl
);
2349 if (DECL_IN_SYSTEM_HEADER (newdecl
)
2350 || DECL_IN_SYSTEM_HEADER (olddecl
)
2351 || warning_suppressed_p (newdecl
, OPT_Wpedantic
)
2352 || warning_suppressed_p (olddecl
, OPT_Wpedantic
))
2353 return true; /* Allow OLDDECL to continue in use. */
2355 if (c_type_variably_modified_p (newtype
))
2357 error ("redefinition of typedef %q+D with variably modified type",
2359 locate_old_decl (olddecl
);
2361 else if (pedwarn_c99 (input_location
, OPT_Wpedantic
,
2362 "redefinition of typedef %q+D", newdecl
))
2363 locate_old_decl (olddecl
);
2368 /* Function declarations can either be 'static' or 'extern' (no
2369 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2370 can never conflict with each other on account of linkage
2371 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2372 gnu89 mode permits two definitions if one is 'extern inline' and
2373 one is not. The non- extern-inline definition supersedes the
2374 extern-inline definition. */
2376 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2378 /* If you declare a built-in function name as static, or
2379 define the built-in with an old-style definition (so we
2380 can't validate the argument list) the built-in definition is
2381 overridden, but optionally warn this was a bad choice of name. */
2382 if (fndecl_built_in_p (olddecl
)
2383 && !C_DECL_DECLARED_BUILTIN (olddecl
))
2385 if (!TREE_PUBLIC (newdecl
)
2386 || (DECL_INITIAL (newdecl
)
2387 && !prototype_p (TREE_TYPE (newdecl
))))
2389 warning_at (DECL_SOURCE_LOCATION (newdecl
),
2390 OPT_Wshadow
, "declaration of %qD shadows "
2391 "a built-in function", newdecl
);
2392 /* Discard the old built-in function. */
2396 if (!prototype_p (TREE_TYPE (newdecl
)))
2398 /* Set for built-ins that take no arguments. */
2399 bool func_void_args
= false;
2400 if (tree at
= TYPE_ARG_TYPES (oldtype
))
2401 func_void_args
= VOID_TYPE_P (TREE_VALUE (at
));
2403 if (extra_warnings
&& !func_void_args
)
2404 warning_at (DECL_SOURCE_LOCATION (newdecl
),
2405 OPT_Wbuiltin_declaration_mismatch
,
2406 "declaration of built-in function %qD without "
2407 "a prototype; expected %qT",
2408 newdecl
, TREE_TYPE (olddecl
));
2412 if (DECL_INITIAL (newdecl
))
2414 if (DECL_INITIAL (olddecl
))
2416 /* If the new declaration isn't overriding an extern inline
2417 reject the new decl. In c99, no overriding is allowed
2418 in the same translation unit. */
2419 if (!DECL_EXTERN_INLINE (olddecl
)
2420 || DECL_EXTERN_INLINE (newdecl
)
2421 || (!flag_gnu89_inline
2422 && (!DECL_DECLARED_INLINE_P (olddecl
)
2423 || !lookup_attribute ("gnu_inline",
2424 DECL_ATTRIBUTES (olddecl
)))
2425 && (!DECL_DECLARED_INLINE_P (newdecl
)
2426 || !lookup_attribute ("gnu_inline",
2427 DECL_ATTRIBUTES (newdecl
)))))
2429 auto_diagnostic_group d
;
2430 error ("redefinition of %q+D", newdecl
);
2431 locate_old_decl (olddecl
);
2436 /* If we have a prototype after an old-style function definition,
2437 the argument types must be checked specially. */
2438 else if (DECL_INITIAL (olddecl
)
2439 && !prototype_p (oldtype
) && prototype_p (newtype
)
2440 && TYPE_ACTUAL_ARG_TYPES (oldtype
))
2442 auto_diagnostic_group d
;
2443 if (!validate_proto_after_old_defn (newdecl
, newtype
, oldtype
))
2445 locate_old_decl (olddecl
);
2449 /* A non-static declaration (even an "extern") followed by a
2450 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2451 The same is true for a static forward declaration at block
2452 scope followed by a non-static declaration/definition at file
2453 scope. Static followed by non-static at the same scope is
2454 not undefined behavior, and is the most convenient way to get
2455 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2456 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2457 we do diagnose it if -Wtraditional. */
2458 if (TREE_PUBLIC (olddecl
) && !TREE_PUBLIC (newdecl
))
2460 /* Two exceptions to the rule. If olddecl is an extern
2461 inline, or a predeclared function that isn't actually
2462 built in, newdecl silently overrides olddecl. The latter
2463 occur only in Objective C; see also above. (FIXME: Make
2464 Objective C use normal builtins.) */
2465 if (!DECL_IS_UNDECLARED_BUILTIN (olddecl
)
2466 && !DECL_EXTERN_INLINE (olddecl
))
2468 auto_diagnostic_group d
;
2469 error ("static declaration of %q+D follows "
2470 "non-static declaration", newdecl
);
2471 locate_old_decl (olddecl
);
2475 else if (TREE_PUBLIC (newdecl
) && !TREE_PUBLIC (olddecl
))
2477 if (DECL_CONTEXT (olddecl
))
2479 auto_diagnostic_group d
;
2480 error ("non-static declaration of %q+D follows "
2481 "static declaration", newdecl
);
2482 locate_old_decl (olddecl
);
2485 else if (warn_traditional
)
2487 warned
|= warning (OPT_Wtraditional
,
2488 "non-static declaration of %q+D "
2489 "follows static declaration", newdecl
);
2493 /* Make sure gnu_inline attribute is either not present, or
2494 present on all inline decls. */
2495 if (DECL_DECLARED_INLINE_P (olddecl
)
2496 && DECL_DECLARED_INLINE_P (newdecl
))
2498 bool newa
= lookup_attribute ("gnu_inline",
2499 DECL_ATTRIBUTES (newdecl
)) != NULL
;
2500 bool olda
= lookup_attribute ("gnu_inline",
2501 DECL_ATTRIBUTES (olddecl
)) != NULL
;
2504 auto_diagnostic_group d
;
2505 error_at (input_location
, "%<gnu_inline%> attribute present on %q+D",
2506 newa
? newdecl
: olddecl
);
2507 error_at (DECL_SOURCE_LOCATION (newa
? olddecl
: newdecl
),
2512 else if (VAR_P (newdecl
))
2514 /* Only variables can be thread-local, and all declarations must
2515 agree on this property. */
2516 if (C_DECL_THREADPRIVATE_P (olddecl
) && !DECL_THREAD_LOCAL_P (newdecl
))
2518 /* Nothing to check. Since OLDDECL is marked threadprivate
2519 and NEWDECL does not have a thread-local attribute, we
2520 will merge the threadprivate attribute into NEWDECL. */
2523 else if (DECL_THREAD_LOCAL_P (newdecl
) != DECL_THREAD_LOCAL_P (olddecl
))
2525 auto_diagnostic_group d
;
2526 if (DECL_THREAD_LOCAL_P (newdecl
))
2527 error ("thread-local declaration of %q+D follows "
2528 "non-thread-local declaration", newdecl
);
2530 error ("non-thread-local declaration of %q+D follows "
2531 "thread-local declaration", newdecl
);
2533 locate_old_decl (olddecl
);
2537 /* Multiple initialized definitions are not allowed (6.9p3,5).
2538 For this purpose, C23 makes it clear that thread-local
2539 declarations without extern are definitions, not tentative
2540 definitions, whether or not they have initializers. The
2541 wording before C23 was unclear; literally it would have made
2542 uninitialized thread-local declarations into tentative
2543 definitions only if they also used static, but without saying
2544 explicitly whether or not other cases count as
2545 definitions at all. */
2546 if ((DECL_INITIAL (newdecl
) && DECL_INITIAL (olddecl
))
2548 && DECL_THREAD_LOCAL_P (newdecl
)
2549 && !DECL_EXTERNAL (newdecl
)
2550 && !DECL_EXTERNAL (olddecl
)))
2552 auto_diagnostic_group d
;
2553 error ("redefinition of %q+D", newdecl
);
2554 locate_old_decl (olddecl
);
2558 /* Objects declared at file scope: if the first declaration had
2559 external linkage (even if it was an external reference) the
2560 second must have external linkage as well, or the behavior is
2561 undefined. If the first declaration had internal linkage, then
2562 the second must too, or else be an external reference (in which
2563 case the composite declaration still has internal linkage).
2564 As for function declarations, we warn about the static-then-
2565 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2566 if (DECL_FILE_SCOPE_P (newdecl
)
2567 && TREE_PUBLIC (newdecl
) != TREE_PUBLIC (olddecl
))
2569 if (DECL_EXTERNAL (newdecl
))
2571 if (!DECL_FILE_SCOPE_P (olddecl
))
2573 auto_diagnostic_group d
;
2574 error ("extern declaration of %q+D follows "
2575 "declaration with no linkage", newdecl
);
2576 locate_old_decl (olddecl
);
2579 else if (warn_traditional
)
2581 warned
|= warning (OPT_Wtraditional
,
2582 "non-static declaration of %q+D "
2583 "follows static declaration", newdecl
);
2588 auto_diagnostic_group d
;
2589 if (TREE_PUBLIC (newdecl
))
2590 error ("non-static declaration of %q+D follows "
2591 "static declaration", newdecl
);
2593 error ("static declaration of %q+D follows "
2594 "non-static declaration", newdecl
);
2596 locate_old_decl (olddecl
);
2600 /* Two objects with the same name declared at the same block
2601 scope must both be external references (6.7p3). */
2602 else if (!DECL_FILE_SCOPE_P (newdecl
))
2604 if (DECL_EXTERNAL (newdecl
))
2606 /* Extern with initializer at block scope, which will
2607 already have received an error. */
2609 else if (DECL_EXTERNAL (olddecl
))
2611 auto_diagnostic_group d
;
2612 error ("declaration of %q+D with no linkage follows "
2613 "extern declaration", newdecl
);
2614 locate_old_decl (olddecl
);
2618 auto_diagnostic_group d
;
2619 error ("redeclaration of %q+D with no linkage", newdecl
);
2620 locate_old_decl (olddecl
);
2626 /* C++ does not permit a decl to appear multiple times at file
2629 && DECL_FILE_SCOPE_P (newdecl
)
2630 && !DECL_EXTERNAL (newdecl
)
2631 && !DECL_EXTERNAL (olddecl
))
2632 warned
|= warning_at (DECL_SOURCE_LOCATION (newdecl
),
2634 ("duplicate declaration of %qD is "
2640 /* All decls must agree on a visibility. */
2641 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl
), TS_DECL_WITH_VIS
)
2642 && DECL_VISIBILITY_SPECIFIED (newdecl
) && DECL_VISIBILITY_SPECIFIED (olddecl
)
2643 && DECL_VISIBILITY (newdecl
) != DECL_VISIBILITY (olddecl
))
2645 warned
|= warning (0, "redeclaration of %q+D with different visibility "
2646 "(old visibility preserved)", newdecl
);
2649 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2650 warned
|= diagnose_mismatched_attributes (olddecl
, newdecl
);
2651 else /* PARM_DECL, VAR_DECL */
2653 /* Redeclaration of a parameter is a constraint violation (this is
2654 not explicitly stated, but follows from C99 6.7p3 [no more than
2655 one declaration of the same identifier with no linkage in the
2656 same scope, except type tags] and 6.2.2p6 [parameters have no
2657 linkage]). We must check for a forward parameter declaration,
2658 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2659 an extension, the mandatory diagnostic for which is handled by
2660 mark_forward_parm_decls. */
2662 if (TREE_CODE (newdecl
) == PARM_DECL
2663 && (!TREE_ASM_WRITTEN (olddecl
) || TREE_ASM_WRITTEN (newdecl
)))
2665 auto_diagnostic_group d
;
2666 error ("redefinition of parameter %q+D", newdecl
);
2667 locate_old_decl (olddecl
);
2672 /* Optional warning for completely redundant decls. */
2673 if (!warned
&& !pedwarned
2674 && warn_redundant_decls
2675 /* Don't warn about a function declaration followed by a
2677 && !(TREE_CODE (newdecl
) == FUNCTION_DECL
2678 && DECL_INITIAL (newdecl
) && !DECL_INITIAL (olddecl
))
2679 /* Don't warn about redundant redeclarations of builtins. */
2680 && !(TREE_CODE (newdecl
) == FUNCTION_DECL
2681 && !fndecl_built_in_p (newdecl
)
2682 && fndecl_built_in_p (olddecl
)
2683 && !C_DECL_DECLARED_BUILTIN (olddecl
))
2684 /* Don't warn about an extern followed by a definition. */
2685 && !(DECL_EXTERNAL (olddecl
) && !DECL_EXTERNAL (newdecl
))
2686 /* Don't warn about forward parameter decls. */
2687 && !(TREE_CODE (newdecl
) == PARM_DECL
2688 && TREE_ASM_WRITTEN (olddecl
) && !TREE_ASM_WRITTEN (newdecl
))
2689 /* Don't warn about a variable definition following a declaration. */
2690 && !(VAR_P (newdecl
)
2691 && DECL_INITIAL (newdecl
) && !DECL_INITIAL (olddecl
)))
2693 warned
= warning (OPT_Wredundant_decls
, "redundant redeclaration of %q+D",
2697 /* Report location of previous decl/defn. */
2698 if (warned
|| pedwarned
)
2699 locate_old_decl (olddecl
);
2701 #undef DECL_EXTERN_INLINE
2706 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2707 consistent with OLDDECL, but carries new information. Merge the
2708 new information into OLDDECL. This function issues no
2712 merge_decls (tree newdecl
, tree olddecl
, tree newtype
, tree oldtype
)
2714 bool new_is_definition
= (TREE_CODE (newdecl
) == FUNCTION_DECL
2715 && DECL_INITIAL (newdecl
) != NULL_TREE
);
2716 bool new_is_prototype
= (TREE_CODE (newdecl
) == FUNCTION_DECL
2717 && prototype_p (TREE_TYPE (newdecl
)));
2718 bool old_is_prototype
= (TREE_CODE (olddecl
) == FUNCTION_DECL
2719 && prototype_p (TREE_TYPE (olddecl
)));
2721 /* For real parm decl following a forward decl, rechain the old decl
2722 in its new location and clear TREE_ASM_WRITTEN (it's not a
2723 forward decl anymore). */
2724 if (TREE_CODE (newdecl
) == PARM_DECL
2725 && TREE_ASM_WRITTEN (olddecl
) && !TREE_ASM_WRITTEN (newdecl
))
2727 struct c_binding
*b
, **here
;
2729 for (here
= ¤t_scope
->bindings
; *here
; here
= &(*here
)->prev
)
2730 if ((*here
)->decl
== olddecl
)
2737 b
->prev
= current_scope
->bindings
;
2738 current_scope
->bindings
= b
;
2740 TREE_ASM_WRITTEN (olddecl
) = 0;
2743 DECL_ATTRIBUTES (newdecl
)
2744 = targetm
.merge_decl_attributes (olddecl
, newdecl
);
2746 /* For typedefs use the old type, as the new type's DECL_NAME points
2747 at newdecl, which will be ggc_freed. */
2748 if (TREE_CODE (newdecl
) == TYPE_DECL
)
2750 /* But NEWTYPE might have an attribute, honor that. */
2754 if (TYPE_USER_ALIGN (tem
))
2756 if (TYPE_ALIGN (tem
) > TYPE_ALIGN (newtype
))
2757 SET_TYPE_ALIGN (newtype
, TYPE_ALIGN (tem
));
2758 TYPE_USER_ALIGN (newtype
) = true;
2761 /* And remove the new type from the variants list. */
2762 if (TYPE_NAME (TREE_TYPE (newdecl
)) == newdecl
)
2764 tree remove
= TREE_TYPE (newdecl
);
2765 if (TYPE_MAIN_VARIANT (remove
) == remove
)
2767 gcc_assert (TYPE_NEXT_VARIANT (remove
) == NULL_TREE
);
2768 /* If remove is the main variant, no need to remove that
2769 from the list. One of the DECL_ORIGINAL_TYPE
2770 variants, e.g. created for aligned attribute, might still
2771 refer to the newdecl TYPE_DECL though, so remove that one
2773 if (DECL_ORIGINAL_TYPE (newdecl
)
2774 && DECL_ORIGINAL_TYPE (newdecl
) != remove
)
2775 for (tree t
= TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl
));
2776 t
; t
= TYPE_MAIN_VARIANT (t
))
2777 if (TYPE_NAME (TYPE_NEXT_VARIANT (t
)) == newdecl
)
2779 TYPE_NEXT_VARIANT (t
)
2780 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t
));
2785 for (tree t
= TYPE_MAIN_VARIANT (remove
); ;
2786 t
= TYPE_NEXT_VARIANT (t
))
2787 if (TYPE_NEXT_VARIANT (t
) == remove
)
2789 TYPE_NEXT_VARIANT (t
) = TYPE_NEXT_VARIANT (remove
);
2795 /* Merge the data types specified in the two decls. */
2797 = TREE_TYPE (olddecl
)
2798 = composite_type (newtype
, oldtype
);
2800 /* Lay the type out, unless already done. */
2801 if (!comptypes (oldtype
, TREE_TYPE (newdecl
)))
2803 if (TREE_TYPE (newdecl
) != error_mark_node
)
2804 layout_type (TREE_TYPE (newdecl
));
2805 if (TREE_CODE (newdecl
) != FUNCTION_DECL
2806 && TREE_CODE (newdecl
) != TYPE_DECL
2807 && TREE_CODE (newdecl
) != CONST_DECL
)
2808 layout_decl (newdecl
, 0);
2812 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2813 DECL_SIZE (newdecl
) = DECL_SIZE (olddecl
);
2814 DECL_SIZE_UNIT (newdecl
) = DECL_SIZE_UNIT (olddecl
);
2815 SET_DECL_MODE (newdecl
, DECL_MODE (olddecl
));
2816 if (DECL_ALIGN (olddecl
) > DECL_ALIGN (newdecl
))
2818 SET_DECL_ALIGN (newdecl
, DECL_ALIGN (olddecl
));
2819 DECL_USER_ALIGN (newdecl
) |= DECL_USER_ALIGN (olddecl
);
2821 else if (DECL_ALIGN (olddecl
) == DECL_ALIGN (newdecl
)
2822 && DECL_USER_ALIGN (olddecl
) != DECL_USER_ALIGN (newdecl
))
2823 DECL_USER_ALIGN (newdecl
) = 1;
2824 if (DECL_WARN_IF_NOT_ALIGN (olddecl
)
2825 > DECL_WARN_IF_NOT_ALIGN (newdecl
))
2826 SET_DECL_WARN_IF_NOT_ALIGN (newdecl
,
2827 DECL_WARN_IF_NOT_ALIGN (olddecl
));
2830 /* Keep the old rtl since we can safely use it. */
2831 if (HAS_RTL_P (olddecl
))
2832 COPY_DECL_RTL (olddecl
, newdecl
);
2834 /* Merge the type qualifiers. */
2835 if (TREE_READONLY (newdecl
))
2836 TREE_READONLY (olddecl
) = 1;
2838 if (TREE_THIS_VOLATILE (newdecl
))
2839 TREE_THIS_VOLATILE (olddecl
) = 1;
2841 /* Merge deprecatedness. */
2842 if (TREE_DEPRECATED (newdecl
))
2843 TREE_DEPRECATED (olddecl
) = 1;
2845 /* Merge unavailability. */
2846 if (TREE_UNAVAILABLE (newdecl
))
2847 TREE_UNAVAILABLE (olddecl
) = 1;
2849 /* If a decl is in a system header and the other isn't, keep the one on the
2850 system header. Otherwise, keep source location of definition rather than
2851 declaration and of prototype rather than non-prototype unless that
2852 prototype is built-in. */
2853 if (HAS_DECL_ASSEMBLER_NAME_P (olddecl
)
2854 && DECL_IN_SYSTEM_HEADER (olddecl
)
2855 && !DECL_IN_SYSTEM_HEADER (newdecl
) )
2856 DECL_SOURCE_LOCATION (newdecl
) = DECL_SOURCE_LOCATION (olddecl
);
2857 else if (HAS_DECL_ASSEMBLER_NAME_P (olddecl
)
2858 && DECL_IN_SYSTEM_HEADER (newdecl
)
2859 && !DECL_IN_SYSTEM_HEADER (olddecl
))
2860 DECL_SOURCE_LOCATION (olddecl
) = DECL_SOURCE_LOCATION (newdecl
);
2861 else if ((DECL_INITIAL (newdecl
) == NULL_TREE
2862 && DECL_INITIAL (olddecl
) != NULL_TREE
)
2863 || (old_is_prototype
&& !new_is_prototype
2864 && !C_DECL_BUILTIN_PROTOTYPE (olddecl
)))
2865 DECL_SOURCE_LOCATION (newdecl
) = DECL_SOURCE_LOCATION (olddecl
);
2867 /* Merge the initialization information. */
2868 if (DECL_INITIAL (newdecl
) == NULL_TREE
)
2869 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
2871 /* Merge 'constexpr' information. */
2872 if (VAR_P (olddecl
) && VAR_P (newdecl
))
2874 if (C_DECL_DECLARED_CONSTEXPR (olddecl
))
2875 C_DECL_DECLARED_CONSTEXPR (newdecl
) = 1;
2876 else if (C_DECL_DECLARED_CONSTEXPR (newdecl
))
2877 C_DECL_DECLARED_CONSTEXPR (olddecl
) = 1;
2880 /* Merge the threadprivate attribute. */
2881 if (VAR_P (olddecl
) && C_DECL_THREADPRIVATE_P (olddecl
))
2882 C_DECL_THREADPRIVATE_P (newdecl
) = 1;
2884 if (HAS_DECL_ASSEMBLER_NAME_P (olddecl
))
2886 /* Copy the assembler name.
2887 Currently, it can only be defined in the prototype. */
2888 COPY_DECL_ASSEMBLER_NAME (olddecl
, newdecl
);
2890 /* Use visibility of whichever declaration had it specified */
2891 if (DECL_VISIBILITY_SPECIFIED (olddecl
))
2893 DECL_VISIBILITY (newdecl
) = DECL_VISIBILITY (olddecl
);
2894 DECL_VISIBILITY_SPECIFIED (newdecl
) = 1;
2897 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2899 DECL_STATIC_CONSTRUCTOR(newdecl
) |= DECL_STATIC_CONSTRUCTOR(olddecl
);
2900 DECL_STATIC_DESTRUCTOR (newdecl
) |= DECL_STATIC_DESTRUCTOR (olddecl
);
2901 DECL_NO_LIMIT_STACK (newdecl
) |= DECL_NO_LIMIT_STACK (olddecl
);
2902 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl
)
2903 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl
);
2904 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
2905 DECL_IS_MALLOC (newdecl
) |= DECL_IS_MALLOC (olddecl
);
2906 if (DECL_IS_OPERATOR_NEW_P (olddecl
))
2907 DECL_SET_IS_OPERATOR_NEW (newdecl
, true);
2908 if (DECL_IS_OPERATOR_DELETE_P (olddecl
))
2909 DECL_SET_IS_OPERATOR_DELETE (newdecl
, true);
2910 TREE_READONLY (newdecl
) |= TREE_READONLY (olddecl
);
2911 DECL_PURE_P (newdecl
) |= DECL_PURE_P (olddecl
);
2912 DECL_IS_NOVOPS (newdecl
) |= DECL_IS_NOVOPS (olddecl
);
2915 /* Merge the storage class information. */
2916 merge_weak (newdecl
, olddecl
);
2918 /* For functions, static overrides non-static. */
2919 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2921 TREE_PUBLIC (newdecl
) &= TREE_PUBLIC (olddecl
);
2922 /* This is since we don't automatically
2923 copy the attributes of NEWDECL into OLDDECL. */
2924 TREE_PUBLIC (olddecl
) = TREE_PUBLIC (newdecl
);
2925 /* If this clears `static', clear it in the identifier too. */
2926 if (!TREE_PUBLIC (olddecl
))
2927 TREE_PUBLIC (DECL_NAME (olddecl
)) = 0;
2931 /* In c99, 'extern' declaration before (or after) 'inline' means this
2932 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2934 if (TREE_CODE (newdecl
) == FUNCTION_DECL
2935 && !flag_gnu89_inline
2936 && (DECL_DECLARED_INLINE_P (newdecl
)
2937 || DECL_DECLARED_INLINE_P (olddecl
))
2938 && (!DECL_DECLARED_INLINE_P (newdecl
)
2939 || !DECL_DECLARED_INLINE_P (olddecl
)
2940 || !DECL_EXTERNAL (olddecl
))
2941 && DECL_EXTERNAL (newdecl
)
2942 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl
))
2943 && !current_function_decl
)
2944 DECL_EXTERNAL (newdecl
) = 0;
2946 /* An inline definition following a static declaration is not
2948 if (new_is_definition
2949 && (DECL_DECLARED_INLINE_P (newdecl
)
2950 || DECL_DECLARED_INLINE_P (olddecl
))
2951 && !TREE_PUBLIC (olddecl
))
2952 DECL_EXTERNAL (newdecl
) = 0;
2954 if (DECL_EXTERNAL (newdecl
))
2956 TREE_STATIC (newdecl
) = TREE_STATIC (olddecl
);
2957 DECL_EXTERNAL (newdecl
) = DECL_EXTERNAL (olddecl
);
2959 /* An extern decl does not override previous storage class. */
2960 TREE_PUBLIC (newdecl
) = TREE_PUBLIC (olddecl
);
2961 if (!DECL_EXTERNAL (newdecl
))
2963 DECL_CONTEXT (newdecl
) = DECL_CONTEXT (olddecl
);
2964 DECL_COMMON (newdecl
) = DECL_COMMON (olddecl
);
2969 TREE_STATIC (olddecl
) = TREE_STATIC (newdecl
);
2970 TREE_PUBLIC (olddecl
) = TREE_PUBLIC (newdecl
);
2973 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2975 /* If we're redefining a function previously defined as extern
2976 inline, make sure we emit debug info for the inline before we
2977 throw it away, in case it was inlined into a function that
2978 hasn't been written out yet. */
2979 if (new_is_definition
&& DECL_INITIAL (olddecl
))
2980 /* The new defn must not be inline. */
2981 DECL_UNINLINABLE (newdecl
) = 1;
2984 /* If either decl says `inline', this fn is inline, unless
2985 its definition was passed already. */
2986 if (DECL_DECLARED_INLINE_P (newdecl
)
2987 || DECL_DECLARED_INLINE_P (olddecl
))
2988 DECL_DECLARED_INLINE_P (newdecl
) = 1;
2990 DECL_UNINLINABLE (newdecl
) = DECL_UNINLINABLE (olddecl
)
2991 = (DECL_UNINLINABLE (newdecl
) || DECL_UNINLINABLE (olddecl
));
2993 DECL_DISREGARD_INLINE_LIMITS (newdecl
)
2994 = DECL_DISREGARD_INLINE_LIMITS (olddecl
)
2995 = (DECL_DISREGARD_INLINE_LIMITS (newdecl
)
2996 || DECL_DISREGARD_INLINE_LIMITS (olddecl
));
2999 if (fndecl_built_in_p (olddecl
))
3001 /* If redeclaring a builtin function, it stays built in.
3002 But it gets tagged as having been declared. */
3003 copy_decl_built_in_function (newdecl
, olddecl
);
3004 C_DECL_DECLARED_BUILTIN (newdecl
) = 1;
3005 if (new_is_prototype
)
3007 C_DECL_BUILTIN_PROTOTYPE (newdecl
) = 0;
3008 if (DECL_BUILT_IN_CLASS (newdecl
) == BUILT_IN_NORMAL
)
3010 enum built_in_function fncode
= DECL_FUNCTION_CODE (newdecl
);
3013 /* If a compatible prototype of these builtin functions
3014 is seen, assume the runtime implements it with the
3015 expected semantics. */
3016 case BUILT_IN_STPCPY
:
3017 if (builtin_decl_explicit_p (fncode
))
3018 set_builtin_decl_implicit_p (fncode
, true);
3021 if (builtin_decl_explicit_p (fncode
))
3022 set_builtin_decl_declared_p (fncode
, true);
3026 copy_attributes_to_builtin (newdecl
);
3030 C_DECL_BUILTIN_PROTOTYPE (newdecl
)
3031 = C_DECL_BUILTIN_PROTOTYPE (olddecl
);
3034 /* Preserve function specific target and optimization options */
3035 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl
)
3036 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl
))
3037 DECL_FUNCTION_SPECIFIC_TARGET (newdecl
)
3038 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl
);
3040 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
)
3041 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
))
3042 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
)
3043 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
);
3045 /* Also preserve various other info from the definition. */
3046 if (!new_is_definition
)
3049 DECL_RESULT (newdecl
) = DECL_RESULT (olddecl
);
3050 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
3051 DECL_STRUCT_FUNCTION (newdecl
) = DECL_STRUCT_FUNCTION (olddecl
);
3052 DECL_SAVED_TREE (newdecl
) = DECL_SAVED_TREE (olddecl
);
3053 DECL_ARGUMENTS (newdecl
) = copy_list (DECL_ARGUMENTS (olddecl
));
3054 for (t
= DECL_ARGUMENTS (newdecl
); t
; t
= DECL_CHAIN (t
))
3055 DECL_CONTEXT (t
) = newdecl
;
3057 /* See if we've got a function to instantiate from. */
3058 if (DECL_SAVED_TREE (olddecl
))
3059 DECL_ABSTRACT_ORIGIN (newdecl
)
3060 = DECL_ABSTRACT_ORIGIN (olddecl
);
3064 /* Merge the USED information. */
3065 if (TREE_USED (olddecl
))
3066 TREE_USED (newdecl
) = 1;
3067 else if (TREE_USED (newdecl
))
3068 TREE_USED (olddecl
) = 1;
3069 if (VAR_P (olddecl
) || TREE_CODE (olddecl
) == PARM_DECL
)
3070 DECL_READ_P (newdecl
) |= DECL_READ_P (olddecl
);
3071 if (DECL_PRESERVE_P (olddecl
))
3072 DECL_PRESERVE_P (newdecl
) = 1;
3073 else if (DECL_PRESERVE_P (newdecl
))
3074 DECL_PRESERVE_P (olddecl
) = 1;
3076 /* Merge DECL_COMMON */
3077 if (VAR_P (olddecl
) && VAR_P (newdecl
)
3078 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl
))
3079 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl
)))
3080 DECL_COMMON (newdecl
) = DECL_COMMON (newdecl
) && DECL_COMMON (olddecl
);
3082 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
3083 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
3084 DECL_ARGUMENTS (if appropriate). */
3086 unsigned olddecl_uid
= DECL_UID (olddecl
);
3087 tree olddecl_context
= DECL_CONTEXT (olddecl
);
3088 tree olddecl_arguments
= NULL
;
3089 if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
3090 olddecl_arguments
= DECL_ARGUMENTS (olddecl
);
3092 memcpy ((char *) olddecl
+ sizeof (struct tree_common
),
3093 (char *) newdecl
+ sizeof (struct tree_common
),
3094 sizeof (struct tree_decl_common
) - sizeof (struct tree_common
));
3095 DECL_USER_ALIGN (olddecl
) = DECL_USER_ALIGN (newdecl
);
3096 switch (TREE_CODE (olddecl
))
3101 struct symtab_node
*snode
= olddecl
->decl_with_vis
.symtab_node
;
3103 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
3104 (char *) newdecl
+ sizeof (struct tree_decl_common
),
3105 tree_code_size (TREE_CODE (olddecl
)) - sizeof (struct tree_decl_common
));
3106 olddecl
->decl_with_vis
.symtab_node
= snode
;
3108 if ((DECL_EXTERNAL (olddecl
)
3109 || TREE_PUBLIC (olddecl
)
3110 || TREE_STATIC (olddecl
))
3111 && DECL_SECTION_NAME (newdecl
) != NULL
)
3112 set_decl_section_name (olddecl
, newdecl
);
3114 /* This isn't quite correct for something like
3115 int __thread x attribute ((tls_model ("local-exec")));
3116 extern int __thread x;
3117 as we'll lose the "local-exec" model. */
3118 if (VAR_P (olddecl
) && DECL_THREAD_LOCAL_P (newdecl
))
3119 set_decl_tls_model (olddecl
, DECL_TLS_MODEL (newdecl
));
3129 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
3130 (char *) newdecl
+ sizeof (struct tree_decl_common
),
3131 tree_code_size (TREE_CODE (olddecl
)) - sizeof (struct tree_decl_common
));
3136 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
3137 (char *) newdecl
+ sizeof (struct tree_decl_common
),
3138 sizeof (struct tree_decl_non_common
) - sizeof (struct tree_decl_common
));
3140 DECL_UID (olddecl
) = olddecl_uid
;
3141 DECL_CONTEXT (olddecl
) = olddecl_context
;
3142 if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
3143 DECL_ARGUMENTS (olddecl
) = olddecl_arguments
;
3146 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3147 so that encode_section_info has a chance to look at the new decl
3148 flags and attributes. */
3149 if (DECL_RTL_SET_P (olddecl
)
3150 && (TREE_CODE (olddecl
) == FUNCTION_DECL
3151 || (VAR_P (olddecl
) && TREE_STATIC (olddecl
))))
3152 make_decl_rtl (olddecl
);
3155 /* Handle when a new declaration NEWDECL has the same name as an old
3156 one OLDDECL in the same binding contour. Prints an error message
3159 If safely possible, alter OLDDECL to look like NEWDECL, and return
3160 true. Otherwise, return false. */
3163 duplicate_decls (tree newdecl
, tree olddecl
)
3165 tree newtype
= NULL
, oldtype
= NULL
;
3167 if (!diagnose_mismatched_decls (newdecl
, olddecl
, &newtype
, &oldtype
))
3169 /* Avoid `unused variable' and other warnings for OLDDECL. */
3170 suppress_warning (olddecl
, OPT_Wunused
);
3171 /* If the types are completely different, poison them both with
3173 if (TREE_CODE (TREE_TYPE (newdecl
)) != TREE_CODE (TREE_TYPE (olddecl
))
3174 && olddecl
!= error_mark_node
3177 if (TREE_CODE (olddecl
) != FUNCTION_DECL
)
3178 TREE_TYPE (olddecl
) = error_mark_node
;
3179 if (TREE_CODE (newdecl
) != FUNCTION_DECL
)
3180 TREE_TYPE (newdecl
) = error_mark_node
;
3185 merge_decls (newdecl
, olddecl
, newtype
, oldtype
);
3187 /* The NEWDECL will no longer be needed.
3189 Before releasing the node, be sure to remove function from symbol
3190 table that might have been inserted there to record comdat group.
3191 Be sure to however do not free DECL_STRUCT_FUNCTION because this
3192 structure is shared in between NEWDECL and OLDECL. */
3193 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
3194 DECL_STRUCT_FUNCTION (newdecl
) = NULL
;
3195 if (VAR_OR_FUNCTION_DECL_P (newdecl
))
3197 struct symtab_node
*snode
= symtab_node::get (newdecl
);
3206 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
3208 warn_if_shadowing (tree new_decl
)
3210 struct c_binding
*b
;
3212 /* Shadow warnings wanted? */
3214 || warn_shadow_local
3215 || warn_shadow_compatible_local
)
3216 /* No shadow warnings for internally generated vars. */
3217 || DECL_IS_UNDECLARED_BUILTIN (new_decl
))
3220 /* Is anything being shadowed? Invisible decls do not count. */
3221 for (b
= I_SYMBOL_BINDING (DECL_NAME (new_decl
)); b
; b
= b
->shadowed
)
3222 if (b
->decl
&& b
->decl
!= new_decl
&& !b
->invisible
3223 && (b
->decl
== error_mark_node
3224 || diagnostic_report_warnings_p (global_dc
,
3225 DECL_SOURCE_LOCATION (b
->decl
))))
3227 tree old_decl
= b
->decl
;
3229 if (old_decl
== error_mark_node
)
3231 warning (OPT_Wshadow
, "declaration of %q+D shadows previous "
3232 "non-variable", new_decl
);
3236 bool warned
= false;
3237 auto_diagnostic_group d
;
3238 if (TREE_CODE (old_decl
) == PARM_DECL
)
3240 enum opt_code warning_code
;
3242 /* If '-Wshadow=compatible-local' is specified without other
3243 -Wshadow= flags, we will warn only when the types of the
3244 shadowing variable (i.e. new_decl) and the shadowed variable
3245 (old_decl) are compatible. */
3247 warning_code
= OPT_Wshadow
;
3248 else if (comptypes (TREE_TYPE (old_decl
), TREE_TYPE (new_decl
)))
3249 warning_code
= OPT_Wshadow_compatible_local
;
3251 warning_code
= OPT_Wshadow_local
;
3252 warned
= warning_at (DECL_SOURCE_LOCATION (new_decl
), warning_code
,
3253 "declaration of %qD shadows a parameter",
3256 else if (DECL_FILE_SCOPE_P (old_decl
))
3258 /* Do not warn if a variable shadows a function, unless
3259 the variable is a function or a pointer-to-function. */
3260 if (TREE_CODE (old_decl
) == FUNCTION_DECL
3261 && TREE_CODE (new_decl
) != FUNCTION_DECL
3262 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl
)))
3265 warned
= warning_at (DECL_SOURCE_LOCATION (new_decl
), OPT_Wshadow
,
3266 "declaration of %qD shadows a global "
3270 else if (TREE_CODE (old_decl
) == FUNCTION_DECL
3271 && fndecl_built_in_p (old_decl
))
3273 warning (OPT_Wshadow
, "declaration of %q+D shadows "
3274 "a built-in function", new_decl
);
3279 enum opt_code warning_code
;
3281 /* If '-Wshadow=compatible-local' is specified without other
3282 -Wshadow= flags, we will warn only when the types of the
3283 shadowing variable (i.e. new_decl) and the shadowed variable
3284 (old_decl) are compatible. */
3286 warning_code
= OPT_Wshadow
;
3287 else if (comptypes (TREE_TYPE (old_decl
), TREE_TYPE (new_decl
)))
3288 warning_code
= OPT_Wshadow_compatible_local
;
3290 warning_code
= OPT_Wshadow_local
;
3291 warned
= warning_at (DECL_SOURCE_LOCATION (new_decl
), warning_code
,
3292 "declaration of %qD shadows a previous local",
3297 inform (DECL_SOURCE_LOCATION (old_decl
),
3298 "shadowed declaration is here");
3304 /* Record a decl-node X as belonging to the current lexical scope.
3305 Check for errors (such as an incompatible declaration for the same
3306 name already seen in the same scope).
3308 Returns either X or an old decl for the same name.
3309 If an old decl is returned, it may have been smashed
3310 to agree with what X says. */
3315 tree name
= DECL_NAME (x
);
3316 struct c_scope
*scope
= current_scope
;
3317 struct c_binding
*b
;
3318 bool nested
= false;
3319 location_t locus
= DECL_SOURCE_LOCATION (x
);
3321 /* Must set DECL_CONTEXT for everything not at file scope or
3322 DECL_FILE_SCOPE_P won't work. Local externs don't count
3323 unless they have initializers (which generate code). We
3324 also exclude CONST_DECLs because enumerators will get the
3325 type of the enum as context. */
3326 if (current_function_decl
3327 && TREE_CODE (x
) != CONST_DECL
3328 && (!VAR_OR_FUNCTION_DECL_P (x
)
3329 || DECL_INITIAL (x
) || !TREE_PUBLIC (x
)))
3330 DECL_CONTEXT (x
) = current_function_decl
;
3332 /* Anonymous decls are just inserted in the scope. */
3335 bind (name
, x
, scope
, /*invisible=*/false, /*nested=*/false,
3340 /* First, see if there is another declaration with the same name in
3341 the current scope. If there is, duplicate_decls may do all the
3342 work for us. If duplicate_decls returns false, that indicates
3343 two incompatible decls in the same scope; we are to silently
3344 replace the old one (duplicate_decls has issued all appropriate
3345 diagnostics). In particular, we should not consider possible
3346 duplicates in the external scope, or shadowing. */
3347 b
= I_SYMBOL_BINDING (name
);
3348 if (b
&& B_IN_SCOPE (b
, scope
))
3350 struct c_binding
*b_ext
, *b_use
;
3351 tree type
= TREE_TYPE (x
);
3352 tree visdecl
= b
->decl
;
3353 tree vistype
= TREE_TYPE (visdecl
);
3354 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
3355 && COMPLETE_TYPE_P (TREE_TYPE (x
)))
3356 b
->inner_comp
= false;
3359 /* If this is an external linkage declaration, we should check
3360 for compatibility with the type in the external scope before
3361 setting the type at this scope based on the visible
3362 information only. */
3363 if (TREE_PUBLIC (x
) && TREE_PUBLIC (visdecl
))
3365 while (b_ext
&& !B_IN_EXTERNAL_SCOPE (b_ext
))
3366 b_ext
= b_ext
->shadowed
;
3371 TREE_TYPE (b_use
->decl
) = b_use
->u
.type
;
3374 if (duplicate_decls (x
, b_use
->decl
))
3378 /* Save the updated type in the external scope and
3379 restore the proper type for this scope. */
3381 if (comptypes (vistype
, type
))
3382 thistype
= composite_type (vistype
, type
);
3384 thistype
= TREE_TYPE (b_use
->decl
);
3385 b_use
->u
.type
= TREE_TYPE (b_use
->decl
);
3386 if (TREE_CODE (b_use
->decl
) == FUNCTION_DECL
3387 && fndecl_built_in_p (b_use
->decl
))
3389 = build_type_attribute_variant (thistype
,
3392 TREE_TYPE (b_use
->decl
) = thistype
;
3397 goto skip_external_and_shadow_checks
;
3400 /* All declarations with external linkage, and all external
3401 references, go in the external scope, no matter what scope is
3402 current. However, the binding in that scope is ignored for
3403 purposes of normal name lookup. A separate binding structure is
3404 created in the requested scope; this governs the normal
3405 visibility of the symbol.
3407 The binding in the externals scope is used exclusively for
3408 detecting duplicate declarations of the same object, no matter
3409 what scope they are in; this is what we do here. (C99 6.2.7p2:
3410 All declarations that refer to the same object or function shall
3411 have compatible type; otherwise, the behavior is undefined.)
3412 However, in Objective-C, we also want to detect declarations
3413 conflicting with those of the basic types. */
3414 if ((DECL_EXTERNAL (x
) || scope
== file_scope
)
3415 && (VAR_OR_FUNCTION_DECL_P (x
) || c_dialect_objc ()))
3417 tree type
= TREE_TYPE (x
);
3418 tree vistype
= NULL_TREE
;
3419 tree visdecl
= NULL_TREE
;
3420 bool type_saved
= false;
3421 if (b
&& !B_IN_EXTERNAL_SCOPE (b
)
3422 && VAR_OR_FUNCTION_DECL_P (b
->decl
)
3423 && DECL_FILE_SCOPE_P (b
->decl
))
3426 vistype
= TREE_TYPE (visdecl
);
3428 if (scope
!= file_scope
3429 && !DECL_IN_SYSTEM_HEADER (x
))
3430 warning_at (locus
, OPT_Wnested_externs
,
3431 "nested extern declaration of %qD", x
);
3433 while (b
&& !B_IN_EXTERNAL_SCOPE (b
))
3435 /* If this decl might be modified, save its type. This is
3436 done here rather than when the decl is first bound
3437 because the type may change after first binding, through
3438 being completed or through attributes being added. If we
3439 encounter multiple such decls, only the first should have
3440 its type saved; the others will already have had their
3441 proper types saved and the types will not have changed as
3442 their scopes will not have been re-entered. */
3443 if (DECL_P (b
->decl
) && DECL_FILE_SCOPE_P (b
->decl
) && !type_saved
)
3445 b
->u
.type
= TREE_TYPE (b
->decl
);
3448 if (B_IN_FILE_SCOPE (b
)
3450 && TREE_STATIC (b
->decl
)
3451 && TREE_CODE (TREE_TYPE (b
->decl
)) == ARRAY_TYPE
3452 && !TYPE_DOMAIN (TREE_TYPE (b
->decl
))
3453 && TREE_CODE (type
) == ARRAY_TYPE
3454 && TYPE_DOMAIN (type
)
3455 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
3456 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))
3458 /* Array type completed in inner scope, which should be
3459 diagnosed if the completion does not have size 1 and
3460 it does not get completed in the file scope. */
3461 b
->inner_comp
= true;
3466 /* If a matching external declaration has been found, set its
3467 type to the composite of all the types of that declaration.
3468 After the consistency checks, it will be reset to the
3469 composite of the visible types only. */
3471 TREE_TYPE (b
->decl
) = b
->u
.type
;
3473 /* the static does not go in the externals scope. */
3474 if (b
&& duplicate_decls (x
, b
->decl
))
3479 if (comptypes (vistype
, type
))
3480 thistype
= composite_type (vistype
, type
);
3482 thistype
= TREE_TYPE (b
->decl
);
3486 b
->u
.type
= TREE_TYPE (b
->decl
);
3487 /* Propagate the type attributes to the decl. */
3489 = build_type_attribute_variant (thistype
,
3490 TYPE_ATTRIBUTES (b
->u
.type
));
3491 TREE_TYPE (b
->decl
) = thistype
;
3492 bind (name
, b
->decl
, scope
, /*invisible=*/false, /*nested=*/true,
3496 else if (TREE_PUBLIC (x
))
3498 if (visdecl
&& !b
&& duplicate_decls (x
, visdecl
))
3500 /* An external declaration at block scope referring to a
3501 visible entity with internal linkage. The composite
3502 type will already be correct for this scope, so we
3503 just need to fall through to make the declaration in
3510 bind (name
, x
, external_scope
, /*invisible=*/true,
3511 /*nested=*/false, locus
);
3517 if (TREE_CODE (x
) != PARM_DECL
)
3518 warn_if_shadowing (x
);
3520 skip_external_and_shadow_checks
:
3521 if (TREE_CODE (x
) == TYPE_DECL
)
3523 /* So this is a typedef, set its underlying type. */
3524 set_underlying_type (x
);
3526 /* If X is a typedef defined in the current function, record it
3527 for the purpose of implementing the -Wunused-local-typedefs
3529 record_locally_defined_typedef (x
);
3532 bind (name
, x
, scope
, /*invisible=*/false, nested
, locus
);
3534 /* If x's type is incomplete because it's based on a
3535 structure or union which has not yet been fully declared,
3536 attach it to that structure or union type, so we can go
3537 back and complete the variable declaration later, if the
3538 structure or union gets fully declared.
3540 If the input is erroneous, we can have error_mark in the type
3541 slot (e.g. "f(void a, ...)") - that doesn't count as an
3543 if (TREE_TYPE (x
) != error_mark_node
3544 && !COMPLETE_TYPE_P (TREE_TYPE (x
)))
3546 tree element
= TREE_TYPE (x
);
3548 while (TREE_CODE (element
) == ARRAY_TYPE
)
3549 element
= TREE_TYPE (element
);
3550 element
= TYPE_MAIN_VARIANT (element
);
3552 if ((RECORD_OR_UNION_TYPE_P (element
)
3553 || TREE_CODE (element
) == ENUMERAL_TYPE
)
3554 && (TREE_CODE (x
) != TYPE_DECL
3555 || TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
)
3556 && !COMPLETE_TYPE_P (element
))
3557 C_TYPE_INCOMPLETE_VARS (element
)
3558 = tree_cons (NULL_TREE
, x
, C_TYPE_INCOMPLETE_VARS (element
));
3564 /* Issue a permerror about implicit function declaration. ID is the function
3565 identifier, OLDDECL is a declaration of the function in a different scope,
3569 implicit_decl_permerror (location_t loc
, tree id
, tree olddecl
)
3571 if (!warn_implicit_function_declaration
)
3575 auto_diagnostic_group d
;
3578 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_FUNCTION_NAME
, loc
);
3582 if (const char *suggestion
= hint
.suggestion ())
3584 gcc_rich_location
richloc (loc
);
3585 richloc
.add_fixit_replace (suggestion
);
3586 warned
= permerror_opt (&richloc
, OPT_Wimplicit_function_declaration
,
3587 "implicit declaration of function %qE;"
3588 " did you mean %qs?",
3592 warned
= permerror_opt (loc
, OPT_Wimplicit_function_declaration
,
3593 "implicit declaration of function %qE", id
);
3595 else if (const char *suggestion
= hint
.suggestion ())
3597 gcc_rich_location
richloc (loc
);
3598 richloc
.add_fixit_replace (suggestion
);
3600 (&richloc
, OPT_Wimplicit_function_declaration
,
3601 G_("implicit declaration of function %qE; did you mean %qs?"),
3605 warned
= warning_at (loc
, OPT_Wimplicit_function_declaration
,
3606 G_("implicit declaration of function %qE"), id
);
3610 /* Whether the olddecl is an undeclared builtin function.
3611 locate_old_decl will not generate a diagnostic for those,
3612 so in that case we want to look elsewhere. */
3613 bool undeclared_builtin
= (olddecl
3614 && TREE_CODE (olddecl
) == FUNCTION_DECL
3615 && fndecl_built_in_p (olddecl
)
3616 && !C_DECL_DECLARED_BUILTIN (olddecl
));
3617 if (undeclared_builtin
)
3619 const char *header
= header_for_builtin_fn (olddecl
);
3622 rich_location
richloc (line_table
, loc
);
3623 maybe_add_include_fixit (&richloc
, header
, true);
3625 "include %qs or provide a declaration of %qE",
3630 locate_old_decl (olddecl
);
3637 /* Return the name of the header file that declares built-in function
3638 FNDECL, or null if either we don't know or don't expect to see an
3639 explicit declaration. */
3642 header_for_builtin_fn (tree fndecl
)
3644 if (DECL_BUILT_IN_CLASS (fndecl
) != BUILT_IN_NORMAL
)
3647 switch (DECL_FUNCTION_CODE (fndecl
))
3649 CASE_FLT_FN (BUILT_IN_ACOS
):
3650 CASE_FLT_FN (BUILT_IN_ACOSH
):
3651 CASE_FLT_FN (BUILT_IN_ASIN
):
3652 CASE_FLT_FN (BUILT_IN_ASINH
):
3653 CASE_FLT_FN (BUILT_IN_ATAN
):
3654 CASE_FLT_FN (BUILT_IN_ATANH
):
3655 CASE_FLT_FN (BUILT_IN_ATAN2
):
3656 CASE_FLT_FN (BUILT_IN_CBRT
):
3657 CASE_FLT_FN (BUILT_IN_CEIL
):
3658 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL
):
3659 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
3660 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN
):
3661 CASE_FLT_FN (BUILT_IN_COS
):
3662 CASE_FLT_FN (BUILT_IN_COSH
):
3663 CASE_FLT_FN (BUILT_IN_ERF
):
3664 CASE_FLT_FN (BUILT_IN_ERFC
):
3665 CASE_FLT_FN (BUILT_IN_EXP
):
3666 CASE_FLT_FN (BUILT_IN_EXP2
):
3667 CASE_FLT_FN (BUILT_IN_EXPM1
):
3668 CASE_FLT_FN (BUILT_IN_FABS
):
3669 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS
):
3670 CASE_FLT_FN (BUILT_IN_FDIM
):
3671 CASE_FLT_FN (BUILT_IN_FLOOR
):
3672 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR
):
3673 CASE_FLT_FN (BUILT_IN_FMA
):
3674 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA
):
3675 CASE_FLT_FN (BUILT_IN_FMAX
):
3676 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX
):
3677 CASE_FLT_FN (BUILT_IN_FMIN
):
3678 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN
):
3679 CASE_FLT_FN (BUILT_IN_FMOD
):
3680 CASE_FLT_FN (BUILT_IN_FREXP
):
3681 CASE_FLT_FN (BUILT_IN_HYPOT
):
3682 CASE_FLT_FN (BUILT_IN_ILOGB
):
3683 CASE_FLT_FN (BUILT_IN_LDEXP
):
3684 CASE_FLT_FN (BUILT_IN_LGAMMA
):
3685 CASE_FLT_FN (BUILT_IN_LLRINT
):
3686 CASE_FLT_FN (BUILT_IN_LLROUND
):
3687 CASE_FLT_FN (BUILT_IN_LOG
):
3688 CASE_FLT_FN (BUILT_IN_LOG10
):
3689 CASE_FLT_FN (BUILT_IN_LOG1P
):
3690 CASE_FLT_FN (BUILT_IN_LOG2
):
3691 CASE_FLT_FN (BUILT_IN_LOGB
):
3692 CASE_FLT_FN (BUILT_IN_LRINT
):
3693 CASE_FLT_FN (BUILT_IN_LROUND
):
3694 CASE_FLT_FN (BUILT_IN_MODF
):
3695 CASE_FLT_FN (BUILT_IN_NAN
):
3696 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
3697 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT
):
3698 CASE_FLT_FN (BUILT_IN_NEXTAFTER
):
3699 CASE_FLT_FN (BUILT_IN_NEXTTOWARD
):
3700 CASE_FLT_FN (BUILT_IN_POW
):
3701 CASE_FLT_FN (BUILT_IN_REMAINDER
):
3702 CASE_FLT_FN (BUILT_IN_REMQUO
):
3703 CASE_FLT_FN (BUILT_IN_RINT
):
3704 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT
):
3705 CASE_FLT_FN (BUILT_IN_ROUND
):
3706 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND
):
3707 CASE_FLT_FN (BUILT_IN_SCALBLN
):
3708 CASE_FLT_FN (BUILT_IN_SCALBN
):
3709 CASE_FLT_FN (BUILT_IN_SIN
):
3710 CASE_FLT_FN (BUILT_IN_SINH
):
3711 CASE_FLT_FN (BUILT_IN_SINCOS
):
3712 CASE_FLT_FN (BUILT_IN_SQRT
):
3713 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT
):
3714 CASE_FLT_FN (BUILT_IN_TAN
):
3715 CASE_FLT_FN (BUILT_IN_TANH
):
3716 CASE_FLT_FN (BUILT_IN_TGAMMA
):
3717 CASE_FLT_FN (BUILT_IN_TRUNC
):
3718 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC
):
3719 case BUILT_IN_ISINF
:
3720 case BUILT_IN_ISNAN
:
3722 CASE_FLT_FN (BUILT_IN_CABS
):
3723 CASE_FLT_FN (BUILT_IN_CACOS
):
3724 CASE_FLT_FN (BUILT_IN_CACOSH
):
3725 CASE_FLT_FN (BUILT_IN_CARG
):
3726 CASE_FLT_FN (BUILT_IN_CASIN
):
3727 CASE_FLT_FN (BUILT_IN_CASINH
):
3728 CASE_FLT_FN (BUILT_IN_CATAN
):
3729 CASE_FLT_FN (BUILT_IN_CATANH
):
3730 CASE_FLT_FN (BUILT_IN_CCOS
):
3731 CASE_FLT_FN (BUILT_IN_CCOSH
):
3732 CASE_FLT_FN (BUILT_IN_CEXP
):
3733 CASE_FLT_FN (BUILT_IN_CIMAG
):
3734 CASE_FLT_FN (BUILT_IN_CLOG
):
3735 CASE_FLT_FN (BUILT_IN_CONJ
):
3736 CASE_FLT_FN (BUILT_IN_CPOW
):
3737 CASE_FLT_FN (BUILT_IN_CPROJ
):
3738 CASE_FLT_FN (BUILT_IN_CREAL
):
3739 CASE_FLT_FN (BUILT_IN_CSIN
):
3740 CASE_FLT_FN (BUILT_IN_CSINH
):
3741 CASE_FLT_FN (BUILT_IN_CSQRT
):
3742 CASE_FLT_FN (BUILT_IN_CTAN
):
3743 CASE_FLT_FN (BUILT_IN_CTANH
):
3744 return "<complex.h>";
3745 case BUILT_IN_MEMCHR
:
3746 case BUILT_IN_MEMCMP
:
3747 case BUILT_IN_MEMCPY
:
3748 case BUILT_IN_MEMMOVE
:
3749 case BUILT_IN_MEMSET
:
3750 case BUILT_IN_STRCAT
:
3751 case BUILT_IN_STRCHR
:
3752 case BUILT_IN_STRCMP
:
3753 case BUILT_IN_STRCPY
:
3754 case BUILT_IN_STRCSPN
:
3755 case BUILT_IN_STRLEN
:
3756 case BUILT_IN_STRNCAT
:
3757 case BUILT_IN_STRNCMP
:
3758 case BUILT_IN_STRNCPY
:
3759 case BUILT_IN_STRPBRK
:
3760 case BUILT_IN_STRRCHR
:
3761 case BUILT_IN_STRSPN
:
3762 case BUILT_IN_STRSTR
:
3763 return "<string.h>";
3764 case BUILT_IN_FPRINTF
:
3766 case BUILT_IN_FPUTC
:
3767 case BUILT_IN_FPUTS
:
3768 case BUILT_IN_FSCANF
:
3769 case BUILT_IN_FWRITE
:
3770 case BUILT_IN_PRINTF
:
3771 case BUILT_IN_PUTCHAR
:
3773 case BUILT_IN_SCANF
:
3774 case BUILT_IN_SNPRINTF
:
3775 case BUILT_IN_SPRINTF
:
3776 case BUILT_IN_SSCANF
:
3777 case BUILT_IN_VFPRINTF
:
3778 case BUILT_IN_VFSCANF
:
3779 case BUILT_IN_VPRINTF
:
3780 case BUILT_IN_VSCANF
:
3781 case BUILT_IN_VSNPRINTF
:
3782 case BUILT_IN_VSPRINTF
:
3783 case BUILT_IN_VSSCANF
:
3785 case BUILT_IN_ISALNUM
:
3786 case BUILT_IN_ISALPHA
:
3787 case BUILT_IN_ISBLANK
:
3788 case BUILT_IN_ISCNTRL
:
3789 case BUILT_IN_ISDIGIT
:
3790 case BUILT_IN_ISGRAPH
:
3791 case BUILT_IN_ISLOWER
:
3792 case BUILT_IN_ISPRINT
:
3793 case BUILT_IN_ISPUNCT
:
3794 case BUILT_IN_ISSPACE
:
3795 case BUILT_IN_ISUPPER
:
3796 case BUILT_IN_ISXDIGIT
:
3797 case BUILT_IN_TOLOWER
:
3798 case BUILT_IN_TOUPPER
:
3800 case BUILT_IN_ISWALNUM
:
3801 case BUILT_IN_ISWALPHA
:
3802 case BUILT_IN_ISWBLANK
:
3803 case BUILT_IN_ISWCNTRL
:
3804 case BUILT_IN_ISWDIGIT
:
3805 case BUILT_IN_ISWGRAPH
:
3806 case BUILT_IN_ISWLOWER
:
3807 case BUILT_IN_ISWPRINT
:
3808 case BUILT_IN_ISWPUNCT
:
3809 case BUILT_IN_ISWSPACE
:
3810 case BUILT_IN_ISWUPPER
:
3811 case BUILT_IN_ISWXDIGIT
:
3812 case BUILT_IN_TOWLOWER
:
3813 case BUILT_IN_TOWUPPER
:
3814 return "<wctype.h>";
3815 case BUILT_IN_ABORT
:
3817 case BUILT_IN_CALLOC
:
3821 case BUILT_IN_LLABS
:
3822 case BUILT_IN_MALLOC
:
3823 case BUILT_IN_REALLOC
:
3824 case BUILT_IN__EXIT2
:
3825 case BUILT_IN_ALIGNED_ALLOC
:
3826 return "<stdlib.h>";
3827 case BUILT_IN_IMAXABS
:
3828 return "<inttypes.h>";
3829 case BUILT_IN_STRFTIME
:
3836 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3837 function of type int (). */
3840 implicitly_declare (location_t loc
, tree functionid
)
3842 struct c_binding
*b
;
3843 tree decl
= NULL_TREE
;
3846 for (b
= I_SYMBOL_BINDING (functionid
); b
; b
= b
->shadowed
)
3848 if (B_IN_SCOPE (b
, external_scope
))
3857 if (TREE_CODE (decl
) != FUNCTION_DECL
)
3860 /* FIXME: Objective-C has weird not-really-builtin functions
3861 which are supposed to be visible automatically. They wind up
3862 in the external scope because they're pushed before the file
3863 scope gets created. Catch this here and rebind them into the
3865 if (!fndecl_built_in_p (decl
) && DECL_IS_UNDECLARED_BUILTIN (decl
))
3867 bind (functionid
, decl
, file_scope
,
3868 /*invisible=*/false, /*nested=*/true,
3869 DECL_SOURCE_LOCATION (decl
));
3874 tree newtype
= default_function_type
;
3876 TREE_TYPE (decl
) = b
->u
.type
;
3877 /* Implicit declaration of a function already declared
3878 (somehow) in a different scope, or as a built-in.
3879 If this is the first time this has happened, warn;
3880 then recycle the old declaration but with the new type. */
3881 if (!C_DECL_IMPLICIT (decl
))
3883 implicit_decl_permerror (loc
, functionid
, decl
);
3884 C_DECL_IMPLICIT (decl
) = 1;
3886 if (fndecl_built_in_p (decl
))
3888 newtype
= build_type_attribute_variant (newtype
,
3890 (TREE_TYPE (decl
)));
3891 if (!comptypes (newtype
, TREE_TYPE (decl
)))
3893 auto_diagnostic_group d
;
3894 bool warned
= warning_at (loc
,
3895 OPT_Wbuiltin_declaration_mismatch
,
3896 "incompatible implicit "
3897 "declaration of built-in "
3898 "function %qD", decl
);
3899 /* See if we can hint which header to include. */
3900 const char *header
= header_for_builtin_fn (decl
);
3901 if (header
!= NULL
&& warned
)
3903 rich_location
richloc (line_table
, loc
);
3904 maybe_add_include_fixit (&richloc
, header
, true);
3906 "include %qs or provide a declaration of %qD",
3909 newtype
= TREE_TYPE (decl
);
3914 if (!comptypes (newtype
, TREE_TYPE (decl
)))
3916 auto_diagnostic_group d
;
3917 error_at (loc
, "incompatible implicit declaration of "
3918 "function %qD", decl
);
3919 locate_old_decl (decl
);
3922 b
->u
.type
= TREE_TYPE (decl
);
3923 TREE_TYPE (decl
) = newtype
;
3924 bind (functionid
, decl
, current_scope
,
3925 /*invisible=*/false, /*nested=*/true,
3926 DECL_SOURCE_LOCATION (decl
));
3931 /* Not seen before. */
3932 decl
= build_decl (loc
, FUNCTION_DECL
, functionid
, default_function_type
);
3933 DECL_EXTERNAL (decl
) = 1;
3934 TREE_PUBLIC (decl
) = 1;
3935 C_DECL_IMPLICIT (decl
) = 1;
3936 implicit_decl_permerror (loc
, functionid
, 0);
3937 asmspec_tree
= maybe_apply_renaming_pragma (decl
, /*asmname=*/NULL
);
3939 set_user_assembler_name (decl
, TREE_STRING_POINTER (asmspec_tree
));
3941 /* C89 says implicit declarations are in the innermost block.
3942 So we record the decl in the standard fashion. */
3943 decl
= pushdecl (decl
);
3945 /* No need to call objc_check_decl here - it's a function type. */
3946 rest_of_decl_compilation (decl
, 0, 0);
3948 /* Write a record describing this implicit function declaration
3949 to the prototypes file (if requested). */
3950 gen_aux_info_record (decl
, 0, 1, 0);
3952 /* Possibly apply some default attributes to this implicit declaration. */
3953 decl_attributes (&decl
, NULL_TREE
, 0);
3958 /* Issue an error message for a reference to an undeclared variable
3959 ID, including a reference to a builtin outside of function-call
3960 context. Establish a binding of the identifier to error_mark_node
3961 in an appropriate scope, which will suppress further errors for the
3962 same identifier. The error message should be given location LOC. */
3964 undeclared_variable (location_t loc
, tree id
)
3966 static bool already
= false;
3967 struct c_scope
*scope
;
3969 auto_diagnostic_group d
;
3970 if (current_function_decl
== NULL_TREE
)
3972 name_hint guessed_id
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_NAME
, loc
);
3973 if (const char *suggestion
= guessed_id
.suggestion ())
3975 gcc_rich_location
richloc (loc
);
3976 richloc
.add_fixit_replace (suggestion
);
3978 "%qE undeclared here (not in a function);"
3979 " did you mean %qs?",
3983 error_at (loc
, "%qE undeclared here (not in a function)", id
);
3984 scope
= current_scope
;
3988 if (!objc_diagnose_private_ivar (id
))
3990 name_hint guessed_id
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_NAME
, loc
);
3991 if (const char *suggestion
= guessed_id
.suggestion ())
3993 gcc_rich_location
richloc (loc
);
3994 richloc
.add_fixit_replace (suggestion
);
3996 "%qE undeclared (first use in this function);"
3997 " did you mean %qs?",
4001 error_at (loc
, "%qE undeclared (first use in this function)", id
);
4005 inform (loc
, "each undeclared identifier is reported only"
4006 " once for each function it appears in");
4010 /* If we are parsing old-style parameter decls, current_function_decl
4011 will be nonnull but current_function_scope will be null. */
4012 scope
= current_function_scope
? current_function_scope
: current_scope
;
4014 bind (id
, error_mark_node
, scope
, /*invisible=*/false, /*nested=*/false,
4018 /* Subroutine of lookup_label, declare_label, define_label: construct a
4019 LABEL_DECL with all the proper frills. Also create a struct
4020 c_label_vars initialized for the current scope. */
4023 make_label (location_t location
, tree name
, bool defining
,
4024 struct c_label_vars
**p_label_vars
)
4026 tree label
= build_decl (location
, LABEL_DECL
, name
, void_type_node
);
4027 DECL_CONTEXT (label
) = current_function_decl
;
4028 SET_DECL_MODE (label
, VOIDmode
);
4030 c_label_vars
*label_vars
= ggc_alloc
<c_label_vars
> ();
4031 label_vars
->shadowed
= NULL
;
4032 set_spot_bindings (&label_vars
->label_bindings
, defining
);
4033 label_vars
->decls_in_scope
= make_tree_vector ();
4034 label_vars
->gotos
= NULL
;
4035 *p_label_vars
= label_vars
;
4040 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
4041 Create one if none exists so far for the current function.
4042 This is called when a label is used in a goto expression or
4043 has its address taken. */
4046 lookup_label (tree name
)
4049 struct c_label_vars
*label_vars
;
4051 if (current_function_scope
== 0)
4053 error ("label %qE referenced outside of any function", name
);
4057 /* Use a label already defined or ref'd with this name, but not if
4058 it is inherited from a containing function and wasn't declared
4060 label
= I_LABEL_DECL (name
);
4061 if (label
&& (DECL_CONTEXT (label
) == current_function_decl
4062 || C_DECLARED_LABEL_FLAG (label
)))
4064 /* If the label has only been declared, update its apparent
4065 location to point here, for better diagnostics if it
4066 turns out not to have been defined. */
4067 if (DECL_INITIAL (label
) == NULL_TREE
)
4068 DECL_SOURCE_LOCATION (label
) = input_location
;
4072 /* No label binding for that identifier; make one. */
4073 label
= make_label (input_location
, name
, false, &label_vars
);
4075 /* Ordinary labels go in the current function scope. */
4076 bind_label (name
, label
, current_function_scope
, label_vars
);
4081 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
4085 warn_about_goto (location_t goto_loc
, tree label
, tree decl
)
4087 auto_diagnostic_group d
;
4088 if (c_type_variably_modified_p (TREE_TYPE (decl
)))
4090 "jump into scope of identifier with variably modified type");
4091 else if (flag_openmp
4092 && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl
)))
4093 error_at (goto_loc
, "jump skips OpenMP %<allocate%> allocation");
4095 if (!warning_at (goto_loc
, OPT_Wjump_misses_init
,
4096 "jump skips variable initialization"))
4098 inform (DECL_SOURCE_LOCATION (label
), "label %qD defined here", label
);
4099 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
4102 /* Look up a label because of a goto statement. This is like
4103 lookup_label, but also issues any appropriate warnings. */
4106 lookup_label_for_goto (location_t loc
, tree name
)
4109 struct c_label_vars
*label_vars
;
4113 label
= lookup_label (name
);
4114 if (label
== NULL_TREE
)
4117 /* If we are jumping to a different function, we can't issue any
4119 if (DECL_CONTEXT (label
) != current_function_decl
)
4121 gcc_assert (C_DECLARED_LABEL_FLAG (label
));
4125 label_vars
= I_LABEL_BINDING (name
)->u
.label
;
4127 /* If the label has not yet been defined, then push this goto on a
4128 list for possible later warnings. */
4129 if (label_vars
->label_bindings
.scope
== NULL
)
4131 c_goto_bindings
*g
= ggc_alloc
<c_goto_bindings
> ();
4134 set_spot_bindings (&g
->goto_bindings
, true);
4135 vec_safe_push (label_vars
->gotos
, g
);
4139 /* If there are any decls in label_vars->decls_in_scope, then this
4140 goto has missed the declaration of the decl. This happens for a
4146 Issue a warning or error. */
4147 FOR_EACH_VEC_SAFE_ELT (label_vars
->decls_in_scope
, ix
, decl
)
4148 warn_about_goto (loc
, label
, decl
);
4150 if (label_vars
->label_bindings
.left_stmt_expr
)
4152 auto_diagnostic_group d
;
4153 error_at (loc
, "jump into statement expression");
4154 inform (DECL_SOURCE_LOCATION (label
), "label %qD defined here", label
);
4160 /* Make a label named NAME in the current function, shadowing silently
4161 any that may be inherited from containing functions or containing
4162 scopes. This is called for __label__ declarations. */
4165 declare_label (tree name
)
4167 struct c_binding
*b
= I_LABEL_BINDING (name
);
4169 struct c_label_vars
*label_vars
;
4171 /* Check to make sure that the label hasn't already been declared
4173 if (b
&& B_IN_CURRENT_SCOPE (b
))
4175 auto_diagnostic_group d
;
4176 error ("duplicate label declaration %qE", name
);
4177 locate_old_decl (b
->decl
);
4179 /* Just use the previous declaration. */
4183 label
= make_label (input_location
, name
, false, &label_vars
);
4184 C_DECLARED_LABEL_FLAG (label
) = 1;
4186 /* Declared labels go in the current scope. */
4187 bind_label (name
, label
, current_scope
, label_vars
);
4192 /* When we define a label, issue any appropriate warnings if there are
4193 any gotos earlier in the function which jump to this label. */
4196 check_earlier_gotos (tree label
, struct c_label_vars
* label_vars
)
4199 struct c_goto_bindings
*g
;
4201 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
4203 struct c_binding
*b
;
4204 struct c_scope
*scope
;
4206 /* We have a goto to this label. The goto is going forward. In
4207 g->scope, the goto is going to skip any binding which was
4208 defined after g->bindings_in_scope. */
4209 if (g
->goto_bindings
.scope
->has_jump_unsafe_decl
)
4211 for (b
= g
->goto_bindings
.scope
->bindings
;
4212 b
!= g
->goto_bindings
.bindings_in_scope
;
4215 if (decl_jump_unsafe (b
->decl
))
4216 warn_about_goto (g
->loc
, label
, b
->decl
);
4220 /* We also need to warn about decls defined in any scopes
4221 between the scope of the label and the scope of the goto. */
4222 for (scope
= label_vars
->label_bindings
.scope
;
4223 scope
!= g
->goto_bindings
.scope
;
4224 scope
= scope
->outer
)
4226 gcc_assert (scope
!= NULL
);
4227 if (scope
->has_jump_unsafe_decl
)
4229 if (scope
== label_vars
->label_bindings
.scope
)
4230 b
= label_vars
->label_bindings
.bindings_in_scope
;
4232 b
= scope
->bindings
;
4233 for (; b
!= NULL
; b
= b
->prev
)
4235 if (decl_jump_unsafe (b
->decl
))
4236 warn_about_goto (g
->loc
, label
, b
->decl
);
4241 if (g
->goto_bindings
.stmt_exprs
> 0)
4243 auto_diagnostic_group d
;
4244 error_at (g
->loc
, "jump into statement expression");
4245 inform (DECL_SOURCE_LOCATION (label
), "label %qD defined here",
4250 /* Now that the label is defined, we will issue warnings about
4251 subsequent gotos to this label when we see them. */
4252 vec_safe_truncate (label_vars
->gotos
, 0);
4253 label_vars
->gotos
= NULL
;
4256 /* Define a label, specifying the location in the source file.
4257 Return the LABEL_DECL node for the label, if the definition is valid.
4258 Otherwise return NULL_TREE. */
4261 define_label (location_t location
, tree name
)
4263 /* Find any preexisting label with this name. It is an error
4264 if that label has already been defined in this function, or
4265 if there is a containing function with a declared label with
4267 tree label
= I_LABEL_DECL (name
);
4270 && ((DECL_CONTEXT (label
) == current_function_decl
4271 && DECL_INITIAL (label
) != NULL_TREE
)
4272 || (DECL_CONTEXT (label
) != current_function_decl
4273 && C_DECLARED_LABEL_FLAG (label
))))
4275 auto_diagnostic_group d
;
4276 error_at (location
, "duplicate label %qD", label
);
4277 locate_old_decl (label
);
4280 else if (label
&& DECL_CONTEXT (label
) == current_function_decl
)
4282 struct c_label_vars
*label_vars
= I_LABEL_BINDING (name
)->u
.label
;
4284 /* The label has been used or declared already in this function,
4285 but not defined. Update its location to point to this
4287 DECL_SOURCE_LOCATION (label
) = location
;
4288 set_spot_bindings (&label_vars
->label_bindings
, true);
4290 /* Issue warnings as required about any goto statements from
4291 earlier in the function. */
4292 check_earlier_gotos (label
, label_vars
);
4296 struct c_label_vars
*label_vars
;
4298 /* No label binding for that identifier; make one. */
4299 label
= make_label (location
, name
, true, &label_vars
);
4301 /* Ordinary labels go in the current function scope. */
4302 bind_label (name
, label
, current_function_scope
, label_vars
);
4305 if (!in_system_header_at (input_location
) && lookup_name (name
))
4306 warning_at (location
, OPT_Wtraditional
,
4307 "traditional C lacks a separate namespace "
4308 "for labels, identifier %qE conflicts", name
);
4310 /* Mark label as having been defined. */
4311 DECL_INITIAL (label
) = error_mark_node
;
4315 /* Get the bindings for a new switch statement. This is used to issue
4316 warnings as appropriate for jumps from the switch to case or
4319 struct c_spot_bindings
*
4320 c_get_switch_bindings (void)
4322 struct c_spot_bindings
*switch_bindings
;
4324 switch_bindings
= XNEW (struct c_spot_bindings
);
4325 set_spot_bindings (switch_bindings
, true);
4326 return switch_bindings
;
4330 c_release_switch_bindings (struct c_spot_bindings
*bindings
)
4332 gcc_assert (bindings
->stmt_exprs
== 0 && !bindings
->left_stmt_expr
);
4336 /* This is called at the point of a case or default label to issue
4337 warnings about decls as needed. It returns true if it found an
4338 error, not just a warning. */
4341 c_check_switch_jump_warnings (struct c_spot_bindings
*switch_bindings
,
4342 location_t switch_loc
, location_t case_loc
)
4345 struct c_scope
*scope
;
4348 for (scope
= current_scope
;
4349 scope
!= switch_bindings
->scope
;
4350 scope
= scope
->outer
)
4352 struct c_binding
*b
;
4354 gcc_assert (scope
!= NULL
);
4356 if (!scope
->has_jump_unsafe_decl
)
4359 for (b
= scope
->bindings
; b
!= NULL
; b
= b
->prev
)
4361 if (decl_jump_unsafe (b
->decl
))
4363 auto_diagnostic_group d
;
4365 if (c_type_variably_modified_p (TREE_TYPE (b
->decl
)))
4369 ("switch jumps into scope of identifier with "
4370 "variably modified type"));
4373 else if (flag_openmp
4374 && lookup_attribute ("omp allocate",
4375 DECL_ATTRIBUTES (b
->decl
)))
4379 "switch jumps over OpenMP %<allocate%> allocation");
4384 = warning_at (case_loc
, OPT_Wjump_misses_init
,
4385 "switch jumps over variable initialization");
4388 inform (switch_loc
, "switch starts here");
4389 inform (DECL_SOURCE_LOCATION (b
->decl
), "%qD declared here",
4396 if (switch_bindings
->stmt_exprs
> 0)
4399 auto_diagnostic_group d
;
4400 error_at (case_loc
, "switch jumps into statement expression");
4401 inform (switch_loc
, "switch starts here");
4407 /* Given NAME, an IDENTIFIER_NODE,
4408 return the structure (or union or enum) definition for that name.
4409 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4410 CODE says which kind of type the caller wants;
4411 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4412 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4413 location where the tag was defined.
4414 If the wrong kind of type is found, an error is reported. */
4417 lookup_tag (enum tree_code code
, tree name
, bool thislevel_only
,
4420 struct c_binding
*b
= I_TAG_BINDING (name
);
4421 bool thislevel
= false;
4426 /* We only care about whether it's in this level if
4427 thislevel_only was set or it might be a type clash. */
4428 if (thislevel_only
|| TREE_CODE (b
->decl
) != code
)
4430 /* For our purposes, a tag in the external scope is the same as
4431 a tag in the file scope. (Primarily relevant to Objective-C
4432 and its builtin structure tags, which get pushed before the
4433 file scope is created.) */
4434 if (B_IN_CURRENT_SCOPE (b
)
4435 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
4439 if (thislevel_only
&& !thislevel
)
4442 if (TREE_CODE (b
->decl
) != code
)
4444 /* Definition isn't the kind we were looking for. */
4445 pending_invalid_xref
= name
;
4446 pending_invalid_xref_location
= input_location
;
4448 /* If in the same binding level as a declaration as a tag
4449 of a different type, this must not be allowed to
4450 shadow that tag, so give the error immediately.
4451 (For example, "struct foo; union foo;" is invalid.) */
4453 pending_xref_error ();
4462 /* Return true if a definition exists for NAME with code CODE. */
4465 tag_exists_p (enum tree_code code
, tree name
)
4467 struct c_binding
*b
= I_TAG_BINDING (name
);
4469 if (b
== NULL
|| b
->decl
== NULL_TREE
)
4471 return TREE_CODE (b
->decl
) == code
;
4474 /* Print an error message now
4475 for a recent invalid struct, union or enum cross reference.
4476 We don't print them immediately because they are not invalid
4477 when used in the `struct foo;' construct for shadowing. */
4480 pending_xref_error (void)
4482 if (pending_invalid_xref
!= NULL_TREE
)
4483 error_at (pending_invalid_xref_location
, "%qE defined as wrong kind of tag",
4484 pending_invalid_xref
);
4485 pending_invalid_xref
= NULL_TREE
;
4489 /* Look up NAME in the current scope and its superiors
4490 in the namespace of variables, functions and typedefs.
4491 Return a ..._DECL node of some kind representing its definition,
4492 or return NULL_TREE if it is undefined. */
4495 lookup_name (tree name
)
4497 struct c_binding
*b
= I_SYMBOL_BINDING (name
);
4498 if (b
&& !b
->invisible
)
4500 maybe_record_typedef_use (b
->decl
);
4506 /* Similar to `lookup_name' but look only at the indicated scope. */
4509 lookup_name_in_scope (tree name
, struct c_scope
*scope
)
4511 struct c_binding
*b
;
4513 for (b
= I_SYMBOL_BINDING (name
); b
; b
= b
->shadowed
)
4514 if (B_IN_SCOPE (b
, scope
))
4519 /* Look for the closest match for NAME within the currently valid
4522 This finds the identifier with the lowest Levenshtein distance to
4523 NAME. If there are multiple candidates with equal minimal distance,
4524 the first one found is returned. Scopes are searched from innermost
4525 outwards, and within a scope in reverse order of declaration, thus
4526 benefiting candidates "near" to the current scope.
4528 The function also looks for similar macro names to NAME, since a
4529 misspelled macro name will not be expanded, and hence looks like an
4530 identifier to the C frontend.
4532 It also looks for start_typename keywords, to detect "singed" vs "signed"
4535 Use LOC for any deferred diagnostics. */
4538 lookup_name_fuzzy (tree name
, enum lookup_name_fuzzy_kind kind
, location_t loc
)
4540 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
4542 /* First, try some well-known names in the C standard library, in case
4543 the user forgot a #include. */
4544 const char *header_hint
4545 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name
));
4548 return name_hint (NULL
,
4549 new suggest_missing_header (loc
,
4550 IDENTIFIER_POINTER (name
),
4553 /* Only suggest names reserved for the implementation if NAME begins
4554 with an underscore. */
4555 bool consider_implementation_names
= (IDENTIFIER_POINTER (name
)[0] == '_');
4557 best_match
<tree
, tree
> bm (name
);
4559 /* Look within currently valid scopes. */
4560 for (c_scope
*scope
= current_scope
; scope
; scope
= scope
->outer
)
4561 for (c_binding
*binding
= scope
->bindings
; binding
; binding
= binding
->prev
)
4563 if (!binding
->id
|| binding
->invisible
)
4565 if (binding
->decl
== error_mark_node
)
4567 /* Don't use bindings from implicitly declared functions,
4568 as they were likely misspellings themselves. */
4569 if (TREE_CODE (binding
->decl
) == FUNCTION_DECL
)
4570 if (C_DECL_IMPLICIT (binding
->decl
))
4572 /* Don't suggest names that are reserved for use by the
4573 implementation, unless NAME began with an underscore. */
4574 if (!consider_implementation_names
)
4576 const char *suggestion_str
= IDENTIFIER_POINTER (binding
->id
);
4577 if (name_reserved_for_implementation_p (suggestion_str
))
4582 case FUZZY_LOOKUP_TYPENAME
:
4583 if (TREE_CODE (binding
->decl
) != TYPE_DECL
)
4587 case FUZZY_LOOKUP_FUNCTION_NAME
:
4588 if (TREE_CODE (binding
->decl
) != FUNCTION_DECL
)
4590 /* Allow function pointers. */
4591 if ((VAR_P (binding
->decl
)
4592 || TREE_CODE (binding
->decl
) == PARM_DECL
)
4593 && TREE_CODE (TREE_TYPE (binding
->decl
)) == POINTER_TYPE
4594 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding
->decl
)))
4604 bm
.consider (binding
->id
);
4607 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4609 x = SOME_OTHER_MACRO (y);
4610 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4611 as a misspelled identifier.
4613 Use the best distance so far so that a candidate is only set if
4614 a macro is better than anything so far. This allows early rejection
4615 (without calculating the edit distance) of macro names that must have
4616 distance >= bm.get_best_distance (), and means that we only get a
4617 non-NULL result for best_macro_match if it's better than any of
4618 the identifiers already checked, which avoids needless creation
4619 of identifiers for macro hashnodes. */
4620 best_macro_match
bmm (name
, bm
.get_best_distance (), parse_in
);
4621 cpp_hashnode
*best_macro
= bmm
.get_best_meaningful_candidate ();
4622 /* If a macro is the closest so far to NAME, use it, creating an
4623 identifier tree node for it. */
4626 const char *id
= (const char *)best_macro
->ident
.str
;
4627 tree macro_as_identifier
4628 = get_identifier_with_length (id
, best_macro
->ident
.len
);
4629 bm
.set_best_so_far (macro_as_identifier
,
4630 bmm
.get_best_distance (),
4631 bmm
.get_best_candidate_length ());
4634 /* Try the "start_typename" keywords to detect
4635 "singed" vs "signed" typos. */
4636 if (kind
== FUZZY_LOOKUP_TYPENAME
)
4638 for (unsigned i
= 0; i
< num_c_common_reswords
; i
++)
4640 const c_common_resword
*resword
= &c_common_reswords
[i
];
4641 if (!c_keyword_starts_typename (resword
->rid
))
4643 tree resword_identifier
= ridpointers
[resword
->rid
];
4644 if (!resword_identifier
)
4646 gcc_assert (TREE_CODE (resword_identifier
) == IDENTIFIER_NODE
);
4647 bm
.consider (resword_identifier
);
4651 tree best
= bm
.get_best_meaningful_candidate ();
4653 return name_hint (IDENTIFIER_POINTER (best
), NULL
);
4655 return name_hint (NULL
, NULL
);
4659 /* Handle the standard [[nodiscard]] attribute. */
4662 handle_nodiscard_attribute (tree
*node
, tree name
, tree
/*args*/,
4663 int /*flags*/, bool *no_add_attrs
)
4665 if (TREE_CODE (*node
) == FUNCTION_DECL
)
4667 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node
))))
4668 warning_at (DECL_SOURCE_LOCATION (*node
),
4669 OPT_Wattributes
, "%qE attribute applied to %qD with void "
4670 "return type", name
, *node
);
4672 else if (RECORD_OR_UNION_TYPE_P (*node
)
4673 || TREE_CODE (*node
) == ENUMERAL_TYPE
)
4677 pedwarn (input_location
,
4678 OPT_Wattributes
, "%qE attribute can only be applied to "
4679 "functions or to structure, union or enumeration types", name
);
4680 *no_add_attrs
= true;
4685 /* Handle the standard [[noreturn]] attribute. */
4688 handle_std_noreturn_attribute (tree
*node
, tree name
, tree args
,
4689 int flags
, bool *no_add_attrs
)
4691 /* Unlike GNU __attribute__ ((noreturn)), the standard [[noreturn]]
4692 only applies to functions, not function pointers. */
4693 if (TREE_CODE (*node
) == FUNCTION_DECL
)
4694 return handle_noreturn_attribute (node
, name
, args
, flags
, no_add_attrs
);
4697 pedwarn (input_location
, OPT_Wattributes
,
4698 "standard %qE attribute can only be applied to functions",
4700 *no_add_attrs
= true;
4705 /* Table of supported standard (C23) attributes. */
4706 static const attribute_spec std_attributes
[] =
4708 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4709 affects_type_identity, handler, exclude } */
4710 { "_Noreturn", 0, 0, false, false, false, false,
4711 handle_std_noreturn_attribute
, NULL
},
4712 { "deprecated", 0, 1, false, false, false, false,
4713 handle_deprecated_attribute
, NULL
},
4714 { "fallthrough", 0, 0, false, false, false, false,
4715 handle_fallthrough_attribute
, NULL
},
4716 { "maybe_unused", 0, 0, false, false, false, false,
4717 handle_unused_attribute
, NULL
},
4718 { "nodiscard", 0, 1, false, false, false, false,
4719 handle_nodiscard_attribute
, NULL
},
4720 { "noreturn", 0, 0, false, false, false, false,
4721 handle_std_noreturn_attribute
, NULL
}
4724 const scoped_attribute_specs std_attribute_table
=
4726 nullptr, { std_attributes
}
4729 /* Create the predefined scalar types of C,
4730 and some nodes representing standard constants (0, 1, (void *) 0).
4731 Initialize the global scope.
4732 Make definitions for built-in primitive functions. */
4735 c_init_decl_processing (void)
4737 location_t save_loc
= input_location
;
4739 /* Initialize reserved words for parser. */
4742 current_function_decl
= NULL_TREE
;
4744 gcc_obstack_init (&parser_obstack
);
4746 /* Make the externals scope. */
4748 external_scope
= current_scope
;
4750 /* Declarations from c_common_nodes_and_builtins must not be associated
4751 with this input file, lest we get differences between using and not
4752 using preprocessed headers. */
4753 input_location
= BUILTINS_LOCATION
;
4755 c_common_nodes_and_builtins ();
4757 /* In C, comparisons and TRUTH_* expressions have type int. */
4758 truthvalue_type_node
= integer_type_node
;
4759 truthvalue_true_node
= integer_one_node
;
4760 truthvalue_false_node
= integer_zero_node
;
4762 /* Even in C99, which has a real boolean type. */
4763 pushdecl (build_decl (UNKNOWN_LOCATION
, TYPE_DECL
, get_identifier ("_Bool"),
4764 boolean_type_node
));
4766 /* C-specific nullptr initialization. */
4767 record_builtin_type (RID_MAX
, "nullptr_t", nullptr_type_node
);
4768 /* The size and alignment of nullptr_t is the same as for a pointer to
4770 SET_TYPE_ALIGN (nullptr_type_node
, GET_MODE_ALIGNMENT (ptr_mode
));
4772 input_location
= save_loc
;
4774 make_fname_decl
= c_make_fname_decl
;
4775 start_fname_decls ();
4778 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4779 give the decl, NAME is the initialization string and TYPE_DEP
4780 indicates whether NAME depended on the type of the function. As we
4781 don't yet implement delayed emission of static data, we mark the
4782 decl as emitted so it is not placed in the output. Anything using
4783 it must therefore pull out the STRING_CST initializer directly.
4787 c_make_fname_decl (location_t loc
, tree id
, int type_dep
)
4789 const char *name
= fname_as_string (type_dep
);
4790 tree decl
, type
, init
;
4791 size_t length
= strlen (name
);
4793 type
= build_array_type (char_type_node
,
4794 build_index_type (size_int (length
)));
4795 type
= c_build_qualified_type (type
, TYPE_QUAL_CONST
);
4797 decl
= build_decl (loc
, VAR_DECL
, id
, type
);
4799 TREE_STATIC (decl
) = 1;
4800 TREE_READONLY (decl
) = 1;
4801 DECL_ARTIFICIAL (decl
) = 1;
4803 init
= build_string (length
+ 1, name
);
4804 free (CONST_CAST (char *, name
));
4805 TREE_TYPE (init
) = type
;
4806 DECL_INITIAL (decl
) = init
;
4808 TREE_USED (decl
) = 1;
4810 if (current_function_decl
4811 /* For invalid programs like this:
4814 const char* p = __FUNCTION__;
4816 the __FUNCTION__ is believed to appear in K&R style function
4817 parameter declarator. In that case we still don't have
4819 && current_function_scope
)
4821 DECL_CONTEXT (decl
) = current_function_decl
;
4822 bind (id
, decl
, current_function_scope
,
4823 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
4826 finish_decl (decl
, loc
, init
, NULL_TREE
, NULL_TREE
);
4832 c_builtin_function (tree decl
)
4834 tree type
= TREE_TYPE (decl
);
4835 tree id
= DECL_NAME (decl
);
4837 const char *name
= IDENTIFIER_POINTER (id
);
4838 C_DECL_BUILTIN_PROTOTYPE (decl
) = prototype_p (type
);
4840 /* Should never be called on a symbol with a preexisting meaning. */
4841 gcc_assert (!I_SYMBOL_BINDING (id
));
4843 bind (id
, decl
, external_scope
, /*invisible=*/true, /*nested=*/false,
4846 /* Builtins in the implementation namespace are made visible without
4847 needing to be explicitly declared. See push_file_scope. */
4848 if (name
[0] == '_' && (name
[1] == '_' || ISUPPER (name
[1])))
4850 DECL_CHAIN (decl
) = visible_builtins
;
4851 visible_builtins
= decl
;
4858 c_builtin_function_ext_scope (tree decl
)
4860 tree type
= TREE_TYPE (decl
);
4861 tree id
= DECL_NAME (decl
);
4863 const char *name
= IDENTIFIER_POINTER (id
);
4864 C_DECL_BUILTIN_PROTOTYPE (decl
) = prototype_p (type
);
4867 bind (id
, decl
, external_scope
, /*invisible=*/false, /*nested=*/false,
4870 /* Builtins in the implementation namespace are made visible without
4871 needing to be explicitly declared. See push_file_scope. */
4872 if (name
[0] == '_' && (name
[1] == '_' || ISUPPER (name
[1])))
4874 DECL_CHAIN (decl
) = visible_builtins
;
4875 visible_builtins
= decl
;
4881 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4884 c_simulate_builtin_function_decl (tree decl
)
4886 tree type
= TREE_TYPE (decl
);
4887 C_DECL_BUILTIN_PROTOTYPE (decl
) = prototype_p (type
);
4888 return pushdecl (decl
);
4891 /* Warn about attributes in a context where they are unused
4892 (attribute-declarations, except for the "fallthrough" case, and
4893 attributes on statements). */
4896 c_warn_unused_attributes (tree attrs
)
4898 for (tree t
= attrs
; t
!= NULL_TREE
; t
= TREE_CHAIN (t
))
4899 if (get_attribute_namespace (t
) == NULL_TREE
)
4900 /* The specifications of standard attributes mean this is a
4901 constraint violation. */
4902 pedwarn (input_location
, OPT_Wattributes
, "%qE attribute ignored",
4903 get_attribute_name (t
));
4904 else if (!attribute_ignored_p (t
))
4905 warning (OPT_Wattributes
, "%qE attribute ignored",
4906 get_attribute_name (t
));
4909 /* Warn for standard attributes being applied to a type that is not
4910 being defined, where that is a constraint violation, and return a
4911 list of attributes with them removed. */
4914 c_warn_type_attributes (tree attrs
)
4916 tree
*attr_ptr
= &attrs
;
4918 if (get_attribute_namespace (*attr_ptr
) == NULL_TREE
)
4920 pedwarn (input_location
, OPT_Wattributes
, "%qE attribute ignored",
4921 get_attribute_name (*attr_ptr
));
4922 *attr_ptr
= TREE_CHAIN (*attr_ptr
);
4925 attr_ptr
= &TREE_CHAIN (*attr_ptr
);
4929 /* Called when a declaration is seen that contains no names to declare.
4930 If its type is a reference to a structure, union or enum inherited
4931 from a containing scope, shadow that tag name for the current scope
4932 with a forward reference.
4933 If its type defines a new named structure or union
4934 or defines an enum, it is valid but we need not do anything here.
4935 Otherwise, it is an error. */
4938 shadow_tag (const struct c_declspecs
*declspecs
)
4940 shadow_tag_warned (declspecs
, 0);
4943 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4946 shadow_tag_warned (const struct c_declspecs
*declspecs
, int warned
)
4948 bool found_tag
= false;
4950 if (declspecs
->type
&& !declspecs
->default_int_p
&& !declspecs
->typedef_p
)
4952 tree value
= declspecs
->type
;
4953 enum tree_code code
= TREE_CODE (value
);
4955 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
|| code
== ENUMERAL_TYPE
)
4956 /* Used to test also that TYPE_SIZE (value) != 0.
4957 That caused warning for `struct foo;' at top level in the file. */
4959 tree name
= TYPE_NAME (value
);
4964 if (declspecs
->restrict_p
)
4966 error ("invalid use of %<restrict%>");
4970 if (in_underspecified_init
)
4972 /* This can only occur with extensions such as statement
4973 expressions, but is still appropriate as an error to
4974 avoid types declared in such a context escaping to
4975 the type of an auto variable. */
4976 error ("%qT declared in underspecified object initializer",
4981 if (name
== NULL_TREE
)
4983 if (warned
!= 1 && code
!= ENUMERAL_TYPE
)
4984 /* Empty unnamed enum OK */
4986 pedwarn (input_location
, 0,
4987 "unnamed struct/union that defines no instances");
4991 else if (declspecs
->typespec_kind
!= ctsk_tagdef
4992 && declspecs
->typespec_kind
!= ctsk_tagfirstref
4993 && declspecs
->typespec_kind
!= ctsk_tagfirstref_attrs
4994 && declspecs
->storage_class
!= csc_none
)
4997 pedwarn (input_location
, 0,
4998 "empty declaration with storage class specifier "
4999 "does not redeclare tag");
5001 pending_xref_error ();
5003 else if (declspecs
->typespec_kind
!= ctsk_tagdef
5004 && declspecs
->typespec_kind
!= ctsk_tagfirstref
5005 && declspecs
->typespec_kind
!= ctsk_tagfirstref_attrs
5006 && (declspecs
->const_p
5007 || declspecs
->volatile_p
5008 || declspecs
->atomic_p
5009 || declspecs
->restrict_p
5010 || declspecs
->address_space
))
5013 pedwarn (input_location
, 0,
5014 "empty declaration with type qualifier "
5015 "does not redeclare tag");
5017 pending_xref_error ();
5019 else if (declspecs
->typespec_kind
!= ctsk_tagdef
5020 && declspecs
->typespec_kind
!= ctsk_tagfirstref
5021 && declspecs
->typespec_kind
!= ctsk_tagfirstref_attrs
5022 && declspecs
->alignas_p
)
5025 pedwarn (input_location
, 0,
5026 "empty declaration with %<_Alignas%> "
5027 "does not redeclare tag");
5029 pending_xref_error ();
5031 else if (declspecs
->typespec_kind
!= ctsk_tagdef
5032 && declspecs
->typespec_kind
!= ctsk_tagfirstref
5033 && declspecs
->typespec_kind
!= ctsk_tagfirstref_attrs
5034 && code
== ENUMERAL_TYPE
5035 && !declspecs
->enum_type_specifier_ref_p
)
5037 bool warned_enum
= false;
5039 warned_enum
= pedwarn (input_location
, OPT_Wpedantic
,
5040 "empty declaration of %<enum%> type "
5041 "does not redeclare tag");
5044 pending_xref_error ();
5048 pending_invalid_xref
= NULL_TREE
;
5049 t
= lookup_tag (code
, name
, true, NULL
);
5053 t
= make_node (code
);
5054 pushtag (input_location
, name
, t
);
5060 if (warned
!= 1 && !in_system_header_at (input_location
))
5062 pedwarn (input_location
, 0,
5063 "useless type name in empty declaration");
5068 else if (warned
!= 1 && !in_system_header_at (input_location
)
5069 && declspecs
->typedef_p
)
5071 pedwarn (input_location
, 0, "useless type name in empty declaration");
5075 pending_invalid_xref
= NULL_TREE
;
5077 if (declspecs
->inline_p
)
5079 error ("%<inline%> in empty declaration");
5083 if (declspecs
->noreturn_p
)
5085 error ("%<_Noreturn%> in empty declaration");
5089 if (declspecs
->constexpr_p
)
5091 error ("%<constexpr%> in empty declaration");
5095 if (current_scope
== file_scope
&& declspecs
->storage_class
== csc_auto
)
5097 error ("%<auto%> in file-scope empty declaration");
5101 if (current_scope
== file_scope
&& declspecs
->storage_class
== csc_register
)
5103 error ("%<register%> in file-scope empty declaration");
5107 if (declspecs
->enum_type_specifier_ref_p
&& !warned
)
5109 if (declspecs
->storage_class
!= csc_none
)
5111 error ("storage class specifier in empty declaration with %<enum%> "
5115 else if (declspecs
->thread_p
)
5117 error ("%qs in empty declaration with %<enum%> underlying type",
5118 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
5121 else if (declspecs
->const_p
5122 || declspecs
->volatile_p
5123 || declspecs
->atomic_p
5124 || declspecs
->restrict_p
5125 || declspecs
->address_space
)
5127 error ("type qualifier in empty declaration with %<enum%> "
5131 else if (declspecs
->alignas_p
)
5133 error ("%<alignas%> in empty declaration with %<enum%> "
5139 if (!warned
&& !in_system_header_at (input_location
)
5140 && declspecs
->storage_class
!= csc_none
)
5142 warning (0, "useless storage class specifier in empty declaration");
5146 if (!warned
&& !in_system_header_at (input_location
) && declspecs
->thread_p
)
5148 warning (0, "useless %qs in empty declaration",
5149 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
5154 && !in_system_header_at (input_location
)
5155 && (declspecs
->const_p
5156 || declspecs
->volatile_p
5157 || declspecs
->atomic_p
5158 || declspecs
->restrict_p
5159 || declspecs
->address_space
))
5161 warning (0, "useless type qualifier in empty declaration");
5165 if (!warned
&& !in_system_header_at (input_location
)
5166 && declspecs
->alignas_p
)
5168 warning (0, "useless %<_Alignas%> in empty declaration");
5174 && (declspecs
->typespec_kind
== ctsk_tagref_attrs
5175 || declspecs
->typespec_kind
== ctsk_tagfirstref_attrs
))
5177 /* Standard attributes after the "struct" or "union" keyword are
5178 only permitted when the contents of the type are defined, or
5179 in the form "struct-or-union attribute-specifier-sequence
5180 identifier;". If the ';' was not present, attributes were
5181 diagnosed in the parser. Here, ensure that any other useless
5182 elements of the declaration result in a pedwarn, not just a
5183 warning. Forward declarations of enum types are not part of
5184 standard C, but handle them the same. */
5185 pedwarn (input_location
, 0,
5186 "invalid use of attributes in empty declaration");
5192 if (declspecs
->declspecs_seen_p
5193 && !declspecs
->non_std_attrs_seen_p
)
5194 /* An attribute declaration (but not a fallthrough attribute
5195 declaration, which was handled separately); warn if there
5196 are any attributes being ignored (but not if the attributes
5198 c_warn_unused_attributes (declspecs
->attrs
);
5199 else if (!found_tag
)
5200 pedwarn (input_location
, 0, "empty declaration");
5205 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
5206 bits. SPECS represents declaration specifiers that the grammar
5207 only permits to contain type qualifiers and attributes. */
5210 quals_from_declspecs (const struct c_declspecs
*specs
)
5212 int quals
= ((specs
->const_p
? TYPE_QUAL_CONST
: 0)
5213 | (specs
->volatile_p
? TYPE_QUAL_VOLATILE
: 0)
5214 | (specs
->restrict_p
? TYPE_QUAL_RESTRICT
: 0)
5215 | (specs
->atomic_p
? TYPE_QUAL_ATOMIC
: 0)
5216 | (ENCODE_QUAL_ADDR_SPACE (specs
->address_space
)));
5217 gcc_assert (!specs
->type
5218 && !specs
->decl_attr
5219 && specs
->typespec_word
== cts_none
5220 && specs
->storage_class
== csc_none
5221 && !specs
->typedef_p
5222 && !specs
->explicit_signed_p
5223 && !specs
->deprecated_p
5224 && !specs
->unavailable_p
5226 && !specs
->long_long_p
5229 && !specs
->unsigned_p
5230 && !specs
->complex_p
5232 && !specs
->noreturn_p
5233 && !specs
->thread_p
);
5237 /* Construct an array declarator. LOC is the location of the
5238 beginning of the array (usually the opening brace). EXPR is the
5239 expression inside [], or NULL_TREE. QUALS are the type qualifiers
5240 inside the [] (to be applied to the pointer to which a parameter
5241 array is converted). STATIC_P is true if "static" is inside the
5242 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
5243 VLA of unspecified length which is nevertheless a complete type,
5244 false otherwise. The field for the contained declarator is left to
5245 be filled in by set_array_declarator_inner. */
5247 struct c_declarator
*
5248 build_array_declarator (location_t loc
,
5249 tree expr
, struct c_declspecs
*quals
, bool static_p
,
5252 struct c_declarator
*declarator
= XOBNEW (&parser_obstack
,
5253 struct c_declarator
);
5254 declarator
->id_loc
= loc
;
5255 declarator
->kind
= cdk_array
;
5256 declarator
->declarator
= 0;
5257 declarator
->u
.array
.dimen
= expr
;
5260 declarator
->u
.array
.attrs
= quals
->attrs
;
5261 declarator
->u
.array
.quals
= quals_from_declspecs (quals
);
5265 declarator
->u
.array
.attrs
= NULL_TREE
;
5266 declarator
->u
.array
.quals
= 0;
5268 declarator
->u
.array
.static_p
= static_p
;
5269 declarator
->u
.array
.vla_unspec_p
= vla_unspec_p
;
5270 if (static_p
|| quals
!= NULL
)
5271 pedwarn_c90 (loc
, OPT_Wpedantic
,
5272 "ISO C90 does not support %<static%> or type "
5273 "qualifiers in parameter array declarators");
5275 pedwarn_c90 (loc
, OPT_Wpedantic
,
5276 "ISO C90 does not support %<[*]%> array declarators");
5279 if (!current_scope
->parm_flag
)
5282 error_at (loc
, "%<[*]%> not allowed in other than "
5283 "function prototype scope");
5284 declarator
->u
.array
.vla_unspec_p
= false;
5287 current_scope
->had_vla_unspec
= true;
5292 /* Set the contained declarator of an array declarator. DECL is the
5293 declarator, as constructed by build_array_declarator; INNER is what
5294 appears on the left of the []. */
5296 struct c_declarator
*
5297 set_array_declarator_inner (struct c_declarator
*decl
,
5298 struct c_declarator
*inner
)
5300 decl
->declarator
= inner
;
5304 /* Determine whether TYPE is a ISO C99 flexible array memeber type "[]". */
5306 flexible_array_member_type_p (const_tree type
)
5308 if (TREE_CODE (type
) == ARRAY_TYPE
5309 && TYPE_SIZE (type
) == NULL_TREE
5310 && TYPE_DOMAIN (type
) != NULL_TREE
5311 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
)
5317 /* Determine whether TYPE is a one-element array type "[1]". */
5319 one_element_array_type_p (const_tree type
)
5321 if (TREE_CODE (type
) != ARRAY_TYPE
)
5323 return integer_zerop (array_type_nelts (type
));
5326 /* Determine whether TYPE is a zero-length array type "[0]". */
5328 zero_length_array_type_p (const_tree type
)
5330 if (TREE_CODE (type
) == ARRAY_TYPE
)
5331 if (tree type_size
= TYPE_SIZE_UNIT (type
))
5332 if ((integer_zerop (type_size
))
5333 && TYPE_DOMAIN (type
) != NULL_TREE
5334 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
)
5339 /* INIT is a constructor that forms DECL's initializer. If the final
5340 element initializes a flexible array field, add the size of that
5341 initializer to DECL's size. */
5344 add_flexible_array_elts_to_size (tree decl
, tree init
)
5348 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
5351 elt
= CONSTRUCTOR_ELTS (init
)->last ().value
;
5352 type
= TREE_TYPE (elt
);
5353 if (flexible_array_member_type_p (type
))
5355 complete_array_type (&type
, elt
, false);
5357 = size_binop (PLUS_EXPR
, DECL_SIZE (decl
), TYPE_SIZE (type
));
5358 DECL_SIZE_UNIT (decl
)
5359 = size_binop (PLUS_EXPR
, DECL_SIZE_UNIT (decl
), TYPE_SIZE_UNIT (type
));
5363 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
5364 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
5365 before the type name, and set *EXPR_CONST_OPERANDS, if
5366 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
5367 appear in a constant expression. */
5370 groktypename (struct c_type_name
*type_name
, tree
*expr
,
5371 bool *expr_const_operands
)
5374 tree attrs
= type_name
->specs
->attrs
;
5376 type_name
->specs
->attrs
= NULL_TREE
;
5378 type
= grokdeclarator (type_name
->declarator
, type_name
->specs
, TYPENAME
,
5379 false, NULL
, &attrs
, expr
, expr_const_operands
,
5382 /* Apply attributes. */
5383 attrs
= c_warn_type_attributes (attrs
);
5384 decl_attributes (&type
, attrs
, 0);
5389 /* Looks up the most recent pushed declaration corresponding to DECL. */
5392 lookup_last_decl (tree decl
)
5394 tree last_decl
= lookup_name (DECL_NAME (decl
));
5396 last_decl
= lookup_name_in_scope (DECL_NAME (decl
), external_scope
);
5400 /* Wrapper for decl_attributes that adds some implicit attributes
5401 to VAR_DECLs or FUNCTION_DECLs. */
5404 c_decl_attributes (tree
*node
, tree attributes
, int flags
)
5406 /* Add implicit "omp declare target" attribute if requested. */
5407 if (vec_safe_length (current_omp_declare_target_attribute
)
5408 && ((VAR_P (*node
) && is_global_var (*node
))
5409 || TREE_CODE (*node
) == FUNCTION_DECL
))
5411 if (VAR_P (*node
) && !omp_mappable_type (TREE_TYPE (*node
)))
5412 attributes
= tree_cons (get_identifier ("omp declare target implicit"),
5413 NULL_TREE
, attributes
);
5416 attributes
= tree_cons (get_identifier ("omp declare target"),
5417 NULL_TREE
, attributes
);
5418 attributes
= tree_cons (get_identifier ("omp declare target block"),
5419 NULL_TREE
, attributes
);
5421 if (TREE_CODE (*node
) == FUNCTION_DECL
)
5424 = current_omp_declare_target_attribute
->last ().device_type
;
5425 device_type
= MAX (device_type
, 0);
5426 if ((device_type
& OMP_CLAUSE_DEVICE_TYPE_HOST
) != 0
5427 && !lookup_attribute ("omp declare target host", attributes
))
5429 = tree_cons (get_identifier ("omp declare target host"),
5430 NULL_TREE
, attributes
);
5431 if ((device_type
& OMP_CLAUSE_DEVICE_TYPE_NOHOST
) != 0
5432 && !lookup_attribute ("omp declare target nohost", attributes
))
5434 = tree_cons (get_identifier ("omp declare target nohost"),
5435 NULL_TREE
, attributes
);
5438 = current_omp_declare_target_attribute
->last ().indirect
;
5439 if (indirect
&& !lookup_attribute ("omp declare target indirect",
5442 = tree_cons (get_identifier ("omp declare target indirect"),
5443 NULL_TREE
, attributes
);
5447 if (flag_openmp
|| flag_openmp_simd
)
5449 bool diagnosed
= false;
5450 for (tree
*pa
= &attributes
; *pa
; )
5452 if (is_attribute_namespace_p ("omp", *pa
))
5454 tree name
= get_attribute_name (*pa
);
5455 if (is_attribute_p ("directive", name
)
5456 || is_attribute_p ("sequence", name
)
5457 || is_attribute_p ("decl", name
))
5459 const char *p
= NULL
;
5460 if (TREE_VALUE (*pa
) == NULL_TREE
)
5461 p
= IDENTIFIER_POINTER (name
);
5462 for (tree a
= TREE_VALUE (*pa
); a
; a
= TREE_CHAIN (a
))
5464 tree d
= TREE_VALUE (a
);
5465 gcc_assert (TREE_CODE (d
) == C_TOKEN_VEC
);
5468 || TREE_CODE (*node
) == FUNCTION_DECL
)
5469 && c_maybe_parse_omp_decl (*node
, d
))
5471 p
= TREE_PUBLIC (d
) ? "decl" : "directive";
5473 if (p
&& !diagnosed
)
5475 error ("%<omp::%s%> not allowed to be specified in "
5481 *pa
= TREE_CHAIN (*pa
);
5486 pa
= &TREE_CHAIN (*pa
);
5490 /* Look up the current declaration with all the attributes merged
5491 so far so that attributes on the current declaration that's
5492 about to be pushed that conflict with the former can be detected,
5493 diagnosed, and rejected as appropriate. */
5494 tree last_decl
= lookup_last_decl (*node
);
5495 return decl_attributes (node
, attributes
, flags
, last_decl
);
5499 /* Decode a declarator in an ordinary declaration or data definition.
5500 This is called as soon as the type information and variable name
5501 have been parsed, before parsing the initializer if any.
5502 Here we create the ..._DECL node, fill in its type,
5503 and (if DO_PUSH) put it on the list of decls for the current context.
5504 When nonnull, set *LASTLOC to the location of the prior declaration
5505 of the same entity if one exists.
5506 The ..._DECL node is returned as the value.
5508 Exception: for arrays where the length is not specified,
5509 the type is left null, to be filled in by `finish_decl'.
5511 Function definitions do not come here; they go to start_function
5512 instead. However, external and forward declarations of functions
5513 do go through here. Structure field declarations are done by
5514 grokfield and not through here. */
5517 start_decl (struct c_declarator
*declarator
, struct c_declspecs
*declspecs
,
5518 bool initialized
, tree attributes
, bool do_push
/* = true */,
5519 location_t
*lastloc
/* = NULL */)
5524 tree expr
= NULL_TREE
;
5525 enum deprecated_states deprecated_state
= DEPRECATED_NORMAL
;
5527 /* An object declared as __attribute__((unavailable)) suppresses
5528 warnings and errors from __attribute__((deprecated/unavailable))
5530 An object declared as __attribute__((deprecated)) suppresses
5531 warnings of uses of other deprecated items. */
5532 if (lookup_attribute ("unavailable", attributes
))
5533 deprecated_state
= UNAVAILABLE_DEPRECATED_SUPPRESS
;
5534 else if (lookup_attribute ("deprecated", attributes
))
5535 deprecated_state
= DEPRECATED_SUPPRESS
;
5537 decl
= grokdeclarator (declarator
, declspecs
,
5538 NORMAL
, initialized
, NULL
, &attributes
, &expr
, NULL
,
5540 if (!decl
|| decl
== error_mark_node
)
5543 old_decl
= lookup_last_decl (decl
);
5545 if (tree lastdecl
= lastloc
? old_decl
: NULL_TREE
)
5546 if (lastdecl
!= error_mark_node
)
5547 *lastloc
= DECL_SOURCE_LOCATION (lastdecl
);
5549 /* Make sure the size expression is evaluated at this point. */
5550 if (expr
&& !current_scope
->parm_flag
)
5551 add_stmt (fold_convert (void_type_node
, expr
));
5553 if (TREE_CODE (decl
) != FUNCTION_DECL
&& MAIN_NAME_P (DECL_NAME (decl
))
5554 && TREE_PUBLIC (decl
))
5555 warning (OPT_Wmain
, "%q+D is usually a function", decl
);
5557 if (warn_missing_variable_declarations
&& VAR_P (decl
)
5558 && !DECL_EXTERNAL (decl
) && TREE_PUBLIC (decl
) && old_decl
== NULL_TREE
)
5559 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wmissing_variable_declarations
,
5560 "no previous declaration for %qD", decl
);
5563 /* Is it valid for this decl to have an initializer at all?
5564 If not, set INITIALIZED to zero, which will indirectly
5565 tell 'finish_decl' to ignore the initializer once it is parsed. */
5566 switch (TREE_CODE (decl
))
5569 error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl
);
5570 initialized
= false;
5574 error ("function %qD is initialized like a variable", decl
);
5575 initialized
= false;
5579 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5580 error ("parameter %qD is initialized", decl
);
5581 initialized
= false;
5585 /* Don't allow initializations for incomplete types except for
5586 arrays which might be completed by the initialization. */
5588 /* This can happen if the array size is an undefined macro.
5589 We already gave a warning, so we don't need another one. */
5590 if (TREE_TYPE (decl
) == error_mark_node
)
5591 initialized
= false;
5592 else if (COMPLETE_TYPE_P (TREE_TYPE (decl
)))
5594 /* A complete type is ok if size is fixed. If the size is
5595 variable, an empty initializer is OK and nonempty
5596 initializers will be diagnosed in the parser. */
5598 else if (TREE_CODE (TREE_TYPE (decl
)) != ARRAY_TYPE
)
5600 error ("variable %qD has initializer but incomplete type", decl
);
5601 initialized
= false;
5607 if (current_scope
== file_scope
)
5608 TREE_STATIC (decl
) = 1;
5610 /* Tell 'pushdecl' this is an initialized decl
5611 even though we don't yet have the initializer expression.
5612 Also tell 'finish_decl' it may store the real initializer. */
5613 DECL_INITIAL (decl
) = error_mark_node
;
5616 /* If this is a function declaration, write a record describing it to the
5617 prototypes file (if requested). */
5619 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5620 gen_aux_info_record (decl
, 0, 0, prototype_p (TREE_TYPE (decl
)));
5622 /* ANSI specifies that a tentative definition which is not merged with
5623 a non-tentative definition behaves exactly like a definition with an
5624 initializer equal to zero. (Section 3.7.2)
5626 -fno-common gives strict ANSI behavior, though this tends to break
5627 a large body of code that grew up without this rule.
5629 Thread-local variables are never common, since there's no entrenched
5630 body of code to break, and it allows more efficient variable references
5631 in the presence of dynamic linking. */
5635 && TREE_PUBLIC (decl
)
5636 && !DECL_THREAD_LOCAL_P (decl
)
5638 DECL_COMMON (decl
) = 1;
5640 /* Set attributes here so if duplicate decl, will have proper attributes. */
5641 c_decl_attributes (&decl
, attributes
, 0);
5643 /* Handle gnu_inline attribute. */
5644 if (declspecs
->inline_p
5645 && !flag_gnu89_inline
5646 && TREE_CODE (decl
) == FUNCTION_DECL
5647 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl
))
5648 || current_function_decl
))
5650 if (declspecs
->storage_class
== csc_auto
&& current_scope
!= file_scope
)
5652 else if (declspecs
->storage_class
!= csc_static
)
5653 DECL_EXTERNAL (decl
) = !DECL_EXTERNAL (decl
);
5656 if (TREE_CODE (decl
) == FUNCTION_DECL
5657 && targetm
.calls
.promote_prototypes (TREE_TYPE (decl
)))
5659 struct c_declarator
*ce
= declarator
;
5661 if (ce
->kind
== cdk_pointer
)
5662 ce
= declarator
->declarator
;
5663 if (ce
->kind
== cdk_function
)
5665 tree args
= ce
->u
.arg_info
->parms
;
5666 for (; args
; args
= DECL_CHAIN (args
))
5668 tree type
= TREE_TYPE (args
);
5669 if (type
&& INTEGRAL_TYPE_P (type
)
5670 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
5671 DECL_ARG_TYPE (args
) = c_type_promotes_to (type
);
5676 if (TREE_CODE (decl
) == FUNCTION_DECL
5677 && DECL_DECLARED_INLINE_P (decl
)
5678 && DECL_UNINLINABLE (decl
)
5679 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl
)))
5680 warning (OPT_Wattributes
, "inline function %q+D given attribute %qs",
5683 /* C99 6.7.4p3: An inline definition of a function with external
5684 linkage shall not contain a definition of a modifiable object
5685 with static storage duration... */
5687 && current_scope
!= file_scope
5688 && TREE_STATIC (decl
)
5689 && !TREE_READONLY (decl
)
5690 && DECL_DECLARED_INLINE_P (current_function_decl
)
5691 && DECL_EXTERNAL (current_function_decl
))
5692 record_inline_static (input_location
, current_function_decl
,
5693 decl
, csi_modifiable
);
5695 if (c_dialect_objc ()
5696 && VAR_OR_FUNCTION_DECL_P (decl
))
5697 objc_check_global_decl (decl
);
5699 /* Add this decl to the current scope.
5700 TEM may equal DECL or it may be a previous decl of the same name. */
5703 tem
= pushdecl (decl
);
5705 if (initialized
&& DECL_EXTERNAL (tem
))
5707 DECL_EXTERNAL (tem
) = 0;
5708 TREE_STATIC (tem
) = 1;
5717 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5718 DECL or the non-array element type if DECL is an uninitialized array.
5719 If that type has a const member, diagnose this. */
5722 diagnose_uninitialized_cst_member (tree decl
, tree type
)
5725 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
5728 if (TREE_CODE (field
) != FIELD_DECL
)
5730 field_type
= strip_array_types (TREE_TYPE (field
));
5732 if (TYPE_QUALS (field_type
) & TYPE_QUAL_CONST
)
5734 auto_diagnostic_group d
;
5735 if (warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
5736 "uninitialized const member in %qT is invalid in C++",
5737 strip_array_types (TREE_TYPE (decl
))))
5738 inform (DECL_SOURCE_LOCATION (field
), "%qD should be initialized", field
);
5741 if (RECORD_OR_UNION_TYPE_P (field_type
))
5742 diagnose_uninitialized_cst_member (decl
, field_type
);
5746 /* Finish processing of a declaration;
5747 install its initial value.
5748 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5749 If the length of an array type is not known before,
5750 it must be determined now, from the initial value, or it is an error.
5752 INIT_LOC is the location of the initial value. */
5755 finish_decl (tree decl
, location_t init_loc
, tree init
,
5756 tree origtype
, tree asmspec_tree
)
5759 bool was_incomplete
= (DECL_SIZE (decl
) == NULL_TREE
);
5760 const char *asmspec
= 0;
5762 /* If a name was specified, get the string. */
5763 if (VAR_OR_FUNCTION_DECL_P (decl
)
5764 && DECL_FILE_SCOPE_P (decl
))
5765 asmspec_tree
= maybe_apply_renaming_pragma (decl
, asmspec_tree
);
5767 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
5770 && TREE_STATIC (decl
)
5771 && global_bindings_p ())
5772 /* So decl is a global variable. Record the types it uses
5773 so that we can decide later to emit debug info for them. */
5774 record_types_used_by_current_var_decl (decl
);
5776 /* If `start_decl' didn't like having an initialization, ignore it now. */
5777 if (init
!= NULL_TREE
&& DECL_INITIAL (decl
) == NULL_TREE
)
5780 /* Don't crash if parm is initialized. */
5781 if (TREE_CODE (decl
) == PARM_DECL
)
5785 store_init_value (init_loc
, decl
, init
, origtype
);
5787 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl
)
5788 || TREE_CODE (decl
) == FIELD_DECL
))
5789 objc_check_decl (decl
);
5791 type
= TREE_TYPE (decl
);
5793 /* Deduce size of array from initialization, if not already known.
5794 This is only needed for an initialization in the current scope;
5795 it must not be done for a file-scope initialization of a
5796 declaration with external linkage, redeclared in an inner scope
5797 with the outer declaration shadowed in an intermediate scope. */
5798 if (TREE_CODE (type
) == ARRAY_TYPE
5799 && TYPE_DOMAIN (type
) == NULL_TREE
5800 && TREE_CODE (decl
) != TYPE_DECL
5801 && !(TREE_PUBLIC (decl
) && current_scope
!= file_scope
))
5804 = (TREE_STATIC (decl
)
5805 /* Even if pedantic, an external linkage array
5806 may have incomplete type at first. */
5807 ? pedantic
&& !TREE_PUBLIC (decl
)
5808 : !DECL_EXTERNAL (decl
));
5810 = complete_array_type (&TREE_TYPE (decl
), DECL_INITIAL (decl
),
5813 /* Get the completed type made by complete_array_type. */
5814 type
= TREE_TYPE (decl
);
5819 error ("initializer fails to determine size of %q+D", decl
);
5824 error ("array size missing in %q+D", decl
);
5828 error ("zero or negative size array %q+D", decl
);
5832 /* For global variables, update the copy of the type that
5833 exists in the binding. */
5834 if (TREE_PUBLIC (decl
))
5836 struct c_binding
*b_ext
= I_SYMBOL_BINDING (DECL_NAME (decl
));
5837 while (b_ext
&& !B_IN_EXTERNAL_SCOPE (b_ext
))
5838 b_ext
= b_ext
->shadowed
;
5839 if (b_ext
&& TREE_CODE (decl
) == TREE_CODE (b_ext
->decl
))
5841 if (b_ext
->u
.type
&& comptypes (b_ext
->u
.type
, type
))
5842 b_ext
->u
.type
= composite_type (b_ext
->u
.type
, type
);
5844 b_ext
->u
.type
= type
;
5853 if (DECL_INITIAL (decl
) && DECL_INITIAL (decl
) != error_mark_node
)
5854 TREE_TYPE (DECL_INITIAL (decl
)) = type
;
5856 relayout_decl (decl
);
5859 /* Look for braced array initializers for character arrays and
5860 recursively convert them into STRING_CSTs. */
5861 if (tree init
= DECL_INITIAL (decl
))
5862 DECL_INITIAL (decl
) = braced_lists_to_strings (type
, init
);
5866 if (init
&& TREE_CODE (init
) == CONSTRUCTOR
)
5867 add_flexible_array_elts_to_size (decl
, init
);
5869 complete_flexible_array_elts (DECL_INITIAL (decl
));
5871 if (is_global_var (decl
))
5873 type_context_kind context
= (DECL_THREAD_LOCAL_P (decl
)
5874 ? TCTX_THREAD_STORAGE
5875 : TCTX_STATIC_STORAGE
);
5876 if (!verify_type_context (input_location
, context
, TREE_TYPE (decl
)))
5877 TREE_TYPE (decl
) = error_mark_node
;
5880 if (DECL_SIZE (decl
) == NULL_TREE
&& TREE_TYPE (decl
) != error_mark_node
5881 && COMPLETE_TYPE_P (TREE_TYPE (decl
)))
5882 layout_decl (decl
, 0);
5884 if (DECL_SIZE (decl
) == NULL_TREE
5885 /* Don't give an error if we already gave one earlier. */
5886 && TREE_TYPE (decl
) != error_mark_node
5887 && (TREE_STATIC (decl
)
5888 /* A static variable with an incomplete type
5889 is an error if it is initialized.
5890 Also if it is not file scope.
5891 Also if it is thread-local (in C23).
5892 Otherwise, let it through, but if it is not `extern'
5893 then it may cause an error message later. */
5894 ? (DECL_INITIAL (decl
) != NULL_TREE
5895 || !DECL_FILE_SCOPE_P (decl
)
5896 || (flag_isoc23
&& DECL_THREAD_LOCAL_P (decl
)))
5897 /* An automatic variable with an incomplete type
5899 : !DECL_EXTERNAL (decl
)))
5901 error ("storage size of %q+D isn%'t known", decl
);
5902 TREE_TYPE (decl
) = error_mark_node
;
5905 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
))
5906 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
5907 && DECL_SIZE (decl
) == NULL_TREE
5908 && TREE_STATIC (decl
))
5909 incomplete_record_decls
.safe_push (decl
);
5911 if (is_global_var (decl
)
5912 && DECL_SIZE (decl
) != NULL_TREE
5913 && TREE_TYPE (decl
) != error_mark_node
)
5915 if (TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
)
5916 constant_expression_warning (DECL_SIZE (decl
));
5919 error ("storage size of %q+D isn%'t constant", decl
);
5920 TREE_TYPE (decl
) = error_mark_node
;
5924 if (TREE_USED (type
))
5926 TREE_USED (decl
) = 1;
5927 DECL_READ_P (decl
) = 1;
5931 /* If this is a function and an assembler name is specified, reset DECL_RTL
5932 so we can give it its new name. Also, update builtin_decl if it
5933 was a normal built-in. */
5934 if (TREE_CODE (decl
) == FUNCTION_DECL
&& asmspec
)
5936 if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
5937 set_builtin_user_assembler_name (decl
, asmspec
);
5938 set_user_assembler_name (decl
, asmspec
);
5941 /* If #pragma weak was used, mark the decl weak now. */
5942 maybe_apply_pragma_weak (decl
);
5944 /* Output the assembler code and/or RTL code for variables and functions,
5945 unless the type is an undefined structure or union.
5946 If not, it will get done when the type is completed. */
5948 if (VAR_OR_FUNCTION_DECL_P (decl
))
5950 /* Determine the ELF visibility. */
5951 if (TREE_PUBLIC (decl
))
5952 c_determine_visibility (decl
);
5954 /* This is a no-op in c-lang.cc or something real in objc-act.cc. */
5955 if (c_dialect_objc ())
5956 objc_check_decl (decl
);
5960 /* If this is not a static variable, issue a warning.
5961 It doesn't make any sense to give an ASMSPEC for an
5962 ordinary, non-register local variable. Historically,
5963 GCC has accepted -- but ignored -- the ASMSPEC in
5965 if (!DECL_FILE_SCOPE_P (decl
)
5967 && !C_DECL_REGISTER (decl
)
5968 && !TREE_STATIC (decl
))
5969 warning (0, "ignoring %<asm%> specifier for non-static local "
5970 "variable %q+D", decl
);
5972 set_user_assembler_name (decl
, asmspec
);
5975 if (DECL_FILE_SCOPE_P (decl
))
5977 if (DECL_INITIAL (decl
) == NULL_TREE
5978 || DECL_INITIAL (decl
) == error_mark_node
)
5979 /* Don't output anything
5980 when a tentative file-scope definition is seen.
5981 But at end of compilation, do output code for them. */
5982 DECL_DEFER_OUTPUT (decl
) = 1;
5983 if (asmspec
&& VAR_P (decl
) && C_DECL_REGISTER (decl
))
5984 DECL_HARD_REGISTER (decl
) = 1;
5985 rest_of_decl_compilation (decl
, true, 0);
5987 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5989 tree parms
= DECL_ARGUMENTS (decl
);
5990 const bool builtin
= fndecl_built_in_p (decl
);
5991 if (tree access
= build_attr_access_from_parms (parms
, !builtin
))
5992 decl_attributes (&decl
, access
, 0);
5997 /* In conjunction with an ASMSPEC, the `register'
5998 keyword indicates that we should place the variable
5999 in a particular register. */
6000 if (asmspec
&& C_DECL_REGISTER (decl
))
6002 DECL_HARD_REGISTER (decl
) = 1;
6003 /* This cannot be done for a structure with volatile
6004 fields, on which DECL_REGISTER will have been
6006 if (!DECL_REGISTER (decl
))
6007 error ("cannot put object with volatile field into register");
6010 if (TREE_CODE (decl
) != FUNCTION_DECL
)
6012 /* If we're building a variable sized type, and we might be
6013 reachable other than via the top of the current binding
6014 level, then create a new BIND_EXPR so that we deallocate
6015 the object at the right time. */
6016 /* Note that DECL_SIZE can be null due to errors. */
6017 if (DECL_SIZE (decl
)
6018 && !TREE_CONSTANT (DECL_SIZE (decl
))
6019 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list
))
6022 bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, NULL
);
6023 TREE_SIDE_EFFECTS (bind
) = 1;
6025 BIND_EXPR_BODY (bind
) = push_stmt_list ();
6027 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl
),
6033 if (!DECL_FILE_SCOPE_P (decl
))
6035 /* Recompute the RTL of a local array now
6036 if it used to be an incomplete type. */
6037 if (was_incomplete
&& !is_global_var (decl
))
6039 /* If we used it already as memory, it must stay in memory. */
6040 TREE_ADDRESSABLE (decl
) = TREE_USED (decl
);
6041 /* If it's still incomplete now, no init will save it. */
6042 if (DECL_SIZE (decl
) == NULL_TREE
)
6043 DECL_INITIAL (decl
) = NULL_TREE
;
6048 if (TREE_CODE (decl
) == TYPE_DECL
)
6050 if (!DECL_FILE_SCOPE_P (decl
)
6051 && c_type_variably_modified_p (TREE_TYPE (decl
)))
6052 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl
), DECL_EXPR
, decl
));
6054 rest_of_decl_compilation (decl
, DECL_FILE_SCOPE_P (decl
), 0);
6057 /* Install a cleanup (aka destructor) if one was given. */
6058 if (VAR_P (decl
) && !TREE_STATIC (decl
))
6060 tree attr
= lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl
));
6063 tree cleanup_id
= TREE_VALUE (TREE_VALUE (attr
));
6064 tree cleanup_decl
= lookup_name (cleanup_id
);
6066 vec
<tree
, va_gc
> *v
;
6068 /* Build "cleanup(&decl)" for the destructor. */
6069 cleanup
= build_unary_op (input_location
, ADDR_EXPR
, decl
, false);
6071 v
->quick_push (cleanup
);
6072 cleanup
= c_build_function_call_vec (DECL_SOURCE_LOCATION (decl
),
6073 vNULL
, cleanup_decl
, v
, NULL
);
6076 /* Don't warn about decl unused; the cleanup uses it. */
6077 TREE_USED (decl
) = 1;
6078 TREE_USED (cleanup_decl
) = 1;
6079 DECL_READ_P (decl
) = 1;
6081 push_cleanup (decl
, cleanup
, false);
6087 && !DECL_EXTERNAL (decl
)
6088 && DECL_INITIAL (decl
) == NULL_TREE
)
6090 type
= strip_array_types (type
);
6091 if (TREE_READONLY (decl
))
6092 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
6093 "uninitialized %<const %D%> is invalid in C++", decl
);
6094 else if (RECORD_OR_UNION_TYPE_P (type
)
6095 && C_TYPE_FIELDS_READONLY (type
))
6096 diagnose_uninitialized_cst_member (decl
, type
);
6101 && lookup_attribute ("omp declare target implicit",
6102 DECL_ATTRIBUTES (decl
)))
6104 DECL_ATTRIBUTES (decl
)
6105 = remove_attribute ("omp declare target implicit",
6106 DECL_ATTRIBUTES (decl
));
6107 if (!omp_mappable_type (TREE_TYPE (decl
)))
6108 error ("%q+D in declare target directive does not have mappable type",
6110 else if (!lookup_attribute ("omp declare target",
6111 DECL_ATTRIBUTES (decl
))
6112 && !lookup_attribute ("omp declare target link",
6113 DECL_ATTRIBUTES (decl
)))
6115 DECL_ATTRIBUTES (decl
)
6116 = tree_cons (get_identifier ("omp declare target"),
6117 NULL_TREE
, DECL_ATTRIBUTES (decl
));
6118 symtab_node
*node
= symtab_node::get (decl
);
6121 node
->offloadable
= 1;
6122 if (ENABLE_OFFLOADING
)
6124 g
->have_offload
= true;
6125 if (is_a
<varpool_node
*> (node
))
6126 vec_safe_push (offload_vars
, decl
);
6132 /* This is the last point we can lower alignment so give the target the
6135 && !is_global_var (decl
)
6136 && !DECL_HARD_REGISTER (decl
))
6137 targetm
.lower_local_decl_alignment (decl
);
6139 invoke_plugin_callbacks (PLUGIN_FINISH_DECL
, decl
);
6142 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
6143 EXPR is NULL or a pointer to an expression that needs to be
6144 evaluated for the side effects of array size expressions in the
6148 grokparm (const struct c_parm
*parm
, tree
*expr
)
6150 tree attrs
= parm
->attrs
;
6151 tree decl
= grokdeclarator (parm
->declarator
, parm
->specs
, PARM
, false,
6152 NULL
, &attrs
, expr
, NULL
, DEPRECATED_NORMAL
);
6154 decl_attributes (&decl
, attrs
, 0);
6159 /* Return attribute "arg spec" corresponding to an array/VLA parameter
6160 described by PARM, concatenated onto attributes ATTRS.
6161 The spec consists of one dollar symbol for each specified variable
6162 bound, one asterisk for each unspecified variable bound, followed
6163 by at most one specification of the most significant bound of
6164 an ordinary array parameter. For ordinary arrays the specification
6165 is either the constant bound itself, or the space character for
6166 an array with an unspecified bound (the [] form). Finally, a chain
6167 of specified variable bounds is appended to the spec, starting with
6168 the most significant bound. For example, the PARM T a[2][m][3][n]
6169 will produce __attribute__((arg spec ("[$$2]", m, n)).
6170 For T a typedef for an array with variable bounds, the bounds are
6171 included in the specification in the expected order.
6172 No "arg spec" is created for parameters of pointer types, making
6173 a distinction between T(*)[N] (or, equivalently, T[][N]) and
6174 the T[M][N] form, all of which have the same type and are represented
6175 the same, but only the last of which gets an "arg spec" describing
6176 the most significant bound M. */
6179 get_parm_array_spec (const struct c_parm
*parm
, tree attrs
)
6181 /* The attribute specification string, minor bound first. */
6184 /* A list of VLA variable bounds, major first, or null if unspecified
6186 tree vbchain
= NULL_TREE
;
6187 /* True for a pointer parameter. */
6188 bool pointer
= false;
6189 /* True for an ordinary array with an unpecified bound. */
6190 bool nobound
= false;
6192 /* Create a string representation for the bounds of the array/VLA. */
6193 for (c_declarator
*pd
= parm
->declarator
, *next
; pd
; pd
= next
)
6195 next
= pd
->declarator
;
6196 while (next
&& next
->kind
== cdk_attrs
)
6197 next
= next
->declarator
;
6199 /* Remember if a pointer has been seen to avoid storing the constant
6201 if (pd
->kind
== cdk_pointer
)
6204 if ((pd
->kind
== cdk_pointer
|| pd
->kind
== cdk_function
)
6205 && (!next
|| next
->kind
== cdk_id
))
6207 /* Do nothing for the common case of a pointer. The fact that
6208 the parameter is one can be deduced from the absence of
6209 an arg spec for it. */
6213 if (pd
->kind
== cdk_id
)
6216 || !parm
->specs
->type
6217 || TREE_CODE (parm
->specs
->type
) != ARRAY_TYPE
6218 || !TYPE_DOMAIN (parm
->specs
->type
)
6219 || !TYPE_MAX_VALUE (TYPE_DOMAIN (parm
->specs
->type
)))
6222 tree max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
->specs
->type
));
6224 && TREE_CODE (max
) == INTEGER_CST
)
6226 /* Extract the upper bound from a parameter of an array type
6227 unless the parameter is an ordinary array of unspecified
6228 bound in which case a next iteration of the loop will
6230 if (spec
.empty () || spec
.end ()[-1] != ' ')
6232 if (!tree_fits_shwi_p (max
))
6235 /* The upper bound is the value of the largest valid
6237 HOST_WIDE_INT n
= tree_to_shwi (max
) + 1;
6239 sprintf (buf
, "%lu", (unsigned long)n
);
6245 /* For a VLA typedef, create a list of its variable bounds and
6246 append it in the expected order to VBCHAIN. */
6247 tree tpbnds
= NULL_TREE
;
6248 for (tree type
= parm
->specs
->type
; TREE_CODE (type
) == ARRAY_TYPE
;
6249 type
= TREE_TYPE (type
))
6251 tree nelts
= array_type_nelts (type
);
6252 if (error_operand_p (nelts
))
6254 if (TREE_CODE (nelts
) != INTEGER_CST
)
6256 /* Each variable VLA bound is represented by the dollar
6259 tpbnds
= tree_cons (NULL_TREE
, nelts
, tpbnds
);
6262 tpbnds
= nreverse (tpbnds
);
6263 vbchain
= chainon (vbchain
, tpbnds
);
6267 if (pd
->kind
!= cdk_array
)
6270 if (pd
->u
.array
.vla_unspec_p
)
6272 /* Each unspecified bound is represented by a star. There
6273 can be any number of these in a declaration (but none in
6279 tree nelts
= pd
->u
.array
.dimen
;
6282 /* Ordinary array of unspecified size. There can be at most
6283 one for the most significant bound. Exit on the next
6284 iteration which determines whether or not PARM is declared
6285 as a pointer or an array. */
6290 if (pd
->u
.array
.static_p
)
6293 if (!INTEGRAL_TYPE_P (TREE_TYPE (nelts
)))
6294 /* Avoid invalid NELTS. */
6298 nelts
= c_fully_fold (nelts
, false, nullptr);
6299 if (TREE_CODE (nelts
) == INTEGER_CST
)
6301 /* Skip all constant bounds except the most significant one.
6302 The interior ones are included in the array type. */
6303 if (next
&& (next
->kind
== cdk_array
|| next
->kind
== cdk_pointer
))
6306 if (!tree_fits_uhwi_p (nelts
))
6307 /* Bail completely on invalid bounds. */
6311 unsigned HOST_WIDE_INT n
= tree_to_uhwi (nelts
);
6312 sprintf (buf
, "%llu", (unsigned long long)n
);
6317 /* Each variable VLA bound is represented by a dollar sign. */
6319 vbchain
= tree_cons (NULL_TREE
, nelts
, vbchain
);
6322 if (spec
.empty () && !nobound
)
6325 spec
.insert (0, "[");
6327 /* Ordinary array of unspecified bound is represented by a space.
6328 It must be last in the spec. */
6332 tree acsstr
= build_string (spec
.length () + 1, spec
.c_str ());
6333 tree args
= tree_cons (NULL_TREE
, acsstr
, vbchain
);
6334 tree name
= get_identifier ("arg spec");
6335 return tree_cons (name
, args
, attrs
);
6338 /* Given a parsed parameter declaration, decode it into a PARM_DECL
6339 and push that on the current scope. EXPR is a pointer to an
6340 expression that needs to be evaluated for the side effects of array
6341 size expressions in the parameters. */
6344 push_parm_decl (const struct c_parm
*parm
, tree
*expr
)
6346 tree attrs
= parm
->attrs
;
6347 tree decl
= grokdeclarator (parm
->declarator
, parm
->specs
, PARM
, false, NULL
,
6348 &attrs
, expr
, NULL
, DEPRECATED_NORMAL
);
6349 if (decl
&& DECL_P (decl
))
6350 DECL_SOURCE_LOCATION (decl
) = parm
->loc
;
6352 attrs
= get_parm_array_spec (parm
, attrs
);
6353 decl_attributes (&decl
, attrs
, 0);
6355 decl
= pushdecl (decl
);
6357 finish_decl (decl
, input_location
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
6360 /* Mark all the parameter declarations to date as forward decls.
6361 Also diagnose use of this extension. */
6364 mark_forward_parm_decls (void)
6366 struct c_binding
*b
;
6368 if (pedantic
&& !current_scope
->warned_forward_parm_decls
)
6370 pedwarn (input_location
, OPT_Wpedantic
,
6371 "ISO C forbids forward parameter declarations");
6372 current_scope
->warned_forward_parm_decls
= true;
6375 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
6376 if (TREE_CODE (b
->decl
) == PARM_DECL
)
6377 TREE_ASM_WRITTEN (b
->decl
) = 1;
6380 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
6381 literal, which may be an incomplete array type completed by the
6382 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
6383 literal. NON_CONST is true if the initializers contain something
6384 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
6385 it is the (valid) alignment for this compound literal, as specified
6386 with _Alignas. SCSPECS are the storage class specifiers (C23) from the
6387 compound literal. */
6390 build_compound_literal (location_t loc
, tree type
, tree init
, bool non_const
,
6391 unsigned int alignas_align
,
6392 struct c_declspecs
*scspecs
)
6394 /* We do not use start_decl here because we have a type, not a declarator;
6395 and do not use finish_decl because the decl should be stored inside
6396 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
6400 bool threadp
= scspecs
? scspecs
->thread_p
: false;
6401 enum c_storage_class storage_class
= (scspecs
6402 ? scspecs
->storage_class
6405 if (type
== error_mark_node
6406 || init
== error_mark_node
)
6407 return error_mark_node
;
6409 if (current_scope
== file_scope
&& storage_class
== csc_register
)
6411 error_at (loc
, "file-scope compound literal specifies %<register%>");
6412 storage_class
= csc_none
;
6415 if (current_scope
!= file_scope
&& threadp
&& storage_class
== csc_none
)
6417 error_at (loc
, "compound literal implicitly auto and declared %qs",
6418 scspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
6422 decl
= build_decl (loc
, VAR_DECL
, NULL_TREE
, type
);
6423 DECL_EXTERNAL (decl
) = 0;
6424 TREE_PUBLIC (decl
) = 0;
6425 TREE_STATIC (decl
) = (current_scope
== file_scope
6426 || storage_class
== csc_static
);
6427 DECL_CONTEXT (decl
) = current_function_decl
;
6428 TREE_USED (decl
) = 1;
6429 DECL_READ_P (decl
) = 1;
6430 DECL_ARTIFICIAL (decl
) = 1;
6431 DECL_IGNORED_P (decl
) = 1;
6432 C_DECL_COMPOUND_LITERAL_P (decl
) = 1;
6433 C_DECL_DECLARED_CONSTEXPR (decl
) = scspecs
&& scspecs
->constexpr_p
;
6434 TREE_TYPE (decl
) = type
;
6436 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
6437 if (storage_class
== csc_register
)
6439 C_DECL_REGISTER (decl
) = 1;
6440 DECL_REGISTER (decl
) = 1;
6442 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type
)), decl
);
6445 SET_DECL_ALIGN (decl
, alignas_align
* BITS_PER_UNIT
);
6446 DECL_USER_ALIGN (decl
) = 1;
6448 store_init_value (loc
, decl
, init
, NULL_TREE
);
6449 if (current_scope
!= file_scope
6450 && TREE_STATIC (decl
)
6451 && !TREE_READONLY (decl
)
6452 && DECL_DECLARED_INLINE_P (current_function_decl
)
6453 && DECL_EXTERNAL (current_function_decl
))
6454 record_inline_static (input_location
, current_function_decl
,
6455 decl
, csi_modifiable
);
6457 if (TREE_CODE (type
) == ARRAY_TYPE
&& !COMPLETE_TYPE_P (type
))
6459 int failure
= complete_array_type (&TREE_TYPE (decl
),
6460 DECL_INITIAL (decl
), true);
6461 /* If complete_array_type returns 3, it means that the
6462 initial value of the compound literal is empty. Allow it. */
6463 gcc_assert (failure
== 0 || failure
== 3);
6465 type
= TREE_TYPE (decl
);
6466 TREE_TYPE (DECL_INITIAL (decl
)) = type
;
6469 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
6471 c_incomplete_type_error (loc
, NULL_TREE
, type
);
6472 return error_mark_node
;
6475 if (TREE_STATIC (decl
)
6476 && !verify_type_context (loc
, TCTX_STATIC_STORAGE
, type
))
6477 return error_mark_node
;
6479 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), DECL_EXPR
, decl
);
6480 complit
= build1 (COMPOUND_LITERAL_EXPR
, type
, stmt
);
6481 TREE_SIDE_EFFECTS (complit
) = 1;
6483 layout_decl (decl
, 0);
6485 if (TREE_STATIC (decl
))
6487 /* This decl needs a name for the assembler output. */
6488 set_compound_literal_name (decl
);
6489 DECL_DEFER_OUTPUT (decl
) = 1;
6490 DECL_COMDAT (decl
) = 1;
6492 rest_of_decl_compilation (decl
, 1, 0);
6494 else if (current_function_decl
&& !current_scope
->parm_flag
)
6499 complit
= build2 (C_MAYBE_CONST_EXPR
, type
, NULL
, complit
);
6500 C_MAYBE_CONST_EXPR_NON_CONST (complit
) = 1;
6506 /* Check the type of a compound literal. Here we just check that it
6507 is valid for C++. */
6510 check_compound_literal_type (location_t loc
, struct c_type_name
*type_name
)
6513 && (type_name
->specs
->typespec_kind
== ctsk_tagdef
6514 || type_name
->specs
->typespec_kind
== ctsk_tagfirstref
6515 || type_name
->specs
->typespec_kind
== ctsk_tagfirstref_attrs
))
6516 warning_at (loc
, OPT_Wc___compat
,
6517 "defining a type in a compound literal is invalid in C++");
6520 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6521 replacing with appropriate values if they are invalid. */
6524 check_bitfield_type_and_width (location_t loc
, tree
*type
, tree
*width
,
6528 unsigned int max_width
;
6529 unsigned HOST_WIDE_INT w
;
6530 const char *name
= (orig_name
6531 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name
))
6532 : _("<anonymous>"));
6534 /* Detect and ignore out of range field width and process valid
6536 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width
)))
6538 error_at (loc
, "bit-field %qs width not an integer constant", name
);
6539 *width
= integer_one_node
;
6543 if (TREE_CODE (*width
) != INTEGER_CST
)
6545 *width
= c_fully_fold (*width
, false, NULL
);
6546 if (TREE_CODE (*width
) == INTEGER_CST
)
6547 pedwarn (loc
, OPT_Wpedantic
,
6548 "bit-field %qs width not an integer constant expression",
6551 if (TREE_CODE (*width
) != INTEGER_CST
)
6553 error_at (loc
, "bit-field %qs width not an integer constant", name
);
6554 *width
= integer_one_node
;
6556 constant_expression_warning (*width
);
6557 if (tree_int_cst_sgn (*width
) < 0)
6559 error_at (loc
, "negative width in bit-field %qs", name
);
6560 *width
= integer_one_node
;
6562 else if (integer_zerop (*width
) && orig_name
)
6564 error_at (loc
, "zero width for bit-field %qs", name
);
6565 *width
= integer_one_node
;
6569 /* Detect invalid bit-field type. */
6570 if (TREE_CODE (*type
) != INTEGER_TYPE
6571 && TREE_CODE (*type
) != BOOLEAN_TYPE
6572 && TREE_CODE (*type
) != ENUMERAL_TYPE
6573 && TREE_CODE (*type
) != BITINT_TYPE
)
6575 error_at (loc
, "bit-field %qs has invalid type", name
);
6576 *type
= unsigned_type_node
;
6579 if (TYPE_WARN_IF_NOT_ALIGN (*type
))
6581 error_at (loc
, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6583 *type
= unsigned_type_node
;
6586 type_mv
= TYPE_MAIN_VARIANT (*type
);
6587 if (!in_system_header_at (input_location
)
6588 && type_mv
!= integer_type_node
6589 && type_mv
!= unsigned_type_node
6590 && type_mv
!= boolean_type_node
)
6591 pedwarn_c90 (loc
, OPT_Wpedantic
,
6592 "type of bit-field %qs is a GCC extension", name
);
6594 max_width
= TYPE_PRECISION (*type
);
6596 if (compare_tree_int (*width
, max_width
) > 0)
6598 error_at (loc
, "width of %qs exceeds its type", name
);
6600 *width
= build_int_cst (integer_type_node
, w
);
6603 w
= tree_to_uhwi (*width
);
6605 /* Truncation of hardbool false and true representation values is always safe:
6606 either the values remain different, or we'll report a problem when creating
6607 the narrower type. */
6608 if (c_hardbool_type_attr (*type
))
6611 if (TREE_CODE (*type
) == ENUMERAL_TYPE
)
6613 struct lang_type
*lt
= TYPE_LANG_SPECIFIC (*type
);
6615 || w
< tree_int_cst_min_precision (lt
->enum_min
, TYPE_SIGN (*type
))
6616 || w
< tree_int_cst_min_precision (lt
->enum_max
, TYPE_SIGN (*type
)))
6617 warning_at (loc
, 0, "%qs is narrower than values of its type", name
);
6623 /* Print warning about variable length array if necessary. */
6626 warn_variable_length_array (tree name
, tree size
)
6628 if (TREE_CONSTANT (size
))
6631 pedwarn_c90 (input_location
, OPT_Wvla
,
6632 "ISO C90 forbids array %qE whose size "
6633 "cannot be evaluated", name
);
6635 pedwarn_c90 (input_location
, OPT_Wvla
, "ISO C90 forbids array "
6636 "whose size cannot be evaluated");
6641 pedwarn_c90 (input_location
, OPT_Wvla
,
6642 "ISO C90 forbids variable length array %qE", name
);
6644 pedwarn_c90 (input_location
, OPT_Wvla
, "ISO C90 forbids variable "
6649 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6650 considering only those c_declspec_words found in LIST, which
6651 must be terminated by cdw_number_of_elements. */
6654 smallest_type_quals_location (const location_t
*locations
,
6655 const c_declspec_word
*list
)
6657 location_t loc
= UNKNOWN_LOCATION
;
6658 while (*list
!= cdw_number_of_elements
)
6660 location_t newloc
= locations
[*list
];
6661 if (loc
== UNKNOWN_LOCATION
6662 || (newloc
!= UNKNOWN_LOCATION
&& newloc
< loc
))
6671 /* We attach an artificial TYPE_DECL to pointed-to type
6672 and arrange for it to be included in a DECL_EXPR. This
6673 forces the sizes evaluation at a safe point and ensures it
6674 is not deferred until e.g. within a deeper conditional context.
6676 PARM contexts have no enclosing statement list that
6677 can hold the DECL_EXPR, so we need to use a BIND_EXPR
6678 instead, and add it to the list of expressions that
6679 need to be evaluated.
6681 TYPENAME contexts do have an enclosing statement list,
6682 but it would be incorrect to use it, as the size should
6683 only be evaluated if the containing expression is
6684 evaluated. We might also be in the middle of an
6685 expression with side effects on the pointed-to type size
6686 "arguments" prior to the pointer declaration point and
6687 the fake TYPE_DECL in the enclosing context would force
6688 the size evaluation prior to the side effects. We therefore
6689 use BIND_EXPRs in TYPENAME contexts too. */
6691 add_decl_expr (location_t loc
, tree type
, tree
*expr
, bool set_name_p
)
6693 tree bind
= NULL_TREE
;
6696 bind
= build3 (BIND_EXPR
, void_type_node
, NULL_TREE
, NULL_TREE
,
6698 TREE_SIDE_EFFECTS (bind
) = 1;
6699 BIND_EXPR_BODY (bind
) = push_stmt_list ();
6703 tree decl
= build_decl (loc
, TYPE_DECL
, NULL_TREE
, type
);
6705 DECL_ARTIFICIAL (decl
) = 1;
6706 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl
), DECL_EXPR
, decl
));
6708 TYPE_NAME (type
) = decl
;
6713 BIND_EXPR_BODY (bind
) = pop_stmt_list (BIND_EXPR_BODY (bind
));
6715 *expr
= build2 (COMPOUND_EXPR
, void_type_node
, *expr
, bind
);
6721 /* Given declspecs and a declarator,
6722 determine the name and type of the object declared
6723 and construct a ..._DECL node for it.
6724 (In one case we can return a ..._TYPE node instead.
6725 For invalid input we sometimes return NULL_TREE.)
6727 DECLSPECS is a c_declspecs structure for the declaration specifiers.
6729 DECL_CONTEXT says which syntactic context this declaration is in:
6730 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6731 FUNCDEF for a function definition. Like NORMAL but a few different
6732 error messages in each case. Return value may be zero meaning
6733 this definition is too screwy to try to parse.
6734 PARM for a parameter declaration (either within a function prototype
6735 or before a function body). Make a PARM_DECL, or return void_type_node.
6736 TYPENAME if for a typename (in a cast or sizeof).
6737 Don't make a DECL node; just return the ..._TYPE node.
6738 FIELD for a struct or union field; make a FIELD_DECL.
6739 INITIALIZED is true if the decl has an initializer.
6740 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6741 representing the width of the bit-field.
6742 DECL_ATTRS points to the list of attributes that should be added to this
6743 decl. Any nested attributes that belong on the decl itself will be
6745 If EXPR is not NULL, any expressions that need to be evaluated as
6746 part of evaluating variably modified types will be stored in *EXPR.
6747 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6748 set to indicate whether operands in *EXPR can be used in constant
6750 DEPRECATED_STATE is a deprecated_states value indicating whether
6751 deprecation/unavailability warnings should be suppressed.
6753 In the TYPENAME case, DECLARATOR is really an absolute declarator.
6754 It may also be so in the PARM case, for a prototype where the
6755 argument type is specified but not the name.
6757 This function is where the complicated C meanings of `static'
6758 and `extern' are interpreted. */
6761 grokdeclarator (const struct c_declarator
*declarator
,
6762 struct c_declspecs
*declspecs
,
6763 enum decl_context decl_context
, bool initialized
, tree
*width
,
6764 tree
*decl_attrs
, tree
*expr
, bool *expr_const_operands
,
6765 enum deprecated_states deprecated_state
)
6767 tree type
= declspecs
->type
;
6768 bool threadp
= declspecs
->thread_p
;
6769 bool constexprp
= declspecs
->constexpr_p
;
6770 enum c_storage_class storage_class
= declspecs
->storage_class
;
6775 int type_quals
= TYPE_UNQUALIFIED
;
6776 tree name
= NULL_TREE
;
6777 bool funcdef_flag
= false;
6778 bool funcdef_syntax
= false;
6779 bool size_varies
= false;
6780 tree decl_attr
= declspecs
->decl_attr
;
6781 int array_ptr_quals
= TYPE_UNQUALIFIED
;
6782 tree array_ptr_attrs
= NULL_TREE
;
6783 bool array_parm_static
= false;
6784 bool array_parm_vla_unspec_p
= false;
6785 tree returned_attrs
= NULL_TREE
;
6786 tree decl_id_attrs
= NULL_TREE
;
6787 bool bitfield
= width
!= NULL
;
6789 tree orig_qual_type
= NULL
;
6790 size_t orig_qual_indirect
= 0;
6791 struct c_arg_info
*arg_info
= 0;
6792 addr_space_t as1
, as2
, address_space
;
6793 location_t loc
= UNKNOWN_LOCATION
;
6795 bool expr_const_operands_dummy
;
6796 enum c_declarator_kind first_non_attr_kind
;
6797 unsigned int alignas_align
= 0;
6799 if (type
== NULL_TREE
)
6801 /* This can occur for auto on a parameter in C23 mode. Set a
6802 dummy type here so subsequent code can give diagnostics for
6804 gcc_assert (declspecs
->c23_auto_p
);
6805 gcc_assert (decl_context
== PARM
);
6806 type
= declspecs
->type
= integer_type_node
;
6808 if (TREE_CODE (type
) == ERROR_MARK
)
6809 return error_mark_node
;
6813 expr_dummy
= NULL_TREE
;
6815 if (expr_const_operands
== NULL
)
6816 expr_const_operands
= &expr_const_operands_dummy
;
6818 if (declspecs
->expr
)
6821 *expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (declspecs
->expr
), *expr
,
6824 *expr
= declspecs
->expr
;
6826 *expr_const_operands
= declspecs
->expr_const_operands
;
6828 if (decl_context
== FUNCDEF
)
6829 funcdef_flag
= true, decl_context
= NORMAL
;
6831 /* Look inside a declarator for the name being declared
6832 and get it as an IDENTIFIER_NODE, for an error message. */
6834 const struct c_declarator
*decl
= declarator
;
6836 first_non_attr_kind
= cdk_attrs
;
6846 funcdef_syntax
= (decl
->kind
== cdk_function
);
6847 if (first_non_attr_kind
== cdk_attrs
)
6848 first_non_attr_kind
= decl
->kind
;
6849 decl
= decl
->declarator
;
6853 decl
= decl
->declarator
;
6859 name
= decl
->u
.id
.id
;
6860 decl_id_attrs
= decl
->u
.id
.attrs
;
6861 if (first_non_attr_kind
== cdk_attrs
)
6862 first_non_attr_kind
= decl
->kind
;
6869 if (name
== NULL_TREE
)
6871 gcc_assert (decl_context
== PARM
6872 || decl_context
== TYPENAME
6873 || (decl_context
== FIELD
6874 && declarator
->kind
== cdk_id
));
6875 gcc_assert (!initialized
);
6879 /* An enum type specifier (": specifier-qualifier-list") may only be
6880 specified when the enum is being defined or in an empty
6881 declaration of the form "enum identifier enum-type-specifier;".
6882 Except for the case of an empty declaration that has additional
6883 declaration specifiers, all invalid contexts (declarations that
6884 aren't empty, type names, parameter declarations, member
6885 declarations) pass through grokdeclarator. */
6886 if (declspecs
->enum_type_specifier_ref_p
)
6887 error_at (loc
, "%<enum%> underlying type may not be specified here");
6889 /* A function definition's declarator must have the form of
6890 a function declarator. */
6892 if (funcdef_flag
&& !funcdef_syntax
)
6895 /* If this looks like a function definition, make it one,
6896 even if it occurs where parms are expected.
6897 Then store_parm_decls will reject it and not use it as a parm. */
6898 if (decl_context
== NORMAL
&& !funcdef_flag
&& current_scope
->parm_flag
)
6899 decl_context
= PARM
;
6901 if (deprecated_state
!= UNAVAILABLE_DEPRECATED_SUPPRESS
)
6903 if (declspecs
->unavailable_p
)
6904 error_unavailable_use (declspecs
->type
, declspecs
->decl_attr
);
6905 else if (declspecs
->deprecated_p
6906 && deprecated_state
!= DEPRECATED_SUPPRESS
)
6907 warn_deprecated_use (declspecs
->type
, declspecs
->decl_attr
);
6910 if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
6911 && current_scope
== file_scope
6912 && c_type_variably_modified_p (type
))
6915 error_at (loc
, "variably modified %qE at file scope", name
);
6917 error_at (loc
, "variably modified field at file scope");
6918 type
= integer_type_node
;
6921 size_varies
= C_TYPE_VARIABLE_SIZE (type
) != 0;
6923 /* Diagnose defaulting to "int". */
6925 if (declspecs
->default_int_p
)
6927 /* Issue a warning if this is an ISO C 99 program or if
6928 -Wreturn-type and this is a function, or if -Wimplicit;
6929 prefer the former warning since it is more explicit. */
6930 if ((warn_implicit_int
|| warn_return_type
> 0 || flag_isoc99
)
6932 warn_about_return_type
= 1;
6936 permerror_opt (loc
, OPT_Wimplicit_int
,
6937 "type defaults to %<int%> in declaration "
6940 permerror_opt (loc
, OPT_Wimplicit_int
,
6941 "type defaults to %<int%> in type name");
6945 /* Adjust the type if a bit-field is being declared,
6946 -funsigned-bitfields applied and the type is not explicitly
6948 if (bitfield
&& !flag_signed_bitfields
&& !declspecs
->explicit_signed_p
6949 && TREE_CODE (type
) == INTEGER_TYPE
)
6950 type
= unsigned_type_for (type
);
6952 /* Figure out the type qualifiers for the declaration. There are
6953 two ways a declaration can become qualified. One is something
6954 like `const int i' where the `const' is explicit. Another is
6955 something like `typedef const int CI; CI i' where the type of the
6956 declaration contains the `const'. A third possibility is that
6957 there is a type qualifier on the element type of a typedefed
6958 array type, in which case we should extract that qualifier so
6959 that c_apply_type_quals_to_decl receives the full list of
6960 qualifiers to work with (C90 is not entirely clear about whether
6961 duplicate qualifiers should be diagnosed in this case, but it
6962 seems most appropriate to do so). */
6963 element_type
= strip_array_types (type
);
6964 constp
= declspecs
->const_p
+ TYPE_READONLY (element_type
);
6965 restrictp
= declspecs
->restrict_p
+ TYPE_RESTRICT (element_type
);
6966 volatilep
= declspecs
->volatile_p
+ TYPE_VOLATILE (element_type
);
6967 atomicp
= declspecs
->atomic_p
+ TYPE_ATOMIC (element_type
);
6968 as1
= declspecs
->address_space
;
6969 as2
= TYPE_ADDR_SPACE (element_type
);
6970 address_space
= ADDR_SPACE_GENERIC_P (as1
)? as2
: as1
;
6973 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<const%>");
6975 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<restrict%>");
6977 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<volatile%>");
6979 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<_Atomic%>");
6981 if (!ADDR_SPACE_GENERIC_P (as1
) && !ADDR_SPACE_GENERIC_P (as2
) && as1
!= as2
)
6982 error_at (loc
, "conflicting named address spaces (%s vs %s)",
6983 c_addr_space_name (as1
), c_addr_space_name (as2
));
6985 if ((TREE_CODE (type
) == ARRAY_TYPE
6986 || first_non_attr_kind
== cdk_array
)
6987 && TYPE_QUALS (element_type
))
6989 orig_qual_type
= type
;
6990 type
= TYPE_MAIN_VARIANT (type
);
6992 type_quals
= ((constp
? TYPE_QUAL_CONST
: 0)
6993 | (restrictp
? TYPE_QUAL_RESTRICT
: 0)
6994 | (volatilep
? TYPE_QUAL_VOLATILE
: 0)
6995 | (atomicp
? TYPE_QUAL_ATOMIC
: 0)
6996 | ENCODE_QUAL_ADDR_SPACE (address_space
));
6997 if (type_quals
!= TYPE_QUALS (element_type
))
6998 orig_qual_type
= NULL_TREE
;
7000 /* Applying the _Atomic qualifier to an array type (through the use
7001 of typedefs or typeof) must be detected here. If the qualifier
7002 is introduced later, any appearance of applying it to an array is
7003 actually applying it to an element of that array. */
7004 if (declspecs
->atomic_p
&& TREE_CODE (type
) == ARRAY_TYPE
)
7005 error_at (loc
, "%<_Atomic%>-qualified array type");
7007 /* Warn about storage classes that are invalid for certain
7008 kinds of declarations (parameters, typenames, etc.). */
7013 || storage_class
== csc_auto
7014 || storage_class
== csc_register
7015 || storage_class
== csc_typedef
))
7017 if (storage_class
== csc_auto
)
7019 (current_scope
== file_scope
) ? 0 : OPT_Wpedantic
,
7020 "function definition declared %<auto%>");
7021 if (storage_class
== csc_register
)
7022 error_at (loc
, "function definition declared %<register%>");
7023 if (storage_class
== csc_typedef
)
7024 error_at (loc
, "function definition declared %<typedef%>");
7026 error_at (loc
, "function definition declared %qs",
7027 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
7029 /* The parser ensures a constexpr function definition never
7031 gcc_assert (!constexprp
);
7032 if (storage_class
== csc_auto
7033 || storage_class
== csc_register
7034 || storage_class
== csc_typedef
)
7035 storage_class
= csc_none
;
7037 else if (decl_context
!= NORMAL
&& (storage_class
!= csc_none
7040 || declspecs
->c23_auto_p
))
7042 if (decl_context
== PARM
7043 && storage_class
== csc_register
7045 && !declspecs
->c23_auto_p
)
7049 switch (decl_context
)
7053 error_at (loc
, "storage class specified for structure "
7056 error_at (loc
, "storage class specified for structure field");
7060 error_at (loc
, "storage class specified for parameter %qE",
7063 error_at (loc
, "storage class specified for unnamed parameter");
7066 error_at (loc
, "storage class specified for typename");
7069 storage_class
= csc_none
;
7074 else if (storage_class
== csc_extern
7078 /* 'extern' with initialization is invalid if not at file scope. */
7079 if (current_scope
== file_scope
)
7081 /* It is fine to have 'extern const' when compiling at C
7082 and C++ intersection. */
7083 if (!(warn_cxx_compat
&& constp
))
7084 warning_at (loc
, 0, "%qE initialized and declared %<extern%>",
7088 error_at (loc
, "%qE has both %<extern%> and initializer", name
);
7090 else if (current_scope
== file_scope
)
7092 if (storage_class
== csc_auto
)
7093 error_at (loc
, "file-scope declaration of %qE specifies %<auto%>",
7095 if (pedantic
&& storage_class
== csc_register
)
7096 pedwarn (input_location
, OPT_Wpedantic
,
7097 "file-scope declaration of %qE specifies %<register%>", name
);
7101 if (storage_class
== csc_extern
&& funcdef_flag
)
7102 error_at (loc
, "nested function %qE declared %<extern%>", name
);
7103 else if (threadp
&& storage_class
== csc_none
)
7105 error_at (loc
, "function-scope %qE implicitly auto and declared "
7107 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
7112 /* Now figure out the structure of the declarator proper.
7113 Descend through it, creating more complex types, until we reach
7114 the declared identifier (or NULL_TREE, in an absolute declarator).
7115 At each stage we maintain an unqualified version of the type
7116 together with any qualifiers that should be applied to it with
7117 c_build_qualified_type; this way, array types including
7118 multidimensional array types are first built up in unqualified
7119 form and then the qualified form is created with
7120 TYPE_MAIN_VARIANT pointing to the unqualified form. */
7122 while (declarator
&& declarator
->kind
!= cdk_id
)
7124 if (type
== error_mark_node
)
7126 declarator
= declarator
->declarator
;
7130 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
7131 a cdk_pointer (for *...),
7132 a cdk_function (for ...(...)),
7133 a cdk_attrs (for nested attributes),
7134 or a cdk_id (for the name being declared
7135 or the place in an absolute declarator
7136 where the name was omitted).
7137 For the last case, we have just exited the loop.
7139 At this point, TYPE is the type of elements of an array,
7140 or for a function to return, or for a pointer to point to.
7141 After this sequence of ifs, TYPE is the type of the
7142 array or function or pointer, and DECLARATOR has had its
7143 outermost layer removed. */
7145 if (array_ptr_quals
!= TYPE_UNQUALIFIED
7146 || array_ptr_attrs
!= NULL_TREE
7147 || array_parm_static
)
7149 /* Only the innermost declarator (making a parameter be of
7150 array type which is converted to pointer type)
7151 may have static or type qualifiers. */
7152 error_at (loc
, "static or type qualifiers in non-parameter array declarator");
7153 array_ptr_quals
= TYPE_UNQUALIFIED
;
7154 array_ptr_attrs
= NULL_TREE
;
7155 array_parm_static
= false;
7158 bool varmod
= C_TYPE_VARIABLY_MODIFIED (type
);
7160 switch (declarator
->kind
)
7164 /* A declarator with embedded attributes. */
7165 tree attrs
= declarator
->u
.attrs
;
7166 const struct c_declarator
*inner_decl
;
7168 declarator
= declarator
->declarator
;
7169 /* Standard attribute syntax precisely defines what entity
7170 an attribute in each position appertains to, so only
7171 apply laxity about positioning to GNU attribute syntax.
7172 Standard attributes applied to a function or array
7173 declarator apply exactly to that type; standard
7174 attributes applied to the identifier apply to the
7175 declaration rather than to the type, and are specified
7176 using a cdk_id declarator rather than using
7178 inner_decl
= declarator
;
7179 while (inner_decl
->kind
== cdk_attrs
)
7180 inner_decl
= inner_decl
->declarator
;
7181 if (!cxx11_attribute_p (attrs
))
7183 if (inner_decl
->kind
== cdk_id
)
7184 attr_flags
|= (int) ATTR_FLAG_DECL_NEXT
;
7185 else if (inner_decl
->kind
== cdk_function
)
7186 attr_flags
|= (int) ATTR_FLAG_FUNCTION_NEXT
;
7187 else if (inner_decl
->kind
== cdk_array
)
7188 attr_flags
|= (int) ATTR_FLAG_ARRAY_NEXT
;
7190 attrs
= c_warn_type_attributes (attrs
);
7191 returned_attrs
= decl_attributes (&type
,
7192 chainon (returned_attrs
, attrs
),
7198 tree itype
= NULL_TREE
;
7199 tree size
= declarator
->u
.array
.dimen
;
7200 /* The index is a signed object `sizetype' bits wide. */
7201 tree index_type
= c_common_signed_type (sizetype
);
7203 array_ptr_quals
= declarator
->u
.array
.quals
;
7204 array_ptr_attrs
= declarator
->u
.array
.attrs
;
7205 array_parm_static
= declarator
->u
.array
.static_p
;
7206 array_parm_vla_unspec_p
= declarator
->u
.array
.vla_unspec_p
;
7208 declarator
= declarator
->declarator
;
7210 /* Check for some types that there cannot be arrays of. */
7212 if (VOID_TYPE_P (type
))
7215 error_at (loc
, "declaration of %qE as array of voids", name
);
7217 error_at (loc
, "declaration of type name as array of voids");
7218 type
= error_mark_node
;
7221 if (TREE_CODE (type
) == FUNCTION_TYPE
)
7224 error_at (loc
, "declaration of %qE as array of functions",
7227 error_at (loc
, "declaration of type name as array of "
7229 type
= error_mark_node
;
7232 if (pedantic
&& !in_system_header_at (input_location
)
7233 && flexible_array_type_p (type
))
7234 pedwarn (loc
, OPT_Wpedantic
,
7235 "invalid use of structure with flexible array member");
7237 if (size
== error_mark_node
)
7238 type
= error_mark_node
;
7240 if (type
== error_mark_node
)
7243 if (!verify_type_context (loc
, TCTX_ARRAY_ELEMENT
, type
))
7245 type
= error_mark_node
;
7249 /* If size was specified, set ITYPE to a range-type for
7250 that size. Otherwise, ITYPE remains null. finish_decl
7251 may figure it out from an initial value. */
7255 bool size_maybe_const
= true;
7256 bool size_int_const
= (TREE_CODE (size
) == INTEGER_CST
7257 && !TREE_OVERFLOW (size
));
7258 bool this_size_varies
= false;
7260 /* Strip NON_LVALUE_EXPRs since we aren't using as an
7262 STRIP_TYPE_NOPS (size
);
7264 if (!INTEGRAL_TYPE_P (TREE_TYPE (size
)))
7267 error_at (loc
, "size of array %qE has non-integer type",
7271 "size of unnamed array has non-integer type");
7272 size
= integer_one_node
;
7273 size_int_const
= true;
7275 /* This can happen with enum forward declaration. */
7276 else if (!COMPLETE_TYPE_P (TREE_TYPE (size
)))
7279 error_at (loc
, "size of array %qE has incomplete type",
7282 error_at (loc
, "size of unnamed array has incomplete "
7284 size
= integer_one_node
;
7285 size_int_const
= true;
7288 size
= c_fully_fold (size
, false, &size_maybe_const
);
7290 if (pedantic
&& size_maybe_const
&& integer_zerop (size
))
7293 pedwarn (loc
, OPT_Wpedantic
,
7294 "ISO C forbids zero-size array %qE", name
);
7296 pedwarn (loc
, OPT_Wpedantic
,
7297 "ISO C forbids zero-size array");
7300 if (TREE_CODE (size
) == INTEGER_CST
&& size_maybe_const
)
7302 constant_expression_warning (size
);
7303 if (tree_int_cst_sgn (size
) < 0)
7306 error_at (loc
, "size of array %qE is negative", name
);
7308 error_at (loc
, "size of unnamed array is negative");
7309 size
= integer_one_node
;
7310 size_int_const
= true;
7312 /* Handle a size folded to an integer constant but
7313 not an integer constant expression. */
7314 if (!size_int_const
)
7316 /* If this is a file scope declaration of an
7317 ordinary identifier, this is invalid code;
7318 diagnosing it here and not subsequently
7319 treating the type as variable-length avoids
7320 more confusing diagnostics later. */
7321 if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
7322 && current_scope
== file_scope
)
7323 pedwarn (input_location
, 0,
7324 "variably modified %qE at file scope",
7327 this_size_varies
= size_varies
= true;
7328 warn_variable_length_array (name
, size
);
7331 else if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
7332 && current_scope
== file_scope
)
7334 error_at (loc
, "variably modified %qE at file scope", name
);
7335 size
= integer_one_node
;
7339 /* Make sure the array size remains visibly
7340 nonconstant even if it is (eg) a const variable
7341 with known value. */
7342 this_size_varies
= size_varies
= true;
7343 warn_variable_length_array (name
, size
);
7344 if (sanitize_flags_p (SANITIZE_VLA
)
7345 && current_function_decl
!= NULL_TREE
7346 && decl_context
== NORMAL
)
7348 /* Evaluate the array size only once. */
7349 size
= save_expr (size
);
7350 size
= c_fully_fold (size
, false, NULL
);
7351 size
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
7352 ubsan_instrument_vla (loc
, size
),
7357 if (integer_zerop (size
) && !this_size_varies
)
7359 /* A zero-length array cannot be represented with
7360 an unsigned index type, which is what we'll
7361 get with build_index_type. Create an
7362 open-ended range instead. */
7363 itype
= build_range_type (sizetype
, size
, NULL_TREE
);
7367 /* Arrange for the SAVE_EXPR on the inside of the
7368 MINUS_EXPR, which allows the -1 to get folded
7369 with the +1 that happens when building TYPE_SIZE. */
7371 size
= save_expr (size
);
7372 if (this_size_varies
&& TREE_CODE (size
) == INTEGER_CST
)
7373 size
= build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
7374 integer_zero_node
, size
);
7376 /* Compute the maximum valid index, that is, size
7377 - 1. Do the calculation in index_type, so that
7378 if it is a variable the computations will be
7379 done in the proper mode. */
7380 itype
= fold_build2_loc (loc
, MINUS_EXPR
, index_type
,
7381 convert (index_type
, size
),
7382 convert (index_type
,
7385 /* The above overflows when size does not fit
7387 ??? While a size of INT_MAX+1 technically shouldn't
7388 cause an overflow (because we subtract 1), handling
7389 this case seems like an unnecessary complication. */
7390 if (TREE_CODE (size
) == INTEGER_CST
7391 && !int_fits_type_p (size
, index_type
))
7394 error_at (loc
, "size of array %qE is too large",
7397 error_at (loc
, "size of unnamed array is too large");
7398 type
= error_mark_node
;
7402 itype
= build_index_type (itype
);
7404 if (this_size_varies
)
7406 if (TREE_SIDE_EFFECTS (size
))
7409 *expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
7414 *expr_const_operands
&= size_maybe_const
;
7417 else if (decl_context
== FIELD
)
7419 bool flexible_array_member
= false;
7420 if (array_parm_vla_unspec_p
)
7421 /* Field names can in fact have function prototype
7422 scope so [*] is disallowed here through making
7423 the field variably modified, not through being
7424 something other than a declaration with function
7429 const struct c_declarator
*t
= declarator
;
7430 while (t
->kind
== cdk_attrs
)
7432 flexible_array_member
= (t
->kind
== cdk_id
);
7434 if (flexible_array_member
7435 && !in_system_header_at (input_location
))
7436 pedwarn_c90 (loc
, OPT_Wpedantic
, "ISO C90 does not "
7437 "support flexible array members");
7439 /* ISO C99 Flexible array members are effectively
7440 identical to GCC's zero-length array extension. */
7441 if (flexible_array_member
|| array_parm_vla_unspec_p
)
7442 itype
= build_range_type (sizetype
, size_zero_node
,
7445 else if (decl_context
== PARM
)
7447 if (array_parm_vla_unspec_p
)
7449 itype
= build_range_type (sizetype
, size_zero_node
, NULL_TREE
);
7453 else if (decl_context
== TYPENAME
)
7455 if (array_parm_vla_unspec_p
)
7458 warning (0, "%<[*]%> not in a declaration");
7459 /* We use this to avoid messing up with incomplete
7460 array types of the same type, that would
7461 otherwise be modified below. */
7462 itype
= build_range_type (sizetype
, size_zero_node
,
7468 /* Complain about arrays of incomplete types. */
7469 if (!COMPLETE_TYPE_P (type
))
7471 auto_diagnostic_group d
;
7472 error_at (loc
, "array type has incomplete element type %qT",
7474 /* See if we can be more helpful. */
7475 if (TREE_CODE (type
) == ARRAY_TYPE
)
7478 inform (loc
, "declaration of %qE as multidimensional "
7479 "array must have bounds for all dimensions "
7480 "except the first", name
);
7482 inform (loc
, "declaration of multidimensional array "
7483 "must have bounds for all dimensions except "
7486 type
= error_mark_node
;
7489 /* When itype is NULL, a shared incomplete array type is
7490 returned for all array of a given type. Elsewhere we
7491 make sure we don't complete that type before copying
7492 it, but here we want to make sure we don't ever
7493 modify the shared type, so we gcc_assert (itype)
7496 addr_space_t as
= DECODE_QUAL_ADDR_SPACE (type_quals
);
7497 if (!ADDR_SPACE_GENERIC_P (as
) && as
!= TYPE_ADDR_SPACE (type
))
7498 type
= build_qualified_type (type
,
7499 ENCODE_QUAL_ADDR_SPACE (as
));
7501 type
= build_array_type (type
, itype
);
7504 if (type
!= error_mark_node
)
7508 /* It is ok to modify type here even if itype is
7509 NULL: if size_varies, we're in a
7510 multi-dimensional array and the inner type has
7511 variable size, so the enclosing shared array type
7513 if (size
&& TREE_CODE (size
) == INTEGER_CST
)
7514 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
7515 C_TYPE_VARIABLE_SIZE (type
) = 1;
7518 /* The GCC extension for zero-length arrays differs from
7519 ISO flexible array members in that sizeof yields
7521 if (size
&& integer_zerop (size
))
7524 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
7525 TYPE_SIZE (type
) = bitsize_zero_node
;
7526 TYPE_SIZE_UNIT (type
) = size_zero_node
;
7527 SET_TYPE_STRUCTURAL_EQUALITY (type
);
7529 if (array_parm_vla_unspec_p
)
7532 /* The type is complete. C99 6.7.5.2p4 */
7533 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
7534 TYPE_SIZE (type
) = bitsize_zero_node
;
7535 TYPE_SIZE_UNIT (type
) = size_zero_node
;
7536 SET_TYPE_STRUCTURAL_EQUALITY (type
);
7539 if (!valid_array_size_p (loc
, type
, name
))
7540 type
= error_mark_node
;
7543 if (decl_context
!= PARM
7544 && (array_ptr_quals
!= TYPE_UNQUALIFIED
7545 || array_ptr_attrs
!= NULL_TREE
7546 || array_parm_static
))
7548 error_at (loc
, "static or type qualifiers in non-parameter "
7549 "array declarator");
7550 array_ptr_quals
= TYPE_UNQUALIFIED
;
7551 array_ptr_attrs
= NULL_TREE
;
7552 array_parm_static
= false;
7554 orig_qual_indirect
++;
7559 /* Say it's a definition only for the declarator closest
7560 to the identifier, apart possibly from some
7562 bool really_funcdef
= false;
7564 orig_qual_type
= NULL_TREE
;
7567 const struct c_declarator
*t
= declarator
->declarator
;
7568 while (t
->kind
== cdk_attrs
)
7570 really_funcdef
= (t
->kind
== cdk_id
);
7573 /* Declaring a function type. Make sure we have a valid
7574 type for the function to return. */
7575 if (type
== error_mark_node
)
7578 size_varies
= false;
7580 /* Warn about some types functions can't return. */
7581 if (TREE_CODE (type
) == FUNCTION_TYPE
)
7584 error_at (loc
, "%qE declared as function returning a "
7587 error_at (loc
, "type name declared as function "
7588 "returning a function");
7589 type
= integer_type_node
;
7591 if (TREE_CODE (type
) == ARRAY_TYPE
)
7594 error_at (loc
, "%qE declared as function returning an array",
7597 error_at (loc
, "type name declared as function returning "
7599 type
= integer_type_node
;
7602 /* Construct the function type and go to the next
7603 inner layer of declarator. */
7604 arg_info
= declarator
->u
.arg_info
;
7605 arg_types
= grokparms (arg_info
, really_funcdef
);
7607 /* Type qualifiers before the return type of the function
7608 qualify the return type, not the function type. */
7611 const enum c_declspec_word ignored_quals_list
[] =
7613 cdw_const
, cdw_volatile
, cdw_restrict
, cdw_address_space
,
7614 cdw_atomic
, cdw_number_of_elements
7616 location_t specs_loc
7617 = smallest_type_quals_location (declspecs
->locations
,
7618 ignored_quals_list
);
7619 if (specs_loc
== UNKNOWN_LOCATION
)
7620 specs_loc
= declspecs
->locations
[cdw_typedef
];
7621 if (specs_loc
== UNKNOWN_LOCATION
)
7624 /* Type qualifiers on a function return type are
7625 normally permitted by the standard but have no
7626 effect, so give a warning at -Wreturn-type.
7627 Qualifiers on a void return type are banned on
7628 function definitions in ISO C; GCC used to used
7629 them for noreturn functions. The resolution of C11
7630 DR#423 means qualifiers (other than _Atomic) are
7631 actually removed from the return type when
7632 determining the function type. For C23, _Atomic is
7634 int quals_used
= type_quals
;
7637 else if (flag_isoc11
)
7638 quals_used
&= TYPE_QUAL_ATOMIC
;
7639 if (quals_used
&& VOID_TYPE_P (type
) && really_funcdef
)
7640 pedwarn (specs_loc
, 0,
7641 "function definition has qualified void "
7644 warning_at (specs_loc
, OPT_Wignored_qualifiers
,
7645 "type qualifiers ignored on function "
7648 /* Ensure an error for restrict on invalid types; the
7649 DR#423 resolution is not entirely clear about
7652 && (type_quals
& TYPE_QUAL_RESTRICT
)
7653 && (!POINTER_TYPE_P (type
)
7654 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
7655 error_at (loc
, "invalid use of %<restrict%>");
7656 type
= c_build_qualified_type (type
, quals_used
);
7658 type_quals
= TYPE_UNQUALIFIED
;
7660 type
= build_function_type (type
, arg_types
,
7661 arg_info
->no_named_args_stdarg_p
);
7662 declarator
= declarator
->declarator
;
7664 /* Set the TYPE_CONTEXTs for each tagged type which is local to
7665 the formal parameter list of this FUNCTION_TYPE to point to
7666 the FUNCTION_TYPE node itself. */
7671 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info
->tags
, ix
, tag
)
7672 TYPE_CONTEXT (tag
->type
) = type
;
7678 /* Merge any constancy or volatility into the target type
7680 if ((type_quals
& TYPE_QUAL_ATOMIC
)
7681 && TREE_CODE (type
) == FUNCTION_TYPE
)
7684 "%<_Atomic%>-qualified function type");
7685 type_quals
&= ~TYPE_QUAL_ATOMIC
;
7687 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
7689 pedwarn (loc
, OPT_Wpedantic
,
7690 "ISO C forbids qualified function types");
7692 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
7693 orig_qual_indirect
);
7694 orig_qual_type
= NULL_TREE
;
7695 size_varies
= false;
7697 /* When the pointed-to type involves components of variable size,
7698 care must be taken to ensure that the size evaluation code is
7699 emitted early enough to dominate all the possible later uses
7700 and late enough for the variables on which it depends to have
7703 This is expected to happen automatically when the pointed-to
7704 type has a name/declaration of it's own, but special attention
7705 is required if the type is anonymous. */
7706 if (!TYPE_NAME (type
) && c_type_variably_modified_p (type
))
7708 bool bind_p
= decl_context
== TYPENAME
7709 || decl_context
== FIELD
7710 || decl_context
== PARM
;
7711 add_decl_expr (loc
, type
, bind_p
? expr
: NULL
, true);
7714 type
= c_build_pointer_type (type
);
7716 /* Process type qualifiers (such as const or volatile)
7717 that were given inside the `*'. */
7718 type_quals
= declarator
->u
.pointer_quals
;
7720 declarator
= declarator
->declarator
;
7726 if (type
!= error_mark_node
)
7727 C_TYPE_VARIABLY_MODIFIED (type
) = varmod
|| size_varies
;
7729 *decl_attrs
= chainon (returned_attrs
, *decl_attrs
);
7730 *decl_attrs
= chainon (decl_id_attrs
, *decl_attrs
);
7732 /* Now TYPE has the actual type, apart from any qualifiers in
7735 /* Warn about address space used for things other than static memory or
7737 address_space
= DECODE_QUAL_ADDR_SPACE (type_quals
);
7738 if (!ADDR_SPACE_GENERIC_P (address_space
))
7740 if (decl_context
== NORMAL
)
7742 switch (storage_class
)
7745 error ("%qs combined with %<auto%> qualifier for %qE",
7746 c_addr_space_name (address_space
), name
);
7749 error ("%qs combined with %<register%> qualifier for %qE",
7750 c_addr_space_name (address_space
), name
);
7753 if (current_function_scope
)
7755 error ("%qs specified for auto variable %qE",
7756 c_addr_space_name (address_space
), name
);
7768 else if (decl_context
== PARM
&& TREE_CODE (type
) != ARRAY_TYPE
)
7771 error ("%qs specified for parameter %qE",
7772 c_addr_space_name (address_space
), name
);
7774 error ("%qs specified for unnamed parameter",
7775 c_addr_space_name (address_space
));
7777 else if (decl_context
== FIELD
)
7780 error ("%qs specified for structure field %qE",
7781 c_addr_space_name (address_space
), name
);
7783 error ("%qs specified for structure field",
7784 c_addr_space_name (address_space
));
7788 /* Check the type and width of a bit-field. */
7791 check_bitfield_type_and_width (loc
, &type
, width
, name
);
7792 /* C11 makes it implementation-defined (6.7.2.1#5) whether
7793 atomic types are permitted for bit-fields; we have no code to
7794 make bit-field accesses atomic, so disallow them. */
7795 if (type_quals
& TYPE_QUAL_ATOMIC
)
7798 error_at (loc
, "bit-field %qE has atomic type", name
);
7800 error_at (loc
, "bit-field has atomic type");
7801 type_quals
&= ~TYPE_QUAL_ATOMIC
;
7805 /* Reject invalid uses of _Alignas. */
7806 if (declspecs
->alignas_p
)
7808 if (storage_class
== csc_typedef
)
7809 error_at (loc
, "alignment specified for typedef %qE", name
);
7810 else if (storage_class
== csc_register
)
7811 error_at (loc
, "alignment specified for %<register%> object %qE",
7813 else if (decl_context
== PARM
)
7816 error_at (loc
, "alignment specified for parameter %qE", name
);
7818 error_at (loc
, "alignment specified for unnamed parameter");
7823 error_at (loc
, "alignment specified for bit-field %qE", name
);
7825 error_at (loc
, "alignment specified for unnamed bit-field");
7827 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
7828 error_at (loc
, "alignment specified for function %qE", name
);
7829 else if (declspecs
->align_log
!= -1 && TYPE_P (type
))
7831 alignas_align
= 1U << declspecs
->align_log
;
7832 if (alignas_align
< min_align_of_type (type
))
7835 error_at (loc
, "%<_Alignas%> specifiers cannot reduce "
7836 "alignment of %qE", name
);
7838 error_at (loc
, "%<_Alignas%> specifiers cannot reduce "
7839 "alignment of unnamed field");
7845 /* If this is declaring a typedef name, return a TYPE_DECL. */
7847 if (storage_class
== csc_typedef
)
7850 if ((type_quals
& TYPE_QUAL_ATOMIC
)
7851 && TREE_CODE (type
) == FUNCTION_TYPE
)
7854 "%<_Atomic%>-qualified function type");
7855 type_quals
&= ~TYPE_QUAL_ATOMIC
;
7857 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
7859 pedwarn (loc
, OPT_Wpedantic
,
7860 "ISO C forbids qualified function types");
7862 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
7863 orig_qual_indirect
);
7864 decl
= build_decl (declarator
->id_loc
,
7865 TYPE_DECL
, declarator
->u
.id
.id
, type
);
7866 if (declspecs
->explicit_signed_p
)
7867 C_TYPEDEF_EXPLICITLY_SIGNED (decl
) = 1;
7868 if (declspecs
->inline_p
)
7869 pedwarn (loc
, 0,"typedef %q+D declared %<inline%>", decl
);
7870 if (declspecs
->noreturn_p
)
7871 pedwarn (loc
, 0,"typedef %q+D declared %<_Noreturn%>", decl
);
7873 if (warn_cxx_compat
&& declarator
->u
.id
.id
!= NULL_TREE
)
7875 struct c_binding
*b
= I_TAG_BINDING (declarator
->u
.id
.id
);
7878 && b
->decl
!= NULL_TREE
7879 && (B_IN_CURRENT_SCOPE (b
)
7880 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
7881 && TYPE_MAIN_VARIANT (b
->decl
) != TYPE_MAIN_VARIANT (type
))
7883 auto_diagnostic_group d
;
7884 if (warning_at (declarator
->id_loc
, OPT_Wc___compat
,
7885 ("using %qD as both a typedef and a tag is "
7886 "invalid in C++"), decl
)
7887 && b
->locus
!= UNKNOWN_LOCATION
)
7888 inform (b
->locus
, "originally defined here");
7895 /* If this is a type name (such as, in a cast or sizeof),
7896 compute the type and return it now. */
7898 if (decl_context
== TYPENAME
)
7900 /* Note that the grammar rejects storage classes in typenames
7902 gcc_assert (storage_class
== csc_none
&& !threadp
7903 && !declspecs
->inline_p
&& !declspecs
->noreturn_p
);
7904 if ((type_quals
& TYPE_QUAL_ATOMIC
)
7905 && TREE_CODE (type
) == FUNCTION_TYPE
)
7908 "%<_Atomic%>-qualified function type");
7909 type_quals
&= ~TYPE_QUAL_ATOMIC
;
7911 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
7913 pedwarn (loc
, OPT_Wpedantic
,
7914 "ISO C forbids const or volatile function types");
7916 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
7917 orig_qual_indirect
);
7921 if (pedantic
&& decl_context
== FIELD
7922 && c_type_variably_modified_p (type
))
7925 pedwarn (loc
, OPT_Wpedantic
, "a member of a structure or union cannot "
7926 "have a variably modified type");
7929 /* Aside from typedefs and type names (handle above),
7930 `void' at top level (not within pointer)
7931 is allowed only in public variables.
7932 We don't complain about parms either, but that is because
7933 a better error message can be made later. */
7935 if (VOID_TYPE_P (type
) && decl_context
!= PARM
7936 && !((decl_context
!= FIELD
&& TREE_CODE (type
) != FUNCTION_TYPE
)
7937 && (storage_class
== csc_extern
7938 || (current_scope
== file_scope
7939 && !(storage_class
== csc_static
7940 || storage_class
== csc_register
)))))
7942 error_at (loc
, "variable or field %qE declared void", name
);
7943 type
= integer_type_node
;
7946 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7947 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
7952 if (decl_context
== PARM
)
7955 bool array_parameter_p
= false;
7957 /* A parameter declared as an array of T is really a pointer to T.
7958 One declared as a function is really a pointer to a function. */
7960 if (TREE_CODE (type
) == ARRAY_TYPE
)
7962 /* Transfer const-ness of array into that of type pointed to. */
7963 type
= TREE_TYPE (type
);
7964 if (orig_qual_type
!= NULL_TREE
)
7966 if (orig_qual_indirect
== 0)
7967 orig_qual_type
= TREE_TYPE (orig_qual_type
);
7969 orig_qual_indirect
--;
7972 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
7973 orig_qual_indirect
);
7975 /* The pointed-to type may need a decl expr (see above). */
7976 if (!TYPE_NAME (type
) && c_type_variably_modified_p (type
))
7978 bool bind_p
= decl_context
== TYPENAME
7979 || decl_context
== FIELD
7980 || decl_context
== PARM
;
7981 add_decl_expr (loc
, type
, bind_p
? expr
: NULL
, true);
7984 type
= c_build_pointer_type (type
);
7985 type_quals
= array_ptr_quals
;
7987 type
= c_build_qualified_type (type
, type_quals
);
7989 /* We don't yet implement attributes in this context. */
7990 if (array_ptr_attrs
!= NULL_TREE
)
7991 warning_at (loc
, OPT_Wattributes
,
7992 "attributes in parameter array declarator ignored");
7994 size_varies
= false;
7995 array_parameter_p
= true;
7997 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
7999 if (type_quals
& TYPE_QUAL_ATOMIC
)
8002 "%<_Atomic%>-qualified function type");
8003 type_quals
&= ~TYPE_QUAL_ATOMIC
;
8005 else if (type_quals
)
8006 pedwarn (loc
, OPT_Wpedantic
,
8007 "ISO C forbids qualified function types");
8009 type
= c_build_qualified_type (type
, type_quals
);
8010 type
= c_build_pointer_type (type
);
8011 type_quals
= TYPE_UNQUALIFIED
;
8013 else if (type_quals
)
8014 type
= c_build_qualified_type (type
, type_quals
);
8016 decl
= build_decl (declarator
->id_loc
,
8017 PARM_DECL
, declarator
->u
.id
.id
, type
);
8019 C_DECL_VARIABLE_SIZE (decl
) = 1;
8020 C_ARRAY_PARAMETER (decl
) = array_parameter_p
;
8022 /* Compute the type actually passed in the parmlist,
8023 for the case where there is no prototype.
8024 (For example, shorts and chars are passed as ints.)
8025 When there is a prototype, this is overridden later. */
8027 if (type
== error_mark_node
)
8028 promoted_type
= type
;
8030 promoted_type
= c_type_promotes_to (type
);
8032 DECL_ARG_TYPE (decl
) = promoted_type
;
8033 if (declspecs
->inline_p
)
8034 pedwarn (loc
, 0, "parameter %q+D declared %<inline%>", decl
);
8035 if (declspecs
->noreturn_p
)
8036 pedwarn (loc
, 0, "parameter %q+D declared %<_Noreturn%>", decl
);
8038 else if (decl_context
== FIELD
)
8040 /* Note that the grammar rejects storage classes in typenames
8042 gcc_assert (storage_class
== csc_none
&& !threadp
8043 && !declspecs
->inline_p
&& !declspecs
->noreturn_p
);
8045 /* Structure field. It may not be a function. */
8047 if (TREE_CODE (type
) == FUNCTION_TYPE
)
8049 error_at (loc
, "field %qE declared as a function", name
);
8050 type
= build_pointer_type (type
);
8052 else if (TREE_CODE (type
) != ERROR_MARK
8053 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type
))
8056 error_at (loc
, "field %qE has incomplete type", name
);
8058 error_at (loc
, "unnamed field has incomplete type");
8059 type
= error_mark_node
;
8061 else if (TREE_CODE (type
) == ARRAY_TYPE
8062 && TYPE_DOMAIN (type
) == NULL_TREE
)
8064 /* We have a flexible array member through a typedef.
8065 Set suitable range. Whether this is a correct position
8066 for a flexible array member will be determined elsewhere. */
8067 if (!in_system_header_at (input_location
))
8068 pedwarn_c90 (loc
, OPT_Wpedantic
, "ISO C90 does not "
8069 "support flexible array members");
8070 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
8071 TYPE_DOMAIN (type
) = build_range_type (sizetype
, size_zero_node
,
8073 if (orig_qual_indirect
== 0)
8074 orig_qual_type
= NULL_TREE
;
8076 if (type
!= error_mark_node
8077 && !verify_type_context (loc
, TCTX_FIELD
, type
))
8078 type
= error_mark_node
;
8080 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
8081 orig_qual_indirect
);
8082 decl
= build_decl (declarator
->id_loc
,
8083 FIELD_DECL
, declarator
->u
.id
.id
, type
);
8084 DECL_NONADDRESSABLE_P (decl
) = bitfield
;
8085 if (bitfield
&& !declarator
->u
.id
.id
)
8086 DECL_PADDING_P (decl
) = 1;
8089 C_DECL_VARIABLE_SIZE (decl
) = 1;
8091 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
8093 if (storage_class
== csc_register
|| threadp
|| constexprp
)
8095 error_at (loc
, "invalid storage class for function %qE", name
);
8097 else if (current_scope
!= file_scope
)
8099 /* Function declaration not at file scope. Storage
8100 classes other than `extern' are not allowed, C99
8101 6.7.1p5, and `extern' makes no difference. However,
8102 GCC allows 'auto', perhaps with 'inline', to support
8103 nested functions. */
8104 if (storage_class
== csc_auto
)
8105 pedwarn (loc
, OPT_Wpedantic
,
8106 "invalid storage class for function %qE", name
);
8107 else if (storage_class
== csc_static
)
8109 error_at (loc
, "invalid storage class for function %qE", name
);
8111 storage_class
= declspecs
->storage_class
= csc_none
;
8117 decl
= build_decl (declarator
->id_loc
,
8118 FUNCTION_DECL
, declarator
->u
.id
.id
, type
);
8119 decl
= build_decl_attribute_variant (decl
, decl_attr
);
8121 if (type_quals
& TYPE_QUAL_ATOMIC
)
8124 "%<_Atomic%>-qualified function type");
8125 type_quals
&= ~TYPE_QUAL_ATOMIC
;
8127 else if (pedantic
&& type_quals
&& !DECL_IN_SYSTEM_HEADER (decl
))
8128 pedwarn (loc
, OPT_Wpedantic
,
8129 "ISO C forbids qualified function types");
8131 /* Every function declaration is an external reference
8132 (DECL_EXTERNAL) except for those which are not at file
8133 scope and are explicitly declared "auto". This is
8134 forbidden by standard C (C99 6.7.1p5) and is interpreted by
8135 GCC to signify a forward declaration of a nested function. */
8136 if (storage_class
== csc_auto
&& current_scope
!= file_scope
)
8137 DECL_EXTERNAL (decl
) = 0;
8138 /* In C99, a function which is declared 'inline' with 'extern'
8139 is not an external reference (which is confusing). It
8140 means that the later definition of the function must be output
8141 in this file, C99 6.7.4p6. In GNU C89, a function declared
8142 'extern inline' is an external reference. */
8143 else if (declspecs
->inline_p
&& storage_class
!= csc_static
)
8144 DECL_EXTERNAL (decl
) = ((storage_class
== csc_extern
)
8145 == flag_gnu89_inline
);
8147 DECL_EXTERNAL (decl
) = !initialized
;
8149 /* Record absence of global scope for `static' or `auto'. */
8151 = !(storage_class
== csc_static
|| storage_class
== csc_auto
);
8153 /* For a function definition, record the argument information
8154 block where store_parm_decls will look for it. */
8156 current_function_arg_info
= arg_info
;
8158 if (declspecs
->default_int_p
)
8159 C_FUNCTION_IMPLICIT_INT (decl
) = 1;
8161 /* Record presence of `inline' and `_Noreturn', if it is
8163 if (flag_hosted
&& MAIN_NAME_P (declarator
->u
.id
.id
))
8165 if (declspecs
->inline_p
)
8166 pedwarn (loc
, 0, "cannot inline function %<main%>");
8167 if (declspecs
->noreturn_p
)
8168 pedwarn (loc
, 0, "%<main%> declared %<_Noreturn%>");
8172 if (declspecs
->inline_p
)
8173 /* Record that the function is declared `inline'. */
8174 DECL_DECLARED_INLINE_P (decl
) = 1;
8175 if (declspecs
->noreturn_p
)
8178 pedwarn_c99 (loc
, OPT_Wpedantic
,
8179 "ISO C99 does not support %<_Noreturn%>");
8181 pedwarn_c99 (loc
, OPT_Wpedantic
,
8182 "ISO C90 does not support %<_Noreturn%>");
8183 TREE_THIS_VOLATILE (decl
) = 1;
8187 /* C99 6.2.2p7: It is invalid (compile-time undefined
8188 behavior) to create an 'extern' declaration for a
8189 function if there is a global declaration that is
8190 'static' and the global declaration is not visible.
8191 (If the static declaration _is_ currently visible,
8192 the 'extern' declaration is taken to refer to that decl.) */
8194 && TREE_PUBLIC (decl
)
8195 && current_scope
!= file_scope
)
8197 tree global_decl
= identifier_global_value (declarator
->u
.id
.id
);
8198 tree visible_decl
= lookup_name (declarator
->u
.id
.id
);
8201 && global_decl
!= visible_decl
8202 && VAR_OR_FUNCTION_DECL_P (global_decl
)
8203 && !TREE_PUBLIC (global_decl
))
8204 error_at (loc
, "function previously declared %<static%> "
8205 "redeclared %<extern%>");
8210 /* It's a variable. */
8211 /* An uninitialized decl with `extern' is a reference. */
8212 int extern_ref
= !initialized
&& storage_class
== csc_extern
;
8216 /* The type of a constexpr variable must not be variably
8217 modified, volatile, atomic or restrict qualified or
8218 have a member with such a qualifier. const
8219 qualification is implicitly added, and, at file scope,
8220 has internal linkage. */
8221 if (c_type_variably_modified_p (type
))
8222 error_at (loc
, "%<constexpr%> object has variably modified "
8225 & (TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
| TYPE_QUAL_ATOMIC
))
8226 error_at (loc
, "invalid qualifiers for %<constexpr%> object");
8229 tree type_no_array
= strip_array_types (type
);
8230 if (RECORD_OR_UNION_TYPE_P (type_no_array
)
8231 && C_TYPE_FIELDS_NON_CONSTEXPR (type_no_array
))
8232 error_at (loc
, "invalid qualifiers for field of "
8233 "%<constexpr%> object");
8235 type_quals
|= TYPE_QUAL_CONST
;
8236 if (current_scope
== file_scope
)
8237 storage_class
= csc_static
;
8240 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
8241 orig_qual_indirect
);
8243 /* C99 6.2.2p7: It is invalid (compile-time undefined
8244 behavior) to create an 'extern' declaration for a
8245 variable if there is a global declaration that is
8246 'static' and the global declaration is not visible.
8247 (If the static declaration _is_ currently visible,
8248 the 'extern' declaration is taken to refer to that decl.) */
8249 if (extern_ref
&& current_scope
!= file_scope
)
8251 tree global_decl
= identifier_global_value (declarator
->u
.id
.id
);
8252 tree visible_decl
= lookup_name (declarator
->u
.id
.id
);
8255 && global_decl
!= visible_decl
8256 && VAR_P (global_decl
)
8257 && !TREE_PUBLIC (global_decl
))
8258 error_at (loc
, "variable previously declared %<static%> "
8259 "redeclared %<extern%>");
8262 decl
= build_decl (declarator
->id_loc
,
8263 VAR_DECL
, declarator
->u
.id
.id
, type
);
8265 C_DECL_VARIABLE_SIZE (decl
) = 1;
8267 C_DECL_DECLARED_CONSTEXPR (decl
) = 1;
8269 if (declspecs
->inline_p
)
8270 pedwarn (loc
, 0, "variable %q+D declared %<inline%>", decl
);
8271 if (declspecs
->noreturn_p
)
8272 pedwarn (loc
, 0, "variable %q+D declared %<_Noreturn%>", decl
);
8274 /* At file scope, an initialized extern declaration may follow
8275 a static declaration. In that case, DECL_EXTERNAL will be
8276 reset later in start_decl. */
8277 DECL_EXTERNAL (decl
) = (storage_class
== csc_extern
);
8279 /* At file scope, the presence of a `static' or `register' storage
8280 class specifier, or the absence of all storage class specifiers
8281 makes this declaration a definition (perhaps tentative). Also,
8282 the absence of `static' makes it public. */
8283 if (current_scope
== file_scope
)
8285 TREE_PUBLIC (decl
) = storage_class
!= csc_static
;
8286 TREE_STATIC (decl
) = !extern_ref
;
8288 /* Not at file scope, only `static' makes a static definition. */
8291 TREE_STATIC (decl
) = (storage_class
== csc_static
);
8292 TREE_PUBLIC (decl
) = extern_ref
;
8296 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
8299 if ((storage_class
== csc_extern
8300 || (storage_class
== csc_none
8301 && TREE_CODE (type
) == FUNCTION_TYPE
8303 && c_type_variably_modified_p (type
))
8306 if (TREE_CODE (type
) == FUNCTION_TYPE
)
8307 error_at (loc
, "non-nested function with variably modified type");
8309 error_at (loc
, "object with variably modified type must have "
8313 /* For nested functions disqualify ones taking VLAs by value
8314 from inlining since the middle-end cannot deal with this.
8315 ??? We should arrange for those to be passed by reference
8316 with emitting the copy on the caller side in the frontend. */
8317 if (storage_class
== csc_none
8318 && TREE_CODE (type
) == FUNCTION_TYPE
)
8319 for (tree al
= TYPE_ARG_TYPES (type
); al
; al
= TREE_CHAIN (al
))
8321 tree arg
= TREE_VALUE (al
);
8322 if (arg
!= error_mark_node
8323 && C_TYPE_VARIABLE_SIZE (arg
))
8325 DECL_UNINLINABLE (decl
) = 1;
8330 /* Record `register' declaration for warnings on &
8331 and in case doing stupid register allocation. */
8333 if (storage_class
== csc_register
)
8335 C_DECL_REGISTER (decl
) = 1;
8336 DECL_REGISTER (decl
) = 1;
8339 /* Record constancy and volatility. */
8340 c_apply_type_quals_to_decl (type_quals
, decl
);
8342 /* Apply _Alignas specifiers. */
8345 SET_DECL_ALIGN (decl
, alignas_align
* BITS_PER_UNIT
);
8346 DECL_USER_ALIGN (decl
) = 1;
8349 /* If a type has volatile components, it should be stored in memory.
8350 Otherwise, the fact that those components are volatile
8351 will be ignored, and would even crash the compiler.
8352 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
8353 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl
))
8354 && (VAR_P (decl
) || TREE_CODE (decl
) == PARM_DECL
8355 || TREE_CODE (decl
) == RESULT_DECL
))
8357 /* It is not an error for a structure with volatile fields to
8358 be declared register, but reset DECL_REGISTER since it
8359 cannot actually go in a register. */
8360 int was_reg
= C_DECL_REGISTER (decl
);
8361 C_DECL_REGISTER (decl
) = 0;
8362 DECL_REGISTER (decl
) = 0;
8363 c_mark_addressable (decl
);
8364 C_DECL_REGISTER (decl
) = was_reg
;
8367 /* This is the earliest point at which we might know the assembler
8368 name of a variable. Thus, if it's known before this, die horribly. */
8369 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl
)
8370 || !DECL_ASSEMBLER_NAME_SET_P (decl
));
8374 && TREE_PUBLIC (decl
)
8375 && TREE_STATIC (decl
)
8376 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
))
8377 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
8378 && TYPE_NAME (TREE_TYPE (decl
)) == NULL_TREE
)
8379 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
8380 ("non-local variable %qD with anonymous type is "
8381 "questionable in C++"),
8388 /* Decode the parameter-list info for a function type or function definition.
8389 The argument is the value returned by `get_parm_info' (or made in c-parse.c
8390 if there is an identifier list instead of a parameter decl list).
8391 These two functions are separate because when a function returns
8392 or receives functions then each is called multiple times but the order
8393 of calls is different. The last call to `grokparms' is always the one
8394 that contains the formal parameter names of a function definition.
8396 Return a list of arg types to use in the FUNCTION_TYPE for this function.
8398 FUNCDEF_FLAG is true for a function definition, false for
8399 a mere declaration. A nonempty identifier-list gets an error message
8400 when FUNCDEF_FLAG is false. */
8403 grokparms (struct c_arg_info
*arg_info
, bool funcdef_flag
)
8405 tree arg_types
= arg_info
->types
;
8407 if (funcdef_flag
&& arg_info
->had_vla_unspec
)
8409 /* A function definition isn't function prototype scope C99 6.2.1p4. */
8411 error ("%<[*]%> not allowed in other than function prototype scope");
8414 if (arg_types
== NULL_TREE
&& !funcdef_flag
&& !flag_isoc23
8415 && !in_system_header_at (input_location
))
8416 warning (OPT_Wstrict_prototypes
,
8417 "function declaration isn%'t a prototype");
8419 if (arg_types
== error_mark_node
)
8420 /* Don't set TYPE_ARG_TYPES in this case. */
8423 else if (arg_types
&& TREE_CODE (TREE_VALUE (arg_types
)) == IDENTIFIER_NODE
)
8427 permerror_opt (input_location
,
8428 OPT_Wdeclaration_missing_parameter_type
,
8429 "parameter names (without types) in "
8430 "function declaration");
8431 arg_info
->parms
= NULL_TREE
;
8434 arg_info
->parms
= arg_info
->types
;
8436 arg_info
->types
= NULL_TREE
;
8441 tree parm
, type
, typelt
;
8442 unsigned int parmno
;
8444 /* In C23, convert () to (void). */
8448 && !arg_info
->no_named_args_stdarg_p
)
8449 arg_types
= arg_info
->types
= void_list_node
;
8451 /* If there is a parameter of incomplete type in a definition,
8452 this is an error. In a declaration this is valid, and a
8453 struct or union type may be completed later, before any calls
8454 or definition of the function. In the case where the tag was
8455 first declared within the parameter list, a warning has
8456 already been given. If a parameter has void type, then
8457 however the function cannot be defined or called, so
8460 for (parm
= arg_info
->parms
, typelt
= arg_types
, parmno
= 1;
8462 parm
= DECL_CHAIN (parm
), typelt
= TREE_CHAIN (typelt
), parmno
++)
8464 type
= TREE_VALUE (typelt
);
8465 if (type
== error_mark_node
)
8468 if (!COMPLETE_TYPE_P (type
))
8472 if (DECL_NAME (parm
))
8473 error_at (input_location
,
8474 "parameter %u (%q+D) has incomplete type",
8477 error_at (DECL_SOURCE_LOCATION (parm
),
8478 "parameter %u has incomplete type",
8481 TREE_VALUE (typelt
) = error_mark_node
;
8482 TREE_TYPE (parm
) = error_mark_node
;
8483 arg_types
= NULL_TREE
;
8485 else if (VOID_TYPE_P (type
))
8487 if (DECL_NAME (parm
))
8488 warning_at (input_location
, 0,
8489 "parameter %u (%q+D) has void type",
8492 warning_at (DECL_SOURCE_LOCATION (parm
), 0,
8493 "parameter %u has void type",
8498 if (DECL_NAME (parm
) && TREE_USED (parm
))
8499 warn_if_shadowing (parm
);
8505 /* Allocate and initialize a c_arg_info structure from the parser's
8509 build_arg_info (void)
8511 struct c_arg_info
*ret
= XOBNEW (&parser_obstack
, struct c_arg_info
);
8512 ret
->parms
= NULL_TREE
;
8514 ret
->types
= NULL_TREE
;
8515 ret
->others
= NULL_TREE
;
8516 ret
->pending_sizes
= NULL
;
8517 ret
->had_vla_unspec
= 0;
8518 ret
->no_named_args_stdarg_p
= 0;
8522 /* Take apart the current scope and return a c_arg_info structure with
8523 info on a parameter list just parsed.
8525 This structure is later fed to 'grokparms' and 'store_parm_decls'.
8527 ELLIPSIS being true means the argument list ended in '...' so don't
8528 append a sentinel (void_list_node) to the end of the type-list.
8530 EXPR is NULL or an expression that needs to be evaluated for the
8531 side effects of array size expressions in the parameters. */
8534 get_parm_info (bool ellipsis
, tree expr
)
8536 struct c_binding
*b
= current_scope
->bindings
;
8537 struct c_arg_info
*arg_info
= build_arg_info ();
8539 tree parms
= NULL_TREE
;
8540 vec
<c_arg_tag
, va_gc
> *tags
= NULL
;
8541 tree types
= NULL_TREE
;
8542 tree others
= NULL_TREE
;
8544 bool gave_void_only_once_err
= false;
8546 arg_info
->had_vla_unspec
= current_scope
->had_vla_unspec
;
8548 /* The bindings in this scope must not get put into a block.
8549 We will take care of deleting the binding nodes. */
8550 current_scope
->bindings
= 0;
8552 /* This function is only called if there was *something* on the
8556 /* A parameter list consisting solely of 'void' indicates that the
8557 function takes no arguments. But if the 'void' is qualified
8558 (by 'const' or 'volatile'), or has a storage class specifier
8559 ('register'), then the behavior is undefined; issue an error.
8560 Typedefs for 'void' are OK (see DR#157). */
8561 if (b
->prev
== 0 /* one binding */
8562 && TREE_CODE (b
->decl
) == PARM_DECL
/* which is a parameter */
8563 && !DECL_NAME (b
->decl
) /* anonymous */
8564 && VOID_TYPE_P (TREE_TYPE (b
->decl
))) /* of void type */
8566 if (TYPE_QUALS (TREE_TYPE (b
->decl
)) != TYPE_UNQUALIFIED
8567 || C_DECL_REGISTER (b
->decl
))
8568 error_at (b
->locus
, "%<void%> as only parameter may not be qualified");
8570 /* There cannot be an ellipsis. */
8572 error_at (b
->locus
, "%<void%> must be the only parameter");
8574 arg_info
->types
= void_list_node
;
8579 types
= void_list_node
;
8581 /* Break up the bindings list into parms, tags, types, and others;
8582 apply sanity checks; purge the name-to-decl bindings. */
8585 tree decl
= b
->decl
;
8586 tree type
= TREE_TYPE (decl
);
8588 const char *keyword
;
8590 switch (TREE_CODE (decl
))
8595 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
8596 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
8599 /* Check for forward decls that never got their actual decl. */
8600 if (TREE_ASM_WRITTEN (decl
))
8602 "parameter %q+D has just a forward declaration", decl
);
8603 /* Check for (..., void, ...) and issue an error. */
8604 else if (VOID_TYPE_P (type
) && !DECL_NAME (decl
))
8606 if (!gave_void_only_once_err
)
8608 error_at (b
->locus
, "%<void%> must be the only parameter");
8609 gave_void_only_once_err
= true;
8614 /* Valid parameter, add it to the list. */
8615 DECL_CHAIN (decl
) = parms
;
8618 /* Since there is a prototype, args are passed in their
8619 declared types. The back end may override this later. */
8620 DECL_ARG_TYPE (decl
) = type
;
8621 types
= tree_cons (0, type
, types
);
8625 case ENUMERAL_TYPE
: keyword
= "enum"; goto tag
;
8626 case UNION_TYPE
: keyword
= "union"; goto tag
;
8627 case RECORD_TYPE
: keyword
= "struct"; goto tag
;
8629 /* Types may not have tag-names, in which case the type
8630 appears in the bindings list with b->id NULL. */
8633 gcc_assert (I_TAG_BINDING (b
->id
) == b
);
8634 I_TAG_BINDING (b
->id
) = b
->shadowed
;
8637 /* Warn about any struct, union or enum tags defined in a
8638 parameter list. The scope of such types is limited to
8639 the parameter list, which is rarely if ever desirable
8640 (it's impossible to call such a function with type-
8641 correct arguments). An anonymous union parm type is
8642 meaningful as a GNU extension, so don't warn for that. */
8643 if (TREE_CODE (decl
) != UNION_TYPE
|| b
->id
!= NULL_TREE
)
8647 /* The %s will be one of 'struct', 'union', or 'enum'. */
8649 warning_at (b
->locus
, 0,
8650 "%<%s %E%> declared inside parameter list"
8651 " will not be visible outside of this definition or"
8652 " declaration", keyword
, b
->id
);
8655 /* The %s will be one of 'struct', 'union', or 'enum'. */
8656 warning_at (b
->locus
, 0,
8657 "anonymous %s declared inside parameter list"
8658 " will not be visible outside of this definition or"
8659 " declaration", keyword
);
8664 vec_safe_push (tags
, tag
);
8668 /* FUNCTION_DECLs appear when there is an implicit function
8669 declaration in the parameter list. */
8670 gcc_assert (b
->nested
|| seen_error ());
8675 /* CONST_DECLs appear here when we have an embedded enum,
8676 and TYPE_DECLs appear here when we have an embedded struct
8677 or union. No warnings for this - we already warned about the
8680 /* When we reinsert this decl in the function body, we need
8681 to reconstruct whether it was marked as nested. */
8682 gcc_assert (!b
->nested
);
8683 DECL_CHAIN (decl
) = others
;
8689 /* error_mark_node appears here when we have an undeclared
8690 variable. Just throw it away. */
8693 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
8694 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
8698 /* Other things that might be encountered. */
8705 b
= free_binding_and_advance (b
);
8708 arg_info
->parms
= parms
;
8709 arg_info
->tags
= tags
;
8710 arg_info
->types
= types
;
8711 arg_info
->others
= others
;
8712 arg_info
->pending_sizes
= expr
;
8713 arg_info
->no_named_args_stdarg_p
= ellipsis
&& !types
;
8717 /* Get the struct, enum or union (CODE says which) with tag NAME.
8718 Define the tag as a forward-reference with location LOC if it is
8719 not defined. HAVE_STD_ATTRS says whether any standard attributes
8720 were present after the struct, union or enum keyword; ATTRS are the
8721 standard attributes present there. HAS_ENUM_TYPE_SPECIFIER says
8722 whether an enum type specifier (": specifier-qualifier-list") is
8723 present; if so, this is called before that specifier is parsed, so
8724 that the tag is in scope for that specifier. Return a c_typespec
8725 structure for the type specifier. */
8728 parser_xref_tag (location_t loc
, enum tree_code code
, tree name
,
8729 bool have_std_attrs
, tree attrs
, bool has_enum_type_specifier
)
8731 struct c_typespec ret
;
8735 ret
.expr
= NULL_TREE
;
8736 ret
.expr_const_operands
= true;
8737 ret
.has_enum_type_specifier
= has_enum_type_specifier
;
8739 /* If a cross reference is requested, look up the type already
8740 defined for this tag and return it. If an enum type specifier is
8741 present, only a definition in the current scope is relevant. */
8743 ref
= lookup_tag (code
, name
, has_enum_type_specifier
, &refloc
);
8745 /* If the visble type is still being defined, see if there is
8746 an earlier definition (which may be complete). We do not
8747 have to loop because nested redefinitions are not allowed. */
8748 if (flag_isoc23
&& ref
&& C_TYPE_BEING_DEFINED (ref
))
8750 tree vis
= previous_tag (ref
);
8755 /* If this is the right type of tag, return what we found.
8756 (This reference will be shadowed by shadow_tag later if appropriate.)
8757 If this is the wrong type of tag, do not return it. If it was the
8758 wrong type in the same scope, we will have had an error
8759 message already; if in a different scope and declaring
8760 a name, pending_xref_error will give an error message; but if in a
8761 different scope and not declaring a name, this tag should
8762 shadow the previous declaration of a different type of tag, and
8763 this would not work properly if we return the reference found.
8764 (For example, with "struct foo" in an outer scope, "union foo;"
8765 must shadow that tag with a new one of union type.) */
8767 ? (have_std_attrs
? ctsk_tagref_attrs
: ctsk_tagref
)
8768 : (have_std_attrs
? ctsk_tagfirstref_attrs
: ctsk_tagfirstref
));
8769 if (ref
&& TREE_CODE (ref
) == code
)
8771 decl_attributes (&ref
, attrs
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
8772 if (C_TYPE_DEFINED_IN_STRUCT (ref
)
8773 && loc
!= UNKNOWN_LOCATION
8776 auto_diagnostic_group d
;
8780 if (warning_at (loc
, OPT_Wc___compat
,
8781 ("enum type defined in struct or union "
8782 "is not visible in C++")))
8783 inform (refloc
, "enum type defined here");
8786 if (warning_at (loc
, OPT_Wc___compat
,
8787 ("struct defined in struct or union "
8788 "is not visible in C++")))
8789 inform (refloc
, "struct defined here");
8792 if (warning_at (loc
, OPT_Wc___compat
,
8793 ("union defined in struct or union "
8794 "is not visible in C++")))
8795 inform (refloc
, "union defined here");
8806 /* If no such tag is yet defined, create a forward-reference node
8807 and record it as the "definition".
8808 When a real declaration of this type is found,
8809 the forward-reference will be altered into a real type. */
8811 ref
= make_node (code
);
8812 if (code
== ENUMERAL_TYPE
)
8814 /* Give the type a default layout like unsigned int
8815 to avoid crashing if it does not get defined. */
8816 SET_TYPE_MODE (ref
, TYPE_MODE (unsigned_type_node
));
8817 SET_TYPE_ALIGN (ref
, TYPE_ALIGN (unsigned_type_node
));
8818 TYPE_USER_ALIGN (ref
) = 0;
8819 TYPE_UNSIGNED (ref
) = 1;
8820 TYPE_PRECISION (ref
) = TYPE_PRECISION (unsigned_type_node
);
8821 TYPE_MIN_VALUE (ref
) = TYPE_MIN_VALUE (unsigned_type_node
);
8822 TYPE_MAX_VALUE (ref
) = TYPE_MAX_VALUE (unsigned_type_node
);
8823 ENUM_FIXED_UNDERLYING_TYPE_P (ref
) = has_enum_type_specifier
;
8826 pushtag (loc
, name
, ref
);
8827 decl_attributes (&ref
, attrs
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
8828 if (in_underspecified_init
)
8829 error_at (loc
, "%qT declared in underspecified object initializer",
8836 /* Get the struct, enum or union (CODE says which) with tag NAME.
8837 Define the tag as a forward-reference if it is not defined.
8838 Return a tree for the type. */
8841 xref_tag (enum tree_code code
, tree name
)
8843 return parser_xref_tag (input_location
, code
, name
, false, NULL_TREE
,
8847 /* Make sure that the tag NAME is defined *in the current scope*
8848 at least as a forward reference.
8849 LOC is the location of the struct's definition.
8850 CODE says which kind of tag NAME ought to be.
8852 This stores the current value of the file static STRUCT_PARSE_INFO
8853 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8854 new c_struct_parse_info structure. The old value of
8855 STRUCT_PARSE_INFO is restored in finish_struct. */
8858 start_struct (location_t loc
, enum tree_code code
, tree name
,
8859 class c_struct_parse_info
**enclosing_struct_parse_info
)
8861 /* If there is already a tag defined at this scope
8862 (as a forward reference), just return it. */
8864 tree ref
= NULL_TREE
;
8865 location_t refloc
= UNKNOWN_LOCATION
;
8867 if (name
!= NULL_TREE
)
8868 ref
= lookup_tag (code
, name
, true, &refloc
);
8870 /* For C23, even if we already have a completed definition,
8871 we do not use it. We will check for consistency later.
8872 If we are in a nested redefinition the type is not
8873 complete. We will then detect this below. */
8874 if (flag_isoc23
&& ref
&& TYPE_SIZE (ref
))
8877 if (ref
&& TREE_CODE (ref
) == code
)
8879 if (TYPE_STUB_DECL (ref
))
8880 refloc
= DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref
));
8882 if (TYPE_SIZE (ref
))
8884 auto_diagnostic_group d
;
8885 if (code
== UNION_TYPE
)
8886 error_at (loc
, "redefinition of %<union %E%>", name
);
8888 error_at (loc
, "redefinition of %<struct %E%>", name
);
8889 if (refloc
!= UNKNOWN_LOCATION
)
8890 inform (refloc
, "originally defined here");
8891 /* Don't create structures using a name already in use. */
8894 else if (C_TYPE_BEING_DEFINED (ref
))
8896 if (code
== UNION_TYPE
)
8897 error_at (loc
, "nested redefinition of %<union %E%>", name
);
8899 error_at (loc
, "nested redefinition of %<struct %E%>", name
);
8900 /* Don't bother to report "originally defined here" for a
8901 nested redefinition; the original definition should be
8903 /* Don't create structures that contain themselves. */
8908 /* Otherwise create a forward-reference just so the tag is in scope. */
8910 if (ref
== NULL_TREE
|| TREE_CODE (ref
) != code
)
8912 ref
= make_node (code
);
8913 pushtag (loc
, name
, ref
);
8916 C_TYPE_BEING_DEFINED (ref
) = 1;
8917 for (tree v
= TYPE_MAIN_VARIANT (ref
); v
; v
= TYPE_NEXT_VARIANT (v
))
8918 TYPE_PACKED (v
) = flag_pack_struct
;
8920 *enclosing_struct_parse_info
= struct_parse_info
;
8921 struct_parse_info
= new c_struct_parse_info ();
8923 /* FIXME: This will issue a warning for a use of a type defined
8924 within a statement expr used within sizeof, et. al. This is not
8925 terribly serious as C++ doesn't permit statement exprs within
8927 if (warn_cxx_compat
&& (in_sizeof
|| in_typeof
|| in_alignof
))
8928 warning_at (loc
, OPT_Wc___compat
,
8929 "defining type in %qs expression is invalid in C++",
8932 : (in_typeof
? "typeof" : "alignof")));
8934 if (in_underspecified_init
)
8935 error_at (loc
, "%qT defined in underspecified object initializer", ref
);
8940 /* Process the specs, declarator and width (NULL if omitted)
8941 of a structure component, returning a FIELD_DECL node.
8942 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
8943 DECL_ATTRS is as for grokdeclarator.
8945 LOC is the location of the structure component.
8947 This is done during the parsing of the struct declaration.
8948 The FIELD_DECL nodes are chained together and the lot of them
8949 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
8952 grokfield (location_t loc
,
8953 struct c_declarator
*declarator
, struct c_declspecs
*declspecs
,
8954 tree width
, tree
*decl_attrs
, tree
*expr
)
8958 if (declarator
->kind
== cdk_id
&& declarator
->u
.id
.id
== NULL_TREE
8959 && width
== NULL_TREE
)
8961 /* This is an unnamed decl.
8963 If we have something of the form "union { list } ;" then this
8964 is the anonymous union extension. Similarly for struct.
8966 If this is something of the form "struct foo;", then
8967 If MS or Plan 9 extensions are enabled, this is handled as
8968 an anonymous struct.
8969 Otherwise this is a forward declaration of a structure tag.
8971 If this is something of the form "foo;" and foo is a TYPE_DECL, then
8972 If foo names a structure or union without a tag, then this
8973 is an anonymous struct (this is permitted by C11).
8974 If MS or Plan 9 extensions are enabled and foo names a
8975 structure, then again this is an anonymous struct.
8976 Otherwise this is an error.
8978 Oh what a horrid tangled web we weave. I wonder if MS consciously
8979 took this from Plan 9 or if it was an accident of implementation
8980 that took root before someone noticed the bug... */
8982 tree type
= declspecs
->type
;
8985 if (RECORD_OR_UNION_TYPE_P (type
)
8986 && (flag_ms_extensions
8987 || flag_plan9_extensions
8988 || !declspecs
->typedef_p
))
8990 if (flag_ms_extensions
|| flag_plan9_extensions
)
8992 else if (TYPE_NAME (type
) == NULL
)
8999 pedwarn (loc
, 0, "declaration does not declare anything");
9003 pedwarn_c99 (loc
, OPT_Wpedantic
,
9004 "ISO C99 doesn%'t support unnamed structs/unions");
9006 pedwarn_c99 (loc
, OPT_Wpedantic
,
9007 "ISO C90 doesn%'t support unnamed structs/unions");
9010 value
= grokdeclarator (declarator
, declspecs
, FIELD
, false,
9011 width
? &width
: NULL
, decl_attrs
, expr
, NULL
,
9014 finish_decl (value
, loc
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
9015 DECL_INITIAL (value
) = width
;
9017 SET_DECL_C_BIT_FIELD (value
);
9019 if (warn_cxx_compat
&& DECL_NAME (value
) != NULL_TREE
)
9021 /* If we currently have a binding for this field, set the
9022 in_struct field in the binding, so that we warn about lookups
9024 struct c_binding
*b
= I_SYMBOL_BINDING (DECL_NAME (value
));
9027 /* If the in_struct field is not yet set, push it on a list
9028 to be cleared when this struct is finished. */
9031 struct_parse_info
->fields
.safe_push (b
);
9040 /* Subroutine of detect_field_duplicates: return whether X and Y,
9041 which are both fields in the same struct, have duplicate field
9045 is_duplicate_field (tree x
, tree y
)
9047 if (DECL_NAME (x
) != NULL_TREE
&& DECL_NAME (x
) == DECL_NAME (y
))
9050 /* When using -fplan9-extensions, an anonymous field whose name is a
9051 typedef can duplicate a field name. */
9052 if (flag_plan9_extensions
9053 && (DECL_NAME (x
) == NULL_TREE
|| DECL_NAME (y
) == NULL_TREE
))
9055 tree xt
, xn
, yt
, yn
;
9058 if (DECL_NAME (x
) != NULL_TREE
)
9060 else if (RECORD_OR_UNION_TYPE_P (xt
)
9061 && TYPE_NAME (xt
) != NULL_TREE
9062 && TREE_CODE (TYPE_NAME (xt
)) == TYPE_DECL
)
9063 xn
= DECL_NAME (TYPE_NAME (xt
));
9068 if (DECL_NAME (y
) != NULL_TREE
)
9070 else if (RECORD_OR_UNION_TYPE_P (yt
)
9071 && TYPE_NAME (yt
) != NULL_TREE
9072 && TREE_CODE (TYPE_NAME (yt
)) == TYPE_DECL
)
9073 yn
= DECL_NAME (TYPE_NAME (yt
));
9077 if (xn
!= NULL_TREE
&& xn
== yn
)
9084 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
9085 to HTAB, giving errors for any duplicates. */
9088 detect_field_duplicates_hash (tree fieldlist
,
9089 hash_table
<nofree_ptr_hash
<tree_node
> > *htab
)
9094 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
9095 if ((y
= DECL_NAME (x
)) != NULL_TREE
)
9097 slot
= htab
->find_slot (y
, INSERT
);
9100 error ("duplicate member %q+D", x
);
9101 DECL_NAME (x
) = NULL_TREE
;
9105 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
9107 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x
)), htab
);
9109 /* When using -fplan9-extensions, an anonymous field whose
9110 name is a typedef can duplicate a field name. */
9111 if (flag_plan9_extensions
9112 && TYPE_NAME (TREE_TYPE (x
)) != NULL_TREE
9113 && TREE_CODE (TYPE_NAME (TREE_TYPE (x
))) == TYPE_DECL
)
9115 tree xn
= DECL_NAME (TYPE_NAME (TREE_TYPE (x
)));
9116 slot
= htab
->find_slot (xn
, INSERT
);
9118 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x
)));
9124 /* Generate an error for any duplicate field names in FIELDLIST. Munge
9125 the list such that this does not present a problem later. */
9128 detect_field_duplicates (tree fieldlist
)
9133 /* If the struct is the list of instance variables of an Objective-C
9134 class, then we need to check all the instance variables of
9135 superclasses when checking for duplicates (since you can't have
9136 an instance variable in a subclass with the same name as an
9137 instance variable in a superclass). We pass on this job to the
9138 Objective-C compiler. objc_detect_field_duplicates() will return
9139 false if we are not checking the list of instance variables and
9140 the C frontend should proceed with the standard field duplicate
9141 checks. If we are checking the list of instance variables, the
9142 ObjC frontend will do the check, emit the errors if needed, and
9143 then return true. */
9144 if (c_dialect_objc ())
9145 if (objc_detect_field_duplicates (false))
9148 /* First, see if there are more than "a few" fields.
9149 This is trivially true if there are zero or one fields. */
9150 if (!fieldlist
|| !DECL_CHAIN (fieldlist
))
9155 if (DECL_NAME (x
) == NULL_TREE
9156 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
9159 } while (timeout
> 0 && x
);
9161 /* If there were "few" fields and no anonymous structures or unions,
9162 avoid the overhead of allocating a hash table. Instead just do
9163 the nested traversal thing. */
9166 for (x
= DECL_CHAIN (fieldlist
); x
; x
= DECL_CHAIN (x
))
9167 /* When using -fplan9-extensions, we can have duplicates
9168 between typedef names and fields. */
9170 || (flag_plan9_extensions
9171 && DECL_NAME (x
) == NULL_TREE
9172 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
))
9173 && TYPE_NAME (TREE_TYPE (x
)) != NULL_TREE
9174 && TREE_CODE (TYPE_NAME (TREE_TYPE (x
))) == TYPE_DECL
))
9176 for (y
= fieldlist
; y
!= x
; y
= TREE_CHAIN (y
))
9177 if (is_duplicate_field (y
, x
))
9179 error ("duplicate member %q+D", x
);
9180 DECL_NAME (x
) = NULL_TREE
;
9186 hash_table
<nofree_ptr_hash
<tree_node
> > htab (37);
9187 detect_field_duplicates_hash (fieldlist
, &htab
);
9191 /* Finish up struct info used by -Wc++-compat. */
9194 warn_cxx_compat_finish_struct (tree fieldlist
, enum tree_code code
,
9195 location_t record_loc
)
9199 struct c_binding
*b
;
9201 if (fieldlist
== NULL_TREE
)
9203 if (code
== RECORD_TYPE
)
9204 warning_at (record_loc
, OPT_Wc___compat
,
9205 "empty struct has size 0 in C, size 1 in C++");
9207 warning_at (record_loc
, OPT_Wc___compat
,
9208 "empty union has size 0 in C, size 1 in C++");
9211 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
9212 the current struct. We do this now at the end of the struct
9213 because the flag is used to issue visibility warnings, and we
9214 only want to issue those warnings if the type is referenced
9215 outside of the struct declaration. */
9216 FOR_EACH_VEC_ELT (struct_parse_info
->struct_types
, ix
, x
)
9217 C_TYPE_DEFINED_IN_STRUCT (x
) = 1;
9219 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
9220 typedefs used when declaring fields in this struct. If the name
9221 of any of the fields is also a typedef name then the struct would
9222 not parse in C++, because the C++ lookup rules say that the
9223 typedef name would be looked up in the context of the struct, and
9224 would thus be the field rather than the typedef. */
9225 if (!struct_parse_info
->typedefs_seen
.is_empty ()
9226 && fieldlist
!= NULL_TREE
)
9228 /* Use a hash_set<tree> using the name of the typedef. We can use
9229 a hash_set<tree> because identifiers are interned. */
9230 hash_set
<tree
> tset
;
9232 FOR_EACH_VEC_ELT (struct_parse_info
->typedefs_seen
, ix
, x
)
9233 tset
.add (DECL_NAME (x
));
9235 for (x
= fieldlist
; x
!= NULL_TREE
; x
= DECL_CHAIN (x
))
9237 if (DECL_NAME (x
) != NULL_TREE
9238 && tset
.contains (DECL_NAME (x
)))
9240 warning_at (DECL_SOURCE_LOCATION (x
), OPT_Wc___compat
,
9241 ("using %qD as both field and typedef name is "
9244 /* FIXME: It would be nice to report the location where
9245 the typedef name is used. */
9250 /* For each field which has a binding and which was not defined in
9251 an enclosing struct, clear the in_struct field. */
9252 FOR_EACH_VEC_ELT (struct_parse_info
->fields
, ix
, b
)
9256 /* Function to help qsort sort FIELD_DECLs by name order. */
9259 field_decl_cmp (const void *x_p
, const void *y_p
)
9261 const tree
*const x
= (const tree
*) x_p
;
9262 const tree
*const y
= (const tree
*) y_p
;
9264 if (DECL_NAME (*x
) == DECL_NAME (*y
))
9265 /* A nontype is "greater" than a type. */
9266 return (TREE_CODE (*y
) == TYPE_DECL
) - (TREE_CODE (*x
) == TYPE_DECL
);
9267 if (DECL_NAME (*x
) == NULL_TREE
)
9269 if (DECL_NAME (*y
) == NULL_TREE
)
9271 if (DECL_NAME (*x
) < DECL_NAME (*y
))
9276 /* If this structure or union completes the type of any previous
9277 variable declaration, lay it out and output its rtl. */
9279 finish_incomplete_vars (tree incomplete_vars
, bool toplevel
)
9281 for (tree x
= incomplete_vars
; x
; x
= TREE_CHAIN (x
))
9283 tree decl
= TREE_VALUE (x
);
9284 if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
9285 layout_array_type (TREE_TYPE (decl
));
9286 if (TREE_CODE (decl
) != TYPE_DECL
)
9288 relayout_decl (decl
);
9289 if (c_dialect_objc ())
9290 objc_check_decl (decl
);
9291 rest_of_decl_compilation (decl
, toplevel
, 0);
9296 /* Determine whether the FIELD_DECL X is a flexible array member according to
9298 A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT;
9299 B. whether the FIELD_DECL is an array that is declared as "[]", "[0]",
9301 C. flag_strict_flex_arrays;
9302 D. the attribute strict_flex_array that is attached to the field
9304 Return TRUE when it's a flexible array member, FALSE otherwise. */
9307 is_flexible_array_member_p (bool is_last_field
,
9310 /* If not the last field, return false. */
9314 /* If not an array field, return false. */
9315 if (TREE_CODE (TREE_TYPE (x
)) != ARRAY_TYPE
)
9318 bool is_zero_length_array
= zero_length_array_type_p (TREE_TYPE (x
));
9319 bool is_one_element_array
= one_element_array_type_p (TREE_TYPE (x
));
9320 bool is_flexible_array
= flexible_array_member_type_p (TREE_TYPE (x
));
9322 unsigned int strict_flex_array_level
= c_strict_flex_array_level_of (x
);
9324 switch (strict_flex_array_level
)
9327 /* Default, all trailing arrays are flexible array members. */
9330 /* Level 1: all "[1]", "[0]", and "[]" are flexible array members. */
9331 if (is_one_element_array
)
9335 /* Level 2: all "[0]", and "[]" are flexible array members. */
9336 if (is_zero_length_array
)
9340 /* Level 3: Only "[]" are flexible array members. */
9341 if (is_flexible_array
)
9351 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
9352 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
9353 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
9354 ATTRIBUTES are attributes to be applied to the structure.
9356 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
9357 the struct was started. */
9360 finish_struct (location_t loc
, tree t
, tree fieldlist
, tree attributes
,
9361 class c_struct_parse_info
*enclosing_struct_parse_info
,
9365 bool toplevel
= file_scope
== current_scope
;
9367 /* If this type was previously laid out as a forward reference,
9368 make sure we lay it out again. */
9370 TYPE_SIZE (t
) = NULL_TREE
;
9372 decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
9376 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
9378 if (DECL_NAME (x
) != NULL_TREE
)
9380 if (flag_isoc11
&& RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
9386 if (TREE_CODE (t
) == UNION_TYPE
)
9389 pedwarn (loc
, OPT_Wpedantic
, "union has no named members");
9391 pedwarn (loc
, OPT_Wpedantic
, "union has no members");
9396 pedwarn (loc
, OPT_Wpedantic
, "struct has no named members");
9398 pedwarn (loc
, OPT_Wpedantic
, "struct has no members");
9403 /* Install struct as DECL_CONTEXT of each field decl.
9404 Also process specified field sizes, found in the DECL_INITIAL,
9405 storing 0 there after the type has been changed to precision equal
9406 to its width, rather than the precision of the specified standard
9407 type. (Correct layout requires the original type to have been preserved
9410 bool saw_named_field
= false;
9411 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
9413 /* Whether this field is the last field of the structure or union.
9414 for UNION, any field is the last field of it. */
9415 bool is_last_field
= (DECL_CHAIN (x
) == NULL_TREE
)
9416 || (TREE_CODE (t
) == UNION_TYPE
);
9418 if (TREE_TYPE (x
) == error_mark_node
)
9421 DECL_CONTEXT (x
) = t
;
9423 tree t1
= strip_array_types (TREE_TYPE (x
));
9424 /* If any field is const, the structure type is pseudo-const. */
9425 if (TREE_READONLY (x
))
9426 C_TYPE_FIELDS_READONLY (t
) = 1;
9429 /* A field that is pseudo-const makes the structure likewise. */
9430 if (RECORD_OR_UNION_TYPE_P (t1
) && C_TYPE_FIELDS_READONLY (t1
))
9431 C_TYPE_FIELDS_READONLY (t
) = 1;
9434 /* Any field that is volatile means variables of this type must be
9435 treated in some ways as volatile. */
9436 if (TREE_THIS_VOLATILE (x
))
9438 C_TYPE_FIELDS_VOLATILE (t
) = 1;
9439 C_TYPE_FIELDS_NON_CONSTEXPR (t
) = 1;
9442 /* Any field that is volatile, restrict-qualified or atomic
9443 means the type cannot be used for a constexpr object. */
9445 & (TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
| TYPE_QUAL_ATOMIC
))
9446 C_TYPE_FIELDS_NON_CONSTEXPR (t
) = 1;
9447 else if (RECORD_OR_UNION_TYPE_P (t1
) && C_TYPE_FIELDS_NON_CONSTEXPR (t1
))
9448 C_TYPE_FIELDS_NON_CONSTEXPR (t
) = 1;
9450 /* Any field of nominal variable size implies structure is too. */
9451 if (C_DECL_VARIABLE_SIZE (x
))
9452 C_TYPE_VARIABLE_SIZE (t
) = 1;
9454 /* If any field is variably modified, record this fact. */
9455 if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (x
)))
9456 C_TYPE_VARIABLY_MODIFIED (t
) = 1;
9458 if (DECL_C_BIT_FIELD (x
))
9460 unsigned HOST_WIDE_INT width
= tree_to_uhwi (DECL_INITIAL (x
));
9461 DECL_SIZE (x
) = bitsize_int (width
);
9462 DECL_BIT_FIELD (x
) = 1;
9466 && (DECL_BIT_FIELD (x
)
9467 || TYPE_ALIGN (TREE_TYPE (x
)) > BITS_PER_UNIT
))
9468 DECL_PACKED (x
) = 1;
9470 /* Detect flexible array member in an invalid context. */
9471 if (flexible_array_member_type_p (TREE_TYPE (x
)))
9473 if (TREE_CODE (t
) == UNION_TYPE
)
9475 error_at (DECL_SOURCE_LOCATION (x
),
9476 "flexible array member in union");
9477 TREE_TYPE (x
) = error_mark_node
;
9479 else if (!is_last_field
)
9481 error_at (DECL_SOURCE_LOCATION (x
),
9482 "flexible array member not at end of struct");
9483 TREE_TYPE (x
) = error_mark_node
;
9485 else if (!saw_named_field
)
9487 error_at (DECL_SOURCE_LOCATION (x
),
9488 "flexible array member in a struct with no named "
9490 TREE_TYPE (x
) = error_mark_node
;
9494 if (pedantic
&& TREE_CODE (t
) == RECORD_TYPE
9495 && flexible_array_type_p (TREE_TYPE (x
)))
9496 pedwarn (DECL_SOURCE_LOCATION (x
), OPT_Wpedantic
,
9497 "invalid use of structure with flexible array member");
9499 /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */
9500 DECL_NOT_FLEXARRAY (x
) = !is_flexible_array_member_p (is_last_field
, x
);
9502 /* Set TYPE_INCLUDES_FLEXARRAY for the context of x, t.
9503 when x is an array and is the last field. */
9504 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
)
9505 TYPE_INCLUDES_FLEXARRAY (t
)
9506 = is_last_field
&& flexible_array_member_type_p (TREE_TYPE (x
));
9507 /* Recursively set TYPE_INCLUDES_FLEXARRAY for the context of x, t
9508 when x is an union or record and is the last field. */
9509 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
9510 TYPE_INCLUDES_FLEXARRAY (t
)
9511 = is_last_field
&& TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x
));
9513 if (warn_flex_array_member_not_at_end
9515 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
))
9516 && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x
)))
9517 warning_at (DECL_SOURCE_LOCATION (x
),
9518 OPT_Wflex_array_member_not_at_end
,
9519 "structure containing a flexible array member"
9520 " is not at the end of another structure");
9523 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
9524 saw_named_field
= true;
9527 detect_field_duplicates (fieldlist
);
9529 /* Now we have the nearly final fieldlist. Record it,
9530 then lay out the structure or union (including the fields). */
9532 TYPE_FIELDS (t
) = fieldlist
;
9534 maybe_apply_pragma_scalar_storage_order (t
);
9538 if (TYPE_SIZE_UNIT (t
)
9539 && TREE_CODE (TYPE_SIZE_UNIT (t
)) == INTEGER_CST
9540 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t
))
9541 && !valid_constant_size_p (TYPE_SIZE_UNIT (t
)))
9542 error ("type %qT is too large", t
);
9544 /* Give bit-fields their proper types and rewrite the type of array fields
9545 with scalar component if the enclosing type has reverse storage order. */
9546 for (tree field
= fieldlist
; field
; field
= DECL_CHAIN (field
))
9548 if (TREE_CODE (field
) == FIELD_DECL
9549 && DECL_INITIAL (field
)
9550 && TREE_TYPE (field
) != error_mark_node
)
9552 unsigned HOST_WIDE_INT width
9553 = tree_to_uhwi (DECL_INITIAL (field
));
9554 tree type
= TREE_TYPE (field
);
9555 if (width
!= TYPE_PRECISION (type
))
9557 if (TREE_CODE (type
) == BITINT_TYPE
9558 && (width
> 1 || TYPE_UNSIGNED (type
)))
9560 = build_bitint_type (width
, TYPE_UNSIGNED (type
));
9563 = c_build_bitfield_integer_type (width
,
9564 TYPE_UNSIGNED (type
));
9565 if (tree attr
= c_hardbool_type_attr (type
))
9566 decl_attributes (&TREE_TYPE (field
),
9569 SET_DECL_MODE (field
, TYPE_MODE (TREE_TYPE (field
)));
9571 DECL_INITIAL (field
) = NULL_TREE
;
9573 else if (TYPE_REVERSE_STORAGE_ORDER (t
)
9574 && TREE_CODE (field
) == FIELD_DECL
9575 && TREE_CODE (TREE_TYPE (field
)) == ARRAY_TYPE
)
9577 tree ftype
= TREE_TYPE (field
);
9578 tree ctype
= strip_array_types (ftype
);
9579 if (!RECORD_OR_UNION_TYPE_P (ctype
) && TYPE_MODE (ctype
) != QImode
)
9581 tree fmain_type
= TYPE_MAIN_VARIANT (ftype
);
9582 tree
*typep
= &fmain_type
;
9584 *typep
= build_distinct_type_copy (*typep
);
9585 TYPE_REVERSE_STORAGE_ORDER (*typep
) = 1;
9586 typep
= &TREE_TYPE (*typep
);
9587 } while (TREE_CODE (*typep
) == ARRAY_TYPE
);
9589 = c_build_qualified_type (fmain_type
, TYPE_QUALS (ftype
));
9593 /* Warn on problematic type punning for storage order purposes. */
9594 if (TREE_CODE (t
) == UNION_TYPE
9595 && TREE_CODE (field
) == FIELD_DECL
9596 && AGGREGATE_TYPE_P (TREE_TYPE (field
)))
9598 tree ftype
= TREE_TYPE (field
);
9599 if (TREE_CODE (ftype
) == ARRAY_TYPE
)
9600 ftype
= strip_array_types (ftype
);
9601 if (RECORD_OR_UNION_TYPE_P (ftype
)
9602 && TYPE_REVERSE_STORAGE_ORDER (ftype
)
9603 != TYPE_REVERSE_STORAGE_ORDER (t
))
9604 warning_at (DECL_SOURCE_LOCATION (field
),
9605 OPT_Wscalar_storage_order
,
9606 "type punning toggles scalar storage order");
9610 /* Now we have the truly final field list.
9611 Store it in this type and in the variants. */
9613 TYPE_FIELDS (t
) = fieldlist
;
9615 /* If there are lots of fields, sort so we can look through them fast.
9616 We arbitrarily consider 16 or more elts to be "a lot". */
9621 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
9623 if (len
> 15 || DECL_NAME (x
) == NULL
)
9631 struct lang_type
*space
;
9632 struct sorted_fields_type
*space2
;
9634 len
+= list_length (x
);
9636 /* Use the same allocation policy here that make_node uses, to
9637 ensure that this lives as long as the rest of the struct decl.
9638 All decls in an inline function need to be saved. */
9640 space
= ggc_cleared_alloc
<struct lang_type
> ();
9641 space2
= (sorted_fields_type
*) ggc_internal_alloc
9642 (sizeof (struct sorted_fields_type
) + len
* sizeof (tree
));
9646 field_array
= &space2
->elts
[0];
9647 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
9649 field_array
[len
++] = x
;
9651 /* If there is anonymous struct or union, break out of the loop. */
9652 if (DECL_NAME (x
) == NULL
)
9655 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
9658 TYPE_LANG_SPECIFIC (t
) = space
;
9659 TYPE_LANG_SPECIFIC (t
)->s
->len
= len
;
9660 field_array
= TYPE_LANG_SPECIFIC (t
)->s
->elts
;
9661 qsort (field_array
, len
, sizeof (tree
), field_decl_cmp
);
9666 /* If this was supposed to be a transparent union, but we can't
9667 make it one, warn and turn off the flag. */
9668 if (TREE_CODE (t
) == UNION_TYPE
9669 && TYPE_TRANSPARENT_AGGR (t
)
9670 && (!TYPE_FIELDS (t
) || TYPE_MODE (t
) != DECL_MODE (TYPE_FIELDS (t
))))
9672 TYPE_TRANSPARENT_AGGR (t
) = 0;
9673 warning_at (loc
, 0, "union cannot be made transparent");
9676 /* Check for consistency with previous definition. */
9677 if (flag_isoc23
&& NULL
!= enclosing_struct_parse_info
)
9679 tree vistype
= previous_tag (t
);
9681 && TREE_CODE (vistype
) == TREE_CODE (t
)
9682 && !C_TYPE_BEING_DEFINED (vistype
))
9684 TYPE_STUB_DECL (vistype
) = TYPE_STUB_DECL (t
);
9685 if (c_type_variably_modified_p (t
))
9686 error ("redefinition of struct or union %qT with variably "
9687 "modified type", t
);
9688 else if (!comptypes_same_p (t
, vistype
))
9689 error ("redefinition of struct or union %qT", t
);
9693 C_TYPE_BEING_DEFINED (t
) = 0;
9695 /* Set type canonical based on equivalence class. */
9698 if (NULL
== c_struct_htab
)
9699 c_struct_htab
= hash_table
<c_struct_hasher
>::create_ggc (61);
9701 hashval_t hash
= c_struct_hasher::hash (t
);
9703 tree
*e
= c_struct_htab
->find_slot_with_hash (t
, hash
, INSERT
);
9705 TYPE_CANONICAL (t
) = *e
;
9708 TYPE_CANONICAL (t
) = t
;
9713 tree incomplete_vars
= C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t
));
9714 for (x
= TYPE_MAIN_VARIANT (t
); x
; x
= TYPE_NEXT_VARIANT (x
))
9716 TYPE_FIELDS (x
) = TYPE_FIELDS (t
);
9717 TYPE_LANG_SPECIFIC (x
) = TYPE_LANG_SPECIFIC (t
);
9718 TYPE_TRANSPARENT_AGGR (x
) = TYPE_TRANSPARENT_AGGR (t
);
9719 C_TYPE_FIELDS_READONLY (x
) = C_TYPE_FIELDS_READONLY (t
);
9720 C_TYPE_FIELDS_VOLATILE (x
) = C_TYPE_FIELDS_VOLATILE (t
);
9721 C_TYPE_FIELDS_NON_CONSTEXPR (x
) = C_TYPE_FIELDS_NON_CONSTEXPR (t
);
9722 C_TYPE_VARIABLE_SIZE (x
) = C_TYPE_VARIABLE_SIZE (t
);
9723 C_TYPE_VARIABLY_MODIFIED (x
) = C_TYPE_VARIABLY_MODIFIED (t
);
9724 C_TYPE_INCOMPLETE_VARS (x
) = NULL_TREE
;
9727 /* Update type location to the one of the definition, instead of e.g.
9728 a forward declaration. */
9729 if (TYPE_STUB_DECL (t
))
9730 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t
)) = loc
;
9732 /* Finish debugging output for this type. */
9733 rest_of_type_compilation (t
, toplevel
);
9735 finish_incomplete_vars (incomplete_vars
, toplevel
);
9737 /* Make sure a DECL_EXPR is created for structs with VLA members.
9738 Because we do not know the context, we always pass expr
9739 to force creation of a BIND_EXPR which is required in some
9741 if (c_type_variably_modified_p (t
))
9742 add_decl_expr (loc
, t
, expr
, false);
9744 if (warn_cxx_compat
)
9745 warn_cxx_compat_finish_struct (fieldlist
, TREE_CODE (t
), loc
);
9747 if (NULL
!= enclosing_struct_parse_info
)
9749 delete struct_parse_info
;
9751 struct_parse_info
= enclosing_struct_parse_info
;
9753 /* If this struct is defined inside a struct, add it to
9756 && struct_parse_info
!= NULL
9757 && !in_sizeof
&& !in_typeof
&& !in_alignof
)
9758 struct_parse_info
->struct_types
.safe_push (t
);
9765 gt_pointer_operator new_value
;
9769 /* This routine compares two fields like field_decl_cmp but using the
9770 pointer operator in resort_data. */
9773 resort_field_decl_cmp (const void *x_p
, const void *y_p
)
9775 const tree
*const x
= (const tree
*) x_p
;
9776 const tree
*const y
= (const tree
*) y_p
;
9778 if (DECL_NAME (*x
) == DECL_NAME (*y
))
9779 /* A nontype is "greater" than a type. */
9780 return (TREE_CODE (*y
) == TYPE_DECL
) - (TREE_CODE (*x
) == TYPE_DECL
);
9781 if (DECL_NAME (*x
) == NULL_TREE
)
9783 if (DECL_NAME (*y
) == NULL_TREE
)
9786 tree d1
= DECL_NAME (*x
);
9787 tree d2
= DECL_NAME (*y
);
9788 resort_data
.new_value (&d1
, &d1
, resort_data
.cookie
);
9789 resort_data
.new_value (&d2
, &d2
, resort_data
.cookie
);
9796 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9799 resort_sorted_fields (void *obj
,
9800 void * ARG_UNUSED (orig_obj
),
9801 gt_pointer_operator new_value
,
9804 struct sorted_fields_type
*sf
= (struct sorted_fields_type
*) obj
;
9805 resort_data
.new_value
= new_value
;
9806 resort_data
.cookie
= cookie
;
9807 qsort (&sf
->elts
[0], sf
->len
, sizeof (tree
),
9808 resort_field_decl_cmp
);
9811 /* Lay out the type T, and its element type, and so on. */
9814 layout_array_type (tree t
)
9816 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
9817 layout_array_type (TREE_TYPE (t
));
9821 /* Begin compiling the definition of an enumeration type.
9822 NAME is its name (or null if anonymous).
9823 LOC is the enum's location.
9824 FIXED_UNDERLYING_TYPE is the (C23) underlying type specified in the
9826 Returns the type object, as yet incomplete.
9827 Also records info about it so that build_enumerator
9828 may be used to declare the individual values as they are read. */
9831 start_enum (location_t loc
, struct c_enum_contents
*the_enum
, tree name
,
9832 tree fixed_underlying_type
, bool potential_nesting_p
)
9834 tree enumtype
= NULL_TREE
;
9835 location_t enumloc
= UNKNOWN_LOCATION
;
9837 /* If this is the real definition for a previous forward reference,
9838 fill in the contents in the same object that used to be the
9839 forward reference. */
9841 if (name
!= NULL_TREE
)
9842 enumtype
= lookup_tag (ENUMERAL_TYPE
, name
, true, &enumloc
);
9844 if (enumtype
!= NULL_TREE
&& TREE_CODE (enumtype
) == ENUMERAL_TYPE
)
9846 /* If the type is currently being defined or if we have seen an
9847 incomplete version which is now complete, this is a nested
9848 redefinition. The later happens if the redefinition occurs
9849 inside the enum specifier itself. */
9850 if (C_TYPE_BEING_DEFINED (enumtype
)
9851 || (potential_nesting_p
&& TYPE_VALUES (enumtype
) != NULL_TREE
))
9852 error_at (loc
, "nested redefinition of %<enum %E%>", name
);
9854 /* For C23 we allow redefinitions. We set to zero and check for
9855 consistency later. */
9856 if (flag_isoc23
&& TYPE_VALUES (enumtype
) != NULL_TREE
)
9857 enumtype
= NULL_TREE
;
9860 if (enumtype
== NULL_TREE
|| TREE_CODE (enumtype
) != ENUMERAL_TYPE
)
9862 enumtype
= make_node (ENUMERAL_TYPE
);
9863 TYPE_SIZE (enumtype
) = NULL_TREE
;
9864 pushtag (loc
, name
, enumtype
);
9865 if (fixed_underlying_type
!= NULL_TREE
)
9867 /* For an enum definition with a fixed underlying type, the
9868 type is complete during the definition and the
9869 enumeration constants have that type. If there was a
9870 tag, the type was completed in c_parser_enum_specifier.
9871 If not, it must be completed here. */
9872 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype
) = true;
9873 TYPE_MIN_VALUE (enumtype
) = TYPE_MIN_VALUE (fixed_underlying_type
);
9874 TYPE_MAX_VALUE (enumtype
) = TYPE_MAX_VALUE (fixed_underlying_type
);
9875 TYPE_UNSIGNED (enumtype
) = TYPE_UNSIGNED (fixed_underlying_type
);
9876 SET_TYPE_ALIGN (enumtype
, TYPE_ALIGN (fixed_underlying_type
));
9877 TYPE_SIZE (enumtype
) = NULL_TREE
;
9878 TYPE_PRECISION (enumtype
) = TYPE_PRECISION (fixed_underlying_type
);
9879 ENUM_UNDERLYING_TYPE (enumtype
) = fixed_underlying_type
;
9880 layout_type (enumtype
);
9883 /* Update type location to the one of the definition, instead of e.g.
9884 a forward declaration. */
9885 else if (TYPE_STUB_DECL (enumtype
))
9887 enumloc
= DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype
));
9888 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype
)) = loc
;
9891 C_TYPE_BEING_DEFINED (enumtype
) = 1;
9893 if (TYPE_VALUES (enumtype
) != NULL_TREE
)
9895 /* This enum is a named one that has been declared already. */
9896 auto_diagnostic_group d
;
9897 error_at (loc
, "redeclaration of %<enum %E%>", name
);
9898 if (enumloc
!= UNKNOWN_LOCATION
)
9899 inform (enumloc
, "originally defined here");
9901 /* Completely replace its old definition.
9902 The old enumerators remain defined, however. */
9903 TYPE_VALUES (enumtype
) = NULL_TREE
;
9906 if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype
)
9907 && fixed_underlying_type
== NULL_TREE
)
9908 error_at (loc
, "%<enum%> declared with but defined without "
9909 "fixed underlying type");
9911 the_enum
->enum_next_value
= integer_zero_node
;
9912 the_enum
->enum_type
= enumtype
;
9913 the_enum
->enum_overflow
= 0;
9915 if (flag_short_enums
&& !ENUM_FIXED_UNDERLYING_TYPE_P (enumtype
))
9916 for (tree v
= TYPE_MAIN_VARIANT (enumtype
); v
; v
= TYPE_NEXT_VARIANT (v
))
9917 TYPE_PACKED (v
) = 1;
9919 /* FIXME: This will issue a warning for a use of a type defined
9920 within sizeof in a statement expr. This is not terribly serious
9921 as C++ doesn't permit statement exprs within sizeof anyhow. */
9922 if (warn_cxx_compat
&& (in_sizeof
|| in_typeof
|| in_alignof
))
9923 warning_at (loc
, OPT_Wc___compat
,
9924 "defining type in %qs expression is invalid in C++",
9927 : (in_typeof
? "typeof" : "alignof")));
9929 if (in_underspecified_init
)
9930 error_at (loc
, "%qT defined in underspecified object initializer",
9936 /* After processing and defining all the values of an enumeration type,
9937 install their decls in the enumeration type and finish it off.
9938 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
9939 and ATTRIBUTES are the specified attributes.
9940 Returns ENUMTYPE. */
9943 finish_enum (tree enumtype
, tree values
, tree attributes
)
9946 tree minnode
= NULL_TREE
, maxnode
= NULL_TREE
;
9949 bool toplevel
= (file_scope
== current_scope
);
9950 struct lang_type
*lt
;
9952 decl_attributes (&enumtype
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
9954 /* Calculate the maximum value of any enumerator in this type. */
9956 if (values
== error_mark_node
)
9957 minnode
= maxnode
= integer_zero_node
;
9960 minnode
= maxnode
= TREE_VALUE (values
);
9961 for (pair
= TREE_CHAIN (values
); pair
; pair
= TREE_CHAIN (pair
))
9963 tree value
= TREE_VALUE (pair
);
9964 if (tree_int_cst_lt (maxnode
, value
))
9966 if (tree_int_cst_lt (value
, minnode
))
9971 /* Construct the final type of this enumeration. It is the same
9972 as one of the integral types - the narrowest one that fits, except
9973 that normally we only go as narrow as int - and signed iff any of
9974 the values are negative. */
9975 sign
= (tree_int_cst_sgn (minnode
) >= 0) ? UNSIGNED
: SIGNED
;
9976 precision
= MAX (tree_int_cst_min_precision (minnode
, sign
),
9977 tree_int_cst_min_precision (maxnode
, sign
));
9979 bool wider_than_int
=
9980 (tree_int_cst_lt (minnode
, TYPE_MIN_VALUE (integer_type_node
))
9981 || tree_int_cst_lt (TYPE_MAX_VALUE (integer_type_node
), maxnode
));
9984 if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype
))
9986 /* If the precision of the type was specified with an attribute and it
9987 was too small, give an error. Otherwise, use it. */
9988 if (TYPE_PRECISION (enumtype
) && lookup_attribute ("mode", attributes
))
9990 if (precision
> TYPE_PRECISION (enumtype
))
9992 TYPE_PRECISION (enumtype
) = 0;
9993 error ("specified mode too small for enumerated values");
9996 precision
= TYPE_PRECISION (enumtype
);
9999 TYPE_PRECISION (enumtype
) = 0;
10001 if (TYPE_PACKED (enumtype
)
10002 || precision
> TYPE_PRECISION (integer_type_node
)
10003 || TYPE_PRECISION (enumtype
))
10005 tem
= c_common_type_for_size (precision
, sign
== UNSIGNED
? 1 : 0);
10008 /* This should only occur when both signed and unsigned
10009 values of maximum precision occur among the
10011 pedwarn (input_location
, 0,
10012 "enumeration values exceed range of largest integer");
10013 tem
= widest_integer_literal_type_node
;
10015 else if (precision
> TYPE_PRECISION (intmax_type_node
)
10016 && !tree_int_cst_lt (minnode
,
10017 TYPE_MIN_VALUE (intmax_type_node
))
10018 && !tree_int_cst_lt (TYPE_MAX_VALUE (uintmax_type_node
),
10020 pedwarn (input_location
, OPT_Wpedantic
,
10021 "enumeration values exceed range of %qs",
10022 sign
== UNSIGNED
? "uintmax_t" : "intmax_t");
10025 tem
= sign
== UNSIGNED
? unsigned_type_node
: integer_type_node
;
10027 TYPE_MIN_VALUE (enumtype
) = TYPE_MIN_VALUE (tem
);
10028 TYPE_MAX_VALUE (enumtype
) = TYPE_MAX_VALUE (tem
);
10029 TYPE_UNSIGNED (enumtype
) = TYPE_UNSIGNED (tem
);
10030 SET_TYPE_ALIGN (enumtype
, TYPE_ALIGN (tem
));
10031 TYPE_SIZE (enumtype
) = NULL_TREE
;
10032 TYPE_PRECISION (enumtype
) = TYPE_PRECISION (tem
);
10033 ENUM_UNDERLYING_TYPE (enumtype
) =
10034 c_common_type_for_size (TYPE_PRECISION (tem
), TYPE_UNSIGNED (tem
));
10036 layout_type (enumtype
);
10039 if (values
!= error_mark_node
)
10041 /* Change the type of the enumerators to be the enum type. We
10042 need to do this irrespective of the size of the enum, for
10043 proper type checking. Replace the DECL_INITIALs of the
10044 enumerators, and the value slots of the list, with copies
10045 that have the enum type; they cannot be modified in place
10046 because they may be shared (e.g. integer_zero_node) Finally,
10047 change the purpose slots to point to the names of the decls. */
10048 for (pair
= values
; pair
; pair
= TREE_CHAIN (pair
))
10050 tree enu
= TREE_PURPOSE (pair
);
10051 tree ini
= DECL_INITIAL (enu
);
10053 TREE_TYPE (enu
) = enumtype
;
10055 /* Before C23, the ISO C Standard mandates enumerators to
10056 have type int, even though the underlying type of an enum
10057 type is unspecified. However, C23 allows enumerators of
10058 any integer type, and if an enumeration has any
10059 enumerators wider than int, all enumerators have the
10060 enumerated type after it is parsed. Any enumerators that
10061 fit in int are given type int in build_enumerator (which
10062 is the correct type while the enumeration is being
10063 parsed), so no conversions are needed here if all
10064 enumerators fit in int. If the enum has a fixed
10065 underlying type, the correct type was also given in
10066 build_enumerator. */
10067 if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype
) && wider_than_int
)
10068 ini
= convert (enumtype
, ini
);
10070 DECL_INITIAL (enu
) = ini
;
10071 TREE_PURPOSE (pair
) = DECL_NAME (enu
);
10072 /* To match the C++ FE, store the CONST_DECL rather than just its
10074 TREE_VALUE (pair
) = enu
;
10077 TYPE_VALUES (enumtype
) = values
;
10080 /* Record the min/max values so that we can warn about bit-field
10081 enumerations that are too small for the values. */
10082 lt
= ggc_cleared_alloc
<struct lang_type
> ();
10083 lt
->enum_min
= minnode
;
10084 lt
->enum_max
= maxnode
;
10085 TYPE_LANG_SPECIFIC (enumtype
) = lt
;
10087 /* Fix up all variant types of this enum type. */
10088 tree incomplete_vars
= C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype
));
10089 for (tem
= TYPE_MAIN_VARIANT (enumtype
); tem
; tem
= TYPE_NEXT_VARIANT (tem
))
10091 C_TYPE_INCOMPLETE_VARS (tem
) = NULL_TREE
;
10092 if (tem
== enumtype
)
10094 TYPE_VALUES (tem
) = TYPE_VALUES (enumtype
);
10095 TYPE_MIN_VALUE (tem
) = TYPE_MIN_VALUE (enumtype
);
10096 TYPE_MAX_VALUE (tem
) = TYPE_MAX_VALUE (enumtype
);
10097 TYPE_SIZE (tem
) = TYPE_SIZE (enumtype
);
10098 TYPE_SIZE_UNIT (tem
) = TYPE_SIZE_UNIT (enumtype
);
10099 SET_TYPE_MODE (tem
, TYPE_MODE (enumtype
));
10100 TYPE_PRECISION (tem
) = TYPE_PRECISION (enumtype
);
10101 SET_TYPE_ALIGN (tem
, TYPE_ALIGN (enumtype
));
10102 TYPE_USER_ALIGN (tem
) = TYPE_USER_ALIGN (enumtype
);
10103 TYPE_UNSIGNED (tem
) = TYPE_UNSIGNED (enumtype
);
10104 TYPE_LANG_SPECIFIC (tem
) = TYPE_LANG_SPECIFIC (enumtype
);
10105 ENUM_UNDERLYING_TYPE (tem
) = ENUM_UNDERLYING_TYPE (enumtype
);
10108 /* Finish debugging output for this type. */
10109 rest_of_type_compilation (enumtype
, toplevel
);
10111 finish_incomplete_vars (incomplete_vars
, toplevel
);
10113 /* If this enum is defined inside a struct, add it to
10115 if (warn_cxx_compat
10116 && struct_parse_info
!= NULL
10117 && !in_sizeof
&& !in_typeof
&& !in_alignof
)
10118 struct_parse_info
->struct_types
.safe_push (enumtype
);
10120 /* Check for consistency with previous definition */
10123 tree vistype
= previous_tag (enumtype
);
10125 && TREE_CODE (vistype
) == TREE_CODE (enumtype
)
10126 && !C_TYPE_BEING_DEFINED (vistype
))
10128 TYPE_STUB_DECL (vistype
) = TYPE_STUB_DECL (enumtype
);
10129 if (!comptypes_same_p (enumtype
, vistype
))
10130 error("conflicting redefinition of enum %qT", enumtype
);
10134 C_TYPE_BEING_DEFINED (enumtype
) = 0;
10139 /* Build and install a CONST_DECL for one value of the
10140 current enumeration type (one that was begun with start_enum).
10141 DECL_LOC is the location of the enumerator.
10142 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
10143 Return a tree-list containing the CONST_DECL and its value.
10144 Assignment of sequential values by default is handled here. */
10147 build_enumerator (location_t decl_loc
, location_t loc
,
10148 struct c_enum_contents
*the_enum
, tree name
, tree value
)
10152 /* Validate and default VALUE. */
10154 if (value
!= NULL_TREE
)
10156 /* Don't issue more errors for error_mark_node (i.e. an
10157 undeclared identifier) - just ignore the value expression. */
10158 if (value
== error_mark_node
)
10160 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value
)))
10162 error_at (loc
, "enumerator value for %qE is not an integer constant",
10168 if (TREE_CODE (value
) != INTEGER_CST
)
10170 value
= c_fully_fold (value
, false, NULL
);
10171 if (TREE_CODE (value
) == INTEGER_CST
)
10172 pedwarn (loc
, OPT_Wpedantic
,
10173 "enumerator value for %qE is not an integer "
10174 "constant expression", name
);
10176 if (TREE_CODE (value
) != INTEGER_CST
)
10178 error ("enumerator value for %qE is not an integer constant",
10184 value
= default_conversion (value
);
10185 constant_expression_warning (value
);
10190 /* Default based on previous value. */
10191 /* It should no longer be possible to have NON_LVALUE_EXPR
10193 if (value
== NULL_TREE
)
10195 value
= the_enum
->enum_next_value
;
10196 if (the_enum
->enum_overflow
)
10197 error_at (loc
, "overflow in enumeration values");
10199 if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum
->enum_type
))
10201 /* Enumeration constants must fit in the fixed underlying type. */
10202 if (!int_fits_type_p (value
, ENUM_UNDERLYING_TYPE (the_enum
->enum_type
)))
10204 "enumerator value outside the range of underlying type");
10205 /* Enumeration constants for an enum with fixed underlying type
10206 have the enum type, both inside and outside the
10208 value
= convert (the_enum
->enum_type
, value
);
10212 /* Even though the underlying type of an enum is unspecified, the
10213 type of enumeration constants is explicitly defined as int
10214 (6.4.4.3/2 in the C99 Standard). C23 allows any integer type, and
10215 GCC allows such types for older standards as an extension. */
10216 bool warned_range
= false;
10217 if (!int_fits_type_p (value
,
10218 (TYPE_UNSIGNED (TREE_TYPE (value
))
10219 ? uintmax_type_node
10220 : intmax_type_node
)))
10221 /* GCC does not consider its types larger than intmax_t to be
10222 extended integer types (although C23 would permit such types to
10223 be considered extended integer types if all the features
10224 required by <stdint.h> and <inttypes.h> macros, such as support
10225 for integer constants and I/O, were present), so diagnose if
10226 such a wider type is used. (If the wider type arose from a
10227 constant of such a type, that will also have been diagnosed,
10228 but this is the only diagnostic in the case where it arises
10229 from choosing a wider type automatically when adding 1
10231 warned_range
= pedwarn (loc
, OPT_Wpedantic
,
10232 "enumerator value outside the range of %qs",
10233 (TYPE_UNSIGNED (TREE_TYPE (value
))
10236 if (!warned_range
&& !int_fits_type_p (value
, integer_type_node
))
10237 pedwarn_c11 (loc
, OPT_Wpedantic
,
10238 "ISO C restricts enumerator values to range of %<int%> "
10241 /* The ISO C Standard mandates enumerators to have type int before
10242 C23, even though the underlying type of an enum type is
10243 unspecified. C23 allows enumerators of any integer type. During
10244 the parsing of the enumeration, C23 specifies that constants
10245 representable in int have type int, constants not representable
10246 in int have the type of the given expression if any, and
10247 constants not representable in int and derived by adding 1 to the
10248 previous constant have the type of that constant unless the
10249 addition would overflow or wraparound, in which case a wider type
10250 of the same signedness is chosen automatically; after the
10251 enumeration is parsed, all the constants have the type of the
10252 enumeration if any do not fit in int. */
10253 if (int_fits_type_p (value
, integer_type_node
))
10254 value
= convert (integer_type_node
, value
);
10257 /* Set basis for default for next value. */
10258 if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum
->enum_type
))
10260 tree underlying_type
= ENUM_UNDERLYING_TYPE (the_enum
->enum_type
);
10261 if (TREE_CODE (underlying_type
) == BOOLEAN_TYPE
)
10262 /* A value of 2 following a value of 1 overflows bool, but we
10263 cannot carry out addition directly on bool without
10264 promotion, and converting the result of arithmetic in a
10265 wider type back to bool would not produce the right result
10266 for this overflow check. */
10267 the_enum
->enum_next_value
= invert_truthvalue_loc (loc
, value
);
10269 the_enum
->enum_next_value
10270 = build_binary_op (EXPR_LOC_OR_LOC (value
, input_location
),
10271 PLUS_EXPR
, convert (underlying_type
, value
),
10272 convert (underlying_type
, integer_one_node
),
10276 the_enum
->enum_next_value
10277 = build_binary_op (EXPR_LOC_OR_LOC (value
, input_location
),
10278 PLUS_EXPR
, value
, integer_one_node
, false);
10279 the_enum
->enum_overflow
= tree_int_cst_lt (the_enum
->enum_next_value
, value
);
10280 if (the_enum
->enum_overflow
10281 && !ENUM_FIXED_UNDERLYING_TYPE_P (the_enum
->enum_type
))
10283 /* Choose a wider type with the same signedness if
10285 int prec
= TYPE_PRECISION (TREE_TYPE (value
)) + 1;
10286 bool unsignedp
= TYPE_UNSIGNED (TREE_TYPE (value
));
10287 tree new_type
= (unsignedp
10288 ? long_unsigned_type_node
10289 : long_integer_type_node
);
10290 if (prec
> TYPE_PRECISION (new_type
))
10291 new_type
= (unsignedp
10292 ? long_long_unsigned_type_node
10293 : long_long_integer_type_node
);
10294 if (prec
> TYPE_PRECISION (new_type
))
10295 new_type
= (unsignedp
10296 ? widest_unsigned_literal_type_node
10297 : widest_integer_literal_type_node
);
10298 if (prec
<= TYPE_PRECISION (new_type
))
10300 the_enum
->enum_overflow
= false;
10301 the_enum
->enum_next_value
10302 = build_binary_op (EXPR_LOC_OR_LOC (value
, input_location
),
10303 PLUS_EXPR
, convert (new_type
, value
),
10304 integer_one_node
, false);
10305 gcc_assert (!tree_int_cst_lt (the_enum
->enum_next_value
, value
));
10309 /* Now create a declaration for the enum value name. */
10311 decl
= build_decl (decl_loc
, CONST_DECL
, name
, TREE_TYPE (value
));
10312 DECL_INITIAL (decl
) = value
;
10313 DECL_CONTEXT (decl
) = the_enum
->enum_type
;
10316 return tree_cons (decl
, value
, NULL_TREE
);
10319 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
10322 c_simulate_enum_decl (location_t loc
, const char *name
,
10323 vec
<string_int_pair
> *values_ptr
)
10325 location_t saved_loc
= input_location
;
10326 input_location
= loc
;
10328 struct c_enum_contents the_enum
;
10329 tree enumtype
= start_enum (loc
, &the_enum
, get_identifier (name
),
10332 tree value_chain
= NULL_TREE
;
10333 string_int_pair
*value
;
10334 vec
<string_int_pair
> values
= *values_ptr
;
10336 FOR_EACH_VEC_ELT (values
, i
, value
)
10338 tree decl
= build_enumerator (loc
, loc
, &the_enum
,
10339 get_identifier (value
->first
),
10340 build_int_cst (integer_type_node
,
10342 TREE_CHAIN (decl
) = value_chain
;
10343 value_chain
= decl
;
10346 finish_enum (enumtype
, nreverse (value_chain
), NULL_TREE
);
10348 input_location
= saved_loc
;
10352 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
10355 c_simulate_record_decl (location_t loc
, const char *name
,
10356 array_slice
<const tree
> fields
)
10358 location_t saved_loc
= input_location
;
10359 input_location
= loc
;
10361 class c_struct_parse_info
*struct_info
;
10362 tree ident
= get_identifier (name
);
10363 tree type
= start_struct (loc
, RECORD_TYPE
, ident
, &struct_info
);
10365 for (unsigned int i
= 0; i
< fields
.size (); ++i
)
10367 DECL_FIELD_CONTEXT (fields
[i
]) = type
;
10369 DECL_CHAIN (fields
[i
- 1]) = fields
[i
];
10372 finish_struct (loc
, type
, fields
[0], NULL_TREE
, struct_info
);
10374 tree decl
= build_decl (loc
, TYPE_DECL
, ident
, type
);
10375 set_underlying_type (decl
);
10376 lang_hooks
.decls
.pushdecl (decl
);
10378 input_location
= saved_loc
;
10382 /* Create the FUNCTION_DECL for a function definition.
10383 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
10384 the declaration; they describe the function's name and the type it returns,
10385 but twisted together in a fashion that parallels the syntax of C.
10387 This function creates a binding context for the function body
10388 as well as setting up the FUNCTION_DECL in current_function_decl.
10390 Returns true on success. If the DECLARATOR is not suitable for a function
10391 (it defines a datum instead), we return false to report a parse error. */
10394 start_function (struct c_declspecs
*declspecs
, struct c_declarator
*declarator
,
10397 tree decl1
, old_decl
;
10398 tree restype
, resdecl
;
10400 location_t result_loc
;
10403 current_function_returns_value
= 0; /* Assume, until we see it does. */
10404 current_function_returns_null
= 0;
10405 current_function_returns_abnormally
= 0;
10406 warn_about_return_type
= 0;
10407 c_switch_stack
= NULL
;
10409 /* Indicate no valid break/continue context. */
10412 decl1
= grokdeclarator (declarator
, declspecs
, FUNCDEF
, true, NULL
,
10413 &attributes
, &expr
, NULL
, DEPRECATED_NORMAL
);
10414 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION
, decl1
);
10416 /* If the declarator is not suitable for a function definition,
10417 cause a syntax error. */
10418 if (decl1
== NULL_TREE
10419 || TREE_CODE (decl1
) != FUNCTION_DECL
)
10422 /* Nested functions may have variably modified (return) type.
10423 Make sure the size expression is evaluated at this point. */
10424 if (expr
&& !current_scope
->parm_flag
)
10425 add_stmt (fold_convert (void_type_node
, expr
));
10427 loc
= DECL_SOURCE_LOCATION (decl1
);
10429 /* A nested function is not global. */
10430 if (current_function_decl
!= NULL_TREE
)
10431 TREE_PUBLIC (decl1
) = 0;
10433 c_decl_attributes (&decl1
, attributes
, 0);
10435 if (DECL_DECLARED_INLINE_P (decl1
)
10436 && DECL_UNINLINABLE (decl1
)
10437 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1
)))
10438 warning_at (loc
, OPT_Wattributes
,
10439 "inline function %qD given attribute %qs",
10440 decl1
, "noinline");
10442 /* Handle gnu_inline attribute. */
10443 if (declspecs
->inline_p
10444 && !flag_gnu89_inline
10445 && TREE_CODE (decl1
) == FUNCTION_DECL
10446 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1
))
10447 || current_function_decl
))
10449 if (declspecs
->storage_class
!= csc_static
)
10450 DECL_EXTERNAL (decl1
) = !DECL_EXTERNAL (decl1
);
10453 announce_function (decl1
);
10455 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1
))))
10457 error_at (loc
, "return type is an incomplete type");
10458 /* Make it return void instead. */
10460 = build_function_type (void_type_node
,
10461 TYPE_ARG_TYPES (TREE_TYPE (decl1
)),
10462 TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl1
)));
10465 if (warn_about_return_type
)
10466 permerror_opt (loc
, flag_isoc99
? OPT_Wimplicit_int
10467 : (warn_return_type
> 0 ? OPT_Wreturn_type
10468 : OPT_Wimplicit_int
),
10469 "return type defaults to %<int%>");
10471 /* Make the init_value nonzero so pushdecl knows this is not tentative.
10472 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
10473 DECL_INITIAL (decl1
) = error_mark_node
;
10475 /* If this definition isn't a prototype and we had a prototype declaration
10476 before, copy the arg type info from that prototype. */
10477 old_decl
= lookup_name_in_scope (DECL_NAME (decl1
), current_scope
);
10478 if (old_decl
&& TREE_CODE (old_decl
) != FUNCTION_DECL
)
10479 old_decl
= NULL_TREE
;
10481 current_function_prototype_locus
= UNKNOWN_LOCATION
;
10482 current_function_prototype_built_in
= false;
10483 current_function_prototype_arg_types
= NULL_TREE
;
10484 tree newtype
= TREE_TYPE (decl1
);
10485 tree oldtype
= old_decl
? TREE_TYPE (old_decl
) : newtype
;
10486 if (!prototype_p (newtype
))
10488 tree oldrt
= TREE_TYPE (oldtype
);
10489 tree newrt
= TREE_TYPE (newtype
);
10490 if (old_decl
!= NULL_TREE
10491 && TREE_CODE (oldtype
) == FUNCTION_TYPE
10492 && comptypes (oldrt
, newrt
))
10494 if (stdarg_p (oldtype
))
10496 auto_diagnostic_group d
;
10497 warning_at (loc
, 0, "%q+D defined as variadic function "
10498 "without prototype", decl1
);
10499 locate_old_decl (old_decl
);
10501 TREE_TYPE (decl1
) = composite_type (oldtype
, newtype
);
10502 current_function_prototype_locus
= DECL_SOURCE_LOCATION (old_decl
);
10503 current_function_prototype_built_in
10504 = C_DECL_BUILTIN_PROTOTYPE (old_decl
);
10505 current_function_prototype_arg_types
10506 = TYPE_ARG_TYPES (newtype
);
10508 if (TREE_PUBLIC (decl1
))
10510 /* If there is an external prototype declaration of this
10511 function, record its location but do not copy information
10512 to this decl. This may be an invisible declaration
10513 (built-in or in a scope which has finished) or simply
10514 have more refined argument types than any declaration
10516 struct c_binding
*b
;
10517 for (b
= I_SYMBOL_BINDING (DECL_NAME (decl1
)); b
; b
= b
->shadowed
)
10518 if (B_IN_SCOPE (b
, external_scope
))
10522 tree ext_decl
, ext_type
;
10523 ext_decl
= b
->decl
;
10524 ext_type
= b
->u
.type
? b
->u
.type
: TREE_TYPE (ext_decl
);
10525 if (TREE_CODE (ext_type
) == FUNCTION_TYPE
10526 && comptypes (TREE_TYPE (TREE_TYPE (decl1
)),
10527 TREE_TYPE (ext_type
)))
10529 current_function_prototype_locus
10530 = DECL_SOURCE_LOCATION (ext_decl
);
10531 current_function_prototype_built_in
10532 = C_DECL_BUILTIN_PROTOTYPE (ext_decl
);
10533 current_function_prototype_arg_types
10534 = TYPE_ARG_TYPES (ext_type
);
10540 /* Optionally warn of old-fashioned def with no previous prototype. */
10541 if (warn_strict_prototypes
10542 && old_decl
!= error_mark_node
10543 && !prototype_p (TREE_TYPE (decl1
))
10544 && C_DECL_ISNT_PROTOTYPE (old_decl
))
10545 warning_at (loc
, OPT_Wstrict_prototypes
,
10546 "function declaration isn%'t a prototype");
10547 /* Optionally warn of any global def with no previous prototype. */
10548 else if (warn_missing_prototypes
10549 && old_decl
!= error_mark_node
10550 && TREE_PUBLIC (decl1
)
10551 && !MAIN_NAME_P (DECL_NAME (decl1
))
10552 && C_DECL_ISNT_PROTOTYPE (old_decl
)
10553 && !DECL_DECLARED_INLINE_P (decl1
))
10554 warning_at (loc
, OPT_Wmissing_prototypes
,
10555 "no previous prototype for %qD", decl1
);
10556 /* Optionally warn of any def with no previous prototype
10557 if the function has already been used. */
10558 else if (warn_missing_prototypes
10559 && old_decl
!= NULL_TREE
10560 && old_decl
!= error_mark_node
10561 && TREE_USED (old_decl
)
10562 && !prototype_p (TREE_TYPE (old_decl
)))
10563 warning_at (loc
, OPT_Wmissing_prototypes
,
10564 "%qD was used with no prototype before its definition", decl1
);
10565 /* Optionally warn of any global def with no previous declaration. */
10566 else if (warn_missing_declarations
10567 && TREE_PUBLIC (decl1
)
10568 && old_decl
== NULL_TREE
10569 && !MAIN_NAME_P (DECL_NAME (decl1
))
10570 && !DECL_DECLARED_INLINE_P (decl1
))
10571 warning_at (loc
, OPT_Wmissing_declarations
,
10572 "no previous declaration for %qD",
10574 /* Optionally warn of any def with no previous declaration
10575 if the function has already been used. */
10576 else if (warn_missing_declarations
10577 && old_decl
!= NULL_TREE
10578 && old_decl
!= error_mark_node
10579 && TREE_USED (old_decl
)
10580 && C_DECL_IMPLICIT (old_decl
))
10581 warning_at (loc
, OPT_Wmissing_declarations
,
10582 "%qD was used with no declaration before its definition", decl1
);
10584 /* This function exists in static storage.
10585 (This does not mean `static' in the C sense!) */
10586 TREE_STATIC (decl1
) = 1;
10588 /* This is the earliest point at which we might know the assembler
10589 name of the function. Thus, if it's set before this, die horribly. */
10590 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1
));
10592 /* If #pragma weak was used, mark the decl weak now. */
10593 if (current_scope
== file_scope
)
10594 maybe_apply_pragma_weak (decl1
);
10596 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
10597 if (warn_main
&& MAIN_NAME_P (DECL_NAME (decl1
)))
10599 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1
)))
10600 != integer_type_node
)
10601 pedwarn (loc
, OPT_Wmain
, "return type of %qD is not %<int%>", decl1
);
10602 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1
))))
10603 pedwarn (loc
, OPT_Wmain
, "%<_Atomic%>-qualified return type of %qD",
10606 check_main_parameter_types (decl1
);
10608 if (!TREE_PUBLIC (decl1
))
10609 pedwarn (loc
, OPT_Wmain
,
10610 "%qD is normally a non-static function", decl1
);
10613 tree parms
= current_function_arg_info
->parms
;
10616 location_t origloc
= DECL_SOURCE_LOCATION (old_decl
);
10617 warn_parm_array_mismatch (origloc
, old_decl
, parms
);
10620 /* Record the decl so that the function name is defined.
10621 If we already have a decl for this name, and it is a FUNCTION_DECL,
10622 use the old decl. */
10624 current_function_decl
= pushdecl (decl1
);
10626 if (tree access
= build_attr_access_from_parms (parms
, false))
10627 decl_attributes (¤t_function_decl
, access
, ATTR_FLAG_INTERNAL
,
10631 declare_parm_level ();
10633 /* Set the result decl source location to the location of the typespec. */
10634 result_loc
= (declspecs
->locations
[cdw_typespec
] == UNKNOWN_LOCATION
10635 ? loc
: declspecs
->locations
[cdw_typespec
]);
10636 restype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
10637 resdecl
= build_decl (result_loc
, RESULT_DECL
, NULL_TREE
, restype
);
10638 DECL_ARTIFICIAL (resdecl
) = 1;
10639 DECL_IGNORED_P (resdecl
) = 1;
10640 DECL_RESULT (current_function_decl
) = resdecl
;
10642 start_fname_decls ();
10647 /* Subroutine of store_parm_decls which handles new-style function
10648 definitions (prototype format). The parms already have decls, so we
10649 need only record them as in effect and complain if any redundant
10650 old-style parm decls were written. */
10652 store_parm_decls_newstyle (tree fndecl
, const struct c_arg_info
*arg_info
)
10658 if (current_scope
->bindings
)
10660 error_at (DECL_SOURCE_LOCATION (fndecl
),
10661 "old-style parameter declarations in prototyped "
10662 "function definition");
10664 /* Get rid of the old-style declarations. */
10668 /* Don't issue this warning for nested functions, and don't issue this
10669 warning if we got here because ARG_INFO_TYPES was error_mark_node
10670 (this happens when a function definition has just an ellipsis in
10671 its parameter list). */
10672 else if (!in_system_header_at (input_location
)
10673 && !current_function_scope
10674 && arg_info
->types
!= error_mark_node
)
10675 warning_at (DECL_SOURCE_LOCATION (fndecl
), OPT_Wtraditional
,
10676 "traditional C rejects ISO C style function definitions");
10678 /* Now make all the parameter declarations visible in the function body.
10679 We can bypass most of the grunt work of pushdecl. */
10680 for (decl
= arg_info
->parms
; decl
; decl
= DECL_CHAIN (decl
))
10682 DECL_CONTEXT (decl
) = current_function_decl
;
10683 if (DECL_NAME (decl
))
10685 bind (DECL_NAME (decl
), decl
, current_scope
,
10686 /*invisible=*/false, /*nested=*/false,
10688 if (!TREE_USED (decl
))
10689 warn_if_shadowing (decl
);
10692 pedwarn_c11 (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
10693 "ISO C does not support omitting parameter names in "
10694 "function definitions before C23");
10697 /* Record the parameter list in the function declaration. */
10698 DECL_ARGUMENTS (fndecl
) = arg_info
->parms
;
10700 /* Now make all the ancillary declarations visible, likewise. */
10701 for (decl
= arg_info
->others
; decl
; decl
= DECL_CHAIN (decl
))
10703 DECL_CONTEXT (decl
) = current_function_decl
;
10704 if (DECL_NAME (decl
))
10705 bind (DECL_NAME (decl
), decl
, current_scope
,
10706 /*invisible=*/false,
10707 /*nested=*/(TREE_CODE (decl
) == FUNCTION_DECL
),
10711 /* And all the tag declarations. */
10712 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info
->tags
, ix
, tag
)
10714 bind (tag
->id
, tag
->type
, current_scope
,
10715 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
10718 /* Subroutine of store_parm_decls which handles old-style function
10719 definitions (separate parameter list and declarations). */
10722 store_parm_decls_oldstyle (tree fndecl
, const struct c_arg_info
*arg_info
)
10724 struct c_binding
*b
;
10725 tree parm
, decl
, last
;
10726 tree parmids
= arg_info
->parms
;
10727 hash_set
<tree
> seen_args
;
10729 if (!in_system_header_at (input_location
))
10732 pedwarn (DECL_SOURCE_LOCATION (fndecl
),
10733 OPT_Wold_style_definition
, "old-style function definition");
10735 warning_at (DECL_SOURCE_LOCATION (fndecl
),
10736 OPT_Wold_style_definition
,
10737 "old-style function definition");
10740 if (current_scope
->had_vla_unspec
)
10741 error ("%<[*]%> not allowed in other than function prototype scope");
10743 /* Match each formal parameter name with its declaration. Save each
10744 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
10745 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
10747 if (TREE_VALUE (parm
) == NULL_TREE
)
10749 error_at (DECL_SOURCE_LOCATION (fndecl
),
10750 "parameter name missing from parameter list");
10751 TREE_PURPOSE (parm
) = NULL_TREE
;
10755 b
= I_SYMBOL_BINDING (TREE_VALUE (parm
));
10756 if (b
&& B_IN_CURRENT_SCOPE (b
))
10759 /* Skip erroneous parameters. */
10760 if (decl
== error_mark_node
)
10762 /* If we got something other than a PARM_DECL it is an error. */
10763 if (TREE_CODE (decl
) != PARM_DECL
)
10765 error_at (DECL_SOURCE_LOCATION (decl
),
10766 "%qD declared as a non-parameter", decl
);
10769 /* If the declaration is already marked, we have a duplicate
10770 name. Complain and ignore the duplicate. */
10771 else if (seen_args
.contains (decl
))
10773 error_at (DECL_SOURCE_LOCATION (decl
),
10774 "multiple parameters named %qD", decl
);
10775 TREE_PURPOSE (parm
) = NULL_TREE
;
10778 /* If the declaration says "void", complain and turn it into
10780 else if (VOID_TYPE_P (TREE_TYPE (decl
)))
10782 error_at (DECL_SOURCE_LOCATION (decl
),
10783 "parameter %qD declared with void type", decl
);
10784 TREE_TYPE (decl
) = integer_type_node
;
10785 DECL_ARG_TYPE (decl
) = integer_type_node
;
10786 layout_decl (decl
, 0);
10788 warn_if_shadowing (decl
);
10790 /* If no declaration found, default to int. */
10793 /* FIXME diagnostics: This should be the location of the argument,
10794 not the FNDECL. E.g., for an old-style declaration
10796 int f10(v) { blah; }
10798 We should use the location of the V, not the F10.
10799 Unfortunately, the V is an IDENTIFIER_NODE which has no
10800 location. In the future we need locations for c_arg_info
10803 See gcc.dg/Wshadow-3.c for an example of this problem. */
10804 decl
= build_decl (DECL_SOURCE_LOCATION (fndecl
),
10805 PARM_DECL
, TREE_VALUE (parm
), integer_type_node
);
10806 DECL_ARG_TYPE (decl
) = TREE_TYPE (decl
);
10808 warn_if_shadowing (decl
);
10811 permerror_opt (DECL_SOURCE_LOCATION (decl
),
10812 OPT_Wimplicit_int
, "type of %qD defaults to %<int%>",
10815 warning_at (DECL_SOURCE_LOCATION (decl
),
10816 OPT_Wmissing_parameter_type
,
10817 "type of %qD defaults to %<int%>", decl
);
10820 TREE_PURPOSE (parm
) = decl
;
10821 seen_args
.add (decl
);
10824 /* Now examine the parms chain for incomplete declarations
10825 and declarations with no corresponding names. */
10827 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
10830 if (TREE_CODE (parm
) != PARM_DECL
)
10833 if (TREE_TYPE (parm
) != error_mark_node
10834 && !COMPLETE_TYPE_P (TREE_TYPE (parm
)))
10836 error_at (DECL_SOURCE_LOCATION (parm
),
10837 "parameter %qD has incomplete type", parm
);
10838 TREE_TYPE (parm
) = error_mark_node
;
10841 if (!seen_args
.contains (parm
))
10843 error_at (DECL_SOURCE_LOCATION (parm
),
10844 "declaration for parameter %qD but no such parameter",
10847 /* Pretend the parameter was not missing.
10848 This gets us to a standard state and minimizes
10849 further error messages. */
10850 parmids
= chainon (parmids
, tree_cons (parm
, 0, 0));
10854 /* Chain the declarations together in the order of the list of
10855 names. Store that chain in the function decl, replacing the
10856 list of names. Update the current scope to match. */
10857 DECL_ARGUMENTS (fndecl
) = NULL_TREE
;
10859 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
10860 if (TREE_PURPOSE (parm
))
10862 if (parm
&& TREE_PURPOSE (parm
))
10864 last
= TREE_PURPOSE (parm
);
10865 DECL_ARGUMENTS (fndecl
) = last
;
10867 for (parm
= TREE_CHAIN (parm
); parm
; parm
= TREE_CHAIN (parm
))
10868 if (TREE_PURPOSE (parm
))
10870 DECL_CHAIN (last
) = TREE_PURPOSE (parm
);
10871 last
= TREE_PURPOSE (parm
);
10873 DECL_CHAIN (last
) = NULL_TREE
;
10876 /* If there was a previous prototype,
10877 set the DECL_ARG_TYPE of each argument according to
10878 the type previously specified, and report any mismatches. */
10880 if (current_function_prototype_arg_types
)
10883 for (parm
= DECL_ARGUMENTS (fndecl
),
10884 type
= current_function_prototype_arg_types
;
10885 parm
|| (type
!= NULL_TREE
10886 && TREE_VALUE (type
) != error_mark_node
10887 && TYPE_MAIN_VARIANT (TREE_VALUE (type
)) != void_type_node
);
10888 parm
= DECL_CHAIN (parm
), type
= TREE_CHAIN (type
))
10890 if (parm
== NULL_TREE
10891 || type
== NULL_TREE
10892 || (TREE_VALUE (type
) != error_mark_node
10893 && TYPE_MAIN_VARIANT (TREE_VALUE (type
)) == void_type_node
))
10895 if (current_function_prototype_built_in
)
10896 warning_at (DECL_SOURCE_LOCATION (fndecl
),
10897 0, "number of arguments doesn%'t match "
10898 "built-in prototype");
10901 /* FIXME diagnostics: This should be the location of
10902 FNDECL, but there is bug when a prototype is
10903 declared inside function context, but defined
10904 outside of it (e.g., gcc.dg/pr15698-2.c). In
10905 which case FNDECL gets the location of the
10906 prototype, not the definition. */
10907 error_at (input_location
,
10908 "number of arguments doesn%'t match prototype");
10910 error_at (current_function_prototype_locus
,
10911 "prototype declaration");
10915 /* Type for passing arg must be consistent with that
10916 declared for the arg. ISO C says we take the unqualified
10917 type for parameters declared with qualified type. */
10918 if (TREE_TYPE (parm
) != error_mark_node
10919 && TREE_VALUE (type
) != error_mark_node
10920 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm
))
10921 != TYPE_ATOMIC (TREE_VALUE (type
)))
10922 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm
)),
10923 TYPE_MAIN_VARIANT (TREE_VALUE (type
)))))
10925 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm
))
10926 == TYPE_ATOMIC (TREE_VALUE (type
)))
10927 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm
))
10928 == TYPE_MAIN_VARIANT (TREE_VALUE (type
))))
10930 /* Adjust argument to match prototype. E.g. a previous
10931 `int foo(float);' prototype causes
10932 `int foo(x) float x; {...}' to be treated like
10933 `int foo(float x) {...}'. This is particularly
10934 useful for argument types like uid_t. */
10935 DECL_ARG_TYPE (parm
) = TREE_TYPE (parm
);
10937 if (targetm
.calls
.promote_prototypes (TREE_TYPE (current_function_decl
))
10938 && INTEGRAL_TYPE_P (TREE_TYPE (parm
))
10939 && (TYPE_PRECISION (TREE_TYPE (parm
))
10940 < TYPE_PRECISION (integer_type_node
)))
10941 DECL_ARG_TYPE (parm
)
10942 = c_type_promotes_to (TREE_TYPE (parm
));
10944 /* ??? Is it possible to get here with a
10945 built-in prototype or will it always have
10946 been diagnosed as conflicting with an
10947 old-style definition and discarded? */
10948 if (current_function_prototype_built_in
)
10949 warning_at (DECL_SOURCE_LOCATION (parm
),
10950 OPT_Wpedantic
, "promoted argument %qD "
10951 "doesn%'t match built-in prototype", parm
);
10954 pedwarn (DECL_SOURCE_LOCATION (parm
),
10955 OPT_Wpedantic
, "promoted argument %qD "
10956 "doesn%'t match prototype", parm
);
10957 pedwarn (current_function_prototype_locus
, OPT_Wpedantic
,
10958 "prototype declaration");
10963 if (current_function_prototype_built_in
)
10964 warning_at (DECL_SOURCE_LOCATION (parm
),
10965 0, "argument %qD doesn%'t match "
10966 "built-in prototype", parm
);
10969 error_at (DECL_SOURCE_LOCATION (parm
),
10970 "argument %qD doesn%'t match prototype", parm
);
10971 error_at (current_function_prototype_locus
,
10972 "prototype declaration");
10977 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = NULL_TREE
;
10980 /* Otherwise, create a prototype that would match. */
10984 tree actual
= NULL_TREE
, last
= NULL_TREE
, type
;
10986 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= DECL_CHAIN (parm
))
10988 type
= tree_cons (NULL_TREE
, DECL_ARG_TYPE (parm
), NULL_TREE
);
10990 TREE_CHAIN (last
) = type
;
10995 type
= tree_cons (NULL_TREE
, void_type_node
, NULL_TREE
);
10997 TREE_CHAIN (last
) = type
;
11001 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
11002 of the type of this function, but we need to avoid having this
11003 affect the types of other similarly-typed functions, so we must
11004 first force the generation of an identical (but separate) type
11005 node for the relevant function type. The new node we create
11006 will be a variant of the main variant of the original function
11009 TREE_TYPE (fndecl
) = build_variant_type_copy (TREE_TYPE (fndecl
));
11011 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = actual
;
11015 /* Store parameter declarations passed in ARG_INFO into the current
11016 function declaration. */
11019 store_parm_decls_from (struct c_arg_info
*arg_info
)
11021 current_function_arg_info
= arg_info
;
11022 store_parm_decls ();
11025 /* Called by walk_tree to look for and update context-less labels
11026 or labels with context in the parent function. */
11029 set_labels_context_r (tree
*tp
, int *walk_subtrees
, void *data
)
11031 tree ctx
= static_cast<tree
>(data
);
11032 if (TREE_CODE (*tp
) == LABEL_EXPR
11033 && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp
)) == NULL_TREE
11034 || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp
)) == DECL_CONTEXT (ctx
)))
11036 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp
)) = ctx
;
11037 *walk_subtrees
= 0;
11043 /* Store the parameter declarations into the current function declaration.
11044 This is called after parsing the parameter declarations, before
11045 digesting the body of the function.
11047 For an old-style definition, construct a prototype out of the old-style
11048 parameter declarations and inject it into the function's type. */
11051 store_parm_decls (void)
11053 tree fndecl
= current_function_decl
;
11056 /* The argument information block for FNDECL. */
11057 struct c_arg_info
*arg_info
= current_function_arg_info
;
11058 current_function_arg_info
= 0;
11060 /* True if this definition is written with a prototype. In C23, an
11061 empty argument list was converted to (void) in grokparms; in
11062 older C standard versions, it does not give the function a type
11063 with a prototype for future calls. */
11064 proto
= arg_info
->types
!= 0 || arg_info
->no_named_args_stdarg_p
;
11067 store_parm_decls_newstyle (fndecl
, arg_info
);
11069 store_parm_decls_oldstyle (fndecl
, arg_info
);
11071 /* The next call to push_scope will be a function body. */
11073 next_is_function_body
= true;
11075 /* Write a record describing this function definition to the prototypes
11076 file (if requested). */
11078 gen_aux_info_record (fndecl
, 1, 0, proto
);
11080 /* Initialize the RTL code for the function. */
11081 allocate_struct_function (fndecl
, false);
11083 if (warn_unused_local_typedefs
)
11084 cfun
->language
= ggc_cleared_alloc
<language_function
> ();
11086 /* Begin the statement tree for this function. */
11087 DECL_SAVED_TREE (fndecl
) = push_stmt_list ();
11089 /* ??? Insert the contents of the pending sizes list into the function
11090 to be evaluated. The only reason left to have this is
11091 void foo(int n, int array[n++])
11092 because we throw away the array type in favor of a pointer type, and
11093 thus won't naturally see the SAVE_EXPR containing the increment. All
11094 other pending sizes would be handled by gimplify_parameters. */
11095 if (arg_info
->pending_sizes
)
11097 /* In very special circumstances, e.g. for code like
11099 void f (int a[i += 2]) {}
11100 we need to execute the atomic assignment on function entry.
11101 But in this case, it is not just a straight store, it has the
11102 op= form, which means that build_atomic_assign has generated
11103 gotos, labels, etc. Because at that time the function decl
11104 for F has not been created yet, those labels do not have any
11105 function context. But we have the fndecl now, so update the
11106 labels accordingly. gimplify_expr would crash otherwise.
11107 Or with nested functions the labels could be created with parent
11108 function's context, while when the statement is emitted at the
11109 start of the nested function, it needs the nested function's
11111 walk_tree_without_duplicates (&arg_info
->pending_sizes
,
11112 set_labels_context_r
, fndecl
);
11113 add_stmt (arg_info
->pending_sizes
);
11117 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
11118 c_finish_omp_declare_simd for function prototypes. No diagnostics
11122 temp_store_parm_decls (tree fndecl
, tree parms
)
11125 for (tree p
= parms
; p
; p
= DECL_CHAIN (p
))
11127 DECL_CONTEXT (p
) = fndecl
;
11129 bind (DECL_NAME (p
), p
, current_scope
,
11130 /*invisible=*/false, /*nested=*/false,
11135 /* Undo what temp_store_parm_decls did. */
11138 temp_pop_parm_decls (void)
11140 /* Clear all bindings in this temporary scope, so that
11141 pop_scope doesn't create a BLOCK. */
11142 struct c_binding
*b
= current_scope
->bindings
;
11143 current_scope
->bindings
= NULL
;
11144 for (; b
; b
= free_binding_and_advance (b
))
11146 gcc_assert (TREE_CODE (b
->decl
) == PARM_DECL
11147 || b
->decl
== error_mark_node
);
11148 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
11149 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
11150 if (b
->shadowed
&& b
->shadowed
->u
.type
)
11151 TREE_TYPE (b
->shadowed
->decl
) = b
->shadowed
->u
.type
;
11157 /* Finish up a function declaration and compile that function
11158 all the way to assembler language output. Then free the storage
11159 for the function definition.
11161 This is called after parsing the body of the function definition. */
11164 finish_function (location_t end_loc
)
11166 tree fndecl
= current_function_decl
;
11168 if (c_dialect_objc ())
11169 objc_finish_function ();
11171 if (TREE_CODE (fndecl
) == FUNCTION_DECL
11172 && targetm
.calls
.promote_prototypes (TREE_TYPE (fndecl
)))
11174 tree args
= DECL_ARGUMENTS (fndecl
);
11175 for (; args
; args
= DECL_CHAIN (args
))
11177 tree type
= TREE_TYPE (args
);
11178 if (INTEGRAL_TYPE_P (type
)
11179 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
11180 DECL_ARG_TYPE (args
) = c_type_promotes_to (type
);
11184 if (DECL_INITIAL (fndecl
) && DECL_INITIAL (fndecl
) != error_mark_node
)
11185 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
11187 /* Must mark the RESULT_DECL as being in this function. */
11189 if (DECL_RESULT (fndecl
) && DECL_RESULT (fndecl
) != error_mark_node
)
11190 DECL_CONTEXT (DECL_RESULT (fndecl
)) = fndecl
;
11192 if (MAIN_NAME_P (DECL_NAME (fndecl
)) && !TREE_THIS_VOLATILE (fndecl
)
11193 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl
)))
11194 == integer_type_node
&& flag_isoc99
)
11196 /* Hack. We don't want the middle-end to warn that this return
11197 is unreachable, so we mark its location as special. Using
11198 UNKNOWN_LOCATION has the problem that it gets clobbered in
11199 annotate_one_with_locus. A cleaner solution might be to
11200 ensure ! should_carry_locus_p (stmt), but that needs a flag.
11202 c_finish_return (BUILTINS_LOCATION
, integer_zero_node
, NULL_TREE
);
11205 /* Tie off the statement tree for this function. */
11206 DECL_SAVED_TREE (fndecl
) = pop_stmt_list (DECL_SAVED_TREE (fndecl
));
11208 finish_fname_decls ();
11210 /* Complain if there's no return statement only if option specified on
11212 if (warn_return_type
> 0
11213 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl
))) != VOID_TYPE
11214 && !current_function_returns_value
&& !current_function_returns_null
11215 /* Don't complain if we are no-return. */
11216 && !current_function_returns_abnormally
11217 /* Don't complain if we are declared noreturn. */
11218 && !TREE_THIS_VOLATILE (fndecl
)
11219 /* Don't warn for main(). */
11220 && !MAIN_NAME_P (DECL_NAME (fndecl
))
11221 /* Or if they didn't actually specify a return type. */
11222 && !C_FUNCTION_IMPLICIT_INT (fndecl
)
11223 /* Normally, with -Wreturn-type, flow will complain, but we might
11224 optimize out static functions. */
11225 && !TREE_PUBLIC (fndecl
)
11226 && targetm
.warn_func_return (fndecl
)
11227 && warning (OPT_Wreturn_type
,
11228 "no return statement in function returning non-void"))
11229 suppress_warning (fndecl
, OPT_Wreturn_type
);
11231 /* Complain about parameters that are only set, but never otherwise used. */
11232 if (warn_unused_but_set_parameter
)
11236 for (decl
= DECL_ARGUMENTS (fndecl
);
11238 decl
= DECL_CHAIN (decl
))
11239 if (TREE_USED (decl
)
11240 && TREE_CODE (decl
) == PARM_DECL
11241 && !DECL_READ_P (decl
)
11242 && DECL_NAME (decl
)
11243 && !DECL_ARTIFICIAL (decl
)
11244 && !warning_suppressed_p (decl
, OPT_Wunused_but_set_parameter
))
11245 warning_at (DECL_SOURCE_LOCATION (decl
),
11246 OPT_Wunused_but_set_parameter
,
11247 "parameter %qD set but not used", decl
);
11250 /* Complain about locally defined typedefs that are not used in this
11252 maybe_warn_unused_local_typedefs ();
11254 /* Possibly warn about unused parameters. */
11255 if (warn_unused_parameter
)
11256 do_warn_unused_parameter (fndecl
);
11258 /* Store the end of the function, so that we get good line number
11259 info for the epilogue. */
11260 cfun
->function_end_locus
= end_loc
;
11262 /* Finalize the ELF visibility for the function. */
11263 c_determine_visibility (fndecl
);
11265 /* For GNU C extern inline functions disregard inline limits. */
11266 if (DECL_EXTERNAL (fndecl
)
11267 && DECL_DECLARED_INLINE_P (fndecl
)
11268 && (flag_gnu89_inline
11269 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl
))))
11270 DECL_DISREGARD_INLINE_LIMITS (fndecl
) = 1;
11272 /* Genericize before inlining. Delay genericizing nested functions
11273 until their parent function is genericized. Since finalizing
11274 requires GENERIC, delay that as well. */
11276 if (DECL_INITIAL (fndecl
) && DECL_INITIAL (fndecl
) != error_mark_node
11277 && !undef_nested_function
)
11279 if (!decl_function_context (fndecl
))
11281 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE
, fndecl
);
11282 c_genericize (fndecl
);
11284 /* ??? Objc emits functions after finalizing the compilation unit.
11285 This should be cleaned up later and this conditional removed. */
11286 if (symtab
->global_info_ready
)
11288 cgraph_node::add_new_function (fndecl
, false);
11291 cgraph_node::finalize_function (fndecl
, false);
11295 /* Register this function with cgraph just far enough to get it
11296 added to our parent's nested function list. Handy, since the
11297 C front end doesn't have such a list. */
11298 (void) cgraph_node::get_create (fndecl
);
11302 if (!decl_function_context (fndecl
))
11303 undef_nested_function
= false;
11305 if (cfun
->language
!= NULL
)
11307 ggc_free (cfun
->language
);
11308 cfun
->language
= NULL
;
11311 /* We're leaving the context of this function, so zap cfun.
11312 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
11313 tree_rest_of_compilation. */
11315 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION
, current_function_decl
);
11316 current_function_decl
= NULL
;
11319 /* Check the declarations given in a for-loop for satisfying the C99
11320 constraints. If exactly one such decl is found, return it. LOC is
11321 the location of the opening parenthesis of the for loop. The last
11322 parameter allows you to control the "for loop initial declarations
11323 are only allowed in C99 mode". Normally, you should pass
11324 flag_isoc99 as that parameter. But in some cases (Objective-C
11325 foreach loop, for example) we want to run the checks in this
11326 function even if not in C99 mode, so we allow the caller to turn
11327 off the error about not being in C99 mode.
11331 check_for_loop_decls (location_t loc
, bool turn_off_iso_c99_error
)
11333 struct c_binding
*b
;
11334 tree one_decl
= NULL_TREE
;
11337 if (!turn_off_iso_c99_error
)
11339 static bool hint
= true;
11340 /* If we get here, declarations have been used in a for loop without
11341 the C99 for loop scope. This doesn't make much sense, so don't
11343 auto_diagnostic_group d
;
11344 error_at (loc
, "%<for%> loop initial declarations "
11345 "are only allowed in C99 or C11 mode");
11349 "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
11350 "%<-std=gnu11%> to compile your code");
11356 pedwarn_c90 (loc
, OPT_Wpedantic
, "ISO C90 does not support %<for%> loop "
11357 "initial declarations");
11359 /* C99 subclause 6.8.5 paragraph 3:
11361 [#3] The declaration part of a for statement shall only
11362 declare identifiers for objects having storage class auto or
11365 It isn't clear whether, in this sentence, "identifiers" binds to
11366 "shall only declare" or to "objects" - that is, whether all identifiers
11367 declared must be identifiers for objects, or whether the restriction
11368 only applies to those that are. (A question on this in comp.std.c
11369 in November 2000 received no answer.) We implement the strictest
11370 interpretation, to avoid creating an extension which later causes
11373 This constraint was removed in C23. */
11375 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
11378 tree decl
= b
->decl
;
11383 switch (TREE_CODE (decl
))
11387 location_t decl_loc
= DECL_SOURCE_LOCATION (decl
);
11388 if (TREE_STATIC (decl
))
11389 pedwarn_c11 (decl_loc
, OPT_Wpedantic
,
11390 "declaration of static variable %qD in %<for%> "
11391 "loop initial declaration", decl
);
11392 else if (DECL_EXTERNAL (decl
))
11393 pedwarn_c11 (decl_loc
, OPT_Wpedantic
,
11394 "declaration of %<extern%> variable %qD in %<for%> "
11395 "loop initial declaration", decl
);
11400 pedwarn_c11 (loc
, OPT_Wpedantic
,
11401 "%<struct %E%> declared in %<for%> loop initial "
11402 "declaration", id
);
11405 pedwarn_c11 (loc
, OPT_Wpedantic
,
11406 "%<union %E%> declared in %<for%> loop initial "
11410 case ENUMERAL_TYPE
:
11411 pedwarn_c11 (loc
, OPT_Wpedantic
,
11412 "%<enum %E%> declared in %<for%> loop "
11413 "initial declaration", id
);
11416 pedwarn_c11 (loc
, OPT_Wpedantic
, "declaration of non-variable "
11417 "%qD in %<for%> loop initial declaration", decl
);
11424 return n_decls
== 1 ? one_decl
: NULL_TREE
;
11427 /* Save and reinitialize the variables
11428 used during compilation of a C function. */
11431 c_push_function_context (void)
11433 struct language_function
*p
= cfun
->language
;
11434 /* cfun->language might have been already allocated by the use of
11435 -Wunused-local-typedefs. In that case, just re-use it. */
11437 cfun
->language
= p
= ggc_cleared_alloc
<language_function
> ();
11439 p
->base
.x_stmt_tree
= c_stmt_tree
;
11440 c_stmt_tree
.x_cur_stmt_list
= vec_safe_copy (c_stmt_tree
.x_cur_stmt_list
);
11441 p
->x_in_statement
= in_statement
;
11442 p
->x_switch_stack
= c_switch_stack
;
11443 p
->arg_info
= current_function_arg_info
;
11444 p
->returns_value
= current_function_returns_value
;
11445 p
->returns_null
= current_function_returns_null
;
11446 p
->returns_abnormally
= current_function_returns_abnormally
;
11447 p
->warn_about_return_type
= warn_about_return_type
;
11449 push_function_context ();
11452 /* Restore the variables used during compilation of a C function. */
11455 c_pop_function_context (void)
11457 struct language_function
*p
;
11459 pop_function_context ();
11460 p
= cfun
->language
;
11462 /* When -Wunused-local-typedefs is in effect, cfun->languages is
11463 used to store data throughout the life time of the current cfun,
11464 So don't deallocate it. */
11465 if (!warn_unused_local_typedefs
)
11466 cfun
->language
= NULL
;
11468 if (DECL_STRUCT_FUNCTION (current_function_decl
) == 0
11469 && DECL_SAVED_TREE (current_function_decl
) == NULL_TREE
)
11471 /* Stop pointing to the local nodes about to be freed. */
11472 /* But DECL_INITIAL must remain nonzero so we know this
11473 was an actual function definition. */
11474 DECL_INITIAL (current_function_decl
) = error_mark_node
;
11475 DECL_ARGUMENTS (current_function_decl
) = NULL_TREE
;
11478 c_stmt_tree
= p
->base
.x_stmt_tree
;
11479 p
->base
.x_stmt_tree
.x_cur_stmt_list
= NULL
;
11480 in_statement
= p
->x_in_statement
;
11481 c_switch_stack
= p
->x_switch_stack
;
11482 current_function_arg_info
= p
->arg_info
;
11483 current_function_returns_value
= p
->returns_value
;
11484 current_function_returns_null
= p
->returns_null
;
11485 current_function_returns_abnormally
= p
->returns_abnormally
;
11486 warn_about_return_type
= p
->warn_about_return_type
;
11489 /* The functions below are required for functionality of doing
11490 function at once processing in the C front end. Currently these
11491 functions are not called from anywhere in the C front end, but as
11492 these changes continue, that will change. */
11494 /* Returns the stmt_tree (if any) to which statements are currently
11495 being added. If there is no active statement-tree, NULL is
11499 current_stmt_tree (void)
11501 return &c_stmt_tree
;
11504 /* Return the global value of T as a symbol. */
11507 identifier_global_value (tree t
)
11509 struct c_binding
*b
;
11511 for (b
= I_SYMBOL_BINDING (t
); b
; b
= b
->shadowed
)
11512 if (B_IN_FILE_SCOPE (b
) || B_IN_EXTERNAL_SCOPE (b
))
11518 /* Return the global value of tag T as a symbol. */
11521 identifier_global_tag (tree t
)
11523 struct c_binding
*b
;
11525 for (b
= I_TAG_BINDING (t
); b
; b
= b
->shadowed
)
11526 if (B_IN_FILE_SCOPE (b
) || B_IN_EXTERNAL_SCOPE (b
))
11532 /* Returns true if NAME refers to a built-in function or function-like
11536 names_builtin_p (const char *name
)
11538 tree id
= get_identifier (name
);
11539 if (tree decl
= identifier_global_value (id
))
11540 return TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_IS_UNDECLARED_BUILTIN (decl
);
11542 /* Also detect common reserved C words that aren't strictly built-in
11544 switch (C_RID_CODE (id
))
11546 case RID_BUILTIN_ASSOC_BARRIER
:
11547 case RID_BUILTIN_CONVERTVECTOR
:
11548 case RID_BUILTIN_HAS_ATTRIBUTE
:
11549 case RID_BUILTIN_SHUFFLE
:
11550 case RID_BUILTIN_SHUFFLEVECTOR
:
11551 case RID_BUILTIN_STDC
:
11552 case RID_CHOOSE_EXPR
:
11554 case RID_TYPES_COMPATIBLE_P
:
11563 /* In C, the only C-linkage public declaration is at file scope. */
11566 c_linkage_bindings (tree name
)
11568 return identifier_global_value (name
);
11571 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
11572 otherwise the name is found in ridpointers from RID_INDEX. */
11575 record_builtin_type (enum rid rid_index
, const char *name
, tree type
)
11579 id
= ridpointers
[(int) rid_index
];
11581 id
= get_identifier (name
);
11582 decl
= build_decl (UNKNOWN_LOCATION
, TYPE_DECL
, id
, type
);
11584 if (debug_hooks
->type_decl
)
11585 debug_hooks
->type_decl (decl
, false);
11588 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
11591 build_c_parm (struct c_declspecs
*specs
, tree attrs
,
11592 struct c_declarator
*declarator
,
11595 struct c_parm
*ret
= XOBNEW (&parser_obstack
, struct c_parm
);
11596 ret
->specs
= specs
;
11597 ret
->attrs
= attrs
;
11598 ret
->declarator
= declarator
;
11603 /* Return a declarator with nested attributes. TARGET is the inner
11604 declarator to which these attributes apply. ATTRS are the
11607 struct c_declarator
*
11608 build_attrs_declarator (tree attrs
, struct c_declarator
*target
)
11610 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
11611 ret
->kind
= cdk_attrs
;
11612 ret
->declarator
= target
;
11613 ret
->u
.attrs
= attrs
;
11617 /* Return a declarator for a function with arguments specified by ARGS
11618 and return type specified by TARGET. */
11620 struct c_declarator
*
11621 build_function_declarator (struct c_arg_info
*args
,
11622 struct c_declarator
*target
)
11624 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
11625 ret
->kind
= cdk_function
;
11626 ret
->declarator
= target
;
11627 ret
->u
.arg_info
= args
;
11631 /* Return a declarator for the identifier IDENT (which may be
11632 NULL_TREE for an abstract declarator). */
11634 struct c_declarator
*
11635 build_id_declarator (tree ident
)
11637 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
11638 ret
->kind
= cdk_id
;
11639 ret
->declarator
= 0;
11640 ret
->u
.id
.id
= ident
;
11641 ret
->u
.id
.attrs
= NULL_TREE
;
11642 /* Default value - may get reset to a more precise location. */
11643 ret
->id_loc
= input_location
;
11647 /* Return something to represent absolute declarators containing a *.
11648 TARGET is the absolute declarator that the * contains.
11649 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
11650 to apply to the pointer type. */
11652 struct c_declarator
*
11653 make_pointer_declarator (struct c_declspecs
*type_quals_attrs
,
11654 struct c_declarator
*target
)
11658 struct c_declarator
*itarget
= target
;
11659 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
11660 if (type_quals_attrs
)
11662 attrs
= type_quals_attrs
->attrs
;
11663 quals
= quals_from_declspecs (type_quals_attrs
);
11664 if (attrs
!= NULL_TREE
)
11665 itarget
= build_attrs_declarator (attrs
, target
);
11667 ret
->kind
= cdk_pointer
;
11668 ret
->declarator
= itarget
;
11669 ret
->u
.pointer_quals
= quals
;
11673 /* Return a pointer to a structure for an empty list of declaration
11676 struct c_declspecs
*
11677 build_null_declspecs (void)
11679 struct c_declspecs
*ret
= XOBNEW (&parser_obstack
, struct c_declspecs
);
11680 memset (ret
, 0, sizeof *ret
);
11681 ret
->align_log
= -1;
11682 ret
->typespec_word
= cts_none
;
11683 ret
->storage_class
= csc_none
;
11684 ret
->expr_const_operands
= true;
11685 ret
->typespec_kind
= ctsk_none
;
11686 ret
->address_space
= ADDR_SPACE_GENERIC
;
11690 /* Add the address space ADDRSPACE to the declaration specifiers
11691 SPECS, returning SPECS. */
11693 struct c_declspecs
*
11694 declspecs_add_addrspace (location_t location
,
11695 struct c_declspecs
*specs
, addr_space_t as
)
11697 specs
->non_sc_seen_p
= true;
11698 specs
->declspecs_seen_p
= true;
11699 specs
->non_std_attrs_seen_p
= true;
11701 if (!ADDR_SPACE_GENERIC_P (specs
->address_space
)
11702 && specs
->address_space
!= as
)
11703 error ("incompatible address space qualifiers %qs and %qs",
11704 c_addr_space_name (as
),
11705 c_addr_space_name (specs
->address_space
));
11708 specs
->address_space
= as
;
11709 specs
->locations
[cdw_address_space
] = location
;
11714 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
11715 returning SPECS. */
11717 struct c_declspecs
*
11718 declspecs_add_qual (location_t loc
,
11719 struct c_declspecs
*specs
, tree qual
)
11723 specs
->non_sc_seen_p
= true;
11724 specs
->declspecs_seen_p
= true;
11725 specs
->non_std_attrs_seen_p
= true;
11726 gcc_assert (TREE_CODE (qual
) == IDENTIFIER_NODE
11727 && C_IS_RESERVED_WORD (qual
));
11728 i
= C_RID_CODE (qual
);
11729 location_t prev_loc
= UNKNOWN_LOCATION
;
11733 dupe
= specs
->const_p
;
11734 specs
->const_p
= true;
11735 prev_loc
= specs
->locations
[cdw_const
];
11736 specs
->locations
[cdw_const
] = loc
;
11739 dupe
= specs
->volatile_p
;
11740 specs
->volatile_p
= true;
11741 prev_loc
= specs
->locations
[cdw_volatile
];
11742 specs
->locations
[cdw_volatile
] = loc
;
11745 dupe
= specs
->restrict_p
;
11746 specs
->restrict_p
= true;
11747 prev_loc
= specs
->locations
[cdw_restrict
];
11748 specs
->locations
[cdw_restrict
] = loc
;
11751 dupe
= specs
->atomic_p
;
11752 specs
->atomic_p
= true;
11753 prev_loc
= specs
->locations
[cdw_atomic
];
11754 specs
->locations
[cdw_atomic
] = loc
;
11757 gcc_unreachable ();
11761 bool warned
= pedwarn_c90 (loc
, OPT_Wpedantic
,
11762 "duplicate %qE declaration specifier", qual
);
11764 && warn_duplicate_decl_specifier
11765 && prev_loc
>= RESERVED_LOCATION_COUNT
11766 && !from_macro_expansion_at (prev_loc
)
11767 && !from_macro_expansion_at (loc
))
11768 warning_at (loc
, OPT_Wduplicate_decl_specifier
,
11769 "duplicate %qE declaration specifier", qual
);
11774 /* Add the type specifier TYPE to the declaration specifiers SPECS,
11775 returning SPECS. */
11777 struct c_declspecs
*
11778 declspecs_add_type (location_t loc
, struct c_declspecs
*specs
,
11779 struct c_typespec spec
)
11781 tree type
= spec
.spec
;
11782 specs
->non_sc_seen_p
= true;
11783 specs
->declspecs_seen_p
= true;
11784 specs
->non_std_attrs_seen_p
= true;
11785 specs
->typespec_kind
= spec
.kind
;
11786 if (TREE_DEPRECATED (type
))
11787 specs
->deprecated_p
= true;
11788 if (TREE_UNAVAILABLE (type
))
11789 specs
->unavailable_p
= true;
11791 /* As a type specifier is present, "auto" must be used as a storage
11792 class specifier, not for type deduction. */
11793 if (specs
->c23_auto_p
)
11795 specs
->c23_auto_p
= false;
11796 if (specs
->storage_class
!= csc_none
)
11797 error ("multiple storage classes in declaration specifiers");
11798 else if (specs
->thread_p
)
11799 error ("%qs used with %<auto%>",
11800 specs
->thread_gnu_p
? "__thread" : "_Thread_local");
11801 else if (specs
->constexpr_p
)
11802 /* auto may only be used with another storage class specifier,
11803 such as constexpr, if the type is inferred. */
11804 error ("%<auto%> used with %<constexpr%>");
11806 specs
->storage_class
= csc_auto
;
11809 /* Handle type specifier keywords. */
11810 if (TREE_CODE (type
) == IDENTIFIER_NODE
11811 && C_IS_RESERVED_WORD (type
)
11812 && C_RID_CODE (type
) != RID_CXX_COMPAT_WARN
)
11814 enum rid i
= C_RID_CODE (type
);
11817 error_at (loc
, "two or more data types in declaration specifiers");
11820 if ((int) i
<= (int) RID_LAST_MODIFIER
)
11822 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
11827 if (specs
->long_long_p
)
11829 error_at (loc
, "%<long long long%> is too long for GCC");
11834 if (specs
->typespec_word
== cts_double
)
11837 ("both %<long long%> and %<double%> in "
11838 "declaration specifiers"));
11841 pedwarn_c90 (loc
, OPT_Wlong_long
,
11842 "ISO C90 does not support %<long long%>");
11843 specs
->long_long_p
= 1;
11844 specs
->locations
[cdw_long_long
] = loc
;
11847 if (specs
->short_p
)
11849 ("both %<long%> and %<short%> in "
11850 "declaration specifiers"));
11851 else if (specs
->typespec_word
== cts_auto_type
)
11853 ("both %<long%> and %<__auto_type%> in "
11854 "declaration specifiers"));
11855 else if (specs
->typespec_word
== cts_void
)
11857 ("both %<long%> and %<void%> in "
11858 "declaration specifiers"));
11859 else if (specs
->typespec_word
== cts_int_n
)
11861 ("both %<long%> and %<__int%d%> in "
11862 "declaration specifiers"),
11863 int_n_data
[specs
->u
.int_n_idx
].bitsize
);
11864 else if (specs
->typespec_word
== cts_bool
)
11866 ("both %<long%> and %<_Bool%> in "
11867 "declaration specifiers"));
11868 else if (specs
->typespec_word
== cts_bitint
)
11870 ("both %<long%> and %<_BitInt%> in "
11871 "declaration specifiers"));
11872 else if (specs
->typespec_word
== cts_char
)
11874 ("both %<long%> and %<char%> in "
11875 "declaration specifiers"));
11876 else if (specs
->typespec_word
== cts_float
)
11878 ("both %<long%> and %<float%> in "
11879 "declaration specifiers"));
11880 else if (specs
->typespec_word
== cts_floatn_nx
)
11882 ("both %<long%> and %<_Float%d%s%> in "
11883 "declaration specifiers"),
11884 floatn_nx_types
[specs
->u
.floatn_nx_idx
].n
,
11885 (floatn_nx_types
[specs
->u
.floatn_nx_idx
].extended
11888 else if (specs
->typespec_word
== cts_dfloat32
)
11890 ("both %<long%> and %<_Decimal32%> in "
11891 "declaration specifiers"));
11892 else if (specs
->typespec_word
== cts_dfloat64
)
11894 ("both %<long%> and %<_Decimal64%> in "
11895 "declaration specifiers"));
11896 else if (specs
->typespec_word
== cts_dfloat128
)
11898 ("both %<long%> and %<_Decimal128%> in "
11899 "declaration specifiers"));
11902 specs
->long_p
= true;
11903 specs
->locations
[cdw_long
] = loc
;
11907 dupe
= specs
->short_p
;
11910 ("both %<long%> and %<short%> in "
11911 "declaration specifiers"));
11912 else if (specs
->typespec_word
== cts_auto_type
)
11914 ("both %<short%> and %<__auto_type%> in "
11915 "declaration specifiers"));
11916 else if (specs
->typespec_word
== cts_void
)
11918 ("both %<short%> and %<void%> in "
11919 "declaration specifiers"));
11920 else if (specs
->typespec_word
== cts_int_n
)
11922 ("both %<short%> and %<__int%d%> in "
11923 "declaration specifiers"),
11924 int_n_data
[specs
->u
.int_n_idx
].bitsize
);
11925 else if (specs
->typespec_word
== cts_bool
)
11927 ("both %<short%> and %<_Bool%> in "
11928 "declaration specifiers"));
11929 else if (specs
->typespec_word
== cts_bitint
)
11931 ("both %<short%> and %<_BitInt%> in "
11932 "declaration specifiers"));
11933 else if (specs
->typespec_word
== cts_char
)
11935 ("both %<short%> and %<char%> in "
11936 "declaration specifiers"));
11937 else if (specs
->typespec_word
== cts_float
)
11939 ("both %<short%> and %<float%> in "
11940 "declaration specifiers"));
11941 else if (specs
->typespec_word
== cts_double
)
11943 ("both %<short%> and %<double%> in "
11944 "declaration specifiers"));
11945 else if (specs
->typespec_word
== cts_floatn_nx
)
11947 ("both %<short%> and %<_Float%d%s%> in "
11948 "declaration specifiers"),
11949 floatn_nx_types
[specs
->u
.floatn_nx_idx
].n
,
11950 (floatn_nx_types
[specs
->u
.floatn_nx_idx
].extended
11953 else if (specs
->typespec_word
== cts_dfloat32
)
11955 ("both %<short%> and %<_Decimal32%> in "
11956 "declaration specifiers"));
11957 else if (specs
->typespec_word
== cts_dfloat64
)
11959 ("both %<short%> and %<_Decimal64%> in "
11960 "declaration specifiers"));
11961 else if (specs
->typespec_word
== cts_dfloat128
)
11963 ("both %<short%> and %<_Decimal128%> in "
11964 "declaration specifiers"));
11967 specs
->short_p
= true;
11968 specs
->locations
[cdw_short
] = loc
;
11972 dupe
= specs
->signed_p
;
11973 if (specs
->unsigned_p
)
11975 ("both %<signed%> and %<unsigned%> in "
11976 "declaration specifiers"));
11977 else if (specs
->typespec_word
== cts_auto_type
)
11979 ("both %<signed%> and %<__auto_type%> in "
11980 "declaration specifiers"));
11981 else if (specs
->typespec_word
== cts_void
)
11983 ("both %<signed%> and %<void%> in "
11984 "declaration specifiers"));
11985 else if (specs
->typespec_word
== cts_bool
)
11987 ("both %<signed%> and %<_Bool%> in "
11988 "declaration specifiers"));
11989 else if (specs
->typespec_word
== cts_float
)
11991 ("both %<signed%> and %<float%> in "
11992 "declaration specifiers"));
11993 else if (specs
->typespec_word
== cts_double
)
11995 ("both %<signed%> and %<double%> in "
11996 "declaration specifiers"));
11997 else if (specs
->typespec_word
== cts_floatn_nx
)
11999 ("both %<signed%> and %<_Float%d%s%> in "
12000 "declaration specifiers"),
12001 floatn_nx_types
[specs
->u
.floatn_nx_idx
].n
,
12002 (floatn_nx_types
[specs
->u
.floatn_nx_idx
].extended
12005 else if (specs
->typespec_word
== cts_dfloat32
)
12007 ("both %<signed%> and %<_Decimal32%> in "
12008 "declaration specifiers"));
12009 else if (specs
->typespec_word
== cts_dfloat64
)
12011 ("both %<signed%> and %<_Decimal64%> in "
12012 "declaration specifiers"));
12013 else if (specs
->typespec_word
== cts_dfloat128
)
12015 ("both %<signed%> and %<_Decimal128%> in "
12016 "declaration specifiers"));
12019 specs
->signed_p
= true;
12020 specs
->locations
[cdw_signed
] = loc
;
12024 dupe
= specs
->unsigned_p
;
12025 if (specs
->signed_p
)
12027 ("both %<signed%> and %<unsigned%> in "
12028 "declaration specifiers"));
12029 else if (specs
->typespec_word
== cts_auto_type
)
12031 ("both %<unsigned%> and %<__auto_type%> in "
12032 "declaration specifiers"));
12033 else if (specs
->typespec_word
== cts_void
)
12035 ("both %<unsigned%> and %<void%> in "
12036 "declaration specifiers"));
12037 else if (specs
->typespec_word
== cts_bool
)
12039 ("both %<unsigned%> and %<_Bool%> in "
12040 "declaration specifiers"));
12041 else if (specs
->typespec_word
== cts_float
)
12043 ("both %<unsigned%> and %<float%> in "
12044 "declaration specifiers"));
12045 else if (specs
->typespec_word
== cts_double
)
12047 ("both %<unsigned%> and %<double%> in "
12048 "declaration specifiers"));
12049 else if (specs
->typespec_word
== cts_floatn_nx
)
12051 ("both %<unsigned%> and %<_Float%d%s%> in "
12052 "declaration specifiers"),
12053 floatn_nx_types
[specs
->u
.floatn_nx_idx
].n
,
12054 (floatn_nx_types
[specs
->u
.floatn_nx_idx
].extended
12057 else if (specs
->typespec_word
== cts_dfloat32
)
12059 ("both %<unsigned%> and %<_Decimal32%> in "
12060 "declaration specifiers"));
12061 else if (specs
->typespec_word
== cts_dfloat64
)
12063 ("both %<unsigned%> and %<_Decimal64%> in "
12064 "declaration specifiers"));
12065 else if (specs
->typespec_word
== cts_dfloat128
)
12067 ("both %<unsigned%> and %<_Decimal128%> in "
12068 "declaration specifiers"));
12071 specs
->unsigned_p
= true;
12072 specs
->locations
[cdw_unsigned
] = loc
;
12076 dupe
= specs
->complex_p
;
12077 if (!in_system_header_at (loc
))
12078 pedwarn_c90 (loc
, OPT_Wpedantic
,
12079 "ISO C90 does not support complex types");
12080 if (specs
->typespec_word
== cts_auto_type
)
12082 ("both %<complex%> and %<__auto_type%> in "
12083 "declaration specifiers"));
12084 else if (specs
->typespec_word
== cts_void
)
12086 ("both %<complex%> and %<void%> in "
12087 "declaration specifiers"));
12088 else if (specs
->typespec_word
== cts_bool
)
12090 ("both %<complex%> and %<_Bool%> in "
12091 "declaration specifiers"));
12092 else if (specs
->typespec_word
== cts_bitint
)
12094 ("both %<complex%> and %<_BitInt%> in "
12095 "declaration specifiers"));
12096 else if (specs
->typespec_word
== cts_dfloat32
)
12098 ("both %<complex%> and %<_Decimal32%> in "
12099 "declaration specifiers"));
12100 else if (specs
->typespec_word
== cts_dfloat64
)
12102 ("both %<complex%> and %<_Decimal64%> in "
12103 "declaration specifiers"));
12104 else if (specs
->typespec_word
== cts_dfloat128
)
12106 ("both %<complex%> and %<_Decimal128%> in "
12107 "declaration specifiers"));
12108 else if (specs
->typespec_word
== cts_fract
)
12110 ("both %<complex%> and %<_Fract%> in "
12111 "declaration specifiers"));
12112 else if (specs
->typespec_word
== cts_accum
)
12114 ("both %<complex%> and %<_Accum%> in "
12115 "declaration specifiers"));
12116 else if (specs
->saturating_p
)
12118 ("both %<complex%> and %<_Sat%> in "
12119 "declaration specifiers"));
12122 specs
->complex_p
= true;
12123 specs
->locations
[cdw_complex
] = loc
;
12127 dupe
= specs
->saturating_p
;
12128 pedwarn (loc
, OPT_Wpedantic
,
12129 "ISO C does not support saturating types");
12130 if (specs
->typespec_word
== cts_int_n
)
12133 ("both %<_Sat%> and %<__int%d%> in "
12134 "declaration specifiers"),
12135 int_n_data
[specs
->u
.int_n_idx
].bitsize
);
12137 else if (specs
->typespec_word
== cts_auto_type
)
12139 ("both %<_Sat%> and %<__auto_type%> in "
12140 "declaration specifiers"));
12141 else if (specs
->typespec_word
== cts_void
)
12143 ("both %<_Sat%> and %<void%> in "
12144 "declaration specifiers"));
12145 else if (specs
->typespec_word
== cts_bool
)
12147 ("both %<_Sat%> and %<_Bool%> in "
12148 "declaration specifiers"));
12149 else if (specs
->typespec_word
== cts_bitint
)
12151 ("both %<_Sat%> and %<_BitInt%> in "
12152 "declaration specifiers"));
12153 else if (specs
->typespec_word
== cts_char
)
12155 ("both %<_Sat%> and %<char%> in "
12156 "declaration specifiers"));
12157 else if (specs
->typespec_word
== cts_int
)
12159 ("both %<_Sat%> and %<int%> in "
12160 "declaration specifiers"));
12161 else if (specs
->typespec_word
== cts_float
)
12163 ("both %<_Sat%> and %<float%> in "
12164 "declaration specifiers"));
12165 else if (specs
->typespec_word
== cts_double
)
12167 ("both %<_Sat%> and %<double%> in "
12168 "declaration specifiers"));
12169 else if (specs
->typespec_word
== cts_floatn_nx
)
12171 ("both %<_Sat%> and %<_Float%d%s%> in "
12172 "declaration specifiers"),
12173 floatn_nx_types
[specs
->u
.floatn_nx_idx
].n
,
12174 (floatn_nx_types
[specs
->u
.floatn_nx_idx
].extended
12177 else if (specs
->typespec_word
== cts_dfloat32
)
12179 ("both %<_Sat%> and %<_Decimal32%> in "
12180 "declaration specifiers"));
12181 else if (specs
->typespec_word
== cts_dfloat64
)
12183 ("both %<_Sat%> and %<_Decimal64%> in "
12184 "declaration specifiers"));
12185 else if (specs
->typespec_word
== cts_dfloat128
)
12187 ("both %<_Sat%> and %<_Decimal128%> in "
12188 "declaration specifiers"));
12189 else if (specs
->complex_p
)
12191 ("both %<_Sat%> and %<complex%> in "
12192 "declaration specifiers"));
12195 specs
->saturating_p
= true;
12196 specs
->locations
[cdw_saturating
] = loc
;
12200 gcc_unreachable ();
12204 error_at (loc
, "duplicate %qE", type
);
12210 /* "void", "_Bool", "char", "int", "float", "double",
12211 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
12212 "_Decimal64", "_Decimal128", "_Fract", "_Accum", "_BitInt(N)" or
12214 if (specs
->typespec_word
!= cts_none
)
12217 "two or more data types in declaration specifiers");
12222 case RID_AUTO_TYPE
:
12225 ("both %<long%> and %<__auto_type%> in "
12226 "declaration specifiers"));
12227 else if (specs
->short_p
)
12229 ("both %<short%> and %<__auto_type%> in "
12230 "declaration specifiers"));
12231 else if (specs
->signed_p
)
12233 ("both %<signed%> and %<__auto_type%> in "
12234 "declaration specifiers"));
12235 else if (specs
->unsigned_p
)
12237 ("both %<unsigned%> and %<__auto_type%> in "
12238 "declaration specifiers"));
12239 else if (specs
->complex_p
)
12241 ("both %<complex%> and %<__auto_type%> in "
12242 "declaration specifiers"));
12243 else if (specs
->saturating_p
)
12245 ("both %<_Sat%> and %<__auto_type%> in "
12246 "declaration specifiers"));
12249 specs
->typespec_word
= cts_auto_type
;
12250 specs
->locations
[cdw_typespec
] = loc
;
12257 specs
->u
.int_n_idx
= i
- RID_INT_N_0
;
12258 if (!in_system_header_at (input_location
)
12259 /* If the INT_N type ends in "__", and so is of the format
12260 "__intN__", don't pedwarn. */
12261 && (strncmp (IDENTIFIER_POINTER (type
)
12262 + (IDENTIFIER_LENGTH (type
) - 2), "__", 2) != 0))
12263 pedwarn (loc
, OPT_Wpedantic
,
12264 "ISO C does not support %<__int%d%> types",
12265 int_n_data
[specs
->u
.int_n_idx
].bitsize
);
12269 ("both %<__int%d%> and %<long%> in "
12270 "declaration specifiers"),
12271 int_n_data
[specs
->u
.int_n_idx
].bitsize
);
12272 else if (specs
->saturating_p
)
12274 ("both %<_Sat%> and %<__int%d%> in "
12275 "declaration specifiers"),
12276 int_n_data
[specs
->u
.int_n_idx
].bitsize
);
12277 else if (specs
->short_p
)
12279 ("both %<__int%d%> and %<short%> in "
12280 "declaration specifiers"),
12281 int_n_data
[specs
->u
.int_n_idx
].bitsize
);
12282 else if (! int_n_enabled_p
[specs
->u
.int_n_idx
])
12284 specs
->typespec_word
= cts_int_n
;
12286 "%<__int%d%> is not supported on this target",
12287 int_n_data
[specs
->u
.int_n_idx
].bitsize
);
12291 specs
->typespec_word
= cts_int_n
;
12292 specs
->locations
[cdw_typespec
] = loc
;
12298 ("both %<long%> and %<void%> in "
12299 "declaration specifiers"));
12300 else if (specs
->short_p
)
12302 ("both %<short%> and %<void%> in "
12303 "declaration specifiers"));
12304 else if (specs
->signed_p
)
12306 ("both %<signed%> and %<void%> in "
12307 "declaration specifiers"));
12308 else if (specs
->unsigned_p
)
12310 ("both %<unsigned%> and %<void%> in "
12311 "declaration specifiers"));
12312 else if (specs
->complex_p
)
12314 ("both %<complex%> and %<void%> in "
12315 "declaration specifiers"));
12316 else if (specs
->saturating_p
)
12318 ("both %<_Sat%> and %<void%> in "
12319 "declaration specifiers"));
12322 specs
->typespec_word
= cts_void
;
12323 specs
->locations
[cdw_typespec
] = loc
;
12327 if (!in_system_header_at (loc
))
12328 pedwarn_c90 (loc
, OPT_Wpedantic
,
12329 "ISO C90 does not support boolean types");
12332 ("both %<long%> and %<_Bool%> in "
12333 "declaration specifiers"));
12334 else if (specs
->short_p
)
12336 ("both %<short%> and %<_Bool%> in "
12337 "declaration specifiers"));
12338 else if (specs
->signed_p
)
12340 ("both %<signed%> and %<_Bool%> in "
12341 "declaration specifiers"));
12342 else if (specs
->unsigned_p
)
12344 ("both %<unsigned%> and %<_Bool%> in "
12345 "declaration specifiers"));
12346 else if (specs
->complex_p
)
12348 ("both %<complex%> and %<_Bool%> in "
12349 "declaration specifiers"));
12350 else if (specs
->saturating_p
)
12352 ("both %<_Sat%> and %<_Bool%> in "
12353 "declaration specifiers"));
12356 specs
->typespec_word
= cts_bool
;
12357 specs
->locations
[cdw_typespec
] = loc
;
12363 ("both %<long%> and %<char%> in "
12364 "declaration specifiers"));
12365 else if (specs
->short_p
)
12367 ("both %<short%> and %<char%> in "
12368 "declaration specifiers"));
12369 else if (specs
->saturating_p
)
12371 ("both %<_Sat%> and %<char%> in "
12372 "declaration specifiers"));
12375 specs
->typespec_word
= cts_char
;
12376 specs
->locations
[cdw_typespec
] = loc
;
12380 if (specs
->saturating_p
)
12382 ("both %<_Sat%> and %<int%> in "
12383 "declaration specifiers"));
12386 specs
->typespec_word
= cts_int
;
12387 specs
->locations
[cdw_typespec
] = loc
;
12393 ("both %<long%> and %<float%> in "
12394 "declaration specifiers"));
12395 else if (specs
->short_p
)
12397 ("both %<short%> and %<float%> in "
12398 "declaration specifiers"));
12399 else if (specs
->signed_p
)
12401 ("both %<signed%> and %<float%> in "
12402 "declaration specifiers"));
12403 else if (specs
->unsigned_p
)
12405 ("both %<unsigned%> and %<float%> in "
12406 "declaration specifiers"));
12407 else if (specs
->saturating_p
)
12409 ("both %<_Sat%> and %<float%> in "
12410 "declaration specifiers"));
12413 specs
->typespec_word
= cts_float
;
12414 specs
->locations
[cdw_typespec
] = loc
;
12418 if (specs
->long_long_p
)
12420 ("both %<long long%> and %<double%> in "
12421 "declaration specifiers"));
12422 else if (specs
->short_p
)
12424 ("both %<short%> and %<double%> in "
12425 "declaration specifiers"));
12426 else if (specs
->signed_p
)
12428 ("both %<signed%> and %<double%> in "
12429 "declaration specifiers"));
12430 else if (specs
->unsigned_p
)
12432 ("both %<unsigned%> and %<double%> in "
12433 "declaration specifiers"));
12434 else if (specs
->saturating_p
)
12436 ("both %<_Sat%> and %<double%> in "
12437 "declaration specifiers"));
12440 specs
->typespec_word
= cts_double
;
12441 specs
->locations
[cdw_typespec
] = loc
;
12444 CASE_RID_FLOATN_NX
:
12445 specs
->u
.floatn_nx_idx
= i
- RID_FLOATN_NX_FIRST
;
12446 if (!in_system_header_at (input_location
))
12447 pedwarn_c11 (loc
, OPT_Wpedantic
,
12448 "ISO C does not support the %<_Float%d%s%> type"
12450 floatn_nx_types
[specs
->u
.floatn_nx_idx
].n
,
12451 (floatn_nx_types
[specs
->u
.floatn_nx_idx
].extended
12457 ("both %<long%> and %<_Float%d%s%> in "
12458 "declaration specifiers"),
12459 floatn_nx_types
[specs
->u
.floatn_nx_idx
].n
,
12460 (floatn_nx_types
[specs
->u
.floatn_nx_idx
].extended
12463 else if (specs
->short_p
)
12465 ("both %<short%> and %<_Float%d%s%> in "
12466 "declaration specifiers"),
12467 floatn_nx_types
[specs
->u
.floatn_nx_idx
].n
,
12468 (floatn_nx_types
[specs
->u
.floatn_nx_idx
].extended
12471 else if (specs
->signed_p
)
12473 ("both %<signed%> and %<_Float%d%s%> in "
12474 "declaration specifiers"),
12475 floatn_nx_types
[specs
->u
.floatn_nx_idx
].n
,
12476 (floatn_nx_types
[specs
->u
.floatn_nx_idx
].extended
12479 else if (specs
->unsigned_p
)
12481 ("both %<unsigned%> and %<_Float%d%s%> in "
12482 "declaration specifiers"),
12483 floatn_nx_types
[specs
->u
.floatn_nx_idx
].n
,
12484 (floatn_nx_types
[specs
->u
.floatn_nx_idx
].extended
12487 else if (specs
->saturating_p
)
12489 ("both %<_Sat%> and %<_Float%d%s%> in "
12490 "declaration specifiers"),
12491 floatn_nx_types
[specs
->u
.floatn_nx_idx
].n
,
12492 (floatn_nx_types
[specs
->u
.floatn_nx_idx
].extended
12495 else if (FLOATN_NX_TYPE_NODE (specs
->u
.floatn_nx_idx
) == NULL_TREE
)
12497 specs
->typespec_word
= cts_floatn_nx
;
12499 "%<_Float%d%s%> is not supported on this target",
12500 floatn_nx_types
[specs
->u
.floatn_nx_idx
].n
,
12501 (floatn_nx_types
[specs
->u
.floatn_nx_idx
].extended
12507 specs
->typespec_word
= cts_floatn_nx
;
12508 specs
->locations
[cdw_typespec
] = loc
;
12513 case RID_DFLOAT128
:
12516 if (i
== RID_DFLOAT32
)
12517 str
= "_Decimal32";
12518 else if (i
== RID_DFLOAT64
)
12519 str
= "_Decimal64";
12521 str
= "_Decimal128";
12522 if (specs
->long_long_p
)
12524 ("both %<long long%> and %qs in "
12525 "declaration specifiers"),
12529 ("both %<long%> and %qs in "
12530 "declaration specifiers"),
12532 else if (specs
->short_p
)
12534 ("both %<short%> and %qs in "
12535 "declaration specifiers"),
12537 else if (specs
->signed_p
)
12539 ("both %<signed%> and %qs in "
12540 "declaration specifiers"),
12542 else if (specs
->unsigned_p
)
12544 ("both %<unsigned%> and %qs in "
12545 "declaration specifiers"),
12547 else if (specs
->complex_p
)
12549 ("both %<complex%> and %qs in "
12550 "declaration specifiers"),
12552 else if (specs
->saturating_p
)
12554 ("both %<_Sat%> and %qs in "
12555 "declaration specifiers"),
12557 else if (i
== RID_DFLOAT32
)
12558 specs
->typespec_word
= cts_dfloat32
;
12559 else if (i
== RID_DFLOAT64
)
12560 specs
->typespec_word
= cts_dfloat64
;
12562 specs
->typespec_word
= cts_dfloat128
;
12563 specs
->locations
[cdw_typespec
] = loc
;
12565 if (!targetm
.decimal_float_supported_p ())
12567 ("decimal floating-point not supported "
12568 "for this target"));
12569 pedwarn_c11 (loc
, OPT_Wpedantic
,
12570 "ISO C does not support decimal floating-point "
12577 if (i
== RID_FRACT
)
12581 if (specs
->complex_p
)
12583 ("both %<complex%> and %qs in "
12584 "declaration specifiers"),
12586 else if (i
== RID_FRACT
)
12587 specs
->typespec_word
= cts_fract
;
12589 specs
->typespec_word
= cts_accum
;
12590 specs
->locations
[cdw_typespec
] = loc
;
12592 if (!targetm
.fixed_point_supported_p ())
12594 "fixed-point types not supported for this target");
12595 pedwarn (loc
, OPT_Wpedantic
,
12596 "ISO C does not support fixed-point types");
12601 ("both %<long%> and %<_BitInt%> in "
12602 "declaration specifiers"));
12603 else if (specs
->short_p
)
12605 ("both %<short%> and %<_BitInt%> in "
12606 "declaration specifiers"));
12607 else if (specs
->complex_p
)
12609 ("both %<complex%> and %<_BitInt%> in "
12610 "declaration specifiers"));
12611 else if (specs
->saturating_p
)
12613 ("both %<_Sat%> and %<_BitInt%> in "
12614 "declaration specifiers"));
12617 specs
->typespec_word
= cts_bitint
;
12618 specs
->locations
[cdw_typespec
] = loc
;
12619 specs
->u
.bitint_prec
= -1;
12620 if (error_operand_p (spec
.expr
))
12622 if (TREE_CODE (spec
.expr
) != INTEGER_CST
12623 || !INTEGRAL_TYPE_P (TREE_TYPE (spec
.expr
)))
12625 error_at (loc
, "%<_BitInt%> argument is not an integer "
12626 "constant expression");
12629 if (tree_int_cst_sgn (spec
.expr
) <= 0)
12631 error_at (loc
, "%<_BitInt%> argument %qE is not a "
12632 "positive integer constant expression",
12636 if (wi::to_widest (spec
.expr
) > WIDE_INT_MAX_PRECISION
- 1)
12638 error_at (loc
, "%<_BitInt%> argument %qE is larger than "
12639 "%<BITINT_MAXWIDTH%> %qd",
12640 spec
.expr
, (int) WIDE_INT_MAX_PRECISION
- 1);
12643 specs
->u
.bitint_prec
= tree_to_uhwi (spec
.expr
);
12644 struct bitint_info info
;
12645 if (!targetm
.c
.bitint_type_info (specs
->u
.bitint_prec
,
12648 sorry_at (loc
, "%<_BitInt(%d)%> is not supported on "
12649 "this target", specs
->u
.bitint_prec
);
12650 specs
->u
.bitint_prec
= -1;
12656 /* ObjC reserved word "id", handled below. */
12662 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
12663 form of ObjC type, cases such as "int" and "long" being handled
12664 above), a TYPE (struct, union, enum and typeof specifiers) or an
12665 ERROR_MARK. In none of these cases may there have previously
12666 been any type specifiers. */
12667 if (specs
->type
|| specs
->typespec_word
!= cts_none
12668 || specs
->long_p
|| specs
->short_p
|| specs
->signed_p
12669 || specs
->unsigned_p
|| specs
->complex_p
)
12670 error_at (loc
, "two or more data types in declaration specifiers");
12671 else if (TREE_CODE (type
) == TYPE_DECL
)
12673 specs
->type
= TREE_TYPE (type
);
12674 if (TREE_TYPE (type
) != error_mark_node
)
12676 specs
->decl_attr
= DECL_ATTRIBUTES (type
);
12677 specs
->typedef_p
= true;
12678 specs
->explicit_signed_p
= C_TYPEDEF_EXPLICITLY_SIGNED (type
);
12679 specs
->locations
[cdw_typedef
] = loc
;
12681 /* If this typedef name is defined in a struct, then a C++
12682 lookup would return a different value. */
12683 if (warn_cxx_compat
12684 && I_SYMBOL_BINDING (DECL_NAME (type
))->in_struct
)
12685 warning_at (loc
, OPT_Wc___compat
,
12686 "C++ lookup of %qD would return a field, not a type",
12689 /* If we are parsing a struct, record that a struct field
12691 if (warn_cxx_compat
&& struct_parse_info
!= NULL
)
12692 struct_parse_info
->typedefs_seen
.safe_push (type
);
12695 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
12697 tree t
= lookup_name (type
);
12698 if (!t
|| TREE_CODE (t
) != TYPE_DECL
)
12699 error_at (loc
, "%qE fails to be a typedef or built in type", type
);
12700 else if (TREE_TYPE (t
) == error_mark_node
)
12704 specs
->type
= TREE_TYPE (t
);
12705 specs
->locations
[cdw_typespec
] = loc
;
12710 if (TREE_CODE (type
) != ERROR_MARK
)
12712 if (spec
.kind
== ctsk_typeof
)
12714 specs
->typedef_p
= true;
12715 specs
->locations
[cdw_typedef
] = loc
;
12720 specs
->expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (spec
.expr
),
12721 specs
->expr
, spec
.expr
);
12723 specs
->expr
= spec
.expr
;
12724 specs
->expr_const_operands
&= spec
.expr_const_operands
;
12727 specs
->type
= type
;
12728 if (spec
.has_enum_type_specifier
12729 && spec
.kind
!= ctsk_tagdef
)
12730 specs
->enum_type_specifier_ref_p
= true;
12736 /* Add the storage class specifier or function specifier SCSPEC to the
12737 declaration specifiers SPECS, returning SPECS. */
12739 struct c_declspecs
*
12740 declspecs_add_scspec (location_t loc
,
12741 struct c_declspecs
*specs
,
12745 enum c_storage_class n
= csc_none
;
12747 specs
->declspecs_seen_p
= true;
12748 specs
->non_std_attrs_seen_p
= true;
12749 gcc_assert (TREE_CODE (scspec
) == IDENTIFIER_NODE
12750 && C_IS_RESERVED_WORD (scspec
));
12751 i
= C_RID_CODE (scspec
);
12752 if (specs
->non_sc_seen_p
)
12753 warning (OPT_Wold_style_declaration
,
12754 "%qE is not at beginning of declaration", scspec
);
12758 /* C99 permits duplicate inline. Although of doubtful utility,
12759 it seems simplest to permit it in gnu89 mode as well, as
12760 there is also little utility in maintaining this as a
12761 difference between gnu89 and C99 inline. */
12763 specs
->inline_p
= true;
12764 specs
->locations
[cdw_inline
] = loc
;
12767 /* Duplicate _Noreturn is permitted. */
12769 specs
->noreturn_p
= true;
12770 specs
->locations
[cdw_noreturn
] = loc
;
12773 dupe
= specs
->thread_p
;
12774 if (specs
->storage_class
== csc_auto
)
12775 error ("%qE used with %<auto%>", scspec
);
12776 else if (specs
->storage_class
== csc_register
)
12777 error ("%qE used with %<register%>", scspec
);
12778 else if (specs
->storage_class
== csc_typedef
)
12779 error ("%qE used with %<typedef%>", scspec
);
12780 else if (specs
->constexpr_p
)
12781 error ("%qE used with %<constexpr%>", scspec
);
12784 specs
->thread_p
= true;
12785 specs
->thread_gnu_p
= (strcmp (IDENTIFIER_POINTER (scspec
),
12787 /* A diagnostic is not required for the use of this
12788 identifier in the implementation namespace; only diagnose
12789 it for the C11 spelling because of existing code using
12790 the other spelling. */
12791 if (!specs
->thread_gnu_p
)
12794 pedwarn_c99 (loc
, OPT_Wpedantic
,
12795 "ISO C99 does not support %qE", scspec
);
12797 pedwarn_c99 (loc
, OPT_Wpedantic
,
12798 "ISO C90 does not support %qE", scspec
);
12800 specs
->locations
[cdw_thread
] = loc
;
12805 && specs
->typespec_kind
== ctsk_none
12806 && specs
->storage_class
!= csc_typedef
)
12808 /* "auto" potentially used for type deduction. */
12809 if (specs
->c23_auto_p
)
12810 error ("duplicate %qE", scspec
);
12811 specs
->c23_auto_p
= true;
12815 /* auto may only be used with another storage class specifier,
12816 such as constexpr, if the type is inferred. */
12817 if (specs
->constexpr_p
)
12818 error ("%qE used with %<constexpr%>", scspec
);
12822 /* Diagnose "__thread extern". */
12823 if (specs
->thread_p
&& specs
->thread_gnu_p
)
12824 error ("%<__thread%> before %<extern%>");
12831 /* Diagnose "__thread static". */
12832 if (specs
->thread_p
&& specs
->thread_gnu_p
)
12833 error ("%<__thread%> before %<static%>");
12837 if (specs
->c23_auto_p
)
12839 error ("%<typedef%> used with %<auto%>");
12840 specs
->c23_auto_p
= false;
12843 case RID_CONSTEXPR
:
12844 dupe
= specs
->constexpr_p
;
12845 if (specs
->storage_class
== csc_extern
)
12846 error ("%qE used with %<extern%>", scspec
);
12847 else if (specs
->storage_class
== csc_typedef
)
12848 error ("%qE used with %<typedef%>", scspec
);
12849 else if (specs
->storage_class
== csc_auto
)
12850 /* auto may only be used with another storage class specifier,
12851 such as constexpr, if the type is inferred. */
12852 error ("%qE used with %<auto%>", scspec
);
12853 else if (specs
->thread_p
)
12854 error ("%qE used with %qs", scspec
,
12855 specs
->thread_gnu_p
? "__thread" : "_Thread_local");
12857 specs
->constexpr_p
= true;
12860 gcc_unreachable ();
12862 if (n
!= csc_none
&& n
== specs
->storage_class
)
12866 if (i
== RID_THREAD
)
12867 error ("duplicate %<_Thread_local%> or %<__thread%>");
12869 error ("duplicate %qE", scspec
);
12873 if (specs
->storage_class
!= csc_none
&& n
!= specs
->storage_class
)
12875 error ("multiple storage classes in declaration specifiers");
12879 specs
->storage_class
= n
;
12880 specs
->locations
[cdw_storage_class
] = loc
;
12881 if (n
!= csc_extern
&& n
!= csc_static
&& specs
->thread_p
)
12883 error ("%qs used with %qE",
12884 specs
->thread_gnu_p
? "__thread" : "_Thread_local",
12886 specs
->thread_p
= false;
12888 if (n
!= csc_auto
&& n
!= csc_register
&& n
!= csc_static
12889 && specs
->constexpr_p
)
12891 error ("%<constexpr%> used with %qE", scspec
);
12892 specs
->constexpr_p
= false;
12899 /* Add the attributes ATTRS to the declaration specifiers SPECS,
12900 returning SPECS. */
12902 struct c_declspecs
*
12903 declspecs_add_attrs (location_t loc
, struct c_declspecs
*specs
, tree attrs
)
12905 specs
->attrs
= chainon (attrs
, specs
->attrs
);
12906 specs
->locations
[cdw_attributes
] = loc
;
12907 specs
->declspecs_seen_p
= true;
12908 /* In the case of standard attributes at the start of the
12909 declaration, the caller will reset this. */
12910 specs
->non_std_attrs_seen_p
= true;
12914 /* Add an _Alignas specifier (expression ALIGN, or type whose
12915 alignment is ALIGN) to the declaration specifiers SPECS, returning
12917 struct c_declspecs
*
12918 declspecs_add_alignas (location_t loc
,
12919 struct c_declspecs
*specs
, tree align
)
12921 specs
->alignas_p
= true;
12922 specs
->locations
[cdw_alignas
] = loc
;
12923 if (align
== error_mark_node
)
12926 /* Only accept the alignment if it's valid and greater than
12927 the current one. Zero is invalid but by C11 required to
12928 be silently ignored. */
12929 int align_log
= check_user_alignment (align
, false, /* warn_zero = */false);
12930 if (align_log
> specs
->align_log
)
12931 specs
->align_log
= align_log
;
12935 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
12936 specifiers with any other type specifier to determine the resulting
12937 type. This is where ISO C checks on complex types are made, since
12938 "_Complex long" is a prefix of the valid ISO C type "_Complex long
12939 double". Also apply postfix standard attributes to modify the type. */
12941 struct c_declspecs
*
12942 finish_declspecs (struct c_declspecs
*specs
)
12944 /* If a type was specified as a whole, we have no modifiers and are
12946 if (specs
->type
!= NULL_TREE
)
12948 gcc_assert (!specs
->long_p
&& !specs
->long_long_p
&& !specs
->short_p
12949 && !specs
->signed_p
&& !specs
->unsigned_p
12950 && !specs
->complex_p
&& !specs
->c23_auto_p
);
12952 /* Set a dummy type. */
12953 if (TREE_CODE (specs
->type
) == ERROR_MARK
)
12954 specs
->type
= integer_type_node
;
12955 goto handle_postfix_attrs
;
12958 /* If none of "void", "_Bool", "char", "int", "float" or "double"
12959 has been specified, treat it as "int" unless "_Complex" is
12960 present and there are no other specifiers. If we just have
12961 "_Complex", it is equivalent to "_Complex double", but e.g.
12962 "_Complex short" is equivalent to "_Complex short int". */
12963 if (specs
->typespec_word
== cts_none
)
12965 if (specs
->saturating_p
)
12967 error_at (specs
->locations
[cdw_saturating
],
12968 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
12969 if (!targetm
.fixed_point_supported_p ())
12970 error_at (specs
->locations
[cdw_saturating
],
12971 "fixed-point types not supported for this target");
12972 specs
->typespec_word
= cts_fract
;
12974 else if (specs
->long_p
|| specs
->short_p
12975 || specs
->signed_p
|| specs
->unsigned_p
)
12977 specs
->typespec_word
= cts_int
;
12979 else if (specs
->complex_p
)
12981 specs
->typespec_word
= cts_double
;
12982 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
12983 "ISO C does not support plain %<complex%> meaning "
12984 "%<double complex%>");
12986 else if (specs
->c23_auto_p
)
12988 /* Type to be filled in later, including applying postfix
12989 attributes. This warning only actually appears for
12990 -Wc11-c23-compat in C23 mode; in older modes, there may
12991 be a warning or pedwarn for implicit "int" instead, or
12992 other errors for use of auto at file scope. */
12993 pedwarn_c11 (input_location
, OPT_Wpedantic
,
12994 "ISO C does not support %<auto%> type deduction "
13000 specs
->typespec_word
= cts_int
;
13001 specs
->default_int_p
= true;
13002 /* We don't diagnose this here because grokdeclarator will
13003 give more specific diagnostics according to whether it is
13004 a function definition. */
13008 /* If "signed" was specified, record this to distinguish "int" and
13009 "signed int" in the case of a bit-field with
13010 -funsigned-bitfields. */
13011 specs
->explicit_signed_p
= specs
->signed_p
;
13013 /* Now compute the actual type. */
13014 gcc_assert (!specs
->c23_auto_p
);
13015 switch (specs
->typespec_word
)
13017 case cts_auto_type
:
13018 gcc_assert (!specs
->long_p
&& !specs
->short_p
13019 && !specs
->signed_p
&& !specs
->unsigned_p
13020 && !specs
->complex_p
);
13021 /* Type to be filled in later. */
13022 if (specs
->postfix_attrs
)
13023 error ("%<__auto_type%> followed by %<[[]]%> attributes");
13026 gcc_assert (!specs
->long_p
&& !specs
->short_p
13027 && !specs
->signed_p
&& !specs
->unsigned_p
13028 && !specs
->complex_p
);
13029 specs
->type
= void_type_node
;
13032 gcc_assert (!specs
->long_p
&& !specs
->short_p
13033 && !specs
->signed_p
&& !specs
->unsigned_p
13034 && !specs
->complex_p
);
13035 specs
->type
= boolean_type_node
;
13038 gcc_assert (!specs
->long_p
&& !specs
->short_p
);
13039 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
13040 if (specs
->signed_p
)
13041 specs
->type
= signed_char_type_node
;
13042 else if (specs
->unsigned_p
)
13043 specs
->type
= unsigned_char_type_node
;
13045 specs
->type
= char_type_node
;
13046 if (specs
->complex_p
)
13048 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
13049 "ISO C does not support complex integer types");
13050 specs
->type
= build_complex_type (specs
->type
);
13054 gcc_assert (!specs
->long_p
&& !specs
->short_p
&& !specs
->long_long_p
);
13055 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
13056 if (! int_n_enabled_p
[specs
->u
.int_n_idx
])
13057 specs
->type
= integer_type_node
;
13059 specs
->type
= (specs
->unsigned_p
13060 ? int_n_trees
[specs
->u
.int_n_idx
].unsigned_type
13061 : int_n_trees
[specs
->u
.int_n_idx
].signed_type
);
13062 if (specs
->complex_p
)
13064 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
13065 "ISO C does not support complex integer types");
13066 specs
->type
= build_complex_type (specs
->type
);
13070 gcc_assert (!(specs
->long_p
&& specs
->short_p
));
13071 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
13072 if (specs
->long_long_p
)
13073 specs
->type
= (specs
->unsigned_p
13074 ? long_long_unsigned_type_node
13075 : long_long_integer_type_node
);
13076 else if (specs
->long_p
)
13077 specs
->type
= (specs
->unsigned_p
13078 ? long_unsigned_type_node
13079 : long_integer_type_node
);
13080 else if (specs
->short_p
)
13081 specs
->type
= (specs
->unsigned_p
13082 ? short_unsigned_type_node
13083 : short_integer_type_node
);
13085 specs
->type
= (specs
->unsigned_p
13086 ? unsigned_type_node
13087 : integer_type_node
);
13088 if (specs
->complex_p
)
13090 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
13091 "ISO C does not support complex integer types");
13092 specs
->type
= build_complex_type (specs
->type
);
13096 gcc_assert (!specs
->long_p
&& !specs
->short_p
13097 && !specs
->signed_p
&& !specs
->unsigned_p
);
13098 specs
->type
= (specs
->complex_p
13099 ? complex_float_type_node
13100 : float_type_node
);
13103 gcc_assert (!specs
->long_long_p
&& !specs
->short_p
13104 && !specs
->signed_p
&& !specs
->unsigned_p
);
13107 specs
->type
= (specs
->complex_p
13108 ? complex_long_double_type_node
13109 : long_double_type_node
);
13113 specs
->type
= (specs
->complex_p
13114 ? complex_double_type_node
13115 : double_type_node
);
13118 case cts_floatn_nx
:
13119 gcc_assert (!specs
->long_p
&& !specs
->short_p
13120 && !specs
->signed_p
&& !specs
->unsigned_p
);
13121 if (FLOATN_NX_TYPE_NODE (specs
->u
.floatn_nx_idx
) == NULL_TREE
)
13122 specs
->type
= integer_type_node
;
13123 else if (specs
->complex_p
)
13124 specs
->type
= COMPLEX_FLOATN_NX_TYPE_NODE (specs
->u
.floatn_nx_idx
);
13126 specs
->type
= FLOATN_NX_TYPE_NODE (specs
->u
.floatn_nx_idx
);
13130 case cts_dfloat128
:
13131 gcc_assert (!specs
->long_p
&& !specs
->long_long_p
&& !specs
->short_p
13132 && !specs
->signed_p
&& !specs
->unsigned_p
&& !specs
->complex_p
);
13133 if (!targetm
.decimal_float_supported_p ())
13134 specs
->type
= integer_type_node
;
13135 else if (specs
->typespec_word
== cts_dfloat32
)
13136 specs
->type
= dfloat32_type_node
;
13137 else if (specs
->typespec_word
== cts_dfloat64
)
13138 specs
->type
= dfloat64_type_node
;
13140 specs
->type
= dfloat128_type_node
;
13143 gcc_assert (!specs
->complex_p
);
13144 if (!targetm
.fixed_point_supported_p ())
13145 specs
->type
= integer_type_node
;
13146 else if (specs
->saturating_p
)
13148 if (specs
->long_long_p
)
13149 specs
->type
= specs
->unsigned_p
13150 ? sat_unsigned_long_long_fract_type_node
13151 : sat_long_long_fract_type_node
;
13152 else if (specs
->long_p
)
13153 specs
->type
= specs
->unsigned_p
13154 ? sat_unsigned_long_fract_type_node
13155 : sat_long_fract_type_node
;
13156 else if (specs
->short_p
)
13157 specs
->type
= specs
->unsigned_p
13158 ? sat_unsigned_short_fract_type_node
13159 : sat_short_fract_type_node
;
13161 specs
->type
= specs
->unsigned_p
13162 ? sat_unsigned_fract_type_node
13163 : sat_fract_type_node
;
13167 if (specs
->long_long_p
)
13168 specs
->type
= specs
->unsigned_p
13169 ? unsigned_long_long_fract_type_node
13170 : long_long_fract_type_node
;
13171 else if (specs
->long_p
)
13172 specs
->type
= specs
->unsigned_p
13173 ? unsigned_long_fract_type_node
13174 : long_fract_type_node
;
13175 else if (specs
->short_p
)
13176 specs
->type
= specs
->unsigned_p
13177 ? unsigned_short_fract_type_node
13178 : short_fract_type_node
;
13180 specs
->type
= specs
->unsigned_p
13181 ? unsigned_fract_type_node
13186 gcc_assert (!specs
->complex_p
);
13187 if (!targetm
.fixed_point_supported_p ())
13188 specs
->type
= integer_type_node
;
13189 else if (specs
->saturating_p
)
13191 if (specs
->long_long_p
)
13192 specs
->type
= specs
->unsigned_p
13193 ? sat_unsigned_long_long_accum_type_node
13194 : sat_long_long_accum_type_node
;
13195 else if (specs
->long_p
)
13196 specs
->type
= specs
->unsigned_p
13197 ? sat_unsigned_long_accum_type_node
13198 : sat_long_accum_type_node
;
13199 else if (specs
->short_p
)
13200 specs
->type
= specs
->unsigned_p
13201 ? sat_unsigned_short_accum_type_node
13202 : sat_short_accum_type_node
;
13204 specs
->type
= specs
->unsigned_p
13205 ? sat_unsigned_accum_type_node
13206 : sat_accum_type_node
;
13210 if (specs
->long_long_p
)
13211 specs
->type
= specs
->unsigned_p
13212 ? unsigned_long_long_accum_type_node
13213 : long_long_accum_type_node
;
13214 else if (specs
->long_p
)
13215 specs
->type
= specs
->unsigned_p
13216 ? unsigned_long_accum_type_node
13217 : long_accum_type_node
;
13218 else if (specs
->short_p
)
13219 specs
->type
= specs
->unsigned_p
13220 ? unsigned_short_accum_type_node
13221 : short_accum_type_node
;
13223 specs
->type
= specs
->unsigned_p
13224 ? unsigned_accum_type_node
13229 gcc_assert (!specs
->long_p
&& !specs
->short_p
13230 && !specs
->complex_p
);
13231 if (!specs
->unsigned_p
&& specs
->u
.bitint_prec
== 1)
13233 error_at (specs
->locations
[cdw_typespec
],
13234 "%<signed _BitInt%> argument must be at least 2");
13235 specs
->type
= integer_type_node
;
13238 if (specs
->u
.bitint_prec
== -1)
13239 specs
->type
= integer_type_node
;
13242 pedwarn_c11 (specs
->locations
[cdw_typespec
], OPT_Wpedantic
,
13243 "ISO C does not support %<%s_BitInt(%d)%> before C23",
13244 specs
->unsigned_p
? "unsigned "
13245 : specs
->signed_p
? "signed " : "",
13246 specs
->u
.bitint_prec
);
13247 specs
->type
= build_bitint_type (specs
->u
.bitint_prec
,
13248 specs
->unsigned_p
);
13252 gcc_unreachable ();
13254 handle_postfix_attrs
:
13255 if (specs
->type
!= NULL
)
13257 specs
->postfix_attrs
= c_warn_type_attributes (specs
->postfix_attrs
);
13258 decl_attributes (&specs
->type
, specs
->postfix_attrs
, 0);
13259 specs
->postfix_attrs
= NULL_TREE
;
13265 /* Perform final processing on one file scope's declarations (or the
13266 external scope's declarations), GLOBALS. */
13269 c_write_global_declarations_1 (tree globals
)
13274 /* Process the decls in the order they were written. */
13275 for (decl
= globals
; decl
; decl
= DECL_CHAIN (decl
))
13277 /* Check for used but undefined static functions using the C
13278 standard's definition of "used", and set TREE_NO_WARNING so
13279 that check_global_declaration doesn't repeat the check. */
13280 if (TREE_CODE (decl
) == FUNCTION_DECL
13281 && DECL_INITIAL (decl
) == NULL_TREE
13282 && DECL_EXTERNAL (decl
)
13283 && !TREE_PUBLIC (decl
))
13285 if (C_DECL_USED (decl
))
13287 /* TODO: Add OPT_Wundefined-inline. */
13288 if (pedwarn (input_location
, 0, "%q+F used but never defined",
13290 suppress_warning (decl
/* OPT_Wundefined-inline. */);
13292 /* For -Wunused-function warn about unused static prototypes. */
13293 else if (warn_unused_function
13294 && ! DECL_ARTIFICIAL (decl
)
13295 && ! warning_suppressed_p (decl
, OPT_Wunused_function
))
13297 if (warning (OPT_Wunused_function
,
13298 "%q+F declared %<static%> but never defined",
13300 suppress_warning (decl
, OPT_Wunused_function
);
13304 wrapup_global_declaration_1 (decl
);
13309 reconsider
= false;
13310 for (decl
= globals
; decl
; decl
= DECL_CHAIN (decl
))
13311 reconsider
|= wrapup_global_declaration_2 (decl
);
13313 while (reconsider
);
13316 /* Preserve the external declarations scope across a garbage collect. */
13317 static GTY(()) tree ext_block
;
13319 /* Collect all references relevant to SOURCE_FILE. */
13322 collect_all_refs (const char *source_file
)
13327 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
13328 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t
)), source_file
);
13330 collect_ada_nodes (BLOCK_VARS (ext_block
), source_file
);
13333 /* Collect source file references at global level. */
13336 collect_source_refs (void)
13343 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
13345 decls
= DECL_INITIAL (t
);
13346 for (decl
= BLOCK_VARS (decls
); decl
; decl
= TREE_CHAIN (decl
))
13347 if (!DECL_IS_UNDECLARED_BUILTIN (decl
))
13348 collect_source_ref (DECL_SOURCE_FILE (decl
));
13351 for (decl
= BLOCK_VARS (ext_block
); decl
; decl
= TREE_CHAIN (decl
))
13352 if (!DECL_IS_UNDECLARED_BUILTIN (decl
))
13353 collect_source_ref (DECL_SOURCE_FILE (decl
));
13356 /* Free attribute access data that are not needed by the middle end. */
13359 free_attr_access_data ()
13361 struct cgraph_node
*n
;
13363 /* Iterate over all functions declared in the translation unit. */
13364 FOR_EACH_FUNCTION (n
)
13366 for (tree parm
= DECL_ARGUMENTS (n
->decl
); parm
; parm
= TREE_CHAIN (parm
))
13367 if (tree attrs
= DECL_ATTRIBUTES (parm
))
13368 attr_access::free_lang_data (attrs
);
13370 tree fntype
= TREE_TYPE (n
->decl
);
13371 if (!fntype
|| fntype
== error_mark_node
)
13373 tree attrs
= TYPE_ATTRIBUTES (fntype
);
13377 attr_access::free_lang_data (attrs
);
13381 /* Perform any final parser cleanups and generate initial debugging
13385 c_parse_final_cleanups (void)
13390 /* We don't want to do this if generating a PCH. */
13394 timevar_stop (TV_PHASE_PARSING
);
13395 timevar_start (TV_PHASE_DEFERRED
);
13397 /* Do the Objective-C stuff. This is where all the Objective-C
13398 module stuff gets generated (symtab, class/protocol/selector
13400 if (c_dialect_objc ())
13401 objc_write_global_declarations ();
13403 /* Close the external scope. */
13404 ext_block
= pop_scope ();
13405 external_scope
= 0;
13406 gcc_assert (!current_scope
);
13408 /* Handle -fdump-ada-spec[-slim]. */
13409 if (flag_dump_ada_spec
|| flag_dump_ada_spec_slim
)
13411 /* Build a table of files to generate specs for */
13412 collect_source_ref (main_input_filename
);
13413 if (!flag_dump_ada_spec_slim
)
13414 collect_source_refs ();
13416 dump_ada_specs (collect_all_refs
, NULL
);
13419 /* Process all file scopes in this compilation, and the external_scope,
13420 through wrapup_global_declarations. */
13421 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
13422 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t
)));
13423 c_write_global_declarations_1 (BLOCK_VARS (ext_block
));
13426 free_attr_access_data ();
13428 timevar_stop (TV_PHASE_DEFERRED
);
13429 timevar_start (TV_PHASE_PARSING
);
13434 /* Register reserved keyword WORD as qualifier for address space AS. */
13437 c_register_addr_space (const char *word
, addr_space_t as
)
13439 int rid
= RID_FIRST_ADDR_SPACE
+ as
;
13442 /* Address space qualifiers are only supported
13443 in C with GNU extensions enabled. */
13444 if (c_dialect_objc () || flag_no_asm
)
13447 id
= get_identifier (word
);
13448 C_SET_RID_CODE (id
, rid
);
13449 C_IS_RESERVED_WORD (id
) = 1;
13450 ridpointers
[rid
] = id
;
13453 /* Return identifier to look up for omp declare reduction. */
13456 c_omp_reduction_id (enum tree_code reduction_code
, tree reduction_id
)
13458 const char *p
= NULL
;
13459 switch (reduction_code
)
13461 case PLUS_EXPR
: p
= "+"; break;
13462 case MULT_EXPR
: p
= "*"; break;
13463 case MINUS_EXPR
: p
= "-"; break;
13464 case BIT_AND_EXPR
: p
= "&"; break;
13465 case BIT_XOR_EXPR
: p
= "^"; break;
13466 case BIT_IOR_EXPR
: p
= "|"; break;
13467 case TRUTH_ANDIF_EXPR
: p
= "&&"; break;
13468 case TRUTH_ORIF_EXPR
: p
= "||"; break;
13469 case MIN_EXPR
: p
= "min"; break;
13470 case MAX_EXPR
: p
= "max"; break;
13477 if (TREE_CODE (reduction_id
) != IDENTIFIER_NODE
)
13478 return error_mark_node
;
13479 p
= IDENTIFIER_POINTER (reduction_id
);
13482 const char prefix
[] = "omp declare reduction ";
13483 size_t lenp
= sizeof (prefix
);
13484 size_t len
= strlen (p
);
13485 char *name
= XALLOCAVEC (char, lenp
+ len
);
13486 memcpy (name
, prefix
, lenp
- 1);
13487 memcpy (name
+ lenp
- 1, p
, len
+ 1);
13488 return get_identifier (name
);
13491 /* Lookup REDUCTION_ID in the current scope, or create an artificial
13492 VAR_DECL, bind it into the current scope and return it. */
13495 c_omp_reduction_decl (tree reduction_id
)
13497 struct c_binding
*b
= I_SYMBOL_BINDING (reduction_id
);
13498 if (b
!= NULL
&& B_IN_CURRENT_SCOPE (b
))
13501 tree decl
= build_decl (BUILTINS_LOCATION
, VAR_DECL
,
13502 reduction_id
, integer_type_node
);
13503 DECL_ARTIFICIAL (decl
) = 1;
13504 DECL_EXTERNAL (decl
) = 1;
13505 TREE_STATIC (decl
) = 1;
13506 TREE_PUBLIC (decl
) = 0;
13507 bind (reduction_id
, decl
, current_scope
, true, false, BUILTINS_LOCATION
);
13511 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
13514 c_omp_reduction_lookup (tree reduction_id
, tree type
)
13516 struct c_binding
*b
= I_SYMBOL_BINDING (reduction_id
);
13520 for (t
= DECL_INITIAL (b
->decl
); t
; t
= TREE_CHAIN (t
))
13521 if (comptypes (TREE_PURPOSE (t
), type
))
13522 return TREE_VALUE (t
);
13525 return error_mark_node
;
13528 /* Helper function called via walk_tree, to diagnose invalid
13529 #pragma omp declare reduction combiners or initializers. */
13532 c_check_omp_declare_reduction_r (tree
*tp
, int *, void *data
)
13534 tree
*vars
= (tree
*) data
;
13535 if (SSA_VAR_P (*tp
)
13536 && !DECL_ARTIFICIAL (*tp
)
13540 location_t loc
= DECL_SOURCE_LOCATION (vars
[0]);
13541 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars
[0])), "omp_out") == 0)
13542 error_at (loc
, "%<#pragma omp declare reduction%> combiner refers to "
13543 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
13546 error_at (loc
, "%<#pragma omp declare reduction%> initializer refers "
13547 "to variable %qD which is not %<omp_priv%> nor "
13557 c_check_in_current_scope (tree decl
)
13559 struct c_binding
*b
= I_SYMBOL_BINDING (DECL_NAME (decl
));
13560 return b
!= NULL
&& B_IN_CURRENT_SCOPE (b
);
13563 #include "gt-c-c-decl.h"