1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2020 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
) || !TREE_PUBLIC (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 /* Table of supported standard (C2x) attributes. */
4340 const struct attribute_spec std_attribute_table
[] =
4342 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4343 affects_type_identity, handler, exclude } */
4344 { "deprecated", 0, 1, false, false, false, false,
4345 handle_deprecated_attribute
, NULL
},
4346 { "fallthrough", 0, 0, false, false, false, false,
4347 handle_fallthrough_attribute
, NULL
},
4348 { "maybe_unused", 0, 0, false, false, false, false,
4349 handle_unused_attribute
, NULL
},
4350 { NULL
, 0, 0, false, false, false, false, NULL
, NULL
}
4353 /* Create the predefined scalar types of C,
4354 and some nodes representing standard constants (0, 1, (void *) 0).
4355 Initialize the global scope.
4356 Make definitions for built-in primitive functions. */
4359 c_init_decl_processing (void)
4361 location_t save_loc
= input_location
;
4363 /* Initialize reserved words for parser. */
4366 register_scoped_attributes (std_attribute_table
, NULL
);
4368 current_function_decl
= NULL_TREE
;
4370 gcc_obstack_init (&parser_obstack
);
4372 /* Make the externals scope. */
4374 external_scope
= current_scope
;
4376 /* Declarations from c_common_nodes_and_builtins must not be associated
4377 with this input file, lest we get differences between using and not
4378 using preprocessed headers. */
4379 input_location
= BUILTINS_LOCATION
;
4381 c_common_nodes_and_builtins ();
4383 /* In C, comparisons and TRUTH_* expressions have type int. */
4384 truthvalue_type_node
= integer_type_node
;
4385 truthvalue_true_node
= integer_one_node
;
4386 truthvalue_false_node
= integer_zero_node
;
4388 /* Even in C99, which has a real boolean type. */
4389 pushdecl (build_decl (UNKNOWN_LOCATION
, TYPE_DECL
, get_identifier ("_Bool"),
4390 boolean_type_node
));
4392 input_location
= save_loc
;
4394 make_fname_decl
= c_make_fname_decl
;
4395 start_fname_decls ();
4398 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4399 give the decl, NAME is the initialization string and TYPE_DEP
4400 indicates whether NAME depended on the type of the function. As we
4401 don't yet implement delayed emission of static data, we mark the
4402 decl as emitted so it is not placed in the output. Anything using
4403 it must therefore pull out the STRING_CST initializer directly.
4407 c_make_fname_decl (location_t loc
, tree id
, int type_dep
)
4409 const char *name
= fname_as_string (type_dep
);
4410 tree decl
, type
, init
;
4411 size_t length
= strlen (name
);
4413 type
= build_array_type (char_type_node
,
4414 build_index_type (size_int (length
)));
4415 type
= c_build_qualified_type (type
, TYPE_QUAL_CONST
);
4417 decl
= build_decl (loc
, VAR_DECL
, id
, type
);
4419 TREE_STATIC (decl
) = 1;
4420 TREE_READONLY (decl
) = 1;
4421 DECL_ARTIFICIAL (decl
) = 1;
4423 init
= build_string (length
+ 1, name
);
4424 free (CONST_CAST (char *, name
));
4425 TREE_TYPE (init
) = type
;
4426 DECL_INITIAL (decl
) = init
;
4428 TREE_USED (decl
) = 1;
4430 if (current_function_decl
4431 /* For invalid programs like this:
4434 const char* p = __FUNCTION__;
4436 the __FUNCTION__ is believed to appear in K&R style function
4437 parameter declarator. In that case we still don't have
4439 && current_function_scope
)
4441 DECL_CONTEXT (decl
) = current_function_decl
;
4442 bind (id
, decl
, current_function_scope
,
4443 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
4446 finish_decl (decl
, loc
, init
, NULL_TREE
, NULL_TREE
);
4452 c_builtin_function (tree decl
)
4454 tree type
= TREE_TYPE (decl
);
4455 tree id
= DECL_NAME (decl
);
4457 const char *name
= IDENTIFIER_POINTER (id
);
4458 C_DECL_BUILTIN_PROTOTYPE (decl
) = prototype_p (type
);
4460 /* Should never be called on a symbol with a preexisting meaning. */
4461 gcc_assert (!I_SYMBOL_BINDING (id
));
4463 bind (id
, decl
, external_scope
, /*invisible=*/true, /*nested=*/false,
4466 /* Builtins in the implementation namespace are made visible without
4467 needing to be explicitly declared. See push_file_scope. */
4468 if (name
[0] == '_' && (name
[1] == '_' || ISUPPER (name
[1])))
4470 DECL_CHAIN (decl
) = visible_builtins
;
4471 visible_builtins
= decl
;
4478 c_builtin_function_ext_scope (tree decl
)
4480 tree type
= TREE_TYPE (decl
);
4481 tree id
= DECL_NAME (decl
);
4483 const char *name
= IDENTIFIER_POINTER (id
);
4484 C_DECL_BUILTIN_PROTOTYPE (decl
) = prototype_p (type
);
4487 bind (id
, decl
, external_scope
, /*invisible=*/false, /*nested=*/false,
4490 /* Builtins in the implementation namespace are made visible without
4491 needing to be explicitly declared. See push_file_scope. */
4492 if (name
[0] == '_' && (name
[1] == '_' || ISUPPER (name
[1])))
4494 DECL_CHAIN (decl
) = visible_builtins
;
4495 visible_builtins
= decl
;
4501 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4504 c_simulate_builtin_function_decl (tree decl
)
4506 tree type
= TREE_TYPE (decl
);
4507 C_DECL_BUILTIN_PROTOTYPE (decl
) = prototype_p (type
);
4508 return pushdecl (decl
);
4511 /* Warn about attributes in a context where they are unused
4512 (attribute-declarations, except for the "fallthrough" case, and
4513 attributes on statements). */
4516 c_warn_unused_attributes (tree attrs
)
4518 for (tree t
= attrs
; t
!= NULL_TREE
; t
= TREE_CHAIN (t
))
4519 if (get_attribute_namespace (t
) == NULL_TREE
)
4520 /* The specifications of standard attributes mean this is a
4521 constraint violation. */
4522 pedwarn (input_location
, OPT_Wattributes
, "%qE attribute ignored",
4523 get_attribute_name (t
));
4525 warning (OPT_Wattributes
, "%qE attribute ignored",
4526 get_attribute_name (t
));
4529 /* Warn for standard attributes being applied to a type that is not
4530 being defined, where that is a constraint violation, and return a
4531 list of attributes with them removed. */
4534 c_warn_type_attributes (tree attrs
)
4536 tree
*attr_ptr
= &attrs
;
4538 if (get_attribute_namespace (*attr_ptr
) == NULL_TREE
)
4540 pedwarn (input_location
, OPT_Wattributes
, "%qE attribute ignored",
4541 get_attribute_name (*attr_ptr
));
4542 *attr_ptr
= TREE_CHAIN (*attr_ptr
);
4545 attr_ptr
= &TREE_CHAIN (*attr_ptr
);
4549 /* Called when a declaration is seen that contains no names to declare.
4550 If its type is a reference to a structure, union or enum inherited
4551 from a containing scope, shadow that tag name for the current scope
4552 with a forward reference.
4553 If its type defines a new named structure or union
4554 or defines an enum, it is valid but we need not do anything here.
4555 Otherwise, it is an error. */
4558 shadow_tag (const struct c_declspecs
*declspecs
)
4560 shadow_tag_warned (declspecs
, 0);
4563 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4566 shadow_tag_warned (const struct c_declspecs
*declspecs
, int warned
)
4568 bool found_tag
= false;
4570 if (declspecs
->type
&& !declspecs
->default_int_p
&& !declspecs
->typedef_p
)
4572 tree value
= declspecs
->type
;
4573 enum tree_code code
= TREE_CODE (value
);
4575 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
|| code
== ENUMERAL_TYPE
)
4576 /* Used to test also that TYPE_SIZE (value) != 0.
4577 That caused warning for `struct foo;' at top level in the file. */
4579 tree name
= TYPE_NAME (value
);
4584 if (declspecs
->restrict_p
)
4586 error ("invalid use of %<restrict%>");
4590 if (name
== NULL_TREE
)
4592 if (warned
!= 1 && code
!= ENUMERAL_TYPE
)
4593 /* Empty unnamed enum OK */
4595 pedwarn (input_location
, 0,
4596 "unnamed struct/union that defines no instances");
4600 else if (declspecs
->typespec_kind
!= ctsk_tagdef
4601 && declspecs
->typespec_kind
!= ctsk_tagfirstref
4602 && declspecs
->typespec_kind
!= ctsk_tagfirstref_attrs
4603 && declspecs
->storage_class
!= csc_none
)
4606 pedwarn (input_location
, 0,
4607 "empty declaration with storage class specifier "
4608 "does not redeclare tag");
4610 pending_xref_error ();
4612 else if (declspecs
->typespec_kind
!= ctsk_tagdef
4613 && declspecs
->typespec_kind
!= ctsk_tagfirstref
4614 && declspecs
->typespec_kind
!= ctsk_tagfirstref_attrs
4615 && (declspecs
->const_p
4616 || declspecs
->volatile_p
4617 || declspecs
->atomic_p
4618 || declspecs
->restrict_p
4619 || declspecs
->address_space
))
4622 pedwarn (input_location
, 0,
4623 "empty declaration with type qualifier "
4624 "does not redeclare tag");
4626 pending_xref_error ();
4628 else if (declspecs
->typespec_kind
!= ctsk_tagdef
4629 && declspecs
->typespec_kind
!= ctsk_tagfirstref
4630 && declspecs
->typespec_kind
!= ctsk_tagfirstref_attrs
4631 && declspecs
->alignas_p
)
4634 pedwarn (input_location
, 0,
4635 "empty declaration with %<_Alignas%> "
4636 "does not redeclare tag");
4638 pending_xref_error ();
4642 pending_invalid_xref
= NULL_TREE
;
4643 t
= lookup_tag (code
, name
, true, NULL
);
4647 t
= make_node (code
);
4648 pushtag (input_location
, name
, t
);
4654 if (warned
!= 1 && !in_system_header_at (input_location
))
4656 pedwarn (input_location
, 0,
4657 "useless type name in empty declaration");
4662 else if (warned
!= 1 && !in_system_header_at (input_location
)
4663 && declspecs
->typedef_p
)
4665 pedwarn (input_location
, 0, "useless type name in empty declaration");
4669 pending_invalid_xref
= NULL_TREE
;
4671 if (declspecs
->inline_p
)
4673 error ("%<inline%> in empty declaration");
4677 if (declspecs
->noreturn_p
)
4679 error ("%<_Noreturn%> in empty declaration");
4683 if (current_scope
== file_scope
&& declspecs
->storage_class
== csc_auto
)
4685 error ("%<auto%> in file-scope empty declaration");
4689 if (current_scope
== file_scope
&& declspecs
->storage_class
== csc_register
)
4691 error ("%<register%> in file-scope empty declaration");
4695 if (!warned
&& !in_system_header_at (input_location
)
4696 && declspecs
->storage_class
!= csc_none
)
4698 warning (0, "useless storage class specifier in empty declaration");
4702 if (!warned
&& !in_system_header_at (input_location
) && declspecs
->thread_p
)
4704 warning (0, "useless %qs in empty declaration",
4705 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
4710 && !in_system_header_at (input_location
)
4711 && (declspecs
->const_p
4712 || declspecs
->volatile_p
4713 || declspecs
->atomic_p
4714 || declspecs
->restrict_p
4715 || declspecs
->address_space
))
4717 warning (0, "useless type qualifier in empty declaration");
4721 if (!warned
&& !in_system_header_at (input_location
)
4722 && declspecs
->alignas_p
)
4724 warning (0, "useless %<_Alignas%> in empty declaration");
4730 && (declspecs
->typespec_kind
== ctsk_tagref_attrs
4731 || declspecs
->typespec_kind
== ctsk_tagfirstref_attrs
))
4733 /* Standard attributes after the "struct" or "union" keyword are
4734 only permitted when the contents of the type are defined, or
4735 in the form "struct-or-union attribute-specifier-sequence
4736 identifier;". If the ';' was not present, attributes were
4737 diagnosed in the parser. Here, ensure that any other useless
4738 elements of the declaration result in a pedwarn, not just a
4739 warning. Forward declarations of enum types are not part of
4740 standard C, but handle them the same. */
4741 pedwarn (input_location
, 0,
4742 "invalid use of attributes in empty declaration");
4748 if (declspecs
->declspecs_seen_p
4749 && !declspecs
->non_std_attrs_seen_p
)
4750 /* An attribute declaration (but not a fallthrough attribute
4751 declaration, which was handled separately); warn if there
4752 are any attributes being ignored (but not if the attributes
4754 c_warn_unused_attributes (declspecs
->attrs
);
4755 else if (!found_tag
)
4756 pedwarn (input_location
, 0, "empty declaration");
4761 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4762 bits. SPECS represents declaration specifiers that the grammar
4763 only permits to contain type qualifiers and attributes. */
4766 quals_from_declspecs (const struct c_declspecs
*specs
)
4768 int quals
= ((specs
->const_p
? TYPE_QUAL_CONST
: 0)
4769 | (specs
->volatile_p
? TYPE_QUAL_VOLATILE
: 0)
4770 | (specs
->restrict_p
? TYPE_QUAL_RESTRICT
: 0)
4771 | (specs
->atomic_p
? TYPE_QUAL_ATOMIC
: 0)
4772 | (ENCODE_QUAL_ADDR_SPACE (specs
->address_space
)));
4773 gcc_assert (!specs
->type
4774 && !specs
->decl_attr
4775 && specs
->typespec_word
== cts_none
4776 && specs
->storage_class
== csc_none
4777 && !specs
->typedef_p
4778 && !specs
->explicit_signed_p
4779 && !specs
->deprecated_p
4781 && !specs
->long_long_p
4784 && !specs
->unsigned_p
4785 && !specs
->complex_p
4787 && !specs
->noreturn_p
4788 && !specs
->thread_p
);
4792 /* Construct an array declarator. LOC is the location of the
4793 beginning of the array (usually the opening brace). EXPR is the
4794 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4795 inside the [] (to be applied to the pointer to which a parameter
4796 array is converted). STATIC_P is true if "static" is inside the
4797 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4798 VLA of unspecified length which is nevertheless a complete type,
4799 false otherwise. The field for the contained declarator is left to
4800 be filled in by set_array_declarator_inner. */
4802 struct c_declarator
*
4803 build_array_declarator (location_t loc
,
4804 tree expr
, struct c_declspecs
*quals
, bool static_p
,
4807 struct c_declarator
*declarator
= XOBNEW (&parser_obstack
,
4808 struct c_declarator
);
4809 declarator
->id_loc
= loc
;
4810 declarator
->kind
= cdk_array
;
4811 declarator
->declarator
= 0;
4812 declarator
->u
.array
.dimen
= expr
;
4815 declarator
->u
.array
.attrs
= quals
->attrs
;
4816 declarator
->u
.array
.quals
= quals_from_declspecs (quals
);
4820 declarator
->u
.array
.attrs
= NULL_TREE
;
4821 declarator
->u
.array
.quals
= 0;
4823 declarator
->u
.array
.static_p
= static_p
;
4824 declarator
->u
.array
.vla_unspec_p
= vla_unspec_p
;
4825 if (static_p
|| quals
!= NULL
)
4826 pedwarn_c90 (loc
, OPT_Wpedantic
,
4827 "ISO C90 does not support %<static%> or type "
4828 "qualifiers in parameter array declarators");
4830 pedwarn_c90 (loc
, OPT_Wpedantic
,
4831 "ISO C90 does not support %<[*]%> array declarators");
4834 if (!current_scope
->parm_flag
)
4837 error_at (loc
, "%<[*]%> not allowed in other than "
4838 "function prototype scope");
4839 declarator
->u
.array
.vla_unspec_p
= false;
4842 current_scope
->had_vla_unspec
= true;
4847 /* Set the contained declarator of an array declarator. DECL is the
4848 declarator, as constructed by build_array_declarator; INNER is what
4849 appears on the left of the []. */
4851 struct c_declarator
*
4852 set_array_declarator_inner (struct c_declarator
*decl
,
4853 struct c_declarator
*inner
)
4855 decl
->declarator
= inner
;
4859 /* INIT is a constructor that forms DECL's initializer. If the final
4860 element initializes a flexible array field, add the size of that
4861 initializer to DECL's size. */
4864 add_flexible_array_elts_to_size (tree decl
, tree init
)
4868 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
4871 elt
= CONSTRUCTOR_ELTS (init
)->last ().value
;
4872 type
= TREE_TYPE (elt
);
4873 if (TREE_CODE (type
) == ARRAY_TYPE
4874 && TYPE_SIZE (type
) == NULL_TREE
4875 && TYPE_DOMAIN (type
) != NULL_TREE
4876 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
)
4878 complete_array_type (&type
, elt
, false);
4880 = size_binop (PLUS_EXPR
, DECL_SIZE (decl
), TYPE_SIZE (type
));
4881 DECL_SIZE_UNIT (decl
)
4882 = size_binop (PLUS_EXPR
, DECL_SIZE_UNIT (decl
), TYPE_SIZE_UNIT (type
));
4886 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4887 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4888 before the type name, and set *EXPR_CONST_OPERANDS, if
4889 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4890 appear in a constant expression. */
4893 groktypename (struct c_type_name
*type_name
, tree
*expr
,
4894 bool *expr_const_operands
)
4897 tree attrs
= type_name
->specs
->attrs
;
4899 type_name
->specs
->attrs
= NULL_TREE
;
4901 type
= grokdeclarator (type_name
->declarator
, type_name
->specs
, TYPENAME
,
4902 false, NULL
, &attrs
, expr
, expr_const_operands
,
4905 /* Apply attributes. */
4906 attrs
= c_warn_type_attributes (attrs
);
4907 decl_attributes (&type
, attrs
, 0);
4912 /* Wrapper for decl_attributes that adds some implicit attributes
4913 to VAR_DECLs or FUNCTION_DECLs. */
4916 c_decl_attributes (tree
*node
, tree attributes
, int flags
)
4918 /* Add implicit "omp declare target" attribute if requested. */
4919 if (current_omp_declare_target_attribute
4920 && ((VAR_P (*node
) && is_global_var (*node
))
4921 || TREE_CODE (*node
) == FUNCTION_DECL
))
4924 && !lang_hooks
.types
.omp_mappable_type (TREE_TYPE (*node
)))
4925 attributes
= tree_cons (get_identifier ("omp declare target implicit"),
4926 NULL_TREE
, attributes
);
4929 attributes
= tree_cons (get_identifier ("omp declare target"),
4930 NULL_TREE
, attributes
);
4931 attributes
= tree_cons (get_identifier ("omp declare target block"),
4932 NULL_TREE
, attributes
);
4936 /* Look up the current declaration with all the attributes merged
4937 so far so that attributes on the current declaration that's
4938 about to be pushed that conflict with the former can be detected,
4939 diagnosed, and rejected as appropriate. */
4940 tree last_decl
= lookup_name (DECL_NAME (*node
));
4942 last_decl
= lookup_name_in_scope (DECL_NAME (*node
), external_scope
);
4944 return decl_attributes (node
, attributes
, flags
, last_decl
);
4948 /* Decode a declarator in an ordinary declaration or data definition.
4949 This is called as soon as the type information and variable name
4950 have been parsed, before parsing the initializer if any.
4951 Here we create the ..._DECL node, fill in its type,
4952 and put it on the list of decls for the current context.
4953 The ..._DECL node is returned as the value.
4955 Exception: for arrays where the length is not specified,
4956 the type is left null, to be filled in by `finish_decl'.
4958 Function definitions do not come here; they go to start_function
4959 instead. However, external and forward declarations of functions
4960 do go through here. Structure field declarations are done by
4961 grokfield and not through here. */
4964 start_decl (struct c_declarator
*declarator
, struct c_declspecs
*declspecs
,
4965 bool initialized
, tree attributes
)
4969 tree expr
= NULL_TREE
;
4970 enum deprecated_states deprecated_state
= DEPRECATED_NORMAL
;
4972 /* An object declared as __attribute__((deprecated)) suppresses
4973 warnings of uses of other deprecated items. */
4974 if (lookup_attribute ("deprecated", attributes
))
4975 deprecated_state
= DEPRECATED_SUPPRESS
;
4977 decl
= grokdeclarator (declarator
, declspecs
,
4978 NORMAL
, initialized
, NULL
, &attributes
, &expr
, NULL
,
4980 if (!decl
|| decl
== error_mark_node
)
4984 add_stmt (fold_convert (void_type_node
, expr
));
4986 if (TREE_CODE (decl
) != FUNCTION_DECL
&& MAIN_NAME_P (DECL_NAME (decl
))
4987 && TREE_PUBLIC (decl
))
4988 warning (OPT_Wmain
, "%q+D is usually a function", decl
);
4991 /* Is it valid for this decl to have an initializer at all?
4992 If not, set INITIALIZED to zero, which will indirectly
4993 tell 'finish_decl' to ignore the initializer once it is parsed. */
4994 switch (TREE_CODE (decl
))
4997 error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl
);
4998 initialized
= false;
5002 error ("function %qD is initialized like a variable", decl
);
5003 initialized
= false;
5007 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5008 error ("parameter %qD is initialized", decl
);
5009 initialized
= false;
5013 /* Don't allow initializations for incomplete types except for
5014 arrays which might be completed by the initialization. */
5016 /* This can happen if the array size is an undefined macro.
5017 We already gave a warning, so we don't need another one. */
5018 if (TREE_TYPE (decl
) == error_mark_node
)
5019 initialized
= false;
5020 else if (COMPLETE_TYPE_P (TREE_TYPE (decl
)))
5022 /* A complete type is ok if size is fixed. */
5024 if (!poly_int_tree_p (TYPE_SIZE (TREE_TYPE (decl
)))
5025 || C_DECL_VARIABLE_SIZE (decl
))
5027 error ("variable-sized object may not be initialized");
5028 initialized
= false;
5031 else if (TREE_CODE (TREE_TYPE (decl
)) != ARRAY_TYPE
)
5033 error ("variable %qD has initializer but incomplete type", decl
);
5034 initialized
= false;
5036 else if (C_DECL_VARIABLE_SIZE (decl
))
5038 /* Although C99 is unclear about whether incomplete arrays
5039 of VLAs themselves count as VLAs, it does not make
5040 sense to permit them to be initialized given that
5041 ordinary VLAs may not be initialized. */
5042 error ("variable-sized object may not be initialized");
5043 initialized
= false;
5049 if (current_scope
== file_scope
)
5050 TREE_STATIC (decl
) = 1;
5052 /* Tell 'pushdecl' this is an initialized decl
5053 even though we don't yet have the initializer expression.
5054 Also tell 'finish_decl' it may store the real initializer. */
5055 DECL_INITIAL (decl
) = error_mark_node
;
5058 /* If this is a function declaration, write a record describing it to the
5059 prototypes file (if requested). */
5061 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5062 gen_aux_info_record (decl
, 0, 0, prototype_p (TREE_TYPE (decl
)));
5064 /* ANSI specifies that a tentative definition which is not merged with
5065 a non-tentative definition behaves exactly like a definition with an
5066 initializer equal to zero. (Section 3.7.2)
5068 -fno-common gives strict ANSI behavior, though this tends to break
5069 a large body of code that grew up without this rule.
5071 Thread-local variables are never common, since there's no entrenched
5072 body of code to break, and it allows more efficient variable references
5073 in the presence of dynamic linking. */
5077 && TREE_PUBLIC (decl
)
5078 && !DECL_THREAD_LOCAL_P (decl
)
5080 DECL_COMMON (decl
) = 1;
5082 /* Set attributes here so if duplicate decl, will have proper attributes. */
5083 c_decl_attributes (&decl
, attributes
, 0);
5085 /* Handle gnu_inline attribute. */
5086 if (declspecs
->inline_p
5087 && !flag_gnu89_inline
5088 && TREE_CODE (decl
) == FUNCTION_DECL
5089 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl
))
5090 || current_function_decl
))
5092 if (declspecs
->storage_class
== csc_auto
&& current_scope
!= file_scope
)
5094 else if (declspecs
->storage_class
!= csc_static
)
5095 DECL_EXTERNAL (decl
) = !DECL_EXTERNAL (decl
);
5098 if (TREE_CODE (decl
) == FUNCTION_DECL
5099 && targetm
.calls
.promote_prototypes (TREE_TYPE (decl
)))
5101 struct c_declarator
*ce
= declarator
;
5103 if (ce
->kind
== cdk_pointer
)
5104 ce
= declarator
->declarator
;
5105 if (ce
->kind
== cdk_function
)
5107 tree args
= ce
->u
.arg_info
->parms
;
5108 for (; args
; args
= DECL_CHAIN (args
))
5110 tree type
= TREE_TYPE (args
);
5111 if (type
&& INTEGRAL_TYPE_P (type
)
5112 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
5113 DECL_ARG_TYPE (args
) = c_type_promotes_to (type
);
5118 if (TREE_CODE (decl
) == FUNCTION_DECL
5119 && DECL_DECLARED_INLINE_P (decl
)
5120 && DECL_UNINLINABLE (decl
)
5121 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl
)))
5122 warning (OPT_Wattributes
, "inline function %q+D given attribute %qs",
5125 /* C99 6.7.4p3: An inline definition of a function with external
5126 linkage shall not contain a definition of a modifiable object
5127 with static storage duration... */
5129 && current_scope
!= file_scope
5130 && TREE_STATIC (decl
)
5131 && !TREE_READONLY (decl
)
5132 && DECL_DECLARED_INLINE_P (current_function_decl
)
5133 && DECL_EXTERNAL (current_function_decl
))
5134 record_inline_static (input_location
, current_function_decl
,
5135 decl
, csi_modifiable
);
5137 if (c_dialect_objc ()
5138 && VAR_OR_FUNCTION_DECL_P (decl
))
5139 objc_check_global_decl (decl
);
5141 /* Add this decl to the current scope.
5142 TEM may equal DECL or it may be a previous decl of the same name. */
5143 tem
= pushdecl (decl
);
5145 if (initialized
&& DECL_EXTERNAL (tem
))
5147 DECL_EXTERNAL (tem
) = 0;
5148 TREE_STATIC (tem
) = 1;
5154 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5155 DECL or the non-array element type if DECL is an uninitialized array.
5156 If that type has a const member, diagnose this. */
5159 diagnose_uninitialized_cst_member (tree decl
, tree type
)
5162 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
5165 if (TREE_CODE (field
) != FIELD_DECL
)
5167 field_type
= strip_array_types (TREE_TYPE (field
));
5169 if (TYPE_QUALS (field_type
) & TYPE_QUAL_CONST
)
5171 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
5172 "uninitialized const member in %qT is invalid in C++",
5173 strip_array_types (TREE_TYPE (decl
)));
5174 inform (DECL_SOURCE_LOCATION (field
), "%qD should be initialized", field
);
5177 if (RECORD_OR_UNION_TYPE_P (field_type
))
5178 diagnose_uninitialized_cst_member (decl
, field_type
);
5182 /* Finish processing of a declaration;
5183 install its initial value.
5184 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5185 If the length of an array type is not known before,
5186 it must be determined now, from the initial value, or it is an error.
5188 INIT_LOC is the location of the initial value. */
5191 finish_decl (tree decl
, location_t init_loc
, tree init
,
5192 tree origtype
, tree asmspec_tree
)
5195 bool was_incomplete
= (DECL_SIZE (decl
) == NULL_TREE
);
5196 const char *asmspec
= 0;
5198 /* If a name was specified, get the string. */
5199 if (VAR_OR_FUNCTION_DECL_P (decl
)
5200 && DECL_FILE_SCOPE_P (decl
))
5201 asmspec_tree
= maybe_apply_renaming_pragma (decl
, asmspec_tree
);
5203 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
5206 && TREE_STATIC (decl
)
5207 && global_bindings_p ())
5208 /* So decl is a global variable. Record the types it uses
5209 so that we can decide later to emit debug info for them. */
5210 record_types_used_by_current_var_decl (decl
);
5212 /* If `start_decl' didn't like having an initialization, ignore it now. */
5213 if (init
!= NULL_TREE
&& DECL_INITIAL (decl
) == NULL_TREE
)
5216 /* Don't crash if parm is initialized. */
5217 if (TREE_CODE (decl
) == PARM_DECL
)
5221 store_init_value (init_loc
, decl
, init
, origtype
);
5223 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl
)
5224 || TREE_CODE (decl
) == FIELD_DECL
))
5225 objc_check_decl (decl
);
5227 type
= TREE_TYPE (decl
);
5229 /* Deduce size of array from initialization, if not already known.
5230 This is only needed for an initialization in the current scope;
5231 it must not be done for a file-scope initialization of a
5232 declaration with external linkage, redeclared in an inner scope
5233 with the outer declaration shadowed in an intermediate scope. */
5234 if (TREE_CODE (type
) == ARRAY_TYPE
5235 && TYPE_DOMAIN (type
) == NULL_TREE
5236 && TREE_CODE (decl
) != TYPE_DECL
5237 && !(TREE_PUBLIC (decl
) && current_scope
!= file_scope
))
5240 = (TREE_STATIC (decl
)
5241 /* Even if pedantic, an external linkage array
5242 may have incomplete type at first. */
5243 ? pedantic
&& !TREE_PUBLIC (decl
)
5244 : !DECL_EXTERNAL (decl
));
5246 = complete_array_type (&TREE_TYPE (decl
), DECL_INITIAL (decl
),
5249 /* Get the completed type made by complete_array_type. */
5250 type
= TREE_TYPE (decl
);
5255 error ("initializer fails to determine size of %q+D", decl
);
5260 error ("array size missing in %q+D", decl
);
5264 error ("zero or negative size array %q+D", decl
);
5268 /* For global variables, update the copy of the type that
5269 exists in the binding. */
5270 if (TREE_PUBLIC (decl
))
5272 struct c_binding
*b_ext
= I_SYMBOL_BINDING (DECL_NAME (decl
));
5273 while (b_ext
&& !B_IN_EXTERNAL_SCOPE (b_ext
))
5274 b_ext
= b_ext
->shadowed
;
5275 if (b_ext
&& TREE_CODE (decl
) == TREE_CODE (b_ext
->decl
))
5277 if (b_ext
->u
.type
&& comptypes (b_ext
->u
.type
, type
))
5278 b_ext
->u
.type
= composite_type (b_ext
->u
.type
, type
);
5280 b_ext
->u
.type
= type
;
5289 if (DECL_INITIAL (decl
))
5290 TREE_TYPE (DECL_INITIAL (decl
)) = type
;
5292 relayout_decl (decl
);
5295 /* Look for braced array initializers for character arrays and
5296 recursively convert them into STRING_CSTs. */
5297 if (tree init
= DECL_INITIAL (decl
))
5298 DECL_INITIAL (decl
) = braced_lists_to_strings (type
, init
);
5302 if (init
&& TREE_CODE (init
) == CONSTRUCTOR
)
5303 add_flexible_array_elts_to_size (decl
, init
);
5305 complete_flexible_array_elts (DECL_INITIAL (decl
));
5307 if (is_global_var (decl
))
5309 type_context_kind context
= (DECL_THREAD_LOCAL_P (decl
)
5310 ? TCTX_THREAD_STORAGE
5311 : TCTX_STATIC_STORAGE
);
5312 if (!verify_type_context (input_location
, context
, TREE_TYPE (decl
)))
5313 TREE_TYPE (decl
) = error_mark_node
;
5316 if (DECL_SIZE (decl
) == NULL_TREE
&& TREE_TYPE (decl
) != error_mark_node
5317 && COMPLETE_TYPE_P (TREE_TYPE (decl
)))
5318 layout_decl (decl
, 0);
5320 if (DECL_SIZE (decl
) == NULL_TREE
5321 /* Don't give an error if we already gave one earlier. */
5322 && TREE_TYPE (decl
) != error_mark_node
5323 && (TREE_STATIC (decl
)
5324 /* A static variable with an incomplete type
5325 is an error if it is initialized.
5326 Also if it is not file scope.
5327 Otherwise, let it through, but if it is not `extern'
5328 then it may cause an error message later. */
5329 ? (DECL_INITIAL (decl
) != NULL_TREE
5330 || !DECL_FILE_SCOPE_P (decl
))
5331 /* An automatic variable with an incomplete type
5333 : !DECL_EXTERNAL (decl
)))
5335 error ("storage size of %q+D isn%'t known", decl
);
5336 TREE_TYPE (decl
) = error_mark_node
;
5339 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
))
5340 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
5341 && DECL_SIZE (decl
) == NULL_TREE
5342 && TREE_STATIC (decl
))
5343 incomplete_record_decls
.safe_push (decl
);
5345 if (is_global_var (decl
)
5346 && DECL_SIZE (decl
) != NULL_TREE
5347 && TREE_TYPE (decl
) != error_mark_node
)
5349 if (TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
)
5350 constant_expression_warning (DECL_SIZE (decl
));
5353 error ("storage size of %q+D isn%'t constant", decl
);
5354 TREE_TYPE (decl
) = error_mark_node
;
5358 if (TREE_USED (type
))
5360 TREE_USED (decl
) = 1;
5361 DECL_READ_P (decl
) = 1;
5365 /* If this is a function and an assembler name is specified, reset DECL_RTL
5366 so we can give it its new name. Also, update builtin_decl if it
5367 was a normal built-in. */
5368 if (TREE_CODE (decl
) == FUNCTION_DECL
&& asmspec
)
5370 if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
5371 set_builtin_user_assembler_name (decl
, asmspec
);
5372 set_user_assembler_name (decl
, asmspec
);
5375 /* If #pragma weak was used, mark the decl weak now. */
5376 maybe_apply_pragma_weak (decl
);
5378 /* Output the assembler code and/or RTL code for variables and functions,
5379 unless the type is an undefined structure or union.
5380 If not, it will get done when the type is completed. */
5382 if (VAR_OR_FUNCTION_DECL_P (decl
))
5384 /* Determine the ELF visibility. */
5385 if (TREE_PUBLIC (decl
))
5386 c_determine_visibility (decl
);
5388 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5389 if (c_dialect_objc ())
5390 objc_check_decl (decl
);
5394 /* If this is not a static variable, issue a warning.
5395 It doesn't make any sense to give an ASMSPEC for an
5396 ordinary, non-register local variable. Historically,
5397 GCC has accepted -- but ignored -- the ASMSPEC in
5399 if (!DECL_FILE_SCOPE_P (decl
)
5401 && !C_DECL_REGISTER (decl
)
5402 && !TREE_STATIC (decl
))
5403 warning (0, "ignoring %<asm%> specifier for non-static local "
5404 "variable %q+D", decl
);
5406 set_user_assembler_name (decl
, asmspec
);
5409 if (DECL_FILE_SCOPE_P (decl
))
5411 if (DECL_INITIAL (decl
) == NULL_TREE
5412 || DECL_INITIAL (decl
) == error_mark_node
)
5413 /* Don't output anything
5414 when a tentative file-scope definition is seen.
5415 But at end of compilation, do output code for them. */
5416 DECL_DEFER_OUTPUT (decl
) = 1;
5417 if (asmspec
&& VAR_P (decl
) && C_DECL_REGISTER (decl
))
5418 DECL_HARD_REGISTER (decl
) = 1;
5419 rest_of_decl_compilation (decl
, true, 0);
5423 /* In conjunction with an ASMSPEC, the `register'
5424 keyword indicates that we should place the variable
5425 in a particular register. */
5426 if (asmspec
&& C_DECL_REGISTER (decl
))
5428 DECL_HARD_REGISTER (decl
) = 1;
5429 /* This cannot be done for a structure with volatile
5430 fields, on which DECL_REGISTER will have been
5432 if (!DECL_REGISTER (decl
))
5433 error ("cannot put object with volatile field into register");
5436 if (TREE_CODE (decl
) != FUNCTION_DECL
)
5438 /* If we're building a variable sized type, and we might be
5439 reachable other than via the top of the current binding
5440 level, then create a new BIND_EXPR so that we deallocate
5441 the object at the right time. */
5442 /* Note that DECL_SIZE can be null due to errors. */
5443 if (DECL_SIZE (decl
)
5444 && !TREE_CONSTANT (DECL_SIZE (decl
))
5445 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list
))
5448 bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, NULL
);
5449 TREE_SIDE_EFFECTS (bind
) = 1;
5451 BIND_EXPR_BODY (bind
) = push_stmt_list ();
5453 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl
),
5459 if (!DECL_FILE_SCOPE_P (decl
))
5461 /* Recompute the RTL of a local array now
5462 if it used to be an incomplete type. */
5463 if (was_incomplete
&& !is_global_var (decl
))
5465 /* If we used it already as memory, it must stay in memory. */
5466 TREE_ADDRESSABLE (decl
) = TREE_USED (decl
);
5467 /* If it's still incomplete now, no init will save it. */
5468 if (DECL_SIZE (decl
) == NULL_TREE
)
5469 DECL_INITIAL (decl
) = NULL_TREE
;
5474 if (TREE_CODE (decl
) == TYPE_DECL
)
5476 if (!DECL_FILE_SCOPE_P (decl
)
5477 && variably_modified_type_p (TREE_TYPE (decl
), NULL_TREE
))
5478 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl
), DECL_EXPR
, decl
));
5480 rest_of_decl_compilation (decl
, DECL_FILE_SCOPE_P (decl
), 0);
5483 /* Install a cleanup (aka destructor) if one was given. */
5484 if (VAR_P (decl
) && !TREE_STATIC (decl
))
5486 tree attr
= lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl
));
5489 tree cleanup_id
= TREE_VALUE (TREE_VALUE (attr
));
5490 tree cleanup_decl
= lookup_name (cleanup_id
);
5492 vec
<tree
, va_gc
> *v
;
5494 /* Build "cleanup(&decl)" for the destructor. */
5495 cleanup
= build_unary_op (input_location
, ADDR_EXPR
, decl
, false);
5497 v
->quick_push (cleanup
);
5498 cleanup
= c_build_function_call_vec (DECL_SOURCE_LOCATION (decl
),
5499 vNULL
, cleanup_decl
, v
, NULL
);
5502 /* Don't warn about decl unused; the cleanup uses it. */
5503 TREE_USED (decl
) = 1;
5504 TREE_USED (cleanup_decl
) = 1;
5505 DECL_READ_P (decl
) = 1;
5507 push_cleanup (decl
, cleanup
, false);
5513 && !DECL_EXTERNAL (decl
)
5514 && DECL_INITIAL (decl
) == NULL_TREE
)
5516 type
= strip_array_types (type
);
5517 if (TREE_READONLY (decl
))
5518 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
5519 "uninitialized %<const %D%> is invalid in C++", decl
);
5520 else if (RECORD_OR_UNION_TYPE_P (type
)
5521 && C_TYPE_FIELDS_READONLY (type
))
5522 diagnose_uninitialized_cst_member (decl
, type
);
5527 && lookup_attribute ("omp declare target implicit",
5528 DECL_ATTRIBUTES (decl
)))
5530 DECL_ATTRIBUTES (decl
)
5531 = remove_attribute ("omp declare target implicit",
5532 DECL_ATTRIBUTES (decl
));
5533 if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (decl
)))
5534 error ("%q+D in declare target directive does not have mappable type",
5536 else if (!lookup_attribute ("omp declare target",
5537 DECL_ATTRIBUTES (decl
))
5538 && !lookup_attribute ("omp declare target link",
5539 DECL_ATTRIBUTES (decl
)))
5540 DECL_ATTRIBUTES (decl
)
5541 = tree_cons (get_identifier ("omp declare target"),
5542 NULL_TREE
, DECL_ATTRIBUTES (decl
));
5545 invoke_plugin_callbacks (PLUGIN_FINISH_DECL
, decl
);
5548 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5549 EXPR is NULL or a pointer to an expression that needs to be
5550 evaluated for the side effects of array size expressions in the
5554 grokparm (const struct c_parm
*parm
, tree
*expr
)
5556 tree attrs
= parm
->attrs
;
5557 tree decl
= grokdeclarator (parm
->declarator
, parm
->specs
, PARM
, false,
5558 NULL
, &attrs
, expr
, NULL
, DEPRECATED_NORMAL
);
5560 decl_attributes (&decl
, attrs
, 0);
5565 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5566 and push that on the current scope. EXPR is a pointer to an
5567 expression that needs to be evaluated for the side effects of array
5568 size expressions in the parameters. */
5571 push_parm_decl (const struct c_parm
*parm
, tree
*expr
)
5573 tree attrs
= parm
->attrs
;
5576 decl
= grokdeclarator (parm
->declarator
, parm
->specs
, PARM
, false, NULL
,
5577 &attrs
, expr
, NULL
, DEPRECATED_NORMAL
);
5578 if (decl
&& DECL_P (decl
))
5579 DECL_SOURCE_LOCATION (decl
) = parm
->loc
;
5580 decl_attributes (&decl
, attrs
, 0);
5582 decl
= pushdecl (decl
);
5584 finish_decl (decl
, input_location
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
5587 /* Mark all the parameter declarations to date as forward decls.
5588 Also diagnose use of this extension. */
5591 mark_forward_parm_decls (void)
5593 struct c_binding
*b
;
5595 if (pedantic
&& !current_scope
->warned_forward_parm_decls
)
5597 pedwarn (input_location
, OPT_Wpedantic
,
5598 "ISO C forbids forward parameter declarations");
5599 current_scope
->warned_forward_parm_decls
= true;
5602 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
5603 if (TREE_CODE (b
->decl
) == PARM_DECL
)
5604 TREE_ASM_WRITTEN (b
->decl
) = 1;
5607 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5608 literal, which may be an incomplete array type completed by the
5609 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5610 literal. NON_CONST is true if the initializers contain something
5611 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
5612 it is the (valid) alignment for this compound literal, as specified
5616 build_compound_literal (location_t loc
, tree type
, tree init
, bool non_const
,
5617 unsigned int alignas_align
)
5619 /* We do not use start_decl here because we have a type, not a declarator;
5620 and do not use finish_decl because the decl should be stored inside
5621 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5626 if (type
== error_mark_node
5627 || init
== error_mark_node
)
5628 return error_mark_node
;
5630 decl
= build_decl (loc
, VAR_DECL
, NULL_TREE
, type
);
5631 DECL_EXTERNAL (decl
) = 0;
5632 TREE_PUBLIC (decl
) = 0;
5633 TREE_STATIC (decl
) = (current_scope
== file_scope
);
5634 DECL_CONTEXT (decl
) = current_function_decl
;
5635 TREE_USED (decl
) = 1;
5636 DECL_READ_P (decl
) = 1;
5637 DECL_ARTIFICIAL (decl
) = 1;
5638 DECL_IGNORED_P (decl
) = 1;
5639 C_DECL_COMPOUND_LITERAL_P (decl
) = 1;
5640 TREE_TYPE (decl
) = type
;
5641 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type
)), decl
);
5644 SET_DECL_ALIGN (decl
, alignas_align
* BITS_PER_UNIT
);
5645 DECL_USER_ALIGN (decl
) = 1;
5647 store_init_value (loc
, decl
, init
, NULL_TREE
);
5649 if (TREE_CODE (type
) == ARRAY_TYPE
&& !COMPLETE_TYPE_P (type
))
5651 int failure
= complete_array_type (&TREE_TYPE (decl
),
5652 DECL_INITIAL (decl
), true);
5653 /* If complete_array_type returns 3, it means that the
5654 initial value of the compound literal is empty. Allow it. */
5655 gcc_assert (failure
== 0 || failure
== 3);
5657 type
= TREE_TYPE (decl
);
5658 TREE_TYPE (DECL_INITIAL (decl
)) = type
;
5661 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
5663 c_incomplete_type_error (loc
, NULL_TREE
, type
);
5664 return error_mark_node
;
5667 if (TREE_STATIC (decl
)
5668 && !verify_type_context (loc
, TCTX_STATIC_STORAGE
, type
))
5669 return error_mark_node
;
5671 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), DECL_EXPR
, decl
);
5672 complit
= build1 (COMPOUND_LITERAL_EXPR
, type
, stmt
);
5673 TREE_SIDE_EFFECTS (complit
) = 1;
5675 layout_decl (decl
, 0);
5677 if (TREE_STATIC (decl
))
5679 /* This decl needs a name for the assembler output. */
5680 set_compound_literal_name (decl
);
5681 DECL_DEFER_OUTPUT (decl
) = 1;
5682 DECL_COMDAT (decl
) = 1;
5684 rest_of_decl_compilation (decl
, 1, 0);
5686 else if (current_function_decl
&& !current_scope
->parm_flag
)
5691 complit
= build2 (C_MAYBE_CONST_EXPR
, type
, NULL
, complit
);
5692 C_MAYBE_CONST_EXPR_NON_CONST (complit
) = 1;
5698 /* Check the type of a compound literal. Here we just check that it
5699 is valid for C++. */
5702 check_compound_literal_type (location_t loc
, struct c_type_name
*type_name
)
5705 && (type_name
->specs
->typespec_kind
== ctsk_tagdef
5706 || type_name
->specs
->typespec_kind
== ctsk_tagfirstref
5707 || type_name
->specs
->typespec_kind
== ctsk_tagfirstref_attrs
))
5708 warning_at (loc
, OPT_Wc___compat
,
5709 "defining a type in a compound literal is invalid in C++");
5712 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5713 replacing with appropriate values if they are invalid. */
5716 check_bitfield_type_and_width (location_t loc
, tree
*type
, tree
*width
,
5720 unsigned int max_width
;
5721 unsigned HOST_WIDE_INT w
;
5722 const char *name
= (orig_name
5723 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name
))
5724 : _("<anonymous>"));
5726 /* Detect and ignore out of range field width and process valid
5728 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width
)))
5730 error_at (loc
, "bit-field %qs width not an integer constant", name
);
5731 *width
= integer_one_node
;
5735 if (TREE_CODE (*width
) != INTEGER_CST
)
5737 *width
= c_fully_fold (*width
, false, NULL
);
5738 if (TREE_CODE (*width
) == INTEGER_CST
)
5739 pedwarn (loc
, OPT_Wpedantic
,
5740 "bit-field %qs width not an integer constant expression",
5743 if (TREE_CODE (*width
) != INTEGER_CST
)
5745 error_at (loc
, "bit-field %qs width not an integer constant", name
);
5746 *width
= integer_one_node
;
5748 constant_expression_warning (*width
);
5749 if (tree_int_cst_sgn (*width
) < 0)
5751 error_at (loc
, "negative width in bit-field %qs", name
);
5752 *width
= integer_one_node
;
5754 else if (integer_zerop (*width
) && orig_name
)
5756 error_at (loc
, "zero width for bit-field %qs", name
);
5757 *width
= integer_one_node
;
5761 /* Detect invalid bit-field type. */
5762 if (TREE_CODE (*type
) != INTEGER_TYPE
5763 && TREE_CODE (*type
) != BOOLEAN_TYPE
5764 && TREE_CODE (*type
) != ENUMERAL_TYPE
)
5766 error_at (loc
, "bit-field %qs has invalid type", name
);
5767 *type
= unsigned_type_node
;
5770 if (TYPE_WARN_IF_NOT_ALIGN (*type
))
5772 error_at (loc
, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
5774 *type
= unsigned_type_node
;
5777 type_mv
= TYPE_MAIN_VARIANT (*type
);
5778 if (!in_system_header_at (input_location
)
5779 && type_mv
!= integer_type_node
5780 && type_mv
!= unsigned_type_node
5781 && type_mv
!= boolean_type_node
)
5782 pedwarn_c90 (loc
, OPT_Wpedantic
,
5783 "type of bit-field %qs is a GCC extension", name
);
5785 max_width
= TYPE_PRECISION (*type
);
5787 if (compare_tree_int (*width
, max_width
) > 0)
5789 error_at (loc
, "width of %qs exceeds its type", name
);
5791 *width
= build_int_cst (integer_type_node
, w
);
5794 w
= tree_to_uhwi (*width
);
5796 if (TREE_CODE (*type
) == ENUMERAL_TYPE
)
5798 struct lang_type
*lt
= TYPE_LANG_SPECIFIC (*type
);
5800 || w
< tree_int_cst_min_precision (lt
->enum_min
, TYPE_SIGN (*type
))
5801 || w
< tree_int_cst_min_precision (lt
->enum_max
, TYPE_SIGN (*type
)))
5802 warning_at (loc
, 0, "%qs is narrower than values of its type", name
);
5808 /* Print warning about variable length array if necessary. */
5811 warn_variable_length_array (tree name
, tree size
)
5813 if (TREE_CONSTANT (size
))
5816 pedwarn_c90 (input_location
, OPT_Wvla
,
5817 "ISO C90 forbids array %qE whose size "
5818 "cannot be evaluated", name
);
5820 pedwarn_c90 (input_location
, OPT_Wvla
, "ISO C90 forbids array "
5821 "whose size cannot be evaluated");
5826 pedwarn_c90 (input_location
, OPT_Wvla
,
5827 "ISO C90 forbids variable length array %qE", name
);
5829 pedwarn_c90 (input_location
, OPT_Wvla
, "ISO C90 forbids variable "
5834 /* Print warning about defaulting to int if necessary. */
5837 warn_defaults_to (location_t location
, int opt
, const char *gmsgid
, ...)
5839 diagnostic_info diagnostic
;
5841 rich_location
richloc (line_table
, location
);
5843 va_start (ap
, gmsgid
);
5844 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
,
5845 flag_isoc99
? DK_PEDWARN
: DK_WARNING
);
5846 diagnostic
.option_index
= opt
;
5847 diagnostic_report_diagnostic (global_dc
, &diagnostic
);
5851 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
5852 considering only those c_declspec_words found in LIST, which
5853 must be terminated by cdw_number_of_elements. */
5856 smallest_type_quals_location (const location_t
*locations
,
5857 const c_declspec_word
*list
)
5859 location_t loc
= UNKNOWN_LOCATION
;
5860 while (*list
!= cdw_number_of_elements
)
5862 location_t newloc
= locations
[*list
];
5863 if (loc
== UNKNOWN_LOCATION
5864 || (newloc
!= UNKNOWN_LOCATION
&& newloc
< loc
))
5872 /* Given declspecs and a declarator,
5873 determine the name and type of the object declared
5874 and construct a ..._DECL node for it.
5875 (In one case we can return a ..._TYPE node instead.
5876 For invalid input we sometimes return NULL_TREE.)
5878 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5880 DECL_CONTEXT says which syntactic context this declaration is in:
5881 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5882 FUNCDEF for a function definition. Like NORMAL but a few different
5883 error messages in each case. Return value may be zero meaning
5884 this definition is too screwy to try to parse.
5885 PARM for a parameter declaration (either within a function prototype
5886 or before a function body). Make a PARM_DECL, or return void_type_node.
5887 TYPENAME if for a typename (in a cast or sizeof).
5888 Don't make a DECL node; just return the ..._TYPE node.
5889 FIELD for a struct or union field; make a FIELD_DECL.
5890 INITIALIZED is true if the decl has an initializer.
5891 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5892 representing the width of the bit-field.
5893 DECL_ATTRS points to the list of attributes that should be added to this
5894 decl. Any nested attributes that belong on the decl itself will be
5896 If EXPR is not NULL, any expressions that need to be evaluated as
5897 part of evaluating variably modified types will be stored in *EXPR.
5898 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5899 set to indicate whether operands in *EXPR can be used in constant
5901 DEPRECATED_STATE is a deprecated_states value indicating whether
5902 deprecation warnings should be suppressed.
5904 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5905 It may also be so in the PARM case, for a prototype where the
5906 argument type is specified but not the name.
5908 This function is where the complicated C meanings of `static'
5909 and `extern' are interpreted. */
5912 grokdeclarator (const struct c_declarator
*declarator
,
5913 struct c_declspecs
*declspecs
,
5914 enum decl_context decl_context
, bool initialized
, tree
*width
,
5915 tree
*decl_attrs
, tree
*expr
, bool *expr_const_operands
,
5916 enum deprecated_states deprecated_state
)
5918 tree type
= declspecs
->type
;
5919 bool threadp
= declspecs
->thread_p
;
5920 enum c_storage_class storage_class
= declspecs
->storage_class
;
5925 int type_quals
= TYPE_UNQUALIFIED
;
5926 tree name
= NULL_TREE
;
5927 bool funcdef_flag
= false;
5928 bool funcdef_syntax
= false;
5929 bool size_varies
= false;
5930 tree decl_attr
= declspecs
->decl_attr
;
5931 int array_ptr_quals
= TYPE_UNQUALIFIED
;
5932 tree array_ptr_attrs
= NULL_TREE
;
5933 bool array_parm_static
= false;
5934 bool array_parm_vla_unspec_p
= false;
5935 tree returned_attrs
= NULL_TREE
;
5936 tree decl_id_attrs
= NULL_TREE
;
5937 bool bitfield
= width
!= NULL
;
5939 tree orig_qual_type
= NULL
;
5940 size_t orig_qual_indirect
= 0;
5941 struct c_arg_info
*arg_info
= 0;
5942 addr_space_t as1
, as2
, address_space
;
5943 location_t loc
= UNKNOWN_LOCATION
;
5945 bool expr_const_operands_dummy
;
5946 enum c_declarator_kind first_non_attr_kind
;
5947 unsigned int alignas_align
= 0;
5949 if (TREE_CODE (type
) == ERROR_MARK
)
5950 return error_mark_node
;
5954 expr_dummy
= NULL_TREE
;
5956 if (expr_const_operands
== NULL
)
5957 expr_const_operands
= &expr_const_operands_dummy
;
5959 if (declspecs
->expr
)
5962 *expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (declspecs
->expr
), *expr
,
5965 *expr
= declspecs
->expr
;
5967 *expr_const_operands
= declspecs
->expr_const_operands
;
5969 if (decl_context
== FUNCDEF
)
5970 funcdef_flag
= true, decl_context
= NORMAL
;
5972 /* Look inside a declarator for the name being declared
5973 and get it as an IDENTIFIER_NODE, for an error message. */
5975 const struct c_declarator
*decl
= declarator
;
5977 first_non_attr_kind
= cdk_attrs
;
5987 funcdef_syntax
= (decl
->kind
== cdk_function
);
5988 if (first_non_attr_kind
== cdk_attrs
)
5989 first_non_attr_kind
= decl
->kind
;
5990 decl
= decl
->declarator
;
5994 decl
= decl
->declarator
;
6000 name
= decl
->u
.id
.id
;
6001 decl_id_attrs
= decl
->u
.id
.attrs
;
6002 if (first_non_attr_kind
== cdk_attrs
)
6003 first_non_attr_kind
= decl
->kind
;
6010 if (name
== NULL_TREE
)
6012 gcc_assert (decl_context
== PARM
6013 || decl_context
== TYPENAME
6014 || (decl_context
== FIELD
6015 && declarator
->kind
== cdk_id
));
6016 gcc_assert (!initialized
);
6020 /* A function definition's declarator must have the form of
6021 a function declarator. */
6023 if (funcdef_flag
&& !funcdef_syntax
)
6026 /* If this looks like a function definition, make it one,
6027 even if it occurs where parms are expected.
6028 Then store_parm_decls will reject it and not use it as a parm. */
6029 if (decl_context
== NORMAL
&& !funcdef_flag
&& current_scope
->parm_flag
)
6030 decl_context
= PARM
;
6032 if (declspecs
->deprecated_p
&& deprecated_state
!= DEPRECATED_SUPPRESS
)
6033 warn_deprecated_use (declspecs
->type
, declspecs
->decl_attr
);
6035 if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
6036 && current_scope
== file_scope
6037 && variably_modified_type_p (type
, NULL_TREE
))
6040 error_at (loc
, "variably modified %qE at file scope", name
);
6042 error_at (loc
, "variably modified field at file scope");
6043 type
= integer_type_node
;
6046 size_varies
= C_TYPE_VARIABLE_SIZE (type
) != 0;
6048 /* Diagnose defaulting to "int". */
6050 if (declspecs
->default_int_p
&& !in_system_header_at (input_location
))
6052 /* Issue a warning if this is an ISO C 99 program or if
6053 -Wreturn-type and this is a function, or if -Wimplicit;
6054 prefer the former warning since it is more explicit. */
6055 if ((warn_implicit_int
|| warn_return_type
> 0 || flag_isoc99
)
6057 warn_about_return_type
= 1;
6061 warn_defaults_to (loc
, OPT_Wimplicit_int
,
6062 "type defaults to %<int%> in declaration "
6065 warn_defaults_to (loc
, OPT_Wimplicit_int
,
6066 "type defaults to %<int%> in type name");
6070 /* Adjust the type if a bit-field is being declared,
6071 -funsigned-bitfields applied and the type is not explicitly
6073 if (bitfield
&& !flag_signed_bitfields
&& !declspecs
->explicit_signed_p
6074 && TREE_CODE (type
) == INTEGER_TYPE
)
6075 type
= unsigned_type_for (type
);
6077 /* Figure out the type qualifiers for the declaration. There are
6078 two ways a declaration can become qualified. One is something
6079 like `const int i' where the `const' is explicit. Another is
6080 something like `typedef const int CI; CI i' where the type of the
6081 declaration contains the `const'. A third possibility is that
6082 there is a type qualifier on the element type of a typedefed
6083 array type, in which case we should extract that qualifier so
6084 that c_apply_type_quals_to_decl receives the full list of
6085 qualifiers to work with (C90 is not entirely clear about whether
6086 duplicate qualifiers should be diagnosed in this case, but it
6087 seems most appropriate to do so). */
6088 element_type
= strip_array_types (type
);
6089 constp
= declspecs
->const_p
+ TYPE_READONLY (element_type
);
6090 restrictp
= declspecs
->restrict_p
+ TYPE_RESTRICT (element_type
);
6091 volatilep
= declspecs
->volatile_p
+ TYPE_VOLATILE (element_type
);
6092 atomicp
= declspecs
->atomic_p
+ TYPE_ATOMIC (element_type
);
6093 as1
= declspecs
->address_space
;
6094 as2
= TYPE_ADDR_SPACE (element_type
);
6095 address_space
= ADDR_SPACE_GENERIC_P (as1
)? as2
: as1
;
6098 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<const%>");
6100 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<restrict%>");
6102 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<volatile%>");
6104 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<_Atomic%>");
6106 if (!ADDR_SPACE_GENERIC_P (as1
) && !ADDR_SPACE_GENERIC_P (as2
) && as1
!= as2
)
6107 error_at (loc
, "conflicting named address spaces (%s vs %s)",
6108 c_addr_space_name (as1
), c_addr_space_name (as2
));
6110 if ((TREE_CODE (type
) == ARRAY_TYPE
6111 || first_non_attr_kind
== cdk_array
)
6112 && TYPE_QUALS (element_type
))
6114 orig_qual_type
= type
;
6115 type
= TYPE_MAIN_VARIANT (type
);
6117 type_quals
= ((constp
? TYPE_QUAL_CONST
: 0)
6118 | (restrictp
? TYPE_QUAL_RESTRICT
: 0)
6119 | (volatilep
? TYPE_QUAL_VOLATILE
: 0)
6120 | (atomicp
? TYPE_QUAL_ATOMIC
: 0)
6121 | ENCODE_QUAL_ADDR_SPACE (address_space
));
6122 if (type_quals
!= TYPE_QUALS (element_type
))
6123 orig_qual_type
= NULL_TREE
;
6125 /* Applying the _Atomic qualifier to an array type (through the use
6126 of typedefs or typeof) must be detected here. If the qualifier
6127 is introduced later, any appearance of applying it to an array is
6128 actually applying it to an element of that array. */
6129 if (declspecs
->atomic_p
&& TREE_CODE (type
) == ARRAY_TYPE
)
6130 error_at (loc
, "%<_Atomic%>-qualified array type");
6132 /* Warn about storage classes that are invalid for certain
6133 kinds of declarations (parameters, typenames, etc.). */
6137 || storage_class
== csc_auto
6138 || storage_class
== csc_register
6139 || storage_class
== csc_typedef
))
6141 if (storage_class
== csc_auto
)
6143 (current_scope
== file_scope
) ? 0 : OPT_Wpedantic
,
6144 "function definition declared %<auto%>");
6145 if (storage_class
== csc_register
)
6146 error_at (loc
, "function definition declared %<register%>");
6147 if (storage_class
== csc_typedef
)
6148 error_at (loc
, "function definition declared %<typedef%>");
6150 error_at (loc
, "function definition declared %qs",
6151 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
6153 if (storage_class
== csc_auto
6154 || storage_class
== csc_register
6155 || storage_class
== csc_typedef
)
6156 storage_class
= csc_none
;
6158 else if (decl_context
!= NORMAL
&& (storage_class
!= csc_none
|| threadp
))
6160 if (decl_context
== PARM
&& storage_class
== csc_register
)
6164 switch (decl_context
)
6168 error_at (loc
, "storage class specified for structure "
6171 error_at (loc
, "storage class specified for structure field");
6175 error_at (loc
, "storage class specified for parameter %qE",
6178 error_at (loc
, "storage class specified for unnamed parameter");
6181 error_at (loc
, "storage class specified for typename");
6184 storage_class
= csc_none
;
6188 else if (storage_class
== csc_extern
6192 /* 'extern' with initialization is invalid if not at file scope. */
6193 if (current_scope
== file_scope
)
6195 /* It is fine to have 'extern const' when compiling at C
6196 and C++ intersection. */
6197 if (!(warn_cxx_compat
&& constp
))
6198 warning_at (loc
, 0, "%qE initialized and declared %<extern%>",
6202 error_at (loc
, "%qE has both %<extern%> and initializer", name
);
6204 else if (current_scope
== file_scope
)
6206 if (storage_class
== csc_auto
)
6207 error_at (loc
, "file-scope declaration of %qE specifies %<auto%>",
6209 if (pedantic
&& storage_class
== csc_register
)
6210 pedwarn (input_location
, OPT_Wpedantic
,
6211 "file-scope declaration of %qE specifies %<register%>", name
);
6215 if (storage_class
== csc_extern
&& funcdef_flag
)
6216 error_at (loc
, "nested function %qE declared %<extern%>", name
);
6217 else if (threadp
&& storage_class
== csc_none
)
6219 error_at (loc
, "function-scope %qE implicitly auto and declared "
6221 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
6226 /* Now figure out the structure of the declarator proper.
6227 Descend through it, creating more complex types, until we reach
6228 the declared identifier (or NULL_TREE, in an absolute declarator).
6229 At each stage we maintain an unqualified version of the type
6230 together with any qualifiers that should be applied to it with
6231 c_build_qualified_type; this way, array types including
6232 multidimensional array types are first built up in unqualified
6233 form and then the qualified form is created with
6234 TYPE_MAIN_VARIANT pointing to the unqualified form. */
6236 while (declarator
&& declarator
->kind
!= cdk_id
)
6238 if (type
== error_mark_node
)
6240 declarator
= declarator
->declarator
;
6244 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
6245 a cdk_pointer (for *...),
6246 a cdk_function (for ...(...)),
6247 a cdk_attrs (for nested attributes),
6248 or a cdk_id (for the name being declared
6249 or the place in an absolute declarator
6250 where the name was omitted).
6251 For the last case, we have just exited the loop.
6253 At this point, TYPE is the type of elements of an array,
6254 or for a function to return, or for a pointer to point to.
6255 After this sequence of ifs, TYPE is the type of the
6256 array or function or pointer, and DECLARATOR has had its
6257 outermost layer removed. */
6259 if (array_ptr_quals
!= TYPE_UNQUALIFIED
6260 || array_ptr_attrs
!= NULL_TREE
6261 || array_parm_static
)
6263 /* Only the innermost declarator (making a parameter be of
6264 array type which is converted to pointer type)
6265 may have static or type qualifiers. */
6266 error_at (loc
, "static or type qualifiers in non-parameter array declarator");
6267 array_ptr_quals
= TYPE_UNQUALIFIED
;
6268 array_ptr_attrs
= NULL_TREE
;
6269 array_parm_static
= false;
6272 switch (declarator
->kind
)
6276 /* A declarator with embedded attributes. */
6277 tree attrs
= declarator
->u
.attrs
;
6278 const struct c_declarator
*inner_decl
;
6280 declarator
= declarator
->declarator
;
6281 /* Standard attribute syntax precisely defines what entity
6282 an attribute in each position appertains to, so only
6283 apply laxity about positioning to GNU attribute syntax.
6284 Standard attributes applied to a function or array
6285 declarator apply exactly to that type; standard
6286 attributes applied to the identifier apply to the
6287 declaration rather than to the type, and are specified
6288 using a cdk_id declarator rather than using
6290 inner_decl
= declarator
;
6291 while (inner_decl
->kind
== cdk_attrs
)
6292 inner_decl
= inner_decl
->declarator
;
6293 if (!cxx11_attribute_p (attrs
))
6295 if (inner_decl
->kind
== cdk_id
)
6296 attr_flags
|= (int) ATTR_FLAG_DECL_NEXT
;
6297 else if (inner_decl
->kind
== cdk_function
)
6298 attr_flags
|= (int) ATTR_FLAG_FUNCTION_NEXT
;
6299 else if (inner_decl
->kind
== cdk_array
)
6300 attr_flags
|= (int) ATTR_FLAG_ARRAY_NEXT
;
6302 attrs
= c_warn_type_attributes (attrs
);
6303 returned_attrs
= decl_attributes (&type
,
6304 chainon (returned_attrs
, attrs
),
6310 tree itype
= NULL_TREE
;
6311 tree size
= declarator
->u
.array
.dimen
;
6312 /* The index is a signed object `sizetype' bits wide. */
6313 tree index_type
= c_common_signed_type (sizetype
);
6315 array_ptr_quals
= declarator
->u
.array
.quals
;
6316 array_ptr_attrs
= declarator
->u
.array
.attrs
;
6317 array_parm_static
= declarator
->u
.array
.static_p
;
6318 array_parm_vla_unspec_p
= declarator
->u
.array
.vla_unspec_p
;
6320 declarator
= declarator
->declarator
;
6322 /* Check for some types that there cannot be arrays of. */
6324 if (VOID_TYPE_P (type
))
6327 error_at (loc
, "declaration of %qE as array of voids", name
);
6329 error_at (loc
, "declaration of type name as array of voids");
6330 type
= error_mark_node
;
6333 if (TREE_CODE (type
) == FUNCTION_TYPE
)
6336 error_at (loc
, "declaration of %qE as array of functions",
6339 error_at (loc
, "declaration of type name as array of "
6341 type
= error_mark_node
;
6344 if (pedantic
&& !in_system_header_at (input_location
)
6345 && flexible_array_type_p (type
))
6346 pedwarn (loc
, OPT_Wpedantic
,
6347 "invalid use of structure with flexible array member");
6349 if (size
== error_mark_node
)
6350 type
= error_mark_node
;
6352 if (type
== error_mark_node
)
6355 if (!verify_type_context (loc
, TCTX_ARRAY_ELEMENT
, type
))
6357 type
= error_mark_node
;
6361 /* If size was specified, set ITYPE to a range-type for
6362 that size. Otherwise, ITYPE remains null. finish_decl
6363 may figure it out from an initial value. */
6367 bool size_maybe_const
= true;
6368 bool size_int_const
= (TREE_CODE (size
) == INTEGER_CST
6369 && !TREE_OVERFLOW (size
));
6370 bool this_size_varies
= false;
6372 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6374 STRIP_TYPE_NOPS (size
);
6376 if (!INTEGRAL_TYPE_P (TREE_TYPE (size
)))
6379 error_at (loc
, "size of array %qE has non-integer type",
6383 "size of unnamed array has non-integer type");
6384 size
= integer_one_node
;
6386 /* This can happen with enum forward declaration. */
6387 else if (!COMPLETE_TYPE_P (TREE_TYPE (size
)))
6390 error_at (loc
, "size of array %qE has incomplete type",
6393 error_at (loc
, "size of unnamed array has incomplete "
6395 size
= integer_one_node
;
6398 size
= c_fully_fold (size
, false, &size_maybe_const
);
6400 if (pedantic
&& size_maybe_const
&& integer_zerop (size
))
6403 pedwarn (loc
, OPT_Wpedantic
,
6404 "ISO C forbids zero-size array %qE", name
);
6406 pedwarn (loc
, OPT_Wpedantic
,
6407 "ISO C forbids zero-size array");
6410 if (TREE_CODE (size
) == INTEGER_CST
&& size_maybe_const
)
6412 constant_expression_warning (size
);
6413 if (tree_int_cst_sgn (size
) < 0)
6416 error_at (loc
, "size of array %qE is negative", name
);
6418 error_at (loc
, "size of unnamed array is negative");
6419 size
= integer_one_node
;
6421 /* Handle a size folded to an integer constant but
6422 not an integer constant expression. */
6423 if (!size_int_const
)
6425 /* If this is a file scope declaration of an
6426 ordinary identifier, this is invalid code;
6427 diagnosing it here and not subsequently
6428 treating the type as variable-length avoids
6429 more confusing diagnostics later. */
6430 if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
6431 && current_scope
== file_scope
)
6432 pedwarn (input_location
, 0,
6433 "variably modified %qE at file scope",
6436 this_size_varies
= size_varies
= true;
6437 warn_variable_length_array (name
, size
);
6440 else if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
6441 && current_scope
== file_scope
)
6443 error_at (loc
, "variably modified %qE at file scope", name
);
6444 size
= integer_one_node
;
6448 /* Make sure the array size remains visibly
6449 nonconstant even if it is (eg) a const variable
6450 with known value. */
6451 this_size_varies
= size_varies
= true;
6452 warn_variable_length_array (name
, size
);
6453 if (sanitize_flags_p (SANITIZE_VLA
)
6454 && current_function_decl
!= NULL_TREE
6455 && decl_context
== NORMAL
)
6457 /* Evaluate the array size only once. */
6458 size
= save_expr (size
);
6459 size
= c_fully_fold (size
, false, NULL
);
6460 size
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
6461 ubsan_instrument_vla (loc
, size
),
6466 if (integer_zerop (size
) && !this_size_varies
)
6468 /* A zero-length array cannot be represented with
6469 an unsigned index type, which is what we'll
6470 get with build_index_type. Create an
6471 open-ended range instead. */
6472 itype
= build_range_type (sizetype
, size
, NULL_TREE
);
6476 /* Arrange for the SAVE_EXPR on the inside of the
6477 MINUS_EXPR, which allows the -1 to get folded
6478 with the +1 that happens when building TYPE_SIZE. */
6480 size
= save_expr (size
);
6481 if (this_size_varies
&& TREE_CODE (size
) == INTEGER_CST
)
6482 size
= build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
6483 integer_zero_node
, size
);
6485 /* Compute the maximum valid index, that is, size
6486 - 1. Do the calculation in index_type, so that
6487 if it is a variable the computations will be
6488 done in the proper mode. */
6489 itype
= fold_build2_loc (loc
, MINUS_EXPR
, index_type
,
6490 convert (index_type
, size
),
6491 convert (index_type
,
6494 /* The above overflows when size does not fit
6496 ??? While a size of INT_MAX+1 technically shouldn't
6497 cause an overflow (because we subtract 1), handling
6498 this case seems like an unnecessary complication. */
6499 if (TREE_CODE (size
) == INTEGER_CST
6500 && !int_fits_type_p (size
, index_type
))
6503 error_at (loc
, "size of array %qE is too large",
6506 error_at (loc
, "size of unnamed array is too large");
6507 type
= error_mark_node
;
6511 itype
= build_index_type (itype
);
6513 if (this_size_varies
)
6516 *expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
6520 *expr_const_operands
&= size_maybe_const
;
6523 else if (decl_context
== FIELD
)
6525 bool flexible_array_member
= false;
6526 if (array_parm_vla_unspec_p
)
6527 /* Field names can in fact have function prototype
6528 scope so [*] is disallowed here through making
6529 the field variably modified, not through being
6530 something other than a declaration with function
6535 const struct c_declarator
*t
= declarator
;
6536 while (t
->kind
== cdk_attrs
)
6538 flexible_array_member
= (t
->kind
== cdk_id
);
6540 if (flexible_array_member
6541 && !in_system_header_at (input_location
))
6542 pedwarn_c90 (loc
, OPT_Wpedantic
, "ISO C90 does not "
6543 "support flexible array members");
6545 /* ISO C99 Flexible array members are effectively
6546 identical to GCC's zero-length array extension. */
6547 if (flexible_array_member
|| array_parm_vla_unspec_p
)
6548 itype
= build_range_type (sizetype
, size_zero_node
,
6551 else if (decl_context
== PARM
)
6553 if (array_parm_vla_unspec_p
)
6555 itype
= build_range_type (sizetype
, size_zero_node
, NULL_TREE
);
6559 else if (decl_context
== TYPENAME
)
6561 if (array_parm_vla_unspec_p
)
6564 warning (0, "%<[*]%> not in a declaration");
6565 /* We use this to avoid messing up with incomplete
6566 array types of the same type, that would
6567 otherwise be modified below. */
6568 itype
= build_range_type (sizetype
, size_zero_node
,
6574 /* Complain about arrays of incomplete types. */
6575 if (!COMPLETE_TYPE_P (type
))
6577 error_at (loc
, "array type has incomplete element type %qT",
6579 /* See if we can be more helpful. */
6580 if (TREE_CODE (type
) == ARRAY_TYPE
)
6583 inform (loc
, "declaration of %qE as multidimensional "
6584 "array must have bounds for all dimensions "
6585 "except the first", name
);
6587 inform (loc
, "declaration of multidimensional array "
6588 "must have bounds for all dimensions except "
6591 type
= error_mark_node
;
6594 /* When itype is NULL, a shared incomplete array type is
6595 returned for all array of a given type. Elsewhere we
6596 make sure we don't complete that type before copying
6597 it, but here we want to make sure we don't ever
6598 modify the shared type, so we gcc_assert (itype)
6601 addr_space_t as
= DECODE_QUAL_ADDR_SPACE (type_quals
);
6602 if (!ADDR_SPACE_GENERIC_P (as
) && as
!= TYPE_ADDR_SPACE (type
))
6603 type
= build_qualified_type (type
,
6604 ENCODE_QUAL_ADDR_SPACE (as
));
6606 type
= build_array_type (type
, itype
);
6609 if (type
!= error_mark_node
)
6613 /* It is ok to modify type here even if itype is
6614 NULL: if size_varies, we're in a
6615 multi-dimensional array and the inner type has
6616 variable size, so the enclosing shared array type
6618 if (size
&& TREE_CODE (size
) == INTEGER_CST
)
6620 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6621 C_TYPE_VARIABLE_SIZE (type
) = 1;
6624 /* The GCC extension for zero-length arrays differs from
6625 ISO flexible array members in that sizeof yields
6627 if (size
&& integer_zerop (size
))
6630 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6631 TYPE_SIZE (type
) = bitsize_zero_node
;
6632 TYPE_SIZE_UNIT (type
) = size_zero_node
;
6633 SET_TYPE_STRUCTURAL_EQUALITY (type
);
6635 if (array_parm_vla_unspec_p
)
6638 /* The type is complete. C99 6.7.5.2p4 */
6639 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6640 TYPE_SIZE (type
) = bitsize_zero_node
;
6641 TYPE_SIZE_UNIT (type
) = size_zero_node
;
6642 SET_TYPE_STRUCTURAL_EQUALITY (type
);
6645 if (!valid_array_size_p (loc
, type
, name
))
6646 type
= error_mark_node
;
6649 if (decl_context
!= PARM
6650 && (array_ptr_quals
!= TYPE_UNQUALIFIED
6651 || array_ptr_attrs
!= NULL_TREE
6652 || array_parm_static
))
6654 error_at (loc
, "static or type qualifiers in non-parameter "
6655 "array declarator");
6656 array_ptr_quals
= TYPE_UNQUALIFIED
;
6657 array_ptr_attrs
= NULL_TREE
;
6658 array_parm_static
= false;
6660 orig_qual_indirect
++;
6665 /* Say it's a definition only for the declarator closest
6666 to the identifier, apart possibly from some
6668 bool really_funcdef
= false;
6670 orig_qual_type
= NULL_TREE
;
6673 const struct c_declarator
*t
= declarator
->declarator
;
6674 while (t
->kind
== cdk_attrs
)
6676 really_funcdef
= (t
->kind
== cdk_id
);
6679 /* Declaring a function type. Make sure we have a valid
6680 type for the function to return. */
6681 if (type
== error_mark_node
)
6684 size_varies
= false;
6686 /* Warn about some types functions can't return. */
6687 if (TREE_CODE (type
) == FUNCTION_TYPE
)
6690 error_at (loc
, "%qE declared as function returning a "
6693 error_at (loc
, "type name declared as function "
6694 "returning a function");
6695 type
= integer_type_node
;
6697 if (TREE_CODE (type
) == ARRAY_TYPE
)
6700 error_at (loc
, "%qE declared as function returning an array",
6703 error_at (loc
, "type name declared as function returning "
6705 type
= integer_type_node
;
6708 /* Construct the function type and go to the next
6709 inner layer of declarator. */
6710 arg_info
= declarator
->u
.arg_info
;
6711 arg_types
= grokparms (arg_info
, really_funcdef
);
6713 /* Type qualifiers before the return type of the function
6714 qualify the return type, not the function type. */
6717 const enum c_declspec_word ignored_quals_list
[] =
6719 cdw_const
, cdw_volatile
, cdw_restrict
, cdw_address_space
,
6720 cdw_atomic
, cdw_number_of_elements
6722 location_t specs_loc
6723 = smallest_type_quals_location (declspecs
->locations
,
6724 ignored_quals_list
);
6725 if (specs_loc
== UNKNOWN_LOCATION
)
6726 specs_loc
= declspecs
->locations
[cdw_typedef
];
6727 if (specs_loc
== UNKNOWN_LOCATION
)
6730 /* Type qualifiers on a function return type are
6731 normally permitted by the standard but have no
6732 effect, so give a warning at -Wreturn-type.
6733 Qualifiers on a void return type are banned on
6734 function definitions in ISO C; GCC used to used
6735 them for noreturn functions. The resolution of C11
6736 DR#423 means qualifiers (other than _Atomic) are
6737 actually removed from the return type when
6738 determining the function type. */
6739 int quals_used
= type_quals
;
6741 quals_used
&= TYPE_QUAL_ATOMIC
;
6742 if (quals_used
&& VOID_TYPE_P (type
) && really_funcdef
)
6743 pedwarn (specs_loc
, 0,
6744 "function definition has qualified void "
6747 warning_at (specs_loc
, OPT_Wignored_qualifiers
,
6748 "type qualifiers ignored on function "
6751 /* Ensure an error for restrict on invalid types; the
6752 DR#423 resolution is not entirely clear about
6755 && (type_quals
& TYPE_QUAL_RESTRICT
)
6756 && (!POINTER_TYPE_P (type
)
6757 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
6758 error_at (loc
, "invalid use of %<restrict%>");
6759 type
= c_build_qualified_type (type
, quals_used
);
6761 type_quals
= TYPE_UNQUALIFIED
;
6763 type
= build_function_type (type
, arg_types
);
6764 declarator
= declarator
->declarator
;
6766 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6767 the formal parameter list of this FUNCTION_TYPE to point to
6768 the FUNCTION_TYPE node itself. */
6773 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info
->tags
, ix
, tag
)
6774 TYPE_CONTEXT (tag
->type
) = type
;
6780 /* Merge any constancy or volatility into the target type
6782 if ((type_quals
& TYPE_QUAL_ATOMIC
)
6783 && TREE_CODE (type
) == FUNCTION_TYPE
)
6786 "%<_Atomic%>-qualified function type");
6787 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6789 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
6791 pedwarn (loc
, OPT_Wpedantic
,
6792 "ISO C forbids qualified function types");
6794 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6795 orig_qual_indirect
);
6796 orig_qual_type
= NULL_TREE
;
6797 size_varies
= false;
6799 /* When the pointed-to type involves components of variable size,
6800 care must be taken to ensure that the size evaluation code is
6801 emitted early enough to dominate all the possible later uses
6802 and late enough for the variables on which it depends to have
6805 This is expected to happen automatically when the pointed-to
6806 type has a name/declaration of it's own, but special attention
6807 is required if the type is anonymous.
6809 We attach an artificial TYPE_DECL to such pointed-to type
6810 and arrange for it to be included in a DECL_EXPR. This
6811 forces the sizes evaluation at a safe point and ensures it
6812 is not deferred until e.g. within a deeper conditional context.
6814 PARM contexts have no enclosing statement list that
6815 can hold the DECL_EXPR, so we need to use a BIND_EXPR
6816 instead, and add it to the list of expressions that
6817 need to be evaluated.
6819 TYPENAME contexts do have an enclosing statement list,
6820 but it would be incorrect to use it, as the size should
6821 only be evaluated if the containing expression is
6822 evaluated. We might also be in the middle of an
6823 expression with side effects on the pointed-to type size
6824 "arguments" prior to the pointer declaration point and
6825 the fake TYPE_DECL in the enclosing context would force
6826 the size evaluation prior to the side effects. We therefore
6827 use BIND_EXPRs in TYPENAME contexts too. */
6828 if (!TYPE_NAME (type
)
6829 && variably_modified_type_p (type
, NULL_TREE
))
6831 tree bind
= NULL_TREE
;
6832 if (decl_context
== TYPENAME
|| decl_context
== PARM
)
6834 bind
= build3 (BIND_EXPR
, void_type_node
, NULL_TREE
,
6835 NULL_TREE
, NULL_TREE
);
6836 TREE_SIDE_EFFECTS (bind
) = 1;
6837 BIND_EXPR_BODY (bind
) = push_stmt_list ();
6840 tree decl
= build_decl (loc
, TYPE_DECL
, NULL_TREE
, type
);
6841 DECL_ARTIFICIAL (decl
) = 1;
6843 finish_decl (decl
, loc
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
6844 TYPE_NAME (type
) = decl
;
6848 BIND_EXPR_BODY (bind
)
6849 = pop_stmt_list (BIND_EXPR_BODY (bind
));
6851 *expr
= build2 (COMPOUND_EXPR
, void_type_node
, *expr
,
6858 type
= c_build_pointer_type (type
);
6860 /* Process type qualifiers (such as const or volatile)
6861 that were given inside the `*'. */
6862 type_quals
= declarator
->u
.pointer_quals
;
6864 declarator
= declarator
->declarator
;
6871 *decl_attrs
= chainon (returned_attrs
, *decl_attrs
);
6872 *decl_attrs
= chainon (decl_id_attrs
, *decl_attrs
);
6874 /* Now TYPE has the actual type, apart from any qualifiers in
6877 /* Warn about address space used for things other than static memory or
6879 address_space
= DECODE_QUAL_ADDR_SPACE (type_quals
);
6880 if (!ADDR_SPACE_GENERIC_P (address_space
))
6882 if (decl_context
== NORMAL
)
6884 switch (storage_class
)
6887 error ("%qs combined with %<auto%> qualifier for %qE",
6888 c_addr_space_name (address_space
), name
);
6891 error ("%qs combined with %<register%> qualifier for %qE",
6892 c_addr_space_name (address_space
), name
);
6895 if (current_function_scope
)
6897 error ("%qs specified for auto variable %qE",
6898 c_addr_space_name (address_space
), name
);
6910 else if (decl_context
== PARM
&& TREE_CODE (type
) != ARRAY_TYPE
)
6913 error ("%qs specified for parameter %qE",
6914 c_addr_space_name (address_space
), name
);
6916 error ("%qs specified for unnamed parameter",
6917 c_addr_space_name (address_space
));
6919 else if (decl_context
== FIELD
)
6922 error ("%qs specified for structure field %qE",
6923 c_addr_space_name (address_space
), name
);
6925 error ("%qs specified for structure field",
6926 c_addr_space_name (address_space
));
6930 /* Check the type and width of a bit-field. */
6933 check_bitfield_type_and_width (loc
, &type
, width
, name
);
6934 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6935 atomic types are permitted for bit-fields; we have no code to
6936 make bit-field accesses atomic, so disallow them. */
6937 if (type_quals
& TYPE_QUAL_ATOMIC
)
6940 error_at (loc
, "bit-field %qE has atomic type", name
);
6942 error_at (loc
, "bit-field has atomic type");
6943 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6947 /* Reject invalid uses of _Alignas. */
6948 if (declspecs
->alignas_p
)
6950 if (storage_class
== csc_typedef
)
6951 error_at (loc
, "alignment specified for typedef %qE", name
);
6952 else if (storage_class
== csc_register
)
6953 error_at (loc
, "alignment specified for %<register%> object %qE",
6955 else if (decl_context
== PARM
)
6958 error_at (loc
, "alignment specified for parameter %qE", name
);
6960 error_at (loc
, "alignment specified for unnamed parameter");
6965 error_at (loc
, "alignment specified for bit-field %qE", name
);
6967 error_at (loc
, "alignment specified for unnamed bit-field");
6969 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
6970 error_at (loc
, "alignment specified for function %qE", name
);
6971 else if (declspecs
->align_log
!= -1 && TYPE_P (type
))
6973 alignas_align
= 1U << declspecs
->align_log
;
6974 if (alignas_align
< min_align_of_type (type
))
6977 error_at (loc
, "%<_Alignas%> specifiers cannot reduce "
6978 "alignment of %qE", name
);
6980 error_at (loc
, "%<_Alignas%> specifiers cannot reduce "
6981 "alignment of unnamed field");
6987 /* If this is declaring a typedef name, return a TYPE_DECL. */
6989 if (storage_class
== csc_typedef
)
6992 if ((type_quals
& TYPE_QUAL_ATOMIC
)
6993 && TREE_CODE (type
) == FUNCTION_TYPE
)
6996 "%<_Atomic%>-qualified function type");
6997 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6999 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
7001 pedwarn (loc
, OPT_Wpedantic
,
7002 "ISO C forbids qualified function types");
7004 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
7005 orig_qual_indirect
);
7006 decl
= build_decl (declarator
->id_loc
,
7007 TYPE_DECL
, declarator
->u
.id
.id
, type
);
7008 if (declspecs
->explicit_signed_p
)
7009 C_TYPEDEF_EXPLICITLY_SIGNED (decl
) = 1;
7010 if (declspecs
->inline_p
)
7011 pedwarn (loc
, 0,"typedef %q+D declared %<inline%>", decl
);
7012 if (declspecs
->noreturn_p
)
7013 pedwarn (loc
, 0,"typedef %q+D declared %<_Noreturn%>", decl
);
7015 if (warn_cxx_compat
&& declarator
->u
.id
.id
!= NULL_TREE
)
7017 struct c_binding
*b
= I_TAG_BINDING (declarator
->u
.id
.id
);
7020 && b
->decl
!= NULL_TREE
7021 && (B_IN_CURRENT_SCOPE (b
)
7022 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
7023 && TYPE_MAIN_VARIANT (b
->decl
) != TYPE_MAIN_VARIANT (type
))
7025 auto_diagnostic_group d
;
7026 if (warning_at (declarator
->id_loc
, OPT_Wc___compat
,
7027 ("using %qD as both a typedef and a tag is "
7028 "invalid in C++"), decl
)
7029 && b
->locus
!= UNKNOWN_LOCATION
)
7030 inform (b
->locus
, "originally defined here");
7037 /* If this is a type name (such as, in a cast or sizeof),
7038 compute the type and return it now. */
7040 if (decl_context
== TYPENAME
)
7042 /* Note that the grammar rejects storage classes in typenames
7044 gcc_assert (storage_class
== csc_none
&& !threadp
7045 && !declspecs
->inline_p
&& !declspecs
->noreturn_p
);
7046 if ((type_quals
& TYPE_QUAL_ATOMIC
)
7047 && TREE_CODE (type
) == FUNCTION_TYPE
)
7050 "%<_Atomic%>-qualified function type");
7051 type_quals
&= ~TYPE_QUAL_ATOMIC
;
7053 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
7055 pedwarn (loc
, OPT_Wpedantic
,
7056 "ISO C forbids const or volatile function types");
7058 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
7059 orig_qual_indirect
);
7063 if (pedantic
&& decl_context
== FIELD
7064 && variably_modified_type_p (type
, NULL_TREE
))
7067 pedwarn (loc
, OPT_Wpedantic
, "a member of a structure or union cannot "
7068 "have a variably modified type");
7071 /* Aside from typedefs and type names (handle above),
7072 `void' at top level (not within pointer)
7073 is allowed only in public variables.
7074 We don't complain about parms either, but that is because
7075 a better error message can be made later. */
7077 if (VOID_TYPE_P (type
) && decl_context
!= PARM
7078 && !((decl_context
!= FIELD
&& TREE_CODE (type
) != FUNCTION_TYPE
)
7079 && (storage_class
== csc_extern
7080 || (current_scope
== file_scope
7081 && !(storage_class
== csc_static
7082 || storage_class
== csc_register
)))))
7084 error_at (loc
, "variable or field %qE declared void", name
);
7085 type
= integer_type_node
;
7088 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7089 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
7094 if (decl_context
== PARM
)
7097 bool array_parameter_p
= false;
7099 /* A parameter declared as an array of T is really a pointer to T.
7100 One declared as a function is really a pointer to a function. */
7102 if (TREE_CODE (type
) == ARRAY_TYPE
)
7104 /* Transfer const-ness of array into that of type pointed to. */
7105 type
= TREE_TYPE (type
);
7106 if (orig_qual_type
!= NULL_TREE
)
7108 if (orig_qual_indirect
== 0)
7109 orig_qual_type
= TREE_TYPE (orig_qual_type
);
7111 orig_qual_indirect
--;
7114 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
7115 orig_qual_indirect
);
7116 type
= c_build_pointer_type (type
);
7117 type_quals
= array_ptr_quals
;
7119 type
= c_build_qualified_type (type
, type_quals
);
7121 /* We don't yet implement attributes in this context. */
7122 if (array_ptr_attrs
!= NULL_TREE
)
7123 warning_at (loc
, OPT_Wattributes
,
7124 "attributes in parameter array declarator ignored");
7126 size_varies
= false;
7127 array_parameter_p
= true;
7129 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
7131 if (type_quals
& TYPE_QUAL_ATOMIC
)
7134 "%<_Atomic%>-qualified function type");
7135 type_quals
&= ~TYPE_QUAL_ATOMIC
;
7137 else if (type_quals
)
7138 pedwarn (loc
, OPT_Wpedantic
,
7139 "ISO C forbids qualified function types");
7141 type
= c_build_qualified_type (type
, type_quals
);
7142 type
= c_build_pointer_type (type
);
7143 type_quals
= TYPE_UNQUALIFIED
;
7145 else if (type_quals
)
7146 type
= c_build_qualified_type (type
, type_quals
);
7148 decl
= build_decl (declarator
->id_loc
,
7149 PARM_DECL
, declarator
->u
.id
.id
, type
);
7151 C_DECL_VARIABLE_SIZE (decl
) = 1;
7152 C_ARRAY_PARAMETER (decl
) = array_parameter_p
;
7154 /* Compute the type actually passed in the parmlist,
7155 for the case where there is no prototype.
7156 (For example, shorts and chars are passed as ints.)
7157 When there is a prototype, this is overridden later. */
7159 if (type
== error_mark_node
)
7160 promoted_type
= type
;
7162 promoted_type
= c_type_promotes_to (type
);
7164 DECL_ARG_TYPE (decl
) = promoted_type
;
7165 if (declspecs
->inline_p
)
7166 pedwarn (loc
, 0, "parameter %q+D declared %<inline%>", decl
);
7167 if (declspecs
->noreturn_p
)
7168 pedwarn (loc
, 0, "parameter %q+D declared %<_Noreturn%>", decl
);
7170 else if (decl_context
== FIELD
)
7172 /* Note that the grammar rejects storage classes in typenames
7174 gcc_assert (storage_class
== csc_none
&& !threadp
7175 && !declspecs
->inline_p
&& !declspecs
->noreturn_p
);
7177 /* Structure field. It may not be a function. */
7179 if (TREE_CODE (type
) == FUNCTION_TYPE
)
7181 error_at (loc
, "field %qE declared as a function", name
);
7182 type
= build_pointer_type (type
);
7184 else if (TREE_CODE (type
) != ERROR_MARK
7185 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type
))
7188 error_at (loc
, "field %qE has incomplete type", name
);
7190 error_at (loc
, "unnamed field has incomplete type");
7191 type
= error_mark_node
;
7193 else if (TREE_CODE (type
) == ARRAY_TYPE
7194 && TYPE_DOMAIN (type
) == NULL_TREE
)
7196 /* We have a flexible array member through a typedef.
7197 Set suitable range. Whether this is a correct position
7198 for a flexible array member will be determined elsewhere. */
7199 if (!in_system_header_at (input_location
))
7200 pedwarn_c90 (loc
, OPT_Wpedantic
, "ISO C90 does not "
7201 "support flexible array members");
7202 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
7203 TYPE_DOMAIN (type
) = build_range_type (sizetype
, size_zero_node
,
7205 if (orig_qual_indirect
== 0)
7206 orig_qual_type
= NULL_TREE
;
7208 if (type
!= error_mark_node
7209 && !verify_type_context (loc
, TCTX_FIELD
, type
))
7210 type
= error_mark_node
;
7212 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
7213 orig_qual_indirect
);
7214 decl
= build_decl (declarator
->id_loc
,
7215 FIELD_DECL
, declarator
->u
.id
.id
, type
);
7216 DECL_NONADDRESSABLE_P (decl
) = bitfield
;
7217 if (bitfield
&& !declarator
->u
.id
.id
)
7219 TREE_NO_WARNING (decl
) = 1;
7220 DECL_PADDING_P (decl
) = 1;
7224 C_DECL_VARIABLE_SIZE (decl
) = 1;
7226 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
7228 if (storage_class
== csc_register
|| threadp
)
7230 error_at (loc
, "invalid storage class for function %qE", name
);
7232 else if (current_scope
!= file_scope
)
7234 /* Function declaration not at file scope. Storage
7235 classes other than `extern' are not allowed, C99
7236 6.7.1p5, and `extern' makes no difference. However,
7237 GCC allows 'auto', perhaps with 'inline', to support
7238 nested functions. */
7239 if (storage_class
== csc_auto
)
7240 pedwarn (loc
, OPT_Wpedantic
,
7241 "invalid storage class for function %qE", name
);
7242 else if (storage_class
== csc_static
)
7244 error_at (loc
, "invalid storage class for function %qE", name
);
7246 storage_class
= declspecs
->storage_class
= csc_none
;
7252 decl
= build_decl (declarator
->id_loc
,
7253 FUNCTION_DECL
, declarator
->u
.id
.id
, type
);
7254 decl
= build_decl_attribute_variant (decl
, decl_attr
);
7256 if (type_quals
& TYPE_QUAL_ATOMIC
)
7259 "%<_Atomic%>-qualified function type");
7260 type_quals
&= ~TYPE_QUAL_ATOMIC
;
7262 else if (pedantic
&& type_quals
&& !DECL_IN_SYSTEM_HEADER (decl
))
7263 pedwarn (loc
, OPT_Wpedantic
,
7264 "ISO C forbids qualified function types");
7266 /* Every function declaration is an external reference
7267 (DECL_EXTERNAL) except for those which are not at file
7268 scope and are explicitly declared "auto". This is
7269 forbidden by standard C (C99 6.7.1p5) and is interpreted by
7270 GCC to signify a forward declaration of a nested function. */
7271 if (storage_class
== csc_auto
&& current_scope
!= file_scope
)
7272 DECL_EXTERNAL (decl
) = 0;
7273 /* In C99, a function which is declared 'inline' with 'extern'
7274 is not an external reference (which is confusing). It
7275 means that the later definition of the function must be output
7276 in this file, C99 6.7.4p6. In GNU C89, a function declared
7277 'extern inline' is an external reference. */
7278 else if (declspecs
->inline_p
&& storage_class
!= csc_static
)
7279 DECL_EXTERNAL (decl
) = ((storage_class
== csc_extern
)
7280 == flag_gnu89_inline
);
7282 DECL_EXTERNAL (decl
) = !initialized
;
7284 /* Record absence of global scope for `static' or `auto'. */
7286 = !(storage_class
== csc_static
|| storage_class
== csc_auto
);
7288 /* For a function definition, record the argument information
7289 block where store_parm_decls will look for it. */
7291 current_function_arg_info
= arg_info
;
7293 if (declspecs
->default_int_p
)
7294 C_FUNCTION_IMPLICIT_INT (decl
) = 1;
7296 /* Record presence of `inline' and `_Noreturn', if it is
7298 if (flag_hosted
&& MAIN_NAME_P (declarator
->u
.id
.id
))
7300 if (declspecs
->inline_p
)
7301 pedwarn (loc
, 0, "cannot inline function %<main%>");
7302 if (declspecs
->noreturn_p
)
7303 pedwarn (loc
, 0, "%<main%> declared %<_Noreturn%>");
7307 if (declspecs
->inline_p
)
7308 /* Record that the function is declared `inline'. */
7309 DECL_DECLARED_INLINE_P (decl
) = 1;
7310 if (declspecs
->noreturn_p
)
7313 pedwarn_c99 (loc
, OPT_Wpedantic
,
7314 "ISO C99 does not support %<_Noreturn%>");
7316 pedwarn_c99 (loc
, OPT_Wpedantic
,
7317 "ISO C90 does not support %<_Noreturn%>");
7318 TREE_THIS_VOLATILE (decl
) = 1;
7324 /* It's a variable. */
7325 /* An uninitialized decl with `extern' is a reference. */
7326 int extern_ref
= !initialized
&& storage_class
== csc_extern
;
7328 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
7329 orig_qual_indirect
);
7331 /* C99 6.2.2p7: It is invalid (compile-time undefined
7332 behavior) to create an 'extern' declaration for a
7333 variable if there is a global declaration that is
7334 'static' and the global declaration is not visible.
7335 (If the static declaration _is_ currently visible,
7336 the 'extern' declaration is taken to refer to that decl.) */
7337 if (extern_ref
&& current_scope
!= file_scope
)
7339 tree global_decl
= identifier_global_value (declarator
->u
.id
.id
);
7340 tree visible_decl
= lookup_name (declarator
->u
.id
.id
);
7343 && global_decl
!= visible_decl
7344 && VAR_P (global_decl
)
7345 && !TREE_PUBLIC (global_decl
))
7346 error_at (loc
, "variable previously declared %<static%> "
7347 "redeclared %<extern%>");
7350 decl
= build_decl (declarator
->id_loc
,
7351 VAR_DECL
, declarator
->u
.id
.id
, type
);
7353 C_DECL_VARIABLE_SIZE (decl
) = 1;
7355 if (declspecs
->inline_p
)
7356 pedwarn (loc
, 0, "variable %q+D declared %<inline%>", decl
);
7357 if (declspecs
->noreturn_p
)
7358 pedwarn (loc
, 0, "variable %q+D declared %<_Noreturn%>", decl
);
7360 /* At file scope, an initialized extern declaration may follow
7361 a static declaration. In that case, DECL_EXTERNAL will be
7362 reset later in start_decl. */
7363 DECL_EXTERNAL (decl
) = (storage_class
== csc_extern
);
7365 /* At file scope, the presence of a `static' or `register' storage
7366 class specifier, or the absence of all storage class specifiers
7367 makes this declaration a definition (perhaps tentative). Also,
7368 the absence of `static' makes it public. */
7369 if (current_scope
== file_scope
)
7371 TREE_PUBLIC (decl
) = storage_class
!= csc_static
;
7372 TREE_STATIC (decl
) = !extern_ref
;
7374 /* Not at file scope, only `static' makes a static definition. */
7377 TREE_STATIC (decl
) = (storage_class
== csc_static
);
7378 TREE_PUBLIC (decl
) = extern_ref
;
7382 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
7385 if ((storage_class
== csc_extern
7386 || (storage_class
== csc_none
7387 && TREE_CODE (type
) == FUNCTION_TYPE
7389 && variably_modified_type_p (type
, NULL_TREE
))
7392 if (TREE_CODE (type
) == FUNCTION_TYPE
)
7393 error_at (loc
, "non-nested function with variably modified type");
7395 error_at (loc
, "object with variably modified type must have "
7399 /* For nested functions disqualify ones taking VLAs by value
7400 from inlining since the middle-end cannot deal with this.
7401 ??? We should arrange for those to be passed by reference
7402 with emitting the copy on the caller side in the frontend. */
7403 if (storage_class
== csc_none
7404 && TREE_CODE (type
) == FUNCTION_TYPE
)
7405 for (tree al
= TYPE_ARG_TYPES (type
); al
; al
= TREE_CHAIN (al
))
7407 tree arg
= TREE_VALUE (al
);
7408 if (arg
!= error_mark_node
7409 && C_TYPE_VARIABLE_SIZE (arg
))
7411 DECL_UNINLINABLE (decl
) = 1;
7416 /* Record `register' declaration for warnings on &
7417 and in case doing stupid register allocation. */
7419 if (storage_class
== csc_register
)
7421 C_DECL_REGISTER (decl
) = 1;
7422 DECL_REGISTER (decl
) = 1;
7425 /* Record constancy and volatility. */
7426 c_apply_type_quals_to_decl (type_quals
, decl
);
7428 /* Apply _Alignas specifiers. */
7431 SET_DECL_ALIGN (decl
, alignas_align
* BITS_PER_UNIT
);
7432 DECL_USER_ALIGN (decl
) = 1;
7435 /* If a type has volatile components, it should be stored in memory.
7436 Otherwise, the fact that those components are volatile
7437 will be ignored, and would even crash the compiler.
7438 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7439 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl
))
7440 && (VAR_P (decl
) || TREE_CODE (decl
) == PARM_DECL
7441 || TREE_CODE (decl
) == RESULT_DECL
))
7443 /* It is not an error for a structure with volatile fields to
7444 be declared register, but reset DECL_REGISTER since it
7445 cannot actually go in a register. */
7446 int was_reg
= C_DECL_REGISTER (decl
);
7447 C_DECL_REGISTER (decl
) = 0;
7448 DECL_REGISTER (decl
) = 0;
7449 c_mark_addressable (decl
);
7450 C_DECL_REGISTER (decl
) = was_reg
;
7453 /* This is the earliest point at which we might know the assembler
7454 name of a variable. Thus, if it's known before this, die horribly. */
7455 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl
)
7456 || !DECL_ASSEMBLER_NAME_SET_P (decl
));
7460 && TREE_PUBLIC (decl
)
7461 && TREE_STATIC (decl
)
7462 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
))
7463 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
7464 && TYPE_NAME (TREE_TYPE (decl
)) == NULL_TREE
)
7465 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
7466 ("non-local variable %qD with anonymous type is "
7467 "questionable in C++"),
7474 /* Decode the parameter-list info for a function type or function definition.
7475 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7476 if there is an identifier list instead of a parameter decl list).
7477 These two functions are separate because when a function returns
7478 or receives functions then each is called multiple times but the order
7479 of calls is different. The last call to `grokparms' is always the one
7480 that contains the formal parameter names of a function definition.
7482 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7484 FUNCDEF_FLAG is true for a function definition, false for
7485 a mere declaration. A nonempty identifier-list gets an error message
7486 when FUNCDEF_FLAG is false. */
7489 grokparms (struct c_arg_info
*arg_info
, bool funcdef_flag
)
7491 tree arg_types
= arg_info
->types
;
7493 if (funcdef_flag
&& arg_info
->had_vla_unspec
)
7495 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7497 error ("%<[*]%> not allowed in other than function prototype scope");
7500 if (arg_types
== NULL_TREE
&& !funcdef_flag
7501 && !in_system_header_at (input_location
))
7502 warning (OPT_Wstrict_prototypes
,
7503 "function declaration isn%'t a prototype");
7505 if (arg_types
== error_mark_node
)
7506 /* Don't set TYPE_ARG_TYPES in this case. */
7509 else if (arg_types
&& TREE_CODE (TREE_VALUE (arg_types
)) == IDENTIFIER_NODE
)
7513 pedwarn (input_location
, 0, "parameter names (without types) in "
7514 "function declaration");
7515 arg_info
->parms
= NULL_TREE
;
7518 arg_info
->parms
= arg_info
->types
;
7520 arg_info
->types
= NULL_TREE
;
7525 tree parm
, type
, typelt
;
7526 unsigned int parmno
;
7528 /* In C2X, convert () in a function definition to (void). */
7532 && !arg_info
->parms
)
7533 arg_types
= arg_info
->types
= void_list_node
;
7535 /* If there is a parameter of incomplete type in a definition,
7536 this is an error. In a declaration this is valid, and a
7537 struct or union type may be completed later, before any calls
7538 or definition of the function. In the case where the tag was
7539 first declared within the parameter list, a warning has
7540 already been given. If a parameter has void type, then
7541 however the function cannot be defined or called, so
7544 for (parm
= arg_info
->parms
, typelt
= arg_types
, parmno
= 1;
7546 parm
= DECL_CHAIN (parm
), typelt
= TREE_CHAIN (typelt
), parmno
++)
7548 type
= TREE_VALUE (typelt
);
7549 if (type
== error_mark_node
)
7552 if (!COMPLETE_TYPE_P (type
))
7556 if (DECL_NAME (parm
))
7557 error_at (input_location
,
7558 "parameter %u (%q+D) has incomplete type",
7561 error_at (DECL_SOURCE_LOCATION (parm
),
7562 "parameter %u has incomplete type",
7565 TREE_VALUE (typelt
) = error_mark_node
;
7566 TREE_TYPE (parm
) = error_mark_node
;
7567 arg_types
= NULL_TREE
;
7569 else if (VOID_TYPE_P (type
))
7571 if (DECL_NAME (parm
))
7572 warning_at (input_location
, 0,
7573 "parameter %u (%q+D) has void type",
7576 warning_at (DECL_SOURCE_LOCATION (parm
), 0,
7577 "parameter %u has void type",
7582 if (DECL_NAME (parm
) && TREE_USED (parm
))
7583 warn_if_shadowing (parm
);
7589 /* Allocate and initialize a c_arg_info structure from the parser's
7593 build_arg_info (void)
7595 struct c_arg_info
*ret
= XOBNEW (&parser_obstack
, struct c_arg_info
);
7596 ret
->parms
= NULL_TREE
;
7598 ret
->types
= NULL_TREE
;
7599 ret
->others
= NULL_TREE
;
7600 ret
->pending_sizes
= NULL
;
7601 ret
->had_vla_unspec
= 0;
7605 /* Take apart the current scope and return a c_arg_info structure with
7606 info on a parameter list just parsed.
7608 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7610 ELLIPSIS being true means the argument list ended in '...' so don't
7611 append a sentinel (void_list_node) to the end of the type-list.
7613 EXPR is NULL or an expression that needs to be evaluated for the
7614 side effects of array size expressions in the parameters. */
7617 get_parm_info (bool ellipsis
, tree expr
)
7619 struct c_binding
*b
= current_scope
->bindings
;
7620 struct c_arg_info
*arg_info
= build_arg_info ();
7622 tree parms
= NULL_TREE
;
7623 vec
<c_arg_tag
, va_gc
> *tags
= NULL
;
7624 tree types
= NULL_TREE
;
7625 tree others
= NULL_TREE
;
7627 bool gave_void_only_once_err
= false;
7629 arg_info
->had_vla_unspec
= current_scope
->had_vla_unspec
;
7631 /* The bindings in this scope must not get put into a block.
7632 We will take care of deleting the binding nodes. */
7633 current_scope
->bindings
= 0;
7635 /* This function is only called if there was *something* on the
7639 /* A parameter list consisting solely of 'void' indicates that the
7640 function takes no arguments. But if the 'void' is qualified
7641 (by 'const' or 'volatile'), or has a storage class specifier
7642 ('register'), then the behavior is undefined; issue an error.
7643 Typedefs for 'void' are OK (see DR#157). */
7644 if (b
->prev
== 0 /* one binding */
7645 && TREE_CODE (b
->decl
) == PARM_DECL
/* which is a parameter */
7646 && !DECL_NAME (b
->decl
) /* anonymous */
7647 && VOID_TYPE_P (TREE_TYPE (b
->decl
))) /* of void type */
7649 if (TYPE_QUALS (TREE_TYPE (b
->decl
)) != TYPE_UNQUALIFIED
7650 || C_DECL_REGISTER (b
->decl
))
7651 error_at (b
->locus
, "%<void%> as only parameter may not be qualified");
7653 /* There cannot be an ellipsis. */
7655 error_at (b
->locus
, "%<void%> must be the only parameter");
7657 arg_info
->types
= void_list_node
;
7662 types
= void_list_node
;
7664 /* Break up the bindings list into parms, tags, types, and others;
7665 apply sanity checks; purge the name-to-decl bindings. */
7668 tree decl
= b
->decl
;
7669 tree type
= TREE_TYPE (decl
);
7671 const char *keyword
;
7673 switch (TREE_CODE (decl
))
7678 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
7679 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
7682 /* Check for forward decls that never got their actual decl. */
7683 if (TREE_ASM_WRITTEN (decl
))
7685 "parameter %q+D has just a forward declaration", decl
);
7686 /* Check for (..., void, ...) and issue an error. */
7687 else if (VOID_TYPE_P (type
) && !DECL_NAME (decl
))
7689 if (!gave_void_only_once_err
)
7691 error_at (b
->locus
, "%<void%> must be the only parameter");
7692 gave_void_only_once_err
= true;
7697 /* Valid parameter, add it to the list. */
7698 DECL_CHAIN (decl
) = parms
;
7701 /* Since there is a prototype, args are passed in their
7702 declared types. The back end may override this later. */
7703 DECL_ARG_TYPE (decl
) = type
;
7704 types
= tree_cons (0, type
, types
);
7708 case ENUMERAL_TYPE
: keyword
= "enum"; goto tag
;
7709 case UNION_TYPE
: keyword
= "union"; goto tag
;
7710 case RECORD_TYPE
: keyword
= "struct"; goto tag
;
7712 /* Types may not have tag-names, in which case the type
7713 appears in the bindings list with b->id NULL. */
7716 gcc_assert (I_TAG_BINDING (b
->id
) == b
);
7717 I_TAG_BINDING (b
->id
) = b
->shadowed
;
7720 /* Warn about any struct, union or enum tags defined in a
7721 parameter list. The scope of such types is limited to
7722 the parameter list, which is rarely if ever desirable
7723 (it's impossible to call such a function with type-
7724 correct arguments). An anonymous union parm type is
7725 meaningful as a GNU extension, so don't warn for that. */
7726 if (TREE_CODE (decl
) != UNION_TYPE
|| b
->id
!= NULL_TREE
)
7729 /* The %s will be one of 'struct', 'union', or 'enum'. */
7730 warning_at (b
->locus
, 0,
7731 "%<%s %E%> declared inside parameter list"
7732 " will not be visible outside of this definition or"
7733 " declaration", keyword
, b
->id
);
7735 /* The %s will be one of 'struct', 'union', or 'enum'. */
7736 warning_at (b
->locus
, 0,
7737 "anonymous %s declared inside parameter list"
7738 " will not be visible outside of this definition or"
7739 " declaration", keyword
);
7744 vec_safe_push (tags
, tag
);
7748 /* FUNCTION_DECLs appear when there is an implicit function
7749 declaration in the parameter list. */
7750 gcc_assert (b
->nested
|| seen_error ());
7755 /* CONST_DECLs appear here when we have an embedded enum,
7756 and TYPE_DECLs appear here when we have an embedded struct
7757 or union. No warnings for this - we already warned about the
7760 /* When we reinsert this decl in the function body, we need
7761 to reconstruct whether it was marked as nested. */
7762 gcc_assert (!b
->nested
);
7763 DECL_CHAIN (decl
) = others
;
7769 /* error_mark_node appears here when we have an undeclared
7770 variable. Just throw it away. */
7773 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
7774 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
7778 /* Other things that might be encountered. */
7785 b
= free_binding_and_advance (b
);
7788 arg_info
->parms
= parms
;
7789 arg_info
->tags
= tags
;
7790 arg_info
->types
= types
;
7791 arg_info
->others
= others
;
7792 arg_info
->pending_sizes
= expr
;
7796 /* Get the struct, enum or union (CODE says which) with tag NAME.
7797 Define the tag as a forward-reference with location LOC if it is
7798 not defined. HAVE_STD_ATTRS says whether any standard attributes
7799 were present after the struct, union or enum keyword; ATTRS are the
7800 standard attributes present there. Return a c_typespec structure
7801 for the type specifier. */
7804 parser_xref_tag (location_t loc
, enum tree_code code
, tree name
,
7805 bool have_std_attrs
, tree attrs
)
7807 struct c_typespec ret
;
7811 ret
.expr
= NULL_TREE
;
7812 ret
.expr_const_operands
= true;
7814 /* If a cross reference is requested, look up the type
7815 already defined for this tag and return it. */
7817 ref
= lookup_tag (code
, name
, false, &refloc
);
7818 /* If this is the right type of tag, return what we found.
7819 (This reference will be shadowed by shadow_tag later if appropriate.)
7820 If this is the wrong type of tag, do not return it. If it was the
7821 wrong type in the same scope, we will have had an error
7822 message already; if in a different scope and declaring
7823 a name, pending_xref_error will give an error message; but if in a
7824 different scope and not declaring a name, this tag should
7825 shadow the previous declaration of a different type of tag, and
7826 this would not work properly if we return the reference found.
7827 (For example, with "struct foo" in an outer scope, "union foo;"
7828 must shadow that tag with a new one of union type.) */
7830 ? (have_std_attrs
? ctsk_tagref_attrs
: ctsk_tagref
)
7831 : (have_std_attrs
? ctsk_tagfirstref_attrs
: ctsk_tagfirstref
));
7832 if (ref
&& TREE_CODE (ref
) == code
)
7834 decl_attributes (&ref
, attrs
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
7835 if (C_TYPE_DEFINED_IN_STRUCT (ref
)
7836 && loc
!= UNKNOWN_LOCATION
7842 warning_at (loc
, OPT_Wc___compat
,
7843 ("enum type defined in struct or union "
7844 "is not visible in C++"));
7845 inform (refloc
, "enum type defined here");
7848 warning_at (loc
, OPT_Wc___compat
,
7849 ("struct defined in struct or union "
7850 "is not visible in C++"));
7851 inform (refloc
, "struct defined here");
7854 warning_at (loc
, OPT_Wc___compat
,
7855 ("union defined in struct or union "
7856 "is not visible in C++"));
7857 inform (refloc
, "union defined here");
7868 /* If no such tag is yet defined, create a forward-reference node
7869 and record it as the "definition".
7870 When a real declaration of this type is found,
7871 the forward-reference will be altered into a real type. */
7873 ref
= make_node (code
);
7874 if (code
== ENUMERAL_TYPE
)
7876 /* Give the type a default layout like unsigned int
7877 to avoid crashing if it does not get defined. */
7878 SET_TYPE_MODE (ref
, TYPE_MODE (unsigned_type_node
));
7879 SET_TYPE_ALIGN (ref
, TYPE_ALIGN (unsigned_type_node
));
7880 TYPE_USER_ALIGN (ref
) = 0;
7881 TYPE_UNSIGNED (ref
) = 1;
7882 TYPE_PRECISION (ref
) = TYPE_PRECISION (unsigned_type_node
);
7883 TYPE_MIN_VALUE (ref
) = TYPE_MIN_VALUE (unsigned_type_node
);
7884 TYPE_MAX_VALUE (ref
) = TYPE_MAX_VALUE (unsigned_type_node
);
7887 pushtag (loc
, name
, ref
);
7888 decl_attributes (&ref
, attrs
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
7894 /* Get the struct, enum or union (CODE says which) with tag NAME.
7895 Define the tag as a forward-reference if it is not defined.
7896 Return a tree for the type. */
7899 xref_tag (enum tree_code code
, tree name
)
7901 return parser_xref_tag (input_location
, code
, name
, false, NULL_TREE
).spec
;
7904 /* Make sure that the tag NAME is defined *in the current scope*
7905 at least as a forward reference.
7906 LOC is the location of the struct's definition.
7907 CODE says which kind of tag NAME ought to be.
7909 This stores the current value of the file static STRUCT_PARSE_INFO
7910 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7911 new c_struct_parse_info structure. The old value of
7912 STRUCT_PARSE_INFO is restored in finish_struct. */
7915 start_struct (location_t loc
, enum tree_code code
, tree name
,
7916 class c_struct_parse_info
**enclosing_struct_parse_info
)
7918 /* If there is already a tag defined at this scope
7919 (as a forward reference), just return it. */
7921 tree ref
= NULL_TREE
;
7922 location_t refloc
= UNKNOWN_LOCATION
;
7924 if (name
!= NULL_TREE
)
7925 ref
= lookup_tag (code
, name
, true, &refloc
);
7926 if (ref
&& TREE_CODE (ref
) == code
)
7928 if (TYPE_STUB_DECL (ref
))
7929 refloc
= DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref
));
7931 if (TYPE_SIZE (ref
))
7933 if (code
== UNION_TYPE
)
7934 error_at (loc
, "redefinition of %<union %E%>", name
);
7936 error_at (loc
, "redefinition of %<struct %E%>", name
);
7937 if (refloc
!= UNKNOWN_LOCATION
)
7938 inform (refloc
, "originally defined here");
7939 /* Don't create structures using a name already in use. */
7942 else if (C_TYPE_BEING_DEFINED (ref
))
7944 if (code
== UNION_TYPE
)
7945 error_at (loc
, "nested redefinition of %<union %E%>", name
);
7947 error_at (loc
, "nested redefinition of %<struct %E%>", name
);
7948 /* Don't bother to report "originally defined here" for a
7949 nested redefinition; the original definition should be
7951 /* Don't create structures that contain themselves. */
7956 /* Otherwise create a forward-reference just so the tag is in scope. */
7958 if (ref
== NULL_TREE
|| TREE_CODE (ref
) != code
)
7960 ref
= make_node (code
);
7961 pushtag (loc
, name
, ref
);
7964 C_TYPE_BEING_DEFINED (ref
) = 1;
7965 for (tree v
= TYPE_MAIN_VARIANT (ref
); v
; v
= TYPE_NEXT_VARIANT (v
))
7966 TYPE_PACKED (v
) = flag_pack_struct
;
7968 *enclosing_struct_parse_info
= struct_parse_info
;
7969 struct_parse_info
= new c_struct_parse_info ();
7971 /* FIXME: This will issue a warning for a use of a type defined
7972 within a statement expr used within sizeof, et. al. This is not
7973 terribly serious as C++ doesn't permit statement exprs within
7975 if (warn_cxx_compat
&& (in_sizeof
|| in_typeof
|| in_alignof
))
7976 warning_at (loc
, OPT_Wc___compat
,
7977 "defining type in %qs expression is invalid in C++",
7980 : (in_typeof
? "typeof" : "alignof")));
7985 /* Process the specs, declarator and width (NULL if omitted)
7986 of a structure component, returning a FIELD_DECL node.
7987 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7988 DECL_ATTRS is as for grokdeclarator.
7990 LOC is the location of the structure component.
7992 This is done during the parsing of the struct declaration.
7993 The FIELD_DECL nodes are chained together and the lot of them
7994 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7997 grokfield (location_t loc
,
7998 struct c_declarator
*declarator
, struct c_declspecs
*declspecs
,
7999 tree width
, tree
*decl_attrs
)
8003 if (declarator
->kind
== cdk_id
&& declarator
->u
.id
.id
== NULL_TREE
8004 && width
== NULL_TREE
)
8006 /* This is an unnamed decl.
8008 If we have something of the form "union { list } ;" then this
8009 is the anonymous union extension. Similarly for struct.
8011 If this is something of the form "struct foo;", then
8012 If MS or Plan 9 extensions are enabled, this is handled as
8013 an anonymous struct.
8014 Otherwise this is a forward declaration of a structure tag.
8016 If this is something of the form "foo;" and foo is a TYPE_DECL, then
8017 If foo names a structure or union without a tag, then this
8018 is an anonymous struct (this is permitted by C11).
8019 If MS or Plan 9 extensions are enabled and foo names a
8020 structure, then again this is an anonymous struct.
8021 Otherwise this is an error.
8023 Oh what a horrid tangled web we weave. I wonder if MS consciously
8024 took this from Plan 9 or if it was an accident of implementation
8025 that took root before someone noticed the bug... */
8027 tree type
= declspecs
->type
;
8030 if (RECORD_OR_UNION_TYPE_P (type
)
8031 && (flag_ms_extensions
8032 || flag_plan9_extensions
8033 || !declspecs
->typedef_p
))
8035 if (flag_ms_extensions
|| flag_plan9_extensions
)
8037 else if (TYPE_NAME (type
) == NULL
)
8044 pedwarn (loc
, 0, "declaration does not declare anything");
8048 pedwarn_c99 (loc
, OPT_Wpedantic
,
8049 "ISO C99 doesn%'t support unnamed structs/unions");
8051 pedwarn_c99 (loc
, OPT_Wpedantic
,
8052 "ISO C90 doesn%'t support unnamed structs/unions");
8055 value
= grokdeclarator (declarator
, declspecs
, FIELD
, false,
8056 width
? &width
: NULL
, decl_attrs
, NULL
, NULL
,
8059 finish_decl (value
, loc
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
8060 DECL_INITIAL (value
) = width
;
8062 SET_DECL_C_BIT_FIELD (value
);
8064 if (warn_cxx_compat
&& DECL_NAME (value
) != NULL_TREE
)
8066 /* If we currently have a binding for this field, set the
8067 in_struct field in the binding, so that we warn about lookups
8069 struct c_binding
*b
= I_SYMBOL_BINDING (DECL_NAME (value
));
8072 /* If the in_struct field is not yet set, push it on a list
8073 to be cleared when this struct is finished. */
8076 struct_parse_info
->fields
.safe_push (b
);
8085 /* Subroutine of detect_field_duplicates: return whether X and Y,
8086 which are both fields in the same struct, have duplicate field
8090 is_duplicate_field (tree x
, tree y
)
8092 if (DECL_NAME (x
) != NULL_TREE
&& DECL_NAME (x
) == DECL_NAME (y
))
8095 /* When using -fplan9-extensions, an anonymous field whose name is a
8096 typedef can duplicate a field name. */
8097 if (flag_plan9_extensions
8098 && (DECL_NAME (x
) == NULL_TREE
|| DECL_NAME (y
) == NULL_TREE
))
8100 tree xt
, xn
, yt
, yn
;
8103 if (DECL_NAME (x
) != NULL_TREE
)
8105 else if (RECORD_OR_UNION_TYPE_P (xt
)
8106 && TYPE_NAME (xt
) != NULL_TREE
8107 && TREE_CODE (TYPE_NAME (xt
)) == TYPE_DECL
)
8108 xn
= DECL_NAME (TYPE_NAME (xt
));
8113 if (DECL_NAME (y
) != NULL_TREE
)
8115 else if (RECORD_OR_UNION_TYPE_P (yt
)
8116 && TYPE_NAME (yt
) != NULL_TREE
8117 && TREE_CODE (TYPE_NAME (yt
)) == TYPE_DECL
)
8118 yn
= DECL_NAME (TYPE_NAME (yt
));
8122 if (xn
!= NULL_TREE
&& xn
== yn
)
8129 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
8130 to HTAB, giving errors for any duplicates. */
8133 detect_field_duplicates_hash (tree fieldlist
,
8134 hash_table
<nofree_ptr_hash
<tree_node
> > *htab
)
8139 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
8140 if ((y
= DECL_NAME (x
)) != NULL_TREE
)
8142 slot
= htab
->find_slot (y
, INSERT
);
8145 error ("duplicate member %q+D", x
);
8146 DECL_NAME (x
) = NULL_TREE
;
8150 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
8152 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x
)), htab
);
8154 /* When using -fplan9-extensions, an anonymous field whose
8155 name is a typedef can duplicate a field name. */
8156 if (flag_plan9_extensions
8157 && TYPE_NAME (TREE_TYPE (x
)) != NULL_TREE
8158 && TREE_CODE (TYPE_NAME (TREE_TYPE (x
))) == TYPE_DECL
)
8160 tree xn
= DECL_NAME (TYPE_NAME (TREE_TYPE (x
)));
8161 slot
= htab
->find_slot (xn
, INSERT
);
8163 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x
)));
8169 /* Generate an error for any duplicate field names in FIELDLIST. Munge
8170 the list such that this does not present a problem later. */
8173 detect_field_duplicates (tree fieldlist
)
8178 /* If the struct is the list of instance variables of an Objective-C
8179 class, then we need to check all the instance variables of
8180 superclasses when checking for duplicates (since you can't have
8181 an instance variable in a subclass with the same name as an
8182 instance variable in a superclass). We pass on this job to the
8183 Objective-C compiler. objc_detect_field_duplicates() will return
8184 false if we are not checking the list of instance variables and
8185 the C frontend should proceed with the standard field duplicate
8186 checks. If we are checking the list of instance variables, the
8187 ObjC frontend will do the check, emit the errors if needed, and
8188 then return true. */
8189 if (c_dialect_objc ())
8190 if (objc_detect_field_duplicates (false))
8193 /* First, see if there are more than "a few" fields.
8194 This is trivially true if there are zero or one fields. */
8195 if (!fieldlist
|| !DECL_CHAIN (fieldlist
))
8200 if (DECL_NAME (x
) == NULL_TREE
8201 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
8204 } while (timeout
> 0 && x
);
8206 /* If there were "few" fields and no anonymous structures or unions,
8207 avoid the overhead of allocating a hash table. Instead just do
8208 the nested traversal thing. */
8211 for (x
= DECL_CHAIN (fieldlist
); x
; x
= DECL_CHAIN (x
))
8212 /* When using -fplan9-extensions, we can have duplicates
8213 between typedef names and fields. */
8215 || (flag_plan9_extensions
8216 && DECL_NAME (x
) == NULL_TREE
8217 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
))
8218 && TYPE_NAME (TREE_TYPE (x
)) != NULL_TREE
8219 && TREE_CODE (TYPE_NAME (TREE_TYPE (x
))) == TYPE_DECL
))
8221 for (y
= fieldlist
; y
!= x
; y
= TREE_CHAIN (y
))
8222 if (is_duplicate_field (y
, x
))
8224 error ("duplicate member %q+D", x
);
8225 DECL_NAME (x
) = NULL_TREE
;
8231 hash_table
<nofree_ptr_hash
<tree_node
> > htab (37);
8232 detect_field_duplicates_hash (fieldlist
, &htab
);
8236 /* Finish up struct info used by -Wc++-compat. */
8239 warn_cxx_compat_finish_struct (tree fieldlist
, enum tree_code code
,
8240 location_t record_loc
)
8244 struct c_binding
*b
;
8246 if (fieldlist
== NULL_TREE
)
8248 if (code
== RECORD_TYPE
)
8249 warning_at (record_loc
, OPT_Wc___compat
,
8250 "empty struct has size 0 in C, size 1 in C++");
8252 warning_at (record_loc
, OPT_Wc___compat
,
8253 "empty union has size 0 in C, size 1 in C++");
8256 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
8257 the current struct. We do this now at the end of the struct
8258 because the flag is used to issue visibility warnings, and we
8259 only want to issue those warnings if the type is referenced
8260 outside of the struct declaration. */
8261 FOR_EACH_VEC_ELT (struct_parse_info
->struct_types
, ix
, x
)
8262 C_TYPE_DEFINED_IN_STRUCT (x
) = 1;
8264 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
8265 typedefs used when declaring fields in this struct. If the name
8266 of any of the fields is also a typedef name then the struct would
8267 not parse in C++, because the C++ lookup rules say that the
8268 typedef name would be looked up in the context of the struct, and
8269 would thus be the field rather than the typedef. */
8270 if (!struct_parse_info
->typedefs_seen
.is_empty ()
8271 && fieldlist
!= NULL_TREE
)
8273 /* Use a hash_set<tree> using the name of the typedef. We can use
8274 a hash_set<tree> because identifiers are interned. */
8275 hash_set
<tree
> tset
;
8277 FOR_EACH_VEC_ELT (struct_parse_info
->typedefs_seen
, ix
, x
)
8278 tset
.add (DECL_NAME (x
));
8280 for (x
= fieldlist
; x
!= NULL_TREE
; x
= DECL_CHAIN (x
))
8282 if (DECL_NAME (x
) != NULL_TREE
8283 && tset
.contains (DECL_NAME (x
)))
8285 warning_at (DECL_SOURCE_LOCATION (x
), OPT_Wc___compat
,
8286 ("using %qD as both field and typedef name is "
8289 /* FIXME: It would be nice to report the location where
8290 the typedef name is used. */
8295 /* For each field which has a binding and which was not defined in
8296 an enclosing struct, clear the in_struct field. */
8297 FOR_EACH_VEC_ELT (struct_parse_info
->fields
, ix
, b
)
8301 /* Function to help qsort sort FIELD_DECLs by name order. */
8304 field_decl_cmp (const void *x_p
, const void *y_p
)
8306 const tree
*const x
= (const tree
*) x_p
;
8307 const tree
*const y
= (const tree
*) y_p
;
8309 if (DECL_NAME (*x
) == DECL_NAME (*y
))
8310 /* A nontype is "greater" than a type. */
8311 return (TREE_CODE (*y
) == TYPE_DECL
) - (TREE_CODE (*x
) == TYPE_DECL
);
8312 if (DECL_NAME (*x
) == NULL_TREE
)
8314 if (DECL_NAME (*y
) == NULL_TREE
)
8316 if (DECL_NAME (*x
) < DECL_NAME (*y
))
8321 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
8322 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
8323 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
8324 ATTRIBUTES are attributes to be applied to the structure.
8326 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
8327 the struct was started. */
8330 finish_struct (location_t loc
, tree t
, tree fieldlist
, tree attributes
,
8331 class c_struct_parse_info
*enclosing_struct_parse_info
)
8334 bool toplevel
= file_scope
== current_scope
;
8336 /* If this type was previously laid out as a forward reference,
8337 make sure we lay it out again. */
8339 TYPE_SIZE (t
) = NULL_TREE
;
8341 decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
8345 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
8347 if (DECL_NAME (x
) != NULL_TREE
)
8349 if (flag_isoc11
&& RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
8355 if (TREE_CODE (t
) == UNION_TYPE
)
8358 pedwarn (loc
, OPT_Wpedantic
, "union has no named members");
8360 pedwarn (loc
, OPT_Wpedantic
, "union has no members");
8365 pedwarn (loc
, OPT_Wpedantic
, "struct has no named members");
8367 pedwarn (loc
, OPT_Wpedantic
, "struct has no members");
8372 /* Install struct as DECL_CONTEXT of each field decl.
8373 Also process specified field sizes, found in the DECL_INITIAL,
8374 storing 0 there after the type has been changed to precision equal
8375 to its width, rather than the precision of the specified standard
8376 type. (Correct layout requires the original type to have been preserved
8379 bool saw_named_field
= false;
8380 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
8382 if (TREE_TYPE (x
) == error_mark_node
)
8385 DECL_CONTEXT (x
) = t
;
8387 /* If any field is const, the structure type is pseudo-const. */
8388 if (TREE_READONLY (x
))
8389 C_TYPE_FIELDS_READONLY (t
) = 1;
8392 /* A field that is pseudo-const makes the structure likewise. */
8393 tree t1
= strip_array_types (TREE_TYPE (x
));
8394 if (RECORD_OR_UNION_TYPE_P (t1
) && C_TYPE_FIELDS_READONLY (t1
))
8395 C_TYPE_FIELDS_READONLY (t
) = 1;
8398 /* Any field that is volatile means variables of this type must be
8399 treated in some ways as volatile. */
8400 if (TREE_THIS_VOLATILE (x
))
8401 C_TYPE_FIELDS_VOLATILE (t
) = 1;
8403 /* Any field of nominal variable size implies structure is too. */
8404 if (C_DECL_VARIABLE_SIZE (x
))
8405 C_TYPE_VARIABLE_SIZE (t
) = 1;
8407 if (DECL_C_BIT_FIELD (x
))
8409 unsigned HOST_WIDE_INT width
= tree_to_uhwi (DECL_INITIAL (x
));
8410 DECL_SIZE (x
) = bitsize_int (width
);
8411 DECL_BIT_FIELD (x
) = 1;
8415 && (DECL_BIT_FIELD (x
)
8416 || TYPE_ALIGN (TREE_TYPE (x
)) > BITS_PER_UNIT
))
8417 DECL_PACKED (x
) = 1;
8419 /* Detect flexible array member in an invalid context. */
8420 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
8421 && TYPE_SIZE (TREE_TYPE (x
)) == NULL_TREE
8422 && TYPE_DOMAIN (TREE_TYPE (x
)) != NULL_TREE
8423 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x
))) == NULL_TREE
)
8425 if (TREE_CODE (t
) == UNION_TYPE
)
8427 error_at (DECL_SOURCE_LOCATION (x
),
8428 "flexible array member in union");
8429 TREE_TYPE (x
) = error_mark_node
;
8431 else if (DECL_CHAIN (x
) != NULL_TREE
)
8433 error_at (DECL_SOURCE_LOCATION (x
),
8434 "flexible array member not at end of struct");
8435 TREE_TYPE (x
) = error_mark_node
;
8437 else if (!saw_named_field
)
8439 error_at (DECL_SOURCE_LOCATION (x
),
8440 "flexible array member in a struct with no named "
8442 TREE_TYPE (x
) = error_mark_node
;
8446 if (pedantic
&& TREE_CODE (t
) == RECORD_TYPE
8447 && flexible_array_type_p (TREE_TYPE (x
)))
8448 pedwarn (DECL_SOURCE_LOCATION (x
), OPT_Wpedantic
,
8449 "invalid use of structure with flexible array member");
8452 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
8453 saw_named_field
= true;
8456 detect_field_duplicates (fieldlist
);
8458 /* Now we have the nearly final fieldlist. Record it,
8459 then lay out the structure or union (including the fields). */
8461 TYPE_FIELDS (t
) = fieldlist
;
8463 maybe_apply_pragma_scalar_storage_order (t
);
8467 if (TYPE_SIZE_UNIT (t
)
8468 && TREE_CODE (TYPE_SIZE_UNIT (t
)) == INTEGER_CST
8469 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t
))
8470 && !valid_constant_size_p (TYPE_SIZE_UNIT (t
)))
8471 error ("type %qT is too large", t
);
8473 /* Give bit-fields their proper types and rewrite the type of array fields
8474 with scalar component if the enclosing type has reverse storage order. */
8475 for (tree field
= fieldlist
; field
; field
= DECL_CHAIN (field
))
8477 if (TREE_CODE (field
) == FIELD_DECL
8478 && DECL_INITIAL (field
)
8479 && TREE_TYPE (field
) != error_mark_node
)
8481 unsigned HOST_WIDE_INT width
8482 = tree_to_uhwi (DECL_INITIAL (field
));
8483 tree type
= TREE_TYPE (field
);
8484 if (width
!= TYPE_PRECISION (type
))
8487 = c_build_bitfield_integer_type (width
, TYPE_UNSIGNED (type
));
8488 SET_DECL_MODE (field
, TYPE_MODE (TREE_TYPE (field
)));
8490 DECL_INITIAL (field
) = NULL_TREE
;
8492 else if (TYPE_REVERSE_STORAGE_ORDER (t
)
8493 && TREE_CODE (field
) == FIELD_DECL
8494 && TREE_CODE (TREE_TYPE (field
)) == ARRAY_TYPE
)
8496 tree ftype
= TREE_TYPE (field
);
8497 tree ctype
= strip_array_types (ftype
);
8498 if (!RECORD_OR_UNION_TYPE_P (ctype
) && TYPE_MODE (ctype
) != QImode
)
8500 tree fmain_type
= TYPE_MAIN_VARIANT (ftype
);
8501 tree
*typep
= &fmain_type
;
8503 *typep
= build_distinct_type_copy (*typep
);
8504 TYPE_REVERSE_STORAGE_ORDER (*typep
) = 1;
8505 typep
= &TREE_TYPE (*typep
);
8506 } while (TREE_CODE (*typep
) == ARRAY_TYPE
);
8508 = c_build_qualified_type (fmain_type
, TYPE_QUALS (ftype
));
8513 /* Now we have the truly final field list.
8514 Store it in this type and in the variants. */
8516 TYPE_FIELDS (t
) = fieldlist
;
8518 /* If there are lots of fields, sort so we can look through them fast.
8519 We arbitrarily consider 16 or more elts to be "a lot". */
8524 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
8526 if (len
> 15 || DECL_NAME (x
) == NULL
)
8534 struct lang_type
*space
;
8535 struct sorted_fields_type
*space2
;
8537 len
+= list_length (x
);
8539 /* Use the same allocation policy here that make_node uses, to
8540 ensure that this lives as long as the rest of the struct decl.
8541 All decls in an inline function need to be saved. */
8543 space
= ggc_cleared_alloc
<struct lang_type
> ();
8544 space2
= (sorted_fields_type
*) ggc_internal_alloc
8545 (sizeof (struct sorted_fields_type
) + len
* sizeof (tree
));
8549 field_array
= &space2
->elts
[0];
8550 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
8552 field_array
[len
++] = x
;
8554 /* If there is anonymous struct or union, break out of the loop. */
8555 if (DECL_NAME (x
) == NULL
)
8558 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8561 TYPE_LANG_SPECIFIC (t
) = space
;
8562 TYPE_LANG_SPECIFIC (t
)->s
->len
= len
;
8563 field_array
= TYPE_LANG_SPECIFIC (t
)->s
->elts
;
8564 qsort (field_array
, len
, sizeof (tree
), field_decl_cmp
);
8569 /* If this was supposed to be a transparent union, but we can't
8570 make it one, warn and turn off the flag. */
8571 if (TREE_CODE (t
) == UNION_TYPE
8572 && TYPE_TRANSPARENT_AGGR (t
)
8573 && (!TYPE_FIELDS (t
) || TYPE_MODE (t
) != DECL_MODE (TYPE_FIELDS (t
))))
8575 TYPE_TRANSPARENT_AGGR (t
) = 0;
8576 warning_at (loc
, 0, "union cannot be made transparent");
8579 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8580 in dwarf2out via rest_of_decl_compilation below and means
8581 something totally different. Since we will be clearing
8582 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8583 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8584 C_TYPE_INCOMPLETE_VARS should use some language specific
8586 tree incomplete_vars
= C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t
));
8587 for (x
= TYPE_MAIN_VARIANT (t
); x
; x
= TYPE_NEXT_VARIANT (x
))
8589 TYPE_FIELDS (x
) = TYPE_FIELDS (t
);
8590 TYPE_LANG_SPECIFIC (x
) = TYPE_LANG_SPECIFIC (t
);
8591 TYPE_TRANSPARENT_AGGR (x
) = TYPE_TRANSPARENT_AGGR (t
);
8592 C_TYPE_FIELDS_READONLY (x
) = C_TYPE_FIELDS_READONLY (t
);
8593 C_TYPE_FIELDS_VOLATILE (x
) = C_TYPE_FIELDS_VOLATILE (t
);
8594 C_TYPE_VARIABLE_SIZE (x
) = C_TYPE_VARIABLE_SIZE (t
);
8595 C_TYPE_INCOMPLETE_VARS (x
) = NULL_TREE
;
8598 /* Update type location to the one of the definition, instead of e.g.
8599 a forward declaration. */
8600 if (TYPE_STUB_DECL (t
))
8601 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t
)) = loc
;
8603 /* Finish debugging output for this type. */
8604 rest_of_type_compilation (t
, toplevel
);
8606 /* If this structure or union completes the type of any previous
8607 variable declaration, lay it out and output its rtl. */
8608 for (x
= incomplete_vars
; x
; x
= TREE_CHAIN (x
))
8610 tree decl
= TREE_VALUE (x
);
8611 if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
8612 layout_array_type (TREE_TYPE (decl
));
8613 if (TREE_CODE (decl
) != TYPE_DECL
)
8615 layout_decl (decl
, 0);
8616 if (c_dialect_objc ())
8617 objc_check_decl (decl
);
8618 rest_of_decl_compilation (decl
, toplevel
, 0);
8622 /* If we're inside a function proper, i.e. not file-scope and not still
8623 parsing parameters, then arrange for the size of a variable sized type
8625 if (building_stmt_list_p () && variably_modified_type_p (t
, NULL_TREE
))
8626 add_stmt (build_stmt (loc
,
8627 DECL_EXPR
, build_decl (loc
, TYPE_DECL
, NULL
, t
)));
8629 if (warn_cxx_compat
)
8630 warn_cxx_compat_finish_struct (fieldlist
, TREE_CODE (t
), loc
);
8632 delete struct_parse_info
;
8634 struct_parse_info
= enclosing_struct_parse_info
;
8636 /* If this struct is defined inside a struct, add it to
8639 && struct_parse_info
!= NULL
8640 && !in_sizeof
&& !in_typeof
&& !in_alignof
)
8641 struct_parse_info
->struct_types
.safe_push (t
);
8647 gt_pointer_operator new_value
;
8651 /* This routine compares two fields like field_decl_cmp but using the
8652 pointer operator in resort_data. */
8655 resort_field_decl_cmp (const void *x_p
, const void *y_p
)
8657 const tree
*const x
= (const tree
*) x_p
;
8658 const tree
*const y
= (const tree
*) y_p
;
8660 if (DECL_NAME (*x
) == DECL_NAME (*y
))
8661 /* A nontype is "greater" than a type. */
8662 return (TREE_CODE (*y
) == TYPE_DECL
) - (TREE_CODE (*x
) == TYPE_DECL
);
8663 if (DECL_NAME (*x
) == NULL_TREE
)
8665 if (DECL_NAME (*y
) == NULL_TREE
)
8668 tree d1
= DECL_NAME (*x
);
8669 tree d2
= DECL_NAME (*y
);
8670 resort_data
.new_value (&d1
, resort_data
.cookie
);
8671 resort_data
.new_value (&d2
, resort_data
.cookie
);
8678 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8681 resort_sorted_fields (void *obj
,
8682 void * ARG_UNUSED (orig_obj
),
8683 gt_pointer_operator new_value
,
8686 struct sorted_fields_type
*sf
= (struct sorted_fields_type
*) obj
;
8687 resort_data
.new_value
= new_value
;
8688 resort_data
.cookie
= cookie
;
8689 qsort (&sf
->elts
[0], sf
->len
, sizeof (tree
),
8690 resort_field_decl_cmp
);
8693 /* Lay out the type T, and its element type, and so on. */
8696 layout_array_type (tree t
)
8698 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
8699 layout_array_type (TREE_TYPE (t
));
8703 /* Begin compiling the definition of an enumeration type.
8704 NAME is its name (or null if anonymous).
8705 LOC is the enum's location.
8706 Returns the type object, as yet incomplete.
8707 Also records info about it so that build_enumerator
8708 may be used to declare the individual values as they are read. */
8711 start_enum (location_t loc
, struct c_enum_contents
*the_enum
, tree name
)
8713 tree enumtype
= NULL_TREE
;
8714 location_t enumloc
= UNKNOWN_LOCATION
;
8716 /* If this is the real definition for a previous forward reference,
8717 fill in the contents in the same object that used to be the
8718 forward reference. */
8720 if (name
!= NULL_TREE
)
8721 enumtype
= lookup_tag (ENUMERAL_TYPE
, name
, true, &enumloc
);
8723 if (enumtype
== NULL_TREE
|| TREE_CODE (enumtype
) != ENUMERAL_TYPE
)
8725 enumtype
= make_node (ENUMERAL_TYPE
);
8726 pushtag (loc
, name
, enumtype
);
8728 /* Update type location to the one of the definition, instead of e.g.
8729 a forward declaration. */
8730 else if (TYPE_STUB_DECL (enumtype
))
8732 enumloc
= DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype
));
8733 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype
)) = loc
;
8736 if (C_TYPE_BEING_DEFINED (enumtype
))
8737 error_at (loc
, "nested redefinition of %<enum %E%>", name
);
8739 C_TYPE_BEING_DEFINED (enumtype
) = 1;
8741 if (TYPE_VALUES (enumtype
) != NULL_TREE
)
8743 /* This enum is a named one that has been declared already. */
8744 error_at (loc
, "redeclaration of %<enum %E%>", name
);
8745 if (enumloc
!= UNKNOWN_LOCATION
)
8746 inform (enumloc
, "originally defined here");
8748 /* Completely replace its old definition.
8749 The old enumerators remain defined, however. */
8750 TYPE_VALUES (enumtype
) = NULL_TREE
;
8753 the_enum
->enum_next_value
= integer_zero_node
;
8754 the_enum
->enum_overflow
= 0;
8756 if (flag_short_enums
)
8757 for (tree v
= TYPE_MAIN_VARIANT (enumtype
); v
; v
= TYPE_NEXT_VARIANT (v
))
8758 TYPE_PACKED (v
) = 1;
8760 /* FIXME: This will issue a warning for a use of a type defined
8761 within sizeof in a statement expr. This is not terribly serious
8762 as C++ doesn't permit statement exprs within sizeof anyhow. */
8763 if (warn_cxx_compat
&& (in_sizeof
|| in_typeof
|| in_alignof
))
8764 warning_at (loc
, OPT_Wc___compat
,
8765 "defining type in %qs expression is invalid in C++",
8768 : (in_typeof
? "typeof" : "alignof")));
8773 /* After processing and defining all the values of an enumeration type,
8774 install their decls in the enumeration type and finish it off.
8775 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8776 and ATTRIBUTES are the specified attributes.
8777 Returns ENUMTYPE. */
8780 finish_enum (tree enumtype
, tree values
, tree attributes
)
8783 tree minnode
= NULL_TREE
, maxnode
= NULL_TREE
;
8786 bool toplevel
= (file_scope
== current_scope
);
8787 struct lang_type
*lt
;
8789 decl_attributes (&enumtype
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
8791 /* Calculate the maximum value of any enumerator in this type. */
8793 if (values
== error_mark_node
)
8794 minnode
= maxnode
= integer_zero_node
;
8797 minnode
= maxnode
= TREE_VALUE (values
);
8798 for (pair
= TREE_CHAIN (values
); pair
; pair
= TREE_CHAIN (pair
))
8800 tree value
= TREE_VALUE (pair
);
8801 if (tree_int_cst_lt (maxnode
, value
))
8803 if (tree_int_cst_lt (value
, minnode
))
8808 /* Construct the final type of this enumeration. It is the same
8809 as one of the integral types - the narrowest one that fits, except
8810 that normally we only go as narrow as int - and signed iff any of
8811 the values are negative. */
8812 sign
= (tree_int_cst_sgn (minnode
) >= 0) ? UNSIGNED
: SIGNED
;
8813 precision
= MAX (tree_int_cst_min_precision (minnode
, sign
),
8814 tree_int_cst_min_precision (maxnode
, sign
));
8816 /* If the precision of the type was specified with an attribute and it
8817 was too small, give an error. Otherwise, use it. */
8818 if (TYPE_PRECISION (enumtype
) && lookup_attribute ("mode", attributes
))
8820 if (precision
> TYPE_PRECISION (enumtype
))
8822 TYPE_PRECISION (enumtype
) = 0;
8823 error ("specified mode too small for enumerated values");
8826 precision
= TYPE_PRECISION (enumtype
);
8829 TYPE_PRECISION (enumtype
) = 0;
8831 if (TYPE_PACKED (enumtype
)
8832 || precision
> TYPE_PRECISION (integer_type_node
)
8833 || TYPE_PRECISION (enumtype
))
8835 tem
= c_common_type_for_size (precision
, sign
== UNSIGNED
? 1 : 0);
8838 warning (0, "enumeration values exceed range of largest integer");
8839 tem
= long_long_integer_type_node
;
8843 tem
= sign
== UNSIGNED
? unsigned_type_node
: integer_type_node
;
8845 TYPE_MIN_VALUE (enumtype
) = TYPE_MIN_VALUE (tem
);
8846 TYPE_MAX_VALUE (enumtype
) = TYPE_MAX_VALUE (tem
);
8847 TYPE_UNSIGNED (enumtype
) = TYPE_UNSIGNED (tem
);
8848 SET_TYPE_ALIGN (enumtype
, TYPE_ALIGN (tem
));
8849 TYPE_SIZE (enumtype
) = NULL_TREE
;
8850 TYPE_PRECISION (enumtype
) = TYPE_PRECISION (tem
);
8852 layout_type (enumtype
);
8854 if (values
!= error_mark_node
)
8856 /* Change the type of the enumerators to be the enum type. We
8857 need to do this irrespective of the size of the enum, for
8858 proper type checking. Replace the DECL_INITIALs of the
8859 enumerators, and the value slots of the list, with copies
8860 that have the enum type; they cannot be modified in place
8861 because they may be shared (e.g. integer_zero_node) Finally,
8862 change the purpose slots to point to the names of the decls. */
8863 for (pair
= values
; pair
; pair
= TREE_CHAIN (pair
))
8865 tree enu
= TREE_PURPOSE (pair
);
8866 tree ini
= DECL_INITIAL (enu
);
8868 TREE_TYPE (enu
) = enumtype
;
8870 /* The ISO C Standard mandates enumerators to have type int,
8871 even though the underlying type of an enum type is
8872 unspecified. However, GCC allows enumerators of any
8873 integer type as an extensions. build_enumerator()
8874 converts any enumerators that fit in an int to type int,
8875 to avoid promotions to unsigned types when comparing
8876 integers with enumerators that fit in the int range.
8877 When -pedantic is given, build_enumerator() would have
8878 already warned about those that don't fit. Here we
8879 convert the rest to the enumerator type. */
8880 if (TREE_TYPE (ini
) != integer_type_node
)
8881 ini
= convert (enumtype
, ini
);
8883 DECL_INITIAL (enu
) = ini
;
8884 TREE_PURPOSE (pair
) = DECL_NAME (enu
);
8885 TREE_VALUE (pair
) = ini
;
8888 TYPE_VALUES (enumtype
) = values
;
8891 /* Record the min/max values so that we can warn about bit-field
8892 enumerations that are too small for the values. */
8893 lt
= ggc_cleared_alloc
<struct lang_type
> ();
8894 lt
->enum_min
= minnode
;
8895 lt
->enum_max
= maxnode
;
8896 TYPE_LANG_SPECIFIC (enumtype
) = lt
;
8898 /* Fix up all variant types of this enum type. */
8899 for (tem
= TYPE_MAIN_VARIANT (enumtype
); tem
; tem
= TYPE_NEXT_VARIANT (tem
))
8901 if (tem
== enumtype
)
8903 TYPE_VALUES (tem
) = TYPE_VALUES (enumtype
);
8904 TYPE_MIN_VALUE (tem
) = TYPE_MIN_VALUE (enumtype
);
8905 TYPE_MAX_VALUE (tem
) = TYPE_MAX_VALUE (enumtype
);
8906 TYPE_SIZE (tem
) = TYPE_SIZE (enumtype
);
8907 TYPE_SIZE_UNIT (tem
) = TYPE_SIZE_UNIT (enumtype
);
8908 SET_TYPE_MODE (tem
, TYPE_MODE (enumtype
));
8909 TYPE_PRECISION (tem
) = TYPE_PRECISION (enumtype
);
8910 SET_TYPE_ALIGN (tem
, TYPE_ALIGN (enumtype
));
8911 TYPE_USER_ALIGN (tem
) = TYPE_USER_ALIGN (enumtype
);
8912 TYPE_UNSIGNED (tem
) = TYPE_UNSIGNED (enumtype
);
8913 TYPE_LANG_SPECIFIC (tem
) = TYPE_LANG_SPECIFIC (enumtype
);
8916 /* Finish debugging output for this type. */
8917 rest_of_type_compilation (enumtype
, toplevel
);
8919 /* If this enum is defined inside a struct, add it to
8922 && struct_parse_info
!= NULL
8923 && !in_sizeof
&& !in_typeof
&& !in_alignof
)
8924 struct_parse_info
->struct_types
.safe_push (enumtype
);
8926 C_TYPE_BEING_DEFINED (enumtype
) = 0;
8931 /* Build and install a CONST_DECL for one value of the
8932 current enumeration type (one that was begun with start_enum).
8933 DECL_LOC is the location of the enumerator.
8934 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8935 Return a tree-list containing the CONST_DECL and its value.
8936 Assignment of sequential values by default is handled here. */
8939 build_enumerator (location_t decl_loc
, location_t loc
,
8940 struct c_enum_contents
*the_enum
, tree name
, tree value
)
8944 /* Validate and default VALUE. */
8946 if (value
!= NULL_TREE
)
8948 /* Don't issue more errors for error_mark_node (i.e. an
8949 undeclared identifier) - just ignore the value expression. */
8950 if (value
== error_mark_node
)
8952 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value
)))
8954 error_at (loc
, "enumerator value for %qE is not an integer constant",
8960 if (TREE_CODE (value
) != INTEGER_CST
)
8962 value
= c_fully_fold (value
, false, NULL
);
8963 if (TREE_CODE (value
) == INTEGER_CST
)
8964 pedwarn (loc
, OPT_Wpedantic
,
8965 "enumerator value for %qE is not an integer "
8966 "constant expression", name
);
8968 if (TREE_CODE (value
) != INTEGER_CST
)
8970 error ("enumerator value for %qE is not an integer constant",
8976 value
= default_conversion (value
);
8977 constant_expression_warning (value
);
8982 /* Default based on previous value. */
8983 /* It should no longer be possible to have NON_LVALUE_EXPR
8985 if (value
== NULL_TREE
)
8987 value
= the_enum
->enum_next_value
;
8988 if (the_enum
->enum_overflow
)
8989 error_at (loc
, "overflow in enumeration values");
8991 /* Even though the underlying type of an enum is unspecified, the
8992 type of enumeration constants is explicitly defined as int
8993 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8995 else if (!int_fits_type_p (value
, integer_type_node
))
8996 pedwarn (loc
, OPT_Wpedantic
,
8997 "ISO C restricts enumerator values to range of %<int%>");
8999 /* The ISO C Standard mandates enumerators to have type int, even
9000 though the underlying type of an enum type is unspecified.
9001 However, GCC allows enumerators of any integer type as an
9002 extensions. Here we convert any enumerators that fit in an int
9003 to type int, to avoid promotions to unsigned types when comparing
9004 integers with enumerators that fit in the int range. When
9005 -pedantic is given, we would have already warned about those that
9006 don't fit. We have to do this here rather than in finish_enum
9007 because this value may be used to define more enumerators. */
9008 if (int_fits_type_p (value
, integer_type_node
))
9009 value
= convert (integer_type_node
, value
);
9011 /* Set basis for default for next value. */
9012 the_enum
->enum_next_value
9013 = build_binary_op (EXPR_LOC_OR_LOC (value
, input_location
),
9014 PLUS_EXPR
, value
, integer_one_node
, false);
9015 the_enum
->enum_overflow
= tree_int_cst_lt (the_enum
->enum_next_value
, value
);
9017 /* Now create a declaration for the enum value name. */
9019 type
= TREE_TYPE (value
);
9020 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
9021 TYPE_PRECISION (integer_type_node
)),
9022 (TYPE_PRECISION (type
)
9023 >= TYPE_PRECISION (integer_type_node
)
9024 && TYPE_UNSIGNED (type
)));
9026 decl
= build_decl (decl_loc
, CONST_DECL
, name
, type
);
9027 DECL_INITIAL (decl
) = convert (type
, value
);
9030 return tree_cons (decl
, value
, NULL_TREE
);
9033 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
9036 c_simulate_enum_decl (location_t loc
, const char *name
,
9037 vec
<string_int_pair
> values
)
9039 location_t saved_loc
= input_location
;
9040 input_location
= loc
;
9042 struct c_enum_contents the_enum
;
9043 tree enumtype
= start_enum (loc
, &the_enum
, get_identifier (name
));
9045 tree value_chain
= NULL_TREE
;
9046 string_int_pair
*value
;
9048 FOR_EACH_VEC_ELT (values
, i
, value
)
9050 tree decl
= build_enumerator (loc
, loc
, &the_enum
,
9051 get_identifier (value
->first
),
9052 build_int_cst (integer_type_node
,
9054 TREE_CHAIN (decl
) = value_chain
;
9058 finish_enum (enumtype
, nreverse (value_chain
), NULL_TREE
);
9060 input_location
= saved_loc
;
9064 /* Create the FUNCTION_DECL for a function definition.
9065 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
9066 the declaration; they describe the function's name and the type it returns,
9067 but twisted together in a fashion that parallels the syntax of C.
9069 This function creates a binding context for the function body
9070 as well as setting up the FUNCTION_DECL in current_function_decl.
9072 Returns true on success. If the DECLARATOR is not suitable for a function
9073 (it defines a datum instead), we return false to report a parse error. */
9076 start_function (struct c_declspecs
*declspecs
, struct c_declarator
*declarator
,
9079 tree decl1
, old_decl
;
9080 tree restype
, resdecl
;
9083 current_function_returns_value
= 0; /* Assume, until we see it does. */
9084 current_function_returns_null
= 0;
9085 current_function_returns_abnormally
= 0;
9086 warn_about_return_type
= 0;
9087 c_switch_stack
= NULL
;
9089 /* Indicate no valid break/continue context by setting these variables
9090 to some non-null, non-label value. We'll notice and emit the proper
9091 error message in c_finish_bc_stmt. */
9092 c_break_label
= c_cont_label
= size_zero_node
;
9094 decl1
= grokdeclarator (declarator
, declspecs
, FUNCDEF
, true, NULL
,
9095 &attributes
, NULL
, NULL
, DEPRECATED_NORMAL
);
9096 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION
, decl1
);
9098 /* If the declarator is not suitable for a function definition,
9099 cause a syntax error. */
9100 if (decl1
== NULL_TREE
9101 || TREE_CODE (decl1
) != FUNCTION_DECL
)
9104 loc
= DECL_SOURCE_LOCATION (decl1
);
9106 /* A nested function is not global. */
9107 if (current_function_decl
!= NULL_TREE
)
9108 TREE_PUBLIC (decl1
) = 0;
9110 c_decl_attributes (&decl1
, attributes
, 0);
9112 if (DECL_DECLARED_INLINE_P (decl1
)
9113 && DECL_UNINLINABLE (decl1
)
9114 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1
)))
9115 warning_at (loc
, OPT_Wattributes
,
9116 "inline function %qD given attribute %qs",
9119 /* Handle gnu_inline attribute. */
9120 if (declspecs
->inline_p
9121 && !flag_gnu89_inline
9122 && TREE_CODE (decl1
) == FUNCTION_DECL
9123 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1
))
9124 || current_function_decl
))
9126 if (declspecs
->storage_class
!= csc_static
)
9127 DECL_EXTERNAL (decl1
) = !DECL_EXTERNAL (decl1
);
9130 announce_function (decl1
);
9132 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1
))))
9134 error_at (loc
, "return type is an incomplete type");
9135 /* Make it return void instead. */
9137 = build_function_type (void_type_node
,
9138 TYPE_ARG_TYPES (TREE_TYPE (decl1
)));
9141 if (warn_about_return_type
)
9142 warn_defaults_to (loc
, flag_isoc99
? OPT_Wimplicit_int
9143 : (warn_return_type
> 0 ? OPT_Wreturn_type
9144 : OPT_Wimplicit_int
),
9145 "return type defaults to %<int%>");
9147 /* Make the init_value nonzero so pushdecl knows this is not tentative.
9148 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
9149 DECL_INITIAL (decl1
) = error_mark_node
;
9151 /* If this definition isn't a prototype and we had a prototype declaration
9152 before, copy the arg type info from that prototype. */
9153 old_decl
= lookup_name_in_scope (DECL_NAME (decl1
), current_scope
);
9154 if (old_decl
&& TREE_CODE (old_decl
) != FUNCTION_DECL
)
9155 old_decl
= NULL_TREE
;
9156 current_function_prototype_locus
= UNKNOWN_LOCATION
;
9157 current_function_prototype_built_in
= false;
9158 current_function_prototype_arg_types
= NULL_TREE
;
9159 if (!prototype_p (TREE_TYPE (decl1
)))
9161 if (old_decl
!= NULL_TREE
9162 && TREE_CODE (TREE_TYPE (old_decl
)) == FUNCTION_TYPE
9163 && comptypes (TREE_TYPE (TREE_TYPE (decl1
)),
9164 TREE_TYPE (TREE_TYPE (old_decl
))))
9166 if (stdarg_p (TREE_TYPE (old_decl
)))
9168 auto_diagnostic_group d
;
9169 warning_at (loc
, 0, "%q+D defined as variadic function "
9170 "without prototype", decl1
);
9171 locate_old_decl (old_decl
);
9173 TREE_TYPE (decl1
) = composite_type (TREE_TYPE (old_decl
),
9175 current_function_prototype_locus
= DECL_SOURCE_LOCATION (old_decl
);
9176 current_function_prototype_built_in
9177 = C_DECL_BUILTIN_PROTOTYPE (old_decl
);
9178 current_function_prototype_arg_types
9179 = TYPE_ARG_TYPES (TREE_TYPE (decl1
));
9181 if (TREE_PUBLIC (decl1
))
9183 /* If there is an external prototype declaration of this
9184 function, record its location but do not copy information
9185 to this decl. This may be an invisible declaration
9186 (built-in or in a scope which has finished) or simply
9187 have more refined argument types than any declaration
9189 struct c_binding
*b
;
9190 for (b
= I_SYMBOL_BINDING (DECL_NAME (decl1
)); b
; b
= b
->shadowed
)
9191 if (B_IN_SCOPE (b
, external_scope
))
9195 tree ext_decl
, ext_type
;
9197 ext_type
= b
->u
.type
? b
->u
.type
: TREE_TYPE (ext_decl
);
9198 if (TREE_CODE (ext_type
) == FUNCTION_TYPE
9199 && comptypes (TREE_TYPE (TREE_TYPE (decl1
)),
9200 TREE_TYPE (ext_type
)))
9202 current_function_prototype_locus
9203 = DECL_SOURCE_LOCATION (ext_decl
);
9204 current_function_prototype_built_in
9205 = C_DECL_BUILTIN_PROTOTYPE (ext_decl
);
9206 current_function_prototype_arg_types
9207 = TYPE_ARG_TYPES (ext_type
);
9213 /* Optionally warn of old-fashioned def with no previous prototype. */
9214 if (warn_strict_prototypes
9215 && old_decl
!= error_mark_node
9216 && !prototype_p (TREE_TYPE (decl1
))
9217 && C_DECL_ISNT_PROTOTYPE (old_decl
))
9218 warning_at (loc
, OPT_Wstrict_prototypes
,
9219 "function declaration isn%'t a prototype");
9220 /* Optionally warn of any global def with no previous prototype. */
9221 else if (warn_missing_prototypes
9222 && old_decl
!= error_mark_node
9223 && TREE_PUBLIC (decl1
)
9224 && !MAIN_NAME_P (DECL_NAME (decl1
))
9225 && C_DECL_ISNT_PROTOTYPE (old_decl
)
9226 && !DECL_DECLARED_INLINE_P (decl1
))
9227 warning_at (loc
, OPT_Wmissing_prototypes
,
9228 "no previous prototype for %qD", decl1
);
9229 /* Optionally warn of any def with no previous prototype
9230 if the function has already been used. */
9231 else if (warn_missing_prototypes
9232 && old_decl
!= NULL_TREE
9233 && old_decl
!= error_mark_node
9234 && TREE_USED (old_decl
)
9235 && !prototype_p (TREE_TYPE (old_decl
)))
9236 warning_at (loc
, OPT_Wmissing_prototypes
,
9237 "%qD was used with no prototype before its definition", decl1
);
9238 /* Optionally warn of any global def with no previous declaration. */
9239 else if (warn_missing_declarations
9240 && TREE_PUBLIC (decl1
)
9241 && old_decl
== NULL_TREE
9242 && !MAIN_NAME_P (DECL_NAME (decl1
))
9243 && !DECL_DECLARED_INLINE_P (decl1
))
9244 warning_at (loc
, OPT_Wmissing_declarations
,
9245 "no previous declaration for %qD",
9247 /* Optionally warn of any def with no previous declaration
9248 if the function has already been used. */
9249 else if (warn_missing_declarations
9250 && old_decl
!= NULL_TREE
9251 && old_decl
!= error_mark_node
9252 && TREE_USED (old_decl
)
9253 && C_DECL_IMPLICIT (old_decl
))
9254 warning_at (loc
, OPT_Wmissing_declarations
,
9255 "%qD was used with no declaration before its definition", decl1
);
9257 /* This function exists in static storage.
9258 (This does not mean `static' in the C sense!) */
9259 TREE_STATIC (decl1
) = 1;
9261 /* This is the earliest point at which we might know the assembler
9262 name of the function. Thus, if it's set before this, die horribly. */
9263 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1
));
9265 /* If #pragma weak was used, mark the decl weak now. */
9266 if (current_scope
== file_scope
)
9267 maybe_apply_pragma_weak (decl1
);
9269 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
9270 if (warn_main
&& MAIN_NAME_P (DECL_NAME (decl1
)))
9272 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1
)))
9273 != integer_type_node
)
9274 pedwarn (loc
, OPT_Wmain
, "return type of %qD is not %<int%>", decl1
);
9275 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1
))))
9276 pedwarn (loc
, OPT_Wmain
, "%<_Atomic%>-qualified return type of %qD",
9279 check_main_parameter_types (decl1
);
9281 if (!TREE_PUBLIC (decl1
))
9282 pedwarn (loc
, OPT_Wmain
,
9283 "%qD is normally a non-static function", decl1
);
9286 /* Record the decl so that the function name is defined.
9287 If we already have a decl for this name, and it is a FUNCTION_DECL,
9288 use the old decl. */
9290 current_function_decl
= pushdecl (decl1
);
9293 declare_parm_level ();
9295 restype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
9296 resdecl
= build_decl (loc
, RESULT_DECL
, NULL_TREE
, restype
);
9297 DECL_ARTIFICIAL (resdecl
) = 1;
9298 DECL_IGNORED_P (resdecl
) = 1;
9299 DECL_RESULT (current_function_decl
) = resdecl
;
9301 start_fname_decls ();
9306 /* Subroutine of store_parm_decls which handles new-style function
9307 definitions (prototype format). The parms already have decls, so we
9308 need only record them as in effect and complain if any redundant
9309 old-style parm decls were written. */
9311 store_parm_decls_newstyle (tree fndecl
, const struct c_arg_info
*arg_info
)
9317 if (current_scope
->bindings
)
9319 error_at (DECL_SOURCE_LOCATION (fndecl
),
9320 "old-style parameter declarations in prototyped "
9321 "function definition");
9323 /* Get rid of the old-style declarations. */
9327 /* Don't issue this warning for nested functions, and don't issue this
9328 warning if we got here because ARG_INFO_TYPES was error_mark_node
9329 (this happens when a function definition has just an ellipsis in
9330 its parameter list). */
9331 else if (!in_system_header_at (input_location
)
9332 && !current_function_scope
9333 && arg_info
->types
!= error_mark_node
)
9334 warning_at (DECL_SOURCE_LOCATION (fndecl
), OPT_Wtraditional
,
9335 "traditional C rejects ISO C style function definitions");
9337 /* Now make all the parameter declarations visible in the function body.
9338 We can bypass most of the grunt work of pushdecl. */
9339 for (decl
= arg_info
->parms
; decl
; decl
= DECL_CHAIN (decl
))
9341 DECL_CONTEXT (decl
) = current_function_decl
;
9342 if (DECL_NAME (decl
))
9344 bind (DECL_NAME (decl
), decl
, current_scope
,
9345 /*invisible=*/false, /*nested=*/false,
9347 if (!TREE_USED (decl
))
9348 warn_if_shadowing (decl
);
9351 error_at (DECL_SOURCE_LOCATION (decl
), "parameter name omitted");
9354 /* Record the parameter list in the function declaration. */
9355 DECL_ARGUMENTS (fndecl
) = arg_info
->parms
;
9357 /* Now make all the ancillary declarations visible, likewise. */
9358 for (decl
= arg_info
->others
; decl
; decl
= DECL_CHAIN (decl
))
9360 DECL_CONTEXT (decl
) = current_function_decl
;
9361 if (DECL_NAME (decl
))
9362 bind (DECL_NAME (decl
), decl
, current_scope
,
9363 /*invisible=*/false,
9364 /*nested=*/(TREE_CODE (decl
) == FUNCTION_DECL
),
9368 /* And all the tag declarations. */
9369 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info
->tags
, ix
, tag
)
9371 bind (tag
->id
, tag
->type
, current_scope
,
9372 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
9375 /* Subroutine of store_parm_decls which handles old-style function
9376 definitions (separate parameter list and declarations). */
9379 store_parm_decls_oldstyle (tree fndecl
, const struct c_arg_info
*arg_info
)
9381 struct c_binding
*b
;
9382 tree parm
, decl
, last
;
9383 tree parmids
= arg_info
->parms
;
9384 hash_set
<tree
> seen_args
;
9386 if (!in_system_header_at (input_location
))
9389 pedwarn (DECL_SOURCE_LOCATION (fndecl
),
9390 OPT_Wold_style_definition
, "old-style function definition");
9392 warning_at (DECL_SOURCE_LOCATION (fndecl
),
9393 OPT_Wold_style_definition
,
9394 "old-style function definition");
9397 if (current_scope
->had_vla_unspec
)
9398 error ("%<[*]%> not allowed in other than function prototype scope");
9400 /* Match each formal parameter name with its declaration. Save each
9401 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
9402 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
9404 if (TREE_VALUE (parm
) == NULL_TREE
)
9406 error_at (DECL_SOURCE_LOCATION (fndecl
),
9407 "parameter name missing from parameter list");
9408 TREE_PURPOSE (parm
) = NULL_TREE
;
9412 b
= I_SYMBOL_BINDING (TREE_VALUE (parm
));
9413 if (b
&& B_IN_CURRENT_SCOPE (b
))
9416 /* Skip erroneous parameters. */
9417 if (decl
== error_mark_node
)
9419 /* If we got something other than a PARM_DECL it is an error. */
9420 if (TREE_CODE (decl
) != PARM_DECL
)
9422 error_at (DECL_SOURCE_LOCATION (decl
),
9423 "%qD declared as a non-parameter", decl
);
9426 /* If the declaration is already marked, we have a duplicate
9427 name. Complain and ignore the duplicate. */
9428 else if (seen_args
.contains (decl
))
9430 error_at (DECL_SOURCE_LOCATION (decl
),
9431 "multiple parameters named %qD", decl
);
9432 TREE_PURPOSE (parm
) = NULL_TREE
;
9435 /* If the declaration says "void", complain and turn it into
9437 else if (VOID_TYPE_P (TREE_TYPE (decl
)))
9439 error_at (DECL_SOURCE_LOCATION (decl
),
9440 "parameter %qD declared with void type", decl
);
9441 TREE_TYPE (decl
) = integer_type_node
;
9442 DECL_ARG_TYPE (decl
) = integer_type_node
;
9443 layout_decl (decl
, 0);
9445 warn_if_shadowing (decl
);
9447 /* If no declaration found, default to int. */
9450 /* FIXME diagnostics: This should be the location of the argument,
9451 not the FNDECL. E.g., for an old-style declaration
9453 int f10(v) { blah; }
9455 We should use the location of the V, not the F10.
9456 Unfortunately, the V is an IDENTIFIER_NODE which has no
9457 location. In the future we need locations for c_arg_info
9460 See gcc.dg/Wshadow-3.c for an example of this problem. */
9461 decl
= build_decl (DECL_SOURCE_LOCATION (fndecl
),
9462 PARM_DECL
, TREE_VALUE (parm
), integer_type_node
);
9463 DECL_ARG_TYPE (decl
) = TREE_TYPE (decl
);
9465 warn_if_shadowing (decl
);
9468 pedwarn (DECL_SOURCE_LOCATION (decl
),
9469 OPT_Wimplicit_int
, "type of %qD defaults to %<int%>",
9472 warning_at (DECL_SOURCE_LOCATION (decl
),
9473 OPT_Wmissing_parameter_type
,
9474 "type of %qD defaults to %<int%>", decl
);
9477 TREE_PURPOSE (parm
) = decl
;
9478 seen_args
.add (decl
);
9481 /* Now examine the parms chain for incomplete declarations
9482 and declarations with no corresponding names. */
9484 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
9487 if (TREE_CODE (parm
) != PARM_DECL
)
9490 if (TREE_TYPE (parm
) != error_mark_node
9491 && !COMPLETE_TYPE_P (TREE_TYPE (parm
)))
9493 error_at (DECL_SOURCE_LOCATION (parm
),
9494 "parameter %qD has incomplete type", parm
);
9495 TREE_TYPE (parm
) = error_mark_node
;
9498 if (!seen_args
.contains (parm
))
9500 error_at (DECL_SOURCE_LOCATION (parm
),
9501 "declaration for parameter %qD but no such parameter",
9504 /* Pretend the parameter was not missing.
9505 This gets us to a standard state and minimizes
9506 further error messages. */
9507 parmids
= chainon (parmids
, tree_cons (parm
, 0, 0));
9511 /* Chain the declarations together in the order of the list of
9512 names. Store that chain in the function decl, replacing the
9513 list of names. Update the current scope to match. */
9514 DECL_ARGUMENTS (fndecl
) = NULL_TREE
;
9516 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
9517 if (TREE_PURPOSE (parm
))
9519 if (parm
&& TREE_PURPOSE (parm
))
9521 last
= TREE_PURPOSE (parm
);
9522 DECL_ARGUMENTS (fndecl
) = last
;
9524 for (parm
= TREE_CHAIN (parm
); parm
; parm
= TREE_CHAIN (parm
))
9525 if (TREE_PURPOSE (parm
))
9527 DECL_CHAIN (last
) = TREE_PURPOSE (parm
);
9528 last
= TREE_PURPOSE (parm
);
9530 DECL_CHAIN (last
) = NULL_TREE
;
9533 /* If there was a previous prototype,
9534 set the DECL_ARG_TYPE of each argument according to
9535 the type previously specified, and report any mismatches. */
9537 if (current_function_prototype_arg_types
)
9540 for (parm
= DECL_ARGUMENTS (fndecl
),
9541 type
= current_function_prototype_arg_types
;
9542 parm
|| (type
!= NULL_TREE
9543 && TREE_VALUE (type
) != error_mark_node
9544 && TYPE_MAIN_VARIANT (TREE_VALUE (type
)) != void_type_node
);
9545 parm
= DECL_CHAIN (parm
), type
= TREE_CHAIN (type
))
9547 if (parm
== NULL_TREE
9548 || type
== NULL_TREE
9549 || (TREE_VALUE (type
) != error_mark_node
9550 && TYPE_MAIN_VARIANT (TREE_VALUE (type
)) == void_type_node
))
9552 if (current_function_prototype_built_in
)
9553 warning_at (DECL_SOURCE_LOCATION (fndecl
),
9554 0, "number of arguments doesn%'t match "
9555 "built-in prototype");
9558 /* FIXME diagnostics: This should be the location of
9559 FNDECL, but there is bug when a prototype is
9560 declared inside function context, but defined
9561 outside of it (e.g., gcc.dg/pr15698-2.c). In
9562 which case FNDECL gets the location of the
9563 prototype, not the definition. */
9564 error_at (input_location
,
9565 "number of arguments doesn%'t match prototype");
9567 error_at (current_function_prototype_locus
,
9568 "prototype declaration");
9572 /* Type for passing arg must be consistent with that
9573 declared for the arg. ISO C says we take the unqualified
9574 type for parameters declared with qualified type. */
9575 if (TREE_TYPE (parm
) != error_mark_node
9576 && TREE_VALUE (type
) != error_mark_node
9577 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm
))
9578 != TYPE_ATOMIC (TREE_VALUE (type
)))
9579 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm
)),
9580 TYPE_MAIN_VARIANT (TREE_VALUE (type
)))))
9582 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm
))
9583 == TYPE_ATOMIC (TREE_VALUE (type
)))
9584 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm
))
9585 == TYPE_MAIN_VARIANT (TREE_VALUE (type
))))
9587 /* Adjust argument to match prototype. E.g. a previous
9588 `int foo(float);' prototype causes
9589 `int foo(x) float x; {...}' to be treated like
9590 `int foo(float x) {...}'. This is particularly
9591 useful for argument types like uid_t. */
9592 DECL_ARG_TYPE (parm
) = TREE_TYPE (parm
);
9594 if (targetm
.calls
.promote_prototypes (TREE_TYPE (current_function_decl
))
9595 && INTEGRAL_TYPE_P (TREE_TYPE (parm
))
9596 && (TYPE_PRECISION (TREE_TYPE (parm
))
9597 < TYPE_PRECISION (integer_type_node
)))
9598 DECL_ARG_TYPE (parm
)
9599 = c_type_promotes_to (TREE_TYPE (parm
));
9601 /* ??? Is it possible to get here with a
9602 built-in prototype or will it always have
9603 been diagnosed as conflicting with an
9604 old-style definition and discarded? */
9605 if (current_function_prototype_built_in
)
9606 warning_at (DECL_SOURCE_LOCATION (parm
),
9607 OPT_Wpedantic
, "promoted argument %qD "
9608 "doesn%'t match built-in prototype", parm
);
9611 pedwarn (DECL_SOURCE_LOCATION (parm
),
9612 OPT_Wpedantic
, "promoted argument %qD "
9613 "doesn%'t match prototype", parm
);
9614 pedwarn (current_function_prototype_locus
, OPT_Wpedantic
,
9615 "prototype declaration");
9620 if (current_function_prototype_built_in
)
9621 warning_at (DECL_SOURCE_LOCATION (parm
),
9622 0, "argument %qD doesn%'t match "
9623 "built-in prototype", parm
);
9626 error_at (DECL_SOURCE_LOCATION (parm
),
9627 "argument %qD doesn%'t match prototype", parm
);
9628 error_at (current_function_prototype_locus
,
9629 "prototype declaration");
9634 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = NULL_TREE
;
9637 /* Otherwise, create a prototype that would match. */
9641 tree actual
= NULL_TREE
, last
= NULL_TREE
, type
;
9643 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= DECL_CHAIN (parm
))
9645 type
= tree_cons (NULL_TREE
, DECL_ARG_TYPE (parm
), NULL_TREE
);
9647 TREE_CHAIN (last
) = type
;
9652 type
= tree_cons (NULL_TREE
, void_type_node
, NULL_TREE
);
9654 TREE_CHAIN (last
) = type
;
9658 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9659 of the type of this function, but we need to avoid having this
9660 affect the types of other similarly-typed functions, so we must
9661 first force the generation of an identical (but separate) type
9662 node for the relevant function type. The new node we create
9663 will be a variant of the main variant of the original function
9666 TREE_TYPE (fndecl
) = build_variant_type_copy (TREE_TYPE (fndecl
));
9668 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = actual
;
9672 /* Store parameter declarations passed in ARG_INFO into the current
9673 function declaration. */
9676 store_parm_decls_from (struct c_arg_info
*arg_info
)
9678 current_function_arg_info
= arg_info
;
9679 store_parm_decls ();
9682 /* Called by walk_tree to look for and update context-less labels. */
9685 set_labels_context_r (tree
*tp
, int *walk_subtrees
, void *data
)
9687 if (TREE_CODE (*tp
) == LABEL_EXPR
9688 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp
)) == NULL_TREE
)
9690 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp
)) = static_cast<tree
>(data
);
9697 /* Store the parameter declarations into the current function declaration.
9698 This is called after parsing the parameter declarations, before
9699 digesting the body of the function.
9701 For an old-style definition, construct a prototype out of the old-style
9702 parameter declarations and inject it into the function's type. */
9705 store_parm_decls (void)
9707 tree fndecl
= current_function_decl
;
9710 /* The argument information block for FNDECL. */
9711 struct c_arg_info
*arg_info
= current_function_arg_info
;
9712 current_function_arg_info
= 0;
9714 /* True if this definition is written with a prototype. In C2X, an
9715 empty argument list was converted to (void) in grokparms; in
9716 older C standard versions, it does not give the function a type
9717 with a prototype for future calls. */
9718 proto
= arg_info
->types
!= 0;
9721 store_parm_decls_newstyle (fndecl
, arg_info
);
9723 store_parm_decls_oldstyle (fndecl
, arg_info
);
9725 /* The next call to push_scope will be a function body. */
9727 next_is_function_body
= true;
9729 /* Write a record describing this function definition to the prototypes
9730 file (if requested). */
9732 gen_aux_info_record (fndecl
, 1, 0, proto
);
9734 /* Initialize the RTL code for the function. */
9735 allocate_struct_function (fndecl
, false);
9737 if (warn_unused_local_typedefs
)
9738 cfun
->language
= ggc_cleared_alloc
<language_function
> ();
9740 /* Begin the statement tree for this function. */
9741 DECL_SAVED_TREE (fndecl
) = push_stmt_list ();
9743 /* ??? Insert the contents of the pending sizes list into the function
9744 to be evaluated. The only reason left to have this is
9745 void foo(int n, int array[n++])
9746 because we throw away the array type in favor of a pointer type, and
9747 thus won't naturally see the SAVE_EXPR containing the increment. All
9748 other pending sizes would be handled by gimplify_parameters. */
9749 if (arg_info
->pending_sizes
)
9751 /* In very special circumstances, e.g. for code like
9753 void f (int a[i += 2]) {}
9754 we need to execute the atomic assignment on function entry.
9755 But in this case, it is not just a straight store, it has the
9756 op= form, which means that build_atomic_assign has generated
9757 gotos, labels, etc. Because at that time the function decl
9758 for F has not been created yet, those labels do not have any
9759 function context. But we have the fndecl now, so update the
9760 labels accordingly. gimplify_expr would crash otherwise. */
9761 walk_tree_without_duplicates (&arg_info
->pending_sizes
,
9762 set_labels_context_r
, fndecl
);
9763 add_stmt (arg_info
->pending_sizes
);
9767 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9768 c_finish_omp_declare_simd for function prototypes. No diagnostics
9772 temp_store_parm_decls (tree fndecl
, tree parms
)
9775 for (tree p
= parms
; p
; p
= DECL_CHAIN (p
))
9777 DECL_CONTEXT (p
) = fndecl
;
9779 bind (DECL_NAME (p
), p
, current_scope
,
9780 /*invisible=*/false, /*nested=*/false,
9785 /* Undo what temp_store_parm_decls did. */
9788 temp_pop_parm_decls (void)
9790 /* Clear all bindings in this temporary scope, so that
9791 pop_scope doesn't create a BLOCK. */
9792 struct c_binding
*b
= current_scope
->bindings
;
9793 current_scope
->bindings
= NULL
;
9794 for (; b
; b
= free_binding_and_advance (b
))
9796 gcc_assert (TREE_CODE (b
->decl
) == PARM_DECL
9797 || b
->decl
== error_mark_node
);
9798 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
9799 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
9800 if (b
->shadowed
&& b
->shadowed
->u
.type
)
9801 TREE_TYPE (b
->shadowed
->decl
) = b
->shadowed
->u
.type
;
9807 /* Finish up a function declaration and compile that function
9808 all the way to assembler language output. Then free the storage
9809 for the function definition.
9811 This is called after parsing the body of the function definition. */
9814 finish_function (void)
9816 tree fndecl
= current_function_decl
;
9818 if (c_dialect_objc ())
9819 objc_finish_function ();
9821 if (TREE_CODE (fndecl
) == FUNCTION_DECL
9822 && targetm
.calls
.promote_prototypes (TREE_TYPE (fndecl
)))
9824 tree args
= DECL_ARGUMENTS (fndecl
);
9825 for (; args
; args
= DECL_CHAIN (args
))
9827 tree type
= TREE_TYPE (args
);
9828 if (INTEGRAL_TYPE_P (type
)
9829 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
9830 DECL_ARG_TYPE (args
) = c_type_promotes_to (type
);
9834 if (DECL_INITIAL (fndecl
) && DECL_INITIAL (fndecl
) != error_mark_node
)
9835 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
9837 /* Must mark the RESULT_DECL as being in this function. */
9839 if (DECL_RESULT (fndecl
) && DECL_RESULT (fndecl
) != error_mark_node
)
9840 DECL_CONTEXT (DECL_RESULT (fndecl
)) = fndecl
;
9842 if (MAIN_NAME_P (DECL_NAME (fndecl
)) && flag_hosted
9843 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl
)))
9844 == integer_type_node
&& flag_isoc99
)
9846 /* Hack. We don't want the middle-end to warn that this return
9847 is unreachable, so we mark its location as special. Using
9848 UNKNOWN_LOCATION has the problem that it gets clobbered in
9849 annotate_one_with_locus. A cleaner solution might be to
9850 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9852 c_finish_return (BUILTINS_LOCATION
, integer_zero_node
, NULL_TREE
);
9855 /* Tie off the statement tree for this function. */
9856 DECL_SAVED_TREE (fndecl
) = pop_stmt_list (DECL_SAVED_TREE (fndecl
));
9858 finish_fname_decls ();
9860 /* Complain if there's no return statement only if option specified on
9862 if (warn_return_type
> 0
9863 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl
))) != VOID_TYPE
9864 && !current_function_returns_value
&& !current_function_returns_null
9865 /* Don't complain if we are no-return. */
9866 && !current_function_returns_abnormally
9867 /* Don't complain if we are declared noreturn. */
9868 && !TREE_THIS_VOLATILE (fndecl
)
9869 /* Don't warn for main(). */
9870 && !MAIN_NAME_P (DECL_NAME (fndecl
))
9871 /* Or if they didn't actually specify a return type. */
9872 && !C_FUNCTION_IMPLICIT_INT (fndecl
)
9873 /* Normally, with -Wreturn-type, flow will complain, but we might
9874 optimize out static functions. */
9875 && !TREE_PUBLIC (fndecl
)
9876 && targetm
.warn_func_return (fndecl
)
9877 && warning (OPT_Wreturn_type
,
9878 "no return statement in function returning non-void"))
9879 TREE_NO_WARNING (fndecl
) = 1;
9881 /* Complain about parameters that are only set, but never otherwise used. */
9882 if (warn_unused_but_set_parameter
)
9886 for (decl
= DECL_ARGUMENTS (fndecl
);
9888 decl
= DECL_CHAIN (decl
))
9889 if (TREE_USED (decl
)
9890 && TREE_CODE (decl
) == PARM_DECL
9891 && !DECL_READ_P (decl
)
9893 && !DECL_ARTIFICIAL (decl
)
9894 && !TREE_NO_WARNING (decl
))
9895 warning_at (DECL_SOURCE_LOCATION (decl
),
9896 OPT_Wunused_but_set_parameter
,
9897 "parameter %qD set but not used", decl
);
9900 /* Complain about locally defined typedefs that are not used in this
9902 maybe_warn_unused_local_typedefs ();
9904 /* Possibly warn about unused parameters. */
9905 if (warn_unused_parameter
)
9906 do_warn_unused_parameter (fndecl
);
9908 /* Store the end of the function, so that we get good line number
9909 info for the epilogue. */
9910 cfun
->function_end_locus
= input_location
;
9912 /* Finalize the ELF visibility for the function. */
9913 c_determine_visibility (fndecl
);
9915 /* For GNU C extern inline functions disregard inline limits. */
9916 if (DECL_EXTERNAL (fndecl
)
9917 && DECL_DECLARED_INLINE_P (fndecl
)
9918 && (flag_gnu89_inline
9919 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl
))))
9920 DECL_DISREGARD_INLINE_LIMITS (fndecl
) = 1;
9922 /* Genericize before inlining. Delay genericizing nested functions
9923 until their parent function is genericized. Since finalizing
9924 requires GENERIC, delay that as well. */
9926 if (DECL_INITIAL (fndecl
) && DECL_INITIAL (fndecl
) != error_mark_node
9927 && !undef_nested_function
)
9929 if (!decl_function_context (fndecl
))
9931 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE
, fndecl
);
9932 c_genericize (fndecl
);
9934 /* ??? Objc emits functions after finalizing the compilation unit.
9935 This should be cleaned up later and this conditional removed. */
9936 if (symtab
->global_info_ready
)
9938 cgraph_node::add_new_function (fndecl
, false);
9941 cgraph_node::finalize_function (fndecl
, false);
9945 /* Register this function with cgraph just far enough to get it
9946 added to our parent's nested function list. Handy, since the
9947 C front end doesn't have such a list. */
9948 (void) cgraph_node::get_create (fndecl
);
9952 if (!decl_function_context (fndecl
))
9953 undef_nested_function
= false;
9955 if (cfun
->language
!= NULL
)
9957 ggc_free (cfun
->language
);
9958 cfun
->language
= NULL
;
9961 /* We're leaving the context of this function, so zap cfun.
9962 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9963 tree_rest_of_compilation. */
9965 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION
, current_function_decl
);
9966 current_function_decl
= NULL
;
9969 /* Check the declarations given in a for-loop for satisfying the C99
9970 constraints. If exactly one such decl is found, return it. LOC is
9971 the location of the opening parenthesis of the for loop. The last
9972 parameter allows you to control the "for loop initial declarations
9973 are only allowed in C99 mode". Normally, you should pass
9974 flag_isoc99 as that parameter. But in some cases (Objective-C
9975 foreach loop, for example) we want to run the checks in this
9976 function even if not in C99 mode, so we allow the caller to turn
9977 off the error about not being in C99 mode.
9981 check_for_loop_decls (location_t loc
, bool turn_off_iso_c99_error
)
9983 struct c_binding
*b
;
9984 tree one_decl
= NULL_TREE
;
9987 if (!turn_off_iso_c99_error
)
9989 static bool hint
= true;
9990 /* If we get here, declarations have been used in a for loop without
9991 the C99 for loop scope. This doesn't make much sense, so don't
9993 error_at (loc
, "%<for%> loop initial declarations "
9994 "are only allowed in C99 or C11 mode");
9998 "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
9999 "%<-std=gnu11%> to compile your code");
10005 pedwarn_c90 (loc
, OPT_Wpedantic
, "ISO C90 does not support %<for%> loop "
10006 "initial declarations");
10008 /* C99 subclause 6.8.5 paragraph 3:
10010 [#3] The declaration part of a for statement shall only
10011 declare identifiers for objects having storage class auto or
10014 It isn't clear whether, in this sentence, "identifiers" binds to
10015 "shall only declare" or to "objects" - that is, whether all identifiers
10016 declared must be identifiers for objects, or whether the restriction
10017 only applies to those that are. (A question on this in comp.std.c
10018 in November 2000 received no answer.) We implement the strictest
10019 interpretation, to avoid creating an extension which later causes
10022 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
10025 tree decl
= b
->decl
;
10030 switch (TREE_CODE (decl
))
10034 location_t decl_loc
= DECL_SOURCE_LOCATION (decl
);
10035 if (TREE_STATIC (decl
))
10036 error_at (decl_loc
,
10037 "declaration of static variable %qD in %<for%> loop "
10038 "initial declaration", decl
);
10039 else if (DECL_EXTERNAL (decl
))
10040 error_at (decl_loc
,
10041 "declaration of %<extern%> variable %qD in %<for%> loop "
10042 "initial declaration", decl
);
10048 "%<struct %E%> declared in %<for%> loop initial "
10049 "declaration", id
);
10053 "%<union %E%> declared in %<for%> loop initial declaration",
10056 case ENUMERAL_TYPE
:
10057 error_at (loc
, "%<enum %E%> declared in %<for%> loop "
10058 "initial declaration", id
);
10061 error_at (loc
, "declaration of non-variable "
10062 "%qD in %<for%> loop initial declaration", decl
);
10069 return n_decls
== 1 ? one_decl
: NULL_TREE
;
10072 /* Save and reinitialize the variables
10073 used during compilation of a C function. */
10076 c_push_function_context (void)
10078 struct language_function
*p
= cfun
->language
;
10079 /* cfun->language might have been already allocated by the use of
10080 -Wunused-local-typedefs. In that case, just re-use it. */
10082 cfun
->language
= p
= ggc_cleared_alloc
<language_function
> ();
10084 p
->base
.x_stmt_tree
= c_stmt_tree
;
10085 c_stmt_tree
.x_cur_stmt_list
= vec_safe_copy (c_stmt_tree
.x_cur_stmt_list
);
10086 p
->x_break_label
= c_break_label
;
10087 p
->x_cont_label
= c_cont_label
;
10088 p
->x_switch_stack
= c_switch_stack
;
10089 p
->arg_info
= current_function_arg_info
;
10090 p
->returns_value
= current_function_returns_value
;
10091 p
->returns_null
= current_function_returns_null
;
10092 p
->returns_abnormally
= current_function_returns_abnormally
;
10093 p
->warn_about_return_type
= warn_about_return_type
;
10095 push_function_context ();
10098 /* Restore the variables used during compilation of a C function. */
10101 c_pop_function_context (void)
10103 struct language_function
*p
;
10105 pop_function_context ();
10106 p
= cfun
->language
;
10108 /* When -Wunused-local-typedefs is in effect, cfun->languages is
10109 used to store data throughout the life time of the current cfun,
10110 So don't deallocate it. */
10111 if (!warn_unused_local_typedefs
)
10112 cfun
->language
= NULL
;
10114 if (DECL_STRUCT_FUNCTION (current_function_decl
) == 0
10115 && DECL_SAVED_TREE (current_function_decl
) == NULL_TREE
)
10117 /* Stop pointing to the local nodes about to be freed. */
10118 /* But DECL_INITIAL must remain nonzero so we know this
10119 was an actual function definition. */
10120 DECL_INITIAL (current_function_decl
) = error_mark_node
;
10121 DECL_ARGUMENTS (current_function_decl
) = NULL_TREE
;
10124 c_stmt_tree
= p
->base
.x_stmt_tree
;
10125 p
->base
.x_stmt_tree
.x_cur_stmt_list
= NULL
;
10126 c_break_label
= p
->x_break_label
;
10127 c_cont_label
= p
->x_cont_label
;
10128 c_switch_stack
= p
->x_switch_stack
;
10129 current_function_arg_info
= p
->arg_info
;
10130 current_function_returns_value
= p
->returns_value
;
10131 current_function_returns_null
= p
->returns_null
;
10132 current_function_returns_abnormally
= p
->returns_abnormally
;
10133 warn_about_return_type
= p
->warn_about_return_type
;
10136 /* The functions below are required for functionality of doing
10137 function at once processing in the C front end. Currently these
10138 functions are not called from anywhere in the C front end, but as
10139 these changes continue, that will change. */
10141 /* Returns the stmt_tree (if any) to which statements are currently
10142 being added. If there is no active statement-tree, NULL is
10146 current_stmt_tree (void)
10148 return &c_stmt_tree
;
10151 /* Return the global value of T as a symbol. */
10154 identifier_global_value (tree t
)
10156 struct c_binding
*b
;
10158 for (b
= I_SYMBOL_BINDING (t
); b
; b
= b
->shadowed
)
10159 if (B_IN_FILE_SCOPE (b
) || B_IN_EXTERNAL_SCOPE (b
))
10165 /* Return the global value of tag T as a symbol. */
10168 identifier_global_tag (tree t
)
10170 struct c_binding
*b
;
10172 for (b
= I_TAG_BINDING (t
); b
; b
= b
->shadowed
)
10173 if (B_IN_FILE_SCOPE (b
) || B_IN_EXTERNAL_SCOPE (b
))
10179 /* Returns true if NAME refers to a built-in function or function-like
10183 names_builtin_p (const char *name
)
10185 tree id
= get_identifier (name
);
10186 if (tree decl
= identifier_global_value (id
))
10187 return TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_IS_BUILTIN (decl
);
10189 /* Also detect common reserved C words that aren't strictly built-in
10191 switch (C_RID_CODE (id
))
10193 case RID_BUILTIN_CONVERTVECTOR
:
10194 case RID_BUILTIN_HAS_ATTRIBUTE
:
10195 case RID_BUILTIN_SHUFFLE
:
10196 case RID_CHOOSE_EXPR
:
10198 case RID_TYPES_COMPATIBLE_P
:
10207 /* In C, the only C-linkage public declaration is at file scope. */
10210 c_linkage_bindings (tree name
)
10212 return identifier_global_value (name
);
10215 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
10216 otherwise the name is found in ridpointers from RID_INDEX. */
10219 record_builtin_type (enum rid rid_index
, const char *name
, tree type
)
10223 id
= ridpointers
[(int) rid_index
];
10225 id
= get_identifier (name
);
10226 decl
= build_decl (UNKNOWN_LOCATION
, TYPE_DECL
, id
, type
);
10228 if (debug_hooks
->type_decl
)
10229 debug_hooks
->type_decl (decl
, false);
10232 /* Build the void_list_node (void_type_node having been created). */
10234 build_void_list_node (void)
10236 tree t
= build_tree_list (NULL_TREE
, void_type_node
);
10240 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
10243 build_c_parm (struct c_declspecs
*specs
, tree attrs
,
10244 struct c_declarator
*declarator
,
10247 struct c_parm
*ret
= XOBNEW (&parser_obstack
, struct c_parm
);
10248 ret
->specs
= specs
;
10249 ret
->attrs
= attrs
;
10250 ret
->declarator
= declarator
;
10255 /* Return a declarator with nested attributes. TARGET is the inner
10256 declarator to which these attributes apply. ATTRS are the
10259 struct c_declarator
*
10260 build_attrs_declarator (tree attrs
, struct c_declarator
*target
)
10262 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
10263 ret
->kind
= cdk_attrs
;
10264 ret
->declarator
= target
;
10265 ret
->u
.attrs
= attrs
;
10269 /* Return a declarator for a function with arguments specified by ARGS
10270 and return type specified by TARGET. */
10272 struct c_declarator
*
10273 build_function_declarator (struct c_arg_info
*args
,
10274 struct c_declarator
*target
)
10276 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
10277 ret
->kind
= cdk_function
;
10278 ret
->declarator
= target
;
10279 ret
->u
.arg_info
= args
;
10283 /* Return a declarator for the identifier IDENT (which may be
10284 NULL_TREE for an abstract declarator). */
10286 struct c_declarator
*
10287 build_id_declarator (tree ident
)
10289 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
10290 ret
->kind
= cdk_id
;
10291 ret
->declarator
= 0;
10292 ret
->u
.id
.id
= ident
;
10293 ret
->u
.id
.attrs
= NULL_TREE
;
10294 /* Default value - may get reset to a more precise location. */
10295 ret
->id_loc
= input_location
;
10299 /* Return something to represent absolute declarators containing a *.
10300 TARGET is the absolute declarator that the * contains.
10301 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
10302 to apply to the pointer type. */
10304 struct c_declarator
*
10305 make_pointer_declarator (struct c_declspecs
*type_quals_attrs
,
10306 struct c_declarator
*target
)
10310 struct c_declarator
*itarget
= target
;
10311 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
10312 if (type_quals_attrs
)
10314 attrs
= type_quals_attrs
->attrs
;
10315 quals
= quals_from_declspecs (type_quals_attrs
);
10316 if (attrs
!= NULL_TREE
)
10317 itarget
= build_attrs_declarator (attrs
, target
);
10319 ret
->kind
= cdk_pointer
;
10320 ret
->declarator
= itarget
;
10321 ret
->u
.pointer_quals
= quals
;
10325 /* Return a pointer to a structure for an empty list of declaration
10328 struct c_declspecs
*
10329 build_null_declspecs (void)
10331 struct c_declspecs
*ret
= XOBNEW (&parser_obstack
, struct c_declspecs
);
10332 memset (ret
, 0, sizeof *ret
);
10333 ret
->align_log
= -1;
10334 ret
->typespec_word
= cts_none
;
10335 ret
->storage_class
= csc_none
;
10336 ret
->expr_const_operands
= true;
10337 ret
->typespec_kind
= ctsk_none
;
10338 ret
->address_space
= ADDR_SPACE_GENERIC
;
10342 /* Add the address space ADDRSPACE to the declaration specifiers
10343 SPECS, returning SPECS. */
10345 struct c_declspecs
*
10346 declspecs_add_addrspace (location_t location
,
10347 struct c_declspecs
*specs
, addr_space_t as
)
10349 specs
->non_sc_seen_p
= true;
10350 specs
->declspecs_seen_p
= true;
10351 specs
->non_std_attrs_seen_p
= true;
10353 if (!ADDR_SPACE_GENERIC_P (specs
->address_space
)
10354 && specs
->address_space
!= as
)
10355 error ("incompatible address space qualifiers %qs and %qs",
10356 c_addr_space_name (as
),
10357 c_addr_space_name (specs
->address_space
));
10360 specs
->address_space
= as
;
10361 specs
->locations
[cdw_address_space
] = location
;
10366 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
10367 returning SPECS. */
10369 struct c_declspecs
*
10370 declspecs_add_qual (location_t loc
,
10371 struct c_declspecs
*specs
, tree qual
)
10375 specs
->non_sc_seen_p
= true;
10376 specs
->declspecs_seen_p
= true;
10377 specs
->non_std_attrs_seen_p
= true;
10378 gcc_assert (TREE_CODE (qual
) == IDENTIFIER_NODE
10379 && C_IS_RESERVED_WORD (qual
));
10380 i
= C_RID_CODE (qual
);
10381 location_t prev_loc
= UNKNOWN_LOCATION
;
10385 dupe
= specs
->const_p
;
10386 specs
->const_p
= true;
10387 prev_loc
= specs
->locations
[cdw_const
];
10388 specs
->locations
[cdw_const
] = loc
;
10391 dupe
= specs
->volatile_p
;
10392 specs
->volatile_p
= true;
10393 prev_loc
= specs
->locations
[cdw_volatile
];
10394 specs
->locations
[cdw_volatile
] = loc
;
10397 dupe
= specs
->restrict_p
;
10398 specs
->restrict_p
= true;
10399 prev_loc
= specs
->locations
[cdw_restrict
];
10400 specs
->locations
[cdw_restrict
] = loc
;
10403 dupe
= specs
->atomic_p
;
10404 specs
->atomic_p
= true;
10405 prev_loc
= specs
->locations
[cdw_atomic
];
10406 specs
->locations
[cdw_atomic
] = loc
;
10409 gcc_unreachable ();
10413 bool warned
= pedwarn_c90 (loc
, OPT_Wpedantic
,
10414 "duplicate %qE declaration specifier", qual
);
10416 && warn_duplicate_decl_specifier
10417 && prev_loc
>= RESERVED_LOCATION_COUNT
10418 && !from_macro_expansion_at (prev_loc
)
10419 && !from_macro_expansion_at (loc
))
10420 warning_at (loc
, OPT_Wduplicate_decl_specifier
,
10421 "duplicate %qE declaration specifier", qual
);
10426 /* Add the type specifier TYPE to the declaration specifiers SPECS,
10427 returning SPECS. */
10429 struct c_declspecs
*
10430 declspecs_add_type (location_t loc
, struct c_declspecs
*specs
,
10431 struct c_typespec spec
)
10433 tree type
= spec
.spec
;
10434 specs
->non_sc_seen_p
= true;
10435 specs
->declspecs_seen_p
= true;
10436 specs
->non_std_attrs_seen_p
= true;
10437 specs
->typespec_kind
= spec
.kind
;
10438 if (TREE_DEPRECATED (type
))
10439 specs
->deprecated_p
= true;
10441 /* Handle type specifier keywords. */
10442 if (TREE_CODE (type
) == IDENTIFIER_NODE
10443 && C_IS_RESERVED_WORD (type
)
10444 && C_RID_CODE (type
) != RID_CXX_COMPAT_WARN
)
10446 enum rid i
= C_RID_CODE (type
);
10449 error_at (loc
, "two or more data types in declaration specifiers");
10452 if ((int) i
<= (int) RID_LAST_MODIFIER
)
10454 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
10459 if (specs
->long_long_p
)
10461 error_at (loc
, "%<long long long%> is too long for GCC");
10466 if (specs
->typespec_word
== cts_double
)
10469 ("both %<long long%> and %<double%> in "
10470 "declaration specifiers"));
10473 pedwarn_c90 (loc
, OPT_Wlong_long
,
10474 "ISO C90 does not support %<long long%>");
10475 specs
->long_long_p
= 1;
10476 specs
->locations
[cdw_long_long
] = loc
;
10479 if (specs
->short_p
)
10481 ("both %<long%> and %<short%> in "
10482 "declaration specifiers"));
10483 else if (specs
->typespec_word
== cts_auto_type
)
10485 ("both %<long%> and %<__auto_type%> in "
10486 "declaration specifiers"));
10487 else if (specs
->typespec_word
== cts_void
)
10489 ("both %<long%> and %<void%> in "
10490 "declaration specifiers"));
10491 else if (specs
->typespec_word
== cts_int_n
)
10493 ("both %<long%> and %<__int%d%> in "
10494 "declaration specifiers"),
10495 int_n_data
[specs
->int_n_idx
].bitsize
);
10496 else if (specs
->typespec_word
== cts_bool
)
10498 ("both %<long%> and %<_Bool%> in "
10499 "declaration specifiers"));
10500 else if (specs
->typespec_word
== cts_char
)
10502 ("both %<long%> and %<char%> in "
10503 "declaration specifiers"));
10504 else if (specs
->typespec_word
== cts_float
)
10506 ("both %<long%> and %<float%> in "
10507 "declaration specifiers"));
10508 else if (specs
->typespec_word
== cts_floatn_nx
)
10510 ("both %<long%> and %<_Float%d%s%> in "
10511 "declaration specifiers"),
10512 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10513 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10516 else if (specs
->typespec_word
== cts_dfloat32
)
10518 ("both %<long%> and %<_Decimal32%> in "
10519 "declaration specifiers"));
10520 else if (specs
->typespec_word
== cts_dfloat64
)
10522 ("both %<long%> and %<_Decimal64%> in "
10523 "declaration specifiers"));
10524 else if (specs
->typespec_word
== cts_dfloat128
)
10526 ("both %<long%> and %<_Decimal128%> in "
10527 "declaration specifiers"));
10530 specs
->long_p
= true;
10531 specs
->locations
[cdw_long
] = loc
;
10535 dupe
= specs
->short_p
;
10538 ("both %<long%> and %<short%> in "
10539 "declaration specifiers"));
10540 else if (specs
->typespec_word
== cts_auto_type
)
10542 ("both %<short%> and %<__auto_type%> in "
10543 "declaration specifiers"));
10544 else if (specs
->typespec_word
== cts_void
)
10546 ("both %<short%> and %<void%> in "
10547 "declaration specifiers"));
10548 else if (specs
->typespec_word
== cts_int_n
)
10550 ("both %<short%> and %<__int%d%> in "
10551 "declaration specifiers"),
10552 int_n_data
[specs
->int_n_idx
].bitsize
);
10553 else if (specs
->typespec_word
== cts_bool
)
10555 ("both %<short%> and %<_Bool%> in "
10556 "declaration specifiers"));
10557 else if (specs
->typespec_word
== cts_char
)
10559 ("both %<short%> and %<char%> in "
10560 "declaration specifiers"));
10561 else if (specs
->typespec_word
== cts_float
)
10563 ("both %<short%> and %<float%> in "
10564 "declaration specifiers"));
10565 else if (specs
->typespec_word
== cts_double
)
10567 ("both %<short%> and %<double%> in "
10568 "declaration specifiers"));
10569 else if (specs
->typespec_word
== cts_floatn_nx
)
10571 ("both %<short%> and %<_Float%d%s%> in "
10572 "declaration specifiers"),
10573 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10574 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10577 else if (specs
->typespec_word
== cts_dfloat32
)
10579 ("both %<short%> and %<_Decimal32%> in "
10580 "declaration specifiers"));
10581 else if (specs
->typespec_word
== cts_dfloat64
)
10583 ("both %<short%> and %<_Decimal64%> in "
10584 "declaration specifiers"));
10585 else if (specs
->typespec_word
== cts_dfloat128
)
10587 ("both %<short%> and %<_Decimal128%> in "
10588 "declaration specifiers"));
10591 specs
->short_p
= true;
10592 specs
->locations
[cdw_short
] = loc
;
10596 dupe
= specs
->signed_p
;
10597 if (specs
->unsigned_p
)
10599 ("both %<signed%> and %<unsigned%> in "
10600 "declaration specifiers"));
10601 else if (specs
->typespec_word
== cts_auto_type
)
10603 ("both %<signed%> and %<__auto_type%> in "
10604 "declaration specifiers"));
10605 else if (specs
->typespec_word
== cts_void
)
10607 ("both %<signed%> and %<void%> in "
10608 "declaration specifiers"));
10609 else if (specs
->typespec_word
== cts_bool
)
10611 ("both %<signed%> and %<_Bool%> in "
10612 "declaration specifiers"));
10613 else if (specs
->typespec_word
== cts_float
)
10615 ("both %<signed%> and %<float%> in "
10616 "declaration specifiers"));
10617 else if (specs
->typespec_word
== cts_double
)
10619 ("both %<signed%> and %<double%> in "
10620 "declaration specifiers"));
10621 else if (specs
->typespec_word
== cts_floatn_nx
)
10623 ("both %<signed%> and %<_Float%d%s%> in "
10624 "declaration specifiers"),
10625 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10626 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10629 else if (specs
->typespec_word
== cts_dfloat32
)
10631 ("both %<signed%> and %<_Decimal32%> in "
10632 "declaration specifiers"));
10633 else if (specs
->typespec_word
== cts_dfloat64
)
10635 ("both %<signed%> and %<_Decimal64%> in "
10636 "declaration specifiers"));
10637 else if (specs
->typespec_word
== cts_dfloat128
)
10639 ("both %<signed%> and %<_Decimal128%> in "
10640 "declaration specifiers"));
10643 specs
->signed_p
= true;
10644 specs
->locations
[cdw_signed
] = loc
;
10648 dupe
= specs
->unsigned_p
;
10649 if (specs
->signed_p
)
10651 ("both %<signed%> and %<unsigned%> in "
10652 "declaration specifiers"));
10653 else if (specs
->typespec_word
== cts_auto_type
)
10655 ("both %<unsigned%> and %<__auto_type%> in "
10656 "declaration specifiers"));
10657 else if (specs
->typespec_word
== cts_void
)
10659 ("both %<unsigned%> and %<void%> in "
10660 "declaration specifiers"));
10661 else if (specs
->typespec_word
== cts_bool
)
10663 ("both %<unsigned%> and %<_Bool%> in "
10664 "declaration specifiers"));
10665 else if (specs
->typespec_word
== cts_float
)
10667 ("both %<unsigned%> and %<float%> in "
10668 "declaration specifiers"));
10669 else if (specs
->typespec_word
== cts_double
)
10671 ("both %<unsigned%> and %<double%> in "
10672 "declaration specifiers"));
10673 else if (specs
->typespec_word
== cts_floatn_nx
)
10675 ("both %<unsigned%> and %<_Float%d%s%> in "
10676 "declaration specifiers"),
10677 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10678 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10681 else if (specs
->typespec_word
== cts_dfloat32
)
10683 ("both %<unsigned%> and %<_Decimal32%> in "
10684 "declaration specifiers"));
10685 else if (specs
->typespec_word
== cts_dfloat64
)
10687 ("both %<unsigned%> and %<_Decimal64%> in "
10688 "declaration specifiers"));
10689 else if (specs
->typespec_word
== cts_dfloat128
)
10691 ("both %<unsigned%> and %<_Decimal128%> in "
10692 "declaration specifiers"));
10695 specs
->unsigned_p
= true;
10696 specs
->locations
[cdw_unsigned
] = loc
;
10700 dupe
= specs
->complex_p
;
10701 if (!in_system_header_at (loc
))
10702 pedwarn_c90 (loc
, OPT_Wpedantic
,
10703 "ISO C90 does not support complex types");
10704 if (specs
->typespec_word
== cts_auto_type
)
10706 ("both %<complex%> and %<__auto_type%> in "
10707 "declaration specifiers"));
10708 else if (specs
->typespec_word
== cts_void
)
10710 ("both %<complex%> and %<void%> in "
10711 "declaration specifiers"));
10712 else if (specs
->typespec_word
== cts_bool
)
10714 ("both %<complex%> and %<_Bool%> in "
10715 "declaration specifiers"));
10716 else if (specs
->typespec_word
== cts_dfloat32
)
10718 ("both %<complex%> and %<_Decimal32%> in "
10719 "declaration specifiers"));
10720 else if (specs
->typespec_word
== cts_dfloat64
)
10722 ("both %<complex%> and %<_Decimal64%> in "
10723 "declaration specifiers"));
10724 else if (specs
->typespec_word
== cts_dfloat128
)
10726 ("both %<complex%> and %<_Decimal128%> in "
10727 "declaration specifiers"));
10728 else if (specs
->typespec_word
== cts_fract
)
10730 ("both %<complex%> and %<_Fract%> in "
10731 "declaration specifiers"));
10732 else if (specs
->typespec_word
== cts_accum
)
10734 ("both %<complex%> and %<_Accum%> in "
10735 "declaration specifiers"));
10736 else if (specs
->saturating_p
)
10738 ("both %<complex%> and %<_Sat%> in "
10739 "declaration specifiers"));
10742 specs
->complex_p
= true;
10743 specs
->locations
[cdw_complex
] = loc
;
10747 dupe
= specs
->saturating_p
;
10748 pedwarn (loc
, OPT_Wpedantic
,
10749 "ISO C does not support saturating types");
10750 if (specs
->typespec_word
== cts_int_n
)
10753 ("both %<_Sat%> and %<__int%d%> in "
10754 "declaration specifiers"),
10755 int_n_data
[specs
->int_n_idx
].bitsize
);
10757 else if (specs
->typespec_word
== cts_auto_type
)
10759 ("both %<_Sat%> and %<__auto_type%> in "
10760 "declaration specifiers"));
10761 else if (specs
->typespec_word
== cts_void
)
10763 ("both %<_Sat%> and %<void%> in "
10764 "declaration specifiers"));
10765 else if (specs
->typespec_word
== cts_bool
)
10767 ("both %<_Sat%> and %<_Bool%> in "
10768 "declaration specifiers"));
10769 else if (specs
->typespec_word
== cts_char
)
10771 ("both %<_Sat%> and %<char%> in "
10772 "declaration specifiers"));
10773 else if (specs
->typespec_word
== cts_int
)
10775 ("both %<_Sat%> and %<int%> in "
10776 "declaration specifiers"));
10777 else if (specs
->typespec_word
== cts_float
)
10779 ("both %<_Sat%> and %<float%> in "
10780 "declaration specifiers"));
10781 else if (specs
->typespec_word
== cts_double
)
10783 ("both %<_Sat%> and %<double%> in "
10784 "declaration specifiers"));
10785 else if (specs
->typespec_word
== cts_floatn_nx
)
10787 ("both %<_Sat%> and %<_Float%d%s%> in "
10788 "declaration specifiers"),
10789 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
10790 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
10793 else if (specs
->typespec_word
== cts_dfloat32
)
10795 ("both %<_Sat%> and %<_Decimal32%> in "
10796 "declaration specifiers"));
10797 else if (specs
->typespec_word
== cts_dfloat64
)
10799 ("both %<_Sat%> and %<_Decimal64%> in "
10800 "declaration specifiers"));
10801 else if (specs
->typespec_word
== cts_dfloat128
)
10803 ("both %<_Sat%> and %<_Decimal128%> in "
10804 "declaration specifiers"));
10805 else if (specs
->complex_p
)
10807 ("both %<_Sat%> and %<complex%> in "
10808 "declaration specifiers"));
10811 specs
->saturating_p
= true;
10812 specs
->locations
[cdw_saturating
] = loc
;
10816 gcc_unreachable ();
10820 error_at (loc
, "duplicate %qE", type
);
10826 /* "void", "_Bool", "char", "int", "float", "double",
10827 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
10828 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10830 if (specs
->typespec_word
!= cts_none
)
10833 "two or more data types in declaration specifiers");
10838 case RID_AUTO_TYPE
:
10841 ("both %<long%> and %<__auto_type%> in "
10842 "declaration specifiers"));
10843 else if (specs
->short_p
)
10845 ("both %<short%> and %<__auto_type%> in "
10846 "declaration specifiers"));
10847 else if (specs
->signed_p
)
10849 ("both %<signed%> and %<__auto_type%> in "
10850 "declaration specifiers"));
10851 else if (specs
->unsigned_p
)
10853 ("both %<unsigned%> and %<__auto_type%> in "
10854 "declaration specifiers"));
10855 else if (specs
->complex_p
)
10857 ("both %<complex%> and %<__auto_type%> in "
10858 "declaration specifiers"));
10859 else if (specs
->saturating_p
)
10861 ("both %<_Sat%> and %<__auto_type%> in "
10862 "declaration specifiers"));
10865 specs
->typespec_word
= cts_auto_type
;
10866 specs
->locations
[cdw_typespec
] = loc
;
10873 specs
->int_n_idx
= i
- RID_INT_N_0
;
10874 if (!in_system_header_at (input_location
)
10875 /* If the INT_N type ends in "__", and so is of the format
10876 "__intN__", don't pedwarn. */
10877 && (strncmp (IDENTIFIER_POINTER (type
)
10878 + (IDENTIFIER_LENGTH (type
) - 2), "__", 2) != 0))
10879 pedwarn (loc
, OPT_Wpedantic
,
10880 "ISO C does not support %<__int%d%> types",
10881 int_n_data
[specs
->int_n_idx
].bitsize
);
10885 ("both %<__int%d%> and %<long%> in "
10886 "declaration specifiers"),
10887 int_n_data
[specs
->int_n_idx
].bitsize
);
10888 else if (specs
->saturating_p
)
10890 ("both %<_Sat%> and %<__int%d%> in "
10891 "declaration specifiers"),
10892 int_n_data
[specs
->int_n_idx
].bitsize
);
10893 else if (specs
->short_p
)
10895 ("both %<__int%d%> and %<short%> in "
10896 "declaration specifiers"),
10897 int_n_data
[specs
->int_n_idx
].bitsize
);
10898 else if (! int_n_enabled_p
[specs
->int_n_idx
])
10900 specs
->typespec_word
= cts_int_n
;
10902 "%<__int%d%> is not supported on this target",
10903 int_n_data
[specs
->int_n_idx
].bitsize
);
10907 specs
->typespec_word
= cts_int_n
;
10908 specs
->locations
[cdw_typespec
] = loc
;
10914 ("both %<long%> and %<void%> in "
10915 "declaration specifiers"));
10916 else if (specs
->short_p
)
10918 ("both %<short%> and %<void%> in "
10919 "declaration specifiers"));
10920 else if (specs
->signed_p
)
10922 ("both %<signed%> and %<void%> in "
10923 "declaration specifiers"));
10924 else if (specs
->unsigned_p
)
10926 ("both %<unsigned%> and %<void%> in "
10927 "declaration specifiers"));
10928 else if (specs
->complex_p
)
10930 ("both %<complex%> and %<void%> in "
10931 "declaration specifiers"));
10932 else if (specs
->saturating_p
)
10934 ("both %<_Sat%> and %<void%> in "
10935 "declaration specifiers"));
10938 specs
->typespec_word
= cts_void
;
10939 specs
->locations
[cdw_typespec
] = loc
;
10943 if (!in_system_header_at (loc
))
10944 pedwarn_c90 (loc
, OPT_Wpedantic
,
10945 "ISO C90 does not support boolean types");
10948 ("both %<long%> and %<_Bool%> in "
10949 "declaration specifiers"));
10950 else if (specs
->short_p
)
10952 ("both %<short%> and %<_Bool%> in "
10953 "declaration specifiers"));
10954 else if (specs
->signed_p
)
10956 ("both %<signed%> and %<_Bool%> in "
10957 "declaration specifiers"));
10958 else if (specs
->unsigned_p
)
10960 ("both %<unsigned%> and %<_Bool%> in "
10961 "declaration specifiers"));
10962 else if (specs
->complex_p
)
10964 ("both %<complex%> and %<_Bool%> in "
10965 "declaration specifiers"));
10966 else if (specs
->saturating_p
)
10968 ("both %<_Sat%> and %<_Bool%> in "
10969 "declaration specifiers"));
10972 specs
->typespec_word
= cts_bool
;
10973 specs
->locations
[cdw_typespec
] = loc
;
10979 ("both %<long%> and %<char%> in "
10980 "declaration specifiers"));
10981 else if (specs
->short_p
)
10983 ("both %<short%> and %<char%> in "
10984 "declaration specifiers"));
10985 else if (specs
->saturating_p
)
10987 ("both %<_Sat%> and %<char%> in "
10988 "declaration specifiers"));
10991 specs
->typespec_word
= cts_char
;
10992 specs
->locations
[cdw_typespec
] = loc
;
10996 if (specs
->saturating_p
)
10998 ("both %<_Sat%> and %<int%> in "
10999 "declaration specifiers"));
11002 specs
->typespec_word
= cts_int
;
11003 specs
->locations
[cdw_typespec
] = loc
;
11009 ("both %<long%> and %<float%> in "
11010 "declaration specifiers"));
11011 else if (specs
->short_p
)
11013 ("both %<short%> and %<float%> in "
11014 "declaration specifiers"));
11015 else if (specs
->signed_p
)
11017 ("both %<signed%> and %<float%> in "
11018 "declaration specifiers"));
11019 else if (specs
->unsigned_p
)
11021 ("both %<unsigned%> and %<float%> in "
11022 "declaration specifiers"));
11023 else if (specs
->saturating_p
)
11025 ("both %<_Sat%> and %<float%> in "
11026 "declaration specifiers"));
11029 specs
->typespec_word
= cts_float
;
11030 specs
->locations
[cdw_typespec
] = loc
;
11034 if (specs
->long_long_p
)
11036 ("both %<long long%> and %<double%> in "
11037 "declaration specifiers"));
11038 else if (specs
->short_p
)
11040 ("both %<short%> and %<double%> in "
11041 "declaration specifiers"));
11042 else if (specs
->signed_p
)
11044 ("both %<signed%> and %<double%> in "
11045 "declaration specifiers"));
11046 else if (specs
->unsigned_p
)
11048 ("both %<unsigned%> and %<double%> in "
11049 "declaration specifiers"));
11050 else if (specs
->saturating_p
)
11052 ("both %<_Sat%> and %<double%> in "
11053 "declaration specifiers"));
11056 specs
->typespec_word
= cts_double
;
11057 specs
->locations
[cdw_typespec
] = loc
;
11060 CASE_RID_FLOATN_NX
:
11061 specs
->floatn_nx_idx
= i
- RID_FLOATN_NX_FIRST
;
11062 if (!in_system_header_at (input_location
))
11063 pedwarn (loc
, OPT_Wpedantic
,
11064 "ISO C does not support the %<_Float%d%s%> type",
11065 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
11066 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
11072 ("both %<long%> and %<_Float%d%s%> in "
11073 "declaration specifiers"),
11074 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
11075 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
11078 else if (specs
->short_p
)
11080 ("both %<short%> and %<_Float%d%s%> in "
11081 "declaration specifiers"),
11082 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
11083 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
11086 else if (specs
->signed_p
)
11088 ("both %<signed%> and %<_Float%d%s%> in "
11089 "declaration specifiers"),
11090 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
11091 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
11094 else if (specs
->unsigned_p
)
11096 ("both %<unsigned%> and %<_Float%d%s%> in "
11097 "declaration specifiers"),
11098 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
11099 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
11102 else if (specs
->saturating_p
)
11104 ("both %<_Sat%> and %<_Float%d%s%> in "
11105 "declaration specifiers"),
11106 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
11107 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
11110 else if (FLOATN_NX_TYPE_NODE (specs
->floatn_nx_idx
) == NULL_TREE
)
11112 specs
->typespec_word
= cts_floatn_nx
;
11114 "%<_Float%d%s%> is not supported on this target",
11115 floatn_nx_types
[specs
->floatn_nx_idx
].n
,
11116 (floatn_nx_types
[specs
->floatn_nx_idx
].extended
11122 specs
->typespec_word
= cts_floatn_nx
;
11123 specs
->locations
[cdw_typespec
] = loc
;
11128 case RID_DFLOAT128
:
11131 if (i
== RID_DFLOAT32
)
11132 str
= "_Decimal32";
11133 else if (i
== RID_DFLOAT64
)
11134 str
= "_Decimal64";
11136 str
= "_Decimal128";
11137 if (specs
->long_long_p
)
11139 ("both %<long long%> and %qs in "
11140 "declaration specifiers"),
11144 ("both %<long%> and %qs in "
11145 "declaration specifiers"),
11147 else if (specs
->short_p
)
11149 ("both %<short%> and %qs in "
11150 "declaration specifiers"),
11152 else if (specs
->signed_p
)
11154 ("both %<signed%> and %qs in "
11155 "declaration specifiers"),
11157 else if (specs
->unsigned_p
)
11159 ("both %<unsigned%> and %qs in "
11160 "declaration specifiers"),
11162 else if (specs
->complex_p
)
11164 ("both %<complex%> and %qs in "
11165 "declaration specifiers"),
11167 else if (specs
->saturating_p
)
11169 ("both %<_Sat%> and %qs in "
11170 "declaration specifiers"),
11172 else if (i
== RID_DFLOAT32
)
11173 specs
->typespec_word
= cts_dfloat32
;
11174 else if (i
== RID_DFLOAT64
)
11175 specs
->typespec_word
= cts_dfloat64
;
11177 specs
->typespec_word
= cts_dfloat128
;
11178 specs
->locations
[cdw_typespec
] = loc
;
11180 if (!targetm
.decimal_float_supported_p ())
11182 ("decimal floating-point not supported "
11183 "for this target"));
11184 pedwarn_c11 (loc
, OPT_Wpedantic
,
11185 "ISO C does not support decimal floating-point "
11192 if (i
== RID_FRACT
)
11196 if (specs
->complex_p
)
11198 ("both %<complex%> and %qs in "
11199 "declaration specifiers"),
11201 else if (i
== RID_FRACT
)
11202 specs
->typespec_word
= cts_fract
;
11204 specs
->typespec_word
= cts_accum
;
11205 specs
->locations
[cdw_typespec
] = loc
;
11207 if (!targetm
.fixed_point_supported_p ())
11209 "fixed-point types not supported for this target");
11210 pedwarn (loc
, OPT_Wpedantic
,
11211 "ISO C does not support fixed-point types");
11214 /* ObjC reserved word "id", handled below. */
11220 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
11221 form of ObjC type, cases such as "int" and "long" being handled
11222 above), a TYPE (struct, union, enum and typeof specifiers) or an
11223 ERROR_MARK. In none of these cases may there have previously
11224 been any type specifiers. */
11225 if (specs
->type
|| specs
->typespec_word
!= cts_none
11226 || specs
->long_p
|| specs
->short_p
|| specs
->signed_p
11227 || specs
->unsigned_p
|| specs
->complex_p
)
11228 error_at (loc
, "two or more data types in declaration specifiers");
11229 else if (TREE_CODE (type
) == TYPE_DECL
)
11231 if (TREE_TYPE (type
) == error_mark_node
)
11232 ; /* Allow the type to default to int to avoid cascading errors. */
11235 specs
->type
= TREE_TYPE (type
);
11236 specs
->decl_attr
= DECL_ATTRIBUTES (type
);
11237 specs
->typedef_p
= true;
11238 specs
->explicit_signed_p
= C_TYPEDEF_EXPLICITLY_SIGNED (type
);
11239 specs
->locations
[cdw_typedef
] = loc
;
11241 /* If this typedef name is defined in a struct, then a C++
11242 lookup would return a different value. */
11243 if (warn_cxx_compat
11244 && I_SYMBOL_BINDING (DECL_NAME (type
))->in_struct
)
11245 warning_at (loc
, OPT_Wc___compat
,
11246 "C++ lookup of %qD would return a field, not a type",
11249 /* If we are parsing a struct, record that a struct field
11251 if (warn_cxx_compat
&& struct_parse_info
!= NULL
)
11252 struct_parse_info
->typedefs_seen
.safe_push (type
);
11255 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
11257 tree t
= lookup_name (type
);
11258 if (!t
|| TREE_CODE (t
) != TYPE_DECL
)
11259 error_at (loc
, "%qE fails to be a typedef or built in type", type
);
11260 else if (TREE_TYPE (t
) == error_mark_node
)
11264 specs
->type
= TREE_TYPE (t
);
11265 specs
->locations
[cdw_typespec
] = loc
;
11270 if (TREE_CODE (type
) != ERROR_MARK
&& spec
.kind
== ctsk_typeof
)
11272 specs
->typedef_p
= true;
11273 specs
->locations
[cdw_typedef
] = loc
;
11277 specs
->expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (spec
.expr
),
11278 specs
->expr
, spec
.expr
);
11280 specs
->expr
= spec
.expr
;
11281 specs
->expr_const_operands
&= spec
.expr_const_operands
;
11284 specs
->type
= type
;
11290 /* Add the storage class specifier or function specifier SCSPEC to the
11291 declaration specifiers SPECS, returning SPECS. */
11293 struct c_declspecs
*
11294 declspecs_add_scspec (location_t loc
,
11295 struct c_declspecs
*specs
,
11299 enum c_storage_class n
= csc_none
;
11301 specs
->declspecs_seen_p
= true;
11302 specs
->non_std_attrs_seen_p
= true;
11303 gcc_assert (TREE_CODE (scspec
) == IDENTIFIER_NODE
11304 && C_IS_RESERVED_WORD (scspec
));
11305 i
= C_RID_CODE (scspec
);
11306 if (specs
->non_sc_seen_p
)
11307 warning (OPT_Wold_style_declaration
,
11308 "%qE is not at beginning of declaration", scspec
);
11312 /* C99 permits duplicate inline. Although of doubtful utility,
11313 it seems simplest to permit it in gnu89 mode as well, as
11314 there is also little utility in maintaining this as a
11315 difference between gnu89 and C99 inline. */
11317 specs
->inline_p
= true;
11318 specs
->locations
[cdw_inline
] = loc
;
11321 /* Duplicate _Noreturn is permitted. */
11323 specs
->noreturn_p
= true;
11324 specs
->locations
[cdw_noreturn
] = loc
;
11327 dupe
= specs
->thread_p
;
11328 if (specs
->storage_class
== csc_auto
)
11329 error ("%qE used with %<auto%>", scspec
);
11330 else if (specs
->storage_class
== csc_register
)
11331 error ("%qE used with %<register%>", scspec
);
11332 else if (specs
->storage_class
== csc_typedef
)
11333 error ("%qE used with %<typedef%>", scspec
);
11336 specs
->thread_p
= true;
11337 specs
->thread_gnu_p
= (strcmp (IDENTIFIER_POINTER (scspec
),
11339 /* A diagnostic is not required for the use of this
11340 identifier in the implementation namespace; only diagnose
11341 it for the C11 spelling because of existing code using
11342 the other spelling. */
11343 if (!specs
->thread_gnu_p
)
11346 pedwarn_c99 (loc
, OPT_Wpedantic
,
11347 "ISO C99 does not support %qE", scspec
);
11349 pedwarn_c99 (loc
, OPT_Wpedantic
,
11350 "ISO C90 does not support %qE", scspec
);
11352 specs
->locations
[cdw_thread
] = loc
;
11360 /* Diagnose "__thread extern". */
11361 if (specs
->thread_p
&& specs
->thread_gnu_p
)
11362 error ("%<__thread%> before %<extern%>");
11369 /* Diagnose "__thread static". */
11370 if (specs
->thread_p
&& specs
->thread_gnu_p
)
11371 error ("%<__thread%> before %<static%>");
11377 gcc_unreachable ();
11379 if (n
!= csc_none
&& n
== specs
->storage_class
)
11383 if (i
== RID_THREAD
)
11384 error ("duplicate %<_Thread_local%> or %<__thread%>");
11386 error ("duplicate %qE", scspec
);
11390 if (specs
->storage_class
!= csc_none
&& n
!= specs
->storage_class
)
11392 error ("multiple storage classes in declaration specifiers");
11396 specs
->storage_class
= n
;
11397 specs
->locations
[cdw_storage_class
] = loc
;
11398 if (n
!= csc_extern
&& n
!= csc_static
&& specs
->thread_p
)
11400 error ("%qs used with %qE",
11401 specs
->thread_gnu_p
? "__thread" : "_Thread_local",
11403 specs
->thread_p
= false;
11410 /* Add the attributes ATTRS to the declaration specifiers SPECS,
11411 returning SPECS. */
11413 struct c_declspecs
*
11414 declspecs_add_attrs (location_t loc
, struct c_declspecs
*specs
, tree attrs
)
11416 specs
->attrs
= chainon (attrs
, specs
->attrs
);
11417 specs
->locations
[cdw_attributes
] = loc
;
11418 specs
->declspecs_seen_p
= true;
11419 /* In the case of standard attributes at the start of the
11420 declaration, the caller will reset this. */
11421 specs
->non_std_attrs_seen_p
= true;
11425 /* Add an _Alignas specifier (expression ALIGN, or type whose
11426 alignment is ALIGN) to the declaration specifiers SPECS, returning
11428 struct c_declspecs
*
11429 declspecs_add_alignas (location_t loc
,
11430 struct c_declspecs
*specs
, tree align
)
11432 specs
->alignas_p
= true;
11433 specs
->locations
[cdw_alignas
] = loc
;
11434 if (align
== error_mark_node
)
11437 /* Only accept the alignment if it's valid and greater than
11438 the current one. Zero is invalid but by C11 required to
11439 be silently ignored. */
11440 int align_log
= check_user_alignment (align
, false, /* warn_zero = */false);
11441 if (align_log
> specs
->align_log
)
11442 specs
->align_log
= align_log
;
11446 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
11447 specifiers with any other type specifier to determine the resulting
11448 type. This is where ISO C checks on complex types are made, since
11449 "_Complex long" is a prefix of the valid ISO C type "_Complex long
11450 double". Also apply postfix standard attributes to modify the type. */
11452 struct c_declspecs
*
11453 finish_declspecs (struct c_declspecs
*specs
)
11455 /* If a type was specified as a whole, we have no modifiers and are
11457 if (specs
->type
!= NULL_TREE
)
11459 gcc_assert (!specs
->long_p
&& !specs
->long_long_p
&& !specs
->short_p
11460 && !specs
->signed_p
&& !specs
->unsigned_p
11461 && !specs
->complex_p
);
11463 /* Set a dummy type. */
11464 if (TREE_CODE (specs
->type
) == ERROR_MARK
)
11465 specs
->type
= integer_type_node
;
11466 goto handle_postfix_attrs
;
11469 /* If none of "void", "_Bool", "char", "int", "float" or "double"
11470 has been specified, treat it as "int" unless "_Complex" is
11471 present and there are no other specifiers. If we just have
11472 "_Complex", it is equivalent to "_Complex double", but e.g.
11473 "_Complex short" is equivalent to "_Complex short int". */
11474 if (specs
->typespec_word
== cts_none
)
11476 if (specs
->saturating_p
)
11478 error_at (specs
->locations
[cdw_saturating
],
11479 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
11480 if (!targetm
.fixed_point_supported_p ())
11481 error_at (specs
->locations
[cdw_saturating
],
11482 "fixed-point types not supported for this target");
11483 specs
->typespec_word
= cts_fract
;
11485 else if (specs
->long_p
|| specs
->short_p
11486 || specs
->signed_p
|| specs
->unsigned_p
)
11488 specs
->typespec_word
= cts_int
;
11490 else if (specs
->complex_p
)
11492 specs
->typespec_word
= cts_double
;
11493 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
11494 "ISO C does not support plain %<complex%> meaning "
11495 "%<double complex%>");
11499 specs
->typespec_word
= cts_int
;
11500 specs
->default_int_p
= true;
11501 /* We don't diagnose this here because grokdeclarator will
11502 give more specific diagnostics according to whether it is
11503 a function definition. */
11507 /* If "signed" was specified, record this to distinguish "int" and
11508 "signed int" in the case of a bit-field with
11509 -funsigned-bitfields. */
11510 specs
->explicit_signed_p
= specs
->signed_p
;
11512 /* Now compute the actual type. */
11513 switch (specs
->typespec_word
)
11515 case cts_auto_type
:
11516 gcc_assert (!specs
->long_p
&& !specs
->short_p
11517 && !specs
->signed_p
&& !specs
->unsigned_p
11518 && !specs
->complex_p
);
11519 /* Type to be filled in later. */
11520 if (specs
->postfix_attrs
)
11521 error ("%<__auto_type%> followed by %<[[]]%> attributes");
11524 gcc_assert (!specs
->long_p
&& !specs
->short_p
11525 && !specs
->signed_p
&& !specs
->unsigned_p
11526 && !specs
->complex_p
);
11527 specs
->type
= void_type_node
;
11530 gcc_assert (!specs
->long_p
&& !specs
->short_p
11531 && !specs
->signed_p
&& !specs
->unsigned_p
11532 && !specs
->complex_p
);
11533 specs
->type
= boolean_type_node
;
11536 gcc_assert (!specs
->long_p
&& !specs
->short_p
);
11537 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
11538 if (specs
->signed_p
)
11539 specs
->type
= signed_char_type_node
;
11540 else if (specs
->unsigned_p
)
11541 specs
->type
= unsigned_char_type_node
;
11543 specs
->type
= char_type_node
;
11544 if (specs
->complex_p
)
11546 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
11547 "ISO C does not support complex integer types");
11548 specs
->type
= build_complex_type (specs
->type
);
11552 gcc_assert (!specs
->long_p
&& !specs
->short_p
&& !specs
->long_long_p
);
11553 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
11554 if (! int_n_enabled_p
[specs
->int_n_idx
])
11555 specs
->type
= integer_type_node
;
11557 specs
->type
= (specs
->unsigned_p
11558 ? int_n_trees
[specs
->int_n_idx
].unsigned_type
11559 : int_n_trees
[specs
->int_n_idx
].signed_type
);
11560 if (specs
->complex_p
)
11562 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
11563 "ISO C does not support complex integer types");
11564 specs
->type
= build_complex_type (specs
->type
);
11568 gcc_assert (!(specs
->long_p
&& specs
->short_p
));
11569 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
11570 if (specs
->long_long_p
)
11571 specs
->type
= (specs
->unsigned_p
11572 ? long_long_unsigned_type_node
11573 : long_long_integer_type_node
);
11574 else if (specs
->long_p
)
11575 specs
->type
= (specs
->unsigned_p
11576 ? long_unsigned_type_node
11577 : long_integer_type_node
);
11578 else if (specs
->short_p
)
11579 specs
->type
= (specs
->unsigned_p
11580 ? short_unsigned_type_node
11581 : short_integer_type_node
);
11583 specs
->type
= (specs
->unsigned_p
11584 ? unsigned_type_node
11585 : integer_type_node
);
11586 if (specs
->complex_p
)
11588 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
11589 "ISO C does not support complex integer types");
11590 specs
->type
= build_complex_type (specs
->type
);
11594 gcc_assert (!specs
->long_p
&& !specs
->short_p
11595 && !specs
->signed_p
&& !specs
->unsigned_p
);
11596 specs
->type
= (specs
->complex_p
11597 ? complex_float_type_node
11598 : float_type_node
);
11601 gcc_assert (!specs
->long_long_p
&& !specs
->short_p
11602 && !specs
->signed_p
&& !specs
->unsigned_p
);
11605 specs
->type
= (specs
->complex_p
11606 ? complex_long_double_type_node
11607 : long_double_type_node
);
11611 specs
->type
= (specs
->complex_p
11612 ? complex_double_type_node
11613 : double_type_node
);
11616 case cts_floatn_nx
:
11617 gcc_assert (!specs
->long_p
&& !specs
->short_p
11618 && !specs
->signed_p
&& !specs
->unsigned_p
);
11619 if (FLOATN_NX_TYPE_NODE (specs
->floatn_nx_idx
) == NULL_TREE
)
11620 specs
->type
= integer_type_node
;
11621 else if (specs
->complex_p
)
11622 specs
->type
= COMPLEX_FLOATN_NX_TYPE_NODE (specs
->floatn_nx_idx
);
11624 specs
->type
= FLOATN_NX_TYPE_NODE (specs
->floatn_nx_idx
);
11628 case cts_dfloat128
:
11629 gcc_assert (!specs
->long_p
&& !specs
->long_long_p
&& !specs
->short_p
11630 && !specs
->signed_p
&& !specs
->unsigned_p
&& !specs
->complex_p
);
11631 if (!targetm
.decimal_float_supported_p ())
11632 specs
->type
= integer_type_node
;
11633 else if (specs
->typespec_word
== cts_dfloat32
)
11634 specs
->type
= dfloat32_type_node
;
11635 else if (specs
->typespec_word
== cts_dfloat64
)
11636 specs
->type
= dfloat64_type_node
;
11638 specs
->type
= dfloat128_type_node
;
11641 gcc_assert (!specs
->complex_p
);
11642 if (!targetm
.fixed_point_supported_p ())
11643 specs
->type
= integer_type_node
;
11644 else if (specs
->saturating_p
)
11646 if (specs
->long_long_p
)
11647 specs
->type
= specs
->unsigned_p
11648 ? sat_unsigned_long_long_fract_type_node
11649 : sat_long_long_fract_type_node
;
11650 else if (specs
->long_p
)
11651 specs
->type
= specs
->unsigned_p
11652 ? sat_unsigned_long_fract_type_node
11653 : sat_long_fract_type_node
;
11654 else if (specs
->short_p
)
11655 specs
->type
= specs
->unsigned_p
11656 ? sat_unsigned_short_fract_type_node
11657 : sat_short_fract_type_node
;
11659 specs
->type
= specs
->unsigned_p
11660 ? sat_unsigned_fract_type_node
11661 : sat_fract_type_node
;
11665 if (specs
->long_long_p
)
11666 specs
->type
= specs
->unsigned_p
11667 ? unsigned_long_long_fract_type_node
11668 : long_long_fract_type_node
;
11669 else if (specs
->long_p
)
11670 specs
->type
= specs
->unsigned_p
11671 ? unsigned_long_fract_type_node
11672 : long_fract_type_node
;
11673 else if (specs
->short_p
)
11674 specs
->type
= specs
->unsigned_p
11675 ? unsigned_short_fract_type_node
11676 : short_fract_type_node
;
11678 specs
->type
= specs
->unsigned_p
11679 ? unsigned_fract_type_node
11684 gcc_assert (!specs
->complex_p
);
11685 if (!targetm
.fixed_point_supported_p ())
11686 specs
->type
= integer_type_node
;
11687 else if (specs
->saturating_p
)
11689 if (specs
->long_long_p
)
11690 specs
->type
= specs
->unsigned_p
11691 ? sat_unsigned_long_long_accum_type_node
11692 : sat_long_long_accum_type_node
;
11693 else if (specs
->long_p
)
11694 specs
->type
= specs
->unsigned_p
11695 ? sat_unsigned_long_accum_type_node
11696 : sat_long_accum_type_node
;
11697 else if (specs
->short_p
)
11698 specs
->type
= specs
->unsigned_p
11699 ? sat_unsigned_short_accum_type_node
11700 : sat_short_accum_type_node
;
11702 specs
->type
= specs
->unsigned_p
11703 ? sat_unsigned_accum_type_node
11704 : sat_accum_type_node
;
11708 if (specs
->long_long_p
)
11709 specs
->type
= specs
->unsigned_p
11710 ? unsigned_long_long_accum_type_node
11711 : long_long_accum_type_node
;
11712 else if (specs
->long_p
)
11713 specs
->type
= specs
->unsigned_p
11714 ? unsigned_long_accum_type_node
11715 : long_accum_type_node
;
11716 else if (specs
->short_p
)
11717 specs
->type
= specs
->unsigned_p
11718 ? unsigned_short_accum_type_node
11719 : short_accum_type_node
;
11721 specs
->type
= specs
->unsigned_p
11722 ? unsigned_accum_type_node
11727 gcc_unreachable ();
11729 handle_postfix_attrs
:
11730 if (specs
->type
!= NULL
)
11732 specs
->postfix_attrs
= c_warn_type_attributes (specs
->postfix_attrs
);
11733 decl_attributes (&specs
->type
, specs
->postfix_attrs
, 0);
11734 specs
->postfix_attrs
= NULL_TREE
;
11740 /* Perform final processing on one file scope's declarations (or the
11741 external scope's declarations), GLOBALS. */
11744 c_write_global_declarations_1 (tree globals
)
11749 /* Process the decls in the order they were written. */
11750 for (decl
= globals
; decl
; decl
= DECL_CHAIN (decl
))
11752 /* Check for used but undefined static functions using the C
11753 standard's definition of "used", and set TREE_NO_WARNING so
11754 that check_global_declaration doesn't repeat the check. */
11755 if (TREE_CODE (decl
) == FUNCTION_DECL
11756 && DECL_INITIAL (decl
) == NULL_TREE
11757 && DECL_EXTERNAL (decl
)
11758 && !TREE_PUBLIC (decl
))
11760 if (C_DECL_USED (decl
))
11762 if (pedwarn (input_location
, 0, "%q+F used but never defined",
11764 TREE_NO_WARNING (decl
) = 1;
11766 /* For -Wunused-function warn about unused static prototypes. */
11767 else if (warn_unused_function
11768 && ! DECL_ARTIFICIAL (decl
)
11769 && ! TREE_NO_WARNING (decl
))
11771 if (warning (OPT_Wunused_function
,
11772 "%q+F declared %<static%> but never defined",
11774 TREE_NO_WARNING (decl
) = 1;
11778 wrapup_global_declaration_1 (decl
);
11783 reconsider
= false;
11784 for (decl
= globals
; decl
; decl
= DECL_CHAIN (decl
))
11785 reconsider
|= wrapup_global_declaration_2 (decl
);
11787 while (reconsider
);
11790 /* Preserve the external declarations scope across a garbage collect. */
11791 static GTY(()) tree ext_block
;
11793 /* Collect all references relevant to SOURCE_FILE. */
11796 collect_all_refs (const char *source_file
)
11801 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
11802 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t
)), source_file
);
11804 collect_ada_nodes (BLOCK_VARS (ext_block
), source_file
);
11807 /* Collect source file references at global level. */
11810 collect_source_refs (void)
11817 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
11819 decls
= DECL_INITIAL (t
);
11820 for (decl
= BLOCK_VARS (decls
); decl
; decl
= TREE_CHAIN (decl
))
11821 if (!DECL_IS_BUILTIN (decl
))
11822 collect_source_ref (DECL_SOURCE_FILE (decl
));
11825 for (decl
= BLOCK_VARS (ext_block
); decl
; decl
= TREE_CHAIN (decl
))
11826 if (!DECL_IS_BUILTIN (decl
))
11827 collect_source_ref (DECL_SOURCE_FILE (decl
));
11830 /* Perform any final parser cleanups and generate initial debugging
11834 c_parse_final_cleanups (void)
11839 /* We don't want to do this if generating a PCH. */
11843 timevar_stop (TV_PHASE_PARSING
);
11844 timevar_start (TV_PHASE_DEFERRED
);
11846 /* Do the Objective-C stuff. This is where all the Objective-C
11847 module stuff gets generated (symtab, class/protocol/selector
11849 if (c_dialect_objc ())
11850 objc_write_global_declarations ();
11852 /* Close the external scope. */
11853 ext_block
= pop_scope ();
11854 external_scope
= 0;
11855 gcc_assert (!current_scope
);
11857 /* Handle -fdump-ada-spec[-slim]. */
11858 if (flag_dump_ada_spec
|| flag_dump_ada_spec_slim
)
11860 /* Build a table of files to generate specs for */
11861 collect_source_ref (main_input_filename
);
11862 if (!flag_dump_ada_spec_slim
)
11863 collect_source_refs ();
11865 dump_ada_specs (collect_all_refs
, NULL
);
11868 /* Process all file scopes in this compilation, and the external_scope,
11869 through wrapup_global_declarations. */
11870 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
11871 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t
)));
11872 c_write_global_declarations_1 (BLOCK_VARS (ext_block
));
11874 timevar_stop (TV_PHASE_DEFERRED
);
11875 timevar_start (TV_PHASE_PARSING
);
11880 /* Register reserved keyword WORD as qualifier for address space AS. */
11883 c_register_addr_space (const char *word
, addr_space_t as
)
11885 int rid
= RID_FIRST_ADDR_SPACE
+ as
;
11888 /* Address space qualifiers are only supported
11889 in C with GNU extensions enabled. */
11890 if (c_dialect_objc () || flag_no_asm
)
11893 id
= get_identifier (word
);
11894 C_SET_RID_CODE (id
, rid
);
11895 C_IS_RESERVED_WORD (id
) = 1;
11896 ridpointers
[rid
] = id
;
11899 /* Return identifier to look up for omp declare reduction. */
11902 c_omp_reduction_id (enum tree_code reduction_code
, tree reduction_id
)
11904 const char *p
= NULL
;
11905 switch (reduction_code
)
11907 case PLUS_EXPR
: p
= "+"; break;
11908 case MULT_EXPR
: p
= "*"; break;
11909 case MINUS_EXPR
: p
= "-"; break;
11910 case BIT_AND_EXPR
: p
= "&"; break;
11911 case BIT_XOR_EXPR
: p
= "^"; break;
11912 case BIT_IOR_EXPR
: p
= "|"; break;
11913 case TRUTH_ANDIF_EXPR
: p
= "&&"; break;
11914 case TRUTH_ORIF_EXPR
: p
= "||"; break;
11915 case MIN_EXPR
: p
= "min"; break;
11916 case MAX_EXPR
: p
= "max"; break;
11923 if (TREE_CODE (reduction_id
) != IDENTIFIER_NODE
)
11924 return error_mark_node
;
11925 p
= IDENTIFIER_POINTER (reduction_id
);
11928 const char prefix
[] = "omp declare reduction ";
11929 size_t lenp
= sizeof (prefix
);
11930 size_t len
= strlen (p
);
11931 char *name
= XALLOCAVEC (char, lenp
+ len
);
11932 memcpy (name
, prefix
, lenp
- 1);
11933 memcpy (name
+ lenp
- 1, p
, len
+ 1);
11934 return get_identifier (name
);
11937 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11938 VAR_DECL, bind it into the current scope and return it. */
11941 c_omp_reduction_decl (tree reduction_id
)
11943 struct c_binding
*b
= I_SYMBOL_BINDING (reduction_id
);
11944 if (b
!= NULL
&& B_IN_CURRENT_SCOPE (b
))
11947 tree decl
= build_decl (BUILTINS_LOCATION
, VAR_DECL
,
11948 reduction_id
, integer_type_node
);
11949 DECL_ARTIFICIAL (decl
) = 1;
11950 DECL_EXTERNAL (decl
) = 1;
11951 TREE_STATIC (decl
) = 1;
11952 TREE_PUBLIC (decl
) = 0;
11953 bind (reduction_id
, decl
, current_scope
, true, false, BUILTINS_LOCATION
);
11957 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11960 c_omp_reduction_lookup (tree reduction_id
, tree type
)
11962 struct c_binding
*b
= I_SYMBOL_BINDING (reduction_id
);
11966 for (t
= DECL_INITIAL (b
->decl
); t
; t
= TREE_CHAIN (t
))
11967 if (comptypes (TREE_PURPOSE (t
), type
))
11968 return TREE_VALUE (t
);
11971 return error_mark_node
;
11974 /* Helper function called via walk_tree, to diagnose invalid
11975 #pragma omp declare reduction combiners or initializers. */
11978 c_check_omp_declare_reduction_r (tree
*tp
, int *, void *data
)
11980 tree
*vars
= (tree
*) data
;
11981 if (SSA_VAR_P (*tp
)
11982 && !DECL_ARTIFICIAL (*tp
)
11986 location_t loc
= DECL_SOURCE_LOCATION (vars
[0]);
11987 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars
[0])), "omp_out") == 0)
11988 error_at (loc
, "%<#pragma omp declare reduction%> combiner refers to "
11989 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11992 error_at (loc
, "%<#pragma omp declare reduction%> initializer refers "
11993 "to variable %qD which is not %<omp_priv%> nor "
12001 #include "gt-c-c-decl.h"