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 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 vec
<c_binding_ptr
> fields
;
583 /* If warn_cxx_compat, a list of typedef names used when defining
584 fields in this struct. */
585 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 tree hint
= NULL_TREE
;
3093 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_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 %qE?",
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 %qE?"),
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 tree 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 %qE?",
3447 error_at (loc
, "%qE undeclared here (not in a function)", id
);
3448 scope
= current_scope
;
3452 if (!objc_diagnose_private_ivar (id
))
3454 tree 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 %qE?",
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 /* Specialization of edit_distance_traits for preprocessor macros. */
3961 struct edit_distance_traits
<cpp_hashnode
*>
3963 static size_t get_length (cpp_hashnode
*hashnode
)
3965 return hashnode
->ident
.len
;
3968 static const char *get_string (cpp_hashnode
*hashnode
)
3970 return (const char *)hashnode
->ident
.str
;
3974 /* Specialization of best_match<> for finding the closest preprocessor
3975 macro to a given identifier. */
3977 typedef best_match
<tree
, cpp_hashnode
*> best_macro_match
;
3979 /* A callback for cpp_forall_identifiers, for use by lookup_name_fuzzy.
3980 Process HASHNODE and update the best_macro_match instance pointed to be
3984 find_closest_macro_cpp_cb (cpp_reader
*, cpp_hashnode
*hashnode
,
3987 if (hashnode
->type
!= NT_MACRO
)
3990 best_macro_match
*bmm
= (best_macro_match
*)user_data
;
3991 bmm
->consider (hashnode
);
3993 /* Keep iterating. */
3997 /* Look for the closest match for NAME within the currently valid
4000 This finds the identifier with the lowest Levenshtein distance to
4001 NAME. If there are multiple candidates with equal minimal distance,
4002 the first one found is returned. Scopes are searched from innermost
4003 outwards, and within a scope in reverse order of declaration, thus
4004 benefiting candidates "near" to the current scope.
4006 The function also looks for similar macro names to NAME, since a
4007 misspelled macro name will not be expanded, and hence looks like an
4008 identifier to the C frontend.
4010 It also looks for start_typename keywords, to detect "singed" vs "signed"
4014 lookup_name_fuzzy (tree name
, enum lookup_name_fuzzy_kind kind
)
4016 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
4018 best_match
<tree
, tree
> bm (name
);
4020 /* Look within currently valid scopes. */
4021 for (c_scope
*scope
= current_scope
; scope
; scope
= scope
->outer
)
4022 for (c_binding
*binding
= scope
->bindings
; binding
; binding
= binding
->prev
)
4026 /* Don't use bindings from implicitly declared functions,
4027 as they were likely misspellings themselves. */
4028 if (TREE_CODE (binding
->decl
) == FUNCTION_DECL
)
4029 if (C_DECL_IMPLICIT (binding
->decl
))
4031 if (kind
== FUZZY_LOOKUP_TYPENAME
)
4032 if (TREE_CODE (binding
->decl
) != TYPE_DECL
)
4034 bm
.consider (binding
->id
);
4037 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4039 x = SOME_OTHER_MACRO (y);
4040 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4041 as a misspelled identifier.
4043 Use the best distance so far so that a candidate is only set if
4044 a macro is better than anything so far. This allows early rejection
4045 (without calculating the edit distance) of macro names that must have
4046 distance >= bm.get_best_distance (), and means that we only get a
4047 non-NULL result for best_macro_match if it's better than any of
4048 the identifiers already checked, which avoids needless creation
4049 of identifiers for macro hashnodes. */
4050 best_macro_match
bmm (name
, bm
.get_best_distance ());
4051 cpp_forall_identifiers (parse_in
, find_closest_macro_cpp_cb
, &bmm
);
4052 cpp_hashnode
*best_macro
= bmm
.get_best_meaningful_candidate ();
4053 /* If a macro is the closest so far to NAME, use it, creating an
4054 identifier tree node for it. */
4057 const char *id
= (const char *)best_macro
->ident
.str
;
4058 tree macro_as_identifier
4059 = get_identifier_with_length (id
, best_macro
->ident
.len
);
4060 bm
.set_best_so_far (macro_as_identifier
,
4061 bmm
.get_best_distance (),
4062 bmm
.get_best_candidate_length ());
4065 /* Try the "start_typename" keywords to detect
4066 "singed" vs "signed" typos. */
4067 if (kind
== FUZZY_LOOKUP_TYPENAME
)
4069 for (unsigned i
= 0; i
< num_c_common_reswords
; i
++)
4071 const c_common_resword
*resword
= &c_common_reswords
[i
];
4072 if (!c_keyword_starts_typename (resword
->rid
))
4074 tree resword_identifier
= ridpointers
[resword
->rid
];
4075 if (!resword_identifier
)
4077 gcc_assert (TREE_CODE (resword_identifier
) == IDENTIFIER_NODE
);
4078 bm
.consider (resword_identifier
);
4082 return bm
.get_best_meaningful_candidate ();
4086 /* Create the predefined scalar types of C,
4087 and some nodes representing standard constants (0, 1, (void *) 0).
4088 Initialize the global scope.
4089 Make definitions for built-in primitive functions. */
4092 c_init_decl_processing (void)
4094 location_t save_loc
= input_location
;
4096 /* Initialize reserved words for parser. */
4099 current_function_decl
= 0;
4101 gcc_obstack_init (&parser_obstack
);
4103 /* Make the externals scope. */
4105 external_scope
= current_scope
;
4107 /* Declarations from c_common_nodes_and_builtins must not be associated
4108 with this input file, lest we get differences between using and not
4109 using preprocessed headers. */
4110 input_location
= BUILTINS_LOCATION
;
4112 c_common_nodes_and_builtins ();
4114 /* In C, comparisons and TRUTH_* expressions have type int. */
4115 truthvalue_type_node
= integer_type_node
;
4116 truthvalue_true_node
= integer_one_node
;
4117 truthvalue_false_node
= integer_zero_node
;
4119 /* Even in C99, which has a real boolean type. */
4120 pushdecl (build_decl (UNKNOWN_LOCATION
, TYPE_DECL
, get_identifier ("_Bool"),
4121 boolean_type_node
));
4123 input_location
= save_loc
;
4125 make_fname_decl
= c_make_fname_decl
;
4126 start_fname_decls ();
4129 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4130 give the decl, NAME is the initialization string and TYPE_DEP
4131 indicates whether NAME depended on the type of the function. As we
4132 don't yet implement delayed emission of static data, we mark the
4133 decl as emitted so it is not placed in the output. Anything using
4134 it must therefore pull out the STRING_CST initializer directly.
4138 c_make_fname_decl (location_t loc
, tree id
, int type_dep
)
4140 const char *name
= fname_as_string (type_dep
);
4141 tree decl
, type
, init
;
4142 size_t length
= strlen (name
);
4144 type
= build_array_type (char_type_node
,
4145 build_index_type (size_int (length
)));
4146 type
= c_build_qualified_type (type
, TYPE_QUAL_CONST
);
4148 decl
= build_decl (loc
, VAR_DECL
, id
, type
);
4150 TREE_STATIC (decl
) = 1;
4151 TREE_READONLY (decl
) = 1;
4152 DECL_ARTIFICIAL (decl
) = 1;
4154 init
= build_string (length
+ 1, name
);
4155 free (CONST_CAST (char *, name
));
4156 TREE_TYPE (init
) = type
;
4157 DECL_INITIAL (decl
) = init
;
4159 TREE_USED (decl
) = 1;
4161 if (current_function_decl
4162 /* For invalid programs like this:
4165 const char* p = __FUNCTION__;
4167 the __FUNCTION__ is believed to appear in K&R style function
4168 parameter declarator. In that case we still don't have
4170 && current_function_scope
)
4172 DECL_CONTEXT (decl
) = current_function_decl
;
4173 bind (id
, decl
, current_function_scope
,
4174 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
4177 finish_decl (decl
, loc
, init
, NULL_TREE
, NULL_TREE
);
4183 c_builtin_function (tree decl
)
4185 tree type
= TREE_TYPE (decl
);
4186 tree id
= DECL_NAME (decl
);
4188 const char *name
= IDENTIFIER_POINTER (id
);
4189 C_DECL_BUILTIN_PROTOTYPE (decl
) = prototype_p (type
);
4191 /* Should never be called on a symbol with a preexisting meaning. */
4192 gcc_assert (!I_SYMBOL_BINDING (id
));
4194 bind (id
, decl
, external_scope
, /*invisible=*/true, /*nested=*/false,
4197 /* Builtins in the implementation namespace are made visible without
4198 needing to be explicitly declared. See push_file_scope. */
4199 if (name
[0] == '_' && (name
[1] == '_' || ISUPPER (name
[1])))
4201 DECL_CHAIN (decl
) = visible_builtins
;
4202 visible_builtins
= decl
;
4209 c_builtin_function_ext_scope (tree decl
)
4211 tree type
= TREE_TYPE (decl
);
4212 tree id
= DECL_NAME (decl
);
4214 const char *name
= IDENTIFIER_POINTER (id
);
4215 C_DECL_BUILTIN_PROTOTYPE (decl
) = prototype_p (type
);
4218 bind (id
, decl
, external_scope
, /*invisible=*/false, /*nested=*/false,
4221 /* Builtins in the implementation namespace are made visible without
4222 needing to be explicitly declared. See push_file_scope. */
4223 if (name
[0] == '_' && (name
[1] == '_' || ISUPPER (name
[1])))
4225 DECL_CHAIN (decl
) = visible_builtins
;
4226 visible_builtins
= decl
;
4232 /* Called when a declaration is seen that contains no names to declare.
4233 If its type is a reference to a structure, union or enum inherited
4234 from a containing scope, shadow that tag name for the current scope
4235 with a forward reference.
4236 If its type defines a new named structure or union
4237 or defines an enum, it is valid but we need not do anything here.
4238 Otherwise, it is an error. */
4241 shadow_tag (const struct c_declspecs
*declspecs
)
4243 shadow_tag_warned (declspecs
, 0);
4246 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4249 shadow_tag_warned (const struct c_declspecs
*declspecs
, int warned
)
4251 bool found_tag
= false;
4253 if (declspecs
->type
&& !declspecs
->default_int_p
&& !declspecs
->typedef_p
)
4255 tree value
= declspecs
->type
;
4256 enum tree_code code
= TREE_CODE (value
);
4258 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
|| code
== ENUMERAL_TYPE
)
4259 /* Used to test also that TYPE_SIZE (value) != 0.
4260 That caused warning for `struct foo;' at top level in the file. */
4262 tree name
= TYPE_NAME (value
);
4267 if (declspecs
->restrict_p
)
4269 error ("invalid use of %<restrict%>");
4275 if (warned
!= 1 && code
!= ENUMERAL_TYPE
)
4276 /* Empty unnamed enum OK */
4278 pedwarn (input_location
, 0,
4279 "unnamed struct/union that defines no instances");
4283 else if (declspecs
->typespec_kind
!= ctsk_tagdef
4284 && declspecs
->typespec_kind
!= ctsk_tagfirstref
4285 && declspecs
->storage_class
!= csc_none
)
4288 pedwarn (input_location
, 0,
4289 "empty declaration with storage class specifier "
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
->const_p
4297 || declspecs
->volatile_p
4298 || declspecs
->atomic_p
4299 || declspecs
->restrict_p
4300 || declspecs
->address_space
))
4303 pedwarn (input_location
, 0,
4304 "empty declaration with type qualifier "
4305 "does not redeclare tag");
4307 pending_xref_error ();
4309 else if (declspecs
->typespec_kind
!= ctsk_tagdef
4310 && declspecs
->typespec_kind
!= ctsk_tagfirstref
4311 && declspecs
->alignas_p
)
4314 pedwarn (input_location
, 0,
4315 "empty declaration with %<_Alignas%> "
4316 "does not redeclare tag");
4318 pending_xref_error ();
4322 pending_invalid_xref
= 0;
4323 t
= lookup_tag (code
, name
, true, NULL
);
4327 t
= make_node (code
);
4328 pushtag (input_location
, name
, t
);
4334 if (warned
!= 1 && !in_system_header_at (input_location
))
4336 pedwarn (input_location
, 0,
4337 "useless type name in empty declaration");
4342 else if (warned
!= 1 && !in_system_header_at (input_location
)
4343 && declspecs
->typedef_p
)
4345 pedwarn (input_location
, 0, "useless type name in empty declaration");
4349 pending_invalid_xref
= 0;
4351 if (declspecs
->inline_p
)
4353 error ("%<inline%> in empty declaration");
4357 if (declspecs
->noreturn_p
)
4359 error ("%<_Noreturn%> in empty declaration");
4363 if (current_scope
== file_scope
&& declspecs
->storage_class
== csc_auto
)
4365 error ("%<auto%> in file-scope empty declaration");
4369 if (current_scope
== file_scope
&& declspecs
->storage_class
== csc_register
)
4371 error ("%<register%> in file-scope empty declaration");
4375 if (!warned
&& !in_system_header_at (input_location
)
4376 && declspecs
->storage_class
!= csc_none
)
4378 warning (0, "useless storage class specifier in empty declaration");
4382 if (!warned
&& !in_system_header_at (input_location
) && declspecs
->thread_p
)
4384 warning (0, "useless %qs in empty declaration",
4385 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
4390 && !in_system_header_at (input_location
)
4391 && (declspecs
->const_p
4392 || declspecs
->volatile_p
4393 || declspecs
->atomic_p
4394 || declspecs
->restrict_p
4395 || declspecs
->address_space
))
4397 warning (0, "useless type qualifier in empty declaration");
4401 if (!warned
&& !in_system_header_at (input_location
)
4402 && declspecs
->alignas_p
)
4404 warning (0, "useless %<_Alignas%> in empty declaration");
4411 pedwarn (input_location
, 0, "empty declaration");
4416 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4417 bits. SPECS represents declaration specifiers that the grammar
4418 only permits to contain type qualifiers and attributes. */
4421 quals_from_declspecs (const struct c_declspecs
*specs
)
4423 int quals
= ((specs
->const_p
? TYPE_QUAL_CONST
: 0)
4424 | (specs
->volatile_p
? TYPE_QUAL_VOLATILE
: 0)
4425 | (specs
->restrict_p
? TYPE_QUAL_RESTRICT
: 0)
4426 | (specs
->atomic_p
? TYPE_QUAL_ATOMIC
: 0)
4427 | (ENCODE_QUAL_ADDR_SPACE (specs
->address_space
)));
4428 gcc_assert (!specs
->type
4429 && !specs
->decl_attr
4430 && specs
->typespec_word
== cts_none
4431 && specs
->storage_class
== csc_none
4432 && !specs
->typedef_p
4433 && !specs
->explicit_signed_p
4434 && !specs
->deprecated_p
4436 && !specs
->long_long_p
4439 && !specs
->unsigned_p
4440 && !specs
->complex_p
4442 && !specs
->noreturn_p
4443 && !specs
->thread_p
);
4447 /* Construct an array declarator. LOC is the location of the
4448 beginning of the array (usually the opening brace). EXPR is the
4449 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4450 inside the [] (to be applied to the pointer to which a parameter
4451 array is converted). STATIC_P is true if "static" is inside the
4452 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4453 VLA of unspecified length which is nevertheless a complete type,
4454 false otherwise. The field for the contained declarator is left to
4455 be filled in by set_array_declarator_inner. */
4457 struct c_declarator
*
4458 build_array_declarator (location_t loc
,
4459 tree expr
, struct c_declspecs
*quals
, bool static_p
,
4462 struct c_declarator
*declarator
= XOBNEW (&parser_obstack
,
4463 struct c_declarator
);
4464 declarator
->id_loc
= loc
;
4465 declarator
->kind
= cdk_array
;
4466 declarator
->declarator
= 0;
4467 declarator
->u
.array
.dimen
= expr
;
4470 declarator
->u
.array
.attrs
= quals
->attrs
;
4471 declarator
->u
.array
.quals
= quals_from_declspecs (quals
);
4475 declarator
->u
.array
.attrs
= NULL_TREE
;
4476 declarator
->u
.array
.quals
= 0;
4478 declarator
->u
.array
.static_p
= static_p
;
4479 declarator
->u
.array
.vla_unspec_p
= vla_unspec_p
;
4480 if (static_p
|| quals
!= NULL
)
4481 pedwarn_c90 (loc
, OPT_Wpedantic
,
4482 "ISO C90 does not support %<static%> or type "
4483 "qualifiers in parameter array declarators");
4485 pedwarn_c90 (loc
, OPT_Wpedantic
,
4486 "ISO C90 does not support %<[*]%> array declarators");
4489 if (!current_scope
->parm_flag
)
4492 error_at (loc
, "%<[*]%> not allowed in other than "
4493 "function prototype scope");
4494 declarator
->u
.array
.vla_unspec_p
= false;
4497 current_scope
->had_vla_unspec
= true;
4502 /* Set the contained declarator of an array declarator. DECL is the
4503 declarator, as constructed by build_array_declarator; INNER is what
4504 appears on the left of the []. */
4506 struct c_declarator
*
4507 set_array_declarator_inner (struct c_declarator
*decl
,
4508 struct c_declarator
*inner
)
4510 decl
->declarator
= inner
;
4514 /* INIT is a constructor that forms DECL's initializer. If the final
4515 element initializes a flexible array field, add the size of that
4516 initializer to DECL's size. */
4519 add_flexible_array_elts_to_size (tree decl
, tree init
)
4523 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
4526 elt
= CONSTRUCTOR_ELTS (init
)->last ().value
;
4527 type
= TREE_TYPE (elt
);
4528 if (TREE_CODE (type
) == ARRAY_TYPE
4529 && TYPE_SIZE (type
) == NULL_TREE
4530 && TYPE_DOMAIN (type
) != NULL_TREE
4531 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
)
4533 complete_array_type (&type
, elt
, false);
4535 = size_binop (PLUS_EXPR
, DECL_SIZE (decl
), TYPE_SIZE (type
));
4536 DECL_SIZE_UNIT (decl
)
4537 = size_binop (PLUS_EXPR
, DECL_SIZE_UNIT (decl
), TYPE_SIZE_UNIT (type
));
4541 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4542 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4543 before the type name, and set *EXPR_CONST_OPERANDS, if
4544 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4545 appear in a constant expression. */
4548 groktypename (struct c_type_name
*type_name
, tree
*expr
,
4549 bool *expr_const_operands
)
4552 tree attrs
= type_name
->specs
->attrs
;
4554 type_name
->specs
->attrs
= NULL_TREE
;
4556 type
= grokdeclarator (type_name
->declarator
, type_name
->specs
, TYPENAME
,
4557 false, NULL
, &attrs
, expr
, expr_const_operands
,
4560 /* Apply attributes. */
4561 decl_attributes (&type
, attrs
, 0);
4566 /* Wrapper for decl_attributes that adds some implicit attributes
4567 to VAR_DECLs or FUNCTION_DECLs. */
4570 c_decl_attributes (tree
*node
, tree attributes
, int flags
)
4572 /* Add implicit "omp declare target" attribute if requested. */
4573 if (current_omp_declare_target_attribute
4574 && ((VAR_P (*node
) && is_global_var (*node
))
4575 || TREE_CODE (*node
) == FUNCTION_DECL
))
4578 && !lang_hooks
.types
.omp_mappable_type (TREE_TYPE (*node
)))
4579 error ("%q+D in declare target directive does not have mappable type",
4582 attributes
= tree_cons (get_identifier ("omp declare target"),
4583 NULL_TREE
, attributes
);
4585 return decl_attributes (node
, attributes
, flags
);
4589 /* Decode a declarator in an ordinary declaration or data definition.
4590 This is called as soon as the type information and variable name
4591 have been parsed, before parsing the initializer if any.
4592 Here we create the ..._DECL node, fill in its type,
4593 and put it on the list of decls for the current context.
4594 The ..._DECL node is returned as the value.
4596 Exception: for arrays where the length is not specified,
4597 the type is left null, to be filled in by `finish_decl'.
4599 Function definitions do not come here; they go to start_function
4600 instead. However, external and forward declarations of functions
4601 do go through here. Structure field declarations are done by
4602 grokfield and not through here. */
4605 start_decl (struct c_declarator
*declarator
, struct c_declspecs
*declspecs
,
4606 bool initialized
, tree attributes
)
4610 tree expr
= NULL_TREE
;
4611 enum deprecated_states deprecated_state
= DEPRECATED_NORMAL
;
4613 /* An object declared as __attribute__((deprecated)) suppresses
4614 warnings of uses of other deprecated items. */
4615 if (lookup_attribute ("deprecated", attributes
))
4616 deprecated_state
= DEPRECATED_SUPPRESS
;
4618 decl
= grokdeclarator (declarator
, declspecs
,
4619 NORMAL
, initialized
, NULL
, &attributes
, &expr
, NULL
,
4621 if (!decl
|| decl
== error_mark_node
)
4625 add_stmt (fold_convert (void_type_node
, expr
));
4627 if (TREE_CODE (decl
) != FUNCTION_DECL
&& MAIN_NAME_P (DECL_NAME (decl
)))
4628 warning (OPT_Wmain
, "%q+D is usually a function", decl
);
4631 /* Is it valid for this decl to have an initializer at all?
4632 If not, set INITIALIZED to zero, which will indirectly
4633 tell 'finish_decl' to ignore the initializer once it is parsed. */
4634 switch (TREE_CODE (decl
))
4637 error ("typedef %qD is initialized (use __typeof__ instead)", decl
);
4642 error ("function %qD is initialized like a variable", decl
);
4647 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4648 error ("parameter %qD is initialized", decl
);
4653 /* Don't allow initializations for incomplete types except for
4654 arrays which might be completed by the initialization. */
4656 /* This can happen if the array size is an undefined macro.
4657 We already gave a warning, so we don't need another one. */
4658 if (TREE_TYPE (decl
) == error_mark_node
)
4660 else if (COMPLETE_TYPE_P (TREE_TYPE (decl
)))
4662 /* A complete type is ok if size is fixed. */
4664 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl
))) != INTEGER_CST
4665 || C_DECL_VARIABLE_SIZE (decl
))
4667 error ("variable-sized object may not be initialized");
4671 else if (TREE_CODE (TREE_TYPE (decl
)) != ARRAY_TYPE
)
4673 error ("variable %qD has initializer but incomplete type", decl
);
4676 else if (C_DECL_VARIABLE_SIZE (decl
))
4678 /* Although C99 is unclear about whether incomplete arrays
4679 of VLAs themselves count as VLAs, it does not make
4680 sense to permit them to be initialized given that
4681 ordinary VLAs may not be initialized. */
4682 error ("variable-sized object may not be initialized");
4689 if (current_scope
== file_scope
)
4690 TREE_STATIC (decl
) = 1;
4692 /* Tell 'pushdecl' this is an initialized decl
4693 even though we don't yet have the initializer expression.
4694 Also tell 'finish_decl' it may store the real initializer. */
4695 DECL_INITIAL (decl
) = error_mark_node
;
4698 /* If this is a function declaration, write a record describing it to the
4699 prototypes file (if requested). */
4701 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4702 gen_aux_info_record (decl
, 0, 0, prototype_p (TREE_TYPE (decl
)));
4704 /* ANSI specifies that a tentative definition which is not merged with
4705 a non-tentative definition behaves exactly like a definition with an
4706 initializer equal to zero. (Section 3.7.2)
4708 -fno-common gives strict ANSI behavior, though this tends to break
4709 a large body of code that grew up without this rule.
4711 Thread-local variables are never common, since there's no entrenched
4712 body of code to break, and it allows more efficient variable references
4713 in the presence of dynamic linking. */
4717 && TREE_PUBLIC (decl
)
4718 && !DECL_THREAD_LOCAL_P (decl
)
4720 DECL_COMMON (decl
) = 1;
4722 /* Set attributes here so if duplicate decl, will have proper attributes. */
4723 c_decl_attributes (&decl
, attributes
, 0);
4725 /* Handle gnu_inline attribute. */
4726 if (declspecs
->inline_p
4727 && !flag_gnu89_inline
4728 && TREE_CODE (decl
) == FUNCTION_DECL
4729 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl
))
4730 || current_function_decl
))
4732 if (declspecs
->storage_class
== csc_auto
&& current_scope
!= file_scope
)
4734 else if (declspecs
->storage_class
!= csc_static
)
4735 DECL_EXTERNAL (decl
) = !DECL_EXTERNAL (decl
);
4738 if (TREE_CODE (decl
) == FUNCTION_DECL
4739 && targetm
.calls
.promote_prototypes (TREE_TYPE (decl
)))
4741 struct c_declarator
*ce
= declarator
;
4743 if (ce
->kind
== cdk_pointer
)
4744 ce
= declarator
->declarator
;
4745 if (ce
->kind
== cdk_function
)
4747 tree args
= ce
->u
.arg_info
->parms
;
4748 for (; args
; args
= DECL_CHAIN (args
))
4750 tree type
= TREE_TYPE (args
);
4751 if (type
&& INTEGRAL_TYPE_P (type
)
4752 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
4753 DECL_ARG_TYPE (args
) = c_type_promotes_to (type
);
4758 if (TREE_CODE (decl
) == FUNCTION_DECL
4759 && DECL_DECLARED_INLINE_P (decl
)
4760 && DECL_UNINLINABLE (decl
)
4761 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl
)))
4762 warning (OPT_Wattributes
, "inline function %q+D given attribute noinline",
4765 /* C99 6.7.4p3: An inline definition of a function with external
4766 linkage shall not contain a definition of a modifiable object
4767 with static storage duration... */
4769 && current_scope
!= file_scope
4770 && TREE_STATIC (decl
)
4771 && !TREE_READONLY (decl
)
4772 && DECL_DECLARED_INLINE_P (current_function_decl
)
4773 && DECL_EXTERNAL (current_function_decl
))
4774 record_inline_static (input_location
, current_function_decl
,
4775 decl
, csi_modifiable
);
4777 if (c_dialect_objc ()
4778 && VAR_OR_FUNCTION_DECL_P (decl
))
4779 objc_check_global_decl (decl
);
4781 /* Add this decl to the current scope.
4782 TEM may equal DECL or it may be a previous decl of the same name. */
4783 tem
= pushdecl (decl
);
4785 if (initialized
&& DECL_EXTERNAL (tem
))
4787 DECL_EXTERNAL (tem
) = 0;
4788 TREE_STATIC (tem
) = 1;
4794 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4795 DECL or the non-array element type if DECL is an uninitialized array.
4796 If that type has a const member, diagnose this. */
4799 diagnose_uninitialized_cst_member (tree decl
, tree type
)
4802 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
4805 if (TREE_CODE (field
) != FIELD_DECL
)
4807 field_type
= strip_array_types (TREE_TYPE (field
));
4809 if (TYPE_QUALS (field_type
) & TYPE_QUAL_CONST
)
4811 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
4812 "uninitialized const member in %qT is invalid in C++",
4813 strip_array_types (TREE_TYPE (decl
)));
4814 inform (DECL_SOURCE_LOCATION (field
), "%qD should be initialized", field
);
4817 if (RECORD_OR_UNION_TYPE_P (field_type
))
4818 diagnose_uninitialized_cst_member (decl
, field_type
);
4822 /* Finish processing of a declaration;
4823 install its initial value.
4824 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4825 If the length of an array type is not known before,
4826 it must be determined now, from the initial value, or it is an error.
4828 INIT_LOC is the location of the initial value. */
4831 finish_decl (tree decl
, location_t init_loc
, tree init
,
4832 tree origtype
, tree asmspec_tree
)
4835 bool was_incomplete
= (DECL_SIZE (decl
) == 0);
4836 const char *asmspec
= 0;
4838 /* If a name was specified, get the string. */
4839 if (VAR_OR_FUNCTION_DECL_P (decl
)
4840 && DECL_FILE_SCOPE_P (decl
))
4841 asmspec_tree
= maybe_apply_renaming_pragma (decl
, asmspec_tree
);
4843 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
4846 && TREE_STATIC (decl
)
4847 && global_bindings_p ())
4848 /* So decl is a global variable. Record the types it uses
4849 so that we can decide later to emit debug info for them. */
4850 record_types_used_by_current_var_decl (decl
);
4852 /* If `start_decl' didn't like having an initialization, ignore it now. */
4853 if (init
!= 0 && DECL_INITIAL (decl
) == 0)
4856 /* Don't crash if parm is initialized. */
4857 if (TREE_CODE (decl
) == PARM_DECL
)
4861 store_init_value (init_loc
, decl
, init
, origtype
);
4863 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl
)
4864 || TREE_CODE (decl
) == FIELD_DECL
))
4865 objc_check_decl (decl
);
4867 type
= TREE_TYPE (decl
);
4869 /* Deduce size of array from initialization, if not already known. */
4870 if (TREE_CODE (type
) == ARRAY_TYPE
4871 && TYPE_DOMAIN (type
) == 0
4872 && TREE_CODE (decl
) != TYPE_DECL
)
4875 = (TREE_STATIC (decl
)
4876 /* Even if pedantic, an external linkage array
4877 may have incomplete type at first. */
4878 ? pedantic
&& !TREE_PUBLIC (decl
)
4879 : !DECL_EXTERNAL (decl
));
4881 = complete_array_type (&TREE_TYPE (decl
), DECL_INITIAL (decl
),
4884 /* Get the completed type made by complete_array_type. */
4885 type
= TREE_TYPE (decl
);
4890 error ("initializer fails to determine size of %q+D", decl
);
4895 error ("array size missing in %q+D", decl
);
4896 /* If a `static' var's size isn't known,
4897 make it extern as well as static, so it does not get
4899 If it is not `static', then do not mark extern;
4900 finish_incomplete_decl will give it a default size
4901 and it will get allocated. */
4902 else if (!pedantic
&& TREE_STATIC (decl
) && !TREE_PUBLIC (decl
))
4903 DECL_EXTERNAL (decl
) = 1;
4907 error ("zero or negative size array %q+D", decl
);
4911 /* For global variables, update the copy of the type that
4912 exists in the binding. */
4913 if (TREE_PUBLIC (decl
))
4915 struct c_binding
*b_ext
= I_SYMBOL_BINDING (DECL_NAME (decl
));
4916 while (b_ext
&& !B_IN_EXTERNAL_SCOPE (b_ext
))
4917 b_ext
= b_ext
->shadowed
;
4918 if (b_ext
&& TREE_CODE (decl
) == TREE_CODE (b_ext
->decl
))
4920 if (b_ext
->u
.type
&& comptypes (b_ext
->u
.type
, type
))
4921 b_ext
->u
.type
= composite_type (b_ext
->u
.type
, type
);
4923 b_ext
->u
.type
= type
;
4932 if (DECL_INITIAL (decl
))
4933 TREE_TYPE (DECL_INITIAL (decl
)) = type
;
4935 relayout_decl (decl
);
4940 if (init
&& TREE_CODE (init
) == CONSTRUCTOR
)
4941 add_flexible_array_elts_to_size (decl
, init
);
4943 if (DECL_SIZE (decl
) == 0 && TREE_TYPE (decl
) != error_mark_node
4944 && COMPLETE_TYPE_P (TREE_TYPE (decl
)))
4945 layout_decl (decl
, 0);
4947 if (DECL_SIZE (decl
) == 0
4948 /* Don't give an error if we already gave one earlier. */
4949 && TREE_TYPE (decl
) != error_mark_node
4950 && (TREE_STATIC (decl
)
4951 /* A static variable with an incomplete type
4952 is an error if it is initialized.
4953 Also if it is not file scope.
4954 Otherwise, let it through, but if it is not `extern'
4955 then it may cause an error message later. */
4956 ? (DECL_INITIAL (decl
) != 0
4957 || !DECL_FILE_SCOPE_P (decl
))
4958 /* An automatic variable with an incomplete type
4960 : !DECL_EXTERNAL (decl
)))
4962 error ("storage size of %q+D isn%'t known", decl
);
4963 TREE_TYPE (decl
) = error_mark_node
;
4966 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
))
4967 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
4968 && DECL_SIZE (decl
) == NULL_TREE
4969 && TREE_STATIC (decl
))
4970 incomplete_record_decls
.safe_push (decl
);
4972 if (is_global_var (decl
) && DECL_SIZE (decl
) != 0)
4974 if (TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
)
4975 constant_expression_warning (DECL_SIZE (decl
));
4978 error ("storage size of %q+D isn%'t constant", decl
);
4979 TREE_TYPE (decl
) = error_mark_node
;
4983 if (TREE_USED (type
))
4985 TREE_USED (decl
) = 1;
4986 DECL_READ_P (decl
) = 1;
4990 /* If this is a function and an assembler name is specified, reset DECL_RTL
4991 so we can give it its new name. Also, update builtin_decl if it
4992 was a normal built-in. */
4993 if (TREE_CODE (decl
) == FUNCTION_DECL
&& asmspec
)
4995 if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
4996 set_builtin_user_assembler_name (decl
, asmspec
);
4997 set_user_assembler_name (decl
, asmspec
);
5000 /* If #pragma weak was used, mark the decl weak now. */
5001 maybe_apply_pragma_weak (decl
);
5003 /* Output the assembler code and/or RTL code for variables and functions,
5004 unless the type is an undefined structure or union.
5005 If not, it will get done when the type is completed. */
5007 if (VAR_OR_FUNCTION_DECL_P (decl
))
5009 /* Determine the ELF visibility. */
5010 if (TREE_PUBLIC (decl
))
5011 c_determine_visibility (decl
);
5013 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5014 if (c_dialect_objc ())
5015 objc_check_decl (decl
);
5019 /* If this is not a static variable, issue a warning.
5020 It doesn't make any sense to give an ASMSPEC for an
5021 ordinary, non-register local variable. Historically,
5022 GCC has accepted -- but ignored -- the ASMSPEC in
5024 if (!DECL_FILE_SCOPE_P (decl
)
5026 && !C_DECL_REGISTER (decl
)
5027 && !TREE_STATIC (decl
))
5028 warning (0, "ignoring asm-specifier for non-static local "
5029 "variable %q+D", decl
);
5031 set_user_assembler_name (decl
, asmspec
);
5034 if (DECL_FILE_SCOPE_P (decl
))
5036 if (DECL_INITIAL (decl
) == NULL_TREE
5037 || DECL_INITIAL (decl
) == error_mark_node
)
5038 /* Don't output anything
5039 when a tentative file-scope definition is seen.
5040 But at end of compilation, do output code for them. */
5041 DECL_DEFER_OUTPUT (decl
) = 1;
5042 if (asmspec
&& C_DECL_REGISTER (decl
))
5043 DECL_HARD_REGISTER (decl
) = 1;
5044 rest_of_decl_compilation (decl
, true, 0);
5048 /* In conjunction with an ASMSPEC, the `register'
5049 keyword indicates that we should place the variable
5050 in a particular register. */
5051 if (asmspec
&& C_DECL_REGISTER (decl
))
5053 DECL_HARD_REGISTER (decl
) = 1;
5054 /* This cannot be done for a structure with volatile
5055 fields, on which DECL_REGISTER will have been
5057 if (!DECL_REGISTER (decl
))
5058 error ("cannot put object with volatile field into register");
5061 if (TREE_CODE (decl
) != FUNCTION_DECL
)
5063 /* If we're building a variable sized type, and we might be
5064 reachable other than via the top of the current binding
5065 level, then create a new BIND_EXPR so that we deallocate
5066 the object at the right time. */
5067 /* Note that DECL_SIZE can be null due to errors. */
5068 if (DECL_SIZE (decl
)
5069 && !TREE_CONSTANT (DECL_SIZE (decl
))
5070 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list
))
5073 bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, NULL
);
5074 TREE_SIDE_EFFECTS (bind
) = 1;
5076 BIND_EXPR_BODY (bind
) = push_stmt_list ();
5078 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl
),
5084 if (!DECL_FILE_SCOPE_P (decl
))
5086 /* Recompute the RTL of a local array now
5087 if it used to be an incomplete type. */
5088 if (was_incomplete
&& !is_global_var (decl
))
5090 /* If we used it already as memory, it must stay in memory. */
5091 TREE_ADDRESSABLE (decl
) = TREE_USED (decl
);
5092 /* If it's still incomplete now, no init will save it. */
5093 if (DECL_SIZE (decl
) == 0)
5094 DECL_INITIAL (decl
) = 0;
5099 if (TREE_CODE (decl
) == TYPE_DECL
)
5101 if (!DECL_FILE_SCOPE_P (decl
)
5102 && variably_modified_type_p (TREE_TYPE (decl
), NULL_TREE
))
5103 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl
), DECL_EXPR
, decl
));
5105 rest_of_decl_compilation (decl
, DECL_FILE_SCOPE_P (decl
), 0);
5108 /* Install a cleanup (aka destructor) if one was given. */
5109 if (VAR_P (decl
) && !TREE_STATIC (decl
))
5111 tree attr
= lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl
));
5114 tree cleanup_id
= TREE_VALUE (TREE_VALUE (attr
));
5115 tree cleanup_decl
= lookup_name (cleanup_id
);
5117 vec
<tree
, va_gc
> *v
;
5119 /* Build "cleanup(&decl)" for the destructor. */
5120 cleanup
= build_unary_op (input_location
, ADDR_EXPR
, decl
, 0);
5122 v
->quick_push (cleanup
);
5123 cleanup
= c_build_function_call_vec (DECL_SOURCE_LOCATION (decl
),
5124 vNULL
, cleanup_decl
, v
, NULL
);
5127 /* Don't warn about decl unused; the cleanup uses it. */
5128 TREE_USED (decl
) = 1;
5129 TREE_USED (cleanup_decl
) = 1;
5130 DECL_READ_P (decl
) = 1;
5132 push_cleanup (decl
, cleanup
, false);
5138 && !DECL_EXTERNAL (decl
)
5139 && DECL_INITIAL (decl
) == NULL_TREE
)
5141 type
= strip_array_types (type
);
5142 if (TREE_READONLY (decl
))
5143 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
5144 "uninitialized const %qD is invalid in C++", decl
);
5145 else if (RECORD_OR_UNION_TYPE_P (type
)
5146 && C_TYPE_FIELDS_READONLY (type
))
5147 diagnose_uninitialized_cst_member (decl
, type
);
5150 invoke_plugin_callbacks (PLUGIN_FINISH_DECL
, decl
);
5153 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5154 EXPR is NULL or a pointer to an expression that needs to be
5155 evaluated for the side effects of array size expressions in the
5159 grokparm (const struct c_parm
*parm
, tree
*expr
)
5161 tree attrs
= parm
->attrs
;
5162 tree decl
= grokdeclarator (parm
->declarator
, parm
->specs
, PARM
, false,
5163 NULL
, &attrs
, expr
, NULL
, DEPRECATED_NORMAL
);
5165 decl_attributes (&decl
, attrs
, 0);
5170 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5171 and push that on the current scope. EXPR is a pointer to an
5172 expression that needs to be evaluated for the side effects of array
5173 size expressions in the parameters. */
5176 push_parm_decl (const struct c_parm
*parm
, tree
*expr
)
5178 tree attrs
= parm
->attrs
;
5181 decl
= grokdeclarator (parm
->declarator
, parm
->specs
, PARM
, false, NULL
,
5182 &attrs
, expr
, NULL
, DEPRECATED_NORMAL
);
5183 decl_attributes (&decl
, attrs
, 0);
5185 decl
= pushdecl (decl
);
5187 finish_decl (decl
, input_location
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
5190 /* Mark all the parameter declarations to date as forward decls.
5191 Also diagnose use of this extension. */
5194 mark_forward_parm_decls (void)
5196 struct c_binding
*b
;
5198 if (pedantic
&& !current_scope
->warned_forward_parm_decls
)
5200 pedwarn (input_location
, OPT_Wpedantic
,
5201 "ISO C forbids forward parameter declarations");
5202 current_scope
->warned_forward_parm_decls
= true;
5205 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
5206 if (TREE_CODE (b
->decl
) == PARM_DECL
)
5207 TREE_ASM_WRITTEN (b
->decl
) = 1;
5210 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5211 literal, which may be an incomplete array type completed by the
5212 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5213 literal. NON_CONST is true if the initializers contain something
5214 that cannot occur in a constant expression. */
5217 build_compound_literal (location_t loc
, tree type
, tree init
, bool non_const
)
5219 /* We do not use start_decl here because we have a type, not a declarator;
5220 and do not use finish_decl because the decl should be stored inside
5221 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5226 if (type
== error_mark_node
5227 || init
== error_mark_node
)
5228 return error_mark_node
;
5230 decl
= build_decl (loc
, VAR_DECL
, NULL_TREE
, type
);
5231 DECL_EXTERNAL (decl
) = 0;
5232 TREE_PUBLIC (decl
) = 0;
5233 TREE_STATIC (decl
) = (current_scope
== file_scope
);
5234 DECL_CONTEXT (decl
) = current_function_decl
;
5235 TREE_USED (decl
) = 1;
5236 DECL_READ_P (decl
) = 1;
5237 TREE_TYPE (decl
) = type
;
5238 TREE_READONLY (decl
) = (TYPE_READONLY (type
)
5239 || (TREE_CODE (type
) == ARRAY_TYPE
5240 && TYPE_READONLY (TREE_TYPE (type
))));
5241 store_init_value (loc
, decl
, init
, NULL_TREE
);
5243 if (TREE_CODE (type
) == ARRAY_TYPE
&& !COMPLETE_TYPE_P (type
))
5245 int failure
= complete_array_type (&TREE_TYPE (decl
),
5246 DECL_INITIAL (decl
), true);
5247 /* If complete_array_type returns 3, it means that the
5248 initial value of the compound literal is empty. Allow it. */
5249 gcc_assert (failure
== 0 || failure
== 3);
5251 type
= TREE_TYPE (decl
);
5252 TREE_TYPE (DECL_INITIAL (decl
)) = type
;
5255 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
5257 c_incomplete_type_error (loc
, NULL_TREE
, type
);
5258 return error_mark_node
;
5261 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), DECL_EXPR
, decl
);
5262 complit
= build1 (COMPOUND_LITERAL_EXPR
, type
, stmt
);
5263 TREE_SIDE_EFFECTS (complit
) = 1;
5265 layout_decl (decl
, 0);
5267 if (TREE_STATIC (decl
))
5269 /* This decl needs a name for the assembler output. */
5270 set_compound_literal_name (decl
);
5271 DECL_DEFER_OUTPUT (decl
) = 1;
5272 DECL_COMDAT (decl
) = 1;
5273 DECL_ARTIFICIAL (decl
) = 1;
5274 DECL_IGNORED_P (decl
) = 1;
5276 rest_of_decl_compilation (decl
, 1, 0);
5281 complit
= build2 (C_MAYBE_CONST_EXPR
, type
, NULL
, complit
);
5282 C_MAYBE_CONST_EXPR_NON_CONST (complit
) = 1;
5288 /* Check the type of a compound literal. Here we just check that it
5289 is valid for C++. */
5292 check_compound_literal_type (location_t loc
, struct c_type_name
*type_name
)
5295 && (type_name
->specs
->typespec_kind
== ctsk_tagdef
5296 || type_name
->specs
->typespec_kind
== ctsk_tagfirstref
))
5297 warning_at (loc
, OPT_Wc___compat
,
5298 "defining a type in a compound literal is invalid in C++");
5301 /* Determine whether TYPE is a structure with a flexible array member,
5302 or a union containing such a structure (possibly recursively). */
5305 flexible_array_type_p (tree type
)
5308 switch (TREE_CODE (type
))
5311 x
= TYPE_FIELDS (type
);
5314 while (DECL_CHAIN (x
) != NULL_TREE
)
5316 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
5317 && TYPE_SIZE (TREE_TYPE (x
)) == NULL_TREE
5318 && TYPE_DOMAIN (TREE_TYPE (x
)) != NULL_TREE
5319 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x
))) == NULL_TREE
)
5323 for (x
= TYPE_FIELDS (type
); x
!= NULL_TREE
; x
= DECL_CHAIN (x
))
5325 if (flexible_array_type_p (TREE_TYPE (x
)))
5334 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5335 replacing with appropriate values if they are invalid. */
5338 check_bitfield_type_and_width (location_t loc
, tree
*type
, tree
*width
,
5342 unsigned int max_width
;
5343 unsigned HOST_WIDE_INT w
;
5344 const char *name
= (orig_name
5345 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name
))
5346 : _("<anonymous>"));
5348 /* Detect and ignore out of range field width and process valid
5350 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width
)))
5352 error_at (loc
, "bit-field %qs width not an integer constant", name
);
5353 *width
= integer_one_node
;
5357 if (TREE_CODE (*width
) != INTEGER_CST
)
5359 *width
= c_fully_fold (*width
, false, NULL
);
5360 if (TREE_CODE (*width
) == INTEGER_CST
)
5361 pedwarn (loc
, OPT_Wpedantic
,
5362 "bit-field %qs width not an integer constant expression",
5365 if (TREE_CODE (*width
) != INTEGER_CST
)
5367 error_at (loc
, "bit-field %qs width not an integer constant", name
);
5368 *width
= integer_one_node
;
5370 constant_expression_warning (*width
);
5371 if (tree_int_cst_sgn (*width
) < 0)
5373 error_at (loc
, "negative width in bit-field %qs", name
);
5374 *width
= integer_one_node
;
5376 else if (integer_zerop (*width
) && orig_name
)
5378 error_at (loc
, "zero width for bit-field %qs", name
);
5379 *width
= integer_one_node
;
5383 /* Detect invalid bit-field type. */
5384 if (TREE_CODE (*type
) != INTEGER_TYPE
5385 && TREE_CODE (*type
) != BOOLEAN_TYPE
5386 && TREE_CODE (*type
) != ENUMERAL_TYPE
)
5388 error_at (loc
, "bit-field %qs has invalid type", name
);
5389 *type
= unsigned_type_node
;
5392 type_mv
= TYPE_MAIN_VARIANT (*type
);
5393 if (!in_system_header_at (input_location
)
5394 && type_mv
!= integer_type_node
5395 && type_mv
!= unsigned_type_node
5396 && type_mv
!= boolean_type_node
)
5397 pedwarn_c90 (loc
, OPT_Wpedantic
,
5398 "type of bit-field %qs is a GCC extension", name
);
5400 max_width
= TYPE_PRECISION (*type
);
5402 if (0 < compare_tree_int (*width
, max_width
))
5404 error_at (loc
, "width of %qs exceeds its type", name
);
5406 *width
= build_int_cst (integer_type_node
, w
);
5409 w
= tree_to_uhwi (*width
);
5411 if (TREE_CODE (*type
) == ENUMERAL_TYPE
)
5413 struct lang_type
*lt
= TYPE_LANG_SPECIFIC (*type
);
5415 || w
< tree_int_cst_min_precision (lt
->enum_min
, TYPE_SIGN (*type
))
5416 || w
< tree_int_cst_min_precision (lt
->enum_max
, TYPE_SIGN (*type
)))
5417 warning_at (loc
, 0, "%qs is narrower than values of its type", name
);
5423 /* Print warning about variable length array if necessary. */
5426 warn_variable_length_array (tree name
, tree size
)
5428 if (TREE_CONSTANT (size
))
5431 pedwarn_c90 (input_location
, OPT_Wvla
,
5432 "ISO C90 forbids array %qE whose size "
5433 "can%'t be evaluated", name
);
5435 pedwarn_c90 (input_location
, OPT_Wvla
, "ISO C90 forbids array "
5436 "whose size can%'t be evaluated");
5441 pedwarn_c90 (input_location
, OPT_Wvla
,
5442 "ISO C90 forbids variable length array %qE", name
);
5444 pedwarn_c90 (input_location
, OPT_Wvla
, "ISO C90 forbids variable "
5449 /* Print warning about defaulting to int if necessary. */
5452 warn_defaults_to (location_t location
, int opt
, const char *gmsgid
, ...)
5454 diagnostic_info diagnostic
;
5456 rich_location
richloc (line_table
, location
);
5458 va_start (ap
, gmsgid
);
5459 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
,
5460 flag_isoc99
? DK_PEDWARN
: DK_WARNING
);
5461 diagnostic
.option_index
= opt
;
5462 report_diagnostic (&diagnostic
);
5466 /* Given declspecs and a declarator,
5467 determine the name and type of the object declared
5468 and construct a ..._DECL node for it.
5469 (In one case we can return a ..._TYPE node instead.
5470 For invalid input we sometimes return 0.)
5472 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5474 DECL_CONTEXT says which syntactic context this declaration is in:
5475 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5476 FUNCDEF for a function definition. Like NORMAL but a few different
5477 error messages in each case. Return value may be zero meaning
5478 this definition is too screwy to try to parse.
5479 PARM for a parameter declaration (either within a function prototype
5480 or before a function body). Make a PARM_DECL, or return void_type_node.
5481 TYPENAME if for a typename (in a cast or sizeof).
5482 Don't make a DECL node; just return the ..._TYPE node.
5483 FIELD for a struct or union field; make a FIELD_DECL.
5484 INITIALIZED is true if the decl has an initializer.
5485 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5486 representing the width of the bit-field.
5487 DECL_ATTRS points to the list of attributes that should be added to this
5488 decl. Any nested attributes that belong on the decl itself will be
5490 If EXPR is not NULL, any expressions that need to be evaluated as
5491 part of evaluating variably modified types will be stored in *EXPR.
5492 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5493 set to indicate whether operands in *EXPR can be used in constant
5495 DEPRECATED_STATE is a deprecated_states value indicating whether
5496 deprecation warnings should be suppressed.
5498 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5499 It may also be so in the PARM case, for a prototype where the
5500 argument type is specified but not the name.
5502 This function is where the complicated C meanings of `static'
5503 and `extern' are interpreted. */
5506 grokdeclarator (const struct c_declarator
*declarator
,
5507 struct c_declspecs
*declspecs
,
5508 enum decl_context decl_context
, bool initialized
, tree
*width
,
5509 tree
*decl_attrs
, tree
*expr
, bool *expr_const_operands
,
5510 enum deprecated_states deprecated_state
)
5512 tree type
= declspecs
->type
;
5513 bool threadp
= declspecs
->thread_p
;
5514 enum c_storage_class storage_class
= declspecs
->storage_class
;
5519 int type_quals
= TYPE_UNQUALIFIED
;
5520 tree name
= NULL_TREE
;
5521 bool funcdef_flag
= false;
5522 bool funcdef_syntax
= false;
5523 bool size_varies
= false;
5524 tree decl_attr
= declspecs
->decl_attr
;
5525 int array_ptr_quals
= TYPE_UNQUALIFIED
;
5526 tree array_ptr_attrs
= NULL_TREE
;
5527 int array_parm_static
= 0;
5528 bool array_parm_vla_unspec_p
= false;
5529 tree returned_attrs
= NULL_TREE
;
5530 bool bitfield
= width
!= NULL
;
5532 tree orig_qual_type
= NULL
;
5533 size_t orig_qual_indirect
= 0;
5534 struct c_arg_info
*arg_info
= 0;
5535 addr_space_t as1
, as2
, address_space
;
5536 location_t loc
= UNKNOWN_LOCATION
;
5538 bool expr_const_operands_dummy
;
5539 enum c_declarator_kind first_non_attr_kind
;
5540 unsigned int alignas_align
= 0;
5542 if (TREE_CODE (type
) == ERROR_MARK
)
5543 return error_mark_node
;
5546 if (expr_const_operands
== NULL
)
5547 expr_const_operands
= &expr_const_operands_dummy
;
5549 *expr
= declspecs
->expr
;
5550 *expr_const_operands
= declspecs
->expr_const_operands
;
5552 if (decl_context
== FUNCDEF
)
5553 funcdef_flag
= true, decl_context
= NORMAL
;
5555 /* Look inside a declarator for the name being declared
5556 and get it as an IDENTIFIER_NODE, for an error message. */
5558 const struct c_declarator
*decl
= declarator
;
5560 first_non_attr_kind
= cdk_attrs
;
5570 funcdef_syntax
= (decl
->kind
== cdk_function
);
5571 if (first_non_attr_kind
== cdk_attrs
)
5572 first_non_attr_kind
= decl
->kind
;
5573 decl
= decl
->declarator
;
5577 decl
= decl
->declarator
;
5584 if (first_non_attr_kind
== cdk_attrs
)
5585 first_non_attr_kind
= decl
->kind
;
5594 gcc_assert (decl_context
== PARM
5595 || decl_context
== TYPENAME
5596 || (decl_context
== FIELD
5597 && declarator
->kind
== cdk_id
));
5598 gcc_assert (!initialized
);
5602 /* A function definition's declarator must have the form of
5603 a function declarator. */
5605 if (funcdef_flag
&& !funcdef_syntax
)
5608 /* If this looks like a function definition, make it one,
5609 even if it occurs where parms are expected.
5610 Then store_parm_decls will reject it and not use it as a parm. */
5611 if (decl_context
== NORMAL
&& !funcdef_flag
&& current_scope
->parm_flag
)
5612 decl_context
= PARM
;
5614 if (declspecs
->deprecated_p
&& deprecated_state
!= DEPRECATED_SUPPRESS
)
5615 warn_deprecated_use (declspecs
->type
, declspecs
->decl_attr
);
5617 if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
5618 && current_scope
== file_scope
5619 && variably_modified_type_p (type
, NULL_TREE
))
5622 error_at (loc
, "variably modified %qE at file scope", name
);
5624 error_at (loc
, "variably modified field at file scope");
5625 type
= integer_type_node
;
5628 size_varies
= C_TYPE_VARIABLE_SIZE (type
) != 0;
5630 /* Diagnose defaulting to "int". */
5632 if (declspecs
->default_int_p
&& !in_system_header_at (input_location
))
5634 /* Issue a warning if this is an ISO C 99 program or if
5635 -Wreturn-type and this is a function, or if -Wimplicit;
5636 prefer the former warning since it is more explicit. */
5637 if ((warn_implicit_int
|| warn_return_type
|| flag_isoc99
)
5639 warn_about_return_type
= 1;
5643 warn_defaults_to (loc
, OPT_Wimplicit_int
,
5644 "type defaults to %<int%> in declaration "
5647 warn_defaults_to (loc
, OPT_Wimplicit_int
,
5648 "type defaults to %<int%> in type name");
5652 /* Adjust the type if a bit-field is being declared,
5653 -funsigned-bitfields applied and the type is not explicitly
5655 if (bitfield
&& !flag_signed_bitfields
&& !declspecs
->explicit_signed_p
5656 && TREE_CODE (type
) == INTEGER_TYPE
)
5657 type
= unsigned_type_for (type
);
5659 /* Figure out the type qualifiers for the declaration. There are
5660 two ways a declaration can become qualified. One is something
5661 like `const int i' where the `const' is explicit. Another is
5662 something like `typedef const int CI; CI i' where the type of the
5663 declaration contains the `const'. A third possibility is that
5664 there is a type qualifier on the element type of a typedefed
5665 array type, in which case we should extract that qualifier so
5666 that c_apply_type_quals_to_decl receives the full list of
5667 qualifiers to work with (C90 is not entirely clear about whether
5668 duplicate qualifiers should be diagnosed in this case, but it
5669 seems most appropriate to do so). */
5670 element_type
= strip_array_types (type
);
5671 constp
= declspecs
->const_p
+ TYPE_READONLY (element_type
);
5672 restrictp
= declspecs
->restrict_p
+ TYPE_RESTRICT (element_type
);
5673 volatilep
= declspecs
->volatile_p
+ TYPE_VOLATILE (element_type
);
5674 atomicp
= declspecs
->atomic_p
+ TYPE_ATOMIC (element_type
);
5675 as1
= declspecs
->address_space
;
5676 as2
= TYPE_ADDR_SPACE (element_type
);
5677 address_space
= ADDR_SPACE_GENERIC_P (as1
)? as2
: as1
;
5680 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<const%>");
5682 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<restrict%>");
5684 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<volatile%>");
5686 pedwarn_c90 (loc
, OPT_Wpedantic
, "duplicate %<_Atomic%>");
5688 if (!ADDR_SPACE_GENERIC_P (as1
) && !ADDR_SPACE_GENERIC_P (as2
) && as1
!= as2
)
5689 error_at (loc
, "conflicting named address spaces (%s vs %s)",
5690 c_addr_space_name (as1
), c_addr_space_name (as2
));
5692 if ((TREE_CODE (type
) == ARRAY_TYPE
5693 || first_non_attr_kind
== cdk_array
)
5694 && TYPE_QUALS (element_type
))
5696 orig_qual_type
= type
;
5697 type
= TYPE_MAIN_VARIANT (type
);
5699 type_quals
= ((constp
? TYPE_QUAL_CONST
: 0)
5700 | (restrictp
? TYPE_QUAL_RESTRICT
: 0)
5701 | (volatilep
? TYPE_QUAL_VOLATILE
: 0)
5702 | (atomicp
? TYPE_QUAL_ATOMIC
: 0)
5703 | ENCODE_QUAL_ADDR_SPACE (address_space
));
5704 if (type_quals
!= TYPE_QUALS (element_type
))
5705 orig_qual_type
= NULL_TREE
;
5707 /* Applying the _Atomic qualifier to an array type (through the use
5708 of typedefs or typeof) must be detected here. If the qualifier
5709 is introduced later, any appearance of applying it to an array is
5710 actually applying it to an element of that array. */
5711 if (atomicp
&& TREE_CODE (type
) == ARRAY_TYPE
)
5712 error_at (loc
, "%<_Atomic%>-qualified array type");
5714 /* Warn about storage classes that are invalid for certain
5715 kinds of declarations (parameters, typenames, etc.). */
5719 || storage_class
== csc_auto
5720 || storage_class
== csc_register
5721 || storage_class
== csc_typedef
))
5723 if (storage_class
== csc_auto
)
5725 (current_scope
== file_scope
) ? 0 : OPT_Wpedantic
,
5726 "function definition declared %<auto%>");
5727 if (storage_class
== csc_register
)
5728 error_at (loc
, "function definition declared %<register%>");
5729 if (storage_class
== csc_typedef
)
5730 error_at (loc
, "function definition declared %<typedef%>");
5732 error_at (loc
, "function definition declared %qs",
5733 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
5735 if (storage_class
== csc_auto
5736 || storage_class
== csc_register
5737 || storage_class
== csc_typedef
)
5738 storage_class
= csc_none
;
5740 else if (decl_context
!= NORMAL
&& (storage_class
!= csc_none
|| threadp
))
5742 if (decl_context
== PARM
&& storage_class
== csc_register
)
5746 switch (decl_context
)
5750 error_at (loc
, "storage class specified for structure "
5753 error_at (loc
, "storage class specified for structure field");
5757 error_at (loc
, "storage class specified for parameter %qE",
5760 error_at (loc
, "storage class specified for unnamed parameter");
5763 error_at (loc
, "storage class specified for typename");
5766 storage_class
= csc_none
;
5770 else if (storage_class
== csc_extern
5774 /* 'extern' with initialization is invalid if not at file scope. */
5775 if (current_scope
== file_scope
)
5777 /* It is fine to have 'extern const' when compiling at C
5778 and C++ intersection. */
5779 if (!(warn_cxx_compat
&& constp
))
5780 warning_at (loc
, 0, "%qE initialized and declared %<extern%>",
5784 error_at (loc
, "%qE has both %<extern%> and initializer", name
);
5786 else if (current_scope
== file_scope
)
5788 if (storage_class
== csc_auto
)
5789 error_at (loc
, "file-scope declaration of %qE specifies %<auto%>",
5791 if (pedantic
&& storage_class
== csc_register
)
5792 pedwarn (input_location
, OPT_Wpedantic
,
5793 "file-scope declaration of %qE specifies %<register%>", name
);
5797 if (storage_class
== csc_extern
&& funcdef_flag
)
5798 error_at (loc
, "nested function %qE declared %<extern%>", name
);
5799 else if (threadp
&& storage_class
== csc_none
)
5801 error_at (loc
, "function-scope %qE implicitly auto and declared "
5803 declspecs
->thread_gnu_p
? "__thread" : "_Thread_local");
5808 /* Now figure out the structure of the declarator proper.
5809 Descend through it, creating more complex types, until we reach
5810 the declared identifier (or NULL_TREE, in an absolute declarator).
5811 At each stage we maintain an unqualified version of the type
5812 together with any qualifiers that should be applied to it with
5813 c_build_qualified_type; this way, array types including
5814 multidimensional array types are first built up in unqualified
5815 form and then the qualified form is created with
5816 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5818 while (declarator
&& declarator
->kind
!= cdk_id
)
5820 if (type
== error_mark_node
)
5822 declarator
= declarator
->declarator
;
5826 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5827 a cdk_pointer (for *...),
5828 a cdk_function (for ...(...)),
5829 a cdk_attrs (for nested attributes),
5830 or a cdk_id (for the name being declared
5831 or the place in an absolute declarator
5832 where the name was omitted).
5833 For the last case, we have just exited the loop.
5835 At this point, TYPE is the type of elements of an array,
5836 or for a function to return, or for a pointer to point to.
5837 After this sequence of ifs, TYPE is the type of the
5838 array or function or pointer, and DECLARATOR has had its
5839 outermost layer removed. */
5841 if (array_ptr_quals
!= TYPE_UNQUALIFIED
5842 || array_ptr_attrs
!= NULL_TREE
5843 || array_parm_static
)
5845 /* Only the innermost declarator (making a parameter be of
5846 array type which is converted to pointer type)
5847 may have static or type qualifiers. */
5848 error_at (loc
, "static or type qualifiers in non-parameter array declarator");
5849 array_ptr_quals
= TYPE_UNQUALIFIED
;
5850 array_ptr_attrs
= NULL_TREE
;
5851 array_parm_static
= 0;
5854 switch (declarator
->kind
)
5858 /* A declarator with embedded attributes. */
5859 tree attrs
= declarator
->u
.attrs
;
5860 const struct c_declarator
*inner_decl
;
5862 declarator
= declarator
->declarator
;
5863 inner_decl
= declarator
;
5864 while (inner_decl
->kind
== cdk_attrs
)
5865 inner_decl
= inner_decl
->declarator
;
5866 if (inner_decl
->kind
== cdk_id
)
5867 attr_flags
|= (int) ATTR_FLAG_DECL_NEXT
;
5868 else if (inner_decl
->kind
== cdk_function
)
5869 attr_flags
|= (int) ATTR_FLAG_FUNCTION_NEXT
;
5870 else if (inner_decl
->kind
== cdk_array
)
5871 attr_flags
|= (int) ATTR_FLAG_ARRAY_NEXT
;
5872 returned_attrs
= decl_attributes (&type
,
5873 chainon (returned_attrs
, attrs
),
5879 tree itype
= NULL_TREE
;
5880 tree size
= declarator
->u
.array
.dimen
;
5881 /* The index is a signed object `sizetype' bits wide. */
5882 tree index_type
= c_common_signed_type (sizetype
);
5884 array_ptr_quals
= declarator
->u
.array
.quals
;
5885 array_ptr_attrs
= declarator
->u
.array
.attrs
;
5886 array_parm_static
= declarator
->u
.array
.static_p
;
5887 array_parm_vla_unspec_p
= declarator
->u
.array
.vla_unspec_p
;
5889 declarator
= declarator
->declarator
;
5891 /* Check for some types that there cannot be arrays of. */
5893 if (VOID_TYPE_P (type
))
5896 error_at (loc
, "declaration of %qE as array of voids", name
);
5898 error_at (loc
, "declaration of type name as array of voids");
5899 type
= error_mark_node
;
5902 if (TREE_CODE (type
) == FUNCTION_TYPE
)
5905 error_at (loc
, "declaration of %qE as array of functions",
5908 error_at (loc
, "declaration of type name as array of "
5910 type
= error_mark_node
;
5913 if (pedantic
&& !in_system_header_at (input_location
)
5914 && flexible_array_type_p (type
))
5915 pedwarn (loc
, OPT_Wpedantic
,
5916 "invalid use of structure with flexible array member");
5918 if (size
== error_mark_node
)
5919 type
= error_mark_node
;
5921 if (type
== error_mark_node
)
5924 /* If size was specified, set ITYPE to a range-type for
5925 that size. Otherwise, ITYPE remains null. finish_decl
5926 may figure it out from an initial value. */
5930 bool size_maybe_const
= true;
5931 bool size_int_const
= (TREE_CODE (size
) == INTEGER_CST
5932 && !TREE_OVERFLOW (size
));
5933 bool this_size_varies
= false;
5935 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5937 STRIP_TYPE_NOPS (size
);
5939 if (!INTEGRAL_TYPE_P (TREE_TYPE (size
)))
5942 error_at (loc
, "size of array %qE has non-integer type",
5946 "size of unnamed array has non-integer type");
5947 size
= integer_one_node
;
5949 /* This can happen with enum forward declaration. */
5950 else if (!COMPLETE_TYPE_P (TREE_TYPE (size
)))
5953 error_at (loc
, "size of array %qE has incomplete type",
5956 error_at (loc
, "size of unnamed array has incomplete "
5958 size
= integer_one_node
;
5961 size
= c_fully_fold (size
, false, &size_maybe_const
);
5963 if (pedantic
&& size_maybe_const
&& integer_zerop (size
))
5966 pedwarn (loc
, OPT_Wpedantic
,
5967 "ISO C forbids zero-size array %qE", name
);
5969 pedwarn (loc
, OPT_Wpedantic
,
5970 "ISO C forbids zero-size array");
5973 if (TREE_CODE (size
) == INTEGER_CST
&& size_maybe_const
)
5975 constant_expression_warning (size
);
5976 if (tree_int_cst_sgn (size
) < 0)
5979 error_at (loc
, "size of array %qE is negative", name
);
5981 error_at (loc
, "size of unnamed array is negative");
5982 size
= integer_one_node
;
5984 /* Handle a size folded to an integer constant but
5985 not an integer constant expression. */
5986 if (!size_int_const
)
5988 /* If this is a file scope declaration of an
5989 ordinary identifier, this is invalid code;
5990 diagnosing it here and not subsequently
5991 treating the type as variable-length avoids
5992 more confusing diagnostics later. */
5993 if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
5994 && current_scope
== file_scope
)
5995 pedwarn (input_location
, 0,
5996 "variably modified %qE at file scope",
5999 this_size_varies
= size_varies
= true;
6000 warn_variable_length_array (name
, size
);
6003 else if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
6004 && current_scope
== file_scope
)
6006 error_at (loc
, "variably modified %qE at file scope", name
);
6007 size
= integer_one_node
;
6011 /* Make sure the array size remains visibly
6012 nonconstant even if it is (eg) a const variable
6013 with known value. */
6014 this_size_varies
= size_varies
= true;
6015 warn_variable_length_array (name
, size
);
6016 if (flag_sanitize
& SANITIZE_VLA
6017 && decl_context
== NORMAL
6018 && do_ubsan_in_current_function ())
6020 /* Evaluate the array size only once. */
6021 size
= c_save_expr (size
);
6022 size
= c_fully_fold (size
, false, NULL
);
6023 size
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
6024 ubsan_instrument_vla (loc
, size
),
6029 if (integer_zerop (size
) && !this_size_varies
)
6031 /* A zero-length array cannot be represented with
6032 an unsigned index type, which is what we'll
6033 get with build_index_type. Create an
6034 open-ended range instead. */
6035 itype
= build_range_type (sizetype
, size
, NULL_TREE
);
6039 /* Arrange for the SAVE_EXPR on the inside of the
6040 MINUS_EXPR, which allows the -1 to get folded
6041 with the +1 that happens when building TYPE_SIZE. */
6043 size
= save_expr (size
);
6044 if (this_size_varies
&& TREE_CODE (size
) == INTEGER_CST
)
6045 size
= build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
6046 integer_zero_node
, size
);
6048 /* Compute the maximum valid index, that is, size
6049 - 1. Do the calculation in index_type, so that
6050 if it is a variable the computations will be
6051 done in the proper mode. */
6052 itype
= fold_build2_loc (loc
, MINUS_EXPR
, index_type
,
6053 convert (index_type
, size
),
6054 convert (index_type
,
6057 /* The above overflows when size does not fit
6059 ??? While a size of INT_MAX+1 technically shouldn't
6060 cause an overflow (because we subtract 1), handling
6061 this case seems like an unnecessary complication. */
6062 if (TREE_CODE (size
) == INTEGER_CST
6063 && !int_fits_type_p (size
, index_type
))
6066 error_at (loc
, "size of array %qE is too large",
6069 error_at (loc
, "size of unnamed array is too large");
6070 type
= error_mark_node
;
6074 itype
= build_index_type (itype
);
6076 if (this_size_varies
)
6079 *expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
6083 *expr_const_operands
&= size_maybe_const
;
6086 else if (decl_context
== FIELD
)
6088 bool flexible_array_member
= false;
6089 if (array_parm_vla_unspec_p
)
6090 /* Field names can in fact have function prototype
6091 scope so [*] is disallowed here through making
6092 the field variably modified, not through being
6093 something other than a declaration with function
6098 const struct c_declarator
*t
= declarator
;
6099 while (t
->kind
== cdk_attrs
)
6101 flexible_array_member
= (t
->kind
== cdk_id
);
6103 if (flexible_array_member
6104 && !in_system_header_at (input_location
))
6105 pedwarn_c90 (loc
, OPT_Wpedantic
, "ISO C90 does not "
6106 "support flexible array members");
6108 /* ISO C99 Flexible array members are effectively
6109 identical to GCC's zero-length array extension. */
6110 if (flexible_array_member
|| array_parm_vla_unspec_p
)
6111 itype
= build_range_type (sizetype
, size_zero_node
,
6114 else if (decl_context
== PARM
)
6116 if (array_parm_vla_unspec_p
)
6118 itype
= build_range_type (sizetype
, size_zero_node
, NULL_TREE
);
6122 else if (decl_context
== TYPENAME
)
6124 if (array_parm_vla_unspec_p
)
6127 warning (0, "%<[*]%> not in a declaration");
6128 /* We use this to avoid messing up with incomplete
6129 array types of the same type, that would
6130 otherwise be modified below. */
6131 itype
= build_range_type (sizetype
, size_zero_node
,
6137 /* Complain about arrays of incomplete types. */
6138 if (!COMPLETE_TYPE_P (type
))
6140 error_at (loc
, "array type has incomplete element type %qT",
6142 /* See if we can be more helpful. */
6143 if (TREE_CODE (type
) == ARRAY_TYPE
)
6146 inform (loc
, "declaration of %qE as multidimensional "
6147 "array must have bounds for all dimensions "
6148 "except the first", name
);
6150 inform (loc
, "declaration of multidimensional array "
6151 "must have bounds for all dimensions except "
6154 type
= error_mark_node
;
6157 /* When itype is NULL, a shared incomplete array type is
6158 returned for all array of a given type. Elsewhere we
6159 make sure we don't complete that type before copying
6160 it, but here we want to make sure we don't ever
6161 modify the shared type, so we gcc_assert (itype)
6164 addr_space_t as
= DECODE_QUAL_ADDR_SPACE (type_quals
);
6165 if (!ADDR_SPACE_GENERIC_P (as
) && as
!= TYPE_ADDR_SPACE (type
))
6166 type
= build_qualified_type (type
,
6167 ENCODE_QUAL_ADDR_SPACE (as
));
6169 type
= build_array_type (type
, itype
);
6172 if (type
!= error_mark_node
)
6176 /* It is ok to modify type here even if itype is
6177 NULL: if size_varies, we're in a
6178 multi-dimensional array and the inner type has
6179 variable size, so the enclosing shared array type
6181 if (size
&& TREE_CODE (size
) == INTEGER_CST
)
6183 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6184 C_TYPE_VARIABLE_SIZE (type
) = 1;
6187 /* The GCC extension for zero-length arrays differs from
6188 ISO flexible array members in that sizeof yields
6190 if (size
&& integer_zerop (size
))
6193 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6194 TYPE_SIZE (type
) = bitsize_zero_node
;
6195 TYPE_SIZE_UNIT (type
) = size_zero_node
;
6196 SET_TYPE_STRUCTURAL_EQUALITY (type
);
6198 if (array_parm_vla_unspec_p
)
6201 /* The type is complete. C99 6.7.5.2p4 */
6202 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6203 TYPE_SIZE (type
) = bitsize_zero_node
;
6204 TYPE_SIZE_UNIT (type
) = size_zero_node
;
6205 SET_TYPE_STRUCTURAL_EQUALITY (type
);
6208 if (!valid_array_size_p (loc
, type
, name
))
6209 type
= error_mark_node
;
6212 if (decl_context
!= PARM
6213 && (array_ptr_quals
!= TYPE_UNQUALIFIED
6214 || array_ptr_attrs
!= NULL_TREE
6215 || array_parm_static
))
6217 error_at (loc
, "static or type qualifiers in non-parameter "
6218 "array declarator");
6219 array_ptr_quals
= TYPE_UNQUALIFIED
;
6220 array_ptr_attrs
= NULL_TREE
;
6221 array_parm_static
= 0;
6223 orig_qual_indirect
++;
6228 /* Say it's a definition only for the declarator closest
6229 to the identifier, apart possibly from some
6231 bool really_funcdef
= false;
6233 orig_qual_type
= NULL_TREE
;
6236 const struct c_declarator
*t
= declarator
->declarator
;
6237 while (t
->kind
== cdk_attrs
)
6239 really_funcdef
= (t
->kind
== cdk_id
);
6242 /* Declaring a function type. Make sure we have a valid
6243 type for the function to return. */
6244 if (type
== error_mark_node
)
6247 size_varies
= false;
6249 /* Warn about some types functions can't return. */
6250 if (TREE_CODE (type
) == FUNCTION_TYPE
)
6253 error_at (loc
, "%qE declared as function returning a "
6256 error_at (loc
, "type name declared as function "
6257 "returning a function");
6258 type
= integer_type_node
;
6260 if (TREE_CODE (type
) == ARRAY_TYPE
)
6263 error_at (loc
, "%qE declared as function returning an array",
6266 error_at (loc
, "type name declared as function returning "
6268 type
= integer_type_node
;
6271 /* Construct the function type and go to the next
6272 inner layer of declarator. */
6273 arg_info
= declarator
->u
.arg_info
;
6274 arg_types
= grokparms (arg_info
, really_funcdef
);
6276 /* Type qualifiers before the return type of the function
6277 qualify the return type, not the function type. */
6280 int quals_used
= type_quals
;
6281 /* Type qualifiers on a function return type are
6282 normally permitted by the standard but have no
6283 effect, so give a warning at -Wreturn-type.
6284 Qualifiers on a void return type are banned on
6285 function definitions in ISO C; GCC used to used
6286 them for noreturn functions. The resolution of C11
6287 DR#423 means qualifiers (other than _Atomic) are
6288 actually removed from the return type when
6289 determining the function type. */
6291 quals_used
&= TYPE_QUAL_ATOMIC
;
6292 if (quals_used
&& VOID_TYPE_P (type
) && really_funcdef
)
6294 "function definition has qualified void return type");
6296 warning_at (loc
, OPT_Wignored_qualifiers
,
6297 "type qualifiers ignored on function return type");
6299 /* Ensure an error for restrict on invalid types; the
6300 DR#423 resolution is not entirely clear about
6303 && (type_quals
& TYPE_QUAL_RESTRICT
)
6304 && (!POINTER_TYPE_P (type
)
6305 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
6306 error_at (loc
, "invalid use of %<restrict%>");
6308 type
= c_build_qualified_type (type
, quals_used
);
6310 type_quals
= TYPE_UNQUALIFIED
;
6312 type
= build_function_type (type
, arg_types
);
6313 declarator
= declarator
->declarator
;
6315 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6316 the formal parameter list of this FUNCTION_TYPE to point to
6317 the FUNCTION_TYPE node itself. */
6322 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info
->tags
, ix
, tag
)
6323 TYPE_CONTEXT (tag
->type
) = type
;
6329 /* Merge any constancy or volatility into the target type
6331 if ((type_quals
& TYPE_QUAL_ATOMIC
)
6332 && TREE_CODE (type
) == FUNCTION_TYPE
)
6335 "%<_Atomic%>-qualified function type");
6336 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6338 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
6340 pedwarn (loc
, OPT_Wpedantic
,
6341 "ISO C forbids qualified function types");
6343 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6344 orig_qual_indirect
);
6345 orig_qual_type
= NULL_TREE
;
6346 size_varies
= false;
6348 /* When the pointed-to type involves components of variable size,
6349 care must be taken to ensure that the size evaluation code is
6350 emitted early enough to dominate all the possible later uses
6351 and late enough for the variables on which it depends to have
6354 This is expected to happen automatically when the pointed-to
6355 type has a name/declaration of it's own, but special attention
6356 is required if the type is anonymous.
6358 We handle the NORMAL and FIELD contexts here by attaching an
6359 artificial TYPE_DECL to such pointed-to type. This forces the
6360 sizes evaluation at a safe point and ensures it is not deferred
6361 until e.g. within a deeper conditional context.
6363 We expect nothing to be needed here for PARM or TYPENAME.
6364 Pushing a TYPE_DECL at this point for TYPENAME would actually
6365 be incorrect, as we might be in the middle of an expression
6366 with side effects on the pointed-to type size "arguments" prior
6367 to the pointer declaration point and the fake TYPE_DECL in the
6368 enclosing context would force the size evaluation prior to the
6371 if (!TYPE_NAME (type
)
6372 && (decl_context
== NORMAL
|| decl_context
== FIELD
)
6373 && variably_modified_type_p (type
, NULL_TREE
))
6375 tree decl
= build_decl (loc
, TYPE_DECL
, NULL_TREE
, type
);
6376 DECL_ARTIFICIAL (decl
) = 1;
6378 finish_decl (decl
, loc
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
6379 TYPE_NAME (type
) = decl
;
6382 type
= c_build_pointer_type (type
);
6384 /* Process type qualifiers (such as const or volatile)
6385 that were given inside the `*'. */
6386 type_quals
= declarator
->u
.pointer_quals
;
6388 declarator
= declarator
->declarator
;
6395 *decl_attrs
= chainon (returned_attrs
, *decl_attrs
);
6397 /* Now TYPE has the actual type, apart from any qualifiers in
6400 /* Warn about address space used for things other than static memory or
6402 address_space
= DECODE_QUAL_ADDR_SPACE (type_quals
);
6403 if (!ADDR_SPACE_GENERIC_P (address_space
))
6405 if (decl_context
== NORMAL
)
6407 switch (storage_class
)
6410 error ("%qs combined with %<auto%> qualifier for %qE",
6411 c_addr_space_name (address_space
), name
);
6414 error ("%qs combined with %<register%> qualifier for %qE",
6415 c_addr_space_name (address_space
), name
);
6418 if (current_function_scope
)
6420 error ("%qs specified for auto variable %qE",
6421 c_addr_space_name (address_space
), name
);
6433 else if (decl_context
== PARM
&& TREE_CODE (type
) != ARRAY_TYPE
)
6436 error ("%qs specified for parameter %qE",
6437 c_addr_space_name (address_space
), name
);
6439 error ("%qs specified for unnamed parameter",
6440 c_addr_space_name (address_space
));
6442 else if (decl_context
== FIELD
)
6445 error ("%qs specified for structure field %qE",
6446 c_addr_space_name (address_space
), name
);
6448 error ("%qs specified for structure field",
6449 c_addr_space_name (address_space
));
6453 /* Check the type and width of a bit-field. */
6456 check_bitfield_type_and_width (loc
, &type
, width
, name
);
6457 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6458 atomic types are permitted for bit-fields; we have no code to
6459 make bit-field accesses atomic, so disallow them. */
6460 if (type_quals
& TYPE_QUAL_ATOMIC
)
6463 error_at (loc
, "bit-field %qE has atomic type", name
);
6465 error_at (loc
, "bit-field has atomic type");
6466 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6470 /* Reject invalid uses of _Alignas. */
6471 if (declspecs
->alignas_p
)
6473 if (storage_class
== csc_typedef
)
6474 error_at (loc
, "alignment specified for typedef %qE", name
);
6475 else if (storage_class
== csc_register
)
6476 error_at (loc
, "alignment specified for %<register%> object %qE",
6478 else if (decl_context
== PARM
)
6481 error_at (loc
, "alignment specified for parameter %qE", name
);
6483 error_at (loc
, "alignment specified for unnamed parameter");
6488 error_at (loc
, "alignment specified for bit-field %qE", name
);
6490 error_at (loc
, "alignment specified for unnamed bit-field");
6492 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
6493 error_at (loc
, "alignment specified for function %qE", name
);
6494 else if (declspecs
->align_log
!= -1 && TYPE_P (type
))
6496 alignas_align
= 1U << declspecs
->align_log
;
6497 if (alignas_align
< min_align_of_type (type
))
6500 error_at (loc
, "%<_Alignas%> specifiers cannot reduce "
6501 "alignment of %qE", name
);
6503 error_at (loc
, "%<_Alignas%> specifiers cannot reduce "
6504 "alignment of unnamed field");
6510 /* If this is declaring a typedef name, return a TYPE_DECL. */
6512 if (storage_class
== csc_typedef
)
6515 if ((type_quals
& TYPE_QUAL_ATOMIC
)
6516 && TREE_CODE (type
) == FUNCTION_TYPE
)
6519 "%<_Atomic%>-qualified function type");
6520 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6522 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
6524 pedwarn (loc
, OPT_Wpedantic
,
6525 "ISO C forbids qualified function types");
6527 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6528 orig_qual_indirect
);
6529 decl
= build_decl (declarator
->id_loc
,
6530 TYPE_DECL
, declarator
->u
.id
, type
);
6531 if (declspecs
->explicit_signed_p
)
6532 C_TYPEDEF_EXPLICITLY_SIGNED (decl
) = 1;
6533 if (declspecs
->inline_p
)
6534 pedwarn (loc
, 0,"typedef %q+D declared %<inline%>", decl
);
6535 if (declspecs
->noreturn_p
)
6536 pedwarn (loc
, 0,"typedef %q+D declared %<_Noreturn%>", decl
);
6538 if (warn_cxx_compat
&& declarator
->u
.id
!= NULL_TREE
)
6540 struct c_binding
*b
= I_TAG_BINDING (declarator
->u
.id
);
6543 && b
->decl
!= NULL_TREE
6544 && (B_IN_CURRENT_SCOPE (b
)
6545 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
6546 && TYPE_MAIN_VARIANT (b
->decl
) != TYPE_MAIN_VARIANT (type
))
6548 warning_at (declarator
->id_loc
, OPT_Wc___compat
,
6549 ("using %qD as both a typedef and a tag is "
6552 if (b
->locus
!= UNKNOWN_LOCATION
)
6553 inform (b
->locus
, "originally defined here");
6560 /* If this is a type name (such as, in a cast or sizeof),
6561 compute the type and return it now. */
6563 if (decl_context
== TYPENAME
)
6565 /* Note that the grammar rejects storage classes in typenames
6567 gcc_assert (storage_class
== csc_none
&& !threadp
6568 && !declspecs
->inline_p
&& !declspecs
->noreturn_p
);
6569 if ((type_quals
& TYPE_QUAL_ATOMIC
)
6570 && TREE_CODE (type
) == FUNCTION_TYPE
)
6573 "%<_Atomic%>-qualified function type");
6574 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6576 else if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
6578 pedwarn (loc
, OPT_Wpedantic
,
6579 "ISO C forbids const or volatile function types");
6581 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6582 orig_qual_indirect
);
6586 if (pedantic
&& decl_context
== FIELD
6587 && variably_modified_type_p (type
, NULL_TREE
))
6590 pedwarn (loc
, OPT_Wpedantic
, "a member of a structure or union cannot "
6591 "have a variably modified type");
6594 /* Aside from typedefs and type names (handle above),
6595 `void' at top level (not within pointer)
6596 is allowed only in public variables.
6597 We don't complain about parms either, but that is because
6598 a better error message can be made later. */
6600 if (VOID_TYPE_P (type
) && decl_context
!= PARM
6601 && !((decl_context
!= FIELD
&& TREE_CODE (type
) != FUNCTION_TYPE
)
6602 && (storage_class
== csc_extern
6603 || (current_scope
== file_scope
6604 && !(storage_class
== csc_static
6605 || storage_class
== csc_register
)))))
6607 error_at (loc
, "variable or field %qE declared void", name
);
6608 type
= integer_type_node
;
6611 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6612 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6617 if (decl_context
== PARM
)
6620 bool array_parameter_p
= false;
6622 /* A parameter declared as an array of T is really a pointer to T.
6623 One declared as a function is really a pointer to a function. */
6625 if (TREE_CODE (type
) == ARRAY_TYPE
)
6627 /* Transfer const-ness of array into that of type pointed to. */
6628 type
= TREE_TYPE (type
);
6629 if (orig_qual_type
!= NULL_TREE
)
6631 if (orig_qual_indirect
== 0)
6632 orig_qual_type
= TREE_TYPE (orig_qual_type
);
6634 orig_qual_indirect
--;
6637 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6638 orig_qual_indirect
);
6639 type
= c_build_pointer_type (type
);
6640 type_quals
= array_ptr_quals
;
6642 type
= c_build_qualified_type (type
, type_quals
);
6644 /* We don't yet implement attributes in this context. */
6645 if (array_ptr_attrs
!= NULL_TREE
)
6646 warning_at (loc
, OPT_Wattributes
,
6647 "attributes in parameter array declarator ignored");
6649 size_varies
= false;
6650 array_parameter_p
= true;
6652 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
6654 if (type_quals
& TYPE_QUAL_ATOMIC
)
6657 "%<_Atomic%>-qualified function type");
6658 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6660 else if (type_quals
)
6661 pedwarn (loc
, OPT_Wpedantic
,
6662 "ISO C forbids qualified function types");
6664 type
= c_build_qualified_type (type
, type_quals
);
6665 type
= c_build_pointer_type (type
);
6666 type_quals
= TYPE_UNQUALIFIED
;
6668 else if (type_quals
)
6669 type
= c_build_qualified_type (type
, type_quals
);
6671 decl
= build_decl (declarator
->id_loc
,
6672 PARM_DECL
, declarator
->u
.id
, type
);
6674 C_DECL_VARIABLE_SIZE (decl
) = 1;
6675 C_ARRAY_PARAMETER (decl
) = array_parameter_p
;
6677 /* Compute the type actually passed in the parmlist,
6678 for the case where there is no prototype.
6679 (For example, shorts and chars are passed as ints.)
6680 When there is a prototype, this is overridden later. */
6682 if (type
== error_mark_node
)
6683 promoted_type
= type
;
6685 promoted_type
= c_type_promotes_to (type
);
6687 DECL_ARG_TYPE (decl
) = promoted_type
;
6688 if (declspecs
->inline_p
)
6689 pedwarn (loc
, 0, "parameter %q+D declared %<inline%>", decl
);
6690 if (declspecs
->noreturn_p
)
6691 pedwarn (loc
, 0, "parameter %q+D declared %<_Noreturn%>", decl
);
6693 else if (decl_context
== FIELD
)
6695 /* Note that the grammar rejects storage classes in typenames
6697 gcc_assert (storage_class
== csc_none
&& !threadp
6698 && !declspecs
->inline_p
&& !declspecs
->noreturn_p
);
6700 /* Structure field. It may not be a function. */
6702 if (TREE_CODE (type
) == FUNCTION_TYPE
)
6704 error_at (loc
, "field %qE declared as a function", name
);
6705 type
= build_pointer_type (type
);
6707 else if (TREE_CODE (type
) != ERROR_MARK
6708 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type
))
6711 error_at (loc
, "field %qE has incomplete type", name
);
6713 error_at (loc
, "unnamed field has incomplete type");
6714 type
= error_mark_node
;
6716 else if (TREE_CODE (type
) == ARRAY_TYPE
6717 && TYPE_DOMAIN (type
) == NULL_TREE
)
6719 /* We have a flexible array member through a typedef.
6720 Set suitable range. Whether this is a correct position
6721 for a flexible array member will be determined elsewhere. */
6722 if (!in_system_header_at (input_location
))
6723 pedwarn_c90 (loc
, OPT_Wpedantic
, "ISO C90 does not "
6724 "support flexible array members");
6725 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6726 TYPE_DOMAIN (type
) = build_range_type (sizetype
, size_zero_node
,
6729 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6730 orig_qual_indirect
);
6731 decl
= build_decl (declarator
->id_loc
,
6732 FIELD_DECL
, declarator
->u
.id
, type
);
6733 DECL_NONADDRESSABLE_P (decl
) = bitfield
;
6734 if (bitfield
&& !declarator
->u
.id
)
6735 TREE_NO_WARNING (decl
) = 1;
6738 C_DECL_VARIABLE_SIZE (decl
) = 1;
6740 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
6742 if (storage_class
== csc_register
|| threadp
)
6744 error_at (loc
, "invalid storage class for function %qE", name
);
6746 else if (current_scope
!= file_scope
)
6748 /* Function declaration not at file scope. Storage
6749 classes other than `extern' are not allowed, C99
6750 6.7.1p5, and `extern' makes no difference. However,
6751 GCC allows 'auto', perhaps with 'inline', to support
6752 nested functions. */
6753 if (storage_class
== csc_auto
)
6754 pedwarn (loc
, OPT_Wpedantic
,
6755 "invalid storage class for function %qE", name
);
6756 else if (storage_class
== csc_static
)
6758 error_at (loc
, "invalid storage class for function %qE", name
);
6760 storage_class
= declspecs
->storage_class
= csc_none
;
6766 decl
= build_decl (declarator
->id_loc
,
6767 FUNCTION_DECL
, declarator
->u
.id
, type
);
6768 decl
= build_decl_attribute_variant (decl
, decl_attr
);
6770 if (type_quals
& TYPE_QUAL_ATOMIC
)
6773 "%<_Atomic%>-qualified function type");
6774 type_quals
&= ~TYPE_QUAL_ATOMIC
;
6776 else if (pedantic
&& type_quals
&& !DECL_IN_SYSTEM_HEADER (decl
))
6777 pedwarn (loc
, OPT_Wpedantic
,
6778 "ISO C forbids qualified function types");
6780 /* Every function declaration is an external reference
6781 (DECL_EXTERNAL) except for those which are not at file
6782 scope and are explicitly declared "auto". This is
6783 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6784 GCC to signify a forward declaration of a nested function. */
6785 if (storage_class
== csc_auto
&& current_scope
!= file_scope
)
6786 DECL_EXTERNAL (decl
) = 0;
6787 /* In C99, a function which is declared 'inline' with 'extern'
6788 is not an external reference (which is confusing). It
6789 means that the later definition of the function must be output
6790 in this file, C99 6.7.4p6. In GNU C89, a function declared
6791 'extern inline' is an external reference. */
6792 else if (declspecs
->inline_p
&& storage_class
!= csc_static
)
6793 DECL_EXTERNAL (decl
) = ((storage_class
== csc_extern
)
6794 == flag_gnu89_inline
);
6796 DECL_EXTERNAL (decl
) = !initialized
;
6798 /* Record absence of global scope for `static' or `auto'. */
6800 = !(storage_class
== csc_static
|| storage_class
== csc_auto
);
6802 /* For a function definition, record the argument information
6803 block where store_parm_decls will look for it. */
6805 current_function_arg_info
= arg_info
;
6807 if (declspecs
->default_int_p
)
6808 C_FUNCTION_IMPLICIT_INT (decl
) = 1;
6810 /* Record presence of `inline' and `_Noreturn', if it is
6812 if (flag_hosted
&& MAIN_NAME_P (declarator
->u
.id
))
6814 if (declspecs
->inline_p
)
6815 pedwarn (loc
, 0, "cannot inline function %<main%>");
6816 if (declspecs
->noreturn_p
)
6817 pedwarn (loc
, 0, "%<main%> declared %<_Noreturn%>");
6821 if (declspecs
->inline_p
)
6822 /* Record that the function is declared `inline'. */
6823 DECL_DECLARED_INLINE_P (decl
) = 1;
6824 if (declspecs
->noreturn_p
)
6827 pedwarn_c99 (loc
, OPT_Wpedantic
,
6828 "ISO C99 does not support %<_Noreturn%>");
6830 pedwarn_c99 (loc
, OPT_Wpedantic
,
6831 "ISO C90 does not support %<_Noreturn%>");
6832 TREE_THIS_VOLATILE (decl
) = 1;
6838 /* It's a variable. */
6839 /* An uninitialized decl with `extern' is a reference. */
6840 int extern_ref
= !initialized
&& storage_class
== csc_extern
;
6842 type
= c_build_qualified_type (type
, type_quals
, orig_qual_type
,
6843 orig_qual_indirect
);
6845 /* C99 6.2.2p7: It is invalid (compile-time undefined
6846 behavior) to create an 'extern' declaration for a
6847 variable if there is a global declaration that is
6848 'static' and the global declaration is not visible.
6849 (If the static declaration _is_ currently visible,
6850 the 'extern' declaration is taken to refer to that decl.) */
6851 if (extern_ref
&& current_scope
!= file_scope
)
6853 tree global_decl
= identifier_global_value (declarator
->u
.id
);
6854 tree visible_decl
= lookup_name (declarator
->u
.id
);
6857 && global_decl
!= visible_decl
6858 && VAR_P (global_decl
)
6859 && !TREE_PUBLIC (global_decl
))
6860 error_at (loc
, "variable previously declared %<static%> "
6861 "redeclared %<extern%>");
6864 decl
= build_decl (declarator
->id_loc
,
6865 VAR_DECL
, declarator
->u
.id
, type
);
6867 C_DECL_VARIABLE_SIZE (decl
) = 1;
6869 if (declspecs
->inline_p
)
6870 pedwarn (loc
, 0, "variable %q+D declared %<inline%>", decl
);
6871 if (declspecs
->noreturn_p
)
6872 pedwarn (loc
, 0, "variable %q+D declared %<_Noreturn%>", decl
);
6874 /* At file scope, an initialized extern declaration may follow
6875 a static declaration. In that case, DECL_EXTERNAL will be
6876 reset later in start_decl. */
6877 DECL_EXTERNAL (decl
) = (storage_class
== csc_extern
);
6879 /* At file scope, the presence of a `static' or `register' storage
6880 class specifier, or the absence of all storage class specifiers
6881 makes this declaration a definition (perhaps tentative). Also,
6882 the absence of `static' makes it public. */
6883 if (current_scope
== file_scope
)
6885 TREE_PUBLIC (decl
) = storage_class
!= csc_static
;
6886 TREE_STATIC (decl
) = !extern_ref
;
6888 /* Not at file scope, only `static' makes a static definition. */
6891 TREE_STATIC (decl
) = (storage_class
== csc_static
);
6892 TREE_PUBLIC (decl
) = extern_ref
;
6896 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
6899 if ((storage_class
== csc_extern
6900 || (storage_class
== csc_none
6901 && TREE_CODE (type
) == FUNCTION_TYPE
6903 && variably_modified_type_p (type
, NULL_TREE
))
6906 if (TREE_CODE (type
) == FUNCTION_TYPE
)
6907 error_at (loc
, "non-nested function with variably modified type");
6909 error_at (loc
, "object with variably modified type must have "
6913 /* Record `register' declaration for warnings on &
6914 and in case doing stupid register allocation. */
6916 if (storage_class
== csc_register
)
6918 C_DECL_REGISTER (decl
) = 1;
6919 DECL_REGISTER (decl
) = 1;
6922 /* Record constancy and volatility. */
6923 c_apply_type_quals_to_decl (type_quals
, decl
);
6925 /* Apply _Alignas specifiers. */
6928 SET_DECL_ALIGN (decl
, alignas_align
* BITS_PER_UNIT
);
6929 DECL_USER_ALIGN (decl
) = 1;
6932 /* If a type has volatile components, it should be stored in memory.
6933 Otherwise, the fact that those components are volatile
6934 will be ignored, and would even crash the compiler.
6935 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6936 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl
))
6937 && (VAR_P (decl
) || TREE_CODE (decl
) == PARM_DECL
6938 || TREE_CODE (decl
) == RESULT_DECL
))
6940 /* It is not an error for a structure with volatile fields to
6941 be declared register, but reset DECL_REGISTER since it
6942 cannot actually go in a register. */
6943 int was_reg
= C_DECL_REGISTER (decl
);
6944 C_DECL_REGISTER (decl
) = 0;
6945 DECL_REGISTER (decl
) = 0;
6946 c_mark_addressable (decl
);
6947 C_DECL_REGISTER (decl
) = was_reg
;
6950 /* This is the earliest point at which we might know the assembler
6951 name of a variable. Thus, if it's known before this, die horribly. */
6952 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl
));
6956 && TREE_PUBLIC (decl
)
6957 && TREE_STATIC (decl
)
6958 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
))
6959 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
6960 && TYPE_NAME (TREE_TYPE (decl
)) == NULL_TREE
)
6961 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
6962 ("non-local variable %qD with anonymous type is "
6963 "questionable in C++"),
6970 /* Decode the parameter-list info for a function type or function definition.
6971 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6972 if there is an identifier list instead of a parameter decl list).
6973 These two functions are separate because when a function returns
6974 or receives functions then each is called multiple times but the order
6975 of calls is different. The last call to `grokparms' is always the one
6976 that contains the formal parameter names of a function definition.
6978 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6980 FUNCDEF_FLAG is true for a function definition, false for
6981 a mere declaration. A nonempty identifier-list gets an error message
6982 when FUNCDEF_FLAG is false. */
6985 grokparms (struct c_arg_info
*arg_info
, bool funcdef_flag
)
6987 tree arg_types
= arg_info
->types
;
6989 if (funcdef_flag
&& arg_info
->had_vla_unspec
)
6991 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6993 error ("%<[*]%> not allowed in other than function prototype scope");
6996 if (arg_types
== 0 && !funcdef_flag
6997 && !in_system_header_at (input_location
))
6998 warning (OPT_Wstrict_prototypes
,
6999 "function declaration isn%'t a prototype");
7001 if (arg_types
== error_mark_node
)
7002 return 0; /* don't set TYPE_ARG_TYPES in this case */
7004 else if (arg_types
&& TREE_CODE (TREE_VALUE (arg_types
)) == IDENTIFIER_NODE
)
7008 pedwarn (input_location
, 0, "parameter names (without types) in function declaration");
7009 arg_info
->parms
= NULL_TREE
;
7012 arg_info
->parms
= arg_info
->types
;
7014 arg_info
->types
= 0;
7019 tree parm
, type
, typelt
;
7020 unsigned int parmno
;
7022 /* If there is a parameter of incomplete type in a definition,
7023 this is an error. In a declaration this is valid, and a
7024 struct or union type may be completed later, before any calls
7025 or definition of the function. In the case where the tag was
7026 first declared within the parameter list, a warning has
7027 already been given. If a parameter has void type, then
7028 however the function cannot be defined or called, so
7031 for (parm
= arg_info
->parms
, typelt
= arg_types
, parmno
= 1;
7033 parm
= DECL_CHAIN (parm
), typelt
= TREE_CHAIN (typelt
), parmno
++)
7035 type
= TREE_VALUE (typelt
);
7036 if (type
== error_mark_node
)
7039 if (!COMPLETE_TYPE_P (type
))
7043 if (DECL_NAME (parm
))
7044 error_at (input_location
,
7045 "parameter %u (%q+D) has incomplete type",
7048 error_at (DECL_SOURCE_LOCATION (parm
),
7049 "parameter %u has incomplete type",
7052 TREE_VALUE (typelt
) = error_mark_node
;
7053 TREE_TYPE (parm
) = error_mark_node
;
7054 arg_types
= NULL_TREE
;
7056 else if (VOID_TYPE_P (type
))
7058 if (DECL_NAME (parm
))
7059 warning_at (input_location
, 0,
7060 "parameter %u (%q+D) has void type",
7063 warning_at (DECL_SOURCE_LOCATION (parm
), 0,
7064 "parameter %u has void type",
7069 if (DECL_NAME (parm
) && TREE_USED (parm
))
7070 warn_if_shadowing (parm
);
7076 /* Allocate and initialize a c_arg_info structure from the parser's
7080 build_arg_info (void)
7082 struct c_arg_info
*ret
= XOBNEW (&parser_obstack
, struct c_arg_info
);
7083 ret
->parms
= NULL_TREE
;
7085 ret
->types
= NULL_TREE
;
7086 ret
->others
= NULL_TREE
;
7087 ret
->pending_sizes
= NULL
;
7088 ret
->had_vla_unspec
= 0;
7092 /* Take apart the current scope and return a c_arg_info structure with
7093 info on a parameter list just parsed.
7095 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7097 ELLIPSIS being true means the argument list ended in '...' so don't
7098 append a sentinel (void_list_node) to the end of the type-list.
7100 EXPR is NULL or an expression that needs to be evaluated for the
7101 side effects of array size expressions in the parameters. */
7104 get_parm_info (bool ellipsis
, tree expr
)
7106 struct c_binding
*b
= current_scope
->bindings
;
7107 struct c_arg_info
*arg_info
= build_arg_info ();
7110 vec
<c_arg_tag
, va_gc
> *tags
= NULL
;
7114 bool gave_void_only_once_err
= false;
7116 arg_info
->had_vla_unspec
= current_scope
->had_vla_unspec
;
7118 /* The bindings in this scope must not get put into a block.
7119 We will take care of deleting the binding nodes. */
7120 current_scope
->bindings
= 0;
7122 /* This function is only called if there was *something* on the
7126 /* A parameter list consisting solely of 'void' indicates that the
7127 function takes no arguments. But if the 'void' is qualified
7128 (by 'const' or 'volatile'), or has a storage class specifier
7129 ('register'), then the behavior is undefined; issue an error.
7130 Typedefs for 'void' are OK (see DR#157). */
7131 if (b
->prev
== 0 /* one binding */
7132 && TREE_CODE (b
->decl
) == PARM_DECL
/* which is a parameter */
7133 && !DECL_NAME (b
->decl
) /* anonymous */
7134 && VOID_TYPE_P (TREE_TYPE (b
->decl
))) /* of void type */
7136 if (TYPE_QUALS (TREE_TYPE (b
->decl
)) != TYPE_UNQUALIFIED
7137 || C_DECL_REGISTER (b
->decl
))
7138 error_at (b
->locus
, "%<void%> as only parameter may not be qualified");
7140 /* There cannot be an ellipsis. */
7142 error_at (b
->locus
, "%<void%> must be the only parameter");
7144 arg_info
->types
= void_list_node
;
7149 types
= void_list_node
;
7151 /* Break up the bindings list into parms, tags, types, and others;
7152 apply sanity checks; purge the name-to-decl bindings. */
7155 tree decl
= b
->decl
;
7156 tree type
= TREE_TYPE (decl
);
7158 const char *keyword
;
7160 switch (TREE_CODE (decl
))
7165 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
7166 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
7169 /* Check for forward decls that never got their actual decl. */
7170 if (TREE_ASM_WRITTEN (decl
))
7172 "parameter %q+D has just a forward declaration", decl
);
7173 /* Check for (..., void, ...) and issue an error. */
7174 else if (VOID_TYPE_P (type
) && !DECL_NAME (decl
))
7176 if (!gave_void_only_once_err
)
7178 error_at (b
->locus
, "%<void%> must be the only parameter");
7179 gave_void_only_once_err
= true;
7184 /* Valid parameter, add it to the list. */
7185 DECL_CHAIN (decl
) = parms
;
7188 /* Since there is a prototype, args are passed in their
7189 declared types. The back end may override this later. */
7190 DECL_ARG_TYPE (decl
) = type
;
7191 types
= tree_cons (0, type
, types
);
7195 case ENUMERAL_TYPE
: keyword
= "enum"; goto tag
;
7196 case UNION_TYPE
: keyword
= "union"; goto tag
;
7197 case RECORD_TYPE
: keyword
= "struct"; goto tag
;
7199 /* Types may not have tag-names, in which case the type
7200 appears in the bindings list with b->id NULL. */
7203 gcc_assert (I_TAG_BINDING (b
->id
) == b
);
7204 I_TAG_BINDING (b
->id
) = b
->shadowed
;
7207 /* Warn about any struct, union or enum tags defined in a
7208 parameter list. The scope of such types is limited to
7209 the parameter list, which is rarely if ever desirable
7210 (it's impossible to call such a function with type-
7211 correct arguments). An anonymous union parm type is
7212 meaningful as a GNU extension, so don't warn for that. */
7213 if (TREE_CODE (decl
) != UNION_TYPE
|| b
->id
!= 0)
7216 /* The %s will be one of 'struct', 'union', or 'enum'. */
7217 warning_at (b
->locus
, 0,
7218 "%<%s %E%> declared inside parameter list"
7219 " will not be visible outside of this definition or"
7220 " declaration", keyword
, b
->id
);
7222 /* The %s will be one of 'struct', 'union', or 'enum'. */
7223 warning_at (b
->locus
, 0,
7224 "anonymous %s declared inside parameter list"
7225 " will not be visible outside of this definition or"
7226 " declaration", keyword
);
7231 vec_safe_push (tags
, tag
);
7235 /* FUNCTION_DECLs appear when there is an implicit function
7236 declaration in the parameter list. */
7237 gcc_assert (b
->nested
|| seen_error ());
7242 /* CONST_DECLs appear here when we have an embedded enum,
7243 and TYPE_DECLs appear here when we have an embedded struct
7244 or union. No warnings for this - we already warned about the
7247 /* When we reinsert this decl in the function body, we need
7248 to reconstruct whether it was marked as nested. */
7249 gcc_assert (!b
->nested
);
7250 DECL_CHAIN (decl
) = others
;
7256 /* error_mark_node appears here when we have an undeclared
7257 variable. Just throw it away. */
7260 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
7261 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
7265 /* Other things that might be encountered. */
7272 b
= free_binding_and_advance (b
);
7275 arg_info
->parms
= parms
;
7276 arg_info
->tags
= tags
;
7277 arg_info
->types
= types
;
7278 arg_info
->others
= others
;
7279 arg_info
->pending_sizes
= expr
;
7283 /* Get the struct, enum or union (CODE says which) with tag NAME.
7284 Define the tag as a forward-reference with location LOC if it is
7285 not defined. Return a c_typespec structure for the type
7289 parser_xref_tag (location_t loc
, enum tree_code code
, tree name
)
7291 struct c_typespec ret
;
7295 ret
.expr
= NULL_TREE
;
7296 ret
.expr_const_operands
= true;
7298 /* If a cross reference is requested, look up the type
7299 already defined for this tag and return it. */
7301 ref
= lookup_tag (code
, name
, false, &refloc
);
7302 /* If this is the right type of tag, return what we found.
7303 (This reference will be shadowed by shadow_tag later if appropriate.)
7304 If this is the wrong type of tag, do not return it. If it was the
7305 wrong type in the same scope, we will have had an error
7306 message already; if in a different scope and declaring
7307 a name, pending_xref_error will give an error message; but if in a
7308 different scope and not declaring a name, this tag should
7309 shadow the previous declaration of a different type of tag, and
7310 this would not work properly if we return the reference found.
7311 (For example, with "struct foo" in an outer scope, "union foo;"
7312 must shadow that tag with a new one of union type.) */
7313 ret
.kind
= (ref
? ctsk_tagref
: ctsk_tagfirstref
);
7314 if (ref
&& TREE_CODE (ref
) == code
)
7316 if (C_TYPE_DEFINED_IN_STRUCT (ref
)
7317 && loc
!= UNKNOWN_LOCATION
7323 warning_at (loc
, OPT_Wc___compat
,
7324 ("enum type defined in struct or union "
7325 "is not visible in C++"));
7326 inform (refloc
, "enum type defined here");
7329 warning_at (loc
, OPT_Wc___compat
,
7330 ("struct defined in struct or union "
7331 "is not visible in C++"));
7332 inform (refloc
, "struct defined here");
7335 warning_at (loc
, OPT_Wc___compat
,
7336 ("union defined in struct or union "
7337 "is not visible in C++"));
7338 inform (refloc
, "union defined here");
7349 /* If no such tag is yet defined, create a forward-reference node
7350 and record it as the "definition".
7351 When a real declaration of this type is found,
7352 the forward-reference will be altered into a real type. */
7354 ref
= make_node (code
);
7355 if (code
== ENUMERAL_TYPE
)
7357 /* Give the type a default layout like unsigned int
7358 to avoid crashing if it does not get defined. */
7359 SET_TYPE_MODE (ref
, TYPE_MODE (unsigned_type_node
));
7360 SET_TYPE_ALIGN (ref
, TYPE_ALIGN (unsigned_type_node
));
7361 TYPE_USER_ALIGN (ref
) = 0;
7362 TYPE_UNSIGNED (ref
) = 1;
7363 TYPE_PRECISION (ref
) = TYPE_PRECISION (unsigned_type_node
);
7364 TYPE_MIN_VALUE (ref
) = TYPE_MIN_VALUE (unsigned_type_node
);
7365 TYPE_MAX_VALUE (ref
) = TYPE_MAX_VALUE (unsigned_type_node
);
7368 pushtag (loc
, name
, ref
);
7374 /* Get the struct, enum or union (CODE says which) with tag NAME.
7375 Define the tag as a forward-reference if it is not defined.
7376 Return a tree for the type. */
7379 xref_tag (enum tree_code code
, tree name
)
7381 return parser_xref_tag (input_location
, code
, name
).spec
;
7384 /* Make sure that the tag NAME is defined *in the current scope*
7385 at least as a forward reference.
7386 LOC is the location of the struct's definition.
7387 CODE says which kind of tag NAME ought to be.
7389 This stores the current value of the file static STRUCT_PARSE_INFO
7390 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7391 new c_struct_parse_info structure. The old value of
7392 STRUCT_PARSE_INFO is restored in finish_struct. */
7395 start_struct (location_t loc
, enum tree_code code
, tree name
,
7396 struct c_struct_parse_info
**enclosing_struct_parse_info
)
7398 /* If there is already a tag defined at this scope
7399 (as a forward reference), just return it. */
7401 tree ref
= NULL_TREE
;
7402 location_t refloc
= UNKNOWN_LOCATION
;
7404 if (name
!= NULL_TREE
)
7405 ref
= lookup_tag (code
, name
, true, &refloc
);
7406 if (ref
&& TREE_CODE (ref
) == code
)
7408 if (TYPE_SIZE (ref
))
7410 if (code
== UNION_TYPE
)
7411 error_at (loc
, "redefinition of %<union %E%>", name
);
7413 error_at (loc
, "redefinition of %<struct %E%>", name
);
7414 if (refloc
!= UNKNOWN_LOCATION
)
7415 inform (refloc
, "originally defined here");
7416 /* Don't create structures using a name already in use. */
7419 else if (C_TYPE_BEING_DEFINED (ref
))
7421 if (code
== UNION_TYPE
)
7422 error_at (loc
, "nested redefinition of %<union %E%>", name
);
7424 error_at (loc
, "nested redefinition of %<struct %E%>", name
);
7425 /* Don't bother to report "originally defined here" for a
7426 nested redefinition; the original definition should be
7428 /* Don't create structures that contain themselves. */
7433 /* Otherwise create a forward-reference just so the tag is in scope. */
7435 if (ref
== NULL_TREE
|| TREE_CODE (ref
) != code
)
7437 ref
= make_node (code
);
7438 pushtag (loc
, name
, ref
);
7441 C_TYPE_BEING_DEFINED (ref
) = 1;
7442 for (tree v
= TYPE_MAIN_VARIANT (ref
); v
; v
= TYPE_NEXT_VARIANT (v
))
7443 TYPE_PACKED (v
) = flag_pack_struct
;
7445 *enclosing_struct_parse_info
= struct_parse_info
;
7446 struct_parse_info
= XNEW (struct c_struct_parse_info
);
7447 struct_parse_info
->struct_types
.create (0);
7448 struct_parse_info
->fields
.create (0);
7449 struct_parse_info
->typedefs_seen
.create (0);
7451 /* FIXME: This will issue a warning for a use of a type defined
7452 within a statement expr used within sizeof, et. al. This is not
7453 terribly serious as C++ doesn't permit statement exprs within
7455 if (warn_cxx_compat
&& (in_sizeof
|| in_typeof
|| in_alignof
))
7456 warning_at (loc
, OPT_Wc___compat
,
7457 "defining type in %qs expression is invalid in C++",
7460 : (in_typeof
? "typeof" : "alignof")));
7465 /* Process the specs, declarator and width (NULL if omitted)
7466 of a structure component, returning a FIELD_DECL node.
7467 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7468 DECL_ATTRS is as for grokdeclarator.
7470 LOC is the location of the structure component.
7472 This is done during the parsing of the struct declaration.
7473 The FIELD_DECL nodes are chained together and the lot of them
7474 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7477 grokfield (location_t loc
,
7478 struct c_declarator
*declarator
, struct c_declspecs
*declspecs
,
7479 tree width
, tree
*decl_attrs
)
7483 if (declarator
->kind
== cdk_id
&& declarator
->u
.id
== NULL_TREE
7484 && width
== NULL_TREE
)
7486 /* This is an unnamed decl.
7488 If we have something of the form "union { list } ;" then this
7489 is the anonymous union extension. Similarly for struct.
7491 If this is something of the form "struct foo;", then
7492 If MS or Plan 9 extensions are enabled, this is handled as
7493 an anonymous struct.
7494 Otherwise this is a forward declaration of a structure tag.
7496 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7497 If foo names a structure or union without a tag, then this
7498 is an anonymous struct (this is permitted by C11).
7499 If MS or Plan 9 extensions are enabled and foo names a
7500 structure, then again this is an anonymous struct.
7501 Otherwise this is an error.
7503 Oh what a horrid tangled web we weave. I wonder if MS consciously
7504 took this from Plan 9 or if it was an accident of implementation
7505 that took root before someone noticed the bug... */
7507 tree type
= declspecs
->type
;
7508 bool type_ok
= RECORD_OR_UNION_TYPE_P (type
);
7512 && (flag_ms_extensions
7513 || flag_plan9_extensions
7514 || !declspecs
->typedef_p
))
7516 if (flag_ms_extensions
|| flag_plan9_extensions
)
7518 else if (TYPE_NAME (type
) == NULL
)
7525 pedwarn (loc
, 0, "declaration does not declare anything");
7529 pedwarn_c99 (loc
, OPT_Wpedantic
,
7530 "ISO C99 doesn%'t support unnamed structs/unions");
7532 pedwarn_c99 (loc
, OPT_Wpedantic
,
7533 "ISO C90 doesn%'t support unnamed structs/unions");
7536 value
= grokdeclarator (declarator
, declspecs
, FIELD
, false,
7537 width
? &width
: NULL
, decl_attrs
, NULL
, NULL
,
7540 finish_decl (value
, loc
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
7541 DECL_INITIAL (value
) = width
;
7543 if (warn_cxx_compat
&& DECL_NAME (value
) != NULL_TREE
)
7545 /* If we currently have a binding for this field, set the
7546 in_struct field in the binding, so that we warn about lookups
7548 struct c_binding
*b
= I_SYMBOL_BINDING (DECL_NAME (value
));
7551 /* If the in_struct field is not yet set, push it on a list
7552 to be cleared when this struct is finished. */
7555 struct_parse_info
->fields
.safe_push (b
);
7564 /* Subroutine of detect_field_duplicates: return whether X and Y,
7565 which are both fields in the same struct, have duplicate field
7569 is_duplicate_field (tree x
, tree y
)
7571 if (DECL_NAME (x
) != NULL_TREE
&& DECL_NAME (x
) == DECL_NAME (y
))
7574 /* When using -fplan9-extensions, an anonymous field whose name is a
7575 typedef can duplicate a field name. */
7576 if (flag_plan9_extensions
7577 && (DECL_NAME (x
) == NULL_TREE
|| DECL_NAME (y
) == NULL_TREE
))
7579 tree xt
, xn
, yt
, yn
;
7582 if (DECL_NAME (x
) != NULL_TREE
)
7584 else if (RECORD_OR_UNION_TYPE_P (xt
)
7585 && TYPE_NAME (xt
) != NULL_TREE
7586 && TREE_CODE (TYPE_NAME (xt
)) == TYPE_DECL
)
7587 xn
= DECL_NAME (TYPE_NAME (xt
));
7592 if (DECL_NAME (y
) != NULL_TREE
)
7594 else if (RECORD_OR_UNION_TYPE_P (yt
)
7595 && TYPE_NAME (yt
) != NULL_TREE
7596 && TREE_CODE (TYPE_NAME (yt
)) == TYPE_DECL
)
7597 yn
= DECL_NAME (TYPE_NAME (yt
));
7601 if (xn
!= NULL_TREE
&& xn
== yn
)
7608 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7609 to HTAB, giving errors for any duplicates. */
7612 detect_field_duplicates_hash (tree fieldlist
,
7613 hash_table
<nofree_ptr_hash
<tree_node
> > *htab
)
7618 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
7619 if ((y
= DECL_NAME (x
)) != 0)
7621 slot
= htab
->find_slot (y
, INSERT
);
7624 error ("duplicate member %q+D", x
);
7625 DECL_NAME (x
) = NULL_TREE
;
7629 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
7631 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x
)), htab
);
7633 /* When using -fplan9-extensions, an anonymous field whose
7634 name is a typedef can duplicate a field name. */
7635 if (flag_plan9_extensions
7636 && TYPE_NAME (TREE_TYPE (x
)) != NULL_TREE
7637 && TREE_CODE (TYPE_NAME (TREE_TYPE (x
))) == TYPE_DECL
)
7639 tree xn
= DECL_NAME (TYPE_NAME (TREE_TYPE (x
)));
7640 slot
= htab
->find_slot (xn
, INSERT
);
7642 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x
)));
7648 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7649 the list such that this does not present a problem later. */
7652 detect_field_duplicates (tree fieldlist
)
7657 /* If the struct is the list of instance variables of an Objective-C
7658 class, then we need to check all the instance variables of
7659 superclasses when checking for duplicates (since you can't have
7660 an instance variable in a subclass with the same name as an
7661 instance variable in a superclass). We pass on this job to the
7662 Objective-C compiler. objc_detect_field_duplicates() will return
7663 false if we are not checking the list of instance variables and
7664 the C frontend should proceed with the standard field duplicate
7665 checks. If we are checking the list of instance variables, the
7666 ObjC frontend will do the check, emit the errors if needed, and
7667 then return true. */
7668 if (c_dialect_objc ())
7669 if (objc_detect_field_duplicates (false))
7672 /* First, see if there are more than "a few" fields.
7673 This is trivially true if there are zero or one fields. */
7674 if (!fieldlist
|| !DECL_CHAIN (fieldlist
))
7679 if (DECL_NAME (x
) == NULL_TREE
7680 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
7683 } while (timeout
> 0 && x
);
7685 /* If there were "few" fields and no anonymous structures or unions,
7686 avoid the overhead of allocating a hash table. Instead just do
7687 the nested traversal thing. */
7690 for (x
= DECL_CHAIN (fieldlist
); x
; x
= DECL_CHAIN (x
))
7691 /* When using -fplan9-extensions, we can have duplicates
7692 between typedef names and fields. */
7694 || (flag_plan9_extensions
7695 && DECL_NAME (x
) == NULL_TREE
7696 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
))
7697 && TYPE_NAME (TREE_TYPE (x
)) != NULL_TREE
7698 && TREE_CODE (TYPE_NAME (TREE_TYPE (x
))) == TYPE_DECL
))
7700 for (y
= fieldlist
; y
!= x
; y
= TREE_CHAIN (y
))
7701 if (is_duplicate_field (y
, x
))
7703 error ("duplicate member %q+D", x
);
7704 DECL_NAME (x
) = NULL_TREE
;
7710 hash_table
<nofree_ptr_hash
<tree_node
> > htab (37);
7711 detect_field_duplicates_hash (fieldlist
, &htab
);
7715 /* Finish up struct info used by -Wc++-compat. */
7718 warn_cxx_compat_finish_struct (tree fieldlist
, enum tree_code code
,
7719 location_t record_loc
)
7723 struct c_binding
*b
;
7725 if (fieldlist
== NULL_TREE
)
7727 if (code
== RECORD_TYPE
)
7728 warning_at (record_loc
, OPT_Wc___compat
,
7729 "empty struct has size 0 in C, size 1 in C++");
7731 warning_at (record_loc
, OPT_Wc___compat
,
7732 "empty union has size 0 in C, size 1 in C++");
7735 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7736 the current struct. We do this now at the end of the struct
7737 because the flag is used to issue visibility warnings, and we
7738 only want to issue those warnings if the type is referenced
7739 outside of the struct declaration. */
7740 FOR_EACH_VEC_ELT (struct_parse_info
->struct_types
, ix
, x
)
7741 C_TYPE_DEFINED_IN_STRUCT (x
) = 1;
7743 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7744 typedefs used when declaring fields in this struct. If the name
7745 of any of the fields is also a typedef name then the struct would
7746 not parse in C++, because the C++ lookup rules say that the
7747 typedef name would be looked up in the context of the struct, and
7748 would thus be the field rather than the typedef. */
7749 if (!struct_parse_info
->typedefs_seen
.is_empty ()
7750 && fieldlist
!= NULL_TREE
)
7752 /* Use a hash_set<tree> using the name of the typedef. We can use
7753 a hash_set<tree> because identifiers are interned. */
7754 hash_set
<tree
> tset
;
7756 FOR_EACH_VEC_ELT (struct_parse_info
->typedefs_seen
, ix
, x
)
7757 tset
.add (DECL_NAME (x
));
7759 for (x
= fieldlist
; x
!= NULL_TREE
; x
= DECL_CHAIN (x
))
7761 if (DECL_NAME (x
) != NULL_TREE
7762 && tset
.contains (DECL_NAME (x
)))
7764 warning_at (DECL_SOURCE_LOCATION (x
), OPT_Wc___compat
,
7765 ("using %qD as both field and typedef name is "
7768 /* FIXME: It would be nice to report the location where
7769 the typedef name is used. */
7774 /* For each field which has a binding and which was not defined in
7775 an enclosing struct, clear the in_struct field. */
7776 FOR_EACH_VEC_ELT (struct_parse_info
->fields
, ix
, b
)
7780 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7781 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7782 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7783 ATTRIBUTES are attributes to be applied to the structure.
7785 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7786 the struct was started. */
7789 finish_struct (location_t loc
, tree t
, tree fieldlist
, tree attributes
,
7790 struct c_struct_parse_info
*enclosing_struct_parse_info
)
7793 bool toplevel
= file_scope
== current_scope
;
7794 int saw_named_field
;
7796 /* If this type was previously laid out as a forward reference,
7797 make sure we lay it out again. */
7801 decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
7805 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
7807 if (DECL_NAME (x
) != 0)
7809 if (flag_isoc11
&& RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
7815 if (TREE_CODE (t
) == UNION_TYPE
)
7818 pedwarn (loc
, OPT_Wpedantic
, "union has no named members");
7820 pedwarn (loc
, OPT_Wpedantic
, "union has no members");
7825 pedwarn (loc
, OPT_Wpedantic
, "struct has no named members");
7827 pedwarn (loc
, OPT_Wpedantic
, "struct has no members");
7832 /* Install struct as DECL_CONTEXT of each field decl.
7833 Also process specified field sizes, found in the DECL_INITIAL,
7834 storing 0 there after the type has been changed to precision equal
7835 to its width, rather than the precision of the specified standard
7836 type. (Correct layout requires the original type to have been preserved
7839 saw_named_field
= 0;
7840 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
7842 if (TREE_TYPE (x
) == error_mark_node
)
7845 DECL_CONTEXT (x
) = t
;
7847 /* If any field is const, the structure type is pseudo-const. */
7848 if (TREE_READONLY (x
))
7849 C_TYPE_FIELDS_READONLY (t
) = 1;
7852 /* A field that is pseudo-const makes the structure likewise. */
7853 tree t1
= strip_array_types (TREE_TYPE (x
));
7854 if (RECORD_OR_UNION_TYPE_P (t1
) && C_TYPE_FIELDS_READONLY (t1
))
7855 C_TYPE_FIELDS_READONLY (t
) = 1;
7858 /* Any field that is volatile means variables of this type must be
7859 treated in some ways as volatile. */
7860 if (TREE_THIS_VOLATILE (x
))
7861 C_TYPE_FIELDS_VOLATILE (t
) = 1;
7863 /* Any field of nominal variable size implies structure is too. */
7864 if (C_DECL_VARIABLE_SIZE (x
))
7865 C_TYPE_VARIABLE_SIZE (t
) = 1;
7867 if (DECL_INITIAL (x
))
7869 unsigned HOST_WIDE_INT width
= tree_to_uhwi (DECL_INITIAL (x
));
7870 DECL_SIZE (x
) = bitsize_int (width
);
7871 DECL_BIT_FIELD (x
) = 1;
7872 SET_DECL_C_BIT_FIELD (x
);
7876 && (DECL_BIT_FIELD (x
)
7877 || TYPE_ALIGN (TREE_TYPE (x
)) > BITS_PER_UNIT
))
7878 DECL_PACKED (x
) = 1;
7880 /* Detect flexible array member in an invalid context. */
7881 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
7882 && TYPE_SIZE (TREE_TYPE (x
)) == NULL_TREE
7883 && TYPE_DOMAIN (TREE_TYPE (x
)) != NULL_TREE
7884 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x
))) == NULL_TREE
)
7886 if (TREE_CODE (t
) == UNION_TYPE
)
7888 error_at (DECL_SOURCE_LOCATION (x
),
7889 "flexible array member in union");
7890 TREE_TYPE (x
) = error_mark_node
;
7892 else if (DECL_CHAIN (x
) != NULL_TREE
)
7894 error_at (DECL_SOURCE_LOCATION (x
),
7895 "flexible array member not at end of struct");
7896 TREE_TYPE (x
) = error_mark_node
;
7898 else if (!saw_named_field
)
7900 error_at (DECL_SOURCE_LOCATION (x
),
7901 "flexible array member in otherwise empty struct");
7902 TREE_TYPE (x
) = error_mark_node
;
7906 if (pedantic
&& TREE_CODE (t
) == RECORD_TYPE
7907 && flexible_array_type_p (TREE_TYPE (x
)))
7908 pedwarn (DECL_SOURCE_LOCATION (x
), OPT_Wpedantic
,
7909 "invalid use of structure with flexible array member");
7912 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x
)))
7913 saw_named_field
= 1;
7916 detect_field_duplicates (fieldlist
);
7918 /* Now we have the nearly final fieldlist. Record it,
7919 then lay out the structure or union (including the fields). */
7921 TYPE_FIELDS (t
) = fieldlist
;
7923 maybe_apply_pragma_scalar_storage_order (t
);
7927 if (TYPE_SIZE_UNIT (t
)
7928 && TREE_CODE (TYPE_SIZE_UNIT (t
)) == INTEGER_CST
7929 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t
))
7930 && !valid_constant_size_p (TYPE_SIZE_UNIT (t
)))
7931 error ("type %qT is too large", t
);
7933 /* Give bit-fields their proper types and rewrite the type of array fields
7934 with scalar component if the enclosing type has reverse storage order. */
7935 for (tree field
= fieldlist
; field
; field
= DECL_CHAIN (field
))
7937 if (TREE_CODE (field
) == FIELD_DECL
7938 && DECL_INITIAL (field
)
7939 && TREE_TYPE (field
) != error_mark_node
)
7941 unsigned HOST_WIDE_INT width
7942 = tree_to_uhwi (DECL_INITIAL (field
));
7943 tree type
= TREE_TYPE (field
);
7944 if (width
!= TYPE_PRECISION (type
))
7947 = c_build_bitfield_integer_type (width
, TYPE_UNSIGNED (type
));
7948 DECL_MODE (field
) = TYPE_MODE (TREE_TYPE (field
));
7950 DECL_INITIAL (field
) = 0;
7952 else if (TYPE_REVERSE_STORAGE_ORDER (t
)
7953 && TREE_CODE (field
) == FIELD_DECL
7954 && TREE_CODE (TREE_TYPE (field
)) == ARRAY_TYPE
)
7956 tree ftype
= TREE_TYPE (field
);
7957 tree ctype
= strip_array_types (ftype
);
7958 if (!RECORD_OR_UNION_TYPE_P (ctype
) && TYPE_MODE (ctype
) != QImode
)
7960 tree fmain_type
= TYPE_MAIN_VARIANT (ftype
);
7961 tree
*typep
= &fmain_type
;
7963 *typep
= build_distinct_type_copy (*typep
);
7964 TYPE_REVERSE_STORAGE_ORDER (*typep
) = 1;
7965 typep
= &TREE_TYPE (*typep
);
7966 } while (TREE_CODE (*typep
) == ARRAY_TYPE
);
7968 = c_build_qualified_type (fmain_type
, TYPE_QUALS (ftype
));
7973 /* Now we have the truly final field list.
7974 Store it in this type and in the variants. */
7976 TYPE_FIELDS (t
) = fieldlist
;
7978 /* If there are lots of fields, sort so we can look through them fast.
7979 We arbitrarily consider 16 or more elts to be "a lot". */
7984 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
7986 if (len
> 15 || DECL_NAME (x
) == NULL
)
7994 struct lang_type
*space
;
7995 struct sorted_fields_type
*space2
;
7997 len
+= list_length (x
);
7999 /* Use the same allocation policy here that make_node uses, to
8000 ensure that this lives as long as the rest of the struct decl.
8001 All decls in an inline function need to be saved. */
8003 space
= ggc_cleared_alloc
<struct lang_type
> ();
8004 space2
= (sorted_fields_type
*) ggc_internal_alloc
8005 (sizeof (struct sorted_fields_type
) + len
* sizeof (tree
));
8009 field_array
= &space2
->elts
[0];
8010 for (x
= fieldlist
; x
; x
= DECL_CHAIN (x
))
8012 field_array
[len
++] = x
;
8014 /* If there is anonymous struct or union, break out of the loop. */
8015 if (DECL_NAME (x
) == NULL
)
8018 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8021 TYPE_LANG_SPECIFIC (t
) = space
;
8022 TYPE_LANG_SPECIFIC (t
)->s
->len
= len
;
8023 field_array
= TYPE_LANG_SPECIFIC (t
)->s
->elts
;
8024 qsort (field_array
, len
, sizeof (tree
), field_decl_cmp
);
8029 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8030 in dwarf2out via rest_of_decl_compilation below and means
8031 something totally different. Since we will be clearing
8032 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8033 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8034 C_TYPE_INCOMPLETE_VARS should use some language specific
8036 tree incomplete_vars
= C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t
));
8037 for (x
= TYPE_MAIN_VARIANT (t
); x
; x
= TYPE_NEXT_VARIANT (x
))
8039 TYPE_FIELDS (x
) = TYPE_FIELDS (t
);
8040 TYPE_LANG_SPECIFIC (x
) = TYPE_LANG_SPECIFIC (t
);
8041 C_TYPE_FIELDS_READONLY (x
) = C_TYPE_FIELDS_READONLY (t
);
8042 C_TYPE_FIELDS_VOLATILE (x
) = C_TYPE_FIELDS_VOLATILE (t
);
8043 C_TYPE_VARIABLE_SIZE (x
) = C_TYPE_VARIABLE_SIZE (t
);
8044 C_TYPE_INCOMPLETE_VARS (x
) = NULL_TREE
;
8047 /* If this was supposed to be a transparent union, but we can't
8048 make it one, warn and turn off the flag. */
8049 if (TREE_CODE (t
) == UNION_TYPE
8050 && TYPE_TRANSPARENT_AGGR (t
)
8051 && (!TYPE_FIELDS (t
) || TYPE_MODE (t
) != DECL_MODE (TYPE_FIELDS (t
))))
8053 TYPE_TRANSPARENT_AGGR (t
) = 0;
8054 warning_at (loc
, 0, "union cannot be made transparent");
8057 /* If this structure or union completes the type of any previous
8058 variable declaration, lay it out and output its rtl. */
8059 for (x
= incomplete_vars
; x
; x
= TREE_CHAIN (x
))
8061 tree decl
= TREE_VALUE (x
);
8062 if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
8063 layout_array_type (TREE_TYPE (decl
));
8064 if (TREE_CODE (decl
) != TYPE_DECL
)
8066 layout_decl (decl
, 0);
8067 if (c_dialect_objc ())
8068 objc_check_decl (decl
);
8069 rest_of_decl_compilation (decl
, toplevel
, 0);
8073 /* Update type location to the one of the definition, instead of e.g.
8074 a forward declaration. */
8075 if (TYPE_STUB_DECL (t
))
8076 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t
)) = loc
;
8078 /* Finish debugging output for this type. */
8079 rest_of_type_compilation (t
, toplevel
);
8081 /* If we're inside a function proper, i.e. not file-scope and not still
8082 parsing parameters, then arrange for the size of a variable sized type
8084 if (building_stmt_list_p () && variably_modified_type_p (t
, NULL_TREE
))
8085 add_stmt (build_stmt (loc
,
8086 DECL_EXPR
, build_decl (loc
, TYPE_DECL
, NULL
, t
)));
8088 if (warn_cxx_compat
)
8089 warn_cxx_compat_finish_struct (fieldlist
, TREE_CODE (t
), loc
);
8091 struct_parse_info
->struct_types
.release ();
8092 struct_parse_info
->fields
.release ();
8093 struct_parse_info
->typedefs_seen
.release ();
8094 XDELETE (struct_parse_info
);
8096 struct_parse_info
= enclosing_struct_parse_info
;
8098 /* If this struct is defined inside a struct, add it to
8101 && struct_parse_info
!= NULL
8102 && !in_sizeof
&& !in_typeof
&& !in_alignof
)
8103 struct_parse_info
->struct_types
.safe_push (t
);
8108 /* Lay out the type T, and its element type, and so on. */
8111 layout_array_type (tree t
)
8113 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
8114 layout_array_type (TREE_TYPE (t
));
8118 /* Begin compiling the definition of an enumeration type.
8119 NAME is its name (or null if anonymous).
8120 LOC is the enum's location.
8121 Returns the type object, as yet incomplete.
8122 Also records info about it so that build_enumerator
8123 may be used to declare the individual values as they are read. */
8126 start_enum (location_t loc
, struct c_enum_contents
*the_enum
, tree name
)
8128 tree enumtype
= NULL_TREE
;
8129 location_t enumloc
= UNKNOWN_LOCATION
;
8131 /* If this is the real definition for a previous forward reference,
8132 fill in the contents in the same object that used to be the
8133 forward reference. */
8135 if (name
!= NULL_TREE
)
8136 enumtype
= lookup_tag (ENUMERAL_TYPE
, name
, true, &enumloc
);
8138 if (enumtype
== NULL_TREE
|| TREE_CODE (enumtype
) != ENUMERAL_TYPE
)
8140 enumtype
= make_node (ENUMERAL_TYPE
);
8141 pushtag (loc
, name
, enumtype
);
8144 if (C_TYPE_BEING_DEFINED (enumtype
))
8145 error_at (loc
, "nested redefinition of %<enum %E%>", name
);
8147 C_TYPE_BEING_DEFINED (enumtype
) = 1;
8149 if (TYPE_VALUES (enumtype
) != 0)
8151 /* This enum is a named one that has been declared already. */
8152 error_at (loc
, "redeclaration of %<enum %E%>", name
);
8153 if (enumloc
!= UNKNOWN_LOCATION
)
8154 inform (enumloc
, "originally defined here");
8156 /* Completely replace its old definition.
8157 The old enumerators remain defined, however. */
8158 TYPE_VALUES (enumtype
) = 0;
8161 the_enum
->enum_next_value
= integer_zero_node
;
8162 the_enum
->enum_overflow
= 0;
8164 if (flag_short_enums
)
8165 for (tree v
= TYPE_MAIN_VARIANT (enumtype
); v
; v
= TYPE_NEXT_VARIANT (v
))
8166 TYPE_PACKED (v
) = 1;
8168 /* FIXME: This will issue a warning for a use of a type defined
8169 within sizeof in a statement expr. This is not terribly serious
8170 as C++ doesn't permit statement exprs within sizeof anyhow. */
8171 if (warn_cxx_compat
&& (in_sizeof
|| in_typeof
|| in_alignof
))
8172 warning_at (loc
, OPT_Wc___compat
,
8173 "defining type in %qs expression is invalid in C++",
8176 : (in_typeof
? "typeof" : "alignof")));
8181 /* After processing and defining all the values of an enumeration type,
8182 install their decls in the enumeration type and finish it off.
8183 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8184 and ATTRIBUTES are the specified attributes.
8185 Returns ENUMTYPE. */
8188 finish_enum (tree enumtype
, tree values
, tree attributes
)
8191 tree minnode
= 0, maxnode
= 0;
8194 bool toplevel
= (file_scope
== current_scope
);
8195 struct lang_type
*lt
;
8197 decl_attributes (&enumtype
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
8199 /* Calculate the maximum value of any enumerator in this type. */
8201 if (values
== error_mark_node
)
8202 minnode
= maxnode
= integer_zero_node
;
8205 minnode
= maxnode
= TREE_VALUE (values
);
8206 for (pair
= TREE_CHAIN (values
); pair
; pair
= TREE_CHAIN (pair
))
8208 tree value
= TREE_VALUE (pair
);
8209 if (tree_int_cst_lt (maxnode
, value
))
8211 if (tree_int_cst_lt (value
, minnode
))
8216 /* Construct the final type of this enumeration. It is the same
8217 as one of the integral types - the narrowest one that fits, except
8218 that normally we only go as narrow as int - and signed iff any of
8219 the values are negative. */
8220 sign
= (tree_int_cst_sgn (minnode
) >= 0) ? UNSIGNED
: SIGNED
;
8221 precision
= MAX (tree_int_cst_min_precision (minnode
, sign
),
8222 tree_int_cst_min_precision (maxnode
, sign
));
8224 /* If the precision of the type was specified with an attribute and it
8225 was too small, give an error. Otherwise, use it. */
8226 if (TYPE_PRECISION (enumtype
) && lookup_attribute ("mode", attributes
))
8228 if (precision
> TYPE_PRECISION (enumtype
))
8230 TYPE_PRECISION (enumtype
) = 0;
8231 error ("specified mode too small for enumeral values");
8234 precision
= TYPE_PRECISION (enumtype
);
8237 TYPE_PRECISION (enumtype
) = 0;
8239 if (TYPE_PACKED (enumtype
)
8240 || precision
> TYPE_PRECISION (integer_type_node
)
8241 || TYPE_PRECISION (enumtype
))
8243 tem
= c_common_type_for_size (precision
, sign
== UNSIGNED
? 1 : 0);
8246 warning (0, "enumeration values exceed range of largest integer");
8247 tem
= long_long_integer_type_node
;
8251 tem
= sign
== UNSIGNED
? unsigned_type_node
: integer_type_node
;
8253 TYPE_MIN_VALUE (enumtype
) = TYPE_MIN_VALUE (tem
);
8254 TYPE_MAX_VALUE (enumtype
) = TYPE_MAX_VALUE (tem
);
8255 TYPE_UNSIGNED (enumtype
) = TYPE_UNSIGNED (tem
);
8256 SET_TYPE_ALIGN (enumtype
, TYPE_ALIGN (tem
));
8257 TYPE_SIZE (enumtype
) = 0;
8258 TYPE_PRECISION (enumtype
) = TYPE_PRECISION (tem
);
8260 layout_type (enumtype
);
8262 if (values
!= error_mark_node
)
8264 /* Change the type of the enumerators to be the enum type. We
8265 need to do this irrespective of the size of the enum, for
8266 proper type checking. Replace the DECL_INITIALs of the
8267 enumerators, and the value slots of the list, with copies
8268 that have the enum type; they cannot be modified in place
8269 because they may be shared (e.g. integer_zero_node) Finally,
8270 change the purpose slots to point to the names of the decls. */
8271 for (pair
= values
; pair
; pair
= TREE_CHAIN (pair
))
8273 tree enu
= TREE_PURPOSE (pair
);
8274 tree ini
= DECL_INITIAL (enu
);
8276 TREE_TYPE (enu
) = enumtype
;
8278 /* The ISO C Standard mandates enumerators to have type int,
8279 even though the underlying type of an enum type is
8280 unspecified. However, GCC allows enumerators of any
8281 integer type as an extensions. build_enumerator()
8282 converts any enumerators that fit in an int to type int,
8283 to avoid promotions to unsigned types when comparing
8284 integers with enumerators that fit in the int range.
8285 When -pedantic is given, build_enumerator() would have
8286 already warned about those that don't fit. Here we
8287 convert the rest to the enumerator type. */
8288 if (TREE_TYPE (ini
) != integer_type_node
)
8289 ini
= convert (enumtype
, ini
);
8291 DECL_INITIAL (enu
) = ini
;
8292 TREE_PURPOSE (pair
) = DECL_NAME (enu
);
8293 TREE_VALUE (pair
) = ini
;
8296 TYPE_VALUES (enumtype
) = values
;
8299 /* Record the min/max values so that we can warn about bit-field
8300 enumerations that are too small for the values. */
8301 lt
= ggc_cleared_alloc
<struct lang_type
> ();
8302 lt
->enum_min
= minnode
;
8303 lt
->enum_max
= maxnode
;
8304 TYPE_LANG_SPECIFIC (enumtype
) = lt
;
8306 /* Fix up all variant types of this enum type. */
8307 for (tem
= TYPE_MAIN_VARIANT (enumtype
); tem
; tem
= TYPE_NEXT_VARIANT (tem
))
8309 if (tem
== enumtype
)
8311 TYPE_VALUES (tem
) = TYPE_VALUES (enumtype
);
8312 TYPE_MIN_VALUE (tem
) = TYPE_MIN_VALUE (enumtype
);
8313 TYPE_MAX_VALUE (tem
) = TYPE_MAX_VALUE (enumtype
);
8314 TYPE_SIZE (tem
) = TYPE_SIZE (enumtype
);
8315 TYPE_SIZE_UNIT (tem
) = TYPE_SIZE_UNIT (enumtype
);
8316 SET_TYPE_MODE (tem
, TYPE_MODE (enumtype
));
8317 TYPE_PRECISION (tem
) = TYPE_PRECISION (enumtype
);
8318 SET_TYPE_ALIGN (tem
, TYPE_ALIGN (enumtype
));
8319 TYPE_USER_ALIGN (tem
) = TYPE_USER_ALIGN (enumtype
);
8320 TYPE_UNSIGNED (tem
) = TYPE_UNSIGNED (enumtype
);
8321 TYPE_LANG_SPECIFIC (tem
) = TYPE_LANG_SPECIFIC (enumtype
);
8324 /* Finish debugging output for this type. */
8325 rest_of_type_compilation (enumtype
, toplevel
);
8327 /* If this enum is defined inside a struct, add it to
8330 && struct_parse_info
!= NULL
8331 && !in_sizeof
&& !in_typeof
&& !in_alignof
)
8332 struct_parse_info
->struct_types
.safe_push (enumtype
);
8337 /* Build and install a CONST_DECL for one value of the
8338 current enumeration type (one that was begun with start_enum).
8339 DECL_LOC is the location of the enumerator.
8340 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8341 Return a tree-list containing the CONST_DECL and its value.
8342 Assignment of sequential values by default is handled here. */
8345 build_enumerator (location_t decl_loc
, location_t loc
,
8346 struct c_enum_contents
*the_enum
, tree name
, tree value
)
8350 /* Validate and default VALUE. */
8354 /* Don't issue more errors for error_mark_node (i.e. an
8355 undeclared identifier) - just ignore the value expression. */
8356 if (value
== error_mark_node
)
8358 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value
)))
8360 error_at (loc
, "enumerator value for %qE is not an integer constant",
8366 if (TREE_CODE (value
) != INTEGER_CST
)
8368 value
= c_fully_fold (value
, false, NULL
);
8369 if (TREE_CODE (value
) == INTEGER_CST
)
8370 pedwarn (loc
, OPT_Wpedantic
,
8371 "enumerator value for %qE is not an integer "
8372 "constant expression", name
);
8374 if (TREE_CODE (value
) != INTEGER_CST
)
8376 error ("enumerator value for %qE is not an integer constant",
8382 value
= default_conversion (value
);
8383 constant_expression_warning (value
);
8388 /* Default based on previous value. */
8389 /* It should no longer be possible to have NON_LVALUE_EXPR
8393 value
= the_enum
->enum_next_value
;
8394 if (the_enum
->enum_overflow
)
8395 error_at (loc
, "overflow in enumeration values");
8397 /* Even though the underlying type of an enum is unspecified, the
8398 type of enumeration constants is explicitly defined as int
8399 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8401 else if (!int_fits_type_p (value
, integer_type_node
))
8402 pedwarn (loc
, OPT_Wpedantic
,
8403 "ISO C restricts enumerator values to range of %<int%>");
8405 /* The ISO C Standard mandates enumerators to have type int, even
8406 though the underlying type of an enum type is unspecified.
8407 However, GCC allows enumerators of any integer type as an
8408 extensions. Here we convert any enumerators that fit in an int
8409 to type int, to avoid promotions to unsigned types when comparing
8410 integers with enumerators that fit in the int range. When
8411 -pedantic is given, we would have already warned about those that
8412 don't fit. We have to do this here rather than in finish_enum
8413 because this value may be used to define more enumerators. */
8414 if (int_fits_type_p (value
, integer_type_node
))
8415 value
= convert (integer_type_node
, value
);
8417 /* Set basis for default for next value. */
8418 the_enum
->enum_next_value
8419 = build_binary_op (EXPR_LOC_OR_LOC (value
, input_location
),
8420 PLUS_EXPR
, value
, integer_one_node
, 0);
8421 the_enum
->enum_overflow
= tree_int_cst_lt (the_enum
->enum_next_value
, value
);
8423 /* Now create a declaration for the enum value name. */
8425 type
= TREE_TYPE (value
);
8426 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
8427 TYPE_PRECISION (integer_type_node
)),
8428 (TYPE_PRECISION (type
)
8429 >= TYPE_PRECISION (integer_type_node
)
8430 && TYPE_UNSIGNED (type
)));
8432 decl
= build_decl (decl_loc
, CONST_DECL
, name
, type
);
8433 DECL_INITIAL (decl
) = convert (type
, value
);
8436 return tree_cons (decl
, value
, NULL_TREE
);
8440 /* Create the FUNCTION_DECL for a function definition.
8441 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8442 the declaration; they describe the function's name and the type it returns,
8443 but twisted together in a fashion that parallels the syntax of C.
8445 This function creates a binding context for the function body
8446 as well as setting up the FUNCTION_DECL in current_function_decl.
8448 Returns 1 on success. If the DECLARATOR is not suitable for a function
8449 (it defines a datum instead), we return 0, which tells
8450 yyparse to report a parse error. */
8453 start_function (struct c_declspecs
*declspecs
, struct c_declarator
*declarator
,
8456 tree decl1
, old_decl
;
8457 tree restype
, resdecl
;
8460 current_function_returns_value
= 0; /* Assume, until we see it does. */
8461 current_function_returns_null
= 0;
8462 current_function_returns_abnormally
= 0;
8463 warn_about_return_type
= 0;
8464 c_switch_stack
= NULL
;
8466 /* Indicate no valid break/continue context by setting these variables
8467 to some non-null, non-label value. We'll notice and emit the proper
8468 error message in c_finish_bc_stmt. */
8469 c_break_label
= c_cont_label
= size_zero_node
;
8471 decl1
= grokdeclarator (declarator
, declspecs
, FUNCDEF
, true, NULL
,
8472 &attributes
, NULL
, NULL
, DEPRECATED_NORMAL
);
8473 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION
, decl1
);
8475 /* If the declarator is not suitable for a function definition,
8476 cause a syntax error. */
8478 || TREE_CODE (decl1
) != FUNCTION_DECL
)
8481 loc
= DECL_SOURCE_LOCATION (decl1
);
8483 c_decl_attributes (&decl1
, attributes
, 0);
8485 if (DECL_DECLARED_INLINE_P (decl1
)
8486 && DECL_UNINLINABLE (decl1
)
8487 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1
)))
8488 warning_at (loc
, OPT_Wattributes
,
8489 "inline function %qD given attribute noinline",
8492 /* Handle gnu_inline attribute. */
8493 if (declspecs
->inline_p
8494 && !flag_gnu89_inline
8495 && TREE_CODE (decl1
) == FUNCTION_DECL
8496 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1
))
8497 || current_function_decl
))
8499 if (declspecs
->storage_class
!= csc_static
)
8500 DECL_EXTERNAL (decl1
) = !DECL_EXTERNAL (decl1
);
8503 announce_function (decl1
);
8505 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1
))))
8507 error_at (loc
, "return type is an incomplete type");
8508 /* Make it return void instead. */
8510 = build_function_type (void_type_node
,
8511 TYPE_ARG_TYPES (TREE_TYPE (decl1
)));
8514 if (warn_about_return_type
)
8515 warn_defaults_to (loc
, flag_isoc99
? OPT_Wimplicit_int
8516 : (warn_return_type
? OPT_Wreturn_type
8517 : OPT_Wimplicit_int
),
8518 "return type defaults to %<int%>");
8520 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8521 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8522 DECL_INITIAL (decl1
) = error_mark_node
;
8524 /* A nested function is not global. */
8525 if (current_function_decl
!= 0)
8526 TREE_PUBLIC (decl1
) = 0;
8528 /* If this definition isn't a prototype and we had a prototype declaration
8529 before, copy the arg type info from that prototype. */
8530 old_decl
= lookup_name_in_scope (DECL_NAME (decl1
), current_scope
);
8531 if (old_decl
&& TREE_CODE (old_decl
) != FUNCTION_DECL
)
8533 current_function_prototype_locus
= UNKNOWN_LOCATION
;
8534 current_function_prototype_built_in
= false;
8535 current_function_prototype_arg_types
= NULL_TREE
;
8536 if (!prototype_p (TREE_TYPE (decl1
)))
8538 if (old_decl
!= 0 && TREE_CODE (TREE_TYPE (old_decl
)) == FUNCTION_TYPE
8539 && comptypes (TREE_TYPE (TREE_TYPE (decl1
)),
8540 TREE_TYPE (TREE_TYPE (old_decl
))))
8542 if (stdarg_p (TREE_TYPE (old_decl
)))
8544 warning_at (loc
, 0, "%q+D defined as variadic function "
8545 "without prototype", decl1
);
8546 locate_old_decl (old_decl
);
8548 TREE_TYPE (decl1
) = composite_type (TREE_TYPE (old_decl
),
8550 current_function_prototype_locus
= DECL_SOURCE_LOCATION (old_decl
);
8551 current_function_prototype_built_in
8552 = C_DECL_BUILTIN_PROTOTYPE (old_decl
);
8553 current_function_prototype_arg_types
8554 = TYPE_ARG_TYPES (TREE_TYPE (decl1
));
8556 if (TREE_PUBLIC (decl1
))
8558 /* If there is an external prototype declaration of this
8559 function, record its location but do not copy information
8560 to this decl. This may be an invisible declaration
8561 (built-in or in a scope which has finished) or simply
8562 have more refined argument types than any declaration
8564 struct c_binding
*b
;
8565 for (b
= I_SYMBOL_BINDING (DECL_NAME (decl1
)); b
; b
= b
->shadowed
)
8566 if (B_IN_SCOPE (b
, external_scope
))
8570 tree ext_decl
, ext_type
;
8572 ext_type
= b
->u
.type
? b
->u
.type
: TREE_TYPE (ext_decl
);
8573 if (TREE_CODE (ext_type
) == FUNCTION_TYPE
8574 && comptypes (TREE_TYPE (TREE_TYPE (decl1
)),
8575 TREE_TYPE (ext_type
)))
8577 current_function_prototype_locus
8578 = DECL_SOURCE_LOCATION (ext_decl
);
8579 current_function_prototype_built_in
8580 = C_DECL_BUILTIN_PROTOTYPE (ext_decl
);
8581 current_function_prototype_arg_types
8582 = TYPE_ARG_TYPES (ext_type
);
8588 /* Optionally warn of old-fashioned def with no previous prototype. */
8589 if (warn_strict_prototypes
8590 && old_decl
!= error_mark_node
8591 && !prototype_p (TREE_TYPE (decl1
))
8592 && C_DECL_ISNT_PROTOTYPE (old_decl
))
8593 warning_at (loc
, OPT_Wstrict_prototypes
,
8594 "function declaration isn%'t a prototype");
8595 /* Optionally warn of any global def with no previous prototype. */
8596 else if (warn_missing_prototypes
8597 && old_decl
!= error_mark_node
8598 && TREE_PUBLIC (decl1
)
8599 && !MAIN_NAME_P (DECL_NAME (decl1
))
8600 && C_DECL_ISNT_PROTOTYPE (old_decl
)
8601 && !DECL_DECLARED_INLINE_P (decl1
))
8602 warning_at (loc
, OPT_Wmissing_prototypes
,
8603 "no previous prototype for %qD", decl1
);
8604 /* Optionally warn of any def with no previous prototype
8605 if the function has already been used. */
8606 else if (warn_missing_prototypes
8608 && old_decl
!= error_mark_node
8609 && TREE_USED (old_decl
)
8610 && !prototype_p (TREE_TYPE (old_decl
)))
8611 warning_at (loc
, OPT_Wmissing_prototypes
,
8612 "%qD was used with no prototype before its definition", decl1
);
8613 /* Optionally warn of any global def with no previous declaration. */
8614 else if (warn_missing_declarations
8615 && TREE_PUBLIC (decl1
)
8617 && !MAIN_NAME_P (DECL_NAME (decl1
))
8618 && !DECL_DECLARED_INLINE_P (decl1
))
8619 warning_at (loc
, OPT_Wmissing_declarations
,
8620 "no previous declaration for %qD",
8622 /* Optionally warn of any def with no previous declaration
8623 if the function has already been used. */
8624 else if (warn_missing_declarations
8626 && old_decl
!= error_mark_node
8627 && TREE_USED (old_decl
)
8628 && C_DECL_IMPLICIT (old_decl
))
8629 warning_at (loc
, OPT_Wmissing_declarations
,
8630 "%qD was used with no declaration before its definition", decl1
);
8632 /* This function exists in static storage.
8633 (This does not mean `static' in the C sense!) */
8634 TREE_STATIC (decl1
) = 1;
8636 /* This is the earliest point at which we might know the assembler
8637 name of the function. Thus, if it's set before this, die horribly. */
8638 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1
));
8640 /* If #pragma weak was used, mark the decl weak now. */
8641 if (current_scope
== file_scope
)
8642 maybe_apply_pragma_weak (decl1
);
8644 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8645 if (warn_main
&& MAIN_NAME_P (DECL_NAME (decl1
)))
8647 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1
)))
8648 != integer_type_node
)
8649 pedwarn (loc
, OPT_Wmain
, "return type of %qD is not %<int%>", decl1
);
8650 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1
))))
8651 pedwarn (loc
, OPT_Wmain
, "%<_Atomic%>-qualified return type of %qD",
8654 check_main_parameter_types (decl1
);
8656 if (!TREE_PUBLIC (decl1
))
8657 pedwarn (loc
, OPT_Wmain
,
8658 "%qD is normally a non-static function", decl1
);
8661 /* Record the decl so that the function name is defined.
8662 If we already have a decl for this name, and it is a FUNCTION_DECL,
8663 use the old decl. */
8665 current_function_decl
= pushdecl (decl1
);
8668 declare_parm_level ();
8670 restype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
8671 resdecl
= build_decl (loc
, RESULT_DECL
, NULL_TREE
, restype
);
8672 DECL_ARTIFICIAL (resdecl
) = 1;
8673 DECL_IGNORED_P (resdecl
) = 1;
8674 DECL_RESULT (current_function_decl
) = resdecl
;
8676 start_fname_decls ();
8681 /* Subroutine of store_parm_decls which handles new-style function
8682 definitions (prototype format). The parms already have decls, so we
8683 need only record them as in effect and complain if any redundant
8684 old-style parm decls were written. */
8686 store_parm_decls_newstyle (tree fndecl
, const struct c_arg_info
*arg_info
)
8692 if (current_scope
->bindings
)
8694 error_at (DECL_SOURCE_LOCATION (fndecl
),
8695 "old-style parameter declarations in prototyped "
8696 "function definition");
8698 /* Get rid of the old-style declarations. */
8702 /* Don't issue this warning for nested functions, and don't issue this
8703 warning if we got here because ARG_INFO_TYPES was error_mark_node
8704 (this happens when a function definition has just an ellipsis in
8705 its parameter list). */
8706 else if (!in_system_header_at (input_location
)
8707 && !current_function_scope
8708 && arg_info
->types
!= error_mark_node
)
8709 warning_at (DECL_SOURCE_LOCATION (fndecl
), OPT_Wtraditional
,
8710 "traditional C rejects ISO C style function definitions");
8712 /* Now make all the parameter declarations visible in the function body.
8713 We can bypass most of the grunt work of pushdecl. */
8714 for (decl
= arg_info
->parms
; decl
; decl
= DECL_CHAIN (decl
))
8716 DECL_CONTEXT (decl
) = current_function_decl
;
8717 if (DECL_NAME (decl
))
8719 bind (DECL_NAME (decl
), decl
, current_scope
,
8720 /*invisible=*/false, /*nested=*/false,
8722 if (!TREE_USED (decl
))
8723 warn_if_shadowing (decl
);
8726 error_at (DECL_SOURCE_LOCATION (decl
), "parameter name omitted");
8729 /* Record the parameter list in the function declaration. */
8730 DECL_ARGUMENTS (fndecl
) = arg_info
->parms
;
8732 /* Now make all the ancillary declarations visible, likewise. */
8733 for (decl
= arg_info
->others
; decl
; decl
= DECL_CHAIN (decl
))
8735 DECL_CONTEXT (decl
) = current_function_decl
;
8736 if (DECL_NAME (decl
))
8737 bind (DECL_NAME (decl
), decl
, current_scope
,
8738 /*invisible=*/false,
8739 /*nested=*/(TREE_CODE (decl
) == FUNCTION_DECL
),
8743 /* And all the tag declarations. */
8744 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info
->tags
, ix
, tag
)
8746 bind (tag
->id
, tag
->type
, current_scope
,
8747 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
8750 /* Subroutine of store_parm_decls which handles old-style function
8751 definitions (separate parameter list and declarations). */
8754 store_parm_decls_oldstyle (tree fndecl
, const struct c_arg_info
*arg_info
)
8756 struct c_binding
*b
;
8757 tree parm
, decl
, last
;
8758 tree parmids
= arg_info
->parms
;
8759 hash_set
<tree
> seen_args
;
8761 if (!in_system_header_at (input_location
))
8762 warning_at (DECL_SOURCE_LOCATION (fndecl
),
8763 OPT_Wold_style_definition
, "old-style function definition");
8765 /* Match each formal parameter name with its declaration. Save each
8766 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8767 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
8769 if (TREE_VALUE (parm
) == 0)
8771 error_at (DECL_SOURCE_LOCATION (fndecl
),
8772 "parameter name missing from parameter list");
8773 TREE_PURPOSE (parm
) = 0;
8777 b
= I_SYMBOL_BINDING (TREE_VALUE (parm
));
8778 if (b
&& B_IN_CURRENT_SCOPE (b
))
8781 /* Skip erroneous parameters. */
8782 if (decl
== error_mark_node
)
8784 /* If we got something other than a PARM_DECL it is an error. */
8785 if (TREE_CODE (decl
) != PARM_DECL
)
8787 error_at (DECL_SOURCE_LOCATION (decl
),
8788 "%qD declared as a non-parameter", decl
);
8791 /* If the declaration is already marked, we have a duplicate
8792 name. Complain and ignore the duplicate. */
8793 else if (seen_args
.contains (decl
))
8795 error_at (DECL_SOURCE_LOCATION (decl
),
8796 "multiple parameters named %qD", decl
);
8797 TREE_PURPOSE (parm
) = 0;
8800 /* If the declaration says "void", complain and turn it into
8802 else if (VOID_TYPE_P (TREE_TYPE (decl
)))
8804 error_at (DECL_SOURCE_LOCATION (decl
),
8805 "parameter %qD declared with void type", decl
);
8806 TREE_TYPE (decl
) = integer_type_node
;
8807 DECL_ARG_TYPE (decl
) = integer_type_node
;
8808 layout_decl (decl
, 0);
8810 warn_if_shadowing (decl
);
8812 /* If no declaration found, default to int. */
8815 /* FIXME diagnostics: This should be the location of the argument,
8816 not the FNDECL. E.g., for an old-style declaration
8818 int f10(v) { blah; }
8820 We should use the location of the V, not the F10.
8821 Unfortunately, the V is an IDENTIFIER_NODE which has no
8822 location. In the future we need locations for c_arg_info
8825 See gcc.dg/Wshadow-3.c for an example of this problem. */
8826 decl
= build_decl (DECL_SOURCE_LOCATION (fndecl
),
8827 PARM_DECL
, TREE_VALUE (parm
), integer_type_node
);
8828 DECL_ARG_TYPE (decl
) = TREE_TYPE (decl
);
8830 warn_if_shadowing (decl
);
8833 pedwarn (DECL_SOURCE_LOCATION (decl
),
8834 OPT_Wimplicit_int
, "type of %qD defaults to %<int%>",
8837 warning_at (DECL_SOURCE_LOCATION (decl
),
8838 OPT_Wmissing_parameter_type
,
8839 "type of %qD defaults to %<int%>", decl
);
8842 TREE_PURPOSE (parm
) = decl
;
8843 seen_args
.add (decl
);
8846 /* Now examine the parms chain for incomplete declarations
8847 and declarations with no corresponding names. */
8849 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
8852 if (TREE_CODE (parm
) != PARM_DECL
)
8855 if (TREE_TYPE (parm
) != error_mark_node
8856 && !COMPLETE_TYPE_P (TREE_TYPE (parm
)))
8858 error_at (DECL_SOURCE_LOCATION (parm
),
8859 "parameter %qD has incomplete type", parm
);
8860 TREE_TYPE (parm
) = error_mark_node
;
8863 if (!seen_args
.contains (parm
))
8865 error_at (DECL_SOURCE_LOCATION (parm
),
8866 "declaration for parameter %qD but no such parameter",
8869 /* Pretend the parameter was not missing.
8870 This gets us to a standard state and minimizes
8871 further error messages. */
8872 parmids
= chainon (parmids
, tree_cons (parm
, 0, 0));
8876 /* Chain the declarations together in the order of the list of
8877 names. Store that chain in the function decl, replacing the
8878 list of names. Update the current scope to match. */
8879 DECL_ARGUMENTS (fndecl
) = 0;
8881 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
8882 if (TREE_PURPOSE (parm
))
8884 if (parm
&& TREE_PURPOSE (parm
))
8886 last
= TREE_PURPOSE (parm
);
8887 DECL_ARGUMENTS (fndecl
) = last
;
8889 for (parm
= TREE_CHAIN (parm
); parm
; parm
= TREE_CHAIN (parm
))
8890 if (TREE_PURPOSE (parm
))
8892 DECL_CHAIN (last
) = TREE_PURPOSE (parm
);
8893 last
= TREE_PURPOSE (parm
);
8895 DECL_CHAIN (last
) = 0;
8898 /* If there was a previous prototype,
8899 set the DECL_ARG_TYPE of each argument according to
8900 the type previously specified, and report any mismatches. */
8902 if (current_function_prototype_arg_types
)
8905 for (parm
= DECL_ARGUMENTS (fndecl
),
8906 type
= current_function_prototype_arg_types
;
8907 parm
|| (type
&& TREE_VALUE (type
) != error_mark_node
8908 && (TYPE_MAIN_VARIANT (TREE_VALUE (type
)) != void_type_node
));
8909 parm
= DECL_CHAIN (parm
), type
= TREE_CHAIN (type
))
8911 if (parm
== 0 || type
== 0
8912 || TYPE_MAIN_VARIANT (TREE_VALUE (type
)) == void_type_node
)
8914 if (current_function_prototype_built_in
)
8915 warning_at (DECL_SOURCE_LOCATION (fndecl
),
8916 0, "number of arguments doesn%'t match "
8917 "built-in prototype");
8920 /* FIXME diagnostics: This should be the location of
8921 FNDECL, but there is bug when a prototype is
8922 declared inside function context, but defined
8923 outside of it (e.g., gcc.dg/pr15698-2.c). In
8924 which case FNDECL gets the location of the
8925 prototype, not the definition. */
8926 error_at (input_location
,
8927 "number of arguments doesn%'t match prototype");
8929 error_at (current_function_prototype_locus
,
8930 "prototype declaration");
8934 /* Type for passing arg must be consistent with that
8935 declared for the arg. ISO C says we take the unqualified
8936 type for parameters declared with qualified type. */
8937 if (TREE_TYPE (parm
) != error_mark_node
8938 && TREE_TYPE (type
) != error_mark_node
8939 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm
))
8940 != TYPE_ATOMIC (TREE_VALUE (type
)))
8941 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm
)),
8942 TYPE_MAIN_VARIANT (TREE_VALUE (type
)))))
8944 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm
))
8945 == TYPE_ATOMIC (TREE_VALUE (type
)))
8946 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm
))
8947 == TYPE_MAIN_VARIANT (TREE_VALUE (type
))))
8949 /* Adjust argument to match prototype. E.g. a previous
8950 `int foo(float);' prototype causes
8951 `int foo(x) float x; {...}' to be treated like
8952 `int foo(float x) {...}'. This is particularly
8953 useful for argument types like uid_t. */
8954 DECL_ARG_TYPE (parm
) = TREE_TYPE (parm
);
8956 if (targetm
.calls
.promote_prototypes (TREE_TYPE (current_function_decl
))
8957 && INTEGRAL_TYPE_P (TREE_TYPE (parm
))
8958 && TYPE_PRECISION (TREE_TYPE (parm
))
8959 < TYPE_PRECISION (integer_type_node
))
8960 DECL_ARG_TYPE (parm
)
8961 = c_type_promotes_to (TREE_TYPE (parm
));
8963 /* ??? Is it possible to get here with a
8964 built-in prototype or will it always have
8965 been diagnosed as conflicting with an
8966 old-style definition and discarded? */
8967 if (current_function_prototype_built_in
)
8968 warning_at (DECL_SOURCE_LOCATION (parm
),
8969 OPT_Wpedantic
, "promoted argument %qD "
8970 "doesn%'t match built-in prototype", parm
);
8973 pedwarn (DECL_SOURCE_LOCATION (parm
),
8974 OPT_Wpedantic
, "promoted argument %qD "
8975 "doesn%'t match prototype", parm
);
8976 pedwarn (current_function_prototype_locus
, OPT_Wpedantic
,
8977 "prototype declaration");
8982 if (current_function_prototype_built_in
)
8983 warning_at (DECL_SOURCE_LOCATION (parm
),
8984 0, "argument %qD doesn%'t match "
8985 "built-in prototype", parm
);
8988 error_at (DECL_SOURCE_LOCATION (parm
),
8989 "argument %qD doesn%'t match prototype", parm
);
8990 error_at (current_function_prototype_locus
,
8991 "prototype declaration");
8996 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = 0;
8999 /* Otherwise, create a prototype that would match. */
9003 tree actual
= 0, last
= 0, type
;
9005 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= DECL_CHAIN (parm
))
9007 type
= tree_cons (NULL_TREE
, DECL_ARG_TYPE (parm
), NULL_TREE
);
9009 TREE_CHAIN (last
) = type
;
9014 type
= tree_cons (NULL_TREE
, void_type_node
, NULL_TREE
);
9016 TREE_CHAIN (last
) = type
;
9020 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9021 of the type of this function, but we need to avoid having this
9022 affect the types of other similarly-typed functions, so we must
9023 first force the generation of an identical (but separate) type
9024 node for the relevant function type. The new node we create
9025 will be a variant of the main variant of the original function
9028 TREE_TYPE (fndecl
) = build_variant_type_copy (TREE_TYPE (fndecl
));
9030 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = actual
;
9034 /* Store parameter declarations passed in ARG_INFO into the current
9035 function declaration. */
9038 store_parm_decls_from (struct c_arg_info
*arg_info
)
9040 current_function_arg_info
= arg_info
;
9041 store_parm_decls ();
9044 /* Called by walk_tree to look for and update context-less labels. */
9047 set_labels_context_r (tree
*tp
, int *walk_subtrees
, void *data
)
9049 if (TREE_CODE (*tp
) == LABEL_EXPR
9050 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp
)) == NULL_TREE
)
9052 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp
)) = static_cast<tree
>(data
);
9059 /* Store the parameter declarations into the current function declaration.
9060 This is called after parsing the parameter declarations, before
9061 digesting the body of the function.
9063 For an old-style definition, construct a prototype out of the old-style
9064 parameter declarations and inject it into the function's type. */
9067 store_parm_decls (void)
9069 tree fndecl
= current_function_decl
;
9072 /* The argument information block for FNDECL. */
9073 struct c_arg_info
*arg_info
= current_function_arg_info
;
9074 current_function_arg_info
= 0;
9076 /* True if this definition is written with a prototype. Note:
9077 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9078 list in a function definition as equivalent to (void) -- an
9079 empty argument list specifies the function has no parameters,
9080 but only (void) sets up a prototype for future calls. */
9081 proto
= arg_info
->types
!= 0;
9084 store_parm_decls_newstyle (fndecl
, arg_info
);
9086 store_parm_decls_oldstyle (fndecl
, arg_info
);
9088 /* The next call to push_scope will be a function body. */
9090 next_is_function_body
= true;
9092 /* Write a record describing this function definition to the prototypes
9093 file (if requested). */
9095 gen_aux_info_record (fndecl
, 1, 0, proto
);
9097 /* Initialize the RTL code for the function. */
9098 allocate_struct_function (fndecl
, false);
9100 if (warn_unused_local_typedefs
)
9101 cfun
->language
= ggc_cleared_alloc
<language_function
> ();
9103 /* Begin the statement tree for this function. */
9104 DECL_SAVED_TREE (fndecl
) = push_stmt_list ();
9106 /* ??? Insert the contents of the pending sizes list into the function
9107 to be evaluated. The only reason left to have this is
9108 void foo(int n, int array[n++])
9109 because we throw away the array type in favor of a pointer type, and
9110 thus won't naturally see the SAVE_EXPR containing the increment. All
9111 other pending sizes would be handled by gimplify_parameters. */
9112 if (arg_info
->pending_sizes
)
9114 /* In very special circumstances, e.g. for code like
9116 void f (int a[i += 2]) {}
9117 we need to execute the atomic assignment on function entry.
9118 But in this case, it is not just a straight store, it has the
9119 op= form, which means that build_atomic_assign has generated
9120 gotos, labels, etc. Because at that time the function decl
9121 for F has not been created yet, those labels do not have any
9122 function context. But we have the fndecl now, so update the
9123 labels accordingly. gimplify_expr would crash otherwise. */
9124 walk_tree_without_duplicates (&arg_info
->pending_sizes
,
9125 set_labels_context_r
, fndecl
);
9126 add_stmt (arg_info
->pending_sizes
);
9130 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9131 c_finish_omp_declare_simd for function prototypes. No diagnostics
9135 temp_store_parm_decls (tree fndecl
, tree parms
)
9138 for (tree p
= parms
; p
; p
= DECL_CHAIN (p
))
9140 DECL_CONTEXT (p
) = fndecl
;
9142 bind (DECL_NAME (p
), p
, current_scope
,
9143 /*invisible=*/false, /*nested=*/false,
9148 /* Undo what temp_store_parm_decls did. */
9151 temp_pop_parm_decls (void)
9153 /* Clear all bindings in this temporary scope, so that
9154 pop_scope doesn't create a BLOCK. */
9155 struct c_binding
*b
= current_scope
->bindings
;
9156 current_scope
->bindings
= NULL
;
9157 for (; b
; b
= free_binding_and_advance (b
))
9159 gcc_assert (TREE_CODE (b
->decl
) == PARM_DECL
9160 || b
->decl
== error_mark_node
);
9161 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
9162 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
9163 if (b
->shadowed
&& b
->shadowed
->u
.type
)
9164 TREE_TYPE (b
->shadowed
->decl
) = b
->shadowed
->u
.type
;
9170 /* Finish up a function declaration and compile that function
9171 all the way to assembler language output. Then free the storage
9172 for the function definition.
9174 This is called after parsing the body of the function definition. */
9177 finish_function (void)
9179 tree fndecl
= current_function_decl
;
9181 if (c_dialect_objc ())
9182 objc_finish_function ();
9184 if (TREE_CODE (fndecl
) == FUNCTION_DECL
9185 && targetm
.calls
.promote_prototypes (TREE_TYPE (fndecl
)))
9187 tree args
= DECL_ARGUMENTS (fndecl
);
9188 for (; args
; args
= DECL_CHAIN (args
))
9190 tree type
= TREE_TYPE (args
);
9191 if (INTEGRAL_TYPE_P (type
)
9192 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
9193 DECL_ARG_TYPE (args
) = c_type_promotes_to (type
);
9197 if (DECL_INITIAL (fndecl
) && DECL_INITIAL (fndecl
) != error_mark_node
)
9198 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
9200 /* Must mark the RESULT_DECL as being in this function. */
9202 if (DECL_RESULT (fndecl
) && DECL_RESULT (fndecl
) != error_mark_node
)
9203 DECL_CONTEXT (DECL_RESULT (fndecl
)) = fndecl
;
9205 if (MAIN_NAME_P (DECL_NAME (fndecl
)) && flag_hosted
9206 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl
)))
9207 == integer_type_node
&& flag_isoc99
)
9209 /* Hack. We don't want the middle-end to warn that this return
9210 is unreachable, so we mark its location as special. Using
9211 UNKNOWN_LOCATION has the problem that it gets clobbered in
9212 annotate_one_with_locus. A cleaner solution might be to
9213 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9215 c_finish_return (BUILTINS_LOCATION
, integer_zero_node
, NULL_TREE
);
9218 /* Tie off the statement tree for this function. */
9219 DECL_SAVED_TREE (fndecl
) = pop_stmt_list (DECL_SAVED_TREE (fndecl
));
9221 /* If the function has _Cilk_spawn in front of a function call inside it
9222 i.e. it is a spawning function, then add the appropriate Cilk plus
9223 functions inside. */
9224 if (fn_contains_cilk_spawn_p (cfun
))
9225 cfun
->cilk_frame_decl
= insert_cilk_frame (fndecl
);
9227 finish_fname_decls ();
9229 /* Complain if there's just no return statement. */
9230 if (warn_return_type
9231 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl
))) != VOID_TYPE
9232 && !current_function_returns_value
&& !current_function_returns_null
9233 /* Don't complain if we are no-return. */
9234 && !current_function_returns_abnormally
9235 /* Don't complain if we are declared noreturn. */
9236 && !TREE_THIS_VOLATILE (fndecl
)
9237 /* Don't warn for main(). */
9238 && !MAIN_NAME_P (DECL_NAME (fndecl
))
9239 /* Or if they didn't actually specify a return type. */
9240 && !C_FUNCTION_IMPLICIT_INT (fndecl
)
9241 /* Normally, with -Wreturn-type, flow will complain, but we might
9242 optimize out static functions. */
9243 && !TREE_PUBLIC (fndecl
))
9245 warning (OPT_Wreturn_type
,
9246 "no return statement in function returning non-void");
9247 TREE_NO_WARNING (fndecl
) = 1;
9250 /* Complain about parameters that are only set, but never otherwise used. */
9251 if (warn_unused_but_set_parameter
)
9255 for (decl
= DECL_ARGUMENTS (fndecl
);
9257 decl
= DECL_CHAIN (decl
))
9258 if (TREE_USED (decl
)
9259 && TREE_CODE (decl
) == PARM_DECL
9260 && !DECL_READ_P (decl
)
9262 && !DECL_ARTIFICIAL (decl
)
9263 && !TREE_NO_WARNING (decl
))
9264 warning_at (DECL_SOURCE_LOCATION (decl
),
9265 OPT_Wunused_but_set_parameter
,
9266 "parameter %qD set but not used", decl
);
9269 /* Complain about locally defined typedefs that are not used in this
9271 maybe_warn_unused_local_typedefs ();
9273 /* Possibly warn about unused parameters. */
9274 if (warn_unused_parameter
)
9275 do_warn_unused_parameter (fndecl
);
9277 /* Store the end of the function, so that we get good line number
9278 info for the epilogue. */
9279 cfun
->function_end_locus
= input_location
;
9281 /* Finalize the ELF visibility for the function. */
9282 c_determine_visibility (fndecl
);
9284 /* For GNU C extern inline functions disregard inline limits. */
9285 if (DECL_EXTERNAL (fndecl
)
9286 && DECL_DECLARED_INLINE_P (fndecl
))
9287 DECL_DISREGARD_INLINE_LIMITS (fndecl
) = 1;
9289 /* Genericize before inlining. Delay genericizing nested functions
9290 until their parent function is genericized. Since finalizing
9291 requires GENERIC, delay that as well. */
9293 if (DECL_INITIAL (fndecl
) && DECL_INITIAL (fndecl
) != error_mark_node
9294 && !undef_nested_function
)
9296 if (!decl_function_context (fndecl
))
9298 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE
, fndecl
);
9299 c_genericize (fndecl
);
9301 /* ??? Objc emits functions after finalizing the compilation unit.
9302 This should be cleaned up later and this conditional removed. */
9303 if (symtab
->global_info_ready
)
9305 cgraph_node::add_new_function (fndecl
, false);
9308 cgraph_node::finalize_function (fndecl
, false);
9312 /* Register this function with cgraph just far enough to get it
9313 added to our parent's nested function list. Handy, since the
9314 C front end doesn't have such a list. */
9315 (void) cgraph_node::get_create (fndecl
);
9319 if (!decl_function_context (fndecl
))
9320 undef_nested_function
= false;
9322 if (cfun
->language
!= NULL
)
9324 ggc_free (cfun
->language
);
9325 cfun
->language
= NULL
;
9328 /* We're leaving the context of this function, so zap cfun.
9329 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9330 tree_rest_of_compilation. */
9332 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION
, current_function_decl
);
9333 current_function_decl
= NULL
;
9336 /* Check the declarations given in a for-loop for satisfying the C99
9337 constraints. If exactly one such decl is found, return it. LOC is
9338 the location of the opening parenthesis of the for loop. The last
9339 parameter allows you to control the "for loop initial declarations
9340 are only allowed in C99 mode". Normally, you should pass
9341 flag_isoc99 as that parameter. But in some cases (Objective-C
9342 foreach loop, for example) we want to run the checks in this
9343 function even if not in C99 mode, so we allow the caller to turn
9344 off the error about not being in C99 mode.
9348 check_for_loop_decls (location_t loc
, bool turn_off_iso_c99_error
)
9350 struct c_binding
*b
;
9351 tree one_decl
= NULL_TREE
;
9354 if (!turn_off_iso_c99_error
)
9356 static bool hint
= true;
9357 /* If we get here, declarations have been used in a for loop without
9358 the C99 for loop scope. This doesn't make much sense, so don't
9360 error_at (loc
, "%<for%> loop initial declarations "
9361 "are only allowed in C99 or C11 mode");
9365 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9366 "to compile your code");
9371 /* C99 subclause 6.8.5 paragraph 3:
9373 [#3] The declaration part of a for statement shall only
9374 declare identifiers for objects having storage class auto or
9377 It isn't clear whether, in this sentence, "identifiers" binds to
9378 "shall only declare" or to "objects" - that is, whether all identifiers
9379 declared must be identifiers for objects, or whether the restriction
9380 only applies to those that are. (A question on this in comp.std.c
9381 in November 2000 received no answer.) We implement the strictest
9382 interpretation, to avoid creating an extension which later causes
9385 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
9388 tree decl
= b
->decl
;
9393 switch (TREE_CODE (decl
))
9397 location_t decl_loc
= DECL_SOURCE_LOCATION (decl
);
9398 if (TREE_STATIC (decl
))
9400 "declaration of static variable %qD in %<for%> loop "
9401 "initial declaration", decl
);
9402 else if (DECL_EXTERNAL (decl
))
9404 "declaration of %<extern%> variable %qD in %<for%> loop "
9405 "initial declaration", decl
);
9411 "%<struct %E%> declared in %<for%> loop initial "
9416 "%<union %E%> declared in %<for%> loop initial declaration",
9420 error_at (loc
, "%<enum %E%> declared in %<for%> loop "
9421 "initial declaration", id
);
9424 error_at (loc
, "declaration of non-variable "
9425 "%qD in %<for%> loop initial declaration", decl
);
9432 return n_decls
== 1 ? one_decl
: NULL_TREE
;
9435 /* Save and reinitialize the variables
9436 used during compilation of a C function. */
9439 c_push_function_context (void)
9441 struct language_function
*p
= cfun
->language
;
9442 /* cfun->language might have been already allocated by the use of
9443 -Wunused-local-typedefs. In that case, just re-use it. */
9445 cfun
->language
= p
= ggc_cleared_alloc
<language_function
> ();
9447 p
->base
.x_stmt_tree
= c_stmt_tree
;
9448 c_stmt_tree
.x_cur_stmt_list
= vec_safe_copy (c_stmt_tree
.x_cur_stmt_list
);
9449 p
->x_break_label
= c_break_label
;
9450 p
->x_cont_label
= c_cont_label
;
9451 p
->x_switch_stack
= c_switch_stack
;
9452 p
->arg_info
= current_function_arg_info
;
9453 p
->returns_value
= current_function_returns_value
;
9454 p
->returns_null
= current_function_returns_null
;
9455 p
->returns_abnormally
= current_function_returns_abnormally
;
9456 p
->warn_about_return_type
= warn_about_return_type
;
9458 push_function_context ();
9461 /* Restore the variables used during compilation of a C function. */
9464 c_pop_function_context (void)
9466 struct language_function
*p
;
9468 pop_function_context ();
9471 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9472 used to store data throughout the life time of the current cfun,
9473 So don't deallocate it. */
9474 if (!warn_unused_local_typedefs
)
9475 cfun
->language
= NULL
;
9477 if (DECL_STRUCT_FUNCTION (current_function_decl
) == 0
9478 && DECL_SAVED_TREE (current_function_decl
) == NULL_TREE
)
9480 /* Stop pointing to the local nodes about to be freed. */
9481 /* But DECL_INITIAL must remain nonzero so we know this
9482 was an actual function definition. */
9483 DECL_INITIAL (current_function_decl
) = error_mark_node
;
9484 DECL_ARGUMENTS (current_function_decl
) = 0;
9487 c_stmt_tree
= p
->base
.x_stmt_tree
;
9488 p
->base
.x_stmt_tree
.x_cur_stmt_list
= NULL
;
9489 c_break_label
= p
->x_break_label
;
9490 c_cont_label
= p
->x_cont_label
;
9491 c_switch_stack
= p
->x_switch_stack
;
9492 current_function_arg_info
= p
->arg_info
;
9493 current_function_returns_value
= p
->returns_value
;
9494 current_function_returns_null
= p
->returns_null
;
9495 current_function_returns_abnormally
= p
->returns_abnormally
;
9496 warn_about_return_type
= p
->warn_about_return_type
;
9499 /* The functions below are required for functionality of doing
9500 function at once processing in the C front end. Currently these
9501 functions are not called from anywhere in the C front end, but as
9502 these changes continue, that will change. */
9504 /* Returns the stmt_tree (if any) to which statements are currently
9505 being added. If there is no active statement-tree, NULL is
9509 current_stmt_tree (void)
9511 return &c_stmt_tree
;
9514 /* Return the global value of T as a symbol. */
9517 identifier_global_value (tree t
)
9519 struct c_binding
*b
;
9521 for (b
= I_SYMBOL_BINDING (t
); b
; b
= b
->shadowed
)
9522 if (B_IN_FILE_SCOPE (b
) || B_IN_EXTERNAL_SCOPE (b
))
9528 /* In C, the only C-linkage public declaration is at file scope. */
9531 c_linkage_bindings (tree name
)
9533 return identifier_global_value (name
);
9536 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9537 otherwise the name is found in ridpointers from RID_INDEX. */
9540 record_builtin_type (enum rid rid_index
, const char *name
, tree type
)
9544 id
= ridpointers
[(int) rid_index
];
9546 id
= get_identifier (name
);
9547 decl
= build_decl (UNKNOWN_LOCATION
, TYPE_DECL
, id
, type
);
9549 if (debug_hooks
->type_decl
)
9550 debug_hooks
->type_decl (decl
, false);
9553 /* Build the void_list_node (void_type_node having been created). */
9555 build_void_list_node (void)
9557 tree t
= build_tree_list (NULL_TREE
, void_type_node
);
9561 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9564 build_c_parm (struct c_declspecs
*specs
, tree attrs
,
9565 struct c_declarator
*declarator
)
9567 struct c_parm
*ret
= XOBNEW (&parser_obstack
, struct c_parm
);
9570 ret
->declarator
= declarator
;
9574 /* Return a declarator with nested attributes. TARGET is the inner
9575 declarator to which these attributes apply. ATTRS are the
9578 struct c_declarator
*
9579 build_attrs_declarator (tree attrs
, struct c_declarator
*target
)
9581 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
9582 ret
->kind
= cdk_attrs
;
9583 ret
->declarator
= target
;
9584 ret
->u
.attrs
= attrs
;
9588 /* Return a declarator for a function with arguments specified by ARGS
9589 and return type specified by TARGET. */
9591 struct c_declarator
*
9592 build_function_declarator (struct c_arg_info
*args
,
9593 struct c_declarator
*target
)
9595 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
9596 ret
->kind
= cdk_function
;
9597 ret
->declarator
= target
;
9598 ret
->u
.arg_info
= args
;
9602 /* Return a declarator for the identifier IDENT (which may be
9603 NULL_TREE for an abstract declarator). */
9605 struct c_declarator
*
9606 build_id_declarator (tree ident
)
9608 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
9610 ret
->declarator
= 0;
9612 /* Default value - may get reset to a more precise location. */
9613 ret
->id_loc
= input_location
;
9617 /* Return something to represent absolute declarators containing a *.
9618 TARGET is the absolute declarator that the * contains.
9619 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9620 to apply to the pointer type. */
9622 struct c_declarator
*
9623 make_pointer_declarator (struct c_declspecs
*type_quals_attrs
,
9624 struct c_declarator
*target
)
9628 struct c_declarator
*itarget
= target
;
9629 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
9630 if (type_quals_attrs
)
9632 attrs
= type_quals_attrs
->attrs
;
9633 quals
= quals_from_declspecs (type_quals_attrs
);
9634 if (attrs
!= NULL_TREE
)
9635 itarget
= build_attrs_declarator (attrs
, target
);
9637 ret
->kind
= cdk_pointer
;
9638 ret
->declarator
= itarget
;
9639 ret
->u
.pointer_quals
= quals
;
9643 /* Return a pointer to a structure for an empty list of declaration
9646 struct c_declspecs
*
9647 build_null_declspecs (void)
9649 struct c_declspecs
*ret
= XOBNEW (&parser_obstack
, struct c_declspecs
);
9650 memset (ret
, 0, sizeof *ret
);
9651 ret
->align_log
= -1;
9652 ret
->typespec_word
= cts_none
;
9653 ret
->storage_class
= csc_none
;
9654 ret
->expr_const_operands
= true;
9655 ret
->typespec_kind
= ctsk_none
;
9656 ret
->address_space
= ADDR_SPACE_GENERIC
;
9660 /* Add the address space ADDRSPACE to the declaration specifiers
9661 SPECS, returning SPECS. */
9663 struct c_declspecs
*
9664 declspecs_add_addrspace (source_location location
,
9665 struct c_declspecs
*specs
, addr_space_t as
)
9667 specs
->non_sc_seen_p
= true;
9668 specs
->declspecs_seen_p
= true;
9670 if (!ADDR_SPACE_GENERIC_P (specs
->address_space
)
9671 && specs
->address_space
!= as
)
9672 error ("incompatible address space qualifiers %qs and %qs",
9673 c_addr_space_name (as
),
9674 c_addr_space_name (specs
->address_space
));
9677 specs
->address_space
= as
;
9678 specs
->locations
[cdw_address_space
] = location
;
9683 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9686 struct c_declspecs
*
9687 declspecs_add_qual (source_location loc
,
9688 struct c_declspecs
*specs
, tree qual
)
9692 specs
->non_sc_seen_p
= true;
9693 specs
->declspecs_seen_p
= true;
9694 gcc_assert (TREE_CODE (qual
) == IDENTIFIER_NODE
9695 && C_IS_RESERVED_WORD (qual
));
9696 i
= C_RID_CODE (qual
);
9697 location_t prev_loc
= 0;
9701 dupe
= specs
->const_p
;
9702 specs
->const_p
= true;
9703 prev_loc
= specs
->locations
[cdw_const
];
9704 specs
->locations
[cdw_const
] = loc
;
9707 dupe
= specs
->volatile_p
;
9708 specs
->volatile_p
= true;
9709 prev_loc
= specs
->locations
[cdw_volatile
];
9710 specs
->locations
[cdw_volatile
] = loc
;
9713 dupe
= specs
->restrict_p
;
9714 specs
->restrict_p
= true;
9715 prev_loc
= specs
->locations
[cdw_restrict
];
9716 specs
->locations
[cdw_restrict
] = loc
;
9719 dupe
= specs
->atomic_p
;
9720 specs
->atomic_p
= true;
9721 prev_loc
= specs
->locations
[cdw_atomic
];
9722 specs
->locations
[cdw_atomic
] = loc
;
9729 bool warned
= pedwarn_c90 (loc
, OPT_Wpedantic
,
9730 "duplicate %qE declaration specifier", qual
);
9732 && warn_duplicate_decl_specifier
9733 && prev_loc
>= RESERVED_LOCATION_COUNT
9734 && !from_macro_expansion_at (prev_loc
)
9735 && !from_macro_expansion_at (loc
))
9736 warning_at (loc
, OPT_Wduplicate_decl_specifier
,
9737 "duplicate %qE declaration specifier", qual
);
9742 /* Add the type specifier TYPE to the declaration specifiers SPECS,
9745 struct c_declspecs
*
9746 declspecs_add_type (location_t loc
, struct c_declspecs
*specs
,
9747 struct c_typespec spec
)
9749 tree type
= spec
.spec
;
9750 specs
->non_sc_seen_p
= true;
9751 specs
->declspecs_seen_p
= true;
9752 specs
->typespec_kind
= spec
.kind
;
9753 if (TREE_DEPRECATED (type
))
9754 specs
->deprecated_p
= true;
9756 /* Handle type specifier keywords. */
9757 if (TREE_CODE (type
) == IDENTIFIER_NODE
9758 && C_IS_RESERVED_WORD (type
)
9759 && C_RID_CODE (type
) != RID_CXX_COMPAT_WARN
)
9761 enum rid i
= C_RID_CODE (type
);
9764 error_at (loc
, "two or more data types in declaration specifiers");
9767 if ((int) i
<= (int) RID_LAST_MODIFIER
)
9769 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
9774 if (specs
->long_long_p
)
9776 error_at (loc
, "%<long long long%> is too long for GCC");
9781 if (specs
->typespec_word
== cts_double
)
9784 ("both %<long long%> and %<double%> in "
9785 "declaration specifiers"));
9788 pedwarn_c90 (loc
, OPT_Wlong_long
,
9789 "ISO C90 does not support %<long long%>");
9790 specs
->long_long_p
= 1;
9791 specs
->locations
[cdw_long_long
] = loc
;
9796 ("both %<long%> and %<short%> in "
9797 "declaration specifiers"));
9798 else if (specs
->typespec_word
== cts_auto_type
)
9800 ("both %<long%> and %<__auto_type%> in "
9801 "declaration specifiers"));
9802 else if (specs
->typespec_word
== cts_void
)
9804 ("both %<long%> and %<void%> in "
9805 "declaration specifiers"));
9806 else if (specs
->typespec_word
== cts_int_n
)
9808 ("both %<long%> and %<__int%d%> in "
9809 "declaration specifiers"),
9810 int_n_data
[specs
->int_n_idx
].bitsize
);
9811 else if (specs
->typespec_word
== cts_bool
)
9813 ("both %<long%> and %<_Bool%> in "
9814 "declaration specifiers"));
9815 else if (specs
->typespec_word
== cts_char
)
9817 ("both %<long%> and %<char%> in "
9818 "declaration specifiers"));
9819 else if (specs
->typespec_word
== cts_float
)
9821 ("both %<long%> and %<float%> in "
9822 "declaration specifiers"));
9823 else if (specs
->typespec_word
== cts_dfloat32
)
9825 ("both %<long%> and %<_Decimal32%> in "
9826 "declaration specifiers"));
9827 else if (specs
->typespec_word
== cts_dfloat64
)
9829 ("both %<long%> and %<_Decimal64%> in "
9830 "declaration specifiers"));
9831 else if (specs
->typespec_word
== cts_dfloat128
)
9833 ("both %<long%> and %<_Decimal128%> in "
9834 "declaration specifiers"));
9837 specs
->long_p
= true;
9838 specs
->locations
[cdw_long
] = loc
;
9842 dupe
= specs
->short_p
;
9845 ("both %<long%> and %<short%> in "
9846 "declaration specifiers"));
9847 else if (specs
->typespec_word
== cts_auto_type
)
9849 ("both %<short%> and %<__auto_type%> in "
9850 "declaration specifiers"));
9851 else if (specs
->typespec_word
== cts_void
)
9853 ("both %<short%> and %<void%> in "
9854 "declaration specifiers"));
9855 else if (specs
->typespec_word
== cts_int_n
)
9857 ("both %<short%> and %<__int%d%> in "
9858 "declaration specifiers"),
9859 int_n_data
[specs
->int_n_idx
].bitsize
);
9860 else if (specs
->typespec_word
== cts_bool
)
9862 ("both %<short%> and %<_Bool%> in "
9863 "declaration specifiers"));
9864 else if (specs
->typespec_word
== cts_char
)
9866 ("both %<short%> and %<char%> in "
9867 "declaration specifiers"));
9868 else if (specs
->typespec_word
== cts_float
)
9870 ("both %<short%> and %<float%> in "
9871 "declaration specifiers"));
9872 else if (specs
->typespec_word
== cts_double
)
9874 ("both %<short%> and %<double%> in "
9875 "declaration specifiers"));
9876 else if (specs
->typespec_word
== cts_dfloat32
)
9878 ("both %<short%> and %<_Decimal32%> in "
9879 "declaration specifiers"));
9880 else if (specs
->typespec_word
== cts_dfloat64
)
9882 ("both %<short%> and %<_Decimal64%> in "
9883 "declaration specifiers"));
9884 else if (specs
->typespec_word
== cts_dfloat128
)
9886 ("both %<short%> and %<_Decimal128%> in "
9887 "declaration specifiers"));
9890 specs
->short_p
= true;
9891 specs
->locations
[cdw_short
] = loc
;
9895 dupe
= specs
->signed_p
;
9896 if (specs
->unsigned_p
)
9898 ("both %<signed%> and %<unsigned%> in "
9899 "declaration specifiers"));
9900 else if (specs
->typespec_word
== cts_auto_type
)
9902 ("both %<signed%> and %<__auto_type%> in "
9903 "declaration specifiers"));
9904 else if (specs
->typespec_word
== cts_void
)
9906 ("both %<signed%> and %<void%> in "
9907 "declaration specifiers"));
9908 else if (specs
->typespec_word
== cts_bool
)
9910 ("both %<signed%> and %<_Bool%> in "
9911 "declaration specifiers"));
9912 else if (specs
->typespec_word
== cts_float
)
9914 ("both %<signed%> and %<float%> in "
9915 "declaration specifiers"));
9916 else if (specs
->typespec_word
== cts_double
)
9918 ("both %<signed%> and %<double%> in "
9919 "declaration specifiers"));
9920 else if (specs
->typespec_word
== cts_dfloat32
)
9922 ("both %<signed%> and %<_Decimal32%> in "
9923 "declaration specifiers"));
9924 else if (specs
->typespec_word
== cts_dfloat64
)
9926 ("both %<signed%> and %<_Decimal64%> in "
9927 "declaration specifiers"));
9928 else if (specs
->typespec_word
== cts_dfloat128
)
9930 ("both %<signed%> and %<_Decimal128%> in "
9931 "declaration specifiers"));
9934 specs
->signed_p
= true;
9935 specs
->locations
[cdw_signed
] = loc
;
9939 dupe
= specs
->unsigned_p
;
9940 if (specs
->signed_p
)
9942 ("both %<signed%> and %<unsigned%> in "
9943 "declaration specifiers"));
9944 else if (specs
->typespec_word
== cts_auto_type
)
9946 ("both %<unsigned%> and %<__auto_type%> in "
9947 "declaration specifiers"));
9948 else if (specs
->typespec_word
== cts_void
)
9950 ("both %<unsigned%> and %<void%> in "
9951 "declaration specifiers"));
9952 else if (specs
->typespec_word
== cts_bool
)
9954 ("both %<unsigned%> and %<_Bool%> in "
9955 "declaration specifiers"));
9956 else if (specs
->typespec_word
== cts_float
)
9958 ("both %<unsigned%> and %<float%> in "
9959 "declaration specifiers"));
9960 else if (specs
->typespec_word
== cts_double
)
9962 ("both %<unsigned%> and %<double%> in "
9963 "declaration specifiers"));
9964 else if (specs
->typespec_word
== cts_dfloat32
)
9966 ("both %<unsigned%> and %<_Decimal32%> in "
9967 "declaration specifiers"));
9968 else if (specs
->typespec_word
== cts_dfloat64
)
9970 ("both %<unsigned%> and %<_Decimal64%> in "
9971 "declaration specifiers"));
9972 else if (specs
->typespec_word
== cts_dfloat128
)
9974 ("both %<unsigned%> and %<_Decimal128%> in "
9975 "declaration specifiers"));
9978 specs
->unsigned_p
= true;
9979 specs
->locations
[cdw_unsigned
] = loc
;
9983 dupe
= specs
->complex_p
;
9984 if (!in_system_header_at (loc
))
9985 pedwarn_c90 (loc
, OPT_Wpedantic
,
9986 "ISO C90 does not support complex types");
9987 if (specs
->typespec_word
== cts_auto_type
)
9989 ("both %<complex%> and %<__auto_type%> in "
9990 "declaration specifiers"));
9991 else if (specs
->typespec_word
== cts_void
)
9993 ("both %<complex%> and %<void%> in "
9994 "declaration specifiers"));
9995 else if (specs
->typespec_word
== cts_bool
)
9997 ("both %<complex%> and %<_Bool%> in "
9998 "declaration specifiers"));
9999 else if (specs
->typespec_word
== cts_dfloat32
)
10001 ("both %<complex%> and %<_Decimal32%> in "
10002 "declaration specifiers"));
10003 else if (specs
->typespec_word
== cts_dfloat64
)
10005 ("both %<complex%> and %<_Decimal64%> in "
10006 "declaration specifiers"));
10007 else if (specs
->typespec_word
== cts_dfloat128
)
10009 ("both %<complex%> and %<_Decimal128%> in "
10010 "declaration specifiers"));
10011 else if (specs
->typespec_word
== cts_fract
)
10013 ("both %<complex%> and %<_Fract%> in "
10014 "declaration specifiers"));
10015 else if (specs
->typespec_word
== cts_accum
)
10017 ("both %<complex%> and %<_Accum%> in "
10018 "declaration specifiers"));
10019 else if (specs
->saturating_p
)
10021 ("both %<complex%> and %<_Sat%> in "
10022 "declaration specifiers"));
10025 specs
->complex_p
= true;
10026 specs
->locations
[cdw_complex
] = loc
;
10030 dupe
= specs
->saturating_p
;
10031 pedwarn (loc
, OPT_Wpedantic
,
10032 "ISO C does not support saturating types");
10033 if (specs
->typespec_word
== cts_int_n
)
10036 ("both %<_Sat%> and %<__int%d%> in "
10037 "declaration specifiers"),
10038 int_n_data
[specs
->int_n_idx
].bitsize
);
10040 else if (specs
->typespec_word
== cts_auto_type
)
10042 ("both %<_Sat%> and %<__auto_type%> in "
10043 "declaration specifiers"));
10044 else if (specs
->typespec_word
== cts_void
)
10046 ("both %<_Sat%> and %<void%> in "
10047 "declaration specifiers"));
10048 else if (specs
->typespec_word
== cts_bool
)
10050 ("both %<_Sat%> and %<_Bool%> in "
10051 "declaration specifiers"));
10052 else if (specs
->typespec_word
== cts_char
)
10054 ("both %<_Sat%> and %<char%> in "
10055 "declaration specifiers"));
10056 else if (specs
->typespec_word
== cts_int
)
10058 ("both %<_Sat%> and %<int%> in "
10059 "declaration specifiers"));
10060 else if (specs
->typespec_word
== cts_float
)
10062 ("both %<_Sat%> and %<float%> in "
10063 "declaration specifiers"));
10064 else if (specs
->typespec_word
== cts_double
)
10066 ("both %<_Sat%> and %<double%> in "
10067 "declaration specifiers"));
10068 else if (specs
->typespec_word
== cts_dfloat32
)
10070 ("both %<_Sat%> and %<_Decimal32%> in "
10071 "declaration specifiers"));
10072 else if (specs
->typespec_word
== cts_dfloat64
)
10074 ("both %<_Sat%> and %<_Decimal64%> in "
10075 "declaration specifiers"));
10076 else if (specs
->typespec_word
== cts_dfloat128
)
10078 ("both %<_Sat%> and %<_Decimal128%> in "
10079 "declaration specifiers"));
10080 else if (specs
->complex_p
)
10082 ("both %<_Sat%> and %<complex%> in "
10083 "declaration specifiers"));
10086 specs
->saturating_p
= true;
10087 specs
->locations
[cdw_saturating
] = loc
;
10091 gcc_unreachable ();
10095 error_at (loc
, "duplicate %qE", type
);
10101 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
10102 "__intN", "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10104 if (specs
->typespec_word
!= cts_none
)
10107 "two or more data types in declaration specifiers");
10112 case RID_AUTO_TYPE
:
10115 ("both %<long%> and %<__auto_type%> in "
10116 "declaration specifiers"));
10117 else if (specs
->short_p
)
10119 ("both %<short%> and %<__auto_type%> in "
10120 "declaration specifiers"));
10121 else if (specs
->signed_p
)
10123 ("both %<signed%> and %<__auto_type%> in "
10124 "declaration specifiers"));
10125 else if (specs
->unsigned_p
)
10127 ("both %<unsigned%> and %<__auto_type%> in "
10128 "declaration specifiers"));
10129 else if (specs
->complex_p
)
10131 ("both %<complex%> and %<__auto_type%> in "
10132 "declaration specifiers"));
10133 else if (specs
->saturating_p
)
10135 ("both %<_Sat%> and %<__auto_type%> in "
10136 "declaration specifiers"));
10139 specs
->typespec_word
= cts_auto_type
;
10140 specs
->locations
[cdw_typespec
] = loc
;
10147 specs
->int_n_idx
= i
- RID_INT_N_0
;
10148 if (!in_system_header_at (input_location
))
10149 pedwarn (loc
, OPT_Wpedantic
,
10150 "ISO C does not support %<__int%d%> types",
10151 int_n_data
[specs
->int_n_idx
].bitsize
);
10155 ("both %<__int%d%> and %<long%> in "
10156 "declaration specifiers"),
10157 int_n_data
[specs
->int_n_idx
].bitsize
);
10158 else if (specs
->saturating_p
)
10160 ("both %<_Sat%> and %<__int%d%> in "
10161 "declaration specifiers"),
10162 int_n_data
[specs
->int_n_idx
].bitsize
);
10163 else if (specs
->short_p
)
10165 ("both %<__int%d%> and %<short%> in "
10166 "declaration specifiers"),
10167 int_n_data
[specs
->int_n_idx
].bitsize
);
10168 else if (! int_n_enabled_p
[specs
->int_n_idx
])
10170 "%<__int%d%> is not supported on this target",
10171 int_n_data
[specs
->int_n_idx
].bitsize
);
10174 specs
->typespec_word
= cts_int_n
;
10175 specs
->locations
[cdw_typespec
] = loc
;
10181 ("both %<long%> and %<void%> in "
10182 "declaration specifiers"));
10183 else if (specs
->short_p
)
10185 ("both %<short%> and %<void%> in "
10186 "declaration specifiers"));
10187 else if (specs
->signed_p
)
10189 ("both %<signed%> and %<void%> in "
10190 "declaration specifiers"));
10191 else if (specs
->unsigned_p
)
10193 ("both %<unsigned%> and %<void%> in "
10194 "declaration specifiers"));
10195 else if (specs
->complex_p
)
10197 ("both %<complex%> and %<void%> in "
10198 "declaration specifiers"));
10199 else if (specs
->saturating_p
)
10201 ("both %<_Sat%> and %<void%> in "
10202 "declaration specifiers"));
10205 specs
->typespec_word
= cts_void
;
10206 specs
->locations
[cdw_typespec
] = loc
;
10210 if (!in_system_header_at (loc
))
10211 pedwarn_c90 (loc
, OPT_Wpedantic
,
10212 "ISO C90 does not support boolean types");
10215 ("both %<long%> and %<_Bool%> in "
10216 "declaration specifiers"));
10217 else if (specs
->short_p
)
10219 ("both %<short%> and %<_Bool%> in "
10220 "declaration specifiers"));
10221 else if (specs
->signed_p
)
10223 ("both %<signed%> and %<_Bool%> in "
10224 "declaration specifiers"));
10225 else if (specs
->unsigned_p
)
10227 ("both %<unsigned%> and %<_Bool%> in "
10228 "declaration specifiers"));
10229 else if (specs
->complex_p
)
10231 ("both %<complex%> and %<_Bool%> in "
10232 "declaration specifiers"));
10233 else if (specs
->saturating_p
)
10235 ("both %<_Sat%> and %<_Bool%> in "
10236 "declaration specifiers"));
10239 specs
->typespec_word
= cts_bool
;
10240 specs
->locations
[cdw_typespec
] = loc
;
10246 ("both %<long%> and %<char%> in "
10247 "declaration specifiers"));
10248 else if (specs
->short_p
)
10250 ("both %<short%> and %<char%> in "
10251 "declaration specifiers"));
10252 else if (specs
->saturating_p
)
10254 ("both %<_Sat%> and %<char%> in "
10255 "declaration specifiers"));
10258 specs
->typespec_word
= cts_char
;
10259 specs
->locations
[cdw_typespec
] = loc
;
10263 if (specs
->saturating_p
)
10265 ("both %<_Sat%> and %<int%> in "
10266 "declaration specifiers"));
10269 specs
->typespec_word
= cts_int
;
10270 specs
->locations
[cdw_typespec
] = loc
;
10276 ("both %<long%> and %<float%> in "
10277 "declaration specifiers"));
10278 else if (specs
->short_p
)
10280 ("both %<short%> and %<float%> in "
10281 "declaration specifiers"));
10282 else if (specs
->signed_p
)
10284 ("both %<signed%> and %<float%> in "
10285 "declaration specifiers"));
10286 else if (specs
->unsigned_p
)
10288 ("both %<unsigned%> and %<float%> in "
10289 "declaration specifiers"));
10290 else if (specs
->saturating_p
)
10292 ("both %<_Sat%> and %<float%> in "
10293 "declaration specifiers"));
10296 specs
->typespec_word
= cts_float
;
10297 specs
->locations
[cdw_typespec
] = loc
;
10301 if (specs
->long_long_p
)
10303 ("both %<long long%> and %<double%> in "
10304 "declaration specifiers"));
10305 else if (specs
->short_p
)
10307 ("both %<short%> and %<double%> in "
10308 "declaration specifiers"));
10309 else if (specs
->signed_p
)
10311 ("both %<signed%> and %<double%> in "
10312 "declaration specifiers"));
10313 else if (specs
->unsigned_p
)
10315 ("both %<unsigned%> and %<double%> in "
10316 "declaration specifiers"));
10317 else if (specs
->saturating_p
)
10319 ("both %<_Sat%> and %<double%> in "
10320 "declaration specifiers"));
10323 specs
->typespec_word
= cts_double
;
10324 specs
->locations
[cdw_typespec
] = loc
;
10329 case RID_DFLOAT128
:
10332 if (i
== RID_DFLOAT32
)
10333 str
= "_Decimal32";
10334 else if (i
== RID_DFLOAT64
)
10335 str
= "_Decimal64";
10337 str
= "_Decimal128";
10338 if (specs
->long_long_p
)
10340 ("both %<long long%> and %<%s%> in "
10341 "declaration specifiers"),
10345 ("both %<long%> and %<%s%> in "
10346 "declaration specifiers"),
10348 else if (specs
->short_p
)
10350 ("both %<short%> and %<%s%> in "
10351 "declaration specifiers"),
10353 else if (specs
->signed_p
)
10355 ("both %<signed%> and %<%s%> in "
10356 "declaration specifiers"),
10358 else if (specs
->unsigned_p
)
10360 ("both %<unsigned%> and %<%s%> in "
10361 "declaration specifiers"),
10363 else if (specs
->complex_p
)
10365 ("both %<complex%> and %<%s%> in "
10366 "declaration specifiers"),
10368 else if (specs
->saturating_p
)
10370 ("both %<_Sat%> and %<%s%> in "
10371 "declaration specifiers"),
10373 else if (i
== RID_DFLOAT32
)
10374 specs
->typespec_word
= cts_dfloat32
;
10375 else if (i
== RID_DFLOAT64
)
10376 specs
->typespec_word
= cts_dfloat64
;
10378 specs
->typespec_word
= cts_dfloat128
;
10379 specs
->locations
[cdw_typespec
] = loc
;
10381 if (!targetm
.decimal_float_supported_p ())
10383 ("decimal floating point not supported "
10384 "for this target"));
10385 pedwarn (loc
, OPT_Wpedantic
,
10386 "ISO C does not support decimal floating point");
10392 if (i
== RID_FRACT
)
10396 if (specs
->complex_p
)
10398 ("both %<complex%> and %<%s%> in "
10399 "declaration specifiers"),
10401 else if (i
== RID_FRACT
)
10402 specs
->typespec_word
= cts_fract
;
10404 specs
->typespec_word
= cts_accum
;
10405 specs
->locations
[cdw_typespec
] = loc
;
10407 if (!targetm
.fixed_point_supported_p ())
10409 "fixed-point types not supported for this target");
10410 pedwarn (loc
, OPT_Wpedantic
,
10411 "ISO C does not support fixed-point types");
10414 /* ObjC reserved word "id", handled below. */
10420 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10421 form of ObjC type, cases such as "int" and "long" being handled
10422 above), a TYPE (struct, union, enum and typeof specifiers) or an
10423 ERROR_MARK. In none of these cases may there have previously
10424 been any type specifiers. */
10425 if (specs
->type
|| specs
->typespec_word
!= cts_none
10426 || specs
->long_p
|| specs
->short_p
|| specs
->signed_p
10427 || specs
->unsigned_p
|| specs
->complex_p
)
10428 error_at (loc
, "two or more data types in declaration specifiers");
10429 else if (TREE_CODE (type
) == TYPE_DECL
)
10431 if (TREE_TYPE (type
) == error_mark_node
)
10432 ; /* Allow the type to default to int to avoid cascading errors. */
10435 specs
->type
= TREE_TYPE (type
);
10436 specs
->decl_attr
= DECL_ATTRIBUTES (type
);
10437 specs
->typedef_p
= true;
10438 specs
->explicit_signed_p
= C_TYPEDEF_EXPLICITLY_SIGNED (type
);
10439 specs
->locations
[cdw_typedef
] = loc
;
10441 /* If this typedef name is defined in a struct, then a C++
10442 lookup would return a different value. */
10443 if (warn_cxx_compat
10444 && I_SYMBOL_BINDING (DECL_NAME (type
))->in_struct
)
10445 warning_at (loc
, OPT_Wc___compat
,
10446 "C++ lookup of %qD would return a field, not a type",
10449 /* If we are parsing a struct, record that a struct field
10451 if (warn_cxx_compat
&& struct_parse_info
!= NULL
)
10452 struct_parse_info
->typedefs_seen
.safe_push (type
);
10455 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
10457 tree t
= lookup_name (type
);
10458 if (!t
|| TREE_CODE (t
) != TYPE_DECL
)
10459 error_at (loc
, "%qE fails to be a typedef or built in type", type
);
10460 else if (TREE_TYPE (t
) == error_mark_node
)
10464 specs
->type
= TREE_TYPE (t
);
10465 specs
->locations
[cdw_typespec
] = loc
;
10470 if (TREE_CODE (type
) != ERROR_MARK
&& spec
.kind
== ctsk_typeof
)
10472 specs
->typedef_p
= true;
10473 specs
->locations
[cdw_typedef
] = loc
;
10477 specs
->expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (spec
.expr
),
10478 specs
->expr
, spec
.expr
);
10480 specs
->expr
= spec
.expr
;
10481 specs
->expr_const_operands
&= spec
.expr_const_operands
;
10484 specs
->type
= type
;
10490 /* Add the storage class specifier or function specifier SCSPEC to the
10491 declaration specifiers SPECS, returning SPECS. */
10493 struct c_declspecs
*
10494 declspecs_add_scspec (source_location loc
,
10495 struct c_declspecs
*specs
,
10499 enum c_storage_class n
= csc_none
;
10501 specs
->declspecs_seen_p
= true;
10502 gcc_assert (TREE_CODE (scspec
) == IDENTIFIER_NODE
10503 && C_IS_RESERVED_WORD (scspec
));
10504 i
= C_RID_CODE (scspec
);
10505 if (specs
->non_sc_seen_p
)
10506 warning (OPT_Wold_style_declaration
,
10507 "%qE is not at beginning of declaration", scspec
);
10511 /* C99 permits duplicate inline. Although of doubtful utility,
10512 it seems simplest to permit it in gnu89 mode as well, as
10513 there is also little utility in maintaining this as a
10514 difference between gnu89 and C99 inline. */
10516 specs
->inline_p
= true;
10517 specs
->locations
[cdw_inline
] = loc
;
10520 /* Duplicate _Noreturn is permitted. */
10522 specs
->noreturn_p
= true;
10523 specs
->locations
[cdw_noreturn
] = loc
;
10526 dupe
= specs
->thread_p
;
10527 if (specs
->storage_class
== csc_auto
)
10528 error ("%qE used with %<auto%>", scspec
);
10529 else if (specs
->storage_class
== csc_register
)
10530 error ("%qE used with %<register%>", scspec
);
10531 else if (specs
->storage_class
== csc_typedef
)
10532 error ("%qE used with %<typedef%>", scspec
);
10535 specs
->thread_p
= true;
10536 specs
->thread_gnu_p
= (strcmp (IDENTIFIER_POINTER (scspec
),
10538 /* A diagnostic is not required for the use of this
10539 identifier in the implementation namespace; only diagnose
10540 it for the C11 spelling because of existing code using
10541 the other spelling. */
10542 if (!specs
->thread_gnu_p
)
10545 pedwarn_c99 (loc
, OPT_Wpedantic
,
10546 "ISO C99 does not support %qE", scspec
);
10548 pedwarn_c99 (loc
, OPT_Wpedantic
,
10549 "ISO C90 does not support %qE", scspec
);
10551 specs
->locations
[cdw_thread
] = loc
;
10559 /* Diagnose "__thread extern". */
10560 if (specs
->thread_p
&& specs
->thread_gnu_p
)
10561 error ("%<__thread%> before %<extern%>");
10568 /* Diagnose "__thread static". */
10569 if (specs
->thread_p
&& specs
->thread_gnu_p
)
10570 error ("%<__thread%> before %<static%>");
10576 gcc_unreachable ();
10578 if (n
!= csc_none
&& n
== specs
->storage_class
)
10582 if (i
== RID_THREAD
)
10583 error ("duplicate %<_Thread_local%> or %<__thread%>");
10585 error ("duplicate %qE", scspec
);
10589 if (specs
->storage_class
!= csc_none
&& n
!= specs
->storage_class
)
10591 error ("multiple storage classes in declaration specifiers");
10595 specs
->storage_class
= n
;
10596 specs
->locations
[cdw_storage_class
] = loc
;
10597 if (n
!= csc_extern
&& n
!= csc_static
&& specs
->thread_p
)
10599 error ("%qs used with %qE",
10600 specs
->thread_gnu_p
? "__thread" : "_Thread_local",
10602 specs
->thread_p
= false;
10609 /* Add the attributes ATTRS to the declaration specifiers SPECS,
10610 returning SPECS. */
10612 struct c_declspecs
*
10613 declspecs_add_attrs (source_location loc
, struct c_declspecs
*specs
, tree attrs
)
10615 specs
->attrs
= chainon (attrs
, specs
->attrs
);
10616 specs
->locations
[cdw_attributes
] = loc
;
10617 specs
->declspecs_seen_p
= true;
10621 /* Add an _Alignas specifier (expression ALIGN, or type whose
10622 alignment is ALIGN) to the declaration specifiers SPECS, returning
10624 struct c_declspecs
*
10625 declspecs_add_alignas (source_location loc
,
10626 struct c_declspecs
*specs
, tree align
)
10629 specs
->alignas_p
= true;
10630 specs
->locations
[cdw_alignas
] = loc
;
10631 if (align
== error_mark_node
)
10633 align_log
= check_user_alignment (align
, true);
10634 if (align_log
> specs
->align_log
)
10635 specs
->align_log
= align_log
;
10639 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
10640 specifiers with any other type specifier to determine the resulting
10641 type. This is where ISO C checks on complex types are made, since
10642 "_Complex long" is a prefix of the valid ISO C type "_Complex long
10645 struct c_declspecs
*
10646 finish_declspecs (struct c_declspecs
*specs
)
10648 /* If a type was specified as a whole, we have no modifiers and are
10650 if (specs
->type
!= NULL_TREE
)
10652 gcc_assert (!specs
->long_p
&& !specs
->long_long_p
&& !specs
->short_p
10653 && !specs
->signed_p
&& !specs
->unsigned_p
10654 && !specs
->complex_p
);
10656 /* Set a dummy type. */
10657 if (TREE_CODE (specs
->type
) == ERROR_MARK
)
10658 specs
->type
= integer_type_node
;
10662 /* If none of "void", "_Bool", "char", "int", "float" or "double"
10663 has been specified, treat it as "int" unless "_Complex" is
10664 present and there are no other specifiers. If we just have
10665 "_Complex", it is equivalent to "_Complex double", but e.g.
10666 "_Complex short" is equivalent to "_Complex short int". */
10667 if (specs
->typespec_word
== cts_none
)
10669 if (specs
->saturating_p
)
10671 error_at (specs
->locations
[cdw_saturating
],
10672 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
10673 if (!targetm
.fixed_point_supported_p ())
10674 error_at (specs
->locations
[cdw_saturating
],
10675 "fixed-point types not supported for this target");
10676 specs
->typespec_word
= cts_fract
;
10678 else if (specs
->long_p
|| specs
->short_p
10679 || specs
->signed_p
|| specs
->unsigned_p
)
10681 specs
->typespec_word
= cts_int
;
10683 else if (specs
->complex_p
)
10685 specs
->typespec_word
= cts_double
;
10686 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
10687 "ISO C does not support plain %<complex%> meaning "
10688 "%<double complex%>");
10692 specs
->typespec_word
= cts_int
;
10693 specs
->default_int_p
= true;
10694 /* We don't diagnose this here because grokdeclarator will
10695 give more specific diagnostics according to whether it is
10696 a function definition. */
10700 /* If "signed" was specified, record this to distinguish "int" and
10701 "signed int" in the case of a bit-field with
10702 -funsigned-bitfields. */
10703 specs
->explicit_signed_p
= specs
->signed_p
;
10705 /* Now compute the actual type. */
10706 switch (specs
->typespec_word
)
10708 case cts_auto_type
:
10709 gcc_assert (!specs
->long_p
&& !specs
->short_p
10710 && !specs
->signed_p
&& !specs
->unsigned_p
10711 && !specs
->complex_p
);
10712 /* Type to be filled in later. */
10715 gcc_assert (!specs
->long_p
&& !specs
->short_p
10716 && !specs
->signed_p
&& !specs
->unsigned_p
10717 && !specs
->complex_p
);
10718 specs
->type
= void_type_node
;
10721 gcc_assert (!specs
->long_p
&& !specs
->short_p
10722 && !specs
->signed_p
&& !specs
->unsigned_p
10723 && !specs
->complex_p
);
10724 specs
->type
= boolean_type_node
;
10727 gcc_assert (!specs
->long_p
&& !specs
->short_p
);
10728 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
10729 if (specs
->signed_p
)
10730 specs
->type
= signed_char_type_node
;
10731 else if (specs
->unsigned_p
)
10732 specs
->type
= unsigned_char_type_node
;
10734 specs
->type
= char_type_node
;
10735 if (specs
->complex_p
)
10737 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
10738 "ISO C does not support complex integer types");
10739 specs
->type
= build_complex_type (specs
->type
);
10743 gcc_assert (!specs
->long_p
&& !specs
->short_p
&& !specs
->long_long_p
);
10744 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
10745 specs
->type
= (specs
->unsigned_p
10746 ? int_n_trees
[specs
->int_n_idx
].unsigned_type
10747 : int_n_trees
[specs
->int_n_idx
].signed_type
);
10748 if (specs
->complex_p
)
10750 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
10751 "ISO C does not support complex integer types");
10752 specs
->type
= build_complex_type (specs
->type
);
10756 gcc_assert (!(specs
->long_p
&& specs
->short_p
));
10757 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
10758 if (specs
->long_long_p
)
10759 specs
->type
= (specs
->unsigned_p
10760 ? long_long_unsigned_type_node
10761 : long_long_integer_type_node
);
10762 else if (specs
->long_p
)
10763 specs
->type
= (specs
->unsigned_p
10764 ? long_unsigned_type_node
10765 : long_integer_type_node
);
10766 else if (specs
->short_p
)
10767 specs
->type
= (specs
->unsigned_p
10768 ? short_unsigned_type_node
10769 : short_integer_type_node
);
10771 specs
->type
= (specs
->unsigned_p
10772 ? unsigned_type_node
10773 : integer_type_node
);
10774 if (specs
->complex_p
)
10776 pedwarn (specs
->locations
[cdw_complex
], OPT_Wpedantic
,
10777 "ISO C does not support complex integer types");
10778 specs
->type
= build_complex_type (specs
->type
);
10782 gcc_assert (!specs
->long_p
&& !specs
->short_p
10783 && !specs
->signed_p
&& !specs
->unsigned_p
);
10784 specs
->type
= (specs
->complex_p
10785 ? complex_float_type_node
10786 : float_type_node
);
10789 gcc_assert (!specs
->long_long_p
&& !specs
->short_p
10790 && !specs
->signed_p
&& !specs
->unsigned_p
);
10793 specs
->type
= (specs
->complex_p
10794 ? complex_long_double_type_node
10795 : long_double_type_node
);
10799 specs
->type
= (specs
->complex_p
10800 ? complex_double_type_node
10801 : double_type_node
);
10806 case cts_dfloat128
:
10807 gcc_assert (!specs
->long_p
&& !specs
->long_long_p
&& !specs
->short_p
10808 && !specs
->signed_p
&& !specs
->unsigned_p
&& !specs
->complex_p
);
10809 if (specs
->typespec_word
== cts_dfloat32
)
10810 specs
->type
= dfloat32_type_node
;
10811 else if (specs
->typespec_word
== cts_dfloat64
)
10812 specs
->type
= dfloat64_type_node
;
10814 specs
->type
= dfloat128_type_node
;
10817 gcc_assert (!specs
->complex_p
);
10818 if (!targetm
.fixed_point_supported_p ())
10819 specs
->type
= integer_type_node
;
10820 else if (specs
->saturating_p
)
10822 if (specs
->long_long_p
)
10823 specs
->type
= specs
->unsigned_p
10824 ? sat_unsigned_long_long_fract_type_node
10825 : sat_long_long_fract_type_node
;
10826 else if (specs
->long_p
)
10827 specs
->type
= specs
->unsigned_p
10828 ? sat_unsigned_long_fract_type_node
10829 : sat_long_fract_type_node
;
10830 else if (specs
->short_p
)
10831 specs
->type
= specs
->unsigned_p
10832 ? sat_unsigned_short_fract_type_node
10833 : sat_short_fract_type_node
;
10835 specs
->type
= specs
->unsigned_p
10836 ? sat_unsigned_fract_type_node
10837 : sat_fract_type_node
;
10841 if (specs
->long_long_p
)
10842 specs
->type
= specs
->unsigned_p
10843 ? unsigned_long_long_fract_type_node
10844 : long_long_fract_type_node
;
10845 else if (specs
->long_p
)
10846 specs
->type
= specs
->unsigned_p
10847 ? unsigned_long_fract_type_node
10848 : long_fract_type_node
;
10849 else if (specs
->short_p
)
10850 specs
->type
= specs
->unsigned_p
10851 ? unsigned_short_fract_type_node
10852 : short_fract_type_node
;
10854 specs
->type
= specs
->unsigned_p
10855 ? unsigned_fract_type_node
10860 gcc_assert (!specs
->complex_p
);
10861 if (!targetm
.fixed_point_supported_p ())
10862 specs
->type
= integer_type_node
;
10863 else if (specs
->saturating_p
)
10865 if (specs
->long_long_p
)
10866 specs
->type
= specs
->unsigned_p
10867 ? sat_unsigned_long_long_accum_type_node
10868 : sat_long_long_accum_type_node
;
10869 else if (specs
->long_p
)
10870 specs
->type
= specs
->unsigned_p
10871 ? sat_unsigned_long_accum_type_node
10872 : sat_long_accum_type_node
;
10873 else if (specs
->short_p
)
10874 specs
->type
= specs
->unsigned_p
10875 ? sat_unsigned_short_accum_type_node
10876 : sat_short_accum_type_node
;
10878 specs
->type
= specs
->unsigned_p
10879 ? sat_unsigned_accum_type_node
10880 : sat_accum_type_node
;
10884 if (specs
->long_long_p
)
10885 specs
->type
= specs
->unsigned_p
10886 ? unsigned_long_long_accum_type_node
10887 : long_long_accum_type_node
;
10888 else if (specs
->long_p
)
10889 specs
->type
= specs
->unsigned_p
10890 ? unsigned_long_accum_type_node
10891 : long_accum_type_node
;
10892 else if (specs
->short_p
)
10893 specs
->type
= specs
->unsigned_p
10894 ? unsigned_short_accum_type_node
10895 : short_accum_type_node
;
10897 specs
->type
= specs
->unsigned_p
10898 ? unsigned_accum_type_node
10903 gcc_unreachable ();
10909 /* Perform final processing on one file scope's declarations (or the
10910 external scope's declarations), GLOBALS. */
10913 c_write_global_declarations_1 (tree globals
)
10918 /* Process the decls in the order they were written. */
10919 for (decl
= globals
; decl
; decl
= DECL_CHAIN (decl
))
10921 /* Check for used but undefined static functions using the C
10922 standard's definition of "used", and set TREE_NO_WARNING so
10923 that check_global_declaration doesn't repeat the check. */
10924 if (TREE_CODE (decl
) == FUNCTION_DECL
10925 && DECL_INITIAL (decl
) == 0
10926 && DECL_EXTERNAL (decl
)
10927 && !TREE_PUBLIC (decl
))
10929 if (C_DECL_USED (decl
))
10931 pedwarn (input_location
, 0, "%q+F used but never defined", decl
);
10932 TREE_NO_WARNING (decl
) = 1;
10934 /* For -Wunused-function warn about unused static prototypes. */
10935 else if (warn_unused_function
10936 && ! DECL_ARTIFICIAL (decl
)
10937 && ! TREE_NO_WARNING (decl
))
10939 warning (OPT_Wunused_function
,
10940 "%q+F declared %<static%> but never defined", decl
);
10941 TREE_NO_WARNING (decl
) = 1;
10945 wrapup_global_declaration_1 (decl
);
10950 reconsider
= false;
10951 for (decl
= globals
; decl
; decl
= DECL_CHAIN (decl
))
10952 reconsider
|= wrapup_global_declaration_2 (decl
);
10954 while (reconsider
);
10957 /* Callback to collect a source_ref from a DECL. */
10960 collect_source_ref_cb (tree decl
)
10962 if (!DECL_IS_BUILTIN (decl
))
10963 collect_source_ref (LOCATION_FILE (decl_sloc (decl
, false)));
10966 /* Preserve the external declarations scope across a garbage collect. */
10967 static GTY(()) tree ext_block
;
10969 /* Collect all references relevant to SOURCE_FILE. */
10972 collect_all_refs (const char *source_file
)
10977 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
10978 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t
)), source_file
);
10980 collect_ada_nodes (BLOCK_VARS (ext_block
), source_file
);
10983 /* Iterate over all global declarations and call CALLBACK. */
10986 for_each_global_decl (void (*callback
) (tree decl
))
10993 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
10995 decls
= DECL_INITIAL (t
);
10996 for (decl
= BLOCK_VARS (decls
); decl
; decl
= TREE_CHAIN (decl
))
11000 for (decl
= BLOCK_VARS (ext_block
); decl
; decl
= TREE_CHAIN (decl
))
11004 /* Perform any final parser cleanups and generate initial debugging
11008 c_parse_final_cleanups (void)
11013 /* We don't want to do this if generating a PCH. */
11017 timevar_stop (TV_PHASE_PARSING
);
11018 timevar_start (TV_PHASE_DEFERRED
);
11020 /* Do the Objective-C stuff. This is where all the Objective-C
11021 module stuff gets generated (symtab, class/protocol/selector
11023 if (c_dialect_objc ())
11024 objc_write_global_declarations ();
11026 /* Close the external scope. */
11027 ext_block
= pop_scope ();
11028 external_scope
= 0;
11029 gcc_assert (!current_scope
);
11031 /* Handle -fdump-ada-spec[-slim]. */
11032 if (flag_dump_ada_spec
|| flag_dump_ada_spec_slim
)
11034 /* Build a table of files to generate specs for */
11035 if (flag_dump_ada_spec_slim
)
11036 collect_source_ref (main_input_filename
);
11038 for_each_global_decl (collect_source_ref_cb
);
11040 dump_ada_specs (collect_all_refs
, NULL
);
11045 tree tmp
= BLOCK_VARS (ext_block
);
11047 FILE * stream
= dump_begin (TDI_tu
, &flags
);
11050 dump_node (tmp
, flags
& ~TDF_SLIM
, stream
);
11051 dump_end (TDI_tu
, stream
);
11055 /* Process all file scopes in this compilation, and the external_scope,
11056 through wrapup_global_declarations. */
11057 FOR_EACH_VEC_ELT (*all_translation_units
, i
, t
)
11058 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t
)));
11059 c_write_global_declarations_1 (BLOCK_VARS (ext_block
));
11061 timevar_stop (TV_PHASE_DEFERRED
);
11062 timevar_start (TV_PHASE_PARSING
);
11067 /* Register reserved keyword WORD as qualifier for address space AS. */
11070 c_register_addr_space (const char *word
, addr_space_t as
)
11072 int rid
= RID_FIRST_ADDR_SPACE
+ as
;
11075 /* Address space qualifiers are only supported
11076 in C with GNU extensions enabled. */
11077 if (c_dialect_objc () || flag_no_asm
)
11080 id
= get_identifier (word
);
11081 C_SET_RID_CODE (id
, rid
);
11082 C_IS_RESERVED_WORD (id
) = 1;
11083 ridpointers
[rid
] = id
;
11086 /* Return identifier to look up for omp declare reduction. */
11089 c_omp_reduction_id (enum tree_code reduction_code
, tree reduction_id
)
11091 const char *p
= NULL
;
11092 switch (reduction_code
)
11094 case PLUS_EXPR
: p
= "+"; break;
11095 case MULT_EXPR
: p
= "*"; break;
11096 case MINUS_EXPR
: p
= "-"; break;
11097 case BIT_AND_EXPR
: p
= "&"; break;
11098 case BIT_XOR_EXPR
: p
= "^"; break;
11099 case BIT_IOR_EXPR
: p
= "|"; break;
11100 case TRUTH_ANDIF_EXPR
: p
= "&&"; break;
11101 case TRUTH_ORIF_EXPR
: p
= "||"; break;
11102 case MIN_EXPR
: p
= "min"; break;
11103 case MAX_EXPR
: p
= "max"; break;
11110 if (TREE_CODE (reduction_id
) != IDENTIFIER_NODE
)
11111 return error_mark_node
;
11112 p
= IDENTIFIER_POINTER (reduction_id
);
11115 const char prefix
[] = "omp declare reduction ";
11116 size_t lenp
= sizeof (prefix
);
11117 size_t len
= strlen (p
);
11118 char *name
= XALLOCAVEC (char, lenp
+ len
);
11119 memcpy (name
, prefix
, lenp
- 1);
11120 memcpy (name
+ lenp
- 1, p
, len
+ 1);
11121 return get_identifier (name
);
11124 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11125 VAR_DECL, bind it into the current scope and return it. */
11128 c_omp_reduction_decl (tree reduction_id
)
11130 struct c_binding
*b
= I_SYMBOL_BINDING (reduction_id
);
11131 if (b
!= NULL
&& B_IN_CURRENT_SCOPE (b
))
11134 tree decl
= build_decl (BUILTINS_LOCATION
, VAR_DECL
,
11135 reduction_id
, integer_type_node
);
11136 DECL_ARTIFICIAL (decl
) = 1;
11137 DECL_EXTERNAL (decl
) = 1;
11138 TREE_STATIC (decl
) = 1;
11139 TREE_PUBLIC (decl
) = 0;
11140 bind (reduction_id
, decl
, current_scope
, true, false, BUILTINS_LOCATION
);
11144 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11147 c_omp_reduction_lookup (tree reduction_id
, tree type
)
11149 struct c_binding
*b
= I_SYMBOL_BINDING (reduction_id
);
11153 for (t
= DECL_INITIAL (b
->decl
); t
; t
= TREE_CHAIN (t
))
11154 if (comptypes (TREE_PURPOSE (t
), type
))
11155 return TREE_VALUE (t
);
11158 return error_mark_node
;
11161 /* Helper function called via walk_tree, to diagnose invalid
11162 #pragma omp declare reduction combiners or initializers. */
11165 c_check_omp_declare_reduction_r (tree
*tp
, int *, void *data
)
11167 tree
*vars
= (tree
*) data
;
11168 if (SSA_VAR_P (*tp
)
11169 && !DECL_ARTIFICIAL (*tp
)
11173 location_t loc
= DECL_SOURCE_LOCATION (vars
[0]);
11174 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars
[0])), "omp_out") == 0)
11175 error_at (loc
, "%<#pragma omp declare reduction%> combiner refers to "
11176 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11179 error_at (loc
, "%<#pragma omp declare reduction%> initializer refers "
11180 "to variable %qD which is not %<omp_priv%> nor "
11188 #include "gt-c-c-decl.h"