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 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 /* Merged C99 inline changes from gcc trunk 122565 2007-03-05 */
24 /* Fixed problems with compiling inline-25.c and inline-26.c */
25 /* XXX still fails inline-29.c, inline-31.c, and inline-32.c */
27 /* Process declarations and symbol lookup for C front end.
28 Also constructs types; the standard scalar types at initialization,
29 and structure, union, array and enum types when they are declared. */
31 /* ??? not all decl nodes are given the most useful possible
32 line numbers. For example, the CONST_DECLs for enum values. */
36 #include "coretypes.h"
41 #include "tree-inline.h"
58 #include "langhooks.h"
59 #include "tree-mudflap.h"
60 #include "tree-gimple.h"
61 #include "diagnostic.h"
62 #include "tree-dump.h"
67 #include "langhooks-def.h"
68 #include "pointer-set.h"
70 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
72 { NORMAL, /* Ordinary declaration */
73 FUNCDEF, /* Function definition */
74 PARM, /* Declaration of parm before function body */
75 FIELD, /* Declaration inside struct or union */
76 TYPENAME}; /* Typename (inside cast or sizeof) */
79 /* Nonzero if we have seen an invalid cross reference
80 to a struct, union, or enum, but not yet printed the message. */
81 tree pending_invalid_xref;
83 /* File and line to appear in the eventual error message. */
84 location_t pending_invalid_xref_location;
86 /* True means we've initialized exception handling. */
87 bool c_eh_initialized_p;
89 /* While defining an enum type, this is 1 plus the last enumerator
90 constant value. Note that will do not have to save this or `enum_overflow'
91 around nested function definition since such a definition could only
92 occur in an enum value expression and we don't use these variables in
95 static tree enum_next_value;
97 /* Nonzero means that there was overflow computing enum_next_value. */
99 static int enum_overflow;
101 /* The file and line that the prototype came from if this is an
102 old-style definition; used for diagnostics in
103 store_parm_decls_oldstyle. */
105 static location_t current_function_prototype_locus;
107 /* Whether this prototype was built-in. */
109 static bool current_function_prototype_built_in;
111 /* The argument type information of this prototype. */
113 static tree current_function_prototype_arg_types;
115 /* The argument information structure for the function currently being
118 static struct c_arg_info *current_function_arg_info;
120 /* The obstack on which parser and related data structures, which are
121 not live beyond their top-level declaration or definition, are
123 struct obstack parser_obstack;
125 /* The current statement tree. */
127 static GTY(()) struct stmt_tree_s c_stmt_tree;
129 /* State saving variables. */
133 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
134 included in this invocation. Note that the current translation
135 unit is not included in this list. */
137 static GTY(()) tree all_translation_units;
139 /* A list of decls to be made automatically visible in each file scope. */
140 static GTY(()) tree visible_builtins;
142 /* Set to 0 at beginning of a function definition, set to 1 if
143 a return statement that specifies a return value is seen. */
145 int current_function_returns_value;
147 /* Set to 0 at beginning of a function definition, set to 1 if
148 a return statement with no argument is seen. */
150 int current_function_returns_null;
152 /* Set to 0 at beginning of a function definition, set to 1 if
153 a call to a noreturn function is seen. */
155 int current_function_returns_abnormally;
157 /* Set to nonzero by `grokdeclarator' for a function
158 whose return type is defaulted, if warnings for this are desired. */
160 static int warn_about_return_type;
162 /* Nonzero when the current toplevel function contains a declaration
163 of a nested function which is never defined. */
165 static bool undef_nested_function;
167 /* True means global_bindings_p should return false even if the scope stack
168 says we are in file scope. */
169 bool c_override_global_bindings_to_false;
172 /* Each c_binding structure describes one binding of an identifier to
173 a decl. All the decls in a scope - irrespective of namespace - are
174 chained together by the ->prev field, which (as the name implies)
175 runs in reverse order. All the decls in a given namespace bound to
176 a given identifier are chained by the ->shadowed field, which runs
177 from inner to outer scopes.
179 The ->decl field usually points to a DECL node, but there are two
180 exceptions. In the namespace of type tags, the bound entity is a
181 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
182 identifier is encountered, it is bound to error_mark_node to
183 suppress further errors about that identifier in the current
186 The ->type field stores the type of the declaration in this scope;
187 if NULL, the type is the type of the ->decl field. This is only of
188 relevance for objects with external or internal linkage which may
189 be redeclared in inner scopes, forming composite types that only
190 persist for the duration of those scopes. In the external scope,
191 this stores the composite of all the types declared for this
192 object, visible or not. The ->inner_comp field (used only at file
193 scope) stores whether an incomplete array type at file scope was
194 completed at an inner scope to an array size other than 1.
196 The depth field is copied from the scope structure that holds this
197 decl. It is used to preserve the proper ordering of the ->shadowed
198 field (see bind()) and also for a handful of special-case checks.
199 Finally, the invisible bit is true for a decl which should be
200 ignored for purposes of normal name lookup, and the nested bit is
201 true for a decl that's been bound a second time in an inner scope;
202 in all such cases, the binding in the outer scope will have its
203 invisible bit true. */
205 struct c_binding GTY((chain_next ("%h.prev")))
207 tree decl; /* the decl bound */
208 tree type; /* the type in this scope */
209 tree id; /* the identifier it's bound to */
210 struct c_binding *prev; /* the previous decl in this scope */
211 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
212 unsigned int depth : 28; /* depth of this scope */
213 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
214 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
215 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
218 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
219 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
220 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
221 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
223 #define I_SYMBOL_BINDING(node) \
224 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
225 #define I_SYMBOL_DECL(node) \
226 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
228 #define I_TAG_BINDING(node) \
229 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
230 #define I_TAG_DECL(node) \
231 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
233 #define I_LABEL_BINDING(node) \
234 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
235 #define I_LABEL_DECL(node) \
236 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
238 /* Each C symbol points to three linked lists of c_binding structures.
239 These describe the values of the identifier in the three different
240 namespaces defined by the language. */
242 struct lang_identifier GTY(())
244 struct c_common_identifier common_id;
245 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
246 struct c_binding *tag_binding; /* struct/union/enum tags */
247 struct c_binding *label_binding; /* labels */
250 /* Validate c-lang.c's assumptions. */
251 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
252 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
254 /* The resulting tree type. */
257 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
258 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)")))
260 union tree_node GTY ((tag ("0"),
261 desc ("tree_node_structure (&%h)")))
263 struct lang_identifier GTY ((tag ("1"))) identifier;
266 /* Each c_scope structure describes the complete contents of one
267 scope. Four scopes are distinguished specially: the innermost or
268 current scope, the innermost function scope, the file scope (always
269 the second to outermost) and the outermost or external scope.
271 Most declarations are recorded in the current scope.
273 All normal label declarations are recorded in the innermost
274 function scope, as are bindings of undeclared identifiers to
275 error_mark_node. (GCC permits nested functions as an extension,
276 hence the 'innermost' qualifier.) Explicitly declared labels
277 (using the __label__ extension) appear in the current scope.
279 Being in the file scope (current_scope == file_scope) causes
280 special behavior in several places below. Also, under some
281 conditions the Objective-C front end records declarations in the
282 file scope even though that isn't the current scope.
284 All declarations with external linkage are recorded in the external
285 scope, even if they aren't visible there; this models the fact that
286 such declarations are visible to the entire program, and (with a
287 bit of cleverness, see pushdecl) allows diagnosis of some violations
288 of C99 6.2.2p7 and 6.2.7p2:
290 If, within the same translation unit, the same identifier appears
291 with both internal and external linkage, the behavior is
294 All declarations that refer to the same object or function shall
295 have compatible type; otherwise, the behavior is undefined.
297 Initially only the built-in declarations, which describe compiler
298 intrinsic functions plus a subset of the standard library, are in
301 The order of the blocks list matters, and it is frequently appended
302 to. To avoid having to walk all the way to the end of the list on
303 each insertion, or reverse the list later, we maintain a pointer to
304 the last list entry. (FIXME: It should be feasible to use a reversed
307 The bindings list is strictly in reverse order of declarations;
308 pop_scope relies on this. */
311 struct c_scope GTY((chain_next ("%h.outer")))
313 /* The scope containing this one. */
314 struct c_scope *outer;
316 /* The next outermost function scope. */
317 struct c_scope *outer_function;
319 /* All bindings in this scope. */
320 struct c_binding *bindings;
322 /* For each scope (except the global one), a chain of BLOCK nodes
323 for all the scopes that were entered and exited one level down. */
327 /* The depth of this scope. Used to keep the ->shadowed chain of
328 bindings sorted innermost to outermost. */
329 unsigned int depth : 28;
331 /* True if we are currently filling this scope with parameter
333 BOOL_BITFIELD parm_flag : 1;
335 /* True if we saw [*] in this scope. Used to give an error messages
336 if these appears in a function definition. */
337 BOOL_BITFIELD had_vla_unspec : 1;
339 /* True if we already complained about forward parameter decls
340 in this scope. This prevents double warnings on
341 foo (int a; int b; ...) */
342 BOOL_BITFIELD warned_forward_parm_decls : 1;
344 /* True if this is the outermost block scope of a function body.
345 This scope contains the parameters, the local variables declared
346 in the outermost block, and all the labels (except those in
347 nested functions, or declared at block scope with __label__). */
348 BOOL_BITFIELD function_body : 1;
350 /* True means make a BLOCK for this scope no matter what. */
351 BOOL_BITFIELD keep : 1;
354 /* The scope currently in effect. */
356 static GTY(()) struct c_scope *current_scope;
358 /* The innermost function scope. Ordinary (not explicitly declared)
359 labels, bindings to error_mark_node, and the lazily-created
360 bindings of __func__ and its friends get this scope. */
362 static GTY(()) struct c_scope *current_function_scope;
364 /* The C file scope. This is reset for each input translation unit. */
366 static GTY(()) struct c_scope *file_scope;
368 /* The outermost scope. This is used for all declarations with
369 external linkage, and only these, hence the name. */
371 static GTY(()) struct c_scope *external_scope;
373 /* A chain of c_scope structures awaiting reuse. */
375 static GTY((deletable)) struct c_scope *scope_freelist;
377 /* A chain of c_binding structures awaiting reuse. */
379 static GTY((deletable)) struct c_binding *binding_freelist;
381 /* Append VAR to LIST in scope SCOPE. */
382 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
383 struct c_scope *s_ = (scope); \
385 if (s_->list##_last) \
386 TREE_CHAIN (s_->list##_last) = d_; \
389 s_->list##_last = d_; \
392 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
393 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
394 struct c_scope *t_ = (tscope); \
395 struct c_scope *f_ = (fscope); \
397 TREE_CHAIN (t_->to##_last) = f_->from; \
400 t_->to##_last = f_->from##_last; \
403 /* True means unconditionally make a BLOCK for the next scope pushed. */
405 static bool keep_next_level_flag;
407 /* True means the next call to push_scope will be the outermost scope
408 of a function body, so do not push a new scope, merely cease
409 expecting parameter decls. */
411 static bool next_is_function_body;
413 /* Functions called automatically at the beginning and end of execution. */
415 static GTY(()) tree static_ctors;
416 static GTY(()) tree static_dtors;
418 /* Forward declarations. */
419 static tree lookup_name_in_scope (tree, struct c_scope *);
420 static tree c_make_fname_decl (tree, int);
421 static tree grokdeclarator (const struct c_declarator *,
422 struct c_declspecs *,
423 enum decl_context, bool, tree *);
424 static tree grokparms (struct c_arg_info *, bool);
425 static void layout_array_type (tree);
427 /* T is a statement. Add it to the statement-tree. This is the
428 C/ObjC version--C++ has a slightly different version of this
434 enum tree_code code = TREE_CODE (t);
436 if (EXPR_P (t) && code != LABEL_EXPR)
438 if (!EXPR_HAS_LOCATION (t))
439 SET_EXPR_LOCATION (t, input_location);
442 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
443 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
445 /* Add T to the statement-tree. Non-side-effect statements need to be
446 recorded during statement expressions. */
447 append_to_statement_list_force (t, &cur_stmt_list);
452 /* States indicating how grokdeclarator() should handle declspecs marked
453 with __attribute__((deprecated)). An object declared as
454 __attribute__((deprecated)) suppresses warnings of uses of other
456 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
457 /* Also add an __attribute__((unavailable)). An object declared as
458 __attribute__((unavailable)) suppresses any reports of being
459 declared with unavailable or deprecated items. */
460 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
462 enum deprecated_states {
465 /* APPLE LOCAL "unavailable" attribute (radar 2809697) */
466 , DEPRECATED_UNAVAILABLE_SUPPRESS
469 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
472 c_print_identifier (FILE *file, tree node, int indent)
474 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
475 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
476 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
477 if (C_IS_RESERVED_WORD (node))
479 tree rid = ridpointers[C_RID_CODE (node)];
480 indent_to (file, indent + 4);
481 fprintf (file, "rid %p \"%s\"",
482 (void *) rid, IDENTIFIER_POINTER (rid));
486 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
487 which may be any of several kinds of DECL or TYPE or error_mark_node,
488 in the scope SCOPE. */
490 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
492 struct c_binding *b, **here;
494 if (binding_freelist)
496 b = binding_freelist;
497 binding_freelist = b->prev;
500 b = GGC_NEW (struct c_binding);
505 b->depth = scope->depth;
506 b->invisible = invisible;
512 b->prev = scope->bindings;
518 switch (TREE_CODE (decl))
520 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
523 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
529 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
535 /* Locate the appropriate place in the chain of shadowed decls
536 to insert this binding. Normally, scope == current_scope and
537 this does nothing. */
538 while (*here && (*here)->depth > scope->depth)
539 here = &(*here)->shadowed;
545 /* Clear the binding structure B, stick it on the binding_freelist,
546 and return the former value of b->prev. This is used by pop_scope
547 and get_parm_info to iterate destructively over all the bindings
548 from a given scope. */
549 static struct c_binding *
550 free_binding_and_advance (struct c_binding *b)
552 struct c_binding *prev = b->prev;
554 memset (b, 0, sizeof (struct c_binding));
555 b->prev = binding_freelist;
556 binding_freelist = b;
562 /* Hook called at end of compilation to assume 1 elt
563 for a file-scope tentative array defn that wasn't complete before. */
566 c_finish_incomplete_decl (tree decl)
568 if (TREE_CODE (decl) == VAR_DECL)
570 tree type = TREE_TYPE (decl);
571 if (type != error_mark_node
572 && TREE_CODE (type) == ARRAY_TYPE
573 && !DECL_EXTERNAL (decl)
574 && TYPE_DOMAIN (type) == 0)
576 warning (0, "array %q+D assumed to have one element", decl);
578 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
580 layout_decl (decl, 0);
585 /* The Objective-C front-end often needs to determine the current scope. */
588 objc_get_current_scope (void)
590 return current_scope;
593 /* The following function is used only by Objective-C. It needs to live here
594 because it accesses the innards of c_scope. */
597 objc_mark_locals_volatile (void *enclosing_blk)
599 struct c_scope *scope;
602 for (scope = current_scope;
603 scope && scope != enclosing_blk;
604 scope = scope->outer)
606 for (b = scope->bindings; b; b = b->prev)
607 objc_volatilize_decl (b->decl);
609 /* Do not climb up past the current function. */
610 if (scope->function_body)
615 /* Nonzero if we are currently in file scope. */
618 global_bindings_p (void)
620 return current_scope == file_scope && !c_override_global_bindings_to_false;
624 keep_next_level (void)
626 keep_next_level_flag = true;
629 /* Identify this scope as currently being filled with parameters. */
632 declare_parm_level (void)
634 current_scope->parm_flag = true;
640 if (next_is_function_body)
642 /* This is the transition from the parameters to the top level
643 of the function body. These are the same scope
644 (C99 6.2.1p4,6) so we do not push another scope structure.
645 next_is_function_body is set only by store_parm_decls, which
646 in turn is called when and only when we are about to
647 encounter the opening curly brace for the function body.
649 The outermost block of a function always gets a BLOCK node,
650 because the debugging output routines expect that each
651 function has at least one BLOCK. */
652 current_scope->parm_flag = false;
653 current_scope->function_body = true;
654 current_scope->keep = true;
655 current_scope->outer_function = current_function_scope;
656 current_function_scope = current_scope;
658 keep_next_level_flag = false;
659 next_is_function_body = false;
663 struct c_scope *scope;
666 scope = scope_freelist;
667 scope_freelist = scope->outer;
670 scope = GGC_CNEW (struct c_scope);
672 scope->keep = keep_next_level_flag;
673 scope->outer = current_scope;
674 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
676 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
678 if (current_scope && scope->depth == 0)
681 sorry ("GCC supports only %u nested scopes", scope->depth);
684 current_scope = scope;
685 keep_next_level_flag = false;
689 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
692 set_type_context (tree type, tree context)
694 for (type = TYPE_MAIN_VARIANT (type); type;
695 type = TYPE_NEXT_VARIANT (type))
696 TYPE_CONTEXT (type) = context;
699 /* Exit a scope. Restore the state of the identifier-decl mappings
700 that were in effect when this scope was entered. Return a BLOCK
701 node containing all the DECLs in this scope that are of interest
702 to debug info generation. */
707 struct c_scope *scope = current_scope;
708 tree block, context, p;
711 bool functionbody = scope->function_body;
712 bool keep = functionbody || scope->keep || scope->bindings;
714 c_end_vm_scope (scope->depth);
716 /* If appropriate, create a BLOCK to record the decls for the life
721 block = make_node (BLOCK);
722 BLOCK_SUBBLOCKS (block) = scope->blocks;
723 TREE_USED (block) = 1;
725 /* In each subblock, record that this is its superior. */
726 for (p = scope->blocks; p; p = TREE_CHAIN (p))
727 BLOCK_SUPERCONTEXT (p) = block;
729 BLOCK_VARS (block) = 0;
732 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
733 scope must be set so that they point to the appropriate
734 construct, i.e. either to the current FUNCTION_DECL node, or
735 else to the BLOCK node we just constructed.
737 Note that for tagged types whose scope is just the formal
738 parameter list for some function type specification, we can't
739 properly set their TYPE_CONTEXTs here, because we don't have a
740 pointer to the appropriate FUNCTION_TYPE node readily available
741 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
742 type nodes get set in `grokdeclarator' as soon as we have created
743 the FUNCTION_TYPE node which will represent the "scope" for these
744 "parameter list local" tagged types. */
745 if (scope->function_body)
746 context = current_function_decl;
747 else if (scope == file_scope)
749 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
750 TREE_CHAIN (file_decl) = all_translation_units;
751 all_translation_units = file_decl;
757 /* Clear all bindings in this scope. */
758 for (b = scope->bindings; b; b = free_binding_and_advance (b))
761 switch (TREE_CODE (p))
764 /* Warnings for unused labels, errors for undefined labels. */
765 if (TREE_USED (p) && !DECL_INITIAL (p))
767 error ("label %q+D used but not defined", p);
768 DECL_INITIAL (p) = error_mark_node;
770 else if (!TREE_USED (p) && warn_unused_label)
772 if (DECL_INITIAL (p))
773 warning (0, "label %q+D defined but not used", p);
775 warning (0, "label %q+D declared but not defined", p);
777 /* Labels go in BLOCK_VARS. */
778 TREE_CHAIN (p) = BLOCK_VARS (block);
779 BLOCK_VARS (block) = p;
780 gcc_assert (I_LABEL_BINDING (b->id) == b);
781 I_LABEL_BINDING (b->id) = b->shadowed;
787 set_type_context (p, context);
789 /* Types may not have tag-names, in which case the type
790 appears in the bindings list with b->id NULL. */
793 gcc_assert (I_TAG_BINDING (b->id) == b);
794 I_TAG_BINDING (b->id) = b->shadowed;
799 /* Propagate TREE_ADDRESSABLE from nested functions to their
800 containing functions. */
801 if (!TREE_ASM_WRITTEN (p)
802 && DECL_INITIAL (p) != 0
803 && TREE_ADDRESSABLE (p)
804 && DECL_ABSTRACT_ORIGIN (p) != 0
805 && DECL_ABSTRACT_ORIGIN (p) != p)
806 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
807 if (!DECL_EXTERNAL (p)
808 && DECL_INITIAL (p) == 0
809 && scope != file_scope
810 && scope != external_scope)
812 error ("nested function %q+D declared but never defined", p);
813 undef_nested_function = true;
815 /* C99 6.7.4p6: "a function with external linkage... declared
816 with an inline function specifier ... shall also be defined in the
817 same translation unit." */
818 else if (DECL_DECLARED_INLINE_P (p)
821 && !flag_gnu89_inline)
822 pedwarn ("inline function %q+D declared but never defined", p);
827 /* Warnings for unused variables. */
829 && !TREE_NO_WARNING (p)
830 && !DECL_IN_SYSTEM_HEADER (p)
832 && !DECL_ARTIFICIAL (p)
833 && scope != file_scope
834 && scope != external_scope)
835 warning (OPT_Wunused_variable, "unused variable %q+D", p);
839 error ("type of array %q+D completed incompatibly with"
840 " implicit initialization", p);
847 /* All of these go in BLOCK_VARS, but only if this is the
848 binding in the home scope. */
851 TREE_CHAIN (p) = BLOCK_VARS (block);
852 BLOCK_VARS (block) = p;
854 /* If this is the file scope, and we are processing more
855 than one translation unit in this compilation, set
856 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
857 This makes same_translation_unit_p work, and causes
858 static declarations to be given disambiguating suffixes. */
859 if (scope == file_scope && num_in_fnames > 1)
861 DECL_CONTEXT (p) = context;
862 if (TREE_CODE (p) == TYPE_DECL)
863 set_type_context (TREE_TYPE (p), context);
867 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
868 already been put there by store_parm_decls. Unused-
869 parameter warnings are handled by function.c.
870 error_mark_node obviously does not go in BLOCK_VARS and
871 does not get unused-variable warnings. */
874 /* It is possible for a decl not to have a name. We get
875 here with b->id NULL in this case. */
878 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
879 I_SYMBOL_BINDING (b->id) = b->shadowed;
880 if (b->shadowed && b->shadowed->type)
881 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
891 /* Dispose of the block that we just made inside some higher level. */
892 if ((scope->function_body || scope == file_scope) && context)
894 DECL_INITIAL (context) = block;
895 BLOCK_SUPERCONTEXT (block) = context;
897 else if (scope->outer)
900 SCOPE_LIST_APPEND (scope->outer, blocks, block);
901 /* If we did not make a block for the scope just exited, any
902 blocks made for inner scopes must be carried forward so they
903 will later become subblocks of something else. */
904 else if (scope->blocks)
905 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
908 /* Pop the current scope, and free the structure for reuse. */
909 current_scope = scope->outer;
910 if (scope->function_body)
911 current_function_scope = scope->outer_function;
913 memset (scope, 0, sizeof (struct c_scope));
914 scope->outer = scope_freelist;
915 scope_freelist = scope;
921 push_file_scope (void)
929 file_scope = current_scope;
931 start_fname_decls ();
933 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
934 bind (DECL_NAME (decl), decl, file_scope,
935 /*invisible=*/false, /*nested=*/true);
939 pop_file_scope (void)
941 /* In case there were missing closebraces, get us back to the global
943 while (current_scope != file_scope)
946 /* __FUNCTION__ is defined at file scope (""). This
947 call may not be necessary as my tests indicate it
948 still works without it. */
949 finish_fname_decls ();
951 /* This is the point to write out a PCH if we're doing that.
952 In that case we do not want to do anything else. */
955 c_common_write_pch ();
959 /* Pop off the file scope and close this translation unit. */
963 maybe_apply_pending_pragma_weaks ();
964 cgraph_finalize_compilation_unit ();
967 /* Insert BLOCK at the end of the list of subblocks of the current
968 scope. This is used when a BIND_EXPR is expanded, to handle the
969 BLOCK node inside the BIND_EXPR. */
972 insert_block (tree block)
974 TREE_USED (block) = 1;
975 SCOPE_LIST_APPEND (current_scope, blocks, block);
978 /* Push a definition or a declaration of struct, union or enum tag "name".
979 "type" should be the type node.
980 We assume that the tag "name" is not already defined.
982 Note that the definition may really be just a forward reference.
983 In that case, the TYPE_SIZE will be zero. */
986 pushtag (tree name, tree type)
988 /* Record the identifier as the type's name if it has none. */
989 if (name && !TYPE_NAME (type))
990 TYPE_NAME (type) = name;
991 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
993 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
994 tagged type we just added to the current scope. This fake
995 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
996 to output a representation of a tagged type, and it also gives
997 us a convenient place to record the "scope start" address for the
1000 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
1002 /* An approximation for now, so we can tell this is a function-scope tag.
1003 This will be updated in pop_scope. */
1004 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1007 /* Subroutine of compare_decls. Allow harmless mismatches in return
1008 and argument types provided that the type modes match. This function
1009 return a unified type given a suitable match, and 0 otherwise. */
1012 match_builtin_function_types (tree newtype, tree oldtype)
1014 tree newrettype, oldrettype;
1015 tree newargs, oldargs;
1016 tree trytype, tryargs;
1018 /* Accept the return type of the new declaration if same modes. */
1019 oldrettype = TREE_TYPE (oldtype);
1020 newrettype = TREE_TYPE (newtype);
1022 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1025 oldargs = TYPE_ARG_TYPES (oldtype);
1026 newargs = TYPE_ARG_TYPES (newtype);
1029 while (oldargs || newargs)
1033 || !TREE_VALUE (oldargs)
1034 || !TREE_VALUE (newargs)
1035 || TYPE_MODE (TREE_VALUE (oldargs))
1036 != TYPE_MODE (TREE_VALUE (newargs)))
1039 oldargs = TREE_CHAIN (oldargs);
1040 newargs = TREE_CHAIN (newargs);
1043 trytype = build_function_type (newrettype, tryargs);
1044 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1047 /* Subroutine of diagnose_mismatched_decls. Check for function type
1048 mismatch involving an empty arglist vs a nonempty one and give clearer
1051 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1052 tree newtype, tree oldtype)
1056 if (TREE_CODE (olddecl) != FUNCTION_DECL
1057 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1058 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1060 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1063 t = TYPE_ARG_TYPES (oldtype);
1065 t = TYPE_ARG_TYPES (newtype);
1066 for (; t; t = TREE_CHAIN (t))
1068 tree type = TREE_VALUE (t);
1070 if (TREE_CHAIN (t) == 0
1071 && TYPE_MAIN_VARIANT (type) != void_type_node)
1073 inform ("a parameter list with an ellipsis can%'t match "
1074 "an empty parameter name list declaration");
1078 if (c_type_promotes_to (type) != type)
1080 inform ("an argument type that has a default promotion can%'t match "
1081 "an empty parameter name list declaration");
1087 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1088 old-style function definition, NEWDECL is a prototype declaration.
1089 Diagnose inconsistencies in the argument list. Returns TRUE if
1090 the prototype is compatible, FALSE if not. */
1092 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1094 tree newargs, oldargs;
1097 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1099 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1100 newargs = TYPE_ARG_TYPES (newtype);
1105 tree oldargtype = TREE_VALUE (oldargs);
1106 tree newargtype = TREE_VALUE (newargs);
1108 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1111 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1112 newargtype = TYPE_MAIN_VARIANT (newargtype);
1114 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1117 /* Reaching the end of just one list means the two decls don't
1118 agree on the number of arguments. */
1119 if (END_OF_ARGLIST (oldargtype))
1121 error ("prototype for %q+D declares more arguments "
1122 "than previous old-style definition", newdecl);
1125 else if (END_OF_ARGLIST (newargtype))
1127 error ("prototype for %q+D declares fewer arguments "
1128 "than previous old-style definition", newdecl);
1132 /* Type for passing arg must be consistent with that declared
1134 else if (!comptypes (oldargtype, newargtype))
1136 error ("prototype for %q+D declares argument %d"
1137 " with incompatible type",
1142 oldargs = TREE_CHAIN (oldargs);
1143 newargs = TREE_CHAIN (newargs);
1147 /* If we get here, no errors were found, but do issue a warning
1148 for this poor-style construct. */
1149 warning (0, "prototype for %q+D follows non-prototype definition",
1152 #undef END_OF_ARGLIST
1155 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1156 first in a pair of mismatched declarations, using the diagnostic
1159 locate_old_decl (tree decl, void (*diag)(const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2))
1161 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1163 else if (DECL_INITIAL (decl))
1164 diag (G_("previous definition of %q+D was here"), decl);
1165 else if (C_DECL_IMPLICIT (decl))
1166 diag (G_("previous implicit declaration of %q+D was here"), decl);
1168 diag (G_("previous declaration of %q+D was here"), decl);
1171 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1172 Returns true if the caller should proceed to merge the two, false
1173 if OLDDECL should simply be discarded. As a side effect, issues
1174 all necessary diagnostics for invalid or poor-style combinations.
1175 If it returns true, writes the types of NEWDECL and OLDDECL to
1176 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1177 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1180 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1181 tree *newtypep, tree *oldtypep)
1183 tree newtype, oldtype;
1184 bool pedwarned = false;
1185 bool warned = false;
1188 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1189 && DECL_EXTERNAL (DECL))
1191 /* If we have error_mark_node for either decl or type, just discard
1192 the previous decl - we're in an error cascade already. */
1193 if (olddecl == error_mark_node || newdecl == error_mark_node)
1195 *oldtypep = oldtype = TREE_TYPE (olddecl);
1196 *newtypep = newtype = TREE_TYPE (newdecl);
1197 if (oldtype == error_mark_node || newtype == error_mark_node)
1200 /* Two different categories of symbol altogether. This is an error
1201 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1202 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1204 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1205 && DECL_BUILT_IN (olddecl)
1206 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1208 error ("%q+D redeclared as different kind of symbol", newdecl);
1209 locate_old_decl (olddecl, error);
1211 else if (TREE_PUBLIC (newdecl))
1212 warning (0, "built-in function %q+D declared as non-function",
1215 warning (OPT_Wshadow, "declaration of %q+D shadows "
1216 "a built-in function", newdecl);
1220 /* Enumerators have no linkage, so may only be declared once in a
1222 if (TREE_CODE (olddecl) == CONST_DECL)
1224 error ("redeclaration of enumerator %q+D", newdecl);
1225 locate_old_decl (olddecl, error);
1229 if (!comptypes (oldtype, newtype))
1231 if (TREE_CODE (olddecl) == FUNCTION_DECL
1232 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1234 /* Accept harmless mismatch in function types.
1235 This is for the ffs and fprintf builtins. */
1236 tree trytype = match_builtin_function_types (newtype, oldtype);
1238 if (trytype && comptypes (newtype, trytype))
1239 *oldtypep = oldtype = trytype;
1242 /* If types don't match for a built-in, throw away the
1243 built-in. No point in calling locate_old_decl here, it
1244 won't print anything. */
1245 warning (0, "conflicting types for built-in function %q+D",
1250 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1251 && DECL_IS_BUILTIN (olddecl))
1253 /* A conflicting function declaration for a predeclared
1254 function that isn't actually built in. Objective C uses
1255 these. The new declaration silently overrides everything
1256 but the volatility (i.e. noreturn) indication. See also
1257 below. FIXME: Make Objective C use normal builtins. */
1258 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1261 /* Permit void foo (...) to match int foo (...) if the latter is
1262 the definition and implicit int was used. See
1263 c-torture/compile/920625-2.c. */
1264 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1265 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1266 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1267 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1269 pedwarn ("conflicting types for %q+D", newdecl);
1270 /* Make sure we keep void as the return type. */
1271 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1272 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1275 /* Permit void foo (...) to match an earlier call to foo (...) with
1276 no declared type (thus, implicitly int). */
1277 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1278 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1279 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1280 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1282 pedwarn ("conflicting types for %q+D", newdecl);
1283 /* Make sure we keep void as the return type. */
1284 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1289 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1290 error ("conflicting type qualifiers for %q+D", newdecl);
1292 error ("conflicting types for %q+D", newdecl);
1293 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1294 locate_old_decl (olddecl, error);
1299 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1300 but silently ignore the redeclaration if either is in a system
1301 header. (Conflicting redeclarations were handled above.) */
1302 if (TREE_CODE (newdecl) == TYPE_DECL)
1304 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1305 return true; /* Allow OLDDECL to continue in use. */
1307 error ("redefinition of typedef %q+D", newdecl);
1308 locate_old_decl (olddecl, error);
1312 /* Function declarations can either be 'static' or 'extern' (no
1313 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1314 can never conflict with each other on account of linkage
1315 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1316 gnu89 mode permits two definitions if one is 'extern inline' and
1317 one is not. The non- extern-inline definition supersedes the
1318 extern-inline definition. */
1320 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1322 /* If you declare a built-in function name as static, or
1323 define the built-in with an old-style definition (so we
1324 can't validate the argument list) the built-in definition is
1325 overridden, but optionally warn this was a bad choice of name. */
1326 if (DECL_BUILT_IN (olddecl)
1327 && !C_DECL_DECLARED_BUILTIN (olddecl)
1328 && (!TREE_PUBLIC (newdecl)
1329 || (DECL_INITIAL (newdecl)
1330 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1332 warning (OPT_Wshadow, "declaration of %q+D shadows "
1333 "a built-in function", newdecl);
1334 /* Discard the old built-in function. */
1338 if (DECL_INITIAL (newdecl))
1340 if (DECL_INITIAL (olddecl))
1342 /* If both decls are in the same TU and the new declaration
1343 isn't overriding an extern inline reject the new decl.
1344 In c99, no overriding is allowed in the same translation
1346 if ((!DECL_EXTERN_INLINE (olddecl)
1347 || DECL_EXTERN_INLINE (newdecl)
1348 || (!flag_gnu89_inline
1349 && (!DECL_DECLARED_INLINE_P (olddecl)
1350 || !lookup_attribute ("gnu_inline",
1351 DECL_ATTRIBUTES (olddecl)))
1352 && (!DECL_DECLARED_INLINE_P (newdecl)
1353 || !lookup_attribute ("gnu_inline",
1354 DECL_ATTRIBUTES (newdecl))))
1356 && same_translation_unit_p (newdecl, olddecl))
1358 error ("redefinition of %q+D", newdecl);
1359 locate_old_decl (olddecl, error);
1364 /* If we have a prototype after an old-style function definition,
1365 the argument types must be checked specially. */
1366 else if (DECL_INITIAL (olddecl)
1367 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1368 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1369 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1371 locate_old_decl (olddecl, error);
1374 /* A non-static declaration (even an "extern") followed by a
1375 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1376 The same is true for a static forward declaration at block
1377 scope followed by a non-static declaration/definition at file
1378 scope. Static followed by non-static at the same scope is
1379 not undefined behavior, and is the most convenient way to get
1380 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1381 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1382 we do diagnose it if -Wtraditional. */
1383 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1385 /* Two exceptions to the rule. If olddecl is an extern
1386 inline, or a predeclared function that isn't actually
1387 built in, newdecl silently overrides olddecl. The latter
1388 occur only in Objective C; see also above. (FIXME: Make
1389 Objective C use normal builtins.) */
1390 if (!DECL_IS_BUILTIN (olddecl)
1391 && !DECL_EXTERN_INLINE (olddecl))
1393 error ("static declaration of %q+D follows "
1394 "non-static declaration", newdecl);
1395 locate_old_decl (olddecl, error);
1399 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1401 if (DECL_CONTEXT (olddecl))
1403 error ("non-static declaration of %q+D follows "
1404 "static declaration", newdecl);
1405 locate_old_decl (olddecl, error);
1408 else if (warn_traditional)
1410 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1411 "follows static declaration", newdecl);
1416 /* Make sure gnu_inline attribute is either not present, or
1417 present on all inline decls. */
1418 if (DECL_DECLARED_INLINE_P (olddecl)
1419 && DECL_DECLARED_INLINE_P (newdecl))
1421 bool newa = lookup_attribute ("gnu_inline",
1422 DECL_ATTRIBUTES (newdecl)) != NULL;
1423 bool olda = lookup_attribute ("gnu_inline",
1424 DECL_ATTRIBUTES (olddecl)) != NULL;
1427 error ("%<gnu_inline%> attribute present on %q+D",
1428 newa ? newdecl : olddecl);
1429 error ("%Jbut not here", newa ? olddecl : newdecl);
1433 else if (TREE_CODE (newdecl) == VAR_DECL)
1435 /* Only variables can be thread-local, and all declarations must
1436 agree on this property. */
1437 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1439 /* Nothing to check. Since OLDDECL is marked threadprivate
1440 and NEWDECL does not have a thread-local attribute, we
1441 will merge the threadprivate attribute into NEWDECL. */
1444 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1446 if (DECL_THREAD_LOCAL_P (newdecl))
1447 error ("thread-local declaration of %q+D follows "
1448 "non-thread-local declaration", newdecl);
1450 error ("non-thread-local declaration of %q+D follows "
1451 "thread-local declaration", newdecl);
1453 locate_old_decl (olddecl, error);
1457 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1458 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1460 error ("redefinition of %q+D", newdecl);
1461 locate_old_decl (olddecl, error);
1465 /* Objects declared at file scope: if the first declaration had
1466 external linkage (even if it was an external reference) the
1467 second must have external linkage as well, or the behavior is
1468 undefined. If the first declaration had internal linkage, then
1469 the second must too, or else be an external reference (in which
1470 case the composite declaration still has internal linkage).
1471 As for function declarations, we warn about the static-then-
1472 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1473 if (DECL_FILE_SCOPE_P (newdecl)
1474 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1476 if (DECL_EXTERNAL (newdecl))
1478 if (!DECL_FILE_SCOPE_P (olddecl))
1480 error ("extern declaration of %q+D follows "
1481 "declaration with no linkage", newdecl);
1482 locate_old_decl (olddecl, error);
1485 else if (warn_traditional)
1487 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1488 "follows static declaration", newdecl);
1494 if (TREE_PUBLIC (newdecl))
1495 error ("non-static declaration of %q+D follows "
1496 "static declaration", newdecl);
1498 error ("static declaration of %q+D follows "
1499 "non-static declaration", newdecl);
1501 locate_old_decl (olddecl, error);
1505 /* Two objects with the same name declared at the same block
1506 scope must both be external references (6.7p3). */
1507 else if (!DECL_FILE_SCOPE_P (newdecl))
1509 if (DECL_EXTERNAL (newdecl))
1511 /* Extern with initializer at block scope, which will
1512 already have received an error. */
1514 else if (DECL_EXTERNAL (olddecl))
1516 error ("declaration of %q+D with no linkage follows "
1517 "extern declaration", newdecl);
1518 locate_old_decl (olddecl, error);
1522 error ("redeclaration of %q+D with no linkage", newdecl);
1523 locate_old_decl (olddecl, error);
1531 /* All decls must agree on a visibility. */
1532 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1533 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1534 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1536 warning (0, "redeclaration of %q+D with different visibility "
1537 "(old visibility preserved)", newdecl);
1541 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1543 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1544 if (DECL_DECLARED_INLINE_P (newdecl)
1545 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1547 warning (OPT_Wattributes, "inline declaration of %qD follows "
1548 "declaration with attribute noinline", newdecl);
1551 else if (DECL_DECLARED_INLINE_P (olddecl)
1552 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1554 warning (OPT_Wattributes, "declaration of %q+D with attribute "
1555 "noinline follows inline declaration ", newdecl);
1559 /* Inline declaration after use or definition.
1560 ??? Should we still warn about this now we have unit-at-a-time
1561 mode and can get it right?
1562 Definitely don't complain if the decls are in different translation
1564 C99 permits this, so don't warn in that case. (The function
1565 may not be inlined everywhere in function-at-a-time mode, but
1566 we still shouldn't warn.) */
1567 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1568 && same_translation_unit_p (olddecl, newdecl)
1569 && flag_gnu89_inline)
1571 if (TREE_USED (olddecl))
1573 warning (0, "%q+D declared inline after being called", olddecl);
1576 else if (DECL_INITIAL (olddecl))
1578 warning (0, "%q+D declared inline after its definition", olddecl);
1583 else /* PARM_DECL, VAR_DECL */
1585 /* Redeclaration of a parameter is a constraint violation (this is
1586 not explicitly stated, but follows from C99 6.7p3 [no more than
1587 one declaration of the same identifier with no linkage in the
1588 same scope, except type tags] and 6.2.2p6 [parameters have no
1589 linkage]). We must check for a forward parameter declaration,
1590 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1591 an extension, the mandatory diagnostic for which is handled by
1592 mark_forward_parm_decls. */
1594 if (TREE_CODE (newdecl) == PARM_DECL
1595 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1597 error ("redefinition of parameter %q+D", newdecl);
1598 locate_old_decl (olddecl, error);
1603 /* Optional warning for completely redundant decls. */
1604 if (!warned && !pedwarned
1605 && warn_redundant_decls
1606 /* Don't warn about a function declaration followed by a
1608 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1609 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1610 /* Don't warn about redundant redeclarations of builtins. */
1611 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1612 && !DECL_BUILT_IN (newdecl)
1613 && DECL_BUILT_IN (olddecl)
1614 && !C_DECL_DECLARED_BUILTIN (olddecl))
1615 /* Don't warn about an extern followed by a definition. */
1616 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1617 /* Don't warn about forward parameter decls. */
1618 && !(TREE_CODE (newdecl) == PARM_DECL
1619 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1620 /* Don't warn about a variable definition following a declaration. */
1621 && !(TREE_CODE (newdecl) == VAR_DECL
1622 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1624 warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1629 /* Report location of previous decl/defn in a consistent manner. */
1630 if (warned || pedwarned)
1631 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1633 #undef DECL_EXTERN_INLINE
1638 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1639 consistent with OLDDECL, but carries new information. Merge the
1640 new information into OLDDECL. This function issues no
1644 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1646 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1647 && DECL_INITIAL (newdecl) != 0);
1648 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1649 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1650 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1651 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1652 bool extern_changed = false;
1654 /* For real parm decl following a forward decl, rechain the old decl
1655 in its new location and clear TREE_ASM_WRITTEN (it's not a
1656 forward decl anymore). */
1657 if (TREE_CODE (newdecl) == PARM_DECL
1658 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1660 struct c_binding *b, **here;
1662 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
1663 if ((*here)->decl == olddecl)
1670 b->prev = current_scope->bindings;
1671 current_scope->bindings = b;
1673 TREE_ASM_WRITTEN (olddecl) = 0;
1676 DECL_ATTRIBUTES (newdecl)
1677 = targetm.merge_decl_attributes (olddecl, newdecl);
1679 /* Merge the data types specified in the two decls. */
1681 = TREE_TYPE (olddecl)
1682 = composite_type (newtype, oldtype);
1684 /* Lay the type out, unless already done. */
1685 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1687 if (TREE_TYPE (newdecl) != error_mark_node)
1688 layout_type (TREE_TYPE (newdecl));
1689 if (TREE_CODE (newdecl) != FUNCTION_DECL
1690 && TREE_CODE (newdecl) != TYPE_DECL
1691 && TREE_CODE (newdecl) != CONST_DECL)
1692 layout_decl (newdecl, 0);
1696 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1697 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1698 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1699 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1700 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1702 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1703 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1708 /* Merge the type qualifiers. */
1709 if (TREE_READONLY (newdecl))
1710 TREE_READONLY (olddecl) = 1;
1712 if (TREE_THIS_VOLATILE (newdecl))
1713 TREE_THIS_VOLATILE (olddecl) = 1;
1715 /* Merge deprecatedness. */
1716 if (TREE_DEPRECATED (newdecl))
1717 TREE_DEPRECATED (olddecl) = 1;
1719 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
1720 /* Merge unavailableness. */
1721 if (TREE_UNAVAILABLE (newdecl))
1722 TREE_UNAVAILABLE (olddecl) = 1;
1723 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
1725 /* Keep source location of definition rather than declaration and of
1726 prototype rather than non-prototype unless that prototype is
1728 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1729 || (old_is_prototype && !new_is_prototype
1730 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1731 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1733 /* Merge the initialization information. */
1734 if (DECL_INITIAL (newdecl) == 0)
1735 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1737 /* Merge the threadprivate attribute. */
1738 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1740 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1741 C_DECL_THREADPRIVATE_P (newdecl) = 1;
1744 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1746 /* Merge the unused-warning information. */
1747 if (DECL_IN_SYSTEM_HEADER (olddecl))
1748 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1749 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1750 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1752 /* Merge the section attribute.
1753 We want to issue an error if the sections conflict but that
1754 must be done later in decl_attributes since we are called
1755 before attributes are assigned. */
1756 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1757 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1759 /* Copy the assembler name.
1760 Currently, it can only be defined in the prototype. */
1761 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1763 /* Use visibility of whichever declaration had it specified */
1764 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1766 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1767 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1770 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1772 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1773 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1774 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1775 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1776 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1777 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1778 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1779 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1780 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1781 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1784 /* Merge the storage class information. */
1785 merge_weak (newdecl, olddecl);
1787 /* For functions, static overrides non-static. */
1788 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1790 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1791 /* This is since we don't automatically
1792 copy the attributes of NEWDECL into OLDDECL. */
1793 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1794 /* If this clears `static', clear it in the identifier too. */
1795 if (!TREE_PUBLIC (olddecl))
1796 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1800 /* In c99, 'extern' declaration before (or after) 'inline' means this
1801 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1803 if (TREE_CODE (newdecl) == FUNCTION_DECL
1804 && !flag_gnu89_inline
1805 && (DECL_DECLARED_INLINE_P (newdecl)
1806 || DECL_DECLARED_INLINE_P (olddecl))
1807 && (!DECL_DECLARED_INLINE_P (newdecl)
1808 || !DECL_DECLARED_INLINE_P (olddecl)
1809 || !DECL_EXTERNAL (olddecl))
1810 && DECL_EXTERNAL (newdecl)
1811 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl)))
1812 DECL_EXTERNAL (newdecl) = 0;
1814 if (DECL_EXTERNAL (newdecl))
1816 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1817 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1819 /* An extern decl does not override previous storage class. */
1820 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1821 if (!DECL_EXTERNAL (newdecl))
1823 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1824 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1829 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1830 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1833 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1835 /* If we're redefining a function previously defined as extern
1836 inline, make sure we emit debug info for the inline before we
1837 throw it away, in case it was inlined into a function that
1838 hasn't been written out yet. */
1839 if (new_is_definition && DECL_INITIAL (olddecl))
1841 if (TREE_USED (olddecl)
1842 /* In unit-at-a-time mode we never inline re-defined extern
1843 inline functions. */
1844 && !flag_unit_at_a_time
1845 && cgraph_function_possibly_inlined_p (olddecl))
1846 (*debug_hooks->outlining_inline_function) (olddecl);
1848 /* The new defn must not be inline. */
1849 DECL_INLINE (newdecl) = 0;
1850 DECL_UNINLINABLE (newdecl) = 1;
1854 /* If either decl says `inline', this fn is inline, unless
1855 its definition was passed already. */
1856 if (DECL_DECLARED_INLINE_P (newdecl)
1857 || DECL_DECLARED_INLINE_P (olddecl))
1858 DECL_DECLARED_INLINE_P (newdecl) = 1;
1860 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1861 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1864 if (DECL_BUILT_IN (olddecl))
1866 /* If redeclaring a builtin function, it stays built in.
1867 But it gets tagged as having been declared. */
1868 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1869 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1870 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1871 if (new_is_prototype)
1872 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1874 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1875 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1878 /* Also preserve various other info from the definition. */
1879 if (!new_is_definition)
1881 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1882 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1883 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1884 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1885 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1887 /* Set DECL_INLINE on the declaration if we've got a body
1888 from which to instantiate. */
1889 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1891 DECL_INLINE (newdecl) = 1;
1892 DECL_ABSTRACT_ORIGIN (newdecl)
1893 = DECL_ABSTRACT_ORIGIN (olddecl);
1898 /* If a previous declaration said inline, mark the
1899 definition as inlinable. */
1900 if (DECL_DECLARED_INLINE_P (newdecl)
1901 && !DECL_UNINLINABLE (newdecl))
1902 DECL_INLINE (newdecl) = 1;
1906 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
1908 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1909 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1911 unsigned olddecl_uid = DECL_UID (olddecl);
1912 tree olddecl_context = DECL_CONTEXT (olddecl);
1914 memcpy ((char *) olddecl + sizeof (struct tree_common),
1915 (char *) newdecl + sizeof (struct tree_common),
1916 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1917 switch (TREE_CODE (olddecl))
1927 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1928 (char *) newdecl + sizeof (struct tree_decl_common),
1929 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1934 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1935 (char *) newdecl + sizeof (struct tree_decl_common),
1936 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1938 DECL_UID (olddecl) = olddecl_uid;
1939 DECL_CONTEXT (olddecl) = olddecl_context;
1942 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1943 so that encode_section_info has a chance to look at the new decl
1944 flags and attributes. */
1945 if (DECL_RTL_SET_P (olddecl)
1946 && (TREE_CODE (olddecl) == FUNCTION_DECL
1947 || (TREE_CODE (olddecl) == VAR_DECL
1948 && TREE_STATIC (olddecl))))
1949 make_decl_rtl (olddecl);
1951 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
1952 and the definition is coming from the old version, cgraph needs
1953 to be called again. */
1954 if (extern_changed && !new_is_definition
1955 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
1956 cgraph_finalize_function (olddecl, false);
1959 /* Handle when a new declaration NEWDECL has the same name as an old
1960 one OLDDECL in the same binding contour. Prints an error message
1963 If safely possible, alter OLDDECL to look like NEWDECL, and return
1964 true. Otherwise, return false. */
1967 duplicate_decls (tree newdecl, tree olddecl)
1969 tree newtype = NULL, oldtype = NULL;
1971 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1973 /* Avoid `unused variable' and other warnings warnings for OLDDECL. */
1974 TREE_NO_WARNING (olddecl) = 1;
1978 merge_decls (newdecl, olddecl, newtype, oldtype);
1983 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1985 warn_if_shadowing (tree new_decl)
1987 struct c_binding *b;
1989 /* Shadow warnings wanted? */
1991 /* No shadow warnings for internally generated vars. */
1992 || DECL_IS_BUILTIN (new_decl)
1993 /* No shadow warnings for vars made for inlining. */
1994 || DECL_FROM_INLINE (new_decl))
1997 /* Is anything being shadowed? Invisible decls do not count. */
1998 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1999 if (b->decl && b->decl != new_decl && !b->invisible)
2001 tree old_decl = b->decl;
2003 if (old_decl == error_mark_node)
2005 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2006 "non-variable", new_decl);
2009 else if (TREE_CODE (old_decl) == PARM_DECL)
2010 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2012 else if (DECL_FILE_SCOPE_P (old_decl))
2013 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2014 "declaration", new_decl);
2015 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2016 && DECL_BUILT_IN (old_decl))
2018 warning (OPT_Wshadow, "declaration of %q+D shadows "
2019 "a built-in function", new_decl);
2023 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2026 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
2033 /* Subroutine of pushdecl.
2035 X is a TYPE_DECL for a typedef statement. Create a brand new
2036 ..._TYPE node (which will be just a variant of the existing
2037 ..._TYPE node with identical properties) and then install X
2038 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
2040 The whole point here is to end up with a situation where each
2041 and every ..._TYPE node the compiler creates will be uniquely
2042 associated with AT MOST one node representing a typedef name.
2043 This way, even though the compiler substitutes corresponding
2044 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
2045 early on, later parts of the compiler can always do the reverse
2046 translation and get back the corresponding typedef name. For
2049 typedef struct S MY_TYPE;
2052 Later parts of the compiler might only know that `object' was of
2053 type `struct S' if it were not for code just below. With this
2054 code however, later parts of the compiler see something like:
2056 struct S' == struct S
2057 typedef struct S' MY_TYPE;
2060 And they can then deduce (from the node for type struct S') that
2061 the original object declaration was:
2065 Being able to do this is important for proper support of protoize,
2066 and also for generating precise symbolic debugging information
2067 which takes full account of the programmer's (typedef) vocabulary.
2069 Obviously, we don't want to generate a duplicate ..._TYPE node if
2070 the TYPE_DECL node that we are now processing really represents a
2071 standard built-in type.
2073 Since all standard types are effectively declared at line zero
2074 in the source file, we can easily check to see if we are working
2075 on a standard type by checking the current value of lineno. */
2078 clone_underlying_type (tree x)
2080 if (DECL_IS_BUILTIN (x))
2082 if (TYPE_NAME (TREE_TYPE (x)) == 0)
2083 TYPE_NAME (TREE_TYPE (x)) = x;
2085 else if (TREE_TYPE (x) != error_mark_node
2086 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
2088 tree tt = TREE_TYPE (x);
2089 DECL_ORIGINAL_TYPE (x) = tt;
2090 tt = build_variant_type_copy (tt);
2092 TREE_USED (tt) = TREE_USED (x);
2097 /* Record a decl-node X as belonging to the current lexical scope.
2098 Check for errors (such as an incompatible declaration for the same
2099 name already seen in the same scope).
2101 Returns either X or an old decl for the same name.
2102 If an old decl is returned, it may have been smashed
2103 to agree with what X says. */
2108 tree name = DECL_NAME (x);
2109 struct c_scope *scope = current_scope;
2110 struct c_binding *b;
2111 bool nested = false;
2113 /* Functions need the lang_decl data. */
2114 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
2115 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
2117 /* Must set DECL_CONTEXT for everything not at file scope or
2118 DECL_FILE_SCOPE_P won't work. Local externs don't count
2119 unless they have initializers (which generate code). */
2120 if (current_function_decl
2121 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2122 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2123 DECL_CONTEXT (x) = current_function_decl;
2125 /* If this is of variably modified type, prevent jumping into its
2127 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2128 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2129 c_begin_vm_scope (scope->depth);
2131 /* Anonymous decls are just inserted in the scope. */
2134 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2138 /* First, see if there is another declaration with the same name in
2139 the current scope. If there is, duplicate_decls may do all the
2140 work for us. If duplicate_decls returns false, that indicates
2141 two incompatible decls in the same scope; we are to silently
2142 replace the old one (duplicate_decls has issued all appropriate
2143 diagnostics). In particular, we should not consider possible
2144 duplicates in the external scope, or shadowing. */
2145 b = I_SYMBOL_BINDING (name);
2146 if (b && B_IN_SCOPE (b, scope))
2148 struct c_binding *b_ext, *b_use;
2149 tree type = TREE_TYPE (x);
2150 tree visdecl = b->decl;
2151 tree vistype = TREE_TYPE (visdecl);
2152 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2153 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2154 b->inner_comp = false;
2157 /* If this is an external linkage declaration, we should check
2158 for compatibility with the type in the external scope before
2159 setting the type at this scope based on the visible
2160 information only. */
2161 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2163 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2164 b_ext = b_ext->shadowed;
2169 TREE_TYPE (b_use->decl) = b_use->type;
2172 if (duplicate_decls (x, b_use->decl))
2176 /* Save the updated type in the external scope and
2177 restore the proper type for this scope. */
2179 if (comptypes (vistype, type))
2180 thistype = composite_type (vistype, type);
2182 thistype = TREE_TYPE (b_use->decl);
2183 b_use->type = TREE_TYPE (b_use->decl);
2184 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2185 && DECL_BUILT_IN (b_use->decl))
2187 = build_type_attribute_variant (thistype,
2190 TREE_TYPE (b_use->decl) = thistype;
2195 goto skip_external_and_shadow_checks;
2198 /* All declarations with external linkage, and all external
2199 references, go in the external scope, no matter what scope is
2200 current. However, the binding in that scope is ignored for
2201 purposes of normal name lookup. A separate binding structure is
2202 created in the requested scope; this governs the normal
2203 visibility of the symbol.
2205 The binding in the externals scope is used exclusively for
2206 detecting duplicate declarations of the same object, no matter
2207 what scope they are in; this is what we do here. (C99 6.2.7p2:
2208 All declarations that refer to the same object or function shall
2209 have compatible type; otherwise, the behavior is undefined.) */
2210 if (DECL_EXTERNAL (x) || scope == file_scope)
2212 tree type = TREE_TYPE (x);
2215 bool type_saved = false;
2216 if (b && !B_IN_EXTERNAL_SCOPE (b)
2217 && (TREE_CODE (b->decl) == FUNCTION_DECL
2218 || TREE_CODE (b->decl) == VAR_DECL)
2219 && DECL_FILE_SCOPE_P (b->decl))
2222 vistype = TREE_TYPE (visdecl);
2224 if (scope != file_scope
2225 && !DECL_IN_SYSTEM_HEADER (x))
2226 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2228 while (b && !B_IN_EXTERNAL_SCOPE (b))
2230 /* If this decl might be modified, save its type. This is
2231 done here rather than when the decl is first bound
2232 because the type may change after first binding, through
2233 being completed or through attributes being added. If we
2234 encounter multiple such decls, only the first should have
2235 its type saved; the others will already have had their
2236 proper types saved and the types will not have changed as
2237 their scopes will not have been re-entered. */
2238 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2240 b->type = TREE_TYPE (b->decl);
2243 if (B_IN_FILE_SCOPE (b)
2244 && TREE_CODE (b->decl) == VAR_DECL
2245 && TREE_STATIC (b->decl)
2246 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2247 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2248 && TREE_CODE (type) == ARRAY_TYPE
2249 && TYPE_DOMAIN (type)
2250 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2251 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2253 /* Array type completed in inner scope, which should be
2254 diagnosed if the completion does not have size 1 and
2255 it does not get completed in the file scope. */
2256 b->inner_comp = true;
2261 /* If a matching external declaration has been found, set its
2262 type to the composite of all the types of that declaration.
2263 After the consistency checks, it will be reset to the
2264 composite of the visible types only. */
2265 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2267 TREE_TYPE (b->decl) = b->type;
2269 /* The point of the same_translation_unit_p check here is,
2270 we want to detect a duplicate decl for a construct like
2271 foo() { extern bar(); } ... static bar(); but not if
2272 they are in different translation units. In any case,
2273 the static does not go in the externals scope. */
2275 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2276 && duplicate_decls (x, b->decl))
2281 if (comptypes (vistype, type))
2282 thistype = composite_type (vistype, type);
2284 thistype = TREE_TYPE (b->decl);
2288 b->type = TREE_TYPE (b->decl);
2289 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2291 = build_type_attribute_variant (thistype,
2292 TYPE_ATTRIBUTES (b->type));
2293 TREE_TYPE (b->decl) = thistype;
2294 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2297 else if (TREE_PUBLIC (x))
2299 if (visdecl && !b && duplicate_decls (x, visdecl))
2301 /* An external declaration at block scope referring to a
2302 visible entity with internal linkage. The composite
2303 type will already be correct for this scope, so we
2304 just need to fall through to make the declaration in
2311 bind (name, x, external_scope, /*invisible=*/true,
2318 if (TREE_CODE (x) != PARM_DECL)
2319 warn_if_shadowing (x);
2321 skip_external_and_shadow_checks:
2322 if (TREE_CODE (x) == TYPE_DECL)
2323 clone_underlying_type (x);
2325 bind (name, x, scope, /*invisible=*/false, nested);
2327 /* If x's type is incomplete because it's based on a
2328 structure or union which has not yet been fully declared,
2329 attach it to that structure or union type, so we can go
2330 back and complete the variable declaration later, if the
2331 structure or union gets fully declared.
2333 If the input is erroneous, we can have error_mark in the type
2334 slot (e.g. "f(void a, ...)") - that doesn't count as an
2336 if (TREE_TYPE (x) != error_mark_node
2337 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2339 tree element = TREE_TYPE (x);
2341 while (TREE_CODE (element) == ARRAY_TYPE)
2342 element = TREE_TYPE (element);
2343 element = TYPE_MAIN_VARIANT (element);
2345 if ((TREE_CODE (element) == RECORD_TYPE
2346 || TREE_CODE (element) == UNION_TYPE)
2347 && (TREE_CODE (x) != TYPE_DECL
2348 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2349 && !COMPLETE_TYPE_P (element))
2350 C_TYPE_INCOMPLETE_VARS (element)
2351 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2356 /* Record X as belonging to file scope.
2357 This is used only internally by the Objective-C front end,
2358 and is limited to its needs. duplicate_decls is not called;
2359 if there is any preexisting decl for this identifier, it is an ICE. */
2362 pushdecl_top_level (tree x)
2365 bool nested = false;
2366 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2368 name = DECL_NAME (x);
2370 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2372 if (TREE_PUBLIC (x))
2374 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2378 bind (name, x, file_scope, /*invisible=*/false, nested);
2384 implicit_decl_warning (tree id, tree olddecl)
2386 void (*diag) (const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2);
2387 switch (mesg_implicit_function_declaration)
2390 case 1: diag = warning0; break;
2391 case 2: diag = error; break;
2392 default: gcc_unreachable ();
2395 diag (G_("implicit declaration of function %qE"), id);
2397 locate_old_decl (olddecl, diag);
2400 /* Generate an implicit declaration for identifier FUNCTIONID as a
2401 function of type int (). */
2404 implicitly_declare (tree functionid)
2406 struct c_binding *b;
2410 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2412 if (B_IN_SCOPE (b, external_scope))
2421 if (decl == error_mark_node)
2424 /* FIXME: Objective-C has weird not-really-builtin functions
2425 which are supposed to be visible automatically. They wind up
2426 in the external scope because they're pushed before the file
2427 scope gets created. Catch this here and rebind them into the
2429 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2431 bind (functionid, decl, file_scope,
2432 /*invisible=*/false, /*nested=*/true);
2437 tree newtype = default_function_type;
2439 TREE_TYPE (decl) = b->type;
2440 /* Implicit declaration of a function already declared
2441 (somehow) in a different scope, or as a built-in.
2442 If this is the first time this has happened, warn;
2443 then recycle the old declaration but with the new type. */
2444 if (!C_DECL_IMPLICIT (decl))
2446 implicit_decl_warning (functionid, decl);
2447 C_DECL_IMPLICIT (decl) = 1;
2449 if (DECL_BUILT_IN (decl))
2451 newtype = build_type_attribute_variant (newtype,
2453 (TREE_TYPE (decl)));
2454 if (!comptypes (newtype, TREE_TYPE (decl)))
2456 warning (0, "incompatible implicit declaration of built-in"
2457 " function %qD", decl);
2458 newtype = TREE_TYPE (decl);
2463 if (!comptypes (newtype, TREE_TYPE (decl)))
2465 error ("incompatible implicit declaration of function %qD",
2467 locate_old_decl (decl, error);
2470 b->type = TREE_TYPE (decl);
2471 TREE_TYPE (decl) = newtype;
2472 bind (functionid, decl, current_scope,
2473 /*invisible=*/false, /*nested=*/true);
2478 /* Not seen before. */
2479 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2480 DECL_EXTERNAL (decl) = 1;
2481 TREE_PUBLIC (decl) = 1;
2482 C_DECL_IMPLICIT (decl) = 1;
2483 implicit_decl_warning (functionid, 0);
2484 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2486 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2488 /* C89 says implicit declarations are in the innermost block.
2489 So we record the decl in the standard fashion. */
2490 decl = pushdecl (decl);
2492 /* No need to call objc_check_decl here - it's a function type. */
2493 rest_of_decl_compilation (decl, 0, 0);
2495 /* Write a record describing this implicit function declaration
2496 to the prototypes file (if requested). */
2497 gen_aux_info_record (decl, 0, 1, 0);
2499 /* Possibly apply some default attributes to this implicit declaration. */
2500 decl_attributes (&decl, NULL_TREE, 0);
2505 /* Issue an error message for a reference to an undeclared variable
2506 ID, including a reference to a builtin outside of function-call
2507 context. Establish a binding of the identifier to error_mark_node
2508 in an appropriate scope, which will suppress further errors for the
2509 same identifier. The error message should be given location LOC. */
2511 undeclared_variable (tree id, location_t loc)
2513 static bool already = false;
2514 struct c_scope *scope;
2516 if (current_function_decl == 0)
2518 error ("%H%qE undeclared here (not in a function)", &loc, id);
2519 scope = current_scope;
2523 error ("%H%qE undeclared (first use in this function)", &loc, id);
2527 error ("%H(Each undeclared identifier is reported only once", &loc);
2528 error ("%Hfor each function it appears in.)", &loc);
2532 /* If we are parsing old-style parameter decls, current_function_decl
2533 will be nonnull but current_function_scope will be null. */
2534 scope = current_function_scope ? current_function_scope : current_scope;
2536 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2539 /* Subroutine of lookup_label, declare_label, define_label: construct a
2540 LABEL_DECL with all the proper frills. */
2543 make_label (tree name, location_t location)
2545 tree label = build_decl (LABEL_DECL, name, void_type_node);
2547 DECL_CONTEXT (label) = current_function_decl;
2548 DECL_MODE (label) = VOIDmode;
2549 DECL_SOURCE_LOCATION (label) = location;
2554 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2555 Create one if none exists so far for the current function.
2556 This is called when a label is used in a goto expression or
2557 has its address taken. */
2560 lookup_label (tree name)
2564 if (current_function_decl == 0)
2566 error ("label %qE referenced outside of any function", name);
2570 /* Use a label already defined or ref'd with this name, but not if
2571 it is inherited from a containing function and wasn't declared
2573 label = I_LABEL_DECL (name);
2574 if (label && (DECL_CONTEXT (label) == current_function_decl
2575 || C_DECLARED_LABEL_FLAG (label)))
2577 /* If the label has only been declared, update its apparent
2578 location to point here, for better diagnostics if it
2579 turns out not to have been defined. */
2580 if (!TREE_USED (label))
2581 DECL_SOURCE_LOCATION (label) = input_location;
2585 /* No label binding for that identifier; make one. */
2586 label = make_label (name, input_location);
2588 /* Ordinary labels go in the current function scope. */
2589 bind (name, label, current_function_scope,
2590 /*invisible=*/false, /*nested=*/false);
2594 /* Make a label named NAME in the current function, shadowing silently
2595 any that may be inherited from containing functions or containing
2596 scopes. This is called for __label__ declarations. */
2599 declare_label (tree name)
2601 struct c_binding *b = I_LABEL_BINDING (name);
2604 /* Check to make sure that the label hasn't already been declared
2606 if (b && B_IN_CURRENT_SCOPE (b))
2608 error ("duplicate label declaration %qE", name);
2609 locate_old_decl (b->decl, error);
2611 /* Just use the previous declaration. */
2615 label = make_label (name, input_location);
2616 C_DECLARED_LABEL_FLAG (label) = 1;
2618 /* Declared labels go in the current scope. */
2619 bind (name, label, current_scope,
2620 /*invisible=*/false, /*nested=*/false);
2624 /* Define a label, specifying the location in the source file.
2625 Return the LABEL_DECL node for the label, if the definition is valid.
2626 Otherwise return 0. */
2629 define_label (location_t location, tree name)
2631 /* Find any preexisting label with this name. It is an error
2632 if that label has already been defined in this function, or
2633 if there is a containing function with a declared label with
2635 tree label = I_LABEL_DECL (name);
2636 struct c_label_list *nlist_se, *nlist_vm;
2639 && ((DECL_CONTEXT (label) == current_function_decl
2640 && DECL_INITIAL (label) != 0)
2641 || (DECL_CONTEXT (label) != current_function_decl
2642 && C_DECLARED_LABEL_FLAG (label))))
2644 error ("%Hduplicate label %qD", &location, label);
2645 locate_old_decl (label, error);
2648 else if (label && DECL_CONTEXT (label) == current_function_decl)
2650 /* The label has been used or declared already in this function,
2651 but not defined. Update its location to point to this
2653 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2654 error ("%Jjump into statement expression", label);
2655 if (C_DECL_UNDEFINABLE_VM (label))
2656 error ("%Jjump into scope of identifier with variably modified type",
2658 DECL_SOURCE_LOCATION (label) = location;
2662 /* No label binding for that identifier; make one. */
2663 label = make_label (name, location);
2665 /* Ordinary labels go in the current function scope. */
2666 bind (name, label, current_function_scope,
2667 /*invisible=*/false, /*nested=*/false);
2670 if (!in_system_header && lookup_name (name))
2671 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2672 "for labels, identifier %qE conflicts", &location, name);
2674 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2675 nlist_se->next = label_context_stack_se->labels_def;
2676 nlist_se->label = label;
2677 label_context_stack_se->labels_def = nlist_se;
2679 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2680 nlist_vm->next = label_context_stack_vm->labels_def;
2681 nlist_vm->label = label;
2682 label_context_stack_vm->labels_def = nlist_vm;
2684 /* Mark label as having been defined. */
2685 DECL_INITIAL (label) = error_mark_node;
2689 /* Given NAME, an IDENTIFIER_NODE,
2690 return the structure (or union or enum) definition for that name.
2691 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2692 CODE says which kind of type the caller wants;
2693 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2694 If the wrong kind of type is found, an error is reported. */
2697 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2699 struct c_binding *b = I_TAG_BINDING (name);
2705 /* We only care about whether it's in this level if
2706 thislevel_only was set or it might be a type clash. */
2707 if (thislevel_only || TREE_CODE (b->decl) != code)
2709 /* For our purposes, a tag in the external scope is the same as
2710 a tag in the file scope. (Primarily relevant to Objective-C
2711 and its builtin structure tags, which get pushed before the
2712 file scope is created.) */
2713 if (B_IN_CURRENT_SCOPE (b)
2714 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2718 if (thislevel_only && !thislevel)
2721 if (TREE_CODE (b->decl) != code)
2723 /* Definition isn't the kind we were looking for. */
2724 pending_invalid_xref = name;
2725 pending_invalid_xref_location = input_location;
2727 /* If in the same binding level as a declaration as a tag
2728 of a different type, this must not be allowed to
2729 shadow that tag, so give the error immediately.
2730 (For example, "struct foo; union foo;" is invalid.) */
2732 pending_xref_error ();
2737 /* Print an error message now
2738 for a recent invalid struct, union or enum cross reference.
2739 We don't print them immediately because they are not invalid
2740 when used in the `struct foo;' construct for shadowing. */
2743 pending_xref_error (void)
2745 if (pending_invalid_xref != 0)
2746 error ("%H%qE defined as wrong kind of tag",
2747 &pending_invalid_xref_location, pending_invalid_xref);
2748 pending_invalid_xref = 0;
2752 /* Look up NAME in the current scope and its superiors
2753 in the namespace of variables, functions and typedefs.
2754 Return a ..._DECL node of some kind representing its definition,
2755 or return 0 if it is undefined. */
2758 lookup_name (tree name)
2760 struct c_binding *b = I_SYMBOL_BINDING (name);
2761 if (b && !b->invisible)
2766 /* Similar to `lookup_name' but look only at the indicated scope. */
2769 lookup_name_in_scope (tree name, struct c_scope *scope)
2771 struct c_binding *b;
2773 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2774 if (B_IN_SCOPE (b, scope))
2779 /* Create the predefined scalar types of C,
2780 and some nodes representing standard constants (0, 1, (void *) 0).
2781 Initialize the global scope.
2782 Make definitions for built-in primitive functions. */
2785 c_init_decl_processing (void)
2787 location_t save_loc = input_location;
2789 /* Initialize reserved words for parser. */
2792 current_function_decl = 0;
2794 gcc_obstack_init (&parser_obstack);
2796 /* Make the externals scope. */
2798 external_scope = current_scope;
2800 /* Declarations from c_common_nodes_and_builtins must not be associated
2801 with this input file, lest we get differences between using and not
2802 using preprocessed headers. */
2803 #ifdef USE_MAPPED_LOCATION
2804 input_location = BUILTINS_LOCATION;
2806 input_location.file = "<built-in>";
2807 input_location.line = 0;
2810 build_common_tree_nodes (flag_signed_char, false);
2812 c_common_nodes_and_builtins ();
2814 /* In C, comparisons and TRUTH_* expressions have type int. */
2815 truthvalue_type_node = integer_type_node;
2816 truthvalue_true_node = integer_one_node;
2817 truthvalue_false_node = integer_zero_node;
2819 /* Even in C99, which has a real boolean type. */
2820 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2821 boolean_type_node));
2823 input_location = save_loc;
2825 pedantic_lvalues = true;
2827 make_fname_decl = c_make_fname_decl;
2828 start_fname_decls ();
2831 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2832 decl, NAME is the initialization string and TYPE_DEP indicates whether
2833 NAME depended on the type of the function. As we don't yet implement
2834 delayed emission of static data, we mark the decl as emitted
2835 so it is not placed in the output. Anything using it must therefore pull
2836 out the STRING_CST initializer directly. FIXME. */
2839 c_make_fname_decl (tree id, int type_dep)
2841 const char *name = fname_as_string (type_dep);
2842 tree decl, type, init;
2843 size_t length = strlen (name);
2845 type = build_array_type (char_type_node,
2846 build_index_type (size_int (length)));
2847 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2849 decl = build_decl (VAR_DECL, id, type);
2851 TREE_STATIC (decl) = 1;
2852 TREE_READONLY (decl) = 1;
2853 DECL_ARTIFICIAL (decl) = 1;
2855 init = build_string (length + 1, name);
2856 free ((char *) name);
2857 TREE_TYPE (init) = type;
2858 DECL_INITIAL (decl) = init;
2860 TREE_USED (decl) = 1;
2862 if (current_function_decl
2863 /* For invalid programs like this:
2866 const char* p = __FUNCTION__;
2868 the __FUNCTION__ is believed to appear in K&R style function
2869 parameter declarator. In that case we still don't have
2871 && (!errorcount || current_function_scope))
2873 DECL_CONTEXT (decl) = current_function_decl;
2874 bind (id, decl, current_function_scope,
2875 /*invisible=*/false, /*nested=*/false);
2878 finish_decl (decl, init, NULL_TREE);
2883 /* Return a definition for a builtin function named NAME and whose data type
2884 is TYPE. TYPE should be a function type with argument types.
2885 FUNCTION_CODE tells later passes how to compile calls to this function.
2886 See tree.h for its possible values.
2888 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2889 the name to be called if we can't opencode the function. If
2890 ATTRS is nonzero, use that for the function's attribute list. */
2893 builtin_function (const char *name, tree type, int function_code,
2894 enum built_in_class cl, const char *library_name,
2897 tree id = get_identifier (name);
2898 tree decl = build_decl (FUNCTION_DECL, id, type);
2899 TREE_PUBLIC (decl) = 1;
2900 DECL_EXTERNAL (decl) = 1;
2901 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2902 DECL_BUILT_IN_CLASS (decl) = cl;
2903 DECL_FUNCTION_CODE (decl) = function_code;
2904 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2906 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2908 /* Should never be called on a symbol with a preexisting meaning. */
2909 gcc_assert (!I_SYMBOL_BINDING (id));
2911 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2913 /* Builtins in the implementation namespace are made visible without
2914 needing to be explicitly declared. See push_file_scope. */
2915 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2917 TREE_CHAIN (decl) = visible_builtins;
2918 visible_builtins = decl;
2921 /* Possibly apply some default attributes to this built-in function. */
2923 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2925 decl_attributes (&decl, NULL_TREE, 0);
2930 /* Called when a declaration is seen that contains no names to declare.
2931 If its type is a reference to a structure, union or enum inherited
2932 from a containing scope, shadow that tag name for the current scope
2933 with a forward reference.
2934 If its type defines a new named structure or union
2935 or defines an enum, it is valid but we need not do anything here.
2936 Otherwise, it is an error. */
2939 shadow_tag (const struct c_declspecs *declspecs)
2941 shadow_tag_warned (declspecs, 0);
2944 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2947 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2949 bool found_tag = false;
2951 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2953 tree value = declspecs->type;
2954 enum tree_code code = TREE_CODE (value);
2956 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2957 /* Used to test also that TYPE_SIZE (value) != 0.
2958 That caused warning for `struct foo;' at top level in the file. */
2960 tree name = TYPE_NAME (value);
2967 if (warned != 1 && code != ENUMERAL_TYPE)
2968 /* Empty unnamed enum OK */
2970 pedwarn ("unnamed struct/union that defines no instances");
2974 else if (!declspecs->tag_defined_p
2975 && declspecs->storage_class != csc_none)
2978 pedwarn ("empty declaration with storage class specifier "
2979 "does not redeclare tag");
2981 pending_xref_error ();
2983 else if (!declspecs->tag_defined_p
2984 && (declspecs->const_p
2985 || declspecs->volatile_p
2986 || declspecs->restrict_p))
2989 pedwarn ("empty declaration with type qualifier "
2990 "does not redeclare tag");
2992 pending_xref_error ();
2996 pending_invalid_xref = 0;
2997 t = lookup_tag (code, name, 1);
3001 t = make_node (code);
3008 if (warned != 1 && !in_system_header)
3010 pedwarn ("useless type name in empty declaration");
3015 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
3017 pedwarn ("useless type name in empty declaration");
3021 pending_invalid_xref = 0;
3023 if (declspecs->inline_p)
3025 error ("%<inline%> in empty declaration");
3029 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3031 error ("%<auto%> in file-scope empty declaration");
3035 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3037 error ("%<register%> in file-scope empty declaration");
3041 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3043 warning (0, "useless storage class specifier in empty declaration");
3047 if (!warned && !in_system_header && declspecs->thread_p)
3049 warning (0, "useless %<__thread%> in empty declaration");
3053 if (!warned && !in_system_header && (declspecs->const_p
3054 || declspecs->volatile_p
3055 || declspecs->restrict_p))
3057 warning (0, "useless type qualifier in empty declaration");
3064 pedwarn ("empty declaration");
3069 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3070 bits. SPECS represents declaration specifiers that the grammar
3071 only permits to contain type qualifiers and attributes. */
3074 quals_from_declspecs (const struct c_declspecs *specs)
3076 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3077 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3078 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3079 gcc_assert (!specs->type
3080 && !specs->decl_attr
3081 && specs->typespec_word == cts_none
3082 && specs->storage_class == csc_none
3083 && !specs->typedef_p
3084 && !specs->explicit_signed_p
3085 && !specs->deprecated_p
3087 && !specs->long_long_p
3090 && !specs->unsigned_p
3091 && !specs->complex_p
3093 && !specs->thread_p);
3097 /* Construct an array declarator. EXPR is the expression inside [],
3098 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3099 applied to the pointer to which a parameter array is converted).
3100 STATIC_P is true if "static" is inside the [], false otherwise.
3101 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3102 length which is nevertheless a complete type, false otherwise. The
3103 field for the contained declarator is left to be filled in by
3104 set_array_declarator_inner. */
3106 struct c_declarator *
3107 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3110 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3111 struct c_declarator);
3112 declarator->kind = cdk_array;
3113 declarator->declarator = 0;
3114 declarator->u.array.dimen = expr;
3117 declarator->u.array.attrs = quals->attrs;
3118 declarator->u.array.quals = quals_from_declspecs (quals);
3122 declarator->u.array.attrs = NULL_TREE;
3123 declarator->u.array.quals = 0;
3125 declarator->u.array.static_p = static_p;
3126 declarator->u.array.vla_unspec_p = vla_unspec_p;
3127 if (pedantic && !flag_isoc99)
3129 if (static_p || quals != NULL)
3130 pedwarn ("ISO C90 does not support %<static%> or type "
3131 "qualifiers in parameter array declarators");
3133 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3137 if (!current_scope->parm_flag)
3140 error ("%<[*]%> not allowed in other than function prototype scope");
3141 declarator->u.array.vla_unspec_p = false;
3144 current_scope->had_vla_unspec = true;
3149 /* Set the contained declarator of an array declarator. DECL is the
3150 declarator, as constructed by build_array_declarator; INNER is what
3151 appears on the left of the []. ABSTRACT_P is true if it is an
3152 abstract declarator, false otherwise; this is used to reject static
3153 and type qualifiers in abstract declarators, where they are not in
3154 the C99 grammar (subject to possible change in DR#289). */
3156 struct c_declarator *
3157 set_array_declarator_inner (struct c_declarator *decl,
3158 struct c_declarator *inner, bool abstract_p)
3160 decl->declarator = inner;
3161 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3162 || decl->u.array.attrs != NULL_TREE
3163 || decl->u.array.static_p))
3164 error ("static or type qualifiers in abstract declarator");
3168 /* INIT is a constructor that forms DECL's initializer. If the final
3169 element initializes a flexible array field, add the size of that
3170 initializer to DECL's size. */
3173 add_flexible_array_elts_to_size (tree decl, tree init)
3177 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3180 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3181 type = TREE_TYPE (elt);
3182 if (TREE_CODE (type) == ARRAY_TYPE
3183 && TYPE_SIZE (type) == NULL_TREE
3184 && TYPE_DOMAIN (type) != NULL_TREE
3185 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3187 complete_array_type (&type, elt, false);
3189 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3190 DECL_SIZE_UNIT (decl)
3191 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3195 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3198 groktypename (struct c_type_name *type_name)
3201 tree attrs = type_name->specs->attrs;
3203 type_name->specs->attrs = NULL_TREE;
3205 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3208 /* Apply attributes. */
3209 decl_attributes (&type, attrs, 0);
3214 /* Decode a declarator in an ordinary declaration or data definition.
3215 This is called as soon as the type information and variable name
3216 have been parsed, before parsing the initializer if any.
3217 Here we create the ..._DECL node, fill in its type,
3218 and put it on the list of decls for the current context.
3219 The ..._DECL node is returned as the value.
3221 Exception: for arrays where the length is not specified,
3222 the type is left null, to be filled in by `finish_decl'.
3224 Function definitions do not come here; they go to start_function
3225 instead. However, external and forward declarations of functions
3226 do go through here. Structure field declarations are done by
3227 grokfield and not through here. */
3230 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3231 bool initialized, tree attributes)
3236 /* An object declared as __attribute__((deprecated)) suppresses
3237 warnings of uses of other deprecated items. */
3238 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
3239 /* An object declared as __attribute__((unavailable)) suppresses
3240 any reports of being declared with unavailable or deprecated
3241 items. An object declared as __attribute__((deprecated))
3242 suppresses warnings of uses of other deprecated items. */
3243 #ifdef A_LESS_INEFFICENT_WAY /* which I really don't want to do! */
3244 if (lookup_attribute ("deprecated", attributes))
3245 deprecated_state = DEPRECATED_SUPPRESS;
3246 else if (lookup_attribute ("unavailable", attributes))
3247 deprecated_state = DEPRECATED_UNAVAILABLE_SUPPRESS;
3248 #else /* a more efficient way doing what lookup_attribute would do */
3251 for (a = attributes; a; a = TREE_CHAIN (a))
3253 tree name = TREE_PURPOSE (a);
3254 if (TREE_CODE (name) == IDENTIFIER_NODE)
3255 if (is_attribute_p ("deprecated", name))
3257 deprecated_state = DEPRECATED_SUPPRESS;
3260 if (is_attribute_p ("unavailable", name))
3262 deprecated_state = DEPRECATED_UNAVAILABLE_SUPPRESS;
3267 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
3269 decl = grokdeclarator (declarator, declspecs,
3270 NORMAL, initialized, NULL);
3274 deprecated_state = DEPRECATED_NORMAL;
3276 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3277 && MAIN_NAME_P (DECL_NAME (decl)))
3278 warning (OPT_Wmain, "%q+D is usually a function", decl);
3281 /* Is it valid for this decl to have an initializer at all?
3282 If not, set INITIALIZED to zero, which will indirectly
3283 tell 'finish_decl' to ignore the initializer once it is parsed. */
3284 switch (TREE_CODE (decl))
3287 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3292 error ("function %qD is initialized like a variable", decl);
3297 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3298 error ("parameter %qD is initialized", decl);
3303 /* Don't allow initializations for incomplete types except for
3304 arrays which might be completed by the initialization. */
3306 /* This can happen if the array size is an undefined macro.
3307 We already gave a warning, so we don't need another one. */
3308 if (TREE_TYPE (decl) == error_mark_node)
3310 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3312 /* A complete type is ok if size is fixed. */
3314 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3315 || C_DECL_VARIABLE_SIZE (decl))
3317 error ("variable-sized object may not be initialized");
3321 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3323 error ("variable %qD has initializer but incomplete type", decl);
3326 else if (C_DECL_VARIABLE_SIZE (decl))
3328 /* Although C99 is unclear about whether incomplete arrays
3329 of VLAs themselves count as VLAs, it does not make
3330 sense to permit them to be initialized given that
3331 ordinary VLAs may not be initialized. */
3332 error ("variable-sized object may not be initialized");
3339 if (current_scope == file_scope)
3340 TREE_STATIC (decl) = 1;
3342 /* Tell 'pushdecl' this is an initialized decl
3343 even though we don't yet have the initializer expression.
3344 Also tell 'finish_decl' it may store the real initializer. */
3345 DECL_INITIAL (decl) = error_mark_node;
3348 /* If this is a function declaration, write a record describing it to the
3349 prototypes file (if requested). */
3351 if (TREE_CODE (decl) == FUNCTION_DECL)
3352 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3354 /* ANSI specifies that a tentative definition which is not merged with
3355 a non-tentative definition behaves exactly like a definition with an
3356 initializer equal to zero. (Section 3.7.2)
3358 -fno-common gives strict ANSI behavior, though this tends to break
3359 a large body of code that grew up without this rule.
3361 Thread-local variables are never common, since there's no entrenched
3362 body of code to break, and it allows more efficient variable references
3363 in the presence of dynamic linking. */
3365 if (TREE_CODE (decl) == VAR_DECL
3367 && TREE_PUBLIC (decl)
3368 && !DECL_THREAD_LOCAL_P (decl)
3370 DECL_COMMON (decl) = 1;
3372 /* Set attributes here so if duplicate decl, will have proper attributes. */
3373 decl_attributes (&decl, attributes, 0);
3375 /* Handle gnu_inline attribute. */
3376 if (declspecs->inline_p
3377 && !flag_gnu89_inline
3378 && TREE_CODE (decl) == FUNCTION_DECL
3379 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
3381 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3383 else if (declspecs->storage_class != csc_static)
3384 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3387 if (TREE_CODE (decl) == FUNCTION_DECL
3388 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3390 struct c_declarator *ce = declarator;
3392 if (ce->kind == cdk_pointer)
3393 ce = declarator->declarator;
3394 if (ce->kind == cdk_function)
3396 tree args = ce->u.arg_info->parms;
3397 for (; args; args = TREE_CHAIN (args))
3399 tree type = TREE_TYPE (args);
3400 if (type && INTEGRAL_TYPE_P (type)
3401 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3402 DECL_ARG_TYPE (args) = integer_type_node;
3407 if (TREE_CODE (decl) == FUNCTION_DECL
3408 && DECL_DECLARED_INLINE_P (decl)
3409 && DECL_UNINLINABLE (decl)
3410 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3411 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3414 /* C99 6.7.4p3: An inline definition of a function with external
3415 linkage shall not contain a definition of a modifiable object
3416 with static storage duration... */
3417 if (TREE_CODE (decl) == VAR_DECL
3418 && current_scope != file_scope
3419 && TREE_STATIC (decl)
3420 && !TREE_READONLY (decl)
3421 && DECL_DECLARED_INLINE_P (current_function_decl)
3422 && DECL_EXTERNAL (current_function_decl))
3423 pedwarn ("%q+D is static but declared in inline function %qD "
3424 "which is not static", decl, current_function_decl);
3426 /* Add this decl to the current scope.
3427 TEM may equal DECL or it may be a previous decl of the same name. */
3428 tem = pushdecl (decl);
3430 if (initialized && DECL_EXTERNAL (tem))
3432 DECL_EXTERNAL (tem) = 0;
3433 TREE_STATIC (tem) = 1;
3439 /* Initialize EH if not initialized yet and exceptions are enabled. */
3442 c_maybe_initialize_eh (void)
3444 if (!flag_exceptions || c_eh_initialized_p)
3447 c_eh_initialized_p = true;
3448 eh_personality_libfunc
3449 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3450 ? "__gcc_personality_sj0"
3451 : "__gcc_personality_v0");
3452 default_init_unwind_resume_libfunc ();
3453 using_eh_for_cleanups ();
3456 /* Finish processing of a declaration;
3457 install its initial value.
3458 If the length of an array type is not known before,
3459 it must be determined now, from the initial value, or it is an error. */
3462 finish_decl (tree decl, tree init, tree asmspec_tree)
3465 int was_incomplete = (DECL_SIZE (decl) == 0);
3466 const char *asmspec = 0;
3468 /* If a name was specified, get the string. */
3469 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3470 && DECL_FILE_SCOPE_P (decl))
3471 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3473 asmspec = TREE_STRING_POINTER (asmspec_tree);
3475 /* If `start_decl' didn't like having an initialization, ignore it now. */
3476 if (init != 0 && DECL_INITIAL (decl) == 0)
3479 /* Don't crash if parm is initialized. */
3480 if (TREE_CODE (decl) == PARM_DECL)
3484 store_init_value (decl, init);
3486 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3487 || TREE_CODE (decl) == FUNCTION_DECL
3488 || TREE_CODE (decl) == FIELD_DECL))
3489 objc_check_decl (decl);
3491 type = TREE_TYPE (decl);
3493 /* Deduce size of array from initialization, if not already known. */
3494 if (TREE_CODE (type) == ARRAY_TYPE
3495 && TYPE_DOMAIN (type) == 0
3496 && TREE_CODE (decl) != TYPE_DECL)
3499 = (TREE_STATIC (decl)
3500 /* Even if pedantic, an external linkage array
3501 may have incomplete type at first. */
3502 ? pedantic && !TREE_PUBLIC (decl)
3503 : !DECL_EXTERNAL (decl));
3505 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3508 /* Get the completed type made by complete_array_type. */
3509 type = TREE_TYPE (decl);
3514 error ("initializer fails to determine size of %q+D", decl);
3519 error ("array size missing in %q+D", decl);
3520 /* If a `static' var's size isn't known,
3521 make it extern as well as static, so it does not get
3523 If it is not `static', then do not mark extern;
3524 finish_incomplete_decl will give it a default size
3525 and it will get allocated. */
3526 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3527 DECL_EXTERNAL (decl) = 1;
3531 error ("zero or negative size array %q+D", decl);
3535 /* For global variables, update the copy of the type that
3536 exists in the binding. */
3537 if (TREE_PUBLIC (decl))
3539 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3540 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3541 b_ext = b_ext->shadowed;
3545 b_ext->type = composite_type (b_ext->type, type);
3556 if (DECL_INITIAL (decl))
3557 TREE_TYPE (DECL_INITIAL (decl)) = type;
3559 layout_decl (decl, 0);
3562 if (TREE_CODE (decl) == VAR_DECL)
3564 if (init && TREE_CODE (init) == CONSTRUCTOR)
3565 add_flexible_array_elts_to_size (decl, init);
3567 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3568 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3569 layout_decl (decl, 0);
3571 if (DECL_SIZE (decl) == 0
3572 /* Don't give an error if we already gave one earlier. */
3573 && TREE_TYPE (decl) != error_mark_node
3574 && (TREE_STATIC (decl)
3575 /* A static variable with an incomplete type
3576 is an error if it is initialized.
3577 Also if it is not file scope.
3578 Otherwise, let it through, but if it is not `extern'
3579 then it may cause an error message later. */
3580 ? (DECL_INITIAL (decl) != 0
3581 || !DECL_FILE_SCOPE_P (decl))
3582 /* An automatic variable with an incomplete type
3584 : !DECL_EXTERNAL (decl)))
3586 error ("storage size of %q+D isn%'t known", decl);
3587 TREE_TYPE (decl) = error_mark_node;
3590 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3591 && DECL_SIZE (decl) != 0)
3593 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3594 constant_expression_warning (DECL_SIZE (decl));
3596 error ("storage size of %q+D isn%'t constant", decl);
3599 if (TREE_USED (type))
3600 TREE_USED (decl) = 1;
3603 /* If this is a function and an assembler name is specified, reset DECL_RTL
3604 so we can give it its new name. Also, update built_in_decls if it
3605 was a normal built-in. */
3606 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3608 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3609 set_builtin_user_assembler_name (decl, asmspec);
3610 set_user_assembler_name (decl, asmspec);
3613 /* If #pragma weak was used, mark the decl weak now. */
3614 maybe_apply_pragma_weak (decl);
3616 /* Output the assembler code and/or RTL code for variables and functions,
3617 unless the type is an undefined structure or union.
3618 If not, it will get done when the type is completed. */
3620 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3622 /* Determine the ELF visibility. */
3623 if (TREE_PUBLIC (decl))
3624 c_determine_visibility (decl);
3626 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3627 if (c_dialect_objc ())
3628 objc_check_decl (decl);
3632 /* If this is not a static variable, issue a warning.
3633 It doesn't make any sense to give an ASMSPEC for an
3634 ordinary, non-register local variable. Historically,
3635 GCC has accepted -- but ignored -- the ASMSPEC in
3637 if (!DECL_FILE_SCOPE_P (decl)
3638 && TREE_CODE (decl) == VAR_DECL
3639 && !C_DECL_REGISTER (decl)
3640 && !TREE_STATIC (decl))
3641 warning (0, "ignoring asm-specifier for non-static local "
3642 "variable %q+D", decl);
3644 set_user_assembler_name (decl, asmspec);
3647 if (DECL_FILE_SCOPE_P (decl))
3649 if (DECL_INITIAL (decl) == NULL_TREE
3650 || DECL_INITIAL (decl) == error_mark_node)
3651 /* Don't output anything
3652 when a tentative file-scope definition is seen.
3653 But at end of compilation, do output code for them. */
3654 DECL_DEFER_OUTPUT (decl) = 1;
3655 rest_of_decl_compilation (decl, true, 0);
3659 /* In conjunction with an ASMSPEC, the `register'
3660 keyword indicates that we should place the variable
3661 in a particular register. */
3662 if (asmspec && C_DECL_REGISTER (decl))
3664 DECL_HARD_REGISTER (decl) = 1;
3665 /* This cannot be done for a structure with volatile
3666 fields, on which DECL_REGISTER will have been
3668 if (!DECL_REGISTER (decl))
3669 error ("cannot put object with volatile field into register");
3672 if (TREE_CODE (decl) != FUNCTION_DECL)
3674 /* If we're building a variable sized type, and we might be
3675 reachable other than via the top of the current binding
3676 level, then create a new BIND_EXPR so that we deallocate
3677 the object at the right time. */
3678 /* Note that DECL_SIZE can be null due to errors. */
3679 if (DECL_SIZE (decl)
3680 && !TREE_CONSTANT (DECL_SIZE (decl))
3681 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3684 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3685 TREE_SIDE_EFFECTS (bind) = 1;
3687 BIND_EXPR_BODY (bind) = push_stmt_list ();
3689 add_stmt (build_stmt (DECL_EXPR, decl));
3694 if (!DECL_FILE_SCOPE_P (decl))
3696 /* Recompute the RTL of a local array now
3697 if it used to be an incomplete type. */
3699 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3701 /* If we used it already as memory, it must stay in memory. */
3702 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3703 /* If it's still incomplete now, no init will save it. */
3704 if (DECL_SIZE (decl) == 0)
3705 DECL_INITIAL (decl) = 0;
3710 /* If this was marked 'used', be sure it will be output. */
3711 if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3712 mark_decl_referenced (decl);
3714 if (TREE_CODE (decl) == TYPE_DECL)
3716 if (!DECL_FILE_SCOPE_P (decl)
3717 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3718 add_stmt (build_stmt (DECL_EXPR, decl));
3720 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3723 /* At the end of a declaration, throw away any variable type sizes
3724 of types defined inside that declaration. There is no use
3725 computing them in the following function definition. */
3726 if (current_scope == file_scope)
3727 get_pending_sizes ();
3729 /* Install a cleanup (aka destructor) if one was given. */
3730 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3732 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3735 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3736 tree cleanup_decl = lookup_name (cleanup_id);
3739 /* Build "cleanup(&decl)" for the destructor. */
3740 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3741 cleanup = build_tree_list (NULL_TREE, cleanup);
3742 cleanup = build_function_call (cleanup_decl, cleanup);
3744 /* Don't warn about decl unused; the cleanup uses it. */
3745 TREE_USED (decl) = 1;
3746 TREE_USED (cleanup_decl) = 1;
3748 /* Initialize EH, if we've been told to do so. */
3749 c_maybe_initialize_eh ();
3751 push_cleanup (decl, cleanup, false);
3756 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3759 grokparm (const struct c_parm *parm)
3761 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3764 decl_attributes (&decl, parm->attrs, 0);
3769 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3770 and push that on the current scope. */
3773 push_parm_decl (const struct c_parm *parm)
3777 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3778 decl_attributes (&decl, parm->attrs, 0);
3780 decl = pushdecl (decl);
3782 finish_decl (decl, NULL_TREE, NULL_TREE);
3785 /* Mark all the parameter declarations to date as forward decls.
3786 Also diagnose use of this extension. */
3789 mark_forward_parm_decls (void)
3791 struct c_binding *b;
3793 if (pedantic && !current_scope->warned_forward_parm_decls)
3795 pedwarn ("ISO C forbids forward parameter declarations");
3796 current_scope->warned_forward_parm_decls = true;
3799 for (b = current_scope->bindings; b; b = b->prev)
3800 if (TREE_CODE (b->decl) == PARM_DECL)
3801 TREE_ASM_WRITTEN (b->decl) = 1;
3804 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3805 literal, which may be an incomplete array type completed by the
3806 initializer; INIT is a CONSTRUCTOR that initializes the compound
3810 build_compound_literal (tree type, tree init)
3812 /* We do not use start_decl here because we have a type, not a declarator;
3813 and do not use finish_decl because the decl should be stored inside
3814 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3819 if (type == error_mark_node)
3820 return error_mark_node;
3822 decl = build_decl (VAR_DECL, NULL_TREE, type);
3823 DECL_EXTERNAL (decl) = 0;
3824 TREE_PUBLIC (decl) = 0;
3825 TREE_STATIC (decl) = (current_scope == file_scope);
3826 DECL_CONTEXT (decl) = current_function_decl;
3827 TREE_USED (decl) = 1;
3828 TREE_TYPE (decl) = type;
3829 TREE_READONLY (decl) = TYPE_READONLY (type);
3830 store_init_value (decl, init);
3832 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3834 int failure = complete_array_type (&TREE_TYPE (decl),
3835 DECL_INITIAL (decl), true);
3836 gcc_assert (!failure);
3838 type = TREE_TYPE (decl);
3839 TREE_TYPE (DECL_INITIAL (decl)) = type;
3842 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3843 return error_mark_node;
3845 stmt = build_stmt (DECL_EXPR, decl);
3846 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3847 TREE_SIDE_EFFECTS (complit) = 1;
3849 layout_decl (decl, 0);
3851 if (TREE_STATIC (decl))
3853 /* This decl needs a name for the assembler output. */
3854 set_compound_literal_name (decl);
3855 DECL_DEFER_OUTPUT (decl) = 1;
3856 DECL_COMDAT (decl) = 1;
3857 DECL_ARTIFICIAL (decl) = 1;
3858 DECL_IGNORED_P (decl) = 1;
3860 rest_of_decl_compilation (decl, 1, 0);
3866 /* Determine whether TYPE is a structure with a flexible array member,
3867 or a union containing such a structure (possibly recursively). */
3870 flexible_array_type_p (tree type)
3873 switch (TREE_CODE (type))
3876 x = TYPE_FIELDS (type);
3879 while (TREE_CHAIN (x) != NULL_TREE)
3881 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3882 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3883 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3884 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3888 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3890 if (flexible_array_type_p (TREE_TYPE (x)))
3899 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3900 replacing with appropriate values if they are invalid. */
3902 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3905 unsigned int max_width;
3906 unsigned HOST_WIDE_INT w;
3907 const char *name = orig_name ? orig_name: _("<anonymous>");
3909 /* Detect and ignore out of range field width and process valid
3911 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3912 || TREE_CODE (*width) != INTEGER_CST)
3914 error ("bit-field %qs width not an integer constant", name);
3915 *width = integer_one_node;
3919 constant_expression_warning (*width);
3920 if (tree_int_cst_sgn (*width) < 0)
3922 error ("negative width in bit-field %qs", name);
3923 *width = integer_one_node;
3925 else if (integer_zerop (*width) && orig_name)
3927 error ("zero width for bit-field %qs", name);
3928 *width = integer_one_node;
3932 /* Detect invalid bit-field type. */
3933 if (TREE_CODE (*type) != INTEGER_TYPE
3934 && TREE_CODE (*type) != BOOLEAN_TYPE
3935 && TREE_CODE (*type) != ENUMERAL_TYPE)
3937 error ("bit-field %qs has invalid type", name);
3938 *type = unsigned_type_node;
3941 type_mv = TYPE_MAIN_VARIANT (*type);
3943 && !in_system_header
3944 && type_mv != integer_type_node
3945 && type_mv != unsigned_type_node
3946 && type_mv != boolean_type_node)
3947 pedwarn ("type of bit-field %qs is a GCC extension", name);
3949 if (type_mv == boolean_type_node)
3950 max_width = CHAR_TYPE_SIZE;
3952 max_width = TYPE_PRECISION (*type);
3954 if (0 < compare_tree_int (*width, max_width))
3956 error ("width of %qs exceeds its type", name);
3958 *width = build_int_cst (NULL_TREE, w);
3961 w = tree_low_cst (*width, 1);
3963 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3965 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3967 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3968 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3969 warning (0, "%qs is narrower than values of its type", name);
3975 /* Print warning about variable length array if necessary. */
3978 warn_variable_length_array (const char *name, tree size)
3980 int ped = !flag_isoc99 && pedantic && warn_vla != 0;
3981 int const_size = TREE_CONSTANT (size);
3988 pedwarn ("ISO C90 forbids array %qs whose size "
3989 "can%'t be evaluated",
3992 pedwarn ("ISO C90 forbids array whose size "
3993 "can%'t be evaluated");
3998 pedwarn ("ISO C90 forbids variable length array %qs",
4001 pedwarn ("ISO C90 forbids variable length array");
4004 else if (warn_vla > 0)
4010 "the size of array %qs can"
4011 "%'t be evaluated", name);
4014 "the size of array can %'t be evaluated");
4020 "variable length array %qs is used",
4024 "variable length array is used");
4029 /* Given declspecs and a declarator,
4030 determine the name and type of the object declared
4031 and construct a ..._DECL node for it.
4032 (In one case we can return a ..._TYPE node instead.
4033 For invalid input we sometimes return 0.)
4035 DECLSPECS is a c_declspecs structure for the declaration specifiers.
4037 DECL_CONTEXT says which syntactic context this declaration is in:
4038 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4039 FUNCDEF for a function definition. Like NORMAL but a few different
4040 error messages in each case. Return value may be zero meaning
4041 this definition is too screwy to try to parse.
4042 PARM for a parameter declaration (either within a function prototype
4043 or before a function body). Make a PARM_DECL, or return void_type_node.
4044 TYPENAME if for a typename (in a cast or sizeof).
4045 Don't make a DECL node; just return the ..._TYPE node.
4046 FIELD for a struct or union field; make a FIELD_DECL.
4047 INITIALIZED is true if the decl has an initializer.
4048 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4049 representing the width of the bit-field.
4051 In the TYPENAME case, DECLARATOR is really an absolute declarator.
4052 It may also be so in the PARM case, for a prototype where the
4053 argument type is specified but not the name.
4055 This function is where the complicated C meanings of `static'
4056 and `extern' are interpreted. */
4059 grokdeclarator (const struct c_declarator *declarator,
4060 struct c_declspecs *declspecs,
4061 enum decl_context decl_context, bool initialized, tree *width)
4063 tree type = declspecs->type;
4064 bool threadp = declspecs->thread_p;
4065 enum c_storage_class storage_class = declspecs->storage_class;
4069 int type_quals = TYPE_UNQUALIFIED;
4070 const char *name, *orig_name;
4071 tree typedef_type = 0;
4072 bool funcdef_flag = false;
4073 bool funcdef_syntax = false;
4074 int size_varies = 0;
4075 tree decl_attr = declspecs->decl_attr;
4076 int array_ptr_quals = TYPE_UNQUALIFIED;
4077 tree array_ptr_attrs = NULL_TREE;
4078 int array_parm_static = 0;
4079 bool array_parm_vla_unspec_p = false;
4080 tree returned_attrs = NULL_TREE;
4081 bool bitfield = width != NULL;
4083 struct c_arg_info *arg_info = 0;
4085 if (decl_context == FUNCDEF)
4086 funcdef_flag = true, decl_context = NORMAL;
4088 /* Look inside a declarator for the name being declared
4089 and get it as a string, for an error message. */
4091 const struct c_declarator *decl = declarator;
4100 funcdef_syntax = (decl->kind == cdk_function);
4101 decl = decl->declarator;
4105 decl = decl->declarator;
4110 name = IDENTIFIER_POINTER (decl->u.id);
4122 /* A function definition's declarator must have the form of
4123 a function declarator. */
4125 if (funcdef_flag && !funcdef_syntax)
4128 /* If this looks like a function definition, make it one,
4129 even if it occurs where parms are expected.
4130 Then store_parm_decls will reject it and not use it as a parm. */
4131 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
4132 if (declspecs->unavailable_p)
4133 error_unavailable_use (declspecs->type);
4135 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
4136 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4137 decl_context = PARM;
4139 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4140 warn_deprecated_use (declspecs->type);
4142 if ((decl_context == NORMAL || decl_context == FIELD)
4143 && current_scope == file_scope
4144 && variably_modified_type_p (type, NULL_TREE))
4146 error ("variably modified %qs at file scope", name);
4147 type = integer_type_node;
4150 typedef_type = type;
4151 size_varies = C_TYPE_VARIABLE_SIZE (type);
4153 /* Diagnose defaulting to "int". */
4155 if (declspecs->default_int_p && !in_system_header)
4157 /* Issue a warning if this is an ISO C 99 program or if
4158 -Wreturn-type and this is a function, or if -Wimplicit;
4159 prefer the former warning since it is more explicit. */
4160 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4162 warn_about_return_type = 1;
4163 else if (warn_implicit_int || flag_isoc99)
4164 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
4167 /* Adjust the type if a bit-field is being declared,
4168 -funsigned-bitfields applied and the type is not explicitly
4170 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4171 && TREE_CODE (type) == INTEGER_TYPE)
4172 type = c_common_unsigned_type (type);
4174 /* Figure out the type qualifiers for the declaration. There are
4175 two ways a declaration can become qualified. One is something
4176 like `const int i' where the `const' is explicit. Another is
4177 something like `typedef const int CI; CI i' where the type of the
4178 declaration contains the `const'. A third possibility is that
4179 there is a type qualifier on the element type of a typedefed
4180 array type, in which case we should extract that qualifier so
4181 that c_apply_type_quals_to_decls receives the full list of
4182 qualifiers to work with (C90 is not entirely clear about whether
4183 duplicate qualifiers should be diagnosed in this case, but it
4184 seems most appropriate to do so). */
4185 element_type = strip_array_types (type);
4186 constp = declspecs->const_p + TYPE_READONLY (element_type);
4187 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4188 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4189 if (pedantic && !flag_isoc99)
4192 pedwarn ("duplicate %<const%>");
4194 pedwarn ("duplicate %<restrict%>");
4196 pedwarn ("duplicate %<volatile%>");
4198 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4199 type = TYPE_MAIN_VARIANT (type);
4200 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4201 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4202 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4204 /* Warn about storage classes that are invalid for certain
4205 kinds of declarations (parameters, typenames, etc.). */
4209 || storage_class == csc_auto
4210 || storage_class == csc_register
4211 || storage_class == csc_typedef))
4213 if (storage_class == csc_auto
4214 && (pedantic || current_scope == file_scope))
4215 pedwarn ("function definition declared %<auto%>");
4216 if (storage_class == csc_register)
4217 error ("function definition declared %<register%>");
4218 if (storage_class == csc_typedef)
4219 error ("function definition declared %<typedef%>");
4221 error ("function definition declared %<__thread%>");
4223 if (storage_class == csc_auto
4224 || storage_class == csc_register
4225 || storage_class == csc_typedef)
4226 storage_class = csc_none;
4228 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4230 if (decl_context == PARM && storage_class == csc_register)
4234 switch (decl_context)
4237 error ("storage class specified for structure field %qs",
4241 error ("storage class specified for parameter %qs", name);
4244 error ("storage class specified for typename");
4247 storage_class = csc_none;
4251 else if (storage_class == csc_extern
4255 /* 'extern' with initialization is invalid if not at file scope. */
4256 if (current_scope == file_scope)
4258 /* It is fine to have 'extern const' when compiling at C
4259 and C++ intersection. */
4260 if (!(warn_cxx_compat && constp))
4261 warning (0, "%qs initialized and declared %<extern%>", name);
4264 error ("%qs has both %<extern%> and initializer", name);
4266 else if (current_scope == file_scope)
4268 if (storage_class == csc_auto)
4269 error ("file-scope declaration of %qs specifies %<auto%>", name);
4270 if (pedantic && storage_class == csc_register)
4271 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
4275 if (storage_class == csc_extern && funcdef_flag)
4276 error ("nested function %qs declared %<extern%>", name);
4277 else if (threadp && storage_class == csc_none)
4279 error ("function-scope %qs implicitly auto and declared "
4286 /* Now figure out the structure of the declarator proper.
4287 Descend through it, creating more complex types, until we reach
4288 the declared identifier (or NULL_TREE, in an absolute declarator).
4289 At each stage we maintain an unqualified version of the type
4290 together with any qualifiers that should be applied to it with
4291 c_build_qualified_type; this way, array types including
4292 multidimensional array types are first built up in unqualified
4293 form and then the qualified form is created with
4294 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4296 while (declarator && declarator->kind != cdk_id)
4298 if (type == error_mark_node)
4300 declarator = declarator->declarator;
4304 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4305 a cdk_pointer (for *...),
4306 a cdk_function (for ...(...)),
4307 a cdk_attrs (for nested attributes),
4308 or a cdk_id (for the name being declared
4309 or the place in an absolute declarator
4310 where the name was omitted).
4311 For the last case, we have just exited the loop.
4313 At this point, TYPE is the type of elements of an array,
4314 or for a function to return, or for a pointer to point to.
4315 After this sequence of ifs, TYPE is the type of the
4316 array or function or pointer, and DECLARATOR has had its
4317 outermost layer removed. */
4319 if (array_ptr_quals != TYPE_UNQUALIFIED
4320 || array_ptr_attrs != NULL_TREE
4321 || array_parm_static)
4323 /* Only the innermost declarator (making a parameter be of
4324 array type which is converted to pointer type)
4325 may have static or type qualifiers. */
4326 error ("static or type qualifiers in non-parameter array declarator");
4327 array_ptr_quals = TYPE_UNQUALIFIED;
4328 array_ptr_attrs = NULL_TREE;
4329 array_parm_static = 0;
4332 switch (declarator->kind)
4336 /* A declarator with embedded attributes. */
4337 tree attrs = declarator->u.attrs;
4338 const struct c_declarator *inner_decl;
4340 declarator = declarator->declarator;
4341 inner_decl = declarator;
4342 while (inner_decl->kind == cdk_attrs)
4343 inner_decl = inner_decl->declarator;
4344 if (inner_decl->kind == cdk_id)
4345 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4346 else if (inner_decl->kind == cdk_function)
4347 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4348 else if (inner_decl->kind == cdk_array)
4349 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4350 returned_attrs = decl_attributes (&type,
4351 chainon (returned_attrs, attrs),
4357 tree itype = NULL_TREE;
4358 tree size = declarator->u.array.dimen;
4359 /* The index is a signed object `sizetype' bits wide. */
4360 tree index_type = c_common_signed_type (sizetype);
4362 array_ptr_quals = declarator->u.array.quals;
4363 array_ptr_attrs = declarator->u.array.attrs;
4364 array_parm_static = declarator->u.array.static_p;
4365 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4367 declarator = declarator->declarator;
4369 /* Check for some types that there cannot be arrays of. */
4371 if (VOID_TYPE_P (type))
4373 error ("declaration of %qs as array of voids", name);
4374 type = error_mark_node;
4377 if (TREE_CODE (type) == FUNCTION_TYPE)
4379 error ("declaration of %qs as array of functions", name);
4380 type = error_mark_node;
4383 if (pedantic && !in_system_header && flexible_array_type_p (type))
4384 pedwarn ("invalid use of structure with flexible array member");
4386 if (size == error_mark_node)
4387 type = error_mark_node;
4389 if (type == error_mark_node)
4392 /* If size was specified, set ITYPE to a range-type for
4393 that size. Otherwise, ITYPE remains null. finish_decl
4394 may figure it out from an initial value. */
4398 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4400 STRIP_TYPE_NOPS (size);
4402 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4404 error ("size of array %qs has non-integer type", name);
4405 size = integer_one_node;
4408 if (pedantic && integer_zerop (size))
4409 pedwarn ("ISO C forbids zero-size array %qs", name);
4411 if (TREE_CODE (size) == INTEGER_CST)
4413 constant_expression_warning (size);
4414 if (tree_int_cst_sgn (size) < 0)
4416 error ("size of array %qs is negative", name);
4417 size = integer_one_node;
4420 else if ((decl_context == NORMAL || decl_context == FIELD)
4421 && current_scope == file_scope)
4423 error ("variably modified %qs at file scope", name);
4424 size = integer_one_node;
4428 /* Make sure the array size remains visibly
4429 nonconstant even if it is (eg) a const variable
4430 with known value. */
4432 warn_variable_length_array (orig_name, size);
4433 if (warn_variable_decl)
4434 warning (0, "variable-sized array %qs", name);
4437 if (integer_zerop (size))
4439 /* A zero-length array cannot be represented with
4440 an unsigned index type, which is what we'll
4441 get with build_index_type. Create an
4442 open-ended range instead. */
4443 itype = build_range_type (sizetype, size, NULL_TREE);
4447 /* Arrange for the SAVE_EXPR on the inside of the
4448 MINUS_EXPR, which allows the -1 to get folded
4449 with the +1 that happens when building TYPE_SIZE. */
4451 size = variable_size (size);
4453 /* Compute the maximum valid index, that is, size
4454 - 1. Do the calculation in index_type, so that
4455 if it is a variable the computations will be
4456 done in the proper mode. */
4457 itype = fold_build2 (MINUS_EXPR, index_type,
4458 convert (index_type, size),
4459 convert (index_type,
4462 /* If that overflowed, the array is too big. ???
4463 While a size of INT_MAX+1 technically shouldn't
4464 cause an overflow (because we subtract 1), the
4465 overflow is recorded during the conversion to
4466 index_type, before the subtraction. Handling
4467 this case seems like an unnecessary
4469 if (TREE_CODE (itype) == INTEGER_CST
4470 && TREE_OVERFLOW (itype))
4472 error ("size of array %qs is too large", name);
4473 type = error_mark_node;
4477 itype = build_index_type (itype);
4480 else if (decl_context == FIELD)
4482 if (pedantic && !flag_isoc99 && !in_system_header)
4483 pedwarn ("ISO C90 does not support flexible array members");
4485 /* ISO C99 Flexible array members are effectively
4486 identical to GCC's zero-length array extension. */
4487 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4489 else if (decl_context == PARM)
4491 if (array_parm_vla_unspec_p)
4496 error ("%<[*]%> not allowed in other than a declaration");
4499 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4503 else if (decl_context == TYPENAME)
4505 if (array_parm_vla_unspec_p)
4507 /* The error is printed elsewhere. We use this to
4508 avoid messing up with incomplete array types of
4509 the same type, that would otherwise be modified
4511 itype = build_range_type (sizetype, size_zero_node,
4516 /* Complain about arrays of incomplete types. */
4517 if (!COMPLETE_TYPE_P (type))
4519 error ("array type has incomplete element type");
4520 type = error_mark_node;
4523 /* When itype is NULL, a shared incomplete array type is
4524 returned for all array of a given type. Elsewhere we
4525 make sure we don't complete that type before copying
4526 it, but here we want to make sure we don't ever
4527 modify the shared type, so we gcc_assert (itype)
4529 type = build_array_type (type, itype);
4531 if (type != error_mark_node)
4535 /* It is ok to modify type here even if itype is
4536 NULL: if size_varies, we're in a
4537 multi-dimensional array and the inner type has
4538 variable size, so the enclosing shared array type
4540 if (size && TREE_CODE (size) == INTEGER_CST)
4542 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4543 C_TYPE_VARIABLE_SIZE (type) = 1;
4546 /* The GCC extension for zero-length arrays differs from
4547 ISO flexible array members in that sizeof yields
4549 if (size && integer_zerop (size))
4552 TYPE_SIZE (type) = bitsize_zero_node;
4553 TYPE_SIZE_UNIT (type) = size_zero_node;
4555 if (array_parm_vla_unspec_p)
4558 /* The type is complete. C99 6.7.5.2p4 */
4559 TYPE_SIZE (type) = bitsize_zero_node;
4560 TYPE_SIZE_UNIT (type) = size_zero_node;
4564 if (decl_context != PARM
4565 && (array_ptr_quals != TYPE_UNQUALIFIED
4566 || array_ptr_attrs != NULL_TREE
4567 || array_parm_static))
4569 error ("static or type qualifiers in non-parameter array declarator");
4570 array_ptr_quals = TYPE_UNQUALIFIED;
4571 array_ptr_attrs = NULL_TREE;
4572 array_parm_static = 0;
4578 /* Say it's a definition only for the declarator closest
4579 to the identifier, apart possibly from some
4581 bool really_funcdef = false;
4585 const struct c_declarator *t = declarator->declarator;
4586 while (t->kind == cdk_attrs)
4588 really_funcdef = (t->kind == cdk_id);
4591 /* Declaring a function type. Make sure we have a valid
4592 type for the function to return. */
4593 if (type == error_mark_node)
4598 /* Warn about some types functions can't return. */
4599 if (TREE_CODE (type) == FUNCTION_TYPE)
4601 error ("%qs declared as function returning a function", name);
4602 type = integer_type_node;
4604 if (TREE_CODE (type) == ARRAY_TYPE)
4606 error ("%qs declared as function returning an array", name);
4607 type = integer_type_node;
4610 /* Construct the function type and go to the next
4611 inner layer of declarator. */
4612 arg_info = declarator->u.arg_info;
4613 arg_types = grokparms (arg_info, really_funcdef);
4615 put_pending_sizes (arg_info->pending_sizes);
4617 /* Type qualifiers before the return type of the function
4618 qualify the return type, not the function type. */
4621 /* Type qualifiers on a function return type are
4622 normally permitted by the standard but have no
4623 effect, so give a warning at -Wreturn-type.
4624 Qualifiers on a void return type are banned on
4625 function definitions in ISO C; GCC used to used
4626 them for noreturn functions. */
4627 if (VOID_TYPE_P (type) && really_funcdef)
4628 pedwarn ("function definition has qualified void return type");
4630 warning (OPT_Wreturn_type,
4631 "type qualifiers ignored on function return type");
4633 type = c_build_qualified_type (type, type_quals);
4635 type_quals = TYPE_UNQUALIFIED;
4637 type = build_function_type (type, arg_types);
4638 declarator = declarator->declarator;
4640 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4641 the formal parameter list of this FUNCTION_TYPE to point to
4642 the FUNCTION_TYPE node itself. */
4646 for (link = arg_info->tags;
4648 link = TREE_CHAIN (link))
4649 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4655 /* Merge any constancy or volatility into the target type
4658 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4660 pedwarn ("ISO C forbids qualified function types");
4662 type = c_build_qualified_type (type, type_quals);
4665 /* When the pointed-to type involves components of variable size,
4666 care must be taken to ensure that the size evaluation code is
4667 emitted early enough to dominate all the possible later uses
4668 and late enough for the variables on which it depends to have
4671 This is expected to happen automatically when the pointed-to
4672 type has a name/declaration of it's own, but special attention
4673 is required if the type is anonymous.
4675 We handle the NORMAL and FIELD contexts here by attaching an
4676 artificial TYPE_DECL to such pointed-to type. This forces the
4677 sizes evaluation at a safe point and ensures it is not deferred
4678 until e.g. within a deeper conditional context.
4680 We expect nothing to be needed here for PARM or TYPENAME.
4681 Pushing a TYPE_DECL at this point for TYPENAME would actually
4682 be incorrect, as we might be in the middle of an expression
4683 with side effects on the pointed-to type size "arguments" prior
4684 to the pointer declaration point and the fake TYPE_DECL in the
4685 enclosing context would force the size evaluation prior to the
4688 if (!TYPE_NAME (type)
4689 && (decl_context == NORMAL || decl_context == FIELD)
4690 && variably_modified_type_p (type, NULL_TREE))
4692 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4693 DECL_ARTIFICIAL (decl) = 1;
4695 finish_decl (decl, NULL_TREE, NULL_TREE);
4696 TYPE_NAME (type) = decl;
4699 type = build_pointer_type (type);
4701 /* Process type qualifiers (such as const or volatile)
4702 that were given inside the `*'. */
4703 type_quals = declarator->u.pointer_quals;
4705 declarator = declarator->declarator;
4713 /* Now TYPE has the actual type, apart from any qualifiers in
4716 /* Check the type and width of a bit-field. */
4718 check_bitfield_type_and_width (&type, width, orig_name);
4720 /* Did array size calculations overflow? */
4722 if (TREE_CODE (type) == ARRAY_TYPE
4723 && COMPLETE_TYPE_P (type)
4724 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4725 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4727 error ("size of array %qs is too large", name);
4728 /* If we proceed with the array type as it is, we'll eventually
4729 crash in tree_low_cst(). */
4730 type = error_mark_node;
4733 /* If this is declaring a typedef name, return a TYPE_DECL. */
4735 if (storage_class == csc_typedef)
4738 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4740 pedwarn ("ISO C forbids qualified function types");
4742 type = c_build_qualified_type (type, type_quals);
4743 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4744 if (declspecs->explicit_signed_p)
4745 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4746 decl_attributes (&decl, returned_attrs, 0);
4747 if (declspecs->inline_p)
4748 pedwarn ("typedef %q+D declared %<inline%>", decl);
4752 /* If this is a type name (such as, in a cast or sizeof),
4753 compute the type and return it now. */
4755 if (decl_context == TYPENAME)
4757 /* Note that the grammar rejects storage classes in typenames
4759 gcc_assert (storage_class == csc_none && !threadp
4760 && !declspecs->inline_p);
4761 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4763 pedwarn ("ISO C forbids const or volatile function types");
4765 type = c_build_qualified_type (type, type_quals);
4766 decl_attributes (&type, returned_attrs, 0);
4770 if (pedantic && decl_context == FIELD
4771 && variably_modified_type_p (type, NULL_TREE))
4774 pedwarn ("a member of a structure or union cannot have a variably modified type");
4777 /* Aside from typedefs and type names (handle above),
4778 `void' at top level (not within pointer)
4779 is allowed only in public variables.
4780 We don't complain about parms either, but that is because
4781 a better error message can be made later. */
4783 if (VOID_TYPE_P (type) && decl_context != PARM
4784 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4785 && (storage_class == csc_extern
4786 || (current_scope == file_scope
4787 && !(storage_class == csc_static
4788 || storage_class == csc_register)))))
4790 error ("variable or field %qs declared void", name);
4791 type = integer_type_node;
4794 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4795 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4800 if (decl_context == PARM)
4802 tree type_as_written;
4805 /* A parameter declared as an array of T is really a pointer to T.
4806 One declared as a function is really a pointer to a function. */
4808 if (TREE_CODE (type) == ARRAY_TYPE)
4810 /* Transfer const-ness of array into that of type pointed to. */
4811 type = TREE_TYPE (type);
4813 type = c_build_qualified_type (type, type_quals);
4814 type = build_pointer_type (type);
4815 type_quals = array_ptr_quals;
4817 /* We don't yet implement attributes in this context. */
4818 if (array_ptr_attrs != NULL_TREE)
4819 warning (OPT_Wattributes,
4820 "attributes in parameter array declarator ignored");
4824 else if (TREE_CODE (type) == FUNCTION_TYPE)
4826 if (pedantic && type_quals)
4827 pedwarn ("ISO C forbids qualified function types");
4829 type = c_build_qualified_type (type, type_quals);
4830 type = build_pointer_type (type);
4831 type_quals = TYPE_UNQUALIFIED;
4833 else if (type_quals)
4834 type = c_build_qualified_type (type, type_quals);
4836 type_as_written = type;
4838 decl = build_decl (PARM_DECL, declarator->u.id, type);
4840 C_DECL_VARIABLE_SIZE (decl) = 1;
4842 /* Compute the type actually passed in the parmlist,
4843 for the case where there is no prototype.
4844 (For example, shorts and chars are passed as ints.)
4845 When there is a prototype, this is overridden later. */
4847 if (type == error_mark_node)
4848 promoted_type = type;
4850 promoted_type = c_type_promotes_to (type);
4852 DECL_ARG_TYPE (decl) = promoted_type;
4853 if (declspecs->inline_p)
4854 pedwarn ("parameter %q+D declared %<inline%>", decl);
4856 else if (decl_context == FIELD)
4858 /* Note that the grammar rejects storage classes in typenames
4860 gcc_assert (storage_class == csc_none && !threadp
4861 && !declspecs->inline_p);
4863 /* Structure field. It may not be a function. */
4865 if (TREE_CODE (type) == FUNCTION_TYPE)
4867 error ("field %qs declared as a function", name);
4868 type = build_pointer_type (type);
4870 else if (TREE_CODE (type) != ERROR_MARK
4871 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4873 error ("field %qs has incomplete type", name);
4874 type = error_mark_node;
4876 type = c_build_qualified_type (type, type_quals);
4877 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4878 DECL_NONADDRESSABLE_P (decl) = bitfield;
4881 C_DECL_VARIABLE_SIZE (decl) = 1;
4883 else if (TREE_CODE (type) == FUNCTION_TYPE)
4885 if (storage_class == csc_register || threadp)
4887 error ("invalid storage class for function %qs", name);
4889 else if (current_scope != file_scope)
4891 /* Function declaration not at file scope. Storage
4892 classes other than `extern' are not allowed, C99
4893 6.7.1p5, and `extern' makes no difference. However,
4894 GCC allows 'auto', perhaps with 'inline', to support
4895 nested functions. */
4896 if (storage_class == csc_auto)
4899 pedwarn ("invalid storage class for function %qs", name);
4901 else if (storage_class == csc_static)
4903 error ("invalid storage class for function %qs", name);
4905 storage_class = declspecs->storage_class = csc_none;
4911 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4912 decl = build_decl_attribute_variant (decl, decl_attr);
4914 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4916 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4917 pedwarn ("ISO C forbids qualified function types");
4919 /* GNU C interprets a volatile-qualified function type to indicate
4920 that the function does not return. */
4921 if ((type_quals & TYPE_QUAL_VOLATILE)
4922 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4923 warning (0, "%<noreturn%> function returns non-void value");
4925 /* Every function declaration is an external reference
4926 (DECL_EXTERNAL) except for those which are not at file
4927 scope and are explicitly declared "auto". This is
4928 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4929 GCC to signify a forward declaration of a nested function. */
4930 if (storage_class == csc_auto && current_scope != file_scope)
4931 DECL_EXTERNAL (decl) = 0;
4932 /* In C99, a function which is declared 'inline' with 'extern'
4933 is not an external reference (which is confusing). It
4934 means that the later definition of the function must be output
4935 in this file, C99 6.7.4p6. In GNU C89, a function declared
4936 'extern inline' is an external reference. */
4937 else if (declspecs->inline_p && storage_class != csc_static)
4938 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
4939 == flag_gnu89_inline);
4941 DECL_EXTERNAL (decl) = !initialized;
4943 /* Record absence of global scope for `static' or `auto'. */
4945 = !(storage_class == csc_static || storage_class == csc_auto);
4947 /* For a function definition, record the argument information
4948 block where store_parm_decls will look for it. */
4950 current_function_arg_info = arg_info;
4952 if (declspecs->default_int_p)
4953 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4955 /* Record presence of `inline', if it is reasonable. */
4956 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4958 if (declspecs->inline_p)
4959 pedwarn ("cannot inline function %<main%>");
4961 else if (declspecs->inline_p)
4963 /* Record that the function is declared `inline'. */
4964 DECL_DECLARED_INLINE_P (decl) = 1;
4966 /* Do not mark bare declarations as DECL_INLINE. Doing so
4967 in the presence of multiple declarations can result in
4968 the abstract origin pointing between the declarations,
4969 which will confuse dwarf2out. */
4971 DECL_INLINE (decl) = 1;
4973 /* If -finline-functions, assume it can be inlined. This does
4974 two things: let the function be deferred until it is actually
4975 needed, and let dwarf2 know that the function is inlinable. */
4976 else if (flag_inline_trees == 2 && initialized)
4977 DECL_INLINE (decl) = 1;
4981 /* It's a variable. */
4982 /* An uninitialized decl with `extern' is a reference. */
4983 int extern_ref = !initialized && storage_class == csc_extern;
4985 type = c_build_qualified_type (type, type_quals);
4987 /* C99 6.2.2p7: It is invalid (compile-time undefined
4988 behavior) to create an 'extern' declaration for a
4989 variable if there is a global declaration that is
4990 'static' and the global declaration is not visible.
4991 (If the static declaration _is_ currently visible,
4992 the 'extern' declaration is taken to refer to that decl.) */
4993 if (extern_ref && current_scope != file_scope)
4995 tree global_decl = identifier_global_value (declarator->u.id);
4996 tree visible_decl = lookup_name (declarator->u.id);
4999 && global_decl != visible_decl
5000 && TREE_CODE (global_decl) == VAR_DECL
5001 && !TREE_PUBLIC (global_decl))
5002 error ("variable previously declared %<static%> redeclared "
5006 decl = build_decl (VAR_DECL, declarator->u.id, type);
5007 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5009 C_DECL_VARIABLE_SIZE (decl) = 1;
5011 if (declspecs->inline_p)
5012 pedwarn ("variable %q+D declared %<inline%>", decl);
5014 /* At file scope, an initialized extern declaration may follow
5015 a static declaration. In that case, DECL_EXTERNAL will be
5016 reset later in start_decl. */
5017 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
5019 /* At file scope, the presence of a `static' or `register' storage
5020 class specifier, or the absence of all storage class specifiers
5021 makes this declaration a definition (perhaps tentative). Also,
5022 the absence of `static' makes it public. */
5023 if (current_scope == file_scope)
5025 TREE_PUBLIC (decl) = storage_class != csc_static;
5026 TREE_STATIC (decl) = !extern_ref;
5028 /* Not at file scope, only `static' makes a static definition. */
5031 TREE_STATIC (decl) = (storage_class == csc_static);
5032 TREE_PUBLIC (decl) = extern_ref;
5037 if (targetm.have_tls)
5038 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
5040 /* A mere warning is sure to result in improper semantics
5041 at runtime. Don't bother to allow this to compile. */
5042 error ("thread-local storage not supported for this target");
5046 if (storage_class == csc_extern
5047 && variably_modified_type_p (type, NULL_TREE))
5050 error ("object with variably modified type must have no linkage");
5053 /* Record `register' declaration for warnings on &
5054 and in case doing stupid register allocation. */
5056 if (storage_class == csc_register)
5058 C_DECL_REGISTER (decl) = 1;
5059 DECL_REGISTER (decl) = 1;
5062 /* Record constancy and volatility. */
5063 c_apply_type_quals_to_decl (type_quals, decl);
5065 /* If a type has volatile components, it should be stored in memory.
5066 Otherwise, the fact that those components are volatile
5067 will be ignored, and would even crash the compiler.
5068 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
5069 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
5070 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
5071 || TREE_CODE (decl) == RESULT_DECL))
5073 /* It is not an error for a structure with volatile fields to
5074 be declared register, but reset DECL_REGISTER since it
5075 cannot actually go in a register. */
5076 int was_reg = C_DECL_REGISTER (decl);
5077 C_DECL_REGISTER (decl) = 0;
5078 DECL_REGISTER (decl) = 0;
5079 c_mark_addressable (decl);
5080 C_DECL_REGISTER (decl) = was_reg;
5083 /* This is the earliest point at which we might know the assembler
5084 name of a variable. Thus, if it's known before this, die horribly. */
5085 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
5087 decl_attributes (&decl, returned_attrs, 0);
5093 /* Decode the parameter-list info for a function type or function definition.
5094 The argument is the value returned by `get_parm_info' (or made in c-parse.c
5095 if there is an identifier list instead of a parameter decl list).
5096 These two functions are separate because when a function returns
5097 or receives functions then each is called multiple times but the order
5098 of calls is different. The last call to `grokparms' is always the one
5099 that contains the formal parameter names of a function definition.
5101 Return a list of arg types to use in the FUNCTION_TYPE for this function.
5103 FUNCDEF_FLAG is true for a function definition, false for
5104 a mere declaration. A nonempty identifier-list gets an error message
5105 when FUNCDEF_FLAG is false. */
5108 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
5110 tree arg_types = arg_info->types;
5112 if (funcdef_flag && arg_info->had_vla_unspec)
5114 /* A function definition isn't function prototype scope C99 6.2.1p4. */
5116 error ("%<[*]%> not allowed in other than function prototype scope");
5119 if (arg_types == 0 && !funcdef_flag && !in_system_header)
5120 warning (OPT_Wstrict_prototypes,
5121 "function declaration isn%'t a prototype");
5123 if (arg_types == error_mark_node)
5124 return 0; /* don't set TYPE_ARG_TYPES in this case */
5126 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5129 pedwarn ("parameter names (without types) in function declaration");
5131 arg_info->parms = arg_info->types;
5132 arg_info->types = 0;
5137 tree parm, type, typelt;
5138 unsigned int parmno;
5140 /* If there is a parameter of incomplete type in a definition,
5141 this is an error. In a declaration this is valid, and a
5142 struct or union type may be completed later, before any calls
5143 or definition of the function. In the case where the tag was
5144 first declared within the parameter list, a warning has
5145 already been given. If a parameter has void type, then
5146 however the function cannot be defined or called, so
5149 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5151 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5153 type = TREE_VALUE (typelt);
5154 if (type == error_mark_node)
5157 if (!COMPLETE_TYPE_P (type))
5161 if (DECL_NAME (parm))
5162 error ("parameter %u (%q+D) has incomplete type",
5165 error ("%Jparameter %u has incomplete type",
5168 TREE_VALUE (typelt) = error_mark_node;
5169 TREE_TYPE (parm) = error_mark_node;
5171 else if (VOID_TYPE_P (type))
5173 if (DECL_NAME (parm))
5174 warning (0, "parameter %u (%q+D) has void type",
5177 warning (0, "%Jparameter %u has void type",
5182 if (DECL_NAME (parm) && TREE_USED (parm))
5183 warn_if_shadowing (parm);
5189 /* Take apart the current scope and return a c_arg_info structure with
5190 info on a parameter list just parsed.
5192 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5194 ELLIPSIS being true means the argument list ended in '...' so don't
5195 append a sentinel (void_list_node) to the end of the type-list. */
5198 get_parm_info (bool ellipsis)
5200 struct c_binding *b = current_scope->bindings;
5201 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5208 static bool explained_incomplete_types = false;
5209 bool gave_void_only_once_err = false;
5211 arg_info->parms = 0;
5213 arg_info->types = 0;
5214 arg_info->others = 0;
5215 arg_info->pending_sizes = 0;
5216 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5218 /* The bindings in this scope must not get put into a block.
5219 We will take care of deleting the binding nodes. */
5220 current_scope->bindings = 0;
5222 /* This function is only called if there was *something* on the
5226 /* A parameter list consisting solely of 'void' indicates that the
5227 function takes no arguments. But if the 'void' is qualified
5228 (by 'const' or 'volatile'), or has a storage class specifier
5229 ('register'), then the behavior is undefined; issue an error.
5230 Typedefs for 'void' are OK (see DR#157). */
5231 if (b->prev == 0 /* one binding */
5232 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5233 && !DECL_NAME (b->decl) /* anonymous */
5234 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5236 if (TREE_THIS_VOLATILE (b->decl)
5237 || TREE_READONLY (b->decl)
5238 || C_DECL_REGISTER (b->decl))
5239 error ("%<void%> as only parameter may not be qualified");
5241 /* There cannot be an ellipsis. */
5243 error ("%<void%> must be the only parameter");
5245 arg_info->types = void_list_node;
5250 types = void_list_node;
5252 /* Break up the bindings list into parms, tags, types, and others;
5253 apply sanity checks; purge the name-to-decl bindings. */
5256 tree decl = b->decl;
5257 tree type = TREE_TYPE (decl);
5258 const char *keyword;
5260 switch (TREE_CODE (decl))
5265 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5266 I_SYMBOL_BINDING (b->id) = b->shadowed;
5269 /* Check for forward decls that never got their actual decl. */
5270 if (TREE_ASM_WRITTEN (decl))
5271 error ("parameter %q+D has just a forward declaration", decl);
5272 /* Check for (..., void, ...) and issue an error. */
5273 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5275 if (!gave_void_only_once_err)
5277 error ("%<void%> must be the only parameter");
5278 gave_void_only_once_err = true;
5283 /* Valid parameter, add it to the list. */
5284 TREE_CHAIN (decl) = parms;
5287 /* Since there is a prototype, args are passed in their
5288 declared types. The back end may override this later. */
5289 DECL_ARG_TYPE (decl) = type;
5290 types = tree_cons (0, type, types);
5294 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5295 case UNION_TYPE: keyword = "union"; goto tag;
5296 case RECORD_TYPE: keyword = "struct"; goto tag;
5298 /* Types may not have tag-names, in which case the type
5299 appears in the bindings list with b->id NULL. */
5302 gcc_assert (I_TAG_BINDING (b->id) == b);
5303 I_TAG_BINDING (b->id) = b->shadowed;
5306 /* Warn about any struct, union or enum tags defined in a
5307 parameter list. The scope of such types is limited to
5308 the parameter list, which is rarely if ever desirable
5309 (it's impossible to call such a function with type-
5310 correct arguments). An anonymous union parm type is
5311 meaningful as a GNU extension, so don't warn for that. */
5312 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5315 /* The %s will be one of 'struct', 'union', or 'enum'. */
5316 warning (0, "%<%s %E%> declared inside parameter list",
5319 /* The %s will be one of 'struct', 'union', or 'enum'. */
5320 warning (0, "anonymous %s declared inside parameter list",
5323 if (!explained_incomplete_types)
5325 warning (0, "its scope is only this definition or declaration,"
5326 " which is probably not what you want");
5327 explained_incomplete_types = true;
5331 tags = tree_cons (b->id, decl, tags);
5337 /* CONST_DECLs appear here when we have an embedded enum,
5338 and TYPE_DECLs appear here when we have an embedded struct
5339 or union. No warnings for this - we already warned about the
5340 type itself. FUNCTION_DECLs appear when there is an implicit
5341 function declaration in the parameter list. */
5343 TREE_CHAIN (decl) = others;
5348 /* error_mark_node appears here when we have an undeclared
5349 variable. Just throw it away. */
5352 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5353 I_SYMBOL_BINDING (b->id) = b->shadowed;
5357 /* Other things that might be encountered. */
5364 b = free_binding_and_advance (b);
5367 arg_info->parms = parms;
5368 arg_info->tags = tags;
5369 arg_info->types = types;
5370 arg_info->others = others;
5371 arg_info->pending_sizes = get_pending_sizes ();
5375 /* Get the struct, enum or union (CODE says which) with tag NAME.
5376 Define the tag as a forward-reference if it is not defined.
5377 Return a c_typespec structure for the type specifier. */
5380 parser_xref_tag (enum tree_code code, tree name)
5382 struct c_typespec ret;
5383 /* If a cross reference is requested, look up the type
5384 already defined for this tag and return it. */
5386 tree ref = lookup_tag (code, name, 0);
5387 /* If this is the right type of tag, return what we found.
5388 (This reference will be shadowed by shadow_tag later if appropriate.)
5389 If this is the wrong type of tag, do not return it. If it was the
5390 wrong type in the same scope, we will have had an error
5391 message already; if in a different scope and declaring
5392 a name, pending_xref_error will give an error message; but if in a
5393 different scope and not declaring a name, this tag should
5394 shadow the previous declaration of a different type of tag, and
5395 this would not work properly if we return the reference found.
5396 (For example, with "struct foo" in an outer scope, "union foo;"
5397 must shadow that tag with a new one of union type.) */
5398 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5399 if (ref && TREE_CODE (ref) == code)
5405 /* If no such tag is yet defined, create a forward-reference node
5406 and record it as the "definition".
5407 When a real declaration of this type is found,
5408 the forward-reference will be altered into a real type. */
5410 ref = make_node (code);
5411 if (code == ENUMERAL_TYPE)
5413 /* Give the type a default layout like unsigned int
5414 to avoid crashing if it does not get defined. */
5415 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5416 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5417 TYPE_USER_ALIGN (ref) = 0;
5418 TYPE_UNSIGNED (ref) = 1;
5419 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5420 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5421 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5424 pushtag (name, ref);
5430 /* Get the struct, enum or union (CODE says which) with tag NAME.
5431 Define the tag as a forward-reference if it is not defined.
5432 Return a tree for the type. */
5435 xref_tag (enum tree_code code, tree name)
5437 return parser_xref_tag (code, name).spec;
5440 /* Make sure that the tag NAME is defined *in the current scope*
5441 at least as a forward reference.
5442 CODE says which kind of tag NAME ought to be. */
5445 start_struct (enum tree_code code, tree name)
5447 /* If there is already a tag defined at this scope
5448 (as a forward reference), just return it. */
5453 ref = lookup_tag (code, name, 1);
5454 if (ref && TREE_CODE (ref) == code)
5456 if (TYPE_SIZE (ref))
5458 if (code == UNION_TYPE)
5459 error ("redefinition of %<union %E%>", name);
5461 error ("redefinition of %<struct %E%>", name);
5463 else if (C_TYPE_BEING_DEFINED (ref))
5465 if (code == UNION_TYPE)
5466 error ("nested redefinition of %<union %E%>", name);
5468 error ("nested redefinition of %<struct %E%>", name);
5469 /* Don't create structures that contain themselves. */
5474 /* Otherwise create a forward-reference just so the tag is in scope. */
5476 if (ref == NULL_TREE || TREE_CODE (ref) != code)
5478 ref = make_node (code);
5479 pushtag (name, ref);
5482 C_TYPE_BEING_DEFINED (ref) = 1;
5483 TYPE_PACKED (ref) = flag_pack_struct;
5487 /* Process the specs, declarator and width (NULL if omitted)
5488 of a structure component, returning a FIELD_DECL node.
5489 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5491 This is done during the parsing of the struct declaration.
5492 The FIELD_DECL nodes are chained together and the lot of them
5493 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5496 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5501 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5502 && width == NULL_TREE)
5504 /* This is an unnamed decl.
5506 If we have something of the form "union { list } ;" then this
5507 is the anonymous union extension. Similarly for struct.
5509 If this is something of the form "struct foo;", then
5510 If MS extensions are enabled, this is handled as an
5512 Otherwise this is a forward declaration of a structure tag.
5514 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5515 If MS extensions are enabled and foo names a structure, then
5516 again this is an anonymous struct.
5517 Otherwise this is an error.
5519 Oh what a horrid tangled web we weave. I wonder if MS consciously
5520 took this from Plan 9 or if it was an accident of implementation
5521 that took root before someone noticed the bug... */
5523 tree type = declspecs->type;
5524 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5525 || TREE_CODE (type) == UNION_TYPE);
5529 && (flag_ms_extensions || !declspecs->typedef_p))
5531 if (flag_ms_extensions)
5535 else if (TYPE_NAME (type) == NULL)
5542 pedwarn ("declaration does not declare anything");
5546 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5549 value = grokdeclarator (declarator, declspecs, FIELD, false,
5550 width ? &width : NULL);
5552 finish_decl (value, NULL_TREE, NULL_TREE);
5553 DECL_INITIAL (value) = width;
5558 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5559 the list such that this does not present a problem later. */
5562 detect_field_duplicates (tree fieldlist)
5567 /* First, see if there are more than "a few" fields.
5568 This is trivially true if there are zero or one fields. */
5571 x = TREE_CHAIN (fieldlist);
5577 } while (timeout > 0 && x);
5579 /* If there were "few" fields, avoid the overhead of allocating
5580 a hash table. Instead just do the nested traversal thing. */
5583 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5586 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5587 if (DECL_NAME (y) == DECL_NAME (x))
5589 error ("duplicate member %q+D", x);
5590 DECL_NAME (x) = NULL_TREE;
5596 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5599 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5600 if ((y = DECL_NAME (x)) != 0)
5602 slot = htab_find_slot (htab, y, INSERT);
5605 error ("duplicate member %q+D", x);
5606 DECL_NAME (x) = NULL_TREE;
5615 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5616 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5617 ATTRIBUTES are attributes to be applied to the structure. */
5620 finish_struct (tree t, tree fieldlist, tree attributes)
5623 bool toplevel = file_scope == current_scope;
5624 int saw_named_field;
5626 /* If this type was previously laid out as a forward reference,
5627 make sure we lay it out again. */
5631 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5635 for (x = fieldlist; x; x = TREE_CHAIN (x))
5636 if (DECL_NAME (x) != 0)
5641 if (TREE_CODE (t) == UNION_TYPE)
5644 pedwarn ("union has no named members");
5646 pedwarn ("union has no members");
5651 pedwarn ("struct has no named members");
5653 pedwarn ("struct has no members");
5658 /* Install struct as DECL_CONTEXT of each field decl.
5659 Also process specified field sizes, found in the DECL_INITIAL,
5660 storing 0 there after the type has been changed to precision equal
5661 to its width, rather than the precision of the specified standard
5662 type. (Correct layout requires the original type to have been preserved
5665 saw_named_field = 0;
5666 for (x = fieldlist; x; x = TREE_CHAIN (x))
5668 if (TREE_TYPE (x) == error_mark_node)
5671 DECL_CONTEXT (x) = t;
5673 if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
5674 DECL_PACKED (x) = 1;
5676 /* If any field is const, the structure type is pseudo-const. */
5677 if (TREE_READONLY (x))
5678 C_TYPE_FIELDS_READONLY (t) = 1;
5681 /* A field that is pseudo-const makes the structure likewise. */
5682 tree t1 = TREE_TYPE (x);
5683 while (TREE_CODE (t1) == ARRAY_TYPE)
5684 t1 = TREE_TYPE (t1);
5685 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5686 && C_TYPE_FIELDS_READONLY (t1))
5687 C_TYPE_FIELDS_READONLY (t) = 1;
5690 /* Any field that is volatile means variables of this type must be
5691 treated in some ways as volatile. */
5692 if (TREE_THIS_VOLATILE (x))
5693 C_TYPE_FIELDS_VOLATILE (t) = 1;
5695 /* Any field of nominal variable size implies structure is too. */
5696 if (C_DECL_VARIABLE_SIZE (x))
5697 C_TYPE_VARIABLE_SIZE (t) = 1;
5699 if (DECL_INITIAL (x))
5701 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5702 DECL_SIZE (x) = bitsize_int (width);
5703 DECL_BIT_FIELD (x) = 1;
5704 SET_DECL_C_BIT_FIELD (x);
5707 /* Detect flexible array member in an invalid context. */
5708 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5709 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5710 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5711 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5713 if (TREE_CODE (t) == UNION_TYPE)
5715 error ("%Jflexible array member in union", x);
5716 TREE_TYPE (x) = error_mark_node;
5718 else if (TREE_CHAIN (x) != NULL_TREE)
5720 error ("%Jflexible array member not at end of struct", x);
5721 TREE_TYPE (x) = error_mark_node;
5723 else if (!saw_named_field)
5725 error ("%Jflexible array member in otherwise empty struct", x);
5726 TREE_TYPE (x) = error_mark_node;
5730 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5731 && flexible_array_type_p (TREE_TYPE (x)))
5732 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5735 saw_named_field = 1;
5738 detect_field_duplicates (fieldlist);
5740 /* Now we have the nearly final fieldlist. Record it,
5741 then lay out the structure or union (including the fields). */
5743 TYPE_FIELDS (t) = fieldlist;
5747 /* Give bit-fields their proper types. */
5749 tree *fieldlistp = &fieldlist;
5751 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5752 && TREE_TYPE (*fieldlistp) != error_mark_node)
5754 unsigned HOST_WIDE_INT width
5755 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5756 tree type = TREE_TYPE (*fieldlistp);
5757 if (width != TYPE_PRECISION (type))
5759 TREE_TYPE (*fieldlistp)
5760 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5761 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5763 DECL_INITIAL (*fieldlistp) = 0;
5766 fieldlistp = &TREE_CHAIN (*fieldlistp);
5769 /* Now we have the truly final field list.
5770 Store it in this type and in the variants. */
5772 TYPE_FIELDS (t) = fieldlist;
5774 /* If there are lots of fields, sort so we can look through them fast.
5775 We arbitrarily consider 16 or more elts to be "a lot". */
5780 for (x = fieldlist; x; x = TREE_CHAIN (x))
5782 if (len > 15 || DECL_NAME (x) == NULL)
5790 struct lang_type *space;
5791 struct sorted_fields_type *space2;
5793 len += list_length (x);
5795 /* Use the same allocation policy here that make_node uses, to
5796 ensure that this lives as long as the rest of the struct decl.
5797 All decls in an inline function need to be saved. */
5799 space = GGC_CNEW (struct lang_type);
5800 space2 = GGC_NEWVAR (struct sorted_fields_type,
5801 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5805 field_array = &space2->elts[0];
5806 for (x = fieldlist; x; x = TREE_CHAIN (x))
5808 field_array[len++] = x;
5810 /* If there is anonymous struct or union, break out of the loop. */
5811 if (DECL_NAME (x) == NULL)
5814 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5817 TYPE_LANG_SPECIFIC (t) = space;
5818 TYPE_LANG_SPECIFIC (t)->s->len = len;
5819 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5820 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5825 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5827 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5828 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5829 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5830 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5831 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5834 /* If this was supposed to be a transparent union, but we can't
5835 make it one, warn and turn off the flag. */
5836 if (TREE_CODE (t) == UNION_TYPE
5837 && TYPE_TRANSPARENT_UNION (t)
5838 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5840 TYPE_TRANSPARENT_UNION (t) = 0;
5841 warning (0, "union cannot be made transparent");
5844 /* If this structure or union completes the type of any previous
5845 variable declaration, lay it out and output its rtl. */
5846 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5850 tree decl = TREE_VALUE (x);
5851 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5852 layout_array_type (TREE_TYPE (decl));
5853 if (TREE_CODE (decl) != TYPE_DECL)
5855 layout_decl (decl, 0);
5856 if (c_dialect_objc ())
5857 objc_check_decl (decl);
5858 rest_of_decl_compilation (decl, toplevel, 0);
5863 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5865 /* Finish debugging output for this type. */
5866 rest_of_type_compilation (t, toplevel);
5868 /* If we're inside a function proper, i.e. not file-scope and not still
5869 parsing parameters, then arrange for the size of a variable sized type
5871 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
5872 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5877 /* Lay out the type T, and its element type, and so on. */
5880 layout_array_type (tree t)
5882 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5883 layout_array_type (TREE_TYPE (t));
5887 /* Begin compiling the definition of an enumeration type.
5888 NAME is its name (or null if anonymous).
5889 Returns the type object, as yet incomplete.
5890 Also records info about it so that build_enumerator
5891 may be used to declare the individual values as they are read. */
5894 start_enum (tree name)
5898 /* If this is the real definition for a previous forward reference,
5899 fill in the contents in the same object that used to be the
5900 forward reference. */
5903 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5905 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5907 enumtype = make_node (ENUMERAL_TYPE);
5908 pushtag (name, enumtype);
5911 if (C_TYPE_BEING_DEFINED (enumtype))
5912 error ("nested redefinition of %<enum %E%>", name);
5914 C_TYPE_BEING_DEFINED (enumtype) = 1;
5916 if (TYPE_VALUES (enumtype) != 0)
5918 /* This enum is a named one that has been declared already. */
5919 error ("redeclaration of %<enum %E%>", name);
5921 /* Completely replace its old definition.
5922 The old enumerators remain defined, however. */
5923 TYPE_VALUES (enumtype) = 0;
5926 enum_next_value = integer_zero_node;
5929 if (flag_short_enums)
5930 TYPE_PACKED (enumtype) = 1;
5935 /* After processing and defining all the values of an enumeration type,
5936 install their decls in the enumeration type and finish it off.
5937 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5938 and ATTRIBUTES are the specified attributes.
5939 Returns ENUMTYPE. */
5942 finish_enum (tree enumtype, tree values, tree attributes)
5945 tree minnode = 0, maxnode = 0;
5946 int precision, unsign;
5947 bool toplevel = (file_scope == current_scope);
5948 struct lang_type *lt;
5950 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5952 /* Calculate the maximum value of any enumerator in this type. */
5954 if (values == error_mark_node)
5955 minnode = maxnode = integer_zero_node;
5958 minnode = maxnode = TREE_VALUE (values);
5959 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5961 tree value = TREE_VALUE (pair);
5962 if (tree_int_cst_lt (maxnode, value))
5964 if (tree_int_cst_lt (value, minnode))
5969 /* Construct the final type of this enumeration. It is the same
5970 as one of the integral types - the narrowest one that fits, except
5971 that normally we only go as narrow as int - and signed iff any of
5972 the values are negative. */
5973 unsign = (tree_int_cst_sgn (minnode) >= 0);
5974 precision = MAX (min_precision (minnode, unsign),
5975 min_precision (maxnode, unsign));
5977 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5979 tem = c_common_type_for_size (precision, unsign);
5982 warning (0, "enumeration values exceed range of largest integer");
5983 tem = long_long_integer_type_node;
5987 tem = unsign ? unsigned_type_node : integer_type_node;
5989 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5990 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5991 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5992 TYPE_SIZE (enumtype) = 0;
5994 /* If the precision of the type was specific with an attribute and it
5995 was too small, give an error. Otherwise, use it. */
5996 if (TYPE_PRECISION (enumtype))
5998 if (precision > TYPE_PRECISION (enumtype))
5999 error ("specified mode too small for enumeral values");
6002 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
6004 layout_type (enumtype);
6006 if (values != error_mark_node)
6008 /* Change the type of the enumerators to be the enum type. We
6009 need to do this irrespective of the size of the enum, for
6010 proper type checking. Replace the DECL_INITIALs of the
6011 enumerators, and the value slots of the list, with copies
6012 that have the enum type; they cannot be modified in place
6013 because they may be shared (e.g. integer_zero_node) Finally,
6014 change the purpose slots to point to the names of the decls. */
6015 for (pair = values; pair; pair = TREE_CHAIN (pair))
6017 tree enu = TREE_PURPOSE (pair);
6018 tree ini = DECL_INITIAL (enu);
6020 TREE_TYPE (enu) = enumtype;
6022 /* The ISO C Standard mandates enumerators to have type int,
6023 even though the underlying type of an enum type is
6024 unspecified. Here we convert any enumerators that fit in
6025 an int to type int, to avoid promotions to unsigned types
6026 when comparing integers with enumerators that fit in the
6027 int range. When -pedantic is given, build_enumerator()
6028 would have already taken care of those that don't fit. */
6029 if (int_fits_type_p (ini, integer_type_node))
6030 tem = integer_type_node;
6033 ini = convert (tem, ini);
6035 DECL_INITIAL (enu) = ini;
6036 TREE_PURPOSE (pair) = DECL_NAME (enu);
6037 TREE_VALUE (pair) = ini;
6040 TYPE_VALUES (enumtype) = values;
6043 /* Record the min/max values so that we can warn about bit-field
6044 enumerations that are too small for the values. */
6045 lt = GGC_CNEW (struct lang_type);
6046 lt->enum_min = minnode;
6047 lt->enum_max = maxnode;
6048 TYPE_LANG_SPECIFIC (enumtype) = lt;
6050 /* Fix up all variant types of this enum type. */
6051 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
6053 if (tem == enumtype)
6055 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
6056 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
6057 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
6058 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
6059 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
6060 TYPE_MODE (tem) = TYPE_MODE (enumtype);
6061 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
6062 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
6063 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
6064 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
6065 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
6068 /* Finish debugging output for this type. */
6069 rest_of_type_compilation (enumtype, toplevel);
6074 /* Build and install a CONST_DECL for one value of the
6075 current enumeration type (one that was begun with start_enum).
6076 Return a tree-list containing the CONST_DECL and its value.
6077 Assignment of sequential values by default is handled here. */
6080 build_enumerator (tree name, tree value)
6084 /* Validate and default VALUE. */
6088 /* Don't issue more errors for error_mark_node (i.e. an
6089 undeclared identifier) - just ignore the value expression. */
6090 if (value == error_mark_node)
6092 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
6093 || TREE_CODE (value) != INTEGER_CST)
6095 error ("enumerator value for %qE is not an integer constant", name);
6100 value = default_conversion (value);
6101 constant_expression_warning (value);
6105 /* Default based on previous value. */
6106 /* It should no longer be possible to have NON_LVALUE_EXPR
6110 value = enum_next_value;
6112 error ("overflow in enumeration values");
6115 if (pedantic && !int_fits_type_p (value, integer_type_node))
6117 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
6118 /* XXX This causes -pedantic to change the meaning of the program.
6119 Remove? -zw 2004-03-15 */
6120 value = convert (integer_type_node, value);
6123 /* Set basis for default for next value. */
6124 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
6125 enum_overflow = tree_int_cst_lt (enum_next_value, value);
6127 /* Now create a declaration for the enum value name. */
6129 type = TREE_TYPE (value);
6130 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6131 TYPE_PRECISION (integer_type_node)),
6132 (TYPE_PRECISION (type)
6133 >= TYPE_PRECISION (integer_type_node)
6134 && TYPE_UNSIGNED (type)));
6136 decl = build_decl (CONST_DECL, name, type);
6137 DECL_INITIAL (decl) = convert (type, value);
6140 return tree_cons (decl, value, NULL_TREE);
6144 /* Create the FUNCTION_DECL for a function definition.
6145 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6146 the declaration; they describe the function's name and the type it returns,
6147 but twisted together in a fashion that parallels the syntax of C.
6149 This function creates a binding context for the function body
6150 as well as setting up the FUNCTION_DECL in current_function_decl.
6152 Returns 1 on success. If the DECLARATOR is not suitable for a function
6153 (it defines a datum instead), we return 0, which tells
6154 yyparse to report a parse error. */
6157 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6160 tree decl1, old_decl;
6161 tree restype, resdecl;
6162 struct c_label_context_se *nstack_se;
6163 struct c_label_context_vm *nstack_vm;
6165 current_function_returns_value = 0; /* Assume, until we see it does. */
6166 current_function_returns_null = 0;
6167 current_function_returns_abnormally = 0;
6168 warn_about_return_type = 0;
6169 c_switch_stack = NULL;
6171 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6172 nstack_se->labels_def = NULL;
6173 nstack_se->labels_used = NULL;
6174 nstack_se->next = label_context_stack_se;
6175 label_context_stack_se = nstack_se;
6177 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6178 nstack_vm->labels_def = NULL;
6179 nstack_vm->labels_used = NULL;
6180 nstack_vm->scope = 0;
6181 nstack_vm->next = label_context_stack_vm;
6182 label_context_stack_vm = nstack_vm;
6184 /* Indicate no valid break/continue context by setting these variables
6185 to some non-null, non-label value. We'll notice and emit the proper
6186 error message in c_finish_bc_stmt. */
6187 c_break_label = c_cont_label = size_zero_node;
6189 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
6191 /* If the declarator is not suitable for a function definition,
6192 cause a syntax error. */
6195 label_context_stack_se = label_context_stack_se->next;
6196 label_context_stack_vm = label_context_stack_vm->next;
6200 decl_attributes (&decl1, attributes, 0);
6202 if (DECL_DECLARED_INLINE_P (decl1)
6203 && DECL_UNINLINABLE (decl1)
6204 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6205 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6208 /* Handle gnu_inline attribute. */
6209 if (declspecs->inline_p
6210 && !flag_gnu89_inline
6211 && TREE_CODE (decl1) == FUNCTION_DECL
6212 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1)))
6214 if (declspecs->storage_class != csc_static)
6215 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6218 announce_function (decl1);
6220 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6222 error ("return type is an incomplete type");
6223 /* Make it return void instead. */
6225 = build_function_type (void_type_node,
6226 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6229 if (warn_about_return_type)
6230 pedwarn_c99 ("return type defaults to %<int%>");
6232 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6233 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6234 DECL_INITIAL (decl1) = error_mark_node;
6236 /* If this definition isn't a prototype and we had a prototype declaration
6237 before, copy the arg type info from that prototype. */
6238 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6239 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6241 current_function_prototype_locus = UNKNOWN_LOCATION;
6242 current_function_prototype_built_in = false;
6243 current_function_prototype_arg_types = NULL_TREE;
6244 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6246 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6247 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6248 TREE_TYPE (TREE_TYPE (old_decl))))
6250 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6252 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6253 current_function_prototype_built_in
6254 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6255 current_function_prototype_arg_types
6256 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6258 if (TREE_PUBLIC (decl1))
6260 /* If there is an external prototype declaration of this
6261 function, record its location but do not copy information
6262 to this decl. This may be an invisible declaration
6263 (built-in or in a scope which has finished) or simply
6264 have more refined argument types than any declaration
6266 struct c_binding *b;
6267 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6268 if (B_IN_SCOPE (b, external_scope))
6272 tree ext_decl, ext_type;
6274 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6275 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6276 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6277 TREE_TYPE (ext_type)))
6279 current_function_prototype_locus
6280 = DECL_SOURCE_LOCATION (ext_decl);
6281 current_function_prototype_built_in
6282 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6283 current_function_prototype_arg_types
6284 = TYPE_ARG_TYPES (ext_type);
6290 /* Optionally warn of old-fashioned def with no previous prototype. */
6291 if (warn_strict_prototypes
6292 && old_decl != error_mark_node
6293 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6294 && C_DECL_ISNT_PROTOTYPE (old_decl))
6295 warning (OPT_Wstrict_prototypes,
6296 "function declaration isn%'t a prototype");
6297 /* Optionally warn of any global def with no previous prototype. */
6298 else if (warn_missing_prototypes
6299 && old_decl != error_mark_node
6300 && TREE_PUBLIC (decl1)
6301 && !MAIN_NAME_P (DECL_NAME (decl1))
6302 && C_DECL_ISNT_PROTOTYPE (old_decl))
6303 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6304 /* Optionally warn of any def with no previous prototype
6305 if the function has already been used. */
6306 else if (warn_missing_prototypes
6308 && old_decl != error_mark_node
6309 && TREE_USED (old_decl)
6310 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6311 warning (OPT_Wmissing_prototypes,
6312 "%q+D was used with no prototype before its definition", decl1);
6313 /* Optionally warn of any global def with no previous declaration. */
6314 else if (warn_missing_declarations
6315 && TREE_PUBLIC (decl1)
6317 && !MAIN_NAME_P (DECL_NAME (decl1)))
6318 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6320 /* Optionally warn of any def with no previous declaration
6321 if the function has already been used. */
6322 else if (warn_missing_declarations
6324 && old_decl != error_mark_node
6325 && TREE_USED (old_decl)
6326 && C_DECL_IMPLICIT (old_decl))
6327 warning (OPT_Wmissing_declarations,
6328 "%q+D was used with no declaration before its definition", decl1);
6330 /* This function exists in static storage.
6331 (This does not mean `static' in the C sense!) */
6332 TREE_STATIC (decl1) = 1;
6334 /* A nested function is not global. */
6335 if (current_function_decl != 0)
6336 TREE_PUBLIC (decl1) = 0;
6338 /* This is the earliest point at which we might know the assembler
6339 name of the function. Thus, if it's set before this, die horribly. */
6340 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6342 /* If #pragma weak was used, mark the decl weak now. */
6343 if (current_scope == file_scope)
6344 maybe_apply_pragma_weak (decl1);
6346 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6347 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6352 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6353 != integer_type_node)
6354 pedwarn ("return type of %q+D is not %<int%>", decl1);
6356 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
6357 args = TREE_CHAIN (args))
6359 tree type = args ? TREE_VALUE (args) : 0;
6361 if (type == void_type_node)
6368 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
6369 pedwarn ("first argument of %q+D should be %<int%>", decl1);
6373 if (TREE_CODE (type) != POINTER_TYPE
6374 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6375 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6377 pedwarn ("second argument of %q+D should be %<char **%>",
6382 if (TREE_CODE (type) != POINTER_TYPE
6383 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6384 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6386 pedwarn ("third argument of %q+D should probably be "
6387 "%<char **%>", decl1);
6392 /* It is intentional that this message does not mention the third
6393 argument because it's only mentioned in an appendix of the
6395 if (argct > 0 && (argct < 2 || argct > 3))
6396 pedwarn ("%q+D takes only zero or two arguments", decl1);
6398 if (!TREE_PUBLIC (decl1))
6399 pedwarn ("%q+D is normally a non-static function", decl1);
6402 /* Record the decl so that the function name is defined.
6403 If we already have a decl for this name, and it is a FUNCTION_DECL,
6404 use the old decl. */
6406 current_function_decl = pushdecl (decl1);
6409 declare_parm_level ();
6411 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6412 /* Promote the value to int before returning it. */
6413 if (c_promoting_integer_type_p (restype))
6415 /* It retains unsignedness if not really getting wider. */
6416 if (TYPE_UNSIGNED (restype)
6417 && (TYPE_PRECISION (restype)
6418 == TYPE_PRECISION (integer_type_node)))
6419 restype = unsigned_type_node;
6421 restype = integer_type_node;
6424 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6425 DECL_ARTIFICIAL (resdecl) = 1;
6426 DECL_IGNORED_P (resdecl) = 1;
6427 DECL_RESULT (current_function_decl) = resdecl;
6429 start_fname_decls ();
6434 /* Subroutine of store_parm_decls which handles new-style function
6435 definitions (prototype format). The parms already have decls, so we
6436 need only record them as in effect and complain if any redundant
6437 old-style parm decls were written. */
6439 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6443 if (current_scope->bindings)
6445 error ("%Jold-style parameter declarations in prototyped "
6446 "function definition", fndecl);
6448 /* Get rid of the old-style declarations. */
6452 /* Don't issue this warning for nested functions, and don't issue this
6453 warning if we got here because ARG_INFO_TYPES was error_mark_node
6454 (this happens when a function definition has just an ellipsis in
6455 its parameter list). */
6456 else if (!in_system_header && !current_function_scope
6457 && arg_info->types != error_mark_node)
6458 warning (OPT_Wtraditional,
6459 "%Jtraditional C rejects ISO C style function definitions",
6462 /* Now make all the parameter declarations visible in the function body.
6463 We can bypass most of the grunt work of pushdecl. */
6464 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6466 DECL_CONTEXT (decl) = current_function_decl;
6467 if (DECL_NAME (decl))
6469 bind (DECL_NAME (decl), decl, current_scope,
6470 /*invisible=*/false, /*nested=*/false);
6471 if (!TREE_USED (decl))
6472 warn_if_shadowing (decl);
6475 error ("%Jparameter name omitted", decl);
6478 /* Record the parameter list in the function declaration. */
6479 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6481 /* Now make all the ancillary declarations visible, likewise. */
6482 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6484 DECL_CONTEXT (decl) = current_function_decl;
6485 if (DECL_NAME (decl))
6486 bind (DECL_NAME (decl), decl, current_scope,
6487 /*invisible=*/false, /*nested=*/false);
6490 /* And all the tag declarations. */
6491 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6492 if (TREE_PURPOSE (decl))
6493 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6494 /*invisible=*/false, /*nested=*/false);
6497 /* Subroutine of store_parm_decls which handles old-style function
6498 definitions (separate parameter list and declarations). */
6501 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6503 struct c_binding *b;
6504 tree parm, decl, last;
6505 tree parmids = arg_info->parms;
6506 struct pointer_set_t *seen_args = pointer_set_create ();
6508 if (!in_system_header)
6509 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6512 /* Match each formal parameter name with its declaration. Save each
6513 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6514 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6516 if (TREE_VALUE (parm) == 0)
6518 error ("%Jparameter name missing from parameter list", fndecl);
6519 TREE_PURPOSE (parm) = 0;
6523 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6524 if (b && B_IN_CURRENT_SCOPE (b))
6527 /* If we got something other than a PARM_DECL it is an error. */
6528 if (TREE_CODE (decl) != PARM_DECL)
6529 error ("%q+D declared as a non-parameter", decl);
6530 /* If the declaration is already marked, we have a duplicate
6531 name. Complain and ignore the duplicate. */
6532 else if (pointer_set_contains (seen_args, decl))
6534 error ("multiple parameters named %q+D", decl);
6535 TREE_PURPOSE (parm) = 0;
6538 /* If the declaration says "void", complain and turn it into
6540 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6542 error ("parameter %q+D declared with void type", decl);
6543 TREE_TYPE (decl) = integer_type_node;
6544 DECL_ARG_TYPE (decl) = integer_type_node;
6545 layout_decl (decl, 0);
6547 warn_if_shadowing (decl);
6549 /* If no declaration found, default to int. */
6552 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6553 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6554 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6556 warn_if_shadowing (decl);
6559 pedwarn ("type of %q+D defaults to %<int%>", decl);
6560 else if (extra_warnings)
6561 warning (OPT_Wextra, "type of %q+D defaults to %<int%>", decl);
6564 TREE_PURPOSE (parm) = decl;
6565 pointer_set_insert (seen_args, decl);
6568 /* Now examine the parms chain for incomplete declarations
6569 and declarations with no corresponding names. */
6571 for (b = current_scope->bindings; b; b = b->prev)
6574 if (TREE_CODE (parm) != PARM_DECL)
6577 if (TREE_TYPE (parm) != error_mark_node
6578 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6580 error ("parameter %q+D has incomplete type", parm);
6581 TREE_TYPE (parm) = error_mark_node;
6584 if (!pointer_set_contains (seen_args, parm))
6586 error ("declaration for parameter %q+D but no such parameter", parm);
6588 /* Pretend the parameter was not missing.
6589 This gets us to a standard state and minimizes
6590 further error messages. */
6591 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6595 /* Chain the declarations together in the order of the list of
6596 names. Store that chain in the function decl, replacing the
6597 list of names. Update the current scope to match. */
6598 DECL_ARGUMENTS (fndecl) = 0;
6600 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6601 if (TREE_PURPOSE (parm))
6603 if (parm && TREE_PURPOSE (parm))
6605 last = TREE_PURPOSE (parm);
6606 DECL_ARGUMENTS (fndecl) = last;
6608 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6609 if (TREE_PURPOSE (parm))
6611 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6612 last = TREE_PURPOSE (parm);
6614 TREE_CHAIN (last) = 0;
6617 pointer_set_destroy (seen_args);
6619 /* If there was a previous prototype,
6620 set the DECL_ARG_TYPE of each argument according to
6621 the type previously specified, and report any mismatches. */
6623 if (current_function_prototype_arg_types)
6626 for (parm = DECL_ARGUMENTS (fndecl),
6627 type = current_function_prototype_arg_types;
6628 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6629 != void_type_node));
6630 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6632 if (parm == 0 || type == 0
6633 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6635 if (current_function_prototype_built_in)
6636 warning (0, "number of arguments doesn%'t match "
6637 "built-in prototype");
6640 error ("number of arguments doesn%'t match prototype");
6641 error ("%Hprototype declaration",
6642 ¤t_function_prototype_locus);
6646 /* Type for passing arg must be consistent with that
6647 declared for the arg. ISO C says we take the unqualified
6648 type for parameters declared with qualified type. */
6649 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6650 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6652 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6653 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6655 /* Adjust argument to match prototype. E.g. a previous
6656 `int foo(float);' prototype causes
6657 `int foo(x) float x; {...}' to be treated like
6658 `int foo(float x) {...}'. This is particularly
6659 useful for argument types like uid_t. */
6660 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6662 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6663 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6664 && TYPE_PRECISION (TREE_TYPE (parm))
6665 < TYPE_PRECISION (integer_type_node))
6666 DECL_ARG_TYPE (parm) = integer_type_node;
6670 /* ??? Is it possible to get here with a
6671 built-in prototype or will it always have
6672 been diagnosed as conflicting with an
6673 old-style definition and discarded? */
6674 if (current_function_prototype_built_in)
6675 warning (0, "promoted argument %qD "
6676 "doesn%'t match built-in prototype", parm);
6679 pedwarn ("promoted argument %qD "
6680 "doesn%'t match prototype", parm);
6681 pedwarn ("%Hprototype declaration",
6682 ¤t_function_prototype_locus);
6688 if (current_function_prototype_built_in)
6689 warning (0, "argument %qD doesn%'t match "
6690 "built-in prototype", parm);
6693 error ("argument %qD doesn%'t match prototype", parm);
6694 error ("%Hprototype declaration",
6695 ¤t_function_prototype_locus);
6700 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6703 /* Otherwise, create a prototype that would match. */
6707 tree actual = 0, last = 0, type;
6709 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6711 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6713 TREE_CHAIN (last) = type;
6718 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6720 TREE_CHAIN (last) = type;
6724 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6725 of the type of this function, but we need to avoid having this
6726 affect the types of other similarly-typed functions, so we must
6727 first force the generation of an identical (but separate) type
6728 node for the relevant function type. The new node we create
6729 will be a variant of the main variant of the original function
6732 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6734 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6738 /* Store parameter declarations passed in ARG_INFO into the current
6739 function declaration. */
6742 store_parm_decls_from (struct c_arg_info *arg_info)
6744 current_function_arg_info = arg_info;
6745 store_parm_decls ();
6748 /* Store the parameter declarations into the current function declaration.
6749 This is called after parsing the parameter declarations, before
6750 digesting the body of the function.
6752 For an old-style definition, construct a prototype out of the old-style
6753 parameter declarations and inject it into the function's type. */
6756 store_parm_decls (void)
6758 tree fndecl = current_function_decl;
6761 /* The argument information block for FNDECL. */
6762 struct c_arg_info *arg_info = current_function_arg_info;
6763 current_function_arg_info = 0;
6765 /* True if this definition is written with a prototype. Note:
6766 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6767 list in a function definition as equivalent to (void) -- an
6768 empty argument list specifies the function has no parameters,
6769 but only (void) sets up a prototype for future calls. */
6770 proto = arg_info->types != 0;
6773 store_parm_decls_newstyle (fndecl, arg_info);
6775 store_parm_decls_oldstyle (fndecl, arg_info);
6777 /* The next call to push_scope will be a function body. */
6779 next_is_function_body = true;
6781 /* Write a record describing this function definition to the prototypes
6782 file (if requested). */
6784 gen_aux_info_record (fndecl, 1, 0, proto);
6786 /* Initialize the RTL code for the function. */
6787 allocate_struct_function (fndecl);
6789 /* Begin the statement tree for this function. */
6790 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6792 /* ??? Insert the contents of the pending sizes list into the function
6793 to be evaluated. The only reason left to have this is
6794 void foo(int n, int array[n++])
6795 because we throw away the array type in favor of a pointer type, and
6796 thus won't naturally see the SAVE_EXPR containing the increment. All
6797 other pending sizes would be handled by gimplify_parameters. */
6800 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6801 add_stmt (TREE_VALUE (t));
6804 /* Even though we're inside a function body, we still don't want to
6805 call expand_expr to calculate the size of a variable-sized array.
6806 We haven't necessarily assigned RTL to all variables yet, so it's
6807 not safe to try to expand expressions involving them. */
6808 cfun->x_dont_save_pending_sizes_p = 1;
6811 /* Emit diagnostics that require gimple input for detection. Operate on
6812 FNDECL and all its nested functions. */
6815 c_gimple_diagnostics_recursively (tree fndecl)
6817 struct cgraph_node *cgn;
6819 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6820 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6822 /* Notice when OpenMP structured block constraints are violated. */
6824 diagnose_omp_structured_block_errors (fndecl);
6826 /* Finalize all nested functions now. */
6827 cgn = cgraph_node (fndecl);
6828 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6829 c_gimple_diagnostics_recursively (cgn->decl);
6832 /* Finish up a function declaration and compile that function
6833 all the way to assembler language output. The free the storage
6834 for the function definition.
6836 This is called after parsing the body of the function definition. */
6839 finish_function (void)
6841 tree fndecl = current_function_decl;
6843 label_context_stack_se = label_context_stack_se->next;
6844 label_context_stack_vm = label_context_stack_vm->next;
6846 if (TREE_CODE (fndecl) == FUNCTION_DECL
6847 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6849 tree args = DECL_ARGUMENTS (fndecl);
6850 for (; args; args = TREE_CHAIN (args))
6852 tree type = TREE_TYPE (args);
6853 if (INTEGRAL_TYPE_P (type)
6854 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6855 DECL_ARG_TYPE (args) = integer_type_node;
6859 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6860 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6862 /* Must mark the RESULT_DECL as being in this function. */
6864 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6865 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6867 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6869 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6870 != integer_type_node)
6872 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6873 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6875 pedwarn ("return type of %q+D is not %<int%>", fndecl);
6881 tree stmt = c_finish_return (integer_zero_node);
6882 #ifdef USE_MAPPED_LOCATION
6883 /* Hack. We don't want the middle-end to warn that this return
6884 is unreachable, so we mark its location as special. Using
6885 UNKNOWN_LOCATION has the problem that it gets clobbered in
6886 annotate_one_with_locus. A cleaner solution might be to
6887 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6889 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6891 /* Hack. We don't want the middle-end to warn that this
6892 return is unreachable, so put the statement on the
6894 annotate_with_file_line (stmt, input_filename, 0);
6900 /* Tie off the statement tree for this function. */
6901 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6903 finish_fname_decls ();
6905 /* Complain if there's just no return statement. */
6906 if (warn_return_type
6907 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6908 && !current_function_returns_value && !current_function_returns_null
6909 /* Don't complain if we are no-return. */
6910 && !current_function_returns_abnormally
6911 /* Don't warn for main(). */
6912 && !MAIN_NAME_P (DECL_NAME (fndecl))
6913 /* Or if they didn't actually specify a return type. */
6914 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6915 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6916 inline function, as we might never be compiled separately. */
6917 && DECL_INLINE (fndecl))
6919 warning (OPT_Wreturn_type,
6920 "no return statement in function returning non-void");
6921 TREE_NO_WARNING (fndecl) = 1;
6924 /* With just -Wextra, complain only if function returns both with
6925 and without a value. */
6927 && current_function_returns_value
6928 && current_function_returns_null)
6929 warning (OPT_Wextra, "this function may return with or without a value");
6931 /* Store the end of the function, so that we get good line number
6932 info for the epilogue. */
6933 cfun->function_end_locus = input_location;
6935 /* If we don't have ctors/dtors sections, and this is a static
6936 constructor or destructor, it must be recorded now. */
6937 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6938 && !targetm.have_ctors_dtors)
6939 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6940 if (DECL_STATIC_DESTRUCTOR (fndecl)
6941 && !targetm.have_ctors_dtors)
6942 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6944 /* Finalize the ELF visibility for the function. */
6945 c_determine_visibility (fndecl);
6947 /* Genericize before inlining. Delay genericizing nested functions
6948 until their parent function is genericized. Since finalizing
6949 requires GENERIC, delay that as well. */
6951 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6952 && !undef_nested_function)
6954 if (!decl_function_context (fndecl))
6956 c_genericize (fndecl);
6957 c_gimple_diagnostics_recursively (fndecl);
6959 /* ??? Objc emits functions after finalizing the compilation unit.
6960 This should be cleaned up later and this conditional removed. */
6961 if (cgraph_global_info_ready)
6963 c_expand_body (fndecl);
6967 cgraph_finalize_function (fndecl, false);
6971 /* Register this function with cgraph just far enough to get it
6972 added to our parent's nested function list. Handy, since the
6973 C front end doesn't have such a list. */
6974 (void) cgraph_node (fndecl);
6978 if (!decl_function_context (fndecl))
6979 undef_nested_function = false;
6981 /* We're leaving the context of this function, so zap cfun.
6982 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6983 tree_rest_of_compilation. */
6985 current_function_decl = NULL;
6988 /* Generate the RTL for the body of FNDECL. */
6991 c_expand_body (tree fndecl)
6994 if (!DECL_INITIAL (fndecl)
6995 || DECL_INITIAL (fndecl) == error_mark_node)
6998 tree_rest_of_compilation (fndecl);
7000 if (DECL_STATIC_CONSTRUCTOR (fndecl)
7001 && targetm.have_ctors_dtors)
7002 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
7003 DEFAULT_INIT_PRIORITY);
7004 if (DECL_STATIC_DESTRUCTOR (fndecl)
7005 && targetm.have_ctors_dtors)
7006 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
7007 DEFAULT_INIT_PRIORITY);
7010 /* Check the declarations given in a for-loop for satisfying the C99
7011 constraints. If exactly one such decl is found, return it. */
7014 check_for_loop_decls (void)
7016 struct c_binding *b;
7017 tree one_decl = NULL_TREE;
7023 /* If we get here, declarations have been used in a for loop without
7024 the C99 for loop scope. This doesn't make much sense, so don't
7026 error ("%<for%> loop initial declaration used outside C99 mode");
7029 /* C99 subclause 6.8.5 paragraph 3:
7031 [#3] The declaration part of a for statement shall only
7032 declare identifiers for objects having storage class auto or
7035 It isn't clear whether, in this sentence, "identifiers" binds to
7036 "shall only declare" or to "objects" - that is, whether all identifiers
7037 declared must be identifiers for objects, or whether the restriction
7038 only applies to those that are. (A question on this in comp.std.c
7039 in November 2000 received no answer.) We implement the strictest
7040 interpretation, to avoid creating an extension which later causes
7043 for (b = current_scope->bindings; b; b = b->prev)
7046 tree decl = b->decl;
7051 switch (TREE_CODE (decl))
7054 if (TREE_STATIC (decl))
7055 error ("declaration of static variable %q+D in %<for%> loop "
7056 "initial declaration", decl);
7057 else if (DECL_EXTERNAL (decl))
7058 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
7059 "initial declaration", decl);
7063 error ("%<struct %E%> declared in %<for%> loop initial declaration",
7067 error ("%<union %E%> declared in %<for%> loop initial declaration",
7071 error ("%<enum %E%> declared in %<for%> loop initial declaration",
7075 error ("declaration of non-variable %q+D in %<for%> loop "
7076 "initial declaration", decl);
7083 return n_decls == 1 ? one_decl : NULL_TREE;
7086 /* Save and reinitialize the variables
7087 used during compilation of a C function. */
7090 c_push_function_context (struct function *f)
7092 struct language_function *p;
7093 p = GGC_NEW (struct language_function);
7096 p->base.x_stmt_tree = c_stmt_tree;
7097 p->x_break_label = c_break_label;
7098 p->x_cont_label = c_cont_label;
7099 p->x_switch_stack = c_switch_stack;
7100 p->arg_info = current_function_arg_info;
7101 p->returns_value = current_function_returns_value;
7102 p->returns_null = current_function_returns_null;
7103 p->returns_abnormally = current_function_returns_abnormally;
7104 p->warn_about_return_type = warn_about_return_type;
7107 /* Restore the variables used during compilation of a C function. */
7110 c_pop_function_context (struct function *f)
7112 struct language_function *p = f->language;
7114 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
7115 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
7117 /* Stop pointing to the local nodes about to be freed. */
7118 /* But DECL_INITIAL must remain nonzero so we know this
7119 was an actual function definition. */
7120 DECL_INITIAL (current_function_decl) = error_mark_node;
7121 DECL_ARGUMENTS (current_function_decl) = 0;
7124 c_stmt_tree = p->base.x_stmt_tree;
7125 c_break_label = p->x_break_label;
7126 c_cont_label = p->x_cont_label;
7127 c_switch_stack = p->x_switch_stack;
7128 current_function_arg_info = p->arg_info;
7129 current_function_returns_value = p->returns_value;
7130 current_function_returns_null = p->returns_null;
7131 current_function_returns_abnormally = p->returns_abnormally;
7132 warn_about_return_type = p->warn_about_return_type;
7137 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
7140 c_dup_lang_specific_decl (tree decl)
7142 struct lang_decl *ld;
7144 if (!DECL_LANG_SPECIFIC (decl))
7147 ld = GGC_NEW (struct lang_decl);
7148 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
7149 DECL_LANG_SPECIFIC (decl) = ld;
7152 /* The functions below are required for functionality of doing
7153 function at once processing in the C front end. Currently these
7154 functions are not called from anywhere in the C front end, but as
7155 these changes continue, that will change. */
7157 /* Returns the stmt_tree (if any) to which statements are currently
7158 being added. If there is no active statement-tree, NULL is
7162 current_stmt_tree (void)
7164 return &c_stmt_tree;
7167 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
7171 anon_aggr_type_p (tree ARG_UNUSED (node))
7176 /* Return the global value of T as a symbol. */
7179 identifier_global_value (tree t)
7181 struct c_binding *b;
7183 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
7184 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
7190 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
7191 otherwise the name is found in ridpointers from RID_INDEX. */
7194 record_builtin_type (enum rid rid_index, const char *name, tree type)
7198 id = ridpointers[(int) rid_index];
7200 id = get_identifier (name);
7201 decl = build_decl (TYPE_DECL, id, type);
7203 if (debug_hooks->type_decl)
7204 debug_hooks->type_decl (decl, false);
7207 /* Build the void_list_node (void_type_node having been created). */
7209 build_void_list_node (void)
7211 tree t = build_tree_list (NULL_TREE, void_type_node);
7215 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7218 build_c_parm (struct c_declspecs *specs, tree attrs,
7219 struct c_declarator *declarator)
7221 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7224 ret->declarator = declarator;
7228 /* Return a declarator with nested attributes. TARGET is the inner
7229 declarator to which these attributes apply. ATTRS are the
7232 struct c_declarator *
7233 build_attrs_declarator (tree attrs, struct c_declarator *target)
7235 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7236 ret->kind = cdk_attrs;
7237 ret->declarator = target;
7238 ret->u.attrs = attrs;
7242 /* Return a declarator for a function with arguments specified by ARGS
7243 and return type specified by TARGET. */
7245 struct c_declarator *
7246 build_function_declarator (struct c_arg_info *args,
7247 struct c_declarator *target)
7249 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7250 ret->kind = cdk_function;
7251 ret->declarator = target;
7252 ret->u.arg_info = args;
7256 /* Return a declarator for the identifier IDENT (which may be
7257 NULL_TREE for an abstract declarator). */
7259 struct c_declarator *
7260 build_id_declarator (tree ident)
7262 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7264 ret->declarator = 0;
7266 /* Default value - may get reset to a more precise location. */
7267 ret->id_loc = input_location;
7271 /* Return something to represent absolute declarators containing a *.
7272 TARGET is the absolute declarator that the * contains.
7273 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7274 to apply to the pointer type. */
7276 struct c_declarator *
7277 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7278 struct c_declarator *target)
7282 struct c_declarator *itarget = target;
7283 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7284 if (type_quals_attrs)
7286 attrs = type_quals_attrs->attrs;
7287 quals = quals_from_declspecs (type_quals_attrs);
7288 if (attrs != NULL_TREE)
7289 itarget = build_attrs_declarator (attrs, target);
7291 ret->kind = cdk_pointer;
7292 ret->declarator = itarget;
7293 ret->u.pointer_quals = quals;
7297 /* Return a pointer to a structure for an empty list of declaration
7300 struct c_declspecs *
7301 build_null_declspecs (void)
7303 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7307 ret->typespec_word = cts_none;
7308 ret->storage_class = csc_none;
7309 ret->declspecs_seen_p = false;
7310 ret->type_seen_p = false;
7311 ret->non_sc_seen_p = false;
7312 ret->typedef_p = false;
7313 ret->tag_defined_p = false;
7314 ret->explicit_signed_p = false;
7315 ret->deprecated_p = false;
7316 /* APPLE LOCAL "unavailable" attribute (radar 2809697) */
7317 ret->unavailable_p = false;
7318 ret->default_int_p = false;
7319 ret->long_p = false;
7320 ret->long_long_p = false;
7321 ret->short_p = false;
7322 ret->signed_p = false;
7323 ret->unsigned_p = false;
7324 ret->complex_p = false;
7325 ret->inline_p = false;
7326 ret->thread_p = false;
7327 ret->const_p = false;
7328 ret->volatile_p = false;
7329 ret->restrict_p = false;
7333 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7336 struct c_declspecs *
7337 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7341 specs->non_sc_seen_p = true;
7342 specs->declspecs_seen_p = true;
7343 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7344 && C_IS_RESERVED_WORD (qual));
7345 i = C_RID_CODE (qual);
7349 dupe = specs->const_p;
7350 specs->const_p = true;
7353 dupe = specs->volatile_p;
7354 specs->volatile_p = true;
7357 dupe = specs->restrict_p;
7358 specs->restrict_p = true;
7363 if (dupe && pedantic && !flag_isoc99)
7364 pedwarn ("duplicate %qE", qual);
7368 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7371 struct c_declspecs *
7372 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7374 tree type = spec.spec;
7375 specs->non_sc_seen_p = true;
7376 specs->declspecs_seen_p = true;
7377 specs->type_seen_p = true;
7378 if (TREE_DEPRECATED (type))
7379 specs->deprecated_p = true;
7381 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
7382 if (TREE_UNAVAILABLE (type))
7383 specs->unavailable_p = true;
7384 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
7386 /* Handle type specifier keywords. */
7387 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
7389 enum rid i = C_RID_CODE (type);
7392 error ("two or more data types in declaration specifiers");
7395 if ((int) i <= (int) RID_LAST_MODIFIER)
7397 /* "long", "short", "signed", "unsigned" or "_Complex". */
7402 if (specs->long_long_p)
7404 error ("%<long long long%> is too long for GCC");
7409 if (specs->typespec_word == cts_double)
7411 error ("both %<long long%> and %<double%> in "
7412 "declaration specifiers");
7415 if (pedantic && !flag_isoc99 && !in_system_header
7417 pedwarn ("ISO C90 does not support %<long long%>");
7418 specs->long_long_p = 1;
7422 error ("both %<long%> and %<short%> in "
7423 "declaration specifiers");
7424 else if (specs->typespec_word == cts_void)
7425 error ("both %<long%> and %<void%> in "
7426 "declaration specifiers");
7427 else if (specs->typespec_word == cts_bool)
7428 error ("both %<long%> and %<_Bool%> in "
7429 "declaration specifiers");
7430 else if (specs->typespec_word == cts_char)
7431 error ("both %<long%> and %<char%> in "
7432 "declaration specifiers");
7433 else if (specs->typespec_word == cts_float)
7434 error ("both %<long%> and %<float%> in "
7435 "declaration specifiers");
7436 else if (specs->typespec_word == cts_dfloat32)
7437 error ("both %<long%> and %<_Decimal32%> in "
7438 "declaration specifiers");
7439 else if (specs->typespec_word == cts_dfloat64)
7440 error ("both %<long%> and %<_Decimal64%> in "
7441 "declaration specifiers");
7442 else if (specs->typespec_word == cts_dfloat128)
7443 error ("both %<long%> and %<_Decimal128%> in "
7444 "declaration specifiers");
7446 specs->long_p = true;
7449 dupe = specs->short_p;
7451 error ("both %<long%> and %<short%> in "
7452 "declaration specifiers");
7453 else if (specs->typespec_word == cts_void)
7454 error ("both %<short%> and %<void%> in "
7455 "declaration specifiers");
7456 else if (specs->typespec_word == cts_bool)
7457 error ("both %<short%> and %<_Bool%> in "
7458 "declaration specifiers");
7459 else if (specs->typespec_word == cts_char)
7460 error ("both %<short%> and %<char%> in "
7461 "declaration specifiers");
7462 else if (specs->typespec_word == cts_float)
7463 error ("both %<short%> and %<float%> in "
7464 "declaration specifiers");
7465 else if (specs->typespec_word == cts_double)
7466 error ("both %<short%> and %<double%> in "
7467 "declaration specifiers");
7468 else if (specs->typespec_word == cts_dfloat32)
7469 error ("both %<short%> and %<_Decimal32%> in "
7470 "declaration specifiers");
7471 else if (specs->typespec_word == cts_dfloat64)
7472 error ("both %<short%> and %<_Decimal64%> in "
7473 "declaration specifiers");
7474 else if (specs->typespec_word == cts_dfloat128)
7475 error ("both %<short%> and %<_Decimal128%> in "
7476 "declaration specifiers");
7478 specs->short_p = true;
7481 dupe = specs->signed_p;
7482 if (specs->unsigned_p)
7483 error ("both %<signed%> and %<unsigned%> in "
7484 "declaration specifiers");
7485 else if (specs->typespec_word == cts_void)
7486 error ("both %<signed%> and %<void%> in "
7487 "declaration specifiers");
7488 else if (specs->typespec_word == cts_bool)
7489 error ("both %<signed%> and %<_Bool%> in "
7490 "declaration specifiers");
7491 else if (specs->typespec_word == cts_float)
7492 error ("both %<signed%> and %<float%> in "
7493 "declaration specifiers");
7494 else if (specs->typespec_word == cts_double)
7495 error ("both %<signed%> and %<double%> in "
7496 "declaration specifiers");
7497 else if (specs->typespec_word == cts_dfloat32)
7498 error ("both %<signed%> and %<_Decimal32%> in "
7499 "declaration specifiers");
7500 else if (specs->typespec_word == cts_dfloat64)
7501 error ("both %<signed%> and %<_Decimal64%> in "
7502 "declaration specifiers");
7503 else if (specs->typespec_word == cts_dfloat128)
7504 error ("both %<signed%> and %<_Decimal128%> in "
7505 "declaration specifiers");
7507 specs->signed_p = true;
7510 dupe = specs->unsigned_p;
7511 if (specs->signed_p)
7512 error ("both %<signed%> and %<unsigned%> in "
7513 "declaration specifiers");
7514 else if (specs->typespec_word == cts_void)
7515 error ("both %<unsigned%> and %<void%> in "
7516 "declaration specifiers");
7517 else if (specs->typespec_word == cts_bool)
7518 error ("both %<unsigned%> and %<_Bool%> in "
7519 "declaration specifiers");
7520 else if (specs->typespec_word == cts_float)
7521 error ("both %<unsigned%> and %<float%> in "
7522 "declaration specifiers");
7523 else if (specs->typespec_word == cts_double)
7524 error ("both %<unsigned%> and %<double%> in "
7525 "declaration specifiers");
7526 else if (specs->typespec_word == cts_dfloat32)
7527 error ("both %<unsigned%> and %<_Decimal32%> in "
7528 "declaration specifiers");
7529 else if (specs->typespec_word == cts_dfloat64)
7530 error ("both %<unsigned%> and %<_Decimal64%> in "
7531 "declaration specifiers");
7532 else if (specs->typespec_word == cts_dfloat128)
7533 error ("both %<unsigned%> and %<_Decimal128%> in "
7534 "declaration specifiers");
7536 specs->unsigned_p = true;
7539 dupe = specs->complex_p;
7540 if (pedantic && !flag_isoc99 && !in_system_header)
7541 pedwarn ("ISO C90 does not support complex types");
7542 if (specs->typespec_word == cts_void)
7543 error ("both %<complex%> and %<void%> in "
7544 "declaration specifiers");
7545 else if (specs->typespec_word == cts_bool)
7546 error ("both %<complex%> and %<_Bool%> in "
7547 "declaration specifiers");
7548 else if (specs->typespec_word == cts_dfloat32)
7549 error ("both %<complex%> and %<_Decimal32%> in "
7550 "declaration specifiers");
7551 else if (specs->typespec_word == cts_dfloat64)
7552 error ("both %<complex%> and %<_Decimal64%> in "
7553 "declaration specifiers");
7554 else if (specs->typespec_word == cts_dfloat128)
7555 error ("both %<complex%> and %<_Decimal128%> in "
7556 "declaration specifiers");
7558 specs->complex_p = true;
7565 error ("duplicate %qE", type);
7571 /* "void", "_Bool", "char", "int", "float" or "double". */
7572 if (specs->typespec_word != cts_none)
7574 error ("two or more data types in declaration specifiers");
7581 error ("both %<long%> and %<void%> in "
7582 "declaration specifiers");
7583 else if (specs->short_p)
7584 error ("both %<short%> and %<void%> in "
7585 "declaration specifiers");
7586 else if (specs->signed_p)
7587 error ("both %<signed%> and %<void%> in "
7588 "declaration specifiers");
7589 else if (specs->unsigned_p)
7590 error ("both %<unsigned%> and %<void%> in "
7591 "declaration specifiers");
7592 else if (specs->complex_p)
7593 error ("both %<complex%> and %<void%> in "
7594 "declaration specifiers");
7596 specs->typespec_word = cts_void;
7600 error ("both %<long%> and %<_Bool%> in "
7601 "declaration specifiers");
7602 else if (specs->short_p)
7603 error ("both %<short%> and %<_Bool%> in "
7604 "declaration specifiers");
7605 else if (specs->signed_p)
7606 error ("both %<signed%> and %<_Bool%> in "
7607 "declaration specifiers");
7608 else if (specs->unsigned_p)
7609 error ("both %<unsigned%> and %<_Bool%> in "
7610 "declaration specifiers");
7611 else if (specs->complex_p)
7612 error ("both %<complex%> and %<_Bool%> in "
7613 "declaration specifiers");
7615 specs->typespec_word = cts_bool;
7619 error ("both %<long%> and %<char%> in "
7620 "declaration specifiers");
7621 else if (specs->short_p)
7622 error ("both %<short%> and %<char%> in "
7623 "declaration specifiers");
7625 specs->typespec_word = cts_char;
7628 specs->typespec_word = cts_int;
7632 error ("both %<long%> and %<float%> in "
7633 "declaration specifiers");
7634 else if (specs->short_p)
7635 error ("both %<short%> and %<float%> in "
7636 "declaration specifiers");
7637 else if (specs->signed_p)
7638 error ("both %<signed%> and %<float%> in "
7639 "declaration specifiers");
7640 else if (specs->unsigned_p)
7641 error ("both %<unsigned%> and %<float%> in "
7642 "declaration specifiers");
7644 specs->typespec_word = cts_float;
7647 if (specs->long_long_p)
7648 error ("both %<long long%> and %<double%> in "
7649 "declaration specifiers");
7650 else if (specs->short_p)
7651 error ("both %<short%> and %<double%> in "
7652 "declaration specifiers");
7653 else if (specs->signed_p)
7654 error ("both %<signed%> and %<double%> in "
7655 "declaration specifiers");
7656 else if (specs->unsigned_p)
7657 error ("both %<unsigned%> and %<double%> in "
7658 "declaration specifiers");
7660 specs->typespec_word = cts_double;
7667 if (i == RID_DFLOAT32)
7669 else if (i == RID_DFLOAT64)
7672 str = "_Decimal128";
7673 if (specs->long_long_p)
7674 error ("both %<long long%> and %<%s%> in "
7675 "declaration specifiers", str);
7677 error ("both %<long%> and %<%s%> in "
7678 "declaration specifiers", str);
7679 else if (specs->short_p)
7680 error ("both %<short%> and %<%s%> in "
7681 "declaration specifiers", str);
7682 else if (specs->signed_p)
7683 error ("both %<signed%> and %<%s%> in "
7684 "declaration specifiers", str);
7685 else if (specs->unsigned_p)
7686 error ("both %<unsigned%> and %<%s%> in "
7687 "declaration specifiers", str);
7688 else if (specs->complex_p)
7689 error ("both %<complex%> and %<%s%> in "
7690 "declaration specifiers", str);
7691 else if (i == RID_DFLOAT32)
7692 specs->typespec_word = cts_dfloat32;
7693 else if (i == RID_DFLOAT64)
7694 specs->typespec_word = cts_dfloat64;
7696 specs->typespec_word = cts_dfloat128;
7698 if (!targetm.decimal_float_supported_p ())
7699 error ("decimal floating point not supported for this target");
7701 pedwarn ("ISO C does not support decimal floating point");
7704 /* ObjC reserved word "id", handled below. */
7710 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7711 form of ObjC type, cases such as "int" and "long" being handled
7712 above), a TYPE (struct, union, enum and typeof specifiers) or an
7713 ERROR_MARK. In none of these cases may there have previously
7714 been any type specifiers. */
7715 if (specs->type || specs->typespec_word != cts_none
7716 || specs->long_p || specs->short_p || specs->signed_p
7717 || specs->unsigned_p || specs->complex_p)
7718 error ("two or more data types in declaration specifiers");
7719 else if (TREE_CODE (type) == TYPE_DECL)
7721 if (TREE_TYPE (type) == error_mark_node)
7722 ; /* Allow the type to default to int to avoid cascading errors. */
7725 specs->type = TREE_TYPE (type);
7726 specs->decl_attr = DECL_ATTRIBUTES (type);
7727 specs->typedef_p = true;
7728 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7731 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7733 tree t = lookup_name (type);
7734 if (!t || TREE_CODE (t) != TYPE_DECL)
7735 error ("%qE fails to be a typedef or built in type", type);
7736 else if (TREE_TYPE (t) == error_mark_node)
7739 specs->type = TREE_TYPE (t);
7741 else if (TREE_CODE (type) != ERROR_MARK)
7743 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7744 specs->tag_defined_p = true;
7745 if (spec.kind == ctsk_typeof)
7746 specs->typedef_p = true;
7753 /* Add the storage class specifier or function specifier SCSPEC to the
7754 declaration specifiers SPECS, returning SPECS. */
7756 struct c_declspecs *
7757 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7760 enum c_storage_class n = csc_none;
7762 specs->declspecs_seen_p = true;
7763 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7764 && C_IS_RESERVED_WORD (scspec));
7765 i = C_RID_CODE (scspec);
7766 if (extra_warnings && specs->non_sc_seen_p)
7767 warning (OPT_Wextra, "%qE is not at beginning of declaration", scspec);
7771 /* C99 permits duplicate inline. Although of doubtful utility,
7772 it seems simplest to permit it in gnu89 mode as well, as
7773 there is also little utility in maintaining this as a
7774 difference between gnu89 and C99 inline. */
7776 specs->inline_p = true;
7779 dupe = specs->thread_p;
7780 if (specs->storage_class == csc_auto)
7781 error ("%<__thread%> used with %<auto%>");
7782 else if (specs->storage_class == csc_register)
7783 error ("%<__thread%> used with %<register%>");
7784 else if (specs->storage_class == csc_typedef)
7785 error ("%<__thread%> used with %<typedef%>");
7787 specs->thread_p = true;
7794 /* Diagnose "__thread extern". */
7795 if (specs->thread_p)
7796 error ("%<__thread%> before %<extern%>");
7803 /* Diagnose "__thread static". */
7804 if (specs->thread_p)
7805 error ("%<__thread%> before %<static%>");
7813 if (n != csc_none && n == specs->storage_class)
7816 error ("duplicate %qE", scspec);
7819 if (specs->storage_class != csc_none && n != specs->storage_class)
7821 error ("multiple storage classes in declaration specifiers");
7825 specs->storage_class = n;
7826 if (n != csc_extern && n != csc_static && specs->thread_p)
7828 error ("%<__thread%> used with %qE", scspec);
7829 specs->thread_p = false;
7836 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7839 struct c_declspecs *
7840 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7842 specs->attrs = chainon (attrs, specs->attrs);
7843 specs->declspecs_seen_p = true;
7847 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7848 specifiers with any other type specifier to determine the resulting
7849 type. This is where ISO C checks on complex types are made, since
7850 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7853 struct c_declspecs *
7854 finish_declspecs (struct c_declspecs *specs)
7856 /* If a type was specified as a whole, we have no modifiers and are
7858 if (specs->type != NULL_TREE)
7860 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7861 && !specs->signed_p && !specs->unsigned_p
7862 && !specs->complex_p);
7866 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7867 has been specified, treat it as "int" unless "_Complex" is
7868 present and there are no other specifiers. If we just have
7869 "_Complex", it is equivalent to "_Complex double", but e.g.
7870 "_Complex short" is equivalent to "_Complex short int". */
7871 if (specs->typespec_word == cts_none)
7873 if (specs->long_p || specs->short_p
7874 || specs->signed_p || specs->unsigned_p)
7876 specs->typespec_word = cts_int;
7878 else if (specs->complex_p)
7880 specs->typespec_word = cts_double;
7882 pedwarn ("ISO C does not support plain %<complex%> meaning "
7883 "%<double complex%>");
7887 specs->typespec_word = cts_int;
7888 specs->default_int_p = true;
7889 /* We don't diagnose this here because grokdeclarator will
7890 give more specific diagnostics according to whether it is
7891 a function definition. */
7895 /* If "signed" was specified, record this to distinguish "int" and
7896 "signed int" in the case of a bit-field with
7897 -funsigned-bitfields. */
7898 specs->explicit_signed_p = specs->signed_p;
7900 /* Now compute the actual type. */
7901 switch (specs->typespec_word)
7904 gcc_assert (!specs->long_p && !specs->short_p
7905 && !specs->signed_p && !specs->unsigned_p
7906 && !specs->complex_p);
7907 specs->type = void_type_node;
7910 gcc_assert (!specs->long_p && !specs->short_p
7911 && !specs->signed_p && !specs->unsigned_p
7912 && !specs->complex_p);
7913 specs->type = boolean_type_node;
7916 gcc_assert (!specs->long_p && !specs->short_p);
7917 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7918 if (specs->signed_p)
7919 specs->type = signed_char_type_node;
7920 else if (specs->unsigned_p)
7921 specs->type = unsigned_char_type_node;
7923 specs->type = char_type_node;
7924 if (specs->complex_p)
7927 pedwarn ("ISO C does not support complex integer types");
7928 specs->type = build_complex_type (specs->type);
7932 gcc_assert (!(specs->long_p && specs->short_p));
7933 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7934 if (specs->long_long_p)
7935 specs->type = (specs->unsigned_p
7936 ? long_long_unsigned_type_node
7937 : long_long_integer_type_node);
7938 else if (specs->long_p)
7939 specs->type = (specs->unsigned_p
7940 ? long_unsigned_type_node
7941 : long_integer_type_node);
7942 else if (specs->short_p)
7943 specs->type = (specs->unsigned_p
7944 ? short_unsigned_type_node
7945 : short_integer_type_node);
7947 specs->type = (specs->unsigned_p
7948 ? unsigned_type_node
7949 : integer_type_node);
7950 if (specs->complex_p)
7953 pedwarn ("ISO C does not support complex integer types");
7954 specs->type = build_complex_type (specs->type);
7958 gcc_assert (!specs->long_p && !specs->short_p
7959 && !specs->signed_p && !specs->unsigned_p);
7960 specs->type = (specs->complex_p
7961 ? complex_float_type_node
7965 gcc_assert (!specs->long_long_p && !specs->short_p
7966 && !specs->signed_p && !specs->unsigned_p);
7969 specs->type = (specs->complex_p
7970 ? complex_long_double_type_node
7971 : long_double_type_node);
7975 specs->type = (specs->complex_p
7976 ? complex_double_type_node
7977 : double_type_node);
7983 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7984 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
7985 if (specs->typespec_word == cts_dfloat32)
7986 specs->type = dfloat32_type_node;
7987 else if (specs->typespec_word == cts_dfloat64)
7988 specs->type = dfloat64_type_node;
7990 specs->type = dfloat128_type_node;
7999 /* Synthesize a function which calls all the global ctors or global
8000 dtors in this file. This is only used for targets which do not
8001 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
8003 build_cdtor (int method_type, tree cdtors)
8010 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
8011 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
8014 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
8017 /* A subroutine of c_write_global_declarations. Perform final processing
8018 on one file scope's declarations (or the external scope's declarations),
8022 c_write_global_declarations_1 (tree globals)
8027 /* Process the decls in the order they were written. */
8028 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8030 /* Check for used but undefined static functions using the C
8031 standard's definition of "used", and set TREE_NO_WARNING so
8032 that check_global_declarations doesn't repeat the check. */
8033 if (TREE_CODE (decl) == FUNCTION_DECL
8034 && DECL_INITIAL (decl) == 0
8035 && DECL_EXTERNAL (decl)
8036 && !TREE_PUBLIC (decl)
8037 && C_DECL_USED (decl))
8039 pedwarn ("%q+F used but never defined", decl);
8040 TREE_NO_WARNING (decl) = 1;
8043 wrapup_global_declaration_1 (decl);
8049 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8050 reconsider |= wrapup_global_declaration_2 (decl);
8054 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8055 check_global_declaration_1 (decl);
8058 /* A subroutine of c_write_global_declarations Emit debug information for each
8059 of the declarations in GLOBALS. */
8062 c_write_global_declarations_2 (tree globals)
8066 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
8067 debug_hooks->global_decl (decl);
8070 /* Preserve the external declarations scope across a garbage collect. */
8071 static GTY(()) tree ext_block;
8074 c_write_global_declarations (void)
8078 /* We don't want to do this if generating a PCH. */
8082 /* Don't waste time on further processing if -fsyntax-only or we've
8083 encountered errors. */
8084 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
8087 /* Close the external scope. */
8088 ext_block = pop_scope ();
8090 gcc_assert (!current_scope);
8094 tree tmp = BLOCK_VARS (ext_block);
8096 FILE * stream = dump_begin (TDI_tu, &flags);
8099 dump_node (tmp, flags & ~TDF_SLIM, stream);
8100 dump_end (TDI_tu, stream);
8104 /* Process all file scopes in this compilation, and the external_scope,
8105 through wrapup_global_declarations and check_global_declarations. */
8106 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8107 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
8108 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
8110 /* Generate functions to call static constructors and destructors
8111 for targets that do not support .ctors/.dtors sections. These
8112 functions have magic names which are detected by collect2. */
8113 build_cdtor ('I', static_ctors); static_ctors = 0;
8114 build_cdtor ('D', static_dtors); static_dtors = 0;
8116 /* We're done parsing; proceed to optimize and emit assembly.
8117 FIXME: shouldn't be the front end's responsibility to call this. */
8120 /* After cgraph has had a chance to emit everything that's going to
8121 be emitted, output debug information for globals. */
8122 if (errorcount == 0 && sorrycount == 0)
8124 timevar_push (TV_SYMOUT);
8125 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8126 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
8127 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
8128 timevar_pop (TV_SYMOUT);
8134 #include "gt-c-decl.h"