1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2013 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. */
29 #include "coretypes.h"
34 #include "tree-inline.h"
45 #include "c-family/c-common.h"
46 #include "c-family/c-objc.h"
47 #include "c-family/c-pragma.h"
48 #include "c-family/c-ubsan.h"
50 #include "langhooks.h"
51 #include "tree-iterator.h"
52 #include "diagnostic-core.h"
55 #include "hash-table.h"
56 #include "langhooks-def.h"
57 #include "pointer-set.h"
59 #include "c-family/c-ada-spec.h"
62 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
64 { NORMAL
, /* Ordinary declaration */
65 FUNCDEF
, /* Function definition */
66 PARM
, /* Declaration of parm before function body */
67 FIELD
, /* Declaration inside struct or union */
68 TYPENAME
}; /* Typename (inside cast or sizeof) */
70 /* States indicating how grokdeclarator() should handle declspecs marked
71 with __attribute__((deprecated)). An object declared as
72 __attribute__((deprecated)) suppresses warnings of uses of other
75 enum deprecated_states
{
81 /* Nonzero if we have seen an invalid cross reference
82 to a struct, union, or enum, but not yet printed the message. */
83 tree pending_invalid_xref
;
85 /* File and line to appear in the eventual error message. */
86 location_t pending_invalid_xref_location
;
88 /* The file and line that the prototype came from if this is an
89 old-style definition; used for diagnostics in
90 store_parm_decls_oldstyle. */
92 static location_t current_function_prototype_locus
;
94 /* Whether this prototype was built-in. */
96 static bool current_function_prototype_built_in
;
98 /* The argument type information of this prototype. */
100 static tree current_function_prototype_arg_types
;
102 /* The argument information structure for the function currently being
105 static struct c_arg_info
*current_function_arg_info
;
107 /* The obstack on which parser and related data structures, which are
108 not live beyond their top-level declaration or definition, are
110 struct obstack parser_obstack
;
112 /* The current statement tree. */
114 static GTY(()) struct stmt_tree_s c_stmt_tree
;
116 /* State saving variables. */
120 /* A list of decls to be made automatically visible in each file scope. */
121 static GTY(()) tree visible_builtins
;
123 /* Set to 0 at beginning of a function definition, set to 1 if
124 a return statement that specifies a return value is seen. */
126 int current_function_returns_value
;
128 /* Set to 0 at beginning of a function definition, set to 1 if
129 a return statement with no argument is seen. */
131 int current_function_returns_null
;
133 /* Set to 0 at beginning of a function definition, set to 1 if
134 a call to a noreturn function is seen. */
136 int current_function_returns_abnormally
;
138 /* Set to nonzero by `grokdeclarator' for a function
139 whose return type is defaulted, if warnings for this are desired. */
141 static int warn_about_return_type
;
143 /* Nonzero when the current toplevel function contains a declaration
144 of a nested function which is never defined. */
146 static bool undef_nested_function
;
148 /* Mode used to build pointers (VOIDmode means ptr_mode). */
150 enum machine_mode c_default_pointer_mode
= VOIDmode
;
152 /* If non-zero, implicit "omp declare target" attribute is added into the
154 int current_omp_declare_target_attribute
;
156 /* Each c_binding structure describes one binding of an identifier to
157 a decl. All the decls in a scope - irrespective of namespace - are
158 chained together by the ->prev field, which (as the name implies)
159 runs in reverse order. All the decls in a given namespace bound to
160 a given identifier are chained by the ->shadowed field, which runs
161 from inner to outer scopes.
163 The ->decl field usually points to a DECL node, but there are two
164 exceptions. In the namespace of type tags, the bound entity is a
165 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
166 identifier is encountered, it is bound to error_mark_node to
167 suppress further errors about that identifier in the current
170 The ->u.type field stores the type of the declaration in this scope;
171 if NULL, the type is the type of the ->decl field. This is only of
172 relevance for objects with external or internal linkage which may
173 be redeclared in inner scopes, forming composite types that only
174 persist for the duration of those scopes. In the external scope,
175 this stores the composite of all the types declared for this
176 object, visible or not. The ->inner_comp field (used only at file
177 scope) stores whether an incomplete array type at file scope was
178 completed at an inner scope to an array size other than 1.
180 The ->u.label field is used for labels. It points to a structure
181 which stores additional information used for warnings.
183 The depth field is copied from the scope structure that holds this
184 decl. It is used to preserve the proper ordering of the ->shadowed
185 field (see bind()) and also for a handful of special-case checks.
186 Finally, the invisible bit is true for a decl which should be
187 ignored for purposes of normal name lookup, and the nested bit is
188 true for a decl that's been bound a second time in an inner scope;
189 in all such cases, the binding in the outer scope will have its
190 invisible bit true. */
192 struct GTY((chain_next ("%h.prev"))) c_binding
{
193 union GTY(()) { /* first so GTY desc can use decl */
194 tree
GTY((tag ("0"))) type
; /* the type in this scope */
195 struct c_label_vars
* GTY((tag ("1"))) label
; /* for warnings */
196 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u
;
197 tree decl
; /* the decl bound */
198 tree id
; /* the identifier it's bound to */
199 struct c_binding
*prev
; /* the previous decl in this scope */
200 struct c_binding
*shadowed
; /* the innermost decl shadowed by this one */
201 unsigned int depth
: 28; /* depth of this scope */
202 BOOL_BITFIELD invisible
: 1; /* normal lookup should ignore this binding */
203 BOOL_BITFIELD nested
: 1; /* do not set DECL_CONTEXT when popping */
204 BOOL_BITFIELD inner_comp
: 1; /* incomplete array completed in inner scope */
205 BOOL_BITFIELD in_struct
: 1; /* currently defined as struct field */
206 location_t locus
; /* location for nested bindings */
208 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
209 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
210 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
211 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
213 #define I_SYMBOL_BINDING(node) \
214 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
215 #define I_SYMBOL_DECL(node) \
216 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
218 #define I_TAG_BINDING(node) \
219 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
220 #define I_TAG_DECL(node) \
221 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
223 #define I_LABEL_BINDING(node) \
224 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
225 #define I_LABEL_DECL(node) \
226 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
228 /* Each C symbol points to three linked lists of c_binding structures.
229 These describe the values of the identifier in the three different
230 namespaces defined by the language. */
232 struct GTY(()) lang_identifier
{
233 struct c_common_identifier common_id
;
234 struct c_binding
*symbol_binding
; /* vars, funcs, constants, typedefs */
235 struct c_binding
*tag_binding
; /* struct/union/enum tags */
236 struct c_binding
*label_binding
; /* labels */
239 /* Validate c-lang.c's assumptions. */
240 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
241 [(sizeof(struct lang_identifier
) == C_SIZEOF_STRUCT_LANG_IDENTIFIER
) ? 1 : -1];
243 /* The resulting tree type. */
245 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
246 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
248 union tree_node
GTY ((tag ("0"),
249 desc ("tree_node_structure (&%h)")))
251 struct lang_identifier
GTY ((tag ("1"))) identifier
;
254 /* Track bindings and other things that matter for goto warnings. For
255 efficiency, we do not gather all the decls at the point of
256 definition. Instead, we point into the bindings structure. As
257 scopes are popped, we update these structures and gather the decls
258 that matter at that time. */
260 struct GTY(()) c_spot_bindings
{
261 /* The currently open scope which holds bindings defined when the
262 label was defined or the goto statement was found. */
263 struct c_scope
*scope
;
264 /* The bindings in the scope field which were defined at the point
265 of the label or goto. This lets us look at older or newer
266 bindings in the scope, as appropriate. */
267 struct c_binding
*bindings_in_scope
;
268 /* The number of statement expressions that have started since this
269 label or goto statement was defined. This is zero if we are at
270 the same statement expression level. It is positive if we are in
271 a statement expression started since this spot. It is negative
272 if this spot was in a statement expression and we have left
275 /* Whether we started in a statement expression but are no longer in
276 it. This is set to true if stmt_exprs ever goes negative. */
280 /* This structure is used to keep track of bindings seen when a goto
281 statement is defined. This is only used if we see the goto
282 statement before we see the label. */
284 struct GTY(()) c_goto_bindings
{
285 /* The location of the goto statement. */
287 /* The bindings of the goto statement. */
288 struct c_spot_bindings goto_bindings
;
291 typedef struct c_goto_bindings
*c_goto_bindings_p
;
293 /* The additional information we keep track of for a label binding.
294 These fields are updated as scopes are popped. */
296 struct GTY(()) c_label_vars
{
297 /* The shadowed c_label_vars, when one label shadows another (which
298 can only happen using a __label__ declaration). */
299 struct c_label_vars
*shadowed
;
300 /* The bindings when the label was defined. */
301 struct c_spot_bindings label_bindings
;
302 /* A list of decls that we care about: decls about which we should
303 warn if a goto branches to this label from later in the function.
304 Decls are added to this list as scopes are popped. We only add
305 the decls that matter. */
306 vec
<tree
, va_gc
> *decls_in_scope
;
307 /* A list of goto statements to this label. This is only used for
308 goto statements seen before the label was defined, so that we can
309 issue appropriate warnings for them. */
310 vec
<c_goto_bindings_p
, va_gc
> *gotos
;
313 /* Each c_scope structure describes the complete contents of one
314 scope. Four scopes are distinguished specially: the innermost or
315 current scope, the innermost function scope, the file scope (always
316 the second to outermost) and the outermost or external scope.
318 Most declarations are recorded in the current scope.
320 All normal label declarations are recorded in the innermost
321 function scope, as are bindings of undeclared identifiers to
322 error_mark_node. (GCC permits nested functions as an extension,
323 hence the 'innermost' qualifier.) Explicitly declared labels
324 (using the __label__ extension) appear in the current scope.
326 Being in the file scope (current_scope == file_scope) causes
327 special behavior in several places below. Also, under some
328 conditions the Objective-C front end records declarations in the
329 file scope even though that isn't the current scope.
331 All declarations with external linkage are recorded in the external
332 scope, even if they aren't visible there; this models the fact that
333 such declarations are visible to the entire program, and (with a
334 bit of cleverness, see pushdecl) allows diagnosis of some violations
335 of C99 6.2.2p7 and 6.2.7p2:
337 If, within the same translation unit, the same identifier appears
338 with both internal and external linkage, the behavior is
341 All declarations that refer to the same object or function shall
342 have compatible type; otherwise, the behavior is undefined.
344 Initially only the built-in declarations, which describe compiler
345 intrinsic functions plus a subset of the standard library, are in
348 The order of the blocks list matters, and it is frequently appended
349 to. To avoid having to walk all the way to the end of the list on
350 each insertion, or reverse the list later, we maintain a pointer to
351 the last list entry. (FIXME: It should be feasible to use a reversed
354 The bindings list is strictly in reverse order of declarations;
355 pop_scope relies on this. */
358 struct GTY((chain_next ("%h.outer"))) c_scope
{
359 /* The scope containing this one. */
360 struct c_scope
*outer
;
362 /* The next outermost function scope. */
363 struct c_scope
*outer_function
;
365 /* All bindings in this scope. */
366 struct c_binding
*bindings
;
368 /* For each scope (except the global one), a chain of BLOCK nodes
369 for all the scopes that were entered and exited one level down. */
373 /* The depth of this scope. Used to keep the ->shadowed chain of
374 bindings sorted innermost to outermost. */
375 unsigned int depth
: 28;
377 /* True if we are currently filling this scope with parameter
379 BOOL_BITFIELD parm_flag
: 1;
381 /* True if we saw [*] in this scope. Used to give an error messages
382 if these appears in a function definition. */
383 BOOL_BITFIELD had_vla_unspec
: 1;
385 /* True if we already complained about forward parameter decls
386 in this scope. This prevents double warnings on
387 foo (int a; int b; ...) */
388 BOOL_BITFIELD warned_forward_parm_decls
: 1;
390 /* True if this is the outermost block scope of a function body.
391 This scope contains the parameters, the local variables declared
392 in the outermost block, and all the labels (except those in
393 nested functions, or declared at block scope with __label__). */
394 BOOL_BITFIELD function_body
: 1;
396 /* True means make a BLOCK for this scope no matter what. */
397 BOOL_BITFIELD keep
: 1;
399 /* True means that an unsuffixed float constant is _Decimal64. */
400 BOOL_BITFIELD float_const_decimal64
: 1;
402 /* True if this scope has any label bindings. This is used to speed
403 up searching for labels when popping scopes, particularly since
404 labels are normally only found at function scope. */
405 BOOL_BITFIELD has_label_bindings
: 1;
407 /* True if we should issue a warning if a goto statement crosses any
408 of the bindings. We still need to check the list of bindings to
409 find the specific ones we need to warn about. This is true if
410 decl_jump_unsafe would return true for any of the bindings. This
411 is used to avoid looping over all the bindings unnecessarily. */
412 BOOL_BITFIELD has_jump_unsafe_decl
: 1;
415 /* The scope currently in effect. */
417 static GTY(()) struct c_scope
*current_scope
;
419 /* The innermost function scope. Ordinary (not explicitly declared)
420 labels, bindings to error_mark_node, and the lazily-created
421 bindings of __func__ and its friends get this scope. */
423 static GTY(()) struct c_scope
*current_function_scope
;
425 /* The C file scope. This is reset for each input translation unit. */
427 static GTY(()) struct c_scope
*file_scope
;
429 /* The outermost scope. This is used for all declarations with
430 external linkage, and only these, hence the name. */
432 static GTY(()) struct c_scope
*external_scope
;
434 /* A chain of c_scope structures awaiting reuse. */
436 static GTY((deletable
)) struct c_scope
*scope_freelist
;
438 /* A chain of c_binding structures awaiting reuse. */
440 static GTY((deletable
)) struct c_binding
*binding_freelist
;
442 /* Append VAR to LIST in scope SCOPE. */
443 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
444 struct c_scope *s_ = (scope); \
446 if (s_->list##_last) \
447 BLOCK_CHAIN (s_->list##_last) = d_; \
450 s_->list##_last = d_; \
453 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
454 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
455 struct c_scope *t_ = (tscope); \
456 struct c_scope *f_ = (fscope); \
458 BLOCK_CHAIN (t_->to##_last) = f_->from; \
461 t_->to##_last = f_->from##_last; \
464 /* A c_inline_static structure stores details of a static identifier
465 referenced in a definition of a function that may be an inline
466 definition if no subsequent declaration of that function uses
467 "extern" or does not use "inline". */
469 struct GTY((chain_next ("%h.next"))) c_inline_static
{
470 /* The location for a diagnostic. */
473 /* The function that may be an inline definition. */
476 /* The object or function referenced. */
479 /* What sort of reference this is. */
480 enum c_inline_static_type type
;
482 /* The next such structure or NULL. */
483 struct c_inline_static
*next
;
486 /* List of static identifiers used or referenced in functions that may
487 be inline definitions. */
488 static GTY(()) struct c_inline_static
*c_inline_statics
;
490 /* True means unconditionally make a BLOCK for the next scope pushed. */
492 static bool keep_next_level_flag
;
494 /* True means the next call to push_scope will be the outermost scope
495 of a function body, so do not push a new scope, merely cease
496 expecting parameter decls. */
498 static bool next_is_function_body
;
500 /* A vector of pointers to c_binding structures. */
502 typedef struct c_binding
*c_binding_ptr
;
504 /* Information that we keep for a struct or union while it is being
507 struct c_struct_parse_info
509 /* If warn_cxx_compat, a list of types defined within this
511 vec
<tree
> struct_types
;
512 /* If warn_cxx_compat, a list of field names which have bindings,
513 and which are defined in this struct, but which are not defined
514 in any enclosing struct. This is used to clear the in_struct
515 field of the c_bindings structure. */
516 vec
<c_binding_ptr
> fields
;
517 /* If warn_cxx_compat, a list of typedef names used when defining
518 fields in this struct. */
519 vec
<tree
> typedefs_seen
;
522 /* Information for the struct or union currently being parsed, or
523 NULL if not parsing a struct or union. */
524 static struct c_struct_parse_info
*struct_parse_info
;
526 /* Forward declarations. */
527 static tree
lookup_name_in_scope (tree
, struct c_scope
*);
528 static tree
c_make_fname_decl (location_t
, tree
, int);
529 static tree
grokdeclarator (const struct c_declarator
*,
530 struct c_declspecs
*,
531 enum decl_context
, bool, tree
*, tree
*, tree
*,
532 bool *, enum deprecated_states
);
533 static tree
grokparms (struct c_arg_info
*, bool);
534 static void layout_array_type (tree
);
536 /* T is a statement. Add it to the statement-tree. This is the
537 C/ObjC version--C++ has a slightly different version of this
543 enum tree_code code
= TREE_CODE (t
);
545 if (CAN_HAVE_LOCATION_P (t
) && code
!= LABEL_EXPR
)
547 if (!EXPR_HAS_LOCATION (t
))
548 SET_EXPR_LOCATION (t
, input_location
);
551 if (code
== LABEL_EXPR
|| code
== CASE_LABEL_EXPR
)
552 STATEMENT_LIST_HAS_LABEL (cur_stmt_list
) = 1;
554 /* Add T to the statement-tree. Non-side-effect statements need to be
555 recorded during statement expressions. */
556 if (!building_stmt_list_p ())
558 append_to_statement_list_force (t
, &cur_stmt_list
);
563 /* Build a pointer type using the default pointer mode. */
566 c_build_pointer_type (tree to_type
)
568 addr_space_t as
= to_type
== error_mark_node
? ADDR_SPACE_GENERIC
569 : TYPE_ADDR_SPACE (to_type
);
570 enum machine_mode pointer_mode
;
572 if (as
!= ADDR_SPACE_GENERIC
|| c_default_pointer_mode
== VOIDmode
)
573 pointer_mode
= targetm
.addr_space
.pointer_mode (as
);
575 pointer_mode
= c_default_pointer_mode
;
576 return build_pointer_type_for_mode (to_type
, pointer_mode
, false);
580 /* Return true if we will want to say something if a goto statement
584 decl_jump_unsafe (tree decl
)
586 if (error_operand_p (decl
))
589 /* Always warn about crossing variably modified types. */
590 if ((TREE_CODE (decl
) == VAR_DECL
|| TREE_CODE (decl
) == TYPE_DECL
)
591 && variably_modified_type_p (TREE_TYPE (decl
), NULL_TREE
))
594 /* Otherwise, only warn if -Wgoto-misses-init and this is an
595 initialized automatic decl. */
596 if (warn_jump_misses_init
597 && TREE_CODE (decl
) == VAR_DECL
598 && !TREE_STATIC (decl
)
599 && DECL_INITIAL (decl
) != NULL_TREE
)
607 c_print_identifier (FILE *file
, tree node
, int indent
)
609 print_node (file
, "symbol", I_SYMBOL_DECL (node
), indent
+ 4);
610 print_node (file
, "tag", I_TAG_DECL (node
), indent
+ 4);
611 print_node (file
, "label", I_LABEL_DECL (node
), indent
+ 4);
612 if (C_IS_RESERVED_WORD (node
) && C_RID_CODE (node
) != RID_CXX_COMPAT_WARN
)
614 tree rid
= ridpointers
[C_RID_CODE (node
)];
615 indent_to (file
, indent
+ 4);
616 fprintf (file
, "rid " HOST_PTR_PRINTF
" \"%s\"",
617 (void *) rid
, IDENTIFIER_POINTER (rid
));
621 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
622 which may be any of several kinds of DECL or TYPE or error_mark_node,
623 in the scope SCOPE. */
625 bind (tree name
, tree decl
, struct c_scope
*scope
, bool invisible
,
626 bool nested
, location_t locus
)
628 struct c_binding
*b
, **here
;
630 if (binding_freelist
)
632 b
= binding_freelist
;
633 binding_freelist
= b
->prev
;
636 b
= ggc_alloc_c_binding ();
641 b
->depth
= scope
->depth
;
642 b
->invisible
= invisible
;
650 b
->prev
= scope
->bindings
;
653 if (decl_jump_unsafe (decl
))
654 scope
->has_jump_unsafe_decl
= 1;
659 switch (TREE_CODE (decl
))
661 case LABEL_DECL
: here
= &I_LABEL_BINDING (name
); break;
664 case RECORD_TYPE
: here
= &I_TAG_BINDING (name
); break;
670 case ERROR_MARK
: here
= &I_SYMBOL_BINDING (name
); break;
676 /* Locate the appropriate place in the chain of shadowed decls
677 to insert this binding. Normally, scope == current_scope and
678 this does nothing. */
679 while (*here
&& (*here
)->depth
> scope
->depth
)
680 here
= &(*here
)->shadowed
;
686 /* Clear the binding structure B, stick it on the binding_freelist,
687 and return the former value of b->prev. This is used by pop_scope
688 and get_parm_info to iterate destructively over all the bindings
689 from a given scope. */
690 static struct c_binding
*
691 free_binding_and_advance (struct c_binding
*b
)
693 struct c_binding
*prev
= b
->prev
;
695 memset (b
, 0, sizeof (struct c_binding
));
696 b
->prev
= binding_freelist
;
697 binding_freelist
= b
;
702 /* Bind a label. Like bind, but skip fields which aren't used for
703 labels, and add the LABEL_VARS value. */
705 bind_label (tree name
, tree label
, struct c_scope
*scope
,
706 struct c_label_vars
*label_vars
)
710 bind (name
, label
, scope
, /*invisible=*/false, /*nested=*/false,
713 scope
->has_label_bindings
= true;
716 gcc_assert (b
->decl
== label
);
717 label_vars
->shadowed
= b
->u
.label
;
718 b
->u
.label
= label_vars
;
721 /* Hook called at end of compilation to assume 1 elt
722 for a file-scope tentative array defn that wasn't complete before. */
725 c_finish_incomplete_decl (tree decl
)
727 if (TREE_CODE (decl
) == VAR_DECL
)
729 tree type
= TREE_TYPE (decl
);
730 if (type
!= error_mark_node
731 && TREE_CODE (type
) == ARRAY_TYPE
732 && !DECL_EXTERNAL (decl
)
733 && TYPE_DOMAIN (type
) == 0)
735 warning_at (DECL_SOURCE_LOCATION (decl
),
736 0, "array %q+D assumed to have one element", decl
);
738 complete_array_type (&TREE_TYPE (decl
), NULL_TREE
, true);
740 relayout_decl (decl
);
745 /* Record that inline function FUNC contains a reference (location
746 LOC) to static DECL (file-scope or function-local according to
750 record_inline_static (location_t loc
, tree func
, tree decl
,
751 enum c_inline_static_type type
)
753 struct c_inline_static
*csi
= ggc_alloc_c_inline_static ();
755 csi
->function
= func
;
756 csi
->static_decl
= decl
;
758 csi
->next
= c_inline_statics
;
759 c_inline_statics
= csi
;
762 /* Check for references to static declarations in inline functions at
763 the end of the translation unit and diagnose them if the functions
764 are still inline definitions. */
767 check_inline_statics (void)
769 struct c_inline_static
*csi
;
770 for (csi
= c_inline_statics
; csi
; csi
= csi
->next
)
772 if (DECL_EXTERNAL (csi
->function
))
776 pedwarn (csi
->location
, 0,
777 "%qD is static but used in inline function %qD "
778 "which is not static", csi
->static_decl
, csi
->function
);
781 pedwarn (csi
->location
, 0,
782 "%q+D is static but declared in inline function %qD "
783 "which is not static", csi
->static_decl
, csi
->function
);
789 c_inline_statics
= NULL
;
792 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
793 for the current state, otherwise set it to uninitialized. */
796 set_spot_bindings (struct c_spot_bindings
*p
, bool defining
)
800 p
->scope
= current_scope
;
801 p
->bindings_in_scope
= current_scope
->bindings
;
806 p
->bindings_in_scope
= NULL
;
809 p
->left_stmt_expr
= false;
812 /* Update spot bindings P as we pop out of SCOPE. Return true if we
813 should push decls for a label. */
816 update_spot_bindings (struct c_scope
*scope
, struct c_spot_bindings
*p
)
818 if (p
->scope
!= scope
)
820 /* This label or goto is defined in some other scope, or it is a
821 label which is not yet defined. There is nothing to
826 /* Adjust the spot bindings to refer to the bindings already defined
827 in the enclosing scope. */
828 p
->scope
= scope
->outer
;
829 p
->bindings_in_scope
= p
->scope
->bindings
;
834 /* The Objective-C front-end often needs to determine the current scope. */
837 objc_get_current_scope (void)
839 return current_scope
;
842 /* The following function is used only by Objective-C. It needs to live here
843 because it accesses the innards of c_scope. */
846 objc_mark_locals_volatile (void *enclosing_blk
)
848 struct c_scope
*scope
;
851 for (scope
= current_scope
;
852 scope
&& scope
!= enclosing_blk
;
853 scope
= scope
->outer
)
855 for (b
= scope
->bindings
; b
; b
= b
->prev
)
856 objc_volatilize_decl (b
->decl
);
858 /* Do not climb up past the current function. */
859 if (scope
->function_body
)
864 /* Return true if we are in the global binding level. */
867 global_bindings_p (void)
869 return current_scope
== file_scope
;
873 keep_next_level (void)
875 keep_next_level_flag
= true;
878 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
881 set_float_const_decimal64 (void)
883 current_scope
->float_const_decimal64
= true;
886 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
889 clear_float_const_decimal64 (void)
891 current_scope
->float_const_decimal64
= false;
894 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
897 float_const_decimal64_p (void)
899 return current_scope
->float_const_decimal64
;
902 /* Identify this scope as currently being filled with parameters. */
905 declare_parm_level (void)
907 current_scope
->parm_flag
= true;
913 if (next_is_function_body
)
915 /* This is the transition from the parameters to the top level
916 of the function body. These are the same scope
917 (C99 6.2.1p4,6) so we do not push another scope structure.
918 next_is_function_body is set only by store_parm_decls, which
919 in turn is called when and only when we are about to
920 encounter the opening curly brace for the function body.
922 The outermost block of a function always gets a BLOCK node,
923 because the debugging output routines expect that each
924 function has at least one BLOCK. */
925 current_scope
->parm_flag
= false;
926 current_scope
->function_body
= true;
927 current_scope
->keep
= true;
928 current_scope
->outer_function
= current_function_scope
;
929 current_function_scope
= current_scope
;
931 keep_next_level_flag
= false;
932 next_is_function_body
= false;
934 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
935 if (current_scope
->outer
)
936 current_scope
->float_const_decimal64
937 = current_scope
->outer
->float_const_decimal64
;
939 current_scope
->float_const_decimal64
= false;
943 struct c_scope
*scope
;
946 scope
= scope_freelist
;
947 scope_freelist
= scope
->outer
;
950 scope
= ggc_alloc_cleared_c_scope ();
952 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
954 scope
->float_const_decimal64
= current_scope
->float_const_decimal64
;
956 scope
->float_const_decimal64
= false;
958 scope
->keep
= keep_next_level_flag
;
959 scope
->outer
= current_scope
;
960 scope
->depth
= current_scope
? (current_scope
->depth
+ 1) : 0;
962 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
964 if (current_scope
&& scope
->depth
== 0)
967 sorry ("GCC supports only %u nested scopes", scope
->depth
);
970 current_scope
= scope
;
971 keep_next_level_flag
= false;
975 /* This is called when we are leaving SCOPE. For each label defined
976 in SCOPE, add any appropriate decls to its decls_in_scope fields.
977 These are the decls whose initialization will be skipped by a goto
978 later in the function. */
981 update_label_decls (struct c_scope
*scope
)
988 if (s
->has_label_bindings
)
992 for (b
= s
->bindings
; b
!= NULL
; b
= b
->prev
)
994 struct c_label_vars
*label_vars
;
995 struct c_binding
*b1
;
998 struct c_goto_bindings
*g
;
1000 if (TREE_CODE (b
->decl
) != LABEL_DECL
)
1002 label_vars
= b
->u
.label
;
1004 b1
= label_vars
->label_bindings
.bindings_in_scope
;
1005 if (label_vars
->label_bindings
.scope
== NULL
)
1008 hjud
= label_vars
->label_bindings
.scope
->has_jump_unsafe_decl
;
1009 if (update_spot_bindings (scope
, &label_vars
->label_bindings
))
1011 /* This label is defined in this scope. */
1014 for (; b1
!= NULL
; b1
= b1
->prev
)
1016 /* A goto from later in the function to this
1017 label will never see the initialization
1018 of B1, if any. Save it to issue a
1019 warning if needed. */
1020 if (decl_jump_unsafe (b1
->decl
))
1021 vec_safe_push(label_vars
->decls_in_scope
, b1
->decl
);
1026 /* Update the bindings of any goto statements associated
1028 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
1029 update_spot_bindings (scope
, &g
->goto_bindings
);
1033 /* Don't search beyond the current function. */
1034 if (s
== current_function_scope
)
1041 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1044 set_type_context (tree type
, tree context
)
1046 for (type
= TYPE_MAIN_VARIANT (type
); type
;
1047 type
= TYPE_NEXT_VARIANT (type
))
1048 TYPE_CONTEXT (type
) = context
;
1051 /* Exit a scope. Restore the state of the identifier-decl mappings
1052 that were in effect when this scope was entered. Return a BLOCK
1053 node containing all the DECLs in this scope that are of interest
1054 to debug info generation. */
1059 struct c_scope
*scope
= current_scope
;
1060 tree block
, context
, p
;
1061 struct c_binding
*b
;
1063 bool functionbody
= scope
->function_body
;
1064 bool keep
= functionbody
|| scope
->keep
|| scope
->bindings
;
1066 update_label_decls (scope
);
1068 /* If appropriate, create a BLOCK to record the decls for the life
1069 of this function. */
1073 block
= make_node (BLOCK
);
1074 BLOCK_SUBBLOCKS (block
) = scope
->blocks
;
1075 TREE_USED (block
) = 1;
1077 /* In each subblock, record that this is its superior. */
1078 for (p
= scope
->blocks
; p
; p
= BLOCK_CHAIN (p
))
1079 BLOCK_SUPERCONTEXT (p
) = block
;
1081 BLOCK_VARS (block
) = 0;
1084 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1085 scope must be set so that they point to the appropriate
1086 construct, i.e. either to the current FUNCTION_DECL node, or
1087 else to the BLOCK node we just constructed.
1089 Note that for tagged types whose scope is just the formal
1090 parameter list for some function type specification, we can't
1091 properly set their TYPE_CONTEXTs here, because we don't have a
1092 pointer to the appropriate FUNCTION_TYPE node readily available
1093 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1094 type nodes get set in `grokdeclarator' as soon as we have created
1095 the FUNCTION_TYPE node which will represent the "scope" for these
1096 "parameter list local" tagged types. */
1097 if (scope
->function_body
)
1098 context
= current_function_decl
;
1099 else if (scope
== file_scope
)
1101 tree file_decl
= build_translation_unit_decl (NULL_TREE
);
1102 context
= file_decl
;
1107 /* Clear all bindings in this scope. */
1108 for (b
= scope
->bindings
; b
; b
= free_binding_and_advance (b
))
1111 switch (TREE_CODE (p
))
1114 /* Warnings for unused labels, errors for undefined labels. */
1115 if (TREE_USED (p
) && !DECL_INITIAL (p
))
1117 error ("label %q+D used but not defined", p
);
1118 DECL_INITIAL (p
) = error_mark_node
;
1121 warn_for_unused_label (p
);
1123 /* Labels go in BLOCK_VARS. */
1124 DECL_CHAIN (p
) = BLOCK_VARS (block
);
1125 BLOCK_VARS (block
) = p
;
1126 gcc_assert (I_LABEL_BINDING (b
->id
) == b
);
1127 I_LABEL_BINDING (b
->id
) = b
->shadowed
;
1129 /* Also pop back to the shadowed label_vars. */
1130 release_tree_vector (b
->u
.label
->decls_in_scope
);
1131 b
->u
.label
= b
->u
.label
->shadowed
;
1137 set_type_context (p
, context
);
1139 /* Types may not have tag-names, in which case the type
1140 appears in the bindings list with b->id NULL. */
1143 gcc_assert (I_TAG_BINDING (b
->id
) == b
);
1144 I_TAG_BINDING (b
->id
) = b
->shadowed
;
1149 /* Propagate TREE_ADDRESSABLE from nested functions to their
1150 containing functions. */
1151 if (!TREE_ASM_WRITTEN (p
)
1152 && DECL_INITIAL (p
) != 0
1153 && TREE_ADDRESSABLE (p
)
1154 && DECL_ABSTRACT_ORIGIN (p
) != 0
1155 && DECL_ABSTRACT_ORIGIN (p
) != p
)
1156 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p
)) = 1;
1157 if (!DECL_EXTERNAL (p
)
1158 && !DECL_INITIAL (p
)
1159 && scope
!= file_scope
1160 && scope
!= external_scope
)
1162 error ("nested function %q+D declared but never defined", p
);
1163 undef_nested_function
= true;
1165 else if (DECL_DECLARED_INLINE_P (p
)
1167 && !DECL_INITIAL (p
))
1169 /* C99 6.7.4p6: "a function with external linkage... declared
1170 with an inline function specifier ... shall also be defined
1171 in the same translation unit." */
1172 if (!flag_gnu89_inline
)
1173 pedwarn (input_location
, 0,
1174 "inline function %q+D declared but never defined", p
);
1175 DECL_EXTERNAL (p
) = 1;
1181 /* Warnings for unused variables. */
1182 if ((!TREE_USED (p
) || !DECL_READ_P (p
))
1183 && !TREE_NO_WARNING (p
)
1184 && !DECL_IN_SYSTEM_HEADER (p
)
1186 && !DECL_ARTIFICIAL (p
)
1187 && scope
!= file_scope
1188 && scope
!= external_scope
)
1191 warning (OPT_Wunused_variable
, "unused variable %q+D", p
);
1192 else if (DECL_CONTEXT (p
) == current_function_decl
)
1193 warning_at (DECL_SOURCE_LOCATION (p
),
1194 OPT_Wunused_but_set_variable
,
1195 "variable %qD set but not used", p
);
1200 error ("type of array %q+D completed incompatibly with"
1201 " implicit initialization", p
);
1208 /* All of these go in BLOCK_VARS, but only if this is the
1209 binding in the home scope. */
1212 DECL_CHAIN (p
) = BLOCK_VARS (block
);
1213 BLOCK_VARS (block
) = p
;
1215 else if (VAR_OR_FUNCTION_DECL_P (p
) && scope
!= file_scope
)
1217 /* For block local externs add a special
1218 DECL_EXTERNAL decl for debug info generation. */
1219 tree extp
= copy_node (p
);
1221 DECL_EXTERNAL (extp
) = 1;
1222 TREE_STATIC (extp
) = 0;
1223 TREE_PUBLIC (extp
) = 1;
1224 DECL_INITIAL (extp
) = NULL_TREE
;
1225 DECL_LANG_SPECIFIC (extp
) = NULL
;
1226 DECL_CONTEXT (extp
) = current_function_decl
;
1227 if (TREE_CODE (p
) == FUNCTION_DECL
)
1229 DECL_RESULT (extp
) = NULL_TREE
;
1230 DECL_SAVED_TREE (extp
) = NULL_TREE
;
1231 DECL_STRUCT_FUNCTION (extp
) = NULL
;
1233 if (b
->locus
!= UNKNOWN_LOCATION
)
1234 DECL_SOURCE_LOCATION (extp
) = b
->locus
;
1235 DECL_CHAIN (extp
) = BLOCK_VARS (block
);
1236 BLOCK_VARS (block
) = extp
;
1238 /* If this is the file scope set DECL_CONTEXT of each decl to
1239 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1241 if (scope
== file_scope
)
1243 DECL_CONTEXT (p
) = context
;
1244 if (TREE_CODE (p
) == TYPE_DECL
1245 && TREE_TYPE (p
) != error_mark_node
)
1246 set_type_context (TREE_TYPE (p
), context
);
1250 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1251 already been put there by store_parm_decls. Unused-
1252 parameter warnings are handled by function.c.
1253 error_mark_node obviously does not go in BLOCK_VARS and
1254 does not get unused-variable warnings. */
1257 /* It is possible for a decl not to have a name. We get
1258 here with b->id NULL in this case. */
1261 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
1262 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
1263 if (b
->shadowed
&& b
->shadowed
->u
.type
)
1264 TREE_TYPE (b
->shadowed
->decl
) = b
->shadowed
->u
.type
;
1274 /* Dispose of the block that we just made inside some higher level. */
1275 if ((scope
->function_body
|| scope
== file_scope
) && context
)
1277 DECL_INITIAL (context
) = block
;
1278 BLOCK_SUPERCONTEXT (block
) = context
;
1280 else if (scope
->outer
)
1283 SCOPE_LIST_APPEND (scope
->outer
, blocks
, block
);
1284 /* If we did not make a block for the scope just exited, any
1285 blocks made for inner scopes must be carried forward so they
1286 will later become subblocks of something else. */
1287 else if (scope
->blocks
)
1288 SCOPE_LIST_CONCAT (scope
->outer
, blocks
, scope
, blocks
);
1291 /* Pop the current scope, and free the structure for reuse. */
1292 current_scope
= scope
->outer
;
1293 if (scope
->function_body
)
1294 current_function_scope
= scope
->outer_function
;
1296 memset (scope
, 0, sizeof (struct c_scope
));
1297 scope
->outer
= scope_freelist
;
1298 scope_freelist
= scope
;
1304 push_file_scope (void)
1312 file_scope
= current_scope
;
1314 start_fname_decls ();
1316 for (decl
= visible_builtins
; decl
; decl
= DECL_CHAIN (decl
))
1317 bind (DECL_NAME (decl
), decl
, file_scope
,
1318 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl
));
1322 pop_file_scope (void)
1324 /* In case there were missing closebraces, get us back to the global
1326 while (current_scope
!= file_scope
)
1329 /* __FUNCTION__ is defined at file scope (""). This
1330 call may not be necessary as my tests indicate it
1331 still works without it. */
1332 finish_fname_decls ();
1334 check_inline_statics ();
1336 /* This is the point to write out a PCH if we're doing that.
1337 In that case we do not want to do anything else. */
1340 c_common_write_pch ();
1344 /* Pop off the file scope and close this translation unit. */
1348 maybe_apply_pending_pragma_weaks ();
1351 /* Adjust the bindings for the start of a statement expression. */
1354 c_bindings_start_stmt_expr (struct c_spot_bindings
* switch_bindings
)
1356 struct c_scope
*scope
;
1358 for (scope
= current_scope
; scope
!= NULL
; scope
= scope
->outer
)
1360 struct c_binding
*b
;
1362 if (!scope
->has_label_bindings
)
1365 for (b
= scope
->bindings
; b
!= NULL
; b
= b
->prev
)
1367 struct c_label_vars
*label_vars
;
1369 struct c_goto_bindings
*g
;
1371 if (TREE_CODE (b
->decl
) != LABEL_DECL
)
1373 label_vars
= b
->u
.label
;
1374 ++label_vars
->label_bindings
.stmt_exprs
;
1375 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
1376 ++g
->goto_bindings
.stmt_exprs
;
1380 if (switch_bindings
!= NULL
)
1381 ++switch_bindings
->stmt_exprs
;
1384 /* Adjust the bindings for the end of a statement expression. */
1387 c_bindings_end_stmt_expr (struct c_spot_bindings
*switch_bindings
)
1389 struct c_scope
*scope
;
1391 for (scope
= current_scope
; scope
!= NULL
; scope
= scope
->outer
)
1393 struct c_binding
*b
;
1395 if (!scope
->has_label_bindings
)
1398 for (b
= scope
->bindings
; b
!= NULL
; b
= b
->prev
)
1400 struct c_label_vars
*label_vars
;
1402 struct c_goto_bindings
*g
;
1404 if (TREE_CODE (b
->decl
) != LABEL_DECL
)
1406 label_vars
= b
->u
.label
;
1407 --label_vars
->label_bindings
.stmt_exprs
;
1408 if (label_vars
->label_bindings
.stmt_exprs
< 0)
1410 label_vars
->label_bindings
.left_stmt_expr
= true;
1411 label_vars
->label_bindings
.stmt_exprs
= 0;
1413 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
1415 --g
->goto_bindings
.stmt_exprs
;
1416 if (g
->goto_bindings
.stmt_exprs
< 0)
1418 g
->goto_bindings
.left_stmt_expr
= true;
1419 g
->goto_bindings
.stmt_exprs
= 0;
1425 if (switch_bindings
!= NULL
)
1427 --switch_bindings
->stmt_exprs
;
1428 gcc_assert (switch_bindings
->stmt_exprs
>= 0);
1432 /* Push a definition or a declaration of struct, union or enum tag "name".
1433 "type" should be the type node.
1434 We assume that the tag "name" is not already defined, and has a location
1437 Note that the definition may really be just a forward reference.
1438 In that case, the TYPE_SIZE will be zero. */
1441 pushtag (location_t loc
, tree name
, tree type
)
1443 /* Record the identifier as the type's name if it has none. */
1444 if (name
&& !TYPE_NAME (type
))
1445 TYPE_NAME (type
) = name
;
1446 bind (name
, type
, current_scope
, /*invisible=*/false, /*nested=*/false, loc
);
1448 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1449 tagged type we just added to the current scope. This fake
1450 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1451 to output a representation of a tagged type, and it also gives
1452 us a convenient place to record the "scope start" address for the
1455 TYPE_STUB_DECL (type
) = pushdecl (build_decl (loc
,
1456 TYPE_DECL
, NULL_TREE
, type
));
1458 /* An approximation for now, so we can tell this is a function-scope tag.
1459 This will be updated in pop_scope. */
1460 TYPE_CONTEXT (type
) = DECL_CONTEXT (TYPE_STUB_DECL (type
));
1462 if (warn_cxx_compat
&& name
!= NULL_TREE
)
1464 struct c_binding
*b
= I_SYMBOL_BINDING (name
);
1467 && b
->decl
!= NULL_TREE
1468 && TREE_CODE (b
->decl
) == TYPE_DECL
1469 && (B_IN_CURRENT_SCOPE (b
)
1470 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
1471 && (TYPE_MAIN_VARIANT (TREE_TYPE (b
->decl
))
1472 != TYPE_MAIN_VARIANT (type
)))
1474 warning_at (loc
, OPT_Wc___compat
,
1475 ("using %qD as both a typedef and a tag is "
1478 if (b
->locus
!= UNKNOWN_LOCATION
)
1479 inform (b
->locus
, "originally defined here");
1484 /* Subroutine of compare_decls. Allow harmless mismatches in return
1485 and argument types provided that the type modes match. This function
1486 return a unified type given a suitable match, and 0 otherwise. */
1489 match_builtin_function_types (tree newtype
, tree oldtype
)
1491 tree newrettype
, oldrettype
;
1492 tree newargs
, oldargs
;
1493 tree trytype
, tryargs
;
1495 /* Accept the return type of the new declaration if same modes. */
1496 oldrettype
= TREE_TYPE (oldtype
);
1497 newrettype
= TREE_TYPE (newtype
);
1499 if (TYPE_MODE (oldrettype
) != TYPE_MODE (newrettype
))
1502 oldargs
= TYPE_ARG_TYPES (oldtype
);
1503 newargs
= TYPE_ARG_TYPES (newtype
);
1506 while (oldargs
|| newargs
)
1510 || !TREE_VALUE (oldargs
)
1511 || !TREE_VALUE (newargs
)
1512 || TYPE_MODE (TREE_VALUE (oldargs
))
1513 != TYPE_MODE (TREE_VALUE (newargs
)))
1516 oldargs
= TREE_CHAIN (oldargs
);
1517 newargs
= TREE_CHAIN (newargs
);
1520 trytype
= build_function_type (newrettype
, tryargs
);
1521 return build_type_attribute_variant (trytype
, TYPE_ATTRIBUTES (oldtype
));
1524 /* Subroutine of diagnose_mismatched_decls. Check for function type
1525 mismatch involving an empty arglist vs a nonempty one and give clearer
1528 diagnose_arglist_conflict (tree newdecl
, tree olddecl
,
1529 tree newtype
, tree oldtype
)
1533 if (TREE_CODE (olddecl
) != FUNCTION_DECL
1534 || !comptypes (TREE_TYPE (oldtype
), TREE_TYPE (newtype
))
1535 || !((!prototype_p (oldtype
) && DECL_INITIAL (olddecl
) == 0)
1536 || (!prototype_p (newtype
) && DECL_INITIAL (newdecl
) == 0)))
1539 t
= TYPE_ARG_TYPES (oldtype
);
1541 t
= TYPE_ARG_TYPES (newtype
);
1542 for (; t
; t
= TREE_CHAIN (t
))
1544 tree type
= TREE_VALUE (t
);
1546 if (TREE_CHAIN (t
) == 0
1547 && TYPE_MAIN_VARIANT (type
) != void_type_node
)
1549 inform (input_location
, "a parameter list with an ellipsis can%'t match "
1550 "an empty parameter name list declaration");
1554 if (c_type_promotes_to (type
) != type
)
1556 inform (input_location
, "an argument type that has a default promotion can%'t match "
1557 "an empty parameter name list declaration");
1563 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1564 old-style function definition, NEWDECL is a prototype declaration.
1565 Diagnose inconsistencies in the argument list. Returns TRUE if
1566 the prototype is compatible, FALSE if not. */
1568 validate_proto_after_old_defn (tree newdecl
, tree newtype
, tree oldtype
)
1570 tree newargs
, oldargs
;
1573 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1575 oldargs
= TYPE_ACTUAL_ARG_TYPES (oldtype
);
1576 newargs
= TYPE_ARG_TYPES (newtype
);
1581 tree oldargtype
= TREE_VALUE (oldargs
);
1582 tree newargtype
= TREE_VALUE (newargs
);
1584 if (oldargtype
== error_mark_node
|| newargtype
== error_mark_node
)
1587 oldargtype
= (TYPE_ATOMIC (oldargtype
)
1588 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype
),
1590 : TYPE_MAIN_VARIANT (oldargtype
));
1591 newargtype
= (TYPE_ATOMIC (newargtype
)
1592 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype
),
1594 : TYPE_MAIN_VARIANT (newargtype
));
1596 if (END_OF_ARGLIST (oldargtype
) && END_OF_ARGLIST (newargtype
))
1599 /* Reaching the end of just one list means the two decls don't
1600 agree on the number of arguments. */
1601 if (END_OF_ARGLIST (oldargtype
))
1603 error ("prototype for %q+D declares more arguments "
1604 "than previous old-style definition", newdecl
);
1607 else if (END_OF_ARGLIST (newargtype
))
1609 error ("prototype for %q+D declares fewer arguments "
1610 "than previous old-style definition", newdecl
);
1614 /* Type for passing arg must be consistent with that declared
1616 else if (!comptypes (oldargtype
, newargtype
))
1618 error ("prototype for %q+D declares argument %d"
1619 " with incompatible type",
1624 oldargs
= TREE_CHAIN (oldargs
);
1625 newargs
= TREE_CHAIN (newargs
);
1629 /* If we get here, no errors were found, but do issue a warning
1630 for this poor-style construct. */
1631 warning (0, "prototype for %q+D follows non-prototype definition",
1634 #undef END_OF_ARGLIST
1637 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1638 first in a pair of mismatched declarations, using the diagnostic
1641 locate_old_decl (tree decl
)
1643 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (decl
)
1644 && !C_DECL_DECLARED_BUILTIN (decl
))
1646 else if (DECL_INITIAL (decl
))
1647 inform (input_location
, "previous definition of %q+D was here", decl
);
1648 else if (C_DECL_IMPLICIT (decl
))
1649 inform (input_location
, "previous implicit declaration of %q+D was here", decl
);
1651 inform (input_location
, "previous declaration of %q+D was here", decl
);
1654 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1655 Returns true if the caller should proceed to merge the two, false
1656 if OLDDECL should simply be discarded. As a side effect, issues
1657 all necessary diagnostics for invalid or poor-style combinations.
1658 If it returns true, writes the types of NEWDECL and OLDDECL to
1659 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1660 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1663 diagnose_mismatched_decls (tree newdecl
, tree olddecl
,
1664 tree
*newtypep
, tree
*oldtypep
)
1666 tree newtype
, oldtype
;
1667 bool pedwarned
= false;
1668 bool warned
= false;
1671 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1672 && DECL_EXTERNAL (DECL))
1674 /* If we have error_mark_node for either decl or type, just discard
1675 the previous decl - we're in an error cascade already. */
1676 if (olddecl
== error_mark_node
|| newdecl
== error_mark_node
)
1678 *oldtypep
= oldtype
= TREE_TYPE (olddecl
);
1679 *newtypep
= newtype
= TREE_TYPE (newdecl
);
1680 if (oldtype
== error_mark_node
|| newtype
== error_mark_node
)
1683 /* Two different categories of symbol altogether. This is an error
1684 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1685 if (TREE_CODE (olddecl
) != TREE_CODE (newdecl
))
1687 if (!(TREE_CODE (olddecl
) == FUNCTION_DECL
1688 && DECL_BUILT_IN (olddecl
)
1689 && !C_DECL_DECLARED_BUILTIN (olddecl
)))
1691 error ("%q+D redeclared as different kind of symbol", newdecl
);
1692 locate_old_decl (olddecl
);
1694 else if (TREE_PUBLIC (newdecl
))
1695 warning (0, "built-in function %q+D declared as non-function",
1698 warning (OPT_Wshadow
, "declaration of %q+D shadows "
1699 "a built-in function", newdecl
);
1703 /* Enumerators have no linkage, so may only be declared once in a
1705 if (TREE_CODE (olddecl
) == CONST_DECL
)
1707 error ("redeclaration of enumerator %q+D", newdecl
);
1708 locate_old_decl (olddecl
);
1712 if (!comptypes (oldtype
, newtype
))
1714 if (TREE_CODE (olddecl
) == FUNCTION_DECL
1715 && DECL_BUILT_IN (olddecl
) && !C_DECL_DECLARED_BUILTIN (olddecl
))
1717 /* Accept harmless mismatch in function types.
1718 This is for the ffs and fprintf builtins. */
1719 tree trytype
= match_builtin_function_types (newtype
, oldtype
);
1721 if (trytype
&& comptypes (newtype
, trytype
))
1722 *oldtypep
= oldtype
= trytype
;
1725 /* If types don't match for a built-in, throw away the
1726 built-in. No point in calling locate_old_decl here, it
1727 won't print anything. */
1728 warning (0, "conflicting types for built-in function %q+D",
1733 else if (TREE_CODE (olddecl
) == FUNCTION_DECL
1734 && DECL_IS_BUILTIN (olddecl
))
1736 /* A conflicting function declaration for a predeclared
1737 function that isn't actually built in. Objective C uses
1738 these. The new declaration silently overrides everything
1739 but the volatility (i.e. noreturn) indication. See also
1740 below. FIXME: Make Objective C use normal builtins. */
1741 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
1744 /* Permit void foo (...) to match int foo (...) if the latter is
1745 the definition and implicit int was used. See
1746 c-torture/compile/920625-2.c. */
1747 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
&& DECL_INITIAL (newdecl
)
1748 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype
)) == void_type_node
1749 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype
)) == integer_type_node
1750 && C_FUNCTION_IMPLICIT_INT (newdecl
) && !DECL_INITIAL (olddecl
))
1752 pedwarned
= pedwarn (input_location
, 0,
1753 "conflicting types for %q+D", newdecl
);
1754 /* Make sure we keep void as the return type. */
1755 TREE_TYPE (newdecl
) = *newtypep
= newtype
= oldtype
;
1756 C_FUNCTION_IMPLICIT_INT (newdecl
) = 0;
1758 /* Permit void foo (...) to match an earlier call to foo (...) with
1759 no declared type (thus, implicitly int). */
1760 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
1761 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype
)) == void_type_node
1762 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype
)) == integer_type_node
1763 && C_DECL_IMPLICIT (olddecl
) && !DECL_INITIAL (olddecl
))
1765 pedwarned
= pedwarn (input_location
, 0,
1766 "conflicting types for %q+D", newdecl
);
1767 /* Make sure we keep void as the return type. */
1768 TREE_TYPE (olddecl
) = *oldtypep
= oldtype
= newtype
;
1772 int new_quals
= TYPE_QUALS (newtype
);
1773 int old_quals
= TYPE_QUALS (oldtype
);
1775 if (new_quals
!= old_quals
)
1777 addr_space_t new_addr
= DECODE_QUAL_ADDR_SPACE (new_quals
);
1778 addr_space_t old_addr
= DECODE_QUAL_ADDR_SPACE (old_quals
);
1779 if (new_addr
!= old_addr
)
1781 if (ADDR_SPACE_GENERIC_P (new_addr
))
1782 error ("conflicting named address spaces (generic vs %s) "
1784 c_addr_space_name (old_addr
), newdecl
);
1785 else if (ADDR_SPACE_GENERIC_P (old_addr
))
1786 error ("conflicting named address spaces (%s vs generic) "
1788 c_addr_space_name (new_addr
), newdecl
);
1790 error ("conflicting named address spaces (%s vs %s) "
1792 c_addr_space_name (new_addr
),
1793 c_addr_space_name (old_addr
),
1797 if (CLEAR_QUAL_ADDR_SPACE (new_quals
)
1798 != CLEAR_QUAL_ADDR_SPACE (old_quals
))
1799 error ("conflicting type qualifiers for %q+D", newdecl
);
1802 error ("conflicting types for %q+D", newdecl
);
1803 diagnose_arglist_conflict (newdecl
, olddecl
, newtype
, oldtype
);
1804 locate_old_decl (olddecl
);
1809 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1810 but silently ignore the redeclaration if either is in a system
1811 header. (Conflicting redeclarations were handled above.) This
1812 is allowed for C11 if the types are the same, not just
1814 if (TREE_CODE (newdecl
) == TYPE_DECL
)
1816 bool types_different
= false;
1817 int comptypes_result
;
1820 = comptypes_check_different_types (oldtype
, newtype
, &types_different
);
1822 if (comptypes_result
!= 1 || types_different
)
1824 error ("redefinition of typedef %q+D with different type", newdecl
);
1825 locate_old_decl (olddecl
);
1829 if (DECL_IN_SYSTEM_HEADER (newdecl
)
1830 || DECL_IN_SYSTEM_HEADER (olddecl
)
1831 || TREE_NO_WARNING (newdecl
)
1832 || TREE_NO_WARNING (olddecl
))
1833 return true; /* Allow OLDDECL to continue in use. */
1835 if (variably_modified_type_p (newtype
, NULL
))
1837 error ("redefinition of typedef %q+D with variably modified type",
1839 locate_old_decl (olddecl
);
1841 else if (pedantic
&& !flag_isoc11
)
1843 pedwarn (input_location
, OPT_Wpedantic
,
1844 "redefinition of typedef %q+D", newdecl
);
1845 locate_old_decl (olddecl
);
1851 /* Function declarations can either be 'static' or 'extern' (no
1852 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1853 can never conflict with each other on account of linkage
1854 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1855 gnu89 mode permits two definitions if one is 'extern inline' and
1856 one is not. The non- extern-inline definition supersedes the
1857 extern-inline definition. */
1859 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1861 /* If you declare a built-in function name as static, or
1862 define the built-in with an old-style definition (so we
1863 can't validate the argument list) the built-in definition is
1864 overridden, but optionally warn this was a bad choice of name. */
1865 if (DECL_BUILT_IN (olddecl
)
1866 && !C_DECL_DECLARED_BUILTIN (olddecl
)
1867 && (!TREE_PUBLIC (newdecl
)
1868 || (DECL_INITIAL (newdecl
)
1869 && !prototype_p (TREE_TYPE (newdecl
)))))
1871 warning (OPT_Wshadow
, "declaration of %q+D shadows "
1872 "a built-in function", newdecl
);
1873 /* Discard the old built-in function. */
1877 if (DECL_INITIAL (newdecl
))
1879 if (DECL_INITIAL (olddecl
))
1881 /* If both decls are in the same TU and the new declaration
1882 isn't overriding an extern inline reject the new decl.
1883 In c99, no overriding is allowed in the same translation
1885 if ((!DECL_EXTERN_INLINE (olddecl
)
1886 || DECL_EXTERN_INLINE (newdecl
)
1887 || (!flag_gnu89_inline
1888 && (!DECL_DECLARED_INLINE_P (olddecl
)
1889 || !lookup_attribute ("gnu_inline",
1890 DECL_ATTRIBUTES (olddecl
)))
1891 && (!DECL_DECLARED_INLINE_P (newdecl
)
1892 || !lookup_attribute ("gnu_inline",
1893 DECL_ATTRIBUTES (newdecl
))))
1895 && same_translation_unit_p (newdecl
, olddecl
))
1897 error ("redefinition of %q+D", newdecl
);
1898 locate_old_decl (olddecl
);
1903 /* If we have a prototype after an old-style function definition,
1904 the argument types must be checked specially. */
1905 else if (DECL_INITIAL (olddecl
)
1906 && !prototype_p (oldtype
) && prototype_p (newtype
)
1907 && TYPE_ACTUAL_ARG_TYPES (oldtype
)
1908 && !validate_proto_after_old_defn (newdecl
, newtype
, oldtype
))
1910 locate_old_decl (olddecl
);
1913 /* A non-static declaration (even an "extern") followed by a
1914 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1915 The same is true for a static forward declaration at block
1916 scope followed by a non-static declaration/definition at file
1917 scope. Static followed by non-static at the same scope is
1918 not undefined behavior, and is the most convenient way to get
1919 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1920 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1921 we do diagnose it if -Wtraditional. */
1922 if (TREE_PUBLIC (olddecl
) && !TREE_PUBLIC (newdecl
))
1924 /* Two exceptions to the rule. If olddecl is an extern
1925 inline, or a predeclared function that isn't actually
1926 built in, newdecl silently overrides olddecl. The latter
1927 occur only in Objective C; see also above. (FIXME: Make
1928 Objective C use normal builtins.) */
1929 if (!DECL_IS_BUILTIN (olddecl
)
1930 && !DECL_EXTERN_INLINE (olddecl
))
1932 error ("static declaration of %q+D follows "
1933 "non-static declaration", newdecl
);
1934 locate_old_decl (olddecl
);
1938 else if (TREE_PUBLIC (newdecl
) && !TREE_PUBLIC (olddecl
))
1940 if (DECL_CONTEXT (olddecl
))
1942 error ("non-static declaration of %q+D follows "
1943 "static declaration", newdecl
);
1944 locate_old_decl (olddecl
);
1947 else if (warn_traditional
)
1949 warned
|= warning (OPT_Wtraditional
,
1950 "non-static declaration of %q+D "
1951 "follows static declaration", newdecl
);
1955 /* Make sure gnu_inline attribute is either not present, or
1956 present on all inline decls. */
1957 if (DECL_DECLARED_INLINE_P (olddecl
)
1958 && DECL_DECLARED_INLINE_P (newdecl
))
1960 bool newa
= lookup_attribute ("gnu_inline",
1961 DECL_ATTRIBUTES (newdecl
)) != NULL
;
1962 bool olda
= lookup_attribute ("gnu_inline",
1963 DECL_ATTRIBUTES (olddecl
)) != NULL
;
1966 error_at (input_location
, "%<gnu_inline%> attribute present on %q+D",
1967 newa
? newdecl
: olddecl
);
1968 error_at (DECL_SOURCE_LOCATION (newa
? olddecl
: newdecl
),
1973 else if (TREE_CODE (newdecl
) == VAR_DECL
)
1975 /* Only variables can be thread-local, and all declarations must
1976 agree on this property. */
1977 if (C_DECL_THREADPRIVATE_P (olddecl
) && !DECL_THREAD_LOCAL_P (newdecl
))
1979 /* Nothing to check. Since OLDDECL is marked threadprivate
1980 and NEWDECL does not have a thread-local attribute, we
1981 will merge the threadprivate attribute into NEWDECL. */
1984 else if (DECL_THREAD_LOCAL_P (newdecl
) != DECL_THREAD_LOCAL_P (olddecl
))
1986 if (DECL_THREAD_LOCAL_P (newdecl
))
1987 error ("thread-local declaration of %q+D follows "
1988 "non-thread-local declaration", newdecl
);
1990 error ("non-thread-local declaration of %q+D follows "
1991 "thread-local declaration", newdecl
);
1993 locate_old_decl (olddecl
);
1997 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1998 if (DECL_INITIAL (newdecl
) && DECL_INITIAL (olddecl
))
2000 error ("redefinition of %q+D", newdecl
);
2001 locate_old_decl (olddecl
);
2005 /* Objects declared at file scope: if the first declaration had
2006 external linkage (even if it was an external reference) the
2007 second must have external linkage as well, or the behavior is
2008 undefined. If the first declaration had internal linkage, then
2009 the second must too, or else be an external reference (in which
2010 case the composite declaration still has internal linkage).
2011 As for function declarations, we warn about the static-then-
2012 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2013 if (DECL_FILE_SCOPE_P (newdecl
)
2014 && TREE_PUBLIC (newdecl
) != TREE_PUBLIC (olddecl
))
2016 if (DECL_EXTERNAL (newdecl
))
2018 if (!DECL_FILE_SCOPE_P (olddecl
))
2020 error ("extern declaration of %q+D follows "
2021 "declaration with no linkage", newdecl
);
2022 locate_old_decl (olddecl
);
2025 else if (warn_traditional
)
2027 warned
|= warning (OPT_Wtraditional
,
2028 "non-static declaration of %q+D "
2029 "follows static declaration", newdecl
);
2034 if (TREE_PUBLIC (newdecl
))
2035 error ("non-static declaration of %q+D follows "
2036 "static declaration", newdecl
);
2038 error ("static declaration of %q+D follows "
2039 "non-static declaration", newdecl
);
2041 locate_old_decl (olddecl
);
2045 /* Two objects with the same name declared at the same block
2046 scope must both be external references (6.7p3). */
2047 else if (!DECL_FILE_SCOPE_P (newdecl
))
2049 if (DECL_EXTERNAL (newdecl
))
2051 /* Extern with initializer at block scope, which will
2052 already have received an error. */
2054 else if (DECL_EXTERNAL (olddecl
))
2056 error ("declaration of %q+D with no linkage follows "
2057 "extern declaration", newdecl
);
2058 locate_old_decl (olddecl
);
2062 error ("redeclaration of %q+D with no linkage", newdecl
);
2063 locate_old_decl (olddecl
);
2069 /* C++ does not permit a decl to appear multiple times at file
2072 && DECL_FILE_SCOPE_P (newdecl
)
2073 && !DECL_EXTERNAL (newdecl
)
2074 && !DECL_EXTERNAL (olddecl
))
2075 warned
|= warning_at (DECL_SOURCE_LOCATION (newdecl
),
2077 ("duplicate declaration of %qD is "
2083 /* All decls must agree on a visibility. */
2084 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl
), TS_DECL_WITH_VIS
)
2085 && DECL_VISIBILITY_SPECIFIED (newdecl
) && DECL_VISIBILITY_SPECIFIED (olddecl
)
2086 && DECL_VISIBILITY (newdecl
) != DECL_VISIBILITY (olddecl
))
2088 warned
|= warning (0, "redeclaration of %q+D with different visibility "
2089 "(old visibility preserved)", newdecl
);
2092 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2094 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2095 if (DECL_DECLARED_INLINE_P (newdecl
)
2096 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl
)))
2098 warned
|= warning (OPT_Wattributes
,
2099 "inline declaration of %qD follows "
2100 "declaration with attribute noinline", newdecl
);
2102 else if (DECL_DECLARED_INLINE_P (olddecl
)
2103 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl
)))
2105 warned
|= warning (OPT_Wattributes
,
2106 "declaration of %q+D with attribute "
2107 "noinline follows inline declaration ", newdecl
);
2110 else /* PARM_DECL, VAR_DECL */
2112 /* Redeclaration of a parameter is a constraint violation (this is
2113 not explicitly stated, but follows from C99 6.7p3 [no more than
2114 one declaration of the same identifier with no linkage in the
2115 same scope, except type tags] and 6.2.2p6 [parameters have no
2116 linkage]). We must check for a forward parameter declaration,
2117 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2118 an extension, the mandatory diagnostic for which is handled by
2119 mark_forward_parm_decls. */
2121 if (TREE_CODE (newdecl
) == PARM_DECL
2122 && (!TREE_ASM_WRITTEN (olddecl
) || TREE_ASM_WRITTEN (newdecl
)))
2124 error ("redefinition of parameter %q+D", newdecl
);
2125 locate_old_decl (olddecl
);
2130 /* Optional warning for completely redundant decls. */
2131 if (!warned
&& !pedwarned
2132 && warn_redundant_decls
2133 /* Don't warn about a function declaration followed by a
2135 && !(TREE_CODE (newdecl
) == FUNCTION_DECL
2136 && DECL_INITIAL (newdecl
) && !DECL_INITIAL (olddecl
))
2137 /* Don't warn about redundant redeclarations of builtins. */
2138 && !(TREE_CODE (newdecl
) == FUNCTION_DECL
2139 && !DECL_BUILT_IN (newdecl
)
2140 && DECL_BUILT_IN (olddecl
)
2141 && !C_DECL_DECLARED_BUILTIN (olddecl
))
2142 /* Don't warn about an extern followed by a definition. */
2143 && !(DECL_EXTERNAL (olddecl
) && !DECL_EXTERNAL (newdecl
))
2144 /* Don't warn about forward parameter decls. */
2145 && !(TREE_CODE (newdecl
) == PARM_DECL
2146 && TREE_ASM_WRITTEN (olddecl
) && !TREE_ASM_WRITTEN (newdecl
))
2147 /* Don't warn about a variable definition following a declaration. */
2148 && !(TREE_CODE (newdecl
) == VAR_DECL
2149 && DECL_INITIAL (newdecl
) && !DECL_INITIAL (olddecl
)))
2151 warned
= warning (OPT_Wredundant_decls
, "redundant redeclaration of %q+D",
2155 /* Report location of previous decl/defn. */
2156 if (warned
|| pedwarned
)
2157 locate_old_decl (olddecl
);
2159 #undef DECL_EXTERN_INLINE
2164 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2165 consistent with OLDDECL, but carries new information. Merge the
2166 new information into OLDDECL. This function issues no
2170 merge_decls (tree newdecl
, tree olddecl
, tree newtype
, tree oldtype
)
2172 bool new_is_definition
= (TREE_CODE (newdecl
) == FUNCTION_DECL
2173 && DECL_INITIAL (newdecl
) != 0);
2174 bool new_is_prototype
= (TREE_CODE (newdecl
) == FUNCTION_DECL
2175 && prototype_p (TREE_TYPE (newdecl
)));
2176 bool old_is_prototype
= (TREE_CODE (olddecl
) == FUNCTION_DECL
2177 && prototype_p (TREE_TYPE (olddecl
)));
2179 /* For real parm decl following a forward decl, rechain the old decl
2180 in its new location and clear TREE_ASM_WRITTEN (it's not a
2181 forward decl anymore). */
2182 if (TREE_CODE (newdecl
) == PARM_DECL
2183 && TREE_ASM_WRITTEN (olddecl
) && !TREE_ASM_WRITTEN (newdecl
))
2185 struct c_binding
*b
, **here
;
2187 for (here
= ¤t_scope
->bindings
; *here
; here
= &(*here
)->prev
)
2188 if ((*here
)->decl
== olddecl
)
2195 b
->prev
= current_scope
->bindings
;
2196 current_scope
->bindings
= b
;
2198 TREE_ASM_WRITTEN (olddecl
) = 0;
2201 DECL_ATTRIBUTES (newdecl
)
2202 = targetm
.merge_decl_attributes (olddecl
, newdecl
);
2204 /* Merge the data types specified in the two decls. */
2206 = TREE_TYPE (olddecl
)
2207 = composite_type (newtype
, oldtype
);
2209 /* Lay the type out, unless already done. */
2210 if (!comptypes (oldtype
, TREE_TYPE (newdecl
)))
2212 if (TREE_TYPE (newdecl
) != error_mark_node
)
2213 layout_type (TREE_TYPE (newdecl
));
2214 if (TREE_CODE (newdecl
) != FUNCTION_DECL
2215 && TREE_CODE (newdecl
) != TYPE_DECL
2216 && TREE_CODE (newdecl
) != CONST_DECL
)
2217 layout_decl (newdecl
, 0);
2221 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2222 DECL_SIZE (newdecl
) = DECL_SIZE (olddecl
);
2223 DECL_SIZE_UNIT (newdecl
) = DECL_SIZE_UNIT (olddecl
);
2224 DECL_MODE (newdecl
) = DECL_MODE (olddecl
);
2225 if (DECL_ALIGN (olddecl
) > DECL_ALIGN (newdecl
))
2227 DECL_ALIGN (newdecl
) = DECL_ALIGN (olddecl
);
2228 DECL_USER_ALIGN (newdecl
) |= DECL_USER_ALIGN (olddecl
);
2232 /* Keep the old rtl since we can safely use it. */
2233 if (HAS_RTL_P (olddecl
))
2234 COPY_DECL_RTL (olddecl
, newdecl
);
2236 /* Merge the type qualifiers. */
2237 if (TREE_READONLY (newdecl
))
2238 TREE_READONLY (olddecl
) = 1;
2240 if (TREE_THIS_VOLATILE (newdecl
))
2241 TREE_THIS_VOLATILE (olddecl
) = 1;
2243 /* Merge deprecatedness. */
2244 if (TREE_DEPRECATED (newdecl
))
2245 TREE_DEPRECATED (olddecl
) = 1;
2247 /* If a decl is in a system header and the other isn't, keep the one on the
2248 system header. Otherwise, keep source location of definition rather than
2249 declaration and of prototype rather than non-prototype unless that
2250 prototype is built-in. */
2251 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl
), TS_DECL_WITH_VIS
)
2252 && DECL_IN_SYSTEM_HEADER (olddecl
)
2253 && !DECL_IN_SYSTEM_HEADER (newdecl
) )
2254 DECL_SOURCE_LOCATION (newdecl
) = DECL_SOURCE_LOCATION (olddecl
);
2255 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl
), TS_DECL_WITH_VIS
)
2256 && DECL_IN_SYSTEM_HEADER (newdecl
)
2257 && !DECL_IN_SYSTEM_HEADER (olddecl
))
2258 DECL_SOURCE_LOCATION (olddecl
) = DECL_SOURCE_LOCATION (newdecl
);
2259 else if ((DECL_INITIAL (newdecl
) == 0 && DECL_INITIAL (olddecl
) != 0)
2260 || (old_is_prototype
&& !new_is_prototype
2261 && !C_DECL_BUILTIN_PROTOTYPE (olddecl
)))
2262 DECL_SOURCE_LOCATION (newdecl
) = DECL_SOURCE_LOCATION (olddecl
);
2264 /* Merge the initialization information. */
2265 if (DECL_INITIAL (newdecl
) == 0)
2266 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
2268 /* Merge the threadprivate attribute. */
2269 if (TREE_CODE (olddecl
) == VAR_DECL
&& C_DECL_THREADPRIVATE_P (olddecl
))
2271 DECL_TLS_MODEL (newdecl
) = DECL_TLS_MODEL (olddecl
);
2272 C_DECL_THREADPRIVATE_P (newdecl
) = 1;
2275 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl
), TS_DECL_WITH_VIS
))
2277 /* Merge the section attribute.
2278 We want to issue an error if the sections conflict but that
2279 must be done later in decl_attributes since we are called
2280 before attributes are assigned. */
2281 if (DECL_SECTION_NAME (newdecl
) == NULL_TREE
)
2282 DECL_SECTION_NAME (newdecl
) = DECL_SECTION_NAME (olddecl
);
2284 /* Copy the assembler name.
2285 Currently, it can only be defined in the prototype. */
2286 COPY_DECL_ASSEMBLER_NAME (olddecl
, newdecl
);
2288 /* Use visibility of whichever declaration had it specified */
2289 if (DECL_VISIBILITY_SPECIFIED (olddecl
))
2291 DECL_VISIBILITY (newdecl
) = DECL_VISIBILITY (olddecl
);
2292 DECL_VISIBILITY_SPECIFIED (newdecl
) = 1;
2295 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2297 DECL_STATIC_CONSTRUCTOR(newdecl
) |= DECL_STATIC_CONSTRUCTOR(olddecl
);
2298 DECL_STATIC_DESTRUCTOR (newdecl
) |= DECL_STATIC_DESTRUCTOR (olddecl
);
2299 DECL_NO_LIMIT_STACK (newdecl
) |= DECL_NO_LIMIT_STACK (olddecl
);
2300 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl
)
2301 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl
);
2302 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
2303 DECL_IS_MALLOC (newdecl
) |= DECL_IS_MALLOC (olddecl
);
2304 DECL_IS_OPERATOR_NEW (newdecl
) |= DECL_IS_OPERATOR_NEW (olddecl
);
2305 TREE_READONLY (newdecl
) |= TREE_READONLY (olddecl
);
2306 DECL_PURE_P (newdecl
) |= DECL_PURE_P (olddecl
);
2307 DECL_IS_NOVOPS (newdecl
) |= DECL_IS_NOVOPS (olddecl
);
2310 /* Merge the storage class information. */
2311 merge_weak (newdecl
, olddecl
);
2313 /* For functions, static overrides non-static. */
2314 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2316 TREE_PUBLIC (newdecl
) &= TREE_PUBLIC (olddecl
);
2317 /* This is since we don't automatically
2318 copy the attributes of NEWDECL into OLDDECL. */
2319 TREE_PUBLIC (olddecl
) = TREE_PUBLIC (newdecl
);
2320 /* If this clears `static', clear it in the identifier too. */
2321 if (!TREE_PUBLIC (olddecl
))
2322 TREE_PUBLIC (DECL_NAME (olddecl
)) = 0;
2326 /* In c99, 'extern' declaration before (or after) 'inline' means this
2327 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2329 if (TREE_CODE (newdecl
) == FUNCTION_DECL
2330 && !flag_gnu89_inline
2331 && (DECL_DECLARED_INLINE_P (newdecl
)
2332 || DECL_DECLARED_INLINE_P (olddecl
))
2333 && (!DECL_DECLARED_INLINE_P (newdecl
)
2334 || !DECL_DECLARED_INLINE_P (olddecl
)
2335 || !DECL_EXTERNAL (olddecl
))
2336 && DECL_EXTERNAL (newdecl
)
2337 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl
))
2338 && !current_function_decl
)
2339 DECL_EXTERNAL (newdecl
) = 0;
2341 if (DECL_EXTERNAL (newdecl
))
2343 TREE_STATIC (newdecl
) = TREE_STATIC (olddecl
);
2344 DECL_EXTERNAL (newdecl
) = DECL_EXTERNAL (olddecl
);
2346 /* An extern decl does not override previous storage class. */
2347 TREE_PUBLIC (newdecl
) = TREE_PUBLIC (olddecl
);
2348 if (!DECL_EXTERNAL (newdecl
))
2350 DECL_CONTEXT (newdecl
) = DECL_CONTEXT (olddecl
);
2351 DECL_COMMON (newdecl
) = DECL_COMMON (olddecl
);
2356 TREE_STATIC (olddecl
) = TREE_STATIC (newdecl
);
2357 TREE_PUBLIC (olddecl
) = TREE_PUBLIC (newdecl
);
2360 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2362 /* If we're redefining a function previously defined as extern
2363 inline, make sure we emit debug info for the inline before we
2364 throw it away, in case it was inlined into a function that
2365 hasn't been written out yet. */
2366 if (new_is_definition
&& DECL_INITIAL (olddecl
))
2367 /* The new defn must not be inline. */
2368 DECL_UNINLINABLE (newdecl
) = 1;
2371 /* If either decl says `inline', this fn is inline, unless
2372 its definition was passed already. */
2373 if (DECL_DECLARED_INLINE_P (newdecl
)
2374 || DECL_DECLARED_INLINE_P (olddecl
))
2375 DECL_DECLARED_INLINE_P (newdecl
) = 1;
2377 DECL_UNINLINABLE (newdecl
) = DECL_UNINLINABLE (olddecl
)
2378 = (DECL_UNINLINABLE (newdecl
) || DECL_UNINLINABLE (olddecl
));
2380 DECL_DISREGARD_INLINE_LIMITS (newdecl
)
2381 = DECL_DISREGARD_INLINE_LIMITS (olddecl
)
2382 = (DECL_DISREGARD_INLINE_LIMITS (newdecl
)
2383 || DECL_DISREGARD_INLINE_LIMITS (olddecl
));
2386 if (DECL_BUILT_IN (olddecl
))
2388 /* If redeclaring a builtin function, it stays built in.
2389 But it gets tagged as having been declared. */
2390 DECL_BUILT_IN_CLASS (newdecl
) = DECL_BUILT_IN_CLASS (olddecl
);
2391 DECL_FUNCTION_CODE (newdecl
) = DECL_FUNCTION_CODE (olddecl
);
2392 C_DECL_DECLARED_BUILTIN (newdecl
) = 1;
2393 if (new_is_prototype
)
2395 C_DECL_BUILTIN_PROTOTYPE (newdecl
) = 0;
2396 if (DECL_BUILT_IN_CLASS (newdecl
) == BUILT_IN_NORMAL
)
2398 enum built_in_function fncode
= DECL_FUNCTION_CODE (newdecl
);
2401 /* If a compatible prototype of these builtin functions
2402 is seen, assume the runtime implements it with the
2403 expected semantics. */
2404 case BUILT_IN_STPCPY
:
2405 if (builtin_decl_explicit_p (fncode
))
2406 set_builtin_decl_implicit_p (fncode
, true);
2414 C_DECL_BUILTIN_PROTOTYPE (newdecl
)
2415 = C_DECL_BUILTIN_PROTOTYPE (olddecl
);
2418 /* Preserve function specific target and optimization options */
2419 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl
)
2420 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl
))
2421 DECL_FUNCTION_SPECIFIC_TARGET (newdecl
)
2422 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl
);
2424 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
)
2425 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
))
2426 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
)
2427 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
);
2429 /* Also preserve various other info from the definition. */
2430 if (!new_is_definition
)
2433 DECL_RESULT (newdecl
) = DECL_RESULT (olddecl
);
2434 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
2435 DECL_STRUCT_FUNCTION (newdecl
) = DECL_STRUCT_FUNCTION (olddecl
);
2436 DECL_SAVED_TREE (newdecl
) = DECL_SAVED_TREE (olddecl
);
2437 DECL_ARGUMENTS (newdecl
) = copy_list (DECL_ARGUMENTS (olddecl
));
2438 for (t
= DECL_ARGUMENTS (newdecl
); t
; t
= DECL_CHAIN (t
))
2439 DECL_CONTEXT (t
) = newdecl
;
2441 /* See if we've got a function to instantiate from. */
2442 if (DECL_SAVED_TREE (olddecl
))
2443 DECL_ABSTRACT_ORIGIN (newdecl
)
2444 = DECL_ABSTRACT_ORIGIN (olddecl
);
2448 /* Merge the USED information. */
2449 if (TREE_USED (olddecl
))
2450 TREE_USED (newdecl
) = 1;
2451 else if (TREE_USED (newdecl
))
2452 TREE_USED (olddecl
) = 1;
2453 if (TREE_CODE (olddecl
) == VAR_DECL
|| TREE_CODE (olddecl
) == PARM_DECL
)
2454 DECL_READ_P (newdecl
) |= DECL_READ_P (olddecl
);
2455 if (DECL_PRESERVE_P (olddecl
))
2456 DECL_PRESERVE_P (newdecl
) = 1;
2457 else if (DECL_PRESERVE_P (newdecl
))
2458 DECL_PRESERVE_P (olddecl
) = 1;
2460 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2461 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2462 DECL_ARGUMENTS (if appropriate). */
2464 unsigned olddecl_uid
= DECL_UID (olddecl
);
2465 tree olddecl_context
= DECL_CONTEXT (olddecl
);
2466 tree olddecl_arguments
= NULL
;
2467 if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
2468 olddecl_arguments
= DECL_ARGUMENTS (olddecl
);
2470 memcpy ((char *) olddecl
+ sizeof (struct tree_common
),
2471 (char *) newdecl
+ sizeof (struct tree_common
),
2472 sizeof (struct tree_decl_common
) - sizeof (struct tree_common
));
2473 DECL_USER_ALIGN (olddecl
) = DECL_USER_ALIGN (newdecl
);
2474 switch (TREE_CODE (olddecl
))
2484 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2485 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2486 tree_code_size (TREE_CODE (olddecl
)) - sizeof (struct tree_decl_common
));
2491 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2492 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2493 sizeof (struct tree_decl_non_common
) - sizeof (struct tree_decl_common
));
2495 DECL_UID (olddecl
) = olddecl_uid
;
2496 DECL_CONTEXT (olddecl
) = olddecl_context
;
2497 if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
2498 DECL_ARGUMENTS (olddecl
) = olddecl_arguments
;
2501 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2502 so that encode_section_info has a chance to look at the new decl
2503 flags and attributes. */
2504 if (DECL_RTL_SET_P (olddecl
)
2505 && (TREE_CODE (olddecl
) == FUNCTION_DECL
2506 || (TREE_CODE (olddecl
) == VAR_DECL
2507 && TREE_STATIC (olddecl
))))
2508 make_decl_rtl (olddecl
);
2511 /* Handle when a new declaration NEWDECL has the same name as an old
2512 one OLDDECL in the same binding contour. Prints an error message
2515 If safely possible, alter OLDDECL to look like NEWDECL, and return
2516 true. Otherwise, return false. */
2519 duplicate_decls (tree newdecl
, tree olddecl
)
2521 tree newtype
= NULL
, oldtype
= NULL
;
2523 if (!diagnose_mismatched_decls (newdecl
, olddecl
, &newtype
, &oldtype
))
2525 /* Avoid `unused variable' and other warnings for OLDDECL. */
2526 TREE_NO_WARNING (olddecl
) = 1;
2530 merge_decls (newdecl
, olddecl
, newtype
, oldtype
);
2535 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2537 warn_if_shadowing (tree new_decl
)
2539 struct c_binding
*b
;
2541 /* Shadow warnings wanted? */
2543 /* No shadow warnings for internally generated vars. */
2544 || DECL_IS_BUILTIN (new_decl
)
2545 /* No shadow warnings for vars made for inlining. */
2546 || DECL_FROM_INLINE (new_decl
))
2549 /* Is anything being shadowed? Invisible decls do not count. */
2550 for (b
= I_SYMBOL_BINDING (DECL_NAME (new_decl
)); b
; b
= b
->shadowed
)
2551 if (b
->decl
&& b
->decl
!= new_decl
&& !b
->invisible
2552 && (b
->decl
== error_mark_node
2553 || diagnostic_report_warnings_p (global_dc
,
2554 DECL_SOURCE_LOCATION (b
->decl
))))
2556 tree old_decl
= b
->decl
;
2558 if (old_decl
== error_mark_node
)
2560 warning (OPT_Wshadow
, "declaration of %q+D shadows previous "
2561 "non-variable", new_decl
);
2564 else if (TREE_CODE (old_decl
) == PARM_DECL
)
2565 warning (OPT_Wshadow
, "declaration of %q+D shadows a parameter",
2567 else if (DECL_FILE_SCOPE_P (old_decl
))
2569 /* Do not warn if a variable shadows a function, unless
2570 the variable is a function or a pointer-to-function. */
2571 if (TREE_CODE (old_decl
) == FUNCTION_DECL
2572 && TREE_CODE (new_decl
) != FUNCTION_DECL
2573 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl
)))
2576 warning_at (DECL_SOURCE_LOCATION (new_decl
), OPT_Wshadow
,
2577 "declaration of %qD shadows a global declaration",
2580 else if (TREE_CODE (old_decl
) == FUNCTION_DECL
2581 && DECL_BUILT_IN (old_decl
))
2583 warning (OPT_Wshadow
, "declaration of %q+D shadows "
2584 "a built-in function", new_decl
);
2588 warning (OPT_Wshadow
, "declaration of %q+D shadows a previous local",
2591 warning_at (DECL_SOURCE_LOCATION (old_decl
), OPT_Wshadow
,
2592 "shadowed declaration is here");
2598 /* Record a decl-node X as belonging to the current lexical scope.
2599 Check for errors (such as an incompatible declaration for the same
2600 name already seen in the same scope).
2602 Returns either X or an old decl for the same name.
2603 If an old decl is returned, it may have been smashed
2604 to agree with what X says. */
2609 tree name
= DECL_NAME (x
);
2610 struct c_scope
*scope
= current_scope
;
2611 struct c_binding
*b
;
2612 bool nested
= false;
2613 location_t locus
= DECL_SOURCE_LOCATION (x
);
2615 /* Must set DECL_CONTEXT for everything not at file scope or
2616 DECL_FILE_SCOPE_P won't work. Local externs don't count
2617 unless they have initializers (which generate code). */
2618 if (current_function_decl
2619 && ((TREE_CODE (x
) != FUNCTION_DECL
&& TREE_CODE (x
) != VAR_DECL
)
2620 || DECL_INITIAL (x
) || !DECL_EXTERNAL (x
)))
2621 DECL_CONTEXT (x
) = current_function_decl
;
2623 /* Anonymous decls are just inserted in the scope. */
2626 bind (name
, x
, scope
, /*invisible=*/false, /*nested=*/false,
2631 /* First, see if there is another declaration with the same name in
2632 the current scope. If there is, duplicate_decls may do all the
2633 work for us. If duplicate_decls returns false, that indicates
2634 two incompatible decls in the same scope; we are to silently
2635 replace the old one (duplicate_decls has issued all appropriate
2636 diagnostics). In particular, we should not consider possible
2637 duplicates in the external scope, or shadowing. */
2638 b
= I_SYMBOL_BINDING (name
);
2639 if (b
&& B_IN_SCOPE (b
, scope
))
2641 struct c_binding
*b_ext
, *b_use
;
2642 tree type
= TREE_TYPE (x
);
2643 tree visdecl
= b
->decl
;
2644 tree vistype
= TREE_TYPE (visdecl
);
2645 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
2646 && COMPLETE_TYPE_P (TREE_TYPE (x
)))
2647 b
->inner_comp
= false;
2650 /* If this is an external linkage declaration, we should check
2651 for compatibility with the type in the external scope before
2652 setting the type at this scope based on the visible
2653 information only. */
2654 if (TREE_PUBLIC (x
) && TREE_PUBLIC (visdecl
))
2656 while (b_ext
&& !B_IN_EXTERNAL_SCOPE (b_ext
))
2657 b_ext
= b_ext
->shadowed
;
2662 TREE_TYPE (b_use
->decl
) = b_use
->u
.type
;
2665 if (duplicate_decls (x
, b_use
->decl
))
2669 /* Save the updated type in the external scope and
2670 restore the proper type for this scope. */
2672 if (comptypes (vistype
, type
))
2673 thistype
= composite_type (vistype
, type
);
2675 thistype
= TREE_TYPE (b_use
->decl
);
2676 b_use
->u
.type
= TREE_TYPE (b_use
->decl
);
2677 if (TREE_CODE (b_use
->decl
) == FUNCTION_DECL
2678 && DECL_BUILT_IN (b_use
->decl
))
2680 = build_type_attribute_variant (thistype
,
2683 TREE_TYPE (b_use
->decl
) = thistype
;
2688 goto skip_external_and_shadow_checks
;
2691 /* All declarations with external linkage, and all external
2692 references, go in the external scope, no matter what scope is
2693 current. However, the binding in that scope is ignored for
2694 purposes of normal name lookup. A separate binding structure is
2695 created in the requested scope; this governs the normal
2696 visibility of the symbol.
2698 The binding in the externals scope is used exclusively for
2699 detecting duplicate declarations of the same object, no matter
2700 what scope they are in; this is what we do here. (C99 6.2.7p2:
2701 All declarations that refer to the same object or function shall
2702 have compatible type; otherwise, the behavior is undefined.) */
2703 if (DECL_EXTERNAL (x
) || scope
== file_scope
)
2705 tree type
= TREE_TYPE (x
);
2708 bool type_saved
= false;
2709 if (b
&& !B_IN_EXTERNAL_SCOPE (b
)
2710 && (TREE_CODE (b
->decl
) == FUNCTION_DECL
2711 || TREE_CODE (b
->decl
) == VAR_DECL
)
2712 && DECL_FILE_SCOPE_P (b
->decl
))
2715 vistype
= TREE_TYPE (visdecl
);
2717 if (scope
!= file_scope
2718 && !DECL_IN_SYSTEM_HEADER (x
))
2719 warning (OPT_Wnested_externs
, "nested extern declaration of %qD", x
);
2721 while (b
&& !B_IN_EXTERNAL_SCOPE (b
))
2723 /* If this decl might be modified, save its type. This is
2724 done here rather than when the decl is first bound
2725 because the type may change after first binding, through
2726 being completed or through attributes being added. If we
2727 encounter multiple such decls, only the first should have
2728 its type saved; the others will already have had their
2729 proper types saved and the types will not have changed as
2730 their scopes will not have been re-entered. */
2731 if (DECL_P (b
->decl
) && DECL_FILE_SCOPE_P (b
->decl
) && !type_saved
)
2733 b
->u
.type
= TREE_TYPE (b
->decl
);
2736 if (B_IN_FILE_SCOPE (b
)
2737 && TREE_CODE (b
->decl
) == VAR_DECL
2738 && TREE_STATIC (b
->decl
)
2739 && TREE_CODE (TREE_TYPE (b
->decl
)) == ARRAY_TYPE
2740 && !TYPE_DOMAIN (TREE_TYPE (b
->decl
))
2741 && TREE_CODE (type
) == ARRAY_TYPE
2742 && TYPE_DOMAIN (type
)
2743 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
2744 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))
2746 /* Array type completed in inner scope, which should be
2747 diagnosed if the completion does not have size 1 and
2748 it does not get completed in the file scope. */
2749 b
->inner_comp
= true;
2754 /* If a matching external declaration has been found, set its
2755 type to the composite of all the types of that declaration.
2756 After the consistency checks, it will be reset to the
2757 composite of the visible types only. */
2758 if (b
&& (TREE_PUBLIC (x
) || same_translation_unit_p (x
, b
->decl
))
2760 TREE_TYPE (b
->decl
) = b
->u
.type
;
2762 /* The point of the same_translation_unit_p check here is,
2763 we want to detect a duplicate decl for a construct like
2764 foo() { extern bar(); } ... static bar(); but not if
2765 they are in different translation units. In any case,
2766 the static does not go in the externals scope. */
2768 && (TREE_PUBLIC (x
) || same_translation_unit_p (x
, b
->decl
))
2769 && duplicate_decls (x
, b
->decl
))
2774 if (comptypes (vistype
, type
))
2775 thistype
= composite_type (vistype
, type
);
2777 thistype
= TREE_TYPE (b
->decl
);
2781 b
->u
.type
= TREE_TYPE (b
->decl
);
2782 if (TREE_CODE (b
->decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (b
->decl
))
2784 = build_type_attribute_variant (thistype
,
2785 TYPE_ATTRIBUTES (b
->u
.type
));
2786 TREE_TYPE (b
->decl
) = thistype
;
2787 bind (name
, b
->decl
, scope
, /*invisible=*/false, /*nested=*/true,
2791 else if (TREE_PUBLIC (x
))
2793 if (visdecl
&& !b
&& duplicate_decls (x
, visdecl
))
2795 /* An external declaration at block scope referring to a
2796 visible entity with internal linkage. The composite
2797 type will already be correct for this scope, so we
2798 just need to fall through to make the declaration in
2805 bind (name
, x
, external_scope
, /*invisible=*/true,
2806 /*nested=*/false, locus
);
2812 if (TREE_CODE (x
) != PARM_DECL
)
2813 warn_if_shadowing (x
);
2815 skip_external_and_shadow_checks
:
2816 if (TREE_CODE (x
) == TYPE_DECL
)
2818 /* So this is a typedef, set its underlying type. */
2819 set_underlying_type (x
);
2821 /* If X is a typedef defined in the current function, record it
2822 for the purpose of implementing the -Wunused-local-typedefs
2824 record_locally_defined_typedef (x
);
2827 bind (name
, x
, scope
, /*invisible=*/false, nested
, locus
);
2829 /* If x's type is incomplete because it's based on a
2830 structure or union which has not yet been fully declared,
2831 attach it to that structure or union type, so we can go
2832 back and complete the variable declaration later, if the
2833 structure or union gets fully declared.
2835 If the input is erroneous, we can have error_mark in the type
2836 slot (e.g. "f(void a, ...)") - that doesn't count as an
2838 if (TREE_TYPE (x
) != error_mark_node
2839 && !COMPLETE_TYPE_P (TREE_TYPE (x
)))
2841 tree element
= TREE_TYPE (x
);
2843 while (TREE_CODE (element
) == ARRAY_TYPE
)
2844 element
= TREE_TYPE (element
);
2845 element
= TYPE_MAIN_VARIANT (element
);
2847 if ((TREE_CODE (element
) == RECORD_TYPE
2848 || TREE_CODE (element
) == UNION_TYPE
)
2849 && (TREE_CODE (x
) != TYPE_DECL
2850 || TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
)
2851 && !COMPLETE_TYPE_P (element
))
2852 C_TYPE_INCOMPLETE_VARS (element
)
2853 = tree_cons (NULL_TREE
, x
, C_TYPE_INCOMPLETE_VARS (element
));
2858 /* Record X as belonging to file scope.
2859 This is used only internally by the Objective-C front end,
2860 and is limited to its needs. duplicate_decls is not called;
2861 if there is any preexisting decl for this identifier, it is an ICE. */
2864 pushdecl_top_level (tree x
)
2867 bool nested
= false;
2868 gcc_assert (TREE_CODE (x
) == VAR_DECL
|| TREE_CODE (x
) == CONST_DECL
);
2870 name
= DECL_NAME (x
);
2872 gcc_assert (TREE_CODE (x
) == CONST_DECL
|| !I_SYMBOL_BINDING (name
));
2874 if (TREE_PUBLIC (x
))
2876 bind (name
, x
, external_scope
, /*invisible=*/true, /*nested=*/false,
2881 bind (name
, x
, file_scope
, /*invisible=*/false, nested
, UNKNOWN_LOCATION
);
2887 implicit_decl_warning (tree id
, tree olddecl
)
2889 if (warn_implicit_function_declaration
)
2894 warned
= pedwarn (input_location
, OPT_Wimplicit_function_declaration
,
2895 "implicit declaration of function %qE", id
);
2897 warned
= warning (OPT_Wimplicit_function_declaration
,
2898 G_("implicit declaration of function %qE"), id
);
2899 if (olddecl
&& warned
)
2900 locate_old_decl (olddecl
);
2904 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
2905 function of type int (). */
2908 implicitly_declare (location_t loc
, tree functionid
)
2910 struct c_binding
*b
;
2914 for (b
= I_SYMBOL_BINDING (functionid
); b
; b
= b
->shadowed
)
2916 if (B_IN_SCOPE (b
, external_scope
))
2925 if (decl
== error_mark_node
)
2928 /* FIXME: Objective-C has weird not-really-builtin functions
2929 which are supposed to be visible automatically. They wind up
2930 in the external scope because they're pushed before the file
2931 scope gets created. Catch this here and rebind them into the
2933 if (!DECL_BUILT_IN (decl
) && DECL_IS_BUILTIN (decl
))
2935 bind (functionid
, decl
, file_scope
,
2936 /*invisible=*/false, /*nested=*/true,
2937 DECL_SOURCE_LOCATION (decl
));
2942 tree newtype
= default_function_type
;
2944 TREE_TYPE (decl
) = b
->u
.type
;
2945 /* Implicit declaration of a function already declared
2946 (somehow) in a different scope, or as a built-in.
2947 If this is the first time this has happened, warn;
2948 then recycle the old declaration but with the new type. */
2949 if (!C_DECL_IMPLICIT (decl
))
2951 implicit_decl_warning (functionid
, decl
);
2952 C_DECL_IMPLICIT (decl
) = 1;
2954 if (DECL_BUILT_IN (decl
))
2956 newtype
= build_type_attribute_variant (newtype
,
2958 (TREE_TYPE (decl
)));
2959 if (!comptypes (newtype
, TREE_TYPE (decl
)))
2961 warning_at (loc
, 0, "incompatible implicit declaration of "
2962 "built-in function %qD", decl
);
2963 newtype
= TREE_TYPE (decl
);
2968 if (!comptypes (newtype
, TREE_TYPE (decl
)))
2970 error_at (loc
, "incompatible implicit declaration of function %qD", decl
);
2971 locate_old_decl (decl
);
2974 b
->u
.type
= TREE_TYPE (decl
);
2975 TREE_TYPE (decl
) = newtype
;
2976 bind (functionid
, decl
, current_scope
,
2977 /*invisible=*/false, /*nested=*/true,
2978 DECL_SOURCE_LOCATION (decl
));
2983 /* Not seen before. */
2984 decl
= build_decl (loc
, FUNCTION_DECL
, functionid
, default_function_type
);
2985 DECL_EXTERNAL (decl
) = 1;
2986 TREE_PUBLIC (decl
) = 1;
2987 C_DECL_IMPLICIT (decl
) = 1;
2988 implicit_decl_warning (functionid
, 0);
2989 asmspec_tree
= maybe_apply_renaming_pragma (decl
, /*asmname=*/NULL
);
2991 set_user_assembler_name (decl
, TREE_STRING_POINTER (asmspec_tree
));
2993 /* C89 says implicit declarations are in the innermost block.
2994 So we record the decl in the standard fashion. */
2995 decl
= pushdecl (decl
);
2997 /* No need to call objc_check_decl here - it's a function type. */
2998 rest_of_decl_compilation (decl
, 0, 0);
3000 /* Write a record describing this implicit function declaration
3001 to the prototypes file (if requested). */
3002 gen_aux_info_record (decl
, 0, 1, 0);
3004 /* Possibly apply some default attributes to this implicit declaration. */
3005 decl_attributes (&decl
, NULL_TREE
, 0);
3010 /* Issue an error message for a reference to an undeclared variable
3011 ID, including a reference to a builtin outside of function-call
3012 context. Establish a binding of the identifier to error_mark_node
3013 in an appropriate scope, which will suppress further errors for the
3014 same identifier. The error message should be given location LOC. */
3016 undeclared_variable (location_t loc
, tree id
)
3018 static bool already
= false;
3019 struct c_scope
*scope
;
3021 if (current_function_decl
== 0)
3023 error_at (loc
, "%qE undeclared here (not in a function)", id
);
3024 scope
= current_scope
;
3028 if (!objc_diagnose_private_ivar (id
))
3029 error_at (loc
, "%qE undeclared (first use in this function)", id
);
3032 inform (loc
, "each undeclared identifier is reported only"
3033 " once for each function it appears in");
3037 /* If we are parsing old-style parameter decls, current_function_decl
3038 will be nonnull but current_function_scope will be null. */
3039 scope
= current_function_scope
? current_function_scope
: current_scope
;
3041 bind (id
, error_mark_node
, scope
, /*invisible=*/false, /*nested=*/false,
3045 /* Subroutine of lookup_label, declare_label, define_label: construct a
3046 LABEL_DECL with all the proper frills. Also create a struct
3047 c_label_vars initialized for the current scope. */
3050 make_label (location_t location
, tree name
, bool defining
,
3051 struct c_label_vars
**p_label_vars
)
3053 tree label
= build_decl (location
, LABEL_DECL
, name
, void_type_node
);
3054 struct c_label_vars
*label_vars
;
3056 DECL_CONTEXT (label
) = current_function_decl
;
3057 DECL_MODE (label
) = VOIDmode
;
3059 label_vars
= ggc_alloc_c_label_vars ();
3060 label_vars
->shadowed
= NULL
;
3061 set_spot_bindings (&label_vars
->label_bindings
, defining
);
3062 label_vars
->decls_in_scope
= make_tree_vector ();
3063 label_vars
->gotos
= NULL
;
3064 *p_label_vars
= label_vars
;
3069 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3070 Create one if none exists so far for the current function.
3071 This is called when a label is used in a goto expression or
3072 has its address taken. */
3075 lookup_label (tree name
)
3078 struct c_label_vars
*label_vars
;
3080 if (current_function_scope
== 0)
3082 error ("label %qE referenced outside of any function", name
);
3086 /* Use a label already defined or ref'd with this name, but not if
3087 it is inherited from a containing function and wasn't declared
3089 label
= I_LABEL_DECL (name
);
3090 if (label
&& (DECL_CONTEXT (label
) == current_function_decl
3091 || C_DECLARED_LABEL_FLAG (label
)))
3093 /* If the label has only been declared, update its apparent
3094 location to point here, for better diagnostics if it
3095 turns out not to have been defined. */
3096 if (DECL_INITIAL (label
) == NULL_TREE
)
3097 DECL_SOURCE_LOCATION (label
) = input_location
;
3101 /* No label binding for that identifier; make one. */
3102 label
= make_label (input_location
, name
, false, &label_vars
);
3104 /* Ordinary labels go in the current function scope. */
3105 bind_label (name
, label
, current_function_scope
, label_vars
);
3110 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3114 warn_about_goto (location_t goto_loc
, tree label
, tree decl
)
3116 if (variably_modified_type_p (TREE_TYPE (decl
), NULL_TREE
))
3118 "jump into scope of identifier with variably modified type");
3120 warning_at (goto_loc
, OPT_Wjump_misses_init
,
3121 "jump skips variable initialization");
3122 inform (DECL_SOURCE_LOCATION (label
), "label %qD defined here", label
);
3123 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3126 /* Look up a label because of a goto statement. This is like
3127 lookup_label, but also issues any appropriate warnings. */
3130 lookup_label_for_goto (location_t loc
, tree name
)
3133 struct c_label_vars
*label_vars
;
3137 label
= lookup_label (name
);
3138 if (label
== NULL_TREE
)
3141 /* If we are jumping to a different function, we can't issue any
3143 if (DECL_CONTEXT (label
) != current_function_decl
)
3145 gcc_assert (C_DECLARED_LABEL_FLAG (label
));
3149 label_vars
= I_LABEL_BINDING (name
)->u
.label
;
3151 /* If the label has not yet been defined, then push this goto on a
3152 list for possible later warnings. */
3153 if (label_vars
->label_bindings
.scope
== NULL
)
3155 struct c_goto_bindings
*g
;
3157 g
= ggc_alloc_c_goto_bindings ();
3159 set_spot_bindings (&g
->goto_bindings
, true);
3160 vec_safe_push (label_vars
->gotos
, g
);
3164 /* If there are any decls in label_vars->decls_in_scope, then this
3165 goto has missed the declaration of the decl. This happens for a
3171 Issue a warning or error. */
3172 FOR_EACH_VEC_SAFE_ELT (label_vars
->decls_in_scope
, ix
, decl
)
3173 warn_about_goto (loc
, label
, decl
);
3175 if (label_vars
->label_bindings
.left_stmt_expr
)
3177 error_at (loc
, "jump into statement expression");
3178 inform (DECL_SOURCE_LOCATION (label
), "label %qD defined here", label
);
3184 /* Make a label named NAME in the current function, shadowing silently
3185 any that may be inherited from containing functions or containing
3186 scopes. This is called for __label__ declarations. */
3189 declare_label (tree name
)
3191 struct c_binding
*b
= I_LABEL_BINDING (name
);
3193 struct c_label_vars
*label_vars
;
3195 /* Check to make sure that the label hasn't already been declared
3197 if (b
&& B_IN_CURRENT_SCOPE (b
))
3199 error ("duplicate label declaration %qE", name
);
3200 locate_old_decl (b
->decl
);
3202 /* Just use the previous declaration. */
3206 label
= make_label (input_location
, name
, false, &label_vars
);
3207 C_DECLARED_LABEL_FLAG (label
) = 1;
3209 /* Declared labels go in the current scope. */
3210 bind_label (name
, label
, current_scope
, label_vars
);
3215 /* When we define a label, issue any appropriate warnings if there are
3216 any gotos earlier in the function which jump to this label. */
3219 check_earlier_gotos (tree label
, struct c_label_vars
* label_vars
)
3222 struct c_goto_bindings
*g
;
3224 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
3226 struct c_binding
*b
;
3227 struct c_scope
*scope
;
3229 /* We have a goto to this label. The goto is going forward. In
3230 g->scope, the goto is going to skip any binding which was
3231 defined after g->bindings_in_scope. */
3232 if (g
->goto_bindings
.scope
->has_jump_unsafe_decl
)
3234 for (b
= g
->goto_bindings
.scope
->bindings
;
3235 b
!= g
->goto_bindings
.bindings_in_scope
;
3238 if (decl_jump_unsafe (b
->decl
))
3239 warn_about_goto (g
->loc
, label
, b
->decl
);
3243 /* We also need to warn about decls defined in any scopes
3244 between the scope of the label and the scope of the goto. */
3245 for (scope
= label_vars
->label_bindings
.scope
;
3246 scope
!= g
->goto_bindings
.scope
;
3247 scope
= scope
->outer
)
3249 gcc_assert (scope
!= NULL
);
3250 if (scope
->has_jump_unsafe_decl
)
3252 if (scope
== label_vars
->label_bindings
.scope
)
3253 b
= label_vars
->label_bindings
.bindings_in_scope
;
3255 b
= scope
->bindings
;
3256 for (; b
!= NULL
; b
= b
->prev
)
3258 if (decl_jump_unsafe (b
->decl
))
3259 warn_about_goto (g
->loc
, label
, b
->decl
);
3264 if (g
->goto_bindings
.stmt_exprs
> 0)
3266 error_at (g
->loc
, "jump into statement expression");
3267 inform (DECL_SOURCE_LOCATION (label
), "label %qD defined here",
3272 /* Now that the label is defined, we will issue warnings about
3273 subsequent gotos to this label when we see them. */
3274 vec_safe_truncate (label_vars
->gotos
, 0);
3275 label_vars
->gotos
= NULL
;
3278 /* Define a label, specifying the location in the source file.
3279 Return the LABEL_DECL node for the label, if the definition is valid.
3280 Otherwise return 0. */
3283 define_label (location_t location
, tree name
)
3285 /* Find any preexisting label with this name. It is an error
3286 if that label has already been defined in this function, or
3287 if there is a containing function with a declared label with
3289 tree label
= I_LABEL_DECL (name
);
3292 && ((DECL_CONTEXT (label
) == current_function_decl
3293 && DECL_INITIAL (label
) != 0)
3294 || (DECL_CONTEXT (label
) != current_function_decl
3295 && C_DECLARED_LABEL_FLAG (label
))))
3297 error_at (location
, "duplicate label %qD", label
);
3298 locate_old_decl (label
);
3301 else if (label
&& DECL_CONTEXT (label
) == current_function_decl
)
3303 struct c_label_vars
*label_vars
= I_LABEL_BINDING (name
)->u
.label
;
3305 /* The label has been used or declared already in this function,
3306 but not defined. Update its location to point to this
3308 DECL_SOURCE_LOCATION (label
) = location
;
3309 set_spot_bindings (&label_vars
->label_bindings
, true);
3311 /* Issue warnings as required about any goto statements from
3312 earlier in the function. */
3313 check_earlier_gotos (label
, label_vars
);
3317 struct c_label_vars
*label_vars
;
3319 /* No label binding for that identifier; make one. */
3320 label
= make_label (location
, name
, true, &label_vars
);
3322 /* Ordinary labels go in the current function scope. */
3323 bind_label (name
, label
, current_function_scope
, label_vars
);
3326 if (!in_system_header
&& lookup_name (name
))
3327 warning_at (location
, OPT_Wtraditional
,
3328 "traditional C lacks a separate namespace "
3329 "for labels, identifier %qE conflicts", name
);
3331 /* Mark label as having been defined. */
3332 DECL_INITIAL (label
) = error_mark_node
;
3336 /* Get the bindings for a new switch statement. This is used to issue
3337 warnings as appropriate for jumps from the switch to case or
3340 struct c_spot_bindings
*
3341 c_get_switch_bindings (void)
3343 struct c_spot_bindings
*switch_bindings
;
3345 switch_bindings
= XNEW (struct c_spot_bindings
);
3346 set_spot_bindings (switch_bindings
, true);
3347 return switch_bindings
;
3351 c_release_switch_bindings (struct c_spot_bindings
*bindings
)
3353 gcc_assert (bindings
->stmt_exprs
== 0 && !bindings
->left_stmt_expr
);
3357 /* This is called at the point of a case or default label to issue
3358 warnings about decls as needed. It returns true if it found an
3359 error, not just a warning. */
3362 c_check_switch_jump_warnings (struct c_spot_bindings
*switch_bindings
,
3363 location_t switch_loc
, location_t case_loc
)
3366 struct c_scope
*scope
;
3369 for (scope
= current_scope
;
3370 scope
!= switch_bindings
->scope
;
3371 scope
= scope
->outer
)
3373 struct c_binding
*b
;
3375 gcc_assert (scope
!= NULL
);
3377 if (!scope
->has_jump_unsafe_decl
)
3380 for (b
= scope
->bindings
; b
!= NULL
; b
= b
->prev
)
3382 if (decl_jump_unsafe (b
->decl
))
3384 if (variably_modified_type_p (TREE_TYPE (b
->decl
), NULL_TREE
))
3388 ("switch jumps into scope of identifier with "
3389 "variably modified type"));
3392 warning_at (case_loc
, OPT_Wjump_misses_init
,
3393 "switch jumps over variable initialization");
3394 inform (switch_loc
, "switch starts here");
3395 inform (DECL_SOURCE_LOCATION (b
->decl
), "%qD declared here",
3401 if (switch_bindings
->stmt_exprs
> 0)
3404 error_at (case_loc
, "switch jumps into statement expression");
3405 inform (switch_loc
, "switch starts here");
3411 /* Given NAME, an IDENTIFIER_NODE,
3412 return the structure (or union or enum) definition for that name.
3413 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3414 CODE says which kind of type the caller wants;
3415 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3416 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3417 location where the tag was defined.
3418 If the wrong kind of type is found, an error is reported. */
3421 lookup_tag (enum tree_code code
, tree name
, int thislevel_only
,
3424 struct c_binding
*b
= I_TAG_BINDING (name
);
3430 /* We only care about whether it's in this level if
3431 thislevel_only was set or it might be a type clash. */
3432 if (thislevel_only
|| TREE_CODE (b
->decl
) != code
)
3434 /* For our purposes, a tag in the external scope is the same as
3435 a tag in the file scope. (Primarily relevant to Objective-C
3436 and its builtin structure tags, which get pushed before the
3437 file scope is created.) */
3438 if (B_IN_CURRENT_SCOPE (b
)
3439 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
3443 if (thislevel_only
&& !thislevel
)
3446 if (TREE_CODE (b
->decl
) != code
)
3448 /* Definition isn't the kind we were looking for. */
3449 pending_invalid_xref
= name
;
3450 pending_invalid_xref_location
= input_location
;
3452 /* If in the same binding level as a declaration as a tag
3453 of a different type, this must not be allowed to
3454 shadow that tag, so give the error immediately.
3455 (For example, "struct foo; union foo;" is invalid.) */
3457 pending_xref_error ();
3466 /* Print an error message now
3467 for a recent invalid struct, union or enum cross reference.
3468 We don't print them immediately because they are not invalid
3469 when used in the `struct foo;' construct for shadowing. */
3472 pending_xref_error (void)
3474 if (pending_invalid_xref
!= 0)
3475 error_at (pending_invalid_xref_location
, "%qE defined as wrong kind of tag",
3476 pending_invalid_xref
);
3477 pending_invalid_xref
= 0;
3481 /* Look up NAME in the current scope and its superiors
3482 in the namespace of variables, functions and typedefs.
3483 Return a ..._DECL node of some kind representing its definition,
3484 or return 0 if it is undefined. */
3487 lookup_name (tree name
)
3489 struct c_binding
*b
= I_SYMBOL_BINDING (name
);
3490 if (b
&& !b
->invisible
)
3492 maybe_record_typedef_use (b
->decl
);
3498 /* Similar to `lookup_name' but look only at the indicated scope. */
3501 lookup_name_in_scope (tree name
, struct c_scope
*scope
)
3503 struct c_binding
*b
;
3505 for (b
= I_SYMBOL_BINDING (name
); b
; b
= b
->shadowed
)
3506 if (B_IN_SCOPE (b
, scope
))
3511 /* Create the predefined scalar types of C,
3512 and some nodes representing standard constants (0, 1, (void *) 0).
3513 Initialize the global scope.
3514 Make definitions for built-in primitive functions. */
3517 c_init_decl_processing (void)
3519 location_t save_loc
= input_location
;
3521 /* Initialize reserved words for parser. */
3524 current_function_decl
= 0;
3526 gcc_obstack_init (&parser_obstack
);
3528 /* Make the externals scope. */
3530 external_scope
= current_scope
;
3532 /* Declarations from c_common_nodes_and_builtins must not be associated
3533 with this input file, lest we get differences between using and not
3534 using preprocessed headers. */
3535 input_location
= BUILTINS_LOCATION
;
3537 c_common_nodes_and_builtins ();
3539 /* In C, comparisons and TRUTH_* expressions have type int. */
3540 truthvalue_type_node
= integer_type_node
;
3541 truthvalue_true_node
= integer_one_node
;
3542 truthvalue_false_node
= integer_zero_node
;
3544 /* Even in C99, which has a real boolean type. */
3545 pushdecl (build_decl (UNKNOWN_LOCATION
, TYPE_DECL
, get_identifier ("_Bool"),
3546 boolean_type_node
));
3548 input_location
= save_loc
;
3550 pedantic_lvalues
= true;
3552 make_fname_decl
= c_make_fname_decl
;
3553 start_fname_decls ();
3556 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
3557 give the decl, NAME is the initialization string and TYPE_DEP
3558 indicates whether NAME depended on the type of the function. As we
3559 don't yet implement delayed emission of static data, we mark the
3560 decl as emitted so it is not placed in the output. Anything using
3561 it must therefore pull out the STRING_CST initializer directly.
3565 c_make_fname_decl (location_t loc
, tree id
, int type_dep
)
3567 const char *name
= fname_as_string (type_dep
);
3568 tree decl
, type
, init
;
3569 size_t length
= strlen (name
);
3571 type
= build_array_type (char_type_node
,
3572 build_index_type (size_int (length
)));
3573 type
= c_build_qualified_type (type
, TYPE_QUAL_CONST
);
3575 decl
= build_decl (loc
, VAR_DECL
, id
, type
);
3577 TREE_STATIC (decl
) = 1;
3578 TREE_READONLY (decl
) = 1;
3579 DECL_ARTIFICIAL (decl
) = 1;
3581 init
= build_string (length
+ 1, name
);
3582 free (CONST_CAST (char *, name
));
3583 TREE_TYPE (init
) = type
;
3584 DECL_INITIAL (decl
) = init
;
3586 TREE_USED (decl
) = 1;
3588 if (current_function_decl
3589 /* For invalid programs like this:
3592 const char* p = __FUNCTION__;
3594 the __FUNCTION__ is believed to appear in K&R style function
3595 parameter declarator. In that case we still don't have
3597 && (!seen_error () || current_function_scope
))
3599 DECL_CONTEXT (decl
) = current_function_decl
;
3600 bind (id
, decl
, current_function_scope
,
3601 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
3604 finish_decl (decl
, loc
, init
, NULL_TREE
, NULL_TREE
);
3610 c_builtin_function (tree decl
)
3612 tree type
= TREE_TYPE (decl
);
3613 tree id
= DECL_NAME (decl
);
3615 const char *name
= IDENTIFIER_POINTER (id
);
3616 C_DECL_BUILTIN_PROTOTYPE (decl
) = prototype_p (type
);
3618 /* Should never be called on a symbol with a preexisting meaning. */
3619 gcc_assert (!I_SYMBOL_BINDING (id
));
3621 bind (id
, decl
, external_scope
, /*invisible=*/true, /*nested=*/false,
3624 /* Builtins in the implementation namespace are made visible without
3625 needing to be explicitly declared. See push_file_scope. */
3626 if (name
[0] == '_' && (name
[1] == '_' || ISUPPER (name
[1])))
3628 DECL_CHAIN (decl
) = visible_builtins
;
3629 visible_builtins
= decl
;
3636 c_builtin_function_ext_scope (tree decl
)
3638 tree type
= TREE_TYPE (decl
);
3639 tree id
= DECL_NAME (decl
);
3641 const char *name
= IDENTIFIER_POINTER (id
);
3642 C_DECL_BUILTIN_PROTOTYPE (decl
) = prototype_p (type
);
3644 bind (id
, decl
, external_scope
, /*invisible=*/false, /*nested=*/false,
3647 /* Builtins in the implementation namespace are made visible without
3648 needing to be explicitly declared. See push_file_scope. */
3649 if (name
[0] == '_' && (name
[1] == '_' || ISUPPER (name
[1])))
3651 DECL_CHAIN (decl
) = visible_builtins
;
3652 visible_builtins
= decl
;
3658 /* Called when a declaration is seen that contains no names to declare.
3659 If its type is a reference to a structure, union or enum inherited
3660 from a containing scope, shadow that tag name for the current scope
3661 with a forward reference.
3662 If its type defines a new named structure or union
3663 or defines an enum, it is valid but we need not do anything here.
3664 Otherwise, it is an error. */
3667 shadow_tag (const struct c_declspecs
*declspecs
)
3669 shadow_tag_warned (declspecs
, 0);
3672 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
3675 shadow_tag_warned (const struct c_declspecs
*declspecs
, int warned
)
3677 bool found_tag
= false;
3679 if (declspecs
->type
&& !declspecs
->default_int_p
&& !declspecs
->typedef_p
)
3681 tree value
= declspecs
->type
;
3682 enum tree_code code
= TREE_CODE (value
);
3684 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
|| code
== ENUMERAL_TYPE
)
3685 /* Used to test also that TYPE_SIZE (value) != 0.
3686 That caused warning for `struct foo;' at top level in the file. */
3688 tree name
= TYPE_NAME (value
);
3693 if (declspecs
->restrict_p
)
3695 error ("invalid use of %<restrict%>");
3701 if (warned
!= 1 && code
!= ENUMERAL_TYPE
)
3702 /* Empty unnamed enum OK */
3704 pedwarn (input_location
, 0,
3705 "unnamed struct/union that defines no instances");
3709 else if (declspecs
->typespec_kind
!= ctsk_tagdef
3710 && declspecs
->typespec_kind
!= ctsk_tagfirstref
3711 && declspecs
->storage_class
!= csc_none
)
3714 pedwarn (input_location
, 0,
3715 "empty declaration with storage class specifier "
3716 "does not redeclare tag");
3718 pending_xref_error ();
3720 else if (declspecs
->typespec_kind
!= ctsk_tagdef
3721 && declspecs
->typespec_kind
!= ctsk_tagfirstref
3722 && (declspecs
->const_p
3723 || declspecs
->volatile_p
3724 || declspecs
->atomic_p
3725 || declspecs
->restrict_p
3726 || declspecs
->address_space
))
3729 pedwarn (input_location
, 0,
3730 "empty declaration with type qualifier "
3731 "does not redeclare tag");
3733 pending_xref_error ();
3735 else if (declspecs
->typespec_kind
!= ctsk_tagdef
3736 && declspecs
->typespec_kind
!= ctsk_tagfirstref
3737 && declspecs
->alignas_p
)
3740 pedwarn (input_location
, 0,
3741 "empty declaration with %<_Alignas%> "
3742 "does not redeclare tag");
3744 pending_xref_error ();
3748 pending_invalid_xref
= 0;
3749 t
= lookup_tag (code
, name
, 1, NULL
);
3753 t
= make_node (code
);
3754 pushtag (input_location
, name
, t
);
3760 if (warned
!= 1 && !in_system_header
)
3762 pedwarn (input_location
, 0,
3763 "useless type name in empty declaration");
3768 else if (warned
!= 1 && !in_system_header
&& declspecs
->typedef_p
)
3770 pedwarn (input_location
, 0, "useless type name in empty declaration");
3774 pending_invalid_xref
= 0;
3776 if (declspecs
->inline_p
)
3778 error ("%<inline%> in empty declaration");
3782 if (declspecs
->noreturn_p
)
3784 error ("%<_Noreturn%> in empty declaration");
3788 if (current_scope
== file_scope
&& declspecs
->storage_class
== csc_auto
)
3790 error ("%<auto%> in file-scope empty declaration");
3794 if (current_scope
== file_scope
&& declspecs
->storage_class
== csc_register
)
3796 error ("%<register%> in file-scope empty declaration");
3800 if (!warned
&& !in_system_header
&& declspecs
->storage_class
!= csc_none
)
3802 warning (0, "useless storage class specifier in empty declaration");
3806 if (!warned
&& !in_system_header
&& declspecs
->thread_p
)
3808 warning (0, "useless %<__thread%> in empty declaration");
3812 if (!warned
&& !in_system_header
&& (declspecs
->const_p
3813 || declspecs
->volatile_p
3814 || declspecs
->atomic_p
3815 || declspecs
->restrict_p
3816 || declspecs
->address_space
))
3818 warning (0, "useless type qualifier in empty declaration");
3822 if (!warned
&& !in_system_header
&& declspecs
->alignas_p
)
3824 warning (0, "useless %<_Alignas%> in empty declaration");
3831 pedwarn (input_location
, 0, "empty declaration");
3836 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3837 bits. SPECS represents declaration specifiers that the grammar
3838 only permits to contain type qualifiers and attributes. */
3841 quals_from_declspecs (const struct c_declspecs
*specs
)
3843 int quals
= ((specs
->const_p
? TYPE_QUAL_CONST
: 0)
3844 | (specs
->volatile_p
? TYPE_QUAL_VOLATILE
: 0)
3845 | (specs
->restrict_p
? TYPE_QUAL_RESTRICT
: 0)
3846 | (specs
->atomic_p
? TYPE_QUAL_ATOMIC
: 0)
3847 | (ENCODE_QUAL_ADDR_SPACE (specs
->address_space
)));
3848 gcc_assert (!specs
->type
3849 && !specs
->decl_attr
3850 && specs
->typespec_word
== cts_none
3851 && specs
->storage_class
== csc_none
3852 && !specs
->typedef_p
3853 && !specs
->explicit_signed_p
3854 && !specs
->deprecated_p
3856 && !specs
->long_long_p
3859 && !specs
->unsigned_p
3860 && !specs
->complex_p
3862 && !specs
->noreturn_p
3863 && !specs
->thread_p
);
3867 /* Construct an array declarator. LOC is the location of the
3868 beginning of the array (usually the opening brace). EXPR is the
3869 expression inside [], or NULL_TREE. QUALS are the type qualifiers
3870 inside the [] (to be applied to the pointer to which a parameter
3871 array is converted). STATIC_P is true if "static" is inside the
3872 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
3873 VLA of unspecified length which is nevertheless a complete type,
3874 false otherwise. The field for the contained declarator is left to
3875 be filled in by set_array_declarator_inner. */
3877 struct c_declarator
*
3878 build_array_declarator (location_t loc
,
3879 tree expr
, struct c_declspecs
*quals
, bool static_p
,
3882 struct c_declarator
*declarator
= XOBNEW (&parser_obstack
,
3883 struct c_declarator
);
3884 declarator
->id_loc
= loc
;
3885 declarator
->kind
= cdk_array
;
3886 declarator
->declarator
= 0;
3887 declarator
->u
.array
.dimen
= expr
;
3890 declarator
->u
.array
.attrs
= quals
->attrs
;
3891 declarator
->u
.array
.quals
= quals_from_declspecs (quals
);
3895 declarator
->u
.array
.attrs
= NULL_TREE
;
3896 declarator
->u
.array
.quals
= 0;
3898 declarator
->u
.array
.static_p
= static_p
;
3899 declarator
->u
.array
.vla_unspec_p
= vla_unspec_p
;
3902 if (static_p
|| quals
!= NULL
)
3903 pedwarn (loc
, OPT_Wpedantic
,
3904 "ISO C90 does not support %<static%> or type "
3905 "qualifiers in parameter array declarators");
3907 pedwarn (loc
, OPT_Wpedantic
,
3908 "ISO C90 does not support %<[*]%> array declarators");
3912 if (!current_scope
->parm_flag
)
3915 error_at (loc
, "%<[*]%> not allowed in other than "
3916 "function prototype scope");
3917 declarator
->u
.array
.vla_unspec_p
= false;
3920 current_scope
->had_vla_unspec
= true;
3925 /* Set the contained declarator of an array declarator. DECL is the
3926 declarator, as constructed by build_array_declarator; INNER is what
3927 appears on the left of the []. */
3929 struct c_declarator
*
3930 set_array_declarator_inner (struct c_declarator
*decl
,
3931 struct c_declarator
*inner
)
3933 decl
->declarator
= inner
;
3937 /* INIT is a constructor that forms DECL's initializer. If the final
3938 element initializes a flexible array field, add the size of that
3939 initializer to DECL's size. */
3942 add_flexible_array_elts_to_size (tree decl
, tree init
)
3946 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
3949 elt
= CONSTRUCTOR_ELTS (init
)->last ().value
;
3950 type
= TREE_TYPE (elt
);
3951 if (TREE_CODE (type
) == ARRAY_TYPE
3952 && TYPE_SIZE (type
) == NULL_TREE
3953 && TYPE_DOMAIN (type
) != NULL_TREE
3954 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
)
3956 complete_array_type (&type
, elt
, false);
3958 = size_binop (PLUS_EXPR
, DECL_SIZE (decl
), TYPE_SIZE (type
));
3959 DECL_SIZE_UNIT (decl
)
3960 = size_binop (PLUS_EXPR
, DECL_SIZE_UNIT (decl
), TYPE_SIZE_UNIT (type
));
3964 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3965 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3966 before the type name, and set *EXPR_CONST_OPERANDS, if
3967 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3968 appear in a constant expression. */
3971 groktypename (struct c_type_name
*type_name
, tree
*expr
,
3972 bool *expr_const_operands
)
3975 tree attrs
= type_name
->specs
->attrs
;
3977 type_name
->specs
->attrs
= NULL_TREE
;
3979 type
= grokdeclarator (type_name
->declarator
, type_name
->specs
, TYPENAME
,
3980 false, NULL
, &attrs
, expr
, expr_const_operands
,
3983 /* Apply attributes. */
3984 decl_attributes (&type
, attrs
, 0);
3989 /* Wrapper for decl_attributes that adds some implicit attributes
3990 to VAR_DECLs or FUNCTION_DECLs. */
3993 c_decl_attributes (tree
*node
, tree attributes
, int flags
)
3995 /* Add implicit "omp declare target" attribute if requested. */
3996 if (current_omp_declare_target_attribute
3997 && ((TREE_CODE (*node
) == VAR_DECL
&& TREE_STATIC (*node
))
3998 || TREE_CODE (*node
) == FUNCTION_DECL
))
4000 if (TREE_CODE (*node
) == VAR_DECL
4001 && ((DECL_CONTEXT (*node
)
4002 && TREE_CODE (DECL_CONTEXT (*node
)) == FUNCTION_DECL
)
4003 || (current_function_decl
&& !DECL_EXTERNAL (*node
))))
4004 error ("%q+D in block scope inside of declare target directive",
4006 else if (TREE_CODE (*node
) == VAR_DECL
4007 && !COMPLETE_TYPE_P (TREE_TYPE (*node
)))
4008 error ("%q+D in declare target directive does not have mappable type",
4011 attributes
= tree_cons (get_identifier ("omp declare target"),
4012 NULL_TREE
, attributes
);
4014 return decl_attributes (node
, attributes
, flags
);
4018 /* Decode a declarator in an ordinary declaration or data definition.
4019 This is called as soon as the type information and variable name
4020 have been parsed, before parsing the initializer if any.
4021 Here we create the ..._DECL node, fill in its type,
4022 and put it on the list of decls for the current context.
4023 The ..._DECL node is returned as the value.
4025 Exception: for arrays where the length is not specified,
4026 the type is left null, to be filled in by `finish_decl'.
4028 Function definitions do not come here; they go to start_function
4029 instead. However, external and forward declarations of functions
4030 do go through here. Structure field declarations are done by
4031 grokfield and not through here. */
4034 start_decl (struct c_declarator
*declarator
, struct c_declspecs
*declspecs
,
4035 bool initialized
, tree attributes
)
4039 tree expr
= NULL_TREE
;
4040 enum deprecated_states deprecated_state
= DEPRECATED_NORMAL
;
4042 /* An object declared as __attribute__((deprecated)) suppresses
4043 warnings of uses of other deprecated items. */
4044 if (lookup_attribute ("deprecated", attributes
))
4045 deprecated_state
= DEPRECATED_SUPPRESS
;
4047 decl
= grokdeclarator (declarator
, declspecs
,
4048 NORMAL
, initialized
, NULL
, &attributes
, &expr
, NULL
,
4054 add_stmt (fold_convert (void_type_node
, expr
));
4056 if (TREE_CODE (decl
) != FUNCTION_DECL
&& MAIN_NAME_P (DECL_NAME (decl
)))
4057 warning (OPT_Wmain
, "%q+D is usually a function", decl
);
4060 /* Is it valid for this decl to have an initializer at all?
4061 If not, set INITIALIZED to zero, which will indirectly
4062 tell 'finish_decl' to ignore the initializer once it is parsed. */
4063 switch (TREE_CODE (decl
))
4066 error ("typedef %qD is initialized (use __typeof__ instead)", decl
);
4071 error ("function %qD is initialized like a variable", decl
);
4076 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4077 error ("parameter %qD is initialized", decl
);
4082 /* Don't allow initializations for incomplete types except for
4083 arrays which might be completed by the initialization. */
4085 /* This can happen if the array size is an undefined macro.
4086 We already gave a warning, so we don't need another one. */
4087 if (TREE_TYPE (decl
) == error_mark_node
)
4089 else if (COMPLETE_TYPE_P (TREE_TYPE (decl
)))
4091 /* A complete type is ok if size is fixed. */
4093 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl
))) != INTEGER_CST
4094 || C_DECL_VARIABLE_SIZE (decl
))
4096 error ("variable-sized object may not be initialized");
4100 else if (TREE_CODE (TREE_TYPE (decl
)) != ARRAY_TYPE
)
4102 error ("variable %qD has initializer but incomplete type", decl
);
4105 else if (C_DECL_VARIABLE_SIZE (decl
))
4107 /* Although C99 is unclear about whether incomplete arrays
4108 of VLAs themselves count as VLAs, it does not make
4109 sense to permit them to be initialized given that
4110 ordinary VLAs may not be initialized. */
4111 error ("variable-sized object may not be initialized");
4118 if (current_scope
== file_scope
)
4119 TREE_STATIC (decl
) = 1;
4121 /* Tell 'pushdecl' this is an initialized decl
4122 even though we don't yet have the initializer expression.
4123 Also tell 'finish_decl' it may store the real initializer. */
4124 DECL_INITIAL (decl
) = error_mark_node
;
4127 /* If this is a function declaration, write a record describing it to the
4128 prototypes file (if requested). */
4130 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4131 gen_aux_info_record (decl
, 0, 0, prototype_p (TREE_TYPE (decl
)));
4133 /* ANSI specifies that a tentative definition which is not merged with
4134 a non-tentative definition behaves exactly like a definition with an
4135 initializer equal to zero. (Section 3.7.2)
4137 -fno-common gives strict ANSI behavior, though this tends to break
4138 a large body of code that grew up without this rule.
4140 Thread-local variables are never common, since there's no entrenched
4141 body of code to break, and it allows more efficient variable references
4142 in the presence of dynamic linking. */
4144 if (TREE_CODE (decl
) == VAR_DECL
4146 && TREE_PUBLIC (decl
)
4147 && !DECL_THREAD_LOCAL_P (decl
)
4149 DECL_COMMON (decl
) = 1;
4151 /* Set attributes here so if duplicate decl, will have proper attributes. */
4152 c_decl_attributes (&decl
, attributes
, 0);
4154 /* Handle gnu_inline attribute. */
4155 if (declspecs
->inline_p
4156 && !flag_gnu89_inline
4157 && TREE_CODE (decl
) == FUNCTION_DECL
4158 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl
))
4159 || current_function_decl
))
4161 if (declspecs
->storage_class
== csc_auto
&& current_scope
!= file_scope
)
4163 else if (declspecs
->storage_class
!= csc_static
)
4164 DECL_EXTERNAL (decl
) = !DECL_EXTERNAL (decl
);
4167 if (TREE_CODE (decl
) == FUNCTION_DECL
4168 && targetm
.calls
.promote_prototypes (TREE_TYPE (decl
)))
4170 struct c_declarator
*ce
= declarator
;
4172 if (ce
->kind
== cdk_pointer
)
4173 ce
= declarator
->declarator
;
4174 if (ce
->kind
== cdk_function
)
4176 tree args
= ce
->u
.arg_info
->parms
;
4177 for (; args
; args
= DECL_CHAIN (args
))
4179 tree type
= TREE_TYPE (args
);
4180 if (type
&& INTEGRAL_TYPE_P (type
)
4181 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
4182 DECL_ARG_TYPE (args
) = c_type_promotes_to (type
);
4187 if (TREE_CODE (decl
) == FUNCTION_DECL
4188 && DECL_DECLARED_INLINE_P (decl
)
4189 && DECL_UNINLINABLE (decl
)
4190 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl
)))
4191 warning (OPT_Wattributes
, "inline function %q+D given attribute noinline",
4194 /* C99 6.7.4p3: An inline definition of a function with external
4195 linkage shall not contain a definition of a modifiable object
4196 with static storage duration... */
4197 if (TREE_CODE (decl
) == VAR_DECL
4198 && current_scope
!= file_scope
4199 && TREE_STATIC (decl
)
4200 && !TREE_READONLY (decl
)
4201 && DECL_DECLARED_INLINE_P (current_function_decl
)
4202 && DECL_EXTERNAL (current_function_decl
))
4203 record_inline_static (input_location
, current_function_decl
,
4204 decl
, csi_modifiable
);
4206 if (c_dialect_objc ()
4207 && (TREE_CODE (decl
) == VAR_DECL
4208 || TREE_CODE (decl
) == FUNCTION_DECL
))
4209 objc_check_global_decl (decl
);
4211 /* Add this decl to the current scope.
4212 TEM may equal DECL or it may be a previous decl of the same name. */
4213 tem
= pushdecl (decl
);
4215 if (initialized
&& DECL_EXTERNAL (tem
))
4217 DECL_EXTERNAL (tem
) = 0;
4218 TREE_STATIC (tem
) = 1;
4224 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4225 DECL or the non-array element type if DECL is an uninitialized array.
4226 If that type has a const member, diagnose this. */
4229 diagnose_uninitialized_cst_member (tree decl
, tree type
)
4232 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
4235 if (TREE_CODE (field
) != FIELD_DECL
)
4237 field_type
= strip_array_types (TREE_TYPE (field
));
4239 if (TYPE_QUALS (field_type
) & TYPE_QUAL_CONST
)
4241 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
4242 "uninitialized const member in %qT is invalid in C++",
4243 strip_array_types (TREE_TYPE (decl
)));
4244 inform (DECL_SOURCE_LOCATION (field
), "%qD should be initialized", field
);
4247 if (TREE_CODE (field_type
) == RECORD_TYPE
4248 || TREE_CODE (field_type
) == UNION_TYPE
)
4249 diagnose_uninitialized_cst_member (decl
, field_type
);
4253 /* Finish processing of a declaration;
4254 install its initial value.
4255 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4256 If the length of an array type is not known before,
4257 it must be determined now, from the initial value, or it is an error.
4259 INIT_LOC is the location of the initial value. */
4262 finish_decl (tree decl
, location_t init_loc
, tree init
,
4263 tree origtype
, tree asmspec_tree
)
4266 bool was_incomplete
= (DECL_SIZE (decl
) == 0);
4267 const char *asmspec
= 0;
4269 /* If a name was specified, get the string. */
4270 if ((TREE_CODE (decl
) == FUNCTION_DECL
|| TREE_CODE (decl
) == VAR_DECL
)
4271 && DECL_FILE_SCOPE_P (decl
))
4272 asmspec_tree
= maybe_apply_renaming_pragma (decl
, asmspec_tree
);
4274 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
4276 if (TREE_CODE (decl
) == VAR_DECL
4277 && TREE_STATIC (decl
)
4278 && global_bindings_p ())
4279 /* So decl is a global variable. Record the types it uses
4280 so that we can decide later to emit debug info for them. */
4281 record_types_used_by_current_var_decl (decl
);
4283 /* If `start_decl' didn't like having an initialization, ignore it now. */
4284 if (init
!= 0 && DECL_INITIAL (decl
) == 0)
4287 /* Don't crash if parm is initialized. */
4288 if (TREE_CODE (decl
) == PARM_DECL
)
4292 store_init_value (init_loc
, decl
, init
, origtype
);
4294 if (c_dialect_objc () && (TREE_CODE (decl
) == VAR_DECL
4295 || TREE_CODE (decl
) == FUNCTION_DECL
4296 || TREE_CODE (decl
) == FIELD_DECL
))
4297 objc_check_decl (decl
);
4299 type
= TREE_TYPE (decl
);
4301 /* Deduce size of array from initialization, if not already known. */
4302 if (TREE_CODE (type
) == ARRAY_TYPE
4303 && TYPE_DOMAIN (type
) == 0
4304 && TREE_CODE (decl
) != TYPE_DECL
)
4307 = (TREE_STATIC (decl
)
4308 /* Even if pedantic, an external linkage array
4309 may have incomplete type at first. */
4310 ? pedantic
&& !TREE_PUBLIC (decl
)
4311 : !DECL_EXTERNAL (decl
));
4313 = complete_array_type (&TREE_TYPE (decl
), DECL_INITIAL (decl
),
4316 /* Get the completed type made by complete_array_type. */
4317 type
= TREE_TYPE (decl
);
4322 error ("initializer fails to determine size of %q+D", decl
);
4327 error ("array size missing in %q+D", decl
);
4328 /* If a `static' var's size isn't known,
4329 make it extern as well as static, so it does not get
4331 If it is not `static', then do not mark extern;
4332 finish_incomplete_decl will give it a default size
4333 and it will get allocated. */
4334 else if (!pedantic
&& TREE_STATIC (decl
) && !TREE_PUBLIC (decl
))
4335 DECL_EXTERNAL (decl
) = 1;
4339 error ("zero or negative size array %q+D", decl
);
4343 /* For global variables, update the copy of the type that
4344 exists in the binding. */
4345 if (TREE_PUBLIC (decl
))
4347 struct c_binding
*b_ext
= I_SYMBOL_BINDING (DECL_NAME (decl
));
4348 while (b_ext
&& !B_IN_EXTERNAL_SCOPE (b_ext
))
4349 b_ext
= b_ext
->shadowed
;
4352 if (b_ext
->u
.type
&& comptypes (b_ext
->u
.type
, type
))
4353 b_ext
->u
.type
= composite_type (b_ext
->u
.type
, type
);
4355 b_ext
->u
.type
= type
;
4364 if (DECL_INITIAL (decl
))
4365 TREE_TYPE (DECL_INITIAL (decl
)) = type
;
4367 relayout_decl (decl
);
4370 if (TREE_CODE (decl
) == VAR_DECL
)
4372 if (init
&& TREE_CODE (init
) == CONSTRUCTOR
)
4373 add_flexible_array_elts_to_size (decl
, init
);
4375 if (DECL_SIZE (decl
) == 0 && TREE_TYPE (decl
) != error_mark_node
4376 && COMPLETE_TYPE_P (TREE_TYPE (decl
)))
4377 layout_decl (decl
, 0);
4379 if (DECL_SIZE (decl
) == 0
4380 /* Don't give an error if we already gave one earlier. */
4381 && TREE_TYPE (decl
) != error_mark_node
4382 && (TREE_STATIC (decl
)
4383 /* A static variable with an incomplete type
4384 is an error if it is initialized.
4385 Also if it is not file scope.
4386 Otherwise, let it through, but if it is not `extern'
4387 then it may cause an error message later. */
4388 ? (DECL_INITIAL (decl
) != 0
4389 || !DECL_FILE_SCOPE_P (decl
))
4390 /* An automatic variable with an incomplete type
4392 : !DECL_EXTERNAL (decl
)))
4394 error ("storage size of %q+D isn%'t known", decl
);
4395 TREE_TYPE (decl
) = error_mark_node
;
4398 if ((DECL_EXTERNAL (decl
) || TREE_STATIC (decl
))
4399 && DECL_SIZE (decl
) != 0)
4401 if (TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
)
4402 constant_expression_warning (DECL_SIZE (decl
));
4405 error ("storage size of %q+D isn%'t constant", decl
);
4406 TREE_TYPE (decl
) = error_mark_node
;
4410 if (TREE_USED (type
))
4412 TREE_USED (decl
) = 1;
4413 DECL_READ_P (decl
) = 1;
4417 /* If this is a function and an assembler name is specified, reset DECL_RTL
4418 so we can give it its new name. Also, update builtin_decl if it
4419 was a normal built-in. */
4420 if (TREE_CODE (decl
) == FUNCTION_DECL
&& asmspec
)
4422 if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
4423 set_builtin_user_assembler_name (decl
, asmspec
);
4424 set_user_assembler_name (decl
, asmspec
);
4427 /* If #pragma weak was used, mark the decl weak now. */
4428 maybe_apply_pragma_weak (decl
);
4430 /* Output the assembler code and/or RTL code for variables and functions,
4431 unless the type is an undefined structure or union.
4432 If not, it will get done when the type is completed. */
4434 if (TREE_CODE (decl
) == VAR_DECL
|| TREE_CODE (decl
) == FUNCTION_DECL
)
4436 /* Determine the ELF visibility. */
4437 if (TREE_PUBLIC (decl
))
4438 c_determine_visibility (decl
);
4440 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4441 if (c_dialect_objc ())
4442 objc_check_decl (decl
);
4446 /* If this is not a static variable, issue a warning.
4447 It doesn't make any sense to give an ASMSPEC for an
4448 ordinary, non-register local variable. Historically,
4449 GCC has accepted -- but ignored -- the ASMSPEC in
4451 if (!DECL_FILE_SCOPE_P (decl
)
4452 && TREE_CODE (decl
) == VAR_DECL
4453 && !C_DECL_REGISTER (decl
)
4454 && !TREE_STATIC (decl
))
4455 warning (0, "ignoring asm-specifier for non-static local "
4456 "variable %q+D", decl
);
4458 set_user_assembler_name (decl
, asmspec
);
4461 if (DECL_FILE_SCOPE_P (decl
))
4463 if (DECL_INITIAL (decl
) == NULL_TREE
4464 || DECL_INITIAL (decl
) == error_mark_node
)
4465 /* Don't output anything
4466 when a tentative file-scope definition is seen.
4467 But at end of compilation, do output code for them. */
4468 DECL_DEFER_OUTPUT (decl
) = 1;
4469 if (asmspec
&& C_DECL_REGISTER (decl
))
4470 DECL_HARD_REGISTER (decl
) = 1;
4471 rest_of_decl_compilation (decl
, true, 0);
4475 /* In conjunction with an ASMSPEC, the `register'
4476 keyword indicates that we should place the variable
4477 in a particular register. */
4478 if (asmspec
&& C_DECL_REGISTER (decl
))
4480 DECL_HARD_REGISTER (decl
) = 1;
4481 /* This cannot be done for a structure with volatile
4482 fields, on which DECL_REGISTER will have been
4484 if (!DECL_REGISTER (decl
))
4485 error ("cannot put object with volatile field into register");
4488 if (TREE_CODE (decl
) != FUNCTION_DECL
)
4490 /* If we're building a variable sized type, and we might be
4491 reachable other than via the top of the current binding
4492 level, then create a new BIND_EXPR so that we deallocate
4493 the object at the right time. */
4494 /* Note that DECL_SIZE can be null due to errors. */
4495 if (DECL_SIZE (decl
)
4496 && !TREE_CONSTANT (DECL_SIZE (decl
))
4497 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list
))
4500 bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, NULL
);
4501 TREE_SIDE_EFFECTS (bind
) = 1;
4503 BIND_EXPR_BODY (bind
) = push_stmt_list ();
4505 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl
),
4511 if (!DECL_FILE_SCOPE_P (decl
))
4513 /* Recompute the RTL of a local array now
4514 if it used to be an incomplete type. */
4516 && !TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
4518 /* If we used it already as memory, it must stay in memory. */
4519 TREE_ADDRESSABLE (decl
) = TREE_USED (decl
);
4520 /* If it's still incomplete now, no init will save it. */
4521 if (DECL_SIZE (decl
) == 0)
4522 DECL_INITIAL (decl
) = 0;
4527 if (TREE_CODE (decl
) == TYPE_DECL
)
4529 if (!DECL_FILE_SCOPE_P (decl
)
4530 && variably_modified_type_p (TREE_TYPE (decl
), NULL_TREE
))
4531 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl
), DECL_EXPR
, decl
));
4533 rest_of_decl_compilation (decl
, DECL_FILE_SCOPE_P (decl
), 0);
4536 /* Install a cleanup (aka destructor) if one was given. */
4537 if (TREE_CODE (decl
) == VAR_DECL
&& !TREE_STATIC (decl
))
4539 tree attr
= lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl
));
4542 tree cleanup_id
= TREE_VALUE (TREE_VALUE (attr
));
4543 tree cleanup_decl
= lookup_name (cleanup_id
);
4545 vec
<tree
, va_gc
> *v
;
4547 /* Build "cleanup(&decl)" for the destructor. */
4548 cleanup
= build_unary_op (input_location
, ADDR_EXPR
, decl
, 0);
4550 v
->quick_push (cleanup
);
4551 cleanup
= build_function_call_vec (DECL_SOURCE_LOCATION (decl
),
4552 cleanup_decl
, v
, NULL
);
4555 /* Don't warn about decl unused; the cleanup uses it. */
4556 TREE_USED (decl
) = 1;
4557 TREE_USED (cleanup_decl
) = 1;
4558 DECL_READ_P (decl
) = 1;
4560 push_cleanup (decl
, cleanup
, false);
4565 && TREE_CODE (decl
) == VAR_DECL
4566 && !DECL_EXTERNAL (decl
)
4567 && DECL_INITIAL (decl
) == NULL_TREE
)
4569 type
= strip_array_types (type
);
4570 if (TREE_READONLY (decl
))
4571 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
4572 "uninitialized const %qD is invalid in C++", decl
);
4573 else if ((TREE_CODE (type
) == RECORD_TYPE
4574 || TREE_CODE (type
) == UNION_TYPE
)
4575 && C_TYPE_FIELDS_READONLY (type
))
4576 diagnose_uninitialized_cst_member (decl
, type
);
4579 invoke_plugin_callbacks (PLUGIN_FINISH_DECL
, decl
);
4582 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
4583 EXPR is NULL or a pointer to an expression that needs to be
4584 evaluated for the side effects of array size expressions in the
4588 grokparm (const struct c_parm
*parm
, tree
*expr
)
4590 tree attrs
= parm
->attrs
;
4591 tree decl
= grokdeclarator (parm
->declarator
, parm
->specs
, PARM
, false,
4592 NULL
, &attrs
, expr
, NULL
, DEPRECATED_NORMAL
);
4594 decl_attributes (&decl
, attrs
, 0);
4599 /* Given a parsed parameter declaration, decode it into a PARM_DECL
4600 and push that on the current scope. EXPR is a pointer to an
4601 expression that needs to be evaluated for the side effects of array
4602 size expressions in the parameters. */
4605 push_parm_decl (const struct c_parm
*parm
, tree
*expr
)
4607 tree attrs
= parm
->attrs
;
4610 decl
= grokdeclarator (parm
->declarator
, parm
->specs
, PARM
, false, NULL
,
4611 &attrs
, expr
, NULL
, DEPRECATED_NORMAL
);
4612 decl_attributes (&decl
, attrs
, 0);
4614 decl
= pushdecl (decl
);
4616 finish_decl (decl
, input_location
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
4619 /* Mark all the parameter declarations to date as forward decls.
4620 Also diagnose use of this extension. */
4623 mark_forward_parm_decls (void)
4625 struct c_binding
*b
;
4627 if (pedantic
&& !current_scope
->warned_forward_parm_decls
)
4629 pedwarn (input_location
, OPT_Wpedantic
,
4630 "ISO C forbids forward parameter declarations");
4631 current_scope
->warned_forward_parm_decls
= true;
4634 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
4635 if (TREE_CODE (b
->decl
) == PARM_DECL
)
4636 TREE_ASM_WRITTEN (b
->decl
) = 1;
4639 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
4640 literal, which may be an incomplete array type completed by the
4641 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
4642 literal. NON_CONST is true if the initializers contain something
4643 that cannot occur in a constant expression. */
4646 build_compound_literal (location_t loc
, tree type
, tree init
, bool non_const
)
4648 /* We do not use start_decl here because we have a type, not a declarator;
4649 and do not use finish_decl because the decl should be stored inside
4650 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
4655 if (type
== error_mark_node
4656 || init
== error_mark_node
)
4657 return error_mark_node
;
4659 decl
= build_decl (loc
, VAR_DECL
, NULL_TREE
, type
);
4660 DECL_EXTERNAL (decl
) = 0;
4661 TREE_PUBLIC (decl
) = 0;
4662 TREE_STATIC (decl
) = (current_scope
== file_scope
);
4663 DECL_CONTEXT (decl
) = current_function_decl
;
4664 TREE_USED (decl
) = 1;
4665 DECL_READ_P (decl
) = 1;
4666 TREE_TYPE (decl
) = type
;
4667 TREE_READONLY (decl
) = (TYPE_READONLY (type
)
4668 || (TREE_CODE (type
) == ARRAY_TYPE
4669 && TYPE_READONLY (TREE_TYPE (type
))));
4670 store_init_value (loc
, decl
, init
, NULL_TREE
);
4672 if (TREE_CODE (type
) == ARRAY_TYPE
&& !COMPLETE_TYPE_P (type
))
4674 int failure
= complete_array_type (&TREE_TYPE (decl
),
4675 DECL_INITIAL (decl
), true);
4676 gcc_assert (!failure
);
4678 type
= TREE_TYPE (decl
);
4679 TREE_TYPE (DECL_INITIAL (decl
)) = type
;
4682 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
4684 c_incomplete_type_error (NULL_TREE
, type
);
4685 return error_mark_node
;
4688 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), DECL_EXPR
, decl
);
4689 complit
= build1 (COMPOUND_LITERAL_EXPR
, type
, stmt
);
4690 TREE_SIDE_EFFECTS (complit
) = 1;
4692 layout_decl (decl
, 0);
4694 if (TREE_STATIC (decl
))
4696 /* This decl needs a name for the assembler output. */
4697 set_compound_literal_name (decl
);
4698 DECL_DEFER_OUTPUT (decl
) = 1;
4699 DECL_COMDAT (decl
) = 1;
4700 DECL_ARTIFICIAL (decl
) = 1;
4701 DECL_IGNORED_P (decl
) = 1;
4703 rest_of_decl_compilation (decl
, 1, 0);
4708 complit
= build2 (C_MAYBE_CONST_EXPR
, type
, NULL
, complit
);
4709 C_MAYBE_CONST_EXPR_NON_CONST (complit
) = 1;
4715 /* Check the type of a compound literal. Here we just check that it
4716 is valid for C++. */
4719 check_compound_literal_type (location_t loc
, struct c_type_name
*type_name
)
4722 && (type_name
->specs
->typespec_kind
== ctsk_tagdef
4723 || type_name
->specs
->typespec_kind
== ctsk_tagfirstref
))
4724 warning_at (loc
, OPT_Wc___compat
,
4725 "defining a type in a compound literal is invalid in C++");
4728 /* Determine whether TYPE is a structure with a flexible array member,
4729 or a union containing such a structure (possibly recursively). */
4732 flexible_array_type_p (tree type
)
4735 switch (TREE_CODE (type
))
4738 x
= TYPE_FIELDS (type
);
4741 while (DECL_CHAIN (x
) != NULL_TREE
)
4743 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
4744 && TYPE_SIZE (TREE_TYPE (x
)) == NULL_TREE
4745 && TYPE_DOMAIN (TREE_TYPE (x
)) != NULL_TREE
4746 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x
))) == NULL_TREE
)
4750 for (x
= TYPE_FIELDS (type
); x
!= NULL_TREE
; x
= DECL_CHAIN (x
))
4752 if (flexible_array_type_p (TREE_TYPE (x
)))
4761 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
4762 replacing with appropriate values if they are invalid. */
4764 check_bitfield_type_and_width (tree
*type
, tree
*width
, tree orig_name
)
4767 unsigned int max_width
;
4768 unsigned HOST_WIDE_INT w
;
4769 const char *name
= (orig_name
4770 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name
))
4771 : _("<anonymous>"));
4773 /* Detect and ignore out of range field width and process valid
4775 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width
)))
4777 error ("bit-field %qs width not an integer constant", name
);
4778 *width
= integer_one_node
;
4782 if (TREE_CODE (*width
) != INTEGER_CST
)
4784 *width
= c_fully_fold (*width
, false, NULL
);
4785 if (TREE_CODE (*width
) == INTEGER_CST
)
4786 pedwarn (input_location
, OPT_Wpedantic
,
4787 "bit-field %qs width not an integer constant expression",
4790 if (TREE_CODE (*width
) != INTEGER_CST
)
4792 error ("bit-field %qs width not an integer constant", name
);
4793 *width
= integer_one_node
;
4795 constant_expression_warning (*width
);
4796 if (tree_int_cst_sgn (*width
) < 0)
4798 error ("negative width in bit-field %qs", name
);
4799 *width
= integer_one_node
;
4801 else if (integer_zerop (*width
) && orig_name
)
4803 error ("zero width for bit-field %qs", name
);
4804 *width
= integer_one_node
;
4808 /* Detect invalid bit-field type. */
4809 if (TREE_CODE (*type
) != INTEGER_TYPE
4810 && TREE_CODE (*type
) != BOOLEAN_TYPE
4811 && TREE_CODE (*type
) != ENUMERAL_TYPE
)
4813 error ("bit-field %qs has invalid type", name
);
4814 *type
= unsigned_type_node
;
4817 type_mv
= TYPE_MAIN_VARIANT (*type
);
4818 if (!in_system_header
4819 && type_mv
!= integer_type_node
4820 && type_mv
!= unsigned_type_node
4821 && type_mv
!= boolean_type_node
)
4822 pedwarn (input_location
, OPT_Wpedantic
,
4823 "type of bit-field %qs is a GCC extension", name
);
4825 max_width
= TYPE_PRECISION (*type
);
4827 if (0 < compare_tree_int (*width
, max_width
))
4829 error ("width of %qs exceeds its type", name
);
4831 *width
= build_int_cst (integer_type_node
, w
);
4834 w
= tree_to_uhwi (*width
);
4836 if (TREE_CODE (*type
) == ENUMERAL_TYPE
)
4838 struct lang_type
*lt
= TYPE_LANG_SPECIFIC (*type
);
4840 || w
< tree_int_cst_min_precision (lt
->enum_min
, TYPE_SIGN (*type
))
4841 || w
< tree_int_cst_min_precision (lt
->enum_max
, TYPE_SIGN (*type
)))
4842 warning (0, "%qs is narrower than values of its type", name
);
4848 /* Print warning about variable length array if necessary. */
4851 warn_variable_length_array (tree name
, tree size
)
4853 int const_size
= TREE_CONSTANT (size
);
4855 if (!flag_isoc99
&& pedantic
&& warn_vla
!= 0)
4860 pedwarn (input_location
, OPT_Wvla
,
4861 "ISO C90 forbids array %qE whose size "
4862 "can%'t be evaluated",
4865 pedwarn (input_location
, OPT_Wvla
, "ISO C90 forbids array whose size "
4866 "can%'t be evaluated");
4871 pedwarn (input_location
, OPT_Wvla
,
4872 "ISO C90 forbids variable length array %qE",
4875 pedwarn (input_location
, OPT_Wvla
, "ISO C90 forbids variable length array");
4878 else if (warn_vla
> 0)
4884 "the size of array %qE can"
4885 "%'t be evaluated", name
);
4888 "the size of array can %'t be evaluated");
4894 "variable length array %qE is used",
4898 "variable length array is used");
4903 /* Given declspecs and a declarator,
4904 determine the name and type of the object declared
4905 and construct a ..._DECL node for it.
4906 (In one case we can return a ..._TYPE node instead.
4907 For invalid input we sometimes return 0.)
4909 DECLSPECS is a c_declspecs structure for the declaration specifiers.
4911 DECL_CONTEXT says which syntactic context this declaration is in:
4912 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4913 FUNCDEF for a function definition. Like NORMAL but a few different
4914 error messages in each case. Return value may be zero meaning
4915 this definition is too screwy to try to parse.
4916 PARM for a parameter declaration (either within a function prototype
4917 or before a function body). Make a PARM_DECL, or return void_type_node.
4918 TYPENAME if for a typename (in a cast or sizeof).
4919 Don't make a DECL node; just return the ..._TYPE node.
4920 FIELD for a struct or union field; make a FIELD_DECL.
4921 INITIALIZED is true if the decl has an initializer.
4922 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4923 representing the width of the bit-field.
4924 DECL_ATTRS points to the list of attributes that should be added to this
4925 decl. Any nested attributes that belong on the decl itself will be
4927 If EXPR is not NULL, any expressions that need to be evaluated as
4928 part of evaluating variably modified types will be stored in *EXPR.
4929 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
4930 set to indicate whether operands in *EXPR can be used in constant
4932 DEPRECATED_STATE is a deprecated_states value indicating whether
4933 deprecation warnings should be suppressed.
4935 In the TYPENAME case, DECLARATOR is really an absolute declarator.
4936 It may also be so in the PARM case, for a prototype where the
4937 argument type is specified but not the name.
4939 This function is where the complicated C meanings of `static'
4940 and `extern' are interpreted. */
4943 grokdeclarator (const struct c_declarator
*declarator
,
4944 struct c_declspecs
*declspecs
,
4945 enum decl_context decl_context
, bool initialized
, tree
*width
,
4946 tree
*decl_attrs
, tree
*expr
, bool *expr_const_operands
,
4947 enum deprecated_states deprecated_state
)
4949 tree type
= declspecs
->type
;
4950 bool threadp
= declspecs
->thread_p
;
4951 enum c_storage_class storage_class
= declspecs
->storage_class
;
4956 int type_quals
= TYPE_UNQUALIFIED
;
4957 tree name
= NULL_TREE
;
4958 bool funcdef_flag
= false;
4959 bool funcdef_syntax
= false;
4960 bool size_varies
= false;
4961 tree decl_attr
= declspecs
->decl_attr
;
4962 int array_ptr_quals
= TYPE_UNQUALIFIED
;
4963 tree array_ptr_attrs
= NULL_TREE
;
4964 int array_parm_static
= 0;
4965 bool array_parm_vla_unspec_p
= false;
4966 tree returned_attrs
= NULL_TREE
;
4967 bool bitfield
= width
!= NULL
;
4969 struct c_arg_info
*arg_info
= 0;
4970 addr_space_t as1
, as2
, address_space
;
4971 location_t loc
= UNKNOWN_LOCATION
;
4974 bool expr_const_operands_dummy
;
4975 enum c_declarator_kind first_non_attr_kind
;
4976 unsigned int alignas_align
= 0;
4978 if (TREE_CODE (type
) == ERROR_MARK
)
4979 return error_mark_node
;
4982 if (expr_const_operands
== NULL
)
4983 expr_const_operands
= &expr_const_operands_dummy
;
4985 *expr
= declspecs
->expr
;
4986 *expr_const_operands
= declspecs
->expr_const_operands
;
4988 if (decl_context
== FUNCDEF
)
4989 funcdef_flag
= true, decl_context
= NORMAL
;
4991 /* Look inside a declarator for the name being declared
4992 and get it as an IDENTIFIER_NODE, for an error message. */
4994 const struct c_declarator
*decl
= declarator
;
4996 first_non_attr_kind
= cdk_attrs
;
5006 funcdef_syntax
= (decl
->kind
== cdk_function
);
5007 decl
= decl
->declarator
;
5008 if (first_non_attr_kind
== cdk_attrs
)
5009 first_non_attr_kind
= decl
->kind
;
5013 decl
= decl
->declarator
;
5020 if (first_non_attr_kind
== cdk_attrs
)
5021 first_non_attr_kind
= decl
->kind
;
5030 gcc_assert (decl_context
== PARM
5031 || decl_context
== TYPENAME
5032 || (decl_context
== FIELD
5033 && declarator
->kind
== cdk_id
));
5034 gcc_assert (!initialized
);
5038 /* A function definition's declarator must have the form of
5039 a function declarator. */
5041 if (funcdef_flag
&& !funcdef_syntax
)
5044 /* If this looks like a function definition, make it one,
5045 even if it occurs where parms are expected.
5046 Then store_parm_decls will reject it and not use it as a parm. */
5047 if (decl_context
== NORMAL
&& !funcdef_flag
&& current_scope
->parm_flag
)
5048 decl_context
= PARM
;
5050 if (declspecs
->deprecated_p
&& deprecated_state
!= DEPRECATED_SUPPRESS
)
5051 warn_deprecated_use (declspecs
->type
, declspecs
->decl_attr
);
5053 if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
5054 && current_scope
== file_scope
5055 && variably_modified_type_p (type
, NULL_TREE
))
5058 error_at (loc
, "variably modified %qE at file scope", name
);
5060 error_at (loc
, "variably modified field at file scope");
5061 type
= integer_type_node
;
5064 size_varies
= C_TYPE_VARIABLE_SIZE (type
) != 0;
5066 /* Diagnose defaulting to "int". */
5068 if (declspecs
->default_int_p
&& !in_system_header
)
5070 /* Issue a warning if this is an ISO C 99 program or if
5071 -Wreturn-type and this is a function, or if -Wimplicit;
5072 prefer the former warning since it is more explicit. */
5073 if ((warn_implicit_int
|| warn_return_type
|| flag_isoc99
)
5075 warn_about_return_type
= 1;
5079 pedwarn_c99 (loc
, flag_isoc99
? 0 : OPT_Wimplicit_int
,
5080 "type defaults to %<int%> in declaration of %qE",
5083 pedwarn_c99 (input_location
, flag_isoc99
? 0 : OPT_Wimplicit_int
,
5084 "type defaults to %<int%> in type name");
5088 /* Adjust the type if a bit-field is being declared,
5089 -funsigned-bitfields applied and the type is not explicitly
5091 if (bitfield
&& !flag_signed_bitfields
&& !declspecs
->explicit_signed_p
5092 && TREE_CODE (type
) == INTEGER_TYPE
)
5093 type
= unsigned_type_for (type
);
5095 /* Figure out the type qualifiers for the declaration. There are
5096 two ways a declaration can become qualified. One is something
5097 like `const int i' where the `const' is explicit. Another is
5098 something like `typedef const int CI; CI i' where the type of the
5099 declaration contains the `const'. A third possibility is that
5100 there is a type qualifier on the element type of a typedefed
5101 array type, in which case we should extract that qualifier so
5102 that c_apply_type_quals_to_decl receives the full list of
5103 qualifiers to work with (C90 is not entirely clear about whether
5104 duplicate qualifiers should be diagnosed in this case, but it
5105 seems most appropriate to do so). */
5106 element_type
= strip_array_types (type
);
5107 constp
= declspecs
->const_p
+ TYPE_READONLY (element_type
);
5108 restrictp
= declspecs
->restrict_p
+ TYPE_RESTRICT (element_type
);
5109 volatilep
= declspecs
->volatile_p
+ TYPE_VOLATILE (element_type
);
5110 atomicp
= declspecs
->atomic_p
+ TYPE_ATOMIC (element_type
);
5111 as1
= declspecs
->address_space
;
5112 as2
= TYPE_ADDR_SPACE (element_type
);
5113 address_space
= ADDR_SPACE_GENERIC_P (as1
)? as2
: as1
;
5115 if (pedantic
&& !flag_isoc99
)
5118 pedwarn (loc
, OPT_Wpedantic
, "duplicate %<const%>");
5120 pedwarn (loc
, OPT_Wpedantic
, "duplicate %<restrict%>");
5122 pedwarn (loc
, OPT_Wpedantic
, "duplicate %<volatile%>");
5124 pedwarn (loc
, OPT_Wpedantic
, "duplicate %<_Atomic%>");
5128 if (!ADDR_SPACE_GENERIC_P (as1
) && !ADDR_SPACE_GENERIC_P (as2
) && as1
!= as2
)
5129 error_at (loc
, "conflicting named address spaces (%s vs %s)",
5130 c_addr_space_name (as1
), c_addr_space_name (as2
));
5132 if ((TREE_CODE (type
) == ARRAY_TYPE
5133 || first_non_attr_kind
== cdk_array
)
5134 && TYPE_QUALS (element_type
))
5135 type
= TYPE_MAIN_VARIANT (type
);
5136 type_quals
= ((constp
? TYPE_QUAL_CONST
: 0)
5137 | (restrictp
? TYPE_QUAL_RESTRICT
: 0)
5138 | (volatilep
? TYPE_QUAL_VOLATILE
: 0)
5139 | (atomicp
? TYPE_QUAL_ATOMIC
: 0)
5140 | ENCODE_QUAL_ADDR_SPACE (address_space
));
5142 /* Applying the _Atomic qualifier to an array type (through the use
5143 of typedefs or typeof) must be detected here. If the qualifier
5144 is introduced later, any appearance of applying it to an array is
5145 actually applying it to an element of that array. */
5146 if (atomicp
&& TREE_CODE (type
) == ARRAY_TYPE
)
5147 error_at (loc
, "%<_Atomic%>-qualified array type");
5149 /* Warn about storage classes that are invalid for certain
5150 kinds of declarations (parameters, typenames, etc.). */
5154 || storage_class
== csc_auto
5155 || storage_class
== csc_register
5156 || storage_class
== csc_typedef
))
5158 if (storage_class
== csc_auto
)
5160 (current_scope
== file_scope
) ? 0 : OPT_Wpedantic
,
5161 "function definition declared %<auto%>");
5162 if (storage_class
== csc_register
)
5163 error_at (loc
, "function definition declared %<register%>");
5164 if (storage_class
== csc_typedef
)
5165 error_at (loc
, "function definition declared %<typedef%>");
5167 error_at (loc
, "function definition declared %<__thread%>");
5169 if (storage_class
== csc_auto
5170 || storage_class
== csc_register
5171 || storage_class
== csc_typedef
)
5172 storage_class
= csc_none
;
5174 else if (decl_context
!= NORMAL
&& (storage_class
!= csc_none
|| threadp
))
5176 if (decl_context
== PARM
&& storage_class
== csc_register
)
5180 switch (decl_context
)
5184 error_at (loc
, "storage class specified for structure "
5187 error_at (loc
, "storage class specified for structure field");
5191 error_at (loc
, "storage class specified for parameter %qE",
5194 error_at (loc
, "storage class specified for unnamed parameter");
5197 error_at (loc
, "storage class specified for typename");
5200 storage_class
= csc_none
;
5204 else if (storage_class
== csc_extern
5208 /* 'extern' with initialization is invalid if not at file scope. */
5209 if (current_scope
== file_scope
)
5211 /* It is fine to have 'extern const' when compiling at C
5212 and C++ intersection. */
5213 if (!(warn_cxx_compat
&& constp
))
5214 warning_at (loc
, 0, "%qE initialized and declared %<extern%>",
5218 error_at (loc
, "%qE has both %<extern%> and initializer", name
);
5220 else if (current_scope
== file_scope
)
5222 if (storage_class
== csc_auto
)
5223 error_at (loc
, "file-scope declaration of %qE specifies %<auto%>",
5225 if (pedantic
&& storage_class
== csc_register
)
5226 pedwarn (input_location
, OPT_Wpedantic
,
5227 "file-scope declaration of %qE specifies %<register%>", name
);
5231 if (storage_class
== csc_extern
&& funcdef_flag
)
5232 error_at (loc
, "nested function %qE declared %<extern%>", name
);
5233 else if (threadp
&& storage_class
== csc_none
)
5235 error_at (loc
, "function-scope %qE implicitly auto and declared "
5242 /* Now figure out the structure of the declarator proper.
5243 Descend through it, creating more complex types, until we reach
5244 the declared identifier (or NULL_TREE, in an absolute declarator).
5245 At each stage we maintain an unqualified version of the type
5246 together with any qualifiers that should be applied to it with
5247 c_build_qualified_type; this way, array types including
5248 multidimensional array types are first built up in unqualified
5249 form and then the qualified form is created with
5250 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5252 while (declarator
&& declarator
->kind
!= cdk_id
)
5254 if (type
== error_mark_node
)
5256 declarator
= declarator
->declarator
;
5260 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5261 a cdk_pointer (for *...),
5262 a cdk_function (for ...(...)),
5263 a cdk_attrs (for nested attributes),
5264 or a cdk_id (for the name being declared
5265 or the place in an absolute declarator
5266 where the name was omitted).
5267 For the last case, we have just exited the loop.
5269 At this point, TYPE is the type of elements of an array,
5270 or for a function to return, or for a pointer to point to.
5271 After this sequence of ifs, TYPE is the type of the
5272 array or function or pointer, and DECLARATOR has had its
5273 outermost layer removed. */
5275 if (array_ptr_quals
!= TYPE_UNQUALIFIED
5276 || array_ptr_attrs
!= NULL_TREE
5277 || array_parm_static
)
5279 /* Only the innermost declarator (making a parameter be of
5280 array type which is converted to pointer type)
5281 may have static or type qualifiers. */
5282 error_at (loc
, "static or type qualifiers in non-parameter array declarator");
5283 array_ptr_quals
= TYPE_UNQUALIFIED
;
5284 array_ptr_attrs
= NULL_TREE
;
5285 array_parm_static
= 0;
5288 switch (declarator
->kind
)
5292 /* A declarator with embedded attributes. */
5293 tree attrs
= declarator
->u
.attrs
;
5294 const struct c_declarator
*inner_decl
;
5296 declarator
= declarator
->declarator
;
5297 inner_decl
= declarator
;
5298 while (inner_decl
->kind
== cdk_attrs
)
5299 inner_decl
= inner_decl
->declarator
;
5300 if (inner_decl
->kind
== cdk_id
)
5301 attr_flags
|= (int) ATTR_FLAG_DECL_NEXT
;
5302 else if (inner_decl
->kind
== cdk_function
)
5303 attr_flags
|= (int) ATTR_FLAG_FUNCTION_NEXT
;
5304 else if (inner_decl
->kind
== cdk_array
)
5305 attr_flags
|= (int) ATTR_FLAG_ARRAY_NEXT
;
5306 returned_attrs
= decl_attributes (&type
,
5307 chainon (returned_attrs
, attrs
),
5313 tree itype
= NULL_TREE
;
5314 tree size
= declarator
->u
.array
.dimen
;
5315 /* The index is a signed object `sizetype' bits wide. */
5316 tree index_type
= c_common_signed_type (sizetype
);
5318 array_ptr_quals
= declarator
->u
.array
.quals
;
5319 array_ptr_attrs
= declarator
->u
.array
.attrs
;
5320 array_parm_static
= declarator
->u
.array
.static_p
;
5321 array_parm_vla_unspec_p
= declarator
->u
.array
.vla_unspec_p
;
5323 declarator
= declarator
->declarator
;
5325 /* Check for some types that there cannot be arrays of. */
5327 if (VOID_TYPE_P (type
))
5330 error_at (loc
, "declaration of %qE as array of voids", name
);
5332 error_at (loc
, "declaration of type name as array of voids");
5333 type
= error_mark_node
;
5336 if (TREE_CODE (type
) == FUNCTION_TYPE
)
5339 error_at (loc
, "declaration of %qE as array of functions",
5342 error_at (loc
, "declaration of type name as array of "
5344 type
= error_mark_node
;
5347 if (pedantic
&& !in_system_header
&& flexible_array_type_p (type
))
5348 pedwarn (loc
, OPT_Wpedantic
,
5349 "invalid use of structure with flexible array member");
5351 if (size
== error_mark_node
)
5352 type
= error_mark_node
;
5354 if (type
== error_mark_node
)
5357 /* If size was specified, set ITYPE to a range-type for
5358 that size. Otherwise, ITYPE remains null. finish_decl
5359 may figure it out from an initial value. */
5363 bool size_maybe_const
= true;
5364 bool size_int_const
= (TREE_CODE (size
) == INTEGER_CST
5365 && !TREE_OVERFLOW (size
));
5366 bool this_size_varies
= false;
5368 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5370 STRIP_TYPE_NOPS (size
);
5372 if (!INTEGRAL_TYPE_P (TREE_TYPE (size
)))
5375 error_at (loc
, "size of array %qE has non-integer type",
5379 "size of unnamed array has non-integer type");
5380 size
= integer_one_node
;
5383 size
= c_fully_fold (size
, false, &size_maybe_const
);
5385 if (pedantic
&& size_maybe_const
&& integer_zerop (size
))
5388 pedwarn (loc
, OPT_Wpedantic
,
5389 "ISO C forbids zero-size array %qE", name
);
5391 pedwarn (loc
, OPT_Wpedantic
,
5392 "ISO C forbids zero-size array");
5395 if (TREE_CODE (size
) == INTEGER_CST
&& size_maybe_const
)
5397 constant_expression_warning (size
);
5398 if (tree_int_cst_sgn (size
) < 0)
5401 error_at (loc
, "size of array %qE is negative", name
);
5403 error_at (loc
, "size of unnamed array is negative");
5404 size
= integer_one_node
;
5406 /* Handle a size folded to an integer constant but
5407 not an integer constant expression. */
5408 if (!size_int_const
)
5410 /* If this is a file scope declaration of an
5411 ordinary identifier, this is invalid code;
5412 diagnosing it here and not subsequently
5413 treating the type as variable-length avoids
5414 more confusing diagnostics later. */
5415 if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
5416 && current_scope
== file_scope
)
5417 pedwarn (input_location
, 0,
5418 "variably modified %qE at file scope",
5421 this_size_varies
= size_varies
= true;
5422 warn_variable_length_array (name
, size
);
5425 else if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
5426 && current_scope
== file_scope
)
5428 error_at (loc
, "variably modified %qE at file scope", name
);
5429 size
= integer_one_node
;
5433 /* Make sure the array size remains visibly
5434 nonconstant even if it is (eg) a const variable
5435 with known value. */
5436 this_size_varies
= size_varies
= true;
5437 warn_variable_length_array (name
, size
);
5438 if (flag_sanitize
& SANITIZE_VLA
5439 && decl_context
== NORMAL
)
5441 /* Evaluate the array size only once. */
5442 size
= c_save_expr (size
);
5443 size
= c_fully_fold (size
, false, NULL
);
5444 size
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
5445 ubsan_instrument_vla (loc
, size
),
5450 if (integer_zerop (size
) && !this_size_varies
)
5452 /* A zero-length array cannot be represented with
5453 an unsigned index type, which is what we'll
5454 get with build_index_type. Create an
5455 open-ended range instead. */
5456 itype
= build_range_type (sizetype
, size
, NULL_TREE
);
5460 /* Arrange for the SAVE_EXPR on the inside of the
5461 MINUS_EXPR, which allows the -1 to get folded
5462 with the +1 that happens when building TYPE_SIZE. */
5464 size
= save_expr (size
);
5465 if (this_size_varies
&& TREE_CODE (size
) == INTEGER_CST
)
5466 size
= build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
5467 integer_zero_node
, size
);
5469 /* Compute the maximum valid index, that is, size
5470 - 1. Do the calculation in index_type, so that
5471 if it is a variable the computations will be
5472 done in the proper mode. */
5473 itype
= fold_build2_loc (loc
, MINUS_EXPR
, index_type
,
5474 convert (index_type
, size
),
5475 convert (index_type
,
5478 /* The above overflows when size does not fit
5480 ??? While a size of INT_MAX+1 technically shouldn't
5481 cause an overflow (because we subtract 1), handling
5482 this case seems like an unnecessary complication. */
5483 if (TREE_CODE (size
) == INTEGER_CST
5484 && !int_fits_type_p (size
, index_type
))
5487 error_at (loc
, "size of array %qE is too large",
5490 error_at (loc
, "size of unnamed array is too large");
5491 type
= error_mark_node
;
5495 itype
= build_index_type (itype
);
5497 if (this_size_varies
)
5500 *expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
5504 *expr_const_operands
&= size_maybe_const
;
5507 else if (decl_context
== FIELD
)
5509 bool flexible_array_member
= false;
5510 if (array_parm_vla_unspec_p
)
5511 /* Field names can in fact have function prototype
5512 scope so [*] is disallowed here through making
5513 the field variably modified, not through being
5514 something other than a declaration with function
5519 const struct c_declarator
*t
= declarator
;
5520 while (t
->kind
== cdk_attrs
)
5522 flexible_array_member
= (t
->kind
== cdk_id
);
5524 if (flexible_array_member
5525 && pedantic
&& !flag_isoc99
&& !in_system_header
)
5526 pedwarn (loc
, OPT_Wpedantic
,
5527 "ISO C90 does not support flexible array members");
5529 /* ISO C99 Flexible array members are effectively
5530 identical to GCC's zero-length array extension. */
5531 if (flexible_array_member
|| array_parm_vla_unspec_p
)
5532 itype
= build_range_type (sizetype
, size_zero_node
,
5535 else if (decl_context
== PARM
)
5537 if (array_parm_vla_unspec_p
)
5539 itype
= build_range_type (sizetype
, size_zero_node
, NULL_TREE
);
5543 else if (decl_context
== TYPENAME
)
5545 if (array_parm_vla_unspec_p
)
5548 warning (0, "%<[*]%> not in a declaration");
5549 /* We use this to avoid messing up with incomplete
5550 array types of the same type, that would
5551 otherwise be modified below. */
5552 itype
= build_range_type (sizetype
, size_zero_node
,
5558 /* Complain about arrays of incomplete types. */
5559 if (!COMPLETE_TYPE_P (type
))
5561 error_at (loc
, "array type has incomplete element type");
5562 type
= error_mark_node
;
5565 /* When itype is NULL, a shared incomplete array type is
5566 returned for all array of a given type. Elsewhere we
5567 make sure we don't complete that type before copying
5568 it, but here we want to make sure we don't ever
5569 modify the shared type, so we gcc_assert (itype)
5572 addr_space_t as
= DECODE_QUAL_ADDR_SPACE (type_quals
);
5573 if (!ADDR_SPACE_GENERIC_P (as
) && as
!= TYPE_ADDR_SPACE (type
))
5574 type
= build_qualified_type (type
,
5575 ENCODE_QUAL_ADDR_SPACE (as
));
5577 type
= build_array_type (type
, itype
);
5580 if (type
!= error_mark_node
)
5584 /* It is ok to modify type here even if itype is
5585 NULL: if size_varies, we're in a
5586 multi-dimensional array and the inner type has
5587 variable size, so the enclosing shared array type
5589 if (size
&& TREE_CODE (size
) == INTEGER_CST
)
5591 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
5592 C_TYPE_VARIABLE_SIZE (type
) = 1;
5595 /* The GCC extension for zero-length arrays differs from
5596 ISO flexible array members in that sizeof yields
5598 if (size
&& integer_zerop (size
))
5601 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
5602 TYPE_SIZE (type
) = bitsize_zero_node
;
5603 TYPE_SIZE_UNIT (type
) = size_zero_node
;
5604 SET_TYPE_STRUCTURAL_EQUALITY (type
);
5606 if (array_parm_vla_unspec_p
)
5609 /* The type is complete. C99 6.7.5.2p4 */
5610 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
5611 TYPE_SIZE (type
) = bitsize_zero_node
;
5612 TYPE_SIZE_UNIT (type
) = size_zero_node
;
5613 SET_TYPE_STRUCTURAL_EQUALITY (type
);
5617 if (decl_context
!= PARM
5618 && (array_ptr_quals
!= TYPE_UNQUALIFIED
5619 || array_ptr_attrs
!= NULL_TREE
5620 || array_parm_static
))
5622 error_at (loc
, "static or type qualifiers in non-parameter array declarator");
5623 array_ptr_quals
= TYPE_UNQUALIFIED
;
5624 array_ptr_attrs
= NULL_TREE
;
5625 array_parm_static
= 0;
5631 /* Say it's a definition only for the declarator closest
5632 to the identifier, apart possibly from some
5634 bool really_funcdef
= false;
5638 const struct c_declarator
*t
= declarator
->declarator
;
5639 while (t
->kind
== cdk_attrs
)
5641 really_funcdef
= (t
->kind
== cdk_id
);
5644 /* Declaring a function type. Make sure we have a valid
5645 type for the function to return. */
5646 if (type
== error_mark_node
)
5649 size_varies
= false;
5651 /* Warn about some types functions can't return. */
5652 if (TREE_CODE (type
) == FUNCTION_TYPE
)
5655 error_at (loc
, "%qE declared as function returning a "
5658 error_at (loc
, "type name declared as function "
5659 "returning a function");
5660 type
= integer_type_node
;
5662 if (TREE_CODE (type
) == ARRAY_TYPE
)
5665 error_at (loc
, "%qE declared as function returning an array",
5668 error_at (loc
, "type name declared as function returning "
5670 type
= integer_type_node
;
5672 errmsg
= targetm
.invalid_return_type (type
);
5676 type
= integer_type_node
;
5679 /* Construct the function type and go to the next
5680 inner layer of declarator. */
5681 arg_info
= declarator
->u
.arg_info
;
5682 arg_types
= grokparms (arg_info
, really_funcdef
);
5684 /* Type qualifiers before the return type of the function
5685 qualify the return type, not the function type. */
5688 /* Type qualifiers on a function return type are
5689 normally permitted by the standard but have no
5690 effect, so give a warning at -Wreturn-type.
5691 Qualifiers on a void return type are banned on
5692 function definitions in ISO C; GCC used to used
5693 them for noreturn functions. */
5694 if (VOID_TYPE_P (type
) && really_funcdef
)
5696 "function definition has qualified void return type");
5698 warning_at (loc
, OPT_Wignored_qualifiers
,
5699 "type qualifiers ignored on function return type");
5701 type
= c_build_qualified_type (type
, type_quals
);
5703 type_quals
= TYPE_UNQUALIFIED
;
5705 type
= build_function_type (type
, arg_types
);
5706 declarator
= declarator
->declarator
;
5708 /* Set the TYPE_CONTEXTs for each tagged type which is local to
5709 the formal parameter list of this FUNCTION_TYPE to point to
5710 the FUNCTION_TYPE node itself. */
5715 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info
->tags
, ix
, tag
)
5716 TYPE_CONTEXT (tag
->type
) = type
;
5722 /* Merge any constancy or volatility into the target type
5724 if ((type_quals
& TYPE_QUAL_ATOMIC
)
5725 && TREE_CODE (type
) == FUNCTION_TYPE
)
5728 "%<_Atomic%>-qualified function type");
5729 type_quals
&= ~TYPE_QUAL_ATOMIC
;
5731 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
5733 pedwarn (loc
, OPT_Wpedantic
,
5734 "ISO C forbids qualified function types");
5736 type
= c_build_qualified_type (type
, type_quals
);
5737 size_varies
= false;
5739 /* When the pointed-to type involves components of variable size,
5740 care must be taken to ensure that the size evaluation code is
5741 emitted early enough to dominate all the possible later uses
5742 and late enough for the variables on which it depends to have
5745 This is expected to happen automatically when the pointed-to
5746 type has a name/declaration of it's own, but special attention
5747 is required if the type is anonymous.
5749 We handle the NORMAL and FIELD contexts here by attaching an
5750 artificial TYPE_DECL to such pointed-to type. This forces the
5751 sizes evaluation at a safe point and ensures it is not deferred
5752 until e.g. within a deeper conditional context.
5754 We expect nothing to be needed here for PARM or TYPENAME.
5755 Pushing a TYPE_DECL at this point for TYPENAME would actually
5756 be incorrect, as we might be in the middle of an expression
5757 with side effects on the pointed-to type size "arguments" prior
5758 to the pointer declaration point and the fake TYPE_DECL in the
5759 enclosing context would force the size evaluation prior to the
5762 if (!TYPE_NAME (type
)
5763 && (decl_context
== NORMAL
|| decl_context
== FIELD
)
5764 && variably_modified_type_p (type
, NULL_TREE
))
5766 tree decl
= build_decl (loc
, TYPE_DECL
, NULL_TREE
, type
);
5767 DECL_ARTIFICIAL (decl
) = 1;
5769 finish_decl (decl
, loc
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
5770 TYPE_NAME (type
) = decl
;
5773 type
= c_build_pointer_type (type
);
5775 /* Process type qualifiers (such as const or volatile)
5776 that were given inside the `*'. */
5777 type_quals
= declarator
->u
.pointer_quals
;
5779 declarator
= declarator
->declarator
;
5786 *decl_attrs
= chainon (returned_attrs
, *decl_attrs
);
5788 /* Now TYPE has the actual type, apart from any qualifiers in
5791 /* Warn about address space used for things other than static memory or
5793 address_space
= DECODE_QUAL_ADDR_SPACE (type_quals
);
5794 if (!ADDR_SPACE_GENERIC_P (address_space
))
5796 if (decl_context
== NORMAL
)
5798 switch (storage_class
)
5801 error ("%qs combined with %<auto%> qualifier for %qE",
5802 c_addr_space_name (address_space
), name
);
5805 error ("%qs combined with %<register%> qualifier for %qE",
5806 c_addr_space_name (address_space
), name
);
5809 if (current_function_scope
)
5811 error ("%qs specified for auto variable %qE",
5812 c_addr_space_name (address_space
), name
);
5824 else if (decl_context
== PARM
&& TREE_CODE (type
) != ARRAY_TYPE
)
5827 error ("%qs specified for parameter %qE",
5828 c_addr_space_name (address_space
), name
);
5830 error ("%qs specified for unnamed parameter",
5831 c_addr_space_name (address_space
));
5833 else if (decl_context
== FIELD
)
5836 error ("%qs specified for structure field %qE",
5837 c_addr_space_name (address_space
), name
);
5839 error ("%qs specified for structure field",
5840 c_addr_space_name (address_space
));
5844 /* Check the type and width of a bit-field. */
5847 check_bitfield_type_and_width (&type
, width
, name
);
5848 /* C11 makes it implementation-defined (6.7.2.1#5) whether
5849 atomic types are permitted for bit-fields; we have no code to
5850 make bit-field accesses atomic, so disallow them. */
5851 if (type_quals
& TYPE_QUAL_ATOMIC
)
5854 error ("bit-field %qE has atomic type", name
);
5856 error ("bit-field has atomic type");
5857 type_quals
&= ~TYPE_QUAL_ATOMIC
;
5861 /* Reject invalid uses of _Alignas. */
5862 if (declspecs
->alignas_p
)
5864 if (storage_class
== csc_typedef
)
5865 error_at (loc
, "alignment specified for typedef %qE", name
);
5866 else if (storage_class
== csc_register
)
5867 error_at (loc
, "alignment specified for %<register%> object %qE",
5869 else if (decl_context
== PARM
)
5872 error_at (loc
, "alignment specified for parameter %qE", name
);
5874 error_at (loc
, "alignment specified for unnamed parameter");
5879 error_at (loc
, "alignment specified for bit-field %qE", name
);
5881 error_at (loc
, "alignment specified for unnamed bit-field");
5883 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
5884 error_at (loc
, "alignment specified for function %qE", name
);
5885 else if (declspecs
->align_log
!= -1)
5887 alignas_align
= 1U << declspecs
->align_log
;
5888 if (alignas_align
< TYPE_ALIGN_UNIT (type
))
5891 error_at (loc
, "%<_Alignas%> specifiers cannot reduce "
5892 "alignment of %qE", name
);
5894 error_at (loc
, "%<_Alignas%> specifiers cannot reduce "
5895 "alignment of unnamed field");
5901 /* Did array size calculations overflow or does the array cover more
5902 than half of the address-space? */
5903 if (TREE_CODE (type
) == ARRAY_TYPE
5904 && COMPLETE_TYPE_P (type
)
5905 && TREE_CODE (TYPE_SIZE_UNIT (type
)) == INTEGER_CST
5906 && ! valid_constant_size_p (TYPE_SIZE_UNIT (type
)))
5909 error_at (loc
, "size of array %qE is too large", name
);
5911 error_at (loc
, "size of unnamed array is too large");
5912 /* If we proceed with the array type as it is, we'll eventually
5913 crash in tree_to_uhwi (). */
5914 type
= error_mark_node
;
5917 /* If this is declaring a typedef name, return a TYPE_DECL. */
5919 if (storage_class
== csc_typedef
)
5922 if ((type_quals
& TYPE_QUAL_ATOMIC
)
5923 && TREE_CODE (type
) == FUNCTION_TYPE
)
5926 "%<_Atomic%>-qualified function type");
5927 type_quals
&= ~TYPE_QUAL_ATOMIC
;
5929 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
5931 pedwarn (loc
, OPT_Wpedantic
,
5932 "ISO C forbids qualified function types");
5934 type
= c_build_qualified_type (type
, type_quals
);
5935 decl
= build_decl (declarator
->id_loc
,
5936 TYPE_DECL
, declarator
->u
.id
, type
);
5937 if (declspecs
->explicit_signed_p
)
5938 C_TYPEDEF_EXPLICITLY_SIGNED (decl
) = 1;
5939 if (declspecs
->inline_p
)
5940 pedwarn (loc
, 0,"typedef %q+D declared %<inline%>", decl
);
5941 if (declspecs
->noreturn_p
)
5942 pedwarn (loc
, 0,"typedef %q+D declared %<_Noreturn%>", decl
);
5944 if (warn_cxx_compat
&& declarator
->u
.id
!= NULL_TREE
)
5946 struct c_binding
*b
= I_TAG_BINDING (declarator
->u
.id
);
5949 && b
->decl
!= NULL_TREE
5950 && (B_IN_CURRENT_SCOPE (b
)
5951 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
5952 && TYPE_MAIN_VARIANT (b
->decl
) != TYPE_MAIN_VARIANT (type
))
5954 warning_at (declarator
->id_loc
, OPT_Wc___compat
,
5955 ("using %qD as both a typedef and a tag is "
5958 if (b
->locus
!= UNKNOWN_LOCATION
)
5959 inform (b
->locus
, "originally defined here");
5966 /* If this is a type name (such as, in a cast or sizeof),
5967 compute the type and return it now. */
5969 if (decl_context
== TYPENAME
)
5971 /* Note that the grammar rejects storage classes in typenames
5973 gcc_assert (storage_class
== csc_none
&& !threadp
5974 && !declspecs
->inline_p
&& !declspecs
->noreturn_p
);
5975 if ((type_quals
& TYPE_QUAL_ATOMIC
)
5976 && TREE_CODE (type
) == FUNCTION_TYPE
)
5979 "%<_Atomic%>-qualified function type");
5980 type_quals
&= ~TYPE_QUAL_ATOMIC
;
5982 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
5984 pedwarn (loc
, OPT_Wpedantic
,
5985 "ISO C forbids const or volatile function types");
5987 type
= c_build_qualified_type (type
, type_quals
);
5991 if (pedantic
&& decl_context
== FIELD
5992 && variably_modified_type_p (type
, NULL_TREE
))
5995 pedwarn (loc
, OPT_Wpedantic
, "a member of a structure or union cannot "
5996 "have a variably modified type");
5999 /* Aside from typedefs and type names (handle above),
6000 `void' at top level (not within pointer)
6001 is allowed only in public variables.
6002 We don't complain about parms either, but that is because
6003 a better error message can be made later. */
6005 if (VOID_TYPE_P (type
) && decl_context
!= PARM
6006 && !((decl_context
!= FIELD
&& TREE_CODE (type
) != FUNCTION_TYPE
)
6007 && (storage_class
== csc_extern
6008 || (current_scope
== file_scope
6009 && !(storage_class
== csc_static
6010 || storage_class
== csc_register
)))))
6012 error_at (loc
, "variable or field %qE declared void", name
);
6013 type
= integer_type_node
;
6016 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6017 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6022 if (decl_context
== PARM
)
6026 /* A parameter declared as an array of T is really a pointer to T.
6027 One declared as a function is really a pointer to a function. */
6029 if (TREE_CODE (type
) == ARRAY_TYPE
)
6031 /* Transfer const-ness of array into that of type pointed to. */
6032 type
= TREE_TYPE (type
);
6034 type
= c_build_qualified_type (type
, type_quals
);
6035 type
= c_build_pointer_type (type
);
6036 type_quals
= array_ptr_quals
;
6038 type
= c_build_qualified_type (type
, type_quals
);
6040 /* We don't yet implement attributes in this context. */
6041 if (array_ptr_attrs
!= NULL_TREE
)
6042 warning_at (loc
, OPT_Wattributes
,
6043 "attributes in parameter array declarator ignored");
6045 size_varies
= false;
6047 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
6049 if (type_quals
& TYPE_QUAL_ATOMIC
)
6052 "%<_Atomic%>-qualified function type");
6053 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6055 else if (type_quals
)
6056 pedwarn (loc
, OPT_Wpedantic
,
6057 "ISO C forbids qualified function types");
6059 type
= c_build_qualified_type (type
, type_quals
);
6060 type
= c_build_pointer_type (type
);
6061 type_quals
= TYPE_UNQUALIFIED
;
6063 else if (type_quals
)
6064 type
= c_build_qualified_type (type
, type_quals
);
6066 decl
= build_decl (declarator
->id_loc
,
6067 PARM_DECL
, declarator
->u
.id
, type
);
6069 C_DECL_VARIABLE_SIZE (decl
) = 1;
6071 /* Compute the type actually passed in the parmlist,
6072 for the case where there is no prototype.
6073 (For example, shorts and chars are passed as ints.)
6074 When there is a prototype, this is overridden later. */
6076 if (type
== error_mark_node
)
6077 promoted_type
= type
;
6079 promoted_type
= c_type_promotes_to (type
);
6081 DECL_ARG_TYPE (decl
) = promoted_type
;
6082 if (declspecs
->inline_p
)
6083 pedwarn (loc
, 0, "parameter %q+D declared %<inline%>", decl
);
6084 if (declspecs
->noreturn_p
)
6085 pedwarn (loc
, 0, "parameter %q+D declared %<_Noreturn%>", decl
);
6087 else if (decl_context
== FIELD
)
6089 /* Note that the grammar rejects storage classes in typenames
6091 gcc_assert (storage_class
== csc_none
&& !threadp
6092 && !declspecs
->inline_p
&& !declspecs
->noreturn_p
);
6094 /* Structure field. It may not be a function. */
6096 if (TREE_CODE (type
) == FUNCTION_TYPE
)
6098 error_at (loc
, "field %qE declared as a function", name
);
6099 type
= build_pointer_type (type
);
6101 else if (TREE_CODE (type
) != ERROR_MARK
6102 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type
))
6105 error_at (loc
, "field %qE has incomplete type", name
);
6107 error_at (loc
, "unnamed field has incomplete type");
6108 type
= error_mark_node
;
6110 type
= c_build_qualified_type (type
, type_quals
);
6111 decl
= build_decl (declarator
->id_loc
,
6112 FIELD_DECL
, declarator
->u
.id
, type
);
6113 DECL_NONADDRESSABLE_P (decl
) = bitfield
;
6114 if (bitfield
&& !declarator
->u
.id
)
6115 TREE_NO_WARNING (decl
) = 1;
6118 C_DECL_VARIABLE_SIZE (decl
) = 1;
6120 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
6122 if (storage_class
== csc_register
|| threadp
)
6124 error_at (loc
, "invalid storage class for function %qE", name
);
6126 else if (current_scope
!= file_scope
)
6128 /* Function declaration not at file scope. Storage
6129 classes other than `extern' are not allowed, C99
6130 6.7.1p5, and `extern' makes no difference. However,
6131 GCC allows 'auto', perhaps with 'inline', to support
6132 nested functions. */
6133 if (storage_class
== csc_auto
)
6134 pedwarn (loc
, OPT_Wpedantic
,
6135 "invalid storage class for function %qE", name
);
6136 else if (storage_class
== csc_static
)
6138 error_at (loc
, "invalid storage class for function %qE", name
);
6140 storage_class
= declspecs
->storage_class
= csc_none
;
6146 decl
= build_decl (declarator
->id_loc
,
6147 FUNCTION_DECL
, declarator
->u
.id
, type
);
6148 decl
= build_decl_attribute_variant (decl
, decl_attr
);
6150 if (type_quals
& TYPE_QUAL_ATOMIC
)
6153 "%<_Atomic%>-qualified function type");
6154 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6156 else if (pedantic
&& type_quals
&& !DECL_IN_SYSTEM_HEADER (decl
))
6157 pedwarn (loc
, OPT_Wpedantic
,
6158 "ISO C forbids qualified function types");
6160 /* Every function declaration is an external reference
6161 (DECL_EXTERNAL) except for those which are not at file
6162 scope and are explicitly declared "auto". This is
6163 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6164 GCC to signify a forward declaration of a nested function. */
6165 if (storage_class
== csc_auto
&& current_scope
!= file_scope
)
6166 DECL_EXTERNAL (decl
) = 0;
6167 /* In C99, a function which is declared 'inline' with 'extern'
6168 is not an external reference (which is confusing). It
6169 means that the later definition of the function must be output
6170 in this file, C99 6.7.4p6. In GNU C89, a function declared
6171 'extern inline' is an external reference. */
6172 else if (declspecs
->inline_p
&& storage_class
!= csc_static
)
6173 DECL_EXTERNAL (decl
) = ((storage_class
== csc_extern
)
6174 == flag_gnu89_inline
);
6176 DECL_EXTERNAL (decl
) = !initialized
;
6178 /* Record absence of global scope for `static' or `auto'. */
6180 = !(storage_class
== csc_static
|| storage_class
== csc_auto
);
6182 /* For a function definition, record the argument information
6183 block where store_parm_decls will look for it. */
6185 current_function_arg_info
= arg_info
;
6187 if (declspecs
->default_int_p
)
6188 C_FUNCTION_IMPLICIT_INT (decl
) = 1;
6190 /* Record presence of `inline' and `_Noreturn', if it is
6192 if (flag_hosted
&& MAIN_NAME_P (declarator
->u
.id
))
6194 if (declspecs
->inline_p
)
6195 pedwarn (loc
, 0, "cannot inline function %<main%>");
6196 if (declspecs
->noreturn_p
)
6197 pedwarn (loc
, 0, "%<main%> declared %<_Noreturn%>");
6201 if (declspecs
->inline_p
)
6202 /* Record that the function is declared `inline'. */
6203 DECL_DECLARED_INLINE_P (decl
) = 1;
6204 if (declspecs
->noreturn_p
)
6209 pedwarn (loc
, OPT_Wpedantic
,
6210 "ISO C99 does not support %<_Noreturn%>");
6212 pedwarn (loc
, OPT_Wpedantic
,
6213 "ISO C90 does not support %<_Noreturn%>");
6215 TREE_THIS_VOLATILE (decl
) = 1;
6221 /* It's a variable. */
6222 /* An uninitialized decl with `extern' is a reference. */
6223 int extern_ref
= !initialized
&& storage_class
== csc_extern
;
6225 type
= c_build_qualified_type (type
, type_quals
);
6227 /* C99 6.2.2p7: It is invalid (compile-time undefined
6228 behavior) to create an 'extern' declaration for a
6229 variable if there is a global declaration that is
6230 'static' and the global declaration is not visible.
6231 (If the static declaration _is_ currently visible,
6232 the 'extern' declaration is taken to refer to that decl.) */
6233 if (extern_ref
&& current_scope
!= file_scope
)
6235 tree global_decl
= identifier_global_value (declarator
->u
.id
);
6236 tree visible_decl
= lookup_name (declarator
->u
.id
);
6239 && global_decl
!= visible_decl
6240 && TREE_CODE (global_decl
) == VAR_DECL
6241 && !TREE_PUBLIC (global_decl
))
6242 error_at (loc
, "variable previously declared %<static%> "
6243 "redeclared %<extern%>");
6246 decl
= build_decl (declarator
->id_loc
,
6247 VAR_DECL
, declarator
->u
.id
, type
);
6249 C_DECL_VARIABLE_SIZE (decl
) = 1;
6251 if (declspecs
->inline_p
)
6252 pedwarn (loc
, 0, "variable %q+D declared %<inline%>", decl
);
6253 if (declspecs
->noreturn_p
)
6254 pedwarn (loc
, 0, "variable %q+D declared %<_Noreturn%>", decl
);
6256 /* At file scope, an initialized extern declaration may follow
6257 a static declaration. In that case, DECL_EXTERNAL will be
6258 reset later in start_decl. */
6259 DECL_EXTERNAL (decl
) = (storage_class
== csc_extern
);
6261 /* At file scope, the presence of a `static' or `register' storage
6262 class specifier, or the absence of all storage class specifiers
6263 makes this declaration a definition (perhaps tentative). Also,
6264 the absence of `static' makes it public. */
6265 if (current_scope
== file_scope
)
6267 TREE_PUBLIC (decl
) = storage_class
!= csc_static
;
6268 TREE_STATIC (decl
) = !extern_ref
;
6270 /* Not at file scope, only `static' makes a static definition. */
6273 TREE_STATIC (decl
) = (storage_class
== csc_static
);
6274 TREE_PUBLIC (decl
) = extern_ref
;
6278 DECL_TLS_MODEL (decl
) = decl_default_tls_model (decl
);
6281 if ((storage_class
== csc_extern
6282 || (storage_class
== csc_none
6283 && TREE_CODE (type
) == FUNCTION_TYPE
6285 && variably_modified_type_p (type
, NULL_TREE
))
6288 if (TREE_CODE (type
) == FUNCTION_TYPE
)
6289 error_at (loc
, "non-nested function with variably modified type");
6291 error_at (loc
, "object with variably modified type must have "
6295 /* Record `register' declaration for warnings on &
6296 and in case doing stupid register allocation. */
6298 if (storage_class
== csc_register
)
6300 C_DECL_REGISTER (decl
) = 1;
6301 DECL_REGISTER (decl
) = 1;
6304 /* Record constancy and volatility. */
6305 c_apply_type_quals_to_decl (type_quals
, decl
);
6307 /* Apply _Alignas specifiers. */
6310 DECL_ALIGN (decl
) = alignas_align
* BITS_PER_UNIT
;
6311 DECL_USER_ALIGN (decl
) = 1;
6314 /* If a type has volatile components, it should be stored in memory.
6315 Otherwise, the fact that those components are volatile
6316 will be ignored, and would even crash the compiler.
6317 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6318 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl
))
6319 && (TREE_CODE (decl
) == VAR_DECL
|| TREE_CODE (decl
) == PARM_DECL
6320 || TREE_CODE (decl
) == RESULT_DECL
))
6322 /* It is not an error for a structure with volatile fields to
6323 be declared register, but reset DECL_REGISTER since it
6324 cannot actually go in a register. */
6325 int was_reg
= C_DECL_REGISTER (decl
);
6326 C_DECL_REGISTER (decl
) = 0;
6327 DECL_REGISTER (decl
) = 0;
6328 c_mark_addressable (decl
);
6329 C_DECL_REGISTER (decl
) = was_reg
;
6332 /* This is the earliest point at which we might know the assembler
6333 name of a variable. Thus, if it's known before this, die horribly. */
6334 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl
));
6337 && TREE_CODE (decl
) == VAR_DECL
6338 && TREE_PUBLIC (decl
)
6339 && TREE_STATIC (decl
)
6340 && (TREE_CODE (TREE_TYPE (decl
)) == RECORD_TYPE
6341 || TREE_CODE (TREE_TYPE (decl
)) == UNION_TYPE
6342 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
6343 && TYPE_NAME (TREE_TYPE (decl
)) == NULL_TREE
)
6344 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
6345 ("non-local variable %qD with anonymous type is "
6346 "questionable in C++"),
6353 /* Decode the parameter-list info for a function type or function definition.
6354 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6355 if there is an identifier list instead of a parameter decl list).
6356 These two functions are separate because when a function returns
6357 or receives functions then each is called multiple times but the order
6358 of calls is different. The last call to `grokparms' is always the one
6359 that contains the formal parameter names of a function definition.
6361 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6363 FUNCDEF_FLAG is true for a function definition, false for
6364 a mere declaration. A nonempty identifier-list gets an error message
6365 when FUNCDEF_FLAG is false. */
6368 grokparms (struct c_arg_info
*arg_info
, bool funcdef_flag
)
6370 tree arg_types
= arg_info
->types
;
6372 if (funcdef_flag
&& arg_info
->had_vla_unspec
)
6374 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6376 error ("%<[*]%> not allowed in other than function prototype scope");
6379 if (arg_types
== 0 && !funcdef_flag
&& !in_system_header
)
6380 warning (OPT_Wstrict_prototypes
,
6381 "function declaration isn%'t a prototype");
6383 if (arg_types
== error_mark_node
)
6384 return 0; /* don't set TYPE_ARG_TYPES in this case */
6386 else if (arg_types
&& TREE_CODE (TREE_VALUE (arg_types
)) == IDENTIFIER_NODE
)
6390 pedwarn (input_location
, 0, "parameter names (without types) in function declaration");
6391 arg_info
->parms
= NULL_TREE
;
6394 arg_info
->parms
= arg_info
->types
;
6396 arg_info
->types
= 0;
6401 tree parm
, type
, typelt
;
6402 unsigned int parmno
;
6405 /* If there is a parameter of incomplete type in a definition,
6406 this is an error. In a declaration this is valid, and a
6407 struct or union type may be completed later, before any calls
6408 or definition of the function. In the case where the tag was
6409 first declared within the parameter list, a warning has
6410 already been given. If a parameter has void type, then
6411 however the function cannot be defined or called, so
6414 for (parm
= arg_info
->parms
, typelt
= arg_types
, parmno
= 1;
6416 parm
= DECL_CHAIN (parm
), typelt
= TREE_CHAIN (typelt
), parmno
++)
6418 type
= TREE_VALUE (typelt
);
6419 if (type
== error_mark_node
)
6422 if (!COMPLETE_TYPE_P (type
))
6426 if (DECL_NAME (parm
))
6427 error_at (input_location
,
6428 "parameter %u (%q+D) has incomplete type",
6431 error_at (DECL_SOURCE_LOCATION (parm
),
6432 "parameter %u has incomplete type",
6435 TREE_VALUE (typelt
) = error_mark_node
;
6436 TREE_TYPE (parm
) = error_mark_node
;
6437 arg_types
= NULL_TREE
;
6439 else if (VOID_TYPE_P (type
))
6441 if (DECL_NAME (parm
))
6442 warning_at (input_location
, 0,
6443 "parameter %u (%q+D) has void type",
6446 warning_at (DECL_SOURCE_LOCATION (parm
), 0,
6447 "parameter %u has void type",
6452 errmsg
= targetm
.invalid_parameter_type (type
);
6456 TREE_VALUE (typelt
) = error_mark_node
;
6457 TREE_TYPE (parm
) = error_mark_node
;
6458 arg_types
= NULL_TREE
;
6461 if (DECL_NAME (parm
) && TREE_USED (parm
))
6462 warn_if_shadowing (parm
);
6468 /* Allocate and initialize a c_arg_info structure from the parser's
6472 build_arg_info (void)
6474 struct c_arg_info
*ret
= XOBNEW (&parser_obstack
, struct c_arg_info
);
6475 ret
->parms
= NULL_TREE
;
6477 ret
->types
= NULL_TREE
;
6478 ret
->others
= NULL_TREE
;
6479 ret
->pending_sizes
= NULL
;
6480 ret
->had_vla_unspec
= 0;
6484 /* Take apart the current scope and return a c_arg_info structure with
6485 info on a parameter list just parsed.
6487 This structure is later fed to 'grokparms' and 'store_parm_decls'.
6489 ELLIPSIS being true means the argument list ended in '...' so don't
6490 append a sentinel (void_list_node) to the end of the type-list.
6492 EXPR is NULL or an expression that needs to be evaluated for the
6493 side effects of array size expressions in the parameters. */
6496 get_parm_info (bool ellipsis
, tree expr
)
6498 struct c_binding
*b
= current_scope
->bindings
;
6499 struct c_arg_info
*arg_info
= build_arg_info ();
6502 vec
<c_arg_tag
, va_gc
> *tags
= NULL
;
6506 static bool explained_incomplete_types
= false;
6507 bool gave_void_only_once_err
= false;
6509 arg_info
->had_vla_unspec
= current_scope
->had_vla_unspec
;
6511 /* The bindings in this scope must not get put into a block.
6512 We will take care of deleting the binding nodes. */
6513 current_scope
->bindings
= 0;
6515 /* This function is only called if there was *something* on the
6519 /* A parameter list consisting solely of 'void' indicates that the
6520 function takes no arguments. But if the 'void' is qualified
6521 (by 'const' or 'volatile'), or has a storage class specifier
6522 ('register'), then the behavior is undefined; issue an error.
6523 Typedefs for 'void' are OK (see DR#157). */
6524 if (b
->prev
== 0 /* one binding */
6525 && TREE_CODE (b
->decl
) == PARM_DECL
/* which is a parameter */
6526 && !DECL_NAME (b
->decl
) /* anonymous */
6527 && VOID_TYPE_P (TREE_TYPE (b
->decl
))) /* of void type */
6529 if (TYPE_QUALS (TREE_TYPE (b
->decl
)) != TYPE_UNQUALIFIED
6530 || C_DECL_REGISTER (b
->decl
))
6531 error ("%<void%> as only parameter may not be qualified");
6533 /* There cannot be an ellipsis. */
6535 error ("%<void%> must be the only parameter");
6537 arg_info
->types
= void_list_node
;
6542 types
= void_list_node
;
6544 /* Break up the bindings list into parms, tags, types, and others;
6545 apply sanity checks; purge the name-to-decl bindings. */
6548 tree decl
= b
->decl
;
6549 tree type
= TREE_TYPE (decl
);
6551 const char *keyword
;
6553 switch (TREE_CODE (decl
))
6558 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
6559 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
6562 /* Check for forward decls that never got their actual decl. */
6563 if (TREE_ASM_WRITTEN (decl
))
6564 error ("parameter %q+D has just a forward declaration", decl
);
6565 /* Check for (..., void, ...) and issue an error. */
6566 else if (VOID_TYPE_P (type
) && !DECL_NAME (decl
))
6568 if (!gave_void_only_once_err
)
6570 error ("%<void%> must be the only parameter");
6571 gave_void_only_once_err
= true;
6576 /* Valid parameter, add it to the list. */
6577 DECL_CHAIN (decl
) = parms
;
6580 /* Since there is a prototype, args are passed in their
6581 declared types. The back end may override this later. */
6582 DECL_ARG_TYPE (decl
) = type
;
6583 types
= tree_cons (0, type
, types
);
6587 case ENUMERAL_TYPE
: keyword
= "enum"; goto tag
;
6588 case UNION_TYPE
: keyword
= "union"; goto tag
;
6589 case RECORD_TYPE
: keyword
= "struct"; goto tag
;
6591 /* Types may not have tag-names, in which case the type
6592 appears in the bindings list with b->id NULL. */
6595 gcc_assert (I_TAG_BINDING (b
->id
) == b
);
6596 I_TAG_BINDING (b
->id
) = b
->shadowed
;
6599 /* Warn about any struct, union or enum tags defined in a
6600 parameter list. The scope of such types is limited to
6601 the parameter list, which is rarely if ever desirable
6602 (it's impossible to call such a function with type-
6603 correct arguments). An anonymous union parm type is
6604 meaningful as a GNU extension, so don't warn for that. */
6605 if (TREE_CODE (decl
) != UNION_TYPE
|| b
->id
!= 0)
6608 /* The %s will be one of 'struct', 'union', or 'enum'. */
6609 warning (0, "%<%s %E%> declared inside parameter list",
6612 /* The %s will be one of 'struct', 'union', or 'enum'. */
6613 warning (0, "anonymous %s declared inside parameter list",
6616 if (!explained_incomplete_types
)
6618 warning (0, "its scope is only this definition or declaration,"
6619 " which is probably not what you want");
6620 explained_incomplete_types
= true;
6626 vec_safe_push (tags
, tag
);
6632 /* CONST_DECLs appear here when we have an embedded enum,
6633 and TYPE_DECLs appear here when we have an embedded struct
6634 or union. No warnings for this - we already warned about the
6635 type itself. FUNCTION_DECLs appear when there is an implicit
6636 function declaration in the parameter list. */
6638 /* When we reinsert this decl in the function body, we need
6639 to reconstruct whether it was marked as nested. */
6640 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
6643 DECL_CHAIN (decl
) = others
;
6648 /* error_mark_node appears here when we have an undeclared
6649 variable. Just throw it away. */
6652 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
6653 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
6657 /* Other things that might be encountered. */
6664 b
= free_binding_and_advance (b
);
6667 arg_info
->parms
= parms
;
6668 arg_info
->tags
= tags
;
6669 arg_info
->types
= types
;
6670 arg_info
->others
= others
;
6671 arg_info
->pending_sizes
= expr
;
6675 /* Get the struct, enum or union (CODE says which) with tag NAME.
6676 Define the tag as a forward-reference with location LOC if it is
6677 not defined. Return a c_typespec structure for the type
6681 parser_xref_tag (location_t loc
, enum tree_code code
, tree name
)
6683 struct c_typespec ret
;
6687 ret
.expr
= NULL_TREE
;
6688 ret
.expr_const_operands
= true;
6690 /* If a cross reference is requested, look up the type
6691 already defined for this tag and return it. */
6693 ref
= lookup_tag (code
, name
, 0, &refloc
);
6694 /* If this is the right type of tag, return what we found.
6695 (This reference will be shadowed by shadow_tag later if appropriate.)
6696 If this is the wrong type of tag, do not return it. If it was the
6697 wrong type in the same scope, we will have had an error
6698 message already; if in a different scope and declaring
6699 a name, pending_xref_error will give an error message; but if in a
6700 different scope and not declaring a name, this tag should
6701 shadow the previous declaration of a different type of tag, and
6702 this would not work properly if we return the reference found.
6703 (For example, with "struct foo" in an outer scope, "union foo;"
6704 must shadow that tag with a new one of union type.) */
6705 ret
.kind
= (ref
? ctsk_tagref
: ctsk_tagfirstref
);
6706 if (ref
&& TREE_CODE (ref
) == code
)
6708 if (C_TYPE_DEFINED_IN_STRUCT (ref
)
6709 && loc
!= UNKNOWN_LOCATION
6715 warning_at (loc
, OPT_Wc___compat
,
6716 ("enum type defined in struct or union "
6717 "is not visible in C++"));
6718 inform (refloc
, "enum type defined here");
6721 warning_at (loc
, OPT_Wc___compat
,
6722 ("struct defined in struct or union "
6723 "is not visible in C++"));
6724 inform (refloc
, "struct defined here");
6727 warning_at (loc
, OPT_Wc___compat
,
6728 ("union defined in struct or union "
6729 "is not visible in C++"));
6730 inform (refloc
, "union defined here");
6741 /* If no such tag is yet defined, create a forward-reference node
6742 and record it as the "definition".
6743 When a real declaration of this type is found,
6744 the forward-reference will be altered into a real type. */
6746 ref
= make_node (code
);
6747 if (code
== ENUMERAL_TYPE
)
6749 /* Give the type a default layout like unsigned int
6750 to avoid crashing if it does not get defined. */
6751 SET_TYPE_MODE (ref
, TYPE_MODE (unsigned_type_node
));
6752 TYPE_ALIGN (ref
) = TYPE_ALIGN (unsigned_type_node
);
6753 TYPE_USER_ALIGN (ref
) = 0;
6754 TYPE_UNSIGNED (ref
) = 1;
6755 TYPE_PRECISION (ref
) = TYPE_PRECISION (unsigned_type_node
);
6756 TYPE_MIN_VALUE (ref
) = TYPE_MIN_VALUE (unsigned_type_node
);
6757 TYPE_MAX_VALUE (ref
) = TYPE_MAX_VALUE (unsigned_type_node
);
6760 pushtag (loc
, name
, ref
);
6766 /* Get the struct, enum or union (CODE says which) with tag NAME.
6767 Define the tag as a forward-reference if it is not defined.
6768 Return a tree for the type. */
6771 xref_tag (enum tree_code code
, tree name
)
6773 return parser_xref_tag (input_location
, code
, name
).spec
;
6776 /* Make sure that the tag NAME is defined *in the current scope*
6777 at least as a forward reference.
6778 LOC is the location of the struct's definition.
6779 CODE says which kind of tag NAME ought to be.
6781 This stores the current value of the file static STRUCT_PARSE_INFO
6782 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
6783 new c_struct_parse_info structure. The old value of
6784 STRUCT_PARSE_INFO is restored in finish_struct. */
6787 start_struct (location_t loc
, enum tree_code code
, tree name
,
6788 struct c_struct_parse_info
**enclosing_struct_parse_info
)
6790 /* If there is already a tag defined at this scope
6791 (as a forward reference), just return it. */
6793 tree ref
= NULL_TREE
;
6794 location_t refloc
= UNKNOWN_LOCATION
;
6796 if (name
!= NULL_TREE
)
6797 ref
= lookup_tag (code
, name
, 1, &refloc
);
6798 if (ref
&& TREE_CODE (ref
) == code
)
6800 if (TYPE_SIZE (ref
))
6802 if (code
== UNION_TYPE
)
6803 error_at (loc
, "redefinition of %<union %E%>", name
);
6805 error_at (loc
, "redefinition of %<struct %E%>", name
);
6806 if (refloc
!= UNKNOWN_LOCATION
)
6807 inform (refloc
, "originally defined here");
6808 /* Don't create structures using a name already in use. */
6811 else if (C_TYPE_BEING_DEFINED (ref
))
6813 if (code
== UNION_TYPE
)
6814 error_at (loc
, "nested redefinition of %<union %E%>", name
);
6816 error_at (loc
, "nested redefinition of %<struct %E%>", name
);
6817 /* Don't bother to report "originally defined here" for a
6818 nested redefinition; the original definition should be
6820 /* Don't create structures that contain themselves. */
6825 /* Otherwise create a forward-reference just so the tag is in scope. */
6827 if (ref
== NULL_TREE
|| TREE_CODE (ref
) != code
)
6829 ref
= make_node (code
);
6830 pushtag (loc
, name
, ref
);
6833 C_TYPE_BEING_DEFINED (ref
) = 1;
6834 TYPE_PACKED (ref
) = flag_pack_struct
;
6836 *enclosing_struct_parse_info
= struct_parse_info
;
6837 struct_parse_info
= XNEW (struct c_struct_parse_info
);
6838 struct_parse_info
->struct_types
.create (0);
6839 struct_parse_info
->fields
.create (0);
6840 struct_parse_info
->typedefs_seen
.create (0);
6842 /* FIXME: This will issue a warning for a use of a type defined
6843 within a statement expr used within sizeof, et. al. This is not
6844 terribly serious as C++ doesn't permit statement exprs within
6846 if (warn_cxx_compat
&& (in_sizeof
|| in_typeof
|| in_alignof
))
6847 warning_at (loc
, OPT_Wc___compat
,
6848 "defining type in %qs expression is invalid in C++",
6851 : (in_typeof
? "typeof" : "alignof")));
6856 /* Process the specs, declarator and width (NULL if omitted)
6857 of a structure component, returning a FIELD_DECL node.
6858 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
6859 DECL_ATTRS is as for grokdeclarator.
6861 LOC is the location of the structure component.
6863 This is done during the parsing of the struct declaration.
6864 The FIELD_DECL nodes are chained together and the lot of them
6865 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
6868 grokfield (location_t loc
,
6869 struct c_declarator
*declarator
, struct c_declspecs
*declspecs
,
6870 tree width
, tree
*decl_attrs
)
6874 if (declarator
->kind
== cdk_id
&& declarator
->u
.id
== NULL_TREE
6875 && width
== NULL_TREE
)
6877 /* This is an unnamed decl.
6879 If we have something of the form "union { list } ;" then this
6880 is the anonymous union extension. Similarly for struct.
6882 If this is something of the form "struct foo;", then
6883 If MS or Plan 9 extensions are enabled, this is handled as
6884 an anonymous struct.
6885 Otherwise this is a forward declaration of a structure tag.
6887 If this is something of the form "foo;" and foo is a TYPE_DECL, then
6888 If foo names a structure or union without a tag, then this
6889 is an anonymous struct (this is permitted by C11).
6890 If MS or Plan 9 extensions are enabled and foo names a
6891 structure, then again this is an anonymous struct.
6892 Otherwise this is an error.
6894 Oh what a horrid tangled web we weave. I wonder if MS consciously
6895 took this from Plan 9 or if it was an accident of implementation
6896 that took root before someone noticed the bug... */
6898 tree type
= declspecs
->type
;
6899 bool type_ok
= (TREE_CODE (type
) == RECORD_TYPE
6900 || TREE_CODE (type
) == UNION_TYPE
);
6904 && (flag_ms_extensions
6905 || flag_plan9_extensions
6906 || !declspecs
->typedef_p
))
6908 if (flag_ms_extensions
|| flag_plan9_extensions
)
6910 else if (TYPE_NAME (type
) == NULL
)
6917 pedwarn (loc
, 0, "declaration does not declare anything");
6923 pedwarn (loc
, OPT_Wpedantic
,
6924 "ISO C99 doesn%'t support unnamed structs/unions");
6926 pedwarn (loc
, OPT_Wpedantic
,
6927 "ISO C90 doesn%'t support unnamed structs/unions");
6931 value
= grokdeclarator (declarator
, declspecs
, FIELD
, false,
6932 width
? &width
: NULL
, decl_attrs
, NULL
, NULL
,
6935 finish_decl (value
, loc
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
6936 DECL_INITIAL (value
) = width
;
6938 if (warn_cxx_compat
&& DECL_NAME (value
) != NULL_TREE
)
6940 /* If we currently have a binding for this field, set the
6941 in_struct field in the binding, so that we warn about lookups
6943 struct c_binding
*b
= I_SYMBOL_BINDING (DECL_NAME (value
));
6946 /* If the in_struct field is not yet set, push it on a list
6947 to be cleared when this struct is finished. */
6950 struct_parse_info
->fields
.safe_push (b
);
6959 /* Subroutine of detect_field_duplicates: return whether X and Y,
6960 which are both fields in the same struct, have duplicate field
6964 is_duplicate_field (tree x
, tree y
)
6966 if (DECL_NAME (x
) != NULL_TREE
&& DECL_NAME (x
) == DECL_NAME (y
))
6969 /* When using -fplan9-extensions, an anonymous field whose name is a
6970 typedef can duplicate a field name. */
6971 if (flag_plan9_extensions
6972 && (DECL_NAME (x
) == NULL_TREE
|| DECL_NAME (y
) == NULL_TREE
))
6974 tree xt
, xn
, yt
, yn
;
6977 if (DECL_NAME (x
) != NULL_TREE
)
6979 else if ((TREE_CODE (xt
) == RECORD_TYPE
|| TREE_CODE (xt
) == UNION_TYPE
)
6980 && TYPE_NAME (xt
) != NULL_TREE
6981 && TREE_CODE (TYPE_NAME (xt
)) == TYPE_DECL
)
6982 xn
= DECL_NAME (TYPE_NAME (xt
));
6987 if (DECL_NAME (y
) != NULL_TREE
)
6989 else if ((TREE_CODE (yt
) == RECORD_TYPE
|| TREE_CODE (yt
) == UNION_TYPE
)
6990 && TYPE_NAME (yt
) != NULL_TREE
6991 && TREE_CODE (TYPE_NAME (yt
)) == TYPE_DECL
)
6992 yn
= DECL_NAME (TYPE_NAME (yt
));
6996 if (xn
!= NULL_TREE
&& xn
== yn
)
7003 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7004 to HTAB, giving errors for any duplicates. */
7007 detect_field_duplicates_hash (tree fieldlist
,
7008 hash_table
<pointer_hash
<tree_node
> > htab
)
7013 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
7014 if ((y
= DECL_NAME (x
)) != 0)
7016 slot
= htab
.find_slot (y
, INSERT
);
7019 error ("duplicate member %q+D", x
);
7020 DECL_NAME (x
) = NULL_TREE
;
7024 else if (TREE_CODE (TREE_TYPE (x
)) == RECORD_TYPE
7025 || TREE_CODE (TREE_TYPE (x
)) == UNION_TYPE
)
7027 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x
)), htab
);
7029 /* When using -fplan9-extensions, an anonymous field whose
7030 name is a typedef can duplicate a field name. */
7031 if (flag_plan9_extensions
7032 && TYPE_NAME (TREE_TYPE (x
)) != NULL_TREE
7033 && TREE_CODE (TYPE_NAME (TREE_TYPE (x
))) == TYPE_DECL
)
7035 tree xn
= DECL_NAME (TYPE_NAME (TREE_TYPE (x
)));
7036 slot
= htab
.find_slot (xn
, INSERT
);
7038 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x
)));
7044 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7045 the list such that this does not present a problem later. */
7048 detect_field_duplicates (tree fieldlist
)
7053 /* If the struct is the list of instance variables of an Objective-C
7054 class, then we need to check all the instance variables of
7055 superclasses when checking for duplicates (since you can't have
7056 an instance variable in a subclass with the same name as an
7057 instance variable in a superclass). We pass on this job to the
7058 Objective-C compiler. objc_detect_field_duplicates() will return
7059 false if we are not checking the list of instance variables and
7060 the C frontend should proceed with the standard field duplicate
7061 checks. If we are checking the list of instance variables, the
7062 ObjC frontend will do the check, emit the errors if needed, and
7063 then return true. */
7064 if (c_dialect_objc ())
7065 if (objc_detect_field_duplicates (false))
7068 /* First, see if there are more than "a few" fields.
7069 This is trivially true if there are zero or one fields. */
7070 if (!fieldlist
|| !DECL_CHAIN (fieldlist
))
7075 if (DECL_NAME (x
) == NULL_TREE
7076 && (TREE_CODE (TREE_TYPE (x
)) == RECORD_TYPE
7077 || TREE_CODE (TREE_TYPE (x
)) == UNION_TYPE
))
7080 } while (timeout
> 0 && x
);
7082 /* If there were "few" fields and no anonymous structures or unions,
7083 avoid the overhead of allocating a hash table. Instead just do
7084 the nested traversal thing. */
7087 for (x
= DECL_CHAIN (fieldlist
); x
; x
= DECL_CHAIN (x
))
7088 /* When using -fplan9-extensions, we can have duplicates
7089 between typedef names and fields. */
7091 || (flag_plan9_extensions
7092 && DECL_NAME (x
) == NULL_TREE
7093 && (TREE_CODE (TREE_TYPE (x
)) == RECORD_TYPE
7094 || TREE_CODE (TREE_TYPE (x
)) == UNION_TYPE
)
7095 && TYPE_NAME (TREE_TYPE (x
)) != NULL_TREE
7096 && TREE_CODE (TYPE_NAME (TREE_TYPE (x
))) == TYPE_DECL
))
7098 for (y
= fieldlist
; y
!= x
; y
= TREE_CHAIN (y
))
7099 if (is_duplicate_field (y
, x
))
7101 error ("duplicate member %q+D", x
);
7102 DECL_NAME (x
) = NULL_TREE
;
7108 hash_table
<pointer_hash
<tree_node
> > htab
;
7111 detect_field_duplicates_hash (fieldlist
, htab
);
7116 /* Finish up struct info used by -Wc++-compat. */
7119 warn_cxx_compat_finish_struct (tree fieldlist
)
7123 struct c_binding
*b
;
7125 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7126 the current struct. We do this now at the end of the struct
7127 because the flag is used to issue visibility warnings, and we
7128 only want to issue those warnings if the type is referenced
7129 outside of the struct declaration. */
7130 FOR_EACH_VEC_ELT (struct_parse_info
->struct_types
, ix
, x
)
7131 C_TYPE_DEFINED_IN_STRUCT (x
) = 1;
7133 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7134 typedefs used when declaring fields in this struct. If the name
7135 of any of the fields is also a typedef name then the struct would
7136 not parse in C++, because the C++ lookup rules say that the
7137 typedef name would be looked up in the context of the struct, and
7138 would thus be the field rather than the typedef. */
7139 if (!struct_parse_info
->typedefs_seen
.is_empty ()
7140 && fieldlist
!= NULL_TREE
)
7142 /* Use a pointer_set using the name of the typedef. We can use
7143 a pointer_set because identifiers are interned. */
7144 struct pointer_set_t
*tset
= pointer_set_create ();
7146 FOR_EACH_VEC_ELT (struct_parse_info
->typedefs_seen
, ix
, x
)
7147 pointer_set_insert (tset
, DECL_NAME (x
));
7149 for (x
= fieldlist
; x
!= NULL_TREE
; x
= DECL_CHAIN (x
))
7151 if (DECL_NAME (x
) != NULL_TREE
7152 && pointer_set_contains (tset
, DECL_NAME (x
)))
7154 warning_at (DECL_SOURCE_LOCATION (x
), OPT_Wc___compat
,
7155 ("using %qD as both field and typedef name is "
7158 /* FIXME: It would be nice to report the location where
7159 the typedef name is used. */
7163 pointer_set_destroy (tset
);
7166 /* For each field which has a binding and which was not defined in
7167 an enclosing struct, clear the in_struct field. */
7168 FOR_EACH_VEC_ELT (struct_parse_info
->fields
, ix
, b
)
7172 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7173 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7174 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7175 ATTRIBUTES are attributes to be applied to the structure.
7177 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7178 the struct was started. */
7181 finish_struct (location_t loc
, tree t
, tree fieldlist
, tree attributes
,
7182 struct c_struct_parse_info
*enclosing_struct_parse_info
)
7185 bool toplevel
= file_scope
== current_scope
;
7186 int saw_named_field
;
7188 /* If this type was previously laid out as a forward reference,
7189 make sure we lay it out again. */
7193 decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
7197 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
7199 if (DECL_NAME (x
) != 0)
7202 && (TREE_CODE (TREE_TYPE (x
)) == RECORD_TYPE
7203 || TREE_CODE (TREE_TYPE (x
)) == UNION_TYPE
))
7209 if (TREE_CODE (t
) == UNION_TYPE
)
7212 pedwarn (loc
, OPT_Wpedantic
, "union has no named members");
7214 pedwarn (loc
, OPT_Wpedantic
, "union has no members");
7219 pedwarn (loc
, OPT_Wpedantic
, "struct has no named members");
7221 pedwarn (loc
, OPT_Wpedantic
, "struct has no members");
7226 /* Install struct as DECL_CONTEXT of each field decl.
7227 Also process specified field sizes, found in the DECL_INITIAL,
7228 storing 0 there after the type has been changed to precision equal
7229 to its width, rather than the precision of the specified standard
7230 type. (Correct layout requires the original type to have been preserved
7233 saw_named_field
= 0;
7234 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
7236 if (TREE_TYPE (x
) == error_mark_node
)
7239 DECL_CONTEXT (x
) = t
;
7241 /* If any field is const, the structure type is pseudo-const. */
7242 if (TREE_READONLY (x
))
7243 C_TYPE_FIELDS_READONLY (t
) = 1;
7246 /* A field that is pseudo-const makes the structure likewise. */
7247 tree t1
= strip_array_types (TREE_TYPE (x
));
7248 if ((TREE_CODE (t1
) == RECORD_TYPE
|| TREE_CODE (t1
) == UNION_TYPE
)
7249 && C_TYPE_FIELDS_READONLY (t1
))
7250 C_TYPE_FIELDS_READONLY (t
) = 1;
7253 /* Any field that is volatile means variables of this type must be
7254 treated in some ways as volatile. */
7255 if (TREE_THIS_VOLATILE (x
))
7256 C_TYPE_FIELDS_VOLATILE (t
) = 1;
7258 /* Any field of nominal variable size implies structure is too. */
7259 if (C_DECL_VARIABLE_SIZE (x
))
7260 C_TYPE_VARIABLE_SIZE (t
) = 1;
7262 if (DECL_INITIAL (x
))
7264 unsigned HOST_WIDE_INT width
= tree_to_uhwi (DECL_INITIAL (x
));
7265 DECL_SIZE (x
) = bitsize_int (width
);
7266 DECL_BIT_FIELD (x
) = 1;
7267 SET_DECL_C_BIT_FIELD (x
);
7271 && (DECL_BIT_FIELD (x
)
7272 || TYPE_ALIGN (TREE_TYPE (x
)) > BITS_PER_UNIT
))
7273 DECL_PACKED (x
) = 1;
7275 /* Detect flexible array member in an invalid context. */
7276 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
7277 && TYPE_SIZE (TREE_TYPE (x
)) == NULL_TREE
7278 && TYPE_DOMAIN (TREE_TYPE (x
)) != NULL_TREE
7279 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x
))) == NULL_TREE
)
7281 if (TREE_CODE (t
) == UNION_TYPE
)
7283 error_at (DECL_SOURCE_LOCATION (x
),
7284 "flexible array member in union");
7285 TREE_TYPE (x
) = error_mark_node
;
7287 else if (DECL_CHAIN (x
) != NULL_TREE
)
7289 error_at (DECL_SOURCE_LOCATION (x
),
7290 "flexible array member not at end of struct");
7291 TREE_TYPE (x
) = error_mark_node
;
7293 else if (!saw_named_field
)
7295 error_at (DECL_SOURCE_LOCATION (x
),
7296 "flexible array member in otherwise empty struct");
7297 TREE_TYPE (x
) = error_mark_node
;
7301 if (pedantic
&& TREE_CODE (t
) == RECORD_TYPE
7302 && flexible_array_type_p (TREE_TYPE (x
)))
7303 pedwarn (DECL_SOURCE_LOCATION (x
), OPT_Wpedantic
,
7304 "invalid use of structure with flexible array member");
7307 || TREE_CODE (TREE_TYPE (x
)) == RECORD_TYPE
7308 || TREE_CODE (TREE_TYPE (x
)) == UNION_TYPE
)
7309 saw_named_field
= 1;
7312 detect_field_duplicates (fieldlist
);
7314 /* Now we have the nearly final fieldlist. Record it,
7315 then lay out the structure or union (including the fields). */
7317 TYPE_FIELDS (t
) = fieldlist
;
7321 if (TYPE_SIZE_UNIT (t
)
7322 && TREE_CODE (TYPE_SIZE_UNIT (t
)) == INTEGER_CST
7323 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t
))
7324 && !valid_constant_size_p (TYPE_SIZE_UNIT (t
)))
7325 error ("type %qT is too large", t
);
7327 /* Give bit-fields their proper types. */
7329 tree
*fieldlistp
= &fieldlist
;
7331 if (TREE_CODE (*fieldlistp
) == FIELD_DECL
&& DECL_INITIAL (*fieldlistp
)
7332 && TREE_TYPE (*fieldlistp
) != error_mark_node
)
7334 unsigned HOST_WIDE_INT width
7335 = tree_to_uhwi (DECL_INITIAL (*fieldlistp
));
7336 tree type
= TREE_TYPE (*fieldlistp
);
7337 if (width
!= TYPE_PRECISION (type
))
7339 TREE_TYPE (*fieldlistp
)
7340 = c_build_bitfield_integer_type (width
, TYPE_UNSIGNED (type
));
7341 DECL_MODE (*fieldlistp
) = TYPE_MODE (TREE_TYPE (*fieldlistp
));
7343 DECL_INITIAL (*fieldlistp
) = 0;
7346 fieldlistp
= &DECL_CHAIN (*fieldlistp
);
7349 /* Now we have the truly final field list.
7350 Store it in this type and in the variants. */
7352 TYPE_FIELDS (t
) = fieldlist
;
7354 /* If there are lots of fields, sort so we can look through them fast.
7355 We arbitrarily consider 16 or more elts to be "a lot". */
7360 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
7362 if (len
> 15 || DECL_NAME (x
) == NULL
)
7370 struct lang_type
*space
;
7371 struct sorted_fields_type
*space2
;
7373 len
+= list_length (x
);
7375 /* Use the same allocation policy here that make_node uses, to
7376 ensure that this lives as long as the rest of the struct decl.
7377 All decls in an inline function need to be saved. */
7379 space
= ggc_alloc_cleared_lang_type (sizeof (struct lang_type
));
7380 space2
= ggc_alloc_sorted_fields_type
7381 (sizeof (struct sorted_fields_type
) + len
* sizeof (tree
));
7385 field_array
= &space2
->elts
[0];
7386 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
7388 field_array
[len
++] = x
;
7390 /* If there is anonymous struct or union, break out of the loop. */
7391 if (DECL_NAME (x
) == NULL
)
7394 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
7397 TYPE_LANG_SPECIFIC (t
) = space
;
7398 TYPE_LANG_SPECIFIC (t
)->s
->len
= len
;
7399 field_array
= TYPE_LANG_SPECIFIC (t
)->s
->elts
;
7400 qsort (field_array
, len
, sizeof (tree
), field_decl_cmp
);
7405 for (x
= TYPE_MAIN_VARIANT (t
); x
; x
= TYPE_NEXT_VARIANT (x
))
7407 TYPE_FIELDS (x
) = TYPE_FIELDS (t
);
7408 TYPE_LANG_SPECIFIC (x
) = TYPE_LANG_SPECIFIC (t
);
7409 C_TYPE_FIELDS_READONLY (x
) = C_TYPE_FIELDS_READONLY (t
);
7410 C_TYPE_FIELDS_VOLATILE (x
) = C_TYPE_FIELDS_VOLATILE (t
);
7411 C_TYPE_VARIABLE_SIZE (x
) = C_TYPE_VARIABLE_SIZE (t
);
7414 /* If this was supposed to be a transparent union, but we can't
7415 make it one, warn and turn off the flag. */
7416 if (TREE_CODE (t
) == UNION_TYPE
7417 && TYPE_TRANSPARENT_AGGR (t
)
7418 && (!TYPE_FIELDS (t
) || TYPE_MODE (t
) != DECL_MODE (TYPE_FIELDS (t
))))
7420 TYPE_TRANSPARENT_AGGR (t
) = 0;
7421 warning_at (loc
, 0, "union cannot be made transparent");
7424 /* If this structure or union completes the type of any previous
7425 variable declaration, lay it out and output its rtl. */
7426 for (x
= C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t
));
7430 tree decl
= TREE_VALUE (x
);
7431 if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
7432 layout_array_type (TREE_TYPE (decl
));
7433 if (TREE_CODE (decl
) != TYPE_DECL
)
7435 layout_decl (decl
, 0);
7436 if (c_dialect_objc ())
7437 objc_check_decl (decl
);
7438 rest_of_decl_compilation (decl
, toplevel
, 0);
7441 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t
)) = 0;
7443 /* Update type location to the one of the definition, instead of e.g.
7444 a forward declaration. */
7445 if (TYPE_STUB_DECL (t
))
7446 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t
)) = loc
;
7448 /* Finish debugging output for this type. */
7449 rest_of_type_compilation (t
, toplevel
);
7451 /* If we're inside a function proper, i.e. not file-scope and not still
7452 parsing parameters, then arrange for the size of a variable sized type
7454 if (building_stmt_list_p () && variably_modified_type_p (t
, NULL_TREE
))
7455 add_stmt (build_stmt (loc
,
7456 DECL_EXPR
, build_decl (loc
, TYPE_DECL
, NULL
, t
)));
7458 if (warn_cxx_compat
)
7459 warn_cxx_compat_finish_struct (fieldlist
);
7461 struct_parse_info
->struct_types
.release ();
7462 struct_parse_info
->fields
.release ();
7463 struct_parse_info
->typedefs_seen
.release ();
7464 XDELETE (struct_parse_info
);
7466 struct_parse_info
= enclosing_struct_parse_info
;
7468 /* If this struct is defined inside a struct, add it to
7471 && struct_parse_info
!= NULL
7472 && !in_sizeof
&& !in_typeof
&& !in_alignof
)
7473 struct_parse_info
->struct_types
.safe_push (t
);
7478 /* Lay out the type T, and its element type, and so on. */
7481 layout_array_type (tree t
)
7483 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
7484 layout_array_type (TREE_TYPE (t
));
7488 /* Begin compiling the definition of an enumeration type.
7489 NAME is its name (or null if anonymous).
7490 LOC is the enum's location.
7491 Returns the type object, as yet incomplete.
7492 Also records info about it so that build_enumerator
7493 may be used to declare the individual values as they are read. */
7496 start_enum (location_t loc
, struct c_enum_contents
*the_enum
, tree name
)
7498 tree enumtype
= NULL_TREE
;
7499 location_t enumloc
= UNKNOWN_LOCATION
;
7501 /* If this is the real definition for a previous forward reference,
7502 fill in the contents in the same object that used to be the
7503 forward reference. */
7505 if (name
!= NULL_TREE
)
7506 enumtype
= lookup_tag (ENUMERAL_TYPE
, name
, 1, &enumloc
);
7508 if (enumtype
== 0 || TREE_CODE (enumtype
) != ENUMERAL_TYPE
)
7510 enumtype
= make_node (ENUMERAL_TYPE
);
7511 pushtag (loc
, name
, enumtype
);
7514 if (C_TYPE_BEING_DEFINED (enumtype
))
7515 error_at (loc
, "nested redefinition of %<enum %E%>", name
);
7517 C_TYPE_BEING_DEFINED (enumtype
) = 1;
7519 if (TYPE_VALUES (enumtype
) != 0)
7521 /* This enum is a named one that has been declared already. */
7522 error_at (loc
, "redeclaration of %<enum %E%>", name
);
7523 if (enumloc
!= UNKNOWN_LOCATION
)
7524 inform (enumloc
, "originally defined here");
7526 /* Completely replace its old definition.
7527 The old enumerators remain defined, however. */
7528 TYPE_VALUES (enumtype
) = 0;
7531 the_enum
->enum_next_value
= integer_zero_node
;
7532 the_enum
->enum_overflow
= 0;
7534 if (flag_short_enums
)
7535 TYPE_PACKED (enumtype
) = 1;
7537 /* FIXME: This will issue a warning for a use of a type defined
7538 within sizeof in a statement expr. This is not terribly serious
7539 as C++ doesn't permit statement exprs within sizeof anyhow. */
7540 if (warn_cxx_compat
&& (in_sizeof
|| in_typeof
|| in_alignof
))
7541 warning_at (loc
, OPT_Wc___compat
,
7542 "defining type in %qs expression is invalid in C++",
7545 : (in_typeof
? "typeof" : "alignof")));
7550 /* After processing and defining all the values of an enumeration type,
7551 install their decls in the enumeration type and finish it off.
7552 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
7553 and ATTRIBUTES are the specified attributes.
7554 Returns ENUMTYPE. */
7557 finish_enum (tree enumtype
, tree values
, tree attributes
)
7560 tree minnode
= 0, maxnode
= 0;
7563 bool toplevel
= (file_scope
== current_scope
);
7564 struct lang_type
*lt
;
7566 decl_attributes (&enumtype
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
7568 /* Calculate the maximum value of any enumerator in this type. */
7570 if (values
== error_mark_node
)
7571 minnode
= maxnode
= integer_zero_node
;
7574 minnode
= maxnode
= TREE_VALUE (values
);
7575 for (pair
= TREE_CHAIN (values
); pair
; pair
= TREE_CHAIN (pair
))
7577 tree value
= TREE_VALUE (pair
);
7578 if (tree_int_cst_lt (maxnode
, value
))
7580 if (tree_int_cst_lt (value
, minnode
))
7585 /* Construct the final type of this enumeration. It is the same
7586 as one of the integral types - the narrowest one that fits, except
7587 that normally we only go as narrow as int - and signed iff any of
7588 the values are negative. */
7589 sign
= (tree_int_cst_sgn (minnode
) >= 0) ? UNSIGNED
: SIGNED
;
7590 precision
= MAX (tree_int_cst_min_precision (minnode
, sign
),
7591 tree_int_cst_min_precision (maxnode
, sign
));
7593 if (TYPE_PACKED (enumtype
) || precision
> TYPE_PRECISION (integer_type_node
))
7595 tem
= c_common_type_for_size (precision
, sign
== UNSIGNED
? 1 : 0);
7598 warning (0, "enumeration values exceed range of largest integer");
7599 tem
= long_long_integer_type_node
;
7603 tem
= sign
== UNSIGNED
? unsigned_type_node
: integer_type_node
;
7605 TYPE_MIN_VALUE (enumtype
) = TYPE_MIN_VALUE (tem
);
7606 TYPE_MAX_VALUE (enumtype
) = TYPE_MAX_VALUE (tem
);
7607 TYPE_UNSIGNED (enumtype
) = TYPE_UNSIGNED (tem
);
7608 TYPE_SIZE (enumtype
) = 0;
7610 /* If the precision of the type was specific with an attribute and it
7611 was too small, give an error. Otherwise, use it. */
7612 if (TYPE_PRECISION (enumtype
))
7614 if (precision
> TYPE_PRECISION (enumtype
))
7615 error ("specified mode too small for enumeral values");
7618 TYPE_PRECISION (enumtype
) = TYPE_PRECISION (tem
);
7620 layout_type (enumtype
);
7622 if (values
!= error_mark_node
)
7624 /* Change the type of the enumerators to be the enum type. We
7625 need to do this irrespective of the size of the enum, for
7626 proper type checking. Replace the DECL_INITIALs of the
7627 enumerators, and the value slots of the list, with copies
7628 that have the enum type; they cannot be modified in place
7629 because they may be shared (e.g. integer_zero_node) Finally,
7630 change the purpose slots to point to the names of the decls. */
7631 for (pair
= values
; pair
; pair
= TREE_CHAIN (pair
))
7633 tree enu
= TREE_PURPOSE (pair
);
7634 tree ini
= DECL_INITIAL (enu
);
7636 TREE_TYPE (enu
) = enumtype
;
7638 /* The ISO C Standard mandates enumerators to have type int,
7639 even though the underlying type of an enum type is
7640 unspecified. However, GCC allows enumerators of any
7641 integer type as an extensions. build_enumerator()
7642 converts any enumerators that fit in an int to type int,
7643 to avoid promotions to unsigned types when comparing
7644 integers with enumerators that fit in the int range.
7645 When -pedantic is given, build_enumerator() would have
7646 already warned about those that don't fit. Here we
7647 convert the rest to the enumerator type. */
7648 if (TREE_TYPE (ini
) != integer_type_node
)
7649 ini
= convert (enumtype
, ini
);
7651 DECL_INITIAL (enu
) = ini
;
7652 TREE_PURPOSE (pair
) = DECL_NAME (enu
);
7653 TREE_VALUE (pair
) = ini
;
7656 TYPE_VALUES (enumtype
) = values
;
7659 /* Record the min/max values so that we can warn about bit-field
7660 enumerations that are too small for the values. */
7661 lt
= ggc_alloc_cleared_lang_type (sizeof (struct lang_type
));
7662 lt
->enum_min
= minnode
;
7663 lt
->enum_max
= maxnode
;
7664 TYPE_LANG_SPECIFIC (enumtype
) = lt
;
7666 /* Fix up all variant types of this enum type. */
7667 for (tem
= TYPE_MAIN_VARIANT (enumtype
); tem
; tem
= TYPE_NEXT_VARIANT (tem
))
7669 if (tem
== enumtype
)
7671 TYPE_VALUES (tem
) = TYPE_VALUES (enumtype
);
7672 TYPE_MIN_VALUE (tem
) = TYPE_MIN_VALUE (enumtype
);
7673 TYPE_MAX_VALUE (tem
) = TYPE_MAX_VALUE (enumtype
);
7674 TYPE_SIZE (tem
) = TYPE_SIZE (enumtype
);
7675 TYPE_SIZE_UNIT (tem
) = TYPE_SIZE_UNIT (enumtype
);
7676 SET_TYPE_MODE (tem
, TYPE_MODE (enumtype
));
7677 TYPE_PRECISION (tem
) = TYPE_PRECISION (enumtype
);
7678 TYPE_ALIGN (tem
) = TYPE_ALIGN (enumtype
);
7679 TYPE_USER_ALIGN (tem
) = TYPE_USER_ALIGN (enumtype
);
7680 TYPE_UNSIGNED (tem
) = TYPE_UNSIGNED (enumtype
);
7681 TYPE_LANG_SPECIFIC (tem
) = TYPE_LANG_SPECIFIC (enumtype
);
7684 /* Finish debugging output for this type. */
7685 rest_of_type_compilation (enumtype
, toplevel
);
7687 /* If this enum is defined inside a struct, add it to
7690 && struct_parse_info
!= NULL
7691 && !in_sizeof
&& !in_typeof
&& !in_alignof
)
7692 struct_parse_info
->struct_types
.safe_push (enumtype
);
7697 /* Build and install a CONST_DECL for one value of the
7698 current enumeration type (one that was begun with start_enum).
7699 DECL_LOC is the location of the enumerator.
7700 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
7701 Return a tree-list containing the CONST_DECL and its value.
7702 Assignment of sequential values by default is handled here. */
7705 build_enumerator (location_t decl_loc
, location_t loc
,
7706 struct c_enum_contents
*the_enum
, tree name
, tree value
)
7710 /* Validate and default VALUE. */
7714 /* Don't issue more errors for error_mark_node (i.e. an
7715 undeclared identifier) - just ignore the value expression. */
7716 if (value
== error_mark_node
)
7718 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value
)))
7720 error_at (loc
, "enumerator value for %qE is not an integer constant",
7726 if (TREE_CODE (value
) != INTEGER_CST
)
7728 value
= c_fully_fold (value
, false, NULL
);
7729 if (TREE_CODE (value
) == INTEGER_CST
)
7730 pedwarn (loc
, OPT_Wpedantic
,
7731 "enumerator value for %qE is not an integer "
7732 "constant expression", name
);
7734 if (TREE_CODE (value
) != INTEGER_CST
)
7736 error ("enumerator value for %qE is not an integer constant",
7742 value
= default_conversion (value
);
7743 constant_expression_warning (value
);
7748 /* Default based on previous value. */
7749 /* It should no longer be possible to have NON_LVALUE_EXPR
7753 value
= the_enum
->enum_next_value
;
7754 if (the_enum
->enum_overflow
)
7755 error_at (loc
, "overflow in enumeration values");
7757 /* Even though the underlying type of an enum is unspecified, the
7758 type of enumeration constants is explicitly defined as int
7759 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
7761 else if (!int_fits_type_p (value
, integer_type_node
))
7762 pedwarn (loc
, OPT_Wpedantic
,
7763 "ISO C restricts enumerator values to range of %<int%>");
7765 /* The ISO C Standard mandates enumerators to have type int, even
7766 though the underlying type of an enum type is unspecified.
7767 However, GCC allows enumerators of any integer type as an
7768 extensions. Here we convert any enumerators that fit in an int
7769 to type int, to avoid promotions to unsigned types when comparing
7770 integers with enumerators that fit in the int range. When
7771 -pedantic is given, we would have already warned about those that
7772 don't fit. We have to do this here rather than in finish_enum
7773 because this value may be used to define more enumerators. */
7774 if (int_fits_type_p (value
, integer_type_node
))
7775 value
= convert (integer_type_node
, value
);
7777 /* Set basis for default for next value. */
7778 the_enum
->enum_next_value
7779 = build_binary_op (EXPR_LOC_OR_HERE (value
),
7780 PLUS_EXPR
, value
, integer_one_node
, 0);
7781 the_enum
->enum_overflow
= tree_int_cst_lt (the_enum
->enum_next_value
, value
);
7783 /* Now create a declaration for the enum value name. */
7785 type
= TREE_TYPE (value
);
7786 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
7787 TYPE_PRECISION (integer_type_node
)),
7788 (TYPE_PRECISION (type
)
7789 >= TYPE_PRECISION (integer_type_node
)
7790 && TYPE_UNSIGNED (type
)));
7792 decl
= build_decl (decl_loc
, CONST_DECL
, name
, type
);
7793 DECL_INITIAL (decl
) = convert (type
, value
);
7796 return tree_cons (decl
, value
, NULL_TREE
);
7800 /* Create the FUNCTION_DECL for a function definition.
7801 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
7802 the declaration; they describe the function's name and the type it returns,
7803 but twisted together in a fashion that parallels the syntax of C.
7805 This function creates a binding context for the function body
7806 as well as setting up the FUNCTION_DECL in current_function_decl.
7808 Returns 1 on success. If the DECLARATOR is not suitable for a function
7809 (it defines a datum instead), we return 0, which tells
7810 yyparse to report a parse error. */
7813 start_function (struct c_declspecs
*declspecs
, struct c_declarator
*declarator
,
7816 tree decl1
, old_decl
;
7817 tree restype
, resdecl
;
7820 current_function_returns_value
= 0; /* Assume, until we see it does. */
7821 current_function_returns_null
= 0;
7822 current_function_returns_abnormally
= 0;
7823 warn_about_return_type
= 0;
7824 c_switch_stack
= NULL
;
7826 /* Indicate no valid break/continue context by setting these variables
7827 to some non-null, non-label value. We'll notice and emit the proper
7828 error message in c_finish_bc_stmt. */
7829 c_break_label
= c_cont_label
= size_zero_node
;
7831 decl1
= grokdeclarator (declarator
, declspecs
, FUNCDEF
, true, NULL
,
7832 &attributes
, NULL
, NULL
, DEPRECATED_NORMAL
);
7834 /* If the declarator is not suitable for a function definition,
7835 cause a syntax error. */
7837 || TREE_CODE (decl1
) != FUNCTION_DECL
)
7840 loc
= DECL_SOURCE_LOCATION (decl1
);
7842 c_decl_attributes (&decl1
, attributes
, 0);
7844 if (DECL_DECLARED_INLINE_P (decl1
)
7845 && DECL_UNINLINABLE (decl1
)
7846 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1
)))
7847 warning_at (loc
, OPT_Wattributes
,
7848 "inline function %qD given attribute noinline",
7851 /* Handle gnu_inline attribute. */
7852 if (declspecs
->inline_p
7853 && !flag_gnu89_inline
7854 && TREE_CODE (decl1
) == FUNCTION_DECL
7855 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1
))
7856 || current_function_decl
))
7858 if (declspecs
->storage_class
!= csc_static
)
7859 DECL_EXTERNAL (decl1
) = !DECL_EXTERNAL (decl1
);
7862 announce_function (decl1
);
7864 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1
))))
7866 error_at (loc
, "return type is an incomplete type");
7867 /* Make it return void instead. */
7869 = build_function_type (void_type_node
,
7870 TYPE_ARG_TYPES (TREE_TYPE (decl1
)));
7873 if (warn_about_return_type
)
7874 pedwarn_c99 (loc
, flag_isoc99
? 0
7875 : (warn_return_type
? OPT_Wreturn_type
: OPT_Wimplicit_int
),
7876 "return type defaults to %<int%>");
7878 /* Make the init_value nonzero so pushdecl knows this is not tentative.
7879 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
7880 DECL_INITIAL (decl1
) = error_mark_node
;
7882 /* A nested function is not global. */
7883 if (current_function_decl
!= 0)
7884 TREE_PUBLIC (decl1
) = 0;
7886 /* If this definition isn't a prototype and we had a prototype declaration
7887 before, copy the arg type info from that prototype. */
7888 old_decl
= lookup_name_in_scope (DECL_NAME (decl1
), current_scope
);
7889 if (old_decl
&& TREE_CODE (old_decl
) != FUNCTION_DECL
)
7891 current_function_prototype_locus
= UNKNOWN_LOCATION
;
7892 current_function_prototype_built_in
= false;
7893 current_function_prototype_arg_types
= NULL_TREE
;
7894 if (!prototype_p (TREE_TYPE (decl1
)))
7896 if (old_decl
!= 0 && TREE_CODE (TREE_TYPE (old_decl
)) == FUNCTION_TYPE
7897 && comptypes (TREE_TYPE (TREE_TYPE (decl1
)),
7898 TREE_TYPE (TREE_TYPE (old_decl
))))
7900 TREE_TYPE (decl1
) = composite_type (TREE_TYPE (old_decl
),
7902 current_function_prototype_locus
= DECL_SOURCE_LOCATION (old_decl
);
7903 current_function_prototype_built_in
7904 = C_DECL_BUILTIN_PROTOTYPE (old_decl
);
7905 current_function_prototype_arg_types
7906 = TYPE_ARG_TYPES (TREE_TYPE (decl1
));
7908 if (TREE_PUBLIC (decl1
))
7910 /* If there is an external prototype declaration of this
7911 function, record its location but do not copy information
7912 to this decl. This may be an invisible declaration
7913 (built-in or in a scope which has finished) or simply
7914 have more refined argument types than any declaration
7916 struct c_binding
*b
;
7917 for (b
= I_SYMBOL_BINDING (DECL_NAME (decl1
)); b
; b
= b
->shadowed
)
7918 if (B_IN_SCOPE (b
, external_scope
))
7922 tree ext_decl
, ext_type
;
7924 ext_type
= b
->u
.type
? b
->u
.type
: TREE_TYPE (ext_decl
);
7925 if (TREE_CODE (ext_type
) == FUNCTION_TYPE
7926 && comptypes (TREE_TYPE (TREE_TYPE (decl1
)),
7927 TREE_TYPE (ext_type
)))
7929 current_function_prototype_locus
7930 = DECL_SOURCE_LOCATION (ext_decl
);
7931 current_function_prototype_built_in
7932 = C_DECL_BUILTIN_PROTOTYPE (ext_decl
);
7933 current_function_prototype_arg_types
7934 = TYPE_ARG_TYPES (ext_type
);
7940 /* Optionally warn of old-fashioned def with no previous prototype. */
7941 if (warn_strict_prototypes
7942 && old_decl
!= error_mark_node
7943 && !prototype_p (TREE_TYPE (decl1
))
7944 && C_DECL_ISNT_PROTOTYPE (old_decl
))
7945 warning_at (loc
, OPT_Wstrict_prototypes
,
7946 "function declaration isn%'t a prototype");
7947 /* Optionally warn of any global def with no previous prototype. */
7948 else if (warn_missing_prototypes
7949 && old_decl
!= error_mark_node
7950 && TREE_PUBLIC (decl1
)
7951 && !MAIN_NAME_P (DECL_NAME (decl1
))
7952 && C_DECL_ISNT_PROTOTYPE (old_decl
))
7953 warning_at (loc
, OPT_Wmissing_prototypes
,
7954 "no previous prototype for %qD", decl1
);
7955 /* Optionally warn of any def with no previous prototype
7956 if the function has already been used. */
7957 else if (warn_missing_prototypes
7959 && old_decl
!= error_mark_node
7960 && TREE_USED (old_decl
)
7961 && !prototype_p (TREE_TYPE (old_decl
)))
7962 warning_at (loc
, OPT_Wmissing_prototypes
,
7963 "%qD was used with no prototype before its definition", decl1
);
7964 /* Optionally warn of any global def with no previous declaration. */
7965 else if (warn_missing_declarations
7966 && TREE_PUBLIC (decl1
)
7968 && !MAIN_NAME_P (DECL_NAME (decl1
)))
7969 warning_at (loc
, OPT_Wmissing_declarations
,
7970 "no previous declaration for %qD",
7972 /* Optionally warn of any def with no previous declaration
7973 if the function has already been used. */
7974 else if (warn_missing_declarations
7976 && old_decl
!= error_mark_node
7977 && TREE_USED (old_decl
)
7978 && C_DECL_IMPLICIT (old_decl
))
7979 warning_at (loc
, OPT_Wmissing_declarations
,
7980 "%qD was used with no declaration before its definition", decl1
);
7982 /* This function exists in static storage.
7983 (This does not mean `static' in the C sense!) */
7984 TREE_STATIC (decl1
) = 1;
7986 /* This is the earliest point at which we might know the assembler
7987 name of the function. Thus, if it's set before this, die horribly. */
7988 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1
));
7990 /* If #pragma weak was used, mark the decl weak now. */
7991 if (current_scope
== file_scope
)
7992 maybe_apply_pragma_weak (decl1
);
7994 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
7995 if (warn_main
&& MAIN_NAME_P (DECL_NAME (decl1
)))
7997 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1
)))
7998 != integer_type_node
)
7999 pedwarn (loc
, OPT_Wmain
, "return type of %qD is not %<int%>", decl1
);
8001 check_main_parameter_types (decl1
);
8003 if (!TREE_PUBLIC (decl1
))
8004 pedwarn (loc
, OPT_Wmain
,
8005 "%qD is normally a non-static function", decl1
);
8008 /* Record the decl so that the function name is defined.
8009 If we already have a decl for this name, and it is a FUNCTION_DECL,
8010 use the old decl. */
8012 current_function_decl
= pushdecl (decl1
);
8015 declare_parm_level ();
8017 restype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
8018 resdecl
= build_decl (loc
, RESULT_DECL
, NULL_TREE
, restype
);
8019 DECL_ARTIFICIAL (resdecl
) = 1;
8020 DECL_IGNORED_P (resdecl
) = 1;
8021 DECL_RESULT (current_function_decl
) = resdecl
;
8023 start_fname_decls ();
8028 /* Subroutine of store_parm_decls which handles new-style function
8029 definitions (prototype format). The parms already have decls, so we
8030 need only record them as in effect and complain if any redundant
8031 old-style parm decls were written. */
8033 store_parm_decls_newstyle (tree fndecl
, const struct c_arg_info
*arg_info
)
8039 if (current_scope
->bindings
)
8041 error_at (DECL_SOURCE_LOCATION (fndecl
),
8042 "old-style parameter declarations in prototyped "
8043 "function definition");
8045 /* Get rid of the old-style declarations. */
8049 /* Don't issue this warning for nested functions, and don't issue this
8050 warning if we got here because ARG_INFO_TYPES was error_mark_node
8051 (this happens when a function definition has just an ellipsis in
8052 its parameter list). */
8053 else if (!in_system_header
&& !current_function_scope
8054 && arg_info
->types
!= error_mark_node
)
8055 warning_at (DECL_SOURCE_LOCATION (fndecl
), OPT_Wtraditional
,
8056 "traditional C rejects ISO C style function definitions");
8058 /* Now make all the parameter declarations visible in the function body.
8059 We can bypass most of the grunt work of pushdecl. */
8060 for (decl
= arg_info
->parms
; decl
; decl
= DECL_CHAIN (decl
))
8062 DECL_CONTEXT (decl
) = current_function_decl
;
8063 if (DECL_NAME (decl
))
8065 bind (DECL_NAME (decl
), decl
, current_scope
,
8066 /*invisible=*/false, /*nested=*/false,
8068 if (!TREE_USED (decl
))
8069 warn_if_shadowing (decl
);
8072 error_at (DECL_SOURCE_LOCATION (decl
), "parameter name omitted");
8075 /* Record the parameter list in the function declaration. */
8076 DECL_ARGUMENTS (fndecl
) = arg_info
->parms
;
8078 /* Now make all the ancillary declarations visible, likewise. */
8079 for (decl
= arg_info
->others
; decl
; decl
= DECL_CHAIN (decl
))
8081 DECL_CONTEXT (decl
) = current_function_decl
;
8082 if (DECL_NAME (decl
))
8083 bind (DECL_NAME (decl
), decl
, current_scope
,
8084 /*invisible=*/false,
8085 /*nested=*/(TREE_CODE (decl
) == FUNCTION_DECL
),
8089 /* And all the tag declarations. */
8090 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info
->tags
, ix
, tag
)
8092 bind (tag
->id
, tag
->type
, current_scope
,
8093 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
8096 /* Subroutine of store_parm_decls which handles old-style function
8097 definitions (separate parameter list and declarations). */
8100 store_parm_decls_oldstyle (tree fndecl
, const struct c_arg_info
*arg_info
)
8102 struct c_binding
*b
;
8103 tree parm
, decl
, last
;
8104 tree parmids
= arg_info
->parms
;
8105 struct pointer_set_t
*seen_args
= pointer_set_create ();
8107 if (!in_system_header
)
8108 warning_at (DECL_SOURCE_LOCATION (fndecl
),
8109 OPT_Wold_style_definition
, "old-style function definition");
8111 /* Match each formal parameter name with its declaration. Save each
8112 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8113 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
8115 if (TREE_VALUE (parm
) == 0)
8117 error_at (DECL_SOURCE_LOCATION (fndecl
),
8118 "parameter name missing from parameter list");
8119 TREE_PURPOSE (parm
) = 0;
8123 b
= I_SYMBOL_BINDING (TREE_VALUE (parm
));
8124 if (b
&& B_IN_CURRENT_SCOPE (b
))
8127 /* Skip erroneous parameters. */
8128 if (decl
== error_mark_node
)
8130 /* If we got something other than a PARM_DECL it is an error. */
8131 if (TREE_CODE (decl
) != PARM_DECL
)
8132 error_at (DECL_SOURCE_LOCATION (decl
),
8133 "%qD declared as a non-parameter", decl
);
8134 /* If the declaration is already marked, we have a duplicate
8135 name. Complain and ignore the duplicate. */
8136 else if (pointer_set_contains (seen_args
, decl
))
8138 error_at (DECL_SOURCE_LOCATION (decl
),
8139 "multiple parameters named %qD", decl
);
8140 TREE_PURPOSE (parm
) = 0;
8143 /* If the declaration says "void", complain and turn it into
8145 else if (VOID_TYPE_P (TREE_TYPE (decl
)))
8147 error_at (DECL_SOURCE_LOCATION (decl
),
8148 "parameter %qD declared with void type", decl
);
8149 TREE_TYPE (decl
) = integer_type_node
;
8150 DECL_ARG_TYPE (decl
) = integer_type_node
;
8151 layout_decl (decl
, 0);
8153 warn_if_shadowing (decl
);
8155 /* If no declaration found, default to int. */
8158 /* FIXME diagnostics: This should be the location of the argument,
8159 not the FNDECL. E.g., for an old-style declaration
8161 int f10(v) { blah; }
8163 We should use the location of the V, not the F10.
8164 Unfortunately, the V is an IDENTIFIER_NODE which has no
8165 location. In the future we need locations for c_arg_info
8168 See gcc.dg/Wshadow-3.c for an example of this problem. */
8169 decl
= build_decl (DECL_SOURCE_LOCATION (fndecl
),
8170 PARM_DECL
, TREE_VALUE (parm
), integer_type_node
);
8171 DECL_ARG_TYPE (decl
) = TREE_TYPE (decl
);
8173 warn_if_shadowing (decl
);
8176 pedwarn (DECL_SOURCE_LOCATION (decl
),
8177 0, "type of %qD defaults to %<int%>", decl
);
8179 warning_at (DECL_SOURCE_LOCATION (decl
),
8180 OPT_Wmissing_parameter_type
,
8181 "type of %qD defaults to %<int%>", decl
);
8184 TREE_PURPOSE (parm
) = decl
;
8185 pointer_set_insert (seen_args
, decl
);
8188 /* Now examine the parms chain for incomplete declarations
8189 and declarations with no corresponding names. */
8191 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
8194 if (TREE_CODE (parm
) != PARM_DECL
)
8197 if (TREE_TYPE (parm
) != error_mark_node
8198 && !COMPLETE_TYPE_P (TREE_TYPE (parm
)))
8200 error_at (DECL_SOURCE_LOCATION (parm
),
8201 "parameter %qD has incomplete type", parm
);
8202 TREE_TYPE (parm
) = error_mark_node
;
8205 if (!pointer_set_contains (seen_args
, parm
))
8207 error_at (DECL_SOURCE_LOCATION (parm
),
8208 "declaration for parameter %qD but no such parameter",
8211 /* Pretend the parameter was not missing.
8212 This gets us to a standard state and minimizes
8213 further error messages. */
8214 parmids
= chainon (parmids
, tree_cons (parm
, 0, 0));
8218 /* Chain the declarations together in the order of the list of
8219 names. Store that chain in the function decl, replacing the
8220 list of names. Update the current scope to match. */
8221 DECL_ARGUMENTS (fndecl
) = 0;
8223 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
8224 if (TREE_PURPOSE (parm
))
8226 if (parm
&& TREE_PURPOSE (parm
))
8228 last
= TREE_PURPOSE (parm
);
8229 DECL_ARGUMENTS (fndecl
) = last
;
8231 for (parm
= TREE_CHAIN (parm
); parm
; parm
= TREE_CHAIN (parm
))
8232 if (TREE_PURPOSE (parm
))
8234 DECL_CHAIN (last
) = TREE_PURPOSE (parm
);
8235 last
= TREE_PURPOSE (parm
);
8237 DECL_CHAIN (last
) = 0;
8240 pointer_set_destroy (seen_args
);
8242 /* If there was a previous prototype,
8243 set the DECL_ARG_TYPE of each argument according to
8244 the type previously specified, and report any mismatches. */
8246 if (current_function_prototype_arg_types
)
8249 for (parm
= DECL_ARGUMENTS (fndecl
),
8250 type
= current_function_prototype_arg_types
;
8251 parm
|| (type
&& TREE_VALUE (type
) != error_mark_node
8252 && (TYPE_MAIN_VARIANT (TREE_VALUE (type
)) != void_type_node
));
8253 parm
= DECL_CHAIN (parm
), type
= TREE_CHAIN (type
))
8255 if (parm
== 0 || type
== 0
8256 || TYPE_MAIN_VARIANT (TREE_VALUE (type
)) == void_type_node
)
8258 if (current_function_prototype_built_in
)
8259 warning_at (DECL_SOURCE_LOCATION (fndecl
),
8260 0, "number of arguments doesn%'t match "
8261 "built-in prototype");
8264 /* FIXME diagnostics: This should be the location of
8265 FNDECL, but there is bug when a prototype is
8266 declared inside function context, but defined
8267 outside of it (e.g., gcc.dg/pr15698-2.c). In
8268 which case FNDECL gets the location of the
8269 prototype, not the definition. */
8270 error_at (input_location
,
8271 "number of arguments doesn%'t match prototype");
8273 error_at (current_function_prototype_locus
,
8274 "prototype declaration");
8278 /* Type for passing arg must be consistent with that
8279 declared for the arg. ISO C says we take the unqualified
8280 type for parameters declared with qualified type. */
8281 if (TREE_TYPE (parm
) != error_mark_node
8282 && TREE_TYPE (type
) != error_mark_node
8283 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm
))
8284 != TYPE_ATOMIC (TREE_VALUE (type
)))
8285 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm
)),
8286 TYPE_MAIN_VARIANT (TREE_VALUE (type
)))))
8288 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm
))
8289 == TYPE_ATOMIC (TREE_VALUE (type
)))
8290 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm
))
8291 == TYPE_MAIN_VARIANT (TREE_VALUE (type
))))
8293 /* Adjust argument to match prototype. E.g. a previous
8294 `int foo(float);' prototype causes
8295 `int foo(x) float x; {...}' to be treated like
8296 `int foo(float x) {...}'. This is particularly
8297 useful for argument types like uid_t. */
8298 DECL_ARG_TYPE (parm
) = TREE_TYPE (parm
);
8300 if (targetm
.calls
.promote_prototypes (TREE_TYPE (current_function_decl
))
8301 && INTEGRAL_TYPE_P (TREE_TYPE (parm
))
8302 && TYPE_PRECISION (TREE_TYPE (parm
))
8303 < TYPE_PRECISION (integer_type_node
))
8304 DECL_ARG_TYPE (parm
)
8305 = c_type_promotes_to (TREE_TYPE (parm
));
8307 /* ??? Is it possible to get here with a
8308 built-in prototype or will it always have
8309 been diagnosed as conflicting with an
8310 old-style definition and discarded? */
8311 if (current_function_prototype_built_in
)
8312 warning_at (DECL_SOURCE_LOCATION (parm
),
8313 OPT_Wpedantic
, "promoted argument %qD "
8314 "doesn%'t match built-in prototype", parm
);
8317 pedwarn (DECL_SOURCE_LOCATION (parm
),
8318 OPT_Wpedantic
, "promoted argument %qD "
8319 "doesn%'t match prototype", parm
);
8320 pedwarn (current_function_prototype_locus
, OPT_Wpedantic
,
8321 "prototype declaration");
8326 if (current_function_prototype_built_in
)
8327 warning_at (DECL_SOURCE_LOCATION (parm
),
8328 0, "argument %qD doesn%'t match "
8329 "built-in prototype", parm
);
8332 error_at (DECL_SOURCE_LOCATION (parm
),
8333 "argument %qD doesn%'t match prototype", parm
);
8334 error_at (current_function_prototype_locus
,
8335 "prototype declaration");
8340 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = 0;
8343 /* Otherwise, create a prototype that would match. */
8347 tree actual
= 0, last
= 0, type
;
8349 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= DECL_CHAIN (parm
))
8351 type
= tree_cons (NULL_TREE
, DECL_ARG_TYPE (parm
), NULL_TREE
);
8353 TREE_CHAIN (last
) = type
;
8358 type
= tree_cons (NULL_TREE
, void_type_node
, NULL_TREE
);
8360 TREE_CHAIN (last
) = type
;
8364 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
8365 of the type of this function, but we need to avoid having this
8366 affect the types of other similarly-typed functions, so we must
8367 first force the generation of an identical (but separate) type
8368 node for the relevant function type. The new node we create
8369 will be a variant of the main variant of the original function
8372 TREE_TYPE (fndecl
) = build_variant_type_copy (TREE_TYPE (fndecl
));
8374 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = actual
;
8378 /* Store parameter declarations passed in ARG_INFO into the current
8379 function declaration. */
8382 store_parm_decls_from (struct c_arg_info
*arg_info
)
8384 current_function_arg_info
= arg_info
;
8385 store_parm_decls ();
8388 /* Store the parameter declarations into the current function declaration.
8389 This is called after parsing the parameter declarations, before
8390 digesting the body of the function.
8392 For an old-style definition, construct a prototype out of the old-style
8393 parameter declarations and inject it into the function's type. */
8396 store_parm_decls (void)
8398 tree fndecl
= current_function_decl
;
8401 /* The argument information block for FNDECL. */
8402 struct c_arg_info
*arg_info
= current_function_arg_info
;
8403 current_function_arg_info
= 0;
8405 /* True if this definition is written with a prototype. Note:
8406 despite C99 6.7.5.3p14, we can *not* treat an empty argument
8407 list in a function definition as equivalent to (void) -- an
8408 empty argument list specifies the function has no parameters,
8409 but only (void) sets up a prototype for future calls. */
8410 proto
= arg_info
->types
!= 0;
8413 store_parm_decls_newstyle (fndecl
, arg_info
);
8415 store_parm_decls_oldstyle (fndecl
, arg_info
);
8417 /* The next call to push_scope will be a function body. */
8419 next_is_function_body
= true;
8421 /* Write a record describing this function definition to the prototypes
8422 file (if requested). */
8424 gen_aux_info_record (fndecl
, 1, 0, proto
);
8426 /* Initialize the RTL code for the function. */
8427 allocate_struct_function (fndecl
, false);
8429 if (warn_unused_local_typedefs
)
8430 cfun
->language
= ggc_alloc_cleared_language_function ();
8432 /* Begin the statement tree for this function. */
8433 DECL_SAVED_TREE (fndecl
) = push_stmt_list ();
8435 /* ??? Insert the contents of the pending sizes list into the function
8436 to be evaluated. The only reason left to have this is
8437 void foo(int n, int array[n++])
8438 because we throw away the array type in favor of a pointer type, and
8439 thus won't naturally see the SAVE_EXPR containing the increment. All
8440 other pending sizes would be handled by gimplify_parameters. */
8441 if (arg_info
->pending_sizes
)
8442 add_stmt (arg_info
->pending_sizes
);
8445 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
8446 c_finish_omp_declare_simd for function prototypes. No diagnostics
8450 temp_store_parm_decls (tree fndecl
, tree parms
)
8453 for (tree p
= parms
; p
; p
= DECL_CHAIN (p
))
8455 DECL_CONTEXT (p
) = fndecl
;
8457 bind (DECL_NAME (p
), p
, current_scope
,
8458 /*invisible=*/false, /*nested=*/false,
8463 /* Undo what temp_store_parm_decls did. */
8466 temp_pop_parm_decls (void)
8468 /* Clear all bindings in this temporary scope, so that
8469 pop_scope doesn't create a BLOCK. */
8470 struct c_binding
*b
= current_scope
->bindings
;
8471 current_scope
->bindings
= NULL
;
8472 for (; b
; b
= free_binding_and_advance (b
))
8474 gcc_assert (TREE_CODE (b
->decl
) == PARM_DECL
);
8475 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
8476 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
8477 if (b
->shadowed
&& b
->shadowed
->u
.type
)
8478 TREE_TYPE (b
->shadowed
->decl
) = b
->shadowed
->u
.type
;
8484 /* Finish up a function declaration and compile that function
8485 all the way to assembler language output. Then free the storage
8486 for the function definition.
8488 This is called after parsing the body of the function definition. */
8491 finish_function (void)
8493 tree fndecl
= current_function_decl
;
8495 if (c_dialect_objc ())
8496 objc_finish_function ();
8498 if (TREE_CODE (fndecl
) == FUNCTION_DECL
8499 && targetm
.calls
.promote_prototypes (TREE_TYPE (fndecl
)))
8501 tree args
= DECL_ARGUMENTS (fndecl
);
8502 for (; args
; args
= DECL_CHAIN (args
))
8504 tree type
= TREE_TYPE (args
);
8505 if (INTEGRAL_TYPE_P (type
)
8506 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
8507 DECL_ARG_TYPE (args
) = c_type_promotes_to (type
);
8511 if (DECL_INITIAL (fndecl
) && DECL_INITIAL (fndecl
) != error_mark_node
)
8512 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
8514 /* Must mark the RESULT_DECL as being in this function. */
8516 if (DECL_RESULT (fndecl
) && DECL_RESULT (fndecl
) != error_mark_node
)
8517 DECL_CONTEXT (DECL_RESULT (fndecl
)) = fndecl
;
8519 if (MAIN_NAME_P (DECL_NAME (fndecl
)) && flag_hosted
8520 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl
)))
8521 == integer_type_node
&& flag_isoc99
)
8523 /* Hack. We don't want the middle-end to warn that this return
8524 is unreachable, so we mark its location as special. Using
8525 UNKNOWN_LOCATION has the problem that it gets clobbered in
8526 annotate_one_with_locus. A cleaner solution might be to
8527 ensure ! should_carry_locus_p (stmt), but that needs a flag.
8529 c_finish_return (BUILTINS_LOCATION
, integer_zero_node
, NULL_TREE
);
8532 /* Tie off the statement tree for this function. */
8533 DECL_SAVED_TREE (fndecl
) = pop_stmt_list (DECL_SAVED_TREE (fndecl
));
8535 /* If the function has _Cilk_spawn in front of a function call inside it
8536 i.e. it is a spawning function, then add the appropriate Cilk plus
8537 functions inside. */
8538 if (fn_contains_cilk_spawn_p (cfun
))
8539 cfun
->cilk_frame_decl
= insert_cilk_frame (fndecl
);
8541 finish_fname_decls ();
8543 /* Complain if there's just no return statement. */
8544 if (warn_return_type
8545 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl
))) != VOID_TYPE
8546 && !current_function_returns_value
&& !current_function_returns_null
8547 /* Don't complain if we are no-return. */
8548 && !current_function_returns_abnormally
8549 /* Don't complain if we are declared noreturn. */
8550 && !TREE_THIS_VOLATILE (fndecl
)
8551 /* Don't warn for main(). */
8552 && !MAIN_NAME_P (DECL_NAME (fndecl
))
8553 /* Or if they didn't actually specify a return type. */
8554 && !C_FUNCTION_IMPLICIT_INT (fndecl
)
8555 /* Normally, with -Wreturn-type, flow will complain, but we might
8556 optimize out static functions. */
8557 && !TREE_PUBLIC (fndecl
))
8559 warning (OPT_Wreturn_type
,
8560 "no return statement in function returning non-void");
8561 TREE_NO_WARNING (fndecl
) = 1;
8564 /* Complain about parameters that are only set, but never otherwise used. */
8565 if (warn_unused_but_set_parameter
)
8569 for (decl
= DECL_ARGUMENTS (fndecl
);
8571 decl
= DECL_CHAIN (decl
))
8572 if (TREE_USED (decl
)
8573 && TREE_CODE (decl
) == PARM_DECL
8574 && !DECL_READ_P (decl
)
8576 && !DECL_ARTIFICIAL (decl
)
8577 && !TREE_NO_WARNING (decl
))
8578 warning_at (DECL_SOURCE_LOCATION (decl
),
8579 OPT_Wunused_but_set_parameter
,
8580 "parameter %qD set but not used", decl
);
8583 /* Complain about locally defined typedefs that are not used in this
8585 maybe_warn_unused_local_typedefs ();
8587 /* Store the end of the function, so that we get good line number
8588 info for the epilogue. */
8589 cfun
->function_end_locus
= input_location
;
8591 /* Finalize the ELF visibility for the function. */
8592 c_determine_visibility (fndecl
);
8594 /* For GNU C extern inline functions disregard inline limits. */
8595 if (DECL_EXTERNAL (fndecl
)
8596 && DECL_DECLARED_INLINE_P (fndecl
))
8597 DECL_DISREGARD_INLINE_LIMITS (fndecl
) = 1;
8599 /* Genericize before inlining. Delay genericizing nested functions
8600 until their parent function is genericized. Since finalizing
8601 requires GENERIC, delay that as well. */
8603 if (DECL_INITIAL (fndecl
) && DECL_INITIAL (fndecl
) != error_mark_node
8604 && !undef_nested_function
)
8606 if (!decl_function_context (fndecl
))
8608 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE
, fndecl
);
8609 c_genericize (fndecl
);
8611 /* ??? Objc emits functions after finalizing the compilation unit.
8612 This should be cleaned up later and this conditional removed. */
8613 if (cgraph_global_info_ready
)
8615 cgraph_add_new_function (fndecl
, false);
8618 cgraph_finalize_function (fndecl
, false);
8622 /* Register this function with cgraph just far enough to get it
8623 added to our parent's nested function list. Handy, since the
8624 C front end doesn't have such a list. */
8625 (void) cgraph_get_create_node (fndecl
);
8629 if (!decl_function_context (fndecl
))
8630 undef_nested_function
= false;
8632 if (cfun
->language
!= NULL
)
8634 ggc_free (cfun
->language
);
8635 cfun
->language
= NULL
;
8638 /* We're leaving the context of this function, so zap cfun.
8639 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
8640 tree_rest_of_compilation. */
8642 current_function_decl
= NULL
;
8645 /* Check the declarations given in a for-loop for satisfying the C99
8646 constraints. If exactly one such decl is found, return it. LOC is
8647 the location of the opening parenthesis of the for loop. The last
8648 parameter allows you to control the "for loop initial declarations
8649 are only allowed in C99 mode". Normally, you should pass
8650 flag_isoc99 as that parameter. But in some cases (Objective-C
8651 foreach loop, for example) we want to run the checks in this
8652 function even if not in C99 mode, so we allow the caller to turn
8653 off the error about not being in C99 mode.
8657 check_for_loop_decls (location_t loc
, bool turn_off_iso_c99_error
)
8659 struct c_binding
*b
;
8660 tree one_decl
= NULL_TREE
;
8663 if (!turn_off_iso_c99_error
)
8665 static bool hint
= true;
8666 /* If we get here, declarations have been used in a for loop without
8667 the C99 for loop scope. This doesn't make much sense, so don't
8669 error_at (loc
, "%<for%> loop initial declarations "
8670 "are only allowed in C99 or C11 mode");
8674 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
8675 "to compile your code");
8680 /* C99 subclause 6.8.5 paragraph 3:
8682 [#3] The declaration part of a for statement shall only
8683 declare identifiers for objects having storage class auto or
8686 It isn't clear whether, in this sentence, "identifiers" binds to
8687 "shall only declare" or to "objects" - that is, whether all identifiers
8688 declared must be identifiers for objects, or whether the restriction
8689 only applies to those that are. (A question on this in comp.std.c
8690 in November 2000 received no answer.) We implement the strictest
8691 interpretation, to avoid creating an extension which later causes
8694 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
8697 tree decl
= b
->decl
;
8702 switch (TREE_CODE (decl
))
8706 location_t decl_loc
= DECL_SOURCE_LOCATION (decl
);
8707 if (TREE_STATIC (decl
))
8709 "declaration of static variable %qD in %<for%> loop "
8710 "initial declaration", decl
);
8711 else if (DECL_EXTERNAL (decl
))
8713 "declaration of %<extern%> variable %qD in %<for%> loop "
8714 "initial declaration", decl
);
8720 "%<struct %E%> declared in %<for%> loop initial "
8725 "%<union %E%> declared in %<for%> loop initial declaration",
8729 error_at (loc
, "%<enum %E%> declared in %<for%> loop "
8730 "initial declaration", id
);
8733 error_at (loc
, "declaration of non-variable "
8734 "%qD in %<for%> loop initial declaration", decl
);
8741 return n_decls
== 1 ? one_decl
: NULL_TREE
;
8744 /* Save and reinitialize the variables
8745 used during compilation of a C function. */
8748 c_push_function_context (void)
8750 struct language_function
*p
= cfun
->language
;
8751 /* cfun->language might have been already allocated by the use of
8752 -Wunused-local-typedefs. In that case, just re-use it. */
8754 cfun
->language
= p
= ggc_alloc_cleared_language_function ();
8756 p
->base
.x_stmt_tree
= c_stmt_tree
;
8757 c_stmt_tree
.x_cur_stmt_list
= vec_safe_copy (c_stmt_tree
.x_cur_stmt_list
);
8758 p
->x_break_label
= c_break_label
;
8759 p
->x_cont_label
= c_cont_label
;
8760 p
->x_switch_stack
= c_switch_stack
;
8761 p
->arg_info
= current_function_arg_info
;
8762 p
->returns_value
= current_function_returns_value
;
8763 p
->returns_null
= current_function_returns_null
;
8764 p
->returns_abnormally
= current_function_returns_abnormally
;
8765 p
->warn_about_return_type
= warn_about_return_type
;
8767 push_function_context ();
8770 /* Restore the variables used during compilation of a C function. */
8773 c_pop_function_context (void)
8775 struct language_function
*p
;
8777 pop_function_context ();
8780 /* When -Wunused-local-typedefs is in effect, cfun->languages is
8781 used to store data throughout the life time of the current cfun,
8782 So don't deallocate it. */
8783 if (!warn_unused_local_typedefs
)
8784 cfun
->language
= NULL
;
8786 if (DECL_STRUCT_FUNCTION (current_function_decl
) == 0
8787 && DECL_SAVED_TREE (current_function_decl
) == NULL_TREE
)
8789 /* Stop pointing to the local nodes about to be freed. */
8790 /* But DECL_INITIAL must remain nonzero so we know this
8791 was an actual function definition. */
8792 DECL_INITIAL (current_function_decl
) = error_mark_node
;
8793 DECL_ARGUMENTS (current_function_decl
) = 0;
8796 c_stmt_tree
= p
->base
.x_stmt_tree
;
8797 p
->base
.x_stmt_tree
.x_cur_stmt_list
= NULL
;
8798 c_break_label
= p
->x_break_label
;
8799 c_cont_label
= p
->x_cont_label
;
8800 c_switch_stack
= p
->x_switch_stack
;
8801 current_function_arg_info
= p
->arg_info
;
8802 current_function_returns_value
= p
->returns_value
;
8803 current_function_returns_null
= p
->returns_null
;
8804 current_function_returns_abnormally
= p
->returns_abnormally
;
8805 warn_about_return_type
= p
->warn_about_return_type
;
8808 /* The functions below are required for functionality of doing
8809 function at once processing in the C front end. Currently these
8810 functions are not called from anywhere in the C front end, but as
8811 these changes continue, that will change. */
8813 /* Returns the stmt_tree (if any) to which statements are currently
8814 being added. If there is no active statement-tree, NULL is
8818 current_stmt_tree (void)
8820 return &c_stmt_tree
;
8823 /* Return the global value of T as a symbol. */
8826 identifier_global_value (tree t
)
8828 struct c_binding
*b
;
8830 for (b
= I_SYMBOL_BINDING (t
); b
; b
= b
->shadowed
)
8831 if (B_IN_FILE_SCOPE (b
) || B_IN_EXTERNAL_SCOPE (b
))
8837 /* In C, the only C-linkage public declaration is at file scope. */
8840 c_linkage_bindings (tree name
)
8842 return identifier_global_value (name
);
8845 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
8846 otherwise the name is found in ridpointers from RID_INDEX. */
8849 record_builtin_type (enum rid rid_index
, const char *name
, tree type
)
8853 id
= ridpointers
[(int) rid_index
];
8855 id
= get_identifier (name
);
8856 decl
= build_decl (UNKNOWN_LOCATION
, TYPE_DECL
, id
, type
);
8858 if (debug_hooks
->type_decl
)
8859 debug_hooks
->type_decl (decl
, false);
8862 /* Build the void_list_node (void_type_node having been created). */
8864 build_void_list_node (void)
8866 tree t
= build_tree_list (NULL_TREE
, void_type_node
);
8870 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
8873 build_c_parm (struct c_declspecs
*specs
, tree attrs
,
8874 struct c_declarator
*declarator
)
8876 struct c_parm
*ret
= XOBNEW (&parser_obstack
, struct c_parm
);
8879 ret
->declarator
= declarator
;
8883 /* Return a declarator with nested attributes. TARGET is the inner
8884 declarator to which these attributes apply. ATTRS are the
8887 struct c_declarator
*
8888 build_attrs_declarator (tree attrs
, struct c_declarator
*target
)
8890 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
8891 ret
->kind
= cdk_attrs
;
8892 ret
->declarator
= target
;
8893 ret
->u
.attrs
= attrs
;
8897 /* Return a declarator for a function with arguments specified by ARGS
8898 and return type specified by TARGET. */
8900 struct c_declarator
*
8901 build_function_declarator (struct c_arg_info
*args
,
8902 struct c_declarator
*target
)
8904 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
8905 ret
->kind
= cdk_function
;
8906 ret
->declarator
= target
;
8907 ret
->u
.arg_info
= args
;
8911 /* Return a declarator for the identifier IDENT (which may be
8912 NULL_TREE for an abstract declarator). */
8914 struct c_declarator
*
8915 build_id_declarator (tree ident
)
8917 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
8919 ret
->declarator
= 0;
8921 /* Default value - may get reset to a more precise location. */
8922 ret
->id_loc
= input_location
;
8926 /* Return something to represent absolute declarators containing a *.
8927 TARGET is the absolute declarator that the * contains.
8928 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
8929 to apply to the pointer type. */
8931 struct c_declarator
*
8932 make_pointer_declarator (struct c_declspecs
*type_quals_attrs
,
8933 struct c_declarator
*target
)
8937 struct c_declarator
*itarget
= target
;
8938 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
8939 if (type_quals_attrs
)
8941 attrs
= type_quals_attrs
->attrs
;
8942 quals
= quals_from_declspecs (type_quals_attrs
);
8943 if (attrs
!= NULL_TREE
)
8944 itarget
= build_attrs_declarator (attrs
, target
);
8946 ret
->kind
= cdk_pointer
;
8947 ret
->declarator
= itarget
;
8948 ret
->u
.pointer_quals
= quals
;
8952 /* Return a pointer to a structure for an empty list of declaration
8955 struct c_declspecs
*
8956 build_null_declspecs (void)
8958 struct c_declspecs
*ret
= XOBNEW (&parser_obstack
, struct c_declspecs
);
8959 memset (&ret
->locations
, 0, cdw_number_of_elements
);
8964 ret
->align_log
= -1;
8965 ret
->typespec_word
= cts_none
;
8966 ret
->storage_class
= csc_none
;
8967 ret
->expr_const_operands
= true;
8968 ret
->declspecs_seen_p
= false;
8969 ret
->typespec_kind
= ctsk_none
;
8970 ret
->non_sc_seen_p
= false;
8971 ret
->typedef_p
= false;
8972 ret
->explicit_signed_p
= false;
8973 ret
->deprecated_p
= false;
8974 ret
->default_int_p
= false;
8975 ret
->long_p
= false;
8976 ret
->long_long_p
= false;
8977 ret
->short_p
= false;
8978 ret
->signed_p
= false;
8979 ret
->unsigned_p
= false;
8980 ret
->complex_p
= false;
8981 ret
->inline_p
= false;
8982 ret
->noreturn_p
= false;
8983 ret
->thread_p
= false;
8984 ret
->const_p
= false;
8985 ret
->volatile_p
= false;
8986 ret
->atomic_p
= false;
8987 ret
->restrict_p
= false;
8988 ret
->saturating_p
= false;
8989 ret
->alignas_p
= false;
8990 ret
->address_space
= ADDR_SPACE_GENERIC
;
8994 /* Add the address space ADDRSPACE to the declaration specifiers
8995 SPECS, returning SPECS. */
8997 struct c_declspecs
*
8998 declspecs_add_addrspace (source_location location
,
8999 struct c_declspecs
*specs
, addr_space_t as
)
9001 specs
->non_sc_seen_p
= true;
9002 specs
->declspecs_seen_p
= true;
9004 if (!ADDR_SPACE_GENERIC_P (specs
->address_space
)
9005 && specs
->address_space
!= as
)
9006 error ("incompatible address space qualifiers %qs and %qs",
9007 c_addr_space_name (as
),
9008 c_addr_space_name (specs
->address_space
));
9011 specs
->address_space
= as
;
9012 specs
->locations
[cdw_address_space
] = location
;
9017 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9020 struct c_declspecs
*
9021 declspecs_add_qual (source_location loc
,
9022 struct c_declspecs
*specs
, tree qual
)
9026 specs
->non_sc_seen_p
= true;
9027 specs
->declspecs_seen_p
= true;
9028 gcc_assert (TREE_CODE (qual
) == IDENTIFIER_NODE
9029 && C_IS_RESERVED_WORD (qual
));
9030 i
= C_RID_CODE (qual
);
9034 dupe
= specs
->const_p
;
9035 specs
->const_p
= true;
9036 specs
->locations
[cdw_const
] = loc
;
9039 dupe
= specs
->volatile_p
;
9040 specs
->volatile_p
= true;
9041 specs
->locations
[cdw_volatile
] = loc
;
9044 dupe
= specs
->restrict_p
;
9045 specs
->restrict_p
= true;
9046 specs
->locations
[cdw_restrict
] = loc
;
9049 dupe
= specs
->atomic_p
;
9050 specs
->atomic_p
= true;
9055 if (dupe
&& !flag_isoc99
)
9056 pedwarn (loc
, OPT_Wpedantic
, "duplicate %qE", qual
);
9060 /* Add the type specifier TYPE to the declaration specifiers SPECS,
9063 struct c_declspecs
*
9064 declspecs_add_type (location_t loc
, struct c_declspecs
*specs
,
9065 struct c_typespec spec
)
9067 tree type
= spec
.spec
;
9068 specs
->non_sc_seen_p
= true;
9069 specs
->declspecs_seen_p
= true;
9070 specs
->typespec_kind
= spec
.kind
;
9071 if (TREE_DEPRECATED (type
))
9072 specs
->deprecated_p
= true;
9074 /* Handle type specifier keywords. */
9075 if (TREE_CODE (type
) == IDENTIFIER_NODE
9076 && C_IS_RESERVED_WORD (type
)
9077 && C_RID_CODE (type
) != RID_CXX_COMPAT_WARN
)
9079 enum rid i
= C_RID_CODE (type
);
9082 error_at (loc
, "two or more data types in declaration specifiers");
9085 if ((int) i
<= (int) RID_LAST_MODIFIER
)
9087 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
9092 if (specs
->long_long_p
)
9094 error_at (loc
, "%<long long long%> is too long for GCC");
9099 if (specs
->typespec_word
== cts_double
)
9102 ("both %<long long%> and %<double%> in "
9103 "declaration specifiers"));
9106 pedwarn_c90 (loc
, OPT_Wlong_long
,
9107 "ISO C90 does not support %<long long%>");
9108 specs
->long_long_p
= 1;
9109 specs
->locations
[cdw_long_long
] = loc
;
9114 ("both %<long%> and %<short%> in "
9115 "declaration specifiers"));
9116 else if (specs
->typespec_word
== cts_void
)
9118 ("both %<long%> and %<void%> in "
9119 "declaration specifiers"));
9120 else if (specs
->typespec_word
== cts_int128
)
9122 ("both %<long%> and %<__int128%> in "
9123 "declaration specifiers"));
9124 else if (specs
->typespec_word
== cts_bool
)
9126 ("both %<long%> and %<_Bool%> in "
9127 "declaration specifiers"));
9128 else if (specs
->typespec_word
== cts_char
)
9130 ("both %<long%> and %<char%> in "
9131 "declaration specifiers"));
9132 else if (specs
->typespec_word
== cts_float
)
9134 ("both %<long%> and %<float%> in "
9135 "declaration specifiers"));
9136 else if (specs
->typespec_word
== cts_dfloat32
)
9138 ("both %<long%> and %<_Decimal32%> in "
9139 "declaration specifiers"));
9140 else if (specs
->typespec_word
== cts_dfloat64
)
9142 ("both %<long%> and %<_Decimal64%> in "
9143 "declaration specifiers"));
9144 else if (specs
->typespec_word
== cts_dfloat128
)
9146 ("both %<long%> and %<_Decimal128%> in "
9147 "declaration specifiers"));
9150 specs
->long_p
= true;
9151 specs
->locations
[cdw_long
] = loc
;
9155 dupe
= specs
->short_p
;
9158 ("both %<long%> and %<short%> in "
9159 "declaration specifiers"));
9160 else if (specs
->typespec_word
== cts_void
)
9162 ("both %<short%> and %<void%> in "
9163 "declaration specifiers"));
9164 else if (specs
->typespec_word
== cts_int128
)
9166 ("both %<short%> and %<__int128%> in "
9167 "declaration specifiers"));
9168 else if (specs
->typespec_word
== cts_bool
)
9170 ("both %<short%> and %<_Bool%> in "
9171 "declaration specifiers"));
9172 else if (specs
->typespec_word
== cts_char
)
9174 ("both %<short%> and %<char%> in "
9175 "declaration specifiers"));
9176 else if (specs
->typespec_word
== cts_float
)
9178 ("both %<short%> and %<float%> in "
9179 "declaration specifiers"));
9180 else if (specs
->typespec_word
== cts_double
)
9182 ("both %<short%> and %<double%> in "
9183 "declaration specifiers"));
9184 else if (specs
->typespec_word
== cts_dfloat32
)
9186 ("both %<short%> and %<_Decimal32%> in "
9187 "declaration specifiers"));
9188 else if (specs
->typespec_word
== cts_dfloat64
)
9190 ("both %<short%> and %<_Decimal64%> in "
9191 "declaration specifiers"));
9192 else if (specs
->typespec_word
== cts_dfloat128
)
9194 ("both %<short%> and %<_Decimal128%> in "
9195 "declaration specifiers"));
9198 specs
->short_p
= true;
9199 specs
->locations
[cdw_short
] = loc
;
9203 dupe
= specs
->signed_p
;
9204 if (specs
->unsigned_p
)
9206 ("both %<signed%> and %<unsigned%> in "
9207 "declaration specifiers"));
9208 else if (specs
->typespec_word
== cts_void
)
9210 ("both %<signed%> and %<void%> in "
9211 "declaration specifiers"));
9212 else if (specs
->typespec_word
== cts_bool
)
9214 ("both %<signed%> and %<_Bool%> in "
9215 "declaration specifiers"));
9216 else if (specs
->typespec_word
== cts_float
)
9218 ("both %<signed%> and %<float%> in "
9219 "declaration specifiers"));
9220 else if (specs
->typespec_word
== cts_double
)
9222 ("both %<signed%> and %<double%> in "
9223 "declaration specifiers"));
9224 else if (specs
->typespec_word
== cts_dfloat32
)
9226 ("both %<signed%> and %<_Decimal32%> in "
9227 "declaration specifiers"));
9228 else if (specs
->typespec_word
== cts_dfloat64
)
9230 ("both %<signed%> and %<_Decimal64%> in "
9231 "declaration specifiers"));
9232 else if (specs
->typespec_word
== cts_dfloat128
)
9234 ("both %<signed%> and %<_Decimal128%> in "
9235 "declaration specifiers"));
9238 specs
->signed_p
= true;
9239 specs
->locations
[cdw_signed
] = loc
;
9243 dupe
= specs
->unsigned_p
;
9244 if (specs
->signed_p
)
9246 ("both %<signed%> and %<unsigned%> in "
9247 "declaration specifiers"));
9248 else if (specs
->typespec_word
== cts_void
)
9250 ("both %<unsigned%> and %<void%> in "
9251 "declaration specifiers"));
9252 else if (specs
->typespec_word
== cts_bool
)
9254 ("both %<unsigned%> and %<_Bool%> in "
9255 "declaration specifiers"));
9256 else if (specs
->typespec_word
== cts_float
)
9258 ("both %<unsigned%> and %<float%> in "
9259 "declaration specifiers"));
9260 else if (specs
->typespec_word
== cts_double
)
9262 ("both %<unsigned%> and %<double%> in "
9263 "declaration specifiers"));
9264 else if (specs
->typespec_word
== cts_dfloat32
)
9266 ("both %<unsigned%> and %<_Decimal32%> in "
9267 "declaration specifiers"));
9268 else if (specs
->typespec_word
== cts_dfloat64
)
9270 ("both %<unsigned%> and %<_Decimal64%> in "
9271 "declaration specifiers"));
9272 else if (specs
->typespec_word
== cts_dfloat128
)
9274 ("both %<unsigned%> and %<_Decimal128%> in "
9275 "declaration specifiers"));
9278 specs
->unsigned_p
= true;
9279 specs
->locations
[cdw_unsigned
] = loc
;
9283 dupe
= specs
->complex_p
;
9284 if (!flag_isoc99
&& !in_system_header_at (loc
))
9285 pedwarn (loc
, OPT_Wpedantic
,
9286 "ISO C90 does not support complex types");
9287 if (specs
->typespec_word
== cts_void
)
9289 ("both %<complex%> and %<void%> in "
9290 "declaration specifiers"));
9291 else if (specs
->typespec_word
== cts_bool
)
9293 ("both %<complex%> and %<_Bool%> in "
9294 "declaration specifiers"));
9295 else if (specs
->typespec_word
== cts_dfloat32
)
9297 ("both %<complex%> and %<_Decimal32%> in "
9298 "declaration specifiers"));
9299 else if (specs
->typespec_word
== cts_dfloat64
)
9301 ("both %<complex%> and %<_Decimal64%> in "
9302 "declaration specifiers"));
9303 else if (specs
->typespec_word
== cts_dfloat128
)
9305 ("both %<complex%> and %<_Decimal128%> in "
9306 "declaration specifiers"));
9307 else if (specs
->typespec_word
== cts_fract
)
9309 ("both %<complex%> and %<_Fract%> in "
9310 "declaration specifiers"));
9311 else if (specs
->typespec_word
== cts_accum
)
9313 ("both %<complex%> and %<_Accum%> in "
9314 "declaration specifiers"));
9315 else if (specs
->saturating_p
)
9317 ("both %<complex%> and %<_Sat%> in "
9318 "declaration specifiers"));
9321 specs
->complex_p
= true;
9322 specs
->locations
[cdw_complex
] = loc
;
9326 dupe
= specs
->saturating_p
;
9327 pedwarn (loc
, OPT_Wpedantic
,
9328 "ISO C does not support saturating types");
9329 if (specs
->typespec_word
== cts_int128
)
9332 ("both %<_Sat%> and %<__int128%> in "
9333 "declaration specifiers"));
9335 else if (specs
->typespec_word
== cts_void
)
9337 ("both %<_Sat%> and %<void%> in "
9338 "declaration specifiers"));
9339 else if (specs
->typespec_word
== cts_bool
)
9341 ("both %<_Sat%> and %<_Bool%> in "
9342 "declaration specifiers"));
9343 else if (specs
->typespec_word
== cts_char
)
9345 ("both %<_Sat%> and %<char%> in "
9346 "declaration specifiers"));
9347 else if (specs
->typespec_word
== cts_int
)
9349 ("both %<_Sat%> and %<int%> in "
9350 "declaration specifiers"));
9351 else if (specs
->typespec_word
== cts_float
)
9353 ("both %<_Sat%> and %<float%> in "
9354 "declaration specifiers"));
9355 else if (specs
->typespec_word
== cts_double
)
9357 ("both %<_Sat%> and %<double%> in "
9358 "declaration specifiers"));
9359 else if (specs
->typespec_word
== cts_dfloat32
)
9361 ("both %<_Sat%> and %<_Decimal32%> in "
9362 "declaration specifiers"));
9363 else if (specs
->typespec_word
== cts_dfloat64
)
9365 ("both %<_Sat%> and %<_Decimal64%> in "
9366 "declaration specifiers"));
9367 else if (specs
->typespec_word
== cts_dfloat128
)
9369 ("both %<_Sat%> and %<_Decimal128%> in "
9370 "declaration specifiers"));
9371 else if (specs
->complex_p
)
9373 ("both %<_Sat%> and %<complex%> in "
9374 "declaration specifiers"));
9377 specs
->saturating_p
= true;
9378 specs
->locations
[cdw_saturating
] = loc
;
9386 error_at (loc
, "duplicate %qE", type
);
9392 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
9393 "__int128", "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
9394 if (specs
->typespec_word
!= cts_none
)
9397 "two or more data types in declaration specifiers");
9403 if (int128_integer_type_node
== NULL_TREE
)
9405 error_at (loc
, "%<__int128%> is not supported for this target");
9408 if (!in_system_header
)
9409 pedwarn (loc
, OPT_Wpedantic
,
9410 "ISO C does not support %<__int128%> type");
9414 ("both %<__int128%> and %<long%> in "
9415 "declaration specifiers"));
9416 else if (specs
->saturating_p
)
9418 ("both %<_Sat%> and %<__int128%> in "
9419 "declaration specifiers"));
9420 else if (specs
->short_p
)
9422 ("both %<__int128%> and %<short%> in "
9423 "declaration specifiers"));
9426 specs
->typespec_word
= cts_int128
;
9427 specs
->locations
[cdw_typespec
] = loc
;
9433 ("both %<long%> and %<void%> in "
9434 "declaration specifiers"));
9435 else if (specs
->short_p
)
9437 ("both %<short%> and %<void%> in "
9438 "declaration specifiers"));
9439 else if (specs
->signed_p
)
9441 ("both %<signed%> and %<void%> in "
9442 "declaration specifiers"));
9443 else if (specs
->unsigned_p
)
9445 ("both %<unsigned%> and %<void%> in "
9446 "declaration specifiers"));
9447 else if (specs
->complex_p
)
9449 ("both %<complex%> and %<void%> in "
9450 "declaration specifiers"));
9451 else if (specs
->saturating_p
)
9453 ("both %<_Sat%> and %<void%> in "
9454 "declaration specifiers"));
9457 specs
->typespec_word
= cts_void
;
9458 specs
->locations
[cdw_typespec
] = loc
;
9464 ("both %<long%> and %<_Bool%> in "
9465 "declaration specifiers"));
9466 else if (specs
->short_p
)
9468 ("both %<short%> and %<_Bool%> in "
9469 "declaration specifiers"));
9470 else if (specs
->signed_p
)
9472 ("both %<signed%> and %<_Bool%> in "
9473 "declaration specifiers"));
9474 else if (specs
->unsigned_p
)
9476 ("both %<unsigned%> and %<_Bool%> in "
9477 "declaration specifiers"));
9478 else if (specs
->complex_p
)
9480 ("both %<complex%> and %<_Bool%> in "
9481 "declaration specifiers"));
9482 else if (specs
->saturating_p
)
9484 ("both %<_Sat%> and %<_Bool%> in "
9485 "declaration specifiers"));
9488 specs
->typespec_word
= cts_bool
;
9489 specs
->locations
[cdw_typespec
] = loc
;
9495 ("both %<long%> and %<char%> in "
9496 "declaration specifiers"));
9497 else if (specs
->short_p
)
9499 ("both %<short%> and %<char%> in "
9500 "declaration specifiers"));
9501 else if (specs
->saturating_p
)
9503 ("both %<_Sat%> and %<char%> in "
9504 "declaration specifiers"));
9507 specs
->typespec_word
= cts_char
;
9508 specs
->locations
[cdw_typespec
] = loc
;
9512 if (specs
->saturating_p
)
9514 ("both %<_Sat%> and %<int%> in "
9515 "declaration specifiers"));
9518 specs
->typespec_word
= cts_int
;
9519 specs
->locations
[cdw_typespec
] = loc
;
9525 ("both %<long%> and %<float%> in "
9526 "declaration specifiers"));
9527 else if (specs
->short_p
)
9529 ("both %<short%> and %<float%> in "
9530 "declaration specifiers"));
9531 else if (specs
->signed_p
)
9533 ("both %<signed%> and %<float%> in "
9534 "declaration specifiers"));
9535 else if (specs
->unsigned_p
)
9537 ("both %<unsigned%> and %<float%> in "
9538 "declaration specifiers"));
9539 else if (specs
->saturating_p
)
9541 ("both %<_Sat%> and %<float%> in "
9542 "declaration specifiers"));
9545 specs
->typespec_word
= cts_float
;
9546 specs
->locations
[cdw_typespec
] = loc
;
9550 if (specs
->long_long_p
)
9552 ("both %<long long%> and %<double%> in "
9553 "declaration specifiers"));
9554 else if (specs
->short_p
)
9556 ("both %<short%> and %<double%> in "
9557 "declaration specifiers"));
9558 else if (specs
->signed_p
)
9560 ("both %<signed%> and %<double%> in "
9561 "declaration specifiers"));
9562 else if (specs
->unsigned_p
)
9564 ("both %<unsigned%> and %<double%> in "
9565 "declaration specifiers"));
9566 else if (specs
->saturating_p
)
9568 ("both %<_Sat%> and %<double%> in "
9569 "declaration specifiers"));
9572 specs
->typespec_word
= cts_double
;
9573 specs
->locations
[cdw_typespec
] = loc
;
9581 if (i
== RID_DFLOAT32
)
9583 else if (i
== RID_DFLOAT64
)
9586 str
= "_Decimal128";
9587 if (specs
->long_long_p
)
9589 ("both %<long long%> and %<%s%> in "
9590 "declaration specifiers"),
9594 ("both %<long%> and %<%s%> in "
9595 "declaration specifiers"),
9597 else if (specs
->short_p
)
9599 ("both %<short%> and %<%s%> in "
9600 "declaration specifiers"),
9602 else if (specs
->signed_p
)
9604 ("both %<signed%> and %<%s%> in "
9605 "declaration specifiers"),
9607 else if (specs
->unsigned_p
)
9609 ("both %<unsigned%> and %<%s%> in "
9610 "declaration specifiers"),
9612 else if (specs
->complex_p
)
9614 ("both %<complex%> and %<%s%> in "
9615 "declaration specifiers"),
9617 else if (specs
->saturating_p
)
9619 ("both %<_Sat%> and %<%s%> in "
9620 "declaration specifiers"),
9622 else if (i
== RID_DFLOAT32
)
9623 specs
->typespec_word
= cts_dfloat32
;
9624 else if (i
== RID_DFLOAT64
)
9625 specs
->typespec_word
= cts_dfloat64
;
9627 specs
->typespec_word
= cts_dfloat128
;
9628 specs
->locations
[cdw_typespec
] = loc
;
9630 if (!targetm
.decimal_float_supported_p ())
9632 ("decimal floating point not supported "
9633 "for this target"));
9634 pedwarn (loc
, OPT_Wpedantic
,
9635 "ISO C does not support decimal floating point");
9645 if (specs
->complex_p
)
9647 ("both %<complex%> and %<%s%> in "
9648 "declaration specifiers"),
9650 else if (i
== RID_FRACT
)
9651 specs
->typespec_word
= cts_fract
;
9653 specs
->typespec_word
= cts_accum
;
9654 specs
->locations
[cdw_typespec
] = loc
;
9656 if (!targetm
.fixed_point_supported_p ())
9658 "fixed-point types not supported for this target");
9659 pedwarn (loc
, OPT_Wpedantic
,
9660 "ISO C does not support fixed-point types");
9663 /* ObjC reserved word "id", handled below. */
9669 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
9670 form of ObjC type, cases such as "int" and "long" being handled
9671 above), a TYPE (struct, union, enum and typeof specifiers) or an
9672 ERROR_MARK. In none of these cases may there have previously
9673 been any type specifiers. */
9674 if (specs
->type
|| specs
->typespec_word
!= cts_none
9675 || specs
->long_p
|| specs
->short_p
|| specs
->signed_p
9676 || specs
->unsigned_p
|| specs
->complex_p
)
9677 error_at (loc
, "two or more data types in declaration specifiers");
9678 else if (TREE_CODE (type
) == TYPE_DECL
)
9680 if (TREE_TYPE (type
) == error_mark_node
)
9681 ; /* Allow the type to default to int to avoid cascading errors. */
9684 specs
->type
= TREE_TYPE (type
);
9685 specs
->decl_attr
= DECL_ATTRIBUTES (type
);
9686 specs
->typedef_p
= true;
9687 specs
->explicit_signed_p
= C_TYPEDEF_EXPLICITLY_SIGNED (type
);
9688 specs
->locations
[cdw_typedef
] = loc
;
9690 /* If this typedef name is defined in a struct, then a C++
9691 lookup would return a different value. */
9693 && I_SYMBOL_BINDING (DECL_NAME (type
))->in_struct
)
9694 warning_at (loc
, OPT_Wc___compat
,
9695 "C++ lookup of %qD would return a field, not a type",
9698 /* If we are parsing a struct, record that a struct field
9700 if (warn_cxx_compat
&& struct_parse_info
!= NULL
)
9701 struct_parse_info
->typedefs_seen
.safe_push (type
);
9704 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
9706 tree t
= lookup_name (type
);
9707 if (!t
|| TREE_CODE (t
) != TYPE_DECL
)
9708 error_at (loc
, "%qE fails to be a typedef or built in type", type
);
9709 else if (TREE_TYPE (t
) == error_mark_node
)
9713 specs
->type
= TREE_TYPE (t
);
9714 specs
->locations
[cdw_typespec
] = loc
;
9719 if (TREE_CODE (type
) != ERROR_MARK
&& spec
.kind
== ctsk_typeof
)
9721 specs
->typedef_p
= true;
9722 specs
->locations
[cdw_typedef
] = loc
;
9726 specs
->expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (spec
.expr
),
9727 specs
->expr
, spec
.expr
);
9729 specs
->expr
= spec
.expr
;
9730 specs
->expr_const_operands
&= spec
.expr_const_operands
;
9739 /* Add the storage class specifier or function specifier SCSPEC to the
9740 declaration specifiers SPECS, returning SPECS. */
9742 struct c_declspecs
*
9743 declspecs_add_scspec (source_location loc
,
9744 struct c_declspecs
*specs
,
9748 enum c_storage_class n
= csc_none
;
9750 specs
->declspecs_seen_p
= true;
9751 gcc_assert (TREE_CODE (scspec
) == IDENTIFIER_NODE
9752 && C_IS_RESERVED_WORD (scspec
));
9753 i
= C_RID_CODE (scspec
);
9754 if (specs
->non_sc_seen_p
)
9755 warning (OPT_Wold_style_declaration
,
9756 "%qE is not at beginning of declaration", scspec
);
9760 /* C99 permits duplicate inline. Although of doubtful utility,
9761 it seems simplest to permit it in gnu89 mode as well, as
9762 there is also little utility in maintaining this as a
9763 difference between gnu89 and C99 inline. */
9765 specs
->inline_p
= true;
9766 specs
->locations
[cdw_inline
] = loc
;
9769 /* Duplicate _Noreturn is permitted. */
9771 specs
->noreturn_p
= true;
9772 specs
->locations
[cdw_noreturn
] = loc
;
9775 dupe
= specs
->thread_p
;
9776 if (specs
->storage_class
== csc_auto
)
9777 error ("%<__thread%> used with %<auto%>");
9778 else if (specs
->storage_class
== csc_register
)
9779 error ("%<__thread%> used with %<register%>");
9780 else if (specs
->storage_class
== csc_typedef
)
9781 error ("%<__thread%> used with %<typedef%>");
9784 specs
->thread_p
= true;
9785 specs
->locations
[cdw_thread
] = loc
;
9793 /* Diagnose "__thread extern". */
9794 if (specs
->thread_p
)
9795 error ("%<__thread%> before %<extern%>");
9802 /* Diagnose "__thread static". */
9803 if (specs
->thread_p
)
9804 error ("%<__thread%> before %<static%>");
9812 if (n
!= csc_none
&& n
== specs
->storage_class
)
9815 error ("duplicate %qE", scspec
);
9818 if (specs
->storage_class
!= csc_none
&& n
!= specs
->storage_class
)
9820 error ("multiple storage classes in declaration specifiers");
9824 specs
->storage_class
= n
;
9825 specs
->locations
[cdw_storage_class
] = loc
;
9826 if (n
!= csc_extern
&& n
!= csc_static
&& specs
->thread_p
)
9828 error ("%<__thread%> used with %qE", scspec
);
9829 specs
->thread_p
= false;
9836 /* Add the attributes ATTRS to the declaration specifiers SPECS,
9839 struct c_declspecs
*
9840 declspecs_add_attrs (source_location loc
, struct c_declspecs
*specs
, tree attrs
)
9842 specs
->attrs
= chainon (attrs
, specs
->attrs
);
9843 specs
->locations
[cdw_attributes
] = loc
;
9844 specs
->declspecs_seen_p
= true;
9848 /* Add an _Alignas specifier (expression ALIGN, or type whose
9849 alignment is ALIGN) to the declaration specifiers SPECS, returning
9851 struct c_declspecs
*
9852 declspecs_add_alignas (source_location loc
,
9853 struct c_declspecs
*specs
, tree align
)
9856 specs
->alignas_p
= true;
9857 specs
->locations
[cdw_alignas
] = loc
;
9858 if (align
== error_mark_node
)
9860 align_log
= check_user_alignment (align
, true);
9861 if (align_log
> specs
->align_log
)
9862 specs
->align_log
= align_log
;
9866 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
9867 specifiers with any other type specifier to determine the resulting
9868 type. This is where ISO C checks on complex types are made, since
9869 "_Complex long" is a prefix of the valid ISO C type "_Complex long
9872 struct c_declspecs
*
9873 finish_declspecs (struct c_declspecs
*specs
)
9875 /* If a type was specified as a whole, we have no modifiers and are
9877 if (specs
->type
!= NULL_TREE
)
9879 gcc_assert (!specs
->long_p
&& !specs
->long_long_p
&& !specs
->short_p
9880 && !specs
->signed_p
&& !specs
->unsigned_p
9881 && !specs
->complex_p
);
9883 /* Set a dummy type. */
9884 if (TREE_CODE (specs
->type
) == ERROR_MARK
)
9885 specs
->type
= integer_type_node
;
9889 /* If none of "void", "_Bool", "char", "int", "float" or "double"
9890 has been specified, treat it as "int" unless "_Complex" is
9891 present and there are no other specifiers. If we just have
9892 "_Complex", it is equivalent to "_Complex double", but e.g.
9893 "_Complex short" is equivalent to "_Complex short int". */
9894 if (specs
->typespec_word
== cts_none
)
9896 if (specs
->saturating_p
)
9898 error_at (specs
->locations
[cdw_saturating
],
9899 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
9900 if (!targetm
.fixed_point_supported_p ())
9901 error_at (specs
->locations
[cdw_saturating
],
9902 "fixed-point types not supported for this target");
9903 specs
->typespec_word
= cts_fract
;
9905 else if (specs
->long_p
|| specs
->short_p
9906 || specs
->signed_p
|| specs
->unsigned_p
)
9908 specs
->typespec_word
= cts_int
;
9910 else if (specs
->complex_p
)
9912 specs
->typespec_word
= cts_double
;
9913 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
9914 "ISO C does not support plain %<complex%> meaning "
9915 "%<double complex%>");
9919 specs
->typespec_word
= cts_int
;
9920 specs
->default_int_p
= true;
9921 /* We don't diagnose this here because grokdeclarator will
9922 give more specific diagnostics according to whether it is
9923 a function definition. */
9927 /* If "signed" was specified, record this to distinguish "int" and
9928 "signed int" in the case of a bit-field with
9929 -funsigned-bitfields. */
9930 specs
->explicit_signed_p
= specs
->signed_p
;
9932 /* Now compute the actual type. */
9933 switch (specs
->typespec_word
)
9936 gcc_assert (!specs
->long_p
&& !specs
->short_p
9937 && !specs
->signed_p
&& !specs
->unsigned_p
9938 && !specs
->complex_p
);
9939 specs
->type
= void_type_node
;
9942 gcc_assert (!specs
->long_p
&& !specs
->short_p
9943 && !specs
->signed_p
&& !specs
->unsigned_p
9944 && !specs
->complex_p
);
9945 specs
->type
= boolean_type_node
;
9948 gcc_assert (!specs
->long_p
&& !specs
->short_p
);
9949 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
9950 if (specs
->signed_p
)
9951 specs
->type
= signed_char_type_node
;
9952 else if (specs
->unsigned_p
)
9953 specs
->type
= unsigned_char_type_node
;
9955 specs
->type
= char_type_node
;
9956 if (specs
->complex_p
)
9958 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
9959 "ISO C does not support complex integer types");
9960 specs
->type
= build_complex_type (specs
->type
);
9964 gcc_assert (!specs
->long_p
&& !specs
->short_p
&& !specs
->long_long_p
);
9965 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
9966 specs
->type
= (specs
->unsigned_p
9967 ? int128_unsigned_type_node
9968 : int128_integer_type_node
);
9969 if (specs
->complex_p
)
9971 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
9972 "ISO C does not support complex integer types");
9973 specs
->type
= build_complex_type (specs
->type
);
9977 gcc_assert (!(specs
->long_p
&& specs
->short_p
));
9978 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
9979 if (specs
->long_long_p
)
9980 specs
->type
= (specs
->unsigned_p
9981 ? long_long_unsigned_type_node
9982 : long_long_integer_type_node
);
9983 else if (specs
->long_p
)
9984 specs
->type
= (specs
->unsigned_p
9985 ? long_unsigned_type_node
9986 : long_integer_type_node
);
9987 else if (specs
->short_p
)
9988 specs
->type
= (specs
->unsigned_p
9989 ? short_unsigned_type_node
9990 : short_integer_type_node
);
9992 specs
->type
= (specs
->unsigned_p
9993 ? unsigned_type_node
9994 : integer_type_node
);
9995 if (specs
->complex_p
)
9997 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
9998 "ISO C does not support complex integer types");
9999 specs
->type
= build_complex_type (specs
->type
);
10003 gcc_assert (!specs
->long_p
&& !specs
->short_p
10004 && !specs
->signed_p
&& !specs
->unsigned_p
);
10005 specs
->type
= (specs
->complex_p
10006 ? complex_float_type_node
10007 : float_type_node
);
10010 gcc_assert (!specs
->long_long_p
&& !specs
->short_p
10011 && !specs
->signed_p
&& !specs
->unsigned_p
);
10014 specs
->type
= (specs
->complex_p
10015 ? complex_long_double_type_node
10016 : long_double_type_node
);
10020 specs
->type
= (specs
->complex_p
10021 ? complex_double_type_node
10022 : double_type_node
);
10027 case cts_dfloat128
:
10028 gcc_assert (!specs
->long_p
&& !specs
->long_long_p
&& !specs
->short_p
10029 && !specs
->signed_p
&& !specs
->unsigned_p
&& !specs
->complex_p
);
10030 if (specs
->typespec_word
== cts_dfloat32
)
10031 specs
->type
= dfloat32_type_node
;
10032 else if (specs
->typespec_word
== cts_dfloat64
)
10033 specs
->type
= dfloat64_type_node
;
10035 specs
->type
= dfloat128_type_node
;
10038 gcc_assert (!specs
->complex_p
);
10039 if (!targetm
.fixed_point_supported_p ())
10040 specs
->type
= integer_type_node
;
10041 else if (specs
->saturating_p
)
10043 if (specs
->long_long_p
)
10044 specs
->type
= specs
->unsigned_p
10045 ? sat_unsigned_long_long_fract_type_node
10046 : sat_long_long_fract_type_node
;
10047 else if (specs
->long_p
)
10048 specs
->type
= specs
->unsigned_p
10049 ? sat_unsigned_long_fract_type_node
10050 : sat_long_fract_type_node
;
10051 else if (specs
->short_p
)
10052 specs
->type
= specs
->unsigned_p
10053 ? sat_unsigned_short_fract_type_node
10054 : sat_short_fract_type_node
;
10056 specs
->type
= specs
->unsigned_p
10057 ? sat_unsigned_fract_type_node
10058 : sat_fract_type_node
;
10062 if (specs
->long_long_p
)
10063 specs
->type
= specs
->unsigned_p
10064 ? unsigned_long_long_fract_type_node
10065 : long_long_fract_type_node
;
10066 else if (specs
->long_p
)
10067 specs
->type
= specs
->unsigned_p
10068 ? unsigned_long_fract_type_node
10069 : long_fract_type_node
;
10070 else if (specs
->short_p
)
10071 specs
->type
= specs
->unsigned_p
10072 ? unsigned_short_fract_type_node
10073 : short_fract_type_node
;
10075 specs
->type
= specs
->unsigned_p
10076 ? unsigned_fract_type_node
10081 gcc_assert (!specs
->complex_p
);
10082 if (!targetm
.fixed_point_supported_p ())
10083 specs
->type
= integer_type_node
;
10084 else if (specs
->saturating_p
)
10086 if (specs
->long_long_p
)
10087 specs
->type
= specs
->unsigned_p
10088 ? sat_unsigned_long_long_accum_type_node
10089 : sat_long_long_accum_type_node
;
10090 else if (specs
->long_p
)
10091 specs
->type
= specs
->unsigned_p
10092 ? sat_unsigned_long_accum_type_node
10093 : sat_long_accum_type_node
;
10094 else if (specs
->short_p
)
10095 specs
->type
= specs
->unsigned_p
10096 ? sat_unsigned_short_accum_type_node
10097 : sat_short_accum_type_node
;
10099 specs
->type
= specs
->unsigned_p
10100 ? sat_unsigned_accum_type_node
10101 : sat_accum_type_node
;
10105 if (specs
->long_long_p
)
10106 specs
->type
= specs
->unsigned_p
10107 ? unsigned_long_long_accum_type_node
10108 : long_long_accum_type_node
;
10109 else if (specs
->long_p
)
10110 specs
->type
= specs
->unsigned_p
10111 ? unsigned_long_accum_type_node
10112 : long_accum_type_node
;
10113 else if (specs
->short_p
)
10114 specs
->type
= specs
->unsigned_p
10115 ? unsigned_short_accum_type_node
10116 : short_accum_type_node
;
10118 specs
->type
= specs
->unsigned_p
10119 ? unsigned_accum_type_node
10124 gcc_unreachable ();
10130 /* A subroutine of c_write_global_declarations. Perform final processing
10131 on one file scope's declarations (or the external scope's declarations),
10135 c_write_global_declarations_1 (tree globals
)
10140 /* Process the decls in the order they were written. */
10141 for (decl
= globals
; decl
; decl
= DECL_CHAIN (decl
))
10143 /* Check for used but undefined static functions using the C
10144 standard's definition of "used", and set TREE_NO_WARNING so
10145 that check_global_declarations doesn't repeat the check. */
10146 if (TREE_CODE (decl
) == FUNCTION_DECL
10147 && DECL_INITIAL (decl
) == 0
10148 && DECL_EXTERNAL (decl
)
10149 && !TREE_PUBLIC (decl
)
10150 && C_DECL_USED (decl
))
10152 pedwarn (input_location
, 0, "%q+F used but never defined", decl
);
10153 TREE_NO_WARNING (decl
) = 1;
10156 wrapup_global_declaration_1 (decl
);
10161 reconsider
= false;
10162 for (decl
= globals
; decl
; decl
= DECL_CHAIN (decl
))
10163 reconsider
|= wrapup_global_declaration_2 (decl
);
10165 while (reconsider
);
10167 for (decl
= globals
; decl
; decl
= DECL_CHAIN (decl
))
10168 check_global_declaration_1 (decl
);
10171 /* A subroutine of c_write_global_declarations Emit debug information for each
10172 of the declarations in GLOBALS. */
10175 c_write_global_declarations_2 (tree globals
)
10179 for (decl
= globals
; decl
; decl
= DECL_CHAIN (decl
))
10180 debug_hooks
->global_decl (decl
);
10183 /* Callback to collect a source_ref from a DECL. */
10186 collect_source_ref_cb (tree decl
)
10188 if (!DECL_IS_BUILTIN (decl
))
10189 collect_source_ref (LOCATION_FILE (decl_sloc (decl
, false)));
10192 /* Preserve the external declarations scope across a garbage collect. */
10193 static GTY(()) tree ext_block
;
10195 /* Collect all references relevant to SOURCE_FILE. */
10198 collect_all_refs (const char *source_file
)
10203 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
10204 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t
)), source_file
);
10206 collect_ada_nodes (BLOCK_VARS (ext_block
), source_file
);
10209 /* Iterate over all global declarations and call CALLBACK. */
10212 for_each_global_decl (void (*callback
) (tree decl
))
10219 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
10221 decls
= DECL_INITIAL (t
);
10222 for (decl
= BLOCK_VARS (decls
); decl
; decl
= TREE_CHAIN (decl
))
10226 for (decl
= BLOCK_VARS (ext_block
); decl
; decl
= TREE_CHAIN (decl
))
10231 c_write_global_declarations (void)
10236 /* We don't want to do this if generating a PCH. */
10240 timevar_start (TV_PHASE_DEFERRED
);
10242 /* Do the Objective-C stuff. This is where all the Objective-C
10243 module stuff gets generated (symtab, class/protocol/selector
10245 if (c_dialect_objc ())
10246 objc_write_global_declarations ();
10248 /* Close the external scope. */
10249 ext_block
= pop_scope ();
10250 external_scope
= 0;
10251 gcc_assert (!current_scope
);
10253 /* Handle -fdump-ada-spec[-slim]. */
10254 if (flag_dump_ada_spec
|| flag_dump_ada_spec_slim
)
10256 /* Build a table of files to generate specs for */
10257 if (flag_dump_ada_spec_slim
)
10258 collect_source_ref (main_input_filename
);
10260 for_each_global_decl (collect_source_ref_cb
);
10262 dump_ada_specs (collect_all_refs
, NULL
);
10267 tree tmp
= BLOCK_VARS (ext_block
);
10269 FILE * stream
= dump_begin (TDI_tu
, &flags
);
10272 dump_node (tmp
, flags
& ~TDF_SLIM
, stream
);
10273 dump_end (TDI_tu
, stream
);
10277 /* Process all file scopes in this compilation, and the external_scope,
10278 through wrapup_global_declarations and check_global_declarations. */
10279 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
10280 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t
)));
10281 c_write_global_declarations_1 (BLOCK_VARS (ext_block
));
10283 timevar_stop (TV_PHASE_DEFERRED
);
10284 timevar_start (TV_PHASE_OPT_GEN
);
10286 /* We're done parsing; proceed to optimize and emit assembly.
10287 FIXME: shouldn't be the front end's responsibility to call this. */
10288 finalize_compilation_unit ();
10290 timevar_stop (TV_PHASE_OPT_GEN
);
10291 timevar_start (TV_PHASE_DBGINFO
);
10293 /* After cgraph has had a chance to emit everything that's going to
10294 be emitted, output debug information for globals. */
10295 if (!seen_error ())
10297 timevar_push (TV_SYMOUT
);
10298 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
10299 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t
)));
10300 c_write_global_declarations_2 (BLOCK_VARS (ext_block
));
10301 timevar_pop (TV_SYMOUT
);
10305 timevar_stop (TV_PHASE_DBGINFO
);
10308 /* Register reserved keyword WORD as qualifier for address space AS. */
10311 c_register_addr_space (const char *word
, addr_space_t as
)
10313 int rid
= RID_FIRST_ADDR_SPACE
+ as
;
10316 /* Address space qualifiers are only supported
10317 in C with GNU extensions enabled. */
10318 if (c_dialect_objc () || flag_no_asm
)
10321 id
= get_identifier (word
);
10322 C_SET_RID_CODE (id
, rid
);
10323 C_IS_RESERVED_WORD (id
) = 1;
10324 ridpointers
[rid
] = id
;
10327 /* Return identifier to look up for omp declare reduction. */
10330 c_omp_reduction_id (enum tree_code reduction_code
, tree reduction_id
)
10332 const char *p
= NULL
;
10333 switch (reduction_code
)
10335 case PLUS_EXPR
: p
= "+"; break;
10336 case MULT_EXPR
: p
= "*"; break;
10337 case MINUS_EXPR
: p
= "-"; break;
10338 case BIT_AND_EXPR
: p
= "&"; break;
10339 case BIT_XOR_EXPR
: p
= "^"; break;
10340 case BIT_IOR_EXPR
: p
= "|"; break;
10341 case TRUTH_ANDIF_EXPR
: p
= "&&"; break;
10342 case TRUTH_ORIF_EXPR
: p
= "||"; break;
10343 case MIN_EXPR
: p
= "min"; break;
10344 case MAX_EXPR
: p
= "max"; break;
10351 if (TREE_CODE (reduction_id
) != IDENTIFIER_NODE
)
10352 return error_mark_node
;
10353 p
= IDENTIFIER_POINTER (reduction_id
);
10356 const char prefix
[] = "omp declare reduction ";
10357 size_t lenp
= sizeof (prefix
);
10358 size_t len
= strlen (p
);
10359 char *name
= XALLOCAVEC (char, lenp
+ len
);
10360 memcpy (name
, prefix
, lenp
- 1);
10361 memcpy (name
+ lenp
- 1, p
, len
+ 1);
10362 return get_identifier (name
);
10365 /* Lookup REDUCTION_ID in the current scope, or create an artificial
10366 VAR_DECL, bind it into the current scope and return it. */
10369 c_omp_reduction_decl (tree reduction_id
)
10371 struct c_binding
*b
= I_SYMBOL_BINDING (reduction_id
);
10372 if (b
!= NULL
&& B_IN_CURRENT_SCOPE (b
))
10375 tree decl
= build_decl (BUILTINS_LOCATION
, VAR_DECL
,
10376 reduction_id
, integer_type_node
);
10377 DECL_ARTIFICIAL (decl
) = 1;
10378 DECL_EXTERNAL (decl
) = 1;
10379 TREE_STATIC (decl
) = 1;
10380 TREE_PUBLIC (decl
) = 0;
10381 bind (reduction_id
, decl
, current_scope
, true, false, BUILTINS_LOCATION
);
10385 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
10388 c_omp_reduction_lookup (tree reduction_id
, tree type
)
10390 struct c_binding
*b
= I_SYMBOL_BINDING (reduction_id
);
10394 for (t
= DECL_INITIAL (b
->decl
); t
; t
= TREE_CHAIN (t
))
10395 if (comptypes (TREE_PURPOSE (t
), type
))
10396 return TREE_VALUE (t
);
10399 return error_mark_node
;
10402 /* Helper function called via walk_tree, to diagnose invalid
10403 #pragma omp declare reduction combiners or initializers. */
10406 c_check_omp_declare_reduction_r (tree
*tp
, int *, void *data
)
10408 tree
*vars
= (tree
*) data
;
10409 if (SSA_VAR_P (*tp
)
10410 && !DECL_ARTIFICIAL (*tp
)
10414 location_t loc
= DECL_SOURCE_LOCATION (vars
[0]);
10415 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars
[0])), "omp_out") == 0)
10416 error_at (loc
, "%<#pragma omp declare reduction%> combiner refers to "
10417 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
10420 error_at (loc
, "%<#pragma omp declare reduction%> initializer refers "
10421 "to variable %qD which is not %<omp_priv%> nor "
10429 #include "gt-c-c-decl.h"