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 class c_struct_parse_info
580 /* If warn_cxx_compat, a list of types defined within this
582 auto_vec
<tree
> struct_types
;
583 /* If warn_cxx_compat, a list of field names which have bindings,
584 and which are defined in this struct, but which are not defined
585 in any enclosing struct. This is used to clear the in_struct
586 field of the c_bindings structure. */
587 auto_vec
<c_binding_ptr
> fields
;
588 /* If warn_cxx_compat, a list of typedef names used when defining
589 fields in this struct. */
590 auto_vec
<tree
> typedefs_seen
;
593 /* Information for the struct or union currently being parsed, or
594 NULL if not parsing a struct or union. */
595 static class c_struct_parse_info
*struct_parse_info
;
597 /* Forward declarations. */
598 static tree
lookup_name_in_scope (tree
, struct c_scope
*);
599 static tree
c_make_fname_decl (location_t
, tree
, int);
600 static tree
grokdeclarator (const struct c_declarator
*,
601 struct c_declspecs
*,
602 enum decl_context
, bool, tree
*, tree
*, tree
*,
603 bool *, enum deprecated_states
);
604 static tree
grokparms (struct c_arg_info
*, bool);
605 static void layout_array_type (tree
);
606 static void warn_defaults_to (location_t
, int, const char *, ...)
607 ATTRIBUTE_GCC_DIAG(3,4);
608 static const char *header_for_builtin_fn (tree
);
610 /* T is a statement. Add it to the statement-tree. This is the
611 C/ObjC version--C++ has a slightly different version of this
617 enum tree_code code
= TREE_CODE (t
);
619 if (CAN_HAVE_LOCATION_P (t
) && code
!= LABEL_EXPR
)
621 if (!EXPR_HAS_LOCATION (t
))
622 SET_EXPR_LOCATION (t
, input_location
);
625 if (code
== LABEL_EXPR
|| code
== CASE_LABEL_EXPR
)
626 STATEMENT_LIST_HAS_LABEL (cur_stmt_list
) = 1;
628 /* Add T to the statement-tree. Non-side-effect statements need to be
629 recorded during statement expressions. */
630 if (!building_stmt_list_p ())
632 append_to_statement_list_force (t
, &cur_stmt_list
);
637 /* Build a pointer type using the default pointer mode. */
640 c_build_pointer_type (tree to_type
)
642 addr_space_t as
= to_type
== error_mark_node
? ADDR_SPACE_GENERIC
643 : TYPE_ADDR_SPACE (to_type
);
644 machine_mode pointer_mode
;
646 if (as
!= ADDR_SPACE_GENERIC
|| c_default_pointer_mode
== VOIDmode
)
647 pointer_mode
= targetm
.addr_space
.pointer_mode (as
);
649 pointer_mode
= c_default_pointer_mode
;
650 return build_pointer_type_for_mode (to_type
, pointer_mode
, false);
654 /* Return true if we will want to say something if a goto statement
658 decl_jump_unsafe (tree decl
)
660 if (error_operand_p (decl
))
663 /* Don't warn for compound literals. If a goto statement crosses
664 their initialization, it should cross also all the places where
665 the complit is used or where the complit address might be saved
666 into some variable, so code after the label to which goto jumps
667 should not be able to refer to the compound literal. */
668 if (VAR_P (decl
) && C_DECL_COMPOUND_LITERAL_P (decl
))
671 /* Always warn about crossing variably modified types. */
672 if ((VAR_P (decl
) || TREE_CODE (decl
) == TYPE_DECL
)
673 && variably_modified_type_p (TREE_TYPE (decl
), NULL_TREE
))
676 /* Otherwise, only warn if -Wgoto-misses-init and this is an
677 initialized automatic decl. */
678 if (warn_jump_misses_init
680 && !TREE_STATIC (decl
)
681 && DECL_INITIAL (decl
) != NULL_TREE
)
689 c_print_identifier (FILE *file
, tree node
, int indent
)
691 void (*save
) (enum c_oracle_request
, tree identifier
);
693 /* Temporarily hide any binding oracle. Without this, calls to
694 debug_tree from the debugger will end up calling into the oracle,
695 making for a confusing debug session. As the oracle isn't needed
696 here for normal operation, it's simplest to suppress it. */
697 save
= c_binding_oracle
;
698 c_binding_oracle
= NULL
;
700 print_node (file
, "symbol", I_SYMBOL_DECL (node
), indent
+ 4);
701 print_node (file
, "tag", I_TAG_DECL (node
), indent
+ 4);
702 print_node (file
, "label", I_LABEL_DECL (node
), indent
+ 4);
703 if (C_IS_RESERVED_WORD (node
) && C_RID_CODE (node
) != RID_CXX_COMPAT_WARN
)
705 tree rid
= ridpointers
[C_RID_CODE (node
)];
706 indent_to (file
, indent
+ 4);
707 fprintf (file
, "rid " HOST_PTR_PRINTF
" \"%s\"",
708 (void *) rid
, IDENTIFIER_POINTER (rid
));
711 c_binding_oracle
= save
;
714 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
715 which may be any of several kinds of DECL or TYPE or error_mark_node,
716 in the scope SCOPE. */
718 bind (tree name
, tree decl
, struct c_scope
*scope
, bool invisible
,
719 bool nested
, location_t locus
)
721 struct c_binding
*b
, **here
;
723 if (binding_freelist
)
725 b
= binding_freelist
;
726 binding_freelist
= b
->prev
;
729 b
= ggc_alloc
<c_binding
> ();
734 b
->depth
= scope
->depth
;
735 b
->invisible
= invisible
;
743 b
->prev
= scope
->bindings
;
746 if (decl_jump_unsafe (decl
))
747 scope
->has_jump_unsafe_decl
= 1;
752 switch (TREE_CODE (decl
))
754 case LABEL_DECL
: here
= &I_LABEL_BINDING (name
); break;
757 case RECORD_TYPE
: here
= &I_TAG_BINDING (name
); break;
763 case ERROR_MARK
: here
= &I_SYMBOL_BINDING (name
); break;
769 /* Locate the appropriate place in the chain of shadowed decls
770 to insert this binding. Normally, scope == current_scope and
771 this does nothing. */
772 while (*here
&& (*here
)->depth
> scope
->depth
)
773 here
= &(*here
)->shadowed
;
779 /* Clear the binding structure B, stick it on the binding_freelist,
780 and return the former value of b->prev. This is used by pop_scope
781 and get_parm_info to iterate destructively over all the bindings
782 from a given scope. */
783 static struct c_binding
*
784 free_binding_and_advance (struct c_binding
*b
)
786 struct c_binding
*prev
= b
->prev
;
788 memset (b
, 0, sizeof (struct c_binding
));
789 b
->prev
= binding_freelist
;
790 binding_freelist
= b
;
795 /* Bind a label. Like bind, but skip fields which aren't used for
796 labels, and add the LABEL_VARS value. */
798 bind_label (tree name
, tree label
, struct c_scope
*scope
,
799 struct c_label_vars
*label_vars
)
803 bind (name
, label
, scope
, /*invisible=*/false, /*nested=*/false,
806 scope
->has_label_bindings
= true;
809 gcc_assert (b
->decl
== label
);
810 label_vars
->shadowed
= b
->u
.label
;
811 b
->u
.label
= label_vars
;
814 /* Hook called at end of compilation to assume 1 elt
815 for a file-scope tentative array defn that wasn't complete before. */
818 c_finish_incomplete_decl (tree decl
)
822 tree type
= TREE_TYPE (decl
);
823 if (type
!= error_mark_node
824 && TREE_CODE (type
) == ARRAY_TYPE
825 && !DECL_EXTERNAL (decl
)
826 && TYPE_DOMAIN (type
) == NULL_TREE
)
828 warning_at (DECL_SOURCE_LOCATION (decl
),
829 0, "array %q+D assumed to have one element", decl
);
831 complete_array_type (&TREE_TYPE (decl
), NULL_TREE
, true);
833 relayout_decl (decl
);
838 /* Record that inline function FUNC contains a reference (location
839 LOC) to static DECL (file-scope or function-local according to
843 record_inline_static (location_t loc
, tree func
, tree decl
,
844 enum c_inline_static_type type
)
846 c_inline_static
*csi
= ggc_alloc
<c_inline_static
> ();
848 csi
->function
= func
;
849 csi
->static_decl
= decl
;
851 csi
->next
= c_inline_statics
;
852 c_inline_statics
= csi
;
855 /* Check for references to static declarations in inline functions at
856 the end of the translation unit and diagnose them if the functions
857 are still inline definitions. */
860 check_inline_statics (void)
862 struct c_inline_static
*csi
;
863 for (csi
= c_inline_statics
; csi
; csi
= csi
->next
)
865 if (DECL_EXTERNAL (csi
->function
))
869 pedwarn (csi
->location
, 0,
870 "%qD is static but used in inline function %qD "
871 "which is not static", csi
->static_decl
, csi
->function
);
874 pedwarn (csi
->location
, 0,
875 "%q+D is static but declared in inline function %qD "
876 "which is not static", csi
->static_decl
, csi
->function
);
882 c_inline_statics
= NULL
;
885 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
886 for the current state, otherwise set it to uninitialized. */
889 set_spot_bindings (struct c_spot_bindings
*p
, bool defining
)
893 p
->scope
= current_scope
;
894 p
->bindings_in_scope
= current_scope
->bindings
;
899 p
->bindings_in_scope
= NULL
;
902 p
->left_stmt_expr
= false;
905 /* Update spot bindings P as we pop out of SCOPE. Return true if we
906 should push decls for a label. */
909 update_spot_bindings (struct c_scope
*scope
, struct c_spot_bindings
*p
)
911 if (p
->scope
!= scope
)
913 /* This label or goto is defined in some other scope, or it is a
914 label which is not yet defined. There is nothing to
919 /* Adjust the spot bindings to refer to the bindings already defined
920 in the enclosing scope. */
921 p
->scope
= scope
->outer
;
922 p
->bindings_in_scope
= p
->scope
->bindings
;
927 /* The Objective-C front-end often needs to determine the current scope. */
930 objc_get_current_scope (void)
932 return current_scope
;
935 /* The following function is used only by Objective-C. It needs to live here
936 because it accesses the innards of c_scope. */
939 objc_mark_locals_volatile (void *enclosing_blk
)
941 struct c_scope
*scope
;
944 for (scope
= current_scope
;
945 scope
&& scope
!= enclosing_blk
;
946 scope
= scope
->outer
)
948 for (b
= scope
->bindings
; b
; b
= b
->prev
)
949 objc_volatilize_decl (b
->decl
);
951 /* Do not climb up past the current function. */
952 if (scope
->function_body
)
957 /* Return true if we are in the global binding level. */
960 global_bindings_p (void)
962 return current_scope
== file_scope
;
965 /* Return true if we're declaring parameters in an old-style function
969 old_style_parameter_scope (void)
971 /* If processing parameters and there is no function statement list, we
972 * have an old-style function declaration. */
973 return (current_scope
->parm_flag
&& !DECL_SAVED_TREE (current_function_decl
));
977 keep_next_level (void)
979 keep_next_level_flag
= true;
982 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
985 set_float_const_decimal64 (void)
987 current_scope
->float_const_decimal64
= true;
990 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
993 clear_float_const_decimal64 (void)
995 current_scope
->float_const_decimal64
= false;
998 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
1001 float_const_decimal64_p (void)
1003 return current_scope
->float_const_decimal64
;
1006 /* Identify this scope as currently being filled with parameters. */
1009 declare_parm_level (void)
1011 current_scope
->parm_flag
= true;
1017 if (next_is_function_body
)
1019 /* This is the transition from the parameters to the top level
1020 of the function body. These are the same scope
1021 (C99 6.2.1p4,6) so we do not push another scope structure.
1022 next_is_function_body is set only by store_parm_decls, which
1023 in turn is called when and only when we are about to
1024 encounter the opening curly brace for the function body.
1026 The outermost block of a function always gets a BLOCK node,
1027 because the debugging output routines expect that each
1028 function has at least one BLOCK. */
1029 current_scope
->parm_flag
= false;
1030 current_scope
->function_body
= true;
1031 current_scope
->keep
= true;
1032 current_scope
->outer_function
= current_function_scope
;
1033 current_function_scope
= current_scope
;
1035 keep_next_level_flag
= false;
1036 next_is_function_body
= false;
1038 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1039 if (current_scope
->outer
)
1040 current_scope
->float_const_decimal64
1041 = current_scope
->outer
->float_const_decimal64
;
1043 current_scope
->float_const_decimal64
= false;
1047 struct c_scope
*scope
;
1050 scope
= scope_freelist
;
1051 scope_freelist
= scope
->outer
;
1054 scope
= ggc_cleared_alloc
<c_scope
> ();
1056 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1058 scope
->float_const_decimal64
= current_scope
->float_const_decimal64
;
1060 scope
->float_const_decimal64
= false;
1062 scope
->keep
= keep_next_level_flag
;
1063 scope
->outer
= current_scope
;
1064 scope
->depth
= current_scope
? (current_scope
->depth
+ 1) : 0;
1066 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1068 if (current_scope
&& scope
->depth
== 0)
1071 sorry ("GCC supports only %u nested scopes", scope
->depth
);
1074 current_scope
= scope
;
1075 keep_next_level_flag
= false;
1079 /* This is called when we are leaving SCOPE. For each label defined
1080 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1081 These are the decls whose initialization will be skipped by a goto
1082 later in the function. */
1085 update_label_decls (struct c_scope
*scope
)
1092 if (s
->has_label_bindings
)
1094 struct c_binding
*b
;
1096 for (b
= s
->bindings
; b
!= NULL
; b
= b
->prev
)
1098 struct c_label_vars
*label_vars
;
1099 struct c_binding
*b1
;
1102 struct c_goto_bindings
*g
;
1104 if (TREE_CODE (b
->decl
) != LABEL_DECL
)
1106 label_vars
= b
->u
.label
;
1108 b1
= label_vars
->label_bindings
.bindings_in_scope
;
1109 if (label_vars
->label_bindings
.scope
== NULL
)
1112 hjud
= label_vars
->label_bindings
.scope
->has_jump_unsafe_decl
;
1113 if (update_spot_bindings (scope
, &label_vars
->label_bindings
))
1115 /* This label is defined in this scope. */
1118 for (; b1
!= NULL
; b1
= b1
->prev
)
1120 /* A goto from later in the function to this
1121 label will never see the initialization
1122 of B1, if any. Save it to issue a
1123 warning if needed. */
1124 if (decl_jump_unsafe (b1
->decl
))
1125 vec_safe_push(label_vars
->decls_in_scope
, b1
->decl
);
1130 /* Update the bindings of any goto statements associated
1132 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
1133 update_spot_bindings (scope
, &g
->goto_bindings
);
1137 /* Don't search beyond the current function. */
1138 if (s
== current_function_scope
)
1145 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1148 set_type_context (tree type
, tree context
)
1150 for (type
= TYPE_MAIN_VARIANT (type
); type
;
1151 type
= TYPE_NEXT_VARIANT (type
))
1152 TYPE_CONTEXT (type
) = context
;
1155 /* Exit a scope. Restore the state of the identifier-decl mappings
1156 that were in effect when this scope was entered. Return a BLOCK
1157 node containing all the DECLs in this scope that are of interest
1158 to debug info generation. */
1163 struct c_scope
*scope
= current_scope
;
1164 tree block
, context
, p
;
1165 struct c_binding
*b
;
1167 bool functionbody
= scope
->function_body
;
1168 bool keep
= functionbody
|| scope
->keep
|| scope
->bindings
;
1170 update_label_decls (scope
);
1172 /* If appropriate, create a BLOCK to record the decls for the life
1173 of this function. */
1177 block
= make_node (BLOCK
);
1178 BLOCK_SUBBLOCKS (block
) = scope
->blocks
;
1179 TREE_USED (block
) = 1;
1181 /* In each subblock, record that this is its superior. */
1182 for (p
= scope
->blocks
; p
; p
= BLOCK_CHAIN (p
))
1183 BLOCK_SUPERCONTEXT (p
) = block
;
1185 BLOCK_VARS (block
) = NULL_TREE
;
1188 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1189 scope must be set so that they point to the appropriate
1190 construct, i.e. either to the current FUNCTION_DECL node, or
1191 else to the BLOCK node we just constructed.
1193 Note that for tagged types whose scope is just the formal
1194 parameter list for some function type specification, we can't
1195 properly set their TYPE_CONTEXTs here, because we don't have a
1196 pointer to the appropriate FUNCTION_TYPE node readily available
1197 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1198 type nodes get set in `grokdeclarator' as soon as we have created
1199 the FUNCTION_TYPE node which will represent the "scope" for these
1200 "parameter list local" tagged types. */
1201 if (scope
->function_body
)
1202 context
= current_function_decl
;
1203 else if (scope
== file_scope
)
1206 = build_translation_unit_decl (get_identifier (main_input_filename
));
1207 context
= file_decl
;
1208 debug_hooks
->register_main_translation_unit (file_decl
);
1213 /* Clear all bindings in this scope. */
1214 for (b
= scope
->bindings
; b
; b
= free_binding_and_advance (b
))
1217 switch (TREE_CODE (p
))
1220 /* Warnings for unused labels, errors for undefined labels. */
1221 if (TREE_USED (p
) && !DECL_INITIAL (p
))
1223 error ("label %q+D used but not defined", p
);
1224 DECL_INITIAL (p
) = error_mark_node
;
1227 warn_for_unused_label (p
);
1229 /* Labels go in BLOCK_VARS. */
1230 DECL_CHAIN (p
) = BLOCK_VARS (block
);
1231 BLOCK_VARS (block
) = p
;
1232 gcc_assert (I_LABEL_BINDING (b
->id
) == b
);
1233 I_LABEL_BINDING (b
->id
) = b
->shadowed
;
1235 /* Also pop back to the shadowed label_vars. */
1236 release_tree_vector (b
->u
.label
->decls_in_scope
);
1237 b
->u
.label
= b
->u
.label
->shadowed
;
1243 set_type_context (p
, context
);
1245 /* Types may not have tag-names, in which case the type
1246 appears in the bindings list with b->id NULL. */
1249 gcc_assert (I_TAG_BINDING (b
->id
) == b
);
1250 I_TAG_BINDING (b
->id
) = b
->shadowed
;
1255 /* Propagate TREE_ADDRESSABLE from nested functions to their
1256 containing functions. */
1257 if (!TREE_ASM_WRITTEN (p
)
1258 && DECL_INITIAL (p
) != NULL_TREE
1259 && TREE_ADDRESSABLE (p
)
1260 && DECL_ABSTRACT_ORIGIN (p
) != NULL_TREE
1261 && DECL_ABSTRACT_ORIGIN (p
) != p
)
1262 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p
)) = 1;
1263 if (!TREE_PUBLIC (p
)
1264 && !DECL_INITIAL (p
)
1266 && scope
!= file_scope
1267 && scope
!= external_scope
)
1269 error ("nested function %q+D declared but never defined", p
);
1270 undef_nested_function
= true;
1272 else if (DECL_DECLARED_INLINE_P (p
)
1274 && !DECL_INITIAL (p
))
1276 /* C99 6.7.4p6: "a function with external linkage... declared
1277 with an inline function specifier ... shall also be defined
1278 in the same translation unit." */
1279 if (!flag_gnu89_inline
1280 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p
))
1281 && scope
== external_scope
)
1282 pedwarn (input_location
, 0,
1283 "inline function %q+D declared but never defined", p
);
1284 DECL_EXTERNAL (p
) = 1;
1290 /* Warnings for unused variables. */
1291 if ((!TREE_USED (p
) || !DECL_READ_P (p
))
1292 && !TREE_NO_WARNING (p
)
1293 && !DECL_IN_SYSTEM_HEADER (p
)
1295 && !DECL_ARTIFICIAL (p
)
1296 && scope
!= file_scope
1297 && scope
!= external_scope
)
1300 warning (OPT_Wunused_variable
, "unused variable %q+D", p
);
1301 else if (DECL_CONTEXT (p
) == current_function_decl
)
1302 warning_at (DECL_SOURCE_LOCATION (p
),
1303 OPT_Wunused_but_set_variable
,
1304 "variable %qD set but not used", p
);
1309 error ("type of array %q+D completed incompatibly with"
1310 " implicit initialization", p
);
1317 /* All of these go in BLOCK_VARS, but only if this is the
1318 binding in the home scope. */
1321 DECL_CHAIN (p
) = BLOCK_VARS (block
);
1322 BLOCK_VARS (block
) = p
;
1324 else if (VAR_OR_FUNCTION_DECL_P (p
) && scope
!= file_scope
)
1326 /* For block local externs add a special
1327 DECL_EXTERNAL decl for debug info generation. */
1328 tree extp
= copy_node (p
);
1330 DECL_EXTERNAL (extp
) = 1;
1331 TREE_STATIC (extp
) = 0;
1332 TREE_PUBLIC (extp
) = 1;
1333 DECL_INITIAL (extp
) = NULL_TREE
;
1334 DECL_LANG_SPECIFIC (extp
) = NULL
;
1335 DECL_CONTEXT (extp
) = current_function_decl
;
1336 if (TREE_CODE (p
) == FUNCTION_DECL
)
1338 DECL_RESULT (extp
) = NULL_TREE
;
1339 DECL_SAVED_TREE (extp
) = NULL_TREE
;
1340 DECL_STRUCT_FUNCTION (extp
) = NULL
;
1342 if (b
->locus
!= UNKNOWN_LOCATION
)
1343 DECL_SOURCE_LOCATION (extp
) = b
->locus
;
1344 DECL_CHAIN (extp
) = BLOCK_VARS (block
);
1345 BLOCK_VARS (block
) = extp
;
1347 /* If this is the file scope set DECL_CONTEXT of each decl to
1348 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1350 if (scope
== file_scope
)
1352 DECL_CONTEXT (p
) = context
;
1353 if (TREE_CODE (p
) == TYPE_DECL
1354 && TREE_TYPE (p
) != error_mark_node
)
1355 set_type_context (TREE_TYPE (p
), context
);
1359 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1360 already been put there by store_parm_decls. Unused-
1361 parameter warnings are handled by function.c.
1362 error_mark_node obviously does not go in BLOCK_VARS and
1363 does not get unused-variable warnings. */
1366 /* It is possible for a decl not to have a name. We get
1367 here with b->id NULL in this case. */
1370 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
1371 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
1372 if (b
->shadowed
&& b
->shadowed
->u
.type
)
1373 TREE_TYPE (b
->shadowed
->decl
) = b
->shadowed
->u
.type
;
1383 /* Dispose of the block that we just made inside some higher level. */
1384 if ((scope
->function_body
|| scope
== file_scope
) && context
)
1386 DECL_INITIAL (context
) = block
;
1387 BLOCK_SUPERCONTEXT (block
) = context
;
1389 else if (scope
->outer
)
1392 SCOPE_LIST_APPEND (scope
->outer
, blocks
, block
);
1393 /* If we did not make a block for the scope just exited, any
1394 blocks made for inner scopes must be carried forward so they
1395 will later become subblocks of something else. */
1396 else if (scope
->blocks
)
1397 SCOPE_LIST_CONCAT (scope
->outer
, blocks
, scope
, blocks
);
1400 /* Pop the current scope, and free the structure for reuse. */
1401 current_scope
= scope
->outer
;
1402 if (scope
->function_body
)
1403 current_function_scope
= scope
->outer_function
;
1405 memset (scope
, 0, sizeof (struct c_scope
));
1406 scope
->outer
= scope_freelist
;
1407 scope_freelist
= scope
;
1413 push_file_scope (void)
1421 file_scope
= current_scope
;
1423 start_fname_decls ();
1425 for (decl
= visible_builtins
; decl
; decl
= DECL_CHAIN (decl
))
1426 bind (DECL_NAME (decl
), decl
, file_scope
,
1427 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl
));
1431 pop_file_scope (void)
1433 /* In case there were missing closebraces, get us back to the global
1435 while (current_scope
!= file_scope
)
1438 /* __FUNCTION__ is defined at file scope (""). This
1439 call may not be necessary as my tests indicate it
1440 still works without it. */
1441 finish_fname_decls ();
1443 check_inline_statics ();
1445 /* This is the point to write out a PCH if we're doing that.
1446 In that case we do not want to do anything else. */
1449 c_common_write_pch ();
1450 /* Ensure even the callers don't try to finalize the CU. */
1451 flag_syntax_only
= 1;
1455 /* Pop off the file scope and close this translation unit. */
1459 maybe_apply_pending_pragma_weaks ();
1462 /* Adjust the bindings for the start of a statement expression. */
1465 c_bindings_start_stmt_expr (struct c_spot_bindings
* switch_bindings
)
1467 struct c_scope
*scope
;
1469 for (scope
= current_scope
; scope
!= NULL
; scope
= scope
->outer
)
1471 struct c_binding
*b
;
1473 if (!scope
->has_label_bindings
)
1476 for (b
= scope
->bindings
; b
!= NULL
; b
= b
->prev
)
1478 struct c_label_vars
*label_vars
;
1480 struct c_goto_bindings
*g
;
1482 if (TREE_CODE (b
->decl
) != LABEL_DECL
)
1484 label_vars
= b
->u
.label
;
1485 ++label_vars
->label_bindings
.stmt_exprs
;
1486 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
1487 ++g
->goto_bindings
.stmt_exprs
;
1491 if (switch_bindings
!= NULL
)
1492 ++switch_bindings
->stmt_exprs
;
1495 /* Adjust the bindings for the end of a statement expression. */
1498 c_bindings_end_stmt_expr (struct c_spot_bindings
*switch_bindings
)
1500 struct c_scope
*scope
;
1502 for (scope
= current_scope
; scope
!= NULL
; scope
= scope
->outer
)
1504 struct c_binding
*b
;
1506 if (!scope
->has_label_bindings
)
1509 for (b
= scope
->bindings
; b
!= NULL
; b
= b
->prev
)
1511 struct c_label_vars
*label_vars
;
1513 struct c_goto_bindings
*g
;
1515 if (TREE_CODE (b
->decl
) != LABEL_DECL
)
1517 label_vars
= b
->u
.label
;
1518 --label_vars
->label_bindings
.stmt_exprs
;
1519 if (label_vars
->label_bindings
.stmt_exprs
< 0)
1521 label_vars
->label_bindings
.left_stmt_expr
= true;
1522 label_vars
->label_bindings
.stmt_exprs
= 0;
1524 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
1526 --g
->goto_bindings
.stmt_exprs
;
1527 if (g
->goto_bindings
.stmt_exprs
< 0)
1529 g
->goto_bindings
.left_stmt_expr
= true;
1530 g
->goto_bindings
.stmt_exprs
= 0;
1536 if (switch_bindings
!= NULL
)
1538 --switch_bindings
->stmt_exprs
;
1539 gcc_assert (switch_bindings
->stmt_exprs
>= 0);
1543 /* Push a definition or a declaration of struct, union or enum tag "name".
1544 "type" should be the type node.
1545 We assume that the tag "name" is not already defined, and has a location
1548 Note that the definition may really be just a forward reference.
1549 In that case, the TYPE_SIZE will be zero. */
1552 pushtag (location_t loc
, tree name
, tree type
)
1554 /* Record the identifier as the type's name if it has none. */
1555 if (name
&& !TYPE_NAME (type
))
1556 TYPE_NAME (type
) = name
;
1557 bind (name
, type
, current_scope
, /*invisible=*/false, /*nested=*/false, loc
);
1559 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1560 tagged type we just added to the current scope. This fake
1561 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1562 to output a representation of a tagged type, and it also gives
1563 us a convenient place to record the "scope start" address for the
1566 TYPE_STUB_DECL (type
) = pushdecl (build_decl (loc
,
1567 TYPE_DECL
, NULL_TREE
, type
));
1569 /* An approximation for now, so we can tell this is a function-scope tag.
1570 This will be updated in pop_scope. */
1571 TYPE_CONTEXT (type
) = DECL_CONTEXT (TYPE_STUB_DECL (type
));
1573 if (warn_cxx_compat
&& name
!= NULL_TREE
)
1575 struct c_binding
*b
= I_SYMBOL_BINDING (name
);
1578 && b
->decl
!= NULL_TREE
1579 && TREE_CODE (b
->decl
) == TYPE_DECL
1580 && (B_IN_CURRENT_SCOPE (b
)
1581 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
1582 && (TYPE_MAIN_VARIANT (TREE_TYPE (b
->decl
))
1583 != TYPE_MAIN_VARIANT (type
)))
1585 auto_diagnostic_group d
;
1586 if (warning_at (loc
, OPT_Wc___compat
,
1587 ("using %qD as both a typedef and a tag is "
1588 "invalid in C++"), b
->decl
)
1589 && b
->locus
!= UNKNOWN_LOCATION
)
1590 inform (b
->locus
, "originally defined here");
1595 /* An exported interface to pushtag. This is used by the gdb plugin's
1596 binding oracle to introduce a new tag binding. */
1599 c_pushtag (location_t loc
, tree name
, tree type
)
1601 pushtag (loc
, name
, type
);
1604 /* An exported interface to bind a declaration. LOC is the location
1605 to use. DECL is the declaration to bind. The decl's name is used
1606 to determine how it is bound. If DECL is a VAR_DECL, then
1607 IS_GLOBAL determines whether the decl is put into the global (file
1608 and external) scope or the current function's scope; if DECL is not
1609 a VAR_DECL then it is always put into the file scope. */
1612 c_bind (location_t loc
, tree decl
, bool is_global
)
1614 struct c_scope
*scope
;
1615 bool nested
= false;
1617 if (!VAR_P (decl
) || current_function_scope
== NULL
)
1619 /* Types and functions are always considered to be global. */
1621 DECL_EXTERNAL (decl
) = 1;
1622 TREE_PUBLIC (decl
) = 1;
1626 /* Also bind it into the external scope. */
1627 bind (DECL_NAME (decl
), decl
, external_scope
, true, false, loc
);
1630 DECL_EXTERNAL (decl
) = 1;
1631 TREE_PUBLIC (decl
) = 1;
1635 DECL_CONTEXT (decl
) = current_function_decl
;
1636 TREE_PUBLIC (decl
) = 0;
1637 scope
= current_function_scope
;
1640 bind (DECL_NAME (decl
), decl
, scope
, false, nested
, loc
);
1644 /* Stores the first FILE*, const struct tm* etc. argument type (whatever it
1645 is) seen in a declaration of a file I/O etc. built-in. Subsequent
1646 declarations of such built-ins are expected to refer to it rather than to
1647 fileptr_type_node etc. which is just void* (or to any other type).
1648 Used only by match_builtin_function_types. */
1650 static GTY(()) tree last_structptr_types
[6];
1652 /* Subroutine of compare_decls. Allow harmless mismatches in return
1653 and argument types provided that the type modes match. Set *STRICT
1654 and *ARGNO to the expected argument type and number in case of
1655 an argument type mismatch or null and zero otherwise. Return
1656 a unified type given a suitable match, and 0 otherwise. */
1659 match_builtin_function_types (tree newtype
, tree oldtype
,
1660 tree
*strict
, unsigned *argno
)
1662 /* Accept the return type of the new declaration if same modes. */
1663 tree oldrettype
= TREE_TYPE (oldtype
);
1664 tree newrettype
= TREE_TYPE (newtype
);
1667 *strict
= NULL_TREE
;
1669 if (TYPE_MODE (oldrettype
) != TYPE_MODE (newrettype
))
1672 if (!comptypes (TYPE_MAIN_VARIANT (oldrettype
),
1673 TYPE_MAIN_VARIANT (newrettype
)))
1674 *strict
= oldrettype
;
1676 tree oldargs
= TYPE_ARG_TYPES (oldtype
);
1677 tree newargs
= TYPE_ARG_TYPES (newtype
);
1678 tree tryargs
= newargs
;
1680 gcc_checking_assert ((sizeof (last_structptr_types
)
1681 / sizeof (last_structptr_types
[0]))
1682 == (sizeof (builtin_structptr_types
)
1683 / sizeof (builtin_structptr_types
[0])));
1684 for (unsigned i
= 1; oldargs
|| newargs
; ++i
)
1688 || !TREE_VALUE (oldargs
)
1689 || !TREE_VALUE (newargs
))
1692 tree oldtype
= TYPE_MAIN_VARIANT (TREE_VALUE (oldargs
));
1693 tree newtype
= TYPE_MAIN_VARIANT (TREE_VALUE (newargs
));
1695 /* Fail for types with incompatible modes/sizes. */
1696 if (TYPE_MODE (TREE_VALUE (oldargs
))
1697 != TYPE_MODE (TREE_VALUE (newargs
)))
1700 /* Fail for function and object pointer mismatches. */
1701 if ((FUNCTION_POINTER_TYPE_P (oldtype
)
1702 != FUNCTION_POINTER_TYPE_P (newtype
))
1703 || POINTER_TYPE_P (oldtype
) != POINTER_TYPE_P (newtype
))
1706 unsigned j
= (sizeof (builtin_structptr_types
)
1707 / sizeof (builtin_structptr_types
[0]));
1708 if (POINTER_TYPE_P (oldtype
))
1709 for (j
= 0; j
< (sizeof (builtin_structptr_types
)
1710 / sizeof (builtin_structptr_types
[0])); ++j
)
1712 if (TREE_VALUE (oldargs
) != builtin_structptr_types
[j
].node
)
1714 /* Store the first FILE* etc. argument type (whatever it is), and
1715 expect any subsequent declarations of file I/O etc. built-ins
1716 to refer to it rather than to fileptr_type_node etc. which is
1717 just void* (or const void*). */
1718 if (last_structptr_types
[j
])
1720 if (!comptypes (last_structptr_types
[j
], newtype
))
1723 *strict
= last_structptr_types
[j
];
1727 last_structptr_types
[j
] = newtype
;
1730 if (j
== (sizeof (builtin_structptr_types
)
1731 / sizeof (builtin_structptr_types
[0]))
1733 && !comptypes (oldtype
, newtype
))
1739 oldargs
= TREE_CHAIN (oldargs
);
1740 newargs
= TREE_CHAIN (newargs
);
1743 tree trytype
= build_function_type (newrettype
, tryargs
);
1745 /* Allow declaration to change transaction_safe attribute. */
1746 tree oldattrs
= TYPE_ATTRIBUTES (oldtype
);
1747 tree oldtsafe
= lookup_attribute ("transaction_safe", oldattrs
);
1748 tree newattrs
= TYPE_ATTRIBUTES (newtype
);
1749 tree newtsafe
= lookup_attribute ("transaction_safe", newattrs
);
1750 if (oldtsafe
&& !newtsafe
)
1751 oldattrs
= remove_attribute ("transaction_safe", oldattrs
);
1752 else if (newtsafe
&& !oldtsafe
)
1753 oldattrs
= tree_cons (get_identifier ("transaction_safe"),
1754 NULL_TREE
, oldattrs
);
1756 return build_type_attribute_variant (trytype
, oldattrs
);
1759 /* Subroutine of diagnose_mismatched_decls. Check for function type
1760 mismatch involving an empty arglist vs a nonempty one and give clearer
1763 diagnose_arglist_conflict (tree newdecl
, tree olddecl
,
1764 tree newtype
, tree oldtype
)
1768 if (TREE_CODE (olddecl
) != FUNCTION_DECL
1769 || !comptypes (TREE_TYPE (oldtype
), TREE_TYPE (newtype
))
1770 || !((!prototype_p (oldtype
) && DECL_INITIAL (olddecl
) == NULL_TREE
)
1771 || (!prototype_p (newtype
) && DECL_INITIAL (newdecl
) == NULL_TREE
)))
1774 t
= TYPE_ARG_TYPES (oldtype
);
1776 t
= TYPE_ARG_TYPES (newtype
);
1777 for (; t
; t
= TREE_CHAIN (t
))
1779 tree type
= TREE_VALUE (t
);
1781 if (TREE_CHAIN (t
) == NULL_TREE
1782 && TYPE_MAIN_VARIANT (type
) != void_type_node
)
1784 inform (input_location
, "a parameter list with an ellipsis "
1785 "cannot match an empty parameter name list declaration");
1789 if (c_type_promotes_to (type
) != type
)
1791 inform (input_location
, "an argument type that has a default "
1792 "promotion cannot match an empty parameter name list "
1799 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1800 old-style function definition, NEWDECL is a prototype declaration.
1801 Diagnose inconsistencies in the argument list. Returns TRUE if
1802 the prototype is compatible, FALSE if not. */
1804 validate_proto_after_old_defn (tree newdecl
, tree newtype
, tree oldtype
)
1806 tree newargs
, oldargs
;
1809 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1811 oldargs
= TYPE_ACTUAL_ARG_TYPES (oldtype
);
1812 newargs
= TYPE_ARG_TYPES (newtype
);
1817 tree oldargtype
= TREE_VALUE (oldargs
);
1818 tree newargtype
= TREE_VALUE (newargs
);
1820 if (oldargtype
== error_mark_node
|| newargtype
== error_mark_node
)
1823 oldargtype
= (TYPE_ATOMIC (oldargtype
)
1824 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype
),
1826 : TYPE_MAIN_VARIANT (oldargtype
));
1827 newargtype
= (TYPE_ATOMIC (newargtype
)
1828 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype
),
1830 : TYPE_MAIN_VARIANT (newargtype
));
1832 if (END_OF_ARGLIST (oldargtype
) && END_OF_ARGLIST (newargtype
))
1835 /* Reaching the end of just one list means the two decls don't
1836 agree on the number of arguments. */
1837 if (END_OF_ARGLIST (oldargtype
))
1839 error ("prototype for %q+D declares more arguments "
1840 "than previous old-style definition", newdecl
);
1843 else if (END_OF_ARGLIST (newargtype
))
1845 error ("prototype for %q+D declares fewer arguments "
1846 "than previous old-style definition", newdecl
);
1850 /* Type for passing arg must be consistent with that declared
1852 else if (!comptypes (oldargtype
, newargtype
))
1854 error ("prototype for %q+D declares argument %d"
1855 " with incompatible type",
1860 oldargs
= TREE_CHAIN (oldargs
);
1861 newargs
= TREE_CHAIN (newargs
);
1865 /* If we get here, no errors were found, but do issue a warning
1866 for this poor-style construct. */
1867 warning (0, "prototype for %q+D follows non-prototype definition",
1870 #undef END_OF_ARGLIST
1873 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1874 first in a pair of mismatched declarations, using the diagnostic
1877 locate_old_decl (tree decl
)
1879 if (TREE_CODE (decl
) == FUNCTION_DECL
&& fndecl_built_in_p (decl
)
1880 && !C_DECL_DECLARED_BUILTIN (decl
))
1882 else if (DECL_INITIAL (decl
))
1883 inform (input_location
, "previous definition of %q+D was here", decl
);
1884 else if (C_DECL_IMPLICIT (decl
))
1885 inform (input_location
, "previous implicit declaration of %q+D was here", decl
);
1887 inform (input_location
, "previous declaration of %q+D was here", decl
);
1890 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1891 Returns true if the caller should proceed to merge the two, false
1892 if OLDDECL should simply be discarded. As a side effect, issues
1893 all necessary diagnostics for invalid or poor-style combinations.
1894 If it returns true, writes the types of NEWDECL and OLDDECL to
1895 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1896 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1899 diagnose_mismatched_decls (tree newdecl
, tree olddecl
,
1900 tree
*newtypep
, tree
*oldtypep
)
1902 tree newtype
, oldtype
;
1905 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1906 && DECL_EXTERNAL (DECL))
1908 /* If we have error_mark_node for either decl or type, just discard
1909 the previous decl - we're in an error cascade already. */
1910 if (olddecl
== error_mark_node
|| newdecl
== error_mark_node
)
1912 *oldtypep
= oldtype
= TREE_TYPE (olddecl
);
1913 *newtypep
= newtype
= TREE_TYPE (newdecl
);
1914 if (oldtype
== error_mark_node
|| newtype
== error_mark_node
)
1917 /* Two different categories of symbol altogether. This is an error
1918 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1919 if (TREE_CODE (olddecl
) != TREE_CODE (newdecl
))
1921 if (!(TREE_CODE (olddecl
) == FUNCTION_DECL
1922 && fndecl_built_in_p (olddecl
)
1923 && !C_DECL_DECLARED_BUILTIN (olddecl
)))
1925 auto_diagnostic_group d
;
1926 error ("%q+D redeclared as different kind of symbol", newdecl
);
1927 locate_old_decl (olddecl
);
1929 else if (TREE_PUBLIC (newdecl
))
1930 warning (OPT_Wbuiltin_declaration_mismatch
,
1931 "built-in function %q+D declared as non-function",
1934 warning (OPT_Wshadow
, "declaration of %q+D shadows "
1935 "a built-in function", newdecl
);
1939 /* Enumerators have no linkage, so may only be declared once in a
1941 if (TREE_CODE (olddecl
) == CONST_DECL
)
1943 auto_diagnostic_group d
;
1944 error ("redeclaration of enumerator %q+D", newdecl
);
1945 locate_old_decl (olddecl
);
1949 bool pedwarned
= false;
1950 bool warned
= false;
1951 auto_diagnostic_group d
;
1953 if (!comptypes (oldtype
, newtype
))
1955 if (TREE_CODE (olddecl
) == FUNCTION_DECL
1956 && fndecl_built_in_p (olddecl
, BUILT_IN_NORMAL
)
1957 && !C_DECL_DECLARED_BUILTIN (olddecl
))
1959 /* Accept "harmless" mismatches in function types such
1960 as missing qualifiers or pointer vs same size integer
1961 mismatches. This is for the ffs and fprintf builtins.
1962 However, with -Wextra in effect, diagnose return and
1963 argument types that are incompatible according to
1965 tree mismatch_expect
;
1966 unsigned mismatch_argno
;
1968 tree trytype
= match_builtin_function_types (newtype
, oldtype
,
1972 if (trytype
&& comptypes (newtype
, trytype
))
1973 *oldtypep
= oldtype
= trytype
;
1976 /* If types don't match for a built-in, throw away the
1977 built-in. No point in calling locate_old_decl here, it
1978 won't print anything. */
1979 const char *header
= header_for_builtin_fn (olddecl
);
1980 location_t loc
= DECL_SOURCE_LOCATION (newdecl
);
1981 if (warning_at (loc
, OPT_Wbuiltin_declaration_mismatch
,
1982 "conflicting types for built-in function %q+D; "
1987 /* Suggest the right header to include as the preferred
1988 solution rather than the spelling of the declaration. */
1989 rich_location
richloc (line_table
, loc
);
1990 maybe_add_include_fixit (&richloc
, header
, true);
1992 "%qD is declared in header %qs", olddecl
, header
);
1997 if (mismatch_expect
&& extra_warnings
)
1999 /* If types match only loosely, print a warning but accept
2000 the redeclaration. */
2001 location_t newloc
= DECL_SOURCE_LOCATION (newdecl
);
2003 warning_at (newloc
, OPT_Wbuiltin_declaration_mismatch
,
2004 "mismatch in argument %u type of built-in "
2005 "function %qD; expected %qT",
2006 mismatch_argno
, newdecl
, mismatch_expect
);
2008 warning_at (newloc
, OPT_Wbuiltin_declaration_mismatch
,
2009 "mismatch in return type of built-in "
2010 "function %qD; expected %qT",
2011 newdecl
, mismatch_expect
);
2014 else if (TREE_CODE (olddecl
) == FUNCTION_DECL
2015 && DECL_IS_BUILTIN (olddecl
))
2017 /* A conflicting function declaration for a predeclared
2018 function that isn't actually built in. Objective C uses
2019 these. The new declaration silently overrides everything
2020 but the volatility (i.e. noreturn) indication. See also
2021 below. FIXME: Make Objective C use normal builtins. */
2022 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
2025 /* Permit void foo (...) to match int foo (...) if the latter is
2026 the definition and implicit int was used. See
2027 c-torture/compile/920625-2.c. */
2028 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
&& DECL_INITIAL (newdecl
)
2029 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype
)) == void_type_node
2030 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype
)) == integer_type_node
2031 && C_FUNCTION_IMPLICIT_INT (newdecl
) && !DECL_INITIAL (olddecl
))
2033 pedwarned
= pedwarn (input_location
, 0,
2034 "conflicting types for %q+D", newdecl
);
2035 /* Make sure we keep void as the return type. */
2036 TREE_TYPE (newdecl
) = *newtypep
= newtype
= oldtype
;
2037 C_FUNCTION_IMPLICIT_INT (newdecl
) = 0;
2039 /* Permit void foo (...) to match an earlier call to foo (...) with
2040 no declared type (thus, implicitly int). */
2041 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
2042 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype
)) == void_type_node
2043 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype
)) == integer_type_node
2044 && C_DECL_IMPLICIT (olddecl
) && !DECL_INITIAL (olddecl
))
2046 pedwarned
= pedwarn (input_location
, 0,
2047 "conflicting types for %q+D", newdecl
);
2048 /* Make sure we keep void as the return type. */
2049 TREE_TYPE (olddecl
) = *oldtypep
= oldtype
= newtype
;
2053 int new_quals
= TYPE_QUALS (newtype
);
2054 int old_quals
= TYPE_QUALS (oldtype
);
2056 if (new_quals
!= old_quals
)
2058 addr_space_t new_addr
= DECODE_QUAL_ADDR_SPACE (new_quals
);
2059 addr_space_t old_addr
= DECODE_QUAL_ADDR_SPACE (old_quals
);
2060 if (new_addr
!= old_addr
)
2062 if (ADDR_SPACE_GENERIC_P (new_addr
))
2063 error ("conflicting named address spaces (generic vs %s) "
2065 c_addr_space_name (old_addr
), newdecl
);
2066 else if (ADDR_SPACE_GENERIC_P (old_addr
))
2067 error ("conflicting named address spaces (%s vs generic) "
2069 c_addr_space_name (new_addr
), newdecl
);
2071 error ("conflicting named address spaces (%s vs %s) "
2073 c_addr_space_name (new_addr
),
2074 c_addr_space_name (old_addr
),
2078 if (CLEAR_QUAL_ADDR_SPACE (new_quals
)
2079 != CLEAR_QUAL_ADDR_SPACE (old_quals
))
2080 error ("conflicting type qualifiers for %q+D", newdecl
);
2083 error ("conflicting types for %q+D", newdecl
);
2084 diagnose_arglist_conflict (newdecl
, olddecl
, newtype
, oldtype
);
2085 locate_old_decl (olddecl
);
2090 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2091 but silently ignore the redeclaration if either is in a system
2092 header. (Conflicting redeclarations were handled above.) This
2093 is allowed for C11 if the types are the same, not just
2095 if (TREE_CODE (newdecl
) == TYPE_DECL
)
2097 bool types_different
= false;
2098 int comptypes_result
;
2101 = comptypes_check_different_types (oldtype
, newtype
, &types_different
);
2103 if (comptypes_result
!= 1 || types_different
)
2105 error ("redefinition of typedef %q+D with different type", newdecl
);
2106 locate_old_decl (olddecl
);
2110 if (DECL_IN_SYSTEM_HEADER (newdecl
)
2111 || DECL_IN_SYSTEM_HEADER (olddecl
)
2112 || TREE_NO_WARNING (newdecl
)
2113 || TREE_NO_WARNING (olddecl
))
2114 return true; /* Allow OLDDECL to continue in use. */
2116 if (variably_modified_type_p (newtype
, NULL
))
2118 error ("redefinition of typedef %q+D with variably modified type",
2120 locate_old_decl (olddecl
);
2122 else if (pedwarn_c99 (input_location
, OPT_Wpedantic
,
2123 "redefinition of typedef %q+D", newdecl
))
2124 locate_old_decl (olddecl
);
2129 /* Function declarations can either be 'static' or 'extern' (no
2130 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2131 can never conflict with each other on account of linkage
2132 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2133 gnu89 mode permits two definitions if one is 'extern inline' and
2134 one is not. The non- extern-inline definition supersedes the
2135 extern-inline definition. */
2137 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2139 /* If you declare a built-in function name as static, or
2140 define the built-in with an old-style definition (so we
2141 can't validate the argument list) the built-in definition is
2142 overridden, but optionally warn this was a bad choice of name. */
2143 if (fndecl_built_in_p (olddecl
)
2144 && !C_DECL_DECLARED_BUILTIN (olddecl
))
2146 if (!TREE_PUBLIC (newdecl
)
2147 || (DECL_INITIAL (newdecl
)
2148 && !prototype_p (TREE_TYPE (newdecl
))))
2150 warning_at (DECL_SOURCE_LOCATION (newdecl
),
2151 OPT_Wshadow
, "declaration of %qD shadows "
2152 "a built-in function", newdecl
);
2153 /* Discard the old built-in function. */
2157 if (!prototype_p (TREE_TYPE (newdecl
)))
2159 /* Set for built-ins that take no arguments. */
2160 bool func_void_args
= false;
2161 if (tree at
= TYPE_ARG_TYPES (oldtype
))
2162 func_void_args
= VOID_TYPE_P (TREE_VALUE (at
));
2164 if (extra_warnings
&& !func_void_args
)
2165 warning_at (DECL_SOURCE_LOCATION (newdecl
),
2166 OPT_Wbuiltin_declaration_mismatch
,
2167 "declaration of built-in function %qD without "
2168 "a prototype; expected %qT",
2169 newdecl
, TREE_TYPE (olddecl
));
2173 if (DECL_INITIAL (newdecl
))
2175 if (DECL_INITIAL (olddecl
))
2177 /* If both decls are in the same TU and the new declaration
2178 isn't overriding an extern inline reject the new decl.
2179 In c99, no overriding is allowed in the same translation
2181 if ((!DECL_EXTERN_INLINE (olddecl
)
2182 || DECL_EXTERN_INLINE (newdecl
)
2183 || (!flag_gnu89_inline
2184 && (!DECL_DECLARED_INLINE_P (olddecl
)
2185 || !lookup_attribute ("gnu_inline",
2186 DECL_ATTRIBUTES (olddecl
)))
2187 && (!DECL_DECLARED_INLINE_P (newdecl
)
2188 || !lookup_attribute ("gnu_inline",
2189 DECL_ATTRIBUTES (newdecl
))))
2191 && same_translation_unit_p (newdecl
, olddecl
))
2193 auto_diagnostic_group d
;
2194 error ("redefinition of %q+D", newdecl
);
2195 locate_old_decl (olddecl
);
2200 /* If we have a prototype after an old-style function definition,
2201 the argument types must be checked specially. */
2202 else if (DECL_INITIAL (olddecl
)
2203 && !prototype_p (oldtype
) && prototype_p (newtype
)
2204 && TYPE_ACTUAL_ARG_TYPES (oldtype
))
2206 auto_diagnostic_group d
;
2207 if (!validate_proto_after_old_defn (newdecl
, newtype
, oldtype
))
2209 locate_old_decl (olddecl
);
2213 /* A non-static declaration (even an "extern") followed by a
2214 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2215 The same is true for a static forward declaration at block
2216 scope followed by a non-static declaration/definition at file
2217 scope. Static followed by non-static at the same scope is
2218 not undefined behavior, and is the most convenient way to get
2219 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2220 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2221 we do diagnose it if -Wtraditional. */
2222 if (TREE_PUBLIC (olddecl
) && !TREE_PUBLIC (newdecl
))
2224 /* Two exceptions to the rule. If olddecl is an extern
2225 inline, or a predeclared function that isn't actually
2226 built in, newdecl silently overrides olddecl. The latter
2227 occur only in Objective C; see also above. (FIXME: Make
2228 Objective C use normal builtins.) */
2229 if (!DECL_IS_BUILTIN (olddecl
)
2230 && !DECL_EXTERN_INLINE (olddecl
))
2232 auto_diagnostic_group d
;
2233 error ("static declaration of %q+D follows "
2234 "non-static declaration", newdecl
);
2235 locate_old_decl (olddecl
);
2239 else if (TREE_PUBLIC (newdecl
) && !TREE_PUBLIC (olddecl
))
2241 if (DECL_CONTEXT (olddecl
))
2243 auto_diagnostic_group d
;
2244 error ("non-static declaration of %q+D follows "
2245 "static declaration", newdecl
);
2246 locate_old_decl (olddecl
);
2249 else if (warn_traditional
)
2251 warned
|= warning (OPT_Wtraditional
,
2252 "non-static declaration of %q+D "
2253 "follows static declaration", newdecl
);
2257 /* Make sure gnu_inline attribute is either not present, or
2258 present on all inline decls. */
2259 if (DECL_DECLARED_INLINE_P (olddecl
)
2260 && DECL_DECLARED_INLINE_P (newdecl
))
2262 bool newa
= lookup_attribute ("gnu_inline",
2263 DECL_ATTRIBUTES (newdecl
)) != NULL
;
2264 bool olda
= lookup_attribute ("gnu_inline",
2265 DECL_ATTRIBUTES (olddecl
)) != NULL
;
2268 auto_diagnostic_group d
;
2269 error_at (input_location
, "%<gnu_inline%> attribute present on %q+D",
2270 newa
? newdecl
: olddecl
);
2271 error_at (DECL_SOURCE_LOCATION (newa
? olddecl
: newdecl
),
2276 else if (VAR_P (newdecl
))
2278 /* Only variables can be thread-local, and all declarations must
2279 agree on this property. */
2280 if (C_DECL_THREADPRIVATE_P (olddecl
) && !DECL_THREAD_LOCAL_P (newdecl
))
2282 /* Nothing to check. Since OLDDECL is marked threadprivate
2283 and NEWDECL does not have a thread-local attribute, we
2284 will merge the threadprivate attribute into NEWDECL. */
2287 else if (DECL_THREAD_LOCAL_P (newdecl
) != DECL_THREAD_LOCAL_P (olddecl
))
2289 auto_diagnostic_group d
;
2290 if (DECL_THREAD_LOCAL_P (newdecl
))
2291 error ("thread-local declaration of %q+D follows "
2292 "non-thread-local declaration", newdecl
);
2294 error ("non-thread-local declaration of %q+D follows "
2295 "thread-local declaration", newdecl
);
2297 locate_old_decl (olddecl
);
2301 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2302 if (DECL_INITIAL (newdecl
) && DECL_INITIAL (olddecl
))
2304 auto_diagnostic_group d
;
2305 error ("redefinition of %q+D", newdecl
);
2306 locate_old_decl (olddecl
);
2310 /* Objects declared at file scope: if the first declaration had
2311 external linkage (even if it was an external reference) the
2312 second must have external linkage as well, or the behavior is
2313 undefined. If the first declaration had internal linkage, then
2314 the second must too, or else be an external reference (in which
2315 case the composite declaration still has internal linkage).
2316 As for function declarations, we warn about the static-then-
2317 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2318 if (DECL_FILE_SCOPE_P (newdecl
)
2319 && TREE_PUBLIC (newdecl
) != TREE_PUBLIC (olddecl
))
2321 if (DECL_EXTERNAL (newdecl
))
2323 if (!DECL_FILE_SCOPE_P (olddecl
))
2325 auto_diagnostic_group d
;
2326 error ("extern declaration of %q+D follows "
2327 "declaration with no linkage", newdecl
);
2328 locate_old_decl (olddecl
);
2331 else if (warn_traditional
)
2333 warned
|= warning (OPT_Wtraditional
,
2334 "non-static declaration of %q+D "
2335 "follows static declaration", newdecl
);
2340 auto_diagnostic_group d
;
2341 if (TREE_PUBLIC (newdecl
))
2342 error ("non-static declaration of %q+D follows "
2343 "static declaration", newdecl
);
2345 error ("static declaration of %q+D follows "
2346 "non-static declaration", newdecl
);
2348 locate_old_decl (olddecl
);
2352 /* Two objects with the same name declared at the same block
2353 scope must both be external references (6.7p3). */
2354 else if (!DECL_FILE_SCOPE_P (newdecl
))
2356 if (DECL_EXTERNAL (newdecl
))
2358 /* Extern with initializer at block scope, which will
2359 already have received an error. */
2361 else if (DECL_EXTERNAL (olddecl
))
2363 auto_diagnostic_group d
;
2364 error ("declaration of %q+D with no linkage follows "
2365 "extern declaration", newdecl
);
2366 locate_old_decl (olddecl
);
2370 auto_diagnostic_group d
;
2371 error ("redeclaration of %q+D with no linkage", newdecl
);
2372 locate_old_decl (olddecl
);
2378 /* C++ does not permit a decl to appear multiple times at file
2381 && DECL_FILE_SCOPE_P (newdecl
)
2382 && !DECL_EXTERNAL (newdecl
)
2383 && !DECL_EXTERNAL (olddecl
))
2384 warned
|= warning_at (DECL_SOURCE_LOCATION (newdecl
),
2386 ("duplicate declaration of %qD is "
2392 /* All decls must agree on a visibility. */
2393 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl
), TS_DECL_WITH_VIS
)
2394 && DECL_VISIBILITY_SPECIFIED (newdecl
) && DECL_VISIBILITY_SPECIFIED (olddecl
)
2395 && DECL_VISIBILITY (newdecl
) != DECL_VISIBILITY (olddecl
))
2397 warned
|= warning (0, "redeclaration of %q+D with different visibility "
2398 "(old visibility preserved)", newdecl
);
2401 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2402 warned
|= diagnose_mismatched_attributes (olddecl
, newdecl
);
2403 else /* PARM_DECL, VAR_DECL */
2405 /* Redeclaration of a parameter is a constraint violation (this is
2406 not explicitly stated, but follows from C99 6.7p3 [no more than
2407 one declaration of the same identifier with no linkage in the
2408 same scope, except type tags] and 6.2.2p6 [parameters have no
2409 linkage]). We must check for a forward parameter declaration,
2410 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2411 an extension, the mandatory diagnostic for which is handled by
2412 mark_forward_parm_decls. */
2414 if (TREE_CODE (newdecl
) == PARM_DECL
2415 && (!TREE_ASM_WRITTEN (olddecl
) || TREE_ASM_WRITTEN (newdecl
)))
2417 auto_diagnostic_group d
;
2418 error ("redefinition of parameter %q+D", newdecl
);
2419 locate_old_decl (olddecl
);
2424 /* Optional warning for completely redundant decls. */
2425 if (!warned
&& !pedwarned
2426 && warn_redundant_decls
2427 /* Don't warn about a function declaration followed by a
2429 && !(TREE_CODE (newdecl
) == FUNCTION_DECL
2430 && DECL_INITIAL (newdecl
) && !DECL_INITIAL (olddecl
))
2431 /* Don't warn about redundant redeclarations of builtins. */
2432 && !(TREE_CODE (newdecl
) == FUNCTION_DECL
2433 && !fndecl_built_in_p (newdecl
)
2434 && fndecl_built_in_p (olddecl
)
2435 && !C_DECL_DECLARED_BUILTIN (olddecl
))
2436 /* Don't warn about an extern followed by a definition. */
2437 && !(DECL_EXTERNAL (olddecl
) && !DECL_EXTERNAL (newdecl
))
2438 /* Don't warn about forward parameter decls. */
2439 && !(TREE_CODE (newdecl
) == PARM_DECL
2440 && TREE_ASM_WRITTEN (olddecl
) && !TREE_ASM_WRITTEN (newdecl
))
2441 /* Don't warn about a variable definition following a declaration. */
2442 && !(VAR_P (newdecl
)
2443 && DECL_INITIAL (newdecl
) && !DECL_INITIAL (olddecl
)))
2445 warned
= warning (OPT_Wredundant_decls
, "redundant redeclaration of %q+D",
2449 /* Report location of previous decl/defn. */
2450 if (warned
|| pedwarned
)
2451 locate_old_decl (olddecl
);
2453 #undef DECL_EXTERN_INLINE
2458 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2459 consistent with OLDDECL, but carries new information. Merge the
2460 new information into OLDDECL. This function issues no
2464 merge_decls (tree newdecl
, tree olddecl
, tree newtype
, tree oldtype
)
2466 bool new_is_definition
= (TREE_CODE (newdecl
) == FUNCTION_DECL
2467 && DECL_INITIAL (newdecl
) != NULL_TREE
);
2468 bool new_is_prototype
= (TREE_CODE (newdecl
) == FUNCTION_DECL
2469 && prototype_p (TREE_TYPE (newdecl
)));
2470 bool old_is_prototype
= (TREE_CODE (olddecl
) == FUNCTION_DECL
2471 && prototype_p (TREE_TYPE (olddecl
)));
2473 /* For real parm decl following a forward decl, rechain the old decl
2474 in its new location and clear TREE_ASM_WRITTEN (it's not a
2475 forward decl anymore). */
2476 if (TREE_CODE (newdecl
) == PARM_DECL
2477 && TREE_ASM_WRITTEN (olddecl
) && !TREE_ASM_WRITTEN (newdecl
))
2479 struct c_binding
*b
, **here
;
2481 for (here
= ¤t_scope
->bindings
; *here
; here
= &(*here
)->prev
)
2482 if ((*here
)->decl
== olddecl
)
2489 b
->prev
= current_scope
->bindings
;
2490 current_scope
->bindings
= b
;
2492 TREE_ASM_WRITTEN (olddecl
) = 0;
2495 DECL_ATTRIBUTES (newdecl
)
2496 = targetm
.merge_decl_attributes (olddecl
, newdecl
);
2498 /* For typedefs use the old type, as the new type's DECL_NAME points
2499 at newdecl, which will be ggc_freed. */
2500 if (TREE_CODE (newdecl
) == TYPE_DECL
)
2502 /* But NEWTYPE might have an attribute, honor that. */
2506 if (TYPE_USER_ALIGN (tem
))
2508 if (TYPE_ALIGN (tem
) > TYPE_ALIGN (newtype
))
2509 SET_TYPE_ALIGN (newtype
, TYPE_ALIGN (tem
));
2510 TYPE_USER_ALIGN (newtype
) = true;
2513 /* And remove the new type from the variants list. */
2514 if (TYPE_NAME (TREE_TYPE (newdecl
)) == newdecl
)
2516 tree remove
= TREE_TYPE (newdecl
);
2517 if (TYPE_MAIN_VARIANT (remove
) == remove
)
2519 gcc_assert (TYPE_NEXT_VARIANT (remove
) == NULL_TREE
);
2520 /* If remove is the main variant, no need to remove that
2521 from the list. One of the DECL_ORIGINAL_TYPE
2522 variants, e.g. created for aligned attribute, might still
2523 refer to the newdecl TYPE_DECL though, so remove that one
2525 if (DECL_ORIGINAL_TYPE (newdecl
)
2526 && DECL_ORIGINAL_TYPE (newdecl
) != remove
)
2527 for (tree t
= TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl
));
2528 t
; t
= TYPE_MAIN_VARIANT (t
))
2529 if (TYPE_NAME (TYPE_NEXT_VARIANT (t
)) == newdecl
)
2531 TYPE_NEXT_VARIANT (t
)
2532 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t
));
2537 for (tree t
= TYPE_MAIN_VARIANT (remove
); ;
2538 t
= TYPE_NEXT_VARIANT (t
))
2539 if (TYPE_NEXT_VARIANT (t
) == remove
)
2541 TYPE_NEXT_VARIANT (t
) = TYPE_NEXT_VARIANT (remove
);
2547 /* Merge the data types specified in the two decls. */
2549 = TREE_TYPE (olddecl
)
2550 = composite_type (newtype
, oldtype
);
2552 /* Lay the type out, unless already done. */
2553 if (!comptypes (oldtype
, TREE_TYPE (newdecl
)))
2555 if (TREE_TYPE (newdecl
) != error_mark_node
)
2556 layout_type (TREE_TYPE (newdecl
));
2557 if (TREE_CODE (newdecl
) != FUNCTION_DECL
2558 && TREE_CODE (newdecl
) != TYPE_DECL
2559 && TREE_CODE (newdecl
) != CONST_DECL
)
2560 layout_decl (newdecl
, 0);
2564 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2565 DECL_SIZE (newdecl
) = DECL_SIZE (olddecl
);
2566 DECL_SIZE_UNIT (newdecl
) = DECL_SIZE_UNIT (olddecl
);
2567 SET_DECL_MODE (newdecl
, DECL_MODE (olddecl
));
2568 if (DECL_ALIGN (olddecl
) > DECL_ALIGN (newdecl
))
2570 SET_DECL_ALIGN (newdecl
, DECL_ALIGN (olddecl
));
2571 DECL_USER_ALIGN (newdecl
) |= DECL_USER_ALIGN (olddecl
);
2573 if (DECL_WARN_IF_NOT_ALIGN (olddecl
)
2574 > DECL_WARN_IF_NOT_ALIGN (newdecl
))
2575 SET_DECL_WARN_IF_NOT_ALIGN (newdecl
,
2576 DECL_WARN_IF_NOT_ALIGN (olddecl
));
2579 /* Keep the old rtl since we can safely use it. */
2580 if (HAS_RTL_P (olddecl
))
2581 COPY_DECL_RTL (olddecl
, newdecl
);
2583 /* Merge the type qualifiers. */
2584 if (TREE_READONLY (newdecl
))
2585 TREE_READONLY (olddecl
) = 1;
2587 if (TREE_THIS_VOLATILE (newdecl
))
2588 TREE_THIS_VOLATILE (olddecl
) = 1;
2590 /* Merge deprecatedness. */
2591 if (TREE_DEPRECATED (newdecl
))
2592 TREE_DEPRECATED (olddecl
) = 1;
2594 /* If a decl is in a system header and the other isn't, keep the one on the
2595 system header. Otherwise, keep source location of definition rather than
2596 declaration and of prototype rather than non-prototype unless that
2597 prototype is built-in. */
2598 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl
), TS_DECL_WITH_VIS
)
2599 && DECL_IN_SYSTEM_HEADER (olddecl
)
2600 && !DECL_IN_SYSTEM_HEADER (newdecl
) )
2601 DECL_SOURCE_LOCATION (newdecl
) = DECL_SOURCE_LOCATION (olddecl
);
2602 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl
), TS_DECL_WITH_VIS
)
2603 && DECL_IN_SYSTEM_HEADER (newdecl
)
2604 && !DECL_IN_SYSTEM_HEADER (olddecl
))
2605 DECL_SOURCE_LOCATION (olddecl
) = DECL_SOURCE_LOCATION (newdecl
);
2606 else if ((DECL_INITIAL (newdecl
) == NULL_TREE
2607 && DECL_INITIAL (olddecl
) != NULL_TREE
)
2608 || (old_is_prototype
&& !new_is_prototype
2609 && !C_DECL_BUILTIN_PROTOTYPE (olddecl
)))
2610 DECL_SOURCE_LOCATION (newdecl
) = DECL_SOURCE_LOCATION (olddecl
);
2612 /* Merge the initialization information. */
2613 if (DECL_INITIAL (newdecl
) == NULL_TREE
)
2614 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
2616 /* Merge the threadprivate attribute. */
2617 if (VAR_P (olddecl
) && C_DECL_THREADPRIVATE_P (olddecl
))
2618 C_DECL_THREADPRIVATE_P (newdecl
) = 1;
2620 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl
), TS_DECL_WITH_VIS
))
2622 /* Copy the assembler name.
2623 Currently, it can only be defined in the prototype. */
2624 COPY_DECL_ASSEMBLER_NAME (olddecl
, newdecl
);
2626 /* Use visibility of whichever declaration had it specified */
2627 if (DECL_VISIBILITY_SPECIFIED (olddecl
))
2629 DECL_VISIBILITY (newdecl
) = DECL_VISIBILITY (olddecl
);
2630 DECL_VISIBILITY_SPECIFIED (newdecl
) = 1;
2633 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2635 DECL_STATIC_CONSTRUCTOR(newdecl
) |= DECL_STATIC_CONSTRUCTOR(olddecl
);
2636 DECL_STATIC_DESTRUCTOR (newdecl
) |= DECL_STATIC_DESTRUCTOR (olddecl
);
2637 DECL_NO_LIMIT_STACK (newdecl
) |= DECL_NO_LIMIT_STACK (olddecl
);
2638 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl
)
2639 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl
);
2640 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
2641 DECL_IS_MALLOC (newdecl
) |= DECL_IS_MALLOC (olddecl
);
2642 if (DECL_IS_OPERATOR_NEW_P (olddecl
))
2643 DECL_SET_IS_OPERATOR_NEW (newdecl
, true);
2644 if (DECL_IS_OPERATOR_DELETE_P (olddecl
))
2645 DECL_SET_IS_OPERATOR_DELETE (newdecl
, true);
2646 TREE_READONLY (newdecl
) |= TREE_READONLY (olddecl
);
2647 DECL_PURE_P (newdecl
) |= DECL_PURE_P (olddecl
);
2648 DECL_IS_NOVOPS (newdecl
) |= DECL_IS_NOVOPS (olddecl
);
2651 /* Merge the storage class information. */
2652 merge_weak (newdecl
, olddecl
);
2654 /* For functions, static overrides non-static. */
2655 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2657 TREE_PUBLIC (newdecl
) &= TREE_PUBLIC (olddecl
);
2658 /* This is since we don't automatically
2659 copy the attributes of NEWDECL into OLDDECL. */
2660 TREE_PUBLIC (olddecl
) = TREE_PUBLIC (newdecl
);
2661 /* If this clears `static', clear it in the identifier too. */
2662 if (!TREE_PUBLIC (olddecl
))
2663 TREE_PUBLIC (DECL_NAME (olddecl
)) = 0;
2667 /* In c99, 'extern' declaration before (or after) 'inline' means this
2668 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2670 if (TREE_CODE (newdecl
) == FUNCTION_DECL
2671 && !flag_gnu89_inline
2672 && (DECL_DECLARED_INLINE_P (newdecl
)
2673 || DECL_DECLARED_INLINE_P (olddecl
))
2674 && (!DECL_DECLARED_INLINE_P (newdecl
)
2675 || !DECL_DECLARED_INLINE_P (olddecl
)
2676 || !DECL_EXTERNAL (olddecl
))
2677 && DECL_EXTERNAL (newdecl
)
2678 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl
))
2679 && !current_function_decl
)
2680 DECL_EXTERNAL (newdecl
) = 0;
2682 /* An inline definition following a static declaration is not
2684 if (new_is_definition
2685 && (DECL_DECLARED_INLINE_P (newdecl
)
2686 || DECL_DECLARED_INLINE_P (olddecl
))
2687 && !TREE_PUBLIC (olddecl
))
2688 DECL_EXTERNAL (newdecl
) = 0;
2690 if (DECL_EXTERNAL (newdecl
))
2692 TREE_STATIC (newdecl
) = TREE_STATIC (olddecl
);
2693 DECL_EXTERNAL (newdecl
) = DECL_EXTERNAL (olddecl
);
2695 /* An extern decl does not override previous storage class. */
2696 TREE_PUBLIC (newdecl
) = TREE_PUBLIC (olddecl
);
2697 if (!DECL_EXTERNAL (newdecl
))
2699 DECL_CONTEXT (newdecl
) = DECL_CONTEXT (olddecl
);
2700 DECL_COMMON (newdecl
) = DECL_COMMON (olddecl
);
2705 TREE_STATIC (olddecl
) = TREE_STATIC (newdecl
);
2706 TREE_PUBLIC (olddecl
) = TREE_PUBLIC (newdecl
);
2709 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2711 /* If we're redefining a function previously defined as extern
2712 inline, make sure we emit debug info for the inline before we
2713 throw it away, in case it was inlined into a function that
2714 hasn't been written out yet. */
2715 if (new_is_definition
&& DECL_INITIAL (olddecl
))
2716 /* The new defn must not be inline. */
2717 DECL_UNINLINABLE (newdecl
) = 1;
2720 /* If either decl says `inline', this fn is inline, unless
2721 its definition was passed already. */
2722 if (DECL_DECLARED_INLINE_P (newdecl
)
2723 || DECL_DECLARED_INLINE_P (olddecl
))
2724 DECL_DECLARED_INLINE_P (newdecl
) = 1;
2726 DECL_UNINLINABLE (newdecl
) = DECL_UNINLINABLE (olddecl
)
2727 = (DECL_UNINLINABLE (newdecl
) || DECL_UNINLINABLE (olddecl
));
2729 DECL_DISREGARD_INLINE_LIMITS (newdecl
)
2730 = DECL_DISREGARD_INLINE_LIMITS (olddecl
)
2731 = (DECL_DISREGARD_INLINE_LIMITS (newdecl
)
2732 || DECL_DISREGARD_INLINE_LIMITS (olddecl
));
2735 if (fndecl_built_in_p (olddecl
))
2737 /* If redeclaring a builtin function, it stays built in.
2738 But it gets tagged as having been declared. */
2739 copy_decl_built_in_function (newdecl
, olddecl
);
2740 C_DECL_DECLARED_BUILTIN (newdecl
) = 1;
2741 if (new_is_prototype
)
2743 C_DECL_BUILTIN_PROTOTYPE (newdecl
) = 0;
2744 if (DECL_BUILT_IN_CLASS (newdecl
) == BUILT_IN_NORMAL
)
2746 enum built_in_function fncode
= DECL_FUNCTION_CODE (newdecl
);
2749 /* If a compatible prototype of these builtin functions
2750 is seen, assume the runtime implements it with the
2751 expected semantics. */
2752 case BUILT_IN_STPCPY
:
2753 if (builtin_decl_explicit_p (fncode
))
2754 set_builtin_decl_implicit_p (fncode
, true);
2757 if (builtin_decl_explicit_p (fncode
))
2758 set_builtin_decl_declared_p (fncode
, true);
2762 copy_attributes_to_builtin (newdecl
);
2766 C_DECL_BUILTIN_PROTOTYPE (newdecl
)
2767 = C_DECL_BUILTIN_PROTOTYPE (olddecl
);
2770 /* Preserve function specific target and optimization options */
2771 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl
)
2772 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl
))
2773 DECL_FUNCTION_SPECIFIC_TARGET (newdecl
)
2774 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl
);
2776 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
)
2777 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
))
2778 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
)
2779 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
);
2781 /* Also preserve various other info from the definition. */
2782 if (!new_is_definition
)
2785 DECL_RESULT (newdecl
) = DECL_RESULT (olddecl
);
2786 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
2787 DECL_STRUCT_FUNCTION (newdecl
) = DECL_STRUCT_FUNCTION (olddecl
);
2788 DECL_SAVED_TREE (newdecl
) = DECL_SAVED_TREE (olddecl
);
2789 DECL_ARGUMENTS (newdecl
) = copy_list (DECL_ARGUMENTS (olddecl
));
2790 for (t
= DECL_ARGUMENTS (newdecl
); t
; t
= DECL_CHAIN (t
))
2791 DECL_CONTEXT (t
) = newdecl
;
2793 /* See if we've got a function to instantiate from. */
2794 if (DECL_SAVED_TREE (olddecl
))
2795 DECL_ABSTRACT_ORIGIN (newdecl
)
2796 = DECL_ABSTRACT_ORIGIN (olddecl
);
2800 /* Merge the USED information. */
2801 if (TREE_USED (olddecl
))
2802 TREE_USED (newdecl
) = 1;
2803 else if (TREE_USED (newdecl
))
2804 TREE_USED (olddecl
) = 1;
2805 if (VAR_P (olddecl
) || TREE_CODE (olddecl
) == PARM_DECL
)
2806 DECL_READ_P (newdecl
) |= DECL_READ_P (olddecl
);
2807 if (DECL_PRESERVE_P (olddecl
))
2808 DECL_PRESERVE_P (newdecl
) = 1;
2809 else if (DECL_PRESERVE_P (newdecl
))
2810 DECL_PRESERVE_P (olddecl
) = 1;
2812 /* Merge DECL_COMMON */
2813 if (VAR_P (olddecl
) && VAR_P (newdecl
)
2814 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl
))
2815 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl
)))
2816 DECL_COMMON (newdecl
) = DECL_COMMON (newdecl
) && DECL_COMMON (olddecl
);
2818 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2819 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2820 DECL_ARGUMENTS (if appropriate). */
2822 unsigned olddecl_uid
= DECL_UID (olddecl
);
2823 tree olddecl_context
= DECL_CONTEXT (olddecl
);
2824 tree olddecl_arguments
= NULL
;
2825 if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
2826 olddecl_arguments
= DECL_ARGUMENTS (olddecl
);
2828 memcpy ((char *) olddecl
+ sizeof (struct tree_common
),
2829 (char *) newdecl
+ sizeof (struct tree_common
),
2830 sizeof (struct tree_decl_common
) - sizeof (struct tree_common
));
2831 DECL_USER_ALIGN (olddecl
) = DECL_USER_ALIGN (newdecl
);
2832 switch (TREE_CODE (olddecl
))
2837 struct symtab_node
*snode
= olddecl
->decl_with_vis
.symtab_node
;
2839 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2840 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2841 tree_code_size (TREE_CODE (olddecl
)) - sizeof (struct tree_decl_common
));
2842 olddecl
->decl_with_vis
.symtab_node
= snode
;
2844 if ((DECL_EXTERNAL (olddecl
)
2845 || TREE_PUBLIC (olddecl
)
2846 || TREE_STATIC (olddecl
))
2847 && DECL_SECTION_NAME (newdecl
) != NULL
)
2848 set_decl_section_name (olddecl
, DECL_SECTION_NAME (newdecl
));
2850 /* This isn't quite correct for something like
2851 int __thread x attribute ((tls_model ("local-exec")));
2852 extern int __thread x;
2853 as we'll lose the "local-exec" model. */
2854 if (VAR_P (olddecl
) && DECL_THREAD_LOCAL_P (newdecl
))
2855 set_decl_tls_model (olddecl
, DECL_TLS_MODEL (newdecl
));
2865 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2866 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2867 tree_code_size (TREE_CODE (olddecl
)) - sizeof (struct tree_decl_common
));
2872 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2873 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2874 sizeof (struct tree_decl_non_common
) - sizeof (struct tree_decl_common
));
2876 DECL_UID (olddecl
) = olddecl_uid
;
2877 DECL_CONTEXT (olddecl
) = olddecl_context
;
2878 if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
2879 DECL_ARGUMENTS (olddecl
) = olddecl_arguments
;
2882 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2883 so that encode_section_info has a chance to look at the new decl
2884 flags and attributes. */
2885 if (DECL_RTL_SET_P (olddecl
)
2886 && (TREE_CODE (olddecl
) == FUNCTION_DECL
2887 || (VAR_P (olddecl
) && TREE_STATIC (olddecl
))))
2888 make_decl_rtl (olddecl
);
2891 /* Handle when a new declaration NEWDECL has the same name as an old
2892 one OLDDECL in the same binding contour. Prints an error message
2895 If safely possible, alter OLDDECL to look like NEWDECL, and return
2896 true. Otherwise, return false. */
2899 duplicate_decls (tree newdecl
, tree olddecl
)
2901 tree newtype
= NULL
, oldtype
= NULL
;
2903 if (!diagnose_mismatched_decls (newdecl
, olddecl
, &newtype
, &oldtype
))
2905 /* Avoid `unused variable' and other warnings for OLDDECL. */
2906 TREE_NO_WARNING (olddecl
) = 1;
2910 merge_decls (newdecl
, olddecl
, newtype
, oldtype
);
2912 /* The NEWDECL will no longer be needed.
2914 Before releasing the node, be sure to remove function from symbol
2915 table that might have been inserted there to record comdat group.
2916 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2917 structure is shared in between NEWDECL and OLDECL. */
2918 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2919 DECL_STRUCT_FUNCTION (newdecl
) = NULL
;
2920 if (VAR_OR_FUNCTION_DECL_P (newdecl
))
2922 struct symtab_node
*snode
= symtab_node::get (newdecl
);
2931 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2933 warn_if_shadowing (tree new_decl
)
2935 struct c_binding
*b
;
2937 /* Shadow warnings wanted? */
2939 || warn_shadow_local
2940 || warn_shadow_compatible_local
)
2941 /* No shadow warnings for internally generated vars. */
2942 || DECL_IS_BUILTIN (new_decl
))
2945 /* Is anything being shadowed? Invisible decls do not count. */
2946 for (b
= I_SYMBOL_BINDING (DECL_NAME (new_decl
)); b
; b
= b
->shadowed
)
2947 if (b
->decl
&& b
->decl
!= new_decl
&& !b
->invisible
2948 && (b
->decl
== error_mark_node
2949 || diagnostic_report_warnings_p (global_dc
,
2950 DECL_SOURCE_LOCATION (b
->decl
))))
2952 tree old_decl
= b
->decl
;
2954 if (old_decl
== error_mark_node
)
2956 warning (OPT_Wshadow
, "declaration of %q+D shadows previous "
2957 "non-variable", new_decl
);
2961 bool warned
= false;
2962 auto_diagnostic_group d
;
2963 if (TREE_CODE (old_decl
) == PARM_DECL
)
2965 enum opt_code warning_code
;
2967 /* If '-Wshadow=compatible-local' is specified without other
2968 -Wshadow= flags, we will warn only when the types of the
2969 shadowing variable (i.e. new_decl) and the shadowed variable
2970 (old_decl) are compatible. */
2972 warning_code
= OPT_Wshadow
;
2973 else if (comptypes (TREE_TYPE (old_decl
), TREE_TYPE (new_decl
)))
2974 warning_code
= OPT_Wshadow_compatible_local
;
2976 warning_code
= OPT_Wshadow_local
;
2977 warned
= warning_at (DECL_SOURCE_LOCATION (new_decl
), warning_code
,
2978 "declaration of %qD shadows a parameter",
2981 else if (DECL_FILE_SCOPE_P (old_decl
))
2983 /* Do not warn if a variable shadows a function, unless
2984 the variable is a function or a pointer-to-function. */
2985 if (TREE_CODE (old_decl
) == FUNCTION_DECL
2986 && TREE_CODE (new_decl
) != FUNCTION_DECL
2987 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl
)))
2990 warned
= warning_at (DECL_SOURCE_LOCATION (new_decl
), OPT_Wshadow
,
2991 "declaration of %qD shadows a global "
2995 else if (TREE_CODE (old_decl
) == FUNCTION_DECL
2996 && fndecl_built_in_p (old_decl
))
2998 warning (OPT_Wshadow
, "declaration of %q+D shadows "
2999 "a built-in function", new_decl
);
3004 enum opt_code warning_code
;
3006 /* If '-Wshadow=compatible-local' is specified without other
3007 -Wshadow= flags, we will warn only when the types of the
3008 shadowing variable (i.e. new_decl) and the shadowed variable
3009 (old_decl) are compatible. */
3011 warning_code
= OPT_Wshadow
;
3012 else if (comptypes (TREE_TYPE (old_decl
), TREE_TYPE (new_decl
)))
3013 warning_code
= OPT_Wshadow_compatible_local
;
3015 warning_code
= OPT_Wshadow_local
;
3016 warned
= warning_at (DECL_SOURCE_LOCATION (new_decl
), warning_code
,
3017 "declaration of %qD shadows a previous local",
3022 inform (DECL_SOURCE_LOCATION (old_decl
),
3023 "shadowed declaration is here");
3029 /* Record a decl-node X as belonging to the current lexical scope.
3030 Check for errors (such as an incompatible declaration for the same
3031 name already seen in the same scope).
3033 Returns either X or an old decl for the same name.
3034 If an old decl is returned, it may have been smashed
3035 to agree with what X says. */
3040 tree name
= DECL_NAME (x
);
3041 struct c_scope
*scope
= current_scope
;
3042 struct c_binding
*b
;
3043 bool nested
= false;
3044 location_t locus
= DECL_SOURCE_LOCATION (x
);
3046 /* Must set DECL_CONTEXT for everything not at file scope or
3047 DECL_FILE_SCOPE_P won't work. Local externs don't count
3048 unless they have initializers (which generate code). */
3049 if (current_function_decl
3050 && (!VAR_OR_FUNCTION_DECL_P (x
)
3051 || DECL_INITIAL (x
) || !DECL_EXTERNAL (x
)))
3052 DECL_CONTEXT (x
) = current_function_decl
;
3054 /* Anonymous decls are just inserted in the scope. */
3057 bind (name
, x
, scope
, /*invisible=*/false, /*nested=*/false,
3062 /* First, see if there is another declaration with the same name in
3063 the current scope. If there is, duplicate_decls may do all the
3064 work for us. If duplicate_decls returns false, that indicates
3065 two incompatible decls in the same scope; we are to silently
3066 replace the old one (duplicate_decls has issued all appropriate
3067 diagnostics). In particular, we should not consider possible
3068 duplicates in the external scope, or shadowing. */
3069 b
= I_SYMBOL_BINDING (name
);
3070 if (b
&& B_IN_SCOPE (b
, scope
))
3072 struct c_binding
*b_ext
, *b_use
;
3073 tree type
= TREE_TYPE (x
);
3074 tree visdecl
= b
->decl
;
3075 tree vistype
= TREE_TYPE (visdecl
);
3076 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
3077 && COMPLETE_TYPE_P (TREE_TYPE (x
)))
3078 b
->inner_comp
= false;
3081 /* If this is an external linkage declaration, we should check
3082 for compatibility with the type in the external scope before
3083 setting the type at this scope based on the visible
3084 information only. */
3085 if (TREE_PUBLIC (x
) && TREE_PUBLIC (visdecl
))
3087 while (b_ext
&& !B_IN_EXTERNAL_SCOPE (b_ext
))
3088 b_ext
= b_ext
->shadowed
;
3093 TREE_TYPE (b_use
->decl
) = b_use
->u
.type
;
3096 if (duplicate_decls (x
, b_use
->decl
))
3100 /* Save the updated type in the external scope and
3101 restore the proper type for this scope. */
3103 if (comptypes (vistype
, type
))
3104 thistype
= composite_type (vistype
, type
);
3106 thistype
= TREE_TYPE (b_use
->decl
);
3107 b_use
->u
.type
= TREE_TYPE (b_use
->decl
);
3108 if (TREE_CODE (b_use
->decl
) == FUNCTION_DECL
3109 && fndecl_built_in_p (b_use
->decl
))
3111 = build_type_attribute_variant (thistype
,
3114 TREE_TYPE (b_use
->decl
) = thistype
;
3119 goto skip_external_and_shadow_checks
;
3122 /* All declarations with external linkage, and all external
3123 references, go in the external scope, no matter what scope is
3124 current. However, the binding in that scope is ignored for
3125 purposes of normal name lookup. A separate binding structure is
3126 created in the requested scope; this governs the normal
3127 visibility of the symbol.
3129 The binding in the externals scope is used exclusively for
3130 detecting duplicate declarations of the same object, no matter
3131 what scope they are in; this is what we do here. (C99 6.2.7p2:
3132 All declarations that refer to the same object or function shall
3133 have compatible type; otherwise, the behavior is undefined.)
3134 However, in Objective-C, we also want to detect declarations
3135 conflicting with those of the basic types. */
3136 if ((DECL_EXTERNAL (x
) || scope
== file_scope
)
3137 && (VAR_OR_FUNCTION_DECL_P (x
) || c_dialect_objc ()))
3139 tree type
= TREE_TYPE (x
);
3140 tree vistype
= NULL_TREE
;
3141 tree visdecl
= NULL_TREE
;
3142 bool type_saved
= false;
3143 if (b
&& !B_IN_EXTERNAL_SCOPE (b
)
3144 && VAR_OR_FUNCTION_DECL_P (b
->decl
)
3145 && DECL_FILE_SCOPE_P (b
->decl
))
3148 vistype
= TREE_TYPE (visdecl
);
3150 if (scope
!= file_scope
3151 && !DECL_IN_SYSTEM_HEADER (x
))
3152 warning_at (locus
, OPT_Wnested_externs
,
3153 "nested extern declaration of %qD", x
);
3155 while (b
&& !B_IN_EXTERNAL_SCOPE (b
))
3157 /* If this decl might be modified, save its type. This is
3158 done here rather than when the decl is first bound
3159 because the type may change after first binding, through
3160 being completed or through attributes being added. If we
3161 encounter multiple such decls, only the first should have
3162 its type saved; the others will already have had their
3163 proper types saved and the types will not have changed as
3164 their scopes will not have been re-entered. */
3165 if (DECL_P (b
->decl
) && DECL_FILE_SCOPE_P (b
->decl
) && !type_saved
)
3167 b
->u
.type
= TREE_TYPE (b
->decl
);
3170 if (B_IN_FILE_SCOPE (b
)
3172 && TREE_STATIC (b
->decl
)
3173 && TREE_CODE (TREE_TYPE (b
->decl
)) == ARRAY_TYPE
3174 && !TYPE_DOMAIN (TREE_TYPE (b
->decl
))
3175 && TREE_CODE (type
) == ARRAY_TYPE
3176 && TYPE_DOMAIN (type
)
3177 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
3178 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))
3180 /* Array type completed in inner scope, which should be
3181 diagnosed if the completion does not have size 1 and
3182 it does not get completed in the file scope. */
3183 b
->inner_comp
= true;
3188 /* If a matching external declaration has been found, set its
3189 type to the composite of all the types of that declaration.
3190 After the consistency checks, it will be reset to the
3191 composite of the visible types only. */
3192 if (b
&& (TREE_PUBLIC (x
) || same_translation_unit_p (x
, b
->decl
))
3194 TREE_TYPE (b
->decl
) = b
->u
.type
;
3196 /* The point of the same_translation_unit_p check here is,
3197 we want to detect a duplicate decl for a construct like
3198 foo() { extern bar(); } ... static bar(); but not if
3199 they are in different translation units. In any case,
3200 the static does not go in the externals scope. */
3202 && (TREE_PUBLIC (x
) || same_translation_unit_p (x
, b
->decl
))
3203 && duplicate_decls (x
, b
->decl
))
3208 if (comptypes (vistype
, type
))
3209 thistype
= composite_type (vistype
, type
);
3211 thistype
= TREE_TYPE (b
->decl
);
3215 b
->u
.type
= TREE_TYPE (b
->decl
);
3216 if (TREE_CODE (b
->decl
) == FUNCTION_DECL
3217 && fndecl_built_in_p (b
->decl
))
3219 = build_type_attribute_variant (thistype
,
3220 TYPE_ATTRIBUTES (b
->u
.type
));
3221 TREE_TYPE (b
->decl
) = thistype
;
3222 bind (name
, b
->decl
, scope
, /*invisible=*/false, /*nested=*/true,
3226 else if (TREE_PUBLIC (x
))
3228 if (visdecl
&& !b
&& duplicate_decls (x
, visdecl
))
3230 /* An external declaration at block scope referring to a
3231 visible entity with internal linkage. The composite
3232 type will already be correct for this scope, so we
3233 just need to fall through to make the declaration in
3240 bind (name
, x
, external_scope
, /*invisible=*/true,
3241 /*nested=*/false, locus
);
3247 if (TREE_CODE (x
) != PARM_DECL
)
3248 warn_if_shadowing (x
);
3250 skip_external_and_shadow_checks
:
3251 if (TREE_CODE (x
) == TYPE_DECL
)
3253 /* So this is a typedef, set its underlying type. */
3254 set_underlying_type (x
);
3256 /* If X is a typedef defined in the current function, record it
3257 for the purpose of implementing the -Wunused-local-typedefs
3259 record_locally_defined_typedef (x
);
3262 bind (name
, x
, scope
, /*invisible=*/false, nested
, locus
);
3264 /* If x's type is incomplete because it's based on a
3265 structure or union which has not yet been fully declared,
3266 attach it to that structure or union type, so we can go
3267 back and complete the variable declaration later, if the
3268 structure or union gets fully declared.
3270 If the input is erroneous, we can have error_mark in the type
3271 slot (e.g. "f(void a, ...)") - that doesn't count as an
3273 if (TREE_TYPE (x
) != error_mark_node
3274 && !COMPLETE_TYPE_P (TREE_TYPE (x
)))
3276 tree element
= TREE_TYPE (x
);
3278 while (TREE_CODE (element
) == ARRAY_TYPE
)
3279 element
= TREE_TYPE (element
);
3280 element
= TYPE_MAIN_VARIANT (element
);
3282 if (RECORD_OR_UNION_TYPE_P (element
)
3283 && (TREE_CODE (x
) != TYPE_DECL
3284 || TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
)
3285 && !COMPLETE_TYPE_P (element
))
3286 C_TYPE_INCOMPLETE_VARS (element
)
3287 = tree_cons (NULL_TREE
, x
, C_TYPE_INCOMPLETE_VARS (element
));
3293 /* Issue a warning about implicit function declaration. ID is the function
3294 identifier, OLDDECL is a declaration of the function in a different scope,
3298 implicit_decl_warning (location_t loc
, tree id
, tree olddecl
)
3300 if (!warn_implicit_function_declaration
)
3304 auto_diagnostic_group d
;
3307 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_FUNCTION_NAME
, loc
);
3311 if (const char *suggestion
= hint
.suggestion ())
3313 gcc_rich_location
richloc (loc
);
3314 richloc
.add_fixit_replace (suggestion
);
3315 warned
= pedwarn (&richloc
, OPT_Wimplicit_function_declaration
,
3316 "implicit declaration of function %qE;"
3317 " did you mean %qs?",
3321 warned
= pedwarn (loc
, OPT_Wimplicit_function_declaration
,
3322 "implicit declaration of function %qE", id
);
3324 else if (const char *suggestion
= hint
.suggestion ())
3326 gcc_rich_location
richloc (loc
);
3327 richloc
.add_fixit_replace (suggestion
);
3329 (&richloc
, OPT_Wimplicit_function_declaration
,
3330 G_("implicit declaration of function %qE; did you mean %qs?"),
3334 warned
= warning_at (loc
, OPT_Wimplicit_function_declaration
,
3335 G_("implicit declaration of function %qE"), id
);
3337 if (olddecl
&& warned
)
3338 locate_old_decl (olddecl
);
3344 /* Return the name of the header file that declares built-in function
3345 FNDECL, or null if either we don't know or don't expect to see an
3346 explicit declaration. */
3349 header_for_builtin_fn (tree fndecl
)
3351 if (DECL_BUILT_IN_CLASS (fndecl
) != BUILT_IN_NORMAL
)
3354 switch (DECL_FUNCTION_CODE (fndecl
))
3356 CASE_FLT_FN (BUILT_IN_ACOS
):
3357 CASE_FLT_FN (BUILT_IN_ACOSH
):
3358 CASE_FLT_FN (BUILT_IN_ASIN
):
3359 CASE_FLT_FN (BUILT_IN_ASINH
):
3360 CASE_FLT_FN (BUILT_IN_ATAN
):
3361 CASE_FLT_FN (BUILT_IN_ATANH
):
3362 CASE_FLT_FN (BUILT_IN_ATAN2
):
3363 CASE_FLT_FN (BUILT_IN_CBRT
):
3364 CASE_FLT_FN (BUILT_IN_CEIL
):
3365 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL
):
3366 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
3367 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN
):
3368 CASE_FLT_FN (BUILT_IN_COS
):
3369 CASE_FLT_FN (BUILT_IN_COSH
):
3370 CASE_FLT_FN (BUILT_IN_ERF
):
3371 CASE_FLT_FN (BUILT_IN_ERFC
):
3372 CASE_FLT_FN (BUILT_IN_EXP
):
3373 CASE_FLT_FN (BUILT_IN_EXP2
):
3374 CASE_FLT_FN (BUILT_IN_EXPM1
):
3375 CASE_FLT_FN (BUILT_IN_FABS
):
3376 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS
):
3377 CASE_FLT_FN (BUILT_IN_FDIM
):
3378 CASE_FLT_FN (BUILT_IN_FLOOR
):
3379 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR
):
3380 CASE_FLT_FN (BUILT_IN_FMA
):
3381 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA
):
3382 CASE_FLT_FN (BUILT_IN_FMAX
):
3383 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX
):
3384 CASE_FLT_FN (BUILT_IN_FMIN
):
3385 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN
):
3386 CASE_FLT_FN (BUILT_IN_FMOD
):
3387 CASE_FLT_FN (BUILT_IN_FREXP
):
3388 CASE_FLT_FN (BUILT_IN_HYPOT
):
3389 CASE_FLT_FN (BUILT_IN_ILOGB
):
3390 CASE_FLT_FN (BUILT_IN_LDEXP
):
3391 CASE_FLT_FN (BUILT_IN_LGAMMA
):
3392 CASE_FLT_FN (BUILT_IN_LLRINT
):
3393 CASE_FLT_FN (BUILT_IN_LLROUND
):
3394 CASE_FLT_FN (BUILT_IN_LOG
):
3395 CASE_FLT_FN (BUILT_IN_LOG10
):
3396 CASE_FLT_FN (BUILT_IN_LOG1P
):
3397 CASE_FLT_FN (BUILT_IN_LOG2
):
3398 CASE_FLT_FN (BUILT_IN_LOGB
):
3399 CASE_FLT_FN (BUILT_IN_LRINT
):
3400 CASE_FLT_FN (BUILT_IN_LROUND
):
3401 CASE_FLT_FN (BUILT_IN_MODF
):
3402 CASE_FLT_FN (BUILT_IN_NAN
):
3403 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
3404 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT
):
3405 CASE_FLT_FN (BUILT_IN_NEXTAFTER
):
3406 CASE_FLT_FN (BUILT_IN_NEXTTOWARD
):
3407 CASE_FLT_FN (BUILT_IN_POW
):
3408 CASE_FLT_FN (BUILT_IN_REMAINDER
):
3409 CASE_FLT_FN (BUILT_IN_REMQUO
):
3410 CASE_FLT_FN (BUILT_IN_RINT
):
3411 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT
):
3412 CASE_FLT_FN (BUILT_IN_ROUND
):
3413 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND
):
3414 CASE_FLT_FN (BUILT_IN_SCALBLN
):
3415 CASE_FLT_FN (BUILT_IN_SCALBN
):
3416 CASE_FLT_FN (BUILT_IN_SIN
):
3417 CASE_FLT_FN (BUILT_IN_SINH
):
3418 CASE_FLT_FN (BUILT_IN_SINCOS
):
3419 CASE_FLT_FN (BUILT_IN_SQRT
):
3420 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT
):
3421 CASE_FLT_FN (BUILT_IN_TAN
):
3422 CASE_FLT_FN (BUILT_IN_TANH
):
3423 CASE_FLT_FN (BUILT_IN_TGAMMA
):
3424 CASE_FLT_FN (BUILT_IN_TRUNC
):
3425 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC
):
3426 case BUILT_IN_ISINF
:
3427 case BUILT_IN_ISNAN
:
3429 CASE_FLT_FN (BUILT_IN_CABS
):
3430 CASE_FLT_FN (BUILT_IN_CACOS
):
3431 CASE_FLT_FN (BUILT_IN_CACOSH
):
3432 CASE_FLT_FN (BUILT_IN_CARG
):
3433 CASE_FLT_FN (BUILT_IN_CASIN
):
3434 CASE_FLT_FN (BUILT_IN_CASINH
):
3435 CASE_FLT_FN (BUILT_IN_CATAN
):
3436 CASE_FLT_FN (BUILT_IN_CATANH
):
3437 CASE_FLT_FN (BUILT_IN_CCOS
):
3438 CASE_FLT_FN (BUILT_IN_CCOSH
):
3439 CASE_FLT_FN (BUILT_IN_CEXP
):
3440 CASE_FLT_FN (BUILT_IN_CIMAG
):
3441 CASE_FLT_FN (BUILT_IN_CLOG
):
3442 CASE_FLT_FN (BUILT_IN_CONJ
):
3443 CASE_FLT_FN (BUILT_IN_CPOW
):
3444 CASE_FLT_FN (BUILT_IN_CPROJ
):
3445 CASE_FLT_FN (BUILT_IN_CREAL
):
3446 CASE_FLT_FN (BUILT_IN_CSIN
):
3447 CASE_FLT_FN (BUILT_IN_CSINH
):
3448 CASE_FLT_FN (BUILT_IN_CSQRT
):
3449 CASE_FLT_FN (BUILT_IN_CTAN
):
3450 CASE_FLT_FN (BUILT_IN_CTANH
):
3451 return "<complex.h>";
3452 case BUILT_IN_MEMCHR
:
3453 case BUILT_IN_MEMCMP
:
3454 case BUILT_IN_MEMCPY
:
3455 case BUILT_IN_MEMMOVE
:
3456 case BUILT_IN_MEMSET
:
3457 case BUILT_IN_STRCAT
:
3458 case BUILT_IN_STRCHR
:
3459 case BUILT_IN_STRCMP
:
3460 case BUILT_IN_STRCPY
:
3461 case BUILT_IN_STRCSPN
:
3462 case BUILT_IN_STRLEN
:
3463 case BUILT_IN_STRNCAT
:
3464 case BUILT_IN_STRNCMP
:
3465 case BUILT_IN_STRNCPY
:
3466 case BUILT_IN_STRPBRK
:
3467 case BUILT_IN_STRRCHR
:
3468 case BUILT_IN_STRSPN
:
3469 case BUILT_IN_STRSTR
:
3470 return "<string.h>";
3471 case BUILT_IN_FPRINTF
:
3473 case BUILT_IN_FPUTC
:
3474 case BUILT_IN_FPUTS
:
3475 case BUILT_IN_FSCANF
:
3476 case BUILT_IN_FWRITE
:
3477 case BUILT_IN_PRINTF
:
3478 case BUILT_IN_PUTCHAR
:
3480 case BUILT_IN_SCANF
:
3481 case BUILT_IN_SNPRINTF
:
3482 case BUILT_IN_SPRINTF
:
3483 case BUILT_IN_SSCANF
:
3484 case BUILT_IN_VFPRINTF
:
3485 case BUILT_IN_VFSCANF
:
3486 case BUILT_IN_VPRINTF
:
3487 case BUILT_IN_VSCANF
:
3488 case BUILT_IN_VSNPRINTF
:
3489 case BUILT_IN_VSPRINTF
:
3490 case BUILT_IN_VSSCANF
:
3492 case BUILT_IN_ISALNUM
:
3493 case BUILT_IN_ISALPHA
:
3494 case BUILT_IN_ISBLANK
:
3495 case BUILT_IN_ISCNTRL
:
3496 case BUILT_IN_ISDIGIT
:
3497 case BUILT_IN_ISGRAPH
:
3498 case BUILT_IN_ISLOWER
:
3499 case BUILT_IN_ISPRINT
:
3500 case BUILT_IN_ISPUNCT
:
3501 case BUILT_IN_ISSPACE
:
3502 case BUILT_IN_ISUPPER
:
3503 case BUILT_IN_ISXDIGIT
:
3504 case BUILT_IN_TOLOWER
:
3505 case BUILT_IN_TOUPPER
:
3507 case BUILT_IN_ISWALNUM
:
3508 case BUILT_IN_ISWALPHA
:
3509 case BUILT_IN_ISWBLANK
:
3510 case BUILT_IN_ISWCNTRL
:
3511 case BUILT_IN_ISWDIGIT
:
3512 case BUILT_IN_ISWGRAPH
:
3513 case BUILT_IN_ISWLOWER
:
3514 case BUILT_IN_ISWPRINT
:
3515 case BUILT_IN_ISWPUNCT
:
3516 case BUILT_IN_ISWSPACE
:
3517 case BUILT_IN_ISWUPPER
:
3518 case BUILT_IN_ISWXDIGIT
:
3519 case BUILT_IN_TOWLOWER
:
3520 case BUILT_IN_TOWUPPER
:
3521 return "<wctype.h>";
3522 case BUILT_IN_ABORT
:
3524 case BUILT_IN_CALLOC
:
3528 case BUILT_IN_LLABS
:
3529 case BUILT_IN_MALLOC
:
3530 case BUILT_IN_REALLOC
:
3531 case BUILT_IN__EXIT2
:
3532 case BUILT_IN_ALIGNED_ALLOC
:
3533 return "<stdlib.h>";
3534 case BUILT_IN_IMAXABS
:
3535 return "<inttypes.h>";
3536 case BUILT_IN_STRFTIME
:
3543 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3544 function of type int (). */
3547 implicitly_declare (location_t loc
, tree functionid
)
3549 struct c_binding
*b
;
3550 tree decl
= NULL_TREE
;
3553 for (b
= I_SYMBOL_BINDING (functionid
); b
; b
= b
->shadowed
)
3555 if (B_IN_SCOPE (b
, external_scope
))
3564 if (TREE_CODE (decl
) != FUNCTION_DECL
)
3567 /* FIXME: Objective-C has weird not-really-builtin functions
3568 which are supposed to be visible automatically. They wind up
3569 in the external scope because they're pushed before the file
3570 scope gets created. Catch this here and rebind them into the
3572 if (!fndecl_built_in_p (decl
) && DECL_IS_BUILTIN (decl
))
3574 bind (functionid
, decl
, file_scope
,
3575 /*invisible=*/false, /*nested=*/true,
3576 DECL_SOURCE_LOCATION (decl
));
3581 tree newtype
= default_function_type
;
3583 TREE_TYPE (decl
) = b
->u
.type
;
3584 /* Implicit declaration of a function already declared
3585 (somehow) in a different scope, or as a built-in.
3586 If this is the first time this has happened, warn;
3587 then recycle the old declaration but with the new type. */
3588 if (!C_DECL_IMPLICIT (decl
))
3590 implicit_decl_warning (loc
, functionid
, decl
);
3591 C_DECL_IMPLICIT (decl
) = 1;
3593 if (fndecl_built_in_p (decl
))
3595 newtype
= build_type_attribute_variant (newtype
,
3597 (TREE_TYPE (decl
)));
3598 if (!comptypes (newtype
, TREE_TYPE (decl
)))
3600 bool warned
= warning_at (loc
, 0, "incompatible implicit "
3601 "declaration of built-in "
3602 "function %qD", decl
);
3603 /* See if we can hint which header to include. */
3604 const char *header
= header_for_builtin_fn (decl
);
3605 if (header
!= NULL
&& warned
)
3607 rich_location
richloc (line_table
, loc
);
3608 maybe_add_include_fixit (&richloc
, header
, true);
3610 "include %qs or provide a declaration of %qD",
3613 newtype
= TREE_TYPE (decl
);
3618 if (!comptypes (newtype
, TREE_TYPE (decl
)))
3620 auto_diagnostic_group d
;
3621 error_at (loc
, "incompatible implicit declaration of "
3622 "function %qD", decl
);
3623 locate_old_decl (decl
);
3626 b
->u
.type
= TREE_TYPE (decl
);
3627 TREE_TYPE (decl
) = newtype
;
3628 bind (functionid
, decl
, current_scope
,
3629 /*invisible=*/false, /*nested=*/true,
3630 DECL_SOURCE_LOCATION (decl
));
3635 /* Not seen before. */
3636 decl
= build_decl (loc
, FUNCTION_DECL
, functionid
, default_function_type
);
3637 DECL_EXTERNAL (decl
) = 1;
3638 TREE_PUBLIC (decl
) = 1;
3639 C_DECL_IMPLICIT (decl
) = 1;
3640 implicit_decl_warning (loc
, functionid
, 0);
3641 asmspec_tree
= maybe_apply_renaming_pragma (decl
, /*asmname=*/NULL
);
3643 set_user_assembler_name (decl
, TREE_STRING_POINTER (asmspec_tree
));
3645 /* C89 says implicit declarations are in the innermost block.
3646 So we record the decl in the standard fashion. */
3647 decl
= pushdecl (decl
);
3649 /* No need to call objc_check_decl here - it's a function type. */
3650 rest_of_decl_compilation (decl
, 0, 0);
3652 /* Write a record describing this implicit function declaration
3653 to the prototypes file (if requested). */
3654 gen_aux_info_record (decl
, 0, 1, 0);
3656 /* Possibly apply some default attributes to this implicit declaration. */
3657 decl_attributes (&decl
, NULL_TREE
, 0);
3662 /* Issue an error message for a reference to an undeclared variable
3663 ID, including a reference to a builtin outside of function-call
3664 context. Establish a binding of the identifier to error_mark_node
3665 in an appropriate scope, which will suppress further errors for the
3666 same identifier. The error message should be given location LOC. */
3668 undeclared_variable (location_t loc
, tree id
)
3670 static bool already
= false;
3671 struct c_scope
*scope
;
3673 auto_diagnostic_group d
;
3674 if (current_function_decl
== NULL_TREE
)
3676 name_hint guessed_id
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_NAME
, loc
);
3677 if (const char *suggestion
= guessed_id
.suggestion ())
3679 gcc_rich_location
richloc (loc
);
3680 richloc
.add_fixit_replace (suggestion
);
3682 "%qE undeclared here (not in a function);"
3683 " did you mean %qs?",
3687 error_at (loc
, "%qE undeclared here (not in a function)", id
);
3688 scope
= current_scope
;
3692 if (!objc_diagnose_private_ivar (id
))
3694 name_hint guessed_id
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_NAME
, loc
);
3695 if (const char *suggestion
= guessed_id
.suggestion ())
3697 gcc_rich_location
richloc (loc
);
3698 richloc
.add_fixit_replace (suggestion
);
3700 "%qE undeclared (first use in this function);"
3701 " did you mean %qs?",
3705 error_at (loc
, "%qE undeclared (first use in this function)", id
);
3709 inform (loc
, "each undeclared identifier is reported only"
3710 " once for each function it appears in");
3714 /* If we are parsing old-style parameter decls, current_function_decl
3715 will be nonnull but current_function_scope will be null. */
3716 scope
= current_function_scope
? current_function_scope
: current_scope
;
3718 bind (id
, error_mark_node
, scope
, /*invisible=*/false, /*nested=*/false,
3722 /* Subroutine of lookup_label, declare_label, define_label: construct a
3723 LABEL_DECL with all the proper frills. Also create a struct
3724 c_label_vars initialized for the current scope. */
3727 make_label (location_t location
, tree name
, bool defining
,
3728 struct c_label_vars
**p_label_vars
)
3730 tree label
= build_decl (location
, LABEL_DECL
, name
, void_type_node
);
3731 DECL_CONTEXT (label
) = current_function_decl
;
3732 SET_DECL_MODE (label
, VOIDmode
);
3734 c_label_vars
*label_vars
= ggc_alloc
<c_label_vars
> ();
3735 label_vars
->shadowed
= NULL
;
3736 set_spot_bindings (&label_vars
->label_bindings
, defining
);
3737 label_vars
->decls_in_scope
= make_tree_vector ();
3738 label_vars
->gotos
= NULL
;
3739 *p_label_vars
= label_vars
;
3744 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3745 Create one if none exists so far for the current function.
3746 This is called when a label is used in a goto expression or
3747 has its address taken. */
3750 lookup_label (tree name
)
3753 struct c_label_vars
*label_vars
;
3755 if (current_function_scope
== 0)
3757 error ("label %qE referenced outside of any function", name
);
3761 /* Use a label already defined or ref'd with this name, but not if
3762 it is inherited from a containing function and wasn't declared
3764 label
= I_LABEL_DECL (name
);
3765 if (label
&& (DECL_CONTEXT (label
) == current_function_decl
3766 || C_DECLARED_LABEL_FLAG (label
)))
3768 /* If the label has only been declared, update its apparent
3769 location to point here, for better diagnostics if it
3770 turns out not to have been defined. */
3771 if (DECL_INITIAL (label
) == NULL_TREE
)
3772 DECL_SOURCE_LOCATION (label
) = input_location
;
3776 /* No label binding for that identifier; make one. */
3777 label
= make_label (input_location
, name
, false, &label_vars
);
3779 /* Ordinary labels go in the current function scope. */
3780 bind_label (name
, label
, current_function_scope
, label_vars
);
3785 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3789 warn_about_goto (location_t goto_loc
, tree label
, tree decl
)
3791 if (variably_modified_type_p (TREE_TYPE (decl
), NULL_TREE
))
3793 "jump into scope of identifier with variably modified type");
3795 warning_at (goto_loc
, OPT_Wjump_misses_init
,
3796 "jump skips variable initialization");
3797 inform (DECL_SOURCE_LOCATION (label
), "label %qD defined here", label
);
3798 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3801 /* Look up a label because of a goto statement. This is like
3802 lookup_label, but also issues any appropriate warnings. */
3805 lookup_label_for_goto (location_t loc
, tree name
)
3808 struct c_label_vars
*label_vars
;
3812 label
= lookup_label (name
);
3813 if (label
== NULL_TREE
)
3816 /* If we are jumping to a different function, we can't issue any
3818 if (DECL_CONTEXT (label
) != current_function_decl
)
3820 gcc_assert (C_DECLARED_LABEL_FLAG (label
));
3824 label_vars
= I_LABEL_BINDING (name
)->u
.label
;
3826 /* If the label has not yet been defined, then push this goto on a
3827 list for possible later warnings. */
3828 if (label_vars
->label_bindings
.scope
== NULL
)
3830 c_goto_bindings
*g
= ggc_alloc
<c_goto_bindings
> ();
3833 set_spot_bindings (&g
->goto_bindings
, true);
3834 vec_safe_push (label_vars
->gotos
, g
);
3838 /* If there are any decls in label_vars->decls_in_scope, then this
3839 goto has missed the declaration of the decl. This happens for a
3845 Issue a warning or error. */
3846 FOR_EACH_VEC_SAFE_ELT (label_vars
->decls_in_scope
, ix
, decl
)
3847 warn_about_goto (loc
, label
, decl
);
3849 if (label_vars
->label_bindings
.left_stmt_expr
)
3851 error_at (loc
, "jump into statement expression");
3852 inform (DECL_SOURCE_LOCATION (label
), "label %qD defined here", label
);
3858 /* Make a label named NAME in the current function, shadowing silently
3859 any that may be inherited from containing functions or containing
3860 scopes. This is called for __label__ declarations. */
3863 declare_label (tree name
)
3865 struct c_binding
*b
= I_LABEL_BINDING (name
);
3867 struct c_label_vars
*label_vars
;
3869 /* Check to make sure that the label hasn't already been declared
3871 if (b
&& B_IN_CURRENT_SCOPE (b
))
3873 auto_diagnostic_group d
;
3874 error ("duplicate label declaration %qE", name
);
3875 locate_old_decl (b
->decl
);
3877 /* Just use the previous declaration. */
3881 label
= make_label (input_location
, name
, false, &label_vars
);
3882 C_DECLARED_LABEL_FLAG (label
) = 1;
3884 /* Declared labels go in the current scope. */
3885 bind_label (name
, label
, current_scope
, label_vars
);
3890 /* When we define a label, issue any appropriate warnings if there are
3891 any gotos earlier in the function which jump to this label. */
3894 check_earlier_gotos (tree label
, struct c_label_vars
* label_vars
)
3897 struct c_goto_bindings
*g
;
3899 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
3901 struct c_binding
*b
;
3902 struct c_scope
*scope
;
3904 /* We have a goto to this label. The goto is going forward. In
3905 g->scope, the goto is going to skip any binding which was
3906 defined after g->bindings_in_scope. */
3907 if (g
->goto_bindings
.scope
->has_jump_unsafe_decl
)
3909 for (b
= g
->goto_bindings
.scope
->bindings
;
3910 b
!= g
->goto_bindings
.bindings_in_scope
;
3913 if (decl_jump_unsafe (b
->decl
))
3914 warn_about_goto (g
->loc
, label
, b
->decl
);
3918 /* We also need to warn about decls defined in any scopes
3919 between the scope of the label and the scope of the goto. */
3920 for (scope
= label_vars
->label_bindings
.scope
;
3921 scope
!= g
->goto_bindings
.scope
;
3922 scope
= scope
->outer
)
3924 gcc_assert (scope
!= NULL
);
3925 if (scope
->has_jump_unsafe_decl
)
3927 if (scope
== label_vars
->label_bindings
.scope
)
3928 b
= label_vars
->label_bindings
.bindings_in_scope
;
3930 b
= scope
->bindings
;
3931 for (; b
!= NULL
; b
= b
->prev
)
3933 if (decl_jump_unsafe (b
->decl
))
3934 warn_about_goto (g
->loc
, label
, b
->decl
);
3939 if (g
->goto_bindings
.stmt_exprs
> 0)
3941 error_at (g
->loc
, "jump into statement expression");
3942 inform (DECL_SOURCE_LOCATION (label
), "label %qD defined here",
3947 /* Now that the label is defined, we will issue warnings about
3948 subsequent gotos to this label when we see them. */
3949 vec_safe_truncate (label_vars
->gotos
, 0);
3950 label_vars
->gotos
= NULL
;
3953 /* Define a label, specifying the location in the source file.
3954 Return the LABEL_DECL node for the label, if the definition is valid.
3955 Otherwise return NULL_TREE. */
3958 define_label (location_t location
, tree name
)
3960 /* Find any preexisting label with this name. It is an error
3961 if that label has already been defined in this function, or
3962 if there is a containing function with a declared label with
3964 tree label
= I_LABEL_DECL (name
);
3967 && ((DECL_CONTEXT (label
) == current_function_decl
3968 && DECL_INITIAL (label
) != NULL_TREE
)
3969 || (DECL_CONTEXT (label
) != current_function_decl
3970 && C_DECLARED_LABEL_FLAG (label
))))
3972 auto_diagnostic_group d
;
3973 error_at (location
, "duplicate label %qD", label
);
3974 locate_old_decl (label
);
3977 else if (label
&& DECL_CONTEXT (label
) == current_function_decl
)
3979 struct c_label_vars
*label_vars
= I_LABEL_BINDING (name
)->u
.label
;
3981 /* The label has been used or declared already in this function,
3982 but not defined. Update its location to point to this
3984 DECL_SOURCE_LOCATION (label
) = location
;
3985 set_spot_bindings (&label_vars
->label_bindings
, true);
3987 /* Issue warnings as required about any goto statements from
3988 earlier in the function. */
3989 check_earlier_gotos (label
, label_vars
);
3993 struct c_label_vars
*label_vars
;
3995 /* No label binding for that identifier; make one. */
3996 label
= make_label (location
, name
, true, &label_vars
);
3998 /* Ordinary labels go in the current function scope. */
3999 bind_label (name
, label
, current_function_scope
, label_vars
);
4002 if (!in_system_header_at (input_location
) && lookup_name (name
))
4003 warning_at (location
, OPT_Wtraditional
,
4004 "traditional C lacks a separate namespace "
4005 "for labels, identifier %qE conflicts", name
);
4007 /* Mark label as having been defined. */
4008 DECL_INITIAL (label
) = error_mark_node
;
4012 /* Get the bindings for a new switch statement. This is used to issue
4013 warnings as appropriate for jumps from the switch to case or
4016 struct c_spot_bindings
*
4017 c_get_switch_bindings (void)
4019 struct c_spot_bindings
*switch_bindings
;
4021 switch_bindings
= XNEW (struct c_spot_bindings
);
4022 set_spot_bindings (switch_bindings
, true);
4023 return switch_bindings
;
4027 c_release_switch_bindings (struct c_spot_bindings
*bindings
)
4029 gcc_assert (bindings
->stmt_exprs
== 0 && !bindings
->left_stmt_expr
);
4033 /* This is called at the point of a case or default label to issue
4034 warnings about decls as needed. It returns true if it found an
4035 error, not just a warning. */
4038 c_check_switch_jump_warnings (struct c_spot_bindings
*switch_bindings
,
4039 location_t switch_loc
, location_t case_loc
)
4042 struct c_scope
*scope
;
4045 for (scope
= current_scope
;
4046 scope
!= switch_bindings
->scope
;
4047 scope
= scope
->outer
)
4049 struct c_binding
*b
;
4051 gcc_assert (scope
!= NULL
);
4053 if (!scope
->has_jump_unsafe_decl
)
4056 for (b
= scope
->bindings
; b
!= NULL
; b
= b
->prev
)
4058 if (decl_jump_unsafe (b
->decl
))
4060 if (variably_modified_type_p (TREE_TYPE (b
->decl
), NULL_TREE
))
4064 ("switch jumps into scope of identifier with "
4065 "variably modified type"));
4068 warning_at (case_loc
, OPT_Wjump_misses_init
,
4069 "switch jumps over variable initialization");
4070 inform (switch_loc
, "switch starts here");
4071 inform (DECL_SOURCE_LOCATION (b
->decl
), "%qD declared here",
4077 if (switch_bindings
->stmt_exprs
> 0)
4080 error_at (case_loc
, "switch jumps into statement expression");
4081 inform (switch_loc
, "switch starts here");
4087 /* Given NAME, an IDENTIFIER_NODE,
4088 return the structure (or union or enum) definition for that name.
4089 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4090 CODE says which kind of type the caller wants;
4091 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4092 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4093 location where the tag was defined.
4094 If the wrong kind of type is found, an error is reported. */
4097 lookup_tag (enum tree_code code
, tree name
, bool thislevel_only
,
4100 struct c_binding
*b
= I_TAG_BINDING (name
);
4101 bool thislevel
= false;
4106 /* We only care about whether it's in this level if
4107 thislevel_only was set or it might be a type clash. */
4108 if (thislevel_only
|| TREE_CODE (b
->decl
) != code
)
4110 /* For our purposes, a tag in the external scope is the same as
4111 a tag in the file scope. (Primarily relevant to Objective-C
4112 and its builtin structure tags, which get pushed before the
4113 file scope is created.) */
4114 if (B_IN_CURRENT_SCOPE (b
)
4115 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
4119 if (thislevel_only
&& !thislevel
)
4122 if (TREE_CODE (b
->decl
) != code
)
4124 /* Definition isn't the kind we were looking for. */
4125 pending_invalid_xref
= name
;
4126 pending_invalid_xref_location
= input_location
;
4128 /* If in the same binding level as a declaration as a tag
4129 of a different type, this must not be allowed to
4130 shadow that tag, so give the error immediately.
4131 (For example, "struct foo; union foo;" is invalid.) */
4133 pending_xref_error ();
4142 /* Return true if a definition exists for NAME with code CODE. */
4145 tag_exists_p (enum tree_code code
, tree name
)
4147 struct c_binding
*b
= I_TAG_BINDING (name
);
4149 if (b
== NULL
|| b
->decl
== NULL_TREE
)
4151 return TREE_CODE (b
->decl
) == code
;
4154 /* Print an error message now
4155 for a recent invalid struct, union or enum cross reference.
4156 We don't print them immediately because they are not invalid
4157 when used in the `struct foo;' construct for shadowing. */
4160 pending_xref_error (void)
4162 if (pending_invalid_xref
!= NULL_TREE
)
4163 error_at (pending_invalid_xref_location
, "%qE defined as wrong kind of tag",
4164 pending_invalid_xref
);
4165 pending_invalid_xref
= NULL_TREE
;
4169 /* Look up NAME in the current scope and its superiors
4170 in the namespace of variables, functions and typedefs.
4171 Return a ..._DECL node of some kind representing its definition,
4172 or return NULL_TREE if it is undefined. */
4175 lookup_name (tree name
)
4177 struct c_binding
*b
= I_SYMBOL_BINDING (name
);
4178 if (b
&& !b
->invisible
)
4180 maybe_record_typedef_use (b
->decl
);
4186 /* Similar to `lookup_name' but look only at the indicated scope. */
4189 lookup_name_in_scope (tree name
, struct c_scope
*scope
)
4191 struct c_binding
*b
;
4193 for (b
= I_SYMBOL_BINDING (name
); b
; b
= b
->shadowed
)
4194 if (B_IN_SCOPE (b
, scope
))
4199 /* Look for the closest match for NAME within the currently valid
4202 This finds the identifier with the lowest Levenshtein distance to
4203 NAME. If there are multiple candidates with equal minimal distance,
4204 the first one found is returned. Scopes are searched from innermost
4205 outwards, and within a scope in reverse order of declaration, thus
4206 benefiting candidates "near" to the current scope.
4208 The function also looks for similar macro names to NAME, since a
4209 misspelled macro name will not be expanded, and hence looks like an
4210 identifier to the C frontend.
4212 It also looks for start_typename keywords, to detect "singed" vs "signed"
4215 Use LOC for any deferred diagnostics. */
4218 lookup_name_fuzzy (tree name
, enum lookup_name_fuzzy_kind kind
, location_t loc
)
4220 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
4222 /* First, try some well-known names in the C standard library, in case
4223 the user forgot a #include. */
4224 const char *header_hint
4225 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name
));
4228 return name_hint (NULL
,
4229 new suggest_missing_header (loc
,
4230 IDENTIFIER_POINTER (name
),
4233 /* Only suggest names reserved for the implementation if NAME begins
4234 with an underscore. */
4235 bool consider_implementation_names
= (IDENTIFIER_POINTER (name
)[0] == '_');
4237 best_match
<tree
, tree
> bm (name
);
4239 /* Look within currently valid scopes. */
4240 for (c_scope
*scope
= current_scope
; scope
; scope
= scope
->outer
)
4241 for (c_binding
*binding
= scope
->bindings
; binding
; binding
= binding
->prev
)
4243 if (!binding
->id
|| binding
->invisible
)
4245 if (binding
->decl
== error_mark_node
)
4247 /* Don't use bindings from implicitly declared functions,
4248 as they were likely misspellings themselves. */
4249 if (TREE_CODE (binding
->decl
) == FUNCTION_DECL
)
4250 if (C_DECL_IMPLICIT (binding
->decl
))
4252 /* Don't suggest names that are reserved for use by the
4253 implementation, unless NAME began with an underscore. */
4254 if (!consider_implementation_names
)
4256 const char *suggestion_str
= IDENTIFIER_POINTER (binding
->id
);
4257 if (name_reserved_for_implementation_p (suggestion_str
))
4262 case FUZZY_LOOKUP_TYPENAME
:
4263 if (TREE_CODE (binding
->decl
) != TYPE_DECL
)
4267 case FUZZY_LOOKUP_FUNCTION_NAME
:
4268 if (TREE_CODE (binding
->decl
) != FUNCTION_DECL
)
4270 /* Allow function pointers. */
4271 if ((VAR_P (binding
->decl
)
4272 || TREE_CODE (binding
->decl
) == PARM_DECL
)
4273 && TREE_CODE (TREE_TYPE (binding
->decl
)) == POINTER_TYPE
4274 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding
->decl
)))
4284 bm
.consider (binding
->id
);
4287 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4289 x = SOME_OTHER_MACRO (y);
4290 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4291 as a misspelled identifier.
4293 Use the best distance so far so that a candidate is only set if
4294 a macro is better than anything so far. This allows early rejection
4295 (without calculating the edit distance) of macro names that must have
4296 distance >= bm.get_best_distance (), and means that we only get a
4297 non-NULL result for best_macro_match if it's better than any of
4298 the identifiers already checked, which avoids needless creation
4299 of identifiers for macro hashnodes. */
4300 best_macro_match
bmm (name
, bm
.get_best_distance (), parse_in
);
4301 cpp_hashnode
*best_macro
= bmm
.get_best_meaningful_candidate ();
4302 /* If a macro is the closest so far to NAME, use it, creating an
4303 identifier tree node for it. */
4306 const char *id
= (const char *)best_macro
->ident
.str
;
4307 tree macro_as_identifier
4308 = get_identifier_with_length (id
, best_macro
->ident
.len
);
4309 bm
.set_best_so_far (macro_as_identifier
,
4310 bmm
.get_best_distance (),
4311 bmm
.get_best_candidate_length ());
4314 /* Try the "start_typename" keywords to detect
4315 "singed" vs "signed" typos. */
4316 if (kind
== FUZZY_LOOKUP_TYPENAME
)
4318 for (unsigned i
= 0; i
< num_c_common_reswords
; i
++)
4320 const c_common_resword
*resword
= &c_common_reswords
[i
];
4321 if (!c_keyword_starts_typename (resword
->rid
))
4323 tree resword_identifier
= ridpointers
[resword
->rid
];
4324 if (!resword_identifier
)
4326 gcc_assert (TREE_CODE (resword_identifier
) == IDENTIFIER_NODE
);
4327 bm
.consider (resword_identifier
);
4331 tree best
= bm
.get_best_meaningful_candidate ();
4333 return name_hint (IDENTIFIER_POINTER (best
), NULL
);
4335 return name_hint (NULL
, NULL
);
4339 /* Create the predefined scalar types of C,
4340 and some nodes representing standard constants (0, 1, (void *) 0).
4341 Initialize the global scope.
4342 Make definitions for built-in primitive functions. */
4345 c_init_decl_processing (void)
4347 location_t save_loc
= input_location
;
4349 /* Initialize reserved words for parser. */
4352 current_function_decl
= NULL_TREE
;
4354 gcc_obstack_init (&parser_obstack
);
4356 /* Make the externals scope. */
4358 external_scope
= current_scope
;
4360 /* Declarations from c_common_nodes_and_builtins must not be associated
4361 with this input file, lest we get differences between using and not
4362 using preprocessed headers. */
4363 input_location
= BUILTINS_LOCATION
;
4365 c_common_nodes_and_builtins ();
4367 /* In C, comparisons and TRUTH_* expressions have type int. */
4368 truthvalue_type_node
= integer_type_node
;
4369 truthvalue_true_node
= integer_one_node
;
4370 truthvalue_false_node
= integer_zero_node
;
4372 /* Even in C99, which has a real boolean type. */
4373 pushdecl (build_decl (UNKNOWN_LOCATION
, TYPE_DECL
, get_identifier ("_Bool"),
4374 boolean_type_node
));
4376 input_location
= save_loc
;
4378 make_fname_decl
= c_make_fname_decl
;
4379 start_fname_decls ();
4382 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4383 give the decl, NAME is the initialization string and TYPE_DEP
4384 indicates whether NAME depended on the type of the function. As we
4385 don't yet implement delayed emission of static data, we mark the
4386 decl as emitted so it is not placed in the output. Anything using
4387 it must therefore pull out the STRING_CST initializer directly.
4391 c_make_fname_decl (location_t loc
, tree id
, int type_dep
)
4393 const char *name
= fname_as_string (type_dep
);
4394 tree decl
, type
, init
;
4395 size_t length
= strlen (name
);
4397 type
= build_array_type (char_type_node
,
4398 build_index_type (size_int (length
)));
4399 type
= c_build_qualified_type (type
, TYPE_QUAL_CONST
);
4401 decl
= build_decl (loc
, VAR_DECL
, id
, type
);
4403 TREE_STATIC (decl
) = 1;
4404 TREE_READONLY (decl
) = 1;
4405 DECL_ARTIFICIAL (decl
) = 1;
4407 init
= build_string (length
+ 1, name
);
4408 free (CONST_CAST (char *, name
));
4409 TREE_TYPE (init
) = type
;
4410 DECL_INITIAL (decl
) = init
;
4412 TREE_USED (decl
) = 1;
4414 if (current_function_decl
4415 /* For invalid programs like this:
4418 const char* p = __FUNCTION__;
4420 the __FUNCTION__ is believed to appear in K&R style function
4421 parameter declarator. In that case we still don't have
4423 && current_function_scope
)
4425 DECL_CONTEXT (decl
) = current_function_decl
;
4426 bind (id
, decl
, current_function_scope
,
4427 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
4430 finish_decl (decl
, loc
, init
, NULL_TREE
, NULL_TREE
);
4436 c_builtin_function (tree decl
)
4438 tree type
= TREE_TYPE (decl
);
4439 tree id
= DECL_NAME (decl
);
4441 const char *name
= IDENTIFIER_POINTER (id
);
4442 C_DECL_BUILTIN_PROTOTYPE (decl
) = prototype_p (type
);
4444 /* Should never be called on a symbol with a preexisting meaning. */
4445 gcc_assert (!I_SYMBOL_BINDING (id
));
4447 bind (id
, decl
, external_scope
, /*invisible=*/true, /*nested=*/false,
4450 /* Builtins in the implementation namespace are made visible without
4451 needing to be explicitly declared. See push_file_scope. */
4452 if (name
[0] == '_' && (name
[1] == '_' || ISUPPER (name
[1])))
4454 DECL_CHAIN (decl
) = visible_builtins
;
4455 visible_builtins
= decl
;
4462 c_builtin_function_ext_scope (tree decl
)
4464 tree type
= TREE_TYPE (decl
);
4465 tree id
= DECL_NAME (decl
);
4467 const char *name
= IDENTIFIER_POINTER (id
);
4468 C_DECL_BUILTIN_PROTOTYPE (decl
) = prototype_p (type
);
4471 bind (id
, decl
, external_scope
, /*invisible=*/false, /*nested=*/false,
4474 /* Builtins in the implementation namespace are made visible without
4475 needing to be explicitly declared. See push_file_scope. */
4476 if (name
[0] == '_' && (name
[1] == '_' || ISUPPER (name
[1])))
4478 DECL_CHAIN (decl
) = visible_builtins
;
4479 visible_builtins
= decl
;
4485 /* Called when a declaration is seen that contains no names to declare.
4486 If its type is a reference to a structure, union or enum inherited
4487 from a containing scope, shadow that tag name for the current scope
4488 with a forward reference.
4489 If its type defines a new named structure or union
4490 or defines an enum, it is valid but we need not do anything here.
4491 Otherwise, it is an error. */
4494 shadow_tag (const struct c_declspecs
*declspecs
)
4496 shadow_tag_warned (declspecs
, 0);
4499 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4502 shadow_tag_warned (const struct c_declspecs
*declspecs
, int warned
)
4504 bool found_tag
= false;
4506 if (declspecs
->type
&& !declspecs
->default_int_p
&& !declspecs
->typedef_p
)
4508 tree value
= declspecs
->type
;
4509 enum tree_code code
= TREE_CODE (value
);
4511 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
|| code
== ENUMERAL_TYPE
)
4512 /* Used to test also that TYPE_SIZE (value) != 0.
4513 That caused warning for `struct foo;' at top level in the file. */
4515 tree name
= TYPE_NAME (value
);
4520 if (declspecs
->restrict_p
)
4522 error ("invalid use of %<restrict%>");
4526 if (name
== NULL_TREE
)
4528 if (warned
!= 1 && code
!= ENUMERAL_TYPE
)
4529 /* Empty unnamed enum OK */
4531 pedwarn (input_location
, 0,
4532 "unnamed struct/union that defines no instances");
4536 else if (declspecs
->typespec_kind
!= ctsk_tagdef
4537 && declspecs
->typespec_kind
!= ctsk_tagfirstref
4538 && declspecs
->storage_class
!= csc_none
)
4541 pedwarn (input_location
, 0,
4542 "empty declaration with storage class specifier "
4543 "does not redeclare tag");
4545 pending_xref_error ();
4547 else if (declspecs
->typespec_kind
!= ctsk_tagdef
4548 && declspecs
->typespec_kind
!= ctsk_tagfirstref
4549 && (declspecs
->const_p
4550 || declspecs
->volatile_p
4551 || declspecs
->atomic_p
4552 || declspecs
->restrict_p
4553 || declspecs
->address_space
))
4556 pedwarn (input_location
, 0,
4557 "empty declaration with type qualifier "
4558 "does not redeclare tag");
4560 pending_xref_error ();
4562 else if (declspecs
->typespec_kind
!= ctsk_tagdef
4563 && declspecs
->typespec_kind
!= ctsk_tagfirstref
4564 && declspecs
->alignas_p
)
4567 pedwarn (input_location
, 0,
4568 "empty declaration with %<_Alignas%> "
4569 "does not redeclare tag");
4571 pending_xref_error ();
4575 pending_invalid_xref
= NULL_TREE
;
4576 t
= lookup_tag (code
, name
, true, NULL
);
4580 t
= make_node (code
);
4581 pushtag (input_location
, name
, t
);
4587 if (warned
!= 1 && !in_system_header_at (input_location
))
4589 pedwarn (input_location
, 0,
4590 "useless type name in empty declaration");
4595 else if (warned
!= 1 && !in_system_header_at (input_location
)
4596 && declspecs
->typedef_p
)
4598 pedwarn (input_location
, 0, "useless type name in empty declaration");
4602 pending_invalid_xref
= NULL_TREE
;
4604 if (declspecs
->inline_p
)
4606 error ("%<inline%> in empty declaration");
4610 if (declspecs
->noreturn_p
)
4612 error ("%<_Noreturn%> in empty declaration");
4616 if (current_scope
== file_scope
&& declspecs
->storage_class
== csc_auto
)
4618 error ("%<auto%> in file-scope empty declaration");
4622 if (current_scope
== file_scope
&& declspecs
->storage_class
== csc_register
)
4624 error ("%<register%> in file-scope empty declaration");
4628 if (!warned
&& !in_system_header_at (input_location
)
4629 && declspecs
->storage_class
!= csc_none
)
4631 warning (0, "useless storage class specifier in empty declaration");
4635 if (!warned
&& !in_system_header_at (input_location
) && declspecs
->thread_p
)
4637 warning (0, "useless %qs in empty declaration",
4638 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
4643 && !in_system_header_at (input_location
)
4644 && (declspecs
->const_p
4645 || declspecs
->volatile_p
4646 || declspecs
->atomic_p
4647 || declspecs
->restrict_p
4648 || declspecs
->address_space
))
4650 warning (0, "useless type qualifier in empty declaration");
4654 if (!warned
&& !in_system_header_at (input_location
)
4655 && declspecs
->alignas_p
)
4657 warning (0, "useless %<_Alignas%> in empty declaration");
4664 pedwarn (input_location
, 0, "empty declaration");
4669 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4670 bits. SPECS represents declaration specifiers that the grammar
4671 only permits to contain type qualifiers and attributes. */
4674 quals_from_declspecs (const struct c_declspecs
*specs
)
4676 int quals
= ((specs
->const_p
? TYPE_QUAL_CONST
: 0)
4677 | (specs
->volatile_p
? TYPE_QUAL_VOLATILE
: 0)
4678 | (specs
->restrict_p
? TYPE_QUAL_RESTRICT
: 0)
4679 | (specs
->atomic_p
? TYPE_QUAL_ATOMIC
: 0)
4680 | (ENCODE_QUAL_ADDR_SPACE (specs
->address_space
)));
4681 gcc_assert (!specs
->type
4682 && !specs
->decl_attr
4683 && specs
->typespec_word
== cts_none
4684 && specs
->storage_class
== csc_none
4685 && !specs
->typedef_p
4686 && !specs
->explicit_signed_p
4687 && !specs
->deprecated_p
4689 && !specs
->long_long_p
4692 && !specs
->unsigned_p
4693 && !specs
->complex_p
4695 && !specs
->noreturn_p
4696 && !specs
->thread_p
);
4700 /* Construct an array declarator. LOC is the location of the
4701 beginning of the array (usually the opening brace). EXPR is the
4702 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4703 inside the [] (to be applied to the pointer to which a parameter
4704 array is converted). STATIC_P is true if "static" is inside the
4705 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4706 VLA of unspecified length which is nevertheless a complete type,
4707 false otherwise. The field for the contained declarator is left to
4708 be filled in by set_array_declarator_inner. */
4710 struct c_declarator
*
4711 build_array_declarator (location_t loc
,
4712 tree expr
, struct c_declspecs
*quals
, bool static_p
,
4715 struct c_declarator
*declarator
= XOBNEW (&parser_obstack
,
4716 struct c_declarator
);
4717 declarator
->id_loc
= loc
;
4718 declarator
->kind
= cdk_array
;
4719 declarator
->declarator
= 0;
4720 declarator
->u
.array
.dimen
= expr
;
4723 declarator
->u
.array
.attrs
= quals
->attrs
;
4724 declarator
->u
.array
.quals
= quals_from_declspecs (quals
);
4728 declarator
->u
.array
.attrs
= NULL_TREE
;
4729 declarator
->u
.array
.quals
= 0;
4731 declarator
->u
.array
.static_p
= static_p
;
4732 declarator
->u
.array
.vla_unspec_p
= vla_unspec_p
;
4733 if (static_p
|| quals
!= NULL
)
4734 pedwarn_c90 (loc
, OPT_Wpedantic
,
4735 "ISO C90 does not support %<static%> or type "
4736 "qualifiers in parameter array declarators");
4738 pedwarn_c90 (loc
, OPT_Wpedantic
,
4739 "ISO C90 does not support %<[*]%> array declarators");
4742 if (!current_scope
->parm_flag
)
4745 error_at (loc
, "%<[*]%> not allowed in other than "
4746 "function prototype scope");
4747 declarator
->u
.array
.vla_unspec_p
= false;
4750 current_scope
->had_vla_unspec
= true;
4755 /* Set the contained declarator of an array declarator. DECL is the
4756 declarator, as constructed by build_array_declarator; INNER is what
4757 appears on the left of the []. */
4759 struct c_declarator
*
4760 set_array_declarator_inner (struct c_declarator
*decl
,
4761 struct c_declarator
*inner
)
4763 decl
->declarator
= inner
;
4767 /* INIT is a constructor that forms DECL's initializer. If the final
4768 element initializes a flexible array field, add the size of that
4769 initializer to DECL's size. */
4772 add_flexible_array_elts_to_size (tree decl
, tree init
)
4776 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
4779 elt
= CONSTRUCTOR_ELTS (init
)->last ().value
;
4780 type
= TREE_TYPE (elt
);
4781 if (TREE_CODE (type
) == ARRAY_TYPE
4782 && TYPE_SIZE (type
) == NULL_TREE
4783 && TYPE_DOMAIN (type
) != NULL_TREE
4784 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
)
4786 complete_array_type (&type
, elt
, false);
4788 = size_binop (PLUS_EXPR
, DECL_SIZE (decl
), TYPE_SIZE (type
));
4789 DECL_SIZE_UNIT (decl
)
4790 = size_binop (PLUS_EXPR
, DECL_SIZE_UNIT (decl
), TYPE_SIZE_UNIT (type
));
4794 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4795 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4796 before the type name, and set *EXPR_CONST_OPERANDS, if
4797 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4798 appear in a constant expression. */
4801 groktypename (struct c_type_name
*type_name
, tree
*expr
,
4802 bool *expr_const_operands
)
4805 tree attrs
= type_name
->specs
->attrs
;
4807 type_name
->specs
->attrs
= NULL_TREE
;
4809 type
= grokdeclarator (type_name
->declarator
, type_name
->specs
, TYPENAME
,
4810 false, NULL
, &attrs
, expr
, expr_const_operands
,
4813 /* Apply attributes. */
4814 decl_attributes (&type
, attrs
, 0);
4819 /* Wrapper for decl_attributes that adds some implicit attributes
4820 to VAR_DECLs or FUNCTION_DECLs. */
4823 c_decl_attributes (tree
*node
, tree attributes
, int flags
)
4825 /* Add implicit "omp declare target" attribute if requested. */
4826 if (current_omp_declare_target_attribute
4827 && ((VAR_P (*node
) && is_global_var (*node
))
4828 || TREE_CODE (*node
) == FUNCTION_DECL
))
4831 && !lang_hooks
.types
.omp_mappable_type (TREE_TYPE (*node
)))
4832 attributes
= tree_cons (get_identifier ("omp declare target implicit"),
4833 NULL_TREE
, attributes
);
4835 attributes
= tree_cons (get_identifier ("omp declare target"),
4836 NULL_TREE
, attributes
);
4839 /* Look up the current declaration with all the attributes merged
4840 so far so that attributes on the current declaration that's
4841 about to be pushed that conflict with the former can be detected,
4842 diagnosed, and rejected as appropriate. */
4843 tree last_decl
= lookup_name (DECL_NAME (*node
));
4845 last_decl
= lookup_name_in_scope (DECL_NAME (*node
), external_scope
);
4847 return decl_attributes (node
, attributes
, flags
, last_decl
);
4851 /* Decode a declarator in an ordinary declaration or data definition.
4852 This is called as soon as the type information and variable name
4853 have been parsed, before parsing the initializer if any.
4854 Here we create the ..._DECL node, fill in its type,
4855 and put it on the list of decls for the current context.
4856 The ..._DECL node is returned as the value.
4858 Exception: for arrays where the length is not specified,
4859 the type is left null, to be filled in by `finish_decl'.
4861 Function definitions do not come here; they go to start_function
4862 instead. However, external and forward declarations of functions
4863 do go through here. Structure field declarations are done by
4864 grokfield and not through here. */
4867 start_decl (struct c_declarator
*declarator
, struct c_declspecs
*declspecs
,
4868 bool initialized
, tree attributes
)
4872 tree expr
= NULL_TREE
;
4873 enum deprecated_states deprecated_state
= DEPRECATED_NORMAL
;
4875 /* An object declared as __attribute__((deprecated)) suppresses
4876 warnings of uses of other deprecated items. */
4877 if (lookup_attribute ("deprecated", attributes
))
4878 deprecated_state
= DEPRECATED_SUPPRESS
;
4880 decl
= grokdeclarator (declarator
, declspecs
,
4881 NORMAL
, initialized
, NULL
, &attributes
, &expr
, NULL
,
4883 if (!decl
|| decl
== error_mark_node
)
4887 add_stmt (fold_convert (void_type_node
, expr
));
4889 if (TREE_CODE (decl
) != FUNCTION_DECL
&& MAIN_NAME_P (DECL_NAME (decl
))
4890 && TREE_PUBLIC (decl
))
4891 warning (OPT_Wmain
, "%q+D is usually a function", decl
);
4894 /* Is it valid for this decl to have an initializer at all?
4895 If not, set INITIALIZED to zero, which will indirectly
4896 tell 'finish_decl' to ignore the initializer once it is parsed. */
4897 switch (TREE_CODE (decl
))
4900 error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl
);
4901 initialized
= false;
4905 error ("function %qD is initialized like a variable", decl
);
4906 initialized
= false;
4910 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4911 error ("parameter %qD is initialized", decl
);
4912 initialized
= false;
4916 /* Don't allow initializations for incomplete types except for
4917 arrays which might be completed by the initialization. */
4919 /* This can happen if the array size is an undefined macro.
4920 We already gave a warning, so we don't need another one. */
4921 if (TREE_TYPE (decl
) == error_mark_node
)
4922 initialized
= false;
4923 else if (COMPLETE_TYPE_P (TREE_TYPE (decl
)))
4925 /* A complete type is ok if size is fixed. */
4927 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl
))) != INTEGER_CST
4928 || C_DECL_VARIABLE_SIZE (decl
))
4930 error ("variable-sized object may not be initialized");
4931 initialized
= false;
4934 else if (TREE_CODE (TREE_TYPE (decl
)) != ARRAY_TYPE
)
4936 error ("variable %qD has initializer but incomplete type", decl
);
4937 initialized
= false;
4939 else if (C_DECL_VARIABLE_SIZE (decl
))
4941 /* Although C99 is unclear about whether incomplete arrays
4942 of VLAs themselves count as VLAs, it does not make
4943 sense to permit them to be initialized given that
4944 ordinary VLAs may not be initialized. */
4945 error ("variable-sized object may not be initialized");
4946 initialized
= false;
4952 if (current_scope
== file_scope
)
4953 TREE_STATIC (decl
) = 1;
4955 /* Tell 'pushdecl' this is an initialized decl
4956 even though we don't yet have the initializer expression.
4957 Also tell 'finish_decl' it may store the real initializer. */
4958 DECL_INITIAL (decl
) = error_mark_node
;
4961 /* If this is a function declaration, write a record describing it to the
4962 prototypes file (if requested). */
4964 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4965 gen_aux_info_record (decl
, 0, 0, prototype_p (TREE_TYPE (decl
)));
4967 /* ANSI specifies that a tentative definition which is not merged with
4968 a non-tentative definition behaves exactly like a definition with an
4969 initializer equal to zero. (Section 3.7.2)
4971 -fno-common gives strict ANSI behavior, though this tends to break
4972 a large body of code that grew up without this rule.
4974 Thread-local variables are never common, since there's no entrenched
4975 body of code to break, and it allows more efficient variable references
4976 in the presence of dynamic linking. */
4980 && TREE_PUBLIC (decl
)
4981 && !DECL_THREAD_LOCAL_P (decl
)
4983 DECL_COMMON (decl
) = 1;
4985 /* Set attributes here so if duplicate decl, will have proper attributes. */
4986 c_decl_attributes (&decl
, attributes
, 0);
4988 /* Handle gnu_inline attribute. */
4989 if (declspecs
->inline_p
4990 && !flag_gnu89_inline
4991 && TREE_CODE (decl
) == FUNCTION_DECL
4992 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl
))
4993 || current_function_decl
))
4995 if (declspecs
->storage_class
== csc_auto
&& current_scope
!= file_scope
)
4997 else if (declspecs
->storage_class
!= csc_static
)
4998 DECL_EXTERNAL (decl
) = !DECL_EXTERNAL (decl
);
5001 if (TREE_CODE (decl
) == FUNCTION_DECL
5002 && targetm
.calls
.promote_prototypes (TREE_TYPE (decl
)))
5004 struct c_declarator
*ce
= declarator
;
5006 if (ce
->kind
== cdk_pointer
)
5007 ce
= declarator
->declarator
;
5008 if (ce
->kind
== cdk_function
)
5010 tree args
= ce
->u
.arg_info
->parms
;
5011 for (; args
; args
= DECL_CHAIN (args
))
5013 tree type
= TREE_TYPE (args
);
5014 if (type
&& INTEGRAL_TYPE_P (type
)
5015 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
5016 DECL_ARG_TYPE (args
) = c_type_promotes_to (type
);
5021 if (TREE_CODE (decl
) == FUNCTION_DECL
5022 && DECL_DECLARED_INLINE_P (decl
)
5023 && DECL_UNINLINABLE (decl
)
5024 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl
)))
5025 warning (OPT_Wattributes
, "inline function %q+D given attribute %qs",
5028 /* C99 6.7.4p3: An inline definition of a function with external
5029 linkage shall not contain a definition of a modifiable object
5030 with static storage duration... */
5032 && current_scope
!= file_scope
5033 && TREE_STATIC (decl
)
5034 && !TREE_READONLY (decl
)
5035 && DECL_DECLARED_INLINE_P (current_function_decl
)
5036 && DECL_EXTERNAL (current_function_decl
))
5037 record_inline_static (input_location
, current_function_decl
,
5038 decl
, csi_modifiable
);
5040 if (c_dialect_objc ()
5041 && VAR_OR_FUNCTION_DECL_P (decl
))
5042 objc_check_global_decl (decl
);
5044 /* Add this decl to the current scope.
5045 TEM may equal DECL or it may be a previous decl of the same name. */
5046 tem
= pushdecl (decl
);
5048 if (initialized
&& DECL_EXTERNAL (tem
))
5050 DECL_EXTERNAL (tem
) = 0;
5051 TREE_STATIC (tem
) = 1;
5057 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5058 DECL or the non-array element type if DECL is an uninitialized array.
5059 If that type has a const member, diagnose this. */
5062 diagnose_uninitialized_cst_member (tree decl
, tree type
)
5065 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
5068 if (TREE_CODE (field
) != FIELD_DECL
)
5070 field_type
= strip_array_types (TREE_TYPE (field
));
5072 if (TYPE_QUALS (field_type
) & TYPE_QUAL_CONST
)
5074 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
5075 "uninitialized const member in %qT is invalid in C++",
5076 strip_array_types (TREE_TYPE (decl
)));
5077 inform (DECL_SOURCE_LOCATION (field
), "%qD should be initialized", field
);
5080 if (RECORD_OR_UNION_TYPE_P (field_type
))
5081 diagnose_uninitialized_cst_member (decl
, field_type
);
5085 /* Finish processing of a declaration;
5086 install its initial value.
5087 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5088 If the length of an array type is not known before,
5089 it must be determined now, from the initial value, or it is an error.
5091 INIT_LOC is the location of the initial value. */
5094 finish_decl (tree decl
, location_t init_loc
, tree init
,
5095 tree origtype
, tree asmspec_tree
)
5098 bool was_incomplete
= (DECL_SIZE (decl
) == NULL_TREE
);
5099 const char *asmspec
= 0;
5101 /* If a name was specified, get the string. */
5102 if (VAR_OR_FUNCTION_DECL_P (decl
)
5103 && DECL_FILE_SCOPE_P (decl
))
5104 asmspec_tree
= maybe_apply_renaming_pragma (decl
, asmspec_tree
);
5106 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
5109 && TREE_STATIC (decl
)
5110 && global_bindings_p ())
5111 /* So decl is a global variable. Record the types it uses
5112 so that we can decide later to emit debug info for them. */
5113 record_types_used_by_current_var_decl (decl
);
5115 /* If `start_decl' didn't like having an initialization, ignore it now. */
5116 if (init
!= NULL_TREE
&& DECL_INITIAL (decl
) == NULL_TREE
)
5119 /* Don't crash if parm is initialized. */
5120 if (TREE_CODE (decl
) == PARM_DECL
)
5124 store_init_value (init_loc
, decl
, init
, origtype
);
5126 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl
)
5127 || TREE_CODE (decl
) == FIELD_DECL
))
5128 objc_check_decl (decl
);
5130 type
= TREE_TYPE (decl
);
5132 /* Deduce size of array from initialization, if not already known.
5133 This is only needed for an initialization in the current scope;
5134 it must not be done for a file-scope initialization of a
5135 declaration with external linkage, redeclared in an inner scope
5136 with the outer declaration shadowed in an intermediate scope. */
5137 if (TREE_CODE (type
) == ARRAY_TYPE
5138 && TYPE_DOMAIN (type
) == NULL_TREE
5139 && TREE_CODE (decl
) != TYPE_DECL
5140 && !(TREE_PUBLIC (decl
) && current_scope
!= file_scope
))
5143 = (TREE_STATIC (decl
)
5144 /* Even if pedantic, an external linkage array
5145 may have incomplete type at first. */
5146 ? pedantic
&& !TREE_PUBLIC (decl
)
5147 : !DECL_EXTERNAL (decl
));
5149 = complete_array_type (&TREE_TYPE (decl
), DECL_INITIAL (decl
),
5152 /* Get the completed type made by complete_array_type. */
5153 type
= TREE_TYPE (decl
);
5158 error ("initializer fails to determine size of %q+D", decl
);
5163 error ("array size missing in %q+D", decl
);
5167 error ("zero or negative size array %q+D", decl
);
5171 /* For global variables, update the copy of the type that
5172 exists in the binding. */
5173 if (TREE_PUBLIC (decl
))
5175 struct c_binding
*b_ext
= I_SYMBOL_BINDING (DECL_NAME (decl
));
5176 while (b_ext
&& !B_IN_EXTERNAL_SCOPE (b_ext
))
5177 b_ext
= b_ext
->shadowed
;
5178 if (b_ext
&& TREE_CODE (decl
) == TREE_CODE (b_ext
->decl
))
5180 if (b_ext
->u
.type
&& comptypes (b_ext
->u
.type
, type
))
5181 b_ext
->u
.type
= composite_type (b_ext
->u
.type
, type
);
5183 b_ext
->u
.type
= type
;
5192 if (DECL_INITIAL (decl
))
5193 TREE_TYPE (DECL_INITIAL (decl
)) = type
;
5195 relayout_decl (decl
);
5198 /* Look for braced array initializers for character arrays and
5199 recursively convert them into STRING_CSTs. */
5200 if (tree init
= DECL_INITIAL (decl
))
5201 DECL_INITIAL (decl
) = braced_lists_to_strings (type
, init
);
5205 if (init
&& TREE_CODE (init
) == CONSTRUCTOR
)
5206 add_flexible_array_elts_to_size (decl
, init
);
5208 complete_flexible_array_elts (DECL_INITIAL (decl
));
5210 if (DECL_SIZE (decl
) == NULL_TREE
&& TREE_TYPE (decl
) != error_mark_node
5211 && COMPLETE_TYPE_P (TREE_TYPE (decl
)))
5212 layout_decl (decl
, 0);
5214 if (DECL_SIZE (decl
) == NULL_TREE
5215 /* Don't give an error if we already gave one earlier. */
5216 && TREE_TYPE (decl
) != error_mark_node
5217 && (TREE_STATIC (decl
)
5218 /* A static variable with an incomplete type
5219 is an error if it is initialized.
5220 Also if it is not file scope.
5221 Otherwise, let it through, but if it is not `extern'
5222 then it may cause an error message later. */
5223 ? (DECL_INITIAL (decl
) != NULL_TREE
5224 || !DECL_FILE_SCOPE_P (decl
))
5225 /* An automatic variable with an incomplete type
5227 : !DECL_EXTERNAL (decl
)))
5229 error ("storage size of %q+D isn%'t known", decl
);
5230 TREE_TYPE (decl
) = error_mark_node
;
5233 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
))
5234 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
5235 && DECL_SIZE (decl
) == NULL_TREE
5236 && TREE_STATIC (decl
))
5237 incomplete_record_decls
.safe_push (decl
);
5239 if (is_global_var (decl
) && DECL_SIZE (decl
) != NULL_TREE
)
5241 if (TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
)
5242 constant_expression_warning (DECL_SIZE (decl
));
5245 error ("storage size of %q+D isn%'t constant", decl
);
5246 TREE_TYPE (decl
) = error_mark_node
;
5250 if (TREE_USED (type
))
5252 TREE_USED (decl
) = 1;
5253 DECL_READ_P (decl
) = 1;
5257 /* If this is a function and an assembler name is specified, reset DECL_RTL
5258 so we can give it its new name. Also, update builtin_decl if it
5259 was a normal built-in. */
5260 if (TREE_CODE (decl
) == FUNCTION_DECL
&& asmspec
)
5262 if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
5263 set_builtin_user_assembler_name (decl
, asmspec
);
5264 set_user_assembler_name (decl
, asmspec
);
5267 /* If #pragma weak was used, mark the decl weak now. */
5268 maybe_apply_pragma_weak (decl
);
5270 /* Output the assembler code and/or RTL code for variables and functions,
5271 unless the type is an undefined structure or union.
5272 If not, it will get done when the type is completed. */
5274 if (VAR_OR_FUNCTION_DECL_P (decl
))
5276 /* Determine the ELF visibility. */
5277 if (TREE_PUBLIC (decl
))
5278 c_determine_visibility (decl
);
5280 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5281 if (c_dialect_objc ())
5282 objc_check_decl (decl
);
5286 /* If this is not a static variable, issue a warning.
5287 It doesn't make any sense to give an ASMSPEC for an
5288 ordinary, non-register local variable. Historically,
5289 GCC has accepted -- but ignored -- the ASMSPEC in
5291 if (!DECL_FILE_SCOPE_P (decl
)
5293 && !C_DECL_REGISTER (decl
)
5294 && !TREE_STATIC (decl
))
5295 warning (0, "ignoring %<asm%> specifier for non-static local "
5296 "variable %q+D", decl
);
5298 set_user_assembler_name (decl
, asmspec
);
5301 if (DECL_FILE_SCOPE_P (decl
))
5303 if (DECL_INITIAL (decl
) == NULL_TREE
5304 || DECL_INITIAL (decl
) == error_mark_node
)
5305 /* Don't output anything
5306 when a tentative file-scope definition is seen.
5307 But at end of compilation, do output code for them. */
5308 DECL_DEFER_OUTPUT (decl
) = 1;
5309 if (asmspec
&& VAR_P (decl
) && C_DECL_REGISTER (decl
))
5310 DECL_HARD_REGISTER (decl
) = 1;
5311 rest_of_decl_compilation (decl
, true, 0);
5315 /* In conjunction with an ASMSPEC, the `register'
5316 keyword indicates that we should place the variable
5317 in a particular register. */
5318 if (asmspec
&& C_DECL_REGISTER (decl
))
5320 DECL_HARD_REGISTER (decl
) = 1;
5321 /* This cannot be done for a structure with volatile
5322 fields, on which DECL_REGISTER will have been
5324 if (!DECL_REGISTER (decl
))
5325 error ("cannot put object with volatile field into register");
5328 if (TREE_CODE (decl
) != FUNCTION_DECL
)
5330 /* If we're building a variable sized type, and we might be
5331 reachable other than via the top of the current binding
5332 level, then create a new BIND_EXPR so that we deallocate
5333 the object at the right time. */
5334 /* Note that DECL_SIZE can be null due to errors. */
5335 if (DECL_SIZE (decl
)
5336 && !TREE_CONSTANT (DECL_SIZE (decl
))
5337 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list
))
5340 bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, NULL
);
5341 TREE_SIDE_EFFECTS (bind
) = 1;
5343 BIND_EXPR_BODY (bind
) = push_stmt_list ();
5345 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl
),
5351 if (!DECL_FILE_SCOPE_P (decl
))
5353 /* Recompute the RTL of a local array now
5354 if it used to be an incomplete type. */
5355 if (was_incomplete
&& !is_global_var (decl
))
5357 /* If we used it already as memory, it must stay in memory. */
5358 TREE_ADDRESSABLE (decl
) = TREE_USED (decl
);
5359 /* If it's still incomplete now, no init will save it. */
5360 if (DECL_SIZE (decl
) == NULL_TREE
)
5361 DECL_INITIAL (decl
) = NULL_TREE
;
5366 if (TREE_CODE (decl
) == TYPE_DECL
)
5368 if (!DECL_FILE_SCOPE_P (decl
)
5369 && variably_modified_type_p (TREE_TYPE (decl
), NULL_TREE
))
5370 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl
), DECL_EXPR
, decl
));
5372 rest_of_decl_compilation (decl
, DECL_FILE_SCOPE_P (decl
), 0);
5375 /* Install a cleanup (aka destructor) if one was given. */
5376 if (VAR_P (decl
) && !TREE_STATIC (decl
))
5378 tree attr
= lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl
));
5381 tree cleanup_id
= TREE_VALUE (TREE_VALUE (attr
));
5382 tree cleanup_decl
= lookup_name (cleanup_id
);
5384 vec
<tree
, va_gc
> *v
;
5386 /* Build "cleanup(&decl)" for the destructor. */
5387 cleanup
= build_unary_op (input_location
, ADDR_EXPR
, decl
, false);
5389 v
->quick_push (cleanup
);
5390 cleanup
= c_build_function_call_vec (DECL_SOURCE_LOCATION (decl
),
5391 vNULL
, cleanup_decl
, v
, NULL
);
5394 /* Don't warn about decl unused; the cleanup uses it. */
5395 TREE_USED (decl
) = 1;
5396 TREE_USED (cleanup_decl
) = 1;
5397 DECL_READ_P (decl
) = 1;
5399 push_cleanup (decl
, cleanup
, false);
5405 && !DECL_EXTERNAL (decl
)
5406 && DECL_INITIAL (decl
) == NULL_TREE
)
5408 type
= strip_array_types (type
);
5409 if (TREE_READONLY (decl
))
5410 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
5411 "uninitialized %<const %D%> is invalid in C++", decl
);
5412 else if (RECORD_OR_UNION_TYPE_P (type
)
5413 && C_TYPE_FIELDS_READONLY (type
))
5414 diagnose_uninitialized_cst_member (decl
, type
);
5419 && lookup_attribute ("omp declare target implicit",
5420 DECL_ATTRIBUTES (decl
)))
5422 DECL_ATTRIBUTES (decl
)
5423 = remove_attribute ("omp declare target implicit",
5424 DECL_ATTRIBUTES (decl
));
5425 if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (decl
)))
5426 error ("%q+D in declare target directive does not have mappable type",
5428 else if (!lookup_attribute ("omp declare target",
5429 DECL_ATTRIBUTES (decl
))
5430 && !lookup_attribute ("omp declare target link",
5431 DECL_ATTRIBUTES (decl
)))
5432 DECL_ATTRIBUTES (decl
)
5433 = tree_cons (get_identifier ("omp declare target"),
5434 NULL_TREE
, DECL_ATTRIBUTES (decl
));
5437 invoke_plugin_callbacks (PLUGIN_FINISH_DECL
, decl
);
5440 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5441 EXPR is NULL or a pointer to an expression that needs to be
5442 evaluated for the side effects of array size expressions in the
5446 grokparm (const struct c_parm
*parm
, tree
*expr
)
5448 tree attrs
= parm
->attrs
;
5449 tree decl
= grokdeclarator (parm
->declarator
, parm
->specs
, PARM
, false,
5450 NULL
, &attrs
, expr
, NULL
, DEPRECATED_NORMAL
);
5452 decl_attributes (&decl
, attrs
, 0);
5457 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5458 and push that on the current scope. EXPR is a pointer to an
5459 expression that needs to be evaluated for the side effects of array
5460 size expressions in the parameters. */
5463 push_parm_decl (const struct c_parm
*parm
, tree
*expr
)
5465 tree attrs
= parm
->attrs
;
5468 decl
= grokdeclarator (parm
->declarator
, parm
->specs
, PARM
, false, NULL
,
5469 &attrs
, expr
, NULL
, DEPRECATED_NORMAL
);
5470 if (decl
&& DECL_P (decl
))
5471 DECL_SOURCE_LOCATION (decl
) = parm
->loc
;
5472 decl_attributes (&decl
, attrs
, 0);
5474 decl
= pushdecl (decl
);
5476 finish_decl (decl
, input_location
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
5479 /* Mark all the parameter declarations to date as forward decls.
5480 Also diagnose use of this extension. */
5483 mark_forward_parm_decls (void)
5485 struct c_binding
*b
;
5487 if (pedantic
&& !current_scope
->warned_forward_parm_decls
)
5489 pedwarn (input_location
, OPT_Wpedantic
,
5490 "ISO C forbids forward parameter declarations");
5491 current_scope
->warned_forward_parm_decls
= true;
5494 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
5495 if (TREE_CODE (b
->decl
) == PARM_DECL
)
5496 TREE_ASM_WRITTEN (b
->decl
) = 1;
5499 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5500 literal, which may be an incomplete array type completed by the
5501 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5502 literal. NON_CONST is true if the initializers contain something
5503 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
5504 it is the (valid) alignment for this compound literal, as specified
5508 build_compound_literal (location_t loc
, tree type
, tree init
, bool non_const
,
5509 unsigned int alignas_align
)
5511 /* We do not use start_decl here because we have a type, not a declarator;
5512 and do not use finish_decl because the decl should be stored inside
5513 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5518 if (type
== error_mark_node
5519 || init
== error_mark_node
)
5520 return error_mark_node
;
5522 decl
= build_decl (loc
, VAR_DECL
, NULL_TREE
, type
);
5523 DECL_EXTERNAL (decl
) = 0;
5524 TREE_PUBLIC (decl
) = 0;
5525 TREE_STATIC (decl
) = (current_scope
== file_scope
);
5526 DECL_CONTEXT (decl
) = current_function_decl
;
5527 TREE_USED (decl
) = 1;
5528 DECL_READ_P (decl
) = 1;
5529 DECL_ARTIFICIAL (decl
) = 1;
5530 DECL_IGNORED_P (decl
) = 1;
5531 C_DECL_COMPOUND_LITERAL_P (decl
) = 1;
5532 TREE_TYPE (decl
) = type
;
5533 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type
)), decl
);
5536 SET_DECL_ALIGN (decl
, alignas_align
* BITS_PER_UNIT
);
5537 DECL_USER_ALIGN (decl
) = 1;
5539 store_init_value (loc
, decl
, init
, NULL_TREE
);
5541 if (TREE_CODE (type
) == ARRAY_TYPE
&& !COMPLETE_TYPE_P (type
))
5543 int failure
= complete_array_type (&TREE_TYPE (decl
),
5544 DECL_INITIAL (decl
), true);
5545 /* If complete_array_type returns 3, it means that the
5546 initial value of the compound literal is empty. Allow it. */
5547 gcc_assert (failure
== 0 || failure
== 3);
5549 type
= TREE_TYPE (decl
);
5550 TREE_TYPE (DECL_INITIAL (decl
)) = type
;
5553 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
5555 c_incomplete_type_error (loc
, NULL_TREE
, type
);
5556 return error_mark_node
;
5559 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), DECL_EXPR
, decl
);
5560 complit
= build1 (COMPOUND_LITERAL_EXPR
, type
, stmt
);
5561 TREE_SIDE_EFFECTS (complit
) = 1;
5563 layout_decl (decl
, 0);
5565 if (TREE_STATIC (decl
))
5567 /* This decl needs a name for the assembler output. */
5568 set_compound_literal_name (decl
);
5569 DECL_DEFER_OUTPUT (decl
) = 1;
5570 DECL_COMDAT (decl
) = 1;
5572 rest_of_decl_compilation (decl
, 1, 0);
5574 else if (current_function_decl
&& !current_scope
->parm_flag
)
5579 complit
= build2 (C_MAYBE_CONST_EXPR
, type
, NULL
, complit
);
5580 C_MAYBE_CONST_EXPR_NON_CONST (complit
) = 1;
5586 /* Check the type of a compound literal. Here we just check that it
5587 is valid for C++. */
5590 check_compound_literal_type (location_t loc
, struct c_type_name
*type_name
)
5593 && (type_name
->specs
->typespec_kind
== ctsk_tagdef
5594 || type_name
->specs
->typespec_kind
== ctsk_tagfirstref
))
5595 warning_at (loc
, OPT_Wc___compat
,
5596 "defining a type in a compound literal is invalid in C++");
5599 /* Determine whether TYPE is a structure with a flexible array member,
5600 or a union containing such a structure (possibly recursively). */
5603 flexible_array_type_p (tree type
)
5606 switch (TREE_CODE (type
))
5609 x
= TYPE_FIELDS (type
);
5612 while (DECL_CHAIN (x
) != NULL_TREE
)
5614 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
5615 && TYPE_SIZE (TREE_TYPE (x
)) == NULL_TREE
5616 && TYPE_DOMAIN (TREE_TYPE (x
)) != NULL_TREE
5617 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x
))) == NULL_TREE
)
5621 for (x
= TYPE_FIELDS (type
); x
!= NULL_TREE
; x
= DECL_CHAIN (x
))
5623 if (flexible_array_type_p (TREE_TYPE (x
)))
5632 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5633 replacing with appropriate values if they are invalid. */
5636 check_bitfield_type_and_width (location_t loc
, tree
*type
, tree
*width
,
5640 unsigned int max_width
;
5641 unsigned HOST_WIDE_INT w
;
5642 const char *name
= (orig_name
5643 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name
))
5644 : _("<anonymous>"));
5646 /* Detect and ignore out of range field width and process valid
5648 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width
)))
5650 error_at (loc
, "bit-field %qs width not an integer constant", name
);
5651 *width
= integer_one_node
;
5655 if (TREE_CODE (*width
) != INTEGER_CST
)
5657 *width
= c_fully_fold (*width
, false, NULL
);
5658 if (TREE_CODE (*width
) == INTEGER_CST
)
5659 pedwarn (loc
, OPT_Wpedantic
,
5660 "bit-field %qs width not an integer constant expression",
5663 if (TREE_CODE (*width
) != INTEGER_CST
)
5665 error_at (loc
, "bit-field %qs width not an integer constant", name
);
5666 *width
= integer_one_node
;
5668 constant_expression_warning (*width
);
5669 if (tree_int_cst_sgn (*width
) < 0)
5671 error_at (loc
, "negative width in bit-field %qs", name
);
5672 *width
= integer_one_node
;
5674 else if (integer_zerop (*width
) && orig_name
)
5676 error_at (loc
, "zero width for bit-field %qs", name
);
5677 *width
= integer_one_node
;
5681 /* Detect invalid bit-field type. */
5682 if (TREE_CODE (*type
) != INTEGER_TYPE
5683 && TREE_CODE (*type
) != BOOLEAN_TYPE
5684 && TREE_CODE (*type
) != ENUMERAL_TYPE
)
5686 error_at (loc
, "bit-field %qs has invalid type", name
);
5687 *type
= unsigned_type_node
;
5690 if (TYPE_WARN_IF_NOT_ALIGN (*type
))
5692 error_at (loc
, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
5694 *type
= unsigned_type_node
;
5697 type_mv
= TYPE_MAIN_VARIANT (*type
);
5698 if (!in_system_header_at (input_location
)
5699 && type_mv
!= integer_type_node
5700 && type_mv
!= unsigned_type_node
5701 && type_mv
!= boolean_type_node
)
5702 pedwarn_c90 (loc
, OPT_Wpedantic
,
5703 "type of bit-field %qs is a GCC extension", name
);
5705 max_width
= TYPE_PRECISION (*type
);
5707 if (compare_tree_int (*width
, max_width
) > 0)
5709 error_at (loc
, "width of %qs exceeds its type", name
);
5711 *width
= build_int_cst (integer_type_node
, w
);
5714 w
= tree_to_uhwi (*width
);
5716 if (TREE_CODE (*type
) == ENUMERAL_TYPE
)
5718 struct lang_type
*lt
= TYPE_LANG_SPECIFIC (*type
);
5720 || w
< tree_int_cst_min_precision (lt
->enum_min
, TYPE_SIGN (*type
))
5721 || w
< tree_int_cst_min_precision (lt
->enum_max
, TYPE_SIGN (*type
)))
5722 warning_at (loc
, 0, "%qs is narrower than values of its type", name
);
5728 /* Print warning about variable length array if necessary. */
5731 warn_variable_length_array (tree name
, tree size
)
5733 if (TREE_CONSTANT (size
))
5736 pedwarn_c90 (input_location
, OPT_Wvla
,
5737 "ISO C90 forbids array %qE whose size "
5738 "cannot be evaluated", name
);
5740 pedwarn_c90 (input_location
, OPT_Wvla
, "ISO C90 forbids array "
5741 "whose size cannot be evaluated");
5746 pedwarn_c90 (input_location
, OPT_Wvla
,
5747 "ISO C90 forbids variable length array %qE", name
);
5749 pedwarn_c90 (input_location
, OPT_Wvla
, "ISO C90 forbids variable "
5754 /* Print warning about defaulting to int if necessary. */
5757 warn_defaults_to (location_t location
, int opt
, const char *gmsgid
, ...)
5759 diagnostic_info diagnostic
;
5761 rich_location
richloc (line_table
, location
);
5763 va_start (ap
, gmsgid
);
5764 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
,
5765 flag_isoc99
? DK_PEDWARN
: DK_WARNING
);
5766 diagnostic
.option_index
= opt
;
5767 diagnostic_report_diagnostic (global_dc
, &diagnostic
);
5771 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
5772 considering only those c_declspec_words found in LIST, which
5773 must be terminated by cdw_number_of_elements. */
5776 smallest_type_quals_location (const location_t
*locations
,
5777 const c_declspec_word
*list
)
5779 location_t loc
= UNKNOWN_LOCATION
;
5780 while (*list
!= cdw_number_of_elements
)
5782 location_t newloc
= locations
[*list
];
5783 if (loc
== UNKNOWN_LOCATION
5784 || (newloc
!= UNKNOWN_LOCATION
&& newloc
< loc
))
5792 /* Given declspecs and a declarator,
5793 determine the name and type of the object declared
5794 and construct a ..._DECL node for it.
5795 (In one case we can return a ..._TYPE node instead.
5796 For invalid input we sometimes return NULL_TREE.)
5798 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5800 DECL_CONTEXT says which syntactic context this declaration is in:
5801 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5802 FUNCDEF for a function definition. Like NORMAL but a few different
5803 error messages in each case. Return value may be zero meaning
5804 this definition is too screwy to try to parse.
5805 PARM for a parameter declaration (either within a function prototype
5806 or before a function body). Make a PARM_DECL, or return void_type_node.
5807 TYPENAME if for a typename (in a cast or sizeof).
5808 Don't make a DECL node; just return the ..._TYPE node.
5809 FIELD for a struct or union field; make a FIELD_DECL.
5810 INITIALIZED is true if the decl has an initializer.
5811 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5812 representing the width of the bit-field.
5813 DECL_ATTRS points to the list of attributes that should be added to this
5814 decl. Any nested attributes that belong on the decl itself will be
5816 If EXPR is not NULL, any expressions that need to be evaluated as
5817 part of evaluating variably modified types will be stored in *EXPR.
5818 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5819 set to indicate whether operands in *EXPR can be used in constant
5821 DEPRECATED_STATE is a deprecated_states value indicating whether
5822 deprecation warnings should be suppressed.
5824 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5825 It may also be so in the PARM case, for a prototype where the
5826 argument type is specified but not the name.
5828 This function is where the complicated C meanings of `static'
5829 and `extern' are interpreted. */
5832 grokdeclarator (const struct c_declarator
*declarator
,
5833 struct c_declspecs
*declspecs
,
5834 enum decl_context decl_context
, bool initialized
, tree
*width
,
5835 tree
*decl_attrs
, tree
*expr
, bool *expr_const_operands
,
5836 enum deprecated_states deprecated_state
)
5838 tree type
= declspecs
->type
;
5839 bool threadp
= declspecs
->thread_p
;
5840 enum c_storage_class storage_class
= declspecs
->storage_class
;
5845 int type_quals
= TYPE_UNQUALIFIED
;
5846 tree name
= NULL_TREE
;
5847 bool funcdef_flag
= false;
5848 bool funcdef_syntax
= false;
5849 bool size_varies
= false;
5850 tree decl_attr
= declspecs
->decl_attr
;
5851 int array_ptr_quals
= TYPE_UNQUALIFIED
;
5852 tree array_ptr_attrs
= NULL_TREE
;
5853 bool array_parm_static
= false;
5854 bool array_parm_vla_unspec_p
= false;
5855 tree returned_attrs
= NULL_TREE
;
5856 bool bitfield
= width
!= NULL
;
5858 tree orig_qual_type
= NULL
;
5859 size_t orig_qual_indirect
= 0;
5860 struct c_arg_info
*arg_info
= 0;
5861 addr_space_t as1
, as2
, address_space
;
5862 location_t loc
= UNKNOWN_LOCATION
;
5864 bool expr_const_operands_dummy
;
5865 enum c_declarator_kind first_non_attr_kind
;
5866 unsigned int alignas_align
= 0;
5868 if (TREE_CODE (type
) == ERROR_MARK
)
5869 return error_mark_node
;
5873 expr_dummy
= NULL_TREE
;
5875 if (expr_const_operands
== NULL
)
5876 expr_const_operands
= &expr_const_operands_dummy
;
5878 if (declspecs
->expr
)
5881 *expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (declspecs
->expr
), *expr
,
5884 *expr
= declspecs
->expr
;
5886 *expr_const_operands
= declspecs
->expr_const_operands
;
5888 if (decl_context
== FUNCDEF
)
5889 funcdef_flag
= true, decl_context
= NORMAL
;
5891 /* Look inside a declarator for the name being declared
5892 and get it as an IDENTIFIER_NODE, for an error message. */
5894 const struct c_declarator
*decl
= declarator
;
5896 first_non_attr_kind
= cdk_attrs
;
5906 funcdef_syntax
= (decl
->kind
== cdk_function
);
5907 if (first_non_attr_kind
== cdk_attrs
)
5908 first_non_attr_kind
= decl
->kind
;
5909 decl
= decl
->declarator
;
5913 decl
= decl
->declarator
;
5920 if (first_non_attr_kind
== cdk_attrs
)
5921 first_non_attr_kind
= decl
->kind
;
5928 if (name
== NULL_TREE
)
5930 gcc_assert (decl_context
== PARM
5931 || decl_context
== TYPENAME
5932 || (decl_context
== FIELD
5933 && declarator
->kind
== cdk_id
));
5934 gcc_assert (!initialized
);
5938 /* A function definition's declarator must have the form of
5939 a function declarator. */
5941 if (funcdef_flag
&& !funcdef_syntax
)
5944 /* If this looks like a function definition, make it one,
5945 even if it occurs where parms are expected.
5946 Then store_parm_decls will reject it and not use it as a parm. */
5947 if (decl_context
== NORMAL
&& !funcdef_flag
&& current_scope
->parm_flag
)
5948 decl_context
= PARM
;
5950 if (declspecs
->deprecated_p
&& deprecated_state
!= DEPRECATED_SUPPRESS
)
5951 warn_deprecated_use (declspecs
->type
, declspecs
->decl_attr
);
5953 if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
5954 && current_scope
== file_scope
5955 && variably_modified_type_p (type
, NULL_TREE
))
5958 error_at (loc
, "variably modified %qE at file scope", name
);
5960 error_at (loc
, "variably modified field at file scope");
5961 type
= integer_type_node
;
5964 size_varies
= C_TYPE_VARIABLE_SIZE (type
) != 0;
5966 /* Diagnose defaulting to "int". */
5968 if (declspecs
->default_int_p
&& !in_system_header_at (input_location
))
5970 /* Issue a warning if this is an ISO C 99 program or if
5971 -Wreturn-type and this is a function, or if -Wimplicit;
5972 prefer the former warning since it is more explicit. */
5973 if ((warn_implicit_int
|| warn_return_type
> 0 || flag_isoc99
)
5975 warn_about_return_type
= 1;
5979 warn_defaults_to (loc
, OPT_Wimplicit_int
,
5980 "type defaults to %<int%> in declaration "
5983 warn_defaults_to (loc
, OPT_Wimplicit_int
,
5984 "type defaults to %<int%> in type name");
5988 /* Adjust the type if a bit-field is being declared,
5989 -funsigned-bitfields applied and the type is not explicitly
5991 if (bitfield
&& !flag_signed_bitfields
&& !declspecs
->explicit_signed_p
5992 && TREE_CODE (type
) == INTEGER_TYPE
)
5993 type
= unsigned_type_for (type
);
5995 /* Figure out the type qualifiers for the declaration. There are
5996 two ways a declaration can become qualified. One is something
5997 like `const int i' where the `const' is explicit. Another is
5998 something like `typedef const int CI; CI i' where the type of the
5999 declaration contains the `const'. A third possibility is that
6000 there is a type qualifier on the element type of a typedefed
6001 array type, in which case we should extract that qualifier so
6002 that c_apply_type_quals_to_decl receives the full list of
6003 qualifiers to work with (C90 is not entirely clear about whether
6004 duplicate qualifiers should be diagnosed in this case, but it
6005 seems most appropriate to do so). */
6006 element_type
= strip_array_types (type
);
6007 constp
= declspecs
->const_p
+ TYPE_READONLY (element_type
);
6008 restrictp
= declspecs
->restrict_p
+ TYPE_RESTRICT (element_type
);
6009 volatilep
= declspecs
->volatile_p
+ TYPE_VOLATILE (element_type
);
6010 atomicp
= declspecs
->atomic_p
+ TYPE_ATOMIC (element_type
);
6011 as1
= declspecs
->address_space
;
6012 as2
= TYPE_ADDR_SPACE (element_type
);
6013 address_space
= ADDR_SPACE_GENERIC_P (as1
)? as2
: as1
;
6016 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<const%>");
6018 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<restrict%>");
6020 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<volatile%>");
6022 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<_Atomic%>");
6024 if (!ADDR_SPACE_GENERIC_P (as1
) && !ADDR_SPACE_GENERIC_P (as2
) && as1
!= as2
)
6025 error_at (loc
, "conflicting named address spaces (%s vs %s)",
6026 c_addr_space_name (as1
), c_addr_space_name (as2
));
6028 if ((TREE_CODE (type
) == ARRAY_TYPE
6029 || first_non_attr_kind
== cdk_array
)
6030 && TYPE_QUALS (element_type
))
6032 orig_qual_type
= type
;
6033 type
= TYPE_MAIN_VARIANT (type
);
6035 type_quals
= ((constp
? TYPE_QUAL_CONST
: 0)
6036 | (restrictp
? TYPE_QUAL_RESTRICT
: 0)
6037 | (volatilep
? TYPE_QUAL_VOLATILE
: 0)
6038 | (atomicp
? TYPE_QUAL_ATOMIC
: 0)
6039 | ENCODE_QUAL_ADDR_SPACE (address_space
));
6040 if (type_quals
!= TYPE_QUALS (element_type
))
6041 orig_qual_type
= NULL_TREE
;
6043 /* Applying the _Atomic qualifier to an array type (through the use
6044 of typedefs or typeof) must be detected here. If the qualifier
6045 is introduced later, any appearance of applying it to an array is
6046 actually applying it to an element of that array. */
6047 if (declspecs
->atomic_p
&& TREE_CODE (type
) == ARRAY_TYPE
)
6048 error_at (loc
, "%<_Atomic%>-qualified array type");
6050 /* Warn about storage classes that are invalid for certain
6051 kinds of declarations (parameters, typenames, etc.). */
6055 || storage_class
== csc_auto
6056 || storage_class
== csc_register
6057 || storage_class
== csc_typedef
))
6059 if (storage_class
== csc_auto
)
6061 (current_scope
== file_scope
) ? 0 : OPT_Wpedantic
,
6062 "function definition declared %<auto%>");
6063 if (storage_class
== csc_register
)
6064 error_at (loc
, "function definition declared %<register%>");
6065 if (storage_class
== csc_typedef
)
6066 error_at (loc
, "function definition declared %<typedef%>");
6068 error_at (loc
, "function definition declared %qs",
6069 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
6071 if (storage_class
== csc_auto
6072 || storage_class
== csc_register
6073 || storage_class
== csc_typedef
)
6074 storage_class
= csc_none
;
6076 else if (decl_context
!= NORMAL
&& (storage_class
!= csc_none
|| threadp
))
6078 if (decl_context
== PARM
&& storage_class
== csc_register
)
6082 switch (decl_context
)
6086 error_at (loc
, "storage class specified for structure "
6089 error_at (loc
, "storage class specified for structure field");
6093 error_at (loc
, "storage class specified for parameter %qE",
6096 error_at (loc
, "storage class specified for unnamed parameter");
6099 error_at (loc
, "storage class specified for typename");
6102 storage_class
= csc_none
;
6106 else if (storage_class
== csc_extern
6110 /* 'extern' with initialization is invalid if not at file scope. */
6111 if (current_scope
== file_scope
)
6113 /* It is fine to have 'extern const' when compiling at C
6114 and C++ intersection. */
6115 if (!(warn_cxx_compat
&& constp
))
6116 warning_at (loc
, 0, "%qE initialized and declared %<extern%>",
6120 error_at (loc
, "%qE has both %<extern%> and initializer", name
);
6122 else if (current_scope
== file_scope
)
6124 if (storage_class
== csc_auto
)
6125 error_at (loc
, "file-scope declaration of %qE specifies %<auto%>",
6127 if (pedantic
&& storage_class
== csc_register
)
6128 pedwarn (input_location
, OPT_Wpedantic
,
6129 "file-scope declaration of %qE specifies %<register%>", name
);
6133 if (storage_class
== csc_extern
&& funcdef_flag
)
6134 error_at (loc
, "nested function %qE declared %<extern%>", name
);
6135 else if (threadp
&& storage_class
== csc_none
)
6137 error_at (loc
, "function-scope %qE implicitly auto and declared "
6139 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
6144 /* Now figure out the structure of the declarator proper.
6145 Descend through it, creating more complex types, until we reach
6146 the declared identifier (or NULL_TREE, in an absolute declarator).
6147 At each stage we maintain an unqualified version of the type
6148 together with any qualifiers that should be applied to it with
6149 c_build_qualified_type; this way, array types including
6150 multidimensional array types are first built up in unqualified
6151 form and then the qualified form is created with
6152 TYPE_MAIN_VARIANT pointing to the unqualified form. */
6154 while (declarator
&& declarator
->kind
!= cdk_id
)
6156 if (type
== error_mark_node
)
6158 declarator
= declarator
->declarator
;
6162 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
6163 a cdk_pointer (for *...),
6164 a cdk_function (for ...(...)),
6165 a cdk_attrs (for nested attributes),
6166 or a cdk_id (for the name being declared
6167 or the place in an absolute declarator
6168 where the name was omitted).
6169 For the last case, we have just exited the loop.
6171 At this point, TYPE is the type of elements of an array,
6172 or for a function to return, or for a pointer to point to.
6173 After this sequence of ifs, TYPE is the type of the
6174 array or function or pointer, and DECLARATOR has had its
6175 outermost layer removed. */
6177 if (array_ptr_quals
!= TYPE_UNQUALIFIED
6178 || array_ptr_attrs
!= NULL_TREE
6179 || array_parm_static
)
6181 /* Only the innermost declarator (making a parameter be of
6182 array type which is converted to pointer type)
6183 may have static or type qualifiers. */
6184 error_at (loc
, "static or type qualifiers in non-parameter array declarator");
6185 array_ptr_quals
= TYPE_UNQUALIFIED
;
6186 array_ptr_attrs
= NULL_TREE
;
6187 array_parm_static
= false;
6190 switch (declarator
->kind
)
6194 /* A declarator with embedded attributes. */
6195 tree attrs
= declarator
->u
.attrs
;
6196 const struct c_declarator
*inner_decl
;
6198 declarator
= declarator
->declarator
;
6199 inner_decl
= declarator
;
6200 while (inner_decl
->kind
== cdk_attrs
)
6201 inner_decl
= inner_decl
->declarator
;
6202 if (inner_decl
->kind
== cdk_id
)
6203 attr_flags
|= (int) ATTR_FLAG_DECL_NEXT
;
6204 else if (inner_decl
->kind
== cdk_function
)
6205 attr_flags
|= (int) ATTR_FLAG_FUNCTION_NEXT
;
6206 else if (inner_decl
->kind
== cdk_array
)
6207 attr_flags
|= (int) ATTR_FLAG_ARRAY_NEXT
;
6208 returned_attrs
= decl_attributes (&type
,
6209 chainon (returned_attrs
, attrs
),
6215 tree itype
= NULL_TREE
;
6216 tree size
= declarator
->u
.array
.dimen
;
6217 /* The index is a signed object `sizetype' bits wide. */
6218 tree index_type
= c_common_signed_type (sizetype
);
6220 array_ptr_quals
= declarator
->u
.array
.quals
;
6221 array_ptr_attrs
= declarator
->u
.array
.attrs
;
6222 array_parm_static
= declarator
->u
.array
.static_p
;
6223 array_parm_vla_unspec_p
= declarator
->u
.array
.vla_unspec_p
;
6225 declarator
= declarator
->declarator
;
6227 /* Check for some types that there cannot be arrays of. */
6229 if (VOID_TYPE_P (type
))
6232 error_at (loc
, "declaration of %qE as array of voids", name
);
6234 error_at (loc
, "declaration of type name as array of voids");
6235 type
= error_mark_node
;
6238 if (TREE_CODE (type
) == FUNCTION_TYPE
)
6241 error_at (loc
, "declaration of %qE as array of functions",
6244 error_at (loc
, "declaration of type name as array of "
6246 type
= error_mark_node
;
6249 if (pedantic
&& !in_system_header_at (input_location
)
6250 && flexible_array_type_p (type
))
6251 pedwarn (loc
, OPT_Wpedantic
,
6252 "invalid use of structure with flexible array member");
6254 if (size
== error_mark_node
)
6255 type
= error_mark_node
;
6257 if (type
== error_mark_node
)
6260 /* If size was specified, set ITYPE to a range-type for
6261 that size. Otherwise, ITYPE remains null. finish_decl
6262 may figure it out from an initial value. */
6266 bool size_maybe_const
= true;
6267 bool size_int_const
= (TREE_CODE (size
) == INTEGER_CST
6268 && !TREE_OVERFLOW (size
));
6269 bool this_size_varies
= false;
6271 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6273 STRIP_TYPE_NOPS (size
);
6275 if (!INTEGRAL_TYPE_P (TREE_TYPE (size
)))
6278 error_at (loc
, "size of array %qE has non-integer type",
6282 "size of unnamed array has non-integer type");
6283 size
= integer_one_node
;
6285 /* This can happen with enum forward declaration. */
6286 else if (!COMPLETE_TYPE_P (TREE_TYPE (size
)))
6289 error_at (loc
, "size of array %qE has incomplete type",
6292 error_at (loc
, "size of unnamed array has incomplete "
6294 size
= integer_one_node
;
6297 size
= c_fully_fold (size
, false, &size_maybe_const
);
6299 if (pedantic
&& size_maybe_const
&& integer_zerop (size
))
6302 pedwarn (loc
, OPT_Wpedantic
,
6303 "ISO C forbids zero-size array %qE", name
);
6305 pedwarn (loc
, OPT_Wpedantic
,
6306 "ISO C forbids zero-size array");
6309 if (TREE_CODE (size
) == INTEGER_CST
&& size_maybe_const
)
6311 constant_expression_warning (size
);
6312 if (tree_int_cst_sgn (size
) < 0)
6315 error_at (loc
, "size of array %qE is negative", name
);
6317 error_at (loc
, "size of unnamed array is negative");
6318 size
= integer_one_node
;
6320 /* Handle a size folded to an integer constant but
6321 not an integer constant expression. */
6322 if (!size_int_const
)
6324 /* If this is a file scope declaration of an
6325 ordinary identifier, this is invalid code;
6326 diagnosing it here and not subsequently
6327 treating the type as variable-length avoids
6328 more confusing diagnostics later. */
6329 if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
6330 && current_scope
== file_scope
)
6331 pedwarn (input_location
, 0,
6332 "variably modified %qE at file scope",
6335 this_size_varies
= size_varies
= true;
6336 warn_variable_length_array (name
, size
);
6339 else if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
6340 && current_scope
== file_scope
)
6342 error_at (loc
, "variably modified %qE at file scope", name
);
6343 size
= integer_one_node
;
6347 /* Make sure the array size remains visibly
6348 nonconstant even if it is (eg) a const variable
6349 with known value. */
6350 this_size_varies
= size_varies
= true;
6351 warn_variable_length_array (name
, size
);
6352 if (sanitize_flags_p (SANITIZE_VLA
)
6353 && current_function_decl
!= NULL_TREE
6354 && decl_context
== NORMAL
)
6356 /* Evaluate the array size only once. */
6357 size
= save_expr (size
);
6358 size
= c_fully_fold (size
, false, NULL
);
6359 size
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
6360 ubsan_instrument_vla (loc
, size
),
6365 if (integer_zerop (size
) && !this_size_varies
)
6367 /* A zero-length array cannot be represented with
6368 an unsigned index type, which is what we'll
6369 get with build_index_type. Create an
6370 open-ended range instead. */
6371 itype
= build_range_type (sizetype
, size
, NULL_TREE
);
6375 /* Arrange for the SAVE_EXPR on the inside of the
6376 MINUS_EXPR, which allows the -1 to get folded
6377 with the +1 that happens when building TYPE_SIZE. */
6379 size
= save_expr (size
);
6380 if (this_size_varies
&& TREE_CODE (size
) == INTEGER_CST
)
6381 size
= build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
6382 integer_zero_node
, size
);
6384 /* Compute the maximum valid index, that is, size
6385 - 1. Do the calculation in index_type, so that
6386 if it is a variable the computations will be
6387 done in the proper mode. */
6388 itype
= fold_build2_loc (loc
, MINUS_EXPR
, index_type
,
6389 convert (index_type
, size
),
6390 convert (index_type
,
6393 /* The above overflows when size does not fit
6395 ??? While a size of INT_MAX+1 technically shouldn't
6396 cause an overflow (because we subtract 1), handling
6397 this case seems like an unnecessary complication. */
6398 if (TREE_CODE (size
) == INTEGER_CST
6399 && !int_fits_type_p (size
, index_type
))
6402 error_at (loc
, "size of array %qE is too large",
6405 error_at (loc
, "size of unnamed array is too large");
6406 type
= error_mark_node
;
6410 itype
= build_index_type (itype
);
6412 if (this_size_varies
)
6415 *expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
6419 *expr_const_operands
&= size_maybe_const
;
6422 else if (decl_context
== FIELD
)
6424 bool flexible_array_member
= false;
6425 if (array_parm_vla_unspec_p
)
6426 /* Field names can in fact have function prototype
6427 scope so [*] is disallowed here through making
6428 the field variably modified, not through being
6429 something other than a declaration with function
6434 const struct c_declarator
*t
= declarator
;
6435 while (t
->kind
== cdk_attrs
)
6437 flexible_array_member
= (t
->kind
== cdk_id
);
6439 if (flexible_array_member
6440 && !in_system_header_at (input_location
))
6441 pedwarn_c90 (loc
, OPT_Wpedantic
, "ISO C90 does not "
6442 "support flexible array members");
6444 /* ISO C99 Flexible array members are effectively
6445 identical to GCC's zero-length array extension. */
6446 if (flexible_array_member
|| array_parm_vla_unspec_p
)
6447 itype
= build_range_type (sizetype
, size_zero_node
,
6450 else if (decl_context
== PARM
)
6452 if (array_parm_vla_unspec_p
)
6454 itype
= build_range_type (sizetype
, size_zero_node
, NULL_TREE
);
6458 else if (decl_context
== TYPENAME
)
6460 if (array_parm_vla_unspec_p
)
6463 warning (0, "%<[*]%> not in a declaration");
6464 /* We use this to avoid messing up with incomplete
6465 array types of the same type, that would
6466 otherwise be modified below. */
6467 itype
= build_range_type (sizetype
, size_zero_node
,
6473 /* Complain about arrays of incomplete types. */
6474 if (!COMPLETE_TYPE_P (type
))
6476 error_at (loc
, "array type has incomplete element type %qT",
6478 /* See if we can be more helpful. */
6479 if (TREE_CODE (type
) == ARRAY_TYPE
)
6482 inform (loc
, "declaration of %qE as multidimensional "
6483 "array must have bounds for all dimensions "
6484 "except the first", name
);
6486 inform (loc
, "declaration of multidimensional array "
6487 "must have bounds for all dimensions except "
6490 type
= error_mark_node
;
6493 /* When itype is NULL, a shared incomplete array type is
6494 returned for all array of a given type. Elsewhere we
6495 make sure we don't complete that type before copying
6496 it, but here we want to make sure we don't ever
6497 modify the shared type, so we gcc_assert (itype)
6500 addr_space_t as
= DECODE_QUAL_ADDR_SPACE (type_quals
);
6501 if (!ADDR_SPACE_GENERIC_P (as
) && as
!= TYPE_ADDR_SPACE (type
))
6502 type
= build_qualified_type (type
,
6503 ENCODE_QUAL_ADDR_SPACE (as
));
6505 type
= build_array_type (type
, itype
);
6508 if (type
!= error_mark_node
)
6512 /* It is ok to modify type here even if itype is
6513 NULL: if size_varies, we're in a
6514 multi-dimensional array and the inner type has
6515 variable size, so the enclosing shared array type
6517 if (size
&& TREE_CODE (size
) == INTEGER_CST
)
6519 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6520 C_TYPE_VARIABLE_SIZE (type
) = 1;
6523 /* The GCC extension for zero-length arrays differs from
6524 ISO flexible array members in that sizeof yields
6526 if (size
&& integer_zerop (size
))
6529 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6530 TYPE_SIZE (type
) = bitsize_zero_node
;
6531 TYPE_SIZE_UNIT (type
) = size_zero_node
;
6532 SET_TYPE_STRUCTURAL_EQUALITY (type
);
6534 if (array_parm_vla_unspec_p
)
6537 /* The type is complete. C99 6.7.5.2p4 */
6538 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6539 TYPE_SIZE (type
) = bitsize_zero_node
;
6540 TYPE_SIZE_UNIT (type
) = size_zero_node
;
6541 SET_TYPE_STRUCTURAL_EQUALITY (type
);
6544 if (!valid_array_size_p (loc
, type
, name
))
6545 type
= error_mark_node
;
6548 if (decl_context
!= PARM
6549 && (array_ptr_quals
!= TYPE_UNQUALIFIED
6550 || array_ptr_attrs
!= NULL_TREE
6551 || array_parm_static
))
6553 error_at (loc
, "static or type qualifiers in non-parameter "
6554 "array declarator");
6555 array_ptr_quals
= TYPE_UNQUALIFIED
;
6556 array_ptr_attrs
= NULL_TREE
;
6557 array_parm_static
= false;
6559 orig_qual_indirect
++;
6564 /* Say it's a definition only for the declarator closest
6565 to the identifier, apart possibly from some
6567 bool really_funcdef
= false;
6569 orig_qual_type
= NULL_TREE
;
6572 const struct c_declarator
*t
= declarator
->declarator
;
6573 while (t
->kind
== cdk_attrs
)
6575 really_funcdef
= (t
->kind
== cdk_id
);
6578 /* Declaring a function type. Make sure we have a valid
6579 type for the function to return. */
6580 if (type
== error_mark_node
)
6583 size_varies
= false;
6585 /* Warn about some types functions can't return. */
6586 if (TREE_CODE (type
) == FUNCTION_TYPE
)
6589 error_at (loc
, "%qE declared as function returning a "
6592 error_at (loc
, "type name declared as function "
6593 "returning a function");
6594 type
= integer_type_node
;
6596 if (TREE_CODE (type
) == ARRAY_TYPE
)
6599 error_at (loc
, "%qE declared as function returning an array",
6602 error_at (loc
, "type name declared as function returning "
6604 type
= integer_type_node
;
6607 /* Construct the function type and go to the next
6608 inner layer of declarator. */
6609 arg_info
= declarator
->u
.arg_info
;
6610 arg_types
= grokparms (arg_info
, really_funcdef
);
6612 /* Type qualifiers before the return type of the function
6613 qualify the return type, not the function type. */
6616 const enum c_declspec_word ignored_quals_list
[] =
6618 cdw_const
, cdw_volatile
, cdw_restrict
, cdw_address_space
,
6619 cdw_atomic
, cdw_number_of_elements
6621 location_t specs_loc
6622 = smallest_type_quals_location (declspecs
->locations
,
6623 ignored_quals_list
);
6624 if (specs_loc
== UNKNOWN_LOCATION
)
6625 specs_loc
= declspecs
->locations
[cdw_typedef
];
6626 if (specs_loc
== UNKNOWN_LOCATION
)
6629 /* Type qualifiers on a function return type are
6630 normally permitted by the standard but have no
6631 effect, so give a warning at -Wreturn-type.
6632 Qualifiers on a void return type are banned on
6633 function definitions in ISO C; GCC used to used
6634 them for noreturn functions. The resolution of C11
6635 DR#423 means qualifiers (other than _Atomic) are
6636 actually removed from the return type when
6637 determining the function type. */
6638 int quals_used
= type_quals
;
6640 quals_used
&= TYPE_QUAL_ATOMIC
;
6641 if (quals_used
&& VOID_TYPE_P (type
) && really_funcdef
)
6642 pedwarn (specs_loc
, 0,
6643 "function definition has qualified void "
6646 warning_at (specs_loc
, OPT_Wignored_qualifiers
,
6647 "type qualifiers ignored on function "
6650 /* Ensure an error for restrict on invalid types; the
6651 DR#423 resolution is not entirely clear about
6654 && (type_quals
& TYPE_QUAL_RESTRICT
)
6655 && (!POINTER_TYPE_P (type
)
6656 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
6657 error_at (loc
, "invalid use of %<restrict%>");
6658 type
= c_build_qualified_type (type
, quals_used
);
6660 type_quals
= TYPE_UNQUALIFIED
;
6662 type
= build_function_type (type
, arg_types
);
6663 declarator
= declarator
->declarator
;
6665 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6666 the formal parameter list of this FUNCTION_TYPE to point to
6667 the FUNCTION_TYPE node itself. */
6672 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info
->tags
, ix
, tag
)
6673 TYPE_CONTEXT (tag
->type
) = type
;
6679 /* Merge any constancy or volatility into the target type
6681 if ((type_quals
& TYPE_QUAL_ATOMIC
)
6682 && TREE_CODE (type
) == FUNCTION_TYPE
)
6685 "%<_Atomic%>-qualified function type");
6686 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6688 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
6690 pedwarn (loc
, OPT_Wpedantic
,
6691 "ISO C forbids qualified function types");
6693 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6694 orig_qual_indirect
);
6695 orig_qual_type
= NULL_TREE
;
6696 size_varies
= false;
6698 /* When the pointed-to type involves components of variable size,
6699 care must be taken to ensure that the size evaluation code is
6700 emitted early enough to dominate all the possible later uses
6701 and late enough for the variables on which it depends to have
6704 This is expected to happen automatically when the pointed-to
6705 type has a name/declaration of it's own, but special attention
6706 is required if the type is anonymous.
6708 We attach an artificial TYPE_DECL to such pointed-to type
6709 and arrange for it to be included in a DECL_EXPR. This
6710 forces the sizes evaluation at a safe point and ensures it
6711 is not deferred until e.g. within a deeper conditional context.
6713 PARM contexts have no enclosing statement list that
6714 can hold the DECL_EXPR, so we need to use a BIND_EXPR
6715 instead, and add it to the list of expressions that
6716 need to be evaluated.
6718 TYPENAME contexts do have an enclosing statement list,
6719 but it would be incorrect to use it, as the size should
6720 only be evaluated if the containing expression is
6721 evaluated. We might also be in the middle of an
6722 expression with side effects on the pointed-to type size
6723 "arguments" prior to the pointer declaration point and
6724 the fake TYPE_DECL in the enclosing context would force
6725 the size evaluation prior to the side effects. We therefore
6726 use BIND_EXPRs in TYPENAME contexts too. */
6727 if (!TYPE_NAME (type
)
6728 && variably_modified_type_p (type
, NULL_TREE
))
6730 tree bind
= NULL_TREE
;
6731 if (decl_context
== TYPENAME
|| decl_context
== PARM
)
6733 bind
= build3 (BIND_EXPR
, void_type_node
, NULL_TREE
,
6734 NULL_TREE
, NULL_TREE
);
6735 TREE_SIDE_EFFECTS (bind
) = 1;
6736 BIND_EXPR_BODY (bind
) = push_stmt_list ();
6739 tree decl
= build_decl (loc
, TYPE_DECL
, NULL_TREE
, type
);
6740 DECL_ARTIFICIAL (decl
) = 1;
6742 finish_decl (decl
, loc
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
6743 TYPE_NAME (type
) = decl
;
6747 BIND_EXPR_BODY (bind
)
6748 = pop_stmt_list (BIND_EXPR_BODY (bind
));
6750 *expr
= build2 (COMPOUND_EXPR
, void_type_node
, *expr
,
6757 type
= c_build_pointer_type (type
);
6759 /* Process type qualifiers (such as const or volatile)
6760 that were given inside the `*'. */
6761 type_quals
= declarator
->u
.pointer_quals
;
6763 declarator
= declarator
->declarator
;
6770 *decl_attrs
= chainon (returned_attrs
, *decl_attrs
);
6772 /* Now TYPE has the actual type, apart from any qualifiers in
6775 /* Warn about address space used for things other than static memory or
6777 address_space
= DECODE_QUAL_ADDR_SPACE (type_quals
);
6778 if (!ADDR_SPACE_GENERIC_P (address_space
))
6780 if (decl_context
== NORMAL
)
6782 switch (storage_class
)
6785 error ("%qs combined with %<auto%> qualifier for %qE",
6786 c_addr_space_name (address_space
), name
);
6789 error ("%qs combined with %<register%> qualifier for %qE",
6790 c_addr_space_name (address_space
), name
);
6793 if (current_function_scope
)
6795 error ("%qs specified for auto variable %qE",
6796 c_addr_space_name (address_space
), name
);
6808 else if (decl_context
== PARM
&& TREE_CODE (type
) != ARRAY_TYPE
)
6811 error ("%qs specified for parameter %qE",
6812 c_addr_space_name (address_space
), name
);
6814 error ("%qs specified for unnamed parameter",
6815 c_addr_space_name (address_space
));
6817 else if (decl_context
== FIELD
)
6820 error ("%qs specified for structure field %qE",
6821 c_addr_space_name (address_space
), name
);
6823 error ("%qs specified for structure field",
6824 c_addr_space_name (address_space
));
6828 /* Check the type and width of a bit-field. */
6831 check_bitfield_type_and_width (loc
, &type
, width
, name
);
6832 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6833 atomic types are permitted for bit-fields; we have no code to
6834 make bit-field accesses atomic, so disallow them. */
6835 if (type_quals
& TYPE_QUAL_ATOMIC
)
6838 error_at (loc
, "bit-field %qE has atomic type", name
);
6840 error_at (loc
, "bit-field has atomic type");
6841 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6845 /* Reject invalid uses of _Alignas. */
6846 if (declspecs
->alignas_p
)
6848 if (storage_class
== csc_typedef
)
6849 error_at (loc
, "alignment specified for typedef %qE", name
);
6850 else if (storage_class
== csc_register
)
6851 error_at (loc
, "alignment specified for %<register%> object %qE",
6853 else if (decl_context
== PARM
)
6856 error_at (loc
, "alignment specified for parameter %qE", name
);
6858 error_at (loc
, "alignment specified for unnamed parameter");
6863 error_at (loc
, "alignment specified for bit-field %qE", name
);
6865 error_at (loc
, "alignment specified for unnamed bit-field");
6867 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
6868 error_at (loc
, "alignment specified for function %qE", name
);
6869 else if (declspecs
->align_log
!= -1 && TYPE_P (type
))
6871 alignas_align
= 1U << declspecs
->align_log
;
6872 if (alignas_align
< min_align_of_type (type
))
6875 error_at (loc
, "%<_Alignas%> specifiers cannot reduce "
6876 "alignment of %qE", name
);
6878 error_at (loc
, "%<_Alignas%> specifiers cannot reduce "
6879 "alignment of unnamed field");
6885 /* If this is declaring a typedef name, return a TYPE_DECL. */
6887 if (storage_class
== csc_typedef
)
6890 if ((type_quals
& TYPE_QUAL_ATOMIC
)
6891 && TREE_CODE (type
) == FUNCTION_TYPE
)
6894 "%<_Atomic%>-qualified function type");
6895 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6897 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
6899 pedwarn (loc
, OPT_Wpedantic
,
6900 "ISO C forbids qualified function types");
6902 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6903 orig_qual_indirect
);
6904 decl
= build_decl (declarator
->id_loc
,
6905 TYPE_DECL
, declarator
->u
.id
, type
);
6906 if (declspecs
->explicit_signed_p
)
6907 C_TYPEDEF_EXPLICITLY_SIGNED (decl
) = 1;
6908 if (declspecs
->inline_p
)
6909 pedwarn (loc
, 0,"typedef %q+D declared %<inline%>", decl
);
6910 if (declspecs
->noreturn_p
)
6911 pedwarn (loc
, 0,"typedef %q+D declared %<_Noreturn%>", decl
);
6913 if (warn_cxx_compat
&& declarator
->u
.id
!= NULL_TREE
)
6915 struct c_binding
*b
= I_TAG_BINDING (declarator
->u
.id
);
6918 && b
->decl
!= NULL_TREE
6919 && (B_IN_CURRENT_SCOPE (b
)
6920 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
6921 && TYPE_MAIN_VARIANT (b
->decl
) != TYPE_MAIN_VARIANT (type
))
6923 auto_diagnostic_group d
;
6924 if (warning_at (declarator
->id_loc
, OPT_Wc___compat
,
6925 ("using %qD as both a typedef and a tag is "
6926 "invalid in C++"), decl
)
6927 && b
->locus
!= UNKNOWN_LOCATION
)
6928 inform (b
->locus
, "originally defined here");
6935 /* If this is a type name (such as, in a cast or sizeof),
6936 compute the type and return it now. */
6938 if (decl_context
== TYPENAME
)
6940 /* Note that the grammar rejects storage classes in typenames
6942 gcc_assert (storage_class
== csc_none
&& !threadp
6943 && !declspecs
->inline_p
&& !declspecs
->noreturn_p
);
6944 if ((type_quals
& TYPE_QUAL_ATOMIC
)
6945 && TREE_CODE (type
) == FUNCTION_TYPE
)
6948 "%<_Atomic%>-qualified function type");
6949 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6951 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
6953 pedwarn (loc
, OPT_Wpedantic
,
6954 "ISO C forbids const or volatile function types");
6956 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6957 orig_qual_indirect
);
6961 if (pedantic
&& decl_context
== FIELD
6962 && variably_modified_type_p (type
, NULL_TREE
))
6965 pedwarn (loc
, OPT_Wpedantic
, "a member of a structure or union cannot "
6966 "have a variably modified type");
6969 /* Aside from typedefs and type names (handle above),
6970 `void' at top level (not within pointer)
6971 is allowed only in public variables.
6972 We don't complain about parms either, but that is because
6973 a better error message can be made later. */
6975 if (VOID_TYPE_P (type
) && decl_context
!= PARM
6976 && !((decl_context
!= FIELD
&& TREE_CODE (type
) != FUNCTION_TYPE
)
6977 && (storage_class
== csc_extern
6978 || (current_scope
== file_scope
6979 && !(storage_class
== csc_static
6980 || storage_class
== csc_register
)))))
6982 error_at (loc
, "variable or field %qE declared void", name
);
6983 type
= integer_type_node
;
6986 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6987 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6992 if (decl_context
== PARM
)
6995 bool array_parameter_p
= false;
6997 /* A parameter declared as an array of T is really a pointer to T.
6998 One declared as a function is really a pointer to a function. */
7000 if (TREE_CODE (type
) == ARRAY_TYPE
)
7002 /* Transfer const-ness of array into that of type pointed to. */
7003 type
= TREE_TYPE (type
);
7004 if (orig_qual_type
!= NULL_TREE
)
7006 if (orig_qual_indirect
== 0)
7007 orig_qual_type
= TREE_TYPE (orig_qual_type
);
7009 orig_qual_indirect
--;
7012 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
7013 orig_qual_indirect
);
7014 type
= c_build_pointer_type (type
);
7015 type_quals
= array_ptr_quals
;
7017 type
= c_build_qualified_type (type
, type_quals
);
7019 /* We don't yet implement attributes in this context. */
7020 if (array_ptr_attrs
!= NULL_TREE
)
7021 warning_at (loc
, OPT_Wattributes
,
7022 "attributes in parameter array declarator ignored");
7024 size_varies
= false;
7025 array_parameter_p
= true;
7027 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
7029 if (type_quals
& TYPE_QUAL_ATOMIC
)
7032 "%<_Atomic%>-qualified function type");
7033 type_quals
&= ~TYPE_QUAL_ATOMIC
;
7035 else if (type_quals
)
7036 pedwarn (loc
, OPT_Wpedantic
,
7037 "ISO C forbids qualified function types");
7039 type
= c_build_qualified_type (type
, type_quals
);
7040 type
= c_build_pointer_type (type
);
7041 type_quals
= TYPE_UNQUALIFIED
;
7043 else if (type_quals
)
7044 type
= c_build_qualified_type (type
, type_quals
);
7046 decl
= build_decl (declarator
->id_loc
,
7047 PARM_DECL
, declarator
->u
.id
, type
);
7049 C_DECL_VARIABLE_SIZE (decl
) = 1;
7050 C_ARRAY_PARAMETER (decl
) = array_parameter_p
;
7052 /* Compute the type actually passed in the parmlist,
7053 for the case where there is no prototype.
7054 (For example, shorts and chars are passed as ints.)
7055 When there is a prototype, this is overridden later. */
7057 if (type
== error_mark_node
)
7058 promoted_type
= type
;
7060 promoted_type
= c_type_promotes_to (type
);
7062 DECL_ARG_TYPE (decl
) = promoted_type
;
7063 if (declspecs
->inline_p
)
7064 pedwarn (loc
, 0, "parameter %q+D declared %<inline%>", decl
);
7065 if (declspecs
->noreturn_p
)
7066 pedwarn (loc
, 0, "parameter %q+D declared %<_Noreturn%>", decl
);
7068 else if (decl_context
== FIELD
)
7070 /* Note that the grammar rejects storage classes in typenames
7072 gcc_assert (storage_class
== csc_none
&& !threadp
7073 && !declspecs
->inline_p
&& !declspecs
->noreturn_p
);
7075 /* Structure field. It may not be a function. */
7077 if (TREE_CODE (type
) == FUNCTION_TYPE
)
7079 error_at (loc
, "field %qE declared as a function", name
);
7080 type
= build_pointer_type (type
);
7082 else if (TREE_CODE (type
) != ERROR_MARK
7083 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type
))
7086 error_at (loc
, "field %qE has incomplete type", name
);
7088 error_at (loc
, "unnamed field has incomplete type");
7089 type
= error_mark_node
;
7091 else if (TREE_CODE (type
) == ARRAY_TYPE
7092 && TYPE_DOMAIN (type
) == NULL_TREE
)
7094 /* We have a flexible array member through a typedef.
7095 Set suitable range. Whether this is a correct position
7096 for a flexible array member will be determined elsewhere. */
7097 if (!in_system_header_at (input_location
))
7098 pedwarn_c90 (loc
, OPT_Wpedantic
, "ISO C90 does not "
7099 "support flexible array members");
7100 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
7101 TYPE_DOMAIN (type
) = build_range_type (sizetype
, size_zero_node
,
7103 if (orig_qual_indirect
== 0)
7104 orig_qual_type
= NULL_TREE
;
7106 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
7107 orig_qual_indirect
);
7108 decl
= build_decl (declarator
->id_loc
,
7109 FIELD_DECL
, declarator
->u
.id
, type
);
7110 DECL_NONADDRESSABLE_P (decl
) = bitfield
;
7111 if (bitfield
&& !declarator
->u
.id
)
7113 TREE_NO_WARNING (decl
) = 1;
7114 DECL_PADDING_P (decl
) = 1;
7118 C_DECL_VARIABLE_SIZE (decl
) = 1;
7120 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
7122 if (storage_class
== csc_register
|| threadp
)
7124 error_at (loc
, "invalid storage class for function %qE", name
);
7126 else if (current_scope
!= file_scope
)
7128 /* Function declaration not at file scope. Storage
7129 classes other than `extern' are not allowed, C99
7130 6.7.1p5, and `extern' makes no difference. However,
7131 GCC allows 'auto', perhaps with 'inline', to support
7132 nested functions. */
7133 if (storage_class
== csc_auto
)
7134 pedwarn (loc
, OPT_Wpedantic
,
7135 "invalid storage class for function %qE", name
);
7136 else if (storage_class
== csc_static
)
7138 error_at (loc
, "invalid storage class for function %qE", name
);
7140 storage_class
= declspecs
->storage_class
= csc_none
;
7146 decl
= build_decl (declarator
->id_loc
,
7147 FUNCTION_DECL
, declarator
->u
.id
, type
);
7148 decl
= build_decl_attribute_variant (decl
, decl_attr
);
7150 if (type_quals
& TYPE_QUAL_ATOMIC
)
7153 "%<_Atomic%>-qualified function type");
7154 type_quals
&= ~TYPE_QUAL_ATOMIC
;
7156 else if (pedantic
&& type_quals
&& !DECL_IN_SYSTEM_HEADER (decl
))
7157 pedwarn (loc
, OPT_Wpedantic
,
7158 "ISO C forbids qualified function types");
7160 /* Every function declaration is an external reference
7161 (DECL_EXTERNAL) except for those which are not at file
7162 scope and are explicitly declared "auto". This is
7163 forbidden by standard C (C99 6.7.1p5) and is interpreted by
7164 GCC to signify a forward declaration of a nested function. */
7165 if (storage_class
== csc_auto
&& current_scope
!= file_scope
)
7166 DECL_EXTERNAL (decl
) = 0;
7167 /* In C99, a function which is declared 'inline' with 'extern'
7168 is not an external reference (which is confusing). It
7169 means that the later definition of the function must be output
7170 in this file, C99 6.7.4p6. In GNU C89, a function declared
7171 'extern inline' is an external reference. */
7172 else if (declspecs
->inline_p
&& storage_class
!= csc_static
)
7173 DECL_EXTERNAL (decl
) = ((storage_class
== csc_extern
)
7174 == flag_gnu89_inline
);
7176 DECL_EXTERNAL (decl
) = !initialized
;
7178 /* Record absence of global scope for `static' or `auto'. */
7180 = !(storage_class
== csc_static
|| storage_class
== csc_auto
);
7182 /* For a function definition, record the argument information
7183 block where store_parm_decls will look for it. */
7185 current_function_arg_info
= arg_info
;
7187 if (declspecs
->default_int_p
)
7188 C_FUNCTION_IMPLICIT_INT (decl
) = 1;
7190 /* Record presence of `inline' and `_Noreturn', if it is
7192 if (flag_hosted
&& MAIN_NAME_P (declarator
->u
.id
))
7194 if (declspecs
->inline_p
)
7195 pedwarn (loc
, 0, "cannot inline function %<main%>");
7196 if (declspecs
->noreturn_p
)
7197 pedwarn (loc
, 0, "%<main%> declared %<_Noreturn%>");
7201 if (declspecs
->inline_p
)
7202 /* Record that the function is declared `inline'. */
7203 DECL_DECLARED_INLINE_P (decl
) = 1;
7204 if (declspecs
->noreturn_p
)
7207 pedwarn_c99 (loc
, OPT_Wpedantic
,
7208 "ISO C99 does not support %<_Noreturn%>");
7210 pedwarn_c99 (loc
, OPT_Wpedantic
,
7211 "ISO C90 does not support %<_Noreturn%>");
7212 TREE_THIS_VOLATILE (decl
) = 1;
7218 /* It's a variable. */
7219 /* An uninitialized decl with `extern' is a reference. */
7220 int extern_ref
= !initialized
&& storage_class
== csc_extern
;
7222 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
7223 orig_qual_indirect
);
7225 /* C99 6.2.2p7: It is invalid (compile-time undefined
7226 behavior) to create an 'extern' declaration for a
7227 variable if there is a global declaration that is
7228 'static' and the global declaration is not visible.
7229 (If the static declaration _is_ currently visible,
7230 the 'extern' declaration is taken to refer to that decl.) */
7231 if (extern_ref
&& current_scope
!= file_scope
)
7233 tree global_decl
= identifier_global_value (declarator
->u
.id
);
7234 tree visible_decl
= lookup_name (declarator
->u
.id
);
7237 && global_decl
!= visible_decl
7238 && VAR_P (global_decl
)
7239 && !TREE_PUBLIC (global_decl
))
7240 error_at (loc
, "variable previously declared %<static%> "
7241 "redeclared %<extern%>");
7244 decl
= build_decl (declarator
->id_loc
,
7245 VAR_DECL
, declarator
->u
.id
, type
);
7247 C_DECL_VARIABLE_SIZE (decl
) = 1;
7249 if (declspecs
->inline_p
)
7250 pedwarn (loc
, 0, "variable %q+D declared %<inline%>", decl
);
7251 if (declspecs
->noreturn_p
)
7252 pedwarn (loc
, 0, "variable %q+D declared %<_Noreturn%>", decl
);
7254 /* At file scope, an initialized extern declaration may follow
7255 a static declaration. In that case, DECL_EXTERNAL will be
7256 reset later in start_decl. */
7257 DECL_EXTERNAL (decl
) = (storage_class
== csc_extern
);
7259 /* At file scope, the presence of a `static' or `register' storage
7260 class specifier, or the absence of all storage class specifiers
7261 makes this declaration a definition (perhaps tentative). Also,
7262 the absence of `static' makes it public. */
7263 if (current_scope
== file_scope
)
7265 TREE_PUBLIC (decl
) = storage_class
!= csc_static
;
7266 TREE_STATIC (decl
) = !extern_ref
;
7268 /* Not at file scope, only `static' makes a static definition. */
7271 TREE_STATIC (decl
) = (storage_class
== csc_static
);
7272 TREE_PUBLIC (decl
) = extern_ref
;
7276 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
7279 if ((storage_class
== csc_extern
7280 || (storage_class
== csc_none
7281 && TREE_CODE (type
) == FUNCTION_TYPE
7283 && variably_modified_type_p (type
, NULL_TREE
))
7286 if (TREE_CODE (type
) == FUNCTION_TYPE
)
7287 error_at (loc
, "non-nested function with variably modified type");
7289 error_at (loc
, "object with variably modified type must have "
7293 /* Record `register' declaration for warnings on &
7294 and in case doing stupid register allocation. */
7296 if (storage_class
== csc_register
)
7298 C_DECL_REGISTER (decl
) = 1;
7299 DECL_REGISTER (decl
) = 1;
7302 /* Record constancy and volatility. */
7303 c_apply_type_quals_to_decl (type_quals
, decl
);
7305 /* Apply _Alignas specifiers. */
7308 SET_DECL_ALIGN (decl
, alignas_align
* BITS_PER_UNIT
);
7309 DECL_USER_ALIGN (decl
) = 1;
7312 /* If a type has volatile components, it should be stored in memory.
7313 Otherwise, the fact that those components are volatile
7314 will be ignored, and would even crash the compiler.
7315 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7316 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl
))
7317 && (VAR_P (decl
) || TREE_CODE (decl
) == PARM_DECL
7318 || TREE_CODE (decl
) == RESULT_DECL
))
7320 /* It is not an error for a structure with volatile fields to
7321 be declared register, but reset DECL_REGISTER since it
7322 cannot actually go in a register. */
7323 int was_reg
= C_DECL_REGISTER (decl
);
7324 C_DECL_REGISTER (decl
) = 0;
7325 DECL_REGISTER (decl
) = 0;
7326 c_mark_addressable (decl
);
7327 C_DECL_REGISTER (decl
) = was_reg
;
7330 /* This is the earliest point at which we might know the assembler
7331 name of a variable. Thus, if it's known before this, die horribly. */
7332 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl
)
7333 || !DECL_ASSEMBLER_NAME_SET_P (decl
));
7337 && TREE_PUBLIC (decl
)
7338 && TREE_STATIC (decl
)
7339 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
))
7340 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
7341 && TYPE_NAME (TREE_TYPE (decl
)) == NULL_TREE
)
7342 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
7343 ("non-local variable %qD with anonymous type is "
7344 "questionable in C++"),
7351 /* Decode the parameter-list info for a function type or function definition.
7352 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7353 if there is an identifier list instead of a parameter decl list).
7354 These two functions are separate because when a function returns
7355 or receives functions then each is called multiple times but the order
7356 of calls is different. The last call to `grokparms' is always the one
7357 that contains the formal parameter names of a function definition.
7359 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7361 FUNCDEF_FLAG is true for a function definition, false for
7362 a mere declaration. A nonempty identifier-list gets an error message
7363 when FUNCDEF_FLAG is false. */
7366 grokparms (struct c_arg_info
*arg_info
, bool funcdef_flag
)
7368 tree arg_types
= arg_info
->types
;
7370 if (funcdef_flag
&& arg_info
->had_vla_unspec
)
7372 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7374 error ("%<[*]%> not allowed in other than function prototype scope");
7377 if (arg_types
== NULL_TREE
&& !funcdef_flag
7378 && !in_system_header_at (input_location
))
7379 warning (OPT_Wstrict_prototypes
,
7380 "function declaration isn%'t a prototype");
7382 if (arg_types
== error_mark_node
)
7383 /* Don't set TYPE_ARG_TYPES in this case. */
7386 else if (arg_types
&& TREE_CODE (TREE_VALUE (arg_types
)) == IDENTIFIER_NODE
)
7390 pedwarn (input_location
, 0, "parameter names (without types) in "
7391 "function declaration");
7392 arg_info
->parms
= NULL_TREE
;
7395 arg_info
->parms
= arg_info
->types
;
7397 arg_info
->types
= NULL_TREE
;
7402 tree parm
, type
, typelt
;
7403 unsigned int parmno
;
7405 /* If there is a parameter of incomplete type in a definition,
7406 this is an error. In a declaration this is valid, and a
7407 struct or union type may be completed later, before any calls
7408 or definition of the function. In the case where the tag was
7409 first declared within the parameter list, a warning has
7410 already been given. If a parameter has void type, then
7411 however the function cannot be defined or called, so
7414 for (parm
= arg_info
->parms
, typelt
= arg_types
, parmno
= 1;
7416 parm
= DECL_CHAIN (parm
), typelt
= TREE_CHAIN (typelt
), parmno
++)
7418 type
= TREE_VALUE (typelt
);
7419 if (type
== error_mark_node
)
7422 if (!COMPLETE_TYPE_P (type
))
7426 if (DECL_NAME (parm
))
7427 error_at (input_location
,
7428 "parameter %u (%q+D) has incomplete type",
7431 error_at (DECL_SOURCE_LOCATION (parm
),
7432 "parameter %u has incomplete type",
7435 TREE_VALUE (typelt
) = error_mark_node
;
7436 TREE_TYPE (parm
) = error_mark_node
;
7437 arg_types
= NULL_TREE
;
7439 else if (VOID_TYPE_P (type
))
7441 if (DECL_NAME (parm
))
7442 warning_at (input_location
, 0,
7443 "parameter %u (%q+D) has void type",
7446 warning_at (DECL_SOURCE_LOCATION (parm
), 0,
7447 "parameter %u has void type",
7452 if (DECL_NAME (parm
) && TREE_USED (parm
))
7453 warn_if_shadowing (parm
);
7459 /* Allocate and initialize a c_arg_info structure from the parser's
7463 build_arg_info (void)
7465 struct c_arg_info
*ret
= XOBNEW (&parser_obstack
, struct c_arg_info
);
7466 ret
->parms
= NULL_TREE
;
7468 ret
->types
= NULL_TREE
;
7469 ret
->others
= NULL_TREE
;
7470 ret
->pending_sizes
= NULL
;
7471 ret
->had_vla_unspec
= 0;
7475 /* Take apart the current scope and return a c_arg_info structure with
7476 info on a parameter list just parsed.
7478 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7480 ELLIPSIS being true means the argument list ended in '...' so don't
7481 append a sentinel (void_list_node) to the end of the type-list.
7483 EXPR is NULL or an expression that needs to be evaluated for the
7484 side effects of array size expressions in the parameters. */
7487 get_parm_info (bool ellipsis
, tree expr
)
7489 struct c_binding
*b
= current_scope
->bindings
;
7490 struct c_arg_info
*arg_info
= build_arg_info ();
7492 tree parms
= NULL_TREE
;
7493 vec
<c_arg_tag
, va_gc
> *tags
= NULL
;
7494 tree types
= NULL_TREE
;
7495 tree others
= NULL_TREE
;
7497 bool gave_void_only_once_err
= false;
7499 arg_info
->had_vla_unspec
= current_scope
->had_vla_unspec
;
7501 /* The bindings in this scope must not get put into a block.
7502 We will take care of deleting the binding nodes. */
7503 current_scope
->bindings
= 0;
7505 /* This function is only called if there was *something* on the
7509 /* A parameter list consisting solely of 'void' indicates that the
7510 function takes no arguments. But if the 'void' is qualified
7511 (by 'const' or 'volatile'), or has a storage class specifier
7512 ('register'), then the behavior is undefined; issue an error.
7513 Typedefs for 'void' are OK (see DR#157). */
7514 if (b
->prev
== 0 /* one binding */
7515 && TREE_CODE (b
->decl
) == PARM_DECL
/* which is a parameter */
7516 && !DECL_NAME (b
->decl
) /* anonymous */
7517 && VOID_TYPE_P (TREE_TYPE (b
->decl
))) /* of void type */
7519 if (TYPE_QUALS (TREE_TYPE (b
->decl
)) != TYPE_UNQUALIFIED
7520 || C_DECL_REGISTER (b
->decl
))
7521 error_at (b
->locus
, "%<void%> as only parameter may not be qualified");
7523 /* There cannot be an ellipsis. */
7525 error_at (b
->locus
, "%<void%> must be the only parameter");
7527 arg_info
->types
= void_list_node
;
7532 types
= void_list_node
;
7534 /* Break up the bindings list into parms, tags, types, and others;
7535 apply sanity checks; purge the name-to-decl bindings. */
7538 tree decl
= b
->decl
;
7539 tree type
= TREE_TYPE (decl
);
7541 const char *keyword
;
7543 switch (TREE_CODE (decl
))
7548 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
7549 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
7552 /* Check for forward decls that never got their actual decl. */
7553 if (TREE_ASM_WRITTEN (decl
))
7555 "parameter %q+D has just a forward declaration", decl
);
7556 /* Check for (..., void, ...) and issue an error. */
7557 else if (VOID_TYPE_P (type
) && !DECL_NAME (decl
))
7559 if (!gave_void_only_once_err
)
7561 error_at (b
->locus
, "%<void%> must be the only parameter");
7562 gave_void_only_once_err
= true;
7567 /* Valid parameter, add it to the list. */
7568 DECL_CHAIN (decl
) = parms
;
7571 /* Since there is a prototype, args are passed in their
7572 declared types. The back end may override this later. */
7573 DECL_ARG_TYPE (decl
) = type
;
7574 types
= tree_cons (0, type
, types
);
7578 case ENUMERAL_TYPE
: keyword
= "enum"; goto tag
;
7579 case UNION_TYPE
: keyword
= "union"; goto tag
;
7580 case RECORD_TYPE
: keyword
= "struct"; goto tag
;
7582 /* Types may not have tag-names, in which case the type
7583 appears in the bindings list with b->id NULL. */
7586 gcc_assert (I_TAG_BINDING (b
->id
) == b
);
7587 I_TAG_BINDING (b
->id
) = b
->shadowed
;
7590 /* Warn about any struct, union or enum tags defined in a
7591 parameter list. The scope of such types is limited to
7592 the parameter list, which is rarely if ever desirable
7593 (it's impossible to call such a function with type-
7594 correct arguments). An anonymous union parm type is
7595 meaningful as a GNU extension, so don't warn for that. */
7596 if (TREE_CODE (decl
) != UNION_TYPE
|| b
->id
!= NULL_TREE
)
7599 /* The %s will be one of 'struct', 'union', or 'enum'. */
7600 warning_at (b
->locus
, 0,
7601 "%<%s %E%> declared inside parameter list"
7602 " will not be visible outside of this definition or"
7603 " declaration", keyword
, b
->id
);
7605 /* The %s will be one of 'struct', 'union', or 'enum'. */
7606 warning_at (b
->locus
, 0,
7607 "anonymous %s declared inside parameter list"
7608 " will not be visible outside of this definition or"
7609 " declaration", keyword
);
7614 vec_safe_push (tags
, tag
);
7618 /* FUNCTION_DECLs appear when there is an implicit function
7619 declaration in the parameter list. */
7620 gcc_assert (b
->nested
|| seen_error ());
7625 /* CONST_DECLs appear here when we have an embedded enum,
7626 and TYPE_DECLs appear here when we have an embedded struct
7627 or union. No warnings for this - we already warned about the
7630 /* When we reinsert this decl in the function body, we need
7631 to reconstruct whether it was marked as nested. */
7632 gcc_assert (!b
->nested
);
7633 DECL_CHAIN (decl
) = others
;
7639 /* error_mark_node appears here when we have an undeclared
7640 variable. Just throw it away. */
7643 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
7644 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
7648 /* Other things that might be encountered. */
7655 b
= free_binding_and_advance (b
);
7658 arg_info
->parms
= parms
;
7659 arg_info
->tags
= tags
;
7660 arg_info
->types
= types
;
7661 arg_info
->others
= others
;
7662 arg_info
->pending_sizes
= expr
;
7666 /* Get the struct, enum or union (CODE says which) with tag NAME.
7667 Define the tag as a forward-reference with location LOC if it is
7668 not defined. Return a c_typespec structure for the type
7672 parser_xref_tag (location_t loc
, enum tree_code code
, tree name
)
7674 struct c_typespec ret
;
7678 ret
.expr
= NULL_TREE
;
7679 ret
.expr_const_operands
= true;
7681 /* If a cross reference is requested, look up the type
7682 already defined for this tag and return it. */
7684 ref
= lookup_tag (code
, name
, false, &refloc
);
7685 /* If this is the right type of tag, return what we found.
7686 (This reference will be shadowed by shadow_tag later if appropriate.)
7687 If this is the wrong type of tag, do not return it. If it was the
7688 wrong type in the same scope, we will have had an error
7689 message already; if in a different scope and declaring
7690 a name, pending_xref_error will give an error message; but if in a
7691 different scope and not declaring a name, this tag should
7692 shadow the previous declaration of a different type of tag, and
7693 this would not work properly if we return the reference found.
7694 (For example, with "struct foo" in an outer scope, "union foo;"
7695 must shadow that tag with a new one of union type.) */
7696 ret
.kind
= (ref
? ctsk_tagref
: ctsk_tagfirstref
);
7697 if (ref
&& TREE_CODE (ref
) == code
)
7699 if (C_TYPE_DEFINED_IN_STRUCT (ref
)
7700 && loc
!= UNKNOWN_LOCATION
7706 warning_at (loc
, OPT_Wc___compat
,
7707 ("enum type defined in struct or union "
7708 "is not visible in C++"));
7709 inform (refloc
, "enum type defined here");
7712 warning_at (loc
, OPT_Wc___compat
,
7713 ("struct defined in struct or union "
7714 "is not visible in C++"));
7715 inform (refloc
, "struct defined here");
7718 warning_at (loc
, OPT_Wc___compat
,
7719 ("union defined in struct or union "
7720 "is not visible in C++"));
7721 inform (refloc
, "union defined here");
7732 /* If no such tag is yet defined, create a forward-reference node
7733 and record it as the "definition".
7734 When a real declaration of this type is found,
7735 the forward-reference will be altered into a real type. */
7737 ref
= make_node (code
);
7738 if (code
== ENUMERAL_TYPE
)
7740 /* Give the type a default layout like unsigned int
7741 to avoid crashing if it does not get defined. */
7742 SET_TYPE_MODE (ref
, TYPE_MODE (unsigned_type_node
));
7743 SET_TYPE_ALIGN (ref
, TYPE_ALIGN (unsigned_type_node
));
7744 TYPE_USER_ALIGN (ref
) = 0;
7745 TYPE_UNSIGNED (ref
) = 1;
7746 TYPE_PRECISION (ref
) = TYPE_PRECISION (unsigned_type_node
);
7747 TYPE_MIN_VALUE (ref
) = TYPE_MIN_VALUE (unsigned_type_node
);
7748 TYPE_MAX_VALUE (ref
) = TYPE_MAX_VALUE (unsigned_type_node
);
7751 pushtag (loc
, name
, ref
);
7757 /* Get the struct, enum or union (CODE says which) with tag NAME.
7758 Define the tag as a forward-reference if it is not defined.
7759 Return a tree for the type. */
7762 xref_tag (enum tree_code code
, tree name
)
7764 return parser_xref_tag (input_location
, code
, name
).spec
;
7767 /* Make sure that the tag NAME is defined *in the current scope*
7768 at least as a forward reference.
7769 LOC is the location of the struct's definition.
7770 CODE says which kind of tag NAME ought to be.
7772 This stores the current value of the file static STRUCT_PARSE_INFO
7773 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7774 new c_struct_parse_info structure. The old value of
7775 STRUCT_PARSE_INFO is restored in finish_struct. */
7778 start_struct (location_t loc
, enum tree_code code
, tree name
,
7779 class c_struct_parse_info
**enclosing_struct_parse_info
)
7781 /* If there is already a tag defined at this scope
7782 (as a forward reference), just return it. */
7784 tree ref
= NULL_TREE
;
7785 location_t refloc
= UNKNOWN_LOCATION
;
7787 if (name
!= NULL_TREE
)
7788 ref
= lookup_tag (code
, name
, true, &refloc
);
7789 if (ref
&& TREE_CODE (ref
) == code
)
7791 if (TYPE_STUB_DECL (ref
))
7792 refloc
= DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref
));
7794 if (TYPE_SIZE (ref
))
7796 if (code
== UNION_TYPE
)
7797 error_at (loc
, "redefinition of %<union %E%>", name
);
7799 error_at (loc
, "redefinition of %<struct %E%>", name
);
7800 if (refloc
!= UNKNOWN_LOCATION
)
7801 inform (refloc
, "originally defined here");
7802 /* Don't create structures using a name already in use. */
7805 else if (C_TYPE_BEING_DEFINED (ref
))
7807 if (code
== UNION_TYPE
)
7808 error_at (loc
, "nested redefinition of %<union %E%>", name
);
7810 error_at (loc
, "nested redefinition of %<struct %E%>", name
);
7811 /* Don't bother to report "originally defined here" for a
7812 nested redefinition; the original definition should be
7814 /* Don't create structures that contain themselves. */
7819 /* Otherwise create a forward-reference just so the tag is in scope. */
7821 if (ref
== NULL_TREE
|| TREE_CODE (ref
) != code
)
7823 ref
= make_node (code
);
7824 pushtag (loc
, name
, ref
);
7827 C_TYPE_BEING_DEFINED (ref
) = 1;
7828 for (tree v
= TYPE_MAIN_VARIANT (ref
); v
; v
= TYPE_NEXT_VARIANT (v
))
7829 TYPE_PACKED (v
) = flag_pack_struct
;
7831 *enclosing_struct_parse_info
= struct_parse_info
;
7832 struct_parse_info
= new c_struct_parse_info ();
7834 /* FIXME: This will issue a warning for a use of a type defined
7835 within a statement expr used within sizeof, et. al. This is not
7836 terribly serious as C++ doesn't permit statement exprs within
7838 if (warn_cxx_compat
&& (in_sizeof
|| in_typeof
|| in_alignof
))
7839 warning_at (loc
, OPT_Wc___compat
,
7840 "defining type in %qs expression is invalid in C++",
7843 : (in_typeof
? "typeof" : "alignof")));
7848 /* Process the specs, declarator and width (NULL if omitted)
7849 of a structure component, returning a FIELD_DECL node.
7850 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7851 DECL_ATTRS is as for grokdeclarator.
7853 LOC is the location of the structure component.
7855 This is done during the parsing of the struct declaration.
7856 The FIELD_DECL nodes are chained together and the lot of them
7857 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7860 grokfield (location_t loc
,
7861 struct c_declarator
*declarator
, struct c_declspecs
*declspecs
,
7862 tree width
, tree
*decl_attrs
)
7866 if (declarator
->kind
== cdk_id
&& declarator
->u
.id
== NULL_TREE
7867 && width
== NULL_TREE
)
7869 /* This is an unnamed decl.
7871 If we have something of the form "union { list } ;" then this
7872 is the anonymous union extension. Similarly for struct.
7874 If this is something of the form "struct foo;", then
7875 If MS or Plan 9 extensions are enabled, this is handled as
7876 an anonymous struct.
7877 Otherwise this is a forward declaration of a structure tag.
7879 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7880 If foo names a structure or union without a tag, then this
7881 is an anonymous struct (this is permitted by C11).
7882 If MS or Plan 9 extensions are enabled and foo names a
7883 structure, then again this is an anonymous struct.
7884 Otherwise this is an error.
7886 Oh what a horrid tangled web we weave. I wonder if MS consciously
7887 took this from Plan 9 or if it was an accident of implementation
7888 that took root before someone noticed the bug... */
7890 tree type
= declspecs
->type
;
7893 if (RECORD_OR_UNION_TYPE_P (type
)
7894 && (flag_ms_extensions
7895 || flag_plan9_extensions
7896 || !declspecs
->typedef_p
))
7898 if (flag_ms_extensions
|| flag_plan9_extensions
)
7900 else if (TYPE_NAME (type
) == NULL
)
7907 pedwarn (loc
, 0, "declaration does not declare anything");
7911 pedwarn_c99 (loc
, OPT_Wpedantic
,
7912 "ISO C99 doesn%'t support unnamed structs/unions");
7914 pedwarn_c99 (loc
, OPT_Wpedantic
,
7915 "ISO C90 doesn%'t support unnamed structs/unions");
7918 value
= grokdeclarator (declarator
, declspecs
, FIELD
, false,
7919 width
? &width
: NULL
, decl_attrs
, NULL
, NULL
,
7922 finish_decl (value
, loc
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
7923 DECL_INITIAL (value
) = width
;
7925 SET_DECL_C_BIT_FIELD (value
);
7927 if (warn_cxx_compat
&& DECL_NAME (value
) != NULL_TREE
)
7929 /* If we currently have a binding for this field, set the
7930 in_struct field in the binding, so that we warn about lookups
7932 struct c_binding
*b
= I_SYMBOL_BINDING (DECL_NAME (value
));
7935 /* If the in_struct field is not yet set, push it on a list
7936 to be cleared when this struct is finished. */
7939 struct_parse_info
->fields
.safe_push (b
);
7948 /* Subroutine of detect_field_duplicates: return whether X and Y,
7949 which are both fields in the same struct, have duplicate field
7953 is_duplicate_field (tree x
, tree y
)
7955 if (DECL_NAME (x
) != NULL_TREE
&& DECL_NAME (x
) == DECL_NAME (y
))
7958 /* When using -fplan9-extensions, an anonymous field whose name is a
7959 typedef can duplicate a field name. */
7960 if (flag_plan9_extensions
7961 && (DECL_NAME (x
) == NULL_TREE
|| DECL_NAME (y
) == NULL_TREE
))
7963 tree xt
, xn
, yt
, yn
;
7966 if (DECL_NAME (x
) != NULL_TREE
)
7968 else if (RECORD_OR_UNION_TYPE_P (xt
)
7969 && TYPE_NAME (xt
) != NULL_TREE
7970 && TREE_CODE (TYPE_NAME (xt
)) == TYPE_DECL
)
7971 xn
= DECL_NAME (TYPE_NAME (xt
));
7976 if (DECL_NAME (y
) != NULL_TREE
)
7978 else if (RECORD_OR_UNION_TYPE_P (yt
)
7979 && TYPE_NAME (yt
) != NULL_TREE
7980 && TREE_CODE (TYPE_NAME (yt
)) == TYPE_DECL
)
7981 yn
= DECL_NAME (TYPE_NAME (yt
));
7985 if (xn
!= NULL_TREE
&& xn
== yn
)
7992 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7993 to HTAB, giving errors for any duplicates. */
7996 detect_field_duplicates_hash (tree fieldlist
,
7997 hash_table
<nofree_ptr_hash
<tree_node
> > *htab
)
8002 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
8003 if ((y
= DECL_NAME (x
)) != NULL_TREE
)
8005 slot
= htab
->find_slot (y
, INSERT
);
8008 error ("duplicate member %q+D", x
);
8009 DECL_NAME (x
) = NULL_TREE
;
8013 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
8015 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x
)), htab
);
8017 /* When using -fplan9-extensions, an anonymous field whose
8018 name is a typedef can duplicate a field name. */
8019 if (flag_plan9_extensions
8020 && TYPE_NAME (TREE_TYPE (x
)) != NULL_TREE
8021 && TREE_CODE (TYPE_NAME (TREE_TYPE (x
))) == TYPE_DECL
)
8023 tree xn
= DECL_NAME (TYPE_NAME (TREE_TYPE (x
)));
8024 slot
= htab
->find_slot (xn
, INSERT
);
8026 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x
)));
8032 /* Generate an error for any duplicate field names in FIELDLIST. Munge
8033 the list such that this does not present a problem later. */
8036 detect_field_duplicates (tree fieldlist
)
8041 /* If the struct is the list of instance variables of an Objective-C
8042 class, then we need to check all the instance variables of
8043 superclasses when checking for duplicates (since you can't have
8044 an instance variable in a subclass with the same name as an
8045 instance variable in a superclass). We pass on this job to the
8046 Objective-C compiler. objc_detect_field_duplicates() will return
8047 false if we are not checking the list of instance variables and
8048 the C frontend should proceed with the standard field duplicate
8049 checks. If we are checking the list of instance variables, the
8050 ObjC frontend will do the check, emit the errors if needed, and
8051 then return true. */
8052 if (c_dialect_objc ())
8053 if (objc_detect_field_duplicates (false))
8056 /* First, see if there are more than "a few" fields.
8057 This is trivially true if there are zero or one fields. */
8058 if (!fieldlist
|| !DECL_CHAIN (fieldlist
))
8063 if (DECL_NAME (x
) == NULL_TREE
8064 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
8067 } while (timeout
> 0 && x
);
8069 /* If there were "few" fields and no anonymous structures or unions,
8070 avoid the overhead of allocating a hash table. Instead just do
8071 the nested traversal thing. */
8074 for (x
= DECL_CHAIN (fieldlist
); x
; x
= DECL_CHAIN (x
))
8075 /* When using -fplan9-extensions, we can have duplicates
8076 between typedef names and fields. */
8078 || (flag_plan9_extensions
8079 && DECL_NAME (x
) == NULL_TREE
8080 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
))
8081 && TYPE_NAME (TREE_TYPE (x
)) != NULL_TREE
8082 && TREE_CODE (TYPE_NAME (TREE_TYPE (x
))) == TYPE_DECL
))
8084 for (y
= fieldlist
; y
!= x
; y
= TREE_CHAIN (y
))
8085 if (is_duplicate_field (y
, x
))
8087 error ("duplicate member %q+D", x
);
8088 DECL_NAME (x
) = NULL_TREE
;
8094 hash_table
<nofree_ptr_hash
<tree_node
> > htab (37);
8095 detect_field_duplicates_hash (fieldlist
, &htab
);
8099 /* Finish up struct info used by -Wc++-compat. */
8102 warn_cxx_compat_finish_struct (tree fieldlist
, enum tree_code code
,
8103 location_t record_loc
)
8107 struct c_binding
*b
;
8109 if (fieldlist
== NULL_TREE
)
8111 if (code
== RECORD_TYPE
)
8112 warning_at (record_loc
, OPT_Wc___compat
,
8113 "empty struct has size 0 in C, size 1 in C++");
8115 warning_at (record_loc
, OPT_Wc___compat
,
8116 "empty union has size 0 in C, size 1 in C++");
8119 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
8120 the current struct. We do this now at the end of the struct
8121 because the flag is used to issue visibility warnings, and we
8122 only want to issue those warnings if the type is referenced
8123 outside of the struct declaration. */
8124 FOR_EACH_VEC_ELT (struct_parse_info
->struct_types
, ix
, x
)
8125 C_TYPE_DEFINED_IN_STRUCT (x
) = 1;
8127 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
8128 typedefs used when declaring fields in this struct. If the name
8129 of any of the fields is also a typedef name then the struct would
8130 not parse in C++, because the C++ lookup rules say that the
8131 typedef name would be looked up in the context of the struct, and
8132 would thus be the field rather than the typedef. */
8133 if (!struct_parse_info
->typedefs_seen
.is_empty ()
8134 && fieldlist
!= NULL_TREE
)
8136 /* Use a hash_set<tree> using the name of the typedef. We can use
8137 a hash_set<tree> because identifiers are interned. */
8138 hash_set
<tree
> tset
;
8140 FOR_EACH_VEC_ELT (struct_parse_info
->typedefs_seen
, ix
, x
)
8141 tset
.add (DECL_NAME (x
));
8143 for (x
= fieldlist
; x
!= NULL_TREE
; x
= DECL_CHAIN (x
))
8145 if (DECL_NAME (x
) != NULL_TREE
8146 && tset
.contains (DECL_NAME (x
)))
8148 warning_at (DECL_SOURCE_LOCATION (x
), OPT_Wc___compat
,
8149 ("using %qD as both field and typedef name is "
8152 /* FIXME: It would be nice to report the location where
8153 the typedef name is used. */
8158 /* For each field which has a binding and which was not defined in
8159 an enclosing struct, clear the in_struct field. */
8160 FOR_EACH_VEC_ELT (struct_parse_info
->fields
, ix
, b
)
8164 /* Function to help qsort sort FIELD_DECLs by name order. */
8167 field_decl_cmp (const void *x_p
, const void *y_p
)
8169 const tree
*const x
= (const tree
*) x_p
;
8170 const tree
*const y
= (const tree
*) y_p
;
8172 if (DECL_NAME (*x
) == DECL_NAME (*y
))
8173 /* A nontype is "greater" than a type. */
8174 return (TREE_CODE (*y
) == TYPE_DECL
) - (TREE_CODE (*x
) == TYPE_DECL
);
8175 if (DECL_NAME (*x
) == NULL_TREE
)
8177 if (DECL_NAME (*y
) == NULL_TREE
)
8179 if (DECL_NAME (*x
) < DECL_NAME (*y
))
8184 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
8185 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
8186 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
8187 ATTRIBUTES are attributes to be applied to the structure.
8189 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
8190 the struct was started. */
8193 finish_struct (location_t loc
, tree t
, tree fieldlist
, tree attributes
,
8194 class c_struct_parse_info
*enclosing_struct_parse_info
)
8197 bool toplevel
= file_scope
== current_scope
;
8199 /* If this type was previously laid out as a forward reference,
8200 make sure we lay it out again. */
8202 TYPE_SIZE (t
) = NULL_TREE
;
8204 decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
8208 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
8210 if (DECL_NAME (x
) != NULL_TREE
)
8212 if (flag_isoc11
&& RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
8218 if (TREE_CODE (t
) == UNION_TYPE
)
8221 pedwarn (loc
, OPT_Wpedantic
, "union has no named members");
8223 pedwarn (loc
, OPT_Wpedantic
, "union has no members");
8228 pedwarn (loc
, OPT_Wpedantic
, "struct has no named members");
8230 pedwarn (loc
, OPT_Wpedantic
, "struct has no members");
8235 /* Install struct as DECL_CONTEXT of each field decl.
8236 Also process specified field sizes, found in the DECL_INITIAL,
8237 storing 0 there after the type has been changed to precision equal
8238 to its width, rather than the precision of the specified standard
8239 type. (Correct layout requires the original type to have been preserved
8242 bool saw_named_field
= false;
8243 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
8245 if (TREE_TYPE (x
) == error_mark_node
)
8248 DECL_CONTEXT (x
) = t
;
8250 /* If any field is const, the structure type is pseudo-const. */
8251 if (TREE_READONLY (x
))
8252 C_TYPE_FIELDS_READONLY (t
) = 1;
8255 /* A field that is pseudo-const makes the structure likewise. */
8256 tree t1
= strip_array_types (TREE_TYPE (x
));
8257 if (RECORD_OR_UNION_TYPE_P (t1
) && C_TYPE_FIELDS_READONLY (t1
))
8258 C_TYPE_FIELDS_READONLY (t
) = 1;
8261 /* Any field that is volatile means variables of this type must be
8262 treated in some ways as volatile. */
8263 if (TREE_THIS_VOLATILE (x
))
8264 C_TYPE_FIELDS_VOLATILE (t
) = 1;
8266 /* Any field of nominal variable size implies structure is too. */
8267 if (C_DECL_VARIABLE_SIZE (x
))
8268 C_TYPE_VARIABLE_SIZE (t
) = 1;
8270 if (DECL_C_BIT_FIELD (x
))
8272 unsigned HOST_WIDE_INT width
= tree_to_uhwi (DECL_INITIAL (x
));
8273 DECL_SIZE (x
) = bitsize_int (width
);
8274 DECL_BIT_FIELD (x
) = 1;
8278 && (DECL_BIT_FIELD (x
)
8279 || TYPE_ALIGN (TREE_TYPE (x
)) > BITS_PER_UNIT
))
8280 DECL_PACKED (x
) = 1;
8282 /* Detect flexible array member in an invalid context. */
8283 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
8284 && TYPE_SIZE (TREE_TYPE (x
)) == NULL_TREE
8285 && TYPE_DOMAIN (TREE_TYPE (x
)) != NULL_TREE
8286 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x
))) == NULL_TREE
)
8288 if (TREE_CODE (t
) == UNION_TYPE
)
8290 error_at (DECL_SOURCE_LOCATION (x
),
8291 "flexible array member in union");
8292 TREE_TYPE (x
) = error_mark_node
;
8294 else if (DECL_CHAIN (x
) != NULL_TREE
)
8296 error_at (DECL_SOURCE_LOCATION (x
),
8297 "flexible array member not at end of struct");
8298 TREE_TYPE (x
) = error_mark_node
;
8300 else if (!saw_named_field
)
8302 error_at (DECL_SOURCE_LOCATION (x
),
8303 "flexible array member in a struct with no named "
8305 TREE_TYPE (x
) = error_mark_node
;
8309 if (pedantic
&& TREE_CODE (t
) == RECORD_TYPE
8310 && flexible_array_type_p (TREE_TYPE (x
)))
8311 pedwarn (DECL_SOURCE_LOCATION (x
), OPT_Wpedantic
,
8312 "invalid use of structure with flexible array member");
8315 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
8316 saw_named_field
= true;
8319 detect_field_duplicates (fieldlist
);
8321 /* Now we have the nearly final fieldlist. Record it,
8322 then lay out the structure or union (including the fields). */
8324 TYPE_FIELDS (t
) = fieldlist
;
8326 maybe_apply_pragma_scalar_storage_order (t
);
8330 if (TYPE_SIZE_UNIT (t
)
8331 && TREE_CODE (TYPE_SIZE_UNIT (t
)) == INTEGER_CST
8332 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t
))
8333 && !valid_constant_size_p (TYPE_SIZE_UNIT (t
)))
8334 error ("type %qT is too large", t
);
8336 /* Give bit-fields their proper types and rewrite the type of array fields
8337 with scalar component if the enclosing type has reverse storage order. */
8338 for (tree field
= fieldlist
; field
; field
= DECL_CHAIN (field
))
8340 if (TREE_CODE (field
) == FIELD_DECL
8341 && DECL_INITIAL (field
)
8342 && TREE_TYPE (field
) != error_mark_node
)
8344 unsigned HOST_WIDE_INT width
8345 = tree_to_uhwi (DECL_INITIAL (field
));
8346 tree type
= TREE_TYPE (field
);
8347 if (width
!= TYPE_PRECISION (type
))
8350 = c_build_bitfield_integer_type (width
, TYPE_UNSIGNED (type
));
8351 SET_DECL_MODE (field
, TYPE_MODE (TREE_TYPE (field
)));
8353 DECL_INITIAL (field
) = NULL_TREE
;
8355 else if (TYPE_REVERSE_STORAGE_ORDER (t
)
8356 && TREE_CODE (field
) == FIELD_DECL
8357 && TREE_CODE (TREE_TYPE (field
)) == ARRAY_TYPE
)
8359 tree ftype
= TREE_TYPE (field
);
8360 tree ctype
= strip_array_types (ftype
);
8361 if (!RECORD_OR_UNION_TYPE_P (ctype
) && TYPE_MODE (ctype
) != QImode
)
8363 tree fmain_type
= TYPE_MAIN_VARIANT (ftype
);
8364 tree
*typep
= &fmain_type
;
8366 *typep
= build_distinct_type_copy (*typep
);
8367 TYPE_REVERSE_STORAGE_ORDER (*typep
) = 1;
8368 typep
= &TREE_TYPE (*typep
);
8369 } while (TREE_CODE (*typep
) == ARRAY_TYPE
);
8371 = c_build_qualified_type (fmain_type
, TYPE_QUALS (ftype
));
8376 /* Now we have the truly final field list.
8377 Store it in this type and in the variants. */
8379 TYPE_FIELDS (t
) = fieldlist
;
8381 /* If there are lots of fields, sort so we can look through them fast.
8382 We arbitrarily consider 16 or more elts to be "a lot". */
8387 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
8389 if (len
> 15 || DECL_NAME (x
) == NULL
)
8397 struct lang_type
*space
;
8398 struct sorted_fields_type
*space2
;
8400 len
+= list_length (x
);
8402 /* Use the same allocation policy here that make_node uses, to
8403 ensure that this lives as long as the rest of the struct decl.
8404 All decls in an inline function need to be saved. */
8406 space
= ggc_cleared_alloc
<struct lang_type
> ();
8407 space2
= (sorted_fields_type
*) ggc_internal_alloc
8408 (sizeof (struct sorted_fields_type
) + len
* sizeof (tree
));
8412 field_array
= &space2
->elts
[0];
8413 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
8415 field_array
[len
++] = x
;
8417 /* If there is anonymous struct or union, break out of the loop. */
8418 if (DECL_NAME (x
) == NULL
)
8421 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8424 TYPE_LANG_SPECIFIC (t
) = space
;
8425 TYPE_LANG_SPECIFIC (t
)->s
->len
= len
;
8426 field_array
= TYPE_LANG_SPECIFIC (t
)->s
->elts
;
8427 qsort (field_array
, len
, sizeof (tree
), field_decl_cmp
);
8432 /* If this was supposed to be a transparent union, but we can't
8433 make it one, warn and turn off the flag. */
8434 if (TREE_CODE (t
) == UNION_TYPE
8435 && TYPE_TRANSPARENT_AGGR (t
)
8436 && (!TYPE_FIELDS (t
) || TYPE_MODE (t
) != DECL_MODE (TYPE_FIELDS (t
))))
8438 TYPE_TRANSPARENT_AGGR (t
) = 0;
8439 warning_at (loc
, 0, "union cannot be made transparent");
8442 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8443 in dwarf2out via rest_of_decl_compilation below and means
8444 something totally different. Since we will be clearing
8445 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8446 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8447 C_TYPE_INCOMPLETE_VARS should use some language specific
8449 tree incomplete_vars
= C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t
));
8450 for (x
= TYPE_MAIN_VARIANT (t
); x
; x
= TYPE_NEXT_VARIANT (x
))
8452 TYPE_FIELDS (x
) = TYPE_FIELDS (t
);
8453 TYPE_LANG_SPECIFIC (x
) = TYPE_LANG_SPECIFIC (t
);
8454 TYPE_TRANSPARENT_AGGR (x
) = TYPE_TRANSPARENT_AGGR (t
);
8455 C_TYPE_FIELDS_READONLY (x
) = C_TYPE_FIELDS_READONLY (t
);
8456 C_TYPE_FIELDS_VOLATILE (x
) = C_TYPE_FIELDS_VOLATILE (t
);
8457 C_TYPE_VARIABLE_SIZE (x
) = C_TYPE_VARIABLE_SIZE (t
);
8458 C_TYPE_INCOMPLETE_VARS (x
) = NULL_TREE
;
8461 /* Update type location to the one of the definition, instead of e.g.
8462 a forward declaration. */
8463 if (TYPE_STUB_DECL (t
))
8464 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t
)) = loc
;
8466 /* Finish debugging output for this type. */
8467 rest_of_type_compilation (t
, toplevel
);
8469 /* If this structure or union completes the type of any previous
8470 variable declaration, lay it out and output its rtl. */
8471 for (x
= incomplete_vars
; x
; x
= TREE_CHAIN (x
))
8473 tree decl
= TREE_VALUE (x
);
8474 if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
8475 layout_array_type (TREE_TYPE (decl
));
8476 if (TREE_CODE (decl
) != TYPE_DECL
)
8478 layout_decl (decl
, 0);
8479 if (c_dialect_objc ())
8480 objc_check_decl (decl
);
8481 rest_of_decl_compilation (decl
, toplevel
, 0);
8485 /* If we're inside a function proper, i.e. not file-scope and not still
8486 parsing parameters, then arrange for the size of a variable sized type
8488 if (building_stmt_list_p () && variably_modified_type_p (t
, NULL_TREE
))
8489 add_stmt (build_stmt (loc
,
8490 DECL_EXPR
, build_decl (loc
, TYPE_DECL
, NULL
, t
)));
8492 if (warn_cxx_compat
)
8493 warn_cxx_compat_finish_struct (fieldlist
, TREE_CODE (t
), loc
);
8495 delete struct_parse_info
;
8497 struct_parse_info
= enclosing_struct_parse_info
;
8499 /* If this struct is defined inside a struct, add it to
8502 && struct_parse_info
!= NULL
8503 && !in_sizeof
&& !in_typeof
&& !in_alignof
)
8504 struct_parse_info
->struct_types
.safe_push (t
);
8510 gt_pointer_operator new_value
;
8514 /* This routine compares two fields like field_decl_cmp but using the
8515 pointer operator in resort_data. */
8518 resort_field_decl_cmp (const void *x_p
, const void *y_p
)
8520 const tree
*const x
= (const tree
*) x_p
;
8521 const tree
*const y
= (const tree
*) y_p
;
8523 if (DECL_NAME (*x
) == DECL_NAME (*y
))
8524 /* A nontype is "greater" than a type. */
8525 return (TREE_CODE (*y
) == TYPE_DECL
) - (TREE_CODE (*x
) == TYPE_DECL
);
8526 if (DECL_NAME (*x
) == NULL_TREE
)
8528 if (DECL_NAME (*y
) == NULL_TREE
)
8531 tree d1
= DECL_NAME (*x
);
8532 tree d2
= DECL_NAME (*y
);
8533 resort_data
.new_value (&d1
, resort_data
.cookie
);
8534 resort_data
.new_value (&d2
, resort_data
.cookie
);
8541 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8544 resort_sorted_fields (void *obj
,
8545 void * ARG_UNUSED (orig_obj
),
8546 gt_pointer_operator new_value
,
8549 struct sorted_fields_type
*sf
= (struct sorted_fields_type
*) obj
;
8550 resort_data
.new_value
= new_value
;
8551 resort_data
.cookie
= cookie
;
8552 qsort (&sf
->elts
[0], sf
->len
, sizeof (tree
),
8553 resort_field_decl_cmp
);
8556 /* Lay out the type T, and its element type, and so on. */
8559 layout_array_type (tree t
)
8561 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
8562 layout_array_type (TREE_TYPE (t
));
8566 /* Begin compiling the definition of an enumeration type.
8567 NAME is its name (or null if anonymous).
8568 LOC is the enum's location.
8569 Returns the type object, as yet incomplete.
8570 Also records info about it so that build_enumerator
8571 may be used to declare the individual values as they are read. */
8574 start_enum (location_t loc
, struct c_enum_contents
*the_enum
, tree name
)
8576 tree enumtype
= NULL_TREE
;
8577 location_t enumloc
= UNKNOWN_LOCATION
;
8579 /* If this is the real definition for a previous forward reference,
8580 fill in the contents in the same object that used to be the
8581 forward reference. */
8583 if (name
!= NULL_TREE
)
8584 enumtype
= lookup_tag (ENUMERAL_TYPE
, name
, true, &enumloc
);
8586 if (enumtype
== NULL_TREE
|| TREE_CODE (enumtype
) != ENUMERAL_TYPE
)
8588 enumtype
= make_node (ENUMERAL_TYPE
);
8589 pushtag (loc
, name
, enumtype
);
8591 /* Update type location to the one of the definition, instead of e.g.
8592 a forward declaration. */
8593 else if (TYPE_STUB_DECL (enumtype
))
8595 enumloc
= DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype
));
8596 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype
)) = loc
;
8599 if (C_TYPE_BEING_DEFINED (enumtype
))
8600 error_at (loc
, "nested redefinition of %<enum %E%>", name
);
8602 C_TYPE_BEING_DEFINED (enumtype
) = 1;
8604 if (TYPE_VALUES (enumtype
) != NULL_TREE
)
8606 /* This enum is a named one that has been declared already. */
8607 error_at (loc
, "redeclaration of %<enum %E%>", name
);
8608 if (enumloc
!= UNKNOWN_LOCATION
)
8609 inform (enumloc
, "originally defined here");
8611 /* Completely replace its old definition.
8612 The old enumerators remain defined, however. */
8613 TYPE_VALUES (enumtype
) = NULL_TREE
;
8616 the_enum
->enum_next_value
= integer_zero_node
;
8617 the_enum
->enum_overflow
= 0;
8619 if (flag_short_enums
)
8620 for (tree v
= TYPE_MAIN_VARIANT (enumtype
); v
; v
= TYPE_NEXT_VARIANT (v
))
8621 TYPE_PACKED (v
) = 1;
8623 /* FIXME: This will issue a warning for a use of a type defined
8624 within sizeof in a statement expr. This is not terribly serious
8625 as C++ doesn't permit statement exprs within sizeof anyhow. */
8626 if (warn_cxx_compat
&& (in_sizeof
|| in_typeof
|| in_alignof
))
8627 warning_at (loc
, OPT_Wc___compat
,
8628 "defining type in %qs expression is invalid in C++",
8631 : (in_typeof
? "typeof" : "alignof")));
8636 /* After processing and defining all the values of an enumeration type,
8637 install their decls in the enumeration type and finish it off.
8638 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8639 and ATTRIBUTES are the specified attributes.
8640 Returns ENUMTYPE. */
8643 finish_enum (tree enumtype
, tree values
, tree attributes
)
8646 tree minnode
= NULL_TREE
, maxnode
= NULL_TREE
;
8649 bool toplevel
= (file_scope
== current_scope
);
8650 struct lang_type
*lt
;
8652 decl_attributes (&enumtype
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
8654 /* Calculate the maximum value of any enumerator in this type. */
8656 if (values
== error_mark_node
)
8657 minnode
= maxnode
= integer_zero_node
;
8660 minnode
= maxnode
= TREE_VALUE (values
);
8661 for (pair
= TREE_CHAIN (values
); pair
; pair
= TREE_CHAIN (pair
))
8663 tree value
= TREE_VALUE (pair
);
8664 if (tree_int_cst_lt (maxnode
, value
))
8666 if (tree_int_cst_lt (value
, minnode
))
8671 /* Construct the final type of this enumeration. It is the same
8672 as one of the integral types - the narrowest one that fits, except
8673 that normally we only go as narrow as int - and signed iff any of
8674 the values are negative. */
8675 sign
= (tree_int_cst_sgn (minnode
) >= 0) ? UNSIGNED
: SIGNED
;
8676 precision
= MAX (tree_int_cst_min_precision (minnode
, sign
),
8677 tree_int_cst_min_precision (maxnode
, sign
));
8679 /* If the precision of the type was specified with an attribute and it
8680 was too small, give an error. Otherwise, use it. */
8681 if (TYPE_PRECISION (enumtype
) && lookup_attribute ("mode", attributes
))
8683 if (precision
> TYPE_PRECISION (enumtype
))
8685 TYPE_PRECISION (enumtype
) = 0;
8686 error ("specified mode too small for enumerated values");
8689 precision
= TYPE_PRECISION (enumtype
);
8692 TYPE_PRECISION (enumtype
) = 0;
8694 if (TYPE_PACKED (enumtype
)
8695 || precision
> TYPE_PRECISION (integer_type_node
)
8696 || TYPE_PRECISION (enumtype
))
8698 tem
= c_common_type_for_size (precision
, sign
== UNSIGNED
? 1 : 0);
8701 warning (0, "enumeration values exceed range of largest integer");
8702 tem
= long_long_integer_type_node
;
8706 tem
= sign
== UNSIGNED
? unsigned_type_node
: integer_type_node
;
8708 TYPE_MIN_VALUE (enumtype
) = TYPE_MIN_VALUE (tem
);
8709 TYPE_MAX_VALUE (enumtype
) = TYPE_MAX_VALUE (tem
);
8710 TYPE_UNSIGNED (enumtype
) = TYPE_UNSIGNED (tem
);
8711 SET_TYPE_ALIGN (enumtype
, TYPE_ALIGN (tem
));
8712 TYPE_SIZE (enumtype
) = NULL_TREE
;
8713 TYPE_PRECISION (enumtype
) = TYPE_PRECISION (tem
);
8715 layout_type (enumtype
);
8717 if (values
!= error_mark_node
)
8719 /* Change the type of the enumerators to be the enum type. We
8720 need to do this irrespective of the size of the enum, for
8721 proper type checking. Replace the DECL_INITIALs of the
8722 enumerators, and the value slots of the list, with copies
8723 that have the enum type; they cannot be modified in place
8724 because they may be shared (e.g. integer_zero_node) Finally,
8725 change the purpose slots to point to the names of the decls. */
8726 for (pair
= values
; pair
; pair
= TREE_CHAIN (pair
))
8728 tree enu
= TREE_PURPOSE (pair
);
8729 tree ini
= DECL_INITIAL (enu
);
8731 TREE_TYPE (enu
) = enumtype
;
8733 /* The ISO C Standard mandates enumerators to have type int,
8734 even though the underlying type of an enum type is
8735 unspecified. However, GCC allows enumerators of any
8736 integer type as an extensions. build_enumerator()
8737 converts any enumerators that fit in an int to type int,
8738 to avoid promotions to unsigned types when comparing
8739 integers with enumerators that fit in the int range.
8740 When -pedantic is given, build_enumerator() would have
8741 already warned about those that don't fit. Here we
8742 convert the rest to the enumerator type. */
8743 if (TREE_TYPE (ini
) != integer_type_node
)
8744 ini
= convert (enumtype
, ini
);
8746 DECL_INITIAL (enu
) = ini
;
8747 TREE_PURPOSE (pair
) = DECL_NAME (enu
);
8748 TREE_VALUE (pair
) = ini
;
8751 TYPE_VALUES (enumtype
) = values
;
8754 /* Record the min/max values so that we can warn about bit-field
8755 enumerations that are too small for the values. */
8756 lt
= ggc_cleared_alloc
<struct lang_type
> ();
8757 lt
->enum_min
= minnode
;
8758 lt
->enum_max
= maxnode
;
8759 TYPE_LANG_SPECIFIC (enumtype
) = lt
;
8761 /* Fix up all variant types of this enum type. */
8762 for (tem
= TYPE_MAIN_VARIANT (enumtype
); tem
; tem
= TYPE_NEXT_VARIANT (tem
))
8764 if (tem
== enumtype
)
8766 TYPE_VALUES (tem
) = TYPE_VALUES (enumtype
);
8767 TYPE_MIN_VALUE (tem
) = TYPE_MIN_VALUE (enumtype
);
8768 TYPE_MAX_VALUE (tem
) = TYPE_MAX_VALUE (enumtype
);
8769 TYPE_SIZE (tem
) = TYPE_SIZE (enumtype
);
8770 TYPE_SIZE_UNIT (tem
) = TYPE_SIZE_UNIT (enumtype
);
8771 SET_TYPE_MODE (tem
, TYPE_MODE (enumtype
));
8772 TYPE_PRECISION (tem
) = TYPE_PRECISION (enumtype
);
8773 SET_TYPE_ALIGN (tem
, TYPE_ALIGN (enumtype
));
8774 TYPE_USER_ALIGN (tem
) = TYPE_USER_ALIGN (enumtype
);
8775 TYPE_UNSIGNED (tem
) = TYPE_UNSIGNED (enumtype
);
8776 TYPE_LANG_SPECIFIC (tem
) = TYPE_LANG_SPECIFIC (enumtype
);
8779 /* Finish debugging output for this type. */
8780 rest_of_type_compilation (enumtype
, toplevel
);
8782 /* If this enum is defined inside a struct, add it to
8785 && struct_parse_info
!= NULL
8786 && !in_sizeof
&& !in_typeof
&& !in_alignof
)
8787 struct_parse_info
->struct_types
.safe_push (enumtype
);
8789 C_TYPE_BEING_DEFINED (enumtype
) = 0;
8794 /* Build and install a CONST_DECL for one value of the
8795 current enumeration type (one that was begun with start_enum).
8796 DECL_LOC is the location of the enumerator.
8797 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8798 Return a tree-list containing the CONST_DECL and its value.
8799 Assignment of sequential values by default is handled here. */
8802 build_enumerator (location_t decl_loc
, location_t loc
,
8803 struct c_enum_contents
*the_enum
, tree name
, tree value
)
8807 /* Validate and default VALUE. */
8809 if (value
!= NULL_TREE
)
8811 /* Don't issue more errors for error_mark_node (i.e. an
8812 undeclared identifier) - just ignore the value expression. */
8813 if (value
== error_mark_node
)
8815 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value
)))
8817 error_at (loc
, "enumerator value for %qE is not an integer constant",
8823 if (TREE_CODE (value
) != INTEGER_CST
)
8825 value
= c_fully_fold (value
, false, NULL
);
8826 if (TREE_CODE (value
) == INTEGER_CST
)
8827 pedwarn (loc
, OPT_Wpedantic
,
8828 "enumerator value for %qE is not an integer "
8829 "constant expression", name
);
8831 if (TREE_CODE (value
) != INTEGER_CST
)
8833 error ("enumerator value for %qE is not an integer constant",
8839 value
= default_conversion (value
);
8840 constant_expression_warning (value
);
8845 /* Default based on previous value. */
8846 /* It should no longer be possible to have NON_LVALUE_EXPR
8848 if (value
== NULL_TREE
)
8850 value
= the_enum
->enum_next_value
;
8851 if (the_enum
->enum_overflow
)
8852 error_at (loc
, "overflow in enumeration values");
8854 /* Even though the underlying type of an enum is unspecified, the
8855 type of enumeration constants is explicitly defined as int
8856 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8858 else if (!int_fits_type_p (value
, integer_type_node
))
8859 pedwarn (loc
, OPT_Wpedantic
,
8860 "ISO C restricts enumerator values to range of %<int%>");
8862 /* The ISO C Standard mandates enumerators to have type int, even
8863 though the underlying type of an enum type is unspecified.
8864 However, GCC allows enumerators of any integer type as an
8865 extensions. Here we convert any enumerators that fit in an int
8866 to type int, to avoid promotions to unsigned types when comparing
8867 integers with enumerators that fit in the int range. When
8868 -pedantic is given, we would have already warned about those that
8869 don't fit. We have to do this here rather than in finish_enum
8870 because this value may be used to define more enumerators. */
8871 if (int_fits_type_p (value
, integer_type_node
))
8872 value
= convert (integer_type_node
, value
);
8874 /* Set basis for default for next value. */
8875 the_enum
->enum_next_value
8876 = build_binary_op (EXPR_LOC_OR_LOC (value
, input_location
),
8877 PLUS_EXPR
, value
, integer_one_node
, false);
8878 the_enum
->enum_overflow
= tree_int_cst_lt (the_enum
->enum_next_value
, value
);
8880 /* Now create a declaration for the enum value name. */
8882 type
= TREE_TYPE (value
);
8883 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
8884 TYPE_PRECISION (integer_type_node
)),
8885 (TYPE_PRECISION (type
)
8886 >= TYPE_PRECISION (integer_type_node
)
8887 && TYPE_UNSIGNED (type
)));
8889 decl
= build_decl (decl_loc
, CONST_DECL
, name
, type
);
8890 DECL_INITIAL (decl
) = convert (type
, value
);
8893 return tree_cons (decl
, value
, NULL_TREE
);
8897 /* Create the FUNCTION_DECL for a function definition.
8898 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8899 the declaration; they describe the function's name and the type it returns,
8900 but twisted together in a fashion that parallels the syntax of C.
8902 This function creates a binding context for the function body
8903 as well as setting up the FUNCTION_DECL in current_function_decl.
8905 Returns true on success. If the DECLARATOR is not suitable for a function
8906 (it defines a datum instead), we return false to report a parse error. */
8909 start_function (struct c_declspecs
*declspecs
, struct c_declarator
*declarator
,
8912 tree decl1
, old_decl
;
8913 tree restype
, resdecl
;
8916 current_function_returns_value
= 0; /* Assume, until we see it does. */
8917 current_function_returns_null
= 0;
8918 current_function_returns_abnormally
= 0;
8919 warn_about_return_type
= 0;
8920 c_switch_stack
= NULL
;
8922 /* Indicate no valid break/continue context by setting these variables
8923 to some non-null, non-label value. We'll notice and emit the proper
8924 error message in c_finish_bc_stmt. */
8925 c_break_label
= c_cont_label
= size_zero_node
;
8927 decl1
= grokdeclarator (declarator
, declspecs
, FUNCDEF
, true, NULL
,
8928 &attributes
, NULL
, NULL
, DEPRECATED_NORMAL
);
8929 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION
, decl1
);
8931 /* If the declarator is not suitable for a function definition,
8932 cause a syntax error. */
8933 if (decl1
== NULL_TREE
8934 || TREE_CODE (decl1
) != FUNCTION_DECL
)
8937 loc
= DECL_SOURCE_LOCATION (decl1
);
8939 /* A nested function is not global. */
8940 if (current_function_decl
!= NULL_TREE
)
8941 TREE_PUBLIC (decl1
) = 0;
8943 c_decl_attributes (&decl1
, attributes
, 0);
8945 if (DECL_DECLARED_INLINE_P (decl1
)
8946 && DECL_UNINLINABLE (decl1
)
8947 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1
)))
8948 warning_at (loc
, OPT_Wattributes
,
8949 "inline function %qD given attribute %qs",
8952 /* Handle gnu_inline attribute. */
8953 if (declspecs
->inline_p
8954 && !flag_gnu89_inline
8955 && TREE_CODE (decl1
) == FUNCTION_DECL
8956 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1
))
8957 || current_function_decl
))
8959 if (declspecs
->storage_class
!= csc_static
)
8960 DECL_EXTERNAL (decl1
) = !DECL_EXTERNAL (decl1
);
8963 announce_function (decl1
);
8965 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1
))))
8967 error_at (loc
, "return type is an incomplete type");
8968 /* Make it return void instead. */
8970 = build_function_type (void_type_node
,
8971 TYPE_ARG_TYPES (TREE_TYPE (decl1
)));
8974 if (warn_about_return_type
)
8975 warn_defaults_to (loc
, flag_isoc99
? OPT_Wimplicit_int
8976 : (warn_return_type
> 0 ? OPT_Wreturn_type
8977 : OPT_Wimplicit_int
),
8978 "return type defaults to %<int%>");
8980 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8981 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8982 DECL_INITIAL (decl1
) = error_mark_node
;
8984 /* If this definition isn't a prototype and we had a prototype declaration
8985 before, copy the arg type info from that prototype. */
8986 old_decl
= lookup_name_in_scope (DECL_NAME (decl1
), current_scope
);
8987 if (old_decl
&& TREE_CODE (old_decl
) != FUNCTION_DECL
)
8988 old_decl
= NULL_TREE
;
8989 current_function_prototype_locus
= UNKNOWN_LOCATION
;
8990 current_function_prototype_built_in
= false;
8991 current_function_prototype_arg_types
= NULL_TREE
;
8992 if (!prototype_p (TREE_TYPE (decl1
)))
8994 if (old_decl
!= NULL_TREE
8995 && TREE_CODE (TREE_TYPE (old_decl
)) == FUNCTION_TYPE
8996 && comptypes (TREE_TYPE (TREE_TYPE (decl1
)),
8997 TREE_TYPE (TREE_TYPE (old_decl
))))
8999 if (stdarg_p (TREE_TYPE (old_decl
)))
9001 auto_diagnostic_group d
;
9002 warning_at (loc
, 0, "%q+D defined as variadic function "
9003 "without prototype", decl1
);
9004 locate_old_decl (old_decl
);
9006 TREE_TYPE (decl1
) = composite_type (TREE_TYPE (old_decl
),
9008 current_function_prototype_locus
= DECL_SOURCE_LOCATION (old_decl
);
9009 current_function_prototype_built_in
9010 = C_DECL_BUILTIN_PROTOTYPE (old_decl
);
9011 current_function_prototype_arg_types
9012 = TYPE_ARG_TYPES (TREE_TYPE (decl1
));
9014 if (TREE_PUBLIC (decl1
))
9016 /* If there is an external prototype declaration of this
9017 function, record its location but do not copy information
9018 to this decl. This may be an invisible declaration
9019 (built-in or in a scope which has finished) or simply
9020 have more refined argument types than any declaration
9022 struct c_binding
*b
;
9023 for (b
= I_SYMBOL_BINDING (DECL_NAME (decl1
)); b
; b
= b
->shadowed
)
9024 if (B_IN_SCOPE (b
, external_scope
))
9028 tree ext_decl
, ext_type
;
9030 ext_type
= b
->u
.type
? b
->u
.type
: TREE_TYPE (ext_decl
);
9031 if (TREE_CODE (ext_type
) == FUNCTION_TYPE
9032 && comptypes (TREE_TYPE (TREE_TYPE (decl1
)),
9033 TREE_TYPE (ext_type
)))
9035 current_function_prototype_locus
9036 = DECL_SOURCE_LOCATION (ext_decl
);
9037 current_function_prototype_built_in
9038 = C_DECL_BUILTIN_PROTOTYPE (ext_decl
);
9039 current_function_prototype_arg_types
9040 = TYPE_ARG_TYPES (ext_type
);
9046 /* Optionally warn of old-fashioned def with no previous prototype. */
9047 if (warn_strict_prototypes
9048 && old_decl
!= error_mark_node
9049 && !prototype_p (TREE_TYPE (decl1
))
9050 && C_DECL_ISNT_PROTOTYPE (old_decl
))
9051 warning_at (loc
, OPT_Wstrict_prototypes
,
9052 "function declaration isn%'t a prototype");
9053 /* Optionally warn of any global def with no previous prototype. */
9054 else if (warn_missing_prototypes
9055 && old_decl
!= error_mark_node
9056 && TREE_PUBLIC (decl1
)
9057 && !MAIN_NAME_P (DECL_NAME (decl1
))
9058 && C_DECL_ISNT_PROTOTYPE (old_decl
)
9059 && !DECL_DECLARED_INLINE_P (decl1
))
9060 warning_at (loc
, OPT_Wmissing_prototypes
,
9061 "no previous prototype for %qD", decl1
);
9062 /* Optionally warn of any def with no previous prototype
9063 if the function has already been used. */
9064 else if (warn_missing_prototypes
9065 && old_decl
!= NULL_TREE
9066 && old_decl
!= error_mark_node
9067 && TREE_USED (old_decl
)
9068 && !prototype_p (TREE_TYPE (old_decl
)))
9069 warning_at (loc
, OPT_Wmissing_prototypes
,
9070 "%qD was used with no prototype before its definition", decl1
);
9071 /* Optionally warn of any global def with no previous declaration. */
9072 else if (warn_missing_declarations
9073 && TREE_PUBLIC (decl1
)
9074 && old_decl
== NULL_TREE
9075 && !MAIN_NAME_P (DECL_NAME (decl1
))
9076 && !DECL_DECLARED_INLINE_P (decl1
))
9077 warning_at (loc
, OPT_Wmissing_declarations
,
9078 "no previous declaration for %qD",
9080 /* Optionally warn of any def with no previous declaration
9081 if the function has already been used. */
9082 else if (warn_missing_declarations
9083 && old_decl
!= NULL_TREE
9084 && old_decl
!= error_mark_node
9085 && TREE_USED (old_decl
)
9086 && C_DECL_IMPLICIT (old_decl
))
9087 warning_at (loc
, OPT_Wmissing_declarations
,
9088 "%qD was used with no declaration before its definition", decl1
);
9090 /* This function exists in static storage.
9091 (This does not mean `static' in the C sense!) */
9092 TREE_STATIC (decl1
) = 1;
9094 /* This is the earliest point at which we might know the assembler
9095 name of the function. Thus, if it's set before this, die horribly. */
9096 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1
));
9098 /* If #pragma weak was used, mark the decl weak now. */
9099 if (current_scope
== file_scope
)
9100 maybe_apply_pragma_weak (decl1
);
9102 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
9103 if (warn_main
&& MAIN_NAME_P (DECL_NAME (decl1
)))
9105 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1
)))
9106 != integer_type_node
)
9107 pedwarn (loc
, OPT_Wmain
, "return type of %qD is not %<int%>", decl1
);
9108 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1
))))
9109 pedwarn (loc
, OPT_Wmain
, "%<_Atomic%>-qualified return type of %qD",
9112 check_main_parameter_types (decl1
);
9114 if (!TREE_PUBLIC (decl1
))
9115 pedwarn (loc
, OPT_Wmain
,
9116 "%qD is normally a non-static function", decl1
);
9119 /* Record the decl so that the function name is defined.
9120 If we already have a decl for this name, and it is a FUNCTION_DECL,
9121 use the old decl. */
9123 current_function_decl
= pushdecl (decl1
);
9126 declare_parm_level ();
9128 restype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
9129 resdecl
= build_decl (loc
, RESULT_DECL
, NULL_TREE
, restype
);
9130 DECL_ARTIFICIAL (resdecl
) = 1;
9131 DECL_IGNORED_P (resdecl
) = 1;
9132 DECL_RESULT (current_function_decl
) = resdecl
;
9134 start_fname_decls ();
9139 /* Subroutine of store_parm_decls which handles new-style function
9140 definitions (prototype format). The parms already have decls, so we
9141 need only record them as in effect and complain if any redundant
9142 old-style parm decls were written. */
9144 store_parm_decls_newstyle (tree fndecl
, const struct c_arg_info
*arg_info
)
9150 if (current_scope
->bindings
)
9152 error_at (DECL_SOURCE_LOCATION (fndecl
),
9153 "old-style parameter declarations in prototyped "
9154 "function definition");
9156 /* Get rid of the old-style declarations. */
9160 /* Don't issue this warning for nested functions, and don't issue this
9161 warning if we got here because ARG_INFO_TYPES was error_mark_node
9162 (this happens when a function definition has just an ellipsis in
9163 its parameter list). */
9164 else if (!in_system_header_at (input_location
)
9165 && !current_function_scope
9166 && arg_info
->types
!= error_mark_node
)
9167 warning_at (DECL_SOURCE_LOCATION (fndecl
), OPT_Wtraditional
,
9168 "traditional C rejects ISO C style function definitions");
9170 /* Now make all the parameter declarations visible in the function body.
9171 We can bypass most of the grunt work of pushdecl. */
9172 for (decl
= arg_info
->parms
; decl
; decl
= DECL_CHAIN (decl
))
9174 DECL_CONTEXT (decl
) = current_function_decl
;
9175 if (DECL_NAME (decl
))
9177 bind (DECL_NAME (decl
), decl
, current_scope
,
9178 /*invisible=*/false, /*nested=*/false,
9180 if (!TREE_USED (decl
))
9181 warn_if_shadowing (decl
);
9184 error_at (DECL_SOURCE_LOCATION (decl
), "parameter name omitted");
9187 /* Record the parameter list in the function declaration. */
9188 DECL_ARGUMENTS (fndecl
) = arg_info
->parms
;
9190 /* Now make all the ancillary declarations visible, likewise. */
9191 for (decl
= arg_info
->others
; decl
; decl
= DECL_CHAIN (decl
))
9193 DECL_CONTEXT (decl
) = current_function_decl
;
9194 if (DECL_NAME (decl
))
9195 bind (DECL_NAME (decl
), decl
, current_scope
,
9196 /*invisible=*/false,
9197 /*nested=*/(TREE_CODE (decl
) == FUNCTION_DECL
),
9201 /* And all the tag declarations. */
9202 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info
->tags
, ix
, tag
)
9204 bind (tag
->id
, tag
->type
, current_scope
,
9205 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
9208 /* Subroutine of store_parm_decls which handles old-style function
9209 definitions (separate parameter list and declarations). */
9212 store_parm_decls_oldstyle (tree fndecl
, const struct c_arg_info
*arg_info
)
9214 struct c_binding
*b
;
9215 tree parm
, decl
, last
;
9216 tree parmids
= arg_info
->parms
;
9217 hash_set
<tree
> seen_args
;
9219 if (!in_system_header_at (input_location
))
9220 warning_at (DECL_SOURCE_LOCATION (fndecl
),
9221 OPT_Wold_style_definition
, "old-style function definition");
9223 /* Match each formal parameter name with its declaration. Save each
9224 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
9225 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
9227 if (TREE_VALUE (parm
) == NULL_TREE
)
9229 error_at (DECL_SOURCE_LOCATION (fndecl
),
9230 "parameter name missing from parameter list");
9231 TREE_PURPOSE (parm
) = NULL_TREE
;
9235 b
= I_SYMBOL_BINDING (TREE_VALUE (parm
));
9236 if (b
&& B_IN_CURRENT_SCOPE (b
))
9239 /* Skip erroneous parameters. */
9240 if (decl
== error_mark_node
)
9242 /* If we got something other than a PARM_DECL it is an error. */
9243 if (TREE_CODE (decl
) != PARM_DECL
)
9245 error_at (DECL_SOURCE_LOCATION (decl
),
9246 "%qD declared as a non-parameter", decl
);
9249 /* If the declaration is already marked, we have a duplicate
9250 name. Complain and ignore the duplicate. */
9251 else if (seen_args
.contains (decl
))
9253 error_at (DECL_SOURCE_LOCATION (decl
),
9254 "multiple parameters named %qD", decl
);
9255 TREE_PURPOSE (parm
) = NULL_TREE
;
9258 /* If the declaration says "void", complain and turn it into
9260 else if (VOID_TYPE_P (TREE_TYPE (decl
)))
9262 error_at (DECL_SOURCE_LOCATION (decl
),
9263 "parameter %qD declared with void type", decl
);
9264 TREE_TYPE (decl
) = integer_type_node
;
9265 DECL_ARG_TYPE (decl
) = integer_type_node
;
9266 layout_decl (decl
, 0);
9268 warn_if_shadowing (decl
);
9270 /* If no declaration found, default to int. */
9273 /* FIXME diagnostics: This should be the location of the argument,
9274 not the FNDECL. E.g., for an old-style declaration
9276 int f10(v) { blah; }
9278 We should use the location of the V, not the F10.
9279 Unfortunately, the V is an IDENTIFIER_NODE which has no
9280 location. In the future we need locations for c_arg_info
9283 See gcc.dg/Wshadow-3.c for an example of this problem. */
9284 decl
= build_decl (DECL_SOURCE_LOCATION (fndecl
),
9285 PARM_DECL
, TREE_VALUE (parm
), integer_type_node
);
9286 DECL_ARG_TYPE (decl
) = TREE_TYPE (decl
);
9288 warn_if_shadowing (decl
);
9291 pedwarn (DECL_SOURCE_LOCATION (decl
),
9292 OPT_Wimplicit_int
, "type of %qD defaults to %<int%>",
9295 warning_at (DECL_SOURCE_LOCATION (decl
),
9296 OPT_Wmissing_parameter_type
,
9297 "type of %qD defaults to %<int%>", decl
);
9300 TREE_PURPOSE (parm
) = decl
;
9301 seen_args
.add (decl
);
9304 /* Now examine the parms chain for incomplete declarations
9305 and declarations with no corresponding names. */
9307 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
9310 if (TREE_CODE (parm
) != PARM_DECL
)
9313 if (TREE_TYPE (parm
) != error_mark_node
9314 && !COMPLETE_TYPE_P (TREE_TYPE (parm
)))
9316 error_at (DECL_SOURCE_LOCATION (parm
),
9317 "parameter %qD has incomplete type", parm
);
9318 TREE_TYPE (parm
) = error_mark_node
;
9321 if (!seen_args
.contains (parm
))
9323 error_at (DECL_SOURCE_LOCATION (parm
),
9324 "declaration for parameter %qD but no such parameter",
9327 /* Pretend the parameter was not missing.
9328 This gets us to a standard state and minimizes
9329 further error messages. */
9330 parmids
= chainon (parmids
, tree_cons (parm
, 0, 0));
9334 /* Chain the declarations together in the order of the list of
9335 names. Store that chain in the function decl, replacing the
9336 list of names. Update the current scope to match. */
9337 DECL_ARGUMENTS (fndecl
) = NULL_TREE
;
9339 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
9340 if (TREE_PURPOSE (parm
))
9342 if (parm
&& TREE_PURPOSE (parm
))
9344 last
= TREE_PURPOSE (parm
);
9345 DECL_ARGUMENTS (fndecl
) = last
;
9347 for (parm
= TREE_CHAIN (parm
); parm
; parm
= TREE_CHAIN (parm
))
9348 if (TREE_PURPOSE (parm
))
9350 DECL_CHAIN (last
) = TREE_PURPOSE (parm
);
9351 last
= TREE_PURPOSE (parm
);
9353 DECL_CHAIN (last
) = NULL_TREE
;
9356 /* If there was a previous prototype,
9357 set the DECL_ARG_TYPE of each argument according to
9358 the type previously specified, and report any mismatches. */
9360 if (current_function_prototype_arg_types
)
9363 for (parm
= DECL_ARGUMENTS (fndecl
),
9364 type
= current_function_prototype_arg_types
;
9365 parm
|| (type
!= NULL_TREE
9366 && TREE_VALUE (type
) != error_mark_node
9367 && TYPE_MAIN_VARIANT (TREE_VALUE (type
)) != void_type_node
);
9368 parm
= DECL_CHAIN (parm
), type
= TREE_CHAIN (type
))
9370 if (parm
== NULL_TREE
9371 || type
== NULL_TREE
9372 || (TREE_VALUE (type
) != error_mark_node
9373 && TYPE_MAIN_VARIANT (TREE_VALUE (type
)) == void_type_node
))
9375 if (current_function_prototype_built_in
)
9376 warning_at (DECL_SOURCE_LOCATION (fndecl
),
9377 0, "number of arguments doesn%'t match "
9378 "built-in prototype");
9381 /* FIXME diagnostics: This should be the location of
9382 FNDECL, but there is bug when a prototype is
9383 declared inside function context, but defined
9384 outside of it (e.g., gcc.dg/pr15698-2.c). In
9385 which case FNDECL gets the location of the
9386 prototype, not the definition. */
9387 error_at (input_location
,
9388 "number of arguments doesn%'t match prototype");
9390 error_at (current_function_prototype_locus
,
9391 "prototype declaration");
9395 /* Type for passing arg must be consistent with that
9396 declared for the arg. ISO C says we take the unqualified
9397 type for parameters declared with qualified type. */
9398 if (TREE_TYPE (parm
) != error_mark_node
9399 && TREE_VALUE (type
) != error_mark_node
9400 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm
))
9401 != TYPE_ATOMIC (TREE_VALUE (type
)))
9402 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm
)),
9403 TYPE_MAIN_VARIANT (TREE_VALUE (type
)))))
9405 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm
))
9406 == TYPE_ATOMIC (TREE_VALUE (type
)))
9407 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm
))
9408 == TYPE_MAIN_VARIANT (TREE_VALUE (type
))))
9410 /* Adjust argument to match prototype. E.g. a previous
9411 `int foo(float);' prototype causes
9412 `int foo(x) float x; {...}' to be treated like
9413 `int foo(float x) {...}'. This is particularly
9414 useful for argument types like uid_t. */
9415 DECL_ARG_TYPE (parm
) = TREE_TYPE (parm
);
9417 if (targetm
.calls
.promote_prototypes (TREE_TYPE (current_function_decl
))
9418 && INTEGRAL_TYPE_P (TREE_TYPE (parm
))
9419 && (TYPE_PRECISION (TREE_TYPE (parm
))
9420 < TYPE_PRECISION (integer_type_node
)))
9421 DECL_ARG_TYPE (parm
)
9422 = c_type_promotes_to (TREE_TYPE (parm
));
9424 /* ??? Is it possible to get here with a
9425 built-in prototype or will it always have
9426 been diagnosed as conflicting with an
9427 old-style definition and discarded? */
9428 if (current_function_prototype_built_in
)
9429 warning_at (DECL_SOURCE_LOCATION (parm
),
9430 OPT_Wpedantic
, "promoted argument %qD "
9431 "doesn%'t match built-in prototype", parm
);
9434 pedwarn (DECL_SOURCE_LOCATION (parm
),
9435 OPT_Wpedantic
, "promoted argument %qD "
9436 "doesn%'t match prototype", parm
);
9437 pedwarn (current_function_prototype_locus
, OPT_Wpedantic
,
9438 "prototype declaration");
9443 if (current_function_prototype_built_in
)
9444 warning_at (DECL_SOURCE_LOCATION (parm
),
9445 0, "argument %qD doesn%'t match "
9446 "built-in prototype", parm
);
9449 error_at (DECL_SOURCE_LOCATION (parm
),
9450 "argument %qD doesn%'t match prototype", parm
);
9451 error_at (current_function_prototype_locus
,
9452 "prototype declaration");
9457 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = NULL_TREE
;
9460 /* Otherwise, create a prototype that would match. */
9464 tree actual
= NULL_TREE
, last
= NULL_TREE
, type
;
9466 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= DECL_CHAIN (parm
))
9468 type
= tree_cons (NULL_TREE
, DECL_ARG_TYPE (parm
), NULL_TREE
);
9470 TREE_CHAIN (last
) = type
;
9475 type
= tree_cons (NULL_TREE
, void_type_node
, NULL_TREE
);
9477 TREE_CHAIN (last
) = type
;
9481 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9482 of the type of this function, but we need to avoid having this
9483 affect the types of other similarly-typed functions, so we must
9484 first force the generation of an identical (but separate) type
9485 node for the relevant function type. The new node we create
9486 will be a variant of the main variant of the original function
9489 TREE_TYPE (fndecl
) = build_variant_type_copy (TREE_TYPE (fndecl
));
9491 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = actual
;
9495 /* Store parameter declarations passed in ARG_INFO into the current
9496 function declaration. */
9499 store_parm_decls_from (struct c_arg_info
*arg_info
)
9501 current_function_arg_info
= arg_info
;
9502 store_parm_decls ();
9505 /* Called by walk_tree to look for and update context-less labels. */
9508 set_labels_context_r (tree
*tp
, int *walk_subtrees
, void *data
)
9510 if (TREE_CODE (*tp
) == LABEL_EXPR
9511 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp
)) == NULL_TREE
)
9513 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp
)) = static_cast<tree
>(data
);
9520 /* Store the parameter declarations into the current function declaration.
9521 This is called after parsing the parameter declarations, before
9522 digesting the body of the function.
9524 For an old-style definition, construct a prototype out of the old-style
9525 parameter declarations and inject it into the function's type. */
9528 store_parm_decls (void)
9530 tree fndecl
= current_function_decl
;
9533 /* The argument information block for FNDECL. */
9534 struct c_arg_info
*arg_info
= current_function_arg_info
;
9535 current_function_arg_info
= 0;
9537 /* True if this definition is written with a prototype. Note:
9538 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9539 list in a function definition as equivalent to (void) -- an
9540 empty argument list specifies the function has no parameters,
9541 but only (void) sets up a prototype for future calls. */
9542 proto
= arg_info
->types
!= 0;
9545 store_parm_decls_newstyle (fndecl
, arg_info
);
9547 store_parm_decls_oldstyle (fndecl
, arg_info
);
9549 /* The next call to push_scope will be a function body. */
9551 next_is_function_body
= true;
9553 /* Write a record describing this function definition to the prototypes
9554 file (if requested). */
9556 gen_aux_info_record (fndecl
, 1, 0, proto
);
9558 /* Initialize the RTL code for the function. */
9559 allocate_struct_function (fndecl
, false);
9561 if (warn_unused_local_typedefs
)
9562 cfun
->language
= ggc_cleared_alloc
<language_function
> ();
9564 /* Begin the statement tree for this function. */
9565 DECL_SAVED_TREE (fndecl
) = push_stmt_list ();
9567 /* ??? Insert the contents of the pending sizes list into the function
9568 to be evaluated. The only reason left to have this is
9569 void foo(int n, int array[n++])
9570 because we throw away the array type in favor of a pointer type, and
9571 thus won't naturally see the SAVE_EXPR containing the increment. All
9572 other pending sizes would be handled by gimplify_parameters. */
9573 if (arg_info
->pending_sizes
)
9575 /* In very special circumstances, e.g. for code like
9577 void f (int a[i += 2]) {}
9578 we need to execute the atomic assignment on function entry.
9579 But in this case, it is not just a straight store, it has the
9580 op= form, which means that build_atomic_assign has generated
9581 gotos, labels, etc. Because at that time the function decl
9582 for F has not been created yet, those labels do not have any
9583 function context. But we have the fndecl now, so update the
9584 labels accordingly. gimplify_expr would crash otherwise. */
9585 walk_tree_without_duplicates (&arg_info
->pending_sizes
,
9586 set_labels_context_r
, fndecl
);
9587 add_stmt (arg_info
->pending_sizes
);
9591 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9592 c_finish_omp_declare_simd for function prototypes. No diagnostics
9596 temp_store_parm_decls (tree fndecl
, tree parms
)
9599 for (tree p
= parms
; p
; p
= DECL_CHAIN (p
))
9601 DECL_CONTEXT (p
) = fndecl
;
9603 bind (DECL_NAME (p
), p
, current_scope
,
9604 /*invisible=*/false, /*nested=*/false,
9609 /* Undo what temp_store_parm_decls did. */
9612 temp_pop_parm_decls (void)
9614 /* Clear all bindings in this temporary scope, so that
9615 pop_scope doesn't create a BLOCK. */
9616 struct c_binding
*b
= current_scope
->bindings
;
9617 current_scope
->bindings
= NULL
;
9618 for (; b
; b
= free_binding_and_advance (b
))
9620 gcc_assert (TREE_CODE (b
->decl
) == PARM_DECL
9621 || b
->decl
== error_mark_node
);
9622 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
9623 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
9624 if (b
->shadowed
&& b
->shadowed
->u
.type
)
9625 TREE_TYPE (b
->shadowed
->decl
) = b
->shadowed
->u
.type
;
9631 /* Finish up a function declaration and compile that function
9632 all the way to assembler language output. Then free the storage
9633 for the function definition.
9635 This is called after parsing the body of the function definition. */
9638 finish_function (void)
9640 tree fndecl
= current_function_decl
;
9642 if (c_dialect_objc ())
9643 objc_finish_function ();
9645 if (TREE_CODE (fndecl
) == FUNCTION_DECL
9646 && targetm
.calls
.promote_prototypes (TREE_TYPE (fndecl
)))
9648 tree args
= DECL_ARGUMENTS (fndecl
);
9649 for (; args
; args
= DECL_CHAIN (args
))
9651 tree type
= TREE_TYPE (args
);
9652 if (INTEGRAL_TYPE_P (type
)
9653 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
9654 DECL_ARG_TYPE (args
) = c_type_promotes_to (type
);
9658 if (DECL_INITIAL (fndecl
) && DECL_INITIAL (fndecl
) != error_mark_node
)
9659 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
9661 /* Must mark the RESULT_DECL as being in this function. */
9663 if (DECL_RESULT (fndecl
) && DECL_RESULT (fndecl
) != error_mark_node
)
9664 DECL_CONTEXT (DECL_RESULT (fndecl
)) = fndecl
;
9666 if (MAIN_NAME_P (DECL_NAME (fndecl
)) && flag_hosted
9667 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl
)))
9668 == integer_type_node
&& flag_isoc99
)
9670 /* Hack. We don't want the middle-end to warn that this return
9671 is unreachable, so we mark its location as special. Using
9672 UNKNOWN_LOCATION has the problem that it gets clobbered in
9673 annotate_one_with_locus. A cleaner solution might be to
9674 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9676 c_finish_return (BUILTINS_LOCATION
, integer_zero_node
, NULL_TREE
);
9679 /* Tie off the statement tree for this function. */
9680 DECL_SAVED_TREE (fndecl
) = pop_stmt_list (DECL_SAVED_TREE (fndecl
));
9682 finish_fname_decls ();
9684 /* Complain if there's no return statement only if option specified on
9686 if (warn_return_type
> 0
9687 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl
))) != VOID_TYPE
9688 && !current_function_returns_value
&& !current_function_returns_null
9689 /* Don't complain if we are no-return. */
9690 && !current_function_returns_abnormally
9691 /* Don't complain if we are declared noreturn. */
9692 && !TREE_THIS_VOLATILE (fndecl
)
9693 /* Don't warn for main(). */
9694 && !MAIN_NAME_P (DECL_NAME (fndecl
))
9695 /* Or if they didn't actually specify a return type. */
9696 && !C_FUNCTION_IMPLICIT_INT (fndecl
)
9697 /* Normally, with -Wreturn-type, flow will complain, but we might
9698 optimize out static functions. */
9699 && !TREE_PUBLIC (fndecl
)
9700 && targetm
.warn_func_return (fndecl
)
9701 && warning (OPT_Wreturn_type
,
9702 "no return statement in function returning non-void"))
9703 TREE_NO_WARNING (fndecl
) = 1;
9705 /* Complain about parameters that are only set, but never otherwise used. */
9706 if (warn_unused_but_set_parameter
)
9710 for (decl
= DECL_ARGUMENTS (fndecl
);
9712 decl
= DECL_CHAIN (decl
))
9713 if (TREE_USED (decl
)
9714 && TREE_CODE (decl
) == PARM_DECL
9715 && !DECL_READ_P (decl
)
9717 && !DECL_ARTIFICIAL (decl
)
9718 && !TREE_NO_WARNING (decl
))
9719 warning_at (DECL_SOURCE_LOCATION (decl
),
9720 OPT_Wunused_but_set_parameter
,
9721 "parameter %qD set but not used", decl
);
9724 /* Complain about locally defined typedefs that are not used in this
9726 maybe_warn_unused_local_typedefs ();
9728 /* Possibly warn about unused parameters. */
9729 if (warn_unused_parameter
)
9730 do_warn_unused_parameter (fndecl
);
9732 /* Store the end of the function, so that we get good line number
9733 info for the epilogue. */
9734 cfun
->function_end_locus
= input_location
;
9736 /* Finalize the ELF visibility for the function. */
9737 c_determine_visibility (fndecl
);
9739 /* For GNU C extern inline functions disregard inline limits. */
9740 if (DECL_EXTERNAL (fndecl
)
9741 && DECL_DECLARED_INLINE_P (fndecl
)
9742 && (flag_gnu89_inline
9743 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl
))))
9744 DECL_DISREGARD_INLINE_LIMITS (fndecl
) = 1;
9746 /* Genericize before inlining. Delay genericizing nested functions
9747 until their parent function is genericized. Since finalizing
9748 requires GENERIC, delay that as well. */
9750 if (DECL_INITIAL (fndecl
) && DECL_INITIAL (fndecl
) != error_mark_node
9751 && !undef_nested_function
)
9753 if (!decl_function_context (fndecl
))
9755 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE
, fndecl
);
9756 c_genericize (fndecl
);
9758 /* ??? Objc emits functions after finalizing the compilation unit.
9759 This should be cleaned up later and this conditional removed. */
9760 if (symtab
->global_info_ready
)
9762 cgraph_node::add_new_function (fndecl
, false);
9765 cgraph_node::finalize_function (fndecl
, false);
9769 /* Register this function with cgraph just far enough to get it
9770 added to our parent's nested function list. Handy, since the
9771 C front end doesn't have such a list. */
9772 (void) cgraph_node::get_create (fndecl
);
9776 if (!decl_function_context (fndecl
))
9777 undef_nested_function
= false;
9779 if (cfun
->language
!= NULL
)
9781 ggc_free (cfun
->language
);
9782 cfun
->language
= NULL
;
9785 /* We're leaving the context of this function, so zap cfun.
9786 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9787 tree_rest_of_compilation. */
9789 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION
, current_function_decl
);
9790 current_function_decl
= NULL
;
9793 /* Check the declarations given in a for-loop for satisfying the C99
9794 constraints. If exactly one such decl is found, return it. LOC is
9795 the location of the opening parenthesis of the for loop. The last
9796 parameter allows you to control the "for loop initial declarations
9797 are only allowed in C99 mode". Normally, you should pass
9798 flag_isoc99 as that parameter. But in some cases (Objective-C
9799 foreach loop, for example) we want to run the checks in this
9800 function even if not in C99 mode, so we allow the caller to turn
9801 off the error about not being in C99 mode.
9805 check_for_loop_decls (location_t loc
, bool turn_off_iso_c99_error
)
9807 struct c_binding
*b
;
9808 tree one_decl
= NULL_TREE
;
9811 if (!turn_off_iso_c99_error
)
9813 static bool hint
= true;
9814 /* If we get here, declarations have been used in a for loop without
9815 the C99 for loop scope. This doesn't make much sense, so don't
9817 error_at (loc
, "%<for%> loop initial declarations "
9818 "are only allowed in C99 or C11 mode");
9822 "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
9823 "%<-std=gnu11%> to compile your code");
9829 pedwarn_c90 (loc
, OPT_Wpedantic
, "ISO C90 does not support %<for%> loop "
9830 "initial declarations");
9832 /* C99 subclause 6.8.5 paragraph 3:
9834 [#3] The declaration part of a for statement shall only
9835 declare identifiers for objects having storage class auto or
9838 It isn't clear whether, in this sentence, "identifiers" binds to
9839 "shall only declare" or to "objects" - that is, whether all identifiers
9840 declared must be identifiers for objects, or whether the restriction
9841 only applies to those that are. (A question on this in comp.std.c
9842 in November 2000 received no answer.) We implement the strictest
9843 interpretation, to avoid creating an extension which later causes
9846 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
9849 tree decl
= b
->decl
;
9854 switch (TREE_CODE (decl
))
9858 location_t decl_loc
= DECL_SOURCE_LOCATION (decl
);
9859 if (TREE_STATIC (decl
))
9861 "declaration of static variable %qD in %<for%> loop "
9862 "initial declaration", decl
);
9863 else if (DECL_EXTERNAL (decl
))
9865 "declaration of %<extern%> variable %qD in %<for%> loop "
9866 "initial declaration", decl
);
9872 "%<struct %E%> declared in %<for%> loop initial "
9877 "%<union %E%> declared in %<for%> loop initial declaration",
9881 error_at (loc
, "%<enum %E%> declared in %<for%> loop "
9882 "initial declaration", id
);
9885 error_at (loc
, "declaration of non-variable "
9886 "%qD in %<for%> loop initial declaration", decl
);
9893 return n_decls
== 1 ? one_decl
: NULL_TREE
;
9896 /* Save and reinitialize the variables
9897 used during compilation of a C function. */
9900 c_push_function_context (void)
9902 struct language_function
*p
= cfun
->language
;
9903 /* cfun->language might have been already allocated by the use of
9904 -Wunused-local-typedefs. In that case, just re-use it. */
9906 cfun
->language
= p
= ggc_cleared_alloc
<language_function
> ();
9908 p
->base
.x_stmt_tree
= c_stmt_tree
;
9909 c_stmt_tree
.x_cur_stmt_list
= vec_safe_copy (c_stmt_tree
.x_cur_stmt_list
);
9910 p
->x_break_label
= c_break_label
;
9911 p
->x_cont_label
= c_cont_label
;
9912 p
->x_switch_stack
= c_switch_stack
;
9913 p
->arg_info
= current_function_arg_info
;
9914 p
->returns_value
= current_function_returns_value
;
9915 p
->returns_null
= current_function_returns_null
;
9916 p
->returns_abnormally
= current_function_returns_abnormally
;
9917 p
->warn_about_return_type
= warn_about_return_type
;
9919 push_function_context ();
9922 /* Restore the variables used during compilation of a C function. */
9925 c_pop_function_context (void)
9927 struct language_function
*p
;
9929 pop_function_context ();
9932 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9933 used to store data throughout the life time of the current cfun,
9934 So don't deallocate it. */
9935 if (!warn_unused_local_typedefs
)
9936 cfun
->language
= NULL
;
9938 if (DECL_STRUCT_FUNCTION (current_function_decl
) == 0
9939 && DECL_SAVED_TREE (current_function_decl
) == NULL_TREE
)
9941 /* Stop pointing to the local nodes about to be freed. */
9942 /* But DECL_INITIAL must remain nonzero so we know this
9943 was an actual function definition. */
9944 DECL_INITIAL (current_function_decl
) = error_mark_node
;
9945 DECL_ARGUMENTS (current_function_decl
) = NULL_TREE
;
9948 c_stmt_tree
= p
->base
.x_stmt_tree
;
9949 p
->base
.x_stmt_tree
.x_cur_stmt_list
= NULL
;
9950 c_break_label
= p
->x_break_label
;
9951 c_cont_label
= p
->x_cont_label
;
9952 c_switch_stack
= p
->x_switch_stack
;
9953 current_function_arg_info
= p
->arg_info
;
9954 current_function_returns_value
= p
->returns_value
;
9955 current_function_returns_null
= p
->returns_null
;
9956 current_function_returns_abnormally
= p
->returns_abnormally
;
9957 warn_about_return_type
= p
->warn_about_return_type
;
9960 /* The functions below are required for functionality of doing
9961 function at once processing in the C front end. Currently these
9962 functions are not called from anywhere in the C front end, but as
9963 these changes continue, that will change. */
9965 /* Returns the stmt_tree (if any) to which statements are currently
9966 being added. If there is no active statement-tree, NULL is
9970 current_stmt_tree (void)
9972 return &c_stmt_tree
;
9975 /* Return the global value of T as a symbol. */
9978 identifier_global_value (tree t
)
9980 struct c_binding
*b
;
9982 for (b
= I_SYMBOL_BINDING (t
); b
; b
= b
->shadowed
)
9983 if (B_IN_FILE_SCOPE (b
) || B_IN_EXTERNAL_SCOPE (b
))
9989 /* In C, the only C-linkage public declaration is at file scope. */
9992 c_linkage_bindings (tree name
)
9994 return identifier_global_value (name
);
9997 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9998 otherwise the name is found in ridpointers from RID_INDEX. */
10001 record_builtin_type (enum rid rid_index
, const char *name
, tree type
)
10005 id
= ridpointers
[(int) rid_index
];
10007 id
= get_identifier (name
);
10008 decl
= build_decl (UNKNOWN_LOCATION
, TYPE_DECL
, id
, type
);
10010 if (debug_hooks
->type_decl
)
10011 debug_hooks
->type_decl (decl
, false);
10014 /* Build the void_list_node (void_type_node having been created). */
10016 build_void_list_node (void)
10018 tree t
= build_tree_list (NULL_TREE
, void_type_node
);
10022 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
10025 build_c_parm (struct c_declspecs
*specs
, tree attrs
,
10026 struct c_declarator
*declarator
,
10029 struct c_parm
*ret
= XOBNEW (&parser_obstack
, struct c_parm
);
10030 ret
->specs
= specs
;
10031 ret
->attrs
= attrs
;
10032 ret
->declarator
= declarator
;
10037 /* Return a declarator with nested attributes. TARGET is the inner
10038 declarator to which these attributes apply. ATTRS are the
10041 struct c_declarator
*
10042 build_attrs_declarator (tree attrs
, struct c_declarator
*target
)
10044 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
10045 ret
->kind
= cdk_attrs
;
10046 ret
->declarator
= target
;
10047 ret
->u
.attrs
= attrs
;
10051 /* Return a declarator for a function with arguments specified by ARGS
10052 and return type specified by TARGET. */
10054 struct c_declarator
*
10055 build_function_declarator (struct c_arg_info
*args
,
10056 struct c_declarator
*target
)
10058 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
10059 ret
->kind
= cdk_function
;
10060 ret
->declarator
= target
;
10061 ret
->u
.arg_info
= args
;
10065 /* Return a declarator for the identifier IDENT (which may be
10066 NULL_TREE for an abstract declarator). */
10068 struct c_declarator
*
10069 build_id_declarator (tree ident
)
10071 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
10072 ret
->kind
= cdk_id
;
10073 ret
->declarator
= 0;
10075 /* Default value - may get reset to a more precise location. */
10076 ret
->id_loc
= input_location
;
10080 /* Return something to represent absolute declarators containing a *.
10081 TARGET is the absolute declarator that the * contains.
10082 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
10083 to apply to the pointer type. */
10085 struct c_declarator
*
10086 make_pointer_declarator (struct c_declspecs
*type_quals_attrs
,
10087 struct c_declarator
*target
)
10091 struct c_declarator
*itarget
= target
;
10092 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
10093 if (type_quals_attrs
)
10095 attrs
= type_quals_attrs
->attrs
;
10096 quals
= quals_from_declspecs (type_quals_attrs
);
10097 if (attrs
!= NULL_TREE
)
10098 itarget
= build_attrs_declarator (attrs
, target
);
10100 ret
->kind
= cdk_pointer
;
10101 ret
->declarator
= itarget
;
10102 ret
->u
.pointer_quals
= quals
;
10106 /* Return a pointer to a structure for an empty list of declaration
10109 struct c_declspecs
*
10110 build_null_declspecs (void)
10112 struct c_declspecs
*ret
= XOBNEW (&parser_obstack
, struct c_declspecs
);
10113 memset (ret
, 0, sizeof *ret
);
10114 ret
->align_log
= -1;
10115 ret
->typespec_word
= cts_none
;
10116 ret
->storage_class
= csc_none
;
10117 ret
->expr_const_operands
= true;
10118 ret
->typespec_kind
= ctsk_none
;
10119 ret
->address_space
= ADDR_SPACE_GENERIC
;
10123 /* Add the address space ADDRSPACE to the declaration specifiers
10124 SPECS, returning SPECS. */
10126 struct c_declspecs
*
10127 declspecs_add_addrspace (location_t location
,
10128 struct c_declspecs
*specs
, addr_space_t as
)
10130 specs
->non_sc_seen_p
= true;
10131 specs
->declspecs_seen_p
= true;
10133 if (!ADDR_SPACE_GENERIC_P (specs
->address_space
)
10134 && specs
->address_space
!= as
)
10135 error ("incompatible address space qualifiers %qs and %qs",
10136 c_addr_space_name (as
),
10137 c_addr_space_name (specs
->address_space
));
10140 specs
->address_space
= as
;
10141 specs
->locations
[cdw_address_space
] = location
;
10146 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
10147 returning SPECS. */
10149 struct c_declspecs
*
10150 declspecs_add_qual (location_t loc
,
10151 struct c_declspecs
*specs
, tree qual
)
10155 specs
->non_sc_seen_p
= true;
10156 specs
->declspecs_seen_p
= true;
10157 gcc_assert (TREE_CODE (qual
) == IDENTIFIER_NODE
10158 && C_IS_RESERVED_WORD (qual
));
10159 i
= C_RID_CODE (qual
);
10160 location_t prev_loc
= UNKNOWN_LOCATION
;
10164 dupe
= specs
->const_p
;
10165 specs
->const_p
= true;
10166 prev_loc
= specs
->locations
[cdw_const
];
10167 specs
->locations
[cdw_const
] = loc
;
10170 dupe
= specs
->volatile_p
;
10171 specs
->volatile_p
= true;
10172 prev_loc
= specs
->locations
[cdw_volatile
];
10173 specs
->locations
[cdw_volatile
] = loc
;
10176 dupe
= specs
->restrict_p
;
10177 specs
->restrict_p
= true;
10178 prev_loc
= specs
->locations
[cdw_restrict
];
10179 specs
->locations
[cdw_restrict
] = loc
;
10182 dupe
= specs
->atomic_p
;
10183 specs
->atomic_p
= true;
10184 prev_loc
= specs
->locations
[cdw_atomic
];
10185 specs
->locations
[cdw_atomic
] = loc
;
10188 gcc_unreachable ();
10192 bool warned
= pedwarn_c90 (loc
, OPT_Wpedantic
,
10193 "duplicate %qE declaration specifier", qual
);
10195 && warn_duplicate_decl_specifier
10196 && prev_loc
>= RESERVED_LOCATION_COUNT
10197 && !from_macro_expansion_at (prev_loc
)
10198 && !from_macro_expansion_at (loc
))
10199 warning_at (loc
, OPT_Wduplicate_decl_specifier
,
10200 "duplicate %qE declaration specifier", qual
);
10205 /* Add the type specifier TYPE to the declaration specifiers SPECS,
10206 returning SPECS. */
10208 struct c_declspecs
*
10209 declspecs_add_type (location_t loc
, struct c_declspecs
*specs
,
10210 struct c_typespec spec
)
10212 tree type
= spec
.spec
;
10213 specs
->non_sc_seen_p
= true;
10214 specs
->declspecs_seen_p
= true;
10215 specs
->typespec_kind
= spec
.kind
;
10216 if (TREE_DEPRECATED (type
))
10217 specs
->deprecated_p
= true;
10219 /* Handle type specifier keywords. */
10220 if (TREE_CODE (type
) == IDENTIFIER_NODE
10221 && C_IS_RESERVED_WORD (type
)
10222 && C_RID_CODE (type
) != RID_CXX_COMPAT_WARN
)
10224 enum rid i
= C_RID_CODE (type
);
10227 error_at (loc
, "two or more data types in declaration specifiers");
10230 if ((int) i
<= (int) RID_LAST_MODIFIER
)
10232 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
10237 if (specs
->long_long_p
)
10239 error_at (loc
, "%<long long long%> is too long for GCC");
10244 if (specs
->typespec_word
== cts_double
)
10247 ("both %<long long%> and %<double%> in "
10248 "declaration specifiers"));
10251 pedwarn_c90 (loc
, OPT_Wlong_long
,
10252 "ISO C90 does not support %<long long%>");
10253 specs
->long_long_p
= 1;
10254 specs
->locations
[cdw_long_long
] = loc
;
10257 if (specs
->short_p
)
10259 ("both %<long%> and %<short%> in "
10260 "declaration specifiers"));
10261 else if (specs
->typespec_word
== cts_auto_type
)
10263 ("both %<long%> and %<__auto_type%> in "
10264 "declaration specifiers"));
10265 else if (specs
->typespec_word
== cts_void
)
10267 ("both %<long%> and %<void%> in "
10268 "declaration specifiers"));
10269 else if (specs
->typespec_word
== cts_int_n
)
10271 ("both %<long%> and %<__int%d%> in "
10272 "declaration specifiers"),
10273 int_n_data
[specs
->int_n_idx
].bitsize
);
10274 else if (specs
->typespec_word
== cts_bool
)
10276 ("both %<long%> and %<_Bool%> in "
10277 "declaration specifiers"));
10278 else if (specs
->typespec_word
== cts_char
)
10280 ("both %<long%> and %<char%> in "
10281 "declaration specifiers"));
10282 else if (specs
->typespec_word
== cts_float
)
10284 ("both %<long%> and %<float%> in "
10285 "declaration specifiers"));
10286 else if (specs
->typespec_word
== cts_floatn_nx
)
10288 ("both %<long%> and %<_Float%d%s%> in "
10289 "declaration specifiers"),
10290 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10291 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10294 else if (specs
->typespec_word
== cts_dfloat32
)
10296 ("both %<long%> and %<_Decimal32%> in "
10297 "declaration specifiers"));
10298 else if (specs
->typespec_word
== cts_dfloat64
)
10300 ("both %<long%> and %<_Decimal64%> in "
10301 "declaration specifiers"));
10302 else if (specs
->typespec_word
== cts_dfloat128
)
10304 ("both %<long%> and %<_Decimal128%> in "
10305 "declaration specifiers"));
10308 specs
->long_p
= true;
10309 specs
->locations
[cdw_long
] = loc
;
10313 dupe
= specs
->short_p
;
10316 ("both %<long%> and %<short%> in "
10317 "declaration specifiers"));
10318 else if (specs
->typespec_word
== cts_auto_type
)
10320 ("both %<short%> and %<__auto_type%> in "
10321 "declaration specifiers"));
10322 else if (specs
->typespec_word
== cts_void
)
10324 ("both %<short%> and %<void%> in "
10325 "declaration specifiers"));
10326 else if (specs
->typespec_word
== cts_int_n
)
10328 ("both %<short%> and %<__int%d%> in "
10329 "declaration specifiers"),
10330 int_n_data
[specs
->int_n_idx
].bitsize
);
10331 else if (specs
->typespec_word
== cts_bool
)
10333 ("both %<short%> and %<_Bool%> in "
10334 "declaration specifiers"));
10335 else if (specs
->typespec_word
== cts_char
)
10337 ("both %<short%> and %<char%> in "
10338 "declaration specifiers"));
10339 else if (specs
->typespec_word
== cts_float
)
10341 ("both %<short%> and %<float%> in "
10342 "declaration specifiers"));
10343 else if (specs
->typespec_word
== cts_double
)
10345 ("both %<short%> and %<double%> in "
10346 "declaration specifiers"));
10347 else if (specs
->typespec_word
== cts_floatn_nx
)
10349 ("both %<short%> and %<_Float%d%s%> in "
10350 "declaration specifiers"),
10351 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10352 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10355 else if (specs
->typespec_word
== cts_dfloat32
)
10357 ("both %<short%> and %<_Decimal32%> in "
10358 "declaration specifiers"));
10359 else if (specs
->typespec_word
== cts_dfloat64
)
10361 ("both %<short%> and %<_Decimal64%> in "
10362 "declaration specifiers"));
10363 else if (specs
->typespec_word
== cts_dfloat128
)
10365 ("both %<short%> and %<_Decimal128%> in "
10366 "declaration specifiers"));
10369 specs
->short_p
= true;
10370 specs
->locations
[cdw_short
] = loc
;
10374 dupe
= specs
->signed_p
;
10375 if (specs
->unsigned_p
)
10377 ("both %<signed%> and %<unsigned%> in "
10378 "declaration specifiers"));
10379 else if (specs
->typespec_word
== cts_auto_type
)
10381 ("both %<signed%> and %<__auto_type%> in "
10382 "declaration specifiers"));
10383 else if (specs
->typespec_word
== cts_void
)
10385 ("both %<signed%> and %<void%> in "
10386 "declaration specifiers"));
10387 else if (specs
->typespec_word
== cts_bool
)
10389 ("both %<signed%> and %<_Bool%> in "
10390 "declaration specifiers"));
10391 else if (specs
->typespec_word
== cts_float
)
10393 ("both %<signed%> and %<float%> in "
10394 "declaration specifiers"));
10395 else if (specs
->typespec_word
== cts_double
)
10397 ("both %<signed%> and %<double%> in "
10398 "declaration specifiers"));
10399 else if (specs
->typespec_word
== cts_floatn_nx
)
10401 ("both %<signed%> and %<_Float%d%s%> in "
10402 "declaration specifiers"),
10403 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10404 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10407 else if (specs
->typespec_word
== cts_dfloat32
)
10409 ("both %<signed%> and %<_Decimal32%> in "
10410 "declaration specifiers"));
10411 else if (specs
->typespec_word
== cts_dfloat64
)
10413 ("both %<signed%> and %<_Decimal64%> in "
10414 "declaration specifiers"));
10415 else if (specs
->typespec_word
== cts_dfloat128
)
10417 ("both %<signed%> and %<_Decimal128%> in "
10418 "declaration specifiers"));
10421 specs
->signed_p
= true;
10422 specs
->locations
[cdw_signed
] = loc
;
10426 dupe
= specs
->unsigned_p
;
10427 if (specs
->signed_p
)
10429 ("both %<signed%> and %<unsigned%> in "
10430 "declaration specifiers"));
10431 else if (specs
->typespec_word
== cts_auto_type
)
10433 ("both %<unsigned%> and %<__auto_type%> in "
10434 "declaration specifiers"));
10435 else if (specs
->typespec_word
== cts_void
)
10437 ("both %<unsigned%> and %<void%> in "
10438 "declaration specifiers"));
10439 else if (specs
->typespec_word
== cts_bool
)
10441 ("both %<unsigned%> and %<_Bool%> in "
10442 "declaration specifiers"));
10443 else if (specs
->typespec_word
== cts_float
)
10445 ("both %<unsigned%> and %<float%> in "
10446 "declaration specifiers"));
10447 else if (specs
->typespec_word
== cts_double
)
10449 ("both %<unsigned%> and %<double%> in "
10450 "declaration specifiers"));
10451 else if (specs
->typespec_word
== cts_floatn_nx
)
10453 ("both %<unsigned%> and %<_Float%d%s%> in "
10454 "declaration specifiers"),
10455 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10456 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10459 else if (specs
->typespec_word
== cts_dfloat32
)
10461 ("both %<unsigned%> and %<_Decimal32%> in "
10462 "declaration specifiers"));
10463 else if (specs
->typespec_word
== cts_dfloat64
)
10465 ("both %<unsigned%> and %<_Decimal64%> in "
10466 "declaration specifiers"));
10467 else if (specs
->typespec_word
== cts_dfloat128
)
10469 ("both %<unsigned%> and %<_Decimal128%> in "
10470 "declaration specifiers"));
10473 specs
->unsigned_p
= true;
10474 specs
->locations
[cdw_unsigned
] = loc
;
10478 dupe
= specs
->complex_p
;
10479 if (!in_system_header_at (loc
))
10480 pedwarn_c90 (loc
, OPT_Wpedantic
,
10481 "ISO C90 does not support complex types");
10482 if (specs
->typespec_word
== cts_auto_type
)
10484 ("both %<complex%> and %<__auto_type%> in "
10485 "declaration specifiers"));
10486 else if (specs
->typespec_word
== cts_void
)
10488 ("both %<complex%> and %<void%> in "
10489 "declaration specifiers"));
10490 else if (specs
->typespec_word
== cts_bool
)
10492 ("both %<complex%> and %<_Bool%> in "
10493 "declaration specifiers"));
10494 else if (specs
->typespec_word
== cts_dfloat32
)
10496 ("both %<complex%> and %<_Decimal32%> in "
10497 "declaration specifiers"));
10498 else if (specs
->typespec_word
== cts_dfloat64
)
10500 ("both %<complex%> and %<_Decimal64%> in "
10501 "declaration specifiers"));
10502 else if (specs
->typespec_word
== cts_dfloat128
)
10504 ("both %<complex%> and %<_Decimal128%> in "
10505 "declaration specifiers"));
10506 else if (specs
->typespec_word
== cts_fract
)
10508 ("both %<complex%> and %<_Fract%> in "
10509 "declaration specifiers"));
10510 else if (specs
->typespec_word
== cts_accum
)
10512 ("both %<complex%> and %<_Accum%> in "
10513 "declaration specifiers"));
10514 else if (specs
->saturating_p
)
10516 ("both %<complex%> and %<_Sat%> in "
10517 "declaration specifiers"));
10520 specs
->complex_p
= true;
10521 specs
->locations
[cdw_complex
] = loc
;
10525 dupe
= specs
->saturating_p
;
10526 pedwarn (loc
, OPT_Wpedantic
,
10527 "ISO C does not support saturating types");
10528 if (specs
->typespec_word
== cts_int_n
)
10531 ("both %<_Sat%> and %<__int%d%> in "
10532 "declaration specifiers"),
10533 int_n_data
[specs
->int_n_idx
].bitsize
);
10535 else if (specs
->typespec_word
== cts_auto_type
)
10537 ("both %<_Sat%> and %<__auto_type%> in "
10538 "declaration specifiers"));
10539 else if (specs
->typespec_word
== cts_void
)
10541 ("both %<_Sat%> and %<void%> in "
10542 "declaration specifiers"));
10543 else if (specs
->typespec_word
== cts_bool
)
10545 ("both %<_Sat%> and %<_Bool%> in "
10546 "declaration specifiers"));
10547 else if (specs
->typespec_word
== cts_char
)
10549 ("both %<_Sat%> and %<char%> in "
10550 "declaration specifiers"));
10551 else if (specs
->typespec_word
== cts_int
)
10553 ("both %<_Sat%> and %<int%> in "
10554 "declaration specifiers"));
10555 else if (specs
->typespec_word
== cts_float
)
10557 ("both %<_Sat%> and %<float%> in "
10558 "declaration specifiers"));
10559 else if (specs
->typespec_word
== cts_double
)
10561 ("both %<_Sat%> and %<double%> in "
10562 "declaration specifiers"));
10563 else if (specs
->typespec_word
== cts_floatn_nx
)
10565 ("both %<_Sat%> and %<_Float%d%s%> in "
10566 "declaration specifiers"),
10567 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10568 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10571 else if (specs
->typespec_word
== cts_dfloat32
)
10573 ("both %<_Sat%> and %<_Decimal32%> in "
10574 "declaration specifiers"));
10575 else if (specs
->typespec_word
== cts_dfloat64
)
10577 ("both %<_Sat%> and %<_Decimal64%> in "
10578 "declaration specifiers"));
10579 else if (specs
->typespec_word
== cts_dfloat128
)
10581 ("both %<_Sat%> and %<_Decimal128%> in "
10582 "declaration specifiers"));
10583 else if (specs
->complex_p
)
10585 ("both %<_Sat%> and %<complex%> in "
10586 "declaration specifiers"));
10589 specs
->saturating_p
= true;
10590 specs
->locations
[cdw_saturating
] = loc
;
10594 gcc_unreachable ();
10598 error_at (loc
, "duplicate %qE", type
);
10604 /* "void", "_Bool", "char", "int", "float", "double",
10605 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
10606 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10608 if (specs
->typespec_word
!= cts_none
)
10611 "two or more data types in declaration specifiers");
10616 case RID_AUTO_TYPE
:
10619 ("both %<long%> and %<__auto_type%> in "
10620 "declaration specifiers"));
10621 else if (specs
->short_p
)
10623 ("both %<short%> and %<__auto_type%> in "
10624 "declaration specifiers"));
10625 else if (specs
->signed_p
)
10627 ("both %<signed%> and %<__auto_type%> in "
10628 "declaration specifiers"));
10629 else if (specs
->unsigned_p
)
10631 ("both %<unsigned%> and %<__auto_type%> in "
10632 "declaration specifiers"));
10633 else if (specs
->complex_p
)
10635 ("both %<complex%> and %<__auto_type%> in "
10636 "declaration specifiers"));
10637 else if (specs
->saturating_p
)
10639 ("both %<_Sat%> and %<__auto_type%> in "
10640 "declaration specifiers"));
10643 specs
->typespec_word
= cts_auto_type
;
10644 specs
->locations
[cdw_typespec
] = loc
;
10651 specs
->int_n_idx
= i
- RID_INT_N_0
;
10652 if (!in_system_header_at (input_location
)
10653 /* If the INT_N type ends in "__", and so is of the format
10654 "__intN__", don't pedwarn. */
10655 && (strncmp (IDENTIFIER_POINTER (type
)
10656 + (IDENTIFIER_LENGTH (type
) - 2), "__", 2) != 0))
10657 pedwarn (loc
, OPT_Wpedantic
,
10658 "ISO C does not support %<__int%d%> types",
10659 int_n_data
[specs
->int_n_idx
].bitsize
);
10663 ("both %<__int%d%> and %<long%> in "
10664 "declaration specifiers"),
10665 int_n_data
[specs
->int_n_idx
].bitsize
);
10666 else if (specs
->saturating_p
)
10668 ("both %<_Sat%> and %<__int%d%> in "
10669 "declaration specifiers"),
10670 int_n_data
[specs
->int_n_idx
].bitsize
);
10671 else if (specs
->short_p
)
10673 ("both %<__int%d%> and %<short%> in "
10674 "declaration specifiers"),
10675 int_n_data
[specs
->int_n_idx
].bitsize
);
10676 else if (! int_n_enabled_p
[specs
->int_n_idx
])
10678 specs
->typespec_word
= cts_int_n
;
10680 "%<__int%d%> is not supported on this target",
10681 int_n_data
[specs
->int_n_idx
].bitsize
);
10685 specs
->typespec_word
= cts_int_n
;
10686 specs
->locations
[cdw_typespec
] = loc
;
10692 ("both %<long%> and %<void%> in "
10693 "declaration specifiers"));
10694 else if (specs
->short_p
)
10696 ("both %<short%> and %<void%> in "
10697 "declaration specifiers"));
10698 else if (specs
->signed_p
)
10700 ("both %<signed%> and %<void%> in "
10701 "declaration specifiers"));
10702 else if (specs
->unsigned_p
)
10704 ("both %<unsigned%> and %<void%> in "
10705 "declaration specifiers"));
10706 else if (specs
->complex_p
)
10708 ("both %<complex%> and %<void%> in "
10709 "declaration specifiers"));
10710 else if (specs
->saturating_p
)
10712 ("both %<_Sat%> and %<void%> in "
10713 "declaration specifiers"));
10716 specs
->typespec_word
= cts_void
;
10717 specs
->locations
[cdw_typespec
] = loc
;
10721 if (!in_system_header_at (loc
))
10722 pedwarn_c90 (loc
, OPT_Wpedantic
,
10723 "ISO C90 does not support boolean types");
10726 ("both %<long%> and %<_Bool%> in "
10727 "declaration specifiers"));
10728 else if (specs
->short_p
)
10730 ("both %<short%> and %<_Bool%> in "
10731 "declaration specifiers"));
10732 else if (specs
->signed_p
)
10734 ("both %<signed%> and %<_Bool%> in "
10735 "declaration specifiers"));
10736 else if (specs
->unsigned_p
)
10738 ("both %<unsigned%> and %<_Bool%> in "
10739 "declaration specifiers"));
10740 else if (specs
->complex_p
)
10742 ("both %<complex%> and %<_Bool%> in "
10743 "declaration specifiers"));
10744 else if (specs
->saturating_p
)
10746 ("both %<_Sat%> and %<_Bool%> in "
10747 "declaration specifiers"));
10750 specs
->typespec_word
= cts_bool
;
10751 specs
->locations
[cdw_typespec
] = loc
;
10757 ("both %<long%> and %<char%> in "
10758 "declaration specifiers"));
10759 else if (specs
->short_p
)
10761 ("both %<short%> and %<char%> in "
10762 "declaration specifiers"));
10763 else if (specs
->saturating_p
)
10765 ("both %<_Sat%> and %<char%> in "
10766 "declaration specifiers"));
10769 specs
->typespec_word
= cts_char
;
10770 specs
->locations
[cdw_typespec
] = loc
;
10774 if (specs
->saturating_p
)
10776 ("both %<_Sat%> and %<int%> in "
10777 "declaration specifiers"));
10780 specs
->typespec_word
= cts_int
;
10781 specs
->locations
[cdw_typespec
] = loc
;
10787 ("both %<long%> and %<float%> in "
10788 "declaration specifiers"));
10789 else if (specs
->short_p
)
10791 ("both %<short%> and %<float%> in "
10792 "declaration specifiers"));
10793 else if (specs
->signed_p
)
10795 ("both %<signed%> and %<float%> in "
10796 "declaration specifiers"));
10797 else if (specs
->unsigned_p
)
10799 ("both %<unsigned%> and %<float%> in "
10800 "declaration specifiers"));
10801 else if (specs
->saturating_p
)
10803 ("both %<_Sat%> and %<float%> in "
10804 "declaration specifiers"));
10807 specs
->typespec_word
= cts_float
;
10808 specs
->locations
[cdw_typespec
] = loc
;
10812 if (specs
->long_long_p
)
10814 ("both %<long long%> and %<double%> in "
10815 "declaration specifiers"));
10816 else if (specs
->short_p
)
10818 ("both %<short%> and %<double%> in "
10819 "declaration specifiers"));
10820 else if (specs
->signed_p
)
10822 ("both %<signed%> and %<double%> in "
10823 "declaration specifiers"));
10824 else if (specs
->unsigned_p
)
10826 ("both %<unsigned%> and %<double%> in "
10827 "declaration specifiers"));
10828 else if (specs
->saturating_p
)
10830 ("both %<_Sat%> and %<double%> in "
10831 "declaration specifiers"));
10834 specs
->typespec_word
= cts_double
;
10835 specs
->locations
[cdw_typespec
] = loc
;
10838 CASE_RID_FLOATN_NX
:
10839 specs
->floatn_nx_idx
= i
- RID_FLOATN_NX_FIRST
;
10840 if (!in_system_header_at (input_location
))
10841 pedwarn (loc
, OPT_Wpedantic
,
10842 "ISO C does not support the %<_Float%d%s%> type",
10843 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10844 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10850 ("both %<long%> and %<_Float%d%s%> in "
10851 "declaration specifiers"),
10852 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10853 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10856 else if (specs
->short_p
)
10858 ("both %<short%> and %<_Float%d%s%> in "
10859 "declaration specifiers"),
10860 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10861 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10864 else if (specs
->signed_p
)
10866 ("both %<signed%> and %<_Float%d%s%> in "
10867 "declaration specifiers"),
10868 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10869 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10872 else if (specs
->unsigned_p
)
10874 ("both %<unsigned%> and %<_Float%d%s%> in "
10875 "declaration specifiers"),
10876 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10877 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10880 else if (specs
->saturating_p
)
10882 ("both %<_Sat%> and %<_Float%d%s%> in "
10883 "declaration specifiers"),
10884 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10885 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10888 else if (FLOATN_NX_TYPE_NODE (specs
->floatn_nx_idx
) == NULL_TREE
)
10890 specs
->typespec_word
= cts_floatn_nx
;
10892 "%<_Float%d%s%> is not supported on this target",
10893 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10894 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10900 specs
->typespec_word
= cts_floatn_nx
;
10901 specs
->locations
[cdw_typespec
] = loc
;
10906 case RID_DFLOAT128
:
10909 if (i
== RID_DFLOAT32
)
10910 str
= "_Decimal32";
10911 else if (i
== RID_DFLOAT64
)
10912 str
= "_Decimal64";
10914 str
= "_Decimal128";
10915 if (specs
->long_long_p
)
10917 ("both %<long long%> and %qs in "
10918 "declaration specifiers"),
10922 ("both %<long%> and %qs in "
10923 "declaration specifiers"),
10925 else if (specs
->short_p
)
10927 ("both %<short%> and %qs in "
10928 "declaration specifiers"),
10930 else if (specs
->signed_p
)
10932 ("both %<signed%> and %qs in "
10933 "declaration specifiers"),
10935 else if (specs
->unsigned_p
)
10937 ("both %<unsigned%> and %qs in "
10938 "declaration specifiers"),
10940 else if (specs
->complex_p
)
10942 ("both %<complex%> and %qs in "
10943 "declaration specifiers"),
10945 else if (specs
->saturating_p
)
10947 ("both %<_Sat%> and %qs in "
10948 "declaration specifiers"),
10950 else if (i
== RID_DFLOAT32
)
10951 specs
->typespec_word
= cts_dfloat32
;
10952 else if (i
== RID_DFLOAT64
)
10953 specs
->typespec_word
= cts_dfloat64
;
10955 specs
->typespec_word
= cts_dfloat128
;
10956 specs
->locations
[cdw_typespec
] = loc
;
10958 if (!targetm
.decimal_float_supported_p ())
10960 ("decimal floating-point not supported "
10961 "for this target"));
10962 pedwarn (loc
, OPT_Wpedantic
,
10963 "ISO C does not support decimal floating-point");
10969 if (i
== RID_FRACT
)
10973 if (specs
->complex_p
)
10975 ("both %<complex%> and %qs in "
10976 "declaration specifiers"),
10978 else if (i
== RID_FRACT
)
10979 specs
->typespec_word
= cts_fract
;
10981 specs
->typespec_word
= cts_accum
;
10982 specs
->locations
[cdw_typespec
] = loc
;
10984 if (!targetm
.fixed_point_supported_p ())
10986 "fixed-point types not supported for this target");
10987 pedwarn (loc
, OPT_Wpedantic
,
10988 "ISO C does not support fixed-point types");
10991 /* ObjC reserved word "id", handled below. */
10997 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10998 form of ObjC type, cases such as "int" and "long" being handled
10999 above), a TYPE (struct, union, enum and typeof specifiers) or an
11000 ERROR_MARK. In none of these cases may there have previously
11001 been any type specifiers. */
11002 if (specs
->type
|| specs
->typespec_word
!= cts_none
11003 || specs
->long_p
|| specs
->short_p
|| specs
->signed_p
11004 || specs
->unsigned_p
|| specs
->complex_p
)
11005 error_at (loc
, "two or more data types in declaration specifiers");
11006 else if (TREE_CODE (type
) == TYPE_DECL
)
11008 if (TREE_TYPE (type
) == error_mark_node
)
11009 ; /* Allow the type to default to int to avoid cascading errors. */
11012 specs
->type
= TREE_TYPE (type
);
11013 specs
->decl_attr
= DECL_ATTRIBUTES (type
);
11014 specs
->typedef_p
= true;
11015 specs
->explicit_signed_p
= C_TYPEDEF_EXPLICITLY_SIGNED (type
);
11016 specs
->locations
[cdw_typedef
] = loc
;
11018 /* If this typedef name is defined in a struct, then a C++
11019 lookup would return a different value. */
11020 if (warn_cxx_compat
11021 && I_SYMBOL_BINDING (DECL_NAME (type
))->in_struct
)
11022 warning_at (loc
, OPT_Wc___compat
,
11023 "C++ lookup of %qD would return a field, not a type",
11026 /* If we are parsing a struct, record that a struct field
11028 if (warn_cxx_compat
&& struct_parse_info
!= NULL
)
11029 struct_parse_info
->typedefs_seen
.safe_push (type
);
11032 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
11034 tree t
= lookup_name (type
);
11035 if (!t
|| TREE_CODE (t
) != TYPE_DECL
)
11036 error_at (loc
, "%qE fails to be a typedef or built in type", type
);
11037 else if (TREE_TYPE (t
) == error_mark_node
)
11041 specs
->type
= TREE_TYPE (t
);
11042 specs
->locations
[cdw_typespec
] = loc
;
11047 if (TREE_CODE (type
) != ERROR_MARK
&& spec
.kind
== ctsk_typeof
)
11049 specs
->typedef_p
= true;
11050 specs
->locations
[cdw_typedef
] = loc
;
11054 specs
->expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (spec
.expr
),
11055 specs
->expr
, spec
.expr
);
11057 specs
->expr
= spec
.expr
;
11058 specs
->expr_const_operands
&= spec
.expr_const_operands
;
11061 specs
->type
= type
;
11067 /* Add the storage class specifier or function specifier SCSPEC to the
11068 declaration specifiers SPECS, returning SPECS. */
11070 struct c_declspecs
*
11071 declspecs_add_scspec (location_t loc
,
11072 struct c_declspecs
*specs
,
11076 enum c_storage_class n
= csc_none
;
11078 specs
->declspecs_seen_p
= true;
11079 gcc_assert (TREE_CODE (scspec
) == IDENTIFIER_NODE
11080 && C_IS_RESERVED_WORD (scspec
));
11081 i
= C_RID_CODE (scspec
);
11082 if (specs
->non_sc_seen_p
)
11083 warning (OPT_Wold_style_declaration
,
11084 "%qE is not at beginning of declaration", scspec
);
11088 /* C99 permits duplicate inline. Although of doubtful utility,
11089 it seems simplest to permit it in gnu89 mode as well, as
11090 there is also little utility in maintaining this as a
11091 difference between gnu89 and C99 inline. */
11093 specs
->inline_p
= true;
11094 specs
->locations
[cdw_inline
] = loc
;
11097 /* Duplicate _Noreturn is permitted. */
11099 specs
->noreturn_p
= true;
11100 specs
->locations
[cdw_noreturn
] = loc
;
11103 dupe
= specs
->thread_p
;
11104 if (specs
->storage_class
== csc_auto
)
11105 error ("%qE used with %<auto%>", scspec
);
11106 else if (specs
->storage_class
== csc_register
)
11107 error ("%qE used with %<register%>", scspec
);
11108 else if (specs
->storage_class
== csc_typedef
)
11109 error ("%qE used with %<typedef%>", scspec
);
11112 specs
->thread_p
= true;
11113 specs
->thread_gnu_p
= (strcmp (IDENTIFIER_POINTER (scspec
),
11115 /* A diagnostic is not required for the use of this
11116 identifier in the implementation namespace; only diagnose
11117 it for the C11 spelling because of existing code using
11118 the other spelling. */
11119 if (!specs
->thread_gnu_p
)
11122 pedwarn_c99 (loc
, OPT_Wpedantic
,
11123 "ISO C99 does not support %qE", scspec
);
11125 pedwarn_c99 (loc
, OPT_Wpedantic
,
11126 "ISO C90 does not support %qE", scspec
);
11128 specs
->locations
[cdw_thread
] = loc
;
11136 /* Diagnose "__thread extern". */
11137 if (specs
->thread_p
&& specs
->thread_gnu_p
)
11138 error ("%<__thread%> before %<extern%>");
11145 /* Diagnose "__thread static". */
11146 if (specs
->thread_p
&& specs
->thread_gnu_p
)
11147 error ("%<__thread%> before %<static%>");
11153 gcc_unreachable ();
11155 if (n
!= csc_none
&& n
== specs
->storage_class
)
11159 if (i
== RID_THREAD
)
11160 error ("duplicate %<_Thread_local%> or %<__thread%>");
11162 error ("duplicate %qE", scspec
);
11166 if (specs
->storage_class
!= csc_none
&& n
!= specs
->storage_class
)
11168 error ("multiple storage classes in declaration specifiers");
11172 specs
->storage_class
= n
;
11173 specs
->locations
[cdw_storage_class
] = loc
;
11174 if (n
!= csc_extern
&& n
!= csc_static
&& specs
->thread_p
)
11176 error ("%qs used with %qE",
11177 specs
->thread_gnu_p
? "__thread" : "_Thread_local",
11179 specs
->thread_p
= false;
11186 /* Add the attributes ATTRS to the declaration specifiers SPECS,
11187 returning SPECS. */
11189 struct c_declspecs
*
11190 declspecs_add_attrs (location_t loc
, struct c_declspecs
*specs
, tree attrs
)
11192 specs
->attrs
= chainon (attrs
, specs
->attrs
);
11193 specs
->locations
[cdw_attributes
] = loc
;
11194 specs
->declspecs_seen_p
= true;
11198 /* Add an _Alignas specifier (expression ALIGN, or type whose
11199 alignment is ALIGN) to the declaration specifiers SPECS, returning
11201 struct c_declspecs
*
11202 declspecs_add_alignas (location_t loc
,
11203 struct c_declspecs
*specs
, tree align
)
11205 specs
->alignas_p
= true;
11206 specs
->locations
[cdw_alignas
] = loc
;
11207 if (align
== error_mark_node
)
11210 /* Only accept the alignment if it's valid and greater than
11211 the current one. Zero is invalid but by C11 required to
11212 be silently ignored. */
11213 int align_log
= check_user_alignment (align
, false, /* warn_zero = */false);
11214 if (align_log
> specs
->align_log
)
11215 specs
->align_log
= align_log
;
11219 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
11220 specifiers with any other type specifier to determine the resulting
11221 type. This is where ISO C checks on complex types are made, since
11222 "_Complex long" is a prefix of the valid ISO C type "_Complex long
11225 struct c_declspecs
*
11226 finish_declspecs (struct c_declspecs
*specs
)
11228 /* If a type was specified as a whole, we have no modifiers and are
11230 if (specs
->type
!= NULL_TREE
)
11232 gcc_assert (!specs
->long_p
&& !specs
->long_long_p
&& !specs
->short_p
11233 && !specs
->signed_p
&& !specs
->unsigned_p
11234 && !specs
->complex_p
);
11236 /* Set a dummy type. */
11237 if (TREE_CODE (specs
->type
) == ERROR_MARK
)
11238 specs
->type
= integer_type_node
;
11242 /* If none of "void", "_Bool", "char", "int", "float" or "double"
11243 has been specified, treat it as "int" unless "_Complex" is
11244 present and there are no other specifiers. If we just have
11245 "_Complex", it is equivalent to "_Complex double", but e.g.
11246 "_Complex short" is equivalent to "_Complex short int". */
11247 if (specs
->typespec_word
== cts_none
)
11249 if (specs
->saturating_p
)
11251 error_at (specs
->locations
[cdw_saturating
],
11252 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
11253 if (!targetm
.fixed_point_supported_p ())
11254 error_at (specs
->locations
[cdw_saturating
],
11255 "fixed-point types not supported for this target");
11256 specs
->typespec_word
= cts_fract
;
11258 else if (specs
->long_p
|| specs
->short_p
11259 || specs
->signed_p
|| specs
->unsigned_p
)
11261 specs
->typespec_word
= cts_int
;
11263 else if (specs
->complex_p
)
11265 specs
->typespec_word
= cts_double
;
11266 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
11267 "ISO C does not support plain %<complex%> meaning "
11268 "%<double complex%>");
11272 specs
->typespec_word
= cts_int
;
11273 specs
->default_int_p
= true;
11274 /* We don't diagnose this here because grokdeclarator will
11275 give more specific diagnostics according to whether it is
11276 a function definition. */
11280 /* If "signed" was specified, record this to distinguish "int" and
11281 "signed int" in the case of a bit-field with
11282 -funsigned-bitfields. */
11283 specs
->explicit_signed_p
= specs
->signed_p
;
11285 /* Now compute the actual type. */
11286 switch (specs
->typespec_word
)
11288 case cts_auto_type
:
11289 gcc_assert (!specs
->long_p
&& !specs
->short_p
11290 && !specs
->signed_p
&& !specs
->unsigned_p
11291 && !specs
->complex_p
);
11292 /* Type to be filled in later. */
11295 gcc_assert (!specs
->long_p
&& !specs
->short_p
11296 && !specs
->signed_p
&& !specs
->unsigned_p
11297 && !specs
->complex_p
);
11298 specs
->type
= void_type_node
;
11301 gcc_assert (!specs
->long_p
&& !specs
->short_p
11302 && !specs
->signed_p
&& !specs
->unsigned_p
11303 && !specs
->complex_p
);
11304 specs
->type
= boolean_type_node
;
11307 gcc_assert (!specs
->long_p
&& !specs
->short_p
);
11308 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
11309 if (specs
->signed_p
)
11310 specs
->type
= signed_char_type_node
;
11311 else if (specs
->unsigned_p
)
11312 specs
->type
= unsigned_char_type_node
;
11314 specs
->type
= char_type_node
;
11315 if (specs
->complex_p
)
11317 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
11318 "ISO C does not support complex integer types");
11319 specs
->type
= build_complex_type (specs
->type
);
11323 gcc_assert (!specs
->long_p
&& !specs
->short_p
&& !specs
->long_long_p
);
11324 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
11325 if (! int_n_enabled_p
[specs
->int_n_idx
])
11326 specs
->type
= integer_type_node
;
11328 specs
->type
= (specs
->unsigned_p
11329 ? int_n_trees
[specs
->int_n_idx
].unsigned_type
11330 : int_n_trees
[specs
->int_n_idx
].signed_type
);
11331 if (specs
->complex_p
)
11333 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
11334 "ISO C does not support complex integer types");
11335 specs
->type
= build_complex_type (specs
->type
);
11339 gcc_assert (!(specs
->long_p
&& specs
->short_p
));
11340 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
11341 if (specs
->long_long_p
)
11342 specs
->type
= (specs
->unsigned_p
11343 ? long_long_unsigned_type_node
11344 : long_long_integer_type_node
);
11345 else if (specs
->long_p
)
11346 specs
->type
= (specs
->unsigned_p
11347 ? long_unsigned_type_node
11348 : long_integer_type_node
);
11349 else if (specs
->short_p
)
11350 specs
->type
= (specs
->unsigned_p
11351 ? short_unsigned_type_node
11352 : short_integer_type_node
);
11354 specs
->type
= (specs
->unsigned_p
11355 ? unsigned_type_node
11356 : integer_type_node
);
11357 if (specs
->complex_p
)
11359 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
11360 "ISO C does not support complex integer types");
11361 specs
->type
= build_complex_type (specs
->type
);
11365 gcc_assert (!specs
->long_p
&& !specs
->short_p
11366 && !specs
->signed_p
&& !specs
->unsigned_p
);
11367 specs
->type
= (specs
->complex_p
11368 ? complex_float_type_node
11369 : float_type_node
);
11372 gcc_assert (!specs
->long_long_p
&& !specs
->short_p
11373 && !specs
->signed_p
&& !specs
->unsigned_p
);
11376 specs
->type
= (specs
->complex_p
11377 ? complex_long_double_type_node
11378 : long_double_type_node
);
11382 specs
->type
= (specs
->complex_p
11383 ? complex_double_type_node
11384 : double_type_node
);
11387 case cts_floatn_nx
:
11388 gcc_assert (!specs
->long_p
&& !specs
->short_p
11389 && !specs
->signed_p
&& !specs
->unsigned_p
);
11390 if (FLOATN_NX_TYPE_NODE (specs
->floatn_nx_idx
) == NULL_TREE
)
11391 specs
->type
= integer_type_node
;
11392 else if (specs
->complex_p
)
11393 specs
->type
= COMPLEX_FLOATN_NX_TYPE_NODE (specs
->floatn_nx_idx
);
11395 specs
->type
= FLOATN_NX_TYPE_NODE (specs
->floatn_nx_idx
);
11399 case cts_dfloat128
:
11400 gcc_assert (!specs
->long_p
&& !specs
->long_long_p
&& !specs
->short_p
11401 && !specs
->signed_p
&& !specs
->unsigned_p
&& !specs
->complex_p
);
11402 if (specs
->typespec_word
== cts_dfloat32
)
11403 specs
->type
= dfloat32_type_node
;
11404 else if (specs
->typespec_word
== cts_dfloat64
)
11405 specs
->type
= dfloat64_type_node
;
11407 specs
->type
= dfloat128_type_node
;
11410 gcc_assert (!specs
->complex_p
);
11411 if (!targetm
.fixed_point_supported_p ())
11412 specs
->type
= integer_type_node
;
11413 else if (specs
->saturating_p
)
11415 if (specs
->long_long_p
)
11416 specs
->type
= specs
->unsigned_p
11417 ? sat_unsigned_long_long_fract_type_node
11418 : sat_long_long_fract_type_node
;
11419 else if (specs
->long_p
)
11420 specs
->type
= specs
->unsigned_p
11421 ? sat_unsigned_long_fract_type_node
11422 : sat_long_fract_type_node
;
11423 else if (specs
->short_p
)
11424 specs
->type
= specs
->unsigned_p
11425 ? sat_unsigned_short_fract_type_node
11426 : sat_short_fract_type_node
;
11428 specs
->type
= specs
->unsigned_p
11429 ? sat_unsigned_fract_type_node
11430 : sat_fract_type_node
;
11434 if (specs
->long_long_p
)
11435 specs
->type
= specs
->unsigned_p
11436 ? unsigned_long_long_fract_type_node
11437 : long_long_fract_type_node
;
11438 else if (specs
->long_p
)
11439 specs
->type
= specs
->unsigned_p
11440 ? unsigned_long_fract_type_node
11441 : long_fract_type_node
;
11442 else if (specs
->short_p
)
11443 specs
->type
= specs
->unsigned_p
11444 ? unsigned_short_fract_type_node
11445 : short_fract_type_node
;
11447 specs
->type
= specs
->unsigned_p
11448 ? unsigned_fract_type_node
11453 gcc_assert (!specs
->complex_p
);
11454 if (!targetm
.fixed_point_supported_p ())
11455 specs
->type
= integer_type_node
;
11456 else if (specs
->saturating_p
)
11458 if (specs
->long_long_p
)
11459 specs
->type
= specs
->unsigned_p
11460 ? sat_unsigned_long_long_accum_type_node
11461 : sat_long_long_accum_type_node
;
11462 else if (specs
->long_p
)
11463 specs
->type
= specs
->unsigned_p
11464 ? sat_unsigned_long_accum_type_node
11465 : sat_long_accum_type_node
;
11466 else if (specs
->short_p
)
11467 specs
->type
= specs
->unsigned_p
11468 ? sat_unsigned_short_accum_type_node
11469 : sat_short_accum_type_node
;
11471 specs
->type
= specs
->unsigned_p
11472 ? sat_unsigned_accum_type_node
11473 : sat_accum_type_node
;
11477 if (specs
->long_long_p
)
11478 specs
->type
= specs
->unsigned_p
11479 ? unsigned_long_long_accum_type_node
11480 : long_long_accum_type_node
;
11481 else if (specs
->long_p
)
11482 specs
->type
= specs
->unsigned_p
11483 ? unsigned_long_accum_type_node
11484 : long_accum_type_node
;
11485 else if (specs
->short_p
)
11486 specs
->type
= specs
->unsigned_p
11487 ? unsigned_short_accum_type_node
11488 : short_accum_type_node
;
11490 specs
->type
= specs
->unsigned_p
11491 ? unsigned_accum_type_node
11496 gcc_unreachable ();
11502 /* Perform final processing on one file scope's declarations (or the
11503 external scope's declarations), GLOBALS. */
11506 c_write_global_declarations_1 (tree globals
)
11511 /* Process the decls in the order they were written. */
11512 for (decl
= globals
; decl
; decl
= DECL_CHAIN (decl
))
11514 /* Check for used but undefined static functions using the C
11515 standard's definition of "used", and set TREE_NO_WARNING so
11516 that check_global_declaration doesn't repeat the check. */
11517 if (TREE_CODE (decl
) == FUNCTION_DECL
11518 && DECL_INITIAL (decl
) == NULL_TREE
11519 && DECL_EXTERNAL (decl
)
11520 && !TREE_PUBLIC (decl
))
11522 if (C_DECL_USED (decl
))
11524 if (pedwarn (input_location
, 0, "%q+F used but never defined",
11526 TREE_NO_WARNING (decl
) = 1;
11528 /* For -Wunused-function warn about unused static prototypes. */
11529 else if (warn_unused_function
11530 && ! DECL_ARTIFICIAL (decl
)
11531 && ! TREE_NO_WARNING (decl
))
11533 if (warning (OPT_Wunused_function
,
11534 "%q+F declared %<static%> but never defined",
11536 TREE_NO_WARNING (decl
) = 1;
11540 wrapup_global_declaration_1 (decl
);
11545 reconsider
= false;
11546 for (decl
= globals
; decl
; decl
= DECL_CHAIN (decl
))
11547 reconsider
|= wrapup_global_declaration_2 (decl
);
11549 while (reconsider
);
11552 /* Callback to collect a source_ref from a DECL. */
11555 collect_source_ref_cb (tree decl
)
11557 if (!DECL_IS_BUILTIN (decl
))
11558 collect_source_ref (LOCATION_FILE (decl_sloc (decl
, false)));
11561 /* Preserve the external declarations scope across a garbage collect. */
11562 static GTY(()) tree ext_block
;
11564 /* Collect all references relevant to SOURCE_FILE. */
11567 collect_all_refs (const char *source_file
)
11572 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
11573 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t
)), source_file
);
11575 collect_ada_nodes (BLOCK_VARS (ext_block
), source_file
);
11578 /* Iterate over all global declarations and call CALLBACK. */
11581 for_each_global_decl (void (*callback
) (tree decl
))
11588 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
11590 decls
= DECL_INITIAL (t
);
11591 for (decl
= BLOCK_VARS (decls
); decl
; decl
= TREE_CHAIN (decl
))
11595 for (decl
= BLOCK_VARS (ext_block
); decl
; decl
= TREE_CHAIN (decl
))
11599 /* Perform any final parser cleanups and generate initial debugging
11603 c_parse_final_cleanups (void)
11608 /* We don't want to do this if generating a PCH. */
11612 timevar_stop (TV_PHASE_PARSING
);
11613 timevar_start (TV_PHASE_DEFERRED
);
11615 /* Do the Objective-C stuff. This is where all the Objective-C
11616 module stuff gets generated (symtab, class/protocol/selector
11618 if (c_dialect_objc ())
11619 objc_write_global_declarations ();
11621 /* Close the external scope. */
11622 ext_block
= pop_scope ();
11623 external_scope
= 0;
11624 gcc_assert (!current_scope
);
11626 /* Handle -fdump-ada-spec[-slim]. */
11627 if (flag_dump_ada_spec
|| flag_dump_ada_spec_slim
)
11629 /* Build a table of files to generate specs for */
11630 if (flag_dump_ada_spec_slim
)
11631 collect_source_ref (main_input_filename
);
11633 for_each_global_decl (collect_source_ref_cb
);
11635 dump_ada_specs (collect_all_refs
, NULL
);
11638 /* Process all file scopes in this compilation, and the external_scope,
11639 through wrapup_global_declarations. */
11640 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
11641 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t
)));
11642 c_write_global_declarations_1 (BLOCK_VARS (ext_block
));
11644 timevar_stop (TV_PHASE_DEFERRED
);
11645 timevar_start (TV_PHASE_PARSING
);
11650 /* Register reserved keyword WORD as qualifier for address space AS. */
11653 c_register_addr_space (const char *word
, addr_space_t as
)
11655 int rid
= RID_FIRST_ADDR_SPACE
+ as
;
11658 /* Address space qualifiers are only supported
11659 in C with GNU extensions enabled. */
11660 if (c_dialect_objc () || flag_no_asm
)
11663 id
= get_identifier (word
);
11664 C_SET_RID_CODE (id
, rid
);
11665 C_IS_RESERVED_WORD (id
) = 1;
11666 ridpointers
[rid
] = id
;
11669 /* Return identifier to look up for omp declare reduction. */
11672 c_omp_reduction_id (enum tree_code reduction_code
, tree reduction_id
)
11674 const char *p
= NULL
;
11675 switch (reduction_code
)
11677 case PLUS_EXPR
: p
= "+"; break;
11678 case MULT_EXPR
: p
= "*"; break;
11679 case MINUS_EXPR
: p
= "-"; break;
11680 case BIT_AND_EXPR
: p
= "&"; break;
11681 case BIT_XOR_EXPR
: p
= "^"; break;
11682 case BIT_IOR_EXPR
: p
= "|"; break;
11683 case TRUTH_ANDIF_EXPR
: p
= "&&"; break;
11684 case TRUTH_ORIF_EXPR
: p
= "||"; break;
11685 case MIN_EXPR
: p
= "min"; break;
11686 case MAX_EXPR
: p
= "max"; break;
11693 if (TREE_CODE (reduction_id
) != IDENTIFIER_NODE
)
11694 return error_mark_node
;
11695 p
= IDENTIFIER_POINTER (reduction_id
);
11698 const char prefix
[] = "omp declare reduction ";
11699 size_t lenp
= sizeof (prefix
);
11700 size_t len
= strlen (p
);
11701 char *name
= XALLOCAVEC (char, lenp
+ len
);
11702 memcpy (name
, prefix
, lenp
- 1);
11703 memcpy (name
+ lenp
- 1, p
, len
+ 1);
11704 return get_identifier (name
);
11707 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11708 VAR_DECL, bind it into the current scope and return it. */
11711 c_omp_reduction_decl (tree reduction_id
)
11713 struct c_binding
*b
= I_SYMBOL_BINDING (reduction_id
);
11714 if (b
!= NULL
&& B_IN_CURRENT_SCOPE (b
))
11717 tree decl
= build_decl (BUILTINS_LOCATION
, VAR_DECL
,
11718 reduction_id
, integer_type_node
);
11719 DECL_ARTIFICIAL (decl
) = 1;
11720 DECL_EXTERNAL (decl
) = 1;
11721 TREE_STATIC (decl
) = 1;
11722 TREE_PUBLIC (decl
) = 0;
11723 bind (reduction_id
, decl
, current_scope
, true, false, BUILTINS_LOCATION
);
11727 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11730 c_omp_reduction_lookup (tree reduction_id
, tree type
)
11732 struct c_binding
*b
= I_SYMBOL_BINDING (reduction_id
);
11736 for (t
= DECL_INITIAL (b
->decl
); t
; t
= TREE_CHAIN (t
))
11737 if (comptypes (TREE_PURPOSE (t
), type
))
11738 return TREE_VALUE (t
);
11741 return error_mark_node
;
11744 /* Helper function called via walk_tree, to diagnose invalid
11745 #pragma omp declare reduction combiners or initializers. */
11748 c_check_omp_declare_reduction_r (tree
*tp
, int *, void *data
)
11750 tree
*vars
= (tree
*) data
;
11751 if (SSA_VAR_P (*tp
)
11752 && !DECL_ARTIFICIAL (*tp
)
11756 location_t loc
= DECL_SOURCE_LOCATION (vars
[0]);
11757 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars
[0])), "omp_out") == 0)
11758 error_at (loc
, "%<#pragma omp declare reduction%> combiner refers to "
11759 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11762 error_at (loc
, "%<#pragma omp declare reduction%> initializer refers "
11763 "to variable %qD which is not %<omp_priv%> nor "
11771 #include "gt-c-c-decl.h"