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, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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"
56 #include "tree-iterator.h"
57 #include "diagnostic.h"
58 #include "tree-dump.h"
63 #include "langhooks-def.h"
64 #include "pointer-set.h"
68 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
70 { NORMAL
, /* Ordinary declaration */
71 FUNCDEF
, /* Function definition */
72 PARM
, /* Declaration of parm before function body */
73 FIELD
, /* Declaration inside struct or union */
74 TYPENAME
}; /* Typename (inside cast or sizeof) */
76 /* States indicating how grokdeclarator() should handle declspecs marked
77 with __attribute__((deprecated)). An object declared as
78 __attribute__((deprecated)) suppresses warnings of uses of other
81 enum deprecated_states
{
87 /* Nonzero if we have seen an invalid cross reference
88 to a struct, union, or enum, but not yet printed the message. */
89 tree pending_invalid_xref
;
91 /* File and line to appear in the eventual error message. */
92 location_t pending_invalid_xref_location
;
94 /* True means we've initialized exception handling. */
95 bool c_eh_initialized_p
;
97 /* The file and line that the prototype came from if this is an
98 old-style definition; used for diagnostics in
99 store_parm_decls_oldstyle. */
101 static location_t current_function_prototype_locus
;
103 /* Whether this prototype was built-in. */
105 static bool current_function_prototype_built_in
;
107 /* The argument type information of this prototype. */
109 static tree current_function_prototype_arg_types
;
111 /* The argument information structure for the function currently being
114 static struct c_arg_info
*current_function_arg_info
;
116 /* The obstack on which parser and related data structures, which are
117 not live beyond their top-level declaration or definition, are
119 struct obstack parser_obstack
;
121 /* The current statement tree. */
123 static GTY(()) struct stmt_tree_s c_stmt_tree
;
125 /* State saving variables. */
129 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
130 included in this invocation. Note that the current translation
131 unit is not included in this list. */
133 static GTY(()) tree all_translation_units
;
135 /* A list of decls to be made automatically visible in each file scope. */
136 static GTY(()) tree visible_builtins
;
138 /* Set to 0 at beginning of a function definition, set to 1 if
139 a return statement that specifies a return value is seen. */
141 int current_function_returns_value
;
143 /* Set to 0 at beginning of a function definition, set to 1 if
144 a return statement with no argument is seen. */
146 int current_function_returns_null
;
148 /* Set to 0 at beginning of a function definition, set to 1 if
149 a call to a noreturn function is seen. */
151 int current_function_returns_abnormally
;
153 /* Set to nonzero by `grokdeclarator' for a function
154 whose return type is defaulted, if warnings for this are desired. */
156 static int warn_about_return_type
;
158 /* Nonzero when the current toplevel function contains a declaration
159 of a nested function which is never defined. */
161 static bool undef_nested_function
;
163 /* True means global_bindings_p should return false even if the scope stack
164 says we are in file scope. */
165 bool c_override_global_bindings_to_false
;
168 /* Each c_binding structure describes one binding of an identifier to
169 a decl. All the decls in a scope - irrespective of namespace - are
170 chained together by the ->prev field, which (as the name implies)
171 runs in reverse order. All the decls in a given namespace bound to
172 a given identifier are chained by the ->shadowed field, which runs
173 from inner to outer scopes.
175 The ->decl field usually points to a DECL node, but there are two
176 exceptions. In the namespace of type tags, the bound entity is a
177 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
178 identifier is encountered, it is bound to error_mark_node to
179 suppress further errors about that identifier in the current
182 The ->type field stores the type of the declaration in this scope;
183 if NULL, the type is the type of the ->decl field. This is only of
184 relevance for objects with external or internal linkage which may
185 be redeclared in inner scopes, forming composite types that only
186 persist for the duration of those scopes. In the external scope,
187 this stores the composite of all the types declared for this
188 object, visible or not. The ->inner_comp field (used only at file
189 scope) stores whether an incomplete array type at file scope was
190 completed at an inner scope to an array size other than 1.
192 The depth field is copied from the scope structure that holds this
193 decl. It is used to preserve the proper ordering of the ->shadowed
194 field (see bind()) and also for a handful of special-case checks.
195 Finally, the invisible bit is true for a decl which should be
196 ignored for purposes of normal name lookup, and the nested bit is
197 true for a decl that's been bound a second time in an inner scope;
198 in all such cases, the binding in the outer scope will have its
199 invisible bit true. */
201 struct GTY((chain_next ("%h.prev"))) c_binding
{
202 tree decl
; /* the decl bound */
203 tree type
; /* the type in this scope */
204 tree id
; /* the identifier it's bound to */
205 struct c_binding
*prev
; /* the previous decl in this scope */
206 struct c_binding
*shadowed
; /* the innermost decl shadowed by this one */
207 unsigned int depth
: 28; /* depth of this scope */
208 BOOL_BITFIELD invisible
: 1; /* normal lookup should ignore this binding */
209 BOOL_BITFIELD nested
: 1; /* do not set DECL_CONTEXT when popping */
210 BOOL_BITFIELD inner_comp
: 1; /* incomplete array completed in inner scope */
212 location_t locus
; /* location for nested bindings */
214 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
215 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
216 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
217 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
219 #define I_SYMBOL_BINDING(node) \
220 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
221 #define I_SYMBOL_DECL(node) \
222 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
224 #define I_TAG_BINDING(node) \
225 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
226 #define I_TAG_DECL(node) \
227 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
229 #define I_LABEL_BINDING(node) \
230 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
231 #define I_LABEL_DECL(node) \
232 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
234 /* Each C symbol points to three linked lists of c_binding structures.
235 These describe the values of the identifier in the three different
236 namespaces defined by the language. */
238 struct GTY(()) lang_identifier
{
239 struct c_common_identifier common_id
;
240 struct c_binding
*symbol_binding
; /* vars, funcs, constants, typedefs */
241 struct c_binding
*tag_binding
; /* struct/union/enum tags */
242 struct c_binding
*label_binding
; /* labels */
245 /* Validate c-lang.c's assumptions. */
246 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
247 [(sizeof(struct lang_identifier
) == C_SIZEOF_STRUCT_LANG_IDENTIFIER
) ? 1 : -1];
249 /* The resulting tree type. */
251 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
252 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))"))) lang_tree_node
254 union tree_node
GTY ((tag ("0"),
255 desc ("tree_node_structure (&%h)")))
257 struct lang_identifier
GTY ((tag ("1"))) identifier
;
260 /* Each c_scope structure describes the complete contents of one
261 scope. Four scopes are distinguished specially: the innermost or
262 current scope, the innermost function scope, the file scope (always
263 the second to outermost) and the outermost or external scope.
265 Most declarations are recorded in the current scope.
267 All normal label declarations are recorded in the innermost
268 function scope, as are bindings of undeclared identifiers to
269 error_mark_node. (GCC permits nested functions as an extension,
270 hence the 'innermost' qualifier.) Explicitly declared labels
271 (using the __label__ extension) appear in the current scope.
273 Being in the file scope (current_scope == file_scope) causes
274 special behavior in several places below. Also, under some
275 conditions the Objective-C front end records declarations in the
276 file scope even though that isn't the current scope.
278 All declarations with external linkage are recorded in the external
279 scope, even if they aren't visible there; this models the fact that
280 such declarations are visible to the entire program, and (with a
281 bit of cleverness, see pushdecl) allows diagnosis of some violations
282 of C99 6.2.2p7 and 6.2.7p2:
284 If, within the same translation unit, the same identifier appears
285 with both internal and external linkage, the behavior is
288 All declarations that refer to the same object or function shall
289 have compatible type; otherwise, the behavior is undefined.
291 Initially only the built-in declarations, which describe compiler
292 intrinsic functions plus a subset of the standard library, are in
295 The order of the blocks list matters, and it is frequently appended
296 to. To avoid having to walk all the way to the end of the list on
297 each insertion, or reverse the list later, we maintain a pointer to
298 the last list entry. (FIXME: It should be feasible to use a reversed
301 The bindings list is strictly in reverse order of declarations;
302 pop_scope relies on this. */
305 struct GTY((chain_next ("%h.outer"))) c_scope
{
306 /* The scope containing this one. */
307 struct c_scope
*outer
;
309 /* The next outermost function scope. */
310 struct c_scope
*outer_function
;
312 /* All bindings in this scope. */
313 struct c_binding
*bindings
;
315 /* For each scope (except the global one), a chain of BLOCK nodes
316 for all the scopes that were entered and exited one level down. */
320 /* The depth of this scope. Used to keep the ->shadowed chain of
321 bindings sorted innermost to outermost. */
322 unsigned int depth
: 28;
324 /* True if we are currently filling this scope with parameter
326 BOOL_BITFIELD parm_flag
: 1;
328 /* True if we saw [*] in this scope. Used to give an error messages
329 if these appears in a function definition. */
330 BOOL_BITFIELD had_vla_unspec
: 1;
332 /* True if we already complained about forward parameter decls
333 in this scope. This prevents double warnings on
334 foo (int a; int b; ...) */
335 BOOL_BITFIELD warned_forward_parm_decls
: 1;
337 /* True if this is the outermost block scope of a function body.
338 This scope contains the parameters, the local variables declared
339 in the outermost block, and all the labels (except those in
340 nested functions, or declared at block scope with __label__). */
341 BOOL_BITFIELD function_body
: 1;
343 /* True means make a BLOCK for this scope no matter what. */
344 BOOL_BITFIELD keep
: 1;
347 /* The scope currently in effect. */
349 static GTY(()) struct c_scope
*current_scope
;
351 /* The innermost function scope. Ordinary (not explicitly declared)
352 labels, bindings to error_mark_node, and the lazily-created
353 bindings of __func__ and its friends get this scope. */
355 static GTY(()) struct c_scope
*current_function_scope
;
357 /* The C file scope. This is reset for each input translation unit. */
359 static GTY(()) struct c_scope
*file_scope
;
361 /* The outermost scope. This is used for all declarations with
362 external linkage, and only these, hence the name. */
364 static GTY(()) struct c_scope
*external_scope
;
366 /* A chain of c_scope structures awaiting reuse. */
368 static GTY((deletable
)) struct c_scope
*scope_freelist
;
370 /* A chain of c_binding structures awaiting reuse. */
372 static GTY((deletable
)) struct c_binding
*binding_freelist
;
374 /* Append VAR to LIST in scope SCOPE. */
375 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
376 struct c_scope *s_ = (scope); \
378 if (s_->list##_last) \
379 BLOCK_CHAIN (s_->list##_last) = d_; \
382 s_->list##_last = d_; \
385 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
386 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
387 struct c_scope *t_ = (tscope); \
388 struct c_scope *f_ = (fscope); \
390 BLOCK_CHAIN (t_->to##_last) = f_->from; \
393 t_->to##_last = f_->from##_last; \
396 /* A c_inline_static structure stores details of a static identifier
397 referenced in a definition of a function that may be an inline
398 definition if no subsequent declaration of that function uses
399 "extern" or does not use "inline". */
401 struct GTY((chain_next ("%h.next"))) c_inline_static
{
402 /* The location for a diagnostic. */
405 /* The function that may be an inline definition. */
408 /* The object or function referenced. */
411 /* What sort of reference this is. */
412 enum c_inline_static_type type
;
414 /* The next such structure or NULL. */
415 struct c_inline_static
*next
;
418 /* List of static identifiers used or referenced in functions that may
419 be inline definitions. */
420 static GTY(()) struct c_inline_static
*c_inline_statics
;
422 /* True means unconditionally make a BLOCK for the next scope pushed. */
424 static bool keep_next_level_flag
;
426 /* True means the next call to push_scope will be the outermost scope
427 of a function body, so do not push a new scope, merely cease
428 expecting parameter decls. */
430 static bool next_is_function_body
;
432 /* Forward declarations. */
433 static tree
lookup_name_in_scope (tree
, struct c_scope
*);
434 static tree
c_make_fname_decl (tree
, int);
435 static tree
grokdeclarator (const struct c_declarator
*,
436 struct c_declspecs
*,
437 enum decl_context
, bool, tree
*, tree
*, tree
*,
438 bool *, enum deprecated_states
);
439 static tree
grokparms (struct c_arg_info
*, bool);
440 static void layout_array_type (tree
);
442 /* T is a statement. Add it to the statement-tree. This is the
443 C/ObjC version--C++ has a slightly different version of this
449 enum tree_code code
= TREE_CODE (t
);
451 if (CAN_HAVE_LOCATION_P (t
) && code
!= LABEL_EXPR
)
453 if (!EXPR_HAS_LOCATION (t
))
454 SET_EXPR_LOCATION (t
, input_location
);
457 if (code
== LABEL_EXPR
|| code
== CASE_LABEL_EXPR
)
458 STATEMENT_LIST_HAS_LABEL (cur_stmt_list
) = 1;
460 /* Add T to the statement-tree. Non-side-effect statements need to be
461 recorded during statement expressions. */
462 append_to_statement_list_force (t
, &cur_stmt_list
);
469 c_print_identifier (FILE *file
, tree node
, int indent
)
471 print_node (file
, "symbol", I_SYMBOL_DECL (node
), indent
+ 4);
472 print_node (file
, "tag", I_TAG_DECL (node
), indent
+ 4);
473 print_node (file
, "label", I_LABEL_DECL (node
), indent
+ 4);
474 if (C_IS_RESERVED_WORD (node
) && C_RID_CODE (node
) != RID_CXX_COMPAT_WARN
)
476 tree rid
= ridpointers
[C_RID_CODE (node
)];
477 indent_to (file
, indent
+ 4);
478 fprintf (file
, "rid %p \"%s\"",
479 (void *) rid
, IDENTIFIER_POINTER (rid
));
483 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
484 which may be any of several kinds of DECL or TYPE or error_mark_node,
485 in the scope SCOPE. */
487 bind (tree name
, tree decl
, struct c_scope
*scope
, bool invisible
,
488 bool nested
, location_t locus
)
490 struct c_binding
*b
, **here
;
492 if (binding_freelist
)
494 b
= binding_freelist
;
495 binding_freelist
= b
->prev
;
498 b
= GGC_NEW (struct c_binding
);
503 b
->depth
= scope
->depth
;
504 b
->invisible
= invisible
;
511 b
->prev
= scope
->bindings
;
517 switch (TREE_CODE (decl
))
519 case LABEL_DECL
: here
= &I_LABEL_BINDING (name
); break;
522 case RECORD_TYPE
: here
= &I_TAG_BINDING (name
); break;
528 case ERROR_MARK
: here
= &I_SYMBOL_BINDING (name
); break;
534 /* Locate the appropriate place in the chain of shadowed decls
535 to insert this binding. Normally, scope == current_scope and
536 this does nothing. */
537 while (*here
&& (*here
)->depth
> scope
->depth
)
538 here
= &(*here
)->shadowed
;
544 /* Clear the binding structure B, stick it on the binding_freelist,
545 and return the former value of b->prev. This is used by pop_scope
546 and get_parm_info to iterate destructively over all the bindings
547 from a given scope. */
548 static struct c_binding
*
549 free_binding_and_advance (struct c_binding
*b
)
551 struct c_binding
*prev
= b
->prev
;
553 memset (b
, 0, sizeof (struct c_binding
));
554 b
->prev
= binding_freelist
;
555 binding_freelist
= b
;
561 /* Hook called at end of compilation to assume 1 elt
562 for a file-scope tentative array defn that wasn't complete before. */
565 c_finish_incomplete_decl (tree decl
)
567 if (TREE_CODE (decl
) == VAR_DECL
)
569 tree type
= TREE_TYPE (decl
);
570 if (type
!= error_mark_node
571 && TREE_CODE (type
) == ARRAY_TYPE
572 && !DECL_EXTERNAL (decl
)
573 && TYPE_DOMAIN (type
) == 0)
575 warning (0, "array %q+D assumed to have one element", decl
);
577 complete_array_type (&TREE_TYPE (decl
), NULL_TREE
, true);
579 layout_decl (decl
, 0);
584 /* Record that inline function FUNC contains a reference (location
585 LOC) to static DECL (file-scope or function-local according to
589 record_inline_static (location_t loc
, tree func
, tree decl
,
590 enum c_inline_static_type type
)
592 struct c_inline_static
*csi
= GGC_NEW (struct c_inline_static
);
594 csi
->function
= func
;
595 csi
->static_decl
= decl
;
597 csi
->next
= c_inline_statics
;
598 c_inline_statics
= csi
;
601 /* Check for references to static declarations in inline functions at
602 the end of the translation unit and diagnose them if the functions
603 are still inline definitions. */
606 check_inline_statics (void)
608 struct c_inline_static
*csi
;
609 for (csi
= c_inline_statics
; csi
; csi
= csi
->next
)
611 if (DECL_EXTERNAL (csi
->function
))
615 pedwarn (csi
->location
, 0,
616 "%qD is static but used in inline function %qD "
617 "which is not static", csi
->static_decl
, csi
->function
);
620 pedwarn (csi
->location
, 0,
621 "%q+D is static but declared in inline function %qD "
622 "which is not static", csi
->static_decl
, csi
->function
);
628 c_inline_statics
= NULL
;
631 /* The Objective-C front-end often needs to determine the current scope. */
634 objc_get_current_scope (void)
636 return current_scope
;
639 /* The following function is used only by Objective-C. It needs to live here
640 because it accesses the innards of c_scope. */
643 objc_mark_locals_volatile (void *enclosing_blk
)
645 struct c_scope
*scope
;
648 for (scope
= current_scope
;
649 scope
&& scope
!= enclosing_blk
;
650 scope
= scope
->outer
)
652 for (b
= scope
->bindings
; b
; b
= b
->prev
)
653 objc_volatilize_decl (b
->decl
);
655 /* Do not climb up past the current function. */
656 if (scope
->function_body
)
661 /* Nonzero if we are currently in file scope. */
664 global_bindings_p (void)
666 return current_scope
== file_scope
&& !c_override_global_bindings_to_false
;
670 keep_next_level (void)
672 keep_next_level_flag
= true;
675 /* Identify this scope as currently being filled with parameters. */
678 declare_parm_level (void)
680 current_scope
->parm_flag
= true;
686 if (next_is_function_body
)
688 /* This is the transition from the parameters to the top level
689 of the function body. These are the same scope
690 (C99 6.2.1p4,6) so we do not push another scope structure.
691 next_is_function_body is set only by store_parm_decls, which
692 in turn is called when and only when we are about to
693 encounter the opening curly brace for the function body.
695 The outermost block of a function always gets a BLOCK node,
696 because the debugging output routines expect that each
697 function has at least one BLOCK. */
698 current_scope
->parm_flag
= false;
699 current_scope
->function_body
= true;
700 current_scope
->keep
= true;
701 current_scope
->outer_function
= current_function_scope
;
702 current_function_scope
= current_scope
;
704 keep_next_level_flag
= false;
705 next_is_function_body
= false;
709 struct c_scope
*scope
;
712 scope
= scope_freelist
;
713 scope_freelist
= scope
->outer
;
716 scope
= GGC_CNEW (struct c_scope
);
718 scope
->keep
= keep_next_level_flag
;
719 scope
->outer
= current_scope
;
720 scope
->depth
= current_scope
? (current_scope
->depth
+ 1) : 0;
722 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
724 if (current_scope
&& scope
->depth
== 0)
727 sorry ("GCC supports only %u nested scopes", scope
->depth
);
730 current_scope
= scope
;
731 keep_next_level_flag
= false;
735 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
738 set_type_context (tree type
, tree context
)
740 for (type
= TYPE_MAIN_VARIANT (type
); type
;
741 type
= TYPE_NEXT_VARIANT (type
))
742 TYPE_CONTEXT (type
) = context
;
745 /* Exit a scope. Restore the state of the identifier-decl mappings
746 that were in effect when this scope was entered. Return a BLOCK
747 node containing all the DECLs in this scope that are of interest
748 to debug info generation. */
753 struct c_scope
*scope
= current_scope
;
754 tree block
, context
, p
;
757 bool functionbody
= scope
->function_body
;
758 bool keep
= functionbody
|| scope
->keep
|| scope
->bindings
;
760 c_end_vm_scope (scope
->depth
);
762 /* If appropriate, create a BLOCK to record the decls for the life
767 block
= make_node (BLOCK
);
768 BLOCK_SUBBLOCKS (block
) = scope
->blocks
;
769 TREE_USED (block
) = 1;
771 /* In each subblock, record that this is its superior. */
772 for (p
= scope
->blocks
; p
; p
= BLOCK_CHAIN (p
))
773 BLOCK_SUPERCONTEXT (p
) = block
;
775 BLOCK_VARS (block
) = 0;
778 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
779 scope must be set so that they point to the appropriate
780 construct, i.e. either to the current FUNCTION_DECL node, or
781 else to the BLOCK node we just constructed.
783 Note that for tagged types whose scope is just the formal
784 parameter list for some function type specification, we can't
785 properly set their TYPE_CONTEXTs here, because we don't have a
786 pointer to the appropriate FUNCTION_TYPE node readily available
787 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
788 type nodes get set in `grokdeclarator' as soon as we have created
789 the FUNCTION_TYPE node which will represent the "scope" for these
790 "parameter list local" tagged types. */
791 if (scope
->function_body
)
792 context
= current_function_decl
;
793 else if (scope
== file_scope
)
795 tree file_decl
= build_decl (TRANSLATION_UNIT_DECL
, 0, 0);
796 TREE_CHAIN (file_decl
) = all_translation_units
;
797 all_translation_units
= file_decl
;
803 /* Clear all bindings in this scope. */
804 for (b
= scope
->bindings
; b
; b
= free_binding_and_advance (b
))
807 switch (TREE_CODE (p
))
810 /* Warnings for unused labels, errors for undefined labels. */
811 if (TREE_USED (p
) && !DECL_INITIAL (p
))
813 error ("label %q+D used but not defined", p
);
814 DECL_INITIAL (p
) = error_mark_node
;
817 warn_for_unused_label (p
);
819 /* Labels go in BLOCK_VARS. */
820 TREE_CHAIN (p
) = BLOCK_VARS (block
);
821 BLOCK_VARS (block
) = p
;
822 gcc_assert (I_LABEL_BINDING (b
->id
) == b
);
823 I_LABEL_BINDING (b
->id
) = b
->shadowed
;
829 set_type_context (p
, context
);
831 /* Types may not have tag-names, in which case the type
832 appears in the bindings list with b->id NULL. */
835 gcc_assert (I_TAG_BINDING (b
->id
) == b
);
836 I_TAG_BINDING (b
->id
) = b
->shadowed
;
841 /* Propagate TREE_ADDRESSABLE from nested functions to their
842 containing functions. */
843 if (!TREE_ASM_WRITTEN (p
)
844 && DECL_INITIAL (p
) != 0
845 && TREE_ADDRESSABLE (p
)
846 && DECL_ABSTRACT_ORIGIN (p
) != 0
847 && DECL_ABSTRACT_ORIGIN (p
) != p
)
848 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p
)) = 1;
849 if (!DECL_EXTERNAL (p
)
851 && scope
!= file_scope
852 && scope
!= external_scope
)
854 error ("nested function %q+D declared but never defined", p
);
855 undef_nested_function
= true;
857 else if (DECL_DECLARED_INLINE_P (p
)
859 && !DECL_INITIAL (p
))
861 /* C99 6.7.4p6: "a function with external linkage... declared
862 with an inline function specifier ... shall also be defined
863 in the same translation unit." */
864 if (!flag_gnu89_inline
)
865 pedwarn (input_location
, 0,
866 "inline function %q+D declared but never defined", p
);
867 DECL_EXTERNAL (p
) = 1;
873 /* Warnings for unused variables. */
875 && !TREE_NO_WARNING (p
)
876 && !DECL_IN_SYSTEM_HEADER (p
)
878 && !DECL_ARTIFICIAL (p
)
879 && scope
!= file_scope
880 && scope
!= external_scope
)
881 warning (OPT_Wunused_variable
, "unused variable %q+D", p
);
885 error ("type of array %q+D completed incompatibly with"
886 " implicit initialization", p
);
893 /* All of these go in BLOCK_VARS, but only if this is the
894 binding in the home scope. */
897 TREE_CHAIN (p
) = BLOCK_VARS (block
);
898 BLOCK_VARS (block
) = p
;
900 else if (VAR_OR_FUNCTION_DECL_P (p
))
902 /* For block local externs add a special
903 DECL_EXTERNAL decl for debug info generation. */
904 tree extp
= copy_node (p
);
906 DECL_EXTERNAL (extp
) = 1;
907 TREE_STATIC (extp
) = 0;
908 TREE_PUBLIC (extp
) = 1;
909 DECL_INITIAL (extp
) = NULL_TREE
;
910 DECL_LANG_SPECIFIC (extp
) = NULL
;
911 DECL_CONTEXT (extp
) = current_function_decl
;
912 if (TREE_CODE (p
) == FUNCTION_DECL
)
914 DECL_RESULT (extp
) = NULL_TREE
;
915 DECL_SAVED_TREE (extp
) = NULL_TREE
;
916 DECL_STRUCT_FUNCTION (extp
) = NULL
;
918 if (b
->locus
!= UNKNOWN_LOCATION
)
919 DECL_SOURCE_LOCATION (extp
) = b
->locus
;
920 TREE_CHAIN (extp
) = BLOCK_VARS (block
);
921 BLOCK_VARS (block
) = extp
;
923 /* If this is the file scope, and we are processing more
924 than one translation unit in this compilation, set
925 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
926 This makes same_translation_unit_p work, and causes
927 static declarations to be given disambiguating suffixes. */
928 if (scope
== file_scope
&& num_in_fnames
> 1)
930 DECL_CONTEXT (p
) = context
;
931 if (TREE_CODE (p
) == TYPE_DECL
)
932 set_type_context (TREE_TYPE (p
), context
);
936 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
937 already been put there by store_parm_decls. Unused-
938 parameter warnings are handled by function.c.
939 error_mark_node obviously does not go in BLOCK_VARS and
940 does not get unused-variable warnings. */
943 /* It is possible for a decl not to have a name. We get
944 here with b->id NULL in this case. */
947 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
948 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
949 if (b
->shadowed
&& b
->shadowed
->type
)
950 TREE_TYPE (b
->shadowed
->decl
) = b
->shadowed
->type
;
960 /* Dispose of the block that we just made inside some higher level. */
961 if ((scope
->function_body
|| scope
== file_scope
) && context
)
963 DECL_INITIAL (context
) = block
;
964 BLOCK_SUPERCONTEXT (block
) = context
;
966 else if (scope
->outer
)
969 SCOPE_LIST_APPEND (scope
->outer
, blocks
, block
);
970 /* If we did not make a block for the scope just exited, any
971 blocks made for inner scopes must be carried forward so they
972 will later become subblocks of something else. */
973 else if (scope
->blocks
)
974 SCOPE_LIST_CONCAT (scope
->outer
, blocks
, scope
, blocks
);
977 /* Pop the current scope, and free the structure for reuse. */
978 current_scope
= scope
->outer
;
979 if (scope
->function_body
)
980 current_function_scope
= scope
->outer_function
;
982 memset (scope
, 0, sizeof (struct c_scope
));
983 scope
->outer
= scope_freelist
;
984 scope_freelist
= scope
;
990 push_file_scope (void)
998 file_scope
= current_scope
;
1000 start_fname_decls ();
1002 for (decl
= visible_builtins
; decl
; decl
= TREE_CHAIN (decl
))
1003 bind (DECL_NAME (decl
), decl
, file_scope
,
1004 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl
));
1008 pop_file_scope (void)
1010 /* In case there were missing closebraces, get us back to the global
1012 while (current_scope
!= file_scope
)
1015 /* __FUNCTION__ is defined at file scope (""). This
1016 call may not be necessary as my tests indicate it
1017 still works without it. */
1018 finish_fname_decls ();
1020 check_inline_statics ();
1022 /* This is the point to write out a PCH if we're doing that.
1023 In that case we do not want to do anything else. */
1026 c_common_write_pch ();
1030 /* Pop off the file scope and close this translation unit. */
1034 maybe_apply_pending_pragma_weaks ();
1035 cgraph_finalize_compilation_unit ();
1039 /* Push a definition or a declaration of struct, union or enum tag "name".
1040 "type" should be the type node.
1041 We assume that the tag "name" is not already defined.
1043 Note that the definition may really be just a forward reference.
1044 In that case, the TYPE_SIZE will be zero. */
1047 pushtag (tree name
, tree type
)
1049 /* Record the identifier as the type's name if it has none. */
1050 if (name
&& !TYPE_NAME (type
))
1051 TYPE_NAME (type
) = name
;
1052 bind (name
, type
, current_scope
, /*invisible=*/false, /*nested=*/false,
1055 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1056 tagged type we just added to the current scope. This fake
1057 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1058 to output a representation of a tagged type, and it also gives
1059 us a convenient place to record the "scope start" address for the
1062 TYPE_STUB_DECL (type
) = pushdecl (build_decl (TYPE_DECL
, NULL_TREE
, type
));
1064 /* An approximation for now, so we can tell this is a function-scope tag.
1065 This will be updated in pop_scope. */
1066 TYPE_CONTEXT (type
) = DECL_CONTEXT (TYPE_STUB_DECL (type
));
1069 /* Subroutine of compare_decls. Allow harmless mismatches in return
1070 and argument types provided that the type modes match. This function
1071 return a unified type given a suitable match, and 0 otherwise. */
1074 match_builtin_function_types (tree newtype
, tree oldtype
)
1076 tree newrettype
, oldrettype
;
1077 tree newargs
, oldargs
;
1078 tree trytype
, tryargs
;
1080 /* Accept the return type of the new declaration if same modes. */
1081 oldrettype
= TREE_TYPE (oldtype
);
1082 newrettype
= TREE_TYPE (newtype
);
1084 if (TYPE_MODE (oldrettype
) != TYPE_MODE (newrettype
))
1087 oldargs
= TYPE_ARG_TYPES (oldtype
);
1088 newargs
= TYPE_ARG_TYPES (newtype
);
1091 while (oldargs
|| newargs
)
1095 || !TREE_VALUE (oldargs
)
1096 || !TREE_VALUE (newargs
)
1097 || TYPE_MODE (TREE_VALUE (oldargs
))
1098 != TYPE_MODE (TREE_VALUE (newargs
)))
1101 oldargs
= TREE_CHAIN (oldargs
);
1102 newargs
= TREE_CHAIN (newargs
);
1105 trytype
= build_function_type (newrettype
, tryargs
);
1106 return build_type_attribute_variant (trytype
, TYPE_ATTRIBUTES (oldtype
));
1109 /* Subroutine of diagnose_mismatched_decls. Check for function type
1110 mismatch involving an empty arglist vs a nonempty one and give clearer
1113 diagnose_arglist_conflict (tree newdecl
, tree olddecl
,
1114 tree newtype
, tree oldtype
)
1118 if (TREE_CODE (olddecl
) != FUNCTION_DECL
1119 || !comptypes (TREE_TYPE (oldtype
), TREE_TYPE (newtype
))
1120 || !((TYPE_ARG_TYPES (oldtype
) == 0 && DECL_INITIAL (olddecl
) == 0)
1122 (TYPE_ARG_TYPES (newtype
) == 0 && DECL_INITIAL (newdecl
) == 0)))
1125 t
= TYPE_ARG_TYPES (oldtype
);
1127 t
= TYPE_ARG_TYPES (newtype
);
1128 for (; t
; t
= TREE_CHAIN (t
))
1130 tree type
= TREE_VALUE (t
);
1132 if (TREE_CHAIN (t
) == 0
1133 && TYPE_MAIN_VARIANT (type
) != void_type_node
)
1135 inform (input_location
, "a parameter list with an ellipsis can%'t match "
1136 "an empty parameter name list declaration");
1140 if (c_type_promotes_to (type
) != type
)
1142 inform (input_location
, "an argument type that has a default promotion can%'t match "
1143 "an empty parameter name list declaration");
1149 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1150 old-style function definition, NEWDECL is a prototype declaration.
1151 Diagnose inconsistencies in the argument list. Returns TRUE if
1152 the prototype is compatible, FALSE if not. */
1154 validate_proto_after_old_defn (tree newdecl
, tree newtype
, tree oldtype
)
1156 tree newargs
, oldargs
;
1159 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1161 oldargs
= TYPE_ACTUAL_ARG_TYPES (oldtype
);
1162 newargs
= TYPE_ARG_TYPES (newtype
);
1167 tree oldargtype
= TREE_VALUE (oldargs
);
1168 tree newargtype
= TREE_VALUE (newargs
);
1170 if (oldargtype
== error_mark_node
|| newargtype
== error_mark_node
)
1173 oldargtype
= TYPE_MAIN_VARIANT (oldargtype
);
1174 newargtype
= TYPE_MAIN_VARIANT (newargtype
);
1176 if (END_OF_ARGLIST (oldargtype
) && END_OF_ARGLIST (newargtype
))
1179 /* Reaching the end of just one list means the two decls don't
1180 agree on the number of arguments. */
1181 if (END_OF_ARGLIST (oldargtype
))
1183 error ("prototype for %q+D declares more arguments "
1184 "than previous old-style definition", newdecl
);
1187 else if (END_OF_ARGLIST (newargtype
))
1189 error ("prototype for %q+D declares fewer arguments "
1190 "than previous old-style definition", newdecl
);
1194 /* Type for passing arg must be consistent with that declared
1196 else if (!comptypes (oldargtype
, newargtype
))
1198 error ("prototype for %q+D declares argument %d"
1199 " with incompatible type",
1204 oldargs
= TREE_CHAIN (oldargs
);
1205 newargs
= TREE_CHAIN (newargs
);
1209 /* If we get here, no errors were found, but do issue a warning
1210 for this poor-style construct. */
1211 warning (0, "prototype for %q+D follows non-prototype definition",
1214 #undef END_OF_ARGLIST
1217 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1218 first in a pair of mismatched declarations, using the diagnostic
1221 locate_old_decl (tree decl
)
1223 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (decl
))
1225 else if (DECL_INITIAL (decl
))
1226 inform (input_location
, "previous definition of %q+D was here", decl
);
1227 else if (C_DECL_IMPLICIT (decl
))
1228 inform (input_location
, "previous implicit declaration of %q+D was here", decl
);
1230 inform (input_location
, "previous declaration of %q+D was here", decl
);
1233 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1234 Returns true if the caller should proceed to merge the two, false
1235 if OLDDECL should simply be discarded. As a side effect, issues
1236 all necessary diagnostics for invalid or poor-style combinations.
1237 If it returns true, writes the types of NEWDECL and OLDDECL to
1238 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1239 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1242 diagnose_mismatched_decls (tree newdecl
, tree olddecl
,
1243 tree
*newtypep
, tree
*oldtypep
)
1245 tree newtype
, oldtype
;
1246 bool pedwarned
= false;
1247 bool warned
= false;
1250 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1251 && DECL_EXTERNAL (DECL))
1253 /* If we have error_mark_node for either decl or type, just discard
1254 the previous decl - we're in an error cascade already. */
1255 if (olddecl
== error_mark_node
|| newdecl
== error_mark_node
)
1257 *oldtypep
= oldtype
= TREE_TYPE (olddecl
);
1258 *newtypep
= newtype
= TREE_TYPE (newdecl
);
1259 if (oldtype
== error_mark_node
|| newtype
== error_mark_node
)
1262 /* Two different categories of symbol altogether. This is an error
1263 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1264 if (TREE_CODE (olddecl
) != TREE_CODE (newdecl
))
1266 if (!(TREE_CODE (olddecl
) == FUNCTION_DECL
1267 && DECL_BUILT_IN (olddecl
)
1268 && !C_DECL_DECLARED_BUILTIN (olddecl
)))
1270 error ("%q+D redeclared as different kind of symbol", newdecl
);
1271 locate_old_decl (olddecl
);
1273 else if (TREE_PUBLIC (newdecl
))
1274 warning (0, "built-in function %q+D declared as non-function",
1277 warning (OPT_Wshadow
, "declaration of %q+D shadows "
1278 "a built-in function", newdecl
);
1282 /* Enumerators have no linkage, so may only be declared once in a
1284 if (TREE_CODE (olddecl
) == CONST_DECL
)
1286 error ("redeclaration of enumerator %q+D", newdecl
);
1287 locate_old_decl (olddecl
);
1291 if (!comptypes (oldtype
, newtype
))
1293 if (TREE_CODE (olddecl
) == FUNCTION_DECL
1294 && DECL_BUILT_IN (olddecl
) && !C_DECL_DECLARED_BUILTIN (olddecl
))
1296 /* Accept harmless mismatch in function types.
1297 This is for the ffs and fprintf builtins. */
1298 tree trytype
= match_builtin_function_types (newtype
, oldtype
);
1300 if (trytype
&& comptypes (newtype
, trytype
))
1301 *oldtypep
= oldtype
= trytype
;
1304 /* If types don't match for a built-in, throw away the
1305 built-in. No point in calling locate_old_decl here, it
1306 won't print anything. */
1307 warning (0, "conflicting types for built-in function %q+D",
1312 else if (TREE_CODE (olddecl
) == FUNCTION_DECL
1313 && DECL_IS_BUILTIN (olddecl
))
1315 /* A conflicting function declaration for a predeclared
1316 function that isn't actually built in. Objective C uses
1317 these. The new declaration silently overrides everything
1318 but the volatility (i.e. noreturn) indication. See also
1319 below. FIXME: Make Objective C use normal builtins. */
1320 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
1323 /* Permit void foo (...) to match int foo (...) if the latter is
1324 the definition and implicit int was used. See
1325 c-torture/compile/920625-2.c. */
1326 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
&& DECL_INITIAL (newdecl
)
1327 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype
)) == void_type_node
1328 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype
)) == integer_type_node
1329 && C_FUNCTION_IMPLICIT_INT (newdecl
) && !DECL_INITIAL (olddecl
))
1331 pedwarned
= pedwarn (input_location
, 0,
1332 "conflicting types for %q+D", newdecl
);
1333 /* Make sure we keep void as the return type. */
1334 TREE_TYPE (newdecl
) = *newtypep
= newtype
= oldtype
;
1335 C_FUNCTION_IMPLICIT_INT (newdecl
) = 0;
1337 /* Permit void foo (...) to match an earlier call to foo (...) with
1338 no declared type (thus, implicitly int). */
1339 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
1340 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype
)) == void_type_node
1341 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype
)) == integer_type_node
1342 && C_DECL_IMPLICIT (olddecl
) && !DECL_INITIAL (olddecl
))
1344 pedwarned
= pedwarn (input_location
, 0,
1345 "conflicting types for %q+D", newdecl
);
1346 /* Make sure we keep void as the return type. */
1347 TREE_TYPE (olddecl
) = *oldtypep
= oldtype
= newtype
;
1351 if (TYPE_QUALS (newtype
) != TYPE_QUALS (oldtype
))
1352 error ("conflicting type qualifiers for %q+D", newdecl
);
1354 error ("conflicting types for %q+D", newdecl
);
1355 diagnose_arglist_conflict (newdecl
, olddecl
, newtype
, oldtype
);
1356 locate_old_decl (olddecl
);
1361 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1362 but silently ignore the redeclaration if either is in a system
1363 header. (Conflicting redeclarations were handled above.) */
1364 if (TREE_CODE (newdecl
) == TYPE_DECL
)
1366 if (DECL_IN_SYSTEM_HEADER (newdecl
)
1367 || DECL_IN_SYSTEM_HEADER (olddecl
)
1368 || TREE_NO_WARNING (newdecl
)
1369 || TREE_NO_WARNING (olddecl
))
1370 return true; /* Allow OLDDECL to continue in use. */
1372 error ("redefinition of typedef %q+D", newdecl
);
1373 locate_old_decl (olddecl
);
1377 /* Function declarations can either be 'static' or 'extern' (no
1378 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1379 can never conflict with each other on account of linkage
1380 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1381 gnu89 mode permits two definitions if one is 'extern inline' and
1382 one is not. The non- extern-inline definition supersedes the
1383 extern-inline definition. */
1385 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1387 /* If you declare a built-in function name as static, or
1388 define the built-in with an old-style definition (so we
1389 can't validate the argument list) the built-in definition is
1390 overridden, but optionally warn this was a bad choice of name. */
1391 if (DECL_BUILT_IN (olddecl
)
1392 && !C_DECL_DECLARED_BUILTIN (olddecl
)
1393 && (!TREE_PUBLIC (newdecl
)
1394 || (DECL_INITIAL (newdecl
)
1395 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl
)))))
1397 warning (OPT_Wshadow
, "declaration of %q+D shadows "
1398 "a built-in function", newdecl
);
1399 /* Discard the old built-in function. */
1403 if (DECL_INITIAL (newdecl
))
1405 if (DECL_INITIAL (olddecl
))
1407 /* If both decls are in the same TU and the new declaration
1408 isn't overriding an extern inline reject the new decl.
1409 In c99, no overriding is allowed in the same translation
1411 if ((!DECL_EXTERN_INLINE (olddecl
)
1412 || DECL_EXTERN_INLINE (newdecl
)
1413 || (!flag_gnu89_inline
1414 && (!DECL_DECLARED_INLINE_P (olddecl
)
1415 || !lookup_attribute ("gnu_inline",
1416 DECL_ATTRIBUTES (olddecl
)))
1417 && (!DECL_DECLARED_INLINE_P (newdecl
)
1418 || !lookup_attribute ("gnu_inline",
1419 DECL_ATTRIBUTES (newdecl
))))
1421 && same_translation_unit_p (newdecl
, olddecl
))
1423 error ("redefinition of %q+D", newdecl
);
1424 locate_old_decl (olddecl
);
1429 /* If we have a prototype after an old-style function definition,
1430 the argument types must be checked specially. */
1431 else if (DECL_INITIAL (olddecl
)
1432 && !TYPE_ARG_TYPES (oldtype
) && TYPE_ARG_TYPES (newtype
)
1433 && TYPE_ACTUAL_ARG_TYPES (oldtype
)
1434 && !validate_proto_after_old_defn (newdecl
, newtype
, oldtype
))
1436 locate_old_decl (olddecl
);
1439 /* A non-static declaration (even an "extern") followed by a
1440 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1441 The same is true for a static forward declaration at block
1442 scope followed by a non-static declaration/definition at file
1443 scope. Static followed by non-static at the same scope is
1444 not undefined behavior, and is the most convenient way to get
1445 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1446 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1447 we do diagnose it if -Wtraditional. */
1448 if (TREE_PUBLIC (olddecl
) && !TREE_PUBLIC (newdecl
))
1450 /* Two exceptions to the rule. If olddecl is an extern
1451 inline, or a predeclared function that isn't actually
1452 built in, newdecl silently overrides olddecl. The latter
1453 occur only in Objective C; see also above. (FIXME: Make
1454 Objective C use normal builtins.) */
1455 if (!DECL_IS_BUILTIN (olddecl
)
1456 && !DECL_EXTERN_INLINE (olddecl
))
1458 error ("static declaration of %q+D follows "
1459 "non-static declaration", newdecl
);
1460 locate_old_decl (olddecl
);
1464 else if (TREE_PUBLIC (newdecl
) && !TREE_PUBLIC (olddecl
))
1466 if (DECL_CONTEXT (olddecl
))
1468 error ("non-static declaration of %q+D follows "
1469 "static declaration", newdecl
);
1470 locate_old_decl (olddecl
);
1473 else if (warn_traditional
)
1475 warned
|= warning (OPT_Wtraditional
,
1476 "non-static declaration of %q+D "
1477 "follows static declaration", newdecl
);
1481 /* Make sure gnu_inline attribute is either not present, or
1482 present on all inline decls. */
1483 if (DECL_DECLARED_INLINE_P (olddecl
)
1484 && DECL_DECLARED_INLINE_P (newdecl
))
1486 bool newa
= lookup_attribute ("gnu_inline",
1487 DECL_ATTRIBUTES (newdecl
)) != NULL
;
1488 bool olda
= lookup_attribute ("gnu_inline",
1489 DECL_ATTRIBUTES (olddecl
)) != NULL
;
1492 error ("%<gnu_inline%> attribute present on %q+D",
1493 newa
? newdecl
: olddecl
);
1494 error ("%Jbut not here", newa
? olddecl
: newdecl
);
1498 else if (TREE_CODE (newdecl
) == VAR_DECL
)
1500 /* Only variables can be thread-local, and all declarations must
1501 agree on this property. */
1502 if (C_DECL_THREADPRIVATE_P (olddecl
) && !DECL_THREAD_LOCAL_P (newdecl
))
1504 /* Nothing to check. Since OLDDECL is marked threadprivate
1505 and NEWDECL does not have a thread-local attribute, we
1506 will merge the threadprivate attribute into NEWDECL. */
1509 else if (DECL_THREAD_LOCAL_P (newdecl
) != DECL_THREAD_LOCAL_P (olddecl
))
1511 if (DECL_THREAD_LOCAL_P (newdecl
))
1512 error ("thread-local declaration of %q+D follows "
1513 "non-thread-local declaration", newdecl
);
1515 error ("non-thread-local declaration of %q+D follows "
1516 "thread-local declaration", newdecl
);
1518 locate_old_decl (olddecl
);
1522 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1523 if (DECL_INITIAL (newdecl
) && DECL_INITIAL (olddecl
))
1525 error ("redefinition of %q+D", newdecl
);
1526 locate_old_decl (olddecl
);
1530 /* Objects declared at file scope: if the first declaration had
1531 external linkage (even if it was an external reference) the
1532 second must have external linkage as well, or the behavior is
1533 undefined. If the first declaration had internal linkage, then
1534 the second must too, or else be an external reference (in which
1535 case the composite declaration still has internal linkage).
1536 As for function declarations, we warn about the static-then-
1537 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1538 if (DECL_FILE_SCOPE_P (newdecl
)
1539 && TREE_PUBLIC (newdecl
) != TREE_PUBLIC (olddecl
))
1541 if (DECL_EXTERNAL (newdecl
))
1543 if (!DECL_FILE_SCOPE_P (olddecl
))
1545 error ("extern declaration of %q+D follows "
1546 "declaration with no linkage", newdecl
);
1547 locate_old_decl (olddecl
);
1550 else if (warn_traditional
)
1552 warned
|= warning (OPT_Wtraditional
,
1553 "non-static declaration of %q+D "
1554 "follows static declaration", newdecl
);
1559 if (TREE_PUBLIC (newdecl
))
1560 error ("non-static declaration of %q+D follows "
1561 "static declaration", newdecl
);
1563 error ("static declaration of %q+D follows "
1564 "non-static declaration", newdecl
);
1566 locate_old_decl (olddecl
);
1570 /* Two objects with the same name declared at the same block
1571 scope must both be external references (6.7p3). */
1572 else if (!DECL_FILE_SCOPE_P (newdecl
))
1574 if (DECL_EXTERNAL (newdecl
))
1576 /* Extern with initializer at block scope, which will
1577 already have received an error. */
1579 else if (DECL_EXTERNAL (olddecl
))
1581 error ("declaration of %q+D with no linkage follows "
1582 "extern declaration", newdecl
);
1583 locate_old_decl (olddecl
);
1587 error ("redeclaration of %q+D with no linkage", newdecl
);
1588 locate_old_decl (olddecl
);
1596 /* All decls must agree on a visibility. */
1597 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl
), TS_DECL_WITH_VIS
)
1598 && DECL_VISIBILITY_SPECIFIED (newdecl
) && DECL_VISIBILITY_SPECIFIED (olddecl
)
1599 && DECL_VISIBILITY (newdecl
) != DECL_VISIBILITY (olddecl
))
1601 warned
|= warning (0, "redeclaration of %q+D with different visibility "
1602 "(old visibility preserved)", newdecl
);
1605 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1607 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1608 if (DECL_DECLARED_INLINE_P (newdecl
)
1609 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl
)))
1611 warned
|= warning (OPT_Wattributes
,
1612 "inline declaration of %qD follows "
1613 "declaration with attribute noinline", newdecl
);
1615 else if (DECL_DECLARED_INLINE_P (olddecl
)
1616 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl
)))
1618 warned
|= warning (OPT_Wattributes
,
1619 "declaration of %q+D with attribute "
1620 "noinline follows inline declaration ", newdecl
);
1623 else /* PARM_DECL, VAR_DECL */
1625 /* Redeclaration of a parameter is a constraint violation (this is
1626 not explicitly stated, but follows from C99 6.7p3 [no more than
1627 one declaration of the same identifier with no linkage in the
1628 same scope, except type tags] and 6.2.2p6 [parameters have no
1629 linkage]). We must check for a forward parameter declaration,
1630 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1631 an extension, the mandatory diagnostic for which is handled by
1632 mark_forward_parm_decls. */
1634 if (TREE_CODE (newdecl
) == PARM_DECL
1635 && (!TREE_ASM_WRITTEN (olddecl
) || TREE_ASM_WRITTEN (newdecl
)))
1637 error ("redefinition of parameter %q+D", newdecl
);
1638 locate_old_decl (olddecl
);
1643 /* Optional warning for completely redundant decls. */
1644 if (!warned
&& !pedwarned
1645 && warn_redundant_decls
1646 /* Don't warn about a function declaration followed by a
1648 && !(TREE_CODE (newdecl
) == FUNCTION_DECL
1649 && DECL_INITIAL (newdecl
) && !DECL_INITIAL (olddecl
))
1650 /* Don't warn about redundant redeclarations of builtins. */
1651 && !(TREE_CODE (newdecl
) == FUNCTION_DECL
1652 && !DECL_BUILT_IN (newdecl
)
1653 && DECL_BUILT_IN (olddecl
)
1654 && !C_DECL_DECLARED_BUILTIN (olddecl
))
1655 /* Don't warn about an extern followed by a definition. */
1656 && !(DECL_EXTERNAL (olddecl
) && !DECL_EXTERNAL (newdecl
))
1657 /* Don't warn about forward parameter decls. */
1658 && !(TREE_CODE (newdecl
) == PARM_DECL
1659 && TREE_ASM_WRITTEN (olddecl
) && !TREE_ASM_WRITTEN (newdecl
))
1660 /* Don't warn about a variable definition following a declaration. */
1661 && !(TREE_CODE (newdecl
) == VAR_DECL
1662 && DECL_INITIAL (newdecl
) && !DECL_INITIAL (olddecl
)))
1664 warned
= warning (OPT_Wredundant_decls
, "redundant redeclaration of %q+D",
1668 /* Report location of previous decl/defn. */
1669 if (warned
|| pedwarned
)
1670 locate_old_decl (olddecl
);
1672 #undef DECL_EXTERN_INLINE
1677 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1678 consistent with OLDDECL, but carries new information. Merge the
1679 new information into OLDDECL. This function issues no
1683 merge_decls (tree newdecl
, tree olddecl
, tree newtype
, tree oldtype
)
1685 bool new_is_definition
= (TREE_CODE (newdecl
) == FUNCTION_DECL
1686 && DECL_INITIAL (newdecl
) != 0);
1687 bool new_is_prototype
= (TREE_CODE (newdecl
) == FUNCTION_DECL
1688 && TYPE_ARG_TYPES (TREE_TYPE (newdecl
)) != 0);
1689 bool old_is_prototype
= (TREE_CODE (olddecl
) == FUNCTION_DECL
1690 && TYPE_ARG_TYPES (TREE_TYPE (olddecl
)) != 0);
1691 bool extern_changed
= false;
1693 /* For real parm decl following a forward decl, rechain the old decl
1694 in its new location and clear TREE_ASM_WRITTEN (it's not a
1695 forward decl anymore). */
1696 if (TREE_CODE (newdecl
) == PARM_DECL
1697 && TREE_ASM_WRITTEN (olddecl
) && !TREE_ASM_WRITTEN (newdecl
))
1699 struct c_binding
*b
, **here
;
1701 for (here
= ¤t_scope
->bindings
; *here
; here
= &(*here
)->prev
)
1702 if ((*here
)->decl
== olddecl
)
1709 b
->prev
= current_scope
->bindings
;
1710 current_scope
->bindings
= b
;
1712 TREE_ASM_WRITTEN (olddecl
) = 0;
1715 DECL_ATTRIBUTES (newdecl
)
1716 = targetm
.merge_decl_attributes (olddecl
, newdecl
);
1718 /* Merge the data types specified in the two decls. */
1720 = TREE_TYPE (olddecl
)
1721 = composite_type (newtype
, oldtype
);
1723 /* Lay the type out, unless already done. */
1724 if (!comptypes (oldtype
, TREE_TYPE (newdecl
)))
1726 if (TREE_TYPE (newdecl
) != error_mark_node
)
1727 layout_type (TREE_TYPE (newdecl
));
1728 if (TREE_CODE (newdecl
) != FUNCTION_DECL
1729 && TREE_CODE (newdecl
) != TYPE_DECL
1730 && TREE_CODE (newdecl
) != CONST_DECL
)
1731 layout_decl (newdecl
, 0);
1735 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1736 DECL_SIZE (newdecl
) = DECL_SIZE (olddecl
);
1737 DECL_SIZE_UNIT (newdecl
) = DECL_SIZE_UNIT (olddecl
);
1738 DECL_MODE (newdecl
) = DECL_MODE (olddecl
);
1739 if (DECL_ALIGN (olddecl
) > DECL_ALIGN (newdecl
))
1741 DECL_ALIGN (newdecl
) = DECL_ALIGN (olddecl
);
1742 DECL_USER_ALIGN (newdecl
) |= DECL_USER_ALIGN (olddecl
);
1746 /* Keep the old rtl since we can safely use it. */
1747 if (HAS_RTL_P (olddecl
))
1748 COPY_DECL_RTL (olddecl
, newdecl
);
1750 /* Merge the type qualifiers. */
1751 if (TREE_READONLY (newdecl
))
1752 TREE_READONLY (olddecl
) = 1;
1754 if (TREE_THIS_VOLATILE (newdecl
))
1755 TREE_THIS_VOLATILE (olddecl
) = 1;
1757 /* Merge deprecatedness. */
1758 if (TREE_DEPRECATED (newdecl
))
1759 TREE_DEPRECATED (olddecl
) = 1;
1761 /* If a decl is in a system header and the other isn't, keep the one on the
1762 system header. Otherwise, keep source location of definition rather than
1763 declaration and of prototype rather than non-prototype unless that
1764 prototype is built-in. */
1765 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl
), TS_DECL_WITH_VIS
)
1766 && DECL_IN_SYSTEM_HEADER (olddecl
)
1767 && !DECL_IN_SYSTEM_HEADER (newdecl
) )
1768 DECL_SOURCE_LOCATION (newdecl
) = DECL_SOURCE_LOCATION (olddecl
);
1769 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl
), TS_DECL_WITH_VIS
)
1770 && DECL_IN_SYSTEM_HEADER (newdecl
)
1771 && !DECL_IN_SYSTEM_HEADER (olddecl
))
1772 DECL_SOURCE_LOCATION (olddecl
) = DECL_SOURCE_LOCATION (newdecl
);
1773 else if ((DECL_INITIAL (newdecl
) == 0 && DECL_INITIAL (olddecl
) != 0)
1774 || (old_is_prototype
&& !new_is_prototype
1775 && !C_DECL_BUILTIN_PROTOTYPE (olddecl
)))
1776 DECL_SOURCE_LOCATION (newdecl
) = DECL_SOURCE_LOCATION (olddecl
);
1778 /* Merge the initialization information. */
1779 if (DECL_INITIAL (newdecl
) == 0)
1780 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
1782 /* Merge the threadprivate attribute. */
1783 if (TREE_CODE (olddecl
) == VAR_DECL
&& C_DECL_THREADPRIVATE_P (olddecl
))
1785 DECL_TLS_MODEL (newdecl
) = DECL_TLS_MODEL (olddecl
);
1786 C_DECL_THREADPRIVATE_P (newdecl
) = 1;
1789 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl
), TS_DECL_WITH_VIS
))
1791 /* Merge the section attribute.
1792 We want to issue an error if the sections conflict but that
1793 must be done later in decl_attributes since we are called
1794 before attributes are assigned. */
1795 if (DECL_SECTION_NAME (newdecl
) == NULL_TREE
)
1796 DECL_SECTION_NAME (newdecl
) = DECL_SECTION_NAME (olddecl
);
1798 /* Copy the assembler name.
1799 Currently, it can only be defined in the prototype. */
1800 COPY_DECL_ASSEMBLER_NAME (olddecl
, newdecl
);
1802 /* Use visibility of whichever declaration had it specified */
1803 if (DECL_VISIBILITY_SPECIFIED (olddecl
))
1805 DECL_VISIBILITY (newdecl
) = DECL_VISIBILITY (olddecl
);
1806 DECL_VISIBILITY_SPECIFIED (newdecl
) = 1;
1809 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1811 DECL_STATIC_CONSTRUCTOR(newdecl
) |= DECL_STATIC_CONSTRUCTOR(olddecl
);
1812 DECL_STATIC_DESTRUCTOR (newdecl
) |= DECL_STATIC_DESTRUCTOR (olddecl
);
1813 DECL_NO_LIMIT_STACK (newdecl
) |= DECL_NO_LIMIT_STACK (olddecl
);
1814 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl
)
1815 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl
);
1816 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
1817 DECL_IS_MALLOC (newdecl
) |= DECL_IS_MALLOC (olddecl
);
1818 DECL_IS_OPERATOR_NEW (newdecl
) |= DECL_IS_OPERATOR_NEW (olddecl
);
1819 TREE_READONLY (newdecl
) |= TREE_READONLY (olddecl
);
1820 DECL_PURE_P (newdecl
) |= DECL_PURE_P (olddecl
);
1821 DECL_IS_NOVOPS (newdecl
) |= DECL_IS_NOVOPS (olddecl
);
1824 /* Merge the storage class information. */
1825 merge_weak (newdecl
, olddecl
);
1827 /* For functions, static overrides non-static. */
1828 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1830 TREE_PUBLIC (newdecl
) &= TREE_PUBLIC (olddecl
);
1831 /* This is since we don't automatically
1832 copy the attributes of NEWDECL into OLDDECL. */
1833 TREE_PUBLIC (olddecl
) = TREE_PUBLIC (newdecl
);
1834 /* If this clears `static', clear it in the identifier too. */
1835 if (!TREE_PUBLIC (olddecl
))
1836 TREE_PUBLIC (DECL_NAME (olddecl
)) = 0;
1840 /* In c99, 'extern' declaration before (or after) 'inline' means this
1841 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1843 if (TREE_CODE (newdecl
) == FUNCTION_DECL
1844 && !flag_gnu89_inline
1845 && (DECL_DECLARED_INLINE_P (newdecl
)
1846 || DECL_DECLARED_INLINE_P (olddecl
))
1847 && (!DECL_DECLARED_INLINE_P (newdecl
)
1848 || !DECL_DECLARED_INLINE_P (olddecl
)
1849 || !DECL_EXTERNAL (olddecl
))
1850 && DECL_EXTERNAL (newdecl
)
1851 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl
))
1852 && !current_function_decl
)
1853 DECL_EXTERNAL (newdecl
) = 0;
1855 if (DECL_EXTERNAL (newdecl
))
1857 TREE_STATIC (newdecl
) = TREE_STATIC (olddecl
);
1858 DECL_EXTERNAL (newdecl
) = DECL_EXTERNAL (olddecl
);
1860 /* An extern decl does not override previous storage class. */
1861 TREE_PUBLIC (newdecl
) = TREE_PUBLIC (olddecl
);
1862 if (!DECL_EXTERNAL (newdecl
))
1864 DECL_CONTEXT (newdecl
) = DECL_CONTEXT (olddecl
);
1865 DECL_COMMON (newdecl
) = DECL_COMMON (olddecl
);
1870 TREE_STATIC (olddecl
) = TREE_STATIC (newdecl
);
1871 TREE_PUBLIC (olddecl
) = TREE_PUBLIC (newdecl
);
1874 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1876 /* If we're redefining a function previously defined as extern
1877 inline, make sure we emit debug info for the inline before we
1878 throw it away, in case it was inlined into a function that
1879 hasn't been written out yet. */
1880 if (new_is_definition
&& DECL_INITIAL (olddecl
))
1881 /* The new defn must not be inline. */
1882 DECL_UNINLINABLE (newdecl
) = 1;
1885 /* If either decl says `inline', this fn is inline, unless
1886 its definition was passed already. */
1887 if (DECL_DECLARED_INLINE_P (newdecl
)
1888 || DECL_DECLARED_INLINE_P (olddecl
))
1889 DECL_DECLARED_INLINE_P (newdecl
) = 1;
1891 DECL_UNINLINABLE (newdecl
) = DECL_UNINLINABLE (olddecl
)
1892 = (DECL_UNINLINABLE (newdecl
) || DECL_UNINLINABLE (olddecl
));
1894 DECL_DISREGARD_INLINE_LIMITS (newdecl
)
1895 = DECL_DISREGARD_INLINE_LIMITS (olddecl
)
1896 = (DECL_DISREGARD_INLINE_LIMITS (newdecl
)
1897 || DECL_DISREGARD_INLINE_LIMITS (olddecl
));
1900 if (DECL_BUILT_IN (olddecl
))
1902 /* If redeclaring a builtin function, it stays built in.
1903 But it gets tagged as having been declared. */
1904 DECL_BUILT_IN_CLASS (newdecl
) = DECL_BUILT_IN_CLASS (olddecl
);
1905 DECL_FUNCTION_CODE (newdecl
) = DECL_FUNCTION_CODE (olddecl
);
1906 C_DECL_DECLARED_BUILTIN (newdecl
) = 1;
1907 if (new_is_prototype
)
1908 C_DECL_BUILTIN_PROTOTYPE (newdecl
) = 0;
1910 C_DECL_BUILTIN_PROTOTYPE (newdecl
)
1911 = C_DECL_BUILTIN_PROTOTYPE (olddecl
);
1914 /* Preserve function specific target and optimization options */
1915 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl
)
1916 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl
))
1917 DECL_FUNCTION_SPECIFIC_TARGET (newdecl
)
1918 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl
);
1920 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
)
1921 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
))
1922 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
)
1923 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
);
1925 /* Also preserve various other info from the definition. */
1926 if (!new_is_definition
)
1929 DECL_RESULT (newdecl
) = DECL_RESULT (olddecl
);
1930 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
1931 DECL_STRUCT_FUNCTION (newdecl
) = DECL_STRUCT_FUNCTION (olddecl
);
1932 DECL_SAVED_TREE (newdecl
) = DECL_SAVED_TREE (olddecl
);
1933 gimple_set_body (newdecl
, gimple_body (olddecl
));
1934 DECL_ARGUMENTS (newdecl
) = copy_list (DECL_ARGUMENTS (olddecl
));
1935 for (t
= DECL_ARGUMENTS (newdecl
); t
; t
= TREE_CHAIN (t
))
1936 DECL_CONTEXT (t
) = newdecl
;
1938 /* See if we've got a function to instantiate from. */
1939 if (DECL_SAVED_TREE (olddecl
))
1940 DECL_ABSTRACT_ORIGIN (newdecl
)
1941 = DECL_ABSTRACT_ORIGIN (olddecl
);
1945 extern_changed
= DECL_EXTERNAL (olddecl
) && !DECL_EXTERNAL (newdecl
);
1947 /* Merge the USED information. */
1948 if (TREE_USED (olddecl
))
1949 TREE_USED (newdecl
) = 1;
1950 else if (TREE_USED (newdecl
))
1951 TREE_USED (olddecl
) = 1;
1953 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1954 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1956 unsigned olddecl_uid
= DECL_UID (olddecl
);
1957 tree olddecl_context
= DECL_CONTEXT (olddecl
);
1958 tree olddecl_arguments
= NULL
;
1959 if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
1960 olddecl_arguments
= DECL_ARGUMENTS (olddecl
);
1962 memcpy ((char *) olddecl
+ sizeof (struct tree_common
),
1963 (char *) newdecl
+ sizeof (struct tree_common
),
1964 sizeof (struct tree_decl_common
) - sizeof (struct tree_common
));
1965 switch (TREE_CODE (olddecl
))
1968 gimple_set_body (olddecl
, gimple_body (newdecl
));
1978 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
1979 (char *) newdecl
+ sizeof (struct tree_decl_common
),
1980 tree_code_size (TREE_CODE (olddecl
)) - sizeof (struct tree_decl_common
));
1985 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
1986 (char *) newdecl
+ sizeof (struct tree_decl_common
),
1987 sizeof (struct tree_decl_non_common
) - sizeof (struct tree_decl_common
));
1989 DECL_UID (olddecl
) = olddecl_uid
;
1990 DECL_CONTEXT (olddecl
) = olddecl_context
;
1991 if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
1992 DECL_ARGUMENTS (olddecl
) = olddecl_arguments
;
1995 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1996 so that encode_section_info has a chance to look at the new decl
1997 flags and attributes. */
1998 if (DECL_RTL_SET_P (olddecl
)
1999 && (TREE_CODE (olddecl
) == FUNCTION_DECL
2000 || (TREE_CODE (olddecl
) == VAR_DECL
2001 && TREE_STATIC (olddecl
))))
2002 make_decl_rtl (olddecl
);
2004 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
2005 and the definition is coming from the old version, cgraph needs
2006 to be called again. */
2007 if (extern_changed
&& !new_is_definition
2008 && TREE_CODE (olddecl
) == FUNCTION_DECL
&& DECL_INITIAL (olddecl
))
2009 cgraph_mark_if_needed (olddecl
);
2012 /* Handle when a new declaration NEWDECL has the same name as an old
2013 one OLDDECL in the same binding contour. Prints an error message
2016 If safely possible, alter OLDDECL to look like NEWDECL, and return
2017 true. Otherwise, return false. */
2020 duplicate_decls (tree newdecl
, tree olddecl
)
2022 tree newtype
= NULL
, oldtype
= NULL
;
2024 if (!diagnose_mismatched_decls (newdecl
, olddecl
, &newtype
, &oldtype
))
2026 /* Avoid `unused variable' and other warnings for OLDDECL. */
2027 TREE_NO_WARNING (olddecl
) = 1;
2031 merge_decls (newdecl
, olddecl
, newtype
, oldtype
);
2036 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2038 warn_if_shadowing (tree new_decl
)
2040 struct c_binding
*b
;
2042 /* Shadow warnings wanted? */
2044 /* No shadow warnings for internally generated vars. */
2045 || DECL_IS_BUILTIN (new_decl
)
2046 /* No shadow warnings for vars made for inlining. */
2047 || DECL_FROM_INLINE (new_decl
))
2050 /* Is anything being shadowed? Invisible decls do not count. */
2051 for (b
= I_SYMBOL_BINDING (DECL_NAME (new_decl
)); b
; b
= b
->shadowed
)
2052 if (b
->decl
&& b
->decl
!= new_decl
&& !b
->invisible
)
2054 tree old_decl
= b
->decl
;
2056 if (old_decl
== error_mark_node
)
2058 warning (OPT_Wshadow
, "declaration of %q+D shadows previous "
2059 "non-variable", new_decl
);
2062 else if (TREE_CODE (old_decl
) == PARM_DECL
)
2063 warning (OPT_Wshadow
, "declaration of %q+D shadows a parameter",
2065 else if (DECL_FILE_SCOPE_P (old_decl
))
2066 warning (OPT_Wshadow
, "declaration of %q+D shadows a global "
2067 "declaration", new_decl
);
2068 else if (TREE_CODE (old_decl
) == FUNCTION_DECL
2069 && DECL_BUILT_IN (old_decl
))
2071 warning (OPT_Wshadow
, "declaration of %q+D shadows "
2072 "a built-in function", new_decl
);
2076 warning (OPT_Wshadow
, "declaration of %q+D shadows a previous local",
2079 warning (OPT_Wshadow
, "%Jshadowed declaration is here", old_decl
);
2085 /* Record a decl-node X as belonging to the current lexical scope.
2086 Check for errors (such as an incompatible declaration for the same
2087 name already seen in the same scope).
2089 Returns either X or an old decl for the same name.
2090 If an old decl is returned, it may have been smashed
2091 to agree with what X says. */
2096 tree name
= DECL_NAME (x
);
2097 struct c_scope
*scope
= current_scope
;
2098 struct c_binding
*b
;
2099 bool nested
= false;
2100 location_t locus
= DECL_SOURCE_LOCATION (x
);
2102 /* Must set DECL_CONTEXT for everything not at file scope or
2103 DECL_FILE_SCOPE_P won't work. Local externs don't count
2104 unless they have initializers (which generate code). */
2105 if (current_function_decl
2106 && ((TREE_CODE (x
) != FUNCTION_DECL
&& TREE_CODE (x
) != VAR_DECL
)
2107 || DECL_INITIAL (x
) || !DECL_EXTERNAL (x
)))
2108 DECL_CONTEXT (x
) = current_function_decl
;
2110 /* If this is of variably modified type, prevent jumping into its
2112 if ((TREE_CODE (x
) == VAR_DECL
|| TREE_CODE (x
) == TYPE_DECL
)
2113 && variably_modified_type_p (TREE_TYPE (x
), NULL_TREE
))
2114 c_begin_vm_scope (scope
->depth
);
2116 /* Anonymous decls are just inserted in the scope. */
2119 bind (name
, x
, scope
, /*invisible=*/false, /*nested=*/false,
2124 /* First, see if there is another declaration with the same name in
2125 the current scope. If there is, duplicate_decls may do all the
2126 work for us. If duplicate_decls returns false, that indicates
2127 two incompatible decls in the same scope; we are to silently
2128 replace the old one (duplicate_decls has issued all appropriate
2129 diagnostics). In particular, we should not consider possible
2130 duplicates in the external scope, or shadowing. */
2131 b
= I_SYMBOL_BINDING (name
);
2132 if (b
&& B_IN_SCOPE (b
, scope
))
2134 struct c_binding
*b_ext
, *b_use
;
2135 tree type
= TREE_TYPE (x
);
2136 tree visdecl
= b
->decl
;
2137 tree vistype
= TREE_TYPE (visdecl
);
2138 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
2139 && COMPLETE_TYPE_P (TREE_TYPE (x
)))
2140 b
->inner_comp
= false;
2143 /* If this is an external linkage declaration, we should check
2144 for compatibility with the type in the external scope before
2145 setting the type at this scope based on the visible
2146 information only. */
2147 if (TREE_PUBLIC (x
) && TREE_PUBLIC (visdecl
))
2149 while (b_ext
&& !B_IN_EXTERNAL_SCOPE (b_ext
))
2150 b_ext
= b_ext
->shadowed
;
2155 TREE_TYPE (b_use
->decl
) = b_use
->type
;
2158 if (duplicate_decls (x
, b_use
->decl
))
2162 /* Save the updated type in the external scope and
2163 restore the proper type for this scope. */
2165 if (comptypes (vistype
, type
))
2166 thistype
= composite_type (vistype
, type
);
2168 thistype
= TREE_TYPE (b_use
->decl
);
2169 b_use
->type
= TREE_TYPE (b_use
->decl
);
2170 if (TREE_CODE (b_use
->decl
) == FUNCTION_DECL
2171 && DECL_BUILT_IN (b_use
->decl
))
2173 = build_type_attribute_variant (thistype
,
2176 TREE_TYPE (b_use
->decl
) = thistype
;
2181 goto skip_external_and_shadow_checks
;
2184 /* All declarations with external linkage, and all external
2185 references, go in the external scope, no matter what scope is
2186 current. However, the binding in that scope is ignored for
2187 purposes of normal name lookup. A separate binding structure is
2188 created in the requested scope; this governs the normal
2189 visibility of the symbol.
2191 The binding in the externals scope is used exclusively for
2192 detecting duplicate declarations of the same object, no matter
2193 what scope they are in; this is what we do here. (C99 6.2.7p2:
2194 All declarations that refer to the same object or function shall
2195 have compatible type; otherwise, the behavior is undefined.) */
2196 if (DECL_EXTERNAL (x
) || scope
== file_scope
)
2198 tree type
= TREE_TYPE (x
);
2201 bool type_saved
= false;
2202 if (b
&& !B_IN_EXTERNAL_SCOPE (b
)
2203 && (TREE_CODE (b
->decl
) == FUNCTION_DECL
2204 || TREE_CODE (b
->decl
) == VAR_DECL
)
2205 && DECL_FILE_SCOPE_P (b
->decl
))
2208 vistype
= TREE_TYPE (visdecl
);
2210 if (scope
!= file_scope
2211 && !DECL_IN_SYSTEM_HEADER (x
))
2212 warning (OPT_Wnested_externs
, "nested extern declaration of %qD", x
);
2214 while (b
&& !B_IN_EXTERNAL_SCOPE (b
))
2216 /* If this decl might be modified, save its type. This is
2217 done here rather than when the decl is first bound
2218 because the type may change after first binding, through
2219 being completed or through attributes being added. If we
2220 encounter multiple such decls, only the first should have
2221 its type saved; the others will already have had their
2222 proper types saved and the types will not have changed as
2223 their scopes will not have been re-entered. */
2224 if (DECL_P (b
->decl
) && DECL_FILE_SCOPE_P (b
->decl
) && !type_saved
)
2226 b
->type
= TREE_TYPE (b
->decl
);
2229 if (B_IN_FILE_SCOPE (b
)
2230 && TREE_CODE (b
->decl
) == VAR_DECL
2231 && TREE_STATIC (b
->decl
)
2232 && TREE_CODE (TREE_TYPE (b
->decl
)) == ARRAY_TYPE
2233 && !TYPE_DOMAIN (TREE_TYPE (b
->decl
))
2234 && TREE_CODE (type
) == ARRAY_TYPE
2235 && TYPE_DOMAIN (type
)
2236 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
2237 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))
2239 /* Array type completed in inner scope, which should be
2240 diagnosed if the completion does not have size 1 and
2241 it does not get completed in the file scope. */
2242 b
->inner_comp
= true;
2247 /* If a matching external declaration has been found, set its
2248 type to the composite of all the types of that declaration.
2249 After the consistency checks, it will be reset to the
2250 composite of the visible types only. */
2251 if (b
&& (TREE_PUBLIC (x
) || same_translation_unit_p (x
, b
->decl
))
2253 TREE_TYPE (b
->decl
) = b
->type
;
2255 /* The point of the same_translation_unit_p check here is,
2256 we want to detect a duplicate decl for a construct like
2257 foo() { extern bar(); } ... static bar(); but not if
2258 they are in different translation units. In any case,
2259 the static does not go in the externals scope. */
2261 && (TREE_PUBLIC (x
) || same_translation_unit_p (x
, b
->decl
))
2262 && duplicate_decls (x
, b
->decl
))
2267 if (comptypes (vistype
, type
))
2268 thistype
= composite_type (vistype
, type
);
2270 thistype
= TREE_TYPE (b
->decl
);
2274 b
->type
= TREE_TYPE (b
->decl
);
2275 if (TREE_CODE (b
->decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (b
->decl
))
2277 = build_type_attribute_variant (thistype
,
2278 TYPE_ATTRIBUTES (b
->type
));
2279 TREE_TYPE (b
->decl
) = thistype
;
2280 bind (name
, b
->decl
, scope
, /*invisible=*/false, /*nested=*/true,
2284 else if (TREE_PUBLIC (x
))
2286 if (visdecl
&& !b
&& duplicate_decls (x
, visdecl
))
2288 /* An external declaration at block scope referring to a
2289 visible entity with internal linkage. The composite
2290 type will already be correct for this scope, so we
2291 just need to fall through to make the declaration in
2298 bind (name
, x
, external_scope
, /*invisible=*/true,
2299 /*nested=*/false, locus
);
2305 if (TREE_CODE (x
) != PARM_DECL
)
2306 warn_if_shadowing (x
);
2308 skip_external_and_shadow_checks
:
2309 if (TREE_CODE (x
) == TYPE_DECL
)
2310 set_underlying_type (x
);
2312 bind (name
, x
, scope
, /*invisible=*/false, nested
, locus
);
2314 /* If x's type is incomplete because it's based on a
2315 structure or union which has not yet been fully declared,
2316 attach it to that structure or union type, so we can go
2317 back and complete the variable declaration later, if the
2318 structure or union gets fully declared.
2320 If the input is erroneous, we can have error_mark in the type
2321 slot (e.g. "f(void a, ...)") - that doesn't count as an
2323 if (TREE_TYPE (x
) != error_mark_node
2324 && !COMPLETE_TYPE_P (TREE_TYPE (x
)))
2326 tree element
= TREE_TYPE (x
);
2328 while (TREE_CODE (element
) == ARRAY_TYPE
)
2329 element
= TREE_TYPE (element
);
2330 element
= TYPE_MAIN_VARIANT (element
);
2332 if ((TREE_CODE (element
) == RECORD_TYPE
2333 || TREE_CODE (element
) == UNION_TYPE
)
2334 && (TREE_CODE (x
) != TYPE_DECL
2335 || TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
)
2336 && !COMPLETE_TYPE_P (element
))
2337 C_TYPE_INCOMPLETE_VARS (element
)
2338 = tree_cons (NULL_TREE
, x
, C_TYPE_INCOMPLETE_VARS (element
));
2343 /* Record X as belonging to file scope.
2344 This is used only internally by the Objective-C front end,
2345 and is limited to its needs. duplicate_decls is not called;
2346 if there is any preexisting decl for this identifier, it is an ICE. */
2349 pushdecl_top_level (tree x
)
2352 bool nested
= false;
2353 gcc_assert (TREE_CODE (x
) == VAR_DECL
|| TREE_CODE (x
) == CONST_DECL
);
2355 name
= DECL_NAME (x
);
2357 gcc_assert (TREE_CODE (x
) == CONST_DECL
|| !I_SYMBOL_BINDING (name
));
2359 if (TREE_PUBLIC (x
))
2361 bind (name
, x
, external_scope
, /*invisible=*/true, /*nested=*/false,
2366 bind (name
, x
, file_scope
, /*invisible=*/false, nested
, UNKNOWN_LOCATION
);
2372 implicit_decl_warning (tree id
, tree olddecl
)
2374 if (warn_implicit_function_declaration
)
2379 warned
= pedwarn (input_location
, OPT_Wimplicit_function_declaration
,
2380 "implicit declaration of function %qE", id
);
2382 warned
= warning (OPT_Wimplicit_function_declaration
,
2383 G_("implicit declaration of function %qE"), id
);
2384 if (olddecl
&& warned
)
2385 locate_old_decl (olddecl
);
2389 /* Generate an implicit declaration for identifier FUNCTIONID as a
2390 function of type int (). */
2393 implicitly_declare (tree functionid
)
2395 struct c_binding
*b
;
2399 for (b
= I_SYMBOL_BINDING (functionid
); b
; b
= b
->shadowed
)
2401 if (B_IN_SCOPE (b
, external_scope
))
2410 if (decl
== error_mark_node
)
2413 /* FIXME: Objective-C has weird not-really-builtin functions
2414 which are supposed to be visible automatically. They wind up
2415 in the external scope because they're pushed before the file
2416 scope gets created. Catch this here and rebind them into the
2418 if (!DECL_BUILT_IN (decl
) && DECL_IS_BUILTIN (decl
))
2420 bind (functionid
, decl
, file_scope
,
2421 /*invisible=*/false, /*nested=*/true,
2422 DECL_SOURCE_LOCATION (decl
));
2427 tree newtype
= default_function_type
;
2429 TREE_TYPE (decl
) = b
->type
;
2430 /* Implicit declaration of a function already declared
2431 (somehow) in a different scope, or as a built-in.
2432 If this is the first time this has happened, warn;
2433 then recycle the old declaration but with the new type. */
2434 if (!C_DECL_IMPLICIT (decl
))
2436 implicit_decl_warning (functionid
, decl
);
2437 C_DECL_IMPLICIT (decl
) = 1;
2439 if (DECL_BUILT_IN (decl
))
2441 newtype
= build_type_attribute_variant (newtype
,
2443 (TREE_TYPE (decl
)));
2444 if (!comptypes (newtype
, TREE_TYPE (decl
)))
2446 warning (0, "incompatible implicit declaration of built-in"
2447 " function %qD", decl
);
2448 newtype
= TREE_TYPE (decl
);
2453 if (!comptypes (newtype
, TREE_TYPE (decl
)))
2455 error ("incompatible implicit declaration of function %qD",
2457 locate_old_decl (decl
);
2460 b
->type
= TREE_TYPE (decl
);
2461 TREE_TYPE (decl
) = newtype
;
2462 bind (functionid
, decl
, current_scope
,
2463 /*invisible=*/false, /*nested=*/true,
2464 DECL_SOURCE_LOCATION (decl
));
2469 /* Not seen before. */
2470 decl
= build_decl (FUNCTION_DECL
, functionid
, default_function_type
);
2471 DECL_EXTERNAL (decl
) = 1;
2472 TREE_PUBLIC (decl
) = 1;
2473 C_DECL_IMPLICIT (decl
) = 1;
2474 implicit_decl_warning (functionid
, 0);
2475 asmspec_tree
= maybe_apply_renaming_pragma (decl
, /*asmname=*/NULL
);
2477 set_user_assembler_name (decl
, TREE_STRING_POINTER (asmspec_tree
));
2479 /* C89 says implicit declarations are in the innermost block.
2480 So we record the decl in the standard fashion. */
2481 decl
= pushdecl (decl
);
2483 /* No need to call objc_check_decl here - it's a function type. */
2484 rest_of_decl_compilation (decl
, 0, 0);
2486 /* Write a record describing this implicit function declaration
2487 to the prototypes file (if requested). */
2488 gen_aux_info_record (decl
, 0, 1, 0);
2490 /* Possibly apply some default attributes to this implicit declaration. */
2491 decl_attributes (&decl
, NULL_TREE
, 0);
2496 /* Issue an error message for a reference to an undeclared variable
2497 ID, including a reference to a builtin outside of function-call
2498 context. Establish a binding of the identifier to error_mark_node
2499 in an appropriate scope, which will suppress further errors for the
2500 same identifier. The error message should be given location LOC. */
2502 undeclared_variable (tree id
, location_t loc
)
2504 static bool already
= false;
2505 struct c_scope
*scope
;
2507 if (current_function_decl
== 0)
2509 error ("%H%qE undeclared here (not in a function)", &loc
, id
);
2510 scope
= current_scope
;
2514 error ("%H%qE undeclared (first use in this function)", &loc
, id
);
2518 error ("%H(Each undeclared identifier is reported only once", &loc
);
2519 error ("%Hfor each function it appears in.)", &loc
);
2523 /* If we are parsing old-style parameter decls, current_function_decl
2524 will be nonnull but current_function_scope will be null. */
2525 scope
= current_function_scope
? current_function_scope
: current_scope
;
2527 bind (id
, error_mark_node
, scope
, /*invisible=*/false, /*nested=*/false,
2531 /* Subroutine of lookup_label, declare_label, define_label: construct a
2532 LABEL_DECL with all the proper frills. */
2535 make_label (tree name
, location_t location
)
2537 tree label
= build_decl (LABEL_DECL
, name
, void_type_node
);
2539 DECL_CONTEXT (label
) = current_function_decl
;
2540 DECL_MODE (label
) = VOIDmode
;
2541 DECL_SOURCE_LOCATION (label
) = location
;
2546 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2547 Create one if none exists so far for the current function.
2548 This is called when a label is used in a goto expression or
2549 has its address taken. */
2552 lookup_label (tree name
)
2556 if (current_function_decl
== 0)
2558 error ("label %qE referenced outside of any function", name
);
2562 /* Use a label already defined or ref'd with this name, but not if
2563 it is inherited from a containing function and wasn't declared
2565 label
= I_LABEL_DECL (name
);
2566 if (label
&& (DECL_CONTEXT (label
) == current_function_decl
2567 || C_DECLARED_LABEL_FLAG (label
)))
2569 /* If the label has only been declared, update its apparent
2570 location to point here, for better diagnostics if it
2571 turns out not to have been defined. */
2572 if (!TREE_USED (label
))
2573 DECL_SOURCE_LOCATION (label
) = input_location
;
2577 /* No label binding for that identifier; make one. */
2578 label
= make_label (name
, input_location
);
2580 /* Ordinary labels go in the current function scope. */
2581 bind (name
, label
, current_function_scope
,
2582 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
2586 /* Make a label named NAME in the current function, shadowing silently
2587 any that may be inherited from containing functions or containing
2588 scopes. This is called for __label__ declarations. */
2591 declare_label (tree name
)
2593 struct c_binding
*b
= I_LABEL_BINDING (name
);
2596 /* Check to make sure that the label hasn't already been declared
2598 if (b
&& B_IN_CURRENT_SCOPE (b
))
2600 error ("duplicate label declaration %qE", name
);
2601 locate_old_decl (b
->decl
);
2603 /* Just use the previous declaration. */
2607 label
= make_label (name
, input_location
);
2608 C_DECLARED_LABEL_FLAG (label
) = 1;
2610 /* Declared labels go in the current scope. */
2611 bind (name
, label
, current_scope
,
2612 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
2616 /* Define a label, specifying the location in the source file.
2617 Return the LABEL_DECL node for the label, if the definition is valid.
2618 Otherwise return 0. */
2621 define_label (location_t location
, tree name
)
2623 /* Find any preexisting label with this name. It is an error
2624 if that label has already been defined in this function, or
2625 if there is a containing function with a declared label with
2627 tree label
= I_LABEL_DECL (name
);
2628 struct c_label_list
*nlist_se
, *nlist_vm
;
2631 && ((DECL_CONTEXT (label
) == current_function_decl
2632 && DECL_INITIAL (label
) != 0)
2633 || (DECL_CONTEXT (label
) != current_function_decl
2634 && C_DECLARED_LABEL_FLAG (label
))))
2636 error ("%Hduplicate label %qD", &location
, label
);
2637 locate_old_decl (label
);
2640 else if (label
&& DECL_CONTEXT (label
) == current_function_decl
)
2642 /* The label has been used or declared already in this function,
2643 but not defined. Update its location to point to this
2645 if (C_DECL_UNDEFINABLE_STMT_EXPR (label
))
2646 error ("%Jjump into statement expression", label
);
2647 if (C_DECL_UNDEFINABLE_VM (label
))
2648 error ("%Jjump into scope of identifier with variably modified type",
2650 DECL_SOURCE_LOCATION (label
) = location
;
2654 /* No label binding for that identifier; make one. */
2655 label
= make_label (name
, location
);
2657 /* Ordinary labels go in the current function scope. */
2658 bind (name
, label
, current_function_scope
,
2659 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
2662 if (!in_system_header
&& lookup_name (name
))
2663 warning (OPT_Wtraditional
, "%Htraditional C lacks a separate namespace "
2664 "for labels, identifier %qE conflicts", &location
, name
);
2666 nlist_se
= XOBNEW (&parser_obstack
, struct c_label_list
);
2667 nlist_se
->next
= label_context_stack_se
->labels_def
;
2668 nlist_se
->label
= label
;
2669 label_context_stack_se
->labels_def
= nlist_se
;
2671 nlist_vm
= XOBNEW (&parser_obstack
, struct c_label_list
);
2672 nlist_vm
->next
= label_context_stack_vm
->labels_def
;
2673 nlist_vm
->label
= label
;
2674 label_context_stack_vm
->labels_def
= nlist_vm
;
2676 /* Mark label as having been defined. */
2677 DECL_INITIAL (label
) = error_mark_node
;
2681 /* Given NAME, an IDENTIFIER_NODE,
2682 return the structure (or union or enum) definition for that name.
2683 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2684 CODE says which kind of type the caller wants;
2685 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2686 If the wrong kind of type is found, an error is reported. */
2689 lookup_tag (enum tree_code code
, tree name
, int thislevel_only
)
2691 struct c_binding
*b
= I_TAG_BINDING (name
);
2697 /* We only care about whether it's in this level if
2698 thislevel_only was set or it might be a type clash. */
2699 if (thislevel_only
|| TREE_CODE (b
->decl
) != code
)
2701 /* For our purposes, a tag in the external scope is the same as
2702 a tag in the file scope. (Primarily relevant to Objective-C
2703 and its builtin structure tags, which get pushed before the
2704 file scope is created.) */
2705 if (B_IN_CURRENT_SCOPE (b
)
2706 || (current_scope
== file_scope
&& B_IN_EXTERNAL_SCOPE (b
)))
2710 if (thislevel_only
&& !thislevel
)
2713 if (TREE_CODE (b
->decl
) != code
)
2715 /* Definition isn't the kind we were looking for. */
2716 pending_invalid_xref
= name
;
2717 pending_invalid_xref_location
= input_location
;
2719 /* If in the same binding level as a declaration as a tag
2720 of a different type, this must not be allowed to
2721 shadow that tag, so give the error immediately.
2722 (For example, "struct foo; union foo;" is invalid.) */
2724 pending_xref_error ();
2729 /* Print an error message now
2730 for a recent invalid struct, union or enum cross reference.
2731 We don't print them immediately because they are not invalid
2732 when used in the `struct foo;' construct for shadowing. */
2735 pending_xref_error (void)
2737 if (pending_invalid_xref
!= 0)
2738 error ("%H%qE defined as wrong kind of tag",
2739 &pending_invalid_xref_location
, pending_invalid_xref
);
2740 pending_invalid_xref
= 0;
2744 /* Look up NAME in the current scope and its superiors
2745 in the namespace of variables, functions and typedefs.
2746 Return a ..._DECL node of some kind representing its definition,
2747 or return 0 if it is undefined. */
2750 lookup_name (tree name
)
2752 struct c_binding
*b
= I_SYMBOL_BINDING (name
);
2753 if (b
&& !b
->invisible
)
2758 /* Similar to `lookup_name' but look only at the indicated scope. */
2761 lookup_name_in_scope (tree name
, struct c_scope
*scope
)
2763 struct c_binding
*b
;
2765 for (b
= I_SYMBOL_BINDING (name
); b
; b
= b
->shadowed
)
2766 if (B_IN_SCOPE (b
, scope
))
2771 /* Create the predefined scalar types of C,
2772 and some nodes representing standard constants (0, 1, (void *) 0).
2773 Initialize the global scope.
2774 Make definitions for built-in primitive functions. */
2777 c_init_decl_processing (void)
2779 location_t save_loc
= input_location
;
2781 /* Initialize reserved words for parser. */
2784 current_function_decl
= 0;
2786 gcc_obstack_init (&parser_obstack
);
2788 /* Make the externals scope. */
2790 external_scope
= current_scope
;
2792 /* Declarations from c_common_nodes_and_builtins must not be associated
2793 with this input file, lest we get differences between using and not
2794 using preprocessed headers. */
2795 input_location
= BUILTINS_LOCATION
;
2797 build_common_tree_nodes (flag_signed_char
, false);
2799 c_common_nodes_and_builtins ();
2801 /* In C, comparisons and TRUTH_* expressions have type int. */
2802 truthvalue_type_node
= integer_type_node
;
2803 truthvalue_true_node
= integer_one_node
;
2804 truthvalue_false_node
= integer_zero_node
;
2806 /* Even in C99, which has a real boolean type. */
2807 pushdecl (build_decl (TYPE_DECL
, get_identifier ("_Bool"),
2808 boolean_type_node
));
2810 input_location
= save_loc
;
2812 pedantic_lvalues
= true;
2814 make_fname_decl
= c_make_fname_decl
;
2815 start_fname_decls ();
2818 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2819 decl, NAME is the initialization string and TYPE_DEP indicates whether
2820 NAME depended on the type of the function. As we don't yet implement
2821 delayed emission of static data, we mark the decl as emitted
2822 so it is not placed in the output. Anything using it must therefore pull
2823 out the STRING_CST initializer directly. FIXME. */
2826 c_make_fname_decl (tree id
, int type_dep
)
2828 const char *name
= fname_as_string (type_dep
);
2829 tree decl
, type
, init
;
2830 size_t length
= strlen (name
);
2832 type
= build_array_type (char_type_node
,
2833 build_index_type (size_int (length
)));
2834 type
= c_build_qualified_type (type
, TYPE_QUAL_CONST
);
2836 decl
= build_decl (VAR_DECL
, id
, type
);
2838 TREE_STATIC (decl
) = 1;
2839 TREE_READONLY (decl
) = 1;
2840 DECL_ARTIFICIAL (decl
) = 1;
2842 init
= build_string (length
+ 1, name
);
2843 free (CONST_CAST (char *, name
));
2844 TREE_TYPE (init
) = type
;
2845 DECL_INITIAL (decl
) = init
;
2847 TREE_USED (decl
) = 1;
2849 if (current_function_decl
2850 /* For invalid programs like this:
2853 const char* p = __FUNCTION__;
2855 the __FUNCTION__ is believed to appear in K&R style function
2856 parameter declarator. In that case we still don't have
2858 && (!errorcount
|| current_function_scope
))
2860 DECL_CONTEXT (decl
) = current_function_decl
;
2861 bind (id
, decl
, current_function_scope
,
2862 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
2865 finish_decl (decl
, init
, NULL_TREE
, NULL_TREE
);
2871 c_builtin_function (tree decl
)
2873 tree type
= TREE_TYPE (decl
);
2874 tree id
= DECL_NAME (decl
);
2876 const char *name
= IDENTIFIER_POINTER (id
);
2877 C_DECL_BUILTIN_PROTOTYPE (decl
) = (TYPE_ARG_TYPES (type
) != 0);
2879 /* Should never be called on a symbol with a preexisting meaning. */
2880 gcc_assert (!I_SYMBOL_BINDING (id
));
2882 bind (id
, decl
, external_scope
, /*invisible=*/true, /*nested=*/false,
2885 /* Builtins in the implementation namespace are made visible without
2886 needing to be explicitly declared. See push_file_scope. */
2887 if (name
[0] == '_' && (name
[1] == '_' || ISUPPER (name
[1])))
2889 TREE_CHAIN (decl
) = visible_builtins
;
2890 visible_builtins
= decl
;
2897 c_builtin_function_ext_scope (tree decl
)
2899 tree type
= TREE_TYPE (decl
);
2900 tree id
= DECL_NAME (decl
);
2902 const char *name
= IDENTIFIER_POINTER (id
);
2903 C_DECL_BUILTIN_PROTOTYPE (decl
) = (TYPE_ARG_TYPES (type
) != 0);
2905 /* Should never be called on a symbol with a preexisting meaning. */
2906 gcc_assert (!I_SYMBOL_BINDING (id
));
2908 bind (id
, decl
, external_scope
, /*invisible=*/false, /*nested=*/false,
2911 /* Builtins in the implementation namespace are made visible without
2912 needing to be explicitly declared. See push_file_scope. */
2913 if (name
[0] == '_' && (name
[1] == '_' || ISUPPER (name
[1])))
2915 TREE_CHAIN (decl
) = visible_builtins
;
2916 visible_builtins
= decl
;
2922 /* Called when a declaration is seen that contains no names to declare.
2923 If its type is a reference to a structure, union or enum inherited
2924 from a containing scope, shadow that tag name for the current scope
2925 with a forward reference.
2926 If its type defines a new named structure or union
2927 or defines an enum, it is valid but we need not do anything here.
2928 Otherwise, it is an error. */
2931 shadow_tag (const struct c_declspecs
*declspecs
)
2933 shadow_tag_warned (declspecs
, 0);
2936 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2939 shadow_tag_warned (const struct c_declspecs
*declspecs
, int warned
)
2941 bool found_tag
= false;
2943 if (declspecs
->type
&& !declspecs
->default_int_p
&& !declspecs
->typedef_p
)
2945 tree value
= declspecs
->type
;
2946 enum tree_code code
= TREE_CODE (value
);
2948 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
|| code
== ENUMERAL_TYPE
)
2949 /* Used to test also that TYPE_SIZE (value) != 0.
2950 That caused warning for `struct foo;' at top level in the file. */
2952 tree name
= TYPE_NAME (value
);
2957 if (declspecs
->restrict_p
)
2959 error ("invalid use of %<restrict%>");
2965 if (warned
!= 1 && code
!= ENUMERAL_TYPE
)
2966 /* Empty unnamed enum OK */
2968 pedwarn (input_location
, 0,
2969 "unnamed struct/union that defines no instances");
2973 else if (!declspecs
->tag_defined_p
2974 && declspecs
->storage_class
!= csc_none
)
2977 pedwarn (input_location
, 0,
2978 "empty declaration with storage class specifier "
2979 "does not redeclare tag");
2981 pending_xref_error ();
2983 else if (!declspecs
->tag_defined_p
2984 && (declspecs
->const_p
2985 || declspecs
->volatile_p
2986 || declspecs
->restrict_p
))
2989 pedwarn (input_location
, 0,
2990 "empty declaration with type qualifier "
2991 "does not redeclare tag");
2993 pending_xref_error ();
2997 pending_invalid_xref
= 0;
2998 t
= lookup_tag (code
, name
, 1);
3002 t
= make_node (code
);
3009 if (warned
!= 1 && !in_system_header
)
3011 pedwarn (input_location
, 0,
3012 "useless type name in empty declaration");
3017 else if (warned
!= 1 && !in_system_header
&& declspecs
->typedef_p
)
3019 pedwarn (input_location
, 0, "useless type name in empty declaration");
3023 pending_invalid_xref
= 0;
3025 if (declspecs
->inline_p
)
3027 error ("%<inline%> in empty declaration");
3031 if (current_scope
== file_scope
&& declspecs
->storage_class
== csc_auto
)
3033 error ("%<auto%> in file-scope empty declaration");
3037 if (current_scope
== file_scope
&& declspecs
->storage_class
== csc_register
)
3039 error ("%<register%> in file-scope empty declaration");
3043 if (!warned
&& !in_system_header
&& declspecs
->storage_class
!= csc_none
)
3045 warning (0, "useless storage class specifier in empty declaration");
3049 if (!warned
&& !in_system_header
&& declspecs
->thread_p
)
3051 warning (0, "useless %<__thread%> in empty declaration");
3055 if (!warned
&& !in_system_header
&& (declspecs
->const_p
3056 || declspecs
->volatile_p
3057 || declspecs
->restrict_p
))
3059 warning (0, "useless type qualifier in empty declaration");
3066 pedwarn (input_location
, 0, "empty declaration");
3071 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3072 bits. SPECS represents declaration specifiers that the grammar
3073 only permits to contain type qualifiers and attributes. */
3076 quals_from_declspecs (const struct c_declspecs
*specs
)
3078 int quals
= ((specs
->const_p
? TYPE_QUAL_CONST
: 0)
3079 | (specs
->volatile_p
? TYPE_QUAL_VOLATILE
: 0)
3080 | (specs
->restrict_p
? TYPE_QUAL_RESTRICT
: 0));
3081 gcc_assert (!specs
->type
3082 && !specs
->decl_attr
3083 && specs
->typespec_word
== cts_none
3084 && specs
->storage_class
== csc_none
3085 && !specs
->typedef_p
3086 && !specs
->explicit_signed_p
3087 && !specs
->deprecated_p
3089 && !specs
->long_long_p
3092 && !specs
->unsigned_p
3093 && !specs
->complex_p
3095 && !specs
->thread_p
);
3099 /* Construct an array declarator. EXPR is the expression inside [],
3100 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3101 applied to the pointer to which a parameter array is converted).
3102 STATIC_P is true if "static" is inside the [], false otherwise.
3103 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3104 length which is nevertheless a complete type, false otherwise. The
3105 field for the contained declarator is left to be filled in by
3106 set_array_declarator_inner. */
3108 struct c_declarator
*
3109 build_array_declarator (tree expr
, struct c_declspecs
*quals
, bool static_p
,
3112 struct c_declarator
*declarator
= XOBNEW (&parser_obstack
,
3113 struct c_declarator
);
3114 declarator
->kind
= cdk_array
;
3115 declarator
->declarator
= 0;
3116 declarator
->u
.array
.dimen
= expr
;
3119 declarator
->u
.array
.attrs
= quals
->attrs
;
3120 declarator
->u
.array
.quals
= quals_from_declspecs (quals
);
3124 declarator
->u
.array
.attrs
= NULL_TREE
;
3125 declarator
->u
.array
.quals
= 0;
3127 declarator
->u
.array
.static_p
= static_p
;
3128 declarator
->u
.array
.vla_unspec_p
= vla_unspec_p
;
3131 if (static_p
|| quals
!= NULL
)
3132 pedwarn (input_location
, OPT_pedantic
,
3133 "ISO C90 does not support %<static%> or type "
3134 "qualifiers in parameter array declarators");
3136 pedwarn (input_location
, OPT_pedantic
,
3137 "ISO C90 does not support %<[*]%> array declarators");
3141 if (!current_scope
->parm_flag
)
3144 error ("%<[*]%> not allowed in other than function prototype scope");
3145 declarator
->u
.array
.vla_unspec_p
= false;
3148 current_scope
->had_vla_unspec
= true;
3153 /* Set the contained declarator of an array declarator. DECL is the
3154 declarator, as constructed by build_array_declarator; INNER is what
3155 appears on the left of the []. */
3157 struct c_declarator
*
3158 set_array_declarator_inner (struct c_declarator
*decl
,
3159 struct c_declarator
*inner
)
3161 decl
->declarator
= inner
;
3165 /* INIT is a constructor that forms DECL's initializer. If the final
3166 element initializes a flexible array field, add the size of that
3167 initializer to DECL's size. */
3170 add_flexible_array_elts_to_size (tree decl
, tree init
)
3174 if (VEC_empty (constructor_elt
, CONSTRUCTOR_ELTS (init
)))
3177 elt
= VEC_last (constructor_elt
, CONSTRUCTOR_ELTS (init
))->value
;
3178 type
= TREE_TYPE (elt
);
3179 if (TREE_CODE (type
) == ARRAY_TYPE
3180 && TYPE_SIZE (type
) == NULL_TREE
3181 && TYPE_DOMAIN (type
) != NULL_TREE
3182 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
)
3184 complete_array_type (&type
, elt
, false);
3186 = size_binop (PLUS_EXPR
, DECL_SIZE (decl
), TYPE_SIZE (type
));
3187 DECL_SIZE_UNIT (decl
)
3188 = size_binop (PLUS_EXPR
, DECL_SIZE_UNIT (decl
), TYPE_SIZE_UNIT (type
));
3192 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3193 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3194 before the type name, and set *EXPR_CONST_OPERANDS, if
3195 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3196 appear in a constant expression. */
3199 groktypename (struct c_type_name
*type_name
, tree
*expr
,
3200 bool *expr_const_operands
)
3203 tree attrs
= type_name
->specs
->attrs
;
3205 type_name
->specs
->attrs
= NULL_TREE
;
3207 type
= grokdeclarator (type_name
->declarator
, type_name
->specs
, TYPENAME
,
3208 false, NULL
, &attrs
, expr
, expr_const_operands
,
3211 /* Apply attributes. */
3212 decl_attributes (&type
, attrs
, 0);
3217 /* Decode a declarator in an ordinary declaration or data definition.
3218 This is called as soon as the type information and variable name
3219 have been parsed, before parsing the initializer if any.
3220 Here we create the ..._DECL node, fill in its type,
3221 and put it on the list of decls for the current context.
3222 The ..._DECL node is returned as the value.
3224 Exception: for arrays where the length is not specified,
3225 the type is left null, to be filled in by `finish_decl'.
3227 Function definitions do not come here; they go to start_function
3228 instead. However, external and forward declarations of functions
3229 do go through here. Structure field declarations are done by
3230 grokfield and not through here. */
3233 start_decl (struct c_declarator
*declarator
, struct c_declspecs
*declspecs
,
3234 bool initialized
, tree attributes
)
3238 tree expr
= NULL_TREE
;
3239 enum deprecated_states deprecated_state
= DEPRECATED_NORMAL
;
3241 /* An object declared as __attribute__((deprecated)) suppresses
3242 warnings of uses of other deprecated items. */
3243 if (lookup_attribute ("deprecated", attributes
))
3244 deprecated_state
= DEPRECATED_SUPPRESS
;
3246 decl
= grokdeclarator (declarator
, declspecs
,
3247 NORMAL
, initialized
, NULL
, &attributes
, &expr
, NULL
,
3255 if (TREE_CODE (decl
) != FUNCTION_DECL
&& MAIN_NAME_P (DECL_NAME (decl
)))
3256 warning (OPT_Wmain
, "%q+D is usually a function", decl
);
3259 /* Is it valid for this decl to have an initializer at all?
3260 If not, set INITIALIZED to zero, which will indirectly
3261 tell 'finish_decl' to ignore the initializer once it is parsed. */
3262 switch (TREE_CODE (decl
))
3265 error ("typedef %qD is initialized (use __typeof__ instead)", decl
);
3270 error ("function %qD is initialized like a variable", decl
);
3275 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3276 error ("parameter %qD is initialized", decl
);
3281 /* Don't allow initializations for incomplete types except for
3282 arrays which might be completed by the initialization. */
3284 /* This can happen if the array size is an undefined macro.
3285 We already gave a warning, so we don't need another one. */
3286 if (TREE_TYPE (decl
) == error_mark_node
)
3288 else if (COMPLETE_TYPE_P (TREE_TYPE (decl
)))
3290 /* A complete type is ok if size is fixed. */
3292 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl
))) != INTEGER_CST
3293 || C_DECL_VARIABLE_SIZE (decl
))
3295 error ("variable-sized object may not be initialized");
3299 else if (TREE_CODE (TREE_TYPE (decl
)) != ARRAY_TYPE
)
3301 error ("variable %qD has initializer but incomplete type", decl
);
3304 else if (C_DECL_VARIABLE_SIZE (decl
))
3306 /* Although C99 is unclear about whether incomplete arrays
3307 of VLAs themselves count as VLAs, it does not make
3308 sense to permit them to be initialized given that
3309 ordinary VLAs may not be initialized. */
3310 error ("variable-sized object may not be initialized");
3317 if (current_scope
== file_scope
)
3318 TREE_STATIC (decl
) = 1;
3320 /* Tell 'pushdecl' this is an initialized decl
3321 even though we don't yet have the initializer expression.
3322 Also tell 'finish_decl' it may store the real initializer. */
3323 DECL_INITIAL (decl
) = error_mark_node
;
3326 /* If this is a function declaration, write a record describing it to the
3327 prototypes file (if requested). */
3329 if (TREE_CODE (decl
) == FUNCTION_DECL
)
3330 gen_aux_info_record (decl
, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl
)) != 0);
3332 /* ANSI specifies that a tentative definition which is not merged with
3333 a non-tentative definition behaves exactly like a definition with an
3334 initializer equal to zero. (Section 3.7.2)
3336 -fno-common gives strict ANSI behavior, though this tends to break
3337 a large body of code that grew up without this rule.
3339 Thread-local variables are never common, since there's no entrenched
3340 body of code to break, and it allows more efficient variable references
3341 in the presence of dynamic linking. */
3343 if (TREE_CODE (decl
) == VAR_DECL
3345 && TREE_PUBLIC (decl
)
3346 && !DECL_THREAD_LOCAL_P (decl
)
3348 DECL_COMMON (decl
) = 1;
3350 /* Set attributes here so if duplicate decl, will have proper attributes. */
3351 decl_attributes (&decl
, attributes
, 0);
3353 /* Handle gnu_inline attribute. */
3354 if (declspecs
->inline_p
3355 && !flag_gnu89_inline
3356 && TREE_CODE (decl
) == FUNCTION_DECL
3357 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl
))
3358 || current_function_decl
))
3360 if (declspecs
->storage_class
== csc_auto
&& current_scope
!= file_scope
)
3362 else if (declspecs
->storage_class
!= csc_static
)
3363 DECL_EXTERNAL (decl
) = !DECL_EXTERNAL (decl
);
3366 if (TREE_CODE (decl
) == FUNCTION_DECL
3367 && targetm
.calls
.promote_prototypes (TREE_TYPE (decl
)))
3369 struct c_declarator
*ce
= declarator
;
3371 if (ce
->kind
== cdk_pointer
)
3372 ce
= declarator
->declarator
;
3373 if (ce
->kind
== cdk_function
)
3375 tree args
= ce
->u
.arg_info
->parms
;
3376 for (; args
; args
= TREE_CHAIN (args
))
3378 tree type
= TREE_TYPE (args
);
3379 if (type
&& INTEGRAL_TYPE_P (type
)
3380 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
3381 DECL_ARG_TYPE (args
) = integer_type_node
;
3386 if (TREE_CODE (decl
) == FUNCTION_DECL
3387 && DECL_DECLARED_INLINE_P (decl
)
3388 && DECL_UNINLINABLE (decl
)
3389 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl
)))
3390 warning (OPT_Wattributes
, "inline function %q+D given attribute noinline",
3393 /* C99 6.7.4p3: An inline definition of a function with external
3394 linkage shall not contain a definition of a modifiable object
3395 with static storage duration... */
3396 if (TREE_CODE (decl
) == VAR_DECL
3397 && current_scope
!= file_scope
3398 && TREE_STATIC (decl
)
3399 && !TREE_READONLY (decl
)
3400 && DECL_DECLARED_INLINE_P (current_function_decl
)
3401 && DECL_EXTERNAL (current_function_decl
))
3402 record_inline_static (input_location
, current_function_decl
,
3403 decl
, csi_modifiable
);
3405 /* Add this decl to the current scope.
3406 TEM may equal DECL or it may be a previous decl of the same name. */
3407 tem
= pushdecl (decl
);
3409 if (initialized
&& DECL_EXTERNAL (tem
))
3411 DECL_EXTERNAL (tem
) = 0;
3412 TREE_STATIC (tem
) = 1;
3418 /* Initialize EH if not initialized yet and exceptions are enabled. */
3421 c_maybe_initialize_eh (void)
3423 if (!flag_exceptions
|| c_eh_initialized_p
)
3426 c_eh_initialized_p
= true;
3427 eh_personality_libfunc
3428 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3429 ? "__gcc_personality_sj0"
3430 : "__gcc_personality_v0");
3431 default_init_unwind_resume_libfunc ();
3432 using_eh_for_cleanups ();
3435 /* Finish processing of a declaration;
3436 install its initial value.
3437 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
3438 If the length of an array type is not known before,
3439 it must be determined now, from the initial value, or it is an error. */
3442 finish_decl (tree decl
, tree init
, tree origtype
, tree asmspec_tree
)
3445 int was_incomplete
= (DECL_SIZE (decl
) == 0);
3446 const char *asmspec
= 0;
3448 /* If a name was specified, get the string. */
3449 if ((TREE_CODE (decl
) == FUNCTION_DECL
|| TREE_CODE (decl
) == VAR_DECL
)
3450 && DECL_FILE_SCOPE_P (decl
))
3451 asmspec_tree
= maybe_apply_renaming_pragma (decl
, asmspec_tree
);
3453 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
3455 /* If `start_decl' didn't like having an initialization, ignore it now. */
3456 if (init
!= 0 && DECL_INITIAL (decl
) == 0)
3459 /* Don't crash if parm is initialized. */
3460 if (TREE_CODE (decl
) == PARM_DECL
)
3464 store_init_value (decl
, init
, origtype
);
3466 if (c_dialect_objc () && (TREE_CODE (decl
) == VAR_DECL
3467 || TREE_CODE (decl
) == FUNCTION_DECL
3468 || TREE_CODE (decl
) == FIELD_DECL
))
3469 objc_check_decl (decl
);
3471 type
= TREE_TYPE (decl
);
3473 /* Deduce size of array from initialization, if not already known. */
3474 if (TREE_CODE (type
) == ARRAY_TYPE
3475 && TYPE_DOMAIN (type
) == 0
3476 && TREE_CODE (decl
) != TYPE_DECL
)
3479 = (TREE_STATIC (decl
)
3480 /* Even if pedantic, an external linkage array
3481 may have incomplete type at first. */
3482 ? pedantic
&& !TREE_PUBLIC (decl
)
3483 : !DECL_EXTERNAL (decl
));
3485 = complete_array_type (&TREE_TYPE (decl
), DECL_INITIAL (decl
),
3488 /* Get the completed type made by complete_array_type. */
3489 type
= TREE_TYPE (decl
);
3494 error ("initializer fails to determine size of %q+D", decl
);
3499 error ("array size missing in %q+D", decl
);
3500 /* If a `static' var's size isn't known,
3501 make it extern as well as static, so it does not get
3503 If it is not `static', then do not mark extern;
3504 finish_incomplete_decl will give it a default size
3505 and it will get allocated. */
3506 else if (!pedantic
&& TREE_STATIC (decl
) && !TREE_PUBLIC (decl
))
3507 DECL_EXTERNAL (decl
) = 1;
3511 error ("zero or negative size array %q+D", decl
);
3515 /* For global variables, update the copy of the type that
3516 exists in the binding. */
3517 if (TREE_PUBLIC (decl
))
3519 struct c_binding
*b_ext
= I_SYMBOL_BINDING (DECL_NAME (decl
));
3520 while (b_ext
&& !B_IN_EXTERNAL_SCOPE (b_ext
))
3521 b_ext
= b_ext
->shadowed
;
3525 b_ext
->type
= composite_type (b_ext
->type
, type
);
3536 if (DECL_INITIAL (decl
))
3537 TREE_TYPE (DECL_INITIAL (decl
)) = type
;
3539 layout_decl (decl
, 0);
3542 if (TREE_CODE (decl
) == VAR_DECL
)
3544 if (init
&& TREE_CODE (init
) == CONSTRUCTOR
)
3545 add_flexible_array_elts_to_size (decl
, init
);
3547 if (DECL_SIZE (decl
) == 0 && TREE_TYPE (decl
) != error_mark_node
3548 && COMPLETE_TYPE_P (TREE_TYPE (decl
)))
3549 layout_decl (decl
, 0);
3551 if (DECL_SIZE (decl
) == 0
3552 /* Don't give an error if we already gave one earlier. */
3553 && TREE_TYPE (decl
) != error_mark_node
3554 && (TREE_STATIC (decl
)
3555 /* A static variable with an incomplete type
3556 is an error if it is initialized.
3557 Also if it is not file scope.
3558 Otherwise, let it through, but if it is not `extern'
3559 then it may cause an error message later. */
3560 ? (DECL_INITIAL (decl
) != 0
3561 || !DECL_FILE_SCOPE_P (decl
))
3562 /* An automatic variable with an incomplete type
3564 : !DECL_EXTERNAL (decl
)))
3566 error ("storage size of %q+D isn%'t known", decl
);
3567 TREE_TYPE (decl
) = error_mark_node
;
3570 if ((DECL_EXTERNAL (decl
) || TREE_STATIC (decl
))
3571 && DECL_SIZE (decl
) != 0)
3573 if (TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
)
3574 constant_expression_warning (DECL_SIZE (decl
));
3577 error ("storage size of %q+D isn%'t constant", decl
);
3578 TREE_TYPE (decl
) = error_mark_node
;
3582 if (TREE_USED (type
))
3583 TREE_USED (decl
) = 1;
3586 /* If this is a function and an assembler name is specified, reset DECL_RTL
3587 so we can give it its new name. Also, update built_in_decls if it
3588 was a normal built-in. */
3589 if (TREE_CODE (decl
) == FUNCTION_DECL
&& asmspec
)
3591 if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
3592 set_builtin_user_assembler_name (decl
, asmspec
);
3593 set_user_assembler_name (decl
, asmspec
);
3596 /* If #pragma weak was used, mark the decl weak now. */
3597 maybe_apply_pragma_weak (decl
);
3599 /* Output the assembler code and/or RTL code for variables and functions,
3600 unless the type is an undefined structure or union.
3601 If not, it will get done when the type is completed. */
3603 if (TREE_CODE (decl
) == VAR_DECL
|| TREE_CODE (decl
) == FUNCTION_DECL
)
3605 /* Determine the ELF visibility. */
3606 if (TREE_PUBLIC (decl
))
3607 c_determine_visibility (decl
);
3609 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3610 if (c_dialect_objc ())
3611 objc_check_decl (decl
);
3615 /* If this is not a static variable, issue a warning.
3616 It doesn't make any sense to give an ASMSPEC for an
3617 ordinary, non-register local variable. Historically,
3618 GCC has accepted -- but ignored -- the ASMSPEC in
3620 if (!DECL_FILE_SCOPE_P (decl
)
3621 && TREE_CODE (decl
) == VAR_DECL
3622 && !C_DECL_REGISTER (decl
)
3623 && !TREE_STATIC (decl
))
3624 warning (0, "ignoring asm-specifier for non-static local "
3625 "variable %q+D", decl
);
3627 set_user_assembler_name (decl
, asmspec
);
3630 if (DECL_FILE_SCOPE_P (decl
))
3632 if (DECL_INITIAL (decl
) == NULL_TREE
3633 || DECL_INITIAL (decl
) == error_mark_node
)
3634 /* Don't output anything
3635 when a tentative file-scope definition is seen.
3636 But at end of compilation, do output code for them. */
3637 DECL_DEFER_OUTPUT (decl
) = 1;
3638 rest_of_decl_compilation (decl
, true, 0);
3642 /* In conjunction with an ASMSPEC, the `register'
3643 keyword indicates that we should place the variable
3644 in a particular register. */
3645 if (asmspec
&& C_DECL_REGISTER (decl
))
3647 DECL_HARD_REGISTER (decl
) = 1;
3648 /* This cannot be done for a structure with volatile
3649 fields, on which DECL_REGISTER will have been
3651 if (!DECL_REGISTER (decl
))
3652 error ("cannot put object with volatile field into register");
3655 if (TREE_CODE (decl
) != FUNCTION_DECL
)
3657 /* If we're building a variable sized type, and we might be
3658 reachable other than via the top of the current binding
3659 level, then create a new BIND_EXPR so that we deallocate
3660 the object at the right time. */
3661 /* Note that DECL_SIZE can be null due to errors. */
3662 if (DECL_SIZE (decl
)
3663 && !TREE_CONSTANT (DECL_SIZE (decl
))
3664 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list
))
3667 bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, NULL
);
3668 TREE_SIDE_EFFECTS (bind
) = 1;
3670 BIND_EXPR_BODY (bind
) = push_stmt_list ();
3672 add_stmt (build_stmt (DECL_EXPR
, decl
));
3677 if (!DECL_FILE_SCOPE_P (decl
))
3679 /* Recompute the RTL of a local array now
3680 if it used to be an incomplete type. */
3682 && !TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
3684 /* If we used it already as memory, it must stay in memory. */
3685 TREE_ADDRESSABLE (decl
) = TREE_USED (decl
);
3686 /* If it's still incomplete now, no init will save it. */
3687 if (DECL_SIZE (decl
) == 0)
3688 DECL_INITIAL (decl
) = 0;
3693 if (TREE_CODE (decl
) == TYPE_DECL
)
3695 if (!DECL_FILE_SCOPE_P (decl
)
3696 && variably_modified_type_p (TREE_TYPE (decl
), NULL_TREE
))
3697 add_stmt (build_stmt (DECL_EXPR
, decl
));
3699 rest_of_decl_compilation (decl
, DECL_FILE_SCOPE_P (decl
), 0);
3702 /* At the end of a declaration, throw away any variable type sizes
3703 of types defined inside that declaration. There is no use
3704 computing them in the following function definition. */
3705 if (current_scope
== file_scope
)
3706 get_pending_sizes ();
3708 /* Install a cleanup (aka destructor) if one was given. */
3709 if (TREE_CODE (decl
) == VAR_DECL
&& !TREE_STATIC (decl
))
3711 tree attr
= lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl
));
3714 tree cleanup_id
= TREE_VALUE (TREE_VALUE (attr
));
3715 tree cleanup_decl
= lookup_name (cleanup_id
);
3719 /* Build "cleanup(&decl)" for the destructor. */
3720 cleanup
= build_unary_op (input_location
, ADDR_EXPR
, decl
, 0);
3721 vec
= VEC_alloc (tree
, gc
, 1);
3722 VEC_quick_push (tree
, vec
, cleanup
);
3723 cleanup
= build_function_call_vec (cleanup_decl
, vec
, NULL
);
3724 VEC_free (tree
, gc
, vec
);
3726 /* Don't warn about decl unused; the cleanup uses it. */
3727 TREE_USED (decl
) = 1;
3728 TREE_USED (cleanup_decl
) = 1;
3730 /* Initialize EH, if we've been told to do so. */
3731 c_maybe_initialize_eh ();
3733 push_cleanup (decl
, cleanup
, false);
3738 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3741 grokparm (const struct c_parm
*parm
)
3743 tree attrs
= parm
->attrs
;
3744 tree decl
= grokdeclarator (parm
->declarator
, parm
->specs
, PARM
, false,
3745 NULL
, &attrs
, NULL
, NULL
, DEPRECATED_NORMAL
);
3747 decl_attributes (&decl
, attrs
, 0);
3752 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3753 and push that on the current scope. */
3756 push_parm_decl (const struct c_parm
*parm
)
3758 tree attrs
= parm
->attrs
;
3761 decl
= grokdeclarator (parm
->declarator
, parm
->specs
, PARM
, false, NULL
,
3762 &attrs
, NULL
, NULL
, DEPRECATED_NORMAL
);
3763 decl_attributes (&decl
, attrs
, 0);
3765 decl
= pushdecl (decl
);
3767 finish_decl (decl
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
3770 /* Mark all the parameter declarations to date as forward decls.
3771 Also diagnose use of this extension. */
3774 mark_forward_parm_decls (void)
3776 struct c_binding
*b
;
3778 if (pedantic
&& !current_scope
->warned_forward_parm_decls
)
3780 pedwarn (input_location
, OPT_pedantic
,
3781 "ISO C forbids forward parameter declarations");
3782 current_scope
->warned_forward_parm_decls
= true;
3785 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
3786 if (TREE_CODE (b
->decl
) == PARM_DECL
)
3787 TREE_ASM_WRITTEN (b
->decl
) = 1;
3790 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3791 literal, which may be an incomplete array type completed by the
3792 initializer; INIT is a CONSTRUCTOR that initializes the compound
3793 literal. NON_CONST is true if the initializers contain something
3794 that cannot occur in a constant expression. */
3797 build_compound_literal (tree type
, tree init
, bool non_const
)
3799 /* We do not use start_decl here because we have a type, not a declarator;
3800 and do not use finish_decl because the decl should be stored inside
3801 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3806 if (type
== error_mark_node
)
3807 return error_mark_node
;
3809 decl
= build_decl (VAR_DECL
, NULL_TREE
, type
);
3810 DECL_EXTERNAL (decl
) = 0;
3811 TREE_PUBLIC (decl
) = 0;
3812 TREE_STATIC (decl
) = (current_scope
== file_scope
);
3813 DECL_CONTEXT (decl
) = current_function_decl
;
3814 TREE_USED (decl
) = 1;
3815 TREE_TYPE (decl
) = type
;
3816 TREE_READONLY (decl
) = TYPE_READONLY (type
);
3817 store_init_value (decl
, init
, NULL_TREE
);
3819 if (TREE_CODE (type
) == ARRAY_TYPE
&& !COMPLETE_TYPE_P (type
))
3821 int failure
= complete_array_type (&TREE_TYPE (decl
),
3822 DECL_INITIAL (decl
), true);
3823 gcc_assert (!failure
);
3825 type
= TREE_TYPE (decl
);
3826 TREE_TYPE (DECL_INITIAL (decl
)) = type
;
3829 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
3830 return error_mark_node
;
3832 stmt
= build_stmt (DECL_EXPR
, decl
);
3833 complit
= build1 (COMPOUND_LITERAL_EXPR
, type
, stmt
);
3834 TREE_SIDE_EFFECTS (complit
) = 1;
3836 layout_decl (decl
, 0);
3838 if (TREE_STATIC (decl
))
3840 /* This decl needs a name for the assembler output. */
3841 set_compound_literal_name (decl
);
3842 DECL_DEFER_OUTPUT (decl
) = 1;
3843 DECL_COMDAT (decl
) = 1;
3844 DECL_ARTIFICIAL (decl
) = 1;
3845 DECL_IGNORED_P (decl
) = 1;
3847 rest_of_decl_compilation (decl
, 1, 0);
3852 complit
= build2 (C_MAYBE_CONST_EXPR
, type
, NULL
, complit
);
3853 C_MAYBE_CONST_EXPR_NON_CONST (complit
) = 1;
3859 /* Determine whether TYPE is a structure with a flexible array member,
3860 or a union containing such a structure (possibly recursively). */
3863 flexible_array_type_p (tree type
)
3866 switch (TREE_CODE (type
))
3869 x
= TYPE_FIELDS (type
);
3872 while (TREE_CHAIN (x
) != NULL_TREE
)
3874 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
3875 && TYPE_SIZE (TREE_TYPE (x
)) == NULL_TREE
3876 && TYPE_DOMAIN (TREE_TYPE (x
)) != NULL_TREE
3877 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x
))) == NULL_TREE
)
3881 for (x
= TYPE_FIELDS (type
); x
!= NULL_TREE
; x
= TREE_CHAIN (x
))
3883 if (flexible_array_type_p (TREE_TYPE (x
)))
3892 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3893 replacing with appropriate values if they are invalid. */
3895 check_bitfield_type_and_width (tree
*type
, tree
*width
, const char *orig_name
)
3898 unsigned int max_width
;
3899 unsigned HOST_WIDE_INT w
;
3900 const char *name
= orig_name
? orig_name
: _("<anonymous>");
3902 /* Detect and ignore out of range field width and process valid
3904 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width
))
3905 || TREE_CODE (*width
) != INTEGER_CST
)
3907 error ("bit-field %qs width not an integer constant", name
);
3908 *width
= integer_one_node
;
3912 constant_expression_warning (*width
);
3913 if (tree_int_cst_sgn (*width
) < 0)
3915 error ("negative width in bit-field %qs", name
);
3916 *width
= integer_one_node
;
3918 else if (integer_zerop (*width
) && orig_name
)
3920 error ("zero width for bit-field %qs", name
);
3921 *width
= integer_one_node
;
3925 /* Detect invalid bit-field type. */
3926 if (TREE_CODE (*type
) != INTEGER_TYPE
3927 && TREE_CODE (*type
) != BOOLEAN_TYPE
3928 && TREE_CODE (*type
) != ENUMERAL_TYPE
)
3930 error ("bit-field %qs has invalid type", name
);
3931 *type
= unsigned_type_node
;
3934 type_mv
= TYPE_MAIN_VARIANT (*type
);
3935 if (!in_system_header
3936 && type_mv
!= integer_type_node
3937 && type_mv
!= unsigned_type_node
3938 && type_mv
!= boolean_type_node
)
3939 pedwarn (input_location
, OPT_pedantic
,
3940 "type of bit-field %qs is a GCC extension", name
);
3942 max_width
= TYPE_PRECISION (*type
);
3944 if (0 < compare_tree_int (*width
, max_width
))
3946 error ("width of %qs exceeds its type", name
);
3948 *width
= build_int_cst (NULL_TREE
, w
);
3951 w
= tree_low_cst (*width
, 1);
3953 if (TREE_CODE (*type
) == ENUMERAL_TYPE
)
3955 struct lang_type
*lt
= TYPE_LANG_SPECIFIC (*type
);
3957 || w
< tree_int_cst_min_precision (lt
->enum_min
, TYPE_UNSIGNED (*type
))
3958 || w
< tree_int_cst_min_precision (lt
->enum_max
, TYPE_UNSIGNED (*type
)))
3959 warning (0, "%qs is narrower than values of its type", name
);
3965 /* Print warning about variable length array if necessary. */
3968 warn_variable_length_array (const char *name
, tree size
)
3970 int const_size
= TREE_CONSTANT (size
);
3972 if (!flag_isoc99
&& pedantic
&& warn_vla
!= 0)
3977 pedwarn (input_location
, OPT_Wvla
, "ISO C90 forbids array %qs whose size "
3978 "can%'t be evaluated",
3981 pedwarn (input_location
, OPT_Wvla
, "ISO C90 forbids array whose size "
3982 "can%'t be evaluated");
3987 pedwarn (input_location
, OPT_Wvla
, "ISO C90 forbids variable length array %qs",
3990 pedwarn (input_location
, OPT_Wvla
, "ISO C90 forbids variable length array");
3993 else if (warn_vla
> 0)
3999 "the size of array %qs can"
4000 "%'t be evaluated", name
);
4003 "the size of array can %'t be evaluated");
4009 "variable length array %qs is used",
4013 "variable length array is used");
4018 /* Given declspecs and a declarator,
4019 determine the name and type of the object declared
4020 and construct a ..._DECL node for it.
4021 (In one case we can return a ..._TYPE node instead.
4022 For invalid input we sometimes return 0.)
4024 DECLSPECS is a c_declspecs structure for the declaration specifiers.
4026 DECL_CONTEXT says which syntactic context this declaration is in:
4027 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4028 FUNCDEF for a function definition. Like NORMAL but a few different
4029 error messages in each case. Return value may be zero meaning
4030 this definition is too screwy to try to parse.
4031 PARM for a parameter declaration (either within a function prototype
4032 or before a function body). Make a PARM_DECL, or return void_type_node.
4033 TYPENAME if for a typename (in a cast or sizeof).
4034 Don't make a DECL node; just return the ..._TYPE node.
4035 FIELD for a struct or union field; make a FIELD_DECL.
4036 INITIALIZED is true if the decl has an initializer.
4037 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4038 representing the width of the bit-field.
4039 DECL_ATTRS points to the list of attributes that should be added to this
4040 decl. Any nested attributes that belong on the decl itself will be
4042 If EXPR is not NULL, any expressions that need to be evaluated as
4043 part of evaluating variably modified types will be stored in *EXPR.
4044 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
4045 set to indicate whether operands in *EXPR can be used in constant
4047 DEPRECATED_STATE is a deprecated_states value indicating whether
4048 deprecation warnings should be suppressed.
4050 In the TYPENAME case, DECLARATOR is really an absolute declarator.
4051 It may also be so in the PARM case, for a prototype where the
4052 argument type is specified but not the name.
4054 This function is where the complicated C meanings of `static'
4055 and `extern' are interpreted. */
4058 grokdeclarator (const struct c_declarator
*declarator
,
4059 struct c_declspecs
*declspecs
,
4060 enum decl_context decl_context
, bool initialized
, tree
*width
,
4061 tree
*decl_attrs
, tree
*expr
, bool *expr_const_operands
,
4062 enum deprecated_states deprecated_state
)
4064 tree type
= declspecs
->type
;
4065 bool threadp
= declspecs
->thread_p
;
4066 enum c_storage_class storage_class
= declspecs
->storage_class
;
4070 int type_quals
= TYPE_UNQUALIFIED
;
4071 const char *name
, *orig_name
;
4072 bool funcdef_flag
= false;
4073 bool funcdef_syntax
= false;
4074 int size_varies
= 0;
4075 tree decl_attr
= declspecs
->decl_attr
;
4076 int array_ptr_quals
= TYPE_UNQUALIFIED
;
4077 tree array_ptr_attrs
= NULL_TREE
;
4078 int array_parm_static
= 0;
4079 bool array_parm_vla_unspec_p
= false;
4080 tree returned_attrs
= NULL_TREE
;
4081 bool bitfield
= width
!= NULL
;
4083 struct c_arg_info
*arg_info
= 0;
4085 bool expr_const_operands_dummy
;
4089 if (expr_const_operands
== NULL
)
4090 expr_const_operands
= &expr_const_operands_dummy
;
4092 *expr
= declspecs
->expr
;
4093 *expr_const_operands
= declspecs
->expr_const_operands
;
4095 if (decl_context
== FUNCDEF
)
4096 funcdef_flag
= true, decl_context
= NORMAL
;
4098 /* Look inside a declarator for the name being declared
4099 and get it as a string, for an error message. */
4101 const struct c_declarator
*decl
= declarator
;
4110 funcdef_syntax
= (decl
->kind
== cdk_function
);
4111 decl
= decl
->declarator
;
4115 decl
= decl
->declarator
;
4120 name
= IDENTIFIER_POINTER (decl
->u
.id
);
4132 /* A function definition's declarator must have the form of
4133 a function declarator. */
4135 if (funcdef_flag
&& !funcdef_syntax
)
4138 /* If this looks like a function definition, make it one,
4139 even if it occurs where parms are expected.
4140 Then store_parm_decls will reject it and not use it as a parm. */
4141 if (decl_context
== NORMAL
&& !funcdef_flag
&& current_scope
->parm_flag
)
4142 decl_context
= PARM
;
4144 if (declspecs
->deprecated_p
&& deprecated_state
!= DEPRECATED_SUPPRESS
)
4145 warn_deprecated_use (declspecs
->type
);
4147 if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
4148 && current_scope
== file_scope
4149 && variably_modified_type_p (type
, NULL_TREE
))
4151 error ("variably modified %qs at file scope", name
);
4152 type
= integer_type_node
;
4155 size_varies
= C_TYPE_VARIABLE_SIZE (type
);
4157 /* Diagnose defaulting to "int". */
4159 if (declspecs
->default_int_p
&& !in_system_header
)
4161 /* Issue a warning if this is an ISO C 99 program or if
4162 -Wreturn-type and this is a function, or if -Wimplicit;
4163 prefer the former warning since it is more explicit. */
4164 if ((warn_implicit_int
|| warn_return_type
|| flag_isoc99
)
4166 warn_about_return_type
= 1;
4168 pedwarn_c99 (input_location
, flag_isoc99
? 0 : OPT_Wimplicit_int
,
4169 "type defaults to %<int%> in declaration of %qs", name
);
4172 /* Adjust the type if a bit-field is being declared,
4173 -funsigned-bitfields applied and the type is not explicitly
4175 if (bitfield
&& !flag_signed_bitfields
&& !declspecs
->explicit_signed_p
4176 && TREE_CODE (type
) == INTEGER_TYPE
)
4177 type
= unsigned_type_for (type
);
4179 /* Figure out the type qualifiers for the declaration. There are
4180 two ways a declaration can become qualified. One is something
4181 like `const int i' where the `const' is explicit. Another is
4182 something like `typedef const int CI; CI i' where the type of the
4183 declaration contains the `const'. A third possibility is that
4184 there is a type qualifier on the element type of a typedefed
4185 array type, in which case we should extract that qualifier so
4186 that c_apply_type_quals_to_decl receives the full list of
4187 qualifiers to work with (C90 is not entirely clear about whether
4188 duplicate qualifiers should be diagnosed in this case, but it
4189 seems most appropriate to do so). */
4190 element_type
= strip_array_types (type
);
4191 constp
= declspecs
->const_p
+ TYPE_READONLY (element_type
);
4192 restrictp
= declspecs
->restrict_p
+ TYPE_RESTRICT (element_type
);
4193 volatilep
= declspecs
->volatile_p
+ TYPE_VOLATILE (element_type
);
4194 if (pedantic
&& !flag_isoc99
)
4197 pedwarn (input_location
, OPT_pedantic
, "duplicate %<const%>");
4199 pedwarn (input_location
, OPT_pedantic
, "duplicate %<restrict%>");
4201 pedwarn (input_location
, OPT_pedantic
, "duplicate %<volatile%>");
4203 if (!flag_gen_aux_info
&& (TYPE_QUALS (element_type
)))
4204 type
= TYPE_MAIN_VARIANT (type
);
4205 type_quals
= ((constp
? TYPE_QUAL_CONST
: 0)
4206 | (restrictp
? TYPE_QUAL_RESTRICT
: 0)
4207 | (volatilep
? TYPE_QUAL_VOLATILE
: 0));
4209 /* Warn about storage classes that are invalid for certain
4210 kinds of declarations (parameters, typenames, etc.). */
4214 || storage_class
== csc_auto
4215 || storage_class
== csc_register
4216 || storage_class
== csc_typedef
))
4218 if (storage_class
== csc_auto
)
4219 pedwarn (input_location
,
4220 (current_scope
== file_scope
) ? 0 : OPT_pedantic
,
4221 "function definition declared %<auto%>");
4222 if (storage_class
== csc_register
)
4223 error ("function definition declared %<register%>");
4224 if (storage_class
== csc_typedef
)
4225 error ("function definition declared %<typedef%>");
4227 error ("function definition declared %<__thread%>");
4229 if (storage_class
== csc_auto
4230 || storage_class
== csc_register
4231 || storage_class
== csc_typedef
)
4232 storage_class
= csc_none
;
4234 else if (decl_context
!= NORMAL
&& (storage_class
!= csc_none
|| threadp
))
4236 if (decl_context
== PARM
&& storage_class
== csc_register
)
4240 switch (decl_context
)
4243 error ("storage class specified for structure field %qs",
4247 error ("storage class specified for parameter %qs", name
);
4250 error ("storage class specified for typename");
4253 storage_class
= csc_none
;
4257 else if (storage_class
== csc_extern
4261 /* 'extern' with initialization is invalid if not at file scope. */
4262 if (current_scope
== file_scope
)
4264 /* It is fine to have 'extern const' when compiling at C
4265 and C++ intersection. */
4266 if (!(warn_cxx_compat
&& constp
))
4267 warning (0, "%qs initialized and declared %<extern%>", name
);
4270 error ("%qs has both %<extern%> and initializer", name
);
4272 else if (current_scope
== file_scope
)
4274 if (storage_class
== csc_auto
)
4275 error ("file-scope declaration of %qs specifies %<auto%>", name
);
4276 if (pedantic
&& storage_class
== csc_register
)
4277 pedwarn (input_location
, OPT_pedantic
,
4278 "file-scope declaration of %qs specifies %<register%>", name
);
4282 if (storage_class
== csc_extern
&& funcdef_flag
)
4283 error ("nested function %qs declared %<extern%>", name
);
4284 else if (threadp
&& storage_class
== csc_none
)
4286 error ("function-scope %qs implicitly auto and declared "
4293 /* Now figure out the structure of the declarator proper.
4294 Descend through it, creating more complex types, until we reach
4295 the declared identifier (or NULL_TREE, in an absolute declarator).
4296 At each stage we maintain an unqualified version of the type
4297 together with any qualifiers that should be applied to it with
4298 c_build_qualified_type; this way, array types including
4299 multidimensional array types are first built up in unqualified
4300 form and then the qualified form is created with
4301 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4303 while (declarator
&& declarator
->kind
!= cdk_id
)
4305 if (type
== error_mark_node
)
4307 declarator
= declarator
->declarator
;
4311 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4312 a cdk_pointer (for *...),
4313 a cdk_function (for ...(...)),
4314 a cdk_attrs (for nested attributes),
4315 or a cdk_id (for the name being declared
4316 or the place in an absolute declarator
4317 where the name was omitted).
4318 For the last case, we have just exited the loop.
4320 At this point, TYPE is the type of elements of an array,
4321 or for a function to return, or for a pointer to point to.
4322 After this sequence of ifs, TYPE is the type of the
4323 array or function or pointer, and DECLARATOR has had its
4324 outermost layer removed. */
4326 if (array_ptr_quals
!= TYPE_UNQUALIFIED
4327 || array_ptr_attrs
!= NULL_TREE
4328 || array_parm_static
)
4330 /* Only the innermost declarator (making a parameter be of
4331 array type which is converted to pointer type)
4332 may have static or type qualifiers. */
4333 error ("static or type qualifiers in non-parameter array declarator");
4334 array_ptr_quals
= TYPE_UNQUALIFIED
;
4335 array_ptr_attrs
= NULL_TREE
;
4336 array_parm_static
= 0;
4339 switch (declarator
->kind
)
4343 /* A declarator with embedded attributes. */
4344 tree attrs
= declarator
->u
.attrs
;
4345 const struct c_declarator
*inner_decl
;
4347 declarator
= declarator
->declarator
;
4348 inner_decl
= declarator
;
4349 while (inner_decl
->kind
== cdk_attrs
)
4350 inner_decl
= inner_decl
->declarator
;
4351 if (inner_decl
->kind
== cdk_id
)
4352 attr_flags
|= (int) ATTR_FLAG_DECL_NEXT
;
4353 else if (inner_decl
->kind
== cdk_function
)
4354 attr_flags
|= (int) ATTR_FLAG_FUNCTION_NEXT
;
4355 else if (inner_decl
->kind
== cdk_array
)
4356 attr_flags
|= (int) ATTR_FLAG_ARRAY_NEXT
;
4357 returned_attrs
= decl_attributes (&type
,
4358 chainon (returned_attrs
, attrs
),
4364 tree itype
= NULL_TREE
;
4365 tree size
= declarator
->u
.array
.dimen
;
4366 /* The index is a signed object `sizetype' bits wide. */
4367 tree index_type
= c_common_signed_type (sizetype
);
4369 array_ptr_quals
= declarator
->u
.array
.quals
;
4370 array_ptr_attrs
= declarator
->u
.array
.attrs
;
4371 array_parm_static
= declarator
->u
.array
.static_p
;
4372 array_parm_vla_unspec_p
= declarator
->u
.array
.vla_unspec_p
;
4374 declarator
= declarator
->declarator
;
4376 /* Check for some types that there cannot be arrays of. */
4378 if (VOID_TYPE_P (type
))
4380 error ("declaration of %qs as array of voids", name
);
4381 type
= error_mark_node
;
4384 if (TREE_CODE (type
) == FUNCTION_TYPE
)
4386 error ("declaration of %qs as array of functions", name
);
4387 type
= error_mark_node
;
4390 if (pedantic
&& !in_system_header
&& flexible_array_type_p (type
))
4391 pedwarn (input_location
, OPT_pedantic
,
4392 "invalid use of structure with flexible array member");
4394 if (size
== error_mark_node
)
4395 type
= error_mark_node
;
4397 if (type
== error_mark_node
)
4400 /* If size was specified, set ITYPE to a range-type for
4401 that size. Otherwise, ITYPE remains null. finish_decl
4402 may figure it out from an initial value. */
4406 bool size_maybe_const
= true;
4407 bool size_int_const
= (TREE_CODE (size
) == INTEGER_CST
4408 && !TREE_OVERFLOW (size
));
4409 bool this_size_varies
= false;
4411 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4413 STRIP_TYPE_NOPS (size
);
4415 if (!INTEGRAL_TYPE_P (TREE_TYPE (size
)))
4417 error ("size of array %qs has non-integer type", name
);
4418 size
= integer_one_node
;
4421 size
= c_fully_fold (size
, false, &size_maybe_const
);
4423 if (pedantic
&& size_maybe_const
&& integer_zerop (size
))
4424 pedwarn (input_location
, OPT_pedantic
,
4425 "ISO C forbids zero-size array %qs", name
);
4427 if (TREE_CODE (size
) == INTEGER_CST
&& size_maybe_const
)
4429 constant_expression_warning (size
);
4430 if (tree_int_cst_sgn (size
) < 0)
4432 error ("size of array %qs is negative", name
);
4433 size
= integer_one_node
;
4435 /* Handle a size folded to an integer constant but
4436 not an integer constant expression. */
4437 if (!size_int_const
)
4439 /* If this is a file scope declaration of an
4440 ordinary identifier, this is invalid code;
4441 diagnosing it here and not subsequently
4442 treating the type as variable-length avoids
4443 more confusing diagnostics later. */
4444 if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
4445 && current_scope
== file_scope
)
4446 pedwarn (input_location
, 0,
4447 "variably modified %qs at file scope", name
);
4449 this_size_varies
= size_varies
= 1;
4450 warn_variable_length_array (orig_name
, size
);
4453 else if ((decl_context
== NORMAL
|| decl_context
== FIELD
)
4454 && current_scope
== file_scope
)
4456 error ("variably modified %qs at file scope", name
);
4457 size
= integer_one_node
;
4461 /* Make sure the array size remains visibly
4462 nonconstant even if it is (eg) a const variable
4463 with known value. */
4464 this_size_varies
= size_varies
= 1;
4465 warn_variable_length_array (orig_name
, size
);
4468 if (integer_zerop (size
) && !this_size_varies
)
4470 /* A zero-length array cannot be represented with
4471 an unsigned index type, which is what we'll
4472 get with build_index_type. Create an
4473 open-ended range instead. */
4474 itype
= build_range_type (sizetype
, size
, NULL_TREE
);
4478 /* Arrange for the SAVE_EXPR on the inside of the
4479 MINUS_EXPR, which allows the -1 to get folded
4480 with the +1 that happens when building TYPE_SIZE. */
4482 size
= variable_size (size
);
4483 if (this_size_varies
&& TREE_CODE (size
) == INTEGER_CST
)
4484 size
= build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
4485 integer_zero_node
, size
);
4487 /* Compute the maximum valid index, that is, size
4488 - 1. Do the calculation in index_type, so that
4489 if it is a variable the computations will be
4490 done in the proper mode. */
4491 itype
= fold_build2 (MINUS_EXPR
, index_type
,
4492 convert (index_type
, size
),
4493 convert (index_type
,
4496 /* If that overflowed, the array is too big. ???
4497 While a size of INT_MAX+1 technically shouldn't
4498 cause an overflow (because we subtract 1), the
4499 overflow is recorded during the conversion to
4500 index_type, before the subtraction. Handling
4501 this case seems like an unnecessary
4503 if (TREE_CODE (itype
) == INTEGER_CST
4504 && TREE_OVERFLOW (itype
))
4506 error ("size of array %qs is too large", name
);
4507 type
= error_mark_node
;
4511 itype
= build_index_type (itype
);
4513 if (this_size_varies
)
4516 *expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (size
),
4520 *expr_const_operands
&= size_maybe_const
;
4523 else if (decl_context
== FIELD
)
4525 bool flexible_array_member
= false;
4526 if (array_parm_vla_unspec_p
)
4527 /* Field names can in fact have function prototype
4528 scope so [*] is disallowed here through making
4529 the field variably modified, not through being
4530 something other than a declaration with function
4535 const struct c_declarator
*t
= declarator
;
4536 while (t
->kind
== cdk_attrs
)
4538 flexible_array_member
= (t
->kind
== cdk_id
);
4540 if (flexible_array_member
4541 && pedantic
&& !flag_isoc99
&& !in_system_header
)
4542 pedwarn (input_location
, OPT_pedantic
,
4543 "ISO C90 does not support flexible array members");
4545 /* ISO C99 Flexible array members are effectively
4546 identical to GCC's zero-length array extension. */
4547 if (flexible_array_member
|| array_parm_vla_unspec_p
)
4548 itype
= build_range_type (sizetype
, size_zero_node
,
4551 else if (decl_context
== PARM
)
4553 if (array_parm_vla_unspec_p
)
4555 itype
= build_range_type (sizetype
, size_zero_node
, NULL_TREE
);
4559 else if (decl_context
== TYPENAME
)
4561 if (array_parm_vla_unspec_p
)
4564 warning (0, "%<[*]%> not in a declaration");
4565 /* We use this to avoid messing up with incomplete
4566 array types of the same type, that would
4567 otherwise be modified below. */
4568 itype
= build_range_type (sizetype
, size_zero_node
,
4574 /* Complain about arrays of incomplete types. */
4575 if (!COMPLETE_TYPE_P (type
))
4577 error ("array type has incomplete element type");
4578 type
= error_mark_node
;
4581 /* When itype is NULL, a shared incomplete array type is
4582 returned for all array of a given type. Elsewhere we
4583 make sure we don't complete that type before copying
4584 it, but here we want to make sure we don't ever
4585 modify the shared type, so we gcc_assert (itype)
4587 type
= build_array_type (type
, itype
);
4589 if (type
!= error_mark_node
)
4593 /* It is ok to modify type here even if itype is
4594 NULL: if size_varies, we're in a
4595 multi-dimensional array and the inner type has
4596 variable size, so the enclosing shared array type
4598 if (size
&& TREE_CODE (size
) == INTEGER_CST
)
4600 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
4601 C_TYPE_VARIABLE_SIZE (type
) = 1;
4604 /* The GCC extension for zero-length arrays differs from
4605 ISO flexible array members in that sizeof yields
4607 if (size
&& integer_zerop (size
))
4610 TYPE_SIZE (type
) = bitsize_zero_node
;
4611 TYPE_SIZE_UNIT (type
) = size_zero_node
;
4613 if (array_parm_vla_unspec_p
)
4616 /* The type is complete. C99 6.7.5.2p4 */
4617 TYPE_SIZE (type
) = bitsize_zero_node
;
4618 TYPE_SIZE_UNIT (type
) = size_zero_node
;
4622 if (decl_context
!= PARM
4623 && (array_ptr_quals
!= TYPE_UNQUALIFIED
4624 || array_ptr_attrs
!= NULL_TREE
4625 || array_parm_static
))
4627 error ("static or type qualifiers in non-parameter array declarator");
4628 array_ptr_quals
= TYPE_UNQUALIFIED
;
4629 array_ptr_attrs
= NULL_TREE
;
4630 array_parm_static
= 0;
4636 /* Say it's a definition only for the declarator closest
4637 to the identifier, apart possibly from some
4639 bool really_funcdef
= false;
4643 const struct c_declarator
*t
= declarator
->declarator
;
4644 while (t
->kind
== cdk_attrs
)
4646 really_funcdef
= (t
->kind
== cdk_id
);
4649 /* Declaring a function type. Make sure we have a valid
4650 type for the function to return. */
4651 if (type
== error_mark_node
)
4656 /* Warn about some types functions can't return. */
4657 if (TREE_CODE (type
) == FUNCTION_TYPE
)
4659 error ("%qs declared as function returning a function", name
);
4660 type
= integer_type_node
;
4662 if (TREE_CODE (type
) == ARRAY_TYPE
)
4664 error ("%qs declared as function returning an array", name
);
4665 type
= integer_type_node
;
4668 /* Construct the function type and go to the next
4669 inner layer of declarator. */
4670 arg_info
= declarator
->u
.arg_info
;
4671 arg_types
= grokparms (arg_info
, really_funcdef
);
4673 put_pending_sizes (arg_info
->pending_sizes
);
4675 /* Type qualifiers before the return type of the function
4676 qualify the return type, not the function type. */
4679 /* Type qualifiers on a function return type are
4680 normally permitted by the standard but have no
4681 effect, so give a warning at -Wreturn-type.
4682 Qualifiers on a void return type are banned on
4683 function definitions in ISO C; GCC used to used
4684 them for noreturn functions. */
4685 if (VOID_TYPE_P (type
) && really_funcdef
)
4686 pedwarn (input_location
, 0,
4687 "function definition has qualified void return type");
4689 warning (OPT_Wignored_qualifiers
,
4690 "type qualifiers ignored on function return type");
4692 type
= c_build_qualified_type (type
, type_quals
);
4694 type_quals
= TYPE_UNQUALIFIED
;
4696 type
= build_function_type (type
, arg_types
);
4697 declarator
= declarator
->declarator
;
4699 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4700 the formal parameter list of this FUNCTION_TYPE to point to
4701 the FUNCTION_TYPE node itself. */
4705 for (link
= arg_info
->tags
;
4707 link
= TREE_CHAIN (link
))
4708 TYPE_CONTEXT (TREE_VALUE (link
)) = type
;
4714 /* Merge any constancy or volatility into the target type
4717 if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
4719 pedwarn (input_location
, OPT_pedantic
,
4720 "ISO C forbids qualified function types");
4722 type
= c_build_qualified_type (type
, type_quals
);
4725 /* When the pointed-to type involves components of variable size,
4726 care must be taken to ensure that the size evaluation code is
4727 emitted early enough to dominate all the possible later uses
4728 and late enough for the variables on which it depends to have
4731 This is expected to happen automatically when the pointed-to
4732 type has a name/declaration of it's own, but special attention
4733 is required if the type is anonymous.
4735 We handle the NORMAL and FIELD contexts here by attaching an
4736 artificial TYPE_DECL to such pointed-to type. This forces the
4737 sizes evaluation at a safe point and ensures it is not deferred
4738 until e.g. within a deeper conditional context.
4740 We expect nothing to be needed here for PARM or TYPENAME.
4741 Pushing a TYPE_DECL at this point for TYPENAME would actually
4742 be incorrect, as we might be in the middle of an expression
4743 with side effects on the pointed-to type size "arguments" prior
4744 to the pointer declaration point and the fake TYPE_DECL in the
4745 enclosing context would force the size evaluation prior to the
4748 if (!TYPE_NAME (type
)
4749 && (decl_context
== NORMAL
|| decl_context
== FIELD
)
4750 && variably_modified_type_p (type
, NULL_TREE
))
4752 tree decl
= build_decl (TYPE_DECL
, NULL_TREE
, type
);
4753 DECL_ARTIFICIAL (decl
) = 1;
4755 finish_decl (decl
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
4756 TYPE_NAME (type
) = decl
;
4759 type
= build_pointer_type (type
);
4761 /* Process type qualifiers (such as const or volatile)
4762 that were given inside the `*'. */
4763 type_quals
= declarator
->u
.pointer_quals
;
4765 declarator
= declarator
->declarator
;
4772 *decl_attrs
= chainon (returned_attrs
, *decl_attrs
);
4774 /* Now TYPE has the actual type, apart from any qualifiers in
4777 /* Check the type and width of a bit-field. */
4779 check_bitfield_type_and_width (&type
, width
, orig_name
);
4781 /* Did array size calculations overflow? */
4783 if (TREE_CODE (type
) == ARRAY_TYPE
4784 && COMPLETE_TYPE_P (type
)
4785 && TREE_CODE (TYPE_SIZE_UNIT (type
)) == INTEGER_CST
4786 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type
)))
4788 error ("size of array %qs is too large", name
);
4789 /* If we proceed with the array type as it is, we'll eventually
4790 crash in tree_low_cst(). */
4791 type
= error_mark_node
;
4794 /* If this is declaring a typedef name, return a TYPE_DECL. */
4796 if (storage_class
== csc_typedef
)
4799 if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
4801 pedwarn (input_location
, OPT_pedantic
,
4802 "ISO C forbids qualified function types");
4804 type
= c_build_qualified_type (type
, type_quals
);
4805 decl
= build_decl (TYPE_DECL
, declarator
->u
.id
, type
);
4806 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
4807 if (declspecs
->explicit_signed_p
)
4808 C_TYPEDEF_EXPLICITLY_SIGNED (decl
) = 1;
4809 if (declspecs
->inline_p
)
4810 pedwarn (input_location
, 0,"typedef %q+D declared %<inline%>", decl
);
4814 /* If this is a type name (such as, in a cast or sizeof),
4815 compute the type and return it now. */
4817 if (decl_context
== TYPENAME
)
4819 /* Note that the grammar rejects storage classes in typenames
4821 gcc_assert (storage_class
== csc_none
&& !threadp
4822 && !declspecs
->inline_p
);
4823 if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
4825 pedwarn (input_location
, OPT_pedantic
,
4826 "ISO C forbids const or volatile function types");
4828 type
= c_build_qualified_type (type
, type_quals
);
4832 if (pedantic
&& decl_context
== FIELD
4833 && variably_modified_type_p (type
, NULL_TREE
))
4836 pedwarn (input_location
, OPT_pedantic
,
4837 "a member of a structure or union cannot have a variably modified type");
4840 /* Aside from typedefs and type names (handle above),
4841 `void' at top level (not within pointer)
4842 is allowed only in public variables.
4843 We don't complain about parms either, but that is because
4844 a better error message can be made later. */
4846 if (VOID_TYPE_P (type
) && decl_context
!= PARM
4847 && !((decl_context
!= FIELD
&& TREE_CODE (type
) != FUNCTION_TYPE
)
4848 && (storage_class
== csc_extern
4849 || (current_scope
== file_scope
4850 && !(storage_class
== csc_static
4851 || storage_class
== csc_register
)))))
4853 error ("variable or field %qs declared void", name
);
4854 type
= integer_type_node
;
4857 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4858 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4863 if (decl_context
== PARM
)
4867 /* A parameter declared as an array of T is really a pointer to T.
4868 One declared as a function is really a pointer to a function. */
4870 if (TREE_CODE (type
) == ARRAY_TYPE
)
4872 /* Transfer const-ness of array into that of type pointed to. */
4873 type
= TREE_TYPE (type
);
4875 type
= c_build_qualified_type (type
, type_quals
);
4876 type
= build_pointer_type (type
);
4877 type_quals
= array_ptr_quals
;
4879 type
= c_build_qualified_type (type
, type_quals
);
4881 /* We don't yet implement attributes in this context. */
4882 if (array_ptr_attrs
!= NULL_TREE
)
4883 warning (OPT_Wattributes
,
4884 "attributes in parameter array declarator ignored");
4888 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
4891 pedwarn (input_location
, OPT_pedantic
,
4892 "ISO C forbids qualified function types");
4894 type
= c_build_qualified_type (type
, type_quals
);
4895 type
= build_pointer_type (type
);
4896 type_quals
= TYPE_UNQUALIFIED
;
4898 else if (type_quals
)
4899 type
= c_build_qualified_type (type
, type_quals
);
4901 decl
= build_decl (PARM_DECL
, declarator
->u
.id
, type
);
4902 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
4904 C_DECL_VARIABLE_SIZE (decl
) = 1;
4906 /* Compute the type actually passed in the parmlist,
4907 for the case where there is no prototype.
4908 (For example, shorts and chars are passed as ints.)
4909 When there is a prototype, this is overridden later. */
4911 if (type
== error_mark_node
)
4912 promoted_type
= type
;
4914 promoted_type
= c_type_promotes_to (type
);
4916 DECL_ARG_TYPE (decl
) = promoted_type
;
4917 if (declspecs
->inline_p
)
4918 pedwarn (input_location
, 0, "parameter %q+D declared %<inline%>", decl
);
4920 else if (decl_context
== FIELD
)
4922 /* Note that the grammar rejects storage classes in typenames
4924 gcc_assert (storage_class
== csc_none
&& !threadp
4925 && !declspecs
->inline_p
);
4927 /* Structure field. It may not be a function. */
4929 if (TREE_CODE (type
) == FUNCTION_TYPE
)
4931 error ("field %qs declared as a function", name
);
4932 type
= build_pointer_type (type
);
4934 else if (TREE_CODE (type
) != ERROR_MARK
4935 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type
))
4937 error ("field %qs has incomplete type", name
);
4938 type
= error_mark_node
;
4940 type
= c_build_qualified_type (type
, type_quals
);
4941 decl
= build_decl (FIELD_DECL
, declarator
->u
.id
, type
);
4942 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
4943 DECL_NONADDRESSABLE_P (decl
) = bitfield
;
4944 if (bitfield
&& !declarator
->u
.id
)
4945 TREE_NO_WARNING (decl
) = 1;
4948 C_DECL_VARIABLE_SIZE (decl
) = 1;
4950 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
4952 if (storage_class
== csc_register
|| threadp
)
4954 error ("invalid storage class for function %qs", name
);
4956 else if (current_scope
!= file_scope
)
4958 /* Function declaration not at file scope. Storage
4959 classes other than `extern' are not allowed, C99
4960 6.7.1p5, and `extern' makes no difference. However,
4961 GCC allows 'auto', perhaps with 'inline', to support
4962 nested functions. */
4963 if (storage_class
== csc_auto
)
4964 pedwarn (input_location
, OPT_pedantic
, "invalid storage class for function %qs", name
);
4965 else if (storage_class
== csc_static
)
4967 error ("invalid storage class for function %qs", name
);
4969 storage_class
= declspecs
->storage_class
= csc_none
;
4975 decl
= build_decl (FUNCTION_DECL
, declarator
->u
.id
, type
);
4976 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
4977 decl
= build_decl_attribute_variant (decl
, decl_attr
);
4979 if (pedantic
&& type_quals
&& !DECL_IN_SYSTEM_HEADER (decl
))
4980 pedwarn (input_location
, OPT_pedantic
,
4981 "ISO C forbids qualified function types");
4983 /* GNU C interprets a volatile-qualified function type to indicate
4984 that the function does not return. */
4985 if ((type_quals
& TYPE_QUAL_VOLATILE
)
4986 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl
))))
4987 warning (0, "%<noreturn%> function returns non-void value");
4989 /* Every function declaration is an external reference
4990 (DECL_EXTERNAL) except for those which are not at file
4991 scope and are explicitly declared "auto". This is
4992 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4993 GCC to signify a forward declaration of a nested function. */
4994 if (storage_class
== csc_auto
&& current_scope
!= file_scope
)
4995 DECL_EXTERNAL (decl
) = 0;
4996 /* In C99, a function which is declared 'inline' with 'extern'
4997 is not an external reference (which is confusing). It
4998 means that the later definition of the function must be output
4999 in this file, C99 6.7.4p6. In GNU C89, a function declared
5000 'extern inline' is an external reference. */
5001 else if (declspecs
->inline_p
&& storage_class
!= csc_static
)
5002 DECL_EXTERNAL (decl
) = ((storage_class
== csc_extern
)
5003 == flag_gnu89_inline
);
5005 DECL_EXTERNAL (decl
) = !initialized
;
5007 /* Record absence of global scope for `static' or `auto'. */
5009 = !(storage_class
== csc_static
|| storage_class
== csc_auto
);
5011 /* For a function definition, record the argument information
5012 block where store_parm_decls will look for it. */
5014 current_function_arg_info
= arg_info
;
5016 if (declspecs
->default_int_p
)
5017 C_FUNCTION_IMPLICIT_INT (decl
) = 1;
5019 /* Record presence of `inline', if it is reasonable. */
5020 if (flag_hosted
&& MAIN_NAME_P (declarator
->u
.id
))
5022 if (declspecs
->inline_p
)
5023 pedwarn (input_location
, 0, "cannot inline function %<main%>");
5025 else if (declspecs
->inline_p
)
5026 /* Record that the function is declared `inline'. */
5027 DECL_DECLARED_INLINE_P (decl
) = 1;
5031 /* It's a variable. */
5032 /* An uninitialized decl with `extern' is a reference. */
5033 int extern_ref
= !initialized
&& storage_class
== csc_extern
;
5035 type
= c_build_qualified_type (type
, type_quals
);
5037 /* C99 6.2.2p7: It is invalid (compile-time undefined
5038 behavior) to create an 'extern' declaration for a
5039 variable if there is a global declaration that is
5040 'static' and the global declaration is not visible.
5041 (If the static declaration _is_ currently visible,
5042 the 'extern' declaration is taken to refer to that decl.) */
5043 if (extern_ref
&& current_scope
!= file_scope
)
5045 tree global_decl
= identifier_global_value (declarator
->u
.id
);
5046 tree visible_decl
= lookup_name (declarator
->u
.id
);
5049 && global_decl
!= visible_decl
5050 && TREE_CODE (global_decl
) == VAR_DECL
5051 && !TREE_PUBLIC (global_decl
))
5052 error ("variable previously declared %<static%> redeclared "
5056 decl
= build_decl (VAR_DECL
, declarator
->u
.id
, type
);
5057 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
5059 C_DECL_VARIABLE_SIZE (decl
) = 1;
5061 if (declspecs
->inline_p
)
5062 pedwarn (input_location
, 0, "variable %q+D declared %<inline%>", decl
);
5064 /* At file scope, an initialized extern declaration may follow
5065 a static declaration. In that case, DECL_EXTERNAL will be
5066 reset later in start_decl. */
5067 DECL_EXTERNAL (decl
) = (storage_class
== csc_extern
);
5069 /* At file scope, the presence of a `static' or `register' storage
5070 class specifier, or the absence of all storage class specifiers
5071 makes this declaration a definition (perhaps tentative). Also,
5072 the absence of `static' makes it public. */
5073 if (current_scope
== file_scope
)
5075 TREE_PUBLIC (decl
) = storage_class
!= csc_static
;
5076 TREE_STATIC (decl
) = !extern_ref
;
5078 /* Not at file scope, only `static' makes a static definition. */
5081 TREE_STATIC (decl
) = (storage_class
== csc_static
);
5082 TREE_PUBLIC (decl
) = extern_ref
;
5086 DECL_TLS_MODEL (decl
) = decl_default_tls_model (decl
);
5089 if ((storage_class
== csc_extern
5090 || (storage_class
== csc_none
5091 && TREE_CODE (type
) == FUNCTION_TYPE
5093 && variably_modified_type_p (type
, NULL_TREE
))
5096 if (TREE_CODE (type
) == FUNCTION_TYPE
)
5097 error ("non-nested function with variably modified type");
5099 error ("object with variably modified type must have no linkage");
5102 /* Record `register' declaration for warnings on &
5103 and in case doing stupid register allocation. */
5105 if (storage_class
== csc_register
)
5107 C_DECL_REGISTER (decl
) = 1;
5108 DECL_REGISTER (decl
) = 1;
5111 /* Record constancy and volatility. */
5112 c_apply_type_quals_to_decl (type_quals
, decl
);
5114 /* If a type has volatile components, it should be stored in memory.
5115 Otherwise, the fact that those components are volatile
5116 will be ignored, and would even crash the compiler.
5117 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
5118 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl
))
5119 && (TREE_CODE (decl
) == VAR_DECL
|| TREE_CODE (decl
) == PARM_DECL
5120 || TREE_CODE (decl
) == RESULT_DECL
))
5122 /* It is not an error for a structure with volatile fields to
5123 be declared register, but reset DECL_REGISTER since it
5124 cannot actually go in a register. */
5125 int was_reg
= C_DECL_REGISTER (decl
);
5126 C_DECL_REGISTER (decl
) = 0;
5127 DECL_REGISTER (decl
) = 0;
5128 c_mark_addressable (decl
);
5129 C_DECL_REGISTER (decl
) = was_reg
;
5132 /* This is the earliest point at which we might know the assembler
5133 name of a variable. Thus, if it's known before this, die horribly. */
5134 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl
));
5140 /* Decode the parameter-list info for a function type or function definition.
5141 The argument is the value returned by `get_parm_info' (or made in c-parse.c
5142 if there is an identifier list instead of a parameter decl list).
5143 These two functions are separate because when a function returns
5144 or receives functions then each is called multiple times but the order
5145 of calls is different. The last call to `grokparms' is always the one
5146 that contains the formal parameter names of a function definition.
5148 Return a list of arg types to use in the FUNCTION_TYPE for this function.
5150 FUNCDEF_FLAG is true for a function definition, false for
5151 a mere declaration. A nonempty identifier-list gets an error message
5152 when FUNCDEF_FLAG is false. */
5155 grokparms (struct c_arg_info
*arg_info
, bool funcdef_flag
)
5157 tree arg_types
= arg_info
->types
;
5159 if (funcdef_flag
&& arg_info
->had_vla_unspec
)
5161 /* A function definition isn't function prototype scope C99 6.2.1p4. */
5163 error ("%<[*]%> not allowed in other than function prototype scope");
5166 if (arg_types
== 0 && !funcdef_flag
&& !in_system_header
)
5167 warning (OPT_Wstrict_prototypes
,
5168 "function declaration isn%'t a prototype");
5170 if (arg_types
== error_mark_node
)
5171 return 0; /* don't set TYPE_ARG_TYPES in this case */
5173 else if (arg_types
&& TREE_CODE (TREE_VALUE (arg_types
)) == IDENTIFIER_NODE
)
5176 pedwarn (input_location
, 0, "parameter names (without types) in function declaration");
5178 arg_info
->parms
= arg_info
->types
;
5179 arg_info
->types
= 0;
5184 tree parm
, type
, typelt
;
5185 unsigned int parmno
;
5187 /* If there is a parameter of incomplete type in a definition,
5188 this is an error. In a declaration this is valid, and a
5189 struct or union type may be completed later, before any calls
5190 or definition of the function. In the case where the tag was
5191 first declared within the parameter list, a warning has
5192 already been given. If a parameter has void type, then
5193 however the function cannot be defined or called, so
5196 for (parm
= arg_info
->parms
, typelt
= arg_types
, parmno
= 1;
5198 parm
= TREE_CHAIN (parm
), typelt
= TREE_CHAIN (typelt
), parmno
++)
5200 type
= TREE_VALUE (typelt
);
5201 if (type
== error_mark_node
)
5204 if (!COMPLETE_TYPE_P (type
))
5208 if (DECL_NAME (parm
))
5209 error ("parameter %u (%q+D) has incomplete type",
5212 error ("%Jparameter %u has incomplete type",
5215 TREE_VALUE (typelt
) = error_mark_node
;
5216 TREE_TYPE (parm
) = error_mark_node
;
5218 else if (VOID_TYPE_P (type
))
5220 if (DECL_NAME (parm
))
5221 warning (0, "parameter %u (%q+D) has void type",
5224 warning (0, "%Jparameter %u has void type",
5229 if (DECL_NAME (parm
) && TREE_USED (parm
))
5230 warn_if_shadowing (parm
);
5236 /* Take apart the current scope and return a c_arg_info structure with
5237 info on a parameter list just parsed.
5239 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5241 ELLIPSIS being true means the argument list ended in '...' so don't
5242 append a sentinel (void_list_node) to the end of the type-list. */
5245 get_parm_info (bool ellipsis
)
5247 struct c_binding
*b
= current_scope
->bindings
;
5248 struct c_arg_info
*arg_info
= XOBNEW (&parser_obstack
,
5255 static bool explained_incomplete_types
= false;
5256 bool gave_void_only_once_err
= false;
5258 arg_info
->parms
= 0;
5260 arg_info
->types
= 0;
5261 arg_info
->others
= 0;
5262 arg_info
->pending_sizes
= 0;
5263 arg_info
->had_vla_unspec
= current_scope
->had_vla_unspec
;
5265 /* The bindings in this scope must not get put into a block.
5266 We will take care of deleting the binding nodes. */
5267 current_scope
->bindings
= 0;
5269 /* This function is only called if there was *something* on the
5273 /* A parameter list consisting solely of 'void' indicates that the
5274 function takes no arguments. But if the 'void' is qualified
5275 (by 'const' or 'volatile'), or has a storage class specifier
5276 ('register'), then the behavior is undefined; issue an error.
5277 Typedefs for 'void' are OK (see DR#157). */
5278 if (b
->prev
== 0 /* one binding */
5279 && TREE_CODE (b
->decl
) == PARM_DECL
/* which is a parameter */
5280 && !DECL_NAME (b
->decl
) /* anonymous */
5281 && VOID_TYPE_P (TREE_TYPE (b
->decl
))) /* of void type */
5283 if (TREE_THIS_VOLATILE (b
->decl
)
5284 || TREE_READONLY (b
->decl
)
5285 || C_DECL_REGISTER (b
->decl
))
5286 error ("%<void%> as only parameter may not be qualified");
5288 /* There cannot be an ellipsis. */
5290 error ("%<void%> must be the only parameter");
5292 arg_info
->types
= void_list_node
;
5297 types
= void_list_node
;
5299 /* Break up the bindings list into parms, tags, types, and others;
5300 apply sanity checks; purge the name-to-decl bindings. */
5303 tree decl
= b
->decl
;
5304 tree type
= TREE_TYPE (decl
);
5305 const char *keyword
;
5307 switch (TREE_CODE (decl
))
5312 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
5313 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
5316 /* Check for forward decls that never got their actual decl. */
5317 if (TREE_ASM_WRITTEN (decl
))
5318 error ("parameter %q+D has just a forward declaration", decl
);
5319 /* Check for (..., void, ...) and issue an error. */
5320 else if (VOID_TYPE_P (type
) && !DECL_NAME (decl
))
5322 if (!gave_void_only_once_err
)
5324 error ("%<void%> must be the only parameter");
5325 gave_void_only_once_err
= true;
5330 /* Valid parameter, add it to the list. */
5331 TREE_CHAIN (decl
) = parms
;
5334 /* Since there is a prototype, args are passed in their
5335 declared types. The back end may override this later. */
5336 DECL_ARG_TYPE (decl
) = type
;
5337 types
= tree_cons (0, type
, types
);
5341 case ENUMERAL_TYPE
: keyword
= "enum"; goto tag
;
5342 case UNION_TYPE
: keyword
= "union"; goto tag
;
5343 case RECORD_TYPE
: keyword
= "struct"; goto tag
;
5345 /* Types may not have tag-names, in which case the type
5346 appears in the bindings list with b->id NULL. */
5349 gcc_assert (I_TAG_BINDING (b
->id
) == b
);
5350 I_TAG_BINDING (b
->id
) = b
->shadowed
;
5353 /* Warn about any struct, union or enum tags defined in a
5354 parameter list. The scope of such types is limited to
5355 the parameter list, which is rarely if ever desirable
5356 (it's impossible to call such a function with type-
5357 correct arguments). An anonymous union parm type is
5358 meaningful as a GNU extension, so don't warn for that. */
5359 if (TREE_CODE (decl
) != UNION_TYPE
|| b
->id
!= 0)
5362 /* The %s will be one of 'struct', 'union', or 'enum'. */
5363 warning (0, "%<%s %E%> declared inside parameter list",
5366 /* The %s will be one of 'struct', 'union', or 'enum'. */
5367 warning (0, "anonymous %s declared inside parameter list",
5370 if (!explained_incomplete_types
)
5372 warning (0, "its scope is only this definition or declaration,"
5373 " which is probably not what you want");
5374 explained_incomplete_types
= true;
5378 tags
= tree_cons (b
->id
, decl
, tags
);
5384 /* CONST_DECLs appear here when we have an embedded enum,
5385 and TYPE_DECLs appear here when we have an embedded struct
5386 or union. No warnings for this - we already warned about the
5387 type itself. FUNCTION_DECLs appear when there is an implicit
5388 function declaration in the parameter list. */
5390 TREE_CHAIN (decl
) = others
;
5395 /* error_mark_node appears here when we have an undeclared
5396 variable. Just throw it away. */
5399 gcc_assert (I_SYMBOL_BINDING (b
->id
) == b
);
5400 I_SYMBOL_BINDING (b
->id
) = b
->shadowed
;
5404 /* Other things that might be encountered. */
5411 b
= free_binding_and_advance (b
);
5414 arg_info
->parms
= parms
;
5415 arg_info
->tags
= tags
;
5416 arg_info
->types
= types
;
5417 arg_info
->others
= others
;
5418 arg_info
->pending_sizes
= get_pending_sizes ();
5422 /* Get the struct, enum or union (CODE says which) with tag NAME.
5423 Define the tag as a forward-reference if it is not defined.
5424 Return a c_typespec structure for the type specifier. */
5427 parser_xref_tag (enum tree_code code
, tree name
)
5429 struct c_typespec ret
;
5432 ret
.expr
= NULL_TREE
;
5433 ret
.expr_const_operands
= true;
5435 /* If a cross reference is requested, look up the type
5436 already defined for this tag and return it. */
5438 ref
= lookup_tag (code
, name
, 0);
5439 /* If this is the right type of tag, return what we found.
5440 (This reference will be shadowed by shadow_tag later if appropriate.)
5441 If this is the wrong type of tag, do not return it. If it was the
5442 wrong type in the same scope, we will have had an error
5443 message already; if in a different scope and declaring
5444 a name, pending_xref_error will give an error message; but if in a
5445 different scope and not declaring a name, this tag should
5446 shadow the previous declaration of a different type of tag, and
5447 this would not work properly if we return the reference found.
5448 (For example, with "struct foo" in an outer scope, "union foo;"
5449 must shadow that tag with a new one of union type.) */
5450 ret
.kind
= (ref
? ctsk_tagref
: ctsk_tagfirstref
);
5451 if (ref
&& TREE_CODE (ref
) == code
)
5457 /* If no such tag is yet defined, create a forward-reference node
5458 and record it as the "definition".
5459 When a real declaration of this type is found,
5460 the forward-reference will be altered into a real type. */
5462 ref
= make_node (code
);
5463 if (code
== ENUMERAL_TYPE
)
5465 /* Give the type a default layout like unsigned int
5466 to avoid crashing if it does not get defined. */
5467 SET_TYPE_MODE (ref
, TYPE_MODE (unsigned_type_node
));
5468 TYPE_ALIGN (ref
) = TYPE_ALIGN (unsigned_type_node
);
5469 TYPE_USER_ALIGN (ref
) = 0;
5470 TYPE_UNSIGNED (ref
) = 1;
5471 TYPE_PRECISION (ref
) = TYPE_PRECISION (unsigned_type_node
);
5472 TYPE_MIN_VALUE (ref
) = TYPE_MIN_VALUE (unsigned_type_node
);
5473 TYPE_MAX_VALUE (ref
) = TYPE_MAX_VALUE (unsigned_type_node
);
5476 pushtag (name
, ref
);
5482 /* Get the struct, enum or union (CODE says which) with tag NAME.
5483 Define the tag as a forward-reference if it is not defined.
5484 Return a tree for the type. */
5487 xref_tag (enum tree_code code
, tree name
)
5489 return parser_xref_tag (code
, name
).spec
;
5492 /* Make sure that the tag NAME is defined *in the current scope*
5493 at least as a forward reference.
5494 CODE says which kind of tag NAME ought to be. */
5497 start_struct (enum tree_code code
, tree name
)
5499 /* If there is already a tag defined at this scope
5500 (as a forward reference), just return it. */
5505 ref
= lookup_tag (code
, name
, 1);
5506 if (ref
&& TREE_CODE (ref
) == code
)
5508 if (TYPE_SIZE (ref
))
5510 if (code
== UNION_TYPE
)
5511 error ("redefinition of %<union %E%>", name
);
5513 error ("redefinition of %<struct %E%>", name
);
5514 /* Don't create structures using a name already in use. */
5517 else if (C_TYPE_BEING_DEFINED (ref
))
5519 if (code
== UNION_TYPE
)
5520 error ("nested redefinition of %<union %E%>", name
);
5522 error ("nested redefinition of %<struct %E%>", name
);
5523 /* Don't create structures that contain themselves. */
5528 /* Otherwise create a forward-reference just so the tag is in scope. */
5530 if (ref
== NULL_TREE
|| TREE_CODE (ref
) != code
)
5532 ref
= make_node (code
);
5533 pushtag (name
, ref
);
5536 C_TYPE_BEING_DEFINED (ref
) = 1;
5537 TYPE_PACKED (ref
) = flag_pack_struct
;
5541 /* Process the specs, declarator and width (NULL if omitted)
5542 of a structure component, returning a FIELD_DECL node.
5543 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5544 DECL_ATTRS is as for grokdeclarator.
5546 LOC is the location of the structure component.
5548 This is done during the parsing of the struct declaration.
5549 The FIELD_DECL nodes are chained together and the lot of them
5550 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5553 grokfield (location_t loc
,
5554 struct c_declarator
*declarator
, struct c_declspecs
*declspecs
,
5555 tree width
, tree
*decl_attrs
)
5559 if (declarator
->kind
== cdk_id
&& declarator
->u
.id
== NULL_TREE
5560 && width
== NULL_TREE
)
5562 /* This is an unnamed decl.
5564 If we have something of the form "union { list } ;" then this
5565 is the anonymous union extension. Similarly for struct.
5567 If this is something of the form "struct foo;", then
5568 If MS extensions are enabled, this is handled as an
5570 Otherwise this is a forward declaration of a structure tag.
5572 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5573 If MS extensions are enabled and foo names a structure, then
5574 again this is an anonymous struct.
5575 Otherwise this is an error.
5577 Oh what a horrid tangled web we weave. I wonder if MS consciously
5578 took this from Plan 9 or if it was an accident of implementation
5579 that took root before someone noticed the bug... */
5581 tree type
= declspecs
->type
;
5582 bool type_ok
= (TREE_CODE (type
) == RECORD_TYPE
5583 || TREE_CODE (type
) == UNION_TYPE
);
5587 && (flag_ms_extensions
|| !declspecs
->typedef_p
))
5589 if (flag_ms_extensions
)
5593 else if (TYPE_NAME (type
) == NULL
)
5600 pedwarn (loc
, 0, "declaration does not declare anything");
5603 pedwarn (loc
, OPT_pedantic
, "ISO C doesn%'t support unnamed structs/unions");
5606 value
= grokdeclarator (declarator
, declspecs
, FIELD
, false,
5607 width
? &width
: NULL
, decl_attrs
, NULL
, NULL
,
5610 finish_decl (value
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
5611 DECL_INITIAL (value
) = width
;
5616 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5617 the list such that this does not present a problem later. */
5620 detect_field_duplicates (tree fieldlist
)
5625 /* First, see if there are more than "a few" fields.
5626 This is trivially true if there are zero or one fields. */
5629 x
= TREE_CHAIN (fieldlist
);
5635 } while (timeout
> 0 && x
);
5637 /* If there were "few" fields, avoid the overhead of allocating
5638 a hash table. Instead just do the nested traversal thing. */
5641 for (x
= TREE_CHAIN (fieldlist
); x
; x
= TREE_CHAIN (x
))
5644 for (y
= fieldlist
; y
!= x
; y
= TREE_CHAIN (y
))
5645 if (DECL_NAME (y
) == DECL_NAME (x
))
5647 error ("duplicate member %q+D", x
);
5648 DECL_NAME (x
) = NULL_TREE
;
5654 htab_t htab
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
, NULL
);
5657 for (x
= fieldlist
; x
; x
= TREE_CHAIN (x
))
5658 if ((y
= DECL_NAME (x
)) != 0)
5660 slot
= htab_find_slot (htab
, y
, INSERT
);
5663 error ("duplicate member %q+D", x
);
5664 DECL_NAME (x
) = NULL_TREE
;
5673 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5674 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5675 ATTRIBUTES are attributes to be applied to the structure. */
5678 finish_struct (tree t
, tree fieldlist
, tree attributes
)
5681 bool toplevel
= file_scope
== current_scope
;
5682 int saw_named_field
;
5684 /* If this type was previously laid out as a forward reference,
5685 make sure we lay it out again. */
5689 decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
5693 for (x
= fieldlist
; x
; x
= TREE_CHAIN (x
))
5694 if (DECL_NAME (x
) != 0)
5699 if (TREE_CODE (t
) == UNION_TYPE
)
5702 pedwarn (input_location
, OPT_pedantic
, "union has no named members");
5704 pedwarn (input_location
, OPT_pedantic
, "union has no members");
5709 pedwarn (input_location
, OPT_pedantic
, "struct has no named members");
5711 pedwarn (input_location
, OPT_pedantic
, "struct has no members");
5716 /* Install struct as DECL_CONTEXT of each field decl.
5717 Also process specified field sizes, found in the DECL_INITIAL,
5718 storing 0 there after the type has been changed to precision equal
5719 to its width, rather than the precision of the specified standard
5720 type. (Correct layout requires the original type to have been preserved
5723 saw_named_field
= 0;
5724 for (x
= fieldlist
; x
; x
= TREE_CHAIN (x
))
5726 if (TREE_TYPE (x
) == error_mark_node
)
5729 DECL_CONTEXT (x
) = t
;
5731 /* If any field is const, the structure type is pseudo-const. */
5732 if (TREE_READONLY (x
))
5733 C_TYPE_FIELDS_READONLY (t
) = 1;
5736 /* A field that is pseudo-const makes the structure likewise. */
5737 tree t1
= TREE_TYPE (x
);
5738 while (TREE_CODE (t1
) == ARRAY_TYPE
)
5739 t1
= TREE_TYPE (t1
);
5740 if ((TREE_CODE (t1
) == RECORD_TYPE
|| TREE_CODE (t1
) == UNION_TYPE
)
5741 && C_TYPE_FIELDS_READONLY (t1
))
5742 C_TYPE_FIELDS_READONLY (t
) = 1;
5745 /* Any field that is volatile means variables of this type must be
5746 treated in some ways as volatile. */
5747 if (TREE_THIS_VOLATILE (x
))
5748 C_TYPE_FIELDS_VOLATILE (t
) = 1;
5750 /* Any field of nominal variable size implies structure is too. */
5751 if (C_DECL_VARIABLE_SIZE (x
))
5752 C_TYPE_VARIABLE_SIZE (t
) = 1;
5754 if (DECL_INITIAL (x
))
5756 unsigned HOST_WIDE_INT width
= tree_low_cst (DECL_INITIAL (x
), 1);
5757 DECL_SIZE (x
) = bitsize_int (width
);
5758 DECL_BIT_FIELD (x
) = 1;
5759 SET_DECL_C_BIT_FIELD (x
);
5763 && (DECL_BIT_FIELD (x
)
5764 || TYPE_ALIGN (TREE_TYPE (x
)) > BITS_PER_UNIT
))
5765 DECL_PACKED (x
) = 1;
5767 /* Detect flexible array member in an invalid context. */
5768 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
5769 && TYPE_SIZE (TREE_TYPE (x
)) == NULL_TREE
5770 && TYPE_DOMAIN (TREE_TYPE (x
)) != NULL_TREE
5771 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x
))) == NULL_TREE
)
5773 if (TREE_CODE (t
) == UNION_TYPE
)
5775 error ("%Jflexible array member in union", x
);
5776 TREE_TYPE (x
) = error_mark_node
;
5778 else if (TREE_CHAIN (x
) != NULL_TREE
)
5780 error ("%Jflexible array member not at end of struct", x
);
5781 TREE_TYPE (x
) = error_mark_node
;
5783 else if (!saw_named_field
)
5785 error ("%Jflexible array member in otherwise empty struct", x
);
5786 TREE_TYPE (x
) = error_mark_node
;
5790 if (pedantic
&& !in_system_header
&& TREE_CODE (t
) == RECORD_TYPE
5791 && flexible_array_type_p (TREE_TYPE (x
)))
5792 pedwarn (input_location
, OPT_pedantic
,
5793 "%Jinvalid use of structure with flexible array member", x
);
5796 saw_named_field
= 1;
5799 detect_field_duplicates (fieldlist
);
5801 /* Now we have the nearly final fieldlist. Record it,
5802 then lay out the structure or union (including the fields). */
5804 TYPE_FIELDS (t
) = fieldlist
;
5808 /* Give bit-fields their proper types. */
5810 tree
*fieldlistp
= &fieldlist
;
5812 if (TREE_CODE (*fieldlistp
) == FIELD_DECL
&& DECL_INITIAL (*fieldlistp
)
5813 && TREE_TYPE (*fieldlistp
) != error_mark_node
)
5815 unsigned HOST_WIDE_INT width
5816 = tree_low_cst (DECL_INITIAL (*fieldlistp
), 1);
5817 tree type
= TREE_TYPE (*fieldlistp
);
5818 if (width
!= TYPE_PRECISION (type
))
5820 TREE_TYPE (*fieldlistp
)
5821 = c_build_bitfield_integer_type (width
, TYPE_UNSIGNED (type
));
5822 DECL_MODE (*fieldlistp
) = TYPE_MODE (TREE_TYPE (*fieldlistp
));
5824 DECL_INITIAL (*fieldlistp
) = 0;
5827 fieldlistp
= &TREE_CHAIN (*fieldlistp
);
5830 /* Now we have the truly final field list.
5831 Store it in this type and in the variants. */
5833 TYPE_FIELDS (t
) = fieldlist
;
5835 /* If there are lots of fields, sort so we can look through them fast.
5836 We arbitrarily consider 16 or more elts to be "a lot". */
5841 for (x
= fieldlist
; x
; x
= TREE_CHAIN (x
))
5843 if (len
> 15 || DECL_NAME (x
) == NULL
)
5851 struct lang_type
*space
;
5852 struct sorted_fields_type
*space2
;
5854 len
+= list_length (x
);
5856 /* Use the same allocation policy here that make_node uses, to
5857 ensure that this lives as long as the rest of the struct decl.
5858 All decls in an inline function need to be saved. */
5860 space
= GGC_CNEW (struct lang_type
);
5861 space2
= GGC_NEWVAR (struct sorted_fields_type
,
5862 sizeof (struct sorted_fields_type
) + len
* sizeof (tree
));
5866 field_array
= &space2
->elts
[0];
5867 for (x
= fieldlist
; x
; x
= TREE_CHAIN (x
))
5869 field_array
[len
++] = x
;
5871 /* If there is anonymous struct or union, break out of the loop. */
5872 if (DECL_NAME (x
) == NULL
)
5875 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5878 TYPE_LANG_SPECIFIC (t
) = space
;
5879 TYPE_LANG_SPECIFIC (t
)->s
->len
= len
;
5880 field_array
= TYPE_LANG_SPECIFIC (t
)->s
->elts
;
5881 qsort (field_array
, len
, sizeof (tree
), field_decl_cmp
);
5886 for (x
= TYPE_MAIN_VARIANT (t
); x
; x
= TYPE_NEXT_VARIANT (x
))
5888 TYPE_FIELDS (x
) = TYPE_FIELDS (t
);
5889 TYPE_LANG_SPECIFIC (x
) = TYPE_LANG_SPECIFIC (t
);
5890 C_TYPE_FIELDS_READONLY (x
) = C_TYPE_FIELDS_READONLY (t
);
5891 C_TYPE_FIELDS_VOLATILE (x
) = C_TYPE_FIELDS_VOLATILE (t
);
5892 C_TYPE_VARIABLE_SIZE (x
) = C_TYPE_VARIABLE_SIZE (t
);
5895 /* If this was supposed to be a transparent union, but we can't
5896 make it one, warn and turn off the flag. */
5897 if (TREE_CODE (t
) == UNION_TYPE
5898 && TYPE_TRANSPARENT_UNION (t
)
5899 && (!TYPE_FIELDS (t
) || TYPE_MODE (t
) != DECL_MODE (TYPE_FIELDS (t
))))
5901 TYPE_TRANSPARENT_UNION (t
) = 0;
5902 warning (0, "union cannot be made transparent");
5905 /* If this structure or union completes the type of any previous
5906 variable declaration, lay it out and output its rtl. */
5907 for (x
= C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t
));
5911 tree decl
= TREE_VALUE (x
);
5912 if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
5913 layout_array_type (TREE_TYPE (decl
));
5914 if (TREE_CODE (decl
) != TYPE_DECL
)
5916 layout_decl (decl
, 0);
5917 if (c_dialect_objc ())
5918 objc_check_decl (decl
);
5919 rest_of_decl_compilation (decl
, toplevel
, 0);
5924 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t
)) = 0;
5926 /* Finish debugging output for this type. */
5927 rest_of_type_compilation (t
, toplevel
);
5929 /* If we're inside a function proper, i.e. not file-scope and not still
5930 parsing parameters, then arrange for the size of a variable sized type
5932 if (cur_stmt_list
&& variably_modified_type_p (t
, NULL_TREE
))
5933 add_stmt (build_stmt (DECL_EXPR
, build_decl (TYPE_DECL
, NULL
, t
)));
5938 /* Lay out the type T, and its element type, and so on. */
5941 layout_array_type (tree t
)
5943 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
5944 layout_array_type (TREE_TYPE (t
));
5948 /* Begin compiling the definition of an enumeration type.
5949 NAME is its name (or null if anonymous).
5950 Returns the type object, as yet incomplete.
5951 Also records info about it so that build_enumerator
5952 may be used to declare the individual values as they are read. */
5955 start_enum (struct c_enum_contents
*the_enum
, tree name
)
5959 /* If this is the real definition for a previous forward reference,
5960 fill in the contents in the same object that used to be the
5961 forward reference. */
5964 enumtype
= lookup_tag (ENUMERAL_TYPE
, name
, 1);
5966 if (enumtype
== 0 || TREE_CODE (enumtype
) != ENUMERAL_TYPE
)
5968 enumtype
= make_node (ENUMERAL_TYPE
);
5969 pushtag (name
, enumtype
);
5972 if (C_TYPE_BEING_DEFINED (enumtype
))
5973 error ("nested redefinition of %<enum %E%>", name
);
5975 C_TYPE_BEING_DEFINED (enumtype
) = 1;
5977 if (TYPE_VALUES (enumtype
) != 0)
5979 /* This enum is a named one that has been declared already. */
5980 error ("redeclaration of %<enum %E%>", name
);
5982 /* Completely replace its old definition.
5983 The old enumerators remain defined, however. */
5984 TYPE_VALUES (enumtype
) = 0;
5987 the_enum
->enum_next_value
= integer_zero_node
;
5988 the_enum
->enum_overflow
= 0;
5990 if (flag_short_enums
)
5991 TYPE_PACKED (enumtype
) = 1;
5996 /* After processing and defining all the values of an enumeration type,
5997 install their decls in the enumeration type and finish it off.
5998 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5999 and ATTRIBUTES are the specified attributes.
6000 Returns ENUMTYPE. */
6003 finish_enum (tree enumtype
, tree values
, tree attributes
)
6006 tree minnode
= 0, maxnode
= 0;
6007 int precision
, unsign
;
6008 bool toplevel
= (file_scope
== current_scope
);
6009 struct lang_type
*lt
;
6011 decl_attributes (&enumtype
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
6013 /* Calculate the maximum value of any enumerator in this type. */
6015 if (values
== error_mark_node
)
6016 minnode
= maxnode
= integer_zero_node
;
6019 minnode
= maxnode
= TREE_VALUE (values
);
6020 for (pair
= TREE_CHAIN (values
); pair
; pair
= TREE_CHAIN (pair
))
6022 tree value
= TREE_VALUE (pair
);
6023 if (tree_int_cst_lt (maxnode
, value
))
6025 if (tree_int_cst_lt (value
, minnode
))
6030 /* Construct the final type of this enumeration. It is the same
6031 as one of the integral types - the narrowest one that fits, except
6032 that normally we only go as narrow as int - and signed iff any of
6033 the values are negative. */
6034 unsign
= (tree_int_cst_sgn (minnode
) >= 0);
6035 precision
= MAX (tree_int_cst_min_precision (minnode
, unsign
),
6036 tree_int_cst_min_precision (maxnode
, unsign
));
6038 if (TYPE_PACKED (enumtype
) || precision
> TYPE_PRECISION (integer_type_node
))
6040 tem
= c_common_type_for_size (precision
, unsign
);
6043 warning (0, "enumeration values exceed range of largest integer");
6044 tem
= long_long_integer_type_node
;
6048 tem
= unsign
? unsigned_type_node
: integer_type_node
;
6050 TYPE_MIN_VALUE (enumtype
) = TYPE_MIN_VALUE (tem
);
6051 TYPE_MAX_VALUE (enumtype
) = TYPE_MAX_VALUE (tem
);
6052 TYPE_UNSIGNED (enumtype
) = TYPE_UNSIGNED (tem
);
6053 TYPE_SIZE (enumtype
) = 0;
6055 /* If the precision of the type was specific with an attribute and it
6056 was too small, give an error. Otherwise, use it. */
6057 if (TYPE_PRECISION (enumtype
))
6059 if (precision
> TYPE_PRECISION (enumtype
))
6060 error ("specified mode too small for enumeral values");
6063 TYPE_PRECISION (enumtype
) = TYPE_PRECISION (tem
);
6065 layout_type (enumtype
);
6067 if (values
!= error_mark_node
)
6069 /* Change the type of the enumerators to be the enum type. We
6070 need to do this irrespective of the size of the enum, for
6071 proper type checking. Replace the DECL_INITIALs of the
6072 enumerators, and the value slots of the list, with copies
6073 that have the enum type; they cannot be modified in place
6074 because they may be shared (e.g. integer_zero_node) Finally,
6075 change the purpose slots to point to the names of the decls. */
6076 for (pair
= values
; pair
; pair
= TREE_CHAIN (pair
))
6078 tree enu
= TREE_PURPOSE (pair
);
6079 tree ini
= DECL_INITIAL (enu
);
6081 TREE_TYPE (enu
) = enumtype
;
6083 /* The ISO C Standard mandates enumerators to have type int,
6084 even though the underlying type of an enum type is
6085 unspecified. However, GCC allows enumerators of any
6086 integer type as an extensions. build_enumerator()
6087 converts any enumerators that fit in an int to type int,
6088 to avoid promotions to unsigned types when comparing
6089 integers with enumerators that fit in the int range.
6090 When -pedantic is given, build_enumerator() would have
6091 already warned about those that don't fit. Here we
6092 convert the rest to the enumerator type. */
6093 if (TREE_TYPE (ini
) != integer_type_node
)
6094 ini
= convert (enumtype
, ini
);
6096 DECL_INITIAL (enu
) = ini
;
6097 TREE_PURPOSE (pair
) = DECL_NAME (enu
);
6098 TREE_VALUE (pair
) = ini
;
6101 TYPE_VALUES (enumtype
) = values
;
6104 /* Record the min/max values so that we can warn about bit-field
6105 enumerations that are too small for the values. */
6106 lt
= GGC_CNEW (struct lang_type
);
6107 lt
->enum_min
= minnode
;
6108 lt
->enum_max
= maxnode
;
6109 TYPE_LANG_SPECIFIC (enumtype
) = lt
;
6111 /* Fix up all variant types of this enum type. */
6112 for (tem
= TYPE_MAIN_VARIANT (enumtype
); tem
; tem
= TYPE_NEXT_VARIANT (tem
))
6114 if (tem
== enumtype
)
6116 TYPE_VALUES (tem
) = TYPE_VALUES (enumtype
);
6117 TYPE_MIN_VALUE (tem
) = TYPE_MIN_VALUE (enumtype
);
6118 TYPE_MAX_VALUE (tem
) = TYPE_MAX_VALUE (enumtype
);
6119 TYPE_SIZE (tem
) = TYPE_SIZE (enumtype
);
6120 TYPE_SIZE_UNIT (tem
) = TYPE_SIZE_UNIT (enumtype
);
6121 SET_TYPE_MODE (tem
, TYPE_MODE (enumtype
));
6122 TYPE_PRECISION (tem
) = TYPE_PRECISION (enumtype
);
6123 TYPE_ALIGN (tem
) = TYPE_ALIGN (enumtype
);
6124 TYPE_USER_ALIGN (tem
) = TYPE_USER_ALIGN (enumtype
);
6125 TYPE_UNSIGNED (tem
) = TYPE_UNSIGNED (enumtype
);
6126 TYPE_LANG_SPECIFIC (tem
) = TYPE_LANG_SPECIFIC (enumtype
);
6129 /* Finish debugging output for this type. */
6130 rest_of_type_compilation (enumtype
, toplevel
);
6135 /* Build and install a CONST_DECL for one value of the
6136 current enumeration type (one that was begun with start_enum).
6137 Return a tree-list containing the CONST_DECL and its value.
6138 Assignment of sequential values by default is handled here. */
6141 build_enumerator (struct c_enum_contents
*the_enum
, tree name
, tree value
,
6142 location_t value_loc
)
6146 /* Validate and default VALUE. */
6150 /* Don't issue more errors for error_mark_node (i.e. an
6151 undeclared identifier) - just ignore the value expression. */
6152 if (value
== error_mark_node
)
6154 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value
)))
6156 error ("enumerator value for %qE is not an integer constant", name
);
6161 if (TREE_CODE (value
) != INTEGER_CST
)
6163 value
= c_fully_fold (value
, false, NULL
);
6164 if (TREE_CODE (value
) == INTEGER_CST
)
6165 pedwarn (value_loc
, OPT_pedantic
,
6166 "enumerator value for %qE is not an integer "
6167 "constant expression", name
);
6169 if (TREE_CODE (value
) != INTEGER_CST
)
6171 error ("enumerator value for %qE is not an integer constant",
6177 value
= default_conversion (value
);
6178 constant_expression_warning (value
);
6183 /* Default based on previous value. */
6184 /* It should no longer be possible to have NON_LVALUE_EXPR
6188 value
= the_enum
->enum_next_value
;
6189 if (the_enum
->enum_overflow
)
6190 error ("overflow in enumeration values");
6192 /* Even though the underlying type of an enum is unspecified, the
6193 type of enumeration constants is explicitly defined as int
6194 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
6196 else if (!int_fits_type_p (value
, integer_type_node
))
6197 pedwarn (value_loc
, OPT_pedantic
,
6198 "ISO C restricts enumerator values to range of %<int%>");
6200 /* The ISO C Standard mandates enumerators to have type int, even
6201 though the underlying type of an enum type is unspecified.
6202 However, GCC allows enumerators of any integer type as an
6203 extensions. Here we convert any enumerators that fit in an int
6204 to type int, to avoid promotions to unsigned types when comparing
6205 integers with enumerators that fit in the int range. When
6206 -pedantic is given, we would have already warned about those that
6207 don't fit. We have to do this here rather than in finish_enum
6208 because this value may be used to define more enumerators. */
6209 if (int_fits_type_p (value
, integer_type_node
))
6210 value
= convert (integer_type_node
, value
);
6212 /* Set basis for default for next value. */
6213 the_enum
->enum_next_value
6215 (EXPR_HAS_LOCATION (value
) ? EXPR_LOCATION (value
) : input_location
,
6216 PLUS_EXPR
, value
, integer_one_node
, 0);
6217 the_enum
->enum_overflow
= tree_int_cst_lt (the_enum
->enum_next_value
, value
);
6219 /* Now create a declaration for the enum value name. */
6221 type
= TREE_TYPE (value
);
6222 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
6223 TYPE_PRECISION (integer_type_node
)),
6224 (TYPE_PRECISION (type
)
6225 >= TYPE_PRECISION (integer_type_node
)
6226 && TYPE_UNSIGNED (type
)));
6228 decl
= build_decl (CONST_DECL
, name
, type
);
6229 DECL_INITIAL (decl
) = convert (type
, value
);
6232 return tree_cons (decl
, value
, NULL_TREE
);
6236 /* Create the FUNCTION_DECL for a function definition.
6237 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6238 the declaration; they describe the function's name and the type it returns,
6239 but twisted together in a fashion that parallels the syntax of C.
6241 This function creates a binding context for the function body
6242 as well as setting up the FUNCTION_DECL in current_function_decl.
6244 Returns 1 on success. If the DECLARATOR is not suitable for a function
6245 (it defines a datum instead), we return 0, which tells
6246 yyparse to report a parse error. */
6249 start_function (struct c_declspecs
*declspecs
, struct c_declarator
*declarator
,
6252 tree decl1
, old_decl
;
6253 tree restype
, resdecl
;
6254 struct c_label_context_se
*nstack_se
;
6255 struct c_label_context_vm
*nstack_vm
;
6257 current_function_returns_value
= 0; /* Assume, until we see it does. */
6258 current_function_returns_null
= 0;
6259 current_function_returns_abnormally
= 0;
6260 warn_about_return_type
= 0;
6261 c_switch_stack
= NULL
;
6263 nstack_se
= XOBNEW (&parser_obstack
, struct c_label_context_se
);
6264 nstack_se
->labels_def
= NULL
;
6265 nstack_se
->labels_used
= NULL
;
6266 nstack_se
->next
= label_context_stack_se
;
6267 label_context_stack_se
= nstack_se
;
6269 nstack_vm
= XOBNEW (&parser_obstack
, struct c_label_context_vm
);
6270 nstack_vm
->labels_def
= NULL
;
6271 nstack_vm
->labels_used
= NULL
;
6272 nstack_vm
->scope
= 0;
6273 nstack_vm
->next
= label_context_stack_vm
;
6274 label_context_stack_vm
= nstack_vm
;
6276 /* Indicate no valid break/continue context by setting these variables
6277 to some non-null, non-label value. We'll notice and emit the proper
6278 error message in c_finish_bc_stmt. */
6279 c_break_label
= c_cont_label
= size_zero_node
;
6281 decl1
= grokdeclarator (declarator
, declspecs
, FUNCDEF
, true, NULL
,
6282 &attributes
, NULL
, NULL
, DEPRECATED_NORMAL
);
6284 /* If the declarator is not suitable for a function definition,
6285 cause a syntax error. */
6288 label_context_stack_se
= label_context_stack_se
->next
;
6289 label_context_stack_vm
= label_context_stack_vm
->next
;
6293 decl_attributes (&decl1
, attributes
, 0);
6295 if (DECL_DECLARED_INLINE_P (decl1
)
6296 && DECL_UNINLINABLE (decl1
)
6297 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1
)))
6298 warning (OPT_Wattributes
, "inline function %q+D given attribute noinline",
6301 /* Handle gnu_inline attribute. */
6302 if (declspecs
->inline_p
6303 && !flag_gnu89_inline
6304 && TREE_CODE (decl1
) == FUNCTION_DECL
6305 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1
))
6306 || current_function_decl
))
6308 if (declspecs
->storage_class
!= csc_static
)
6309 DECL_EXTERNAL (decl1
) = !DECL_EXTERNAL (decl1
);
6312 announce_function (decl1
);
6314 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1
))))
6316 error ("return type is an incomplete type");
6317 /* Make it return void instead. */
6319 = build_function_type (void_type_node
,
6320 TYPE_ARG_TYPES (TREE_TYPE (decl1
)));
6323 if (warn_about_return_type
)
6324 pedwarn_c99 (input_location
, flag_isoc99
? 0
6325 : (warn_return_type
? OPT_Wreturn_type
: OPT_Wimplicit_int
),
6326 "return type defaults to %<int%>");
6328 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6329 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6330 DECL_INITIAL (decl1
) = error_mark_node
;
6332 /* If this definition isn't a prototype and we had a prototype declaration
6333 before, copy the arg type info from that prototype. */
6334 old_decl
= lookup_name_in_scope (DECL_NAME (decl1
), current_scope
);
6335 if (old_decl
&& TREE_CODE (old_decl
) != FUNCTION_DECL
)
6337 current_function_prototype_locus
= UNKNOWN_LOCATION
;
6338 current_function_prototype_built_in
= false;
6339 current_function_prototype_arg_types
= NULL_TREE
;
6340 if (TYPE_ARG_TYPES (TREE_TYPE (decl1
)) == 0)
6342 if (old_decl
!= 0 && TREE_CODE (TREE_TYPE (old_decl
)) == FUNCTION_TYPE
6343 && comptypes (TREE_TYPE (TREE_TYPE (decl1
)),
6344 TREE_TYPE (TREE_TYPE (old_decl
))))
6346 TREE_TYPE (decl1
) = composite_type (TREE_TYPE (old_decl
),
6348 current_function_prototype_locus
= DECL_SOURCE_LOCATION (old_decl
);
6349 current_function_prototype_built_in
6350 = C_DECL_BUILTIN_PROTOTYPE (old_decl
);
6351 current_function_prototype_arg_types
6352 = TYPE_ARG_TYPES (TREE_TYPE (decl1
));
6354 if (TREE_PUBLIC (decl1
))
6356 /* If there is an external prototype declaration of this
6357 function, record its location but do not copy information
6358 to this decl. This may be an invisible declaration
6359 (built-in or in a scope which has finished) or simply
6360 have more refined argument types than any declaration
6362 struct c_binding
*b
;
6363 for (b
= I_SYMBOL_BINDING (DECL_NAME (decl1
)); b
; b
= b
->shadowed
)
6364 if (B_IN_SCOPE (b
, external_scope
))
6368 tree ext_decl
, ext_type
;
6370 ext_type
= b
->type
? b
->type
: TREE_TYPE (ext_decl
);
6371 if (TREE_CODE (ext_type
) == FUNCTION_TYPE
6372 && comptypes (TREE_TYPE (TREE_TYPE (decl1
)),
6373 TREE_TYPE (ext_type
)))
6375 current_function_prototype_locus
6376 = DECL_SOURCE_LOCATION (ext_decl
);
6377 current_function_prototype_built_in
6378 = C_DECL_BUILTIN_PROTOTYPE (ext_decl
);
6379 current_function_prototype_arg_types
6380 = TYPE_ARG_TYPES (ext_type
);
6386 /* Optionally warn of old-fashioned def with no previous prototype. */
6387 if (warn_strict_prototypes
6388 && old_decl
!= error_mark_node
6389 && TYPE_ARG_TYPES (TREE_TYPE (decl1
)) == 0
6390 && C_DECL_ISNT_PROTOTYPE (old_decl
))
6391 warning (OPT_Wstrict_prototypes
,
6392 "function declaration isn%'t a prototype");
6393 /* Optionally warn of any global def with no previous prototype. */
6394 else if (warn_missing_prototypes
6395 && old_decl
!= error_mark_node
6396 && TREE_PUBLIC (decl1
)
6397 && !MAIN_NAME_P (DECL_NAME (decl1
))
6398 && C_DECL_ISNT_PROTOTYPE (old_decl
))
6399 warning (OPT_Wmissing_prototypes
, "no previous prototype for %q+D", decl1
);
6400 /* Optionally warn of any def with no previous prototype
6401 if the function has already been used. */
6402 else if (warn_missing_prototypes
6404 && old_decl
!= error_mark_node
6405 && TREE_USED (old_decl
)
6406 && TYPE_ARG_TYPES (TREE_TYPE (old_decl
)) == 0)
6407 warning (OPT_Wmissing_prototypes
,
6408 "%q+D was used with no prototype before its definition", decl1
);
6409 /* Optionally warn of any global def with no previous declaration. */
6410 else if (warn_missing_declarations
6411 && TREE_PUBLIC (decl1
)
6413 && !MAIN_NAME_P (DECL_NAME (decl1
)))
6414 warning (OPT_Wmissing_declarations
, "no previous declaration for %q+D",
6416 /* Optionally warn of any def with no previous declaration
6417 if the function has already been used. */
6418 else if (warn_missing_declarations
6420 && old_decl
!= error_mark_node
6421 && TREE_USED (old_decl
)
6422 && C_DECL_IMPLICIT (old_decl
))
6423 warning (OPT_Wmissing_declarations
,
6424 "%q+D was used with no declaration before its definition", decl1
);
6426 /* This function exists in static storage.
6427 (This does not mean `static' in the C sense!) */
6428 TREE_STATIC (decl1
) = 1;
6430 /* A nested function is not global. */
6431 if (current_function_decl
!= 0)
6432 TREE_PUBLIC (decl1
) = 0;
6434 /* This is the earliest point at which we might know the assembler
6435 name of the function. Thus, if it's set before this, die horribly. */
6436 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1
));
6438 /* If #pragma weak was used, mark the decl weak now. */
6439 if (current_scope
== file_scope
)
6440 maybe_apply_pragma_weak (decl1
);
6442 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6443 if (warn_main
&& MAIN_NAME_P (DECL_NAME (decl1
)))
6445 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1
)))
6446 != integer_type_node
)
6447 pedwarn (input_location
, OPT_Wmain
, "return type of %q+D is not %<int%>", decl1
);
6449 check_main_parameter_types (decl1
);
6451 if (!TREE_PUBLIC (decl1
))
6452 pedwarn (input_location
, OPT_Wmain
, "%q+D is normally a non-static function", decl1
);
6455 /* Record the decl so that the function name is defined.
6456 If we already have a decl for this name, and it is a FUNCTION_DECL,
6457 use the old decl. */
6459 current_function_decl
= pushdecl (decl1
);
6462 declare_parm_level ();
6464 restype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
6465 resdecl
= build_decl (RESULT_DECL
, NULL_TREE
, restype
);
6466 DECL_ARTIFICIAL (resdecl
) = 1;
6467 DECL_IGNORED_P (resdecl
) = 1;
6468 DECL_RESULT (current_function_decl
) = resdecl
;
6470 start_fname_decls ();
6475 /* Subroutine of store_parm_decls which handles new-style function
6476 definitions (prototype format). The parms already have decls, so we
6477 need only record them as in effect and complain if any redundant
6478 old-style parm decls were written. */
6480 store_parm_decls_newstyle (tree fndecl
, const struct c_arg_info
*arg_info
)
6484 if (current_scope
->bindings
)
6486 error ("%Jold-style parameter declarations in prototyped "
6487 "function definition", fndecl
);
6489 /* Get rid of the old-style declarations. */
6493 /* Don't issue this warning for nested functions, and don't issue this
6494 warning if we got here because ARG_INFO_TYPES was error_mark_node
6495 (this happens when a function definition has just an ellipsis in
6496 its parameter list). */
6497 else if (!in_system_header
&& !current_function_scope
6498 && arg_info
->types
!= error_mark_node
)
6499 warning (OPT_Wtraditional
,
6500 "%Jtraditional C rejects ISO C style function definitions",
6503 /* Now make all the parameter declarations visible in the function body.
6504 We can bypass most of the grunt work of pushdecl. */
6505 for (decl
= arg_info
->parms
; decl
; decl
= TREE_CHAIN (decl
))
6507 DECL_CONTEXT (decl
) = current_function_decl
;
6508 if (DECL_NAME (decl
))
6510 bind (DECL_NAME (decl
), decl
, current_scope
,
6511 /*invisible=*/false, /*nested=*/false,
6513 if (!TREE_USED (decl
))
6514 warn_if_shadowing (decl
);
6517 error ("%Jparameter name omitted", decl
);
6520 /* Record the parameter list in the function declaration. */
6521 DECL_ARGUMENTS (fndecl
) = arg_info
->parms
;
6523 /* Now make all the ancillary declarations visible, likewise. */
6524 for (decl
= arg_info
->others
; decl
; decl
= TREE_CHAIN (decl
))
6526 DECL_CONTEXT (decl
) = current_function_decl
;
6527 if (DECL_NAME (decl
))
6528 bind (DECL_NAME (decl
), decl
, current_scope
,
6529 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
6532 /* And all the tag declarations. */
6533 for (decl
= arg_info
->tags
; decl
; decl
= TREE_CHAIN (decl
))
6534 if (TREE_PURPOSE (decl
))
6535 bind (TREE_PURPOSE (decl
), TREE_VALUE (decl
), current_scope
,
6536 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION
);
6539 /* Subroutine of store_parm_decls which handles old-style function
6540 definitions (separate parameter list and declarations). */
6543 store_parm_decls_oldstyle (tree fndecl
, const struct c_arg_info
*arg_info
)
6545 struct c_binding
*b
;
6546 tree parm
, decl
, last
;
6547 tree parmids
= arg_info
->parms
;
6548 struct pointer_set_t
*seen_args
= pointer_set_create ();
6550 if (!in_system_header
)
6551 warning (OPT_Wold_style_definition
, "%Jold-style function definition",
6554 /* Match each formal parameter name with its declaration. Save each
6555 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6556 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
6558 if (TREE_VALUE (parm
) == 0)
6560 error ("%Jparameter name missing from parameter list", fndecl
);
6561 TREE_PURPOSE (parm
) = 0;
6565 b
= I_SYMBOL_BINDING (TREE_VALUE (parm
));
6566 if (b
&& B_IN_CURRENT_SCOPE (b
))
6569 /* If we got something other than a PARM_DECL it is an error. */
6570 if (TREE_CODE (decl
) != PARM_DECL
)
6571 error ("%q+D declared as a non-parameter", decl
);
6572 /* If the declaration is already marked, we have a duplicate
6573 name. Complain and ignore the duplicate. */
6574 else if (pointer_set_contains (seen_args
, decl
))
6576 error ("multiple parameters named %q+D", decl
);
6577 TREE_PURPOSE (parm
) = 0;
6580 /* If the declaration says "void", complain and turn it into
6582 else if (VOID_TYPE_P (TREE_TYPE (decl
)))
6584 error ("parameter %q+D declared with void type", decl
);
6585 TREE_TYPE (decl
) = integer_type_node
;
6586 DECL_ARG_TYPE (decl
) = integer_type_node
;
6587 layout_decl (decl
, 0);
6589 warn_if_shadowing (decl
);
6591 /* If no declaration found, default to int. */
6594 decl
= build_decl (PARM_DECL
, TREE_VALUE (parm
), integer_type_node
);
6595 DECL_ARG_TYPE (decl
) = TREE_TYPE (decl
);
6596 DECL_SOURCE_LOCATION (decl
) = DECL_SOURCE_LOCATION (fndecl
);
6598 warn_if_shadowing (decl
);
6601 pedwarn (input_location
, 0, "type of %q+D defaults to %<int%>", decl
);
6603 warning (OPT_Wmissing_parameter_type
, "type of %q+D defaults to %<int%>", decl
);
6606 TREE_PURPOSE (parm
) = decl
;
6607 pointer_set_insert (seen_args
, decl
);
6610 /* Now examine the parms chain for incomplete declarations
6611 and declarations with no corresponding names. */
6613 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
6616 if (TREE_CODE (parm
) != PARM_DECL
)
6619 if (TREE_TYPE (parm
) != error_mark_node
6620 && !COMPLETE_TYPE_P (TREE_TYPE (parm
)))
6622 error ("parameter %q+D has incomplete type", parm
);
6623 TREE_TYPE (parm
) = error_mark_node
;
6626 if (!pointer_set_contains (seen_args
, parm
))
6628 error ("declaration for parameter %q+D but no such parameter", parm
);
6630 /* Pretend the parameter was not missing.
6631 This gets us to a standard state and minimizes
6632 further error messages. */
6633 parmids
= chainon (parmids
, tree_cons (parm
, 0, 0));
6637 /* Chain the declarations together in the order of the list of
6638 names. Store that chain in the function decl, replacing the
6639 list of names. Update the current scope to match. */
6640 DECL_ARGUMENTS (fndecl
) = 0;
6642 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
6643 if (TREE_PURPOSE (parm
))
6645 if (parm
&& TREE_PURPOSE (parm
))
6647 last
= TREE_PURPOSE (parm
);
6648 DECL_ARGUMENTS (fndecl
) = last
;
6650 for (parm
= TREE_CHAIN (parm
); parm
; parm
= TREE_CHAIN (parm
))
6651 if (TREE_PURPOSE (parm
))
6653 TREE_CHAIN (last
) = TREE_PURPOSE (parm
);
6654 last
= TREE_PURPOSE (parm
);
6656 TREE_CHAIN (last
) = 0;
6659 pointer_set_destroy (seen_args
);
6661 /* If there was a previous prototype,
6662 set the DECL_ARG_TYPE of each argument according to
6663 the type previously specified, and report any mismatches. */
6665 if (current_function_prototype_arg_types
)
6668 for (parm
= DECL_ARGUMENTS (fndecl
),
6669 type
= current_function_prototype_arg_types
;
6670 parm
|| (type
&& TREE_VALUE (type
) != error_mark_node
6671 && (TYPE_MAIN_VARIANT (TREE_VALUE (type
)) != void_type_node
));
6672 parm
= TREE_CHAIN (parm
), type
= TREE_CHAIN (type
))
6674 if (parm
== 0 || type
== 0
6675 || TYPE_MAIN_VARIANT (TREE_VALUE (type
)) == void_type_node
)
6677 if (current_function_prototype_built_in
)
6678 warning (0, "number of arguments doesn%'t match "
6679 "built-in prototype");
6682 error ("number of arguments doesn%'t match prototype");
6683 error ("%Hprototype declaration",
6684 ¤t_function_prototype_locus
);
6688 /* Type for passing arg must be consistent with that
6689 declared for the arg. ISO C says we take the unqualified
6690 type for parameters declared with qualified type. */
6691 if (TREE_TYPE (parm
) != error_mark_node
6692 && TREE_TYPE (type
) != error_mark_node
6693 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm
)),
6694 TYPE_MAIN_VARIANT (TREE_VALUE (type
))))
6696 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm
))
6697 == TYPE_MAIN_VARIANT (TREE_VALUE (type
)))
6699 /* Adjust argument to match prototype. E.g. a previous
6700 `int foo(float);' prototype causes
6701 `int foo(x) float x; {...}' to be treated like
6702 `int foo(float x) {...}'. This is particularly
6703 useful for argument types like uid_t. */
6704 DECL_ARG_TYPE (parm
) = TREE_TYPE (parm
);
6706 if (targetm
.calls
.promote_prototypes (TREE_TYPE (current_function_decl
))
6707 && INTEGRAL_TYPE_P (TREE_TYPE (parm
))
6708 && TYPE_PRECISION (TREE_TYPE (parm
))
6709 < TYPE_PRECISION (integer_type_node
))
6710 DECL_ARG_TYPE (parm
) = integer_type_node
;
6712 /* ??? Is it possible to get here with a
6713 built-in prototype or will it always have
6714 been diagnosed as conflicting with an
6715 old-style definition and discarded? */
6716 if (current_function_prototype_built_in
)
6717 warning (OPT_pedantic
, "promoted argument %qD "
6718 "doesn%'t match built-in prototype", parm
);
6721 pedwarn (input_location
, OPT_pedantic
, "promoted argument %qD "
6722 "doesn%'t match prototype", parm
);
6723 pedwarn (current_function_prototype_locus
, OPT_pedantic
,
6724 "prototype declaration");
6729 if (current_function_prototype_built_in
)
6730 warning (0, "argument %qD doesn%'t match "
6731 "built-in prototype", parm
);
6734 error ("argument %qD doesn%'t match prototype", parm
);
6735 error ("%Hprototype declaration",
6736 ¤t_function_prototype_locus
);
6741 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = 0;
6744 /* Otherwise, create a prototype that would match. */
6748 tree actual
= 0, last
= 0, type
;
6750 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= TREE_CHAIN (parm
))
6752 type
= tree_cons (NULL_TREE
, DECL_ARG_TYPE (parm
), NULL_TREE
);
6754 TREE_CHAIN (last
) = type
;
6759 type
= tree_cons (NULL_TREE
, void_type_node
, NULL_TREE
);
6761 TREE_CHAIN (last
) = type
;
6765 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6766 of the type of this function, but we need to avoid having this
6767 affect the types of other similarly-typed functions, so we must
6768 first force the generation of an identical (but separate) type
6769 node for the relevant function type. The new node we create
6770 will be a variant of the main variant of the original function
6773 TREE_TYPE (fndecl
) = build_variant_type_copy (TREE_TYPE (fndecl
));
6775 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = actual
;
6779 /* Store parameter declarations passed in ARG_INFO into the current
6780 function declaration. */
6783 store_parm_decls_from (struct c_arg_info
*arg_info
)
6785 current_function_arg_info
= arg_info
;
6786 store_parm_decls ();
6789 /* Store the parameter declarations into the current function declaration.
6790 This is called after parsing the parameter declarations, before
6791 digesting the body of the function.
6793 For an old-style definition, construct a prototype out of the old-style
6794 parameter declarations and inject it into the function's type. */
6797 store_parm_decls (void)
6799 tree fndecl
= current_function_decl
;
6802 /* The argument information block for FNDECL. */
6803 struct c_arg_info
*arg_info
= current_function_arg_info
;
6804 current_function_arg_info
= 0;
6806 /* True if this definition is written with a prototype. Note:
6807 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6808 list in a function definition as equivalent to (void) -- an
6809 empty argument list specifies the function has no parameters,
6810 but only (void) sets up a prototype for future calls. */
6811 proto
= arg_info
->types
!= 0;
6814 store_parm_decls_newstyle (fndecl
, arg_info
);
6816 store_parm_decls_oldstyle (fndecl
, arg_info
);
6818 /* The next call to push_scope will be a function body. */
6820 next_is_function_body
= true;
6822 /* Write a record describing this function definition to the prototypes
6823 file (if requested). */
6825 gen_aux_info_record (fndecl
, 1, 0, proto
);
6827 /* Initialize the RTL code for the function. */
6828 allocate_struct_function (fndecl
, false);
6830 /* Begin the statement tree for this function. */
6831 DECL_SAVED_TREE (fndecl
) = push_stmt_list ();
6833 /* ??? Insert the contents of the pending sizes list into the function
6834 to be evaluated. The only reason left to have this is
6835 void foo(int n, int array[n++])
6836 because we throw away the array type in favor of a pointer type, and
6837 thus won't naturally see the SAVE_EXPR containing the increment. All
6838 other pending sizes would be handled by gimplify_parameters. */
6841 for (t
= nreverse (get_pending_sizes ()); t
; t
= TREE_CHAIN (t
))
6842 add_stmt (TREE_VALUE (t
));
6845 /* Even though we're inside a function body, we still don't want to
6846 call expand_expr to calculate the size of a variable-sized array.
6847 We haven't necessarily assigned RTL to all variables yet, so it's
6848 not safe to try to expand expressions involving them. */
6849 cfun
->dont_save_pending_sizes_p
= 1;
6852 /* Emit diagnostics that require gimple input for detection. Operate on
6853 FNDECL and all its nested functions. */
6856 c_gimple_diagnostics_recursively (tree fndecl
)
6858 struct cgraph_node
*cgn
;
6859 gimple_seq body
= gimple_body (fndecl
);
6861 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6862 c_warn_unused_result (body
);
6864 /* Notice when OpenMP structured block constraints are violated. */
6866 diagnose_omp_structured_block_errors (fndecl
);
6868 /* Finalize all nested functions now. */
6869 cgn
= cgraph_node (fndecl
);
6870 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
6871 c_gimple_diagnostics_recursively (cgn
->decl
);
6874 /* Finish up a function declaration and compile that function
6875 all the way to assembler language output. The free the storage
6876 for the function definition.
6878 This is called after parsing the body of the function definition. */
6881 finish_function (void)
6883 tree fndecl
= current_function_decl
;
6885 label_context_stack_se
= label_context_stack_se
->next
;
6886 label_context_stack_vm
= label_context_stack_vm
->next
;
6888 if (TREE_CODE (fndecl
) == FUNCTION_DECL
6889 && targetm
.calls
.promote_prototypes (TREE_TYPE (fndecl
)))
6891 tree args
= DECL_ARGUMENTS (fndecl
);
6892 for (; args
; args
= TREE_CHAIN (args
))
6894 tree type
= TREE_TYPE (args
);
6895 if (INTEGRAL_TYPE_P (type
)
6896 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
6897 DECL_ARG_TYPE (args
) = integer_type_node
;
6901 if (DECL_INITIAL (fndecl
) && DECL_INITIAL (fndecl
) != error_mark_node
)
6902 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
6904 /* Must mark the RESULT_DECL as being in this function. */
6906 if (DECL_RESULT (fndecl
) && DECL_RESULT (fndecl
) != error_mark_node
)
6907 DECL_CONTEXT (DECL_RESULT (fndecl
)) = fndecl
;
6909 if (MAIN_NAME_P (DECL_NAME (fndecl
)) && flag_hosted
6910 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl
)))
6911 == integer_type_node
&& flag_isoc99
)
6913 tree stmt
= c_finish_return (integer_zero_node
, NULL_TREE
);
6914 /* Hack. We don't want the middle-end to warn that this return
6915 is unreachable, so we mark its location as special. Using
6916 UNKNOWN_LOCATION has the problem that it gets clobbered in
6917 annotate_one_with_locus. A cleaner solution might be to
6918 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6920 SET_EXPR_LOCATION (stmt
, BUILTINS_LOCATION
);
6923 /* Tie off the statement tree for this function. */
6924 DECL_SAVED_TREE (fndecl
) = pop_stmt_list (DECL_SAVED_TREE (fndecl
));
6926 finish_fname_decls ();
6928 /* Complain if there's just no return statement. */
6929 if (warn_return_type
6930 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl
))) != VOID_TYPE
6931 && !current_function_returns_value
&& !current_function_returns_null
6932 /* Don't complain if we are no-return. */
6933 && !current_function_returns_abnormally
6934 /* Don't warn for main(). */
6935 && !MAIN_NAME_P (DECL_NAME (fndecl
))
6936 /* Or if they didn't actually specify a return type. */
6937 && !C_FUNCTION_IMPLICIT_INT (fndecl
)
6938 /* Normally, with -Wreturn-type, flow will complain, but we might
6939 optimize out static functions. */
6940 && !TREE_PUBLIC (fndecl
))
6942 warning (OPT_Wreturn_type
,
6943 "no return statement in function returning non-void");
6944 TREE_NO_WARNING (fndecl
) = 1;
6947 /* Store the end of the function, so that we get good line number
6948 info for the epilogue. */
6949 cfun
->function_end_locus
= input_location
;
6951 /* Finalize the ELF visibility for the function. */
6952 c_determine_visibility (fndecl
);
6954 /* For GNU C extern inline functions disregard inline limits. */
6955 if (DECL_EXTERNAL (fndecl
)
6956 && DECL_DECLARED_INLINE_P (fndecl
))
6957 DECL_DISREGARD_INLINE_LIMITS (fndecl
) = 1;
6959 /* Genericize before inlining. Delay genericizing nested functions
6960 until their parent function is genericized. Since finalizing
6961 requires GENERIC, delay that as well. */
6963 if (DECL_INITIAL (fndecl
) && DECL_INITIAL (fndecl
) != error_mark_node
6964 && !undef_nested_function
)
6966 if (!decl_function_context (fndecl
))
6968 c_genericize (fndecl
);
6969 c_gimple_diagnostics_recursively (fndecl
);
6971 /* ??? Objc emits functions after finalizing the compilation unit.
6972 This should be cleaned up later and this conditional removed. */
6973 if (cgraph_global_info_ready
)
6975 cgraph_add_new_function (fndecl
, false);
6979 cgraph_finalize_function (fndecl
, false);
6983 /* Register this function with cgraph just far enough to get it
6984 added to our parent's nested function list. Handy, since the
6985 C front end doesn't have such a list. */
6986 (void) cgraph_node (fndecl
);
6990 if (!decl_function_context (fndecl
))
6991 undef_nested_function
= false;
6993 /* We're leaving the context of this function, so zap cfun.
6994 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6995 tree_rest_of_compilation. */
6997 current_function_decl
= NULL
;
7000 /* Check the declarations given in a for-loop for satisfying the C99
7001 constraints. If exactly one such decl is found, return it. */
7004 check_for_loop_decls (void)
7006 struct c_binding
*b
;
7007 tree one_decl
= NULL_TREE
;
7012 static bool hint
= true;
7013 /* If we get here, declarations have been used in a for loop without
7014 the C99 for loop scope. This doesn't make much sense, so don't
7016 error ("%<for%> loop initial declarations are only allowed in C99 mode");
7019 inform (input_location
,
7020 "use option -std=c99 or -std=gnu99 to compile your code");
7025 /* C99 subclause 6.8.5 paragraph 3:
7027 [#3] The declaration part of a for statement shall only
7028 declare identifiers for objects having storage class auto or
7031 It isn't clear whether, in this sentence, "identifiers" binds to
7032 "shall only declare" or to "objects" - that is, whether all identifiers
7033 declared must be identifiers for objects, or whether the restriction
7034 only applies to those that are. (A question on this in comp.std.c
7035 in November 2000 received no answer.) We implement the strictest
7036 interpretation, to avoid creating an extension which later causes
7039 for (b
= current_scope
->bindings
; b
; b
= b
->prev
)
7042 tree decl
= b
->decl
;
7047 switch (TREE_CODE (decl
))
7050 if (TREE_STATIC (decl
))
7051 error ("declaration of static variable %q+D in %<for%> loop "
7052 "initial declaration", decl
);
7053 else if (DECL_EXTERNAL (decl
))
7054 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
7055 "initial declaration", decl
);
7059 error ("%<struct %E%> declared in %<for%> loop initial declaration",
7063 error ("%<union %E%> declared in %<for%> loop initial declaration",
7067 error ("%<enum %E%> declared in %<for%> loop initial declaration",
7071 error ("declaration of non-variable %q+D in %<for%> loop "
7072 "initial declaration", decl
);
7079 return n_decls
== 1 ? one_decl
: NULL_TREE
;
7082 /* Save and reinitialize the variables
7083 used during compilation of a C function. */
7086 c_push_function_context (void)
7088 struct language_function
*p
;
7089 p
= GGC_NEW (struct language_function
);
7092 p
->base
.x_stmt_tree
= c_stmt_tree
;
7093 p
->x_break_label
= c_break_label
;
7094 p
->x_cont_label
= c_cont_label
;
7095 p
->x_switch_stack
= c_switch_stack
;
7096 p
->arg_info
= current_function_arg_info
;
7097 p
->returns_value
= current_function_returns_value
;
7098 p
->returns_null
= current_function_returns_null
;
7099 p
->returns_abnormally
= current_function_returns_abnormally
;
7100 p
->warn_about_return_type
= warn_about_return_type
;
7102 push_function_context ();
7105 /* Restore the variables used during compilation of a C function. */
7108 c_pop_function_context (void)
7110 struct language_function
*p
;
7112 pop_function_context ();
7114 cfun
->language
= NULL
;
7116 if (DECL_STRUCT_FUNCTION (current_function_decl
) == 0
7117 && DECL_SAVED_TREE (current_function_decl
) == NULL_TREE
)
7119 /* Stop pointing to the local nodes about to be freed. */
7120 /* But DECL_INITIAL must remain nonzero so we know this
7121 was an actual function definition. */
7122 DECL_INITIAL (current_function_decl
) = error_mark_node
;
7123 DECL_ARGUMENTS (current_function_decl
) = 0;
7126 c_stmt_tree
= p
->base
.x_stmt_tree
;
7127 c_break_label
= p
->x_break_label
;
7128 c_cont_label
= p
->x_cont_label
;
7129 c_switch_stack
= p
->x_switch_stack
;
7130 current_function_arg_info
= p
->arg_info
;
7131 current_function_returns_value
= p
->returns_value
;
7132 current_function_returns_null
= p
->returns_null
;
7133 current_function_returns_abnormally
= p
->returns_abnormally
;
7134 warn_about_return_type
= p
->warn_about_return_type
;
7137 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
7140 c_dup_lang_specific_decl (tree decl
)
7142 struct lang_decl
*ld
;
7144 if (!DECL_LANG_SPECIFIC (decl
))
7147 ld
= GGC_NEW (struct lang_decl
);
7148 memcpy (ld
, DECL_LANG_SPECIFIC (decl
), sizeof (struct lang_decl
));
7149 DECL_LANG_SPECIFIC (decl
) = ld
;
7152 /* The functions below are required for functionality of doing
7153 function at once processing in the C front end. Currently these
7154 functions are not called from anywhere in the C front end, but as
7155 these changes continue, that will change. */
7157 /* Returns the stmt_tree (if any) to which statements are currently
7158 being added. If there is no active statement-tree, NULL is
7162 current_stmt_tree (void)
7164 return &c_stmt_tree
;
7167 /* Return the global value of T as a symbol. */
7170 identifier_global_value (tree t
)
7172 struct c_binding
*b
;
7174 for (b
= I_SYMBOL_BINDING (t
); b
; b
= b
->shadowed
)
7175 if (B_IN_FILE_SCOPE (b
) || B_IN_EXTERNAL_SCOPE (b
))
7181 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
7182 otherwise the name is found in ridpointers from RID_INDEX. */
7185 record_builtin_type (enum rid rid_index
, const char *name
, tree type
)
7189 id
= ridpointers
[(int) rid_index
];
7191 id
= get_identifier (name
);
7192 decl
= build_decl (TYPE_DECL
, id
, type
);
7194 if (debug_hooks
->type_decl
)
7195 debug_hooks
->type_decl (decl
, false);
7198 /* Build the void_list_node (void_type_node having been created). */
7200 build_void_list_node (void)
7202 tree t
= build_tree_list (NULL_TREE
, void_type_node
);
7206 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7209 build_c_parm (struct c_declspecs
*specs
, tree attrs
,
7210 struct c_declarator
*declarator
)
7212 struct c_parm
*ret
= XOBNEW (&parser_obstack
, struct c_parm
);
7215 ret
->declarator
= declarator
;
7219 /* Return a declarator with nested attributes. TARGET is the inner
7220 declarator to which these attributes apply. ATTRS are the
7223 struct c_declarator
*
7224 build_attrs_declarator (tree attrs
, struct c_declarator
*target
)
7226 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
7227 ret
->kind
= cdk_attrs
;
7228 ret
->declarator
= target
;
7229 ret
->u
.attrs
= attrs
;
7233 /* Return a declarator for a function with arguments specified by ARGS
7234 and return type specified by TARGET. */
7236 struct c_declarator
*
7237 build_function_declarator (struct c_arg_info
*args
,
7238 struct c_declarator
*target
)
7240 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
7241 ret
->kind
= cdk_function
;
7242 ret
->declarator
= target
;
7243 ret
->u
.arg_info
= args
;
7247 /* Return a declarator for the identifier IDENT (which may be
7248 NULL_TREE for an abstract declarator). */
7250 struct c_declarator
*
7251 build_id_declarator (tree ident
)
7253 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
7255 ret
->declarator
= 0;
7257 /* Default value - may get reset to a more precise location. */
7258 ret
->id_loc
= input_location
;
7262 /* Return something to represent absolute declarators containing a *.
7263 TARGET is the absolute declarator that the * contains.
7264 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7265 to apply to the pointer type. */
7267 struct c_declarator
*
7268 make_pointer_declarator (struct c_declspecs
*type_quals_attrs
,
7269 struct c_declarator
*target
)
7273 struct c_declarator
*itarget
= target
;
7274 struct c_declarator
*ret
= XOBNEW (&parser_obstack
, struct c_declarator
);
7275 if (type_quals_attrs
)
7277 attrs
= type_quals_attrs
->attrs
;
7278 quals
= quals_from_declspecs (type_quals_attrs
);
7279 if (attrs
!= NULL_TREE
)
7280 itarget
= build_attrs_declarator (attrs
, target
);
7282 ret
->kind
= cdk_pointer
;
7283 ret
->declarator
= itarget
;
7284 ret
->u
.pointer_quals
= quals
;
7288 /* Return a pointer to a structure for an empty list of declaration
7291 struct c_declspecs
*
7292 build_null_declspecs (void)
7294 struct c_declspecs
*ret
= XOBNEW (&parser_obstack
, struct c_declspecs
);
7299 ret
->typespec_word
= cts_none
;
7300 ret
->storage_class
= csc_none
;
7301 ret
->expr_const_operands
= true;
7302 ret
->declspecs_seen_p
= false;
7303 ret
->type_seen_p
= false;
7304 ret
->non_sc_seen_p
= false;
7305 ret
->typedef_p
= false;
7306 ret
->tag_defined_p
= false;
7307 ret
->explicit_signed_p
= false;
7308 ret
->deprecated_p
= false;
7309 ret
->default_int_p
= false;
7310 ret
->long_p
= false;
7311 ret
->long_long_p
= false;
7312 ret
->short_p
= false;
7313 ret
->signed_p
= false;
7314 ret
->unsigned_p
= false;
7315 ret
->complex_p
= false;
7316 ret
->inline_p
= false;
7317 ret
->thread_p
= false;
7318 ret
->const_p
= false;
7319 ret
->volatile_p
= false;
7320 ret
->restrict_p
= false;
7321 ret
->saturating_p
= false;
7325 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7328 struct c_declspecs
*
7329 declspecs_add_qual (struct c_declspecs
*specs
, tree qual
)
7333 specs
->non_sc_seen_p
= true;
7334 specs
->declspecs_seen_p
= true;
7335 gcc_assert (TREE_CODE (qual
) == IDENTIFIER_NODE
7336 && C_IS_RESERVED_WORD (qual
));
7337 i
= C_RID_CODE (qual
);
7341 dupe
= specs
->const_p
;
7342 specs
->const_p
= true;
7345 dupe
= specs
->volatile_p
;
7346 specs
->volatile_p
= true;
7349 dupe
= specs
->restrict_p
;
7350 specs
->restrict_p
= true;
7355 if (dupe
&& !flag_isoc99
)
7356 pedwarn (input_location
, OPT_pedantic
, "duplicate %qE", qual
);
7360 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7363 struct c_declspecs
*
7364 declspecs_add_type (struct c_declspecs
*specs
, struct c_typespec spec
)
7366 tree type
= spec
.spec
;
7367 specs
->non_sc_seen_p
= true;
7368 specs
->declspecs_seen_p
= true;
7369 specs
->type_seen_p
= true;
7370 if (TREE_DEPRECATED (type
))
7371 specs
->deprecated_p
= true;
7373 /* Handle type specifier keywords. */
7374 if (TREE_CODE (type
) == IDENTIFIER_NODE
7375 && C_IS_RESERVED_WORD (type
)
7376 && C_RID_CODE (type
) != RID_CXX_COMPAT_WARN
)
7378 enum rid i
= C_RID_CODE (type
);
7381 error ("two or more data types in declaration specifiers");
7384 if ((int) i
<= (int) RID_LAST_MODIFIER
)
7386 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
7391 if (specs
->long_long_p
)
7393 error ("%<long long long%> is too long for GCC");
7398 if (specs
->typespec_word
== cts_double
)
7400 error ("both %<long long%> and %<double%> in "
7401 "declaration specifiers");
7404 pedwarn_c90 (input_location
, OPT_Wlong_long
,
7405 "ISO C90 does not support %<long long%>");
7406 specs
->long_long_p
= 1;
7410 error ("both %<long%> and %<short%> in "
7411 "declaration specifiers");
7412 else if (specs
->typespec_word
== cts_void
)
7413 error ("both %<long%> and %<void%> in "
7414 "declaration specifiers");
7415 else if (specs
->typespec_word
== cts_bool
)
7416 error ("both %<long%> and %<_Bool%> in "
7417 "declaration specifiers");
7418 else if (specs
->typespec_word
== cts_char
)
7419 error ("both %<long%> and %<char%> in "
7420 "declaration specifiers");
7421 else if (specs
->typespec_word
== cts_float
)
7422 error ("both %<long%> and %<float%> in "
7423 "declaration specifiers");
7424 else if (specs
->typespec_word
== cts_dfloat32
)
7425 error ("both %<long%> and %<_Decimal32%> in "
7426 "declaration specifiers");
7427 else if (specs
->typespec_word
== cts_dfloat64
)
7428 error ("both %<long%> and %<_Decimal64%> in "
7429 "declaration specifiers");
7430 else if (specs
->typespec_word
== cts_dfloat128
)
7431 error ("both %<long%> and %<_Decimal128%> in "
7432 "declaration specifiers");
7434 specs
->long_p
= true;
7437 dupe
= specs
->short_p
;
7439 error ("both %<long%> and %<short%> in "
7440 "declaration specifiers");
7441 else if (specs
->typespec_word
== cts_void
)
7442 error ("both %<short%> and %<void%> in "
7443 "declaration specifiers");
7444 else if (specs
->typespec_word
== cts_bool
)
7445 error ("both %<short%> and %<_Bool%> in "
7446 "declaration specifiers");
7447 else if (specs
->typespec_word
== cts_char
)
7448 error ("both %<short%> and %<char%> in "
7449 "declaration specifiers");
7450 else if (specs
->typespec_word
== cts_float
)
7451 error ("both %<short%> and %<float%> in "
7452 "declaration specifiers");
7453 else if (specs
->typespec_word
== cts_double
)
7454 error ("both %<short%> and %<double%> in "
7455 "declaration specifiers");
7456 else if (specs
->typespec_word
== cts_dfloat32
)
7457 error ("both %<short%> and %<_Decimal32%> in "
7458 "declaration specifiers");
7459 else if (specs
->typespec_word
== cts_dfloat64
)
7460 error ("both %<short%> and %<_Decimal64%> in "
7461 "declaration specifiers");
7462 else if (specs
->typespec_word
== cts_dfloat128
)
7463 error ("both %<short%> and %<_Decimal128%> in "
7464 "declaration specifiers");
7466 specs
->short_p
= true;
7469 dupe
= specs
->signed_p
;
7470 if (specs
->unsigned_p
)
7471 error ("both %<signed%> and %<unsigned%> in "
7472 "declaration specifiers");
7473 else if (specs
->typespec_word
== cts_void
)
7474 error ("both %<signed%> and %<void%> in "
7475 "declaration specifiers");
7476 else if (specs
->typespec_word
== cts_bool
)
7477 error ("both %<signed%> and %<_Bool%> in "
7478 "declaration specifiers");
7479 else if (specs
->typespec_word
== cts_float
)
7480 error ("both %<signed%> and %<float%> in "
7481 "declaration specifiers");
7482 else if (specs
->typespec_word
== cts_double
)
7483 error ("both %<signed%> and %<double%> in "
7484 "declaration specifiers");
7485 else if (specs
->typespec_word
== cts_dfloat32
)
7486 error ("both %<signed%> and %<_Decimal32%> in "
7487 "declaration specifiers");
7488 else if (specs
->typespec_word
== cts_dfloat64
)
7489 error ("both %<signed%> and %<_Decimal64%> in "
7490 "declaration specifiers");
7491 else if (specs
->typespec_word
== cts_dfloat128
)
7492 error ("both %<signed%> and %<_Decimal128%> in "
7493 "declaration specifiers");
7495 specs
->signed_p
= true;
7498 dupe
= specs
->unsigned_p
;
7499 if (specs
->signed_p
)
7500 error ("both %<signed%> and %<unsigned%> in "
7501 "declaration specifiers");
7502 else if (specs
->typespec_word
== cts_void
)
7503 error ("both %<unsigned%> and %<void%> in "
7504 "declaration specifiers");
7505 else if (specs
->typespec_word
== cts_bool
)
7506 error ("both %<unsigned%> and %<_Bool%> in "
7507 "declaration specifiers");
7508 else if (specs
->typespec_word
== cts_float
)
7509 error ("both %<unsigned%> and %<float%> in "
7510 "declaration specifiers");
7511 else if (specs
->typespec_word
== cts_double
)
7512 error ("both %<unsigned%> and %<double%> in "
7513 "declaration specifiers");
7514 else if (specs
->typespec_word
== cts_dfloat32
)
7515 error ("both %<unsigned%> and %<_Decimal32%> in "
7516 "declaration specifiers");
7517 else if (specs
->typespec_word
== cts_dfloat64
)
7518 error ("both %<unsigned%> and %<_Decimal64%> in "
7519 "declaration specifiers");
7520 else if (specs
->typespec_word
== cts_dfloat128
)
7521 error ("both %<unsigned%> and %<_Decimal128%> in "
7522 "declaration specifiers");
7524 specs
->unsigned_p
= true;
7527 dupe
= specs
->complex_p
;
7528 if (!flag_isoc99
&& !in_system_header
)
7529 pedwarn (input_location
, OPT_pedantic
, "ISO C90 does not support complex types");
7530 if (specs
->typespec_word
== cts_void
)
7531 error ("both %<complex%> and %<void%> in "
7532 "declaration specifiers");
7533 else if (specs
->typespec_word
== cts_bool
)
7534 error ("both %<complex%> and %<_Bool%> in "
7535 "declaration specifiers");
7536 else if (specs
->typespec_word
== cts_dfloat32
)
7537 error ("both %<complex%> and %<_Decimal32%> in "
7538 "declaration specifiers");
7539 else if (specs
->typespec_word
== cts_dfloat64
)
7540 error ("both %<complex%> and %<_Decimal64%> in "
7541 "declaration specifiers");
7542 else if (specs
->typespec_word
== cts_dfloat128
)
7543 error ("both %<complex%> and %<_Decimal128%> in "
7544 "declaration specifiers");
7545 else if (specs
->typespec_word
== cts_fract
)
7546 error ("both %<complex%> and %<_Fract%> in "
7547 "declaration specifiers");
7548 else if (specs
->typespec_word
== cts_accum
)
7549 error ("both %<complex%> and %<_Accum%> in "
7550 "declaration specifiers");
7551 else if (specs
->saturating_p
)
7552 error ("both %<complex%> and %<_Sat%> in "
7553 "declaration specifiers");
7555 specs
->complex_p
= true;
7558 dupe
= specs
->saturating_p
;
7559 pedwarn (input_location
, OPT_pedantic
, "ISO C does not support saturating types");
7560 if (specs
->typespec_word
== cts_void
)
7561 error ("both %<_Sat%> and %<void%> in "
7562 "declaration specifiers");
7563 else if (specs
->typespec_word
== cts_bool
)
7564 error ("both %<_Sat%> and %<_Bool%> in "
7565 "declaration specifiers");
7566 else if (specs
->typespec_word
== cts_char
)
7567 error ("both %<_Sat%> and %<char%> in "
7568 "declaration specifiers");
7569 else if (specs
->typespec_word
== cts_int
)
7570 error ("both %<_Sat%> and %<int%> in "
7571 "declaration specifiers");
7572 else if (specs
->typespec_word
== cts_float
)
7573 error ("both %<_Sat%> and %<float%> in "
7574 "declaration specifiers");
7575 else if (specs
->typespec_word
== cts_double
)
7576 error ("both %<_Sat%> and %<double%> in "
7577 "declaration specifiers");
7578 else if (specs
->typespec_word
== cts_dfloat32
)
7579 error ("both %<_Sat%> and %<_Decimal32%> in "
7580 "declaration specifiers");
7581 else if (specs
->typespec_word
== cts_dfloat64
)
7582 error ("both %<_Sat%> and %<_Decimal64%> in "
7583 "declaration specifiers");
7584 else if (specs
->typespec_word
== cts_dfloat128
)
7585 error ("both %<_Sat%> and %<_Decimal128%> in "
7586 "declaration specifiers");
7587 else if (specs
->complex_p
)
7588 error ("both %<_Sat%> and %<complex%> in "
7589 "declaration specifiers");
7591 specs
->saturating_p
= true;
7598 error ("duplicate %qE", type
);
7604 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
7605 "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
7606 if (specs
->typespec_word
!= cts_none
)
7608 error ("two or more data types in declaration specifiers");
7615 error ("both %<long%> and %<void%> in "
7616 "declaration specifiers");
7617 else if (specs
->short_p
)
7618 error ("both %<short%> and %<void%> in "
7619 "declaration specifiers");
7620 else if (specs
->signed_p
)
7621 error ("both %<signed%> and %<void%> in "
7622 "declaration specifiers");
7623 else if (specs
->unsigned_p
)
7624 error ("both %<unsigned%> and %<void%> in "
7625 "declaration specifiers");
7626 else if (specs
->complex_p
)
7627 error ("both %<complex%> and %<void%> in "
7628 "declaration specifiers");
7629 else if (specs
->saturating_p
)
7630 error ("both %<_Sat%> and %<void%> in "
7631 "declaration specifiers");
7633 specs
->typespec_word
= cts_void
;
7637 error ("both %<long%> and %<_Bool%> in "
7638 "declaration specifiers");
7639 else if (specs
->short_p
)
7640 error ("both %<short%> and %<_Bool%> in "
7641 "declaration specifiers");
7642 else if (specs
->signed_p
)
7643 error ("both %<signed%> and %<_Bool%> in "
7644 "declaration specifiers");
7645 else if (specs
->unsigned_p
)
7646 error ("both %<unsigned%> and %<_Bool%> in "
7647 "declaration specifiers");
7648 else if (specs
->complex_p
)
7649 error ("both %<complex%> and %<_Bool%> in "
7650 "declaration specifiers");
7651 else if (specs
->saturating_p
)
7652 error ("both %<_Sat%> and %<_Bool%> in "
7653 "declaration specifiers");
7655 specs
->typespec_word
= cts_bool
;
7659 error ("both %<long%> and %<char%> in "
7660 "declaration specifiers");
7661 else if (specs
->short_p
)
7662 error ("both %<short%> and %<char%> in "
7663 "declaration specifiers");
7664 else if (specs
->saturating_p
)
7665 error ("both %<_Sat%> and %<char%> in "
7666 "declaration specifiers");
7668 specs
->typespec_word
= cts_char
;
7671 if (specs
->saturating_p
)
7672 error ("both %<_Sat%> and %<int%> in "
7673 "declaration specifiers");
7675 specs
->typespec_word
= cts_int
;
7679 error ("both %<long%> and %<float%> in "
7680 "declaration specifiers");
7681 else if (specs
->short_p
)
7682 error ("both %<short%> and %<float%> in "
7683 "declaration specifiers");
7684 else if (specs
->signed_p
)
7685 error ("both %<signed%> and %<float%> in "
7686 "declaration specifiers");
7687 else if (specs
->unsigned_p
)
7688 error ("both %<unsigned%> and %<float%> in "
7689 "declaration specifiers");
7690 else if (specs
->saturating_p
)
7691 error ("both %<_Sat%> and %<float%> in "
7692 "declaration specifiers");
7694 specs
->typespec_word
= cts_float
;
7697 if (specs
->long_long_p
)
7698 error ("both %<long long%> and %<double%> in "
7699 "declaration specifiers");
7700 else if (specs
->short_p
)
7701 error ("both %<short%> and %<double%> in "
7702 "declaration specifiers");
7703 else if (specs
->signed_p
)
7704 error ("both %<signed%> and %<double%> in "
7705 "declaration specifiers");
7706 else if (specs
->unsigned_p
)
7707 error ("both %<unsigned%> and %<double%> in "
7708 "declaration specifiers");
7709 else if (specs
->saturating_p
)
7710 error ("both %<_Sat%> and %<double%> in "
7711 "declaration specifiers");
7713 specs
->typespec_word
= cts_double
;
7720 if (i
== RID_DFLOAT32
)
7722 else if (i
== RID_DFLOAT64
)
7725 str
= "_Decimal128";
7726 if (specs
->long_long_p
)
7727 error ("both %<long long%> and %<%s%> in "
7728 "declaration specifiers", str
);
7730 error ("both %<long%> and %<%s%> in "
7731 "declaration specifiers", str
);
7732 else if (specs
->short_p
)
7733 error ("both %<short%> and %<%s%> in "
7734 "declaration specifiers", str
);
7735 else if (specs
->signed_p
)
7736 error ("both %<signed%> and %<%s%> in "
7737 "declaration specifiers", str
);
7738 else if (specs
->unsigned_p
)
7739 error ("both %<unsigned%> and %<%s%> in "
7740 "declaration specifiers", str
);
7741 else if (specs
->complex_p
)
7742 error ("both %<complex%> and %<%s%> in "
7743 "declaration specifiers", str
);
7744 else if (specs
->saturating_p
)
7745 error ("both %<_Sat%> and %<%s%> in "
7746 "declaration specifiers", str
);
7747 else if (i
== RID_DFLOAT32
)
7748 specs
->typespec_word
= cts_dfloat32
;
7749 else if (i
== RID_DFLOAT64
)
7750 specs
->typespec_word
= cts_dfloat64
;
7752 specs
->typespec_word
= cts_dfloat128
;
7754 if (!targetm
.decimal_float_supported_p ())
7755 error ("decimal floating point not supported for this target");
7756 pedwarn (input_location
, OPT_pedantic
,
7757 "ISO C does not support decimal floating point");
7767 if (specs
->complex_p
)
7768 error ("both %<complex%> and %<%s%> in "
7769 "declaration specifiers", str
);
7770 else if (i
== RID_FRACT
)
7771 specs
->typespec_word
= cts_fract
;
7773 specs
->typespec_word
= cts_accum
;
7775 if (!targetm
.fixed_point_supported_p ())
7776 error ("fixed-point types not supported for this target");
7777 pedwarn (input_location
, OPT_pedantic
,
7778 "ISO C does not support fixed-point types");
7781 /* ObjC reserved word "id", handled below. */
7787 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7788 form of ObjC type, cases such as "int" and "long" being handled
7789 above), a TYPE (struct, union, enum and typeof specifiers) or an
7790 ERROR_MARK. In none of these cases may there have previously
7791 been any type specifiers. */
7792 if (specs
->type
|| specs
->typespec_word
!= cts_none
7793 || specs
->long_p
|| specs
->short_p
|| specs
->signed_p
7794 || specs
->unsigned_p
|| specs
->complex_p
)
7795 error ("two or more data types in declaration specifiers");
7796 else if (TREE_CODE (type
) == TYPE_DECL
)
7798 if (TREE_TYPE (type
) == error_mark_node
)
7799 ; /* Allow the type to default to int to avoid cascading errors. */
7802 specs
->type
= TREE_TYPE (type
);
7803 specs
->decl_attr
= DECL_ATTRIBUTES (type
);
7804 specs
->typedef_p
= true;
7805 specs
->explicit_signed_p
= C_TYPEDEF_EXPLICITLY_SIGNED (type
);
7808 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
7810 tree t
= lookup_name (type
);
7811 if (!t
|| TREE_CODE (t
) != TYPE_DECL
)
7812 error ("%qE fails to be a typedef or built in type", type
);
7813 else if (TREE_TYPE (t
) == error_mark_node
)
7816 specs
->type
= TREE_TYPE (t
);
7818 else if (TREE_CODE (type
) != ERROR_MARK
)
7820 if (spec
.kind
== ctsk_tagdef
|| spec
.kind
== ctsk_tagfirstref
)
7821 specs
->tag_defined_p
= true;
7822 if (spec
.kind
== ctsk_typeof
)
7824 specs
->typedef_p
= true;
7828 specs
->expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (spec
.expr
),
7829 specs
->expr
, spec
.expr
);
7831 specs
->expr
= spec
.expr
;
7832 specs
->expr_const_operands
&= spec
.expr_const_operands
;
7841 /* Add the storage class specifier or function specifier SCSPEC to the
7842 declaration specifiers SPECS, returning SPECS. */
7844 struct c_declspecs
*
7845 declspecs_add_scspec (struct c_declspecs
*specs
, tree scspec
)
7848 enum c_storage_class n
= csc_none
;
7850 specs
->declspecs_seen_p
= true;
7851 gcc_assert (TREE_CODE (scspec
) == IDENTIFIER_NODE
7852 && C_IS_RESERVED_WORD (scspec
));
7853 i
= C_RID_CODE (scspec
);
7854 if (specs
->non_sc_seen_p
)
7855 warning (OPT_Wold_style_declaration
,
7856 "%qE is not at beginning of declaration", scspec
);
7860 /* C99 permits duplicate inline. Although of doubtful utility,
7861 it seems simplest to permit it in gnu89 mode as well, as
7862 there is also little utility in maintaining this as a
7863 difference between gnu89 and C99 inline. */
7865 specs
->inline_p
= true;
7868 dupe
= specs
->thread_p
;
7869 if (specs
->storage_class
== csc_auto
)
7870 error ("%<__thread%> used with %<auto%>");
7871 else if (specs
->storage_class
== csc_register
)
7872 error ("%<__thread%> used with %<register%>");
7873 else if (specs
->storage_class
== csc_typedef
)
7874 error ("%<__thread%> used with %<typedef%>");
7876 specs
->thread_p
= true;
7883 /* Diagnose "__thread extern". */
7884 if (specs
->thread_p
)
7885 error ("%<__thread%> before %<extern%>");
7892 /* Diagnose "__thread static". */
7893 if (specs
->thread_p
)
7894 error ("%<__thread%> before %<static%>");
7902 if (n
!= csc_none
&& n
== specs
->storage_class
)
7905 error ("duplicate %qE", scspec
);
7908 if (specs
->storage_class
!= csc_none
&& n
!= specs
->storage_class
)
7910 error ("multiple storage classes in declaration specifiers");
7914 specs
->storage_class
= n
;
7915 if (n
!= csc_extern
&& n
!= csc_static
&& specs
->thread_p
)
7917 error ("%<__thread%> used with %qE", scspec
);
7918 specs
->thread_p
= false;
7925 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7928 struct c_declspecs
*
7929 declspecs_add_attrs (struct c_declspecs
*specs
, tree attrs
)
7931 specs
->attrs
= chainon (attrs
, specs
->attrs
);
7932 specs
->declspecs_seen_p
= true;
7936 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7937 specifiers with any other type specifier to determine the resulting
7938 type. This is where ISO C checks on complex types are made, since
7939 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7942 struct c_declspecs
*
7943 finish_declspecs (struct c_declspecs
*specs
)
7945 /* If a type was specified as a whole, we have no modifiers and are
7947 if (specs
->type
!= NULL_TREE
)
7949 gcc_assert (!specs
->long_p
&& !specs
->long_long_p
&& !specs
->short_p
7950 && !specs
->signed_p
&& !specs
->unsigned_p
7951 && !specs
->complex_p
);
7955 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7956 has been specified, treat it as "int" unless "_Complex" is
7957 present and there are no other specifiers. If we just have
7958 "_Complex", it is equivalent to "_Complex double", but e.g.
7959 "_Complex short" is equivalent to "_Complex short int". */
7960 if (specs
->typespec_word
== cts_none
)
7962 if (specs
->saturating_p
)
7964 error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
7965 if (!targetm
.fixed_point_supported_p ())
7966 error ("fixed-point types not supported for this target");
7967 specs
->typespec_word
= cts_fract
;
7969 else if (specs
->long_p
|| specs
->short_p
7970 || specs
->signed_p
|| specs
->unsigned_p
)
7972 specs
->typespec_word
= cts_int
;
7974 else if (specs
->complex_p
)
7976 specs
->typespec_word
= cts_double
;
7977 pedwarn (input_location
, OPT_pedantic
,
7978 "ISO C does not support plain %<complex%> meaning "
7979 "%<double complex%>");
7983 specs
->typespec_word
= cts_int
;
7984 specs
->default_int_p
= true;
7985 /* We don't diagnose this here because grokdeclarator will
7986 give more specific diagnostics according to whether it is
7987 a function definition. */
7991 /* If "signed" was specified, record this to distinguish "int" and
7992 "signed int" in the case of a bit-field with
7993 -funsigned-bitfields. */
7994 specs
->explicit_signed_p
= specs
->signed_p
;
7996 /* Now compute the actual type. */
7997 switch (specs
->typespec_word
)
8000 gcc_assert (!specs
->long_p
&& !specs
->short_p
8001 && !specs
->signed_p
&& !specs
->unsigned_p
8002 && !specs
->complex_p
);
8003 specs
->type
= void_type_node
;
8006 gcc_assert (!specs
->long_p
&& !specs
->short_p
8007 && !specs
->signed_p
&& !specs
->unsigned_p
8008 && !specs
->complex_p
);
8009 specs
->type
= boolean_type_node
;
8012 gcc_assert (!specs
->long_p
&& !specs
->short_p
);
8013 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
8014 if (specs
->signed_p
)
8015 specs
->type
= signed_char_type_node
;
8016 else if (specs
->unsigned_p
)
8017 specs
->type
= unsigned_char_type_node
;
8019 specs
->type
= char_type_node
;
8020 if (specs
->complex_p
)
8022 pedwarn (input_location
, OPT_pedantic
,
8023 "ISO C does not support complex integer types");
8024 specs
->type
= build_complex_type (specs
->type
);
8028 gcc_assert (!(specs
->long_p
&& specs
->short_p
));
8029 gcc_assert (!(specs
->signed_p
&& specs
->unsigned_p
));
8030 if (specs
->long_long_p
)
8031 specs
->type
= (specs
->unsigned_p
8032 ? long_long_unsigned_type_node
8033 : long_long_integer_type_node
);
8034 else if (specs
->long_p
)
8035 specs
->type
= (specs
->unsigned_p
8036 ? long_unsigned_type_node
8037 : long_integer_type_node
);
8038 else if (specs
->short_p
)
8039 specs
->type
= (specs
->unsigned_p
8040 ? short_unsigned_type_node
8041 : short_integer_type_node
);
8043 specs
->type
= (specs
->unsigned_p
8044 ? unsigned_type_node
8045 : integer_type_node
);
8046 if (specs
->complex_p
)
8048 pedwarn (input_location
, OPT_pedantic
,
8049 "ISO C does not support complex integer types");
8050 specs
->type
= build_complex_type (specs
->type
);
8054 gcc_assert (!specs
->long_p
&& !specs
->short_p
8055 && !specs
->signed_p
&& !specs
->unsigned_p
);
8056 specs
->type
= (specs
->complex_p
8057 ? complex_float_type_node
8061 gcc_assert (!specs
->long_long_p
&& !specs
->short_p
8062 && !specs
->signed_p
&& !specs
->unsigned_p
);
8065 specs
->type
= (specs
->complex_p
8066 ? complex_long_double_type_node
8067 : long_double_type_node
);
8071 specs
->type
= (specs
->complex_p
8072 ? complex_double_type_node
8073 : double_type_node
);
8079 gcc_assert (!specs
->long_p
&& !specs
->long_long_p
&& !specs
->short_p
8080 && !specs
->signed_p
&& !specs
->unsigned_p
&& !specs
->complex_p
);
8081 if (specs
->typespec_word
== cts_dfloat32
)
8082 specs
->type
= dfloat32_type_node
;
8083 else if (specs
->typespec_word
== cts_dfloat64
)
8084 specs
->type
= dfloat64_type_node
;
8086 specs
->type
= dfloat128_type_node
;
8089 gcc_assert (!specs
->complex_p
);
8090 if (!targetm
.fixed_point_supported_p ())
8091 specs
->type
= integer_type_node
;
8092 else if (specs
->saturating_p
)
8094 if (specs
->long_long_p
)
8095 specs
->type
= specs
->unsigned_p
8096 ? sat_unsigned_long_long_fract_type_node
8097 : sat_long_long_fract_type_node
;
8098 else if (specs
->long_p
)
8099 specs
->type
= specs
->unsigned_p
8100 ? sat_unsigned_long_fract_type_node
8101 : sat_long_fract_type_node
;
8102 else if (specs
->short_p
)
8103 specs
->type
= specs
->unsigned_p
8104 ? sat_unsigned_short_fract_type_node
8105 : sat_short_fract_type_node
;
8107 specs
->type
= specs
->unsigned_p
8108 ? sat_unsigned_fract_type_node
8109 : sat_fract_type_node
;
8113 if (specs
->long_long_p
)
8114 specs
->type
= specs
->unsigned_p
8115 ? unsigned_long_long_fract_type_node
8116 : long_long_fract_type_node
;
8117 else if (specs
->long_p
)
8118 specs
->type
= specs
->unsigned_p
8119 ? unsigned_long_fract_type_node
8120 : long_fract_type_node
;
8121 else if (specs
->short_p
)
8122 specs
->type
= specs
->unsigned_p
8123 ? unsigned_short_fract_type_node
8124 : short_fract_type_node
;
8126 specs
->type
= specs
->unsigned_p
8127 ? unsigned_fract_type_node
8132 gcc_assert (!specs
->complex_p
);
8133 if (!targetm
.fixed_point_supported_p ())
8134 specs
->type
= integer_type_node
;
8135 else if (specs
->saturating_p
)
8137 if (specs
->long_long_p
)
8138 specs
->type
= specs
->unsigned_p
8139 ? sat_unsigned_long_long_accum_type_node
8140 : sat_long_long_accum_type_node
;
8141 else if (specs
->long_p
)
8142 specs
->type
= specs
->unsigned_p
8143 ? sat_unsigned_long_accum_type_node
8144 : sat_long_accum_type_node
;
8145 else if (specs
->short_p
)
8146 specs
->type
= specs
->unsigned_p
8147 ? sat_unsigned_short_accum_type_node
8148 : sat_short_accum_type_node
;
8150 specs
->type
= specs
->unsigned_p
8151 ? sat_unsigned_accum_type_node
8152 : sat_accum_type_node
;
8156 if (specs
->long_long_p
)
8157 specs
->type
= specs
->unsigned_p
8158 ? unsigned_long_long_accum_type_node
8159 : long_long_accum_type_node
;
8160 else if (specs
->long_p
)
8161 specs
->type
= specs
->unsigned_p
8162 ? unsigned_long_accum_type_node
8163 : long_accum_type_node
;
8164 else if (specs
->short_p
)
8165 specs
->type
= specs
->unsigned_p
8166 ? unsigned_short_accum_type_node
8167 : short_accum_type_node
;
8169 specs
->type
= specs
->unsigned_p
8170 ? unsigned_accum_type_node
8181 /* A subroutine of c_write_global_declarations. Perform final processing
8182 on one file scope's declarations (or the external scope's declarations),
8186 c_write_global_declarations_1 (tree globals
)
8191 /* Process the decls in the order they were written. */
8192 for (decl
= globals
; decl
; decl
= TREE_CHAIN (decl
))
8194 /* Check for used but undefined static functions using the C
8195 standard's definition of "used", and set TREE_NO_WARNING so
8196 that check_global_declarations doesn't repeat the check. */
8197 if (TREE_CODE (decl
) == FUNCTION_DECL
8198 && DECL_INITIAL (decl
) == 0
8199 && DECL_EXTERNAL (decl
)
8200 && !TREE_PUBLIC (decl
)
8201 && C_DECL_USED (decl
))
8203 pedwarn (input_location
, 0, "%q+F used but never defined", decl
);
8204 TREE_NO_WARNING (decl
) = 1;
8207 wrapup_global_declaration_1 (decl
);
8213 for (decl
= globals
; decl
; decl
= TREE_CHAIN (decl
))
8214 reconsider
|= wrapup_global_declaration_2 (decl
);
8218 for (decl
= globals
; decl
; decl
= TREE_CHAIN (decl
))
8219 check_global_declaration_1 (decl
);
8222 /* A subroutine of c_write_global_declarations Emit debug information for each
8223 of the declarations in GLOBALS. */
8226 c_write_global_declarations_2 (tree globals
)
8230 for (decl
= globals
; decl
; decl
= TREE_CHAIN (decl
))
8231 debug_hooks
->global_decl (decl
);
8234 /* Preserve the external declarations scope across a garbage collect. */
8235 static GTY(()) tree ext_block
;
8238 c_write_global_declarations (void)
8242 /* We don't want to do this if generating a PCH. */
8246 /* Don't waste time on further processing if -fsyntax-only or we've
8247 encountered errors. */
8248 if (flag_syntax_only
|| errorcount
|| sorrycount
)
8251 /* Close the external scope. */
8252 ext_block
= pop_scope ();
8254 gcc_assert (!current_scope
);
8258 tree tmp
= BLOCK_VARS (ext_block
);
8260 FILE * stream
= dump_begin (TDI_tu
, &flags
);
8263 dump_node (tmp
, flags
& ~TDF_SLIM
, stream
);
8264 dump_end (TDI_tu
, stream
);
8268 /* Process all file scopes in this compilation, and the external_scope,
8269 through wrapup_global_declarations and check_global_declarations. */
8270 for (t
= all_translation_units
; t
; t
= TREE_CHAIN (t
))
8271 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t
)));
8272 c_write_global_declarations_1 (BLOCK_VARS (ext_block
));
8274 /* We're done parsing; proceed to optimize and emit assembly.
8275 FIXME: shouldn't be the front end's responsibility to call this. */
8278 /* After cgraph has had a chance to emit everything that's going to
8279 be emitted, output debug information for globals. */
8280 if (errorcount
== 0 && sorrycount
== 0)
8282 timevar_push (TV_SYMOUT
);
8283 for (t
= all_translation_units
; t
; t
= TREE_CHAIN (t
))
8284 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t
)));
8285 c_write_global_declarations_2 (BLOCK_VARS (ext_block
));
8286 timevar_pop (TV_SYMOUT
);
8292 #include "gt-c-decl.h"