1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2019 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_UNIQUE_PTR
30 #include "coretypes.h"
35 #include "stringpool.h"
38 #include "print-tree.h"
39 #include "stor-layout.h"
44 #include "c-family/c-objc.h"
45 #include "c-family/c-pragma.h"
46 #include "c-family/c-ubsan.h"
48 #include "langhooks.h"
49 #include "tree-iterator.h"
52 #include "c-family/c-ada-spec.h"
54 #include "spellcheck-tree.h"
55 #include "gcc-rich-location.h"
57 #include "c-family/name-hint.h"
58 #include "c-family/known-headers.h"
59 #include "c-family/c-spellcheck.h"
61 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
63 { NORMAL
, /* Ordinary declaration */
64 FUNCDEF
, /* Function definition */
65 PARM
, /* Declaration of parm before function body */
66 FIELD
, /* Declaration inside struct or union */
67 TYPENAME
}; /* Typename (inside cast or sizeof) */
69 /* States indicating how grokdeclarator() should handle declspecs marked
70 with __attribute__((deprecated)). An object declared as
71 __attribute__((deprecated)) suppresses warnings of uses of other
74 enum deprecated_states
{
80 /* Nonzero if we have seen an invalid cross reference
81 to a struct, union, or enum, but not yet printed the message. */
82 tree pending_invalid_xref
;
84 /* File and line to appear in the eventual error message. */
85 location_t pending_invalid_xref_location
;
87 /* The file and line that the prototype came from if this is an
88 old-style definition; used for diagnostics in
89 store_parm_decls_oldstyle. */
91 static location_t current_function_prototype_locus
;
93 /* Whether this prototype was built-in. */
95 static bool current_function_prototype_built_in
;
97 /* The argument type information of this prototype. */
99 static tree current_function_prototype_arg_types
;
101 /* The argument information structure for the function currently being
104 static struct c_arg_info
*current_function_arg_info
;
106 /* The obstack on which parser and related data structures, which are
107 not live beyond their top-level declaration or definition, are
109 struct obstack parser_obstack
;
111 /* The current statement tree. */
113 static GTY(()) struct stmt_tree_s c_stmt_tree
;
115 /* State saving variables. */
119 /* A list of decls to be made automatically visible in each file scope. */
120 static GTY(()) tree visible_builtins
;
122 /* Set to 0 at beginning of a function definition, set to 1 if
123 a return statement that specifies a return value is seen. */
125 int current_function_returns_value
;
127 /* Set to 0 at beginning of a function definition, set to 1 if
128 a return statement with no argument is seen. */
130 int current_function_returns_null
;
132 /* Set to 0 at beginning of a function definition, set to 1 if
133 a call to a noreturn function is seen. */
135 int current_function_returns_abnormally
;
137 /* Set to nonzero by `grokdeclarator' for a function
138 whose return type is defaulted, if warnings for this are desired. */
140 static int warn_about_return_type
;
142 /* Nonzero when the current toplevel function contains a declaration
143 of a nested function which is never defined. */
145 static bool undef_nested_function
;
147 /* If non-zero, implicit "omp declare target" attribute is added into the
149 int current_omp_declare_target_attribute
;
151 /* Each c_binding structure describes one binding of an identifier to
152 a decl. All the decls in a scope - irrespective of namespace - are
153 chained together by the ->prev field, which (as the name implies)
154 runs in reverse order. All the decls in a given namespace bound to
155 a given identifier are chained by the ->shadowed field, which runs
156 from inner to outer scopes.
158 The ->decl field usually points to a DECL node, but there are two
159 exceptions. In the namespace of type tags, the bound entity is a
160 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
161 identifier is encountered, it is bound to error_mark_node to
162 suppress further errors about that identifier in the current
165 The ->u.type field stores the type of the declaration in this scope;
166 if NULL, the type is the type of the ->decl field. This is only of
167 relevance for objects with external or internal linkage which may
168 be redeclared in inner scopes, forming composite types that only
169 persist for the duration of those scopes. In the external scope,
170 this stores the composite of all the types declared for this
171 object, visible or not. The ->inner_comp field (used only at file
172 scope) stores whether an incomplete array type at file scope was
173 completed at an inner scope to an array size other than 1.
175 The ->u.label field is used for labels. It points to a structure
176 which stores additional information used for warnings.
178 The depth field is copied from the scope structure that holds this
179 decl. It is used to preserve the proper ordering of the ->shadowed
180 field (see bind()) and also for a handful of special-case checks.
181 Finally, the invisible bit is true for a decl which should be
182 ignored for purposes of normal name lookup, and the nested bit is
183 true for a decl that's been bound a second time in an inner scope;
184 in all such cases, the binding in the outer scope will have its
185 invisible bit true. */
187 struct GTY((chain_next ("%h.prev"))) c_binding
{
188 union GTY(()) { /* first so GTY desc can use decl */
189 tree
GTY((tag ("0"))) type
; /* the type in this scope */
190 struct c_label_vars
* GTY((tag ("1"))) label
; /* for warnings */
191 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u
;
192 tree decl
; /* the decl bound */
193 tree id
; /* the identifier it's bound to */
194 struct c_binding
*prev
; /* the previous decl in this scope */
195 struct c_binding
*shadowed
; /* the innermost decl shadowed by this one */
196 unsigned int depth
: 28; /* depth of this scope */
197 BOOL_BITFIELD invisible
: 1; /* normal lookup should ignore this binding */
198 BOOL_BITFIELD nested
: 1; /* do not set DECL_CONTEXT when popping */
199 BOOL_BITFIELD inner_comp
: 1; /* incomplete array completed in inner scope */
200 BOOL_BITFIELD in_struct
: 1; /* currently defined as struct field */
201 location_t locus
; /* location for nested bindings */
203 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
204 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
205 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
206 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
208 /* Each C symbol points to three linked lists of c_binding structures.
209 These describe the values of the identifier in the three different
210 namespaces defined by the language. */
212 struct GTY(()) lang_identifier
{
213 struct c_common_identifier common_id
;
214 struct c_binding
*symbol_binding
; /* vars, funcs, constants, typedefs */
215 struct c_binding
*tag_binding
; /* struct/union/enum tags */
216 struct c_binding
*label_binding
; /* labels */
219 /* Validate c-lang.c's assumptions. */
220 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
221 [(sizeof(struct lang_identifier
) == C_SIZEOF_STRUCT_LANG_IDENTIFIER
) ? 1 : -1];
223 /* The binding oracle; see c-tree.h. */
224 void (*c_binding_oracle
) (enum c_oracle_request
, tree identifier
);
226 /* This flag is set on an identifier if we have previously asked the
227 binding oracle for this identifier's symbol binding. */
228 #define I_SYMBOL_CHECKED(node) \
229 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
231 static inline struct c_binding
* *
232 i_symbol_binding (tree node
)
234 struct lang_identifier
*lid
235 = (struct lang_identifier
*) IDENTIFIER_NODE_CHECK (node
);
237 if (lid
->symbol_binding
== NULL
238 && c_binding_oracle
!= NULL
239 && !I_SYMBOL_CHECKED (node
))
241 /* Set the "checked" flag first, to avoid infinite recursion
242 when the binding oracle calls back into gcc. */
243 I_SYMBOL_CHECKED (node
) = 1;
244 c_binding_oracle (C_ORACLE_SYMBOL
, node
);
247 return &lid
->symbol_binding
;
250 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
252 #define I_SYMBOL_DECL(node) \
253 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
255 /* This flag is set on an identifier if we have previously asked the
256 binding oracle for this identifier's tag binding. */
257 #define I_TAG_CHECKED(node) \
258 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
260 static inline struct c_binding
**
261 i_tag_binding (tree node
)
263 struct lang_identifier
*lid
264 = (struct lang_identifier
*) IDENTIFIER_NODE_CHECK (node
);
266 if (lid
->tag_binding
== NULL
267 && c_binding_oracle
!= NULL
268 && !I_TAG_CHECKED (node
))
270 /* Set the "checked" flag first, to avoid infinite recursion
271 when the binding oracle calls back into gcc. */
272 I_TAG_CHECKED (node
) = 1;
273 c_binding_oracle (C_ORACLE_TAG
, node
);
276 return &lid
->tag_binding
;
279 #define I_TAG_BINDING(node) (*i_tag_binding (node))
281 #define I_TAG_DECL(node) \
282 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
284 /* This flag is set on an identifier if we have previously asked the
285 binding oracle for this identifier's label binding. */
286 #define I_LABEL_CHECKED(node) \
287 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
289 static inline struct c_binding
**
290 i_label_binding (tree node
)
292 struct lang_identifier
*lid
293 = (struct lang_identifier
*) IDENTIFIER_NODE_CHECK (node
);
295 if (lid
->label_binding
== NULL
296 && c_binding_oracle
!= NULL
297 && !I_LABEL_CHECKED (node
))
299 /* Set the "checked" flag first, to avoid infinite recursion
300 when the binding oracle calls back into gcc. */
301 I_LABEL_CHECKED (node
) = 1;
302 c_binding_oracle (C_ORACLE_LABEL
, node
);
305 return &lid
->label_binding
;
308 #define I_LABEL_BINDING(node) (*i_label_binding (node))
310 #define I_LABEL_DECL(node) \
311 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
313 /* The resulting tree type. */
315 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
316 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
318 union tree_node
GTY ((tag ("0"),
319 desc ("tree_node_structure (&%h)")))
321 struct lang_identifier
GTY ((tag ("1"))) identifier
;
324 /* Track bindings and other things that matter for goto warnings. For
325 efficiency, we do not gather all the decls at the point of
326 definition. Instead, we point into the bindings structure. As
327 scopes are popped, we update these structures and gather the decls
328 that matter at that time. */
330 struct GTY(()) c_spot_bindings
{
331 /* The currently open scope which holds bindings defined when the
332 label was defined or the goto statement was found. */
333 struct c_scope
*scope
;
334 /* The bindings in the scope field which were defined at the point
335 of the label or goto. This lets us look at older or newer
336 bindings in the scope, as appropriate. */
337 struct c_binding
*bindings_in_scope
;
338 /* The number of statement expressions that have started since this
339 label or goto statement was defined. This is zero if we are at
340 the same statement expression level. It is positive if we are in
341 a statement expression started since this spot. It is negative
342 if this spot was in a statement expression and we have left
345 /* Whether we started in a statement expression but are no longer in
346 it. This is set to true if stmt_exprs ever goes negative. */
350 /* This structure is used to keep track of bindings seen when a goto
351 statement is defined. This is only used if we see the goto
352 statement before we see the label. */
354 struct GTY(()) c_goto_bindings
{
355 /* The location of the goto statement. */
357 /* The bindings of the goto statement. */
358 struct c_spot_bindings goto_bindings
;
361 typedef struct c_goto_bindings
*c_goto_bindings_p
;
363 /* The additional information we keep track of for a label binding.
364 These fields are updated as scopes are popped. */
366 struct GTY(()) c_label_vars
{
367 /* The shadowed c_label_vars, when one label shadows another (which
368 can only happen using a __label__ declaration). */
369 struct c_label_vars
*shadowed
;
370 /* The bindings when the label was defined. */
371 struct c_spot_bindings label_bindings
;
372 /* A list of decls that we care about: decls about which we should
373 warn if a goto branches to this label from later in the function.
374 Decls are added to this list as scopes are popped. We only add
375 the decls that matter. */
376 vec
<tree
, va_gc
> *decls_in_scope
;
377 /* A list of goto statements to this label. This is only used for
378 goto statements seen before the label was defined, so that we can
379 issue appropriate warnings for them. */
380 vec
<c_goto_bindings_p
, va_gc
> *gotos
;
383 /* Each c_scope structure describes the complete contents of one
384 scope. Four scopes are distinguished specially: the innermost or
385 current scope, the innermost function scope, the file scope (always
386 the second to outermost) and the outermost or external scope.
388 Most declarations are recorded in the current scope.
390 All normal label declarations are recorded in the innermost
391 function scope, as are bindings of undeclared identifiers to
392 error_mark_node. (GCC permits nested functions as an extension,
393 hence the 'innermost' qualifier.) Explicitly declared labels
394 (using the __label__ extension) appear in the current scope.
396 Being in the file scope (current_scope == file_scope) causes
397 special behavior in several places below. Also, under some
398 conditions the Objective-C front end records declarations in the
399 file scope even though that isn't the current scope.
401 All declarations with external linkage are recorded in the external
402 scope, even if they aren't visible there; this models the fact that
403 such declarations are visible to the entire program, and (with a
404 bit of cleverness, see pushdecl) allows diagnosis of some violations
405 of C99 6.2.2p7 and 6.2.7p2:
407 If, within the same translation unit, the same identifier appears
408 with both internal and external linkage, the behavior is
411 All declarations that refer to the same object or function shall
412 have compatible type; otherwise, the behavior is undefined.
414 Initially only the built-in declarations, which describe compiler
415 intrinsic functions plus a subset of the standard library, are in
418 The order of the blocks list matters, and it is frequently appended
419 to. To avoid having to walk all the way to the end of the list on
420 each insertion, or reverse the list later, we maintain a pointer to
421 the last list entry. (FIXME: It should be feasible to use a reversed
424 The bindings list is strictly in reverse order of declarations;
425 pop_scope relies on this. */
428 struct GTY((chain_next ("%h.outer"))) c_scope
{
429 /* The scope containing this one. */
430 struct c_scope
*outer
;
432 /* The next outermost function scope. */
433 struct c_scope
*outer_function
;
435 /* All bindings in this scope. */
436 struct c_binding
*bindings
;
438 /* For each scope (except the global one), a chain of BLOCK nodes
439 for all the scopes that were entered and exited one level down. */
443 /* The depth of this scope. Used to keep the ->shadowed chain of
444 bindings sorted innermost to outermost. */
445 unsigned int depth
: 28;
447 /* True if we are currently filling this scope with parameter
449 BOOL_BITFIELD parm_flag
: 1;
451 /* True if we saw [*] in this scope. Used to give an error messages
452 if these appears in a function definition. */
453 BOOL_BITFIELD had_vla_unspec
: 1;
455 /* True if we already complained about forward parameter decls
456 in this scope. This prevents double warnings on
457 foo (int a; int b; ...) */
458 BOOL_BITFIELD warned_forward_parm_decls
: 1;
460 /* True if this is the outermost block scope of a function body.
461 This scope contains the parameters, the local variables declared
462 in the outermost block, and all the labels (except those in
463 nested functions, or declared at block scope with __label__). */
464 BOOL_BITFIELD function_body
: 1;
466 /* True means make a BLOCK for this scope no matter what. */
467 BOOL_BITFIELD keep
: 1;
469 /* True means that an unsuffixed float constant is _Decimal64. */
470 BOOL_BITFIELD float_const_decimal64
: 1;
472 /* True if this scope has any label bindings. This is used to speed
473 up searching for labels when popping scopes, particularly since
474 labels are normally only found at function scope. */
475 BOOL_BITFIELD has_label_bindings
: 1;
477 /* True if we should issue a warning if a goto statement crosses any
478 of the bindings. We still need to check the list of bindings to
479 find the specific ones we need to warn about. This is true if
480 decl_jump_unsafe would return true for any of the bindings. This
481 is used to avoid looping over all the bindings unnecessarily. */
482 BOOL_BITFIELD has_jump_unsafe_decl
: 1;
485 /* The scope currently in effect. */
487 static GTY(()) struct c_scope
*current_scope
;
489 /* The innermost function scope. Ordinary (not explicitly declared)
490 labels, bindings to error_mark_node, and the lazily-created
491 bindings of __func__ and its friends get this scope. */
493 static GTY(()) struct c_scope
*current_function_scope
;
495 /* The C file scope. This is reset for each input translation unit. */
497 static GTY(()) struct c_scope
*file_scope
;
499 /* The outermost scope. This is used for all declarations with
500 external linkage, and only these, hence the name. */
502 static GTY(()) struct c_scope
*external_scope
;
504 /* A chain of c_scope structures awaiting reuse. */
506 static GTY((deletable
)) struct c_scope
*scope_freelist
;
508 /* A chain of c_binding structures awaiting reuse. */
510 static GTY((deletable
)) struct c_binding
*binding_freelist
;
512 /* Append VAR to LIST in scope SCOPE. */
513 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
514 struct c_scope *s_ = (scope); \
516 if (s_->list##_last) \
517 BLOCK_CHAIN (s_->list##_last) = d_; \
520 s_->list##_last = d_; \
523 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
524 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
525 struct c_scope *t_ = (tscope); \
526 struct c_scope *f_ = (fscope); \
528 BLOCK_CHAIN (t_->to##_last) = f_->from; \
531 t_->to##_last = f_->from##_last; \
534 /* A c_inline_static structure stores details of a static identifier
535 referenced in a definition of a function that may be an inline
536 definition if no subsequent declaration of that function uses
537 "extern" or does not use "inline". */
539 struct GTY((chain_next ("%h.next"))) c_inline_static
{
540 /* The location for a diagnostic. */
543 /* The function that may be an inline definition. */
546 /* The object or function referenced. */
549 /* What sort of reference this is. */
550 enum c_inline_static_type type
;
552 /* The next such structure or NULL. */
553 struct c_inline_static
*next
;
556 /* List of static identifiers used or referenced in functions that may
557 be inline definitions. */
558 static GTY(()) struct c_inline_static
*c_inline_statics
;
560 /* True means unconditionally make a BLOCK for the next scope pushed. */
562 static bool keep_next_level_flag
;
564 /* True means the next call to push_scope will be the outermost scope
565 of a function body, so do not push a new scope, merely cease
566 expecting parameter decls. */
568 static bool next_is_function_body
;
570 /* A vector of pointers to c_binding structures. */
572 typedef struct c_binding
*c_binding_ptr
;
574 /* Information that we keep for a struct or union while it is being
577 struct c_struct_parse_info
579 /* If warn_cxx_compat, a list of types defined within this
581 auto_vec
<tree
> struct_types
;
582 /* If warn_cxx_compat, a list of field names which have bindings,
583 and which are defined in this struct, but which are not defined
584 in any enclosing struct. This is used to clear the in_struct
585 field of the c_bindings structure. */
586 auto_vec
<c_binding_ptr
> fields
;
587 /* If warn_cxx_compat, a list of typedef names used when defining
588 fields in this struct. */
589 auto_vec
<tree
> typedefs_seen
;
592 /* Information for the struct or union currently being parsed, or
593 NULL if not parsing a struct or union. */
594 static struct c_struct_parse_info
*struct_parse_info
;
596 /* Forward declarations. */
597 static tree
lookup_name_in_scope (tree
, struct c_scope
*);
598 static tree
c_make_fname_decl (location_t
, tree
, int);
599 static tree
grokdeclarator (const struct c_declarator
*,
600 struct c_declspecs
*,
601 enum decl_context
, bool, tree
*, tree
*, tree
*,
602 bool *, enum deprecated_states
);
603 static tree
grokparms (struct c_arg_info
*, bool);
604 static void layout_array_type (tree
);
605 static void warn_defaults_to (location_t
, int, const char *, ...)
606 ATTRIBUTE_GCC_DIAG(3,4);
607 static const char *header_for_builtin_fn (enum built_in_function
);
609 /* T is a statement. Add it to the statement-tree. This is the
610 C/ObjC version--C++ has a slightly different version of this
616 enum tree_code code
= TREE_CODE (t
);
618 if (CAN_HAVE_LOCATION_P (t
) && code
!= LABEL_EXPR
)
620 if (!EXPR_HAS_LOCATION (t
))
621 SET_EXPR_LOCATION (t
, input_location
);
624 if (code
== LABEL_EXPR
|| code
== CASE_LABEL_EXPR
)
625 STATEMENT_LIST_HAS_LABEL (cur_stmt_list
) = 1;
627 /* Add T to the statement-tree. Non-side-effect statements need to be
628 recorded during statement expressions. */
629 if (!building_stmt_list_p ())
631 append_to_statement_list_force (t
, &cur_stmt_list
);
636 /* Build a pointer type using the default pointer mode. */
639 c_build_pointer_type (tree to_type
)
641 addr_space_t as
= to_type
== error_mark_node
? ADDR_SPACE_GENERIC
642 : TYPE_ADDR_SPACE (to_type
);
643 machine_mode pointer_mode
;
645 if (as
!= ADDR_SPACE_GENERIC
|| c_default_pointer_mode
== VOIDmode
)
646 pointer_mode
= targetm
.addr_space
.pointer_mode (as
);
648 pointer_mode
= c_default_pointer_mode
;
649 return build_pointer_type_for_mode (to_type
, pointer_mode
, false);
653 /* Return true if we will want to say something if a goto statement
657 decl_jump_unsafe (tree decl
)
659 if (error_operand_p (decl
))
662 /* Don't warn for compound literals. If a goto statement crosses
663 their initialization, it should cross also all the places where
664 the complit is used or where the complit address might be saved
665 into some variable, so code after the label to which goto jumps
666 should not be able to refer to the compound literal. */
667 if (VAR_P (decl
) && C_DECL_COMPOUND_LITERAL_P (decl
))
670 /* Always warn about crossing variably modified types. */
671 if ((VAR_P (decl
) || TREE_CODE (decl
) == TYPE_DECL
)
672 && variably_modified_type_p (TREE_TYPE (decl
), NULL_TREE
))
675 /* Otherwise, only warn if -Wgoto-misses-init and this is an
676 initialized automatic decl. */
677 if (warn_jump_misses_init
679 && !TREE_STATIC (decl
)
680 && DECL_INITIAL (decl
) != NULL_TREE
)
688 c_print_identifier (FILE *file
, tree node
, int indent
)
690 void (*save
) (enum c_oracle_request
, tree identifier
);
692 /* Temporarily hide any binding oracle. Without this, calls to
693 debug_tree from the debugger will end up calling into the oracle,
694 making for a confusing debug session. As the oracle isn't needed
695 here for normal operation, it's simplest to suppress it. */
696 save
= c_binding_oracle
;
697 c_binding_oracle
= NULL
;
699 print_node (file
, "symbol", I_SYMBOL_DECL (node
), indent
+ 4);
700 print_node (file
, "tag", I_TAG_DECL (node
), indent
+ 4);
701 print_node (file
, "label", I_LABEL_DECL (node
), indent
+ 4);
702 if (C_IS_RESERVED_WORD (node
) && C_RID_CODE (node
) != RID_CXX_COMPAT_WARN
)
704 tree rid
= ridpointers
[C_RID_CODE (node
)];
705 indent_to (file
, indent
+ 4);
706 fprintf (file
, "rid " HOST_PTR_PRINTF
" \"%s\"",
707 (void *) rid
, IDENTIFIER_POINTER (rid
));
710 c_binding_oracle
= save
;
713 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
714 which may be any of several kinds of DECL or TYPE or error_mark_node,
715 in the scope SCOPE. */
717 bind (tree name
, tree decl
, struct c_scope
*scope
, bool invisible
,
718 bool nested
, location_t locus
)
720 struct c_binding
*b
, **here
;
722 if (binding_freelist
)
724 b
= binding_freelist
;
725 binding_freelist
= b
->prev
;
728 b
= ggc_alloc
<c_binding
> ();
733 b
->depth
= scope
->depth
;
734 b
->invisible
= invisible
;
742 b
->prev
= scope
->bindings
;
745 if (decl_jump_unsafe (decl
))
746 scope
->has_jump_unsafe_decl
= 1;
751 switch (TREE_CODE (decl
))
753 case LABEL_DECL
: here
= &I_LABEL_BINDING (name
); break;
756 case RECORD_TYPE
: here
= &I_TAG_BINDING (name
); break;
762 case ERROR_MARK
: here
= &I_SYMBOL_BINDING (name
); break;
768 /* Locate the appropriate place in the chain of shadowed decls
769 to insert this binding. Normally, scope == current_scope and
770 this does nothing. */
771 while (*here
&& (*here
)->depth
> scope
->depth
)
772 here
= &(*here
)->shadowed
;
778 /* Clear the binding structure B, stick it on the binding_freelist,
779 and return the former value of b->prev. This is used by pop_scope
780 and get_parm_info to iterate destructively over all the bindings
781 from a given scope. */
782 static struct c_binding
*
783 free_binding_and_advance (struct c_binding
*b
)
785 struct c_binding
*prev
= b
->prev
;
787 memset (b
, 0, sizeof (struct c_binding
));
788 b
->prev
= binding_freelist
;
789 binding_freelist
= b
;
794 /* Bind a label. Like bind, but skip fields which aren't used for
795 labels, and add the LABEL_VARS value. */
797 bind_label (tree name
, tree label
, struct c_scope
*scope
,
798 struct c_label_vars
*label_vars
)
802 bind (name
, label
, scope
, /*invisible=*/false, /*nested=*/false,
805 scope
->has_label_bindings
= true;
808 gcc_assert (b
->decl
== label
);
809 label_vars
->shadowed
= b
->u
.label
;
810 b
->u
.label
= label_vars
;
813 /* Hook called at end of compilation to assume 1 elt
814 for a file-scope tentative array defn that wasn't complete before. */
817 c_finish_incomplete_decl (tree decl
)
821 tree type
= TREE_TYPE (decl
);
822 if (type
!= error_mark_node
823 && TREE_CODE (type
) == ARRAY_TYPE
824 && !DECL_EXTERNAL (decl
)
825 && TYPE_DOMAIN (type
) == NULL_TREE
)
827 warning_at (DECL_SOURCE_LOCATION (decl
),
828 0, "array %q+D assumed to have one element", decl
);
830 complete_array_type (&TREE_TYPE (decl
), NULL_TREE
, true);
832 relayout_decl (decl
);
837 /* Record that inline function FUNC contains a reference (location
838 LOC) to static DECL (file-scope or function-local according to
842 record_inline_static (location_t loc
, tree func
, tree decl
,
843 enum c_inline_static_type type
)
845 c_inline_static
*csi
= ggc_alloc
<c_inline_static
> ();
847 csi
->function
= func
;
848 csi
->static_decl
= decl
;
850 csi
->next
= c_inline_statics
;
851 c_inline_statics
= csi
;
854 /* Check for references to static declarations in inline functions at
855 the end of the translation unit and diagnose them if the functions
856 are still inline definitions. */
859 check_inline_statics (void)
861 struct c_inline_static
*csi
;
862 for (csi
= c_inline_statics
; csi
; csi
= csi
->next
)
864 if (DECL_EXTERNAL (csi
->function
))
868 pedwarn (csi
->location
, 0,
869 "%qD is static but used in inline function %qD "
870 "which is not static", csi
->static_decl
, csi
->function
);
873 pedwarn (csi
->location
, 0,
874 "%q+D is static but declared in inline function %qD "
875 "which is not static", csi
->static_decl
, csi
->function
);
881 c_inline_statics
= NULL
;
884 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
885 for the current state, otherwise set it to uninitialized. */
888 set_spot_bindings (struct c_spot_bindings
*p
, bool defining
)
892 p
->scope
= current_scope
;
893 p
->bindings_in_scope
= current_scope
->bindings
;
898 p
->bindings_in_scope
= NULL
;
901 p
->left_stmt_expr
= false;
904 /* Update spot bindings P as we pop out of SCOPE. Return true if we
905 should push decls for a label. */
908 update_spot_bindings (struct c_scope
*scope
, struct c_spot_bindings
*p
)
910 if (p
->scope
!= scope
)
912 /* This label or goto is defined in some other scope, or it is a
913 label which is not yet defined. There is nothing to
918 /* Adjust the spot bindings to refer to the bindings already defined
919 in the enclosing scope. */
920 p
->scope
= scope
->outer
;
921 p
->bindings_in_scope
= p
->scope
->bindings
;
926 /* The Objective-C front-end often needs to determine the current scope. */
929 objc_get_current_scope (void)
931 return current_scope
;
934 /* The following function is used only by Objective-C. It needs to live here
935 because it accesses the innards of c_scope. */
938 objc_mark_locals_volatile (void *enclosing_blk
)
940 struct c_scope
*scope
;
943 for (scope
= current_scope
;
944 scope
&& scope
!= enclosing_blk
;
945 scope
= scope
->outer
)
947 for (b
= scope
->bindings
; b
; b
= b
->prev
)
948 objc_volatilize_decl (b
->decl
);
950 /* Do not climb up past the current function. */
951 if (scope
->function_body
)
956 /* Return true if we are in the global binding level. */
959 global_bindings_p (void)
961 return current_scope
== file_scope
;
964 /* Return true if we're declaring parameters in an old-style function
968 old_style_parameter_scope (void)
970 /* If processing parameters and there is no function statement list, we
971 * have an old-style function declaration. */
972 return (current_scope
->parm_flag
&& !DECL_SAVED_TREE (current_function_decl
));
976 keep_next_level (void)
978 keep_next_level_flag
= true;
981 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
984 set_float_const_decimal64 (void)
986 current_scope
->float_const_decimal64
= true;
989 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
992 clear_float_const_decimal64 (void)
994 current_scope
->float_const_decimal64
= false;
997 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
1000 float_const_decimal64_p (void)
1002 return current_scope
->float_const_decimal64
;
1005 /* Identify this scope as currently being filled with parameters. */
1008 declare_parm_level (void)
1010 current_scope
->parm_flag
= true;
1016 if (next_is_function_body
)
1018 /* This is the transition from the parameters to the top level
1019 of the function body. These are the same scope
1020 (C99 6.2.1p4,6) so we do not push another scope structure.
1021 next_is_function_body is set only by store_parm_decls, which
1022 in turn is called when and only when we are about to
1023 encounter the opening curly brace for the function body.
1025 The outermost block of a function always gets a BLOCK node,
1026 because the debugging output routines expect that each
1027 function has at least one BLOCK. */
1028 current_scope
->parm_flag
= false;
1029 current_scope
->function_body
= true;
1030 current_scope
->keep
= true;
1031 current_scope
->outer_function
= current_function_scope
;
1032 current_function_scope
= current_scope
;
1034 keep_next_level_flag
= false;
1035 next_is_function_body
= false;
1037 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1038 if (current_scope
->outer
)
1039 current_scope
->float_const_decimal64
1040 = current_scope
->outer
->float_const_decimal64
;
1042 current_scope
->float_const_decimal64
= false;
1046 struct c_scope
*scope
;
1049 scope
= scope_freelist
;
1050 scope_freelist
= scope
->outer
;
1053 scope
= ggc_cleared_alloc
<c_scope
> ();
1055 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1057 scope
->float_const_decimal64
= current_scope
->float_const_decimal64
;
1059 scope
->float_const_decimal64
= false;
1061 scope
->keep
= keep_next_level_flag
;
1062 scope
->outer
= current_scope
;
1063 scope
->depth
= current_scope
? (current_scope
->depth
+ 1) : 0;
1065 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1067 if (current_scope
&& scope
->depth
== 0)
1070 sorry ("GCC supports only %u nested scopes", scope
->depth
);
1073 current_scope
= scope
;
1074 keep_next_level_flag
= false;
1078 /* This is called when we are leaving SCOPE. For each label defined
1079 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1080 These are the decls whose initialization will be skipped by a goto
1081 later in the function. */
1084 update_label_decls (struct c_scope
*scope
)
1091 if (s
->has_label_bindings
)
1093 struct c_binding
*b
;
1095 for (b
= s
->bindings
; b
!= NULL
; b
= b
->prev
)
1097 struct c_label_vars
*label_vars
;
1098 struct c_binding
*b1
;
1101 struct c_goto_bindings
*g
;
1103 if (TREE_CODE (b
->decl
) != LABEL_DECL
)
1105 label_vars
= b
->u
.label
;
1107 b1
= label_vars
->label_bindings
.bindings_in_scope
;
1108 if (label_vars
->label_bindings
.scope
== NULL
)
1111 hjud
= label_vars
->label_bindings
.scope
->has_jump_unsafe_decl
;
1112 if (update_spot_bindings (scope
, &label_vars
->label_bindings
))
1114 /* This label is defined in this scope. */
1117 for (; b1
!= NULL
; b1
= b1
->prev
)
1119 /* A goto from later in the function to this
1120 label will never see the initialization
1121 of B1, if any. Save it to issue a
1122 warning if needed. */
1123 if (decl_jump_unsafe (b1
->decl
))
1124 vec_safe_push(label_vars
->decls_in_scope
, b1
->decl
);
1129 /* Update the bindings of any goto statements associated
1131 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
1132 update_spot_bindings (scope
, &g
->goto_bindings
);
1136 /* Don't search beyond the current function. */
1137 if (s
== current_function_scope
)
1144 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1147 set_type_context (tree type
, tree context
)
1149 for (type
= TYPE_MAIN_VARIANT (type
); type
;
1150 type
= TYPE_NEXT_VARIANT (type
))
1151 TYPE_CONTEXT (type
) = context
;
1154 /* Exit a scope. Restore the state of the identifier-decl mappings
1155 that were in effect when this scope was entered. Return a BLOCK
1156 node containing all the DECLs in this scope that are of interest
1157 to debug info generation. */
1162 struct c_scope
*scope
= current_scope
;
1163 tree block
, context
, p
;
1164 struct c_binding
*b
;
1166 bool functionbody
= scope
->function_body
;
1167 bool keep
= functionbody
|| scope
->keep
|| scope
->bindings
;
1169 update_label_decls (scope
);
1171 /* If appropriate, create a BLOCK to record the decls for the life
1172 of this function. */
1176 block
= make_node (BLOCK
);
1177 BLOCK_SUBBLOCKS (block
) = scope
->blocks
;
1178 TREE_USED (block
) = 1;
1180 /* In each subblock, record that this is its superior. */
1181 for (p
= scope
->blocks
; p
; p
= BLOCK_CHAIN (p
))
1182 BLOCK_SUPERCONTEXT (p
) = block
;
1184 BLOCK_VARS (block
) = NULL_TREE
;
1187 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1188 scope must be set so that they point to the appropriate
1189 construct, i.e. either to the current FUNCTION_DECL node, or
1190 else to the BLOCK node we just constructed.
1192 Note that for tagged types whose scope is just the formal
1193 parameter list for some function type specification, we can't
1194 properly set their TYPE_CONTEXTs here, because we don't have a
1195 pointer to the appropriate FUNCTION_TYPE node readily available
1196 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1197 type nodes get set in `grokdeclarator' as soon as we have created
1198 the FUNCTION_TYPE node which will represent the "scope" for these
1199 "parameter list local" tagged types. */
1200 if (scope
->function_body
)
1201 context
= current_function_decl
;
1202 else if (scope
== file_scope
)
1205 = build_translation_unit_decl (get_identifier (main_input_filename
));
1206 context
= file_decl
;
1207 debug_hooks
->register_main_translation_unit (file_decl
);
1212 /* Clear all bindings in this scope. */
1213 for (b
= scope
->bindings
; b
; b
= free_binding_and_advance (b
))
1216 switch (TREE_CODE (p
))
1219 /* Warnings for unused labels, errors for undefined labels. */
1220 if (TREE_USED (p
) && !DECL_INITIAL (p
))
1222 error ("label %q+D used but not defined", p
);
1223 DECL_INITIAL (p
) = error_mark_node
;
1226 warn_for_unused_label (p
);
1228 /* Labels go in BLOCK_VARS. */
1229 DECL_CHAIN (p
) = BLOCK_VARS (block
);
1230 BLOCK_VARS (block
) = p
;
1231 gcc_assert (I_LABEL_BINDING (b
->id
) == b
);
1232 I_LABEL_BINDING (b
->id
) = b
->shadowed
;
1234 /* Also pop back to the shadowed label_vars. */
1235 release_tree_vector (b
->u
.label
->decls_in_scope
);
1236 b
->u
.label
= b
->u
.label
->shadowed
;
1242 set_type_context (p
, context
);
1244 /* Types may not have tag-names, in which case the type
1245 appears in the bindings list with b->id NULL. */
1248 gcc_assert (I_TAG_BINDING (b
->id
) == b
);
1249 I_TAG_BINDING (b
->id
) = b
->shadowed
;
1254 /* Propagate TREE_ADDRESSABLE from nested functions to their
1255 containing functions. */
1256 if (!TREE_ASM_WRITTEN (p
)
1257 && DECL_INITIAL (p
) != NULL_TREE
1258 && TREE_ADDRESSABLE (p
)
1259 && DECL_ABSTRACT_ORIGIN (p
) != NULL_TREE
1260 && DECL_ABSTRACT_ORIGIN (p
) != p
)
1261 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p
)) = 1;
1262 if (!TREE_PUBLIC (p
)
1263 && !DECL_INITIAL (p
)
1265 && scope
!= file_scope
1266 && scope
!= external_scope
)
1268 error ("nested function %q+D declared but never defined", p
);
1269 undef_nested_function
= true;
1271 else if (DECL_DECLARED_INLINE_P (p
)
1273 && !DECL_INITIAL (p
))
1275 /* C99 6.7.4p6: "a function with external linkage... declared
1276 with an inline function specifier ... shall also be defined
1277 in the same translation unit." */
1278 if (!flag_gnu89_inline
1279 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p
))
1280 && scope
== external_scope
)
1281 pedwarn (input_location
, 0,
1282 "inline function %q+D declared but never defined", p
);
1283 DECL_EXTERNAL (p
) = 1;
1289 /* Warnings for unused variables. */
1290 if ((!TREE_USED (p
) || !DECL_READ_P (p
))
1291 && !TREE_NO_WARNING (p
)
1292 && !DECL_IN_SYSTEM_HEADER (p
)
1294 && !DECL_ARTIFICIAL (p
)
1295 && scope
!= file_scope
1296 && scope
!= external_scope
)
1299 warning (OPT_Wunused_variable
, "unused variable %q+D", p
);
1300 else if (DECL_CONTEXT (p
) == current_function_decl
)
1301 warning_at (DECL_SOURCE_LOCATION (p
),
1302 OPT_Wunused_but_set_variable
,
1303 "variable %qD set but not used", p
);
1308 error ("type of array %q+D completed incompatibly with"
1309 " implicit initialization", p
);
1316 /* All of these go in BLOCK_VARS, but only if this is the
1317 binding in the home scope. */
1320 DECL_CHAIN (p
) = BLOCK_VARS (block
);
1321 BLOCK_VARS (block
) = p
;
1323 else if (VAR_OR_FUNCTION_DECL_P (p
) && scope
!= file_scope
)
1325 /* For block local externs add a special
1326 DECL_EXTERNAL decl for debug info generation. */
1327 tree extp
= copy_node (p
);
1329 DECL_EXTERNAL (extp
) = 1;
1330 TREE_STATIC (extp
) = 0;
1331 TREE_PUBLIC (extp
) = 1;
1332 DECL_INITIAL (extp
) = NULL_TREE
;
1333 DECL_LANG_SPECIFIC (extp
) = NULL
;
1334 DECL_CONTEXT (extp
) = current_function_decl
;
1335 if (TREE_CODE (p
) == FUNCTION_DECL
)
1337 DECL_RESULT (extp
) = NULL_TREE
;
1338 DECL_SAVED_TREE (extp
) = NULL_TREE
;
1339 DECL_STRUCT_FUNCTION (extp
) = NULL
;
1341 if (b
->locus
!= UNKNOWN_LOCATION
)
1342 DECL_SOURCE_LOCATION (extp
) = b
->locus
;
1343 DECL_CHAIN (extp
) = BLOCK_VARS (block
);
1344 BLOCK_VARS (block
) = extp
;
1346 /* If this is the file scope set DECL_CONTEXT of each decl to
1347 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1349 if (scope
== file_scope
)
1351 DECL_CONTEXT (p
) = context
;
1352 if (TREE_CODE (p
) == TYPE_DECL
1353 && TREE_TYPE (p
) != error_mark_node
)
1354 set_type_context (TREE_TYPE (p
), context
);
1358 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1359 already been put there by store_parm_decls. Unused-
1360 parameter warnings are handled by function.c.
1361 error_mark_node obviously does not go in BLOCK_VARS and
1362 does not get unused-variable warnings. */
1365 /* It is possible for a decl not to have a name. We get
1366 here with b->id NULL in this case. */
1369 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
1370 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
1371 if (b
->shadowed
&& b
->shadowed
->u
.type
)
1372 TREE_TYPE (b
->shadowed
->decl
) = b
->shadowed
->u
.type
;
1382 /* Dispose of the block that we just made inside some higher level. */
1383 if ((scope
->function_body
|| scope
== file_scope
) && context
)
1385 DECL_INITIAL (context
) = block
;
1386 BLOCK_SUPERCONTEXT (block
) = context
;
1388 else if (scope
->outer
)
1391 SCOPE_LIST_APPEND (scope
->outer
, blocks
, block
);
1392 /* If we did not make a block for the scope just exited, any
1393 blocks made for inner scopes must be carried forward so they
1394 will later become subblocks of something else. */
1395 else if (scope
->blocks
)
1396 SCOPE_LIST_CONCAT (scope
->outer
, blocks
, scope
, blocks
);
1399 /* Pop the current scope, and free the structure for reuse. */
1400 current_scope
= scope
->outer
;
1401 if (scope
->function_body
)
1402 current_function_scope
= scope
->outer_function
;
1404 memset (scope
, 0, sizeof (struct c_scope
));
1405 scope
->outer
= scope_freelist
;
1406 scope_freelist
= scope
;
1412 push_file_scope (void)
1420 file_scope
= current_scope
;
1422 start_fname_decls ();
1424 for (decl
= visible_builtins
; decl
; decl
= DECL_CHAIN (decl
))
1425 bind (DECL_NAME (decl
), decl
, file_scope
,
1426 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl
));
1430 pop_file_scope (void)
1432 /* In case there were missing closebraces, get us back to the global
1434 while (current_scope
!= file_scope
)
1437 /* __FUNCTION__ is defined at file scope (""). This
1438 call may not be necessary as my tests indicate it
1439 still works without it. */
1440 finish_fname_decls ();
1442 check_inline_statics ();
1444 /* This is the point to write out a PCH if we're doing that.
1445 In that case we do not want to do anything else. */
1448 c_common_write_pch ();
1449 /* Ensure even the callers don't try to finalize the CU. */
1450 flag_syntax_only
= 1;
1454 /* Pop off the file scope and close this translation unit. */
1458 maybe_apply_pending_pragma_weaks ();
1461 /* Adjust the bindings for the start of a statement expression. */
1464 c_bindings_start_stmt_expr (struct c_spot_bindings
* switch_bindings
)
1466 struct c_scope
*scope
;
1468 for (scope
= current_scope
; scope
!= NULL
; scope
= scope
->outer
)
1470 struct c_binding
*b
;
1472 if (!scope
->has_label_bindings
)
1475 for (b
= scope
->bindings
; b
!= NULL
; b
= b
->prev
)
1477 struct c_label_vars
*label_vars
;
1479 struct c_goto_bindings
*g
;
1481 if (TREE_CODE (b
->decl
) != LABEL_DECL
)
1483 label_vars
= b
->u
.label
;
1484 ++label_vars
->label_bindings
.stmt_exprs
;
1485 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
1486 ++g
->goto_bindings
.stmt_exprs
;
1490 if (switch_bindings
!= NULL
)
1491 ++switch_bindings
->stmt_exprs
;
1494 /* Adjust the bindings for the end of a statement expression. */
1497 c_bindings_end_stmt_expr (struct c_spot_bindings
*switch_bindings
)
1499 struct c_scope
*scope
;
1501 for (scope
= current_scope
; scope
!= NULL
; scope
= scope
->outer
)
1503 struct c_binding
*b
;
1505 if (!scope
->has_label_bindings
)
1508 for (b
= scope
->bindings
; b
!= NULL
; b
= b
->prev
)
1510 struct c_label_vars
*label_vars
;
1512 struct c_goto_bindings
*g
;
1514 if (TREE_CODE (b
->decl
) != LABEL_DECL
)
1516 label_vars
= b
->u
.label
;
1517 --label_vars
->label_bindings
.stmt_exprs
;
1518 if (label_vars
->label_bindings
.stmt_exprs
< 0)
1520 label_vars
->label_bindings
.left_stmt_expr
= true;
1521 label_vars
->label_bindings
.stmt_exprs
= 0;
1523 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
1525 --g
->goto_bindings
.stmt_exprs
;
1526 if (g
->goto_bindings
.stmt_exprs
< 0)
1528 g
->goto_bindings
.left_stmt_expr
= true;
1529 g
->goto_bindings
.stmt_exprs
= 0;
1535 if (switch_bindings
!= NULL
)
1537 --switch_bindings
->stmt_exprs
;
1538 gcc_assert (switch_bindings
->stmt_exprs
>= 0);
1542 /* Push a definition or a declaration of struct, union or enum tag "name".
1543 "type" should be the type node.
1544 We assume that the tag "name" is not already defined, and has a location
1547 Note that the definition may really be just a forward reference.
1548 In that case, the TYPE_SIZE will be zero. */
1551 pushtag (location_t loc
, tree name
, tree type
)
1553 /* Record the identifier as the type's name if it has none. */
1554 if (name
&& !TYPE_NAME (type
))
1555 TYPE_NAME (type
) = name
;
1556 bind (name
, type
, current_scope
, /*invisible=*/false, /*nested=*/false, loc
);
1558 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1559 tagged type we just added to the current scope. This fake
1560 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1561 to output a representation of a tagged type, and it also gives
1562 us a convenient place to record the "scope start" address for the
1565 TYPE_STUB_DECL (type
) = pushdecl (build_decl (loc
,
1566 TYPE_DECL
, NULL_TREE
, type
));
1568 /* An approximation for now, so we can tell this is a function-scope tag.
1569 This will be updated in pop_scope. */
1570 TYPE_CONTEXT (type
) = DECL_CONTEXT (TYPE_STUB_DECL (type
));
1572 if (warn_cxx_compat
&& name
!= NULL_TREE
)
1574 struct c_binding
*b
= I_SYMBOL_BINDING (name
);
1577 && b
->decl
!= NULL_TREE
1578 && TREE_CODE (b
->decl
) == TYPE_DECL
1579 && (B_IN_CURRENT_SCOPE (b
)
1580 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
1581 && (TYPE_MAIN_VARIANT (TREE_TYPE (b
->decl
))
1582 != TYPE_MAIN_VARIANT (type
)))
1584 auto_diagnostic_group d
;
1585 if (warning_at (loc
, OPT_Wc___compat
,
1586 ("using %qD as both a typedef and a tag is "
1587 "invalid in C++"), b
->decl
)
1588 && b
->locus
!= UNKNOWN_LOCATION
)
1589 inform (b
->locus
, "originally defined here");
1594 /* An exported interface to pushtag. This is used by the gdb plugin's
1595 binding oracle to introduce a new tag binding. */
1598 c_pushtag (location_t loc
, tree name
, tree type
)
1600 pushtag (loc
, name
, type
);
1603 /* An exported interface to bind a declaration. LOC is the location
1604 to use. DECL is the declaration to bind. The decl's name is used
1605 to determine how it is bound. If DECL is a VAR_DECL, then
1606 IS_GLOBAL determines whether the decl is put into the global (file
1607 and external) scope or the current function's scope; if DECL is not
1608 a VAR_DECL then it is always put into the file scope. */
1611 c_bind (location_t loc
, tree decl
, bool is_global
)
1613 struct c_scope
*scope
;
1614 bool nested
= false;
1616 if (!VAR_P (decl
) || current_function_scope
== NULL
)
1618 /* Types and functions are always considered to be global. */
1620 DECL_EXTERNAL (decl
) = 1;
1621 TREE_PUBLIC (decl
) = 1;
1625 /* Also bind it into the external scope. */
1626 bind (DECL_NAME (decl
), decl
, external_scope
, true, false, loc
);
1629 DECL_EXTERNAL (decl
) = 1;
1630 TREE_PUBLIC (decl
) = 1;
1634 DECL_CONTEXT (decl
) = current_function_decl
;
1635 TREE_PUBLIC (decl
) = 0;
1636 scope
= current_function_scope
;
1639 bind (DECL_NAME (decl
), decl
, scope
, false, nested
, loc
);
1643 /* Stores the first FILE*, const struct tm* etc. argument type (whatever it
1644 is) seen in a declaration of a file I/O etc. built-in. Subsequent
1645 declarations of such built-ins are expected to refer to it rather than to
1646 fileptr_type_node etc. which is just void* (or to any other type).
1647 Used only by match_builtin_function_types. */
1649 static GTY(()) tree last_structptr_types
[6];
1651 /* Subroutine of compare_decls. Allow harmless mismatches in return
1652 and argument types provided that the type modes match. Set *STRICT
1653 and *ARGNO to the expected argument type and number in case of
1654 an argument type mismatch or null and zero otherwise. Return
1655 a unified type given a suitable match, and 0 otherwise. */
1658 match_builtin_function_types (tree newtype
, tree oldtype
,
1659 tree
*strict
, unsigned *argno
)
1661 /* Accept the return type of the new declaration if same modes. */
1662 tree oldrettype
= TREE_TYPE (oldtype
);
1663 tree newrettype
= TREE_TYPE (newtype
);
1666 *strict
= NULL_TREE
;
1668 if (TYPE_MODE (oldrettype
) != TYPE_MODE (newrettype
))
1671 if (!comptypes (TYPE_MAIN_VARIANT (oldrettype
),
1672 TYPE_MAIN_VARIANT (newrettype
)))
1673 *strict
= oldrettype
;
1675 tree oldargs
= TYPE_ARG_TYPES (oldtype
);
1676 tree newargs
= TYPE_ARG_TYPES (newtype
);
1677 tree tryargs
= newargs
;
1679 gcc_checking_assert ((sizeof (last_structptr_types
)
1680 / sizeof (last_structptr_types
[0]))
1681 == (sizeof (builtin_structptr_types
)
1682 / sizeof (builtin_structptr_types
[0])));
1683 for (unsigned i
= 1; oldargs
|| newargs
; ++i
)
1687 || !TREE_VALUE (oldargs
)
1688 || !TREE_VALUE (newargs
))
1691 tree oldtype
= TYPE_MAIN_VARIANT (TREE_VALUE (oldargs
));
1692 tree newtype
= TYPE_MAIN_VARIANT (TREE_VALUE (newargs
));
1694 /* Fail for types with incompatible modes/sizes. */
1695 if (TYPE_MODE (TREE_VALUE (oldargs
))
1696 != TYPE_MODE (TREE_VALUE (newargs
)))
1699 /* Fail for function and object pointer mismatches. */
1700 if ((FUNCTION_POINTER_TYPE_P (oldtype
)
1701 != FUNCTION_POINTER_TYPE_P (newtype
))
1702 || POINTER_TYPE_P (oldtype
) != POINTER_TYPE_P (newtype
))
1705 unsigned j
= (sizeof (builtin_structptr_types
)
1706 / sizeof (builtin_structptr_types
[0]));
1707 if (POINTER_TYPE_P (oldtype
))
1708 for (j
= 0; j
< (sizeof (builtin_structptr_types
)
1709 / sizeof (builtin_structptr_types
[0])); ++j
)
1711 if (TREE_VALUE (oldargs
) != builtin_structptr_types
[j
].node
)
1713 /* Store the first FILE* etc. argument type (whatever it is), and
1714 expect any subsequent declarations of file I/O etc. built-ins
1715 to refer to it rather than to fileptr_type_node etc. which is
1716 just void* (or const void*). */
1717 if (last_structptr_types
[j
])
1719 if (!comptypes (last_structptr_types
[j
], newtype
))
1722 *strict
= last_structptr_types
[j
];
1726 last_structptr_types
[j
] = newtype
;
1729 if (j
== (sizeof (builtin_structptr_types
)
1730 / sizeof (builtin_structptr_types
[0]))
1732 && !comptypes (oldtype
, newtype
))
1738 oldargs
= TREE_CHAIN (oldargs
);
1739 newargs
= TREE_CHAIN (newargs
);
1742 tree trytype
= build_function_type (newrettype
, tryargs
);
1744 /* Allow declaration to change transaction_safe attribute. */
1745 tree oldattrs
= TYPE_ATTRIBUTES (oldtype
);
1746 tree oldtsafe
= lookup_attribute ("transaction_safe", oldattrs
);
1747 tree newattrs
= TYPE_ATTRIBUTES (newtype
);
1748 tree newtsafe
= lookup_attribute ("transaction_safe", newattrs
);
1749 if (oldtsafe
&& !newtsafe
)
1750 oldattrs
= remove_attribute ("transaction_safe", oldattrs
);
1751 else if (newtsafe
&& !oldtsafe
)
1752 oldattrs
= tree_cons (get_identifier ("transaction_safe"),
1753 NULL_TREE
, oldattrs
);
1755 return build_type_attribute_variant (trytype
, oldattrs
);
1758 /* Subroutine of diagnose_mismatched_decls. Check for function type
1759 mismatch involving an empty arglist vs a nonempty one and give clearer
1762 diagnose_arglist_conflict (tree newdecl
, tree olddecl
,
1763 tree newtype
, tree oldtype
)
1767 if (TREE_CODE (olddecl
) != FUNCTION_DECL
1768 || !comptypes (TREE_TYPE (oldtype
), TREE_TYPE (newtype
))
1769 || !((!prototype_p (oldtype
) && DECL_INITIAL (olddecl
) == NULL_TREE
)
1770 || (!prototype_p (newtype
) && DECL_INITIAL (newdecl
) == NULL_TREE
)))
1773 t
= TYPE_ARG_TYPES (oldtype
);
1775 t
= TYPE_ARG_TYPES (newtype
);
1776 for (; t
; t
= TREE_CHAIN (t
))
1778 tree type
= TREE_VALUE (t
);
1780 if (TREE_CHAIN (t
) == NULL_TREE
1781 && TYPE_MAIN_VARIANT (type
) != void_type_node
)
1783 inform (input_location
, "a parameter list with an ellipsis can%'t match "
1784 "an empty parameter name list declaration");
1788 if (c_type_promotes_to (type
) != type
)
1790 inform (input_location
, "an argument type that has a default promotion can%'t match "
1791 "an empty parameter name list declaration");
1797 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1798 old-style function definition, NEWDECL is a prototype declaration.
1799 Diagnose inconsistencies in the argument list. Returns TRUE if
1800 the prototype is compatible, FALSE if not. */
1802 validate_proto_after_old_defn (tree newdecl
, tree newtype
, tree oldtype
)
1804 tree newargs
, oldargs
;
1807 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1809 oldargs
= TYPE_ACTUAL_ARG_TYPES (oldtype
);
1810 newargs
= TYPE_ARG_TYPES (newtype
);
1815 tree oldargtype
= TREE_VALUE (oldargs
);
1816 tree newargtype
= TREE_VALUE (newargs
);
1818 if (oldargtype
== error_mark_node
|| newargtype
== error_mark_node
)
1821 oldargtype
= (TYPE_ATOMIC (oldargtype
)
1822 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype
),
1824 : TYPE_MAIN_VARIANT (oldargtype
));
1825 newargtype
= (TYPE_ATOMIC (newargtype
)
1826 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype
),
1828 : TYPE_MAIN_VARIANT (newargtype
));
1830 if (END_OF_ARGLIST (oldargtype
) && END_OF_ARGLIST (newargtype
))
1833 /* Reaching the end of just one list means the two decls don't
1834 agree on the number of arguments. */
1835 if (END_OF_ARGLIST (oldargtype
))
1837 error ("prototype for %q+D declares more arguments "
1838 "than previous old-style definition", newdecl
);
1841 else if (END_OF_ARGLIST (newargtype
))
1843 error ("prototype for %q+D declares fewer arguments "
1844 "than previous old-style definition", newdecl
);
1848 /* Type for passing arg must be consistent with that declared
1850 else if (!comptypes (oldargtype
, newargtype
))
1852 error ("prototype for %q+D declares argument %d"
1853 " with incompatible type",
1858 oldargs
= TREE_CHAIN (oldargs
);
1859 newargs
= TREE_CHAIN (newargs
);
1863 /* If we get here, no errors were found, but do issue a warning
1864 for this poor-style construct. */
1865 warning (0, "prototype for %q+D follows non-prototype definition",
1868 #undef END_OF_ARGLIST
1871 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1872 first in a pair of mismatched declarations, using the diagnostic
1875 locate_old_decl (tree decl
)
1877 if (TREE_CODE (decl
) == FUNCTION_DECL
&& fndecl_built_in_p (decl
)
1878 && !C_DECL_DECLARED_BUILTIN (decl
))
1880 else if (DECL_INITIAL (decl
))
1881 inform (input_location
, "previous definition of %q+D was here", decl
);
1882 else if (C_DECL_IMPLICIT (decl
))
1883 inform (input_location
, "previous implicit declaration of %q+D was here", decl
);
1885 inform (input_location
, "previous declaration of %q+D was here", decl
);
1888 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1889 Returns true if the caller should proceed to merge the two, false
1890 if OLDDECL should simply be discarded. As a side effect, issues
1891 all necessary diagnostics for invalid or poor-style combinations.
1892 If it returns true, writes the types of NEWDECL and OLDDECL to
1893 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1894 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1897 diagnose_mismatched_decls (tree newdecl
, tree olddecl
,
1898 tree
*newtypep
, tree
*oldtypep
)
1900 tree newtype
, oldtype
;
1903 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1904 && DECL_EXTERNAL (DECL))
1906 /* If we have error_mark_node for either decl or type, just discard
1907 the previous decl - we're in an error cascade already. */
1908 if (olddecl
== error_mark_node
|| newdecl
== error_mark_node
)
1910 *oldtypep
= oldtype
= TREE_TYPE (olddecl
);
1911 *newtypep
= newtype
= TREE_TYPE (newdecl
);
1912 if (oldtype
== error_mark_node
|| newtype
== error_mark_node
)
1915 /* Two different categories of symbol altogether. This is an error
1916 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1917 if (TREE_CODE (olddecl
) != TREE_CODE (newdecl
))
1919 if (!(TREE_CODE (olddecl
) == FUNCTION_DECL
1920 && fndecl_built_in_p (olddecl
)
1921 && !C_DECL_DECLARED_BUILTIN (olddecl
)))
1923 auto_diagnostic_group d
;
1924 error ("%q+D redeclared as different kind of symbol", newdecl
);
1925 locate_old_decl (olddecl
);
1927 else if (TREE_PUBLIC (newdecl
))
1928 warning (OPT_Wbuiltin_declaration_mismatch
,
1929 "built-in function %q+D declared as non-function",
1932 warning (OPT_Wshadow
, "declaration of %q+D shadows "
1933 "a built-in function", newdecl
);
1937 /* Enumerators have no linkage, so may only be declared once in a
1939 if (TREE_CODE (olddecl
) == CONST_DECL
)
1941 auto_diagnostic_group d
;
1942 error ("redeclaration of enumerator %q+D", newdecl
);
1943 locate_old_decl (olddecl
);
1947 bool pedwarned
= false;
1948 bool warned
= false;
1949 auto_diagnostic_group d
;
1951 if (!comptypes (oldtype
, newtype
))
1953 if (TREE_CODE (olddecl
) == FUNCTION_DECL
1954 && fndecl_built_in_p (olddecl
) && !C_DECL_DECLARED_BUILTIN (olddecl
))
1956 /* Accept "harmless" mismatches in function types such
1957 as missing qualifiers or pointer vs same size integer
1958 mismatches. This is for the ffs and fprintf builtins.
1959 However, with -Wextra in effect, diagnose return and
1960 argument types that are incompatible according to
1962 tree mismatch_expect
;
1963 unsigned mismatch_argno
;
1965 tree trytype
= match_builtin_function_types (newtype
, oldtype
,
1969 if (trytype
&& comptypes (newtype
, trytype
))
1970 *oldtypep
= oldtype
= trytype
;
1973 /* If types don't match for a built-in, throw away the
1974 built-in. No point in calling locate_old_decl here, it
1975 won't print anything. */
1977 = header_for_builtin_fn (DECL_FUNCTION_CODE (olddecl
));
1978 location_t loc
= DECL_SOURCE_LOCATION (newdecl
);
1979 if (warning_at (loc
, OPT_Wbuiltin_declaration_mismatch
,
1980 "conflicting types for built-in function %q+D; "
1985 /* Suggest the right header to include as the preferred
1986 solution rather than the spelling of the declaration. */
1987 rich_location
richloc (line_table
, loc
);
1988 maybe_add_include_fixit (&richloc
, header
, true);
1990 "%qD is declared in header %qs", olddecl
, header
);
1995 if (mismatch_expect
&& extra_warnings
)
1997 /* If types match only loosely, print a warning but accept
1998 the redeclaration. */
1999 location_t newloc
= DECL_SOURCE_LOCATION (newdecl
);
2001 warning_at (newloc
, OPT_Wbuiltin_declaration_mismatch
,
2002 "mismatch in argument %u type of built-in "
2003 "function %qD; expected %qT",
2004 mismatch_argno
, newdecl
, mismatch_expect
);
2006 warning_at (newloc
, OPT_Wbuiltin_declaration_mismatch
,
2007 "mismatch in return type of built-in "
2008 "function %qD; expected %qT",
2009 newdecl
, mismatch_expect
);
2012 else if (TREE_CODE (olddecl
) == FUNCTION_DECL
2013 && DECL_IS_BUILTIN (olddecl
))
2015 /* A conflicting function declaration for a predeclared
2016 function that isn't actually built in. Objective C uses
2017 these. The new declaration silently overrides everything
2018 but the volatility (i.e. noreturn) indication. See also
2019 below. FIXME: Make Objective C use normal builtins. */
2020 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
2023 /* Permit void foo (...) to match int foo (...) if the latter is
2024 the definition and implicit int was used. See
2025 c-torture/compile/920625-2.c. */
2026 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
&& DECL_INITIAL (newdecl
)
2027 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype
)) == void_type_node
2028 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype
)) == integer_type_node
2029 && C_FUNCTION_IMPLICIT_INT (newdecl
) && !DECL_INITIAL (olddecl
))
2031 pedwarned
= pedwarn (input_location
, 0,
2032 "conflicting types for %q+D", newdecl
);
2033 /* Make sure we keep void as the return type. */
2034 TREE_TYPE (newdecl
) = *newtypep
= newtype
= oldtype
;
2035 C_FUNCTION_IMPLICIT_INT (newdecl
) = 0;
2037 /* Permit void foo (...) to match an earlier call to foo (...) with
2038 no declared type (thus, implicitly int). */
2039 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
2040 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype
)) == void_type_node
2041 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype
)) == integer_type_node
2042 && C_DECL_IMPLICIT (olddecl
) && !DECL_INITIAL (olddecl
))
2044 pedwarned
= pedwarn (input_location
, 0,
2045 "conflicting types for %q+D", newdecl
);
2046 /* Make sure we keep void as the return type. */
2047 TREE_TYPE (olddecl
) = *oldtypep
= oldtype
= newtype
;
2051 int new_quals
= TYPE_QUALS (newtype
);
2052 int old_quals
= TYPE_QUALS (oldtype
);
2054 if (new_quals
!= old_quals
)
2056 addr_space_t new_addr
= DECODE_QUAL_ADDR_SPACE (new_quals
);
2057 addr_space_t old_addr
= DECODE_QUAL_ADDR_SPACE (old_quals
);
2058 if (new_addr
!= old_addr
)
2060 if (ADDR_SPACE_GENERIC_P (new_addr
))
2061 error ("conflicting named address spaces (generic vs %s) "
2063 c_addr_space_name (old_addr
), newdecl
);
2064 else if (ADDR_SPACE_GENERIC_P (old_addr
))
2065 error ("conflicting named address spaces (%s vs generic) "
2067 c_addr_space_name (new_addr
), newdecl
);
2069 error ("conflicting named address spaces (%s vs %s) "
2071 c_addr_space_name (new_addr
),
2072 c_addr_space_name (old_addr
),
2076 if (CLEAR_QUAL_ADDR_SPACE (new_quals
)
2077 != CLEAR_QUAL_ADDR_SPACE (old_quals
))
2078 error ("conflicting type qualifiers for %q+D", newdecl
);
2081 error ("conflicting types for %q+D", newdecl
);
2082 diagnose_arglist_conflict (newdecl
, olddecl
, newtype
, oldtype
);
2083 locate_old_decl (olddecl
);
2088 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2089 but silently ignore the redeclaration if either is in a system
2090 header. (Conflicting redeclarations were handled above.) This
2091 is allowed for C11 if the types are the same, not just
2093 if (TREE_CODE (newdecl
) == TYPE_DECL
)
2095 bool types_different
= false;
2096 int comptypes_result
;
2099 = comptypes_check_different_types (oldtype
, newtype
, &types_different
);
2101 if (comptypes_result
!= 1 || types_different
)
2103 error ("redefinition of typedef %q+D with different type", newdecl
);
2104 locate_old_decl (olddecl
);
2108 if (DECL_IN_SYSTEM_HEADER (newdecl
)
2109 || DECL_IN_SYSTEM_HEADER (olddecl
)
2110 || TREE_NO_WARNING (newdecl
)
2111 || TREE_NO_WARNING (olddecl
))
2112 return true; /* Allow OLDDECL to continue in use. */
2114 if (variably_modified_type_p (newtype
, NULL
))
2116 error ("redefinition of typedef %q+D with variably modified type",
2118 locate_old_decl (olddecl
);
2120 else if (pedwarn_c99 (input_location
, OPT_Wpedantic
,
2121 "redefinition of typedef %q+D", newdecl
))
2122 locate_old_decl (olddecl
);
2127 /* Function declarations can either be 'static' or 'extern' (no
2128 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2129 can never conflict with each other on account of linkage
2130 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2131 gnu89 mode permits two definitions if one is 'extern inline' and
2132 one is not. The non- extern-inline definition supersedes the
2133 extern-inline definition. */
2135 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2137 /* If you declare a built-in function name as static, or
2138 define the built-in with an old-style definition (so we
2139 can't validate the argument list) the built-in definition is
2140 overridden, but optionally warn this was a bad choice of name. */
2141 if (fndecl_built_in_p (olddecl
)
2142 && !C_DECL_DECLARED_BUILTIN (olddecl
))
2144 if (!TREE_PUBLIC (newdecl
)
2145 || (DECL_INITIAL (newdecl
)
2146 && !prototype_p (TREE_TYPE (newdecl
))))
2148 warning_at (DECL_SOURCE_LOCATION (newdecl
),
2149 OPT_Wshadow
, "declaration of %qD shadows "
2150 "a built-in function", newdecl
);
2151 /* Discard the old built-in function. */
2155 if (!prototype_p (TREE_TYPE (newdecl
)))
2157 /* Set for built-ins that take no arguments. */
2158 bool func_void_args
= false;
2159 if (tree at
= TYPE_ARG_TYPES (oldtype
))
2160 func_void_args
= VOID_TYPE_P (TREE_VALUE (at
));
2162 if (extra_warnings
&& !func_void_args
)
2163 warning_at (DECL_SOURCE_LOCATION (newdecl
),
2164 OPT_Wbuiltin_declaration_mismatch
,
2165 "declaration of built-in function %qD without "
2166 "a prototype; expected %qT",
2167 newdecl
, TREE_TYPE (olddecl
));
2171 if (DECL_INITIAL (newdecl
))
2173 if (DECL_INITIAL (olddecl
))
2175 /* If both decls are in the same TU and the new declaration
2176 isn't overriding an extern inline reject the new decl.
2177 In c99, no overriding is allowed in the same translation
2179 if ((!DECL_EXTERN_INLINE (olddecl
)
2180 || DECL_EXTERN_INLINE (newdecl
)
2181 || (!flag_gnu89_inline
2182 && (!DECL_DECLARED_INLINE_P (olddecl
)
2183 || !lookup_attribute ("gnu_inline",
2184 DECL_ATTRIBUTES (olddecl
)))
2185 && (!DECL_DECLARED_INLINE_P (newdecl
)
2186 || !lookup_attribute ("gnu_inline",
2187 DECL_ATTRIBUTES (newdecl
))))
2189 && same_translation_unit_p (newdecl
, olddecl
))
2191 auto_diagnostic_group d
;
2192 error ("redefinition of %q+D", newdecl
);
2193 locate_old_decl (olddecl
);
2198 /* If we have a prototype after an old-style function definition,
2199 the argument types must be checked specially. */
2200 else if (DECL_INITIAL (olddecl
)
2201 && !prototype_p (oldtype
) && prototype_p (newtype
)
2202 && TYPE_ACTUAL_ARG_TYPES (oldtype
))
2204 auto_diagnostic_group d
;
2205 if (!validate_proto_after_old_defn (newdecl
, newtype
, oldtype
))
2207 locate_old_decl (olddecl
);
2211 /* A non-static declaration (even an "extern") followed by a
2212 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2213 The same is true for a static forward declaration at block
2214 scope followed by a non-static declaration/definition at file
2215 scope. Static followed by non-static at the same scope is
2216 not undefined behavior, and is the most convenient way to get
2217 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2218 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2219 we do diagnose it if -Wtraditional. */
2220 if (TREE_PUBLIC (olddecl
) && !TREE_PUBLIC (newdecl
))
2222 /* Two exceptions to the rule. If olddecl is an extern
2223 inline, or a predeclared function that isn't actually
2224 built in, newdecl silently overrides olddecl. The latter
2225 occur only in Objective C; see also above. (FIXME: Make
2226 Objective C use normal builtins.) */
2227 if (!DECL_IS_BUILTIN (olddecl
)
2228 && !DECL_EXTERN_INLINE (olddecl
))
2230 auto_diagnostic_group d
;
2231 error ("static declaration of %q+D follows "
2232 "non-static declaration", newdecl
);
2233 locate_old_decl (olddecl
);
2237 else if (TREE_PUBLIC (newdecl
) && !TREE_PUBLIC (olddecl
))
2239 if (DECL_CONTEXT (olddecl
))
2241 auto_diagnostic_group d
;
2242 error ("non-static declaration of %q+D follows "
2243 "static declaration", newdecl
);
2244 locate_old_decl (olddecl
);
2247 else if (warn_traditional
)
2249 warned
|= warning (OPT_Wtraditional
,
2250 "non-static declaration of %q+D "
2251 "follows static declaration", newdecl
);
2255 /* Make sure gnu_inline attribute is either not present, or
2256 present on all inline decls. */
2257 if (DECL_DECLARED_INLINE_P (olddecl
)
2258 && DECL_DECLARED_INLINE_P (newdecl
))
2260 bool newa
= lookup_attribute ("gnu_inline",
2261 DECL_ATTRIBUTES (newdecl
)) != NULL
;
2262 bool olda
= lookup_attribute ("gnu_inline",
2263 DECL_ATTRIBUTES (olddecl
)) != NULL
;
2266 auto_diagnostic_group d
;
2267 error_at (input_location
, "%<gnu_inline%> attribute present on %q+D",
2268 newa
? newdecl
: olddecl
);
2269 error_at (DECL_SOURCE_LOCATION (newa
? olddecl
: newdecl
),
2274 else if (VAR_P (newdecl
))
2276 /* Only variables can be thread-local, and all declarations must
2277 agree on this property. */
2278 if (C_DECL_THREADPRIVATE_P (olddecl
) && !DECL_THREAD_LOCAL_P (newdecl
))
2280 /* Nothing to check. Since OLDDECL is marked threadprivate
2281 and NEWDECL does not have a thread-local attribute, we
2282 will merge the threadprivate attribute into NEWDECL. */
2285 else if (DECL_THREAD_LOCAL_P (newdecl
) != DECL_THREAD_LOCAL_P (olddecl
))
2287 auto_diagnostic_group d
;
2288 if (DECL_THREAD_LOCAL_P (newdecl
))
2289 error ("thread-local declaration of %q+D follows "
2290 "non-thread-local declaration", newdecl
);
2292 error ("non-thread-local declaration of %q+D follows "
2293 "thread-local declaration", newdecl
);
2295 locate_old_decl (olddecl
);
2299 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2300 if (DECL_INITIAL (newdecl
) && DECL_INITIAL (olddecl
))
2302 auto_diagnostic_group d
;
2303 error ("redefinition of %q+D", newdecl
);
2304 locate_old_decl (olddecl
);
2308 /* Objects declared at file scope: if the first declaration had
2309 external linkage (even if it was an external reference) the
2310 second must have external linkage as well, or the behavior is
2311 undefined. If the first declaration had internal linkage, then
2312 the second must too, or else be an external reference (in which
2313 case the composite declaration still has internal linkage).
2314 As for function declarations, we warn about the static-then-
2315 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2316 if (DECL_FILE_SCOPE_P (newdecl
)
2317 && TREE_PUBLIC (newdecl
) != TREE_PUBLIC (olddecl
))
2319 if (DECL_EXTERNAL (newdecl
))
2321 if (!DECL_FILE_SCOPE_P (olddecl
))
2323 auto_diagnostic_group d
;
2324 error ("extern declaration of %q+D follows "
2325 "declaration with no linkage", newdecl
);
2326 locate_old_decl (olddecl
);
2329 else if (warn_traditional
)
2331 warned
|= warning (OPT_Wtraditional
,
2332 "non-static declaration of %q+D "
2333 "follows static declaration", newdecl
);
2338 auto_diagnostic_group d
;
2339 if (TREE_PUBLIC (newdecl
))
2340 error ("non-static declaration of %q+D follows "
2341 "static declaration", newdecl
);
2343 error ("static declaration of %q+D follows "
2344 "non-static declaration", newdecl
);
2346 locate_old_decl (olddecl
);
2350 /* Two objects with the same name declared at the same block
2351 scope must both be external references (6.7p3). */
2352 else if (!DECL_FILE_SCOPE_P (newdecl
))
2354 if (DECL_EXTERNAL (newdecl
))
2356 /* Extern with initializer at block scope, which will
2357 already have received an error. */
2359 else if (DECL_EXTERNAL (olddecl
))
2361 auto_diagnostic_group d
;
2362 error ("declaration of %q+D with no linkage follows "
2363 "extern declaration", newdecl
);
2364 locate_old_decl (olddecl
);
2368 auto_diagnostic_group d
;
2369 error ("redeclaration of %q+D with no linkage", newdecl
);
2370 locate_old_decl (olddecl
);
2376 /* C++ does not permit a decl to appear multiple times at file
2379 && DECL_FILE_SCOPE_P (newdecl
)
2380 && !DECL_EXTERNAL (newdecl
)
2381 && !DECL_EXTERNAL (olddecl
))
2382 warned
|= warning_at (DECL_SOURCE_LOCATION (newdecl
),
2384 ("duplicate declaration of %qD is "
2390 /* All decls must agree on a visibility. */
2391 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl
), TS_DECL_WITH_VIS
)
2392 && DECL_VISIBILITY_SPECIFIED (newdecl
) && DECL_VISIBILITY_SPECIFIED (olddecl
)
2393 && DECL_VISIBILITY (newdecl
) != DECL_VISIBILITY (olddecl
))
2395 warned
|= warning (0, "redeclaration of %q+D with different visibility "
2396 "(old visibility preserved)", newdecl
);
2399 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2400 warned
|= diagnose_mismatched_attributes (olddecl
, newdecl
);
2401 else /* PARM_DECL, VAR_DECL */
2403 /* Redeclaration of a parameter is a constraint violation (this is
2404 not explicitly stated, but follows from C99 6.7p3 [no more than
2405 one declaration of the same identifier with no linkage in the
2406 same scope, except type tags] and 6.2.2p6 [parameters have no
2407 linkage]). We must check for a forward parameter declaration,
2408 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2409 an extension, the mandatory diagnostic for which is handled by
2410 mark_forward_parm_decls. */
2412 if (TREE_CODE (newdecl
) == PARM_DECL
2413 && (!TREE_ASM_WRITTEN (olddecl
) || TREE_ASM_WRITTEN (newdecl
)))
2415 auto_diagnostic_group d
;
2416 error ("redefinition of parameter %q+D", newdecl
);
2417 locate_old_decl (olddecl
);
2422 /* Optional warning for completely redundant decls. */
2423 if (!warned
&& !pedwarned
2424 && warn_redundant_decls
2425 /* Don't warn about a function declaration followed by a
2427 && !(TREE_CODE (newdecl
) == FUNCTION_DECL
2428 && DECL_INITIAL (newdecl
) && !DECL_INITIAL (olddecl
))
2429 /* Don't warn about redundant redeclarations of builtins. */
2430 && !(TREE_CODE (newdecl
) == FUNCTION_DECL
2431 && !fndecl_built_in_p (newdecl
)
2432 && fndecl_built_in_p (olddecl
)
2433 && !C_DECL_DECLARED_BUILTIN (olddecl
))
2434 /* Don't warn about an extern followed by a definition. */
2435 && !(DECL_EXTERNAL (olddecl
) && !DECL_EXTERNAL (newdecl
))
2436 /* Don't warn about forward parameter decls. */
2437 && !(TREE_CODE (newdecl
) == PARM_DECL
2438 && TREE_ASM_WRITTEN (olddecl
) && !TREE_ASM_WRITTEN (newdecl
))
2439 /* Don't warn about a variable definition following a declaration. */
2440 && !(VAR_P (newdecl
)
2441 && DECL_INITIAL (newdecl
) && !DECL_INITIAL (olddecl
)))
2443 warned
= warning (OPT_Wredundant_decls
, "redundant redeclaration of %q+D",
2447 /* Report location of previous decl/defn. */
2448 if (warned
|| pedwarned
)
2449 locate_old_decl (olddecl
);
2451 #undef DECL_EXTERN_INLINE
2456 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2457 consistent with OLDDECL, but carries new information. Merge the
2458 new information into OLDDECL. This function issues no
2462 merge_decls (tree newdecl
, tree olddecl
, tree newtype
, tree oldtype
)
2464 bool new_is_definition
= (TREE_CODE (newdecl
) == FUNCTION_DECL
2465 && DECL_INITIAL (newdecl
) != NULL_TREE
);
2466 bool new_is_prototype
= (TREE_CODE (newdecl
) == FUNCTION_DECL
2467 && prototype_p (TREE_TYPE (newdecl
)));
2468 bool old_is_prototype
= (TREE_CODE (olddecl
) == FUNCTION_DECL
2469 && prototype_p (TREE_TYPE (olddecl
)));
2471 /* For real parm decl following a forward decl, rechain the old decl
2472 in its new location and clear TREE_ASM_WRITTEN (it's not a
2473 forward decl anymore). */
2474 if (TREE_CODE (newdecl
) == PARM_DECL
2475 && TREE_ASM_WRITTEN (olddecl
) && !TREE_ASM_WRITTEN (newdecl
))
2477 struct c_binding
*b
, **here
;
2479 for (here
= ¤t_scope
->bindings
; *here
; here
= &(*here
)->prev
)
2480 if ((*here
)->decl
== olddecl
)
2487 b
->prev
= current_scope
->bindings
;
2488 current_scope
->bindings
= b
;
2490 TREE_ASM_WRITTEN (olddecl
) = 0;
2493 DECL_ATTRIBUTES (newdecl
)
2494 = targetm
.merge_decl_attributes (olddecl
, newdecl
);
2496 /* For typedefs use the old type, as the new type's DECL_NAME points
2497 at newdecl, which will be ggc_freed. */
2498 if (TREE_CODE (newdecl
) == TYPE_DECL
)
2500 /* But NEWTYPE might have an attribute, honor that. */
2504 if (TYPE_USER_ALIGN (tem
))
2506 if (TYPE_ALIGN (tem
) > TYPE_ALIGN (newtype
))
2507 SET_TYPE_ALIGN (newtype
, TYPE_ALIGN (tem
));
2508 TYPE_USER_ALIGN (newtype
) = true;
2511 /* And remove the new type from the variants list. */
2512 if (TYPE_NAME (TREE_TYPE (newdecl
)) == newdecl
)
2514 tree remove
= TREE_TYPE (newdecl
);
2515 if (TYPE_MAIN_VARIANT (remove
) == remove
)
2516 gcc_assert (TYPE_NEXT_VARIANT (remove
) == NULL_TREE
);
2518 for (tree t
= TYPE_MAIN_VARIANT (remove
); ;
2519 t
= TYPE_NEXT_VARIANT (t
))
2520 if (TYPE_NEXT_VARIANT (t
) == remove
)
2522 TYPE_NEXT_VARIANT (t
) = TYPE_NEXT_VARIANT (remove
);
2528 /* Merge the data types specified in the two decls. */
2530 = TREE_TYPE (olddecl
)
2531 = composite_type (newtype
, oldtype
);
2533 /* Lay the type out, unless already done. */
2534 if (!comptypes (oldtype
, TREE_TYPE (newdecl
)))
2536 if (TREE_TYPE (newdecl
) != error_mark_node
)
2537 layout_type (TREE_TYPE (newdecl
));
2538 if (TREE_CODE (newdecl
) != FUNCTION_DECL
2539 && TREE_CODE (newdecl
) != TYPE_DECL
2540 && TREE_CODE (newdecl
) != CONST_DECL
)
2541 layout_decl (newdecl
, 0);
2545 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2546 DECL_SIZE (newdecl
) = DECL_SIZE (olddecl
);
2547 DECL_SIZE_UNIT (newdecl
) = DECL_SIZE_UNIT (olddecl
);
2548 SET_DECL_MODE (newdecl
, DECL_MODE (olddecl
));
2549 if (DECL_ALIGN (olddecl
) > DECL_ALIGN (newdecl
))
2551 SET_DECL_ALIGN (newdecl
, DECL_ALIGN (olddecl
));
2552 DECL_USER_ALIGN (newdecl
) |= DECL_USER_ALIGN (olddecl
);
2554 if (DECL_WARN_IF_NOT_ALIGN (olddecl
)
2555 > DECL_WARN_IF_NOT_ALIGN (newdecl
))
2556 SET_DECL_WARN_IF_NOT_ALIGN (newdecl
,
2557 DECL_WARN_IF_NOT_ALIGN (olddecl
));
2560 /* Keep the old rtl since we can safely use it. */
2561 if (HAS_RTL_P (olddecl
))
2562 COPY_DECL_RTL (olddecl
, newdecl
);
2564 /* Merge the type qualifiers. */
2565 if (TREE_READONLY (newdecl
))
2566 TREE_READONLY (olddecl
) = 1;
2568 if (TREE_THIS_VOLATILE (newdecl
))
2569 TREE_THIS_VOLATILE (olddecl
) = 1;
2571 /* Merge deprecatedness. */
2572 if (TREE_DEPRECATED (newdecl
))
2573 TREE_DEPRECATED (olddecl
) = 1;
2575 /* If a decl is in a system header and the other isn't, keep the one on the
2576 system header. Otherwise, keep source location of definition rather than
2577 declaration and of prototype rather than non-prototype unless that
2578 prototype is built-in. */
2579 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl
), TS_DECL_WITH_VIS
)
2580 && DECL_IN_SYSTEM_HEADER (olddecl
)
2581 && !DECL_IN_SYSTEM_HEADER (newdecl
) )
2582 DECL_SOURCE_LOCATION (newdecl
) = DECL_SOURCE_LOCATION (olddecl
);
2583 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl
), TS_DECL_WITH_VIS
)
2584 && DECL_IN_SYSTEM_HEADER (newdecl
)
2585 && !DECL_IN_SYSTEM_HEADER (olddecl
))
2586 DECL_SOURCE_LOCATION (olddecl
) = DECL_SOURCE_LOCATION (newdecl
);
2587 else if ((DECL_INITIAL (newdecl
) == NULL_TREE
2588 && DECL_INITIAL (olddecl
) != NULL_TREE
)
2589 || (old_is_prototype
&& !new_is_prototype
2590 && !C_DECL_BUILTIN_PROTOTYPE (olddecl
)))
2591 DECL_SOURCE_LOCATION (newdecl
) = DECL_SOURCE_LOCATION (olddecl
);
2593 /* Merge the initialization information. */
2594 if (DECL_INITIAL (newdecl
) == NULL_TREE
)
2595 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
2597 /* Merge the threadprivate attribute. */
2598 if (VAR_P (olddecl
) && C_DECL_THREADPRIVATE_P (olddecl
))
2599 C_DECL_THREADPRIVATE_P (newdecl
) = 1;
2601 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl
), TS_DECL_WITH_VIS
))
2603 /* Copy the assembler name.
2604 Currently, it can only be defined in the prototype. */
2605 COPY_DECL_ASSEMBLER_NAME (olddecl
, newdecl
);
2607 /* Use visibility of whichever declaration had it specified */
2608 if (DECL_VISIBILITY_SPECIFIED (olddecl
))
2610 DECL_VISIBILITY (newdecl
) = DECL_VISIBILITY (olddecl
);
2611 DECL_VISIBILITY_SPECIFIED (newdecl
) = 1;
2614 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2616 DECL_STATIC_CONSTRUCTOR(newdecl
) |= DECL_STATIC_CONSTRUCTOR(olddecl
);
2617 DECL_STATIC_DESTRUCTOR (newdecl
) |= DECL_STATIC_DESTRUCTOR (olddecl
);
2618 DECL_NO_LIMIT_STACK (newdecl
) |= DECL_NO_LIMIT_STACK (olddecl
);
2619 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl
)
2620 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl
);
2621 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
2622 DECL_IS_MALLOC (newdecl
) |= DECL_IS_MALLOC (olddecl
);
2623 DECL_IS_OPERATOR_NEW (newdecl
) |= DECL_IS_OPERATOR_NEW (olddecl
);
2624 TREE_READONLY (newdecl
) |= TREE_READONLY (olddecl
);
2625 DECL_PURE_P (newdecl
) |= DECL_PURE_P (olddecl
);
2626 DECL_IS_NOVOPS (newdecl
) |= DECL_IS_NOVOPS (olddecl
);
2629 /* Merge the storage class information. */
2630 merge_weak (newdecl
, olddecl
);
2632 /* For functions, static overrides non-static. */
2633 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2635 TREE_PUBLIC (newdecl
) &= TREE_PUBLIC (olddecl
);
2636 /* This is since we don't automatically
2637 copy the attributes of NEWDECL into OLDDECL. */
2638 TREE_PUBLIC (olddecl
) = TREE_PUBLIC (newdecl
);
2639 /* If this clears `static', clear it in the identifier too. */
2640 if (!TREE_PUBLIC (olddecl
))
2641 TREE_PUBLIC (DECL_NAME (olddecl
)) = 0;
2645 /* In c99, 'extern' declaration before (or after) 'inline' means this
2646 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2648 if (TREE_CODE (newdecl
) == FUNCTION_DECL
2649 && !flag_gnu89_inline
2650 && (DECL_DECLARED_INLINE_P (newdecl
)
2651 || DECL_DECLARED_INLINE_P (olddecl
))
2652 && (!DECL_DECLARED_INLINE_P (newdecl
)
2653 || !DECL_DECLARED_INLINE_P (olddecl
)
2654 || !DECL_EXTERNAL (olddecl
))
2655 && DECL_EXTERNAL (newdecl
)
2656 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl
))
2657 && !current_function_decl
)
2658 DECL_EXTERNAL (newdecl
) = 0;
2660 /* An inline definition following a static declaration is not
2662 if (new_is_definition
2663 && (DECL_DECLARED_INLINE_P (newdecl
)
2664 || DECL_DECLARED_INLINE_P (olddecl
))
2665 && !TREE_PUBLIC (olddecl
))
2666 DECL_EXTERNAL (newdecl
) = 0;
2668 if (DECL_EXTERNAL (newdecl
))
2670 TREE_STATIC (newdecl
) = TREE_STATIC (olddecl
);
2671 DECL_EXTERNAL (newdecl
) = DECL_EXTERNAL (olddecl
);
2673 /* An extern decl does not override previous storage class. */
2674 TREE_PUBLIC (newdecl
) = TREE_PUBLIC (olddecl
);
2675 if (!DECL_EXTERNAL (newdecl
))
2677 DECL_CONTEXT (newdecl
) = DECL_CONTEXT (olddecl
);
2678 DECL_COMMON (newdecl
) = DECL_COMMON (olddecl
);
2683 TREE_STATIC (olddecl
) = TREE_STATIC (newdecl
);
2684 TREE_PUBLIC (olddecl
) = TREE_PUBLIC (newdecl
);
2687 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2689 /* If we're redefining a function previously defined as extern
2690 inline, make sure we emit debug info for the inline before we
2691 throw it away, in case it was inlined into a function that
2692 hasn't been written out yet. */
2693 if (new_is_definition
&& DECL_INITIAL (olddecl
))
2694 /* The new defn must not be inline. */
2695 DECL_UNINLINABLE (newdecl
) = 1;
2698 /* If either decl says `inline', this fn is inline, unless
2699 its definition was passed already. */
2700 if (DECL_DECLARED_INLINE_P (newdecl
)
2701 || DECL_DECLARED_INLINE_P (olddecl
))
2702 DECL_DECLARED_INLINE_P (newdecl
) = 1;
2704 DECL_UNINLINABLE (newdecl
) = DECL_UNINLINABLE (olddecl
)
2705 = (DECL_UNINLINABLE (newdecl
) || DECL_UNINLINABLE (olddecl
));
2707 DECL_DISREGARD_INLINE_LIMITS (newdecl
)
2708 = DECL_DISREGARD_INLINE_LIMITS (olddecl
)
2709 = (DECL_DISREGARD_INLINE_LIMITS (newdecl
)
2710 || DECL_DISREGARD_INLINE_LIMITS (olddecl
));
2713 if (fndecl_built_in_p (olddecl
))
2715 /* If redeclaring a builtin function, it stays built in.
2716 But it gets tagged as having been declared. */
2717 DECL_BUILT_IN_CLASS (newdecl
) = DECL_BUILT_IN_CLASS (olddecl
);
2718 DECL_FUNCTION_CODE (newdecl
) = DECL_FUNCTION_CODE (olddecl
);
2719 C_DECL_DECLARED_BUILTIN (newdecl
) = 1;
2720 if (new_is_prototype
)
2722 C_DECL_BUILTIN_PROTOTYPE (newdecl
) = 0;
2723 if (DECL_BUILT_IN_CLASS (newdecl
) == BUILT_IN_NORMAL
)
2725 enum built_in_function fncode
= DECL_FUNCTION_CODE (newdecl
);
2728 /* If a compatible prototype of these builtin functions
2729 is seen, assume the runtime implements it with the
2730 expected semantics. */
2731 case BUILT_IN_STPCPY
:
2732 if (builtin_decl_explicit_p (fncode
))
2733 set_builtin_decl_implicit_p (fncode
, true);
2736 if (builtin_decl_explicit_p (fncode
))
2737 set_builtin_decl_declared_p (fncode
, true);
2741 copy_attributes_to_builtin (newdecl
);
2745 C_DECL_BUILTIN_PROTOTYPE (newdecl
)
2746 = C_DECL_BUILTIN_PROTOTYPE (olddecl
);
2749 /* Preserve function specific target and optimization options */
2750 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl
)
2751 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl
))
2752 DECL_FUNCTION_SPECIFIC_TARGET (newdecl
)
2753 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl
);
2755 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
)
2756 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
))
2757 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
)
2758 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
);
2760 /* Also preserve various other info from the definition. */
2761 if (!new_is_definition
)
2764 DECL_RESULT (newdecl
) = DECL_RESULT (olddecl
);
2765 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
2766 DECL_STRUCT_FUNCTION (newdecl
) = DECL_STRUCT_FUNCTION (olddecl
);
2767 DECL_SAVED_TREE (newdecl
) = DECL_SAVED_TREE (olddecl
);
2768 DECL_ARGUMENTS (newdecl
) = copy_list (DECL_ARGUMENTS (olddecl
));
2769 for (t
= DECL_ARGUMENTS (newdecl
); t
; t
= DECL_CHAIN (t
))
2770 DECL_CONTEXT (t
) = newdecl
;
2772 /* See if we've got a function to instantiate from. */
2773 if (DECL_SAVED_TREE (olddecl
))
2774 DECL_ABSTRACT_ORIGIN (newdecl
)
2775 = DECL_ABSTRACT_ORIGIN (olddecl
);
2779 /* Merge the USED information. */
2780 if (TREE_USED (olddecl
))
2781 TREE_USED (newdecl
) = 1;
2782 else if (TREE_USED (newdecl
))
2783 TREE_USED (olddecl
) = 1;
2784 if (VAR_P (olddecl
) || TREE_CODE (olddecl
) == PARM_DECL
)
2785 DECL_READ_P (newdecl
) |= DECL_READ_P (olddecl
);
2786 if (DECL_PRESERVE_P (olddecl
))
2787 DECL_PRESERVE_P (newdecl
) = 1;
2788 else if (DECL_PRESERVE_P (newdecl
))
2789 DECL_PRESERVE_P (olddecl
) = 1;
2791 /* Merge DECL_COMMON */
2792 if (VAR_P (olddecl
) && VAR_P (newdecl
)
2793 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl
))
2794 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl
)))
2795 DECL_COMMON (newdecl
) = DECL_COMMON (newdecl
) && DECL_COMMON (olddecl
);
2797 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2798 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2799 DECL_ARGUMENTS (if appropriate). */
2801 unsigned olddecl_uid
= DECL_UID (olddecl
);
2802 tree olddecl_context
= DECL_CONTEXT (olddecl
);
2803 tree olddecl_arguments
= NULL
;
2804 if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
2805 olddecl_arguments
= DECL_ARGUMENTS (olddecl
);
2807 memcpy ((char *) olddecl
+ sizeof (struct tree_common
),
2808 (char *) newdecl
+ sizeof (struct tree_common
),
2809 sizeof (struct tree_decl_common
) - sizeof (struct tree_common
));
2810 DECL_USER_ALIGN (olddecl
) = DECL_USER_ALIGN (newdecl
);
2811 switch (TREE_CODE (olddecl
))
2816 struct symtab_node
*snode
= olddecl
->decl_with_vis
.symtab_node
;
2818 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2819 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2820 tree_code_size (TREE_CODE (olddecl
)) - sizeof (struct tree_decl_common
));
2821 olddecl
->decl_with_vis
.symtab_node
= snode
;
2823 if ((DECL_EXTERNAL (olddecl
)
2824 || TREE_PUBLIC (olddecl
)
2825 || TREE_STATIC (olddecl
))
2826 && DECL_SECTION_NAME (newdecl
) != NULL
)
2827 set_decl_section_name (olddecl
, DECL_SECTION_NAME (newdecl
));
2829 /* This isn't quite correct for something like
2830 int __thread x attribute ((tls_model ("local-exec")));
2831 extern int __thread x;
2832 as we'll lose the "local-exec" model. */
2833 if (VAR_P (olddecl
) && DECL_THREAD_LOCAL_P (newdecl
))
2834 set_decl_tls_model (olddecl
, DECL_TLS_MODEL (newdecl
));
2844 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2845 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2846 tree_code_size (TREE_CODE (olddecl
)) - sizeof (struct tree_decl_common
));
2851 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2852 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2853 sizeof (struct tree_decl_non_common
) - sizeof (struct tree_decl_common
));
2855 DECL_UID (olddecl
) = olddecl_uid
;
2856 DECL_CONTEXT (olddecl
) = olddecl_context
;
2857 if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
2858 DECL_ARGUMENTS (olddecl
) = olddecl_arguments
;
2861 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2862 so that encode_section_info has a chance to look at the new decl
2863 flags and attributes. */
2864 if (DECL_RTL_SET_P (olddecl
)
2865 && (TREE_CODE (olddecl
) == FUNCTION_DECL
2866 || (VAR_P (olddecl
) && TREE_STATIC (olddecl
))))
2867 make_decl_rtl (olddecl
);
2870 /* Handle when a new declaration NEWDECL has the same name as an old
2871 one OLDDECL in the same binding contour. Prints an error message
2874 If safely possible, alter OLDDECL to look like NEWDECL, and return
2875 true. Otherwise, return false. */
2878 duplicate_decls (tree newdecl
, tree olddecl
)
2880 tree newtype
= NULL
, oldtype
= NULL
;
2882 if (!diagnose_mismatched_decls (newdecl
, olddecl
, &newtype
, &oldtype
))
2884 /* Avoid `unused variable' and other warnings for OLDDECL. */
2885 TREE_NO_WARNING (olddecl
) = 1;
2889 merge_decls (newdecl
, olddecl
, newtype
, oldtype
);
2891 /* The NEWDECL will no longer be needed.
2893 Before releasing the node, be sure to remove function from symbol
2894 table that might have been inserted there to record comdat group.
2895 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2896 structure is shared in between NEWDECL and OLDECL. */
2897 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2898 DECL_STRUCT_FUNCTION (newdecl
) = NULL
;
2899 if (VAR_OR_FUNCTION_DECL_P (newdecl
))
2901 struct symtab_node
*snode
= symtab_node::get (newdecl
);
2910 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2912 warn_if_shadowing (tree new_decl
)
2914 struct c_binding
*b
;
2916 /* Shadow warnings wanted? */
2918 || warn_shadow_local
2919 || warn_shadow_compatible_local
)
2920 /* No shadow warnings for internally generated vars. */
2921 || DECL_IS_BUILTIN (new_decl
))
2924 /* Is anything being shadowed? Invisible decls do not count. */
2925 for (b
= I_SYMBOL_BINDING (DECL_NAME (new_decl
)); b
; b
= b
->shadowed
)
2926 if (b
->decl
&& b
->decl
!= new_decl
&& !b
->invisible
2927 && (b
->decl
== error_mark_node
2928 || diagnostic_report_warnings_p (global_dc
,
2929 DECL_SOURCE_LOCATION (b
->decl
))))
2931 tree old_decl
= b
->decl
;
2933 if (old_decl
== error_mark_node
)
2935 warning (OPT_Wshadow
, "declaration of %q+D shadows previous "
2936 "non-variable", new_decl
);
2940 bool warned
= false;
2941 auto_diagnostic_group d
;
2942 if (TREE_CODE (old_decl
) == PARM_DECL
)
2944 enum opt_code warning_code
;
2946 /* If '-Wshadow=compatible-local' is specified without other
2947 -Wshadow= flags, we will warn only when the types of the
2948 shadowing variable (i.e. new_decl) and the shadowed variable
2949 (old_decl) are compatible. */
2951 warning_code
= OPT_Wshadow
;
2952 else if (comptypes (TREE_TYPE (old_decl
), TREE_TYPE (new_decl
)))
2953 warning_code
= OPT_Wshadow_compatible_local
;
2955 warning_code
= OPT_Wshadow_local
;
2956 warned
= warning_at (DECL_SOURCE_LOCATION (new_decl
), warning_code
,
2957 "declaration of %qD shadows a parameter",
2960 else if (DECL_FILE_SCOPE_P (old_decl
))
2962 /* Do not warn if a variable shadows a function, unless
2963 the variable is a function or a pointer-to-function. */
2964 if (TREE_CODE (old_decl
) == FUNCTION_DECL
2965 && TREE_CODE (new_decl
) != FUNCTION_DECL
2966 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl
)))
2969 warned
= warning_at (DECL_SOURCE_LOCATION (new_decl
), OPT_Wshadow
,
2970 "declaration of %qD shadows a global "
2974 else if (TREE_CODE (old_decl
) == FUNCTION_DECL
2975 && fndecl_built_in_p (old_decl
))
2977 warning (OPT_Wshadow
, "declaration of %q+D shadows "
2978 "a built-in function", new_decl
);
2983 enum opt_code warning_code
;
2985 /* If '-Wshadow=compatible-local' is specified without other
2986 -Wshadow= flags, we will warn only when the types of the
2987 shadowing variable (i.e. new_decl) and the shadowed variable
2988 (old_decl) are compatible. */
2990 warning_code
= OPT_Wshadow
;
2991 else if (comptypes (TREE_TYPE (old_decl
), TREE_TYPE (new_decl
)))
2992 warning_code
= OPT_Wshadow_compatible_local
;
2994 warning_code
= OPT_Wshadow_local
;
2995 warned
= warning_at (DECL_SOURCE_LOCATION (new_decl
), warning_code
,
2996 "declaration of %qD shadows a previous local",
3001 inform (DECL_SOURCE_LOCATION (old_decl
),
3002 "shadowed declaration is here");
3008 /* Record a decl-node X as belonging to the current lexical scope.
3009 Check for errors (such as an incompatible declaration for the same
3010 name already seen in the same scope).
3012 Returns either X or an old decl for the same name.
3013 If an old decl is returned, it may have been smashed
3014 to agree with what X says. */
3019 tree name
= DECL_NAME (x
);
3020 struct c_scope
*scope
= current_scope
;
3021 struct c_binding
*b
;
3022 bool nested
= false;
3023 location_t locus
= DECL_SOURCE_LOCATION (x
);
3025 /* Must set DECL_CONTEXT for everything not at file scope or
3026 DECL_FILE_SCOPE_P won't work. Local externs don't count
3027 unless they have initializers (which generate code). */
3028 if (current_function_decl
3029 && (!VAR_OR_FUNCTION_DECL_P (x
)
3030 || DECL_INITIAL (x
) || !DECL_EXTERNAL (x
)))
3031 DECL_CONTEXT (x
) = current_function_decl
;
3033 /* Anonymous decls are just inserted in the scope. */
3036 bind (name
, x
, scope
, /*invisible=*/false, /*nested=*/false,
3041 /* First, see if there is another declaration with the same name in
3042 the current scope. If there is, duplicate_decls may do all the
3043 work for us. If duplicate_decls returns false, that indicates
3044 two incompatible decls in the same scope; we are to silently
3045 replace the old one (duplicate_decls has issued all appropriate
3046 diagnostics). In particular, we should not consider possible
3047 duplicates in the external scope, or shadowing. */
3048 b
= I_SYMBOL_BINDING (name
);
3049 if (b
&& B_IN_SCOPE (b
, scope
))
3051 struct c_binding
*b_ext
, *b_use
;
3052 tree type
= TREE_TYPE (x
);
3053 tree visdecl
= b
->decl
;
3054 tree vistype
= TREE_TYPE (visdecl
);
3055 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
3056 && COMPLETE_TYPE_P (TREE_TYPE (x
)))
3057 b
->inner_comp
= false;
3060 /* If this is an external linkage declaration, we should check
3061 for compatibility with the type in the external scope before
3062 setting the type at this scope based on the visible
3063 information only. */
3064 if (TREE_PUBLIC (x
) && TREE_PUBLIC (visdecl
))
3066 while (b_ext
&& !B_IN_EXTERNAL_SCOPE (b_ext
))
3067 b_ext
= b_ext
->shadowed
;
3072 TREE_TYPE (b_use
->decl
) = b_use
->u
.type
;
3075 if (duplicate_decls (x
, b_use
->decl
))
3079 /* Save the updated type in the external scope and
3080 restore the proper type for this scope. */
3082 if (comptypes (vistype
, type
))
3083 thistype
= composite_type (vistype
, type
);
3085 thistype
= TREE_TYPE (b_use
->decl
);
3086 b_use
->u
.type
= TREE_TYPE (b_use
->decl
);
3087 if (TREE_CODE (b_use
->decl
) == FUNCTION_DECL
3088 && fndecl_built_in_p (b_use
->decl
))
3090 = build_type_attribute_variant (thistype
,
3093 TREE_TYPE (b_use
->decl
) = thistype
;
3098 goto skip_external_and_shadow_checks
;
3101 /* All declarations with external linkage, and all external
3102 references, go in the external scope, no matter what scope is
3103 current. However, the binding in that scope is ignored for
3104 purposes of normal name lookup. A separate binding structure is
3105 created in the requested scope; this governs the normal
3106 visibility of the symbol.
3108 The binding in the externals scope is used exclusively for
3109 detecting duplicate declarations of the same object, no matter
3110 what scope they are in; this is what we do here. (C99 6.2.7p2:
3111 All declarations that refer to the same object or function shall
3112 have compatible type; otherwise, the behavior is undefined.) */
3113 if (DECL_EXTERNAL (x
) || scope
== file_scope
)
3115 tree type
= TREE_TYPE (x
);
3116 tree vistype
= NULL_TREE
;
3117 tree visdecl
= NULL_TREE
;
3118 bool type_saved
= false;
3119 if (b
&& !B_IN_EXTERNAL_SCOPE (b
)
3120 && VAR_OR_FUNCTION_DECL_P (b
->decl
)
3121 && DECL_FILE_SCOPE_P (b
->decl
))
3124 vistype
= TREE_TYPE (visdecl
);
3126 if (scope
!= file_scope
3127 && !DECL_IN_SYSTEM_HEADER (x
))
3128 warning_at (locus
, OPT_Wnested_externs
,
3129 "nested extern declaration of %qD", x
);
3131 while (b
&& !B_IN_EXTERNAL_SCOPE (b
))
3133 /* If this decl might be modified, save its type. This is
3134 done here rather than when the decl is first bound
3135 because the type may change after first binding, through
3136 being completed or through attributes being added. If we
3137 encounter multiple such decls, only the first should have
3138 its type saved; the others will already have had their
3139 proper types saved and the types will not have changed as
3140 their scopes will not have been re-entered. */
3141 if (DECL_P (b
->decl
) && DECL_FILE_SCOPE_P (b
->decl
) && !type_saved
)
3143 b
->u
.type
= TREE_TYPE (b
->decl
);
3146 if (B_IN_FILE_SCOPE (b
)
3148 && TREE_STATIC (b
->decl
)
3149 && TREE_CODE (TREE_TYPE (b
->decl
)) == ARRAY_TYPE
3150 && !TYPE_DOMAIN (TREE_TYPE (b
->decl
))
3151 && TREE_CODE (type
) == ARRAY_TYPE
3152 && TYPE_DOMAIN (type
)
3153 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
3154 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))
3156 /* Array type completed in inner scope, which should be
3157 diagnosed if the completion does not have size 1 and
3158 it does not get completed in the file scope. */
3159 b
->inner_comp
= true;
3164 /* If a matching external declaration has been found, set its
3165 type to the composite of all the types of that declaration.
3166 After the consistency checks, it will be reset to the
3167 composite of the visible types only. */
3168 if (b
&& (TREE_PUBLIC (x
) || same_translation_unit_p (x
, b
->decl
))
3170 TREE_TYPE (b
->decl
) = b
->u
.type
;
3172 /* The point of the same_translation_unit_p check here is,
3173 we want to detect a duplicate decl for a construct like
3174 foo() { extern bar(); } ... static bar(); but not if
3175 they are in different translation units. In any case,
3176 the static does not go in the externals scope. */
3178 && (TREE_PUBLIC (x
) || same_translation_unit_p (x
, b
->decl
))
3179 && duplicate_decls (x
, b
->decl
))
3184 if (comptypes (vistype
, type
))
3185 thistype
= composite_type (vistype
, type
);
3187 thistype
= TREE_TYPE (b
->decl
);
3191 b
->u
.type
= TREE_TYPE (b
->decl
);
3192 if (TREE_CODE (b
->decl
) == FUNCTION_DECL
3193 && fndecl_built_in_p (b
->decl
))
3195 = build_type_attribute_variant (thistype
,
3196 TYPE_ATTRIBUTES (b
->u
.type
));
3197 TREE_TYPE (b
->decl
) = thistype
;
3198 bind (name
, b
->decl
, scope
, /*invisible=*/false, /*nested=*/true,
3202 else if (TREE_PUBLIC (x
))
3204 if (visdecl
&& !b
&& duplicate_decls (x
, visdecl
))
3206 /* An external declaration at block scope referring to a
3207 visible entity with internal linkage. The composite
3208 type will already be correct for this scope, so we
3209 just need to fall through to make the declaration in
3216 bind (name
, x
, external_scope
, /*invisible=*/true,
3217 /*nested=*/false, locus
);
3223 if (TREE_CODE (x
) != PARM_DECL
)
3224 warn_if_shadowing (x
);
3226 skip_external_and_shadow_checks
:
3227 if (TREE_CODE (x
) == TYPE_DECL
)
3229 /* So this is a typedef, set its underlying type. */
3230 set_underlying_type (x
);
3232 /* If X is a typedef defined in the current function, record it
3233 for the purpose of implementing the -Wunused-local-typedefs
3235 record_locally_defined_typedef (x
);
3238 bind (name
, x
, scope
, /*invisible=*/false, nested
, locus
);
3240 /* If x's type is incomplete because it's based on a
3241 structure or union which has not yet been fully declared,
3242 attach it to that structure or union type, so we can go
3243 back and complete the variable declaration later, if the
3244 structure or union gets fully declared.
3246 If the input is erroneous, we can have error_mark in the type
3247 slot (e.g. "f(void a, ...)") - that doesn't count as an
3249 if (TREE_TYPE (x
) != error_mark_node
3250 && !COMPLETE_TYPE_P (TREE_TYPE (x
)))
3252 tree element
= TREE_TYPE (x
);
3254 while (TREE_CODE (element
) == ARRAY_TYPE
)
3255 element
= TREE_TYPE (element
);
3256 element
= TYPE_MAIN_VARIANT (element
);
3258 if (RECORD_OR_UNION_TYPE_P (element
)
3259 && (TREE_CODE (x
) != TYPE_DECL
3260 || TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
)
3261 && !COMPLETE_TYPE_P (element
))
3262 C_TYPE_INCOMPLETE_VARS (element
)
3263 = tree_cons (NULL_TREE
, x
, C_TYPE_INCOMPLETE_VARS (element
));
3269 /* Issue a warning about implicit function declaration. ID is the function
3270 identifier, OLDDECL is a declaration of the function in a different scope,
3274 implicit_decl_warning (location_t loc
, tree id
, tree olddecl
)
3276 if (!warn_implicit_function_declaration
)
3280 auto_diagnostic_group d
;
3283 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_FUNCTION_NAME
, loc
);
3287 if (const char *suggestion
= hint
.suggestion ())
3289 gcc_rich_location
richloc (loc
);
3290 richloc
.add_fixit_replace (suggestion
);
3291 warned
= pedwarn (&richloc
, OPT_Wimplicit_function_declaration
,
3292 "implicit declaration of function %qE;"
3293 " did you mean %qs?",
3297 warned
= pedwarn (loc
, OPT_Wimplicit_function_declaration
,
3298 "implicit declaration of function %qE", id
);
3300 else if (const char *suggestion
= hint
.suggestion ())
3302 gcc_rich_location
richloc (loc
);
3303 richloc
.add_fixit_replace (suggestion
);
3305 (&richloc
, OPT_Wimplicit_function_declaration
,
3306 G_("implicit declaration of function %qE; did you mean %qs?"),
3310 warned
= warning_at (loc
, OPT_Wimplicit_function_declaration
,
3311 G_("implicit declaration of function %qE"), id
);
3313 if (olddecl
&& warned
)
3314 locate_old_decl (olddecl
);
3320 /* This function represents mapping of a function code FCODE
3321 to its respective header. */
3324 header_for_builtin_fn (enum built_in_function fcode
)
3328 CASE_FLT_FN (BUILT_IN_ACOS
):
3329 CASE_FLT_FN (BUILT_IN_ACOSH
):
3330 CASE_FLT_FN (BUILT_IN_ASIN
):
3331 CASE_FLT_FN (BUILT_IN_ASINH
):
3332 CASE_FLT_FN (BUILT_IN_ATAN
):
3333 CASE_FLT_FN (BUILT_IN_ATANH
):
3334 CASE_FLT_FN (BUILT_IN_ATAN2
):
3335 CASE_FLT_FN (BUILT_IN_CBRT
):
3336 CASE_FLT_FN (BUILT_IN_CEIL
):
3337 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL
):
3338 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
3339 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN
):
3340 CASE_FLT_FN (BUILT_IN_COS
):
3341 CASE_FLT_FN (BUILT_IN_COSH
):
3342 CASE_FLT_FN (BUILT_IN_ERF
):
3343 CASE_FLT_FN (BUILT_IN_ERFC
):
3344 CASE_FLT_FN (BUILT_IN_EXP
):
3345 CASE_FLT_FN (BUILT_IN_EXP2
):
3346 CASE_FLT_FN (BUILT_IN_EXPM1
):
3347 CASE_FLT_FN (BUILT_IN_FABS
):
3348 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS
):
3349 CASE_FLT_FN (BUILT_IN_FDIM
):
3350 CASE_FLT_FN (BUILT_IN_FLOOR
):
3351 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR
):
3352 CASE_FLT_FN (BUILT_IN_FMA
):
3353 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA
):
3354 CASE_FLT_FN (BUILT_IN_FMAX
):
3355 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX
):
3356 CASE_FLT_FN (BUILT_IN_FMIN
):
3357 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN
):
3358 CASE_FLT_FN (BUILT_IN_FMOD
):
3359 CASE_FLT_FN (BUILT_IN_FREXP
):
3360 CASE_FLT_FN (BUILT_IN_HYPOT
):
3361 CASE_FLT_FN (BUILT_IN_ILOGB
):
3362 CASE_FLT_FN (BUILT_IN_LDEXP
):
3363 CASE_FLT_FN (BUILT_IN_LGAMMA
):
3364 CASE_FLT_FN (BUILT_IN_LLRINT
):
3365 CASE_FLT_FN (BUILT_IN_LLROUND
):
3366 CASE_FLT_FN (BUILT_IN_LOG
):
3367 CASE_FLT_FN (BUILT_IN_LOG10
):
3368 CASE_FLT_FN (BUILT_IN_LOG1P
):
3369 CASE_FLT_FN (BUILT_IN_LOG2
):
3370 CASE_FLT_FN (BUILT_IN_LOGB
):
3371 CASE_FLT_FN (BUILT_IN_LRINT
):
3372 CASE_FLT_FN (BUILT_IN_LROUND
):
3373 CASE_FLT_FN (BUILT_IN_MODF
):
3374 CASE_FLT_FN (BUILT_IN_NAN
):
3375 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
3376 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT
):
3377 CASE_FLT_FN (BUILT_IN_NEXTAFTER
):
3378 CASE_FLT_FN (BUILT_IN_NEXTTOWARD
):
3379 CASE_FLT_FN (BUILT_IN_POW
):
3380 CASE_FLT_FN (BUILT_IN_REMAINDER
):
3381 CASE_FLT_FN (BUILT_IN_REMQUO
):
3382 CASE_FLT_FN (BUILT_IN_RINT
):
3383 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT
):
3384 CASE_FLT_FN (BUILT_IN_ROUND
):
3385 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND
):
3386 CASE_FLT_FN (BUILT_IN_SCALBLN
):
3387 CASE_FLT_FN (BUILT_IN_SCALBN
):
3388 CASE_FLT_FN (BUILT_IN_SIN
):
3389 CASE_FLT_FN (BUILT_IN_SINH
):
3390 CASE_FLT_FN (BUILT_IN_SINCOS
):
3391 CASE_FLT_FN (BUILT_IN_SQRT
):
3392 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT
):
3393 CASE_FLT_FN (BUILT_IN_TAN
):
3394 CASE_FLT_FN (BUILT_IN_TANH
):
3395 CASE_FLT_FN (BUILT_IN_TGAMMA
):
3396 CASE_FLT_FN (BUILT_IN_TRUNC
):
3397 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC
):
3398 case BUILT_IN_ISINF
:
3399 case BUILT_IN_ISNAN
:
3401 CASE_FLT_FN (BUILT_IN_CABS
):
3402 CASE_FLT_FN (BUILT_IN_CACOS
):
3403 CASE_FLT_FN (BUILT_IN_CACOSH
):
3404 CASE_FLT_FN (BUILT_IN_CARG
):
3405 CASE_FLT_FN (BUILT_IN_CASIN
):
3406 CASE_FLT_FN (BUILT_IN_CASINH
):
3407 CASE_FLT_FN (BUILT_IN_CATAN
):
3408 CASE_FLT_FN (BUILT_IN_CATANH
):
3409 CASE_FLT_FN (BUILT_IN_CCOS
):
3410 CASE_FLT_FN (BUILT_IN_CCOSH
):
3411 CASE_FLT_FN (BUILT_IN_CEXP
):
3412 CASE_FLT_FN (BUILT_IN_CIMAG
):
3413 CASE_FLT_FN (BUILT_IN_CLOG
):
3414 CASE_FLT_FN (BUILT_IN_CONJ
):
3415 CASE_FLT_FN (BUILT_IN_CPOW
):
3416 CASE_FLT_FN (BUILT_IN_CPROJ
):
3417 CASE_FLT_FN (BUILT_IN_CREAL
):
3418 CASE_FLT_FN (BUILT_IN_CSIN
):
3419 CASE_FLT_FN (BUILT_IN_CSINH
):
3420 CASE_FLT_FN (BUILT_IN_CSQRT
):
3421 CASE_FLT_FN (BUILT_IN_CTAN
):
3422 CASE_FLT_FN (BUILT_IN_CTANH
):
3423 return "<complex.h>";
3424 case BUILT_IN_MEMCHR
:
3425 case BUILT_IN_MEMCMP
:
3426 case BUILT_IN_MEMCPY
:
3427 case BUILT_IN_MEMMOVE
:
3428 case BUILT_IN_MEMSET
:
3429 case BUILT_IN_STRCAT
:
3430 case BUILT_IN_STRCHR
:
3431 case BUILT_IN_STRCMP
:
3432 case BUILT_IN_STRCPY
:
3433 case BUILT_IN_STRCSPN
:
3434 case BUILT_IN_STRLEN
:
3435 case BUILT_IN_STRNCAT
:
3436 case BUILT_IN_STRNCMP
:
3437 case BUILT_IN_STRNCPY
:
3438 case BUILT_IN_STRPBRK
:
3439 case BUILT_IN_STRRCHR
:
3440 case BUILT_IN_STRSPN
:
3441 case BUILT_IN_STRSTR
:
3442 return "<string.h>";
3443 case BUILT_IN_FPRINTF
:
3445 case BUILT_IN_FPUTC
:
3446 case BUILT_IN_FPUTS
:
3447 case BUILT_IN_FSCANF
:
3448 case BUILT_IN_FWRITE
:
3449 case BUILT_IN_PRINTF
:
3450 case BUILT_IN_PUTCHAR
:
3452 case BUILT_IN_SCANF
:
3453 case BUILT_IN_SNPRINTF
:
3454 case BUILT_IN_SPRINTF
:
3455 case BUILT_IN_SSCANF
:
3456 case BUILT_IN_VFPRINTF
:
3457 case BUILT_IN_VFSCANF
:
3458 case BUILT_IN_VPRINTF
:
3459 case BUILT_IN_VSCANF
:
3460 case BUILT_IN_VSNPRINTF
:
3461 case BUILT_IN_VSPRINTF
:
3462 case BUILT_IN_VSSCANF
:
3464 case BUILT_IN_ISALNUM
:
3465 case BUILT_IN_ISALPHA
:
3466 case BUILT_IN_ISBLANK
:
3467 case BUILT_IN_ISCNTRL
:
3468 case BUILT_IN_ISDIGIT
:
3469 case BUILT_IN_ISGRAPH
:
3470 case BUILT_IN_ISLOWER
:
3471 case BUILT_IN_ISPRINT
:
3472 case BUILT_IN_ISPUNCT
:
3473 case BUILT_IN_ISSPACE
:
3474 case BUILT_IN_ISUPPER
:
3475 case BUILT_IN_ISXDIGIT
:
3476 case BUILT_IN_TOLOWER
:
3477 case BUILT_IN_TOUPPER
:
3479 case BUILT_IN_ISWALNUM
:
3480 case BUILT_IN_ISWALPHA
:
3481 case BUILT_IN_ISWBLANK
:
3482 case BUILT_IN_ISWCNTRL
:
3483 case BUILT_IN_ISWDIGIT
:
3484 case BUILT_IN_ISWGRAPH
:
3485 case BUILT_IN_ISWLOWER
:
3486 case BUILT_IN_ISWPRINT
:
3487 case BUILT_IN_ISWPUNCT
:
3488 case BUILT_IN_ISWSPACE
:
3489 case BUILT_IN_ISWUPPER
:
3490 case BUILT_IN_ISWXDIGIT
:
3491 case BUILT_IN_TOWLOWER
:
3492 case BUILT_IN_TOWUPPER
:
3493 return "<wctype.h>";
3494 case BUILT_IN_ABORT
:
3496 case BUILT_IN_CALLOC
:
3500 case BUILT_IN_LLABS
:
3501 case BUILT_IN_MALLOC
:
3502 case BUILT_IN_REALLOC
:
3503 case BUILT_IN__EXIT2
:
3504 case BUILT_IN_ALIGNED_ALLOC
:
3505 return "<stdlib.h>";
3506 case BUILT_IN_IMAXABS
:
3507 return "<inttypes.h>";
3508 case BUILT_IN_STRFTIME
:
3515 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3516 function of type int (). */
3519 implicitly_declare (location_t loc
, tree functionid
)
3521 struct c_binding
*b
;
3522 tree decl
= NULL_TREE
;
3525 for (b
= I_SYMBOL_BINDING (functionid
); b
; b
= b
->shadowed
)
3527 if (B_IN_SCOPE (b
, external_scope
))
3536 if (TREE_CODE (decl
) != FUNCTION_DECL
)
3539 /* FIXME: Objective-C has weird not-really-builtin functions
3540 which are supposed to be visible automatically. They wind up
3541 in the external scope because they're pushed before the file
3542 scope gets created. Catch this here and rebind them into the
3544 if (!fndecl_built_in_p (decl
) && DECL_IS_BUILTIN (decl
))
3546 bind (functionid
, decl
, file_scope
,
3547 /*invisible=*/false, /*nested=*/true,
3548 DECL_SOURCE_LOCATION (decl
));
3553 tree newtype
= default_function_type
;
3555 TREE_TYPE (decl
) = b
->u
.type
;
3556 /* Implicit declaration of a function already declared
3557 (somehow) in a different scope, or as a built-in.
3558 If this is the first time this has happened, warn;
3559 then recycle the old declaration but with the new type. */
3560 if (!C_DECL_IMPLICIT (decl
))
3562 implicit_decl_warning (loc
, functionid
, decl
);
3563 C_DECL_IMPLICIT (decl
) = 1;
3565 if (fndecl_built_in_p (decl
))
3567 newtype
= build_type_attribute_variant (newtype
,
3569 (TREE_TYPE (decl
)));
3570 if (!comptypes (newtype
, TREE_TYPE (decl
)))
3572 bool warned
= warning_at (loc
, 0, "incompatible implicit "
3573 "declaration of built-in "
3574 "function %qD", decl
);
3575 /* See if we can hint which header to include. */
3577 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl
));
3578 if (header
!= NULL
&& warned
)
3580 rich_location
richloc (line_table
, loc
);
3581 maybe_add_include_fixit (&richloc
, header
, true);
3583 "include %qs or provide a declaration of %qD",
3586 newtype
= TREE_TYPE (decl
);
3591 if (!comptypes (newtype
, TREE_TYPE (decl
)))
3593 auto_diagnostic_group d
;
3594 error_at (loc
, "incompatible implicit declaration of "
3595 "function %qD", decl
);
3596 locate_old_decl (decl
);
3599 b
->u
.type
= TREE_TYPE (decl
);
3600 TREE_TYPE (decl
) = newtype
;
3601 bind (functionid
, decl
, current_scope
,
3602 /*invisible=*/false, /*nested=*/true,
3603 DECL_SOURCE_LOCATION (decl
));
3608 /* Not seen before. */
3609 decl
= build_decl (loc
, FUNCTION_DECL
, functionid
, default_function_type
);
3610 DECL_EXTERNAL (decl
) = 1;
3611 TREE_PUBLIC (decl
) = 1;
3612 C_DECL_IMPLICIT (decl
) = 1;
3613 implicit_decl_warning (loc
, functionid
, 0);
3614 asmspec_tree
= maybe_apply_renaming_pragma (decl
, /*asmname=*/NULL
);
3616 set_user_assembler_name (decl
, TREE_STRING_POINTER (asmspec_tree
));
3618 /* C89 says implicit declarations are in the innermost block.
3619 So we record the decl in the standard fashion. */
3620 decl
= pushdecl (decl
);
3622 /* No need to call objc_check_decl here - it's a function type. */
3623 rest_of_decl_compilation (decl
, 0, 0);
3625 /* Write a record describing this implicit function declaration
3626 to the prototypes file (if requested). */
3627 gen_aux_info_record (decl
, 0, 1, 0);
3629 /* Possibly apply some default attributes to this implicit declaration. */
3630 decl_attributes (&decl
, NULL_TREE
, 0);
3635 /* Issue an error message for a reference to an undeclared variable
3636 ID, including a reference to a builtin outside of function-call
3637 context. Establish a binding of the identifier to error_mark_node
3638 in an appropriate scope, which will suppress further errors for the
3639 same identifier. The error message should be given location LOC. */
3641 undeclared_variable (location_t loc
, tree id
)
3643 static bool already
= false;
3644 struct c_scope
*scope
;
3646 auto_diagnostic_group d
;
3647 if (current_function_decl
== NULL_TREE
)
3649 name_hint guessed_id
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_NAME
, loc
);
3650 if (const char *suggestion
= guessed_id
.suggestion ())
3652 gcc_rich_location
richloc (loc
);
3653 richloc
.add_fixit_replace (suggestion
);
3655 "%qE undeclared here (not in a function);"
3656 " did you mean %qs?",
3660 error_at (loc
, "%qE undeclared here (not in a function)", id
);
3661 scope
= current_scope
;
3665 if (!objc_diagnose_private_ivar (id
))
3667 name_hint guessed_id
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_NAME
, loc
);
3668 if (const char *suggestion
= guessed_id
.suggestion ())
3670 gcc_rich_location
richloc (loc
);
3671 richloc
.add_fixit_replace (suggestion
);
3673 "%qE undeclared (first use in this function);"
3674 " did you mean %qs?",
3678 error_at (loc
, "%qE undeclared (first use in this function)", id
);
3682 inform (loc
, "each undeclared identifier is reported only"
3683 " once for each function it appears in");
3687 /* If we are parsing old-style parameter decls, current_function_decl
3688 will be nonnull but current_function_scope will be null. */
3689 scope
= current_function_scope
? current_function_scope
: current_scope
;
3691 bind (id
, error_mark_node
, scope
, /*invisible=*/false, /*nested=*/false,
3695 /* Subroutine of lookup_label, declare_label, define_label: construct a
3696 LABEL_DECL with all the proper frills. Also create a struct
3697 c_label_vars initialized for the current scope. */
3700 make_label (location_t location
, tree name
, bool defining
,
3701 struct c_label_vars
**p_label_vars
)
3703 tree label
= build_decl (location
, LABEL_DECL
, name
, void_type_node
);
3704 DECL_CONTEXT (label
) = current_function_decl
;
3705 SET_DECL_MODE (label
, VOIDmode
);
3707 c_label_vars
*label_vars
= ggc_alloc
<c_label_vars
> ();
3708 label_vars
->shadowed
= NULL
;
3709 set_spot_bindings (&label_vars
->label_bindings
, defining
);
3710 label_vars
->decls_in_scope
= make_tree_vector ();
3711 label_vars
->gotos
= NULL
;
3712 *p_label_vars
= label_vars
;
3717 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3718 Create one if none exists so far for the current function.
3719 This is called when a label is used in a goto expression or
3720 has its address taken. */
3723 lookup_label (tree name
)
3726 struct c_label_vars
*label_vars
;
3728 if (current_function_scope
== 0)
3730 error ("label %qE referenced outside of any function", name
);
3734 /* Use a label already defined or ref'd with this name, but not if
3735 it is inherited from a containing function and wasn't declared
3737 label
= I_LABEL_DECL (name
);
3738 if (label
&& (DECL_CONTEXT (label
) == current_function_decl
3739 || C_DECLARED_LABEL_FLAG (label
)))
3741 /* If the label has only been declared, update its apparent
3742 location to point here, for better diagnostics if it
3743 turns out not to have been defined. */
3744 if (DECL_INITIAL (label
) == NULL_TREE
)
3745 DECL_SOURCE_LOCATION (label
) = input_location
;
3749 /* No label binding for that identifier; make one. */
3750 label
= make_label (input_location
, name
, false, &label_vars
);
3752 /* Ordinary labels go in the current function scope. */
3753 bind_label (name
, label
, current_function_scope
, label_vars
);
3758 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3762 warn_about_goto (location_t goto_loc
, tree label
, tree decl
)
3764 if (variably_modified_type_p (TREE_TYPE (decl
), NULL_TREE
))
3766 "jump into scope of identifier with variably modified type");
3768 warning_at (goto_loc
, OPT_Wjump_misses_init
,
3769 "jump skips variable initialization");
3770 inform (DECL_SOURCE_LOCATION (label
), "label %qD defined here", label
);
3771 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3774 /* Look up a label because of a goto statement. This is like
3775 lookup_label, but also issues any appropriate warnings. */
3778 lookup_label_for_goto (location_t loc
, tree name
)
3781 struct c_label_vars
*label_vars
;
3785 label
= lookup_label (name
);
3786 if (label
== NULL_TREE
)
3789 /* If we are jumping to a different function, we can't issue any
3791 if (DECL_CONTEXT (label
) != current_function_decl
)
3793 gcc_assert (C_DECLARED_LABEL_FLAG (label
));
3797 label_vars
= I_LABEL_BINDING (name
)->u
.label
;
3799 /* If the label has not yet been defined, then push this goto on a
3800 list for possible later warnings. */
3801 if (label_vars
->label_bindings
.scope
== NULL
)
3803 c_goto_bindings
*g
= ggc_alloc
<c_goto_bindings
> ();
3806 set_spot_bindings (&g
->goto_bindings
, true);
3807 vec_safe_push (label_vars
->gotos
, g
);
3811 /* If there are any decls in label_vars->decls_in_scope, then this
3812 goto has missed the declaration of the decl. This happens for a
3818 Issue a warning or error. */
3819 FOR_EACH_VEC_SAFE_ELT (label_vars
->decls_in_scope
, ix
, decl
)
3820 warn_about_goto (loc
, label
, decl
);
3822 if (label_vars
->label_bindings
.left_stmt_expr
)
3824 error_at (loc
, "jump into statement expression");
3825 inform (DECL_SOURCE_LOCATION (label
), "label %qD defined here", label
);
3831 /* Make a label named NAME in the current function, shadowing silently
3832 any that may be inherited from containing functions or containing
3833 scopes. This is called for __label__ declarations. */
3836 declare_label (tree name
)
3838 struct c_binding
*b
= I_LABEL_BINDING (name
);
3840 struct c_label_vars
*label_vars
;
3842 /* Check to make sure that the label hasn't already been declared
3844 if (b
&& B_IN_CURRENT_SCOPE (b
))
3846 auto_diagnostic_group d
;
3847 error ("duplicate label declaration %qE", name
);
3848 locate_old_decl (b
->decl
);
3850 /* Just use the previous declaration. */
3854 label
= make_label (input_location
, name
, false, &label_vars
);
3855 C_DECLARED_LABEL_FLAG (label
) = 1;
3857 /* Declared labels go in the current scope. */
3858 bind_label (name
, label
, current_scope
, label_vars
);
3863 /* When we define a label, issue any appropriate warnings if there are
3864 any gotos earlier in the function which jump to this label. */
3867 check_earlier_gotos (tree label
, struct c_label_vars
* label_vars
)
3870 struct c_goto_bindings
*g
;
3872 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
3874 struct c_binding
*b
;
3875 struct c_scope
*scope
;
3877 /* We have a goto to this label. The goto is going forward. In
3878 g->scope, the goto is going to skip any binding which was
3879 defined after g->bindings_in_scope. */
3880 if (g
->goto_bindings
.scope
->has_jump_unsafe_decl
)
3882 for (b
= g
->goto_bindings
.scope
->bindings
;
3883 b
!= g
->goto_bindings
.bindings_in_scope
;
3886 if (decl_jump_unsafe (b
->decl
))
3887 warn_about_goto (g
->loc
, label
, b
->decl
);
3891 /* We also need to warn about decls defined in any scopes
3892 between the scope of the label and the scope of the goto. */
3893 for (scope
= label_vars
->label_bindings
.scope
;
3894 scope
!= g
->goto_bindings
.scope
;
3895 scope
= scope
->outer
)
3897 gcc_assert (scope
!= NULL
);
3898 if (scope
->has_jump_unsafe_decl
)
3900 if (scope
== label_vars
->label_bindings
.scope
)
3901 b
= label_vars
->label_bindings
.bindings_in_scope
;
3903 b
= scope
->bindings
;
3904 for (; b
!= NULL
; b
= b
->prev
)
3906 if (decl_jump_unsafe (b
->decl
))
3907 warn_about_goto (g
->loc
, label
, b
->decl
);
3912 if (g
->goto_bindings
.stmt_exprs
> 0)
3914 error_at (g
->loc
, "jump into statement expression");
3915 inform (DECL_SOURCE_LOCATION (label
), "label %qD defined here",
3920 /* Now that the label is defined, we will issue warnings about
3921 subsequent gotos to this label when we see them. */
3922 vec_safe_truncate (label_vars
->gotos
, 0);
3923 label_vars
->gotos
= NULL
;
3926 /* Define a label, specifying the location in the source file.
3927 Return the LABEL_DECL node for the label, if the definition is valid.
3928 Otherwise return NULL_TREE. */
3931 define_label (location_t location
, tree name
)
3933 /* Find any preexisting label with this name. It is an error
3934 if that label has already been defined in this function, or
3935 if there is a containing function with a declared label with
3937 tree label
= I_LABEL_DECL (name
);
3940 && ((DECL_CONTEXT (label
) == current_function_decl
3941 && DECL_INITIAL (label
) != NULL_TREE
)
3942 || (DECL_CONTEXT (label
) != current_function_decl
3943 && C_DECLARED_LABEL_FLAG (label
))))
3945 auto_diagnostic_group d
;
3946 error_at (location
, "duplicate label %qD", label
);
3947 locate_old_decl (label
);
3950 else if (label
&& DECL_CONTEXT (label
) == current_function_decl
)
3952 struct c_label_vars
*label_vars
= I_LABEL_BINDING (name
)->u
.label
;
3954 /* The label has been used or declared already in this function,
3955 but not defined. Update its location to point to this
3957 DECL_SOURCE_LOCATION (label
) = location
;
3958 set_spot_bindings (&label_vars
->label_bindings
, true);
3960 /* Issue warnings as required about any goto statements from
3961 earlier in the function. */
3962 check_earlier_gotos (label
, label_vars
);
3966 struct c_label_vars
*label_vars
;
3968 /* No label binding for that identifier; make one. */
3969 label
= make_label (location
, name
, true, &label_vars
);
3971 /* Ordinary labels go in the current function scope. */
3972 bind_label (name
, label
, current_function_scope
, label_vars
);
3975 if (!in_system_header_at (input_location
) && lookup_name (name
))
3976 warning_at (location
, OPT_Wtraditional
,
3977 "traditional C lacks a separate namespace "
3978 "for labels, identifier %qE conflicts", name
);
3980 /* Mark label as having been defined. */
3981 DECL_INITIAL (label
) = error_mark_node
;
3985 /* Get the bindings for a new switch statement. This is used to issue
3986 warnings as appropriate for jumps from the switch to case or
3989 struct c_spot_bindings
*
3990 c_get_switch_bindings (void)
3992 struct c_spot_bindings
*switch_bindings
;
3994 switch_bindings
= XNEW (struct c_spot_bindings
);
3995 set_spot_bindings (switch_bindings
, true);
3996 return switch_bindings
;
4000 c_release_switch_bindings (struct c_spot_bindings
*bindings
)
4002 gcc_assert (bindings
->stmt_exprs
== 0 && !bindings
->left_stmt_expr
);
4006 /* This is called at the point of a case or default label to issue
4007 warnings about decls as needed. It returns true if it found an
4008 error, not just a warning. */
4011 c_check_switch_jump_warnings (struct c_spot_bindings
*switch_bindings
,
4012 location_t switch_loc
, location_t case_loc
)
4015 struct c_scope
*scope
;
4018 for (scope
= current_scope
;
4019 scope
!= switch_bindings
->scope
;
4020 scope
= scope
->outer
)
4022 struct c_binding
*b
;
4024 gcc_assert (scope
!= NULL
);
4026 if (!scope
->has_jump_unsafe_decl
)
4029 for (b
= scope
->bindings
; b
!= NULL
; b
= b
->prev
)
4031 if (decl_jump_unsafe (b
->decl
))
4033 if (variably_modified_type_p (TREE_TYPE (b
->decl
), NULL_TREE
))
4037 ("switch jumps into scope of identifier with "
4038 "variably modified type"));
4041 warning_at (case_loc
, OPT_Wjump_misses_init
,
4042 "switch jumps over variable initialization");
4043 inform (switch_loc
, "switch starts here");
4044 inform (DECL_SOURCE_LOCATION (b
->decl
), "%qD declared here",
4050 if (switch_bindings
->stmt_exprs
> 0)
4053 error_at (case_loc
, "switch jumps into statement expression");
4054 inform (switch_loc
, "switch starts here");
4060 /* Given NAME, an IDENTIFIER_NODE,
4061 return the structure (or union or enum) definition for that name.
4062 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4063 CODE says which kind of type the caller wants;
4064 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4065 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4066 location where the tag was defined.
4067 If the wrong kind of type is found, an error is reported. */
4070 lookup_tag (enum tree_code code
, tree name
, bool thislevel_only
,
4073 struct c_binding
*b
= I_TAG_BINDING (name
);
4074 bool thislevel
= false;
4079 /* We only care about whether it's in this level if
4080 thislevel_only was set or it might be a type clash. */
4081 if (thislevel_only
|| TREE_CODE (b
->decl
) != code
)
4083 /* For our purposes, a tag in the external scope is the same as
4084 a tag in the file scope. (Primarily relevant to Objective-C
4085 and its builtin structure tags, which get pushed before the
4086 file scope is created.) */
4087 if (B_IN_CURRENT_SCOPE (b
)
4088 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
4092 if (thislevel_only
&& !thislevel
)
4095 if (TREE_CODE (b
->decl
) != code
)
4097 /* Definition isn't the kind we were looking for. */
4098 pending_invalid_xref
= name
;
4099 pending_invalid_xref_location
= input_location
;
4101 /* If in the same binding level as a declaration as a tag
4102 of a different type, this must not be allowed to
4103 shadow that tag, so give the error immediately.
4104 (For example, "struct foo; union foo;" is invalid.) */
4106 pending_xref_error ();
4115 /* Return true if a definition exists for NAME with code CODE. */
4118 tag_exists_p (enum tree_code code
, tree name
)
4120 struct c_binding
*b
= I_TAG_BINDING (name
);
4122 if (b
== NULL
|| b
->decl
== NULL_TREE
)
4124 return TREE_CODE (b
->decl
) == code
;
4127 /* Print an error message now
4128 for a recent invalid struct, union or enum cross reference.
4129 We don't print them immediately because they are not invalid
4130 when used in the `struct foo;' construct for shadowing. */
4133 pending_xref_error (void)
4135 if (pending_invalid_xref
!= NULL_TREE
)
4136 error_at (pending_invalid_xref_location
, "%qE defined as wrong kind of tag",
4137 pending_invalid_xref
);
4138 pending_invalid_xref
= NULL_TREE
;
4142 /* Look up NAME in the current scope and its superiors
4143 in the namespace of variables, functions and typedefs.
4144 Return a ..._DECL node of some kind representing its definition,
4145 or return NULL_TREE if it is undefined. */
4148 lookup_name (tree name
)
4150 struct c_binding
*b
= I_SYMBOL_BINDING (name
);
4151 if (b
&& !b
->invisible
)
4153 maybe_record_typedef_use (b
->decl
);
4159 /* Similar to `lookup_name' but look only at the indicated scope. */
4162 lookup_name_in_scope (tree name
, struct c_scope
*scope
)
4164 struct c_binding
*b
;
4166 for (b
= I_SYMBOL_BINDING (name
); b
; b
= b
->shadowed
)
4167 if (B_IN_SCOPE (b
, scope
))
4172 /* Look for the closest match for NAME within the currently valid
4175 This finds the identifier with the lowest Levenshtein distance to
4176 NAME. If there are multiple candidates with equal minimal distance,
4177 the first one found is returned. Scopes are searched from innermost
4178 outwards, and within a scope in reverse order of declaration, thus
4179 benefiting candidates "near" to the current scope.
4181 The function also looks for similar macro names to NAME, since a
4182 misspelled macro name will not be expanded, and hence looks like an
4183 identifier to the C frontend.
4185 It also looks for start_typename keywords, to detect "singed" vs "signed"
4188 Use LOC for any deferred diagnostics. */
4191 lookup_name_fuzzy (tree name
, enum lookup_name_fuzzy_kind kind
, location_t loc
)
4193 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
4195 /* First, try some well-known names in the C standard library, in case
4196 the user forgot a #include. */
4197 const char *header_hint
4198 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name
));
4201 return name_hint (NULL
,
4202 new suggest_missing_header (loc
,
4203 IDENTIFIER_POINTER (name
),
4206 /* Only suggest names reserved for the implementation if NAME begins
4207 with an underscore. */
4208 bool consider_implementation_names
= (IDENTIFIER_POINTER (name
)[0] == '_');
4210 best_match
<tree
, tree
> bm (name
);
4212 /* Look within currently valid scopes. */
4213 for (c_scope
*scope
= current_scope
; scope
; scope
= scope
->outer
)
4214 for (c_binding
*binding
= scope
->bindings
; binding
; binding
= binding
->prev
)
4216 if (!binding
->id
|| binding
->invisible
)
4218 if (binding
->decl
== error_mark_node
)
4220 /* Don't use bindings from implicitly declared functions,
4221 as they were likely misspellings themselves. */
4222 if (TREE_CODE (binding
->decl
) == FUNCTION_DECL
)
4223 if (C_DECL_IMPLICIT (binding
->decl
))
4225 /* Don't suggest names that are reserved for use by the
4226 implementation, unless NAME began with an underscore. */
4227 if (!consider_implementation_names
)
4229 const char *suggestion_str
= IDENTIFIER_POINTER (binding
->id
);
4230 if (name_reserved_for_implementation_p (suggestion_str
))
4235 case FUZZY_LOOKUP_TYPENAME
:
4236 if (TREE_CODE (binding
->decl
) != TYPE_DECL
)
4240 case FUZZY_LOOKUP_FUNCTION_NAME
:
4241 if (TREE_CODE (binding
->decl
) != FUNCTION_DECL
)
4243 /* Allow function pointers. */
4244 if ((VAR_P (binding
->decl
)
4245 || TREE_CODE (binding
->decl
) == PARM_DECL
)
4246 && TREE_CODE (TREE_TYPE (binding
->decl
)) == POINTER_TYPE
4247 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding
->decl
)))
4257 bm
.consider (binding
->id
);
4260 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4262 x = SOME_OTHER_MACRO (y);
4263 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4264 as a misspelled identifier.
4266 Use the best distance so far so that a candidate is only set if
4267 a macro is better than anything so far. This allows early rejection
4268 (without calculating the edit distance) of macro names that must have
4269 distance >= bm.get_best_distance (), and means that we only get a
4270 non-NULL result for best_macro_match if it's better than any of
4271 the identifiers already checked, which avoids needless creation
4272 of identifiers for macro hashnodes. */
4273 best_macro_match
bmm (name
, bm
.get_best_distance (), parse_in
);
4274 cpp_hashnode
*best_macro
= bmm
.get_best_meaningful_candidate ();
4275 /* If a macro is the closest so far to NAME, use it, creating an
4276 identifier tree node for it. */
4279 const char *id
= (const char *)best_macro
->ident
.str
;
4280 tree macro_as_identifier
4281 = get_identifier_with_length (id
, best_macro
->ident
.len
);
4282 bm
.set_best_so_far (macro_as_identifier
,
4283 bmm
.get_best_distance (),
4284 bmm
.get_best_candidate_length ());
4287 /* Try the "start_typename" keywords to detect
4288 "singed" vs "signed" typos. */
4289 if (kind
== FUZZY_LOOKUP_TYPENAME
)
4291 for (unsigned i
= 0; i
< num_c_common_reswords
; i
++)
4293 const c_common_resword
*resword
= &c_common_reswords
[i
];
4294 if (!c_keyword_starts_typename (resword
->rid
))
4296 tree resword_identifier
= ridpointers
[resword
->rid
];
4297 if (!resword_identifier
)
4299 gcc_assert (TREE_CODE (resword_identifier
) == IDENTIFIER_NODE
);
4300 bm
.consider (resword_identifier
);
4304 tree best
= bm
.get_best_meaningful_candidate ();
4306 return name_hint (IDENTIFIER_POINTER (best
), NULL
);
4308 return name_hint (NULL
, NULL
);
4312 /* Create the predefined scalar types of C,
4313 and some nodes representing standard constants (0, 1, (void *) 0).
4314 Initialize the global scope.
4315 Make definitions for built-in primitive functions. */
4318 c_init_decl_processing (void)
4320 location_t save_loc
= input_location
;
4322 /* Initialize reserved words for parser. */
4325 current_function_decl
= NULL_TREE
;
4327 gcc_obstack_init (&parser_obstack
);
4329 /* Make the externals scope. */
4331 external_scope
= current_scope
;
4333 /* Declarations from c_common_nodes_and_builtins must not be associated
4334 with this input file, lest we get differences between using and not
4335 using preprocessed headers. */
4336 input_location
= BUILTINS_LOCATION
;
4338 c_common_nodes_and_builtins ();
4340 /* In C, comparisons and TRUTH_* expressions have type int. */
4341 truthvalue_type_node
= integer_type_node
;
4342 truthvalue_true_node
= integer_one_node
;
4343 truthvalue_false_node
= integer_zero_node
;
4345 /* Even in C99, which has a real boolean type. */
4346 pushdecl (build_decl (UNKNOWN_LOCATION
, TYPE_DECL
, get_identifier ("_Bool"),
4347 boolean_type_node
));
4349 input_location
= save_loc
;
4351 make_fname_decl
= c_make_fname_decl
;
4352 start_fname_decls ();
4355 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4356 give the decl, NAME is the initialization string and TYPE_DEP
4357 indicates whether NAME depended on the type of the function. As we
4358 don't yet implement delayed emission of static data, we mark the
4359 decl as emitted so it is not placed in the output. Anything using
4360 it must therefore pull out the STRING_CST initializer directly.
4364 c_make_fname_decl (location_t loc
, tree id
, int type_dep
)
4366 const char *name
= fname_as_string (type_dep
);
4367 tree decl
, type
, init
;
4368 size_t length
= strlen (name
);
4370 type
= build_array_type (char_type_node
,
4371 build_index_type (size_int (length
)));
4372 type
= c_build_qualified_type (type
, TYPE_QUAL_CONST
);
4374 decl
= build_decl (loc
, VAR_DECL
, id
, type
);
4376 TREE_STATIC (decl
) = 1;
4377 TREE_READONLY (decl
) = 1;
4378 DECL_ARTIFICIAL (decl
) = 1;
4380 init
= build_string (length
+ 1, name
);
4381 free (CONST_CAST (char *, name
));
4382 TREE_TYPE (init
) = type
;
4383 DECL_INITIAL (decl
) = init
;
4385 TREE_USED (decl
) = 1;
4387 if (current_function_decl
4388 /* For invalid programs like this:
4391 const char* p = __FUNCTION__;
4393 the __FUNCTION__ is believed to appear in K&R style function
4394 parameter declarator. In that case we still don't have
4396 && current_function_scope
)
4398 DECL_CONTEXT (decl
) = current_function_decl
;
4399 bind (id
, decl
, current_function_scope
,
4400 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
4403 finish_decl (decl
, loc
, init
, NULL_TREE
, NULL_TREE
);
4409 c_builtin_function (tree decl
)
4411 tree type
= TREE_TYPE (decl
);
4412 tree id
= DECL_NAME (decl
);
4414 const char *name
= IDENTIFIER_POINTER (id
);
4415 C_DECL_BUILTIN_PROTOTYPE (decl
) = prototype_p (type
);
4417 /* Should never be called on a symbol with a preexisting meaning. */
4418 gcc_assert (!I_SYMBOL_BINDING (id
));
4420 bind (id
, decl
, external_scope
, /*invisible=*/true, /*nested=*/false,
4423 /* Builtins in the implementation namespace are made visible without
4424 needing to be explicitly declared. See push_file_scope. */
4425 if (name
[0] == '_' && (name
[1] == '_' || ISUPPER (name
[1])))
4427 DECL_CHAIN (decl
) = visible_builtins
;
4428 visible_builtins
= decl
;
4435 c_builtin_function_ext_scope (tree decl
)
4437 tree type
= TREE_TYPE (decl
);
4438 tree id
= DECL_NAME (decl
);
4440 const char *name
= IDENTIFIER_POINTER (id
);
4441 C_DECL_BUILTIN_PROTOTYPE (decl
) = prototype_p (type
);
4444 bind (id
, decl
, external_scope
, /*invisible=*/false, /*nested=*/false,
4447 /* Builtins in the implementation namespace are made visible without
4448 needing to be explicitly declared. See push_file_scope. */
4449 if (name
[0] == '_' && (name
[1] == '_' || ISUPPER (name
[1])))
4451 DECL_CHAIN (decl
) = visible_builtins
;
4452 visible_builtins
= decl
;
4458 /* Called when a declaration is seen that contains no names to declare.
4459 If its type is a reference to a structure, union or enum inherited
4460 from a containing scope, shadow that tag name for the current scope
4461 with a forward reference.
4462 If its type defines a new named structure or union
4463 or defines an enum, it is valid but we need not do anything here.
4464 Otherwise, it is an error. */
4467 shadow_tag (const struct c_declspecs
*declspecs
)
4469 shadow_tag_warned (declspecs
, 0);
4472 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4475 shadow_tag_warned (const struct c_declspecs
*declspecs
, int warned
)
4477 bool found_tag
= false;
4479 if (declspecs
->type
&& !declspecs
->default_int_p
&& !declspecs
->typedef_p
)
4481 tree value
= declspecs
->type
;
4482 enum tree_code code
= TREE_CODE (value
);
4484 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
|| code
== ENUMERAL_TYPE
)
4485 /* Used to test also that TYPE_SIZE (value) != 0.
4486 That caused warning for `struct foo;' at top level in the file. */
4488 tree name
= TYPE_NAME (value
);
4493 if (declspecs
->restrict_p
)
4495 error ("invalid use of %<restrict%>");
4499 if (name
== NULL_TREE
)
4501 if (warned
!= 1 && code
!= ENUMERAL_TYPE
)
4502 /* Empty unnamed enum OK */
4504 pedwarn (input_location
, 0,
4505 "unnamed struct/union that defines no instances");
4509 else if (declspecs
->typespec_kind
!= ctsk_tagdef
4510 && declspecs
->typespec_kind
!= ctsk_tagfirstref
4511 && declspecs
->storage_class
!= csc_none
)
4514 pedwarn (input_location
, 0,
4515 "empty declaration with storage class specifier "
4516 "does not redeclare tag");
4518 pending_xref_error ();
4520 else if (declspecs
->typespec_kind
!= ctsk_tagdef
4521 && declspecs
->typespec_kind
!= ctsk_tagfirstref
4522 && (declspecs
->const_p
4523 || declspecs
->volatile_p
4524 || declspecs
->atomic_p
4525 || declspecs
->restrict_p
4526 || declspecs
->address_space
))
4529 pedwarn (input_location
, 0,
4530 "empty declaration with type qualifier "
4531 "does not redeclare tag");
4533 pending_xref_error ();
4535 else if (declspecs
->typespec_kind
!= ctsk_tagdef
4536 && declspecs
->typespec_kind
!= ctsk_tagfirstref
4537 && declspecs
->alignas_p
)
4540 pedwarn (input_location
, 0,
4541 "empty declaration with %<_Alignas%> "
4542 "does not redeclare tag");
4544 pending_xref_error ();
4548 pending_invalid_xref
= NULL_TREE
;
4549 t
= lookup_tag (code
, name
, true, NULL
);
4553 t
= make_node (code
);
4554 pushtag (input_location
, name
, t
);
4560 if (warned
!= 1 && !in_system_header_at (input_location
))
4562 pedwarn (input_location
, 0,
4563 "useless type name in empty declaration");
4568 else if (warned
!= 1 && !in_system_header_at (input_location
)
4569 && declspecs
->typedef_p
)
4571 pedwarn (input_location
, 0, "useless type name in empty declaration");
4575 pending_invalid_xref
= NULL_TREE
;
4577 if (declspecs
->inline_p
)
4579 error ("%<inline%> in empty declaration");
4583 if (declspecs
->noreturn_p
)
4585 error ("%<_Noreturn%> in empty declaration");
4589 if (current_scope
== file_scope
&& declspecs
->storage_class
== csc_auto
)
4591 error ("%<auto%> in file-scope empty declaration");
4595 if (current_scope
== file_scope
&& declspecs
->storage_class
== csc_register
)
4597 error ("%<register%> in file-scope empty declaration");
4601 if (!warned
&& !in_system_header_at (input_location
)
4602 && declspecs
->storage_class
!= csc_none
)
4604 warning (0, "useless storage class specifier in empty declaration");
4608 if (!warned
&& !in_system_header_at (input_location
) && declspecs
->thread_p
)
4610 warning (0, "useless %qs in empty declaration",
4611 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
4616 && !in_system_header_at (input_location
)
4617 && (declspecs
->const_p
4618 || declspecs
->volatile_p
4619 || declspecs
->atomic_p
4620 || declspecs
->restrict_p
4621 || declspecs
->address_space
))
4623 warning (0, "useless type qualifier in empty declaration");
4627 if (!warned
&& !in_system_header_at (input_location
)
4628 && declspecs
->alignas_p
)
4630 warning (0, "useless %<_Alignas%> in empty declaration");
4637 pedwarn (input_location
, 0, "empty declaration");
4642 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4643 bits. SPECS represents declaration specifiers that the grammar
4644 only permits to contain type qualifiers and attributes. */
4647 quals_from_declspecs (const struct c_declspecs
*specs
)
4649 int quals
= ((specs
->const_p
? TYPE_QUAL_CONST
: 0)
4650 | (specs
->volatile_p
? TYPE_QUAL_VOLATILE
: 0)
4651 | (specs
->restrict_p
? TYPE_QUAL_RESTRICT
: 0)
4652 | (specs
->atomic_p
? TYPE_QUAL_ATOMIC
: 0)
4653 | (ENCODE_QUAL_ADDR_SPACE (specs
->address_space
)));
4654 gcc_assert (!specs
->type
4655 && !specs
->decl_attr
4656 && specs
->typespec_word
== cts_none
4657 && specs
->storage_class
== csc_none
4658 && !specs
->typedef_p
4659 && !specs
->explicit_signed_p
4660 && !specs
->deprecated_p
4662 && !specs
->long_long_p
4665 && !specs
->unsigned_p
4666 && !specs
->complex_p
4668 && !specs
->noreturn_p
4669 && !specs
->thread_p
);
4673 /* Construct an array declarator. LOC is the location of the
4674 beginning of the array (usually the opening brace). EXPR is the
4675 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4676 inside the [] (to be applied to the pointer to which a parameter
4677 array is converted). STATIC_P is true if "static" is inside the
4678 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4679 VLA of unspecified length which is nevertheless a complete type,
4680 false otherwise. The field for the contained declarator is left to
4681 be filled in by set_array_declarator_inner. */
4683 struct c_declarator
*
4684 build_array_declarator (location_t loc
,
4685 tree expr
, struct c_declspecs
*quals
, bool static_p
,
4688 struct c_declarator
*declarator
= XOBNEW (&parser_obstack
,
4689 struct c_declarator
);
4690 declarator
->id_loc
= loc
;
4691 declarator
->kind
= cdk_array
;
4692 declarator
->declarator
= 0;
4693 declarator
->u
.array
.dimen
= expr
;
4696 declarator
->u
.array
.attrs
= quals
->attrs
;
4697 declarator
->u
.array
.quals
= quals_from_declspecs (quals
);
4701 declarator
->u
.array
.attrs
= NULL_TREE
;
4702 declarator
->u
.array
.quals
= 0;
4704 declarator
->u
.array
.static_p
= static_p
;
4705 declarator
->u
.array
.vla_unspec_p
= vla_unspec_p
;
4706 if (static_p
|| quals
!= NULL
)
4707 pedwarn_c90 (loc
, OPT_Wpedantic
,
4708 "ISO C90 does not support %<static%> or type "
4709 "qualifiers in parameter array declarators");
4711 pedwarn_c90 (loc
, OPT_Wpedantic
,
4712 "ISO C90 does not support %<[*]%> array declarators");
4715 if (!current_scope
->parm_flag
)
4718 error_at (loc
, "%<[*]%> not allowed in other than "
4719 "function prototype scope");
4720 declarator
->u
.array
.vla_unspec_p
= false;
4723 current_scope
->had_vla_unspec
= true;
4728 /* Set the contained declarator of an array declarator. DECL is the
4729 declarator, as constructed by build_array_declarator; INNER is what
4730 appears on the left of the []. */
4732 struct c_declarator
*
4733 set_array_declarator_inner (struct c_declarator
*decl
,
4734 struct c_declarator
*inner
)
4736 decl
->declarator
= inner
;
4740 /* INIT is a constructor that forms DECL's initializer. If the final
4741 element initializes a flexible array field, add the size of that
4742 initializer to DECL's size. */
4745 add_flexible_array_elts_to_size (tree decl
, tree init
)
4749 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
4752 elt
= CONSTRUCTOR_ELTS (init
)->last ().value
;
4753 type
= TREE_TYPE (elt
);
4754 if (TREE_CODE (type
) == ARRAY_TYPE
4755 && TYPE_SIZE (type
) == NULL_TREE
4756 && TYPE_DOMAIN (type
) != NULL_TREE
4757 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
)
4759 complete_array_type (&type
, elt
, false);
4761 = size_binop (PLUS_EXPR
, DECL_SIZE (decl
), TYPE_SIZE (type
));
4762 DECL_SIZE_UNIT (decl
)
4763 = size_binop (PLUS_EXPR
, DECL_SIZE_UNIT (decl
), TYPE_SIZE_UNIT (type
));
4767 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4768 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4769 before the type name, and set *EXPR_CONST_OPERANDS, if
4770 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4771 appear in a constant expression. */
4774 groktypename (struct c_type_name
*type_name
, tree
*expr
,
4775 bool *expr_const_operands
)
4778 tree attrs
= type_name
->specs
->attrs
;
4780 type_name
->specs
->attrs
= NULL_TREE
;
4782 type
= grokdeclarator (type_name
->declarator
, type_name
->specs
, TYPENAME
,
4783 false, NULL
, &attrs
, expr
, expr_const_operands
,
4786 /* Apply attributes. */
4787 decl_attributes (&type
, attrs
, 0);
4792 /* Wrapper for decl_attributes that adds some implicit attributes
4793 to VAR_DECLs or FUNCTION_DECLs. */
4796 c_decl_attributes (tree
*node
, tree attributes
, int flags
)
4798 /* Add implicit "omp declare target" attribute if requested. */
4799 if (current_omp_declare_target_attribute
4800 && ((VAR_P (*node
) && is_global_var (*node
))
4801 || TREE_CODE (*node
) == FUNCTION_DECL
))
4804 && !lang_hooks
.types
.omp_mappable_type (TREE_TYPE (*node
)))
4805 attributes
= tree_cons (get_identifier ("omp declare target implicit"),
4806 NULL_TREE
, attributes
);
4808 attributes
= tree_cons (get_identifier ("omp declare target"),
4809 NULL_TREE
, attributes
);
4812 /* Look up the current declaration with all the attributes merged
4813 so far so that attributes on the current declaration that's
4814 about to be pushed that conflict with the former can be detected,
4815 diagnosed, and rejected as appropriate. */
4816 tree last_decl
= lookup_name (DECL_NAME (*node
));
4818 last_decl
= lookup_name_in_scope (DECL_NAME (*node
), external_scope
);
4820 return decl_attributes (node
, attributes
, flags
, last_decl
);
4824 /* Decode a declarator in an ordinary declaration or data definition.
4825 This is called as soon as the type information and variable name
4826 have been parsed, before parsing the initializer if any.
4827 Here we create the ..._DECL node, fill in its type,
4828 and put it on the list of decls for the current context.
4829 The ..._DECL node is returned as the value.
4831 Exception: for arrays where the length is not specified,
4832 the type is left null, to be filled in by `finish_decl'.
4834 Function definitions do not come here; they go to start_function
4835 instead. However, external and forward declarations of functions
4836 do go through here. Structure field declarations are done by
4837 grokfield and not through here. */
4840 start_decl (struct c_declarator
*declarator
, struct c_declspecs
*declspecs
,
4841 bool initialized
, tree attributes
)
4845 tree expr
= NULL_TREE
;
4846 enum deprecated_states deprecated_state
= DEPRECATED_NORMAL
;
4848 /* An object declared as __attribute__((deprecated)) suppresses
4849 warnings of uses of other deprecated items. */
4850 if (lookup_attribute ("deprecated", attributes
))
4851 deprecated_state
= DEPRECATED_SUPPRESS
;
4853 decl
= grokdeclarator (declarator
, declspecs
,
4854 NORMAL
, initialized
, NULL
, &attributes
, &expr
, NULL
,
4856 if (!decl
|| decl
== error_mark_node
)
4860 add_stmt (fold_convert (void_type_node
, expr
));
4862 if (TREE_CODE (decl
) != FUNCTION_DECL
&& MAIN_NAME_P (DECL_NAME (decl
))
4863 && TREE_PUBLIC (decl
))
4864 warning (OPT_Wmain
, "%q+D is usually a function", decl
);
4867 /* Is it valid for this decl to have an initializer at all?
4868 If not, set INITIALIZED to zero, which will indirectly
4869 tell 'finish_decl' to ignore the initializer once it is parsed. */
4870 switch (TREE_CODE (decl
))
4873 error ("typedef %qD is initialized (use __typeof__ instead)", decl
);
4874 initialized
= false;
4878 error ("function %qD is initialized like a variable", decl
);
4879 initialized
= false;
4883 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4884 error ("parameter %qD is initialized", decl
);
4885 initialized
= false;
4889 /* Don't allow initializations for incomplete types except for
4890 arrays which might be completed by the initialization. */
4892 /* This can happen if the array size is an undefined macro.
4893 We already gave a warning, so we don't need another one. */
4894 if (TREE_TYPE (decl
) == error_mark_node
)
4895 initialized
= false;
4896 else if (COMPLETE_TYPE_P (TREE_TYPE (decl
)))
4898 /* A complete type is ok if size is fixed. */
4900 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl
))) != INTEGER_CST
4901 || C_DECL_VARIABLE_SIZE (decl
))
4903 error ("variable-sized object may not be initialized");
4904 initialized
= false;
4907 else if (TREE_CODE (TREE_TYPE (decl
)) != ARRAY_TYPE
)
4909 error ("variable %qD has initializer but incomplete type", decl
);
4910 initialized
= false;
4912 else if (C_DECL_VARIABLE_SIZE (decl
))
4914 /* Although C99 is unclear about whether incomplete arrays
4915 of VLAs themselves count as VLAs, it does not make
4916 sense to permit them to be initialized given that
4917 ordinary VLAs may not be initialized. */
4918 error ("variable-sized object may not be initialized");
4919 initialized
= false;
4925 if (current_scope
== file_scope
)
4926 TREE_STATIC (decl
) = 1;
4928 /* Tell 'pushdecl' this is an initialized decl
4929 even though we don't yet have the initializer expression.
4930 Also tell 'finish_decl' it may store the real initializer. */
4931 DECL_INITIAL (decl
) = error_mark_node
;
4934 /* If this is a function declaration, write a record describing it to the
4935 prototypes file (if requested). */
4937 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4938 gen_aux_info_record (decl
, 0, 0, prototype_p (TREE_TYPE (decl
)));
4940 /* ANSI specifies that a tentative definition which is not merged with
4941 a non-tentative definition behaves exactly like a definition with an
4942 initializer equal to zero. (Section 3.7.2)
4944 -fno-common gives strict ANSI behavior, though this tends to break
4945 a large body of code that grew up without this rule.
4947 Thread-local variables are never common, since there's no entrenched
4948 body of code to break, and it allows more efficient variable references
4949 in the presence of dynamic linking. */
4953 && TREE_PUBLIC (decl
)
4954 && !DECL_THREAD_LOCAL_P (decl
)
4956 DECL_COMMON (decl
) = 1;
4958 /* Set attributes here so if duplicate decl, will have proper attributes. */
4959 c_decl_attributes (&decl
, attributes
, 0);
4961 /* Handle gnu_inline attribute. */
4962 if (declspecs
->inline_p
4963 && !flag_gnu89_inline
4964 && TREE_CODE (decl
) == FUNCTION_DECL
4965 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl
))
4966 || current_function_decl
))
4968 if (declspecs
->storage_class
== csc_auto
&& current_scope
!= file_scope
)
4970 else if (declspecs
->storage_class
!= csc_static
)
4971 DECL_EXTERNAL (decl
) = !DECL_EXTERNAL (decl
);
4974 if (TREE_CODE (decl
) == FUNCTION_DECL
4975 && targetm
.calls
.promote_prototypes (TREE_TYPE (decl
)))
4977 struct c_declarator
*ce
= declarator
;
4979 if (ce
->kind
== cdk_pointer
)
4980 ce
= declarator
->declarator
;
4981 if (ce
->kind
== cdk_function
)
4983 tree args
= ce
->u
.arg_info
->parms
;
4984 for (; args
; args
= DECL_CHAIN (args
))
4986 tree type
= TREE_TYPE (args
);
4987 if (type
&& INTEGRAL_TYPE_P (type
)
4988 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
4989 DECL_ARG_TYPE (args
) = c_type_promotes_to (type
);
4994 if (TREE_CODE (decl
) == FUNCTION_DECL
4995 && DECL_DECLARED_INLINE_P (decl
)
4996 && DECL_UNINLINABLE (decl
)
4997 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl
)))
4998 warning (OPT_Wattributes
, "inline function %q+D given attribute noinline",
5001 /* C99 6.7.4p3: An inline definition of a function with external
5002 linkage shall not contain a definition of a modifiable object
5003 with static storage duration... */
5005 && current_scope
!= file_scope
5006 && TREE_STATIC (decl
)
5007 && !TREE_READONLY (decl
)
5008 && DECL_DECLARED_INLINE_P (current_function_decl
)
5009 && DECL_EXTERNAL (current_function_decl
))
5010 record_inline_static (input_location
, current_function_decl
,
5011 decl
, csi_modifiable
);
5013 if (c_dialect_objc ()
5014 && VAR_OR_FUNCTION_DECL_P (decl
))
5015 objc_check_global_decl (decl
);
5017 /* Add this decl to the current scope.
5018 TEM may equal DECL or it may be a previous decl of the same name. */
5019 tem
= pushdecl (decl
);
5021 if (initialized
&& DECL_EXTERNAL (tem
))
5023 DECL_EXTERNAL (tem
) = 0;
5024 TREE_STATIC (tem
) = 1;
5030 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5031 DECL or the non-array element type if DECL is an uninitialized array.
5032 If that type has a const member, diagnose this. */
5035 diagnose_uninitialized_cst_member (tree decl
, tree type
)
5038 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
5041 if (TREE_CODE (field
) != FIELD_DECL
)
5043 field_type
= strip_array_types (TREE_TYPE (field
));
5045 if (TYPE_QUALS (field_type
) & TYPE_QUAL_CONST
)
5047 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
5048 "uninitialized const member in %qT is invalid in C++",
5049 strip_array_types (TREE_TYPE (decl
)));
5050 inform (DECL_SOURCE_LOCATION (field
), "%qD should be initialized", field
);
5053 if (RECORD_OR_UNION_TYPE_P (field_type
))
5054 diagnose_uninitialized_cst_member (decl
, field_type
);
5058 /* Finish processing of a declaration;
5059 install its initial value.
5060 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5061 If the length of an array type is not known before,
5062 it must be determined now, from the initial value, or it is an error.
5064 INIT_LOC is the location of the initial value. */
5067 finish_decl (tree decl
, location_t init_loc
, tree init
,
5068 tree origtype
, tree asmspec_tree
)
5071 bool was_incomplete
= (DECL_SIZE (decl
) == NULL_TREE
);
5072 const char *asmspec
= 0;
5074 /* If a name was specified, get the string. */
5075 if (VAR_OR_FUNCTION_DECL_P (decl
)
5076 && DECL_FILE_SCOPE_P (decl
))
5077 asmspec_tree
= maybe_apply_renaming_pragma (decl
, asmspec_tree
);
5079 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
5082 && TREE_STATIC (decl
)
5083 && global_bindings_p ())
5084 /* So decl is a global variable. Record the types it uses
5085 so that we can decide later to emit debug info for them. */
5086 record_types_used_by_current_var_decl (decl
);
5088 /* If `start_decl' didn't like having an initialization, ignore it now. */
5089 if (init
!= NULL_TREE
&& DECL_INITIAL (decl
) == NULL_TREE
)
5092 /* Don't crash if parm is initialized. */
5093 if (TREE_CODE (decl
) == PARM_DECL
)
5097 store_init_value (init_loc
, decl
, init
, origtype
);
5099 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl
)
5100 || TREE_CODE (decl
) == FIELD_DECL
))
5101 objc_check_decl (decl
);
5103 type
= TREE_TYPE (decl
);
5105 /* Deduce size of array from initialization, if not already known.
5106 This is only needed for an initialization in the current scope;
5107 it must not be done for a file-scope initialization of a
5108 declaration with external linkage, redeclared in an inner scope
5109 with the outer declaration shadowed in an intermediate scope. */
5110 if (TREE_CODE (type
) == ARRAY_TYPE
5111 && TYPE_DOMAIN (type
) == NULL_TREE
5112 && TREE_CODE (decl
) != TYPE_DECL
5113 && !(TREE_PUBLIC (decl
) && current_scope
!= file_scope
))
5116 = (TREE_STATIC (decl
)
5117 /* Even if pedantic, an external linkage array
5118 may have incomplete type at first. */
5119 ? pedantic
&& !TREE_PUBLIC (decl
)
5120 : !DECL_EXTERNAL (decl
));
5122 = complete_array_type (&TREE_TYPE (decl
), DECL_INITIAL (decl
),
5125 /* Get the completed type made by complete_array_type. */
5126 type
= TREE_TYPE (decl
);
5131 error ("initializer fails to determine size of %q+D", decl
);
5136 error ("array size missing in %q+D", decl
);
5140 error ("zero or negative size array %q+D", decl
);
5144 /* For global variables, update the copy of the type that
5145 exists in the binding. */
5146 if (TREE_PUBLIC (decl
))
5148 struct c_binding
*b_ext
= I_SYMBOL_BINDING (DECL_NAME (decl
));
5149 while (b_ext
&& !B_IN_EXTERNAL_SCOPE (b_ext
))
5150 b_ext
= b_ext
->shadowed
;
5151 if (b_ext
&& TREE_CODE (decl
) == TREE_CODE (b_ext
->decl
))
5153 if (b_ext
->u
.type
&& comptypes (b_ext
->u
.type
, type
))
5154 b_ext
->u
.type
= composite_type (b_ext
->u
.type
, type
);
5156 b_ext
->u
.type
= type
;
5165 if (DECL_INITIAL (decl
))
5166 TREE_TYPE (DECL_INITIAL (decl
)) = type
;
5168 relayout_decl (decl
);
5171 /* Look for braced array initializers for character arrays and
5172 recursively convert them into STRING_CSTs. */
5173 if (tree init
= DECL_INITIAL (decl
))
5174 DECL_INITIAL (decl
) = braced_lists_to_strings (type
, init
);
5178 if (init
&& TREE_CODE (init
) == CONSTRUCTOR
)
5179 add_flexible_array_elts_to_size (decl
, init
);
5181 complete_flexible_array_elts (DECL_INITIAL (decl
));
5183 if (DECL_SIZE (decl
) == NULL_TREE
&& TREE_TYPE (decl
) != error_mark_node
5184 && COMPLETE_TYPE_P (TREE_TYPE (decl
)))
5185 layout_decl (decl
, 0);
5187 if (DECL_SIZE (decl
) == NULL_TREE
5188 /* Don't give an error if we already gave one earlier. */
5189 && TREE_TYPE (decl
) != error_mark_node
5190 && (TREE_STATIC (decl
)
5191 /* A static variable with an incomplete type
5192 is an error if it is initialized.
5193 Also if it is not file scope.
5194 Otherwise, let it through, but if it is not `extern'
5195 then it may cause an error message later. */
5196 ? (DECL_INITIAL (decl
) != NULL_TREE
5197 || !DECL_FILE_SCOPE_P (decl
))
5198 /* An automatic variable with an incomplete type
5200 : !DECL_EXTERNAL (decl
)))
5202 error ("storage size of %q+D isn%'t known", decl
);
5203 TREE_TYPE (decl
) = error_mark_node
;
5206 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
))
5207 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
5208 && DECL_SIZE (decl
) == NULL_TREE
5209 && TREE_STATIC (decl
))
5210 incomplete_record_decls
.safe_push (decl
);
5212 if (is_global_var (decl
) && DECL_SIZE (decl
) != NULL_TREE
)
5214 if (TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
)
5215 constant_expression_warning (DECL_SIZE (decl
));
5218 error ("storage size of %q+D isn%'t constant", decl
);
5219 TREE_TYPE (decl
) = error_mark_node
;
5223 if (TREE_USED (type
))
5225 TREE_USED (decl
) = 1;
5226 DECL_READ_P (decl
) = 1;
5230 /* If this is a function and an assembler name is specified, reset DECL_RTL
5231 so we can give it its new name. Also, update builtin_decl if it
5232 was a normal built-in. */
5233 if (TREE_CODE (decl
) == FUNCTION_DECL
&& asmspec
)
5235 if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
5236 set_builtin_user_assembler_name (decl
, asmspec
);
5237 set_user_assembler_name (decl
, asmspec
);
5240 /* If #pragma weak was used, mark the decl weak now. */
5241 maybe_apply_pragma_weak (decl
);
5243 /* Output the assembler code and/or RTL code for variables and functions,
5244 unless the type is an undefined structure or union.
5245 If not, it will get done when the type is completed. */
5247 if (VAR_OR_FUNCTION_DECL_P (decl
))
5249 /* Determine the ELF visibility. */
5250 if (TREE_PUBLIC (decl
))
5251 c_determine_visibility (decl
);
5253 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5254 if (c_dialect_objc ())
5255 objc_check_decl (decl
);
5259 /* If this is not a static variable, issue a warning.
5260 It doesn't make any sense to give an ASMSPEC for an
5261 ordinary, non-register local variable. Historically,
5262 GCC has accepted -- but ignored -- the ASMSPEC in
5264 if (!DECL_FILE_SCOPE_P (decl
)
5266 && !C_DECL_REGISTER (decl
)
5267 && !TREE_STATIC (decl
))
5268 warning (0, "ignoring asm-specifier for non-static local "
5269 "variable %q+D", decl
);
5271 set_user_assembler_name (decl
, asmspec
);
5274 if (DECL_FILE_SCOPE_P (decl
))
5276 if (DECL_INITIAL (decl
) == NULL_TREE
5277 || DECL_INITIAL (decl
) == error_mark_node
)
5278 /* Don't output anything
5279 when a tentative file-scope definition is seen.
5280 But at end of compilation, do output code for them. */
5281 DECL_DEFER_OUTPUT (decl
) = 1;
5282 if (asmspec
&& VAR_P (decl
) && C_DECL_REGISTER (decl
))
5283 DECL_HARD_REGISTER (decl
) = 1;
5284 rest_of_decl_compilation (decl
, true, 0);
5288 /* In conjunction with an ASMSPEC, the `register'
5289 keyword indicates that we should place the variable
5290 in a particular register. */
5291 if (asmspec
&& C_DECL_REGISTER (decl
))
5293 DECL_HARD_REGISTER (decl
) = 1;
5294 /* This cannot be done for a structure with volatile
5295 fields, on which DECL_REGISTER will have been
5297 if (!DECL_REGISTER (decl
))
5298 error ("cannot put object with volatile field into register");
5301 if (TREE_CODE (decl
) != FUNCTION_DECL
)
5303 /* If we're building a variable sized type, and we might be
5304 reachable other than via the top of the current binding
5305 level, then create a new BIND_EXPR so that we deallocate
5306 the object at the right time. */
5307 /* Note that DECL_SIZE can be null due to errors. */
5308 if (DECL_SIZE (decl
)
5309 && !TREE_CONSTANT (DECL_SIZE (decl
))
5310 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list
))
5313 bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, NULL
);
5314 TREE_SIDE_EFFECTS (bind
) = 1;
5316 BIND_EXPR_BODY (bind
) = push_stmt_list ();
5318 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl
),
5324 if (!DECL_FILE_SCOPE_P (decl
))
5326 /* Recompute the RTL of a local array now
5327 if it used to be an incomplete type. */
5328 if (was_incomplete
&& !is_global_var (decl
))
5330 /* If we used it already as memory, it must stay in memory. */
5331 TREE_ADDRESSABLE (decl
) = TREE_USED (decl
);
5332 /* If it's still incomplete now, no init will save it. */
5333 if (DECL_SIZE (decl
) == NULL_TREE
)
5334 DECL_INITIAL (decl
) = NULL_TREE
;
5339 if (TREE_CODE (decl
) == TYPE_DECL
)
5341 if (!DECL_FILE_SCOPE_P (decl
)
5342 && variably_modified_type_p (TREE_TYPE (decl
), NULL_TREE
))
5343 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl
), DECL_EXPR
, decl
));
5345 rest_of_decl_compilation (decl
, DECL_FILE_SCOPE_P (decl
), 0);
5348 /* Install a cleanup (aka destructor) if one was given. */
5349 if (VAR_P (decl
) && !TREE_STATIC (decl
))
5351 tree attr
= lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl
));
5354 tree cleanup_id
= TREE_VALUE (TREE_VALUE (attr
));
5355 tree cleanup_decl
= lookup_name (cleanup_id
);
5357 vec
<tree
, va_gc
> *v
;
5359 /* Build "cleanup(&decl)" for the destructor. */
5360 cleanup
= build_unary_op (input_location
, ADDR_EXPR
, decl
, false);
5362 v
->quick_push (cleanup
);
5363 cleanup
= c_build_function_call_vec (DECL_SOURCE_LOCATION (decl
),
5364 vNULL
, cleanup_decl
, v
, NULL
);
5367 /* Don't warn about decl unused; the cleanup uses it. */
5368 TREE_USED (decl
) = 1;
5369 TREE_USED (cleanup_decl
) = 1;
5370 DECL_READ_P (decl
) = 1;
5372 push_cleanup (decl
, cleanup
, false);
5378 && !DECL_EXTERNAL (decl
)
5379 && DECL_INITIAL (decl
) == NULL_TREE
)
5381 type
= strip_array_types (type
);
5382 if (TREE_READONLY (decl
))
5383 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
5384 "uninitialized const %qD is invalid in C++", decl
);
5385 else if (RECORD_OR_UNION_TYPE_P (type
)
5386 && C_TYPE_FIELDS_READONLY (type
))
5387 diagnose_uninitialized_cst_member (decl
, type
);
5392 && lookup_attribute ("omp declare target implicit",
5393 DECL_ATTRIBUTES (decl
)))
5395 DECL_ATTRIBUTES (decl
)
5396 = remove_attribute ("omp declare target implicit",
5397 DECL_ATTRIBUTES (decl
));
5398 if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (decl
)))
5399 error ("%q+D in declare target directive does not have mappable type",
5401 else if (!lookup_attribute ("omp declare target",
5402 DECL_ATTRIBUTES (decl
))
5403 && !lookup_attribute ("omp declare target link",
5404 DECL_ATTRIBUTES (decl
)))
5405 DECL_ATTRIBUTES (decl
)
5406 = tree_cons (get_identifier ("omp declare target"),
5407 NULL_TREE
, DECL_ATTRIBUTES (decl
));
5410 invoke_plugin_callbacks (PLUGIN_FINISH_DECL
, decl
);
5413 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5414 EXPR is NULL or a pointer to an expression that needs to be
5415 evaluated for the side effects of array size expressions in the
5419 grokparm (const struct c_parm
*parm
, tree
*expr
)
5421 tree attrs
= parm
->attrs
;
5422 tree decl
= grokdeclarator (parm
->declarator
, parm
->specs
, PARM
, false,
5423 NULL
, &attrs
, expr
, NULL
, DEPRECATED_NORMAL
);
5425 decl_attributes (&decl
, attrs
, 0);
5430 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5431 and push that on the current scope. EXPR is a pointer to an
5432 expression that needs to be evaluated for the side effects of array
5433 size expressions in the parameters. */
5436 push_parm_decl (const struct c_parm
*parm
, tree
*expr
)
5438 tree attrs
= parm
->attrs
;
5441 decl
= grokdeclarator (parm
->declarator
, parm
->specs
, PARM
, false, NULL
,
5442 &attrs
, expr
, NULL
, DEPRECATED_NORMAL
);
5443 if (decl
&& DECL_P (decl
))
5444 DECL_SOURCE_LOCATION (decl
) = parm
->loc
;
5445 decl_attributes (&decl
, attrs
, 0);
5447 decl
= pushdecl (decl
);
5449 finish_decl (decl
, input_location
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
5452 /* Mark all the parameter declarations to date as forward decls.
5453 Also diagnose use of this extension. */
5456 mark_forward_parm_decls (void)
5458 struct c_binding
*b
;
5460 if (pedantic
&& !current_scope
->warned_forward_parm_decls
)
5462 pedwarn (input_location
, OPT_Wpedantic
,
5463 "ISO C forbids forward parameter declarations");
5464 current_scope
->warned_forward_parm_decls
= true;
5467 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
5468 if (TREE_CODE (b
->decl
) == PARM_DECL
)
5469 TREE_ASM_WRITTEN (b
->decl
) = 1;
5472 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5473 literal, which may be an incomplete array type completed by the
5474 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5475 literal. NON_CONST is true if the initializers contain something
5476 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
5477 it is the (valid) alignment for this compound literal, as specified
5481 build_compound_literal (location_t loc
, tree type
, tree init
, bool non_const
,
5482 unsigned int alignas_align
)
5484 /* We do not use start_decl here because we have a type, not a declarator;
5485 and do not use finish_decl because the decl should be stored inside
5486 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5491 if (type
== error_mark_node
5492 || init
== error_mark_node
)
5493 return error_mark_node
;
5495 decl
= build_decl (loc
, VAR_DECL
, NULL_TREE
, type
);
5496 DECL_EXTERNAL (decl
) = 0;
5497 TREE_PUBLIC (decl
) = 0;
5498 TREE_STATIC (decl
) = (current_scope
== file_scope
);
5499 DECL_CONTEXT (decl
) = current_function_decl
;
5500 TREE_USED (decl
) = 1;
5501 DECL_READ_P (decl
) = 1;
5502 DECL_ARTIFICIAL (decl
) = 1;
5503 DECL_IGNORED_P (decl
) = 1;
5504 C_DECL_COMPOUND_LITERAL_P (decl
) = 1;
5505 TREE_TYPE (decl
) = type
;
5506 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type
)), decl
);
5509 SET_DECL_ALIGN (decl
, alignas_align
* BITS_PER_UNIT
);
5510 DECL_USER_ALIGN (decl
) = 1;
5512 store_init_value (loc
, decl
, init
, NULL_TREE
);
5514 if (TREE_CODE (type
) == ARRAY_TYPE
&& !COMPLETE_TYPE_P (type
))
5516 int failure
= complete_array_type (&TREE_TYPE (decl
),
5517 DECL_INITIAL (decl
), true);
5518 /* If complete_array_type returns 3, it means that the
5519 initial value of the compound literal is empty. Allow it. */
5520 gcc_assert (failure
== 0 || failure
== 3);
5522 type
= TREE_TYPE (decl
);
5523 TREE_TYPE (DECL_INITIAL (decl
)) = type
;
5526 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
5528 c_incomplete_type_error (loc
, NULL_TREE
, type
);
5529 return error_mark_node
;
5532 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), DECL_EXPR
, decl
);
5533 complit
= build1 (COMPOUND_LITERAL_EXPR
, type
, stmt
);
5534 TREE_SIDE_EFFECTS (complit
) = 1;
5536 layout_decl (decl
, 0);
5538 if (TREE_STATIC (decl
))
5540 /* This decl needs a name for the assembler output. */
5541 set_compound_literal_name (decl
);
5542 DECL_DEFER_OUTPUT (decl
) = 1;
5543 DECL_COMDAT (decl
) = 1;
5545 rest_of_decl_compilation (decl
, 1, 0);
5547 else if (current_function_decl
&& !current_scope
->parm_flag
)
5552 complit
= build2 (C_MAYBE_CONST_EXPR
, type
, NULL
, complit
);
5553 C_MAYBE_CONST_EXPR_NON_CONST (complit
) = 1;
5559 /* Check the type of a compound literal. Here we just check that it
5560 is valid for C++. */
5563 check_compound_literal_type (location_t loc
, struct c_type_name
*type_name
)
5566 && (type_name
->specs
->typespec_kind
== ctsk_tagdef
5567 || type_name
->specs
->typespec_kind
== ctsk_tagfirstref
))
5568 warning_at (loc
, OPT_Wc___compat
,
5569 "defining a type in a compound literal is invalid in C++");
5572 /* Determine whether TYPE is a structure with a flexible array member,
5573 or a union containing such a structure (possibly recursively). */
5576 flexible_array_type_p (tree type
)
5579 switch (TREE_CODE (type
))
5582 x
= TYPE_FIELDS (type
);
5585 while (DECL_CHAIN (x
) != NULL_TREE
)
5587 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
5588 && TYPE_SIZE (TREE_TYPE (x
)) == NULL_TREE
5589 && TYPE_DOMAIN (TREE_TYPE (x
)) != NULL_TREE
5590 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x
))) == NULL_TREE
)
5594 for (x
= TYPE_FIELDS (type
); x
!= NULL_TREE
; x
= DECL_CHAIN (x
))
5596 if (flexible_array_type_p (TREE_TYPE (x
)))
5605 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5606 replacing with appropriate values if they are invalid. */
5609 check_bitfield_type_and_width (location_t loc
, tree
*type
, tree
*width
,
5613 unsigned int max_width
;
5614 unsigned HOST_WIDE_INT w
;
5615 const char *name
= (orig_name
5616 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name
))
5617 : _("<anonymous>"));
5619 /* Detect and ignore out of range field width and process valid
5621 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width
)))
5623 error_at (loc
, "bit-field %qs width not an integer constant", name
);
5624 *width
= integer_one_node
;
5628 if (TREE_CODE (*width
) != INTEGER_CST
)
5630 *width
= c_fully_fold (*width
, false, NULL
);
5631 if (TREE_CODE (*width
) == INTEGER_CST
)
5632 pedwarn (loc
, OPT_Wpedantic
,
5633 "bit-field %qs width not an integer constant expression",
5636 if (TREE_CODE (*width
) != INTEGER_CST
)
5638 error_at (loc
, "bit-field %qs width not an integer constant", name
);
5639 *width
= integer_one_node
;
5641 constant_expression_warning (*width
);
5642 if (tree_int_cst_sgn (*width
) < 0)
5644 error_at (loc
, "negative width in bit-field %qs", name
);
5645 *width
= integer_one_node
;
5647 else if (integer_zerop (*width
) && orig_name
)
5649 error_at (loc
, "zero width for bit-field %qs", name
);
5650 *width
= integer_one_node
;
5654 /* Detect invalid bit-field type. */
5655 if (TREE_CODE (*type
) != INTEGER_TYPE
5656 && TREE_CODE (*type
) != BOOLEAN_TYPE
5657 && TREE_CODE (*type
) != ENUMERAL_TYPE
)
5659 error_at (loc
, "bit-field %qs has invalid type", name
);
5660 *type
= unsigned_type_node
;
5663 if (TYPE_WARN_IF_NOT_ALIGN (*type
))
5665 error_at (loc
, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
5667 *type
= unsigned_type_node
;
5670 type_mv
= TYPE_MAIN_VARIANT (*type
);
5671 if (!in_system_header_at (input_location
)
5672 && type_mv
!= integer_type_node
5673 && type_mv
!= unsigned_type_node
5674 && type_mv
!= boolean_type_node
)
5675 pedwarn_c90 (loc
, OPT_Wpedantic
,
5676 "type of bit-field %qs is a GCC extension", name
);
5678 max_width
= TYPE_PRECISION (*type
);
5680 if (compare_tree_int (*width
, max_width
) > 0)
5682 error_at (loc
, "width of %qs exceeds its type", name
);
5684 *width
= build_int_cst (integer_type_node
, w
);
5687 w
= tree_to_uhwi (*width
);
5689 if (TREE_CODE (*type
) == ENUMERAL_TYPE
)
5691 struct lang_type
*lt
= TYPE_LANG_SPECIFIC (*type
);
5693 || w
< tree_int_cst_min_precision (lt
->enum_min
, TYPE_SIGN (*type
))
5694 || w
< tree_int_cst_min_precision (lt
->enum_max
, TYPE_SIGN (*type
)))
5695 warning_at (loc
, 0, "%qs is narrower than values of its type", name
);
5701 /* Print warning about variable length array if necessary. */
5704 warn_variable_length_array (tree name
, tree size
)
5706 if (TREE_CONSTANT (size
))
5709 pedwarn_c90 (input_location
, OPT_Wvla
,
5710 "ISO C90 forbids array %qE whose size "
5711 "can%'t be evaluated", name
);
5713 pedwarn_c90 (input_location
, OPT_Wvla
, "ISO C90 forbids array "
5714 "whose size can%'t be evaluated");
5719 pedwarn_c90 (input_location
, OPT_Wvla
,
5720 "ISO C90 forbids variable length array %qE", name
);
5722 pedwarn_c90 (input_location
, OPT_Wvla
, "ISO C90 forbids variable "
5727 /* Print warning about defaulting to int if necessary. */
5730 warn_defaults_to (location_t location
, int opt
, const char *gmsgid
, ...)
5732 diagnostic_info diagnostic
;
5734 rich_location
richloc (line_table
, location
);
5736 va_start (ap
, gmsgid
);
5737 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
,
5738 flag_isoc99
? DK_PEDWARN
: DK_WARNING
);
5739 diagnostic
.option_index
= opt
;
5740 diagnostic_report_diagnostic (global_dc
, &diagnostic
);
5744 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
5745 considering only those c_declspec_words found in LIST, which
5746 must be terminated by cdw_number_of_elements. */
5749 smallest_type_quals_location (const location_t
*locations
,
5750 const c_declspec_word
*list
)
5752 location_t loc
= UNKNOWN_LOCATION
;
5753 while (*list
!= cdw_number_of_elements
)
5755 location_t newloc
= locations
[*list
];
5756 if (loc
== UNKNOWN_LOCATION
5757 || (newloc
!= UNKNOWN_LOCATION
&& newloc
< loc
))
5765 /* Given declspecs and a declarator,
5766 determine the name and type of the object declared
5767 and construct a ..._DECL node for it.
5768 (In one case we can return a ..._TYPE node instead.
5769 For invalid input we sometimes return NULL_TREE.)
5771 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5773 DECL_CONTEXT says which syntactic context this declaration is in:
5774 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5775 FUNCDEF for a function definition. Like NORMAL but a few different
5776 error messages in each case. Return value may be zero meaning
5777 this definition is too screwy to try to parse.
5778 PARM for a parameter declaration (either within a function prototype
5779 or before a function body). Make a PARM_DECL, or return void_type_node.
5780 TYPENAME if for a typename (in a cast or sizeof).
5781 Don't make a DECL node; just return the ..._TYPE node.
5782 FIELD for a struct or union field; make a FIELD_DECL.
5783 INITIALIZED is true if the decl has an initializer.
5784 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5785 representing the width of the bit-field.
5786 DECL_ATTRS points to the list of attributes that should be added to this
5787 decl. Any nested attributes that belong on the decl itself will be
5789 If EXPR is not NULL, any expressions that need to be evaluated as
5790 part of evaluating variably modified types will be stored in *EXPR.
5791 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5792 set to indicate whether operands in *EXPR can be used in constant
5794 DEPRECATED_STATE is a deprecated_states value indicating whether
5795 deprecation warnings should be suppressed.
5797 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5798 It may also be so in the PARM case, for a prototype where the
5799 argument type is specified but not the name.
5801 This function is where the complicated C meanings of `static'
5802 and `extern' are interpreted. */
5805 grokdeclarator (const struct c_declarator
*declarator
,
5806 struct c_declspecs
*declspecs
,
5807 enum decl_context decl_context
, bool initialized
, tree
*width
,
5808 tree
*decl_attrs
, tree
*expr
, bool *expr_const_operands
,
5809 enum deprecated_states deprecated_state
)
5811 tree type
= declspecs
->type
;
5812 bool threadp
= declspecs
->thread_p
;
5813 enum c_storage_class storage_class
= declspecs
->storage_class
;
5818 int type_quals
= TYPE_UNQUALIFIED
;
5819 tree name
= NULL_TREE
;
5820 bool funcdef_flag
= false;
5821 bool funcdef_syntax
= false;
5822 bool size_varies
= false;
5823 tree decl_attr
= declspecs
->decl_attr
;
5824 int array_ptr_quals
= TYPE_UNQUALIFIED
;
5825 tree array_ptr_attrs
= NULL_TREE
;
5826 bool array_parm_static
= false;
5827 bool array_parm_vla_unspec_p
= false;
5828 tree returned_attrs
= NULL_TREE
;
5829 bool bitfield
= width
!= NULL
;
5831 tree orig_qual_type
= NULL
;
5832 size_t orig_qual_indirect
= 0;
5833 struct c_arg_info
*arg_info
= 0;
5834 addr_space_t as1
, as2
, address_space
;
5835 location_t loc
= UNKNOWN_LOCATION
;
5837 bool expr_const_operands_dummy
;
5838 enum c_declarator_kind first_non_attr_kind
;
5839 unsigned int alignas_align
= 0;
5841 if (TREE_CODE (type
) == ERROR_MARK
)
5842 return error_mark_node
;
5846 expr_dummy
= NULL_TREE
;
5848 if (expr_const_operands
== NULL
)
5849 expr_const_operands
= &expr_const_operands_dummy
;
5851 if (declspecs
->expr
)
5854 *expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (declspecs
->expr
), *expr
,
5857 *expr
= declspecs
->expr
;
5859 *expr_const_operands
= declspecs
->expr_const_operands
;
5861 if (decl_context
== FUNCDEF
)
5862 funcdef_flag
= true, decl_context
= NORMAL
;
5864 /* Look inside a declarator for the name being declared
5865 and get it as an IDENTIFIER_NODE, for an error message. */
5867 const struct c_declarator
*decl
= declarator
;
5869 first_non_attr_kind
= cdk_attrs
;
5879 funcdef_syntax
= (decl
->kind
== cdk_function
);
5880 if (first_non_attr_kind
== cdk_attrs
)
5881 first_non_attr_kind
= decl
->kind
;
5882 decl
= decl
->declarator
;
5886 decl
= decl
->declarator
;
5893 if (first_non_attr_kind
== cdk_attrs
)
5894 first_non_attr_kind
= decl
->kind
;
5901 if (name
== NULL_TREE
)
5903 gcc_assert (decl_context
== PARM
5904 || decl_context
== TYPENAME
5905 || (decl_context
== FIELD
5906 && declarator
->kind
== cdk_id
));
5907 gcc_assert (!initialized
);
5911 /* A function definition's declarator must have the form of
5912 a function declarator. */
5914 if (funcdef_flag
&& !funcdef_syntax
)
5917 /* If this looks like a function definition, make it one,
5918 even if it occurs where parms are expected.
5919 Then store_parm_decls will reject it and not use it as a parm. */
5920 if (decl_context
== NORMAL
&& !funcdef_flag
&& current_scope
->parm_flag
)
5921 decl_context
= PARM
;
5923 if (declspecs
->deprecated_p
&& deprecated_state
!= DEPRECATED_SUPPRESS
)
5924 warn_deprecated_use (declspecs
->type
, declspecs
->decl_attr
);
5926 if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
5927 && current_scope
== file_scope
5928 && variably_modified_type_p (type
, NULL_TREE
))
5931 error_at (loc
, "variably modified %qE at file scope", name
);
5933 error_at (loc
, "variably modified field at file scope");
5934 type
= integer_type_node
;
5937 size_varies
= C_TYPE_VARIABLE_SIZE (type
) != 0;
5939 /* Diagnose defaulting to "int". */
5941 if (declspecs
->default_int_p
&& !in_system_header_at (input_location
))
5943 /* Issue a warning if this is an ISO C 99 program or if
5944 -Wreturn-type and this is a function, or if -Wimplicit;
5945 prefer the former warning since it is more explicit. */
5946 if ((warn_implicit_int
|| warn_return_type
> 0 || flag_isoc99
)
5948 warn_about_return_type
= 1;
5952 warn_defaults_to (loc
, OPT_Wimplicit_int
,
5953 "type defaults to %<int%> in declaration "
5956 warn_defaults_to (loc
, OPT_Wimplicit_int
,
5957 "type defaults to %<int%> in type name");
5961 /* Adjust the type if a bit-field is being declared,
5962 -funsigned-bitfields applied and the type is not explicitly
5964 if (bitfield
&& !flag_signed_bitfields
&& !declspecs
->explicit_signed_p
5965 && TREE_CODE (type
) == INTEGER_TYPE
)
5966 type
= unsigned_type_for (type
);
5968 /* Figure out the type qualifiers for the declaration. There are
5969 two ways a declaration can become qualified. One is something
5970 like `const int i' where the `const' is explicit. Another is
5971 something like `typedef const int CI; CI i' where the type of the
5972 declaration contains the `const'. A third possibility is that
5973 there is a type qualifier on the element type of a typedefed
5974 array type, in which case we should extract that qualifier so
5975 that c_apply_type_quals_to_decl receives the full list of
5976 qualifiers to work with (C90 is not entirely clear about whether
5977 duplicate qualifiers should be diagnosed in this case, but it
5978 seems most appropriate to do so). */
5979 element_type
= strip_array_types (type
);
5980 constp
= declspecs
->const_p
+ TYPE_READONLY (element_type
);
5981 restrictp
= declspecs
->restrict_p
+ TYPE_RESTRICT (element_type
);
5982 volatilep
= declspecs
->volatile_p
+ TYPE_VOLATILE (element_type
);
5983 atomicp
= declspecs
->atomic_p
+ TYPE_ATOMIC (element_type
);
5984 as1
= declspecs
->address_space
;
5985 as2
= TYPE_ADDR_SPACE (element_type
);
5986 address_space
= ADDR_SPACE_GENERIC_P (as1
)? as2
: as1
;
5989 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<const%>");
5991 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<restrict%>");
5993 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<volatile%>");
5995 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<_Atomic%>");
5997 if (!ADDR_SPACE_GENERIC_P (as1
) && !ADDR_SPACE_GENERIC_P (as2
) && as1
!= as2
)
5998 error_at (loc
, "conflicting named address spaces (%s vs %s)",
5999 c_addr_space_name (as1
), c_addr_space_name (as2
));
6001 if ((TREE_CODE (type
) == ARRAY_TYPE
6002 || first_non_attr_kind
== cdk_array
)
6003 && TYPE_QUALS (element_type
))
6005 orig_qual_type
= type
;
6006 type
= TYPE_MAIN_VARIANT (type
);
6008 type_quals
= ((constp
? TYPE_QUAL_CONST
: 0)
6009 | (restrictp
? TYPE_QUAL_RESTRICT
: 0)
6010 | (volatilep
? TYPE_QUAL_VOLATILE
: 0)
6011 | (atomicp
? TYPE_QUAL_ATOMIC
: 0)
6012 | ENCODE_QUAL_ADDR_SPACE (address_space
));
6013 if (type_quals
!= TYPE_QUALS (element_type
))
6014 orig_qual_type
= NULL_TREE
;
6016 /* Applying the _Atomic qualifier to an array type (through the use
6017 of typedefs or typeof) must be detected here. If the qualifier
6018 is introduced later, any appearance of applying it to an array is
6019 actually applying it to an element of that array. */
6020 if (declspecs
->atomic_p
&& TREE_CODE (type
) == ARRAY_TYPE
)
6021 error_at (loc
, "%<_Atomic%>-qualified array type");
6023 /* Warn about storage classes that are invalid for certain
6024 kinds of declarations (parameters, typenames, etc.). */
6028 || storage_class
== csc_auto
6029 || storage_class
== csc_register
6030 || storage_class
== csc_typedef
))
6032 if (storage_class
== csc_auto
)
6034 (current_scope
== file_scope
) ? 0 : OPT_Wpedantic
,
6035 "function definition declared %<auto%>");
6036 if (storage_class
== csc_register
)
6037 error_at (loc
, "function definition declared %<register%>");
6038 if (storage_class
== csc_typedef
)
6039 error_at (loc
, "function definition declared %<typedef%>");
6041 error_at (loc
, "function definition declared %qs",
6042 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
6044 if (storage_class
== csc_auto
6045 || storage_class
== csc_register
6046 || storage_class
== csc_typedef
)
6047 storage_class
= csc_none
;
6049 else if (decl_context
!= NORMAL
&& (storage_class
!= csc_none
|| threadp
))
6051 if (decl_context
== PARM
&& storage_class
== csc_register
)
6055 switch (decl_context
)
6059 error_at (loc
, "storage class specified for structure "
6062 error_at (loc
, "storage class specified for structure field");
6066 error_at (loc
, "storage class specified for parameter %qE",
6069 error_at (loc
, "storage class specified for unnamed parameter");
6072 error_at (loc
, "storage class specified for typename");
6075 storage_class
= csc_none
;
6079 else if (storage_class
== csc_extern
6083 /* 'extern' with initialization is invalid if not at file scope. */
6084 if (current_scope
== file_scope
)
6086 /* It is fine to have 'extern const' when compiling at C
6087 and C++ intersection. */
6088 if (!(warn_cxx_compat
&& constp
))
6089 warning_at (loc
, 0, "%qE initialized and declared %<extern%>",
6093 error_at (loc
, "%qE has both %<extern%> and initializer", name
);
6095 else if (current_scope
== file_scope
)
6097 if (storage_class
== csc_auto
)
6098 error_at (loc
, "file-scope declaration of %qE specifies %<auto%>",
6100 if (pedantic
&& storage_class
== csc_register
)
6101 pedwarn (input_location
, OPT_Wpedantic
,
6102 "file-scope declaration of %qE specifies %<register%>", name
);
6106 if (storage_class
== csc_extern
&& funcdef_flag
)
6107 error_at (loc
, "nested function %qE declared %<extern%>", name
);
6108 else if (threadp
&& storage_class
== csc_none
)
6110 error_at (loc
, "function-scope %qE implicitly auto and declared "
6112 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
6117 /* Now figure out the structure of the declarator proper.
6118 Descend through it, creating more complex types, until we reach
6119 the declared identifier (or NULL_TREE, in an absolute declarator).
6120 At each stage we maintain an unqualified version of the type
6121 together with any qualifiers that should be applied to it with
6122 c_build_qualified_type; this way, array types including
6123 multidimensional array types are first built up in unqualified
6124 form and then the qualified form is created with
6125 TYPE_MAIN_VARIANT pointing to the unqualified form. */
6127 while (declarator
&& declarator
->kind
!= cdk_id
)
6129 if (type
== error_mark_node
)
6131 declarator
= declarator
->declarator
;
6135 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
6136 a cdk_pointer (for *...),
6137 a cdk_function (for ...(...)),
6138 a cdk_attrs (for nested attributes),
6139 or a cdk_id (for the name being declared
6140 or the place in an absolute declarator
6141 where the name was omitted).
6142 For the last case, we have just exited the loop.
6144 At this point, TYPE is the type of elements of an array,
6145 or for a function to return, or for a pointer to point to.
6146 After this sequence of ifs, TYPE is the type of the
6147 array or function or pointer, and DECLARATOR has had its
6148 outermost layer removed. */
6150 if (array_ptr_quals
!= TYPE_UNQUALIFIED
6151 || array_ptr_attrs
!= NULL_TREE
6152 || array_parm_static
)
6154 /* Only the innermost declarator (making a parameter be of
6155 array type which is converted to pointer type)
6156 may have static or type qualifiers. */
6157 error_at (loc
, "static or type qualifiers in non-parameter array declarator");
6158 array_ptr_quals
= TYPE_UNQUALIFIED
;
6159 array_ptr_attrs
= NULL_TREE
;
6160 array_parm_static
= false;
6163 switch (declarator
->kind
)
6167 /* A declarator with embedded attributes. */
6168 tree attrs
= declarator
->u
.attrs
;
6169 const struct c_declarator
*inner_decl
;
6171 declarator
= declarator
->declarator
;
6172 inner_decl
= declarator
;
6173 while (inner_decl
->kind
== cdk_attrs
)
6174 inner_decl
= inner_decl
->declarator
;
6175 if (inner_decl
->kind
== cdk_id
)
6176 attr_flags
|= (int) ATTR_FLAG_DECL_NEXT
;
6177 else if (inner_decl
->kind
== cdk_function
)
6178 attr_flags
|= (int) ATTR_FLAG_FUNCTION_NEXT
;
6179 else if (inner_decl
->kind
== cdk_array
)
6180 attr_flags
|= (int) ATTR_FLAG_ARRAY_NEXT
;
6181 returned_attrs
= decl_attributes (&type
,
6182 chainon (returned_attrs
, attrs
),
6188 tree itype
= NULL_TREE
;
6189 tree size
= declarator
->u
.array
.dimen
;
6190 /* The index is a signed object `sizetype' bits wide. */
6191 tree index_type
= c_common_signed_type (sizetype
);
6193 array_ptr_quals
= declarator
->u
.array
.quals
;
6194 array_ptr_attrs
= declarator
->u
.array
.attrs
;
6195 array_parm_static
= declarator
->u
.array
.static_p
;
6196 array_parm_vla_unspec_p
= declarator
->u
.array
.vla_unspec_p
;
6198 declarator
= declarator
->declarator
;
6200 /* Check for some types that there cannot be arrays of. */
6202 if (VOID_TYPE_P (type
))
6205 error_at (loc
, "declaration of %qE as array of voids", name
);
6207 error_at (loc
, "declaration of type name as array of voids");
6208 type
= error_mark_node
;
6211 if (TREE_CODE (type
) == FUNCTION_TYPE
)
6214 error_at (loc
, "declaration of %qE as array of functions",
6217 error_at (loc
, "declaration of type name as array of "
6219 type
= error_mark_node
;
6222 if (pedantic
&& !in_system_header_at (input_location
)
6223 && flexible_array_type_p (type
))
6224 pedwarn (loc
, OPT_Wpedantic
,
6225 "invalid use of structure with flexible array member");
6227 if (size
== error_mark_node
)
6228 type
= error_mark_node
;
6230 if (type
== error_mark_node
)
6233 /* If size was specified, set ITYPE to a range-type for
6234 that size. Otherwise, ITYPE remains null. finish_decl
6235 may figure it out from an initial value. */
6239 bool size_maybe_const
= true;
6240 bool size_int_const
= (TREE_CODE (size
) == INTEGER_CST
6241 && !TREE_OVERFLOW (size
));
6242 bool this_size_varies
= false;
6244 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6246 STRIP_TYPE_NOPS (size
);
6248 if (!INTEGRAL_TYPE_P (TREE_TYPE (size
)))
6251 error_at (loc
, "size of array %qE has non-integer type",
6255 "size of unnamed array has non-integer type");
6256 size
= integer_one_node
;
6258 /* This can happen with enum forward declaration. */
6259 else if (!COMPLETE_TYPE_P (TREE_TYPE (size
)))
6262 error_at (loc
, "size of array %qE has incomplete type",
6265 error_at (loc
, "size of unnamed array has incomplete "
6267 size
= integer_one_node
;
6270 size
= c_fully_fold (size
, false, &size_maybe_const
);
6272 if (pedantic
&& size_maybe_const
&& integer_zerop (size
))
6275 pedwarn (loc
, OPT_Wpedantic
,
6276 "ISO C forbids zero-size array %qE", name
);
6278 pedwarn (loc
, OPT_Wpedantic
,
6279 "ISO C forbids zero-size array");
6282 if (TREE_CODE (size
) == INTEGER_CST
&& size_maybe_const
)
6284 constant_expression_warning (size
);
6285 if (tree_int_cst_sgn (size
) < 0)
6288 error_at (loc
, "size of array %qE is negative", name
);
6290 error_at (loc
, "size of unnamed array is negative");
6291 size
= integer_one_node
;
6293 /* Handle a size folded to an integer constant but
6294 not an integer constant expression. */
6295 if (!size_int_const
)
6297 /* If this is a file scope declaration of an
6298 ordinary identifier, this is invalid code;
6299 diagnosing it here and not subsequently
6300 treating the type as variable-length avoids
6301 more confusing diagnostics later. */
6302 if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
6303 && current_scope
== file_scope
)
6304 pedwarn (input_location
, 0,
6305 "variably modified %qE at file scope",
6308 this_size_varies
= size_varies
= true;
6309 warn_variable_length_array (name
, size
);
6312 else if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
6313 && current_scope
== file_scope
)
6315 error_at (loc
, "variably modified %qE at file scope", name
);
6316 size
= integer_one_node
;
6320 /* Make sure the array size remains visibly
6321 nonconstant even if it is (eg) a const variable
6322 with known value. */
6323 this_size_varies
= size_varies
= true;
6324 warn_variable_length_array (name
, size
);
6325 if (sanitize_flags_p (SANITIZE_VLA
)
6326 && current_function_decl
!= NULL_TREE
6327 && decl_context
== NORMAL
)
6329 /* Evaluate the array size only once. */
6330 size
= save_expr (size
);
6331 size
= c_fully_fold (size
, false, NULL
);
6332 size
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
6333 ubsan_instrument_vla (loc
, size
),
6338 if (integer_zerop (size
) && !this_size_varies
)
6340 /* A zero-length array cannot be represented with
6341 an unsigned index type, which is what we'll
6342 get with build_index_type. Create an
6343 open-ended range instead. */
6344 itype
= build_range_type (sizetype
, size
, NULL_TREE
);
6348 /* Arrange for the SAVE_EXPR on the inside of the
6349 MINUS_EXPR, which allows the -1 to get folded
6350 with the +1 that happens when building TYPE_SIZE. */
6352 size
= save_expr (size
);
6353 if (this_size_varies
&& TREE_CODE (size
) == INTEGER_CST
)
6354 size
= build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
6355 integer_zero_node
, size
);
6357 /* Compute the maximum valid index, that is, size
6358 - 1. Do the calculation in index_type, so that
6359 if it is a variable the computations will be
6360 done in the proper mode. */
6361 itype
= fold_build2_loc (loc
, MINUS_EXPR
, index_type
,
6362 convert (index_type
, size
),
6363 convert (index_type
,
6366 /* The above overflows when size does not fit
6368 ??? While a size of INT_MAX+1 technically shouldn't
6369 cause an overflow (because we subtract 1), handling
6370 this case seems like an unnecessary complication. */
6371 if (TREE_CODE (size
) == INTEGER_CST
6372 && !int_fits_type_p (size
, index_type
))
6375 error_at (loc
, "size of array %qE is too large",
6378 error_at (loc
, "size of unnamed array is too large");
6379 type
= error_mark_node
;
6383 itype
= build_index_type (itype
);
6385 if (this_size_varies
)
6388 *expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
6392 *expr_const_operands
&= size_maybe_const
;
6395 else if (decl_context
== FIELD
)
6397 bool flexible_array_member
= false;
6398 if (array_parm_vla_unspec_p
)
6399 /* Field names can in fact have function prototype
6400 scope so [*] is disallowed here through making
6401 the field variably modified, not through being
6402 something other than a declaration with function
6407 const struct c_declarator
*t
= declarator
;
6408 while (t
->kind
== cdk_attrs
)
6410 flexible_array_member
= (t
->kind
== cdk_id
);
6412 if (flexible_array_member
6413 && !in_system_header_at (input_location
))
6414 pedwarn_c90 (loc
, OPT_Wpedantic
, "ISO C90 does not "
6415 "support flexible array members");
6417 /* ISO C99 Flexible array members are effectively
6418 identical to GCC's zero-length array extension. */
6419 if (flexible_array_member
|| array_parm_vla_unspec_p
)
6420 itype
= build_range_type (sizetype
, size_zero_node
,
6423 else if (decl_context
== PARM
)
6425 if (array_parm_vla_unspec_p
)
6427 itype
= build_range_type (sizetype
, size_zero_node
, NULL_TREE
);
6431 else if (decl_context
== TYPENAME
)
6433 if (array_parm_vla_unspec_p
)
6436 warning (0, "%<[*]%> not in a declaration");
6437 /* We use this to avoid messing up with incomplete
6438 array types of the same type, that would
6439 otherwise be modified below. */
6440 itype
= build_range_type (sizetype
, size_zero_node
,
6446 /* Complain about arrays of incomplete types. */
6447 if (!COMPLETE_TYPE_P (type
))
6449 error_at (loc
, "array type has incomplete element type %qT",
6451 /* See if we can be more helpful. */
6452 if (TREE_CODE (type
) == ARRAY_TYPE
)
6455 inform (loc
, "declaration of %qE as multidimensional "
6456 "array must have bounds for all dimensions "
6457 "except the first", name
);
6459 inform (loc
, "declaration of multidimensional array "
6460 "must have bounds for all dimensions except "
6463 type
= error_mark_node
;
6466 /* When itype is NULL, a shared incomplete array type is
6467 returned for all array of a given type. Elsewhere we
6468 make sure we don't complete that type before copying
6469 it, but here we want to make sure we don't ever
6470 modify the shared type, so we gcc_assert (itype)
6473 addr_space_t as
= DECODE_QUAL_ADDR_SPACE (type_quals
);
6474 if (!ADDR_SPACE_GENERIC_P (as
) && as
!= TYPE_ADDR_SPACE (type
))
6475 type
= build_qualified_type (type
,
6476 ENCODE_QUAL_ADDR_SPACE (as
));
6478 type
= build_array_type (type
, itype
);
6481 if (type
!= error_mark_node
)
6485 /* It is ok to modify type here even if itype is
6486 NULL: if size_varies, we're in a
6487 multi-dimensional array and the inner type has
6488 variable size, so the enclosing shared array type
6490 if (size
&& TREE_CODE (size
) == INTEGER_CST
)
6492 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6493 C_TYPE_VARIABLE_SIZE (type
) = 1;
6496 /* The GCC extension for zero-length arrays differs from
6497 ISO flexible array members in that sizeof yields
6499 if (size
&& integer_zerop (size
))
6502 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6503 TYPE_SIZE (type
) = bitsize_zero_node
;
6504 TYPE_SIZE_UNIT (type
) = size_zero_node
;
6505 SET_TYPE_STRUCTURAL_EQUALITY (type
);
6507 if (array_parm_vla_unspec_p
)
6510 /* The type is complete. C99 6.7.5.2p4 */
6511 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6512 TYPE_SIZE (type
) = bitsize_zero_node
;
6513 TYPE_SIZE_UNIT (type
) = size_zero_node
;
6514 SET_TYPE_STRUCTURAL_EQUALITY (type
);
6517 if (!valid_array_size_p (loc
, type
, name
))
6518 type
= error_mark_node
;
6521 if (decl_context
!= PARM
6522 && (array_ptr_quals
!= TYPE_UNQUALIFIED
6523 || array_ptr_attrs
!= NULL_TREE
6524 || array_parm_static
))
6526 error_at (loc
, "static or type qualifiers in non-parameter "
6527 "array declarator");
6528 array_ptr_quals
= TYPE_UNQUALIFIED
;
6529 array_ptr_attrs
= NULL_TREE
;
6530 array_parm_static
= false;
6532 orig_qual_indirect
++;
6537 /* Say it's a definition only for the declarator closest
6538 to the identifier, apart possibly from some
6540 bool really_funcdef
= false;
6542 orig_qual_type
= NULL_TREE
;
6545 const struct c_declarator
*t
= declarator
->declarator
;
6546 while (t
->kind
== cdk_attrs
)
6548 really_funcdef
= (t
->kind
== cdk_id
);
6551 /* Declaring a function type. Make sure we have a valid
6552 type for the function to return. */
6553 if (type
== error_mark_node
)
6556 size_varies
= false;
6558 /* Warn about some types functions can't return. */
6559 if (TREE_CODE (type
) == FUNCTION_TYPE
)
6562 error_at (loc
, "%qE declared as function returning a "
6565 error_at (loc
, "type name declared as function "
6566 "returning a function");
6567 type
= integer_type_node
;
6569 if (TREE_CODE (type
) == ARRAY_TYPE
)
6572 error_at (loc
, "%qE declared as function returning an array",
6575 error_at (loc
, "type name declared as function returning "
6577 type
= integer_type_node
;
6580 /* Construct the function type and go to the next
6581 inner layer of declarator. */
6582 arg_info
= declarator
->u
.arg_info
;
6583 arg_types
= grokparms (arg_info
, really_funcdef
);
6585 /* Type qualifiers before the return type of the function
6586 qualify the return type, not the function type. */
6589 const enum c_declspec_word ignored_quals_list
[] =
6591 cdw_const
, cdw_volatile
, cdw_restrict
, cdw_address_space
,
6592 cdw_atomic
, cdw_number_of_elements
6594 location_t specs_loc
6595 = smallest_type_quals_location (declspecs
->locations
,
6596 ignored_quals_list
);
6597 if (specs_loc
== UNKNOWN_LOCATION
)
6598 specs_loc
= declspecs
->locations
[cdw_typedef
];
6599 if (specs_loc
== UNKNOWN_LOCATION
)
6602 /* Type qualifiers on a function return type are
6603 normally permitted by the standard but have no
6604 effect, so give a warning at -Wreturn-type.
6605 Qualifiers on a void return type are banned on
6606 function definitions in ISO C; GCC used to used
6607 them for noreturn functions. The resolution of C11
6608 DR#423 means qualifiers (other than _Atomic) are
6609 actually removed from the return type when
6610 determining the function type. */
6611 int quals_used
= type_quals
;
6613 quals_used
&= TYPE_QUAL_ATOMIC
;
6614 if (quals_used
&& VOID_TYPE_P (type
) && really_funcdef
)
6615 pedwarn (specs_loc
, 0,
6616 "function definition has qualified void "
6619 warning_at (specs_loc
, OPT_Wignored_qualifiers
,
6620 "type qualifiers ignored on function "
6623 /* Ensure an error for restrict on invalid types; the
6624 DR#423 resolution is not entirely clear about
6627 && (type_quals
& TYPE_QUAL_RESTRICT
)
6628 && (!POINTER_TYPE_P (type
)
6629 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
6630 error_at (loc
, "invalid use of %<restrict%>");
6631 type
= c_build_qualified_type (type
, quals_used
);
6633 type_quals
= TYPE_UNQUALIFIED
;
6635 type
= build_function_type (type
, arg_types
);
6636 declarator
= declarator
->declarator
;
6638 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6639 the formal parameter list of this FUNCTION_TYPE to point to
6640 the FUNCTION_TYPE node itself. */
6645 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info
->tags
, ix
, tag
)
6646 TYPE_CONTEXT (tag
->type
) = type
;
6652 /* Merge any constancy or volatility into the target type
6654 if ((type_quals
& TYPE_QUAL_ATOMIC
)
6655 && TREE_CODE (type
) == FUNCTION_TYPE
)
6658 "%<_Atomic%>-qualified function type");
6659 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6661 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
6663 pedwarn (loc
, OPT_Wpedantic
,
6664 "ISO C forbids qualified function types");
6666 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6667 orig_qual_indirect
);
6668 orig_qual_type
= NULL_TREE
;
6669 size_varies
= false;
6671 /* When the pointed-to type involves components of variable size,
6672 care must be taken to ensure that the size evaluation code is
6673 emitted early enough to dominate all the possible later uses
6674 and late enough for the variables on which it depends to have
6677 This is expected to happen automatically when the pointed-to
6678 type has a name/declaration of it's own, but special attention
6679 is required if the type is anonymous.
6681 We attach an artificial TYPE_DECL to such pointed-to type
6682 and arrange for it to be included in a DECL_EXPR. This
6683 forces the sizes evaluation at a safe point and ensures it
6684 is not deferred until e.g. within a deeper conditional context.
6686 PARM contexts have no enclosing statement list that
6687 can hold the DECL_EXPR, so we need to use a BIND_EXPR
6688 instead, and add it to the list of expressions that
6689 need to be evaluated.
6691 TYPENAME contexts do have an enclosing statement list,
6692 but it would be incorrect to use it, as the size should
6693 only be evaluated if the containing expression is
6694 evaluated. We might also be in the middle of an
6695 expression with side effects on the pointed-to type size
6696 "arguments" prior to the pointer declaration point and
6697 the fake TYPE_DECL in the enclosing context would force
6698 the size evaluation prior to the side effects. We therefore
6699 use BIND_EXPRs in TYPENAME contexts too. */
6700 if (!TYPE_NAME (type
)
6701 && variably_modified_type_p (type
, NULL_TREE
))
6703 tree bind
= NULL_TREE
;
6704 if (decl_context
== TYPENAME
|| decl_context
== PARM
)
6706 bind
= build3 (BIND_EXPR
, void_type_node
, NULL_TREE
,
6707 NULL_TREE
, NULL_TREE
);
6708 TREE_SIDE_EFFECTS (bind
) = 1;
6709 BIND_EXPR_BODY (bind
) = push_stmt_list ();
6712 tree decl
= build_decl (loc
, TYPE_DECL
, NULL_TREE
, type
);
6713 DECL_ARTIFICIAL (decl
) = 1;
6715 finish_decl (decl
, loc
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
6716 TYPE_NAME (type
) = decl
;
6720 BIND_EXPR_BODY (bind
)
6721 = pop_stmt_list (BIND_EXPR_BODY (bind
));
6723 *expr
= build2 (COMPOUND_EXPR
, void_type_node
, *expr
,
6730 type
= c_build_pointer_type (type
);
6732 /* Process type qualifiers (such as const or volatile)
6733 that were given inside the `*'. */
6734 type_quals
= declarator
->u
.pointer_quals
;
6736 declarator
= declarator
->declarator
;
6743 *decl_attrs
= chainon (returned_attrs
, *decl_attrs
);
6745 /* Now TYPE has the actual type, apart from any qualifiers in
6748 /* Warn about address space used for things other than static memory or
6750 address_space
= DECODE_QUAL_ADDR_SPACE (type_quals
);
6751 if (!ADDR_SPACE_GENERIC_P (address_space
))
6753 if (decl_context
== NORMAL
)
6755 switch (storage_class
)
6758 error ("%qs combined with %<auto%> qualifier for %qE",
6759 c_addr_space_name (address_space
), name
);
6762 error ("%qs combined with %<register%> qualifier for %qE",
6763 c_addr_space_name (address_space
), name
);
6766 if (current_function_scope
)
6768 error ("%qs specified for auto variable %qE",
6769 c_addr_space_name (address_space
), name
);
6781 else if (decl_context
== PARM
&& TREE_CODE (type
) != ARRAY_TYPE
)
6784 error ("%qs specified for parameter %qE",
6785 c_addr_space_name (address_space
), name
);
6787 error ("%qs specified for unnamed parameter",
6788 c_addr_space_name (address_space
));
6790 else if (decl_context
== FIELD
)
6793 error ("%qs specified for structure field %qE",
6794 c_addr_space_name (address_space
), name
);
6796 error ("%qs specified for structure field",
6797 c_addr_space_name (address_space
));
6801 /* Check the type and width of a bit-field. */
6804 check_bitfield_type_and_width (loc
, &type
, width
, name
);
6805 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6806 atomic types are permitted for bit-fields; we have no code to
6807 make bit-field accesses atomic, so disallow them. */
6808 if (type_quals
& TYPE_QUAL_ATOMIC
)
6811 error_at (loc
, "bit-field %qE has atomic type", name
);
6813 error_at (loc
, "bit-field has atomic type");
6814 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6818 /* Reject invalid uses of _Alignas. */
6819 if (declspecs
->alignas_p
)
6821 if (storage_class
== csc_typedef
)
6822 error_at (loc
, "alignment specified for typedef %qE", name
);
6823 else if (storage_class
== csc_register
)
6824 error_at (loc
, "alignment specified for %<register%> object %qE",
6826 else if (decl_context
== PARM
)
6829 error_at (loc
, "alignment specified for parameter %qE", name
);
6831 error_at (loc
, "alignment specified for unnamed parameter");
6836 error_at (loc
, "alignment specified for bit-field %qE", name
);
6838 error_at (loc
, "alignment specified for unnamed bit-field");
6840 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
6841 error_at (loc
, "alignment specified for function %qE", name
);
6842 else if (declspecs
->align_log
!= -1 && TYPE_P (type
))
6844 alignas_align
= 1U << declspecs
->align_log
;
6845 if (alignas_align
< min_align_of_type (type
))
6848 error_at (loc
, "%<_Alignas%> specifiers cannot reduce "
6849 "alignment of %qE", name
);
6851 error_at (loc
, "%<_Alignas%> specifiers cannot reduce "
6852 "alignment of unnamed field");
6858 /* If this is declaring a typedef name, return a TYPE_DECL. */
6860 if (storage_class
== csc_typedef
)
6863 if ((type_quals
& TYPE_QUAL_ATOMIC
)
6864 && TREE_CODE (type
) == FUNCTION_TYPE
)
6867 "%<_Atomic%>-qualified function type");
6868 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6870 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
6872 pedwarn (loc
, OPT_Wpedantic
,
6873 "ISO C forbids qualified function types");
6875 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6876 orig_qual_indirect
);
6877 decl
= build_decl (declarator
->id_loc
,
6878 TYPE_DECL
, declarator
->u
.id
, type
);
6879 if (declspecs
->explicit_signed_p
)
6880 C_TYPEDEF_EXPLICITLY_SIGNED (decl
) = 1;
6881 if (declspecs
->inline_p
)
6882 pedwarn (loc
, 0,"typedef %q+D declared %<inline%>", decl
);
6883 if (declspecs
->noreturn_p
)
6884 pedwarn (loc
, 0,"typedef %q+D declared %<_Noreturn%>", decl
);
6886 if (warn_cxx_compat
&& declarator
->u
.id
!= NULL_TREE
)
6888 struct c_binding
*b
= I_TAG_BINDING (declarator
->u
.id
);
6891 && b
->decl
!= NULL_TREE
6892 && (B_IN_CURRENT_SCOPE (b
)
6893 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
6894 && TYPE_MAIN_VARIANT (b
->decl
) != TYPE_MAIN_VARIANT (type
))
6896 auto_diagnostic_group d
;
6897 if (warning_at (declarator
->id_loc
, OPT_Wc___compat
,
6898 ("using %qD as both a typedef and a tag is "
6899 "invalid in C++"), decl
)
6900 && b
->locus
!= UNKNOWN_LOCATION
)
6901 inform (b
->locus
, "originally defined here");
6908 /* If this is a type name (such as, in a cast or sizeof),
6909 compute the type and return it now. */
6911 if (decl_context
== TYPENAME
)
6913 /* Note that the grammar rejects storage classes in typenames
6915 gcc_assert (storage_class
== csc_none
&& !threadp
6916 && !declspecs
->inline_p
&& !declspecs
->noreturn_p
);
6917 if ((type_quals
& TYPE_QUAL_ATOMIC
)
6918 && TREE_CODE (type
) == FUNCTION_TYPE
)
6921 "%<_Atomic%>-qualified function type");
6922 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6924 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
6926 pedwarn (loc
, OPT_Wpedantic
,
6927 "ISO C forbids const or volatile function types");
6929 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6930 orig_qual_indirect
);
6934 if (pedantic
&& decl_context
== FIELD
6935 && variably_modified_type_p (type
, NULL_TREE
))
6938 pedwarn (loc
, OPT_Wpedantic
, "a member of a structure or union cannot "
6939 "have a variably modified type");
6942 /* Aside from typedefs and type names (handle above),
6943 `void' at top level (not within pointer)
6944 is allowed only in public variables.
6945 We don't complain about parms either, but that is because
6946 a better error message can be made later. */
6948 if (VOID_TYPE_P (type
) && decl_context
!= PARM
6949 && !((decl_context
!= FIELD
&& TREE_CODE (type
) != FUNCTION_TYPE
)
6950 && (storage_class
== csc_extern
6951 || (current_scope
== file_scope
6952 && !(storage_class
== csc_static
6953 || storage_class
== csc_register
)))))
6955 error_at (loc
, "variable or field %qE declared void", name
);
6956 type
= integer_type_node
;
6959 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6960 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6965 if (decl_context
== PARM
)
6968 bool array_parameter_p
= false;
6970 /* A parameter declared as an array of T is really a pointer to T.
6971 One declared as a function is really a pointer to a function. */
6973 if (TREE_CODE (type
) == ARRAY_TYPE
)
6975 /* Transfer const-ness of array into that of type pointed to. */
6976 type
= TREE_TYPE (type
);
6977 if (orig_qual_type
!= NULL_TREE
)
6979 if (orig_qual_indirect
== 0)
6980 orig_qual_type
= TREE_TYPE (orig_qual_type
);
6982 orig_qual_indirect
--;
6985 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6986 orig_qual_indirect
);
6987 type
= c_build_pointer_type (type
);
6988 type_quals
= array_ptr_quals
;
6990 type
= c_build_qualified_type (type
, type_quals
);
6992 /* We don't yet implement attributes in this context. */
6993 if (array_ptr_attrs
!= NULL_TREE
)
6994 warning_at (loc
, OPT_Wattributes
,
6995 "attributes in parameter array declarator ignored");
6997 size_varies
= false;
6998 array_parameter_p
= true;
7000 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
7002 if (type_quals
& TYPE_QUAL_ATOMIC
)
7005 "%<_Atomic%>-qualified function type");
7006 type_quals
&= ~TYPE_QUAL_ATOMIC
;
7008 else if (type_quals
)
7009 pedwarn (loc
, OPT_Wpedantic
,
7010 "ISO C forbids qualified function types");
7012 type
= c_build_qualified_type (type
, type_quals
);
7013 type
= c_build_pointer_type (type
);
7014 type_quals
= TYPE_UNQUALIFIED
;
7016 else if (type_quals
)
7017 type
= c_build_qualified_type (type
, type_quals
);
7019 decl
= build_decl (declarator
->id_loc
,
7020 PARM_DECL
, declarator
->u
.id
, type
);
7022 C_DECL_VARIABLE_SIZE (decl
) = 1;
7023 C_ARRAY_PARAMETER (decl
) = array_parameter_p
;
7025 /* Compute the type actually passed in the parmlist,
7026 for the case where there is no prototype.
7027 (For example, shorts and chars are passed as ints.)
7028 When there is a prototype, this is overridden later. */
7030 if (type
== error_mark_node
)
7031 promoted_type
= type
;
7033 promoted_type
= c_type_promotes_to (type
);
7035 DECL_ARG_TYPE (decl
) = promoted_type
;
7036 if (declspecs
->inline_p
)
7037 pedwarn (loc
, 0, "parameter %q+D declared %<inline%>", decl
);
7038 if (declspecs
->noreturn_p
)
7039 pedwarn (loc
, 0, "parameter %q+D declared %<_Noreturn%>", decl
);
7041 else if (decl_context
== FIELD
)
7043 /* Note that the grammar rejects storage classes in typenames
7045 gcc_assert (storage_class
== csc_none
&& !threadp
7046 && !declspecs
->inline_p
&& !declspecs
->noreturn_p
);
7048 /* Structure field. It may not be a function. */
7050 if (TREE_CODE (type
) == FUNCTION_TYPE
)
7052 error_at (loc
, "field %qE declared as a function", name
);
7053 type
= build_pointer_type (type
);
7055 else if (TREE_CODE (type
) != ERROR_MARK
7056 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type
))
7059 error_at (loc
, "field %qE has incomplete type", name
);
7061 error_at (loc
, "unnamed field has incomplete type");
7062 type
= error_mark_node
;
7064 else if (TREE_CODE (type
) == ARRAY_TYPE
7065 && TYPE_DOMAIN (type
) == NULL_TREE
)
7067 /* We have a flexible array member through a typedef.
7068 Set suitable range. Whether this is a correct position
7069 for a flexible array member will be determined elsewhere. */
7070 if (!in_system_header_at (input_location
))
7071 pedwarn_c90 (loc
, OPT_Wpedantic
, "ISO C90 does not "
7072 "support flexible array members");
7073 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
7074 TYPE_DOMAIN (type
) = build_range_type (sizetype
, size_zero_node
,
7076 if (orig_qual_indirect
== 0)
7077 orig_qual_type
= NULL_TREE
;
7079 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
7080 orig_qual_indirect
);
7081 decl
= build_decl (declarator
->id_loc
,
7082 FIELD_DECL
, declarator
->u
.id
, type
);
7083 DECL_NONADDRESSABLE_P (decl
) = bitfield
;
7084 if (bitfield
&& !declarator
->u
.id
)
7086 TREE_NO_WARNING (decl
) = 1;
7087 DECL_PADDING_P (decl
) = 1;
7091 C_DECL_VARIABLE_SIZE (decl
) = 1;
7093 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
7095 if (storage_class
== csc_register
|| threadp
)
7097 error_at (loc
, "invalid storage class for function %qE", name
);
7099 else if (current_scope
!= file_scope
)
7101 /* Function declaration not at file scope. Storage
7102 classes other than `extern' are not allowed, C99
7103 6.7.1p5, and `extern' makes no difference. However,
7104 GCC allows 'auto', perhaps with 'inline', to support
7105 nested functions. */
7106 if (storage_class
== csc_auto
)
7107 pedwarn (loc
, OPT_Wpedantic
,
7108 "invalid storage class for function %qE", name
);
7109 else if (storage_class
== csc_static
)
7111 error_at (loc
, "invalid storage class for function %qE", name
);
7113 storage_class
= declspecs
->storage_class
= csc_none
;
7119 decl
= build_decl (declarator
->id_loc
,
7120 FUNCTION_DECL
, declarator
->u
.id
, type
);
7121 decl
= build_decl_attribute_variant (decl
, decl_attr
);
7123 if (type_quals
& TYPE_QUAL_ATOMIC
)
7126 "%<_Atomic%>-qualified function type");
7127 type_quals
&= ~TYPE_QUAL_ATOMIC
;
7129 else if (pedantic
&& type_quals
&& !DECL_IN_SYSTEM_HEADER (decl
))
7130 pedwarn (loc
, OPT_Wpedantic
,
7131 "ISO C forbids qualified function types");
7133 /* Every function declaration is an external reference
7134 (DECL_EXTERNAL) except for those which are not at file
7135 scope and are explicitly declared "auto". This is
7136 forbidden by standard C (C99 6.7.1p5) and is interpreted by
7137 GCC to signify a forward declaration of a nested function. */
7138 if (storage_class
== csc_auto
&& current_scope
!= file_scope
)
7139 DECL_EXTERNAL (decl
) = 0;
7140 /* In C99, a function which is declared 'inline' with 'extern'
7141 is not an external reference (which is confusing). It
7142 means that the later definition of the function must be output
7143 in this file, C99 6.7.4p6. In GNU C89, a function declared
7144 'extern inline' is an external reference. */
7145 else if (declspecs
->inline_p
&& storage_class
!= csc_static
)
7146 DECL_EXTERNAL (decl
) = ((storage_class
== csc_extern
)
7147 == flag_gnu89_inline
);
7149 DECL_EXTERNAL (decl
) = !initialized
;
7151 /* Record absence of global scope for `static' or `auto'. */
7153 = !(storage_class
== csc_static
|| storage_class
== csc_auto
);
7155 /* For a function definition, record the argument information
7156 block where store_parm_decls will look for it. */
7158 current_function_arg_info
= arg_info
;
7160 if (declspecs
->default_int_p
)
7161 C_FUNCTION_IMPLICIT_INT (decl
) = 1;
7163 /* Record presence of `inline' and `_Noreturn', if it is
7165 if (flag_hosted
&& MAIN_NAME_P (declarator
->u
.id
))
7167 if (declspecs
->inline_p
)
7168 pedwarn (loc
, 0, "cannot inline function %<main%>");
7169 if (declspecs
->noreturn_p
)
7170 pedwarn (loc
, 0, "%<main%> declared %<_Noreturn%>");
7174 if (declspecs
->inline_p
)
7175 /* Record that the function is declared `inline'. */
7176 DECL_DECLARED_INLINE_P (decl
) = 1;
7177 if (declspecs
->noreturn_p
)
7180 pedwarn_c99 (loc
, OPT_Wpedantic
,
7181 "ISO C99 does not support %<_Noreturn%>");
7183 pedwarn_c99 (loc
, OPT_Wpedantic
,
7184 "ISO C90 does not support %<_Noreturn%>");
7185 TREE_THIS_VOLATILE (decl
) = 1;
7191 /* It's a variable. */
7192 /* An uninitialized decl with `extern' is a reference. */
7193 int extern_ref
= !initialized
&& storage_class
== csc_extern
;
7195 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
7196 orig_qual_indirect
);
7198 /* C99 6.2.2p7: It is invalid (compile-time undefined
7199 behavior) to create an 'extern' declaration for a
7200 variable if there is a global declaration that is
7201 'static' and the global declaration is not visible.
7202 (If the static declaration _is_ currently visible,
7203 the 'extern' declaration is taken to refer to that decl.) */
7204 if (extern_ref
&& current_scope
!= file_scope
)
7206 tree global_decl
= identifier_global_value (declarator
->u
.id
);
7207 tree visible_decl
= lookup_name (declarator
->u
.id
);
7210 && global_decl
!= visible_decl
7211 && VAR_P (global_decl
)
7212 && !TREE_PUBLIC (global_decl
))
7213 error_at (loc
, "variable previously declared %<static%> "
7214 "redeclared %<extern%>");
7217 decl
= build_decl (declarator
->id_loc
,
7218 VAR_DECL
, declarator
->u
.id
, type
);
7220 C_DECL_VARIABLE_SIZE (decl
) = 1;
7222 if (declspecs
->inline_p
)
7223 pedwarn (loc
, 0, "variable %q+D declared %<inline%>", decl
);
7224 if (declspecs
->noreturn_p
)
7225 pedwarn (loc
, 0, "variable %q+D declared %<_Noreturn%>", decl
);
7227 /* At file scope, an initialized extern declaration may follow
7228 a static declaration. In that case, DECL_EXTERNAL will be
7229 reset later in start_decl. */
7230 DECL_EXTERNAL (decl
) = (storage_class
== csc_extern
);
7232 /* At file scope, the presence of a `static' or `register' storage
7233 class specifier, or the absence of all storage class specifiers
7234 makes this declaration a definition (perhaps tentative). Also,
7235 the absence of `static' makes it public. */
7236 if (current_scope
== file_scope
)
7238 TREE_PUBLIC (decl
) = storage_class
!= csc_static
;
7239 TREE_STATIC (decl
) = !extern_ref
;
7241 /* Not at file scope, only `static' makes a static definition. */
7244 TREE_STATIC (decl
) = (storage_class
== csc_static
);
7245 TREE_PUBLIC (decl
) = extern_ref
;
7249 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
7252 if ((storage_class
== csc_extern
7253 || (storage_class
== csc_none
7254 && TREE_CODE (type
) == FUNCTION_TYPE
7256 && variably_modified_type_p (type
, NULL_TREE
))
7259 if (TREE_CODE (type
) == FUNCTION_TYPE
)
7260 error_at (loc
, "non-nested function with variably modified type");
7262 error_at (loc
, "object with variably modified type must have "
7266 /* Record `register' declaration for warnings on &
7267 and in case doing stupid register allocation. */
7269 if (storage_class
== csc_register
)
7271 C_DECL_REGISTER (decl
) = 1;
7272 DECL_REGISTER (decl
) = 1;
7275 /* Record constancy and volatility. */
7276 c_apply_type_quals_to_decl (type_quals
, decl
);
7278 /* Apply _Alignas specifiers. */
7281 SET_DECL_ALIGN (decl
, alignas_align
* BITS_PER_UNIT
);
7282 DECL_USER_ALIGN (decl
) = 1;
7285 /* If a type has volatile components, it should be stored in memory.
7286 Otherwise, the fact that those components are volatile
7287 will be ignored, and would even crash the compiler.
7288 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7289 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl
))
7290 && (VAR_P (decl
) || TREE_CODE (decl
) == PARM_DECL
7291 || TREE_CODE (decl
) == RESULT_DECL
))
7293 /* It is not an error for a structure with volatile fields to
7294 be declared register, but reset DECL_REGISTER since it
7295 cannot actually go in a register. */
7296 int was_reg
= C_DECL_REGISTER (decl
);
7297 C_DECL_REGISTER (decl
) = 0;
7298 DECL_REGISTER (decl
) = 0;
7299 c_mark_addressable (decl
);
7300 C_DECL_REGISTER (decl
) = was_reg
;
7303 /* This is the earliest point at which we might know the assembler
7304 name of a variable. Thus, if it's known before this, die horribly. */
7305 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl
)
7306 || !DECL_ASSEMBLER_NAME_SET_P (decl
));
7310 && TREE_PUBLIC (decl
)
7311 && TREE_STATIC (decl
)
7312 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
))
7313 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
7314 && TYPE_NAME (TREE_TYPE (decl
)) == NULL_TREE
)
7315 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
7316 ("non-local variable %qD with anonymous type is "
7317 "questionable in C++"),
7324 /* Decode the parameter-list info for a function type or function definition.
7325 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7326 if there is an identifier list instead of a parameter decl list).
7327 These two functions are separate because when a function returns
7328 or receives functions then each is called multiple times but the order
7329 of calls is different. The last call to `grokparms' is always the one
7330 that contains the formal parameter names of a function definition.
7332 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7334 FUNCDEF_FLAG is true for a function definition, false for
7335 a mere declaration. A nonempty identifier-list gets an error message
7336 when FUNCDEF_FLAG is false. */
7339 grokparms (struct c_arg_info
*arg_info
, bool funcdef_flag
)
7341 tree arg_types
= arg_info
->types
;
7343 if (funcdef_flag
&& arg_info
->had_vla_unspec
)
7345 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7347 error ("%<[*]%> not allowed in other than function prototype scope");
7350 if (arg_types
== NULL_TREE
&& !funcdef_flag
7351 && !in_system_header_at (input_location
))
7352 warning (OPT_Wstrict_prototypes
,
7353 "function declaration isn%'t a prototype");
7355 if (arg_types
== error_mark_node
)
7356 /* Don't set TYPE_ARG_TYPES in this case. */
7359 else if (arg_types
&& TREE_CODE (TREE_VALUE (arg_types
)) == IDENTIFIER_NODE
)
7363 pedwarn (input_location
, 0, "parameter names (without types) in "
7364 "function declaration");
7365 arg_info
->parms
= NULL_TREE
;
7368 arg_info
->parms
= arg_info
->types
;
7370 arg_info
->types
= NULL_TREE
;
7375 tree parm
, type
, typelt
;
7376 unsigned int parmno
;
7378 /* If there is a parameter of incomplete type in a definition,
7379 this is an error. In a declaration this is valid, and a
7380 struct or union type may be completed later, before any calls
7381 or definition of the function. In the case where the tag was
7382 first declared within the parameter list, a warning has
7383 already been given. If a parameter has void type, then
7384 however the function cannot be defined or called, so
7387 for (parm
= arg_info
->parms
, typelt
= arg_types
, parmno
= 1;
7389 parm
= DECL_CHAIN (parm
), typelt
= TREE_CHAIN (typelt
), parmno
++)
7391 type
= TREE_VALUE (typelt
);
7392 if (type
== error_mark_node
)
7395 if (!COMPLETE_TYPE_P (type
))
7399 if (DECL_NAME (parm
))
7400 error_at (input_location
,
7401 "parameter %u (%q+D) has incomplete type",
7404 error_at (DECL_SOURCE_LOCATION (parm
),
7405 "parameter %u has incomplete type",
7408 TREE_VALUE (typelt
) = error_mark_node
;
7409 TREE_TYPE (parm
) = error_mark_node
;
7410 arg_types
= NULL_TREE
;
7412 else if (VOID_TYPE_P (type
))
7414 if (DECL_NAME (parm
))
7415 warning_at (input_location
, 0,
7416 "parameter %u (%q+D) has void type",
7419 warning_at (DECL_SOURCE_LOCATION (parm
), 0,
7420 "parameter %u has void type",
7425 if (DECL_NAME (parm
) && TREE_USED (parm
))
7426 warn_if_shadowing (parm
);
7432 /* Allocate and initialize a c_arg_info structure from the parser's
7436 build_arg_info (void)
7438 struct c_arg_info
*ret
= XOBNEW (&parser_obstack
, struct c_arg_info
);
7439 ret
->parms
= NULL_TREE
;
7441 ret
->types
= NULL_TREE
;
7442 ret
->others
= NULL_TREE
;
7443 ret
->pending_sizes
= NULL
;
7444 ret
->had_vla_unspec
= 0;
7448 /* Take apart the current scope and return a c_arg_info structure with
7449 info on a parameter list just parsed.
7451 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7453 ELLIPSIS being true means the argument list ended in '...' so don't
7454 append a sentinel (void_list_node) to the end of the type-list.
7456 EXPR is NULL or an expression that needs to be evaluated for the
7457 side effects of array size expressions in the parameters. */
7460 get_parm_info (bool ellipsis
, tree expr
)
7462 struct c_binding
*b
= current_scope
->bindings
;
7463 struct c_arg_info
*arg_info
= build_arg_info ();
7465 tree parms
= NULL_TREE
;
7466 vec
<c_arg_tag
, va_gc
> *tags
= NULL
;
7467 tree types
= NULL_TREE
;
7468 tree others
= NULL_TREE
;
7470 bool gave_void_only_once_err
= false;
7472 arg_info
->had_vla_unspec
= current_scope
->had_vla_unspec
;
7474 /* The bindings in this scope must not get put into a block.
7475 We will take care of deleting the binding nodes. */
7476 current_scope
->bindings
= 0;
7478 /* This function is only called if there was *something* on the
7482 /* A parameter list consisting solely of 'void' indicates that the
7483 function takes no arguments. But if the 'void' is qualified
7484 (by 'const' or 'volatile'), or has a storage class specifier
7485 ('register'), then the behavior is undefined; issue an error.
7486 Typedefs for 'void' are OK (see DR#157). */
7487 if (b
->prev
== 0 /* one binding */
7488 && TREE_CODE (b
->decl
) == PARM_DECL
/* which is a parameter */
7489 && !DECL_NAME (b
->decl
) /* anonymous */
7490 && VOID_TYPE_P (TREE_TYPE (b
->decl
))) /* of void type */
7492 if (TYPE_QUALS (TREE_TYPE (b
->decl
)) != TYPE_UNQUALIFIED
7493 || C_DECL_REGISTER (b
->decl
))
7494 error_at (b
->locus
, "%<void%> as only parameter may not be qualified");
7496 /* There cannot be an ellipsis. */
7498 error_at (b
->locus
, "%<void%> must be the only parameter");
7500 arg_info
->types
= void_list_node
;
7505 types
= void_list_node
;
7507 /* Break up the bindings list into parms, tags, types, and others;
7508 apply sanity checks; purge the name-to-decl bindings. */
7511 tree decl
= b
->decl
;
7512 tree type
= TREE_TYPE (decl
);
7514 const char *keyword
;
7516 switch (TREE_CODE (decl
))
7521 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
7522 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
7525 /* Check for forward decls that never got their actual decl. */
7526 if (TREE_ASM_WRITTEN (decl
))
7528 "parameter %q+D has just a forward declaration", decl
);
7529 /* Check for (..., void, ...) and issue an error. */
7530 else if (VOID_TYPE_P (type
) && !DECL_NAME (decl
))
7532 if (!gave_void_only_once_err
)
7534 error_at (b
->locus
, "%<void%> must be the only parameter");
7535 gave_void_only_once_err
= true;
7540 /* Valid parameter, add it to the list. */
7541 DECL_CHAIN (decl
) = parms
;
7544 /* Since there is a prototype, args are passed in their
7545 declared types. The back end may override this later. */
7546 DECL_ARG_TYPE (decl
) = type
;
7547 types
= tree_cons (0, type
, types
);
7551 case ENUMERAL_TYPE
: keyword
= "enum"; goto tag
;
7552 case UNION_TYPE
: keyword
= "union"; goto tag
;
7553 case RECORD_TYPE
: keyword
= "struct"; goto tag
;
7555 /* Types may not have tag-names, in which case the type
7556 appears in the bindings list with b->id NULL. */
7559 gcc_assert (I_TAG_BINDING (b
->id
) == b
);
7560 I_TAG_BINDING (b
->id
) = b
->shadowed
;
7563 /* Warn about any struct, union or enum tags defined in a
7564 parameter list. The scope of such types is limited to
7565 the parameter list, which is rarely if ever desirable
7566 (it's impossible to call such a function with type-
7567 correct arguments). An anonymous union parm type is
7568 meaningful as a GNU extension, so don't warn for that. */
7569 if (TREE_CODE (decl
) != UNION_TYPE
|| b
->id
!= NULL_TREE
)
7572 /* The %s will be one of 'struct', 'union', or 'enum'. */
7573 warning_at (b
->locus
, 0,
7574 "%<%s %E%> declared inside parameter list"
7575 " will not be visible outside of this definition or"
7576 " declaration", keyword
, b
->id
);
7578 /* The %s will be one of 'struct', 'union', or 'enum'. */
7579 warning_at (b
->locus
, 0,
7580 "anonymous %s declared inside parameter list"
7581 " will not be visible outside of this definition or"
7582 " declaration", keyword
);
7587 vec_safe_push (tags
, tag
);
7591 /* FUNCTION_DECLs appear when there is an implicit function
7592 declaration in the parameter list. */
7593 gcc_assert (b
->nested
|| seen_error ());
7598 /* CONST_DECLs appear here when we have an embedded enum,
7599 and TYPE_DECLs appear here when we have an embedded struct
7600 or union. No warnings for this - we already warned about the
7603 /* When we reinsert this decl in the function body, we need
7604 to reconstruct whether it was marked as nested. */
7605 gcc_assert (!b
->nested
);
7606 DECL_CHAIN (decl
) = others
;
7612 /* error_mark_node appears here when we have an undeclared
7613 variable. Just throw it away. */
7616 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
7617 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
7621 /* Other things that might be encountered. */
7628 b
= free_binding_and_advance (b
);
7631 arg_info
->parms
= parms
;
7632 arg_info
->tags
= tags
;
7633 arg_info
->types
= types
;
7634 arg_info
->others
= others
;
7635 arg_info
->pending_sizes
= expr
;
7639 /* Get the struct, enum or union (CODE says which) with tag NAME.
7640 Define the tag as a forward-reference with location LOC if it is
7641 not defined. Return a c_typespec structure for the type
7645 parser_xref_tag (location_t loc
, enum tree_code code
, tree name
)
7647 struct c_typespec ret
;
7651 ret
.expr
= NULL_TREE
;
7652 ret
.expr_const_operands
= true;
7654 /* If a cross reference is requested, look up the type
7655 already defined for this tag and return it. */
7657 ref
= lookup_tag (code
, name
, false, &refloc
);
7658 /* If this is the right type of tag, return what we found.
7659 (This reference will be shadowed by shadow_tag later if appropriate.)
7660 If this is the wrong type of tag, do not return it. If it was the
7661 wrong type in the same scope, we will have had an error
7662 message already; if in a different scope and declaring
7663 a name, pending_xref_error will give an error message; but if in a
7664 different scope and not declaring a name, this tag should
7665 shadow the previous declaration of a different type of tag, and
7666 this would not work properly if we return the reference found.
7667 (For example, with "struct foo" in an outer scope, "union foo;"
7668 must shadow that tag with a new one of union type.) */
7669 ret
.kind
= (ref
? ctsk_tagref
: ctsk_tagfirstref
);
7670 if (ref
&& TREE_CODE (ref
) == code
)
7672 if (C_TYPE_DEFINED_IN_STRUCT (ref
)
7673 && loc
!= UNKNOWN_LOCATION
7679 warning_at (loc
, OPT_Wc___compat
,
7680 ("enum type defined in struct or union "
7681 "is not visible in C++"));
7682 inform (refloc
, "enum type defined here");
7685 warning_at (loc
, OPT_Wc___compat
,
7686 ("struct defined in struct or union "
7687 "is not visible in C++"));
7688 inform (refloc
, "struct defined here");
7691 warning_at (loc
, OPT_Wc___compat
,
7692 ("union defined in struct or union "
7693 "is not visible in C++"));
7694 inform (refloc
, "union defined here");
7705 /* If no such tag is yet defined, create a forward-reference node
7706 and record it as the "definition".
7707 When a real declaration of this type is found,
7708 the forward-reference will be altered into a real type. */
7710 ref
= make_node (code
);
7711 if (code
== ENUMERAL_TYPE
)
7713 /* Give the type a default layout like unsigned int
7714 to avoid crashing if it does not get defined. */
7715 SET_TYPE_MODE (ref
, TYPE_MODE (unsigned_type_node
));
7716 SET_TYPE_ALIGN (ref
, TYPE_ALIGN (unsigned_type_node
));
7717 TYPE_USER_ALIGN (ref
) = 0;
7718 TYPE_UNSIGNED (ref
) = 1;
7719 TYPE_PRECISION (ref
) = TYPE_PRECISION (unsigned_type_node
);
7720 TYPE_MIN_VALUE (ref
) = TYPE_MIN_VALUE (unsigned_type_node
);
7721 TYPE_MAX_VALUE (ref
) = TYPE_MAX_VALUE (unsigned_type_node
);
7724 pushtag (loc
, name
, ref
);
7730 /* Get the struct, enum or union (CODE says which) with tag NAME.
7731 Define the tag as a forward-reference if it is not defined.
7732 Return a tree for the type. */
7735 xref_tag (enum tree_code code
, tree name
)
7737 return parser_xref_tag (input_location
, code
, name
).spec
;
7740 /* Make sure that the tag NAME is defined *in the current scope*
7741 at least as a forward reference.
7742 LOC is the location of the struct's definition.
7743 CODE says which kind of tag NAME ought to be.
7745 This stores the current value of the file static STRUCT_PARSE_INFO
7746 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7747 new c_struct_parse_info structure. The old value of
7748 STRUCT_PARSE_INFO is restored in finish_struct. */
7751 start_struct (location_t loc
, enum tree_code code
, tree name
,
7752 struct c_struct_parse_info
**enclosing_struct_parse_info
)
7754 /* If there is already a tag defined at this scope
7755 (as a forward reference), just return it. */
7757 tree ref
= NULL_TREE
;
7758 location_t refloc
= UNKNOWN_LOCATION
;
7760 if (name
!= NULL_TREE
)
7761 ref
= lookup_tag (code
, name
, true, &refloc
);
7762 if (ref
&& TREE_CODE (ref
) == code
)
7764 if (TYPE_STUB_DECL (ref
))
7765 refloc
= DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref
));
7767 if (TYPE_SIZE (ref
))
7769 if (code
== UNION_TYPE
)
7770 error_at (loc
, "redefinition of %<union %E%>", name
);
7772 error_at (loc
, "redefinition of %<struct %E%>", name
);
7773 if (refloc
!= UNKNOWN_LOCATION
)
7774 inform (refloc
, "originally defined here");
7775 /* Don't create structures using a name already in use. */
7778 else if (C_TYPE_BEING_DEFINED (ref
))
7780 if (code
== UNION_TYPE
)
7781 error_at (loc
, "nested redefinition of %<union %E%>", name
);
7783 error_at (loc
, "nested redefinition of %<struct %E%>", name
);
7784 /* Don't bother to report "originally defined here" for a
7785 nested redefinition; the original definition should be
7787 /* Don't create structures that contain themselves. */
7792 /* Otherwise create a forward-reference just so the tag is in scope. */
7794 if (ref
== NULL_TREE
|| TREE_CODE (ref
) != code
)
7796 ref
= make_node (code
);
7797 pushtag (loc
, name
, ref
);
7800 C_TYPE_BEING_DEFINED (ref
) = 1;
7801 for (tree v
= TYPE_MAIN_VARIANT (ref
); v
; v
= TYPE_NEXT_VARIANT (v
))
7802 TYPE_PACKED (v
) = flag_pack_struct
;
7804 *enclosing_struct_parse_info
= struct_parse_info
;
7805 struct_parse_info
= new c_struct_parse_info ();
7807 /* FIXME: This will issue a warning for a use of a type defined
7808 within a statement expr used within sizeof, et. al. This is not
7809 terribly serious as C++ doesn't permit statement exprs within
7811 if (warn_cxx_compat
&& (in_sizeof
|| in_typeof
|| in_alignof
))
7812 warning_at (loc
, OPT_Wc___compat
,
7813 "defining type in %qs expression is invalid in C++",
7816 : (in_typeof
? "typeof" : "alignof")));
7821 /* Process the specs, declarator and width (NULL if omitted)
7822 of a structure component, returning a FIELD_DECL node.
7823 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7824 DECL_ATTRS is as for grokdeclarator.
7826 LOC is the location of the structure component.
7828 This is done during the parsing of the struct declaration.
7829 The FIELD_DECL nodes are chained together and the lot of them
7830 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7833 grokfield (location_t loc
,
7834 struct c_declarator
*declarator
, struct c_declspecs
*declspecs
,
7835 tree width
, tree
*decl_attrs
)
7839 if (declarator
->kind
== cdk_id
&& declarator
->u
.id
== NULL_TREE
7840 && width
== NULL_TREE
)
7842 /* This is an unnamed decl.
7844 If we have something of the form "union { list } ;" then this
7845 is the anonymous union extension. Similarly for struct.
7847 If this is something of the form "struct foo;", then
7848 If MS or Plan 9 extensions are enabled, this is handled as
7849 an anonymous struct.
7850 Otherwise this is a forward declaration of a structure tag.
7852 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7853 If foo names a structure or union without a tag, then this
7854 is an anonymous struct (this is permitted by C11).
7855 If MS or Plan 9 extensions are enabled and foo names a
7856 structure, then again this is an anonymous struct.
7857 Otherwise this is an error.
7859 Oh what a horrid tangled web we weave. I wonder if MS consciously
7860 took this from Plan 9 or if it was an accident of implementation
7861 that took root before someone noticed the bug... */
7863 tree type
= declspecs
->type
;
7866 if (RECORD_OR_UNION_TYPE_P (type
)
7867 && (flag_ms_extensions
7868 || flag_plan9_extensions
7869 || !declspecs
->typedef_p
))
7871 if (flag_ms_extensions
|| flag_plan9_extensions
)
7873 else if (TYPE_NAME (type
) == NULL
)
7880 pedwarn (loc
, 0, "declaration does not declare anything");
7884 pedwarn_c99 (loc
, OPT_Wpedantic
,
7885 "ISO C99 doesn%'t support unnamed structs/unions");
7887 pedwarn_c99 (loc
, OPT_Wpedantic
,
7888 "ISO C90 doesn%'t support unnamed structs/unions");
7891 value
= grokdeclarator (declarator
, declspecs
, FIELD
, false,
7892 width
? &width
: NULL
, decl_attrs
, NULL
, NULL
,
7895 finish_decl (value
, loc
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
7896 DECL_INITIAL (value
) = width
;
7898 SET_DECL_C_BIT_FIELD (value
);
7900 if (warn_cxx_compat
&& DECL_NAME (value
) != NULL_TREE
)
7902 /* If we currently have a binding for this field, set the
7903 in_struct field in the binding, so that we warn about lookups
7905 struct c_binding
*b
= I_SYMBOL_BINDING (DECL_NAME (value
));
7908 /* If the in_struct field is not yet set, push it on a list
7909 to be cleared when this struct is finished. */
7912 struct_parse_info
->fields
.safe_push (b
);
7921 /* Subroutine of detect_field_duplicates: return whether X and Y,
7922 which are both fields in the same struct, have duplicate field
7926 is_duplicate_field (tree x
, tree y
)
7928 if (DECL_NAME (x
) != NULL_TREE
&& DECL_NAME (x
) == DECL_NAME (y
))
7931 /* When using -fplan9-extensions, an anonymous field whose name is a
7932 typedef can duplicate a field name. */
7933 if (flag_plan9_extensions
7934 && (DECL_NAME (x
) == NULL_TREE
|| DECL_NAME (y
) == NULL_TREE
))
7936 tree xt
, xn
, yt
, yn
;
7939 if (DECL_NAME (x
) != NULL_TREE
)
7941 else if (RECORD_OR_UNION_TYPE_P (xt
)
7942 && TYPE_NAME (xt
) != NULL_TREE
7943 && TREE_CODE (TYPE_NAME (xt
)) == TYPE_DECL
)
7944 xn
= DECL_NAME (TYPE_NAME (xt
));
7949 if (DECL_NAME (y
) != NULL_TREE
)
7951 else if (RECORD_OR_UNION_TYPE_P (yt
)
7952 && TYPE_NAME (yt
) != NULL_TREE
7953 && TREE_CODE (TYPE_NAME (yt
)) == TYPE_DECL
)
7954 yn
= DECL_NAME (TYPE_NAME (yt
));
7958 if (xn
!= NULL_TREE
&& xn
== yn
)
7965 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7966 to HTAB, giving errors for any duplicates. */
7969 detect_field_duplicates_hash (tree fieldlist
,
7970 hash_table
<nofree_ptr_hash
<tree_node
> > *htab
)
7975 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
7976 if ((y
= DECL_NAME (x
)) != NULL_TREE
)
7978 slot
= htab
->find_slot (y
, INSERT
);
7981 error ("duplicate member %q+D", x
);
7982 DECL_NAME (x
) = NULL_TREE
;
7986 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
7988 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x
)), htab
);
7990 /* When using -fplan9-extensions, an anonymous field whose
7991 name is a typedef can duplicate a field name. */
7992 if (flag_plan9_extensions
7993 && TYPE_NAME (TREE_TYPE (x
)) != NULL_TREE
7994 && TREE_CODE (TYPE_NAME (TREE_TYPE (x
))) == TYPE_DECL
)
7996 tree xn
= DECL_NAME (TYPE_NAME (TREE_TYPE (x
)));
7997 slot
= htab
->find_slot (xn
, INSERT
);
7999 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x
)));
8005 /* Generate an error for any duplicate field names in FIELDLIST. Munge
8006 the list such that this does not present a problem later. */
8009 detect_field_duplicates (tree fieldlist
)
8014 /* If the struct is the list of instance variables of an Objective-C
8015 class, then we need to check all the instance variables of
8016 superclasses when checking for duplicates (since you can't have
8017 an instance variable in a subclass with the same name as an
8018 instance variable in a superclass). We pass on this job to the
8019 Objective-C compiler. objc_detect_field_duplicates() will return
8020 false if we are not checking the list of instance variables and
8021 the C frontend should proceed with the standard field duplicate
8022 checks. If we are checking the list of instance variables, the
8023 ObjC frontend will do the check, emit the errors if needed, and
8024 then return true. */
8025 if (c_dialect_objc ())
8026 if (objc_detect_field_duplicates (false))
8029 /* First, see if there are more than "a few" fields.
8030 This is trivially true if there are zero or one fields. */
8031 if (!fieldlist
|| !DECL_CHAIN (fieldlist
))
8036 if (DECL_NAME (x
) == NULL_TREE
8037 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
8040 } while (timeout
> 0 && x
);
8042 /* If there were "few" fields and no anonymous structures or unions,
8043 avoid the overhead of allocating a hash table. Instead just do
8044 the nested traversal thing. */
8047 for (x
= DECL_CHAIN (fieldlist
); x
; x
= DECL_CHAIN (x
))
8048 /* When using -fplan9-extensions, we can have duplicates
8049 between typedef names and fields. */
8051 || (flag_plan9_extensions
8052 && DECL_NAME (x
) == NULL_TREE
8053 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
))
8054 && TYPE_NAME (TREE_TYPE (x
)) != NULL_TREE
8055 && TREE_CODE (TYPE_NAME (TREE_TYPE (x
))) == TYPE_DECL
))
8057 for (y
= fieldlist
; y
!= x
; y
= TREE_CHAIN (y
))
8058 if (is_duplicate_field (y
, x
))
8060 error ("duplicate member %q+D", x
);
8061 DECL_NAME (x
) = NULL_TREE
;
8067 hash_table
<nofree_ptr_hash
<tree_node
> > htab (37);
8068 detect_field_duplicates_hash (fieldlist
, &htab
);
8072 /* Finish up struct info used by -Wc++-compat. */
8075 warn_cxx_compat_finish_struct (tree fieldlist
, enum tree_code code
,
8076 location_t record_loc
)
8080 struct c_binding
*b
;
8082 if (fieldlist
== NULL_TREE
)
8084 if (code
== RECORD_TYPE
)
8085 warning_at (record_loc
, OPT_Wc___compat
,
8086 "empty struct has size 0 in C, size 1 in C++");
8088 warning_at (record_loc
, OPT_Wc___compat
,
8089 "empty union has size 0 in C, size 1 in C++");
8092 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
8093 the current struct. We do this now at the end of the struct
8094 because the flag is used to issue visibility warnings, and we
8095 only want to issue those warnings if the type is referenced
8096 outside of the struct declaration. */
8097 FOR_EACH_VEC_ELT (struct_parse_info
->struct_types
, ix
, x
)
8098 C_TYPE_DEFINED_IN_STRUCT (x
) = 1;
8100 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
8101 typedefs used when declaring fields in this struct. If the name
8102 of any of the fields is also a typedef name then the struct would
8103 not parse in C++, because the C++ lookup rules say that the
8104 typedef name would be looked up in the context of the struct, and
8105 would thus be the field rather than the typedef. */
8106 if (!struct_parse_info
->typedefs_seen
.is_empty ()
8107 && fieldlist
!= NULL_TREE
)
8109 /* Use a hash_set<tree> using the name of the typedef. We can use
8110 a hash_set<tree> because identifiers are interned. */
8111 hash_set
<tree
> tset
;
8113 FOR_EACH_VEC_ELT (struct_parse_info
->typedefs_seen
, ix
, x
)
8114 tset
.add (DECL_NAME (x
));
8116 for (x
= fieldlist
; x
!= NULL_TREE
; x
= DECL_CHAIN (x
))
8118 if (DECL_NAME (x
) != NULL_TREE
8119 && tset
.contains (DECL_NAME (x
)))
8121 warning_at (DECL_SOURCE_LOCATION (x
), OPT_Wc___compat
,
8122 ("using %qD as both field and typedef name is "
8125 /* FIXME: It would be nice to report the location where
8126 the typedef name is used. */
8131 /* For each field which has a binding and which was not defined in
8132 an enclosing struct, clear the in_struct field. */
8133 FOR_EACH_VEC_ELT (struct_parse_info
->fields
, ix
, b
)
8137 /* Function to help qsort sort FIELD_DECLs by name order. */
8140 field_decl_cmp (const void *x_p
, const void *y_p
)
8142 const tree
*const x
= (const tree
*) x_p
;
8143 const tree
*const y
= (const tree
*) y_p
;
8145 if (DECL_NAME (*x
) == DECL_NAME (*y
))
8146 /* A nontype is "greater" than a type. */
8147 return (TREE_CODE (*y
) == TYPE_DECL
) - (TREE_CODE (*x
) == TYPE_DECL
);
8148 if (DECL_NAME (*x
) == NULL_TREE
)
8150 if (DECL_NAME (*y
) == NULL_TREE
)
8152 if (DECL_NAME (*x
) < DECL_NAME (*y
))
8157 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
8158 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
8159 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
8160 ATTRIBUTES are attributes to be applied to the structure.
8162 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
8163 the struct was started. */
8166 finish_struct (location_t loc
, tree t
, tree fieldlist
, tree attributes
,
8167 struct c_struct_parse_info
*enclosing_struct_parse_info
)
8170 bool toplevel
= file_scope
== current_scope
;
8172 /* If this type was previously laid out as a forward reference,
8173 make sure we lay it out again. */
8175 TYPE_SIZE (t
) = NULL_TREE
;
8177 decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
8181 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
8183 if (DECL_NAME (x
) != NULL_TREE
)
8185 if (flag_isoc11
&& RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
8191 if (TREE_CODE (t
) == UNION_TYPE
)
8194 pedwarn (loc
, OPT_Wpedantic
, "union has no named members");
8196 pedwarn (loc
, OPT_Wpedantic
, "union has no members");
8201 pedwarn (loc
, OPT_Wpedantic
, "struct has no named members");
8203 pedwarn (loc
, OPT_Wpedantic
, "struct has no members");
8208 /* Install struct as DECL_CONTEXT of each field decl.
8209 Also process specified field sizes, found in the DECL_INITIAL,
8210 storing 0 there after the type has been changed to precision equal
8211 to its width, rather than the precision of the specified standard
8212 type. (Correct layout requires the original type to have been preserved
8215 bool saw_named_field
= false;
8216 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
8218 if (TREE_TYPE (x
) == error_mark_node
)
8221 DECL_CONTEXT (x
) = t
;
8223 /* If any field is const, the structure type is pseudo-const. */
8224 if (TREE_READONLY (x
))
8225 C_TYPE_FIELDS_READONLY (t
) = 1;
8228 /* A field that is pseudo-const makes the structure likewise. */
8229 tree t1
= strip_array_types (TREE_TYPE (x
));
8230 if (RECORD_OR_UNION_TYPE_P (t1
) && C_TYPE_FIELDS_READONLY (t1
))
8231 C_TYPE_FIELDS_READONLY (t
) = 1;
8234 /* Any field that is volatile means variables of this type must be
8235 treated in some ways as volatile. */
8236 if (TREE_THIS_VOLATILE (x
))
8237 C_TYPE_FIELDS_VOLATILE (t
) = 1;
8239 /* Any field of nominal variable size implies structure is too. */
8240 if (C_DECL_VARIABLE_SIZE (x
))
8241 C_TYPE_VARIABLE_SIZE (t
) = 1;
8243 if (DECL_C_BIT_FIELD (x
))
8245 unsigned HOST_WIDE_INT width
= tree_to_uhwi (DECL_INITIAL (x
));
8246 DECL_SIZE (x
) = bitsize_int (width
);
8247 DECL_BIT_FIELD (x
) = 1;
8251 && (DECL_BIT_FIELD (x
)
8252 || TYPE_ALIGN (TREE_TYPE (x
)) > BITS_PER_UNIT
))
8253 DECL_PACKED (x
) = 1;
8255 /* Detect flexible array member in an invalid context. */
8256 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
8257 && TYPE_SIZE (TREE_TYPE (x
)) == NULL_TREE
8258 && TYPE_DOMAIN (TREE_TYPE (x
)) != NULL_TREE
8259 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x
))) == NULL_TREE
)
8261 if (TREE_CODE (t
) == UNION_TYPE
)
8263 error_at (DECL_SOURCE_LOCATION (x
),
8264 "flexible array member in union");
8265 TREE_TYPE (x
) = error_mark_node
;
8267 else if (DECL_CHAIN (x
) != NULL_TREE
)
8269 error_at (DECL_SOURCE_LOCATION (x
),
8270 "flexible array member not at end of struct");
8271 TREE_TYPE (x
) = error_mark_node
;
8273 else if (!saw_named_field
)
8275 error_at (DECL_SOURCE_LOCATION (x
),
8276 "flexible array member in a struct with no named "
8278 TREE_TYPE (x
) = error_mark_node
;
8282 if (pedantic
&& TREE_CODE (t
) == RECORD_TYPE
8283 && flexible_array_type_p (TREE_TYPE (x
)))
8284 pedwarn (DECL_SOURCE_LOCATION (x
), OPT_Wpedantic
,
8285 "invalid use of structure with flexible array member");
8288 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
8289 saw_named_field
= true;
8292 detect_field_duplicates (fieldlist
);
8294 /* Now we have the nearly final fieldlist. Record it,
8295 then lay out the structure or union (including the fields). */
8297 TYPE_FIELDS (t
) = fieldlist
;
8299 maybe_apply_pragma_scalar_storage_order (t
);
8303 if (TYPE_SIZE_UNIT (t
)
8304 && TREE_CODE (TYPE_SIZE_UNIT (t
)) == INTEGER_CST
8305 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t
))
8306 && !valid_constant_size_p (TYPE_SIZE_UNIT (t
)))
8307 error ("type %qT is too large", t
);
8309 /* Give bit-fields their proper types and rewrite the type of array fields
8310 with scalar component if the enclosing type has reverse storage order. */
8311 for (tree field
= fieldlist
; field
; field
= DECL_CHAIN (field
))
8313 if (TREE_CODE (field
) == FIELD_DECL
8314 && DECL_INITIAL (field
)
8315 && TREE_TYPE (field
) != error_mark_node
)
8317 unsigned HOST_WIDE_INT width
8318 = tree_to_uhwi (DECL_INITIAL (field
));
8319 tree type
= TREE_TYPE (field
);
8320 if (width
!= TYPE_PRECISION (type
))
8323 = c_build_bitfield_integer_type (width
, TYPE_UNSIGNED (type
));
8324 SET_DECL_MODE (field
, TYPE_MODE (TREE_TYPE (field
)));
8326 DECL_INITIAL (field
) = NULL_TREE
;
8328 else if (TYPE_REVERSE_STORAGE_ORDER (t
)
8329 && TREE_CODE (field
) == FIELD_DECL
8330 && TREE_CODE (TREE_TYPE (field
)) == ARRAY_TYPE
)
8332 tree ftype
= TREE_TYPE (field
);
8333 tree ctype
= strip_array_types (ftype
);
8334 if (!RECORD_OR_UNION_TYPE_P (ctype
) && TYPE_MODE (ctype
) != QImode
)
8336 tree fmain_type
= TYPE_MAIN_VARIANT (ftype
);
8337 tree
*typep
= &fmain_type
;
8339 *typep
= build_distinct_type_copy (*typep
);
8340 TYPE_REVERSE_STORAGE_ORDER (*typep
) = 1;
8341 typep
= &TREE_TYPE (*typep
);
8342 } while (TREE_CODE (*typep
) == ARRAY_TYPE
);
8344 = c_build_qualified_type (fmain_type
, TYPE_QUALS (ftype
));
8349 /* Now we have the truly final field list.
8350 Store it in this type and in the variants. */
8352 TYPE_FIELDS (t
) = fieldlist
;
8354 /* If there are lots of fields, sort so we can look through them fast.
8355 We arbitrarily consider 16 or more elts to be "a lot". */
8360 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
8362 if (len
> 15 || DECL_NAME (x
) == NULL
)
8370 struct lang_type
*space
;
8371 struct sorted_fields_type
*space2
;
8373 len
+= list_length (x
);
8375 /* Use the same allocation policy here that make_node uses, to
8376 ensure that this lives as long as the rest of the struct decl.
8377 All decls in an inline function need to be saved. */
8379 space
= ggc_cleared_alloc
<struct lang_type
> ();
8380 space2
= (sorted_fields_type
*) ggc_internal_alloc
8381 (sizeof (struct sorted_fields_type
) + len
* sizeof (tree
));
8385 field_array
= &space2
->elts
[0];
8386 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
8388 field_array
[len
++] = x
;
8390 /* If there is anonymous struct or union, break out of the loop. */
8391 if (DECL_NAME (x
) == NULL
)
8394 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8397 TYPE_LANG_SPECIFIC (t
) = space
;
8398 TYPE_LANG_SPECIFIC (t
)->s
->len
= len
;
8399 field_array
= TYPE_LANG_SPECIFIC (t
)->s
->elts
;
8400 qsort (field_array
, len
, sizeof (tree
), field_decl_cmp
);
8405 /* If this was supposed to be a transparent union, but we can't
8406 make it one, warn and turn off the flag. */
8407 if (TREE_CODE (t
) == UNION_TYPE
8408 && TYPE_TRANSPARENT_AGGR (t
)
8409 && (!TYPE_FIELDS (t
) || TYPE_MODE (t
) != DECL_MODE (TYPE_FIELDS (t
))))
8411 TYPE_TRANSPARENT_AGGR (t
) = 0;
8412 warning_at (loc
, 0, "union cannot be made transparent");
8415 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8416 in dwarf2out via rest_of_decl_compilation below and means
8417 something totally different. Since we will be clearing
8418 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8419 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8420 C_TYPE_INCOMPLETE_VARS should use some language specific
8422 tree incomplete_vars
= C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t
));
8423 for (x
= TYPE_MAIN_VARIANT (t
); x
; x
= TYPE_NEXT_VARIANT (x
))
8425 TYPE_FIELDS (x
) = TYPE_FIELDS (t
);
8426 TYPE_LANG_SPECIFIC (x
) = TYPE_LANG_SPECIFIC (t
);
8427 TYPE_TRANSPARENT_AGGR (x
) = TYPE_TRANSPARENT_AGGR (t
);
8428 C_TYPE_FIELDS_READONLY (x
) = C_TYPE_FIELDS_READONLY (t
);
8429 C_TYPE_FIELDS_VOLATILE (x
) = C_TYPE_FIELDS_VOLATILE (t
);
8430 C_TYPE_VARIABLE_SIZE (x
) = C_TYPE_VARIABLE_SIZE (t
);
8431 C_TYPE_INCOMPLETE_VARS (x
) = NULL_TREE
;
8434 /* Update type location to the one of the definition, instead of e.g.
8435 a forward declaration. */
8436 if (TYPE_STUB_DECL (t
))
8437 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t
)) = loc
;
8439 /* Finish debugging output for this type. */
8440 rest_of_type_compilation (t
, toplevel
);
8442 /* If this structure or union completes the type of any previous
8443 variable declaration, lay it out and output its rtl. */
8444 for (x
= incomplete_vars
; x
; x
= TREE_CHAIN (x
))
8446 tree decl
= TREE_VALUE (x
);
8447 if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
8448 layout_array_type (TREE_TYPE (decl
));
8449 if (TREE_CODE (decl
) != TYPE_DECL
)
8451 layout_decl (decl
, 0);
8452 if (c_dialect_objc ())
8453 objc_check_decl (decl
);
8454 rest_of_decl_compilation (decl
, toplevel
, 0);
8458 /* If we're inside a function proper, i.e. not file-scope and not still
8459 parsing parameters, then arrange for the size of a variable sized type
8461 if (building_stmt_list_p () && variably_modified_type_p (t
, NULL_TREE
))
8462 add_stmt (build_stmt (loc
,
8463 DECL_EXPR
, build_decl (loc
, TYPE_DECL
, NULL
, t
)));
8465 if (warn_cxx_compat
)
8466 warn_cxx_compat_finish_struct (fieldlist
, TREE_CODE (t
), loc
);
8468 delete struct_parse_info
;
8470 struct_parse_info
= enclosing_struct_parse_info
;
8472 /* If this struct is defined inside a struct, add it to
8475 && struct_parse_info
!= NULL
8476 && !in_sizeof
&& !in_typeof
&& !in_alignof
)
8477 struct_parse_info
->struct_types
.safe_push (t
);
8483 gt_pointer_operator new_value
;
8487 /* This routine compares two fields like field_decl_cmp but using the
8488 pointer operator in resort_data. */
8491 resort_field_decl_cmp (const void *x_p
, const void *y_p
)
8493 const tree
*const x
= (const tree
*) x_p
;
8494 const tree
*const y
= (const tree
*) y_p
;
8496 if (DECL_NAME (*x
) == DECL_NAME (*y
))
8497 /* A nontype is "greater" than a type. */
8498 return (TREE_CODE (*y
) == TYPE_DECL
) - (TREE_CODE (*x
) == TYPE_DECL
);
8499 if (DECL_NAME (*x
) == NULL_TREE
)
8501 if (DECL_NAME (*y
) == NULL_TREE
)
8504 tree d1
= DECL_NAME (*x
);
8505 tree d2
= DECL_NAME (*y
);
8506 resort_data
.new_value (&d1
, resort_data
.cookie
);
8507 resort_data
.new_value (&d2
, resort_data
.cookie
);
8514 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8517 resort_sorted_fields (void *obj
,
8518 void * ARG_UNUSED (orig_obj
),
8519 gt_pointer_operator new_value
,
8522 struct sorted_fields_type
*sf
= (struct sorted_fields_type
*) obj
;
8523 resort_data
.new_value
= new_value
;
8524 resort_data
.cookie
= cookie
;
8525 qsort (&sf
->elts
[0], sf
->len
, sizeof (tree
),
8526 resort_field_decl_cmp
);
8529 /* Lay out the type T, and its element type, and so on. */
8532 layout_array_type (tree t
)
8534 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
8535 layout_array_type (TREE_TYPE (t
));
8539 /* Begin compiling the definition of an enumeration type.
8540 NAME is its name (or null if anonymous).
8541 LOC is the enum's location.
8542 Returns the type object, as yet incomplete.
8543 Also records info about it so that build_enumerator
8544 may be used to declare the individual values as they are read. */
8547 start_enum (location_t loc
, struct c_enum_contents
*the_enum
, tree name
)
8549 tree enumtype
= NULL_TREE
;
8550 location_t enumloc
= UNKNOWN_LOCATION
;
8552 /* If this is the real definition for a previous forward reference,
8553 fill in the contents in the same object that used to be the
8554 forward reference. */
8556 if (name
!= NULL_TREE
)
8557 enumtype
= lookup_tag (ENUMERAL_TYPE
, name
, true, &enumloc
);
8559 if (enumtype
== NULL_TREE
|| TREE_CODE (enumtype
) != ENUMERAL_TYPE
)
8561 enumtype
= make_node (ENUMERAL_TYPE
);
8562 pushtag (loc
, name
, enumtype
);
8564 /* Update type location to the one of the definition, instead of e.g.
8565 a forward declaration. */
8566 else if (TYPE_STUB_DECL (enumtype
))
8568 enumloc
= DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype
));
8569 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype
)) = loc
;
8572 if (C_TYPE_BEING_DEFINED (enumtype
))
8573 error_at (loc
, "nested redefinition of %<enum %E%>", name
);
8575 C_TYPE_BEING_DEFINED (enumtype
) = 1;
8577 if (TYPE_VALUES (enumtype
) != NULL_TREE
)
8579 /* This enum is a named one that has been declared already. */
8580 error_at (loc
, "redeclaration of %<enum %E%>", name
);
8581 if (enumloc
!= UNKNOWN_LOCATION
)
8582 inform (enumloc
, "originally defined here");
8584 /* Completely replace its old definition.
8585 The old enumerators remain defined, however. */
8586 TYPE_VALUES (enumtype
) = NULL_TREE
;
8589 the_enum
->enum_next_value
= integer_zero_node
;
8590 the_enum
->enum_overflow
= 0;
8592 if (flag_short_enums
)
8593 for (tree v
= TYPE_MAIN_VARIANT (enumtype
); v
; v
= TYPE_NEXT_VARIANT (v
))
8594 TYPE_PACKED (v
) = 1;
8596 /* FIXME: This will issue a warning for a use of a type defined
8597 within sizeof in a statement expr. This is not terribly serious
8598 as C++ doesn't permit statement exprs within sizeof anyhow. */
8599 if (warn_cxx_compat
&& (in_sizeof
|| in_typeof
|| in_alignof
))
8600 warning_at (loc
, OPT_Wc___compat
,
8601 "defining type in %qs expression is invalid in C++",
8604 : (in_typeof
? "typeof" : "alignof")));
8609 /* After processing and defining all the values of an enumeration type,
8610 install their decls in the enumeration type and finish it off.
8611 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8612 and ATTRIBUTES are the specified attributes.
8613 Returns ENUMTYPE. */
8616 finish_enum (tree enumtype
, tree values
, tree attributes
)
8619 tree minnode
= NULL_TREE
, maxnode
= NULL_TREE
;
8622 bool toplevel
= (file_scope
== current_scope
);
8623 struct lang_type
*lt
;
8625 decl_attributes (&enumtype
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
8627 /* Calculate the maximum value of any enumerator in this type. */
8629 if (values
== error_mark_node
)
8630 minnode
= maxnode
= integer_zero_node
;
8633 minnode
= maxnode
= TREE_VALUE (values
);
8634 for (pair
= TREE_CHAIN (values
); pair
; pair
= TREE_CHAIN (pair
))
8636 tree value
= TREE_VALUE (pair
);
8637 if (tree_int_cst_lt (maxnode
, value
))
8639 if (tree_int_cst_lt (value
, minnode
))
8644 /* Construct the final type of this enumeration. It is the same
8645 as one of the integral types - the narrowest one that fits, except
8646 that normally we only go as narrow as int - and signed iff any of
8647 the values are negative. */
8648 sign
= (tree_int_cst_sgn (minnode
) >= 0) ? UNSIGNED
: SIGNED
;
8649 precision
= MAX (tree_int_cst_min_precision (minnode
, sign
),
8650 tree_int_cst_min_precision (maxnode
, sign
));
8652 /* If the precision of the type was specified with an attribute and it
8653 was too small, give an error. Otherwise, use it. */
8654 if (TYPE_PRECISION (enumtype
) && lookup_attribute ("mode", attributes
))
8656 if (precision
> TYPE_PRECISION (enumtype
))
8658 TYPE_PRECISION (enumtype
) = 0;
8659 error ("specified mode too small for enumeral values");
8662 precision
= TYPE_PRECISION (enumtype
);
8665 TYPE_PRECISION (enumtype
) = 0;
8667 if (TYPE_PACKED (enumtype
)
8668 || precision
> TYPE_PRECISION (integer_type_node
)
8669 || TYPE_PRECISION (enumtype
))
8671 tem
= c_common_type_for_size (precision
, sign
== UNSIGNED
? 1 : 0);
8674 warning (0, "enumeration values exceed range of largest integer");
8675 tem
= long_long_integer_type_node
;
8679 tem
= sign
== UNSIGNED
? unsigned_type_node
: integer_type_node
;
8681 TYPE_MIN_VALUE (enumtype
) = TYPE_MIN_VALUE (tem
);
8682 TYPE_MAX_VALUE (enumtype
) = TYPE_MAX_VALUE (tem
);
8683 TYPE_UNSIGNED (enumtype
) = TYPE_UNSIGNED (tem
);
8684 SET_TYPE_ALIGN (enumtype
, TYPE_ALIGN (tem
));
8685 TYPE_SIZE (enumtype
) = NULL_TREE
;
8686 TYPE_PRECISION (enumtype
) = TYPE_PRECISION (tem
);
8688 layout_type (enumtype
);
8690 if (values
!= error_mark_node
)
8692 /* Change the type of the enumerators to be the enum type. We
8693 need to do this irrespective of the size of the enum, for
8694 proper type checking. Replace the DECL_INITIALs of the
8695 enumerators, and the value slots of the list, with copies
8696 that have the enum type; they cannot be modified in place
8697 because they may be shared (e.g. integer_zero_node) Finally,
8698 change the purpose slots to point to the names of the decls. */
8699 for (pair
= values
; pair
; pair
= TREE_CHAIN (pair
))
8701 tree enu
= TREE_PURPOSE (pair
);
8702 tree ini
= DECL_INITIAL (enu
);
8704 TREE_TYPE (enu
) = enumtype
;
8706 /* The ISO C Standard mandates enumerators to have type int,
8707 even though the underlying type of an enum type is
8708 unspecified. However, GCC allows enumerators of any
8709 integer type as an extensions. build_enumerator()
8710 converts any enumerators that fit in an int to type int,
8711 to avoid promotions to unsigned types when comparing
8712 integers with enumerators that fit in the int range.
8713 When -pedantic is given, build_enumerator() would have
8714 already warned about those that don't fit. Here we
8715 convert the rest to the enumerator type. */
8716 if (TREE_TYPE (ini
) != integer_type_node
)
8717 ini
= convert (enumtype
, ini
);
8719 DECL_INITIAL (enu
) = ini
;
8720 TREE_PURPOSE (pair
) = DECL_NAME (enu
);
8721 TREE_VALUE (pair
) = ini
;
8724 TYPE_VALUES (enumtype
) = values
;
8727 /* Record the min/max values so that we can warn about bit-field
8728 enumerations that are too small for the values. */
8729 lt
= ggc_cleared_alloc
<struct lang_type
> ();
8730 lt
->enum_min
= minnode
;
8731 lt
->enum_max
= maxnode
;
8732 TYPE_LANG_SPECIFIC (enumtype
) = lt
;
8734 /* Fix up all variant types of this enum type. */
8735 for (tem
= TYPE_MAIN_VARIANT (enumtype
); tem
; tem
= TYPE_NEXT_VARIANT (tem
))
8737 if (tem
== enumtype
)
8739 TYPE_VALUES (tem
) = TYPE_VALUES (enumtype
);
8740 TYPE_MIN_VALUE (tem
) = TYPE_MIN_VALUE (enumtype
);
8741 TYPE_MAX_VALUE (tem
) = TYPE_MAX_VALUE (enumtype
);
8742 TYPE_SIZE (tem
) = TYPE_SIZE (enumtype
);
8743 TYPE_SIZE_UNIT (tem
) = TYPE_SIZE_UNIT (enumtype
);
8744 SET_TYPE_MODE (tem
, TYPE_MODE (enumtype
));
8745 TYPE_PRECISION (tem
) = TYPE_PRECISION (enumtype
);
8746 SET_TYPE_ALIGN (tem
, TYPE_ALIGN (enumtype
));
8747 TYPE_USER_ALIGN (tem
) = TYPE_USER_ALIGN (enumtype
);
8748 TYPE_UNSIGNED (tem
) = TYPE_UNSIGNED (enumtype
);
8749 TYPE_LANG_SPECIFIC (tem
) = TYPE_LANG_SPECIFIC (enumtype
);
8752 /* Finish debugging output for this type. */
8753 rest_of_type_compilation (enumtype
, toplevel
);
8755 /* If this enum is defined inside a struct, add it to
8758 && struct_parse_info
!= NULL
8759 && !in_sizeof
&& !in_typeof
&& !in_alignof
)
8760 struct_parse_info
->struct_types
.safe_push (enumtype
);
8765 /* Build and install a CONST_DECL for one value of the
8766 current enumeration type (one that was begun with start_enum).
8767 DECL_LOC is the location of the enumerator.
8768 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8769 Return a tree-list containing the CONST_DECL and its value.
8770 Assignment of sequential values by default is handled here. */
8773 build_enumerator (location_t decl_loc
, location_t loc
,
8774 struct c_enum_contents
*the_enum
, tree name
, tree value
)
8778 /* Validate and default VALUE. */
8780 if (value
!= NULL_TREE
)
8782 /* Don't issue more errors for error_mark_node (i.e. an
8783 undeclared identifier) - just ignore the value expression. */
8784 if (value
== error_mark_node
)
8786 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value
)))
8788 error_at (loc
, "enumerator value for %qE is not an integer constant",
8794 if (TREE_CODE (value
) != INTEGER_CST
)
8796 value
= c_fully_fold (value
, false, NULL
);
8797 if (TREE_CODE (value
) == INTEGER_CST
)
8798 pedwarn (loc
, OPT_Wpedantic
,
8799 "enumerator value for %qE is not an integer "
8800 "constant expression", name
);
8802 if (TREE_CODE (value
) != INTEGER_CST
)
8804 error ("enumerator value for %qE is not an integer constant",
8810 value
= default_conversion (value
);
8811 constant_expression_warning (value
);
8816 /* Default based on previous value. */
8817 /* It should no longer be possible to have NON_LVALUE_EXPR
8819 if (value
== NULL_TREE
)
8821 value
= the_enum
->enum_next_value
;
8822 if (the_enum
->enum_overflow
)
8823 error_at (loc
, "overflow in enumeration values");
8825 /* Even though the underlying type of an enum is unspecified, the
8826 type of enumeration constants is explicitly defined as int
8827 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8829 else if (!int_fits_type_p (value
, integer_type_node
))
8830 pedwarn (loc
, OPT_Wpedantic
,
8831 "ISO C restricts enumerator values to range of %<int%>");
8833 /* The ISO C Standard mandates enumerators to have type int, even
8834 though the underlying type of an enum type is unspecified.
8835 However, GCC allows enumerators of any integer type as an
8836 extensions. Here we convert any enumerators that fit in an int
8837 to type int, to avoid promotions to unsigned types when comparing
8838 integers with enumerators that fit in the int range. When
8839 -pedantic is given, we would have already warned about those that
8840 don't fit. We have to do this here rather than in finish_enum
8841 because this value may be used to define more enumerators. */
8842 if (int_fits_type_p (value
, integer_type_node
))
8843 value
= convert (integer_type_node
, value
);
8845 /* Set basis for default for next value. */
8846 the_enum
->enum_next_value
8847 = build_binary_op (EXPR_LOC_OR_LOC (value
, input_location
),
8848 PLUS_EXPR
, value
, integer_one_node
, false);
8849 the_enum
->enum_overflow
= tree_int_cst_lt (the_enum
->enum_next_value
, value
);
8851 /* Now create a declaration for the enum value name. */
8853 type
= TREE_TYPE (value
);
8854 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
8855 TYPE_PRECISION (integer_type_node
)),
8856 (TYPE_PRECISION (type
)
8857 >= TYPE_PRECISION (integer_type_node
)
8858 && TYPE_UNSIGNED (type
)));
8860 decl
= build_decl (decl_loc
, CONST_DECL
, name
, type
);
8861 DECL_INITIAL (decl
) = convert (type
, value
);
8864 return tree_cons (decl
, value
, NULL_TREE
);
8868 /* Create the FUNCTION_DECL for a function definition.
8869 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8870 the declaration; they describe the function's name and the type it returns,
8871 but twisted together in a fashion that parallels the syntax of C.
8873 This function creates a binding context for the function body
8874 as well as setting up the FUNCTION_DECL in current_function_decl.
8876 Returns true on success. If the DECLARATOR is not suitable for a function
8877 (it defines a datum instead), we return false to report a parse error. */
8880 start_function (struct c_declspecs
*declspecs
, struct c_declarator
*declarator
,
8883 tree decl1
, old_decl
;
8884 tree restype
, resdecl
;
8887 current_function_returns_value
= 0; /* Assume, until we see it does. */
8888 current_function_returns_null
= 0;
8889 current_function_returns_abnormally
= 0;
8890 warn_about_return_type
= 0;
8891 c_switch_stack
= NULL
;
8893 /* Indicate no valid break/continue context by setting these variables
8894 to some non-null, non-label value. We'll notice and emit the proper
8895 error message in c_finish_bc_stmt. */
8896 c_break_label
= c_cont_label
= size_zero_node
;
8898 decl1
= grokdeclarator (declarator
, declspecs
, FUNCDEF
, true, NULL
,
8899 &attributes
, NULL
, NULL
, DEPRECATED_NORMAL
);
8900 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION
, decl1
);
8902 /* If the declarator is not suitable for a function definition,
8903 cause a syntax error. */
8904 if (decl1
== NULL_TREE
8905 || TREE_CODE (decl1
) != FUNCTION_DECL
)
8908 loc
= DECL_SOURCE_LOCATION (decl1
);
8910 /* A nested function is not global. */
8911 if (current_function_decl
!= NULL_TREE
)
8912 TREE_PUBLIC (decl1
) = 0;
8914 c_decl_attributes (&decl1
, attributes
, 0);
8916 if (DECL_DECLARED_INLINE_P (decl1
)
8917 && DECL_UNINLINABLE (decl1
)
8918 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1
)))
8919 warning_at (loc
, OPT_Wattributes
,
8920 "inline function %qD given attribute noinline",
8923 /* Handle gnu_inline attribute. */
8924 if (declspecs
->inline_p
8925 && !flag_gnu89_inline
8926 && TREE_CODE (decl1
) == FUNCTION_DECL
8927 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1
))
8928 || current_function_decl
))
8930 if (declspecs
->storage_class
!= csc_static
)
8931 DECL_EXTERNAL (decl1
) = !DECL_EXTERNAL (decl1
);
8934 announce_function (decl1
);
8936 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1
))))
8938 error_at (loc
, "return type is an incomplete type");
8939 /* Make it return void instead. */
8941 = build_function_type (void_type_node
,
8942 TYPE_ARG_TYPES (TREE_TYPE (decl1
)));
8945 if (warn_about_return_type
)
8946 warn_defaults_to (loc
, flag_isoc99
? OPT_Wimplicit_int
8947 : (warn_return_type
> 0 ? OPT_Wreturn_type
8948 : OPT_Wimplicit_int
),
8949 "return type defaults to %<int%>");
8951 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8952 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8953 DECL_INITIAL (decl1
) = error_mark_node
;
8955 /* If this definition isn't a prototype and we had a prototype declaration
8956 before, copy the arg type info from that prototype. */
8957 old_decl
= lookup_name_in_scope (DECL_NAME (decl1
), current_scope
);
8958 if (old_decl
&& TREE_CODE (old_decl
) != FUNCTION_DECL
)
8959 old_decl
= NULL_TREE
;
8960 current_function_prototype_locus
= UNKNOWN_LOCATION
;
8961 current_function_prototype_built_in
= false;
8962 current_function_prototype_arg_types
= NULL_TREE
;
8963 if (!prototype_p (TREE_TYPE (decl1
)))
8965 if (old_decl
!= NULL_TREE
8966 && TREE_CODE (TREE_TYPE (old_decl
)) == FUNCTION_TYPE
8967 && comptypes (TREE_TYPE (TREE_TYPE (decl1
)),
8968 TREE_TYPE (TREE_TYPE (old_decl
))))
8970 if (stdarg_p (TREE_TYPE (old_decl
)))
8972 auto_diagnostic_group d
;
8973 warning_at (loc
, 0, "%q+D defined as variadic function "
8974 "without prototype", decl1
);
8975 locate_old_decl (old_decl
);
8977 TREE_TYPE (decl1
) = composite_type (TREE_TYPE (old_decl
),
8979 current_function_prototype_locus
= DECL_SOURCE_LOCATION (old_decl
);
8980 current_function_prototype_built_in
8981 = C_DECL_BUILTIN_PROTOTYPE (old_decl
);
8982 current_function_prototype_arg_types
8983 = TYPE_ARG_TYPES (TREE_TYPE (decl1
));
8985 if (TREE_PUBLIC (decl1
))
8987 /* If there is an external prototype declaration of this
8988 function, record its location but do not copy information
8989 to this decl. This may be an invisible declaration
8990 (built-in or in a scope which has finished) or simply
8991 have more refined argument types than any declaration
8993 struct c_binding
*b
;
8994 for (b
= I_SYMBOL_BINDING (DECL_NAME (decl1
)); b
; b
= b
->shadowed
)
8995 if (B_IN_SCOPE (b
, external_scope
))
8999 tree ext_decl
, ext_type
;
9001 ext_type
= b
->u
.type
? b
->u
.type
: TREE_TYPE (ext_decl
);
9002 if (TREE_CODE (ext_type
) == FUNCTION_TYPE
9003 && comptypes (TREE_TYPE (TREE_TYPE (decl1
)),
9004 TREE_TYPE (ext_type
)))
9006 current_function_prototype_locus
9007 = DECL_SOURCE_LOCATION (ext_decl
);
9008 current_function_prototype_built_in
9009 = C_DECL_BUILTIN_PROTOTYPE (ext_decl
);
9010 current_function_prototype_arg_types
9011 = TYPE_ARG_TYPES (ext_type
);
9017 /* Optionally warn of old-fashioned def with no previous prototype. */
9018 if (warn_strict_prototypes
9019 && old_decl
!= error_mark_node
9020 && !prototype_p (TREE_TYPE (decl1
))
9021 && C_DECL_ISNT_PROTOTYPE (old_decl
))
9022 warning_at (loc
, OPT_Wstrict_prototypes
,
9023 "function declaration isn%'t a prototype");
9024 /* Optionally warn of any global def with no previous prototype. */
9025 else if (warn_missing_prototypes
9026 && old_decl
!= error_mark_node
9027 && TREE_PUBLIC (decl1
)
9028 && !MAIN_NAME_P (DECL_NAME (decl1
))
9029 && C_DECL_ISNT_PROTOTYPE (old_decl
)
9030 && !DECL_DECLARED_INLINE_P (decl1
))
9031 warning_at (loc
, OPT_Wmissing_prototypes
,
9032 "no previous prototype for %qD", decl1
);
9033 /* Optionally warn of any def with no previous prototype
9034 if the function has already been used. */
9035 else if (warn_missing_prototypes
9036 && old_decl
!= NULL_TREE
9037 && old_decl
!= error_mark_node
9038 && TREE_USED (old_decl
)
9039 && !prototype_p (TREE_TYPE (old_decl
)))
9040 warning_at (loc
, OPT_Wmissing_prototypes
,
9041 "%qD was used with no prototype before its definition", decl1
);
9042 /* Optionally warn of any global def with no previous declaration. */
9043 else if (warn_missing_declarations
9044 && TREE_PUBLIC (decl1
)
9045 && old_decl
== NULL_TREE
9046 && !MAIN_NAME_P (DECL_NAME (decl1
))
9047 && !DECL_DECLARED_INLINE_P (decl1
))
9048 warning_at (loc
, OPT_Wmissing_declarations
,
9049 "no previous declaration for %qD",
9051 /* Optionally warn of any def with no previous declaration
9052 if the function has already been used. */
9053 else if (warn_missing_declarations
9054 && old_decl
!= NULL_TREE
9055 && old_decl
!= error_mark_node
9056 && TREE_USED (old_decl
)
9057 && C_DECL_IMPLICIT (old_decl
))
9058 warning_at (loc
, OPT_Wmissing_declarations
,
9059 "%qD was used with no declaration before its definition", decl1
);
9061 /* This function exists in static storage.
9062 (This does not mean `static' in the C sense!) */
9063 TREE_STATIC (decl1
) = 1;
9065 /* This is the earliest point at which we might know the assembler
9066 name of the function. Thus, if it's set before this, die horribly. */
9067 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1
));
9069 /* If #pragma weak was used, mark the decl weak now. */
9070 if (current_scope
== file_scope
)
9071 maybe_apply_pragma_weak (decl1
);
9073 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
9074 if (warn_main
&& MAIN_NAME_P (DECL_NAME (decl1
)))
9076 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1
)))
9077 != integer_type_node
)
9078 pedwarn (loc
, OPT_Wmain
, "return type of %qD is not %<int%>", decl1
);
9079 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1
))))
9080 pedwarn (loc
, OPT_Wmain
, "%<_Atomic%>-qualified return type of %qD",
9083 check_main_parameter_types (decl1
);
9085 if (!TREE_PUBLIC (decl1
))
9086 pedwarn (loc
, OPT_Wmain
,
9087 "%qD is normally a non-static function", decl1
);
9090 /* Record the decl so that the function name is defined.
9091 If we already have a decl for this name, and it is a FUNCTION_DECL,
9092 use the old decl. */
9094 current_function_decl
= pushdecl (decl1
);
9097 declare_parm_level ();
9099 restype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
9100 resdecl
= build_decl (loc
, RESULT_DECL
, NULL_TREE
, restype
);
9101 DECL_ARTIFICIAL (resdecl
) = 1;
9102 DECL_IGNORED_P (resdecl
) = 1;
9103 DECL_RESULT (current_function_decl
) = resdecl
;
9105 start_fname_decls ();
9110 /* Subroutine of store_parm_decls which handles new-style function
9111 definitions (prototype format). The parms already have decls, so we
9112 need only record them as in effect and complain if any redundant
9113 old-style parm decls were written. */
9115 store_parm_decls_newstyle (tree fndecl
, const struct c_arg_info
*arg_info
)
9121 if (current_scope
->bindings
)
9123 error_at (DECL_SOURCE_LOCATION (fndecl
),
9124 "old-style parameter declarations in prototyped "
9125 "function definition");
9127 /* Get rid of the old-style declarations. */
9131 /* Don't issue this warning for nested functions, and don't issue this
9132 warning if we got here because ARG_INFO_TYPES was error_mark_node
9133 (this happens when a function definition has just an ellipsis in
9134 its parameter list). */
9135 else if (!in_system_header_at (input_location
)
9136 && !current_function_scope
9137 && arg_info
->types
!= error_mark_node
)
9138 warning_at (DECL_SOURCE_LOCATION (fndecl
), OPT_Wtraditional
,
9139 "traditional C rejects ISO C style function definitions");
9141 /* Now make all the parameter declarations visible in the function body.
9142 We can bypass most of the grunt work of pushdecl. */
9143 for (decl
= arg_info
->parms
; decl
; decl
= DECL_CHAIN (decl
))
9145 DECL_CONTEXT (decl
) = current_function_decl
;
9146 if (DECL_NAME (decl
))
9148 bind (DECL_NAME (decl
), decl
, current_scope
,
9149 /*invisible=*/false, /*nested=*/false,
9151 if (!TREE_USED (decl
))
9152 warn_if_shadowing (decl
);
9155 error_at (DECL_SOURCE_LOCATION (decl
), "parameter name omitted");
9158 /* Record the parameter list in the function declaration. */
9159 DECL_ARGUMENTS (fndecl
) = arg_info
->parms
;
9161 /* Now make all the ancillary declarations visible, likewise. */
9162 for (decl
= arg_info
->others
; decl
; decl
= DECL_CHAIN (decl
))
9164 DECL_CONTEXT (decl
) = current_function_decl
;
9165 if (DECL_NAME (decl
))
9166 bind (DECL_NAME (decl
), decl
, current_scope
,
9167 /*invisible=*/false,
9168 /*nested=*/(TREE_CODE (decl
) == FUNCTION_DECL
),
9172 /* And all the tag declarations. */
9173 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info
->tags
, ix
, tag
)
9175 bind (tag
->id
, tag
->type
, current_scope
,
9176 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
9179 /* Subroutine of store_parm_decls which handles old-style function
9180 definitions (separate parameter list and declarations). */
9183 store_parm_decls_oldstyle (tree fndecl
, const struct c_arg_info
*arg_info
)
9185 struct c_binding
*b
;
9186 tree parm
, decl
, last
;
9187 tree parmids
= arg_info
->parms
;
9188 hash_set
<tree
> seen_args
;
9190 if (!in_system_header_at (input_location
))
9191 warning_at (DECL_SOURCE_LOCATION (fndecl
),
9192 OPT_Wold_style_definition
, "old-style function definition");
9194 /* Match each formal parameter name with its declaration. Save each
9195 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
9196 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
9198 if (TREE_VALUE (parm
) == NULL_TREE
)
9200 error_at (DECL_SOURCE_LOCATION (fndecl
),
9201 "parameter name missing from parameter list");
9202 TREE_PURPOSE (parm
) = NULL_TREE
;
9206 b
= I_SYMBOL_BINDING (TREE_VALUE (parm
));
9207 if (b
&& B_IN_CURRENT_SCOPE (b
))
9210 /* Skip erroneous parameters. */
9211 if (decl
== error_mark_node
)
9213 /* If we got something other than a PARM_DECL it is an error. */
9214 if (TREE_CODE (decl
) != PARM_DECL
)
9216 error_at (DECL_SOURCE_LOCATION (decl
),
9217 "%qD declared as a non-parameter", decl
);
9220 /* If the declaration is already marked, we have a duplicate
9221 name. Complain and ignore the duplicate. */
9222 else if (seen_args
.contains (decl
))
9224 error_at (DECL_SOURCE_LOCATION (decl
),
9225 "multiple parameters named %qD", decl
);
9226 TREE_PURPOSE (parm
) = NULL_TREE
;
9229 /* If the declaration says "void", complain and turn it into
9231 else if (VOID_TYPE_P (TREE_TYPE (decl
)))
9233 error_at (DECL_SOURCE_LOCATION (decl
),
9234 "parameter %qD declared with void type", decl
);
9235 TREE_TYPE (decl
) = integer_type_node
;
9236 DECL_ARG_TYPE (decl
) = integer_type_node
;
9237 layout_decl (decl
, 0);
9239 warn_if_shadowing (decl
);
9241 /* If no declaration found, default to int. */
9244 /* FIXME diagnostics: This should be the location of the argument,
9245 not the FNDECL. E.g., for an old-style declaration
9247 int f10(v) { blah; }
9249 We should use the location of the V, not the F10.
9250 Unfortunately, the V is an IDENTIFIER_NODE which has no
9251 location. In the future we need locations for c_arg_info
9254 See gcc.dg/Wshadow-3.c for an example of this problem. */
9255 decl
= build_decl (DECL_SOURCE_LOCATION (fndecl
),
9256 PARM_DECL
, TREE_VALUE (parm
), integer_type_node
);
9257 DECL_ARG_TYPE (decl
) = TREE_TYPE (decl
);
9259 warn_if_shadowing (decl
);
9262 pedwarn (DECL_SOURCE_LOCATION (decl
),
9263 OPT_Wimplicit_int
, "type of %qD defaults to %<int%>",
9266 warning_at (DECL_SOURCE_LOCATION (decl
),
9267 OPT_Wmissing_parameter_type
,
9268 "type of %qD defaults to %<int%>", decl
);
9271 TREE_PURPOSE (parm
) = decl
;
9272 seen_args
.add (decl
);
9275 /* Now examine the parms chain for incomplete declarations
9276 and declarations with no corresponding names. */
9278 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
9281 if (TREE_CODE (parm
) != PARM_DECL
)
9284 if (TREE_TYPE (parm
) != error_mark_node
9285 && !COMPLETE_TYPE_P (TREE_TYPE (parm
)))
9287 error_at (DECL_SOURCE_LOCATION (parm
),
9288 "parameter %qD has incomplete type", parm
);
9289 TREE_TYPE (parm
) = error_mark_node
;
9292 if (!seen_args
.contains (parm
))
9294 error_at (DECL_SOURCE_LOCATION (parm
),
9295 "declaration for parameter %qD but no such parameter",
9298 /* Pretend the parameter was not missing.
9299 This gets us to a standard state and minimizes
9300 further error messages. */
9301 parmids
= chainon (parmids
, tree_cons (parm
, 0, 0));
9305 /* Chain the declarations together in the order of the list of
9306 names. Store that chain in the function decl, replacing the
9307 list of names. Update the current scope to match. */
9308 DECL_ARGUMENTS (fndecl
) = NULL_TREE
;
9310 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
9311 if (TREE_PURPOSE (parm
))
9313 if (parm
&& TREE_PURPOSE (parm
))
9315 last
= TREE_PURPOSE (parm
);
9316 DECL_ARGUMENTS (fndecl
) = last
;
9318 for (parm
= TREE_CHAIN (parm
); parm
; parm
= TREE_CHAIN (parm
))
9319 if (TREE_PURPOSE (parm
))
9321 DECL_CHAIN (last
) = TREE_PURPOSE (parm
);
9322 last
= TREE_PURPOSE (parm
);
9324 DECL_CHAIN (last
) = NULL_TREE
;
9327 /* If there was a previous prototype,
9328 set the DECL_ARG_TYPE of each argument according to
9329 the type previously specified, and report any mismatches. */
9331 if (current_function_prototype_arg_types
)
9334 for (parm
= DECL_ARGUMENTS (fndecl
),
9335 type
= current_function_prototype_arg_types
;
9336 parm
|| (type
!= NULL_TREE
9337 && TREE_VALUE (type
) != error_mark_node
9338 && TYPE_MAIN_VARIANT (TREE_VALUE (type
)) != void_type_node
);
9339 parm
= DECL_CHAIN (parm
), type
= TREE_CHAIN (type
))
9341 if (parm
== NULL_TREE
9342 || type
== NULL_TREE
9343 || (TREE_VALUE (type
) != error_mark_node
9344 && TYPE_MAIN_VARIANT (TREE_VALUE (type
)) == void_type_node
))
9346 if (current_function_prototype_built_in
)
9347 warning_at (DECL_SOURCE_LOCATION (fndecl
),
9348 0, "number of arguments doesn%'t match "
9349 "built-in prototype");
9352 /* FIXME diagnostics: This should be the location of
9353 FNDECL, but there is bug when a prototype is
9354 declared inside function context, but defined
9355 outside of it (e.g., gcc.dg/pr15698-2.c). In
9356 which case FNDECL gets the location of the
9357 prototype, not the definition. */
9358 error_at (input_location
,
9359 "number of arguments doesn%'t match prototype");
9361 error_at (current_function_prototype_locus
,
9362 "prototype declaration");
9366 /* Type for passing arg must be consistent with that
9367 declared for the arg. ISO C says we take the unqualified
9368 type for parameters declared with qualified type. */
9369 if (TREE_TYPE (parm
) != error_mark_node
9370 && TREE_VALUE (type
) != error_mark_node
9371 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm
))
9372 != TYPE_ATOMIC (TREE_VALUE (type
)))
9373 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm
)),
9374 TYPE_MAIN_VARIANT (TREE_VALUE (type
)))))
9376 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm
))
9377 == TYPE_ATOMIC (TREE_VALUE (type
)))
9378 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm
))
9379 == TYPE_MAIN_VARIANT (TREE_VALUE (type
))))
9381 /* Adjust argument to match prototype. E.g. a previous
9382 `int foo(float);' prototype causes
9383 `int foo(x) float x; {...}' to be treated like
9384 `int foo(float x) {...}'. This is particularly
9385 useful for argument types like uid_t. */
9386 DECL_ARG_TYPE (parm
) = TREE_TYPE (parm
);
9388 if (targetm
.calls
.promote_prototypes (TREE_TYPE (current_function_decl
))
9389 && INTEGRAL_TYPE_P (TREE_TYPE (parm
))
9390 && (TYPE_PRECISION (TREE_TYPE (parm
))
9391 < TYPE_PRECISION (integer_type_node
)))
9392 DECL_ARG_TYPE (parm
)
9393 = c_type_promotes_to (TREE_TYPE (parm
));
9395 /* ??? Is it possible to get here with a
9396 built-in prototype or will it always have
9397 been diagnosed as conflicting with an
9398 old-style definition and discarded? */
9399 if (current_function_prototype_built_in
)
9400 warning_at (DECL_SOURCE_LOCATION (parm
),
9401 OPT_Wpedantic
, "promoted argument %qD "
9402 "doesn%'t match built-in prototype", parm
);
9405 pedwarn (DECL_SOURCE_LOCATION (parm
),
9406 OPT_Wpedantic
, "promoted argument %qD "
9407 "doesn%'t match prototype", parm
);
9408 pedwarn (current_function_prototype_locus
, OPT_Wpedantic
,
9409 "prototype declaration");
9414 if (current_function_prototype_built_in
)
9415 warning_at (DECL_SOURCE_LOCATION (parm
),
9416 0, "argument %qD doesn%'t match "
9417 "built-in prototype", parm
);
9420 error_at (DECL_SOURCE_LOCATION (parm
),
9421 "argument %qD doesn%'t match prototype", parm
);
9422 error_at (current_function_prototype_locus
,
9423 "prototype declaration");
9428 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = NULL_TREE
;
9431 /* Otherwise, create a prototype that would match. */
9435 tree actual
= NULL_TREE
, last
= NULL_TREE
, type
;
9437 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= DECL_CHAIN (parm
))
9439 type
= tree_cons (NULL_TREE
, DECL_ARG_TYPE (parm
), NULL_TREE
);
9441 TREE_CHAIN (last
) = type
;
9446 type
= tree_cons (NULL_TREE
, void_type_node
, NULL_TREE
);
9448 TREE_CHAIN (last
) = type
;
9452 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9453 of the type of this function, but we need to avoid having this
9454 affect the types of other similarly-typed functions, so we must
9455 first force the generation of an identical (but separate) type
9456 node for the relevant function type. The new node we create
9457 will be a variant of the main variant of the original function
9460 TREE_TYPE (fndecl
) = build_variant_type_copy (TREE_TYPE (fndecl
));
9462 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = actual
;
9466 /* Store parameter declarations passed in ARG_INFO into the current
9467 function declaration. */
9470 store_parm_decls_from (struct c_arg_info
*arg_info
)
9472 current_function_arg_info
= arg_info
;
9473 store_parm_decls ();
9476 /* Called by walk_tree to look for and update context-less labels. */
9479 set_labels_context_r (tree
*tp
, int *walk_subtrees
, void *data
)
9481 if (TREE_CODE (*tp
) == LABEL_EXPR
9482 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp
)) == NULL_TREE
)
9484 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp
)) = static_cast<tree
>(data
);
9491 /* Store the parameter declarations into the current function declaration.
9492 This is called after parsing the parameter declarations, before
9493 digesting the body of the function.
9495 For an old-style definition, construct a prototype out of the old-style
9496 parameter declarations and inject it into the function's type. */
9499 store_parm_decls (void)
9501 tree fndecl
= current_function_decl
;
9504 /* The argument information block for FNDECL. */
9505 struct c_arg_info
*arg_info
= current_function_arg_info
;
9506 current_function_arg_info
= 0;
9508 /* True if this definition is written with a prototype. Note:
9509 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9510 list in a function definition as equivalent to (void) -- an
9511 empty argument list specifies the function has no parameters,
9512 but only (void) sets up a prototype for future calls. */
9513 proto
= arg_info
->types
!= 0;
9516 store_parm_decls_newstyle (fndecl
, arg_info
);
9518 store_parm_decls_oldstyle (fndecl
, arg_info
);
9520 /* The next call to push_scope will be a function body. */
9522 next_is_function_body
= true;
9524 /* Write a record describing this function definition to the prototypes
9525 file (if requested). */
9527 gen_aux_info_record (fndecl
, 1, 0, proto
);
9529 /* Initialize the RTL code for the function. */
9530 allocate_struct_function (fndecl
, false);
9532 if (warn_unused_local_typedefs
)
9533 cfun
->language
= ggc_cleared_alloc
<language_function
> ();
9535 /* Begin the statement tree for this function. */
9536 DECL_SAVED_TREE (fndecl
) = push_stmt_list ();
9538 /* ??? Insert the contents of the pending sizes list into the function
9539 to be evaluated. The only reason left to have this is
9540 void foo(int n, int array[n++])
9541 because we throw away the array type in favor of a pointer type, and
9542 thus won't naturally see the SAVE_EXPR containing the increment. All
9543 other pending sizes would be handled by gimplify_parameters. */
9544 if (arg_info
->pending_sizes
)
9546 /* In very special circumstances, e.g. for code like
9548 void f (int a[i += 2]) {}
9549 we need to execute the atomic assignment on function entry.
9550 But in this case, it is not just a straight store, it has the
9551 op= form, which means that build_atomic_assign has generated
9552 gotos, labels, etc. Because at that time the function decl
9553 for F has not been created yet, those labels do not have any
9554 function context. But we have the fndecl now, so update the
9555 labels accordingly. gimplify_expr would crash otherwise. */
9556 walk_tree_without_duplicates (&arg_info
->pending_sizes
,
9557 set_labels_context_r
, fndecl
);
9558 add_stmt (arg_info
->pending_sizes
);
9562 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9563 c_finish_omp_declare_simd for function prototypes. No diagnostics
9567 temp_store_parm_decls (tree fndecl
, tree parms
)
9570 for (tree p
= parms
; p
; p
= DECL_CHAIN (p
))
9572 DECL_CONTEXT (p
) = fndecl
;
9574 bind (DECL_NAME (p
), p
, current_scope
,
9575 /*invisible=*/false, /*nested=*/false,
9580 /* Undo what temp_store_parm_decls did. */
9583 temp_pop_parm_decls (void)
9585 /* Clear all bindings in this temporary scope, so that
9586 pop_scope doesn't create a BLOCK. */
9587 struct c_binding
*b
= current_scope
->bindings
;
9588 current_scope
->bindings
= NULL
;
9589 for (; b
; b
= free_binding_and_advance (b
))
9591 gcc_assert (TREE_CODE (b
->decl
) == PARM_DECL
9592 || b
->decl
== error_mark_node
);
9593 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
9594 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
9595 if (b
->shadowed
&& b
->shadowed
->u
.type
)
9596 TREE_TYPE (b
->shadowed
->decl
) = b
->shadowed
->u
.type
;
9602 /* Finish up a function declaration and compile that function
9603 all the way to assembler language output. Then free the storage
9604 for the function definition.
9606 This is called after parsing the body of the function definition. */
9609 finish_function (void)
9611 tree fndecl
= current_function_decl
;
9613 if (c_dialect_objc ())
9614 objc_finish_function ();
9616 if (TREE_CODE (fndecl
) == FUNCTION_DECL
9617 && targetm
.calls
.promote_prototypes (TREE_TYPE (fndecl
)))
9619 tree args
= DECL_ARGUMENTS (fndecl
);
9620 for (; args
; args
= DECL_CHAIN (args
))
9622 tree type
= TREE_TYPE (args
);
9623 if (INTEGRAL_TYPE_P (type
)
9624 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
9625 DECL_ARG_TYPE (args
) = c_type_promotes_to (type
);
9629 if (DECL_INITIAL (fndecl
) && DECL_INITIAL (fndecl
) != error_mark_node
)
9630 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
9632 /* Must mark the RESULT_DECL as being in this function. */
9634 if (DECL_RESULT (fndecl
) && DECL_RESULT (fndecl
) != error_mark_node
)
9635 DECL_CONTEXT (DECL_RESULT (fndecl
)) = fndecl
;
9637 if (MAIN_NAME_P (DECL_NAME (fndecl
)) && flag_hosted
9638 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl
)))
9639 == integer_type_node
&& flag_isoc99
)
9641 /* Hack. We don't want the middle-end to warn that this return
9642 is unreachable, so we mark its location as special. Using
9643 UNKNOWN_LOCATION has the problem that it gets clobbered in
9644 annotate_one_with_locus. A cleaner solution might be to
9645 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9647 c_finish_return (BUILTINS_LOCATION
, integer_zero_node
, NULL_TREE
);
9650 /* Tie off the statement tree for this function. */
9651 DECL_SAVED_TREE (fndecl
) = pop_stmt_list (DECL_SAVED_TREE (fndecl
));
9653 finish_fname_decls ();
9655 /* Complain if there's no return statement only if option specified on
9657 if (warn_return_type
> 0
9658 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl
))) != VOID_TYPE
9659 && !current_function_returns_value
&& !current_function_returns_null
9660 /* Don't complain if we are no-return. */
9661 && !current_function_returns_abnormally
9662 /* Don't complain if we are declared noreturn. */
9663 && !TREE_THIS_VOLATILE (fndecl
)
9664 /* Don't warn for main(). */
9665 && !MAIN_NAME_P (DECL_NAME (fndecl
))
9666 /* Or if they didn't actually specify a return type. */
9667 && !C_FUNCTION_IMPLICIT_INT (fndecl
)
9668 /* Normally, with -Wreturn-type, flow will complain, but we might
9669 optimize out static functions. */
9670 && !TREE_PUBLIC (fndecl
)
9671 && warning (OPT_Wreturn_type
,
9672 "no return statement in function returning non-void"))
9673 TREE_NO_WARNING (fndecl
) = 1;
9675 /* Complain about parameters that are only set, but never otherwise used. */
9676 if (warn_unused_but_set_parameter
)
9680 for (decl
= DECL_ARGUMENTS (fndecl
);
9682 decl
= DECL_CHAIN (decl
))
9683 if (TREE_USED (decl
)
9684 && TREE_CODE (decl
) == PARM_DECL
9685 && !DECL_READ_P (decl
)
9687 && !DECL_ARTIFICIAL (decl
)
9688 && !TREE_NO_WARNING (decl
))
9689 warning_at (DECL_SOURCE_LOCATION (decl
),
9690 OPT_Wunused_but_set_parameter
,
9691 "parameter %qD set but not used", decl
);
9694 /* Complain about locally defined typedefs that are not used in this
9696 maybe_warn_unused_local_typedefs ();
9698 /* Possibly warn about unused parameters. */
9699 if (warn_unused_parameter
)
9700 do_warn_unused_parameter (fndecl
);
9702 /* Store the end of the function, so that we get good line number
9703 info for the epilogue. */
9704 cfun
->function_end_locus
= input_location
;
9706 /* Finalize the ELF visibility for the function. */
9707 c_determine_visibility (fndecl
);
9709 /* For GNU C extern inline functions disregard inline limits. */
9710 if (DECL_EXTERNAL (fndecl
)
9711 && DECL_DECLARED_INLINE_P (fndecl
)
9712 && (flag_gnu89_inline
9713 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl
))))
9714 DECL_DISREGARD_INLINE_LIMITS (fndecl
) = 1;
9716 /* Genericize before inlining. Delay genericizing nested functions
9717 until their parent function is genericized. Since finalizing
9718 requires GENERIC, delay that as well. */
9720 if (DECL_INITIAL (fndecl
) && DECL_INITIAL (fndecl
) != error_mark_node
9721 && !undef_nested_function
)
9723 if (!decl_function_context (fndecl
))
9725 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE
, fndecl
);
9726 c_genericize (fndecl
);
9728 /* ??? Objc emits functions after finalizing the compilation unit.
9729 This should be cleaned up later and this conditional removed. */
9730 if (symtab
->global_info_ready
)
9732 cgraph_node::add_new_function (fndecl
, false);
9735 cgraph_node::finalize_function (fndecl
, false);
9739 /* Register this function with cgraph just far enough to get it
9740 added to our parent's nested function list. Handy, since the
9741 C front end doesn't have such a list. */
9742 (void) cgraph_node::get_create (fndecl
);
9746 if (!decl_function_context (fndecl
))
9747 undef_nested_function
= false;
9749 if (cfun
->language
!= NULL
)
9751 ggc_free (cfun
->language
);
9752 cfun
->language
= NULL
;
9755 /* We're leaving the context of this function, so zap cfun.
9756 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9757 tree_rest_of_compilation. */
9759 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION
, current_function_decl
);
9760 current_function_decl
= NULL
;
9763 /* Check the declarations given in a for-loop for satisfying the C99
9764 constraints. If exactly one such decl is found, return it. LOC is
9765 the location of the opening parenthesis of the for loop. The last
9766 parameter allows you to control the "for loop initial declarations
9767 are only allowed in C99 mode". Normally, you should pass
9768 flag_isoc99 as that parameter. But in some cases (Objective-C
9769 foreach loop, for example) we want to run the checks in this
9770 function even if not in C99 mode, so we allow the caller to turn
9771 off the error about not being in C99 mode.
9775 check_for_loop_decls (location_t loc
, bool turn_off_iso_c99_error
)
9777 struct c_binding
*b
;
9778 tree one_decl
= NULL_TREE
;
9781 if (!turn_off_iso_c99_error
)
9783 static bool hint
= true;
9784 /* If we get here, declarations have been used in a for loop without
9785 the C99 for loop scope. This doesn't make much sense, so don't
9787 error_at (loc
, "%<for%> loop initial declarations "
9788 "are only allowed in C99 or C11 mode");
9792 "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
9793 "%<-std=gnu11%> to compile your code");
9799 pedwarn_c90 (loc
, OPT_Wpedantic
, "ISO C90 does not support %<for%> loop "
9800 "initial declarations");
9802 /* C99 subclause 6.8.5 paragraph 3:
9804 [#3] The declaration part of a for statement shall only
9805 declare identifiers for objects having storage class auto or
9808 It isn't clear whether, in this sentence, "identifiers" binds to
9809 "shall only declare" or to "objects" - that is, whether all identifiers
9810 declared must be identifiers for objects, or whether the restriction
9811 only applies to those that are. (A question on this in comp.std.c
9812 in November 2000 received no answer.) We implement the strictest
9813 interpretation, to avoid creating an extension which later causes
9816 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
9819 tree decl
= b
->decl
;
9824 switch (TREE_CODE (decl
))
9828 location_t decl_loc
= DECL_SOURCE_LOCATION (decl
);
9829 if (TREE_STATIC (decl
))
9831 "declaration of static variable %qD in %<for%> loop "
9832 "initial declaration", decl
);
9833 else if (DECL_EXTERNAL (decl
))
9835 "declaration of %<extern%> variable %qD in %<for%> loop "
9836 "initial declaration", decl
);
9842 "%<struct %E%> declared in %<for%> loop initial "
9847 "%<union %E%> declared in %<for%> loop initial declaration",
9851 error_at (loc
, "%<enum %E%> declared in %<for%> loop "
9852 "initial declaration", id
);
9855 error_at (loc
, "declaration of non-variable "
9856 "%qD in %<for%> loop initial declaration", decl
);
9863 return n_decls
== 1 ? one_decl
: NULL_TREE
;
9866 /* Save and reinitialize the variables
9867 used during compilation of a C function. */
9870 c_push_function_context (void)
9872 struct language_function
*p
= cfun
->language
;
9873 /* cfun->language might have been already allocated by the use of
9874 -Wunused-local-typedefs. In that case, just re-use it. */
9876 cfun
->language
= p
= ggc_cleared_alloc
<language_function
> ();
9878 p
->base
.x_stmt_tree
= c_stmt_tree
;
9879 c_stmt_tree
.x_cur_stmt_list
= vec_safe_copy (c_stmt_tree
.x_cur_stmt_list
);
9880 p
->x_break_label
= c_break_label
;
9881 p
->x_cont_label
= c_cont_label
;
9882 p
->x_switch_stack
= c_switch_stack
;
9883 p
->arg_info
= current_function_arg_info
;
9884 p
->returns_value
= current_function_returns_value
;
9885 p
->returns_null
= current_function_returns_null
;
9886 p
->returns_abnormally
= current_function_returns_abnormally
;
9887 p
->warn_about_return_type
= warn_about_return_type
;
9889 push_function_context ();
9892 /* Restore the variables used during compilation of a C function. */
9895 c_pop_function_context (void)
9897 struct language_function
*p
;
9899 pop_function_context ();
9902 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9903 used to store data throughout the life time of the current cfun,
9904 So don't deallocate it. */
9905 if (!warn_unused_local_typedefs
)
9906 cfun
->language
= NULL
;
9908 if (DECL_STRUCT_FUNCTION (current_function_decl
) == 0
9909 && DECL_SAVED_TREE (current_function_decl
) == NULL_TREE
)
9911 /* Stop pointing to the local nodes about to be freed. */
9912 /* But DECL_INITIAL must remain nonzero so we know this
9913 was an actual function definition. */
9914 DECL_INITIAL (current_function_decl
) = error_mark_node
;
9915 DECL_ARGUMENTS (current_function_decl
) = NULL_TREE
;
9918 c_stmt_tree
= p
->base
.x_stmt_tree
;
9919 p
->base
.x_stmt_tree
.x_cur_stmt_list
= NULL
;
9920 c_break_label
= p
->x_break_label
;
9921 c_cont_label
= p
->x_cont_label
;
9922 c_switch_stack
= p
->x_switch_stack
;
9923 current_function_arg_info
= p
->arg_info
;
9924 current_function_returns_value
= p
->returns_value
;
9925 current_function_returns_null
= p
->returns_null
;
9926 current_function_returns_abnormally
= p
->returns_abnormally
;
9927 warn_about_return_type
= p
->warn_about_return_type
;
9930 /* The functions below are required for functionality of doing
9931 function at once processing in the C front end. Currently these
9932 functions are not called from anywhere in the C front end, but as
9933 these changes continue, that will change. */
9935 /* Returns the stmt_tree (if any) to which statements are currently
9936 being added. If there is no active statement-tree, NULL is
9940 current_stmt_tree (void)
9942 return &c_stmt_tree
;
9945 /* Return the global value of T as a symbol. */
9948 identifier_global_value (tree t
)
9950 struct c_binding
*b
;
9952 for (b
= I_SYMBOL_BINDING (t
); b
; b
= b
->shadowed
)
9953 if (B_IN_FILE_SCOPE (b
) || B_IN_EXTERNAL_SCOPE (b
))
9959 /* In C, the only C-linkage public declaration is at file scope. */
9962 c_linkage_bindings (tree name
)
9964 return identifier_global_value (name
);
9967 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9968 otherwise the name is found in ridpointers from RID_INDEX. */
9971 record_builtin_type (enum rid rid_index
, const char *name
, tree type
)
9975 id
= ridpointers
[(int) rid_index
];
9977 id
= get_identifier (name
);
9978 decl
= build_decl (UNKNOWN_LOCATION
, TYPE_DECL
, id
, type
);
9980 if (debug_hooks
->type_decl
)
9981 debug_hooks
->type_decl (decl
, false);
9984 /* Build the void_list_node (void_type_node having been created). */
9986 build_void_list_node (void)
9988 tree t
= build_tree_list (NULL_TREE
, void_type_node
);
9992 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9995 build_c_parm (struct c_declspecs
*specs
, tree attrs
,
9996 struct c_declarator
*declarator
,
9999 struct c_parm
*ret
= XOBNEW (&parser_obstack
, struct c_parm
);
10000 ret
->specs
= specs
;
10001 ret
->attrs
= attrs
;
10002 ret
->declarator
= declarator
;
10007 /* Return a declarator with nested attributes. TARGET is the inner
10008 declarator to which these attributes apply. ATTRS are the
10011 struct c_declarator
*
10012 build_attrs_declarator (tree attrs
, struct c_declarator
*target
)
10014 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
10015 ret
->kind
= cdk_attrs
;
10016 ret
->declarator
= target
;
10017 ret
->u
.attrs
= attrs
;
10021 /* Return a declarator for a function with arguments specified by ARGS
10022 and return type specified by TARGET. */
10024 struct c_declarator
*
10025 build_function_declarator (struct c_arg_info
*args
,
10026 struct c_declarator
*target
)
10028 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
10029 ret
->kind
= cdk_function
;
10030 ret
->declarator
= target
;
10031 ret
->u
.arg_info
= args
;
10035 /* Return a declarator for the identifier IDENT (which may be
10036 NULL_TREE for an abstract declarator). */
10038 struct c_declarator
*
10039 build_id_declarator (tree ident
)
10041 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
10042 ret
->kind
= cdk_id
;
10043 ret
->declarator
= 0;
10045 /* Default value - may get reset to a more precise location. */
10046 ret
->id_loc
= input_location
;
10050 /* Return something to represent absolute declarators containing a *.
10051 TARGET is the absolute declarator that the * contains.
10052 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
10053 to apply to the pointer type. */
10055 struct c_declarator
*
10056 make_pointer_declarator (struct c_declspecs
*type_quals_attrs
,
10057 struct c_declarator
*target
)
10061 struct c_declarator
*itarget
= target
;
10062 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
10063 if (type_quals_attrs
)
10065 attrs
= type_quals_attrs
->attrs
;
10066 quals
= quals_from_declspecs (type_quals_attrs
);
10067 if (attrs
!= NULL_TREE
)
10068 itarget
= build_attrs_declarator (attrs
, target
);
10070 ret
->kind
= cdk_pointer
;
10071 ret
->declarator
= itarget
;
10072 ret
->u
.pointer_quals
= quals
;
10076 /* Return a pointer to a structure for an empty list of declaration
10079 struct c_declspecs
*
10080 build_null_declspecs (void)
10082 struct c_declspecs
*ret
= XOBNEW (&parser_obstack
, struct c_declspecs
);
10083 memset (ret
, 0, sizeof *ret
);
10084 ret
->align_log
= -1;
10085 ret
->typespec_word
= cts_none
;
10086 ret
->storage_class
= csc_none
;
10087 ret
->expr_const_operands
= true;
10088 ret
->typespec_kind
= ctsk_none
;
10089 ret
->address_space
= ADDR_SPACE_GENERIC
;
10093 /* Add the address space ADDRSPACE to the declaration specifiers
10094 SPECS, returning SPECS. */
10096 struct c_declspecs
*
10097 declspecs_add_addrspace (location_t location
,
10098 struct c_declspecs
*specs
, addr_space_t as
)
10100 specs
->non_sc_seen_p
= true;
10101 specs
->declspecs_seen_p
= true;
10103 if (!ADDR_SPACE_GENERIC_P (specs
->address_space
)
10104 && specs
->address_space
!= as
)
10105 error ("incompatible address space qualifiers %qs and %qs",
10106 c_addr_space_name (as
),
10107 c_addr_space_name (specs
->address_space
));
10110 specs
->address_space
= as
;
10111 specs
->locations
[cdw_address_space
] = location
;
10116 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
10117 returning SPECS. */
10119 struct c_declspecs
*
10120 declspecs_add_qual (location_t loc
,
10121 struct c_declspecs
*specs
, tree qual
)
10125 specs
->non_sc_seen_p
= true;
10126 specs
->declspecs_seen_p
= true;
10127 gcc_assert (TREE_CODE (qual
) == IDENTIFIER_NODE
10128 && C_IS_RESERVED_WORD (qual
));
10129 i
= C_RID_CODE (qual
);
10130 location_t prev_loc
= UNKNOWN_LOCATION
;
10134 dupe
= specs
->const_p
;
10135 specs
->const_p
= true;
10136 prev_loc
= specs
->locations
[cdw_const
];
10137 specs
->locations
[cdw_const
] = loc
;
10140 dupe
= specs
->volatile_p
;
10141 specs
->volatile_p
= true;
10142 prev_loc
= specs
->locations
[cdw_volatile
];
10143 specs
->locations
[cdw_volatile
] = loc
;
10146 dupe
= specs
->restrict_p
;
10147 specs
->restrict_p
= true;
10148 prev_loc
= specs
->locations
[cdw_restrict
];
10149 specs
->locations
[cdw_restrict
] = loc
;
10152 dupe
= specs
->atomic_p
;
10153 specs
->atomic_p
= true;
10154 prev_loc
= specs
->locations
[cdw_atomic
];
10155 specs
->locations
[cdw_atomic
] = loc
;
10158 gcc_unreachable ();
10162 bool warned
= pedwarn_c90 (loc
, OPT_Wpedantic
,
10163 "duplicate %qE declaration specifier", qual
);
10165 && warn_duplicate_decl_specifier
10166 && prev_loc
>= RESERVED_LOCATION_COUNT
10167 && !from_macro_expansion_at (prev_loc
)
10168 && !from_macro_expansion_at (loc
))
10169 warning_at (loc
, OPT_Wduplicate_decl_specifier
,
10170 "duplicate %qE declaration specifier", qual
);
10175 /* Add the type specifier TYPE to the declaration specifiers SPECS,
10176 returning SPECS. */
10178 struct c_declspecs
*
10179 declspecs_add_type (location_t loc
, struct c_declspecs
*specs
,
10180 struct c_typespec spec
)
10182 tree type
= spec
.spec
;
10183 specs
->non_sc_seen_p
= true;
10184 specs
->declspecs_seen_p
= true;
10185 specs
->typespec_kind
= spec
.kind
;
10186 if (TREE_DEPRECATED (type
))
10187 specs
->deprecated_p
= true;
10189 /* Handle type specifier keywords. */
10190 if (TREE_CODE (type
) == IDENTIFIER_NODE
10191 && C_IS_RESERVED_WORD (type
)
10192 && C_RID_CODE (type
) != RID_CXX_COMPAT_WARN
)
10194 enum rid i
= C_RID_CODE (type
);
10197 error_at (loc
, "two or more data types in declaration specifiers");
10200 if ((int) i
<= (int) RID_LAST_MODIFIER
)
10202 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
10207 if (specs
->long_long_p
)
10209 error_at (loc
, "%<long long long%> is too long for GCC");
10214 if (specs
->typespec_word
== cts_double
)
10217 ("both %<long long%> and %<double%> in "
10218 "declaration specifiers"));
10221 pedwarn_c90 (loc
, OPT_Wlong_long
,
10222 "ISO C90 does not support %<long long%>");
10223 specs
->long_long_p
= 1;
10224 specs
->locations
[cdw_long_long
] = loc
;
10227 if (specs
->short_p
)
10229 ("both %<long%> and %<short%> in "
10230 "declaration specifiers"));
10231 else if (specs
->typespec_word
== cts_auto_type
)
10233 ("both %<long%> and %<__auto_type%> in "
10234 "declaration specifiers"));
10235 else if (specs
->typespec_word
== cts_void
)
10237 ("both %<long%> and %<void%> in "
10238 "declaration specifiers"));
10239 else if (specs
->typespec_word
== cts_int_n
)
10241 ("both %<long%> and %<__int%d%> in "
10242 "declaration specifiers"),
10243 int_n_data
[specs
->int_n_idx
].bitsize
);
10244 else if (specs
->typespec_word
== cts_bool
)
10246 ("both %<long%> and %<_Bool%> in "
10247 "declaration specifiers"));
10248 else if (specs
->typespec_word
== cts_char
)
10250 ("both %<long%> and %<char%> in "
10251 "declaration specifiers"));
10252 else if (specs
->typespec_word
== cts_float
)
10254 ("both %<long%> and %<float%> in "
10255 "declaration specifiers"));
10256 else if (specs
->typespec_word
== cts_floatn_nx
)
10258 ("both %<long%> and %<_Float%d%s%> in "
10259 "declaration specifiers"),
10260 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10261 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10264 else if (specs
->typespec_word
== cts_dfloat32
)
10266 ("both %<long%> and %<_Decimal32%> in "
10267 "declaration specifiers"));
10268 else if (specs
->typespec_word
== cts_dfloat64
)
10270 ("both %<long%> and %<_Decimal64%> in "
10271 "declaration specifiers"));
10272 else if (specs
->typespec_word
== cts_dfloat128
)
10274 ("both %<long%> and %<_Decimal128%> in "
10275 "declaration specifiers"));
10278 specs
->long_p
= true;
10279 specs
->locations
[cdw_long
] = loc
;
10283 dupe
= specs
->short_p
;
10286 ("both %<long%> and %<short%> in "
10287 "declaration specifiers"));
10288 else if (specs
->typespec_word
== cts_auto_type
)
10290 ("both %<short%> and %<__auto_type%> in "
10291 "declaration specifiers"));
10292 else if (specs
->typespec_word
== cts_void
)
10294 ("both %<short%> and %<void%> in "
10295 "declaration specifiers"));
10296 else if (specs
->typespec_word
== cts_int_n
)
10298 ("both %<short%> and %<__int%d%> in "
10299 "declaration specifiers"),
10300 int_n_data
[specs
->int_n_idx
].bitsize
);
10301 else if (specs
->typespec_word
== cts_bool
)
10303 ("both %<short%> and %<_Bool%> in "
10304 "declaration specifiers"));
10305 else if (specs
->typespec_word
== cts_char
)
10307 ("both %<short%> and %<char%> in "
10308 "declaration specifiers"));
10309 else if (specs
->typespec_word
== cts_float
)
10311 ("both %<short%> and %<float%> in "
10312 "declaration specifiers"));
10313 else if (specs
->typespec_word
== cts_double
)
10315 ("both %<short%> and %<double%> in "
10316 "declaration specifiers"));
10317 else if (specs
->typespec_word
== cts_floatn_nx
)
10319 ("both %<short%> and %<_Float%d%s%> in "
10320 "declaration specifiers"),
10321 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10322 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10325 else if (specs
->typespec_word
== cts_dfloat32
)
10327 ("both %<short%> and %<_Decimal32%> in "
10328 "declaration specifiers"));
10329 else if (specs
->typespec_word
== cts_dfloat64
)
10331 ("both %<short%> and %<_Decimal64%> in "
10332 "declaration specifiers"));
10333 else if (specs
->typespec_word
== cts_dfloat128
)
10335 ("both %<short%> and %<_Decimal128%> in "
10336 "declaration specifiers"));
10339 specs
->short_p
= true;
10340 specs
->locations
[cdw_short
] = loc
;
10344 dupe
= specs
->signed_p
;
10345 if (specs
->unsigned_p
)
10347 ("both %<signed%> and %<unsigned%> in "
10348 "declaration specifiers"));
10349 else if (specs
->typespec_word
== cts_auto_type
)
10351 ("both %<signed%> and %<__auto_type%> in "
10352 "declaration specifiers"));
10353 else if (specs
->typespec_word
== cts_void
)
10355 ("both %<signed%> and %<void%> in "
10356 "declaration specifiers"));
10357 else if (specs
->typespec_word
== cts_bool
)
10359 ("both %<signed%> and %<_Bool%> in "
10360 "declaration specifiers"));
10361 else if (specs
->typespec_word
== cts_float
)
10363 ("both %<signed%> and %<float%> in "
10364 "declaration specifiers"));
10365 else if (specs
->typespec_word
== cts_double
)
10367 ("both %<signed%> and %<double%> in "
10368 "declaration specifiers"));
10369 else if (specs
->typespec_word
== cts_floatn_nx
)
10371 ("both %<signed%> and %<_Float%d%s%> in "
10372 "declaration specifiers"),
10373 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10374 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10377 else if (specs
->typespec_word
== cts_dfloat32
)
10379 ("both %<signed%> and %<_Decimal32%> in "
10380 "declaration specifiers"));
10381 else if (specs
->typespec_word
== cts_dfloat64
)
10383 ("both %<signed%> and %<_Decimal64%> in "
10384 "declaration specifiers"));
10385 else if (specs
->typespec_word
== cts_dfloat128
)
10387 ("both %<signed%> and %<_Decimal128%> in "
10388 "declaration specifiers"));
10391 specs
->signed_p
= true;
10392 specs
->locations
[cdw_signed
] = loc
;
10396 dupe
= specs
->unsigned_p
;
10397 if (specs
->signed_p
)
10399 ("both %<signed%> and %<unsigned%> in "
10400 "declaration specifiers"));
10401 else if (specs
->typespec_word
== cts_auto_type
)
10403 ("both %<unsigned%> and %<__auto_type%> in "
10404 "declaration specifiers"));
10405 else if (specs
->typespec_word
== cts_void
)
10407 ("both %<unsigned%> and %<void%> in "
10408 "declaration specifiers"));
10409 else if (specs
->typespec_word
== cts_bool
)
10411 ("both %<unsigned%> and %<_Bool%> in "
10412 "declaration specifiers"));
10413 else if (specs
->typespec_word
== cts_float
)
10415 ("both %<unsigned%> and %<float%> in "
10416 "declaration specifiers"));
10417 else if (specs
->typespec_word
== cts_double
)
10419 ("both %<unsigned%> and %<double%> in "
10420 "declaration specifiers"));
10421 else if (specs
->typespec_word
== cts_floatn_nx
)
10423 ("both %<unsigned%> and %<_Float%d%s%> in "
10424 "declaration specifiers"),
10425 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10426 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10429 else if (specs
->typespec_word
== cts_dfloat32
)
10431 ("both %<unsigned%> and %<_Decimal32%> in "
10432 "declaration specifiers"));
10433 else if (specs
->typespec_word
== cts_dfloat64
)
10435 ("both %<unsigned%> and %<_Decimal64%> in "
10436 "declaration specifiers"));
10437 else if (specs
->typespec_word
== cts_dfloat128
)
10439 ("both %<unsigned%> and %<_Decimal128%> in "
10440 "declaration specifiers"));
10443 specs
->unsigned_p
= true;
10444 specs
->locations
[cdw_unsigned
] = loc
;
10448 dupe
= specs
->complex_p
;
10449 if (!in_system_header_at (loc
))
10450 pedwarn_c90 (loc
, OPT_Wpedantic
,
10451 "ISO C90 does not support complex types");
10452 if (specs
->typespec_word
== cts_auto_type
)
10454 ("both %<complex%> and %<__auto_type%> in "
10455 "declaration specifiers"));
10456 else if (specs
->typespec_word
== cts_void
)
10458 ("both %<complex%> and %<void%> in "
10459 "declaration specifiers"));
10460 else if (specs
->typespec_word
== cts_bool
)
10462 ("both %<complex%> and %<_Bool%> in "
10463 "declaration specifiers"));
10464 else if (specs
->typespec_word
== cts_dfloat32
)
10466 ("both %<complex%> and %<_Decimal32%> in "
10467 "declaration specifiers"));
10468 else if (specs
->typespec_word
== cts_dfloat64
)
10470 ("both %<complex%> and %<_Decimal64%> in "
10471 "declaration specifiers"));
10472 else if (specs
->typespec_word
== cts_dfloat128
)
10474 ("both %<complex%> and %<_Decimal128%> in "
10475 "declaration specifiers"));
10476 else if (specs
->typespec_word
== cts_fract
)
10478 ("both %<complex%> and %<_Fract%> in "
10479 "declaration specifiers"));
10480 else if (specs
->typespec_word
== cts_accum
)
10482 ("both %<complex%> and %<_Accum%> in "
10483 "declaration specifiers"));
10484 else if (specs
->saturating_p
)
10486 ("both %<complex%> and %<_Sat%> in "
10487 "declaration specifiers"));
10490 specs
->complex_p
= true;
10491 specs
->locations
[cdw_complex
] = loc
;
10495 dupe
= specs
->saturating_p
;
10496 pedwarn (loc
, OPT_Wpedantic
,
10497 "ISO C does not support saturating types");
10498 if (specs
->typespec_word
== cts_int_n
)
10501 ("both %<_Sat%> and %<__int%d%> in "
10502 "declaration specifiers"),
10503 int_n_data
[specs
->int_n_idx
].bitsize
);
10505 else if (specs
->typespec_word
== cts_auto_type
)
10507 ("both %<_Sat%> and %<__auto_type%> in "
10508 "declaration specifiers"));
10509 else if (specs
->typespec_word
== cts_void
)
10511 ("both %<_Sat%> and %<void%> in "
10512 "declaration specifiers"));
10513 else if (specs
->typespec_word
== cts_bool
)
10515 ("both %<_Sat%> and %<_Bool%> in "
10516 "declaration specifiers"));
10517 else if (specs
->typespec_word
== cts_char
)
10519 ("both %<_Sat%> and %<char%> in "
10520 "declaration specifiers"));
10521 else if (specs
->typespec_word
== cts_int
)
10523 ("both %<_Sat%> and %<int%> in "
10524 "declaration specifiers"));
10525 else if (specs
->typespec_word
== cts_float
)
10527 ("both %<_Sat%> and %<float%> in "
10528 "declaration specifiers"));
10529 else if (specs
->typespec_word
== cts_double
)
10531 ("both %<_Sat%> and %<double%> in "
10532 "declaration specifiers"));
10533 else if (specs
->typespec_word
== cts_floatn_nx
)
10535 ("both %<_Sat%> and %<_Float%d%s%> in "
10536 "declaration specifiers"),
10537 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10538 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10541 else if (specs
->typespec_word
== cts_dfloat32
)
10543 ("both %<_Sat%> and %<_Decimal32%> in "
10544 "declaration specifiers"));
10545 else if (specs
->typespec_word
== cts_dfloat64
)
10547 ("both %<_Sat%> and %<_Decimal64%> in "
10548 "declaration specifiers"));
10549 else if (specs
->typespec_word
== cts_dfloat128
)
10551 ("both %<_Sat%> and %<_Decimal128%> in "
10552 "declaration specifiers"));
10553 else if (specs
->complex_p
)
10555 ("both %<_Sat%> and %<complex%> in "
10556 "declaration specifiers"));
10559 specs
->saturating_p
= true;
10560 specs
->locations
[cdw_saturating
] = loc
;
10564 gcc_unreachable ();
10568 error_at (loc
, "duplicate %qE", type
);
10574 /* "void", "_Bool", "char", "int", "float", "double",
10575 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
10576 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10578 if (specs
->typespec_word
!= cts_none
)
10581 "two or more data types in declaration specifiers");
10586 case RID_AUTO_TYPE
:
10589 ("both %<long%> and %<__auto_type%> in "
10590 "declaration specifiers"));
10591 else if (specs
->short_p
)
10593 ("both %<short%> and %<__auto_type%> in "
10594 "declaration specifiers"));
10595 else if (specs
->signed_p
)
10597 ("both %<signed%> and %<__auto_type%> in "
10598 "declaration specifiers"));
10599 else if (specs
->unsigned_p
)
10601 ("both %<unsigned%> and %<__auto_type%> in "
10602 "declaration specifiers"));
10603 else if (specs
->complex_p
)
10605 ("both %<complex%> and %<__auto_type%> in "
10606 "declaration specifiers"));
10607 else if (specs
->saturating_p
)
10609 ("both %<_Sat%> and %<__auto_type%> in "
10610 "declaration specifiers"));
10613 specs
->typespec_word
= cts_auto_type
;
10614 specs
->locations
[cdw_typespec
] = loc
;
10621 specs
->int_n_idx
= i
- RID_INT_N_0
;
10622 if (!in_system_header_at (input_location
))
10623 pedwarn (loc
, OPT_Wpedantic
,
10624 "ISO C does not support %<__int%d%> types",
10625 int_n_data
[specs
->int_n_idx
].bitsize
);
10629 ("both %<__int%d%> and %<long%> in "
10630 "declaration specifiers"),
10631 int_n_data
[specs
->int_n_idx
].bitsize
);
10632 else if (specs
->saturating_p
)
10634 ("both %<_Sat%> and %<__int%d%> in "
10635 "declaration specifiers"),
10636 int_n_data
[specs
->int_n_idx
].bitsize
);
10637 else if (specs
->short_p
)
10639 ("both %<__int%d%> and %<short%> in "
10640 "declaration specifiers"),
10641 int_n_data
[specs
->int_n_idx
].bitsize
);
10642 else if (! int_n_enabled_p
[specs
->int_n_idx
])
10644 specs
->typespec_word
= cts_int_n
;
10646 "%<__int%d%> is not supported on this target",
10647 int_n_data
[specs
->int_n_idx
].bitsize
);
10651 specs
->typespec_word
= cts_int_n
;
10652 specs
->locations
[cdw_typespec
] = loc
;
10658 ("both %<long%> and %<void%> in "
10659 "declaration specifiers"));
10660 else if (specs
->short_p
)
10662 ("both %<short%> and %<void%> in "
10663 "declaration specifiers"));
10664 else if (specs
->signed_p
)
10666 ("both %<signed%> and %<void%> in "
10667 "declaration specifiers"));
10668 else if (specs
->unsigned_p
)
10670 ("both %<unsigned%> and %<void%> in "
10671 "declaration specifiers"));
10672 else if (specs
->complex_p
)
10674 ("both %<complex%> and %<void%> in "
10675 "declaration specifiers"));
10676 else if (specs
->saturating_p
)
10678 ("both %<_Sat%> and %<void%> in "
10679 "declaration specifiers"));
10682 specs
->typespec_word
= cts_void
;
10683 specs
->locations
[cdw_typespec
] = loc
;
10687 if (!in_system_header_at (loc
))
10688 pedwarn_c90 (loc
, OPT_Wpedantic
,
10689 "ISO C90 does not support boolean types");
10692 ("both %<long%> and %<_Bool%> in "
10693 "declaration specifiers"));
10694 else if (specs
->short_p
)
10696 ("both %<short%> and %<_Bool%> in "
10697 "declaration specifiers"));
10698 else if (specs
->signed_p
)
10700 ("both %<signed%> and %<_Bool%> in "
10701 "declaration specifiers"));
10702 else if (specs
->unsigned_p
)
10704 ("both %<unsigned%> and %<_Bool%> in "
10705 "declaration specifiers"));
10706 else if (specs
->complex_p
)
10708 ("both %<complex%> and %<_Bool%> in "
10709 "declaration specifiers"));
10710 else if (specs
->saturating_p
)
10712 ("both %<_Sat%> and %<_Bool%> in "
10713 "declaration specifiers"));
10716 specs
->typespec_word
= cts_bool
;
10717 specs
->locations
[cdw_typespec
] = loc
;
10723 ("both %<long%> and %<char%> in "
10724 "declaration specifiers"));
10725 else if (specs
->short_p
)
10727 ("both %<short%> and %<char%> in "
10728 "declaration specifiers"));
10729 else if (specs
->saturating_p
)
10731 ("both %<_Sat%> and %<char%> in "
10732 "declaration specifiers"));
10735 specs
->typespec_word
= cts_char
;
10736 specs
->locations
[cdw_typespec
] = loc
;
10740 if (specs
->saturating_p
)
10742 ("both %<_Sat%> and %<int%> in "
10743 "declaration specifiers"));
10746 specs
->typespec_word
= cts_int
;
10747 specs
->locations
[cdw_typespec
] = loc
;
10753 ("both %<long%> and %<float%> in "
10754 "declaration specifiers"));
10755 else if (specs
->short_p
)
10757 ("both %<short%> and %<float%> in "
10758 "declaration specifiers"));
10759 else if (specs
->signed_p
)
10761 ("both %<signed%> and %<float%> in "
10762 "declaration specifiers"));
10763 else if (specs
->unsigned_p
)
10765 ("both %<unsigned%> and %<float%> in "
10766 "declaration specifiers"));
10767 else if (specs
->saturating_p
)
10769 ("both %<_Sat%> and %<float%> in "
10770 "declaration specifiers"));
10773 specs
->typespec_word
= cts_float
;
10774 specs
->locations
[cdw_typespec
] = loc
;
10778 if (specs
->long_long_p
)
10780 ("both %<long long%> and %<double%> in "
10781 "declaration specifiers"));
10782 else if (specs
->short_p
)
10784 ("both %<short%> and %<double%> in "
10785 "declaration specifiers"));
10786 else if (specs
->signed_p
)
10788 ("both %<signed%> and %<double%> in "
10789 "declaration specifiers"));
10790 else if (specs
->unsigned_p
)
10792 ("both %<unsigned%> and %<double%> in "
10793 "declaration specifiers"));
10794 else if (specs
->saturating_p
)
10796 ("both %<_Sat%> and %<double%> in "
10797 "declaration specifiers"));
10800 specs
->typespec_word
= cts_double
;
10801 specs
->locations
[cdw_typespec
] = loc
;
10804 CASE_RID_FLOATN_NX
:
10805 specs
->floatn_nx_idx
= i
- RID_FLOATN_NX_FIRST
;
10806 if (!in_system_header_at (input_location
))
10807 pedwarn (loc
, OPT_Wpedantic
,
10808 "ISO C does not support the %<_Float%d%s%> type",
10809 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10810 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10816 ("both %<long%> and %<_Float%d%s%> in "
10817 "declaration specifiers"),
10818 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10819 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10822 else if (specs
->short_p
)
10824 ("both %<short%> and %<_Float%d%s%> in "
10825 "declaration specifiers"),
10826 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10827 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10830 else if (specs
->signed_p
)
10832 ("both %<signed%> and %<_Float%d%s%> in "
10833 "declaration specifiers"),
10834 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10835 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10838 else if (specs
->unsigned_p
)
10840 ("both %<unsigned%> and %<_Float%d%s%> in "
10841 "declaration specifiers"),
10842 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10843 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10846 else if (specs
->saturating_p
)
10848 ("both %<_Sat%> and %<_Float%d%s%> in "
10849 "declaration specifiers"),
10850 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10851 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10854 else if (FLOATN_NX_TYPE_NODE (specs
->floatn_nx_idx
) == NULL_TREE
)
10856 specs
->typespec_word
= cts_floatn_nx
;
10858 "%<_Float%d%s%> is not supported on this target",
10859 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10860 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10866 specs
->typespec_word
= cts_floatn_nx
;
10867 specs
->locations
[cdw_typespec
] = loc
;
10872 case RID_DFLOAT128
:
10875 if (i
== RID_DFLOAT32
)
10876 str
= "_Decimal32";
10877 else if (i
== RID_DFLOAT64
)
10878 str
= "_Decimal64";
10880 str
= "_Decimal128";
10881 if (specs
->long_long_p
)
10883 ("both %<long long%> and %qs in "
10884 "declaration specifiers"),
10888 ("both %<long%> and %qs in "
10889 "declaration specifiers"),
10891 else if (specs
->short_p
)
10893 ("both %<short%> and %qs in "
10894 "declaration specifiers"),
10896 else if (specs
->signed_p
)
10898 ("both %<signed%> and %qs in "
10899 "declaration specifiers"),
10901 else if (specs
->unsigned_p
)
10903 ("both %<unsigned%> and %qs in "
10904 "declaration specifiers"),
10906 else if (specs
->complex_p
)
10908 ("both %<complex%> and %qs in "
10909 "declaration specifiers"),
10911 else if (specs
->saturating_p
)
10913 ("both %<_Sat%> and %qs in "
10914 "declaration specifiers"),
10916 else if (i
== RID_DFLOAT32
)
10917 specs
->typespec_word
= cts_dfloat32
;
10918 else if (i
== RID_DFLOAT64
)
10919 specs
->typespec_word
= cts_dfloat64
;
10921 specs
->typespec_word
= cts_dfloat128
;
10922 specs
->locations
[cdw_typespec
] = loc
;
10924 if (!targetm
.decimal_float_supported_p ())
10926 ("decimal floating point not supported "
10927 "for this target"));
10928 pedwarn (loc
, OPT_Wpedantic
,
10929 "ISO C does not support decimal floating point");
10935 if (i
== RID_FRACT
)
10939 if (specs
->complex_p
)
10941 ("both %<complex%> and %qs in "
10942 "declaration specifiers"),
10944 else if (i
== RID_FRACT
)
10945 specs
->typespec_word
= cts_fract
;
10947 specs
->typespec_word
= cts_accum
;
10948 specs
->locations
[cdw_typespec
] = loc
;
10950 if (!targetm
.fixed_point_supported_p ())
10952 "fixed-point types not supported for this target");
10953 pedwarn (loc
, OPT_Wpedantic
,
10954 "ISO C does not support fixed-point types");
10957 /* ObjC reserved word "id", handled below. */
10963 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10964 form of ObjC type, cases such as "int" and "long" being handled
10965 above), a TYPE (struct, union, enum and typeof specifiers) or an
10966 ERROR_MARK. In none of these cases may there have previously
10967 been any type specifiers. */
10968 if (specs
->type
|| specs
->typespec_word
!= cts_none
10969 || specs
->long_p
|| specs
->short_p
|| specs
->signed_p
10970 || specs
->unsigned_p
|| specs
->complex_p
)
10971 error_at (loc
, "two or more data types in declaration specifiers");
10972 else if (TREE_CODE (type
) == TYPE_DECL
)
10974 if (TREE_TYPE (type
) == error_mark_node
)
10975 ; /* Allow the type to default to int to avoid cascading errors. */
10978 specs
->type
= TREE_TYPE (type
);
10979 specs
->decl_attr
= DECL_ATTRIBUTES (type
);
10980 specs
->typedef_p
= true;
10981 specs
->explicit_signed_p
= C_TYPEDEF_EXPLICITLY_SIGNED (type
);
10982 specs
->locations
[cdw_typedef
] = loc
;
10984 /* If this typedef name is defined in a struct, then a C++
10985 lookup would return a different value. */
10986 if (warn_cxx_compat
10987 && I_SYMBOL_BINDING (DECL_NAME (type
))->in_struct
)
10988 warning_at (loc
, OPT_Wc___compat
,
10989 "C++ lookup of %qD would return a field, not a type",
10992 /* If we are parsing a struct, record that a struct field
10994 if (warn_cxx_compat
&& struct_parse_info
!= NULL
)
10995 struct_parse_info
->typedefs_seen
.safe_push (type
);
10998 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
11000 tree t
= lookup_name (type
);
11001 if (!t
|| TREE_CODE (t
) != TYPE_DECL
)
11002 error_at (loc
, "%qE fails to be a typedef or built in type", type
);
11003 else if (TREE_TYPE (t
) == error_mark_node
)
11007 specs
->type
= TREE_TYPE (t
);
11008 specs
->locations
[cdw_typespec
] = loc
;
11013 if (TREE_CODE (type
) != ERROR_MARK
&& spec
.kind
== ctsk_typeof
)
11015 specs
->typedef_p
= true;
11016 specs
->locations
[cdw_typedef
] = loc
;
11020 specs
->expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (spec
.expr
),
11021 specs
->expr
, spec
.expr
);
11023 specs
->expr
= spec
.expr
;
11024 specs
->expr_const_operands
&= spec
.expr_const_operands
;
11027 specs
->type
= type
;
11033 /* Add the storage class specifier or function specifier SCSPEC to the
11034 declaration specifiers SPECS, returning SPECS. */
11036 struct c_declspecs
*
11037 declspecs_add_scspec (location_t loc
,
11038 struct c_declspecs
*specs
,
11042 enum c_storage_class n
= csc_none
;
11044 specs
->declspecs_seen_p
= true;
11045 gcc_assert (TREE_CODE (scspec
) == IDENTIFIER_NODE
11046 && C_IS_RESERVED_WORD (scspec
));
11047 i
= C_RID_CODE (scspec
);
11048 if (specs
->non_sc_seen_p
)
11049 warning (OPT_Wold_style_declaration
,
11050 "%qE is not at beginning of declaration", scspec
);
11054 /* C99 permits duplicate inline. Although of doubtful utility,
11055 it seems simplest to permit it in gnu89 mode as well, as
11056 there is also little utility in maintaining this as a
11057 difference between gnu89 and C99 inline. */
11059 specs
->inline_p
= true;
11060 specs
->locations
[cdw_inline
] = loc
;
11063 /* Duplicate _Noreturn is permitted. */
11065 specs
->noreturn_p
= true;
11066 specs
->locations
[cdw_noreturn
] = loc
;
11069 dupe
= specs
->thread_p
;
11070 if (specs
->storage_class
== csc_auto
)
11071 error ("%qE used with %<auto%>", scspec
);
11072 else if (specs
->storage_class
== csc_register
)
11073 error ("%qE used with %<register%>", scspec
);
11074 else if (specs
->storage_class
== csc_typedef
)
11075 error ("%qE used with %<typedef%>", scspec
);
11078 specs
->thread_p
= true;
11079 specs
->thread_gnu_p
= (strcmp (IDENTIFIER_POINTER (scspec
),
11081 /* A diagnostic is not required for the use of this
11082 identifier in the implementation namespace; only diagnose
11083 it for the C11 spelling because of existing code using
11084 the other spelling. */
11085 if (!specs
->thread_gnu_p
)
11088 pedwarn_c99 (loc
, OPT_Wpedantic
,
11089 "ISO C99 does not support %qE", scspec
);
11091 pedwarn_c99 (loc
, OPT_Wpedantic
,
11092 "ISO C90 does not support %qE", scspec
);
11094 specs
->locations
[cdw_thread
] = loc
;
11102 /* Diagnose "__thread extern". */
11103 if (specs
->thread_p
&& specs
->thread_gnu_p
)
11104 error ("%<__thread%> before %<extern%>");
11111 /* Diagnose "__thread static". */
11112 if (specs
->thread_p
&& specs
->thread_gnu_p
)
11113 error ("%<__thread%> before %<static%>");
11119 gcc_unreachable ();
11121 if (n
!= csc_none
&& n
== specs
->storage_class
)
11125 if (i
== RID_THREAD
)
11126 error ("duplicate %<_Thread_local%> or %<__thread%>");
11128 error ("duplicate %qE", scspec
);
11132 if (specs
->storage_class
!= csc_none
&& n
!= specs
->storage_class
)
11134 error ("multiple storage classes in declaration specifiers");
11138 specs
->storage_class
= n
;
11139 specs
->locations
[cdw_storage_class
] = loc
;
11140 if (n
!= csc_extern
&& n
!= csc_static
&& specs
->thread_p
)
11142 error ("%qs used with %qE",
11143 specs
->thread_gnu_p
? "__thread" : "_Thread_local",
11145 specs
->thread_p
= false;
11152 /* Add the attributes ATTRS to the declaration specifiers SPECS,
11153 returning SPECS. */
11155 struct c_declspecs
*
11156 declspecs_add_attrs (location_t loc
, struct c_declspecs
*specs
, tree attrs
)
11158 specs
->attrs
= chainon (attrs
, specs
->attrs
);
11159 specs
->locations
[cdw_attributes
] = loc
;
11160 specs
->declspecs_seen_p
= true;
11164 /* Add an _Alignas specifier (expression ALIGN, or type whose
11165 alignment is ALIGN) to the declaration specifiers SPECS, returning
11167 struct c_declspecs
*
11168 declspecs_add_alignas (location_t loc
,
11169 struct c_declspecs
*specs
, tree align
)
11171 specs
->alignas_p
= true;
11172 specs
->locations
[cdw_alignas
] = loc
;
11173 if (align
== error_mark_node
)
11176 /* Only accept the alignment if it's valid and greater than
11177 the current one. Zero is invalid but by C11 required to
11178 be silently ignored. */
11179 int align_log
= check_user_alignment (align
, false, /* warn_zero = */false);
11180 if (align_log
> specs
->align_log
)
11181 specs
->align_log
= align_log
;
11185 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
11186 specifiers with any other type specifier to determine the resulting
11187 type. This is where ISO C checks on complex types are made, since
11188 "_Complex long" is a prefix of the valid ISO C type "_Complex long
11191 struct c_declspecs
*
11192 finish_declspecs (struct c_declspecs
*specs
)
11194 /* If a type was specified as a whole, we have no modifiers and are
11196 if (specs
->type
!= NULL_TREE
)
11198 gcc_assert (!specs
->long_p
&& !specs
->long_long_p
&& !specs
->short_p
11199 && !specs
->signed_p
&& !specs
->unsigned_p
11200 && !specs
->complex_p
);
11202 /* Set a dummy type. */
11203 if (TREE_CODE (specs
->type
) == ERROR_MARK
)
11204 specs
->type
= integer_type_node
;
11208 /* If none of "void", "_Bool", "char", "int", "float" or "double"
11209 has been specified, treat it as "int" unless "_Complex" is
11210 present and there are no other specifiers. If we just have
11211 "_Complex", it is equivalent to "_Complex double", but e.g.
11212 "_Complex short" is equivalent to "_Complex short int". */
11213 if (specs
->typespec_word
== cts_none
)
11215 if (specs
->saturating_p
)
11217 error_at (specs
->locations
[cdw_saturating
],
11218 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
11219 if (!targetm
.fixed_point_supported_p ())
11220 error_at (specs
->locations
[cdw_saturating
],
11221 "fixed-point types not supported for this target");
11222 specs
->typespec_word
= cts_fract
;
11224 else if (specs
->long_p
|| specs
->short_p
11225 || specs
->signed_p
|| specs
->unsigned_p
)
11227 specs
->typespec_word
= cts_int
;
11229 else if (specs
->complex_p
)
11231 specs
->typespec_word
= cts_double
;
11232 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
11233 "ISO C does not support plain %<complex%> meaning "
11234 "%<double complex%>");
11238 specs
->typespec_word
= cts_int
;
11239 specs
->default_int_p
= true;
11240 /* We don't diagnose this here because grokdeclarator will
11241 give more specific diagnostics according to whether it is
11242 a function definition. */
11246 /* If "signed" was specified, record this to distinguish "int" and
11247 "signed int" in the case of a bit-field with
11248 -funsigned-bitfields. */
11249 specs
->explicit_signed_p
= specs
->signed_p
;
11251 /* Now compute the actual type. */
11252 switch (specs
->typespec_word
)
11254 case cts_auto_type
:
11255 gcc_assert (!specs
->long_p
&& !specs
->short_p
11256 && !specs
->signed_p
&& !specs
->unsigned_p
11257 && !specs
->complex_p
);
11258 /* Type to be filled in later. */
11261 gcc_assert (!specs
->long_p
&& !specs
->short_p
11262 && !specs
->signed_p
&& !specs
->unsigned_p
11263 && !specs
->complex_p
);
11264 specs
->type
= void_type_node
;
11267 gcc_assert (!specs
->long_p
&& !specs
->short_p
11268 && !specs
->signed_p
&& !specs
->unsigned_p
11269 && !specs
->complex_p
);
11270 specs
->type
= boolean_type_node
;
11273 gcc_assert (!specs
->long_p
&& !specs
->short_p
);
11274 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
11275 if (specs
->signed_p
)
11276 specs
->type
= signed_char_type_node
;
11277 else if (specs
->unsigned_p
)
11278 specs
->type
= unsigned_char_type_node
;
11280 specs
->type
= char_type_node
;
11281 if (specs
->complex_p
)
11283 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
11284 "ISO C does not support complex integer types");
11285 specs
->type
= build_complex_type (specs
->type
);
11289 gcc_assert (!specs
->long_p
&& !specs
->short_p
&& !specs
->long_long_p
);
11290 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
11291 if (! int_n_enabled_p
[specs
->int_n_idx
])
11292 specs
->type
= integer_type_node
;
11294 specs
->type
= (specs
->unsigned_p
11295 ? int_n_trees
[specs
->int_n_idx
].unsigned_type
11296 : int_n_trees
[specs
->int_n_idx
].signed_type
);
11297 if (specs
->complex_p
)
11299 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
11300 "ISO C does not support complex integer types");
11301 specs
->type
= build_complex_type (specs
->type
);
11305 gcc_assert (!(specs
->long_p
&& specs
->short_p
));
11306 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
11307 if (specs
->long_long_p
)
11308 specs
->type
= (specs
->unsigned_p
11309 ? long_long_unsigned_type_node
11310 : long_long_integer_type_node
);
11311 else if (specs
->long_p
)
11312 specs
->type
= (specs
->unsigned_p
11313 ? long_unsigned_type_node
11314 : long_integer_type_node
);
11315 else if (specs
->short_p
)
11316 specs
->type
= (specs
->unsigned_p
11317 ? short_unsigned_type_node
11318 : short_integer_type_node
);
11320 specs
->type
= (specs
->unsigned_p
11321 ? unsigned_type_node
11322 : integer_type_node
);
11323 if (specs
->complex_p
)
11325 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
11326 "ISO C does not support complex integer types");
11327 specs
->type
= build_complex_type (specs
->type
);
11331 gcc_assert (!specs
->long_p
&& !specs
->short_p
11332 && !specs
->signed_p
&& !specs
->unsigned_p
);
11333 specs
->type
= (specs
->complex_p
11334 ? complex_float_type_node
11335 : float_type_node
);
11338 gcc_assert (!specs
->long_long_p
&& !specs
->short_p
11339 && !specs
->signed_p
&& !specs
->unsigned_p
);
11342 specs
->type
= (specs
->complex_p
11343 ? complex_long_double_type_node
11344 : long_double_type_node
);
11348 specs
->type
= (specs
->complex_p
11349 ? complex_double_type_node
11350 : double_type_node
);
11353 case cts_floatn_nx
:
11354 gcc_assert (!specs
->long_p
&& !specs
->short_p
11355 && !specs
->signed_p
&& !specs
->unsigned_p
);
11356 if (FLOATN_NX_TYPE_NODE (specs
->floatn_nx_idx
) == NULL_TREE
)
11357 specs
->type
= integer_type_node
;
11358 else if (specs
->complex_p
)
11359 specs
->type
= COMPLEX_FLOATN_NX_TYPE_NODE (specs
->floatn_nx_idx
);
11361 specs
->type
= FLOATN_NX_TYPE_NODE (specs
->floatn_nx_idx
);
11365 case cts_dfloat128
:
11366 gcc_assert (!specs
->long_p
&& !specs
->long_long_p
&& !specs
->short_p
11367 && !specs
->signed_p
&& !specs
->unsigned_p
&& !specs
->complex_p
);
11368 if (specs
->typespec_word
== cts_dfloat32
)
11369 specs
->type
= dfloat32_type_node
;
11370 else if (specs
->typespec_word
== cts_dfloat64
)
11371 specs
->type
= dfloat64_type_node
;
11373 specs
->type
= dfloat128_type_node
;
11376 gcc_assert (!specs
->complex_p
);
11377 if (!targetm
.fixed_point_supported_p ())
11378 specs
->type
= integer_type_node
;
11379 else if (specs
->saturating_p
)
11381 if (specs
->long_long_p
)
11382 specs
->type
= specs
->unsigned_p
11383 ? sat_unsigned_long_long_fract_type_node
11384 : sat_long_long_fract_type_node
;
11385 else if (specs
->long_p
)
11386 specs
->type
= specs
->unsigned_p
11387 ? sat_unsigned_long_fract_type_node
11388 : sat_long_fract_type_node
;
11389 else if (specs
->short_p
)
11390 specs
->type
= specs
->unsigned_p
11391 ? sat_unsigned_short_fract_type_node
11392 : sat_short_fract_type_node
;
11394 specs
->type
= specs
->unsigned_p
11395 ? sat_unsigned_fract_type_node
11396 : sat_fract_type_node
;
11400 if (specs
->long_long_p
)
11401 specs
->type
= specs
->unsigned_p
11402 ? unsigned_long_long_fract_type_node
11403 : long_long_fract_type_node
;
11404 else if (specs
->long_p
)
11405 specs
->type
= specs
->unsigned_p
11406 ? unsigned_long_fract_type_node
11407 : long_fract_type_node
;
11408 else if (specs
->short_p
)
11409 specs
->type
= specs
->unsigned_p
11410 ? unsigned_short_fract_type_node
11411 : short_fract_type_node
;
11413 specs
->type
= specs
->unsigned_p
11414 ? unsigned_fract_type_node
11419 gcc_assert (!specs
->complex_p
);
11420 if (!targetm
.fixed_point_supported_p ())
11421 specs
->type
= integer_type_node
;
11422 else if (specs
->saturating_p
)
11424 if (specs
->long_long_p
)
11425 specs
->type
= specs
->unsigned_p
11426 ? sat_unsigned_long_long_accum_type_node
11427 : sat_long_long_accum_type_node
;
11428 else if (specs
->long_p
)
11429 specs
->type
= specs
->unsigned_p
11430 ? sat_unsigned_long_accum_type_node
11431 : sat_long_accum_type_node
;
11432 else if (specs
->short_p
)
11433 specs
->type
= specs
->unsigned_p
11434 ? sat_unsigned_short_accum_type_node
11435 : sat_short_accum_type_node
;
11437 specs
->type
= specs
->unsigned_p
11438 ? sat_unsigned_accum_type_node
11439 : sat_accum_type_node
;
11443 if (specs
->long_long_p
)
11444 specs
->type
= specs
->unsigned_p
11445 ? unsigned_long_long_accum_type_node
11446 : long_long_accum_type_node
;
11447 else if (specs
->long_p
)
11448 specs
->type
= specs
->unsigned_p
11449 ? unsigned_long_accum_type_node
11450 : long_accum_type_node
;
11451 else if (specs
->short_p
)
11452 specs
->type
= specs
->unsigned_p
11453 ? unsigned_short_accum_type_node
11454 : short_accum_type_node
;
11456 specs
->type
= specs
->unsigned_p
11457 ? unsigned_accum_type_node
11462 gcc_unreachable ();
11468 /* Perform final processing on one file scope's declarations (or the
11469 external scope's declarations), GLOBALS. */
11472 c_write_global_declarations_1 (tree globals
)
11477 /* Process the decls in the order they were written. */
11478 for (decl
= globals
; decl
; decl
= DECL_CHAIN (decl
))
11480 /* Check for used but undefined static functions using the C
11481 standard's definition of "used", and set TREE_NO_WARNING so
11482 that check_global_declaration doesn't repeat the check. */
11483 if (TREE_CODE (decl
) == FUNCTION_DECL
11484 && DECL_INITIAL (decl
) == NULL_TREE
11485 && DECL_EXTERNAL (decl
)
11486 && !TREE_PUBLIC (decl
))
11488 if (C_DECL_USED (decl
))
11490 if (pedwarn (input_location
, 0, "%q+F used but never defined",
11492 TREE_NO_WARNING (decl
) = 1;
11494 /* For -Wunused-function warn about unused static prototypes. */
11495 else if (warn_unused_function
11496 && ! DECL_ARTIFICIAL (decl
)
11497 && ! TREE_NO_WARNING (decl
))
11499 if (warning (OPT_Wunused_function
,
11500 "%q+F declared %<static%> but never defined",
11502 TREE_NO_WARNING (decl
) = 1;
11506 wrapup_global_declaration_1 (decl
);
11511 reconsider
= false;
11512 for (decl
= globals
; decl
; decl
= DECL_CHAIN (decl
))
11513 reconsider
|= wrapup_global_declaration_2 (decl
);
11515 while (reconsider
);
11518 /* Callback to collect a source_ref from a DECL. */
11521 collect_source_ref_cb (tree decl
)
11523 if (!DECL_IS_BUILTIN (decl
))
11524 collect_source_ref (LOCATION_FILE (decl_sloc (decl
, false)));
11527 /* Preserve the external declarations scope across a garbage collect. */
11528 static GTY(()) tree ext_block
;
11530 /* Collect all references relevant to SOURCE_FILE. */
11533 collect_all_refs (const char *source_file
)
11538 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
11539 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t
)), source_file
);
11541 collect_ada_nodes (BLOCK_VARS (ext_block
), source_file
);
11544 /* Iterate over all global declarations and call CALLBACK. */
11547 for_each_global_decl (void (*callback
) (tree decl
))
11554 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
11556 decls
= DECL_INITIAL (t
);
11557 for (decl
= BLOCK_VARS (decls
); decl
; decl
= TREE_CHAIN (decl
))
11561 for (decl
= BLOCK_VARS (ext_block
); decl
; decl
= TREE_CHAIN (decl
))
11565 /* Perform any final parser cleanups and generate initial debugging
11569 c_parse_final_cleanups (void)
11574 /* We don't want to do this if generating a PCH. */
11578 timevar_stop (TV_PHASE_PARSING
);
11579 timevar_start (TV_PHASE_DEFERRED
);
11581 /* Do the Objective-C stuff. This is where all the Objective-C
11582 module stuff gets generated (symtab, class/protocol/selector
11584 if (c_dialect_objc ())
11585 objc_write_global_declarations ();
11587 /* Close the external scope. */
11588 ext_block
= pop_scope ();
11589 external_scope
= 0;
11590 gcc_assert (!current_scope
);
11592 /* Handle -fdump-ada-spec[-slim]. */
11593 if (flag_dump_ada_spec
|| flag_dump_ada_spec_slim
)
11595 /* Build a table of files to generate specs for */
11596 if (flag_dump_ada_spec_slim
)
11597 collect_source_ref (main_input_filename
);
11599 for_each_global_decl (collect_source_ref_cb
);
11601 dump_ada_specs (collect_all_refs
, NULL
);
11604 /* Process all file scopes in this compilation, and the external_scope,
11605 through wrapup_global_declarations. */
11606 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
11607 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t
)));
11608 c_write_global_declarations_1 (BLOCK_VARS (ext_block
));
11610 timevar_stop (TV_PHASE_DEFERRED
);
11611 timevar_start (TV_PHASE_PARSING
);
11616 /* Register reserved keyword WORD as qualifier for address space AS. */
11619 c_register_addr_space (const char *word
, addr_space_t as
)
11621 int rid
= RID_FIRST_ADDR_SPACE
+ as
;
11624 /* Address space qualifiers are only supported
11625 in C with GNU extensions enabled. */
11626 if (c_dialect_objc () || flag_no_asm
)
11629 id
= get_identifier (word
);
11630 C_SET_RID_CODE (id
, rid
);
11631 C_IS_RESERVED_WORD (id
) = 1;
11632 ridpointers
[rid
] = id
;
11635 /* Return identifier to look up for omp declare reduction. */
11638 c_omp_reduction_id (enum tree_code reduction_code
, tree reduction_id
)
11640 const char *p
= NULL
;
11641 switch (reduction_code
)
11643 case PLUS_EXPR
: p
= "+"; break;
11644 case MULT_EXPR
: p
= "*"; break;
11645 case MINUS_EXPR
: p
= "-"; break;
11646 case BIT_AND_EXPR
: p
= "&"; break;
11647 case BIT_XOR_EXPR
: p
= "^"; break;
11648 case BIT_IOR_EXPR
: p
= "|"; break;
11649 case TRUTH_ANDIF_EXPR
: p
= "&&"; break;
11650 case TRUTH_ORIF_EXPR
: p
= "||"; break;
11651 case MIN_EXPR
: p
= "min"; break;
11652 case MAX_EXPR
: p
= "max"; break;
11659 if (TREE_CODE (reduction_id
) != IDENTIFIER_NODE
)
11660 return error_mark_node
;
11661 p
= IDENTIFIER_POINTER (reduction_id
);
11664 const char prefix
[] = "omp declare reduction ";
11665 size_t lenp
= sizeof (prefix
);
11666 size_t len
= strlen (p
);
11667 char *name
= XALLOCAVEC (char, lenp
+ len
);
11668 memcpy (name
, prefix
, lenp
- 1);
11669 memcpy (name
+ lenp
- 1, p
, len
+ 1);
11670 return get_identifier (name
);
11673 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11674 VAR_DECL, bind it into the current scope and return it. */
11677 c_omp_reduction_decl (tree reduction_id
)
11679 struct c_binding
*b
= I_SYMBOL_BINDING (reduction_id
);
11680 if (b
!= NULL
&& B_IN_CURRENT_SCOPE (b
))
11683 tree decl
= build_decl (BUILTINS_LOCATION
, VAR_DECL
,
11684 reduction_id
, integer_type_node
);
11685 DECL_ARTIFICIAL (decl
) = 1;
11686 DECL_EXTERNAL (decl
) = 1;
11687 TREE_STATIC (decl
) = 1;
11688 TREE_PUBLIC (decl
) = 0;
11689 bind (reduction_id
, decl
, current_scope
, true, false, BUILTINS_LOCATION
);
11693 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11696 c_omp_reduction_lookup (tree reduction_id
, tree type
)
11698 struct c_binding
*b
= I_SYMBOL_BINDING (reduction_id
);
11702 for (t
= DECL_INITIAL (b
->decl
); t
; t
= TREE_CHAIN (t
))
11703 if (comptypes (TREE_PURPOSE (t
), type
))
11704 return TREE_VALUE (t
);
11707 return error_mark_node
;
11710 /* Helper function called via walk_tree, to diagnose invalid
11711 #pragma omp declare reduction combiners or initializers. */
11714 c_check_omp_declare_reduction_r (tree
*tp
, int *, void *data
)
11716 tree
*vars
= (tree
*) data
;
11717 if (SSA_VAR_P (*tp
)
11718 && !DECL_ARTIFICIAL (*tp
)
11722 location_t loc
= DECL_SOURCE_LOCATION (vars
[0]);
11723 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars
[0])), "omp_out") == 0)
11724 error_at (loc
, "%<#pragma omp declare reduction%> combiner refers to "
11725 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11728 error_at (loc
, "%<#pragma omp declare reduction%> initializer refers "
11729 "to variable %qD which is not %<omp_priv%> nor "
11737 #include "gt-c-c-decl.h"