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
457 enum deprecated_states {
462 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
465 c_print_identifier (FILE *file, tree node, int indent)
467 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
468 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
469 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
470 if (C_IS_RESERVED_WORD (node))
472 tree rid = ridpointers[C_RID_CODE (node)];
473 indent_to (file, indent + 4);
474 fprintf (file, "rid %p \"%s\"",
475 (void *) rid, IDENTIFIER_POINTER (rid));
479 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
480 which may be any of several kinds of DECL or TYPE or error_mark_node,
481 in the scope SCOPE. */
483 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
485 struct c_binding *b, **here;
487 if (binding_freelist)
489 b = binding_freelist;
490 binding_freelist = b->prev;
493 b = GGC_NEW (struct c_binding);
498 b->depth = scope->depth;
499 b->invisible = invisible;
505 b->prev = scope->bindings;
511 switch (TREE_CODE (decl))
513 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
516 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
522 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
528 /* Locate the appropriate place in the chain of shadowed decls
529 to insert this binding. Normally, scope == current_scope and
530 this does nothing. */
531 while (*here && (*here)->depth > scope->depth)
532 here = &(*here)->shadowed;
538 /* Clear the binding structure B, stick it on the binding_freelist,
539 and return the former value of b->prev. This is used by pop_scope
540 and get_parm_info to iterate destructively over all the bindings
541 from a given scope. */
542 static struct c_binding *
543 free_binding_and_advance (struct c_binding *b)
545 struct c_binding *prev = b->prev;
547 memset (b, 0, sizeof (struct c_binding));
548 b->prev = binding_freelist;
549 binding_freelist = b;
555 /* Hook called at end of compilation to assume 1 elt
556 for a file-scope tentative array defn that wasn't complete before. */
559 c_finish_incomplete_decl (tree decl)
561 if (TREE_CODE (decl) == VAR_DECL)
563 tree type = TREE_TYPE (decl);
564 if (type != error_mark_node
565 && TREE_CODE (type) == ARRAY_TYPE
566 && !DECL_EXTERNAL (decl)
567 && TYPE_DOMAIN (type) == 0)
569 warning (0, "array %q+D assumed to have one element", decl);
571 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
573 layout_decl (decl, 0);
578 /* The Objective-C front-end often needs to determine the current scope. */
581 objc_get_current_scope (void)
583 return current_scope;
586 /* The following function is used only by Objective-C. It needs to live here
587 because it accesses the innards of c_scope. */
590 objc_mark_locals_volatile (void *enclosing_blk)
592 struct c_scope *scope;
595 for (scope = current_scope;
596 scope && scope != enclosing_blk;
597 scope = scope->outer)
599 for (b = scope->bindings; b; b = b->prev)
600 objc_volatilize_decl (b->decl);
602 /* Do not climb up past the current function. */
603 if (scope->function_body)
608 /* Nonzero if we are currently in file scope. */
611 global_bindings_p (void)
613 return current_scope == file_scope && !c_override_global_bindings_to_false;
617 keep_next_level (void)
619 keep_next_level_flag = true;
622 /* Identify this scope as currently being filled with parameters. */
625 declare_parm_level (void)
627 current_scope->parm_flag = true;
633 if (next_is_function_body)
635 /* This is the transition from the parameters to the top level
636 of the function body. These are the same scope
637 (C99 6.2.1p4,6) so we do not push another scope structure.
638 next_is_function_body is set only by store_parm_decls, which
639 in turn is called when and only when we are about to
640 encounter the opening curly brace for the function body.
642 The outermost block of a function always gets a BLOCK node,
643 because the debugging output routines expect that each
644 function has at least one BLOCK. */
645 current_scope->parm_flag = false;
646 current_scope->function_body = true;
647 current_scope->keep = true;
648 current_scope->outer_function = current_function_scope;
649 current_function_scope = current_scope;
651 keep_next_level_flag = false;
652 next_is_function_body = false;
656 struct c_scope *scope;
659 scope = scope_freelist;
660 scope_freelist = scope->outer;
663 scope = GGC_CNEW (struct c_scope);
665 scope->keep = keep_next_level_flag;
666 scope->outer = current_scope;
667 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
669 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
671 if (current_scope && scope->depth == 0)
674 sorry ("GCC supports only %u nested scopes", scope->depth);
677 current_scope = scope;
678 keep_next_level_flag = false;
682 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
685 set_type_context (tree type, tree context)
687 for (type = TYPE_MAIN_VARIANT (type); type;
688 type = TYPE_NEXT_VARIANT (type))
689 TYPE_CONTEXT (type) = context;
692 /* Exit a scope. Restore the state of the identifier-decl mappings
693 that were in effect when this scope was entered. Return a BLOCK
694 node containing all the DECLs in this scope that are of interest
695 to debug info generation. */
700 struct c_scope *scope = current_scope;
701 tree block, context, p;
704 bool functionbody = scope->function_body;
705 bool keep = functionbody || scope->keep || scope->bindings;
707 c_end_vm_scope (scope->depth);
709 /* If appropriate, create a BLOCK to record the decls for the life
714 block = make_node (BLOCK);
715 BLOCK_SUBBLOCKS (block) = scope->blocks;
716 TREE_USED (block) = 1;
718 /* In each subblock, record that this is its superior. */
719 for (p = scope->blocks; p; p = TREE_CHAIN (p))
720 BLOCK_SUPERCONTEXT (p) = block;
722 BLOCK_VARS (block) = 0;
725 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
726 scope must be set so that they point to the appropriate
727 construct, i.e. either to the current FUNCTION_DECL node, or
728 else to the BLOCK node we just constructed.
730 Note that for tagged types whose scope is just the formal
731 parameter list for some function type specification, we can't
732 properly set their TYPE_CONTEXTs here, because we don't have a
733 pointer to the appropriate FUNCTION_TYPE node readily available
734 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
735 type nodes get set in `grokdeclarator' as soon as we have created
736 the FUNCTION_TYPE node which will represent the "scope" for these
737 "parameter list local" tagged types. */
738 if (scope->function_body)
739 context = current_function_decl;
740 else if (scope == file_scope)
742 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
743 TREE_CHAIN (file_decl) = all_translation_units;
744 all_translation_units = file_decl;
750 /* Clear all bindings in this scope. */
751 for (b = scope->bindings; b; b = free_binding_and_advance (b))
754 switch (TREE_CODE (p))
757 /* Warnings for unused labels, errors for undefined labels. */
758 if (TREE_USED (p) && !DECL_INITIAL (p))
760 error ("label %q+D used but not defined", p);
761 DECL_INITIAL (p) = error_mark_node;
763 else if (!TREE_USED (p) && warn_unused_label)
765 if (DECL_INITIAL (p))
766 warning (0, "label %q+D defined but not used", p);
768 warning (0, "label %q+D declared but not defined", p);
770 /* Labels go in BLOCK_VARS. */
771 TREE_CHAIN (p) = BLOCK_VARS (block);
772 BLOCK_VARS (block) = p;
773 gcc_assert (I_LABEL_BINDING (b->id) == b);
774 I_LABEL_BINDING (b->id) = b->shadowed;
780 set_type_context (p, context);
782 /* Types may not have tag-names, in which case the type
783 appears in the bindings list with b->id NULL. */
786 gcc_assert (I_TAG_BINDING (b->id) == b);
787 I_TAG_BINDING (b->id) = b->shadowed;
792 /* Propagate TREE_ADDRESSABLE from nested functions to their
793 containing functions. */
794 if (!TREE_ASM_WRITTEN (p)
795 && DECL_INITIAL (p) != 0
796 && TREE_ADDRESSABLE (p)
797 && DECL_ABSTRACT_ORIGIN (p) != 0
798 && DECL_ABSTRACT_ORIGIN (p) != p)
799 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
800 if (!DECL_EXTERNAL (p)
801 && DECL_INITIAL (p) == 0)
803 error ("nested function %q+D declared but never defined", p);
804 undef_nested_function = true;
806 /* C99 6.7.4p6: "a function with external linkage... declared
807 with an inline function specifier ... shall also be defined in the
808 same translation unit." */
809 else if (DECL_DECLARED_INLINE_P (p)
812 && !flag_gnu89_inline)
813 pedwarn ("inline function %q+D declared but never defined", p);
818 /* Warnings for unused variables. */
820 && !TREE_NO_WARNING (p)
821 && !DECL_IN_SYSTEM_HEADER (p)
823 && !DECL_ARTIFICIAL (p)
824 && scope != file_scope
825 && scope != external_scope)
826 warning (OPT_Wunused_variable, "unused variable %q+D", p);
830 error ("type of array %q+D completed incompatibly with"
831 " implicit initialization", p);
838 /* All of these go in BLOCK_VARS, but only if this is the
839 binding in the home scope. */
842 TREE_CHAIN (p) = BLOCK_VARS (block);
843 BLOCK_VARS (block) = p;
845 /* If this is the file scope, and we are processing more
846 than one translation unit in this compilation, set
847 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
848 This makes same_translation_unit_p work, and causes
849 static declarations to be given disambiguating suffixes. */
850 if (scope == file_scope && num_in_fnames > 1)
852 DECL_CONTEXT (p) = context;
853 if (TREE_CODE (p) == TYPE_DECL)
854 set_type_context (TREE_TYPE (p), context);
858 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
859 already been put there by store_parm_decls. Unused-
860 parameter warnings are handled by function.c.
861 error_mark_node obviously does not go in BLOCK_VARS and
862 does not get unused-variable warnings. */
865 /* It is possible for a decl not to have a name. We get
866 here with b->id NULL in this case. */
869 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
870 I_SYMBOL_BINDING (b->id) = b->shadowed;
871 if (b->shadowed && b->shadowed->type)
872 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
882 /* Dispose of the block that we just made inside some higher level. */
883 if ((scope->function_body || scope == file_scope) && context)
885 DECL_INITIAL (context) = block;
886 BLOCK_SUPERCONTEXT (block) = context;
888 else if (scope->outer)
891 SCOPE_LIST_APPEND (scope->outer, blocks, block);
892 /* If we did not make a block for the scope just exited, any
893 blocks made for inner scopes must be carried forward so they
894 will later become subblocks of something else. */
895 else if (scope->blocks)
896 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
899 /* Pop the current scope, and free the structure for reuse. */
900 current_scope = scope->outer;
901 if (scope->function_body)
902 current_function_scope = scope->outer_function;
904 memset (scope, 0, sizeof (struct c_scope));
905 scope->outer = scope_freelist;
906 scope_freelist = scope;
912 push_file_scope (void)
920 file_scope = current_scope;
922 start_fname_decls ();
924 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
925 bind (DECL_NAME (decl), decl, file_scope,
926 /*invisible=*/false, /*nested=*/true);
930 pop_file_scope (void)
932 /* In case there were missing closebraces, get us back to the global
934 while (current_scope != file_scope)
937 /* __FUNCTION__ is defined at file scope (""). This
938 call may not be necessary as my tests indicate it
939 still works without it. */
940 finish_fname_decls ();
942 /* This is the point to write out a PCH if we're doing that.
943 In that case we do not want to do anything else. */
946 c_common_write_pch ();
950 /* Pop off the file scope and close this translation unit. */
954 maybe_apply_pending_pragma_weaks ();
955 cgraph_finalize_compilation_unit ();
958 /* Insert BLOCK at the end of the list of subblocks of the current
959 scope. This is used when a BIND_EXPR is expanded, to handle the
960 BLOCK node inside the BIND_EXPR. */
963 insert_block (tree block)
965 TREE_USED (block) = 1;
966 SCOPE_LIST_APPEND (current_scope, blocks, block);
969 /* Push a definition or a declaration of struct, union or enum tag "name".
970 "type" should be the type node.
971 We assume that the tag "name" is not already defined.
973 Note that the definition may really be just a forward reference.
974 In that case, the TYPE_SIZE will be zero. */
977 pushtag (tree name, tree type)
979 /* Record the identifier as the type's name if it has none. */
980 if (name && !TYPE_NAME (type))
981 TYPE_NAME (type) = name;
982 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
984 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
985 tagged type we just added to the current scope. This fake
986 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
987 to output a representation of a tagged type, and it also gives
988 us a convenient place to record the "scope start" address for the
991 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
993 /* An approximation for now, so we can tell this is a function-scope tag.
994 This will be updated in pop_scope. */
995 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
998 /* Subroutine of compare_decls. Allow harmless mismatches in return
999 and argument types provided that the type modes match. This function
1000 return a unified type given a suitable match, and 0 otherwise. */
1003 match_builtin_function_types (tree newtype, tree oldtype)
1005 tree newrettype, oldrettype;
1006 tree newargs, oldargs;
1007 tree trytype, tryargs;
1009 /* Accept the return type of the new declaration if same modes. */
1010 oldrettype = TREE_TYPE (oldtype);
1011 newrettype = TREE_TYPE (newtype);
1013 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1016 oldargs = TYPE_ARG_TYPES (oldtype);
1017 newargs = TYPE_ARG_TYPES (newtype);
1020 while (oldargs || newargs)
1024 || !TREE_VALUE (oldargs)
1025 || !TREE_VALUE (newargs)
1026 || TYPE_MODE (TREE_VALUE (oldargs))
1027 != TYPE_MODE (TREE_VALUE (newargs)))
1030 oldargs = TREE_CHAIN (oldargs);
1031 newargs = TREE_CHAIN (newargs);
1034 trytype = build_function_type (newrettype, tryargs);
1035 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1038 /* Subroutine of diagnose_mismatched_decls. Check for function type
1039 mismatch involving an empty arglist vs a nonempty one and give clearer
1042 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1043 tree newtype, tree oldtype)
1047 if (TREE_CODE (olddecl) != FUNCTION_DECL
1048 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1049 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1051 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1054 t = TYPE_ARG_TYPES (oldtype);
1056 t = TYPE_ARG_TYPES (newtype);
1057 for (; t; t = TREE_CHAIN (t))
1059 tree type = TREE_VALUE (t);
1061 if (TREE_CHAIN (t) == 0
1062 && TYPE_MAIN_VARIANT (type) != void_type_node)
1064 inform ("a parameter list with an ellipsis can%'t match "
1065 "an empty parameter name list declaration");
1069 if (c_type_promotes_to (type) != type)
1071 inform ("an argument type that has a default promotion can%'t match "
1072 "an empty parameter name list declaration");
1078 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1079 old-style function definition, NEWDECL is a prototype declaration.
1080 Diagnose inconsistencies in the argument list. Returns TRUE if
1081 the prototype is compatible, FALSE if not. */
1083 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1085 tree newargs, oldargs;
1088 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1090 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1091 newargs = TYPE_ARG_TYPES (newtype);
1096 tree oldargtype = TREE_VALUE (oldargs);
1097 tree newargtype = TREE_VALUE (newargs);
1099 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1102 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1103 newargtype = TYPE_MAIN_VARIANT (newargtype);
1105 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1108 /* Reaching the end of just one list means the two decls don't
1109 agree on the number of arguments. */
1110 if (END_OF_ARGLIST (oldargtype))
1112 error ("prototype for %q+D declares more arguments "
1113 "than previous old-style definition", newdecl);
1116 else if (END_OF_ARGLIST (newargtype))
1118 error ("prototype for %q+D declares fewer arguments "
1119 "than previous old-style definition", newdecl);
1123 /* Type for passing arg must be consistent with that declared
1125 else if (!comptypes (oldargtype, newargtype))
1127 error ("prototype for %q+D declares argument %d"
1128 " with incompatible type",
1133 oldargs = TREE_CHAIN (oldargs);
1134 newargs = TREE_CHAIN (newargs);
1138 /* If we get here, no errors were found, but do issue a warning
1139 for this poor-style construct. */
1140 warning (0, "prototype for %q+D follows non-prototype definition",
1143 #undef END_OF_ARGLIST
1146 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1147 first in a pair of mismatched declarations, using the diagnostic
1150 locate_old_decl (tree decl, void (*diag)(const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2))
1152 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1154 else if (DECL_INITIAL (decl))
1155 diag (G_("previous definition of %q+D was here"), decl);
1156 else if (C_DECL_IMPLICIT (decl))
1157 diag (G_("previous implicit declaration of %q+D was here"), decl);
1159 diag (G_("previous declaration of %q+D was here"), decl);
1162 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1163 Returns true if the caller should proceed to merge the two, false
1164 if OLDDECL should simply be discarded. As a side effect, issues
1165 all necessary diagnostics for invalid or poor-style combinations.
1166 If it returns true, writes the types of NEWDECL and OLDDECL to
1167 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1168 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1171 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1172 tree *newtypep, tree *oldtypep)
1174 tree newtype, oldtype;
1175 bool pedwarned = false;
1176 bool warned = false;
1179 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1180 && DECL_EXTERNAL (DECL))
1182 /* If we have error_mark_node for either decl or type, just discard
1183 the previous decl - we're in an error cascade already. */
1184 if (olddecl == error_mark_node || newdecl == error_mark_node)
1186 *oldtypep = oldtype = TREE_TYPE (olddecl);
1187 *newtypep = newtype = TREE_TYPE (newdecl);
1188 if (oldtype == error_mark_node || newtype == error_mark_node)
1191 /* Two different categories of symbol altogether. This is an error
1192 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1193 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1195 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1196 && DECL_BUILT_IN (olddecl)
1197 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1199 error ("%q+D redeclared as different kind of symbol", newdecl);
1200 locate_old_decl (olddecl, error);
1202 else if (TREE_PUBLIC (newdecl))
1203 warning (0, "built-in function %q+D declared as non-function",
1206 warning (OPT_Wshadow, "declaration of %q+D shadows "
1207 "a built-in function", newdecl);
1211 /* Enumerators have no linkage, so may only be declared once in a
1213 if (TREE_CODE (olddecl) == CONST_DECL)
1215 error ("redeclaration of enumerator %q+D", newdecl);
1216 locate_old_decl (olddecl, error);
1220 if (!comptypes (oldtype, newtype))
1222 if (TREE_CODE (olddecl) == FUNCTION_DECL
1223 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1225 /* Accept harmless mismatch in function types.
1226 This is for the ffs and fprintf builtins. */
1227 tree trytype = match_builtin_function_types (newtype, oldtype);
1229 if (trytype && comptypes (newtype, trytype))
1230 *oldtypep = oldtype = trytype;
1233 /* If types don't match for a built-in, throw away the
1234 built-in. No point in calling locate_old_decl here, it
1235 won't print anything. */
1236 warning (0, "conflicting types for built-in function %q+D",
1241 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1242 && DECL_IS_BUILTIN (olddecl))
1244 /* A conflicting function declaration for a predeclared
1245 function that isn't actually built in. Objective C uses
1246 these. The new declaration silently overrides everything
1247 but the volatility (i.e. noreturn) indication. See also
1248 below. FIXME: Make Objective C use normal builtins. */
1249 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1252 /* Permit void foo (...) to match int foo (...) if the latter is
1253 the definition and implicit int was used. See
1254 c-torture/compile/920625-2.c. */
1255 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1256 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1257 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1258 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1260 pedwarn ("conflicting types for %q+D", newdecl);
1261 /* Make sure we keep void as the return type. */
1262 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1263 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1266 /* Permit void foo (...) to match an earlier call to foo (...) with
1267 no declared type (thus, implicitly int). */
1268 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1269 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1270 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1271 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1273 pedwarn ("conflicting types for %q+D", newdecl);
1274 /* Make sure we keep void as the return type. */
1275 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1280 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1281 error ("conflicting type qualifiers for %q+D", newdecl);
1283 error ("conflicting types for %q+D", newdecl);
1284 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1285 locate_old_decl (olddecl, error);
1290 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1291 but silently ignore the redeclaration if either is in a system
1292 header. (Conflicting redeclarations were handled above.) */
1293 if (TREE_CODE (newdecl) == TYPE_DECL)
1295 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1296 return true; /* Allow OLDDECL to continue in use. */
1298 error ("redefinition of typedef %q+D", newdecl);
1299 locate_old_decl (olddecl, error);
1303 /* Function declarations can either be 'static' or 'extern' (no
1304 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1305 can never conflict with each other on account of linkage
1306 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1307 gnu89 mode permits two definitions if one is 'extern inline' and
1308 one is not. The non- extern-inline definition supersedes the
1309 extern-inline definition. */
1311 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1313 /* If you declare a built-in function name as static, or
1314 define the built-in with an old-style definition (so we
1315 can't validate the argument list) the built-in definition is
1316 overridden, but optionally warn this was a bad choice of name. */
1317 if (DECL_BUILT_IN (olddecl)
1318 && !C_DECL_DECLARED_BUILTIN (olddecl)
1319 && (!TREE_PUBLIC (newdecl)
1320 || (DECL_INITIAL (newdecl)
1321 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1323 warning (OPT_Wshadow, "declaration of %q+D shadows "
1324 "a built-in function", newdecl);
1325 /* Discard the old built-in function. */
1329 if (DECL_INITIAL (newdecl))
1331 if (DECL_INITIAL (olddecl))
1333 /* If both decls are in the same TU and the new declaration
1334 isn't overriding an extern inline reject the new decl.
1335 In c99, no overriding is allowed in the same translation
1337 if ((!DECL_EXTERN_INLINE (olddecl)
1338 || DECL_EXTERN_INLINE (newdecl)
1339 || (!flag_gnu89_inline
1340 && (!DECL_DECLARED_INLINE_P (olddecl)
1341 || !lookup_attribute ("gnu_inline",
1342 DECL_ATTRIBUTES (olddecl)))
1343 && (!DECL_DECLARED_INLINE_P (newdecl)
1344 || !lookup_attribute ("gnu_inline",
1345 DECL_ATTRIBUTES (newdecl))))
1347 && same_translation_unit_p (newdecl, olddecl))
1349 error ("redefinition of %q+D", newdecl);
1350 locate_old_decl (olddecl, error);
1355 /* If we have a prototype after an old-style function definition,
1356 the argument types must be checked specially. */
1357 else if (DECL_INITIAL (olddecl)
1358 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1359 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1360 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1362 locate_old_decl (olddecl, error);
1365 /* A non-static declaration (even an "extern") followed by a
1366 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1367 The same is true for a static forward declaration at block
1368 scope followed by a non-static declaration/definition at file
1369 scope. Static followed by non-static at the same scope is
1370 not undefined behavior, and is the most convenient way to get
1371 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1372 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1373 we do diagnose it if -Wtraditional. */
1374 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1376 /* Two exceptions to the rule. If olddecl is an extern
1377 inline, or a predeclared function that isn't actually
1378 built in, newdecl silently overrides olddecl. The latter
1379 occur only in Objective C; see also above. (FIXME: Make
1380 Objective C use normal builtins.) */
1381 if (!DECL_IS_BUILTIN (olddecl)
1382 && !DECL_EXTERN_INLINE (olddecl))
1384 error ("static declaration of %q+D follows "
1385 "non-static declaration", newdecl);
1386 locate_old_decl (olddecl, error);
1390 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1392 if (DECL_CONTEXT (olddecl))
1394 error ("non-static declaration of %q+D follows "
1395 "static declaration", newdecl);
1396 locate_old_decl (olddecl, error);
1399 else if (warn_traditional)
1401 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1402 "follows static declaration", newdecl);
1407 /* Make sure gnu_inline attribute is either not present, or
1408 present on all inline decls. */
1409 if (DECL_DECLARED_INLINE_P (olddecl)
1410 && DECL_DECLARED_INLINE_P (newdecl))
1412 bool newa = lookup_attribute ("gnu_inline",
1413 DECL_ATTRIBUTES (newdecl)) != NULL;
1414 bool olda = lookup_attribute ("gnu_inline",
1415 DECL_ATTRIBUTES (olddecl)) != NULL;
1418 error ("%<gnu_inline%> attribute present on %q+D",
1419 newa ? newdecl : olddecl);
1420 error ("%Jbut not here", newa ? olddecl : newdecl);
1424 else if (TREE_CODE (newdecl) == VAR_DECL)
1426 /* Only variables can be thread-local, and all declarations must
1427 agree on this property. */
1428 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1430 /* Nothing to check. Since OLDDECL is marked threadprivate
1431 and NEWDECL does not have a thread-local attribute, we
1432 will merge the threadprivate attribute into NEWDECL. */
1435 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1437 if (DECL_THREAD_LOCAL_P (newdecl))
1438 error ("thread-local declaration of %q+D follows "
1439 "non-thread-local declaration", newdecl);
1441 error ("non-thread-local declaration of %q+D follows "
1442 "thread-local declaration", newdecl);
1444 locate_old_decl (olddecl, error);
1448 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1449 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1451 error ("redefinition of %q+D", newdecl);
1452 locate_old_decl (olddecl, error);
1456 /* Objects declared at file scope: if the first declaration had
1457 external linkage (even if it was an external reference) the
1458 second must have external linkage as well, or the behavior is
1459 undefined. If the first declaration had internal linkage, then
1460 the second must too, or else be an external reference (in which
1461 case the composite declaration still has internal linkage).
1462 As for function declarations, we warn about the static-then-
1463 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1464 if (DECL_FILE_SCOPE_P (newdecl)
1465 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1467 if (DECL_EXTERNAL (newdecl))
1469 if (!DECL_FILE_SCOPE_P (olddecl))
1471 error ("extern declaration of %q+D follows "
1472 "declaration with no linkage", newdecl);
1473 locate_old_decl (olddecl, error);
1476 else if (warn_traditional)
1478 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1479 "follows static declaration", newdecl);
1485 if (TREE_PUBLIC (newdecl))
1486 error ("non-static declaration of %q+D follows "
1487 "static declaration", newdecl);
1489 error ("static declaration of %q+D follows "
1490 "non-static declaration", newdecl);
1492 locate_old_decl (olddecl, error);
1496 /* Two objects with the same name declared at the same block
1497 scope must both be external references (6.7p3). */
1498 else if (!DECL_FILE_SCOPE_P (newdecl))
1500 if (DECL_EXTERNAL (newdecl))
1502 /* Extern with initializer at block scope, which will
1503 already have received an error. */
1505 else if (DECL_EXTERNAL (olddecl))
1507 error ("declaration of %q+D with no linkage follows "
1508 "extern declaration", newdecl);
1509 locate_old_decl (olddecl, error);
1513 error ("redeclaration of %q+D with no linkage", newdecl);
1514 locate_old_decl (olddecl, error);
1522 /* All decls must agree on a visibility. */
1523 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1524 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1525 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1527 warning (0, "redeclaration of %q+D with different visibility "
1528 "(old visibility preserved)", newdecl);
1532 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1534 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1535 if (DECL_DECLARED_INLINE_P (newdecl)
1536 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1538 warning (OPT_Wattributes, "inline declaration of %qD follows "
1539 "declaration with attribute noinline", newdecl);
1542 else if (DECL_DECLARED_INLINE_P (olddecl)
1543 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1545 warning (OPT_Wattributes, "declaration of %q+D with attribute "
1546 "noinline follows inline declaration ", newdecl);
1550 /* Inline declaration after use or definition.
1551 ??? Should we still warn about this now we have unit-at-a-time
1552 mode and can get it right?
1553 Definitely don't complain if the decls are in different translation
1555 C99 permits this, so don't warn in that case. (The function
1556 may not be inlined everywhere in function-at-a-time mode, but
1557 we still shouldn't warn.) */
1558 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1559 && same_translation_unit_p (olddecl, newdecl)
1560 && flag_gnu89_inline)
1562 if (TREE_USED (olddecl))
1564 warning (0, "%q+D declared inline after being called", olddecl);
1567 else if (DECL_INITIAL (olddecl))
1569 warning (0, "%q+D declared inline after its definition", olddecl);
1574 else /* PARM_DECL, VAR_DECL */
1576 /* Redeclaration of a parameter is a constraint violation (this is
1577 not explicitly stated, but follows from C99 6.7p3 [no more than
1578 one declaration of the same identifier with no linkage in the
1579 same scope, except type tags] and 6.2.2p6 [parameters have no
1580 linkage]). We must check for a forward parameter declaration,
1581 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1582 an extension, the mandatory diagnostic for which is handled by
1583 mark_forward_parm_decls. */
1585 if (TREE_CODE (newdecl) == PARM_DECL
1586 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1588 error ("redefinition of parameter %q+D", newdecl);
1589 locate_old_decl (olddecl, error);
1594 /* Optional warning for completely redundant decls. */
1595 if (!warned && !pedwarned
1596 && warn_redundant_decls
1597 /* Don't warn about a function declaration followed by a
1599 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1600 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1601 /* Don't warn about redundant redeclarations of builtins. */
1602 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1603 && !DECL_BUILT_IN (newdecl)
1604 && DECL_BUILT_IN (olddecl)
1605 && !C_DECL_DECLARED_BUILTIN (olddecl))
1606 /* Don't warn about an extern followed by a definition. */
1607 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1608 /* Don't warn about forward parameter decls. */
1609 && !(TREE_CODE (newdecl) == PARM_DECL
1610 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1611 /* Don't warn about a variable definition following a declaration. */
1612 && !(TREE_CODE (newdecl) == VAR_DECL
1613 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1615 warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1620 /* Report location of previous decl/defn in a consistent manner. */
1621 if (warned || pedwarned)
1622 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1624 #undef DECL_EXTERN_INLINE
1629 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1630 consistent with OLDDECL, but carries new information. Merge the
1631 new information into OLDDECL. This function issues no
1635 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1637 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1638 && DECL_INITIAL (newdecl) != 0);
1639 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1640 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1641 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1642 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1643 bool extern_changed = false;
1645 /* For real parm decl following a forward decl, rechain the old decl
1646 in its new location and clear TREE_ASM_WRITTEN (it's not a
1647 forward decl anymore). */
1648 if (TREE_CODE (newdecl) == PARM_DECL
1649 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1651 struct c_binding *b, **here;
1653 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
1654 if ((*here)->decl == olddecl)
1661 b->prev = current_scope->bindings;
1662 current_scope->bindings = b;
1664 TREE_ASM_WRITTEN (olddecl) = 0;
1667 DECL_ATTRIBUTES (newdecl)
1668 = targetm.merge_decl_attributes (olddecl, newdecl);
1670 /* Merge the data types specified in the two decls. */
1672 = TREE_TYPE (olddecl)
1673 = composite_type (newtype, oldtype);
1675 /* Lay the type out, unless already done. */
1676 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1678 if (TREE_TYPE (newdecl) != error_mark_node)
1679 layout_type (TREE_TYPE (newdecl));
1680 if (TREE_CODE (newdecl) != FUNCTION_DECL
1681 && TREE_CODE (newdecl) != TYPE_DECL
1682 && TREE_CODE (newdecl) != CONST_DECL)
1683 layout_decl (newdecl, 0);
1687 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1688 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1689 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1690 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1691 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1692 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1694 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1695 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1700 /* Merge the type qualifiers. */
1701 if (TREE_READONLY (newdecl))
1702 TREE_READONLY (olddecl) = 1;
1704 if (TREE_THIS_VOLATILE (newdecl))
1705 TREE_THIS_VOLATILE (olddecl) = 1;
1707 /* Merge deprecatedness. */
1708 if (TREE_DEPRECATED (newdecl))
1709 TREE_DEPRECATED (olddecl) = 1;
1711 /* Keep source location of definition rather than declaration and of
1712 prototype rather than non-prototype unless that prototype is
1714 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1715 || (old_is_prototype && !new_is_prototype
1716 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1717 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1719 /* Merge the initialization information. */
1720 if (DECL_INITIAL (newdecl) == 0)
1721 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1723 /* Merge the threadprivate attribute. */
1724 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1726 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1727 C_DECL_THREADPRIVATE_P (newdecl) = 1;
1730 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1732 /* Merge the unused-warning information. */
1733 if (DECL_IN_SYSTEM_HEADER (olddecl))
1734 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1735 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1736 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1738 /* Merge the section attribute.
1739 We want to issue an error if the sections conflict but that
1740 must be done later in decl_attributes since we are called
1741 before attributes are assigned. */
1742 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1743 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1745 /* Copy the assembler name.
1746 Currently, it can only be defined in the prototype. */
1747 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1749 /* Use visibility of whichever declaration had it specified */
1750 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1752 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1753 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1756 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1758 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1759 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1760 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1761 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1762 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1763 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1764 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1765 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1766 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1767 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1770 /* Merge the storage class information. */
1771 merge_weak (newdecl, olddecl);
1773 /* For functions, static overrides non-static. */
1774 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1776 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1777 /* This is since we don't automatically
1778 copy the attributes of NEWDECL into OLDDECL. */
1779 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1780 /* If this clears `static', clear it in the identifier too. */
1781 if (!TREE_PUBLIC (olddecl))
1782 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1786 /* In c99, 'extern' declaration before (or after) 'inline' means this
1787 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1789 if (TREE_CODE (newdecl) == FUNCTION_DECL
1790 && !flag_gnu89_inline
1791 && (DECL_DECLARED_INLINE_P (newdecl)
1792 || DECL_DECLARED_INLINE_P (olddecl))
1793 && (!DECL_DECLARED_INLINE_P (newdecl)
1794 || !DECL_DECLARED_INLINE_P (olddecl)
1795 || !DECL_EXTERNAL (olddecl))
1796 && DECL_EXTERNAL (newdecl)
1797 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl)))
1798 DECL_EXTERNAL (newdecl) = 0;
1800 if (DECL_EXTERNAL (newdecl))
1802 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1803 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1805 /* An extern decl does not override previous storage class. */
1806 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1807 if (!DECL_EXTERNAL (newdecl))
1809 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1810 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1815 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1816 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1819 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1821 /* If we're redefining a function previously defined as extern
1822 inline, make sure we emit debug info for the inline before we
1823 throw it away, in case it was inlined into a function that
1824 hasn't been written out yet. */
1825 if (new_is_definition && DECL_INITIAL (olddecl))
1827 if (TREE_USED (olddecl)
1828 /* In unit-at-a-time mode we never inline re-defined extern
1829 inline functions. */
1830 && !flag_unit_at_a_time
1831 && cgraph_function_possibly_inlined_p (olddecl))
1832 (*debug_hooks->outlining_inline_function) (olddecl);
1834 /* The new defn must not be inline. */
1835 DECL_INLINE (newdecl) = 0;
1836 DECL_UNINLINABLE (newdecl) = 1;
1840 /* If either decl says `inline', this fn is inline, unless
1841 its definition was passed already. */
1842 if (DECL_DECLARED_INLINE_P (newdecl)
1843 || DECL_DECLARED_INLINE_P (olddecl))
1844 DECL_DECLARED_INLINE_P (newdecl) = 1;
1846 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1847 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1850 if (DECL_BUILT_IN (olddecl))
1852 /* If redeclaring a builtin function, it stays built in.
1853 But it gets tagged as having been declared. */
1854 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1855 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1856 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1857 if (new_is_prototype)
1858 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1860 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1861 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1864 /* Also preserve various other info from the definition. */
1865 if (!new_is_definition)
1867 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1868 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1869 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1870 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1871 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1873 /* Set DECL_INLINE on the declaration if we've got a body
1874 from which to instantiate. */
1875 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1877 DECL_INLINE (newdecl) = 1;
1878 DECL_ABSTRACT_ORIGIN (newdecl)
1879 = DECL_ABSTRACT_ORIGIN (olddecl);
1884 /* If a previous declaration said inline, mark the
1885 definition as inlinable. */
1886 if (DECL_DECLARED_INLINE_P (newdecl)
1887 && !DECL_UNINLINABLE (newdecl))
1888 DECL_INLINE (newdecl) = 1;
1892 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
1894 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1895 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1897 unsigned olddecl_uid = DECL_UID (olddecl);
1898 tree olddecl_context = DECL_CONTEXT (olddecl);
1900 memcpy ((char *) olddecl + sizeof (struct tree_common),
1901 (char *) newdecl + sizeof (struct tree_common),
1902 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1903 switch (TREE_CODE (olddecl))
1913 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1914 (char *) newdecl + sizeof (struct tree_decl_common),
1915 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1920 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1921 (char *) newdecl + sizeof (struct tree_decl_common),
1922 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1924 DECL_UID (olddecl) = olddecl_uid;
1925 DECL_CONTEXT (olddecl) = olddecl_context;
1928 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1929 so that encode_section_info has a chance to look at the new decl
1930 flags and attributes. */
1931 if (DECL_RTL_SET_P (olddecl)
1932 && (TREE_CODE (olddecl) == FUNCTION_DECL
1933 || (TREE_CODE (olddecl) == VAR_DECL
1934 && TREE_STATIC (olddecl))))
1935 make_decl_rtl (olddecl);
1937 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
1938 and the definition is coming from the old version, cgraph needs
1939 to be called again. */
1940 if (extern_changed && !new_is_definition
1941 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
1942 cgraph_finalize_function (olddecl, false);
1945 /* Handle when a new declaration NEWDECL has the same name as an old
1946 one OLDDECL in the same binding contour. Prints an error message
1949 If safely possible, alter OLDDECL to look like NEWDECL, and return
1950 true. Otherwise, return false. */
1953 duplicate_decls (tree newdecl, tree olddecl)
1955 tree newtype = NULL, oldtype = NULL;
1957 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1959 /* Avoid `unused variable' and other warnings warnings for OLDDECL. */
1960 TREE_NO_WARNING (olddecl) = 1;
1964 merge_decls (newdecl, olddecl, newtype, oldtype);
1969 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1971 warn_if_shadowing (tree new_decl)
1973 struct c_binding *b;
1975 /* Shadow warnings wanted? */
1977 /* No shadow warnings for internally generated vars. */
1978 || DECL_IS_BUILTIN (new_decl)
1979 /* No shadow warnings for vars made for inlining. */
1980 || DECL_FROM_INLINE (new_decl))
1983 /* Is anything being shadowed? Invisible decls do not count. */
1984 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1985 if (b->decl && b->decl != new_decl && !b->invisible)
1987 tree old_decl = b->decl;
1989 if (old_decl == error_mark_node)
1991 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
1992 "non-variable", new_decl);
1995 else if (TREE_CODE (old_decl) == PARM_DECL)
1996 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
1998 else if (DECL_FILE_SCOPE_P (old_decl))
1999 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2000 "declaration", new_decl);
2001 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2002 && DECL_BUILT_IN (old_decl))
2004 warning (OPT_Wshadow, "declaration of %q+D shadows "
2005 "a built-in function", new_decl);
2009 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2012 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
2019 /* Subroutine of pushdecl.
2021 X is a TYPE_DECL for a typedef statement. Create a brand new
2022 ..._TYPE node (which will be just a variant of the existing
2023 ..._TYPE node with identical properties) and then install X
2024 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
2026 The whole point here is to end up with a situation where each
2027 and every ..._TYPE node the compiler creates will be uniquely
2028 associated with AT MOST one node representing a typedef name.
2029 This way, even though the compiler substitutes corresponding
2030 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
2031 early on, later parts of the compiler can always do the reverse
2032 translation and get back the corresponding typedef name. For
2035 typedef struct S MY_TYPE;
2038 Later parts of the compiler might only know that `object' was of
2039 type `struct S' if it were not for code just below. With this
2040 code however, later parts of the compiler see something like:
2042 struct S' == struct S
2043 typedef struct S' MY_TYPE;
2046 And they can then deduce (from the node for type struct S') that
2047 the original object declaration was:
2051 Being able to do this is important for proper support of protoize,
2052 and also for generating precise symbolic debugging information
2053 which takes full account of the programmer's (typedef) vocabulary.
2055 Obviously, we don't want to generate a duplicate ..._TYPE node if
2056 the TYPE_DECL node that we are now processing really represents a
2057 standard built-in type.
2059 Since all standard types are effectively declared at line zero
2060 in the source file, we can easily check to see if we are working
2061 on a standard type by checking the current value of lineno. */
2064 clone_underlying_type (tree x)
2066 if (DECL_IS_BUILTIN (x))
2068 if (TYPE_NAME (TREE_TYPE (x)) == 0)
2069 TYPE_NAME (TREE_TYPE (x)) = x;
2071 else if (TREE_TYPE (x) != error_mark_node
2072 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
2074 tree tt = TREE_TYPE (x);
2075 DECL_ORIGINAL_TYPE (x) = tt;
2076 tt = build_variant_type_copy (tt);
2078 TREE_USED (tt) = TREE_USED (x);
2083 /* Record a decl-node X as belonging to the current lexical scope.
2084 Check for errors (such as an incompatible declaration for the same
2085 name already seen in the same scope).
2087 Returns either X or an old decl for the same name.
2088 If an old decl is returned, it may have been smashed
2089 to agree with what X says. */
2094 tree name = DECL_NAME (x);
2095 struct c_scope *scope = current_scope;
2096 struct c_binding *b;
2097 bool nested = false;
2099 /* Functions need the lang_decl data. */
2100 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
2101 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
2103 /* Must set DECL_CONTEXT for everything not at file scope or
2104 DECL_FILE_SCOPE_P won't work. Local externs don't count
2105 unless they have initializers (which generate code). */
2106 if (current_function_decl
2107 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2108 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2109 DECL_CONTEXT (x) = current_function_decl;
2111 /* If this is of variably modified type, prevent jumping into its
2113 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2114 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2115 c_begin_vm_scope (scope->depth);
2117 /* Anonymous decls are just inserted in the scope. */
2120 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2124 /* First, see if there is another declaration with the same name in
2125 the current scope. If there is, duplicate_decls may do all the
2126 work for us. If duplicate_decls returns false, that indicates
2127 two incompatible decls in the same scope; we are to silently
2128 replace the old one (duplicate_decls has issued all appropriate
2129 diagnostics). In particular, we should not consider possible
2130 duplicates in the external scope, or shadowing. */
2131 b = I_SYMBOL_BINDING (name);
2132 if (b && B_IN_SCOPE (b, scope))
2134 struct c_binding *b_ext, *b_use;
2135 tree type = TREE_TYPE (x);
2136 tree visdecl = b->decl;
2137 tree vistype = TREE_TYPE (visdecl);
2138 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2139 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2140 b->inner_comp = false;
2143 /* If this is an external linkage declaration, we should check
2144 for compatibility with the type in the external scope before
2145 setting the type at this scope based on the visible
2146 information only. */
2147 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2149 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2150 b_ext = b_ext->shadowed;
2155 TREE_TYPE (b_use->decl) = b_use->type;
2158 if (duplicate_decls (x, b_use->decl))
2162 /* Save the updated type in the external scope and
2163 restore the proper type for this scope. */
2165 if (comptypes (vistype, type))
2166 thistype = composite_type (vistype, type);
2168 thistype = TREE_TYPE (b_use->decl);
2169 b_use->type = TREE_TYPE (b_use->decl);
2170 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2171 && DECL_BUILT_IN (b_use->decl))
2173 = build_type_attribute_variant (thistype,
2176 TREE_TYPE (b_use->decl) = thistype;
2181 goto skip_external_and_shadow_checks;
2184 /* All declarations with external linkage, and all external
2185 references, go in the external scope, no matter what scope is
2186 current. However, the binding in that scope is ignored for
2187 purposes of normal name lookup. A separate binding structure is
2188 created in the requested scope; this governs the normal
2189 visibility of the symbol.
2191 The binding in the externals scope is used exclusively for
2192 detecting duplicate declarations of the same object, no matter
2193 what scope they are in; this is what we do here. (C99 6.2.7p2:
2194 All declarations that refer to the same object or function shall
2195 have compatible type; otherwise, the behavior is undefined.) */
2196 if (DECL_EXTERNAL (x) || scope == file_scope)
2198 tree type = TREE_TYPE (x);
2201 bool type_saved = false;
2202 if (b && !B_IN_EXTERNAL_SCOPE (b)
2203 && (TREE_CODE (b->decl) == FUNCTION_DECL
2204 || TREE_CODE (b->decl) == VAR_DECL)
2205 && DECL_FILE_SCOPE_P (b->decl))
2208 vistype = TREE_TYPE (visdecl);
2210 if (scope != file_scope
2211 && !DECL_IN_SYSTEM_HEADER (x))
2212 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2214 while (b && !B_IN_EXTERNAL_SCOPE (b))
2216 /* If this decl might be modified, save its type. This is
2217 done here rather than when the decl is first bound
2218 because the type may change after first binding, through
2219 being completed or through attributes being added. If we
2220 encounter multiple such decls, only the first should have
2221 its type saved; the others will already have had their
2222 proper types saved and the types will not have changed as
2223 their scopes will not have been re-entered. */
2224 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2226 b->type = TREE_TYPE (b->decl);
2229 if (B_IN_FILE_SCOPE (b)
2230 && TREE_CODE (b->decl) == VAR_DECL
2231 && TREE_STATIC (b->decl)
2232 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2233 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2234 && TREE_CODE (type) == ARRAY_TYPE
2235 && TYPE_DOMAIN (type)
2236 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2237 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2239 /* Array type completed in inner scope, which should be
2240 diagnosed if the completion does not have size 1 and
2241 it does not get completed in the file scope. */
2242 b->inner_comp = true;
2247 /* If a matching external declaration has been found, set its
2248 type to the composite of all the types of that declaration.
2249 After the consistency checks, it will be reset to the
2250 composite of the visible types only. */
2251 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2253 TREE_TYPE (b->decl) = b->type;
2255 /* The point of the same_translation_unit_p check here is,
2256 we want to detect a duplicate decl for a construct like
2257 foo() { extern bar(); } ... static bar(); but not if
2258 they are in different translation units. In any case,
2259 the static does not go in the externals scope. */
2261 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2262 && duplicate_decls (x, b->decl))
2267 if (comptypes (vistype, type))
2268 thistype = composite_type (vistype, type);
2270 thistype = TREE_TYPE (b->decl);
2274 b->type = TREE_TYPE (b->decl);
2275 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2277 = build_type_attribute_variant (thistype,
2278 TYPE_ATTRIBUTES (b->type));
2279 TREE_TYPE (b->decl) = thistype;
2280 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2283 else if (TREE_PUBLIC (x))
2285 if (visdecl && !b && duplicate_decls (x, visdecl))
2287 /* An external declaration at block scope referring to a
2288 visible entity with internal linkage. The composite
2289 type will already be correct for this scope, so we
2290 just need to fall through to make the declaration in
2297 bind (name, x, external_scope, /*invisible=*/true,
2304 if (TREE_CODE (x) != PARM_DECL)
2305 warn_if_shadowing (x);
2307 skip_external_and_shadow_checks:
2308 if (TREE_CODE (x) == TYPE_DECL)
2309 clone_underlying_type (x);
2311 bind (name, x, scope, /*invisible=*/false, nested);
2313 /* If x's type is incomplete because it's based on a
2314 structure or union which has not yet been fully declared,
2315 attach it to that structure or union type, so we can go
2316 back and complete the variable declaration later, if the
2317 structure or union gets fully declared.
2319 If the input is erroneous, we can have error_mark in the type
2320 slot (e.g. "f(void a, ...)") - that doesn't count as an
2322 if (TREE_TYPE (x) != error_mark_node
2323 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2325 tree element = TREE_TYPE (x);
2327 while (TREE_CODE (element) == ARRAY_TYPE)
2328 element = TREE_TYPE (element);
2329 element = TYPE_MAIN_VARIANT (element);
2331 if ((TREE_CODE (element) == RECORD_TYPE
2332 || TREE_CODE (element) == UNION_TYPE)
2333 && (TREE_CODE (x) != TYPE_DECL
2334 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2335 && !COMPLETE_TYPE_P (element))
2336 C_TYPE_INCOMPLETE_VARS (element)
2337 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2342 /* Record X as belonging to file scope.
2343 This is used only internally by the Objective-C front end,
2344 and is limited to its needs. duplicate_decls is not called;
2345 if there is any preexisting decl for this identifier, it is an ICE. */
2348 pushdecl_top_level (tree x)
2351 bool nested = false;
2352 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2354 name = DECL_NAME (x);
2356 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2358 if (TREE_PUBLIC (x))
2360 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2364 bind (name, x, file_scope, /*invisible=*/false, nested);
2370 implicit_decl_warning (tree id, tree olddecl)
2372 void (*diag) (const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2);
2373 switch (mesg_implicit_function_declaration)
2376 case 1: diag = warning0; break;
2377 case 2: diag = error; break;
2378 default: gcc_unreachable ();
2381 diag (G_("implicit declaration of function %qE"), id);
2383 locate_old_decl (olddecl, diag);
2386 /* Generate an implicit declaration for identifier FUNCTIONID as a
2387 function of type int (). */
2390 implicitly_declare (tree functionid)
2392 struct c_binding *b;
2396 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2398 if (B_IN_SCOPE (b, external_scope))
2407 if (decl == error_mark_node)
2410 /* FIXME: Objective-C has weird not-really-builtin functions
2411 which are supposed to be visible automatically. They wind up
2412 in the external scope because they're pushed before the file
2413 scope gets created. Catch this here and rebind them into the
2415 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2417 bind (functionid, decl, file_scope,
2418 /*invisible=*/false, /*nested=*/true);
2423 tree newtype = default_function_type;
2425 TREE_TYPE (decl) = b->type;
2426 /* Implicit declaration of a function already declared
2427 (somehow) in a different scope, or as a built-in.
2428 If this is the first time this has happened, warn;
2429 then recycle the old declaration but with the new type. */
2430 if (!C_DECL_IMPLICIT (decl))
2432 implicit_decl_warning (functionid, decl);
2433 C_DECL_IMPLICIT (decl) = 1;
2435 if (DECL_BUILT_IN (decl))
2437 newtype = build_type_attribute_variant (newtype,
2439 (TREE_TYPE (decl)));
2440 if (!comptypes (newtype, TREE_TYPE (decl)))
2442 warning (0, "incompatible implicit declaration of built-in"
2443 " function %qD", decl);
2444 newtype = TREE_TYPE (decl);
2449 if (!comptypes (newtype, TREE_TYPE (decl)))
2451 error ("incompatible implicit declaration of function %qD",
2453 locate_old_decl (decl, error);
2456 b->type = TREE_TYPE (decl);
2457 TREE_TYPE (decl) = newtype;
2458 bind (functionid, decl, current_scope,
2459 /*invisible=*/false, /*nested=*/true);
2464 /* Not seen before. */
2465 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2466 DECL_EXTERNAL (decl) = 1;
2467 TREE_PUBLIC (decl) = 1;
2468 C_DECL_IMPLICIT (decl) = 1;
2469 implicit_decl_warning (functionid, 0);
2470 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2472 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2474 /* C89 says implicit declarations are in the innermost block.
2475 So we record the decl in the standard fashion. */
2476 decl = pushdecl (decl);
2478 /* No need to call objc_check_decl here - it's a function type. */
2479 rest_of_decl_compilation (decl, 0, 0);
2481 /* Write a record describing this implicit function declaration
2482 to the prototypes file (if requested). */
2483 gen_aux_info_record (decl, 0, 1, 0);
2485 /* Possibly apply some default attributes to this implicit declaration. */
2486 decl_attributes (&decl, NULL_TREE, 0);
2491 /* Issue an error message for a reference to an undeclared variable
2492 ID, including a reference to a builtin outside of function-call
2493 context. Establish a binding of the identifier to error_mark_node
2494 in an appropriate scope, which will suppress further errors for the
2495 same identifier. The error message should be given location LOC. */
2497 undeclared_variable (tree id, location_t loc)
2499 static bool already = false;
2500 struct c_scope *scope;
2502 if (current_function_decl == 0)
2504 error ("%H%qE undeclared here (not in a function)", &loc, id);
2505 scope = current_scope;
2509 error ("%H%qE undeclared (first use in this function)", &loc, id);
2513 error ("%H(Each undeclared identifier is reported only once", &loc);
2514 error ("%Hfor each function it appears in.)", &loc);
2518 /* If we are parsing old-style parameter decls, current_function_decl
2519 will be nonnull but current_function_scope will be null. */
2520 scope = current_function_scope ? current_function_scope : current_scope;
2522 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2525 /* Subroutine of lookup_label, declare_label, define_label: construct a
2526 LABEL_DECL with all the proper frills. */
2529 make_label (tree name, location_t location)
2531 tree label = build_decl (LABEL_DECL, name, void_type_node);
2533 DECL_CONTEXT (label) = current_function_decl;
2534 DECL_MODE (label) = VOIDmode;
2535 DECL_SOURCE_LOCATION (label) = location;
2540 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2541 Create one if none exists so far for the current function.
2542 This is called when a label is used in a goto expression or
2543 has its address taken. */
2546 lookup_label (tree name)
2550 if (current_function_decl == 0)
2552 error ("label %qE referenced outside of any function", name);
2556 /* Use a label already defined or ref'd with this name, but not if
2557 it is inherited from a containing function and wasn't declared
2559 label = I_LABEL_DECL (name);
2560 if (label && (DECL_CONTEXT (label) == current_function_decl
2561 || C_DECLARED_LABEL_FLAG (label)))
2563 /* If the label has only been declared, update its apparent
2564 location to point here, for better diagnostics if it
2565 turns out not to have been defined. */
2566 if (!TREE_USED (label))
2567 DECL_SOURCE_LOCATION (label) = input_location;
2571 /* No label binding for that identifier; make one. */
2572 label = make_label (name, input_location);
2574 /* Ordinary labels go in the current function scope. */
2575 bind (name, label, current_function_scope,
2576 /*invisible=*/false, /*nested=*/false);
2580 /* Make a label named NAME in the current function, shadowing silently
2581 any that may be inherited from containing functions or containing
2582 scopes. This is called for __label__ declarations. */
2585 declare_label (tree name)
2587 struct c_binding *b = I_LABEL_BINDING (name);
2590 /* Check to make sure that the label hasn't already been declared
2592 if (b && B_IN_CURRENT_SCOPE (b))
2594 error ("duplicate label declaration %qE", name);
2595 locate_old_decl (b->decl, error);
2597 /* Just use the previous declaration. */
2601 label = make_label (name, input_location);
2602 C_DECLARED_LABEL_FLAG (label) = 1;
2604 /* Declared labels go in the current scope. */
2605 bind (name, label, current_scope,
2606 /*invisible=*/false, /*nested=*/false);
2610 /* Define a label, specifying the location in the source file.
2611 Return the LABEL_DECL node for the label, if the definition is valid.
2612 Otherwise return 0. */
2615 define_label (location_t location, tree name)
2617 /* Find any preexisting label with this name. It is an error
2618 if that label has already been defined in this function, or
2619 if there is a containing function with a declared label with
2621 tree label = I_LABEL_DECL (name);
2622 struct c_label_list *nlist_se, *nlist_vm;
2625 && ((DECL_CONTEXT (label) == current_function_decl
2626 && DECL_INITIAL (label) != 0)
2627 || (DECL_CONTEXT (label) != current_function_decl
2628 && C_DECLARED_LABEL_FLAG (label))))
2630 error ("%Hduplicate label %qD", &location, label);
2631 locate_old_decl (label, error);
2634 else if (label && DECL_CONTEXT (label) == current_function_decl)
2636 /* The label has been used or declared already in this function,
2637 but not defined. Update its location to point to this
2639 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2640 error ("%Jjump into statement expression", label);
2641 if (C_DECL_UNDEFINABLE_VM (label))
2642 error ("%Jjump into scope of identifier with variably modified type",
2644 DECL_SOURCE_LOCATION (label) = location;
2648 /* No label binding for that identifier; make one. */
2649 label = make_label (name, location);
2651 /* Ordinary labels go in the current function scope. */
2652 bind (name, label, current_function_scope,
2653 /*invisible=*/false, /*nested=*/false);
2656 if (!in_system_header && lookup_name (name))
2657 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2658 "for labels, identifier %qE conflicts", &location, name);
2660 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2661 nlist_se->next = label_context_stack_se->labels_def;
2662 nlist_se->label = label;
2663 label_context_stack_se->labels_def = nlist_se;
2665 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2666 nlist_vm->next = label_context_stack_vm->labels_def;
2667 nlist_vm->label = label;
2668 label_context_stack_vm->labels_def = nlist_vm;
2670 /* Mark label as having been defined. */
2671 DECL_INITIAL (label) = error_mark_node;
2675 /* Given NAME, an IDENTIFIER_NODE,
2676 return the structure (or union or enum) definition for that name.
2677 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2678 CODE says which kind of type the caller wants;
2679 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2680 If the wrong kind of type is found, an error is reported. */
2683 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2685 struct c_binding *b = I_TAG_BINDING (name);
2691 /* We only care about whether it's in this level if
2692 thislevel_only was set or it might be a type clash. */
2693 if (thislevel_only || TREE_CODE (b->decl) != code)
2695 /* For our purposes, a tag in the external scope is the same as
2696 a tag in the file scope. (Primarily relevant to Objective-C
2697 and its builtin structure tags, which get pushed before the
2698 file scope is created.) */
2699 if (B_IN_CURRENT_SCOPE (b)
2700 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2704 if (thislevel_only && !thislevel)
2707 if (TREE_CODE (b->decl) != code)
2709 /* Definition isn't the kind we were looking for. */
2710 pending_invalid_xref = name;
2711 pending_invalid_xref_location = input_location;
2713 /* If in the same binding level as a declaration as a tag
2714 of a different type, this must not be allowed to
2715 shadow that tag, so give the error immediately.
2716 (For example, "struct foo; union foo;" is invalid.) */
2718 pending_xref_error ();
2723 /* Print an error message now
2724 for a recent invalid struct, union or enum cross reference.
2725 We don't print them immediately because they are not invalid
2726 when used in the `struct foo;' construct for shadowing. */
2729 pending_xref_error (void)
2731 if (pending_invalid_xref != 0)
2732 error ("%H%qE defined as wrong kind of tag",
2733 &pending_invalid_xref_location, pending_invalid_xref);
2734 pending_invalid_xref = 0;
2738 /* Look up NAME in the current scope and its superiors
2739 in the namespace of variables, functions and typedefs.
2740 Return a ..._DECL node of some kind representing its definition,
2741 or return 0 if it is undefined. */
2744 lookup_name (tree name)
2746 struct c_binding *b = I_SYMBOL_BINDING (name);
2747 if (b && !b->invisible)
2752 /* Similar to `lookup_name' but look only at the indicated scope. */
2755 lookup_name_in_scope (tree name, struct c_scope *scope)
2757 struct c_binding *b;
2759 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2760 if (B_IN_SCOPE (b, scope))
2765 /* Create the predefined scalar types of C,
2766 and some nodes representing standard constants (0, 1, (void *) 0).
2767 Initialize the global scope.
2768 Make definitions for built-in primitive functions. */
2771 c_init_decl_processing (void)
2773 location_t save_loc = input_location;
2775 /* Initialize reserved words for parser. */
2778 current_function_decl = 0;
2780 gcc_obstack_init (&parser_obstack);
2782 /* Make the externals scope. */
2784 external_scope = current_scope;
2786 /* Declarations from c_common_nodes_and_builtins must not be associated
2787 with this input file, lest we get differences between using and not
2788 using preprocessed headers. */
2789 #ifdef USE_MAPPED_LOCATION
2790 input_location = BUILTINS_LOCATION;
2792 input_location.file = "<built-in>";
2793 input_location.line = 0;
2796 build_common_tree_nodes (flag_signed_char, false);
2798 c_common_nodes_and_builtins ();
2800 /* In C, comparisons and TRUTH_* expressions have type int. */
2801 truthvalue_type_node = integer_type_node;
2802 truthvalue_true_node = integer_one_node;
2803 truthvalue_false_node = integer_zero_node;
2805 /* Even in C99, which has a real boolean type. */
2806 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2807 boolean_type_node));
2809 input_location = save_loc;
2811 pedantic_lvalues = true;
2813 make_fname_decl = c_make_fname_decl;
2814 start_fname_decls ();
2817 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2818 decl, NAME is the initialization string and TYPE_DEP indicates whether
2819 NAME depended on the type of the function. As we don't yet implement
2820 delayed emission of static data, we mark the decl as emitted
2821 so it is not placed in the output. Anything using it must therefore pull
2822 out the STRING_CST initializer directly. FIXME. */
2825 c_make_fname_decl (tree id, int type_dep)
2827 const char *name = fname_as_string (type_dep);
2828 tree decl, type, init;
2829 size_t length = strlen (name);
2831 type = build_array_type (char_type_node,
2832 build_index_type (size_int (length)));
2833 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2835 decl = build_decl (VAR_DECL, id, type);
2837 TREE_STATIC (decl) = 1;
2838 TREE_READONLY (decl) = 1;
2839 DECL_ARTIFICIAL (decl) = 1;
2841 init = build_string (length + 1, name);
2842 free ((char *) name);
2843 TREE_TYPE (init) = type;
2844 DECL_INITIAL (decl) = init;
2846 TREE_USED (decl) = 1;
2848 if (current_function_decl
2849 /* For invalid programs like this:
2852 const char* p = __FUNCTION__;
2854 the __FUNCTION__ is believed to appear in K&R style function
2855 parameter declarator. In that case we still don't have
2857 && (!errorcount || current_function_scope))
2859 DECL_CONTEXT (decl) = current_function_decl;
2860 bind (id, decl, current_function_scope,
2861 /*invisible=*/false, /*nested=*/false);
2864 finish_decl (decl, init, NULL_TREE);
2869 /* Return a definition for a builtin function named NAME and whose data type
2870 is TYPE. TYPE should be a function type with argument types.
2871 FUNCTION_CODE tells later passes how to compile calls to this function.
2872 See tree.h for its possible values.
2874 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2875 the name to be called if we can't opencode the function. If
2876 ATTRS is nonzero, use that for the function's attribute list. */
2879 builtin_function (const char *name, tree type, int function_code,
2880 enum built_in_class cl, const char *library_name,
2883 tree id = get_identifier (name);
2884 tree decl = build_decl (FUNCTION_DECL, id, type);
2885 TREE_PUBLIC (decl) = 1;
2886 DECL_EXTERNAL (decl) = 1;
2887 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2888 DECL_BUILT_IN_CLASS (decl) = cl;
2889 DECL_FUNCTION_CODE (decl) = function_code;
2890 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2892 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2894 /* Should never be called on a symbol with a preexisting meaning. */
2895 gcc_assert (!I_SYMBOL_BINDING (id));
2897 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2899 /* Builtins in the implementation namespace are made visible without
2900 needing to be explicitly declared. See push_file_scope. */
2901 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2903 TREE_CHAIN (decl) = visible_builtins;
2904 visible_builtins = decl;
2907 /* Possibly apply some default attributes to this built-in function. */
2909 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2911 decl_attributes (&decl, NULL_TREE, 0);
2916 /* Called when a declaration is seen that contains no names to declare.
2917 If its type is a reference to a structure, union or enum inherited
2918 from a containing scope, shadow that tag name for the current scope
2919 with a forward reference.
2920 If its type defines a new named structure or union
2921 or defines an enum, it is valid but we need not do anything here.
2922 Otherwise, it is an error. */
2925 shadow_tag (const struct c_declspecs *declspecs)
2927 shadow_tag_warned (declspecs, 0);
2930 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2933 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2935 bool found_tag = false;
2937 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2939 tree value = declspecs->type;
2940 enum tree_code code = TREE_CODE (value);
2942 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2943 /* Used to test also that TYPE_SIZE (value) != 0.
2944 That caused warning for `struct foo;' at top level in the file. */
2946 tree name = TYPE_NAME (value);
2953 if (warned != 1 && code != ENUMERAL_TYPE)
2954 /* Empty unnamed enum OK */
2956 pedwarn ("unnamed struct/union that defines no instances");
2960 else if (!declspecs->tag_defined_p
2961 && declspecs->storage_class != csc_none)
2964 pedwarn ("empty declaration with storage class specifier "
2965 "does not redeclare tag");
2967 pending_xref_error ();
2969 else if (!declspecs->tag_defined_p
2970 && (declspecs->const_p
2971 || declspecs->volatile_p
2972 || declspecs->restrict_p))
2975 pedwarn ("empty declaration with type qualifier "
2976 "does not redeclare tag");
2978 pending_xref_error ();
2982 pending_invalid_xref = 0;
2983 t = lookup_tag (code, name, 1);
2987 t = make_node (code);
2994 if (warned != 1 && !in_system_header)
2996 pedwarn ("useless type name in empty declaration");
3001 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
3003 pedwarn ("useless type name in empty declaration");
3007 pending_invalid_xref = 0;
3009 if (declspecs->inline_p)
3011 error ("%<inline%> in empty declaration");
3015 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3017 error ("%<auto%> in file-scope empty declaration");
3021 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3023 error ("%<register%> in file-scope empty declaration");
3027 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3029 warning (0, "useless storage class specifier in empty declaration");
3033 if (!warned && !in_system_header && declspecs->thread_p)
3035 warning (0, "useless %<__thread%> in empty declaration");
3039 if (!warned && !in_system_header && (declspecs->const_p
3040 || declspecs->volatile_p
3041 || declspecs->restrict_p))
3043 warning (0, "useless type qualifier in empty declaration");
3050 pedwarn ("empty declaration");
3055 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3056 bits. SPECS represents declaration specifiers that the grammar
3057 only permits to contain type qualifiers and attributes. */
3060 quals_from_declspecs (const struct c_declspecs *specs)
3062 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3063 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3064 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3065 gcc_assert (!specs->type
3066 && !specs->decl_attr
3067 && specs->typespec_word == cts_none
3068 && specs->storage_class == csc_none
3069 && !specs->typedef_p
3070 && !specs->explicit_signed_p
3071 && !specs->deprecated_p
3073 && !specs->long_long_p
3076 && !specs->unsigned_p
3077 && !specs->complex_p
3079 && !specs->thread_p);
3083 /* Construct an array declarator. EXPR is the expression inside [],
3084 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3085 applied to the pointer to which a parameter array is converted).
3086 STATIC_P is true if "static" is inside the [], false otherwise.
3087 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3088 length which is nevertheless a complete type, false otherwise. The
3089 field for the contained declarator is left to be filled in by
3090 set_array_declarator_inner. */
3092 struct c_declarator *
3093 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3096 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3097 struct c_declarator);
3098 declarator->kind = cdk_array;
3099 declarator->declarator = 0;
3100 declarator->u.array.dimen = expr;
3103 declarator->u.array.attrs = quals->attrs;
3104 declarator->u.array.quals = quals_from_declspecs (quals);
3108 declarator->u.array.attrs = NULL_TREE;
3109 declarator->u.array.quals = 0;
3111 declarator->u.array.static_p = static_p;
3112 declarator->u.array.vla_unspec_p = vla_unspec_p;
3113 if (pedantic && !flag_isoc99)
3115 if (static_p || quals != NULL)
3116 pedwarn ("ISO C90 does not support %<static%> or type "
3117 "qualifiers in parameter array declarators");
3119 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3123 if (!current_scope->parm_flag)
3126 error ("%<[*]%> not allowed in other than function prototype scope");
3127 declarator->u.array.vla_unspec_p = false;
3130 current_scope->had_vla_unspec = true;
3135 /* Set the contained declarator of an array declarator. DECL is the
3136 declarator, as constructed by build_array_declarator; INNER is what
3137 appears on the left of the []. ABSTRACT_P is true if it is an
3138 abstract declarator, false otherwise; this is used to reject static
3139 and type qualifiers in abstract declarators, where they are not in
3140 the C99 grammar (subject to possible change in DR#289). */
3142 struct c_declarator *
3143 set_array_declarator_inner (struct c_declarator *decl,
3144 struct c_declarator *inner, bool abstract_p)
3146 decl->declarator = inner;
3147 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3148 || decl->u.array.attrs != NULL_TREE
3149 || decl->u.array.static_p))
3150 error ("static or type qualifiers in abstract declarator");
3154 /* INIT is a constructor that forms DECL's initializer. If the final
3155 element initializes a flexible array field, add the size of that
3156 initializer to DECL's size. */
3159 add_flexible_array_elts_to_size (tree decl, tree init)
3163 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3166 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3167 type = TREE_TYPE (elt);
3168 if (TREE_CODE (type) == ARRAY_TYPE
3169 && TYPE_SIZE (type) == NULL_TREE
3170 && TYPE_DOMAIN (type) != NULL_TREE
3171 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3173 complete_array_type (&type, elt, false);
3175 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3176 DECL_SIZE_UNIT (decl)
3177 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3181 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3184 groktypename (struct c_type_name *type_name)
3187 tree attrs = type_name->specs->attrs;
3189 type_name->specs->attrs = NULL_TREE;
3191 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3194 /* Apply attributes. */
3195 decl_attributes (&type, attrs, 0);
3200 /* Decode a declarator in an ordinary declaration or data definition.
3201 This is called as soon as the type information and variable name
3202 have been parsed, before parsing the initializer if any.
3203 Here we create the ..._DECL node, fill in its type,
3204 and put it on the list of decls for the current context.
3205 The ..._DECL node is returned as the value.
3207 Exception: for arrays where the length is not specified,
3208 the type is left null, to be filled in by `finish_decl'.
3210 Function definitions do not come here; they go to start_function
3211 instead. However, external and forward declarations of functions
3212 do go through here. Structure field declarations are done by
3213 grokfield and not through here. */
3216 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3217 bool initialized, tree attributes)
3222 /* An object declared as __attribute__((deprecated)) suppresses
3223 warnings of uses of other deprecated items. */
3224 if (lookup_attribute ("deprecated", attributes))
3225 deprecated_state = DEPRECATED_SUPPRESS;
3227 decl = grokdeclarator (declarator, declspecs,
3228 NORMAL, initialized, NULL);
3232 deprecated_state = DEPRECATED_NORMAL;
3234 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3235 && MAIN_NAME_P (DECL_NAME (decl)))
3236 warning (OPT_Wmain, "%q+D is usually a function", decl);
3239 /* Is it valid for this decl to have an initializer at all?
3240 If not, set INITIALIZED to zero, which will indirectly
3241 tell 'finish_decl' to ignore the initializer once it is parsed. */
3242 switch (TREE_CODE (decl))
3245 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3250 error ("function %qD is initialized like a variable", decl);
3255 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3256 error ("parameter %qD is initialized", decl);
3261 /* Don't allow initializations for incomplete types except for
3262 arrays which might be completed by the initialization. */
3264 /* This can happen if the array size is an undefined macro.
3265 We already gave a warning, so we don't need another one. */
3266 if (TREE_TYPE (decl) == error_mark_node)
3268 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3270 /* A complete type is ok if size is fixed. */
3272 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3273 || C_DECL_VARIABLE_SIZE (decl))
3275 error ("variable-sized object may not be initialized");
3279 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3281 error ("variable %qD has initializer but incomplete type", decl);
3284 else if (C_DECL_VARIABLE_SIZE (decl))
3286 /* Although C99 is unclear about whether incomplete arrays
3287 of VLAs themselves count as VLAs, it does not make
3288 sense to permit them to be initialized given that
3289 ordinary VLAs may not be initialized. */
3290 error ("variable-sized object may not be initialized");
3297 if (current_scope == file_scope)
3298 TREE_STATIC (decl) = 1;
3300 /* Tell 'pushdecl' this is an initialized decl
3301 even though we don't yet have the initializer expression.
3302 Also tell 'finish_decl' it may store the real initializer. */
3303 DECL_INITIAL (decl) = error_mark_node;
3306 /* If this is a function declaration, write a record describing it to the
3307 prototypes file (if requested). */
3309 if (TREE_CODE (decl) == FUNCTION_DECL)
3310 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3312 /* ANSI specifies that a tentative definition which is not merged with
3313 a non-tentative definition behaves exactly like a definition with an
3314 initializer equal to zero. (Section 3.7.2)
3316 -fno-common gives strict ANSI behavior, though this tends to break
3317 a large body of code that grew up without this rule.
3319 Thread-local variables are never common, since there's no entrenched
3320 body of code to break, and it allows more efficient variable references
3321 in the presence of dynamic linking. */
3323 if (TREE_CODE (decl) == VAR_DECL
3325 && TREE_PUBLIC (decl)
3326 && !DECL_THREAD_LOCAL_P (decl)
3328 DECL_COMMON (decl) = 1;
3330 /* Set attributes here so if duplicate decl, will have proper attributes. */
3331 decl_attributes (&decl, attributes, 0);
3333 /* Handle gnu_inline attribute. */
3334 if (declspecs->inline_p
3335 && !flag_gnu89_inline
3336 && TREE_CODE (decl) == FUNCTION_DECL
3337 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
3339 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3341 else if (declspecs->storage_class != csc_static)
3342 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3345 if (TREE_CODE (decl) == FUNCTION_DECL
3346 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3348 struct c_declarator *ce = declarator;
3350 if (ce->kind == cdk_pointer)
3351 ce = declarator->declarator;
3352 if (ce->kind == cdk_function)
3354 tree args = ce->u.arg_info->parms;
3355 for (; args; args = TREE_CHAIN (args))
3357 tree type = TREE_TYPE (args);
3358 if (type && INTEGRAL_TYPE_P (type)
3359 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3360 DECL_ARG_TYPE (args) = integer_type_node;
3365 if (TREE_CODE (decl) == FUNCTION_DECL
3366 && DECL_DECLARED_INLINE_P (decl)
3367 && DECL_UNINLINABLE (decl)
3368 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3369 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3372 /* C99 6.7.4p3: An inline definition of a function with external
3373 linkage shall not contain a definition of a modifiable object
3374 with static storage duration... */
3375 if (TREE_CODE (decl) == VAR_DECL
3376 && current_scope != file_scope
3377 && TREE_STATIC (decl)
3378 && !TREE_READONLY (decl)
3379 && DECL_DECLARED_INLINE_P (current_function_decl)
3380 && DECL_EXTERNAL (current_function_decl))
3381 pedwarn ("%q+D is static but declared in inline function %qD "
3382 "which is not static", decl, current_function_decl);
3384 /* Add this decl to the current scope.
3385 TEM may equal DECL or it may be a previous decl of the same name. */
3386 tem = pushdecl (decl);
3388 if (initialized && DECL_EXTERNAL (tem))
3390 DECL_EXTERNAL (tem) = 0;
3391 TREE_STATIC (tem) = 1;
3397 /* Initialize EH if not initialized yet and exceptions are enabled. */
3400 c_maybe_initialize_eh (void)
3402 if (!flag_exceptions || c_eh_initialized_p)
3405 c_eh_initialized_p = true;
3406 eh_personality_libfunc
3407 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3408 ? "__gcc_personality_sj0"
3409 : "__gcc_personality_v0");
3410 default_init_unwind_resume_libfunc ();
3411 using_eh_for_cleanups ();
3414 /* Finish processing of a declaration;
3415 install its initial value.
3416 If the length of an array type is not known before,
3417 it must be determined now, from the initial value, or it is an error. */
3420 finish_decl (tree decl, tree init, tree asmspec_tree)
3423 int was_incomplete = (DECL_SIZE (decl) == 0);
3424 const char *asmspec = 0;
3426 /* If a name was specified, get the string. */
3427 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3428 && DECL_FILE_SCOPE_P (decl))
3429 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3431 asmspec = TREE_STRING_POINTER (asmspec_tree);
3433 /* If `start_decl' didn't like having an initialization, ignore it now. */
3434 if (init != 0 && DECL_INITIAL (decl) == 0)
3437 /* Don't crash if parm is initialized. */
3438 if (TREE_CODE (decl) == PARM_DECL)
3442 store_init_value (decl, init);
3444 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3445 || TREE_CODE (decl) == FUNCTION_DECL
3446 || TREE_CODE (decl) == FIELD_DECL))
3447 objc_check_decl (decl);
3449 type = TREE_TYPE (decl);
3451 /* Deduce size of array from initialization, if not already known. */
3452 if (TREE_CODE (type) == ARRAY_TYPE
3453 && TYPE_DOMAIN (type) == 0
3454 && TREE_CODE (decl) != TYPE_DECL)
3457 = (TREE_STATIC (decl)
3458 /* Even if pedantic, an external linkage array
3459 may have incomplete type at first. */
3460 ? pedantic && !TREE_PUBLIC (decl)
3461 : !DECL_EXTERNAL (decl));
3463 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3466 /* Get the completed type made by complete_array_type. */
3467 type = TREE_TYPE (decl);
3472 error ("initializer fails to determine size of %q+D", decl);
3477 error ("array size missing in %q+D", decl);
3478 /* If a `static' var's size isn't known,
3479 make it extern as well as static, so it does not get
3481 If it is not `static', then do not mark extern;
3482 finish_incomplete_decl will give it a default size
3483 and it will get allocated. */
3484 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3485 DECL_EXTERNAL (decl) = 1;
3489 error ("zero or negative size array %q+D", decl);
3493 /* For global variables, update the copy of the type that
3494 exists in the binding. */
3495 if (TREE_PUBLIC (decl))
3497 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3498 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3499 b_ext = b_ext->shadowed;
3503 b_ext->type = composite_type (b_ext->type, type);
3514 if (DECL_INITIAL (decl))
3515 TREE_TYPE (DECL_INITIAL (decl)) = type;
3517 layout_decl (decl, 0);
3520 if (TREE_CODE (decl) == VAR_DECL)
3522 if (init && TREE_CODE (init) == CONSTRUCTOR)
3523 add_flexible_array_elts_to_size (decl, init);
3525 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3526 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3527 layout_decl (decl, 0);
3529 if (DECL_SIZE (decl) == 0
3530 /* Don't give an error if we already gave one earlier. */
3531 && TREE_TYPE (decl) != error_mark_node
3532 && (TREE_STATIC (decl)
3533 /* A static variable with an incomplete type
3534 is an error if it is initialized.
3535 Also if it is not file scope.
3536 Otherwise, let it through, but if it is not `extern'
3537 then it may cause an error message later. */
3538 ? (DECL_INITIAL (decl) != 0
3539 || !DECL_FILE_SCOPE_P (decl))
3540 /* An automatic variable with an incomplete type
3542 : !DECL_EXTERNAL (decl)))
3544 error ("storage size of %q+D isn%'t known", decl);
3545 TREE_TYPE (decl) = error_mark_node;
3548 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3549 && DECL_SIZE (decl) != 0)
3551 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3552 constant_expression_warning (DECL_SIZE (decl));
3554 error ("storage size of %q+D isn%'t constant", decl);
3557 if (TREE_USED (type))
3558 TREE_USED (decl) = 1;
3561 /* If this is a function and an assembler name is specified, reset DECL_RTL
3562 so we can give it its new name. Also, update built_in_decls if it
3563 was a normal built-in. */
3564 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3566 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3567 set_builtin_user_assembler_name (decl, asmspec);
3568 set_user_assembler_name (decl, asmspec);
3571 /* If #pragma weak was used, mark the decl weak now. */
3572 maybe_apply_pragma_weak (decl);
3574 /* Output the assembler code and/or RTL code for variables and functions,
3575 unless the type is an undefined structure or union.
3576 If not, it will get done when the type is completed. */
3578 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3580 /* Determine the ELF visibility. */
3581 if (TREE_PUBLIC (decl))
3582 c_determine_visibility (decl);
3584 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3585 if (c_dialect_objc ())
3586 objc_check_decl (decl);
3590 /* If this is not a static variable, issue a warning.
3591 It doesn't make any sense to give an ASMSPEC for an
3592 ordinary, non-register local variable. Historically,
3593 GCC has accepted -- but ignored -- the ASMSPEC in
3595 if (!DECL_FILE_SCOPE_P (decl)
3596 && TREE_CODE (decl) == VAR_DECL
3597 && !C_DECL_REGISTER (decl)
3598 && !TREE_STATIC (decl))
3599 warning (0, "ignoring asm-specifier for non-static local "
3600 "variable %q+D", decl);
3602 set_user_assembler_name (decl, asmspec);
3605 if (DECL_FILE_SCOPE_P (decl))
3607 if (DECL_INITIAL (decl) == NULL_TREE
3608 || DECL_INITIAL (decl) == error_mark_node)
3609 /* Don't output anything
3610 when a tentative file-scope definition is seen.
3611 But at end of compilation, do output code for them. */
3612 DECL_DEFER_OUTPUT (decl) = 1;
3613 rest_of_decl_compilation (decl, true, 0);
3617 /* In conjunction with an ASMSPEC, the `register'
3618 keyword indicates that we should place the variable
3619 in a particular register. */
3620 if (asmspec && C_DECL_REGISTER (decl))
3622 DECL_HARD_REGISTER (decl) = 1;
3623 /* This cannot be done for a structure with volatile
3624 fields, on which DECL_REGISTER will have been
3626 if (!DECL_REGISTER (decl))
3627 error ("cannot put object with volatile field into register");
3630 if (TREE_CODE (decl) != FUNCTION_DECL)
3632 /* If we're building a variable sized type, and we might be
3633 reachable other than via the top of the current binding
3634 level, then create a new BIND_EXPR so that we deallocate
3635 the object at the right time. */
3636 /* Note that DECL_SIZE can be null due to errors. */
3637 if (DECL_SIZE (decl)
3638 && !TREE_CONSTANT (DECL_SIZE (decl))
3639 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3642 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3643 TREE_SIDE_EFFECTS (bind) = 1;
3645 BIND_EXPR_BODY (bind) = push_stmt_list ();
3647 add_stmt (build_stmt (DECL_EXPR, decl));
3652 if (!DECL_FILE_SCOPE_P (decl))
3654 /* Recompute the RTL of a local array now
3655 if it used to be an incomplete type. */
3657 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3659 /* If we used it already as memory, it must stay in memory. */
3660 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3661 /* If it's still incomplete now, no init will save it. */
3662 if (DECL_SIZE (decl) == 0)
3663 DECL_INITIAL (decl) = 0;
3668 /* If this was marked 'used', be sure it will be output. */
3669 if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3670 mark_decl_referenced (decl);
3672 if (TREE_CODE (decl) == TYPE_DECL)
3674 if (!DECL_FILE_SCOPE_P (decl)
3675 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3676 add_stmt (build_stmt (DECL_EXPR, decl));
3678 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3681 /* At the end of a declaration, throw away any variable type sizes
3682 of types defined inside that declaration. There is no use
3683 computing them in the following function definition. */
3684 if (current_scope == file_scope)
3685 get_pending_sizes ();
3687 /* Install a cleanup (aka destructor) if one was given. */
3688 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3690 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3693 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3694 tree cleanup_decl = lookup_name (cleanup_id);
3697 /* Build "cleanup(&decl)" for the destructor. */
3698 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3699 cleanup = build_tree_list (NULL_TREE, cleanup);
3700 cleanup = build_function_call (cleanup_decl, cleanup);
3702 /* Don't warn about decl unused; the cleanup uses it. */
3703 TREE_USED (decl) = 1;
3704 TREE_USED (cleanup_decl) = 1;
3706 /* Initialize EH, if we've been told to do so. */
3707 c_maybe_initialize_eh ();
3709 push_cleanup (decl, cleanup, false);
3714 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3717 grokparm (const struct c_parm *parm)
3719 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3722 decl_attributes (&decl, parm->attrs, 0);
3727 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3728 and push that on the current scope. */
3731 push_parm_decl (const struct c_parm *parm)
3735 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3736 decl_attributes (&decl, parm->attrs, 0);
3738 decl = pushdecl (decl);
3740 finish_decl (decl, NULL_TREE, NULL_TREE);
3743 /* Mark all the parameter declarations to date as forward decls.
3744 Also diagnose use of this extension. */
3747 mark_forward_parm_decls (void)
3749 struct c_binding *b;
3751 if (pedantic && !current_scope->warned_forward_parm_decls)
3753 pedwarn ("ISO C forbids forward parameter declarations");
3754 current_scope->warned_forward_parm_decls = true;
3757 for (b = current_scope->bindings; b; b = b->prev)
3758 if (TREE_CODE (b->decl) == PARM_DECL)
3759 TREE_ASM_WRITTEN (b->decl) = 1;
3762 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3763 literal, which may be an incomplete array type completed by the
3764 initializer; INIT is a CONSTRUCTOR that initializes the compound
3768 build_compound_literal (tree type, tree init)
3770 /* We do not use start_decl here because we have a type, not a declarator;
3771 and do not use finish_decl because the decl should be stored inside
3772 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3777 if (type == error_mark_node)
3778 return error_mark_node;
3780 decl = build_decl (VAR_DECL, NULL_TREE, type);
3781 DECL_EXTERNAL (decl) = 0;
3782 TREE_PUBLIC (decl) = 0;
3783 TREE_STATIC (decl) = (current_scope == file_scope);
3784 DECL_CONTEXT (decl) = current_function_decl;
3785 TREE_USED (decl) = 1;
3786 TREE_TYPE (decl) = type;
3787 TREE_READONLY (decl) = TYPE_READONLY (type);
3788 store_init_value (decl, init);
3790 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3792 int failure = complete_array_type (&TREE_TYPE (decl),
3793 DECL_INITIAL (decl), true);
3794 gcc_assert (!failure);
3796 type = TREE_TYPE (decl);
3797 TREE_TYPE (DECL_INITIAL (decl)) = type;
3800 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3801 return error_mark_node;
3803 stmt = build_stmt (DECL_EXPR, decl);
3804 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3805 TREE_SIDE_EFFECTS (complit) = 1;
3807 layout_decl (decl, 0);
3809 if (TREE_STATIC (decl))
3811 /* This decl needs a name for the assembler output. */
3812 set_compound_literal_name (decl);
3813 DECL_DEFER_OUTPUT (decl) = 1;
3814 DECL_COMDAT (decl) = 1;
3815 DECL_ARTIFICIAL (decl) = 1;
3816 DECL_IGNORED_P (decl) = 1;
3818 rest_of_decl_compilation (decl, 1, 0);
3824 /* Determine whether TYPE is a structure with a flexible array member,
3825 or a union containing such a structure (possibly recursively). */
3828 flexible_array_type_p (tree type)
3831 switch (TREE_CODE (type))
3834 x = TYPE_FIELDS (type);
3837 while (TREE_CHAIN (x) != NULL_TREE)
3839 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3840 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3841 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3842 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3846 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3848 if (flexible_array_type_p (TREE_TYPE (x)))
3857 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3858 replacing with appropriate values if they are invalid. */
3860 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3863 unsigned int max_width;
3864 unsigned HOST_WIDE_INT w;
3865 const char *name = orig_name ? orig_name: _("<anonymous>");
3867 /* Detect and ignore out of range field width and process valid
3869 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3870 || TREE_CODE (*width) != INTEGER_CST)
3872 error ("bit-field %qs width not an integer constant", name);
3873 *width = integer_one_node;
3877 constant_expression_warning (*width);
3878 if (tree_int_cst_sgn (*width) < 0)
3880 error ("negative width in bit-field %qs", name);
3881 *width = integer_one_node;
3883 else if (integer_zerop (*width) && orig_name)
3885 error ("zero width for bit-field %qs", name);
3886 *width = integer_one_node;
3890 /* Detect invalid bit-field type. */
3891 if (TREE_CODE (*type) != INTEGER_TYPE
3892 && TREE_CODE (*type) != BOOLEAN_TYPE
3893 && TREE_CODE (*type) != ENUMERAL_TYPE)
3895 error ("bit-field %qs has invalid type", name);
3896 *type = unsigned_type_node;
3899 type_mv = TYPE_MAIN_VARIANT (*type);
3901 && !in_system_header
3902 && type_mv != integer_type_node
3903 && type_mv != unsigned_type_node
3904 && type_mv != boolean_type_node)
3905 pedwarn ("type of bit-field %qs is a GCC extension", name);
3907 if (type_mv == boolean_type_node)
3908 max_width = CHAR_TYPE_SIZE;
3910 max_width = TYPE_PRECISION (*type);
3912 if (0 < compare_tree_int (*width, max_width))
3914 error ("width of %qs exceeds its type", name);
3916 *width = build_int_cst (NULL_TREE, w);
3919 w = tree_low_cst (*width, 1);
3921 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3923 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3925 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3926 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3927 warning (0, "%qs is narrower than values of its type", name);
3932 /* Given declspecs and a declarator,
3933 determine the name and type of the object declared
3934 and construct a ..._DECL node for it.
3935 (In one case we can return a ..._TYPE node instead.
3936 For invalid input we sometimes return 0.)
3938 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3940 DECL_CONTEXT says which syntactic context this declaration is in:
3941 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3942 FUNCDEF for a function definition. Like NORMAL but a few different
3943 error messages in each case. Return value may be zero meaning
3944 this definition is too screwy to try to parse.
3945 PARM for a parameter declaration (either within a function prototype
3946 or before a function body). Make a PARM_DECL, or return void_type_node.
3947 TYPENAME if for a typename (in a cast or sizeof).
3948 Don't make a DECL node; just return the ..._TYPE node.
3949 FIELD for a struct or union field; make a FIELD_DECL.
3950 INITIALIZED is true if the decl has an initializer.
3951 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3952 representing the width of the bit-field.
3954 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3955 It may also be so in the PARM case, for a prototype where the
3956 argument type is specified but not the name.
3958 This function is where the complicated C meanings of `static'
3959 and `extern' are interpreted. */
3962 grokdeclarator (const struct c_declarator *declarator,
3963 struct c_declspecs *declspecs,
3964 enum decl_context decl_context, bool initialized, tree *width)
3966 tree type = declspecs->type;
3967 bool threadp = declspecs->thread_p;
3968 enum c_storage_class storage_class = declspecs->storage_class;
3972 int type_quals = TYPE_UNQUALIFIED;
3973 const char *name, *orig_name;
3974 tree typedef_type = 0;
3975 bool funcdef_flag = false;
3976 bool funcdef_syntax = false;
3977 int size_varies = 0;
3978 tree decl_attr = declspecs->decl_attr;
3979 int array_ptr_quals = TYPE_UNQUALIFIED;
3980 tree array_ptr_attrs = NULL_TREE;
3981 int array_parm_static = 0;
3982 bool array_parm_vla_unspec_p = false;
3983 tree returned_attrs = NULL_TREE;
3984 bool bitfield = width != NULL;
3986 struct c_arg_info *arg_info = 0;
3988 if (decl_context == FUNCDEF)
3989 funcdef_flag = true, decl_context = NORMAL;
3991 /* Look inside a declarator for the name being declared
3992 and get it as a string, for an error message. */
3994 const struct c_declarator *decl = declarator;
4003 funcdef_syntax = (decl->kind == cdk_function);
4004 decl = decl->declarator;
4008 decl = decl->declarator;
4013 name = IDENTIFIER_POINTER (decl->u.id);
4025 /* A function definition's declarator must have the form of
4026 a function declarator. */
4028 if (funcdef_flag && !funcdef_syntax)
4031 /* If this looks like a function definition, make it one,
4032 even if it occurs where parms are expected.
4033 Then store_parm_decls will reject it and not use it as a parm. */
4034 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4035 decl_context = PARM;
4037 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4038 warn_deprecated_use (declspecs->type);
4040 if ((decl_context == NORMAL || decl_context == FIELD)
4041 && current_scope == file_scope
4042 && variably_modified_type_p (type, NULL_TREE))
4044 error ("variably modified %qs at file scope", name);
4045 type = integer_type_node;
4048 typedef_type = type;
4049 size_varies = C_TYPE_VARIABLE_SIZE (type);
4051 /* Diagnose defaulting to "int". */
4053 if (declspecs->default_int_p && !in_system_header)
4055 /* Issue a warning if this is an ISO C 99 program or if
4056 -Wreturn-type and this is a function, or if -Wimplicit;
4057 prefer the former warning since it is more explicit. */
4058 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4060 warn_about_return_type = 1;
4061 else if (warn_implicit_int || flag_isoc99)
4062 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
4065 /* Adjust the type if a bit-field is being declared,
4066 -funsigned-bitfields applied and the type is not explicitly
4068 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4069 && TREE_CODE (type) == INTEGER_TYPE)
4070 type = c_common_unsigned_type (type);
4072 /* Figure out the type qualifiers for the declaration. There are
4073 two ways a declaration can become qualified. One is something
4074 like `const int i' where the `const' is explicit. Another is
4075 something like `typedef const int CI; CI i' where the type of the
4076 declaration contains the `const'. A third possibility is that
4077 there is a type qualifier on the element type of a typedefed
4078 array type, in which case we should extract that qualifier so
4079 that c_apply_type_quals_to_decls receives the full list of
4080 qualifiers to work with (C90 is not entirely clear about whether
4081 duplicate qualifiers should be diagnosed in this case, but it
4082 seems most appropriate to do so). */
4083 element_type = strip_array_types (type);
4084 constp = declspecs->const_p + TYPE_READONLY (element_type);
4085 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4086 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4087 if (pedantic && !flag_isoc99)
4090 pedwarn ("duplicate %<const%>");
4092 pedwarn ("duplicate %<restrict%>");
4094 pedwarn ("duplicate %<volatile%>");
4096 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4097 type = TYPE_MAIN_VARIANT (type);
4098 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4099 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4100 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4102 /* Warn about storage classes that are invalid for certain
4103 kinds of declarations (parameters, typenames, etc.). */
4107 || storage_class == csc_auto
4108 || storage_class == csc_register
4109 || storage_class == csc_typedef))
4111 if (storage_class == csc_auto
4112 && (pedantic || current_scope == file_scope))
4113 pedwarn ("function definition declared %<auto%>");
4114 if (storage_class == csc_register)
4115 error ("function definition declared %<register%>");
4116 if (storage_class == csc_typedef)
4117 error ("function definition declared %<typedef%>");
4119 error ("function definition declared %<__thread%>");
4121 if (storage_class == csc_auto
4122 || storage_class == csc_register
4123 || storage_class == csc_typedef)
4124 storage_class = csc_none;
4126 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4128 if (decl_context == PARM && storage_class == csc_register)
4132 switch (decl_context)
4135 error ("storage class specified for structure field %qs",
4139 error ("storage class specified for parameter %qs", name);
4142 error ("storage class specified for typename");
4145 storage_class = csc_none;
4149 else if (storage_class == csc_extern
4153 /* 'extern' with initialization is invalid if not at file scope. */
4154 if (current_scope == file_scope)
4156 /* It is fine to have 'extern const' when compiling at C
4157 and C++ intersection. */
4158 if (!(warn_cxx_compat && constp))
4159 warning (0, "%qs initialized and declared %<extern%>", name);
4162 error ("%qs has both %<extern%> and initializer", name);
4164 else if (current_scope == file_scope)
4166 if (storage_class == csc_auto)
4167 error ("file-scope declaration of %qs specifies %<auto%>", name);
4168 if (pedantic && storage_class == csc_register)
4169 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
4173 if (storage_class == csc_extern && funcdef_flag)
4174 error ("nested function %qs declared %<extern%>", name);
4175 else if (threadp && storage_class == csc_none)
4177 error ("function-scope %qs implicitly auto and declared "
4184 /* Now figure out the structure of the declarator proper.
4185 Descend through it, creating more complex types, until we reach
4186 the declared identifier (or NULL_TREE, in an absolute declarator).
4187 At each stage we maintain an unqualified version of the type
4188 together with any qualifiers that should be applied to it with
4189 c_build_qualified_type; this way, array types including
4190 multidimensional array types are first built up in unqualified
4191 form and then the qualified form is created with
4192 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4194 while (declarator && declarator->kind != cdk_id)
4196 if (type == error_mark_node)
4198 declarator = declarator->declarator;
4202 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4203 a cdk_pointer (for *...),
4204 a cdk_function (for ...(...)),
4205 a cdk_attrs (for nested attributes),
4206 or a cdk_id (for the name being declared
4207 or the place in an absolute declarator
4208 where the name was omitted).
4209 For the last case, we have just exited the loop.
4211 At this point, TYPE is the type of elements of an array,
4212 or for a function to return, or for a pointer to point to.
4213 After this sequence of ifs, TYPE is the type of the
4214 array or function or pointer, and DECLARATOR has had its
4215 outermost layer removed. */
4217 if (array_ptr_quals != TYPE_UNQUALIFIED
4218 || array_ptr_attrs != NULL_TREE
4219 || array_parm_static)
4221 /* Only the innermost declarator (making a parameter be of
4222 array type which is converted to pointer type)
4223 may have static or type qualifiers. */
4224 error ("static or type qualifiers in non-parameter array declarator");
4225 array_ptr_quals = TYPE_UNQUALIFIED;
4226 array_ptr_attrs = NULL_TREE;
4227 array_parm_static = 0;
4230 switch (declarator->kind)
4234 /* A declarator with embedded attributes. */
4235 tree attrs = declarator->u.attrs;
4236 const struct c_declarator *inner_decl;
4238 declarator = declarator->declarator;
4239 inner_decl = declarator;
4240 while (inner_decl->kind == cdk_attrs)
4241 inner_decl = inner_decl->declarator;
4242 if (inner_decl->kind == cdk_id)
4243 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4244 else if (inner_decl->kind == cdk_function)
4245 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4246 else if (inner_decl->kind == cdk_array)
4247 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4248 returned_attrs = decl_attributes (&type,
4249 chainon (returned_attrs, attrs),
4255 tree itype = NULL_TREE;
4256 tree size = declarator->u.array.dimen;
4257 /* The index is a signed object `sizetype' bits wide. */
4258 tree index_type = c_common_signed_type (sizetype);
4260 array_ptr_quals = declarator->u.array.quals;
4261 array_ptr_attrs = declarator->u.array.attrs;
4262 array_parm_static = declarator->u.array.static_p;
4263 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4265 declarator = declarator->declarator;
4267 /* Check for some types that there cannot be arrays of. */
4269 if (VOID_TYPE_P (type))
4271 error ("declaration of %qs as array of voids", name);
4272 type = error_mark_node;
4275 if (TREE_CODE (type) == FUNCTION_TYPE)
4277 error ("declaration of %qs as array of functions", name);
4278 type = error_mark_node;
4281 if (pedantic && !in_system_header && flexible_array_type_p (type))
4282 pedwarn ("invalid use of structure with flexible array member");
4284 if (size == error_mark_node)
4285 type = error_mark_node;
4287 if (type == error_mark_node)
4290 /* If size was specified, set ITYPE to a range-type for
4291 that size. Otherwise, ITYPE remains null. finish_decl
4292 may figure it out from an initial value. */
4296 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4298 STRIP_TYPE_NOPS (size);
4300 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4302 error ("size of array %qs has non-integer type", name);
4303 size = integer_one_node;
4306 if (pedantic && integer_zerop (size))
4307 pedwarn ("ISO C forbids zero-size array %qs", name);
4309 if (TREE_CODE (size) == INTEGER_CST)
4311 constant_expression_warning (size);
4312 if (tree_int_cst_sgn (size) < 0)
4314 error ("size of array %qs is negative", name);
4315 size = integer_one_node;
4318 else if ((decl_context == NORMAL || decl_context == FIELD)
4319 && current_scope == file_scope)
4321 error ("variably modified %qs at file scope", name);
4322 size = integer_one_node;
4326 /* Make sure the array size remains visibly
4327 nonconstant even if it is (eg) a const variable
4328 with known value. */
4331 if (!flag_isoc99 && pedantic)
4333 if (TREE_CONSTANT (size))
4334 pedwarn ("ISO C90 forbids array %qs whose size "
4335 "can%'t be evaluated",
4338 pedwarn ("ISO C90 forbids variable-size array %qs",
4343 if (integer_zerop (size))
4345 /* A zero-length array cannot be represented with
4346 an unsigned index type, which is what we'll
4347 get with build_index_type. Create an
4348 open-ended range instead. */
4349 itype = build_range_type (sizetype, size, NULL_TREE);
4353 /* Arrange for the SAVE_EXPR on the inside of the
4354 MINUS_EXPR, which allows the -1 to get folded
4355 with the +1 that happens when building TYPE_SIZE. */
4357 size = variable_size (size);
4359 /* Compute the maximum valid index, that is, size
4360 - 1. Do the calculation in index_type, so that
4361 if it is a variable the computations will be
4362 done in the proper mode. */
4363 itype = fold_build2 (MINUS_EXPR, index_type,
4364 convert (index_type, size),
4365 convert (index_type,
4368 /* If that overflowed, the array is too big. ???
4369 While a size of INT_MAX+1 technically shouldn't
4370 cause an overflow (because we subtract 1), the
4371 overflow is recorded during the conversion to
4372 index_type, before the subtraction. Handling
4373 this case seems like an unnecessary
4375 if (TREE_CODE (itype) == INTEGER_CST
4376 && TREE_OVERFLOW (itype))
4378 error ("size of array %qs is too large", name);
4379 type = error_mark_node;
4383 itype = build_index_type (itype);
4386 else if (decl_context == FIELD)
4388 if (pedantic && !flag_isoc99 && !in_system_header)
4389 pedwarn ("ISO C90 does not support flexible array members");
4391 /* ISO C99 Flexible array members are effectively
4392 identical to GCC's zero-length array extension. */
4393 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4395 else if (decl_context == PARM)
4397 if (array_parm_vla_unspec_p)
4402 error ("%<[*]%> not allowed in other than a declaration");
4405 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4409 else if (decl_context == TYPENAME)
4411 if (array_parm_vla_unspec_p)
4413 /* The error is printed elsewhere. We use this to
4414 avoid messing up with incomplete array types of
4415 the same type, that would otherwise be modified
4417 itype = build_range_type (sizetype, size_zero_node,
4422 /* Complain about arrays of incomplete types. */
4423 if (!COMPLETE_TYPE_P (type))
4425 error ("array type has incomplete element type");
4426 type = error_mark_node;
4429 /* When itype is NULL, a shared incomplete array type is
4430 returned for all array of a given type. Elsewhere we
4431 make sure we don't complete that type before copying
4432 it, but here we want to make sure we don't ever
4433 modify the shared type, so we gcc_assert (itype)
4435 type = build_array_type (type, itype);
4437 if (type != error_mark_node)
4441 /* It is ok to modify type here even if itype is
4442 NULL: if size_varies, we're in a
4443 multi-dimensional array and the inner type has
4444 variable size, so the enclosing shared array type
4446 if (size && TREE_CODE (size) == INTEGER_CST)
4448 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4449 C_TYPE_VARIABLE_SIZE (type) = 1;
4452 /* The GCC extension for zero-length arrays differs from
4453 ISO flexible array members in that sizeof yields
4455 if (size && integer_zerop (size))
4458 TYPE_SIZE (type) = bitsize_zero_node;
4459 TYPE_SIZE_UNIT (type) = size_zero_node;
4461 if (array_parm_vla_unspec_p)
4464 /* The type is complete. C99 6.7.5.2p4 */
4465 TYPE_SIZE (type) = bitsize_zero_node;
4466 TYPE_SIZE_UNIT (type) = size_zero_node;
4470 if (decl_context != PARM
4471 && (array_ptr_quals != TYPE_UNQUALIFIED
4472 || array_ptr_attrs != NULL_TREE
4473 || array_parm_static))
4475 error ("static or type qualifiers in non-parameter array declarator");
4476 array_ptr_quals = TYPE_UNQUALIFIED;
4477 array_ptr_attrs = NULL_TREE;
4478 array_parm_static = 0;
4484 /* Say it's a definition only for the declarator closest
4485 to the identifier, apart possibly from some
4487 bool really_funcdef = false;
4491 const struct c_declarator *t = declarator->declarator;
4492 while (t->kind == cdk_attrs)
4494 really_funcdef = (t->kind == cdk_id);
4497 /* Declaring a function type. Make sure we have a valid
4498 type for the function to return. */
4499 if (type == error_mark_node)
4504 /* Warn about some types functions can't return. */
4505 if (TREE_CODE (type) == FUNCTION_TYPE)
4507 error ("%qs declared as function returning a function", name);
4508 type = integer_type_node;
4510 if (TREE_CODE (type) == ARRAY_TYPE)
4512 error ("%qs declared as function returning an array", name);
4513 type = integer_type_node;
4516 /* Construct the function type and go to the next
4517 inner layer of declarator. */
4518 arg_info = declarator->u.arg_info;
4519 arg_types = grokparms (arg_info, really_funcdef);
4521 put_pending_sizes (arg_info->pending_sizes);
4523 /* Type qualifiers before the return type of the function
4524 qualify the return type, not the function type. */
4527 /* Type qualifiers on a function return type are
4528 normally permitted by the standard but have no
4529 effect, so give a warning at -Wreturn-type.
4530 Qualifiers on a void return type are banned on
4531 function definitions in ISO C; GCC used to used
4532 them for noreturn functions. */
4533 if (VOID_TYPE_P (type) && really_funcdef)
4534 pedwarn ("function definition has qualified void return type");
4536 warning (OPT_Wreturn_type,
4537 "type qualifiers ignored on function return type");
4539 type = c_build_qualified_type (type, type_quals);
4541 type_quals = TYPE_UNQUALIFIED;
4543 type = build_function_type (type, arg_types);
4544 declarator = declarator->declarator;
4546 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4547 the formal parameter list of this FUNCTION_TYPE to point to
4548 the FUNCTION_TYPE node itself. */
4552 for (link = arg_info->tags;
4554 link = TREE_CHAIN (link))
4555 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4561 /* Merge any constancy or volatility into the target type
4564 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4566 pedwarn ("ISO C forbids qualified function types");
4568 type = c_build_qualified_type (type, type_quals);
4571 /* When the pointed-to type involves components of variable size,
4572 care must be taken to ensure that the size evaluation code is
4573 emitted early enough to dominate all the possible later uses
4574 and late enough for the variables on which it depends to have
4577 This is expected to happen automatically when the pointed-to
4578 type has a name/declaration of it's own, but special attention
4579 is required if the type is anonymous.
4581 We handle the NORMAL and FIELD contexts here by attaching an
4582 artificial TYPE_DECL to such pointed-to type. This forces the
4583 sizes evaluation at a safe point and ensures it is not deferred
4584 until e.g. within a deeper conditional context.
4586 We expect nothing to be needed here for PARM or TYPENAME.
4587 Pushing a TYPE_DECL at this point for TYPENAME would actually
4588 be incorrect, as we might be in the middle of an expression
4589 with side effects on the pointed-to type size "arguments" prior
4590 to the pointer declaration point and the fake TYPE_DECL in the
4591 enclosing context would force the size evaluation prior to the
4594 if (!TYPE_NAME (type)
4595 && (decl_context == NORMAL || decl_context == FIELD)
4596 && variably_modified_type_p (type, NULL_TREE))
4598 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4599 DECL_ARTIFICIAL (decl) = 1;
4601 finish_decl (decl, NULL_TREE, NULL_TREE);
4602 TYPE_NAME (type) = decl;
4605 type = build_pointer_type (type);
4607 /* Process type qualifiers (such as const or volatile)
4608 that were given inside the `*'. */
4609 type_quals = declarator->u.pointer_quals;
4611 declarator = declarator->declarator;
4619 /* Now TYPE has the actual type, apart from any qualifiers in
4622 /* Check the type and width of a bit-field. */
4624 check_bitfield_type_and_width (&type, width, orig_name);
4626 /* Did array size calculations overflow? */
4628 if (TREE_CODE (type) == ARRAY_TYPE
4629 && COMPLETE_TYPE_P (type)
4630 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4631 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4633 error ("size of array %qs is too large", name);
4634 /* If we proceed with the array type as it is, we'll eventually
4635 crash in tree_low_cst(). */
4636 type = error_mark_node;
4639 /* If this is declaring a typedef name, return a TYPE_DECL. */
4641 if (storage_class == csc_typedef)
4644 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4646 pedwarn ("ISO C forbids qualified function types");
4648 type = c_build_qualified_type (type, type_quals);
4649 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4650 if (declspecs->explicit_signed_p)
4651 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4652 decl_attributes (&decl, returned_attrs, 0);
4653 if (declspecs->inline_p)
4654 pedwarn ("typedef %q+D declared %<inline%>", decl);
4658 /* If this is a type name (such as, in a cast or sizeof),
4659 compute the type and return it now. */
4661 if (decl_context == TYPENAME)
4663 /* Note that the grammar rejects storage classes in typenames
4665 gcc_assert (storage_class == csc_none && !threadp
4666 && !declspecs->inline_p);
4667 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4669 pedwarn ("ISO C forbids const or volatile function types");
4671 type = c_build_qualified_type (type, type_quals);
4672 decl_attributes (&type, returned_attrs, 0);
4676 if (pedantic && decl_context == FIELD
4677 && variably_modified_type_p (type, NULL_TREE))
4680 pedwarn ("a member of a structure or union cannot have a variably modified type");
4683 /* Aside from typedefs and type names (handle above),
4684 `void' at top level (not within pointer)
4685 is allowed only in public variables.
4686 We don't complain about parms either, but that is because
4687 a better error message can be made later. */
4689 if (VOID_TYPE_P (type) && decl_context != PARM
4690 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4691 && (storage_class == csc_extern
4692 || (current_scope == file_scope
4693 && !(storage_class == csc_static
4694 || storage_class == csc_register)))))
4696 error ("variable or field %qs declared void", name);
4697 type = integer_type_node;
4700 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4701 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4706 if (decl_context == PARM)
4708 tree type_as_written;
4711 /* A parameter declared as an array of T is really a pointer to T.
4712 One declared as a function is really a pointer to a function. */
4714 if (TREE_CODE (type) == ARRAY_TYPE)
4716 /* Transfer const-ness of array into that of type pointed to. */
4717 type = TREE_TYPE (type);
4719 type = c_build_qualified_type (type, type_quals);
4720 type = build_pointer_type (type);
4721 type_quals = array_ptr_quals;
4723 /* We don't yet implement attributes in this context. */
4724 if (array_ptr_attrs != NULL_TREE)
4725 warning (OPT_Wattributes,
4726 "attributes in parameter array declarator ignored");
4730 else if (TREE_CODE (type) == FUNCTION_TYPE)
4732 if (pedantic && type_quals)
4733 pedwarn ("ISO C forbids qualified function types");
4735 type = c_build_qualified_type (type, type_quals);
4736 type = build_pointer_type (type);
4737 type_quals = TYPE_UNQUALIFIED;
4739 else if (type_quals)
4740 type = c_build_qualified_type (type, type_quals);
4742 type_as_written = type;
4744 decl = build_decl (PARM_DECL, declarator->u.id, type);
4746 C_DECL_VARIABLE_SIZE (decl) = 1;
4748 /* Compute the type actually passed in the parmlist,
4749 for the case where there is no prototype.
4750 (For example, shorts and chars are passed as ints.)
4751 When there is a prototype, this is overridden later. */
4753 if (type == error_mark_node)
4754 promoted_type = type;
4756 promoted_type = c_type_promotes_to (type);
4758 DECL_ARG_TYPE (decl) = promoted_type;
4759 if (declspecs->inline_p)
4760 pedwarn ("parameter %q+D declared %<inline%>", decl);
4762 else if (decl_context == FIELD)
4764 /* Note that the grammar rejects storage classes in typenames
4766 gcc_assert (storage_class == csc_none && !threadp
4767 && !declspecs->inline_p);
4769 /* Structure field. It may not be a function. */
4771 if (TREE_CODE (type) == FUNCTION_TYPE)
4773 error ("field %qs declared as a function", name);
4774 type = build_pointer_type (type);
4776 else if (TREE_CODE (type) != ERROR_MARK
4777 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4779 error ("field %qs has incomplete type", name);
4780 type = error_mark_node;
4782 type = c_build_qualified_type (type, type_quals);
4783 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4784 DECL_NONADDRESSABLE_P (decl) = bitfield;
4787 C_DECL_VARIABLE_SIZE (decl) = 1;
4789 else if (TREE_CODE (type) == FUNCTION_TYPE)
4791 if (storage_class == csc_register || threadp)
4793 error ("invalid storage class for function %qs", name);
4795 else if (current_scope != file_scope)
4797 /* Function declaration not at file scope. Storage
4798 classes other than `extern' are not allowed, C99
4799 6.7.1p5, and `extern' makes no difference. However,
4800 GCC allows 'auto', perhaps with 'inline', to support
4801 nested functions. */
4802 if (storage_class == csc_auto)
4805 pedwarn ("invalid storage class for function %qs", name);
4807 else if (storage_class == csc_static)
4809 error ("invalid storage class for function %qs", name);
4811 storage_class = declspecs->storage_class = csc_none;
4817 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4818 decl = build_decl_attribute_variant (decl, decl_attr);
4820 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4822 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4823 pedwarn ("ISO C forbids qualified function types");
4825 /* GNU C interprets a volatile-qualified function type to indicate
4826 that the function does not return. */
4827 if ((type_quals & TYPE_QUAL_VOLATILE)
4828 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4829 warning (0, "%<noreturn%> function returns non-void value");
4831 /* Every function declaration is an external reference
4832 (DECL_EXTERNAL) except for those which are not at file
4833 scope and are explicitly declared "auto". This is
4834 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4835 GCC to signify a forward declaration of a nested function. */
4836 if (storage_class == csc_auto && current_scope != file_scope)
4837 DECL_EXTERNAL (decl) = 0;
4838 /* In C99, a function which is declared 'inline' with 'extern'
4839 is not an external reference (which is confusing). It
4840 means that the later definition of the function must be output
4841 in this file, C99 6.7.4p6. In GNU C89, a function declared
4842 'extern inline' is an external reference. */
4843 else if (declspecs->inline_p && storage_class != csc_static)
4844 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
4845 == flag_gnu89_inline);
4847 DECL_EXTERNAL (decl) = !initialized;
4849 /* Record absence of global scope for `static' or `auto'. */
4851 = !(storage_class == csc_static || storage_class == csc_auto);
4853 /* For a function definition, record the argument information
4854 block where store_parm_decls will look for it. */
4856 current_function_arg_info = arg_info;
4858 if (declspecs->default_int_p)
4859 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4861 /* Record presence of `inline', if it is reasonable. */
4862 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4864 if (declspecs->inline_p)
4865 pedwarn ("cannot inline function %<main%>");
4867 else if (declspecs->inline_p)
4869 /* Record that the function is declared `inline'. */
4870 DECL_DECLARED_INLINE_P (decl) = 1;
4872 /* Do not mark bare declarations as DECL_INLINE. Doing so
4873 in the presence of multiple declarations can result in
4874 the abstract origin pointing between the declarations,
4875 which will confuse dwarf2out. */
4877 DECL_INLINE (decl) = 1;
4879 /* If -finline-functions, assume it can be inlined. This does
4880 two things: let the function be deferred until it is actually
4881 needed, and let dwarf2 know that the function is inlinable. */
4882 else if (flag_inline_trees == 2 && initialized)
4883 DECL_INLINE (decl) = 1;
4887 /* It's a variable. */
4888 /* An uninitialized decl with `extern' is a reference. */
4889 int extern_ref = !initialized && storage_class == csc_extern;
4891 type = c_build_qualified_type (type, type_quals);
4893 /* C99 6.2.2p7: It is invalid (compile-time undefined
4894 behavior) to create an 'extern' declaration for a
4895 variable if there is a global declaration that is
4896 'static' and the global declaration is not visible.
4897 (If the static declaration _is_ currently visible,
4898 the 'extern' declaration is taken to refer to that decl.) */
4899 if (extern_ref && current_scope != file_scope)
4901 tree global_decl = identifier_global_value (declarator->u.id);
4902 tree visible_decl = lookup_name (declarator->u.id);
4905 && global_decl != visible_decl
4906 && TREE_CODE (global_decl) == VAR_DECL
4907 && !TREE_PUBLIC (global_decl))
4908 error ("variable previously declared %<static%> redeclared "
4912 decl = build_decl (VAR_DECL, declarator->u.id, type);
4913 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4915 C_DECL_VARIABLE_SIZE (decl) = 1;
4917 if (declspecs->inline_p)
4918 pedwarn ("variable %q+D declared %<inline%>", decl);
4920 /* At file scope, an initialized extern declaration may follow
4921 a static declaration. In that case, DECL_EXTERNAL will be
4922 reset later in start_decl. */
4923 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4925 /* At file scope, the presence of a `static' or `register' storage
4926 class specifier, or the absence of all storage class specifiers
4927 makes this declaration a definition (perhaps tentative). Also,
4928 the absence of `static' makes it public. */
4929 if (current_scope == file_scope)
4931 TREE_PUBLIC (decl) = storage_class != csc_static;
4932 TREE_STATIC (decl) = !extern_ref;
4934 /* Not at file scope, only `static' makes a static definition. */
4937 TREE_STATIC (decl) = (storage_class == csc_static);
4938 TREE_PUBLIC (decl) = extern_ref;
4943 if (targetm.have_tls)
4944 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
4946 /* A mere warning is sure to result in improper semantics
4947 at runtime. Don't bother to allow this to compile. */
4948 error ("thread-local storage not supported for this target");
4952 if (storage_class == csc_extern
4953 && variably_modified_type_p (type, NULL_TREE))
4956 error ("object with variably modified type must have no linkage");
4959 /* Record `register' declaration for warnings on &
4960 and in case doing stupid register allocation. */
4962 if (storage_class == csc_register)
4964 C_DECL_REGISTER (decl) = 1;
4965 DECL_REGISTER (decl) = 1;
4968 /* Record constancy and volatility. */
4969 c_apply_type_quals_to_decl (type_quals, decl);
4971 /* If a type has volatile components, it should be stored in memory.
4972 Otherwise, the fact that those components are volatile
4973 will be ignored, and would even crash the compiler.
4974 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
4975 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
4976 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
4977 || TREE_CODE (decl) == RESULT_DECL))
4979 /* It is not an error for a structure with volatile fields to
4980 be declared register, but reset DECL_REGISTER since it
4981 cannot actually go in a register. */
4982 int was_reg = C_DECL_REGISTER (decl);
4983 C_DECL_REGISTER (decl) = 0;
4984 DECL_REGISTER (decl) = 0;
4985 c_mark_addressable (decl);
4986 C_DECL_REGISTER (decl) = was_reg;
4989 /* This is the earliest point at which we might know the assembler
4990 name of a variable. Thus, if it's known before this, die horribly. */
4991 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4993 decl_attributes (&decl, returned_attrs, 0);
4999 /* Decode the parameter-list info for a function type or function definition.
5000 The argument is the value returned by `get_parm_info' (or made in c-parse.c
5001 if there is an identifier list instead of a parameter decl list).
5002 These two functions are separate because when a function returns
5003 or receives functions then each is called multiple times but the order
5004 of calls is different. The last call to `grokparms' is always the one
5005 that contains the formal parameter names of a function definition.
5007 Return a list of arg types to use in the FUNCTION_TYPE for this function.
5009 FUNCDEF_FLAG is true for a function definition, false for
5010 a mere declaration. A nonempty identifier-list gets an error message
5011 when FUNCDEF_FLAG is false. */
5014 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
5016 tree arg_types = arg_info->types;
5018 if (funcdef_flag && arg_info->had_vla_unspec)
5020 /* A function definition isn't function prototype scope C99 6.2.1p4. */
5022 error ("%<[*]%> not allowed in other than function prototype scope");
5025 if (arg_types == 0 && !funcdef_flag && !in_system_header)
5026 warning (OPT_Wstrict_prototypes,
5027 "function declaration isn%'t a prototype");
5029 if (arg_types == error_mark_node)
5030 return 0; /* don't set TYPE_ARG_TYPES in this case */
5032 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5035 pedwarn ("parameter names (without types) in function declaration");
5037 arg_info->parms = arg_info->types;
5038 arg_info->types = 0;
5043 tree parm, type, typelt;
5044 unsigned int parmno;
5046 /* If there is a parameter of incomplete type in a definition,
5047 this is an error. In a declaration this is valid, and a
5048 struct or union type may be completed later, before any calls
5049 or definition of the function. In the case where the tag was
5050 first declared within the parameter list, a warning has
5051 already been given. If a parameter has void type, then
5052 however the function cannot be defined or called, so
5055 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5057 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5059 type = TREE_VALUE (typelt);
5060 if (type == error_mark_node)
5063 if (!COMPLETE_TYPE_P (type))
5067 if (DECL_NAME (parm))
5068 error ("parameter %u (%q+D) has incomplete type",
5071 error ("%Jparameter %u has incomplete type",
5074 TREE_VALUE (typelt) = error_mark_node;
5075 TREE_TYPE (parm) = error_mark_node;
5077 else if (VOID_TYPE_P (type))
5079 if (DECL_NAME (parm))
5080 warning (0, "parameter %u (%q+D) has void type",
5083 warning (0, "%Jparameter %u has void type",
5088 if (DECL_NAME (parm) && TREE_USED (parm))
5089 warn_if_shadowing (parm);
5095 /* Take apart the current scope and return a c_arg_info structure with
5096 info on a parameter list just parsed.
5098 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5100 ELLIPSIS being true means the argument list ended in '...' so don't
5101 append a sentinel (void_list_node) to the end of the type-list. */
5104 get_parm_info (bool ellipsis)
5106 struct c_binding *b = current_scope->bindings;
5107 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5114 static bool explained_incomplete_types = false;
5115 bool gave_void_only_once_err = false;
5117 arg_info->parms = 0;
5119 arg_info->types = 0;
5120 arg_info->others = 0;
5121 arg_info->pending_sizes = 0;
5122 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5124 /* The bindings in this scope must not get put into a block.
5125 We will take care of deleting the binding nodes. */
5126 current_scope->bindings = 0;
5128 /* This function is only called if there was *something* on the
5132 /* A parameter list consisting solely of 'void' indicates that the
5133 function takes no arguments. But if the 'void' is qualified
5134 (by 'const' or 'volatile'), or has a storage class specifier
5135 ('register'), then the behavior is undefined; issue an error.
5136 Typedefs for 'void' are OK (see DR#157). */
5137 if (b->prev == 0 /* one binding */
5138 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5139 && !DECL_NAME (b->decl) /* anonymous */
5140 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5142 if (TREE_THIS_VOLATILE (b->decl)
5143 || TREE_READONLY (b->decl)
5144 || C_DECL_REGISTER (b->decl))
5145 error ("%<void%> as only parameter may not be qualified");
5147 /* There cannot be an ellipsis. */
5149 error ("%<void%> must be the only parameter");
5151 arg_info->types = void_list_node;
5156 types = void_list_node;
5158 /* Break up the bindings list into parms, tags, types, and others;
5159 apply sanity checks; purge the name-to-decl bindings. */
5162 tree decl = b->decl;
5163 tree type = TREE_TYPE (decl);
5164 const char *keyword;
5166 switch (TREE_CODE (decl))
5171 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5172 I_SYMBOL_BINDING (b->id) = b->shadowed;
5175 /* Check for forward decls that never got their actual decl. */
5176 if (TREE_ASM_WRITTEN (decl))
5177 error ("parameter %q+D has just a forward declaration", decl);
5178 /* Check for (..., void, ...) and issue an error. */
5179 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5181 if (!gave_void_only_once_err)
5183 error ("%<void%> must be the only parameter");
5184 gave_void_only_once_err = true;
5189 /* Valid parameter, add it to the list. */
5190 TREE_CHAIN (decl) = parms;
5193 /* Since there is a prototype, args are passed in their
5194 declared types. The back end may override this later. */
5195 DECL_ARG_TYPE (decl) = type;
5196 types = tree_cons (0, type, types);
5200 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5201 case UNION_TYPE: keyword = "union"; goto tag;
5202 case RECORD_TYPE: keyword = "struct"; goto tag;
5204 /* Types may not have tag-names, in which case the type
5205 appears in the bindings list with b->id NULL. */
5208 gcc_assert (I_TAG_BINDING (b->id) == b);
5209 I_TAG_BINDING (b->id) = b->shadowed;
5212 /* Warn about any struct, union or enum tags defined in a
5213 parameter list. The scope of such types is limited to
5214 the parameter list, which is rarely if ever desirable
5215 (it's impossible to call such a function with type-
5216 correct arguments). An anonymous union parm type is
5217 meaningful as a GNU extension, so don't warn for that. */
5218 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5221 /* The %s will be one of 'struct', 'union', or 'enum'. */
5222 warning (0, "%<%s %E%> declared inside parameter list",
5225 /* The %s will be one of 'struct', 'union', or 'enum'. */
5226 warning (0, "anonymous %s declared inside parameter list",
5229 if (!explained_incomplete_types)
5231 warning (0, "its scope is only this definition or declaration,"
5232 " which is probably not what you want");
5233 explained_incomplete_types = true;
5237 tags = tree_cons (b->id, decl, tags);
5243 /* CONST_DECLs appear here when we have an embedded enum,
5244 and TYPE_DECLs appear here when we have an embedded struct
5245 or union. No warnings for this - we already warned about the
5246 type itself. FUNCTION_DECLs appear when there is an implicit
5247 function declaration in the parameter list. */
5249 TREE_CHAIN (decl) = others;
5254 /* error_mark_node appears here when we have an undeclared
5255 variable. Just throw it away. */
5258 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5259 I_SYMBOL_BINDING (b->id) = b->shadowed;
5263 /* Other things that might be encountered. */
5270 b = free_binding_and_advance (b);
5273 arg_info->parms = parms;
5274 arg_info->tags = tags;
5275 arg_info->types = types;
5276 arg_info->others = others;
5277 arg_info->pending_sizes = get_pending_sizes ();
5281 /* Get the struct, enum or union (CODE says which) with tag NAME.
5282 Define the tag as a forward-reference if it is not defined.
5283 Return a c_typespec structure for the type specifier. */
5286 parser_xref_tag (enum tree_code code, tree name)
5288 struct c_typespec ret;
5289 /* If a cross reference is requested, look up the type
5290 already defined for this tag and return it. */
5292 tree ref = lookup_tag (code, name, 0);
5293 /* If this is the right type of tag, return what we found.
5294 (This reference will be shadowed by shadow_tag later if appropriate.)
5295 If this is the wrong type of tag, do not return it. If it was the
5296 wrong type in the same scope, we will have had an error
5297 message already; if in a different scope and declaring
5298 a name, pending_xref_error will give an error message; but if in a
5299 different scope and not declaring a name, this tag should
5300 shadow the previous declaration of a different type of tag, and
5301 this would not work properly if we return the reference found.
5302 (For example, with "struct foo" in an outer scope, "union foo;"
5303 must shadow that tag with a new one of union type.) */
5304 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5305 if (ref && TREE_CODE (ref) == code)
5311 /* If no such tag is yet defined, create a forward-reference node
5312 and record it as the "definition".
5313 When a real declaration of this type is found,
5314 the forward-reference will be altered into a real type. */
5316 ref = make_node (code);
5317 if (code == ENUMERAL_TYPE)
5319 /* Give the type a default layout like unsigned int
5320 to avoid crashing if it does not get defined. */
5321 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5322 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5323 TYPE_USER_ALIGN (ref) = 0;
5324 TYPE_UNSIGNED (ref) = 1;
5325 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5326 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5327 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5330 pushtag (name, ref);
5336 /* Get the struct, enum or union (CODE says which) with tag NAME.
5337 Define the tag as a forward-reference if it is not defined.
5338 Return a tree for the type. */
5341 xref_tag (enum tree_code code, tree name)
5343 return parser_xref_tag (code, name).spec;
5346 /* Make sure that the tag NAME is defined *in the current scope*
5347 at least as a forward reference.
5348 CODE says which kind of tag NAME ought to be. */
5351 start_struct (enum tree_code code, tree name)
5353 /* If there is already a tag defined at this scope
5354 (as a forward reference), just return it. */
5359 ref = lookup_tag (code, name, 1);
5360 if (ref && TREE_CODE (ref) == code)
5362 if (TYPE_SIZE (ref))
5364 if (code == UNION_TYPE)
5365 error ("redefinition of %<union %E%>", name);
5367 error ("redefinition of %<struct %E%>", name);
5369 else if (C_TYPE_BEING_DEFINED (ref))
5371 if (code == UNION_TYPE)
5372 error ("nested redefinition of %<union %E%>", name);
5374 error ("nested redefinition of %<struct %E%>", name);
5375 /* Don't create structures that contain themselves. */
5380 /* Otherwise create a forward-reference just so the tag is in scope. */
5382 if (ref == NULL_TREE || TREE_CODE (ref) != code)
5384 ref = make_node (code);
5385 pushtag (name, ref);
5388 C_TYPE_BEING_DEFINED (ref) = 1;
5389 TYPE_PACKED (ref) = flag_pack_struct;
5393 /* Process the specs, declarator and width (NULL if omitted)
5394 of a structure component, returning a FIELD_DECL node.
5395 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5397 This is done during the parsing of the struct declaration.
5398 The FIELD_DECL nodes are chained together and the lot of them
5399 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5402 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5407 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5408 && width == NULL_TREE)
5410 /* This is an unnamed decl.
5412 If we have something of the form "union { list } ;" then this
5413 is the anonymous union extension. Similarly for struct.
5415 If this is something of the form "struct foo;", then
5416 If MS extensions are enabled, this is handled as an
5418 Otherwise this is a forward declaration of a structure tag.
5420 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5421 If MS extensions are enabled and foo names a structure, then
5422 again this is an anonymous struct.
5423 Otherwise this is an error.
5425 Oh what a horrid tangled web we weave. I wonder if MS consciously
5426 took this from Plan 9 or if it was an accident of implementation
5427 that took root before someone noticed the bug... */
5429 tree type = declspecs->type;
5430 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5431 || TREE_CODE (type) == UNION_TYPE);
5435 && (flag_ms_extensions || !declspecs->typedef_p))
5437 if (flag_ms_extensions)
5441 else if (TYPE_NAME (type) == NULL)
5448 pedwarn ("declaration does not declare anything");
5452 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5455 value = grokdeclarator (declarator, declspecs, FIELD, false,
5456 width ? &width : NULL);
5458 finish_decl (value, NULL_TREE, NULL_TREE);
5459 DECL_INITIAL (value) = width;
5464 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5465 the list such that this does not present a problem later. */
5468 detect_field_duplicates (tree fieldlist)
5473 /* First, see if there are more than "a few" fields.
5474 This is trivially true if there are zero or one fields. */
5477 x = TREE_CHAIN (fieldlist);
5483 } while (timeout > 0 && x);
5485 /* If there were "few" fields, avoid the overhead of allocating
5486 a hash table. Instead just do the nested traversal thing. */
5489 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5492 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5493 if (DECL_NAME (y) == DECL_NAME (x))
5495 error ("duplicate member %q+D", x);
5496 DECL_NAME (x) = NULL_TREE;
5502 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5505 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5506 if ((y = DECL_NAME (x)) != 0)
5508 slot = htab_find_slot (htab, y, INSERT);
5511 error ("duplicate member %q+D", x);
5512 DECL_NAME (x) = NULL_TREE;
5521 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5522 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5523 ATTRIBUTES are attributes to be applied to the structure. */
5526 finish_struct (tree t, tree fieldlist, tree attributes)
5529 bool toplevel = file_scope == current_scope;
5530 int saw_named_field;
5532 /* If this type was previously laid out as a forward reference,
5533 make sure we lay it out again. */
5537 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5541 for (x = fieldlist; x; x = TREE_CHAIN (x))
5542 if (DECL_NAME (x) != 0)
5547 if (TREE_CODE (t) == UNION_TYPE)
5550 pedwarn ("union has no named members");
5552 pedwarn ("union has no members");
5557 pedwarn ("struct has no named members");
5559 pedwarn ("struct has no members");
5564 /* Install struct as DECL_CONTEXT of each field decl.
5565 Also process specified field sizes, found in the DECL_INITIAL,
5566 storing 0 there after the type has been changed to precision equal
5567 to its width, rather than the precision of the specified standard
5568 type. (Correct layout requires the original type to have been preserved
5571 saw_named_field = 0;
5572 for (x = fieldlist; x; x = TREE_CHAIN (x))
5574 if (TREE_TYPE (x) == error_mark_node)
5577 DECL_CONTEXT (x) = t;
5579 if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
5580 DECL_PACKED (x) = 1;
5582 /* If any field is const, the structure type is pseudo-const. */
5583 if (TREE_READONLY (x))
5584 C_TYPE_FIELDS_READONLY (t) = 1;
5587 /* A field that is pseudo-const makes the structure likewise. */
5588 tree t1 = TREE_TYPE (x);
5589 while (TREE_CODE (t1) == ARRAY_TYPE)
5590 t1 = TREE_TYPE (t1);
5591 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5592 && C_TYPE_FIELDS_READONLY (t1))
5593 C_TYPE_FIELDS_READONLY (t) = 1;
5596 /* Any field that is volatile means variables of this type must be
5597 treated in some ways as volatile. */
5598 if (TREE_THIS_VOLATILE (x))
5599 C_TYPE_FIELDS_VOLATILE (t) = 1;
5601 /* Any field of nominal variable size implies structure is too. */
5602 if (C_DECL_VARIABLE_SIZE (x))
5603 C_TYPE_VARIABLE_SIZE (t) = 1;
5605 if (DECL_INITIAL (x))
5607 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5608 DECL_SIZE (x) = bitsize_int (width);
5609 DECL_BIT_FIELD (x) = 1;
5610 SET_DECL_C_BIT_FIELD (x);
5613 /* Detect flexible array member in an invalid context. */
5614 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5615 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5616 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5617 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5619 if (TREE_CODE (t) == UNION_TYPE)
5621 error ("%Jflexible array member in union", x);
5622 TREE_TYPE (x) = error_mark_node;
5624 else if (TREE_CHAIN (x) != NULL_TREE)
5626 error ("%Jflexible array member not at end of struct", x);
5627 TREE_TYPE (x) = error_mark_node;
5629 else if (!saw_named_field)
5631 error ("%Jflexible array member in otherwise empty struct", x);
5632 TREE_TYPE (x) = error_mark_node;
5636 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5637 && flexible_array_type_p (TREE_TYPE (x)))
5638 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5641 saw_named_field = 1;
5644 detect_field_duplicates (fieldlist);
5646 /* Now we have the nearly final fieldlist. Record it,
5647 then lay out the structure or union (including the fields). */
5649 TYPE_FIELDS (t) = fieldlist;
5653 /* Give bit-fields their proper types. */
5655 tree *fieldlistp = &fieldlist;
5657 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5658 && TREE_TYPE (*fieldlistp) != error_mark_node)
5660 unsigned HOST_WIDE_INT width
5661 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5662 tree type = TREE_TYPE (*fieldlistp);
5663 if (width != TYPE_PRECISION (type))
5665 TREE_TYPE (*fieldlistp)
5666 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5667 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5669 DECL_INITIAL (*fieldlistp) = 0;
5672 fieldlistp = &TREE_CHAIN (*fieldlistp);
5675 /* Now we have the truly final field list.
5676 Store it in this type and in the variants. */
5678 TYPE_FIELDS (t) = fieldlist;
5680 /* If there are lots of fields, sort so we can look through them fast.
5681 We arbitrarily consider 16 or more elts to be "a lot". */
5686 for (x = fieldlist; x; x = TREE_CHAIN (x))
5688 if (len > 15 || DECL_NAME (x) == NULL)
5696 struct lang_type *space;
5697 struct sorted_fields_type *space2;
5699 len += list_length (x);
5701 /* Use the same allocation policy here that make_node uses, to
5702 ensure that this lives as long as the rest of the struct decl.
5703 All decls in an inline function need to be saved. */
5705 space = GGC_CNEW (struct lang_type);
5706 space2 = GGC_NEWVAR (struct sorted_fields_type,
5707 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5711 field_array = &space2->elts[0];
5712 for (x = fieldlist; x; x = TREE_CHAIN (x))
5714 field_array[len++] = x;
5716 /* If there is anonymous struct or union, break out of the loop. */
5717 if (DECL_NAME (x) == NULL)
5720 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5723 TYPE_LANG_SPECIFIC (t) = space;
5724 TYPE_LANG_SPECIFIC (t)->s->len = len;
5725 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5726 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5731 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5733 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5734 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5735 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5736 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5737 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5740 /* If this was supposed to be a transparent union, but we can't
5741 make it one, warn and turn off the flag. */
5742 if (TREE_CODE (t) == UNION_TYPE
5743 && TYPE_TRANSPARENT_UNION (t)
5744 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5746 TYPE_TRANSPARENT_UNION (t) = 0;
5747 warning (0, "union cannot be made transparent");
5750 /* If this structure or union completes the type of any previous
5751 variable declaration, lay it out and output its rtl. */
5752 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5756 tree decl = TREE_VALUE (x);
5757 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5758 layout_array_type (TREE_TYPE (decl));
5759 if (TREE_CODE (decl) != TYPE_DECL)
5761 layout_decl (decl, 0);
5762 if (c_dialect_objc ())
5763 objc_check_decl (decl);
5764 rest_of_decl_compilation (decl, toplevel, 0);
5769 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5771 /* Finish debugging output for this type. */
5772 rest_of_type_compilation (t, toplevel);
5774 /* If we're inside a function proper, i.e. not file-scope and not still
5775 parsing parameters, then arrange for the size of a variable sized type
5777 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
5778 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5783 /* Lay out the type T, and its element type, and so on. */
5786 layout_array_type (tree t)
5788 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5789 layout_array_type (TREE_TYPE (t));
5793 /* Begin compiling the definition of an enumeration type.
5794 NAME is its name (or null if anonymous).
5795 Returns the type object, as yet incomplete.
5796 Also records info about it so that build_enumerator
5797 may be used to declare the individual values as they are read. */
5800 start_enum (tree name)
5804 /* If this is the real definition for a previous forward reference,
5805 fill in the contents in the same object that used to be the
5806 forward reference. */
5809 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5811 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5813 enumtype = make_node (ENUMERAL_TYPE);
5814 pushtag (name, enumtype);
5817 if (C_TYPE_BEING_DEFINED (enumtype))
5818 error ("nested redefinition of %<enum %E%>", name);
5820 C_TYPE_BEING_DEFINED (enumtype) = 1;
5822 if (TYPE_VALUES (enumtype) != 0)
5824 /* This enum is a named one that has been declared already. */
5825 error ("redeclaration of %<enum %E%>", name);
5827 /* Completely replace its old definition.
5828 The old enumerators remain defined, however. */
5829 TYPE_VALUES (enumtype) = 0;
5832 enum_next_value = integer_zero_node;
5835 if (flag_short_enums)
5836 TYPE_PACKED (enumtype) = 1;
5841 /* After processing and defining all the values of an enumeration type,
5842 install their decls in the enumeration type and finish it off.
5843 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5844 and ATTRIBUTES are the specified attributes.
5845 Returns ENUMTYPE. */
5848 finish_enum (tree enumtype, tree values, tree attributes)
5851 tree minnode = 0, maxnode = 0;
5852 int precision, unsign;
5853 bool toplevel = (file_scope == current_scope);
5854 struct lang_type *lt;
5856 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5858 /* Calculate the maximum value of any enumerator in this type. */
5860 if (values == error_mark_node)
5861 minnode = maxnode = integer_zero_node;
5864 minnode = maxnode = TREE_VALUE (values);
5865 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5867 tree value = TREE_VALUE (pair);
5868 if (tree_int_cst_lt (maxnode, value))
5870 if (tree_int_cst_lt (value, minnode))
5875 /* Construct the final type of this enumeration. It is the same
5876 as one of the integral types - the narrowest one that fits, except
5877 that normally we only go as narrow as int - and signed iff any of
5878 the values are negative. */
5879 unsign = (tree_int_cst_sgn (minnode) >= 0);
5880 precision = MAX (min_precision (minnode, unsign),
5881 min_precision (maxnode, unsign));
5883 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5885 tem = c_common_type_for_size (precision, unsign);
5888 warning (0, "enumeration values exceed range of largest integer");
5889 tem = long_long_integer_type_node;
5893 tem = unsign ? unsigned_type_node : integer_type_node;
5895 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5896 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5897 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5898 TYPE_SIZE (enumtype) = 0;
5900 /* If the precision of the type was specific with an attribute and it
5901 was too small, give an error. Otherwise, use it. */
5902 if (TYPE_PRECISION (enumtype))
5904 if (precision > TYPE_PRECISION (enumtype))
5905 error ("specified mode too small for enumeral values");
5908 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5910 layout_type (enumtype);
5912 if (values != error_mark_node)
5914 /* Change the type of the enumerators to be the enum type. We
5915 need to do this irrespective of the size of the enum, for
5916 proper type checking. Replace the DECL_INITIALs of the
5917 enumerators, and the value slots of the list, with copies
5918 that have the enum type; they cannot be modified in place
5919 because they may be shared (e.g. integer_zero_node) Finally,
5920 change the purpose slots to point to the names of the decls. */
5921 for (pair = values; pair; pair = TREE_CHAIN (pair))
5923 tree enu = TREE_PURPOSE (pair);
5924 tree ini = DECL_INITIAL (enu);
5926 TREE_TYPE (enu) = enumtype;
5928 /* The ISO C Standard mandates enumerators to have type int,
5929 even though the underlying type of an enum type is
5930 unspecified. Here we convert any enumerators that fit in
5931 an int to type int, to avoid promotions to unsigned types
5932 when comparing integers with enumerators that fit in the
5933 int range. When -pedantic is given, build_enumerator()
5934 would have already taken care of those that don't fit. */
5935 if (int_fits_type_p (ini, integer_type_node))
5936 tem = integer_type_node;
5939 ini = convert (tem, ini);
5941 DECL_INITIAL (enu) = ini;
5942 TREE_PURPOSE (pair) = DECL_NAME (enu);
5943 TREE_VALUE (pair) = ini;
5946 TYPE_VALUES (enumtype) = values;
5949 /* Record the min/max values so that we can warn about bit-field
5950 enumerations that are too small for the values. */
5951 lt = GGC_CNEW (struct lang_type);
5952 lt->enum_min = minnode;
5953 lt->enum_max = maxnode;
5954 TYPE_LANG_SPECIFIC (enumtype) = lt;
5956 /* Fix up all variant types of this enum type. */
5957 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5959 if (tem == enumtype)
5961 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5962 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5963 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5964 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5965 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5966 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5967 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5968 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5969 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5970 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5971 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5974 /* Finish debugging output for this type. */
5975 rest_of_type_compilation (enumtype, toplevel);
5980 /* Build and install a CONST_DECL for one value of the
5981 current enumeration type (one that was begun with start_enum).
5982 Return a tree-list containing the CONST_DECL and its value.
5983 Assignment of sequential values by default is handled here. */
5986 build_enumerator (tree name, tree value)
5990 /* Validate and default VALUE. */
5994 /* Don't issue more errors for error_mark_node (i.e. an
5995 undeclared identifier) - just ignore the value expression. */
5996 if (value == error_mark_node)
5998 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5999 || TREE_CODE (value) != INTEGER_CST)
6001 error ("enumerator value for %qE is not an integer constant", name);
6006 value = default_conversion (value);
6007 constant_expression_warning (value);
6011 /* Default based on previous value. */
6012 /* It should no longer be possible to have NON_LVALUE_EXPR
6016 value = enum_next_value;
6018 error ("overflow in enumeration values");
6021 if (pedantic && !int_fits_type_p (value, integer_type_node))
6023 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
6024 /* XXX This causes -pedantic to change the meaning of the program.
6025 Remove? -zw 2004-03-15 */
6026 value = convert (integer_type_node, value);
6029 /* Set basis for default for next value. */
6030 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
6031 enum_overflow = tree_int_cst_lt (enum_next_value, value);
6033 /* Now create a declaration for the enum value name. */
6035 type = TREE_TYPE (value);
6036 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6037 TYPE_PRECISION (integer_type_node)),
6038 (TYPE_PRECISION (type)
6039 >= TYPE_PRECISION (integer_type_node)
6040 && TYPE_UNSIGNED (type)));
6042 decl = build_decl (CONST_DECL, name, type);
6043 DECL_INITIAL (decl) = convert (type, value);
6046 return tree_cons (decl, value, NULL_TREE);
6050 /* Create the FUNCTION_DECL for a function definition.
6051 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6052 the declaration; they describe the function's name and the type it returns,
6053 but twisted together in a fashion that parallels the syntax of C.
6055 This function creates a binding context for the function body
6056 as well as setting up the FUNCTION_DECL in current_function_decl.
6058 Returns 1 on success. If the DECLARATOR is not suitable for a function
6059 (it defines a datum instead), we return 0, which tells
6060 yyparse to report a parse error. */
6063 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6066 tree decl1, old_decl;
6067 tree restype, resdecl;
6068 struct c_label_context_se *nstack_se;
6069 struct c_label_context_vm *nstack_vm;
6071 current_function_returns_value = 0; /* Assume, until we see it does. */
6072 current_function_returns_null = 0;
6073 current_function_returns_abnormally = 0;
6074 warn_about_return_type = 0;
6075 c_switch_stack = NULL;
6077 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6078 nstack_se->labels_def = NULL;
6079 nstack_se->labels_used = NULL;
6080 nstack_se->next = label_context_stack_se;
6081 label_context_stack_se = nstack_se;
6083 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6084 nstack_vm->labels_def = NULL;
6085 nstack_vm->labels_used = NULL;
6086 nstack_vm->scope = 0;
6087 nstack_vm->next = label_context_stack_vm;
6088 label_context_stack_vm = nstack_vm;
6090 /* Indicate no valid break/continue context by setting these variables
6091 to some non-null, non-label value. We'll notice and emit the proper
6092 error message in c_finish_bc_stmt. */
6093 c_break_label = c_cont_label = size_zero_node;
6095 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
6097 /* If the declarator is not suitable for a function definition,
6098 cause a syntax error. */
6101 label_context_stack_se = label_context_stack_se->next;
6102 label_context_stack_vm = label_context_stack_vm->next;
6106 decl_attributes (&decl1, attributes, 0);
6108 if (DECL_DECLARED_INLINE_P (decl1)
6109 && DECL_UNINLINABLE (decl1)
6110 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6111 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6114 /* Handle gnu_inline attribute. */
6115 if (declspecs->inline_p
6116 && !flag_gnu89_inline
6117 && TREE_CODE (decl1) == FUNCTION_DECL
6118 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1)))
6120 if (declspecs->storage_class != csc_static)
6121 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6124 announce_function (decl1);
6126 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6128 error ("return type is an incomplete type");
6129 /* Make it return void instead. */
6131 = build_function_type (void_type_node,
6132 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6135 if (warn_about_return_type)
6136 pedwarn_c99 ("return type defaults to %<int%>");
6138 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6139 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6140 DECL_INITIAL (decl1) = error_mark_node;
6142 /* If this definition isn't a prototype and we had a prototype declaration
6143 before, copy the arg type info from that prototype. */
6144 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6145 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6147 current_function_prototype_locus = UNKNOWN_LOCATION;
6148 current_function_prototype_built_in = false;
6149 current_function_prototype_arg_types = NULL_TREE;
6150 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6152 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6153 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6154 TREE_TYPE (TREE_TYPE (old_decl))))
6156 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6158 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6159 current_function_prototype_built_in
6160 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6161 current_function_prototype_arg_types
6162 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6164 if (TREE_PUBLIC (decl1))
6166 /* If there is an external prototype declaration of this
6167 function, record its location but do not copy information
6168 to this decl. This may be an invisible declaration
6169 (built-in or in a scope which has finished) or simply
6170 have more refined argument types than any declaration
6172 struct c_binding *b;
6173 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6174 if (B_IN_SCOPE (b, external_scope))
6178 tree ext_decl, ext_type;
6180 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6181 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6182 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6183 TREE_TYPE (ext_type)))
6185 current_function_prototype_locus
6186 = DECL_SOURCE_LOCATION (ext_decl);
6187 current_function_prototype_built_in
6188 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6189 current_function_prototype_arg_types
6190 = TYPE_ARG_TYPES (ext_type);
6196 /* Optionally warn of old-fashioned def with no previous prototype. */
6197 if (warn_strict_prototypes
6198 && old_decl != error_mark_node
6199 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6200 && C_DECL_ISNT_PROTOTYPE (old_decl))
6201 warning (OPT_Wstrict_prototypes,
6202 "function declaration isn%'t a prototype");
6203 /* Optionally warn of any global def with no previous prototype. */
6204 else if (warn_missing_prototypes
6205 && old_decl != error_mark_node
6206 && TREE_PUBLIC (decl1)
6207 && !MAIN_NAME_P (DECL_NAME (decl1))
6208 && C_DECL_ISNT_PROTOTYPE (old_decl))
6209 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6210 /* Optionally warn of any def with no previous prototype
6211 if the function has already been used. */
6212 else if (warn_missing_prototypes
6214 && old_decl != error_mark_node
6215 && TREE_USED (old_decl)
6216 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6217 warning (OPT_Wmissing_prototypes,
6218 "%q+D was used with no prototype before its definition", decl1);
6219 /* Optionally warn of any global def with no previous declaration. */
6220 else if (warn_missing_declarations
6221 && TREE_PUBLIC (decl1)
6223 && !MAIN_NAME_P (DECL_NAME (decl1)))
6224 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6226 /* Optionally warn of any def with no previous declaration
6227 if the function has already been used. */
6228 else if (warn_missing_declarations
6230 && old_decl != error_mark_node
6231 && TREE_USED (old_decl)
6232 && C_DECL_IMPLICIT (old_decl))
6233 warning (OPT_Wmissing_declarations,
6234 "%q+D was used with no declaration before its definition", decl1);
6236 /* This function exists in static storage.
6237 (This does not mean `static' in the C sense!) */
6238 TREE_STATIC (decl1) = 1;
6240 /* A nested function is not global. */
6241 if (current_function_decl != 0)
6242 TREE_PUBLIC (decl1) = 0;
6244 /* This is the earliest point at which we might know the assembler
6245 name of the function. Thus, if it's set before this, die horribly. */
6246 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6248 /* If #pragma weak was used, mark the decl weak now. */
6249 if (current_scope == file_scope)
6250 maybe_apply_pragma_weak (decl1);
6252 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6253 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6258 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6259 != integer_type_node)
6260 pedwarn ("return type of %q+D is not %<int%>", decl1);
6262 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
6263 args = TREE_CHAIN (args))
6265 tree type = args ? TREE_VALUE (args) : 0;
6267 if (type == void_type_node)
6274 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
6275 pedwarn ("first argument of %q+D should be %<int%>", decl1);
6279 if (TREE_CODE (type) != POINTER_TYPE
6280 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6281 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6283 pedwarn ("second argument of %q+D should be %<char **%>",
6288 if (TREE_CODE (type) != POINTER_TYPE
6289 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6290 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6292 pedwarn ("third argument of %q+D should probably be "
6293 "%<char **%>", decl1);
6298 /* It is intentional that this message does not mention the third
6299 argument because it's only mentioned in an appendix of the
6301 if (argct > 0 && (argct < 2 || argct > 3))
6302 pedwarn ("%q+D takes only zero or two arguments", decl1);
6304 if (!TREE_PUBLIC (decl1))
6305 pedwarn ("%q+D is normally a non-static function", decl1);
6308 /* Record the decl so that the function name is defined.
6309 If we already have a decl for this name, and it is a FUNCTION_DECL,
6310 use the old decl. */
6312 current_function_decl = pushdecl (decl1);
6315 declare_parm_level ();
6317 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6318 /* Promote the value to int before returning it. */
6319 if (c_promoting_integer_type_p (restype))
6321 /* It retains unsignedness if not really getting wider. */
6322 if (TYPE_UNSIGNED (restype)
6323 && (TYPE_PRECISION (restype)
6324 == TYPE_PRECISION (integer_type_node)))
6325 restype = unsigned_type_node;
6327 restype = integer_type_node;
6330 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6331 DECL_ARTIFICIAL (resdecl) = 1;
6332 DECL_IGNORED_P (resdecl) = 1;
6333 DECL_RESULT (current_function_decl) = resdecl;
6335 start_fname_decls ();
6340 /* Subroutine of store_parm_decls which handles new-style function
6341 definitions (prototype format). The parms already have decls, so we
6342 need only record them as in effect and complain if any redundant
6343 old-style parm decls were written. */
6345 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6349 if (current_scope->bindings)
6351 error ("%Jold-style parameter declarations in prototyped "
6352 "function definition", fndecl);
6354 /* Get rid of the old-style declarations. */
6358 /* Don't issue this warning for nested functions, and don't issue this
6359 warning if we got here because ARG_INFO_TYPES was error_mark_node
6360 (this happens when a function definition has just an ellipsis in
6361 its parameter list). */
6362 else if (!in_system_header && !current_function_scope
6363 && arg_info->types != error_mark_node)
6364 warning (OPT_Wtraditional,
6365 "%Jtraditional C rejects ISO C style function definitions",
6368 /* Now make all the parameter declarations visible in the function body.
6369 We can bypass most of the grunt work of pushdecl. */
6370 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6372 DECL_CONTEXT (decl) = current_function_decl;
6373 if (DECL_NAME (decl))
6375 bind (DECL_NAME (decl), decl, current_scope,
6376 /*invisible=*/false, /*nested=*/false);
6377 if (!TREE_USED (decl))
6378 warn_if_shadowing (decl);
6381 error ("%Jparameter name omitted", decl);
6384 /* Record the parameter list in the function declaration. */
6385 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6387 /* Now make all the ancillary declarations visible, likewise. */
6388 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6390 DECL_CONTEXT (decl) = current_function_decl;
6391 if (DECL_NAME (decl))
6392 bind (DECL_NAME (decl), decl, current_scope,
6393 /*invisible=*/false, /*nested=*/false);
6396 /* And all the tag declarations. */
6397 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6398 if (TREE_PURPOSE (decl))
6399 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6400 /*invisible=*/false, /*nested=*/false);
6403 /* Subroutine of store_parm_decls which handles old-style function
6404 definitions (separate parameter list and declarations). */
6407 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6409 struct c_binding *b;
6410 tree parm, decl, last;
6411 tree parmids = arg_info->parms;
6412 struct pointer_set_t *seen_args = pointer_set_create ();
6414 if (!in_system_header)
6415 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6418 /* Match each formal parameter name with its declaration. Save each
6419 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6420 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6422 if (TREE_VALUE (parm) == 0)
6424 error ("%Jparameter name missing from parameter list", fndecl);
6425 TREE_PURPOSE (parm) = 0;
6429 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6430 if (b && B_IN_CURRENT_SCOPE (b))
6433 /* If we got something other than a PARM_DECL it is an error. */
6434 if (TREE_CODE (decl) != PARM_DECL)
6435 error ("%q+D declared as a non-parameter", decl);
6436 /* If the declaration is already marked, we have a duplicate
6437 name. Complain and ignore the duplicate. */
6438 else if (pointer_set_contains (seen_args, decl))
6440 error ("multiple parameters named %q+D", decl);
6441 TREE_PURPOSE (parm) = 0;
6444 /* If the declaration says "void", complain and turn it into
6446 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6448 error ("parameter %q+D declared with void type", decl);
6449 TREE_TYPE (decl) = integer_type_node;
6450 DECL_ARG_TYPE (decl) = integer_type_node;
6451 layout_decl (decl, 0);
6453 warn_if_shadowing (decl);
6455 /* If no declaration found, default to int. */
6458 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6459 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6460 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6462 warn_if_shadowing (decl);
6465 pedwarn ("type of %q+D defaults to %<int%>", decl);
6466 else if (extra_warnings)
6467 warning (OPT_Wextra, "type of %q+D defaults to %<int%>", decl);
6470 TREE_PURPOSE (parm) = decl;
6471 pointer_set_insert (seen_args, decl);
6474 /* Now examine the parms chain for incomplete declarations
6475 and declarations with no corresponding names. */
6477 for (b = current_scope->bindings; b; b = b->prev)
6480 if (TREE_CODE (parm) != PARM_DECL)
6483 if (TREE_TYPE (parm) != error_mark_node
6484 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6486 error ("parameter %q+D has incomplete type", parm);
6487 TREE_TYPE (parm) = error_mark_node;
6490 if (!pointer_set_contains (seen_args, parm))
6492 error ("declaration for parameter %q+D but no such parameter", parm);
6494 /* Pretend the parameter was not missing.
6495 This gets us to a standard state and minimizes
6496 further error messages. */
6497 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6501 /* Chain the declarations together in the order of the list of
6502 names. Store that chain in the function decl, replacing the
6503 list of names. Update the current scope to match. */
6504 DECL_ARGUMENTS (fndecl) = 0;
6506 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6507 if (TREE_PURPOSE (parm))
6509 if (parm && TREE_PURPOSE (parm))
6511 last = TREE_PURPOSE (parm);
6512 DECL_ARGUMENTS (fndecl) = last;
6514 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6515 if (TREE_PURPOSE (parm))
6517 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6518 last = TREE_PURPOSE (parm);
6520 TREE_CHAIN (last) = 0;
6523 pointer_set_destroy (seen_args);
6525 /* If there was a previous prototype,
6526 set the DECL_ARG_TYPE of each argument according to
6527 the type previously specified, and report any mismatches. */
6529 if (current_function_prototype_arg_types)
6532 for (parm = DECL_ARGUMENTS (fndecl),
6533 type = current_function_prototype_arg_types;
6534 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6535 != void_type_node));
6536 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6538 if (parm == 0 || type == 0
6539 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6541 if (current_function_prototype_built_in)
6542 warning (0, "number of arguments doesn%'t match "
6543 "built-in prototype");
6546 error ("number of arguments doesn%'t match prototype");
6547 error ("%Hprototype declaration",
6548 ¤t_function_prototype_locus);
6552 /* Type for passing arg must be consistent with that
6553 declared for the arg. ISO C says we take the unqualified
6554 type for parameters declared with qualified type. */
6555 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6556 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6558 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6559 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6561 /* Adjust argument to match prototype. E.g. a previous
6562 `int foo(float);' prototype causes
6563 `int foo(x) float x; {...}' to be treated like
6564 `int foo(float x) {...}'. This is particularly
6565 useful for argument types like uid_t. */
6566 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6568 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6569 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6570 && TYPE_PRECISION (TREE_TYPE (parm))
6571 < TYPE_PRECISION (integer_type_node))
6572 DECL_ARG_TYPE (parm) = integer_type_node;
6576 /* ??? Is it possible to get here with a
6577 built-in prototype or will it always have
6578 been diagnosed as conflicting with an
6579 old-style definition and discarded? */
6580 if (current_function_prototype_built_in)
6581 warning (0, "promoted argument %qD "
6582 "doesn%'t match built-in prototype", parm);
6585 pedwarn ("promoted argument %qD "
6586 "doesn%'t match prototype", parm);
6587 pedwarn ("%Hprototype declaration",
6588 ¤t_function_prototype_locus);
6594 if (current_function_prototype_built_in)
6595 warning (0, "argument %qD doesn%'t match "
6596 "built-in prototype", parm);
6599 error ("argument %qD doesn%'t match prototype", parm);
6600 error ("%Hprototype declaration",
6601 ¤t_function_prototype_locus);
6606 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6609 /* Otherwise, create a prototype that would match. */
6613 tree actual = 0, last = 0, type;
6615 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6617 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6619 TREE_CHAIN (last) = type;
6624 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6626 TREE_CHAIN (last) = type;
6630 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6631 of the type of this function, but we need to avoid having this
6632 affect the types of other similarly-typed functions, so we must
6633 first force the generation of an identical (but separate) type
6634 node for the relevant function type. The new node we create
6635 will be a variant of the main variant of the original function
6638 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6640 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6644 /* Store parameter declarations passed in ARG_INFO into the current
6645 function declaration. */
6648 store_parm_decls_from (struct c_arg_info *arg_info)
6650 current_function_arg_info = arg_info;
6651 store_parm_decls ();
6654 /* Store the parameter declarations into the current function declaration.
6655 This is called after parsing the parameter declarations, before
6656 digesting the body of the function.
6658 For an old-style definition, construct a prototype out of the old-style
6659 parameter declarations and inject it into the function's type. */
6662 store_parm_decls (void)
6664 tree fndecl = current_function_decl;
6667 /* The argument information block for FNDECL. */
6668 struct c_arg_info *arg_info = current_function_arg_info;
6669 current_function_arg_info = 0;
6671 /* True if this definition is written with a prototype. Note:
6672 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6673 list in a function definition as equivalent to (void) -- an
6674 empty argument list specifies the function has no parameters,
6675 but only (void) sets up a prototype for future calls. */
6676 proto = arg_info->types != 0;
6679 store_parm_decls_newstyle (fndecl, arg_info);
6681 store_parm_decls_oldstyle (fndecl, arg_info);
6683 /* The next call to push_scope will be a function body. */
6685 next_is_function_body = true;
6687 /* Write a record describing this function definition to the prototypes
6688 file (if requested). */
6690 gen_aux_info_record (fndecl, 1, 0, proto);
6692 /* Initialize the RTL code for the function. */
6693 allocate_struct_function (fndecl);
6695 /* Begin the statement tree for this function. */
6696 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6698 /* ??? Insert the contents of the pending sizes list into the function
6699 to be evaluated. The only reason left to have this is
6700 void foo(int n, int array[n++])
6701 because we throw away the array type in favor of a pointer type, and
6702 thus won't naturally see the SAVE_EXPR containing the increment. All
6703 other pending sizes would be handled by gimplify_parameters. */
6706 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6707 add_stmt (TREE_VALUE (t));
6710 /* Even though we're inside a function body, we still don't want to
6711 call expand_expr to calculate the size of a variable-sized array.
6712 We haven't necessarily assigned RTL to all variables yet, so it's
6713 not safe to try to expand expressions involving them. */
6714 cfun->x_dont_save_pending_sizes_p = 1;
6717 /* Emit diagnostics that require gimple input for detection. Operate on
6718 FNDECL and all its nested functions. */
6721 c_gimple_diagnostics_recursively (tree fndecl)
6723 struct cgraph_node *cgn;
6725 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6726 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6728 /* Notice when OpenMP structured block constraints are violated. */
6730 diagnose_omp_structured_block_errors (fndecl);
6732 /* Finalize all nested functions now. */
6733 cgn = cgraph_node (fndecl);
6734 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6735 c_gimple_diagnostics_recursively (cgn->decl);
6738 /* Finish up a function declaration and compile that function
6739 all the way to assembler language output. The free the storage
6740 for the function definition.
6742 This is called after parsing the body of the function definition. */
6745 finish_function (void)
6747 tree fndecl = current_function_decl;
6749 label_context_stack_se = label_context_stack_se->next;
6750 label_context_stack_vm = label_context_stack_vm->next;
6752 if (TREE_CODE (fndecl) == FUNCTION_DECL
6753 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6755 tree args = DECL_ARGUMENTS (fndecl);
6756 for (; args; args = TREE_CHAIN (args))
6758 tree type = TREE_TYPE (args);
6759 if (INTEGRAL_TYPE_P (type)
6760 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6761 DECL_ARG_TYPE (args) = integer_type_node;
6765 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6766 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6768 /* Must mark the RESULT_DECL as being in this function. */
6770 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6771 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6773 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6775 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6776 != integer_type_node)
6778 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6779 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6781 pedwarn ("return type of %q+D is not %<int%>", fndecl);
6787 tree stmt = c_finish_return (integer_zero_node);
6788 #ifdef USE_MAPPED_LOCATION
6789 /* Hack. We don't want the middle-end to warn that this return
6790 is unreachable, so we mark its location as special. Using
6791 UNKNOWN_LOCATION has the problem that it gets clobbered in
6792 annotate_one_with_locus. A cleaner solution might be to
6793 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6795 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6797 /* Hack. We don't want the middle-end to warn that this
6798 return is unreachable, so put the statement on the
6800 annotate_with_file_line (stmt, input_filename, 0);
6806 /* Tie off the statement tree for this function. */
6807 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6809 finish_fname_decls ();
6811 /* Complain if there's just no return statement. */
6812 if (warn_return_type
6813 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6814 && !current_function_returns_value && !current_function_returns_null
6815 /* Don't complain if we are no-return. */
6816 && !current_function_returns_abnormally
6817 /* Don't warn for main(). */
6818 && !MAIN_NAME_P (DECL_NAME (fndecl))
6819 /* Or if they didn't actually specify a return type. */
6820 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6821 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6822 inline function, as we might never be compiled separately. */
6823 && DECL_INLINE (fndecl))
6825 warning (OPT_Wreturn_type,
6826 "no return statement in function returning non-void");
6827 TREE_NO_WARNING (fndecl) = 1;
6830 /* With just -Wextra, complain only if function returns both with
6831 and without a value. */
6833 && current_function_returns_value
6834 && current_function_returns_null)
6835 warning (OPT_Wextra, "this function may return with or without a value");
6837 /* Store the end of the function, so that we get good line number
6838 info for the epilogue. */
6839 cfun->function_end_locus = input_location;
6841 /* If we don't have ctors/dtors sections, and this is a static
6842 constructor or destructor, it must be recorded now. */
6843 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6844 && !targetm.have_ctors_dtors)
6845 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6846 if (DECL_STATIC_DESTRUCTOR (fndecl)
6847 && !targetm.have_ctors_dtors)
6848 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6850 /* Finalize the ELF visibility for the function. */
6851 c_determine_visibility (fndecl);
6853 /* Genericize before inlining. Delay genericizing nested functions
6854 until their parent function is genericized. Since finalizing
6855 requires GENERIC, delay that as well. */
6857 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6858 && !undef_nested_function)
6860 if (!decl_function_context (fndecl))
6862 c_genericize (fndecl);
6863 c_gimple_diagnostics_recursively (fndecl);
6865 /* ??? Objc emits functions after finalizing the compilation unit.
6866 This should be cleaned up later and this conditional removed. */
6867 if (cgraph_global_info_ready)
6869 c_expand_body (fndecl);
6873 cgraph_finalize_function (fndecl, false);
6877 /* Register this function with cgraph just far enough to get it
6878 added to our parent's nested function list. Handy, since the
6879 C front end doesn't have such a list. */
6880 (void) cgraph_node (fndecl);
6884 if (!decl_function_context (fndecl))
6885 undef_nested_function = false;
6887 /* We're leaving the context of this function, so zap cfun.
6888 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6889 tree_rest_of_compilation. */
6891 current_function_decl = NULL;
6894 /* Generate the RTL for the body of FNDECL. */
6897 c_expand_body (tree fndecl)
6900 if (!DECL_INITIAL (fndecl)
6901 || DECL_INITIAL (fndecl) == error_mark_node)
6904 tree_rest_of_compilation (fndecl);
6906 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6907 && targetm.have_ctors_dtors)
6908 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6909 DEFAULT_INIT_PRIORITY);
6910 if (DECL_STATIC_DESTRUCTOR (fndecl)
6911 && targetm.have_ctors_dtors)
6912 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6913 DEFAULT_INIT_PRIORITY);
6916 /* Check the declarations given in a for-loop for satisfying the C99
6917 constraints. If exactly one such decl is found, return it. */
6920 check_for_loop_decls (void)
6922 struct c_binding *b;
6923 tree one_decl = NULL_TREE;
6929 /* If we get here, declarations have been used in a for loop without
6930 the C99 for loop scope. This doesn't make much sense, so don't
6932 error ("%<for%> loop initial declaration used outside C99 mode");
6935 /* C99 subclause 6.8.5 paragraph 3:
6937 [#3] The declaration part of a for statement shall only
6938 declare identifiers for objects having storage class auto or
6941 It isn't clear whether, in this sentence, "identifiers" binds to
6942 "shall only declare" or to "objects" - that is, whether all identifiers
6943 declared must be identifiers for objects, or whether the restriction
6944 only applies to those that are. (A question on this in comp.std.c
6945 in November 2000 received no answer.) We implement the strictest
6946 interpretation, to avoid creating an extension which later causes
6949 for (b = current_scope->bindings; b; b = b->prev)
6952 tree decl = b->decl;
6957 switch (TREE_CODE (decl))
6960 if (TREE_STATIC (decl))
6961 error ("declaration of static variable %q+D in %<for%> loop "
6962 "initial declaration", decl);
6963 else if (DECL_EXTERNAL (decl))
6964 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
6965 "initial declaration", decl);
6969 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6973 error ("%<union %E%> declared in %<for%> loop initial declaration",
6977 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6981 error ("declaration of non-variable %q+D in %<for%> loop "
6982 "initial declaration", decl);
6989 return n_decls == 1 ? one_decl : NULL_TREE;
6992 /* Save and reinitialize the variables
6993 used during compilation of a C function. */
6996 c_push_function_context (struct function *f)
6998 struct language_function *p;
6999 p = GGC_NEW (struct language_function);
7002 p->base.x_stmt_tree = c_stmt_tree;
7003 p->x_break_label = c_break_label;
7004 p->x_cont_label = c_cont_label;
7005 p->x_switch_stack = c_switch_stack;
7006 p->arg_info = current_function_arg_info;
7007 p->returns_value = current_function_returns_value;
7008 p->returns_null = current_function_returns_null;
7009 p->returns_abnormally = current_function_returns_abnormally;
7010 p->warn_about_return_type = warn_about_return_type;
7013 /* Restore the variables used during compilation of a C function. */
7016 c_pop_function_context (struct function *f)
7018 struct language_function *p = f->language;
7020 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
7021 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
7023 /* Stop pointing to the local nodes about to be freed. */
7024 /* But DECL_INITIAL must remain nonzero so we know this
7025 was an actual function definition. */
7026 DECL_INITIAL (current_function_decl) = error_mark_node;
7027 DECL_ARGUMENTS (current_function_decl) = 0;
7030 c_stmt_tree = p->base.x_stmt_tree;
7031 c_break_label = p->x_break_label;
7032 c_cont_label = p->x_cont_label;
7033 c_switch_stack = p->x_switch_stack;
7034 current_function_arg_info = p->arg_info;
7035 current_function_returns_value = p->returns_value;
7036 current_function_returns_null = p->returns_null;
7037 current_function_returns_abnormally = p->returns_abnormally;
7038 warn_about_return_type = p->warn_about_return_type;
7043 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
7046 c_dup_lang_specific_decl (tree decl)
7048 struct lang_decl *ld;
7050 if (!DECL_LANG_SPECIFIC (decl))
7053 ld = GGC_NEW (struct lang_decl);
7054 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
7055 DECL_LANG_SPECIFIC (decl) = ld;
7058 /* The functions below are required for functionality of doing
7059 function at once processing in the C front end. Currently these
7060 functions are not called from anywhere in the C front end, but as
7061 these changes continue, that will change. */
7063 /* Returns the stmt_tree (if any) to which statements are currently
7064 being added. If there is no active statement-tree, NULL is
7068 current_stmt_tree (void)
7070 return &c_stmt_tree;
7073 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
7077 anon_aggr_type_p (tree ARG_UNUSED (node))
7082 /* Return the global value of T as a symbol. */
7085 identifier_global_value (tree t)
7087 struct c_binding *b;
7089 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
7090 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
7096 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
7097 otherwise the name is found in ridpointers from RID_INDEX. */
7100 record_builtin_type (enum rid rid_index, const char *name, tree type)
7104 id = ridpointers[(int) rid_index];
7106 id = get_identifier (name);
7107 decl = build_decl (TYPE_DECL, id, type);
7109 if (debug_hooks->type_decl)
7110 debug_hooks->type_decl (decl, false);
7113 /* Build the void_list_node (void_type_node having been created). */
7115 build_void_list_node (void)
7117 tree t = build_tree_list (NULL_TREE, void_type_node);
7121 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7124 build_c_parm (struct c_declspecs *specs, tree attrs,
7125 struct c_declarator *declarator)
7127 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7130 ret->declarator = declarator;
7134 /* Return a declarator with nested attributes. TARGET is the inner
7135 declarator to which these attributes apply. ATTRS are the
7138 struct c_declarator *
7139 build_attrs_declarator (tree attrs, struct c_declarator *target)
7141 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7142 ret->kind = cdk_attrs;
7143 ret->declarator = target;
7144 ret->u.attrs = attrs;
7148 /* Return a declarator for a function with arguments specified by ARGS
7149 and return type specified by TARGET. */
7151 struct c_declarator *
7152 build_function_declarator (struct c_arg_info *args,
7153 struct c_declarator *target)
7155 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7156 ret->kind = cdk_function;
7157 ret->declarator = target;
7158 ret->u.arg_info = args;
7162 /* Return a declarator for the identifier IDENT (which may be
7163 NULL_TREE for an abstract declarator). */
7165 struct c_declarator *
7166 build_id_declarator (tree ident)
7168 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7170 ret->declarator = 0;
7172 /* Default value - may get reset to a more precise location. */
7173 ret->id_loc = input_location;
7177 /* Return something to represent absolute declarators containing a *.
7178 TARGET is the absolute declarator that the * contains.
7179 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7180 to apply to the pointer type. */
7182 struct c_declarator *
7183 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7184 struct c_declarator *target)
7188 struct c_declarator *itarget = target;
7189 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7190 if (type_quals_attrs)
7192 attrs = type_quals_attrs->attrs;
7193 quals = quals_from_declspecs (type_quals_attrs);
7194 if (attrs != NULL_TREE)
7195 itarget = build_attrs_declarator (attrs, target);
7197 ret->kind = cdk_pointer;
7198 ret->declarator = itarget;
7199 ret->u.pointer_quals = quals;
7203 /* Return a pointer to a structure for an empty list of declaration
7206 struct c_declspecs *
7207 build_null_declspecs (void)
7209 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7213 ret->typespec_word = cts_none;
7214 ret->storage_class = csc_none;
7215 ret->declspecs_seen_p = false;
7216 ret->type_seen_p = false;
7217 ret->non_sc_seen_p = false;
7218 ret->typedef_p = false;
7219 ret->tag_defined_p = false;
7220 ret->explicit_signed_p = false;
7221 ret->deprecated_p = false;
7222 ret->default_int_p = false;
7223 ret->long_p = false;
7224 ret->long_long_p = false;
7225 ret->short_p = false;
7226 ret->signed_p = false;
7227 ret->unsigned_p = false;
7228 ret->complex_p = false;
7229 ret->inline_p = false;
7230 ret->thread_p = false;
7231 ret->const_p = false;
7232 ret->volatile_p = false;
7233 ret->restrict_p = false;
7237 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7240 struct c_declspecs *
7241 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7245 specs->non_sc_seen_p = true;
7246 specs->declspecs_seen_p = true;
7247 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7248 && C_IS_RESERVED_WORD (qual));
7249 i = C_RID_CODE (qual);
7253 dupe = specs->const_p;
7254 specs->const_p = true;
7257 dupe = specs->volatile_p;
7258 specs->volatile_p = true;
7261 dupe = specs->restrict_p;
7262 specs->restrict_p = true;
7267 if (dupe && pedantic && !flag_isoc99)
7268 pedwarn ("duplicate %qE", qual);
7272 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7275 struct c_declspecs *
7276 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7278 tree type = spec.spec;
7279 specs->non_sc_seen_p = true;
7280 specs->declspecs_seen_p = true;
7281 specs->type_seen_p = true;
7282 if (TREE_DEPRECATED (type))
7283 specs->deprecated_p = true;
7285 /* Handle type specifier keywords. */
7286 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
7288 enum rid i = C_RID_CODE (type);
7291 error ("two or more data types in declaration specifiers");
7294 if ((int) i <= (int) RID_LAST_MODIFIER)
7296 /* "long", "short", "signed", "unsigned" or "_Complex". */
7301 if (specs->long_long_p)
7303 error ("%<long long long%> is too long for GCC");
7308 if (specs->typespec_word == cts_double)
7310 error ("both %<long long%> and %<double%> in "
7311 "declaration specifiers");
7314 if (pedantic && !flag_isoc99 && !in_system_header
7316 pedwarn ("ISO C90 does not support %<long long%>");
7317 specs->long_long_p = 1;
7321 error ("both %<long%> and %<short%> in "
7322 "declaration specifiers");
7323 else if (specs->typespec_word == cts_void)
7324 error ("both %<long%> and %<void%> in "
7325 "declaration specifiers");
7326 else if (specs->typespec_word == cts_bool)
7327 error ("both %<long%> and %<_Bool%> in "
7328 "declaration specifiers");
7329 else if (specs->typespec_word == cts_char)
7330 error ("both %<long%> and %<char%> in "
7331 "declaration specifiers");
7332 else if (specs->typespec_word == cts_float)
7333 error ("both %<long%> and %<float%> in "
7334 "declaration specifiers");
7335 else if (specs->typespec_word == cts_dfloat32)
7336 error ("both %<long%> and %<_Decimal32%> in "
7337 "declaration specifiers");
7338 else if (specs->typespec_word == cts_dfloat64)
7339 error ("both %<long%> and %<_Decimal64%> in "
7340 "declaration specifiers");
7341 else if (specs->typespec_word == cts_dfloat128)
7342 error ("both %<long%> and %<_Decimal128%> in "
7343 "declaration specifiers");
7345 specs->long_p = true;
7348 dupe = specs->short_p;
7350 error ("both %<long%> and %<short%> in "
7351 "declaration specifiers");
7352 else if (specs->typespec_word == cts_void)
7353 error ("both %<short%> and %<void%> in "
7354 "declaration specifiers");
7355 else if (specs->typespec_word == cts_bool)
7356 error ("both %<short%> and %<_Bool%> in "
7357 "declaration specifiers");
7358 else if (specs->typespec_word == cts_char)
7359 error ("both %<short%> and %<char%> in "
7360 "declaration specifiers");
7361 else if (specs->typespec_word == cts_float)
7362 error ("both %<short%> and %<float%> in "
7363 "declaration specifiers");
7364 else if (specs->typespec_word == cts_double)
7365 error ("both %<short%> and %<double%> in "
7366 "declaration specifiers");
7367 else if (specs->typespec_word == cts_dfloat32)
7368 error ("both %<short%> and %<_Decimal32%> in "
7369 "declaration specifiers");
7370 else if (specs->typespec_word == cts_dfloat64)
7371 error ("both %<short%> and %<_Decimal64%> in "
7372 "declaration specifiers");
7373 else if (specs->typespec_word == cts_dfloat128)
7374 error ("both %<short%> and %<_Decimal128%> in "
7375 "declaration specifiers");
7377 specs->short_p = true;
7380 dupe = specs->signed_p;
7381 if (specs->unsigned_p)
7382 error ("both %<signed%> and %<unsigned%> in "
7383 "declaration specifiers");
7384 else if (specs->typespec_word == cts_void)
7385 error ("both %<signed%> and %<void%> in "
7386 "declaration specifiers");
7387 else if (specs->typespec_word == cts_bool)
7388 error ("both %<signed%> and %<_Bool%> in "
7389 "declaration specifiers");
7390 else if (specs->typespec_word == cts_float)
7391 error ("both %<signed%> and %<float%> in "
7392 "declaration specifiers");
7393 else if (specs->typespec_word == cts_double)
7394 error ("both %<signed%> and %<double%> in "
7395 "declaration specifiers");
7396 else if (specs->typespec_word == cts_dfloat32)
7397 error ("both %<signed%> and %<_Decimal32%> in "
7398 "declaration specifiers");
7399 else if (specs->typespec_word == cts_dfloat64)
7400 error ("both %<signed%> and %<_Decimal64%> in "
7401 "declaration specifiers");
7402 else if (specs->typespec_word == cts_dfloat128)
7403 error ("both %<signed%> and %<_Decimal128%> in "
7404 "declaration specifiers");
7406 specs->signed_p = true;
7409 dupe = specs->unsigned_p;
7410 if (specs->signed_p)
7411 error ("both %<signed%> and %<unsigned%> in "
7412 "declaration specifiers");
7413 else if (specs->typespec_word == cts_void)
7414 error ("both %<unsigned%> and %<void%> in "
7415 "declaration specifiers");
7416 else if (specs->typespec_word == cts_bool)
7417 error ("both %<unsigned%> and %<_Bool%> in "
7418 "declaration specifiers");
7419 else if (specs->typespec_word == cts_float)
7420 error ("both %<unsigned%> and %<float%> in "
7421 "declaration specifiers");
7422 else if (specs->typespec_word == cts_double)
7423 error ("both %<unsigned%> and %<double%> in "
7424 "declaration specifiers");
7425 else if (specs->typespec_word == cts_dfloat32)
7426 error ("both %<unsigned%> and %<_Decimal32%> in "
7427 "declaration specifiers");
7428 else if (specs->typespec_word == cts_dfloat64)
7429 error ("both %<unsigned%> and %<_Decimal64%> in "
7430 "declaration specifiers");
7431 else if (specs->typespec_word == cts_dfloat128)
7432 error ("both %<unsigned%> and %<_Decimal128%> in "
7433 "declaration specifiers");
7435 specs->unsigned_p = true;
7438 dupe = specs->complex_p;
7439 if (pedantic && !flag_isoc99 && !in_system_header)
7440 pedwarn ("ISO C90 does not support complex types");
7441 if (specs->typespec_word == cts_void)
7442 error ("both %<complex%> and %<void%> in "
7443 "declaration specifiers");
7444 else if (specs->typespec_word == cts_bool)
7445 error ("both %<complex%> and %<_Bool%> in "
7446 "declaration specifiers");
7447 else if (specs->typespec_word == cts_dfloat32)
7448 error ("both %<complex%> and %<_Decimal32%> in "
7449 "declaration specifiers");
7450 else if (specs->typespec_word == cts_dfloat64)
7451 error ("both %<complex%> and %<_Decimal64%> in "
7452 "declaration specifiers");
7453 else if (specs->typespec_word == cts_dfloat128)
7454 error ("both %<complex%> and %<_Decimal128%> in "
7455 "declaration specifiers");
7457 specs->complex_p = true;
7464 error ("duplicate %qE", type);
7470 /* "void", "_Bool", "char", "int", "float" or "double". */
7471 if (specs->typespec_word != cts_none)
7473 error ("two or more data types in declaration specifiers");
7480 error ("both %<long%> and %<void%> in "
7481 "declaration specifiers");
7482 else if (specs->short_p)
7483 error ("both %<short%> and %<void%> in "
7484 "declaration specifiers");
7485 else if (specs->signed_p)
7486 error ("both %<signed%> and %<void%> in "
7487 "declaration specifiers");
7488 else if (specs->unsigned_p)
7489 error ("both %<unsigned%> and %<void%> in "
7490 "declaration specifiers");
7491 else if (specs->complex_p)
7492 error ("both %<complex%> and %<void%> in "
7493 "declaration specifiers");
7495 specs->typespec_word = cts_void;
7499 error ("both %<long%> and %<_Bool%> in "
7500 "declaration specifiers");
7501 else if (specs->short_p)
7502 error ("both %<short%> and %<_Bool%> in "
7503 "declaration specifiers");
7504 else if (specs->signed_p)
7505 error ("both %<signed%> and %<_Bool%> in "
7506 "declaration specifiers");
7507 else if (specs->unsigned_p)
7508 error ("both %<unsigned%> and %<_Bool%> in "
7509 "declaration specifiers");
7510 else if (specs->complex_p)
7511 error ("both %<complex%> and %<_Bool%> in "
7512 "declaration specifiers");
7514 specs->typespec_word = cts_bool;
7518 error ("both %<long%> and %<char%> in "
7519 "declaration specifiers");
7520 else if (specs->short_p)
7521 error ("both %<short%> and %<char%> in "
7522 "declaration specifiers");
7524 specs->typespec_word = cts_char;
7527 specs->typespec_word = cts_int;
7531 error ("both %<long%> and %<float%> in "
7532 "declaration specifiers");
7533 else if (specs->short_p)
7534 error ("both %<short%> and %<float%> in "
7535 "declaration specifiers");
7536 else if (specs->signed_p)
7537 error ("both %<signed%> and %<float%> in "
7538 "declaration specifiers");
7539 else if (specs->unsigned_p)
7540 error ("both %<unsigned%> and %<float%> in "
7541 "declaration specifiers");
7543 specs->typespec_word = cts_float;
7546 if (specs->long_long_p)
7547 error ("both %<long long%> and %<double%> in "
7548 "declaration specifiers");
7549 else if (specs->short_p)
7550 error ("both %<short%> and %<double%> in "
7551 "declaration specifiers");
7552 else if (specs->signed_p)
7553 error ("both %<signed%> and %<double%> in "
7554 "declaration specifiers");
7555 else if (specs->unsigned_p)
7556 error ("both %<unsigned%> and %<double%> in "
7557 "declaration specifiers");
7559 specs->typespec_word = cts_double;
7566 if (i == RID_DFLOAT32)
7568 else if (i == RID_DFLOAT64)
7571 str = "_Decimal128";
7572 if (specs->long_long_p)
7573 error ("both %<long long%> and %<%s%> in "
7574 "declaration specifiers", str);
7576 error ("both %<long%> and %<%s%> in "
7577 "declaration specifiers", str);
7578 else if (specs->short_p)
7579 error ("both %<short%> and %<%s%> in "
7580 "declaration specifiers", str);
7581 else if (specs->signed_p)
7582 error ("both %<signed%> and %<%s%> in "
7583 "declaration specifiers", str);
7584 else if (specs->unsigned_p)
7585 error ("both %<unsigned%> and %<%s%> in "
7586 "declaration specifiers", str);
7587 else if (specs->complex_p)
7588 error ("both %<complex%> and %<%s%> in "
7589 "declaration specifiers", str);
7590 else if (i == RID_DFLOAT32)
7591 specs->typespec_word = cts_dfloat32;
7592 else if (i == RID_DFLOAT64)
7593 specs->typespec_word = cts_dfloat64;
7595 specs->typespec_word = cts_dfloat128;
7597 if (!targetm.decimal_float_supported_p ())
7598 error ("decimal floating point not supported for this target");
7600 pedwarn ("ISO C does not support decimal floating point");
7603 /* ObjC reserved word "id", handled below. */
7609 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7610 form of ObjC type, cases such as "int" and "long" being handled
7611 above), a TYPE (struct, union, enum and typeof specifiers) or an
7612 ERROR_MARK. In none of these cases may there have previously
7613 been any type specifiers. */
7614 if (specs->type || specs->typespec_word != cts_none
7615 || specs->long_p || specs->short_p || specs->signed_p
7616 || specs->unsigned_p || specs->complex_p)
7617 error ("two or more data types in declaration specifiers");
7618 else if (TREE_CODE (type) == TYPE_DECL)
7620 if (TREE_TYPE (type) == error_mark_node)
7621 ; /* Allow the type to default to int to avoid cascading errors. */
7624 specs->type = TREE_TYPE (type);
7625 specs->decl_attr = DECL_ATTRIBUTES (type);
7626 specs->typedef_p = true;
7627 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7630 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7632 tree t = lookup_name (type);
7633 if (!t || TREE_CODE (t) != TYPE_DECL)
7634 error ("%qE fails to be a typedef or built in type", type);
7635 else if (TREE_TYPE (t) == error_mark_node)
7638 specs->type = TREE_TYPE (t);
7640 else if (TREE_CODE (type) != ERROR_MARK)
7642 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7643 specs->tag_defined_p = true;
7644 if (spec.kind == ctsk_typeof)
7645 specs->typedef_p = true;
7652 /* Add the storage class specifier or function specifier SCSPEC to the
7653 declaration specifiers SPECS, returning SPECS. */
7655 struct c_declspecs *
7656 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7659 enum c_storage_class n = csc_none;
7661 specs->declspecs_seen_p = true;
7662 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7663 && C_IS_RESERVED_WORD (scspec));
7664 i = C_RID_CODE (scspec);
7665 if (extra_warnings && specs->non_sc_seen_p)
7666 warning (OPT_Wextra, "%qE is not at beginning of declaration", scspec);
7670 /* C99 permits duplicate inline. Although of doubtful utility,
7671 it seems simplest to permit it in gnu89 mode as well, as
7672 there is also little utility in maintaining this as a
7673 difference between gnu89 and C99 inline. */
7675 specs->inline_p = true;
7678 dupe = specs->thread_p;
7679 if (specs->storage_class == csc_auto)
7680 error ("%<__thread%> used with %<auto%>");
7681 else if (specs->storage_class == csc_register)
7682 error ("%<__thread%> used with %<register%>");
7683 else if (specs->storage_class == csc_typedef)
7684 error ("%<__thread%> used with %<typedef%>");
7686 specs->thread_p = true;
7693 /* Diagnose "__thread extern". */
7694 if (specs->thread_p)
7695 error ("%<__thread%> before %<extern%>");
7702 /* Diagnose "__thread static". */
7703 if (specs->thread_p)
7704 error ("%<__thread%> before %<static%>");
7712 if (n != csc_none && n == specs->storage_class)
7715 error ("duplicate %qE", scspec);
7718 if (specs->storage_class != csc_none && n != specs->storage_class)
7720 error ("multiple storage classes in declaration specifiers");
7724 specs->storage_class = n;
7725 if (n != csc_extern && n != csc_static && specs->thread_p)
7727 error ("%<__thread%> used with %qE", scspec);
7728 specs->thread_p = false;
7735 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7738 struct c_declspecs *
7739 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7741 specs->attrs = chainon (attrs, specs->attrs);
7742 specs->declspecs_seen_p = true;
7746 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7747 specifiers with any other type specifier to determine the resulting
7748 type. This is where ISO C checks on complex types are made, since
7749 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7752 struct c_declspecs *
7753 finish_declspecs (struct c_declspecs *specs)
7755 /* If a type was specified as a whole, we have no modifiers and are
7757 if (specs->type != NULL_TREE)
7759 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7760 && !specs->signed_p && !specs->unsigned_p
7761 && !specs->complex_p);
7765 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7766 has been specified, treat it as "int" unless "_Complex" is
7767 present and there are no other specifiers. If we just have
7768 "_Complex", it is equivalent to "_Complex double", but e.g.
7769 "_Complex short" is equivalent to "_Complex short int". */
7770 if (specs->typespec_word == cts_none)
7772 if (specs->long_p || specs->short_p
7773 || specs->signed_p || specs->unsigned_p)
7775 specs->typespec_word = cts_int;
7777 else if (specs->complex_p)
7779 specs->typespec_word = cts_double;
7781 pedwarn ("ISO C does not support plain %<complex%> meaning "
7782 "%<double complex%>");
7786 specs->typespec_word = cts_int;
7787 specs->default_int_p = true;
7788 /* We don't diagnose this here because grokdeclarator will
7789 give more specific diagnostics according to whether it is
7790 a function definition. */
7794 /* If "signed" was specified, record this to distinguish "int" and
7795 "signed int" in the case of a bit-field with
7796 -funsigned-bitfields. */
7797 specs->explicit_signed_p = specs->signed_p;
7799 /* Now compute the actual type. */
7800 switch (specs->typespec_word)
7803 gcc_assert (!specs->long_p && !specs->short_p
7804 && !specs->signed_p && !specs->unsigned_p
7805 && !specs->complex_p);
7806 specs->type = void_type_node;
7809 gcc_assert (!specs->long_p && !specs->short_p
7810 && !specs->signed_p && !specs->unsigned_p
7811 && !specs->complex_p);
7812 specs->type = boolean_type_node;
7815 gcc_assert (!specs->long_p && !specs->short_p);
7816 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7817 if (specs->signed_p)
7818 specs->type = signed_char_type_node;
7819 else if (specs->unsigned_p)
7820 specs->type = unsigned_char_type_node;
7822 specs->type = char_type_node;
7823 if (specs->complex_p)
7826 pedwarn ("ISO C does not support complex integer types");
7827 specs->type = build_complex_type (specs->type);
7831 gcc_assert (!(specs->long_p && specs->short_p));
7832 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7833 if (specs->long_long_p)
7834 specs->type = (specs->unsigned_p
7835 ? long_long_unsigned_type_node
7836 : long_long_integer_type_node);
7837 else if (specs->long_p)
7838 specs->type = (specs->unsigned_p
7839 ? long_unsigned_type_node
7840 : long_integer_type_node);
7841 else if (specs->short_p)
7842 specs->type = (specs->unsigned_p
7843 ? short_unsigned_type_node
7844 : short_integer_type_node);
7846 specs->type = (specs->unsigned_p
7847 ? unsigned_type_node
7848 : integer_type_node);
7849 if (specs->complex_p)
7852 pedwarn ("ISO C does not support complex integer types");
7853 specs->type = build_complex_type (specs->type);
7857 gcc_assert (!specs->long_p && !specs->short_p
7858 && !specs->signed_p && !specs->unsigned_p);
7859 specs->type = (specs->complex_p
7860 ? complex_float_type_node
7864 gcc_assert (!specs->long_long_p && !specs->short_p
7865 && !specs->signed_p && !specs->unsigned_p);
7868 specs->type = (specs->complex_p
7869 ? complex_long_double_type_node
7870 : long_double_type_node);
7874 specs->type = (specs->complex_p
7875 ? complex_double_type_node
7876 : double_type_node);
7882 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7883 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
7884 if (specs->typespec_word == cts_dfloat32)
7885 specs->type = dfloat32_type_node;
7886 else if (specs->typespec_word == cts_dfloat64)
7887 specs->type = dfloat64_type_node;
7889 specs->type = dfloat128_type_node;
7898 /* Synthesize a function which calls all the global ctors or global
7899 dtors in this file. This is only used for targets which do not
7900 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7902 build_cdtor (int method_type, tree cdtors)
7909 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7910 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7913 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7916 /* A subroutine of c_write_global_declarations. Perform final processing
7917 on one file scope's declarations (or the external scope's declarations),
7921 c_write_global_declarations_1 (tree globals)
7926 /* Process the decls in the order they were written. */
7927 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7929 /* Check for used but undefined static functions using the C
7930 standard's definition of "used", and set TREE_NO_WARNING so
7931 that check_global_declarations doesn't repeat the check. */
7932 if (TREE_CODE (decl) == FUNCTION_DECL
7933 && DECL_INITIAL (decl) == 0
7934 && DECL_EXTERNAL (decl)
7935 && !TREE_PUBLIC (decl)
7936 && C_DECL_USED (decl))
7938 pedwarn ("%q+F used but never defined", decl);
7939 TREE_NO_WARNING (decl) = 1;
7942 wrapup_global_declaration_1 (decl);
7948 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7949 reconsider |= wrapup_global_declaration_2 (decl);
7953 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7954 check_global_declaration_1 (decl);
7957 /* A subroutine of c_write_global_declarations Emit debug information for each
7958 of the declarations in GLOBALS. */
7961 c_write_global_declarations_2 (tree globals)
7965 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
7966 debug_hooks->global_decl (decl);
7969 /* Preserve the external declarations scope across a garbage collect. */
7970 static GTY(()) tree ext_block;
7973 c_write_global_declarations (void)
7977 /* We don't want to do this if generating a PCH. */
7981 /* Don't waste time on further processing if -fsyntax-only or we've
7982 encountered errors. */
7983 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7986 /* Close the external scope. */
7987 ext_block = pop_scope ();
7989 gcc_assert (!current_scope);
7993 tree tmp = BLOCK_VARS (ext_block);
7995 FILE * stream = dump_begin (TDI_tu, &flags);
7998 dump_node (tmp, flags & ~TDF_SLIM, stream);
7999 dump_end (TDI_tu, stream);
8003 /* Process all file scopes in this compilation, and the external_scope,
8004 through wrapup_global_declarations and check_global_declarations. */
8005 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8006 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
8007 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
8009 /* Generate functions to call static constructors and destructors
8010 for targets that do not support .ctors/.dtors sections. These
8011 functions have magic names which are detected by collect2. */
8012 build_cdtor ('I', static_ctors); static_ctors = 0;
8013 build_cdtor ('D', static_dtors); static_dtors = 0;
8015 /* We're done parsing; proceed to optimize and emit assembly.
8016 FIXME: shouldn't be the front end's responsibility to call this. */
8019 /* After cgraph has had a chance to emit everything that's going to
8020 be emitted, output debug information for globals. */
8021 if (errorcount == 0 && sorrycount == 0)
8023 timevar_push (TV_SYMOUT);
8024 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8025 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
8026 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
8027 timevar_pop (TV_SYMOUT);
8033 #include "gt-c-decl.h"