1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
31 #include "coretypes.h"
36 #include "tree-inline.h"
53 #include "langhooks.h"
54 #include "tree-mudflap.h"
55 #include "tree-gimple.h"
56 #include "diagnostic.h"
57 #include "tree-dump.h"
62 #include "langhooks-def.h"
64 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
66 { NORMAL
, /* Ordinary declaration */
67 FUNCDEF
, /* Function definition */
68 PARM
, /* Declaration of parm before function body */
69 FIELD
, /* Declaration inside struct or union */
70 TYPENAME
}; /* Typename (inside cast or sizeof) */
73 /* Nonzero if we have seen an invalid cross reference
74 to a struct, union, or enum, but not yet printed the message. */
75 tree pending_invalid_xref
;
77 /* File and line to appear in the eventual error message. */
78 location_t pending_invalid_xref_location
;
80 /* True means we've initialized exception handling. */
81 bool c_eh_initialized_p
;
83 /* While defining an enum type, this is 1 plus the last enumerator
84 constant value. Note that will do not have to save this or `enum_overflow'
85 around nested function definition since such a definition could only
86 occur in an enum value expression and we don't use these variables in
89 static tree enum_next_value
;
91 /* Nonzero means that there was overflow computing enum_next_value. */
93 static int enum_overflow
;
95 /* The file and line that the prototype came from if this is an
96 old-style definition; used for diagnostics in
97 store_parm_decls_oldstyle. */
99 static location_t current_function_prototype_locus
;
101 /* Whether this prototype was built-in. */
103 static bool current_function_prototype_built_in
;
105 /* The argument type information of this prototype. */
107 static tree current_function_prototype_arg_types
;
109 /* The argument information structure for the function currently being
112 static struct c_arg_info
*current_function_arg_info
;
114 /* The obstack on which parser and related data structures, which are
115 not live beyond their top-level declaration or definition, are
117 struct obstack parser_obstack
;
119 /* The current statement tree. */
121 static GTY(()) struct stmt_tree_s c_stmt_tree
;
123 /* State saving variables. */
127 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
128 included in this invocation. Note that the current translation
129 unit is not included in this list. */
131 static GTY(()) tree all_translation_units
;
133 /* A list of decls to be made automatically visible in each file scope. */
134 static GTY(()) tree visible_builtins
;
136 /* Set to 0 at beginning of a function definition, set to 1 if
137 a return statement that specifies a return value is seen. */
139 int current_function_returns_value
;
141 /* Set to 0 at beginning of a function definition, set to 1 if
142 a return statement with no argument is seen. */
144 int current_function_returns_null
;
146 /* Set to 0 at beginning of a function definition, set to 1 if
147 a call to a noreturn function is seen. */
149 int current_function_returns_abnormally
;
151 /* Set to nonzero by `grokdeclarator' for a function
152 whose return type is defaulted, if warnings for this are desired. */
154 static int warn_about_return_type
;
156 /* Nonzero when starting a function declared `extern inline'. */
158 static int current_extern_inline
;
160 /* Nonzero when the current toplevel function contains a declaration
161 of a nested function which is never defined. */
163 static bool undef_nested_function
;
165 /* True means global_bindings_p should return false even if the scope stack
166 says we are in file scope. */
167 bool c_override_global_bindings_to_false
;
170 /* Each c_binding structure describes one binding of an identifier to
171 a decl. All the decls in a scope - irrespective of namespace - are
172 chained together by the ->prev field, which (as the name implies)
173 runs in reverse order. All the decls in a given namespace bound to
174 a given identifier are chained by the ->shadowed field, which runs
175 from inner to outer scopes.
177 The ->decl field usually points to a DECL node, but there are two
178 exceptions. In the namespace of type tags, the bound entity is a
179 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
180 identifier is encountered, it is bound to error_mark_node to
181 suppress further errors about that identifier in the current
184 The ->type field stores the type of the declaration in this scope;
185 if NULL, the type is the type of the ->decl field. This is only of
186 relevance for objects with external or internal linkage which may
187 be redeclared in inner scopes, forming composite types that only
188 persist for the duration of those scopes. In the external scope,
189 this stores the composite of all the types declared for this
190 object, visible or not. The ->inner_comp field (used only at file
191 scope) stores whether an incomplete array type at file scope was
192 completed at an inner scope to an array size other than 1.
194 The depth field is copied from the scope structure that holds this
195 decl. It is used to preserve the proper ordering of the ->shadowed
196 field (see bind()) and also for a handful of special-case checks.
197 Finally, the invisible bit is true for a decl which should be
198 ignored for purposes of normal name lookup, and the nested bit is
199 true for a decl that's been bound a second time in an inner scope;
200 in all such cases, the binding in the outer scope will have its
201 invisible bit true. */
203 struct c_binding
GTY((chain_next ("%h.prev")))
205 tree decl
; /* the decl bound */
206 tree type
; /* the type in this scope */
207 tree id
; /* the identifier it's bound to */
208 struct c_binding
*prev
; /* the previous decl in this scope */
209 struct c_binding
*shadowed
; /* the innermost decl shadowed by this one */
210 unsigned int depth
: 28; /* depth of this scope */
211 BOOL_BITFIELD invisible
: 1; /* normal lookup should ignore this binding */
212 BOOL_BITFIELD nested
: 1; /* do not set DECL_CONTEXT when popping */
213 BOOL_BITFIELD inner_comp
: 1; /* incomplete array completed in inner scope */
216 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
217 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
218 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
219 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
221 #define I_SYMBOL_BINDING(node) \
222 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
223 #define I_SYMBOL_DECL(node) \
224 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
226 #define I_TAG_BINDING(node) \
227 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
228 #define I_TAG_DECL(node) \
229 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
231 #define I_LABEL_BINDING(node) \
232 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
233 #define I_LABEL_DECL(node) \
234 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
236 /* Each C symbol points to three linked lists of c_binding structures.
237 These describe the values of the identifier in the three different
238 namespaces defined by the language. */
240 struct lang_identifier
GTY(())
242 struct c_common_identifier common_id
;
243 struct c_binding
*symbol_binding
; /* vars, funcs, constants, typedefs */
244 struct c_binding
*tag_binding
; /* struct/union/enum tags */
245 struct c_binding
*label_binding
; /* labels */
248 /* Validate c-lang.c's assumptions. */
249 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
250 [(sizeof(struct lang_identifier
) == C_SIZEOF_STRUCT_LANG_IDENTIFIER
) ? 1 : -1];
252 /* The resulting tree type. */
255 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
256 chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *) TYPE_NEXT_VARIANT (&%h.generic) : (union lang_tree_node *) TREE_CHAIN (&%h.generic)")))
258 union tree_node
GTY ((tag ("0"),
259 desc ("tree_node_structure (&%h)")))
261 struct lang_identifier
GTY ((tag ("1"))) identifier
;
264 /* Each c_scope structure describes the complete contents of one
265 scope. Four scopes are distinguished specially: the innermost or
266 current scope, the innermost function scope, the file scope (always
267 the second to outermost) and the outermost or external scope.
269 Most declarations are recorded in the current scope.
271 All normal label declarations are recorded in the innermost
272 function scope, as are bindings of undeclared identifiers to
273 error_mark_node. (GCC permits nested functions as an extension,
274 hence the 'innermost' qualifier.) Explicitly declared labels
275 (using the __label__ extension) appear in the current scope.
277 Being in the file scope (current_scope == file_scope) causes
278 special behavior in several places below. Also, under some
279 conditions the Objective-C front end records declarations in the
280 file scope even though that isn't the current scope.
282 All declarations with external linkage are recorded in the external
283 scope, even if they aren't visible there; this models the fact that
284 such declarations are visible to the entire program, and (with a
285 bit of cleverness, see pushdecl) allows diagnosis of some violations
286 of C99 6.2.2p7 and 6.2.7p2:
288 If, within the same translation unit, the same identifier appears
289 with both internal and external linkage, the behavior is
292 All declarations that refer to the same object or function shall
293 have compatible type; otherwise, the behavior is undefined.
295 Initially only the built-in declarations, which describe compiler
296 intrinsic functions plus a subset of the standard library, are in
299 The order of the blocks list matters, and it is frequently appended
300 to. To avoid having to walk all the way to the end of the list on
301 each insertion, or reverse the list later, we maintain a pointer to
302 the last list entry. (FIXME: It should be feasible to use a reversed
305 The bindings list is strictly in reverse order of declarations;
306 pop_scope relies on this. */
309 struct c_scope
GTY((chain_next ("%h.outer")))
311 /* The scope containing this one. */
312 struct c_scope
*outer
;
314 /* The next outermost function scope. */
315 struct c_scope
*outer_function
;
317 /* All bindings in this scope. */
318 struct c_binding
*bindings
;
320 /* For each scope (except the global one), a chain of BLOCK nodes
321 for all the scopes that were entered and exited one level down. */
325 /* The depth of this scope. Used to keep the ->shadowed chain of
326 bindings sorted innermost to outermost. */
327 unsigned int depth
: 28;
329 /* True if we are currently filling this scope with parameter
331 BOOL_BITFIELD parm_flag
: 1;
333 /* True if we already complained about forward parameter decls
334 in this scope. This prevents double warnings on
335 foo (int a; int b; ...) */
336 BOOL_BITFIELD warned_forward_parm_decls
: 1;
338 /* True if this is the outermost block scope of a function body.
339 This scope contains the parameters, the local variables declared
340 in the outermost block, and all the labels (except those in
341 nested functions, or declared at block scope with __label__). */
342 BOOL_BITFIELD function_body
: 1;
344 /* True means make a BLOCK for this scope no matter what. */
345 BOOL_BITFIELD keep
: 1;
348 /* The scope currently in effect. */
350 static GTY(()) struct c_scope
*current_scope
;
352 /* The innermost function scope. Ordinary (not explicitly declared)
353 labels, bindings to error_mark_node, and the lazily-created
354 bindings of __func__ and its friends get this scope. */
356 static GTY(()) struct c_scope
*current_function_scope
;
358 /* The C file scope. This is reset for each input translation unit. */
360 static GTY(()) struct c_scope
*file_scope
;
362 /* The outermost scope. This is used for all declarations with
363 external linkage, and only these, hence the name. */
365 static GTY(()) struct c_scope
*external_scope
;
367 /* A chain of c_scope structures awaiting reuse. */
369 static GTY((deletable
)) struct c_scope
*scope_freelist
;
371 /* A chain of c_binding structures awaiting reuse. */
373 static GTY((deletable
)) struct c_binding
*binding_freelist
;
375 /* Append VAR to LIST in scope SCOPE. */
376 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
377 struct c_scope *s_ = (scope); \
379 if (s_->list##_last) \
380 TREE_CHAIN (s_->list##_last) = d_; \
383 s_->list##_last = d_; \
386 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
387 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
388 struct c_scope *t_ = (tscope); \
389 struct c_scope *f_ = (fscope); \
391 TREE_CHAIN (t_->to##_last) = f_->from; \
394 t_->to##_last = f_->from##_last; \
397 /* True means unconditionally make a BLOCK for the next scope pushed. */
399 static bool keep_next_level_flag
;
401 /* True means the next call to push_scope will be the outermost scope
402 of a function body, so do not push a new scope, merely cease
403 expecting parameter decls. */
405 static bool next_is_function_body
;
407 /* Functions called automatically at the beginning and end of execution. */
409 static GTY(()) tree static_ctors
;
410 static GTY(()) tree static_dtors
;
412 /* Forward declarations. */
413 static tree
lookup_name_in_scope (tree
, struct c_scope
*);
414 static tree
c_make_fname_decl (tree
, int);
415 static tree
grokdeclarator (const struct c_declarator
*,
416 struct c_declspecs
*,
417 enum decl_context
, bool, tree
*);
418 static tree
grokparms (struct c_arg_info
*, bool);
419 static void layout_array_type (tree
);
421 /* States indicating how grokdeclarator() should handle declspecs marked
422 with __attribute__((deprecated)). An object declared as
423 __attribute__((deprecated)) suppresses warnings of uses of other
426 enum deprecated_states
{
431 static enum deprecated_states deprecated_state
= DEPRECATED_NORMAL
;
434 c_print_identifier (FILE *file
, tree node
, int indent
)
436 print_node (file
, "symbol", I_SYMBOL_DECL (node
), indent
+ 4);
437 print_node (file
, "tag", I_TAG_DECL (node
), indent
+ 4);
438 print_node (file
, "label", I_LABEL_DECL (node
), indent
+ 4);
439 if (C_IS_RESERVED_WORD (node
))
441 tree rid
= ridpointers
[C_RID_CODE (node
)];
442 indent_to (file
, indent
+ 4);
443 fprintf (file
, "rid " HOST_PTR_PRINTF
" \"%s\"",
444 (void *) rid
, IDENTIFIER_POINTER (rid
));
448 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
449 which may be any of several kinds of DECL or TYPE or error_mark_node,
450 in the scope SCOPE. */
452 bind (tree name
, tree decl
, struct c_scope
*scope
, bool invisible
, bool nested
)
454 struct c_binding
*b
, **here
;
456 if (binding_freelist
)
458 b
= binding_freelist
;
459 binding_freelist
= b
->prev
;
462 b
= GGC_NEW (struct c_binding
);
467 b
->depth
= scope
->depth
;
468 b
->invisible
= invisible
;
474 b
->prev
= scope
->bindings
;
480 switch (TREE_CODE (decl
))
482 case LABEL_DECL
: here
= &I_LABEL_BINDING (name
); break;
485 case RECORD_TYPE
: here
= &I_TAG_BINDING (name
); break;
491 case ERROR_MARK
: here
= &I_SYMBOL_BINDING (name
); break;
497 /* Locate the appropriate place in the chain of shadowed decls
498 to insert this binding. Normally, scope == current_scope and
499 this does nothing. */
500 while (*here
&& (*here
)->depth
> scope
->depth
)
501 here
= &(*here
)->shadowed
;
507 /* Clear the binding structure B, stick it on the binding_freelist,
508 and return the former value of b->prev. This is used by pop_scope
509 and get_parm_info to iterate destructively over all the bindings
510 from a given scope. */
511 static struct c_binding
*
512 free_binding_and_advance (struct c_binding
*b
)
514 struct c_binding
*prev
= b
->prev
;
516 memset (b
, 0, sizeof (struct c_binding
));
517 b
->prev
= binding_freelist
;
518 binding_freelist
= b
;
524 /* Hook called at end of compilation to assume 1 elt
525 for a file-scope tentative array defn that wasn't complete before. */
528 c_finish_incomplete_decl (tree decl
)
530 if (TREE_CODE (decl
) == VAR_DECL
)
532 tree type
= TREE_TYPE (decl
);
533 if (type
!= error_mark_node
534 && TREE_CODE (type
) == ARRAY_TYPE
535 && !DECL_EXTERNAL (decl
)
536 && TYPE_DOMAIN (type
) == 0)
538 warning (0, "%Jarray %qD assumed to have one element", decl
, decl
);
540 complete_array_type (&TREE_TYPE (decl
), NULL_TREE
, true);
542 layout_decl (decl
, 0);
547 /* The Objective-C front-end often needs to determine the current scope. */
550 objc_get_current_scope (void)
552 return current_scope
;
555 /* The following function is used only by Objective-C. It needs to live here
556 because it accesses the innards of c_scope. */
559 objc_mark_locals_volatile (void *enclosing_blk
)
561 struct c_scope
*scope
;
564 for (scope
= current_scope
;
565 scope
&& scope
!= enclosing_blk
;
566 scope
= scope
->outer
)
568 for (b
= scope
->bindings
; b
; b
= b
->prev
)
570 if (TREE_CODE (b
->decl
) == VAR_DECL
571 || TREE_CODE (b
->decl
) == PARM_DECL
)
573 C_DECL_REGISTER (b
->decl
) = 0;
574 DECL_REGISTER (b
->decl
) = 0;
575 TREE_THIS_VOLATILE (b
->decl
) = 1;
579 /* Do not climb up past the current function. */
580 if (scope
->function_body
)
585 /* Nonzero if we are currently in file scope. */
588 global_bindings_p (void)
590 return current_scope
== file_scope
&& !c_override_global_bindings_to_false
;
594 keep_next_level (void)
596 keep_next_level_flag
= true;
599 /* Identify this scope as currently being filled with parameters. */
602 declare_parm_level (void)
604 current_scope
->parm_flag
= true;
610 if (next_is_function_body
)
612 /* This is the transition from the parameters to the top level
613 of the function body. These are the same scope
614 (C99 6.2.1p4,6) so we do not push another scope structure.
615 next_is_function_body is set only by store_parm_decls, which
616 in turn is called when and only when we are about to
617 encounter the opening curly brace for the function body.
619 The outermost block of a function always gets a BLOCK node,
620 because the debugging output routines expect that each
621 function has at least one BLOCK. */
622 current_scope
->parm_flag
= false;
623 current_scope
->function_body
= true;
624 current_scope
->keep
= true;
625 current_scope
->outer_function
= current_function_scope
;
626 current_function_scope
= current_scope
;
628 keep_next_level_flag
= false;
629 next_is_function_body
= false;
633 struct c_scope
*scope
;
636 scope
= scope_freelist
;
637 scope_freelist
= scope
->outer
;
640 scope
= GGC_CNEW (struct c_scope
);
642 scope
->keep
= keep_next_level_flag
;
643 scope
->outer
= current_scope
;
644 scope
->depth
= current_scope
? (current_scope
->depth
+ 1) : 0;
646 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
648 if (current_scope
&& scope
->depth
== 0)
651 sorry ("GCC supports only %u nested scopes", scope
->depth
);
654 current_scope
= scope
;
655 keep_next_level_flag
= false;
659 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
662 set_type_context (tree type
, tree context
)
664 for (type
= TYPE_MAIN_VARIANT (type
); type
;
665 type
= TYPE_NEXT_VARIANT (type
))
666 TYPE_CONTEXT (type
) = context
;
669 /* Exit a scope. Restore the state of the identifier-decl mappings
670 that were in effect when this scope was entered. Return a BLOCK
671 node containing all the DECLs in this scope that are of interest
672 to debug info generation. */
677 struct c_scope
*scope
= current_scope
;
678 tree block
, context
, p
;
681 bool functionbody
= scope
->function_body
;
682 bool keep
= functionbody
|| scope
->keep
|| scope
->bindings
;
684 c_end_vm_scope (scope
->depth
);
686 /* If appropriate, create a BLOCK to record the decls for the life
691 block
= make_node (BLOCK
);
692 BLOCK_SUBBLOCKS (block
) = scope
->blocks
;
693 TREE_USED (block
) = 1;
695 /* In each subblock, record that this is its superior. */
696 for (p
= scope
->blocks
; p
; p
= TREE_CHAIN (p
))
697 BLOCK_SUPERCONTEXT (p
) = block
;
699 BLOCK_VARS (block
) = 0;
702 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
703 scope must be set so that they point to the appropriate
704 construct, i.e. either to the current FUNCTION_DECL node, or
705 else to the BLOCK node we just constructed.
707 Note that for tagged types whose scope is just the formal
708 parameter list for some function type specification, we can't
709 properly set their TYPE_CONTEXTs here, because we don't have a
710 pointer to the appropriate FUNCTION_TYPE node readily available
711 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
712 type nodes get set in `grokdeclarator' as soon as we have created
713 the FUNCTION_TYPE node which will represent the "scope" for these
714 "parameter list local" tagged types. */
715 if (scope
->function_body
)
716 context
= current_function_decl
;
717 else if (scope
== file_scope
)
719 tree file_decl
= build_decl (TRANSLATION_UNIT_DECL
, 0, 0);
720 TREE_CHAIN (file_decl
) = all_translation_units
;
721 all_translation_units
= file_decl
;
727 /* Clear all bindings in this scope. */
728 for (b
= scope
->bindings
; b
; b
= free_binding_and_advance (b
))
731 switch (TREE_CODE (p
))
734 /* Warnings for unused labels, errors for undefined labels. */
735 if (TREE_USED (p
) && !DECL_INITIAL (p
))
737 error ("%Jlabel %qD used but not defined", p
, p
);
738 DECL_INITIAL (p
) = error_mark_node
;
740 else if (!TREE_USED (p
) && warn_unused_label
)
742 if (DECL_INITIAL (p
))
743 warning (0, "%Jlabel %qD defined but not used", p
, p
);
745 warning (0, "%Jlabel %qD declared but not defined", p
, p
);
747 /* Labels go in BLOCK_VARS. */
748 TREE_CHAIN (p
) = BLOCK_VARS (block
);
749 BLOCK_VARS (block
) = p
;
750 gcc_assert (I_LABEL_BINDING (b
->id
) == b
);
751 I_LABEL_BINDING (b
->id
) = b
->shadowed
;
757 set_type_context (p
, context
);
759 /* Types may not have tag-names, in which case the type
760 appears in the bindings list with b->id NULL. */
763 gcc_assert (I_TAG_BINDING (b
->id
) == b
);
764 I_TAG_BINDING (b
->id
) = b
->shadowed
;
769 /* Propagate TREE_ADDRESSABLE from nested functions to their
770 containing functions. */
771 if (!TREE_ASM_WRITTEN (p
)
772 && DECL_INITIAL (p
) != 0
773 && TREE_ADDRESSABLE (p
)
774 && DECL_ABSTRACT_ORIGIN (p
) != 0
775 && DECL_ABSTRACT_ORIGIN (p
) != p
)
776 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p
)) = 1;
777 if (!DECL_EXTERNAL (p
)
778 && DECL_INITIAL (p
) == 0)
780 error ("%Jnested function %qD declared but never defined", p
, p
);
781 undef_nested_function
= true;
786 /* Warnings for unused variables. */
787 if (warn_unused_variable
789 && !DECL_IN_SYSTEM_HEADER (p
)
791 && !DECL_ARTIFICIAL (p
)
792 && scope
!= file_scope
793 && scope
!= external_scope
)
794 warning (0, "%Junused variable %qD", p
, p
);
798 error ("%Jtype of array %qD completed incompatibly with"
799 " implicit initialization", p
, p
);
806 /* All of these go in BLOCK_VARS, but only if this is the
807 binding in the home scope. */
810 TREE_CHAIN (p
) = BLOCK_VARS (block
);
811 BLOCK_VARS (block
) = p
;
813 /* If this is the file scope, and we are processing more
814 than one translation unit in this compilation, set
815 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
816 This makes same_translation_unit_p work, and causes
817 static declarations to be given disambiguating suffixes. */
818 if (scope
== file_scope
&& num_in_fnames
> 1)
820 DECL_CONTEXT (p
) = context
;
821 if (TREE_CODE (p
) == TYPE_DECL
)
822 set_type_context (TREE_TYPE (p
), context
);
826 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
827 already been put there by store_parm_decls. Unused-
828 parameter warnings are handled by function.c.
829 error_mark_node obviously does not go in BLOCK_VARS and
830 does not get unused-variable warnings. */
833 /* It is possible for a decl not to have a name. We get
834 here with b->id NULL in this case. */
837 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
838 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
839 if (b
->shadowed
&& b
->shadowed
->type
)
840 TREE_TYPE (b
->shadowed
->decl
) = b
->shadowed
->type
;
850 /* Dispose of the block that we just made inside some higher level. */
851 if ((scope
->function_body
|| scope
== file_scope
) && context
)
853 DECL_INITIAL (context
) = block
;
854 BLOCK_SUPERCONTEXT (block
) = context
;
856 else if (scope
->outer
)
859 SCOPE_LIST_APPEND (scope
->outer
, blocks
, block
);
860 /* If we did not make a block for the scope just exited, any
861 blocks made for inner scopes must be carried forward so they
862 will later become subblocks of something else. */
863 else if (scope
->blocks
)
864 SCOPE_LIST_CONCAT (scope
->outer
, blocks
, scope
, blocks
);
867 /* Pop the current scope, and free the structure for reuse. */
868 current_scope
= scope
->outer
;
869 if (scope
->function_body
)
870 current_function_scope
= scope
->outer_function
;
872 memset (scope
, 0, sizeof (struct c_scope
));
873 scope
->outer
= scope_freelist
;
874 scope_freelist
= scope
;
880 push_file_scope (void)
888 file_scope
= current_scope
;
890 start_fname_decls ();
892 for (decl
= visible_builtins
; decl
; decl
= TREE_CHAIN (decl
))
893 bind (DECL_NAME (decl
), decl
, file_scope
,
894 /*invisible=*/false, /*nested=*/true);
898 pop_file_scope (void)
900 /* In case there were missing closebraces, get us back to the global
902 while (current_scope
!= file_scope
)
905 /* __FUNCTION__ is defined at file scope (""). This
906 call may not be necessary as my tests indicate it
907 still works without it. */
908 finish_fname_decls ();
910 /* This is the point to write out a PCH if we're doing that.
911 In that case we do not want to do anything else. */
914 c_common_write_pch ();
918 /* Pop off the file scope and close this translation unit. */
922 maybe_apply_pending_pragma_weaks ();
923 cgraph_finalize_compilation_unit ();
926 /* Insert BLOCK at the end of the list of subblocks of the current
927 scope. This is used when a BIND_EXPR is expanded, to handle the
928 BLOCK node inside the BIND_EXPR. */
931 insert_block (tree block
)
933 TREE_USED (block
) = 1;
934 SCOPE_LIST_APPEND (current_scope
, blocks
, block
);
937 /* Push a definition or a declaration of struct, union or enum tag "name".
938 "type" should be the type node.
939 We assume that the tag "name" is not already defined.
941 Note that the definition may really be just a forward reference.
942 In that case, the TYPE_SIZE will be zero. */
945 pushtag (tree name
, tree type
)
947 /* Record the identifier as the type's name if it has none. */
948 if (name
&& !TYPE_NAME (type
))
949 TYPE_NAME (type
) = name
;
950 bind (name
, type
, current_scope
, /*invisible=*/false, /*nested=*/false);
952 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
953 tagged type we just added to the current scope. This fake
954 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
955 to output a representation of a tagged type, and it also gives
956 us a convenient place to record the "scope start" address for the
959 TYPE_STUB_DECL (type
) = pushdecl (build_decl (TYPE_DECL
, NULL_TREE
, type
));
961 /* An approximation for now, so we can tell this is a function-scope tag.
962 This will be updated in pop_scope. */
963 TYPE_CONTEXT (type
) = DECL_CONTEXT (TYPE_STUB_DECL (type
));
966 /* Subroutine of compare_decls. Allow harmless mismatches in return
967 and argument types provided that the type modes match. This function
968 return a unified type given a suitable match, and 0 otherwise. */
971 match_builtin_function_types (tree newtype
, tree oldtype
)
973 tree newrettype
, oldrettype
;
974 tree newargs
, oldargs
;
975 tree trytype
, tryargs
;
977 /* Accept the return type of the new declaration if same modes. */
978 oldrettype
= TREE_TYPE (oldtype
);
979 newrettype
= TREE_TYPE (newtype
);
981 if (TYPE_MODE (oldrettype
) != TYPE_MODE (newrettype
))
984 oldargs
= TYPE_ARG_TYPES (oldtype
);
985 newargs
= TYPE_ARG_TYPES (newtype
);
988 while (oldargs
|| newargs
)
992 || !TREE_VALUE (oldargs
)
993 || !TREE_VALUE (newargs
)
994 || TYPE_MODE (TREE_VALUE (oldargs
))
995 != TYPE_MODE (TREE_VALUE (newargs
)))
998 oldargs
= TREE_CHAIN (oldargs
);
999 newargs
= TREE_CHAIN (newargs
);
1002 trytype
= build_function_type (newrettype
, tryargs
);
1003 return build_type_attribute_variant (trytype
, TYPE_ATTRIBUTES (oldtype
));
1006 /* Subroutine of diagnose_mismatched_decls. Check for function type
1007 mismatch involving an empty arglist vs a nonempty one and give clearer
1010 diagnose_arglist_conflict (tree newdecl
, tree olddecl
,
1011 tree newtype
, tree oldtype
)
1015 if (TREE_CODE (olddecl
) != FUNCTION_DECL
1016 || !comptypes (TREE_TYPE (oldtype
), TREE_TYPE (newtype
))
1017 || !((TYPE_ARG_TYPES (oldtype
) == 0 && DECL_INITIAL (olddecl
) == 0)
1019 (TYPE_ARG_TYPES (newtype
) == 0 && DECL_INITIAL (newdecl
) == 0)))
1022 t
= TYPE_ARG_TYPES (oldtype
);
1024 t
= TYPE_ARG_TYPES (newtype
);
1025 for (; t
; t
= TREE_CHAIN (t
))
1027 tree type
= TREE_VALUE (t
);
1029 if (TREE_CHAIN (t
) == 0
1030 && TYPE_MAIN_VARIANT (type
) != void_type_node
)
1032 inform ("a parameter list with an ellipsis can%'t match "
1033 "an empty parameter name list declaration");
1037 if (c_type_promotes_to (type
) != type
)
1039 inform ("an argument type that has a default promotion can%'t match "
1040 "an empty parameter name list declaration");
1046 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1047 old-style function definition, NEWDECL is a prototype declaration.
1048 Diagnose inconsistencies in the argument list. Returns TRUE if
1049 the prototype is compatible, FALSE if not. */
1051 validate_proto_after_old_defn (tree newdecl
, tree newtype
, tree oldtype
)
1053 tree newargs
, oldargs
;
1056 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1058 oldargs
= TYPE_ACTUAL_ARG_TYPES (oldtype
);
1059 newargs
= TYPE_ARG_TYPES (newtype
);
1064 tree oldargtype
= TYPE_MAIN_VARIANT (TREE_VALUE (oldargs
));
1065 tree newargtype
= TYPE_MAIN_VARIANT (TREE_VALUE (newargs
));
1067 if (END_OF_ARGLIST (oldargtype
) && END_OF_ARGLIST (newargtype
))
1070 /* Reaching the end of just one list means the two decls don't
1071 agree on the number of arguments. */
1072 if (END_OF_ARGLIST (oldargtype
))
1074 error ("%Jprototype for %qD declares more arguments "
1075 "than previous old-style definition", newdecl
, newdecl
);
1078 else if (END_OF_ARGLIST (newargtype
))
1080 error ("%Jprototype for %qD declares fewer arguments "
1081 "than previous old-style definition", newdecl
, newdecl
);
1085 /* Type for passing arg must be consistent with that declared
1087 else if (!comptypes (oldargtype
, newargtype
))
1089 error ("%Jprototype for %qD declares argument %d"
1090 " with incompatible type",
1091 newdecl
, newdecl
, i
);
1095 oldargs
= TREE_CHAIN (oldargs
);
1096 newargs
= TREE_CHAIN (newargs
);
1100 /* If we get here, no errors were found, but do issue a warning
1101 for this poor-style construct. */
1102 warning (0, "%Jprototype for %qD follows non-prototype definition",
1105 #undef END_OF_ARGLIST
1108 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1109 first in a pair of mismatched declarations, using the diagnostic
1112 locate_old_decl (tree decl
, void (*diag
)(const char *, ...))
1114 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (decl
))
1116 else if (DECL_INITIAL (decl
))
1117 diag (N_("%Jprevious definition of %qD was here"), decl
, decl
);
1118 else if (C_DECL_IMPLICIT (decl
))
1119 diag (N_("%Jprevious implicit declaration of %qD was here"), decl
, decl
);
1121 diag (N_("%Jprevious declaration of %qD was here"), decl
, decl
);
1124 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1125 Returns true if the caller should proceed to merge the two, false
1126 if OLDDECL should simply be discarded. As a side effect, issues
1127 all necessary diagnostics for invalid or poor-style combinations.
1128 If it returns true, writes the types of NEWDECL and OLDDECL to
1129 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1130 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1133 diagnose_mismatched_decls (tree newdecl
, tree olddecl
,
1134 tree
*newtypep
, tree
*oldtypep
)
1136 tree newtype
, oldtype
;
1137 bool pedwarned
= false;
1138 bool warned
= false;
1141 /* If we have error_mark_node for either decl or type, just discard
1142 the previous decl - we're in an error cascade already. */
1143 if (olddecl
== error_mark_node
|| newdecl
== error_mark_node
)
1145 *oldtypep
= oldtype
= TREE_TYPE (olddecl
);
1146 *newtypep
= newtype
= TREE_TYPE (newdecl
);
1147 if (oldtype
== error_mark_node
|| newtype
== error_mark_node
)
1150 /* Two different categories of symbol altogether. This is an error
1151 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1152 if (TREE_CODE (olddecl
) != TREE_CODE (newdecl
))
1154 if (!(TREE_CODE (olddecl
) == FUNCTION_DECL
1155 && DECL_BUILT_IN (olddecl
)
1156 && !C_DECL_DECLARED_BUILTIN (olddecl
)))
1158 error ("%J%qD redeclared as different kind of symbol",
1160 locate_old_decl (olddecl
, error
);
1162 else if (TREE_PUBLIC (newdecl
))
1163 warning (0, "%Jbuilt-in function %qD declared as non-function",
1165 else if (warn_shadow
)
1166 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1171 /* Enumerators have no linkage, so may only be declared once in a
1173 if (TREE_CODE (olddecl
) == CONST_DECL
)
1175 error ("%Jredeclaration of enumerator %qD", newdecl
, newdecl
);
1176 locate_old_decl (olddecl
, error
);
1180 if (!comptypes (oldtype
, newtype
))
1182 if (TREE_CODE (olddecl
) == FUNCTION_DECL
1183 && DECL_BUILT_IN (olddecl
) && !C_DECL_DECLARED_BUILTIN (olddecl
))
1185 /* Accept harmless mismatch in function types.
1186 This is for the ffs and fprintf builtins. */
1187 tree trytype
= match_builtin_function_types (newtype
, oldtype
);
1189 if (trytype
&& comptypes (newtype
, trytype
))
1190 *oldtypep
= oldtype
= trytype
;
1193 /* If types don't match for a built-in, throw away the
1194 built-in. No point in calling locate_old_decl here, it
1195 won't print anything. */
1196 warning (0, "%Jconflicting types for built-in function %qD",
1201 else if (TREE_CODE (olddecl
) == FUNCTION_DECL
1202 && DECL_IS_BUILTIN (olddecl
))
1204 /* A conflicting function declaration for a predeclared
1205 function that isn't actually built in. Objective C uses
1206 these. The new declaration silently overrides everything
1207 but the volatility (i.e. noreturn) indication. See also
1208 below. FIXME: Make Objective C use normal builtins. */
1209 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
1212 /* Permit void foo (...) to match int foo (...) if the latter is
1213 the definition and implicit int was used. See
1214 c-torture/compile/920625-2.c. */
1215 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
&& DECL_INITIAL (newdecl
)
1216 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype
)) == void_type_node
1217 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype
)) == integer_type_node
1218 && C_FUNCTION_IMPLICIT_INT (newdecl
) && !DECL_INITIAL (olddecl
))
1220 pedwarn ("%Jconflicting types for %qD", newdecl
, newdecl
);
1221 /* Make sure we keep void as the return type. */
1222 TREE_TYPE (newdecl
) = *newtypep
= newtype
= oldtype
;
1223 C_FUNCTION_IMPLICIT_INT (newdecl
) = 0;
1226 /* Permit void foo (...) to match an earlier call to foo (...) with
1227 no declared type (thus, implicitly int). */
1228 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
1229 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype
)) == void_type_node
1230 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype
)) == integer_type_node
1231 && C_DECL_IMPLICIT (olddecl
) && !DECL_INITIAL (olddecl
))
1233 pedwarn ("%Jconflicting types for %qD", newdecl
, newdecl
);
1234 /* Make sure we keep void as the return type. */
1235 TREE_TYPE (olddecl
) = *oldtypep
= oldtype
= newtype
;
1240 if (TYPE_QUALS (newtype
) != TYPE_QUALS (oldtype
))
1241 error ("%J conflicting type qualifiers for %qD", newdecl
, newdecl
);
1243 error ("%Jconflicting types for %qD", newdecl
, newdecl
);
1244 diagnose_arglist_conflict (newdecl
, olddecl
, newtype
, oldtype
);
1245 locate_old_decl (olddecl
, error
);
1250 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1251 but silently ignore the redeclaration if either is in a system
1252 header. (Conflicting redeclarations were handled above.) */
1253 if (TREE_CODE (newdecl
) == TYPE_DECL
)
1255 if (DECL_IN_SYSTEM_HEADER (newdecl
) || DECL_IN_SYSTEM_HEADER (olddecl
))
1256 return true; /* Allow OLDDECL to continue in use. */
1258 error ("%Jredefinition of typedef %qD", newdecl
, newdecl
);
1259 locate_old_decl (olddecl
, error
);
1263 /* Function declarations can either be 'static' or 'extern' (no
1264 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1265 can never conflict with each other on account of linkage (6.2.2p4).
1266 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1267 two definitions if one is 'extern inline' and one is not. The non-
1268 extern-inline definition supersedes the extern-inline definition. */
1269 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1271 /* If you declare a built-in function name as static, or
1272 define the built-in with an old-style definition (so we
1273 can't validate the argument list) the built-in definition is
1274 overridden, but optionally warn this was a bad choice of name. */
1275 if (DECL_BUILT_IN (olddecl
)
1276 && !C_DECL_DECLARED_BUILTIN (olddecl
)
1277 && (!TREE_PUBLIC (newdecl
)
1278 || (DECL_INITIAL (newdecl
)
1279 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl
)))))
1282 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1284 /* Discard the old built-in function. */
1288 if (DECL_INITIAL (newdecl
))
1290 if (DECL_INITIAL (olddecl
))
1292 /* If both decls have extern inline and are in the same TU,
1293 reject the new decl. */
1294 if (DECL_DECLARED_INLINE_P (olddecl
)
1295 && DECL_EXTERNAL (olddecl
)
1296 && DECL_DECLARED_INLINE_P (newdecl
)
1297 && DECL_EXTERNAL (newdecl
)
1298 && same_translation_unit_p (newdecl
, olddecl
))
1300 error ("%Jredefinition of %qD", newdecl
, newdecl
);
1301 locate_old_decl (olddecl
, error
);
1304 /* If both decls have not extern inline, reject the new decl. */
1305 if (!DECL_DECLARED_INLINE_P (olddecl
)
1306 && !DECL_EXTERNAL (olddecl
)
1307 && !DECL_DECLARED_INLINE_P (newdecl
)
1308 && !DECL_EXTERNAL (newdecl
))
1310 error ("%Jredefinition of %qD", newdecl
, newdecl
);
1311 locate_old_decl (olddecl
, error
);
1314 /* If the new decl is declared as extern inline, error if they are
1315 in the same TU, otherwise retain the old decl. */
1316 if (!DECL_DECLARED_INLINE_P (olddecl
)
1317 && !DECL_EXTERNAL (olddecl
)
1318 && DECL_DECLARED_INLINE_P (newdecl
)
1319 && DECL_EXTERNAL (newdecl
))
1321 if (same_translation_unit_p (newdecl
, olddecl
))
1323 error ("%Jredefinition of %qD", newdecl
, newdecl
);
1324 locate_old_decl (olddecl
, error
);
1332 /* If we have a prototype after an old-style function definition,
1333 the argument types must be checked specially. */
1334 else if (DECL_INITIAL (olddecl
)
1335 && !TYPE_ARG_TYPES (oldtype
) && TYPE_ARG_TYPES (newtype
)
1336 && TYPE_ACTUAL_ARG_TYPES (oldtype
)
1337 && !validate_proto_after_old_defn (newdecl
, newtype
, oldtype
))
1339 locate_old_decl (olddecl
, error
);
1342 /* A non-static declaration (even an "extern") followed by a
1343 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1344 The same is true for a static forward declaration at block
1345 scope followed by a non-static declaration/definition at file
1346 scope. Static followed by non-static at the same scope is
1347 not undefined behavior, and is the most convenient way to get
1348 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1349 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1350 we do diagnose it if -Wtraditional. */
1351 if (TREE_PUBLIC (olddecl
) && !TREE_PUBLIC (newdecl
))
1353 /* Two exceptions to the rule. If olddecl is an extern
1354 inline, or a predeclared function that isn't actually
1355 built in, newdecl silently overrides olddecl. The latter
1356 occur only in Objective C; see also above. (FIXME: Make
1357 Objective C use normal builtins.) */
1358 if (!DECL_IS_BUILTIN (olddecl
)
1359 && !(DECL_EXTERNAL (olddecl
)
1360 && DECL_DECLARED_INLINE_P (olddecl
)))
1362 error ("%Jstatic declaration of %qD follows "
1363 "non-static declaration", newdecl
, newdecl
);
1364 locate_old_decl (olddecl
, error
);
1368 else if (TREE_PUBLIC (newdecl
) && !TREE_PUBLIC (olddecl
))
1370 if (DECL_CONTEXT (olddecl
))
1372 error ("%Jnon-static declaration of %qD follows "
1373 "static declaration", newdecl
, newdecl
);
1374 locate_old_decl (olddecl
, error
);
1377 else if (warn_traditional
)
1379 warning (0, "%Jnon-static declaration of %qD follows "
1380 "static declaration", newdecl
, newdecl
);
1385 else if (TREE_CODE (newdecl
) == VAR_DECL
)
1387 /* Only variables can be thread-local, and all declarations must
1388 agree on this property. */
1389 if (DECL_THREAD_LOCAL (newdecl
) != DECL_THREAD_LOCAL (olddecl
))
1391 if (DECL_THREAD_LOCAL (newdecl
))
1392 error ("%Jthread-local declaration of %qD follows "
1393 "non-thread-local declaration", newdecl
, newdecl
);
1395 error ("%Jnon-thread-local declaration of %qD follows "
1396 "thread-local declaration", newdecl
, newdecl
);
1398 locate_old_decl (olddecl
, error
);
1402 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1403 if (DECL_INITIAL (newdecl
) && DECL_INITIAL (olddecl
))
1405 error ("%Jredefinition of %qD", newdecl
, newdecl
);
1406 locate_old_decl (olddecl
, error
);
1410 /* Objects declared at file scope: if the first declaration had
1411 external linkage (even if it was an external reference) the
1412 second must have external linkage as well, or the behavior is
1413 undefined. If the first declaration had internal linkage, then
1414 the second must too, or else be an external reference (in which
1415 case the composite declaration still has internal linkage).
1416 As for function declarations, we warn about the static-then-
1417 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1418 if (DECL_FILE_SCOPE_P (newdecl
)
1419 && TREE_PUBLIC (newdecl
) != TREE_PUBLIC (olddecl
))
1421 if (DECL_EXTERNAL (newdecl
))
1423 if (!DECL_FILE_SCOPE_P (olddecl
))
1425 error ("%Jextern declaration of %qD follows "
1426 "declaration with no linkage", newdecl
, newdecl
);
1427 locate_old_decl (olddecl
, error
);
1430 else if (warn_traditional
)
1432 warning (0, "%Jnon-static declaration of %qD follows "
1433 "static declaration", newdecl
, newdecl
);
1439 if (TREE_PUBLIC (newdecl
))
1440 error ("%Jnon-static declaration of %qD follows "
1441 "static declaration", newdecl
, newdecl
);
1443 error ("%Jstatic declaration of %qD follows "
1444 "non-static declaration", newdecl
, newdecl
);
1446 locate_old_decl (olddecl
, error
);
1450 /* Two objects with the same name declared at the same block
1451 scope must both be external references (6.7p3). */
1452 else if (!DECL_FILE_SCOPE_P (newdecl
))
1454 if (DECL_EXTERNAL (newdecl
))
1456 /* Extern with initializer at block scope, which will
1457 already have received an error. */
1459 else if (DECL_EXTERNAL (olddecl
))
1461 error ("%Jdeclaration of %qD with no linkage follows "
1462 "extern declaration", newdecl
, newdecl
);
1463 locate_old_decl (olddecl
, error
);
1467 error ("%Jredeclaration of %qD with no linkage",
1469 locate_old_decl (olddecl
, error
);
1477 /* All decls must agree on a visibility. */
1478 if (DECL_VISIBILITY_SPECIFIED (newdecl
) && DECL_VISIBILITY_SPECIFIED (olddecl
)
1479 && DECL_VISIBILITY (newdecl
) != DECL_VISIBILITY (olddecl
))
1481 warning (0, "%Jredeclaration of %qD with different visibility "
1482 "(old visibility preserved)", newdecl
, newdecl
);
1486 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1488 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1489 if (DECL_DECLARED_INLINE_P (newdecl
)
1490 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl
)))
1492 warning (0, "%Jinline declaration of %qD follows "
1493 "declaration with attribute noinline", newdecl
, newdecl
);
1496 else if (DECL_DECLARED_INLINE_P (olddecl
)
1497 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl
)))
1499 warning (0, "%Jdeclaration of %qD with attribute noinline follows "
1500 "inline declaration ", newdecl
, newdecl
);
1504 /* Inline declaration after use or definition.
1505 ??? Should we still warn about this now we have unit-at-a-time
1506 mode and can get it right?
1507 Definitely don't complain if the decls are in different translation
1509 if (DECL_DECLARED_INLINE_P (newdecl
) && !DECL_DECLARED_INLINE_P (olddecl
)
1510 && same_translation_unit_p (olddecl
, newdecl
))
1512 if (TREE_USED (olddecl
))
1514 warning (0, "%J%qD declared inline after being called",
1518 else if (DECL_INITIAL (olddecl
))
1520 warning (0, "%J%qD declared inline after its definition",
1526 else /* PARM_DECL, VAR_DECL */
1528 /* Redeclaration of a parameter is a constraint violation (this is
1529 not explicitly stated, but follows from C99 6.7p3 [no more than
1530 one declaration of the same identifier with no linkage in the
1531 same scope, except type tags] and 6.2.2p6 [parameters have no
1532 linkage]). We must check for a forward parameter declaration,
1533 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1534 an extension, the mandatory diagnostic for which is handled by
1535 mark_forward_parm_decls. */
1537 if (TREE_CODE (newdecl
) == PARM_DECL
1538 && (!TREE_ASM_WRITTEN (olddecl
) || TREE_ASM_WRITTEN (newdecl
)))
1540 error ("%Jredefinition of parameter %qD", newdecl
, newdecl
);
1541 locate_old_decl (olddecl
, error
);
1546 /* Optional warning for completely redundant decls. */
1547 if (!warned
&& !pedwarned
1548 && warn_redundant_decls
1549 /* Don't warn about a function declaration followed by a
1551 && !(TREE_CODE (newdecl
) == FUNCTION_DECL
1552 && DECL_INITIAL (newdecl
) && !DECL_INITIAL (olddecl
))
1553 /* Don't warn about redundant redeclarations of builtins. */
1554 && !(TREE_CODE (newdecl
) == FUNCTION_DECL
1555 && !DECL_BUILT_IN (newdecl
)
1556 && DECL_BUILT_IN (olddecl
)
1557 && !C_DECL_DECLARED_BUILTIN (olddecl
))
1558 /* Don't warn about an extern followed by a definition. */
1559 && !(DECL_EXTERNAL (olddecl
) && !DECL_EXTERNAL (newdecl
))
1560 /* Don't warn about forward parameter decls. */
1561 && !(TREE_CODE (newdecl
) == PARM_DECL
1562 && TREE_ASM_WRITTEN (olddecl
) && !TREE_ASM_WRITTEN (newdecl
)))
1564 warning (0, "%Jredundant redeclaration of %qD", newdecl
, newdecl
);
1568 /* Report location of previous decl/defn in a consistent manner. */
1569 if (warned
|| pedwarned
)
1570 locate_old_decl (olddecl
, pedwarned
? pedwarn
: warning0
);
1575 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1576 consistent with OLDDECL, but carries new information. Merge the
1577 new information into OLDDECL. This function issues no
1581 merge_decls (tree newdecl
, tree olddecl
, tree newtype
, tree oldtype
)
1583 int new_is_definition
= (TREE_CODE (newdecl
) == FUNCTION_DECL
1584 && DECL_INITIAL (newdecl
) != 0);
1585 int new_is_prototype
= (TREE_CODE (newdecl
) == FUNCTION_DECL
1586 && TYPE_ARG_TYPES (TREE_TYPE (newdecl
)) != 0);
1587 int old_is_prototype
= (TREE_CODE (olddecl
) == FUNCTION_DECL
1588 && TYPE_ARG_TYPES (TREE_TYPE (olddecl
)) != 0);
1590 /* For real parm decl following a forward decl, rechain the old decl
1591 in its new location and clear TREE_ASM_WRITTEN (it's not a
1592 forward decl anymore). */
1593 if (TREE_CODE (newdecl
) == PARM_DECL
1594 && TREE_ASM_WRITTEN (olddecl
) && !TREE_ASM_WRITTEN (newdecl
))
1596 struct c_binding
*b
, **here
;
1598 for (here
= ¤t_scope
->bindings
; *here
; here
= &(*here
)->prev
)
1599 if ((*here
)->decl
== olddecl
)
1606 b
->prev
= current_scope
->bindings
;
1607 current_scope
->bindings
= b
;
1609 TREE_ASM_WRITTEN (olddecl
) = 0;
1612 DECL_ATTRIBUTES (newdecl
)
1613 = targetm
.merge_decl_attributes (olddecl
, newdecl
);
1615 /* Merge the data types specified in the two decls. */
1617 = TREE_TYPE (olddecl
)
1618 = composite_type (newtype
, oldtype
);
1620 /* Lay the type out, unless already done. */
1621 if (!comptypes (oldtype
, TREE_TYPE (newdecl
)))
1623 if (TREE_TYPE (newdecl
) != error_mark_node
)
1624 layout_type (TREE_TYPE (newdecl
));
1625 if (TREE_CODE (newdecl
) != FUNCTION_DECL
1626 && TREE_CODE (newdecl
) != TYPE_DECL
1627 && TREE_CODE (newdecl
) != CONST_DECL
)
1628 layout_decl (newdecl
, 0);
1632 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1633 DECL_SIZE (newdecl
) = DECL_SIZE (olddecl
);
1634 DECL_SIZE_UNIT (newdecl
) = DECL_SIZE_UNIT (olddecl
);
1635 DECL_MODE (newdecl
) = DECL_MODE (olddecl
);
1636 if (TREE_CODE (olddecl
) != FUNCTION_DECL
)
1637 if (DECL_ALIGN (olddecl
) > DECL_ALIGN (newdecl
))
1639 DECL_ALIGN (newdecl
) = DECL_ALIGN (olddecl
);
1640 DECL_USER_ALIGN (newdecl
) |= DECL_ALIGN (olddecl
);
1644 /* Keep the old rtl since we can safely use it. */
1645 COPY_DECL_RTL (olddecl
, newdecl
);
1647 /* Merge the type qualifiers. */
1648 if (TREE_READONLY (newdecl
))
1649 TREE_READONLY (olddecl
) = 1;
1651 if (TREE_THIS_VOLATILE (newdecl
))
1653 TREE_THIS_VOLATILE (olddecl
) = 1;
1654 if (TREE_CODE (newdecl
) == VAR_DECL
)
1655 make_var_volatile (newdecl
);
1658 /* Merge deprecatedness. */
1659 if (TREE_DEPRECATED (newdecl
))
1660 TREE_DEPRECATED (olddecl
) = 1;
1662 /* Keep source location of definition rather than declaration and of
1663 prototype rather than non-prototype unless that prototype is
1665 if ((DECL_INITIAL (newdecl
) == 0 && DECL_INITIAL (olddecl
) != 0)
1666 || (old_is_prototype
&& !new_is_prototype
1667 && !C_DECL_BUILTIN_PROTOTYPE (olddecl
)))
1668 DECL_SOURCE_LOCATION (newdecl
) = DECL_SOURCE_LOCATION (olddecl
);
1670 /* Merge the unused-warning information. */
1671 if (DECL_IN_SYSTEM_HEADER (olddecl
))
1672 DECL_IN_SYSTEM_HEADER (newdecl
) = 1;
1673 else if (DECL_IN_SYSTEM_HEADER (newdecl
))
1674 DECL_IN_SYSTEM_HEADER (olddecl
) = 1;
1676 /* Merge the initialization information. */
1677 if (DECL_INITIAL (newdecl
) == 0)
1678 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
1680 /* Merge the section attribute.
1681 We want to issue an error if the sections conflict but that must be
1682 done later in decl_attributes since we are called before attributes
1684 if (DECL_SECTION_NAME (newdecl
) == NULL_TREE
)
1685 DECL_SECTION_NAME (newdecl
) = DECL_SECTION_NAME (olddecl
);
1687 /* Copy the assembler name.
1688 Currently, it can only be defined in the prototype. */
1689 COPY_DECL_ASSEMBLER_NAME (olddecl
, newdecl
);
1691 /* Use visibility of whichever declaration had it specified */
1692 if (DECL_VISIBILITY_SPECIFIED (olddecl
))
1694 DECL_VISIBILITY (newdecl
) = DECL_VISIBILITY (olddecl
);
1695 DECL_VISIBILITY_SPECIFIED (newdecl
) = 1;
1698 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1700 DECL_STATIC_CONSTRUCTOR(newdecl
) |= DECL_STATIC_CONSTRUCTOR(olddecl
);
1701 DECL_STATIC_DESTRUCTOR (newdecl
) |= DECL_STATIC_DESTRUCTOR (olddecl
);
1702 DECL_NO_LIMIT_STACK (newdecl
) |= DECL_NO_LIMIT_STACK (olddecl
);
1703 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl
)
1704 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl
);
1705 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
1706 TREE_READONLY (newdecl
) |= TREE_READONLY (olddecl
);
1707 DECL_IS_MALLOC (newdecl
) |= DECL_IS_MALLOC (olddecl
);
1708 DECL_IS_PURE (newdecl
) |= DECL_IS_PURE (olddecl
);
1709 DECL_IS_NOVOPS (newdecl
) |= DECL_IS_NOVOPS (olddecl
);
1712 /* Merge the storage class information. */
1713 merge_weak (newdecl
, olddecl
);
1715 /* For functions, static overrides non-static. */
1716 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1718 TREE_PUBLIC (newdecl
) &= TREE_PUBLIC (olddecl
);
1719 /* This is since we don't automatically
1720 copy the attributes of NEWDECL into OLDDECL. */
1721 TREE_PUBLIC (olddecl
) = TREE_PUBLIC (newdecl
);
1722 /* If this clears `static', clear it in the identifier too. */
1723 if (!TREE_PUBLIC (olddecl
))
1724 TREE_PUBLIC (DECL_NAME (olddecl
)) = 0;
1726 if (DECL_EXTERNAL (newdecl
))
1728 TREE_STATIC (newdecl
) = TREE_STATIC (olddecl
);
1729 DECL_EXTERNAL (newdecl
) = DECL_EXTERNAL (olddecl
);
1731 /* An extern decl does not override previous storage class. */
1732 TREE_PUBLIC (newdecl
) = TREE_PUBLIC (olddecl
);
1733 if (!DECL_EXTERNAL (newdecl
))
1735 DECL_CONTEXT (newdecl
) = DECL_CONTEXT (olddecl
);
1736 DECL_COMMON (newdecl
) = DECL_COMMON (olddecl
);
1741 TREE_STATIC (olddecl
) = TREE_STATIC (newdecl
);
1742 TREE_PUBLIC (olddecl
) = TREE_PUBLIC (newdecl
);
1745 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1747 /* If we're redefining a function previously defined as extern
1748 inline, make sure we emit debug info for the inline before we
1749 throw it away, in case it was inlined into a function that hasn't
1750 been written out yet. */
1751 if (new_is_definition
&& DECL_INITIAL (olddecl
))
1753 if (TREE_USED (olddecl
)
1754 /* In unit-at-a-time mode we never inline re-defined extern
1755 inline functions. */
1756 && !flag_unit_at_a_time
1757 && cgraph_function_possibly_inlined_p (olddecl
))
1758 (*debug_hooks
->outlining_inline_function
) (olddecl
);
1760 /* The new defn must not be inline. */
1761 DECL_INLINE (newdecl
) = 0;
1762 DECL_UNINLINABLE (newdecl
) = 1;
1766 /* If either decl says `inline', this fn is inline,
1767 unless its definition was passed already. */
1768 if (DECL_DECLARED_INLINE_P (newdecl
)
1769 || DECL_DECLARED_INLINE_P (olddecl
))
1770 DECL_DECLARED_INLINE_P (newdecl
) = 1;
1772 DECL_UNINLINABLE (newdecl
) = DECL_UNINLINABLE (olddecl
)
1773 = (DECL_UNINLINABLE (newdecl
) || DECL_UNINLINABLE (olddecl
));
1776 if (DECL_BUILT_IN (olddecl
))
1778 /* If redeclaring a builtin function, it stays built in.
1779 But it gets tagged as having been declared. */
1780 DECL_BUILT_IN_CLASS (newdecl
) = DECL_BUILT_IN_CLASS (olddecl
);
1781 DECL_FUNCTION_CODE (newdecl
) = DECL_FUNCTION_CODE (olddecl
);
1782 C_DECL_DECLARED_BUILTIN (newdecl
) = 1;
1783 if (new_is_prototype
)
1784 C_DECL_BUILTIN_PROTOTYPE (newdecl
) = 0;
1786 C_DECL_BUILTIN_PROTOTYPE (newdecl
)
1787 = C_DECL_BUILTIN_PROTOTYPE (olddecl
);
1790 /* Also preserve various other info from the definition. */
1791 if (!new_is_definition
)
1793 DECL_RESULT (newdecl
) = DECL_RESULT (olddecl
);
1794 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
1795 DECL_STRUCT_FUNCTION (newdecl
) = DECL_STRUCT_FUNCTION (olddecl
);
1796 DECL_SAVED_TREE (newdecl
) = DECL_SAVED_TREE (olddecl
);
1797 DECL_ARGUMENTS (newdecl
) = DECL_ARGUMENTS (olddecl
);
1799 /* Set DECL_INLINE on the declaration if we've got a body
1800 from which to instantiate. */
1801 if (DECL_INLINE (olddecl
) && !DECL_UNINLINABLE (newdecl
))
1803 DECL_INLINE (newdecl
) = 1;
1804 DECL_ABSTRACT_ORIGIN (newdecl
)
1805 = DECL_ABSTRACT_ORIGIN (olddecl
);
1810 /* If a previous declaration said inline, mark the
1811 definition as inlinable. */
1812 if (DECL_DECLARED_INLINE_P (newdecl
)
1813 && !DECL_UNINLINABLE (newdecl
))
1814 DECL_INLINE (newdecl
) = 1;
1818 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1819 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1821 unsigned olddecl_uid
= DECL_UID (olddecl
);
1822 tree olddecl_context
= DECL_CONTEXT (olddecl
);
1824 memcpy ((char *) olddecl
+ sizeof (struct tree_common
),
1825 (char *) newdecl
+ sizeof (struct tree_common
),
1826 sizeof (struct tree_decl
) - sizeof (struct tree_common
));
1827 DECL_UID (olddecl
) = olddecl_uid
;
1828 DECL_CONTEXT (olddecl
) = olddecl_context
;
1831 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1832 so that encode_section_info has a chance to look at the new decl
1833 flags and attributes. */
1834 if (DECL_RTL_SET_P (olddecl
)
1835 && (TREE_CODE (olddecl
) == FUNCTION_DECL
1836 || (TREE_CODE (olddecl
) == VAR_DECL
1837 && TREE_STATIC (olddecl
))))
1838 make_decl_rtl (olddecl
);
1841 /* Handle when a new declaration NEWDECL has the same name as an old
1842 one OLDDECL in the same binding contour. Prints an error message
1845 If safely possible, alter OLDDECL to look like NEWDECL, and return
1846 true. Otherwise, return false. */
1849 duplicate_decls (tree newdecl
, tree olddecl
)
1851 tree newtype
= NULL
, oldtype
= NULL
;
1853 if (!diagnose_mismatched_decls (newdecl
, olddecl
, &newtype
, &oldtype
))
1856 merge_decls (newdecl
, olddecl
, newtype
, oldtype
);
1861 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1863 warn_if_shadowing (tree new_decl
)
1865 struct c_binding
*b
;
1867 /* Shadow warnings wanted? */
1869 /* No shadow warnings for internally generated vars. */
1870 || DECL_IS_BUILTIN (new_decl
)
1871 /* No shadow warnings for vars made for inlining. */
1872 || DECL_FROM_INLINE (new_decl
)
1873 /* Don't warn about the parm names in function declarator
1874 within a function declarator. It would be nice to avoid
1875 warning in any function declarator in a declaration, as
1876 opposed to a definition, but there is no way to tell
1877 it's not a definition at this point. */
1878 || (TREE_CODE (new_decl
) == PARM_DECL
&& current_scope
->outer
->parm_flag
))
1881 /* Is anything being shadowed? Invisible decls do not count. */
1882 for (b
= I_SYMBOL_BINDING (DECL_NAME (new_decl
)); b
; b
= b
->shadowed
)
1883 if (b
->decl
&& b
->decl
!= new_decl
&& !b
->invisible
)
1885 tree old_decl
= b
->decl
;
1887 if (old_decl
== error_mark_node
)
1889 warning (0, "%Jdeclaration of %qD shadows previous non-variable",
1890 new_decl
, new_decl
);
1893 else if (TREE_CODE (old_decl
) == PARM_DECL
)
1894 warning (0, "%Jdeclaration of %qD shadows a parameter",
1895 new_decl
, new_decl
);
1896 else if (DECL_FILE_SCOPE_P (old_decl
))
1897 warning (0, "%Jdeclaration of %qD shadows a global declaration",
1898 new_decl
, new_decl
);
1899 else if (TREE_CODE (old_decl
) == FUNCTION_DECL
1900 && DECL_BUILT_IN (old_decl
))
1902 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1903 new_decl
, new_decl
);
1907 warning (0, "%Jdeclaration of %qD shadows a previous local",
1908 new_decl
, new_decl
);
1910 warning (0, "%Jshadowed declaration is here", old_decl
);
1917 /* Subroutine of pushdecl.
1919 X is a TYPE_DECL for a typedef statement. Create a brand new
1920 ..._TYPE node (which will be just a variant of the existing
1921 ..._TYPE node with identical properties) and then install X
1922 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1924 The whole point here is to end up with a situation where each
1925 and every ..._TYPE node the compiler creates will be uniquely
1926 associated with AT MOST one node representing a typedef name.
1927 This way, even though the compiler substitutes corresponding
1928 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1929 early on, later parts of the compiler can always do the reverse
1930 translation and get back the corresponding typedef name. For
1933 typedef struct S MY_TYPE;
1936 Later parts of the compiler might only know that `object' was of
1937 type `struct S' if it were not for code just below. With this
1938 code however, later parts of the compiler see something like:
1940 struct S' == struct S
1941 typedef struct S' MY_TYPE;
1944 And they can then deduce (from the node for type struct S') that
1945 the original object declaration was:
1949 Being able to do this is important for proper support of protoize,
1950 and also for generating precise symbolic debugging information
1951 which takes full account of the programmer's (typedef) vocabulary.
1953 Obviously, we don't want to generate a duplicate ..._TYPE node if
1954 the TYPE_DECL node that we are now processing really represents a
1955 standard built-in type.
1957 Since all standard types are effectively declared at line zero
1958 in the source file, we can easily check to see if we are working
1959 on a standard type by checking the current value of lineno. */
1962 clone_underlying_type (tree x
)
1964 if (DECL_IS_BUILTIN (x
))
1966 if (TYPE_NAME (TREE_TYPE (x
)) == 0)
1967 TYPE_NAME (TREE_TYPE (x
)) = x
;
1969 else if (TREE_TYPE (x
) != error_mark_node
1970 && DECL_ORIGINAL_TYPE (x
) == NULL_TREE
)
1972 tree tt
= TREE_TYPE (x
);
1973 DECL_ORIGINAL_TYPE (x
) = tt
;
1974 tt
= build_variant_type_copy (tt
);
1976 TREE_USED (tt
) = TREE_USED (x
);
1981 /* Record a decl-node X as belonging to the current lexical scope.
1982 Check for errors (such as an incompatible declaration for the same
1983 name already seen in the same scope).
1985 Returns either X or an old decl for the same name.
1986 If an old decl is returned, it may have been smashed
1987 to agree with what X says. */
1992 tree name
= DECL_NAME (x
);
1993 struct c_scope
*scope
= current_scope
;
1994 struct c_binding
*b
;
1995 bool nested
= false;
1997 /* Functions need the lang_decl data. */
1998 if (TREE_CODE (x
) == FUNCTION_DECL
&& !DECL_LANG_SPECIFIC (x
))
1999 DECL_LANG_SPECIFIC (x
) = GGC_CNEW (struct lang_decl
);
2001 /* Must set DECL_CONTEXT for everything not at file scope or
2002 DECL_FILE_SCOPE_P won't work. Local externs don't count
2003 unless they have initializers (which generate code). */
2004 if (current_function_decl
2005 && ((TREE_CODE (x
) != FUNCTION_DECL
&& TREE_CODE (x
) != VAR_DECL
)
2006 || DECL_INITIAL (x
) || !DECL_EXTERNAL (x
)))
2007 DECL_CONTEXT (x
) = current_function_decl
;
2009 /* If this is of variably modified type, prevent jumping into its
2011 if ((TREE_CODE (x
) == VAR_DECL
|| TREE_CODE (x
) == TYPE_DECL
)
2012 && variably_modified_type_p (TREE_TYPE (x
), NULL_TREE
))
2013 c_begin_vm_scope (scope
->depth
);
2015 /* Anonymous decls are just inserted in the scope. */
2018 bind (name
, x
, scope
, /*invisible=*/false, /*nested=*/false);
2022 /* First, see if there is another declaration with the same name in
2023 the current scope. If there is, duplicate_decls may do all the
2024 work for us. If duplicate_decls returns false, that indicates
2025 two incompatible decls in the same scope; we are to silently
2026 replace the old one (duplicate_decls has issued all appropriate
2027 diagnostics). In particular, we should not consider possible
2028 duplicates in the external scope, or shadowing. */
2029 b
= I_SYMBOL_BINDING (name
);
2030 if (b
&& B_IN_SCOPE (b
, scope
))
2032 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
2033 && COMPLETE_TYPE_P (TREE_TYPE (x
)))
2034 b
->inner_comp
= false;
2035 if (duplicate_decls (x
, b
->decl
))
2038 goto skip_external_and_shadow_checks
;
2041 /* All declarations with external linkage, and all external
2042 references, go in the external scope, no matter what scope is
2043 current. However, the binding in that scope is ignored for
2044 purposes of normal name lookup. A separate binding structure is
2045 created in the requested scope; this governs the normal
2046 visibility of the symbol.
2048 The binding in the externals scope is used exclusively for
2049 detecting duplicate declarations of the same object, no matter
2050 what scope they are in; this is what we do here. (C99 6.2.7p2:
2051 All declarations that refer to the same object or function shall
2052 have compatible type; otherwise, the behavior is undefined.) */
2053 if (DECL_EXTERNAL (x
) || scope
== file_scope
)
2055 tree type
= TREE_TYPE (x
);
2058 bool type_saved
= false;
2059 if (b
&& !B_IN_EXTERNAL_SCOPE (b
)
2060 && (TREE_CODE (b
->decl
) == FUNCTION_DECL
2061 || TREE_CODE (b
->decl
) == VAR_DECL
)
2062 && DECL_FILE_SCOPE_P (b
->decl
))
2065 vistype
= TREE_TYPE (visdecl
);
2067 if (warn_nested_externs
2068 && scope
!= file_scope
2069 && !DECL_IN_SYSTEM_HEADER (x
))
2070 warning (0, "nested extern declaration of %qD", x
);
2072 while (b
&& !B_IN_EXTERNAL_SCOPE (b
))
2074 /* If this decl might be modified, save its type. This is
2075 done here rather than when the decl is first bound
2076 because the type may change after first binding, through
2077 being completed or through attributes being added. If we
2078 encounter multiple such decls, only the first should have
2079 its type saved; the others will already have had their
2080 proper types saved and the types will not have changed as
2081 their scopes will not have been re-entered. */
2082 if (DECL_P (b
->decl
) && DECL_FILE_SCOPE_P (b
->decl
) && !type_saved
)
2084 b
->type
= TREE_TYPE (b
->decl
);
2087 if (B_IN_FILE_SCOPE (b
)
2088 && TREE_CODE (b
->decl
) == VAR_DECL
2089 && TREE_STATIC (b
->decl
)
2090 && TREE_CODE (TREE_TYPE (b
->decl
)) == ARRAY_TYPE
2091 && !TYPE_DOMAIN (TREE_TYPE (b
->decl
))
2092 && TREE_CODE (type
) == ARRAY_TYPE
2093 && TYPE_DOMAIN (type
)
2094 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
2095 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))
2097 /* Array type completed in inner scope, which should be
2098 diagnosed if the completion does not have size 1 and
2099 it does not get completed in the file scope. */
2100 b
->inner_comp
= true;
2105 /* If a matching external declaration has been found, set its
2106 type to the composite of all the types of that declaration.
2107 After the consistency checks, it will be reset to the
2108 composite of the visible types only. */
2109 if (b
&& (TREE_PUBLIC (x
) || same_translation_unit_p (x
, b
->decl
))
2111 TREE_TYPE (b
->decl
) = b
->type
;
2113 /* The point of the same_translation_unit_p check here is,
2114 we want to detect a duplicate decl for a construct like
2115 foo() { extern bar(); } ... static bar(); but not if
2116 they are in different translation units. In any case,
2117 the static does not go in the externals scope. */
2119 && (TREE_PUBLIC (x
) || same_translation_unit_p (x
, b
->decl
))
2120 && duplicate_decls (x
, b
->decl
))
2123 thistype
= (vistype
? composite_type (vistype
, type
) : type
);
2124 b
->type
= TREE_TYPE (b
->decl
);
2125 if (TREE_CODE (b
->decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (b
->decl
))
2127 = build_type_attribute_variant (thistype
,
2128 TYPE_ATTRIBUTES (b
->type
));
2129 TREE_TYPE (b
->decl
) = thistype
;
2130 bind (name
, b
->decl
, scope
, /*invisible=*/false, /*nested=*/true);
2133 else if (TREE_PUBLIC (x
))
2135 if (visdecl
&& !b
&& duplicate_decls (x
, visdecl
))
2137 /* An external declaration at block scope referring to a
2138 visible entity with internal linkage. The composite
2139 type will already be correct for this scope, so we
2140 just need to fall through to make the declaration in
2147 bind (name
, x
, external_scope
, /*invisible=*/true,
2154 warn_if_shadowing (x
);
2156 skip_external_and_shadow_checks
:
2157 if (TREE_CODE (x
) == TYPE_DECL
)
2158 clone_underlying_type (x
);
2160 bind (name
, x
, scope
, /*invisible=*/false, nested
);
2162 /* If x's type is incomplete because it's based on a
2163 structure or union which has not yet been fully declared,
2164 attach it to that structure or union type, so we can go
2165 back and complete the variable declaration later, if the
2166 structure or union gets fully declared.
2168 If the input is erroneous, we can have error_mark in the type
2169 slot (e.g. "f(void a, ...)") - that doesn't count as an
2171 if (TREE_TYPE (x
) != error_mark_node
2172 && !COMPLETE_TYPE_P (TREE_TYPE (x
)))
2174 tree element
= TREE_TYPE (x
);
2176 while (TREE_CODE (element
) == ARRAY_TYPE
)
2177 element
= TREE_TYPE (element
);
2178 element
= TYPE_MAIN_VARIANT (element
);
2180 if ((TREE_CODE (element
) == RECORD_TYPE
2181 || TREE_CODE (element
) == UNION_TYPE
)
2182 && (TREE_CODE (x
) != TYPE_DECL
2183 || TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
)
2184 && !COMPLETE_TYPE_P (element
))
2185 C_TYPE_INCOMPLETE_VARS (element
)
2186 = tree_cons (NULL_TREE
, x
, C_TYPE_INCOMPLETE_VARS (element
));
2191 /* Record X as belonging to file scope.
2192 This is used only internally by the Objective-C front end,
2193 and is limited to its needs. duplicate_decls is not called;
2194 if there is any preexisting decl for this identifier, it is an ICE. */
2197 pushdecl_top_level (tree x
)
2200 bool nested
= false;
2201 gcc_assert (TREE_CODE (x
) == VAR_DECL
|| TREE_CODE (x
) == CONST_DECL
);
2203 name
= DECL_NAME (x
);
2205 gcc_assert (TREE_CODE (x
) == CONST_DECL
|| !I_SYMBOL_BINDING (name
));
2207 if (TREE_PUBLIC (x
))
2209 bind (name
, x
, external_scope
, /*invisible=*/true, /*nested=*/false);
2213 bind (name
, x
, file_scope
, /*invisible=*/false, nested
);
2219 implicit_decl_warning (tree id
, tree olddecl
)
2221 void (*diag
) (const char *, ...);
2222 switch (mesg_implicit_function_declaration
)
2225 case 1: diag
= warning0
; break;
2226 case 2: diag
= error
; break;
2227 default: gcc_unreachable ();
2230 diag (N_("implicit declaration of function %qE"), id
);
2232 locate_old_decl (olddecl
, diag
);
2235 /* Generate an implicit declaration for identifier FUNCTIONID as a
2236 function of type int (). */
2239 implicitly_declare (tree functionid
)
2241 struct c_binding
*b
;
2245 for (b
= I_SYMBOL_BINDING (functionid
); b
; b
= b
->shadowed
)
2247 if (B_IN_SCOPE (b
, external_scope
))
2256 if (decl
== error_mark_node
)
2259 /* FIXME: Objective-C has weird not-really-builtin functions
2260 which are supposed to be visible automatically. They wind up
2261 in the external scope because they're pushed before the file
2262 scope gets created. Catch this here and rebind them into the
2264 if (!DECL_BUILT_IN (decl
) && DECL_IS_BUILTIN (decl
))
2266 bind (functionid
, decl
, file_scope
,
2267 /*invisible=*/false, /*nested=*/true);
2272 tree newtype
= default_function_type
;
2274 TREE_TYPE (decl
) = b
->type
;
2275 /* Implicit declaration of a function already declared
2276 (somehow) in a different scope, or as a built-in.
2277 If this is the first time this has happened, warn;
2278 then recycle the old declaration but with the new type. */
2279 if (!C_DECL_IMPLICIT (decl
))
2281 implicit_decl_warning (functionid
, decl
);
2282 C_DECL_IMPLICIT (decl
) = 1;
2284 if (DECL_BUILT_IN (decl
))
2286 newtype
= build_type_attribute_variant (newtype
,
2288 (TREE_TYPE (decl
)));
2289 if (!comptypes (newtype
, TREE_TYPE (decl
)))
2291 warning (0, "incompatible implicit declaration of built-in"
2292 " function %qD", decl
);
2293 newtype
= TREE_TYPE (decl
);
2298 if (!comptypes (newtype
, TREE_TYPE (decl
)))
2300 error ("incompatible implicit declaration of function %qD",
2302 locate_old_decl (decl
, error
);
2305 b
->type
= TREE_TYPE (decl
);
2306 TREE_TYPE (decl
) = newtype
;
2307 bind (functionid
, decl
, current_scope
,
2308 /*invisible=*/false, /*nested=*/true);
2313 /* Not seen before. */
2314 decl
= build_decl (FUNCTION_DECL
, functionid
, default_function_type
);
2315 DECL_EXTERNAL (decl
) = 1;
2316 TREE_PUBLIC (decl
) = 1;
2317 C_DECL_IMPLICIT (decl
) = 1;
2318 implicit_decl_warning (functionid
, 0);
2319 asmspec_tree
= maybe_apply_renaming_pragma (decl
, /*asmname=*/NULL
);
2321 set_user_assembler_name (decl
, TREE_STRING_POINTER (asmspec_tree
));
2323 /* C89 says implicit declarations are in the innermost block.
2324 So we record the decl in the standard fashion. */
2325 decl
= pushdecl (decl
);
2327 /* No need to call objc_check_decl here - it's a function type. */
2328 rest_of_decl_compilation (decl
, 0, 0);
2330 /* Write a record describing this implicit function declaration
2331 to the prototypes file (if requested). */
2332 gen_aux_info_record (decl
, 0, 1, 0);
2334 /* Possibly apply some default attributes to this implicit declaration. */
2335 decl_attributes (&decl
, NULL_TREE
, 0);
2340 /* Issue an error message for a reference to an undeclared variable
2341 ID, including a reference to a builtin outside of function-call
2342 context. Establish a binding of the identifier to error_mark_node
2343 in an appropriate scope, which will suppress further errors for the
2344 same identifier. The error message should be given location LOC. */
2346 undeclared_variable (tree id
, location_t loc
)
2348 static bool already
= false;
2349 struct c_scope
*scope
;
2351 if (current_function_decl
== 0)
2353 error ("%H%qE undeclared here (not in a function)", &loc
, id
);
2354 scope
= current_scope
;
2358 error ("%H%qE undeclared (first use in this function)", &loc
, id
);
2362 error ("%H(Each undeclared identifier is reported only once", &loc
);
2363 error ("%Hfor each function it appears in.)", &loc
);
2367 /* If we are parsing old-style parameter decls, current_function_decl
2368 will be nonnull but current_function_scope will be null. */
2369 scope
= current_function_scope
? current_function_scope
: current_scope
;
2371 bind (id
, error_mark_node
, scope
, /*invisible=*/false, /*nested=*/false);
2374 /* Subroutine of lookup_label, declare_label, define_label: construct a
2375 LABEL_DECL with all the proper frills. */
2378 make_label (tree name
, location_t location
)
2380 tree label
= build_decl (LABEL_DECL
, name
, void_type_node
);
2382 DECL_CONTEXT (label
) = current_function_decl
;
2383 DECL_MODE (label
) = VOIDmode
;
2384 DECL_SOURCE_LOCATION (label
) = location
;
2389 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2390 Create one if none exists so far for the current function.
2391 This is called when a label is used in a goto expression or
2392 has its address taken. */
2395 lookup_label (tree name
)
2399 if (current_function_decl
== 0)
2401 error ("label %qE referenced outside of any function", name
);
2405 /* Use a label already defined or ref'd with this name, but not if
2406 it is inherited from a containing function and wasn't declared
2408 label
= I_LABEL_DECL (name
);
2409 if (label
&& (DECL_CONTEXT (label
) == current_function_decl
2410 || C_DECLARED_LABEL_FLAG (label
)))
2412 /* If the label has only been declared, update its apparent
2413 location to point here, for better diagnostics if it
2414 turns out not to have been defined. */
2415 if (!TREE_USED (label
))
2416 DECL_SOURCE_LOCATION (label
) = input_location
;
2420 /* No label binding for that identifier; make one. */
2421 label
= make_label (name
, input_location
);
2423 /* Ordinary labels go in the current function scope. */
2424 bind (name
, label
, current_function_scope
,
2425 /*invisible=*/false, /*nested=*/false);
2429 /* Make a label named NAME in the current function, shadowing silently
2430 any that may be inherited from containing functions or containing
2431 scopes. This is called for __label__ declarations. */
2434 declare_label (tree name
)
2436 struct c_binding
*b
= I_LABEL_BINDING (name
);
2439 /* Check to make sure that the label hasn't already been declared
2441 if (b
&& B_IN_CURRENT_SCOPE (b
))
2443 error ("duplicate label declaration %qE", name
);
2444 locate_old_decl (b
->decl
, error
);
2446 /* Just use the previous declaration. */
2450 label
= make_label (name
, input_location
);
2451 C_DECLARED_LABEL_FLAG (label
) = 1;
2453 /* Declared labels go in the current scope. */
2454 bind (name
, label
, current_scope
,
2455 /*invisible=*/false, /*nested=*/false);
2459 /* Define a label, specifying the location in the source file.
2460 Return the LABEL_DECL node for the label, if the definition is valid.
2461 Otherwise return 0. */
2464 define_label (location_t location
, tree name
)
2466 /* Find any preexisting label with this name. It is an error
2467 if that label has already been defined in this function, or
2468 if there is a containing function with a declared label with
2470 tree label
= I_LABEL_DECL (name
);
2471 struct c_label_list
*nlist_se
, *nlist_vm
;
2474 && ((DECL_CONTEXT (label
) == current_function_decl
2475 && DECL_INITIAL (label
) != 0)
2476 || (DECL_CONTEXT (label
) != current_function_decl
2477 && C_DECLARED_LABEL_FLAG (label
))))
2479 error ("%Hduplicate label %qD", &location
, label
);
2480 locate_old_decl (label
, error
);
2483 else if (label
&& DECL_CONTEXT (label
) == current_function_decl
)
2485 /* The label has been used or declared already in this function,
2486 but not defined. Update its location to point to this
2488 if (C_DECL_UNDEFINABLE_STMT_EXPR (label
))
2489 error ("%Jjump into statement expression", label
);
2490 if (C_DECL_UNDEFINABLE_VM (label
))
2491 error ("%Jjump into scope of identifier with variably modified type",
2493 DECL_SOURCE_LOCATION (label
) = location
;
2497 /* No label binding for that identifier; make one. */
2498 label
= make_label (name
, location
);
2500 /* Ordinary labels go in the current function scope. */
2501 bind (name
, label
, current_function_scope
,
2502 /*invisible=*/false, /*nested=*/false);
2505 if (warn_traditional
&& !in_system_header
&& lookup_name (name
))
2506 warning (0, "%Htraditional C lacks a separate namespace for labels, "
2507 "identifier %qE conflicts", &location
, name
);
2509 nlist_se
= XOBNEW (&parser_obstack
, struct c_label_list
);
2510 nlist_se
->next
= label_context_stack_se
->labels_def
;
2511 nlist_se
->label
= label
;
2512 label_context_stack_se
->labels_def
= nlist_se
;
2514 nlist_vm
= XOBNEW (&parser_obstack
, struct c_label_list
);
2515 nlist_vm
->next
= label_context_stack_vm
->labels_def
;
2516 nlist_vm
->label
= label
;
2517 label_context_stack_vm
->labels_def
= nlist_vm
;
2519 /* Mark label as having been defined. */
2520 DECL_INITIAL (label
) = error_mark_node
;
2524 /* Given NAME, an IDENTIFIER_NODE,
2525 return the structure (or union or enum) definition for that name.
2526 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2527 CODE says which kind of type the caller wants;
2528 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2529 If the wrong kind of type is found, an error is reported. */
2532 lookup_tag (enum tree_code code
, tree name
, int thislevel_only
)
2534 struct c_binding
*b
= I_TAG_BINDING (name
);
2540 /* We only care about whether it's in this level if
2541 thislevel_only was set or it might be a type clash. */
2542 if (thislevel_only
|| TREE_CODE (b
->decl
) != code
)
2544 /* For our purposes, a tag in the external scope is the same as
2545 a tag in the file scope. (Primarily relevant to Objective-C
2546 and its builtin structure tags, which get pushed before the
2547 file scope is created.) */
2548 if (B_IN_CURRENT_SCOPE (b
)
2549 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
2553 if (thislevel_only
&& !thislevel
)
2556 if (TREE_CODE (b
->decl
) != code
)
2558 /* Definition isn't the kind we were looking for. */
2559 pending_invalid_xref
= name
;
2560 pending_invalid_xref_location
= input_location
;
2562 /* If in the same binding level as a declaration as a tag
2563 of a different type, this must not be allowed to
2564 shadow that tag, so give the error immediately.
2565 (For example, "struct foo; union foo;" is invalid.) */
2567 pending_xref_error ();
2572 /* Print an error message now
2573 for a recent invalid struct, union or enum cross reference.
2574 We don't print them immediately because they are not invalid
2575 when used in the `struct foo;' construct for shadowing. */
2578 pending_xref_error (void)
2580 if (pending_invalid_xref
!= 0)
2581 error ("%H%qE defined as wrong kind of tag",
2582 &pending_invalid_xref_location
, pending_invalid_xref
);
2583 pending_invalid_xref
= 0;
2587 /* Look up NAME in the current scope and its superiors
2588 in the namespace of variables, functions and typedefs.
2589 Return a ..._DECL node of some kind representing its definition,
2590 or return 0 if it is undefined. */
2593 lookup_name (tree name
)
2595 struct c_binding
*b
= I_SYMBOL_BINDING (name
);
2596 if (b
&& !b
->invisible
)
2601 /* Similar to `lookup_name' but look only at the indicated scope. */
2604 lookup_name_in_scope (tree name
, struct c_scope
*scope
)
2606 struct c_binding
*b
;
2608 for (b
= I_SYMBOL_BINDING (name
); b
; b
= b
->shadowed
)
2609 if (B_IN_SCOPE (b
, scope
))
2614 /* Create the predefined scalar types of C,
2615 and some nodes representing standard constants (0, 1, (void *) 0).
2616 Initialize the global scope.
2617 Make definitions for built-in primitive functions. */
2620 c_init_decl_processing (void)
2622 location_t save_loc
= input_location
;
2624 /* Initialize reserved words for parser. */
2627 current_function_decl
= 0;
2629 gcc_obstack_init (&parser_obstack
);
2631 /* Make the externals scope. */
2633 external_scope
= current_scope
;
2635 /* Declarations from c_common_nodes_and_builtins must not be associated
2636 with this input file, lest we get differences between using and not
2637 using preprocessed headers. */
2638 #ifdef USE_MAPPED_LOCATION
2639 input_location
= BUILTINS_LOCATION
;
2641 input_location
.file
= "<built-in>";
2642 input_location
.line
= 0;
2645 build_common_tree_nodes (flag_signed_char
, false);
2647 c_common_nodes_and_builtins ();
2649 /* In C, comparisons and TRUTH_* expressions have type int. */
2650 truthvalue_type_node
= integer_type_node
;
2651 truthvalue_true_node
= integer_one_node
;
2652 truthvalue_false_node
= integer_zero_node
;
2654 /* Even in C99, which has a real boolean type. */
2655 pushdecl (build_decl (TYPE_DECL
, get_identifier ("_Bool"),
2656 boolean_type_node
));
2658 input_location
= save_loc
;
2660 pedantic_lvalues
= true;
2662 make_fname_decl
= c_make_fname_decl
;
2663 start_fname_decls ();
2666 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2667 decl, NAME is the initialization string and TYPE_DEP indicates whether
2668 NAME depended on the type of the function. As we don't yet implement
2669 delayed emission of static data, we mark the decl as emitted
2670 so it is not placed in the output. Anything using it must therefore pull
2671 out the STRING_CST initializer directly. FIXME. */
2674 c_make_fname_decl (tree id
, int type_dep
)
2676 const char *name
= fname_as_string (type_dep
);
2677 tree decl
, type
, init
;
2678 size_t length
= strlen (name
);
2680 type
= build_array_type (char_type_node
,
2681 build_index_type (size_int (length
)));
2682 type
= c_build_qualified_type (type
, TYPE_QUAL_CONST
);
2684 decl
= build_decl (VAR_DECL
, id
, type
);
2686 TREE_STATIC (decl
) = 1;
2687 TREE_READONLY (decl
) = 1;
2688 DECL_ARTIFICIAL (decl
) = 1;
2690 init
= build_string (length
+ 1, name
);
2691 free ((char *) name
);
2692 TREE_TYPE (init
) = type
;
2693 DECL_INITIAL (decl
) = init
;
2695 TREE_USED (decl
) = 1;
2697 if (current_function_decl
)
2699 DECL_CONTEXT (decl
) = current_function_decl
;
2700 bind (id
, decl
, current_function_scope
,
2701 /*invisible=*/false, /*nested=*/false);
2704 finish_decl (decl
, init
, NULL_TREE
);
2709 /* Return a definition for a builtin function named NAME and whose data type
2710 is TYPE. TYPE should be a function type with argument types.
2711 FUNCTION_CODE tells later passes how to compile calls to this function.
2712 See tree.h for its possible values.
2714 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2715 the name to be called if we can't opencode the function. If
2716 ATTRS is nonzero, use that for the function's attribute list. */
2719 builtin_function (const char *name
, tree type
, int function_code
,
2720 enum built_in_class cl
, const char *library_name
,
2723 tree id
= get_identifier (name
);
2724 tree decl
= build_decl (FUNCTION_DECL
, id
, type
);
2725 TREE_PUBLIC (decl
) = 1;
2726 DECL_EXTERNAL (decl
) = 1;
2727 DECL_LANG_SPECIFIC (decl
) = GGC_CNEW (struct lang_decl
);
2728 DECL_BUILT_IN_CLASS (decl
) = cl
;
2729 DECL_FUNCTION_CODE (decl
) = function_code
;
2730 C_DECL_BUILTIN_PROTOTYPE (decl
) = (TYPE_ARG_TYPES (type
) != 0);
2732 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier (library_name
));
2734 /* Should never be called on a symbol with a preexisting meaning. */
2735 gcc_assert (!I_SYMBOL_BINDING (id
));
2737 bind (id
, decl
, external_scope
, /*invisible=*/true, /*nested=*/false);
2739 /* Builtins in the implementation namespace are made visible without
2740 needing to be explicitly declared. See push_file_scope. */
2741 if (name
[0] == '_' && (name
[1] == '_' || ISUPPER (name
[1])))
2743 TREE_CHAIN (decl
) = visible_builtins
;
2744 visible_builtins
= decl
;
2747 /* Possibly apply some default attributes to this built-in function. */
2749 decl_attributes (&decl
, attrs
, ATTR_FLAG_BUILT_IN
);
2751 decl_attributes (&decl
, NULL_TREE
, 0);
2756 /* Called when a declaration is seen that contains no names to declare.
2757 If its type is a reference to a structure, union or enum inherited
2758 from a containing scope, shadow that tag name for the current scope
2759 with a forward reference.
2760 If its type defines a new named structure or union
2761 or defines an enum, it is valid but we need not do anything here.
2762 Otherwise, it is an error. */
2765 shadow_tag (const struct c_declspecs
*declspecs
)
2767 shadow_tag_warned (declspecs
, 0);
2770 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2773 shadow_tag_warned (const struct c_declspecs
*declspecs
, int warned
)
2775 bool found_tag
= false;
2777 if (declspecs
->type
&& !declspecs
->default_int_p
&& !declspecs
->typedef_p
)
2779 tree value
= declspecs
->type
;
2780 enum tree_code code
= TREE_CODE (value
);
2782 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
|| code
== ENUMERAL_TYPE
)
2783 /* Used to test also that TYPE_SIZE (value) != 0.
2784 That caused warning for `struct foo;' at top level in the file. */
2786 tree name
= TYPE_NAME (value
);
2793 if (warned
!= 1 && code
!= ENUMERAL_TYPE
)
2794 /* Empty unnamed enum OK */
2796 pedwarn ("unnamed struct/union that defines no instances");
2800 else if (!declspecs
->tag_defined_p
2801 && declspecs
->storage_class
!= csc_none
)
2804 pedwarn ("empty declaration with storage class specifier "
2805 "does not redeclare tag");
2807 pending_xref_error ();
2809 else if (!declspecs
->tag_defined_p
2810 && (declspecs
->const_p
2811 || declspecs
->volatile_p
2812 || declspecs
->restrict_p
))
2815 pedwarn ("empty declaration with type qualifier "
2816 "does not redeclare tag");
2818 pending_xref_error ();
2822 pending_invalid_xref
= 0;
2823 t
= lookup_tag (code
, name
, 1);
2827 t
= make_node (code
);
2834 if (warned
!= 1 && !in_system_header
)
2836 pedwarn ("useless type name in empty declaration");
2841 else if (warned
!= 1 && !in_system_header
&& declspecs
->typedef_p
)
2843 pedwarn ("useless type name in empty declaration");
2847 pending_invalid_xref
= 0;
2849 if (declspecs
->inline_p
)
2851 error ("%<inline%> in empty declaration");
2855 if (current_scope
== file_scope
&& declspecs
->storage_class
== csc_auto
)
2857 error ("%<auto%> in file-scope empty declaration");
2861 if (current_scope
== file_scope
&& declspecs
->storage_class
== csc_register
)
2863 error ("%<register%> in file-scope empty declaration");
2867 if (!warned
&& !in_system_header
&& declspecs
->storage_class
!= csc_none
)
2869 warning (0, "useless storage class specifier in empty declaration");
2873 if (!warned
&& !in_system_header
&& declspecs
->thread_p
)
2875 warning (0, "useless %<__thread%> in empty declaration");
2879 if (!warned
&& !in_system_header
&& (declspecs
->const_p
2880 || declspecs
->volatile_p
2881 || declspecs
->restrict_p
))
2883 warning (0, "useless type qualifier in empty declaration");
2890 pedwarn ("empty declaration");
2895 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2896 bits. SPECS represents declaration specifiers that the grammar
2897 only permits to contain type qualifiers and attributes. */
2900 quals_from_declspecs (const struct c_declspecs
*specs
)
2902 int quals
= ((specs
->const_p
? TYPE_QUAL_CONST
: 0)
2903 | (specs
->volatile_p
? TYPE_QUAL_VOLATILE
: 0)
2904 | (specs
->restrict_p
? TYPE_QUAL_RESTRICT
: 0));
2905 gcc_assert (!specs
->type
2906 && !specs
->decl_attr
2907 && specs
->typespec_word
== cts_none
2908 && specs
->storage_class
== csc_none
2909 && !specs
->typedef_p
2910 && !specs
->explicit_signed_p
2911 && !specs
->deprecated_p
2913 && !specs
->long_long_p
2916 && !specs
->unsigned_p
2917 && !specs
->complex_p
2919 && !specs
->thread_p
);
2923 /* Construct an array declarator. EXPR is the expression inside [], or
2924 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2925 to the pointer to which a parameter array is converted). STATIC_P is
2926 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
2927 is true if the array is [*], a VLA of unspecified length which is
2928 nevertheless a complete type (not currently implemented by GCC),
2929 false otherwise. The field for the contained declarator is left to be
2930 filled in by set_array_declarator_inner. */
2932 struct c_declarator
*
2933 build_array_declarator (tree expr
, struct c_declspecs
*quals
, bool static_p
,
2936 struct c_declarator
*declarator
= XOBNEW (&parser_obstack
,
2937 struct c_declarator
);
2938 declarator
->kind
= cdk_array
;
2939 declarator
->declarator
= 0;
2940 declarator
->u
.array
.dimen
= expr
;
2943 declarator
->u
.array
.attrs
= quals
->attrs
;
2944 declarator
->u
.array
.quals
= quals_from_declspecs (quals
);
2948 declarator
->u
.array
.attrs
= NULL_TREE
;
2949 declarator
->u
.array
.quals
= 0;
2951 declarator
->u
.array
.static_p
= static_p
;
2952 declarator
->u
.array
.vla_unspec_p
= vla_unspec_p
;
2953 if (pedantic
&& !flag_isoc99
)
2955 if (static_p
|| quals
!= NULL
)
2956 pedwarn ("ISO C90 does not support %<static%> or type "
2957 "qualifiers in parameter array declarators");
2959 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
2962 warning (0, "GCC does not yet properly implement %<[*]%> array declarators");
2966 /* Set the contained declarator of an array declarator. DECL is the
2967 declarator, as constructed by build_array_declarator; INNER is what
2968 appears on the left of the []. ABSTRACT_P is true if it is an
2969 abstract declarator, false otherwise; this is used to reject static
2970 and type qualifiers in abstract declarators, where they are not in
2971 the C99 grammar (subject to possible change in DR#289). */
2973 struct c_declarator
*
2974 set_array_declarator_inner (struct c_declarator
*decl
,
2975 struct c_declarator
*inner
, bool abstract_p
)
2977 decl
->declarator
= inner
;
2978 if (abstract_p
&& (decl
->u
.array
.quals
!= TYPE_UNQUALIFIED
2979 || decl
->u
.array
.attrs
!= NULL_TREE
2980 || decl
->u
.array
.static_p
))
2981 error ("static or type qualifiers in abstract declarator");
2985 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2988 groktypename (struct c_type_name
*type_name
)
2991 tree attrs
= type_name
->specs
->attrs
;
2993 type_name
->specs
->attrs
= NULL_TREE
;
2995 type
= grokdeclarator (type_name
->declarator
, type_name
->specs
, TYPENAME
,
2998 /* Apply attributes. */
2999 decl_attributes (&type
, attrs
, 0);
3004 /* Decode a declarator in an ordinary declaration or data definition.
3005 This is called as soon as the type information and variable name
3006 have been parsed, before parsing the initializer if any.
3007 Here we create the ..._DECL node, fill in its type,
3008 and put it on the list of decls for the current context.
3009 The ..._DECL node is returned as the value.
3011 Exception: for arrays where the length is not specified,
3012 the type is left null, to be filled in by `finish_decl'.
3014 Function definitions do not come here; they go to start_function
3015 instead. However, external and forward declarations of functions
3016 do go through here. Structure field declarations are done by
3017 grokfield and not through here. */
3020 start_decl (struct c_declarator
*declarator
, struct c_declspecs
*declspecs
,
3021 bool initialized
, tree attributes
)
3026 /* An object declared as __attribute__((deprecated)) suppresses
3027 warnings of uses of other deprecated items. */
3028 if (lookup_attribute ("deprecated", attributes
))
3029 deprecated_state
= DEPRECATED_SUPPRESS
;
3031 decl
= grokdeclarator (declarator
, declspecs
,
3032 NORMAL
, initialized
, NULL
);
3036 deprecated_state
= DEPRECATED_NORMAL
;
3038 if (warn_main
> 0 && TREE_CODE (decl
) != FUNCTION_DECL
3039 && MAIN_NAME_P (DECL_NAME (decl
)))
3040 warning (0, "%J%qD is usually a function", decl
, decl
);
3043 /* Is it valid for this decl to have an initializer at all?
3044 If not, set INITIALIZED to zero, which will indirectly
3045 tell 'finish_decl' to ignore the initializer once it is parsed. */
3046 switch (TREE_CODE (decl
))
3049 error ("typedef %qD is initialized (use __typeof__ instead)", decl
);
3054 error ("function %qD is initialized like a variable", decl
);
3059 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3060 error ("parameter %qD is initialized", decl
);
3065 /* Don't allow initializations for incomplete types except for
3066 arrays which might be completed by the initialization. */
3068 /* This can happen if the array size is an undefined macro.
3069 We already gave a warning, so we don't need another one. */
3070 if (TREE_TYPE (decl
) == error_mark_node
)
3072 else if (COMPLETE_TYPE_P (TREE_TYPE (decl
)))
3074 /* A complete type is ok if size is fixed. */
3076 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl
))) != INTEGER_CST
3077 || C_DECL_VARIABLE_SIZE (decl
))
3079 error ("variable-sized object may not be initialized");
3083 else if (TREE_CODE (TREE_TYPE (decl
)) != ARRAY_TYPE
)
3085 error ("variable %qD has initializer but incomplete type", decl
);
3088 else if (C_DECL_VARIABLE_SIZE (decl
))
3090 /* Although C99 is unclear about whether incomplete arrays
3091 of VLAs themselves count as VLAs, it does not make
3092 sense to permit them to be initialized given that
3093 ordinary VLAs may not be initialized. */
3094 error ("variable-sized object may not be initialized");
3101 if (current_scope
== file_scope
)
3102 TREE_STATIC (decl
) = 1;
3104 /* Tell 'pushdecl' this is an initialized decl
3105 even though we don't yet have the initializer expression.
3106 Also tell 'finish_decl' it may store the real initializer. */
3107 DECL_INITIAL (decl
) = error_mark_node
;
3110 /* If this is a function declaration, write a record describing it to the
3111 prototypes file (if requested). */
3113 if (TREE_CODE (decl
) == FUNCTION_DECL
)
3114 gen_aux_info_record (decl
, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl
)) != 0);
3116 /* ANSI specifies that a tentative definition which is not merged with
3117 a non-tentative definition behaves exactly like a definition with an
3118 initializer equal to zero. (Section 3.7.2)
3120 -fno-common gives strict ANSI behavior, though this tends to break
3121 a large body of code that grew up without this rule.
3123 Thread-local variables are never common, since there's no entrenched
3124 body of code to break, and it allows more efficient variable references
3125 in the presence of dynamic linking. */
3127 if (TREE_CODE (decl
) == VAR_DECL
3129 && TREE_PUBLIC (decl
)
3130 && !DECL_THREAD_LOCAL (decl
)
3132 DECL_COMMON (decl
) = 1;
3134 /* Set attributes here so if duplicate decl, will have proper attributes. */
3135 decl_attributes (&decl
, attributes
, 0);
3137 if (TREE_CODE (decl
) == FUNCTION_DECL
3138 && targetm
.calls
.promote_prototypes (TREE_TYPE (decl
)))
3140 struct c_declarator
*ce
= declarator
;
3142 if (ce
->kind
== cdk_pointer
)
3143 ce
= declarator
->declarator
;
3144 if (ce
->kind
== cdk_function
)
3146 tree args
= ce
->u
.arg_info
->parms
;
3147 for (; args
; args
= TREE_CHAIN (args
))
3149 tree type
= TREE_TYPE (args
);
3150 if (type
&& INTEGRAL_TYPE_P (type
)
3151 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
3152 DECL_ARG_TYPE (args
) = integer_type_node
;
3157 if (TREE_CODE (decl
) == FUNCTION_DECL
3158 && DECL_DECLARED_INLINE_P (decl
)
3159 && DECL_UNINLINABLE (decl
)
3160 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl
)))
3161 warning (0, "%Jinline function %qD given attribute noinline", decl
, decl
);
3163 /* Add this decl to the current scope.
3164 TEM may equal DECL or it may be a previous decl of the same name. */
3165 tem
= pushdecl (decl
);
3167 if (initialized
&& DECL_EXTERNAL (tem
))
3169 DECL_EXTERNAL (tem
) = 0;
3170 TREE_STATIC (tem
) = 1;
3176 /* Finish processing of a declaration;
3177 install its initial value.
3178 If the length of an array type is not known before,
3179 it must be determined now, from the initial value, or it is an error. */
3182 finish_decl (tree decl
, tree init
, tree asmspec_tree
)
3184 tree type
= TREE_TYPE (decl
);
3185 int was_incomplete
= (DECL_SIZE (decl
) == 0);
3186 const char *asmspec
= 0;
3188 /* If a name was specified, get the string. */
3189 if ((TREE_CODE (decl
) == FUNCTION_DECL
|| TREE_CODE (decl
) == VAR_DECL
)
3190 && DECL_FILE_SCOPE_P (decl
))
3191 asmspec_tree
= maybe_apply_renaming_pragma (decl
, asmspec_tree
);
3193 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
3195 /* If `start_decl' didn't like having an initialization, ignore it now. */
3196 if (init
!= 0 && DECL_INITIAL (decl
) == 0)
3199 /* Don't crash if parm is initialized. */
3200 if (TREE_CODE (decl
) == PARM_DECL
)
3204 store_init_value (decl
, init
);
3206 if (c_dialect_objc () && (TREE_CODE (decl
) == VAR_DECL
3207 || TREE_CODE (decl
) == FUNCTION_DECL
3208 || TREE_CODE (decl
) == FIELD_DECL
))
3209 objc_check_decl (decl
);
3211 /* Deduce size of array from initialization, if not already known. */
3212 if (TREE_CODE (type
) == ARRAY_TYPE
3213 && TYPE_DOMAIN (type
) == 0
3214 && TREE_CODE (decl
) != TYPE_DECL
)
3217 = (TREE_STATIC (decl
)
3218 /* Even if pedantic, an external linkage array
3219 may have incomplete type at first. */
3220 ? pedantic
&& !TREE_PUBLIC (decl
)
3221 : !DECL_EXTERNAL (decl
));
3223 = complete_array_type (&TREE_TYPE (decl
), DECL_INITIAL (decl
),
3226 /* Get the completed type made by complete_array_type. */
3227 type
= TREE_TYPE (decl
);
3230 error ("%Jinitializer fails to determine size of %qD", decl
, decl
);
3232 else if (failure
== 2)
3235 error ("%Jarray size missing in %qD", decl
, decl
);
3236 /* If a `static' var's size isn't known,
3237 make it extern as well as static, so it does not get
3239 If it is not `static', then do not mark extern;
3240 finish_incomplete_decl will give it a default size
3241 and it will get allocated. */
3242 else if (!pedantic
&& TREE_STATIC (decl
) && !TREE_PUBLIC (decl
))
3243 DECL_EXTERNAL (decl
) = 1;
3245 else if (failure
== 3)
3246 error ("%Jzero or negative size array %qD", decl
, decl
);
3248 if (DECL_INITIAL (decl
))
3249 TREE_TYPE (DECL_INITIAL (decl
)) = type
;
3251 layout_decl (decl
, 0);
3254 if (TREE_CODE (decl
) == VAR_DECL
)
3256 if (DECL_SIZE (decl
) == 0 && TREE_TYPE (decl
) != error_mark_node
3257 && COMPLETE_TYPE_P (TREE_TYPE (decl
)))
3258 layout_decl (decl
, 0);
3260 if (DECL_SIZE (decl
) == 0
3261 /* Don't give an error if we already gave one earlier. */
3262 && TREE_TYPE (decl
) != error_mark_node
3263 && (TREE_STATIC (decl
)
3264 /* A static variable with an incomplete type
3265 is an error if it is initialized.
3266 Also if it is not file scope.
3267 Otherwise, let it through, but if it is not `extern'
3268 then it may cause an error message later. */
3269 ? (DECL_INITIAL (decl
) != 0
3270 || !DECL_FILE_SCOPE_P (decl
))
3271 /* An automatic variable with an incomplete type
3273 : !DECL_EXTERNAL (decl
)))
3275 error ("%Jstorage size of %qD isn%'t known", decl
, decl
);
3276 TREE_TYPE (decl
) = error_mark_node
;
3279 if ((DECL_EXTERNAL (decl
) || TREE_STATIC (decl
))
3280 && DECL_SIZE (decl
) != 0)
3282 if (TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
)
3283 constant_expression_warning (DECL_SIZE (decl
));
3285 error ("%Jstorage size of %qD isn%'t constant", decl
, decl
);
3288 if (TREE_USED (type
))
3289 TREE_USED (decl
) = 1;
3292 /* If this is a function and an assembler name is specified, reset DECL_RTL
3293 so we can give it its new name. Also, update built_in_decls if it
3294 was a normal built-in. */
3295 if (TREE_CODE (decl
) == FUNCTION_DECL
&& asmspec
)
3297 if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
3298 set_builtin_user_assembler_name (decl
, asmspec
);
3299 set_user_assembler_name (decl
, asmspec
);
3302 /* If #pragma weak was used, mark the decl weak now. */
3303 maybe_apply_pragma_weak (decl
);
3305 /* If this is a variable definition, determine its ELF visibility. */
3306 if (TREE_CODE (decl
) == VAR_DECL
3307 && TREE_STATIC (decl
)
3308 && !DECL_EXTERNAL (decl
))
3309 c_determine_visibility (decl
);
3311 /* Output the assembler code and/or RTL code for variables and functions,
3312 unless the type is an undefined structure or union.
3313 If not, it will get done when the type is completed. */
3315 if (TREE_CODE (decl
) == VAR_DECL
|| TREE_CODE (decl
) == FUNCTION_DECL
)
3317 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3318 if (c_dialect_objc ())
3319 objc_check_decl (decl
);
3323 /* If this is not a static variable, issue a warning.
3324 It doesn't make any sense to give an ASMSPEC for an
3325 ordinary, non-register local variable. Historically,
3326 GCC has accepted -- but ignored -- the ASMSPEC in
3328 if (!DECL_FILE_SCOPE_P (decl
)
3329 && TREE_CODE (decl
) == VAR_DECL
3330 && !C_DECL_REGISTER (decl
)
3331 && !TREE_STATIC (decl
))
3332 warning (0, "%Jignoring asm-specifier for non-static local "
3333 "variable %qD", decl
, decl
);
3334 else if (C_DECL_REGISTER (decl
))
3335 change_decl_assembler_name (decl
, get_identifier (asmspec
));
3337 set_user_assembler_name (decl
, asmspec
);
3340 if (DECL_FILE_SCOPE_P (decl
))
3342 if (DECL_INITIAL (decl
) == NULL_TREE
3343 || DECL_INITIAL (decl
) == error_mark_node
)
3344 /* Don't output anything
3345 when a tentative file-scope definition is seen.
3346 But at end of compilation, do output code for them. */
3347 DECL_DEFER_OUTPUT (decl
) = 1;
3348 rest_of_decl_compilation (decl
, true, 0);
3352 /* In conjunction with an ASMSPEC, the `register'
3353 keyword indicates that we should place the variable
3354 in a particular register. */
3355 if (asmspec
&& C_DECL_REGISTER (decl
))
3357 DECL_HARD_REGISTER (decl
) = 1;
3358 /* This cannot be done for a structure with volatile
3359 fields, on which DECL_REGISTER will have been
3361 if (!DECL_REGISTER (decl
))
3362 error ("cannot put object with volatile field into register");
3365 if (TREE_CODE (decl
) != FUNCTION_DECL
)
3367 /* If we're building a variable sized type, and we might be
3368 reachable other than via the top of the current binding
3369 level, then create a new BIND_EXPR so that we deallocate
3370 the object at the right time. */
3371 /* Note that DECL_SIZE can be null due to errors. */
3372 if (DECL_SIZE (decl
)
3373 && !TREE_CONSTANT (DECL_SIZE (decl
))
3374 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list
))
3377 bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, NULL
);
3378 TREE_SIDE_EFFECTS (bind
) = 1;
3380 BIND_EXPR_BODY (bind
) = push_stmt_list ();
3382 add_stmt (build_stmt (DECL_EXPR
, decl
));
3387 if (!DECL_FILE_SCOPE_P (decl
))
3389 /* Recompute the RTL of a local array now
3390 if it used to be an incomplete type. */
3392 && !TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
3394 /* If we used it already as memory, it must stay in memory. */
3395 TREE_ADDRESSABLE (decl
) = TREE_USED (decl
);
3396 /* If it's still incomplete now, no init will save it. */
3397 if (DECL_SIZE (decl
) == 0)
3398 DECL_INITIAL (decl
) = 0;
3403 /* If this was marked 'used', be sure it will be output. */
3404 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl
)))
3405 mark_decl_referenced (decl
);
3407 if (TREE_CODE (decl
) == TYPE_DECL
)
3409 if (!DECL_FILE_SCOPE_P (decl
)
3410 && variably_modified_type_p (TREE_TYPE (decl
), NULL_TREE
))
3411 add_stmt (build_stmt (DECL_EXPR
, decl
));
3413 rest_of_decl_compilation (decl
, DECL_FILE_SCOPE_P (decl
), 0);
3416 /* At the end of a declaration, throw away any variable type sizes
3417 of types defined inside that declaration. There is no use
3418 computing them in the following function definition. */
3419 if (current_scope
== file_scope
)
3420 get_pending_sizes ();
3422 /* Install a cleanup (aka destructor) if one was given. */
3423 if (TREE_CODE (decl
) == VAR_DECL
&& !TREE_STATIC (decl
))
3425 tree attr
= lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl
));
3428 tree cleanup_id
= TREE_VALUE (TREE_VALUE (attr
));
3429 tree cleanup_decl
= lookup_name (cleanup_id
);
3432 /* Build "cleanup(&decl)" for the destructor. */
3433 cleanup
= build_unary_op (ADDR_EXPR
, decl
, 0);
3434 cleanup
= build_tree_list (NULL_TREE
, cleanup
);
3435 cleanup
= build_function_call (cleanup_decl
, cleanup
);
3437 /* Don't warn about decl unused; the cleanup uses it. */
3438 TREE_USED (decl
) = 1;
3439 TREE_USED (cleanup_decl
) = 1;
3441 /* Initialize EH, if we've been told to do so. */
3442 if (flag_exceptions
&& !c_eh_initialized_p
)
3444 c_eh_initialized_p
= true;
3445 eh_personality_libfunc
3446 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3447 ? "__gcc_personality_sj0"
3448 : "__gcc_personality_v0");
3449 using_eh_for_cleanups ();
3452 push_cleanup (decl
, cleanup
, false);
3457 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3460 grokparm (const struct c_parm
*parm
)
3462 tree decl
= grokdeclarator (parm
->declarator
, parm
->specs
, PARM
, false,
3465 decl_attributes (&decl
, parm
->attrs
, 0);
3470 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3471 and push that on the current scope. */
3474 push_parm_decl (const struct c_parm
*parm
)
3478 decl
= grokdeclarator (parm
->declarator
, parm
->specs
, PARM
, false, NULL
);
3479 decl_attributes (&decl
, parm
->attrs
, 0);
3481 decl
= pushdecl (decl
);
3483 finish_decl (decl
, NULL_TREE
, NULL_TREE
);
3486 /* Mark all the parameter declarations to date as forward decls.
3487 Also diagnose use of this extension. */
3490 mark_forward_parm_decls (void)
3492 struct c_binding
*b
;
3494 if (pedantic
&& !current_scope
->warned_forward_parm_decls
)
3496 pedwarn ("ISO C forbids forward parameter declarations");
3497 current_scope
->warned_forward_parm_decls
= true;
3500 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
3501 if (TREE_CODE (b
->decl
) == PARM_DECL
)
3502 TREE_ASM_WRITTEN (b
->decl
) = 1;
3505 static GTY(()) int compound_literal_number
;
3507 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3508 literal, which may be an incomplete array type completed by the
3509 initializer; INIT is a CONSTRUCTOR that initializes the compound
3513 build_compound_literal (tree type
, tree init
)
3515 /* We do not use start_decl here because we have a type, not a declarator;
3516 and do not use finish_decl because the decl should be stored inside
3517 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3522 if (type
== error_mark_node
)
3523 return error_mark_node
;
3525 decl
= build_decl (VAR_DECL
, NULL_TREE
, type
);
3526 DECL_EXTERNAL (decl
) = 0;
3527 TREE_PUBLIC (decl
) = 0;
3528 TREE_STATIC (decl
) = (current_scope
== file_scope
);
3529 DECL_CONTEXT (decl
) = current_function_decl
;
3530 TREE_USED (decl
) = 1;
3531 TREE_TYPE (decl
) = type
;
3532 TREE_READONLY (decl
) = TYPE_READONLY (type
);
3533 store_init_value (decl
, init
);
3535 if (TREE_CODE (type
) == ARRAY_TYPE
&& !COMPLETE_TYPE_P (type
))
3537 int failure
= complete_array_type (&TREE_TYPE (decl
),
3538 DECL_INITIAL (decl
), true);
3539 gcc_assert (!failure
);
3541 type
= TREE_TYPE (decl
);
3542 TREE_TYPE (DECL_INITIAL (decl
)) = type
;
3545 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
3546 return error_mark_node
;
3548 stmt
= build_stmt (DECL_EXPR
, decl
);
3549 complit
= build1 (COMPOUND_LITERAL_EXPR
, type
, stmt
);
3550 TREE_SIDE_EFFECTS (complit
) = 1;
3552 layout_decl (decl
, 0);
3554 if (TREE_STATIC (decl
))
3556 /* This decl needs a name for the assembler output. We also need
3557 a unique suffix to be added to the name. */
3560 ASM_FORMAT_PRIVATE_NAME (name
, "__compound_literal",
3561 compound_literal_number
);
3562 compound_literal_number
++;
3563 DECL_NAME (decl
) = get_identifier (name
);
3564 DECL_DEFER_OUTPUT (decl
) = 1;
3565 DECL_COMDAT (decl
) = 1;
3566 DECL_ARTIFICIAL (decl
) = 1;
3567 DECL_IGNORED_P (decl
) = 1;
3569 rest_of_decl_compilation (decl
, 1, 0);
3575 /* Determine whether TYPE is a structure with a flexible array member,
3576 or a union containing such a structure (possibly recursively). */
3579 flexible_array_type_p (tree type
)
3582 switch (TREE_CODE (type
))
3585 x
= TYPE_FIELDS (type
);
3588 while (TREE_CHAIN (x
) != NULL_TREE
)
3590 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
3591 && TYPE_SIZE (TREE_TYPE (x
)) == NULL_TREE
3592 && TYPE_DOMAIN (TREE_TYPE (x
)) != NULL_TREE
3593 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x
))) == NULL_TREE
)
3597 for (x
= TYPE_FIELDS (type
); x
!= NULL_TREE
; x
= TREE_CHAIN (x
))
3599 if (flexible_array_type_p (TREE_TYPE (x
)))
3608 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3609 replacing with appropriate values if they are invalid. */
3611 check_bitfield_type_and_width (tree
*type
, tree
*width
, const char *orig_name
)
3614 unsigned int max_width
;
3615 unsigned HOST_WIDE_INT w
;
3616 const char *name
= orig_name
? orig_name
: _("<anonymous>");
3618 /* Detect and ignore out of range field width and process valid
3620 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width
))
3621 || TREE_CODE (*width
) != INTEGER_CST
)
3623 error ("bit-field %qs width not an integer constant", name
);
3624 *width
= integer_one_node
;
3628 constant_expression_warning (*width
);
3629 if (tree_int_cst_sgn (*width
) < 0)
3631 error ("negative width in bit-field %qs", name
);
3632 *width
= integer_one_node
;
3634 else if (integer_zerop (*width
) && orig_name
)
3636 error ("zero width for bit-field %qs", name
);
3637 *width
= integer_one_node
;
3641 /* Detect invalid bit-field type. */
3642 if (TREE_CODE (*type
) != INTEGER_TYPE
3643 && TREE_CODE (*type
) != BOOLEAN_TYPE
3644 && TREE_CODE (*type
) != ENUMERAL_TYPE
)
3646 error ("bit-field %qs has invalid type", name
);
3647 *type
= unsigned_type_node
;
3650 type_mv
= TYPE_MAIN_VARIANT (*type
);
3652 && type_mv
!= integer_type_node
3653 && type_mv
!= unsigned_type_node
3654 && type_mv
!= boolean_type_node
)
3655 pedwarn ("type of bit-field %qs is a GCC extension", name
);
3657 if (type_mv
== boolean_type_node
)
3658 max_width
= CHAR_TYPE_SIZE
;
3660 max_width
= TYPE_PRECISION (*type
);
3662 if (0 < compare_tree_int (*width
, max_width
))
3664 error ("width of %qs exceeds its type", name
);
3666 *width
= build_int_cst (NULL_TREE
, w
);
3669 w
= tree_low_cst (*width
, 1);
3671 if (TREE_CODE (*type
) == ENUMERAL_TYPE
)
3673 struct lang_type
*lt
= TYPE_LANG_SPECIFIC (*type
);
3675 || w
< min_precision (lt
->enum_min
, TYPE_UNSIGNED (*type
))
3676 || w
< min_precision (lt
->enum_max
, TYPE_UNSIGNED (*type
)))
3677 warning (0, "%qs is narrower than values of its type", name
);
3681 /* Given declspecs and a declarator,
3682 determine the name and type of the object declared
3683 and construct a ..._DECL node for it.
3684 (In one case we can return a ..._TYPE node instead.
3685 For invalid input we sometimes return 0.)
3687 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3689 DECL_CONTEXT says which syntactic context this declaration is in:
3690 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3691 FUNCDEF for a function definition. Like NORMAL but a few different
3692 error messages in each case. Return value may be zero meaning
3693 this definition is too screwy to try to parse.
3694 PARM for a parameter declaration (either within a function prototype
3695 or before a function body). Make a PARM_DECL, or return void_type_node.
3696 TYPENAME if for a typename (in a cast or sizeof).
3697 Don't make a DECL node; just return the ..._TYPE node.
3698 FIELD for a struct or union field; make a FIELD_DECL.
3699 INITIALIZED is true if the decl has an initializer.
3700 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3701 representing the width of the bit-field.
3703 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3704 It may also be so in the PARM case, for a prototype where the
3705 argument type is specified but not the name.
3707 This function is where the complicated C meanings of `static'
3708 and `extern' are interpreted. */
3711 grokdeclarator (const struct c_declarator
*declarator
,
3712 struct c_declspecs
*declspecs
,
3713 enum decl_context decl_context
, bool initialized
, tree
*width
)
3715 tree type
= declspecs
->type
;
3716 bool threadp
= declspecs
->thread_p
;
3717 enum c_storage_class storage_class
= declspecs
->storage_class
;
3721 int type_quals
= TYPE_UNQUALIFIED
;
3722 const char *name
, *orig_name
;
3723 tree typedef_type
= 0;
3724 int funcdef_flag
= 0;
3725 bool funcdef_syntax
= false;
3726 int size_varies
= 0;
3727 tree decl_attr
= declspecs
->decl_attr
;
3728 int array_ptr_quals
= TYPE_UNQUALIFIED
;
3729 tree array_ptr_attrs
= NULL_TREE
;
3730 int array_parm_static
= 0;
3731 tree returned_attrs
= NULL_TREE
;
3732 bool bitfield
= width
!= NULL
;
3734 struct c_arg_info
*arg_info
= 0;
3736 if (decl_context
== FUNCDEF
)
3737 funcdef_flag
= 1, decl_context
= NORMAL
;
3739 /* Look inside a declarator for the name being declared
3740 and get it as a string, for an error message. */
3742 const struct c_declarator
*decl
= declarator
;
3751 funcdef_syntax
= (decl
->kind
== cdk_function
);
3752 decl
= decl
->declarator
;
3756 decl
= decl
->declarator
;
3761 name
= IDENTIFIER_POINTER (decl
->u
.id
);
3773 /* A function definition's declarator must have the form of
3774 a function declarator. */
3776 if (funcdef_flag
&& !funcdef_syntax
)
3779 /* If this looks like a function definition, make it one,
3780 even if it occurs where parms are expected.
3781 Then store_parm_decls will reject it and not use it as a parm. */
3782 if (decl_context
== NORMAL
&& !funcdef_flag
&& current_scope
->parm_flag
)
3783 decl_context
= PARM
;
3785 if (declspecs
->deprecated_p
&& deprecated_state
!= DEPRECATED_SUPPRESS
)
3786 warn_deprecated_use (declspecs
->type
);
3788 typedef_type
= type
;
3789 size_varies
= C_TYPE_VARIABLE_SIZE (type
);
3791 /* Diagnose defaulting to "int". */
3793 if (declspecs
->default_int_p
&& !in_system_header
)
3795 /* Issue a warning if this is an ISO C 99 program or if
3796 -Wreturn-type and this is a function, or if -Wimplicit;
3797 prefer the former warning since it is more explicit. */
3798 if ((warn_implicit_int
|| warn_return_type
|| flag_isoc99
)
3800 warn_about_return_type
= 1;
3801 else if (warn_implicit_int
|| flag_isoc99
)
3802 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name
);
3805 /* Adjust the type if a bit-field is being declared,
3806 -funsigned-bitfields applied and the type is not explicitly
3808 if (bitfield
&& !flag_signed_bitfields
&& !declspecs
->explicit_signed_p
3809 && TREE_CODE (type
) == INTEGER_TYPE
)
3810 type
= c_common_unsigned_type (type
);
3812 /* Figure out the type qualifiers for the declaration. There are
3813 two ways a declaration can become qualified. One is something
3814 like `const int i' where the `const' is explicit. Another is
3815 something like `typedef const int CI; CI i' where the type of the
3816 declaration contains the `const'. A third possibility is that
3817 there is a type qualifier on the element type of a typedefed
3818 array type, in which case we should extract that qualifier so
3819 that c_apply_type_quals_to_decls receives the full list of
3820 qualifiers to work with (C90 is not entirely clear about whether
3821 duplicate qualifiers should be diagnosed in this case, but it
3822 seems most appropriate to do so). */
3823 element_type
= strip_array_types (type
);
3824 constp
= declspecs
->const_p
+ TYPE_READONLY (element_type
);
3825 restrictp
= declspecs
->restrict_p
+ TYPE_RESTRICT (element_type
);
3826 volatilep
= declspecs
->volatile_p
+ TYPE_VOLATILE (element_type
);
3827 if (pedantic
&& !flag_isoc99
)
3830 pedwarn ("duplicate %<const%>");
3832 pedwarn ("duplicate %<restrict%>");
3834 pedwarn ("duplicate %<volatile%>");
3836 if (!flag_gen_aux_info
&& (TYPE_QUALS (element_type
)))
3837 type
= TYPE_MAIN_VARIANT (type
);
3838 type_quals
= ((constp
? TYPE_QUAL_CONST
: 0)
3839 | (restrictp
? TYPE_QUAL_RESTRICT
: 0)
3840 | (volatilep
? TYPE_QUAL_VOLATILE
: 0));
3842 /* Warn about storage classes that are invalid for certain
3843 kinds of declarations (parameters, typenames, etc.). */
3847 || storage_class
== csc_auto
3848 || storage_class
== csc_register
3849 || storage_class
== csc_typedef
))
3851 if (storage_class
== csc_auto
3852 && (pedantic
|| current_scope
== file_scope
))
3853 pedwarn ("function definition declared %<auto%>");
3854 if (storage_class
== csc_register
)
3855 error ("function definition declared %<register%>");
3856 if (storage_class
== csc_typedef
)
3857 error ("function definition declared %<typedef%>");
3859 error ("function definition declared %<__thread%>");
3861 if (storage_class
== csc_auto
3862 || storage_class
== csc_register
3863 || storage_class
== csc_typedef
)
3864 storage_class
= csc_none
;
3866 else if (decl_context
!= NORMAL
&& (storage_class
!= csc_none
|| threadp
))
3868 if (decl_context
== PARM
&& storage_class
== csc_register
)
3872 switch (decl_context
)
3875 error ("storage class specified for structure field %qs",
3879 error ("storage class specified for parameter %qs", name
);
3882 error ("storage class specified for typename");
3885 storage_class
= csc_none
;
3889 else if (storage_class
== csc_extern
3893 /* 'extern' with initialization is invalid if not at file scope. */
3894 if (current_scope
== file_scope
)
3895 warning (0, "%qs initialized and declared %<extern%>", name
);
3897 error ("%qs has both %<extern%> and initializer", name
);
3899 else if (current_scope
== file_scope
)
3901 if (storage_class
== csc_auto
)
3902 error ("file-scope declaration of %qs specifies %<auto%>", name
);
3903 if (pedantic
&& storage_class
== csc_register
)
3904 pedwarn ("file-scope declaration of %qs specifies %<register%>", name
);
3908 if (storage_class
== csc_extern
&& funcdef_flag
)
3909 error ("nested function %qs declared %<extern%>", name
);
3910 else if (threadp
&& storage_class
== csc_none
)
3912 error ("function-scope %qs implicitly auto and declared "
3919 /* Now figure out the structure of the declarator proper.
3920 Descend through it, creating more complex types, until we reach
3921 the declared identifier (or NULL_TREE, in an absolute declarator).
3922 At each stage we maintain an unqualified version of the type
3923 together with any qualifiers that should be applied to it with
3924 c_build_qualified_type; this way, array types including
3925 multidimensional array types are first built up in unqualified
3926 form and then the qualified form is created with
3927 TYPE_MAIN_VARIANT pointing to the unqualified form. */
3929 while (declarator
&& declarator
->kind
!= cdk_id
)
3931 if (type
== error_mark_node
)
3933 declarator
= declarator
->declarator
;
3937 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
3938 a cdk_pointer (for *...),
3939 a cdk_function (for ...(...)),
3940 a cdk_attrs (for nested attributes),
3941 or a cdk_id (for the name being declared
3942 or the place in an absolute declarator
3943 where the name was omitted).
3944 For the last case, we have just exited the loop.
3946 At this point, TYPE is the type of elements of an array,
3947 or for a function to return, or for a pointer to point to.
3948 After this sequence of ifs, TYPE is the type of the
3949 array or function or pointer, and DECLARATOR has had its
3950 outermost layer removed. */
3952 if (array_ptr_quals
!= TYPE_UNQUALIFIED
3953 || array_ptr_attrs
!= NULL_TREE
3954 || array_parm_static
)
3956 /* Only the innermost declarator (making a parameter be of
3957 array type which is converted to pointer type)
3958 may have static or type qualifiers. */
3959 error ("static or type qualifiers in non-parameter array declarator");
3960 array_ptr_quals
= TYPE_UNQUALIFIED
;
3961 array_ptr_attrs
= NULL_TREE
;
3962 array_parm_static
= 0;
3965 switch (declarator
->kind
)
3969 /* A declarator with embedded attributes. */
3970 tree attrs
= declarator
->u
.attrs
;
3971 const struct c_declarator
*inner_decl
;
3973 declarator
= declarator
->declarator
;
3974 inner_decl
= declarator
;
3975 while (inner_decl
->kind
== cdk_attrs
)
3976 inner_decl
= inner_decl
->declarator
;
3977 if (inner_decl
->kind
== cdk_id
)
3978 attr_flags
|= (int) ATTR_FLAG_DECL_NEXT
;
3979 else if (inner_decl
->kind
== cdk_function
)
3980 attr_flags
|= (int) ATTR_FLAG_FUNCTION_NEXT
;
3981 else if (inner_decl
->kind
== cdk_array
)
3982 attr_flags
|= (int) ATTR_FLAG_ARRAY_NEXT
;
3983 returned_attrs
= decl_attributes (&type
,
3984 chainon (returned_attrs
, attrs
),
3990 tree itype
= NULL_TREE
;
3991 tree size
= declarator
->u
.array
.dimen
;
3992 /* The index is a signed object `sizetype' bits wide. */
3993 tree index_type
= c_common_signed_type (sizetype
);
3995 array_ptr_quals
= declarator
->u
.array
.quals
;
3996 array_ptr_attrs
= declarator
->u
.array
.attrs
;
3997 array_parm_static
= declarator
->u
.array
.static_p
;
3999 declarator
= declarator
->declarator
;
4001 /* Check for some types that there cannot be arrays of. */
4003 if (VOID_TYPE_P (type
))
4005 error ("declaration of %qs as array of voids", name
);
4006 type
= error_mark_node
;
4009 if (TREE_CODE (type
) == FUNCTION_TYPE
)
4011 error ("declaration of %qs as array of functions", name
);
4012 type
= error_mark_node
;
4015 if (pedantic
&& !in_system_header
&& flexible_array_type_p (type
))
4016 pedwarn ("invalid use of structure with flexible array member");
4018 if (size
== error_mark_node
)
4019 type
= error_mark_node
;
4021 if (type
== error_mark_node
)
4024 /* If size was specified, set ITYPE to a range-type for
4025 that size. Otherwise, ITYPE remains null. finish_decl
4026 may figure it out from an initial value. */
4030 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4032 STRIP_TYPE_NOPS (size
);
4034 if (!INTEGRAL_TYPE_P (TREE_TYPE (size
)))
4036 error ("size of array %qs has non-integer type", name
);
4037 size
= integer_one_node
;
4040 if (pedantic
&& integer_zerop (size
))
4041 pedwarn ("ISO C forbids zero-size array %qs", name
);
4043 if (TREE_CODE (size
) == INTEGER_CST
)
4045 constant_expression_warning (size
);
4046 if (tree_int_cst_sgn (size
) < 0)
4048 error ("size of array %qs is negative", name
);
4049 size
= integer_one_node
;
4054 /* Make sure the array size remains visibly
4055 nonconstant even if it is (eg) a const variable
4056 with known value. */
4059 if (!flag_isoc99
&& pedantic
)
4061 if (TREE_CONSTANT (size
))
4062 pedwarn ("ISO C90 forbids array %qs whose size "
4063 "can%'t be evaluated",
4066 pedwarn ("ISO C90 forbids variable-size array %qs",
4071 if (integer_zerop (size
))
4073 /* A zero-length array cannot be represented with
4074 an unsigned index type, which is what we'll
4075 get with build_index_type. Create an
4076 open-ended range instead. */
4077 itype
= build_range_type (sizetype
, size
, NULL_TREE
);
4081 /* Arrange for the SAVE_EXPR on the inside of the
4082 MINUS_EXPR, which allows the -1 to get folded
4083 with the +1 that happens when building TYPE_SIZE. */
4085 size
= variable_size (size
);
4087 /* Compute the maximum valid index, that is, size
4088 - 1. Do the calculation in index_type, so that
4089 if it is a variable the computations will be
4090 done in the proper mode. */
4091 itype
= fold (build2 (MINUS_EXPR
, index_type
,
4092 convert (index_type
, size
),
4093 convert (index_type
,
4096 /* If that overflowed, the array is too big. ???
4097 While a size of INT_MAX+1 technically shouldn't
4098 cause an overflow (because we subtract 1), the
4099 overflow is recorded during the conversion to
4100 index_type, before the subtraction. Handling
4101 this case seems like an unnecessary
4103 if (TREE_OVERFLOW (itype
))
4105 error ("size of array %qs is too large", name
);
4106 type
= error_mark_node
;
4110 itype
= build_index_type (itype
);
4113 else if (decl_context
== FIELD
)
4115 if (pedantic
&& !flag_isoc99
&& !in_system_header
)
4116 pedwarn ("ISO C90 does not support flexible array members");
4118 /* ISO C99 Flexible array members are effectively
4119 identical to GCC's zero-length array extension. */
4120 itype
= build_range_type (sizetype
, size_zero_node
, NULL_TREE
);
4123 /* Complain about arrays of incomplete types. */
4124 if (!COMPLETE_TYPE_P (type
))
4126 error ("array type has incomplete element type");
4127 type
= error_mark_node
;
4130 type
= build_array_type (type
, itype
);
4133 C_TYPE_VARIABLE_SIZE (type
) = 1;
4135 /* The GCC extension for zero-length arrays differs from
4136 ISO flexible array members in that sizeof yields
4138 if (size
&& integer_zerop (size
))
4140 TYPE_SIZE (type
) = bitsize_zero_node
;
4141 TYPE_SIZE_UNIT (type
) = size_zero_node
;
4144 if (decl_context
!= PARM
4145 && (array_ptr_quals
!= TYPE_UNQUALIFIED
4146 || array_ptr_attrs
!= NULL_TREE
4147 || array_parm_static
))
4149 error ("static or type qualifiers in non-parameter array declarator");
4150 array_ptr_quals
= TYPE_UNQUALIFIED
;
4151 array_ptr_attrs
= NULL_TREE
;
4152 array_parm_static
= 0;
4158 /* Say it's a definition only for the declarator closest
4159 to the identifier, apart possibly from some
4161 bool really_funcdef
= false;
4165 const struct c_declarator
*t
= declarator
->declarator
;
4166 while (t
->kind
== cdk_attrs
)
4168 really_funcdef
= (t
->kind
== cdk_id
);
4171 /* Declaring a function type. Make sure we have a valid
4172 type for the function to return. */
4173 if (type
== error_mark_node
)
4178 /* Warn about some types functions can't return. */
4179 if (TREE_CODE (type
) == FUNCTION_TYPE
)
4181 error ("%qs declared as function returning a function", name
);
4182 type
= integer_type_node
;
4184 if (TREE_CODE (type
) == ARRAY_TYPE
)
4186 error ("%qs declared as function returning an array", name
);
4187 type
= integer_type_node
;
4190 /* Construct the function type and go to the next
4191 inner layer of declarator. */
4192 arg_info
= declarator
->u
.arg_info
;
4193 arg_types
= grokparms (arg_info
, really_funcdef
);
4195 /* Type qualifiers before the return type of the function
4196 qualify the return type, not the function type. */
4199 /* Type qualifiers on a function return type are
4200 normally permitted by the standard but have no
4201 effect, so give a warning at -Wreturn-type.
4202 Qualifiers on a void return type are banned on
4203 function definitions in ISO C; GCC used to used
4204 them for noreturn functions. */
4205 if (VOID_TYPE_P (type
) && really_funcdef
)
4206 pedwarn ("function definition has qualified void return type");
4207 else if (warn_return_type
)
4208 warning (0, "type qualifiers ignored on function return type");
4210 type
= c_build_qualified_type (type
, type_quals
);
4212 type_quals
= TYPE_UNQUALIFIED
;
4214 type
= build_function_type (type
, arg_types
);
4215 declarator
= declarator
->declarator
;
4217 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4218 the formal parameter list of this FUNCTION_TYPE to point to
4219 the FUNCTION_TYPE node itself. */
4223 for (link
= arg_info
->tags
;
4225 link
= TREE_CHAIN (link
))
4226 TYPE_CONTEXT (TREE_VALUE (link
)) = type
;
4232 /* Merge any constancy or volatility into the target type
4235 if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
4237 pedwarn ("ISO C forbids qualified function types");
4239 type
= c_build_qualified_type (type
, type_quals
);
4242 type
= build_pointer_type (type
);
4244 /* Process type qualifiers (such as const or volatile)
4245 that were given inside the `*'. */
4246 type_quals
= declarator
->u
.pointer_quals
;
4248 declarator
= declarator
->declarator
;
4256 /* Now TYPE has the actual type, apart from any qualifiers in
4259 /* Check the type and width of a bit-field. */
4261 check_bitfield_type_and_width (&type
, width
, orig_name
);
4263 /* Did array size calculations overflow? */
4265 if (TREE_CODE (type
) == ARRAY_TYPE
4266 && COMPLETE_TYPE_P (type
)
4267 && TREE_OVERFLOW (TYPE_SIZE (type
)))
4269 error ("size of array %qs is too large", name
);
4270 /* If we proceed with the array type as it is, we'll eventually
4271 crash in tree_low_cst(). */
4272 type
= error_mark_node
;
4275 /* If this is declaring a typedef name, return a TYPE_DECL. */
4277 if (storage_class
== csc_typedef
)
4280 if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
4282 pedwarn ("ISO C forbids qualified function types");
4284 type
= c_build_qualified_type (type
, type_quals
);
4285 decl
= build_decl (TYPE_DECL
, declarator
->u
.id
, type
);
4286 if (declspecs
->explicit_signed_p
)
4287 C_TYPEDEF_EXPLICITLY_SIGNED (decl
) = 1;
4288 decl_attributes (&decl
, returned_attrs
, 0);
4289 if (declspecs
->inline_p
)
4290 pedwarn ("%Jtypedef %qD declared %<inline%>", decl
, decl
);
4294 /* Detect the case of an array type of unspecified size
4295 which came, as such, direct from a typedef name.
4296 We must copy the type, so that each identifier gets
4297 a distinct type, so that each identifier's size can be
4298 controlled separately by its own initializer. */
4300 if (type
!= 0 && typedef_type
!= 0
4301 && TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
) == 0
4302 && TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (typedef_type
))
4304 type
= build_array_type (TREE_TYPE (type
), 0);
4306 C_TYPE_VARIABLE_SIZE (type
) = 1;
4309 /* If this is a type name (such as, in a cast or sizeof),
4310 compute the type and return it now. */
4312 if (decl_context
== TYPENAME
)
4314 /* Note that the grammar rejects storage classes in typenames
4316 gcc_assert (storage_class
== csc_none
&& !threadp
4317 && !declspecs
->inline_p
);
4318 if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
4320 pedwarn ("ISO C forbids const or volatile function types");
4322 type
= c_build_qualified_type (type
, type_quals
);
4323 decl_attributes (&type
, returned_attrs
, 0);
4327 /* Aside from typedefs and type names (handle above),
4328 `void' at top level (not within pointer)
4329 is allowed only in public variables.
4330 We don't complain about parms either, but that is because
4331 a better error message can be made later. */
4333 if (VOID_TYPE_P (type
) && decl_context
!= PARM
4334 && !((decl_context
!= FIELD
&& TREE_CODE (type
) != FUNCTION_TYPE
)
4335 && (storage_class
== csc_extern
4336 || (current_scope
== file_scope
4337 && !(storage_class
== csc_static
4338 || storage_class
== csc_register
)))))
4340 error ("variable or field %qs declared void", name
);
4341 type
= integer_type_node
;
4344 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4345 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4350 if (decl_context
== PARM
)
4352 tree type_as_written
;
4355 /* A parameter declared as an array of T is really a pointer to T.
4356 One declared as a function is really a pointer to a function. */
4358 if (TREE_CODE (type
) == ARRAY_TYPE
)
4360 /* Transfer const-ness of array into that of type pointed to. */
4361 type
= TREE_TYPE (type
);
4363 type
= c_build_qualified_type (type
, type_quals
);
4364 type
= build_pointer_type (type
);
4365 type_quals
= array_ptr_quals
;
4367 /* We don't yet implement attributes in this context. */
4368 if (array_ptr_attrs
!= NULL_TREE
)
4369 warning (0, "attributes in parameter array declarator ignored");
4373 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
4375 if (pedantic
&& type_quals
)
4376 pedwarn ("ISO C forbids qualified function types");
4378 type
= c_build_qualified_type (type
, type_quals
);
4379 type
= build_pointer_type (type
);
4380 type_quals
= TYPE_UNQUALIFIED
;
4382 else if (type_quals
)
4383 type
= c_build_qualified_type (type
, type_quals
);
4385 type_as_written
= type
;
4387 decl
= build_decl (PARM_DECL
, declarator
->u
.id
, type
);
4389 C_DECL_VARIABLE_SIZE (decl
) = 1;
4391 /* Compute the type actually passed in the parmlist,
4392 for the case where there is no prototype.
4393 (For example, shorts and chars are passed as ints.)
4394 When there is a prototype, this is overridden later. */
4396 if (type
== error_mark_node
)
4397 promoted_type
= type
;
4399 promoted_type
= c_type_promotes_to (type
);
4401 DECL_ARG_TYPE (decl
) = promoted_type
;
4402 DECL_ARG_TYPE_AS_WRITTEN (decl
) = type_as_written
;
4403 if (declspecs
->inline_p
)
4404 pedwarn ("%Jparameter %qD declared %<inline%>", decl
, decl
);
4406 else if (decl_context
== FIELD
)
4408 /* Note that the grammar rejects storage classes in typenames
4410 gcc_assert (storage_class
== csc_none
&& !threadp
4411 && !declspecs
->inline_p
);
4413 /* Structure field. It may not be a function. */
4415 if (TREE_CODE (type
) == FUNCTION_TYPE
)
4417 error ("field %qs declared as a function", name
);
4418 type
= build_pointer_type (type
);
4420 else if (TREE_CODE (type
) != ERROR_MARK
4421 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type
))
4423 error ("field %qs has incomplete type", name
);
4424 type
= error_mark_node
;
4426 type
= c_build_qualified_type (type
, type_quals
);
4427 decl
= build_decl (FIELD_DECL
, declarator
->u
.id
, type
);
4428 DECL_NONADDRESSABLE_P (decl
) = bitfield
;
4431 C_DECL_VARIABLE_SIZE (decl
) = 1;
4433 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
4435 if (storage_class
== csc_register
|| threadp
)
4437 error ("invalid storage class for function %qs", name
);
4439 else if (current_scope
!= file_scope
)
4441 /* Function declaration not at file scope. Storage
4442 classes other than `extern' are not allowed, C99
4443 6.7.1p5, and `extern' makes no difference. However,
4444 GCC allows 'auto', perhaps with 'inline', to support
4445 nested functions. */
4446 if (storage_class
== csc_auto
)
4449 pedwarn ("invalid storage class for function %qs", name
);
4451 else if (storage_class
== csc_static
)
4453 error ("invalid storage class for function %qs", name
);
4455 storage_class
= declspecs
->storage_class
= csc_none
;
4461 decl
= build_decl (FUNCTION_DECL
, declarator
->u
.id
, type
);
4462 decl
= build_decl_attribute_variant (decl
, decl_attr
);
4464 DECL_LANG_SPECIFIC (decl
) = GGC_CNEW (struct lang_decl
);
4466 if (pedantic
&& type_quals
&& !DECL_IN_SYSTEM_HEADER (decl
))
4467 pedwarn ("ISO C forbids qualified function types");
4469 /* GNU C interprets a volatile-qualified function type to indicate
4470 that the function does not return. */
4471 if ((type_quals
& TYPE_QUAL_VOLATILE
)
4472 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl
))))
4473 warning (0, "%<noreturn%> function returns non-void value");
4475 /* Every function declaration is an external reference
4476 (DECL_EXTERNAL) except for those which are not at file
4477 scope and are explicitly declared "auto". This is
4478 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4479 GCC to signify a forward declaration of a nested function. */
4480 if (storage_class
== csc_auto
&& current_scope
!= file_scope
)
4481 DECL_EXTERNAL (decl
) = 0;
4483 DECL_EXTERNAL (decl
) = 1;
4485 /* Record absence of global scope for `static' or `auto'. */
4487 = !(storage_class
== csc_static
|| storage_class
== csc_auto
);
4489 /* For a function definition, record the argument information
4490 block where store_parm_decls will look for it. */
4492 current_function_arg_info
= arg_info
;
4494 if (declspecs
->default_int_p
)
4495 C_FUNCTION_IMPLICIT_INT (decl
) = 1;
4497 /* Record presence of `inline', if it is reasonable. */
4498 if (flag_hosted
&& MAIN_NAME_P (declarator
->u
.id
))
4500 if (declspecs
->inline_p
)
4501 pedwarn ("cannot inline function %<main%>");
4503 else if (declspecs
->inline_p
)
4505 /* Record that the function is declared `inline'. */
4506 DECL_DECLARED_INLINE_P (decl
) = 1;
4508 /* Do not mark bare declarations as DECL_INLINE. Doing so
4509 in the presence of multiple declarations can result in
4510 the abstract origin pointing between the declarations,
4511 which will confuse dwarf2out. */
4514 DECL_INLINE (decl
) = 1;
4515 if (storage_class
== csc_extern
)
4516 current_extern_inline
= 1;
4519 /* If -finline-functions, assume it can be inlined. This does
4520 two things: let the function be deferred until it is actually
4521 needed, and let dwarf2 know that the function is inlinable. */
4522 else if (flag_inline_trees
== 2 && initialized
)
4523 DECL_INLINE (decl
) = 1;
4527 /* It's a variable. */
4528 /* An uninitialized decl with `extern' is a reference. */
4529 int extern_ref
= !initialized
&& storage_class
== csc_extern
;
4531 type
= c_build_qualified_type (type
, type_quals
);
4533 /* C99 6.2.2p7: It is invalid (compile-time undefined
4534 behavior) to create an 'extern' declaration for a
4535 variable if there is a global declaration that is
4536 'static' and the global declaration is not visible.
4537 (If the static declaration _is_ currently visible,
4538 the 'extern' declaration is taken to refer to that decl.) */
4539 if (extern_ref
&& current_scope
!= file_scope
)
4541 tree global_decl
= identifier_global_value (declarator
->u
.id
);
4542 tree visible_decl
= lookup_name (declarator
->u
.id
);
4545 && global_decl
!= visible_decl
4546 && TREE_CODE (global_decl
) == VAR_DECL
4547 && !TREE_PUBLIC (global_decl
))
4548 error ("variable previously declared %<static%> redeclared "
4552 decl
= build_decl (VAR_DECL
, declarator
->u
.id
, type
);
4553 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
4555 C_DECL_VARIABLE_SIZE (decl
) = 1;
4557 if (declspecs
->inline_p
)
4558 pedwarn ("%Jvariable %qD declared %<inline%>", decl
, decl
);
4560 /* At file scope, an initialized extern declaration may follow
4561 a static declaration. In that case, DECL_EXTERNAL will be
4562 reset later in start_decl. */
4563 DECL_EXTERNAL (decl
) = (storage_class
== csc_extern
);
4565 /* At file scope, the presence of a `static' or `register' storage
4566 class specifier, or the absence of all storage class specifiers
4567 makes this declaration a definition (perhaps tentative). Also,
4568 the absence of both `static' and `register' makes it public. */
4569 if (current_scope
== file_scope
)
4571 TREE_PUBLIC (decl
) = !(storage_class
== csc_static
4572 || storage_class
== csc_register
);
4573 TREE_STATIC (decl
) = !extern_ref
;
4575 /* Not at file scope, only `static' makes a static definition. */
4578 TREE_STATIC (decl
) = (storage_class
== csc_static
);
4579 TREE_PUBLIC (decl
) = extern_ref
;
4584 if (targetm
.have_tls
)
4585 DECL_THREAD_LOCAL (decl
) = 1;
4587 /* A mere warning is sure to result in improper semantics
4588 at runtime. Don't bother to allow this to compile. */
4589 error ("thread-local storage not supported for this target");
4593 /* Record `register' declaration for warnings on &
4594 and in case doing stupid register allocation. */
4596 if (storage_class
== csc_register
)
4598 C_DECL_REGISTER (decl
) = 1;
4599 DECL_REGISTER (decl
) = 1;
4602 /* Record constancy and volatility. */
4603 c_apply_type_quals_to_decl (type_quals
, decl
);
4605 /* If a type has volatile components, it should be stored in memory.
4606 Otherwise, the fact that those components are volatile
4607 will be ignored, and would even crash the compiler. */
4608 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl
)))
4610 /* It is not an error for a structure with volatile fields to
4611 be declared register, but reset DECL_REGISTER since it
4612 cannot actually go in a register. */
4613 int was_reg
= C_DECL_REGISTER (decl
);
4614 C_DECL_REGISTER (decl
) = 0;
4615 DECL_REGISTER (decl
) = 0;
4616 c_mark_addressable (decl
);
4617 C_DECL_REGISTER (decl
) = was_reg
;
4620 /* This is the earliest point at which we might know the assembler
4621 name of a variable. Thus, if it's known before this, die horribly. */
4622 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl
));
4624 decl_attributes (&decl
, returned_attrs
, 0);
4630 /* Decode the parameter-list info for a function type or function definition.
4631 The argument is the value returned by `get_parm_info' (or made in parse.y
4632 if there is an identifier list instead of a parameter decl list).
4633 These two functions are separate because when a function returns
4634 or receives functions then each is called multiple times but the order
4635 of calls is different. The last call to `grokparms' is always the one
4636 that contains the formal parameter names of a function definition.
4638 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4640 FUNCDEF_FLAG is true for a function definition, false for
4641 a mere declaration. A nonempty identifier-list gets an error message
4642 when FUNCDEF_FLAG is false. */
4645 grokparms (struct c_arg_info
*arg_info
, bool funcdef_flag
)
4647 tree arg_types
= arg_info
->types
;
4649 if (warn_strict_prototypes
&& arg_types
== 0 && !funcdef_flag
4650 && !in_system_header
)
4651 warning (0, "function declaration isn%'t a prototype");
4653 if (arg_types
== error_mark_node
)
4654 return 0; /* don't set TYPE_ARG_TYPES in this case */
4656 else if (arg_types
&& TREE_CODE (TREE_VALUE (arg_types
)) == IDENTIFIER_NODE
)
4659 pedwarn ("parameter names (without types) in function declaration");
4661 arg_info
->parms
= arg_info
->types
;
4662 arg_info
->types
= 0;
4667 tree parm
, type
, typelt
;
4668 unsigned int parmno
;
4670 /* If there is a parameter of incomplete type in a definition,
4671 this is an error. In a declaration this is valid, and a
4672 struct or union type may be completed later, before any calls
4673 or definition of the function. In the case where the tag was
4674 first declared within the parameter list, a warning has
4675 already been given. If a parameter has void type, then
4676 however the function cannot be defined or called, so
4679 for (parm
= arg_info
->parms
, typelt
= arg_types
, parmno
= 1;
4681 parm
= TREE_CHAIN (parm
), typelt
= TREE_CHAIN (typelt
), parmno
++)
4683 type
= TREE_VALUE (typelt
);
4684 if (type
== error_mark_node
)
4687 if (!COMPLETE_TYPE_P (type
))
4691 if (DECL_NAME (parm
))
4692 error ("%Jparameter %u (%qD) has incomplete type",
4693 parm
, parmno
, parm
);
4695 error ("%Jparameter %u has incomplete type",
4698 TREE_VALUE (typelt
) = error_mark_node
;
4699 TREE_TYPE (parm
) = error_mark_node
;
4701 else if (VOID_TYPE_P (type
))
4703 if (DECL_NAME (parm
))
4704 warning (0, "%Jparameter %u (%qD) has void type",
4705 parm
, parmno
, parm
);
4707 warning (0, "%Jparameter %u has void type",
4716 /* Take apart the current scope and return a c_arg_info structure with
4717 info on a parameter list just parsed.
4719 This structure is later fed to 'grokparms' and 'store_parm_decls'.
4721 ELLIPSIS being true means the argument list ended in '...' so don't
4722 append a sentinel (void_list_node) to the end of the type-list. */
4725 get_parm_info (bool ellipsis
)
4727 struct c_binding
*b
= current_scope
->bindings
;
4728 struct c_arg_info
*arg_info
= XOBNEW (&parser_obstack
,
4735 static bool explained_incomplete_types
= false;
4736 bool gave_void_only_once_err
= false;
4738 arg_info
->parms
= 0;
4740 arg_info
->types
= 0;
4741 arg_info
->others
= 0;
4743 /* The bindings in this scope must not get put into a block.
4744 We will take care of deleting the binding nodes. */
4745 current_scope
->bindings
= 0;
4747 /* This function is only called if there was *something* on the
4751 /* A parameter list consisting solely of 'void' indicates that the
4752 function takes no arguments. But if the 'void' is qualified
4753 (by 'const' or 'volatile'), or has a storage class specifier
4754 ('register'), then the behavior is undefined; issue an error.
4755 Typedefs for 'void' are OK (see DR#157). */
4756 if (b
->prev
== 0 /* one binding */
4757 && TREE_CODE (b
->decl
) == PARM_DECL
/* which is a parameter */
4758 && !DECL_NAME (b
->decl
) /* anonymous */
4759 && VOID_TYPE_P (TREE_TYPE (b
->decl
))) /* of void type */
4761 if (TREE_THIS_VOLATILE (b
->decl
)
4762 || TREE_READONLY (b
->decl
)
4763 || C_DECL_REGISTER (b
->decl
))
4764 error ("%<void%> as only parameter may not be qualified");
4766 /* There cannot be an ellipsis. */
4768 error ("%<void%> must be the only parameter");
4770 arg_info
->types
= void_list_node
;
4775 types
= void_list_node
;
4777 /* Break up the bindings list into parms, tags, types, and others;
4778 apply sanity checks; purge the name-to-decl bindings. */
4781 tree decl
= b
->decl
;
4782 tree type
= TREE_TYPE (decl
);
4783 const char *keyword
;
4785 switch (TREE_CODE (decl
))
4790 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
4791 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
4794 /* Check for forward decls that never got their actual decl. */
4795 if (TREE_ASM_WRITTEN (decl
))
4796 error ("%Jparameter %qD has just a forward declaration",
4798 /* Check for (..., void, ...) and issue an error. */
4799 else if (VOID_TYPE_P (type
) && !DECL_NAME (decl
))
4801 if (!gave_void_only_once_err
)
4803 error ("%<void%> must be the only parameter");
4804 gave_void_only_once_err
= true;
4809 /* Valid parameter, add it to the list. */
4810 TREE_CHAIN (decl
) = parms
;
4813 /* Since there is a prototype, args are passed in their
4814 declared types. The back end may override this later. */
4815 DECL_ARG_TYPE (decl
) = type
;
4816 types
= tree_cons (0, type
, types
);
4820 case ENUMERAL_TYPE
: keyword
= "enum"; goto tag
;
4821 case UNION_TYPE
: keyword
= "union"; goto tag
;
4822 case RECORD_TYPE
: keyword
= "struct"; goto tag
;
4824 /* Types may not have tag-names, in which case the type
4825 appears in the bindings list with b->id NULL. */
4828 gcc_assert (I_TAG_BINDING (b
->id
) == b
);
4829 I_TAG_BINDING (b
->id
) = b
->shadowed
;
4832 /* Warn about any struct, union or enum tags defined in a
4833 parameter list. The scope of such types is limited to
4834 the parameter list, which is rarely if ever desirable
4835 (it's impossible to call such a function with type-
4836 correct arguments). An anonymous union parm type is
4837 meaningful as a GNU extension, so don't warn for that. */
4838 if (TREE_CODE (decl
) != UNION_TYPE
|| b
->id
!= 0)
4841 /* The %s will be one of 'struct', 'union', or 'enum'. */
4842 warning (0, "%<%s %E%> declared inside parameter list",
4845 /* The %s will be one of 'struct', 'union', or 'enum'. */
4846 warning (0, "anonymous %s declared inside parameter list",
4849 if (!explained_incomplete_types
)
4851 warning (0, "its scope is only this definition or declaration,"
4852 " which is probably not what you want");
4853 explained_incomplete_types
= true;
4857 tags
= tree_cons (b
->id
, decl
, tags
);
4863 /* CONST_DECLs appear here when we have an embedded enum,
4864 and TYPE_DECLs appear here when we have an embedded struct
4865 or union. No warnings for this - we already warned about the
4866 type itself. FUNCTION_DECLs appear when there is an implicit
4867 function declaration in the parameter list. */
4869 TREE_CHAIN (decl
) = others
;
4874 /* error_mark_node appears here when we have an undeclared
4875 variable. Just throw it away. */
4878 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
4879 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
4883 /* Other things that might be encountered. */
4890 b
= free_binding_and_advance (b
);
4893 arg_info
->parms
= parms
;
4894 arg_info
->tags
= tags
;
4895 arg_info
->types
= types
;
4896 arg_info
->others
= others
;
4900 /* Get the struct, enum or union (CODE says which) with tag NAME.
4901 Define the tag as a forward-reference if it is not defined.
4902 Return a c_typespec structure for the type specifier. */
4905 parser_xref_tag (enum tree_code code
, tree name
)
4907 struct c_typespec ret
;
4908 /* If a cross reference is requested, look up the type
4909 already defined for this tag and return it. */
4911 tree ref
= lookup_tag (code
, name
, 0);
4912 /* If this is the right type of tag, return what we found.
4913 (This reference will be shadowed by shadow_tag later if appropriate.)
4914 If this is the wrong type of tag, do not return it. If it was the
4915 wrong type in the same scope, we will have had an error
4916 message already; if in a different scope and declaring
4917 a name, pending_xref_error will give an error message; but if in a
4918 different scope and not declaring a name, this tag should
4919 shadow the previous declaration of a different type of tag, and
4920 this would not work properly if we return the reference found.
4921 (For example, with "struct foo" in an outer scope, "union foo;"
4922 must shadow that tag with a new one of union type.) */
4923 ret
.kind
= (ref
? ctsk_tagref
: ctsk_tagfirstref
);
4924 if (ref
&& TREE_CODE (ref
) == code
)
4930 /* If no such tag is yet defined, create a forward-reference node
4931 and record it as the "definition".
4932 When a real declaration of this type is found,
4933 the forward-reference will be altered into a real type. */
4935 ref
= make_node (code
);
4936 if (code
== ENUMERAL_TYPE
)
4938 /* Give the type a default layout like unsigned int
4939 to avoid crashing if it does not get defined. */
4940 TYPE_MODE (ref
) = TYPE_MODE (unsigned_type_node
);
4941 TYPE_ALIGN (ref
) = TYPE_ALIGN (unsigned_type_node
);
4942 TYPE_USER_ALIGN (ref
) = 0;
4943 TYPE_UNSIGNED (ref
) = 1;
4944 TYPE_PRECISION (ref
) = TYPE_PRECISION (unsigned_type_node
);
4945 TYPE_MIN_VALUE (ref
) = TYPE_MIN_VALUE (unsigned_type_node
);
4946 TYPE_MAX_VALUE (ref
) = TYPE_MAX_VALUE (unsigned_type_node
);
4949 pushtag (name
, ref
);
4955 /* Get the struct, enum or union (CODE says which) with tag NAME.
4956 Define the tag as a forward-reference if it is not defined.
4957 Return a tree for the type. */
4960 xref_tag (enum tree_code code
, tree name
)
4962 return parser_xref_tag (code
, name
).spec
;
4965 /* Make sure that the tag NAME is defined *in the current scope*
4966 at least as a forward reference.
4967 CODE says which kind of tag NAME ought to be. */
4970 start_struct (enum tree_code code
, tree name
)
4972 /* If there is already a tag defined at this scope
4973 (as a forward reference), just return it. */
4978 ref
= lookup_tag (code
, name
, 1);
4979 if (ref
&& TREE_CODE (ref
) == code
)
4981 if (TYPE_SIZE (ref
))
4983 if (code
== UNION_TYPE
)
4984 error ("redefinition of %<union %E%>", name
);
4986 error ("redefinition of %<struct %E%>", name
);
4988 else if (C_TYPE_BEING_DEFINED (ref
))
4990 if (code
== UNION_TYPE
)
4991 error ("nested redefinition of %<union %E%>", name
);
4993 error ("nested redefinition of %<struct %E%>", name
);
4998 /* Otherwise create a forward-reference just so the tag is in scope. */
5000 ref
= make_node (code
);
5001 pushtag (name
, ref
);
5004 C_TYPE_BEING_DEFINED (ref
) = 1;
5005 TYPE_PACKED (ref
) = flag_pack_struct
;
5009 /* Process the specs, declarator and width (NULL if omitted)
5010 of a structure component, returning a FIELD_DECL node.
5011 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5013 This is done during the parsing of the struct declaration.
5014 The FIELD_DECL nodes are chained together and the lot of them
5015 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5018 grokfield (struct c_declarator
*declarator
, struct c_declspecs
*declspecs
,
5023 if (declarator
->kind
== cdk_id
&& declarator
->u
.id
== NULL_TREE
5024 && width
== NULL_TREE
)
5026 /* This is an unnamed decl.
5028 If we have something of the form "union { list } ;" then this
5029 is the anonymous union extension. Similarly for struct.
5031 If this is something of the form "struct foo;", then
5032 If MS extensions are enabled, this is handled as an
5034 Otherwise this is a forward declaration of a structure tag.
5036 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5037 If MS extensions are enabled and foo names a structure, then
5038 again this is an anonymous struct.
5039 Otherwise this is an error.
5041 Oh what a horrid tangled web we weave. I wonder if MS consciously
5042 took this from Plan 9 or if it was an accident of implementation
5043 that took root before someone noticed the bug... */
5045 tree type
= declspecs
->type
;
5046 bool type_ok
= (TREE_CODE (type
) == RECORD_TYPE
5047 || TREE_CODE (type
) == UNION_TYPE
);
5051 && (flag_ms_extensions
|| !declspecs
->typedef_p
))
5053 if (flag_ms_extensions
)
5057 else if (TYPE_NAME (type
) == NULL
)
5064 pedwarn ("declaration does not declare anything");
5068 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5071 value
= grokdeclarator (declarator
, declspecs
, FIELD
, false,
5072 width
? &width
: NULL
);
5074 finish_decl (value
, NULL_TREE
, NULL_TREE
);
5075 DECL_INITIAL (value
) = width
;
5080 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5081 the list such that this does not present a problem later. */
5084 detect_field_duplicates (tree fieldlist
)
5089 /* First, see if there are more than "a few" fields.
5090 This is trivially true if there are zero or one fields. */
5093 x
= TREE_CHAIN (fieldlist
);
5099 } while (timeout
> 0 && x
);
5101 /* If there were "few" fields, avoid the overhead of allocating
5102 a hash table. Instead just do the nested traversal thing. */
5105 for (x
= TREE_CHAIN (fieldlist
); x
; x
= TREE_CHAIN (x
))
5108 for (y
= fieldlist
; y
!= x
; y
= TREE_CHAIN (y
))
5109 if (DECL_NAME (y
) == DECL_NAME (x
))
5111 error ("%Jduplicate member %qD", x
, x
);
5112 DECL_NAME (x
) = NULL_TREE
;
5118 htab_t htab
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
, NULL
);
5121 for (x
= fieldlist
; x
; x
= TREE_CHAIN (x
))
5122 if ((y
= DECL_NAME (x
)) != 0)
5124 slot
= htab_find_slot (htab
, y
, INSERT
);
5127 error ("%Jduplicate member %qD", x
, x
);
5128 DECL_NAME (x
) = NULL_TREE
;
5137 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5138 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5139 ATTRIBUTES are attributes to be applied to the structure. */
5142 finish_struct (tree t
, tree fieldlist
, tree attributes
)
5145 bool toplevel
= file_scope
== current_scope
;
5146 int saw_named_field
;
5148 /* If this type was previously laid out as a forward reference,
5149 make sure we lay it out again. */
5153 decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
5157 for (x
= fieldlist
; x
; x
= TREE_CHAIN (x
))
5158 if (DECL_NAME (x
) != 0)
5163 if (TREE_CODE (t
) == UNION_TYPE
)
5166 pedwarn ("union has no named members");
5168 pedwarn ("union has no members");
5173 pedwarn ("struct has no named members");
5175 pedwarn ("struct has no members");
5180 /* Install struct as DECL_CONTEXT of each field decl.
5181 Also process specified field sizes, found in the DECL_INITIAL,
5182 storing 0 there after the type has been changed to precision equal
5183 to its width, rather than the precision of the specified standard
5184 type. (Correct layout requires the original type to have been preserved
5187 saw_named_field
= 0;
5188 for (x
= fieldlist
; x
; x
= TREE_CHAIN (x
))
5190 DECL_CONTEXT (x
) = t
;
5191 DECL_PACKED (x
) |= TYPE_PACKED (t
);
5193 /* If any field is const, the structure type is pseudo-const. */
5194 if (TREE_READONLY (x
))
5195 C_TYPE_FIELDS_READONLY (t
) = 1;
5198 /* A field that is pseudo-const makes the structure likewise. */
5199 tree t1
= TREE_TYPE (x
);
5200 while (TREE_CODE (t1
) == ARRAY_TYPE
)
5201 t1
= TREE_TYPE (t1
);
5202 if ((TREE_CODE (t1
) == RECORD_TYPE
|| TREE_CODE (t1
) == UNION_TYPE
)
5203 && C_TYPE_FIELDS_READONLY (t1
))
5204 C_TYPE_FIELDS_READONLY (t
) = 1;
5207 /* Any field that is volatile means variables of this type must be
5208 treated in some ways as volatile. */
5209 if (TREE_THIS_VOLATILE (x
))
5210 C_TYPE_FIELDS_VOLATILE (t
) = 1;
5212 /* Any field of nominal variable size implies structure is too. */
5213 if (C_DECL_VARIABLE_SIZE (x
))
5214 C_TYPE_VARIABLE_SIZE (t
) = 1;
5216 if (DECL_INITIAL (x
))
5218 unsigned HOST_WIDE_INT width
= tree_low_cst (DECL_INITIAL (x
), 1);
5219 DECL_SIZE (x
) = bitsize_int (width
);
5220 DECL_BIT_FIELD (x
) = 1;
5221 SET_DECL_C_BIT_FIELD (x
);
5224 /* Detect flexible array member in an invalid context. */
5225 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
5226 && TYPE_SIZE (TREE_TYPE (x
)) == NULL_TREE
5227 && TYPE_DOMAIN (TREE_TYPE (x
)) != NULL_TREE
5228 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x
))) == NULL_TREE
)
5230 if (TREE_CODE (t
) == UNION_TYPE
)
5232 error ("%Jflexible array member in union", x
);
5233 TREE_TYPE (x
) = error_mark_node
;
5235 else if (TREE_CHAIN (x
) != NULL_TREE
)
5237 error ("%Jflexible array member not at end of struct", x
);
5238 TREE_TYPE (x
) = error_mark_node
;
5240 else if (!saw_named_field
)
5242 error ("%Jflexible array member in otherwise empty struct", x
);
5243 TREE_TYPE (x
) = error_mark_node
;
5247 if (pedantic
&& !in_system_header
&& TREE_CODE (t
) == RECORD_TYPE
5248 && flexible_array_type_p (TREE_TYPE (x
)))
5249 pedwarn ("%Jinvalid use of structure with flexible array member", x
);
5252 saw_named_field
= 1;
5255 detect_field_duplicates (fieldlist
);
5257 /* Now we have the nearly final fieldlist. Record it,
5258 then lay out the structure or union (including the fields). */
5260 TYPE_FIELDS (t
) = fieldlist
;
5264 /* Give bit-fields their proper types. */
5266 tree
*fieldlistp
= &fieldlist
;
5268 if (TREE_CODE (*fieldlistp
) == FIELD_DECL
&& DECL_INITIAL (*fieldlistp
)
5269 && TREE_TYPE (*fieldlistp
) != error_mark_node
)
5271 unsigned HOST_WIDE_INT width
5272 = tree_low_cst (DECL_INITIAL (*fieldlistp
), 1);
5273 tree type
= TREE_TYPE (*fieldlistp
);
5274 if (width
!= TYPE_PRECISION (type
))
5276 TREE_TYPE (*fieldlistp
)
5277 = build_nonstandard_integer_type (width
, TYPE_UNSIGNED (type
));
5278 DECL_MODE (*fieldlistp
) = TYPE_MODE (TREE_TYPE (*fieldlistp
));
5280 DECL_INITIAL (*fieldlistp
) = 0;
5283 fieldlistp
= &TREE_CHAIN (*fieldlistp
);
5286 /* Now we have the truly final field list.
5287 Store it in this type and in the variants. */
5289 TYPE_FIELDS (t
) = fieldlist
;
5291 /* If there are lots of fields, sort so we can look through them fast.
5292 We arbitrarily consider 16 or more elts to be "a lot". */
5297 for (x
= fieldlist
; x
; x
= TREE_CHAIN (x
))
5299 if (len
> 15 || DECL_NAME (x
) == NULL
)
5307 struct lang_type
*space
;
5308 struct sorted_fields_type
*space2
;
5310 len
+= list_length (x
);
5312 /* Use the same allocation policy here that make_node uses, to
5313 ensure that this lives as long as the rest of the struct decl.
5314 All decls in an inline function need to be saved. */
5316 space
= GGC_CNEW (struct lang_type
);
5317 space2
= GGC_NEWVAR (struct sorted_fields_type
,
5318 sizeof (struct sorted_fields_type
) + len
* sizeof (tree
));
5322 field_array
= &space2
->elts
[0];
5323 for (x
= fieldlist
; x
; x
= TREE_CHAIN (x
))
5325 field_array
[len
++] = x
;
5327 /* If there is anonymous struct or union, break out of the loop. */
5328 if (DECL_NAME (x
) == NULL
)
5331 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5334 TYPE_LANG_SPECIFIC (t
) = space
;
5335 TYPE_LANG_SPECIFIC (t
)->s
->len
= len
;
5336 field_array
= TYPE_LANG_SPECIFIC (t
)->s
->elts
;
5337 qsort (field_array
, len
, sizeof (tree
), field_decl_cmp
);
5342 for (x
= TYPE_MAIN_VARIANT (t
); x
; x
= TYPE_NEXT_VARIANT (x
))
5344 TYPE_FIELDS (x
) = TYPE_FIELDS (t
);
5345 TYPE_LANG_SPECIFIC (x
) = TYPE_LANG_SPECIFIC (t
);
5346 TYPE_ALIGN (x
) = TYPE_ALIGN (t
);
5347 TYPE_USER_ALIGN (x
) = TYPE_USER_ALIGN (t
);
5350 /* If this was supposed to be a transparent union, but we can't
5351 make it one, warn and turn off the flag. */
5352 if (TREE_CODE (t
) == UNION_TYPE
5353 && TYPE_TRANSPARENT_UNION (t
)
5354 && (!TYPE_FIELDS (t
) || TYPE_MODE (t
) != DECL_MODE (TYPE_FIELDS (t
))))
5356 TYPE_TRANSPARENT_UNION (t
) = 0;
5357 warning (0, "union cannot be made transparent");
5360 /* If this structure or union completes the type of any previous
5361 variable declaration, lay it out and output its rtl. */
5362 for (x
= C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t
));
5366 tree decl
= TREE_VALUE (x
);
5367 if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
5368 layout_array_type (TREE_TYPE (decl
));
5369 if (TREE_CODE (decl
) != TYPE_DECL
)
5371 layout_decl (decl
, 0);
5372 if (c_dialect_objc ())
5373 objc_check_decl (decl
);
5374 rest_of_decl_compilation (decl
, toplevel
, 0);
5379 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t
)) = 0;
5381 /* Finish debugging output for this type. */
5382 rest_of_type_compilation (t
, toplevel
);
5384 /* If we're inside a function proper, i.e. not file-scope and not still
5385 parsing parameters, then arrange for the size of a variable sized type
5387 if (cur_stmt_list
&& variably_modified_type_p (t
, NULL
))
5388 add_stmt (build_stmt (DECL_EXPR
, build_decl (TYPE_DECL
, NULL
, t
)));
5393 /* Lay out the type T, and its element type, and so on. */
5396 layout_array_type (tree t
)
5398 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
5399 layout_array_type (TREE_TYPE (t
));
5403 /* Begin compiling the definition of an enumeration type.
5404 NAME is its name (or null if anonymous).
5405 Returns the type object, as yet incomplete.
5406 Also records info about it so that build_enumerator
5407 may be used to declare the individual values as they are read. */
5410 start_enum (tree name
)
5414 /* If this is the real definition for a previous forward reference,
5415 fill in the contents in the same object that used to be the
5416 forward reference. */
5419 enumtype
= lookup_tag (ENUMERAL_TYPE
, name
, 1);
5421 if (enumtype
== 0 || TREE_CODE (enumtype
) != ENUMERAL_TYPE
)
5423 enumtype
= make_node (ENUMERAL_TYPE
);
5424 pushtag (name
, enumtype
);
5427 if (C_TYPE_BEING_DEFINED (enumtype
))
5428 error ("nested redefinition of %<enum %E%>", name
);
5430 C_TYPE_BEING_DEFINED (enumtype
) = 1;
5432 if (TYPE_VALUES (enumtype
) != 0)
5434 /* This enum is a named one that has been declared already. */
5435 error ("redeclaration of %<enum %E%>", name
);
5437 /* Completely replace its old definition.
5438 The old enumerators remain defined, however. */
5439 TYPE_VALUES (enumtype
) = 0;
5442 enum_next_value
= integer_zero_node
;
5445 if (flag_short_enums
)
5446 TYPE_PACKED (enumtype
) = 1;
5451 /* After processing and defining all the values of an enumeration type,
5452 install their decls in the enumeration type and finish it off.
5453 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5454 and ATTRIBUTES are the specified attributes.
5455 Returns ENUMTYPE. */
5458 finish_enum (tree enumtype
, tree values
, tree attributes
)
5461 tree minnode
= 0, maxnode
= 0;
5462 int precision
, unsign
;
5463 bool toplevel
= (file_scope
== current_scope
);
5464 struct lang_type
*lt
;
5466 decl_attributes (&enumtype
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
5468 /* Calculate the maximum value of any enumerator in this type. */
5470 if (values
== error_mark_node
)
5471 minnode
= maxnode
= integer_zero_node
;
5474 minnode
= maxnode
= TREE_VALUE (values
);
5475 for (pair
= TREE_CHAIN (values
); pair
; pair
= TREE_CHAIN (pair
))
5477 tree value
= TREE_VALUE (pair
);
5478 if (tree_int_cst_lt (maxnode
, value
))
5480 if (tree_int_cst_lt (value
, minnode
))
5485 /* Construct the final type of this enumeration. It is the same
5486 as one of the integral types - the narrowest one that fits, except
5487 that normally we only go as narrow as int - and signed iff any of
5488 the values are negative. */
5489 unsign
= (tree_int_cst_sgn (minnode
) >= 0);
5490 precision
= MAX (min_precision (minnode
, unsign
),
5491 min_precision (maxnode
, unsign
));
5493 if (TYPE_PACKED (enumtype
) || precision
> TYPE_PRECISION (integer_type_node
))
5495 tem
= c_common_type_for_size (precision
, unsign
);
5498 warning (0, "enumeration values exceed range of largest integer");
5499 tem
= long_long_integer_type_node
;
5503 tem
= unsign
? unsigned_type_node
: integer_type_node
;
5505 TYPE_MIN_VALUE (enumtype
) = TYPE_MIN_VALUE (tem
);
5506 TYPE_MAX_VALUE (enumtype
) = TYPE_MAX_VALUE (tem
);
5507 TYPE_UNSIGNED (enumtype
) = TYPE_UNSIGNED (tem
);
5508 TYPE_SIZE (enumtype
) = 0;
5510 /* If the precision of the type was specific with an attribute and it
5511 was too small, give an error. Otherwise, use it. */
5512 if (TYPE_PRECISION (enumtype
))
5514 if (precision
> TYPE_PRECISION (enumtype
))
5515 error ("specified mode too small for enumeral values");
5518 TYPE_PRECISION (enumtype
) = TYPE_PRECISION (tem
);
5520 layout_type (enumtype
);
5522 if (values
!= error_mark_node
)
5524 /* Change the type of the enumerators to be the enum type. We
5525 need to do this irrespective of the size of the enum, for
5526 proper type checking. Replace the DECL_INITIALs of the
5527 enumerators, and the value slots of the list, with copies
5528 that have the enum type; they cannot be modified in place
5529 because they may be shared (e.g. integer_zero_node) Finally,
5530 change the purpose slots to point to the names of the decls. */
5531 for (pair
= values
; pair
; pair
= TREE_CHAIN (pair
))
5533 tree enu
= TREE_PURPOSE (pair
);
5534 tree ini
= DECL_INITIAL (enu
);
5536 TREE_TYPE (enu
) = enumtype
;
5538 /* The ISO C Standard mandates enumerators to have type int,
5539 even though the underlying type of an enum type is
5540 unspecified. Here we convert any enumerators that fit in
5541 an int to type int, to avoid promotions to unsigned types
5542 when comparing integers with enumerators that fit in the
5543 int range. When -pedantic is given, build_enumerator()
5544 would have already taken care of those that don't fit. */
5545 if (int_fits_type_p (ini
, integer_type_node
))
5546 tem
= integer_type_node
;
5549 ini
= convert (tem
, ini
);
5551 DECL_INITIAL (enu
) = ini
;
5552 TREE_PURPOSE (pair
) = DECL_NAME (enu
);
5553 TREE_VALUE (pair
) = ini
;
5556 TYPE_VALUES (enumtype
) = values
;
5559 /* Record the min/max values so that we can warn about bit-field
5560 enumerations that are too small for the values. */
5561 lt
= GGC_CNEW (struct lang_type
);
5562 lt
->enum_min
= minnode
;
5563 lt
->enum_max
= maxnode
;
5564 TYPE_LANG_SPECIFIC (enumtype
) = lt
;
5566 /* Fix up all variant types of this enum type. */
5567 for (tem
= TYPE_MAIN_VARIANT (enumtype
); tem
; tem
= TYPE_NEXT_VARIANT (tem
))
5569 if (tem
== enumtype
)
5571 TYPE_VALUES (tem
) = TYPE_VALUES (enumtype
);
5572 TYPE_MIN_VALUE (tem
) = TYPE_MIN_VALUE (enumtype
);
5573 TYPE_MAX_VALUE (tem
) = TYPE_MAX_VALUE (enumtype
);
5574 TYPE_SIZE (tem
) = TYPE_SIZE (enumtype
);
5575 TYPE_SIZE_UNIT (tem
) = TYPE_SIZE_UNIT (enumtype
);
5576 TYPE_MODE (tem
) = TYPE_MODE (enumtype
);
5577 TYPE_PRECISION (tem
) = TYPE_PRECISION (enumtype
);
5578 TYPE_ALIGN (tem
) = TYPE_ALIGN (enumtype
);
5579 TYPE_USER_ALIGN (tem
) = TYPE_USER_ALIGN (enumtype
);
5580 TYPE_UNSIGNED (tem
) = TYPE_UNSIGNED (enumtype
);
5581 TYPE_LANG_SPECIFIC (tem
) = TYPE_LANG_SPECIFIC (enumtype
);
5584 /* Finish debugging output for this type. */
5585 rest_of_type_compilation (enumtype
, toplevel
);
5590 /* Build and install a CONST_DECL for one value of the
5591 current enumeration type (one that was begun with start_enum).
5592 Return a tree-list containing the CONST_DECL and its value.
5593 Assignment of sequential values by default is handled here. */
5596 build_enumerator (tree name
, tree value
)
5600 /* Validate and default VALUE. */
5604 /* Don't issue more errors for error_mark_node (i.e. an
5605 undeclared identifier) - just ignore the value expression. */
5606 if (value
== error_mark_node
)
5608 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value
))
5609 || TREE_CODE (value
) != INTEGER_CST
)
5611 error ("enumerator value for %qE is not an integer constant", name
);
5616 value
= default_conversion (value
);
5617 constant_expression_warning (value
);
5621 /* Default based on previous value. */
5622 /* It should no longer be possible to have NON_LVALUE_EXPR
5626 value
= enum_next_value
;
5628 error ("overflow in enumeration values");
5631 if (pedantic
&& !int_fits_type_p (value
, integer_type_node
))
5633 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5634 /* XXX This causes -pedantic to change the meaning of the program.
5635 Remove? -zw 2004-03-15 */
5636 value
= convert (integer_type_node
, value
);
5639 /* Set basis for default for next value. */
5640 enum_next_value
= build_binary_op (PLUS_EXPR
, value
, integer_one_node
, 0);
5641 enum_overflow
= tree_int_cst_lt (enum_next_value
, value
);
5643 /* Now create a declaration for the enum value name. */
5645 type
= TREE_TYPE (value
);
5646 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
5647 TYPE_PRECISION (integer_type_node
)),
5648 (TYPE_PRECISION (type
)
5649 >= TYPE_PRECISION (integer_type_node
)
5650 && TYPE_UNSIGNED (type
)));
5652 decl
= build_decl (CONST_DECL
, name
, type
);
5653 DECL_INITIAL (decl
) = convert (type
, value
);
5656 return tree_cons (decl
, value
, NULL_TREE
);
5660 /* Create the FUNCTION_DECL for a function definition.
5661 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5662 the declaration; they describe the function's name and the type it returns,
5663 but twisted together in a fashion that parallels the syntax of C.
5665 This function creates a binding context for the function body
5666 as well as setting up the FUNCTION_DECL in current_function_decl.
5668 Returns 1 on success. If the DECLARATOR is not suitable for a function
5669 (it defines a datum instead), we return 0, which tells
5670 yyparse to report a parse error. */
5673 start_function (struct c_declspecs
*declspecs
, struct c_declarator
*declarator
,
5676 tree decl1
, old_decl
;
5677 tree restype
, resdecl
;
5678 struct c_label_context_se
*nstack_se
;
5679 struct c_label_context_vm
*nstack_vm
;
5681 current_function_returns_value
= 0; /* Assume, until we see it does. */
5682 current_function_returns_null
= 0;
5683 current_function_returns_abnormally
= 0;
5684 warn_about_return_type
= 0;
5685 current_extern_inline
= 0;
5686 c_switch_stack
= NULL
;
5688 nstack_se
= XOBNEW (&parser_obstack
, struct c_label_context_se
);
5689 nstack_se
->labels_def
= NULL
;
5690 nstack_se
->labels_used
= NULL
;
5691 nstack_se
->next
= label_context_stack_se
;
5692 label_context_stack_se
= nstack_se
;
5694 nstack_vm
= XOBNEW (&parser_obstack
, struct c_label_context_vm
);
5695 nstack_vm
->labels_def
= NULL
;
5696 nstack_vm
->labels_used
= NULL
;
5697 nstack_vm
->scope
= 0;
5698 nstack_vm
->next
= label_context_stack_vm
;
5699 label_context_stack_vm
= nstack_vm
;
5701 /* Indicate no valid break/continue context by setting these variables
5702 to some non-null, non-label value. We'll notice and emit the proper
5703 error message in c_finish_bc_stmt. */
5704 c_break_label
= c_cont_label
= size_zero_node
;
5706 decl1
= grokdeclarator (declarator
, declspecs
, FUNCDEF
, true, NULL
);
5708 /* If the declarator is not suitable for a function definition,
5709 cause a syntax error. */
5713 decl_attributes (&decl1
, attributes
, 0);
5715 if (DECL_DECLARED_INLINE_P (decl1
)
5716 && DECL_UNINLINABLE (decl1
)
5717 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1
)))
5718 warning (0, "%Jinline function %qD given attribute noinline", decl1
, decl1
);
5720 announce_function (decl1
);
5722 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1
))))
5724 error ("return type is an incomplete type");
5725 /* Make it return void instead. */
5727 = build_function_type (void_type_node
,
5728 TYPE_ARG_TYPES (TREE_TYPE (decl1
)));
5731 if (warn_about_return_type
)
5732 pedwarn_c99 ("return type defaults to %<int%>");
5734 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5735 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5736 DECL_INITIAL (decl1
) = error_mark_node
;
5738 /* If this definition isn't a prototype and we had a prototype declaration
5739 before, copy the arg type info from that prototype. */
5740 old_decl
= lookup_name_in_scope (DECL_NAME (decl1
), current_scope
);
5741 current_function_prototype_locus
= UNKNOWN_LOCATION
;
5742 current_function_prototype_built_in
= false;
5743 current_function_prototype_arg_types
= NULL_TREE
;
5744 if (TYPE_ARG_TYPES (TREE_TYPE (decl1
)) == 0)
5746 if (old_decl
!= 0 && TREE_CODE (TREE_TYPE (old_decl
)) == FUNCTION_TYPE
5747 && comptypes (TREE_TYPE (TREE_TYPE (decl1
)),
5748 TREE_TYPE (TREE_TYPE (old_decl
))))
5750 TREE_TYPE (decl1
) = composite_type (TREE_TYPE (old_decl
),
5752 current_function_prototype_locus
= DECL_SOURCE_LOCATION (old_decl
);
5753 current_function_prototype_built_in
5754 = C_DECL_BUILTIN_PROTOTYPE (old_decl
);
5755 current_function_prototype_arg_types
5756 = TYPE_ARG_TYPES (TREE_TYPE (decl1
));
5758 if (TREE_PUBLIC (decl1
))
5760 /* If there is an external prototype declaration of this
5761 function, record its location but do not copy information
5762 to this decl. This may be an invisible declaration
5763 (built-in or in a scope which has finished) or simply
5764 have more refined argument types than any declaration
5766 struct c_binding
*b
;
5767 for (b
= I_SYMBOL_BINDING (DECL_NAME (decl1
)); b
; b
= b
->shadowed
)
5768 if (B_IN_SCOPE (b
, external_scope
))
5772 tree ext_decl
, ext_type
;
5774 ext_type
= b
->type
? b
->type
: TREE_TYPE (ext_decl
);
5775 if (TREE_CODE (ext_type
) == FUNCTION_TYPE
5776 && comptypes (TREE_TYPE (TREE_TYPE (decl1
)),
5777 TREE_TYPE (ext_type
)))
5779 current_function_prototype_locus
5780 = DECL_SOURCE_LOCATION (ext_decl
);
5781 current_function_prototype_built_in
5782 = C_DECL_BUILTIN_PROTOTYPE (ext_decl
);
5783 current_function_prototype_arg_types
5784 = TYPE_ARG_TYPES (ext_type
);
5790 /* Optionally warn of old-fashioned def with no previous prototype. */
5791 if (warn_strict_prototypes
5792 && old_decl
!= error_mark_node
5793 && TYPE_ARG_TYPES (TREE_TYPE (decl1
)) == 0
5794 && C_DECL_ISNT_PROTOTYPE (old_decl
))
5795 warning (0, "function declaration isn%'t a prototype");
5796 /* Optionally warn of any global def with no previous prototype. */
5797 else if (warn_missing_prototypes
5798 && old_decl
!= error_mark_node
5799 && TREE_PUBLIC (decl1
)
5800 && !MAIN_NAME_P (DECL_NAME (decl1
))
5801 && C_DECL_ISNT_PROTOTYPE (old_decl
))
5802 warning (0, "%Jno previous prototype for %qD", decl1
, decl1
);
5803 /* Optionally warn of any def with no previous prototype
5804 if the function has already been used. */
5805 else if (warn_missing_prototypes
5807 && old_decl
!= error_mark_node
5808 && TREE_USED (old_decl
)
5809 && TYPE_ARG_TYPES (TREE_TYPE (old_decl
)) == 0)
5810 warning (0, "%J%qD was used with no prototype before its definition",
5812 /* Optionally warn of any global def with no previous declaration. */
5813 else if (warn_missing_declarations
5814 && TREE_PUBLIC (decl1
)
5816 && !MAIN_NAME_P (DECL_NAME (decl1
)))
5817 warning (0, "%Jno previous declaration for %qD", decl1
, decl1
);
5818 /* Optionally warn of any def with no previous declaration
5819 if the function has already been used. */
5820 else if (warn_missing_declarations
5822 && old_decl
!= error_mark_node
5823 && TREE_USED (old_decl
)
5824 && C_DECL_IMPLICIT (old_decl
))
5825 warning (0, "%J%qD was used with no declaration before its definition",
5828 /* This is a definition, not a reference.
5829 So normally clear DECL_EXTERNAL.
5830 However, `extern inline' acts like a declaration
5831 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5832 DECL_EXTERNAL (decl1
) = current_extern_inline
;
5834 /* This function exists in static storage.
5835 (This does not mean `static' in the C sense!) */
5836 TREE_STATIC (decl1
) = 1;
5838 /* A nested function is not global. */
5839 if (current_function_decl
!= 0)
5840 TREE_PUBLIC (decl1
) = 0;
5842 /* This is the earliest point at which we might know the assembler
5843 name of the function. Thus, if it's set before this, die horribly. */
5844 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1
));
5846 /* If #pragma weak was used, mark the decl weak now. */
5847 if (current_scope
== file_scope
)
5848 maybe_apply_pragma_weak (decl1
);
5850 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5851 if (warn_main
> 0 && MAIN_NAME_P (DECL_NAME (decl1
)))
5856 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1
)))
5857 != integer_type_node
)
5858 pedwarn ("%Jreturn type of %qD is not %<int%>", decl1
, decl1
);
5860 for (args
= TYPE_ARG_TYPES (TREE_TYPE (decl1
)); args
;
5861 args
= TREE_CHAIN (args
))
5863 tree type
= args
? TREE_VALUE (args
) : 0;
5865 if (type
== void_type_node
)
5872 if (TYPE_MAIN_VARIANT (type
) != integer_type_node
)
5873 pedwarn ("%Jfirst argument of %qD should be %<int%>",
5878 if (TREE_CODE (type
) != POINTER_TYPE
5879 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
5880 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
5882 pedwarn ("%Jsecond argument of %qD should be %<char **%>",
5887 if (TREE_CODE (type
) != POINTER_TYPE
5888 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
5889 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
5891 pedwarn ("%Jthird argument of %qD should probably be "
5892 "%<char **%>", decl1
, decl1
);
5897 /* It is intentional that this message does not mention the third
5898 argument because it's only mentioned in an appendix of the
5900 if (argct
> 0 && (argct
< 2 || argct
> 3))
5901 pedwarn ("%J%qD takes only zero or two arguments", decl1
, decl1
);
5903 if (!TREE_PUBLIC (decl1
))
5904 pedwarn ("%J%qD is normally a non-static function", decl1
, decl1
);
5907 /* Record the decl so that the function name is defined.
5908 If we already have a decl for this name, and it is a FUNCTION_DECL,
5909 use the old decl. */
5911 current_function_decl
= pushdecl (decl1
);
5914 declare_parm_level ();
5916 restype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
5917 /* Promote the value to int before returning it. */
5918 if (c_promoting_integer_type_p (restype
))
5920 /* It retains unsignedness if not really getting wider. */
5921 if (TYPE_UNSIGNED (restype
)
5922 && (TYPE_PRECISION (restype
)
5923 == TYPE_PRECISION (integer_type_node
)))
5924 restype
= unsigned_type_node
;
5926 restype
= integer_type_node
;
5929 resdecl
= build_decl (RESULT_DECL
, NULL_TREE
, restype
);
5930 DECL_ARTIFICIAL (resdecl
) = 1;
5931 DECL_IGNORED_P (resdecl
) = 1;
5932 DECL_RESULT (current_function_decl
) = resdecl
;
5934 start_fname_decls ();
5939 /* Subroutine of store_parm_decls which handles new-style function
5940 definitions (prototype format). The parms already have decls, so we
5941 need only record them as in effect and complain if any redundant
5942 old-style parm decls were written. */
5944 store_parm_decls_newstyle (tree fndecl
, const struct c_arg_info
*arg_info
)
5948 if (current_scope
->bindings
)
5950 error ("%Jold-style parameter declarations in prototyped "
5951 "function definition", fndecl
);
5953 /* Get rid of the old-style declarations. */
5957 /* Don't issue this warning for nested functions, and don't issue this
5958 warning if we got here because ARG_INFO_TYPES was error_mark_node
5959 (this happens when a function definition has just an ellipsis in
5960 its parameter list). */
5961 else if (warn_traditional
&& !in_system_header
&& !current_function_scope
5962 && arg_info
->types
!= error_mark_node
)
5963 warning (0, "%Jtraditional C rejects ISO C style function definitions",
5966 /* Now make all the parameter declarations visible in the function body.
5967 We can bypass most of the grunt work of pushdecl. */
5968 for (decl
= arg_info
->parms
; decl
; decl
= TREE_CHAIN (decl
))
5970 DECL_CONTEXT (decl
) = current_function_decl
;
5971 if (DECL_NAME (decl
))
5972 bind (DECL_NAME (decl
), decl
, current_scope
,
5973 /*invisible=*/false, /*nested=*/false);
5975 error ("%Jparameter name omitted", decl
);
5978 /* Record the parameter list in the function declaration. */
5979 DECL_ARGUMENTS (fndecl
) = arg_info
->parms
;
5981 /* Now make all the ancillary declarations visible, likewise. */
5982 for (decl
= arg_info
->others
; decl
; decl
= TREE_CHAIN (decl
))
5984 DECL_CONTEXT (decl
) = current_function_decl
;
5985 if (DECL_NAME (decl
))
5986 bind (DECL_NAME (decl
), decl
, current_scope
,
5987 /*invisible=*/false, /*nested=*/false);
5990 /* And all the tag declarations. */
5991 for (decl
= arg_info
->tags
; decl
; decl
= TREE_CHAIN (decl
))
5992 if (TREE_PURPOSE (decl
))
5993 bind (TREE_PURPOSE (decl
), TREE_VALUE (decl
), current_scope
,
5994 /*invisible=*/false, /*nested=*/false);
5997 /* Subroutine of store_parm_decls which handles old-style function
5998 definitions (separate parameter list and declarations). */
6001 store_parm_decls_oldstyle (tree fndecl
, const struct c_arg_info
*arg_info
)
6003 struct c_binding
*b
;
6004 tree parm
, decl
, last
;
6005 tree parmids
= arg_info
->parms
;
6007 /* We use DECL_WEAK as a flag to show which parameters have been
6008 seen already, since it is not used on PARM_DECL. */
6009 #ifdef ENABLE_CHECKING
6010 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
6011 gcc_assert (TREE_CODE (b
->decl
) != PARM_DECL
|| !DECL_WEAK (b
->decl
));
6014 if (warn_old_style_definition
&& !in_system_header
)
6015 warning (0, "%Jold-style function definition", fndecl
);
6017 /* Match each formal parameter name with its declaration. Save each
6018 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6019 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
6021 if (TREE_VALUE (parm
) == 0)
6023 error ("%Jparameter name missing from parameter list", fndecl
);
6024 TREE_PURPOSE (parm
) = 0;
6028 b
= I_SYMBOL_BINDING (TREE_VALUE (parm
));
6029 if (b
&& B_IN_CURRENT_SCOPE (b
))
6032 /* If we got something other than a PARM_DECL it is an error. */
6033 if (TREE_CODE (decl
) != PARM_DECL
)
6034 error ("%J%qD declared as a non-parameter", decl
, decl
);
6035 /* If the declaration is already marked, we have a duplicate
6036 name. Complain and ignore the duplicate. */
6037 else if (DECL_WEAK (decl
))
6039 error ("%Jmultiple parameters named %qD", decl
, decl
);
6040 TREE_PURPOSE (parm
) = 0;
6043 /* If the declaration says "void", complain and turn it into
6045 else if (VOID_TYPE_P (TREE_TYPE (decl
)))
6047 error ("%Jparameter %qD declared with void type", decl
, decl
);
6048 TREE_TYPE (decl
) = integer_type_node
;
6049 DECL_ARG_TYPE (decl
) = integer_type_node
;
6050 layout_decl (decl
, 0);
6053 /* If no declaration found, default to int. */
6056 decl
= build_decl (PARM_DECL
, TREE_VALUE (parm
), integer_type_node
);
6057 DECL_ARG_TYPE (decl
) = TREE_TYPE (decl
);
6058 DECL_SOURCE_LOCATION (decl
) = DECL_SOURCE_LOCATION (fndecl
);
6062 pedwarn ("%Jtype of %qD defaults to %<int%>", decl
, decl
);
6063 else if (extra_warnings
)
6064 warning (0, "%Jtype of %qD defaults to %<int%>", decl
, decl
);
6067 TREE_PURPOSE (parm
) = decl
;
6068 DECL_WEAK (decl
) = 1;
6071 /* Now examine the parms chain for incomplete declarations
6072 and declarations with no corresponding names. */
6074 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
6077 if (TREE_CODE (parm
) != PARM_DECL
)
6080 if (TREE_TYPE (parm
) != error_mark_node
6081 && !COMPLETE_TYPE_P (TREE_TYPE (parm
)))
6083 error ("%Jparameter %qD has incomplete type", parm
, parm
);
6084 TREE_TYPE (parm
) = error_mark_node
;
6087 if (!DECL_WEAK (parm
))
6089 error ("%Jdeclaration for parameter %qD but no such parameter",
6092 /* Pretend the parameter was not missing.
6093 This gets us to a standard state and minimizes
6094 further error messages. */
6095 parmids
= chainon (parmids
, tree_cons (parm
, 0, 0));
6099 /* Chain the declarations together in the order of the list of
6100 names. Store that chain in the function decl, replacing the
6101 list of names. Update the current scope to match. */
6102 DECL_ARGUMENTS (fndecl
) = 0;
6104 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
6105 if (TREE_PURPOSE (parm
))
6107 if (parm
&& TREE_PURPOSE (parm
))
6109 last
= TREE_PURPOSE (parm
);
6110 DECL_ARGUMENTS (fndecl
) = last
;
6111 DECL_WEAK (last
) = 0;
6113 for (parm
= TREE_CHAIN (parm
); parm
; parm
= TREE_CHAIN (parm
))
6114 if (TREE_PURPOSE (parm
))
6116 TREE_CHAIN (last
) = TREE_PURPOSE (parm
);
6117 last
= TREE_PURPOSE (parm
);
6118 DECL_WEAK (last
) = 0;
6120 TREE_CHAIN (last
) = 0;
6123 /* If there was a previous prototype,
6124 set the DECL_ARG_TYPE of each argument according to
6125 the type previously specified, and report any mismatches. */
6127 if (current_function_prototype_arg_types
)
6130 for (parm
= DECL_ARGUMENTS (fndecl
),
6131 type
= current_function_prototype_arg_types
;
6132 parm
|| (type
&& (TYPE_MAIN_VARIANT (TREE_VALUE (type
))
6133 != void_type_node
));
6134 parm
= TREE_CHAIN (parm
), type
= TREE_CHAIN (type
))
6136 if (parm
== 0 || type
== 0
6137 || TYPE_MAIN_VARIANT (TREE_VALUE (type
)) == void_type_node
)
6139 if (current_function_prototype_built_in
)
6140 warning (0, "number of arguments doesn%'t match "
6141 "built-in prototype");
6144 error ("number of arguments doesn%'t match prototype");
6145 error ("%Hprototype declaration",
6146 ¤t_function_prototype_locus
);
6150 /* Type for passing arg must be consistent with that
6151 declared for the arg. ISO C says we take the unqualified
6152 type for parameters declared with qualified type. */
6153 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm
)),
6154 TYPE_MAIN_VARIANT (TREE_VALUE (type
))))
6156 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm
))
6157 == TYPE_MAIN_VARIANT (TREE_VALUE (type
)))
6159 /* Adjust argument to match prototype. E.g. a previous
6160 `int foo(float);' prototype causes
6161 `int foo(x) float x; {...}' to be treated like
6162 `int foo(float x) {...}'. This is particularly
6163 useful for argument types like uid_t. */
6164 DECL_ARG_TYPE (parm
) = TREE_TYPE (parm
);
6166 if (targetm
.calls
.promote_prototypes (TREE_TYPE (current_function_decl
))
6167 && INTEGRAL_TYPE_P (TREE_TYPE (parm
))
6168 && TYPE_PRECISION (TREE_TYPE (parm
))
6169 < TYPE_PRECISION (integer_type_node
))
6170 DECL_ARG_TYPE (parm
) = integer_type_node
;
6174 /* ??? Is it possible to get here with a
6175 built-in prototype or will it always have
6176 been diagnosed as conflicting with an
6177 old-style definition and discarded? */
6178 if (current_function_prototype_built_in
)
6179 warning (0, "promoted argument %qD "
6180 "doesn%'t match built-in prototype", parm
);
6183 pedwarn ("promoted argument %qD "
6184 "doesn%'t match prototype", parm
);
6185 pedwarn ("%Hprototype declaration",
6186 ¤t_function_prototype_locus
);
6192 if (current_function_prototype_built_in
)
6193 warning (0, "argument %qD doesn%'t match "
6194 "built-in prototype", parm
);
6197 error ("argument %qD doesn%'t match prototype", parm
);
6198 error ("%Hprototype declaration",
6199 ¤t_function_prototype_locus
);
6204 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = 0;
6207 /* Otherwise, create a prototype that would match. */
6211 tree actual
= 0, last
= 0, type
;
6213 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= TREE_CHAIN (parm
))
6215 type
= tree_cons (NULL_TREE
, DECL_ARG_TYPE (parm
), NULL_TREE
);
6217 TREE_CHAIN (last
) = type
;
6222 type
= tree_cons (NULL_TREE
, void_type_node
, NULL_TREE
);
6224 TREE_CHAIN (last
) = type
;
6228 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6229 of the type of this function, but we need to avoid having this
6230 affect the types of other similarly-typed functions, so we must
6231 first force the generation of an identical (but separate) type
6232 node for the relevant function type. The new node we create
6233 will be a variant of the main variant of the original function
6236 TREE_TYPE (fndecl
) = build_variant_type_copy (TREE_TYPE (fndecl
));
6238 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = actual
;
6242 /* Store parameter declarations passed in ARG_INFO into the current
6243 function declaration. */
6246 store_parm_decls_from (struct c_arg_info
*arg_info
)
6248 current_function_arg_info
= arg_info
;
6249 store_parm_decls ();
6252 /* Store the parameter declarations into the current function declaration.
6253 This is called after parsing the parameter declarations, before
6254 digesting the body of the function.
6256 For an old-style definition, construct a prototype out of the old-style
6257 parameter declarations and inject it into the function's type. */
6260 store_parm_decls (void)
6262 tree fndecl
= current_function_decl
;
6265 /* The argument information block for FNDECL. */
6266 struct c_arg_info
*arg_info
= current_function_arg_info
;
6267 current_function_arg_info
= 0;
6269 /* True if this definition is written with a prototype. Note:
6270 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6271 list in a function definition as equivalent to (void) -- an
6272 empty argument list specifies the function has no parameters,
6273 but only (void) sets up a prototype for future calls. */
6274 proto
= arg_info
->types
!= 0;
6277 store_parm_decls_newstyle (fndecl
, arg_info
);
6279 store_parm_decls_oldstyle (fndecl
, arg_info
);
6281 /* The next call to push_scope will be a function body. */
6283 next_is_function_body
= true;
6285 /* Write a record describing this function definition to the prototypes
6286 file (if requested). */
6288 gen_aux_info_record (fndecl
, 1, 0, proto
);
6290 /* Initialize the RTL code for the function. */
6291 allocate_struct_function (fndecl
);
6293 /* Begin the statement tree for this function. */
6294 DECL_SAVED_TREE (fndecl
) = push_stmt_list ();
6296 /* ??? Insert the contents of the pending sizes list into the function
6297 to be evaluated. The only reason left to have this is
6298 void foo(int n, int array[n++])
6299 because we throw away the array type in favor of a pointer type, and
6300 thus won't naturally see the SAVE_EXPR containing the increment. All
6301 other pending sizes would be handled by gimplify_parameters. */
6304 for (t
= nreverse (get_pending_sizes ()); t
; t
= TREE_CHAIN (t
))
6305 add_stmt (TREE_VALUE (t
));
6308 /* Even though we're inside a function body, we still don't want to
6309 call expand_expr to calculate the size of a variable-sized array.
6310 We haven't necessarily assigned RTL to all variables yet, so it's
6311 not safe to try to expand expressions involving them. */
6312 cfun
->x_dont_save_pending_sizes_p
= 1;
6315 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6319 c_warn_unused_result_recursively (tree fndecl
)
6321 struct cgraph_node
*cgn
;
6323 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6324 c_warn_unused_result (&DECL_SAVED_TREE (fndecl
));
6326 /* Finalize all nested functions now. */
6327 cgn
= cgraph_node (fndecl
);
6328 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
6329 c_warn_unused_result_recursively (cgn
->decl
);
6332 /* Finish up a function declaration and compile that function
6333 all the way to assembler language output. The free the storage
6334 for the function definition.
6336 This is called after parsing the body of the function definition. */
6339 finish_function (void)
6341 tree fndecl
= current_function_decl
;
6343 label_context_stack_se
= label_context_stack_se
->next
;
6344 label_context_stack_vm
= label_context_stack_vm
->next
;
6346 if (TREE_CODE (fndecl
) == FUNCTION_DECL
6347 && targetm
.calls
.promote_prototypes (TREE_TYPE (fndecl
)))
6349 tree args
= DECL_ARGUMENTS (fndecl
);
6350 for (; args
; args
= TREE_CHAIN (args
))
6352 tree type
= TREE_TYPE (args
);
6353 if (INTEGRAL_TYPE_P (type
)
6354 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
6355 DECL_ARG_TYPE (args
) = integer_type_node
;
6359 if (DECL_INITIAL (fndecl
) && DECL_INITIAL (fndecl
) != error_mark_node
)
6360 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
6362 /* Must mark the RESULT_DECL as being in this function. */
6364 if (DECL_RESULT (fndecl
) && DECL_RESULT (fndecl
) != error_mark_node
)
6365 DECL_CONTEXT (DECL_RESULT (fndecl
)) = fndecl
;
6367 if (MAIN_NAME_P (DECL_NAME (fndecl
)) && flag_hosted
)
6369 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl
)))
6370 != integer_type_node
)
6372 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6373 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6375 pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl
, fndecl
);
6381 tree stmt
= c_finish_return (integer_zero_node
);
6382 #ifdef USE_MAPPED_LOCATION
6383 /* Hack. We don't want the middle-end to warn that this return
6384 is unreachable, so we mark its location as special. Using
6385 UNKNOWN_LOCATION has the problem that it gets clobbered in
6386 annotate_one_with_locus. A cleaner solution might be to
6387 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6389 SET_EXPR_LOCATION (stmt
, BUILTINS_LOCATION
);
6391 /* Hack. We don't want the middle-end to warn that this
6392 return is unreachable, so put the statement on the
6394 annotate_with_file_line (stmt
, input_filename
, 0);
6400 /* Tie off the statement tree for this function. */
6401 DECL_SAVED_TREE (fndecl
) = pop_stmt_list (DECL_SAVED_TREE (fndecl
));
6403 finish_fname_decls ();
6405 /* Complain if there's just no return statement. */
6406 if (warn_return_type
6407 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl
))) != VOID_TYPE
6408 && !current_function_returns_value
&& !current_function_returns_null
6409 /* Don't complain if we are no-return. */
6410 && !current_function_returns_abnormally
6411 /* Don't warn for main(). */
6412 && !MAIN_NAME_P (DECL_NAME (fndecl
))
6413 /* Or if they didn't actually specify a return type. */
6414 && !C_FUNCTION_IMPLICIT_INT (fndecl
)
6415 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6416 inline function, as we might never be compiled separately. */
6417 && DECL_INLINE (fndecl
))
6418 warning (0, "no return statement in function returning non-void");
6420 /* With just -Wextra, complain only if function returns both with
6421 and without a value. */
6423 && current_function_returns_value
6424 && current_function_returns_null
)
6425 warning (0, "this function may return with or without a value");
6427 /* Store the end of the function, so that we get good line number
6428 info for the epilogue. */
6429 cfun
->function_end_locus
= input_location
;
6431 /* If we don't have ctors/dtors sections, and this is a static
6432 constructor or destructor, it must be recorded now. */
6433 if (DECL_STATIC_CONSTRUCTOR (fndecl
)
6434 && !targetm
.have_ctors_dtors
)
6435 static_ctors
= tree_cons (NULL_TREE
, fndecl
, static_ctors
);
6436 if (DECL_STATIC_DESTRUCTOR (fndecl
)
6437 && !targetm
.have_ctors_dtors
)
6438 static_dtors
= tree_cons (NULL_TREE
, fndecl
, static_dtors
);
6440 /* Finalize the ELF visibility for the function. */
6441 c_determine_visibility (fndecl
);
6443 /* Genericize before inlining. Delay genericizing nested functions
6444 until their parent function is genericized. Since finalizing
6445 requires GENERIC, delay that as well. */
6447 if (DECL_INITIAL (fndecl
) && DECL_INITIAL (fndecl
) != error_mark_node
6448 && !undef_nested_function
)
6450 if (!decl_function_context (fndecl
))
6452 c_genericize (fndecl
);
6453 c_warn_unused_result_recursively (fndecl
);
6455 /* ??? Objc emits functions after finalizing the compilation unit.
6456 This should be cleaned up later and this conditional removed. */
6457 if (cgraph_global_info_ready
)
6459 c_expand_body (fndecl
);
6463 cgraph_finalize_function (fndecl
, false);
6467 /* Register this function with cgraph just far enough to get it
6468 added to our parent's nested function list. Handy, since the
6469 C front end doesn't have such a list. */
6470 (void) cgraph_node (fndecl
);
6474 if (!decl_function_context (fndecl
))
6475 undef_nested_function
= false;
6477 /* We're leaving the context of this function, so zap cfun.
6478 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6479 tree_rest_of_compilation. */
6481 current_function_decl
= NULL
;
6484 /* Generate the RTL for the body of FNDECL. */
6487 c_expand_body (tree fndecl
)
6490 if (!DECL_INITIAL (fndecl
)
6491 || DECL_INITIAL (fndecl
) == error_mark_node
)
6494 tree_rest_of_compilation (fndecl
);
6496 if (DECL_STATIC_CONSTRUCTOR (fndecl
)
6497 && targetm
.have_ctors_dtors
)
6498 targetm
.asm_out
.constructor (XEXP (DECL_RTL (fndecl
), 0),
6499 DEFAULT_INIT_PRIORITY
);
6500 if (DECL_STATIC_DESTRUCTOR (fndecl
)
6501 && targetm
.have_ctors_dtors
)
6502 targetm
.asm_out
.destructor (XEXP (DECL_RTL (fndecl
), 0),
6503 DEFAULT_INIT_PRIORITY
);
6506 /* Check the declarations given in a for-loop for satisfying the C99
6509 check_for_loop_decls (void)
6511 struct c_binding
*b
;
6515 /* If we get here, declarations have been used in a for loop without
6516 the C99 for loop scope. This doesn't make much sense, so don't
6518 error ("%<for%> loop initial declaration used outside C99 mode");
6521 /* C99 subclause 6.8.5 paragraph 3:
6523 [#3] The declaration part of a for statement shall only
6524 declare identifiers for objects having storage class auto or
6527 It isn't clear whether, in this sentence, "identifiers" binds to
6528 "shall only declare" or to "objects" - that is, whether all identifiers
6529 declared must be identifiers for objects, or whether the restriction
6530 only applies to those that are. (A question on this in comp.std.c
6531 in November 2000 received no answer.) We implement the strictest
6532 interpretation, to avoid creating an extension which later causes
6535 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
6538 tree decl
= b
->decl
;
6543 switch (TREE_CODE (decl
))
6546 if (TREE_STATIC (decl
))
6547 error ("%Jdeclaration of static variable %qD in %<for%> loop "
6548 "initial declaration", decl
, decl
);
6549 else if (DECL_EXTERNAL (decl
))
6550 error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6551 "initial declaration", decl
, decl
);
6555 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6559 error ("%<union %E%> declared in %<for%> loop initial declaration",
6563 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6567 error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6568 "initial declaration", decl
, decl
);
6573 /* Save and reinitialize the variables
6574 used during compilation of a C function. */
6577 c_push_function_context (struct function
*f
)
6579 struct language_function
*p
;
6580 p
= GGC_NEW (struct language_function
);
6583 p
->base
.x_stmt_tree
= c_stmt_tree
;
6584 p
->x_break_label
= c_break_label
;
6585 p
->x_cont_label
= c_cont_label
;
6586 p
->x_switch_stack
= c_switch_stack
;
6587 p
->arg_info
= current_function_arg_info
;
6588 p
->returns_value
= current_function_returns_value
;
6589 p
->returns_null
= current_function_returns_null
;
6590 p
->returns_abnormally
= current_function_returns_abnormally
;
6591 p
->warn_about_return_type
= warn_about_return_type
;
6592 p
->extern_inline
= current_extern_inline
;
6595 /* Restore the variables used during compilation of a C function. */
6598 c_pop_function_context (struct function
*f
)
6600 struct language_function
*p
= f
->language
;
6602 if (DECL_STRUCT_FUNCTION (current_function_decl
) == 0
6603 && DECL_SAVED_TREE (current_function_decl
) == NULL_TREE
)
6605 /* Stop pointing to the local nodes about to be freed. */
6606 /* But DECL_INITIAL must remain nonzero so we know this
6607 was an actual function definition. */
6608 DECL_INITIAL (current_function_decl
) = error_mark_node
;
6609 DECL_ARGUMENTS (current_function_decl
) = 0;
6612 c_stmt_tree
= p
->base
.x_stmt_tree
;
6613 c_break_label
= p
->x_break_label
;
6614 c_cont_label
= p
->x_cont_label
;
6615 c_switch_stack
= p
->x_switch_stack
;
6616 current_function_arg_info
= p
->arg_info
;
6617 current_function_returns_value
= p
->returns_value
;
6618 current_function_returns_null
= p
->returns_null
;
6619 current_function_returns_abnormally
= p
->returns_abnormally
;
6620 warn_about_return_type
= p
->warn_about_return_type
;
6621 current_extern_inline
= p
->extern_inline
;
6626 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6629 c_dup_lang_specific_decl (tree decl
)
6631 struct lang_decl
*ld
;
6633 if (!DECL_LANG_SPECIFIC (decl
))
6636 ld
= GGC_NEW (struct lang_decl
);
6637 memcpy (ld
, DECL_LANG_SPECIFIC (decl
), sizeof (struct lang_decl
));
6638 DECL_LANG_SPECIFIC (decl
) = ld
;
6641 /* The functions below are required for functionality of doing
6642 function at once processing in the C front end. Currently these
6643 functions are not called from anywhere in the C front end, but as
6644 these changes continue, that will change. */
6646 /* Returns nonzero if the current statement is a full expression,
6647 i.e. temporaries created during that statement should be destroyed
6648 at the end of the statement. */
6651 stmts_are_full_exprs_p (void)
6656 /* Returns the stmt_tree (if any) to which statements are currently
6657 being added. If there is no active statement-tree, NULL is
6661 current_stmt_tree (void)
6663 return &c_stmt_tree
;
6666 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6670 anon_aggr_type_p (tree
ARG_UNUSED (node
))
6675 /* Return the global value of T as a symbol. */
6678 identifier_global_value (tree t
)
6680 struct c_binding
*b
;
6682 for (b
= I_SYMBOL_BINDING (t
); b
; b
= b
->shadowed
)
6683 if (B_IN_FILE_SCOPE (b
) || B_IN_EXTERNAL_SCOPE (b
))
6689 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6690 otherwise the name is found in ridpointers from RID_INDEX. */
6693 record_builtin_type (enum rid rid_index
, const char *name
, tree type
)
6697 id
= ridpointers
[(int) rid_index
];
6699 id
= get_identifier (name
);
6700 decl
= build_decl (TYPE_DECL
, id
, type
);
6702 if (debug_hooks
->type_decl
)
6703 debug_hooks
->type_decl (decl
, false);
6706 /* Build the void_list_node (void_type_node having been created). */
6708 build_void_list_node (void)
6710 tree t
= build_tree_list (NULL_TREE
, void_type_node
);
6714 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6717 build_c_parm (struct c_declspecs
*specs
, tree attrs
,
6718 struct c_declarator
*declarator
)
6720 struct c_parm
*ret
= XOBNEW (&parser_obstack
, struct c_parm
);
6723 ret
->declarator
= declarator
;
6727 /* Return a declarator with nested attributes. TARGET is the inner
6728 declarator to which these attributes apply. ATTRS are the
6731 struct c_declarator
*
6732 build_attrs_declarator (tree attrs
, struct c_declarator
*target
)
6734 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
6735 ret
->kind
= cdk_attrs
;
6736 ret
->declarator
= target
;
6737 ret
->u
.attrs
= attrs
;
6741 /* Return a declarator for a function with arguments specified by ARGS
6742 and return type specified by TARGET. */
6744 struct c_declarator
*
6745 build_function_declarator (struct c_arg_info
*args
,
6746 struct c_declarator
*target
)
6748 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
6749 ret
->kind
= cdk_function
;
6750 ret
->declarator
= target
;
6751 ret
->u
.arg_info
= args
;
6755 /* Return a declarator for the identifier IDENT (which may be
6756 NULL_TREE for an abstract declarator). */
6758 struct c_declarator
*
6759 build_id_declarator (tree ident
)
6761 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
6763 ret
->declarator
= 0;
6765 /* Default value - may get reset to a more precise location. */
6766 ret
->id_loc
= input_location
;
6770 /* Return something to represent absolute declarators containing a *.
6771 TARGET is the absolute declarator that the * contains.
6772 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6773 to apply to the pointer type. */
6775 struct c_declarator
*
6776 make_pointer_declarator (struct c_declspecs
*type_quals_attrs
,
6777 struct c_declarator
*target
)
6781 struct c_declarator
*itarget
= target
;
6782 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
6783 if (type_quals_attrs
)
6785 attrs
= type_quals_attrs
->attrs
;
6786 quals
= quals_from_declspecs (type_quals_attrs
);
6787 if (attrs
!= NULL_TREE
)
6788 itarget
= build_attrs_declarator (attrs
, target
);
6790 ret
->kind
= cdk_pointer
;
6791 ret
->declarator
= itarget
;
6792 ret
->u
.pointer_quals
= quals
;
6796 /* Return a pointer to a structure for an empty list of declaration
6799 struct c_declspecs
*
6800 build_null_declspecs (void)
6802 struct c_declspecs
*ret
= XOBNEW (&parser_obstack
, struct c_declspecs
);
6806 ret
->typespec_word
= cts_none
;
6807 ret
->storage_class
= csc_none
;
6808 ret
->declspecs_seen_p
= false;
6809 ret
->type_seen_p
= false;
6810 ret
->non_sc_seen_p
= false;
6811 ret
->typedef_p
= false;
6812 ret
->tag_defined_p
= false;
6813 ret
->explicit_signed_p
= false;
6814 ret
->deprecated_p
= false;
6815 ret
->default_int_p
= false;
6816 ret
->long_p
= false;
6817 ret
->long_long_p
= false;
6818 ret
->short_p
= false;
6819 ret
->signed_p
= false;
6820 ret
->unsigned_p
= false;
6821 ret
->complex_p
= false;
6822 ret
->inline_p
= false;
6823 ret
->thread_p
= false;
6824 ret
->const_p
= false;
6825 ret
->volatile_p
= false;
6826 ret
->restrict_p
= false;
6830 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6833 struct c_declspecs
*
6834 declspecs_add_qual (struct c_declspecs
*specs
, tree qual
)
6838 specs
->non_sc_seen_p
= true;
6839 specs
->declspecs_seen_p
= true;
6840 gcc_assert (TREE_CODE (qual
) == IDENTIFIER_NODE
6841 && C_IS_RESERVED_WORD (qual
));
6842 i
= C_RID_CODE (qual
);
6846 dupe
= specs
->const_p
;
6847 specs
->const_p
= true;
6850 dupe
= specs
->volatile_p
;
6851 specs
->volatile_p
= true;
6854 dupe
= specs
->restrict_p
;
6855 specs
->restrict_p
= true;
6860 if (dupe
&& pedantic
&& !flag_isoc99
)
6861 pedwarn ("duplicate %qE", qual
);
6865 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6868 struct c_declspecs
*
6869 declspecs_add_type (struct c_declspecs
*specs
, struct c_typespec spec
)
6871 tree type
= spec
.spec
;
6872 specs
->non_sc_seen_p
= true;
6873 specs
->declspecs_seen_p
= true;
6874 specs
->type_seen_p
= true;
6875 if (TREE_DEPRECATED (type
))
6876 specs
->deprecated_p
= true;
6878 /* Handle type specifier keywords. */
6879 if (TREE_CODE (type
) == IDENTIFIER_NODE
&& C_IS_RESERVED_WORD (type
))
6881 enum rid i
= C_RID_CODE (type
);
6884 error ("two or more data types in declaration specifiers");
6887 if ((int) i
<= (int) RID_LAST_MODIFIER
)
6889 /* "long", "short", "signed", "unsigned" or "_Complex". */
6894 if (specs
->long_long_p
)
6896 error ("%<long long long%> is too long for GCC");
6901 if (specs
->typespec_word
== cts_double
)
6903 error ("both %<long long%> and %<double%> in "
6904 "declaration specifiers");
6907 if (pedantic
&& !flag_isoc99
&& !in_system_header
6909 pedwarn ("ISO C90 does not support %<long long%>");
6910 specs
->long_long_p
= 1;
6914 error ("both %<long%> and %<short%> in "
6915 "declaration specifiers");
6916 else if (specs
->typespec_word
== cts_void
)
6917 error ("both %<long%> and %<void%> in "
6918 "declaration specifiers");
6919 else if (specs
->typespec_word
== cts_bool
)
6920 error ("both %<long%> and %<_Bool%> in "
6921 "declaration specifiers");
6922 else if (specs
->typespec_word
== cts_char
)
6923 error ("both %<long%> and %<char%> in "
6924 "declaration specifiers");
6925 else if (specs
->typespec_word
== cts_float
)
6926 error ("both %<long%> and %<float%> in "
6927 "declaration specifiers");
6929 specs
->long_p
= true;
6932 dupe
= specs
->short_p
;
6934 error ("both %<long%> and %<short%> in "
6935 "declaration specifiers");
6936 else if (specs
->typespec_word
== cts_void
)
6937 error ("both %<short%> and %<void%> in "
6938 "declaration specifiers");
6939 else if (specs
->typespec_word
== cts_bool
)
6940 error ("both %<short%> and %<_Bool%> in "
6941 "declaration specifiers");
6942 else if (specs
->typespec_word
== cts_char
)
6943 error ("both %<short%> and %<char%> in "
6944 "declaration specifiers");
6945 else if (specs
->typespec_word
== cts_float
)
6946 error ("both %<short%> and %<float%> in "
6947 "declaration specifiers");
6948 else if (specs
->typespec_word
== cts_double
)
6949 error ("both %<short%> and %<double%> in "
6950 "declaration specifiers");
6952 specs
->short_p
= true;
6955 dupe
= specs
->signed_p
;
6956 if (specs
->unsigned_p
)
6957 error ("both %<signed%> and %<unsigned%> in "
6958 "declaration specifiers");
6959 else if (specs
->typespec_word
== cts_void
)
6960 error ("both %<signed%> and %<void%> in "
6961 "declaration specifiers");
6962 else if (specs
->typespec_word
== cts_bool
)
6963 error ("both %<signed%> and %<_Bool%> in "
6964 "declaration specifiers");
6965 else if (specs
->typespec_word
== cts_float
)
6966 error ("both %<signed%> and %<float%> in "
6967 "declaration specifiers");
6968 else if (specs
->typespec_word
== cts_double
)
6969 error ("both %<signed%> and %<double%> in "
6970 "declaration specifiers");
6972 specs
->signed_p
= true;
6975 dupe
= specs
->unsigned_p
;
6976 if (specs
->signed_p
)
6977 error ("both %<signed%> and %<unsigned%> in "
6978 "declaration specifiers");
6979 else if (specs
->typespec_word
== cts_void
)
6980 error ("both %<unsigned%> and %<void%> in "
6981 "declaration specifiers");
6982 else if (specs
->typespec_word
== cts_bool
)
6983 error ("both %<unsigned%> and %<_Bool%> in "
6984 "declaration specifiers");
6985 else if (specs
->typespec_word
== cts_float
)
6986 error ("both %<unsigned%> and %<float%> in "
6987 "declaration specifiers");
6988 else if (specs
->typespec_word
== cts_double
)
6989 error ("both %<unsigned%> and %<double%> in "
6990 "declaration specifiers");
6992 specs
->unsigned_p
= true;
6995 dupe
= specs
->complex_p
;
6996 if (pedantic
&& !flag_isoc99
&& !in_system_header
)
6997 pedwarn ("ISO C90 does not support complex types");
6998 if (specs
->typespec_word
== cts_void
)
6999 error ("both %<complex%> and %<void%> in "
7000 "declaration specifiers");
7001 else if (specs
->typespec_word
== cts_bool
)
7002 error ("both %<complex%> and %<_Bool%> in "
7003 "declaration specifiers");
7005 specs
->complex_p
= true;
7012 error ("duplicate %qE", type
);
7018 /* "void", "_Bool", "char", "int", "float" or "double". */
7019 if (specs
->typespec_word
!= cts_none
)
7021 error ("two or more data types in declaration specifiers");
7028 error ("both %<long%> and %<void%> in "
7029 "declaration specifiers");
7030 else if (specs
->short_p
)
7031 error ("both %<short%> and %<void%> in "
7032 "declaration specifiers");
7033 else if (specs
->signed_p
)
7034 error ("both %<signed%> and %<void%> in "
7035 "declaration specifiers");
7036 else if (specs
->unsigned_p
)
7037 error ("both %<unsigned%> and %<void%> in "
7038 "declaration specifiers");
7039 else if (specs
->complex_p
)
7040 error ("both %<complex%> and %<void%> in "
7041 "declaration specifiers");
7043 specs
->typespec_word
= cts_void
;
7047 error ("both %<long%> and %<_Bool%> in "
7048 "declaration specifiers");
7049 else if (specs
->short_p
)
7050 error ("both %<short%> and %<_Bool%> in "
7051 "declaration specifiers");
7052 else if (specs
->signed_p
)
7053 error ("both %<signed%> and %<_Bool%> in "
7054 "declaration specifiers");
7055 else if (specs
->unsigned_p
)
7056 error ("both %<unsigned%> and %<_Bool%> in "
7057 "declaration specifiers");
7058 else if (specs
->complex_p
)
7059 error ("both %<complex%> and %<_Bool%> in "
7060 "declaration specifiers");
7062 specs
->typespec_word
= cts_bool
;
7066 error ("both %<long%> and %<char%> in "
7067 "declaration specifiers");
7068 else if (specs
->short_p
)
7069 error ("both %<short%> and %<char%> in "
7070 "declaration specifiers");
7072 specs
->typespec_word
= cts_char
;
7075 specs
->typespec_word
= cts_int
;
7079 error ("both %<long%> and %<float%> in "
7080 "declaration specifiers");
7081 else if (specs
->short_p
)
7082 error ("both %<short%> and %<float%> in "
7083 "declaration specifiers");
7084 else if (specs
->signed_p
)
7085 error ("both %<signed%> and %<float%> in "
7086 "declaration specifiers");
7087 else if (specs
->unsigned_p
)
7088 error ("both %<unsigned%> and %<float%> in "
7089 "declaration specifiers");
7091 specs
->typespec_word
= cts_float
;
7094 if (specs
->long_long_p
)
7095 error ("both %<long long%> and %<double%> in "
7096 "declaration specifiers");
7097 else if (specs
->short_p
)
7098 error ("both %<short%> and %<double%> in "
7099 "declaration specifiers");
7100 else if (specs
->signed_p
)
7101 error ("both %<signed%> and %<double%> in "
7102 "declaration specifiers");
7103 else if (specs
->unsigned_p
)
7104 error ("both %<unsigned%> and %<double%> in "
7105 "declaration specifiers");
7107 specs
->typespec_word
= cts_double
;
7110 /* ObjC reserved word "id", handled below. */
7116 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7117 form of ObjC type, cases such as "int" and "long" being handled
7118 above), a TYPE (struct, union, enum and typeof specifiers) or an
7119 ERROR_MARK. In none of these cases may there have previously
7120 been any type specifiers. */
7121 if (specs
->type
|| specs
->typespec_word
!= cts_none
7122 || specs
->long_p
|| specs
->short_p
|| specs
->signed_p
7123 || specs
->unsigned_p
|| specs
->complex_p
)
7124 error ("two or more data types in declaration specifiers");
7125 else if (TREE_CODE (type
) == TYPE_DECL
)
7127 if (TREE_TYPE (type
) == error_mark_node
)
7128 ; /* Allow the type to default to int to avoid cascading errors. */
7131 specs
->type
= TREE_TYPE (type
);
7132 specs
->decl_attr
= DECL_ATTRIBUTES (type
);
7133 specs
->typedef_p
= true;
7134 specs
->explicit_signed_p
= C_TYPEDEF_EXPLICITLY_SIGNED (type
);
7137 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
7139 tree t
= lookup_name (type
);
7140 if (!t
|| TREE_CODE (t
) != TYPE_DECL
)
7141 error ("%qE fails to be a typedef or built in type", type
);
7142 else if (TREE_TYPE (t
) == error_mark_node
)
7145 specs
->type
= TREE_TYPE (t
);
7147 else if (TREE_CODE (type
) != ERROR_MARK
)
7149 if (spec
.kind
== ctsk_tagdef
|| spec
.kind
== ctsk_tagfirstref
)
7150 specs
->tag_defined_p
= true;
7151 if (spec
.kind
== ctsk_typeof
)
7152 specs
->typedef_p
= true;
7159 /* Add the storage class specifier or function specifier SCSPEC to the
7160 declaration specifiers SPECS, returning SPECS. */
7162 struct c_declspecs
*
7163 declspecs_add_scspec (struct c_declspecs
*specs
, tree scspec
)
7166 enum c_storage_class n
= csc_none
;
7168 specs
->declspecs_seen_p
= true;
7169 gcc_assert (TREE_CODE (scspec
) == IDENTIFIER_NODE
7170 && C_IS_RESERVED_WORD (scspec
));
7171 i
= C_RID_CODE (scspec
);
7172 if (extra_warnings
&& specs
->non_sc_seen_p
)
7173 warning (0, "%qE is not at beginning of declaration", scspec
);
7177 /* C99 permits duplicate inline. Although of doubtful utility,
7178 it seems simplest to permit it in gnu89 mode as well, as
7179 there is also little utility in maintaining this as a
7180 difference between gnu89 and C99 inline. */
7182 specs
->inline_p
= true;
7185 dupe
= specs
->thread_p
;
7186 if (specs
->storage_class
== csc_auto
)
7187 error ("%<__thread%> used with %<auto%>");
7188 else if (specs
->storage_class
== csc_register
)
7189 error ("%<__thread%> used with %<register%>");
7190 else if (specs
->storage_class
== csc_typedef
)
7191 error ("%<__thread%> used with %<typedef%>");
7193 specs
->thread_p
= true;
7200 /* Diagnose "__thread extern". */
7201 if (specs
->thread_p
)
7202 error ("%<__thread%> before %<extern%>");
7209 /* Diagnose "__thread static". */
7210 if (specs
->thread_p
)
7211 error ("%<__thread%> before %<static%>");
7219 if (n
!= csc_none
&& n
== specs
->storage_class
)
7222 error ("duplicate %qE", scspec
);
7225 if (specs
->storage_class
!= csc_none
&& n
!= specs
->storage_class
)
7227 error ("multiple storage classes in declaration specifiers");
7231 specs
->storage_class
= n
;
7232 if (n
!= csc_extern
&& n
!= csc_static
&& specs
->thread_p
)
7234 error ("%<__thread%> used with %qE", scspec
);
7235 specs
->thread_p
= false;
7242 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7245 struct c_declspecs
*
7246 declspecs_add_attrs (struct c_declspecs
*specs
, tree attrs
)
7248 specs
->attrs
= chainon (attrs
, specs
->attrs
);
7249 specs
->declspecs_seen_p
= true;
7253 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7254 specifiers with any other type specifier to determine the resulting
7255 type. This is where ISO C checks on complex types are made, since
7256 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7259 struct c_declspecs
*
7260 finish_declspecs (struct c_declspecs
*specs
)
7262 /* If a type was specified as a whole, we have no modifiers and are
7264 if (specs
->type
!= NULL_TREE
)
7266 gcc_assert (!specs
->long_p
&& !specs
->long_long_p
&& !specs
->short_p
7267 && !specs
->signed_p
&& !specs
->unsigned_p
7268 && !specs
->complex_p
);
7272 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7273 has been specified, treat it as "int" unless "_Complex" is
7274 present and there are no other specifiers. If we just have
7275 "_Complex", it is equivalent to "_Complex double", but e.g.
7276 "_Complex short" is equivalent to "_Complex short int". */
7277 if (specs
->typespec_word
== cts_none
)
7279 if (specs
->long_p
|| specs
->short_p
7280 || specs
->signed_p
|| specs
->unsigned_p
)
7282 specs
->typespec_word
= cts_int
;
7284 else if (specs
->complex_p
)
7286 specs
->typespec_word
= cts_double
;
7288 pedwarn ("ISO C does not support plain %<complex%> meaning "
7289 "%<double complex%>");
7293 specs
->typespec_word
= cts_int
;
7294 specs
->default_int_p
= true;
7295 /* We don't diagnose this here because grokdeclarator will
7296 give more specific diagnostics according to whether it is
7297 a function definition. */
7301 /* If "signed" was specified, record this to distinguish "int" and
7302 "signed int" in the case of a bit-field with
7303 -funsigned-bitfields. */
7304 specs
->explicit_signed_p
= specs
->signed_p
;
7306 /* Now compute the actual type. */
7307 switch (specs
->typespec_word
)
7310 gcc_assert (!specs
->long_p
&& !specs
->short_p
7311 && !specs
->signed_p
&& !specs
->unsigned_p
7312 && !specs
->complex_p
);
7313 specs
->type
= void_type_node
;
7316 gcc_assert (!specs
->long_p
&& !specs
->short_p
7317 && !specs
->signed_p
&& !specs
->unsigned_p
7318 && !specs
->complex_p
);
7319 specs
->type
= boolean_type_node
;
7322 gcc_assert (!specs
->long_p
&& !specs
->short_p
);
7323 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
7324 if (specs
->signed_p
)
7325 specs
->type
= signed_char_type_node
;
7326 else if (specs
->unsigned_p
)
7327 specs
->type
= unsigned_char_type_node
;
7329 specs
->type
= char_type_node
;
7330 if (specs
->complex_p
)
7333 pedwarn ("ISO C does not support complex integer types");
7334 specs
->type
= build_complex_type (specs
->type
);
7338 gcc_assert (!(specs
->long_p
&& specs
->short_p
));
7339 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
7340 if (specs
->long_long_p
)
7341 specs
->type
= (specs
->unsigned_p
7342 ? long_long_unsigned_type_node
7343 : long_long_integer_type_node
);
7344 else if (specs
->long_p
)
7345 specs
->type
= (specs
->unsigned_p
7346 ? long_unsigned_type_node
7347 : long_integer_type_node
);
7348 else if (specs
->short_p
)
7349 specs
->type
= (specs
->unsigned_p
7350 ? short_unsigned_type_node
7351 : short_integer_type_node
);
7353 specs
->type
= (specs
->unsigned_p
7354 ? unsigned_type_node
7355 : integer_type_node
);
7356 if (specs
->complex_p
)
7359 pedwarn ("ISO C does not support complex integer types");
7360 specs
->type
= build_complex_type (specs
->type
);
7364 gcc_assert (!specs
->long_p
&& !specs
->short_p
7365 && !specs
->signed_p
&& !specs
->unsigned_p
);
7366 specs
->type
= (specs
->complex_p
7367 ? complex_float_type_node
7371 gcc_assert (!specs
->long_long_p
&& !specs
->short_p
7372 && !specs
->signed_p
&& !specs
->unsigned_p
);
7375 specs
->type
= (specs
->complex_p
7376 ? complex_long_double_type_node
7377 : long_double_type_node
);
7381 specs
->type
= (specs
->complex_p
7382 ? complex_double_type_node
7383 : double_type_node
);
7393 /* Synthesize a function which calls all the global ctors or global
7394 dtors in this file. This is only used for targets which do not
7395 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7397 build_cdtor (int method_type
, tree cdtors
)
7404 for (; cdtors
; cdtors
= TREE_CHAIN (cdtors
))
7405 append_to_statement_list (build_function_call (TREE_VALUE (cdtors
), 0),
7408 cgraph_build_static_cdtor (method_type
, body
, DEFAULT_INIT_PRIORITY
);
7411 /* Perform final processing on one file scope's declarations (or the
7412 external scope's declarations), GLOBALS. */
7414 c_write_global_declarations_1 (tree globals
)
7416 size_t len
= list_length (globals
);
7417 tree
*vec
= XNEWVEC (tree
, len
);
7421 /* Process the decls in the order they were written. */
7422 for (i
= 0, decl
= globals
; i
< len
; i
++, decl
= TREE_CHAIN (decl
))
7425 /* Check for used but undefined static functions using the C
7426 standard's definition of "used", and set TREE_NO_WARNING so
7427 that check_global_declarations doesn't repeat the check. */
7428 if (TREE_CODE (decl
) == FUNCTION_DECL
7429 && DECL_INITIAL (decl
) == 0
7430 && DECL_EXTERNAL (decl
)
7431 && !TREE_PUBLIC (decl
)
7432 && C_DECL_USED (decl
))
7434 pedwarn ("%J%qF used but never defined", decl
, decl
);
7435 TREE_NO_WARNING (decl
) = 1;
7439 wrapup_global_declarations (vec
, len
);
7440 check_global_declarations (vec
, len
);
7446 c_write_global_declarations (void)
7450 /* We don't want to do this if generating a PCH. */
7454 /* Don't waste time on further processing if -fsyntax-only or we've
7455 encountered errors. */
7456 if (flag_syntax_only
|| errorcount
|| sorrycount
|| cpp_errors (parse_in
))
7459 /* Close the external scope. */
7460 ext_block
= pop_scope ();
7462 gcc_assert (!current_scope
);
7464 /* Process all file scopes in this compilation, and the external_scope,
7465 through wrapup_global_declarations and check_global_declarations. */
7466 for (t
= all_translation_units
; t
; t
= TREE_CHAIN (t
))
7467 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t
)));
7468 c_write_global_declarations_1 (BLOCK_VARS (ext_block
));
7470 /* Generate functions to call static constructors and destructors
7471 for targets that do not support .ctors/.dtors sections. These
7472 functions have magic names which are detected by collect2. */
7473 build_cdtor ('I', static_ctors
); static_ctors
= 0;
7474 build_cdtor ('D', static_dtors
); static_dtors
= 0;
7476 /* We're done parsing; proceed to optimize and emit assembly.
7477 FIXME: shouldn't be the front end's responsibility to call this. */
7481 #include "gt-c-decl.h"