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 /* APPLE LOCAL blocks 6339747 */
75 BLOCKDEF, /* Block literal declaration */
76 PARM, /* Declaration of parm before function body */
77 FIELD, /* Declaration inside struct or union */
78 TYPENAME}; /* Typename (inside cast or sizeof) */
81 /* Nonzero if we have seen an invalid cross reference
82 to a struct, union, or enum, but not yet printed the message. */
83 tree pending_invalid_xref;
85 /* File and line to appear in the eventual error message. */
86 location_t pending_invalid_xref_location;
88 /* True means we've initialized exception handling. */
89 bool c_eh_initialized_p;
91 /* While defining an enum type, this is 1 plus the last enumerator
92 constant value. Note that will do not have to save this or `enum_overflow'
93 around nested function definition since such a definition could only
94 occur in an enum value expression and we don't use these variables in
97 static tree enum_next_value;
99 /* Nonzero means that there was overflow computing enum_next_value. */
101 static int enum_overflow;
103 /* The file and line that the prototype came from if this is an
104 old-style definition; used for diagnostics in
105 store_parm_decls_oldstyle. */
107 static location_t current_function_prototype_locus;
109 /* Whether this prototype was built-in. */
111 static bool current_function_prototype_built_in;
113 /* The argument type information of this prototype. */
115 static tree current_function_prototype_arg_types;
117 /* The argument information structure for the function currently being
120 static struct c_arg_info *current_function_arg_info;
122 /* The obstack on which parser and related data structures, which are
123 not live beyond their top-level declaration or definition, are
125 struct obstack parser_obstack;
127 /* The current statement tree. */
129 static GTY(()) struct stmt_tree_s c_stmt_tree;
131 /* State saving variables. */
135 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
136 included in this invocation. Note that the current translation
137 unit is not included in this list. */
139 static GTY(()) tree all_translation_units;
141 /* A list of decls to be made automatically visible in each file scope. */
142 static GTY(()) tree visible_builtins;
144 /* Set to 0 at beginning of a function definition, set to 1 if
145 a return statement that specifies a return value is seen. */
147 int current_function_returns_value;
149 /* Set to 0 at beginning of a function definition, set to 1 if
150 a return statement with no argument is seen. */
152 int current_function_returns_null;
154 /* Set to 0 at beginning of a function definition, set to 1 if
155 a call to a noreturn function is seen. */
157 int current_function_returns_abnormally;
159 /* Set to nonzero by `grokdeclarator' for a function
160 whose return type is defaulted, if warnings for this are desired. */
162 static int warn_about_return_type;
164 /* Nonzero when the current toplevel function contains a declaration
165 of a nested function which is never defined. */
167 static bool undef_nested_function;
169 /* True means global_bindings_p should return false even if the scope stack
170 says we are in file scope. */
171 bool c_override_global_bindings_to_false;
174 /* Each c_binding structure describes one binding of an identifier to
175 a decl. All the decls in a scope - irrespective of namespace - are
176 chained together by the ->prev field, which (as the name implies)
177 runs in reverse order. All the decls in a given namespace bound to
178 a given identifier are chained by the ->shadowed field, which runs
179 from inner to outer scopes.
181 The ->decl field usually points to a DECL node, but there are two
182 exceptions. In the namespace of type tags, the bound entity is a
183 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
184 identifier is encountered, it is bound to error_mark_node to
185 suppress further errors about that identifier in the current
188 The ->type field stores the type of the declaration in this scope;
189 if NULL, the type is the type of the ->decl field. This is only of
190 relevance for objects with external or internal linkage which may
191 be redeclared in inner scopes, forming composite types that only
192 persist for the duration of those scopes. In the external scope,
193 this stores the composite of all the types declared for this
194 object, visible or not. The ->inner_comp field (used only at file
195 scope) stores whether an incomplete array type at file scope was
196 completed at an inner scope to an array size other than 1.
198 The depth field is copied from the scope structure that holds this
199 decl. It is used to preserve the proper ordering of the ->shadowed
200 field (see bind()) and also for a handful of special-case checks.
201 Finally, the invisible bit is true for a decl which should be
202 ignored for purposes of normal name lookup, and the nested bit is
203 true for a decl that's been bound a second time in an inner scope;
204 in all such cases, the binding in the outer scope will have its
205 invisible bit true. */
207 struct c_binding GTY((chain_next ("%h.prev")))
209 tree decl; /* the decl bound */
210 tree type; /* the type in this scope */
211 tree id; /* the identifier it's bound to */
212 struct c_binding *prev; /* the previous decl in this scope */
213 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
214 unsigned int depth : 28; /* depth of this scope */
215 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
216 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
217 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
220 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
221 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
222 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
223 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
225 #define I_SYMBOL_BINDING(node) \
226 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
227 #define I_SYMBOL_DECL(node) \
228 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
230 #define I_TAG_BINDING(node) \
231 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
232 #define I_TAG_DECL(node) \
233 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
235 #define I_LABEL_BINDING(node) \
236 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
237 #define I_LABEL_DECL(node) \
238 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
240 /* Each C symbol points to three linked lists of c_binding structures.
241 These describe the values of the identifier in the three different
242 namespaces defined by the language. */
244 struct lang_identifier GTY(())
246 struct c_common_identifier common_id;
247 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
248 struct c_binding *tag_binding; /* struct/union/enum tags */
249 struct c_binding *label_binding; /* labels */
252 /* Validate c-lang.c's assumptions. */
253 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
254 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
256 /* The resulting tree type. */
259 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
260 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)")))
262 union tree_node GTY ((tag ("0"),
263 desc ("tree_node_structure (&%h)")))
265 struct lang_identifier GTY ((tag ("1"))) identifier;
268 /* Each c_scope structure describes the complete contents of one
269 scope. Four scopes are distinguished specially: the innermost or
270 current scope, the innermost function scope, the file scope (always
271 the second to outermost) and the outermost or external scope.
273 Most declarations are recorded in the current scope.
275 All normal label declarations are recorded in the innermost
276 function scope, as are bindings of undeclared identifiers to
277 error_mark_node. (GCC permits nested functions as an extension,
278 hence the 'innermost' qualifier.) Explicitly declared labels
279 (using the __label__ extension) appear in the current scope.
281 Being in the file scope (current_scope == file_scope) causes
282 special behavior in several places below. Also, under some
283 conditions the Objective-C front end records declarations in the
284 file scope even though that isn't the current scope.
286 All declarations with external linkage are recorded in the external
287 scope, even if they aren't visible there; this models the fact that
288 such declarations are visible to the entire program, and (with a
289 bit of cleverness, see pushdecl) allows diagnosis of some violations
290 of C99 6.2.2p7 and 6.2.7p2:
292 If, within the same translation unit, the same identifier appears
293 with both internal and external linkage, the behavior is
296 All declarations that refer to the same object or function shall
297 have compatible type; otherwise, the behavior is undefined.
299 Initially only the built-in declarations, which describe compiler
300 intrinsic functions plus a subset of the standard library, are in
303 The order of the blocks list matters, and it is frequently appended
304 to. To avoid having to walk all the way to the end of the list on
305 each insertion, or reverse the list later, we maintain a pointer to
306 the last list entry. (FIXME: It should be feasible to use a reversed
309 The bindings list is strictly in reverse order of declarations;
310 pop_scope relies on this. */
313 struct c_scope GTY((chain_next ("%h.outer")))
315 /* The scope containing this one. */
316 struct c_scope *outer;
318 /* The next outermost function scope. */
319 struct c_scope *outer_function;
321 /* All bindings in this scope. */
322 struct c_binding *bindings;
324 /* For each scope (except the global one), a chain of BLOCK nodes
325 for all the scopes that were entered and exited one level down. */
329 /* The depth of this scope. Used to keep the ->shadowed chain of
330 bindings sorted innermost to outermost. */
331 unsigned int depth : 28;
333 /* True if we are currently filling this scope with parameter
335 BOOL_BITFIELD parm_flag : 1;
337 /* True if we saw [*] in this scope. Used to give an error messages
338 if these appears in a function definition. */
339 BOOL_BITFIELD had_vla_unspec : 1;
341 /* True if we already complained about forward parameter decls
342 in this scope. This prevents double warnings on
343 foo (int a; int b; ...) */
344 BOOL_BITFIELD warned_forward_parm_decls : 1;
346 /* True if this is the outermost block scope of a function body.
347 This scope contains the parameters, the local variables declared
348 in the outermost block, and all the labels (except those in
349 nested functions, or declared at block scope with __label__). */
350 BOOL_BITFIELD function_body : 1;
352 /* True means make a BLOCK for this scope no matter what. */
353 BOOL_BITFIELD keep : 1;
356 /* The scope currently in effect. */
358 static GTY(()) struct c_scope *current_scope;
360 /* The innermost function scope. Ordinary (not explicitly declared)
361 labels, bindings to error_mark_node, and the lazily-created
362 bindings of __func__ and its friends get this scope. */
364 static GTY(()) struct c_scope *current_function_scope;
366 /* The C file scope. This is reset for each input translation unit. */
368 static GTY(()) struct c_scope *file_scope;
370 /* The outermost scope. This is used for all declarations with
371 external linkage, and only these, hence the name. */
373 static GTY(()) struct c_scope *external_scope;
375 /* A chain of c_scope structures awaiting reuse. */
377 static GTY((deletable)) struct c_scope *scope_freelist;
379 /* A chain of c_binding structures awaiting reuse. */
381 static GTY((deletable)) struct c_binding *binding_freelist;
383 /* Append VAR to LIST in scope SCOPE. */
384 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
385 struct c_scope *s_ = (scope); \
387 if (s_->list##_last) \
388 TREE_CHAIN (s_->list##_last) = d_; \
391 s_->list##_last = d_; \
394 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
395 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
396 struct c_scope *t_ = (tscope); \
397 struct c_scope *f_ = (fscope); \
399 TREE_CHAIN (t_->to##_last) = f_->from; \
402 t_->to##_last = f_->from##_last; \
405 /* True means unconditionally make a BLOCK for the next scope pushed. */
407 static bool keep_next_level_flag;
409 /* True means the next call to push_scope will be the outermost scope
410 of a function body, so do not push a new scope, merely cease
411 expecting parameter decls. */
413 static bool next_is_function_body;
415 /* Functions called automatically at the beginning and end of execution. */
417 static GTY(()) tree static_ctors;
418 static GTY(()) tree static_dtors;
420 /* Forward declarations. */
421 static tree lookup_name_in_scope (tree, struct c_scope *);
422 static tree c_make_fname_decl (tree, int);
423 static tree grokdeclarator (const struct c_declarator *,
424 struct c_declspecs *,
425 enum decl_context, bool, tree *);
426 static tree grokparms (struct c_arg_info *, bool);
427 static void layout_array_type (tree);
429 /* T is a statement. Add it to the statement-tree. This is the
430 C/ObjC version--C++ has a slightly different version of this
436 enum tree_code code = TREE_CODE (t);
438 if (EXPR_P (t) && code != LABEL_EXPR)
440 if (!EXPR_HAS_LOCATION (t))
441 SET_EXPR_LOCATION (t, input_location);
444 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
445 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
447 /* Add T to the statement-tree. Non-side-effect statements need to be
448 recorded during statement expressions. */
449 append_to_statement_list_force (t, &cur_stmt_list);
454 /* States indicating how grokdeclarator() should handle declspecs marked
455 with __attribute__((deprecated)). An object declared as
456 __attribute__((deprecated)) suppresses warnings of uses of other
458 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
459 /* Also add an __attribute__((unavailable)). An object declared as
460 __attribute__((unavailable)) suppresses any reports of being
461 declared with unavailable or deprecated items. */
462 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
464 enum deprecated_states {
467 /* APPLE LOCAL "unavailable" attribute (radar 2809697) */
468 , DEPRECATED_UNAVAILABLE_SUPPRESS
471 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
474 c_print_identifier (FILE *file, tree node, int indent)
476 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
477 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
478 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
479 if (C_IS_RESERVED_WORD (node))
481 tree rid = ridpointers[C_RID_CODE (node)];
482 indent_to (file, indent + 4);
483 fprintf (file, "rid %p \"%s\"",
484 (void *) rid, IDENTIFIER_POINTER (rid));
488 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
489 which may be any of several kinds of DECL or TYPE or error_mark_node,
490 in the scope SCOPE. */
492 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
494 struct c_binding *b, **here;
496 if (binding_freelist)
498 b = binding_freelist;
499 binding_freelist = b->prev;
502 b = GGC_NEW (struct c_binding);
507 b->depth = scope->depth;
508 b->invisible = invisible;
514 b->prev = scope->bindings;
520 switch (TREE_CODE (decl))
522 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
525 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
531 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
537 /* Locate the appropriate place in the chain of shadowed decls
538 to insert this binding. Normally, scope == current_scope and
539 this does nothing. */
540 while (*here && (*here)->depth > scope->depth)
541 here = &(*here)->shadowed;
547 /* Clear the binding structure B, stick it on the binding_freelist,
548 and return the former value of b->prev. This is used by pop_scope
549 and get_parm_info to iterate destructively over all the bindings
550 from a given scope. */
551 static struct c_binding *
552 free_binding_and_advance (struct c_binding *b)
554 struct c_binding *prev = b->prev;
556 memset (b, 0, sizeof (struct c_binding));
557 b->prev = binding_freelist;
558 binding_freelist = b;
564 /* Hook called at end of compilation to assume 1 elt
565 for a file-scope tentative array defn that wasn't complete before. */
568 c_finish_incomplete_decl (tree decl)
570 if (TREE_CODE (decl) == VAR_DECL)
572 tree type = TREE_TYPE (decl);
573 if (type != error_mark_node
574 && TREE_CODE (type) == ARRAY_TYPE
575 && !DECL_EXTERNAL (decl)
576 && TYPE_DOMAIN (type) == 0)
578 warning (0, "array %q+D assumed to have one element", decl);
580 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
582 layout_decl (decl, 0);
587 /* The Objective-C front-end often needs to determine the current scope. */
590 objc_get_current_scope (void)
592 return current_scope;
595 /* The following function is used only by Objective-C. It needs to live here
596 because it accesses the innards of c_scope. */
599 objc_mark_locals_volatile (void *enclosing_blk)
601 struct c_scope *scope;
604 for (scope = current_scope;
605 scope && scope != enclosing_blk;
606 scope = scope->outer)
608 for (b = scope->bindings; b; b = b->prev)
609 objc_volatilize_decl (b->decl);
611 /* Do not climb up past the current function. */
612 if (scope->function_body)
617 /* Nonzero if we are currently in file scope. */
620 global_bindings_p (void)
622 return current_scope == file_scope && !c_override_global_bindings_to_false;
626 keep_next_level (void)
628 keep_next_level_flag = true;
631 /* Identify this scope as currently being filled with parameters. */
634 declare_parm_level (void)
636 current_scope->parm_flag = true;
642 if (next_is_function_body)
644 /* This is the transition from the parameters to the top level
645 of the function body. These are the same scope
646 (C99 6.2.1p4,6) so we do not push another scope structure.
647 next_is_function_body is set only by store_parm_decls, which
648 in turn is called when and only when we are about to
649 encounter the opening curly brace for the function body.
651 The outermost block of a function always gets a BLOCK node,
652 because the debugging output routines expect that each
653 function has at least one BLOCK. */
654 current_scope->parm_flag = false;
655 current_scope->function_body = true;
656 current_scope->keep = true;
657 current_scope->outer_function = current_function_scope;
658 current_function_scope = current_scope;
660 keep_next_level_flag = false;
661 next_is_function_body = false;
665 struct c_scope *scope;
668 scope = scope_freelist;
669 scope_freelist = scope->outer;
672 scope = GGC_CNEW (struct c_scope);
674 scope->keep = keep_next_level_flag;
675 scope->outer = current_scope;
676 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
678 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
680 if (current_scope && scope->depth == 0)
683 sorry ("GCC supports only %u nested scopes", scope->depth);
686 current_scope = scope;
687 keep_next_level_flag = false;
691 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
694 set_type_context (tree type, tree context)
696 for (type = TYPE_MAIN_VARIANT (type); type;
697 type = TYPE_NEXT_VARIANT (type))
698 TYPE_CONTEXT (type) = context;
701 /* Exit a scope. Restore the state of the identifier-decl mappings
702 that were in effect when this scope was entered. Return a BLOCK
703 node containing all the DECLs in this scope that are of interest
704 to debug info generation. */
709 struct c_scope *scope = current_scope;
710 tree block, context, p;
713 bool functionbody = scope->function_body;
714 bool keep = functionbody || scope->keep || scope->bindings;
716 c_end_vm_scope (scope->depth);
718 /* If appropriate, create a BLOCK to record the decls for the life
723 block = make_node (BLOCK);
724 BLOCK_SUBBLOCKS (block) = scope->blocks;
725 TREE_USED (block) = 1;
727 /* In each subblock, record that this is its superior. */
728 for (p = scope->blocks; p; p = TREE_CHAIN (p))
729 BLOCK_SUPERCONTEXT (p) = block;
731 BLOCK_VARS (block) = 0;
734 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
735 scope must be set so that they point to the appropriate
736 construct, i.e. either to the current FUNCTION_DECL node, or
737 else to the BLOCK node we just constructed.
739 Note that for tagged types whose scope is just the formal
740 parameter list for some function type specification, we can't
741 properly set their TYPE_CONTEXTs here, because we don't have a
742 pointer to the appropriate FUNCTION_TYPE node readily available
743 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
744 type nodes get set in `grokdeclarator' as soon as we have created
745 the FUNCTION_TYPE node which will represent the "scope" for these
746 "parameter list local" tagged types. */
747 if (scope->function_body)
748 context = current_function_decl;
749 else if (scope == file_scope)
751 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
752 TREE_CHAIN (file_decl) = all_translation_units;
753 all_translation_units = file_decl;
759 /* Clear all bindings in this scope. */
760 for (b = scope->bindings; b; b = free_binding_and_advance (b))
763 switch (TREE_CODE (p))
766 /* Warnings for unused labels, errors for undefined labels. */
767 if (TREE_USED (p) && !DECL_INITIAL (p))
769 error ("label %q+D used but not defined", p);
770 DECL_INITIAL (p) = error_mark_node;
772 else if (!TREE_USED (p) && warn_unused_label)
774 if (DECL_INITIAL (p))
775 warning (0, "label %q+D defined but not used", p);
777 warning (0, "label %q+D declared but not defined", p);
779 /* Labels go in BLOCK_VARS. */
780 TREE_CHAIN (p) = BLOCK_VARS (block);
781 BLOCK_VARS (block) = p;
782 gcc_assert (I_LABEL_BINDING (b->id) == b);
783 I_LABEL_BINDING (b->id) = b->shadowed;
789 set_type_context (p, context);
791 /* Types may not have tag-names, in which case the type
792 appears in the bindings list with b->id NULL. */
795 gcc_assert (I_TAG_BINDING (b->id) == b);
796 I_TAG_BINDING (b->id) = b->shadowed;
801 /* Propagate TREE_ADDRESSABLE from nested functions to their
802 containing functions. */
803 if (!TREE_ASM_WRITTEN (p)
804 && DECL_INITIAL (p) != 0
805 && TREE_ADDRESSABLE (p)
806 && DECL_ABSTRACT_ORIGIN (p) != 0
807 && DECL_ABSTRACT_ORIGIN (p) != p)
808 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
809 if (!DECL_EXTERNAL (p)
810 && DECL_INITIAL (p) == 0
811 && scope != file_scope
812 && scope != external_scope)
814 error ("nested function %q+D declared but never defined", p);
815 undef_nested_function = true;
817 /* C99 6.7.4p6: "a function with external linkage... declared
818 with an inline function specifier ... shall also be defined in the
819 same translation unit." */
820 else if (DECL_DECLARED_INLINE_P (p)
823 && !flag_gnu89_inline)
824 pedwarn ("inline function %q+D declared but never defined", p);
829 /* Warnings for unused variables. */
831 && !TREE_NO_WARNING (p)
832 && !DECL_IN_SYSTEM_HEADER (p)
834 && !DECL_ARTIFICIAL (p)
835 && scope != file_scope
836 && scope != external_scope)
837 warning (OPT_Wunused_variable, "unused variable %q+D", p);
841 error ("type of array %q+D completed incompatibly with"
842 " implicit initialization", p);
849 /* All of these go in BLOCK_VARS, but only if this is the
850 binding in the home scope. */
853 TREE_CHAIN (p) = BLOCK_VARS (block);
854 BLOCK_VARS (block) = p;
856 /* If this is the file scope, and we are processing more
857 than one translation unit in this compilation, set
858 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
859 This makes same_translation_unit_p work, and causes
860 static declarations to be given disambiguating suffixes. */
861 if (scope == file_scope && num_in_fnames > 1)
863 DECL_CONTEXT (p) = context;
864 if (TREE_CODE (p) == TYPE_DECL)
865 set_type_context (TREE_TYPE (p), context);
869 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
870 already been put there by store_parm_decls. Unused-
871 parameter warnings are handled by function.c.
872 error_mark_node obviously does not go in BLOCK_VARS and
873 does not get unused-variable warnings. */
876 /* It is possible for a decl not to have a name. We get
877 here with b->id NULL in this case. */
880 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
881 I_SYMBOL_BINDING (b->id) = b->shadowed;
882 if (b->shadowed && b->shadowed->type)
883 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
893 /* Dispose of the block that we just made inside some higher level. */
894 if ((scope->function_body || scope == file_scope) && context)
896 DECL_INITIAL (context) = block;
897 BLOCK_SUPERCONTEXT (block) = context;
899 else if (scope->outer)
902 SCOPE_LIST_APPEND (scope->outer, blocks, block);
903 /* If we did not make a block for the scope just exited, any
904 blocks made for inner scopes must be carried forward so they
905 will later become subblocks of something else. */
906 else if (scope->blocks)
907 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
910 /* Pop the current scope, and free the structure for reuse. */
911 current_scope = scope->outer;
912 if (scope->function_body)
913 current_function_scope = scope->outer_function;
915 memset (scope, 0, sizeof (struct c_scope));
916 scope->outer = scope_freelist;
917 scope_freelist = scope;
923 push_file_scope (void)
931 file_scope = current_scope;
933 start_fname_decls ();
935 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
936 bind (DECL_NAME (decl), decl, file_scope,
937 /*invisible=*/false, /*nested=*/true);
941 pop_file_scope (void)
943 /* In case there were missing closebraces, get us back to the global
945 while (current_scope != file_scope)
948 /* __FUNCTION__ is defined at file scope (""). This
949 call may not be necessary as my tests indicate it
950 still works without it. */
951 finish_fname_decls ();
953 /* This is the point to write out a PCH if we're doing that.
954 In that case we do not want to do anything else. */
957 c_common_write_pch ();
961 /* Pop off the file scope and close this translation unit. */
965 maybe_apply_pending_pragma_weaks ();
966 cgraph_finalize_compilation_unit ();
969 /* Insert BLOCK at the end of the list of subblocks of the current
970 scope. This is used when a BIND_EXPR is expanded, to handle the
971 BLOCK node inside the BIND_EXPR. */
974 insert_block (tree block)
976 TREE_USED (block) = 1;
977 SCOPE_LIST_APPEND (current_scope, blocks, block);
980 /* Push a definition or a declaration of struct, union or enum tag "name".
981 "type" should be the type node.
982 We assume that the tag "name" is not already defined.
984 Note that the definition may really be just a forward reference.
985 In that case, the TYPE_SIZE will be zero. */
988 pushtag (tree name, tree type)
990 /* Record the identifier as the type's name if it has none. */
991 if (name && !TYPE_NAME (type))
992 TYPE_NAME (type) = name;
993 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
995 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
996 tagged type we just added to the current scope. This fake
997 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
998 to output a representation of a tagged type, and it also gives
999 us a convenient place to record the "scope start" address for the
1002 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
1004 /* An approximation for now, so we can tell this is a function-scope tag.
1005 This will be updated in pop_scope. */
1006 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1009 /* Subroutine of compare_decls. Allow harmless mismatches in return
1010 and argument types provided that the type modes match. This function
1011 return a unified type given a suitable match, and 0 otherwise. */
1014 match_builtin_function_types (tree newtype, tree oldtype)
1016 tree newrettype, oldrettype;
1017 tree newargs, oldargs;
1018 tree trytype, tryargs;
1020 /* Accept the return type of the new declaration if same modes. */
1021 oldrettype = TREE_TYPE (oldtype);
1022 newrettype = TREE_TYPE (newtype);
1024 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1027 oldargs = TYPE_ARG_TYPES (oldtype);
1028 newargs = TYPE_ARG_TYPES (newtype);
1031 while (oldargs || newargs)
1035 || !TREE_VALUE (oldargs)
1036 || !TREE_VALUE (newargs)
1037 || TYPE_MODE (TREE_VALUE (oldargs))
1038 != TYPE_MODE (TREE_VALUE (newargs)))
1041 oldargs = TREE_CHAIN (oldargs);
1042 newargs = TREE_CHAIN (newargs);
1045 trytype = build_function_type (newrettype, tryargs);
1046 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1049 /* Subroutine of diagnose_mismatched_decls. Check for function type
1050 mismatch involving an empty arglist vs a nonempty one and give clearer
1053 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1054 tree newtype, tree oldtype)
1058 if (TREE_CODE (olddecl) != FUNCTION_DECL
1059 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1060 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1062 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1065 t = TYPE_ARG_TYPES (oldtype);
1067 t = TYPE_ARG_TYPES (newtype);
1068 for (; t; t = TREE_CHAIN (t))
1070 tree type = TREE_VALUE (t);
1072 if (TREE_CHAIN (t) == 0
1073 && TYPE_MAIN_VARIANT (type) != void_type_node)
1075 inform ("a parameter list with an ellipsis can%'t match "
1076 "an empty parameter name list declaration");
1080 if (c_type_promotes_to (type) != type)
1082 inform ("an argument type that has a default promotion can%'t match "
1083 "an empty parameter name list declaration");
1089 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1090 old-style function definition, NEWDECL is a prototype declaration.
1091 Diagnose inconsistencies in the argument list. Returns TRUE if
1092 the prototype is compatible, FALSE if not. */
1094 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1096 tree newargs, oldargs;
1099 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1101 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1102 newargs = TYPE_ARG_TYPES (newtype);
1107 tree oldargtype = TREE_VALUE (oldargs);
1108 tree newargtype = TREE_VALUE (newargs);
1110 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1113 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1114 newargtype = TYPE_MAIN_VARIANT (newargtype);
1116 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1119 /* Reaching the end of just one list means the two decls don't
1120 agree on the number of arguments. */
1121 if (END_OF_ARGLIST (oldargtype))
1123 error ("prototype for %q+D declares more arguments "
1124 "than previous old-style definition", newdecl);
1127 else if (END_OF_ARGLIST (newargtype))
1129 error ("prototype for %q+D declares fewer arguments "
1130 "than previous old-style definition", newdecl);
1134 /* Type for passing arg must be consistent with that declared
1136 else if (!comptypes (oldargtype, newargtype))
1138 error ("prototype for %q+D declares argument %d"
1139 " with incompatible type",
1144 oldargs = TREE_CHAIN (oldargs);
1145 newargs = TREE_CHAIN (newargs);
1149 /* If we get here, no errors were found, but do issue a warning
1150 for this poor-style construct. */
1151 warning (0, "prototype for %q+D follows non-prototype definition",
1154 #undef END_OF_ARGLIST
1157 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1158 first in a pair of mismatched declarations, using the diagnostic
1161 locate_old_decl (tree decl, void (*diag)(const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2))
1163 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1165 else if (DECL_INITIAL (decl))
1166 diag (G_("previous definition of %q+D was here"), decl);
1167 else if (C_DECL_IMPLICIT (decl))
1168 diag (G_("previous implicit declaration of %q+D was here"), decl);
1170 diag (G_("previous declaration of %q+D was here"), decl);
1173 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1174 Returns true if the caller should proceed to merge the two, false
1175 if OLDDECL should simply be discarded. As a side effect, issues
1176 all necessary diagnostics for invalid or poor-style combinations.
1177 If it returns true, writes the types of NEWDECL and OLDDECL to
1178 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1179 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1182 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1183 tree *newtypep, tree *oldtypep)
1185 tree newtype, oldtype;
1186 bool pedwarned = false;
1187 bool warned = false;
1190 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1191 && DECL_EXTERNAL (DECL))
1193 /* If we have error_mark_node for either decl or type, just discard
1194 the previous decl - we're in an error cascade already. */
1195 if (olddecl == error_mark_node || newdecl == error_mark_node)
1197 *oldtypep = oldtype = TREE_TYPE (olddecl);
1198 *newtypep = newtype = TREE_TYPE (newdecl);
1199 if (oldtype == error_mark_node || newtype == error_mark_node)
1202 /* Two different categories of symbol altogether. This is an error
1203 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1204 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1206 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1207 && DECL_BUILT_IN (olddecl)
1208 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1210 error ("%q+D redeclared as different kind of symbol", newdecl);
1211 locate_old_decl (olddecl, error);
1213 else if (TREE_PUBLIC (newdecl))
1214 warning (0, "built-in function %q+D declared as non-function",
1217 warning (OPT_Wshadow, "declaration of %q+D shadows "
1218 "a built-in function", newdecl);
1222 /* Enumerators have no linkage, so may only be declared once in a
1224 if (TREE_CODE (olddecl) == CONST_DECL)
1226 error ("redeclaration of enumerator %q+D", newdecl);
1227 locate_old_decl (olddecl, error);
1231 if (!comptypes (oldtype, newtype))
1233 if (TREE_CODE (olddecl) == FUNCTION_DECL
1234 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1236 /* Accept harmless mismatch in function types.
1237 This is for the ffs and fprintf builtins. */
1238 tree trytype = match_builtin_function_types (newtype, oldtype);
1240 if (trytype && comptypes (newtype, trytype))
1241 *oldtypep = oldtype = trytype;
1244 /* If types don't match for a built-in, throw away the
1245 built-in. No point in calling locate_old_decl here, it
1246 won't print anything. */
1247 warning (0, "conflicting types for built-in function %q+D",
1252 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1253 && DECL_IS_BUILTIN (olddecl))
1255 /* A conflicting function declaration for a predeclared
1256 function that isn't actually built in. Objective C uses
1257 these. The new declaration silently overrides everything
1258 but the volatility (i.e. noreturn) indication. See also
1259 below. FIXME: Make Objective C use normal builtins. */
1260 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1263 /* Permit void foo (...) to match int foo (...) if the latter is
1264 the definition and implicit int was used. See
1265 c-torture/compile/920625-2.c. */
1266 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1267 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1268 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1269 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1271 pedwarn ("conflicting types for %q+D", newdecl);
1272 /* Make sure we keep void as the return type. */
1273 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1274 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1277 /* Permit void foo (...) to match an earlier call to foo (...) with
1278 no declared type (thus, implicitly int). */
1279 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1280 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1281 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1282 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1284 pedwarn ("conflicting types for %q+D", newdecl);
1285 /* Make sure we keep void as the return type. */
1286 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1291 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1292 error ("conflicting type qualifiers for %q+D", newdecl);
1294 error ("conflicting types for %q+D", newdecl);
1295 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1296 locate_old_decl (olddecl, error);
1301 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1302 but silently ignore the redeclaration if either is in a system
1303 header. (Conflicting redeclarations were handled above.) */
1304 if (TREE_CODE (newdecl) == TYPE_DECL)
1306 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1307 return true; /* Allow OLDDECL to continue in use. */
1309 error ("redefinition of typedef %q+D", newdecl);
1310 locate_old_decl (olddecl, error);
1314 /* Function declarations can either be 'static' or 'extern' (no
1315 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1316 can never conflict with each other on account of linkage
1317 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1318 gnu89 mode permits two definitions if one is 'extern inline' and
1319 one is not. The non- extern-inline definition supersedes the
1320 extern-inline definition. */
1322 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1324 /* If you declare a built-in function name as static, or
1325 define the built-in with an old-style definition (so we
1326 can't validate the argument list) the built-in definition is
1327 overridden, but optionally warn this was a bad choice of name. */
1328 if (DECL_BUILT_IN (olddecl)
1329 && !C_DECL_DECLARED_BUILTIN (olddecl)
1330 && (!TREE_PUBLIC (newdecl)
1331 || (DECL_INITIAL (newdecl)
1332 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1334 warning (OPT_Wshadow, "declaration of %q+D shadows "
1335 "a built-in function", newdecl);
1336 /* Discard the old built-in function. */
1340 if (DECL_INITIAL (newdecl))
1342 if (DECL_INITIAL (olddecl))
1344 /* If both decls are in the same TU and the new declaration
1345 isn't overriding an extern inline reject the new decl.
1346 In c99, no overriding is allowed in the same translation
1348 if ((!DECL_EXTERN_INLINE (olddecl)
1349 || DECL_EXTERN_INLINE (newdecl)
1350 || (!flag_gnu89_inline
1351 && (!DECL_DECLARED_INLINE_P (olddecl)
1352 || !lookup_attribute ("gnu_inline",
1353 DECL_ATTRIBUTES (olddecl)))
1354 && (!DECL_DECLARED_INLINE_P (newdecl)
1355 || !lookup_attribute ("gnu_inline",
1356 DECL_ATTRIBUTES (newdecl))))
1358 && same_translation_unit_p (newdecl, olddecl))
1360 error ("redefinition of %q+D", newdecl);
1361 locate_old_decl (olddecl, error);
1366 /* If we have a prototype after an old-style function definition,
1367 the argument types must be checked specially. */
1368 else if (DECL_INITIAL (olddecl)
1369 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1370 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1371 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1373 locate_old_decl (olddecl, error);
1376 /* A non-static declaration (even an "extern") followed by a
1377 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1378 The same is true for a static forward declaration at block
1379 scope followed by a non-static declaration/definition at file
1380 scope. Static followed by non-static at the same scope is
1381 not undefined behavior, and is the most convenient way to get
1382 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1383 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1384 we do diagnose it if -Wtraditional. */
1385 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1387 /* Two exceptions to the rule. If olddecl is an extern
1388 inline, or a predeclared function that isn't actually
1389 built in, newdecl silently overrides olddecl. The latter
1390 occur only in Objective C; see also above. (FIXME: Make
1391 Objective C use normal builtins.) */
1392 if (!DECL_IS_BUILTIN (olddecl)
1393 && !DECL_EXTERN_INLINE (olddecl))
1395 error ("static declaration of %q+D follows "
1396 "non-static declaration", newdecl);
1397 locate_old_decl (olddecl, error);
1401 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1403 if (DECL_CONTEXT (olddecl))
1405 error ("non-static declaration of %q+D follows "
1406 "static declaration", newdecl);
1407 locate_old_decl (olddecl, error);
1410 else if (warn_traditional)
1412 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1413 "follows static declaration", newdecl);
1418 /* Make sure gnu_inline attribute is either not present, or
1419 present on all inline decls. */
1420 if (DECL_DECLARED_INLINE_P (olddecl)
1421 && DECL_DECLARED_INLINE_P (newdecl))
1423 bool newa = lookup_attribute ("gnu_inline",
1424 DECL_ATTRIBUTES (newdecl)) != NULL;
1425 bool olda = lookup_attribute ("gnu_inline",
1426 DECL_ATTRIBUTES (olddecl)) != NULL;
1429 error ("%<gnu_inline%> attribute present on %q+D",
1430 newa ? newdecl : olddecl);
1431 error ("%Jbut not here", newa ? olddecl : newdecl);
1435 else if (TREE_CODE (newdecl) == VAR_DECL)
1437 /* Only variables can be thread-local, and all declarations must
1438 agree on this property. */
1439 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1441 /* Nothing to check. Since OLDDECL is marked threadprivate
1442 and NEWDECL does not have a thread-local attribute, we
1443 will merge the threadprivate attribute into NEWDECL. */
1446 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1448 if (DECL_THREAD_LOCAL_P (newdecl))
1449 error ("thread-local declaration of %q+D follows "
1450 "non-thread-local declaration", newdecl);
1452 error ("non-thread-local declaration of %q+D follows "
1453 "thread-local declaration", newdecl);
1455 locate_old_decl (olddecl, error);
1459 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1460 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1462 error ("redefinition of %q+D", newdecl);
1463 locate_old_decl (olddecl, error);
1467 /* Objects declared at file scope: if the first declaration had
1468 external linkage (even if it was an external reference) the
1469 second must have external linkage as well, or the behavior is
1470 undefined. If the first declaration had internal linkage, then
1471 the second must too, or else be an external reference (in which
1472 case the composite declaration still has internal linkage).
1473 As for function declarations, we warn about the static-then-
1474 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1475 if (DECL_FILE_SCOPE_P (newdecl)
1476 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1478 if (DECL_EXTERNAL (newdecl))
1480 if (!DECL_FILE_SCOPE_P (olddecl))
1482 error ("extern declaration of %q+D follows "
1483 "declaration with no linkage", newdecl);
1484 locate_old_decl (olddecl, error);
1487 else if (warn_traditional)
1489 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1490 "follows static declaration", newdecl);
1496 if (TREE_PUBLIC (newdecl))
1497 error ("non-static declaration of %q+D follows "
1498 "static declaration", newdecl);
1500 error ("static declaration of %q+D follows "
1501 "non-static declaration", newdecl);
1503 locate_old_decl (olddecl, error);
1507 /* Two objects with the same name declared at the same block
1508 scope must both be external references (6.7p3). */
1509 else if (!DECL_FILE_SCOPE_P (newdecl))
1511 if (DECL_EXTERNAL (newdecl))
1513 /* Extern with initializer at block scope, which will
1514 already have received an error. */
1516 else if (DECL_EXTERNAL (olddecl))
1518 error ("declaration of %q+D with no linkage follows "
1519 "extern declaration", newdecl);
1520 locate_old_decl (olddecl, error);
1524 error ("redeclaration of %q+D with no linkage", newdecl);
1525 locate_old_decl (olddecl, error);
1533 /* All decls must agree on a visibility. */
1534 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1535 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1536 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1538 warning (0, "redeclaration of %q+D with different visibility "
1539 "(old visibility preserved)", newdecl);
1543 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1545 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1546 if (DECL_DECLARED_INLINE_P (newdecl)
1547 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1549 warning (OPT_Wattributes, "inline declaration of %qD follows "
1550 "declaration with attribute noinline", newdecl);
1553 else if (DECL_DECLARED_INLINE_P (olddecl)
1554 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1556 warning (OPT_Wattributes, "declaration of %q+D with attribute "
1557 "noinline follows inline declaration ", newdecl);
1561 /* Inline declaration after use or definition.
1562 ??? Should we still warn about this now we have unit-at-a-time
1563 mode and can get it right?
1564 Definitely don't complain if the decls are in different translation
1566 C99 permits this, so don't warn in that case. (The function
1567 may not be inlined everywhere in function-at-a-time mode, but
1568 we still shouldn't warn.) */
1569 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1570 && same_translation_unit_p (olddecl, newdecl)
1571 && flag_gnu89_inline)
1573 if (TREE_USED (olddecl))
1575 warning (0, "%q+D declared inline after being called", olddecl);
1578 else if (DECL_INITIAL (olddecl))
1580 warning (0, "%q+D declared inline after its definition", olddecl);
1585 else /* PARM_DECL, VAR_DECL */
1587 /* Redeclaration of a parameter is a constraint violation (this is
1588 not explicitly stated, but follows from C99 6.7p3 [no more than
1589 one declaration of the same identifier with no linkage in the
1590 same scope, except type tags] and 6.2.2p6 [parameters have no
1591 linkage]). We must check for a forward parameter declaration,
1592 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1593 an extension, the mandatory diagnostic for which is handled by
1594 mark_forward_parm_decls. */
1596 if (TREE_CODE (newdecl) == PARM_DECL
1597 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1599 error ("redefinition of parameter %q+D", newdecl);
1600 locate_old_decl (olddecl, error);
1605 /* Optional warning for completely redundant decls. */
1606 if (!warned && !pedwarned
1607 && warn_redundant_decls
1608 /* Don't warn about a function declaration followed by a
1610 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1611 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1612 /* Don't warn about redundant redeclarations of builtins. */
1613 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1614 && !DECL_BUILT_IN (newdecl)
1615 && DECL_BUILT_IN (olddecl)
1616 && !C_DECL_DECLARED_BUILTIN (olddecl))
1617 /* Don't warn about an extern followed by a definition. */
1618 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1619 /* Don't warn about forward parameter decls. */
1620 && !(TREE_CODE (newdecl) == PARM_DECL
1621 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1622 /* Don't warn about a variable definition following a declaration. */
1623 && !(TREE_CODE (newdecl) == VAR_DECL
1624 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1626 warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1631 /* Report location of previous decl/defn in a consistent manner. */
1632 if (warned || pedwarned)
1633 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1635 #undef DECL_EXTERN_INLINE
1640 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1641 consistent with OLDDECL, but carries new information. Merge the
1642 new information into OLDDECL. This function issues no
1646 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1648 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1649 && DECL_INITIAL (newdecl) != 0);
1650 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1651 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1652 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1653 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1654 bool extern_changed = false;
1656 /* For real parm decl following a forward decl, rechain the old decl
1657 in its new location and clear TREE_ASM_WRITTEN (it's not a
1658 forward decl anymore). */
1659 if (TREE_CODE (newdecl) == PARM_DECL
1660 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1662 struct c_binding *b, **here;
1664 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
1665 if ((*here)->decl == olddecl)
1672 b->prev = current_scope->bindings;
1673 current_scope->bindings = b;
1675 TREE_ASM_WRITTEN (olddecl) = 0;
1678 DECL_ATTRIBUTES (newdecl)
1679 = targetm.merge_decl_attributes (olddecl, newdecl);
1681 /* Merge the data types specified in the two decls. */
1683 = TREE_TYPE (olddecl)
1684 = composite_type (newtype, oldtype);
1686 /* Lay the type out, unless already done. */
1687 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1689 if (TREE_TYPE (newdecl) != error_mark_node)
1690 layout_type (TREE_TYPE (newdecl));
1691 if (TREE_CODE (newdecl) != FUNCTION_DECL
1692 && TREE_CODE (newdecl) != TYPE_DECL
1693 && TREE_CODE (newdecl) != CONST_DECL)
1694 layout_decl (newdecl, 0);
1698 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1699 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1700 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1701 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1702 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1704 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1705 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1710 /* Merge the type qualifiers. */
1711 if (TREE_READONLY (newdecl))
1712 TREE_READONLY (olddecl) = 1;
1714 if (TREE_THIS_VOLATILE (newdecl))
1715 TREE_THIS_VOLATILE (olddecl) = 1;
1717 /* Merge deprecatedness. */
1718 if (TREE_DEPRECATED (newdecl))
1719 TREE_DEPRECATED (olddecl) = 1;
1721 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
1722 /* Merge unavailableness. */
1723 if (TREE_UNAVAILABLE (newdecl))
1724 TREE_UNAVAILABLE (olddecl) = 1;
1725 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
1727 /* Keep source location of definition rather than declaration and of
1728 prototype rather than non-prototype unless that prototype is
1730 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1731 || (old_is_prototype && !new_is_prototype
1732 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1733 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1735 /* Merge the initialization information. */
1736 if (DECL_INITIAL (newdecl) == 0)
1737 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1739 /* Merge the threadprivate attribute. */
1740 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1742 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1743 C_DECL_THREADPRIVATE_P (newdecl) = 1;
1746 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1748 /* Merge the unused-warning information. */
1749 if (DECL_IN_SYSTEM_HEADER (olddecl))
1750 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1751 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1752 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1754 /* Merge the section attribute.
1755 We want to issue an error if the sections conflict but that
1756 must be done later in decl_attributes since we are called
1757 before attributes are assigned. */
1758 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1759 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1761 /* Copy the assembler name.
1762 Currently, it can only be defined in the prototype. */
1763 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1765 /* Use visibility of whichever declaration had it specified */
1766 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1768 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1769 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1772 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1774 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1775 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1776 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1777 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1778 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1779 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1780 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1781 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1782 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1783 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1786 /* Merge the storage class information. */
1787 merge_weak (newdecl, olddecl);
1789 /* For functions, static overrides non-static. */
1790 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1792 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1793 /* This is since we don't automatically
1794 copy the attributes of NEWDECL into OLDDECL. */
1795 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1796 /* If this clears `static', clear it in the identifier too. */
1797 if (!TREE_PUBLIC (olddecl))
1798 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1802 /* In c99, 'extern' declaration before (or after) 'inline' means this
1803 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1805 if (TREE_CODE (newdecl) == FUNCTION_DECL
1806 && !flag_gnu89_inline
1807 && (DECL_DECLARED_INLINE_P (newdecl)
1808 || DECL_DECLARED_INLINE_P (olddecl))
1809 && (!DECL_DECLARED_INLINE_P (newdecl)
1810 || !DECL_DECLARED_INLINE_P (olddecl)
1811 || !DECL_EXTERNAL (olddecl))
1812 && DECL_EXTERNAL (newdecl)
1813 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl)))
1814 DECL_EXTERNAL (newdecl) = 0;
1816 if (DECL_EXTERNAL (newdecl))
1818 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1819 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1821 /* An extern decl does not override previous storage class. */
1822 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1823 if (!DECL_EXTERNAL (newdecl))
1825 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1826 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1831 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1832 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1835 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1837 /* If we're redefining a function previously defined as extern
1838 inline, make sure we emit debug info for the inline before we
1839 throw it away, in case it was inlined into a function that
1840 hasn't been written out yet. */
1841 if (new_is_definition && DECL_INITIAL (olddecl))
1843 if (TREE_USED (olddecl)
1844 /* In unit-at-a-time mode we never inline re-defined extern
1845 inline functions. */
1846 && !flag_unit_at_a_time
1847 && cgraph_function_possibly_inlined_p (olddecl))
1848 (*debug_hooks->outlining_inline_function) (olddecl);
1850 /* The new defn must not be inline. */
1851 DECL_INLINE (newdecl) = 0;
1852 DECL_UNINLINABLE (newdecl) = 1;
1856 /* If either decl says `inline', this fn is inline, unless
1857 its definition was passed already. */
1858 if (DECL_DECLARED_INLINE_P (newdecl)
1859 || DECL_DECLARED_INLINE_P (olddecl))
1860 DECL_DECLARED_INLINE_P (newdecl) = 1;
1862 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1863 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1866 if (DECL_BUILT_IN (olddecl))
1868 /* If redeclaring a builtin function, it stays built in.
1869 But it gets tagged as having been declared. */
1870 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1871 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1872 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1873 if (new_is_prototype)
1874 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1876 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1877 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1880 /* Also preserve various other info from the definition. */
1881 if (!new_is_definition)
1883 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1884 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1885 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1886 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1887 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1889 /* Set DECL_INLINE on the declaration if we've got a body
1890 from which to instantiate. */
1891 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1893 DECL_INLINE (newdecl) = 1;
1894 DECL_ABSTRACT_ORIGIN (newdecl)
1895 = DECL_ABSTRACT_ORIGIN (olddecl);
1900 /* If a previous declaration said inline, mark the
1901 definition as inlinable. */
1902 if (DECL_DECLARED_INLINE_P (newdecl)
1903 && !DECL_UNINLINABLE (newdecl))
1904 DECL_INLINE (newdecl) = 1;
1908 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
1910 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1911 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1913 unsigned olddecl_uid = DECL_UID (olddecl);
1914 tree olddecl_context = DECL_CONTEXT (olddecl);
1916 memcpy ((char *) olddecl + sizeof (struct tree_common),
1917 (char *) newdecl + sizeof (struct tree_common),
1918 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1919 switch (TREE_CODE (olddecl))
1929 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1930 (char *) newdecl + sizeof (struct tree_decl_common),
1931 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1936 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1937 (char *) newdecl + sizeof (struct tree_decl_common),
1938 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1940 DECL_UID (olddecl) = olddecl_uid;
1941 DECL_CONTEXT (olddecl) = olddecl_context;
1944 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1945 so that encode_section_info has a chance to look at the new decl
1946 flags and attributes. */
1947 if (DECL_RTL_SET_P (olddecl)
1948 && (TREE_CODE (olddecl) == FUNCTION_DECL
1949 || (TREE_CODE (olddecl) == VAR_DECL
1950 && TREE_STATIC (olddecl))))
1951 make_decl_rtl (olddecl);
1953 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
1954 and the definition is coming from the old version, cgraph needs
1955 to be called again. */
1956 if (extern_changed && !new_is_definition
1957 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
1958 cgraph_finalize_function (olddecl, false);
1961 /* Handle when a new declaration NEWDECL has the same name as an old
1962 one OLDDECL in the same binding contour. Prints an error message
1965 If safely possible, alter OLDDECL to look like NEWDECL, and return
1966 true. Otherwise, return false. */
1969 duplicate_decls (tree newdecl, tree olddecl)
1971 tree newtype = NULL, oldtype = NULL;
1973 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1975 /* Avoid `unused variable' and other warnings warnings for OLDDECL. */
1976 TREE_NO_WARNING (olddecl) = 1;
1980 merge_decls (newdecl, olddecl, newtype, oldtype);
1985 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1987 warn_if_shadowing (tree new_decl)
1989 struct c_binding *b;
1991 /* Shadow warnings wanted? */
1993 /* No shadow warnings for internally generated vars. */
1994 || DECL_IS_BUILTIN (new_decl)
1995 /* No shadow warnings for vars made for inlining. */
1996 || DECL_FROM_INLINE (new_decl))
1999 /* Is anything being shadowed? Invisible decls do not count. */
2000 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2001 if (b->decl && b->decl != new_decl && !b->invisible)
2003 tree old_decl = b->decl;
2005 if (old_decl == error_mark_node)
2007 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2008 "non-variable", new_decl);
2011 else if (TREE_CODE (old_decl) == PARM_DECL)
2012 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2014 else if (DECL_FILE_SCOPE_P (old_decl))
2015 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2016 "declaration", new_decl);
2017 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2018 && DECL_BUILT_IN (old_decl))
2020 warning (OPT_Wshadow, "declaration of %q+D shadows "
2021 "a built-in function", new_decl);
2025 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2028 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
2035 /* Subroutine of pushdecl.
2037 X is a TYPE_DECL for a typedef statement. Create a brand new
2038 ..._TYPE node (which will be just a variant of the existing
2039 ..._TYPE node with identical properties) and then install X
2040 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
2042 The whole point here is to end up with a situation where each
2043 and every ..._TYPE node the compiler creates will be uniquely
2044 associated with AT MOST one node representing a typedef name.
2045 This way, even though the compiler substitutes corresponding
2046 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
2047 early on, later parts of the compiler can always do the reverse
2048 translation and get back the corresponding typedef name. For
2051 typedef struct S MY_TYPE;
2054 Later parts of the compiler might only know that `object' was of
2055 type `struct S' if it were not for code just below. With this
2056 code however, later parts of the compiler see something like:
2058 struct S' == struct S
2059 typedef struct S' MY_TYPE;
2062 And they can then deduce (from the node for type struct S') that
2063 the original object declaration was:
2067 Being able to do this is important for proper support of protoize,
2068 and also for generating precise symbolic debugging information
2069 which takes full account of the programmer's (typedef) vocabulary.
2071 Obviously, we don't want to generate a duplicate ..._TYPE node if
2072 the TYPE_DECL node that we are now processing really represents a
2073 standard built-in type.
2075 Since all standard types are effectively declared at line zero
2076 in the source file, we can easily check to see if we are working
2077 on a standard type by checking the current value of lineno. */
2080 clone_underlying_type (tree x)
2082 if (DECL_IS_BUILTIN (x))
2084 if (TYPE_NAME (TREE_TYPE (x)) == 0)
2085 TYPE_NAME (TREE_TYPE (x)) = x;
2087 else if (TREE_TYPE (x) != error_mark_node
2088 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
2090 tree tt = TREE_TYPE (x);
2091 DECL_ORIGINAL_TYPE (x) = tt;
2092 tt = build_variant_type_copy (tt);
2094 TREE_USED (tt) = TREE_USED (x);
2099 /* Record a decl-node X as belonging to the current lexical scope.
2100 Check for errors (such as an incompatible declaration for the same
2101 name already seen in the same scope).
2103 Returns either X or an old decl for the same name.
2104 If an old decl is returned, it may have been smashed
2105 to agree with what X says. */
2110 tree name = DECL_NAME (x);
2111 struct c_scope *scope = current_scope;
2112 struct c_binding *b;
2113 bool nested = false;
2115 /* Functions need the lang_decl data. */
2116 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
2117 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
2119 /* Must set DECL_CONTEXT for everything not at file scope or
2120 DECL_FILE_SCOPE_P won't work. Local externs don't count
2121 unless they have initializers (which generate code). */
2122 if (current_function_decl
2123 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2124 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2125 DECL_CONTEXT (x) = current_function_decl;
2127 /* If this is of variably modified type, prevent jumping into its
2129 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2130 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2131 c_begin_vm_scope (scope->depth);
2133 /* Anonymous decls are just inserted in the scope. */
2136 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2140 /* First, see if there is another declaration with the same name in
2141 the current scope. If there is, duplicate_decls may do all the
2142 work for us. If duplicate_decls returns false, that indicates
2143 two incompatible decls in the same scope; we are to silently
2144 replace the old one (duplicate_decls has issued all appropriate
2145 diagnostics). In particular, we should not consider possible
2146 duplicates in the external scope, or shadowing. */
2147 b = I_SYMBOL_BINDING (name);
2148 if (b && B_IN_SCOPE (b, scope))
2150 struct c_binding *b_ext, *b_use;
2151 tree type = TREE_TYPE (x);
2152 tree visdecl = b->decl;
2153 tree vistype = TREE_TYPE (visdecl);
2154 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2155 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2156 b->inner_comp = false;
2159 /* If this is an external linkage declaration, we should check
2160 for compatibility with the type in the external scope before
2161 setting the type at this scope based on the visible
2162 information only. */
2163 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2165 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2166 b_ext = b_ext->shadowed;
2171 TREE_TYPE (b_use->decl) = b_use->type;
2174 if (duplicate_decls (x, b_use->decl))
2178 /* Save the updated type in the external scope and
2179 restore the proper type for this scope. */
2181 if (comptypes (vistype, type))
2182 thistype = composite_type (vistype, type);
2184 thistype = TREE_TYPE (b_use->decl);
2185 b_use->type = TREE_TYPE (b_use->decl);
2186 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2187 && DECL_BUILT_IN (b_use->decl))
2189 = build_type_attribute_variant (thistype,
2192 TREE_TYPE (b_use->decl) = thistype;
2197 goto skip_external_and_shadow_checks;
2200 /* All declarations with external linkage, and all external
2201 references, go in the external scope, no matter what scope is
2202 current. However, the binding in that scope is ignored for
2203 purposes of normal name lookup. A separate binding structure is
2204 created in the requested scope; this governs the normal
2205 visibility of the symbol.
2207 The binding in the externals scope is used exclusively for
2208 detecting duplicate declarations of the same object, no matter
2209 what scope they are in; this is what we do here. (C99 6.2.7p2:
2210 All declarations that refer to the same object or function shall
2211 have compatible type; otherwise, the behavior is undefined.) */
2212 if (DECL_EXTERNAL (x) || scope == file_scope)
2214 tree type = TREE_TYPE (x);
2217 bool type_saved = false;
2218 if (b && !B_IN_EXTERNAL_SCOPE (b)
2219 && (TREE_CODE (b->decl) == FUNCTION_DECL
2220 || TREE_CODE (b->decl) == VAR_DECL)
2221 && DECL_FILE_SCOPE_P (b->decl))
2224 vistype = TREE_TYPE (visdecl);
2226 if (scope != file_scope
2227 && !DECL_IN_SYSTEM_HEADER (x))
2228 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2230 while (b && !B_IN_EXTERNAL_SCOPE (b))
2232 /* If this decl might be modified, save its type. This is
2233 done here rather than when the decl is first bound
2234 because the type may change after first binding, through
2235 being completed or through attributes being added. If we
2236 encounter multiple such decls, only the first should have
2237 its type saved; the others will already have had their
2238 proper types saved and the types will not have changed as
2239 their scopes will not have been re-entered. */
2240 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2242 b->type = TREE_TYPE (b->decl);
2245 if (B_IN_FILE_SCOPE (b)
2246 && TREE_CODE (b->decl) == VAR_DECL
2247 && TREE_STATIC (b->decl)
2248 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2249 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2250 && TREE_CODE (type) == ARRAY_TYPE
2251 && TYPE_DOMAIN (type)
2252 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2253 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2255 /* Array type completed in inner scope, which should be
2256 diagnosed if the completion does not have size 1 and
2257 it does not get completed in the file scope. */
2258 b->inner_comp = true;
2263 /* If a matching external declaration has been found, set its
2264 type to the composite of all the types of that declaration.
2265 After the consistency checks, it will be reset to the
2266 composite of the visible types only. */
2267 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2269 TREE_TYPE (b->decl) = b->type;
2271 /* The point of the same_translation_unit_p check here is,
2272 we want to detect a duplicate decl for a construct like
2273 foo() { extern bar(); } ... static bar(); but not if
2274 they are in different translation units. In any case,
2275 the static does not go in the externals scope. */
2277 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2278 && duplicate_decls (x, b->decl))
2283 if (comptypes (vistype, type))
2284 thistype = composite_type (vistype, type);
2286 thistype = TREE_TYPE (b->decl);
2290 b->type = TREE_TYPE (b->decl);
2291 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2293 = build_type_attribute_variant (thistype,
2294 TYPE_ATTRIBUTES (b->type));
2295 TREE_TYPE (b->decl) = thistype;
2296 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2299 else if (TREE_PUBLIC (x))
2301 if (visdecl && !b && duplicate_decls (x, visdecl))
2303 /* An external declaration at block scope referring to a
2304 visible entity with internal linkage. The composite
2305 type will already be correct for this scope, so we
2306 just need to fall through to make the declaration in
2313 bind (name, x, external_scope, /*invisible=*/true,
2320 if (TREE_CODE (x) != PARM_DECL)
2321 warn_if_shadowing (x);
2323 skip_external_and_shadow_checks:
2324 if (TREE_CODE (x) == TYPE_DECL)
2325 clone_underlying_type (x);
2327 bind (name, x, scope, /*invisible=*/false, nested);
2329 /* If x's type is incomplete because it's based on a
2330 structure or union which has not yet been fully declared,
2331 attach it to that structure or union type, so we can go
2332 back and complete the variable declaration later, if the
2333 structure or union gets fully declared.
2335 If the input is erroneous, we can have error_mark in the type
2336 slot (e.g. "f(void a, ...)") - that doesn't count as an
2338 if (TREE_TYPE (x) != error_mark_node
2339 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2341 tree element = TREE_TYPE (x);
2343 while (TREE_CODE (element) == ARRAY_TYPE)
2344 element = TREE_TYPE (element);
2345 element = TYPE_MAIN_VARIANT (element);
2347 if ((TREE_CODE (element) == RECORD_TYPE
2348 || TREE_CODE (element) == UNION_TYPE)
2349 && (TREE_CODE (x) != TYPE_DECL
2350 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2351 && !COMPLETE_TYPE_P (element))
2352 C_TYPE_INCOMPLETE_VARS (element)
2353 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2358 /* Record X as belonging to file scope.
2359 This is used only internally by the Objective-C front end,
2360 and is limited to its needs. duplicate_decls is not called;
2361 if there is any preexisting decl for this identifier, it is an ICE. */
2364 pushdecl_top_level (tree x)
2367 bool nested = false;
2368 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2370 name = DECL_NAME (x);
2372 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2374 if (TREE_PUBLIC (x))
2376 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2380 bind (name, x, file_scope, /*invisible=*/false, nested);
2386 implicit_decl_warning (tree id, tree olddecl)
2388 void (*diag) (const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2);
2389 switch (mesg_implicit_function_declaration)
2392 case 1: diag = warning0; break;
2393 case 2: diag = error; break;
2394 default: gcc_unreachable ();
2397 diag (G_("implicit declaration of function %qE"), id);
2399 locate_old_decl (olddecl, diag);
2402 /* Generate an implicit declaration for identifier FUNCTIONID as a
2403 function of type int (). */
2406 implicitly_declare (tree functionid)
2408 struct c_binding *b;
2412 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2414 if (B_IN_SCOPE (b, external_scope))
2423 if (decl == error_mark_node)
2426 /* FIXME: Objective-C has weird not-really-builtin functions
2427 which are supposed to be visible automatically. They wind up
2428 in the external scope because they're pushed before the file
2429 scope gets created. Catch this here and rebind them into the
2431 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2433 bind (functionid, decl, file_scope,
2434 /*invisible=*/false, /*nested=*/true);
2439 tree newtype = default_function_type;
2441 TREE_TYPE (decl) = b->type;
2442 /* Implicit declaration of a function already declared
2443 (somehow) in a different scope, or as a built-in.
2444 If this is the first time this has happened, warn;
2445 then recycle the old declaration but with the new type. */
2446 if (!C_DECL_IMPLICIT (decl))
2448 implicit_decl_warning (functionid, decl);
2449 C_DECL_IMPLICIT (decl) = 1;
2451 if (DECL_BUILT_IN (decl))
2453 newtype = build_type_attribute_variant (newtype,
2455 (TREE_TYPE (decl)));
2456 if (!comptypes (newtype, TREE_TYPE (decl)))
2458 warning (0, "incompatible implicit declaration of built-in"
2459 " function %qD", decl);
2460 newtype = TREE_TYPE (decl);
2465 if (!comptypes (newtype, TREE_TYPE (decl)))
2467 error ("incompatible implicit declaration of function %qD",
2469 locate_old_decl (decl, error);
2472 b->type = TREE_TYPE (decl);
2473 TREE_TYPE (decl) = newtype;
2474 bind (functionid, decl, current_scope,
2475 /*invisible=*/false, /*nested=*/true);
2480 /* Not seen before. */
2481 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2482 DECL_EXTERNAL (decl) = 1;
2483 TREE_PUBLIC (decl) = 1;
2484 C_DECL_IMPLICIT (decl) = 1;
2485 implicit_decl_warning (functionid, 0);
2486 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2488 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2490 /* C89 says implicit declarations are in the innermost block.
2491 So we record the decl in the standard fashion. */
2492 decl = pushdecl (decl);
2494 /* No need to call objc_check_decl here - it's a function type. */
2495 rest_of_decl_compilation (decl, 0, 0);
2497 /* Write a record describing this implicit function declaration
2498 to the prototypes file (if requested). */
2499 gen_aux_info_record (decl, 0, 1, 0);
2501 /* Possibly apply some default attributes to this implicit declaration. */
2502 decl_attributes (&decl, NULL_TREE, 0);
2507 /* Issue an error message for a reference to an undeclared variable
2508 ID, including a reference to a builtin outside of function-call
2509 context. Establish a binding of the identifier to error_mark_node
2510 in an appropriate scope, which will suppress further errors for the
2511 same identifier. The error message should be given location LOC. */
2513 undeclared_variable (tree id, location_t loc)
2515 static bool already = false;
2516 struct c_scope *scope;
2518 if (current_function_decl == 0)
2520 error ("%H%qE undeclared here (not in a function)", &loc, id);
2521 scope = current_scope;
2525 error ("%H%qE undeclared (first use in this function)", &loc, id);
2529 error ("%H(Each undeclared identifier is reported only once", &loc);
2530 error ("%Hfor each function it appears in.)", &loc);
2534 /* If we are parsing old-style parameter decls, current_function_decl
2535 will be nonnull but current_function_scope will be null. */
2536 scope = current_function_scope ? current_function_scope : current_scope;
2538 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2541 /* Subroutine of lookup_label, declare_label, define_label: construct a
2542 LABEL_DECL with all the proper frills. */
2545 make_label (tree name, location_t location)
2547 tree label = build_decl (LABEL_DECL, name, void_type_node);
2549 DECL_CONTEXT (label) = current_function_decl;
2550 DECL_MODE (label) = VOIDmode;
2551 DECL_SOURCE_LOCATION (label) = location;
2556 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2557 Create one if none exists so far for the current function.
2558 This is called when a label is used in a goto expression or
2559 has its address taken. */
2562 lookup_label (tree name)
2566 if (current_function_decl == 0)
2568 error ("label %qE referenced outside of any function", name);
2572 /* Use a label already defined or ref'd with this name, but not if
2573 it is inherited from a containing function and wasn't declared
2575 label = I_LABEL_DECL (name);
2576 if (label && (DECL_CONTEXT (label) == current_function_decl
2577 || C_DECLARED_LABEL_FLAG (label)))
2579 /* If the label has only been declared, update its apparent
2580 location to point here, for better diagnostics if it
2581 turns out not to have been defined. */
2582 if (!TREE_USED (label))
2583 DECL_SOURCE_LOCATION (label) = input_location;
2587 /* No label binding for that identifier; make one. */
2588 label = make_label (name, input_location);
2590 /* Ordinary labels go in the current function scope. */
2591 bind (name, label, current_function_scope,
2592 /*invisible=*/false, /*nested=*/false);
2596 /* Make a label named NAME in the current function, shadowing silently
2597 any that may be inherited from containing functions or containing
2598 scopes. This is called for __label__ declarations. */
2601 declare_label (tree name)
2603 struct c_binding *b = I_LABEL_BINDING (name);
2606 /* Check to make sure that the label hasn't already been declared
2608 if (b && B_IN_CURRENT_SCOPE (b))
2610 error ("duplicate label declaration %qE", name);
2611 locate_old_decl (b->decl, error);
2613 /* Just use the previous declaration. */
2617 label = make_label (name, input_location);
2618 C_DECLARED_LABEL_FLAG (label) = 1;
2620 /* Declared labels go in the current scope. */
2621 bind (name, label, current_scope,
2622 /*invisible=*/false, /*nested=*/false);
2626 /* Define a label, specifying the location in the source file.
2627 Return the LABEL_DECL node for the label, if the definition is valid.
2628 Otherwise return 0. */
2631 define_label (location_t location, tree name)
2633 /* Find any preexisting label with this name. It is an error
2634 if that label has already been defined in this function, or
2635 if there is a containing function with a declared label with
2637 tree label = I_LABEL_DECL (name);
2638 struct c_label_list *nlist_se, *nlist_vm;
2641 && ((DECL_CONTEXT (label) == current_function_decl
2642 && DECL_INITIAL (label) != 0)
2643 || (DECL_CONTEXT (label) != current_function_decl
2644 && C_DECLARED_LABEL_FLAG (label))))
2646 error ("%Hduplicate label %qD", &location, label);
2647 locate_old_decl (label, error);
2650 else if (label && DECL_CONTEXT (label) == current_function_decl)
2652 /* The label has been used or declared already in this function,
2653 but not defined. Update its location to point to this
2655 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2656 error ("%Jjump into statement expression", label);
2657 if (C_DECL_UNDEFINABLE_VM (label))
2658 error ("%Jjump into scope of identifier with variably modified type",
2660 DECL_SOURCE_LOCATION (label) = location;
2664 /* No label binding for that identifier; make one. */
2665 label = make_label (name, location);
2667 /* Ordinary labels go in the current function scope. */
2668 bind (name, label, current_function_scope,
2669 /*invisible=*/false, /*nested=*/false);
2672 if (!in_system_header && lookup_name (name))
2673 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2674 "for labels, identifier %qE conflicts", &location, name);
2676 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2677 nlist_se->next = label_context_stack_se->labels_def;
2678 nlist_se->label = label;
2679 label_context_stack_se->labels_def = nlist_se;
2681 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2682 nlist_vm->next = label_context_stack_vm->labels_def;
2683 nlist_vm->label = label;
2684 label_context_stack_vm->labels_def = nlist_vm;
2686 /* Mark label as having been defined. */
2687 DECL_INITIAL (label) = error_mark_node;
2691 /* Given NAME, an IDENTIFIER_NODE,
2692 return the structure (or union or enum) definition for that name.
2693 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2694 CODE says which kind of type the caller wants;
2695 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2696 If the wrong kind of type is found, an error is reported. */
2699 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2701 struct c_binding *b = I_TAG_BINDING (name);
2707 /* We only care about whether it's in this level if
2708 thislevel_only was set or it might be a type clash. */
2709 if (thislevel_only || TREE_CODE (b->decl) != code)
2711 /* For our purposes, a tag in the external scope is the same as
2712 a tag in the file scope. (Primarily relevant to Objective-C
2713 and its builtin structure tags, which get pushed before the
2714 file scope is created.) */
2715 if (B_IN_CURRENT_SCOPE (b)
2716 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2720 if (thislevel_only && !thislevel)
2723 if (TREE_CODE (b->decl) != code)
2725 /* Definition isn't the kind we were looking for. */
2726 pending_invalid_xref = name;
2727 pending_invalid_xref_location = input_location;
2729 /* If in the same binding level as a declaration as a tag
2730 of a different type, this must not be allowed to
2731 shadow that tag, so give the error immediately.
2732 (For example, "struct foo; union foo;" is invalid.) */
2734 pending_xref_error ();
2739 /* Print an error message now
2740 for a recent invalid struct, union or enum cross reference.
2741 We don't print them immediately because they are not invalid
2742 when used in the `struct foo;' construct for shadowing. */
2745 pending_xref_error (void)
2747 if (pending_invalid_xref != 0)
2748 error ("%H%qE defined as wrong kind of tag",
2749 &pending_invalid_xref_location, pending_invalid_xref);
2750 pending_invalid_xref = 0;
2754 /* Look up NAME in the current scope and its superiors
2755 in the namespace of variables, functions and typedefs.
2756 Return a ..._DECL node of some kind representing its definition,
2757 or return 0 if it is undefined. */
2760 lookup_name (tree name)
2762 struct c_binding *b = I_SYMBOL_BINDING (name);
2763 if (b && !b->invisible)
2768 /* Similar to `lookup_name' but look only at the indicated scope. */
2771 lookup_name_in_scope (tree name, struct c_scope *scope)
2773 struct c_binding *b;
2775 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2776 if (B_IN_SCOPE (b, scope))
2781 /* Create the predefined scalar types of C,
2782 and some nodes representing standard constants (0, 1, (void *) 0).
2783 Initialize the global scope.
2784 Make definitions for built-in primitive functions. */
2787 c_init_decl_processing (void)
2789 location_t save_loc = input_location;
2791 /* Initialize reserved words for parser. */
2794 current_function_decl = 0;
2796 gcc_obstack_init (&parser_obstack);
2798 /* Make the externals scope. */
2800 external_scope = current_scope;
2802 /* Declarations from c_common_nodes_and_builtins must not be associated
2803 with this input file, lest we get differences between using and not
2804 using preprocessed headers. */
2805 #ifdef USE_MAPPED_LOCATION
2806 input_location = BUILTINS_LOCATION;
2808 input_location.file = "<built-in>";
2809 input_location.line = 0;
2812 build_common_tree_nodes (flag_signed_char, false);
2814 c_common_nodes_and_builtins ();
2816 /* In C, comparisons and TRUTH_* expressions have type int. */
2817 truthvalue_type_node = integer_type_node;
2818 truthvalue_true_node = integer_one_node;
2819 truthvalue_false_node = integer_zero_node;
2821 /* Even in C99, which has a real boolean type. */
2822 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2823 boolean_type_node));
2825 input_location = save_loc;
2827 pedantic_lvalues = true;
2829 make_fname_decl = c_make_fname_decl;
2830 start_fname_decls ();
2833 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2834 decl, NAME is the initialization string and TYPE_DEP indicates whether
2835 NAME depended on the type of the function. As we don't yet implement
2836 delayed emission of static data, we mark the decl as emitted
2837 so it is not placed in the output. Anything using it must therefore pull
2838 out the STRING_CST initializer directly. FIXME. */
2841 c_make_fname_decl (tree id, int type_dep)
2843 const char *name = fname_as_string (type_dep);
2844 tree decl, type, init;
2845 size_t length = strlen (name);
2847 type = build_array_type (char_type_node,
2848 build_index_type (size_int (length)));
2849 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2851 decl = build_decl (VAR_DECL, id, type);
2853 TREE_STATIC (decl) = 1;
2854 TREE_READONLY (decl) = 1;
2855 DECL_ARTIFICIAL (decl) = 1;
2857 init = build_string (length + 1, name);
2858 free ((char *) name);
2859 TREE_TYPE (init) = type;
2860 DECL_INITIAL (decl) = init;
2862 TREE_USED (decl) = 1;
2864 if (current_function_decl
2865 /* For invalid programs like this:
2868 const char* p = __FUNCTION__;
2870 the __FUNCTION__ is believed to appear in K&R style function
2871 parameter declarator. In that case we still don't have
2873 && (!errorcount || current_function_scope))
2875 DECL_CONTEXT (decl) = current_function_decl;
2876 bind (id, decl, current_function_scope,
2877 /*invisible=*/false, /*nested=*/false);
2880 finish_decl (decl, init, NULL_TREE);
2885 /* Return a definition for a builtin function named NAME and whose data type
2886 is TYPE. TYPE should be a function type with argument types.
2887 FUNCTION_CODE tells later passes how to compile calls to this function.
2888 See tree.h for its possible values.
2890 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2891 the name to be called if we can't opencode the function. If
2892 ATTRS is nonzero, use that for the function's attribute list. */
2895 builtin_function (const char *name, tree type, int function_code,
2896 enum built_in_class cl, const char *library_name,
2899 tree id = get_identifier (name);
2900 tree decl = build_decl (FUNCTION_DECL, id, type);
2901 TREE_PUBLIC (decl) = 1;
2902 DECL_EXTERNAL (decl) = 1;
2903 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2904 DECL_BUILT_IN_CLASS (decl) = cl;
2905 DECL_FUNCTION_CODE (decl) = function_code;
2906 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2908 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2910 /* Should never be called on a symbol with a preexisting meaning. */
2911 gcc_assert (!I_SYMBOL_BINDING (id));
2913 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2915 /* Builtins in the implementation namespace are made visible without
2916 needing to be explicitly declared. See push_file_scope. */
2917 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2919 TREE_CHAIN (decl) = visible_builtins;
2920 visible_builtins = decl;
2923 /* Possibly apply some default attributes to this built-in function. */
2925 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2927 decl_attributes (&decl, NULL_TREE, 0);
2932 /* Called when a declaration is seen that contains no names to declare.
2933 If its type is a reference to a structure, union or enum inherited
2934 from a containing scope, shadow that tag name for the current scope
2935 with a forward reference.
2936 If its type defines a new named structure or union
2937 or defines an enum, it is valid but we need not do anything here.
2938 Otherwise, it is an error. */
2941 shadow_tag (const struct c_declspecs *declspecs)
2943 shadow_tag_warned (declspecs, 0);
2946 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2949 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2951 bool found_tag = false;
2953 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2955 tree value = declspecs->type;
2956 enum tree_code code = TREE_CODE (value);
2958 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2959 /* Used to test also that TYPE_SIZE (value) != 0.
2960 That caused warning for `struct foo;' at top level in the file. */
2962 tree name = TYPE_NAME (value);
2969 if (warned != 1 && code != ENUMERAL_TYPE)
2970 /* Empty unnamed enum OK */
2972 pedwarn ("unnamed struct/union that defines no instances");
2976 else if (!declspecs->tag_defined_p
2977 && declspecs->storage_class != csc_none)
2980 pedwarn ("empty declaration with storage class specifier "
2981 "does not redeclare tag");
2983 pending_xref_error ();
2985 else if (!declspecs->tag_defined_p
2986 && (declspecs->const_p
2987 || declspecs->volatile_p
2988 || declspecs->restrict_p))
2991 pedwarn ("empty declaration with type qualifier "
2992 "does not redeclare tag");
2994 pending_xref_error ();
2998 pending_invalid_xref = 0;
2999 t = lookup_tag (code, name, 1);
3003 t = make_node (code);
3010 if (warned != 1 && !in_system_header)
3012 pedwarn ("useless type name in empty declaration");
3017 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
3019 pedwarn ("useless type name in empty declaration");
3023 pending_invalid_xref = 0;
3025 if (declspecs->inline_p)
3027 error ("%<inline%> in empty declaration");
3031 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3033 error ("%<auto%> in file-scope empty declaration");
3037 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3039 error ("%<register%> in file-scope empty declaration");
3043 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3045 warning (0, "useless storage class specifier in empty declaration");
3049 if (!warned && !in_system_header && declspecs->thread_p)
3051 warning (0, "useless %<__thread%> in empty declaration");
3055 if (!warned && !in_system_header && (declspecs->const_p
3056 || declspecs->volatile_p
3057 || declspecs->restrict_p))
3059 warning (0, "useless type qualifier in empty declaration");
3066 pedwarn ("empty declaration");
3071 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3072 bits. SPECS represents declaration specifiers that the grammar
3073 only permits to contain type qualifiers and attributes. */
3076 quals_from_declspecs (const struct c_declspecs *specs)
3078 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3079 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3080 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3081 gcc_assert (!specs->type
3082 && !specs->decl_attr
3083 && specs->typespec_word == cts_none
3084 && specs->storage_class == csc_none
3085 && !specs->typedef_p
3086 && !specs->explicit_signed_p
3087 && !specs->deprecated_p
3089 && !specs->long_long_p
3092 && !specs->unsigned_p
3093 && !specs->complex_p
3095 && !specs->thread_p);
3099 /* Construct an array declarator. EXPR is the expression inside [],
3100 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3101 applied to the pointer to which a parameter array is converted).
3102 STATIC_P is true if "static" is inside the [], false otherwise.
3103 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3104 length which is nevertheless a complete type, false otherwise. The
3105 field for the contained declarator is left to be filled in by
3106 set_array_declarator_inner. */
3108 struct c_declarator *
3109 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3112 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3113 struct c_declarator);
3114 declarator->kind = cdk_array;
3115 declarator->declarator = 0;
3116 declarator->u.array.dimen = expr;
3119 declarator->u.array.attrs = quals->attrs;
3120 declarator->u.array.quals = quals_from_declspecs (quals);
3124 declarator->u.array.attrs = NULL_TREE;
3125 declarator->u.array.quals = 0;
3127 declarator->u.array.static_p = static_p;
3128 declarator->u.array.vla_unspec_p = vla_unspec_p;
3129 if (pedantic && !flag_isoc99)
3131 if (static_p || quals != NULL)
3132 pedwarn ("ISO C90 does not support %<static%> or type "
3133 "qualifiers in parameter array declarators");
3135 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3139 if (!current_scope->parm_flag)
3142 error ("%<[*]%> not allowed in other than function prototype scope");
3143 declarator->u.array.vla_unspec_p = false;
3146 current_scope->had_vla_unspec = true;
3151 /* Set the contained declarator of an array declarator. DECL is the
3152 declarator, as constructed by build_array_declarator; INNER is what
3153 appears on the left of the []. ABSTRACT_P is true if it is an
3154 abstract declarator, false otherwise; this is used to reject static
3155 and type qualifiers in abstract declarators, where they are not in
3156 the C99 grammar (subject to possible change in DR#289). */
3158 struct c_declarator *
3159 set_array_declarator_inner (struct c_declarator *decl,
3160 struct c_declarator *inner, bool abstract_p)
3162 decl->declarator = inner;
3163 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3164 || decl->u.array.attrs != NULL_TREE
3165 || decl->u.array.static_p))
3166 error ("static or type qualifiers in abstract declarator");
3170 /* INIT is a constructor that forms DECL's initializer. If the final
3171 element initializes a flexible array field, add the size of that
3172 initializer to DECL's size. */
3175 add_flexible_array_elts_to_size (tree decl, tree init)
3179 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3182 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3183 type = TREE_TYPE (elt);
3184 if (TREE_CODE (type) == ARRAY_TYPE
3185 && TYPE_SIZE (type) == NULL_TREE
3186 && TYPE_DOMAIN (type) != NULL_TREE
3187 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3189 complete_array_type (&type, elt, false);
3191 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3192 DECL_SIZE_UNIT (decl)
3193 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3197 /* APPLE LOCAL begin blocks 6339747 */
3198 /* Decode a block literal type, such as "int **", returning a ...FUNCTION_DECL node. */
3201 grokblockdecl (struct c_declspecs *specs, struct c_declarator *declarator)
3204 tree attrs = specs->attrs;
3206 specs->attrs = NULL_TREE;
3208 decl = grokdeclarator (declarator, specs, BLOCKDEF,
3211 /* Apply attributes. */
3212 decl_attributes (&decl, attrs, 0);
3216 /* APPLE LOCAL end blocks 6339747 */
3218 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3221 groktypename (struct c_type_name *type_name)
3224 tree attrs = type_name->specs->attrs;
3226 type_name->specs->attrs = NULL_TREE;
3228 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3231 /* Apply attributes. */
3232 decl_attributes (&type, attrs, 0);
3237 /* Decode a declarator in an ordinary declaration or data definition.
3238 This is called as soon as the type information and variable name
3239 have been parsed, before parsing the initializer if any.
3240 Here we create the ..._DECL node, fill in its type,
3241 and put it on the list of decls for the current context.
3242 The ..._DECL node is returned as the value.
3244 Exception: for arrays where the length is not specified,
3245 the type is left null, to be filled in by `finish_decl'.
3247 Function definitions do not come here; they go to start_function
3248 instead. However, external and forward declarations of functions
3249 do go through here. Structure field declarations are done by
3250 grokfield and not through here. */
3253 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3254 bool initialized, tree attributes)
3259 /* An object declared as __attribute__((deprecated)) suppresses
3260 warnings of uses of other deprecated items. */
3261 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
3262 /* An object declared as __attribute__((unavailable)) suppresses
3263 any reports of being declared with unavailable or deprecated
3264 items. An object declared as __attribute__((deprecated))
3265 suppresses warnings of uses of other deprecated items. */
3266 #ifdef A_LESS_INEFFICENT_WAY /* which I really don't want to do! */
3267 if (lookup_attribute ("deprecated", attributes))
3268 deprecated_state = DEPRECATED_SUPPRESS;
3269 else if (lookup_attribute ("unavailable", attributes))
3270 deprecated_state = DEPRECATED_UNAVAILABLE_SUPPRESS;
3271 #else /* a more efficient way doing what lookup_attribute would do */
3274 for (a = attributes; a; a = TREE_CHAIN (a))
3276 tree name = TREE_PURPOSE (a);
3277 if (TREE_CODE (name) == IDENTIFIER_NODE)
3278 if (is_attribute_p ("deprecated", name))
3280 deprecated_state = DEPRECATED_SUPPRESS;
3283 if (is_attribute_p ("unavailable", name))
3285 deprecated_state = DEPRECATED_UNAVAILABLE_SUPPRESS;
3290 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
3292 decl = grokdeclarator (declarator, declspecs,
3293 NORMAL, initialized, NULL);
3297 deprecated_state = DEPRECATED_NORMAL;
3299 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3300 && MAIN_NAME_P (DECL_NAME (decl)))
3301 warning (OPT_Wmain, "%q+D is usually a function", decl);
3304 /* Is it valid for this decl to have an initializer at all?
3305 If not, set INITIALIZED to zero, which will indirectly
3306 tell 'finish_decl' to ignore the initializer once it is parsed. */
3307 switch (TREE_CODE (decl))
3310 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3315 error ("function %qD is initialized like a variable", decl);
3320 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3321 error ("parameter %qD is initialized", decl);
3326 /* Don't allow initializations for incomplete types except for
3327 arrays which might be completed by the initialization. */
3329 /* This can happen if the array size is an undefined macro.
3330 We already gave a warning, so we don't need another one. */
3331 if (TREE_TYPE (decl) == error_mark_node)
3333 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3335 /* A complete type is ok if size is fixed. */
3337 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3338 || C_DECL_VARIABLE_SIZE (decl))
3340 error ("variable-sized object may not be initialized");
3344 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3346 error ("variable %qD has initializer but incomplete type", decl);
3349 else if (C_DECL_VARIABLE_SIZE (decl))
3351 /* Although C99 is unclear about whether incomplete arrays
3352 of VLAs themselves count as VLAs, it does not make
3353 sense to permit them to be initialized given that
3354 ordinary VLAs may not be initialized. */
3355 error ("variable-sized object may not be initialized");
3362 if (current_scope == file_scope)
3363 TREE_STATIC (decl) = 1;
3365 /* Tell 'pushdecl' this is an initialized decl
3366 even though we don't yet have the initializer expression.
3367 Also tell 'finish_decl' it may store the real initializer. */
3368 DECL_INITIAL (decl) = error_mark_node;
3371 /* If this is a function declaration, write a record describing it to the
3372 prototypes file (if requested). */
3374 if (TREE_CODE (decl) == FUNCTION_DECL)
3375 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3377 /* ANSI specifies that a tentative definition which is not merged with
3378 a non-tentative definition behaves exactly like a definition with an
3379 initializer equal to zero. (Section 3.7.2)
3381 -fno-common gives strict ANSI behavior, though this tends to break
3382 a large body of code that grew up without this rule.
3384 Thread-local variables are never common, since there's no entrenched
3385 body of code to break, and it allows more efficient variable references
3386 in the presence of dynamic linking. */
3388 if (TREE_CODE (decl) == VAR_DECL
3390 && TREE_PUBLIC (decl)
3391 && !DECL_THREAD_LOCAL_P (decl)
3393 DECL_COMMON (decl) = 1;
3395 /* Set attributes here so if duplicate decl, will have proper attributes. */
3396 decl_attributes (&decl, attributes, 0);
3398 /* Handle gnu_inline attribute. */
3399 if (declspecs->inline_p
3400 && !flag_gnu89_inline
3401 && TREE_CODE (decl) == FUNCTION_DECL
3402 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
3404 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3406 else if (declspecs->storage_class != csc_static)
3407 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3410 if (TREE_CODE (decl) == FUNCTION_DECL
3411 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3413 struct c_declarator *ce = declarator;
3415 if (ce->kind == cdk_pointer)
3416 ce = declarator->declarator;
3417 if (ce->kind == cdk_function)
3419 tree args = ce->u.arg_info->parms;
3420 for (; args; args = TREE_CHAIN (args))
3422 tree type = TREE_TYPE (args);
3423 if (type && INTEGRAL_TYPE_P (type)
3424 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3425 DECL_ARG_TYPE (args) = integer_type_node;
3430 if (TREE_CODE (decl) == FUNCTION_DECL
3431 && DECL_DECLARED_INLINE_P (decl)
3432 && DECL_UNINLINABLE (decl)
3433 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3434 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3437 /* C99 6.7.4p3: An inline definition of a function with external
3438 linkage shall not contain a definition of a modifiable object
3439 with static storage duration... */
3440 if (TREE_CODE (decl) == VAR_DECL
3441 && current_scope != file_scope
3442 && TREE_STATIC (decl)
3443 && !TREE_READONLY (decl)
3444 && DECL_DECLARED_INLINE_P (current_function_decl)
3445 && DECL_EXTERNAL (current_function_decl))
3446 pedwarn ("%q+D is static but declared in inline function %qD "
3447 "which is not static", decl, current_function_decl);
3449 /* Add this decl to the current scope.
3450 TEM may equal DECL or it may be a previous decl of the same name. */
3451 tem = pushdecl (decl);
3453 if (initialized && DECL_EXTERNAL (tem))
3455 DECL_EXTERNAL (tem) = 0;
3456 TREE_STATIC (tem) = 1;
3462 /* Initialize EH if not initialized yet and exceptions are enabled. */
3465 c_maybe_initialize_eh (void)
3467 if (!flag_exceptions || c_eh_initialized_p)
3470 c_eh_initialized_p = true;
3471 eh_personality_libfunc
3472 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3473 ? "__gcc_personality_sj0"
3474 : "__gcc_personality_v0");
3475 default_init_unwind_resume_libfunc ();
3476 using_eh_for_cleanups ();
3479 /* APPLE LOCAL begin radar 5932809 - copyable byref blocks (C++ cr) */
3480 #define BLOCK_ALIGN_MAX 18
3481 static tree block_byref_id_object_copy[BLOCK_BYREF_CURRENT_MAX*(BLOCK_ALIGN_MAX+1)];
3482 static tree block_byref_id_object_dispose[BLOCK_BYREF_CURRENT_MAX*(BLOCK_ALIGN_MAX+1)];
3485 This routine builds:
3487 void __Block_byref_id_object_copy(struct Block_byref_id_object *dst,
3488 struct Block_byref_id_object *src) {
3489 _Block_object_assign (&_dest->object, _src->object, BLOCK_FIELD_IS_OBJECT[|BLOCK_FIELD_IS_WEAK]) // objects
3490 _Block_object_assign(&_dest->object, _src->object, BLOCK_FIELD_IS_BLOCK[|BLOCK_FIELD_IS_WEAK]) // blocks
3493 synth_block_byref_id_object_copy_func (int flag, int kind)
3496 tree dst_arg, src_arg;
3497 tree dst_obj, src_obj;
3498 struct c_arg_info * arg_info;
3500 gcc_assert (block_byref_id_object_copy[kind]);
3501 /* Set up: (void* _dest, void*_src) parameters. */
3502 dst_arg = build_decl (PARM_DECL, get_identifier ("_dst"),
3504 TREE_USED (dst_arg) = 1;
3505 DECL_ARG_TYPE (dst_arg) = ptr_type_node;
3506 src_arg = build_decl (PARM_DECL, get_identifier ("_src"),
3508 TREE_USED (src_arg) = 1;
3509 DECL_ARG_TYPE (src_arg) = ptr_type_node;
3510 arg_info = xcalloc (1, sizeof (struct c_arg_info));
3511 TREE_CHAIN (dst_arg) = src_arg;
3512 arg_info->parms = dst_arg;
3513 arg_info->types = tree_cons (NULL_TREE, ptr_type_node,
3514 tree_cons (NULL_TREE,
3517 /* function header synthesis. */
3518 push_function_context ();
3519 start_block_helper_function (block_byref_id_object_copy[kind]);
3520 store_parm_decls_from (arg_info);
3522 /* Body of the function. */
3523 stmt = c_begin_compound_stmt (true);
3524 /* Build dst->object */
3525 dst_obj = build_indirect_object_id_exp (dst_arg);
3528 /* src_obj is: _src->object. */
3529 src_obj = build_indirect_object_id_exp (src_arg);
3531 /* APPLE LOCAL begin radar 6180456 */
3532 /* _Block_object_assign (&_dest->object, _src->object, BLOCK_FIELD_IS_OBJECT) or :
3533 _Block_object_assign (&_dest->object, _src->object, BLOCK_FIELD_IS_BLOCK) */
3534 /* APPLE LOCAL begin radar 6573923 */
3535 /* Also add the new flag when calling _Block_object_dispose
3536 from byref dispose helper. */
3537 flag |= BLOCK_BYREF_CALLER;
3538 /* APPLE LOCAL end radar 6573923 */
3539 call_exp = build_block_object_assign_call_exp (build_fold_addr_expr (dst_obj), src_obj, flag);
3540 add_stmt (call_exp);
3541 /* APPLE LOCAL end radar 6180456 */
3543 fnbody = c_end_compound_stmt (stmt, true);
3546 pop_function_context ();
3551 This routine builds:
3553 void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) {
3554 _Block_object_dispose(_src->object, BLOCK_FIELD_IS_OBJECT[|BLOCK_FIELD_IS_WEAK]) // object
3555 _Block_object_dispose(_src->object, BLOCK_FIELD_IS_BLOCK[|BLOCK_FIELD_IS_WEAK]) // block
3558 synth_block_byref_id_object_dispose_func (int flag, int kind)
3561 tree src_arg, src_obj, rel_exp;
3562 struct c_arg_info * arg_info;
3563 gcc_assert (block_byref_id_object_dispose[kind]);
3564 /* Set up: (void *_src) parameter. */
3565 src_arg = build_decl (PARM_DECL, get_identifier ("_src"),
3567 TREE_USED (src_arg) = 1;
3568 DECL_ARG_TYPE (src_arg) = ptr_type_node;
3569 arg_info = xcalloc (1, sizeof (struct c_arg_info));
3570 arg_info->parms = src_arg;
3571 arg_info->types = tree_cons (NULL_TREE, ptr_type_node,
3573 /* function header synthesis. */
3574 push_function_context ();
3575 start_block_helper_function (block_byref_id_object_dispose[kind]);
3576 store_parm_decls_from (arg_info);
3578 /* Body of the function. */
3579 stmt = c_begin_compound_stmt (true);
3580 src_obj = build_indirect_object_id_exp (src_arg);
3582 /* APPLE LOCAL begin radar 6180456 */
3583 /* _Block_object_dispose(_src->object, BLOCK_FIELD_IS_OBJECT) : or
3584 _Block_object_dispose(_src->object, BLOCK_FIELD_IS_BLOCK) */
3585 /* APPLE LOCAL begin radar 6573923 */
3586 /* Also add the new flag when calling _Block_object_dispose
3587 from byref dispose helper. */
3588 flag |= BLOCK_BYREF_CALLER;
3589 /* APPLE LOCAL end radar 6573923 */
3590 rel_exp = build_block_object_dispose_call_exp (src_obj, flag);
3591 /* APPLE LOCAL end radar 6180456 */
3594 fnbody = c_end_compound_stmt (stmt, true);
3597 pop_function_context ();
3601 /* new_block_byref_decl - This routine changes a 'typex x' declared variable into:
3603 struct __Block_byref_x {
3604 // APPLE LOCAL radar 6244520
3605 void *__isa; // NULL for everything except __weak pointers
3606 struct Block_byref_x *__forwarding;
3609 void *__ByrefKeepFuncPtr; // Only if variable is __block ObjC object
3610 void *__ByrefDestroyFuncPtr; // Only if variable is __block ObjC object
3616 new_block_byref_decl (tree decl)
3618 static int unique_count;
3619 /* APPLE LOCAL radar 5847976 */
3620 int save_flag_objc_gc;
3621 tree Block_byref_type;
3622 tree field_decl_chain, field_decl;
3623 const char *prefix = "__Block_byref_";
3624 char *string = alloca (strlen (IDENTIFIER_POINTER (DECL_NAME (decl))) +
3625 strlen (prefix) + 8 /* to hold the count */);
3627 sprintf (string, "%s%d_%s", prefix, ++unique_count,
3628 IDENTIFIER_POINTER (DECL_NAME (decl)));
3630 push_to_top_level ();
3631 Block_byref_type = start_struct (RECORD_TYPE, get_identifier (string));
3633 /* APPLE LOCAL begin radar 6244520 */
3635 field_decl = build_decl (FIELD_DECL, get_identifier ("__isa"), ptr_type_node);
3636 field_decl_chain = field_decl;
3637 /* APPLE LOCAL end radar 6244520 */
3639 /* struct Block_byref_x *__forwarding; */
3640 field_decl = build_decl (FIELD_DECL, get_identifier ("__forwarding"),
3641 build_pointer_type (Block_byref_type));
3642 /* APPLE LOCAL radar 6244520 */
3643 chainon (field_decl_chain, field_decl);
3645 /* int32_t __flags; */
3646 field_decl = build_decl (FIELD_DECL, get_identifier ("__flags"),
3647 unsigned_type_node);
3648 chainon (field_decl_chain, field_decl);
3650 /* int32_t __size; */
3651 field_decl = build_decl (FIELD_DECL, get_identifier ("__size"),
3652 unsigned_type_node);
3653 chainon (field_decl_chain, field_decl);
3655 if (COPYABLE_BYREF_LOCAL_NONPOD (decl))
3657 /* void *__ByrefKeepFuncPtr; */
3658 field_decl = build_decl (FIELD_DECL, get_identifier ("__ByrefKeepFuncPtr"),
3660 chainon (field_decl_chain, field_decl);
3662 /* void *__ByrefDestroyFuncPtr; */
3663 field_decl = build_decl (FIELD_DECL, get_identifier ("__ByrefDestroyFuncPtr"),
3665 chainon (field_decl_chain, field_decl);
3669 field_decl = build_decl (FIELD_DECL, DECL_NAME (decl), TREE_TYPE (decl));
3670 chainon (field_decl_chain, field_decl);
3672 pop_from_top_level ();
3673 /* APPLE LOCAL begin radar 5847976 */
3674 /* Hack so we don't issue warning on a field_decl having __weak attribute */
3675 save_flag_objc_gc = flag_objc_gc;
3677 finish_struct (Block_byref_type, field_decl_chain, NULL_TREE);
3678 flag_objc_gc = save_flag_objc_gc;
3679 /* APPLE LOCAL end radar 5847976 */
3681 TREE_TYPE (decl) = Block_byref_type;
3682 /* Force layout_decl to recompute these fields. */
3683 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
3684 layout_decl (decl, 0);
3688 /* init_byref_decl - This routine builds the initializer for the __Block_byref_x
3689 type in the form of:
3690 { NULL, &x, 0, sizeof(struct __Block_byref_x), initializer-expr};
3693 { NULL, &x, 0, sizeof(struct __Block_byref_x)};
3694 when INIT is NULL_TREE
3696 For __block ObjC objects, it also adds "byref_keep" and "byref_destroy"
3697 Funtion pointers. So the most general initializers would be:
3699 { NULL, &x, 0, sizeof(struct __Block_byref_x), &byref_keep, &byref_destroy,
3703 /* APPLE LOCAL radar 5847976 */
3704 init_byref_decl (tree decl, tree init, int flag)
3707 tree block_byref_type = TREE_TYPE (decl);
3708 int size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (block_byref_type));
3712 if (COPYABLE_BYREF_LOCAL_NONPOD (decl))
3713 flags = BLOCK_HAS_COPY_DISPOSE;
3715 fields = TYPE_FIELDS (block_byref_type);
3716 /* APPLE LOCAL begin radar 6244520 */
3717 /* APPLE LOCAL begin radar 5847976 */
3718 initlist = tree_cons (fields, fold_convert (ptr_type_node, ((flag & BLOCK_FIELD_IS_WEAK) != 0) ? integer_one_node
3719 : integer_zero_node), 0);
3720 /* APPLE LOCAL end radar 5847976 */
3721 fields = TREE_CHAIN (fields);
3723 initlist = tree_cons (fields,
3724 build_unary_op (ADDR_EXPR, decl, 0), initlist);
3725 /* APPLE LOCAL end radar 6244520 */
3726 fields = TREE_CHAIN (fields);
3728 initlist = tree_cons (fields, build_int_cst (TREE_TYPE (fields), flags),
3730 fields = TREE_CHAIN (fields);
3731 initlist = tree_cons (fields, build_int_cst (TREE_TYPE (fields), size),
3733 fields = TREE_CHAIN (fields);
3735 if (COPYABLE_BYREF_LOCAL_NONPOD (decl))
3738 int align = exact_log2 ((DECL_ALIGN (decl)+TYPE_ALIGN (ptr_type_node)-1) / TYPE_ALIGN (ptr_type_node));
3740 if (align == -1 || align > BLOCK_ALIGN_MAX) {
3741 error ("invalid alignment for __block variable");
3744 kind = align*BLOCK_BYREF_CURRENT_MAX + flag;
3745 /* Add &__Block_byref_id_object_copy, &__Block_byref_id_object_dispose
3747 if (!block_byref_id_object_copy[kind])
3749 /* Build a void __Block_byref_id_object_copy(void*, void*) type. */
3751 build_function_type (void_type_node,
3752 tree_cons (NULL_TREE, ptr_type_node,
3753 tree_cons (NULL_TREE, ptr_type_node,
3755 sprintf (name, "__Block_byref_id_object_copy%d", kind);
3756 block_byref_id_object_copy[kind] = build_helper_func_decl (get_identifier (name),
3758 /* Synthesize function definition. */
3759 synth_block_byref_id_object_copy_func (flag, kind);
3761 initlist = tree_cons (fields,
3762 build_fold_addr_expr (block_byref_id_object_copy[kind]),
3764 fields = TREE_CHAIN (fields);
3766 if (!block_byref_id_object_dispose[kind])
3768 /* Synthesize void __Block_byref_id_object_dispose (void*) and
3769 build &__Block_byref_id_object_dispose. */
3771 build_function_type (void_type_node,
3772 tree_cons (NULL_TREE, ptr_type_node, void_list_node));
3773 sprintf (name, "__Block_byref_id_object_dispose%d", kind);
3774 block_byref_id_object_dispose[kind] = build_helper_func_decl (get_identifier (name),
3776 /* Synthesize function definition. */
3777 synth_block_byref_id_object_dispose_func (flag, kind);
3779 initlist = tree_cons (fields,
3780 build_fold_addr_expr (block_byref_id_object_dispose[kind]),
3782 fields = TREE_CHAIN (fields);
3787 init = do_digest_init (TREE_TYPE (fields), init);
3788 initlist = tree_cons (fields, init, initlist);
3790 init = build_constructor_from_list (block_byref_type, nreverse (initlist));
3793 /* APPLE LOCAL end radar 5932809 - copyable byref blocks (C++ cr) */
3795 /* Finish processing of a declaration;
3796 install its initial value.
3797 If the length of an array type is not known before,
3798 it must be determined now, from the initial value, or it is an error. */
3801 finish_decl (tree decl, tree init, tree asmspec_tree)
3804 int was_incomplete = (DECL_SIZE (decl) == 0);
3805 const char *asmspec = 0;
3807 /* If a name was specified, get the string. */
3808 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3809 && DECL_FILE_SCOPE_P (decl))
3810 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3812 asmspec = TREE_STRING_POINTER (asmspec_tree);
3814 /* If `start_decl' didn't like having an initialization, ignore it now. */
3815 if (init != 0 && DECL_INITIAL (decl) == 0)
3818 /* Don't crash if parm is initialized. */
3819 if (TREE_CODE (decl) == PARM_DECL)
3821 /* APPLE LOCAL begin radar 5932809 - copyable byref blocks (C++ cq) */
3822 /* We build a new type for each local variable declared as __block
3823 and initialize it to a list of initializers. */
3824 else if (TREE_CODE (decl) == VAR_DECL && COPYABLE_BYREF_LOCAL_VAR (decl))
3826 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3828 error ("__block attribute on %q+D not allowed, only allowed on local variables", decl);
3829 COPYABLE_BYREF_LOCAL_VAR (decl) = 0;
3830 COPYABLE_BYREF_LOCAL_NONPOD (decl) = 0;
3835 if (objc_is_gcable_type (TREE_TYPE (decl)) == -1)
3836 flag = BLOCK_FIELD_IS_WEAK;
3837 if (block_requires_copying (decl))
3839 if (TREE_CODE (TREE_TYPE (decl)) == BLOCK_POINTER_TYPE)
3840 flag |= BLOCK_FIELD_IS_BLOCK;
3842 flag |= BLOCK_FIELD_IS_OBJECT;
3844 decl = new_block_byref_decl (decl);
3845 /* APPLE LOCAL begin radar 6289031 */
3847 if (! flag_objc_gc_only)
3850 push_cleanup (decl, build_block_byref_release_exp (decl), false);
3852 /* APPLE LOCAL end radar 6289031 */
3853 /* APPLE LOCAL begin radar 5847976 */
3854 COPYABLE_WEAK_BLOCK (decl) = ((flag & BLOCK_FIELD_IS_WEAK) != 0);
3855 init = init_byref_decl (decl, init, flag);
3856 /* APPLE LOCAL end radar 5847976 */
3859 /* APPLE LOCAL end radar 5932809 - copyable byref blocks (C++ cq) */
3862 store_init_value (decl, init);
3864 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3865 || TREE_CODE (decl) == FUNCTION_DECL
3866 || TREE_CODE (decl) == FIELD_DECL))
3867 objc_check_decl (decl);
3869 type = TREE_TYPE (decl);
3871 /* Deduce size of array from initialization, if not already known. */
3872 if (TREE_CODE (type) == ARRAY_TYPE
3873 && TYPE_DOMAIN (type) == 0
3874 && TREE_CODE (decl) != TYPE_DECL)
3877 = (TREE_STATIC (decl)
3878 /* Even if pedantic, an external linkage array
3879 may have incomplete type at first. */
3880 ? pedantic && !TREE_PUBLIC (decl)
3881 : !DECL_EXTERNAL (decl));
3883 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3886 /* Get the completed type made by complete_array_type. */
3887 type = TREE_TYPE (decl);
3892 error ("initializer fails to determine size of %q+D", decl);
3897 error ("array size missing in %q+D", decl);
3898 /* If a `static' var's size isn't known,
3899 make it extern as well as static, so it does not get
3901 If it is not `static', then do not mark extern;
3902 finish_incomplete_decl will give it a default size
3903 and it will get allocated. */
3904 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3905 DECL_EXTERNAL (decl) = 1;
3909 error ("zero or negative size array %q+D", decl);
3913 /* For global variables, update the copy of the type that
3914 exists in the binding. */
3915 if (TREE_PUBLIC (decl))
3917 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3918 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3919 b_ext = b_ext->shadowed;
3923 b_ext->type = composite_type (b_ext->type, type);
3934 if (DECL_INITIAL (decl))
3935 TREE_TYPE (DECL_INITIAL (decl)) = type;
3937 layout_decl (decl, 0);
3940 if (TREE_CODE (decl) == VAR_DECL)
3942 if (init && TREE_CODE (init) == CONSTRUCTOR)
3943 add_flexible_array_elts_to_size (decl, init);
3945 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3946 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3947 layout_decl (decl, 0);
3949 if (DECL_SIZE (decl) == 0
3950 /* Don't give an error if we already gave one earlier. */
3951 && TREE_TYPE (decl) != error_mark_node
3952 && (TREE_STATIC (decl)
3953 /* A static variable with an incomplete type
3954 is an error if it is initialized.
3955 Also if it is not file scope.
3956 Otherwise, let it through, but if it is not `extern'
3957 then it may cause an error message later. */
3958 ? (DECL_INITIAL (decl) != 0
3959 || !DECL_FILE_SCOPE_P (decl))
3960 /* An automatic variable with an incomplete type
3962 : !DECL_EXTERNAL (decl)))
3964 error ("storage size of %q+D isn%'t known", decl);
3965 TREE_TYPE (decl) = error_mark_node;
3968 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3969 && DECL_SIZE (decl) != 0)
3971 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3972 constant_expression_warning (DECL_SIZE (decl));
3974 error ("storage size of %q+D isn%'t constant", decl);
3977 if (TREE_USED (type))
3978 TREE_USED (decl) = 1;
3981 /* If this is a function and an assembler name is specified, reset DECL_RTL
3982 so we can give it its new name. Also, update built_in_decls if it
3983 was a normal built-in. */
3984 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3986 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3987 set_builtin_user_assembler_name (decl, asmspec);
3988 set_user_assembler_name (decl, asmspec);
3991 /* If #pragma weak was used, mark the decl weak now. */
3992 maybe_apply_pragma_weak (decl);
3994 /* Output the assembler code and/or RTL code for variables and functions,
3995 unless the type is an undefined structure or union.
3996 If not, it will get done when the type is completed. */
3998 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
4000 /* Determine the ELF visibility. */
4001 if (TREE_PUBLIC (decl))
4002 c_determine_visibility (decl);
4004 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4005 if (c_dialect_objc ())
4006 objc_check_decl (decl);
4010 /* If this is not a static variable, issue a warning.
4011 It doesn't make any sense to give an ASMSPEC for an
4012 ordinary, non-register local variable. Historically,
4013 GCC has accepted -- but ignored -- the ASMSPEC in
4015 if (!DECL_FILE_SCOPE_P (decl)
4016 && TREE_CODE (decl) == VAR_DECL
4017 && !C_DECL_REGISTER (decl)
4018 && !TREE_STATIC (decl))
4019 warning (0, "ignoring asm-specifier for non-static local "
4020 "variable %q+D", decl);
4022 set_user_assembler_name (decl, asmspec);
4025 if (DECL_FILE_SCOPE_P (decl))
4027 if (DECL_INITIAL (decl) == NULL_TREE
4028 || DECL_INITIAL (decl) == error_mark_node)
4029 /* Don't output anything
4030 when a tentative file-scope definition is seen.
4031 But at end of compilation, do output code for them. */
4032 DECL_DEFER_OUTPUT (decl) = 1;
4033 rest_of_decl_compilation (decl, true, 0);
4037 /* In conjunction with an ASMSPEC, the `register'
4038 keyword indicates that we should place the variable
4039 in a particular register. */
4040 if (asmspec && C_DECL_REGISTER (decl))
4042 DECL_HARD_REGISTER (decl) = 1;
4043 /* This cannot be done for a structure with volatile
4044 fields, on which DECL_REGISTER will have been
4046 if (!DECL_REGISTER (decl))
4047 error ("cannot put object with volatile field into register");
4050 if (TREE_CODE (decl) != FUNCTION_DECL)
4052 /* If we're building a variable sized type, and we might be
4053 reachable other than via the top of the current binding
4054 level, then create a new BIND_EXPR so that we deallocate
4055 the object at the right time. */
4056 /* Note that DECL_SIZE can be null due to errors. */
4057 if (DECL_SIZE (decl)
4058 && !TREE_CONSTANT (DECL_SIZE (decl))
4059 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
4062 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4063 TREE_SIDE_EFFECTS (bind) = 1;
4065 BIND_EXPR_BODY (bind) = push_stmt_list ();
4067 add_stmt (build_stmt (DECL_EXPR, decl));
4072 if (!DECL_FILE_SCOPE_P (decl))
4074 /* Recompute the RTL of a local array now
4075 if it used to be an incomplete type. */
4077 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
4079 /* If we used it already as memory, it must stay in memory. */
4080 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4081 /* If it's still incomplete now, no init will save it. */
4082 if (DECL_SIZE (decl) == 0)
4083 DECL_INITIAL (decl) = 0;
4088 /* If this was marked 'used', be sure it will be output. */
4089 if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
4090 mark_decl_referenced (decl);
4092 if (TREE_CODE (decl) == TYPE_DECL)
4094 if (!DECL_FILE_SCOPE_P (decl)
4095 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
4096 add_stmt (build_stmt (DECL_EXPR, decl));
4098 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
4101 /* At the end of a declaration, throw away any variable type sizes
4102 of types defined inside that declaration. There is no use
4103 computing them in the following function definition. */
4104 if (current_scope == file_scope)
4105 get_pending_sizes ();
4107 /* Install a cleanup (aka destructor) if one was given. */
4108 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
4110 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
4113 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
4114 tree cleanup_decl = lookup_name (cleanup_id);
4117 /* Build "cleanup(&decl)" for the destructor. */
4118 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
4119 cleanup = build_tree_list (NULL_TREE, cleanup);
4120 cleanup = build_function_call (cleanup_decl, cleanup);
4122 /* Don't warn about decl unused; the cleanup uses it. */
4123 TREE_USED (decl) = 1;
4124 TREE_USED (cleanup_decl) = 1;
4126 /* Initialize EH, if we've been told to do so. */
4127 c_maybe_initialize_eh ();
4129 push_cleanup (decl, cleanup, false);
4134 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
4137 grokparm (const struct c_parm *parm)
4139 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
4142 decl_attributes (&decl, parm->attrs, 0);
4147 /* Given a parsed parameter declaration, decode it into a PARM_DECL
4148 and push that on the current scope. */
4151 push_parm_decl (const struct c_parm *parm)
4155 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
4156 decl_attributes (&decl, parm->attrs, 0);
4158 decl = pushdecl (decl);
4160 finish_decl (decl, NULL_TREE, NULL_TREE);
4163 /* Mark all the parameter declarations to date as forward decls.
4164 Also diagnose use of this extension. */
4167 mark_forward_parm_decls (void)
4169 struct c_binding *b;
4171 if (pedantic && !current_scope->warned_forward_parm_decls)
4173 pedwarn ("ISO C forbids forward parameter declarations");
4174 current_scope->warned_forward_parm_decls = true;
4177 for (b = current_scope->bindings; b; b = b->prev)
4178 if (TREE_CODE (b->decl) == PARM_DECL)
4179 TREE_ASM_WRITTEN (b->decl) = 1;
4182 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
4183 literal, which may be an incomplete array type completed by the
4184 initializer; INIT is a CONSTRUCTOR that initializes the compound
4188 build_compound_literal (tree type, tree init)
4190 /* We do not use start_decl here because we have a type, not a declarator;
4191 and do not use finish_decl because the decl should be stored inside
4192 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
4197 if (type == error_mark_node)
4198 return error_mark_node;
4200 decl = build_decl (VAR_DECL, NULL_TREE, type);
4201 DECL_EXTERNAL (decl) = 0;
4202 TREE_PUBLIC (decl) = 0;
4203 TREE_STATIC (decl) = (current_scope == file_scope);
4204 DECL_CONTEXT (decl) = current_function_decl;
4205 TREE_USED (decl) = 1;
4206 TREE_TYPE (decl) = type;
4207 TREE_READONLY (decl) = TYPE_READONLY (type);
4208 store_init_value (decl, init);
4210 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
4212 int failure = complete_array_type (&TREE_TYPE (decl),
4213 DECL_INITIAL (decl), true);
4214 gcc_assert (!failure);
4216 type = TREE_TYPE (decl);
4217 TREE_TYPE (DECL_INITIAL (decl)) = type;
4220 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4221 return error_mark_node;
4223 stmt = build_stmt (DECL_EXPR, decl);
4224 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
4225 TREE_SIDE_EFFECTS (complit) = 1;
4227 layout_decl (decl, 0);
4229 if (TREE_STATIC (decl))
4231 /* This decl needs a name for the assembler output. */
4232 set_compound_literal_name (decl);
4233 DECL_DEFER_OUTPUT (decl) = 1;
4234 DECL_COMDAT (decl) = 1;
4235 DECL_ARTIFICIAL (decl) = 1;
4236 DECL_IGNORED_P (decl) = 1;
4238 rest_of_decl_compilation (decl, 1, 0);
4244 /* Determine whether TYPE is a structure with a flexible array member,
4245 or a union containing such a structure (possibly recursively). */
4248 flexible_array_type_p (tree type)
4251 switch (TREE_CODE (type))
4254 x = TYPE_FIELDS (type);
4257 while (TREE_CHAIN (x) != NULL_TREE)
4259 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4260 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4261 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
4262 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
4266 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
4268 if (flexible_array_type_p (TREE_TYPE (x)))
4277 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
4278 replacing with appropriate values if they are invalid. */
4280 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
4283 unsigned int max_width;
4284 unsigned HOST_WIDE_INT w;
4285 const char *name = orig_name ? orig_name: _("<anonymous>");
4287 /* Detect and ignore out of range field width and process valid
4289 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
4290 || TREE_CODE (*width) != INTEGER_CST)
4292 error ("bit-field %qs width not an integer constant", name);
4293 *width = integer_one_node;
4297 constant_expression_warning (*width);
4298 if (tree_int_cst_sgn (*width) < 0)
4300 error ("negative width in bit-field %qs", name);
4301 *width = integer_one_node;
4303 else if (integer_zerop (*width) && orig_name)
4305 error ("zero width for bit-field %qs", name);
4306 *width = integer_one_node;
4310 /* Detect invalid bit-field type. */
4311 if (TREE_CODE (*type) != INTEGER_TYPE
4312 && TREE_CODE (*type) != BOOLEAN_TYPE
4313 && TREE_CODE (*type) != ENUMERAL_TYPE)
4315 error ("bit-field %qs has invalid type", name);
4316 *type = unsigned_type_node;
4319 type_mv = TYPE_MAIN_VARIANT (*type);
4321 && !in_system_header
4322 && type_mv != integer_type_node
4323 && type_mv != unsigned_type_node
4324 && type_mv != boolean_type_node)
4325 pedwarn ("type of bit-field %qs is a GCC extension", name);
4327 if (type_mv == boolean_type_node)
4328 max_width = CHAR_TYPE_SIZE;
4330 max_width = TYPE_PRECISION (*type);
4332 if (0 < compare_tree_int (*width, max_width))
4334 error ("width of %qs exceeds its type", name);
4336 *width = build_int_cst (NULL_TREE, w);
4339 w = tree_low_cst (*width, 1);
4341 if (TREE_CODE (*type) == ENUMERAL_TYPE)
4343 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
4345 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
4346 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
4347 warning (0, "%qs is narrower than values of its type", name);
4353 /* Print warning about variable length array if necessary. */
4356 warn_variable_length_array (const char *name, tree size)
4358 int ped = !flag_isoc99 && pedantic && warn_vla != 0;
4359 int const_size = TREE_CONSTANT (size);
4366 pedwarn ("ISO C90 forbids array %qs whose size "
4367 "can%'t be evaluated",
4370 pedwarn ("ISO C90 forbids array whose size "
4371 "can%'t be evaluated");
4376 pedwarn ("ISO C90 forbids variable length array %qs",
4379 pedwarn ("ISO C90 forbids variable length array");
4382 else if (warn_vla > 0)
4388 "the size of array %qs can"
4389 "%'t be evaluated", name);
4392 "the size of array can %'t be evaluated");
4398 "variable length array %qs is used",
4402 "variable length array is used");
4407 /* Given declspecs and a declarator,
4408 determine the name and type of the object declared
4409 and construct a ..._DECL node for it.
4410 (In one case we can return a ..._TYPE node instead.
4411 For invalid input we sometimes return 0.)
4413 DECLSPECS is a c_declspecs structure for the declaration specifiers.
4415 DECL_CONTEXT says which syntactic context this declaration is in:
4416 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4417 FUNCDEF for a function definition. Like NORMAL but a few different
4418 error messages in each case. Return value may be zero meaning
4419 this definition is too screwy to try to parse.
4420 PARM for a parameter declaration (either within a function prototype
4421 or before a function body). Make a PARM_DECL, or return void_type_node.
4422 TYPENAME if for a typename (in a cast or sizeof).
4423 Don't make a DECL node; just return the ..._TYPE node.
4424 FIELD for a struct or union field; make a FIELD_DECL.
4425 INITIALIZED is true if the decl has an initializer.
4426 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4427 representing the width of the bit-field.
4429 In the TYPENAME case, DECLARATOR is really an absolute declarator.
4430 It may also be so in the PARM case, for a prototype where the
4431 argument type is specified but not the name.
4433 This function is where the complicated C meanings of `static'
4434 and `extern' are interpreted. */
4437 grokdeclarator (const struct c_declarator *declarator,
4438 struct c_declspecs *declspecs,
4439 enum decl_context decl_context, bool initialized, tree *width)
4441 tree type = declspecs->type;
4442 bool threadp = declspecs->thread_p;
4443 enum c_storage_class storage_class = declspecs->storage_class;
4447 int type_quals = TYPE_UNQUALIFIED;
4448 const char *name, *orig_name;
4449 tree typedef_type = 0;
4450 bool funcdef_flag = false;
4451 bool funcdef_syntax = false;
4452 int size_varies = 0;
4453 tree decl_attr = declspecs->decl_attr;
4454 int array_ptr_quals = TYPE_UNQUALIFIED;
4455 tree array_ptr_attrs = NULL_TREE;
4456 int array_parm_static = 0;
4457 bool array_parm_vla_unspec_p = false;
4458 tree returned_attrs = NULL_TREE;
4459 bool bitfield = width != NULL;
4461 struct c_arg_info *arg_info = 0;
4463 if (decl_context == FUNCDEF)
4464 funcdef_flag = true, decl_context = NORMAL;
4466 /* Look inside a declarator for the name being declared
4467 and get it as a string, for an error message. */
4469 const struct c_declarator *decl = declarator;
4478 /* APPLE LOCAL radar 5732232 - blocks */
4479 case cdk_block_pointer:
4480 funcdef_syntax = (decl->kind == cdk_function);
4481 decl = decl->declarator;
4485 decl = decl->declarator;
4490 name = IDENTIFIER_POINTER (decl->u.id);
4502 /* A function definition's declarator must have the form of
4503 a function declarator. */
4505 if (funcdef_flag && !funcdef_syntax)
4508 /* If this looks like a function definition, make it one,
4509 even if it occurs where parms are expected.
4510 Then store_parm_decls will reject it and not use it as a parm. */
4511 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
4512 if (declspecs->unavailable_p)
4513 error_unavailable_use (declspecs->type);
4515 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
4516 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4517 decl_context = PARM;
4519 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4520 warn_deprecated_use (declspecs->type);
4522 if ((decl_context == NORMAL || decl_context == FIELD)
4523 && current_scope == file_scope
4524 && variably_modified_type_p (type, NULL_TREE))
4526 error ("variably modified %qs at file scope", name);
4527 type = integer_type_node;
4530 typedef_type = type;
4531 size_varies = C_TYPE_VARIABLE_SIZE (type);
4533 /* Diagnose defaulting to "int". */
4535 if (declspecs->default_int_p && !in_system_header)
4537 /* Issue a warning if this is an ISO C 99 program or if
4538 -Wreturn-type and this is a function, or if -Wimplicit;
4539 prefer the former warning since it is more explicit. */
4540 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4542 warn_about_return_type = 1;
4543 else if (warn_implicit_int || flag_isoc99)
4544 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
4547 /* Adjust the type if a bit-field is being declared,
4548 -funsigned-bitfields applied and the type is not explicitly
4550 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4551 && TREE_CODE (type) == INTEGER_TYPE)
4552 type = c_common_unsigned_type (type);
4554 /* Figure out the type qualifiers for the declaration. There are
4555 two ways a declaration can become qualified. One is something
4556 like `const int i' where the `const' is explicit. Another is
4557 something like `typedef const int CI; CI i' where the type of the
4558 declaration contains the `const'. A third possibility is that
4559 there is a type qualifier on the element type of a typedefed
4560 array type, in which case we should extract that qualifier so
4561 that c_apply_type_quals_to_decls receives the full list of
4562 qualifiers to work with (C90 is not entirely clear about whether
4563 duplicate qualifiers should be diagnosed in this case, but it
4564 seems most appropriate to do so). */
4565 element_type = strip_array_types (type);
4566 constp = declspecs->const_p + TYPE_READONLY (element_type);
4567 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4568 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4569 if (pedantic && !flag_isoc99)
4572 pedwarn ("duplicate %<const%>");
4574 pedwarn ("duplicate %<restrict%>");
4576 pedwarn ("duplicate %<volatile%>");
4578 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4579 type = TYPE_MAIN_VARIANT (type);
4580 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4581 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4582 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4584 /* Warn about storage classes that are invalid for certain
4585 kinds of declarations (parameters, typenames, etc.). */
4589 || storage_class == csc_auto
4590 || storage_class == csc_register
4591 || storage_class == csc_typedef))
4593 if (storage_class == csc_auto
4594 && (pedantic || current_scope == file_scope))
4595 pedwarn ("function definition declared %<auto%>");
4596 if (storage_class == csc_register)
4597 error ("function definition declared %<register%>");
4598 if (storage_class == csc_typedef)
4599 error ("function definition declared %<typedef%>");
4601 error ("function definition declared %<__thread%>");
4603 if (storage_class == csc_auto
4604 || storage_class == csc_register
4605 || storage_class == csc_typedef)
4606 storage_class = csc_none;
4608 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4610 if (decl_context == PARM && storage_class == csc_register)
4614 switch (decl_context)
4617 error ("storage class specified for structure field %qs",
4621 error ("storage class specified for parameter %qs", name);
4624 error ("storage class specified for typename");
4627 storage_class = csc_none;
4631 else if (storage_class == csc_extern
4635 /* 'extern' with initialization is invalid if not at file scope. */
4636 if (current_scope == file_scope)
4638 /* It is fine to have 'extern const' when compiling at C
4639 and C++ intersection. */
4640 if (!(warn_cxx_compat && constp))
4641 warning (0, "%qs initialized and declared %<extern%>", name);
4644 error ("%qs has both %<extern%> and initializer", name);
4646 else if (current_scope == file_scope)
4648 if (storage_class == csc_auto)
4649 error ("file-scope declaration of %qs specifies %<auto%>", name);
4650 if (pedantic && storage_class == csc_register)
4651 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
4655 if (storage_class == csc_extern && funcdef_flag)
4656 error ("nested function %qs declared %<extern%>", name);
4657 else if (threadp && storage_class == csc_none)
4659 error ("function-scope %qs implicitly auto and declared "
4666 /* Now figure out the structure of the declarator proper.
4667 Descend through it, creating more complex types, until we reach
4668 the declared identifier (or NULL_TREE, in an absolute declarator).
4669 At each stage we maintain an unqualified version of the type
4670 together with any qualifiers that should be applied to it with
4671 c_build_qualified_type; this way, array types including
4672 multidimensional array types are first built up in unqualified
4673 form and then the qualified form is created with
4674 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4676 while (declarator && declarator->kind != cdk_id)
4678 if (type == error_mark_node)
4680 declarator = declarator->declarator;
4684 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4685 a cdk_pointer (for *...),
4686 a cdk_function (for ...(...)),
4687 a cdk_attrs (for nested attributes),
4688 or a cdk_id (for the name being declared
4689 or the place in an absolute declarator
4690 where the name was omitted).
4691 For the last case, we have just exited the loop.
4693 At this point, TYPE is the type of elements of an array,
4694 or for a function to return, or for a pointer to point to.
4695 After this sequence of ifs, TYPE is the type of the
4696 array or function or pointer, and DECLARATOR has had its
4697 outermost layer removed. */
4699 if (array_ptr_quals != TYPE_UNQUALIFIED
4700 || array_ptr_attrs != NULL_TREE
4701 || array_parm_static)
4703 /* Only the innermost declarator (making a parameter be of
4704 array type which is converted to pointer type)
4705 may have static or type qualifiers. */
4706 error ("static or type qualifiers in non-parameter array declarator");
4707 array_ptr_quals = TYPE_UNQUALIFIED;
4708 array_ptr_attrs = NULL_TREE;
4709 array_parm_static = 0;
4712 switch (declarator->kind)
4716 /* A declarator with embedded attributes. */
4717 tree attrs = declarator->u.attrs;
4718 const struct c_declarator *inner_decl;
4720 declarator = declarator->declarator;
4721 inner_decl = declarator;
4722 while (inner_decl->kind == cdk_attrs)
4723 inner_decl = inner_decl->declarator;
4724 if (inner_decl->kind == cdk_id)
4725 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4726 else if (inner_decl->kind == cdk_function)
4727 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4728 else if (inner_decl->kind == cdk_array)
4729 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4730 returned_attrs = decl_attributes (&type,
4731 chainon (returned_attrs, attrs),
4737 tree itype = NULL_TREE;
4738 tree size = declarator->u.array.dimen;
4739 /* The index is a signed object `sizetype' bits wide. */
4740 tree index_type = c_common_signed_type (sizetype);
4742 array_ptr_quals = declarator->u.array.quals;
4743 array_ptr_attrs = declarator->u.array.attrs;
4744 array_parm_static = declarator->u.array.static_p;
4745 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4747 declarator = declarator->declarator;
4749 /* Check for some types that there cannot be arrays of. */
4751 if (VOID_TYPE_P (type))
4753 error ("declaration of %qs as array of voids", name);
4754 type = error_mark_node;
4757 if (TREE_CODE (type) == FUNCTION_TYPE)
4759 error ("declaration of %qs as array of functions", name);
4760 type = error_mark_node;
4763 if (pedantic && !in_system_header && flexible_array_type_p (type))
4764 pedwarn ("invalid use of structure with flexible array member");
4766 if (size == error_mark_node)
4767 type = error_mark_node;
4769 if (type == error_mark_node)
4772 /* If size was specified, set ITYPE to a range-type for
4773 that size. Otherwise, ITYPE remains null. finish_decl
4774 may figure it out from an initial value. */
4778 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4780 STRIP_TYPE_NOPS (size);
4782 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4784 error ("size of array %qs has non-integer type", name);
4785 size = integer_one_node;
4788 if (pedantic && integer_zerop (size))
4789 pedwarn ("ISO C forbids zero-size array %qs", name);
4791 if (TREE_CODE (size) == INTEGER_CST)
4793 constant_expression_warning (size);
4794 if (tree_int_cst_sgn (size) < 0)
4796 error ("size of array %qs is negative", name);
4797 size = integer_one_node;
4800 else if ((decl_context == NORMAL || decl_context == FIELD)
4801 && current_scope == file_scope)
4803 error ("variably modified %qs at file scope", name);
4804 size = integer_one_node;
4808 /* Make sure the array size remains visibly
4809 nonconstant even if it is (eg) a const variable
4810 with known value. */
4812 warn_variable_length_array (orig_name, size);
4813 if (warn_variable_decl)
4814 warning (0, "variable-sized array %qs", name);
4817 if (integer_zerop (size))
4819 /* A zero-length array cannot be represented with
4820 an unsigned index type, which is what we'll
4821 get with build_index_type. Create an
4822 open-ended range instead. */
4823 itype = build_range_type (sizetype, size, NULL_TREE);
4827 /* Arrange for the SAVE_EXPR on the inside of the
4828 MINUS_EXPR, which allows the -1 to get folded
4829 with the +1 that happens when building TYPE_SIZE. */
4831 size = variable_size (size);
4833 /* Compute the maximum valid index, that is, size
4834 - 1. Do the calculation in index_type, so that
4835 if it is a variable the computations will be
4836 done in the proper mode. */
4837 itype = fold_build2 (MINUS_EXPR, index_type,
4838 convert (index_type, size),
4839 convert (index_type,
4842 /* If that overflowed, the array is too big. ???
4843 While a size of INT_MAX+1 technically shouldn't
4844 cause an overflow (because we subtract 1), the
4845 overflow is recorded during the conversion to
4846 index_type, before the subtraction. Handling
4847 this case seems like an unnecessary
4849 if (TREE_CODE (itype) == INTEGER_CST
4850 && TREE_OVERFLOW (itype))
4852 error ("size of array %qs is too large", name);
4853 type = error_mark_node;
4857 itype = build_index_type (itype);
4860 else if (decl_context == FIELD)
4862 if (pedantic && !flag_isoc99 && !in_system_header)
4863 pedwarn ("ISO C90 does not support flexible array members");
4865 /* ISO C99 Flexible array members are effectively
4866 identical to GCC's zero-length array extension. */
4867 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4869 else if (decl_context == PARM)
4871 if (array_parm_vla_unspec_p)
4876 error ("%<[*]%> not allowed in other than a declaration");
4879 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4883 else if (decl_context == TYPENAME)
4885 if (array_parm_vla_unspec_p)
4887 /* The error is printed elsewhere. We use this to
4888 avoid messing up with incomplete array types of
4889 the same type, that would otherwise be modified
4891 itype = build_range_type (sizetype, size_zero_node,
4896 /* Complain about arrays of incomplete types. */
4897 if (!COMPLETE_TYPE_P (type))
4899 error ("array type has incomplete element type");
4900 type = error_mark_node;
4903 /* When itype is NULL, a shared incomplete array type is
4904 returned for all array of a given type. Elsewhere we
4905 make sure we don't complete that type before copying
4906 it, but here we want to make sure we don't ever
4907 modify the shared type, so we gcc_assert (itype)
4909 type = build_array_type (type, itype);
4911 if (type != error_mark_node)
4915 /* It is ok to modify type here even if itype is
4916 NULL: if size_varies, we're in a
4917 multi-dimensional array and the inner type has
4918 variable size, so the enclosing shared array type
4920 if (size && TREE_CODE (size) == INTEGER_CST)
4922 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4923 C_TYPE_VARIABLE_SIZE (type) = 1;
4926 /* The GCC extension for zero-length arrays differs from
4927 ISO flexible array members in that sizeof yields
4929 if (size && integer_zerop (size))
4932 TYPE_SIZE (type) = bitsize_zero_node;
4933 TYPE_SIZE_UNIT (type) = size_zero_node;
4935 if (array_parm_vla_unspec_p)
4938 /* The type is complete. C99 6.7.5.2p4 */
4939 TYPE_SIZE (type) = bitsize_zero_node;
4940 TYPE_SIZE_UNIT (type) = size_zero_node;
4944 if (decl_context != PARM
4945 && (array_ptr_quals != TYPE_UNQUALIFIED
4946 || array_ptr_attrs != NULL_TREE
4947 || array_parm_static))
4949 error ("static or type qualifiers in non-parameter array declarator");
4950 array_ptr_quals = TYPE_UNQUALIFIED;
4951 array_ptr_attrs = NULL_TREE;
4952 array_parm_static = 0;
4958 /* Say it's a definition only for the declarator closest
4959 to the identifier, apart possibly from some
4961 bool really_funcdef = false;
4965 const struct c_declarator *t = declarator->declarator;
4966 while (t->kind == cdk_attrs)
4968 really_funcdef = (t->kind == cdk_id);
4971 /* Declaring a function type. Make sure we have a valid
4972 type for the function to return. */
4973 if (type == error_mark_node)
4978 /* Warn about some types functions can't return. */
4979 if (TREE_CODE (type) == FUNCTION_TYPE)
4981 error ("%qs declared as function returning a function", name);
4982 type = integer_type_node;
4984 if (TREE_CODE (type) == ARRAY_TYPE)
4986 error ("%qs declared as function returning an array", name);
4987 type = integer_type_node;
4990 /* Construct the function type and go to the next
4991 inner layer of declarator. */
4992 arg_info = declarator->u.arg_info;
4993 arg_types = grokparms (arg_info, really_funcdef);
4995 put_pending_sizes (arg_info->pending_sizes);
4997 /* Type qualifiers before the return type of the function
4998 qualify the return type, not the function type. */
5001 /* Type qualifiers on a function return type are
5002 normally permitted by the standard but have no
5003 effect, so give a warning at -Wreturn-type.
5004 Qualifiers on a void return type are banned on
5005 function definitions in ISO C; GCC used to used
5006 them for noreturn functions. */
5007 if (VOID_TYPE_P (type) && really_funcdef)
5008 pedwarn ("function definition has qualified void return type");
5010 warning (OPT_Wreturn_type,
5011 "type qualifiers ignored on function return type");
5013 type = c_build_qualified_type (type, type_quals);
5015 type_quals = TYPE_UNQUALIFIED;
5017 type = build_function_type (type, arg_types);
5018 declarator = declarator->declarator;
5020 /* Set the TYPE_CONTEXTs for each tagged type which is local to
5021 the formal parameter list of this FUNCTION_TYPE to point to
5022 the FUNCTION_TYPE node itself. */
5026 for (link = arg_info->tags;
5028 link = TREE_CHAIN (link))
5029 TYPE_CONTEXT (TREE_VALUE (link)) = type;
5035 /* Merge any constancy or volatility into the target type
5038 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5040 pedwarn ("ISO C forbids qualified function types");
5042 type = c_build_qualified_type (type, type_quals);
5045 /* When the pointed-to type involves components of variable size,
5046 care must be taken to ensure that the size evaluation code is
5047 emitted early enough to dominate all the possible later uses
5048 and late enough for the variables on which it depends to have
5051 This is expected to happen automatically when the pointed-to
5052 type has a name/declaration of it's own, but special attention
5053 is required if the type is anonymous.
5055 We handle the NORMAL and FIELD contexts here by attaching an
5056 artificial TYPE_DECL to such pointed-to type. This forces the
5057 sizes evaluation at a safe point and ensures it is not deferred
5058 until e.g. within a deeper conditional context.
5060 We expect nothing to be needed here for PARM or TYPENAME.
5061 Pushing a TYPE_DECL at this point for TYPENAME would actually
5062 be incorrect, as we might be in the middle of an expression
5063 with side effects on the pointed-to type size "arguments" prior
5064 to the pointer declaration point and the fake TYPE_DECL in the
5065 enclosing context would force the size evaluation prior to the
5068 if (!TYPE_NAME (type)
5069 && (decl_context == NORMAL || decl_context == FIELD)
5070 && variably_modified_type_p (type, NULL_TREE))
5072 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
5073 DECL_ARTIFICIAL (decl) = 1;
5075 finish_decl (decl, NULL_TREE, NULL_TREE);
5076 TYPE_NAME (type) = decl;
5079 type = build_pointer_type (type);
5081 /* Process type qualifiers (such as const or volatile)
5082 that were given inside the `*'. */
5083 type_quals = declarator->u.pointer_quals;
5085 declarator = declarator->declarator;
5090 /* APPLE LOCAL begin radar 5732232 - blocks (C++ cj) */
5091 case cdk_block_pointer:
5093 if (TREE_CODE (type) != FUNCTION_TYPE)
5095 error ("block pointer to non-function type is invalid");
5096 type = error_mark_node;
5100 type = build_block_pointer_type (type);
5101 /* APPLE LOCAL begin radar 5814025 (C++ cj) */
5102 /* Process type qualifiers (such as const or volatile)
5103 that were given inside the `^'. */
5104 type_quals = declarator->u.pointer_quals;
5105 /* APPLE LOCAL end radar 5814025 (C++ cj) */
5106 declarator = declarator->declarator;
5110 /* APPLE LOCAL end radar 5732232 - blocks (C++ cj) */
5117 /* Now TYPE has the actual type, apart from any qualifiers in
5120 /* Check the type and width of a bit-field. */
5122 check_bitfield_type_and_width (&type, width, orig_name);
5124 /* Did array size calculations overflow? */
5126 if (TREE_CODE (type) == ARRAY_TYPE
5127 && COMPLETE_TYPE_P (type)
5128 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
5129 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
5131 error ("size of array %qs is too large", name);
5132 /* If we proceed with the array type as it is, we'll eventually
5133 crash in tree_low_cst(). */
5134 type = error_mark_node;
5137 /* APPLE LOCAL begin blocks 6339747 */
5138 if (decl_context == BLOCKDEF)
5142 if (type == error_mark_node)
5143 return error_mark_node;
5145 if (TREE_CODE (type) != FUNCTION_TYPE)
5149 if (TREE_CODE (type) == ARRAY_TYPE)
5151 error ("block declared as returning an array");
5152 return error_mark_node;
5155 arg_info = XOBNEW (&parser_obstack, struct c_arg_info);
5156 arg_info->parms = 0;
5158 arg_info->types = 0;
5159 arg_info->others = 0;
5160 arg_info->pending_sizes = 0;
5161 arg_info->had_vla_unspec = 0;
5162 arg_types = grokparms (arg_info, false);
5163 type_quals = TYPE_UNQUALIFIED;
5164 type = build_function_type (type, arg_types);
5166 decl = build_decl (FUNCTION_DECL, NULL_TREE, type);
5167 DECL_ARGUMENTS (decl) = arg_info ? arg_info->parms : NULL_TREE;
5170 /* APPLE LOCAL end blocks 6339747 */
5172 /* If this is declaring a typedef name, return a TYPE_DECL. */
5174 if (storage_class == csc_typedef)
5177 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5179 pedwarn ("ISO C forbids qualified function types");
5181 type = c_build_qualified_type (type, type_quals);
5182 decl = build_decl (TYPE_DECL, declarator->u.id, type);
5183 if (declspecs->explicit_signed_p)
5184 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
5185 decl_attributes (&decl, returned_attrs, 0);
5186 if (declspecs->inline_p)
5187 pedwarn ("typedef %q+D declared %<inline%>", decl);
5191 /* If this is a type name (such as, in a cast or sizeof),
5192 compute the type and return it now. */
5194 if (decl_context == TYPENAME)
5196 /* Note that the grammar rejects storage classes in typenames
5198 gcc_assert (storage_class == csc_none && !threadp
5199 && !declspecs->inline_p);
5200 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5202 pedwarn ("ISO C forbids const or volatile function types");
5204 type = c_build_qualified_type (type, type_quals);
5205 decl_attributes (&type, returned_attrs, 0);
5209 if (pedantic && decl_context == FIELD
5210 && variably_modified_type_p (type, NULL_TREE))
5213 pedwarn ("a member of a structure or union cannot have a variably modified type");
5216 /* Aside from typedefs and type names (handle above),
5217 `void' at top level (not within pointer)
5218 is allowed only in public variables.
5219 We don't complain about parms either, but that is because
5220 a better error message can be made later. */
5222 if (VOID_TYPE_P (type) && decl_context != PARM
5223 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
5224 && (storage_class == csc_extern
5225 || (current_scope == file_scope
5226 && !(storage_class == csc_static
5227 || storage_class == csc_register)))))
5229 error ("variable or field %qs declared void", name);
5230 type = integer_type_node;
5233 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
5234 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
5239 if (decl_context == PARM)
5241 tree type_as_written;
5244 /* A parameter declared as an array of T is really a pointer to T.
5245 One declared as a function is really a pointer to a function. */
5247 if (TREE_CODE (type) == ARRAY_TYPE)
5249 /* Transfer const-ness of array into that of type pointed to. */
5250 type = TREE_TYPE (type);
5252 type = c_build_qualified_type (type, type_quals);
5253 type = build_pointer_type (type);
5254 type_quals = array_ptr_quals;
5256 /* We don't yet implement attributes in this context. */
5257 if (array_ptr_attrs != NULL_TREE)
5258 warning (OPT_Wattributes,
5259 "attributes in parameter array declarator ignored");
5263 else if (TREE_CODE (type) == FUNCTION_TYPE)
5265 if (pedantic && type_quals)
5266 pedwarn ("ISO C forbids qualified function types");
5268 type = c_build_qualified_type (type, type_quals);
5269 type = build_pointer_type (type);
5270 type_quals = TYPE_UNQUALIFIED;
5272 else if (type_quals)
5273 type = c_build_qualified_type (type, type_quals);
5275 type_as_written = type;
5277 decl = build_decl (PARM_DECL, declarator->u.id, type);
5279 C_DECL_VARIABLE_SIZE (decl) = 1;
5281 /* Compute the type actually passed in the parmlist,
5282 for the case where there is no prototype.
5283 (For example, shorts and chars are passed as ints.)
5284 When there is a prototype, this is overridden later. */
5286 if (type == error_mark_node)
5287 promoted_type = type;
5289 promoted_type = c_type_promotes_to (type);
5291 DECL_ARG_TYPE (decl) = promoted_type;
5292 if (declspecs->inline_p)
5293 pedwarn ("parameter %q+D declared %<inline%>", decl);
5295 else if (decl_context == FIELD)
5297 /* Note that the grammar rejects storage classes in typenames
5299 gcc_assert (storage_class == csc_none && !threadp
5300 && !declspecs->inline_p);
5302 /* Structure field. It may not be a function. */
5304 if (TREE_CODE (type) == FUNCTION_TYPE)
5306 error ("field %qs declared as a function", name);
5307 type = build_pointer_type (type);
5309 else if (TREE_CODE (type) != ERROR_MARK
5310 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
5312 error ("field %qs has incomplete type", name);
5313 type = error_mark_node;
5315 type = c_build_qualified_type (type, type_quals);
5316 decl = build_decl (FIELD_DECL, declarator->u.id, type);
5317 DECL_NONADDRESSABLE_P (decl) = bitfield;
5320 C_DECL_VARIABLE_SIZE (decl) = 1;
5322 else if (TREE_CODE (type) == FUNCTION_TYPE)
5324 if (storage_class == csc_register || threadp)
5326 error ("invalid storage class for function %qs", name);
5328 else if (current_scope != file_scope)
5330 /* Function declaration not at file scope. Storage
5331 classes other than `extern' are not allowed, C99
5332 6.7.1p5, and `extern' makes no difference. However,
5333 GCC allows 'auto', perhaps with 'inline', to support
5334 nested functions. */
5335 if (storage_class == csc_auto)
5338 pedwarn ("invalid storage class for function %qs", name);
5340 else if (storage_class == csc_static)
5342 error ("invalid storage class for function %qs", name);
5344 storage_class = declspecs->storage_class = csc_none;
5350 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
5351 decl = build_decl_attribute_variant (decl, decl_attr);
5353 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
5355 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
5356 pedwarn ("ISO C forbids qualified function types");
5358 /* GNU C interprets a volatile-qualified function type to indicate
5359 that the function does not return. */
5360 if ((type_quals & TYPE_QUAL_VOLATILE)
5361 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5362 warning (0, "%<noreturn%> function returns non-void value");
5364 /* Every function declaration is an external reference
5365 (DECL_EXTERNAL) except for those which are not at file
5366 scope and are explicitly declared "auto". This is
5367 forbidden by standard C (C99 6.7.1p5) and is interpreted by
5368 GCC to signify a forward declaration of a nested function. */
5369 if (storage_class == csc_auto && current_scope != file_scope)
5370 DECL_EXTERNAL (decl) = 0;
5371 /* In C99, a function which is declared 'inline' with 'extern'
5372 is not an external reference (which is confusing). It
5373 means that the later definition of the function must be output
5374 in this file, C99 6.7.4p6. In GNU C89, a function declared
5375 'extern inline' is an external reference. */
5376 else if (declspecs->inline_p && storage_class != csc_static)
5377 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
5378 == flag_gnu89_inline);
5380 DECL_EXTERNAL (decl) = !initialized;
5382 /* Record absence of global scope for `static' or `auto'. */
5384 = !(storage_class == csc_static || storage_class == csc_auto);
5386 /* For a function definition, record the argument information
5387 block where store_parm_decls will look for it. */
5389 current_function_arg_info = arg_info;
5391 if (declspecs->default_int_p)
5392 C_FUNCTION_IMPLICIT_INT (decl) = 1;
5394 /* Record presence of `inline', if it is reasonable. */
5395 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
5397 if (declspecs->inline_p)
5398 pedwarn ("cannot inline function %<main%>");
5400 else if (declspecs->inline_p)
5402 /* Record that the function is declared `inline'. */
5403 DECL_DECLARED_INLINE_P (decl) = 1;
5405 /* Do not mark bare declarations as DECL_INLINE. Doing so
5406 in the presence of multiple declarations can result in
5407 the abstract origin pointing between the declarations,
5408 which will confuse dwarf2out. */
5410 DECL_INLINE (decl) = 1;
5412 /* If -finline-functions, assume it can be inlined. This does
5413 two things: let the function be deferred until it is actually
5414 needed, and let dwarf2 know that the function is inlinable. */
5415 else if (flag_inline_trees == 2 && initialized)
5416 DECL_INLINE (decl) = 1;
5420 /* It's a variable. */
5421 /* An uninitialized decl with `extern' is a reference. */
5422 int extern_ref = !initialized && storage_class == csc_extern;
5424 type = c_build_qualified_type (type, type_quals);
5426 /* C99 6.2.2p7: It is invalid (compile-time undefined
5427 behavior) to create an 'extern' declaration for a
5428 variable if there is a global declaration that is
5429 'static' and the global declaration is not visible.
5430 (If the static declaration _is_ currently visible,
5431 the 'extern' declaration is taken to refer to that decl.) */
5432 if (extern_ref && current_scope != file_scope)
5434 tree global_decl = identifier_global_value (declarator->u.id);
5435 tree visible_decl = lookup_name (declarator->u.id);
5438 && global_decl != visible_decl
5439 && TREE_CODE (global_decl) == VAR_DECL
5440 && !TREE_PUBLIC (global_decl))
5441 error ("variable previously declared %<static%> redeclared "
5445 decl = build_decl (VAR_DECL, declarator->u.id, type);
5446 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5448 C_DECL_VARIABLE_SIZE (decl) = 1;
5450 if (declspecs->inline_p)
5451 pedwarn ("variable %q+D declared %<inline%>", decl);
5453 /* At file scope, an initialized extern declaration may follow
5454 a static declaration. In that case, DECL_EXTERNAL will be
5455 reset later in start_decl. */
5456 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
5458 /* At file scope, the presence of a `static' or `register' storage
5459 class specifier, or the absence of all storage class specifiers
5460 makes this declaration a definition (perhaps tentative). Also,
5461 the absence of `static' makes it public. */
5462 if (current_scope == file_scope)
5464 TREE_PUBLIC (decl) = storage_class != csc_static;
5465 TREE_STATIC (decl) = !extern_ref;
5467 /* Not at file scope, only `static' makes a static definition. */
5470 TREE_STATIC (decl) = (storage_class == csc_static);
5471 TREE_PUBLIC (decl) = extern_ref;
5476 if (targetm.have_tls)
5477 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
5479 /* A mere warning is sure to result in improper semantics
5480 at runtime. Don't bother to allow this to compile. */
5481 error ("thread-local storage not supported for this target");
5485 if (storage_class == csc_extern
5486 && variably_modified_type_p (type, NULL_TREE))
5489 error ("object with variably modified type must have no linkage");
5492 /* Record `register' declaration for warnings on &
5493 and in case doing stupid register allocation. */
5495 if (storage_class == csc_register)
5497 C_DECL_REGISTER (decl) = 1;
5498 DECL_REGISTER (decl) = 1;
5501 /* Record constancy and volatility. */
5502 c_apply_type_quals_to_decl (type_quals, decl);
5504 /* If a type has volatile components, it should be stored in memory.
5505 Otherwise, the fact that those components are volatile
5506 will be ignored, and would even crash the compiler.
5507 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
5508 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
5509 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
5510 || TREE_CODE (decl) == RESULT_DECL))
5512 /* It is not an error for a structure with volatile fields to
5513 be declared register, but reset DECL_REGISTER since it
5514 cannot actually go in a register. */
5515 int was_reg = C_DECL_REGISTER (decl);
5516 C_DECL_REGISTER (decl) = 0;
5517 DECL_REGISTER (decl) = 0;
5518 c_mark_addressable (decl);
5519 C_DECL_REGISTER (decl) = was_reg;
5522 /* This is the earliest point at which we might know the assembler
5523 name of a variable. Thus, if it's known before this, die horribly. */
5524 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
5526 decl_attributes (&decl, returned_attrs, 0);
5532 /* Decode the parameter-list info for a function type or function definition.
5533 The argument is the value returned by `get_parm_info' (or made in c-parse.c
5534 if there is an identifier list instead of a parameter decl list).
5535 These two functions are separate because when a function returns
5536 or receives functions then each is called multiple times but the order
5537 of calls is different. The last call to `grokparms' is always the one
5538 that contains the formal parameter names of a function definition.
5540 Return a list of arg types to use in the FUNCTION_TYPE for this function.
5542 FUNCDEF_FLAG is true for a function definition, false for
5543 a mere declaration. A nonempty identifier-list gets an error message
5544 when FUNCDEF_FLAG is false. */
5547 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
5549 tree arg_types = arg_info->types;
5551 if (funcdef_flag && arg_info->had_vla_unspec)
5553 /* A function definition isn't function prototype scope C99 6.2.1p4. */
5555 error ("%<[*]%> not allowed in other than function prototype scope");
5558 if (arg_types == 0 && !funcdef_flag && !in_system_header)
5559 warning (OPT_Wstrict_prototypes,
5560 "function declaration isn%'t a prototype");
5562 if (arg_types == error_mark_node)
5563 return 0; /* don't set TYPE_ARG_TYPES in this case */
5565 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5568 pedwarn ("parameter names (without types) in function declaration");
5570 arg_info->parms = arg_info->types;
5571 arg_info->types = 0;
5576 tree parm, type, typelt;
5577 unsigned int parmno;
5579 /* If there is a parameter of incomplete type in a definition,
5580 this is an error. In a declaration this is valid, and a
5581 struct or union type may be completed later, before any calls
5582 or definition of the function. In the case where the tag was
5583 first declared within the parameter list, a warning has
5584 already been given. If a parameter has void type, then
5585 however the function cannot be defined or called, so
5588 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5590 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5592 type = TREE_VALUE (typelt);
5593 if (type == error_mark_node)
5596 if (!COMPLETE_TYPE_P (type))
5600 if (DECL_NAME (parm))
5601 error ("parameter %u (%q+D) has incomplete type",
5604 error ("%Jparameter %u has incomplete type",
5607 TREE_VALUE (typelt) = error_mark_node;
5608 TREE_TYPE (parm) = error_mark_node;
5610 else if (VOID_TYPE_P (type))
5612 if (DECL_NAME (parm))
5613 warning (0, "parameter %u (%q+D) has void type",
5616 warning (0, "%Jparameter %u has void type",
5621 if (DECL_NAME (parm) && TREE_USED (parm))
5622 warn_if_shadowing (parm);
5628 /* Take apart the current scope and return a c_arg_info structure with
5629 info on a parameter list just parsed.
5631 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5633 ELLIPSIS being true means the argument list ended in '...' so don't
5634 append a sentinel (void_list_node) to the end of the type-list. */
5637 get_parm_info (bool ellipsis)
5639 struct c_binding *b = current_scope->bindings;
5640 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5647 static bool explained_incomplete_types = false;
5648 bool gave_void_only_once_err = false;
5650 arg_info->parms = 0;
5652 arg_info->types = 0;
5653 arg_info->others = 0;
5654 arg_info->pending_sizes = 0;
5655 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5657 /* The bindings in this scope must not get put into a block.
5658 We will take care of deleting the binding nodes. */
5659 current_scope->bindings = 0;
5661 /* This function is only called if there was *something* on the
5665 /* A parameter list consisting solely of 'void' indicates that the
5666 function takes no arguments. But if the 'void' is qualified
5667 (by 'const' or 'volatile'), or has a storage class specifier
5668 ('register'), then the behavior is undefined; issue an error.
5669 Typedefs for 'void' are OK (see DR#157). */
5670 if (b->prev == 0 /* one binding */
5671 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5672 && !DECL_NAME (b->decl) /* anonymous */
5673 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5675 if (TREE_THIS_VOLATILE (b->decl)
5676 || TREE_READONLY (b->decl)
5677 || C_DECL_REGISTER (b->decl))
5678 error ("%<void%> as only parameter may not be qualified");
5680 /* There cannot be an ellipsis. */
5682 error ("%<void%> must be the only parameter");
5684 arg_info->types = void_list_node;
5689 types = void_list_node;
5691 /* Break up the bindings list into parms, tags, types, and others;
5692 apply sanity checks; purge the name-to-decl bindings. */
5695 tree decl = b->decl;
5696 tree type = TREE_TYPE (decl);
5697 const char *keyword;
5699 switch (TREE_CODE (decl))
5704 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5705 I_SYMBOL_BINDING (b->id) = b->shadowed;
5708 /* Check for forward decls that never got their actual decl. */
5709 if (TREE_ASM_WRITTEN (decl))
5710 error ("parameter %q+D has just a forward declaration", decl);
5711 /* Check for (..., void, ...) and issue an error. */
5712 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5714 if (!gave_void_only_once_err)
5716 error ("%<void%> must be the only parameter");
5717 gave_void_only_once_err = true;
5722 /* Valid parameter, add it to the list. */
5723 TREE_CHAIN (decl) = parms;
5726 /* Since there is a prototype, args are passed in their
5727 declared types. The back end may override this later. */
5728 DECL_ARG_TYPE (decl) = type;
5729 types = tree_cons (0, type, types);
5733 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5734 case UNION_TYPE: keyword = "union"; goto tag;
5735 case RECORD_TYPE: keyword = "struct"; goto tag;
5737 /* Types may not have tag-names, in which case the type
5738 appears in the bindings list with b->id NULL. */
5741 gcc_assert (I_TAG_BINDING (b->id) == b);
5742 I_TAG_BINDING (b->id) = b->shadowed;
5745 /* Warn about any struct, union or enum tags defined in a
5746 parameter list. The scope of such types is limited to
5747 the parameter list, which is rarely if ever desirable
5748 (it's impossible to call such a function with type-
5749 correct arguments). An anonymous union parm type is
5750 meaningful as a GNU extension, so don't warn for that. */
5751 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5754 /* The %s will be one of 'struct', 'union', or 'enum'. */
5755 warning (0, "%<%s %E%> declared inside parameter list",
5758 /* The %s will be one of 'struct', 'union', or 'enum'. */
5759 warning (0, "anonymous %s declared inside parameter list",
5762 if (!explained_incomplete_types)
5764 warning (0, "its scope is only this definition or declaration,"
5765 " which is probably not what you want");
5766 explained_incomplete_types = true;
5770 tags = tree_cons (b->id, decl, tags);
5776 /* CONST_DECLs appear here when we have an embedded enum,
5777 and TYPE_DECLs appear here when we have an embedded struct
5778 or union. No warnings for this - we already warned about the
5779 type itself. FUNCTION_DECLs appear when there is an implicit
5780 function declaration in the parameter list. */
5782 TREE_CHAIN (decl) = others;
5787 /* error_mark_node appears here when we have an undeclared
5788 variable. Just throw it away. */
5791 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5792 I_SYMBOL_BINDING (b->id) = b->shadowed;
5796 /* Other things that might be encountered. */
5803 b = free_binding_and_advance (b);
5806 arg_info->parms = parms;
5807 arg_info->tags = tags;
5808 arg_info->types = types;
5809 arg_info->others = others;
5810 arg_info->pending_sizes = get_pending_sizes ();
5814 /* Get the struct, enum or union (CODE says which) with tag NAME.
5815 Define the tag as a forward-reference if it is not defined.
5816 Return a c_typespec structure for the type specifier. */
5819 parser_xref_tag (enum tree_code code, tree name)
5821 struct c_typespec ret;
5822 /* If a cross reference is requested, look up the type
5823 already defined for this tag and return it. */
5825 tree ref = lookup_tag (code, name, 0);
5826 /* If this is the right type of tag, return what we found.
5827 (This reference will be shadowed by shadow_tag later if appropriate.)
5828 If this is the wrong type of tag, do not return it. If it was the
5829 wrong type in the same scope, we will have had an error
5830 message already; if in a different scope and declaring
5831 a name, pending_xref_error will give an error message; but if in a
5832 different scope and not declaring a name, this tag should
5833 shadow the previous declaration of a different type of tag, and
5834 this would not work properly if we return the reference found.
5835 (For example, with "struct foo" in an outer scope, "union foo;"
5836 must shadow that tag with a new one of union type.) */
5837 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5838 if (ref && TREE_CODE (ref) == code)
5844 /* If no such tag is yet defined, create a forward-reference node
5845 and record it as the "definition".
5846 When a real declaration of this type is found,
5847 the forward-reference will be altered into a real type. */
5849 ref = make_node (code);
5850 if (code == ENUMERAL_TYPE)
5852 /* Give the type a default layout like unsigned int
5853 to avoid crashing if it does not get defined. */
5854 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5855 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5856 TYPE_USER_ALIGN (ref) = 0;
5857 TYPE_UNSIGNED (ref) = 1;
5858 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5859 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5860 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5863 pushtag (name, ref);
5869 /* Get the struct, enum or union (CODE says which) with tag NAME.
5870 Define the tag as a forward-reference if it is not defined.
5871 Return a tree for the type. */
5874 xref_tag (enum tree_code code, tree name)
5876 return parser_xref_tag (code, name).spec;
5879 /* Make sure that the tag NAME is defined *in the current scope*
5880 at least as a forward reference.
5881 CODE says which kind of tag NAME ought to be. */
5884 start_struct (enum tree_code code, tree name)
5886 /* If there is already a tag defined at this scope
5887 (as a forward reference), just return it. */
5892 ref = lookup_tag (code, name, 1);
5893 if (ref && TREE_CODE (ref) == code)
5895 if (TYPE_SIZE (ref))
5897 if (code == UNION_TYPE)
5898 error ("redefinition of %<union %E%>", name);
5900 error ("redefinition of %<struct %E%>", name);
5902 else if (C_TYPE_BEING_DEFINED (ref))
5904 if (code == UNION_TYPE)
5905 error ("nested redefinition of %<union %E%>", name);
5907 error ("nested redefinition of %<struct %E%>", name);
5908 /* Don't create structures that contain themselves. */
5913 /* Otherwise create a forward-reference just so the tag is in scope. */
5915 if (ref == NULL_TREE || TREE_CODE (ref) != code)
5917 ref = make_node (code);
5918 pushtag (name, ref);
5921 C_TYPE_BEING_DEFINED (ref) = 1;
5922 TYPE_PACKED (ref) = flag_pack_struct;
5926 /* Process the specs, declarator and width (NULL if omitted)
5927 of a structure component, returning a FIELD_DECL node.
5928 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5930 This is done during the parsing of the struct declaration.
5931 The FIELD_DECL nodes are chained together and the lot of them
5932 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5935 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5940 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5941 && width == NULL_TREE)
5943 /* This is an unnamed decl.
5945 If we have something of the form "union { list } ;" then this
5946 is the anonymous union extension. Similarly for struct.
5948 If this is something of the form "struct foo;", then
5949 If MS extensions are enabled, this is handled as an
5951 Otherwise this is a forward declaration of a structure tag.
5953 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5954 If MS extensions are enabled and foo names a structure, then
5955 again this is an anonymous struct.
5956 Otherwise this is an error.
5958 Oh what a horrid tangled web we weave. I wonder if MS consciously
5959 took this from Plan 9 or if it was an accident of implementation
5960 that took root before someone noticed the bug... */
5962 tree type = declspecs->type;
5963 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5964 || TREE_CODE (type) == UNION_TYPE);
5968 && (flag_ms_extensions || !declspecs->typedef_p))
5970 if (flag_ms_extensions)
5974 else if (TYPE_NAME (type) == NULL)
5981 pedwarn ("declaration does not declare anything");
5985 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5988 value = grokdeclarator (declarator, declspecs, FIELD, false,
5989 width ? &width : NULL);
5991 finish_decl (value, NULL_TREE, NULL_TREE);
5992 DECL_INITIAL (value) = width;
5997 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5998 the list such that this does not present a problem later. */
6001 detect_field_duplicates (tree fieldlist)
6006 /* First, see if there are more than "a few" fields.
6007 This is trivially true if there are zero or one fields. */
6010 x = TREE_CHAIN (fieldlist);
6016 } while (timeout > 0 && x);
6018 /* If there were "few" fields, avoid the overhead of allocating
6019 a hash table. Instead just do the nested traversal thing. */
6022 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
6025 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
6026 if (DECL_NAME (y) == DECL_NAME (x))
6028 error ("duplicate member %q+D", x);
6029 DECL_NAME (x) = NULL_TREE;
6035 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
6038 for (x = fieldlist; x ; x = TREE_CHAIN (x))
6039 if ((y = DECL_NAME (x)) != 0)
6041 slot = htab_find_slot (htab, y, INSERT);
6044 error ("duplicate member %q+D", x);
6045 DECL_NAME (x) = NULL_TREE;
6054 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
6055 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
6056 ATTRIBUTES are attributes to be applied to the structure. */
6059 finish_struct (tree t, tree fieldlist, tree attributes)
6062 bool toplevel = file_scope == current_scope;
6063 int saw_named_field;
6065 /* If this type was previously laid out as a forward reference,
6066 make sure we lay it out again. */
6070 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6074 for (x = fieldlist; x; x = TREE_CHAIN (x))
6075 if (DECL_NAME (x) != 0)
6080 if (TREE_CODE (t) == UNION_TYPE)
6083 pedwarn ("union has no named members");
6085 pedwarn ("union has no members");
6090 pedwarn ("struct has no named members");
6092 pedwarn ("struct has no members");
6097 /* Install struct as DECL_CONTEXT of each field decl.
6098 Also process specified field sizes, found in the DECL_INITIAL,
6099 storing 0 there after the type has been changed to precision equal
6100 to its width, rather than the precision of the specified standard
6101 type. (Correct layout requires the original type to have been preserved
6104 saw_named_field = 0;
6105 for (x = fieldlist; x; x = TREE_CHAIN (x))
6107 if (TREE_TYPE (x) == error_mark_node)
6110 DECL_CONTEXT (x) = t;
6112 if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
6113 DECL_PACKED (x) = 1;
6115 /* If any field is const, the structure type is pseudo-const. */
6116 if (TREE_READONLY (x))
6117 C_TYPE_FIELDS_READONLY (t) = 1;
6120 /* A field that is pseudo-const makes the structure likewise. */
6121 tree t1 = TREE_TYPE (x);
6122 while (TREE_CODE (t1) == ARRAY_TYPE)
6123 t1 = TREE_TYPE (t1);
6124 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
6125 && C_TYPE_FIELDS_READONLY (t1))
6126 C_TYPE_FIELDS_READONLY (t) = 1;
6129 /* Any field that is volatile means variables of this type must be
6130 treated in some ways as volatile. */
6131 if (TREE_THIS_VOLATILE (x))
6132 C_TYPE_FIELDS_VOLATILE (t) = 1;
6134 /* Any field of nominal variable size implies structure is too. */
6135 if (C_DECL_VARIABLE_SIZE (x))
6136 C_TYPE_VARIABLE_SIZE (t) = 1;
6138 if (DECL_INITIAL (x))
6140 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
6141 DECL_SIZE (x) = bitsize_int (width);
6142 DECL_BIT_FIELD (x) = 1;
6143 SET_DECL_C_BIT_FIELD (x);
6146 /* Detect flexible array member in an invalid context. */
6147 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6148 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
6149 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
6150 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
6152 if (TREE_CODE (t) == UNION_TYPE)
6154 error ("%Jflexible array member in union", x);
6155 TREE_TYPE (x) = error_mark_node;
6157 else if (TREE_CHAIN (x) != NULL_TREE)
6159 error ("%Jflexible array member not at end of struct", x);
6160 TREE_TYPE (x) = error_mark_node;
6162 else if (!saw_named_field)
6164 error ("%Jflexible array member in otherwise empty struct", x);
6165 TREE_TYPE (x) = error_mark_node;
6169 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
6170 && flexible_array_type_p (TREE_TYPE (x)))
6171 pedwarn ("%Jinvalid use of structure with flexible array member", x);
6174 saw_named_field = 1;
6177 detect_field_duplicates (fieldlist);
6179 /* Now we have the nearly final fieldlist. Record it,
6180 then lay out the structure or union (including the fields). */
6182 TYPE_FIELDS (t) = fieldlist;
6186 /* Give bit-fields their proper types. */
6188 tree *fieldlistp = &fieldlist;
6190 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
6191 && TREE_TYPE (*fieldlistp) != error_mark_node)
6193 unsigned HOST_WIDE_INT width
6194 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
6195 tree type = TREE_TYPE (*fieldlistp);
6196 if (width != TYPE_PRECISION (type))
6198 TREE_TYPE (*fieldlistp)
6199 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
6200 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
6202 DECL_INITIAL (*fieldlistp) = 0;
6205 fieldlistp = &TREE_CHAIN (*fieldlistp);
6208 /* Now we have the truly final field list.
6209 Store it in this type and in the variants. */
6211 TYPE_FIELDS (t) = fieldlist;
6213 /* If there are lots of fields, sort so we can look through them fast.
6214 We arbitrarily consider 16 or more elts to be "a lot". */
6219 for (x = fieldlist; x; x = TREE_CHAIN (x))
6221 if (len > 15 || DECL_NAME (x) == NULL)
6229 struct lang_type *space;
6230 struct sorted_fields_type *space2;
6232 len += list_length (x);
6234 /* Use the same allocation policy here that make_node uses, to
6235 ensure that this lives as long as the rest of the struct decl.
6236 All decls in an inline function need to be saved. */
6238 space = GGC_CNEW (struct lang_type);
6239 space2 = GGC_NEWVAR (struct sorted_fields_type,
6240 sizeof (struct sorted_fields_type) + len * sizeof (tree));
6244 field_array = &space2->elts[0];
6245 for (x = fieldlist; x; x = TREE_CHAIN (x))
6247 field_array[len++] = x;
6249 /* If there is anonymous struct or union, break out of the loop. */
6250 if (DECL_NAME (x) == NULL)
6253 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
6256 TYPE_LANG_SPECIFIC (t) = space;
6257 TYPE_LANG_SPECIFIC (t)->s->len = len;
6258 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
6259 qsort (field_array, len, sizeof (tree), field_decl_cmp);
6264 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6266 TYPE_FIELDS (x) = TYPE_FIELDS (t);
6267 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
6268 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
6269 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
6270 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
6273 /* If this was supposed to be a transparent union, but we can't
6274 make it one, warn and turn off the flag. */
6275 if (TREE_CODE (t) == UNION_TYPE
6276 && TYPE_TRANSPARENT_UNION (t)
6277 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
6279 TYPE_TRANSPARENT_UNION (t) = 0;
6280 warning (0, "union cannot be made transparent");
6283 /* If this structure or union completes the type of any previous
6284 variable declaration, lay it out and output its rtl. */
6285 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
6289 tree decl = TREE_VALUE (x);
6290 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6291 layout_array_type (TREE_TYPE (decl));
6292 if (TREE_CODE (decl) != TYPE_DECL)
6294 layout_decl (decl, 0);
6295 if (c_dialect_objc ())
6296 objc_check_decl (decl);
6297 rest_of_decl_compilation (decl, toplevel, 0);
6302 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
6304 /* Finish debugging output for this type. */
6305 rest_of_type_compilation (t, toplevel);
6307 /* If we're inside a function proper, i.e. not file-scope and not still
6308 parsing parameters, then arrange for the size of a variable sized type
6310 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
6311 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
6316 /* Lay out the type T, and its element type, and so on. */
6319 layout_array_type (tree t)
6321 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6322 layout_array_type (TREE_TYPE (t));
6326 /* Begin compiling the definition of an enumeration type.
6327 NAME is its name (or null if anonymous).
6328 Returns the type object, as yet incomplete.
6329 Also records info about it so that build_enumerator
6330 may be used to declare the individual values as they are read. */
6333 start_enum (tree name)
6337 /* If this is the real definition for a previous forward reference,
6338 fill in the contents in the same object that used to be the
6339 forward reference. */
6342 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
6344 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
6346 enumtype = make_node (ENUMERAL_TYPE);
6347 pushtag (name, enumtype);
6350 if (C_TYPE_BEING_DEFINED (enumtype))
6351 error ("nested redefinition of %<enum %E%>", name);
6353 C_TYPE_BEING_DEFINED (enumtype) = 1;
6355 if (TYPE_VALUES (enumtype) != 0)
6357 /* This enum is a named one that has been declared already. */
6358 error ("redeclaration of %<enum %E%>", name);
6360 /* Completely replace its old definition.
6361 The old enumerators remain defined, however. */
6362 TYPE_VALUES (enumtype) = 0;
6365 enum_next_value = integer_zero_node;
6368 if (flag_short_enums)
6369 TYPE_PACKED (enumtype) = 1;
6374 /* After processing and defining all the values of an enumeration type,
6375 install their decls in the enumeration type and finish it off.
6376 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
6377 and ATTRIBUTES are the specified attributes.
6378 Returns ENUMTYPE. */
6381 finish_enum (tree enumtype, tree values, tree attributes)
6384 tree minnode = 0, maxnode = 0;
6385 int precision, unsign;
6386 bool toplevel = (file_scope == current_scope);
6387 struct lang_type *lt;
6389 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6391 /* Calculate the maximum value of any enumerator in this type. */
6393 if (values == error_mark_node)
6394 minnode = maxnode = integer_zero_node;
6397 minnode = maxnode = TREE_VALUE (values);
6398 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
6400 tree value = TREE_VALUE (pair);
6401 if (tree_int_cst_lt (maxnode, value))
6403 if (tree_int_cst_lt (value, minnode))
6408 /* Construct the final type of this enumeration. It is the same
6409 as one of the integral types - the narrowest one that fits, except
6410 that normally we only go as narrow as int - and signed iff any of
6411 the values are negative. */
6412 unsign = (tree_int_cst_sgn (minnode) >= 0);
6413 precision = MAX (min_precision (minnode, unsign),
6414 min_precision (maxnode, unsign));
6416 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
6418 tem = c_common_type_for_size (precision, unsign);
6421 warning (0, "enumeration values exceed range of largest integer");
6422 tem = long_long_integer_type_node;
6426 tem = unsign ? unsigned_type_node : integer_type_node;
6428 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
6429 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
6430 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
6431 TYPE_SIZE (enumtype) = 0;
6433 /* If the precision of the type was specific with an attribute and it
6434 was too small, give an error. Otherwise, use it. */
6435 if (TYPE_PRECISION (enumtype))
6437 if (precision > TYPE_PRECISION (enumtype))
6438 error ("specified mode too small for enumeral values");
6441 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
6443 layout_type (enumtype);
6445 if (values != error_mark_node)
6447 /* Change the type of the enumerators to be the enum type. We
6448 need to do this irrespective of the size of the enum, for
6449 proper type checking. Replace the DECL_INITIALs of the
6450 enumerators, and the value slots of the list, with copies
6451 that have the enum type; they cannot be modified in place
6452 because they may be shared (e.g. integer_zero_node) Finally,
6453 change the purpose slots to point to the names of the decls. */
6454 for (pair = values; pair; pair = TREE_CHAIN (pair))
6456 tree enu = TREE_PURPOSE (pair);
6457 tree ini = DECL_INITIAL (enu);
6459 TREE_TYPE (enu) = enumtype;
6461 /* The ISO C Standard mandates enumerators to have type int,
6462 even though the underlying type of an enum type is
6463 unspecified. Here we convert any enumerators that fit in
6464 an int to type int, to avoid promotions to unsigned types
6465 when comparing integers with enumerators that fit in the
6466 int range. When -pedantic is given, build_enumerator()
6467 would have already taken care of those that don't fit. */
6468 if (int_fits_type_p (ini, integer_type_node))
6469 tem = integer_type_node;
6472 ini = convert (tem, ini);
6474 DECL_INITIAL (enu) = ini;
6475 TREE_PURPOSE (pair) = DECL_NAME (enu);
6476 TREE_VALUE (pair) = ini;
6479 TYPE_VALUES (enumtype) = values;
6482 /* Record the min/max values so that we can warn about bit-field
6483 enumerations that are too small for the values. */
6484 lt = GGC_CNEW (struct lang_type);
6485 lt->enum_min = minnode;
6486 lt->enum_max = maxnode;
6487 TYPE_LANG_SPECIFIC (enumtype) = lt;
6489 /* Fix up all variant types of this enum type. */
6490 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
6492 if (tem == enumtype)
6494 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
6495 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
6496 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
6497 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
6498 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
6499 TYPE_MODE (tem) = TYPE_MODE (enumtype);
6500 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
6501 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
6502 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
6503 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
6504 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
6507 /* Finish debugging output for this type. */
6508 rest_of_type_compilation (enumtype, toplevel);
6513 /* Build and install a CONST_DECL for one value of the
6514 current enumeration type (one that was begun with start_enum).
6515 Return a tree-list containing the CONST_DECL and its value.
6516 Assignment of sequential values by default is handled here. */
6519 build_enumerator (tree name, tree value)
6523 /* Validate and default VALUE. */
6527 /* Don't issue more errors for error_mark_node (i.e. an
6528 undeclared identifier) - just ignore the value expression. */
6529 if (value == error_mark_node)
6531 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
6532 || TREE_CODE (value) != INTEGER_CST)
6534 error ("enumerator value for %qE is not an integer constant", name);
6539 value = default_conversion (value);
6540 constant_expression_warning (value);
6544 /* Default based on previous value. */
6545 /* It should no longer be possible to have NON_LVALUE_EXPR
6549 value = enum_next_value;
6551 error ("overflow in enumeration values");
6554 if (pedantic && !int_fits_type_p (value, integer_type_node))
6556 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
6557 /* XXX This causes -pedantic to change the meaning of the program.
6558 Remove? -zw 2004-03-15 */
6559 value = convert (integer_type_node, value);
6562 /* Set basis for default for next value. */
6563 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
6564 enum_overflow = tree_int_cst_lt (enum_next_value, value);
6566 /* Now create a declaration for the enum value name. */
6568 type = TREE_TYPE (value);
6569 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6570 TYPE_PRECISION (integer_type_node)),
6571 (TYPE_PRECISION (type)
6572 >= TYPE_PRECISION (integer_type_node)
6573 && TYPE_UNSIGNED (type)));
6575 decl = build_decl (CONST_DECL, name, type);
6576 DECL_INITIAL (decl) = convert (type, value);
6579 return tree_cons (decl, value, NULL_TREE);
6583 /* Create the FUNCTION_DECL for a function definition.
6584 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6585 the declaration; they describe the function's name and the type it returns,
6586 but twisted together in a fashion that parallels the syntax of C.
6588 This function creates a binding context for the function body
6589 as well as setting up the FUNCTION_DECL in current_function_decl.
6591 Returns 1 on success. If the DECLARATOR is not suitable for a function
6592 (it defines a datum instead), we return 0, which tells
6593 yyparse to report a parse error. */
6596 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6599 tree decl1, old_decl;
6600 tree restype, resdecl;
6601 struct c_label_context_se *nstack_se;
6602 struct c_label_context_vm *nstack_vm;
6604 current_function_returns_value = 0; /* Assume, until we see it does. */
6605 current_function_returns_null = 0;
6606 current_function_returns_abnormally = 0;
6607 warn_about_return_type = 0;
6608 c_switch_stack = NULL;
6610 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6611 nstack_se->labels_def = NULL;
6612 nstack_se->labels_used = NULL;
6613 nstack_se->next = label_context_stack_se;
6614 label_context_stack_se = nstack_se;
6616 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6617 nstack_vm->labels_def = NULL;
6618 nstack_vm->labels_used = NULL;
6619 nstack_vm->scope = 0;
6620 nstack_vm->next = label_context_stack_vm;
6621 label_context_stack_vm = nstack_vm;
6623 /* Indicate no valid break/continue context by setting these variables
6624 to some non-null, non-label value. We'll notice and emit the proper
6625 error message in c_finish_bc_stmt. */
6626 c_break_label = c_cont_label = size_zero_node;
6628 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
6630 /* If the declarator is not suitable for a function definition,
6631 cause a syntax error. */
6634 label_context_stack_se = label_context_stack_se->next;
6635 label_context_stack_vm = label_context_stack_vm->next;
6639 decl_attributes (&decl1, attributes, 0);
6641 if (DECL_DECLARED_INLINE_P (decl1)
6642 && DECL_UNINLINABLE (decl1)
6643 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6644 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6647 /* Handle gnu_inline attribute. */
6648 if (declspecs->inline_p
6649 && !flag_gnu89_inline
6650 && TREE_CODE (decl1) == FUNCTION_DECL
6651 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1)))
6653 if (declspecs->storage_class != csc_static)
6654 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6657 announce_function (decl1);
6659 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6661 error ("return type is an incomplete type");
6662 /* Make it return void instead. */
6664 = build_function_type (void_type_node,
6665 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6668 if (warn_about_return_type)
6669 pedwarn_c99 ("return type defaults to %<int%>");
6671 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6672 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6673 DECL_INITIAL (decl1) = error_mark_node;
6675 /* If this definition isn't a prototype and we had a prototype declaration
6676 before, copy the arg type info from that prototype. */
6677 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6678 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6680 current_function_prototype_locus = UNKNOWN_LOCATION;
6681 current_function_prototype_built_in = false;
6682 current_function_prototype_arg_types = NULL_TREE;
6683 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6685 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6686 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6687 TREE_TYPE (TREE_TYPE (old_decl))))
6689 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6691 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6692 current_function_prototype_built_in
6693 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6694 current_function_prototype_arg_types
6695 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6697 if (TREE_PUBLIC (decl1))
6699 /* If there is an external prototype declaration of this
6700 function, record its location but do not copy information
6701 to this decl. This may be an invisible declaration
6702 (built-in or in a scope which has finished) or simply
6703 have more refined argument types than any declaration
6705 struct c_binding *b;
6706 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6707 if (B_IN_SCOPE (b, external_scope))
6711 tree ext_decl, ext_type;
6713 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6714 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6715 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6716 TREE_TYPE (ext_type)))
6718 current_function_prototype_locus
6719 = DECL_SOURCE_LOCATION (ext_decl);
6720 current_function_prototype_built_in
6721 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6722 current_function_prototype_arg_types
6723 = TYPE_ARG_TYPES (ext_type);
6729 /* Optionally warn of old-fashioned def with no previous prototype. */
6730 if (warn_strict_prototypes
6731 && old_decl != error_mark_node
6732 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6733 && C_DECL_ISNT_PROTOTYPE (old_decl))
6734 warning (OPT_Wstrict_prototypes,
6735 "function declaration isn%'t a prototype");
6736 /* Optionally warn of any global def with no previous prototype. */
6737 else if (warn_missing_prototypes
6738 && old_decl != error_mark_node
6739 && TREE_PUBLIC (decl1)
6740 && !MAIN_NAME_P (DECL_NAME (decl1))
6741 && C_DECL_ISNT_PROTOTYPE (old_decl))
6742 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6743 /* Optionally warn of any def with no previous prototype
6744 if the function has already been used. */
6745 else if (warn_missing_prototypes
6747 && old_decl != error_mark_node
6748 && TREE_USED (old_decl)
6749 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6750 warning (OPT_Wmissing_prototypes,
6751 "%q+D was used with no prototype before its definition", decl1);
6752 /* Optionally warn of any global def with no previous declaration. */
6753 else if (warn_missing_declarations
6754 && TREE_PUBLIC (decl1)
6756 && !MAIN_NAME_P (DECL_NAME (decl1)))
6757 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6759 /* Optionally warn of any def with no previous declaration
6760 if the function has already been used. */
6761 else if (warn_missing_declarations
6763 && old_decl != error_mark_node
6764 && TREE_USED (old_decl)
6765 && C_DECL_IMPLICIT (old_decl))
6766 warning (OPT_Wmissing_declarations,
6767 "%q+D was used with no declaration before its definition", decl1);
6769 /* This function exists in static storage.
6770 (This does not mean `static' in the C sense!) */
6771 TREE_STATIC (decl1) = 1;
6773 /* A nested function is not global. */
6774 if (current_function_decl != 0)
6775 TREE_PUBLIC (decl1) = 0;
6777 /* This is the earliest point at which we might know the assembler
6778 name of the function. Thus, if it's set before this, die horribly. */
6779 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6781 /* If #pragma weak was used, mark the decl weak now. */
6782 if (current_scope == file_scope)
6783 maybe_apply_pragma_weak (decl1);
6785 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6786 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6791 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6792 != integer_type_node)
6793 pedwarn ("return type of %q+D is not %<int%>", decl1);
6795 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
6796 args = TREE_CHAIN (args))
6798 tree type = args ? TREE_VALUE (args) : 0;
6800 if (type == void_type_node)
6807 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
6808 pedwarn ("first argument of %q+D should be %<int%>", decl1);
6812 if (TREE_CODE (type) != POINTER_TYPE
6813 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6814 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6816 pedwarn ("second argument of %q+D should be %<char **%>",
6821 if (TREE_CODE (type) != POINTER_TYPE
6822 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6823 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6825 pedwarn ("third argument of %q+D should probably be "
6826 "%<char **%>", decl1);
6831 /* It is intentional that this message does not mention the third
6832 argument because it's only mentioned in an appendix of the
6834 if (argct > 0 && (argct < 2 || argct > 3))
6835 pedwarn ("%q+D takes only zero or two arguments", decl1);
6837 if (!TREE_PUBLIC (decl1))
6838 pedwarn ("%q+D is normally a non-static function", decl1);
6841 /* Record the decl so that the function name is defined.
6842 If we already have a decl for this name, and it is a FUNCTION_DECL,
6843 use the old decl. */
6845 current_function_decl = pushdecl (decl1);
6848 declare_parm_level ();
6850 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6851 /* Promote the value to int before returning it. */
6852 if (c_promoting_integer_type_p (restype))
6854 /* It retains unsignedness if not really getting wider. */
6855 if (TYPE_UNSIGNED (restype)
6856 && (TYPE_PRECISION (restype)
6857 == TYPE_PRECISION (integer_type_node)))
6858 restype = unsigned_type_node;
6860 restype = integer_type_node;
6863 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6864 DECL_ARTIFICIAL (resdecl) = 1;
6865 DECL_IGNORED_P (resdecl) = 1;
6866 DECL_RESULT (current_function_decl) = resdecl;
6868 start_fname_decls ();
6873 /* Subroutine of store_parm_decls which handles new-style function
6874 definitions (prototype format). The parms already have decls, so we
6875 need only record them as in effect and complain if any redundant
6876 old-style parm decls were written. */
6878 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6882 if (current_scope->bindings)
6884 error ("%Jold-style parameter declarations in prototyped "
6885 "function definition", fndecl);
6887 /* Get rid of the old-style declarations. */
6891 /* Don't issue this warning for nested functions, and don't issue this
6892 warning if we got here because ARG_INFO_TYPES was error_mark_node
6893 (this happens when a function definition has just an ellipsis in
6894 its parameter list). */
6895 else if (!in_system_header && !current_function_scope
6896 && arg_info->types != error_mark_node)
6897 warning (OPT_Wtraditional,
6898 "%Jtraditional C rejects ISO C style function definitions",
6901 /* Now make all the parameter declarations visible in the function body.
6902 We can bypass most of the grunt work of pushdecl. */
6903 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6905 DECL_CONTEXT (decl) = current_function_decl;
6906 if (DECL_NAME (decl))
6908 bind (DECL_NAME (decl), decl, current_scope,
6909 /*invisible=*/false, /*nested=*/false);
6910 if (!TREE_USED (decl))
6911 warn_if_shadowing (decl);
6914 error ("%Jparameter name omitted", decl);
6917 /* Record the parameter list in the function declaration. */
6918 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6920 /* Now make all the ancillary declarations visible, likewise. */
6921 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6923 DECL_CONTEXT (decl) = current_function_decl;
6924 if (DECL_NAME (decl))
6925 bind (DECL_NAME (decl), decl, current_scope,
6926 /*invisible=*/false, /*nested=*/false);
6929 /* And all the tag declarations. */
6930 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6931 if (TREE_PURPOSE (decl))
6932 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6933 /*invisible=*/false, /*nested=*/false);
6936 /* Subroutine of store_parm_decls which handles old-style function
6937 definitions (separate parameter list and declarations). */
6940 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6942 struct c_binding *b;
6943 tree parm, decl, last;
6944 tree parmids = arg_info->parms;
6945 struct pointer_set_t *seen_args = pointer_set_create ();
6947 if (!in_system_header)
6948 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6951 /* Match each formal parameter name with its declaration. Save each
6952 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6953 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6955 if (TREE_VALUE (parm) == 0)
6957 error ("%Jparameter name missing from parameter list", fndecl);
6958 TREE_PURPOSE (parm) = 0;
6962 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6963 if (b && B_IN_CURRENT_SCOPE (b))
6966 /* If we got something other than a PARM_DECL it is an error. */
6967 if (TREE_CODE (decl) != PARM_DECL)
6968 error ("%q+D declared as a non-parameter", decl);
6969 /* If the declaration is already marked, we have a duplicate
6970 name. Complain and ignore the duplicate. */
6971 else if (pointer_set_contains (seen_args, decl))
6973 error ("multiple parameters named %q+D", decl);
6974 TREE_PURPOSE (parm) = 0;
6977 /* If the declaration says "void", complain and turn it into
6979 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6981 error ("parameter %q+D declared with void type", decl);
6982 TREE_TYPE (decl) = integer_type_node;
6983 DECL_ARG_TYPE (decl) = integer_type_node;
6984 layout_decl (decl, 0);
6986 warn_if_shadowing (decl);
6988 /* If no declaration found, default to int. */
6991 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6992 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6993 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6995 warn_if_shadowing (decl);
6998 pedwarn ("type of %q+D defaults to %<int%>", decl);
6999 else if (extra_warnings)
7000 warning (OPT_Wextra, "type of %q+D defaults to %<int%>", decl);
7003 TREE_PURPOSE (parm) = decl;
7004 pointer_set_insert (seen_args, decl);
7007 /* Now examine the parms chain for incomplete declarations
7008 and declarations with no corresponding names. */
7010 for (b = current_scope->bindings; b; b = b->prev)
7013 if (TREE_CODE (parm) != PARM_DECL)
7016 if (TREE_TYPE (parm) != error_mark_node
7017 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
7019 error ("parameter %q+D has incomplete type", parm);
7020 TREE_TYPE (parm) = error_mark_node;
7023 if (!pointer_set_contains (seen_args, parm))
7025 error ("declaration for parameter %q+D but no such parameter", parm);
7027 /* Pretend the parameter was not missing.
7028 This gets us to a standard state and minimizes
7029 further error messages. */
7030 parmids = chainon (parmids, tree_cons (parm, 0, 0));
7034 /* Chain the declarations together in the order of the list of
7035 names. Store that chain in the function decl, replacing the
7036 list of names. Update the current scope to match. */
7037 DECL_ARGUMENTS (fndecl) = 0;
7039 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
7040 if (TREE_PURPOSE (parm))
7042 if (parm && TREE_PURPOSE (parm))
7044 last = TREE_PURPOSE (parm);
7045 DECL_ARGUMENTS (fndecl) = last;
7047 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
7048 if (TREE_PURPOSE (parm))
7050 TREE_CHAIN (last) = TREE_PURPOSE (parm);
7051 last = TREE_PURPOSE (parm);
7053 TREE_CHAIN (last) = 0;
7056 pointer_set_destroy (seen_args);
7058 /* If there was a previous prototype,
7059 set the DECL_ARG_TYPE of each argument according to
7060 the type previously specified, and report any mismatches. */
7062 if (current_function_prototype_arg_types)
7065 for (parm = DECL_ARGUMENTS (fndecl),
7066 type = current_function_prototype_arg_types;
7067 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
7068 != void_type_node));
7069 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
7071 if (parm == 0 || type == 0
7072 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
7074 if (current_function_prototype_built_in)
7075 warning (0, "number of arguments doesn%'t match "
7076 "built-in prototype");
7079 error ("number of arguments doesn%'t match prototype");
7080 error ("%Hprototype declaration",
7081 ¤t_function_prototype_locus);
7085 /* Type for passing arg must be consistent with that
7086 declared for the arg. ISO C says we take the unqualified
7087 type for parameters declared with qualified type. */
7088 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
7089 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
7091 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
7092 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
7094 /* Adjust argument to match prototype. E.g. a previous
7095 `int foo(float);' prototype causes
7096 `int foo(x) float x; {...}' to be treated like
7097 `int foo(float x) {...}'. This is particularly
7098 useful for argument types like uid_t. */
7099 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
7101 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
7102 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
7103 && TYPE_PRECISION (TREE_TYPE (parm))
7104 < TYPE_PRECISION (integer_type_node))
7105 DECL_ARG_TYPE (parm) = integer_type_node;
7109 /* ??? Is it possible to get here with a
7110 built-in prototype or will it always have
7111 been diagnosed as conflicting with an
7112 old-style definition and discarded? */
7113 if (current_function_prototype_built_in)
7114 warning (0, "promoted argument %qD "
7115 "doesn%'t match built-in prototype", parm);
7118 pedwarn ("promoted argument %qD "
7119 "doesn%'t match prototype", parm);
7120 pedwarn ("%Hprototype declaration",
7121 ¤t_function_prototype_locus);
7127 if (current_function_prototype_built_in)
7128 warning (0, "argument %qD doesn%'t match "
7129 "built-in prototype", parm);
7132 error ("argument %qD doesn%'t match prototype", parm);
7133 error ("%Hprototype declaration",
7134 ¤t_function_prototype_locus);
7139 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
7142 /* Otherwise, create a prototype that would match. */
7146 tree actual = 0, last = 0, type;
7148 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
7150 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
7152 TREE_CHAIN (last) = type;
7157 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
7159 TREE_CHAIN (last) = type;
7163 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
7164 of the type of this function, but we need to avoid having this
7165 affect the types of other similarly-typed functions, so we must
7166 first force the generation of an identical (but separate) type
7167 node for the relevant function type. The new node we create
7168 will be a variant of the main variant of the original function
7171 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
7173 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
7177 /* Store parameter declarations passed in ARG_INFO into the current
7178 function declaration. */
7181 store_parm_decls_from (struct c_arg_info *arg_info)
7183 current_function_arg_info = arg_info;
7184 store_parm_decls ();
7187 /* Store the parameter declarations into the current function declaration.
7188 This is called after parsing the parameter declarations, before
7189 digesting the body of the function.
7191 For an old-style definition, construct a prototype out of the old-style
7192 parameter declarations and inject it into the function's type. */
7195 store_parm_decls (void)
7197 tree fndecl = current_function_decl;
7200 /* The argument information block for FNDECL. */
7201 struct c_arg_info *arg_info = current_function_arg_info;
7202 current_function_arg_info = 0;
7204 /* True if this definition is written with a prototype. Note:
7205 despite C99 6.7.5.3p14, we can *not* treat an empty argument
7206 list in a function definition as equivalent to (void) -- an
7207 empty argument list specifies the function has no parameters,
7208 but only (void) sets up a prototype for future calls. */
7209 proto = arg_info->types != 0;
7212 store_parm_decls_newstyle (fndecl, arg_info);
7214 store_parm_decls_oldstyle (fndecl, arg_info);
7216 /* The next call to push_scope will be a function body. */
7218 next_is_function_body = true;
7220 /* Write a record describing this function definition to the prototypes
7221 file (if requested). */
7223 gen_aux_info_record (fndecl, 1, 0, proto);
7225 /* Initialize the RTL code for the function. */
7226 allocate_struct_function (fndecl);
7228 /* Begin the statement tree for this function. */
7229 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
7231 /* ??? Insert the contents of the pending sizes list into the function
7232 to be evaluated. The only reason left to have this is
7233 void foo(int n, int array[n++])
7234 because we throw away the array type in favor of a pointer type, and
7235 thus won't naturally see the SAVE_EXPR containing the increment. All
7236 other pending sizes would be handled by gimplify_parameters. */
7239 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
7240 add_stmt (TREE_VALUE (t));
7243 /* Even though we're inside a function body, we still don't want to
7244 call expand_expr to calculate the size of a variable-sized array.
7245 We haven't necessarily assigned RTL to all variables yet, so it's
7246 not safe to try to expand expressions involving them. */
7247 cfun->x_dont_save_pending_sizes_p = 1;
7250 /* Emit diagnostics that require gimple input for detection. Operate on
7251 FNDECL and all its nested functions. */
7254 c_gimple_diagnostics_recursively (tree fndecl)
7256 struct cgraph_node *cgn;
7258 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
7259 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
7261 /* Notice when OpenMP structured block constraints are violated. */
7263 diagnose_omp_structured_block_errors (fndecl);
7265 /* Finalize all nested functions now. */
7266 cgn = cgraph_node (fndecl);
7267 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
7268 c_gimple_diagnostics_recursively (cgn->decl);
7271 /* Finish up a function declaration and compile that function
7272 all the way to assembler language output. The free the storage
7273 for the function definition.
7275 This is called after parsing the body of the function definition. */
7278 finish_function (void)
7280 tree fndecl = current_function_decl;
7282 label_context_stack_se = label_context_stack_se->next;
7283 label_context_stack_vm = label_context_stack_vm->next;
7285 if (TREE_CODE (fndecl) == FUNCTION_DECL
7286 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
7288 tree args = DECL_ARGUMENTS (fndecl);
7289 for (; args; args = TREE_CHAIN (args))
7291 tree type = TREE_TYPE (args);
7292 if (INTEGRAL_TYPE_P (type)
7293 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
7294 DECL_ARG_TYPE (args) = integer_type_node;
7298 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
7299 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7301 /* Must mark the RESULT_DECL as being in this function. */
7303 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
7304 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
7306 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
7308 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
7309 != integer_type_node)
7311 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
7312 If warn_main is -1 (-Wno-main) we don't want to be warned. */
7314 pedwarn ("return type of %q+D is not %<int%>", fndecl);
7320 tree stmt = c_finish_return (integer_zero_node);
7321 #ifdef USE_MAPPED_LOCATION
7322 /* Hack. We don't want the middle-end to warn that this return
7323 is unreachable, so we mark its location as special. Using
7324 UNKNOWN_LOCATION has the problem that it gets clobbered in
7325 annotate_one_with_locus. A cleaner solution might be to
7326 ensure ! should_carry_locus_p (stmt), but that needs a flag.
7328 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
7330 /* Hack. We don't want the middle-end to warn that this
7331 return is unreachable, so put the statement on the
7333 annotate_with_file_line (stmt, input_filename, 0);
7339 /* Tie off the statement tree for this function. */
7340 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
7342 finish_fname_decls ();
7344 /* Complain if there's just no return statement. */
7345 if (warn_return_type
7346 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
7347 && !current_function_returns_value && !current_function_returns_null
7348 /* Don't complain if we are no-return. */
7349 && !current_function_returns_abnormally
7350 /* Don't warn for main(). */
7351 && !MAIN_NAME_P (DECL_NAME (fndecl))
7352 /* Or if they didn't actually specify a return type. */
7353 && !C_FUNCTION_IMPLICIT_INT (fndecl)
7354 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
7355 inline function, as we might never be compiled separately. */
7356 && DECL_INLINE (fndecl))
7358 warning (OPT_Wreturn_type,
7359 "no return statement in function returning non-void");
7360 TREE_NO_WARNING (fndecl) = 1;
7363 /* With just -Wextra, complain only if function returns both with
7364 and without a value. */
7366 && current_function_returns_value
7367 && current_function_returns_null)
7368 warning (OPT_Wextra, "this function may return with or without a value");
7370 /* Store the end of the function, so that we get good line number
7371 info for the epilogue. */
7372 cfun->function_end_locus = input_location;
7374 /* If we don't have ctors/dtors sections, and this is a static
7375 constructor or destructor, it must be recorded now. */
7376 if (DECL_STATIC_CONSTRUCTOR (fndecl)
7377 && !targetm.have_ctors_dtors)
7378 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
7379 if (DECL_STATIC_DESTRUCTOR (fndecl)
7380 && !targetm.have_ctors_dtors)
7381 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
7383 /* Finalize the ELF visibility for the function. */
7384 c_determine_visibility (fndecl);
7386 /* Genericize before inlining. Delay genericizing nested functions
7387 until their parent function is genericized. Since finalizing
7388 requires GENERIC, delay that as well. */
7390 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
7391 && !undef_nested_function)
7393 if (!decl_function_context (fndecl))
7395 c_genericize (fndecl);
7396 c_gimple_diagnostics_recursively (fndecl);
7398 /* ??? Objc emits functions after finalizing the compilation unit.
7399 This should be cleaned up later and this conditional removed. */
7400 if (cgraph_global_info_ready)
7402 c_expand_body (fndecl);
7406 cgraph_finalize_function (fndecl, false);
7410 /* Register this function with cgraph just far enough to get it
7411 added to our parent's nested function list. Handy, since the
7412 C front end doesn't have such a list. */
7413 (void) cgraph_node (fndecl);
7417 if (!decl_function_context (fndecl))
7418 undef_nested_function = false;
7420 /* We're leaving the context of this function, so zap cfun.
7421 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
7422 tree_rest_of_compilation. */
7424 current_function_decl = NULL;
7427 /* Generate the RTL for the body of FNDECL. */
7430 c_expand_body (tree fndecl)
7433 if (!DECL_INITIAL (fndecl)
7434 || DECL_INITIAL (fndecl) == error_mark_node)
7437 tree_rest_of_compilation (fndecl);
7439 if (DECL_STATIC_CONSTRUCTOR (fndecl)
7440 && targetm.have_ctors_dtors)
7441 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
7442 DEFAULT_INIT_PRIORITY);
7443 if (DECL_STATIC_DESTRUCTOR (fndecl)
7444 && targetm.have_ctors_dtors)
7445 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
7446 DEFAULT_INIT_PRIORITY);
7449 /* Check the declarations given in a for-loop for satisfying the C99
7450 constraints. If exactly one such decl is found, return it. */
7453 check_for_loop_decls (void)
7455 struct c_binding *b;
7456 tree one_decl = NULL_TREE;
7462 /* If we get here, declarations have been used in a for loop without
7463 the C99 for loop scope. This doesn't make much sense, so don't
7465 error ("%<for%> loop initial declaration used outside C99 mode");
7468 /* C99 subclause 6.8.5 paragraph 3:
7470 [#3] The declaration part of a for statement shall only
7471 declare identifiers for objects having storage class auto or
7474 It isn't clear whether, in this sentence, "identifiers" binds to
7475 "shall only declare" or to "objects" - that is, whether all identifiers
7476 declared must be identifiers for objects, or whether the restriction
7477 only applies to those that are. (A question on this in comp.std.c
7478 in November 2000 received no answer.) We implement the strictest
7479 interpretation, to avoid creating an extension which later causes
7482 for (b = current_scope->bindings; b; b = b->prev)
7485 tree decl = b->decl;
7490 switch (TREE_CODE (decl))
7493 if (TREE_STATIC (decl))
7494 error ("declaration of static variable %q+D in %<for%> loop "
7495 "initial declaration", decl);
7496 else if (DECL_EXTERNAL (decl))
7497 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
7498 "initial declaration", decl);
7502 error ("%<struct %E%> declared in %<for%> loop initial declaration",
7506 error ("%<union %E%> declared in %<for%> loop initial declaration",
7510 error ("%<enum %E%> declared in %<for%> loop initial declaration",
7513 /* APPLE LOCAL begin radar 6268817 */
7515 /* Block helper function can be declared in the statement block
7516 for the for-loop declarations. */
7517 if (BLOCK_SYNTHESIZED_FUNC (decl))
7519 /* APPLE LOCAL end radar 6268817 */
7521 error ("declaration of non-variable %q+D in %<for%> loop "
7522 "initial declaration", decl);
7529 return n_decls == 1 ? one_decl : NULL_TREE;
7532 /* Save and reinitialize the variables
7533 used during compilation of a C function. */
7536 c_push_function_context (struct function *f)
7538 struct language_function *p;
7539 p = GGC_NEW (struct language_function);
7542 p->base.x_stmt_tree = c_stmt_tree;
7543 p->x_break_label = c_break_label;
7544 p->x_cont_label = c_cont_label;
7545 p->x_switch_stack = c_switch_stack;
7546 p->arg_info = current_function_arg_info;
7547 p->returns_value = current_function_returns_value;
7548 p->returns_null = current_function_returns_null;
7549 p->returns_abnormally = current_function_returns_abnormally;
7550 p->warn_about_return_type = warn_about_return_type;
7553 /* Restore the variables used during compilation of a C function. */
7556 c_pop_function_context (struct function *f)
7558 struct language_function *p = f->language;
7560 /* APPLE LOCAL begin blocks 6040305 */
7561 if (current_function_decl
7562 && DECL_STRUCT_FUNCTION (current_function_decl) == 0
7563 /* APPLE LOCAL end blocks 6040305 */
7564 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
7566 /* Stop pointing to the local nodes about to be freed. */
7567 /* But DECL_INITIAL must remain nonzero so we know this
7568 was an actual function definition. */
7569 DECL_INITIAL (current_function_decl) = error_mark_node;
7570 DECL_ARGUMENTS (current_function_decl) = 0;
7573 c_stmt_tree = p->base.x_stmt_tree;
7574 c_break_label = p->x_break_label;
7575 c_cont_label = p->x_cont_label;
7576 c_switch_stack = p->x_switch_stack;
7577 current_function_arg_info = p->arg_info;
7578 current_function_returns_value = p->returns_value;
7579 current_function_returns_null = p->returns_null;
7580 current_function_returns_abnormally = p->returns_abnormally;
7581 warn_about_return_type = p->warn_about_return_type;
7586 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
7589 c_dup_lang_specific_decl (tree decl)
7591 struct lang_decl *ld;
7593 if (!DECL_LANG_SPECIFIC (decl))
7596 ld = GGC_NEW (struct lang_decl);
7597 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
7598 DECL_LANG_SPECIFIC (decl) = ld;
7601 /* The functions below are required for functionality of doing
7602 function at once processing in the C front end. Currently these
7603 functions are not called from anywhere in the C front end, but as
7604 these changes continue, that will change. */
7606 /* Returns the stmt_tree (if any) to which statements are currently
7607 being added. If there is no active statement-tree, NULL is
7611 current_stmt_tree (void)
7613 return &c_stmt_tree;
7616 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
7620 anon_aggr_type_p (tree ARG_UNUSED (node))
7625 /* Return the global value of T as a symbol. */
7628 identifier_global_value (tree t)
7630 struct c_binding *b;
7632 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
7633 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
7639 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
7640 otherwise the name is found in ridpointers from RID_INDEX. */
7643 record_builtin_type (enum rid rid_index, const char *name, tree type)
7647 id = ridpointers[(int) rid_index];
7649 id = get_identifier (name);
7650 decl = build_decl (TYPE_DECL, id, type);
7652 if (debug_hooks->type_decl)
7653 debug_hooks->type_decl (decl, false);
7656 /* Build the void_list_node (void_type_node having been created). */
7658 build_void_list_node (void)
7660 tree t = build_tree_list (NULL_TREE, void_type_node);
7664 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7667 build_c_parm (struct c_declspecs *specs, tree attrs,
7668 struct c_declarator *declarator)
7670 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7673 ret->declarator = declarator;
7677 /* Return a declarator with nested attributes. TARGET is the inner
7678 declarator to which these attributes apply. ATTRS are the
7681 struct c_declarator *
7682 build_attrs_declarator (tree attrs, struct c_declarator *target)
7684 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7685 ret->kind = cdk_attrs;
7686 ret->declarator = target;
7687 ret->u.attrs = attrs;
7691 /* Return a declarator for a function with arguments specified by ARGS
7692 and return type specified by TARGET. */
7694 struct c_declarator *
7695 build_function_declarator (struct c_arg_info *args,
7696 struct c_declarator *target)
7698 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7699 ret->kind = cdk_function;
7700 ret->declarator = target;
7701 ret->u.arg_info = args;
7705 /* Return a declarator for the identifier IDENT (which may be
7706 NULL_TREE for an abstract declarator). */
7708 struct c_declarator *
7709 build_id_declarator (tree ident)
7711 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7713 ret->declarator = 0;
7715 /* Default value - may get reset to a more precise location. */
7716 ret->id_loc = input_location;
7720 /* Return something to represent absolute declarators containing a *.
7721 TARGET is the absolute declarator that the * contains.
7722 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7723 to apply to the pointer type. */
7725 struct c_declarator *
7726 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7727 struct c_declarator *target)
7731 struct c_declarator *itarget = target;
7732 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7733 if (type_quals_attrs)
7735 attrs = type_quals_attrs->attrs;
7736 quals = quals_from_declspecs (type_quals_attrs);
7737 if (attrs != NULL_TREE)
7738 itarget = build_attrs_declarator (attrs, target);
7740 ret->kind = cdk_pointer;
7741 ret->declarator = itarget;
7742 ret->u.pointer_quals = quals;
7746 /* APPLE LOCAL begin radar 5932809 - copyable byref blocks (C++ ch) */
7747 /* build_byref_local_var_access - converts EXPR to:
7748 EXPR.__forwarding-><decl-name>.
7751 build_byref_local_var_access (tree expr, tree decl_name)
7753 tree exp = build_component_ref (expr, get_identifier ("__forwarding"));
7754 exp = build_indirect_ref (exp, "unary *");
7755 exp = build_component_ref (exp, decl_name);
7758 /* APPLE LOCAL end radar 5932809 - copyable byref blocks (C++ ch) */
7759 /* APPLE LOCAL begin radar 5732232 - blocks (C++ ch) */
7761 build_block_byref_decl - This routine inserts a variable declared as a
7762 'byref' variable using the |...| syntax in helper function's outer-most scope.
7765 build_block_byref_decl (tree name, tree decl, tree exp)
7767 struct c_scope *scope = current_scope;
7768 tree ptr_type, byref_decl;
7769 /* APPLE LOCAL begin radar 6225809 */
7770 if (cur_block->prev_block_info) {
7771 /* Traverse enclosing blocks. Insert a __block variable in
7772 each enclosing block which has no declaration of this
7773 variable. This is to ensure that the current (inner) block
7774 gets the __block version of the variable; */
7775 struct block_sema_info *cb = cur_block->prev_block_info;
7777 struct c_binding *b = I_SYMBOL_BINDING (name);
7778 /* Find the first declaration not in current block. */
7780 && (TREE_CODE (b->decl) == VAR_DECL
7781 || TREE_CODE (b->decl) == PARM_DECL)
7782 && b->depth >= cur_block->the_scope->depth)
7785 /* Is the next declaration not in enclosing block? */
7787 && (TREE_CODE (b->decl) == VAR_DECL
7788 || TREE_CODE (b->decl) == PARM_DECL)
7789 && b->depth < cb->the_scope->depth) {
7790 /* No declaration of variable seen in the block. Must insert one. */
7791 struct c_scope *save_scope = current_scope;
7792 struct block_sema_info *save_current_block = cur_block;
7793 current_scope = cb->the_scope;
7795 decl = build_block_byref_decl (name, decl, exp);
7796 cur_block = save_current_block;
7797 current_scope = save_scope;
7799 cb = cb->prev_block_info;
7802 /* APPLE LOCAL end radar 6225809 */
7804 /* If it is already a byref declaration, do not add the pointer type
7805 because such declarations already have the pointer type
7806 added. This happens when we have two nested byref declarations in
7808 ptr_type = (TREE_CODE (decl) == VAR_DECL && BLOCK_DECL_BYREF (decl))
7809 ? TREE_TYPE (decl) : build_pointer_type (TREE_TYPE (decl));
7810 byref_decl = build_decl (VAR_DECL, name, ptr_type);
7811 /* APPLE LOCAL begin radars 6144664 & 6145471 */
7812 DECL_SOURCE_LOCATION (byref_decl) = DECL_SOURCE_LOCATION
7813 (cur_block->helper_func_decl);
7814 /* APPLE LOCAL end radars 6144664 & 6145471 */
7815 BLOCK_DECL_BYREF (byref_decl) = 1;
7817 /* APPLE LOCAL begin radar 5932809 - copyable byref blocks (C++ ch) */
7818 if (TREE_CODE (decl) == VAR_DECL && COPYABLE_BYREF_LOCAL_VAR (decl))
7820 COPYABLE_BYREF_LOCAL_VAR (byref_decl) = 1;
7821 COPYABLE_BYREF_LOCAL_NONPOD (byref_decl) = COPYABLE_BYREF_LOCAL_NONPOD (decl);
7822 /* APPLE LOCAL radar 5847976 */
7823 COPYABLE_WEAK_BLOCK (byref_decl) = COPYABLE_WEAK_BLOCK (decl);
7825 /* APPLE LOCAL end radar 5932809 - copyable byref blocks (C++ ch) */
7827 /* In the presence of nested "{"
7828 move up the scope chain until reaching the main function body's scope.
7830 while (scope && !scope->function_body)
7831 scope = scope->outer;
7832 /* Current scope must be that of the main function body. */
7833 gcc_assert (scope && scope->function_body);
7834 bind (name, byref_decl,
7835 scope, /*invisible=*/false, /*nested=*/false);
7836 cur_block->block_byref_decl_list =
7837 tree_cons (NULL_TREE, byref_decl, cur_block->block_byref_decl_list);
7838 /* APPLE LOCAL radar 5847213 - building block_original_byref_decl_list list removed. */
7839 /* APPLE LOCAL begin radar 6289031 */
7841 if (! flag_objc_gc_only)
7843 push_cleanup (byref_decl, build_block_byref_release_exp (byref_decl), false);
7844 /* APPLE LOCAL end radar 6289031 */
7850 build_block_ref_decl - This routine inserts a copied-in variable (a variable
7851 referenced in the block but whose scope is outside the block) in helper
7852 function's outer-most scope. It also sets its type to 'const' as such
7853 variables are read-only.
7856 build_block_ref_decl (tree name, tree decl)
7858 struct c_scope *scope = current_scope;
7860 /* APPLE LOCAL radar 6212722 */
7862 /* APPLE LOCAL begin radar 5932809 - copyable byref blocks (C++ ch) */
7863 /* 'decl' was previously declared as __block. Simply, copy the value
7864 embedded in the above variable. */
7865 if (TREE_CODE (decl) == VAR_DECL && COPYABLE_BYREF_LOCAL_VAR (decl))
7866 decl = build_byref_local_var_access (decl, DECL_NAME (decl));
7868 /* APPLE LOCAL begin radar 5988451 (C++ ch) */
7869 if (cur_block->prev_block_info) {
7870 /* Traverse enclosing blocks. Insert a copied-in variable in
7871 each enclosing block which has no declaration of this
7872 variable. This is to ensure that the current (inner) block
7873 has the 'frozen' value of the copied-in variable; which means
7874 the value of the copied in variable is at the point of the
7875 block declaration and *not* when the inner block is
7877 struct block_sema_info *cb = cur_block->prev_block_info;
7879 struct c_binding *b = I_SYMBOL_BINDING (name);
7880 /* Find the first declaration not in current block. */
7882 && (TREE_CODE (b->decl) == VAR_DECL
7883 || TREE_CODE (b->decl) == PARM_DECL)
7884 && b->depth >= cur_block->the_scope->depth)
7887 /* Is the next declaration not in enclosing block? */
7889 && (TREE_CODE (b->decl) == VAR_DECL
7890 || TREE_CODE (b->decl) == PARM_DECL)
7891 && b->depth < cb->the_scope->depth) {
7892 /* No declaration of variable seen in the block. Must insert one,
7893 so it 'freezes' the variable in this block. */
7894 struct c_scope *save_scope = current_scope;
7895 struct block_sema_info *save_current_block = cur_block;
7896 current_scope = cb->the_scope;
7898 decl = build_block_ref_decl (name, decl);
7899 cur_block = save_current_block;
7900 current_scope = save_scope;
7902 cb = cb->prev_block_info;
7905 /* APPLE LOCAL end radar 5988451 (C++ ch) */
7907 /* APPLE LOCAL end radar 5932809 - copyable byref blocks (C++ ch) */
7908 /* APPLE LOCAL begin radar 6212722 */
7910 type = TREE_TYPE (exp);
7911 if (TREE_CODE (type) == ARRAY_TYPE) {
7912 exp = array_to_pointer_conversion (decl);
7913 type = TREE_TYPE (exp);
7915 else if (TREE_CODE (type) == FUNCTION_TYPE) {
7916 exp = function_to_pointer_conversion (exp);
7917 type = TREE_TYPE (exp);
7919 ref_decl = build_decl (VAR_DECL, name,
7920 build_qualified_type (type, TYPE_QUAL_CONST));
7921 /* APPLE LOCAL end radar 6212722 */
7922 /* APPLE LOCAL begin radars 6144664 & 6145471 */
7923 DECL_SOURCE_LOCATION (ref_decl) = DECL_SOURCE_LOCATION
7924 (cur_block->helper_func_decl);
7925 /* APPLE LOCAL end radars 6144664 & 6145471 */
7926 DECL_INITIAL (ref_decl) = error_mark_node;
7927 /* APPLE LOCAL radar 5805175 - blocks (C++ ch) */
7928 c_apply_type_quals_to_decl (TYPE_QUAL_CONST, ref_decl);
7929 BLOCK_DECL_COPIED (ref_decl) = 1;
7931 /* Find the scope for function body (outer-most scope) and insert
7932 this variable in that scope. This is to avoid duplicate
7933 declaration of the save variable. */
7934 while (scope && !scope->function_body)
7935 scope = scope->outer;
7936 /* We are enterring the copied-in variable in helper function's
7937 outer scope; that of its main body. */
7939 bind (name, ref_decl,
7940 scope, /*invisible=*/false, /*nested=*/false);
7941 cur_block->block_ref_decl_list =
7942 tree_cons (NULL_TREE, ref_decl, cur_block->block_ref_decl_list);
7943 cur_block->block_original_ref_decl_list =
7944 /* APPLE LOCAL radar 6212722 */
7945 tree_cons (NULL_TREE, exp, cur_block->block_original_ref_decl_list);
7949 /* APPLE LOCAL begin radar 5847213 - radar 6329245 */
7950 static GTY (()) tree descriptor_ptr_type;
7951 static GTY (()) tree descriptor_ptr_type_with_copydispose;
7952 /** build_block_descriptor_type - This routine builds following internal type:
7953 struct __block_descriptor {
7954 unsigned long int reserved; // NULL
7955 unsigned long int Size; // sizeof(struct Block_literal_1)
7957 // optional helper functions
7958 void *CopyFuncPtr; // When BLOCK_HAS_COPY_DISPOSE is set (withCopyDispose true)
7959 void *DestroyFuncPtr; // When BLOCK_HAS_COPY_DISPOSE is set (withCopyDispose true)
7960 } *descriptor_ptr_type;
7962 Objects of this type will always be static. This is one main component of abi change.
7965 build_block_descriptor_type (bool withCopyDispose)
7967 tree field_decl_chain, field_decl;
7970 if (withCopyDispose && descriptor_ptr_type_with_copydispose)
7971 return descriptor_ptr_type_with_copydispose;
7972 if (!withCopyDispose && descriptor_ptr_type)
7973 return descriptor_ptr_type;
7977 start_struct (RECORD_TYPE, get_identifier ("__block_descriptor_withcopydispose"))
7978 : start_struct (RECORD_TYPE, get_identifier ("__block_descriptor"));
7980 /* unsigned long int reserved; */
7981 field_decl = build_decl (FIELD_DECL, get_identifier ("reserved"), long_unsigned_type_node);
7982 field_decl_chain = field_decl;
7984 /* unsigned long int Size; */
7985 field_decl = build_decl (FIELD_DECL, get_identifier ("Size"), long_unsigned_type_node);
7986 chainon (field_decl_chain, field_decl);
7988 if (withCopyDispose)
7990 /* void *CopyFuncPtr; */
7991 field_decl = build_decl (FIELD_DECL, get_identifier ("CopyFuncPtr"), ptr_type_node);
7992 chainon (field_decl_chain, field_decl);
7993 /* void *DestroyFuncPtr; */
7994 field_decl = build_decl (FIELD_DECL, get_identifier ("DestroyFuncPtr"), ptr_type_node);
7995 chainon (field_decl_chain, field_decl);
7998 /* Mark this struct as being a block struct rather than a 'normal'
8000 TYPE_BLOCK_IMPL_STRUCT (main_type) = 1;
8001 finish_struct (main_type, field_decl_chain, NULL_TREE);
8003 main_type = build_pointer_type (main_type);
8004 if (withCopyDispose)
8005 descriptor_ptr_type_with_copydispose = main_type;
8007 descriptor_ptr_type = main_type;
8010 /* APPLE LOCAL end radar 5847213 - radar 6329245 */
8012 /* APPLE LOCAL begin radar 5814025 (C++ ch) */
8013 struct c_declarator *
8014 make_block_pointer_declarator (struct c_declspecs *type_quals_attrs,
8015 struct c_declarator *target)
8018 struct c_declarator *itarget = target;
8019 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8021 if (type_quals_attrs)
8023 tree attrs = type_quals_attrs->attrs;
8024 quals = quals_from_declspecs (type_quals_attrs);
8025 if (attrs != NULL_TREE)
8026 itarget = build_attrs_declarator (attrs, target);
8028 ret->kind = cdk_block_pointer;
8029 /* APPLE LOCAL radar 5882266 (C++ ch) */
8030 ret->declarator = itarget;
8031 ret->u.pointer_quals = quals;
8034 /* APPLE LOCAL end radar 5814025 (C++ ch) */
8039 struct block_sema_info *csi;
8043 csi = (struct block_sema_info*)xcalloc (1, sizeof (struct block_sema_info));
8044 csi->prev_block_info = cur_block;
8049 struct block_sema_info *
8050 finish_block (tree block __attribute__ ((__unused__)))
8052 struct block_sema_info *csi = cur_block;
8053 cur_block = cur_block->prev_block_info;
8063 /* APPLE LOCAL radar 5988451 (C++ ch) */
8064 return (cur_block && cur_block->the_scope == current_scope);
8067 /* This routine returns 'true' if 'name' has a declaration inside the
8068 current block, 'false' otherwise. If 'name' has no declaration in
8069 the current block, it returns in DECL the user declaration for
8070 'name' found in the enclosing scope. Note that if it is declared
8071 in current declaration, it can be either a user declaration or a
8072 byref/copied-in declaration added in current block's scope by the
8075 lookup_name_in_block (tree name, tree *decl)
8079 struct c_binding *b = I_SYMBOL_BINDING (name);
8080 if (b->depth >= cur_block->the_scope->depth)
8083 /* Check for common case of block nested inside a non-block. */
8084 if (!cur_block->prev_block_info)
8086 /* Check for less common case of nested blocks. */
8087 /* Declaration not in current block. Find the first user
8088 declaration of 'name' in outer scope. */
8089 /* APPLE LOCAL begin radar 5988451 (C++ ch) */
8090 /* Check for variables only, as we may have parameters, such as
8092 /* Note that if a copied-in variable (BLOCK_DECL_COPIED) in the
8093 enclosing block is found, it must be returned as this is
8094 where the variable in current (nested block) will have to get
8097 && (TREE_CODE (b->decl) == VAR_DECL)
8098 && BLOCK_DECL_BYREF (b->decl))
8100 /* APPLE LOCAL end radar 5988451 (C++ ch) */
8107 static struct c_scope *save_current_scope;
8108 static tree save_current_function_decl;
8110 push_to_top_level (void)
8112 save_current_scope = current_scope;
8113 save_current_function_decl = current_function_decl;
8114 current_scope = file_scope;
8115 current_function_decl = NULL_TREE;
8119 pop_from_top_level (void)
8121 current_scope = save_current_scope;
8122 current_function_decl = save_current_function_decl;
8126 build_helper_func_decl - This routine builds a FUNCTION_DECL for
8127 a block helper function.
8130 build_helper_func_decl (tree ident, tree type)
8132 tree func_decl = build_decl (FUNCTION_DECL, ident, type);
8133 DECL_EXTERNAL (func_decl) = 0;
8134 TREE_PUBLIC (func_decl) = 0;
8135 TREE_USED (func_decl) = 1;
8136 TREE_NOTHROW (func_decl) = 0;
8137 /* APPLE LOCAL radar 6172148 */
8138 BLOCK_SYNTHESIZED_FUNC (func_decl) = 1;
8143 start_block_helper_function - This is a light-weight version of start_function().
8144 It has removed all the fuss in the start_function().
8147 start_block_helper_function (tree decl1)
8149 struct c_label_context_se *nstack_se;
8150 struct c_label_context_vm *nstack_vm;
8151 tree restype, resdecl;
8153 current_function_returns_value = 0; /* Assume, until we see it does. */
8154 current_function_returns_null = 0;
8155 current_function_returns_abnormally = 0;
8156 warn_about_return_type = 0;
8157 c_switch_stack = NULL;
8159 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
8160 nstack_se->labels_def = NULL;
8161 nstack_se->labels_used = NULL;
8162 nstack_se->next = label_context_stack_se;
8163 label_context_stack_se = nstack_se;
8165 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
8166 nstack_vm->labels_def = NULL;
8167 nstack_vm->labels_used = NULL;
8168 nstack_vm->scope = 0;
8169 nstack_vm->next = label_context_stack_vm;
8170 label_context_stack_vm = nstack_vm;
8172 /* Indicate no valid break/continue context by setting these variables
8173 to some non-null, non-label value. We'll notice and emit the proper
8174 error message in c_finish_bc_stmt. */
8175 c_break_label = c_cont_label = size_zero_node;
8177 announce_function (decl1);
8179 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8180 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8181 DECL_INITIAL (decl1) = error_mark_node;
8183 current_function_prototype_locus = UNKNOWN_LOCATION;
8184 current_function_prototype_built_in = false;
8185 current_function_prototype_arg_types = NULL_TREE;
8187 /* This function exists in static storage.
8188 (This does not mean `static' in the C sense!) */
8189 TREE_STATIC (decl1) = 1;
8190 /* A helper function is not global */
8191 TREE_PUBLIC (decl1) = 0;
8193 /* This is the earliest point at which we might know the assembler
8194 name of the function. Thus, if it's set before this, die horribly. */
8195 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8196 current_function_decl = pushdecl (decl1);
8199 declare_parm_level ();
8201 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8202 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
8203 DECL_ARTIFICIAL (resdecl) = 1;
8204 DECL_IGNORED_P (resdecl) = 1;
8205 DECL_RESULT (current_function_decl) = resdecl;
8207 start_fname_decls ();
8211 declare_block_prologue_local_vars - utility routine to do the actual
8212 declaration and initialization for each referecned block variable.
8215 declare_block_prologue_local_vars (tree self_parm, tree component,
8218 tree decl, block_component;
8219 tree_stmt_iterator i;
8223 block_component = build_component_ref (build_indirect_ref (self_parm, "->"),
8224 DECL_NAME (component));
8225 gcc_assert (block_component);
8226 DECL_EXTERNAL (decl) = 0;
8227 TREE_STATIC (decl) = 0;
8228 TREE_USED (decl) = 1;
8229 DECL_CONTEXT (decl) = current_function_decl;
8230 DECL_ARTIFICIAL (decl) = 1;
8231 DECL_INITIAL (decl) = block_component;
8232 /* Prepend a DECL_EXPR statement to the statement list. */
8233 i = tsi_start (stmt);
8234 decl_stmt = build_stmt (DECL_EXPR, decl);
8235 /* APPLE LOCAL Radar 5811961, Fix location of block prologue vars (C++ ch) */
8236 SET_EXPR_LOCATION (decl_stmt, DECL_SOURCE_LOCATION (decl));
8237 /* APPLE LOCAL begin radar 6163705, Blocks prologues */
8238 /* Give the prologue statements a line number of one before the beginning of
8239 the function, to make them easily identifiable later. */
8240 EXPR_LINENO (decl_stmt) = DECL_SOURCE_LINE (decl) - 1;
8241 /* APPLE LOCAL end radar 6163705, Blocks prologues */
8242 tsi_link_before (&i, decl_stmt, TSI_SAME_STMT);
8246 block_build_prologue
8247 - This routine builds the declarations for the
8248 variables referenced in the block; as in:
8249 int *y = .block_descriptor->y;
8250 int x = .block_descriptor->x;
8252 The decl_expr declaration for each initialization is enterred at the
8253 beginning of the helper function's statement-list which is passed
8254 in block_impl->block_body.
8257 block_build_prologue (struct block_sema_info *block_impl)
8260 /* APPLE LOCAL radar 6404979 */
8261 tree self_parm = lookup_name (get_identifier (".block_descriptor"));
8262 gcc_assert (self_parm);
8264 for (chain = block_impl->block_ref_decl_list; chain;
8265 chain = TREE_CHAIN (chain))
8266 declare_block_prologue_local_vars (self_parm, TREE_VALUE (chain),
8267 block_impl->block_body);
8269 for (chain = block_impl->block_byref_decl_list; chain;
8270 chain = TREE_CHAIN (chain))
8271 declare_block_prologue_local_vars (self_parm, TREE_VALUE (chain),
8272 block_impl->block_body);
8274 /* APPLE LOCAL end radar 5732232 - blocks (C++ ch) */
8276 /* Return a pointer to a structure for an empty list of declaration
8279 struct c_declspecs *
8280 build_null_declspecs (void)
8282 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
8286 ret->typespec_word = cts_none;
8287 ret->storage_class = csc_none;
8288 ret->declspecs_seen_p = false;
8289 ret->type_seen_p = false;
8290 ret->non_sc_seen_p = false;
8291 ret->typedef_p = false;
8292 ret->tag_defined_p = false;
8293 ret->explicit_signed_p = false;
8294 ret->deprecated_p = false;
8295 /* APPLE LOCAL "unavailable" attribute (radar 2809697) */
8296 ret->unavailable_p = false;
8297 ret->default_int_p = false;
8298 ret->long_p = false;
8299 ret->long_long_p = false;
8300 ret->short_p = false;
8301 ret->signed_p = false;
8302 ret->unsigned_p = false;
8303 ret->complex_p = false;
8304 ret->inline_p = false;
8305 ret->thread_p = false;
8306 ret->const_p = false;
8307 ret->volatile_p = false;
8308 ret->restrict_p = false;
8312 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
8315 struct c_declspecs *
8316 declspecs_add_qual (struct c_declspecs *specs, tree qual)
8320 specs->non_sc_seen_p = true;
8321 specs->declspecs_seen_p = true;
8322 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
8323 && C_IS_RESERVED_WORD (qual));
8324 i = C_RID_CODE (qual);
8328 dupe = specs->const_p;
8329 specs->const_p = true;
8332 dupe = specs->volatile_p;
8333 specs->volatile_p = true;
8336 dupe = specs->restrict_p;
8337 specs->restrict_p = true;
8342 if (dupe && pedantic && !flag_isoc99)
8343 pedwarn ("duplicate %qE", qual);
8347 /* Add the type specifier TYPE to the declaration specifiers SPECS,
8350 struct c_declspecs *
8351 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
8353 tree type = spec.spec;
8354 specs->non_sc_seen_p = true;
8355 specs->declspecs_seen_p = true;
8356 specs->type_seen_p = true;
8357 if (TREE_DEPRECATED (type))
8358 specs->deprecated_p = true;
8360 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
8361 if (TREE_UNAVAILABLE (type))
8362 specs->unavailable_p = true;
8363 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
8365 /* Handle type specifier keywords. */
8366 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
8368 enum rid i = C_RID_CODE (type);
8371 error ("two or more data types in declaration specifiers");
8374 if ((int) i <= (int) RID_LAST_MODIFIER)
8376 /* "long", "short", "signed", "unsigned" or "_Complex". */
8381 if (specs->long_long_p)
8383 error ("%<long long long%> is too long for GCC");
8388 if (specs->typespec_word == cts_double)
8390 error ("both %<long long%> and %<double%> in "
8391 "declaration specifiers");
8394 if (pedantic && !flag_isoc99 && !in_system_header
8396 pedwarn ("ISO C90 does not support %<long long%>");
8397 specs->long_long_p = 1;
8401 error ("both %<long%> and %<short%> in "
8402 "declaration specifiers");
8403 else if (specs->typespec_word == cts_void)
8404 error ("both %<long%> and %<void%> in "
8405 "declaration specifiers");
8406 else if (specs->typespec_word == cts_bool)
8407 error ("both %<long%> and %<_Bool%> in "
8408 "declaration specifiers");
8409 else if (specs->typespec_word == cts_char)
8410 error ("both %<long%> and %<char%> in "
8411 "declaration specifiers");
8412 else if (specs->typespec_word == cts_float)
8413 error ("both %<long%> and %<float%> in "
8414 "declaration specifiers");
8415 else if (specs->typespec_word == cts_dfloat32)
8416 error ("both %<long%> and %<_Decimal32%> in "
8417 "declaration specifiers");
8418 else if (specs->typespec_word == cts_dfloat64)
8419 error ("both %<long%> and %<_Decimal64%> in "
8420 "declaration specifiers");
8421 else if (specs->typespec_word == cts_dfloat128)
8422 error ("both %<long%> and %<_Decimal128%> in "
8423 "declaration specifiers");
8425 specs->long_p = true;
8428 dupe = specs->short_p;
8430 error ("both %<long%> and %<short%> in "
8431 "declaration specifiers");
8432 else if (specs->typespec_word == cts_void)
8433 error ("both %<short%> and %<void%> in "
8434 "declaration specifiers");
8435 else if (specs->typespec_word == cts_bool)
8436 error ("both %<short%> and %<_Bool%> in "
8437 "declaration specifiers");
8438 else if (specs->typespec_word == cts_char)
8439 error ("both %<short%> and %<char%> in "
8440 "declaration specifiers");
8441 else if (specs->typespec_word == cts_float)
8442 error ("both %<short%> and %<float%> in "
8443 "declaration specifiers");
8444 else if (specs->typespec_word == cts_double)
8445 error ("both %<short%> and %<double%> in "
8446 "declaration specifiers");
8447 else if (specs->typespec_word == cts_dfloat32)
8448 error ("both %<short%> and %<_Decimal32%> in "
8449 "declaration specifiers");
8450 else if (specs->typespec_word == cts_dfloat64)
8451 error ("both %<short%> and %<_Decimal64%> in "
8452 "declaration specifiers");
8453 else if (specs->typespec_word == cts_dfloat128)
8454 error ("both %<short%> and %<_Decimal128%> in "
8455 "declaration specifiers");
8457 specs->short_p = true;
8460 dupe = specs->signed_p;
8461 if (specs->unsigned_p)
8462 error ("both %<signed%> and %<unsigned%> in "
8463 "declaration specifiers");
8464 else if (specs->typespec_word == cts_void)
8465 error ("both %<signed%> and %<void%> in "
8466 "declaration specifiers");
8467 else if (specs->typespec_word == cts_bool)
8468 error ("both %<signed%> and %<_Bool%> in "
8469 "declaration specifiers");
8470 else if (specs->typespec_word == cts_float)
8471 error ("both %<signed%> and %<float%> in "
8472 "declaration specifiers");
8473 else if (specs->typespec_word == cts_double)
8474 error ("both %<signed%> and %<double%> in "
8475 "declaration specifiers");
8476 else if (specs->typespec_word == cts_dfloat32)
8477 error ("both %<signed%> and %<_Decimal32%> in "
8478 "declaration specifiers");
8479 else if (specs->typespec_word == cts_dfloat64)
8480 error ("both %<signed%> and %<_Decimal64%> in "
8481 "declaration specifiers");
8482 else if (specs->typespec_word == cts_dfloat128)
8483 error ("both %<signed%> and %<_Decimal128%> in "
8484 "declaration specifiers");
8486 specs->signed_p = true;
8489 dupe = specs->unsigned_p;
8490 if (specs->signed_p)
8491 error ("both %<signed%> and %<unsigned%> in "
8492 "declaration specifiers");
8493 else if (specs->typespec_word == cts_void)
8494 error ("both %<unsigned%> and %<void%> in "
8495 "declaration specifiers");
8496 else if (specs->typespec_word == cts_bool)
8497 error ("both %<unsigned%> and %<_Bool%> in "
8498 "declaration specifiers");
8499 else if (specs->typespec_word == cts_float)
8500 error ("both %<unsigned%> and %<float%> in "
8501 "declaration specifiers");
8502 else if (specs->typespec_word == cts_double)
8503 error ("both %<unsigned%> and %<double%> in "
8504 "declaration specifiers");
8505 else if (specs->typespec_word == cts_dfloat32)
8506 error ("both %<unsigned%> and %<_Decimal32%> in "
8507 "declaration specifiers");
8508 else if (specs->typespec_word == cts_dfloat64)
8509 error ("both %<unsigned%> and %<_Decimal64%> in "
8510 "declaration specifiers");
8511 else if (specs->typespec_word == cts_dfloat128)
8512 error ("both %<unsigned%> and %<_Decimal128%> in "
8513 "declaration specifiers");
8515 specs->unsigned_p = true;
8518 dupe = specs->complex_p;
8519 if (pedantic && !flag_isoc99 && !in_system_header)
8520 pedwarn ("ISO C90 does not support complex types");
8521 if (specs->typespec_word == cts_void)
8522 error ("both %<complex%> and %<void%> in "
8523 "declaration specifiers");
8524 else if (specs->typespec_word == cts_bool)
8525 error ("both %<complex%> and %<_Bool%> in "
8526 "declaration specifiers");
8527 else if (specs->typespec_word == cts_dfloat32)
8528 error ("both %<complex%> and %<_Decimal32%> in "
8529 "declaration specifiers");
8530 else if (specs->typespec_word == cts_dfloat64)
8531 error ("both %<complex%> and %<_Decimal64%> in "
8532 "declaration specifiers");
8533 else if (specs->typespec_word == cts_dfloat128)
8534 error ("both %<complex%> and %<_Decimal128%> in "
8535 "declaration specifiers");
8537 specs->complex_p = true;
8544 error ("duplicate %qE", type);
8550 /* "void", "_Bool", "char", "int", "float" or "double". */
8551 if (specs->typespec_word != cts_none)
8553 error ("two or more data types in declaration specifiers");
8560 error ("both %<long%> and %<void%> in "
8561 "declaration specifiers");
8562 else if (specs->short_p)
8563 error ("both %<short%> and %<void%> in "
8564 "declaration specifiers");
8565 else if (specs->signed_p)
8566 error ("both %<signed%> and %<void%> in "
8567 "declaration specifiers");
8568 else if (specs->unsigned_p)
8569 error ("both %<unsigned%> and %<void%> in "
8570 "declaration specifiers");
8571 else if (specs->complex_p)
8572 error ("both %<complex%> and %<void%> in "
8573 "declaration specifiers");
8575 specs->typespec_word = cts_void;
8579 error ("both %<long%> and %<_Bool%> in "
8580 "declaration specifiers");
8581 else if (specs->short_p)
8582 error ("both %<short%> and %<_Bool%> in "
8583 "declaration specifiers");
8584 else if (specs->signed_p)
8585 error ("both %<signed%> and %<_Bool%> in "
8586 "declaration specifiers");
8587 else if (specs->unsigned_p)
8588 error ("both %<unsigned%> and %<_Bool%> in "
8589 "declaration specifiers");
8590 else if (specs->complex_p)
8591 error ("both %<complex%> and %<_Bool%> in "
8592 "declaration specifiers");
8594 specs->typespec_word = cts_bool;
8598 error ("both %<long%> and %<char%> in "
8599 "declaration specifiers");
8600 else if (specs->short_p)
8601 error ("both %<short%> and %<char%> in "
8602 "declaration specifiers");
8604 specs->typespec_word = cts_char;
8607 specs->typespec_word = cts_int;
8611 error ("both %<long%> and %<float%> in "
8612 "declaration specifiers");
8613 else if (specs->short_p)
8614 error ("both %<short%> and %<float%> in "
8615 "declaration specifiers");
8616 else if (specs->signed_p)
8617 error ("both %<signed%> and %<float%> in "
8618 "declaration specifiers");
8619 else if (specs->unsigned_p)
8620 error ("both %<unsigned%> and %<float%> in "
8621 "declaration specifiers");
8623 specs->typespec_word = cts_float;
8626 if (specs->long_long_p)
8627 error ("both %<long long%> and %<double%> in "
8628 "declaration specifiers");
8629 else if (specs->short_p)
8630 error ("both %<short%> and %<double%> in "
8631 "declaration specifiers");
8632 else if (specs->signed_p)
8633 error ("both %<signed%> and %<double%> in "
8634 "declaration specifiers");
8635 else if (specs->unsigned_p)
8636 error ("both %<unsigned%> and %<double%> in "
8637 "declaration specifiers");
8639 specs->typespec_word = cts_double;
8646 if (i == RID_DFLOAT32)
8648 else if (i == RID_DFLOAT64)
8651 str = "_Decimal128";
8652 if (specs->long_long_p)
8653 error ("both %<long long%> and %<%s%> in "
8654 "declaration specifiers", str);
8656 error ("both %<long%> and %<%s%> in "
8657 "declaration specifiers", str);
8658 else if (specs->short_p)
8659 error ("both %<short%> and %<%s%> in "
8660 "declaration specifiers", str);
8661 else if (specs->signed_p)
8662 error ("both %<signed%> and %<%s%> in "
8663 "declaration specifiers", str);
8664 else if (specs->unsigned_p)
8665 error ("both %<unsigned%> and %<%s%> in "
8666 "declaration specifiers", str);
8667 else if (specs->complex_p)
8668 error ("both %<complex%> and %<%s%> in "
8669 "declaration specifiers", str);
8670 else if (i == RID_DFLOAT32)
8671 specs->typespec_word = cts_dfloat32;
8672 else if (i == RID_DFLOAT64)
8673 specs->typespec_word = cts_dfloat64;
8675 specs->typespec_word = cts_dfloat128;
8677 if (!targetm.decimal_float_supported_p ())
8678 error ("decimal floating point not supported for this target");
8680 pedwarn ("ISO C does not support decimal floating point");
8683 /* ObjC reserved word "id", handled below. */
8689 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
8690 form of ObjC type, cases such as "int" and "long" being handled
8691 above), a TYPE (struct, union, enum and typeof specifiers) or an
8692 ERROR_MARK. In none of these cases may there have previously
8693 been any type specifiers. */
8694 if (specs->type || specs->typespec_word != cts_none
8695 || specs->long_p || specs->short_p || specs->signed_p
8696 || specs->unsigned_p || specs->complex_p)
8697 error ("two or more data types in declaration specifiers");
8698 else if (TREE_CODE (type) == TYPE_DECL)
8700 if (TREE_TYPE (type) == error_mark_node)
8701 ; /* Allow the type to default to int to avoid cascading errors. */
8704 specs->type = TREE_TYPE (type);
8705 specs->decl_attr = DECL_ATTRIBUTES (type);
8706 specs->typedef_p = true;
8707 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
8710 else if (TREE_CODE (type) == IDENTIFIER_NODE)
8712 tree t = lookup_name (type);
8713 if (!t || TREE_CODE (t) != TYPE_DECL)
8714 error ("%qE fails to be a typedef or built in type", type);
8715 else if (TREE_TYPE (t) == error_mark_node)
8718 specs->type = TREE_TYPE (t);
8720 else if (TREE_CODE (type) != ERROR_MARK)
8722 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
8723 specs->tag_defined_p = true;
8724 if (spec.kind == ctsk_typeof)
8725 specs->typedef_p = true;
8732 /* Add the storage class specifier or function specifier SCSPEC to the
8733 declaration specifiers SPECS, returning SPECS. */
8735 struct c_declspecs *
8736 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
8739 enum c_storage_class n = csc_none;
8741 specs->declspecs_seen_p = true;
8742 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
8743 && C_IS_RESERVED_WORD (scspec));
8744 i = C_RID_CODE (scspec);
8745 if (extra_warnings && specs->non_sc_seen_p)
8746 warning (OPT_Wextra, "%qE is not at beginning of declaration", scspec);
8750 /* C99 permits duplicate inline. Although of doubtful utility,
8751 it seems simplest to permit it in gnu89 mode as well, as
8752 there is also little utility in maintaining this as a
8753 difference between gnu89 and C99 inline. */
8755 specs->inline_p = true;
8758 dupe = specs->thread_p;
8759 if (specs->storage_class == csc_auto)
8760 error ("%<__thread%> used with %<auto%>");
8761 else if (specs->storage_class == csc_register)
8762 error ("%<__thread%> used with %<register%>");
8763 else if (specs->storage_class == csc_typedef)
8764 error ("%<__thread%> used with %<typedef%>");
8766 specs->thread_p = true;
8773 /* Diagnose "__thread extern". */
8774 if (specs->thread_p)
8775 error ("%<__thread%> before %<extern%>");
8782 /* Diagnose "__thread static". */
8783 if (specs->thread_p)
8784 error ("%<__thread%> before %<static%>");
8792 if (n != csc_none && n == specs->storage_class)
8795 error ("duplicate %qE", scspec);
8798 if (specs->storage_class != csc_none && n != specs->storage_class)
8800 error ("multiple storage classes in declaration specifiers");
8804 specs->storage_class = n;
8805 if (n != csc_extern && n != csc_static && specs->thread_p)
8807 error ("%<__thread%> used with %qE", scspec);
8808 specs->thread_p = false;
8815 /* Add the attributes ATTRS to the declaration specifiers SPECS,
8818 struct c_declspecs *
8819 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
8821 specs->attrs = chainon (attrs, specs->attrs);
8822 specs->declspecs_seen_p = true;
8826 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
8827 specifiers with any other type specifier to determine the resulting
8828 type. This is where ISO C checks on complex types are made, since
8829 "_Complex long" is a prefix of the valid ISO C type "_Complex long
8832 struct c_declspecs *
8833 finish_declspecs (struct c_declspecs *specs)
8835 /* If a type was specified as a whole, we have no modifiers and are
8837 if (specs->type != NULL_TREE)
8839 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
8840 && !specs->signed_p && !specs->unsigned_p
8841 && !specs->complex_p);
8845 /* If none of "void", "_Bool", "char", "int", "float" or "double"
8846 has been specified, treat it as "int" unless "_Complex" is
8847 present and there are no other specifiers. If we just have
8848 "_Complex", it is equivalent to "_Complex double", but e.g.
8849 "_Complex short" is equivalent to "_Complex short int". */
8850 if (specs->typespec_word == cts_none)
8852 if (specs->long_p || specs->short_p
8853 || specs->signed_p || specs->unsigned_p)
8855 specs->typespec_word = cts_int;
8857 else if (specs->complex_p)
8859 specs->typespec_word = cts_double;
8861 pedwarn ("ISO C does not support plain %<complex%> meaning "
8862 "%<double complex%>");
8866 specs->typespec_word = cts_int;
8867 specs->default_int_p = true;
8868 /* We don't diagnose this here because grokdeclarator will
8869 give more specific diagnostics according to whether it is
8870 a function definition. */
8874 /* If "signed" was specified, record this to distinguish "int" and
8875 "signed int" in the case of a bit-field with
8876 -funsigned-bitfields. */
8877 specs->explicit_signed_p = specs->signed_p;
8879 /* Now compute the actual type. */
8880 switch (specs->typespec_word)
8883 gcc_assert (!specs->long_p && !specs->short_p
8884 && !specs->signed_p && !specs->unsigned_p
8885 && !specs->complex_p);
8886 specs->type = void_type_node;
8889 gcc_assert (!specs->long_p && !specs->short_p
8890 && !specs->signed_p && !specs->unsigned_p
8891 && !specs->complex_p);
8892 specs->type = boolean_type_node;
8895 gcc_assert (!specs->long_p && !specs->short_p);
8896 gcc_assert (!(specs->signed_p && specs->unsigned_p));
8897 if (specs->signed_p)
8898 specs->type = signed_char_type_node;
8899 else if (specs->unsigned_p)
8900 specs->type = unsigned_char_type_node;
8902 specs->type = char_type_node;
8903 if (specs->complex_p)
8906 pedwarn ("ISO C does not support complex integer types");
8907 specs->type = build_complex_type (specs->type);
8911 gcc_assert (!(specs->long_p && specs->short_p));
8912 gcc_assert (!(specs->signed_p && specs->unsigned_p));
8913 if (specs->long_long_p)
8914 specs->type = (specs->unsigned_p
8915 ? long_long_unsigned_type_node
8916 : long_long_integer_type_node);
8917 else if (specs->long_p)
8918 specs->type = (specs->unsigned_p
8919 ? long_unsigned_type_node
8920 : long_integer_type_node);
8921 else if (specs->short_p)
8922 specs->type = (specs->unsigned_p
8923 ? short_unsigned_type_node
8924 : short_integer_type_node);
8926 specs->type = (specs->unsigned_p
8927 ? unsigned_type_node
8928 : integer_type_node);
8929 if (specs->complex_p)
8932 pedwarn ("ISO C does not support complex integer types");
8933 specs->type = build_complex_type (specs->type);
8937 gcc_assert (!specs->long_p && !specs->short_p
8938 && !specs->signed_p && !specs->unsigned_p);
8939 specs->type = (specs->complex_p
8940 ? complex_float_type_node
8944 gcc_assert (!specs->long_long_p && !specs->short_p
8945 && !specs->signed_p && !specs->unsigned_p);
8948 specs->type = (specs->complex_p
8949 ? complex_long_double_type_node
8950 : long_double_type_node);
8954 specs->type = (specs->complex_p
8955 ? complex_double_type_node
8956 : double_type_node);
8962 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
8963 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
8964 if (specs->typespec_word == cts_dfloat32)
8965 specs->type = dfloat32_type_node;
8966 else if (specs->typespec_word == cts_dfloat64)
8967 specs->type = dfloat64_type_node;
8969 specs->type = dfloat128_type_node;
8978 /* Synthesize a function which calls all the global ctors or global
8979 dtors in this file. This is only used for targets which do not
8980 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
8982 build_cdtor (int method_type, tree cdtors)
8989 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
8990 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
8993 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
8996 /* A subroutine of c_write_global_declarations. Perform final processing
8997 on one file scope's declarations (or the external scope's declarations),
9001 c_write_global_declarations_1 (tree globals)
9006 /* Process the decls in the order they were written. */
9007 for (decl = globals; decl; decl = TREE_CHAIN (decl))
9009 /* Check for used but undefined static functions using the C
9010 standard's definition of "used", and set TREE_NO_WARNING so
9011 that check_global_declarations doesn't repeat the check. */
9012 if (TREE_CODE (decl) == FUNCTION_DECL
9013 && DECL_INITIAL (decl) == 0
9014 && DECL_EXTERNAL (decl)
9015 && !TREE_PUBLIC (decl)
9016 && C_DECL_USED (decl))
9018 pedwarn ("%q+F used but never defined", decl);
9019 TREE_NO_WARNING (decl) = 1;
9022 wrapup_global_declaration_1 (decl);
9028 for (decl = globals; decl; decl = TREE_CHAIN (decl))
9029 reconsider |= wrapup_global_declaration_2 (decl);
9033 for (decl = globals; decl; decl = TREE_CHAIN (decl))
9034 check_global_declaration_1 (decl);
9037 /* A subroutine of c_write_global_declarations Emit debug information for each
9038 of the declarations in GLOBALS. */
9041 c_write_global_declarations_2 (tree globals)
9045 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
9046 debug_hooks->global_decl (decl);
9049 /* Preserve the external declarations scope across a garbage collect. */
9050 static GTY(()) tree ext_block;
9053 c_write_global_declarations (void)
9057 /* We don't want to do this if generating a PCH. */
9061 /* Don't waste time on further processing if -fsyntax-only or we've
9062 encountered errors. */
9063 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
9066 /* Close the external scope. */
9067 ext_block = pop_scope ();
9069 gcc_assert (!current_scope);
9073 tree tmp = BLOCK_VARS (ext_block);
9075 FILE * stream = dump_begin (TDI_tu, &flags);
9078 dump_node (tmp, flags & ~TDF_SLIM, stream);
9079 dump_end (TDI_tu, stream);
9083 /* Process all file scopes in this compilation, and the external_scope,
9084 through wrapup_global_declarations and check_global_declarations. */
9085 for (t = all_translation_units; t; t = TREE_CHAIN (t))
9086 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
9087 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
9089 /* Generate functions to call static constructors and destructors
9090 for targets that do not support .ctors/.dtors sections. These
9091 functions have magic names which are detected by collect2. */
9092 build_cdtor ('I', static_ctors); static_ctors = 0;
9093 build_cdtor ('D', static_dtors); static_dtors = 0;
9095 /* We're done parsing; proceed to optimize and emit assembly.
9096 FIXME: shouldn't be the front end's responsibility to call this. */
9099 /* After cgraph has had a chance to emit everything that's going to
9100 be emitted, output debug information for globals. */
9101 if (errorcount == 0 && sorrycount == 0)
9103 timevar_push (TV_SYMOUT);
9104 for (t = all_translation_units; t; t = TREE_CHAIN (t))
9105 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
9106 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
9107 timevar_pop (TV_SYMOUT);
9113 /* APPLE LOCAL begin radar 5741070 */
9115 /* Given an IDENTIFIER tree for a class interface, find (if possible) and
9116 return the record type for the class interface. */
9119 c_return_interface_record_type (tree typename)
9121 enum tree_code_class class;
9122 enum tree_code code;
9125 if (typename == NULL)
9128 code = TREE_CODE (typename);
9129 class = TREE_CODE_CLASS (code);
9131 if (code != IDENTIFIER_NODE
9132 || class != tcc_exceptional)
9135 retval = I_TAG_DECL (typename);
9137 if (TREE_CODE (retval) != RECORD_TYPE)
9142 /* APPLE LOCAL end radar 5741070 */
9144 #include "gt-c-decl.h"