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"
67 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
69 { NORMAL, /* Ordinary declaration */
70 FUNCDEF, /* Function definition */
71 PARM, /* Declaration of parm before function body */
72 FIELD, /* Declaration inside struct or union */
73 TYPENAME}; /* Typename (inside cast or sizeof) */
75 /* States indicating how grokdeclarator() should handle declspecs marked
76 with __attribute__((deprecated)). An object declared as
77 __attribute__((deprecated)) suppresses warnings of uses of other
80 enum deprecated_states {
86 /* Nonzero if we have seen an invalid cross reference
87 to a struct, union, or enum, but not yet printed the message. */
88 tree pending_invalid_xref;
90 /* File and line to appear in the eventual error message. */
91 location_t pending_invalid_xref_location;
93 /* True means we've initialized exception handling. */
94 bool c_eh_initialized_p;
96 /* The file and line that the prototype came from if this is an
97 old-style definition; used for diagnostics in
98 store_parm_decls_oldstyle. */
100 static location_t current_function_prototype_locus;
102 /* Whether this prototype was built-in. */
104 static bool current_function_prototype_built_in;
106 /* The argument type information of this prototype. */
108 static tree current_function_prototype_arg_types;
110 /* The argument information structure for the function currently being
113 static struct c_arg_info *current_function_arg_info;
115 /* The obstack on which parser and related data structures, which are
116 not live beyond their top-level declaration or definition, are
118 struct obstack parser_obstack;
120 /* The current statement tree. */
122 static GTY(()) struct stmt_tree_s c_stmt_tree;
124 /* State saving variables. */
128 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
129 included in this invocation. Note that the current translation
130 unit is not included in this list. */
132 static GTY(()) tree all_translation_units;
134 /* A list of decls to be made automatically visible in each file scope. */
135 static GTY(()) tree visible_builtins;
137 /* Set to 0 at beginning of a function definition, set to 1 if
138 a return statement that specifies a return value is seen. */
140 int current_function_returns_value;
142 /* Set to 0 at beginning of a function definition, set to 1 if
143 a return statement with no argument is seen. */
145 int current_function_returns_null;
147 /* Set to 0 at beginning of a function definition, set to 1 if
148 a call to a noreturn function is seen. */
150 int current_function_returns_abnormally;
152 /* Set to nonzero by `grokdeclarator' for a function
153 whose return type is defaulted, if warnings for this are desired. */
155 static int warn_about_return_type;
157 /* Nonzero when the current toplevel function contains a declaration
158 of a nested function which is never defined. */
160 static bool undef_nested_function;
162 /* True means global_bindings_p should return false even if the scope stack
163 says we are in file scope. */
164 bool c_override_global_bindings_to_false;
167 /* Each c_binding structure describes one binding of an identifier to
168 a decl. All the decls in a scope - irrespective of namespace - are
169 chained together by the ->prev field, which (as the name implies)
170 runs in reverse order. All the decls in a given namespace bound to
171 a given identifier are chained by the ->shadowed field, which runs
172 from inner to outer scopes.
174 The ->decl field usually points to a DECL node, but there are two
175 exceptions. In the namespace of type tags, the bound entity is a
176 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
177 identifier is encountered, it is bound to error_mark_node to
178 suppress further errors about that identifier in the current
181 The ->type field stores the type of the declaration in this scope;
182 if NULL, the type is the type of the ->decl field. This is only of
183 relevance for objects with external or internal linkage which may
184 be redeclared in inner scopes, forming composite types that only
185 persist for the duration of those scopes. In the external scope,
186 this stores the composite of all the types declared for this
187 object, visible or not. The ->inner_comp field (used only at file
188 scope) stores whether an incomplete array type at file scope was
189 completed at an inner scope to an array size other than 1.
191 The depth field is copied from the scope structure that holds this
192 decl. It is used to preserve the proper ordering of the ->shadowed
193 field (see bind()) and also for a handful of special-case checks.
194 Finally, the invisible bit is true for a decl which should be
195 ignored for purposes of normal name lookup, and the nested bit is
196 true for a decl that's been bound a second time in an inner scope;
197 in all such cases, the binding in the outer scope will have its
198 invisible bit true. */
200 struct c_binding GTY((chain_next ("%h.prev")))
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 lang_identifier GTY(())
240 struct c_common_identifier common_id;
241 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
242 struct c_binding *tag_binding; /* struct/union/enum tags */
243 struct c_binding *label_binding; /* labels */
246 /* Validate c-lang.c's assumptions. */
247 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
248 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
250 /* The resulting tree type. */
253 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
254 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))")))
256 union tree_node GTY ((tag ("0"),
257 desc ("tree_node_structure (&%h)")))
259 struct lang_identifier GTY ((tag ("1"))) identifier;
262 /* Each c_scope structure describes the complete contents of one
263 scope. Four scopes are distinguished specially: the innermost or
264 current scope, the innermost function scope, the file scope (always
265 the second to outermost) and the outermost or external scope.
267 Most declarations are recorded in the current scope.
269 All normal label declarations are recorded in the innermost
270 function scope, as are bindings of undeclared identifiers to
271 error_mark_node. (GCC permits nested functions as an extension,
272 hence the 'innermost' qualifier.) Explicitly declared labels
273 (using the __label__ extension) appear in the current scope.
275 Being in the file scope (current_scope == file_scope) causes
276 special behavior in several places below. Also, under some
277 conditions the Objective-C front end records declarations in the
278 file scope even though that isn't the current scope.
280 All declarations with external linkage are recorded in the external
281 scope, even if they aren't visible there; this models the fact that
282 such declarations are visible to the entire program, and (with a
283 bit of cleverness, see pushdecl) allows diagnosis of some violations
284 of C99 6.2.2p7 and 6.2.7p2:
286 If, within the same translation unit, the same identifier appears
287 with both internal and external linkage, the behavior is
290 All declarations that refer to the same object or function shall
291 have compatible type; otherwise, the behavior is undefined.
293 Initially only the built-in declarations, which describe compiler
294 intrinsic functions plus a subset of the standard library, are in
297 The order of the blocks list matters, and it is frequently appended
298 to. To avoid having to walk all the way to the end of the list on
299 each insertion, or reverse the list later, we maintain a pointer to
300 the last list entry. (FIXME: It should be feasible to use a reversed
303 The bindings list is strictly in reverse order of declarations;
304 pop_scope relies on this. */
307 struct c_scope GTY((chain_next ("%h.outer")))
309 /* The scope containing this one. */
310 struct c_scope *outer;
312 /* The next outermost function scope. */
313 struct c_scope *outer_function;
315 /* All bindings in this scope. */
316 struct c_binding *bindings;
318 /* For each scope (except the global one), a chain of BLOCK nodes
319 for all the scopes that were entered and exited one level down. */
323 /* The depth of this scope. Used to keep the ->shadowed chain of
324 bindings sorted innermost to outermost. */
325 unsigned int depth : 28;
327 /* True if we are currently filling this scope with parameter
329 BOOL_BITFIELD parm_flag : 1;
331 /* True if we saw [*] in this scope. Used to give an error messages
332 if these appears in a function definition. */
333 BOOL_BITFIELD had_vla_unspec : 1;
335 /* True if we already complained about forward parameter decls
336 in this scope. This prevents double warnings on
337 foo (int a; int b; ...) */
338 BOOL_BITFIELD warned_forward_parm_decls : 1;
340 /* True if this is the outermost block scope of a function body.
341 This scope contains the parameters, the local variables declared
342 in the outermost block, and all the labels (except those in
343 nested functions, or declared at block scope with __label__). */
344 BOOL_BITFIELD function_body : 1;
346 /* True means make a BLOCK for this scope no matter what. */
347 BOOL_BITFIELD keep : 1;
350 /* The scope currently in effect. */
352 static GTY(()) struct c_scope *current_scope;
354 /* The innermost function scope. Ordinary (not explicitly declared)
355 labels, bindings to error_mark_node, and the lazily-created
356 bindings of __func__ and its friends get this scope. */
358 static GTY(()) struct c_scope *current_function_scope;
360 /* The C file scope. This is reset for each input translation unit. */
362 static GTY(()) struct c_scope *file_scope;
364 /* The outermost scope. This is used for all declarations with
365 external linkage, and only these, hence the name. */
367 static GTY(()) struct c_scope *external_scope;
369 /* A chain of c_scope structures awaiting reuse. */
371 static GTY((deletable)) struct c_scope *scope_freelist;
373 /* A chain of c_binding structures awaiting reuse. */
375 static GTY((deletable)) struct c_binding *binding_freelist;
377 /* Append VAR to LIST in scope SCOPE. */
378 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
379 struct c_scope *s_ = (scope); \
381 if (s_->list##_last) \
382 BLOCK_CHAIN (s_->list##_last) = d_; \
385 s_->list##_last = d_; \
388 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
389 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
390 struct c_scope *t_ = (tscope); \
391 struct c_scope *f_ = (fscope); \
393 BLOCK_CHAIN (t_->to##_last) = f_->from; \
396 t_->to##_last = f_->from##_last; \
399 /* True means unconditionally make a BLOCK for the next scope pushed. */
401 static bool keep_next_level_flag;
403 /* True means the next call to push_scope will be the outermost scope
404 of a function body, so do not push a new scope, merely cease
405 expecting parameter decls. */
407 static bool next_is_function_body;
409 /* Forward declarations. */
410 static tree lookup_name_in_scope (tree, struct c_scope *);
411 static tree c_make_fname_decl (tree, int);
412 static tree grokdeclarator (const struct c_declarator *,
413 struct c_declspecs *,
414 enum decl_context, bool, tree *, tree *, tree *,
415 bool *, enum deprecated_states);
416 static tree grokparms (struct c_arg_info *, bool);
417 static void layout_array_type (tree);
419 /* T is a statement. Add it to the statement-tree. This is the
420 C/ObjC version--C++ has a slightly different version of this
426 enum tree_code code = TREE_CODE (t);
428 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
430 if (!EXPR_HAS_LOCATION (t))
431 SET_EXPR_LOCATION (t, input_location);
434 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
435 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
437 /* Add T to the statement-tree. Non-side-effect statements need to be
438 recorded during statement expressions. */
439 append_to_statement_list_force (t, &cur_stmt_list);
446 c_print_identifier (FILE *file, tree node, int indent)
448 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
449 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
450 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
451 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
453 tree rid = ridpointers[C_RID_CODE (node)];
454 indent_to (file, indent + 4);
455 fprintf (file, "rid %p \"%s\"",
456 (void *) rid, IDENTIFIER_POINTER (rid));
460 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
461 which may be any of several kinds of DECL or TYPE or error_mark_node,
462 in the scope SCOPE. */
464 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
465 bool nested, location_t locus)
467 struct c_binding *b, **here;
469 if (binding_freelist)
471 b = binding_freelist;
472 binding_freelist = b->prev;
475 b = GGC_NEW (struct c_binding);
480 b->depth = scope->depth;
481 b->invisible = invisible;
488 b->prev = scope->bindings;
494 switch (TREE_CODE (decl))
496 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
499 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
505 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
511 /* Locate the appropriate place in the chain of shadowed decls
512 to insert this binding. Normally, scope == current_scope and
513 this does nothing. */
514 while (*here && (*here)->depth > scope->depth)
515 here = &(*here)->shadowed;
521 /* Clear the binding structure B, stick it on the binding_freelist,
522 and return the former value of b->prev. This is used by pop_scope
523 and get_parm_info to iterate destructively over all the bindings
524 from a given scope. */
525 static struct c_binding *
526 free_binding_and_advance (struct c_binding *b)
528 struct c_binding *prev = b->prev;
530 memset (b, 0, sizeof (struct c_binding));
531 b->prev = binding_freelist;
532 binding_freelist = b;
538 /* Hook called at end of compilation to assume 1 elt
539 for a file-scope tentative array defn that wasn't complete before. */
542 c_finish_incomplete_decl (tree decl)
544 if (TREE_CODE (decl) == VAR_DECL)
546 tree type = TREE_TYPE (decl);
547 if (type != error_mark_node
548 && TREE_CODE (type) == ARRAY_TYPE
549 && !DECL_EXTERNAL (decl)
550 && TYPE_DOMAIN (type) == 0)
552 warning (0, "array %q+D assumed to have one element", decl);
554 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
556 layout_decl (decl, 0);
561 /* The Objective-C front-end often needs to determine the current scope. */
564 objc_get_current_scope (void)
566 return current_scope;
569 /* The following function is used only by Objective-C. It needs to live here
570 because it accesses the innards of c_scope. */
573 objc_mark_locals_volatile (void *enclosing_blk)
575 struct c_scope *scope;
578 for (scope = current_scope;
579 scope && scope != enclosing_blk;
580 scope = scope->outer)
582 for (b = scope->bindings; b; b = b->prev)
583 objc_volatilize_decl (b->decl);
585 /* Do not climb up past the current function. */
586 if (scope->function_body)
591 /* Nonzero if we are currently in file scope. */
594 global_bindings_p (void)
596 return current_scope == file_scope && !c_override_global_bindings_to_false;
600 keep_next_level (void)
602 keep_next_level_flag = true;
605 /* Identify this scope as currently being filled with parameters. */
608 declare_parm_level (void)
610 current_scope->parm_flag = true;
616 if (next_is_function_body)
618 /* This is the transition from the parameters to the top level
619 of the function body. These are the same scope
620 (C99 6.2.1p4,6) so we do not push another scope structure.
621 next_is_function_body is set only by store_parm_decls, which
622 in turn is called when and only when we are about to
623 encounter the opening curly brace for the function body.
625 The outermost block of a function always gets a BLOCK node,
626 because the debugging output routines expect that each
627 function has at least one BLOCK. */
628 current_scope->parm_flag = false;
629 current_scope->function_body = true;
630 current_scope->keep = true;
631 current_scope->outer_function = current_function_scope;
632 current_function_scope = current_scope;
634 keep_next_level_flag = false;
635 next_is_function_body = false;
639 struct c_scope *scope;
642 scope = scope_freelist;
643 scope_freelist = scope->outer;
646 scope = GGC_CNEW (struct c_scope);
648 scope->keep = keep_next_level_flag;
649 scope->outer = current_scope;
650 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
652 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
654 if (current_scope && scope->depth == 0)
657 sorry ("GCC supports only %u nested scopes", scope->depth);
660 current_scope = scope;
661 keep_next_level_flag = false;
665 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
668 set_type_context (tree type, tree context)
670 for (type = TYPE_MAIN_VARIANT (type); type;
671 type = TYPE_NEXT_VARIANT (type))
672 TYPE_CONTEXT (type) = context;
675 /* Exit a scope. Restore the state of the identifier-decl mappings
676 that were in effect when this scope was entered. Return a BLOCK
677 node containing all the DECLs in this scope that are of interest
678 to debug info generation. */
683 struct c_scope *scope = current_scope;
684 tree block, context, p;
687 bool functionbody = scope->function_body;
688 bool keep = functionbody || scope->keep || scope->bindings;
690 c_end_vm_scope (scope->depth);
692 /* If appropriate, create a BLOCK to record the decls for the life
697 block = make_node (BLOCK);
698 BLOCK_SUBBLOCKS (block) = scope->blocks;
699 TREE_USED (block) = 1;
701 /* In each subblock, record that this is its superior. */
702 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
703 BLOCK_SUPERCONTEXT (p) = block;
705 BLOCK_VARS (block) = 0;
708 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
709 scope must be set so that they point to the appropriate
710 construct, i.e. either to the current FUNCTION_DECL node, or
711 else to the BLOCK node we just constructed.
713 Note that for tagged types whose scope is just the formal
714 parameter list for some function type specification, we can't
715 properly set their TYPE_CONTEXTs here, because we don't have a
716 pointer to the appropriate FUNCTION_TYPE node readily available
717 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
718 type nodes get set in `grokdeclarator' as soon as we have created
719 the FUNCTION_TYPE node which will represent the "scope" for these
720 "parameter list local" tagged types. */
721 if (scope->function_body)
722 context = current_function_decl;
723 else if (scope == file_scope)
725 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
726 TREE_CHAIN (file_decl) = all_translation_units;
727 all_translation_units = file_decl;
733 /* Clear all bindings in this scope. */
734 for (b = scope->bindings; b; b = free_binding_and_advance (b))
737 switch (TREE_CODE (p))
740 /* Warnings for unused labels, errors for undefined labels. */
741 if (TREE_USED (p) && !DECL_INITIAL (p))
743 error ("label %q+D used but not defined", p);
744 DECL_INITIAL (p) = error_mark_node;
747 warn_for_unused_label (p);
749 /* Labels go in BLOCK_VARS. */
750 TREE_CHAIN (p) = BLOCK_VARS (block);
751 BLOCK_VARS (block) = p;
752 gcc_assert (I_LABEL_BINDING (b->id) == b);
753 I_LABEL_BINDING (b->id) = b->shadowed;
759 set_type_context (p, context);
761 /* Types may not have tag-names, in which case the type
762 appears in the bindings list with b->id NULL. */
765 gcc_assert (I_TAG_BINDING (b->id) == b);
766 I_TAG_BINDING (b->id) = b->shadowed;
771 /* Propagate TREE_ADDRESSABLE from nested functions to their
772 containing functions. */
773 if (!TREE_ASM_WRITTEN (p)
774 && DECL_INITIAL (p) != 0
775 && TREE_ADDRESSABLE (p)
776 && DECL_ABSTRACT_ORIGIN (p) != 0
777 && DECL_ABSTRACT_ORIGIN (p) != p)
778 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
779 if (!DECL_EXTERNAL (p)
781 && scope != file_scope
782 && scope != external_scope)
784 error ("nested function %q+D declared but never defined", p);
785 undef_nested_function = true;
787 else if (DECL_DECLARED_INLINE_P (p)
789 && !DECL_INITIAL (p))
791 /* C99 6.7.4p6: "a function with external linkage... declared
792 with an inline function specifier ... shall also be defined
793 in the same translation unit." */
794 if (!flag_gnu89_inline)
795 pedwarn (input_location, 0,
796 "inline function %q+D declared but never defined", p);
797 DECL_EXTERNAL (p) = 1;
803 /* Warnings for unused variables. */
805 && !TREE_NO_WARNING (p)
806 && !DECL_IN_SYSTEM_HEADER (p)
808 && !DECL_ARTIFICIAL (p)
809 && scope != file_scope
810 && scope != external_scope)
811 warning (OPT_Wunused_variable, "unused variable %q+D", p);
815 error ("type of array %q+D completed incompatibly with"
816 " implicit initialization", p);
823 /* All of these go in BLOCK_VARS, but only if this is the
824 binding in the home scope. */
827 TREE_CHAIN (p) = BLOCK_VARS (block);
828 BLOCK_VARS (block) = p;
830 else if (VAR_OR_FUNCTION_DECL_P (p))
832 /* For block local externs add a special
833 DECL_EXTERNAL decl for debug info generation. */
834 tree extp = copy_node (p);
836 DECL_EXTERNAL (extp) = 1;
837 TREE_STATIC (extp) = 0;
838 TREE_PUBLIC (extp) = 1;
839 DECL_INITIAL (extp) = NULL_TREE;
840 DECL_LANG_SPECIFIC (extp) = NULL;
841 DECL_CONTEXT (extp) = current_function_decl;
842 if (TREE_CODE (p) == FUNCTION_DECL)
844 DECL_RESULT (extp) = NULL_TREE;
845 DECL_SAVED_TREE (extp) = NULL_TREE;
846 DECL_STRUCT_FUNCTION (extp) = NULL;
848 if (b->locus != UNKNOWN_LOCATION)
849 DECL_SOURCE_LOCATION (extp) = b->locus;
850 TREE_CHAIN (extp) = BLOCK_VARS (block);
851 BLOCK_VARS (block) = extp;
853 /* If this is the file scope, and we are processing more
854 than one translation unit in this compilation, set
855 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
856 This makes same_translation_unit_p work, and causes
857 static declarations to be given disambiguating suffixes. */
858 if (scope == file_scope && num_in_fnames > 1)
860 DECL_CONTEXT (p) = context;
861 if (TREE_CODE (p) == TYPE_DECL)
862 set_type_context (TREE_TYPE (p), context);
866 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
867 already been put there by store_parm_decls. Unused-
868 parameter warnings are handled by function.c.
869 error_mark_node obviously does not go in BLOCK_VARS and
870 does not get unused-variable warnings. */
873 /* It is possible for a decl not to have a name. We get
874 here with b->id NULL in this case. */
877 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
878 I_SYMBOL_BINDING (b->id) = b->shadowed;
879 if (b->shadowed && b->shadowed->type)
880 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
890 /* Dispose of the block that we just made inside some higher level. */
891 if ((scope->function_body || scope == file_scope) && context)
893 DECL_INITIAL (context) = block;
894 BLOCK_SUPERCONTEXT (block) = context;
896 else if (scope->outer)
899 SCOPE_LIST_APPEND (scope->outer, blocks, block);
900 /* If we did not make a block for the scope just exited, any
901 blocks made for inner scopes must be carried forward so they
902 will later become subblocks of something else. */
903 else if (scope->blocks)
904 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
907 /* Pop the current scope, and free the structure for reuse. */
908 current_scope = scope->outer;
909 if (scope->function_body)
910 current_function_scope = scope->outer_function;
912 memset (scope, 0, sizeof (struct c_scope));
913 scope->outer = scope_freelist;
914 scope_freelist = scope;
920 push_file_scope (void)
928 file_scope = current_scope;
930 start_fname_decls ();
932 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
933 bind (DECL_NAME (decl), decl, file_scope,
934 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
938 pop_file_scope (void)
940 /* In case there were missing closebraces, get us back to the global
942 while (current_scope != file_scope)
945 /* __FUNCTION__ is defined at file scope (""). This
946 call may not be necessary as my tests indicate it
947 still works without it. */
948 finish_fname_decls ();
950 /* This is the point to write out a PCH if we're doing that.
951 In that case we do not want to do anything else. */
954 c_common_write_pch ();
958 /* Pop off the file scope and close this translation unit. */
962 maybe_apply_pending_pragma_weaks ();
963 cgraph_finalize_compilation_unit ();
967 /* Push a definition or a declaration of struct, union or enum tag "name".
968 "type" should be the type node.
969 We assume that the tag "name" is not already defined.
971 Note that the definition may really be just a forward reference.
972 In that case, the TYPE_SIZE will be zero. */
975 pushtag (tree name, tree type)
977 /* Record the identifier as the type's name if it has none. */
978 if (name && !TYPE_NAME (type))
979 TYPE_NAME (type) = name;
980 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false,
983 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
984 tagged type we just added to the current scope. This fake
985 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
986 to output a representation of a tagged type, and it also gives
987 us a convenient place to record the "scope start" address for the
990 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
992 /* An approximation for now, so we can tell this is a function-scope tag.
993 This will be updated in pop_scope. */
994 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
997 /* Subroutine of compare_decls. Allow harmless mismatches in return
998 and argument types provided that the type modes match. This function
999 return a unified type given a suitable match, and 0 otherwise. */
1002 match_builtin_function_types (tree newtype, tree oldtype)
1004 tree newrettype, oldrettype;
1005 tree newargs, oldargs;
1006 tree trytype, tryargs;
1008 /* Accept the return type of the new declaration if same modes. */
1009 oldrettype = TREE_TYPE (oldtype);
1010 newrettype = TREE_TYPE (newtype);
1012 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1015 oldargs = TYPE_ARG_TYPES (oldtype);
1016 newargs = TYPE_ARG_TYPES (newtype);
1019 while (oldargs || newargs)
1023 || !TREE_VALUE (oldargs)
1024 || !TREE_VALUE (newargs)
1025 || TYPE_MODE (TREE_VALUE (oldargs))
1026 != TYPE_MODE (TREE_VALUE (newargs)))
1029 oldargs = TREE_CHAIN (oldargs);
1030 newargs = TREE_CHAIN (newargs);
1033 trytype = build_function_type (newrettype, tryargs);
1034 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1037 /* Subroutine of diagnose_mismatched_decls. Check for function type
1038 mismatch involving an empty arglist vs a nonempty one and give clearer
1041 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1042 tree newtype, tree oldtype)
1046 if (TREE_CODE (olddecl) != FUNCTION_DECL
1047 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1048 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1050 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1053 t = TYPE_ARG_TYPES (oldtype);
1055 t = TYPE_ARG_TYPES (newtype);
1056 for (; t; t = TREE_CHAIN (t))
1058 tree type = TREE_VALUE (t);
1060 if (TREE_CHAIN (t) == 0
1061 && TYPE_MAIN_VARIANT (type) != void_type_node)
1063 inform (input_location, "a parameter list with an ellipsis can%'t match "
1064 "an empty parameter name list declaration");
1068 if (c_type_promotes_to (type) != type)
1070 inform (input_location, "an argument type that has a default promotion can%'t match "
1071 "an empty parameter name list declaration");
1077 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1078 old-style function definition, NEWDECL is a prototype declaration.
1079 Diagnose inconsistencies in the argument list. Returns TRUE if
1080 the prototype is compatible, FALSE if not. */
1082 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1084 tree newargs, oldargs;
1087 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1089 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1090 newargs = TYPE_ARG_TYPES (newtype);
1095 tree oldargtype = TREE_VALUE (oldargs);
1096 tree newargtype = TREE_VALUE (newargs);
1098 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1101 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1102 newargtype = TYPE_MAIN_VARIANT (newargtype);
1104 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1107 /* Reaching the end of just one list means the two decls don't
1108 agree on the number of arguments. */
1109 if (END_OF_ARGLIST (oldargtype))
1111 error ("prototype for %q+D declares more arguments "
1112 "than previous old-style definition", newdecl);
1115 else if (END_OF_ARGLIST (newargtype))
1117 error ("prototype for %q+D declares fewer arguments "
1118 "than previous old-style definition", newdecl);
1122 /* Type for passing arg must be consistent with that declared
1124 else if (!comptypes (oldargtype, newargtype))
1126 error ("prototype for %q+D declares argument %d"
1127 " with incompatible type",
1132 oldargs = TREE_CHAIN (oldargs);
1133 newargs = TREE_CHAIN (newargs);
1137 /* If we get here, no errors were found, but do issue a warning
1138 for this poor-style construct. */
1139 warning (0, "prototype for %q+D follows non-prototype definition",
1142 #undef END_OF_ARGLIST
1145 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1146 first in a pair of mismatched declarations, using the diagnostic
1149 locate_old_decl (tree decl)
1151 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1153 else if (DECL_INITIAL (decl))
1154 inform (input_location, "previous definition of %q+D was here", decl);
1155 else if (C_DECL_IMPLICIT (decl))
1156 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1158 inform (input_location, "previous declaration of %q+D was here", decl);
1161 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1162 Returns true if the caller should proceed to merge the two, false
1163 if OLDDECL should simply be discarded. As a side effect, issues
1164 all necessary diagnostics for invalid or poor-style combinations.
1165 If it returns true, writes the types of NEWDECL and OLDDECL to
1166 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1167 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1170 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1171 tree *newtypep, tree *oldtypep)
1173 tree newtype, oldtype;
1174 bool pedwarned = false;
1175 bool warned = false;
1178 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1179 && DECL_EXTERNAL (DECL))
1181 /* If we have error_mark_node for either decl or type, just discard
1182 the previous decl - we're in an error cascade already. */
1183 if (olddecl == error_mark_node || newdecl == error_mark_node)
1185 *oldtypep = oldtype = TREE_TYPE (olddecl);
1186 *newtypep = newtype = TREE_TYPE (newdecl);
1187 if (oldtype == error_mark_node || newtype == error_mark_node)
1190 /* Two different categories of symbol altogether. This is an error
1191 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1192 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1194 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1195 && DECL_BUILT_IN (olddecl)
1196 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1198 error ("%q+D redeclared as different kind of symbol", newdecl);
1199 locate_old_decl (olddecl);
1201 else if (TREE_PUBLIC (newdecl))
1202 warning (0, "built-in function %q+D declared as non-function",
1205 warning (OPT_Wshadow, "declaration of %q+D shadows "
1206 "a built-in function", newdecl);
1210 /* Enumerators have no linkage, so may only be declared once in a
1212 if (TREE_CODE (olddecl) == CONST_DECL)
1214 error ("redeclaration of enumerator %q+D", newdecl);
1215 locate_old_decl (olddecl);
1219 if (!comptypes (oldtype, newtype))
1221 if (TREE_CODE (olddecl) == FUNCTION_DECL
1222 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1224 /* Accept harmless mismatch in function types.
1225 This is for the ffs and fprintf builtins. */
1226 tree trytype = match_builtin_function_types (newtype, oldtype);
1228 if (trytype && comptypes (newtype, trytype))
1229 *oldtypep = oldtype = trytype;
1232 /* If types don't match for a built-in, throw away the
1233 built-in. No point in calling locate_old_decl here, it
1234 won't print anything. */
1235 warning (0, "conflicting types for built-in function %q+D",
1240 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1241 && DECL_IS_BUILTIN (olddecl))
1243 /* A conflicting function declaration for a predeclared
1244 function that isn't actually built in. Objective C uses
1245 these. The new declaration silently overrides everything
1246 but the volatility (i.e. noreturn) indication. See also
1247 below. FIXME: Make Objective C use normal builtins. */
1248 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1251 /* Permit void foo (...) to match int foo (...) if the latter is
1252 the definition and implicit int was used. See
1253 c-torture/compile/920625-2.c. */
1254 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1255 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1256 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1257 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1259 pedwarned = pedwarn (input_location, 0,
1260 "conflicting types for %q+D", newdecl);
1261 /* Make sure we keep void as the return type. */
1262 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1263 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1265 /* Permit void foo (...) to match an earlier call to foo (...) with
1266 no declared type (thus, implicitly int). */
1267 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1268 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1269 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1270 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1272 pedwarned = pedwarn (input_location, 0,
1273 "conflicting types for %q+D", newdecl);
1274 /* Make sure we keep void as the return type. */
1275 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1279 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1280 error ("conflicting type qualifiers for %q+D", newdecl);
1282 error ("conflicting types for %q+D", newdecl);
1283 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1284 locate_old_decl (olddecl);
1289 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1290 but silently ignore the redeclaration if either is in a system
1291 header. (Conflicting redeclarations were handled above.) */
1292 if (TREE_CODE (newdecl) == TYPE_DECL)
1294 if (DECL_IN_SYSTEM_HEADER (newdecl)
1295 || DECL_IN_SYSTEM_HEADER (olddecl)
1296 || TREE_NO_WARNING (newdecl)
1297 || TREE_NO_WARNING (olddecl))
1298 return true; /* Allow OLDDECL to continue in use. */
1300 error ("redefinition of typedef %q+D", newdecl);
1301 locate_old_decl (olddecl);
1305 /* Function declarations can either be 'static' or 'extern' (no
1306 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1307 can never conflict with each other on account of linkage
1308 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1309 gnu89 mode permits two definitions if one is 'extern inline' and
1310 one is not. The non- extern-inline definition supersedes the
1311 extern-inline definition. */
1313 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1315 /* If you declare a built-in function name as static, or
1316 define the built-in with an old-style definition (so we
1317 can't validate the argument list) the built-in definition is
1318 overridden, but optionally warn this was a bad choice of name. */
1319 if (DECL_BUILT_IN (olddecl)
1320 && !C_DECL_DECLARED_BUILTIN (olddecl)
1321 && (!TREE_PUBLIC (newdecl)
1322 || (DECL_INITIAL (newdecl)
1323 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1325 warning (OPT_Wshadow, "declaration of %q+D shadows "
1326 "a built-in function", newdecl);
1327 /* Discard the old built-in function. */
1331 if (DECL_INITIAL (newdecl))
1333 if (DECL_INITIAL (olddecl))
1335 /* If both decls are in the same TU and the new declaration
1336 isn't overriding an extern inline reject the new decl.
1337 In c99, no overriding is allowed in the same translation
1339 if ((!DECL_EXTERN_INLINE (olddecl)
1340 || DECL_EXTERN_INLINE (newdecl)
1341 || (!flag_gnu89_inline
1342 && (!DECL_DECLARED_INLINE_P (olddecl)
1343 || !lookup_attribute ("gnu_inline",
1344 DECL_ATTRIBUTES (olddecl)))
1345 && (!DECL_DECLARED_INLINE_P (newdecl)
1346 || !lookup_attribute ("gnu_inline",
1347 DECL_ATTRIBUTES (newdecl))))
1349 && same_translation_unit_p (newdecl, olddecl))
1351 error ("redefinition of %q+D", newdecl);
1352 locate_old_decl (olddecl);
1357 /* If we have a prototype after an old-style function definition,
1358 the argument types must be checked specially. */
1359 else if (DECL_INITIAL (olddecl)
1360 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1361 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1362 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1364 locate_old_decl (olddecl);
1367 /* A non-static declaration (even an "extern") followed by a
1368 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1369 The same is true for a static forward declaration at block
1370 scope followed by a non-static declaration/definition at file
1371 scope. Static followed by non-static at the same scope is
1372 not undefined behavior, and is the most convenient way to get
1373 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1374 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1375 we do diagnose it if -Wtraditional. */
1376 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1378 /* Two exceptions to the rule. If olddecl is an extern
1379 inline, or a predeclared function that isn't actually
1380 built in, newdecl silently overrides olddecl. The latter
1381 occur only in Objective C; see also above. (FIXME: Make
1382 Objective C use normal builtins.) */
1383 if (!DECL_IS_BUILTIN (olddecl)
1384 && !DECL_EXTERN_INLINE (olddecl))
1386 error ("static declaration of %q+D follows "
1387 "non-static declaration", newdecl);
1388 locate_old_decl (olddecl);
1392 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1394 if (DECL_CONTEXT (olddecl))
1396 error ("non-static declaration of %q+D follows "
1397 "static declaration", newdecl);
1398 locate_old_decl (olddecl);
1401 else if (warn_traditional)
1403 warned |= warning (OPT_Wtraditional,
1404 "non-static declaration of %q+D "
1405 "follows static declaration", newdecl);
1409 /* Make sure gnu_inline attribute is either not present, or
1410 present on all inline decls. */
1411 if (DECL_DECLARED_INLINE_P (olddecl)
1412 && DECL_DECLARED_INLINE_P (newdecl))
1414 bool newa = lookup_attribute ("gnu_inline",
1415 DECL_ATTRIBUTES (newdecl)) != NULL;
1416 bool olda = lookup_attribute ("gnu_inline",
1417 DECL_ATTRIBUTES (olddecl)) != NULL;
1420 error ("%<gnu_inline%> attribute present on %q+D",
1421 newa ? newdecl : olddecl);
1422 error ("%Jbut not here", newa ? olddecl : newdecl);
1426 else if (TREE_CODE (newdecl) == VAR_DECL)
1428 /* Only variables can be thread-local, and all declarations must
1429 agree on this property. */
1430 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1432 /* Nothing to check. Since OLDDECL is marked threadprivate
1433 and NEWDECL does not have a thread-local attribute, we
1434 will merge the threadprivate attribute into NEWDECL. */
1437 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1439 if (DECL_THREAD_LOCAL_P (newdecl))
1440 error ("thread-local declaration of %q+D follows "
1441 "non-thread-local declaration", newdecl);
1443 error ("non-thread-local declaration of %q+D follows "
1444 "thread-local declaration", newdecl);
1446 locate_old_decl (olddecl);
1450 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1451 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1453 error ("redefinition of %q+D", newdecl);
1454 locate_old_decl (olddecl);
1458 /* Objects declared at file scope: if the first declaration had
1459 external linkage (even if it was an external reference) the
1460 second must have external linkage as well, or the behavior is
1461 undefined. If the first declaration had internal linkage, then
1462 the second must too, or else be an external reference (in which
1463 case the composite declaration still has internal linkage).
1464 As for function declarations, we warn about the static-then-
1465 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1466 if (DECL_FILE_SCOPE_P (newdecl)
1467 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1469 if (DECL_EXTERNAL (newdecl))
1471 if (!DECL_FILE_SCOPE_P (olddecl))
1473 error ("extern declaration of %q+D follows "
1474 "declaration with no linkage", newdecl);
1475 locate_old_decl (olddecl);
1478 else if (warn_traditional)
1480 warned |= warning (OPT_Wtraditional,
1481 "non-static declaration of %q+D "
1482 "follows static declaration", newdecl);
1487 if (TREE_PUBLIC (newdecl))
1488 error ("non-static declaration of %q+D follows "
1489 "static declaration", newdecl);
1491 error ("static declaration of %q+D follows "
1492 "non-static declaration", newdecl);
1494 locate_old_decl (olddecl);
1498 /* Two objects with the same name declared at the same block
1499 scope must both be external references (6.7p3). */
1500 else if (!DECL_FILE_SCOPE_P (newdecl))
1502 if (DECL_EXTERNAL (newdecl))
1504 /* Extern with initializer at block scope, which will
1505 already have received an error. */
1507 else if (DECL_EXTERNAL (olddecl))
1509 error ("declaration of %q+D with no linkage follows "
1510 "extern declaration", newdecl);
1511 locate_old_decl (olddecl);
1515 error ("redeclaration of %q+D with no linkage", newdecl);
1516 locate_old_decl (olddecl);
1524 /* All decls must agree on a visibility. */
1525 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1526 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1527 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1529 warned |= warning (0, "redeclaration of %q+D with different visibility "
1530 "(old visibility preserved)", newdecl);
1533 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1535 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1536 if (DECL_DECLARED_INLINE_P (newdecl)
1537 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1539 warned |= warning (OPT_Wattributes,
1540 "inline declaration of %qD follows "
1541 "declaration with attribute noinline", newdecl);
1543 else if (DECL_DECLARED_INLINE_P (olddecl)
1544 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1546 warned |= warning (OPT_Wattributes,
1547 "declaration of %q+D with attribute "
1548 "noinline follows inline declaration ", newdecl);
1551 else /* PARM_DECL, VAR_DECL */
1553 /* Redeclaration of a parameter is a constraint violation (this is
1554 not explicitly stated, but follows from C99 6.7p3 [no more than
1555 one declaration of the same identifier with no linkage in the
1556 same scope, except type tags] and 6.2.2p6 [parameters have no
1557 linkage]). We must check for a forward parameter declaration,
1558 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1559 an extension, the mandatory diagnostic for which is handled by
1560 mark_forward_parm_decls. */
1562 if (TREE_CODE (newdecl) == PARM_DECL
1563 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1565 error ("redefinition of parameter %q+D", newdecl);
1566 locate_old_decl (olddecl);
1571 /* Optional warning for completely redundant decls. */
1572 if (!warned && !pedwarned
1573 && warn_redundant_decls
1574 /* Don't warn about a function declaration followed by a
1576 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1577 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1578 /* Don't warn about redundant redeclarations of builtins. */
1579 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1580 && !DECL_BUILT_IN (newdecl)
1581 && DECL_BUILT_IN (olddecl)
1582 && !C_DECL_DECLARED_BUILTIN (olddecl))
1583 /* Don't warn about an extern followed by a definition. */
1584 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1585 /* Don't warn about forward parameter decls. */
1586 && !(TREE_CODE (newdecl) == PARM_DECL
1587 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1588 /* Don't warn about a variable definition following a declaration. */
1589 && !(TREE_CODE (newdecl) == VAR_DECL
1590 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1592 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1596 /* Report location of previous decl/defn. */
1597 if (warned || pedwarned)
1598 locate_old_decl (olddecl);
1600 #undef DECL_EXTERN_INLINE
1605 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1606 consistent with OLDDECL, but carries new information. Merge the
1607 new information into OLDDECL. This function issues no
1611 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1613 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1614 && DECL_INITIAL (newdecl) != 0);
1615 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1616 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1617 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1618 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1619 bool extern_changed = false;
1621 /* For real parm decl following a forward decl, rechain the old decl
1622 in its new location and clear TREE_ASM_WRITTEN (it's not a
1623 forward decl anymore). */
1624 if (TREE_CODE (newdecl) == PARM_DECL
1625 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1627 struct c_binding *b, **here;
1629 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
1630 if ((*here)->decl == olddecl)
1637 b->prev = current_scope->bindings;
1638 current_scope->bindings = b;
1640 TREE_ASM_WRITTEN (olddecl) = 0;
1643 DECL_ATTRIBUTES (newdecl)
1644 = targetm.merge_decl_attributes (olddecl, newdecl);
1646 /* Merge the data types specified in the two decls. */
1648 = TREE_TYPE (olddecl)
1649 = composite_type (newtype, oldtype);
1651 /* Lay the type out, unless already done. */
1652 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1654 if (TREE_TYPE (newdecl) != error_mark_node)
1655 layout_type (TREE_TYPE (newdecl));
1656 if (TREE_CODE (newdecl) != FUNCTION_DECL
1657 && TREE_CODE (newdecl) != TYPE_DECL
1658 && TREE_CODE (newdecl) != CONST_DECL)
1659 layout_decl (newdecl, 0);
1663 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1664 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1665 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1666 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1667 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1669 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1670 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
1674 /* Keep the old rtl since we can safely use it. */
1675 if (HAS_RTL_P (olddecl))
1676 COPY_DECL_RTL (olddecl, newdecl);
1678 /* Merge the type qualifiers. */
1679 if (TREE_READONLY (newdecl))
1680 TREE_READONLY (olddecl) = 1;
1682 if (TREE_THIS_VOLATILE (newdecl))
1683 TREE_THIS_VOLATILE (olddecl) = 1;
1685 /* Merge deprecatedness. */
1686 if (TREE_DEPRECATED (newdecl))
1687 TREE_DEPRECATED (olddecl) = 1;
1689 /* If a decl is in a system header and the other isn't, keep the one on the
1690 system header. Otherwise, keep source location of definition rather than
1691 declaration and of prototype rather than non-prototype unless that
1692 prototype is built-in. */
1693 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
1694 && DECL_IN_SYSTEM_HEADER (olddecl)
1695 && !DECL_IN_SYSTEM_HEADER (newdecl) )
1696 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1697 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
1698 && DECL_IN_SYSTEM_HEADER (newdecl)
1699 && !DECL_IN_SYSTEM_HEADER (olddecl))
1700 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
1701 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1702 || (old_is_prototype && !new_is_prototype
1703 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1704 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1706 /* Merge the initialization information. */
1707 if (DECL_INITIAL (newdecl) == 0)
1708 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1710 /* Merge the threadprivate attribute. */
1711 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1713 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1714 C_DECL_THREADPRIVATE_P (newdecl) = 1;
1717 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1719 /* Merge the section attribute.
1720 We want to issue an error if the sections conflict but that
1721 must be done later in decl_attributes since we are called
1722 before attributes are assigned. */
1723 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1724 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1726 /* Copy the assembler name.
1727 Currently, it can only be defined in the prototype. */
1728 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1730 /* Use visibility of whichever declaration had it specified */
1731 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1733 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1734 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1737 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1739 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1740 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1741 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1742 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1743 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1744 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1745 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1746 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
1747 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1748 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1749 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1752 /* Merge the storage class information. */
1753 merge_weak (newdecl, olddecl);
1755 /* For functions, static overrides non-static. */
1756 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1758 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1759 /* This is since we don't automatically
1760 copy the attributes of NEWDECL into OLDDECL. */
1761 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1762 /* If this clears `static', clear it in the identifier too. */
1763 if (!TREE_PUBLIC (olddecl))
1764 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1768 /* In c99, 'extern' declaration before (or after) 'inline' means this
1769 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1771 if (TREE_CODE (newdecl) == FUNCTION_DECL
1772 && !flag_gnu89_inline
1773 && (DECL_DECLARED_INLINE_P (newdecl)
1774 || DECL_DECLARED_INLINE_P (olddecl))
1775 && (!DECL_DECLARED_INLINE_P (newdecl)
1776 || !DECL_DECLARED_INLINE_P (olddecl)
1777 || !DECL_EXTERNAL (olddecl))
1778 && DECL_EXTERNAL (newdecl)
1779 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
1780 && !current_function_decl)
1781 DECL_EXTERNAL (newdecl) = 0;
1783 if (DECL_EXTERNAL (newdecl))
1785 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1786 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1788 /* An extern decl does not override previous storage class. */
1789 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1790 if (!DECL_EXTERNAL (newdecl))
1792 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1793 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1798 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1799 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1802 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1804 /* If we're redefining a function previously defined as extern
1805 inline, make sure we emit debug info for the inline before we
1806 throw it away, in case it was inlined into a function that
1807 hasn't been written out yet. */
1808 if (new_is_definition && DECL_INITIAL (olddecl))
1809 /* The new defn must not be inline. */
1810 DECL_UNINLINABLE (newdecl) = 1;
1813 /* If either decl says `inline', this fn is inline, unless
1814 its definition was passed already. */
1815 if (DECL_DECLARED_INLINE_P (newdecl)
1816 || DECL_DECLARED_INLINE_P (olddecl))
1817 DECL_DECLARED_INLINE_P (newdecl) = 1;
1819 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1820 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1822 DECL_DISREGARD_INLINE_LIMITS (newdecl)
1823 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
1824 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
1825 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
1828 if (DECL_BUILT_IN (olddecl))
1830 /* If redeclaring a builtin function, it stays built in.
1831 But it gets tagged as having been declared. */
1832 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1833 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1834 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1835 if (new_is_prototype)
1836 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1838 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1839 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1842 /* Preserve function specific target and optimization options */
1843 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
1844 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
1845 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
1846 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
1848 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
1849 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
1850 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
1851 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
1853 /* Also preserve various other info from the definition. */
1854 if (!new_is_definition)
1857 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1858 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1859 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1860 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1861 gimple_set_body (newdecl, gimple_body (olddecl));
1862 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
1863 for (t = DECL_ARGUMENTS (newdecl); t ; t = TREE_CHAIN (t))
1864 DECL_CONTEXT (t) = newdecl;
1866 /* See if we've got a function to instantiate from. */
1867 if (DECL_SAVED_TREE (olddecl))
1868 DECL_ABSTRACT_ORIGIN (newdecl)
1869 = DECL_ABSTRACT_ORIGIN (olddecl);
1873 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
1875 /* Merge the USED information. */
1876 if (TREE_USED (olddecl))
1877 TREE_USED (newdecl) = 1;
1878 else if (TREE_USED (newdecl))
1879 TREE_USED (olddecl) = 1;
1881 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1882 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1884 unsigned olddecl_uid = DECL_UID (olddecl);
1885 tree olddecl_context = DECL_CONTEXT (olddecl);
1886 tree olddecl_arguments = NULL;
1887 if (TREE_CODE (olddecl) == FUNCTION_DECL)
1888 olddecl_arguments = DECL_ARGUMENTS (olddecl);
1890 memcpy ((char *) olddecl + sizeof (struct tree_common),
1891 (char *) newdecl + sizeof (struct tree_common),
1892 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1893 switch (TREE_CODE (olddecl))
1896 gimple_set_body (olddecl, gimple_body (newdecl));
1906 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1907 (char *) newdecl + sizeof (struct tree_decl_common),
1908 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1913 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1914 (char *) newdecl + sizeof (struct tree_decl_common),
1915 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1917 DECL_UID (olddecl) = olddecl_uid;
1918 DECL_CONTEXT (olddecl) = olddecl_context;
1919 if (TREE_CODE (olddecl) == FUNCTION_DECL)
1920 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
1923 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1924 so that encode_section_info has a chance to look at the new decl
1925 flags and attributes. */
1926 if (DECL_RTL_SET_P (olddecl)
1927 && (TREE_CODE (olddecl) == FUNCTION_DECL
1928 || (TREE_CODE (olddecl) == VAR_DECL
1929 && TREE_STATIC (olddecl))))
1930 make_decl_rtl (olddecl);
1932 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
1933 and the definition is coming from the old version, cgraph needs
1934 to be called again. */
1935 if (extern_changed && !new_is_definition
1936 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
1937 cgraph_mark_if_needed (olddecl);
1940 /* Handle when a new declaration NEWDECL has the same name as an old
1941 one OLDDECL in the same binding contour. Prints an error message
1944 If safely possible, alter OLDDECL to look like NEWDECL, and return
1945 true. Otherwise, return false. */
1948 duplicate_decls (tree newdecl, tree olddecl)
1950 tree newtype = NULL, oldtype = NULL;
1952 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1954 /* Avoid `unused variable' and other warnings for OLDDECL. */
1955 TREE_NO_WARNING (olddecl) = 1;
1959 merge_decls (newdecl, olddecl, newtype, oldtype);
1964 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1966 warn_if_shadowing (tree new_decl)
1968 struct c_binding *b;
1970 /* Shadow warnings wanted? */
1972 /* No shadow warnings for internally generated vars. */
1973 || DECL_IS_BUILTIN (new_decl)
1974 /* No shadow warnings for vars made for inlining. */
1975 || DECL_FROM_INLINE (new_decl))
1978 /* Is anything being shadowed? Invisible decls do not count. */
1979 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1980 if (b->decl && b->decl != new_decl && !b->invisible)
1982 tree old_decl = b->decl;
1984 if (old_decl == error_mark_node)
1986 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
1987 "non-variable", new_decl);
1990 else if (TREE_CODE (old_decl) == PARM_DECL)
1991 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
1993 else if (DECL_FILE_SCOPE_P (old_decl))
1994 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
1995 "declaration", new_decl);
1996 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1997 && DECL_BUILT_IN (old_decl))
1999 warning (OPT_Wshadow, "declaration of %q+D shadows "
2000 "a built-in function", new_decl);
2004 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2007 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
2013 /* Record a decl-node X as belonging to the current lexical scope.
2014 Check for errors (such as an incompatible declaration for the same
2015 name already seen in the same scope).
2017 Returns either X or an old decl for the same name.
2018 If an old decl is returned, it may have been smashed
2019 to agree with what X says. */
2024 tree name = DECL_NAME (x);
2025 struct c_scope *scope = current_scope;
2026 struct c_binding *b;
2027 bool nested = false;
2028 location_t locus = DECL_SOURCE_LOCATION (x);
2030 /* Must set DECL_CONTEXT for everything not at file scope or
2031 DECL_FILE_SCOPE_P won't work. Local externs don't count
2032 unless they have initializers (which generate code). */
2033 if (current_function_decl
2034 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2035 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2036 DECL_CONTEXT (x) = current_function_decl;
2038 /* If this is of variably modified type, prevent jumping into its
2040 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2041 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2042 c_begin_vm_scope (scope->depth);
2044 /* Anonymous decls are just inserted in the scope. */
2047 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2052 /* First, see if there is another declaration with the same name in
2053 the current scope. If there is, duplicate_decls may do all the
2054 work for us. If duplicate_decls returns false, that indicates
2055 two incompatible decls in the same scope; we are to silently
2056 replace the old one (duplicate_decls has issued all appropriate
2057 diagnostics). In particular, we should not consider possible
2058 duplicates in the external scope, or shadowing. */
2059 b = I_SYMBOL_BINDING (name);
2060 if (b && B_IN_SCOPE (b, scope))
2062 struct c_binding *b_ext, *b_use;
2063 tree type = TREE_TYPE (x);
2064 tree visdecl = b->decl;
2065 tree vistype = TREE_TYPE (visdecl);
2066 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2067 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2068 b->inner_comp = false;
2071 /* If this is an external linkage declaration, we should check
2072 for compatibility with the type in the external scope before
2073 setting the type at this scope based on the visible
2074 information only. */
2075 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2077 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2078 b_ext = b_ext->shadowed;
2083 TREE_TYPE (b_use->decl) = b_use->type;
2086 if (duplicate_decls (x, b_use->decl))
2090 /* Save the updated type in the external scope and
2091 restore the proper type for this scope. */
2093 if (comptypes (vistype, type))
2094 thistype = composite_type (vistype, type);
2096 thistype = TREE_TYPE (b_use->decl);
2097 b_use->type = TREE_TYPE (b_use->decl);
2098 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2099 && DECL_BUILT_IN (b_use->decl))
2101 = build_type_attribute_variant (thistype,
2104 TREE_TYPE (b_use->decl) = thistype;
2109 goto skip_external_and_shadow_checks;
2112 /* All declarations with external linkage, and all external
2113 references, go in the external scope, no matter what scope is
2114 current. However, the binding in that scope is ignored for
2115 purposes of normal name lookup. A separate binding structure is
2116 created in the requested scope; this governs the normal
2117 visibility of the symbol.
2119 The binding in the externals scope is used exclusively for
2120 detecting duplicate declarations of the same object, no matter
2121 what scope they are in; this is what we do here. (C99 6.2.7p2:
2122 All declarations that refer to the same object or function shall
2123 have compatible type; otherwise, the behavior is undefined.) */
2124 if (DECL_EXTERNAL (x) || scope == file_scope)
2126 tree type = TREE_TYPE (x);
2129 bool type_saved = false;
2130 if (b && !B_IN_EXTERNAL_SCOPE (b)
2131 && (TREE_CODE (b->decl) == FUNCTION_DECL
2132 || TREE_CODE (b->decl) == VAR_DECL)
2133 && DECL_FILE_SCOPE_P (b->decl))
2136 vistype = TREE_TYPE (visdecl);
2138 if (scope != file_scope
2139 && !DECL_IN_SYSTEM_HEADER (x))
2140 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2142 while (b && !B_IN_EXTERNAL_SCOPE (b))
2144 /* If this decl might be modified, save its type. This is
2145 done here rather than when the decl is first bound
2146 because the type may change after first binding, through
2147 being completed or through attributes being added. If we
2148 encounter multiple such decls, only the first should have
2149 its type saved; the others will already have had their
2150 proper types saved and the types will not have changed as
2151 their scopes will not have been re-entered. */
2152 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2154 b->type = TREE_TYPE (b->decl);
2157 if (B_IN_FILE_SCOPE (b)
2158 && TREE_CODE (b->decl) == VAR_DECL
2159 && TREE_STATIC (b->decl)
2160 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2161 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2162 && TREE_CODE (type) == ARRAY_TYPE
2163 && TYPE_DOMAIN (type)
2164 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2165 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2167 /* Array type completed in inner scope, which should be
2168 diagnosed if the completion does not have size 1 and
2169 it does not get completed in the file scope. */
2170 b->inner_comp = true;
2175 /* If a matching external declaration has been found, set its
2176 type to the composite of all the types of that declaration.
2177 After the consistency checks, it will be reset to the
2178 composite of the visible types only. */
2179 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2181 TREE_TYPE (b->decl) = b->type;
2183 /* The point of the same_translation_unit_p check here is,
2184 we want to detect a duplicate decl for a construct like
2185 foo() { extern bar(); } ... static bar(); but not if
2186 they are in different translation units. In any case,
2187 the static does not go in the externals scope. */
2189 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2190 && duplicate_decls (x, b->decl))
2195 if (comptypes (vistype, type))
2196 thistype = composite_type (vistype, type);
2198 thistype = TREE_TYPE (b->decl);
2202 b->type = TREE_TYPE (b->decl);
2203 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2205 = build_type_attribute_variant (thistype,
2206 TYPE_ATTRIBUTES (b->type));
2207 TREE_TYPE (b->decl) = thistype;
2208 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2212 else if (TREE_PUBLIC (x))
2214 if (visdecl && !b && duplicate_decls (x, visdecl))
2216 /* An external declaration at block scope referring to a
2217 visible entity with internal linkage. The composite
2218 type will already be correct for this scope, so we
2219 just need to fall through to make the declaration in
2226 bind (name, x, external_scope, /*invisible=*/true,
2227 /*nested=*/false, locus);
2233 if (TREE_CODE (x) != PARM_DECL)
2234 warn_if_shadowing (x);
2236 skip_external_and_shadow_checks:
2237 if (TREE_CODE (x) == TYPE_DECL)
2238 set_underlying_type (x);
2240 bind (name, x, scope, /*invisible=*/false, nested, locus);
2242 /* If x's type is incomplete because it's based on a
2243 structure or union which has not yet been fully declared,
2244 attach it to that structure or union type, so we can go
2245 back and complete the variable declaration later, if the
2246 structure or union gets fully declared.
2248 If the input is erroneous, we can have error_mark in the type
2249 slot (e.g. "f(void a, ...)") - that doesn't count as an
2251 if (TREE_TYPE (x) != error_mark_node
2252 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2254 tree element = TREE_TYPE (x);
2256 while (TREE_CODE (element) == ARRAY_TYPE)
2257 element = TREE_TYPE (element);
2258 element = TYPE_MAIN_VARIANT (element);
2260 if ((TREE_CODE (element) == RECORD_TYPE
2261 || TREE_CODE (element) == UNION_TYPE)
2262 && (TREE_CODE (x) != TYPE_DECL
2263 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2264 && !COMPLETE_TYPE_P (element))
2265 C_TYPE_INCOMPLETE_VARS (element)
2266 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2271 /* Record X as belonging to file scope.
2272 This is used only internally by the Objective-C front end,
2273 and is limited to its needs. duplicate_decls is not called;
2274 if there is any preexisting decl for this identifier, it is an ICE. */
2277 pushdecl_top_level (tree x)
2280 bool nested = false;
2281 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2283 name = DECL_NAME (x);
2285 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2287 if (TREE_PUBLIC (x))
2289 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2294 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2300 implicit_decl_warning (tree id, tree olddecl)
2302 if (warn_implicit_function_declaration)
2307 warned = pedwarn (input_location, OPT_Wimplicit_function_declaration,
2308 "implicit declaration of function %qE", id);
2310 warned = warning (OPT_Wimplicit_function_declaration,
2311 G_("implicit declaration of function %qE"), id);
2312 if (olddecl && warned)
2313 locate_old_decl (olddecl);
2317 /* Generate an implicit declaration for identifier FUNCTIONID as a
2318 function of type int (). */
2321 implicitly_declare (tree functionid)
2323 struct c_binding *b;
2327 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2329 if (B_IN_SCOPE (b, external_scope))
2338 if (decl == error_mark_node)
2341 /* FIXME: Objective-C has weird not-really-builtin functions
2342 which are supposed to be visible automatically. They wind up
2343 in the external scope because they're pushed before the file
2344 scope gets created. Catch this here and rebind them into the
2346 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2348 bind (functionid, decl, file_scope,
2349 /*invisible=*/false, /*nested=*/true,
2350 DECL_SOURCE_LOCATION (decl));
2355 tree newtype = default_function_type;
2357 TREE_TYPE (decl) = b->type;
2358 /* Implicit declaration of a function already declared
2359 (somehow) in a different scope, or as a built-in.
2360 If this is the first time this has happened, warn;
2361 then recycle the old declaration but with the new type. */
2362 if (!C_DECL_IMPLICIT (decl))
2364 implicit_decl_warning (functionid, decl);
2365 C_DECL_IMPLICIT (decl) = 1;
2367 if (DECL_BUILT_IN (decl))
2369 newtype = build_type_attribute_variant (newtype,
2371 (TREE_TYPE (decl)));
2372 if (!comptypes (newtype, TREE_TYPE (decl)))
2374 warning (0, "incompatible implicit declaration of built-in"
2375 " function %qD", decl);
2376 newtype = TREE_TYPE (decl);
2381 if (!comptypes (newtype, TREE_TYPE (decl)))
2383 error ("incompatible implicit declaration of function %qD",
2385 locate_old_decl (decl);
2388 b->type = TREE_TYPE (decl);
2389 TREE_TYPE (decl) = newtype;
2390 bind (functionid, decl, current_scope,
2391 /*invisible=*/false, /*nested=*/true,
2392 DECL_SOURCE_LOCATION (decl));
2397 /* Not seen before. */
2398 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2399 DECL_EXTERNAL (decl) = 1;
2400 TREE_PUBLIC (decl) = 1;
2401 C_DECL_IMPLICIT (decl) = 1;
2402 implicit_decl_warning (functionid, 0);
2403 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2405 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2407 /* C89 says implicit declarations are in the innermost block.
2408 So we record the decl in the standard fashion. */
2409 decl = pushdecl (decl);
2411 /* No need to call objc_check_decl here - it's a function type. */
2412 rest_of_decl_compilation (decl, 0, 0);
2414 /* Write a record describing this implicit function declaration
2415 to the prototypes file (if requested). */
2416 gen_aux_info_record (decl, 0, 1, 0);
2418 /* Possibly apply some default attributes to this implicit declaration. */
2419 decl_attributes (&decl, NULL_TREE, 0);
2424 /* Issue an error message for a reference to an undeclared variable
2425 ID, including a reference to a builtin outside of function-call
2426 context. Establish a binding of the identifier to error_mark_node
2427 in an appropriate scope, which will suppress further errors for the
2428 same identifier. The error message should be given location LOC. */
2430 undeclared_variable (tree id, location_t loc)
2432 static bool already = false;
2433 struct c_scope *scope;
2435 if (current_function_decl == 0)
2437 error ("%H%qE undeclared here (not in a function)", &loc, id);
2438 scope = current_scope;
2442 error ("%H%qE undeclared (first use in this function)", &loc, id);
2446 error ("%H(Each undeclared identifier is reported only once", &loc);
2447 error ("%Hfor each function it appears in.)", &loc);
2451 /* If we are parsing old-style parameter decls, current_function_decl
2452 will be nonnull but current_function_scope will be null. */
2453 scope = current_function_scope ? current_function_scope : current_scope;
2455 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
2459 /* Subroutine of lookup_label, declare_label, define_label: construct a
2460 LABEL_DECL with all the proper frills. */
2463 make_label (tree name, location_t location)
2465 tree label = build_decl (LABEL_DECL, name, void_type_node);
2467 DECL_CONTEXT (label) = current_function_decl;
2468 DECL_MODE (label) = VOIDmode;
2469 DECL_SOURCE_LOCATION (label) = location;
2474 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2475 Create one if none exists so far for the current function.
2476 This is called when a label is used in a goto expression or
2477 has its address taken. */
2480 lookup_label (tree name)
2484 if (current_function_decl == 0)
2486 error ("label %qE referenced outside of any function", name);
2490 /* Use a label already defined or ref'd with this name, but not if
2491 it is inherited from a containing function and wasn't declared
2493 label = I_LABEL_DECL (name);
2494 if (label && (DECL_CONTEXT (label) == current_function_decl
2495 || C_DECLARED_LABEL_FLAG (label)))
2497 /* If the label has only been declared, update its apparent
2498 location to point here, for better diagnostics if it
2499 turns out not to have been defined. */
2500 if (!TREE_USED (label))
2501 DECL_SOURCE_LOCATION (label) = input_location;
2505 /* No label binding for that identifier; make one. */
2506 label = make_label (name, input_location);
2508 /* Ordinary labels go in the current function scope. */
2509 bind (name, label, current_function_scope,
2510 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2514 /* Make a label named NAME in the current function, shadowing silently
2515 any that may be inherited from containing functions or containing
2516 scopes. This is called for __label__ declarations. */
2519 declare_label (tree name)
2521 struct c_binding *b = I_LABEL_BINDING (name);
2524 /* Check to make sure that the label hasn't already been declared
2526 if (b && B_IN_CURRENT_SCOPE (b))
2528 error ("duplicate label declaration %qE", name);
2529 locate_old_decl (b->decl);
2531 /* Just use the previous declaration. */
2535 label = make_label (name, input_location);
2536 C_DECLARED_LABEL_FLAG (label) = 1;
2538 /* Declared labels go in the current scope. */
2539 bind (name, label, current_scope,
2540 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2544 /* Define a label, specifying the location in the source file.
2545 Return the LABEL_DECL node for the label, if the definition is valid.
2546 Otherwise return 0. */
2549 define_label (location_t location, tree name)
2551 /* Find any preexisting label with this name. It is an error
2552 if that label has already been defined in this function, or
2553 if there is a containing function with a declared label with
2555 tree label = I_LABEL_DECL (name);
2556 struct c_label_list *nlist_se, *nlist_vm;
2559 && ((DECL_CONTEXT (label) == current_function_decl
2560 && DECL_INITIAL (label) != 0)
2561 || (DECL_CONTEXT (label) != current_function_decl
2562 && C_DECLARED_LABEL_FLAG (label))))
2564 error ("%Hduplicate label %qD", &location, label);
2565 locate_old_decl (label);
2568 else if (label && DECL_CONTEXT (label) == current_function_decl)
2570 /* The label has been used or declared already in this function,
2571 but not defined. Update its location to point to this
2573 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2574 error ("%Jjump into statement expression", label);
2575 if (C_DECL_UNDEFINABLE_VM (label))
2576 error ("%Jjump into scope of identifier with variably modified type",
2578 DECL_SOURCE_LOCATION (label) = location;
2582 /* No label binding for that identifier; make one. */
2583 label = make_label (name, location);
2585 /* Ordinary labels go in the current function scope. */
2586 bind (name, label, current_function_scope,
2587 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2590 if (!in_system_header && lookup_name (name))
2591 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2592 "for labels, identifier %qE conflicts", &location, name);
2594 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2595 nlist_se->next = label_context_stack_se->labels_def;
2596 nlist_se->label = label;
2597 label_context_stack_se->labels_def = nlist_se;
2599 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2600 nlist_vm->next = label_context_stack_vm->labels_def;
2601 nlist_vm->label = label;
2602 label_context_stack_vm->labels_def = nlist_vm;
2604 /* Mark label as having been defined. */
2605 DECL_INITIAL (label) = error_mark_node;
2609 /* Given NAME, an IDENTIFIER_NODE,
2610 return the structure (or union or enum) definition for that name.
2611 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2612 CODE says which kind of type the caller wants;
2613 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2614 If the wrong kind of type is found, an error is reported. */
2617 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2619 struct c_binding *b = I_TAG_BINDING (name);
2625 /* We only care about whether it's in this level if
2626 thislevel_only was set or it might be a type clash. */
2627 if (thislevel_only || TREE_CODE (b->decl) != code)
2629 /* For our purposes, a tag in the external scope is the same as
2630 a tag in the file scope. (Primarily relevant to Objective-C
2631 and its builtin structure tags, which get pushed before the
2632 file scope is created.) */
2633 if (B_IN_CURRENT_SCOPE (b)
2634 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2638 if (thislevel_only && !thislevel)
2641 if (TREE_CODE (b->decl) != code)
2643 /* Definition isn't the kind we were looking for. */
2644 pending_invalid_xref = name;
2645 pending_invalid_xref_location = input_location;
2647 /* If in the same binding level as a declaration as a tag
2648 of a different type, this must not be allowed to
2649 shadow that tag, so give the error immediately.
2650 (For example, "struct foo; union foo;" is invalid.) */
2652 pending_xref_error ();
2657 /* Print an error message now
2658 for a recent invalid struct, union or enum cross reference.
2659 We don't print them immediately because they are not invalid
2660 when used in the `struct foo;' construct for shadowing. */
2663 pending_xref_error (void)
2665 if (pending_invalid_xref != 0)
2666 error ("%H%qE defined as wrong kind of tag",
2667 &pending_invalid_xref_location, pending_invalid_xref);
2668 pending_invalid_xref = 0;
2672 /* Look up NAME in the current scope and its superiors
2673 in the namespace of variables, functions and typedefs.
2674 Return a ..._DECL node of some kind representing its definition,
2675 or return 0 if it is undefined. */
2678 lookup_name (tree name)
2680 struct c_binding *b = I_SYMBOL_BINDING (name);
2681 if (b && !b->invisible)
2686 /* Similar to `lookup_name' but look only at the indicated scope. */
2689 lookup_name_in_scope (tree name, struct c_scope *scope)
2691 struct c_binding *b;
2693 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2694 if (B_IN_SCOPE (b, scope))
2699 /* Create the predefined scalar types of C,
2700 and some nodes representing standard constants (0, 1, (void *) 0).
2701 Initialize the global scope.
2702 Make definitions for built-in primitive functions. */
2705 c_init_decl_processing (void)
2707 location_t save_loc = input_location;
2709 /* Initialize reserved words for parser. */
2712 current_function_decl = 0;
2714 gcc_obstack_init (&parser_obstack);
2716 /* Make the externals scope. */
2718 external_scope = current_scope;
2720 /* Declarations from c_common_nodes_and_builtins must not be associated
2721 with this input file, lest we get differences between using and not
2722 using preprocessed headers. */
2723 input_location = BUILTINS_LOCATION;
2725 build_common_tree_nodes (flag_signed_char, false);
2727 c_common_nodes_and_builtins ();
2729 /* In C, comparisons and TRUTH_* expressions have type int. */
2730 truthvalue_type_node = integer_type_node;
2731 truthvalue_true_node = integer_one_node;
2732 truthvalue_false_node = integer_zero_node;
2734 /* Even in C99, which has a real boolean type. */
2735 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2736 boolean_type_node));
2738 input_location = save_loc;
2740 pedantic_lvalues = true;
2742 make_fname_decl = c_make_fname_decl;
2743 start_fname_decls ();
2746 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2747 decl, NAME is the initialization string and TYPE_DEP indicates whether
2748 NAME depended on the type of the function. As we don't yet implement
2749 delayed emission of static data, we mark the decl as emitted
2750 so it is not placed in the output. Anything using it must therefore pull
2751 out the STRING_CST initializer directly. FIXME. */
2754 c_make_fname_decl (tree id, int type_dep)
2756 const char *name = fname_as_string (type_dep);
2757 tree decl, type, init;
2758 size_t length = strlen (name);
2760 type = build_array_type (char_type_node,
2761 build_index_type (size_int (length)));
2762 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2764 decl = build_decl (VAR_DECL, id, type);
2766 TREE_STATIC (decl) = 1;
2767 TREE_READONLY (decl) = 1;
2768 DECL_ARTIFICIAL (decl) = 1;
2770 init = build_string (length + 1, name);
2771 free (CONST_CAST (char *, name));
2772 TREE_TYPE (init) = type;
2773 DECL_INITIAL (decl) = init;
2775 TREE_USED (decl) = 1;
2777 if (current_function_decl
2778 /* For invalid programs like this:
2781 const char* p = __FUNCTION__;
2783 the __FUNCTION__ is believed to appear in K&R style function
2784 parameter declarator. In that case we still don't have
2786 && (!errorcount || current_function_scope))
2788 DECL_CONTEXT (decl) = current_function_decl;
2789 bind (id, decl, current_function_scope,
2790 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2793 finish_decl (decl, init, NULL_TREE);
2799 c_builtin_function (tree decl)
2801 tree type = TREE_TYPE (decl);
2802 tree id = DECL_NAME (decl);
2804 const char *name = IDENTIFIER_POINTER (id);
2805 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2807 /* Should never be called on a symbol with a preexisting meaning. */
2808 gcc_assert (!I_SYMBOL_BINDING (id));
2810 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
2813 /* Builtins in the implementation namespace are made visible without
2814 needing to be explicitly declared. See push_file_scope. */
2815 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2817 TREE_CHAIN (decl) = visible_builtins;
2818 visible_builtins = decl;
2825 c_builtin_function_ext_scope (tree decl)
2827 tree type = TREE_TYPE (decl);
2828 tree id = DECL_NAME (decl);
2830 const char *name = IDENTIFIER_POINTER (id);
2831 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2833 /* Should never be called on a symbol with a preexisting meaning. */
2834 gcc_assert (!I_SYMBOL_BINDING (id));
2836 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
2839 /* Builtins in the implementation namespace are made visible without
2840 needing to be explicitly declared. See push_file_scope. */
2841 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2843 TREE_CHAIN (decl) = visible_builtins;
2844 visible_builtins = decl;
2850 /* Called when a declaration is seen that contains no names to declare.
2851 If its type is a reference to a structure, union or enum inherited
2852 from a containing scope, shadow that tag name for the current scope
2853 with a forward reference.
2854 If its type defines a new named structure or union
2855 or defines an enum, it is valid but we need not do anything here.
2856 Otherwise, it is an error. */
2859 shadow_tag (const struct c_declspecs *declspecs)
2861 shadow_tag_warned (declspecs, 0);
2864 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2867 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2869 bool found_tag = false;
2871 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2873 tree value = declspecs->type;
2874 enum tree_code code = TREE_CODE (value);
2876 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2877 /* Used to test also that TYPE_SIZE (value) != 0.
2878 That caused warning for `struct foo;' at top level in the file. */
2880 tree name = TYPE_NAME (value);
2887 if (warned != 1 && code != ENUMERAL_TYPE)
2888 /* Empty unnamed enum OK */
2890 pedwarn (input_location, 0,
2891 "unnamed struct/union that defines no instances");
2895 else if (!declspecs->tag_defined_p
2896 && declspecs->storage_class != csc_none)
2899 pedwarn (input_location, 0,
2900 "empty declaration with storage class specifier "
2901 "does not redeclare tag");
2903 pending_xref_error ();
2905 else if (!declspecs->tag_defined_p
2906 && (declspecs->const_p
2907 || declspecs->volatile_p
2908 || declspecs->restrict_p))
2911 pedwarn (input_location, 0,
2912 "empty declaration with type qualifier "
2913 "does not redeclare tag");
2915 pending_xref_error ();
2919 pending_invalid_xref = 0;
2920 t = lookup_tag (code, name, 1);
2924 t = make_node (code);
2931 if (warned != 1 && !in_system_header)
2933 pedwarn (input_location, 0,
2934 "useless type name in empty declaration");
2939 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2941 pedwarn (input_location, 0, "useless type name in empty declaration");
2945 pending_invalid_xref = 0;
2947 if (declspecs->inline_p)
2949 error ("%<inline%> in empty declaration");
2953 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2955 error ("%<auto%> in file-scope empty declaration");
2959 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2961 error ("%<register%> in file-scope empty declaration");
2965 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2967 warning (0, "useless storage class specifier in empty declaration");
2971 if (!warned && !in_system_header && declspecs->thread_p)
2973 warning (0, "useless %<__thread%> in empty declaration");
2977 if (!warned && !in_system_header && (declspecs->const_p
2978 || declspecs->volatile_p
2979 || declspecs->restrict_p))
2981 warning (0, "useless type qualifier in empty declaration");
2988 pedwarn (input_location, 0, "empty declaration");
2993 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2994 bits. SPECS represents declaration specifiers that the grammar
2995 only permits to contain type qualifiers and attributes. */
2998 quals_from_declspecs (const struct c_declspecs *specs)
3000 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3001 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3002 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3003 gcc_assert (!specs->type
3004 && !specs->decl_attr
3005 && specs->typespec_word == cts_none
3006 && specs->storage_class == csc_none
3007 && !specs->typedef_p
3008 && !specs->explicit_signed_p
3009 && !specs->deprecated_p
3011 && !specs->long_long_p
3014 && !specs->unsigned_p
3015 && !specs->complex_p
3017 && !specs->thread_p);
3021 /* Construct an array declarator. EXPR is the expression inside [],
3022 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3023 applied to the pointer to which a parameter array is converted).
3024 STATIC_P is true if "static" is inside the [], false otherwise.
3025 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3026 length which is nevertheless a complete type, false otherwise. The
3027 field for the contained declarator is left to be filled in by
3028 set_array_declarator_inner. */
3030 struct c_declarator *
3031 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3034 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3035 struct c_declarator);
3036 declarator->kind = cdk_array;
3037 declarator->declarator = 0;
3038 declarator->u.array.dimen = expr;
3041 declarator->u.array.attrs = quals->attrs;
3042 declarator->u.array.quals = quals_from_declspecs (quals);
3046 declarator->u.array.attrs = NULL_TREE;
3047 declarator->u.array.quals = 0;
3049 declarator->u.array.static_p = static_p;
3050 declarator->u.array.vla_unspec_p = vla_unspec_p;
3053 if (static_p || quals != NULL)
3054 pedwarn (input_location, OPT_pedantic,
3055 "ISO C90 does not support %<static%> or type "
3056 "qualifiers in parameter array declarators");
3058 pedwarn (input_location, OPT_pedantic,
3059 "ISO C90 does not support %<[*]%> array declarators");
3063 if (!current_scope->parm_flag)
3066 error ("%<[*]%> not allowed in other than function prototype scope");
3067 declarator->u.array.vla_unspec_p = false;
3070 current_scope->had_vla_unspec = true;
3075 /* Set the contained declarator of an array declarator. DECL is the
3076 declarator, as constructed by build_array_declarator; INNER is what
3077 appears on the left of the []. */
3079 struct c_declarator *
3080 set_array_declarator_inner (struct c_declarator *decl,
3081 struct c_declarator *inner)
3083 decl->declarator = inner;
3087 /* INIT is a constructor that forms DECL's initializer. If the final
3088 element initializes a flexible array field, add the size of that
3089 initializer to DECL's size. */
3092 add_flexible_array_elts_to_size (tree decl, tree init)
3096 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3099 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3100 type = TREE_TYPE (elt);
3101 if (TREE_CODE (type) == ARRAY_TYPE
3102 && TYPE_SIZE (type) == NULL_TREE
3103 && TYPE_DOMAIN (type) != NULL_TREE
3104 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3106 complete_array_type (&type, elt, false);
3108 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3109 DECL_SIZE_UNIT (decl)
3110 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3114 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3115 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3116 before the type name, and set *EXPR_CONST_OPERANDS, if
3117 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3118 appear in a constant expression. */
3121 groktypename (struct c_type_name *type_name, tree *expr,
3122 bool *expr_const_operands)
3125 tree attrs = type_name->specs->attrs;
3127 type_name->specs->attrs = NULL_TREE;
3129 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3130 false, NULL, &attrs, expr, expr_const_operands,
3133 /* Apply attributes. */
3134 decl_attributes (&type, attrs, 0);
3139 /* Decode a declarator in an ordinary declaration or data definition.
3140 This is called as soon as the type information and variable name
3141 have been parsed, before parsing the initializer if any.
3142 Here we create the ..._DECL node, fill in its type,
3143 and put it on the list of decls for the current context.
3144 The ..._DECL node is returned as the value.
3146 Exception: for arrays where the length is not specified,
3147 the type is left null, to be filled in by `finish_decl'.
3149 Function definitions do not come here; they go to start_function
3150 instead. However, external and forward declarations of functions
3151 do go through here. Structure field declarations are done by
3152 grokfield and not through here. */
3155 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3156 bool initialized, tree attributes)
3160 tree expr = NULL_TREE;
3161 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3163 /* An object declared as __attribute__((deprecated)) suppresses
3164 warnings of uses of other deprecated items. */
3165 if (lookup_attribute ("deprecated", attributes))
3166 deprecated_state = DEPRECATED_SUPPRESS;
3168 decl = grokdeclarator (declarator, declspecs,
3169 NORMAL, initialized, NULL, &attributes, &expr, NULL,
3177 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
3178 warning (OPT_Wmain, "%q+D is usually a function", decl);
3181 /* Is it valid for this decl to have an initializer at all?
3182 If not, set INITIALIZED to zero, which will indirectly
3183 tell 'finish_decl' to ignore the initializer once it is parsed. */
3184 switch (TREE_CODE (decl))
3187 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3192 error ("function %qD is initialized like a variable", decl);
3197 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3198 error ("parameter %qD is initialized", decl);
3203 /* Don't allow initializations for incomplete types except for
3204 arrays which might be completed by the initialization. */
3206 /* This can happen if the array size is an undefined macro.
3207 We already gave a warning, so we don't need another one. */
3208 if (TREE_TYPE (decl) == error_mark_node)
3210 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3212 /* A complete type is ok if size is fixed. */
3214 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3215 || C_DECL_VARIABLE_SIZE (decl))
3217 error ("variable-sized object may not be initialized");
3221 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3223 error ("variable %qD has initializer but incomplete type", decl);
3226 else if (C_DECL_VARIABLE_SIZE (decl))
3228 /* Although C99 is unclear about whether incomplete arrays
3229 of VLAs themselves count as VLAs, it does not make
3230 sense to permit them to be initialized given that
3231 ordinary VLAs may not be initialized. */
3232 error ("variable-sized object may not be initialized");
3239 if (current_scope == file_scope)
3240 TREE_STATIC (decl) = 1;
3242 /* Tell 'pushdecl' this is an initialized decl
3243 even though we don't yet have the initializer expression.
3244 Also tell 'finish_decl' it may store the real initializer. */
3245 DECL_INITIAL (decl) = error_mark_node;
3248 /* If this is a function declaration, write a record describing it to the
3249 prototypes file (if requested). */
3251 if (TREE_CODE (decl) == FUNCTION_DECL)
3252 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3254 /* ANSI specifies that a tentative definition which is not merged with
3255 a non-tentative definition behaves exactly like a definition with an
3256 initializer equal to zero. (Section 3.7.2)
3258 -fno-common gives strict ANSI behavior, though this tends to break
3259 a large body of code that grew up without this rule.
3261 Thread-local variables are never common, since there's no entrenched
3262 body of code to break, and it allows more efficient variable references
3263 in the presence of dynamic linking. */
3265 if (TREE_CODE (decl) == VAR_DECL
3267 && TREE_PUBLIC (decl)
3268 && !DECL_THREAD_LOCAL_P (decl)
3270 DECL_COMMON (decl) = 1;
3272 /* Set attributes here so if duplicate decl, will have proper attributes. */
3273 decl_attributes (&decl, attributes, 0);
3275 /* Handle gnu_inline attribute. */
3276 if (declspecs->inline_p
3277 && !flag_gnu89_inline
3278 && TREE_CODE (decl) == FUNCTION_DECL
3279 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
3280 || current_function_decl))
3282 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3284 else if (declspecs->storage_class != csc_static)
3285 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3288 if (TREE_CODE (decl) == FUNCTION_DECL
3289 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3291 struct c_declarator *ce = declarator;
3293 if (ce->kind == cdk_pointer)
3294 ce = declarator->declarator;
3295 if (ce->kind == cdk_function)
3297 tree args = ce->u.arg_info->parms;
3298 for (; args; args = TREE_CHAIN (args))
3300 tree type = TREE_TYPE (args);
3301 if (type && INTEGRAL_TYPE_P (type)
3302 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3303 DECL_ARG_TYPE (args) = integer_type_node;
3308 if (TREE_CODE (decl) == FUNCTION_DECL
3309 && DECL_DECLARED_INLINE_P (decl)
3310 && DECL_UNINLINABLE (decl)
3311 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3312 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3315 /* C99 6.7.4p3: An inline definition of a function with external
3316 linkage shall not contain a definition of a modifiable object
3317 with static storage duration... */
3318 if (TREE_CODE (decl) == VAR_DECL
3319 && current_scope != file_scope
3320 && TREE_STATIC (decl)
3321 && !TREE_READONLY (decl)
3322 && DECL_DECLARED_INLINE_P (current_function_decl)
3323 && DECL_EXTERNAL (current_function_decl))
3324 pedwarn (input_location, 0,
3325 "%q+D is static but declared in inline function %qD "
3326 "which is not static", decl, current_function_decl);
3328 /* Add this decl to the current scope.
3329 TEM may equal DECL or it may be a previous decl of the same name. */
3330 tem = pushdecl (decl);
3332 if (initialized && DECL_EXTERNAL (tem))
3334 DECL_EXTERNAL (tem) = 0;
3335 TREE_STATIC (tem) = 1;
3341 /* Initialize EH if not initialized yet and exceptions are enabled. */
3344 c_maybe_initialize_eh (void)
3346 if (!flag_exceptions || c_eh_initialized_p)
3349 c_eh_initialized_p = true;
3350 eh_personality_libfunc
3351 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3352 ? "__gcc_personality_sj0"
3353 : "__gcc_personality_v0");
3354 default_init_unwind_resume_libfunc ();
3355 using_eh_for_cleanups ();
3358 /* Finish processing of a declaration;
3359 install its initial value.
3360 If the length of an array type is not known before,
3361 it must be determined now, from the initial value, or it is an error. */
3364 finish_decl (tree decl, tree init, tree asmspec_tree)
3367 int was_incomplete = (DECL_SIZE (decl) == 0);
3368 const char *asmspec = 0;
3370 /* If a name was specified, get the string. */
3371 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3372 && DECL_FILE_SCOPE_P (decl))
3373 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3375 asmspec = TREE_STRING_POINTER (asmspec_tree);
3377 /* If `start_decl' didn't like having an initialization, ignore it now. */
3378 if (init != 0 && DECL_INITIAL (decl) == 0)
3381 /* Don't crash if parm is initialized. */
3382 if (TREE_CODE (decl) == PARM_DECL)
3386 store_init_value (decl, init);
3388 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3389 || TREE_CODE (decl) == FUNCTION_DECL
3390 || TREE_CODE (decl) == FIELD_DECL))
3391 objc_check_decl (decl);
3393 type = TREE_TYPE (decl);
3395 /* Deduce size of array from initialization, if not already known. */
3396 if (TREE_CODE (type) == ARRAY_TYPE
3397 && TYPE_DOMAIN (type) == 0
3398 && TREE_CODE (decl) != TYPE_DECL)
3401 = (TREE_STATIC (decl)
3402 /* Even if pedantic, an external linkage array
3403 may have incomplete type at first. */
3404 ? pedantic && !TREE_PUBLIC (decl)
3405 : !DECL_EXTERNAL (decl));
3407 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3410 /* Get the completed type made by complete_array_type. */
3411 type = TREE_TYPE (decl);
3416 error ("initializer fails to determine size of %q+D", decl);
3421 error ("array size missing in %q+D", decl);
3422 /* If a `static' var's size isn't known,
3423 make it extern as well as static, so it does not get
3425 If it is not `static', then do not mark extern;
3426 finish_incomplete_decl will give it a default size
3427 and it will get allocated. */
3428 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3429 DECL_EXTERNAL (decl) = 1;
3433 error ("zero or negative size array %q+D", decl);
3437 /* For global variables, update the copy of the type that
3438 exists in the binding. */
3439 if (TREE_PUBLIC (decl))
3441 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3442 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3443 b_ext = b_ext->shadowed;
3447 b_ext->type = composite_type (b_ext->type, type);
3458 if (DECL_INITIAL (decl))
3459 TREE_TYPE (DECL_INITIAL (decl)) = type;
3461 layout_decl (decl, 0);
3464 if (TREE_CODE (decl) == VAR_DECL)
3466 if (init && TREE_CODE (init) == CONSTRUCTOR)
3467 add_flexible_array_elts_to_size (decl, init);
3469 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3470 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3471 layout_decl (decl, 0);
3473 if (DECL_SIZE (decl) == 0
3474 /* Don't give an error if we already gave one earlier. */
3475 && TREE_TYPE (decl) != error_mark_node
3476 && (TREE_STATIC (decl)
3477 /* A static variable with an incomplete type
3478 is an error if it is initialized.
3479 Also if it is not file scope.
3480 Otherwise, let it through, but if it is not `extern'
3481 then it may cause an error message later. */
3482 ? (DECL_INITIAL (decl) != 0
3483 || !DECL_FILE_SCOPE_P (decl))
3484 /* An automatic variable with an incomplete type
3486 : !DECL_EXTERNAL (decl)))
3488 error ("storage size of %q+D isn%'t known", decl);
3489 TREE_TYPE (decl) = error_mark_node;
3492 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3493 && DECL_SIZE (decl) != 0)
3495 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3496 constant_expression_warning (DECL_SIZE (decl));
3499 error ("storage size of %q+D isn%'t constant", decl);
3500 TREE_TYPE (decl) = error_mark_node;
3504 if (TREE_USED (type))
3505 TREE_USED (decl) = 1;
3508 /* If this is a function and an assembler name is specified, reset DECL_RTL
3509 so we can give it its new name. Also, update built_in_decls if it
3510 was a normal built-in. */
3511 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3513 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3514 set_builtin_user_assembler_name (decl, asmspec);
3515 set_user_assembler_name (decl, asmspec);
3518 /* If #pragma weak was used, mark the decl weak now. */
3519 maybe_apply_pragma_weak (decl);
3521 /* Output the assembler code and/or RTL code for variables and functions,
3522 unless the type is an undefined structure or union.
3523 If not, it will get done when the type is completed. */
3525 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3527 /* Determine the ELF visibility. */
3528 if (TREE_PUBLIC (decl))
3529 c_determine_visibility (decl);
3531 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3532 if (c_dialect_objc ())
3533 objc_check_decl (decl);
3537 /* If this is not a static variable, issue a warning.
3538 It doesn't make any sense to give an ASMSPEC for an
3539 ordinary, non-register local variable. Historically,
3540 GCC has accepted -- but ignored -- the ASMSPEC in
3542 if (!DECL_FILE_SCOPE_P (decl)
3543 && TREE_CODE (decl) == VAR_DECL
3544 && !C_DECL_REGISTER (decl)
3545 && !TREE_STATIC (decl))
3546 warning (0, "ignoring asm-specifier for non-static local "
3547 "variable %q+D", decl);
3549 set_user_assembler_name (decl, asmspec);
3552 if (DECL_FILE_SCOPE_P (decl))
3554 if (DECL_INITIAL (decl) == NULL_TREE
3555 || DECL_INITIAL (decl) == error_mark_node)
3556 /* Don't output anything
3557 when a tentative file-scope definition is seen.
3558 But at end of compilation, do output code for them. */
3559 DECL_DEFER_OUTPUT (decl) = 1;
3560 rest_of_decl_compilation (decl, true, 0);
3564 /* In conjunction with an ASMSPEC, the `register'
3565 keyword indicates that we should place the variable
3566 in a particular register. */
3567 if (asmspec && C_DECL_REGISTER (decl))
3569 DECL_HARD_REGISTER (decl) = 1;
3570 /* This cannot be done for a structure with volatile
3571 fields, on which DECL_REGISTER will have been
3573 if (!DECL_REGISTER (decl))
3574 error ("cannot put object with volatile field into register");
3577 if (TREE_CODE (decl) != FUNCTION_DECL)
3579 /* If we're building a variable sized type, and we might be
3580 reachable other than via the top of the current binding
3581 level, then create a new BIND_EXPR so that we deallocate
3582 the object at the right time. */
3583 /* Note that DECL_SIZE can be null due to errors. */
3584 if (DECL_SIZE (decl)
3585 && !TREE_CONSTANT (DECL_SIZE (decl))
3586 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3589 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3590 TREE_SIDE_EFFECTS (bind) = 1;
3592 BIND_EXPR_BODY (bind) = push_stmt_list ();
3594 add_stmt (build_stmt (DECL_EXPR, decl));
3599 if (!DECL_FILE_SCOPE_P (decl))
3601 /* Recompute the RTL of a local array now
3602 if it used to be an incomplete type. */
3604 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3606 /* If we used it already as memory, it must stay in memory. */
3607 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3608 /* If it's still incomplete now, no init will save it. */
3609 if (DECL_SIZE (decl) == 0)
3610 DECL_INITIAL (decl) = 0;
3615 if (TREE_CODE (decl) == TYPE_DECL)
3617 if (!DECL_FILE_SCOPE_P (decl)
3618 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3619 add_stmt (build_stmt (DECL_EXPR, decl));
3621 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3624 /* At the end of a declaration, throw away any variable type sizes
3625 of types defined inside that declaration. There is no use
3626 computing them in the following function definition. */
3627 if (current_scope == file_scope)
3628 get_pending_sizes ();
3630 /* Install a cleanup (aka destructor) if one was given. */
3631 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3633 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3636 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3637 tree cleanup_decl = lookup_name (cleanup_id);
3640 /* Build "cleanup(&decl)" for the destructor. */
3641 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
3642 cleanup = build_tree_list (NULL_TREE, cleanup);
3643 cleanup = build_function_call (cleanup_decl, cleanup);
3645 /* Don't warn about decl unused; the cleanup uses it. */
3646 TREE_USED (decl) = 1;
3647 TREE_USED (cleanup_decl) = 1;
3649 /* Initialize EH, if we've been told to do so. */
3650 c_maybe_initialize_eh ();
3652 push_cleanup (decl, cleanup, false);
3657 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3660 grokparm (const struct c_parm *parm)
3662 tree attrs = parm->attrs;
3663 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3664 NULL, &attrs, NULL, NULL, DEPRECATED_NORMAL);
3666 decl_attributes (&decl, attrs, 0);
3671 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3672 and push that on the current scope. */
3675 push_parm_decl (const struct c_parm *parm)
3677 tree attrs = parm->attrs;
3680 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
3681 &attrs, NULL, NULL, DEPRECATED_NORMAL);
3682 decl_attributes (&decl, attrs, 0);
3684 decl = pushdecl (decl);
3686 finish_decl (decl, NULL_TREE, NULL_TREE);
3689 /* Mark all the parameter declarations to date as forward decls.
3690 Also diagnose use of this extension. */
3693 mark_forward_parm_decls (void)
3695 struct c_binding *b;
3697 if (pedantic && !current_scope->warned_forward_parm_decls)
3699 pedwarn (input_location, OPT_pedantic,
3700 "ISO C forbids forward parameter declarations");
3701 current_scope->warned_forward_parm_decls = true;
3704 for (b = current_scope->bindings; b; b = b->prev)
3705 if (TREE_CODE (b->decl) == PARM_DECL)
3706 TREE_ASM_WRITTEN (b->decl) = 1;
3709 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3710 literal, which may be an incomplete array type completed by the
3711 initializer; INIT is a CONSTRUCTOR that initializes the compound
3712 literal. NON_CONST is true if the initializers contain something
3713 that cannot occur in a constant expression. */
3716 build_compound_literal (tree type, tree init, bool non_const)
3718 /* We do not use start_decl here because we have a type, not a declarator;
3719 and do not use finish_decl because the decl should be stored inside
3720 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3725 if (type == error_mark_node)
3726 return error_mark_node;
3728 decl = build_decl (VAR_DECL, NULL_TREE, type);
3729 DECL_EXTERNAL (decl) = 0;
3730 TREE_PUBLIC (decl) = 0;
3731 TREE_STATIC (decl) = (current_scope == file_scope);
3732 DECL_CONTEXT (decl) = current_function_decl;
3733 TREE_USED (decl) = 1;
3734 TREE_TYPE (decl) = type;
3735 TREE_READONLY (decl) = TYPE_READONLY (type);
3736 store_init_value (decl, init);
3738 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3740 int failure = complete_array_type (&TREE_TYPE (decl),
3741 DECL_INITIAL (decl), true);
3742 gcc_assert (!failure);
3744 type = TREE_TYPE (decl);
3745 TREE_TYPE (DECL_INITIAL (decl)) = type;
3748 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3749 return error_mark_node;
3751 stmt = build_stmt (DECL_EXPR, decl);
3752 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3753 TREE_SIDE_EFFECTS (complit) = 1;
3755 layout_decl (decl, 0);
3757 if (TREE_STATIC (decl))
3759 /* This decl needs a name for the assembler output. */
3760 set_compound_literal_name (decl);
3761 DECL_DEFER_OUTPUT (decl) = 1;
3762 DECL_COMDAT (decl) = 1;
3763 DECL_ARTIFICIAL (decl) = 1;
3764 DECL_IGNORED_P (decl) = 1;
3766 rest_of_decl_compilation (decl, 1, 0);
3771 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
3772 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
3778 /* Determine whether TYPE is a structure with a flexible array member,
3779 or a union containing such a structure (possibly recursively). */
3782 flexible_array_type_p (tree type)
3785 switch (TREE_CODE (type))
3788 x = TYPE_FIELDS (type);
3791 while (TREE_CHAIN (x) != NULL_TREE)
3793 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3794 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3795 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3796 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3800 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3802 if (flexible_array_type_p (TREE_TYPE (x)))
3811 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3812 replacing with appropriate values if they are invalid. */
3814 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3817 unsigned int max_width;
3818 unsigned HOST_WIDE_INT w;
3819 const char *name = orig_name ? orig_name: _("<anonymous>");
3821 /* Detect and ignore out of range field width and process valid
3823 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3824 || TREE_CODE (*width) != INTEGER_CST)
3826 error ("bit-field %qs width not an integer constant", name);
3827 *width = integer_one_node;
3831 constant_expression_warning (*width);
3832 if (tree_int_cst_sgn (*width) < 0)
3834 error ("negative width in bit-field %qs", name);
3835 *width = integer_one_node;
3837 else if (integer_zerop (*width) && orig_name)
3839 error ("zero width for bit-field %qs", name);
3840 *width = integer_one_node;
3844 /* Detect invalid bit-field type. */
3845 if (TREE_CODE (*type) != INTEGER_TYPE
3846 && TREE_CODE (*type) != BOOLEAN_TYPE
3847 && TREE_CODE (*type) != ENUMERAL_TYPE)
3849 error ("bit-field %qs has invalid type", name);
3850 *type = unsigned_type_node;
3853 type_mv = TYPE_MAIN_VARIANT (*type);
3854 if (!in_system_header
3855 && type_mv != integer_type_node
3856 && type_mv != unsigned_type_node
3857 && type_mv != boolean_type_node)
3858 pedwarn (input_location, OPT_pedantic,
3859 "type of bit-field %qs is a GCC extension", name);
3861 max_width = TYPE_PRECISION (*type);
3863 if (0 < compare_tree_int (*width, max_width))
3865 error ("width of %qs exceeds its type", name);
3867 *width = build_int_cst (NULL_TREE, w);
3870 w = tree_low_cst (*width, 1);
3872 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3874 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3876 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3877 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3878 warning (0, "%qs is narrower than values of its type", name);
3884 /* Print warning about variable length array if necessary. */
3887 warn_variable_length_array (const char *name, tree size)
3889 int const_size = TREE_CONSTANT (size);
3891 if (!flag_isoc99 && pedantic && warn_vla != 0)
3896 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array %qs whose size "
3897 "can%'t be evaluated",
3900 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array whose size "
3901 "can%'t be evaluated");
3906 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array %qs",
3909 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array");
3912 else if (warn_vla > 0)
3918 "the size of array %qs can"
3919 "%'t be evaluated", name);
3922 "the size of array can %'t be evaluated");
3928 "variable length array %qs is used",
3932 "variable length array is used");
3937 /* Given declspecs and a declarator,
3938 determine the name and type of the object declared
3939 and construct a ..._DECL node for it.
3940 (In one case we can return a ..._TYPE node instead.
3941 For invalid input we sometimes return 0.)
3943 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3945 DECL_CONTEXT says which syntactic context this declaration is in:
3946 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3947 FUNCDEF for a function definition. Like NORMAL but a few different
3948 error messages in each case. Return value may be zero meaning
3949 this definition is too screwy to try to parse.
3950 PARM for a parameter declaration (either within a function prototype
3951 or before a function body). Make a PARM_DECL, or return void_type_node.
3952 TYPENAME if for a typename (in a cast or sizeof).
3953 Don't make a DECL node; just return the ..._TYPE node.
3954 FIELD for a struct or union field; make a FIELD_DECL.
3955 INITIALIZED is true if the decl has an initializer.
3956 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3957 representing the width of the bit-field.
3958 DECL_ATTRS points to the list of attributes that should be added to this
3959 decl. Any nested attributes that belong on the decl itself will be
3961 If EXPR is not NULL, any expressions that need to be evaluated as
3962 part of evaluating variably modified types will be stored in *EXPR.
3963 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
3964 set to indicate whether operands in *EXPR can be used in constant
3966 DEPRECATED_STATE is a deprecated_states value indicating whether
3967 deprecation warnings should be suppressed.
3969 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3970 It may also be so in the PARM case, for a prototype where the
3971 argument type is specified but not the name.
3973 This function is where the complicated C meanings of `static'
3974 and `extern' are interpreted. */
3977 grokdeclarator (const struct c_declarator *declarator,
3978 struct c_declspecs *declspecs,
3979 enum decl_context decl_context, bool initialized, tree *width,
3980 tree *decl_attrs, tree *expr, bool *expr_const_operands,
3981 enum deprecated_states deprecated_state)
3983 tree type = declspecs->type;
3984 bool threadp = declspecs->thread_p;
3985 enum c_storage_class storage_class = declspecs->storage_class;
3989 int type_quals = TYPE_UNQUALIFIED;
3990 const char *name, *orig_name;
3991 bool funcdef_flag = false;
3992 bool funcdef_syntax = false;
3993 int size_varies = 0;
3994 tree decl_attr = declspecs->decl_attr;
3995 int array_ptr_quals = TYPE_UNQUALIFIED;
3996 tree array_ptr_attrs = NULL_TREE;
3997 int array_parm_static = 0;
3998 bool array_parm_vla_unspec_p = false;
3999 tree returned_attrs = NULL_TREE;
4000 bool bitfield = width != NULL;
4002 struct c_arg_info *arg_info = 0;
4004 bool expr_const_operands_dummy;
4008 if (expr_const_operands == NULL)
4009 expr_const_operands = &expr_const_operands_dummy;
4011 *expr = declspecs->expr;
4012 *expr_const_operands = declspecs->expr_const_operands;
4014 if (decl_context == FUNCDEF)
4015 funcdef_flag = true, decl_context = NORMAL;
4017 /* Look inside a declarator for the name being declared
4018 and get it as a string, for an error message. */
4020 const struct c_declarator *decl = declarator;
4029 funcdef_syntax = (decl->kind == cdk_function);
4030 decl = decl->declarator;
4034 decl = decl->declarator;
4039 name = IDENTIFIER_POINTER (decl->u.id);
4051 /* A function definition's declarator must have the form of
4052 a function declarator. */
4054 if (funcdef_flag && !funcdef_syntax)
4057 /* If this looks like a function definition, make it one,
4058 even if it occurs where parms are expected.
4059 Then store_parm_decls will reject it and not use it as a parm. */
4060 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4061 decl_context = PARM;
4063 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4064 warn_deprecated_use (declspecs->type);
4066 if ((decl_context == NORMAL || decl_context == FIELD)
4067 && current_scope == file_scope
4068 && variably_modified_type_p (type, NULL_TREE))
4070 error ("variably modified %qs at file scope", name);
4071 type = integer_type_node;
4074 size_varies = C_TYPE_VARIABLE_SIZE (type);
4076 /* Diagnose defaulting to "int". */
4078 if (declspecs->default_int_p && !in_system_header)
4080 /* Issue a warning if this is an ISO C 99 program or if
4081 -Wreturn-type and this is a function, or if -Wimplicit;
4082 prefer the former warning since it is more explicit. */
4083 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4085 warn_about_return_type = 1;
4087 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int,
4088 "type defaults to %<int%> in declaration of %qs", name);
4091 /* Adjust the type if a bit-field is being declared,
4092 -funsigned-bitfields applied and the type is not explicitly
4094 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4095 && TREE_CODE (type) == INTEGER_TYPE)
4096 type = unsigned_type_for (type);
4098 /* Figure out the type qualifiers for the declaration. There are
4099 two ways a declaration can become qualified. One is something
4100 like `const int i' where the `const' is explicit. Another is
4101 something like `typedef const int CI; CI i' where the type of the
4102 declaration contains the `const'. A third possibility is that
4103 there is a type qualifier on the element type of a typedefed
4104 array type, in which case we should extract that qualifier so
4105 that c_apply_type_quals_to_decl receives the full list of
4106 qualifiers to work with (C90 is not entirely clear about whether
4107 duplicate qualifiers should be diagnosed in this case, but it
4108 seems most appropriate to do so). */
4109 element_type = strip_array_types (type);
4110 constp = declspecs->const_p + TYPE_READONLY (element_type);
4111 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4112 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4113 if (pedantic && !flag_isoc99)
4116 pedwarn (input_location, OPT_pedantic, "duplicate %<const%>");
4118 pedwarn (input_location, OPT_pedantic, "duplicate %<restrict%>");
4120 pedwarn (input_location, OPT_pedantic, "duplicate %<volatile%>");
4122 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4123 type = TYPE_MAIN_VARIANT (type);
4124 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4125 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4126 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4128 /* Warn about storage classes that are invalid for certain
4129 kinds of declarations (parameters, typenames, etc.). */
4133 || storage_class == csc_auto
4134 || storage_class == csc_register
4135 || storage_class == csc_typedef))
4137 if (storage_class == csc_auto)
4138 pedwarn (input_location,
4139 (current_scope == file_scope) ? 0 : OPT_pedantic,
4140 "function definition declared %<auto%>");
4141 if (storage_class == csc_register)
4142 error ("function definition declared %<register%>");
4143 if (storage_class == csc_typedef)
4144 error ("function definition declared %<typedef%>");
4146 error ("function definition declared %<__thread%>");
4148 if (storage_class == csc_auto
4149 || storage_class == csc_register
4150 || storage_class == csc_typedef)
4151 storage_class = csc_none;
4153 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4155 if (decl_context == PARM && storage_class == csc_register)
4159 switch (decl_context)
4162 error ("storage class specified for structure field %qs",
4166 error ("storage class specified for parameter %qs", name);
4169 error ("storage class specified for typename");
4172 storage_class = csc_none;
4176 else if (storage_class == csc_extern
4180 /* 'extern' with initialization is invalid if not at file scope. */
4181 if (current_scope == file_scope)
4183 /* It is fine to have 'extern const' when compiling at C
4184 and C++ intersection. */
4185 if (!(warn_cxx_compat && constp))
4186 warning (0, "%qs initialized and declared %<extern%>", name);
4189 error ("%qs has both %<extern%> and initializer", name);
4191 else if (current_scope == file_scope)
4193 if (storage_class == csc_auto)
4194 error ("file-scope declaration of %qs specifies %<auto%>", name);
4195 if (pedantic && storage_class == csc_register)
4196 pedwarn (input_location, OPT_pedantic,
4197 "file-scope declaration of %qs specifies %<register%>", name);
4201 if (storage_class == csc_extern && funcdef_flag)
4202 error ("nested function %qs declared %<extern%>", name);
4203 else if (threadp && storage_class == csc_none)
4205 error ("function-scope %qs implicitly auto and declared "
4212 /* Now figure out the structure of the declarator proper.
4213 Descend through it, creating more complex types, until we reach
4214 the declared identifier (or NULL_TREE, in an absolute declarator).
4215 At each stage we maintain an unqualified version of the type
4216 together with any qualifiers that should be applied to it with
4217 c_build_qualified_type; this way, array types including
4218 multidimensional array types are first built up in unqualified
4219 form and then the qualified form is created with
4220 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4222 while (declarator && declarator->kind != cdk_id)
4224 if (type == error_mark_node)
4226 declarator = declarator->declarator;
4230 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4231 a cdk_pointer (for *...),
4232 a cdk_function (for ...(...)),
4233 a cdk_attrs (for nested attributes),
4234 or a cdk_id (for the name being declared
4235 or the place in an absolute declarator
4236 where the name was omitted).
4237 For the last case, we have just exited the loop.
4239 At this point, TYPE is the type of elements of an array,
4240 or for a function to return, or for a pointer to point to.
4241 After this sequence of ifs, TYPE is the type of the
4242 array or function or pointer, and DECLARATOR has had its
4243 outermost layer removed. */
4245 if (array_ptr_quals != TYPE_UNQUALIFIED
4246 || array_ptr_attrs != NULL_TREE
4247 || array_parm_static)
4249 /* Only the innermost declarator (making a parameter be of
4250 array type which is converted to pointer type)
4251 may have static or type qualifiers. */
4252 error ("static or type qualifiers in non-parameter array declarator");
4253 array_ptr_quals = TYPE_UNQUALIFIED;
4254 array_ptr_attrs = NULL_TREE;
4255 array_parm_static = 0;
4258 switch (declarator->kind)
4262 /* A declarator with embedded attributes. */
4263 tree attrs = declarator->u.attrs;
4264 const struct c_declarator *inner_decl;
4266 declarator = declarator->declarator;
4267 inner_decl = declarator;
4268 while (inner_decl->kind == cdk_attrs)
4269 inner_decl = inner_decl->declarator;
4270 if (inner_decl->kind == cdk_id)
4271 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4272 else if (inner_decl->kind == cdk_function)
4273 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4274 else if (inner_decl->kind == cdk_array)
4275 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4276 returned_attrs = decl_attributes (&type,
4277 chainon (returned_attrs, attrs),
4283 tree itype = NULL_TREE;
4284 tree size = declarator->u.array.dimen;
4285 /* The index is a signed object `sizetype' bits wide. */
4286 tree index_type = c_common_signed_type (sizetype);
4288 array_ptr_quals = declarator->u.array.quals;
4289 array_ptr_attrs = declarator->u.array.attrs;
4290 array_parm_static = declarator->u.array.static_p;
4291 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4293 declarator = declarator->declarator;
4295 /* Check for some types that there cannot be arrays of. */
4297 if (VOID_TYPE_P (type))
4299 error ("declaration of %qs as array of voids", name);
4300 type = error_mark_node;
4303 if (TREE_CODE (type) == FUNCTION_TYPE)
4305 error ("declaration of %qs as array of functions", name);
4306 type = error_mark_node;
4309 if (pedantic && !in_system_header && flexible_array_type_p (type))
4310 pedwarn (input_location, OPT_pedantic,
4311 "invalid use of structure with flexible array member");
4313 if (size == error_mark_node)
4314 type = error_mark_node;
4316 if (type == error_mark_node)
4319 /* If size was specified, set ITYPE to a range-type for
4320 that size. Otherwise, ITYPE remains null. finish_decl
4321 may figure it out from an initial value. */
4325 bool size_maybe_const = true;
4326 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
4327 && !TREE_OVERFLOW (size));
4328 bool this_size_varies = false;
4330 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4332 STRIP_TYPE_NOPS (size);
4334 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4336 error ("size of array %qs has non-integer type", name);
4337 size = integer_one_node;
4340 size = c_fully_fold (size, false, &size_maybe_const);
4342 if (pedantic && size_maybe_const && integer_zerop (size))
4343 pedwarn (input_location, OPT_pedantic,
4344 "ISO C forbids zero-size array %qs", name);
4346 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
4348 constant_expression_warning (size);
4349 if (tree_int_cst_sgn (size) < 0)
4351 error ("size of array %qs is negative", name);
4352 size = integer_one_node;
4354 /* Handle a size folded to an integer constant but
4355 not an integer constant expression. */
4356 if (!size_int_const)
4358 /* If this is a file scope declaration of an
4359 ordinary identifier, this is invalid code;
4360 diagnosing it here and not subsequently
4361 treating the type as variable-length avoids
4362 more confusing diagnostics later. */
4363 if ((decl_context == NORMAL || decl_context == FIELD)
4364 && current_scope == file_scope)
4365 pedwarn (input_location, 0,
4366 "variably modified %qs at file scope", name);
4368 this_size_varies = size_varies = 1;
4369 warn_variable_length_array (orig_name, size);
4372 else if ((decl_context == NORMAL || decl_context == FIELD)
4373 && current_scope == file_scope)
4375 error ("variably modified %qs at file scope", name);
4376 size = integer_one_node;
4380 /* Make sure the array size remains visibly
4381 nonconstant even if it is (eg) a const variable
4382 with known value. */
4383 this_size_varies = size_varies = 1;
4384 warn_variable_length_array (orig_name, size);
4387 if (integer_zerop (size) && !this_size_varies)
4389 /* A zero-length array cannot be represented with
4390 an unsigned index type, which is what we'll
4391 get with build_index_type. Create an
4392 open-ended range instead. */
4393 itype = build_range_type (sizetype, size, NULL_TREE);
4397 /* Arrange for the SAVE_EXPR on the inside of the
4398 MINUS_EXPR, which allows the -1 to get folded
4399 with the +1 that happens when building TYPE_SIZE. */
4401 size = variable_size (size);
4402 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
4403 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
4404 integer_zero_node, size);
4406 /* Compute the maximum valid index, that is, size
4407 - 1. Do the calculation in index_type, so that
4408 if it is a variable the computations will be
4409 done in the proper mode. */
4410 itype = fold_build2 (MINUS_EXPR, index_type,
4411 convert (index_type, size),
4412 convert (index_type,
4415 /* If that overflowed, the array is too big. ???
4416 While a size of INT_MAX+1 technically shouldn't
4417 cause an overflow (because we subtract 1), the
4418 overflow is recorded during the conversion to
4419 index_type, before the subtraction. Handling
4420 this case seems like an unnecessary
4422 if (TREE_CODE (itype) == INTEGER_CST
4423 && TREE_OVERFLOW (itype))
4425 error ("size of array %qs is too large", name);
4426 type = error_mark_node;
4430 itype = build_index_type (itype);
4432 if (this_size_varies)
4435 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
4439 *expr_const_operands &= size_maybe_const;
4442 else if (decl_context == FIELD)
4444 bool flexible_array_member = false;
4445 if (array_parm_vla_unspec_p)
4446 /* Field names can in fact have function prototype
4447 scope so [*] is disallowed here through making
4448 the field variably modified, not through being
4449 something other than a declaration with function
4454 const struct c_declarator *t = declarator;
4455 while (t->kind == cdk_attrs)
4457 flexible_array_member = (t->kind == cdk_id);
4459 if (flexible_array_member
4460 && pedantic && !flag_isoc99 && !in_system_header)
4461 pedwarn (input_location, OPT_pedantic,
4462 "ISO C90 does not support flexible array members");
4464 /* ISO C99 Flexible array members are effectively
4465 identical to GCC's zero-length array extension. */
4466 if (flexible_array_member || array_parm_vla_unspec_p)
4467 itype = build_range_type (sizetype, size_zero_node,
4470 else if (decl_context == PARM)
4472 if (array_parm_vla_unspec_p)
4474 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4478 else if (decl_context == TYPENAME)
4480 if (array_parm_vla_unspec_p)
4483 warning (0, "%<[*]%> not in a declaration");
4484 /* We use this to avoid messing up with incomplete
4485 array types of the same type, that would
4486 otherwise be modified below. */
4487 itype = build_range_type (sizetype, size_zero_node,
4493 /* Complain about arrays of incomplete types. */
4494 if (!COMPLETE_TYPE_P (type))
4496 error ("array type has incomplete element type");
4497 type = error_mark_node;
4500 /* When itype is NULL, a shared incomplete array type is
4501 returned for all array of a given type. Elsewhere we
4502 make sure we don't complete that type before copying
4503 it, but here we want to make sure we don't ever
4504 modify the shared type, so we gcc_assert (itype)
4506 type = build_array_type (type, itype);
4508 if (type != error_mark_node)
4512 /* It is ok to modify type here even if itype is
4513 NULL: if size_varies, we're in a
4514 multi-dimensional array and the inner type has
4515 variable size, so the enclosing shared array type
4517 if (size && TREE_CODE (size) == INTEGER_CST)
4519 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4520 C_TYPE_VARIABLE_SIZE (type) = 1;
4523 /* The GCC extension for zero-length arrays differs from
4524 ISO flexible array members in that sizeof yields
4526 if (size && integer_zerop (size))
4529 TYPE_SIZE (type) = bitsize_zero_node;
4530 TYPE_SIZE_UNIT (type) = size_zero_node;
4532 if (array_parm_vla_unspec_p)
4535 /* The type is complete. C99 6.7.5.2p4 */
4536 TYPE_SIZE (type) = bitsize_zero_node;
4537 TYPE_SIZE_UNIT (type) = size_zero_node;
4541 if (decl_context != PARM
4542 && (array_ptr_quals != TYPE_UNQUALIFIED
4543 || array_ptr_attrs != NULL_TREE
4544 || array_parm_static))
4546 error ("static or type qualifiers in non-parameter array declarator");
4547 array_ptr_quals = TYPE_UNQUALIFIED;
4548 array_ptr_attrs = NULL_TREE;
4549 array_parm_static = 0;
4555 /* Say it's a definition only for the declarator closest
4556 to the identifier, apart possibly from some
4558 bool really_funcdef = false;
4562 const struct c_declarator *t = declarator->declarator;
4563 while (t->kind == cdk_attrs)
4565 really_funcdef = (t->kind == cdk_id);
4568 /* Declaring a function type. Make sure we have a valid
4569 type for the function to return. */
4570 if (type == error_mark_node)
4575 /* Warn about some types functions can't return. */
4576 if (TREE_CODE (type) == FUNCTION_TYPE)
4578 error ("%qs declared as function returning a function", name);
4579 type = integer_type_node;
4581 if (TREE_CODE (type) == ARRAY_TYPE)
4583 error ("%qs declared as function returning an array", name);
4584 type = integer_type_node;
4587 /* Construct the function type and go to the next
4588 inner layer of declarator. */
4589 arg_info = declarator->u.arg_info;
4590 arg_types = grokparms (arg_info, really_funcdef);
4592 put_pending_sizes (arg_info->pending_sizes);
4594 /* Type qualifiers before the return type of the function
4595 qualify the return type, not the function type. */
4598 /* Type qualifiers on a function return type are
4599 normally permitted by the standard but have no
4600 effect, so give a warning at -Wreturn-type.
4601 Qualifiers on a void return type are banned on
4602 function definitions in ISO C; GCC used to used
4603 them for noreturn functions. */
4604 if (VOID_TYPE_P (type) && really_funcdef)
4605 pedwarn (input_location, 0,
4606 "function definition has qualified void return type");
4608 warning (OPT_Wignored_qualifiers,
4609 "type qualifiers ignored on function return type");
4611 type = c_build_qualified_type (type, type_quals);
4613 type_quals = TYPE_UNQUALIFIED;
4615 type = build_function_type (type, arg_types);
4616 declarator = declarator->declarator;
4618 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4619 the formal parameter list of this FUNCTION_TYPE to point to
4620 the FUNCTION_TYPE node itself. */
4624 for (link = arg_info->tags;
4626 link = TREE_CHAIN (link))
4627 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4633 /* Merge any constancy or volatility into the target type
4636 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4638 pedwarn (input_location, OPT_pedantic,
4639 "ISO C forbids qualified function types");
4641 type = c_build_qualified_type (type, type_quals);
4644 /* When the pointed-to type involves components of variable size,
4645 care must be taken to ensure that the size evaluation code is
4646 emitted early enough to dominate all the possible later uses
4647 and late enough for the variables on which it depends to have
4650 This is expected to happen automatically when the pointed-to
4651 type has a name/declaration of it's own, but special attention
4652 is required if the type is anonymous.
4654 We handle the NORMAL and FIELD contexts here by attaching an
4655 artificial TYPE_DECL to such pointed-to type. This forces the
4656 sizes evaluation at a safe point and ensures it is not deferred
4657 until e.g. within a deeper conditional context.
4659 We expect nothing to be needed here for PARM or TYPENAME.
4660 Pushing a TYPE_DECL at this point for TYPENAME would actually
4661 be incorrect, as we might be in the middle of an expression
4662 with side effects on the pointed-to type size "arguments" prior
4663 to the pointer declaration point and the fake TYPE_DECL in the
4664 enclosing context would force the size evaluation prior to the
4667 if (!TYPE_NAME (type)
4668 && (decl_context == NORMAL || decl_context == FIELD)
4669 && variably_modified_type_p (type, NULL_TREE))
4671 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4672 DECL_ARTIFICIAL (decl) = 1;
4674 finish_decl (decl, NULL_TREE, NULL_TREE);
4675 TYPE_NAME (type) = decl;
4678 type = build_pointer_type (type);
4680 /* Process type qualifiers (such as const or volatile)
4681 that were given inside the `*'. */
4682 type_quals = declarator->u.pointer_quals;
4684 declarator = declarator->declarator;
4691 *decl_attrs = chainon (returned_attrs, *decl_attrs);
4693 /* Now TYPE has the actual type, apart from any qualifiers in
4696 /* Check the type and width of a bit-field. */
4698 check_bitfield_type_and_width (&type, width, orig_name);
4700 /* Did array size calculations overflow? */
4702 if (TREE_CODE (type) == ARRAY_TYPE
4703 && COMPLETE_TYPE_P (type)
4704 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4705 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4707 error ("size of array %qs is too large", name);
4708 /* If we proceed with the array type as it is, we'll eventually
4709 crash in tree_low_cst(). */
4710 type = error_mark_node;
4713 /* If this is declaring a typedef name, return a TYPE_DECL. */
4715 if (storage_class == csc_typedef)
4718 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4720 pedwarn (input_location, OPT_pedantic,
4721 "ISO C forbids qualified function types");
4723 type = c_build_qualified_type (type, type_quals);
4724 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4725 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4726 if (declspecs->explicit_signed_p)
4727 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4728 if (declspecs->inline_p)
4729 pedwarn (input_location, 0,"typedef %q+D declared %<inline%>", decl);
4733 /* If this is a type name (such as, in a cast or sizeof),
4734 compute the type and return it now. */
4736 if (decl_context == TYPENAME)
4738 /* Note that the grammar rejects storage classes in typenames
4740 gcc_assert (storage_class == csc_none && !threadp
4741 && !declspecs->inline_p);
4742 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4744 pedwarn (input_location, OPT_pedantic,
4745 "ISO C forbids const or volatile function types");
4747 type = c_build_qualified_type (type, type_quals);
4751 if (pedantic && decl_context == FIELD
4752 && variably_modified_type_p (type, NULL_TREE))
4755 pedwarn (input_location, OPT_pedantic,
4756 "a member of a structure or union cannot have a variably modified type");
4759 /* Aside from typedefs and type names (handle above),
4760 `void' at top level (not within pointer)
4761 is allowed only in public variables.
4762 We don't complain about parms either, but that is because
4763 a better error message can be made later. */
4765 if (VOID_TYPE_P (type) && decl_context != PARM
4766 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4767 && (storage_class == csc_extern
4768 || (current_scope == file_scope
4769 && !(storage_class == csc_static
4770 || storage_class == csc_register)))))
4772 error ("variable or field %qs declared void", name);
4773 type = integer_type_node;
4776 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4777 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4782 if (decl_context == PARM)
4786 /* A parameter declared as an array of T is really a pointer to T.
4787 One declared as a function is really a pointer to a function. */
4789 if (TREE_CODE (type) == ARRAY_TYPE)
4791 /* Transfer const-ness of array into that of type pointed to. */
4792 type = TREE_TYPE (type);
4794 type = c_build_qualified_type (type, type_quals);
4795 type = build_pointer_type (type);
4796 type_quals = array_ptr_quals;
4798 type = c_build_qualified_type (type, type_quals);
4800 /* We don't yet implement attributes in this context. */
4801 if (array_ptr_attrs != NULL_TREE)
4802 warning (OPT_Wattributes,
4803 "attributes in parameter array declarator ignored");
4807 else if (TREE_CODE (type) == FUNCTION_TYPE)
4810 pedwarn (input_location, OPT_pedantic,
4811 "ISO C forbids qualified function types");
4813 type = c_build_qualified_type (type, type_quals);
4814 type = build_pointer_type (type);
4815 type_quals = TYPE_UNQUALIFIED;
4817 else if (type_quals)
4818 type = c_build_qualified_type (type, type_quals);
4820 decl = build_decl (PARM_DECL, declarator->u.id, type);
4821 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4823 C_DECL_VARIABLE_SIZE (decl) = 1;
4825 /* Compute the type actually passed in the parmlist,
4826 for the case where there is no prototype.
4827 (For example, shorts and chars are passed as ints.)
4828 When there is a prototype, this is overridden later. */
4830 if (type == error_mark_node)
4831 promoted_type = type;
4833 promoted_type = c_type_promotes_to (type);
4835 DECL_ARG_TYPE (decl) = promoted_type;
4836 if (declspecs->inline_p)
4837 pedwarn (input_location, 0, "parameter %q+D declared %<inline%>", decl);
4839 else if (decl_context == FIELD)
4841 /* Note that the grammar rejects storage classes in typenames
4843 gcc_assert (storage_class == csc_none && !threadp
4844 && !declspecs->inline_p);
4846 /* Structure field. It may not be a function. */
4848 if (TREE_CODE (type) == FUNCTION_TYPE)
4850 error ("field %qs declared as a function", name);
4851 type = build_pointer_type (type);
4853 else if (TREE_CODE (type) != ERROR_MARK
4854 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4856 error ("field %qs has incomplete type", name);
4857 type = error_mark_node;
4859 type = c_build_qualified_type (type, type_quals);
4860 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4861 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4862 DECL_NONADDRESSABLE_P (decl) = bitfield;
4863 if (bitfield && !declarator->u.id)
4864 TREE_NO_WARNING (decl) = 1;
4867 C_DECL_VARIABLE_SIZE (decl) = 1;
4869 else if (TREE_CODE (type) == FUNCTION_TYPE)
4871 if (storage_class == csc_register || threadp)
4873 error ("invalid storage class for function %qs", name);
4875 else if (current_scope != file_scope)
4877 /* Function declaration not at file scope. Storage
4878 classes other than `extern' are not allowed, C99
4879 6.7.1p5, and `extern' makes no difference. However,
4880 GCC allows 'auto', perhaps with 'inline', to support
4881 nested functions. */
4882 if (storage_class == csc_auto)
4883 pedwarn (input_location, OPT_pedantic, "invalid storage class for function %qs", name);
4884 else if (storage_class == csc_static)
4886 error ("invalid storage class for function %qs", name);
4888 storage_class = declspecs->storage_class = csc_none;
4894 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4895 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4896 decl = build_decl_attribute_variant (decl, decl_attr);
4898 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4899 pedwarn (input_location, OPT_pedantic,
4900 "ISO C forbids qualified function types");
4902 /* GNU C interprets a volatile-qualified function type to indicate
4903 that the function does not return. */
4904 if ((type_quals & TYPE_QUAL_VOLATILE)
4905 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4906 warning (0, "%<noreturn%> function returns non-void value");
4908 /* Every function declaration is an external reference
4909 (DECL_EXTERNAL) except for those which are not at file
4910 scope and are explicitly declared "auto". This is
4911 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4912 GCC to signify a forward declaration of a nested function. */
4913 if (storage_class == csc_auto && current_scope != file_scope)
4914 DECL_EXTERNAL (decl) = 0;
4915 /* In C99, a function which is declared 'inline' with 'extern'
4916 is not an external reference (which is confusing). It
4917 means that the later definition of the function must be output
4918 in this file, C99 6.7.4p6. In GNU C89, a function declared
4919 'extern inline' is an external reference. */
4920 else if (declspecs->inline_p && storage_class != csc_static)
4921 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
4922 == flag_gnu89_inline);
4924 DECL_EXTERNAL (decl) = !initialized;
4926 /* Record absence of global scope for `static' or `auto'. */
4928 = !(storage_class == csc_static || storage_class == csc_auto);
4930 /* For a function definition, record the argument information
4931 block where store_parm_decls will look for it. */
4933 current_function_arg_info = arg_info;
4935 if (declspecs->default_int_p)
4936 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4938 /* Record presence of `inline', if it is reasonable. */
4939 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4941 if (declspecs->inline_p)
4942 pedwarn (input_location, 0, "cannot inline function %<main%>");
4944 else if (declspecs->inline_p)
4945 /* Record that the function is declared `inline'. */
4946 DECL_DECLARED_INLINE_P (decl) = 1;
4950 /* It's a variable. */
4951 /* An uninitialized decl with `extern' is a reference. */
4952 int extern_ref = !initialized && storage_class == csc_extern;
4954 type = c_build_qualified_type (type, type_quals);
4956 /* C99 6.2.2p7: It is invalid (compile-time undefined
4957 behavior) to create an 'extern' declaration for a
4958 variable if there is a global declaration that is
4959 'static' and the global declaration is not visible.
4960 (If the static declaration _is_ currently visible,
4961 the 'extern' declaration is taken to refer to that decl.) */
4962 if (extern_ref && current_scope != file_scope)
4964 tree global_decl = identifier_global_value (declarator->u.id);
4965 tree visible_decl = lookup_name (declarator->u.id);
4968 && global_decl != visible_decl
4969 && TREE_CODE (global_decl) == VAR_DECL
4970 && !TREE_PUBLIC (global_decl))
4971 error ("variable previously declared %<static%> redeclared "
4975 decl = build_decl (VAR_DECL, declarator->u.id, type);
4976 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4978 C_DECL_VARIABLE_SIZE (decl) = 1;
4980 if (declspecs->inline_p)
4981 pedwarn (input_location, 0, "variable %q+D declared %<inline%>", decl);
4983 /* At file scope, an initialized extern declaration may follow
4984 a static declaration. In that case, DECL_EXTERNAL will be
4985 reset later in start_decl. */
4986 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4988 /* At file scope, the presence of a `static' or `register' storage
4989 class specifier, or the absence of all storage class specifiers
4990 makes this declaration a definition (perhaps tentative). Also,
4991 the absence of `static' makes it public. */
4992 if (current_scope == file_scope)
4994 TREE_PUBLIC (decl) = storage_class != csc_static;
4995 TREE_STATIC (decl) = !extern_ref;
4997 /* Not at file scope, only `static' makes a static definition. */
5000 TREE_STATIC (decl) = (storage_class == csc_static);
5001 TREE_PUBLIC (decl) = extern_ref;
5005 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
5008 if (storage_class == csc_extern
5009 && variably_modified_type_p (type, NULL_TREE))
5012 error ("object with variably modified type must have no linkage");
5015 /* Record `register' declaration for warnings on &
5016 and in case doing stupid register allocation. */
5018 if (storage_class == csc_register)
5020 C_DECL_REGISTER (decl) = 1;
5021 DECL_REGISTER (decl) = 1;
5024 /* Record constancy and volatility. */
5025 c_apply_type_quals_to_decl (type_quals, decl);
5027 /* If a type has volatile components, it should be stored in memory.
5028 Otherwise, the fact that those components are volatile
5029 will be ignored, and would even crash the compiler.
5030 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
5031 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
5032 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
5033 || TREE_CODE (decl) == RESULT_DECL))
5035 /* It is not an error for a structure with volatile fields to
5036 be declared register, but reset DECL_REGISTER since it
5037 cannot actually go in a register. */
5038 int was_reg = C_DECL_REGISTER (decl);
5039 C_DECL_REGISTER (decl) = 0;
5040 DECL_REGISTER (decl) = 0;
5041 c_mark_addressable (decl);
5042 C_DECL_REGISTER (decl) = was_reg;
5045 /* This is the earliest point at which we might know the assembler
5046 name of a variable. Thus, if it's known before this, die horribly. */
5047 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
5053 /* Decode the parameter-list info for a function type or function definition.
5054 The argument is the value returned by `get_parm_info' (or made in c-parse.c
5055 if there is an identifier list instead of a parameter decl list).
5056 These two functions are separate because when a function returns
5057 or receives functions then each is called multiple times but the order
5058 of calls is different. The last call to `grokparms' is always the one
5059 that contains the formal parameter names of a function definition.
5061 Return a list of arg types to use in the FUNCTION_TYPE for this function.
5063 FUNCDEF_FLAG is true for a function definition, false for
5064 a mere declaration. A nonempty identifier-list gets an error message
5065 when FUNCDEF_FLAG is false. */
5068 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
5070 tree arg_types = arg_info->types;
5072 if (funcdef_flag && arg_info->had_vla_unspec)
5074 /* A function definition isn't function prototype scope C99 6.2.1p4. */
5076 error ("%<[*]%> not allowed in other than function prototype scope");
5079 if (arg_types == 0 && !funcdef_flag && !in_system_header)
5080 warning (OPT_Wstrict_prototypes,
5081 "function declaration isn%'t a prototype");
5083 if (arg_types == error_mark_node)
5084 return 0; /* don't set TYPE_ARG_TYPES in this case */
5086 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5089 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
5091 arg_info->parms = arg_info->types;
5092 arg_info->types = 0;
5097 tree parm, type, typelt;
5098 unsigned int parmno;
5100 /* If there is a parameter of incomplete type in a definition,
5101 this is an error. In a declaration this is valid, and a
5102 struct or union type may be completed later, before any calls
5103 or definition of the function. In the case where the tag was
5104 first declared within the parameter list, a warning has
5105 already been given. If a parameter has void type, then
5106 however the function cannot be defined or called, so
5109 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5111 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5113 type = TREE_VALUE (typelt);
5114 if (type == error_mark_node)
5117 if (!COMPLETE_TYPE_P (type))
5121 if (DECL_NAME (parm))
5122 error ("parameter %u (%q+D) has incomplete type",
5125 error ("%Jparameter %u has incomplete type",
5128 TREE_VALUE (typelt) = error_mark_node;
5129 TREE_TYPE (parm) = error_mark_node;
5131 else if (VOID_TYPE_P (type))
5133 if (DECL_NAME (parm))
5134 warning (0, "parameter %u (%q+D) has void type",
5137 warning (0, "%Jparameter %u has void type",
5142 if (DECL_NAME (parm) && TREE_USED (parm))
5143 warn_if_shadowing (parm);
5149 /* Take apart the current scope and return a c_arg_info structure with
5150 info on a parameter list just parsed.
5152 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5154 ELLIPSIS being true means the argument list ended in '...' so don't
5155 append a sentinel (void_list_node) to the end of the type-list. */
5158 get_parm_info (bool ellipsis)
5160 struct c_binding *b = current_scope->bindings;
5161 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5168 static bool explained_incomplete_types = false;
5169 bool gave_void_only_once_err = false;
5171 arg_info->parms = 0;
5173 arg_info->types = 0;
5174 arg_info->others = 0;
5175 arg_info->pending_sizes = 0;
5176 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5178 /* The bindings in this scope must not get put into a block.
5179 We will take care of deleting the binding nodes. */
5180 current_scope->bindings = 0;
5182 /* This function is only called if there was *something* on the
5186 /* A parameter list consisting solely of 'void' indicates that the
5187 function takes no arguments. But if the 'void' is qualified
5188 (by 'const' or 'volatile'), or has a storage class specifier
5189 ('register'), then the behavior is undefined; issue an error.
5190 Typedefs for 'void' are OK (see DR#157). */
5191 if (b->prev == 0 /* one binding */
5192 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5193 && !DECL_NAME (b->decl) /* anonymous */
5194 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5196 if (TREE_THIS_VOLATILE (b->decl)
5197 || TREE_READONLY (b->decl)
5198 || C_DECL_REGISTER (b->decl))
5199 error ("%<void%> as only parameter may not be qualified");
5201 /* There cannot be an ellipsis. */
5203 error ("%<void%> must be the only parameter");
5205 arg_info->types = void_list_node;
5210 types = void_list_node;
5212 /* Break up the bindings list into parms, tags, types, and others;
5213 apply sanity checks; purge the name-to-decl bindings. */
5216 tree decl = b->decl;
5217 tree type = TREE_TYPE (decl);
5218 const char *keyword;
5220 switch (TREE_CODE (decl))
5225 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5226 I_SYMBOL_BINDING (b->id) = b->shadowed;
5229 /* Check for forward decls that never got their actual decl. */
5230 if (TREE_ASM_WRITTEN (decl))
5231 error ("parameter %q+D has just a forward declaration", decl);
5232 /* Check for (..., void, ...) and issue an error. */
5233 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5235 if (!gave_void_only_once_err)
5237 error ("%<void%> must be the only parameter");
5238 gave_void_only_once_err = true;
5243 /* Valid parameter, add it to the list. */
5244 TREE_CHAIN (decl) = parms;
5247 /* Since there is a prototype, args are passed in their
5248 declared types. The back end may override this later. */
5249 DECL_ARG_TYPE (decl) = type;
5250 types = tree_cons (0, type, types);
5254 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5255 case UNION_TYPE: keyword = "union"; goto tag;
5256 case RECORD_TYPE: keyword = "struct"; goto tag;
5258 /* Types may not have tag-names, in which case the type
5259 appears in the bindings list with b->id NULL. */
5262 gcc_assert (I_TAG_BINDING (b->id) == b);
5263 I_TAG_BINDING (b->id) = b->shadowed;
5266 /* Warn about any struct, union or enum tags defined in a
5267 parameter list. The scope of such types is limited to
5268 the parameter list, which is rarely if ever desirable
5269 (it's impossible to call such a function with type-
5270 correct arguments). An anonymous union parm type is
5271 meaningful as a GNU extension, so don't warn for that. */
5272 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5275 /* The %s will be one of 'struct', 'union', or 'enum'. */
5276 warning (0, "%<%s %E%> declared inside parameter list",
5279 /* The %s will be one of 'struct', 'union', or 'enum'. */
5280 warning (0, "anonymous %s declared inside parameter list",
5283 if (!explained_incomplete_types)
5285 warning (0, "its scope is only this definition or declaration,"
5286 " which is probably not what you want");
5287 explained_incomplete_types = true;
5291 tags = tree_cons (b->id, decl, tags);
5297 /* CONST_DECLs appear here when we have an embedded enum,
5298 and TYPE_DECLs appear here when we have an embedded struct
5299 or union. No warnings for this - we already warned about the
5300 type itself. FUNCTION_DECLs appear when there is an implicit
5301 function declaration in the parameter list. */
5303 TREE_CHAIN (decl) = others;
5308 /* error_mark_node appears here when we have an undeclared
5309 variable. Just throw it away. */
5312 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5313 I_SYMBOL_BINDING (b->id) = b->shadowed;
5317 /* Other things that might be encountered. */
5324 b = free_binding_and_advance (b);
5327 arg_info->parms = parms;
5328 arg_info->tags = tags;
5329 arg_info->types = types;
5330 arg_info->others = others;
5331 arg_info->pending_sizes = get_pending_sizes ();
5335 /* Get the struct, enum or union (CODE says which) with tag NAME.
5336 Define the tag as a forward-reference if it is not defined.
5337 Return a c_typespec structure for the type specifier. */
5340 parser_xref_tag (enum tree_code code, tree name)
5342 struct c_typespec ret;
5345 ret.expr = NULL_TREE;
5346 ret.expr_const_operands = true;
5348 /* If a cross reference is requested, look up the type
5349 already defined for this tag and return it. */
5351 ref = lookup_tag (code, name, 0);
5352 /* If this is the right type of tag, return what we found.
5353 (This reference will be shadowed by shadow_tag later if appropriate.)
5354 If this is the wrong type of tag, do not return it. If it was the
5355 wrong type in the same scope, we will have had an error
5356 message already; if in a different scope and declaring
5357 a name, pending_xref_error will give an error message; but if in a
5358 different scope and not declaring a name, this tag should
5359 shadow the previous declaration of a different type of tag, and
5360 this would not work properly if we return the reference found.
5361 (For example, with "struct foo" in an outer scope, "union foo;"
5362 must shadow that tag with a new one of union type.) */
5363 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5364 if (ref && TREE_CODE (ref) == code)
5370 /* If no such tag is yet defined, create a forward-reference node
5371 and record it as the "definition".
5372 When a real declaration of this type is found,
5373 the forward-reference will be altered into a real type. */
5375 ref = make_node (code);
5376 if (code == ENUMERAL_TYPE)
5378 /* Give the type a default layout like unsigned int
5379 to avoid crashing if it does not get defined. */
5380 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
5381 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5382 TYPE_USER_ALIGN (ref) = 0;
5383 TYPE_UNSIGNED (ref) = 1;
5384 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5385 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5386 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5389 pushtag (name, ref);
5395 /* Get the struct, enum or union (CODE says which) with tag NAME.
5396 Define the tag as a forward-reference if it is not defined.
5397 Return a tree for the type. */
5400 xref_tag (enum tree_code code, tree name)
5402 return parser_xref_tag (code, name).spec;
5405 /* Make sure that the tag NAME is defined *in the current scope*
5406 at least as a forward reference.
5407 CODE says which kind of tag NAME ought to be. */
5410 start_struct (enum tree_code code, tree name)
5412 /* If there is already a tag defined at this scope
5413 (as a forward reference), just return it. */
5418 ref = lookup_tag (code, name, 1);
5419 if (ref && TREE_CODE (ref) == code)
5421 if (TYPE_SIZE (ref))
5423 if (code == UNION_TYPE)
5424 error ("redefinition of %<union %E%>", name);
5426 error ("redefinition of %<struct %E%>", name);
5427 /* Don't create structures using a name already in use. */
5430 else if (C_TYPE_BEING_DEFINED (ref))
5432 if (code == UNION_TYPE)
5433 error ("nested redefinition of %<union %E%>", name);
5435 error ("nested redefinition of %<struct %E%>", name);
5436 /* Don't create structures that contain themselves. */
5441 /* Otherwise create a forward-reference just so the tag is in scope. */
5443 if (ref == NULL_TREE || TREE_CODE (ref) != code)
5445 ref = make_node (code);
5446 pushtag (name, ref);
5449 C_TYPE_BEING_DEFINED (ref) = 1;
5450 TYPE_PACKED (ref) = flag_pack_struct;
5454 /* Process the specs, declarator and width (NULL if omitted)
5455 of a structure component, returning a FIELD_DECL node.
5456 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5457 DECL_ATTRS is as for grokdeclarator.
5459 LOC is the location of the structure component.
5461 This is done during the parsing of the struct declaration.
5462 The FIELD_DECL nodes are chained together and the lot of them
5463 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5466 grokfield (location_t loc,
5467 struct c_declarator *declarator, struct c_declspecs *declspecs,
5468 tree width, tree *decl_attrs)
5472 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5473 && width == NULL_TREE)
5475 /* This is an unnamed decl.
5477 If we have something of the form "union { list } ;" then this
5478 is the anonymous union extension. Similarly for struct.
5480 If this is something of the form "struct foo;", then
5481 If MS extensions are enabled, this is handled as an
5483 Otherwise this is a forward declaration of a structure tag.
5485 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5486 If MS extensions are enabled and foo names a structure, then
5487 again this is an anonymous struct.
5488 Otherwise this is an error.
5490 Oh what a horrid tangled web we weave. I wonder if MS consciously
5491 took this from Plan 9 or if it was an accident of implementation
5492 that took root before someone noticed the bug... */
5494 tree type = declspecs->type;
5495 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5496 || TREE_CODE (type) == UNION_TYPE);
5500 && (flag_ms_extensions || !declspecs->typedef_p))
5502 if (flag_ms_extensions)
5506 else if (TYPE_NAME (type) == NULL)
5513 pedwarn (loc, 0, "declaration does not declare anything");
5516 pedwarn (loc, OPT_pedantic, "ISO C doesn%'t support unnamed structs/unions");
5519 value = grokdeclarator (declarator, declspecs, FIELD, false,
5520 width ? &width : NULL, decl_attrs, NULL, NULL,
5523 finish_decl (value, NULL_TREE, NULL_TREE);
5524 DECL_INITIAL (value) = width;
5529 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5530 the list such that this does not present a problem later. */
5533 detect_field_duplicates (tree fieldlist)
5538 /* First, see if there are more than "a few" fields.
5539 This is trivially true if there are zero or one fields. */
5542 x = TREE_CHAIN (fieldlist);
5548 } while (timeout > 0 && x);
5550 /* If there were "few" fields, avoid the overhead of allocating
5551 a hash table. Instead just do the nested traversal thing. */
5554 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5557 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5558 if (DECL_NAME (y) == DECL_NAME (x))
5560 error ("duplicate member %q+D", x);
5561 DECL_NAME (x) = NULL_TREE;
5567 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5570 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5571 if ((y = DECL_NAME (x)) != 0)
5573 slot = htab_find_slot (htab, y, INSERT);
5576 error ("duplicate member %q+D", x);
5577 DECL_NAME (x) = NULL_TREE;
5586 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5587 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5588 ATTRIBUTES are attributes to be applied to the structure. */
5591 finish_struct (tree t, tree fieldlist, tree attributes)
5594 bool toplevel = file_scope == current_scope;
5595 int saw_named_field;
5597 /* If this type was previously laid out as a forward reference,
5598 make sure we lay it out again. */
5602 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5606 for (x = fieldlist; x; x = TREE_CHAIN (x))
5607 if (DECL_NAME (x) != 0)
5612 if (TREE_CODE (t) == UNION_TYPE)
5615 pedwarn (input_location, OPT_pedantic, "union has no named members");
5617 pedwarn (input_location, OPT_pedantic, "union has no members");
5622 pedwarn (input_location, OPT_pedantic, "struct has no named members");
5624 pedwarn (input_location, OPT_pedantic, "struct has no members");
5629 /* Install struct as DECL_CONTEXT of each field decl.
5630 Also process specified field sizes, found in the DECL_INITIAL,
5631 storing 0 there after the type has been changed to precision equal
5632 to its width, rather than the precision of the specified standard
5633 type. (Correct layout requires the original type to have been preserved
5636 saw_named_field = 0;
5637 for (x = fieldlist; x; x = TREE_CHAIN (x))
5639 if (TREE_TYPE (x) == error_mark_node)
5642 DECL_CONTEXT (x) = t;
5644 /* If any field is const, the structure type is pseudo-const. */
5645 if (TREE_READONLY (x))
5646 C_TYPE_FIELDS_READONLY (t) = 1;
5649 /* A field that is pseudo-const makes the structure likewise. */
5650 tree t1 = TREE_TYPE (x);
5651 while (TREE_CODE (t1) == ARRAY_TYPE)
5652 t1 = TREE_TYPE (t1);
5653 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5654 && C_TYPE_FIELDS_READONLY (t1))
5655 C_TYPE_FIELDS_READONLY (t) = 1;
5658 /* Any field that is volatile means variables of this type must be
5659 treated in some ways as volatile. */
5660 if (TREE_THIS_VOLATILE (x))
5661 C_TYPE_FIELDS_VOLATILE (t) = 1;
5663 /* Any field of nominal variable size implies structure is too. */
5664 if (C_DECL_VARIABLE_SIZE (x))
5665 C_TYPE_VARIABLE_SIZE (t) = 1;
5667 if (DECL_INITIAL (x))
5669 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5670 DECL_SIZE (x) = bitsize_int (width);
5671 DECL_BIT_FIELD (x) = 1;
5672 SET_DECL_C_BIT_FIELD (x);
5676 && (DECL_BIT_FIELD (x)
5677 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
5678 DECL_PACKED (x) = 1;
5680 /* Detect flexible array member in an invalid context. */
5681 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5682 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5683 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5684 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5686 if (TREE_CODE (t) == UNION_TYPE)
5688 error ("%Jflexible array member in union", x);
5689 TREE_TYPE (x) = error_mark_node;
5691 else if (TREE_CHAIN (x) != NULL_TREE)
5693 error ("%Jflexible array member not at end of struct", x);
5694 TREE_TYPE (x) = error_mark_node;
5696 else if (!saw_named_field)
5698 error ("%Jflexible array member in otherwise empty struct", x);
5699 TREE_TYPE (x) = error_mark_node;
5703 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5704 && flexible_array_type_p (TREE_TYPE (x)))
5705 pedwarn (input_location, OPT_pedantic,
5706 "%Jinvalid use of structure with flexible array member", x);
5709 saw_named_field = 1;
5712 detect_field_duplicates (fieldlist);
5714 /* Now we have the nearly final fieldlist. Record it,
5715 then lay out the structure or union (including the fields). */
5717 TYPE_FIELDS (t) = fieldlist;
5721 /* Give bit-fields their proper types. */
5723 tree *fieldlistp = &fieldlist;
5725 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5726 && TREE_TYPE (*fieldlistp) != error_mark_node)
5728 unsigned HOST_WIDE_INT width
5729 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5730 tree type = TREE_TYPE (*fieldlistp);
5731 if (width != TYPE_PRECISION (type))
5733 TREE_TYPE (*fieldlistp)
5734 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5735 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5737 DECL_INITIAL (*fieldlistp) = 0;
5740 fieldlistp = &TREE_CHAIN (*fieldlistp);
5743 /* Now we have the truly final field list.
5744 Store it in this type and in the variants. */
5746 TYPE_FIELDS (t) = fieldlist;
5748 /* If there are lots of fields, sort so we can look through them fast.
5749 We arbitrarily consider 16 or more elts to be "a lot". */
5754 for (x = fieldlist; x; x = TREE_CHAIN (x))
5756 if (len > 15 || DECL_NAME (x) == NULL)
5764 struct lang_type *space;
5765 struct sorted_fields_type *space2;
5767 len += list_length (x);
5769 /* Use the same allocation policy here that make_node uses, to
5770 ensure that this lives as long as the rest of the struct decl.
5771 All decls in an inline function need to be saved. */
5773 space = GGC_CNEW (struct lang_type);
5774 space2 = GGC_NEWVAR (struct sorted_fields_type,
5775 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5779 field_array = &space2->elts[0];
5780 for (x = fieldlist; x; x = TREE_CHAIN (x))
5782 field_array[len++] = x;
5784 /* If there is anonymous struct or union, break out of the loop. */
5785 if (DECL_NAME (x) == NULL)
5788 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5791 TYPE_LANG_SPECIFIC (t) = space;
5792 TYPE_LANG_SPECIFIC (t)->s->len = len;
5793 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5794 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5799 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5801 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5802 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5803 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5804 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5805 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5808 /* If this was supposed to be a transparent union, but we can't
5809 make it one, warn and turn off the flag. */
5810 if (TREE_CODE (t) == UNION_TYPE
5811 && TYPE_TRANSPARENT_UNION (t)
5812 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5814 TYPE_TRANSPARENT_UNION (t) = 0;
5815 warning (0, "union cannot be made transparent");
5818 /* If this structure or union completes the type of any previous
5819 variable declaration, lay it out and output its rtl. */
5820 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5824 tree decl = TREE_VALUE (x);
5825 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5826 layout_array_type (TREE_TYPE (decl));
5827 if (TREE_CODE (decl) != TYPE_DECL)
5829 layout_decl (decl, 0);
5830 if (c_dialect_objc ())
5831 objc_check_decl (decl);
5832 rest_of_decl_compilation (decl, toplevel, 0);
5837 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5839 /* Finish debugging output for this type. */
5840 rest_of_type_compilation (t, toplevel);
5842 /* If we're inside a function proper, i.e. not file-scope and not still
5843 parsing parameters, then arrange for the size of a variable sized type
5845 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
5846 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5851 /* Lay out the type T, and its element type, and so on. */
5854 layout_array_type (tree t)
5856 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5857 layout_array_type (TREE_TYPE (t));
5861 /* Begin compiling the definition of an enumeration type.
5862 NAME is its name (or null if anonymous).
5863 Returns the type object, as yet incomplete.
5864 Also records info about it so that build_enumerator
5865 may be used to declare the individual values as they are read. */
5868 start_enum (struct c_enum_contents *the_enum, tree name)
5872 /* If this is the real definition for a previous forward reference,
5873 fill in the contents in the same object that used to be the
5874 forward reference. */
5877 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5879 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5881 enumtype = make_node (ENUMERAL_TYPE);
5882 pushtag (name, enumtype);
5885 if (C_TYPE_BEING_DEFINED (enumtype))
5886 error ("nested redefinition of %<enum %E%>", name);
5888 C_TYPE_BEING_DEFINED (enumtype) = 1;
5890 if (TYPE_VALUES (enumtype) != 0)
5892 /* This enum is a named one that has been declared already. */
5893 error ("redeclaration of %<enum %E%>", name);
5895 /* Completely replace its old definition.
5896 The old enumerators remain defined, however. */
5897 TYPE_VALUES (enumtype) = 0;
5900 the_enum->enum_next_value = integer_zero_node;
5901 the_enum->enum_overflow = 0;
5903 if (flag_short_enums)
5904 TYPE_PACKED (enumtype) = 1;
5909 /* After processing and defining all the values of an enumeration type,
5910 install their decls in the enumeration type and finish it off.
5911 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5912 and ATTRIBUTES are the specified attributes.
5913 Returns ENUMTYPE. */
5916 finish_enum (tree enumtype, tree values, tree attributes)
5919 tree minnode = 0, maxnode = 0;
5920 int precision, unsign;
5921 bool toplevel = (file_scope == current_scope);
5922 struct lang_type *lt;
5924 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5926 /* Calculate the maximum value of any enumerator in this type. */
5928 if (values == error_mark_node)
5929 minnode = maxnode = integer_zero_node;
5932 minnode = maxnode = TREE_VALUE (values);
5933 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5935 tree value = TREE_VALUE (pair);
5936 if (tree_int_cst_lt (maxnode, value))
5938 if (tree_int_cst_lt (value, minnode))
5943 /* Construct the final type of this enumeration. It is the same
5944 as one of the integral types - the narrowest one that fits, except
5945 that normally we only go as narrow as int - and signed iff any of
5946 the values are negative. */
5947 unsign = (tree_int_cst_sgn (minnode) >= 0);
5948 precision = MAX (tree_int_cst_min_precision (minnode, unsign),
5949 tree_int_cst_min_precision (maxnode, unsign));
5951 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5953 tem = c_common_type_for_size (precision, unsign);
5956 warning (0, "enumeration values exceed range of largest integer");
5957 tem = long_long_integer_type_node;
5961 tem = unsign ? unsigned_type_node : integer_type_node;
5963 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5964 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5965 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5966 TYPE_SIZE (enumtype) = 0;
5968 /* If the precision of the type was specific with an attribute and it
5969 was too small, give an error. Otherwise, use it. */
5970 if (TYPE_PRECISION (enumtype))
5972 if (precision > TYPE_PRECISION (enumtype))
5973 error ("specified mode too small for enumeral values");
5976 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5978 layout_type (enumtype);
5980 if (values != error_mark_node)
5982 /* Change the type of the enumerators to be the enum type. We
5983 need to do this irrespective of the size of the enum, for
5984 proper type checking. Replace the DECL_INITIALs of the
5985 enumerators, and the value slots of the list, with copies
5986 that have the enum type; they cannot be modified in place
5987 because they may be shared (e.g. integer_zero_node) Finally,
5988 change the purpose slots to point to the names of the decls. */
5989 for (pair = values; pair; pair = TREE_CHAIN (pair))
5991 tree enu = TREE_PURPOSE (pair);
5992 tree ini = DECL_INITIAL (enu);
5994 TREE_TYPE (enu) = enumtype;
5996 /* The ISO C Standard mandates enumerators to have type int,
5997 even though the underlying type of an enum type is
5998 unspecified. However, GCC allows enumerators of any
5999 integer type as an extensions. build_enumerator()
6000 converts any enumerators that fit in an int to type int,
6001 to avoid promotions to unsigned types when comparing
6002 integers with enumerators that fit in the int range.
6003 When -pedantic is given, build_enumerator() would have
6004 already warned about those that don't fit. Here we
6005 convert the rest to the enumerator type. */
6006 if (TREE_TYPE (ini) != integer_type_node)
6007 ini = convert (enumtype, ini);
6009 DECL_INITIAL (enu) = ini;
6010 TREE_PURPOSE (pair) = DECL_NAME (enu);
6011 TREE_VALUE (pair) = ini;
6014 TYPE_VALUES (enumtype) = values;
6017 /* Record the min/max values so that we can warn about bit-field
6018 enumerations that are too small for the values. */
6019 lt = GGC_CNEW (struct lang_type);
6020 lt->enum_min = minnode;
6021 lt->enum_max = maxnode;
6022 TYPE_LANG_SPECIFIC (enumtype) = lt;
6024 /* Fix up all variant types of this enum type. */
6025 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
6027 if (tem == enumtype)
6029 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
6030 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
6031 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
6032 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
6033 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
6034 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
6035 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
6036 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
6037 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
6038 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
6039 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
6042 /* Finish debugging output for this type. */
6043 rest_of_type_compilation (enumtype, toplevel);
6048 /* Build and install a CONST_DECL for one value of the
6049 current enumeration type (one that was begun with start_enum).
6050 Return a tree-list containing the CONST_DECL and its value.
6051 Assignment of sequential values by default is handled here. */
6054 build_enumerator (struct c_enum_contents *the_enum, tree name, tree value,
6055 location_t value_loc)
6059 /* Validate and default VALUE. */
6063 /* Don't issue more errors for error_mark_node (i.e. an
6064 undeclared identifier) - just ignore the value expression. */
6065 if (value == error_mark_node)
6067 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
6068 || TREE_CODE (value) != INTEGER_CST)
6070 error ("enumerator value for %qE is not an integer constant", name);
6075 value = default_conversion (value);
6076 constant_expression_warning (value);
6080 /* Default based on previous value. */
6081 /* It should no longer be possible to have NON_LVALUE_EXPR
6085 value = the_enum->enum_next_value;
6086 if (the_enum->enum_overflow)
6087 error ("overflow in enumeration values");
6089 /* Even though the underlying type of an enum is unspecified, the
6090 type of enumeration constants is explicitly defined as int
6091 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
6093 else if (!int_fits_type_p (value, integer_type_node))
6094 pedwarn (value_loc, OPT_pedantic,
6095 "ISO C restricts enumerator values to range of %<int%>");
6097 /* The ISO C Standard mandates enumerators to have type int, even
6098 though the underlying type of an enum type is unspecified.
6099 However, GCC allows enumerators of any integer type as an
6100 extensions. Here we convert any enumerators that fit in an int
6101 to type int, to avoid promotions to unsigned types when comparing
6102 integers with enumerators that fit in the int range. When
6103 -pedantic is given, we would have already warned about those that
6104 don't fit. We have to do this here rather than in finish_enum
6105 because this value may be used to define more enumerators. */
6106 if (int_fits_type_p (value, integer_type_node))
6107 value = convert (integer_type_node, value);
6109 /* Set basis for default for next value. */
6110 the_enum->enum_next_value
6112 (EXPR_HAS_LOCATION (value) ? EXPR_LOCATION (value) : input_location,
6113 PLUS_EXPR, value, integer_one_node, 0);
6114 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
6116 /* Now create a declaration for the enum value name. */
6118 type = TREE_TYPE (value);
6119 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6120 TYPE_PRECISION (integer_type_node)),
6121 (TYPE_PRECISION (type)
6122 >= TYPE_PRECISION (integer_type_node)
6123 && TYPE_UNSIGNED (type)));
6125 decl = build_decl (CONST_DECL, name, type);
6126 DECL_INITIAL (decl) = convert (type, value);
6129 return tree_cons (decl, value, NULL_TREE);
6133 /* Create the FUNCTION_DECL for a function definition.
6134 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6135 the declaration; they describe the function's name and the type it returns,
6136 but twisted together in a fashion that parallels the syntax of C.
6138 This function creates a binding context for the function body
6139 as well as setting up the FUNCTION_DECL in current_function_decl.
6141 Returns 1 on success. If the DECLARATOR is not suitable for a function
6142 (it defines a datum instead), we return 0, which tells
6143 yyparse to report a parse error. */
6146 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6149 tree decl1, old_decl;
6150 tree restype, resdecl;
6151 struct c_label_context_se *nstack_se;
6152 struct c_label_context_vm *nstack_vm;
6154 current_function_returns_value = 0; /* Assume, until we see it does. */
6155 current_function_returns_null = 0;
6156 current_function_returns_abnormally = 0;
6157 warn_about_return_type = 0;
6158 c_switch_stack = NULL;
6160 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6161 nstack_se->labels_def = NULL;
6162 nstack_se->labels_used = NULL;
6163 nstack_se->next = label_context_stack_se;
6164 label_context_stack_se = nstack_se;
6166 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6167 nstack_vm->labels_def = NULL;
6168 nstack_vm->labels_used = NULL;
6169 nstack_vm->scope = 0;
6170 nstack_vm->next = label_context_stack_vm;
6171 label_context_stack_vm = nstack_vm;
6173 /* Indicate no valid break/continue context by setting these variables
6174 to some non-null, non-label value. We'll notice and emit the proper
6175 error message in c_finish_bc_stmt. */
6176 c_break_label = c_cont_label = size_zero_node;
6178 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
6179 &attributes, NULL, NULL, DEPRECATED_NORMAL);
6181 /* If the declarator is not suitable for a function definition,
6182 cause a syntax error. */
6185 label_context_stack_se = label_context_stack_se->next;
6186 label_context_stack_vm = label_context_stack_vm->next;
6190 decl_attributes (&decl1, attributes, 0);
6192 if (DECL_DECLARED_INLINE_P (decl1)
6193 && DECL_UNINLINABLE (decl1)
6194 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6195 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6198 /* Handle gnu_inline attribute. */
6199 if (declspecs->inline_p
6200 && !flag_gnu89_inline
6201 && TREE_CODE (decl1) == FUNCTION_DECL
6202 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
6203 || current_function_decl))
6205 if (declspecs->storage_class != csc_static)
6206 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6209 announce_function (decl1);
6211 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6213 error ("return type is an incomplete type");
6214 /* Make it return void instead. */
6216 = build_function_type (void_type_node,
6217 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6220 if (warn_about_return_type)
6221 pedwarn_c99 (input_location, flag_isoc99 ? 0
6222 : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
6223 "return type defaults to %<int%>");
6225 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6226 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6227 DECL_INITIAL (decl1) = error_mark_node;
6229 /* If this definition isn't a prototype and we had a prototype declaration
6230 before, copy the arg type info from that prototype. */
6231 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6232 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6234 current_function_prototype_locus = UNKNOWN_LOCATION;
6235 current_function_prototype_built_in = false;
6236 current_function_prototype_arg_types = NULL_TREE;
6237 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6239 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6240 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6241 TREE_TYPE (TREE_TYPE (old_decl))))
6243 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6245 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6246 current_function_prototype_built_in
6247 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6248 current_function_prototype_arg_types
6249 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6251 if (TREE_PUBLIC (decl1))
6253 /* If there is an external prototype declaration of this
6254 function, record its location but do not copy information
6255 to this decl. This may be an invisible declaration
6256 (built-in or in a scope which has finished) or simply
6257 have more refined argument types than any declaration
6259 struct c_binding *b;
6260 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6261 if (B_IN_SCOPE (b, external_scope))
6265 tree ext_decl, ext_type;
6267 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6268 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6269 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6270 TREE_TYPE (ext_type)))
6272 current_function_prototype_locus
6273 = DECL_SOURCE_LOCATION (ext_decl);
6274 current_function_prototype_built_in
6275 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6276 current_function_prototype_arg_types
6277 = TYPE_ARG_TYPES (ext_type);
6283 /* Optionally warn of old-fashioned def with no previous prototype. */
6284 if (warn_strict_prototypes
6285 && old_decl != error_mark_node
6286 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6287 && C_DECL_ISNT_PROTOTYPE (old_decl))
6288 warning (OPT_Wstrict_prototypes,
6289 "function declaration isn%'t a prototype");
6290 /* Optionally warn of any global def with no previous prototype. */
6291 else if (warn_missing_prototypes
6292 && old_decl != error_mark_node
6293 && TREE_PUBLIC (decl1)
6294 && !MAIN_NAME_P (DECL_NAME (decl1))
6295 && C_DECL_ISNT_PROTOTYPE (old_decl))
6296 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6297 /* Optionally warn of any def with no previous prototype
6298 if the function has already been used. */
6299 else if (warn_missing_prototypes
6301 && old_decl != error_mark_node
6302 && TREE_USED (old_decl)
6303 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6304 warning (OPT_Wmissing_prototypes,
6305 "%q+D was used with no prototype before its definition", decl1);
6306 /* Optionally warn of any global def with no previous declaration. */
6307 else if (warn_missing_declarations
6308 && TREE_PUBLIC (decl1)
6310 && !MAIN_NAME_P (DECL_NAME (decl1)))
6311 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6313 /* Optionally warn of any def with no previous declaration
6314 if the function has already been used. */
6315 else if (warn_missing_declarations
6317 && old_decl != error_mark_node
6318 && TREE_USED (old_decl)
6319 && C_DECL_IMPLICIT (old_decl))
6320 warning (OPT_Wmissing_declarations,
6321 "%q+D was used with no declaration before its definition", decl1);
6323 /* This function exists in static storage.
6324 (This does not mean `static' in the C sense!) */
6325 TREE_STATIC (decl1) = 1;
6327 /* A nested function is not global. */
6328 if (current_function_decl != 0)
6329 TREE_PUBLIC (decl1) = 0;
6331 /* This is the earliest point at which we might know the assembler
6332 name of the function. Thus, if it's set before this, die horribly. */
6333 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6335 /* If #pragma weak was used, mark the decl weak now. */
6336 if (current_scope == file_scope)
6337 maybe_apply_pragma_weak (decl1);
6339 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6340 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
6342 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6343 != integer_type_node)
6344 pedwarn (input_location, OPT_Wmain, "return type of %q+D is not %<int%>", decl1);
6346 check_main_parameter_types (decl1);
6348 if (!TREE_PUBLIC (decl1))
6349 pedwarn (input_location, OPT_Wmain, "%q+D is normally a non-static function", decl1);
6352 /* Record the decl so that the function name is defined.
6353 If we already have a decl for this name, and it is a FUNCTION_DECL,
6354 use the old decl. */
6356 current_function_decl = pushdecl (decl1);
6359 declare_parm_level ();
6361 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6362 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6363 DECL_ARTIFICIAL (resdecl) = 1;
6364 DECL_IGNORED_P (resdecl) = 1;
6365 DECL_RESULT (current_function_decl) = resdecl;
6367 start_fname_decls ();
6372 /* Subroutine of store_parm_decls which handles new-style function
6373 definitions (prototype format). The parms already have decls, so we
6374 need only record them as in effect and complain if any redundant
6375 old-style parm decls were written. */
6377 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6381 if (current_scope->bindings)
6383 error ("%Jold-style parameter declarations in prototyped "
6384 "function definition", fndecl);
6386 /* Get rid of the old-style declarations. */
6390 /* Don't issue this warning for nested functions, and don't issue this
6391 warning if we got here because ARG_INFO_TYPES was error_mark_node
6392 (this happens when a function definition has just an ellipsis in
6393 its parameter list). */
6394 else if (!in_system_header && !current_function_scope
6395 && arg_info->types != error_mark_node)
6396 warning (OPT_Wtraditional,
6397 "%Jtraditional C rejects ISO C style function definitions",
6400 /* Now make all the parameter declarations visible in the function body.
6401 We can bypass most of the grunt work of pushdecl. */
6402 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6404 DECL_CONTEXT (decl) = current_function_decl;
6405 if (DECL_NAME (decl))
6407 bind (DECL_NAME (decl), decl, current_scope,
6408 /*invisible=*/false, /*nested=*/false,
6410 if (!TREE_USED (decl))
6411 warn_if_shadowing (decl);
6414 error ("%Jparameter name omitted", decl);
6417 /* Record the parameter list in the function declaration. */
6418 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6420 /* Now make all the ancillary declarations visible, likewise. */
6421 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6423 DECL_CONTEXT (decl) = current_function_decl;
6424 if (DECL_NAME (decl))
6425 bind (DECL_NAME (decl), decl, current_scope,
6426 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
6429 /* And all the tag declarations. */
6430 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6431 if (TREE_PURPOSE (decl))
6432 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6433 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
6436 /* Subroutine of store_parm_decls which handles old-style function
6437 definitions (separate parameter list and declarations). */
6440 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6442 struct c_binding *b;
6443 tree parm, decl, last;
6444 tree parmids = arg_info->parms;
6445 struct pointer_set_t *seen_args = pointer_set_create ();
6447 if (!in_system_header)
6448 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6451 /* Match each formal parameter name with its declaration. Save each
6452 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6453 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6455 if (TREE_VALUE (parm) == 0)
6457 error ("%Jparameter name missing from parameter list", fndecl);
6458 TREE_PURPOSE (parm) = 0;
6462 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6463 if (b && B_IN_CURRENT_SCOPE (b))
6466 /* If we got something other than a PARM_DECL it is an error. */
6467 if (TREE_CODE (decl) != PARM_DECL)
6468 error ("%q+D declared as a non-parameter", decl);
6469 /* If the declaration is already marked, we have a duplicate
6470 name. Complain and ignore the duplicate. */
6471 else if (pointer_set_contains (seen_args, decl))
6473 error ("multiple parameters named %q+D", decl);
6474 TREE_PURPOSE (parm) = 0;
6477 /* If the declaration says "void", complain and turn it into
6479 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6481 error ("parameter %q+D declared with void type", decl);
6482 TREE_TYPE (decl) = integer_type_node;
6483 DECL_ARG_TYPE (decl) = integer_type_node;
6484 layout_decl (decl, 0);
6486 warn_if_shadowing (decl);
6488 /* If no declaration found, default to int. */
6491 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6492 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6493 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6495 warn_if_shadowing (decl);
6498 pedwarn (input_location, 0, "type of %q+D defaults to %<int%>", decl);
6500 warning (OPT_Wmissing_parameter_type, "type of %q+D defaults to %<int%>", decl);
6503 TREE_PURPOSE (parm) = decl;
6504 pointer_set_insert (seen_args, decl);
6507 /* Now examine the parms chain for incomplete declarations
6508 and declarations with no corresponding names. */
6510 for (b = current_scope->bindings; b; b = b->prev)
6513 if (TREE_CODE (parm) != PARM_DECL)
6516 if (TREE_TYPE (parm) != error_mark_node
6517 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6519 error ("parameter %q+D has incomplete type", parm);
6520 TREE_TYPE (parm) = error_mark_node;
6523 if (!pointer_set_contains (seen_args, parm))
6525 error ("declaration for parameter %q+D but no such parameter", parm);
6527 /* Pretend the parameter was not missing.
6528 This gets us to a standard state and minimizes
6529 further error messages. */
6530 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6534 /* Chain the declarations together in the order of the list of
6535 names. Store that chain in the function decl, replacing the
6536 list of names. Update the current scope to match. */
6537 DECL_ARGUMENTS (fndecl) = 0;
6539 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6540 if (TREE_PURPOSE (parm))
6542 if (parm && TREE_PURPOSE (parm))
6544 last = TREE_PURPOSE (parm);
6545 DECL_ARGUMENTS (fndecl) = last;
6547 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6548 if (TREE_PURPOSE (parm))
6550 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6551 last = TREE_PURPOSE (parm);
6553 TREE_CHAIN (last) = 0;
6556 pointer_set_destroy (seen_args);
6558 /* If there was a previous prototype,
6559 set the DECL_ARG_TYPE of each argument according to
6560 the type previously specified, and report any mismatches. */
6562 if (current_function_prototype_arg_types)
6565 for (parm = DECL_ARGUMENTS (fndecl),
6566 type = current_function_prototype_arg_types;
6567 parm || (type && TREE_VALUE (type) != error_mark_node
6568 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
6569 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6571 if (parm == 0 || type == 0
6572 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6574 if (current_function_prototype_built_in)
6575 warning (0, "number of arguments doesn%'t match "
6576 "built-in prototype");
6579 error ("number of arguments doesn%'t match prototype");
6580 error ("%Hprototype declaration",
6581 ¤t_function_prototype_locus);
6585 /* Type for passing arg must be consistent with that
6586 declared for the arg. ISO C says we take the unqualified
6587 type for parameters declared with qualified type. */
6588 if (TREE_TYPE (parm) != error_mark_node
6589 && TREE_TYPE (type) != error_mark_node
6590 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6591 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6593 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6594 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6596 /* Adjust argument to match prototype. E.g. a previous
6597 `int foo(float);' prototype causes
6598 `int foo(x) float x; {...}' to be treated like
6599 `int foo(float x) {...}'. This is particularly
6600 useful for argument types like uid_t. */
6601 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6603 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6604 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6605 && TYPE_PRECISION (TREE_TYPE (parm))
6606 < TYPE_PRECISION (integer_type_node))
6607 DECL_ARG_TYPE (parm) = integer_type_node;
6609 /* ??? Is it possible to get here with a
6610 built-in prototype or will it always have
6611 been diagnosed as conflicting with an
6612 old-style definition and discarded? */
6613 if (current_function_prototype_built_in)
6614 warning (OPT_pedantic, "promoted argument %qD "
6615 "doesn%'t match built-in prototype", parm);
6618 pedwarn (input_location, OPT_pedantic, "promoted argument %qD "
6619 "doesn%'t match prototype", parm);
6620 pedwarn (current_function_prototype_locus, OPT_pedantic,
6621 "prototype declaration");
6626 if (current_function_prototype_built_in)
6627 warning (0, "argument %qD doesn%'t match "
6628 "built-in prototype", parm);
6631 error ("argument %qD doesn%'t match prototype", parm);
6632 error ("%Hprototype declaration",
6633 ¤t_function_prototype_locus);
6638 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6641 /* Otherwise, create a prototype that would match. */
6645 tree actual = 0, last = 0, type;
6647 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6649 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6651 TREE_CHAIN (last) = type;
6656 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6658 TREE_CHAIN (last) = type;
6662 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6663 of the type of this function, but we need to avoid having this
6664 affect the types of other similarly-typed functions, so we must
6665 first force the generation of an identical (but separate) type
6666 node for the relevant function type. The new node we create
6667 will be a variant of the main variant of the original function
6670 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6672 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6676 /* Store parameter declarations passed in ARG_INFO into the current
6677 function declaration. */
6680 store_parm_decls_from (struct c_arg_info *arg_info)
6682 current_function_arg_info = arg_info;
6683 store_parm_decls ();
6686 /* Store the parameter declarations into the current function declaration.
6687 This is called after parsing the parameter declarations, before
6688 digesting the body of the function.
6690 For an old-style definition, construct a prototype out of the old-style
6691 parameter declarations and inject it into the function's type. */
6694 store_parm_decls (void)
6696 tree fndecl = current_function_decl;
6699 /* The argument information block for FNDECL. */
6700 struct c_arg_info *arg_info = current_function_arg_info;
6701 current_function_arg_info = 0;
6703 /* True if this definition is written with a prototype. Note:
6704 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6705 list in a function definition as equivalent to (void) -- an
6706 empty argument list specifies the function has no parameters,
6707 but only (void) sets up a prototype for future calls. */
6708 proto = arg_info->types != 0;
6711 store_parm_decls_newstyle (fndecl, arg_info);
6713 store_parm_decls_oldstyle (fndecl, arg_info);
6715 /* The next call to push_scope will be a function body. */
6717 next_is_function_body = true;
6719 /* Write a record describing this function definition to the prototypes
6720 file (if requested). */
6722 gen_aux_info_record (fndecl, 1, 0, proto);
6724 /* Initialize the RTL code for the function. */
6725 allocate_struct_function (fndecl, false);
6727 /* Begin the statement tree for this function. */
6728 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6730 /* ??? Insert the contents of the pending sizes list into the function
6731 to be evaluated. The only reason left to have this is
6732 void foo(int n, int array[n++])
6733 because we throw away the array type in favor of a pointer type, and
6734 thus won't naturally see the SAVE_EXPR containing the increment. All
6735 other pending sizes would be handled by gimplify_parameters. */
6738 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6739 add_stmt (TREE_VALUE (t));
6742 /* Even though we're inside a function body, we still don't want to
6743 call expand_expr to calculate the size of a variable-sized array.
6744 We haven't necessarily assigned RTL to all variables yet, so it's
6745 not safe to try to expand expressions involving them. */
6746 cfun->dont_save_pending_sizes_p = 1;
6749 /* Emit diagnostics that require gimple input for detection. Operate on
6750 FNDECL and all its nested functions. */
6753 c_gimple_diagnostics_recursively (tree fndecl)
6755 struct cgraph_node *cgn;
6756 gimple_seq body = gimple_body (fndecl);
6758 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6759 c_warn_unused_result (body);
6761 /* Notice when OpenMP structured block constraints are violated. */
6763 diagnose_omp_structured_block_errors (fndecl);
6765 /* Finalize all nested functions now. */
6766 cgn = cgraph_node (fndecl);
6767 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6768 c_gimple_diagnostics_recursively (cgn->decl);
6771 /* Finish up a function declaration and compile that function
6772 all the way to assembler language output. The free the storage
6773 for the function definition.
6775 This is called after parsing the body of the function definition. */
6778 finish_function (void)
6780 tree fndecl = current_function_decl;
6782 label_context_stack_se = label_context_stack_se->next;
6783 label_context_stack_vm = label_context_stack_vm->next;
6785 if (TREE_CODE (fndecl) == FUNCTION_DECL
6786 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6788 tree args = DECL_ARGUMENTS (fndecl);
6789 for (; args; args = TREE_CHAIN (args))
6791 tree type = TREE_TYPE (args);
6792 if (INTEGRAL_TYPE_P (type)
6793 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6794 DECL_ARG_TYPE (args) = integer_type_node;
6798 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6799 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6801 /* Must mark the RESULT_DECL as being in this function. */
6803 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6804 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6806 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
6807 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6808 == integer_type_node && flag_isoc99)
6810 tree stmt = c_finish_return (integer_zero_node);
6811 /* Hack. We don't want the middle-end to warn that this return
6812 is unreachable, so we mark its location as special. Using
6813 UNKNOWN_LOCATION has the problem that it gets clobbered in
6814 annotate_one_with_locus. A cleaner solution might be to
6815 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6817 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6820 /* Tie off the statement tree for this function. */
6821 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6823 finish_fname_decls ();
6825 /* Complain if there's just no return statement. */
6826 if (warn_return_type
6827 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6828 && !current_function_returns_value && !current_function_returns_null
6829 /* Don't complain if we are no-return. */
6830 && !current_function_returns_abnormally
6831 /* Don't warn for main(). */
6832 && !MAIN_NAME_P (DECL_NAME (fndecl))
6833 /* Or if they didn't actually specify a return type. */
6834 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6835 /* Normally, with -Wreturn-type, flow will complain, but we might
6836 optimize out static functions. */
6837 && !TREE_PUBLIC (fndecl))
6839 warning (OPT_Wreturn_type,
6840 "no return statement in function returning non-void");
6841 TREE_NO_WARNING (fndecl) = 1;
6844 /* Store the end of the function, so that we get good line number
6845 info for the epilogue. */
6846 cfun->function_end_locus = input_location;
6848 /* Finalize the ELF visibility for the function. */
6849 c_determine_visibility (fndecl);
6851 /* For GNU C extern inline functions disregard inline limits. */
6852 if (DECL_EXTERNAL (fndecl)
6853 && DECL_DECLARED_INLINE_P (fndecl))
6854 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
6856 /* Genericize before inlining. Delay genericizing nested functions
6857 until their parent function is genericized. Since finalizing
6858 requires GENERIC, delay that as well. */
6860 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6861 && !undef_nested_function)
6863 if (!decl_function_context (fndecl))
6865 c_genericize (fndecl);
6866 c_gimple_diagnostics_recursively (fndecl);
6868 /* ??? Objc emits functions after finalizing the compilation unit.
6869 This should be cleaned up later and this conditional removed. */
6870 if (cgraph_global_info_ready)
6872 cgraph_add_new_function (fndecl, false);
6876 cgraph_finalize_function (fndecl, false);
6880 /* Register this function with cgraph just far enough to get it
6881 added to our parent's nested function list. Handy, since the
6882 C front end doesn't have such a list. */
6883 (void) cgraph_node (fndecl);
6887 if (!decl_function_context (fndecl))
6888 undef_nested_function = false;
6890 /* We're leaving the context of this function, so zap cfun.
6891 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6892 tree_rest_of_compilation. */
6894 current_function_decl = NULL;
6897 /* Check the declarations given in a for-loop for satisfying the C99
6898 constraints. If exactly one such decl is found, return it. */
6901 check_for_loop_decls (void)
6903 struct c_binding *b;
6904 tree one_decl = NULL_TREE;
6909 static bool hint = true;
6910 /* If we get here, declarations have been used in a for loop without
6911 the C99 for loop scope. This doesn't make much sense, so don't
6913 error ("%<for%> loop initial declarations are only allowed in C99 mode");
6916 inform (input_location,
6917 "use option -std=c99 or -std=gnu99 to compile your code");
6922 /* C99 subclause 6.8.5 paragraph 3:
6924 [#3] The declaration part of a for statement shall only
6925 declare identifiers for objects having storage class auto or
6928 It isn't clear whether, in this sentence, "identifiers" binds to
6929 "shall only declare" or to "objects" - that is, whether all identifiers
6930 declared must be identifiers for objects, or whether the restriction
6931 only applies to those that are. (A question on this in comp.std.c
6932 in November 2000 received no answer.) We implement the strictest
6933 interpretation, to avoid creating an extension which later causes
6936 for (b = current_scope->bindings; b; b = b->prev)
6939 tree decl = b->decl;
6944 switch (TREE_CODE (decl))
6947 if (TREE_STATIC (decl))
6948 error ("declaration of static variable %q+D in %<for%> loop "
6949 "initial declaration", decl);
6950 else if (DECL_EXTERNAL (decl))
6951 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
6952 "initial declaration", decl);
6956 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6960 error ("%<union %E%> declared in %<for%> loop initial declaration",
6964 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6968 error ("declaration of non-variable %q+D in %<for%> loop "
6969 "initial declaration", decl);
6976 return n_decls == 1 ? one_decl : NULL_TREE;
6979 /* Save and reinitialize the variables
6980 used during compilation of a C function. */
6983 c_push_function_context (void)
6985 struct language_function *p;
6986 p = GGC_NEW (struct language_function);
6989 p->base.x_stmt_tree = c_stmt_tree;
6990 p->x_break_label = c_break_label;
6991 p->x_cont_label = c_cont_label;
6992 p->x_switch_stack = c_switch_stack;
6993 p->arg_info = current_function_arg_info;
6994 p->returns_value = current_function_returns_value;
6995 p->returns_null = current_function_returns_null;
6996 p->returns_abnormally = current_function_returns_abnormally;
6997 p->warn_about_return_type = warn_about_return_type;
6999 push_function_context ();
7002 /* Restore the variables used during compilation of a C function. */
7005 c_pop_function_context (void)
7007 struct language_function *p;
7009 pop_function_context ();
7011 cfun->language = NULL;
7013 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
7014 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
7016 /* Stop pointing to the local nodes about to be freed. */
7017 /* But DECL_INITIAL must remain nonzero so we know this
7018 was an actual function definition. */
7019 DECL_INITIAL (current_function_decl) = error_mark_node;
7020 DECL_ARGUMENTS (current_function_decl) = 0;
7023 c_stmt_tree = p->base.x_stmt_tree;
7024 c_break_label = p->x_break_label;
7025 c_cont_label = p->x_cont_label;
7026 c_switch_stack = p->x_switch_stack;
7027 current_function_arg_info = p->arg_info;
7028 current_function_returns_value = p->returns_value;
7029 current_function_returns_null = p->returns_null;
7030 current_function_returns_abnormally = p->returns_abnormally;
7031 warn_about_return_type = p->warn_about_return_type;
7034 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
7037 c_dup_lang_specific_decl (tree decl)
7039 struct lang_decl *ld;
7041 if (!DECL_LANG_SPECIFIC (decl))
7044 ld = GGC_NEW (struct lang_decl);
7045 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
7046 DECL_LANG_SPECIFIC (decl) = ld;
7049 /* The functions below are required for functionality of doing
7050 function at once processing in the C front end. Currently these
7051 functions are not called from anywhere in the C front end, but as
7052 these changes continue, that will change. */
7054 /* Returns the stmt_tree (if any) to which statements are currently
7055 being added. If there is no active statement-tree, NULL is
7059 current_stmt_tree (void)
7061 return &c_stmt_tree;
7064 /* Return the global value of T as a symbol. */
7067 identifier_global_value (tree t)
7069 struct c_binding *b;
7071 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
7072 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
7078 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
7079 otherwise the name is found in ridpointers from RID_INDEX. */
7082 record_builtin_type (enum rid rid_index, const char *name, tree type)
7086 id = ridpointers[(int) rid_index];
7088 id = get_identifier (name);
7089 decl = build_decl (TYPE_DECL, id, type);
7091 if (debug_hooks->type_decl)
7092 debug_hooks->type_decl (decl, false);
7095 /* Build the void_list_node (void_type_node having been created). */
7097 build_void_list_node (void)
7099 tree t = build_tree_list (NULL_TREE, void_type_node);
7103 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7106 build_c_parm (struct c_declspecs *specs, tree attrs,
7107 struct c_declarator *declarator)
7109 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7112 ret->declarator = declarator;
7116 /* Return a declarator with nested attributes. TARGET is the inner
7117 declarator to which these attributes apply. ATTRS are the
7120 struct c_declarator *
7121 build_attrs_declarator (tree attrs, struct c_declarator *target)
7123 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7124 ret->kind = cdk_attrs;
7125 ret->declarator = target;
7126 ret->u.attrs = attrs;
7130 /* Return a declarator for a function with arguments specified by ARGS
7131 and return type specified by TARGET. */
7133 struct c_declarator *
7134 build_function_declarator (struct c_arg_info *args,
7135 struct c_declarator *target)
7137 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7138 ret->kind = cdk_function;
7139 ret->declarator = target;
7140 ret->u.arg_info = args;
7144 /* Return a declarator for the identifier IDENT (which may be
7145 NULL_TREE for an abstract declarator). */
7147 struct c_declarator *
7148 build_id_declarator (tree ident)
7150 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7152 ret->declarator = 0;
7154 /* Default value - may get reset to a more precise location. */
7155 ret->id_loc = input_location;
7159 /* Return something to represent absolute declarators containing a *.
7160 TARGET is the absolute declarator that the * contains.
7161 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7162 to apply to the pointer type. */
7164 struct c_declarator *
7165 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7166 struct c_declarator *target)
7170 struct c_declarator *itarget = target;
7171 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7172 if (type_quals_attrs)
7174 attrs = type_quals_attrs->attrs;
7175 quals = quals_from_declspecs (type_quals_attrs);
7176 if (attrs != NULL_TREE)
7177 itarget = build_attrs_declarator (attrs, target);
7179 ret->kind = cdk_pointer;
7180 ret->declarator = itarget;
7181 ret->u.pointer_quals = quals;
7185 /* Return a pointer to a structure for an empty list of declaration
7188 struct c_declspecs *
7189 build_null_declspecs (void)
7191 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7196 ret->typespec_word = cts_none;
7197 ret->storage_class = csc_none;
7198 ret->expr_const_operands = true;
7199 ret->declspecs_seen_p = false;
7200 ret->type_seen_p = false;
7201 ret->non_sc_seen_p = false;
7202 ret->typedef_p = false;
7203 ret->tag_defined_p = false;
7204 ret->explicit_signed_p = false;
7205 ret->deprecated_p = false;
7206 ret->default_int_p = false;
7207 ret->long_p = false;
7208 ret->long_long_p = false;
7209 ret->short_p = false;
7210 ret->signed_p = false;
7211 ret->unsigned_p = false;
7212 ret->complex_p = false;
7213 ret->inline_p = false;
7214 ret->thread_p = false;
7215 ret->const_p = false;
7216 ret->volatile_p = false;
7217 ret->restrict_p = false;
7218 ret->saturating_p = false;
7222 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7225 struct c_declspecs *
7226 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7230 specs->non_sc_seen_p = true;
7231 specs->declspecs_seen_p = true;
7232 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7233 && C_IS_RESERVED_WORD (qual));
7234 i = C_RID_CODE (qual);
7238 dupe = specs->const_p;
7239 specs->const_p = true;
7242 dupe = specs->volatile_p;
7243 specs->volatile_p = true;
7246 dupe = specs->restrict_p;
7247 specs->restrict_p = true;
7252 if (dupe && !flag_isoc99)
7253 pedwarn (input_location, OPT_pedantic, "duplicate %qE", qual);
7257 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7260 struct c_declspecs *
7261 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7263 tree type = spec.spec;
7264 specs->non_sc_seen_p = true;
7265 specs->declspecs_seen_p = true;
7266 specs->type_seen_p = true;
7267 if (TREE_DEPRECATED (type))
7268 specs->deprecated_p = true;
7270 /* Handle type specifier keywords. */
7271 if (TREE_CODE (type) == IDENTIFIER_NODE
7272 && C_IS_RESERVED_WORD (type)
7273 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
7275 enum rid i = C_RID_CODE (type);
7278 error ("two or more data types in declaration specifiers");
7281 if ((int) i <= (int) RID_LAST_MODIFIER)
7283 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
7288 if (specs->long_long_p)
7290 error ("%<long long long%> is too long for GCC");
7295 if (specs->typespec_word == cts_double)
7297 error ("both %<long long%> and %<double%> in "
7298 "declaration specifiers");
7301 if (pedantic && !flag_isoc99 && !in_system_header)
7302 pedwarn (input_location, OPT_Wlong_long, "ISO C90 does not support %<long long%>");
7303 specs->long_long_p = 1;
7307 error ("both %<long%> and %<short%> in "
7308 "declaration specifiers");
7309 else if (specs->typespec_word == cts_void)
7310 error ("both %<long%> and %<void%> in "
7311 "declaration specifiers");
7312 else if (specs->typespec_word == cts_bool)
7313 error ("both %<long%> and %<_Bool%> in "
7314 "declaration specifiers");
7315 else if (specs->typespec_word == cts_char)
7316 error ("both %<long%> and %<char%> in "
7317 "declaration specifiers");
7318 else if (specs->typespec_word == cts_float)
7319 error ("both %<long%> and %<float%> in "
7320 "declaration specifiers");
7321 else if (specs->typespec_word == cts_dfloat32)
7322 error ("both %<long%> and %<_Decimal32%> in "
7323 "declaration specifiers");
7324 else if (specs->typespec_word == cts_dfloat64)
7325 error ("both %<long%> and %<_Decimal64%> in "
7326 "declaration specifiers");
7327 else if (specs->typespec_word == cts_dfloat128)
7328 error ("both %<long%> and %<_Decimal128%> in "
7329 "declaration specifiers");
7331 specs->long_p = true;
7334 dupe = specs->short_p;
7336 error ("both %<long%> and %<short%> in "
7337 "declaration specifiers");
7338 else if (specs->typespec_word == cts_void)
7339 error ("both %<short%> and %<void%> in "
7340 "declaration specifiers");
7341 else if (specs->typespec_word == cts_bool)
7342 error ("both %<short%> and %<_Bool%> in "
7343 "declaration specifiers");
7344 else if (specs->typespec_word == cts_char)
7345 error ("both %<short%> and %<char%> in "
7346 "declaration specifiers");
7347 else if (specs->typespec_word == cts_float)
7348 error ("both %<short%> and %<float%> in "
7349 "declaration specifiers");
7350 else if (specs->typespec_word == cts_double)
7351 error ("both %<short%> and %<double%> in "
7352 "declaration specifiers");
7353 else if (specs->typespec_word == cts_dfloat32)
7354 error ("both %<short%> and %<_Decimal32%> in "
7355 "declaration specifiers");
7356 else if (specs->typespec_word == cts_dfloat64)
7357 error ("both %<short%> and %<_Decimal64%> in "
7358 "declaration specifiers");
7359 else if (specs->typespec_word == cts_dfloat128)
7360 error ("both %<short%> and %<_Decimal128%> in "
7361 "declaration specifiers");
7363 specs->short_p = true;
7366 dupe = specs->signed_p;
7367 if (specs->unsigned_p)
7368 error ("both %<signed%> and %<unsigned%> in "
7369 "declaration specifiers");
7370 else if (specs->typespec_word == cts_void)
7371 error ("both %<signed%> and %<void%> in "
7372 "declaration specifiers");
7373 else if (specs->typespec_word == cts_bool)
7374 error ("both %<signed%> and %<_Bool%> in "
7375 "declaration specifiers");
7376 else if (specs->typespec_word == cts_float)
7377 error ("both %<signed%> and %<float%> in "
7378 "declaration specifiers");
7379 else if (specs->typespec_word == cts_double)
7380 error ("both %<signed%> and %<double%> in "
7381 "declaration specifiers");
7382 else if (specs->typespec_word == cts_dfloat32)
7383 error ("both %<signed%> and %<_Decimal32%> in "
7384 "declaration specifiers");
7385 else if (specs->typespec_word == cts_dfloat64)
7386 error ("both %<signed%> and %<_Decimal64%> in "
7387 "declaration specifiers");
7388 else if (specs->typespec_word == cts_dfloat128)
7389 error ("both %<signed%> and %<_Decimal128%> in "
7390 "declaration specifiers");
7392 specs->signed_p = true;
7395 dupe = specs->unsigned_p;
7396 if (specs->signed_p)
7397 error ("both %<signed%> and %<unsigned%> in "
7398 "declaration specifiers");
7399 else if (specs->typespec_word == cts_void)
7400 error ("both %<unsigned%> and %<void%> in "
7401 "declaration specifiers");
7402 else if (specs->typespec_word == cts_bool)
7403 error ("both %<unsigned%> and %<_Bool%> in "
7404 "declaration specifiers");
7405 else if (specs->typespec_word == cts_float)
7406 error ("both %<unsigned%> and %<float%> in "
7407 "declaration specifiers");
7408 else if (specs->typespec_word == cts_double)
7409 error ("both %<unsigned%> and %<double%> in "
7410 "declaration specifiers");
7411 else if (specs->typespec_word == cts_dfloat32)
7412 error ("both %<unsigned%> and %<_Decimal32%> in "
7413 "declaration specifiers");
7414 else if (specs->typespec_word == cts_dfloat64)
7415 error ("both %<unsigned%> and %<_Decimal64%> in "
7416 "declaration specifiers");
7417 else if (specs->typespec_word == cts_dfloat128)
7418 error ("both %<unsigned%> and %<_Decimal128%> in "
7419 "declaration specifiers");
7421 specs->unsigned_p = true;
7424 dupe = specs->complex_p;
7425 if (!flag_isoc99 && !in_system_header)
7426 pedwarn (input_location, OPT_pedantic, "ISO C90 does not support complex types");
7427 if (specs->typespec_word == cts_void)
7428 error ("both %<complex%> and %<void%> in "
7429 "declaration specifiers");
7430 else if (specs->typespec_word == cts_bool)
7431 error ("both %<complex%> and %<_Bool%> in "
7432 "declaration specifiers");
7433 else if (specs->typespec_word == cts_dfloat32)
7434 error ("both %<complex%> and %<_Decimal32%> in "
7435 "declaration specifiers");
7436 else if (specs->typespec_word == cts_dfloat64)
7437 error ("both %<complex%> and %<_Decimal64%> in "
7438 "declaration specifiers");
7439 else if (specs->typespec_word == cts_dfloat128)
7440 error ("both %<complex%> and %<_Decimal128%> in "
7441 "declaration specifiers");
7442 else if (specs->typespec_word == cts_fract)
7443 error ("both %<complex%> and %<_Fract%> in "
7444 "declaration specifiers");
7445 else if (specs->typespec_word == cts_accum)
7446 error ("both %<complex%> and %<_Accum%> in "
7447 "declaration specifiers");
7448 else if (specs->saturating_p)
7449 error ("both %<complex%> and %<_Sat%> in "
7450 "declaration specifiers");
7452 specs->complex_p = true;
7455 dupe = specs->saturating_p;
7456 pedwarn (input_location, OPT_pedantic, "ISO C does not support saturating types");
7457 if (specs->typespec_word == cts_void)
7458 error ("both %<_Sat%> and %<void%> in "
7459 "declaration specifiers");
7460 else if (specs->typespec_word == cts_bool)
7461 error ("both %<_Sat%> and %<_Bool%> in "
7462 "declaration specifiers");
7463 else if (specs->typespec_word == cts_char)
7464 error ("both %<_Sat%> and %<char%> in "
7465 "declaration specifiers");
7466 else if (specs->typespec_word == cts_int)
7467 error ("both %<_Sat%> and %<int%> in "
7468 "declaration specifiers");
7469 else if (specs->typespec_word == cts_float)
7470 error ("both %<_Sat%> and %<float%> in "
7471 "declaration specifiers");
7472 else if (specs->typespec_word == cts_double)
7473 error ("both %<_Sat%> and %<double%> in "
7474 "declaration specifiers");
7475 else if (specs->typespec_word == cts_dfloat32)
7476 error ("both %<_Sat%> and %<_Decimal32%> in "
7477 "declaration specifiers");
7478 else if (specs->typespec_word == cts_dfloat64)
7479 error ("both %<_Sat%> and %<_Decimal64%> in "
7480 "declaration specifiers");
7481 else if (specs->typespec_word == cts_dfloat128)
7482 error ("both %<_Sat%> and %<_Decimal128%> in "
7483 "declaration specifiers");
7484 else if (specs->complex_p)
7485 error ("both %<_Sat%> and %<complex%> in "
7486 "declaration specifiers");
7488 specs->saturating_p = true;
7495 error ("duplicate %qE", type);
7501 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
7502 "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
7503 if (specs->typespec_word != cts_none)
7505 error ("two or more data types in declaration specifiers");
7512 error ("both %<long%> and %<void%> in "
7513 "declaration specifiers");
7514 else if (specs->short_p)
7515 error ("both %<short%> and %<void%> in "
7516 "declaration specifiers");
7517 else if (specs->signed_p)
7518 error ("both %<signed%> and %<void%> in "
7519 "declaration specifiers");
7520 else if (specs->unsigned_p)
7521 error ("both %<unsigned%> and %<void%> in "
7522 "declaration specifiers");
7523 else if (specs->complex_p)
7524 error ("both %<complex%> and %<void%> in "
7525 "declaration specifiers");
7526 else if (specs->saturating_p)
7527 error ("both %<_Sat%> and %<void%> in "
7528 "declaration specifiers");
7530 specs->typespec_word = cts_void;
7534 error ("both %<long%> and %<_Bool%> in "
7535 "declaration specifiers");
7536 else if (specs->short_p)
7537 error ("both %<short%> and %<_Bool%> in "
7538 "declaration specifiers");
7539 else if (specs->signed_p)
7540 error ("both %<signed%> and %<_Bool%> in "
7541 "declaration specifiers");
7542 else if (specs->unsigned_p)
7543 error ("both %<unsigned%> and %<_Bool%> in "
7544 "declaration specifiers");
7545 else if (specs->complex_p)
7546 error ("both %<complex%> and %<_Bool%> in "
7547 "declaration specifiers");
7548 else if (specs->saturating_p)
7549 error ("both %<_Sat%> and %<_Bool%> in "
7550 "declaration specifiers");
7552 specs->typespec_word = cts_bool;
7556 error ("both %<long%> and %<char%> in "
7557 "declaration specifiers");
7558 else if (specs->short_p)
7559 error ("both %<short%> and %<char%> in "
7560 "declaration specifiers");
7561 else if (specs->saturating_p)
7562 error ("both %<_Sat%> and %<char%> in "
7563 "declaration specifiers");
7565 specs->typespec_word = cts_char;
7568 if (specs->saturating_p)
7569 error ("both %<_Sat%> and %<int%> in "
7570 "declaration specifiers");
7572 specs->typespec_word = cts_int;
7576 error ("both %<long%> and %<float%> in "
7577 "declaration specifiers");
7578 else if (specs->short_p)
7579 error ("both %<short%> and %<float%> in "
7580 "declaration specifiers");
7581 else if (specs->signed_p)
7582 error ("both %<signed%> and %<float%> in "
7583 "declaration specifiers");
7584 else if (specs->unsigned_p)
7585 error ("both %<unsigned%> and %<float%> in "
7586 "declaration specifiers");
7587 else if (specs->saturating_p)
7588 error ("both %<_Sat%> and %<float%> in "
7589 "declaration specifiers");
7591 specs->typespec_word = cts_float;
7594 if (specs->long_long_p)
7595 error ("both %<long long%> and %<double%> in "
7596 "declaration specifiers");
7597 else if (specs->short_p)
7598 error ("both %<short%> and %<double%> in "
7599 "declaration specifiers");
7600 else if (specs->signed_p)
7601 error ("both %<signed%> and %<double%> in "
7602 "declaration specifiers");
7603 else if (specs->unsigned_p)
7604 error ("both %<unsigned%> and %<double%> in "
7605 "declaration specifiers");
7606 else if (specs->saturating_p)
7607 error ("both %<_Sat%> and %<double%> in "
7608 "declaration specifiers");
7610 specs->typespec_word = cts_double;
7617 if (i == RID_DFLOAT32)
7619 else if (i == RID_DFLOAT64)
7622 str = "_Decimal128";
7623 if (specs->long_long_p)
7624 error ("both %<long long%> and %<%s%> in "
7625 "declaration specifiers", str);
7627 error ("both %<long%> and %<%s%> in "
7628 "declaration specifiers", str);
7629 else if (specs->short_p)
7630 error ("both %<short%> and %<%s%> in "
7631 "declaration specifiers", str);
7632 else if (specs->signed_p)
7633 error ("both %<signed%> and %<%s%> in "
7634 "declaration specifiers", str);
7635 else if (specs->unsigned_p)
7636 error ("both %<unsigned%> and %<%s%> in "
7637 "declaration specifiers", str);
7638 else if (specs->complex_p)
7639 error ("both %<complex%> and %<%s%> in "
7640 "declaration specifiers", str);
7641 else if (specs->saturating_p)
7642 error ("both %<_Sat%> and %<%s%> in "
7643 "declaration specifiers", str);
7644 else if (i == RID_DFLOAT32)
7645 specs->typespec_word = cts_dfloat32;
7646 else if (i == RID_DFLOAT64)
7647 specs->typespec_word = cts_dfloat64;
7649 specs->typespec_word = cts_dfloat128;
7651 if (!targetm.decimal_float_supported_p ())
7652 error ("decimal floating point not supported for this target");
7653 pedwarn (input_location, OPT_pedantic,
7654 "ISO C does not support decimal floating point");
7664 if (specs->complex_p)
7665 error ("both %<complex%> and %<%s%> in "
7666 "declaration specifiers", str);
7667 else if (i == RID_FRACT)
7668 specs->typespec_word = cts_fract;
7670 specs->typespec_word = cts_accum;
7672 if (!targetm.fixed_point_supported_p ())
7673 error ("fixed-point types not supported for this target");
7674 pedwarn (input_location, OPT_pedantic,
7675 "ISO C does not support fixed-point types");
7678 /* ObjC reserved word "id", handled below. */
7684 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7685 form of ObjC type, cases such as "int" and "long" being handled
7686 above), a TYPE (struct, union, enum and typeof specifiers) or an
7687 ERROR_MARK. In none of these cases may there have previously
7688 been any type specifiers. */
7689 if (specs->type || specs->typespec_word != cts_none
7690 || specs->long_p || specs->short_p || specs->signed_p
7691 || specs->unsigned_p || specs->complex_p)
7692 error ("two or more data types in declaration specifiers");
7693 else if (TREE_CODE (type) == TYPE_DECL)
7695 if (TREE_TYPE (type) == error_mark_node)
7696 ; /* Allow the type to default to int to avoid cascading errors. */
7699 specs->type = TREE_TYPE (type);
7700 specs->decl_attr = DECL_ATTRIBUTES (type);
7701 specs->typedef_p = true;
7702 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7705 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7707 tree t = lookup_name (type);
7708 if (!t || TREE_CODE (t) != TYPE_DECL)
7709 error ("%qE fails to be a typedef or built in type", type);
7710 else if (TREE_TYPE (t) == error_mark_node)
7713 specs->type = TREE_TYPE (t);
7715 else if (TREE_CODE (type) != ERROR_MARK)
7717 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7718 specs->tag_defined_p = true;
7719 if (spec.kind == ctsk_typeof)
7721 specs->typedef_p = true;
7725 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
7726 specs->expr, spec.expr);
7728 specs->expr = spec.expr;
7729 specs->expr_const_operands &= spec.expr_const_operands;
7738 /* Add the storage class specifier or function specifier SCSPEC to the
7739 declaration specifiers SPECS, returning SPECS. */
7741 struct c_declspecs *
7742 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7745 enum c_storage_class n = csc_none;
7747 specs->declspecs_seen_p = true;
7748 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7749 && C_IS_RESERVED_WORD (scspec));
7750 i = C_RID_CODE (scspec);
7751 if (specs->non_sc_seen_p)
7752 warning (OPT_Wold_style_declaration,
7753 "%qE is not at beginning of declaration", scspec);
7757 /* C99 permits duplicate inline. Although of doubtful utility,
7758 it seems simplest to permit it in gnu89 mode as well, as
7759 there is also little utility in maintaining this as a
7760 difference between gnu89 and C99 inline. */
7762 specs->inline_p = true;
7765 dupe = specs->thread_p;
7766 if (specs->storage_class == csc_auto)
7767 error ("%<__thread%> used with %<auto%>");
7768 else if (specs->storage_class == csc_register)
7769 error ("%<__thread%> used with %<register%>");
7770 else if (specs->storage_class == csc_typedef)
7771 error ("%<__thread%> used with %<typedef%>");
7773 specs->thread_p = true;
7780 /* Diagnose "__thread extern". */
7781 if (specs->thread_p)
7782 error ("%<__thread%> before %<extern%>");
7789 /* Diagnose "__thread static". */
7790 if (specs->thread_p)
7791 error ("%<__thread%> before %<static%>");
7799 if (n != csc_none && n == specs->storage_class)
7802 error ("duplicate %qE", scspec);
7805 if (specs->storage_class != csc_none && n != specs->storage_class)
7807 error ("multiple storage classes in declaration specifiers");
7811 specs->storage_class = n;
7812 if (n != csc_extern && n != csc_static && specs->thread_p)
7814 error ("%<__thread%> used with %qE", scspec);
7815 specs->thread_p = false;
7822 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7825 struct c_declspecs *
7826 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7828 specs->attrs = chainon (attrs, specs->attrs);
7829 specs->declspecs_seen_p = true;
7833 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7834 specifiers with any other type specifier to determine the resulting
7835 type. This is where ISO C checks on complex types are made, since
7836 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7839 struct c_declspecs *
7840 finish_declspecs (struct c_declspecs *specs)
7842 /* If a type was specified as a whole, we have no modifiers and are
7844 if (specs->type != NULL_TREE)
7846 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7847 && !specs->signed_p && !specs->unsigned_p
7848 && !specs->complex_p);
7852 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7853 has been specified, treat it as "int" unless "_Complex" is
7854 present and there are no other specifiers. If we just have
7855 "_Complex", it is equivalent to "_Complex double", but e.g.
7856 "_Complex short" is equivalent to "_Complex short int". */
7857 if (specs->typespec_word == cts_none)
7859 if (specs->saturating_p)
7861 error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
7862 if (!targetm.fixed_point_supported_p ())
7863 error ("fixed-point types not supported for this target");
7864 specs->typespec_word = cts_fract;
7866 else if (specs->long_p || specs->short_p
7867 || specs->signed_p || specs->unsigned_p)
7869 specs->typespec_word = cts_int;
7871 else if (specs->complex_p)
7873 specs->typespec_word = cts_double;
7874 pedwarn (input_location, OPT_pedantic,
7875 "ISO C does not support plain %<complex%> meaning "
7876 "%<double complex%>");
7880 specs->typespec_word = cts_int;
7881 specs->default_int_p = true;
7882 /* We don't diagnose this here because grokdeclarator will
7883 give more specific diagnostics according to whether it is
7884 a function definition. */
7888 /* If "signed" was specified, record this to distinguish "int" and
7889 "signed int" in the case of a bit-field with
7890 -funsigned-bitfields. */
7891 specs->explicit_signed_p = specs->signed_p;
7893 /* Now compute the actual type. */
7894 switch (specs->typespec_word)
7897 gcc_assert (!specs->long_p && !specs->short_p
7898 && !specs->signed_p && !specs->unsigned_p
7899 && !specs->complex_p);
7900 specs->type = void_type_node;
7903 gcc_assert (!specs->long_p && !specs->short_p
7904 && !specs->signed_p && !specs->unsigned_p
7905 && !specs->complex_p);
7906 specs->type = boolean_type_node;
7909 gcc_assert (!specs->long_p && !specs->short_p);
7910 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7911 if (specs->signed_p)
7912 specs->type = signed_char_type_node;
7913 else if (specs->unsigned_p)
7914 specs->type = unsigned_char_type_node;
7916 specs->type = char_type_node;
7917 if (specs->complex_p)
7919 pedwarn (input_location, OPT_pedantic,
7920 "ISO C does not support complex integer types");
7921 specs->type = build_complex_type (specs->type);
7925 gcc_assert (!(specs->long_p && specs->short_p));
7926 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7927 if (specs->long_long_p)
7928 specs->type = (specs->unsigned_p
7929 ? long_long_unsigned_type_node
7930 : long_long_integer_type_node);
7931 else if (specs->long_p)
7932 specs->type = (specs->unsigned_p
7933 ? long_unsigned_type_node
7934 : long_integer_type_node);
7935 else if (specs->short_p)
7936 specs->type = (specs->unsigned_p
7937 ? short_unsigned_type_node
7938 : short_integer_type_node);
7940 specs->type = (specs->unsigned_p
7941 ? unsigned_type_node
7942 : integer_type_node);
7943 if (specs->complex_p)
7945 pedwarn (input_location, OPT_pedantic,
7946 "ISO C does not support complex integer types");
7947 specs->type = build_complex_type (specs->type);
7951 gcc_assert (!specs->long_p && !specs->short_p
7952 && !specs->signed_p && !specs->unsigned_p);
7953 specs->type = (specs->complex_p
7954 ? complex_float_type_node
7958 gcc_assert (!specs->long_long_p && !specs->short_p
7959 && !specs->signed_p && !specs->unsigned_p);
7962 specs->type = (specs->complex_p
7963 ? complex_long_double_type_node
7964 : long_double_type_node);
7968 specs->type = (specs->complex_p
7969 ? complex_double_type_node
7970 : double_type_node);
7976 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7977 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
7978 if (specs->typespec_word == cts_dfloat32)
7979 specs->type = dfloat32_type_node;
7980 else if (specs->typespec_word == cts_dfloat64)
7981 specs->type = dfloat64_type_node;
7983 specs->type = dfloat128_type_node;
7986 gcc_assert (!specs->complex_p);
7987 if (!targetm.fixed_point_supported_p ())
7988 specs->type = integer_type_node;
7989 else if (specs->saturating_p)
7991 if (specs->long_long_p)
7992 specs->type = specs->unsigned_p
7993 ? sat_unsigned_long_long_fract_type_node
7994 : sat_long_long_fract_type_node;
7995 else if (specs->long_p)
7996 specs->type = specs->unsigned_p
7997 ? sat_unsigned_long_fract_type_node
7998 : sat_long_fract_type_node;
7999 else if (specs->short_p)
8000 specs->type = specs->unsigned_p
8001 ? sat_unsigned_short_fract_type_node
8002 : sat_short_fract_type_node;
8004 specs->type = specs->unsigned_p
8005 ? sat_unsigned_fract_type_node
8006 : sat_fract_type_node;
8010 if (specs->long_long_p)
8011 specs->type = specs->unsigned_p
8012 ? unsigned_long_long_fract_type_node
8013 : long_long_fract_type_node;
8014 else if (specs->long_p)
8015 specs->type = specs->unsigned_p
8016 ? unsigned_long_fract_type_node
8017 : long_fract_type_node;
8018 else if (specs->short_p)
8019 specs->type = specs->unsigned_p
8020 ? unsigned_short_fract_type_node
8021 : short_fract_type_node;
8023 specs->type = specs->unsigned_p
8024 ? unsigned_fract_type_node
8029 gcc_assert (!specs->complex_p);
8030 if (!targetm.fixed_point_supported_p ())
8031 specs->type = integer_type_node;
8032 else if (specs->saturating_p)
8034 if (specs->long_long_p)
8035 specs->type = specs->unsigned_p
8036 ? sat_unsigned_long_long_accum_type_node
8037 : sat_long_long_accum_type_node;
8038 else if (specs->long_p)
8039 specs->type = specs->unsigned_p
8040 ? sat_unsigned_long_accum_type_node
8041 : sat_long_accum_type_node;
8042 else if (specs->short_p)
8043 specs->type = specs->unsigned_p
8044 ? sat_unsigned_short_accum_type_node
8045 : sat_short_accum_type_node;
8047 specs->type = specs->unsigned_p
8048 ? sat_unsigned_accum_type_node
8049 : sat_accum_type_node;
8053 if (specs->long_long_p)
8054 specs->type = specs->unsigned_p
8055 ? unsigned_long_long_accum_type_node
8056 : long_long_accum_type_node;
8057 else if (specs->long_p)
8058 specs->type = specs->unsigned_p
8059 ? unsigned_long_accum_type_node
8060 : long_accum_type_node;
8061 else if (specs->short_p)
8062 specs->type = specs->unsigned_p
8063 ? unsigned_short_accum_type_node
8064 : short_accum_type_node;
8066 specs->type = specs->unsigned_p
8067 ? unsigned_accum_type_node
8078 /* A subroutine of c_write_global_declarations. Perform final processing
8079 on one file scope's declarations (or the external scope's declarations),
8083 c_write_global_declarations_1 (tree globals)
8088 /* Process the decls in the order they were written. */
8089 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8091 /* Check for used but undefined static functions using the C
8092 standard's definition of "used", and set TREE_NO_WARNING so
8093 that check_global_declarations doesn't repeat the check. */
8094 if (TREE_CODE (decl) == FUNCTION_DECL
8095 && DECL_INITIAL (decl) == 0
8096 && DECL_EXTERNAL (decl)
8097 && !TREE_PUBLIC (decl)
8098 && C_DECL_USED (decl))
8100 pedwarn (input_location, 0, "%q+F used but never defined", decl);
8101 TREE_NO_WARNING (decl) = 1;
8104 wrapup_global_declaration_1 (decl);
8110 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8111 reconsider |= wrapup_global_declaration_2 (decl);
8115 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8116 check_global_declaration_1 (decl);
8119 /* A subroutine of c_write_global_declarations Emit debug information for each
8120 of the declarations in GLOBALS. */
8123 c_write_global_declarations_2 (tree globals)
8127 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
8128 debug_hooks->global_decl (decl);
8131 /* Preserve the external declarations scope across a garbage collect. */
8132 static GTY(()) tree ext_block;
8135 c_write_global_declarations (void)
8139 /* We don't want to do this if generating a PCH. */
8143 /* Don't waste time on further processing if -fsyntax-only or we've
8144 encountered errors. */
8145 if (flag_syntax_only || errorcount || sorrycount)
8148 /* Close the external scope. */
8149 ext_block = pop_scope ();
8151 gcc_assert (!current_scope);
8155 tree tmp = BLOCK_VARS (ext_block);
8157 FILE * stream = dump_begin (TDI_tu, &flags);
8160 dump_node (tmp, flags & ~TDF_SLIM, stream);
8161 dump_end (TDI_tu, stream);
8165 /* Process all file scopes in this compilation, and the external_scope,
8166 through wrapup_global_declarations and check_global_declarations. */
8167 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8168 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
8169 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
8171 /* We're done parsing; proceed to optimize and emit assembly.
8172 FIXME: shouldn't be the front end's responsibility to call this. */
8175 /* After cgraph has had a chance to emit everything that's going to
8176 be emitted, output debug information for globals. */
8177 if (errorcount == 0 && sorrycount == 0)
8179 timevar_push (TV_SYMOUT);
8180 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8181 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
8182 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
8183 timevar_pop (TV_SYMOUT);
8189 #include "gt-c-decl.h"