1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2016 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 "stringpool.h"
37 #include "print-tree.h"
38 #include "stor-layout.h"
43 #include "c-family/c-objc.h"
44 #include "c-family/c-pragma.h"
45 #include "c-family/c-ubsan.h"
47 #include "langhooks.h"
48 #include "tree-iterator.h"
51 #include "c-family/c-ada-spec.h"
54 #include "spellcheck-tree.h"
55 #include "gcc-rich-location.h"
57 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
59 { NORMAL
, /* Ordinary declaration */
60 FUNCDEF
, /* Function definition */
61 PARM
, /* Declaration of parm before function body */
62 FIELD
, /* Declaration inside struct or union */
63 TYPENAME
}; /* Typename (inside cast or sizeof) */
65 /* States indicating how grokdeclarator() should handle declspecs marked
66 with __attribute__((deprecated)). An object declared as
67 __attribute__((deprecated)) suppresses warnings of uses of other
70 enum deprecated_states
{
76 /* Nonzero if we have seen an invalid cross reference
77 to a struct, union, or enum, but not yet printed the message. */
78 tree pending_invalid_xref
;
80 /* File and line to appear in the eventual error message. */
81 location_t pending_invalid_xref_location
;
83 /* The file and line that the prototype came from if this is an
84 old-style definition; used for diagnostics in
85 store_parm_decls_oldstyle. */
87 static location_t current_function_prototype_locus
;
89 /* Whether this prototype was built-in. */
91 static bool current_function_prototype_built_in
;
93 /* The argument type information of this prototype. */
95 static tree current_function_prototype_arg_types
;
97 /* The argument information structure for the function currently being
100 static struct c_arg_info
*current_function_arg_info
;
102 /* The obstack on which parser and related data structures, which are
103 not live beyond their top-level declaration or definition, are
105 struct obstack parser_obstack
;
107 /* The current statement tree. */
109 static GTY(()) struct stmt_tree_s c_stmt_tree
;
111 /* State saving variables. */
115 /* A list of decls to be made automatically visible in each file scope. */
116 static GTY(()) tree visible_builtins
;
118 /* Set to 0 at beginning of a function definition, set to 1 if
119 a return statement that specifies a return value is seen. */
121 int current_function_returns_value
;
123 /* Set to 0 at beginning of a function definition, set to 1 if
124 a return statement with no argument is seen. */
126 int current_function_returns_null
;
128 /* Set to 0 at beginning of a function definition, set to 1 if
129 a call to a noreturn function is seen. */
131 int current_function_returns_abnormally
;
133 /* Set to nonzero by `grokdeclarator' for a function
134 whose return type is defaulted, if warnings for this are desired. */
136 static int warn_about_return_type
;
138 /* Nonzero when the current toplevel function contains a declaration
139 of a nested function which is never defined. */
141 static bool undef_nested_function
;
143 /* If non-zero, implicit "omp declare target" attribute is added into the
145 int current_omp_declare_target_attribute
;
147 /* Each c_binding structure describes one binding of an identifier to
148 a decl. All the decls in a scope - irrespective of namespace - are
149 chained together by the ->prev field, which (as the name implies)
150 runs in reverse order. All the decls in a given namespace bound to
151 a given identifier are chained by the ->shadowed field, which runs
152 from inner to outer scopes.
154 The ->decl field usually points to a DECL node, but there are two
155 exceptions. In the namespace of type tags, the bound entity is a
156 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
157 identifier is encountered, it is bound to error_mark_node to
158 suppress further errors about that identifier in the current
161 The ->u.type field stores the type of the declaration in this scope;
162 if NULL, the type is the type of the ->decl field. This is only of
163 relevance for objects with external or internal linkage which may
164 be redeclared in inner scopes, forming composite types that only
165 persist for the duration of those scopes. In the external scope,
166 this stores the composite of all the types declared for this
167 object, visible or not. The ->inner_comp field (used only at file
168 scope) stores whether an incomplete array type at file scope was
169 completed at an inner scope to an array size other than 1.
171 The ->u.label field is used for labels. It points to a structure
172 which stores additional information used for warnings.
174 The depth field is copied from the scope structure that holds this
175 decl. It is used to preserve the proper ordering of the ->shadowed
176 field (see bind()) and also for a handful of special-case checks.
177 Finally, the invisible bit is true for a decl which should be
178 ignored for purposes of normal name lookup, and the nested bit is
179 true for a decl that's been bound a second time in an inner scope;
180 in all such cases, the binding in the outer scope will have its
181 invisible bit true. */
183 struct GTY((chain_next ("%h.prev"))) c_binding
{
184 union GTY(()) { /* first so GTY desc can use decl */
185 tree
GTY((tag ("0"))) type
; /* the type in this scope */
186 struct c_label_vars
* GTY((tag ("1"))) label
; /* for warnings */
187 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u
;
188 tree decl
; /* the decl bound */
189 tree id
; /* the identifier it's bound to */
190 struct c_binding
*prev
; /* the previous decl in this scope */
191 struct c_binding
*shadowed
; /* the innermost decl shadowed by this one */
192 unsigned int depth
: 28; /* depth of this scope */
193 BOOL_BITFIELD invisible
: 1; /* normal lookup should ignore this binding */
194 BOOL_BITFIELD nested
: 1; /* do not set DECL_CONTEXT when popping */
195 BOOL_BITFIELD inner_comp
: 1; /* incomplete array completed in inner scope */
196 BOOL_BITFIELD in_struct
: 1; /* currently defined as struct field */
197 location_t locus
; /* location for nested bindings */
199 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
200 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
201 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
202 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
204 /* Each C symbol points to three linked lists of c_binding structures.
205 These describe the values of the identifier in the three different
206 namespaces defined by the language. */
208 struct GTY(()) lang_identifier
{
209 struct c_common_identifier common_id
;
210 struct c_binding
*symbol_binding
; /* vars, funcs, constants, typedefs */
211 struct c_binding
*tag_binding
; /* struct/union/enum tags */
212 struct c_binding
*label_binding
; /* labels */
215 /* Validate c-lang.c's assumptions. */
216 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
217 [(sizeof(struct lang_identifier
) == C_SIZEOF_STRUCT_LANG_IDENTIFIER
) ? 1 : -1];
219 /* The binding oracle; see c-tree.h. */
220 void (*c_binding_oracle
) (enum c_oracle_request
, tree identifier
);
222 /* This flag is set on an identifier if we have previously asked the
223 binding oracle for this identifier's symbol binding. */
224 #define I_SYMBOL_CHECKED(node) \
225 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
227 static inline struct c_binding
* *
228 i_symbol_binding (tree node
)
230 struct lang_identifier
*lid
231 = (struct lang_identifier
*) IDENTIFIER_NODE_CHECK (node
);
233 if (lid
->symbol_binding
== NULL
234 && c_binding_oracle
!= NULL
235 && !I_SYMBOL_CHECKED (node
))
237 /* Set the "checked" flag first, to avoid infinite recursion
238 when the binding oracle calls back into gcc. */
239 I_SYMBOL_CHECKED (node
) = 1;
240 c_binding_oracle (C_ORACLE_SYMBOL
, node
);
243 return &lid
->symbol_binding
;
246 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
248 #define I_SYMBOL_DECL(node) \
249 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
251 /* This flag is set on an identifier if we have previously asked the
252 binding oracle for this identifier's tag binding. */
253 #define I_TAG_CHECKED(node) \
254 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
256 static inline struct c_binding
**
257 i_tag_binding (tree node
)
259 struct lang_identifier
*lid
260 = (struct lang_identifier
*) IDENTIFIER_NODE_CHECK (node
);
262 if (lid
->tag_binding
== NULL
263 && c_binding_oracle
!= NULL
264 && !I_TAG_CHECKED (node
))
266 /* Set the "checked" flag first, to avoid infinite recursion
267 when the binding oracle calls back into gcc. */
268 I_TAG_CHECKED (node
) = 1;
269 c_binding_oracle (C_ORACLE_TAG
, node
);
272 return &lid
->tag_binding
;
275 #define I_TAG_BINDING(node) (*i_tag_binding (node))
277 #define I_TAG_DECL(node) \
278 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
280 /* This flag is set on an identifier if we have previously asked the
281 binding oracle for this identifier's label binding. */
282 #define I_LABEL_CHECKED(node) \
283 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
285 static inline struct c_binding
**
286 i_label_binding (tree node
)
288 struct lang_identifier
*lid
289 = (struct lang_identifier
*) IDENTIFIER_NODE_CHECK (node
);
291 if (lid
->label_binding
== NULL
292 && c_binding_oracle
!= NULL
293 && !I_LABEL_CHECKED (node
))
295 /* Set the "checked" flag first, to avoid infinite recursion
296 when the binding oracle calls back into gcc. */
297 I_LABEL_CHECKED (node
) = 1;
298 c_binding_oracle (C_ORACLE_LABEL
, node
);
301 return &lid
->label_binding
;
304 #define I_LABEL_BINDING(node) (*i_label_binding (node))
306 #define I_LABEL_DECL(node) \
307 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
309 /* The resulting tree type. */
311 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
312 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
314 union tree_node
GTY ((tag ("0"),
315 desc ("tree_node_structure (&%h)")))
317 struct lang_identifier
GTY ((tag ("1"))) identifier
;
320 /* Track bindings and other things that matter for goto warnings. For
321 efficiency, we do not gather all the decls at the point of
322 definition. Instead, we point into the bindings structure. As
323 scopes are popped, we update these structures and gather the decls
324 that matter at that time. */
326 struct GTY(()) c_spot_bindings
{
327 /* The currently open scope which holds bindings defined when the
328 label was defined or the goto statement was found. */
329 struct c_scope
*scope
;
330 /* The bindings in the scope field which were defined at the point
331 of the label or goto. This lets us look at older or newer
332 bindings in the scope, as appropriate. */
333 struct c_binding
*bindings_in_scope
;
334 /* The number of statement expressions that have started since this
335 label or goto statement was defined. This is zero if we are at
336 the same statement expression level. It is positive if we are in
337 a statement expression started since this spot. It is negative
338 if this spot was in a statement expression and we have left
341 /* Whether we started in a statement expression but are no longer in
342 it. This is set to true if stmt_exprs ever goes negative. */
346 /* This structure is used to keep track of bindings seen when a goto
347 statement is defined. This is only used if we see the goto
348 statement before we see the label. */
350 struct GTY(()) c_goto_bindings
{
351 /* The location of the goto statement. */
353 /* The bindings of the goto statement. */
354 struct c_spot_bindings goto_bindings
;
357 typedef struct c_goto_bindings
*c_goto_bindings_p
;
359 /* The additional information we keep track of for a label binding.
360 These fields are updated as scopes are popped. */
362 struct GTY(()) c_label_vars
{
363 /* The shadowed c_label_vars, when one label shadows another (which
364 can only happen using a __label__ declaration). */
365 struct c_label_vars
*shadowed
;
366 /* The bindings when the label was defined. */
367 struct c_spot_bindings label_bindings
;
368 /* A list of decls that we care about: decls about which we should
369 warn if a goto branches to this label from later in the function.
370 Decls are added to this list as scopes are popped. We only add
371 the decls that matter. */
372 vec
<tree
, va_gc
> *decls_in_scope
;
373 /* A list of goto statements to this label. This is only used for
374 goto statements seen before the label was defined, so that we can
375 issue appropriate warnings for them. */
376 vec
<c_goto_bindings_p
, va_gc
> *gotos
;
379 /* Each c_scope structure describes the complete contents of one
380 scope. Four scopes are distinguished specially: the innermost or
381 current scope, the innermost function scope, the file scope (always
382 the second to outermost) and the outermost or external scope.
384 Most declarations are recorded in the current scope.
386 All normal label declarations are recorded in the innermost
387 function scope, as are bindings of undeclared identifiers to
388 error_mark_node. (GCC permits nested functions as an extension,
389 hence the 'innermost' qualifier.) Explicitly declared labels
390 (using the __label__ extension) appear in the current scope.
392 Being in the file scope (current_scope == file_scope) causes
393 special behavior in several places below. Also, under some
394 conditions the Objective-C front end records declarations in the
395 file scope even though that isn't the current scope.
397 All declarations with external linkage are recorded in the external
398 scope, even if they aren't visible there; this models the fact that
399 such declarations are visible to the entire program, and (with a
400 bit of cleverness, see pushdecl) allows diagnosis of some violations
401 of C99 6.2.2p7 and 6.2.7p2:
403 If, within the same translation unit, the same identifier appears
404 with both internal and external linkage, the behavior is
407 All declarations that refer to the same object or function shall
408 have compatible type; otherwise, the behavior is undefined.
410 Initially only the built-in declarations, which describe compiler
411 intrinsic functions plus a subset of the standard library, are in
414 The order of the blocks list matters, and it is frequently appended
415 to. To avoid having to walk all the way to the end of the list on
416 each insertion, or reverse the list later, we maintain a pointer to
417 the last list entry. (FIXME: It should be feasible to use a reversed
420 The bindings list is strictly in reverse order of declarations;
421 pop_scope relies on this. */
424 struct GTY((chain_next ("%h.outer"))) c_scope
{
425 /* The scope containing this one. */
426 struct c_scope
*outer
;
428 /* The next outermost function scope. */
429 struct c_scope
*outer_function
;
431 /* All bindings in this scope. */
432 struct c_binding
*bindings
;
434 /* For each scope (except the global one), a chain of BLOCK nodes
435 for all the scopes that were entered and exited one level down. */
439 /* The depth of this scope. Used to keep the ->shadowed chain of
440 bindings sorted innermost to outermost. */
441 unsigned int depth
: 28;
443 /* True if we are currently filling this scope with parameter
445 BOOL_BITFIELD parm_flag
: 1;
447 /* True if we saw [*] in this scope. Used to give an error messages
448 if these appears in a function definition. */
449 BOOL_BITFIELD had_vla_unspec
: 1;
451 /* True if we already complained about forward parameter decls
452 in this scope. This prevents double warnings on
453 foo (int a; int b; ...) */
454 BOOL_BITFIELD warned_forward_parm_decls
: 1;
456 /* True if this is the outermost block scope of a function body.
457 This scope contains the parameters, the local variables declared
458 in the outermost block, and all the labels (except those in
459 nested functions, or declared at block scope with __label__). */
460 BOOL_BITFIELD function_body
: 1;
462 /* True means make a BLOCK for this scope no matter what. */
463 BOOL_BITFIELD keep
: 1;
465 /* True means that an unsuffixed float constant is _Decimal64. */
466 BOOL_BITFIELD float_const_decimal64
: 1;
468 /* True if this scope has any label bindings. This is used to speed
469 up searching for labels when popping scopes, particularly since
470 labels are normally only found at function scope. */
471 BOOL_BITFIELD has_label_bindings
: 1;
473 /* True if we should issue a warning if a goto statement crosses any
474 of the bindings. We still need to check the list of bindings to
475 find the specific ones we need to warn about. This is true if
476 decl_jump_unsafe would return true for any of the bindings. This
477 is used to avoid looping over all the bindings unnecessarily. */
478 BOOL_BITFIELD has_jump_unsafe_decl
: 1;
481 /* The scope currently in effect. */
483 static GTY(()) struct c_scope
*current_scope
;
485 /* The innermost function scope. Ordinary (not explicitly declared)
486 labels, bindings to error_mark_node, and the lazily-created
487 bindings of __func__ and its friends get this scope. */
489 static GTY(()) struct c_scope
*current_function_scope
;
491 /* The C file scope. This is reset for each input translation unit. */
493 static GTY(()) struct c_scope
*file_scope
;
495 /* The outermost scope. This is used for all declarations with
496 external linkage, and only these, hence the name. */
498 static GTY(()) struct c_scope
*external_scope
;
500 /* A chain of c_scope structures awaiting reuse. */
502 static GTY((deletable
)) struct c_scope
*scope_freelist
;
504 /* A chain of c_binding structures awaiting reuse. */
506 static GTY((deletable
)) struct c_binding
*binding_freelist
;
508 /* Append VAR to LIST in scope SCOPE. */
509 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
510 struct c_scope *s_ = (scope); \
512 if (s_->list##_last) \
513 BLOCK_CHAIN (s_->list##_last) = d_; \
516 s_->list##_last = d_; \
519 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
520 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
521 struct c_scope *t_ = (tscope); \
522 struct c_scope *f_ = (fscope); \
524 BLOCK_CHAIN (t_->to##_last) = f_->from; \
527 t_->to##_last = f_->from##_last; \
530 /* A c_inline_static structure stores details of a static identifier
531 referenced in a definition of a function that may be an inline
532 definition if no subsequent declaration of that function uses
533 "extern" or does not use "inline". */
535 struct GTY((chain_next ("%h.next"))) c_inline_static
{
536 /* The location for a diagnostic. */
539 /* The function that may be an inline definition. */
542 /* The object or function referenced. */
545 /* What sort of reference this is. */
546 enum c_inline_static_type type
;
548 /* The next such structure or NULL. */
549 struct c_inline_static
*next
;
552 /* List of static identifiers used or referenced in functions that may
553 be inline definitions. */
554 static GTY(()) struct c_inline_static
*c_inline_statics
;
556 /* True means unconditionally make a BLOCK for the next scope pushed. */
558 static bool keep_next_level_flag
;
560 /* True means the next call to push_scope will be the outermost scope
561 of a function body, so do not push a new scope, merely cease
562 expecting parameter decls. */
564 static bool next_is_function_body
;
566 /* A vector of pointers to c_binding structures. */
568 typedef struct c_binding
*c_binding_ptr
;
570 /* Information that we keep for a struct or union while it is being
573 struct c_struct_parse_info
575 /* If warn_cxx_compat, a list of types defined within this
577 auto_vec
<tree
> struct_types
;
578 /* If warn_cxx_compat, a list of field names which have bindings,
579 and which are defined in this struct, but which are not defined
580 in any enclosing struct. This is used to clear the in_struct
581 field of the c_bindings structure. */
582 auto_vec
<c_binding_ptr
> fields
;
583 /* If warn_cxx_compat, a list of typedef names used when defining
584 fields in this struct. */
585 auto_vec
<tree
> typedefs_seen
;
588 /* Information for the struct or union currently being parsed, or
589 NULL if not parsing a struct or union. */
590 static struct c_struct_parse_info
*struct_parse_info
;
592 /* Forward declarations. */
593 static tree
lookup_name_in_scope (tree
, struct c_scope
*);
594 static tree
c_make_fname_decl (location_t
, tree
, int);
595 static tree
grokdeclarator (const struct c_declarator
*,
596 struct c_declspecs
*,
597 enum decl_context
, bool, tree
*, tree
*, tree
*,
598 bool *, enum deprecated_states
);
599 static tree
grokparms (struct c_arg_info
*, bool);
600 static void layout_array_type (tree
);
601 static void warn_defaults_to (location_t
, int, const char *, ...)
602 ATTRIBUTE_GCC_DIAG(3,4);
604 /* T is a statement. Add it to the statement-tree. This is the
605 C/ObjC version--C++ has a slightly different version of this
611 enum tree_code code
= TREE_CODE (t
);
613 if (CAN_HAVE_LOCATION_P (t
) && code
!= LABEL_EXPR
)
615 if (!EXPR_HAS_LOCATION (t
))
616 SET_EXPR_LOCATION (t
, input_location
);
619 if (code
== LABEL_EXPR
|| code
== CASE_LABEL_EXPR
)
620 STATEMENT_LIST_HAS_LABEL (cur_stmt_list
) = 1;
622 /* Add T to the statement-tree. Non-side-effect statements need to be
623 recorded during statement expressions. */
624 if (!building_stmt_list_p ())
626 append_to_statement_list_force (t
, &cur_stmt_list
);
631 /* Build a pointer type using the default pointer mode. */
634 c_build_pointer_type (tree to_type
)
636 addr_space_t as
= to_type
== error_mark_node
? ADDR_SPACE_GENERIC
637 : TYPE_ADDR_SPACE (to_type
);
638 machine_mode pointer_mode
;
640 if (as
!= ADDR_SPACE_GENERIC
|| c_default_pointer_mode
== VOIDmode
)
641 pointer_mode
= targetm
.addr_space
.pointer_mode (as
);
643 pointer_mode
= c_default_pointer_mode
;
644 return build_pointer_type_for_mode (to_type
, pointer_mode
, false);
648 /* Return true if we will want to say something if a goto statement
652 decl_jump_unsafe (tree decl
)
654 if (error_operand_p (decl
))
657 /* Always warn about crossing variably modified types. */
658 if ((VAR_P (decl
) || TREE_CODE (decl
) == TYPE_DECL
)
659 && variably_modified_type_p (TREE_TYPE (decl
), NULL_TREE
))
662 /* Otherwise, only warn if -Wgoto-misses-init and this is an
663 initialized automatic decl. */
664 if (warn_jump_misses_init
666 && !TREE_STATIC (decl
)
667 && DECL_INITIAL (decl
) != NULL_TREE
)
675 c_print_identifier (FILE *file
, tree node
, int indent
)
677 void (*save
) (enum c_oracle_request
, tree identifier
);
679 /* Temporarily hide any binding oracle. Without this, calls to
680 debug_tree from the debugger will end up calling into the oracle,
681 making for a confusing debug session. As the oracle isn't needed
682 here for normal operation, it's simplest to suppress it. */
683 save
= c_binding_oracle
;
684 c_binding_oracle
= NULL
;
686 print_node (file
, "symbol", I_SYMBOL_DECL (node
), indent
+ 4);
687 print_node (file
, "tag", I_TAG_DECL (node
), indent
+ 4);
688 print_node (file
, "label", I_LABEL_DECL (node
), indent
+ 4);
689 if (C_IS_RESERVED_WORD (node
) && C_RID_CODE (node
) != RID_CXX_COMPAT_WARN
)
691 tree rid
= ridpointers
[C_RID_CODE (node
)];
692 indent_to (file
, indent
+ 4);
693 fprintf (file
, "rid " HOST_PTR_PRINTF
" \"%s\"",
694 (void *) rid
, IDENTIFIER_POINTER (rid
));
697 c_binding_oracle
= save
;
700 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
701 which may be any of several kinds of DECL or TYPE or error_mark_node,
702 in the scope SCOPE. */
704 bind (tree name
, tree decl
, struct c_scope
*scope
, bool invisible
,
705 bool nested
, location_t locus
)
707 struct c_binding
*b
, **here
;
709 if (binding_freelist
)
711 b
= binding_freelist
;
712 binding_freelist
= b
->prev
;
715 b
= ggc_alloc
<c_binding
> ();
720 b
->depth
= scope
->depth
;
721 b
->invisible
= invisible
;
729 b
->prev
= scope
->bindings
;
732 if (decl_jump_unsafe (decl
))
733 scope
->has_jump_unsafe_decl
= 1;
738 switch (TREE_CODE (decl
))
740 case LABEL_DECL
: here
= &I_LABEL_BINDING (name
); break;
743 case RECORD_TYPE
: here
= &I_TAG_BINDING (name
); break;
749 case ERROR_MARK
: here
= &I_SYMBOL_BINDING (name
); break;
755 /* Locate the appropriate place in the chain of shadowed decls
756 to insert this binding. Normally, scope == current_scope and
757 this does nothing. */
758 while (*here
&& (*here
)->depth
> scope
->depth
)
759 here
= &(*here
)->shadowed
;
765 /* Clear the binding structure B, stick it on the binding_freelist,
766 and return the former value of b->prev. This is used by pop_scope
767 and get_parm_info to iterate destructively over all the bindings
768 from a given scope. */
769 static struct c_binding
*
770 free_binding_and_advance (struct c_binding
*b
)
772 struct c_binding
*prev
= b
->prev
;
774 memset (b
, 0, sizeof (struct c_binding
));
775 b
->prev
= binding_freelist
;
776 binding_freelist
= b
;
781 /* Bind a label. Like bind, but skip fields which aren't used for
782 labels, and add the LABEL_VARS value. */
784 bind_label (tree name
, tree label
, struct c_scope
*scope
,
785 struct c_label_vars
*label_vars
)
789 bind (name
, label
, scope
, /*invisible=*/false, /*nested=*/false,
792 scope
->has_label_bindings
= true;
795 gcc_assert (b
->decl
== label
);
796 label_vars
->shadowed
= b
->u
.label
;
797 b
->u
.label
= label_vars
;
800 /* Hook called at end of compilation to assume 1 elt
801 for a file-scope tentative array defn that wasn't complete before. */
804 c_finish_incomplete_decl (tree decl
)
808 tree type
= TREE_TYPE (decl
);
809 if (type
!= error_mark_node
810 && TREE_CODE (type
) == ARRAY_TYPE
811 && !DECL_EXTERNAL (decl
)
812 && TYPE_DOMAIN (type
) == 0)
814 warning_at (DECL_SOURCE_LOCATION (decl
),
815 0, "array %q+D assumed to have one element", decl
);
817 complete_array_type (&TREE_TYPE (decl
), NULL_TREE
, true);
819 relayout_decl (decl
);
824 /* Record that inline function FUNC contains a reference (location
825 LOC) to static DECL (file-scope or function-local according to
829 record_inline_static (location_t loc
, tree func
, tree decl
,
830 enum c_inline_static_type type
)
832 c_inline_static
*csi
= ggc_alloc
<c_inline_static
> ();
834 csi
->function
= func
;
835 csi
->static_decl
= decl
;
837 csi
->next
= c_inline_statics
;
838 c_inline_statics
= csi
;
841 /* Check for references to static declarations in inline functions at
842 the end of the translation unit and diagnose them if the functions
843 are still inline definitions. */
846 check_inline_statics (void)
848 struct c_inline_static
*csi
;
849 for (csi
= c_inline_statics
; csi
; csi
= csi
->next
)
851 if (DECL_EXTERNAL (csi
->function
))
855 pedwarn (csi
->location
, 0,
856 "%qD is static but used in inline function %qD "
857 "which is not static", csi
->static_decl
, csi
->function
);
860 pedwarn (csi
->location
, 0,
861 "%q+D is static but declared in inline function %qD "
862 "which is not static", csi
->static_decl
, csi
->function
);
868 c_inline_statics
= NULL
;
871 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
872 for the current state, otherwise set it to uninitialized. */
875 set_spot_bindings (struct c_spot_bindings
*p
, bool defining
)
879 p
->scope
= current_scope
;
880 p
->bindings_in_scope
= current_scope
->bindings
;
885 p
->bindings_in_scope
= NULL
;
888 p
->left_stmt_expr
= false;
891 /* Update spot bindings P as we pop out of SCOPE. Return true if we
892 should push decls for a label. */
895 update_spot_bindings (struct c_scope
*scope
, struct c_spot_bindings
*p
)
897 if (p
->scope
!= scope
)
899 /* This label or goto is defined in some other scope, or it is a
900 label which is not yet defined. There is nothing to
905 /* Adjust the spot bindings to refer to the bindings already defined
906 in the enclosing scope. */
907 p
->scope
= scope
->outer
;
908 p
->bindings_in_scope
= p
->scope
->bindings
;
913 /* The Objective-C front-end often needs to determine the current scope. */
916 objc_get_current_scope (void)
918 return current_scope
;
921 /* The following function is used only by Objective-C. It needs to live here
922 because it accesses the innards of c_scope. */
925 objc_mark_locals_volatile (void *enclosing_blk
)
927 struct c_scope
*scope
;
930 for (scope
= current_scope
;
931 scope
&& scope
!= enclosing_blk
;
932 scope
= scope
->outer
)
934 for (b
= scope
->bindings
; b
; b
= b
->prev
)
935 objc_volatilize_decl (b
->decl
);
937 /* Do not climb up past the current function. */
938 if (scope
->function_body
)
943 /* Return true if we are in the global binding level. */
946 global_bindings_p (void)
948 return current_scope
== file_scope
;
952 keep_next_level (void)
954 keep_next_level_flag
= true;
957 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
960 set_float_const_decimal64 (void)
962 current_scope
->float_const_decimal64
= true;
965 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
968 clear_float_const_decimal64 (void)
970 current_scope
->float_const_decimal64
= false;
973 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
976 float_const_decimal64_p (void)
978 return current_scope
->float_const_decimal64
;
981 /* Identify this scope as currently being filled with parameters. */
984 declare_parm_level (void)
986 current_scope
->parm_flag
= true;
992 if (next_is_function_body
)
994 /* This is the transition from the parameters to the top level
995 of the function body. These are the same scope
996 (C99 6.2.1p4,6) so we do not push another scope structure.
997 next_is_function_body is set only by store_parm_decls, which
998 in turn is called when and only when we are about to
999 encounter the opening curly brace for the function body.
1001 The outermost block of a function always gets a BLOCK node,
1002 because the debugging output routines expect that each
1003 function has at least one BLOCK. */
1004 current_scope
->parm_flag
= false;
1005 current_scope
->function_body
= true;
1006 current_scope
->keep
= true;
1007 current_scope
->outer_function
= current_function_scope
;
1008 current_function_scope
= current_scope
;
1010 keep_next_level_flag
= false;
1011 next_is_function_body
= false;
1013 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1014 if (current_scope
->outer
)
1015 current_scope
->float_const_decimal64
1016 = current_scope
->outer
->float_const_decimal64
;
1018 current_scope
->float_const_decimal64
= false;
1022 struct c_scope
*scope
;
1025 scope
= scope_freelist
;
1026 scope_freelist
= scope
->outer
;
1029 scope
= ggc_cleared_alloc
<c_scope
> ();
1031 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1033 scope
->float_const_decimal64
= current_scope
->float_const_decimal64
;
1035 scope
->float_const_decimal64
= false;
1037 scope
->keep
= keep_next_level_flag
;
1038 scope
->outer
= current_scope
;
1039 scope
->depth
= current_scope
? (current_scope
->depth
+ 1) : 0;
1041 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1043 if (current_scope
&& scope
->depth
== 0)
1046 sorry ("GCC supports only %u nested scopes", scope
->depth
);
1049 current_scope
= scope
;
1050 keep_next_level_flag
= false;
1054 /* This is called when we are leaving SCOPE. For each label defined
1055 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1056 These are the decls whose initialization will be skipped by a goto
1057 later in the function. */
1060 update_label_decls (struct c_scope
*scope
)
1067 if (s
->has_label_bindings
)
1069 struct c_binding
*b
;
1071 for (b
= s
->bindings
; b
!= NULL
; b
= b
->prev
)
1073 struct c_label_vars
*label_vars
;
1074 struct c_binding
*b1
;
1077 struct c_goto_bindings
*g
;
1079 if (TREE_CODE (b
->decl
) != LABEL_DECL
)
1081 label_vars
= b
->u
.label
;
1083 b1
= label_vars
->label_bindings
.bindings_in_scope
;
1084 if (label_vars
->label_bindings
.scope
== NULL
)
1087 hjud
= label_vars
->label_bindings
.scope
->has_jump_unsafe_decl
;
1088 if (update_spot_bindings (scope
, &label_vars
->label_bindings
))
1090 /* This label is defined in this scope. */
1093 for (; b1
!= NULL
; b1
= b1
->prev
)
1095 /* A goto from later in the function to this
1096 label will never see the initialization
1097 of B1, if any. Save it to issue a
1098 warning if needed. */
1099 if (decl_jump_unsafe (b1
->decl
))
1100 vec_safe_push(label_vars
->decls_in_scope
, b1
->decl
);
1105 /* Update the bindings of any goto statements associated
1107 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
1108 update_spot_bindings (scope
, &g
->goto_bindings
);
1112 /* Don't search beyond the current function. */
1113 if (s
== current_function_scope
)
1120 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1123 set_type_context (tree type
, tree context
)
1125 for (type
= TYPE_MAIN_VARIANT (type
); type
;
1126 type
= TYPE_NEXT_VARIANT (type
))
1127 TYPE_CONTEXT (type
) = context
;
1130 /* Exit a scope. Restore the state of the identifier-decl mappings
1131 that were in effect when this scope was entered. Return a BLOCK
1132 node containing all the DECLs in this scope that are of interest
1133 to debug info generation. */
1138 struct c_scope
*scope
= current_scope
;
1139 tree block
, context
, p
;
1140 struct c_binding
*b
;
1142 bool functionbody
= scope
->function_body
;
1143 bool keep
= functionbody
|| scope
->keep
|| scope
->bindings
;
1145 update_label_decls (scope
);
1147 /* If appropriate, create a BLOCK to record the decls for the life
1148 of this function. */
1152 block
= make_node (BLOCK
);
1153 BLOCK_SUBBLOCKS (block
) = scope
->blocks
;
1154 TREE_USED (block
) = 1;
1156 /* In each subblock, record that this is its superior. */
1157 for (p
= scope
->blocks
; p
; p
= BLOCK_CHAIN (p
))
1158 BLOCK_SUPERCONTEXT (p
) = block
;
1160 BLOCK_VARS (block
) = 0;
1163 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1164 scope must be set so that they point to the appropriate
1165 construct, i.e. either to the current FUNCTION_DECL node, or
1166 else to the BLOCK node we just constructed.
1168 Note that for tagged types whose scope is just the formal
1169 parameter list for some function type specification, we can't
1170 properly set their TYPE_CONTEXTs here, because we don't have a
1171 pointer to the appropriate FUNCTION_TYPE node readily available
1172 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1173 type nodes get set in `grokdeclarator' as soon as we have created
1174 the FUNCTION_TYPE node which will represent the "scope" for these
1175 "parameter list local" tagged types. */
1176 if (scope
->function_body
)
1177 context
= current_function_decl
;
1178 else if (scope
== file_scope
)
1180 tree file_decl
= build_translation_unit_decl (NULL_TREE
);
1181 context
= file_decl
;
1182 debug_hooks
->register_main_translation_unit (file_decl
);
1187 /* Clear all bindings in this scope. */
1188 for (b
= scope
->bindings
; b
; b
= free_binding_and_advance (b
))
1191 switch (TREE_CODE (p
))
1194 /* Warnings for unused labels, errors for undefined labels. */
1195 if (TREE_USED (p
) && !DECL_INITIAL (p
))
1197 error ("label %q+D used but not defined", p
);
1198 DECL_INITIAL (p
) = error_mark_node
;
1201 warn_for_unused_label (p
);
1203 /* Labels go in BLOCK_VARS. */
1204 DECL_CHAIN (p
) = BLOCK_VARS (block
);
1205 BLOCK_VARS (block
) = p
;
1206 gcc_assert (I_LABEL_BINDING (b
->id
) == b
);
1207 I_LABEL_BINDING (b
->id
) = b
->shadowed
;
1209 /* Also pop back to the shadowed label_vars. */
1210 release_tree_vector (b
->u
.label
->decls_in_scope
);
1211 b
->u
.label
= b
->u
.label
->shadowed
;
1217 set_type_context (p
, context
);
1219 /* Types may not have tag-names, in which case the type
1220 appears in the bindings list with b->id NULL. */
1223 gcc_assert (I_TAG_BINDING (b
->id
) == b
);
1224 I_TAG_BINDING (b
->id
) = b
->shadowed
;
1229 /* Propagate TREE_ADDRESSABLE from nested functions to their
1230 containing functions. */
1231 if (!TREE_ASM_WRITTEN (p
)
1232 && DECL_INITIAL (p
) != 0
1233 && TREE_ADDRESSABLE (p
)
1234 && DECL_ABSTRACT_ORIGIN (p
) != 0
1235 && DECL_ABSTRACT_ORIGIN (p
) != p
)
1236 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p
)) = 1;
1237 if (!DECL_EXTERNAL (p
)
1238 && !DECL_INITIAL (p
)
1239 && scope
!= file_scope
1240 && scope
!= external_scope
)
1242 error ("nested function %q+D declared but never defined", p
);
1243 undef_nested_function
= true;
1245 else if (DECL_DECLARED_INLINE_P (p
)
1247 && !DECL_INITIAL (p
))
1249 /* C99 6.7.4p6: "a function with external linkage... declared
1250 with an inline function specifier ... shall also be defined
1251 in the same translation unit." */
1252 if (!flag_gnu89_inline
1253 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p
))
1254 && scope
!= external_scope
)
1255 pedwarn (input_location
, 0,
1256 "inline function %q+D declared but never defined", p
);
1257 DECL_EXTERNAL (p
) = 1;
1263 /* Warnings for unused variables. */
1264 if ((!TREE_USED (p
) || !DECL_READ_P (p
))
1265 && !TREE_NO_WARNING (p
)
1266 && !DECL_IN_SYSTEM_HEADER (p
)
1268 && !DECL_ARTIFICIAL (p
)
1269 && scope
!= file_scope
1270 && scope
!= external_scope
)
1273 warning (OPT_Wunused_variable
, "unused variable %q+D", p
);
1274 else if (DECL_CONTEXT (p
) == current_function_decl
)
1275 warning_at (DECL_SOURCE_LOCATION (p
),
1276 OPT_Wunused_but_set_variable
,
1277 "variable %qD set but not used", p
);
1282 error ("type of array %q+D completed incompatibly with"
1283 " implicit initialization", p
);
1290 /* All of these go in BLOCK_VARS, but only if this is the
1291 binding in the home scope. */
1294 DECL_CHAIN (p
) = BLOCK_VARS (block
);
1295 BLOCK_VARS (block
) = p
;
1297 else if (VAR_OR_FUNCTION_DECL_P (p
) && scope
!= file_scope
)
1299 /* For block local externs add a special
1300 DECL_EXTERNAL decl for debug info generation. */
1301 tree extp
= copy_node (p
);
1303 DECL_EXTERNAL (extp
) = 1;
1304 TREE_STATIC (extp
) = 0;
1305 TREE_PUBLIC (extp
) = 1;
1306 DECL_INITIAL (extp
) = NULL_TREE
;
1307 DECL_LANG_SPECIFIC (extp
) = NULL
;
1308 DECL_CONTEXT (extp
) = current_function_decl
;
1309 if (TREE_CODE (p
) == FUNCTION_DECL
)
1311 DECL_RESULT (extp
) = NULL_TREE
;
1312 DECL_SAVED_TREE (extp
) = NULL_TREE
;
1313 DECL_STRUCT_FUNCTION (extp
) = NULL
;
1315 if (b
->locus
!= UNKNOWN_LOCATION
)
1316 DECL_SOURCE_LOCATION (extp
) = b
->locus
;
1317 DECL_CHAIN (extp
) = BLOCK_VARS (block
);
1318 BLOCK_VARS (block
) = extp
;
1320 /* If this is the file scope set DECL_CONTEXT of each decl to
1321 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1323 if (scope
== file_scope
)
1325 DECL_CONTEXT (p
) = context
;
1326 if (TREE_CODE (p
) == TYPE_DECL
1327 && TREE_TYPE (p
) != error_mark_node
)
1328 set_type_context (TREE_TYPE (p
), context
);
1332 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1333 already been put there by store_parm_decls. Unused-
1334 parameter warnings are handled by function.c.
1335 error_mark_node obviously does not go in BLOCK_VARS and
1336 does not get unused-variable warnings. */
1339 /* It is possible for a decl not to have a name. We get
1340 here with b->id NULL in this case. */
1343 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
1344 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
1345 if (b
->shadowed
&& b
->shadowed
->u
.type
)
1346 TREE_TYPE (b
->shadowed
->decl
) = b
->shadowed
->u
.type
;
1356 /* Dispose of the block that we just made inside some higher level. */
1357 if ((scope
->function_body
|| scope
== file_scope
) && context
)
1359 DECL_INITIAL (context
) = block
;
1360 BLOCK_SUPERCONTEXT (block
) = context
;
1362 else if (scope
->outer
)
1365 SCOPE_LIST_APPEND (scope
->outer
, blocks
, block
);
1366 /* If we did not make a block for the scope just exited, any
1367 blocks made for inner scopes must be carried forward so they
1368 will later become subblocks of something else. */
1369 else if (scope
->blocks
)
1370 SCOPE_LIST_CONCAT (scope
->outer
, blocks
, scope
, blocks
);
1373 /* Pop the current scope, and free the structure for reuse. */
1374 current_scope
= scope
->outer
;
1375 if (scope
->function_body
)
1376 current_function_scope
= scope
->outer_function
;
1378 memset (scope
, 0, sizeof (struct c_scope
));
1379 scope
->outer
= scope_freelist
;
1380 scope_freelist
= scope
;
1386 push_file_scope (void)
1394 file_scope
= current_scope
;
1396 start_fname_decls ();
1398 for (decl
= visible_builtins
; decl
; decl
= DECL_CHAIN (decl
))
1399 bind (DECL_NAME (decl
), decl
, file_scope
,
1400 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl
));
1404 pop_file_scope (void)
1406 /* In case there were missing closebraces, get us back to the global
1408 while (current_scope
!= file_scope
)
1411 /* __FUNCTION__ is defined at file scope (""). This
1412 call may not be necessary as my tests indicate it
1413 still works without it. */
1414 finish_fname_decls ();
1416 check_inline_statics ();
1418 /* This is the point to write out a PCH if we're doing that.
1419 In that case we do not want to do anything else. */
1422 c_common_write_pch ();
1426 /* Pop off the file scope and close this translation unit. */
1430 maybe_apply_pending_pragma_weaks ();
1433 /* Adjust the bindings for the start of a statement expression. */
1436 c_bindings_start_stmt_expr (struct c_spot_bindings
* switch_bindings
)
1438 struct c_scope
*scope
;
1440 for (scope
= current_scope
; scope
!= NULL
; scope
= scope
->outer
)
1442 struct c_binding
*b
;
1444 if (!scope
->has_label_bindings
)
1447 for (b
= scope
->bindings
; b
!= NULL
; b
= b
->prev
)
1449 struct c_label_vars
*label_vars
;
1451 struct c_goto_bindings
*g
;
1453 if (TREE_CODE (b
->decl
) != LABEL_DECL
)
1455 label_vars
= b
->u
.label
;
1456 ++label_vars
->label_bindings
.stmt_exprs
;
1457 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
1458 ++g
->goto_bindings
.stmt_exprs
;
1462 if (switch_bindings
!= NULL
)
1463 ++switch_bindings
->stmt_exprs
;
1466 /* Adjust the bindings for the end of a statement expression. */
1469 c_bindings_end_stmt_expr (struct c_spot_bindings
*switch_bindings
)
1471 struct c_scope
*scope
;
1473 for (scope
= current_scope
; scope
!= NULL
; scope
= scope
->outer
)
1475 struct c_binding
*b
;
1477 if (!scope
->has_label_bindings
)
1480 for (b
= scope
->bindings
; b
!= NULL
; b
= b
->prev
)
1482 struct c_label_vars
*label_vars
;
1484 struct c_goto_bindings
*g
;
1486 if (TREE_CODE (b
->decl
) != LABEL_DECL
)
1488 label_vars
= b
->u
.label
;
1489 --label_vars
->label_bindings
.stmt_exprs
;
1490 if (label_vars
->label_bindings
.stmt_exprs
< 0)
1492 label_vars
->label_bindings
.left_stmt_expr
= true;
1493 label_vars
->label_bindings
.stmt_exprs
= 0;
1495 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
1497 --g
->goto_bindings
.stmt_exprs
;
1498 if (g
->goto_bindings
.stmt_exprs
< 0)
1500 g
->goto_bindings
.left_stmt_expr
= true;
1501 g
->goto_bindings
.stmt_exprs
= 0;
1507 if (switch_bindings
!= NULL
)
1509 --switch_bindings
->stmt_exprs
;
1510 gcc_assert (switch_bindings
->stmt_exprs
>= 0);
1514 /* Push a definition or a declaration of struct, union or enum tag "name".
1515 "type" should be the type node.
1516 We assume that the tag "name" is not already defined, and has a location
1519 Note that the definition may really be just a forward reference.
1520 In that case, the TYPE_SIZE will be zero. */
1523 pushtag (location_t loc
, tree name
, tree type
)
1525 /* Record the identifier as the type's name if it has none. */
1526 if (name
&& !TYPE_NAME (type
))
1527 TYPE_NAME (type
) = name
;
1528 bind (name
, type
, current_scope
, /*invisible=*/false, /*nested=*/false, loc
);
1530 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1531 tagged type we just added to the current scope. This fake
1532 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1533 to output a representation of a tagged type, and it also gives
1534 us a convenient place to record the "scope start" address for the
1537 TYPE_STUB_DECL (type
) = pushdecl (build_decl (loc
,
1538 TYPE_DECL
, NULL_TREE
, type
));
1540 /* An approximation for now, so we can tell this is a function-scope tag.
1541 This will be updated in pop_scope. */
1542 TYPE_CONTEXT (type
) = DECL_CONTEXT (TYPE_STUB_DECL (type
));
1544 if (warn_cxx_compat
&& name
!= NULL_TREE
)
1546 struct c_binding
*b
= I_SYMBOL_BINDING (name
);
1549 && b
->decl
!= NULL_TREE
1550 && TREE_CODE (b
->decl
) == TYPE_DECL
1551 && (B_IN_CURRENT_SCOPE (b
)
1552 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
1553 && (TYPE_MAIN_VARIANT (TREE_TYPE (b
->decl
))
1554 != TYPE_MAIN_VARIANT (type
)))
1556 warning_at (loc
, OPT_Wc___compat
,
1557 ("using %qD as both a typedef and a tag is "
1560 if (b
->locus
!= UNKNOWN_LOCATION
)
1561 inform (b
->locus
, "originally defined here");
1566 /* An exported interface to pushtag. This is used by the gdb plugin's
1567 binding oracle to introduce a new tag binding. */
1570 c_pushtag (location_t loc
, tree name
, tree type
)
1572 pushtag (loc
, name
, type
);
1575 /* An exported interface to bind a declaration. LOC is the location
1576 to use. DECL is the declaration to bind. The decl's name is used
1577 to determine how it is bound. If DECL is a VAR_DECL, then
1578 IS_GLOBAL determines whether the decl is put into the global (file
1579 and external) scope or the current function's scope; if DECL is not
1580 a VAR_DECL then it is always put into the file scope. */
1583 c_bind (location_t loc
, tree decl
, bool is_global
)
1585 struct c_scope
*scope
;
1586 bool nested
= false;
1588 if (!VAR_P (decl
) || current_function_scope
== NULL
)
1590 /* Types and functions are always considered to be global. */
1592 DECL_EXTERNAL (decl
) = 1;
1593 TREE_PUBLIC (decl
) = 1;
1597 /* Also bind it into the external scope. */
1598 bind (DECL_NAME (decl
), decl
, external_scope
, true, false, loc
);
1601 DECL_EXTERNAL (decl
) = 1;
1602 TREE_PUBLIC (decl
) = 1;
1606 DECL_CONTEXT (decl
) = current_function_decl
;
1607 TREE_PUBLIC (decl
) = 0;
1608 scope
= current_function_scope
;
1611 bind (DECL_NAME (decl
), decl
, scope
, false, nested
, loc
);
1614 /* Subroutine of compare_decls. Allow harmless mismatches in return
1615 and argument types provided that the type modes match. This function
1616 return a unified type given a suitable match, and 0 otherwise. */
1619 match_builtin_function_types (tree newtype
, tree oldtype
)
1621 tree newrettype
, oldrettype
;
1622 tree newargs
, oldargs
;
1623 tree trytype
, tryargs
;
1625 /* Accept the return type of the new declaration if same modes. */
1626 oldrettype
= TREE_TYPE (oldtype
);
1627 newrettype
= TREE_TYPE (newtype
);
1629 if (TYPE_MODE (oldrettype
) != TYPE_MODE (newrettype
))
1632 oldargs
= TYPE_ARG_TYPES (oldtype
);
1633 newargs
= TYPE_ARG_TYPES (newtype
);
1636 while (oldargs
|| newargs
)
1640 || !TREE_VALUE (oldargs
)
1641 || !TREE_VALUE (newargs
)
1642 || TYPE_MODE (TREE_VALUE (oldargs
))
1643 != TYPE_MODE (TREE_VALUE (newargs
)))
1646 oldargs
= TREE_CHAIN (oldargs
);
1647 newargs
= TREE_CHAIN (newargs
);
1650 trytype
= build_function_type (newrettype
, tryargs
);
1652 /* Allow declaration to change transaction_safe attribute. */
1653 tree oldattrs
= TYPE_ATTRIBUTES (oldtype
);
1654 tree oldtsafe
= lookup_attribute ("transaction_safe", oldattrs
);
1655 tree newattrs
= TYPE_ATTRIBUTES (newtype
);
1656 tree newtsafe
= lookup_attribute ("transaction_safe", newattrs
);
1657 if (oldtsafe
&& !newtsafe
)
1658 oldattrs
= remove_attribute ("transaction_safe", oldattrs
);
1659 else if (newtsafe
&& !oldtsafe
)
1660 oldattrs
= tree_cons (get_identifier ("transaction_safe"),
1661 NULL_TREE
, oldattrs
);
1663 return build_type_attribute_variant (trytype
, oldattrs
);
1666 /* Subroutine of diagnose_mismatched_decls. Check for function type
1667 mismatch involving an empty arglist vs a nonempty one and give clearer
1670 diagnose_arglist_conflict (tree newdecl
, tree olddecl
,
1671 tree newtype
, tree oldtype
)
1675 if (TREE_CODE (olddecl
) != FUNCTION_DECL
1676 || !comptypes (TREE_TYPE (oldtype
), TREE_TYPE (newtype
))
1677 || !((!prototype_p (oldtype
) && DECL_INITIAL (olddecl
) == 0)
1678 || (!prototype_p (newtype
) && DECL_INITIAL (newdecl
) == 0)))
1681 t
= TYPE_ARG_TYPES (oldtype
);
1683 t
= TYPE_ARG_TYPES (newtype
);
1684 for (; t
; t
= TREE_CHAIN (t
))
1686 tree type
= TREE_VALUE (t
);
1688 if (TREE_CHAIN (t
) == 0
1689 && TYPE_MAIN_VARIANT (type
) != void_type_node
)
1691 inform (input_location
, "a parameter list with an ellipsis can%'t match "
1692 "an empty parameter name list declaration");
1696 if (c_type_promotes_to (type
) != type
)
1698 inform (input_location
, "an argument type that has a default promotion can%'t match "
1699 "an empty parameter name list declaration");
1705 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1706 old-style function definition, NEWDECL is a prototype declaration.
1707 Diagnose inconsistencies in the argument list. Returns TRUE if
1708 the prototype is compatible, FALSE if not. */
1710 validate_proto_after_old_defn (tree newdecl
, tree newtype
, tree oldtype
)
1712 tree newargs
, oldargs
;
1715 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1717 oldargs
= TYPE_ACTUAL_ARG_TYPES (oldtype
);
1718 newargs
= TYPE_ARG_TYPES (newtype
);
1723 tree oldargtype
= TREE_VALUE (oldargs
);
1724 tree newargtype
= TREE_VALUE (newargs
);
1726 if (oldargtype
== error_mark_node
|| newargtype
== error_mark_node
)
1729 oldargtype
= (TYPE_ATOMIC (oldargtype
)
1730 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype
),
1732 : TYPE_MAIN_VARIANT (oldargtype
));
1733 newargtype
= (TYPE_ATOMIC (newargtype
)
1734 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype
),
1736 : TYPE_MAIN_VARIANT (newargtype
));
1738 if (END_OF_ARGLIST (oldargtype
) && END_OF_ARGLIST (newargtype
))
1741 /* Reaching the end of just one list means the two decls don't
1742 agree on the number of arguments. */
1743 if (END_OF_ARGLIST (oldargtype
))
1745 error ("prototype for %q+D declares more arguments "
1746 "than previous old-style definition", newdecl
);
1749 else if (END_OF_ARGLIST (newargtype
))
1751 error ("prototype for %q+D declares fewer arguments "
1752 "than previous old-style definition", newdecl
);
1756 /* Type for passing arg must be consistent with that declared
1758 else if (!comptypes (oldargtype
, newargtype
))
1760 error ("prototype for %q+D declares argument %d"
1761 " with incompatible type",
1766 oldargs
= TREE_CHAIN (oldargs
);
1767 newargs
= TREE_CHAIN (newargs
);
1771 /* If we get here, no errors were found, but do issue a warning
1772 for this poor-style construct. */
1773 warning (0, "prototype for %q+D follows non-prototype definition",
1776 #undef END_OF_ARGLIST
1779 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1780 first in a pair of mismatched declarations, using the diagnostic
1783 locate_old_decl (tree decl
)
1785 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (decl
)
1786 && !C_DECL_DECLARED_BUILTIN (decl
))
1788 else if (DECL_INITIAL (decl
))
1789 inform (input_location
, "previous definition of %q+D was here", decl
);
1790 else if (C_DECL_IMPLICIT (decl
))
1791 inform (input_location
, "previous implicit declaration of %q+D was here", decl
);
1793 inform (input_location
, "previous declaration of %q+D was here", decl
);
1796 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1797 Returns true if the caller should proceed to merge the two, false
1798 if OLDDECL should simply be discarded. As a side effect, issues
1799 all necessary diagnostics for invalid or poor-style combinations.
1800 If it returns true, writes the types of NEWDECL and OLDDECL to
1801 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1802 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1805 diagnose_mismatched_decls (tree newdecl
, tree olddecl
,
1806 tree
*newtypep
, tree
*oldtypep
)
1808 tree newtype
, oldtype
;
1809 bool pedwarned
= false;
1810 bool warned
= false;
1813 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1814 && DECL_EXTERNAL (DECL))
1816 /* If we have error_mark_node for either decl or type, just discard
1817 the previous decl - we're in an error cascade already. */
1818 if (olddecl
== error_mark_node
|| newdecl
== error_mark_node
)
1820 *oldtypep
= oldtype
= TREE_TYPE (olddecl
);
1821 *newtypep
= newtype
= TREE_TYPE (newdecl
);
1822 if (oldtype
== error_mark_node
|| newtype
== error_mark_node
)
1825 /* Two different categories of symbol altogether. This is an error
1826 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1827 if (TREE_CODE (olddecl
) != TREE_CODE (newdecl
))
1829 if (!(TREE_CODE (olddecl
) == FUNCTION_DECL
1830 && DECL_BUILT_IN (olddecl
)
1831 && !C_DECL_DECLARED_BUILTIN (olddecl
)))
1833 error ("%q+D redeclared as different kind of symbol", newdecl
);
1834 locate_old_decl (olddecl
);
1836 else if (TREE_PUBLIC (newdecl
))
1837 warning (0, "built-in function %q+D declared as non-function",
1840 warning (OPT_Wshadow
, "declaration of %q+D shadows "
1841 "a built-in function", newdecl
);
1845 /* Enumerators have no linkage, so may only be declared once in a
1847 if (TREE_CODE (olddecl
) == CONST_DECL
)
1849 error ("redeclaration of enumerator %q+D", newdecl
);
1850 locate_old_decl (olddecl
);
1854 if (!comptypes (oldtype
, newtype
))
1856 if (TREE_CODE (olddecl
) == FUNCTION_DECL
1857 && DECL_BUILT_IN (olddecl
) && !C_DECL_DECLARED_BUILTIN (olddecl
))
1859 /* Accept harmless mismatch in function types.
1860 This is for the ffs and fprintf builtins. */
1861 tree trytype
= match_builtin_function_types (newtype
, oldtype
);
1863 if (trytype
&& comptypes (newtype
, trytype
))
1864 *oldtypep
= oldtype
= trytype
;
1867 /* If types don't match for a built-in, throw away the
1868 built-in. No point in calling locate_old_decl here, it
1869 won't print anything. */
1870 warning (0, "conflicting types for built-in function %q+D",
1875 else if (TREE_CODE (olddecl
) == FUNCTION_DECL
1876 && DECL_IS_BUILTIN (olddecl
))
1878 /* A conflicting function declaration for a predeclared
1879 function that isn't actually built in. Objective C uses
1880 these. The new declaration silently overrides everything
1881 but the volatility (i.e. noreturn) indication. See also
1882 below. FIXME: Make Objective C use normal builtins. */
1883 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
1886 /* Permit void foo (...) to match int foo (...) if the latter is
1887 the definition and implicit int was used. See
1888 c-torture/compile/920625-2.c. */
1889 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
&& DECL_INITIAL (newdecl
)
1890 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype
)) == void_type_node
1891 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype
)) == integer_type_node
1892 && C_FUNCTION_IMPLICIT_INT (newdecl
) && !DECL_INITIAL (olddecl
))
1894 pedwarned
= pedwarn (input_location
, 0,
1895 "conflicting types for %q+D", newdecl
);
1896 /* Make sure we keep void as the return type. */
1897 TREE_TYPE (newdecl
) = *newtypep
= newtype
= oldtype
;
1898 C_FUNCTION_IMPLICIT_INT (newdecl
) = 0;
1900 /* Permit void foo (...) to match an earlier call to foo (...) with
1901 no declared type (thus, implicitly int). */
1902 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
1903 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype
)) == void_type_node
1904 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype
)) == integer_type_node
1905 && C_DECL_IMPLICIT (olddecl
) && !DECL_INITIAL (olddecl
))
1907 pedwarned
= pedwarn (input_location
, 0,
1908 "conflicting types for %q+D", newdecl
);
1909 /* Make sure we keep void as the return type. */
1910 TREE_TYPE (olddecl
) = *oldtypep
= oldtype
= newtype
;
1914 int new_quals
= TYPE_QUALS (newtype
);
1915 int old_quals
= TYPE_QUALS (oldtype
);
1917 if (new_quals
!= old_quals
)
1919 addr_space_t new_addr
= DECODE_QUAL_ADDR_SPACE (new_quals
);
1920 addr_space_t old_addr
= DECODE_QUAL_ADDR_SPACE (old_quals
);
1921 if (new_addr
!= old_addr
)
1923 if (ADDR_SPACE_GENERIC_P (new_addr
))
1924 error ("conflicting named address spaces (generic vs %s) "
1926 c_addr_space_name (old_addr
), newdecl
);
1927 else if (ADDR_SPACE_GENERIC_P (old_addr
))
1928 error ("conflicting named address spaces (%s vs generic) "
1930 c_addr_space_name (new_addr
), newdecl
);
1932 error ("conflicting named address spaces (%s vs %s) "
1934 c_addr_space_name (new_addr
),
1935 c_addr_space_name (old_addr
),
1939 if (CLEAR_QUAL_ADDR_SPACE (new_quals
)
1940 != CLEAR_QUAL_ADDR_SPACE (old_quals
))
1941 error ("conflicting type qualifiers for %q+D", newdecl
);
1944 error ("conflicting types for %q+D", newdecl
);
1945 diagnose_arglist_conflict (newdecl
, olddecl
, newtype
, oldtype
);
1946 locate_old_decl (olddecl
);
1951 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1952 but silently ignore the redeclaration if either is in a system
1953 header. (Conflicting redeclarations were handled above.) This
1954 is allowed for C11 if the types are the same, not just
1956 if (TREE_CODE (newdecl
) == TYPE_DECL
)
1958 bool types_different
= false;
1959 int comptypes_result
;
1962 = comptypes_check_different_types (oldtype
, newtype
, &types_different
);
1964 if (comptypes_result
!= 1 || types_different
)
1966 error ("redefinition of typedef %q+D with different type", newdecl
);
1967 locate_old_decl (olddecl
);
1971 if (DECL_IN_SYSTEM_HEADER (newdecl
)
1972 || DECL_IN_SYSTEM_HEADER (olddecl
)
1973 || TREE_NO_WARNING (newdecl
)
1974 || TREE_NO_WARNING (olddecl
))
1975 return true; /* Allow OLDDECL to continue in use. */
1977 if (variably_modified_type_p (newtype
, NULL
))
1979 error ("redefinition of typedef %q+D with variably modified type",
1981 locate_old_decl (olddecl
);
1983 else if (pedwarn_c99 (input_location
, OPT_Wpedantic
,
1984 "redefinition of typedef %q+D", newdecl
))
1985 locate_old_decl (olddecl
);
1990 /* Function declarations can either be 'static' or 'extern' (no
1991 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1992 can never conflict with each other on account of linkage
1993 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1994 gnu89 mode permits two definitions if one is 'extern inline' and
1995 one is not. The non- extern-inline definition supersedes the
1996 extern-inline definition. */
1998 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2000 /* If you declare a built-in function name as static, or
2001 define the built-in with an old-style definition (so we
2002 can't validate the argument list) the built-in definition is
2003 overridden, but optionally warn this was a bad choice of name. */
2004 if (DECL_BUILT_IN (olddecl
)
2005 && !C_DECL_DECLARED_BUILTIN (olddecl
)
2006 && (!TREE_PUBLIC (newdecl
)
2007 || (DECL_INITIAL (newdecl
)
2008 && !prototype_p (TREE_TYPE (newdecl
)))))
2010 warning (OPT_Wshadow
, "declaration of %q+D shadows "
2011 "a built-in function", newdecl
);
2012 /* Discard the old built-in function. */
2016 if (DECL_INITIAL (newdecl
))
2018 if (DECL_INITIAL (olddecl
))
2020 /* If both decls are in the same TU and the new declaration
2021 isn't overriding an extern inline reject the new decl.
2022 In c99, no overriding is allowed in the same translation
2024 if ((!DECL_EXTERN_INLINE (olddecl
)
2025 || DECL_EXTERN_INLINE (newdecl
)
2026 || (!flag_gnu89_inline
2027 && (!DECL_DECLARED_INLINE_P (olddecl
)
2028 || !lookup_attribute ("gnu_inline",
2029 DECL_ATTRIBUTES (olddecl
)))
2030 && (!DECL_DECLARED_INLINE_P (newdecl
)
2031 || !lookup_attribute ("gnu_inline",
2032 DECL_ATTRIBUTES (newdecl
))))
2034 && same_translation_unit_p (newdecl
, olddecl
))
2036 error ("redefinition of %q+D", newdecl
);
2037 locate_old_decl (olddecl
);
2042 /* If we have a prototype after an old-style function definition,
2043 the argument types must be checked specially. */
2044 else if (DECL_INITIAL (olddecl
)
2045 && !prototype_p (oldtype
) && prototype_p (newtype
)
2046 && TYPE_ACTUAL_ARG_TYPES (oldtype
)
2047 && !validate_proto_after_old_defn (newdecl
, newtype
, oldtype
))
2049 locate_old_decl (olddecl
);
2052 /* A non-static declaration (even an "extern") followed by a
2053 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2054 The same is true for a static forward declaration at block
2055 scope followed by a non-static declaration/definition at file
2056 scope. Static followed by non-static at the same scope is
2057 not undefined behavior, and is the most convenient way to get
2058 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2059 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2060 we do diagnose it if -Wtraditional. */
2061 if (TREE_PUBLIC (olddecl
) && !TREE_PUBLIC (newdecl
))
2063 /* Two exceptions to the rule. If olddecl is an extern
2064 inline, or a predeclared function that isn't actually
2065 built in, newdecl silently overrides olddecl. The latter
2066 occur only in Objective C; see also above. (FIXME: Make
2067 Objective C use normal builtins.) */
2068 if (!DECL_IS_BUILTIN (olddecl
)
2069 && !DECL_EXTERN_INLINE (olddecl
))
2071 error ("static declaration of %q+D follows "
2072 "non-static declaration", newdecl
);
2073 locate_old_decl (olddecl
);
2077 else if (TREE_PUBLIC (newdecl
) && !TREE_PUBLIC (olddecl
))
2079 if (DECL_CONTEXT (olddecl
))
2081 error ("non-static declaration of %q+D follows "
2082 "static declaration", newdecl
);
2083 locate_old_decl (olddecl
);
2086 else if (warn_traditional
)
2088 warned
|= warning (OPT_Wtraditional
,
2089 "non-static declaration of %q+D "
2090 "follows static declaration", newdecl
);
2094 /* Make sure gnu_inline attribute is either not present, or
2095 present on all inline decls. */
2096 if (DECL_DECLARED_INLINE_P (olddecl
)
2097 && DECL_DECLARED_INLINE_P (newdecl
))
2099 bool newa
= lookup_attribute ("gnu_inline",
2100 DECL_ATTRIBUTES (newdecl
)) != NULL
;
2101 bool olda
= lookup_attribute ("gnu_inline",
2102 DECL_ATTRIBUTES (olddecl
)) != NULL
;
2105 error_at (input_location
, "%<gnu_inline%> attribute present on %q+D",
2106 newa
? newdecl
: olddecl
);
2107 error_at (DECL_SOURCE_LOCATION (newa
? olddecl
: newdecl
),
2112 else if (VAR_P (newdecl
))
2114 /* Only variables can be thread-local, and all declarations must
2115 agree on this property. */
2116 if (C_DECL_THREADPRIVATE_P (olddecl
) && !DECL_THREAD_LOCAL_P (newdecl
))
2118 /* Nothing to check. Since OLDDECL is marked threadprivate
2119 and NEWDECL does not have a thread-local attribute, we
2120 will merge the threadprivate attribute into NEWDECL. */
2123 else if (DECL_THREAD_LOCAL_P (newdecl
) != DECL_THREAD_LOCAL_P (olddecl
))
2125 if (DECL_THREAD_LOCAL_P (newdecl
))
2126 error ("thread-local declaration of %q+D follows "
2127 "non-thread-local declaration", newdecl
);
2129 error ("non-thread-local declaration of %q+D follows "
2130 "thread-local declaration", newdecl
);
2132 locate_old_decl (olddecl
);
2136 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2137 if (DECL_INITIAL (newdecl
) && DECL_INITIAL (olddecl
))
2139 error ("redefinition of %q+D", newdecl
);
2140 locate_old_decl (olddecl
);
2144 /* Objects declared at file scope: if the first declaration had
2145 external linkage (even if it was an external reference) the
2146 second must have external linkage as well, or the behavior is
2147 undefined. If the first declaration had internal linkage, then
2148 the second must too, or else be an external reference (in which
2149 case the composite declaration still has internal linkage).
2150 As for function declarations, we warn about the static-then-
2151 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2152 if (DECL_FILE_SCOPE_P (newdecl
)
2153 && TREE_PUBLIC (newdecl
) != TREE_PUBLIC (olddecl
))
2155 if (DECL_EXTERNAL (newdecl
))
2157 if (!DECL_FILE_SCOPE_P (olddecl
))
2159 error ("extern declaration of %q+D follows "
2160 "declaration with no linkage", newdecl
);
2161 locate_old_decl (olddecl
);
2164 else if (warn_traditional
)
2166 warned
|= warning (OPT_Wtraditional
,
2167 "non-static declaration of %q+D "
2168 "follows static declaration", newdecl
);
2173 if (TREE_PUBLIC (newdecl
))
2174 error ("non-static declaration of %q+D follows "
2175 "static declaration", newdecl
);
2177 error ("static declaration of %q+D follows "
2178 "non-static declaration", newdecl
);
2180 locate_old_decl (olddecl
);
2184 /* Two objects with the same name declared at the same block
2185 scope must both be external references (6.7p3). */
2186 else if (!DECL_FILE_SCOPE_P (newdecl
))
2188 if (DECL_EXTERNAL (newdecl
))
2190 /* Extern with initializer at block scope, which will
2191 already have received an error. */
2193 else if (DECL_EXTERNAL (olddecl
))
2195 error ("declaration of %q+D with no linkage follows "
2196 "extern declaration", newdecl
);
2197 locate_old_decl (olddecl
);
2201 error ("redeclaration of %q+D with no linkage", newdecl
);
2202 locate_old_decl (olddecl
);
2208 /* C++ does not permit a decl to appear multiple times at file
2211 && DECL_FILE_SCOPE_P (newdecl
)
2212 && !DECL_EXTERNAL (newdecl
)
2213 && !DECL_EXTERNAL (olddecl
))
2214 warned
|= warning_at (DECL_SOURCE_LOCATION (newdecl
),
2216 ("duplicate declaration of %qD is "
2222 /* All decls must agree on a visibility. */
2223 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl
), TS_DECL_WITH_VIS
)
2224 && DECL_VISIBILITY_SPECIFIED (newdecl
) && DECL_VISIBILITY_SPECIFIED (olddecl
)
2225 && DECL_VISIBILITY (newdecl
) != DECL_VISIBILITY (olddecl
))
2227 warned
|= warning (0, "redeclaration of %q+D with different visibility "
2228 "(old visibility preserved)", newdecl
);
2231 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2232 warned
|= diagnose_mismatched_attributes (olddecl
, newdecl
);
2233 else /* PARM_DECL, VAR_DECL */
2235 /* Redeclaration of a parameter is a constraint violation (this is
2236 not explicitly stated, but follows from C99 6.7p3 [no more than
2237 one declaration of the same identifier with no linkage in the
2238 same scope, except type tags] and 6.2.2p6 [parameters have no
2239 linkage]). We must check for a forward parameter declaration,
2240 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2241 an extension, the mandatory diagnostic for which is handled by
2242 mark_forward_parm_decls. */
2244 if (TREE_CODE (newdecl
) == PARM_DECL
2245 && (!TREE_ASM_WRITTEN (olddecl
) || TREE_ASM_WRITTEN (newdecl
)))
2247 error ("redefinition of parameter %q+D", newdecl
);
2248 locate_old_decl (olddecl
);
2253 /* Optional warning for completely redundant decls. */
2254 if (!warned
&& !pedwarned
2255 && warn_redundant_decls
2256 /* Don't warn about a function declaration followed by a
2258 && !(TREE_CODE (newdecl
) == FUNCTION_DECL
2259 && DECL_INITIAL (newdecl
) && !DECL_INITIAL (olddecl
))
2260 /* Don't warn about redundant redeclarations of builtins. */
2261 && !(TREE_CODE (newdecl
) == FUNCTION_DECL
2262 && !DECL_BUILT_IN (newdecl
)
2263 && DECL_BUILT_IN (olddecl
)
2264 && !C_DECL_DECLARED_BUILTIN (olddecl
))
2265 /* Don't warn about an extern followed by a definition. */
2266 && !(DECL_EXTERNAL (olddecl
) && !DECL_EXTERNAL (newdecl
))
2267 /* Don't warn about forward parameter decls. */
2268 && !(TREE_CODE (newdecl
) == PARM_DECL
2269 && TREE_ASM_WRITTEN (olddecl
) && !TREE_ASM_WRITTEN (newdecl
))
2270 /* Don't warn about a variable definition following a declaration. */
2271 && !(VAR_P (newdecl
)
2272 && DECL_INITIAL (newdecl
) && !DECL_INITIAL (olddecl
)))
2274 warned
= warning (OPT_Wredundant_decls
, "redundant redeclaration of %q+D",
2278 /* Report location of previous decl/defn. */
2279 if (warned
|| pedwarned
)
2280 locate_old_decl (olddecl
);
2282 #undef DECL_EXTERN_INLINE
2287 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2288 consistent with OLDDECL, but carries new information. Merge the
2289 new information into OLDDECL. This function issues no
2293 merge_decls (tree newdecl
, tree olddecl
, tree newtype
, tree oldtype
)
2295 bool new_is_definition
= (TREE_CODE (newdecl
) == FUNCTION_DECL
2296 && DECL_INITIAL (newdecl
) != 0);
2297 bool new_is_prototype
= (TREE_CODE (newdecl
) == FUNCTION_DECL
2298 && prototype_p (TREE_TYPE (newdecl
)));
2299 bool old_is_prototype
= (TREE_CODE (olddecl
) == FUNCTION_DECL
2300 && prototype_p (TREE_TYPE (olddecl
)));
2302 /* For real parm decl following a forward decl, rechain the old decl
2303 in its new location and clear TREE_ASM_WRITTEN (it's not a
2304 forward decl anymore). */
2305 if (TREE_CODE (newdecl
) == PARM_DECL
2306 && TREE_ASM_WRITTEN (olddecl
) && !TREE_ASM_WRITTEN (newdecl
))
2308 struct c_binding
*b
, **here
;
2310 for (here
= ¤t_scope
->bindings
; *here
; here
= &(*here
)->prev
)
2311 if ((*here
)->decl
== olddecl
)
2318 b
->prev
= current_scope
->bindings
;
2319 current_scope
->bindings
= b
;
2321 TREE_ASM_WRITTEN (olddecl
) = 0;
2324 DECL_ATTRIBUTES (newdecl
)
2325 = targetm
.merge_decl_attributes (olddecl
, newdecl
);
2327 /* For typedefs use the old type, as the new type's DECL_NAME points
2328 at newdecl, which will be ggc_freed. */
2329 if (TREE_CODE (newdecl
) == TYPE_DECL
)
2331 /* But NEWTYPE might have an attribute, honor that. */
2335 if (TYPE_USER_ALIGN (tem
))
2337 if (TYPE_ALIGN (tem
) > TYPE_ALIGN (newtype
))
2338 SET_TYPE_ALIGN (newtype
, TYPE_ALIGN (tem
));
2339 TYPE_USER_ALIGN (newtype
) = true;
2342 /* And remove the new type from the variants list. */
2343 if (TYPE_NAME (TREE_TYPE (newdecl
)) == newdecl
)
2345 tree remove
= TREE_TYPE (newdecl
);
2346 for (tree t
= TYPE_MAIN_VARIANT (remove
); ;
2347 t
= TYPE_NEXT_VARIANT (t
))
2348 if (TYPE_NEXT_VARIANT (t
) == remove
)
2350 TYPE_NEXT_VARIANT (t
) = TYPE_NEXT_VARIANT (remove
);
2356 /* Merge the data types specified in the two decls. */
2358 = TREE_TYPE (olddecl
)
2359 = composite_type (newtype
, oldtype
);
2361 /* Lay the type out, unless already done. */
2362 if (!comptypes (oldtype
, TREE_TYPE (newdecl
)))
2364 if (TREE_TYPE (newdecl
) != error_mark_node
)
2365 layout_type (TREE_TYPE (newdecl
));
2366 if (TREE_CODE (newdecl
) != FUNCTION_DECL
2367 && TREE_CODE (newdecl
) != TYPE_DECL
2368 && TREE_CODE (newdecl
) != CONST_DECL
)
2369 layout_decl (newdecl
, 0);
2373 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2374 DECL_SIZE (newdecl
) = DECL_SIZE (olddecl
);
2375 DECL_SIZE_UNIT (newdecl
) = DECL_SIZE_UNIT (olddecl
);
2376 DECL_MODE (newdecl
) = DECL_MODE (olddecl
);
2377 if (DECL_ALIGN (olddecl
) > DECL_ALIGN (newdecl
))
2379 SET_DECL_ALIGN (newdecl
, DECL_ALIGN (olddecl
));
2380 DECL_USER_ALIGN (newdecl
) |= DECL_USER_ALIGN (olddecl
);
2384 /* Keep the old rtl since we can safely use it. */
2385 if (HAS_RTL_P (olddecl
))
2386 COPY_DECL_RTL (olddecl
, newdecl
);
2388 /* Merge the type qualifiers. */
2389 if (TREE_READONLY (newdecl
))
2390 TREE_READONLY (olddecl
) = 1;
2392 if (TREE_THIS_VOLATILE (newdecl
))
2393 TREE_THIS_VOLATILE (olddecl
) = 1;
2395 /* Merge deprecatedness. */
2396 if (TREE_DEPRECATED (newdecl
))
2397 TREE_DEPRECATED (olddecl
) = 1;
2399 /* If a decl is in a system header and the other isn't, keep the one on the
2400 system header. Otherwise, keep source location of definition rather than
2401 declaration and of prototype rather than non-prototype unless that
2402 prototype is built-in. */
2403 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl
), TS_DECL_WITH_VIS
)
2404 && DECL_IN_SYSTEM_HEADER (olddecl
)
2405 && !DECL_IN_SYSTEM_HEADER (newdecl
) )
2406 DECL_SOURCE_LOCATION (newdecl
) = DECL_SOURCE_LOCATION (olddecl
);
2407 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl
), TS_DECL_WITH_VIS
)
2408 && DECL_IN_SYSTEM_HEADER (newdecl
)
2409 && !DECL_IN_SYSTEM_HEADER (olddecl
))
2410 DECL_SOURCE_LOCATION (olddecl
) = DECL_SOURCE_LOCATION (newdecl
);
2411 else if ((DECL_INITIAL (newdecl
) == 0 && DECL_INITIAL (olddecl
) != 0)
2412 || (old_is_prototype
&& !new_is_prototype
2413 && !C_DECL_BUILTIN_PROTOTYPE (olddecl
)))
2414 DECL_SOURCE_LOCATION (newdecl
) = DECL_SOURCE_LOCATION (olddecl
);
2416 /* Merge the initialization information. */
2417 if (DECL_INITIAL (newdecl
) == 0)
2418 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
2420 /* Merge the threadprivate attribute. */
2421 if (VAR_P (olddecl
) && C_DECL_THREADPRIVATE_P (olddecl
))
2422 C_DECL_THREADPRIVATE_P (newdecl
) = 1;
2424 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl
), TS_DECL_WITH_VIS
))
2426 /* Copy the assembler name.
2427 Currently, it can only be defined in the prototype. */
2428 COPY_DECL_ASSEMBLER_NAME (olddecl
, newdecl
);
2430 /* Use visibility of whichever declaration had it specified */
2431 if (DECL_VISIBILITY_SPECIFIED (olddecl
))
2433 DECL_VISIBILITY (newdecl
) = DECL_VISIBILITY (olddecl
);
2434 DECL_VISIBILITY_SPECIFIED (newdecl
) = 1;
2437 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2439 DECL_STATIC_CONSTRUCTOR(newdecl
) |= DECL_STATIC_CONSTRUCTOR(olddecl
);
2440 DECL_STATIC_DESTRUCTOR (newdecl
) |= DECL_STATIC_DESTRUCTOR (olddecl
);
2441 DECL_NO_LIMIT_STACK (newdecl
) |= DECL_NO_LIMIT_STACK (olddecl
);
2442 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl
)
2443 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl
);
2444 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
2445 DECL_IS_MALLOC (newdecl
) |= DECL_IS_MALLOC (olddecl
);
2446 DECL_IS_OPERATOR_NEW (newdecl
) |= DECL_IS_OPERATOR_NEW (olddecl
);
2447 TREE_READONLY (newdecl
) |= TREE_READONLY (olddecl
);
2448 DECL_PURE_P (newdecl
) |= DECL_PURE_P (olddecl
);
2449 DECL_IS_NOVOPS (newdecl
) |= DECL_IS_NOVOPS (olddecl
);
2452 /* Merge the storage class information. */
2453 merge_weak (newdecl
, olddecl
);
2455 /* For functions, static overrides non-static. */
2456 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2458 TREE_PUBLIC (newdecl
) &= TREE_PUBLIC (olddecl
);
2459 /* This is since we don't automatically
2460 copy the attributes of NEWDECL into OLDDECL. */
2461 TREE_PUBLIC (olddecl
) = TREE_PUBLIC (newdecl
);
2462 /* If this clears `static', clear it in the identifier too. */
2463 if (!TREE_PUBLIC (olddecl
))
2464 TREE_PUBLIC (DECL_NAME (olddecl
)) = 0;
2468 /* In c99, 'extern' declaration before (or after) 'inline' means this
2469 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2471 if (TREE_CODE (newdecl
) == FUNCTION_DECL
2472 && !flag_gnu89_inline
2473 && (DECL_DECLARED_INLINE_P (newdecl
)
2474 || DECL_DECLARED_INLINE_P (olddecl
))
2475 && (!DECL_DECLARED_INLINE_P (newdecl
)
2476 || !DECL_DECLARED_INLINE_P (olddecl
)
2477 || !DECL_EXTERNAL (olddecl
))
2478 && DECL_EXTERNAL (newdecl
)
2479 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl
))
2480 && !current_function_decl
)
2481 DECL_EXTERNAL (newdecl
) = 0;
2483 /* An inline definition following a static declaration is not
2485 if (new_is_definition
2486 && (DECL_DECLARED_INLINE_P (newdecl
)
2487 || DECL_DECLARED_INLINE_P (olddecl
))
2488 && !TREE_PUBLIC (olddecl
))
2489 DECL_EXTERNAL (newdecl
) = 0;
2491 if (DECL_EXTERNAL (newdecl
))
2493 TREE_STATIC (newdecl
) = TREE_STATIC (olddecl
);
2494 DECL_EXTERNAL (newdecl
) = DECL_EXTERNAL (olddecl
);
2496 /* An extern decl does not override previous storage class. */
2497 TREE_PUBLIC (newdecl
) = TREE_PUBLIC (olddecl
);
2498 if (!DECL_EXTERNAL (newdecl
))
2500 DECL_CONTEXT (newdecl
) = DECL_CONTEXT (olddecl
);
2501 DECL_COMMON (newdecl
) = DECL_COMMON (olddecl
);
2506 TREE_STATIC (olddecl
) = TREE_STATIC (newdecl
);
2507 TREE_PUBLIC (olddecl
) = TREE_PUBLIC (newdecl
);
2510 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2512 /* If we're redefining a function previously defined as extern
2513 inline, make sure we emit debug info for the inline before we
2514 throw it away, in case it was inlined into a function that
2515 hasn't been written out yet. */
2516 if (new_is_definition
&& DECL_INITIAL (olddecl
))
2517 /* The new defn must not be inline. */
2518 DECL_UNINLINABLE (newdecl
) = 1;
2521 /* If either decl says `inline', this fn is inline, unless
2522 its definition was passed already. */
2523 if (DECL_DECLARED_INLINE_P (newdecl
)
2524 || DECL_DECLARED_INLINE_P (olddecl
))
2525 DECL_DECLARED_INLINE_P (newdecl
) = 1;
2527 DECL_UNINLINABLE (newdecl
) = DECL_UNINLINABLE (olddecl
)
2528 = (DECL_UNINLINABLE (newdecl
) || DECL_UNINLINABLE (olddecl
));
2530 DECL_DISREGARD_INLINE_LIMITS (newdecl
)
2531 = DECL_DISREGARD_INLINE_LIMITS (olddecl
)
2532 = (DECL_DISREGARD_INLINE_LIMITS (newdecl
)
2533 || DECL_DISREGARD_INLINE_LIMITS (olddecl
));
2536 if (DECL_BUILT_IN (olddecl
))
2538 /* If redeclaring a builtin function, it stays built in.
2539 But it gets tagged as having been declared. */
2540 DECL_BUILT_IN_CLASS (newdecl
) = DECL_BUILT_IN_CLASS (olddecl
);
2541 DECL_FUNCTION_CODE (newdecl
) = DECL_FUNCTION_CODE (olddecl
);
2542 C_DECL_DECLARED_BUILTIN (newdecl
) = 1;
2543 if (new_is_prototype
)
2545 C_DECL_BUILTIN_PROTOTYPE (newdecl
) = 0;
2546 if (DECL_BUILT_IN_CLASS (newdecl
) == BUILT_IN_NORMAL
)
2548 enum built_in_function fncode
= DECL_FUNCTION_CODE (newdecl
);
2551 /* If a compatible prototype of these builtin functions
2552 is seen, assume the runtime implements it with the
2553 expected semantics. */
2554 case BUILT_IN_STPCPY
:
2555 if (builtin_decl_explicit_p (fncode
))
2556 set_builtin_decl_implicit_p (fncode
, true);
2559 if (builtin_decl_explicit_p (fncode
))
2560 set_builtin_decl_declared_p (fncode
, true);
2566 C_DECL_BUILTIN_PROTOTYPE (newdecl
)
2567 = C_DECL_BUILTIN_PROTOTYPE (olddecl
);
2570 /* Preserve function specific target and optimization options */
2571 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl
)
2572 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl
))
2573 DECL_FUNCTION_SPECIFIC_TARGET (newdecl
)
2574 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl
);
2576 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
)
2577 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
))
2578 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
)
2579 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
);
2581 /* Also preserve various other info from the definition. */
2582 if (!new_is_definition
)
2585 DECL_RESULT (newdecl
) = DECL_RESULT (olddecl
);
2586 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
2587 DECL_STRUCT_FUNCTION (newdecl
) = DECL_STRUCT_FUNCTION (olddecl
);
2588 DECL_SAVED_TREE (newdecl
) = DECL_SAVED_TREE (olddecl
);
2589 DECL_ARGUMENTS (newdecl
) = copy_list (DECL_ARGUMENTS (olddecl
));
2590 for (t
= DECL_ARGUMENTS (newdecl
); t
; t
= DECL_CHAIN (t
))
2591 DECL_CONTEXT (t
) = newdecl
;
2593 /* See if we've got a function to instantiate from. */
2594 if (DECL_SAVED_TREE (olddecl
))
2595 DECL_ABSTRACT_ORIGIN (newdecl
)
2596 = DECL_ABSTRACT_ORIGIN (olddecl
);
2600 /* Merge the USED information. */
2601 if (TREE_USED (olddecl
))
2602 TREE_USED (newdecl
) = 1;
2603 else if (TREE_USED (newdecl
))
2604 TREE_USED (olddecl
) = 1;
2605 if (VAR_P (olddecl
) || TREE_CODE (olddecl
) == PARM_DECL
)
2606 DECL_READ_P (newdecl
) |= DECL_READ_P (olddecl
);
2607 if (DECL_PRESERVE_P (olddecl
))
2608 DECL_PRESERVE_P (newdecl
) = 1;
2609 else if (DECL_PRESERVE_P (newdecl
))
2610 DECL_PRESERVE_P (olddecl
) = 1;
2612 /* Merge DECL_COMMON */
2613 if (VAR_P (olddecl
) && VAR_P (newdecl
)
2614 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl
))
2615 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl
)))
2616 DECL_COMMON (newdecl
) = DECL_COMMON (newdecl
) && DECL_COMMON (olddecl
);
2618 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2619 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2620 DECL_ARGUMENTS (if appropriate). */
2622 unsigned olddecl_uid
= DECL_UID (olddecl
);
2623 tree olddecl_context
= DECL_CONTEXT (olddecl
);
2624 tree olddecl_arguments
= NULL
;
2625 if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
2626 olddecl_arguments
= DECL_ARGUMENTS (olddecl
);
2628 memcpy ((char *) olddecl
+ sizeof (struct tree_common
),
2629 (char *) newdecl
+ sizeof (struct tree_common
),
2630 sizeof (struct tree_decl_common
) - sizeof (struct tree_common
));
2631 DECL_USER_ALIGN (olddecl
) = DECL_USER_ALIGN (newdecl
);
2632 switch (TREE_CODE (olddecl
))
2637 struct symtab_node
*snode
= olddecl
->decl_with_vis
.symtab_node
;
2639 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2640 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2641 tree_code_size (TREE_CODE (olddecl
)) - sizeof (struct tree_decl_common
));
2642 olddecl
->decl_with_vis
.symtab_node
= snode
;
2644 if ((DECL_EXTERNAL (olddecl
)
2645 || TREE_PUBLIC (olddecl
)
2646 || TREE_STATIC (olddecl
))
2647 && DECL_SECTION_NAME (newdecl
) != NULL
)
2648 set_decl_section_name (olddecl
, DECL_SECTION_NAME (newdecl
));
2650 /* This isn't quite correct for something like
2651 int __thread x attribute ((tls_model ("local-exec")));
2652 extern int __thread x;
2653 as we'll lose the "local-exec" model. */
2654 if (VAR_P (olddecl
) && DECL_THREAD_LOCAL_P (newdecl
))
2655 set_decl_tls_model (olddecl
, DECL_TLS_MODEL (newdecl
));
2665 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2666 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2667 tree_code_size (TREE_CODE (olddecl
)) - sizeof (struct tree_decl_common
));
2672 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2673 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2674 sizeof (struct tree_decl_non_common
) - sizeof (struct tree_decl_common
));
2676 DECL_UID (olddecl
) = olddecl_uid
;
2677 DECL_CONTEXT (olddecl
) = olddecl_context
;
2678 if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
2679 DECL_ARGUMENTS (olddecl
) = olddecl_arguments
;
2682 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2683 so that encode_section_info has a chance to look at the new decl
2684 flags and attributes. */
2685 if (DECL_RTL_SET_P (olddecl
)
2686 && (TREE_CODE (olddecl
) == FUNCTION_DECL
2687 || (VAR_P (olddecl
) && TREE_STATIC (olddecl
))))
2688 make_decl_rtl (olddecl
);
2691 /* Handle when a new declaration NEWDECL has the same name as an old
2692 one OLDDECL in the same binding contour. Prints an error message
2695 If safely possible, alter OLDDECL to look like NEWDECL, and return
2696 true. Otherwise, return false. */
2699 duplicate_decls (tree newdecl
, tree olddecl
)
2701 tree newtype
= NULL
, oldtype
= NULL
;
2703 if (!diagnose_mismatched_decls (newdecl
, olddecl
, &newtype
, &oldtype
))
2705 /* Avoid `unused variable' and other warnings for OLDDECL. */
2706 TREE_NO_WARNING (olddecl
) = 1;
2710 merge_decls (newdecl
, olddecl
, newtype
, oldtype
);
2712 /* The NEWDECL will no longer be needed.
2714 Before releasing the node, be sure to remove function from symbol
2715 table that might have been inserted there to record comdat group.
2716 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2717 structure is shared in between NEWDECL and OLDECL. */
2718 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2719 DECL_STRUCT_FUNCTION (newdecl
) = NULL
;
2720 if (VAR_OR_FUNCTION_DECL_P (newdecl
))
2722 struct symtab_node
*snode
= symtab_node::get (newdecl
);
2731 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2733 warn_if_shadowing (tree new_decl
)
2735 struct c_binding
*b
;
2737 /* Shadow warnings wanted? */
2739 /* No shadow warnings for internally generated vars. */
2740 || DECL_IS_BUILTIN (new_decl
)
2741 /* No shadow warnings for vars made for inlining. */
2742 || DECL_FROM_INLINE (new_decl
))
2745 /* Is anything being shadowed? Invisible decls do not count. */
2746 for (b
= I_SYMBOL_BINDING (DECL_NAME (new_decl
)); b
; b
= b
->shadowed
)
2747 if (b
->decl
&& b
->decl
!= new_decl
&& !b
->invisible
2748 && (b
->decl
== error_mark_node
2749 || diagnostic_report_warnings_p (global_dc
,
2750 DECL_SOURCE_LOCATION (b
->decl
))))
2752 tree old_decl
= b
->decl
;
2753 bool warned
= false;
2755 if (old_decl
== error_mark_node
)
2757 warning (OPT_Wshadow
, "declaration of %q+D shadows previous "
2758 "non-variable", new_decl
);
2761 else if (TREE_CODE (old_decl
) == PARM_DECL
)
2762 warned
= warning (OPT_Wshadow
,
2763 "declaration of %q+D shadows a parameter",
2765 else if (DECL_FILE_SCOPE_P (old_decl
))
2767 /* Do not warn if a variable shadows a function, unless
2768 the variable is a function or a pointer-to-function. */
2769 if (TREE_CODE (old_decl
) == FUNCTION_DECL
2770 && TREE_CODE (new_decl
) != FUNCTION_DECL
2771 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl
)))
2774 warned
= warning_at (DECL_SOURCE_LOCATION (new_decl
), OPT_Wshadow
,
2775 "declaration of %qD shadows a global "
2779 else if (TREE_CODE (old_decl
) == FUNCTION_DECL
2780 && DECL_BUILT_IN (old_decl
))
2782 warning (OPT_Wshadow
, "declaration of %q+D shadows "
2783 "a built-in function", new_decl
);
2787 warned
= warning (OPT_Wshadow
, "declaration of %q+D shadows a "
2788 "previous local", new_decl
);
2791 inform (DECL_SOURCE_LOCATION (old_decl
),
2792 "shadowed declaration is here");
2798 /* Record a decl-node X as belonging to the current lexical scope.
2799 Check for errors (such as an incompatible declaration for the same
2800 name already seen in the same scope).
2802 Returns either X or an old decl for the same name.
2803 If an old decl is returned, it may have been smashed
2804 to agree with what X says. */
2809 tree name
= DECL_NAME (x
);
2810 struct c_scope
*scope
= current_scope
;
2811 struct c_binding
*b
;
2812 bool nested
= false;
2813 location_t locus
= DECL_SOURCE_LOCATION (x
);
2815 /* Must set DECL_CONTEXT for everything not at file scope or
2816 DECL_FILE_SCOPE_P won't work. Local externs don't count
2817 unless they have initializers (which generate code). */
2818 if (current_function_decl
2819 && (!VAR_OR_FUNCTION_DECL_P (x
)
2820 || DECL_INITIAL (x
) || !DECL_EXTERNAL (x
)))
2821 DECL_CONTEXT (x
) = current_function_decl
;
2823 /* Anonymous decls are just inserted in the scope. */
2826 bind (name
, x
, scope
, /*invisible=*/false, /*nested=*/false,
2831 /* First, see if there is another declaration with the same name in
2832 the current scope. If there is, duplicate_decls may do all the
2833 work for us. If duplicate_decls returns false, that indicates
2834 two incompatible decls in the same scope; we are to silently
2835 replace the old one (duplicate_decls has issued all appropriate
2836 diagnostics). In particular, we should not consider possible
2837 duplicates in the external scope, or shadowing. */
2838 b
= I_SYMBOL_BINDING (name
);
2839 if (b
&& B_IN_SCOPE (b
, scope
))
2841 struct c_binding
*b_ext
, *b_use
;
2842 tree type
= TREE_TYPE (x
);
2843 tree visdecl
= b
->decl
;
2844 tree vistype
= TREE_TYPE (visdecl
);
2845 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
2846 && COMPLETE_TYPE_P (TREE_TYPE (x
)))
2847 b
->inner_comp
= false;
2850 /* If this is an external linkage declaration, we should check
2851 for compatibility with the type in the external scope before
2852 setting the type at this scope based on the visible
2853 information only. */
2854 if (TREE_PUBLIC (x
) && TREE_PUBLIC (visdecl
))
2856 while (b_ext
&& !B_IN_EXTERNAL_SCOPE (b_ext
))
2857 b_ext
= b_ext
->shadowed
;
2862 TREE_TYPE (b_use
->decl
) = b_use
->u
.type
;
2865 if (duplicate_decls (x
, b_use
->decl
))
2869 /* Save the updated type in the external scope and
2870 restore the proper type for this scope. */
2872 if (comptypes (vistype
, type
))
2873 thistype
= composite_type (vistype
, type
);
2875 thistype
= TREE_TYPE (b_use
->decl
);
2876 b_use
->u
.type
= TREE_TYPE (b_use
->decl
);
2877 if (TREE_CODE (b_use
->decl
) == FUNCTION_DECL
2878 && DECL_BUILT_IN (b_use
->decl
))
2880 = build_type_attribute_variant (thistype
,
2883 TREE_TYPE (b_use
->decl
) = thistype
;
2888 goto skip_external_and_shadow_checks
;
2891 /* All declarations with external linkage, and all external
2892 references, go in the external scope, no matter what scope is
2893 current. However, the binding in that scope is ignored for
2894 purposes of normal name lookup. A separate binding structure is
2895 created in the requested scope; this governs the normal
2896 visibility of the symbol.
2898 The binding in the externals scope is used exclusively for
2899 detecting duplicate declarations of the same object, no matter
2900 what scope they are in; this is what we do here. (C99 6.2.7p2:
2901 All declarations that refer to the same object or function shall
2902 have compatible type; otherwise, the behavior is undefined.) */
2903 if (DECL_EXTERNAL (x
) || scope
== file_scope
)
2905 tree type
= TREE_TYPE (x
);
2908 bool type_saved
= false;
2909 if (b
&& !B_IN_EXTERNAL_SCOPE (b
)
2910 && VAR_OR_FUNCTION_DECL_P (b
->decl
)
2911 && DECL_FILE_SCOPE_P (b
->decl
))
2914 vistype
= TREE_TYPE (visdecl
);
2916 if (scope
!= file_scope
2917 && !DECL_IN_SYSTEM_HEADER (x
))
2918 warning_at (locus
, OPT_Wnested_externs
,
2919 "nested extern declaration of %qD", x
);
2921 while (b
&& !B_IN_EXTERNAL_SCOPE (b
))
2923 /* If this decl might be modified, save its type. This is
2924 done here rather than when the decl is first bound
2925 because the type may change after first binding, through
2926 being completed or through attributes being added. If we
2927 encounter multiple such decls, only the first should have
2928 its type saved; the others will already have had their
2929 proper types saved and the types will not have changed as
2930 their scopes will not have been re-entered. */
2931 if (DECL_P (b
->decl
) && DECL_FILE_SCOPE_P (b
->decl
) && !type_saved
)
2933 b
->u
.type
= TREE_TYPE (b
->decl
);
2936 if (B_IN_FILE_SCOPE (b
)
2938 && TREE_STATIC (b
->decl
)
2939 && TREE_CODE (TREE_TYPE (b
->decl
)) == ARRAY_TYPE
2940 && !TYPE_DOMAIN (TREE_TYPE (b
->decl
))
2941 && TREE_CODE (type
) == ARRAY_TYPE
2942 && TYPE_DOMAIN (type
)
2943 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
2944 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))
2946 /* Array type completed in inner scope, which should be
2947 diagnosed if the completion does not have size 1 and
2948 it does not get completed in the file scope. */
2949 b
->inner_comp
= true;
2954 /* If a matching external declaration has been found, set its
2955 type to the composite of all the types of that declaration.
2956 After the consistency checks, it will be reset to the
2957 composite of the visible types only. */
2958 if (b
&& (TREE_PUBLIC (x
) || same_translation_unit_p (x
, b
->decl
))
2960 TREE_TYPE (b
->decl
) = b
->u
.type
;
2962 /* The point of the same_translation_unit_p check here is,
2963 we want to detect a duplicate decl for a construct like
2964 foo() { extern bar(); } ... static bar(); but not if
2965 they are in different translation units. In any case,
2966 the static does not go in the externals scope. */
2968 && (TREE_PUBLIC (x
) || same_translation_unit_p (x
, b
->decl
))
2969 && duplicate_decls (x
, b
->decl
))
2974 if (comptypes (vistype
, type
))
2975 thistype
= composite_type (vistype
, type
);
2977 thistype
= TREE_TYPE (b
->decl
);
2981 b
->u
.type
= TREE_TYPE (b
->decl
);
2982 if (TREE_CODE (b
->decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (b
->decl
))
2984 = build_type_attribute_variant (thistype
,
2985 TYPE_ATTRIBUTES (b
->u
.type
));
2986 TREE_TYPE (b
->decl
) = thistype
;
2987 bind (name
, b
->decl
, scope
, /*invisible=*/false, /*nested=*/true,
2991 else if (TREE_PUBLIC (x
))
2993 if (visdecl
&& !b
&& duplicate_decls (x
, visdecl
))
2995 /* An external declaration at block scope referring to a
2996 visible entity with internal linkage. The composite
2997 type will already be correct for this scope, so we
2998 just need to fall through to make the declaration in
3005 bind (name
, x
, external_scope
, /*invisible=*/true,
3006 /*nested=*/false, locus
);
3012 if (TREE_CODE (x
) != PARM_DECL
)
3013 warn_if_shadowing (x
);
3015 skip_external_and_shadow_checks
:
3016 if (TREE_CODE (x
) == TYPE_DECL
)
3018 /* So this is a typedef, set its underlying type. */
3019 set_underlying_type (x
);
3021 /* If X is a typedef defined in the current function, record it
3022 for the purpose of implementing the -Wunused-local-typedefs
3024 record_locally_defined_typedef (x
);
3027 bind (name
, x
, scope
, /*invisible=*/false, nested
, locus
);
3029 /* If x's type is incomplete because it's based on a
3030 structure or union which has not yet been fully declared,
3031 attach it to that structure or union type, so we can go
3032 back and complete the variable declaration later, if the
3033 structure or union gets fully declared.
3035 If the input is erroneous, we can have error_mark in the type
3036 slot (e.g. "f(void a, ...)") - that doesn't count as an
3038 if (TREE_TYPE (x
) != error_mark_node
3039 && !COMPLETE_TYPE_P (TREE_TYPE (x
)))
3041 tree element
= TREE_TYPE (x
);
3043 while (TREE_CODE (element
) == ARRAY_TYPE
)
3044 element
= TREE_TYPE (element
);
3045 element
= TYPE_MAIN_VARIANT (element
);
3047 if (RECORD_OR_UNION_TYPE_P (element
)
3048 && (TREE_CODE (x
) != TYPE_DECL
3049 || TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
)
3050 && !COMPLETE_TYPE_P (element
))
3051 C_TYPE_INCOMPLETE_VARS (element
)
3052 = tree_cons (NULL_TREE
, x
, C_TYPE_INCOMPLETE_VARS (element
));
3057 /* Record X as belonging to file scope.
3058 This is used only internally by the Objective-C front end,
3059 and is limited to its needs. duplicate_decls is not called;
3060 if there is any preexisting decl for this identifier, it is an ICE. */
3063 pushdecl_top_level (tree x
)
3066 bool nested
= false;
3067 gcc_assert (VAR_P (x
) || TREE_CODE (x
) == CONST_DECL
);
3069 name
= DECL_NAME (x
);
3071 gcc_assert (TREE_CODE (x
) == CONST_DECL
|| !I_SYMBOL_BINDING (name
));
3073 if (TREE_PUBLIC (x
))
3075 bind (name
, x
, external_scope
, /*invisible=*/true, /*nested=*/false,
3080 bind (name
, x
, file_scope
, /*invisible=*/false, nested
, UNKNOWN_LOCATION
);
3086 implicit_decl_warning (location_t loc
, tree id
, tree olddecl
)
3088 if (warn_implicit_function_declaration
)
3091 const char *hint
= NULL
;
3093 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_FUNCTION_NAME
);
3098 gcc_rich_location
richloc (loc
);
3099 richloc
.add_fixit_misspelled_id (loc
, hint
);
3100 warned
= pedwarn_at_rich_loc
3101 (&richloc
, OPT_Wimplicit_function_declaration
,
3102 "implicit declaration of function %qE; did you mean %qs?",
3106 warned
= pedwarn (loc
, OPT_Wimplicit_function_declaration
,
3107 "implicit declaration of function %qE", id
);
3111 gcc_rich_location
richloc (loc
);
3112 richloc
.add_fixit_misspelled_id (loc
, hint
);
3113 warned
= warning_at_rich_loc
3114 (&richloc
, OPT_Wimplicit_function_declaration
,
3115 G_("implicit declaration of function %qE;did you mean %qs?"),
3119 warned
= warning_at (loc
, OPT_Wimplicit_function_declaration
,
3120 G_("implicit declaration of function %qE"), id
);
3121 if (olddecl
&& warned
)
3122 locate_old_decl (olddecl
);
3126 /* This function represents mapping of a function code FCODE
3127 to its respective header. */
3130 header_for_builtin_fn (enum built_in_function fcode
)
3134 CASE_FLT_FN (BUILT_IN_ACOS
):
3135 CASE_FLT_FN (BUILT_IN_ACOSH
):
3136 CASE_FLT_FN (BUILT_IN_ASIN
):
3137 CASE_FLT_FN (BUILT_IN_ASINH
):
3138 CASE_FLT_FN (BUILT_IN_ATAN
):
3139 CASE_FLT_FN (BUILT_IN_ATANH
):
3140 CASE_FLT_FN (BUILT_IN_ATAN2
):
3141 CASE_FLT_FN (BUILT_IN_CBRT
):
3142 CASE_FLT_FN (BUILT_IN_CEIL
):
3143 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
3144 CASE_FLT_FN (BUILT_IN_COS
):
3145 CASE_FLT_FN (BUILT_IN_COSH
):
3146 CASE_FLT_FN (BUILT_IN_ERF
):
3147 CASE_FLT_FN (BUILT_IN_ERFC
):
3148 CASE_FLT_FN (BUILT_IN_EXP
):
3149 CASE_FLT_FN (BUILT_IN_EXP2
):
3150 CASE_FLT_FN (BUILT_IN_EXPM1
):
3151 CASE_FLT_FN (BUILT_IN_FABS
):
3152 CASE_FLT_FN (BUILT_IN_FDIM
):
3153 CASE_FLT_FN (BUILT_IN_FLOOR
):
3154 CASE_FLT_FN (BUILT_IN_FMA
):
3155 CASE_FLT_FN (BUILT_IN_FMAX
):
3156 CASE_FLT_FN (BUILT_IN_FMIN
):
3157 CASE_FLT_FN (BUILT_IN_FMOD
):
3158 CASE_FLT_FN (BUILT_IN_FREXP
):
3159 CASE_FLT_FN (BUILT_IN_HYPOT
):
3160 CASE_FLT_FN (BUILT_IN_ILOGB
):
3161 CASE_FLT_FN (BUILT_IN_LDEXP
):
3162 CASE_FLT_FN (BUILT_IN_LGAMMA
):
3163 CASE_FLT_FN (BUILT_IN_LLRINT
):
3164 CASE_FLT_FN (BUILT_IN_LLROUND
):
3165 CASE_FLT_FN (BUILT_IN_LOG
):
3166 CASE_FLT_FN (BUILT_IN_LOG10
):
3167 CASE_FLT_FN (BUILT_IN_LOG1P
):
3168 CASE_FLT_FN (BUILT_IN_LOG2
):
3169 CASE_FLT_FN (BUILT_IN_LOGB
):
3170 CASE_FLT_FN (BUILT_IN_LRINT
):
3171 CASE_FLT_FN (BUILT_IN_LROUND
):
3172 CASE_FLT_FN (BUILT_IN_MODF
):
3173 CASE_FLT_FN (BUILT_IN_NAN
):
3174 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
3175 CASE_FLT_FN (BUILT_IN_NEXTAFTER
):
3176 CASE_FLT_FN (BUILT_IN_NEXTTOWARD
):
3177 CASE_FLT_FN (BUILT_IN_POW
):
3178 CASE_FLT_FN (BUILT_IN_REMAINDER
):
3179 CASE_FLT_FN (BUILT_IN_REMQUO
):
3180 CASE_FLT_FN (BUILT_IN_RINT
):
3181 CASE_FLT_FN (BUILT_IN_ROUND
):
3182 CASE_FLT_FN (BUILT_IN_SCALBLN
):
3183 CASE_FLT_FN (BUILT_IN_SCALBN
):
3184 CASE_FLT_FN (BUILT_IN_SIN
):
3185 CASE_FLT_FN (BUILT_IN_SINH
):
3186 CASE_FLT_FN (BUILT_IN_SINCOS
):
3187 CASE_FLT_FN (BUILT_IN_SQRT
):
3188 CASE_FLT_FN (BUILT_IN_TAN
):
3189 CASE_FLT_FN (BUILT_IN_TANH
):
3190 CASE_FLT_FN (BUILT_IN_TGAMMA
):
3191 CASE_FLT_FN (BUILT_IN_TRUNC
):
3192 case BUILT_IN_ISINF
:
3193 case BUILT_IN_ISNAN
:
3195 CASE_FLT_FN (BUILT_IN_CABS
):
3196 CASE_FLT_FN (BUILT_IN_CACOS
):
3197 CASE_FLT_FN (BUILT_IN_CACOSH
):
3198 CASE_FLT_FN (BUILT_IN_CARG
):
3199 CASE_FLT_FN (BUILT_IN_CASIN
):
3200 CASE_FLT_FN (BUILT_IN_CASINH
):
3201 CASE_FLT_FN (BUILT_IN_CATAN
):
3202 CASE_FLT_FN (BUILT_IN_CATANH
):
3203 CASE_FLT_FN (BUILT_IN_CCOS
):
3204 CASE_FLT_FN (BUILT_IN_CCOSH
):
3205 CASE_FLT_FN (BUILT_IN_CEXP
):
3206 CASE_FLT_FN (BUILT_IN_CIMAG
):
3207 CASE_FLT_FN (BUILT_IN_CLOG
):
3208 CASE_FLT_FN (BUILT_IN_CONJ
):
3209 CASE_FLT_FN (BUILT_IN_CPOW
):
3210 CASE_FLT_FN (BUILT_IN_CPROJ
):
3211 CASE_FLT_FN (BUILT_IN_CREAL
):
3212 CASE_FLT_FN (BUILT_IN_CSIN
):
3213 CASE_FLT_FN (BUILT_IN_CSINH
):
3214 CASE_FLT_FN (BUILT_IN_CSQRT
):
3215 CASE_FLT_FN (BUILT_IN_CTAN
):
3216 CASE_FLT_FN (BUILT_IN_CTANH
):
3217 return "<complex.h>";
3218 case BUILT_IN_MEMCHR
:
3219 case BUILT_IN_MEMCMP
:
3220 case BUILT_IN_MEMCPY
:
3221 case BUILT_IN_MEMMOVE
:
3222 case BUILT_IN_MEMSET
:
3223 case BUILT_IN_STRCAT
:
3224 case BUILT_IN_STRCHR
:
3225 case BUILT_IN_STRCMP
:
3226 case BUILT_IN_STRCPY
:
3227 case BUILT_IN_STRCSPN
:
3228 case BUILT_IN_STRLEN
:
3229 case BUILT_IN_STRNCAT
:
3230 case BUILT_IN_STRNCMP
:
3231 case BUILT_IN_STRNCPY
:
3232 case BUILT_IN_STRPBRK
:
3233 case BUILT_IN_STRRCHR
:
3234 case BUILT_IN_STRSPN
:
3235 case BUILT_IN_STRSTR
:
3236 return "<string.h>";
3237 case BUILT_IN_FPRINTF
:
3239 case BUILT_IN_FPUTC
:
3240 case BUILT_IN_FPUTS
:
3241 case BUILT_IN_FSCANF
:
3242 case BUILT_IN_FWRITE
:
3243 case BUILT_IN_PRINTF
:
3244 case BUILT_IN_PUTCHAR
:
3246 case BUILT_IN_SCANF
:
3247 case BUILT_IN_SNPRINTF
:
3248 case BUILT_IN_SPRINTF
:
3249 case BUILT_IN_SSCANF
:
3250 case BUILT_IN_VFPRINTF
:
3251 case BUILT_IN_VFSCANF
:
3252 case BUILT_IN_VPRINTF
:
3253 case BUILT_IN_VSCANF
:
3254 case BUILT_IN_VSNPRINTF
:
3255 case BUILT_IN_VSPRINTF
:
3256 case BUILT_IN_VSSCANF
:
3258 case BUILT_IN_ISALNUM
:
3259 case BUILT_IN_ISALPHA
:
3260 case BUILT_IN_ISBLANK
:
3261 case BUILT_IN_ISCNTRL
:
3262 case BUILT_IN_ISDIGIT
:
3263 case BUILT_IN_ISGRAPH
:
3264 case BUILT_IN_ISLOWER
:
3265 case BUILT_IN_ISPRINT
:
3266 case BUILT_IN_ISPUNCT
:
3267 case BUILT_IN_ISSPACE
:
3268 case BUILT_IN_ISUPPER
:
3269 case BUILT_IN_ISXDIGIT
:
3270 case BUILT_IN_TOLOWER
:
3271 case BUILT_IN_TOUPPER
:
3273 case BUILT_IN_ISWALNUM
:
3274 case BUILT_IN_ISWALPHA
:
3275 case BUILT_IN_ISWBLANK
:
3276 case BUILT_IN_ISWCNTRL
:
3277 case BUILT_IN_ISWDIGIT
:
3278 case BUILT_IN_ISWGRAPH
:
3279 case BUILT_IN_ISWLOWER
:
3280 case BUILT_IN_ISWPRINT
:
3281 case BUILT_IN_ISWPUNCT
:
3282 case BUILT_IN_ISWSPACE
:
3283 case BUILT_IN_ISWUPPER
:
3284 case BUILT_IN_ISWXDIGIT
:
3285 case BUILT_IN_TOWLOWER
:
3286 case BUILT_IN_TOWUPPER
:
3287 return "<wctype.h>";
3288 case BUILT_IN_ABORT
:
3290 case BUILT_IN_CALLOC
:
3294 case BUILT_IN_LLABS
:
3295 case BUILT_IN_MALLOC
:
3296 case BUILT_IN_REALLOC
:
3297 case BUILT_IN__EXIT2
:
3298 case BUILT_IN_ALIGNED_ALLOC
:
3299 return "<stdlib.h>";
3300 case BUILT_IN_IMAXABS
:
3301 return "<inttypes.h>";
3302 case BUILT_IN_STRFTIME
:
3309 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3310 function of type int (). */
3313 implicitly_declare (location_t loc
, tree functionid
)
3315 struct c_binding
*b
;
3319 for (b
= I_SYMBOL_BINDING (functionid
); b
; b
= b
->shadowed
)
3321 if (B_IN_SCOPE (b
, external_scope
))
3330 if (decl
== error_mark_node
)
3333 /* FIXME: Objective-C has weird not-really-builtin functions
3334 which are supposed to be visible automatically. They wind up
3335 in the external scope because they're pushed before the file
3336 scope gets created. Catch this here and rebind them into the
3338 if (!DECL_BUILT_IN (decl
) && DECL_IS_BUILTIN (decl
))
3340 bind (functionid
, decl
, file_scope
,
3341 /*invisible=*/false, /*nested=*/true,
3342 DECL_SOURCE_LOCATION (decl
));
3347 tree newtype
= default_function_type
;
3349 TREE_TYPE (decl
) = b
->u
.type
;
3350 /* Implicit declaration of a function already declared
3351 (somehow) in a different scope, or as a built-in.
3352 If this is the first time this has happened, warn;
3353 then recycle the old declaration but with the new type. */
3354 if (!C_DECL_IMPLICIT (decl
))
3356 implicit_decl_warning (loc
, functionid
, decl
);
3357 C_DECL_IMPLICIT (decl
) = 1;
3359 if (DECL_BUILT_IN (decl
))
3361 newtype
= build_type_attribute_variant (newtype
,
3363 (TREE_TYPE (decl
)));
3364 if (!comptypes (newtype
, TREE_TYPE (decl
)))
3366 bool warned
= warning_at (loc
, 0, "incompatible implicit "
3367 "declaration of built-in "
3368 "function %qD", decl
);
3369 /* See if we can hint which header to include. */
3371 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl
));
3372 if (header
!= NULL
&& warned
)
3373 inform (loc
, "include %qs or provide a declaration of %qD",
3375 newtype
= TREE_TYPE (decl
);
3380 if (!comptypes (newtype
, TREE_TYPE (decl
)))
3382 error_at (loc
, "incompatible implicit declaration of "
3383 "function %qD", decl
);
3384 locate_old_decl (decl
);
3387 b
->u
.type
= TREE_TYPE (decl
);
3388 TREE_TYPE (decl
) = newtype
;
3389 bind (functionid
, decl
, current_scope
,
3390 /*invisible=*/false, /*nested=*/true,
3391 DECL_SOURCE_LOCATION (decl
));
3396 /* Not seen before. */
3397 decl
= build_decl (loc
, FUNCTION_DECL
, functionid
, default_function_type
);
3398 DECL_EXTERNAL (decl
) = 1;
3399 TREE_PUBLIC (decl
) = 1;
3400 C_DECL_IMPLICIT (decl
) = 1;
3401 implicit_decl_warning (loc
, functionid
, 0);
3402 asmspec_tree
= maybe_apply_renaming_pragma (decl
, /*asmname=*/NULL
);
3404 set_user_assembler_name (decl
, TREE_STRING_POINTER (asmspec_tree
));
3406 /* C89 says implicit declarations are in the innermost block.
3407 So we record the decl in the standard fashion. */
3408 decl
= pushdecl (decl
);
3410 /* No need to call objc_check_decl here - it's a function type. */
3411 rest_of_decl_compilation (decl
, 0, 0);
3413 /* Write a record describing this implicit function declaration
3414 to the prototypes file (if requested). */
3415 gen_aux_info_record (decl
, 0, 1, 0);
3417 /* Possibly apply some default attributes to this implicit declaration. */
3418 decl_attributes (&decl
, NULL_TREE
, 0);
3423 /* Issue an error message for a reference to an undeclared variable
3424 ID, including a reference to a builtin outside of function-call
3425 context. Establish a binding of the identifier to error_mark_node
3426 in an appropriate scope, which will suppress further errors for the
3427 same identifier. The error message should be given location LOC. */
3429 undeclared_variable (location_t loc
, tree id
)
3431 static bool already
= false;
3432 struct c_scope
*scope
;
3434 if (current_function_decl
== 0)
3436 const char *guessed_id
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_NAME
);
3439 gcc_rich_location
richloc (loc
);
3440 richloc
.add_fixit_misspelled_id (loc
, guessed_id
);
3441 error_at_rich_loc (&richloc
,
3442 "%qE undeclared here (not in a function);"
3443 " did you mean %qs?",
3447 error_at (loc
, "%qE undeclared here (not in a function)", id
);
3448 scope
= current_scope
;
3452 if (!objc_diagnose_private_ivar (id
))
3454 const char *guessed_id
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_NAME
);
3457 gcc_rich_location
richloc (loc
);
3458 richloc
.add_fixit_misspelled_id (loc
, guessed_id
);
3461 "%qE undeclared (first use in this function);"
3462 " did you mean %qs?",
3466 error_at (loc
, "%qE undeclared (first use in this function)", id
);
3470 inform (loc
, "each undeclared identifier is reported only"
3471 " once for each function it appears in");
3475 /* If we are parsing old-style parameter decls, current_function_decl
3476 will be nonnull but current_function_scope will be null. */
3477 scope
= current_function_scope
? current_function_scope
: current_scope
;
3479 bind (id
, error_mark_node
, scope
, /*invisible=*/false, /*nested=*/false,
3483 /* Subroutine of lookup_label, declare_label, define_label: construct a
3484 LABEL_DECL with all the proper frills. Also create a struct
3485 c_label_vars initialized for the current scope. */
3488 make_label (location_t location
, tree name
, bool defining
,
3489 struct c_label_vars
**p_label_vars
)
3491 tree label
= build_decl (location
, LABEL_DECL
, name
, void_type_node
);
3492 DECL_CONTEXT (label
) = current_function_decl
;
3493 DECL_MODE (label
) = VOIDmode
;
3495 c_label_vars
*label_vars
= ggc_alloc
<c_label_vars
> ();
3496 label_vars
->shadowed
= NULL
;
3497 set_spot_bindings (&label_vars
->label_bindings
, defining
);
3498 label_vars
->decls_in_scope
= make_tree_vector ();
3499 label_vars
->gotos
= NULL
;
3500 *p_label_vars
= label_vars
;
3505 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3506 Create one if none exists so far for the current function.
3507 This is called when a label is used in a goto expression or
3508 has its address taken. */
3511 lookup_label (tree name
)
3514 struct c_label_vars
*label_vars
;
3516 if (current_function_scope
== 0)
3518 error ("label %qE referenced outside of any function", name
);
3522 /* Use a label already defined or ref'd with this name, but not if
3523 it is inherited from a containing function and wasn't declared
3525 label
= I_LABEL_DECL (name
);
3526 if (label
&& (DECL_CONTEXT (label
) == current_function_decl
3527 || C_DECLARED_LABEL_FLAG (label
)))
3529 /* If the label has only been declared, update its apparent
3530 location to point here, for better diagnostics if it
3531 turns out not to have been defined. */
3532 if (DECL_INITIAL (label
) == NULL_TREE
)
3533 DECL_SOURCE_LOCATION (label
) = input_location
;
3537 /* No label binding for that identifier; make one. */
3538 label
= make_label (input_location
, name
, false, &label_vars
);
3540 /* Ordinary labels go in the current function scope. */
3541 bind_label (name
, label
, current_function_scope
, label_vars
);
3546 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3550 warn_about_goto (location_t goto_loc
, tree label
, tree decl
)
3552 if (variably_modified_type_p (TREE_TYPE (decl
), NULL_TREE
))
3554 "jump into scope of identifier with variably modified type");
3556 warning_at (goto_loc
, OPT_Wjump_misses_init
,
3557 "jump skips variable initialization");
3558 inform (DECL_SOURCE_LOCATION (label
), "label %qD defined here", label
);
3559 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3562 /* Look up a label because of a goto statement. This is like
3563 lookup_label, but also issues any appropriate warnings. */
3566 lookup_label_for_goto (location_t loc
, tree name
)
3569 struct c_label_vars
*label_vars
;
3573 label
= lookup_label (name
);
3574 if (label
== NULL_TREE
)
3577 /* If we are jumping to a different function, we can't issue any
3579 if (DECL_CONTEXT (label
) != current_function_decl
)
3581 gcc_assert (C_DECLARED_LABEL_FLAG (label
));
3585 label_vars
= I_LABEL_BINDING (name
)->u
.label
;
3587 /* If the label has not yet been defined, then push this goto on a
3588 list for possible later warnings. */
3589 if (label_vars
->label_bindings
.scope
== NULL
)
3591 c_goto_bindings
*g
= ggc_alloc
<c_goto_bindings
> ();
3594 set_spot_bindings (&g
->goto_bindings
, true);
3595 vec_safe_push (label_vars
->gotos
, g
);
3599 /* If there are any decls in label_vars->decls_in_scope, then this
3600 goto has missed the declaration of the decl. This happens for a
3606 Issue a warning or error. */
3607 FOR_EACH_VEC_SAFE_ELT (label_vars
->decls_in_scope
, ix
, decl
)
3608 warn_about_goto (loc
, label
, decl
);
3610 if (label_vars
->label_bindings
.left_stmt_expr
)
3612 error_at (loc
, "jump into statement expression");
3613 inform (DECL_SOURCE_LOCATION (label
), "label %qD defined here", label
);
3619 /* Make a label named NAME in the current function, shadowing silently
3620 any that may be inherited from containing functions or containing
3621 scopes. This is called for __label__ declarations. */
3624 declare_label (tree name
)
3626 struct c_binding
*b
= I_LABEL_BINDING (name
);
3628 struct c_label_vars
*label_vars
;
3630 /* Check to make sure that the label hasn't already been declared
3632 if (b
&& B_IN_CURRENT_SCOPE (b
))
3634 error ("duplicate label declaration %qE", name
);
3635 locate_old_decl (b
->decl
);
3637 /* Just use the previous declaration. */
3641 label
= make_label (input_location
, name
, false, &label_vars
);
3642 C_DECLARED_LABEL_FLAG (label
) = 1;
3644 /* Declared labels go in the current scope. */
3645 bind_label (name
, label
, current_scope
, label_vars
);
3650 /* When we define a label, issue any appropriate warnings if there are
3651 any gotos earlier in the function which jump to this label. */
3654 check_earlier_gotos (tree label
, struct c_label_vars
* label_vars
)
3657 struct c_goto_bindings
*g
;
3659 FOR_EACH_VEC_SAFE_ELT (label_vars
->gotos
, ix
, g
)
3661 struct c_binding
*b
;
3662 struct c_scope
*scope
;
3664 /* We have a goto to this label. The goto is going forward. In
3665 g->scope, the goto is going to skip any binding which was
3666 defined after g->bindings_in_scope. */
3667 if (g
->goto_bindings
.scope
->has_jump_unsafe_decl
)
3669 for (b
= g
->goto_bindings
.scope
->bindings
;
3670 b
!= g
->goto_bindings
.bindings_in_scope
;
3673 if (decl_jump_unsafe (b
->decl
))
3674 warn_about_goto (g
->loc
, label
, b
->decl
);
3678 /* We also need to warn about decls defined in any scopes
3679 between the scope of the label and the scope of the goto. */
3680 for (scope
= label_vars
->label_bindings
.scope
;
3681 scope
!= g
->goto_bindings
.scope
;
3682 scope
= scope
->outer
)
3684 gcc_assert (scope
!= NULL
);
3685 if (scope
->has_jump_unsafe_decl
)
3687 if (scope
== label_vars
->label_bindings
.scope
)
3688 b
= label_vars
->label_bindings
.bindings_in_scope
;
3690 b
= scope
->bindings
;
3691 for (; b
!= NULL
; b
= b
->prev
)
3693 if (decl_jump_unsafe (b
->decl
))
3694 warn_about_goto (g
->loc
, label
, b
->decl
);
3699 if (g
->goto_bindings
.stmt_exprs
> 0)
3701 error_at (g
->loc
, "jump into statement expression");
3702 inform (DECL_SOURCE_LOCATION (label
), "label %qD defined here",
3707 /* Now that the label is defined, we will issue warnings about
3708 subsequent gotos to this label when we see them. */
3709 vec_safe_truncate (label_vars
->gotos
, 0);
3710 label_vars
->gotos
= NULL
;
3713 /* Define a label, specifying the location in the source file.
3714 Return the LABEL_DECL node for the label, if the definition is valid.
3715 Otherwise return 0. */
3718 define_label (location_t location
, tree name
)
3720 /* Find any preexisting label with this name. It is an error
3721 if that label has already been defined in this function, or
3722 if there is a containing function with a declared label with
3724 tree label
= I_LABEL_DECL (name
);
3727 && ((DECL_CONTEXT (label
) == current_function_decl
3728 && DECL_INITIAL (label
) != 0)
3729 || (DECL_CONTEXT (label
) != current_function_decl
3730 && C_DECLARED_LABEL_FLAG (label
))))
3732 error_at (location
, "duplicate label %qD", label
);
3733 locate_old_decl (label
);
3736 else if (label
&& DECL_CONTEXT (label
) == current_function_decl
)
3738 struct c_label_vars
*label_vars
= I_LABEL_BINDING (name
)->u
.label
;
3740 /* The label has been used or declared already in this function,
3741 but not defined. Update its location to point to this
3743 DECL_SOURCE_LOCATION (label
) = location
;
3744 set_spot_bindings (&label_vars
->label_bindings
, true);
3746 /* Issue warnings as required about any goto statements from
3747 earlier in the function. */
3748 check_earlier_gotos (label
, label_vars
);
3752 struct c_label_vars
*label_vars
;
3754 /* No label binding for that identifier; make one. */
3755 label
= make_label (location
, name
, true, &label_vars
);
3757 /* Ordinary labels go in the current function scope. */
3758 bind_label (name
, label
, current_function_scope
, label_vars
);
3761 if (!in_system_header_at (input_location
) && lookup_name (name
))
3762 warning_at (location
, OPT_Wtraditional
,
3763 "traditional C lacks a separate namespace "
3764 "for labels, identifier %qE conflicts", name
);
3766 /* Mark label as having been defined. */
3767 DECL_INITIAL (label
) = error_mark_node
;
3771 /* Get the bindings for a new switch statement. This is used to issue
3772 warnings as appropriate for jumps from the switch to case or
3775 struct c_spot_bindings
*
3776 c_get_switch_bindings (void)
3778 struct c_spot_bindings
*switch_bindings
;
3780 switch_bindings
= XNEW (struct c_spot_bindings
);
3781 set_spot_bindings (switch_bindings
, true);
3782 return switch_bindings
;
3786 c_release_switch_bindings (struct c_spot_bindings
*bindings
)
3788 gcc_assert (bindings
->stmt_exprs
== 0 && !bindings
->left_stmt_expr
);
3792 /* This is called at the point of a case or default label to issue
3793 warnings about decls as needed. It returns true if it found an
3794 error, not just a warning. */
3797 c_check_switch_jump_warnings (struct c_spot_bindings
*switch_bindings
,
3798 location_t switch_loc
, location_t case_loc
)
3801 struct c_scope
*scope
;
3804 for (scope
= current_scope
;
3805 scope
!= switch_bindings
->scope
;
3806 scope
= scope
->outer
)
3808 struct c_binding
*b
;
3810 gcc_assert (scope
!= NULL
);
3812 if (!scope
->has_jump_unsafe_decl
)
3815 for (b
= scope
->bindings
; b
!= NULL
; b
= b
->prev
)
3817 if (decl_jump_unsafe (b
->decl
))
3819 if (variably_modified_type_p (TREE_TYPE (b
->decl
), NULL_TREE
))
3823 ("switch jumps into scope of identifier with "
3824 "variably modified type"));
3827 warning_at (case_loc
, OPT_Wjump_misses_init
,
3828 "switch jumps over variable initialization");
3829 inform (switch_loc
, "switch starts here");
3830 inform (DECL_SOURCE_LOCATION (b
->decl
), "%qD declared here",
3836 if (switch_bindings
->stmt_exprs
> 0)
3839 error_at (case_loc
, "switch jumps into statement expression");
3840 inform (switch_loc
, "switch starts here");
3846 /* Given NAME, an IDENTIFIER_NODE,
3847 return the structure (or union or enum) definition for that name.
3848 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3849 CODE says which kind of type the caller wants;
3850 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3851 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3852 location where the tag was defined.
3853 If the wrong kind of type is found, an error is reported. */
3856 lookup_tag (enum tree_code code
, tree name
, bool thislevel_only
,
3859 struct c_binding
*b
= I_TAG_BINDING (name
);
3860 bool thislevel
= false;
3865 /* We only care about whether it's in this level if
3866 thislevel_only was set or it might be a type clash. */
3867 if (thislevel_only
|| TREE_CODE (b
->decl
) != code
)
3869 /* For our purposes, a tag in the external scope is the same as
3870 a tag in the file scope. (Primarily relevant to Objective-C
3871 and its builtin structure tags, which get pushed before the
3872 file scope is created.) */
3873 if (B_IN_CURRENT_SCOPE (b
)
3874 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
3878 if (thislevel_only
&& !thislevel
)
3881 if (TREE_CODE (b
->decl
) != code
)
3883 /* Definition isn't the kind we were looking for. */
3884 pending_invalid_xref
= name
;
3885 pending_invalid_xref_location
= input_location
;
3887 /* If in the same binding level as a declaration as a tag
3888 of a different type, this must not be allowed to
3889 shadow that tag, so give the error immediately.
3890 (For example, "struct foo; union foo;" is invalid.) */
3892 pending_xref_error ();
3901 /* Return true if a definition exists for NAME with code CODE. */
3904 tag_exists_p (enum tree_code code
, tree name
)
3906 struct c_binding
*b
= I_TAG_BINDING (name
);
3908 if (b
== NULL
|| b
->decl
== NULL_TREE
)
3910 return TREE_CODE (b
->decl
) == code
;
3913 /* Print an error message now
3914 for a recent invalid struct, union or enum cross reference.
3915 We don't print them immediately because they are not invalid
3916 when used in the `struct foo;' construct for shadowing. */
3919 pending_xref_error (void)
3921 if (pending_invalid_xref
!= 0)
3922 error_at (pending_invalid_xref_location
, "%qE defined as wrong kind of tag",
3923 pending_invalid_xref
);
3924 pending_invalid_xref
= 0;
3928 /* Look up NAME in the current scope and its superiors
3929 in the namespace of variables, functions and typedefs.
3930 Return a ..._DECL node of some kind representing its definition,
3931 or return 0 if it is undefined. */
3934 lookup_name (tree name
)
3936 struct c_binding
*b
= I_SYMBOL_BINDING (name
);
3937 if (b
&& !b
->invisible
)
3939 maybe_record_typedef_use (b
->decl
);
3945 /* Similar to `lookup_name' but look only at the indicated scope. */
3948 lookup_name_in_scope (tree name
, struct c_scope
*scope
)
3950 struct c_binding
*b
;
3952 for (b
= I_SYMBOL_BINDING (name
); b
; b
= b
->shadowed
)
3953 if (B_IN_SCOPE (b
, scope
))
3958 /* Look for the closest match for NAME within the currently valid
3961 This finds the identifier with the lowest Levenshtein distance to
3962 NAME. If there are multiple candidates with equal minimal distance,
3963 the first one found is returned. Scopes are searched from innermost
3964 outwards, and within a scope in reverse order of declaration, thus
3965 benefiting candidates "near" to the current scope.
3967 The function also looks for similar macro names to NAME, since a
3968 misspelled macro name will not be expanded, and hence looks like an
3969 identifier to the C frontend.
3971 It also looks for start_typename keywords, to detect "singed" vs "signed"
3975 lookup_name_fuzzy (tree name
, enum lookup_name_fuzzy_kind kind
)
3977 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
3979 best_match
<tree
, tree
> bm (name
);
3981 /* Look within currently valid scopes. */
3982 for (c_scope
*scope
= current_scope
; scope
; scope
= scope
->outer
)
3983 for (c_binding
*binding
= scope
->bindings
; binding
; binding
= binding
->prev
)
3985 if (!binding
->id
|| binding
->invisible
)
3987 /* Don't use bindings from implicitly declared functions,
3988 as they were likely misspellings themselves. */
3989 if (TREE_CODE (binding
->decl
) == FUNCTION_DECL
)
3990 if (C_DECL_IMPLICIT (binding
->decl
))
3994 case FUZZY_LOOKUP_TYPENAME
:
3995 if (TREE_CODE (binding
->decl
) != TYPE_DECL
)
3999 case FUZZY_LOOKUP_FUNCTION_NAME
:
4000 if (TREE_CODE (binding
->decl
) != FUNCTION_DECL
)
4002 /* Allow function pointers. */
4003 if ((VAR_P (binding
->decl
)
4004 || TREE_CODE (binding
->decl
) == PARM_DECL
)
4005 && TREE_CODE (TREE_TYPE (binding
->decl
)) == POINTER_TYPE
4006 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding
->decl
)))
4016 bm
.consider (binding
->id
);
4019 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4021 x = SOME_OTHER_MACRO (y);
4022 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4023 as a misspelled identifier.
4025 Use the best distance so far so that a candidate is only set if
4026 a macro is better than anything so far. This allows early rejection
4027 (without calculating the edit distance) of macro names that must have
4028 distance >= bm.get_best_distance (), and means that we only get a
4029 non-NULL result for best_macro_match if it's better than any of
4030 the identifiers already checked, which avoids needless creation
4031 of identifiers for macro hashnodes. */
4032 best_macro_match
bmm (name
, bm
.get_best_distance (), parse_in
);
4033 cpp_hashnode
*best_macro
= bmm
.get_best_meaningful_candidate ();
4034 /* If a macro is the closest so far to NAME, use it, creating an
4035 identifier tree node for it. */
4038 const char *id
= (const char *)best_macro
->ident
.str
;
4039 tree macro_as_identifier
4040 = get_identifier_with_length (id
, best_macro
->ident
.len
);
4041 bm
.set_best_so_far (macro_as_identifier
,
4042 bmm
.get_best_distance (),
4043 bmm
.get_best_candidate_length ());
4046 /* Try the "start_typename" keywords to detect
4047 "singed" vs "signed" typos. */
4048 if (kind
== FUZZY_LOOKUP_TYPENAME
)
4050 for (unsigned i
= 0; i
< num_c_common_reswords
; i
++)
4052 const c_common_resword
*resword
= &c_common_reswords
[i
];
4053 if (!c_keyword_starts_typename (resword
->rid
))
4055 tree resword_identifier
= ridpointers
[resword
->rid
];
4056 if (!resword_identifier
)
4058 gcc_assert (TREE_CODE (resword_identifier
) == IDENTIFIER_NODE
);
4059 bm
.consider (resword_identifier
);
4063 tree best
= bm
.get_best_meaningful_candidate ();
4065 return IDENTIFIER_POINTER (best
);
4071 /* Create the predefined scalar types of C,
4072 and some nodes representing standard constants (0, 1, (void *) 0).
4073 Initialize the global scope.
4074 Make definitions for built-in primitive functions. */
4077 c_init_decl_processing (void)
4079 location_t save_loc
= input_location
;
4081 /* Initialize reserved words for parser. */
4084 current_function_decl
= 0;
4086 gcc_obstack_init (&parser_obstack
);
4088 /* Make the externals scope. */
4090 external_scope
= current_scope
;
4092 /* Declarations from c_common_nodes_and_builtins must not be associated
4093 with this input file, lest we get differences between using and not
4094 using preprocessed headers. */
4095 input_location
= BUILTINS_LOCATION
;
4097 c_common_nodes_and_builtins ();
4099 /* In C, comparisons and TRUTH_* expressions have type int. */
4100 truthvalue_type_node
= integer_type_node
;
4101 truthvalue_true_node
= integer_one_node
;
4102 truthvalue_false_node
= integer_zero_node
;
4104 /* Even in C99, which has a real boolean type. */
4105 pushdecl (build_decl (UNKNOWN_LOCATION
, TYPE_DECL
, get_identifier ("_Bool"),
4106 boolean_type_node
));
4108 input_location
= save_loc
;
4110 make_fname_decl
= c_make_fname_decl
;
4111 start_fname_decls ();
4114 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4115 give the decl, NAME is the initialization string and TYPE_DEP
4116 indicates whether NAME depended on the type of the function. As we
4117 don't yet implement delayed emission of static data, we mark the
4118 decl as emitted so it is not placed in the output. Anything using
4119 it must therefore pull out the STRING_CST initializer directly.
4123 c_make_fname_decl (location_t loc
, tree id
, int type_dep
)
4125 const char *name
= fname_as_string (type_dep
);
4126 tree decl
, type
, init
;
4127 size_t length
= strlen (name
);
4129 type
= build_array_type (char_type_node
,
4130 build_index_type (size_int (length
)));
4131 type
= c_build_qualified_type (type
, TYPE_QUAL_CONST
);
4133 decl
= build_decl (loc
, VAR_DECL
, id
, type
);
4135 TREE_STATIC (decl
) = 1;
4136 TREE_READONLY (decl
) = 1;
4137 DECL_ARTIFICIAL (decl
) = 1;
4139 init
= build_string (length
+ 1, name
);
4140 free (CONST_CAST (char *, name
));
4141 TREE_TYPE (init
) = type
;
4142 DECL_INITIAL (decl
) = init
;
4144 TREE_USED (decl
) = 1;
4146 if (current_function_decl
4147 /* For invalid programs like this:
4150 const char* p = __FUNCTION__;
4152 the __FUNCTION__ is believed to appear in K&R style function
4153 parameter declarator. In that case we still don't have
4155 && current_function_scope
)
4157 DECL_CONTEXT (decl
) = current_function_decl
;
4158 bind (id
, decl
, current_function_scope
,
4159 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
4162 finish_decl (decl
, loc
, init
, NULL_TREE
, NULL_TREE
);
4168 c_builtin_function (tree decl
)
4170 tree type
= TREE_TYPE (decl
);
4171 tree id
= DECL_NAME (decl
);
4173 const char *name
= IDENTIFIER_POINTER (id
);
4174 C_DECL_BUILTIN_PROTOTYPE (decl
) = prototype_p (type
);
4176 /* Should never be called on a symbol with a preexisting meaning. */
4177 gcc_assert (!I_SYMBOL_BINDING (id
));
4179 bind (id
, decl
, external_scope
, /*invisible=*/true, /*nested=*/false,
4182 /* Builtins in the implementation namespace are made visible without
4183 needing to be explicitly declared. See push_file_scope. */
4184 if (name
[0] == '_' && (name
[1] == '_' || ISUPPER (name
[1])))
4186 DECL_CHAIN (decl
) = visible_builtins
;
4187 visible_builtins
= decl
;
4194 c_builtin_function_ext_scope (tree decl
)
4196 tree type
= TREE_TYPE (decl
);
4197 tree id
= DECL_NAME (decl
);
4199 const char *name
= IDENTIFIER_POINTER (id
);
4200 C_DECL_BUILTIN_PROTOTYPE (decl
) = prototype_p (type
);
4203 bind (id
, decl
, external_scope
, /*invisible=*/false, /*nested=*/false,
4206 /* Builtins in the implementation namespace are made visible without
4207 needing to be explicitly declared. See push_file_scope. */
4208 if (name
[0] == '_' && (name
[1] == '_' || ISUPPER (name
[1])))
4210 DECL_CHAIN (decl
) = visible_builtins
;
4211 visible_builtins
= decl
;
4217 /* Called when a declaration is seen that contains no names to declare.
4218 If its type is a reference to a structure, union or enum inherited
4219 from a containing scope, shadow that tag name for the current scope
4220 with a forward reference.
4221 If its type defines a new named structure or union
4222 or defines an enum, it is valid but we need not do anything here.
4223 Otherwise, it is an error. */
4226 shadow_tag (const struct c_declspecs
*declspecs
)
4228 shadow_tag_warned (declspecs
, 0);
4231 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4234 shadow_tag_warned (const struct c_declspecs
*declspecs
, int warned
)
4236 bool found_tag
= false;
4238 if (declspecs
->type
&& !declspecs
->default_int_p
&& !declspecs
->typedef_p
)
4240 tree value
= declspecs
->type
;
4241 enum tree_code code
= TREE_CODE (value
);
4243 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
|| code
== ENUMERAL_TYPE
)
4244 /* Used to test also that TYPE_SIZE (value) != 0.
4245 That caused warning for `struct foo;' at top level in the file. */
4247 tree name
= TYPE_NAME (value
);
4252 if (declspecs
->restrict_p
)
4254 error ("invalid use of %<restrict%>");
4260 if (warned
!= 1 && code
!= ENUMERAL_TYPE
)
4261 /* Empty unnamed enum OK */
4263 pedwarn (input_location
, 0,
4264 "unnamed struct/union that defines no instances");
4268 else if (declspecs
->typespec_kind
!= ctsk_tagdef
4269 && declspecs
->typespec_kind
!= ctsk_tagfirstref
4270 && declspecs
->storage_class
!= csc_none
)
4273 pedwarn (input_location
, 0,
4274 "empty declaration with storage class specifier "
4275 "does not redeclare tag");
4277 pending_xref_error ();
4279 else if (declspecs
->typespec_kind
!= ctsk_tagdef
4280 && declspecs
->typespec_kind
!= ctsk_tagfirstref
4281 && (declspecs
->const_p
4282 || declspecs
->volatile_p
4283 || declspecs
->atomic_p
4284 || declspecs
->restrict_p
4285 || declspecs
->address_space
))
4288 pedwarn (input_location
, 0,
4289 "empty declaration with type qualifier "
4290 "does not redeclare tag");
4292 pending_xref_error ();
4294 else if (declspecs
->typespec_kind
!= ctsk_tagdef
4295 && declspecs
->typespec_kind
!= ctsk_tagfirstref
4296 && declspecs
->alignas_p
)
4299 pedwarn (input_location
, 0,
4300 "empty declaration with %<_Alignas%> "
4301 "does not redeclare tag");
4303 pending_xref_error ();
4307 pending_invalid_xref
= 0;
4308 t
= lookup_tag (code
, name
, true, NULL
);
4312 t
= make_node (code
);
4313 pushtag (input_location
, name
, t
);
4319 if (warned
!= 1 && !in_system_header_at (input_location
))
4321 pedwarn (input_location
, 0,
4322 "useless type name in empty declaration");
4327 else if (warned
!= 1 && !in_system_header_at (input_location
)
4328 && declspecs
->typedef_p
)
4330 pedwarn (input_location
, 0, "useless type name in empty declaration");
4334 pending_invalid_xref
= 0;
4336 if (declspecs
->inline_p
)
4338 error ("%<inline%> in empty declaration");
4342 if (declspecs
->noreturn_p
)
4344 error ("%<_Noreturn%> in empty declaration");
4348 if (current_scope
== file_scope
&& declspecs
->storage_class
== csc_auto
)
4350 error ("%<auto%> in file-scope empty declaration");
4354 if (current_scope
== file_scope
&& declspecs
->storage_class
== csc_register
)
4356 error ("%<register%> in file-scope empty declaration");
4360 if (!warned
&& !in_system_header_at (input_location
)
4361 && declspecs
->storage_class
!= csc_none
)
4363 warning (0, "useless storage class specifier in empty declaration");
4367 if (!warned
&& !in_system_header_at (input_location
) && declspecs
->thread_p
)
4369 warning (0, "useless %qs in empty declaration",
4370 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
4375 && !in_system_header_at (input_location
)
4376 && (declspecs
->const_p
4377 || declspecs
->volatile_p
4378 || declspecs
->atomic_p
4379 || declspecs
->restrict_p
4380 || declspecs
->address_space
))
4382 warning (0, "useless type qualifier in empty declaration");
4386 if (!warned
&& !in_system_header_at (input_location
)
4387 && declspecs
->alignas_p
)
4389 warning (0, "useless %<_Alignas%> in empty declaration");
4396 pedwarn (input_location
, 0, "empty declaration");
4401 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4402 bits. SPECS represents declaration specifiers that the grammar
4403 only permits to contain type qualifiers and attributes. */
4406 quals_from_declspecs (const struct c_declspecs
*specs
)
4408 int quals
= ((specs
->const_p
? TYPE_QUAL_CONST
: 0)
4409 | (specs
->volatile_p
? TYPE_QUAL_VOLATILE
: 0)
4410 | (specs
->restrict_p
? TYPE_QUAL_RESTRICT
: 0)
4411 | (specs
->atomic_p
? TYPE_QUAL_ATOMIC
: 0)
4412 | (ENCODE_QUAL_ADDR_SPACE (specs
->address_space
)));
4413 gcc_assert (!specs
->type
4414 && !specs
->decl_attr
4415 && specs
->typespec_word
== cts_none
4416 && specs
->storage_class
== csc_none
4417 && !specs
->typedef_p
4418 && !specs
->explicit_signed_p
4419 && !specs
->deprecated_p
4421 && !specs
->long_long_p
4424 && !specs
->unsigned_p
4425 && !specs
->complex_p
4427 && !specs
->noreturn_p
4428 && !specs
->thread_p
);
4432 /* Construct an array declarator. LOC is the location of the
4433 beginning of the array (usually the opening brace). EXPR is the
4434 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4435 inside the [] (to be applied to the pointer to which a parameter
4436 array is converted). STATIC_P is true if "static" is inside the
4437 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4438 VLA of unspecified length which is nevertheless a complete type,
4439 false otherwise. The field for the contained declarator is left to
4440 be filled in by set_array_declarator_inner. */
4442 struct c_declarator
*
4443 build_array_declarator (location_t loc
,
4444 tree expr
, struct c_declspecs
*quals
, bool static_p
,
4447 struct c_declarator
*declarator
= XOBNEW (&parser_obstack
,
4448 struct c_declarator
);
4449 declarator
->id_loc
= loc
;
4450 declarator
->kind
= cdk_array
;
4451 declarator
->declarator
= 0;
4452 declarator
->u
.array
.dimen
= expr
;
4455 declarator
->u
.array
.attrs
= quals
->attrs
;
4456 declarator
->u
.array
.quals
= quals_from_declspecs (quals
);
4460 declarator
->u
.array
.attrs
= NULL_TREE
;
4461 declarator
->u
.array
.quals
= 0;
4463 declarator
->u
.array
.static_p
= static_p
;
4464 declarator
->u
.array
.vla_unspec_p
= vla_unspec_p
;
4465 if (static_p
|| quals
!= NULL
)
4466 pedwarn_c90 (loc
, OPT_Wpedantic
,
4467 "ISO C90 does not support %<static%> or type "
4468 "qualifiers in parameter array declarators");
4470 pedwarn_c90 (loc
, OPT_Wpedantic
,
4471 "ISO C90 does not support %<[*]%> array declarators");
4474 if (!current_scope
->parm_flag
)
4477 error_at (loc
, "%<[*]%> not allowed in other than "
4478 "function prototype scope");
4479 declarator
->u
.array
.vla_unspec_p
= false;
4482 current_scope
->had_vla_unspec
= true;
4487 /* Set the contained declarator of an array declarator. DECL is the
4488 declarator, as constructed by build_array_declarator; INNER is what
4489 appears on the left of the []. */
4491 struct c_declarator
*
4492 set_array_declarator_inner (struct c_declarator
*decl
,
4493 struct c_declarator
*inner
)
4495 decl
->declarator
= inner
;
4499 /* INIT is a constructor that forms DECL's initializer. If the final
4500 element initializes a flexible array field, add the size of that
4501 initializer to DECL's size. */
4504 add_flexible_array_elts_to_size (tree decl
, tree init
)
4508 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
4511 elt
= CONSTRUCTOR_ELTS (init
)->last ().value
;
4512 type
= TREE_TYPE (elt
);
4513 if (TREE_CODE (type
) == ARRAY_TYPE
4514 && TYPE_SIZE (type
) == NULL_TREE
4515 && TYPE_DOMAIN (type
) != NULL_TREE
4516 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
)
4518 complete_array_type (&type
, elt
, false);
4520 = size_binop (PLUS_EXPR
, DECL_SIZE (decl
), TYPE_SIZE (type
));
4521 DECL_SIZE_UNIT (decl
)
4522 = size_binop (PLUS_EXPR
, DECL_SIZE_UNIT (decl
), TYPE_SIZE_UNIT (type
));
4526 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4527 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4528 before the type name, and set *EXPR_CONST_OPERANDS, if
4529 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4530 appear in a constant expression. */
4533 groktypename (struct c_type_name
*type_name
, tree
*expr
,
4534 bool *expr_const_operands
)
4537 tree attrs
= type_name
->specs
->attrs
;
4539 type_name
->specs
->attrs
= NULL_TREE
;
4541 type
= grokdeclarator (type_name
->declarator
, type_name
->specs
, TYPENAME
,
4542 false, NULL
, &attrs
, expr
, expr_const_operands
,
4545 /* Apply attributes. */
4546 decl_attributes (&type
, attrs
, 0);
4551 /* Wrapper for decl_attributes that adds some implicit attributes
4552 to VAR_DECLs or FUNCTION_DECLs. */
4555 c_decl_attributes (tree
*node
, tree attributes
, int flags
)
4557 /* Add implicit "omp declare target" attribute if requested. */
4558 if (current_omp_declare_target_attribute
4559 && ((VAR_P (*node
) && is_global_var (*node
))
4560 || TREE_CODE (*node
) == FUNCTION_DECL
))
4563 && !lang_hooks
.types
.omp_mappable_type (TREE_TYPE (*node
)))
4564 error ("%q+D in declare target directive does not have mappable type",
4567 attributes
= tree_cons (get_identifier ("omp declare target"),
4568 NULL_TREE
, attributes
);
4570 return decl_attributes (node
, attributes
, flags
);
4574 /* Decode a declarator in an ordinary declaration or data definition.
4575 This is called as soon as the type information and variable name
4576 have been parsed, before parsing the initializer if any.
4577 Here we create the ..._DECL node, fill in its type,
4578 and put it on the list of decls for the current context.
4579 The ..._DECL node is returned as the value.
4581 Exception: for arrays where the length is not specified,
4582 the type is left null, to be filled in by `finish_decl'.
4584 Function definitions do not come here; they go to start_function
4585 instead. However, external and forward declarations of functions
4586 do go through here. Structure field declarations are done by
4587 grokfield and not through here. */
4590 start_decl (struct c_declarator
*declarator
, struct c_declspecs
*declspecs
,
4591 bool initialized
, tree attributes
)
4595 tree expr
= NULL_TREE
;
4596 enum deprecated_states deprecated_state
= DEPRECATED_NORMAL
;
4598 /* An object declared as __attribute__((deprecated)) suppresses
4599 warnings of uses of other deprecated items. */
4600 if (lookup_attribute ("deprecated", attributes
))
4601 deprecated_state
= DEPRECATED_SUPPRESS
;
4603 decl
= grokdeclarator (declarator
, declspecs
,
4604 NORMAL
, initialized
, NULL
, &attributes
, &expr
, NULL
,
4606 if (!decl
|| decl
== error_mark_node
)
4610 add_stmt (fold_convert (void_type_node
, expr
));
4612 if (TREE_CODE (decl
) != FUNCTION_DECL
&& MAIN_NAME_P (DECL_NAME (decl
)))
4613 warning (OPT_Wmain
, "%q+D is usually a function", decl
);
4616 /* Is it valid for this decl to have an initializer at all?
4617 If not, set INITIALIZED to zero, which will indirectly
4618 tell 'finish_decl' to ignore the initializer once it is parsed. */
4619 switch (TREE_CODE (decl
))
4622 error ("typedef %qD is initialized (use __typeof__ instead)", decl
);
4627 error ("function %qD is initialized like a variable", decl
);
4632 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4633 error ("parameter %qD is initialized", decl
);
4638 /* Don't allow initializations for incomplete types except for
4639 arrays which might be completed by the initialization. */
4641 /* This can happen if the array size is an undefined macro.
4642 We already gave a warning, so we don't need another one. */
4643 if (TREE_TYPE (decl
) == error_mark_node
)
4645 else if (COMPLETE_TYPE_P (TREE_TYPE (decl
)))
4647 /* A complete type is ok if size is fixed. */
4649 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl
))) != INTEGER_CST
4650 || C_DECL_VARIABLE_SIZE (decl
))
4652 error ("variable-sized object may not be initialized");
4656 else if (TREE_CODE (TREE_TYPE (decl
)) != ARRAY_TYPE
)
4658 error ("variable %qD has initializer but incomplete type", decl
);
4661 else if (C_DECL_VARIABLE_SIZE (decl
))
4663 /* Although C99 is unclear about whether incomplete arrays
4664 of VLAs themselves count as VLAs, it does not make
4665 sense to permit them to be initialized given that
4666 ordinary VLAs may not be initialized. */
4667 error ("variable-sized object may not be initialized");
4674 if (current_scope
== file_scope
)
4675 TREE_STATIC (decl
) = 1;
4677 /* Tell 'pushdecl' this is an initialized decl
4678 even though we don't yet have the initializer expression.
4679 Also tell 'finish_decl' it may store the real initializer. */
4680 DECL_INITIAL (decl
) = error_mark_node
;
4683 /* If this is a function declaration, write a record describing it to the
4684 prototypes file (if requested). */
4686 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4687 gen_aux_info_record (decl
, 0, 0, prototype_p (TREE_TYPE (decl
)));
4689 /* ANSI specifies that a tentative definition which is not merged with
4690 a non-tentative definition behaves exactly like a definition with an
4691 initializer equal to zero. (Section 3.7.2)
4693 -fno-common gives strict ANSI behavior, though this tends to break
4694 a large body of code that grew up without this rule.
4696 Thread-local variables are never common, since there's no entrenched
4697 body of code to break, and it allows more efficient variable references
4698 in the presence of dynamic linking. */
4702 && TREE_PUBLIC (decl
)
4703 && !DECL_THREAD_LOCAL_P (decl
)
4705 DECL_COMMON (decl
) = 1;
4707 /* Set attributes here so if duplicate decl, will have proper attributes. */
4708 c_decl_attributes (&decl
, attributes
, 0);
4710 /* Handle gnu_inline attribute. */
4711 if (declspecs
->inline_p
4712 && !flag_gnu89_inline
4713 && TREE_CODE (decl
) == FUNCTION_DECL
4714 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl
))
4715 || current_function_decl
))
4717 if (declspecs
->storage_class
== csc_auto
&& current_scope
!= file_scope
)
4719 else if (declspecs
->storage_class
!= csc_static
)
4720 DECL_EXTERNAL (decl
) = !DECL_EXTERNAL (decl
);
4723 if (TREE_CODE (decl
) == FUNCTION_DECL
4724 && targetm
.calls
.promote_prototypes (TREE_TYPE (decl
)))
4726 struct c_declarator
*ce
= declarator
;
4728 if (ce
->kind
== cdk_pointer
)
4729 ce
= declarator
->declarator
;
4730 if (ce
->kind
== cdk_function
)
4732 tree args
= ce
->u
.arg_info
->parms
;
4733 for (; args
; args
= DECL_CHAIN (args
))
4735 tree type
= TREE_TYPE (args
);
4736 if (type
&& INTEGRAL_TYPE_P (type
)
4737 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
4738 DECL_ARG_TYPE (args
) = c_type_promotes_to (type
);
4743 if (TREE_CODE (decl
) == FUNCTION_DECL
4744 && DECL_DECLARED_INLINE_P (decl
)
4745 && DECL_UNINLINABLE (decl
)
4746 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl
)))
4747 warning (OPT_Wattributes
, "inline function %q+D given attribute noinline",
4750 /* C99 6.7.4p3: An inline definition of a function with external
4751 linkage shall not contain a definition of a modifiable object
4752 with static storage duration... */
4754 && current_scope
!= file_scope
4755 && TREE_STATIC (decl
)
4756 && !TREE_READONLY (decl
)
4757 && DECL_DECLARED_INLINE_P (current_function_decl
)
4758 && DECL_EXTERNAL (current_function_decl
))
4759 record_inline_static (input_location
, current_function_decl
,
4760 decl
, csi_modifiable
);
4762 if (c_dialect_objc ()
4763 && VAR_OR_FUNCTION_DECL_P (decl
))
4764 objc_check_global_decl (decl
);
4766 /* Add this decl to the current scope.
4767 TEM may equal DECL or it may be a previous decl of the same name. */
4768 tem
= pushdecl (decl
);
4770 if (initialized
&& DECL_EXTERNAL (tem
))
4772 DECL_EXTERNAL (tem
) = 0;
4773 TREE_STATIC (tem
) = 1;
4779 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4780 DECL or the non-array element type if DECL is an uninitialized array.
4781 If that type has a const member, diagnose this. */
4784 diagnose_uninitialized_cst_member (tree decl
, tree type
)
4787 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
4790 if (TREE_CODE (field
) != FIELD_DECL
)
4792 field_type
= strip_array_types (TREE_TYPE (field
));
4794 if (TYPE_QUALS (field_type
) & TYPE_QUAL_CONST
)
4796 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
4797 "uninitialized const member in %qT is invalid in C++",
4798 strip_array_types (TREE_TYPE (decl
)));
4799 inform (DECL_SOURCE_LOCATION (field
), "%qD should be initialized", field
);
4802 if (RECORD_OR_UNION_TYPE_P (field_type
))
4803 diagnose_uninitialized_cst_member (decl
, field_type
);
4807 /* Finish processing of a declaration;
4808 install its initial value.
4809 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4810 If the length of an array type is not known before,
4811 it must be determined now, from the initial value, or it is an error.
4813 INIT_LOC is the location of the initial value. */
4816 finish_decl (tree decl
, location_t init_loc
, tree init
,
4817 tree origtype
, tree asmspec_tree
)
4820 bool was_incomplete
= (DECL_SIZE (decl
) == 0);
4821 const char *asmspec
= 0;
4823 /* If a name was specified, get the string. */
4824 if (VAR_OR_FUNCTION_DECL_P (decl
)
4825 && DECL_FILE_SCOPE_P (decl
))
4826 asmspec_tree
= maybe_apply_renaming_pragma (decl
, asmspec_tree
);
4828 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
4831 && TREE_STATIC (decl
)
4832 && global_bindings_p ())
4833 /* So decl is a global variable. Record the types it uses
4834 so that we can decide later to emit debug info for them. */
4835 record_types_used_by_current_var_decl (decl
);
4837 /* If `start_decl' didn't like having an initialization, ignore it now. */
4838 if (init
!= 0 && DECL_INITIAL (decl
) == 0)
4841 /* Don't crash if parm is initialized. */
4842 if (TREE_CODE (decl
) == PARM_DECL
)
4846 store_init_value (init_loc
, decl
, init
, origtype
);
4848 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl
)
4849 || TREE_CODE (decl
) == FIELD_DECL
))
4850 objc_check_decl (decl
);
4852 type
= TREE_TYPE (decl
);
4854 /* Deduce size of array from initialization, if not already known. */
4855 if (TREE_CODE (type
) == ARRAY_TYPE
4856 && TYPE_DOMAIN (type
) == 0
4857 && TREE_CODE (decl
) != TYPE_DECL
)
4860 = (TREE_STATIC (decl
)
4861 /* Even if pedantic, an external linkage array
4862 may have incomplete type at first. */
4863 ? pedantic
&& !TREE_PUBLIC (decl
)
4864 : !DECL_EXTERNAL (decl
));
4866 = complete_array_type (&TREE_TYPE (decl
), DECL_INITIAL (decl
),
4869 /* Get the completed type made by complete_array_type. */
4870 type
= TREE_TYPE (decl
);
4875 error ("initializer fails to determine size of %q+D", decl
);
4880 error ("array size missing in %q+D", decl
);
4881 /* If a `static' var's size isn't known,
4882 make it extern as well as static, so it does not get
4884 If it is not `static', then do not mark extern;
4885 finish_incomplete_decl will give it a default size
4886 and it will get allocated. */
4887 else if (!pedantic
&& TREE_STATIC (decl
) && !TREE_PUBLIC (decl
))
4888 DECL_EXTERNAL (decl
) = 1;
4892 error ("zero or negative size array %q+D", decl
);
4896 /* For global variables, update the copy of the type that
4897 exists in the binding. */
4898 if (TREE_PUBLIC (decl
))
4900 struct c_binding
*b_ext
= I_SYMBOL_BINDING (DECL_NAME (decl
));
4901 while (b_ext
&& !B_IN_EXTERNAL_SCOPE (b_ext
))
4902 b_ext
= b_ext
->shadowed
;
4903 if (b_ext
&& TREE_CODE (decl
) == TREE_CODE (b_ext
->decl
))
4905 if (b_ext
->u
.type
&& comptypes (b_ext
->u
.type
, type
))
4906 b_ext
->u
.type
= composite_type (b_ext
->u
.type
, type
);
4908 b_ext
->u
.type
= type
;
4917 if (DECL_INITIAL (decl
))
4918 TREE_TYPE (DECL_INITIAL (decl
)) = type
;
4920 relayout_decl (decl
);
4925 if (init
&& TREE_CODE (init
) == CONSTRUCTOR
)
4926 add_flexible_array_elts_to_size (decl
, init
);
4928 if (DECL_SIZE (decl
) == 0 && TREE_TYPE (decl
) != error_mark_node
4929 && COMPLETE_TYPE_P (TREE_TYPE (decl
)))
4930 layout_decl (decl
, 0);
4932 if (DECL_SIZE (decl
) == 0
4933 /* Don't give an error if we already gave one earlier. */
4934 && TREE_TYPE (decl
) != error_mark_node
4935 && (TREE_STATIC (decl
)
4936 /* A static variable with an incomplete type
4937 is an error if it is initialized.
4938 Also if it is not file scope.
4939 Otherwise, let it through, but if it is not `extern'
4940 then it may cause an error message later. */
4941 ? (DECL_INITIAL (decl
) != 0
4942 || !DECL_FILE_SCOPE_P (decl
))
4943 /* An automatic variable with an incomplete type
4945 : !DECL_EXTERNAL (decl
)))
4947 error ("storage size of %q+D isn%'t known", decl
);
4948 TREE_TYPE (decl
) = error_mark_node
;
4951 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
))
4952 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
4953 && DECL_SIZE (decl
) == NULL_TREE
4954 && TREE_STATIC (decl
))
4955 incomplete_record_decls
.safe_push (decl
);
4957 if (is_global_var (decl
) && DECL_SIZE (decl
) != 0)
4959 if (TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
)
4960 constant_expression_warning (DECL_SIZE (decl
));
4963 error ("storage size of %q+D isn%'t constant", decl
);
4964 TREE_TYPE (decl
) = error_mark_node
;
4968 if (TREE_USED (type
))
4970 TREE_USED (decl
) = 1;
4971 DECL_READ_P (decl
) = 1;
4975 /* If this is a function and an assembler name is specified, reset DECL_RTL
4976 so we can give it its new name. Also, update builtin_decl if it
4977 was a normal built-in. */
4978 if (TREE_CODE (decl
) == FUNCTION_DECL
&& asmspec
)
4980 if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
4981 set_builtin_user_assembler_name (decl
, asmspec
);
4982 set_user_assembler_name (decl
, asmspec
);
4985 /* If #pragma weak was used, mark the decl weak now. */
4986 maybe_apply_pragma_weak (decl
);
4988 /* Output the assembler code and/or RTL code for variables and functions,
4989 unless the type is an undefined structure or union.
4990 If not, it will get done when the type is completed. */
4992 if (VAR_OR_FUNCTION_DECL_P (decl
))
4994 /* Determine the ELF visibility. */
4995 if (TREE_PUBLIC (decl
))
4996 c_determine_visibility (decl
);
4998 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4999 if (c_dialect_objc ())
5000 objc_check_decl (decl
);
5004 /* If this is not a static variable, issue a warning.
5005 It doesn't make any sense to give an ASMSPEC for an
5006 ordinary, non-register local variable. Historically,
5007 GCC has accepted -- but ignored -- the ASMSPEC in
5009 if (!DECL_FILE_SCOPE_P (decl
)
5011 && !C_DECL_REGISTER (decl
)
5012 && !TREE_STATIC (decl
))
5013 warning (0, "ignoring asm-specifier for non-static local "
5014 "variable %q+D", decl
);
5016 set_user_assembler_name (decl
, asmspec
);
5019 if (DECL_FILE_SCOPE_P (decl
))
5021 if (DECL_INITIAL (decl
) == NULL_TREE
5022 || DECL_INITIAL (decl
) == error_mark_node
)
5023 /* Don't output anything
5024 when a tentative file-scope definition is seen.
5025 But at end of compilation, do output code for them. */
5026 DECL_DEFER_OUTPUT (decl
) = 1;
5027 if (asmspec
&& C_DECL_REGISTER (decl
))
5028 DECL_HARD_REGISTER (decl
) = 1;
5029 rest_of_decl_compilation (decl
, true, 0);
5033 /* In conjunction with an ASMSPEC, the `register'
5034 keyword indicates that we should place the variable
5035 in a particular register. */
5036 if (asmspec
&& C_DECL_REGISTER (decl
))
5038 DECL_HARD_REGISTER (decl
) = 1;
5039 /* This cannot be done for a structure with volatile
5040 fields, on which DECL_REGISTER will have been
5042 if (!DECL_REGISTER (decl
))
5043 error ("cannot put object with volatile field into register");
5046 if (TREE_CODE (decl
) != FUNCTION_DECL
)
5048 /* If we're building a variable sized type, and we might be
5049 reachable other than via the top of the current binding
5050 level, then create a new BIND_EXPR so that we deallocate
5051 the object at the right time. */
5052 /* Note that DECL_SIZE can be null due to errors. */
5053 if (DECL_SIZE (decl
)
5054 && !TREE_CONSTANT (DECL_SIZE (decl
))
5055 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list
))
5058 bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, NULL
);
5059 TREE_SIDE_EFFECTS (bind
) = 1;
5061 BIND_EXPR_BODY (bind
) = push_stmt_list ();
5063 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl
),
5069 if (!DECL_FILE_SCOPE_P (decl
))
5071 /* Recompute the RTL of a local array now
5072 if it used to be an incomplete type. */
5073 if (was_incomplete
&& !is_global_var (decl
))
5075 /* If we used it already as memory, it must stay in memory. */
5076 TREE_ADDRESSABLE (decl
) = TREE_USED (decl
);
5077 /* If it's still incomplete now, no init will save it. */
5078 if (DECL_SIZE (decl
) == 0)
5079 DECL_INITIAL (decl
) = 0;
5084 if (TREE_CODE (decl
) == TYPE_DECL
)
5086 if (!DECL_FILE_SCOPE_P (decl
)
5087 && variably_modified_type_p (TREE_TYPE (decl
), NULL_TREE
))
5088 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl
), DECL_EXPR
, decl
));
5090 rest_of_decl_compilation (decl
, DECL_FILE_SCOPE_P (decl
), 0);
5093 /* Install a cleanup (aka destructor) if one was given. */
5094 if (VAR_P (decl
) && !TREE_STATIC (decl
))
5096 tree attr
= lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl
));
5099 tree cleanup_id
= TREE_VALUE (TREE_VALUE (attr
));
5100 tree cleanup_decl
= lookup_name (cleanup_id
);
5102 vec
<tree
, va_gc
> *v
;
5104 /* Build "cleanup(&decl)" for the destructor. */
5105 cleanup
= build_unary_op (input_location
, ADDR_EXPR
, decl
, 0);
5107 v
->quick_push (cleanup
);
5108 cleanup
= c_build_function_call_vec (DECL_SOURCE_LOCATION (decl
),
5109 vNULL
, cleanup_decl
, v
, NULL
);
5112 /* Don't warn about decl unused; the cleanup uses it. */
5113 TREE_USED (decl
) = 1;
5114 TREE_USED (cleanup_decl
) = 1;
5115 DECL_READ_P (decl
) = 1;
5117 push_cleanup (decl
, cleanup
, false);
5123 && !DECL_EXTERNAL (decl
)
5124 && DECL_INITIAL (decl
) == NULL_TREE
)
5126 type
= strip_array_types (type
);
5127 if (TREE_READONLY (decl
))
5128 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
5129 "uninitialized const %qD is invalid in C++", decl
);
5130 else if (RECORD_OR_UNION_TYPE_P (type
)
5131 && C_TYPE_FIELDS_READONLY (type
))
5132 diagnose_uninitialized_cst_member (decl
, type
);
5135 invoke_plugin_callbacks (PLUGIN_FINISH_DECL
, decl
);
5138 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5139 EXPR is NULL or a pointer to an expression that needs to be
5140 evaluated for the side effects of array size expressions in the
5144 grokparm (const struct c_parm
*parm
, tree
*expr
)
5146 tree attrs
= parm
->attrs
;
5147 tree decl
= grokdeclarator (parm
->declarator
, parm
->specs
, PARM
, false,
5148 NULL
, &attrs
, expr
, NULL
, DEPRECATED_NORMAL
);
5150 decl_attributes (&decl
, attrs
, 0);
5155 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5156 and push that on the current scope. EXPR is a pointer to an
5157 expression that needs to be evaluated for the side effects of array
5158 size expressions in the parameters. */
5161 push_parm_decl (const struct c_parm
*parm
, tree
*expr
)
5163 tree attrs
= parm
->attrs
;
5166 decl
= grokdeclarator (parm
->declarator
, parm
->specs
, PARM
, false, NULL
,
5167 &attrs
, expr
, NULL
, DEPRECATED_NORMAL
);
5168 decl_attributes (&decl
, attrs
, 0);
5170 decl
= pushdecl (decl
);
5172 finish_decl (decl
, input_location
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
5175 /* Mark all the parameter declarations to date as forward decls.
5176 Also diagnose use of this extension. */
5179 mark_forward_parm_decls (void)
5181 struct c_binding
*b
;
5183 if (pedantic
&& !current_scope
->warned_forward_parm_decls
)
5185 pedwarn (input_location
, OPT_Wpedantic
,
5186 "ISO C forbids forward parameter declarations");
5187 current_scope
->warned_forward_parm_decls
= true;
5190 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
5191 if (TREE_CODE (b
->decl
) == PARM_DECL
)
5192 TREE_ASM_WRITTEN (b
->decl
) = 1;
5195 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5196 literal, which may be an incomplete array type completed by the
5197 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5198 literal. NON_CONST is true if the initializers contain something
5199 that cannot occur in a constant expression. */
5202 build_compound_literal (location_t loc
, tree type
, tree init
, bool non_const
)
5204 /* We do not use start_decl here because we have a type, not a declarator;
5205 and do not use finish_decl because the decl should be stored inside
5206 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5211 if (type
== error_mark_node
5212 || init
== error_mark_node
)
5213 return error_mark_node
;
5215 decl
= build_decl (loc
, VAR_DECL
, NULL_TREE
, type
);
5216 DECL_EXTERNAL (decl
) = 0;
5217 TREE_PUBLIC (decl
) = 0;
5218 TREE_STATIC (decl
) = (current_scope
== file_scope
);
5219 DECL_CONTEXT (decl
) = current_function_decl
;
5220 TREE_USED (decl
) = 1;
5221 DECL_READ_P (decl
) = 1;
5222 TREE_TYPE (decl
) = type
;
5223 TREE_READONLY (decl
) = (TYPE_READONLY (type
)
5224 || (TREE_CODE (type
) == ARRAY_TYPE
5225 && TYPE_READONLY (TREE_TYPE (type
))));
5226 store_init_value (loc
, decl
, init
, NULL_TREE
);
5228 if (TREE_CODE (type
) == ARRAY_TYPE
&& !COMPLETE_TYPE_P (type
))
5230 int failure
= complete_array_type (&TREE_TYPE (decl
),
5231 DECL_INITIAL (decl
), true);
5232 /* If complete_array_type returns 3, it means that the
5233 initial value of the compound literal is empty. Allow it. */
5234 gcc_assert (failure
== 0 || failure
== 3);
5236 type
= TREE_TYPE (decl
);
5237 TREE_TYPE (DECL_INITIAL (decl
)) = type
;
5240 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
5242 c_incomplete_type_error (loc
, NULL_TREE
, type
);
5243 return error_mark_node
;
5246 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), DECL_EXPR
, decl
);
5247 complit
= build1 (COMPOUND_LITERAL_EXPR
, type
, stmt
);
5248 TREE_SIDE_EFFECTS (complit
) = 1;
5250 layout_decl (decl
, 0);
5252 if (TREE_STATIC (decl
))
5254 /* This decl needs a name for the assembler output. */
5255 set_compound_literal_name (decl
);
5256 DECL_DEFER_OUTPUT (decl
) = 1;
5257 DECL_COMDAT (decl
) = 1;
5258 DECL_ARTIFICIAL (decl
) = 1;
5259 DECL_IGNORED_P (decl
) = 1;
5261 rest_of_decl_compilation (decl
, 1, 0);
5266 complit
= build2 (C_MAYBE_CONST_EXPR
, type
, NULL
, complit
);
5267 C_MAYBE_CONST_EXPR_NON_CONST (complit
) = 1;
5273 /* Check the type of a compound literal. Here we just check that it
5274 is valid for C++. */
5277 check_compound_literal_type (location_t loc
, struct c_type_name
*type_name
)
5280 && (type_name
->specs
->typespec_kind
== ctsk_tagdef
5281 || type_name
->specs
->typespec_kind
== ctsk_tagfirstref
))
5282 warning_at (loc
, OPT_Wc___compat
,
5283 "defining a type in a compound literal is invalid in C++");
5286 /* Determine whether TYPE is a structure with a flexible array member,
5287 or a union containing such a structure (possibly recursively). */
5290 flexible_array_type_p (tree type
)
5293 switch (TREE_CODE (type
))
5296 x
= TYPE_FIELDS (type
);
5299 while (DECL_CHAIN (x
) != NULL_TREE
)
5301 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
5302 && TYPE_SIZE (TREE_TYPE (x
)) == NULL_TREE
5303 && TYPE_DOMAIN (TREE_TYPE (x
)) != NULL_TREE
5304 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x
))) == NULL_TREE
)
5308 for (x
= TYPE_FIELDS (type
); x
!= NULL_TREE
; x
= DECL_CHAIN (x
))
5310 if (flexible_array_type_p (TREE_TYPE (x
)))
5319 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5320 replacing with appropriate values if they are invalid. */
5323 check_bitfield_type_and_width (location_t loc
, tree
*type
, tree
*width
,
5327 unsigned int max_width
;
5328 unsigned HOST_WIDE_INT w
;
5329 const char *name
= (orig_name
5330 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name
))
5331 : _("<anonymous>"));
5333 /* Detect and ignore out of range field width and process valid
5335 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width
)))
5337 error_at (loc
, "bit-field %qs width not an integer constant", name
);
5338 *width
= integer_one_node
;
5342 if (TREE_CODE (*width
) != INTEGER_CST
)
5344 *width
= c_fully_fold (*width
, false, NULL
);
5345 if (TREE_CODE (*width
) == INTEGER_CST
)
5346 pedwarn (loc
, OPT_Wpedantic
,
5347 "bit-field %qs width not an integer constant expression",
5350 if (TREE_CODE (*width
) != INTEGER_CST
)
5352 error_at (loc
, "bit-field %qs width not an integer constant", name
);
5353 *width
= integer_one_node
;
5355 constant_expression_warning (*width
);
5356 if (tree_int_cst_sgn (*width
) < 0)
5358 error_at (loc
, "negative width in bit-field %qs", name
);
5359 *width
= integer_one_node
;
5361 else if (integer_zerop (*width
) && orig_name
)
5363 error_at (loc
, "zero width for bit-field %qs", name
);
5364 *width
= integer_one_node
;
5368 /* Detect invalid bit-field type. */
5369 if (TREE_CODE (*type
) != INTEGER_TYPE
5370 && TREE_CODE (*type
) != BOOLEAN_TYPE
5371 && TREE_CODE (*type
) != ENUMERAL_TYPE
)
5373 error_at (loc
, "bit-field %qs has invalid type", name
);
5374 *type
= unsigned_type_node
;
5377 type_mv
= TYPE_MAIN_VARIANT (*type
);
5378 if (!in_system_header_at (input_location
)
5379 && type_mv
!= integer_type_node
5380 && type_mv
!= unsigned_type_node
5381 && type_mv
!= boolean_type_node
)
5382 pedwarn_c90 (loc
, OPT_Wpedantic
,
5383 "type of bit-field %qs is a GCC extension", name
);
5385 max_width
= TYPE_PRECISION (*type
);
5387 if (0 < compare_tree_int (*width
, max_width
))
5389 error_at (loc
, "width of %qs exceeds its type", name
);
5391 *width
= build_int_cst (integer_type_node
, w
);
5394 w
= tree_to_uhwi (*width
);
5396 if (TREE_CODE (*type
) == ENUMERAL_TYPE
)
5398 struct lang_type
*lt
= TYPE_LANG_SPECIFIC (*type
);
5400 || w
< tree_int_cst_min_precision (lt
->enum_min
, TYPE_SIGN (*type
))
5401 || w
< tree_int_cst_min_precision (lt
->enum_max
, TYPE_SIGN (*type
)))
5402 warning_at (loc
, 0, "%qs is narrower than values of its type", name
);
5408 /* Print warning about variable length array if necessary. */
5411 warn_variable_length_array (tree name
, tree size
)
5413 if (TREE_CONSTANT (size
))
5416 pedwarn_c90 (input_location
, OPT_Wvla
,
5417 "ISO C90 forbids array %qE whose size "
5418 "can%'t be evaluated", name
);
5420 pedwarn_c90 (input_location
, OPT_Wvla
, "ISO C90 forbids array "
5421 "whose size can%'t be evaluated");
5426 pedwarn_c90 (input_location
, OPT_Wvla
,
5427 "ISO C90 forbids variable length array %qE", name
);
5429 pedwarn_c90 (input_location
, OPT_Wvla
, "ISO C90 forbids variable "
5434 /* Print warning about defaulting to int if necessary. */
5437 warn_defaults_to (location_t location
, int opt
, const char *gmsgid
, ...)
5439 diagnostic_info diagnostic
;
5441 rich_location
richloc (line_table
, location
);
5443 va_start (ap
, gmsgid
);
5444 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
,
5445 flag_isoc99
? DK_PEDWARN
: DK_WARNING
);
5446 diagnostic
.option_index
= opt
;
5447 report_diagnostic (&diagnostic
);
5451 /* Given declspecs and a declarator,
5452 determine the name and type of the object declared
5453 and construct a ..._DECL node for it.
5454 (In one case we can return a ..._TYPE node instead.
5455 For invalid input we sometimes return 0.)
5457 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5459 DECL_CONTEXT says which syntactic context this declaration is in:
5460 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5461 FUNCDEF for a function definition. Like NORMAL but a few different
5462 error messages in each case. Return value may be zero meaning
5463 this definition is too screwy to try to parse.
5464 PARM for a parameter declaration (either within a function prototype
5465 or before a function body). Make a PARM_DECL, or return void_type_node.
5466 TYPENAME if for a typename (in a cast or sizeof).
5467 Don't make a DECL node; just return the ..._TYPE node.
5468 FIELD for a struct or union field; make a FIELD_DECL.
5469 INITIALIZED is true if the decl has an initializer.
5470 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5471 representing the width of the bit-field.
5472 DECL_ATTRS points to the list of attributes that should be added to this
5473 decl. Any nested attributes that belong on the decl itself will be
5475 If EXPR is not NULL, any expressions that need to be evaluated as
5476 part of evaluating variably modified types will be stored in *EXPR.
5477 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5478 set to indicate whether operands in *EXPR can be used in constant
5480 DEPRECATED_STATE is a deprecated_states value indicating whether
5481 deprecation warnings should be suppressed.
5483 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5484 It may also be so in the PARM case, for a prototype where the
5485 argument type is specified but not the name.
5487 This function is where the complicated C meanings of `static'
5488 and `extern' are interpreted. */
5491 grokdeclarator (const struct c_declarator
*declarator
,
5492 struct c_declspecs
*declspecs
,
5493 enum decl_context decl_context
, bool initialized
, tree
*width
,
5494 tree
*decl_attrs
, tree
*expr
, bool *expr_const_operands
,
5495 enum deprecated_states deprecated_state
)
5497 tree type
= declspecs
->type
;
5498 bool threadp
= declspecs
->thread_p
;
5499 enum c_storage_class storage_class
= declspecs
->storage_class
;
5504 int type_quals
= TYPE_UNQUALIFIED
;
5505 tree name
= NULL_TREE
;
5506 bool funcdef_flag
= false;
5507 bool funcdef_syntax
= false;
5508 bool size_varies
= false;
5509 tree decl_attr
= declspecs
->decl_attr
;
5510 int array_ptr_quals
= TYPE_UNQUALIFIED
;
5511 tree array_ptr_attrs
= NULL_TREE
;
5512 int array_parm_static
= 0;
5513 bool array_parm_vla_unspec_p
= false;
5514 tree returned_attrs
= NULL_TREE
;
5515 bool bitfield
= width
!= NULL
;
5517 tree orig_qual_type
= NULL
;
5518 size_t orig_qual_indirect
= 0;
5519 struct c_arg_info
*arg_info
= 0;
5520 addr_space_t as1
, as2
, address_space
;
5521 location_t loc
= UNKNOWN_LOCATION
;
5523 bool expr_const_operands_dummy
;
5524 enum c_declarator_kind first_non_attr_kind
;
5525 unsigned int alignas_align
= 0;
5527 if (TREE_CODE (type
) == ERROR_MARK
)
5528 return error_mark_node
;
5531 if (expr_const_operands
== NULL
)
5532 expr_const_operands
= &expr_const_operands_dummy
;
5534 *expr
= declspecs
->expr
;
5535 *expr_const_operands
= declspecs
->expr_const_operands
;
5537 if (decl_context
== FUNCDEF
)
5538 funcdef_flag
= true, decl_context
= NORMAL
;
5540 /* Look inside a declarator for the name being declared
5541 and get it as an IDENTIFIER_NODE, for an error message. */
5543 const struct c_declarator
*decl
= declarator
;
5545 first_non_attr_kind
= cdk_attrs
;
5555 funcdef_syntax
= (decl
->kind
== cdk_function
);
5556 if (first_non_attr_kind
== cdk_attrs
)
5557 first_non_attr_kind
= decl
->kind
;
5558 decl
= decl
->declarator
;
5562 decl
= decl
->declarator
;
5569 if (first_non_attr_kind
== cdk_attrs
)
5570 first_non_attr_kind
= decl
->kind
;
5579 gcc_assert (decl_context
== PARM
5580 || decl_context
== TYPENAME
5581 || (decl_context
== FIELD
5582 && declarator
->kind
== cdk_id
));
5583 gcc_assert (!initialized
);
5587 /* A function definition's declarator must have the form of
5588 a function declarator. */
5590 if (funcdef_flag
&& !funcdef_syntax
)
5593 /* If this looks like a function definition, make it one,
5594 even if it occurs where parms are expected.
5595 Then store_parm_decls will reject it and not use it as a parm. */
5596 if (decl_context
== NORMAL
&& !funcdef_flag
&& current_scope
->parm_flag
)
5597 decl_context
= PARM
;
5599 if (declspecs
->deprecated_p
&& deprecated_state
!= DEPRECATED_SUPPRESS
)
5600 warn_deprecated_use (declspecs
->type
, declspecs
->decl_attr
);
5602 if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
5603 && current_scope
== file_scope
5604 && variably_modified_type_p (type
, NULL_TREE
))
5607 error_at (loc
, "variably modified %qE at file scope", name
);
5609 error_at (loc
, "variably modified field at file scope");
5610 type
= integer_type_node
;
5613 size_varies
= C_TYPE_VARIABLE_SIZE (type
) != 0;
5615 /* Diagnose defaulting to "int". */
5617 if (declspecs
->default_int_p
&& !in_system_header_at (input_location
))
5619 /* Issue a warning if this is an ISO C 99 program or if
5620 -Wreturn-type and this is a function, or if -Wimplicit;
5621 prefer the former warning since it is more explicit. */
5622 if ((warn_implicit_int
|| warn_return_type
|| flag_isoc99
)
5624 warn_about_return_type
= 1;
5628 warn_defaults_to (loc
, OPT_Wimplicit_int
,
5629 "type defaults to %<int%> in declaration "
5632 warn_defaults_to (loc
, OPT_Wimplicit_int
,
5633 "type defaults to %<int%> in type name");
5637 /* Adjust the type if a bit-field is being declared,
5638 -funsigned-bitfields applied and the type is not explicitly
5640 if (bitfield
&& !flag_signed_bitfields
&& !declspecs
->explicit_signed_p
5641 && TREE_CODE (type
) == INTEGER_TYPE
)
5642 type
= unsigned_type_for (type
);
5644 /* Figure out the type qualifiers for the declaration. There are
5645 two ways a declaration can become qualified. One is something
5646 like `const int i' where the `const' is explicit. Another is
5647 something like `typedef const int CI; CI i' where the type of the
5648 declaration contains the `const'. A third possibility is that
5649 there is a type qualifier on the element type of a typedefed
5650 array type, in which case we should extract that qualifier so
5651 that c_apply_type_quals_to_decl receives the full list of
5652 qualifiers to work with (C90 is not entirely clear about whether
5653 duplicate qualifiers should be diagnosed in this case, but it
5654 seems most appropriate to do so). */
5655 element_type
= strip_array_types (type
);
5656 constp
= declspecs
->const_p
+ TYPE_READONLY (element_type
);
5657 restrictp
= declspecs
->restrict_p
+ TYPE_RESTRICT (element_type
);
5658 volatilep
= declspecs
->volatile_p
+ TYPE_VOLATILE (element_type
);
5659 atomicp
= declspecs
->atomic_p
+ TYPE_ATOMIC (element_type
);
5660 as1
= declspecs
->address_space
;
5661 as2
= TYPE_ADDR_SPACE (element_type
);
5662 address_space
= ADDR_SPACE_GENERIC_P (as1
)? as2
: as1
;
5665 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<const%>");
5667 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<restrict%>");
5669 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<volatile%>");
5671 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<_Atomic%>");
5673 if (!ADDR_SPACE_GENERIC_P (as1
) && !ADDR_SPACE_GENERIC_P (as2
) && as1
!= as2
)
5674 error_at (loc
, "conflicting named address spaces (%s vs %s)",
5675 c_addr_space_name (as1
), c_addr_space_name (as2
));
5677 if ((TREE_CODE (type
) == ARRAY_TYPE
5678 || first_non_attr_kind
== cdk_array
)
5679 && TYPE_QUALS (element_type
))
5681 orig_qual_type
= type
;
5682 type
= TYPE_MAIN_VARIANT (type
);
5684 type_quals
= ((constp
? TYPE_QUAL_CONST
: 0)
5685 | (restrictp
? TYPE_QUAL_RESTRICT
: 0)
5686 | (volatilep
? TYPE_QUAL_VOLATILE
: 0)
5687 | (atomicp
? TYPE_QUAL_ATOMIC
: 0)
5688 | ENCODE_QUAL_ADDR_SPACE (address_space
));
5689 if (type_quals
!= TYPE_QUALS (element_type
))
5690 orig_qual_type
= NULL_TREE
;
5692 /* Applying the _Atomic qualifier to an array type (through the use
5693 of typedefs or typeof) must be detected here. If the qualifier
5694 is introduced later, any appearance of applying it to an array is
5695 actually applying it to an element of that array. */
5696 if (atomicp
&& TREE_CODE (type
) == ARRAY_TYPE
)
5697 error_at (loc
, "%<_Atomic%>-qualified array type");
5699 /* Warn about storage classes that are invalid for certain
5700 kinds of declarations (parameters, typenames, etc.). */
5704 || storage_class
== csc_auto
5705 || storage_class
== csc_register
5706 || storage_class
== csc_typedef
))
5708 if (storage_class
== csc_auto
)
5710 (current_scope
== file_scope
) ? 0 : OPT_Wpedantic
,
5711 "function definition declared %<auto%>");
5712 if (storage_class
== csc_register
)
5713 error_at (loc
, "function definition declared %<register%>");
5714 if (storage_class
== csc_typedef
)
5715 error_at (loc
, "function definition declared %<typedef%>");
5717 error_at (loc
, "function definition declared %qs",
5718 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
5720 if (storage_class
== csc_auto
5721 || storage_class
== csc_register
5722 || storage_class
== csc_typedef
)
5723 storage_class
= csc_none
;
5725 else if (decl_context
!= NORMAL
&& (storage_class
!= csc_none
|| threadp
))
5727 if (decl_context
== PARM
&& storage_class
== csc_register
)
5731 switch (decl_context
)
5735 error_at (loc
, "storage class specified for structure "
5738 error_at (loc
, "storage class specified for structure field");
5742 error_at (loc
, "storage class specified for parameter %qE",
5745 error_at (loc
, "storage class specified for unnamed parameter");
5748 error_at (loc
, "storage class specified for typename");
5751 storage_class
= csc_none
;
5755 else if (storage_class
== csc_extern
5759 /* 'extern' with initialization is invalid if not at file scope. */
5760 if (current_scope
== file_scope
)
5762 /* It is fine to have 'extern const' when compiling at C
5763 and C++ intersection. */
5764 if (!(warn_cxx_compat
&& constp
))
5765 warning_at (loc
, 0, "%qE initialized and declared %<extern%>",
5769 error_at (loc
, "%qE has both %<extern%> and initializer", name
);
5771 else if (current_scope
== file_scope
)
5773 if (storage_class
== csc_auto
)
5774 error_at (loc
, "file-scope declaration of %qE specifies %<auto%>",
5776 if (pedantic
&& storage_class
== csc_register
)
5777 pedwarn (input_location
, OPT_Wpedantic
,
5778 "file-scope declaration of %qE specifies %<register%>", name
);
5782 if (storage_class
== csc_extern
&& funcdef_flag
)
5783 error_at (loc
, "nested function %qE declared %<extern%>", name
);
5784 else if (threadp
&& storage_class
== csc_none
)
5786 error_at (loc
, "function-scope %qE implicitly auto and declared "
5788 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
5793 /* Now figure out the structure of the declarator proper.
5794 Descend through it, creating more complex types, until we reach
5795 the declared identifier (or NULL_TREE, in an absolute declarator).
5796 At each stage we maintain an unqualified version of the type
5797 together with any qualifiers that should be applied to it with
5798 c_build_qualified_type; this way, array types including
5799 multidimensional array types are first built up in unqualified
5800 form and then the qualified form is created with
5801 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5803 while (declarator
&& declarator
->kind
!= cdk_id
)
5805 if (type
== error_mark_node
)
5807 declarator
= declarator
->declarator
;
5811 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5812 a cdk_pointer (for *...),
5813 a cdk_function (for ...(...)),
5814 a cdk_attrs (for nested attributes),
5815 or a cdk_id (for the name being declared
5816 or the place in an absolute declarator
5817 where the name was omitted).
5818 For the last case, we have just exited the loop.
5820 At this point, TYPE is the type of elements of an array,
5821 or for a function to return, or for a pointer to point to.
5822 After this sequence of ifs, TYPE is the type of the
5823 array or function or pointer, and DECLARATOR has had its
5824 outermost layer removed. */
5826 if (array_ptr_quals
!= TYPE_UNQUALIFIED
5827 || array_ptr_attrs
!= NULL_TREE
5828 || array_parm_static
)
5830 /* Only the innermost declarator (making a parameter be of
5831 array type which is converted to pointer type)
5832 may have static or type qualifiers. */
5833 error_at (loc
, "static or type qualifiers in non-parameter array declarator");
5834 array_ptr_quals
= TYPE_UNQUALIFIED
;
5835 array_ptr_attrs
= NULL_TREE
;
5836 array_parm_static
= 0;
5839 switch (declarator
->kind
)
5843 /* A declarator with embedded attributes. */
5844 tree attrs
= declarator
->u
.attrs
;
5845 const struct c_declarator
*inner_decl
;
5847 declarator
= declarator
->declarator
;
5848 inner_decl
= declarator
;
5849 while (inner_decl
->kind
== cdk_attrs
)
5850 inner_decl
= inner_decl
->declarator
;
5851 if (inner_decl
->kind
== cdk_id
)
5852 attr_flags
|= (int) ATTR_FLAG_DECL_NEXT
;
5853 else if (inner_decl
->kind
== cdk_function
)
5854 attr_flags
|= (int) ATTR_FLAG_FUNCTION_NEXT
;
5855 else if (inner_decl
->kind
== cdk_array
)
5856 attr_flags
|= (int) ATTR_FLAG_ARRAY_NEXT
;
5857 returned_attrs
= decl_attributes (&type
,
5858 chainon (returned_attrs
, attrs
),
5864 tree itype
= NULL_TREE
;
5865 tree size
= declarator
->u
.array
.dimen
;
5866 /* The index is a signed object `sizetype' bits wide. */
5867 tree index_type
= c_common_signed_type (sizetype
);
5869 array_ptr_quals
= declarator
->u
.array
.quals
;
5870 array_ptr_attrs
= declarator
->u
.array
.attrs
;
5871 array_parm_static
= declarator
->u
.array
.static_p
;
5872 array_parm_vla_unspec_p
= declarator
->u
.array
.vla_unspec_p
;
5874 declarator
= declarator
->declarator
;
5876 /* Check for some types that there cannot be arrays of. */
5878 if (VOID_TYPE_P (type
))
5881 error_at (loc
, "declaration of %qE as array of voids", name
);
5883 error_at (loc
, "declaration of type name as array of voids");
5884 type
= error_mark_node
;
5887 if (TREE_CODE (type
) == FUNCTION_TYPE
)
5890 error_at (loc
, "declaration of %qE as array of functions",
5893 error_at (loc
, "declaration of type name as array of "
5895 type
= error_mark_node
;
5898 if (pedantic
&& !in_system_header_at (input_location
)
5899 && flexible_array_type_p (type
))
5900 pedwarn (loc
, OPT_Wpedantic
,
5901 "invalid use of structure with flexible array member");
5903 if (size
== error_mark_node
)
5904 type
= error_mark_node
;
5906 if (type
== error_mark_node
)
5909 /* If size was specified, set ITYPE to a range-type for
5910 that size. Otherwise, ITYPE remains null. finish_decl
5911 may figure it out from an initial value. */
5915 bool size_maybe_const
= true;
5916 bool size_int_const
= (TREE_CODE (size
) == INTEGER_CST
5917 && !TREE_OVERFLOW (size
));
5918 bool this_size_varies
= false;
5920 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5922 STRIP_TYPE_NOPS (size
);
5924 if (!INTEGRAL_TYPE_P (TREE_TYPE (size
)))
5927 error_at (loc
, "size of array %qE has non-integer type",
5931 "size of unnamed array has non-integer type");
5932 size
= integer_one_node
;
5934 /* This can happen with enum forward declaration. */
5935 else if (!COMPLETE_TYPE_P (TREE_TYPE (size
)))
5938 error_at (loc
, "size of array %qE has incomplete type",
5941 error_at (loc
, "size of unnamed array has incomplete "
5943 size
= integer_one_node
;
5946 size
= c_fully_fold (size
, false, &size_maybe_const
);
5948 if (pedantic
&& size_maybe_const
&& integer_zerop (size
))
5951 pedwarn (loc
, OPT_Wpedantic
,
5952 "ISO C forbids zero-size array %qE", name
);
5954 pedwarn (loc
, OPT_Wpedantic
,
5955 "ISO C forbids zero-size array");
5958 if (TREE_CODE (size
) == INTEGER_CST
&& size_maybe_const
)
5960 constant_expression_warning (size
);
5961 if (tree_int_cst_sgn (size
) < 0)
5964 error_at (loc
, "size of array %qE is negative", name
);
5966 error_at (loc
, "size of unnamed array is negative");
5967 size
= integer_one_node
;
5969 /* Handle a size folded to an integer constant but
5970 not an integer constant expression. */
5971 if (!size_int_const
)
5973 /* If this is a file scope declaration of an
5974 ordinary identifier, this is invalid code;
5975 diagnosing it here and not subsequently
5976 treating the type as variable-length avoids
5977 more confusing diagnostics later. */
5978 if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
5979 && current_scope
== file_scope
)
5980 pedwarn (input_location
, 0,
5981 "variably modified %qE at file scope",
5984 this_size_varies
= size_varies
= true;
5985 warn_variable_length_array (name
, size
);
5988 else if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
5989 && current_scope
== file_scope
)
5991 error_at (loc
, "variably modified %qE at file scope", name
);
5992 size
= integer_one_node
;
5996 /* Make sure the array size remains visibly
5997 nonconstant even if it is (eg) a const variable
5998 with known value. */
5999 this_size_varies
= size_varies
= true;
6000 warn_variable_length_array (name
, size
);
6001 if (flag_sanitize
& SANITIZE_VLA
6002 && decl_context
== NORMAL
6003 && do_ubsan_in_current_function ())
6005 /* Evaluate the array size only once. */
6006 size
= c_save_expr (size
);
6007 size
= c_fully_fold (size
, false, NULL
);
6008 size
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
6009 ubsan_instrument_vla (loc
, size
),
6014 if (integer_zerop (size
) && !this_size_varies
)
6016 /* A zero-length array cannot be represented with
6017 an unsigned index type, which is what we'll
6018 get with build_index_type. Create an
6019 open-ended range instead. */
6020 itype
= build_range_type (sizetype
, size
, NULL_TREE
);
6024 /* Arrange for the SAVE_EXPR on the inside of the
6025 MINUS_EXPR, which allows the -1 to get folded
6026 with the +1 that happens when building TYPE_SIZE. */
6028 size
= save_expr (size
);
6029 if (this_size_varies
&& TREE_CODE (size
) == INTEGER_CST
)
6030 size
= build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
6031 integer_zero_node
, size
);
6033 /* Compute the maximum valid index, that is, size
6034 - 1. Do the calculation in index_type, so that
6035 if it is a variable the computations will be
6036 done in the proper mode. */
6037 itype
= fold_build2_loc (loc
, MINUS_EXPR
, index_type
,
6038 convert (index_type
, size
),
6039 convert (index_type
,
6042 /* The above overflows when size does not fit
6044 ??? While a size of INT_MAX+1 technically shouldn't
6045 cause an overflow (because we subtract 1), handling
6046 this case seems like an unnecessary complication. */
6047 if (TREE_CODE (size
) == INTEGER_CST
6048 && !int_fits_type_p (size
, index_type
))
6051 error_at (loc
, "size of array %qE is too large",
6054 error_at (loc
, "size of unnamed array is too large");
6055 type
= error_mark_node
;
6059 itype
= build_index_type (itype
);
6061 if (this_size_varies
)
6064 *expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
6068 *expr_const_operands
&= size_maybe_const
;
6071 else if (decl_context
== FIELD
)
6073 bool flexible_array_member
= false;
6074 if (array_parm_vla_unspec_p
)
6075 /* Field names can in fact have function prototype
6076 scope so [*] is disallowed here through making
6077 the field variably modified, not through being
6078 something other than a declaration with function
6083 const struct c_declarator
*t
= declarator
;
6084 while (t
->kind
== cdk_attrs
)
6086 flexible_array_member
= (t
->kind
== cdk_id
);
6088 if (flexible_array_member
6089 && !in_system_header_at (input_location
))
6090 pedwarn_c90 (loc
, OPT_Wpedantic
, "ISO C90 does not "
6091 "support flexible array members");
6093 /* ISO C99 Flexible array members are effectively
6094 identical to GCC's zero-length array extension. */
6095 if (flexible_array_member
|| array_parm_vla_unspec_p
)
6096 itype
= build_range_type (sizetype
, size_zero_node
,
6099 else if (decl_context
== PARM
)
6101 if (array_parm_vla_unspec_p
)
6103 itype
= build_range_type (sizetype
, size_zero_node
, NULL_TREE
);
6107 else if (decl_context
== TYPENAME
)
6109 if (array_parm_vla_unspec_p
)
6112 warning (0, "%<[*]%> not in a declaration");
6113 /* We use this to avoid messing up with incomplete
6114 array types of the same type, that would
6115 otherwise be modified below. */
6116 itype
= build_range_type (sizetype
, size_zero_node
,
6122 /* Complain about arrays of incomplete types. */
6123 if (!COMPLETE_TYPE_P (type
))
6125 error_at (loc
, "array type has incomplete element type %qT",
6127 /* See if we can be more helpful. */
6128 if (TREE_CODE (type
) == ARRAY_TYPE
)
6131 inform (loc
, "declaration of %qE as multidimensional "
6132 "array must have bounds for all dimensions "
6133 "except the first", name
);
6135 inform (loc
, "declaration of multidimensional array "
6136 "must have bounds for all dimensions except "
6139 type
= error_mark_node
;
6142 /* When itype is NULL, a shared incomplete array type is
6143 returned for all array of a given type. Elsewhere we
6144 make sure we don't complete that type before copying
6145 it, but here we want to make sure we don't ever
6146 modify the shared type, so we gcc_assert (itype)
6149 addr_space_t as
= DECODE_QUAL_ADDR_SPACE (type_quals
);
6150 if (!ADDR_SPACE_GENERIC_P (as
) && as
!= TYPE_ADDR_SPACE (type
))
6151 type
= build_qualified_type (type
,
6152 ENCODE_QUAL_ADDR_SPACE (as
));
6154 type
= build_array_type (type
, itype
);
6157 if (type
!= error_mark_node
)
6161 /* It is ok to modify type here even if itype is
6162 NULL: if size_varies, we're in a
6163 multi-dimensional array and the inner type has
6164 variable size, so the enclosing shared array type
6166 if (size
&& TREE_CODE (size
) == INTEGER_CST
)
6168 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6169 C_TYPE_VARIABLE_SIZE (type
) = 1;
6172 /* The GCC extension for zero-length arrays differs from
6173 ISO flexible array members in that sizeof yields
6175 if (size
&& integer_zerop (size
))
6178 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6179 TYPE_SIZE (type
) = bitsize_zero_node
;
6180 TYPE_SIZE_UNIT (type
) = size_zero_node
;
6181 SET_TYPE_STRUCTURAL_EQUALITY (type
);
6183 if (array_parm_vla_unspec_p
)
6186 /* The type is complete. C99 6.7.5.2p4 */
6187 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6188 TYPE_SIZE (type
) = bitsize_zero_node
;
6189 TYPE_SIZE_UNIT (type
) = size_zero_node
;
6190 SET_TYPE_STRUCTURAL_EQUALITY (type
);
6193 if (!valid_array_size_p (loc
, type
, name
))
6194 type
= error_mark_node
;
6197 if (decl_context
!= PARM
6198 && (array_ptr_quals
!= TYPE_UNQUALIFIED
6199 || array_ptr_attrs
!= NULL_TREE
6200 || array_parm_static
))
6202 error_at (loc
, "static or type qualifiers in non-parameter "
6203 "array declarator");
6204 array_ptr_quals
= TYPE_UNQUALIFIED
;
6205 array_ptr_attrs
= NULL_TREE
;
6206 array_parm_static
= 0;
6208 orig_qual_indirect
++;
6213 /* Say it's a definition only for the declarator closest
6214 to the identifier, apart possibly from some
6216 bool really_funcdef
= false;
6218 orig_qual_type
= NULL_TREE
;
6221 const struct c_declarator
*t
= declarator
->declarator
;
6222 while (t
->kind
== cdk_attrs
)
6224 really_funcdef
= (t
->kind
== cdk_id
);
6227 /* Declaring a function type. Make sure we have a valid
6228 type for the function to return. */
6229 if (type
== error_mark_node
)
6232 size_varies
= false;
6234 /* Warn about some types functions can't return. */
6235 if (TREE_CODE (type
) == FUNCTION_TYPE
)
6238 error_at (loc
, "%qE declared as function returning a "
6241 error_at (loc
, "type name declared as function "
6242 "returning a function");
6243 type
= integer_type_node
;
6245 if (TREE_CODE (type
) == ARRAY_TYPE
)
6248 error_at (loc
, "%qE declared as function returning an array",
6251 error_at (loc
, "type name declared as function returning "
6253 type
= integer_type_node
;
6256 /* Construct the function type and go to the next
6257 inner layer of declarator. */
6258 arg_info
= declarator
->u
.arg_info
;
6259 arg_types
= grokparms (arg_info
, really_funcdef
);
6261 /* Type qualifiers before the return type of the function
6262 qualify the return type, not the function type. */
6265 int quals_used
= type_quals
;
6266 /* Type qualifiers on a function return type are
6267 normally permitted by the standard but have no
6268 effect, so give a warning at -Wreturn-type.
6269 Qualifiers on a void return type are banned on
6270 function definitions in ISO C; GCC used to used
6271 them for noreturn functions. The resolution of C11
6272 DR#423 means qualifiers (other than _Atomic) are
6273 actually removed from the return type when
6274 determining the function type. */
6276 quals_used
&= TYPE_QUAL_ATOMIC
;
6277 if (quals_used
&& VOID_TYPE_P (type
) && really_funcdef
)
6279 "function definition has qualified void return type");
6281 warning_at (loc
, OPT_Wignored_qualifiers
,
6282 "type qualifiers ignored on function return type");
6284 /* Ensure an error for restrict on invalid types; the
6285 DR#423 resolution is not entirely clear about
6288 && (type_quals
& TYPE_QUAL_RESTRICT
)
6289 && (!POINTER_TYPE_P (type
)
6290 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
6291 error_at (loc
, "invalid use of %<restrict%>");
6293 type
= c_build_qualified_type (type
, quals_used
);
6295 type_quals
= TYPE_UNQUALIFIED
;
6297 type
= build_function_type (type
, arg_types
);
6298 declarator
= declarator
->declarator
;
6300 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6301 the formal parameter list of this FUNCTION_TYPE to point to
6302 the FUNCTION_TYPE node itself. */
6307 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info
->tags
, ix
, tag
)
6308 TYPE_CONTEXT (tag
->type
) = type
;
6314 /* Merge any constancy or volatility into the target type
6316 if ((type_quals
& TYPE_QUAL_ATOMIC
)
6317 && TREE_CODE (type
) == FUNCTION_TYPE
)
6320 "%<_Atomic%>-qualified function type");
6321 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6323 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
6325 pedwarn (loc
, OPT_Wpedantic
,
6326 "ISO C forbids qualified function types");
6328 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6329 orig_qual_indirect
);
6330 orig_qual_type
= NULL_TREE
;
6331 size_varies
= false;
6333 /* When the pointed-to type involves components of variable size,
6334 care must be taken to ensure that the size evaluation code is
6335 emitted early enough to dominate all the possible later uses
6336 and late enough for the variables on which it depends to have
6339 This is expected to happen automatically when the pointed-to
6340 type has a name/declaration of it's own, but special attention
6341 is required if the type is anonymous.
6343 We handle the NORMAL and FIELD contexts here by attaching an
6344 artificial TYPE_DECL to such pointed-to type. This forces the
6345 sizes evaluation at a safe point and ensures it is not deferred
6346 until e.g. within a deeper conditional context.
6348 We expect nothing to be needed here for PARM or TYPENAME.
6349 Pushing a TYPE_DECL at this point for TYPENAME would actually
6350 be incorrect, as we might be in the middle of an expression
6351 with side effects on the pointed-to type size "arguments" prior
6352 to the pointer declaration point and the fake TYPE_DECL in the
6353 enclosing context would force the size evaluation prior to the
6356 if (!TYPE_NAME (type
)
6357 && (decl_context
== NORMAL
|| decl_context
== FIELD
)
6358 && variably_modified_type_p (type
, NULL_TREE
))
6360 tree decl
= build_decl (loc
, TYPE_DECL
, NULL_TREE
, type
);
6361 DECL_ARTIFICIAL (decl
) = 1;
6363 finish_decl (decl
, loc
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
6364 TYPE_NAME (type
) = decl
;
6367 type
= c_build_pointer_type (type
);
6369 /* Process type qualifiers (such as const or volatile)
6370 that were given inside the `*'. */
6371 type_quals
= declarator
->u
.pointer_quals
;
6373 declarator
= declarator
->declarator
;
6380 *decl_attrs
= chainon (returned_attrs
, *decl_attrs
);
6382 /* Now TYPE has the actual type, apart from any qualifiers in
6385 /* Warn about address space used for things other than static memory or
6387 address_space
= DECODE_QUAL_ADDR_SPACE (type_quals
);
6388 if (!ADDR_SPACE_GENERIC_P (address_space
))
6390 if (decl_context
== NORMAL
)
6392 switch (storage_class
)
6395 error ("%qs combined with %<auto%> qualifier for %qE",
6396 c_addr_space_name (address_space
), name
);
6399 error ("%qs combined with %<register%> qualifier for %qE",
6400 c_addr_space_name (address_space
), name
);
6403 if (current_function_scope
)
6405 error ("%qs specified for auto variable %qE",
6406 c_addr_space_name (address_space
), name
);
6418 else if (decl_context
== PARM
&& TREE_CODE (type
) != ARRAY_TYPE
)
6421 error ("%qs specified for parameter %qE",
6422 c_addr_space_name (address_space
), name
);
6424 error ("%qs specified for unnamed parameter",
6425 c_addr_space_name (address_space
));
6427 else if (decl_context
== FIELD
)
6430 error ("%qs specified for structure field %qE",
6431 c_addr_space_name (address_space
), name
);
6433 error ("%qs specified for structure field",
6434 c_addr_space_name (address_space
));
6438 /* Check the type and width of a bit-field. */
6441 check_bitfield_type_and_width (loc
, &type
, width
, name
);
6442 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6443 atomic types are permitted for bit-fields; we have no code to
6444 make bit-field accesses atomic, so disallow them. */
6445 if (type_quals
& TYPE_QUAL_ATOMIC
)
6448 error_at (loc
, "bit-field %qE has atomic type", name
);
6450 error_at (loc
, "bit-field has atomic type");
6451 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6455 /* Reject invalid uses of _Alignas. */
6456 if (declspecs
->alignas_p
)
6458 if (storage_class
== csc_typedef
)
6459 error_at (loc
, "alignment specified for typedef %qE", name
);
6460 else if (storage_class
== csc_register
)
6461 error_at (loc
, "alignment specified for %<register%> object %qE",
6463 else if (decl_context
== PARM
)
6466 error_at (loc
, "alignment specified for parameter %qE", name
);
6468 error_at (loc
, "alignment specified for unnamed parameter");
6473 error_at (loc
, "alignment specified for bit-field %qE", name
);
6475 error_at (loc
, "alignment specified for unnamed bit-field");
6477 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
6478 error_at (loc
, "alignment specified for function %qE", name
);
6479 else if (declspecs
->align_log
!= -1 && TYPE_P (type
))
6481 alignas_align
= 1U << declspecs
->align_log
;
6482 if (alignas_align
< min_align_of_type (type
))
6485 error_at (loc
, "%<_Alignas%> specifiers cannot reduce "
6486 "alignment of %qE", name
);
6488 error_at (loc
, "%<_Alignas%> specifiers cannot reduce "
6489 "alignment of unnamed field");
6495 /* If this is declaring a typedef name, return a TYPE_DECL. */
6497 if (storage_class
== csc_typedef
)
6500 if ((type_quals
& TYPE_QUAL_ATOMIC
)
6501 && TREE_CODE (type
) == FUNCTION_TYPE
)
6504 "%<_Atomic%>-qualified function type");
6505 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6507 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
6509 pedwarn (loc
, OPT_Wpedantic
,
6510 "ISO C forbids qualified function types");
6512 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6513 orig_qual_indirect
);
6514 decl
= build_decl (declarator
->id_loc
,
6515 TYPE_DECL
, declarator
->u
.id
, type
);
6516 if (declspecs
->explicit_signed_p
)
6517 C_TYPEDEF_EXPLICITLY_SIGNED (decl
) = 1;
6518 if (declspecs
->inline_p
)
6519 pedwarn (loc
, 0,"typedef %q+D declared %<inline%>", decl
);
6520 if (declspecs
->noreturn_p
)
6521 pedwarn (loc
, 0,"typedef %q+D declared %<_Noreturn%>", decl
);
6523 if (warn_cxx_compat
&& declarator
->u
.id
!= NULL_TREE
)
6525 struct c_binding
*b
= I_TAG_BINDING (declarator
->u
.id
);
6528 && b
->decl
!= NULL_TREE
6529 && (B_IN_CURRENT_SCOPE (b
)
6530 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
6531 && TYPE_MAIN_VARIANT (b
->decl
) != TYPE_MAIN_VARIANT (type
))
6533 warning_at (declarator
->id_loc
, OPT_Wc___compat
,
6534 ("using %qD as both a typedef and a tag is "
6537 if (b
->locus
!= UNKNOWN_LOCATION
)
6538 inform (b
->locus
, "originally defined here");
6545 /* If this is a type name (such as, in a cast or sizeof),
6546 compute the type and return it now. */
6548 if (decl_context
== TYPENAME
)
6550 /* Note that the grammar rejects storage classes in typenames
6552 gcc_assert (storage_class
== csc_none
&& !threadp
6553 && !declspecs
->inline_p
&& !declspecs
->noreturn_p
);
6554 if ((type_quals
& TYPE_QUAL_ATOMIC
)
6555 && TREE_CODE (type
) == FUNCTION_TYPE
)
6558 "%<_Atomic%>-qualified function type");
6559 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6561 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
6563 pedwarn (loc
, OPT_Wpedantic
,
6564 "ISO C forbids const or volatile function types");
6566 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6567 orig_qual_indirect
);
6571 if (pedantic
&& decl_context
== FIELD
6572 && variably_modified_type_p (type
, NULL_TREE
))
6575 pedwarn (loc
, OPT_Wpedantic
, "a member of a structure or union cannot "
6576 "have a variably modified type");
6579 /* Aside from typedefs and type names (handle above),
6580 `void' at top level (not within pointer)
6581 is allowed only in public variables.
6582 We don't complain about parms either, but that is because
6583 a better error message can be made later. */
6585 if (VOID_TYPE_P (type
) && decl_context
!= PARM
6586 && !((decl_context
!= FIELD
&& TREE_CODE (type
) != FUNCTION_TYPE
)
6587 && (storage_class
== csc_extern
6588 || (current_scope
== file_scope
6589 && !(storage_class
== csc_static
6590 || storage_class
== csc_register
)))))
6592 error_at (loc
, "variable or field %qE declared void", name
);
6593 type
= integer_type_node
;
6596 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6597 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6602 if (decl_context
== PARM
)
6605 bool array_parameter_p
= false;
6607 /* A parameter declared as an array of T is really a pointer to T.
6608 One declared as a function is really a pointer to a function. */
6610 if (TREE_CODE (type
) == ARRAY_TYPE
)
6612 /* Transfer const-ness of array into that of type pointed to. */
6613 type
= TREE_TYPE (type
);
6614 if (orig_qual_type
!= NULL_TREE
)
6616 if (orig_qual_indirect
== 0)
6617 orig_qual_type
= TREE_TYPE (orig_qual_type
);
6619 orig_qual_indirect
--;
6622 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6623 orig_qual_indirect
);
6624 type
= c_build_pointer_type (type
);
6625 type_quals
= array_ptr_quals
;
6627 type
= c_build_qualified_type (type
, type_quals
);
6629 /* We don't yet implement attributes in this context. */
6630 if (array_ptr_attrs
!= NULL_TREE
)
6631 warning_at (loc
, OPT_Wattributes
,
6632 "attributes in parameter array declarator ignored");
6634 size_varies
= false;
6635 array_parameter_p
= true;
6637 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
6639 if (type_quals
& TYPE_QUAL_ATOMIC
)
6642 "%<_Atomic%>-qualified function type");
6643 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6645 else if (type_quals
)
6646 pedwarn (loc
, OPT_Wpedantic
,
6647 "ISO C forbids qualified function types");
6649 type
= c_build_qualified_type (type
, type_quals
);
6650 type
= c_build_pointer_type (type
);
6651 type_quals
= TYPE_UNQUALIFIED
;
6653 else if (type_quals
)
6654 type
= c_build_qualified_type (type
, type_quals
);
6656 decl
= build_decl (declarator
->id_loc
,
6657 PARM_DECL
, declarator
->u
.id
, type
);
6659 C_DECL_VARIABLE_SIZE (decl
) = 1;
6660 C_ARRAY_PARAMETER (decl
) = array_parameter_p
;
6662 /* Compute the type actually passed in the parmlist,
6663 for the case where there is no prototype.
6664 (For example, shorts and chars are passed as ints.)
6665 When there is a prototype, this is overridden later. */
6667 if (type
== error_mark_node
)
6668 promoted_type
= type
;
6670 promoted_type
= c_type_promotes_to (type
);
6672 DECL_ARG_TYPE (decl
) = promoted_type
;
6673 if (declspecs
->inline_p
)
6674 pedwarn (loc
, 0, "parameter %q+D declared %<inline%>", decl
);
6675 if (declspecs
->noreturn_p
)
6676 pedwarn (loc
, 0, "parameter %q+D declared %<_Noreturn%>", decl
);
6678 else if (decl_context
== FIELD
)
6680 /* Note that the grammar rejects storage classes in typenames
6682 gcc_assert (storage_class
== csc_none
&& !threadp
6683 && !declspecs
->inline_p
&& !declspecs
->noreturn_p
);
6685 /* Structure field. It may not be a function. */
6687 if (TREE_CODE (type
) == FUNCTION_TYPE
)
6689 error_at (loc
, "field %qE declared as a function", name
);
6690 type
= build_pointer_type (type
);
6692 else if (TREE_CODE (type
) != ERROR_MARK
6693 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type
))
6696 error_at (loc
, "field %qE has incomplete type", name
);
6698 error_at (loc
, "unnamed field has incomplete type");
6699 type
= error_mark_node
;
6701 else if (TREE_CODE (type
) == ARRAY_TYPE
6702 && TYPE_DOMAIN (type
) == NULL_TREE
)
6704 /* We have a flexible array member through a typedef.
6705 Set suitable range. Whether this is a correct position
6706 for a flexible array member will be determined elsewhere. */
6707 if (!in_system_header_at (input_location
))
6708 pedwarn_c90 (loc
, OPT_Wpedantic
, "ISO C90 does not "
6709 "support flexible array members");
6710 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6711 TYPE_DOMAIN (type
) = build_range_type (sizetype
, size_zero_node
,
6714 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6715 orig_qual_indirect
);
6716 decl
= build_decl (declarator
->id_loc
,
6717 FIELD_DECL
, declarator
->u
.id
, type
);
6718 DECL_NONADDRESSABLE_P (decl
) = bitfield
;
6719 if (bitfield
&& !declarator
->u
.id
)
6720 TREE_NO_WARNING (decl
) = 1;
6723 C_DECL_VARIABLE_SIZE (decl
) = 1;
6725 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
6727 if (storage_class
== csc_register
|| threadp
)
6729 error_at (loc
, "invalid storage class for function %qE", name
);
6731 else if (current_scope
!= file_scope
)
6733 /* Function declaration not at file scope. Storage
6734 classes other than `extern' are not allowed, C99
6735 6.7.1p5, and `extern' makes no difference. However,
6736 GCC allows 'auto', perhaps with 'inline', to support
6737 nested functions. */
6738 if (storage_class
== csc_auto
)
6739 pedwarn (loc
, OPT_Wpedantic
,
6740 "invalid storage class for function %qE", name
);
6741 else if (storage_class
== csc_static
)
6743 error_at (loc
, "invalid storage class for function %qE", name
);
6745 storage_class
= declspecs
->storage_class
= csc_none
;
6751 decl
= build_decl (declarator
->id_loc
,
6752 FUNCTION_DECL
, declarator
->u
.id
, type
);
6753 decl
= build_decl_attribute_variant (decl
, decl_attr
);
6755 if (type_quals
& TYPE_QUAL_ATOMIC
)
6758 "%<_Atomic%>-qualified function type");
6759 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6761 else if (pedantic
&& type_quals
&& !DECL_IN_SYSTEM_HEADER (decl
))
6762 pedwarn (loc
, OPT_Wpedantic
,
6763 "ISO C forbids qualified function types");
6765 /* Every function declaration is an external reference
6766 (DECL_EXTERNAL) except for those which are not at file
6767 scope and are explicitly declared "auto". This is
6768 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6769 GCC to signify a forward declaration of a nested function. */
6770 if (storage_class
== csc_auto
&& current_scope
!= file_scope
)
6771 DECL_EXTERNAL (decl
) = 0;
6772 /* In C99, a function which is declared 'inline' with 'extern'
6773 is not an external reference (which is confusing). It
6774 means that the later definition of the function must be output
6775 in this file, C99 6.7.4p6. In GNU C89, a function declared
6776 'extern inline' is an external reference. */
6777 else if (declspecs
->inline_p
&& storage_class
!= csc_static
)
6778 DECL_EXTERNAL (decl
) = ((storage_class
== csc_extern
)
6779 == flag_gnu89_inline
);
6781 DECL_EXTERNAL (decl
) = !initialized
;
6783 /* Record absence of global scope for `static' or `auto'. */
6785 = !(storage_class
== csc_static
|| storage_class
== csc_auto
);
6787 /* For a function definition, record the argument information
6788 block where store_parm_decls will look for it. */
6790 current_function_arg_info
= arg_info
;
6792 if (declspecs
->default_int_p
)
6793 C_FUNCTION_IMPLICIT_INT (decl
) = 1;
6795 /* Record presence of `inline' and `_Noreturn', if it is
6797 if (flag_hosted
&& MAIN_NAME_P (declarator
->u
.id
))
6799 if (declspecs
->inline_p
)
6800 pedwarn (loc
, 0, "cannot inline function %<main%>");
6801 if (declspecs
->noreturn_p
)
6802 pedwarn (loc
, 0, "%<main%> declared %<_Noreturn%>");
6806 if (declspecs
->inline_p
)
6807 /* Record that the function is declared `inline'. */
6808 DECL_DECLARED_INLINE_P (decl
) = 1;
6809 if (declspecs
->noreturn_p
)
6812 pedwarn_c99 (loc
, OPT_Wpedantic
,
6813 "ISO C99 does not support %<_Noreturn%>");
6815 pedwarn_c99 (loc
, OPT_Wpedantic
,
6816 "ISO C90 does not support %<_Noreturn%>");
6817 TREE_THIS_VOLATILE (decl
) = 1;
6823 /* It's a variable. */
6824 /* An uninitialized decl with `extern' is a reference. */
6825 int extern_ref
= !initialized
&& storage_class
== csc_extern
;
6827 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6828 orig_qual_indirect
);
6830 /* C99 6.2.2p7: It is invalid (compile-time undefined
6831 behavior) to create an 'extern' declaration for a
6832 variable if there is a global declaration that is
6833 'static' and the global declaration is not visible.
6834 (If the static declaration _is_ currently visible,
6835 the 'extern' declaration is taken to refer to that decl.) */
6836 if (extern_ref
&& current_scope
!= file_scope
)
6838 tree global_decl
= identifier_global_value (declarator
->u
.id
);
6839 tree visible_decl
= lookup_name (declarator
->u
.id
);
6842 && global_decl
!= visible_decl
6843 && VAR_P (global_decl
)
6844 && !TREE_PUBLIC (global_decl
))
6845 error_at (loc
, "variable previously declared %<static%> "
6846 "redeclared %<extern%>");
6849 decl
= build_decl (declarator
->id_loc
,
6850 VAR_DECL
, declarator
->u
.id
, type
);
6852 C_DECL_VARIABLE_SIZE (decl
) = 1;
6854 if (declspecs
->inline_p
)
6855 pedwarn (loc
, 0, "variable %q+D declared %<inline%>", decl
);
6856 if (declspecs
->noreturn_p
)
6857 pedwarn (loc
, 0, "variable %q+D declared %<_Noreturn%>", decl
);
6859 /* At file scope, an initialized extern declaration may follow
6860 a static declaration. In that case, DECL_EXTERNAL will be
6861 reset later in start_decl. */
6862 DECL_EXTERNAL (decl
) = (storage_class
== csc_extern
);
6864 /* At file scope, the presence of a `static' or `register' storage
6865 class specifier, or the absence of all storage class specifiers
6866 makes this declaration a definition (perhaps tentative). Also,
6867 the absence of `static' makes it public. */
6868 if (current_scope
== file_scope
)
6870 TREE_PUBLIC (decl
) = storage_class
!= csc_static
;
6871 TREE_STATIC (decl
) = !extern_ref
;
6873 /* Not at file scope, only `static' makes a static definition. */
6876 TREE_STATIC (decl
) = (storage_class
== csc_static
);
6877 TREE_PUBLIC (decl
) = extern_ref
;
6881 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
6884 if ((storage_class
== csc_extern
6885 || (storage_class
== csc_none
6886 && TREE_CODE (type
) == FUNCTION_TYPE
6888 && variably_modified_type_p (type
, NULL_TREE
))
6891 if (TREE_CODE (type
) == FUNCTION_TYPE
)
6892 error_at (loc
, "non-nested function with variably modified type");
6894 error_at (loc
, "object with variably modified type must have "
6898 /* Record `register' declaration for warnings on &
6899 and in case doing stupid register allocation. */
6901 if (storage_class
== csc_register
)
6903 C_DECL_REGISTER (decl
) = 1;
6904 DECL_REGISTER (decl
) = 1;
6907 /* Record constancy and volatility. */
6908 c_apply_type_quals_to_decl (type_quals
, decl
);
6910 /* Apply _Alignas specifiers. */
6913 SET_DECL_ALIGN (decl
, alignas_align
* BITS_PER_UNIT
);
6914 DECL_USER_ALIGN (decl
) = 1;
6917 /* If a type has volatile components, it should be stored in memory.
6918 Otherwise, the fact that those components are volatile
6919 will be ignored, and would even crash the compiler.
6920 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6921 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl
))
6922 && (VAR_P (decl
) || TREE_CODE (decl
) == PARM_DECL
6923 || TREE_CODE (decl
) == RESULT_DECL
))
6925 /* It is not an error for a structure with volatile fields to
6926 be declared register, but reset DECL_REGISTER since it
6927 cannot actually go in a register. */
6928 int was_reg
= C_DECL_REGISTER (decl
);
6929 C_DECL_REGISTER (decl
) = 0;
6930 DECL_REGISTER (decl
) = 0;
6931 c_mark_addressable (decl
);
6932 C_DECL_REGISTER (decl
) = was_reg
;
6935 /* This is the earliest point at which we might know the assembler
6936 name of a variable. Thus, if it's known before this, die horribly. */
6937 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl
));
6941 && TREE_PUBLIC (decl
)
6942 && TREE_STATIC (decl
)
6943 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
))
6944 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
6945 && TYPE_NAME (TREE_TYPE (decl
)) == NULL_TREE
)
6946 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
6947 ("non-local variable %qD with anonymous type is "
6948 "questionable in C++"),
6955 /* Decode the parameter-list info for a function type or function definition.
6956 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6957 if there is an identifier list instead of a parameter decl list).
6958 These two functions are separate because when a function returns
6959 or receives functions then each is called multiple times but the order
6960 of calls is different. The last call to `grokparms' is always the one
6961 that contains the formal parameter names of a function definition.
6963 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6965 FUNCDEF_FLAG is true for a function definition, false for
6966 a mere declaration. A nonempty identifier-list gets an error message
6967 when FUNCDEF_FLAG is false. */
6970 grokparms (struct c_arg_info
*arg_info
, bool funcdef_flag
)
6972 tree arg_types
= arg_info
->types
;
6974 if (funcdef_flag
&& arg_info
->had_vla_unspec
)
6976 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6978 error ("%<[*]%> not allowed in other than function prototype scope");
6981 if (arg_types
== 0 && !funcdef_flag
6982 && !in_system_header_at (input_location
))
6983 warning (OPT_Wstrict_prototypes
,
6984 "function declaration isn%'t a prototype");
6986 if (arg_types
== error_mark_node
)
6987 return 0; /* don't set TYPE_ARG_TYPES in this case */
6989 else if (arg_types
&& TREE_CODE (TREE_VALUE (arg_types
)) == IDENTIFIER_NODE
)
6993 pedwarn (input_location
, 0, "parameter names (without types) in function declaration");
6994 arg_info
->parms
= NULL_TREE
;
6997 arg_info
->parms
= arg_info
->types
;
6999 arg_info
->types
= 0;
7004 tree parm
, type
, typelt
;
7005 unsigned int parmno
;
7007 /* If there is a parameter of incomplete type in a definition,
7008 this is an error. In a declaration this is valid, and a
7009 struct or union type may be completed later, before any calls
7010 or definition of the function. In the case where the tag was
7011 first declared within the parameter list, a warning has
7012 already been given. If a parameter has void type, then
7013 however the function cannot be defined or called, so
7016 for (parm
= arg_info
->parms
, typelt
= arg_types
, parmno
= 1;
7018 parm
= DECL_CHAIN (parm
), typelt
= TREE_CHAIN (typelt
), parmno
++)
7020 type
= TREE_VALUE (typelt
);
7021 if (type
== error_mark_node
)
7024 if (!COMPLETE_TYPE_P (type
))
7028 if (DECL_NAME (parm
))
7029 error_at (input_location
,
7030 "parameter %u (%q+D) has incomplete type",
7033 error_at (DECL_SOURCE_LOCATION (parm
),
7034 "parameter %u has incomplete type",
7037 TREE_VALUE (typelt
) = error_mark_node
;
7038 TREE_TYPE (parm
) = error_mark_node
;
7039 arg_types
= NULL_TREE
;
7041 else if (VOID_TYPE_P (type
))
7043 if (DECL_NAME (parm
))
7044 warning_at (input_location
, 0,
7045 "parameter %u (%q+D) has void type",
7048 warning_at (DECL_SOURCE_LOCATION (parm
), 0,
7049 "parameter %u has void type",
7054 if (DECL_NAME (parm
) && TREE_USED (parm
))
7055 warn_if_shadowing (parm
);
7061 /* Allocate and initialize a c_arg_info structure from the parser's
7065 build_arg_info (void)
7067 struct c_arg_info
*ret
= XOBNEW (&parser_obstack
, struct c_arg_info
);
7068 ret
->parms
= NULL_TREE
;
7070 ret
->types
= NULL_TREE
;
7071 ret
->others
= NULL_TREE
;
7072 ret
->pending_sizes
= NULL
;
7073 ret
->had_vla_unspec
= 0;
7077 /* Take apart the current scope and return a c_arg_info structure with
7078 info on a parameter list just parsed.
7080 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7082 ELLIPSIS being true means the argument list ended in '...' so don't
7083 append a sentinel (void_list_node) to the end of the type-list.
7085 EXPR is NULL or an expression that needs to be evaluated for the
7086 side effects of array size expressions in the parameters. */
7089 get_parm_info (bool ellipsis
, tree expr
)
7091 struct c_binding
*b
= current_scope
->bindings
;
7092 struct c_arg_info
*arg_info
= build_arg_info ();
7095 vec
<c_arg_tag
, va_gc
> *tags
= NULL
;
7099 bool gave_void_only_once_err
= false;
7101 arg_info
->had_vla_unspec
= current_scope
->had_vla_unspec
;
7103 /* The bindings in this scope must not get put into a block.
7104 We will take care of deleting the binding nodes. */
7105 current_scope
->bindings
= 0;
7107 /* This function is only called if there was *something* on the
7111 /* A parameter list consisting solely of 'void' indicates that the
7112 function takes no arguments. But if the 'void' is qualified
7113 (by 'const' or 'volatile'), or has a storage class specifier
7114 ('register'), then the behavior is undefined; issue an error.
7115 Typedefs for 'void' are OK (see DR#157). */
7116 if (b
->prev
== 0 /* one binding */
7117 && TREE_CODE (b
->decl
) == PARM_DECL
/* which is a parameter */
7118 && !DECL_NAME (b
->decl
) /* anonymous */
7119 && VOID_TYPE_P (TREE_TYPE (b
->decl
))) /* of void type */
7121 if (TYPE_QUALS (TREE_TYPE (b
->decl
)) != TYPE_UNQUALIFIED
7122 || C_DECL_REGISTER (b
->decl
))
7123 error_at (b
->locus
, "%<void%> as only parameter may not be qualified");
7125 /* There cannot be an ellipsis. */
7127 error_at (b
->locus
, "%<void%> must be the only parameter");
7129 arg_info
->types
= void_list_node
;
7134 types
= void_list_node
;
7136 /* Break up the bindings list into parms, tags, types, and others;
7137 apply sanity checks; purge the name-to-decl bindings. */
7140 tree decl
= b
->decl
;
7141 tree type
= TREE_TYPE (decl
);
7143 const char *keyword
;
7145 switch (TREE_CODE (decl
))
7150 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
7151 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
7154 /* Check for forward decls that never got their actual decl. */
7155 if (TREE_ASM_WRITTEN (decl
))
7157 "parameter %q+D has just a forward declaration", decl
);
7158 /* Check for (..., void, ...) and issue an error. */
7159 else if (VOID_TYPE_P (type
) && !DECL_NAME (decl
))
7161 if (!gave_void_only_once_err
)
7163 error_at (b
->locus
, "%<void%> must be the only parameter");
7164 gave_void_only_once_err
= true;
7169 /* Valid parameter, add it to the list. */
7170 DECL_CHAIN (decl
) = parms
;
7173 /* Since there is a prototype, args are passed in their
7174 declared types. The back end may override this later. */
7175 DECL_ARG_TYPE (decl
) = type
;
7176 types
= tree_cons (0, type
, types
);
7180 case ENUMERAL_TYPE
: keyword
= "enum"; goto tag
;
7181 case UNION_TYPE
: keyword
= "union"; goto tag
;
7182 case RECORD_TYPE
: keyword
= "struct"; goto tag
;
7184 /* Types may not have tag-names, in which case the type
7185 appears in the bindings list with b->id NULL. */
7188 gcc_assert (I_TAG_BINDING (b
->id
) == b
);
7189 I_TAG_BINDING (b
->id
) = b
->shadowed
;
7192 /* Warn about any struct, union or enum tags defined in a
7193 parameter list. The scope of such types is limited to
7194 the parameter list, which is rarely if ever desirable
7195 (it's impossible to call such a function with type-
7196 correct arguments). An anonymous union parm type is
7197 meaningful as a GNU extension, so don't warn for that. */
7198 if (TREE_CODE (decl
) != UNION_TYPE
|| b
->id
!= 0)
7201 /* The %s will be one of 'struct', 'union', or 'enum'. */
7202 warning_at (b
->locus
, 0,
7203 "%<%s %E%> declared inside parameter list"
7204 " will not be visible outside of this definition or"
7205 " declaration", keyword
, b
->id
);
7207 /* The %s will be one of 'struct', 'union', or 'enum'. */
7208 warning_at (b
->locus
, 0,
7209 "anonymous %s declared inside parameter list"
7210 " will not be visible outside of this definition or"
7211 " declaration", keyword
);
7216 vec_safe_push (tags
, tag
);
7220 /* FUNCTION_DECLs appear when there is an implicit function
7221 declaration in the parameter list. */
7222 gcc_assert (b
->nested
|| seen_error ());
7227 /* CONST_DECLs appear here when we have an embedded enum,
7228 and TYPE_DECLs appear here when we have an embedded struct
7229 or union. No warnings for this - we already warned about the
7232 /* When we reinsert this decl in the function body, we need
7233 to reconstruct whether it was marked as nested. */
7234 gcc_assert (!b
->nested
);
7235 DECL_CHAIN (decl
) = others
;
7241 /* error_mark_node appears here when we have an undeclared
7242 variable. Just throw it away. */
7245 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
7246 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
7250 /* Other things that might be encountered. */
7257 b
= free_binding_and_advance (b
);
7260 arg_info
->parms
= parms
;
7261 arg_info
->tags
= tags
;
7262 arg_info
->types
= types
;
7263 arg_info
->others
= others
;
7264 arg_info
->pending_sizes
= expr
;
7268 /* Get the struct, enum or union (CODE says which) with tag NAME.
7269 Define the tag as a forward-reference with location LOC if it is
7270 not defined. Return a c_typespec structure for the type
7274 parser_xref_tag (location_t loc
, enum tree_code code
, tree name
)
7276 struct c_typespec ret
;
7280 ret
.expr
= NULL_TREE
;
7281 ret
.expr_const_operands
= true;
7283 /* If a cross reference is requested, look up the type
7284 already defined for this tag and return it. */
7286 ref
= lookup_tag (code
, name
, false, &refloc
);
7287 /* If this is the right type of tag, return what we found.
7288 (This reference will be shadowed by shadow_tag later if appropriate.)
7289 If this is the wrong type of tag, do not return it. If it was the
7290 wrong type in the same scope, we will have had an error
7291 message already; if in a different scope and declaring
7292 a name, pending_xref_error will give an error message; but if in a
7293 different scope and not declaring a name, this tag should
7294 shadow the previous declaration of a different type of tag, and
7295 this would not work properly if we return the reference found.
7296 (For example, with "struct foo" in an outer scope, "union foo;"
7297 must shadow that tag with a new one of union type.) */
7298 ret
.kind
= (ref
? ctsk_tagref
: ctsk_tagfirstref
);
7299 if (ref
&& TREE_CODE (ref
) == code
)
7301 if (C_TYPE_DEFINED_IN_STRUCT (ref
)
7302 && loc
!= UNKNOWN_LOCATION
7308 warning_at (loc
, OPT_Wc___compat
,
7309 ("enum type defined in struct or union "
7310 "is not visible in C++"));
7311 inform (refloc
, "enum type defined here");
7314 warning_at (loc
, OPT_Wc___compat
,
7315 ("struct defined in struct or union "
7316 "is not visible in C++"));
7317 inform (refloc
, "struct defined here");
7320 warning_at (loc
, OPT_Wc___compat
,
7321 ("union defined in struct or union "
7322 "is not visible in C++"));
7323 inform (refloc
, "union defined here");
7334 /* If no such tag is yet defined, create a forward-reference node
7335 and record it as the "definition".
7336 When a real declaration of this type is found,
7337 the forward-reference will be altered into a real type. */
7339 ref
= make_node (code
);
7340 if (code
== ENUMERAL_TYPE
)
7342 /* Give the type a default layout like unsigned int
7343 to avoid crashing if it does not get defined. */
7344 SET_TYPE_MODE (ref
, TYPE_MODE (unsigned_type_node
));
7345 SET_TYPE_ALIGN (ref
, TYPE_ALIGN (unsigned_type_node
));
7346 TYPE_USER_ALIGN (ref
) = 0;
7347 TYPE_UNSIGNED (ref
) = 1;
7348 TYPE_PRECISION (ref
) = TYPE_PRECISION (unsigned_type_node
);
7349 TYPE_MIN_VALUE (ref
) = TYPE_MIN_VALUE (unsigned_type_node
);
7350 TYPE_MAX_VALUE (ref
) = TYPE_MAX_VALUE (unsigned_type_node
);
7353 pushtag (loc
, name
, ref
);
7359 /* Get the struct, enum or union (CODE says which) with tag NAME.
7360 Define the tag as a forward-reference if it is not defined.
7361 Return a tree for the type. */
7364 xref_tag (enum tree_code code
, tree name
)
7366 return parser_xref_tag (input_location
, code
, name
).spec
;
7369 /* Make sure that the tag NAME is defined *in the current scope*
7370 at least as a forward reference.
7371 LOC is the location of the struct's definition.
7372 CODE says which kind of tag NAME ought to be.
7374 This stores the current value of the file static STRUCT_PARSE_INFO
7375 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7376 new c_struct_parse_info structure. The old value of
7377 STRUCT_PARSE_INFO is restored in finish_struct. */
7380 start_struct (location_t loc
, enum tree_code code
, tree name
,
7381 struct c_struct_parse_info
**enclosing_struct_parse_info
)
7383 /* If there is already a tag defined at this scope
7384 (as a forward reference), just return it. */
7386 tree ref
= NULL_TREE
;
7387 location_t refloc
= UNKNOWN_LOCATION
;
7389 if (name
!= NULL_TREE
)
7390 ref
= lookup_tag (code
, name
, true, &refloc
);
7391 if (ref
&& TREE_CODE (ref
) == code
)
7393 if (TYPE_SIZE (ref
))
7395 if (code
== UNION_TYPE
)
7396 error_at (loc
, "redefinition of %<union %E%>", name
);
7398 error_at (loc
, "redefinition of %<struct %E%>", name
);
7399 if (refloc
!= UNKNOWN_LOCATION
)
7400 inform (refloc
, "originally defined here");
7401 /* Don't create structures using a name already in use. */
7404 else if (C_TYPE_BEING_DEFINED (ref
))
7406 if (code
== UNION_TYPE
)
7407 error_at (loc
, "nested redefinition of %<union %E%>", name
);
7409 error_at (loc
, "nested redefinition of %<struct %E%>", name
);
7410 /* Don't bother to report "originally defined here" for a
7411 nested redefinition; the original definition should be
7413 /* Don't create structures that contain themselves. */
7418 /* Otherwise create a forward-reference just so the tag is in scope. */
7420 if (ref
== NULL_TREE
|| TREE_CODE (ref
) != code
)
7422 ref
= make_node (code
);
7423 pushtag (loc
, name
, ref
);
7426 C_TYPE_BEING_DEFINED (ref
) = 1;
7427 for (tree v
= TYPE_MAIN_VARIANT (ref
); v
; v
= TYPE_NEXT_VARIANT (v
))
7428 TYPE_PACKED (v
) = flag_pack_struct
;
7430 *enclosing_struct_parse_info
= struct_parse_info
;
7431 struct_parse_info
= new c_struct_parse_info ();
7433 /* FIXME: This will issue a warning for a use of a type defined
7434 within a statement expr used within sizeof, et. al. This is not
7435 terribly serious as C++ doesn't permit statement exprs within
7437 if (warn_cxx_compat
&& (in_sizeof
|| in_typeof
|| in_alignof
))
7438 warning_at (loc
, OPT_Wc___compat
,
7439 "defining type in %qs expression is invalid in C++",
7442 : (in_typeof
? "typeof" : "alignof")));
7447 /* Process the specs, declarator and width (NULL if omitted)
7448 of a structure component, returning a FIELD_DECL node.
7449 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7450 DECL_ATTRS is as for grokdeclarator.
7452 LOC is the location of the structure component.
7454 This is done during the parsing of the struct declaration.
7455 The FIELD_DECL nodes are chained together and the lot of them
7456 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7459 grokfield (location_t loc
,
7460 struct c_declarator
*declarator
, struct c_declspecs
*declspecs
,
7461 tree width
, tree
*decl_attrs
)
7465 if (declarator
->kind
== cdk_id
&& declarator
->u
.id
== NULL_TREE
7466 && width
== NULL_TREE
)
7468 /* This is an unnamed decl.
7470 If we have something of the form "union { list } ;" then this
7471 is the anonymous union extension. Similarly for struct.
7473 If this is something of the form "struct foo;", then
7474 If MS or Plan 9 extensions are enabled, this is handled as
7475 an anonymous struct.
7476 Otherwise this is a forward declaration of a structure tag.
7478 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7479 If foo names a structure or union without a tag, then this
7480 is an anonymous struct (this is permitted by C11).
7481 If MS or Plan 9 extensions are enabled and foo names a
7482 structure, then again this is an anonymous struct.
7483 Otherwise this is an error.
7485 Oh what a horrid tangled web we weave. I wonder if MS consciously
7486 took this from Plan 9 or if it was an accident of implementation
7487 that took root before someone noticed the bug... */
7489 tree type
= declspecs
->type
;
7490 bool type_ok
= RECORD_OR_UNION_TYPE_P (type
);
7494 && (flag_ms_extensions
7495 || flag_plan9_extensions
7496 || !declspecs
->typedef_p
))
7498 if (flag_ms_extensions
|| flag_plan9_extensions
)
7500 else if (TYPE_NAME (type
) == NULL
)
7507 pedwarn (loc
, 0, "declaration does not declare anything");
7511 pedwarn_c99 (loc
, OPT_Wpedantic
,
7512 "ISO C99 doesn%'t support unnamed structs/unions");
7514 pedwarn_c99 (loc
, OPT_Wpedantic
,
7515 "ISO C90 doesn%'t support unnamed structs/unions");
7518 value
= grokdeclarator (declarator
, declspecs
, FIELD
, false,
7519 width
? &width
: NULL
, decl_attrs
, NULL
, NULL
,
7522 finish_decl (value
, loc
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
7523 DECL_INITIAL (value
) = width
;
7525 if (warn_cxx_compat
&& DECL_NAME (value
) != NULL_TREE
)
7527 /* If we currently have a binding for this field, set the
7528 in_struct field in the binding, so that we warn about lookups
7530 struct c_binding
*b
= I_SYMBOL_BINDING (DECL_NAME (value
));
7533 /* If the in_struct field is not yet set, push it on a list
7534 to be cleared when this struct is finished. */
7537 struct_parse_info
->fields
.safe_push (b
);
7546 /* Subroutine of detect_field_duplicates: return whether X and Y,
7547 which are both fields in the same struct, have duplicate field
7551 is_duplicate_field (tree x
, tree y
)
7553 if (DECL_NAME (x
) != NULL_TREE
&& DECL_NAME (x
) == DECL_NAME (y
))
7556 /* When using -fplan9-extensions, an anonymous field whose name is a
7557 typedef can duplicate a field name. */
7558 if (flag_plan9_extensions
7559 && (DECL_NAME (x
) == NULL_TREE
|| DECL_NAME (y
) == NULL_TREE
))
7561 tree xt
, xn
, yt
, yn
;
7564 if (DECL_NAME (x
) != NULL_TREE
)
7566 else if (RECORD_OR_UNION_TYPE_P (xt
)
7567 && TYPE_NAME (xt
) != NULL_TREE
7568 && TREE_CODE (TYPE_NAME (xt
)) == TYPE_DECL
)
7569 xn
= DECL_NAME (TYPE_NAME (xt
));
7574 if (DECL_NAME (y
) != NULL_TREE
)
7576 else if (RECORD_OR_UNION_TYPE_P (yt
)
7577 && TYPE_NAME (yt
) != NULL_TREE
7578 && TREE_CODE (TYPE_NAME (yt
)) == TYPE_DECL
)
7579 yn
= DECL_NAME (TYPE_NAME (yt
));
7583 if (xn
!= NULL_TREE
&& xn
== yn
)
7590 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7591 to HTAB, giving errors for any duplicates. */
7594 detect_field_duplicates_hash (tree fieldlist
,
7595 hash_table
<nofree_ptr_hash
<tree_node
> > *htab
)
7600 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
7601 if ((y
= DECL_NAME (x
)) != 0)
7603 slot
= htab
->find_slot (y
, INSERT
);
7606 error ("duplicate member %q+D", x
);
7607 DECL_NAME (x
) = NULL_TREE
;
7611 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
7613 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x
)), htab
);
7615 /* When using -fplan9-extensions, an anonymous field whose
7616 name is a typedef can duplicate a field name. */
7617 if (flag_plan9_extensions
7618 && TYPE_NAME (TREE_TYPE (x
)) != NULL_TREE
7619 && TREE_CODE (TYPE_NAME (TREE_TYPE (x
))) == TYPE_DECL
)
7621 tree xn
= DECL_NAME (TYPE_NAME (TREE_TYPE (x
)));
7622 slot
= htab
->find_slot (xn
, INSERT
);
7624 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x
)));
7630 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7631 the list such that this does not present a problem later. */
7634 detect_field_duplicates (tree fieldlist
)
7639 /* If the struct is the list of instance variables of an Objective-C
7640 class, then we need to check all the instance variables of
7641 superclasses when checking for duplicates (since you can't have
7642 an instance variable in a subclass with the same name as an
7643 instance variable in a superclass). We pass on this job to the
7644 Objective-C compiler. objc_detect_field_duplicates() will return
7645 false if we are not checking the list of instance variables and
7646 the C frontend should proceed with the standard field duplicate
7647 checks. If we are checking the list of instance variables, the
7648 ObjC frontend will do the check, emit the errors if needed, and
7649 then return true. */
7650 if (c_dialect_objc ())
7651 if (objc_detect_field_duplicates (false))
7654 /* First, see if there are more than "a few" fields.
7655 This is trivially true if there are zero or one fields. */
7656 if (!fieldlist
|| !DECL_CHAIN (fieldlist
))
7661 if (DECL_NAME (x
) == NULL_TREE
7662 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
7665 } while (timeout
> 0 && x
);
7667 /* If there were "few" fields and no anonymous structures or unions,
7668 avoid the overhead of allocating a hash table. Instead just do
7669 the nested traversal thing. */
7672 for (x
= DECL_CHAIN (fieldlist
); x
; x
= DECL_CHAIN (x
))
7673 /* When using -fplan9-extensions, we can have duplicates
7674 between typedef names and fields. */
7676 || (flag_plan9_extensions
7677 && DECL_NAME (x
) == NULL_TREE
7678 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
))
7679 && TYPE_NAME (TREE_TYPE (x
)) != NULL_TREE
7680 && TREE_CODE (TYPE_NAME (TREE_TYPE (x
))) == TYPE_DECL
))
7682 for (y
= fieldlist
; y
!= x
; y
= TREE_CHAIN (y
))
7683 if (is_duplicate_field (y
, x
))
7685 error ("duplicate member %q+D", x
);
7686 DECL_NAME (x
) = NULL_TREE
;
7692 hash_table
<nofree_ptr_hash
<tree_node
> > htab (37);
7693 detect_field_duplicates_hash (fieldlist
, &htab
);
7697 /* Finish up struct info used by -Wc++-compat. */
7700 warn_cxx_compat_finish_struct (tree fieldlist
, enum tree_code code
,
7701 location_t record_loc
)
7705 struct c_binding
*b
;
7707 if (fieldlist
== NULL_TREE
)
7709 if (code
== RECORD_TYPE
)
7710 warning_at (record_loc
, OPT_Wc___compat
,
7711 "empty struct has size 0 in C, size 1 in C++");
7713 warning_at (record_loc
, OPT_Wc___compat
,
7714 "empty union has size 0 in C, size 1 in C++");
7717 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7718 the current struct. We do this now at the end of the struct
7719 because the flag is used to issue visibility warnings, and we
7720 only want to issue those warnings if the type is referenced
7721 outside of the struct declaration. */
7722 FOR_EACH_VEC_ELT (struct_parse_info
->struct_types
, ix
, x
)
7723 C_TYPE_DEFINED_IN_STRUCT (x
) = 1;
7725 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7726 typedefs used when declaring fields in this struct. If the name
7727 of any of the fields is also a typedef name then the struct would
7728 not parse in C++, because the C++ lookup rules say that the
7729 typedef name would be looked up in the context of the struct, and
7730 would thus be the field rather than the typedef. */
7731 if (!struct_parse_info
->typedefs_seen
.is_empty ()
7732 && fieldlist
!= NULL_TREE
)
7734 /* Use a hash_set<tree> using the name of the typedef. We can use
7735 a hash_set<tree> because identifiers are interned. */
7736 hash_set
<tree
> tset
;
7738 FOR_EACH_VEC_ELT (struct_parse_info
->typedefs_seen
, ix
, x
)
7739 tset
.add (DECL_NAME (x
));
7741 for (x
= fieldlist
; x
!= NULL_TREE
; x
= DECL_CHAIN (x
))
7743 if (DECL_NAME (x
) != NULL_TREE
7744 && tset
.contains (DECL_NAME (x
)))
7746 warning_at (DECL_SOURCE_LOCATION (x
), OPT_Wc___compat
,
7747 ("using %qD as both field and typedef name is "
7750 /* FIXME: It would be nice to report the location where
7751 the typedef name is used. */
7756 /* For each field which has a binding and which was not defined in
7757 an enclosing struct, clear the in_struct field. */
7758 FOR_EACH_VEC_ELT (struct_parse_info
->fields
, ix
, b
)
7762 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7763 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7764 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7765 ATTRIBUTES are attributes to be applied to the structure.
7767 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7768 the struct was started. */
7771 finish_struct (location_t loc
, tree t
, tree fieldlist
, tree attributes
,
7772 struct c_struct_parse_info
*enclosing_struct_parse_info
)
7775 bool toplevel
= file_scope
== current_scope
;
7776 int saw_named_field
;
7778 /* If this type was previously laid out as a forward reference,
7779 make sure we lay it out again. */
7783 decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
7787 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
7789 if (DECL_NAME (x
) != 0)
7791 if (flag_isoc11
&& RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
7797 if (TREE_CODE (t
) == UNION_TYPE
)
7800 pedwarn (loc
, OPT_Wpedantic
, "union has no named members");
7802 pedwarn (loc
, OPT_Wpedantic
, "union has no members");
7807 pedwarn (loc
, OPT_Wpedantic
, "struct has no named members");
7809 pedwarn (loc
, OPT_Wpedantic
, "struct has no members");
7814 /* Install struct as DECL_CONTEXT of each field decl.
7815 Also process specified field sizes, found in the DECL_INITIAL,
7816 storing 0 there after the type has been changed to precision equal
7817 to its width, rather than the precision of the specified standard
7818 type. (Correct layout requires the original type to have been preserved
7821 saw_named_field
= 0;
7822 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
7824 if (TREE_TYPE (x
) == error_mark_node
)
7827 DECL_CONTEXT (x
) = t
;
7829 /* If any field is const, the structure type is pseudo-const. */
7830 if (TREE_READONLY (x
))
7831 C_TYPE_FIELDS_READONLY (t
) = 1;
7834 /* A field that is pseudo-const makes the structure likewise. */
7835 tree t1
= strip_array_types (TREE_TYPE (x
));
7836 if (RECORD_OR_UNION_TYPE_P (t1
) && C_TYPE_FIELDS_READONLY (t1
))
7837 C_TYPE_FIELDS_READONLY (t
) = 1;
7840 /* Any field that is volatile means variables of this type must be
7841 treated in some ways as volatile. */
7842 if (TREE_THIS_VOLATILE (x
))
7843 C_TYPE_FIELDS_VOLATILE (t
) = 1;
7845 /* Any field of nominal variable size implies structure is too. */
7846 if (C_DECL_VARIABLE_SIZE (x
))
7847 C_TYPE_VARIABLE_SIZE (t
) = 1;
7849 if (DECL_INITIAL (x
))
7851 unsigned HOST_WIDE_INT width
= tree_to_uhwi (DECL_INITIAL (x
));
7852 DECL_SIZE (x
) = bitsize_int (width
);
7853 DECL_BIT_FIELD (x
) = 1;
7854 SET_DECL_C_BIT_FIELD (x
);
7858 && (DECL_BIT_FIELD (x
)
7859 || TYPE_ALIGN (TREE_TYPE (x
)) > BITS_PER_UNIT
))
7860 DECL_PACKED (x
) = 1;
7862 /* Detect flexible array member in an invalid context. */
7863 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
7864 && TYPE_SIZE (TREE_TYPE (x
)) == NULL_TREE
7865 && TYPE_DOMAIN (TREE_TYPE (x
)) != NULL_TREE
7866 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x
))) == NULL_TREE
)
7868 if (TREE_CODE (t
) == UNION_TYPE
)
7870 error_at (DECL_SOURCE_LOCATION (x
),
7871 "flexible array member in union");
7872 TREE_TYPE (x
) = error_mark_node
;
7874 else if (DECL_CHAIN (x
) != NULL_TREE
)
7876 error_at (DECL_SOURCE_LOCATION (x
),
7877 "flexible array member not at end of struct");
7878 TREE_TYPE (x
) = error_mark_node
;
7880 else if (!saw_named_field
)
7882 error_at (DECL_SOURCE_LOCATION (x
),
7883 "flexible array member in otherwise empty struct");
7884 TREE_TYPE (x
) = error_mark_node
;
7888 if (pedantic
&& TREE_CODE (t
) == RECORD_TYPE
7889 && flexible_array_type_p (TREE_TYPE (x
)))
7890 pedwarn (DECL_SOURCE_LOCATION (x
), OPT_Wpedantic
,
7891 "invalid use of structure with flexible array member");
7894 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
7895 saw_named_field
= 1;
7898 detect_field_duplicates (fieldlist
);
7900 /* Now we have the nearly final fieldlist. Record it,
7901 then lay out the structure or union (including the fields). */
7903 TYPE_FIELDS (t
) = fieldlist
;
7905 maybe_apply_pragma_scalar_storage_order (t
);
7909 if (TYPE_SIZE_UNIT (t
)
7910 && TREE_CODE (TYPE_SIZE_UNIT (t
)) == INTEGER_CST
7911 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t
))
7912 && !valid_constant_size_p (TYPE_SIZE_UNIT (t
)))
7913 error ("type %qT is too large", t
);
7915 /* Give bit-fields their proper types and rewrite the type of array fields
7916 with scalar component if the enclosing type has reverse storage order. */
7917 for (tree field
= fieldlist
; field
; field
= DECL_CHAIN (field
))
7919 if (TREE_CODE (field
) == FIELD_DECL
7920 && DECL_INITIAL (field
)
7921 && TREE_TYPE (field
) != error_mark_node
)
7923 unsigned HOST_WIDE_INT width
7924 = tree_to_uhwi (DECL_INITIAL (field
));
7925 tree type
= TREE_TYPE (field
);
7926 if (width
!= TYPE_PRECISION (type
))
7929 = c_build_bitfield_integer_type (width
, TYPE_UNSIGNED (type
));
7930 DECL_MODE (field
) = TYPE_MODE (TREE_TYPE (field
));
7932 DECL_INITIAL (field
) = 0;
7934 else if (TYPE_REVERSE_STORAGE_ORDER (t
)
7935 && TREE_CODE (field
) == FIELD_DECL
7936 && TREE_CODE (TREE_TYPE (field
)) == ARRAY_TYPE
)
7938 tree ftype
= TREE_TYPE (field
);
7939 tree ctype
= strip_array_types (ftype
);
7940 if (!RECORD_OR_UNION_TYPE_P (ctype
) && TYPE_MODE (ctype
) != QImode
)
7942 tree fmain_type
= TYPE_MAIN_VARIANT (ftype
);
7943 tree
*typep
= &fmain_type
;
7945 *typep
= build_distinct_type_copy (*typep
);
7946 TYPE_REVERSE_STORAGE_ORDER (*typep
) = 1;
7947 typep
= &TREE_TYPE (*typep
);
7948 } while (TREE_CODE (*typep
) == ARRAY_TYPE
);
7950 = c_build_qualified_type (fmain_type
, TYPE_QUALS (ftype
));
7955 /* Now we have the truly final field list.
7956 Store it in this type and in the variants. */
7958 TYPE_FIELDS (t
) = fieldlist
;
7960 /* If there are lots of fields, sort so we can look through them fast.
7961 We arbitrarily consider 16 or more elts to be "a lot". */
7966 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
7968 if (len
> 15 || DECL_NAME (x
) == NULL
)
7976 struct lang_type
*space
;
7977 struct sorted_fields_type
*space2
;
7979 len
+= list_length (x
);
7981 /* Use the same allocation policy here that make_node uses, to
7982 ensure that this lives as long as the rest of the struct decl.
7983 All decls in an inline function need to be saved. */
7985 space
= ggc_cleared_alloc
<struct lang_type
> ();
7986 space2
= (sorted_fields_type
*) ggc_internal_alloc
7987 (sizeof (struct sorted_fields_type
) + len
* sizeof (tree
));
7991 field_array
= &space2
->elts
[0];
7992 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
7994 field_array
[len
++] = x
;
7996 /* If there is anonymous struct or union, break out of the loop. */
7997 if (DECL_NAME (x
) == NULL
)
8000 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8003 TYPE_LANG_SPECIFIC (t
) = space
;
8004 TYPE_LANG_SPECIFIC (t
)->s
->len
= len
;
8005 field_array
= TYPE_LANG_SPECIFIC (t
)->s
->elts
;
8006 qsort (field_array
, len
, sizeof (tree
), field_decl_cmp
);
8011 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8012 in dwarf2out via rest_of_decl_compilation below and means
8013 something totally different. Since we will be clearing
8014 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8015 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8016 C_TYPE_INCOMPLETE_VARS should use some language specific
8018 tree incomplete_vars
= C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t
));
8019 for (x
= TYPE_MAIN_VARIANT (t
); x
; x
= TYPE_NEXT_VARIANT (x
))
8021 TYPE_FIELDS (x
) = TYPE_FIELDS (t
);
8022 TYPE_LANG_SPECIFIC (x
) = TYPE_LANG_SPECIFIC (t
);
8023 C_TYPE_FIELDS_READONLY (x
) = C_TYPE_FIELDS_READONLY (t
);
8024 C_TYPE_FIELDS_VOLATILE (x
) = C_TYPE_FIELDS_VOLATILE (t
);
8025 C_TYPE_VARIABLE_SIZE (x
) = C_TYPE_VARIABLE_SIZE (t
);
8026 C_TYPE_INCOMPLETE_VARS (x
) = NULL_TREE
;
8029 /* If this was supposed to be a transparent union, but we can't
8030 make it one, warn and turn off the flag. */
8031 if (TREE_CODE (t
) == UNION_TYPE
8032 && TYPE_TRANSPARENT_AGGR (t
)
8033 && (!TYPE_FIELDS (t
) || TYPE_MODE (t
) != DECL_MODE (TYPE_FIELDS (t
))))
8035 TYPE_TRANSPARENT_AGGR (t
) = 0;
8036 warning_at (loc
, 0, "union cannot be made transparent");
8039 /* If this structure or union completes the type of any previous
8040 variable declaration, lay it out and output its rtl. */
8041 for (x
= incomplete_vars
; x
; x
= TREE_CHAIN (x
))
8043 tree decl
= TREE_VALUE (x
);
8044 if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
8045 layout_array_type (TREE_TYPE (decl
));
8046 if (TREE_CODE (decl
) != TYPE_DECL
)
8048 layout_decl (decl
, 0);
8049 if (c_dialect_objc ())
8050 objc_check_decl (decl
);
8051 rest_of_decl_compilation (decl
, toplevel
, 0);
8055 /* Update type location to the one of the definition, instead of e.g.
8056 a forward declaration. */
8057 if (TYPE_STUB_DECL (t
))
8058 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t
)) = loc
;
8060 /* Finish debugging output for this type. */
8061 rest_of_type_compilation (t
, toplevel
);
8063 /* If we're inside a function proper, i.e. not file-scope and not still
8064 parsing parameters, then arrange for the size of a variable sized type
8066 if (building_stmt_list_p () && variably_modified_type_p (t
, NULL_TREE
))
8067 add_stmt (build_stmt (loc
,
8068 DECL_EXPR
, build_decl (loc
, TYPE_DECL
, NULL
, t
)));
8070 if (warn_cxx_compat
)
8071 warn_cxx_compat_finish_struct (fieldlist
, TREE_CODE (t
), loc
);
8073 delete struct_parse_info
;
8075 struct_parse_info
= enclosing_struct_parse_info
;
8077 /* If this struct is defined inside a struct, add it to
8080 && struct_parse_info
!= NULL
8081 && !in_sizeof
&& !in_typeof
&& !in_alignof
)
8082 struct_parse_info
->struct_types
.safe_push (t
);
8087 /* Lay out the type T, and its element type, and so on. */
8090 layout_array_type (tree t
)
8092 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
8093 layout_array_type (TREE_TYPE (t
));
8097 /* Begin compiling the definition of an enumeration type.
8098 NAME is its name (or null if anonymous).
8099 LOC is the enum's location.
8100 Returns the type object, as yet incomplete.
8101 Also records info about it so that build_enumerator
8102 may be used to declare the individual values as they are read. */
8105 start_enum (location_t loc
, struct c_enum_contents
*the_enum
, tree name
)
8107 tree enumtype
= NULL_TREE
;
8108 location_t enumloc
= UNKNOWN_LOCATION
;
8110 /* If this is the real definition for a previous forward reference,
8111 fill in the contents in the same object that used to be the
8112 forward reference. */
8114 if (name
!= NULL_TREE
)
8115 enumtype
= lookup_tag (ENUMERAL_TYPE
, name
, true, &enumloc
);
8117 if (enumtype
== NULL_TREE
|| TREE_CODE (enumtype
) != ENUMERAL_TYPE
)
8119 enumtype
= make_node (ENUMERAL_TYPE
);
8120 pushtag (loc
, name
, enumtype
);
8123 if (C_TYPE_BEING_DEFINED (enumtype
))
8124 error_at (loc
, "nested redefinition of %<enum %E%>", name
);
8126 C_TYPE_BEING_DEFINED (enumtype
) = 1;
8128 if (TYPE_VALUES (enumtype
) != 0)
8130 /* This enum is a named one that has been declared already. */
8131 error_at (loc
, "redeclaration of %<enum %E%>", name
);
8132 if (enumloc
!= UNKNOWN_LOCATION
)
8133 inform (enumloc
, "originally defined here");
8135 /* Completely replace its old definition.
8136 The old enumerators remain defined, however. */
8137 TYPE_VALUES (enumtype
) = 0;
8140 the_enum
->enum_next_value
= integer_zero_node
;
8141 the_enum
->enum_overflow
= 0;
8143 if (flag_short_enums
)
8144 for (tree v
= TYPE_MAIN_VARIANT (enumtype
); v
; v
= TYPE_NEXT_VARIANT (v
))
8145 TYPE_PACKED (v
) = 1;
8147 /* FIXME: This will issue a warning for a use of a type defined
8148 within sizeof in a statement expr. This is not terribly serious
8149 as C++ doesn't permit statement exprs within sizeof anyhow. */
8150 if (warn_cxx_compat
&& (in_sizeof
|| in_typeof
|| in_alignof
))
8151 warning_at (loc
, OPT_Wc___compat
,
8152 "defining type in %qs expression is invalid in C++",
8155 : (in_typeof
? "typeof" : "alignof")));
8160 /* After processing and defining all the values of an enumeration type,
8161 install their decls in the enumeration type and finish it off.
8162 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8163 and ATTRIBUTES are the specified attributes.
8164 Returns ENUMTYPE. */
8167 finish_enum (tree enumtype
, tree values
, tree attributes
)
8170 tree minnode
= 0, maxnode
= 0;
8173 bool toplevel
= (file_scope
== current_scope
);
8174 struct lang_type
*lt
;
8176 decl_attributes (&enumtype
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
8178 /* Calculate the maximum value of any enumerator in this type. */
8180 if (values
== error_mark_node
)
8181 minnode
= maxnode
= integer_zero_node
;
8184 minnode
= maxnode
= TREE_VALUE (values
);
8185 for (pair
= TREE_CHAIN (values
); pair
; pair
= TREE_CHAIN (pair
))
8187 tree value
= TREE_VALUE (pair
);
8188 if (tree_int_cst_lt (maxnode
, value
))
8190 if (tree_int_cst_lt (value
, minnode
))
8195 /* Construct the final type of this enumeration. It is the same
8196 as one of the integral types - the narrowest one that fits, except
8197 that normally we only go as narrow as int - and signed iff any of
8198 the values are negative. */
8199 sign
= (tree_int_cst_sgn (minnode
) >= 0) ? UNSIGNED
: SIGNED
;
8200 precision
= MAX (tree_int_cst_min_precision (minnode
, sign
),
8201 tree_int_cst_min_precision (maxnode
, sign
));
8203 /* If the precision of the type was specified with an attribute and it
8204 was too small, give an error. Otherwise, use it. */
8205 if (TYPE_PRECISION (enumtype
) && lookup_attribute ("mode", attributes
))
8207 if (precision
> TYPE_PRECISION (enumtype
))
8209 TYPE_PRECISION (enumtype
) = 0;
8210 error ("specified mode too small for enumeral values");
8213 precision
= TYPE_PRECISION (enumtype
);
8216 TYPE_PRECISION (enumtype
) = 0;
8218 if (TYPE_PACKED (enumtype
)
8219 || precision
> TYPE_PRECISION (integer_type_node
)
8220 || TYPE_PRECISION (enumtype
))
8222 tem
= c_common_type_for_size (precision
, sign
== UNSIGNED
? 1 : 0);
8225 warning (0, "enumeration values exceed range of largest integer");
8226 tem
= long_long_integer_type_node
;
8230 tem
= sign
== UNSIGNED
? unsigned_type_node
: integer_type_node
;
8232 TYPE_MIN_VALUE (enumtype
) = TYPE_MIN_VALUE (tem
);
8233 TYPE_MAX_VALUE (enumtype
) = TYPE_MAX_VALUE (tem
);
8234 TYPE_UNSIGNED (enumtype
) = TYPE_UNSIGNED (tem
);
8235 SET_TYPE_ALIGN (enumtype
, TYPE_ALIGN (tem
));
8236 TYPE_SIZE (enumtype
) = 0;
8237 TYPE_PRECISION (enumtype
) = TYPE_PRECISION (tem
);
8239 layout_type (enumtype
);
8241 if (values
!= error_mark_node
)
8243 /* Change the type of the enumerators to be the enum type. We
8244 need to do this irrespective of the size of the enum, for
8245 proper type checking. Replace the DECL_INITIALs of the
8246 enumerators, and the value slots of the list, with copies
8247 that have the enum type; they cannot be modified in place
8248 because they may be shared (e.g. integer_zero_node) Finally,
8249 change the purpose slots to point to the names of the decls. */
8250 for (pair
= values
; pair
; pair
= TREE_CHAIN (pair
))
8252 tree enu
= TREE_PURPOSE (pair
);
8253 tree ini
= DECL_INITIAL (enu
);
8255 TREE_TYPE (enu
) = enumtype
;
8257 /* The ISO C Standard mandates enumerators to have type int,
8258 even though the underlying type of an enum type is
8259 unspecified. However, GCC allows enumerators of any
8260 integer type as an extensions. build_enumerator()
8261 converts any enumerators that fit in an int to type int,
8262 to avoid promotions to unsigned types when comparing
8263 integers with enumerators that fit in the int range.
8264 When -pedantic is given, build_enumerator() would have
8265 already warned about those that don't fit. Here we
8266 convert the rest to the enumerator type. */
8267 if (TREE_TYPE (ini
) != integer_type_node
)
8268 ini
= convert (enumtype
, ini
);
8270 DECL_INITIAL (enu
) = ini
;
8271 TREE_PURPOSE (pair
) = DECL_NAME (enu
);
8272 TREE_VALUE (pair
) = ini
;
8275 TYPE_VALUES (enumtype
) = values
;
8278 /* Record the min/max values so that we can warn about bit-field
8279 enumerations that are too small for the values. */
8280 lt
= ggc_cleared_alloc
<struct lang_type
> ();
8281 lt
->enum_min
= minnode
;
8282 lt
->enum_max
= maxnode
;
8283 TYPE_LANG_SPECIFIC (enumtype
) = lt
;
8285 /* Fix up all variant types of this enum type. */
8286 for (tem
= TYPE_MAIN_VARIANT (enumtype
); tem
; tem
= TYPE_NEXT_VARIANT (tem
))
8288 if (tem
== enumtype
)
8290 TYPE_VALUES (tem
) = TYPE_VALUES (enumtype
);
8291 TYPE_MIN_VALUE (tem
) = TYPE_MIN_VALUE (enumtype
);
8292 TYPE_MAX_VALUE (tem
) = TYPE_MAX_VALUE (enumtype
);
8293 TYPE_SIZE (tem
) = TYPE_SIZE (enumtype
);
8294 TYPE_SIZE_UNIT (tem
) = TYPE_SIZE_UNIT (enumtype
);
8295 SET_TYPE_MODE (tem
, TYPE_MODE (enumtype
));
8296 TYPE_PRECISION (tem
) = TYPE_PRECISION (enumtype
);
8297 SET_TYPE_ALIGN (tem
, TYPE_ALIGN (enumtype
));
8298 TYPE_USER_ALIGN (tem
) = TYPE_USER_ALIGN (enumtype
);
8299 TYPE_UNSIGNED (tem
) = TYPE_UNSIGNED (enumtype
);
8300 TYPE_LANG_SPECIFIC (tem
) = TYPE_LANG_SPECIFIC (enumtype
);
8303 /* Finish debugging output for this type. */
8304 rest_of_type_compilation (enumtype
, toplevel
);
8306 /* If this enum is defined inside a struct, add it to
8309 && struct_parse_info
!= NULL
8310 && !in_sizeof
&& !in_typeof
&& !in_alignof
)
8311 struct_parse_info
->struct_types
.safe_push (enumtype
);
8316 /* Build and install a CONST_DECL for one value of the
8317 current enumeration type (one that was begun with start_enum).
8318 DECL_LOC is the location of the enumerator.
8319 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8320 Return a tree-list containing the CONST_DECL and its value.
8321 Assignment of sequential values by default is handled here. */
8324 build_enumerator (location_t decl_loc
, location_t loc
,
8325 struct c_enum_contents
*the_enum
, tree name
, tree value
)
8329 /* Validate and default VALUE. */
8333 /* Don't issue more errors for error_mark_node (i.e. an
8334 undeclared identifier) - just ignore the value expression. */
8335 if (value
== error_mark_node
)
8337 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value
)))
8339 error_at (loc
, "enumerator value for %qE is not an integer constant",
8345 if (TREE_CODE (value
) != INTEGER_CST
)
8347 value
= c_fully_fold (value
, false, NULL
);
8348 if (TREE_CODE (value
) == INTEGER_CST
)
8349 pedwarn (loc
, OPT_Wpedantic
,
8350 "enumerator value for %qE is not an integer "
8351 "constant expression", name
);
8353 if (TREE_CODE (value
) != INTEGER_CST
)
8355 error ("enumerator value for %qE is not an integer constant",
8361 value
= default_conversion (value
);
8362 constant_expression_warning (value
);
8367 /* Default based on previous value. */
8368 /* It should no longer be possible to have NON_LVALUE_EXPR
8372 value
= the_enum
->enum_next_value
;
8373 if (the_enum
->enum_overflow
)
8374 error_at (loc
, "overflow in enumeration values");
8376 /* Even though the underlying type of an enum is unspecified, the
8377 type of enumeration constants is explicitly defined as int
8378 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8380 else if (!int_fits_type_p (value
, integer_type_node
))
8381 pedwarn (loc
, OPT_Wpedantic
,
8382 "ISO C restricts enumerator values to range of %<int%>");
8384 /* The ISO C Standard mandates enumerators to have type int, even
8385 though the underlying type of an enum type is unspecified.
8386 However, GCC allows enumerators of any integer type as an
8387 extensions. Here we convert any enumerators that fit in an int
8388 to type int, to avoid promotions to unsigned types when comparing
8389 integers with enumerators that fit in the int range. When
8390 -pedantic is given, we would have already warned about those that
8391 don't fit. We have to do this here rather than in finish_enum
8392 because this value may be used to define more enumerators. */
8393 if (int_fits_type_p (value
, integer_type_node
))
8394 value
= convert (integer_type_node
, value
);
8396 /* Set basis for default for next value. */
8397 the_enum
->enum_next_value
8398 = build_binary_op (EXPR_LOC_OR_LOC (value
, input_location
),
8399 PLUS_EXPR
, value
, integer_one_node
, 0);
8400 the_enum
->enum_overflow
= tree_int_cst_lt (the_enum
->enum_next_value
, value
);
8402 /* Now create a declaration for the enum value name. */
8404 type
= TREE_TYPE (value
);
8405 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
8406 TYPE_PRECISION (integer_type_node
)),
8407 (TYPE_PRECISION (type
)
8408 >= TYPE_PRECISION (integer_type_node
)
8409 && TYPE_UNSIGNED (type
)));
8411 decl
= build_decl (decl_loc
, CONST_DECL
, name
, type
);
8412 DECL_INITIAL (decl
) = convert (type
, value
);
8415 return tree_cons (decl
, value
, NULL_TREE
);
8419 /* Create the FUNCTION_DECL for a function definition.
8420 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8421 the declaration; they describe the function's name and the type it returns,
8422 but twisted together in a fashion that parallels the syntax of C.
8424 This function creates a binding context for the function body
8425 as well as setting up the FUNCTION_DECL in current_function_decl.
8427 Returns 1 on success. If the DECLARATOR is not suitable for a function
8428 (it defines a datum instead), we return 0, which tells
8429 yyparse to report a parse error. */
8432 start_function (struct c_declspecs
*declspecs
, struct c_declarator
*declarator
,
8435 tree decl1
, old_decl
;
8436 tree restype
, resdecl
;
8439 current_function_returns_value
= 0; /* Assume, until we see it does. */
8440 current_function_returns_null
= 0;
8441 current_function_returns_abnormally
= 0;
8442 warn_about_return_type
= 0;
8443 c_switch_stack
= NULL
;
8445 /* Indicate no valid break/continue context by setting these variables
8446 to some non-null, non-label value. We'll notice and emit the proper
8447 error message in c_finish_bc_stmt. */
8448 c_break_label
= c_cont_label
= size_zero_node
;
8450 decl1
= grokdeclarator (declarator
, declspecs
, FUNCDEF
, true, NULL
,
8451 &attributes
, NULL
, NULL
, DEPRECATED_NORMAL
);
8452 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION
, decl1
);
8454 /* If the declarator is not suitable for a function definition,
8455 cause a syntax error. */
8457 || TREE_CODE (decl1
) != FUNCTION_DECL
)
8460 loc
= DECL_SOURCE_LOCATION (decl1
);
8462 c_decl_attributes (&decl1
, attributes
, 0);
8464 if (DECL_DECLARED_INLINE_P (decl1
)
8465 && DECL_UNINLINABLE (decl1
)
8466 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1
)))
8467 warning_at (loc
, OPT_Wattributes
,
8468 "inline function %qD given attribute noinline",
8471 /* Handle gnu_inline attribute. */
8472 if (declspecs
->inline_p
8473 && !flag_gnu89_inline
8474 && TREE_CODE (decl1
) == FUNCTION_DECL
8475 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1
))
8476 || current_function_decl
))
8478 if (declspecs
->storage_class
!= csc_static
)
8479 DECL_EXTERNAL (decl1
) = !DECL_EXTERNAL (decl1
);
8482 announce_function (decl1
);
8484 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1
))))
8486 error_at (loc
, "return type is an incomplete type");
8487 /* Make it return void instead. */
8489 = build_function_type (void_type_node
,
8490 TYPE_ARG_TYPES (TREE_TYPE (decl1
)));
8493 if (warn_about_return_type
)
8494 warn_defaults_to (loc
, flag_isoc99
? OPT_Wimplicit_int
8495 : (warn_return_type
? OPT_Wreturn_type
8496 : OPT_Wimplicit_int
),
8497 "return type defaults to %<int%>");
8499 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8500 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8501 DECL_INITIAL (decl1
) = error_mark_node
;
8503 /* A nested function is not global. */
8504 if (current_function_decl
!= 0)
8505 TREE_PUBLIC (decl1
) = 0;
8507 /* If this definition isn't a prototype and we had a prototype declaration
8508 before, copy the arg type info from that prototype. */
8509 old_decl
= lookup_name_in_scope (DECL_NAME (decl1
), current_scope
);
8510 if (old_decl
&& TREE_CODE (old_decl
) != FUNCTION_DECL
)
8512 current_function_prototype_locus
= UNKNOWN_LOCATION
;
8513 current_function_prototype_built_in
= false;
8514 current_function_prototype_arg_types
= NULL_TREE
;
8515 if (!prototype_p (TREE_TYPE (decl1
)))
8517 if (old_decl
!= 0 && TREE_CODE (TREE_TYPE (old_decl
)) == FUNCTION_TYPE
8518 && comptypes (TREE_TYPE (TREE_TYPE (decl1
)),
8519 TREE_TYPE (TREE_TYPE (old_decl
))))
8521 if (stdarg_p (TREE_TYPE (old_decl
)))
8523 warning_at (loc
, 0, "%q+D defined as variadic function "
8524 "without prototype", decl1
);
8525 locate_old_decl (old_decl
);
8527 TREE_TYPE (decl1
) = composite_type (TREE_TYPE (old_decl
),
8529 current_function_prototype_locus
= DECL_SOURCE_LOCATION (old_decl
);
8530 current_function_prototype_built_in
8531 = C_DECL_BUILTIN_PROTOTYPE (old_decl
);
8532 current_function_prototype_arg_types
8533 = TYPE_ARG_TYPES (TREE_TYPE (decl1
));
8535 if (TREE_PUBLIC (decl1
))
8537 /* If there is an external prototype declaration of this
8538 function, record its location but do not copy information
8539 to this decl. This may be an invisible declaration
8540 (built-in or in a scope which has finished) or simply
8541 have more refined argument types than any declaration
8543 struct c_binding
*b
;
8544 for (b
= I_SYMBOL_BINDING (DECL_NAME (decl1
)); b
; b
= b
->shadowed
)
8545 if (B_IN_SCOPE (b
, external_scope
))
8549 tree ext_decl
, ext_type
;
8551 ext_type
= b
->u
.type
? b
->u
.type
: TREE_TYPE (ext_decl
);
8552 if (TREE_CODE (ext_type
) == FUNCTION_TYPE
8553 && comptypes (TREE_TYPE (TREE_TYPE (decl1
)),
8554 TREE_TYPE (ext_type
)))
8556 current_function_prototype_locus
8557 = DECL_SOURCE_LOCATION (ext_decl
);
8558 current_function_prototype_built_in
8559 = C_DECL_BUILTIN_PROTOTYPE (ext_decl
);
8560 current_function_prototype_arg_types
8561 = TYPE_ARG_TYPES (ext_type
);
8567 /* Optionally warn of old-fashioned def with no previous prototype. */
8568 if (warn_strict_prototypes
8569 && old_decl
!= error_mark_node
8570 && !prototype_p (TREE_TYPE (decl1
))
8571 && C_DECL_ISNT_PROTOTYPE (old_decl
))
8572 warning_at (loc
, OPT_Wstrict_prototypes
,
8573 "function declaration isn%'t a prototype");
8574 /* Optionally warn of any global def with no previous prototype. */
8575 else if (warn_missing_prototypes
8576 && old_decl
!= error_mark_node
8577 && TREE_PUBLIC (decl1
)
8578 && !MAIN_NAME_P (DECL_NAME (decl1
))
8579 && C_DECL_ISNT_PROTOTYPE (old_decl
)
8580 && !DECL_DECLARED_INLINE_P (decl1
))
8581 warning_at (loc
, OPT_Wmissing_prototypes
,
8582 "no previous prototype for %qD", decl1
);
8583 /* Optionally warn of any def with no previous prototype
8584 if the function has already been used. */
8585 else if (warn_missing_prototypes
8587 && old_decl
!= error_mark_node
8588 && TREE_USED (old_decl
)
8589 && !prototype_p (TREE_TYPE (old_decl
)))
8590 warning_at (loc
, OPT_Wmissing_prototypes
,
8591 "%qD was used with no prototype before its definition", decl1
);
8592 /* Optionally warn of any global def with no previous declaration. */
8593 else if (warn_missing_declarations
8594 && TREE_PUBLIC (decl1
)
8596 && !MAIN_NAME_P (DECL_NAME (decl1
))
8597 && !DECL_DECLARED_INLINE_P (decl1
))
8598 warning_at (loc
, OPT_Wmissing_declarations
,
8599 "no previous declaration for %qD",
8601 /* Optionally warn of any def with no previous declaration
8602 if the function has already been used. */
8603 else if (warn_missing_declarations
8605 && old_decl
!= error_mark_node
8606 && TREE_USED (old_decl
)
8607 && C_DECL_IMPLICIT (old_decl
))
8608 warning_at (loc
, OPT_Wmissing_declarations
,
8609 "%qD was used with no declaration before its definition", decl1
);
8611 /* This function exists in static storage.
8612 (This does not mean `static' in the C sense!) */
8613 TREE_STATIC (decl1
) = 1;
8615 /* This is the earliest point at which we might know the assembler
8616 name of the function. Thus, if it's set before this, die horribly. */
8617 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1
));
8619 /* If #pragma weak was used, mark the decl weak now. */
8620 if (current_scope
== file_scope
)
8621 maybe_apply_pragma_weak (decl1
);
8623 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8624 if (warn_main
&& MAIN_NAME_P (DECL_NAME (decl1
)))
8626 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1
)))
8627 != integer_type_node
)
8628 pedwarn (loc
, OPT_Wmain
, "return type of %qD is not %<int%>", decl1
);
8629 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1
))))
8630 pedwarn (loc
, OPT_Wmain
, "%<_Atomic%>-qualified return type of %qD",
8633 check_main_parameter_types (decl1
);
8635 if (!TREE_PUBLIC (decl1
))
8636 pedwarn (loc
, OPT_Wmain
,
8637 "%qD is normally a non-static function", decl1
);
8640 /* Record the decl so that the function name is defined.
8641 If we already have a decl for this name, and it is a FUNCTION_DECL,
8642 use the old decl. */
8644 current_function_decl
= pushdecl (decl1
);
8647 declare_parm_level ();
8649 restype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
8650 resdecl
= build_decl (loc
, RESULT_DECL
, NULL_TREE
, restype
);
8651 DECL_ARTIFICIAL (resdecl
) = 1;
8652 DECL_IGNORED_P (resdecl
) = 1;
8653 DECL_RESULT (current_function_decl
) = resdecl
;
8655 start_fname_decls ();
8660 /* Subroutine of store_parm_decls which handles new-style function
8661 definitions (prototype format). The parms already have decls, so we
8662 need only record them as in effect and complain if any redundant
8663 old-style parm decls were written. */
8665 store_parm_decls_newstyle (tree fndecl
, const struct c_arg_info
*arg_info
)
8671 if (current_scope
->bindings
)
8673 error_at (DECL_SOURCE_LOCATION (fndecl
),
8674 "old-style parameter declarations in prototyped "
8675 "function definition");
8677 /* Get rid of the old-style declarations. */
8681 /* Don't issue this warning for nested functions, and don't issue this
8682 warning if we got here because ARG_INFO_TYPES was error_mark_node
8683 (this happens when a function definition has just an ellipsis in
8684 its parameter list). */
8685 else if (!in_system_header_at (input_location
)
8686 && !current_function_scope
8687 && arg_info
->types
!= error_mark_node
)
8688 warning_at (DECL_SOURCE_LOCATION (fndecl
), OPT_Wtraditional
,
8689 "traditional C rejects ISO C style function definitions");
8691 /* Now make all the parameter declarations visible in the function body.
8692 We can bypass most of the grunt work of pushdecl. */
8693 for (decl
= arg_info
->parms
; decl
; decl
= DECL_CHAIN (decl
))
8695 DECL_CONTEXT (decl
) = current_function_decl
;
8696 if (DECL_NAME (decl
))
8698 bind (DECL_NAME (decl
), decl
, current_scope
,
8699 /*invisible=*/false, /*nested=*/false,
8701 if (!TREE_USED (decl
))
8702 warn_if_shadowing (decl
);
8705 error_at (DECL_SOURCE_LOCATION (decl
), "parameter name omitted");
8708 /* Record the parameter list in the function declaration. */
8709 DECL_ARGUMENTS (fndecl
) = arg_info
->parms
;
8711 /* Now make all the ancillary declarations visible, likewise. */
8712 for (decl
= arg_info
->others
; decl
; decl
= DECL_CHAIN (decl
))
8714 DECL_CONTEXT (decl
) = current_function_decl
;
8715 if (DECL_NAME (decl
))
8716 bind (DECL_NAME (decl
), decl
, current_scope
,
8717 /*invisible=*/false,
8718 /*nested=*/(TREE_CODE (decl
) == FUNCTION_DECL
),
8722 /* And all the tag declarations. */
8723 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info
->tags
, ix
, tag
)
8725 bind (tag
->id
, tag
->type
, current_scope
,
8726 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
8729 /* Subroutine of store_parm_decls which handles old-style function
8730 definitions (separate parameter list and declarations). */
8733 store_parm_decls_oldstyle (tree fndecl
, const struct c_arg_info
*arg_info
)
8735 struct c_binding
*b
;
8736 tree parm
, decl
, last
;
8737 tree parmids
= arg_info
->parms
;
8738 hash_set
<tree
> seen_args
;
8740 if (!in_system_header_at (input_location
))
8741 warning_at (DECL_SOURCE_LOCATION (fndecl
),
8742 OPT_Wold_style_definition
, "old-style function definition");
8744 /* Match each formal parameter name with its declaration. Save each
8745 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8746 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
8748 if (TREE_VALUE (parm
) == 0)
8750 error_at (DECL_SOURCE_LOCATION (fndecl
),
8751 "parameter name missing from parameter list");
8752 TREE_PURPOSE (parm
) = 0;
8756 b
= I_SYMBOL_BINDING (TREE_VALUE (parm
));
8757 if (b
&& B_IN_CURRENT_SCOPE (b
))
8760 /* Skip erroneous parameters. */
8761 if (decl
== error_mark_node
)
8763 /* If we got something other than a PARM_DECL it is an error. */
8764 if (TREE_CODE (decl
) != PARM_DECL
)
8766 error_at (DECL_SOURCE_LOCATION (decl
),
8767 "%qD declared as a non-parameter", decl
);
8770 /* If the declaration is already marked, we have a duplicate
8771 name. Complain and ignore the duplicate. */
8772 else if (seen_args
.contains (decl
))
8774 error_at (DECL_SOURCE_LOCATION (decl
),
8775 "multiple parameters named %qD", decl
);
8776 TREE_PURPOSE (parm
) = 0;
8779 /* If the declaration says "void", complain and turn it into
8781 else if (VOID_TYPE_P (TREE_TYPE (decl
)))
8783 error_at (DECL_SOURCE_LOCATION (decl
),
8784 "parameter %qD declared with void type", decl
);
8785 TREE_TYPE (decl
) = integer_type_node
;
8786 DECL_ARG_TYPE (decl
) = integer_type_node
;
8787 layout_decl (decl
, 0);
8789 warn_if_shadowing (decl
);
8791 /* If no declaration found, default to int. */
8794 /* FIXME diagnostics: This should be the location of the argument,
8795 not the FNDECL. E.g., for an old-style declaration
8797 int f10(v) { blah; }
8799 We should use the location of the V, not the F10.
8800 Unfortunately, the V is an IDENTIFIER_NODE which has no
8801 location. In the future we need locations for c_arg_info
8804 See gcc.dg/Wshadow-3.c for an example of this problem. */
8805 decl
= build_decl (DECL_SOURCE_LOCATION (fndecl
),
8806 PARM_DECL
, TREE_VALUE (parm
), integer_type_node
);
8807 DECL_ARG_TYPE (decl
) = TREE_TYPE (decl
);
8809 warn_if_shadowing (decl
);
8812 pedwarn (DECL_SOURCE_LOCATION (decl
),
8813 OPT_Wimplicit_int
, "type of %qD defaults to %<int%>",
8816 warning_at (DECL_SOURCE_LOCATION (decl
),
8817 OPT_Wmissing_parameter_type
,
8818 "type of %qD defaults to %<int%>", decl
);
8821 TREE_PURPOSE (parm
) = decl
;
8822 seen_args
.add (decl
);
8825 /* Now examine the parms chain for incomplete declarations
8826 and declarations with no corresponding names. */
8828 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
8831 if (TREE_CODE (parm
) != PARM_DECL
)
8834 if (TREE_TYPE (parm
) != error_mark_node
8835 && !COMPLETE_TYPE_P (TREE_TYPE (parm
)))
8837 error_at (DECL_SOURCE_LOCATION (parm
),
8838 "parameter %qD has incomplete type", parm
);
8839 TREE_TYPE (parm
) = error_mark_node
;
8842 if (!seen_args
.contains (parm
))
8844 error_at (DECL_SOURCE_LOCATION (parm
),
8845 "declaration for parameter %qD but no such parameter",
8848 /* Pretend the parameter was not missing.
8849 This gets us to a standard state and minimizes
8850 further error messages. */
8851 parmids
= chainon (parmids
, tree_cons (parm
, 0, 0));
8855 /* Chain the declarations together in the order of the list of
8856 names. Store that chain in the function decl, replacing the
8857 list of names. Update the current scope to match. */
8858 DECL_ARGUMENTS (fndecl
) = 0;
8860 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
8861 if (TREE_PURPOSE (parm
))
8863 if (parm
&& TREE_PURPOSE (parm
))
8865 last
= TREE_PURPOSE (parm
);
8866 DECL_ARGUMENTS (fndecl
) = last
;
8868 for (parm
= TREE_CHAIN (parm
); parm
; parm
= TREE_CHAIN (parm
))
8869 if (TREE_PURPOSE (parm
))
8871 DECL_CHAIN (last
) = TREE_PURPOSE (parm
);
8872 last
= TREE_PURPOSE (parm
);
8874 DECL_CHAIN (last
) = 0;
8877 /* If there was a previous prototype,
8878 set the DECL_ARG_TYPE of each argument according to
8879 the type previously specified, and report any mismatches. */
8881 if (current_function_prototype_arg_types
)
8884 for (parm
= DECL_ARGUMENTS (fndecl
),
8885 type
= current_function_prototype_arg_types
;
8886 parm
|| (type
&& TREE_VALUE (type
) != error_mark_node
8887 && (TYPE_MAIN_VARIANT (TREE_VALUE (type
)) != void_type_node
));
8888 parm
= DECL_CHAIN (parm
), type
= TREE_CHAIN (type
))
8890 if (parm
== 0 || type
== 0
8891 || TYPE_MAIN_VARIANT (TREE_VALUE (type
)) == void_type_node
)
8893 if (current_function_prototype_built_in
)
8894 warning_at (DECL_SOURCE_LOCATION (fndecl
),
8895 0, "number of arguments doesn%'t match "
8896 "built-in prototype");
8899 /* FIXME diagnostics: This should be the location of
8900 FNDECL, but there is bug when a prototype is
8901 declared inside function context, but defined
8902 outside of it (e.g., gcc.dg/pr15698-2.c). In
8903 which case FNDECL gets the location of the
8904 prototype, not the definition. */
8905 error_at (input_location
,
8906 "number of arguments doesn%'t match prototype");
8908 error_at (current_function_prototype_locus
,
8909 "prototype declaration");
8913 /* Type for passing arg must be consistent with that
8914 declared for the arg. ISO C says we take the unqualified
8915 type for parameters declared with qualified type. */
8916 if (TREE_TYPE (parm
) != error_mark_node
8917 && TREE_TYPE (type
) != error_mark_node
8918 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm
))
8919 != TYPE_ATOMIC (TREE_VALUE (type
)))
8920 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm
)),
8921 TYPE_MAIN_VARIANT (TREE_VALUE (type
)))))
8923 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm
))
8924 == TYPE_ATOMIC (TREE_VALUE (type
)))
8925 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm
))
8926 == TYPE_MAIN_VARIANT (TREE_VALUE (type
))))
8928 /* Adjust argument to match prototype. E.g. a previous
8929 `int foo(float);' prototype causes
8930 `int foo(x) float x; {...}' to be treated like
8931 `int foo(float x) {...}'. This is particularly
8932 useful for argument types like uid_t. */
8933 DECL_ARG_TYPE (parm
) = TREE_TYPE (parm
);
8935 if (targetm
.calls
.promote_prototypes (TREE_TYPE (current_function_decl
))
8936 && INTEGRAL_TYPE_P (TREE_TYPE (parm
))
8937 && TYPE_PRECISION (TREE_TYPE (parm
))
8938 < TYPE_PRECISION (integer_type_node
))
8939 DECL_ARG_TYPE (parm
)
8940 = c_type_promotes_to (TREE_TYPE (parm
));
8942 /* ??? Is it possible to get here with a
8943 built-in prototype or will it always have
8944 been diagnosed as conflicting with an
8945 old-style definition and discarded? */
8946 if (current_function_prototype_built_in
)
8947 warning_at (DECL_SOURCE_LOCATION (parm
),
8948 OPT_Wpedantic
, "promoted argument %qD "
8949 "doesn%'t match built-in prototype", parm
);
8952 pedwarn (DECL_SOURCE_LOCATION (parm
),
8953 OPT_Wpedantic
, "promoted argument %qD "
8954 "doesn%'t match prototype", parm
);
8955 pedwarn (current_function_prototype_locus
, OPT_Wpedantic
,
8956 "prototype declaration");
8961 if (current_function_prototype_built_in
)
8962 warning_at (DECL_SOURCE_LOCATION (parm
),
8963 0, "argument %qD doesn%'t match "
8964 "built-in prototype", parm
);
8967 error_at (DECL_SOURCE_LOCATION (parm
),
8968 "argument %qD doesn%'t match prototype", parm
);
8969 error_at (current_function_prototype_locus
,
8970 "prototype declaration");
8975 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = 0;
8978 /* Otherwise, create a prototype that would match. */
8982 tree actual
= 0, last
= 0, type
;
8984 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= DECL_CHAIN (parm
))
8986 type
= tree_cons (NULL_TREE
, DECL_ARG_TYPE (parm
), NULL_TREE
);
8988 TREE_CHAIN (last
) = type
;
8993 type
= tree_cons (NULL_TREE
, void_type_node
, NULL_TREE
);
8995 TREE_CHAIN (last
) = type
;
8999 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9000 of the type of this function, but we need to avoid having this
9001 affect the types of other similarly-typed functions, so we must
9002 first force the generation of an identical (but separate) type
9003 node for the relevant function type. The new node we create
9004 will be a variant of the main variant of the original function
9007 TREE_TYPE (fndecl
) = build_variant_type_copy (TREE_TYPE (fndecl
));
9009 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = actual
;
9013 /* Store parameter declarations passed in ARG_INFO into the current
9014 function declaration. */
9017 store_parm_decls_from (struct c_arg_info
*arg_info
)
9019 current_function_arg_info
= arg_info
;
9020 store_parm_decls ();
9023 /* Called by walk_tree to look for and update context-less labels. */
9026 set_labels_context_r (tree
*tp
, int *walk_subtrees
, void *data
)
9028 if (TREE_CODE (*tp
) == LABEL_EXPR
9029 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp
)) == NULL_TREE
)
9031 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp
)) = static_cast<tree
>(data
);
9038 /* Store the parameter declarations into the current function declaration.
9039 This is called after parsing the parameter declarations, before
9040 digesting the body of the function.
9042 For an old-style definition, construct a prototype out of the old-style
9043 parameter declarations and inject it into the function's type. */
9046 store_parm_decls (void)
9048 tree fndecl
= current_function_decl
;
9051 /* The argument information block for FNDECL. */
9052 struct c_arg_info
*arg_info
= current_function_arg_info
;
9053 current_function_arg_info
= 0;
9055 /* True if this definition is written with a prototype. Note:
9056 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9057 list in a function definition as equivalent to (void) -- an
9058 empty argument list specifies the function has no parameters,
9059 but only (void) sets up a prototype for future calls. */
9060 proto
= arg_info
->types
!= 0;
9063 store_parm_decls_newstyle (fndecl
, arg_info
);
9065 store_parm_decls_oldstyle (fndecl
, arg_info
);
9067 /* The next call to push_scope will be a function body. */
9069 next_is_function_body
= true;
9071 /* Write a record describing this function definition to the prototypes
9072 file (if requested). */
9074 gen_aux_info_record (fndecl
, 1, 0, proto
);
9076 /* Initialize the RTL code for the function. */
9077 allocate_struct_function (fndecl
, false);
9079 if (warn_unused_local_typedefs
)
9080 cfun
->language
= ggc_cleared_alloc
<language_function
> ();
9082 /* Begin the statement tree for this function. */
9083 DECL_SAVED_TREE (fndecl
) = push_stmt_list ();
9085 /* ??? Insert the contents of the pending sizes list into the function
9086 to be evaluated. The only reason left to have this is
9087 void foo(int n, int array[n++])
9088 because we throw away the array type in favor of a pointer type, and
9089 thus won't naturally see the SAVE_EXPR containing the increment. All
9090 other pending sizes would be handled by gimplify_parameters. */
9091 if (arg_info
->pending_sizes
)
9093 /* In very special circumstances, e.g. for code like
9095 void f (int a[i += 2]) {}
9096 we need to execute the atomic assignment on function entry.
9097 But in this case, it is not just a straight store, it has the
9098 op= form, which means that build_atomic_assign has generated
9099 gotos, labels, etc. Because at that time the function decl
9100 for F has not been created yet, those labels do not have any
9101 function context. But we have the fndecl now, so update the
9102 labels accordingly. gimplify_expr would crash otherwise. */
9103 walk_tree_without_duplicates (&arg_info
->pending_sizes
,
9104 set_labels_context_r
, fndecl
);
9105 add_stmt (arg_info
->pending_sizes
);
9109 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9110 c_finish_omp_declare_simd for function prototypes. No diagnostics
9114 temp_store_parm_decls (tree fndecl
, tree parms
)
9117 for (tree p
= parms
; p
; p
= DECL_CHAIN (p
))
9119 DECL_CONTEXT (p
) = fndecl
;
9121 bind (DECL_NAME (p
), p
, current_scope
,
9122 /*invisible=*/false, /*nested=*/false,
9127 /* Undo what temp_store_parm_decls did. */
9130 temp_pop_parm_decls (void)
9132 /* Clear all bindings in this temporary scope, so that
9133 pop_scope doesn't create a BLOCK. */
9134 struct c_binding
*b
= current_scope
->bindings
;
9135 current_scope
->bindings
= NULL
;
9136 for (; b
; b
= free_binding_and_advance (b
))
9138 gcc_assert (TREE_CODE (b
->decl
) == PARM_DECL
9139 || b
->decl
== error_mark_node
);
9140 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
9141 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
9142 if (b
->shadowed
&& b
->shadowed
->u
.type
)
9143 TREE_TYPE (b
->shadowed
->decl
) = b
->shadowed
->u
.type
;
9149 /* Finish up a function declaration and compile that function
9150 all the way to assembler language output. Then free the storage
9151 for the function definition.
9153 This is called after parsing the body of the function definition. */
9156 finish_function (void)
9158 tree fndecl
= current_function_decl
;
9160 if (c_dialect_objc ())
9161 objc_finish_function ();
9163 if (TREE_CODE (fndecl
) == FUNCTION_DECL
9164 && targetm
.calls
.promote_prototypes (TREE_TYPE (fndecl
)))
9166 tree args
= DECL_ARGUMENTS (fndecl
);
9167 for (; args
; args
= DECL_CHAIN (args
))
9169 tree type
= TREE_TYPE (args
);
9170 if (INTEGRAL_TYPE_P (type
)
9171 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
9172 DECL_ARG_TYPE (args
) = c_type_promotes_to (type
);
9176 if (DECL_INITIAL (fndecl
) && DECL_INITIAL (fndecl
) != error_mark_node
)
9177 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
9179 /* Must mark the RESULT_DECL as being in this function. */
9181 if (DECL_RESULT (fndecl
) && DECL_RESULT (fndecl
) != error_mark_node
)
9182 DECL_CONTEXT (DECL_RESULT (fndecl
)) = fndecl
;
9184 if (MAIN_NAME_P (DECL_NAME (fndecl
)) && flag_hosted
9185 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl
)))
9186 == integer_type_node
&& flag_isoc99
)
9188 /* Hack. We don't want the middle-end to warn that this return
9189 is unreachable, so we mark its location as special. Using
9190 UNKNOWN_LOCATION has the problem that it gets clobbered in
9191 annotate_one_with_locus. A cleaner solution might be to
9192 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9194 c_finish_return (BUILTINS_LOCATION
, integer_zero_node
, NULL_TREE
);
9197 /* Tie off the statement tree for this function. */
9198 DECL_SAVED_TREE (fndecl
) = pop_stmt_list (DECL_SAVED_TREE (fndecl
));
9200 /* If the function has _Cilk_spawn in front of a function call inside it
9201 i.e. it is a spawning function, then add the appropriate Cilk plus
9202 functions inside. */
9203 if (fn_contains_cilk_spawn_p (cfun
))
9204 cfun
->cilk_frame_decl
= insert_cilk_frame (fndecl
);
9206 finish_fname_decls ();
9208 /* Complain if there's just no return statement. */
9209 if (warn_return_type
9210 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl
))) != VOID_TYPE
9211 && !current_function_returns_value
&& !current_function_returns_null
9212 /* Don't complain if we are no-return. */
9213 && !current_function_returns_abnormally
9214 /* Don't complain if we are declared noreturn. */
9215 && !TREE_THIS_VOLATILE (fndecl
)
9216 /* Don't warn for main(). */
9217 && !MAIN_NAME_P (DECL_NAME (fndecl
))
9218 /* Or if they didn't actually specify a return type. */
9219 && !C_FUNCTION_IMPLICIT_INT (fndecl
)
9220 /* Normally, with -Wreturn-type, flow will complain, but we might
9221 optimize out static functions. */
9222 && !TREE_PUBLIC (fndecl
))
9224 warning (OPT_Wreturn_type
,
9225 "no return statement in function returning non-void");
9226 TREE_NO_WARNING (fndecl
) = 1;
9229 /* Complain about parameters that are only set, but never otherwise used. */
9230 if (warn_unused_but_set_parameter
)
9234 for (decl
= DECL_ARGUMENTS (fndecl
);
9236 decl
= DECL_CHAIN (decl
))
9237 if (TREE_USED (decl
)
9238 && TREE_CODE (decl
) == PARM_DECL
9239 && !DECL_READ_P (decl
)
9241 && !DECL_ARTIFICIAL (decl
)
9242 && !TREE_NO_WARNING (decl
))
9243 warning_at (DECL_SOURCE_LOCATION (decl
),
9244 OPT_Wunused_but_set_parameter
,
9245 "parameter %qD set but not used", decl
);
9248 /* Complain about locally defined typedefs that are not used in this
9250 maybe_warn_unused_local_typedefs ();
9252 /* Possibly warn about unused parameters. */
9253 if (warn_unused_parameter
)
9254 do_warn_unused_parameter (fndecl
);
9256 /* Store the end of the function, so that we get good line number
9257 info for the epilogue. */
9258 cfun
->function_end_locus
= input_location
;
9260 /* Finalize the ELF visibility for the function. */
9261 c_determine_visibility (fndecl
);
9263 /* For GNU C extern inline functions disregard inline limits. */
9264 if (DECL_EXTERNAL (fndecl
)
9265 && DECL_DECLARED_INLINE_P (fndecl
))
9266 DECL_DISREGARD_INLINE_LIMITS (fndecl
) = 1;
9268 /* Genericize before inlining. Delay genericizing nested functions
9269 until their parent function is genericized. Since finalizing
9270 requires GENERIC, delay that as well. */
9272 if (DECL_INITIAL (fndecl
) && DECL_INITIAL (fndecl
) != error_mark_node
9273 && !undef_nested_function
)
9275 if (!decl_function_context (fndecl
))
9277 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE
, fndecl
);
9278 c_genericize (fndecl
);
9280 /* ??? Objc emits functions after finalizing the compilation unit.
9281 This should be cleaned up later and this conditional removed. */
9282 if (symtab
->global_info_ready
)
9284 cgraph_node::add_new_function (fndecl
, false);
9287 cgraph_node::finalize_function (fndecl
, false);
9291 /* Register this function with cgraph just far enough to get it
9292 added to our parent's nested function list. Handy, since the
9293 C front end doesn't have such a list. */
9294 (void) cgraph_node::get_create (fndecl
);
9298 if (!decl_function_context (fndecl
))
9299 undef_nested_function
= false;
9301 if (cfun
->language
!= NULL
)
9303 ggc_free (cfun
->language
);
9304 cfun
->language
= NULL
;
9307 /* We're leaving the context of this function, so zap cfun.
9308 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9309 tree_rest_of_compilation. */
9311 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION
, current_function_decl
);
9312 current_function_decl
= NULL
;
9315 /* Check the declarations given in a for-loop for satisfying the C99
9316 constraints. If exactly one such decl is found, return it. LOC is
9317 the location of the opening parenthesis of the for loop. The last
9318 parameter allows you to control the "for loop initial declarations
9319 are only allowed in C99 mode". Normally, you should pass
9320 flag_isoc99 as that parameter. But in some cases (Objective-C
9321 foreach loop, for example) we want to run the checks in this
9322 function even if not in C99 mode, so we allow the caller to turn
9323 off the error about not being in C99 mode.
9327 check_for_loop_decls (location_t loc
, bool turn_off_iso_c99_error
)
9329 struct c_binding
*b
;
9330 tree one_decl
= NULL_TREE
;
9333 if (!turn_off_iso_c99_error
)
9335 static bool hint
= true;
9336 /* If we get here, declarations have been used in a for loop without
9337 the C99 for loop scope. This doesn't make much sense, so don't
9339 error_at (loc
, "%<for%> loop initial declarations "
9340 "are only allowed in C99 or C11 mode");
9344 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9345 "to compile your code");
9350 /* C99 subclause 6.8.5 paragraph 3:
9352 [#3] The declaration part of a for statement shall only
9353 declare identifiers for objects having storage class auto or
9356 It isn't clear whether, in this sentence, "identifiers" binds to
9357 "shall only declare" or to "objects" - that is, whether all identifiers
9358 declared must be identifiers for objects, or whether the restriction
9359 only applies to those that are. (A question on this in comp.std.c
9360 in November 2000 received no answer.) We implement the strictest
9361 interpretation, to avoid creating an extension which later causes
9364 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
9367 tree decl
= b
->decl
;
9372 switch (TREE_CODE (decl
))
9376 location_t decl_loc
= DECL_SOURCE_LOCATION (decl
);
9377 if (TREE_STATIC (decl
))
9379 "declaration of static variable %qD in %<for%> loop "
9380 "initial declaration", decl
);
9381 else if (DECL_EXTERNAL (decl
))
9383 "declaration of %<extern%> variable %qD in %<for%> loop "
9384 "initial declaration", decl
);
9390 "%<struct %E%> declared in %<for%> loop initial "
9395 "%<union %E%> declared in %<for%> loop initial declaration",
9399 error_at (loc
, "%<enum %E%> declared in %<for%> loop "
9400 "initial declaration", id
);
9403 error_at (loc
, "declaration of non-variable "
9404 "%qD in %<for%> loop initial declaration", decl
);
9411 return n_decls
== 1 ? one_decl
: NULL_TREE
;
9414 /* Save and reinitialize the variables
9415 used during compilation of a C function. */
9418 c_push_function_context (void)
9420 struct language_function
*p
= cfun
->language
;
9421 /* cfun->language might have been already allocated by the use of
9422 -Wunused-local-typedefs. In that case, just re-use it. */
9424 cfun
->language
= p
= ggc_cleared_alloc
<language_function
> ();
9426 p
->base
.x_stmt_tree
= c_stmt_tree
;
9427 c_stmt_tree
.x_cur_stmt_list
= vec_safe_copy (c_stmt_tree
.x_cur_stmt_list
);
9428 p
->x_break_label
= c_break_label
;
9429 p
->x_cont_label
= c_cont_label
;
9430 p
->x_switch_stack
= c_switch_stack
;
9431 p
->arg_info
= current_function_arg_info
;
9432 p
->returns_value
= current_function_returns_value
;
9433 p
->returns_null
= current_function_returns_null
;
9434 p
->returns_abnormally
= current_function_returns_abnormally
;
9435 p
->warn_about_return_type
= warn_about_return_type
;
9437 push_function_context ();
9440 /* Restore the variables used during compilation of a C function. */
9443 c_pop_function_context (void)
9445 struct language_function
*p
;
9447 pop_function_context ();
9450 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9451 used to store data throughout the life time of the current cfun,
9452 So don't deallocate it. */
9453 if (!warn_unused_local_typedefs
)
9454 cfun
->language
= NULL
;
9456 if (DECL_STRUCT_FUNCTION (current_function_decl
) == 0
9457 && DECL_SAVED_TREE (current_function_decl
) == NULL_TREE
)
9459 /* Stop pointing to the local nodes about to be freed. */
9460 /* But DECL_INITIAL must remain nonzero so we know this
9461 was an actual function definition. */
9462 DECL_INITIAL (current_function_decl
) = error_mark_node
;
9463 DECL_ARGUMENTS (current_function_decl
) = 0;
9466 c_stmt_tree
= p
->base
.x_stmt_tree
;
9467 p
->base
.x_stmt_tree
.x_cur_stmt_list
= NULL
;
9468 c_break_label
= p
->x_break_label
;
9469 c_cont_label
= p
->x_cont_label
;
9470 c_switch_stack
= p
->x_switch_stack
;
9471 current_function_arg_info
= p
->arg_info
;
9472 current_function_returns_value
= p
->returns_value
;
9473 current_function_returns_null
= p
->returns_null
;
9474 current_function_returns_abnormally
= p
->returns_abnormally
;
9475 warn_about_return_type
= p
->warn_about_return_type
;
9478 /* The functions below are required for functionality of doing
9479 function at once processing in the C front end. Currently these
9480 functions are not called from anywhere in the C front end, but as
9481 these changes continue, that will change. */
9483 /* Returns the stmt_tree (if any) to which statements are currently
9484 being added. If there is no active statement-tree, NULL is
9488 current_stmt_tree (void)
9490 return &c_stmt_tree
;
9493 /* Return the global value of T as a symbol. */
9496 identifier_global_value (tree t
)
9498 struct c_binding
*b
;
9500 for (b
= I_SYMBOL_BINDING (t
); b
; b
= b
->shadowed
)
9501 if (B_IN_FILE_SCOPE (b
) || B_IN_EXTERNAL_SCOPE (b
))
9507 /* In C, the only C-linkage public declaration is at file scope. */
9510 c_linkage_bindings (tree name
)
9512 return identifier_global_value (name
);
9515 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9516 otherwise the name is found in ridpointers from RID_INDEX. */
9519 record_builtin_type (enum rid rid_index
, const char *name
, tree type
)
9523 id
= ridpointers
[(int) rid_index
];
9525 id
= get_identifier (name
);
9526 decl
= build_decl (UNKNOWN_LOCATION
, TYPE_DECL
, id
, type
);
9528 if (debug_hooks
->type_decl
)
9529 debug_hooks
->type_decl (decl
, false);
9532 /* Build the void_list_node (void_type_node having been created). */
9534 build_void_list_node (void)
9536 tree t
= build_tree_list (NULL_TREE
, void_type_node
);
9540 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9543 build_c_parm (struct c_declspecs
*specs
, tree attrs
,
9544 struct c_declarator
*declarator
)
9546 struct c_parm
*ret
= XOBNEW (&parser_obstack
, struct c_parm
);
9549 ret
->declarator
= declarator
;
9553 /* Return a declarator with nested attributes. TARGET is the inner
9554 declarator to which these attributes apply. ATTRS are the
9557 struct c_declarator
*
9558 build_attrs_declarator (tree attrs
, struct c_declarator
*target
)
9560 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
9561 ret
->kind
= cdk_attrs
;
9562 ret
->declarator
= target
;
9563 ret
->u
.attrs
= attrs
;
9567 /* Return a declarator for a function with arguments specified by ARGS
9568 and return type specified by TARGET. */
9570 struct c_declarator
*
9571 build_function_declarator (struct c_arg_info
*args
,
9572 struct c_declarator
*target
)
9574 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
9575 ret
->kind
= cdk_function
;
9576 ret
->declarator
= target
;
9577 ret
->u
.arg_info
= args
;
9581 /* Return a declarator for the identifier IDENT (which may be
9582 NULL_TREE for an abstract declarator). */
9584 struct c_declarator
*
9585 build_id_declarator (tree ident
)
9587 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
9589 ret
->declarator
= 0;
9591 /* Default value - may get reset to a more precise location. */
9592 ret
->id_loc
= input_location
;
9596 /* Return something to represent absolute declarators containing a *.
9597 TARGET is the absolute declarator that the * contains.
9598 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9599 to apply to the pointer type. */
9601 struct c_declarator
*
9602 make_pointer_declarator (struct c_declspecs
*type_quals_attrs
,
9603 struct c_declarator
*target
)
9607 struct c_declarator
*itarget
= target
;
9608 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
9609 if (type_quals_attrs
)
9611 attrs
= type_quals_attrs
->attrs
;
9612 quals
= quals_from_declspecs (type_quals_attrs
);
9613 if (attrs
!= NULL_TREE
)
9614 itarget
= build_attrs_declarator (attrs
, target
);
9616 ret
->kind
= cdk_pointer
;
9617 ret
->declarator
= itarget
;
9618 ret
->u
.pointer_quals
= quals
;
9622 /* Return a pointer to a structure for an empty list of declaration
9625 struct c_declspecs
*
9626 build_null_declspecs (void)
9628 struct c_declspecs
*ret
= XOBNEW (&parser_obstack
, struct c_declspecs
);
9629 memset (ret
, 0, sizeof *ret
);
9630 ret
->align_log
= -1;
9631 ret
->typespec_word
= cts_none
;
9632 ret
->storage_class
= csc_none
;
9633 ret
->expr_const_operands
= true;
9634 ret
->typespec_kind
= ctsk_none
;
9635 ret
->address_space
= ADDR_SPACE_GENERIC
;
9639 /* Add the address space ADDRSPACE to the declaration specifiers
9640 SPECS, returning SPECS. */
9642 struct c_declspecs
*
9643 declspecs_add_addrspace (source_location location
,
9644 struct c_declspecs
*specs
, addr_space_t as
)
9646 specs
->non_sc_seen_p
= true;
9647 specs
->declspecs_seen_p
= true;
9649 if (!ADDR_SPACE_GENERIC_P (specs
->address_space
)
9650 && specs
->address_space
!= as
)
9651 error ("incompatible address space qualifiers %qs and %qs",
9652 c_addr_space_name (as
),
9653 c_addr_space_name (specs
->address_space
));
9656 specs
->address_space
= as
;
9657 specs
->locations
[cdw_address_space
] = location
;
9662 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9665 struct c_declspecs
*
9666 declspecs_add_qual (source_location loc
,
9667 struct c_declspecs
*specs
, tree qual
)
9671 specs
->non_sc_seen_p
= true;
9672 specs
->declspecs_seen_p
= true;
9673 gcc_assert (TREE_CODE (qual
) == IDENTIFIER_NODE
9674 && C_IS_RESERVED_WORD (qual
));
9675 i
= C_RID_CODE (qual
);
9676 location_t prev_loc
= 0;
9680 dupe
= specs
->const_p
;
9681 specs
->const_p
= true;
9682 prev_loc
= specs
->locations
[cdw_const
];
9683 specs
->locations
[cdw_const
] = loc
;
9686 dupe
= specs
->volatile_p
;
9687 specs
->volatile_p
= true;
9688 prev_loc
= specs
->locations
[cdw_volatile
];
9689 specs
->locations
[cdw_volatile
] = loc
;
9692 dupe
= specs
->restrict_p
;
9693 specs
->restrict_p
= true;
9694 prev_loc
= specs
->locations
[cdw_restrict
];
9695 specs
->locations
[cdw_restrict
] = loc
;
9698 dupe
= specs
->atomic_p
;
9699 specs
->atomic_p
= true;
9700 prev_loc
= specs
->locations
[cdw_atomic
];
9701 specs
->locations
[cdw_atomic
] = loc
;
9708 bool warned
= pedwarn_c90 (loc
, OPT_Wpedantic
,
9709 "duplicate %qE declaration specifier", qual
);
9711 && warn_duplicate_decl_specifier
9712 && prev_loc
>= RESERVED_LOCATION_COUNT
9713 && !from_macro_expansion_at (prev_loc
)
9714 && !from_macro_expansion_at (loc
))
9715 warning_at (loc
, OPT_Wduplicate_decl_specifier
,
9716 "duplicate %qE declaration specifier", qual
);
9721 /* Add the type specifier TYPE to the declaration specifiers SPECS,
9724 struct c_declspecs
*
9725 declspecs_add_type (location_t loc
, struct c_declspecs
*specs
,
9726 struct c_typespec spec
)
9728 tree type
= spec
.spec
;
9729 specs
->non_sc_seen_p
= true;
9730 specs
->declspecs_seen_p
= true;
9731 specs
->typespec_kind
= spec
.kind
;
9732 if (TREE_DEPRECATED (type
))
9733 specs
->deprecated_p
= true;
9735 /* Handle type specifier keywords. */
9736 if (TREE_CODE (type
) == IDENTIFIER_NODE
9737 && C_IS_RESERVED_WORD (type
)
9738 && C_RID_CODE (type
) != RID_CXX_COMPAT_WARN
)
9740 enum rid i
= C_RID_CODE (type
);
9743 error_at (loc
, "two or more data types in declaration specifiers");
9746 if ((int) i
<= (int) RID_LAST_MODIFIER
)
9748 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
9753 if (specs
->long_long_p
)
9755 error_at (loc
, "%<long long long%> is too long for GCC");
9760 if (specs
->typespec_word
== cts_double
)
9763 ("both %<long long%> and %<double%> in "
9764 "declaration specifiers"));
9767 pedwarn_c90 (loc
, OPT_Wlong_long
,
9768 "ISO C90 does not support %<long long%>");
9769 specs
->long_long_p
= 1;
9770 specs
->locations
[cdw_long_long
] = loc
;
9775 ("both %<long%> and %<short%> in "
9776 "declaration specifiers"));
9777 else if (specs
->typespec_word
== cts_auto_type
)
9779 ("both %<long%> and %<__auto_type%> in "
9780 "declaration specifiers"));
9781 else if (specs
->typespec_word
== cts_void
)
9783 ("both %<long%> and %<void%> in "
9784 "declaration specifiers"));
9785 else if (specs
->typespec_word
== cts_int_n
)
9787 ("both %<long%> and %<__int%d%> in "
9788 "declaration specifiers"),
9789 int_n_data
[specs
->int_n_idx
].bitsize
);
9790 else if (specs
->typespec_word
== cts_bool
)
9792 ("both %<long%> and %<_Bool%> in "
9793 "declaration specifiers"));
9794 else if (specs
->typespec_word
== cts_char
)
9796 ("both %<long%> and %<char%> in "
9797 "declaration specifiers"));
9798 else if (specs
->typespec_word
== cts_float
)
9800 ("both %<long%> and %<float%> in "
9801 "declaration specifiers"));
9802 else if (specs
->typespec_word
== cts_dfloat32
)
9804 ("both %<long%> and %<_Decimal32%> in "
9805 "declaration specifiers"));
9806 else if (specs
->typespec_word
== cts_dfloat64
)
9808 ("both %<long%> and %<_Decimal64%> in "
9809 "declaration specifiers"));
9810 else if (specs
->typespec_word
== cts_dfloat128
)
9812 ("both %<long%> and %<_Decimal128%> in "
9813 "declaration specifiers"));
9816 specs
->long_p
= true;
9817 specs
->locations
[cdw_long
] = loc
;
9821 dupe
= specs
->short_p
;
9824 ("both %<long%> and %<short%> in "
9825 "declaration specifiers"));
9826 else if (specs
->typespec_word
== cts_auto_type
)
9828 ("both %<short%> and %<__auto_type%> in "
9829 "declaration specifiers"));
9830 else if (specs
->typespec_word
== cts_void
)
9832 ("both %<short%> and %<void%> in "
9833 "declaration specifiers"));
9834 else if (specs
->typespec_word
== cts_int_n
)
9836 ("both %<short%> and %<__int%d%> in "
9837 "declaration specifiers"),
9838 int_n_data
[specs
->int_n_idx
].bitsize
);
9839 else if (specs
->typespec_word
== cts_bool
)
9841 ("both %<short%> and %<_Bool%> in "
9842 "declaration specifiers"));
9843 else if (specs
->typespec_word
== cts_char
)
9845 ("both %<short%> and %<char%> in "
9846 "declaration specifiers"));
9847 else if (specs
->typespec_word
== cts_float
)
9849 ("both %<short%> and %<float%> in "
9850 "declaration specifiers"));
9851 else if (specs
->typespec_word
== cts_double
)
9853 ("both %<short%> and %<double%> in "
9854 "declaration specifiers"));
9855 else if (specs
->typespec_word
== cts_dfloat32
)
9857 ("both %<short%> and %<_Decimal32%> in "
9858 "declaration specifiers"));
9859 else if (specs
->typespec_word
== cts_dfloat64
)
9861 ("both %<short%> and %<_Decimal64%> in "
9862 "declaration specifiers"));
9863 else if (specs
->typespec_word
== cts_dfloat128
)
9865 ("both %<short%> and %<_Decimal128%> in "
9866 "declaration specifiers"));
9869 specs
->short_p
= true;
9870 specs
->locations
[cdw_short
] = loc
;
9874 dupe
= specs
->signed_p
;
9875 if (specs
->unsigned_p
)
9877 ("both %<signed%> and %<unsigned%> in "
9878 "declaration specifiers"));
9879 else if (specs
->typespec_word
== cts_auto_type
)
9881 ("both %<signed%> and %<__auto_type%> in "
9882 "declaration specifiers"));
9883 else if (specs
->typespec_word
== cts_void
)
9885 ("both %<signed%> and %<void%> in "
9886 "declaration specifiers"));
9887 else if (specs
->typespec_word
== cts_bool
)
9889 ("both %<signed%> and %<_Bool%> in "
9890 "declaration specifiers"));
9891 else if (specs
->typespec_word
== cts_float
)
9893 ("both %<signed%> and %<float%> in "
9894 "declaration specifiers"));
9895 else if (specs
->typespec_word
== cts_double
)
9897 ("both %<signed%> and %<double%> in "
9898 "declaration specifiers"));
9899 else if (specs
->typespec_word
== cts_dfloat32
)
9901 ("both %<signed%> and %<_Decimal32%> in "
9902 "declaration specifiers"));
9903 else if (specs
->typespec_word
== cts_dfloat64
)
9905 ("both %<signed%> and %<_Decimal64%> in "
9906 "declaration specifiers"));
9907 else if (specs
->typespec_word
== cts_dfloat128
)
9909 ("both %<signed%> and %<_Decimal128%> in "
9910 "declaration specifiers"));
9913 specs
->signed_p
= true;
9914 specs
->locations
[cdw_signed
] = loc
;
9918 dupe
= specs
->unsigned_p
;
9919 if (specs
->signed_p
)
9921 ("both %<signed%> and %<unsigned%> in "
9922 "declaration specifiers"));
9923 else if (specs
->typespec_word
== cts_auto_type
)
9925 ("both %<unsigned%> and %<__auto_type%> in "
9926 "declaration specifiers"));
9927 else if (specs
->typespec_word
== cts_void
)
9929 ("both %<unsigned%> and %<void%> in "
9930 "declaration specifiers"));
9931 else if (specs
->typespec_word
== cts_bool
)
9933 ("both %<unsigned%> and %<_Bool%> in "
9934 "declaration specifiers"));
9935 else if (specs
->typespec_word
== cts_float
)
9937 ("both %<unsigned%> and %<float%> in "
9938 "declaration specifiers"));
9939 else if (specs
->typespec_word
== cts_double
)
9941 ("both %<unsigned%> and %<double%> in "
9942 "declaration specifiers"));
9943 else if (specs
->typespec_word
== cts_dfloat32
)
9945 ("both %<unsigned%> and %<_Decimal32%> in "
9946 "declaration specifiers"));
9947 else if (specs
->typespec_word
== cts_dfloat64
)
9949 ("both %<unsigned%> and %<_Decimal64%> in "
9950 "declaration specifiers"));
9951 else if (specs
->typespec_word
== cts_dfloat128
)
9953 ("both %<unsigned%> and %<_Decimal128%> in "
9954 "declaration specifiers"));
9957 specs
->unsigned_p
= true;
9958 specs
->locations
[cdw_unsigned
] = loc
;
9962 dupe
= specs
->complex_p
;
9963 if (!in_system_header_at (loc
))
9964 pedwarn_c90 (loc
, OPT_Wpedantic
,
9965 "ISO C90 does not support complex types");
9966 if (specs
->typespec_word
== cts_auto_type
)
9968 ("both %<complex%> and %<__auto_type%> in "
9969 "declaration specifiers"));
9970 else if (specs
->typespec_word
== cts_void
)
9972 ("both %<complex%> and %<void%> in "
9973 "declaration specifiers"));
9974 else if (specs
->typespec_word
== cts_bool
)
9976 ("both %<complex%> and %<_Bool%> in "
9977 "declaration specifiers"));
9978 else if (specs
->typespec_word
== cts_dfloat32
)
9980 ("both %<complex%> and %<_Decimal32%> in "
9981 "declaration specifiers"));
9982 else if (specs
->typespec_word
== cts_dfloat64
)
9984 ("both %<complex%> and %<_Decimal64%> in "
9985 "declaration specifiers"));
9986 else if (specs
->typespec_word
== cts_dfloat128
)
9988 ("both %<complex%> and %<_Decimal128%> in "
9989 "declaration specifiers"));
9990 else if (specs
->typespec_word
== cts_fract
)
9992 ("both %<complex%> and %<_Fract%> in "
9993 "declaration specifiers"));
9994 else if (specs
->typespec_word
== cts_accum
)
9996 ("both %<complex%> and %<_Accum%> in "
9997 "declaration specifiers"));
9998 else if (specs
->saturating_p
)
10000 ("both %<complex%> and %<_Sat%> in "
10001 "declaration specifiers"));
10004 specs
->complex_p
= true;
10005 specs
->locations
[cdw_complex
] = loc
;
10009 dupe
= specs
->saturating_p
;
10010 pedwarn (loc
, OPT_Wpedantic
,
10011 "ISO C does not support saturating types");
10012 if (specs
->typespec_word
== cts_int_n
)
10015 ("both %<_Sat%> and %<__int%d%> in "
10016 "declaration specifiers"),
10017 int_n_data
[specs
->int_n_idx
].bitsize
);
10019 else if (specs
->typespec_word
== cts_auto_type
)
10021 ("both %<_Sat%> and %<__auto_type%> in "
10022 "declaration specifiers"));
10023 else if (specs
->typespec_word
== cts_void
)
10025 ("both %<_Sat%> and %<void%> in "
10026 "declaration specifiers"));
10027 else if (specs
->typespec_word
== cts_bool
)
10029 ("both %<_Sat%> and %<_Bool%> in "
10030 "declaration specifiers"));
10031 else if (specs
->typespec_word
== cts_char
)
10033 ("both %<_Sat%> and %<char%> in "
10034 "declaration specifiers"));
10035 else if (specs
->typespec_word
== cts_int
)
10037 ("both %<_Sat%> and %<int%> in "
10038 "declaration specifiers"));
10039 else if (specs
->typespec_word
== cts_float
)
10041 ("both %<_Sat%> and %<float%> in "
10042 "declaration specifiers"));
10043 else if (specs
->typespec_word
== cts_double
)
10045 ("both %<_Sat%> and %<double%> in "
10046 "declaration specifiers"));
10047 else if (specs
->typespec_word
== cts_dfloat32
)
10049 ("both %<_Sat%> and %<_Decimal32%> in "
10050 "declaration specifiers"));
10051 else if (specs
->typespec_word
== cts_dfloat64
)
10053 ("both %<_Sat%> and %<_Decimal64%> in "
10054 "declaration specifiers"));
10055 else if (specs
->typespec_word
== cts_dfloat128
)
10057 ("both %<_Sat%> and %<_Decimal128%> in "
10058 "declaration specifiers"));
10059 else if (specs
->complex_p
)
10061 ("both %<_Sat%> and %<complex%> in "
10062 "declaration specifiers"));
10065 specs
->saturating_p
= true;
10066 specs
->locations
[cdw_saturating
] = loc
;
10070 gcc_unreachable ();
10074 error_at (loc
, "duplicate %qE", type
);
10080 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
10081 "__intN", "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10083 if (specs
->typespec_word
!= cts_none
)
10086 "two or more data types in declaration specifiers");
10091 case RID_AUTO_TYPE
:
10094 ("both %<long%> and %<__auto_type%> in "
10095 "declaration specifiers"));
10096 else if (specs
->short_p
)
10098 ("both %<short%> and %<__auto_type%> in "
10099 "declaration specifiers"));
10100 else if (specs
->signed_p
)
10102 ("both %<signed%> and %<__auto_type%> in "
10103 "declaration specifiers"));
10104 else if (specs
->unsigned_p
)
10106 ("both %<unsigned%> and %<__auto_type%> in "
10107 "declaration specifiers"));
10108 else if (specs
->complex_p
)
10110 ("both %<complex%> and %<__auto_type%> in "
10111 "declaration specifiers"));
10112 else if (specs
->saturating_p
)
10114 ("both %<_Sat%> and %<__auto_type%> in "
10115 "declaration specifiers"));
10118 specs
->typespec_word
= cts_auto_type
;
10119 specs
->locations
[cdw_typespec
] = loc
;
10126 specs
->int_n_idx
= i
- RID_INT_N_0
;
10127 if (!in_system_header_at (input_location
))
10128 pedwarn (loc
, OPT_Wpedantic
,
10129 "ISO C does not support %<__int%d%> types",
10130 int_n_data
[specs
->int_n_idx
].bitsize
);
10134 ("both %<__int%d%> and %<long%> in "
10135 "declaration specifiers"),
10136 int_n_data
[specs
->int_n_idx
].bitsize
);
10137 else if (specs
->saturating_p
)
10139 ("both %<_Sat%> and %<__int%d%> in "
10140 "declaration specifiers"),
10141 int_n_data
[specs
->int_n_idx
].bitsize
);
10142 else if (specs
->short_p
)
10144 ("both %<__int%d%> and %<short%> in "
10145 "declaration specifiers"),
10146 int_n_data
[specs
->int_n_idx
].bitsize
);
10147 else if (! int_n_enabled_p
[specs
->int_n_idx
])
10149 "%<__int%d%> is not supported on this target",
10150 int_n_data
[specs
->int_n_idx
].bitsize
);
10153 specs
->typespec_word
= cts_int_n
;
10154 specs
->locations
[cdw_typespec
] = loc
;
10160 ("both %<long%> and %<void%> in "
10161 "declaration specifiers"));
10162 else if (specs
->short_p
)
10164 ("both %<short%> and %<void%> in "
10165 "declaration specifiers"));
10166 else if (specs
->signed_p
)
10168 ("both %<signed%> and %<void%> in "
10169 "declaration specifiers"));
10170 else if (specs
->unsigned_p
)
10172 ("both %<unsigned%> and %<void%> in "
10173 "declaration specifiers"));
10174 else if (specs
->complex_p
)
10176 ("both %<complex%> and %<void%> in "
10177 "declaration specifiers"));
10178 else if (specs
->saturating_p
)
10180 ("both %<_Sat%> and %<void%> in "
10181 "declaration specifiers"));
10184 specs
->typespec_word
= cts_void
;
10185 specs
->locations
[cdw_typespec
] = loc
;
10189 if (!in_system_header_at (loc
))
10190 pedwarn_c90 (loc
, OPT_Wpedantic
,
10191 "ISO C90 does not support boolean types");
10194 ("both %<long%> and %<_Bool%> in "
10195 "declaration specifiers"));
10196 else if (specs
->short_p
)
10198 ("both %<short%> and %<_Bool%> in "
10199 "declaration specifiers"));
10200 else if (specs
->signed_p
)
10202 ("both %<signed%> and %<_Bool%> in "
10203 "declaration specifiers"));
10204 else if (specs
->unsigned_p
)
10206 ("both %<unsigned%> and %<_Bool%> in "
10207 "declaration specifiers"));
10208 else if (specs
->complex_p
)
10210 ("both %<complex%> and %<_Bool%> in "
10211 "declaration specifiers"));
10212 else if (specs
->saturating_p
)
10214 ("both %<_Sat%> and %<_Bool%> in "
10215 "declaration specifiers"));
10218 specs
->typespec_word
= cts_bool
;
10219 specs
->locations
[cdw_typespec
] = loc
;
10225 ("both %<long%> and %<char%> in "
10226 "declaration specifiers"));
10227 else if (specs
->short_p
)
10229 ("both %<short%> and %<char%> in "
10230 "declaration specifiers"));
10231 else if (specs
->saturating_p
)
10233 ("both %<_Sat%> and %<char%> in "
10234 "declaration specifiers"));
10237 specs
->typespec_word
= cts_char
;
10238 specs
->locations
[cdw_typespec
] = loc
;
10242 if (specs
->saturating_p
)
10244 ("both %<_Sat%> and %<int%> in "
10245 "declaration specifiers"));
10248 specs
->typespec_word
= cts_int
;
10249 specs
->locations
[cdw_typespec
] = loc
;
10255 ("both %<long%> and %<float%> in "
10256 "declaration specifiers"));
10257 else if (specs
->short_p
)
10259 ("both %<short%> and %<float%> in "
10260 "declaration specifiers"));
10261 else if (specs
->signed_p
)
10263 ("both %<signed%> and %<float%> in "
10264 "declaration specifiers"));
10265 else if (specs
->unsigned_p
)
10267 ("both %<unsigned%> and %<float%> in "
10268 "declaration specifiers"));
10269 else if (specs
->saturating_p
)
10271 ("both %<_Sat%> and %<float%> in "
10272 "declaration specifiers"));
10275 specs
->typespec_word
= cts_float
;
10276 specs
->locations
[cdw_typespec
] = loc
;
10280 if (specs
->long_long_p
)
10282 ("both %<long long%> and %<double%> in "
10283 "declaration specifiers"));
10284 else if (specs
->short_p
)
10286 ("both %<short%> and %<double%> in "
10287 "declaration specifiers"));
10288 else if (specs
->signed_p
)
10290 ("both %<signed%> and %<double%> in "
10291 "declaration specifiers"));
10292 else if (specs
->unsigned_p
)
10294 ("both %<unsigned%> and %<double%> in "
10295 "declaration specifiers"));
10296 else if (specs
->saturating_p
)
10298 ("both %<_Sat%> and %<double%> in "
10299 "declaration specifiers"));
10302 specs
->typespec_word
= cts_double
;
10303 specs
->locations
[cdw_typespec
] = loc
;
10308 case RID_DFLOAT128
:
10311 if (i
== RID_DFLOAT32
)
10312 str
= "_Decimal32";
10313 else if (i
== RID_DFLOAT64
)
10314 str
= "_Decimal64";
10316 str
= "_Decimal128";
10317 if (specs
->long_long_p
)
10319 ("both %<long long%> and %<%s%> in "
10320 "declaration specifiers"),
10324 ("both %<long%> and %<%s%> in "
10325 "declaration specifiers"),
10327 else if (specs
->short_p
)
10329 ("both %<short%> and %<%s%> in "
10330 "declaration specifiers"),
10332 else if (specs
->signed_p
)
10334 ("both %<signed%> and %<%s%> in "
10335 "declaration specifiers"),
10337 else if (specs
->unsigned_p
)
10339 ("both %<unsigned%> and %<%s%> in "
10340 "declaration specifiers"),
10342 else if (specs
->complex_p
)
10344 ("both %<complex%> and %<%s%> in "
10345 "declaration specifiers"),
10347 else if (specs
->saturating_p
)
10349 ("both %<_Sat%> and %<%s%> in "
10350 "declaration specifiers"),
10352 else if (i
== RID_DFLOAT32
)
10353 specs
->typespec_word
= cts_dfloat32
;
10354 else if (i
== RID_DFLOAT64
)
10355 specs
->typespec_word
= cts_dfloat64
;
10357 specs
->typespec_word
= cts_dfloat128
;
10358 specs
->locations
[cdw_typespec
] = loc
;
10360 if (!targetm
.decimal_float_supported_p ())
10362 ("decimal floating point not supported "
10363 "for this target"));
10364 pedwarn (loc
, OPT_Wpedantic
,
10365 "ISO C does not support decimal floating point");
10371 if (i
== RID_FRACT
)
10375 if (specs
->complex_p
)
10377 ("both %<complex%> and %<%s%> in "
10378 "declaration specifiers"),
10380 else if (i
== RID_FRACT
)
10381 specs
->typespec_word
= cts_fract
;
10383 specs
->typespec_word
= cts_accum
;
10384 specs
->locations
[cdw_typespec
] = loc
;
10386 if (!targetm
.fixed_point_supported_p ())
10388 "fixed-point types not supported for this target");
10389 pedwarn (loc
, OPT_Wpedantic
,
10390 "ISO C does not support fixed-point types");
10393 /* ObjC reserved word "id", handled below. */
10399 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10400 form of ObjC type, cases such as "int" and "long" being handled
10401 above), a TYPE (struct, union, enum and typeof specifiers) or an
10402 ERROR_MARK. In none of these cases may there have previously
10403 been any type specifiers. */
10404 if (specs
->type
|| specs
->typespec_word
!= cts_none
10405 || specs
->long_p
|| specs
->short_p
|| specs
->signed_p
10406 || specs
->unsigned_p
|| specs
->complex_p
)
10407 error_at (loc
, "two or more data types in declaration specifiers");
10408 else if (TREE_CODE (type
) == TYPE_DECL
)
10410 if (TREE_TYPE (type
) == error_mark_node
)
10411 ; /* Allow the type to default to int to avoid cascading errors. */
10414 specs
->type
= TREE_TYPE (type
);
10415 specs
->decl_attr
= DECL_ATTRIBUTES (type
);
10416 specs
->typedef_p
= true;
10417 specs
->explicit_signed_p
= C_TYPEDEF_EXPLICITLY_SIGNED (type
);
10418 specs
->locations
[cdw_typedef
] = loc
;
10420 /* If this typedef name is defined in a struct, then a C++
10421 lookup would return a different value. */
10422 if (warn_cxx_compat
10423 && I_SYMBOL_BINDING (DECL_NAME (type
))->in_struct
)
10424 warning_at (loc
, OPT_Wc___compat
,
10425 "C++ lookup of %qD would return a field, not a type",
10428 /* If we are parsing a struct, record that a struct field
10430 if (warn_cxx_compat
&& struct_parse_info
!= NULL
)
10431 struct_parse_info
->typedefs_seen
.safe_push (type
);
10434 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
10436 tree t
= lookup_name (type
);
10437 if (!t
|| TREE_CODE (t
) != TYPE_DECL
)
10438 error_at (loc
, "%qE fails to be a typedef or built in type", type
);
10439 else if (TREE_TYPE (t
) == error_mark_node
)
10443 specs
->type
= TREE_TYPE (t
);
10444 specs
->locations
[cdw_typespec
] = loc
;
10449 if (TREE_CODE (type
) != ERROR_MARK
&& spec
.kind
== ctsk_typeof
)
10451 specs
->typedef_p
= true;
10452 specs
->locations
[cdw_typedef
] = loc
;
10456 specs
->expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (spec
.expr
),
10457 specs
->expr
, spec
.expr
);
10459 specs
->expr
= spec
.expr
;
10460 specs
->expr_const_operands
&= spec
.expr_const_operands
;
10463 specs
->type
= type
;
10469 /* Add the storage class specifier or function specifier SCSPEC to the
10470 declaration specifiers SPECS, returning SPECS. */
10472 struct c_declspecs
*
10473 declspecs_add_scspec (source_location loc
,
10474 struct c_declspecs
*specs
,
10478 enum c_storage_class n
= csc_none
;
10480 specs
->declspecs_seen_p
= true;
10481 gcc_assert (TREE_CODE (scspec
) == IDENTIFIER_NODE
10482 && C_IS_RESERVED_WORD (scspec
));
10483 i
= C_RID_CODE (scspec
);
10484 if (specs
->non_sc_seen_p
)
10485 warning (OPT_Wold_style_declaration
,
10486 "%qE is not at beginning of declaration", scspec
);
10490 /* C99 permits duplicate inline. Although of doubtful utility,
10491 it seems simplest to permit it in gnu89 mode as well, as
10492 there is also little utility in maintaining this as a
10493 difference between gnu89 and C99 inline. */
10495 specs
->inline_p
= true;
10496 specs
->locations
[cdw_inline
] = loc
;
10499 /* Duplicate _Noreturn is permitted. */
10501 specs
->noreturn_p
= true;
10502 specs
->locations
[cdw_noreturn
] = loc
;
10505 dupe
= specs
->thread_p
;
10506 if (specs
->storage_class
== csc_auto
)
10507 error ("%qE used with %<auto%>", scspec
);
10508 else if (specs
->storage_class
== csc_register
)
10509 error ("%qE used with %<register%>", scspec
);
10510 else if (specs
->storage_class
== csc_typedef
)
10511 error ("%qE used with %<typedef%>", scspec
);
10514 specs
->thread_p
= true;
10515 specs
->thread_gnu_p
= (strcmp (IDENTIFIER_POINTER (scspec
),
10517 /* A diagnostic is not required for the use of this
10518 identifier in the implementation namespace; only diagnose
10519 it for the C11 spelling because of existing code using
10520 the other spelling. */
10521 if (!specs
->thread_gnu_p
)
10524 pedwarn_c99 (loc
, OPT_Wpedantic
,
10525 "ISO C99 does not support %qE", scspec
);
10527 pedwarn_c99 (loc
, OPT_Wpedantic
,
10528 "ISO C90 does not support %qE", scspec
);
10530 specs
->locations
[cdw_thread
] = loc
;
10538 /* Diagnose "__thread extern". */
10539 if (specs
->thread_p
&& specs
->thread_gnu_p
)
10540 error ("%<__thread%> before %<extern%>");
10547 /* Diagnose "__thread static". */
10548 if (specs
->thread_p
&& specs
->thread_gnu_p
)
10549 error ("%<__thread%> before %<static%>");
10555 gcc_unreachable ();
10557 if (n
!= csc_none
&& n
== specs
->storage_class
)
10561 if (i
== RID_THREAD
)
10562 error ("duplicate %<_Thread_local%> or %<__thread%>");
10564 error ("duplicate %qE", scspec
);
10568 if (specs
->storage_class
!= csc_none
&& n
!= specs
->storage_class
)
10570 error ("multiple storage classes in declaration specifiers");
10574 specs
->storage_class
= n
;
10575 specs
->locations
[cdw_storage_class
] = loc
;
10576 if (n
!= csc_extern
&& n
!= csc_static
&& specs
->thread_p
)
10578 error ("%qs used with %qE",
10579 specs
->thread_gnu_p
? "__thread" : "_Thread_local",
10581 specs
->thread_p
= false;
10588 /* Add the attributes ATTRS to the declaration specifiers SPECS,
10589 returning SPECS. */
10591 struct c_declspecs
*
10592 declspecs_add_attrs (source_location loc
, struct c_declspecs
*specs
, tree attrs
)
10594 specs
->attrs
= chainon (attrs
, specs
->attrs
);
10595 specs
->locations
[cdw_attributes
] = loc
;
10596 specs
->declspecs_seen_p
= true;
10600 /* Add an _Alignas specifier (expression ALIGN, or type whose
10601 alignment is ALIGN) to the declaration specifiers SPECS, returning
10603 struct c_declspecs
*
10604 declspecs_add_alignas (source_location loc
,
10605 struct c_declspecs
*specs
, tree align
)
10608 specs
->alignas_p
= true;
10609 specs
->locations
[cdw_alignas
] = loc
;
10610 if (align
== error_mark_node
)
10612 align_log
= check_user_alignment (align
, true);
10613 if (align_log
> specs
->align_log
)
10614 specs
->align_log
= align_log
;
10618 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
10619 specifiers with any other type specifier to determine the resulting
10620 type. This is where ISO C checks on complex types are made, since
10621 "_Complex long" is a prefix of the valid ISO C type "_Complex long
10624 struct c_declspecs
*
10625 finish_declspecs (struct c_declspecs
*specs
)
10627 /* If a type was specified as a whole, we have no modifiers and are
10629 if (specs
->type
!= NULL_TREE
)
10631 gcc_assert (!specs
->long_p
&& !specs
->long_long_p
&& !specs
->short_p
10632 && !specs
->signed_p
&& !specs
->unsigned_p
10633 && !specs
->complex_p
);
10635 /* Set a dummy type. */
10636 if (TREE_CODE (specs
->type
) == ERROR_MARK
)
10637 specs
->type
= integer_type_node
;
10641 /* If none of "void", "_Bool", "char", "int", "float" or "double"
10642 has been specified, treat it as "int" unless "_Complex" is
10643 present and there are no other specifiers. If we just have
10644 "_Complex", it is equivalent to "_Complex double", but e.g.
10645 "_Complex short" is equivalent to "_Complex short int". */
10646 if (specs
->typespec_word
== cts_none
)
10648 if (specs
->saturating_p
)
10650 error_at (specs
->locations
[cdw_saturating
],
10651 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
10652 if (!targetm
.fixed_point_supported_p ())
10653 error_at (specs
->locations
[cdw_saturating
],
10654 "fixed-point types not supported for this target");
10655 specs
->typespec_word
= cts_fract
;
10657 else if (specs
->long_p
|| specs
->short_p
10658 || specs
->signed_p
|| specs
->unsigned_p
)
10660 specs
->typespec_word
= cts_int
;
10662 else if (specs
->complex_p
)
10664 specs
->typespec_word
= cts_double
;
10665 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
10666 "ISO C does not support plain %<complex%> meaning "
10667 "%<double complex%>");
10671 specs
->typespec_word
= cts_int
;
10672 specs
->default_int_p
= true;
10673 /* We don't diagnose this here because grokdeclarator will
10674 give more specific diagnostics according to whether it is
10675 a function definition. */
10679 /* If "signed" was specified, record this to distinguish "int" and
10680 "signed int" in the case of a bit-field with
10681 -funsigned-bitfields. */
10682 specs
->explicit_signed_p
= specs
->signed_p
;
10684 /* Now compute the actual type. */
10685 switch (specs
->typespec_word
)
10687 case cts_auto_type
:
10688 gcc_assert (!specs
->long_p
&& !specs
->short_p
10689 && !specs
->signed_p
&& !specs
->unsigned_p
10690 && !specs
->complex_p
);
10691 /* Type to be filled in later. */
10694 gcc_assert (!specs
->long_p
&& !specs
->short_p
10695 && !specs
->signed_p
&& !specs
->unsigned_p
10696 && !specs
->complex_p
);
10697 specs
->type
= void_type_node
;
10700 gcc_assert (!specs
->long_p
&& !specs
->short_p
10701 && !specs
->signed_p
&& !specs
->unsigned_p
10702 && !specs
->complex_p
);
10703 specs
->type
= boolean_type_node
;
10706 gcc_assert (!specs
->long_p
&& !specs
->short_p
);
10707 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
10708 if (specs
->signed_p
)
10709 specs
->type
= signed_char_type_node
;
10710 else if (specs
->unsigned_p
)
10711 specs
->type
= unsigned_char_type_node
;
10713 specs
->type
= char_type_node
;
10714 if (specs
->complex_p
)
10716 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
10717 "ISO C does not support complex integer types");
10718 specs
->type
= build_complex_type (specs
->type
);
10722 gcc_assert (!specs
->long_p
&& !specs
->short_p
&& !specs
->long_long_p
);
10723 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
10724 specs
->type
= (specs
->unsigned_p
10725 ? int_n_trees
[specs
->int_n_idx
].unsigned_type
10726 : int_n_trees
[specs
->int_n_idx
].signed_type
);
10727 if (specs
->complex_p
)
10729 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
10730 "ISO C does not support complex integer types");
10731 specs
->type
= build_complex_type (specs
->type
);
10735 gcc_assert (!(specs
->long_p
&& specs
->short_p
));
10736 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
10737 if (specs
->long_long_p
)
10738 specs
->type
= (specs
->unsigned_p
10739 ? long_long_unsigned_type_node
10740 : long_long_integer_type_node
);
10741 else if (specs
->long_p
)
10742 specs
->type
= (specs
->unsigned_p
10743 ? long_unsigned_type_node
10744 : long_integer_type_node
);
10745 else if (specs
->short_p
)
10746 specs
->type
= (specs
->unsigned_p
10747 ? short_unsigned_type_node
10748 : short_integer_type_node
);
10750 specs
->type
= (specs
->unsigned_p
10751 ? unsigned_type_node
10752 : integer_type_node
);
10753 if (specs
->complex_p
)
10755 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
10756 "ISO C does not support complex integer types");
10757 specs
->type
= build_complex_type (specs
->type
);
10761 gcc_assert (!specs
->long_p
&& !specs
->short_p
10762 && !specs
->signed_p
&& !specs
->unsigned_p
);
10763 specs
->type
= (specs
->complex_p
10764 ? complex_float_type_node
10765 : float_type_node
);
10768 gcc_assert (!specs
->long_long_p
&& !specs
->short_p
10769 && !specs
->signed_p
&& !specs
->unsigned_p
);
10772 specs
->type
= (specs
->complex_p
10773 ? complex_long_double_type_node
10774 : long_double_type_node
);
10778 specs
->type
= (specs
->complex_p
10779 ? complex_double_type_node
10780 : double_type_node
);
10785 case cts_dfloat128
:
10786 gcc_assert (!specs
->long_p
&& !specs
->long_long_p
&& !specs
->short_p
10787 && !specs
->signed_p
&& !specs
->unsigned_p
&& !specs
->complex_p
);
10788 if (specs
->typespec_word
== cts_dfloat32
)
10789 specs
->type
= dfloat32_type_node
;
10790 else if (specs
->typespec_word
== cts_dfloat64
)
10791 specs
->type
= dfloat64_type_node
;
10793 specs
->type
= dfloat128_type_node
;
10796 gcc_assert (!specs
->complex_p
);
10797 if (!targetm
.fixed_point_supported_p ())
10798 specs
->type
= integer_type_node
;
10799 else if (specs
->saturating_p
)
10801 if (specs
->long_long_p
)
10802 specs
->type
= specs
->unsigned_p
10803 ? sat_unsigned_long_long_fract_type_node
10804 : sat_long_long_fract_type_node
;
10805 else if (specs
->long_p
)
10806 specs
->type
= specs
->unsigned_p
10807 ? sat_unsigned_long_fract_type_node
10808 : sat_long_fract_type_node
;
10809 else if (specs
->short_p
)
10810 specs
->type
= specs
->unsigned_p
10811 ? sat_unsigned_short_fract_type_node
10812 : sat_short_fract_type_node
;
10814 specs
->type
= specs
->unsigned_p
10815 ? sat_unsigned_fract_type_node
10816 : sat_fract_type_node
;
10820 if (specs
->long_long_p
)
10821 specs
->type
= specs
->unsigned_p
10822 ? unsigned_long_long_fract_type_node
10823 : long_long_fract_type_node
;
10824 else if (specs
->long_p
)
10825 specs
->type
= specs
->unsigned_p
10826 ? unsigned_long_fract_type_node
10827 : long_fract_type_node
;
10828 else if (specs
->short_p
)
10829 specs
->type
= specs
->unsigned_p
10830 ? unsigned_short_fract_type_node
10831 : short_fract_type_node
;
10833 specs
->type
= specs
->unsigned_p
10834 ? unsigned_fract_type_node
10839 gcc_assert (!specs
->complex_p
);
10840 if (!targetm
.fixed_point_supported_p ())
10841 specs
->type
= integer_type_node
;
10842 else if (specs
->saturating_p
)
10844 if (specs
->long_long_p
)
10845 specs
->type
= specs
->unsigned_p
10846 ? sat_unsigned_long_long_accum_type_node
10847 : sat_long_long_accum_type_node
;
10848 else if (specs
->long_p
)
10849 specs
->type
= specs
->unsigned_p
10850 ? sat_unsigned_long_accum_type_node
10851 : sat_long_accum_type_node
;
10852 else if (specs
->short_p
)
10853 specs
->type
= specs
->unsigned_p
10854 ? sat_unsigned_short_accum_type_node
10855 : sat_short_accum_type_node
;
10857 specs
->type
= specs
->unsigned_p
10858 ? sat_unsigned_accum_type_node
10859 : sat_accum_type_node
;
10863 if (specs
->long_long_p
)
10864 specs
->type
= specs
->unsigned_p
10865 ? unsigned_long_long_accum_type_node
10866 : long_long_accum_type_node
;
10867 else if (specs
->long_p
)
10868 specs
->type
= specs
->unsigned_p
10869 ? unsigned_long_accum_type_node
10870 : long_accum_type_node
;
10871 else if (specs
->short_p
)
10872 specs
->type
= specs
->unsigned_p
10873 ? unsigned_short_accum_type_node
10874 : short_accum_type_node
;
10876 specs
->type
= specs
->unsigned_p
10877 ? unsigned_accum_type_node
10882 gcc_unreachable ();
10888 /* Perform final processing on one file scope's declarations (or the
10889 external scope's declarations), GLOBALS. */
10892 c_write_global_declarations_1 (tree globals
)
10897 /* Process the decls in the order they were written. */
10898 for (decl
= globals
; decl
; decl
= DECL_CHAIN (decl
))
10900 /* Check for used but undefined static functions using the C
10901 standard's definition of "used", and set TREE_NO_WARNING so
10902 that check_global_declaration doesn't repeat the check. */
10903 if (TREE_CODE (decl
) == FUNCTION_DECL
10904 && DECL_INITIAL (decl
) == 0
10905 && DECL_EXTERNAL (decl
)
10906 && !TREE_PUBLIC (decl
))
10908 if (C_DECL_USED (decl
))
10910 pedwarn (input_location
, 0, "%q+F used but never defined", decl
);
10911 TREE_NO_WARNING (decl
) = 1;
10913 /* For -Wunused-function warn about unused static prototypes. */
10914 else if (warn_unused_function
10915 && ! DECL_ARTIFICIAL (decl
)
10916 && ! TREE_NO_WARNING (decl
))
10918 warning (OPT_Wunused_function
,
10919 "%q+F declared %<static%> but never defined", decl
);
10920 TREE_NO_WARNING (decl
) = 1;
10924 wrapup_global_declaration_1 (decl
);
10929 reconsider
= false;
10930 for (decl
= globals
; decl
; decl
= DECL_CHAIN (decl
))
10931 reconsider
|= wrapup_global_declaration_2 (decl
);
10933 while (reconsider
);
10936 /* Callback to collect a source_ref from a DECL. */
10939 collect_source_ref_cb (tree decl
)
10941 if (!DECL_IS_BUILTIN (decl
))
10942 collect_source_ref (LOCATION_FILE (decl_sloc (decl
, false)));
10945 /* Preserve the external declarations scope across a garbage collect. */
10946 static GTY(()) tree ext_block
;
10948 /* Collect all references relevant to SOURCE_FILE. */
10951 collect_all_refs (const char *source_file
)
10956 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
10957 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t
)), source_file
);
10959 collect_ada_nodes (BLOCK_VARS (ext_block
), source_file
);
10962 /* Iterate over all global declarations and call CALLBACK. */
10965 for_each_global_decl (void (*callback
) (tree decl
))
10972 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
10974 decls
= DECL_INITIAL (t
);
10975 for (decl
= BLOCK_VARS (decls
); decl
; decl
= TREE_CHAIN (decl
))
10979 for (decl
= BLOCK_VARS (ext_block
); decl
; decl
= TREE_CHAIN (decl
))
10983 /* Perform any final parser cleanups and generate initial debugging
10987 c_parse_final_cleanups (void)
10992 /* We don't want to do this if generating a PCH. */
10996 timevar_stop (TV_PHASE_PARSING
);
10997 timevar_start (TV_PHASE_DEFERRED
);
10999 /* Do the Objective-C stuff. This is where all the Objective-C
11000 module stuff gets generated (symtab, class/protocol/selector
11002 if (c_dialect_objc ())
11003 objc_write_global_declarations ();
11005 /* Close the external scope. */
11006 ext_block
= pop_scope ();
11007 external_scope
= 0;
11008 gcc_assert (!current_scope
);
11010 /* Handle -fdump-ada-spec[-slim]. */
11011 if (flag_dump_ada_spec
|| flag_dump_ada_spec_slim
)
11013 /* Build a table of files to generate specs for */
11014 if (flag_dump_ada_spec_slim
)
11015 collect_source_ref (main_input_filename
);
11017 for_each_global_decl (collect_source_ref_cb
);
11019 dump_ada_specs (collect_all_refs
, NULL
);
11024 tree tmp
= BLOCK_VARS (ext_block
);
11026 FILE * stream
= dump_begin (TDI_tu
, &flags
);
11029 dump_node (tmp
, flags
& ~TDF_SLIM
, stream
);
11030 dump_end (TDI_tu
, stream
);
11034 /* Process all file scopes in this compilation, and the external_scope,
11035 through wrapup_global_declarations. */
11036 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
11037 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t
)));
11038 c_write_global_declarations_1 (BLOCK_VARS (ext_block
));
11040 timevar_stop (TV_PHASE_DEFERRED
);
11041 timevar_start (TV_PHASE_PARSING
);
11046 /* Register reserved keyword WORD as qualifier for address space AS. */
11049 c_register_addr_space (const char *word
, addr_space_t as
)
11051 int rid
= RID_FIRST_ADDR_SPACE
+ as
;
11054 /* Address space qualifiers are only supported
11055 in C with GNU extensions enabled. */
11056 if (c_dialect_objc () || flag_no_asm
)
11059 id
= get_identifier (word
);
11060 C_SET_RID_CODE (id
, rid
);
11061 C_IS_RESERVED_WORD (id
) = 1;
11062 ridpointers
[rid
] = id
;
11065 /* Return identifier to look up for omp declare reduction. */
11068 c_omp_reduction_id (enum tree_code reduction_code
, tree reduction_id
)
11070 const char *p
= NULL
;
11071 switch (reduction_code
)
11073 case PLUS_EXPR
: p
= "+"; break;
11074 case MULT_EXPR
: p
= "*"; break;
11075 case MINUS_EXPR
: p
= "-"; break;
11076 case BIT_AND_EXPR
: p
= "&"; break;
11077 case BIT_XOR_EXPR
: p
= "^"; break;
11078 case BIT_IOR_EXPR
: p
= "|"; break;
11079 case TRUTH_ANDIF_EXPR
: p
= "&&"; break;
11080 case TRUTH_ORIF_EXPR
: p
= "||"; break;
11081 case MIN_EXPR
: p
= "min"; break;
11082 case MAX_EXPR
: p
= "max"; break;
11089 if (TREE_CODE (reduction_id
) != IDENTIFIER_NODE
)
11090 return error_mark_node
;
11091 p
= IDENTIFIER_POINTER (reduction_id
);
11094 const char prefix
[] = "omp declare reduction ";
11095 size_t lenp
= sizeof (prefix
);
11096 size_t len
= strlen (p
);
11097 char *name
= XALLOCAVEC (char, lenp
+ len
);
11098 memcpy (name
, prefix
, lenp
- 1);
11099 memcpy (name
+ lenp
- 1, p
, len
+ 1);
11100 return get_identifier (name
);
11103 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11104 VAR_DECL, bind it into the current scope and return it. */
11107 c_omp_reduction_decl (tree reduction_id
)
11109 struct c_binding
*b
= I_SYMBOL_BINDING (reduction_id
);
11110 if (b
!= NULL
&& B_IN_CURRENT_SCOPE (b
))
11113 tree decl
= build_decl (BUILTINS_LOCATION
, VAR_DECL
,
11114 reduction_id
, integer_type_node
);
11115 DECL_ARTIFICIAL (decl
) = 1;
11116 DECL_EXTERNAL (decl
) = 1;
11117 TREE_STATIC (decl
) = 1;
11118 TREE_PUBLIC (decl
) = 0;
11119 bind (reduction_id
, decl
, current_scope
, true, false, BUILTINS_LOCATION
);
11123 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11126 c_omp_reduction_lookup (tree reduction_id
, tree type
)
11128 struct c_binding
*b
= I_SYMBOL_BINDING (reduction_id
);
11132 for (t
= DECL_INITIAL (b
->decl
); t
; t
= TREE_CHAIN (t
))
11133 if (comptypes (TREE_PURPOSE (t
), type
))
11134 return TREE_VALUE (t
);
11137 return error_mark_node
;
11140 /* Helper function called via walk_tree, to diagnose invalid
11141 #pragma omp declare reduction combiners or initializers. */
11144 c_check_omp_declare_reduction_r (tree
*tp
, int *, void *data
)
11146 tree
*vars
= (tree
*) data
;
11147 if (SSA_VAR_P (*tp
)
11148 && !DECL_ARTIFICIAL (*tp
)
11152 location_t loc
= DECL_SOURCE_LOCATION (vars
[0]);
11153 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars
[0])), "omp_out") == 0)
11154 error_at (loc
, "%<#pragma omp declare reduction%> combiner refers to "
11155 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11158 error_at (loc
, "%<#pragma omp declare reduction%> initializer refers "
11159 "to variable %qD which is not %<omp_priv%> nor "
11167 #include "gt-c-c-decl.h"