1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
31 #include "coretypes.h"
36 #include "tree-inline.h"
53 #include "langhooks.h"
54 #include "tree-mudflap.h"
56 #include "tree-iterator.h"
57 #include "diagnostic.h"
58 #include "tree-dump.h"
63 #include "langhooks-def.h"
64 #include "pointer-set.h"
68 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
70 { NORMAL, /* Ordinary declaration */
71 FUNCDEF, /* Function definition */
72 PARM, /* Declaration of parm before function body */
73 FIELD, /* Declaration inside struct or union */
74 TYPENAME}; /* Typename (inside cast or sizeof) */
76 /* States indicating how grokdeclarator() should handle declspecs marked
77 with __attribute__((deprecated)). An object declared as
78 __attribute__((deprecated)) suppresses warnings of uses of other
81 enum deprecated_states {
87 /* Nonzero if we have seen an invalid cross reference
88 to a struct, union, or enum, but not yet printed the message. */
89 tree pending_invalid_xref;
91 /* File and line to appear in the eventual error message. */
92 location_t pending_invalid_xref_location;
94 /* True means we've initialized exception handling. */
95 bool c_eh_initialized_p;
97 /* The file and line that the prototype came from if this is an
98 old-style definition; used for diagnostics in
99 store_parm_decls_oldstyle. */
101 static location_t current_function_prototype_locus;
103 /* Whether this prototype was built-in. */
105 static bool current_function_prototype_built_in;
107 /* The argument type information of this prototype. */
109 static tree current_function_prototype_arg_types;
111 /* The argument information structure for the function currently being
114 static struct c_arg_info *current_function_arg_info;
116 /* The obstack on which parser and related data structures, which are
117 not live beyond their top-level declaration or definition, are
119 struct obstack parser_obstack;
121 /* The current statement tree. */
123 static GTY(()) struct stmt_tree_s c_stmt_tree;
125 /* State saving variables. */
129 /* True if we are currently parsing the fields of a struct or
132 static bool in_struct;
134 /* A list of types defined in the current struct or union. */
136 static VEC(tree,heap) *struct_types;
138 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
139 included in this invocation. Note that the current translation
140 unit is not included in this list. */
142 static GTY(()) tree all_translation_units;
144 /* A list of decls to be made automatically visible in each file scope. */
145 static GTY(()) tree visible_builtins;
147 /* Set to 0 at beginning of a function definition, set to 1 if
148 a return statement that specifies a return value is seen. */
150 int current_function_returns_value;
152 /* Set to 0 at beginning of a function definition, set to 1 if
153 a return statement with no argument is seen. */
155 int current_function_returns_null;
157 /* Set to 0 at beginning of a function definition, set to 1 if
158 a call to a noreturn function is seen. */
160 int current_function_returns_abnormally;
162 /* Set to nonzero by `grokdeclarator' for a function
163 whose return type is defaulted, if warnings for this are desired. */
165 static int warn_about_return_type;
167 /* Nonzero when the current toplevel function contains a declaration
168 of a nested function which is never defined. */
170 static bool undef_nested_function;
172 /* True means global_bindings_p should return false even if the scope stack
173 says we are in file scope. */
174 bool c_override_global_bindings_to_false;
177 /* Each c_binding structure describes one binding of an identifier to
178 a decl. All the decls in a scope - irrespective of namespace - are
179 chained together by the ->prev field, which (as the name implies)
180 runs in reverse order. All the decls in a given namespace bound to
181 a given identifier are chained by the ->shadowed field, which runs
182 from inner to outer scopes.
184 The ->decl field usually points to a DECL node, but there are two
185 exceptions. In the namespace of type tags, the bound entity is a
186 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
187 identifier is encountered, it is bound to error_mark_node to
188 suppress further errors about that identifier in the current
191 The ->type field stores the type of the declaration in this scope;
192 if NULL, the type is the type of the ->decl field. This is only of
193 relevance for objects with external or internal linkage which may
194 be redeclared in inner scopes, forming composite types that only
195 persist for the duration of those scopes. In the external scope,
196 this stores the composite of all the types declared for this
197 object, visible or not. The ->inner_comp field (used only at file
198 scope) stores whether an incomplete array type at file scope was
199 completed at an inner scope to an array size other than 1.
201 The depth field is copied from the scope structure that holds this
202 decl. It is used to preserve the proper ordering of the ->shadowed
203 field (see bind()) and also for a handful of special-case checks.
204 Finally, the invisible bit is true for a decl which should be
205 ignored for purposes of normal name lookup, and the nested bit is
206 true for a decl that's been bound a second time in an inner scope;
207 in all such cases, the binding in the outer scope will have its
208 invisible bit true. */
210 struct GTY((chain_next ("%h.prev"))) c_binding {
211 tree decl; /* the decl bound */
212 tree type; /* the type in this scope */
213 tree id; /* the identifier it's bound to */
214 struct c_binding *prev; /* the previous decl in this scope */
215 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
216 unsigned int depth : 28; /* depth of this scope */
217 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
218 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
219 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
221 location_t locus; /* location for nested bindings */
223 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
224 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
225 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
226 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
228 #define I_SYMBOL_BINDING(node) \
229 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
230 #define I_SYMBOL_DECL(node) \
231 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
233 #define I_TAG_BINDING(node) \
234 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
235 #define I_TAG_DECL(node) \
236 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
238 #define I_LABEL_BINDING(node) \
239 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
240 #define I_LABEL_DECL(node) \
241 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
243 /* Each C symbol points to three linked lists of c_binding structures.
244 These describe the values of the identifier in the three different
245 namespaces defined by the language. */
247 struct GTY(()) lang_identifier {
248 struct c_common_identifier common_id;
249 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
250 struct c_binding *tag_binding; /* struct/union/enum tags */
251 struct c_binding *label_binding; /* labels */
254 /* Validate c-lang.c's assumptions. */
255 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
256 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
258 /* The resulting tree type. */
260 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
261 chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *) TYPE_NEXT_VARIANT (&%h.generic) : ((union lang_tree_node *) TREE_CHAIN (&%h.generic))"))) lang_tree_node
263 union tree_node GTY ((tag ("0"),
264 desc ("tree_node_structure (&%h)")))
266 struct lang_identifier GTY ((tag ("1"))) identifier;
269 /* Each c_scope structure describes the complete contents of one
270 scope. Four scopes are distinguished specially: the innermost or
271 current scope, the innermost function scope, the file scope (always
272 the second to outermost) and the outermost or external scope.
274 Most declarations are recorded in the current scope.
276 All normal label declarations are recorded in the innermost
277 function scope, as are bindings of undeclared identifiers to
278 error_mark_node. (GCC permits nested functions as an extension,
279 hence the 'innermost' qualifier.) Explicitly declared labels
280 (using the __label__ extension) appear in the current scope.
282 Being in the file scope (current_scope == file_scope) causes
283 special behavior in several places below. Also, under some
284 conditions the Objective-C front end records declarations in the
285 file scope even though that isn't the current scope.
287 All declarations with external linkage are recorded in the external
288 scope, even if they aren't visible there; this models the fact that
289 such declarations are visible to the entire program, and (with a
290 bit of cleverness, see pushdecl) allows diagnosis of some violations
291 of C99 6.2.2p7 and 6.2.7p2:
293 If, within the same translation unit, the same identifier appears
294 with both internal and external linkage, the behavior is
297 All declarations that refer to the same object or function shall
298 have compatible type; otherwise, the behavior is undefined.
300 Initially only the built-in declarations, which describe compiler
301 intrinsic functions plus a subset of the standard library, are in
304 The order of the blocks list matters, and it is frequently appended
305 to. To avoid having to walk all the way to the end of the list on
306 each insertion, or reverse the list later, we maintain a pointer to
307 the last list entry. (FIXME: It should be feasible to use a reversed
310 The bindings list is strictly in reverse order of declarations;
311 pop_scope relies on this. */
314 struct GTY((chain_next ("%h.outer"))) c_scope {
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;
355 /* True means that an unsuffixed float constant is _Decimal64. */
356 BOOL_BITFIELD float_const_decimal64 : 1;
359 /* The scope currently in effect. */
361 static GTY(()) struct c_scope *current_scope;
363 /* The innermost function scope. Ordinary (not explicitly declared)
364 labels, bindings to error_mark_node, and the lazily-created
365 bindings of __func__ and its friends get this scope. */
367 static GTY(()) struct c_scope *current_function_scope;
369 /* The C file scope. This is reset for each input translation unit. */
371 static GTY(()) struct c_scope *file_scope;
373 /* The outermost scope. This is used for all declarations with
374 external linkage, and only these, hence the name. */
376 static GTY(()) struct c_scope *external_scope;
378 /* A chain of c_scope structures awaiting reuse. */
380 static GTY((deletable)) struct c_scope *scope_freelist;
382 /* A chain of c_binding structures awaiting reuse. */
384 static GTY((deletable)) struct c_binding *binding_freelist;
386 /* Append VAR to LIST in scope SCOPE. */
387 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
388 struct c_scope *s_ = (scope); \
390 if (s_->list##_last) \
391 BLOCK_CHAIN (s_->list##_last) = d_; \
394 s_->list##_last = d_; \
397 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
398 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
399 struct c_scope *t_ = (tscope); \
400 struct c_scope *f_ = (fscope); \
402 BLOCK_CHAIN (t_->to##_last) = f_->from; \
405 t_->to##_last = f_->from##_last; \
408 /* A c_inline_static structure stores details of a static identifier
409 referenced in a definition of a function that may be an inline
410 definition if no subsequent declaration of that function uses
411 "extern" or does not use "inline". */
413 struct GTY((chain_next ("%h.next"))) c_inline_static {
414 /* The location for a diagnostic. */
417 /* The function that may be an inline definition. */
420 /* The object or function referenced. */
423 /* What sort of reference this is. */
424 enum c_inline_static_type type;
426 /* The next such structure or NULL. */
427 struct c_inline_static *next;
430 /* List of static identifiers used or referenced in functions that may
431 be inline definitions. */
432 static GTY(()) struct c_inline_static *c_inline_statics;
434 /* True means unconditionally make a BLOCK for the next scope pushed. */
436 static bool keep_next_level_flag;
438 /* True means the next call to push_scope will be the outermost scope
439 of a function body, so do not push a new scope, merely cease
440 expecting parameter decls. */
442 static bool next_is_function_body;
444 /* Forward declarations. */
445 static tree lookup_name_in_scope (tree, struct c_scope *);
446 static tree c_make_fname_decl (tree, int);
447 static tree grokdeclarator (const struct c_declarator *,
448 struct c_declspecs *,
449 enum decl_context, bool, tree *, tree *, tree *,
450 bool *, enum deprecated_states);
451 static tree grokparms (struct c_arg_info *, bool);
452 static void layout_array_type (tree);
454 /* T is a statement. Add it to the statement-tree. This is the
455 C/ObjC version--C++ has a slightly different version of this
461 enum tree_code code = TREE_CODE (t);
463 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
465 if (!EXPR_HAS_LOCATION (t))
466 SET_EXPR_LOCATION (t, input_location);
469 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
470 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
472 /* Add T to the statement-tree. Non-side-effect statements need to be
473 recorded during statement expressions. */
474 append_to_statement_list_force (t, &cur_stmt_list);
481 c_print_identifier (FILE *file, tree node, int indent)
483 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
484 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
485 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
486 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
488 tree rid = ridpointers[C_RID_CODE (node)];
489 indent_to (file, indent + 4);
490 fprintf (file, "rid %p \"%s\"",
491 (void *) rid, IDENTIFIER_POINTER (rid));
495 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
496 which may be any of several kinds of DECL or TYPE or error_mark_node,
497 in the scope SCOPE. */
499 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
500 bool nested, location_t locus)
502 struct c_binding *b, **here;
504 if (binding_freelist)
506 b = binding_freelist;
507 binding_freelist = b->prev;
510 b = GGC_NEW (struct c_binding);
515 b->depth = scope->depth;
516 b->invisible = invisible;
523 b->prev = scope->bindings;
529 switch (TREE_CODE (decl))
531 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
534 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
540 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
546 /* Locate the appropriate place in the chain of shadowed decls
547 to insert this binding. Normally, scope == current_scope and
548 this does nothing. */
549 while (*here && (*here)->depth > scope->depth)
550 here = &(*here)->shadowed;
556 /* Clear the binding structure B, stick it on the binding_freelist,
557 and return the former value of b->prev. This is used by pop_scope
558 and get_parm_info to iterate destructively over all the bindings
559 from a given scope. */
560 static struct c_binding *
561 free_binding_and_advance (struct c_binding *b)
563 struct c_binding *prev = b->prev;
565 memset (b, 0, sizeof (struct c_binding));
566 b->prev = binding_freelist;
567 binding_freelist = b;
573 /* Hook called at end of compilation to assume 1 elt
574 for a file-scope tentative array defn that wasn't complete before. */
577 c_finish_incomplete_decl (tree decl)
579 if (TREE_CODE (decl) == VAR_DECL)
581 tree type = TREE_TYPE (decl);
582 if (type != error_mark_node
583 && TREE_CODE (type) == ARRAY_TYPE
584 && !DECL_EXTERNAL (decl)
585 && TYPE_DOMAIN (type) == 0)
587 warning (0, "array %q+D assumed to have one element", decl);
589 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
591 layout_decl (decl, 0);
596 /* Record that inline function FUNC contains a reference (location
597 LOC) to static DECL (file-scope or function-local according to
601 record_inline_static (location_t loc, tree func, tree decl,
602 enum c_inline_static_type type)
604 struct c_inline_static *csi = GGC_NEW (struct c_inline_static);
606 csi->function = func;
607 csi->static_decl = decl;
609 csi->next = c_inline_statics;
610 c_inline_statics = csi;
613 /* Check for references to static declarations in inline functions at
614 the end of the translation unit and diagnose them if the functions
615 are still inline definitions. */
618 check_inline_statics (void)
620 struct c_inline_static *csi;
621 for (csi = c_inline_statics; csi; csi = csi->next)
623 if (DECL_EXTERNAL (csi->function))
627 pedwarn (csi->location, 0,
628 "%qD is static but used in inline function %qD "
629 "which is not static", csi->static_decl, csi->function);
632 pedwarn (csi->location, 0,
633 "%q+D is static but declared in inline function %qD "
634 "which is not static", csi->static_decl, csi->function);
640 c_inline_statics = NULL;
643 /* The Objective-C front-end often needs to determine the current scope. */
646 objc_get_current_scope (void)
648 return current_scope;
651 /* The following function is used only by Objective-C. It needs to live here
652 because it accesses the innards of c_scope. */
655 objc_mark_locals_volatile (void *enclosing_blk)
657 struct c_scope *scope;
660 for (scope = current_scope;
661 scope && scope != enclosing_blk;
662 scope = scope->outer)
664 for (b = scope->bindings; b; b = b->prev)
665 objc_volatilize_decl (b->decl);
667 /* Do not climb up past the current function. */
668 if (scope->function_body)
673 /* Nonzero if we are currently in file scope. */
676 global_bindings_p (void)
678 return (current_scope == file_scope && !c_override_global_bindings_to_false
684 keep_next_level (void)
686 keep_next_level_flag = true;
689 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
692 set_float_const_decimal64 (void)
694 current_scope->float_const_decimal64 = true;
697 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
700 clear_float_const_decimal64 (void)
702 current_scope->float_const_decimal64 = false;
705 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
708 float_const_decimal64_p (void)
710 return current_scope->float_const_decimal64;
713 /* Identify this scope as currently being filled with parameters. */
716 declare_parm_level (void)
718 current_scope->parm_flag = true;
724 if (next_is_function_body)
726 /* This is the transition from the parameters to the top level
727 of the function body. These are the same scope
728 (C99 6.2.1p4,6) so we do not push another scope structure.
729 next_is_function_body is set only by store_parm_decls, which
730 in turn is called when and only when we are about to
731 encounter the opening curly brace for the function body.
733 The outermost block of a function always gets a BLOCK node,
734 because the debugging output routines expect that each
735 function has at least one BLOCK. */
736 current_scope->parm_flag = false;
737 current_scope->function_body = true;
738 current_scope->keep = true;
739 current_scope->outer_function = current_function_scope;
740 current_function_scope = current_scope;
742 keep_next_level_flag = false;
743 next_is_function_body = false;
745 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
746 if (current_scope->outer)
747 current_scope->float_const_decimal64
748 = current_scope->outer->float_const_decimal64;
750 current_scope->float_const_decimal64 = false;
754 struct c_scope *scope;
757 scope = scope_freelist;
758 scope_freelist = scope->outer;
761 scope = GGC_CNEW (struct c_scope);
763 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
765 scope->float_const_decimal64 = current_scope->float_const_decimal64;
767 scope->float_const_decimal64 = false;
769 scope->keep = keep_next_level_flag;
770 scope->outer = current_scope;
771 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
773 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
775 if (current_scope && scope->depth == 0)
778 sorry ("GCC supports only %u nested scopes", scope->depth);
781 current_scope = scope;
782 keep_next_level_flag = false;
786 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
789 set_type_context (tree type, tree context)
791 for (type = TYPE_MAIN_VARIANT (type); type;
792 type = TYPE_NEXT_VARIANT (type))
793 TYPE_CONTEXT (type) = context;
796 /* Exit a scope. Restore the state of the identifier-decl mappings
797 that were in effect when this scope was entered. Return a BLOCK
798 node containing all the DECLs in this scope that are of interest
799 to debug info generation. */
804 struct c_scope *scope = current_scope;
805 tree block, context, p;
808 bool functionbody = scope->function_body;
809 bool keep = functionbody || scope->keep || scope->bindings;
811 c_end_vm_scope (scope->depth);
813 /* If appropriate, create a BLOCK to record the decls for the life
818 block = make_node (BLOCK);
819 BLOCK_SUBBLOCKS (block) = scope->blocks;
820 TREE_USED (block) = 1;
822 /* In each subblock, record that this is its superior. */
823 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
824 BLOCK_SUPERCONTEXT (p) = block;
826 BLOCK_VARS (block) = 0;
829 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
830 scope must be set so that they point to the appropriate
831 construct, i.e. either to the current FUNCTION_DECL node, or
832 else to the BLOCK node we just constructed.
834 Note that for tagged types whose scope is just the formal
835 parameter list for some function type specification, we can't
836 properly set their TYPE_CONTEXTs here, because we don't have a
837 pointer to the appropriate FUNCTION_TYPE node readily available
838 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
839 type nodes get set in `grokdeclarator' as soon as we have created
840 the FUNCTION_TYPE node which will represent the "scope" for these
841 "parameter list local" tagged types. */
842 if (scope->function_body)
843 context = current_function_decl;
844 else if (scope == file_scope)
846 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
847 TREE_CHAIN (file_decl) = all_translation_units;
848 all_translation_units = file_decl;
854 /* Clear all bindings in this scope. */
855 for (b = scope->bindings; b; b = free_binding_and_advance (b))
858 switch (TREE_CODE (p))
861 /* Warnings for unused labels, errors for undefined labels. */
862 if (TREE_USED (p) && !DECL_INITIAL (p))
864 error ("label %q+D used but not defined", p);
865 DECL_INITIAL (p) = error_mark_node;
868 warn_for_unused_label (p);
870 /* Labels go in BLOCK_VARS. */
871 TREE_CHAIN (p) = BLOCK_VARS (block);
872 BLOCK_VARS (block) = p;
873 gcc_assert (I_LABEL_BINDING (b->id) == b);
874 I_LABEL_BINDING (b->id) = b->shadowed;
880 set_type_context (p, context);
882 /* Types may not have tag-names, in which case the type
883 appears in the bindings list with b->id NULL. */
886 gcc_assert (I_TAG_BINDING (b->id) == b);
887 I_TAG_BINDING (b->id) = b->shadowed;
892 /* Propagate TREE_ADDRESSABLE from nested functions to their
893 containing functions. */
894 if (!TREE_ASM_WRITTEN (p)
895 && DECL_INITIAL (p) != 0
896 && TREE_ADDRESSABLE (p)
897 && DECL_ABSTRACT_ORIGIN (p) != 0
898 && DECL_ABSTRACT_ORIGIN (p) != p)
899 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
900 if (!DECL_EXTERNAL (p)
902 && scope != file_scope
903 && scope != external_scope)
905 error ("nested function %q+D declared but never defined", p);
906 undef_nested_function = true;
908 else if (DECL_DECLARED_INLINE_P (p)
910 && !DECL_INITIAL (p))
912 /* C99 6.7.4p6: "a function with external linkage... declared
913 with an inline function specifier ... shall also be defined
914 in the same translation unit." */
915 if (!flag_gnu89_inline)
916 pedwarn (input_location, 0,
917 "inline function %q+D declared but never defined", p);
918 DECL_EXTERNAL (p) = 1;
924 /* Warnings for unused variables. */
926 && !TREE_NO_WARNING (p)
927 && !DECL_IN_SYSTEM_HEADER (p)
929 && !DECL_ARTIFICIAL (p)
930 && scope != file_scope
931 && scope != external_scope)
932 warning (OPT_Wunused_variable, "unused variable %q+D", p);
936 error ("type of array %q+D completed incompatibly with"
937 " implicit initialization", p);
944 /* All of these go in BLOCK_VARS, but only if this is the
945 binding in the home scope. */
948 TREE_CHAIN (p) = BLOCK_VARS (block);
949 BLOCK_VARS (block) = p;
951 else if (VAR_OR_FUNCTION_DECL_P (p))
953 /* For block local externs add a special
954 DECL_EXTERNAL decl for debug info generation. */
955 tree extp = copy_node (p);
957 DECL_EXTERNAL (extp) = 1;
958 TREE_STATIC (extp) = 0;
959 TREE_PUBLIC (extp) = 1;
960 DECL_INITIAL (extp) = NULL_TREE;
961 DECL_LANG_SPECIFIC (extp) = NULL;
962 DECL_CONTEXT (extp) = current_function_decl;
963 if (TREE_CODE (p) == FUNCTION_DECL)
965 DECL_RESULT (extp) = NULL_TREE;
966 DECL_SAVED_TREE (extp) = NULL_TREE;
967 DECL_STRUCT_FUNCTION (extp) = NULL;
969 if (b->locus != UNKNOWN_LOCATION)
970 DECL_SOURCE_LOCATION (extp) = b->locus;
971 TREE_CHAIN (extp) = BLOCK_VARS (block);
972 BLOCK_VARS (block) = extp;
974 /* If this is the file scope, and we are processing more
975 than one translation unit in this compilation, set
976 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
977 This makes same_translation_unit_p work, and causes
978 static declarations to be given disambiguating suffixes. */
979 if (scope == file_scope && num_in_fnames > 1)
981 DECL_CONTEXT (p) = context;
982 if (TREE_CODE (p) == TYPE_DECL)
983 set_type_context (TREE_TYPE (p), context);
987 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
988 already been put there by store_parm_decls. Unused-
989 parameter warnings are handled by function.c.
990 error_mark_node obviously does not go in BLOCK_VARS and
991 does not get unused-variable warnings. */
994 /* It is possible for a decl not to have a name. We get
995 here with b->id NULL in this case. */
998 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
999 I_SYMBOL_BINDING (b->id) = b->shadowed;
1000 if (b->shadowed && b->shadowed->type)
1001 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
1011 /* Dispose of the block that we just made inside some higher level. */
1012 if ((scope->function_body || scope == file_scope) && context)
1014 DECL_INITIAL (context) = block;
1015 BLOCK_SUPERCONTEXT (block) = context;
1017 else if (scope->outer)
1020 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1021 /* If we did not make a block for the scope just exited, any
1022 blocks made for inner scopes must be carried forward so they
1023 will later become subblocks of something else. */
1024 else if (scope->blocks)
1025 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1028 /* Pop the current scope, and free the structure for reuse. */
1029 current_scope = scope->outer;
1030 if (scope->function_body)
1031 current_function_scope = scope->outer_function;
1033 memset (scope, 0, sizeof (struct c_scope));
1034 scope->outer = scope_freelist;
1035 scope_freelist = scope;
1041 push_file_scope (void)
1049 file_scope = current_scope;
1051 start_fname_decls ();
1053 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
1054 bind (DECL_NAME (decl), decl, file_scope,
1055 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1059 pop_file_scope (void)
1061 /* In case there were missing closebraces, get us back to the global
1063 while (current_scope != file_scope)
1066 /* __FUNCTION__ is defined at file scope (""). This
1067 call may not be necessary as my tests indicate it
1068 still works without it. */
1069 finish_fname_decls ();
1071 check_inline_statics ();
1073 /* This is the point to write out a PCH if we're doing that.
1074 In that case we do not want to do anything else. */
1077 c_common_write_pch ();
1081 /* Pop off the file scope and close this translation unit. */
1085 maybe_apply_pending_pragma_weaks ();
1086 cgraph_finalize_compilation_unit ();
1090 /* Push a definition or a declaration of struct, union or enum tag "name".
1091 "type" should be the type node.
1092 We assume that the tag "name" is not already defined.
1094 Note that the definition may really be just a forward reference.
1095 In that case, the TYPE_SIZE will be zero. */
1098 pushtag (tree name, tree type, location_t loc)
1100 /* Record the identifier as the type's name if it has none. */
1101 if (name && !TYPE_NAME (type))
1102 TYPE_NAME (type) = name;
1103 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1105 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1106 tagged type we just added to the current scope. This fake
1107 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1108 to output a representation of a tagged type, and it also gives
1109 us a convenient place to record the "scope start" address for the
1112 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
1114 /* An approximation for now, so we can tell this is a function-scope tag.
1115 This will be updated in pop_scope. */
1116 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1118 if (warn_cxx_compat && name != NULL_TREE)
1120 struct c_binding *b = I_SYMBOL_BINDING (name);
1123 && b->decl != NULL_TREE
1124 && TREE_CODE (b->decl) == TYPE_DECL
1125 && (B_IN_CURRENT_SCOPE (b)
1126 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1127 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1128 != TYPE_MAIN_VARIANT (type)))
1130 warning_at (loc, OPT_Wc___compat,
1131 ("using %qD as both a typedef and a tag is "
1134 if (b->locus != UNKNOWN_LOCATION)
1135 inform (b->locus, "originally defined here");
1140 /* Subroutine of compare_decls. Allow harmless mismatches in return
1141 and argument types provided that the type modes match. This function
1142 return a unified type given a suitable match, and 0 otherwise. */
1145 match_builtin_function_types (tree newtype, tree oldtype)
1147 tree newrettype, oldrettype;
1148 tree newargs, oldargs;
1149 tree trytype, tryargs;
1151 /* Accept the return type of the new declaration if same modes. */
1152 oldrettype = TREE_TYPE (oldtype);
1153 newrettype = TREE_TYPE (newtype);
1155 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1158 oldargs = TYPE_ARG_TYPES (oldtype);
1159 newargs = TYPE_ARG_TYPES (newtype);
1162 while (oldargs || newargs)
1166 || !TREE_VALUE (oldargs)
1167 || !TREE_VALUE (newargs)
1168 || TYPE_MODE (TREE_VALUE (oldargs))
1169 != TYPE_MODE (TREE_VALUE (newargs)))
1172 oldargs = TREE_CHAIN (oldargs);
1173 newargs = TREE_CHAIN (newargs);
1176 trytype = build_function_type (newrettype, tryargs);
1177 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1180 /* Subroutine of diagnose_mismatched_decls. Check for function type
1181 mismatch involving an empty arglist vs a nonempty one and give clearer
1184 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1185 tree newtype, tree oldtype)
1189 if (TREE_CODE (olddecl) != FUNCTION_DECL
1190 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1191 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1193 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1196 t = TYPE_ARG_TYPES (oldtype);
1198 t = TYPE_ARG_TYPES (newtype);
1199 for (; t; t = TREE_CHAIN (t))
1201 tree type = TREE_VALUE (t);
1203 if (TREE_CHAIN (t) == 0
1204 && TYPE_MAIN_VARIANT (type) != void_type_node)
1206 inform (input_location, "a parameter list with an ellipsis can%'t match "
1207 "an empty parameter name list declaration");
1211 if (c_type_promotes_to (type) != type)
1213 inform (input_location, "an argument type that has a default promotion can%'t match "
1214 "an empty parameter name list declaration");
1220 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1221 old-style function definition, NEWDECL is a prototype declaration.
1222 Diagnose inconsistencies in the argument list. Returns TRUE if
1223 the prototype is compatible, FALSE if not. */
1225 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1227 tree newargs, oldargs;
1230 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1232 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1233 newargs = TYPE_ARG_TYPES (newtype);
1238 tree oldargtype = TREE_VALUE (oldargs);
1239 tree newargtype = TREE_VALUE (newargs);
1241 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1244 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1245 newargtype = TYPE_MAIN_VARIANT (newargtype);
1247 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1250 /* Reaching the end of just one list means the two decls don't
1251 agree on the number of arguments. */
1252 if (END_OF_ARGLIST (oldargtype))
1254 error ("prototype for %q+D declares more arguments "
1255 "than previous old-style definition", newdecl);
1258 else if (END_OF_ARGLIST (newargtype))
1260 error ("prototype for %q+D declares fewer arguments "
1261 "than previous old-style definition", newdecl);
1265 /* Type for passing arg must be consistent with that declared
1267 else if (!comptypes (oldargtype, newargtype))
1269 error ("prototype for %q+D declares argument %d"
1270 " with incompatible type",
1275 oldargs = TREE_CHAIN (oldargs);
1276 newargs = TREE_CHAIN (newargs);
1280 /* If we get here, no errors were found, but do issue a warning
1281 for this poor-style construct. */
1282 warning (0, "prototype for %q+D follows non-prototype definition",
1285 #undef END_OF_ARGLIST
1288 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1289 first in a pair of mismatched declarations, using the diagnostic
1292 locate_old_decl (tree decl)
1294 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1296 else if (DECL_INITIAL (decl))
1297 inform (input_location, "previous definition of %q+D was here", decl);
1298 else if (C_DECL_IMPLICIT (decl))
1299 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1301 inform (input_location, "previous declaration of %q+D was here", decl);
1304 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1305 Returns true if the caller should proceed to merge the two, false
1306 if OLDDECL should simply be discarded. As a side effect, issues
1307 all necessary diagnostics for invalid or poor-style combinations.
1308 If it returns true, writes the types of NEWDECL and OLDDECL to
1309 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1310 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1313 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1314 tree *newtypep, tree *oldtypep)
1316 tree newtype, oldtype;
1317 bool pedwarned = false;
1318 bool warned = false;
1321 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1322 && DECL_EXTERNAL (DECL))
1324 /* If we have error_mark_node for either decl or type, just discard
1325 the previous decl - we're in an error cascade already. */
1326 if (olddecl == error_mark_node || newdecl == error_mark_node)
1328 *oldtypep = oldtype = TREE_TYPE (olddecl);
1329 *newtypep = newtype = TREE_TYPE (newdecl);
1330 if (oldtype == error_mark_node || newtype == error_mark_node)
1333 /* Two different categories of symbol altogether. This is an error
1334 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1335 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1337 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1338 && DECL_BUILT_IN (olddecl)
1339 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1341 error ("%q+D redeclared as different kind of symbol", newdecl);
1342 locate_old_decl (olddecl);
1344 else if (TREE_PUBLIC (newdecl))
1345 warning (0, "built-in function %q+D declared as non-function",
1348 warning (OPT_Wshadow, "declaration of %q+D shadows "
1349 "a built-in function", newdecl);
1353 /* Enumerators have no linkage, so may only be declared once in a
1355 if (TREE_CODE (olddecl) == CONST_DECL)
1357 error ("redeclaration of enumerator %q+D", newdecl);
1358 locate_old_decl (olddecl);
1362 if (!comptypes (oldtype, newtype))
1364 if (TREE_CODE (olddecl) == FUNCTION_DECL
1365 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1367 /* Accept harmless mismatch in function types.
1368 This is for the ffs and fprintf builtins. */
1369 tree trytype = match_builtin_function_types (newtype, oldtype);
1371 if (trytype && comptypes (newtype, trytype))
1372 *oldtypep = oldtype = trytype;
1375 /* If types don't match for a built-in, throw away the
1376 built-in. No point in calling locate_old_decl here, it
1377 won't print anything. */
1378 warning (0, "conflicting types for built-in function %q+D",
1383 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1384 && DECL_IS_BUILTIN (olddecl))
1386 /* A conflicting function declaration for a predeclared
1387 function that isn't actually built in. Objective C uses
1388 these. The new declaration silently overrides everything
1389 but the volatility (i.e. noreturn) indication. See also
1390 below. FIXME: Make Objective C use normal builtins. */
1391 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1394 /* Permit void foo (...) to match int foo (...) if the latter is
1395 the definition and implicit int was used. See
1396 c-torture/compile/920625-2.c. */
1397 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1398 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1399 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1400 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1402 pedwarned = pedwarn (input_location, 0,
1403 "conflicting types for %q+D", newdecl);
1404 /* Make sure we keep void as the return type. */
1405 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1406 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1408 /* Permit void foo (...) to match an earlier call to foo (...) with
1409 no declared type (thus, implicitly int). */
1410 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1411 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1412 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1413 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1415 pedwarned = pedwarn (input_location, 0,
1416 "conflicting types for %q+D", newdecl);
1417 /* Make sure we keep void as the return type. */
1418 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1422 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1423 error ("conflicting type qualifiers for %q+D", newdecl);
1425 error ("conflicting types for %q+D", newdecl);
1426 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1427 locate_old_decl (olddecl);
1432 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1433 but silently ignore the redeclaration if either is in a system
1434 header. (Conflicting redeclarations were handled above.) */
1435 if (TREE_CODE (newdecl) == TYPE_DECL)
1437 if (DECL_IN_SYSTEM_HEADER (newdecl)
1438 || DECL_IN_SYSTEM_HEADER (olddecl)
1439 || TREE_NO_WARNING (newdecl)
1440 || TREE_NO_WARNING (olddecl))
1441 return true; /* Allow OLDDECL to continue in use. */
1443 error ("redefinition of typedef %q+D", newdecl);
1444 locate_old_decl (olddecl);
1448 /* Function declarations can either be 'static' or 'extern' (no
1449 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1450 can never conflict with each other on account of linkage
1451 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1452 gnu89 mode permits two definitions if one is 'extern inline' and
1453 one is not. The non- extern-inline definition supersedes the
1454 extern-inline definition. */
1456 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1458 /* If you declare a built-in function name as static, or
1459 define the built-in with an old-style definition (so we
1460 can't validate the argument list) the built-in definition is
1461 overridden, but optionally warn this was a bad choice of name. */
1462 if (DECL_BUILT_IN (olddecl)
1463 && !C_DECL_DECLARED_BUILTIN (olddecl)
1464 && (!TREE_PUBLIC (newdecl)
1465 || (DECL_INITIAL (newdecl)
1466 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1468 warning (OPT_Wshadow, "declaration of %q+D shadows "
1469 "a built-in function", newdecl);
1470 /* Discard the old built-in function. */
1474 if (DECL_INITIAL (newdecl))
1476 if (DECL_INITIAL (olddecl))
1478 /* If both decls are in the same TU and the new declaration
1479 isn't overriding an extern inline reject the new decl.
1480 In c99, no overriding is allowed in the same translation
1482 if ((!DECL_EXTERN_INLINE (olddecl)
1483 || DECL_EXTERN_INLINE (newdecl)
1484 || (!flag_gnu89_inline
1485 && (!DECL_DECLARED_INLINE_P (olddecl)
1486 || !lookup_attribute ("gnu_inline",
1487 DECL_ATTRIBUTES (olddecl)))
1488 && (!DECL_DECLARED_INLINE_P (newdecl)
1489 || !lookup_attribute ("gnu_inline",
1490 DECL_ATTRIBUTES (newdecl))))
1492 && same_translation_unit_p (newdecl, olddecl))
1494 error ("redefinition of %q+D", newdecl);
1495 locate_old_decl (olddecl);
1500 /* If we have a prototype after an old-style function definition,
1501 the argument types must be checked specially. */
1502 else if (DECL_INITIAL (olddecl)
1503 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1504 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1505 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1507 locate_old_decl (olddecl);
1510 /* A non-static declaration (even an "extern") followed by a
1511 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1512 The same is true for a static forward declaration at block
1513 scope followed by a non-static declaration/definition at file
1514 scope. Static followed by non-static at the same scope is
1515 not undefined behavior, and is the most convenient way to get
1516 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1517 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1518 we do diagnose it if -Wtraditional. */
1519 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1521 /* Two exceptions to the rule. If olddecl is an extern
1522 inline, or a predeclared function that isn't actually
1523 built in, newdecl silently overrides olddecl. The latter
1524 occur only in Objective C; see also above. (FIXME: Make
1525 Objective C use normal builtins.) */
1526 if (!DECL_IS_BUILTIN (olddecl)
1527 && !DECL_EXTERN_INLINE (olddecl))
1529 error ("static declaration of %q+D follows "
1530 "non-static declaration", newdecl);
1531 locate_old_decl (olddecl);
1535 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1537 if (DECL_CONTEXT (olddecl))
1539 error ("non-static declaration of %q+D follows "
1540 "static declaration", newdecl);
1541 locate_old_decl (olddecl);
1544 else if (warn_traditional)
1546 warned |= warning (OPT_Wtraditional,
1547 "non-static declaration of %q+D "
1548 "follows static declaration", newdecl);
1552 /* Make sure gnu_inline attribute is either not present, or
1553 present on all inline decls. */
1554 if (DECL_DECLARED_INLINE_P (olddecl)
1555 && DECL_DECLARED_INLINE_P (newdecl))
1557 bool newa = lookup_attribute ("gnu_inline",
1558 DECL_ATTRIBUTES (newdecl)) != NULL;
1559 bool olda = lookup_attribute ("gnu_inline",
1560 DECL_ATTRIBUTES (olddecl)) != NULL;
1563 error ("%<gnu_inline%> attribute present on %q+D",
1564 newa ? newdecl : olddecl);
1565 error ("%Jbut not here", newa ? olddecl : newdecl);
1569 else if (TREE_CODE (newdecl) == VAR_DECL)
1571 /* Only variables can be thread-local, and all declarations must
1572 agree on this property. */
1573 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1575 /* Nothing to check. Since OLDDECL is marked threadprivate
1576 and NEWDECL does not have a thread-local attribute, we
1577 will merge the threadprivate attribute into NEWDECL. */
1580 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1582 if (DECL_THREAD_LOCAL_P (newdecl))
1583 error ("thread-local declaration of %q+D follows "
1584 "non-thread-local declaration", newdecl);
1586 error ("non-thread-local declaration of %q+D follows "
1587 "thread-local declaration", newdecl);
1589 locate_old_decl (olddecl);
1593 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1594 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1596 error ("redefinition of %q+D", newdecl);
1597 locate_old_decl (olddecl);
1601 /* Objects declared at file scope: if the first declaration had
1602 external linkage (even if it was an external reference) the
1603 second must have external linkage as well, or the behavior is
1604 undefined. If the first declaration had internal linkage, then
1605 the second must too, or else be an external reference (in which
1606 case the composite declaration still has internal linkage).
1607 As for function declarations, we warn about the static-then-
1608 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1609 if (DECL_FILE_SCOPE_P (newdecl)
1610 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1612 if (DECL_EXTERNAL (newdecl))
1614 if (!DECL_FILE_SCOPE_P (olddecl))
1616 error ("extern declaration of %q+D follows "
1617 "declaration with no linkage", newdecl);
1618 locate_old_decl (olddecl);
1621 else if (warn_traditional)
1623 warned |= warning (OPT_Wtraditional,
1624 "non-static declaration of %q+D "
1625 "follows static declaration", newdecl);
1630 if (TREE_PUBLIC (newdecl))
1631 error ("non-static declaration of %q+D follows "
1632 "static declaration", newdecl);
1634 error ("static declaration of %q+D follows "
1635 "non-static declaration", newdecl);
1637 locate_old_decl (olddecl);
1641 /* Two objects with the same name declared at the same block
1642 scope must both be external references (6.7p3). */
1643 else if (!DECL_FILE_SCOPE_P (newdecl))
1645 if (DECL_EXTERNAL (newdecl))
1647 /* Extern with initializer at block scope, which will
1648 already have received an error. */
1650 else if (DECL_EXTERNAL (olddecl))
1652 error ("declaration of %q+D with no linkage follows "
1653 "extern declaration", newdecl);
1654 locate_old_decl (olddecl);
1658 error ("redeclaration of %q+D with no linkage", newdecl);
1659 locate_old_decl (olddecl);
1667 /* All decls must agree on a visibility. */
1668 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1669 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1670 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1672 warned |= warning (0, "redeclaration of %q+D with different visibility "
1673 "(old visibility preserved)", newdecl);
1676 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1678 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1679 if (DECL_DECLARED_INLINE_P (newdecl)
1680 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1682 warned |= warning (OPT_Wattributes,
1683 "inline declaration of %qD follows "
1684 "declaration with attribute noinline", newdecl);
1686 else if (DECL_DECLARED_INLINE_P (olddecl)
1687 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1689 warned |= warning (OPT_Wattributes,
1690 "declaration of %q+D with attribute "
1691 "noinline follows inline declaration ", newdecl);
1694 else /* PARM_DECL, VAR_DECL */
1696 /* Redeclaration of a parameter is a constraint violation (this is
1697 not explicitly stated, but follows from C99 6.7p3 [no more than
1698 one declaration of the same identifier with no linkage in the
1699 same scope, except type tags] and 6.2.2p6 [parameters have no
1700 linkage]). We must check for a forward parameter declaration,
1701 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1702 an extension, the mandatory diagnostic for which is handled by
1703 mark_forward_parm_decls. */
1705 if (TREE_CODE (newdecl) == PARM_DECL
1706 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1708 error ("redefinition of parameter %q+D", newdecl);
1709 locate_old_decl (olddecl);
1714 /* Optional warning for completely redundant decls. */
1715 if (!warned && !pedwarned
1716 && warn_redundant_decls
1717 /* Don't warn about a function declaration followed by a
1719 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1720 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1721 /* Don't warn about redundant redeclarations of builtins. */
1722 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1723 && !DECL_BUILT_IN (newdecl)
1724 && DECL_BUILT_IN (olddecl)
1725 && !C_DECL_DECLARED_BUILTIN (olddecl))
1726 /* Don't warn about an extern followed by a definition. */
1727 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1728 /* Don't warn about forward parameter decls. */
1729 && !(TREE_CODE (newdecl) == PARM_DECL
1730 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1731 /* Don't warn about a variable definition following a declaration. */
1732 && !(TREE_CODE (newdecl) == VAR_DECL
1733 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1735 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1739 /* Report location of previous decl/defn. */
1740 if (warned || pedwarned)
1741 locate_old_decl (olddecl);
1743 #undef DECL_EXTERN_INLINE
1748 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1749 consistent with OLDDECL, but carries new information. Merge the
1750 new information into OLDDECL. This function issues no
1754 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1756 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1757 && DECL_INITIAL (newdecl) != 0);
1758 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1759 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1760 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1761 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1762 bool extern_changed = false;
1764 /* For real parm decl following a forward decl, rechain the old decl
1765 in its new location and clear TREE_ASM_WRITTEN (it's not a
1766 forward decl anymore). */
1767 if (TREE_CODE (newdecl) == PARM_DECL
1768 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1770 struct c_binding *b, **here;
1772 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
1773 if ((*here)->decl == olddecl)
1780 b->prev = current_scope->bindings;
1781 current_scope->bindings = b;
1783 TREE_ASM_WRITTEN (olddecl) = 0;
1786 DECL_ATTRIBUTES (newdecl)
1787 = targetm.merge_decl_attributes (olddecl, newdecl);
1789 /* Merge the data types specified in the two decls. */
1791 = TREE_TYPE (olddecl)
1792 = composite_type (newtype, oldtype);
1794 /* Lay the type out, unless already done. */
1795 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1797 if (TREE_TYPE (newdecl) != error_mark_node)
1798 layout_type (TREE_TYPE (newdecl));
1799 if (TREE_CODE (newdecl) != FUNCTION_DECL
1800 && TREE_CODE (newdecl) != TYPE_DECL
1801 && TREE_CODE (newdecl) != CONST_DECL)
1802 layout_decl (newdecl, 0);
1806 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1807 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1808 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1809 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1810 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1812 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1813 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
1817 /* Keep the old rtl since we can safely use it. */
1818 if (HAS_RTL_P (olddecl))
1819 COPY_DECL_RTL (olddecl, newdecl);
1821 /* Merge the type qualifiers. */
1822 if (TREE_READONLY (newdecl))
1823 TREE_READONLY (olddecl) = 1;
1825 if (TREE_THIS_VOLATILE (newdecl))
1826 TREE_THIS_VOLATILE (olddecl) = 1;
1828 /* Merge deprecatedness. */
1829 if (TREE_DEPRECATED (newdecl))
1830 TREE_DEPRECATED (olddecl) = 1;
1832 /* If a decl is in a system header and the other isn't, keep the one on the
1833 system header. Otherwise, keep source location of definition rather than
1834 declaration and of prototype rather than non-prototype unless that
1835 prototype is built-in. */
1836 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
1837 && DECL_IN_SYSTEM_HEADER (olddecl)
1838 && !DECL_IN_SYSTEM_HEADER (newdecl) )
1839 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1840 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
1841 && DECL_IN_SYSTEM_HEADER (newdecl)
1842 && !DECL_IN_SYSTEM_HEADER (olddecl))
1843 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
1844 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1845 || (old_is_prototype && !new_is_prototype
1846 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1847 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1849 /* Merge the initialization information. */
1850 if (DECL_INITIAL (newdecl) == 0)
1851 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1853 /* Merge the threadprivate attribute. */
1854 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1856 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1857 C_DECL_THREADPRIVATE_P (newdecl) = 1;
1860 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1862 /* Merge the section attribute.
1863 We want to issue an error if the sections conflict but that
1864 must be done later in decl_attributes since we are called
1865 before attributes are assigned. */
1866 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1867 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1869 /* Copy the assembler name.
1870 Currently, it can only be defined in the prototype. */
1871 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1873 /* Use visibility of whichever declaration had it specified */
1874 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1876 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1877 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1880 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1882 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1883 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1884 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1885 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1886 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1887 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1888 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1889 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
1890 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1891 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1892 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1895 /* Merge the storage class information. */
1896 merge_weak (newdecl, olddecl);
1898 /* For functions, static overrides non-static. */
1899 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1901 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1902 /* This is since we don't automatically
1903 copy the attributes of NEWDECL into OLDDECL. */
1904 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1905 /* If this clears `static', clear it in the identifier too. */
1906 if (!TREE_PUBLIC (olddecl))
1907 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1911 /* In c99, 'extern' declaration before (or after) 'inline' means this
1912 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1914 if (TREE_CODE (newdecl) == FUNCTION_DECL
1915 && !flag_gnu89_inline
1916 && (DECL_DECLARED_INLINE_P (newdecl)
1917 || DECL_DECLARED_INLINE_P (olddecl))
1918 && (!DECL_DECLARED_INLINE_P (newdecl)
1919 || !DECL_DECLARED_INLINE_P (olddecl)
1920 || !DECL_EXTERNAL (olddecl))
1921 && DECL_EXTERNAL (newdecl)
1922 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
1923 && !current_function_decl)
1924 DECL_EXTERNAL (newdecl) = 0;
1926 if (DECL_EXTERNAL (newdecl))
1928 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1929 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1931 /* An extern decl does not override previous storage class. */
1932 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1933 if (!DECL_EXTERNAL (newdecl))
1935 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1936 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1941 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1942 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1945 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1947 /* If we're redefining a function previously defined as extern
1948 inline, make sure we emit debug info for the inline before we
1949 throw it away, in case it was inlined into a function that
1950 hasn't been written out yet. */
1951 if (new_is_definition && DECL_INITIAL (olddecl))
1952 /* The new defn must not be inline. */
1953 DECL_UNINLINABLE (newdecl) = 1;
1956 /* If either decl says `inline', this fn is inline, unless
1957 its definition was passed already. */
1958 if (DECL_DECLARED_INLINE_P (newdecl)
1959 || DECL_DECLARED_INLINE_P (olddecl))
1960 DECL_DECLARED_INLINE_P (newdecl) = 1;
1962 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1963 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1965 DECL_DISREGARD_INLINE_LIMITS (newdecl)
1966 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
1967 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
1968 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
1971 if (DECL_BUILT_IN (olddecl))
1973 /* If redeclaring a builtin function, it stays built in.
1974 But it gets tagged as having been declared. */
1975 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1976 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1977 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1978 if (new_is_prototype)
1979 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1981 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1982 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1985 /* Preserve function specific target and optimization options */
1986 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
1987 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
1988 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
1989 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
1991 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
1992 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
1993 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
1994 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
1996 /* Also preserve various other info from the definition. */
1997 if (!new_is_definition)
2000 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2001 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2002 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2003 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2004 gimple_set_body (newdecl, gimple_body (olddecl));
2005 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2006 for (t = DECL_ARGUMENTS (newdecl); t ; t = TREE_CHAIN (t))
2007 DECL_CONTEXT (t) = newdecl;
2009 /* See if we've got a function to instantiate from. */
2010 if (DECL_SAVED_TREE (olddecl))
2011 DECL_ABSTRACT_ORIGIN (newdecl)
2012 = DECL_ABSTRACT_ORIGIN (olddecl);
2016 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
2018 /* Merge the USED information. */
2019 if (TREE_USED (olddecl))
2020 TREE_USED (newdecl) = 1;
2021 else if (TREE_USED (newdecl))
2022 TREE_USED (olddecl) = 1;
2024 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2025 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
2027 unsigned olddecl_uid = DECL_UID (olddecl);
2028 tree olddecl_context = DECL_CONTEXT (olddecl);
2029 tree olddecl_arguments = NULL;
2030 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2031 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2033 memcpy ((char *) olddecl + sizeof (struct tree_common),
2034 (char *) newdecl + sizeof (struct tree_common),
2035 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2036 switch (TREE_CODE (olddecl))
2039 gimple_set_body (olddecl, gimple_body (newdecl));
2049 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2050 (char *) newdecl + sizeof (struct tree_decl_common),
2051 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2056 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2057 (char *) newdecl + sizeof (struct tree_decl_common),
2058 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2060 DECL_UID (olddecl) = olddecl_uid;
2061 DECL_CONTEXT (olddecl) = olddecl_context;
2062 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2063 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2066 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2067 so that encode_section_info has a chance to look at the new decl
2068 flags and attributes. */
2069 if (DECL_RTL_SET_P (olddecl)
2070 && (TREE_CODE (olddecl) == FUNCTION_DECL
2071 || (TREE_CODE (olddecl) == VAR_DECL
2072 && TREE_STATIC (olddecl))))
2073 make_decl_rtl (olddecl);
2075 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
2076 and the definition is coming from the old version, cgraph needs
2077 to be called again. */
2078 if (extern_changed && !new_is_definition
2079 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
2080 cgraph_mark_if_needed (olddecl);
2083 /* Handle when a new declaration NEWDECL has the same name as an old
2084 one OLDDECL in the same binding contour. Prints an error message
2087 If safely possible, alter OLDDECL to look like NEWDECL, and return
2088 true. Otherwise, return false. */
2091 duplicate_decls (tree newdecl, tree olddecl)
2093 tree newtype = NULL, oldtype = NULL;
2095 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2097 /* Avoid `unused variable' and other warnings for OLDDECL. */
2098 TREE_NO_WARNING (olddecl) = 1;
2102 merge_decls (newdecl, olddecl, newtype, oldtype);
2107 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2109 warn_if_shadowing (tree new_decl)
2111 struct c_binding *b;
2113 /* Shadow warnings wanted? */
2115 /* No shadow warnings for internally generated vars. */
2116 || DECL_IS_BUILTIN (new_decl)
2117 /* No shadow warnings for vars made for inlining. */
2118 || DECL_FROM_INLINE (new_decl))
2121 /* Is anything being shadowed? Invisible decls do not count. */
2122 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2123 if (b->decl && b->decl != new_decl && !b->invisible)
2125 tree old_decl = b->decl;
2127 if (old_decl == error_mark_node)
2129 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2130 "non-variable", new_decl);
2133 else if (TREE_CODE (old_decl) == PARM_DECL)
2134 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2136 else if (DECL_FILE_SCOPE_P (old_decl))
2137 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2138 "declaration", new_decl);
2139 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2140 && DECL_BUILT_IN (old_decl))
2142 warning (OPT_Wshadow, "declaration of %q+D shadows "
2143 "a built-in function", new_decl);
2147 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2150 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
2156 /* Record a decl-node X as belonging to the current lexical scope.
2157 Check for errors (such as an incompatible declaration for the same
2158 name already seen in the same scope).
2160 Returns either X or an old decl for the same name.
2161 If an old decl is returned, it may have been smashed
2162 to agree with what X says. */
2167 tree name = DECL_NAME (x);
2168 struct c_scope *scope = current_scope;
2169 struct c_binding *b;
2170 bool nested = false;
2171 location_t locus = DECL_SOURCE_LOCATION (x);
2173 /* Must set DECL_CONTEXT for everything not at file scope or
2174 DECL_FILE_SCOPE_P won't work. Local externs don't count
2175 unless they have initializers (which generate code). */
2176 if (current_function_decl
2177 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2178 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2179 DECL_CONTEXT (x) = current_function_decl;
2181 /* If this is of variably modified type, prevent jumping into its
2183 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2184 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2185 c_begin_vm_scope (scope->depth);
2187 /* Anonymous decls are just inserted in the scope. */
2190 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2195 /* First, see if there is another declaration with the same name in
2196 the current scope. If there is, duplicate_decls may do all the
2197 work for us. If duplicate_decls returns false, that indicates
2198 two incompatible decls in the same scope; we are to silently
2199 replace the old one (duplicate_decls has issued all appropriate
2200 diagnostics). In particular, we should not consider possible
2201 duplicates in the external scope, or shadowing. */
2202 b = I_SYMBOL_BINDING (name);
2203 if (b && B_IN_SCOPE (b, scope))
2205 struct c_binding *b_ext, *b_use;
2206 tree type = TREE_TYPE (x);
2207 tree visdecl = b->decl;
2208 tree vistype = TREE_TYPE (visdecl);
2209 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2210 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2211 b->inner_comp = false;
2214 /* If this is an external linkage declaration, we should check
2215 for compatibility with the type in the external scope before
2216 setting the type at this scope based on the visible
2217 information only. */
2218 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2220 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2221 b_ext = b_ext->shadowed;
2226 TREE_TYPE (b_use->decl) = b_use->type;
2229 if (duplicate_decls (x, b_use->decl))
2233 /* Save the updated type in the external scope and
2234 restore the proper type for this scope. */
2236 if (comptypes (vistype, type))
2237 thistype = composite_type (vistype, type);
2239 thistype = TREE_TYPE (b_use->decl);
2240 b_use->type = TREE_TYPE (b_use->decl);
2241 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2242 && DECL_BUILT_IN (b_use->decl))
2244 = build_type_attribute_variant (thistype,
2247 TREE_TYPE (b_use->decl) = thistype;
2252 goto skip_external_and_shadow_checks;
2255 /* All declarations with external linkage, and all external
2256 references, go in the external scope, no matter what scope is
2257 current. However, the binding in that scope is ignored for
2258 purposes of normal name lookup. A separate binding structure is
2259 created in the requested scope; this governs the normal
2260 visibility of the symbol.
2262 The binding in the externals scope is used exclusively for
2263 detecting duplicate declarations of the same object, no matter
2264 what scope they are in; this is what we do here. (C99 6.2.7p2:
2265 All declarations that refer to the same object or function shall
2266 have compatible type; otherwise, the behavior is undefined.) */
2267 if (DECL_EXTERNAL (x) || scope == file_scope)
2269 tree type = TREE_TYPE (x);
2272 bool type_saved = false;
2273 if (b && !B_IN_EXTERNAL_SCOPE (b)
2274 && (TREE_CODE (b->decl) == FUNCTION_DECL
2275 || TREE_CODE (b->decl) == VAR_DECL)
2276 && DECL_FILE_SCOPE_P (b->decl))
2279 vistype = TREE_TYPE (visdecl);
2281 if (scope != file_scope
2282 && !DECL_IN_SYSTEM_HEADER (x))
2283 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2285 while (b && !B_IN_EXTERNAL_SCOPE (b))
2287 /* If this decl might be modified, save its type. This is
2288 done here rather than when the decl is first bound
2289 because the type may change after first binding, through
2290 being completed or through attributes being added. If we
2291 encounter multiple such decls, only the first should have
2292 its type saved; the others will already have had their
2293 proper types saved and the types will not have changed as
2294 their scopes will not have been re-entered. */
2295 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2297 b->type = TREE_TYPE (b->decl);
2300 if (B_IN_FILE_SCOPE (b)
2301 && TREE_CODE (b->decl) == VAR_DECL
2302 && TREE_STATIC (b->decl)
2303 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2304 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2305 && TREE_CODE (type) == ARRAY_TYPE
2306 && TYPE_DOMAIN (type)
2307 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2308 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2310 /* Array type completed in inner scope, which should be
2311 diagnosed if the completion does not have size 1 and
2312 it does not get completed in the file scope. */
2313 b->inner_comp = true;
2318 /* If a matching external declaration has been found, set its
2319 type to the composite of all the types of that declaration.
2320 After the consistency checks, it will be reset to the
2321 composite of the visible types only. */
2322 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2324 TREE_TYPE (b->decl) = b->type;
2326 /* The point of the same_translation_unit_p check here is,
2327 we want to detect a duplicate decl for a construct like
2328 foo() { extern bar(); } ... static bar(); but not if
2329 they are in different translation units. In any case,
2330 the static does not go in the externals scope. */
2332 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2333 && duplicate_decls (x, b->decl))
2338 if (comptypes (vistype, type))
2339 thistype = composite_type (vistype, type);
2341 thistype = TREE_TYPE (b->decl);
2345 b->type = TREE_TYPE (b->decl);
2346 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2348 = build_type_attribute_variant (thistype,
2349 TYPE_ATTRIBUTES (b->type));
2350 TREE_TYPE (b->decl) = thistype;
2351 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2355 else if (TREE_PUBLIC (x))
2357 if (visdecl && !b && duplicate_decls (x, visdecl))
2359 /* An external declaration at block scope referring to a
2360 visible entity with internal linkage. The composite
2361 type will already be correct for this scope, so we
2362 just need to fall through to make the declaration in
2369 bind (name, x, external_scope, /*invisible=*/true,
2370 /*nested=*/false, locus);
2376 if (TREE_CODE (x) != PARM_DECL)
2377 warn_if_shadowing (x);
2379 skip_external_and_shadow_checks:
2380 if (TREE_CODE (x) == TYPE_DECL)
2381 set_underlying_type (x);
2383 bind (name, x, scope, /*invisible=*/false, nested, locus);
2385 /* If x's type is incomplete because it's based on a
2386 structure or union which has not yet been fully declared,
2387 attach it to that structure or union type, so we can go
2388 back and complete the variable declaration later, if the
2389 structure or union gets fully declared.
2391 If the input is erroneous, we can have error_mark in the type
2392 slot (e.g. "f(void a, ...)") - that doesn't count as an
2394 if (TREE_TYPE (x) != error_mark_node
2395 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2397 tree element = TREE_TYPE (x);
2399 while (TREE_CODE (element) == ARRAY_TYPE)
2400 element = TREE_TYPE (element);
2401 element = TYPE_MAIN_VARIANT (element);
2403 if ((TREE_CODE (element) == RECORD_TYPE
2404 || TREE_CODE (element) == UNION_TYPE)
2405 && (TREE_CODE (x) != TYPE_DECL
2406 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2407 && !COMPLETE_TYPE_P (element))
2408 C_TYPE_INCOMPLETE_VARS (element)
2409 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2414 /* Record X as belonging to file scope.
2415 This is used only internally by the Objective-C front end,
2416 and is limited to its needs. duplicate_decls is not called;
2417 if there is any preexisting decl for this identifier, it is an ICE. */
2420 pushdecl_top_level (tree x)
2423 bool nested = false;
2424 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2426 name = DECL_NAME (x);
2428 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2430 if (TREE_PUBLIC (x))
2432 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2437 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2443 implicit_decl_warning (tree id, tree olddecl)
2445 if (warn_implicit_function_declaration)
2450 warned = pedwarn (input_location, OPT_Wimplicit_function_declaration,
2451 "implicit declaration of function %qE", id);
2453 warned = warning (OPT_Wimplicit_function_declaration,
2454 G_("implicit declaration of function %qE"), id);
2455 if (olddecl && warned)
2456 locate_old_decl (olddecl);
2460 /* Generate an implicit declaration for identifier FUNCTIONID as a
2461 function of type int (). */
2464 implicitly_declare (tree functionid)
2466 struct c_binding *b;
2470 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2472 if (B_IN_SCOPE (b, external_scope))
2481 if (decl == error_mark_node)
2484 /* FIXME: Objective-C has weird not-really-builtin functions
2485 which are supposed to be visible automatically. They wind up
2486 in the external scope because they're pushed before the file
2487 scope gets created. Catch this here and rebind them into the
2489 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2491 bind (functionid, decl, file_scope,
2492 /*invisible=*/false, /*nested=*/true,
2493 DECL_SOURCE_LOCATION (decl));
2498 tree newtype = default_function_type;
2500 TREE_TYPE (decl) = b->type;
2501 /* Implicit declaration of a function already declared
2502 (somehow) in a different scope, or as a built-in.
2503 If this is the first time this has happened, warn;
2504 then recycle the old declaration but with the new type. */
2505 if (!C_DECL_IMPLICIT (decl))
2507 implicit_decl_warning (functionid, decl);
2508 C_DECL_IMPLICIT (decl) = 1;
2510 if (DECL_BUILT_IN (decl))
2512 newtype = build_type_attribute_variant (newtype,
2514 (TREE_TYPE (decl)));
2515 if (!comptypes (newtype, TREE_TYPE (decl)))
2517 warning (0, "incompatible implicit declaration of built-in"
2518 " function %qD", decl);
2519 newtype = TREE_TYPE (decl);
2524 if (!comptypes (newtype, TREE_TYPE (decl)))
2526 error ("incompatible implicit declaration of function %qD",
2528 locate_old_decl (decl);
2531 b->type = TREE_TYPE (decl);
2532 TREE_TYPE (decl) = newtype;
2533 bind (functionid, decl, current_scope,
2534 /*invisible=*/false, /*nested=*/true,
2535 DECL_SOURCE_LOCATION (decl));
2540 /* Not seen before. */
2541 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2542 DECL_EXTERNAL (decl) = 1;
2543 TREE_PUBLIC (decl) = 1;
2544 C_DECL_IMPLICIT (decl) = 1;
2545 implicit_decl_warning (functionid, 0);
2546 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2548 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2550 /* C89 says implicit declarations are in the innermost block.
2551 So we record the decl in the standard fashion. */
2552 decl = pushdecl (decl);
2554 /* No need to call objc_check_decl here - it's a function type. */
2555 rest_of_decl_compilation (decl, 0, 0);
2557 /* Write a record describing this implicit function declaration
2558 to the prototypes file (if requested). */
2559 gen_aux_info_record (decl, 0, 1, 0);
2561 /* Possibly apply some default attributes to this implicit declaration. */
2562 decl_attributes (&decl, NULL_TREE, 0);
2567 /* Issue an error message for a reference to an undeclared variable
2568 ID, including a reference to a builtin outside of function-call
2569 context. Establish a binding of the identifier to error_mark_node
2570 in an appropriate scope, which will suppress further errors for the
2571 same identifier. The error message should be given location LOC. */
2573 undeclared_variable (tree id, location_t loc)
2575 static bool already = false;
2576 struct c_scope *scope;
2578 if (current_function_decl == 0)
2580 error ("%H%qE undeclared here (not in a function)", &loc, id);
2581 scope = current_scope;
2585 error ("%H%qE undeclared (first use in this function)", &loc, id);
2589 error ("%H(Each undeclared identifier is reported only once", &loc);
2590 error ("%Hfor each function it appears in.)", &loc);
2594 /* If we are parsing old-style parameter decls, current_function_decl
2595 will be nonnull but current_function_scope will be null. */
2596 scope = current_function_scope ? current_function_scope : current_scope;
2598 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
2602 /* Subroutine of lookup_label, declare_label, define_label: construct a
2603 LABEL_DECL with all the proper frills. */
2606 make_label (tree name, location_t location)
2608 tree label = build_decl (LABEL_DECL, name, void_type_node);
2610 DECL_CONTEXT (label) = current_function_decl;
2611 DECL_MODE (label) = VOIDmode;
2612 DECL_SOURCE_LOCATION (label) = location;
2617 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2618 Create one if none exists so far for the current function.
2619 This is called when a label is used in a goto expression or
2620 has its address taken. */
2623 lookup_label (tree name)
2627 if (current_function_decl == 0)
2629 error ("label %qE referenced outside of any function", name);
2633 /* Use a label already defined or ref'd with this name, but not if
2634 it is inherited from a containing function and wasn't declared
2636 label = I_LABEL_DECL (name);
2637 if (label && (DECL_CONTEXT (label) == current_function_decl
2638 || C_DECLARED_LABEL_FLAG (label)))
2640 /* If the label has only been declared, update its apparent
2641 location to point here, for better diagnostics if it
2642 turns out not to have been defined. */
2643 if (!TREE_USED (label))
2644 DECL_SOURCE_LOCATION (label) = input_location;
2648 /* No label binding for that identifier; make one. */
2649 label = make_label (name, input_location);
2651 /* Ordinary labels go in the current function scope. */
2652 bind (name, label, current_function_scope,
2653 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2657 /* Make a label named NAME in the current function, shadowing silently
2658 any that may be inherited from containing functions or containing
2659 scopes. This is called for __label__ declarations. */
2662 declare_label (tree name)
2664 struct c_binding *b = I_LABEL_BINDING (name);
2667 /* Check to make sure that the label hasn't already been declared
2669 if (b && B_IN_CURRENT_SCOPE (b))
2671 error ("duplicate label declaration %qE", name);
2672 locate_old_decl (b->decl);
2674 /* Just use the previous declaration. */
2678 label = make_label (name, input_location);
2679 C_DECLARED_LABEL_FLAG (label) = 1;
2681 /* Declared labels go in the current scope. */
2682 bind (name, label, current_scope,
2683 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2687 /* Define a label, specifying the location in the source file.
2688 Return the LABEL_DECL node for the label, if the definition is valid.
2689 Otherwise return 0. */
2692 define_label (location_t location, tree name)
2694 /* Find any preexisting label with this name. It is an error
2695 if that label has already been defined in this function, or
2696 if there is a containing function with a declared label with
2698 tree label = I_LABEL_DECL (name);
2699 struct c_label_list *nlist_se, *nlist_vm;
2702 && ((DECL_CONTEXT (label) == current_function_decl
2703 && DECL_INITIAL (label) != 0)
2704 || (DECL_CONTEXT (label) != current_function_decl
2705 && C_DECLARED_LABEL_FLAG (label))))
2707 error ("%Hduplicate label %qD", &location, label);
2708 locate_old_decl (label);
2711 else if (label && DECL_CONTEXT (label) == current_function_decl)
2713 /* The label has been used or declared already in this function,
2714 but not defined. Update its location to point to this
2716 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2717 error ("%Jjump into statement expression", label);
2718 if (C_DECL_UNDEFINABLE_VM (label))
2719 error ("%Jjump into scope of identifier with variably modified type",
2721 DECL_SOURCE_LOCATION (label) = location;
2725 /* No label binding for that identifier; make one. */
2726 label = make_label (name, location);
2728 /* Ordinary labels go in the current function scope. */
2729 bind (name, label, current_function_scope,
2730 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2733 if (!in_system_header && lookup_name (name))
2734 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2735 "for labels, identifier %qE conflicts", &location, name);
2737 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2738 nlist_se->next = label_context_stack_se->labels_def;
2739 nlist_se->label = label;
2740 label_context_stack_se->labels_def = nlist_se;
2742 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2743 nlist_vm->next = label_context_stack_vm->labels_def;
2744 nlist_vm->label = label;
2745 label_context_stack_vm->labels_def = nlist_vm;
2747 /* Mark label as having been defined. */
2748 DECL_INITIAL (label) = error_mark_node;
2752 /* Given NAME, an IDENTIFIER_NODE,
2753 return the structure (or union or enum) definition for that name.
2754 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2755 CODE says which kind of type the caller wants;
2756 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2757 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
2758 location where the tag was defined.
2759 If the wrong kind of type is found, an error is reported. */
2762 lookup_tag (enum tree_code code, tree name, int thislevel_only,
2765 struct c_binding *b = I_TAG_BINDING (name);
2771 /* We only care about whether it's in this level if
2772 thislevel_only was set or it might be a type clash. */
2773 if (thislevel_only || TREE_CODE (b->decl) != code)
2775 /* For our purposes, a tag in the external scope is the same as
2776 a tag in the file scope. (Primarily relevant to Objective-C
2777 and its builtin structure tags, which get pushed before the
2778 file scope is created.) */
2779 if (B_IN_CURRENT_SCOPE (b)
2780 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2784 if (thislevel_only && !thislevel)
2787 if (TREE_CODE (b->decl) != code)
2789 /* Definition isn't the kind we were looking for. */
2790 pending_invalid_xref = name;
2791 pending_invalid_xref_location = input_location;
2793 /* If in the same binding level as a declaration as a tag
2794 of a different type, this must not be allowed to
2795 shadow that tag, so give the error immediately.
2796 (For example, "struct foo; union foo;" is invalid.) */
2798 pending_xref_error ();
2807 /* Print an error message now
2808 for a recent invalid struct, union or enum cross reference.
2809 We don't print them immediately because they are not invalid
2810 when used in the `struct foo;' construct for shadowing. */
2813 pending_xref_error (void)
2815 if (pending_invalid_xref != 0)
2816 error ("%H%qE defined as wrong kind of tag",
2817 &pending_invalid_xref_location, pending_invalid_xref);
2818 pending_invalid_xref = 0;
2822 /* Look up NAME in the current scope and its superiors
2823 in the namespace of variables, functions and typedefs.
2824 Return a ..._DECL node of some kind representing its definition,
2825 or return 0 if it is undefined. */
2828 lookup_name (tree name)
2830 struct c_binding *b = I_SYMBOL_BINDING (name);
2831 if (b && !b->invisible)
2836 /* Similar to `lookup_name' but look only at the indicated scope. */
2839 lookup_name_in_scope (tree name, struct c_scope *scope)
2841 struct c_binding *b;
2843 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2844 if (B_IN_SCOPE (b, scope))
2849 /* Create the predefined scalar types of C,
2850 and some nodes representing standard constants (0, 1, (void *) 0).
2851 Initialize the global scope.
2852 Make definitions for built-in primitive functions. */
2855 c_init_decl_processing (void)
2857 location_t save_loc = input_location;
2859 /* Initialize reserved words for parser. */
2862 current_function_decl = 0;
2864 gcc_obstack_init (&parser_obstack);
2866 /* Make the externals scope. */
2868 external_scope = current_scope;
2870 /* Declarations from c_common_nodes_and_builtins must not be associated
2871 with this input file, lest we get differences between using and not
2872 using preprocessed headers. */
2873 input_location = BUILTINS_LOCATION;
2875 build_common_tree_nodes (flag_signed_char, false);
2877 c_common_nodes_and_builtins ();
2879 /* In C, comparisons and TRUTH_* expressions have type int. */
2880 truthvalue_type_node = integer_type_node;
2881 truthvalue_true_node = integer_one_node;
2882 truthvalue_false_node = integer_zero_node;
2884 /* Even in C99, which has a real boolean type. */
2885 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2886 boolean_type_node));
2888 input_location = save_loc;
2890 pedantic_lvalues = true;
2892 make_fname_decl = c_make_fname_decl;
2893 start_fname_decls ();
2896 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2897 decl, NAME is the initialization string and TYPE_DEP indicates whether
2898 NAME depended on the type of the function. As we don't yet implement
2899 delayed emission of static data, we mark the decl as emitted
2900 so it is not placed in the output. Anything using it must therefore pull
2901 out the STRING_CST initializer directly. FIXME. */
2904 c_make_fname_decl (tree id, int type_dep)
2906 const char *name = fname_as_string (type_dep);
2907 tree decl, type, init;
2908 size_t length = strlen (name);
2910 type = build_array_type (char_type_node,
2911 build_index_type (size_int (length)));
2912 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2914 decl = build_decl (VAR_DECL, id, type);
2916 TREE_STATIC (decl) = 1;
2917 TREE_READONLY (decl) = 1;
2918 DECL_ARTIFICIAL (decl) = 1;
2920 init = build_string (length + 1, name);
2921 free (CONST_CAST (char *, name));
2922 TREE_TYPE (init) = type;
2923 DECL_INITIAL (decl) = init;
2925 TREE_USED (decl) = 1;
2927 if (current_function_decl
2928 /* For invalid programs like this:
2931 const char* p = __FUNCTION__;
2933 the __FUNCTION__ is believed to appear in K&R style function
2934 parameter declarator. In that case we still don't have
2936 && (!errorcount || current_function_scope))
2938 DECL_CONTEXT (decl) = current_function_decl;
2939 bind (id, decl, current_function_scope,
2940 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2943 finish_decl (decl, init, NULL_TREE, NULL_TREE);
2949 c_builtin_function (tree decl)
2951 tree type = TREE_TYPE (decl);
2952 tree id = DECL_NAME (decl);
2954 const char *name = IDENTIFIER_POINTER (id);
2955 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2957 /* Should never be called on a symbol with a preexisting meaning. */
2958 gcc_assert (!I_SYMBOL_BINDING (id));
2960 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
2963 /* Builtins in the implementation namespace are made visible without
2964 needing to be explicitly declared. See push_file_scope. */
2965 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2967 TREE_CHAIN (decl) = visible_builtins;
2968 visible_builtins = decl;
2975 c_builtin_function_ext_scope (tree decl)
2977 tree type = TREE_TYPE (decl);
2978 tree id = DECL_NAME (decl);
2980 const char *name = IDENTIFIER_POINTER (id);
2981 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2983 /* Should never be called on a symbol with a preexisting meaning. */
2984 gcc_assert (!I_SYMBOL_BINDING (id));
2986 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
2989 /* Builtins in the implementation namespace are made visible without
2990 needing to be explicitly declared. See push_file_scope. */
2991 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2993 TREE_CHAIN (decl) = visible_builtins;
2994 visible_builtins = decl;
3000 /* Called when a declaration is seen that contains no names to declare.
3001 If its type is a reference to a structure, union or enum inherited
3002 from a containing scope, shadow that tag name for the current scope
3003 with a forward reference.
3004 If its type defines a new named structure or union
3005 or defines an enum, it is valid but we need not do anything here.
3006 Otherwise, it is an error. */
3009 shadow_tag (const struct c_declspecs *declspecs)
3011 shadow_tag_warned (declspecs, 0);
3014 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
3017 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
3019 bool found_tag = false;
3021 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
3023 tree value = declspecs->type;
3024 enum tree_code code = TREE_CODE (value);
3026 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
3027 /* Used to test also that TYPE_SIZE (value) != 0.
3028 That caused warning for `struct foo;' at top level in the file. */
3030 tree name = TYPE_NAME (value);
3035 if (declspecs->restrict_p)
3037 error ("invalid use of %<restrict%>");
3043 if (warned != 1 && code != ENUMERAL_TYPE)
3044 /* Empty unnamed enum OK */
3046 pedwarn (input_location, 0,
3047 "unnamed struct/union that defines no instances");
3051 else if (!declspecs->tag_defined_p
3052 && declspecs->storage_class != csc_none)
3055 pedwarn (input_location, 0,
3056 "empty declaration with storage class specifier "
3057 "does not redeclare tag");
3059 pending_xref_error ();
3061 else if (!declspecs->tag_defined_p
3062 && (declspecs->const_p
3063 || declspecs->volatile_p
3064 || declspecs->restrict_p))
3067 pedwarn (input_location, 0,
3068 "empty declaration with type qualifier "
3069 "does not redeclare tag");
3071 pending_xref_error ();
3075 pending_invalid_xref = 0;
3076 t = lookup_tag (code, name, 1, NULL);
3080 t = make_node (code);
3081 pushtag (name, t, input_location);
3087 if (warned != 1 && !in_system_header)
3089 pedwarn (input_location, 0,
3090 "useless type name in empty declaration");
3095 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
3097 pedwarn (input_location, 0, "useless type name in empty declaration");
3101 pending_invalid_xref = 0;
3103 if (declspecs->inline_p)
3105 error ("%<inline%> in empty declaration");
3109 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3111 error ("%<auto%> in file-scope empty declaration");
3115 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3117 error ("%<register%> in file-scope empty declaration");
3121 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3123 warning (0, "useless storage class specifier in empty declaration");
3127 if (!warned && !in_system_header && declspecs->thread_p)
3129 warning (0, "useless %<__thread%> in empty declaration");
3133 if (!warned && !in_system_header && (declspecs->const_p
3134 || declspecs->volatile_p
3135 || declspecs->restrict_p))
3137 warning (0, "useless type qualifier in empty declaration");
3144 pedwarn (input_location, 0, "empty declaration");
3149 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3150 bits. SPECS represents declaration specifiers that the grammar
3151 only permits to contain type qualifiers and attributes. */
3154 quals_from_declspecs (const struct c_declspecs *specs)
3156 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3157 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3158 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3159 gcc_assert (!specs->type
3160 && !specs->decl_attr
3161 && specs->typespec_word == cts_none
3162 && specs->storage_class == csc_none
3163 && !specs->typedef_p
3164 && !specs->explicit_signed_p
3165 && !specs->deprecated_p
3167 && !specs->long_long_p
3170 && !specs->unsigned_p
3171 && !specs->complex_p
3173 && !specs->thread_p);
3177 /* Construct an array declarator. EXPR is the expression inside [],
3178 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3179 applied to the pointer to which a parameter array is converted).
3180 STATIC_P is true if "static" is inside the [], false otherwise.
3181 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3182 length which is nevertheless a complete type, false otherwise. The
3183 field for the contained declarator is left to be filled in by
3184 set_array_declarator_inner. */
3186 struct c_declarator *
3187 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3190 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3191 struct c_declarator);
3192 declarator->kind = cdk_array;
3193 declarator->declarator = 0;
3194 declarator->u.array.dimen = expr;
3197 declarator->u.array.attrs = quals->attrs;
3198 declarator->u.array.quals = quals_from_declspecs (quals);
3202 declarator->u.array.attrs = NULL_TREE;
3203 declarator->u.array.quals = 0;
3205 declarator->u.array.static_p = static_p;
3206 declarator->u.array.vla_unspec_p = vla_unspec_p;
3209 if (static_p || quals != NULL)
3210 pedwarn (input_location, OPT_pedantic,
3211 "ISO C90 does not support %<static%> or type "
3212 "qualifiers in parameter array declarators");
3214 pedwarn (input_location, OPT_pedantic,
3215 "ISO C90 does not support %<[*]%> array declarators");
3219 if (!current_scope->parm_flag)
3222 error ("%<[*]%> not allowed in other than function prototype scope");
3223 declarator->u.array.vla_unspec_p = false;
3226 current_scope->had_vla_unspec = true;
3231 /* Set the contained declarator of an array declarator. DECL is the
3232 declarator, as constructed by build_array_declarator; INNER is what
3233 appears on the left of the []. */
3235 struct c_declarator *
3236 set_array_declarator_inner (struct c_declarator *decl,
3237 struct c_declarator *inner)
3239 decl->declarator = inner;
3243 /* INIT is a constructor that forms DECL's initializer. If the final
3244 element initializes a flexible array field, add the size of that
3245 initializer to DECL's size. */
3248 add_flexible_array_elts_to_size (tree decl, tree init)
3252 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3255 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3256 type = TREE_TYPE (elt);
3257 if (TREE_CODE (type) == ARRAY_TYPE
3258 && TYPE_SIZE (type) == NULL_TREE
3259 && TYPE_DOMAIN (type) != NULL_TREE
3260 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3262 complete_array_type (&type, elt, false);
3264 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3265 DECL_SIZE_UNIT (decl)
3266 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3270 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3271 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3272 before the type name, and set *EXPR_CONST_OPERANDS, if
3273 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3274 appear in a constant expression. */
3277 groktypename (struct c_type_name *type_name, tree *expr,
3278 bool *expr_const_operands)
3281 tree attrs = type_name->specs->attrs;
3283 type_name->specs->attrs = NULL_TREE;
3285 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3286 false, NULL, &attrs, expr, expr_const_operands,
3289 /* Apply attributes. */
3290 decl_attributes (&type, attrs, 0);
3295 /* Decode a declarator in an ordinary declaration or data definition.
3296 This is called as soon as the type information and variable name
3297 have been parsed, before parsing the initializer if any.
3298 Here we create the ..._DECL node, fill in its type,
3299 and put it on the list of decls for the current context.
3300 The ..._DECL node is returned as the value.
3302 Exception: for arrays where the length is not specified,
3303 the type is left null, to be filled in by `finish_decl'.
3305 Function definitions do not come here; they go to start_function
3306 instead. However, external and forward declarations of functions
3307 do go through here. Structure field declarations are done by
3308 grokfield and not through here. */
3311 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3312 bool initialized, tree attributes)
3316 tree expr = NULL_TREE;
3317 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3319 /* An object declared as __attribute__((deprecated)) suppresses
3320 warnings of uses of other deprecated items. */
3321 if (lookup_attribute ("deprecated", attributes))
3322 deprecated_state = DEPRECATED_SUPPRESS;
3324 decl = grokdeclarator (declarator, declspecs,
3325 NORMAL, initialized, NULL, &attributes, &expr, NULL,
3333 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
3334 warning (OPT_Wmain, "%q+D is usually a function", decl);
3337 /* Is it valid for this decl to have an initializer at all?
3338 If not, set INITIALIZED to zero, which will indirectly
3339 tell 'finish_decl' to ignore the initializer once it is parsed. */
3340 switch (TREE_CODE (decl))
3343 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3348 error ("function %qD is initialized like a variable", decl);
3353 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3354 error ("parameter %qD is initialized", decl);
3359 /* Don't allow initializations for incomplete types except for
3360 arrays which might be completed by the initialization. */
3362 /* This can happen if the array size is an undefined macro.
3363 We already gave a warning, so we don't need another one. */
3364 if (TREE_TYPE (decl) == error_mark_node)
3366 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3368 /* A complete type is ok if size is fixed. */
3370 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3371 || C_DECL_VARIABLE_SIZE (decl))
3373 error ("variable-sized object may not be initialized");
3377 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3379 error ("variable %qD has initializer but incomplete type", decl);
3382 else if (C_DECL_VARIABLE_SIZE (decl))
3384 /* Although C99 is unclear about whether incomplete arrays
3385 of VLAs themselves count as VLAs, it does not make
3386 sense to permit them to be initialized given that
3387 ordinary VLAs may not be initialized. */
3388 error ("variable-sized object may not be initialized");
3395 if (current_scope == file_scope)
3396 TREE_STATIC (decl) = 1;
3398 /* Tell 'pushdecl' this is an initialized decl
3399 even though we don't yet have the initializer expression.
3400 Also tell 'finish_decl' it may store the real initializer. */
3401 DECL_INITIAL (decl) = error_mark_node;
3404 /* If this is a function declaration, write a record describing it to the
3405 prototypes file (if requested). */
3407 if (TREE_CODE (decl) == FUNCTION_DECL)
3408 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3410 /* ANSI specifies that a tentative definition which is not merged with
3411 a non-tentative definition behaves exactly like a definition with an
3412 initializer equal to zero. (Section 3.7.2)
3414 -fno-common gives strict ANSI behavior, though this tends to break
3415 a large body of code that grew up without this rule.
3417 Thread-local variables are never common, since there's no entrenched
3418 body of code to break, and it allows more efficient variable references
3419 in the presence of dynamic linking. */
3421 if (TREE_CODE (decl) == VAR_DECL
3423 && TREE_PUBLIC (decl)
3424 && !DECL_THREAD_LOCAL_P (decl)
3426 DECL_COMMON (decl) = 1;
3428 /* Set attributes here so if duplicate decl, will have proper attributes. */
3429 decl_attributes (&decl, attributes, 0);
3431 /* Handle gnu_inline attribute. */
3432 if (declspecs->inline_p
3433 && !flag_gnu89_inline
3434 && TREE_CODE (decl) == FUNCTION_DECL
3435 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
3436 || current_function_decl))
3438 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3440 else if (declspecs->storage_class != csc_static)
3441 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3444 if (TREE_CODE (decl) == FUNCTION_DECL
3445 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3447 struct c_declarator *ce = declarator;
3449 if (ce->kind == cdk_pointer)
3450 ce = declarator->declarator;
3451 if (ce->kind == cdk_function)
3453 tree args = ce->u.arg_info->parms;
3454 for (; args; args = TREE_CHAIN (args))
3456 tree type = TREE_TYPE (args);
3457 if (type && INTEGRAL_TYPE_P (type)
3458 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3459 DECL_ARG_TYPE (args) = integer_type_node;
3464 if (TREE_CODE (decl) == FUNCTION_DECL
3465 && DECL_DECLARED_INLINE_P (decl)
3466 && DECL_UNINLINABLE (decl)
3467 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3468 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3471 /* C99 6.7.4p3: An inline definition of a function with external
3472 linkage shall not contain a definition of a modifiable object
3473 with static storage duration... */
3474 if (TREE_CODE (decl) == VAR_DECL
3475 && current_scope != file_scope
3476 && TREE_STATIC (decl)
3477 && !TREE_READONLY (decl)
3478 && DECL_DECLARED_INLINE_P (current_function_decl)
3479 && DECL_EXTERNAL (current_function_decl))
3480 record_inline_static (input_location, current_function_decl,
3481 decl, csi_modifiable);
3483 /* Add this decl to the current scope.
3484 TEM may equal DECL or it may be a previous decl of the same name. */
3485 tem = pushdecl (decl);
3487 if (initialized && DECL_EXTERNAL (tem))
3489 DECL_EXTERNAL (tem) = 0;
3490 TREE_STATIC (tem) = 1;
3496 /* Initialize EH if not initialized yet and exceptions are enabled. */
3499 c_maybe_initialize_eh (void)
3501 if (!flag_exceptions || c_eh_initialized_p)
3504 c_eh_initialized_p = true;
3505 eh_personality_libfunc
3506 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3507 ? "__gcc_personality_sj0"
3508 : "__gcc_personality_v0");
3509 default_init_unwind_resume_libfunc ();
3510 using_eh_for_cleanups ();
3513 /* Finish processing of a declaration;
3514 install its initial value.
3515 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
3516 If the length of an array type is not known before,
3517 it must be determined now, from the initial value, or it is an error. */
3520 finish_decl (tree decl, tree init, tree origtype, tree asmspec_tree)
3523 int was_incomplete = (DECL_SIZE (decl) == 0);
3524 const char *asmspec = 0;
3526 /* If a name was specified, get the string. */
3527 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3528 && DECL_FILE_SCOPE_P (decl))
3529 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3531 asmspec = TREE_STRING_POINTER (asmspec_tree);
3533 /* If `start_decl' didn't like having an initialization, ignore it now. */
3534 if (init != 0 && DECL_INITIAL (decl) == 0)
3537 /* Don't crash if parm is initialized. */
3538 if (TREE_CODE (decl) == PARM_DECL)
3542 store_init_value (decl, init, origtype);
3544 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3545 || TREE_CODE (decl) == FUNCTION_DECL
3546 || TREE_CODE (decl) == FIELD_DECL))
3547 objc_check_decl (decl);
3549 type = TREE_TYPE (decl);
3551 /* Deduce size of array from initialization, if not already known. */
3552 if (TREE_CODE (type) == ARRAY_TYPE
3553 && TYPE_DOMAIN (type) == 0
3554 && TREE_CODE (decl) != TYPE_DECL)
3557 = (TREE_STATIC (decl)
3558 /* Even if pedantic, an external linkage array
3559 may have incomplete type at first. */
3560 ? pedantic && !TREE_PUBLIC (decl)
3561 : !DECL_EXTERNAL (decl));
3563 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3566 /* Get the completed type made by complete_array_type. */
3567 type = TREE_TYPE (decl);
3572 error ("initializer fails to determine size of %q+D", decl);
3577 error ("array size missing in %q+D", decl);
3578 /* If a `static' var's size isn't known,
3579 make it extern as well as static, so it does not get
3581 If it is not `static', then do not mark extern;
3582 finish_incomplete_decl will give it a default size
3583 and it will get allocated. */
3584 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3585 DECL_EXTERNAL (decl) = 1;
3589 error ("zero or negative size array %q+D", decl);
3593 /* For global variables, update the copy of the type that
3594 exists in the binding. */
3595 if (TREE_PUBLIC (decl))
3597 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3598 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3599 b_ext = b_ext->shadowed;
3603 b_ext->type = composite_type (b_ext->type, type);
3614 if (DECL_INITIAL (decl))
3615 TREE_TYPE (DECL_INITIAL (decl)) = type;
3617 layout_decl (decl, 0);
3620 if (TREE_CODE (decl) == VAR_DECL)
3622 if (init && TREE_CODE (init) == CONSTRUCTOR)
3623 add_flexible_array_elts_to_size (decl, init);
3625 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3626 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3627 layout_decl (decl, 0);
3629 if (DECL_SIZE (decl) == 0
3630 /* Don't give an error if we already gave one earlier. */
3631 && TREE_TYPE (decl) != error_mark_node
3632 && (TREE_STATIC (decl)
3633 /* A static variable with an incomplete type
3634 is an error if it is initialized.
3635 Also if it is not file scope.
3636 Otherwise, let it through, but if it is not `extern'
3637 then it may cause an error message later. */
3638 ? (DECL_INITIAL (decl) != 0
3639 || !DECL_FILE_SCOPE_P (decl))
3640 /* An automatic variable with an incomplete type
3642 : !DECL_EXTERNAL (decl)))
3644 error ("storage size of %q+D isn%'t known", decl);
3645 TREE_TYPE (decl) = error_mark_node;
3648 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3649 && DECL_SIZE (decl) != 0)
3651 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3652 constant_expression_warning (DECL_SIZE (decl));
3655 error ("storage size of %q+D isn%'t constant", decl);
3656 TREE_TYPE (decl) = error_mark_node;
3660 if (TREE_USED (type))
3661 TREE_USED (decl) = 1;
3664 /* If this is a function and an assembler name is specified, reset DECL_RTL
3665 so we can give it its new name. Also, update built_in_decls if it
3666 was a normal built-in. */
3667 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3669 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3670 set_builtin_user_assembler_name (decl, asmspec);
3671 set_user_assembler_name (decl, asmspec);
3674 /* If #pragma weak was used, mark the decl weak now. */
3675 maybe_apply_pragma_weak (decl);
3677 /* Output the assembler code and/or RTL code for variables and functions,
3678 unless the type is an undefined structure or union.
3679 If not, it will get done when the type is completed. */
3681 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3683 /* Determine the ELF visibility. */
3684 if (TREE_PUBLIC (decl))
3685 c_determine_visibility (decl);
3687 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3688 if (c_dialect_objc ())
3689 objc_check_decl (decl);
3693 /* If this is not a static variable, issue a warning.
3694 It doesn't make any sense to give an ASMSPEC for an
3695 ordinary, non-register local variable. Historically,
3696 GCC has accepted -- but ignored -- the ASMSPEC in
3698 if (!DECL_FILE_SCOPE_P (decl)
3699 && TREE_CODE (decl) == VAR_DECL
3700 && !C_DECL_REGISTER (decl)
3701 && !TREE_STATIC (decl))
3702 warning (0, "ignoring asm-specifier for non-static local "
3703 "variable %q+D", decl);
3705 set_user_assembler_name (decl, asmspec);
3708 if (DECL_FILE_SCOPE_P (decl))
3710 if (DECL_INITIAL (decl) == NULL_TREE
3711 || DECL_INITIAL (decl) == error_mark_node)
3712 /* Don't output anything
3713 when a tentative file-scope definition is seen.
3714 But at end of compilation, do output code for them. */
3715 DECL_DEFER_OUTPUT (decl) = 1;
3716 rest_of_decl_compilation (decl, true, 0);
3720 /* In conjunction with an ASMSPEC, the `register'
3721 keyword indicates that we should place the variable
3722 in a particular register. */
3723 if (asmspec && C_DECL_REGISTER (decl))
3725 DECL_HARD_REGISTER (decl) = 1;
3726 /* This cannot be done for a structure with volatile
3727 fields, on which DECL_REGISTER will have been
3729 if (!DECL_REGISTER (decl))
3730 error ("cannot put object with volatile field into register");
3733 if (TREE_CODE (decl) != FUNCTION_DECL)
3735 /* If we're building a variable sized type, and we might be
3736 reachable other than via the top of the current binding
3737 level, then create a new BIND_EXPR so that we deallocate
3738 the object at the right time. */
3739 /* Note that DECL_SIZE can be null due to errors. */
3740 if (DECL_SIZE (decl)
3741 && !TREE_CONSTANT (DECL_SIZE (decl))
3742 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3745 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3746 TREE_SIDE_EFFECTS (bind) = 1;
3748 BIND_EXPR_BODY (bind) = push_stmt_list ();
3750 add_stmt (build_stmt (DECL_EXPR, decl));
3755 if (!DECL_FILE_SCOPE_P (decl))
3757 /* Recompute the RTL of a local array now
3758 if it used to be an incomplete type. */
3760 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3762 /* If we used it already as memory, it must stay in memory. */
3763 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3764 /* If it's still incomplete now, no init will save it. */
3765 if (DECL_SIZE (decl) == 0)
3766 DECL_INITIAL (decl) = 0;
3771 if (TREE_CODE (decl) == TYPE_DECL)
3773 if (!DECL_FILE_SCOPE_P (decl)
3774 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3775 add_stmt (build_stmt (DECL_EXPR, decl));
3777 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3780 /* At the end of a declaration, throw away any variable type sizes
3781 of types defined inside that declaration. There is no use
3782 computing them in the following function definition. */
3783 if (current_scope == file_scope)
3784 get_pending_sizes ();
3786 /* Install a cleanup (aka destructor) if one was given. */
3787 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3789 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3792 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3793 tree cleanup_decl = lookup_name (cleanup_id);
3797 /* Build "cleanup(&decl)" for the destructor. */
3798 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
3799 vec = VEC_alloc (tree, gc, 1);
3800 VEC_quick_push (tree, vec, cleanup);
3801 cleanup = build_function_call_vec (cleanup_decl, vec, NULL);
3802 VEC_free (tree, gc, vec);
3804 /* Don't warn about decl unused; the cleanup uses it. */
3805 TREE_USED (decl) = 1;
3806 TREE_USED (cleanup_decl) = 1;
3808 /* Initialize EH, if we've been told to do so. */
3809 c_maybe_initialize_eh ();
3811 push_cleanup (decl, cleanup, false);
3816 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3819 grokparm (const struct c_parm *parm)
3821 tree attrs = parm->attrs;
3822 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3823 NULL, &attrs, NULL, NULL, DEPRECATED_NORMAL);
3825 decl_attributes (&decl, attrs, 0);
3830 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3831 and push that on the current scope. */
3834 push_parm_decl (const struct c_parm *parm)
3836 tree attrs = parm->attrs;
3839 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
3840 &attrs, NULL, NULL, DEPRECATED_NORMAL);
3841 decl_attributes (&decl, attrs, 0);
3843 decl = pushdecl (decl);
3845 finish_decl (decl, NULL_TREE, NULL_TREE, NULL_TREE);
3848 /* Mark all the parameter declarations to date as forward decls.
3849 Also diagnose use of this extension. */
3852 mark_forward_parm_decls (void)
3854 struct c_binding *b;
3856 if (pedantic && !current_scope->warned_forward_parm_decls)
3858 pedwarn (input_location, OPT_pedantic,
3859 "ISO C forbids forward parameter declarations");
3860 current_scope->warned_forward_parm_decls = true;
3863 for (b = current_scope->bindings; b; b = b->prev)
3864 if (TREE_CODE (b->decl) == PARM_DECL)
3865 TREE_ASM_WRITTEN (b->decl) = 1;
3868 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3869 literal, which may be an incomplete array type completed by the
3870 initializer; INIT is a CONSTRUCTOR that initializes the compound
3871 literal. NON_CONST is true if the initializers contain something
3872 that cannot occur in a constant expression. */
3875 build_compound_literal (tree type, tree init, bool non_const)
3877 /* We do not use start_decl here because we have a type, not a declarator;
3878 and do not use finish_decl because the decl should be stored inside
3879 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3884 if (type == error_mark_node)
3885 return error_mark_node;
3887 decl = build_decl (VAR_DECL, NULL_TREE, type);
3888 DECL_EXTERNAL (decl) = 0;
3889 TREE_PUBLIC (decl) = 0;
3890 TREE_STATIC (decl) = (current_scope == file_scope);
3891 DECL_CONTEXT (decl) = current_function_decl;
3892 TREE_USED (decl) = 1;
3893 TREE_TYPE (decl) = type;
3894 TREE_READONLY (decl) = TYPE_READONLY (type);
3895 store_init_value (decl, init, NULL_TREE);
3897 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3899 int failure = complete_array_type (&TREE_TYPE (decl),
3900 DECL_INITIAL (decl), true);
3901 gcc_assert (!failure);
3903 type = TREE_TYPE (decl);
3904 TREE_TYPE (DECL_INITIAL (decl)) = type;
3907 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3908 return error_mark_node;
3910 stmt = build_stmt (DECL_EXPR, decl);
3911 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3912 TREE_SIDE_EFFECTS (complit) = 1;
3914 layout_decl (decl, 0);
3916 if (TREE_STATIC (decl))
3918 /* This decl needs a name for the assembler output. */
3919 set_compound_literal_name (decl);
3920 DECL_DEFER_OUTPUT (decl) = 1;
3921 DECL_COMDAT (decl) = 1;
3922 DECL_ARTIFICIAL (decl) = 1;
3923 DECL_IGNORED_P (decl) = 1;
3925 rest_of_decl_compilation (decl, 1, 0);
3930 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
3931 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
3937 /* Check the type of a compound literal. Here we just check that it
3938 is valid for C++. */
3941 check_compound_literal_type (struct c_type_name *type_name, location_t loc)
3943 if (warn_cxx_compat && type_name->specs->tag_defined_p)
3944 warning_at (loc, OPT_Wc___compat,
3945 "defining a type in a compound literal is invalid in C++");
3948 /* Determine whether TYPE is a structure with a flexible array member,
3949 or a union containing such a structure (possibly recursively). */
3952 flexible_array_type_p (tree type)
3955 switch (TREE_CODE (type))
3958 x = TYPE_FIELDS (type);
3961 while (TREE_CHAIN (x) != NULL_TREE)
3963 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3964 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3965 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3966 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3970 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3972 if (flexible_array_type_p (TREE_TYPE (x)))
3981 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3982 replacing with appropriate values if they are invalid. */
3984 check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
3987 unsigned int max_width;
3988 unsigned HOST_WIDE_INT w;
3989 const char *name = (orig_name
3990 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
3991 : _("<anonymous>"));
3993 /* Detect and ignore out of range field width and process valid
3995 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3996 || TREE_CODE (*width) != INTEGER_CST)
3998 error ("bit-field %qs width not an integer constant", name);
3999 *width = integer_one_node;
4003 constant_expression_warning (*width);
4004 if (tree_int_cst_sgn (*width) < 0)
4006 error ("negative width in bit-field %qs", name);
4007 *width = integer_one_node;
4009 else if (integer_zerop (*width) && orig_name)
4011 error ("zero width for bit-field %qs", name);
4012 *width = integer_one_node;
4016 /* Detect invalid bit-field type. */
4017 if (TREE_CODE (*type) != INTEGER_TYPE
4018 && TREE_CODE (*type) != BOOLEAN_TYPE
4019 && TREE_CODE (*type) != ENUMERAL_TYPE)
4021 error ("bit-field %qs has invalid type", name);
4022 *type = unsigned_type_node;
4025 type_mv = TYPE_MAIN_VARIANT (*type);
4026 if (!in_system_header
4027 && type_mv != integer_type_node
4028 && type_mv != unsigned_type_node
4029 && type_mv != boolean_type_node)
4030 pedwarn (input_location, OPT_pedantic,
4031 "type of bit-field %qs is a GCC extension", name);
4033 max_width = TYPE_PRECISION (*type);
4035 if (0 < compare_tree_int (*width, max_width))
4037 error ("width of %qs exceeds its type", name);
4039 *width = build_int_cst (NULL_TREE, w);
4042 w = tree_low_cst (*width, 1);
4044 if (TREE_CODE (*type) == ENUMERAL_TYPE)
4046 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
4048 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
4049 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
4050 warning (0, "%qs is narrower than values of its type", name);
4056 /* Print warning about variable length array if necessary. */
4059 warn_variable_length_array (tree name, tree size)
4061 int const_size = TREE_CONSTANT (size);
4063 if (!flag_isoc99 && pedantic && warn_vla != 0)
4068 pedwarn (input_location, OPT_Wvla,
4069 "ISO C90 forbids array %qE whose size "
4070 "can%'t be evaluated",
4073 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array whose size "
4074 "can%'t be evaluated");
4079 pedwarn (input_location, OPT_Wvla,
4080 "ISO C90 forbids variable length array %qE",
4083 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array");
4086 else if (warn_vla > 0)
4092 "the size of array %qE can"
4093 "%'t be evaluated", name);
4096 "the size of array can %'t be evaluated");
4102 "variable length array %qE is used",
4106 "variable length array is used");
4111 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR to
4112 serve as the actual size-expression for a type or decl. This is
4113 like variable_size in stor-layout.c, but we make global_bindings_p
4114 return negative to avoid calls to that function from outside the
4115 front end resulting in errors at file scope, then call this version
4116 instead from front-end code. */
4119 c_variable_size (tree size)
4123 if (TREE_CONSTANT (size))
4126 size = save_expr (size);
4128 save = skip_simple_arithmetic (size);
4130 if (cfun && cfun->dont_save_pending_sizes_p)
4133 if (!global_bindings_p ())
4134 put_pending_size (save);
4139 /* Given declspecs and a declarator,
4140 determine the name and type of the object declared
4141 and construct a ..._DECL node for it.
4142 (In one case we can return a ..._TYPE node instead.
4143 For invalid input we sometimes return 0.)
4145 DECLSPECS is a c_declspecs structure for the declaration specifiers.
4147 DECL_CONTEXT says which syntactic context this declaration is in:
4148 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4149 FUNCDEF for a function definition. Like NORMAL but a few different
4150 error messages in each case. Return value may be zero meaning
4151 this definition is too screwy to try to parse.
4152 PARM for a parameter declaration (either within a function prototype
4153 or before a function body). Make a PARM_DECL, or return void_type_node.
4154 TYPENAME if for a typename (in a cast or sizeof).
4155 Don't make a DECL node; just return the ..._TYPE node.
4156 FIELD for a struct or union field; make a FIELD_DECL.
4157 INITIALIZED is true if the decl has an initializer.
4158 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4159 representing the width of the bit-field.
4160 DECL_ATTRS points to the list of attributes that should be added to this
4161 decl. Any nested attributes that belong on the decl itself will be
4163 If EXPR is not NULL, any expressions that need to be evaluated as
4164 part of evaluating variably modified types will be stored in *EXPR.
4165 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
4166 set to indicate whether operands in *EXPR can be used in constant
4168 DEPRECATED_STATE is a deprecated_states value indicating whether
4169 deprecation warnings should be suppressed.
4171 In the TYPENAME case, DECLARATOR is really an absolute declarator.
4172 It may also be so in the PARM case, for a prototype where the
4173 argument type is specified but not the name.
4175 This function is where the complicated C meanings of `static'
4176 and `extern' are interpreted. */
4179 grokdeclarator (const struct c_declarator *declarator,
4180 struct c_declspecs *declspecs,
4181 enum decl_context decl_context, bool initialized, tree *width,
4182 tree *decl_attrs, tree *expr, bool *expr_const_operands,
4183 enum deprecated_states deprecated_state)
4185 tree type = declspecs->type;
4186 bool threadp = declspecs->thread_p;
4187 enum c_storage_class storage_class = declspecs->storage_class;
4191 int type_quals = TYPE_UNQUALIFIED;
4192 tree name = NULL_TREE;
4193 bool funcdef_flag = false;
4194 bool funcdef_syntax = false;
4195 int size_varies = 0;
4196 tree decl_attr = declspecs->decl_attr;
4197 int array_ptr_quals = TYPE_UNQUALIFIED;
4198 tree array_ptr_attrs = NULL_TREE;
4199 int array_parm_static = 0;
4200 bool array_parm_vla_unspec_p = false;
4201 tree returned_attrs = NULL_TREE;
4202 bool bitfield = width != NULL;
4204 struct c_arg_info *arg_info = 0;
4206 bool expr_const_operands_dummy;
4210 if (expr_const_operands == NULL)
4211 expr_const_operands = &expr_const_operands_dummy;
4213 *expr = declspecs->expr;
4214 *expr_const_operands = declspecs->expr_const_operands;
4216 if (decl_context == FUNCDEF)
4217 funcdef_flag = true, decl_context = NORMAL;
4219 /* Look inside a declarator for the name being declared
4220 and get it as an IDENTIFIER_NODE, for an error message. */
4222 const struct c_declarator *decl = declarator;
4230 funcdef_syntax = (decl->kind == cdk_function);
4231 decl = decl->declarator;
4235 decl = decl->declarator;
4249 gcc_assert (decl_context == PARM
4250 || decl_context == TYPENAME
4251 || (decl_context == FIELD
4252 && declarator->kind == cdk_id));
4253 gcc_assert (!initialized);
4257 /* A function definition's declarator must have the form of
4258 a function declarator. */
4260 if (funcdef_flag && !funcdef_syntax)
4263 /* If this looks like a function definition, make it one,
4264 even if it occurs where parms are expected.
4265 Then store_parm_decls will reject it and not use it as a parm. */
4266 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4267 decl_context = PARM;
4269 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4270 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
4272 if ((decl_context == NORMAL || decl_context == FIELD)
4273 && current_scope == file_scope
4274 && variably_modified_type_p (type, NULL_TREE))
4277 error ("variably modified %qE at file scope", name);
4279 error ("variably modified field at file scope");
4280 type = integer_type_node;
4283 size_varies = C_TYPE_VARIABLE_SIZE (type);
4285 /* Diagnose defaulting to "int". */
4287 if (declspecs->default_int_p && !in_system_header)
4289 /* Issue a warning if this is an ISO C 99 program or if
4290 -Wreturn-type and this is a function, or if -Wimplicit;
4291 prefer the former warning since it is more explicit. */
4292 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4294 warn_about_return_type = 1;
4298 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int,
4299 "type defaults to %<int%> in declaration of %qE",
4302 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int,
4303 "type defaults to %<int%> in type name");
4307 /* Adjust the type if a bit-field is being declared,
4308 -funsigned-bitfields applied and the type is not explicitly
4310 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4311 && TREE_CODE (type) == INTEGER_TYPE)
4312 type = unsigned_type_for (type);
4314 /* Figure out the type qualifiers for the declaration. There are
4315 two ways a declaration can become qualified. One is something
4316 like `const int i' where the `const' is explicit. Another is
4317 something like `typedef const int CI; CI i' where the type of the
4318 declaration contains the `const'. A third possibility is that
4319 there is a type qualifier on the element type of a typedefed
4320 array type, in which case we should extract that qualifier so
4321 that c_apply_type_quals_to_decl receives the full list of
4322 qualifiers to work with (C90 is not entirely clear about whether
4323 duplicate qualifiers should be diagnosed in this case, but it
4324 seems most appropriate to do so). */
4325 element_type = strip_array_types (type);
4326 constp = declspecs->const_p + TYPE_READONLY (element_type);
4327 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4328 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4329 if (pedantic && !flag_isoc99)
4332 pedwarn (input_location, OPT_pedantic, "duplicate %<const%>");
4334 pedwarn (input_location, OPT_pedantic, "duplicate %<restrict%>");
4336 pedwarn (input_location, OPT_pedantic, "duplicate %<volatile%>");
4338 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4339 type = TYPE_MAIN_VARIANT (type);
4340 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4341 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4342 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4344 /* Warn about storage classes that are invalid for certain
4345 kinds of declarations (parameters, typenames, etc.). */
4349 || storage_class == csc_auto
4350 || storage_class == csc_register
4351 || storage_class == csc_typedef))
4353 if (storage_class == csc_auto)
4354 pedwarn (input_location,
4355 (current_scope == file_scope) ? 0 : OPT_pedantic,
4356 "function definition declared %<auto%>");
4357 if (storage_class == csc_register)
4358 error ("function definition declared %<register%>");
4359 if (storage_class == csc_typedef)
4360 error ("function definition declared %<typedef%>");
4362 error ("function definition declared %<__thread%>");
4364 if (storage_class == csc_auto
4365 || storage_class == csc_register
4366 || storage_class == csc_typedef)
4367 storage_class = csc_none;
4369 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4371 if (decl_context == PARM && storage_class == csc_register)
4375 switch (decl_context)
4379 error ("storage class specified for structure field %qE",
4382 error ("storage class specified for structure field");
4386 error ("storage class specified for parameter %qE", name);
4388 error ("storage class specified for unnamed parameter");
4391 error ("storage class specified for typename");
4394 storage_class = csc_none;
4398 else if (storage_class == csc_extern
4402 /* 'extern' with initialization is invalid if not at file scope. */
4403 if (current_scope == file_scope)
4405 /* It is fine to have 'extern const' when compiling at C
4406 and C++ intersection. */
4407 if (!(warn_cxx_compat && constp))
4408 warning (0, "%qE initialized and declared %<extern%>", name);
4411 error ("%qE has both %<extern%> and initializer", name);
4413 else if (current_scope == file_scope)
4415 if (storage_class == csc_auto)
4416 error ("file-scope declaration of %qE specifies %<auto%>", name);
4417 if (pedantic && storage_class == csc_register)
4418 pedwarn (input_location, OPT_pedantic,
4419 "file-scope declaration of %qE specifies %<register%>", name);
4423 if (storage_class == csc_extern && funcdef_flag)
4424 error ("nested function %qE declared %<extern%>", name);
4425 else if (threadp && storage_class == csc_none)
4427 error ("function-scope %qE implicitly auto and declared "
4434 /* Now figure out the structure of the declarator proper.
4435 Descend through it, creating more complex types, until we reach
4436 the declared identifier (or NULL_TREE, in an absolute declarator).
4437 At each stage we maintain an unqualified version of the type
4438 together with any qualifiers that should be applied to it with
4439 c_build_qualified_type; this way, array types including
4440 multidimensional array types are first built up in unqualified
4441 form and then the qualified form is created with
4442 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4444 while (declarator && declarator->kind != cdk_id)
4446 if (type == error_mark_node)
4448 declarator = declarator->declarator;
4452 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4453 a cdk_pointer (for *...),
4454 a cdk_function (for ...(...)),
4455 a cdk_attrs (for nested attributes),
4456 or a cdk_id (for the name being declared
4457 or the place in an absolute declarator
4458 where the name was omitted).
4459 For the last case, we have just exited the loop.
4461 At this point, TYPE is the type of elements of an array,
4462 or for a function to return, or for a pointer to point to.
4463 After this sequence of ifs, TYPE is the type of the
4464 array or function or pointer, and DECLARATOR has had its
4465 outermost layer removed. */
4467 if (array_ptr_quals != TYPE_UNQUALIFIED
4468 || array_ptr_attrs != NULL_TREE
4469 || array_parm_static)
4471 /* Only the innermost declarator (making a parameter be of
4472 array type which is converted to pointer type)
4473 may have static or type qualifiers. */
4474 error ("static or type qualifiers in non-parameter array declarator");
4475 array_ptr_quals = TYPE_UNQUALIFIED;
4476 array_ptr_attrs = NULL_TREE;
4477 array_parm_static = 0;
4480 switch (declarator->kind)
4484 /* A declarator with embedded attributes. */
4485 tree attrs = declarator->u.attrs;
4486 const struct c_declarator *inner_decl;
4488 declarator = declarator->declarator;
4489 inner_decl = declarator;
4490 while (inner_decl->kind == cdk_attrs)
4491 inner_decl = inner_decl->declarator;
4492 if (inner_decl->kind == cdk_id)
4493 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4494 else if (inner_decl->kind == cdk_function)
4495 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4496 else if (inner_decl->kind == cdk_array)
4497 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4498 returned_attrs = decl_attributes (&type,
4499 chainon (returned_attrs, attrs),
4505 tree itype = NULL_TREE;
4506 tree size = declarator->u.array.dimen;
4507 /* The index is a signed object `sizetype' bits wide. */
4508 tree index_type = c_common_signed_type (sizetype);
4510 array_ptr_quals = declarator->u.array.quals;
4511 array_ptr_attrs = declarator->u.array.attrs;
4512 array_parm_static = declarator->u.array.static_p;
4513 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4515 declarator = declarator->declarator;
4517 /* Check for some types that there cannot be arrays of. */
4519 if (VOID_TYPE_P (type))
4522 error ("declaration of %qE as array of voids", name);
4524 error ("declaration of type name as array of voids");
4525 type = error_mark_node;
4528 if (TREE_CODE (type) == FUNCTION_TYPE)
4531 error ("declaration of %qE as array of functions", name);
4533 error ("declaration of type name as array of functions");
4534 type = error_mark_node;
4537 if (pedantic && !in_system_header && flexible_array_type_p (type))
4538 pedwarn (input_location, OPT_pedantic,
4539 "invalid use of structure with flexible array member");
4541 if (size == error_mark_node)
4542 type = error_mark_node;
4544 if (type == error_mark_node)
4547 /* If size was specified, set ITYPE to a range-type for
4548 that size. Otherwise, ITYPE remains null. finish_decl
4549 may figure it out from an initial value. */
4553 bool size_maybe_const = true;
4554 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
4555 && !TREE_OVERFLOW (size));
4556 bool this_size_varies = false;
4558 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4560 STRIP_TYPE_NOPS (size);
4562 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4565 error ("size of array %qE has non-integer type", name);
4567 error ("size of unnamed array has non-integer type");
4568 size = integer_one_node;
4571 size = c_fully_fold (size, false, &size_maybe_const);
4573 if (pedantic && size_maybe_const && integer_zerop (size))
4576 pedwarn (input_location, OPT_pedantic,
4577 "ISO C forbids zero-size array %qE", name);
4579 pedwarn (input_location, OPT_pedantic,
4580 "ISO C forbids zero-size array");
4583 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
4585 constant_expression_warning (size);
4586 if (tree_int_cst_sgn (size) < 0)
4589 error ("size of array %qE is negative", name);
4591 error ("size of unnamed array is negative");
4592 size = integer_one_node;
4594 /* Handle a size folded to an integer constant but
4595 not an integer constant expression. */
4596 if (!size_int_const)
4598 /* If this is a file scope declaration of an
4599 ordinary identifier, this is invalid code;
4600 diagnosing it here and not subsequently
4601 treating the type as variable-length avoids
4602 more confusing diagnostics later. */
4603 if ((decl_context == NORMAL || decl_context == FIELD)
4604 && current_scope == file_scope)
4605 pedwarn (input_location, 0,
4606 "variably modified %qE at file scope",
4609 this_size_varies = size_varies = 1;
4610 warn_variable_length_array (name, size);
4613 else if ((decl_context == NORMAL || decl_context == FIELD)
4614 && current_scope == file_scope)
4616 error ("variably modified %qE at file scope", name);
4617 size = integer_one_node;
4621 /* Make sure the array size remains visibly
4622 nonconstant even if it is (eg) a const variable
4623 with known value. */
4624 this_size_varies = size_varies = 1;
4625 warn_variable_length_array (name, size);
4628 if (integer_zerop (size) && !this_size_varies)
4630 /* A zero-length array cannot be represented with
4631 an unsigned index type, which is what we'll
4632 get with build_index_type. Create an
4633 open-ended range instead. */
4634 itype = build_range_type (sizetype, size, NULL_TREE);
4638 /* Arrange for the SAVE_EXPR on the inside of the
4639 MINUS_EXPR, which allows the -1 to get folded
4640 with the +1 that happens when building TYPE_SIZE. */
4642 size = c_variable_size (size);
4643 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
4644 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
4645 integer_zero_node, size);
4647 /* Compute the maximum valid index, that is, size
4648 - 1. Do the calculation in index_type, so that
4649 if it is a variable the computations will be
4650 done in the proper mode. */
4651 itype = fold_build2 (MINUS_EXPR, index_type,
4652 convert (index_type, size),
4653 convert (index_type,
4656 /* If that overflowed, the array is too big. ???
4657 While a size of INT_MAX+1 technically shouldn't
4658 cause an overflow (because we subtract 1), the
4659 overflow is recorded during the conversion to
4660 index_type, before the subtraction. Handling
4661 this case seems like an unnecessary
4663 if (TREE_CODE (itype) == INTEGER_CST
4664 && TREE_OVERFLOW (itype))
4667 error ("size of array %qE is too large", name);
4669 error ("size of unnamed array is too large");
4670 type = error_mark_node;
4674 itype = build_index_type (itype);
4676 if (this_size_varies)
4679 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
4683 *expr_const_operands &= size_maybe_const;
4686 else if (decl_context == FIELD)
4688 bool flexible_array_member = false;
4689 if (array_parm_vla_unspec_p)
4690 /* Field names can in fact have function prototype
4691 scope so [*] is disallowed here through making
4692 the field variably modified, not through being
4693 something other than a declaration with function
4698 const struct c_declarator *t = declarator;
4699 while (t->kind == cdk_attrs)
4701 flexible_array_member = (t->kind == cdk_id);
4703 if (flexible_array_member
4704 && pedantic && !flag_isoc99 && !in_system_header)
4705 pedwarn (input_location, OPT_pedantic,
4706 "ISO C90 does not support flexible array members");
4708 /* ISO C99 Flexible array members are effectively
4709 identical to GCC's zero-length array extension. */
4710 if (flexible_array_member || array_parm_vla_unspec_p)
4711 itype = build_range_type (sizetype, size_zero_node,
4714 else if (decl_context == PARM)
4716 if (array_parm_vla_unspec_p)
4718 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4722 else if (decl_context == TYPENAME)
4724 if (array_parm_vla_unspec_p)
4727 warning (0, "%<[*]%> not in a declaration");
4728 /* We use this to avoid messing up with incomplete
4729 array types of the same type, that would
4730 otherwise be modified below. */
4731 itype = build_range_type (sizetype, size_zero_node,
4737 /* Complain about arrays of incomplete types. */
4738 if (!COMPLETE_TYPE_P (type))
4740 error ("array type has incomplete element type");
4741 type = error_mark_node;
4744 /* When itype is NULL, a shared incomplete array type is
4745 returned for all array of a given type. Elsewhere we
4746 make sure we don't complete that type before copying
4747 it, but here we want to make sure we don't ever
4748 modify the shared type, so we gcc_assert (itype)
4750 type = build_array_type (type, itype);
4752 if (type != error_mark_node)
4756 /* It is ok to modify type here even if itype is
4757 NULL: if size_varies, we're in a
4758 multi-dimensional array and the inner type has
4759 variable size, so the enclosing shared array type
4761 if (size && TREE_CODE (size) == INTEGER_CST)
4763 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4764 C_TYPE_VARIABLE_SIZE (type) = 1;
4767 /* The GCC extension for zero-length arrays differs from
4768 ISO flexible array members in that sizeof yields
4770 if (size && integer_zerop (size))
4773 TYPE_SIZE (type) = bitsize_zero_node;
4774 TYPE_SIZE_UNIT (type) = size_zero_node;
4776 if (array_parm_vla_unspec_p)
4779 /* The type is complete. C99 6.7.5.2p4 */
4780 TYPE_SIZE (type) = bitsize_zero_node;
4781 TYPE_SIZE_UNIT (type) = size_zero_node;
4785 if (decl_context != PARM
4786 && (array_ptr_quals != TYPE_UNQUALIFIED
4787 || array_ptr_attrs != NULL_TREE
4788 || array_parm_static))
4790 error ("static or type qualifiers in non-parameter array declarator");
4791 array_ptr_quals = TYPE_UNQUALIFIED;
4792 array_ptr_attrs = NULL_TREE;
4793 array_parm_static = 0;
4799 /* Say it's a definition only for the declarator closest
4800 to the identifier, apart possibly from some
4802 bool really_funcdef = false;
4806 const struct c_declarator *t = declarator->declarator;
4807 while (t->kind == cdk_attrs)
4809 really_funcdef = (t->kind == cdk_id);
4812 /* Declaring a function type. Make sure we have a valid
4813 type for the function to return. */
4814 if (type == error_mark_node)
4819 /* Warn about some types functions can't return. */
4820 if (TREE_CODE (type) == FUNCTION_TYPE)
4823 error ("%qE declared as function returning a function",
4826 error ("type name declared as function "
4827 "returning a function");
4828 type = integer_type_node;
4830 if (TREE_CODE (type) == ARRAY_TYPE)
4833 error ("%qE declared as function returning an array", name);
4835 error ("type name declared as function returning an array");
4836 type = integer_type_node;
4839 /* Construct the function type and go to the next
4840 inner layer of declarator. */
4841 arg_info = declarator->u.arg_info;
4842 arg_types = grokparms (arg_info, really_funcdef);
4844 put_pending_sizes (arg_info->pending_sizes);
4846 /* Type qualifiers before the return type of the function
4847 qualify the return type, not the function type. */
4850 /* Type qualifiers on a function return type are
4851 normally permitted by the standard but have no
4852 effect, so give a warning at -Wreturn-type.
4853 Qualifiers on a void return type are banned on
4854 function definitions in ISO C; GCC used to used
4855 them for noreturn functions. */
4856 if (VOID_TYPE_P (type) && really_funcdef)
4857 pedwarn (input_location, 0,
4858 "function definition has qualified void return type");
4860 warning (OPT_Wignored_qualifiers,
4861 "type qualifiers ignored on function return type");
4863 type = c_build_qualified_type (type, type_quals);
4865 type_quals = TYPE_UNQUALIFIED;
4867 type = build_function_type (type, arg_types);
4868 declarator = declarator->declarator;
4870 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4871 the formal parameter list of this FUNCTION_TYPE to point to
4872 the FUNCTION_TYPE node itself. */
4876 for (link = arg_info->tags;
4878 link = TREE_CHAIN (link))
4879 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4885 /* Merge any constancy or volatility into the target type
4888 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4890 pedwarn (input_location, OPT_pedantic,
4891 "ISO C forbids qualified function types");
4893 type = c_build_qualified_type (type, type_quals);
4896 /* When the pointed-to type involves components of variable size,
4897 care must be taken to ensure that the size evaluation code is
4898 emitted early enough to dominate all the possible later uses
4899 and late enough for the variables on which it depends to have
4902 This is expected to happen automatically when the pointed-to
4903 type has a name/declaration of it's own, but special attention
4904 is required if the type is anonymous.
4906 We handle the NORMAL and FIELD contexts here by attaching an
4907 artificial TYPE_DECL to such pointed-to type. This forces the
4908 sizes evaluation at a safe point and ensures it is not deferred
4909 until e.g. within a deeper conditional context.
4911 We expect nothing to be needed here for PARM or TYPENAME.
4912 Pushing a TYPE_DECL at this point for TYPENAME would actually
4913 be incorrect, as we might be in the middle of an expression
4914 with side effects on the pointed-to type size "arguments" prior
4915 to the pointer declaration point and the fake TYPE_DECL in the
4916 enclosing context would force the size evaluation prior to the
4919 if (!TYPE_NAME (type)
4920 && (decl_context == NORMAL || decl_context == FIELD)
4921 && variably_modified_type_p (type, NULL_TREE))
4923 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4924 DECL_ARTIFICIAL (decl) = 1;
4926 finish_decl (decl, NULL_TREE, NULL_TREE, NULL_TREE);
4927 TYPE_NAME (type) = decl;
4930 type = build_pointer_type (type);
4932 /* Process type qualifiers (such as const or volatile)
4933 that were given inside the `*'. */
4934 type_quals = declarator->u.pointer_quals;
4936 declarator = declarator->declarator;
4943 *decl_attrs = chainon (returned_attrs, *decl_attrs);
4945 /* Now TYPE has the actual type, apart from any qualifiers in
4948 /* Check the type and width of a bit-field. */
4950 check_bitfield_type_and_width (&type, width, name);
4952 /* Did array size calculations overflow? */
4954 if (TREE_CODE (type) == ARRAY_TYPE
4955 && COMPLETE_TYPE_P (type)
4956 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4957 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4960 error ("size of array %qE is too large", name);
4962 error ("size of unnamed array is too large");
4963 /* If we proceed with the array type as it is, we'll eventually
4964 crash in tree_low_cst(). */
4965 type = error_mark_node;
4968 /* If this is declaring a typedef name, return a TYPE_DECL. */
4970 if (storage_class == csc_typedef)
4973 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4975 pedwarn (input_location, OPT_pedantic,
4976 "ISO C forbids qualified function types");
4978 type = c_build_qualified_type (type, type_quals);
4979 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4980 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4981 if (declspecs->explicit_signed_p)
4982 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4983 if (declspecs->inline_p)
4984 pedwarn (input_location, 0,"typedef %q+D declared %<inline%>", decl);
4986 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
4988 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
4991 && b->decl != NULL_TREE
4992 && (B_IN_CURRENT_SCOPE (b)
4993 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
4994 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
4996 warning_at (declarator->id_loc, OPT_Wc___compat,
4997 ("using %qD as both a typedef and a tag is "
5000 if (b->locus != UNKNOWN_LOCATION)
5001 inform (b->locus, "originally defined here");
5008 /* If this is a type name (such as, in a cast or sizeof),
5009 compute the type and return it now. */
5011 if (decl_context == TYPENAME)
5013 /* Note that the grammar rejects storage classes in typenames
5015 gcc_assert (storage_class == csc_none && !threadp
5016 && !declspecs->inline_p);
5017 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5019 pedwarn (input_location, OPT_pedantic,
5020 "ISO C forbids const or volatile function types");
5022 type = c_build_qualified_type (type, type_quals);
5026 if (pedantic && decl_context == FIELD
5027 && variably_modified_type_p (type, NULL_TREE))
5030 pedwarn (input_location, OPT_pedantic,
5031 "a member of a structure or union cannot have a variably modified type");
5034 /* Aside from typedefs and type names (handle above),
5035 `void' at top level (not within pointer)
5036 is allowed only in public variables.
5037 We don't complain about parms either, but that is because
5038 a better error message can be made later. */
5040 if (VOID_TYPE_P (type) && decl_context != PARM
5041 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
5042 && (storage_class == csc_extern
5043 || (current_scope == file_scope
5044 && !(storage_class == csc_static
5045 || storage_class == csc_register)))))
5047 error ("variable or field %qE declared void", name);
5048 type = integer_type_node;
5051 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
5052 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
5057 if (decl_context == PARM)
5061 /* A parameter declared as an array of T is really a pointer to T.
5062 One declared as a function is really a pointer to a function. */
5064 if (TREE_CODE (type) == ARRAY_TYPE)
5066 /* Transfer const-ness of array into that of type pointed to. */
5067 type = TREE_TYPE (type);
5069 type = c_build_qualified_type (type, type_quals);
5070 type = build_pointer_type (type);
5071 type_quals = array_ptr_quals;
5073 type = c_build_qualified_type (type, type_quals);
5075 /* We don't yet implement attributes in this context. */
5076 if (array_ptr_attrs != NULL_TREE)
5077 warning (OPT_Wattributes,
5078 "attributes in parameter array declarator ignored");
5082 else if (TREE_CODE (type) == FUNCTION_TYPE)
5085 pedwarn (input_location, OPT_pedantic,
5086 "ISO C forbids qualified function types");
5088 type = c_build_qualified_type (type, type_quals);
5089 type = build_pointer_type (type);
5090 type_quals = TYPE_UNQUALIFIED;
5092 else if (type_quals)
5093 type = c_build_qualified_type (type, type_quals);
5095 decl = build_decl (PARM_DECL, declarator->u.id, type);
5096 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5098 C_DECL_VARIABLE_SIZE (decl) = 1;
5100 /* Compute the type actually passed in the parmlist,
5101 for the case where there is no prototype.
5102 (For example, shorts and chars are passed as ints.)
5103 When there is a prototype, this is overridden later. */
5105 if (type == error_mark_node)
5106 promoted_type = type;
5108 promoted_type = c_type_promotes_to (type);
5110 DECL_ARG_TYPE (decl) = promoted_type;
5111 if (declspecs->inline_p)
5112 pedwarn (input_location, 0, "parameter %q+D declared %<inline%>", decl);
5114 else if (decl_context == FIELD)
5116 /* Note that the grammar rejects storage classes in typenames
5118 gcc_assert (storage_class == csc_none && !threadp
5119 && !declspecs->inline_p);
5121 /* Structure field. It may not be a function. */
5123 if (TREE_CODE (type) == FUNCTION_TYPE)
5125 error ("field %qE declared as a function", name);
5126 type = build_pointer_type (type);
5128 else if (TREE_CODE (type) != ERROR_MARK
5129 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
5132 error ("field %qE has incomplete type", name);
5134 error ("unnamed field has incomplete type");
5135 type = error_mark_node;
5137 type = c_build_qualified_type (type, type_quals);
5138 decl = build_decl (FIELD_DECL, declarator->u.id, type);
5139 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5140 DECL_NONADDRESSABLE_P (decl) = bitfield;
5141 if (bitfield && !declarator->u.id)
5142 TREE_NO_WARNING (decl) = 1;
5145 C_DECL_VARIABLE_SIZE (decl) = 1;
5147 else if (TREE_CODE (type) == FUNCTION_TYPE)
5149 if (storage_class == csc_register || threadp)
5151 error ("invalid storage class for function %qE", name);
5153 else if (current_scope != file_scope)
5155 /* Function declaration not at file scope. Storage
5156 classes other than `extern' are not allowed, C99
5157 6.7.1p5, and `extern' makes no difference. However,
5158 GCC allows 'auto', perhaps with 'inline', to support
5159 nested functions. */
5160 if (storage_class == csc_auto)
5161 pedwarn (input_location, OPT_pedantic,
5162 "invalid storage class for function %qE", name);
5163 else if (storage_class == csc_static)
5165 error ("invalid storage class for function %qE", name);
5167 storage_class = declspecs->storage_class = csc_none;
5173 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
5174 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5175 decl = build_decl_attribute_variant (decl, decl_attr);
5177 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
5178 pedwarn (input_location, OPT_pedantic,
5179 "ISO C forbids qualified function types");
5181 /* GNU C interprets a volatile-qualified function type to indicate
5182 that the function does not return. */
5183 if ((type_quals & TYPE_QUAL_VOLATILE)
5184 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5185 warning (0, "%<noreturn%> function returns non-void value");
5187 /* Every function declaration is an external reference
5188 (DECL_EXTERNAL) except for those which are not at file
5189 scope and are explicitly declared "auto". This is
5190 forbidden by standard C (C99 6.7.1p5) and is interpreted by
5191 GCC to signify a forward declaration of a nested function. */
5192 if (storage_class == csc_auto && current_scope != file_scope)
5193 DECL_EXTERNAL (decl) = 0;
5194 /* In C99, a function which is declared 'inline' with 'extern'
5195 is not an external reference (which is confusing). It
5196 means that the later definition of the function must be output
5197 in this file, C99 6.7.4p6. In GNU C89, a function declared
5198 'extern inline' is an external reference. */
5199 else if (declspecs->inline_p && storage_class != csc_static)
5200 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
5201 == flag_gnu89_inline);
5203 DECL_EXTERNAL (decl) = !initialized;
5205 /* Record absence of global scope for `static' or `auto'. */
5207 = !(storage_class == csc_static || storage_class == csc_auto);
5209 /* For a function definition, record the argument information
5210 block where store_parm_decls will look for it. */
5212 current_function_arg_info = arg_info;
5214 if (declspecs->default_int_p)
5215 C_FUNCTION_IMPLICIT_INT (decl) = 1;
5217 /* Record presence of `inline', if it is reasonable. */
5218 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
5220 if (declspecs->inline_p)
5221 pedwarn (input_location, 0, "cannot inline function %<main%>");
5223 else if (declspecs->inline_p)
5224 /* Record that the function is declared `inline'. */
5225 DECL_DECLARED_INLINE_P (decl) = 1;
5229 /* It's a variable. */
5230 /* An uninitialized decl with `extern' is a reference. */
5231 int extern_ref = !initialized && storage_class == csc_extern;
5233 type = c_build_qualified_type (type, type_quals);
5235 /* C99 6.2.2p7: It is invalid (compile-time undefined
5236 behavior) to create an 'extern' declaration for a
5237 variable if there is a global declaration that is
5238 'static' and the global declaration is not visible.
5239 (If the static declaration _is_ currently visible,
5240 the 'extern' declaration is taken to refer to that decl.) */
5241 if (extern_ref && current_scope != file_scope)
5243 tree global_decl = identifier_global_value (declarator->u.id);
5244 tree visible_decl = lookup_name (declarator->u.id);
5247 && global_decl != visible_decl
5248 && TREE_CODE (global_decl) == VAR_DECL
5249 && !TREE_PUBLIC (global_decl))
5250 error ("variable previously declared %<static%> redeclared "
5254 decl = build_decl (VAR_DECL, declarator->u.id, type);
5255 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5257 C_DECL_VARIABLE_SIZE (decl) = 1;
5259 if (declspecs->inline_p)
5260 pedwarn (input_location, 0, "variable %q+D declared %<inline%>", decl);
5262 /* At file scope, an initialized extern declaration may follow
5263 a static declaration. In that case, DECL_EXTERNAL will be
5264 reset later in start_decl. */
5265 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
5267 /* At file scope, the presence of a `static' or `register' storage
5268 class specifier, or the absence of all storage class specifiers
5269 makes this declaration a definition (perhaps tentative). Also,
5270 the absence of `static' makes it public. */
5271 if (current_scope == file_scope)
5273 TREE_PUBLIC (decl) = storage_class != csc_static;
5274 TREE_STATIC (decl) = !extern_ref;
5276 /* Not at file scope, only `static' makes a static definition. */
5279 TREE_STATIC (decl) = (storage_class == csc_static);
5280 TREE_PUBLIC (decl) = extern_ref;
5284 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
5287 if ((storage_class == csc_extern
5288 || (storage_class == csc_none
5289 && TREE_CODE (type) == FUNCTION_TYPE
5291 && variably_modified_type_p (type, NULL_TREE))
5294 if (TREE_CODE (type) == FUNCTION_TYPE)
5295 error ("non-nested function with variably modified type");
5297 error ("object with variably modified type must have no linkage");
5300 /* Record `register' declaration for warnings on &
5301 and in case doing stupid register allocation. */
5303 if (storage_class == csc_register)
5305 C_DECL_REGISTER (decl) = 1;
5306 DECL_REGISTER (decl) = 1;
5309 /* Record constancy and volatility. */
5310 c_apply_type_quals_to_decl (type_quals, decl);
5312 /* If a type has volatile components, it should be stored in memory.
5313 Otherwise, the fact that those components are volatile
5314 will be ignored, and would even crash the compiler.
5315 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
5316 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
5317 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
5318 || TREE_CODE (decl) == RESULT_DECL))
5320 /* It is not an error for a structure with volatile fields to
5321 be declared register, but reset DECL_REGISTER since it
5322 cannot actually go in a register. */
5323 int was_reg = C_DECL_REGISTER (decl);
5324 C_DECL_REGISTER (decl) = 0;
5325 DECL_REGISTER (decl) = 0;
5326 c_mark_addressable (decl);
5327 C_DECL_REGISTER (decl) = was_reg;
5330 /* This is the earliest point at which we might know the assembler
5331 name of a variable. Thus, if it's known before this, die horribly. */
5332 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
5338 /* Decode the parameter-list info for a function type or function definition.
5339 The argument is the value returned by `get_parm_info' (or made in c-parse.c
5340 if there is an identifier list instead of a parameter decl list).
5341 These two functions are separate because when a function returns
5342 or receives functions then each is called multiple times but the order
5343 of calls is different. The last call to `grokparms' is always the one
5344 that contains the formal parameter names of a function definition.
5346 Return a list of arg types to use in the FUNCTION_TYPE for this function.
5348 FUNCDEF_FLAG is true for a function definition, false for
5349 a mere declaration. A nonempty identifier-list gets an error message
5350 when FUNCDEF_FLAG is false. */
5353 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
5355 tree arg_types = arg_info->types;
5357 if (funcdef_flag && arg_info->had_vla_unspec)
5359 /* A function definition isn't function prototype scope C99 6.2.1p4. */
5361 error ("%<[*]%> not allowed in other than function prototype scope");
5364 if (arg_types == 0 && !funcdef_flag && !in_system_header)
5365 warning (OPT_Wstrict_prototypes,
5366 "function declaration isn%'t a prototype");
5368 if (arg_types == error_mark_node)
5369 return 0; /* don't set TYPE_ARG_TYPES in this case */
5371 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5374 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
5376 arg_info->parms = arg_info->types;
5377 arg_info->types = 0;
5382 tree parm, type, typelt;
5383 unsigned int parmno;
5385 /* If there is a parameter of incomplete type in a definition,
5386 this is an error. In a declaration this is valid, and a
5387 struct or union type may be completed later, before any calls
5388 or definition of the function. In the case where the tag was
5389 first declared within the parameter list, a warning has
5390 already been given. If a parameter has void type, then
5391 however the function cannot be defined or called, so
5394 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5396 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5398 type = TREE_VALUE (typelt);
5399 if (type == error_mark_node)
5402 if (!COMPLETE_TYPE_P (type))
5406 if (DECL_NAME (parm))
5407 error ("parameter %u (%q+D) has incomplete type",
5410 error ("%Jparameter %u has incomplete type",
5413 TREE_VALUE (typelt) = error_mark_node;
5414 TREE_TYPE (parm) = error_mark_node;
5416 else if (VOID_TYPE_P (type))
5418 if (DECL_NAME (parm))
5419 warning (0, "parameter %u (%q+D) has void type",
5422 warning (0, "%Jparameter %u has void type",
5427 if (DECL_NAME (parm) && TREE_USED (parm))
5428 warn_if_shadowing (parm);
5434 /* Take apart the current scope and return a c_arg_info structure with
5435 info on a parameter list just parsed.
5437 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5439 ELLIPSIS being true means the argument list ended in '...' so don't
5440 append a sentinel (void_list_node) to the end of the type-list. */
5443 get_parm_info (bool ellipsis)
5445 struct c_binding *b = current_scope->bindings;
5446 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5453 static bool explained_incomplete_types = false;
5454 bool gave_void_only_once_err = false;
5456 arg_info->parms = 0;
5458 arg_info->types = 0;
5459 arg_info->others = 0;
5460 arg_info->pending_sizes = 0;
5461 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5463 /* The bindings in this scope must not get put into a block.
5464 We will take care of deleting the binding nodes. */
5465 current_scope->bindings = 0;
5467 /* This function is only called if there was *something* on the
5471 /* A parameter list consisting solely of 'void' indicates that the
5472 function takes no arguments. But if the 'void' is qualified
5473 (by 'const' or 'volatile'), or has a storage class specifier
5474 ('register'), then the behavior is undefined; issue an error.
5475 Typedefs for 'void' are OK (see DR#157). */
5476 if (b->prev == 0 /* one binding */
5477 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5478 && !DECL_NAME (b->decl) /* anonymous */
5479 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5481 if (TREE_THIS_VOLATILE (b->decl)
5482 || TREE_READONLY (b->decl)
5483 || C_DECL_REGISTER (b->decl))
5484 error ("%<void%> as only parameter may not be qualified");
5486 /* There cannot be an ellipsis. */
5488 error ("%<void%> must be the only parameter");
5490 arg_info->types = void_list_node;
5495 types = void_list_node;
5497 /* Break up the bindings list into parms, tags, types, and others;
5498 apply sanity checks; purge the name-to-decl bindings. */
5501 tree decl = b->decl;
5502 tree type = TREE_TYPE (decl);
5503 const char *keyword;
5505 switch (TREE_CODE (decl))
5510 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5511 I_SYMBOL_BINDING (b->id) = b->shadowed;
5514 /* Check for forward decls that never got their actual decl. */
5515 if (TREE_ASM_WRITTEN (decl))
5516 error ("parameter %q+D has just a forward declaration", decl);
5517 /* Check for (..., void, ...) and issue an error. */
5518 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5520 if (!gave_void_only_once_err)
5522 error ("%<void%> must be the only parameter");
5523 gave_void_only_once_err = true;
5528 /* Valid parameter, add it to the list. */
5529 TREE_CHAIN (decl) = parms;
5532 /* Since there is a prototype, args are passed in their
5533 declared types. The back end may override this later. */
5534 DECL_ARG_TYPE (decl) = type;
5535 types = tree_cons (0, type, types);
5539 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5540 case UNION_TYPE: keyword = "union"; goto tag;
5541 case RECORD_TYPE: keyword = "struct"; goto tag;
5543 /* Types may not have tag-names, in which case the type
5544 appears in the bindings list with b->id NULL. */
5547 gcc_assert (I_TAG_BINDING (b->id) == b);
5548 I_TAG_BINDING (b->id) = b->shadowed;
5551 /* Warn about any struct, union or enum tags defined in a
5552 parameter list. The scope of such types is limited to
5553 the parameter list, which is rarely if ever desirable
5554 (it's impossible to call such a function with type-
5555 correct arguments). An anonymous union parm type is
5556 meaningful as a GNU extension, so don't warn for that. */
5557 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5560 /* The %s will be one of 'struct', 'union', or 'enum'. */
5561 warning (0, "%<%s %E%> declared inside parameter list",
5564 /* The %s will be one of 'struct', 'union', or 'enum'. */
5565 warning (0, "anonymous %s declared inside parameter list",
5568 if (!explained_incomplete_types)
5570 warning (0, "its scope is only this definition or declaration,"
5571 " which is probably not what you want");
5572 explained_incomplete_types = true;
5576 tags = tree_cons (b->id, decl, tags);
5582 /* CONST_DECLs appear here when we have an embedded enum,
5583 and TYPE_DECLs appear here when we have an embedded struct
5584 or union. No warnings for this - we already warned about the
5585 type itself. FUNCTION_DECLs appear when there is an implicit
5586 function declaration in the parameter list. */
5588 TREE_CHAIN (decl) = others;
5593 /* error_mark_node appears here when we have an undeclared
5594 variable. Just throw it away. */
5597 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5598 I_SYMBOL_BINDING (b->id) = b->shadowed;
5602 /* Other things that might be encountered. */
5609 b = free_binding_and_advance (b);
5612 arg_info->parms = parms;
5613 arg_info->tags = tags;
5614 arg_info->types = types;
5615 arg_info->others = others;
5616 arg_info->pending_sizes = get_pending_sizes ();
5620 /* Get the struct, enum or union (CODE says which) with tag NAME.
5621 Define the tag as a forward-reference if it is not defined.
5622 Return a c_typespec structure for the type specifier. */
5625 parser_xref_tag (enum tree_code code, tree name, location_t loc)
5627 struct c_typespec ret;
5631 ret.expr = NULL_TREE;
5632 ret.expr_const_operands = true;
5634 /* If a cross reference is requested, look up the type
5635 already defined for this tag and return it. */
5637 ref = lookup_tag (code, name, 0, &refloc);
5638 /* If this is the right type of tag, return what we found.
5639 (This reference will be shadowed by shadow_tag later if appropriate.)
5640 If this is the wrong type of tag, do not return it. If it was the
5641 wrong type in the same scope, we will have had an error
5642 message already; if in a different scope and declaring
5643 a name, pending_xref_error will give an error message; but if in a
5644 different scope and not declaring a name, this tag should
5645 shadow the previous declaration of a different type of tag, and
5646 this would not work properly if we return the reference found.
5647 (For example, with "struct foo" in an outer scope, "union foo;"
5648 must shadow that tag with a new one of union type.) */
5649 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5650 if (ref && TREE_CODE (ref) == code)
5652 if (C_TYPE_DEFINED_IN_STRUCT (ref)
5653 && loc != UNKNOWN_LOCATION
5659 warning_at (loc, OPT_Wc___compat,
5660 ("enum type defined in struct or union "
5661 "is not visible in C++"));
5662 inform (refloc, "enum type defined here");
5665 warning_at (loc, OPT_Wc___compat,
5666 ("struct defined in struct or union "
5667 "is not visible in C++"));
5668 inform (refloc, "struct defined here");
5671 warning_at (loc, OPT_Wc___compat,
5672 ("union defined in struct or union "
5673 "is not visible in C++"));
5674 inform (refloc, "union defined here");
5685 /* If no such tag is yet defined, create a forward-reference node
5686 and record it as the "definition".
5687 When a real declaration of this type is found,
5688 the forward-reference will be altered into a real type. */
5690 ref = make_node (code);
5691 if (code == ENUMERAL_TYPE)
5693 /* Give the type a default layout like unsigned int
5694 to avoid crashing if it does not get defined. */
5695 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
5696 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5697 TYPE_USER_ALIGN (ref) = 0;
5698 TYPE_UNSIGNED (ref) = 1;
5699 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5700 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5701 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5704 pushtag (name, ref, loc);
5710 /* Get the struct, enum or union (CODE says which) with tag NAME.
5711 Define the tag as a forward-reference if it is not defined.
5712 Return a tree for the type. */
5715 xref_tag (enum tree_code code, tree name)
5717 return parser_xref_tag (code, name, UNKNOWN_LOCATION).spec;
5720 /* Make sure that the tag NAME is defined *in the current scope*
5721 at least as a forward reference.
5722 CODE says which kind of tag NAME ought to be.
5724 This stores the current value of the file static IN_STRUCT in
5725 *ENCLOSING_IN_STRUCT, and sets IN_STRUCT to true. Similarly, this
5726 sets STRUCT_TYPES in *ENCLOSING_STRUCT_TYPES, and sets STRUCT_TYPES
5727 to an empty vector. The old values are restored in
5731 start_struct (enum tree_code code, tree name, bool *enclosing_in_struct,
5732 VEC(tree,heap) **enclosing_struct_types, location_t loc)
5734 /* If there is already a tag defined at this scope
5735 (as a forward reference), just return it. */
5737 tree ref = NULL_TREE;
5738 location_t refloc = UNKNOWN_LOCATION;
5740 if (name != NULL_TREE)
5741 ref = lookup_tag (code, name, 1, &refloc);
5742 if (ref && TREE_CODE (ref) == code)
5744 if (TYPE_SIZE (ref))
5746 if (code == UNION_TYPE)
5747 error_at (loc, "redefinition of %<union %E%>", name);
5749 error_at (loc, "redefinition of %<struct %E%>", name);
5750 if (refloc != UNKNOWN_LOCATION)
5751 inform (refloc, "originally defined here");
5752 /* Don't create structures using a name already in use. */
5755 else if (C_TYPE_BEING_DEFINED (ref))
5757 if (code == UNION_TYPE)
5758 error_at (loc, "nested redefinition of %<union %E%>", name);
5760 error_at (loc, "nested redefinition of %<struct %E%>", name);
5761 /* Don't bother to report "originally defined here" for a
5762 nested redefinition; the original definition should be
5764 /* Don't create structures that contain themselves. */
5769 /* Otherwise create a forward-reference just so the tag is in scope. */
5771 if (ref == NULL_TREE || TREE_CODE (ref) != code)
5773 ref = make_node (code);
5774 pushtag (name, ref, loc);
5777 C_TYPE_BEING_DEFINED (ref) = 1;
5778 TYPE_PACKED (ref) = flag_pack_struct;
5780 *enclosing_in_struct = in_struct;
5781 *enclosing_struct_types = struct_types;
5783 struct_types = VEC_alloc(tree, heap, 0);
5785 /* FIXME: This will issue a warning for a use of a type defined
5786 within a statement expr used within sizeof, et. al. This is not
5787 terribly serious as C++ doesn't permit statement exprs within
5789 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
5790 warning_at (loc, OPT_Wc___compat,
5791 "defining type in %qs expression is invalid in C++",
5794 : (in_typeof ? "typeof" : "alignof")));
5799 /* Process the specs, declarator and width (NULL if omitted)
5800 of a structure component, returning a FIELD_DECL node.
5801 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5802 DECL_ATTRS is as for grokdeclarator.
5804 LOC is the location of the structure component.
5806 This is done during the parsing of the struct declaration.
5807 The FIELD_DECL nodes are chained together and the lot of them
5808 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5811 grokfield (location_t loc,
5812 struct c_declarator *declarator, struct c_declspecs *declspecs,
5813 tree width, tree *decl_attrs)
5817 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5818 && width == NULL_TREE)
5820 /* This is an unnamed decl.
5822 If we have something of the form "union { list } ;" then this
5823 is the anonymous union extension. Similarly for struct.
5825 If this is something of the form "struct foo;", then
5826 If MS extensions are enabled, this is handled as an
5828 Otherwise this is a forward declaration of a structure tag.
5830 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5831 If MS extensions are enabled and foo names a structure, then
5832 again this is an anonymous struct.
5833 Otherwise this is an error.
5835 Oh what a horrid tangled web we weave. I wonder if MS consciously
5836 took this from Plan 9 or if it was an accident of implementation
5837 that took root before someone noticed the bug... */
5839 tree type = declspecs->type;
5840 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5841 || TREE_CODE (type) == UNION_TYPE);
5845 && (flag_ms_extensions || !declspecs->typedef_p))
5847 if (flag_ms_extensions)
5851 else if (TYPE_NAME (type) == NULL)
5858 pedwarn (loc, 0, "declaration does not declare anything");
5861 pedwarn (loc, OPT_pedantic, "ISO C doesn%'t support unnamed structs/unions");
5864 value = grokdeclarator (declarator, declspecs, FIELD, false,
5865 width ? &width : NULL, decl_attrs, NULL, NULL,
5868 finish_decl (value, NULL_TREE, NULL_TREE, NULL_TREE);
5869 DECL_INITIAL (value) = width;
5874 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5875 the list such that this does not present a problem later. */
5878 detect_field_duplicates (tree fieldlist)
5883 /* First, see if there are more than "a few" fields.
5884 This is trivially true if there are zero or one fields. */
5887 x = TREE_CHAIN (fieldlist);
5893 } while (timeout > 0 && x);
5895 /* If there were "few" fields, avoid the overhead of allocating
5896 a hash table. Instead just do the nested traversal thing. */
5899 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5902 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5903 if (DECL_NAME (y) == DECL_NAME (x))
5905 error ("duplicate member %q+D", x);
5906 DECL_NAME (x) = NULL_TREE;
5912 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5915 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5916 if ((y = DECL_NAME (x)) != 0)
5918 slot = htab_find_slot (htab, y, INSERT);
5921 error ("duplicate member %q+D", x);
5922 DECL_NAME (x) = NULL_TREE;
5931 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5932 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5933 ATTRIBUTES are attributes to be applied to the structure.
5935 ENCLOSING_IN_STRUCT is the value of IN_STRUCT, and
5936 ENCLOSING_STRUCT_TYPES is the value of STRUCT_TYPES, when the
5937 struct was started. This sets the C_TYPE_DEFINED_IN_STRUCT flag
5938 for any type defined in the current struct. */
5941 finish_struct (tree t, tree fieldlist, tree attributes,
5942 bool enclosing_in_struct,
5943 VEC(tree,heap) *enclosing_struct_types)
5946 bool toplevel = file_scope == current_scope;
5947 int saw_named_field;
5950 /* If this type was previously laid out as a forward reference,
5951 make sure we lay it out again. */
5955 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5959 for (x = fieldlist; x; x = TREE_CHAIN (x))
5960 if (DECL_NAME (x) != 0)
5965 if (TREE_CODE (t) == UNION_TYPE)
5968 pedwarn (input_location, OPT_pedantic, "union has no named members");
5970 pedwarn (input_location, OPT_pedantic, "union has no members");
5975 pedwarn (input_location, OPT_pedantic, "struct has no named members");
5977 pedwarn (input_location, OPT_pedantic, "struct has no members");
5982 /* Install struct as DECL_CONTEXT of each field decl.
5983 Also process specified field sizes, found in the DECL_INITIAL,
5984 storing 0 there after the type has been changed to precision equal
5985 to its width, rather than the precision of the specified standard
5986 type. (Correct layout requires the original type to have been preserved
5989 saw_named_field = 0;
5990 for (x = fieldlist; x; x = TREE_CHAIN (x))
5992 if (TREE_TYPE (x) == error_mark_node)
5995 DECL_CONTEXT (x) = t;
5997 /* If any field is const, the structure type is pseudo-const. */
5998 if (TREE_READONLY (x))
5999 C_TYPE_FIELDS_READONLY (t) = 1;
6002 /* A field that is pseudo-const makes the structure likewise. */
6003 tree t1 = TREE_TYPE (x);
6004 while (TREE_CODE (t1) == ARRAY_TYPE)
6005 t1 = TREE_TYPE (t1);
6006 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
6007 && C_TYPE_FIELDS_READONLY (t1))
6008 C_TYPE_FIELDS_READONLY (t) = 1;
6011 /* Any field that is volatile means variables of this type must be
6012 treated in some ways as volatile. */
6013 if (TREE_THIS_VOLATILE (x))
6014 C_TYPE_FIELDS_VOLATILE (t) = 1;
6016 /* Any field of nominal variable size implies structure is too. */
6017 if (C_DECL_VARIABLE_SIZE (x))
6018 C_TYPE_VARIABLE_SIZE (t) = 1;
6020 if (DECL_INITIAL (x))
6022 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
6023 DECL_SIZE (x) = bitsize_int (width);
6024 DECL_BIT_FIELD (x) = 1;
6025 SET_DECL_C_BIT_FIELD (x);
6029 && (DECL_BIT_FIELD (x)
6030 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
6031 DECL_PACKED (x) = 1;
6033 /* Detect flexible array member in an invalid context. */
6034 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6035 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
6036 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
6037 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
6039 if (TREE_CODE (t) == UNION_TYPE)
6041 error ("%Jflexible array member in union", x);
6042 TREE_TYPE (x) = error_mark_node;
6044 else if (TREE_CHAIN (x) != NULL_TREE)
6046 error ("%Jflexible array member not at end of struct", x);
6047 TREE_TYPE (x) = error_mark_node;
6049 else if (!saw_named_field)
6051 error ("%Jflexible array member in otherwise empty struct", x);
6052 TREE_TYPE (x) = error_mark_node;
6056 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
6057 && flexible_array_type_p (TREE_TYPE (x)))
6058 pedwarn (input_location, OPT_pedantic,
6059 "%Jinvalid use of structure with flexible array member", x);
6062 saw_named_field = 1;
6065 detect_field_duplicates (fieldlist);
6067 /* Now we have the nearly final fieldlist. Record it,
6068 then lay out the structure or union (including the fields). */
6070 TYPE_FIELDS (t) = fieldlist;
6074 /* Give bit-fields their proper types. */
6076 tree *fieldlistp = &fieldlist;
6078 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
6079 && TREE_TYPE (*fieldlistp) != error_mark_node)
6081 unsigned HOST_WIDE_INT width
6082 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
6083 tree type = TREE_TYPE (*fieldlistp);
6084 if (width != TYPE_PRECISION (type))
6086 TREE_TYPE (*fieldlistp)
6087 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
6088 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
6090 DECL_INITIAL (*fieldlistp) = 0;
6093 fieldlistp = &TREE_CHAIN (*fieldlistp);
6096 /* Now we have the truly final field list.
6097 Store it in this type and in the variants. */
6099 TYPE_FIELDS (t) = fieldlist;
6101 /* If there are lots of fields, sort so we can look through them fast.
6102 We arbitrarily consider 16 or more elts to be "a lot". */
6107 for (x = fieldlist; x; x = TREE_CHAIN (x))
6109 if (len > 15 || DECL_NAME (x) == NULL)
6117 struct lang_type *space;
6118 struct sorted_fields_type *space2;
6120 len += list_length (x);
6122 /* Use the same allocation policy here that make_node uses, to
6123 ensure that this lives as long as the rest of the struct decl.
6124 All decls in an inline function need to be saved. */
6126 space = GGC_CNEW (struct lang_type);
6127 space2 = GGC_NEWVAR (struct sorted_fields_type,
6128 sizeof (struct sorted_fields_type) + len * sizeof (tree));
6132 field_array = &space2->elts[0];
6133 for (x = fieldlist; x; x = TREE_CHAIN (x))
6135 field_array[len++] = x;
6137 /* If there is anonymous struct or union, break out of the loop. */
6138 if (DECL_NAME (x) == NULL)
6141 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
6144 TYPE_LANG_SPECIFIC (t) = space;
6145 TYPE_LANG_SPECIFIC (t)->s->len = len;
6146 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
6147 qsort (field_array, len, sizeof (tree), field_decl_cmp);
6152 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6154 TYPE_FIELDS (x) = TYPE_FIELDS (t);
6155 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
6156 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
6157 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
6158 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
6161 /* If this was supposed to be a transparent union, but we can't
6162 make it one, warn and turn off the flag. */
6163 if (TREE_CODE (t) == UNION_TYPE
6164 && TYPE_TRANSPARENT_UNION (t)
6165 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
6167 TYPE_TRANSPARENT_UNION (t) = 0;
6168 warning (0, "union cannot be made transparent");
6171 /* If this structure or union completes the type of any previous
6172 variable declaration, lay it out and output its rtl. */
6173 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
6177 tree decl = TREE_VALUE (x);
6178 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6179 layout_array_type (TREE_TYPE (decl));
6180 if (TREE_CODE (decl) != TYPE_DECL)
6182 layout_decl (decl, 0);
6183 if (c_dialect_objc ())
6184 objc_check_decl (decl);
6185 rest_of_decl_compilation (decl, toplevel, 0);
6190 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
6192 /* Finish debugging output for this type. */
6193 rest_of_type_compilation (t, toplevel);
6195 /* If we're inside a function proper, i.e. not file-scope and not still
6196 parsing parameters, then arrange for the size of a variable sized type
6198 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
6199 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
6201 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
6202 the current struct. We do this now at the end of the struct
6203 because the flag is used to issue visibility warnings when using
6204 -Wc++-compat, and we only want to issue those warnings if the
6205 type is referenced outside of the struct declaration. */
6206 for (ix = 0; VEC_iterate (tree, struct_types, ix, x); ++ix)
6207 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
6209 VEC_free (tree, heap, struct_types);
6211 in_struct = enclosing_in_struct;
6212 struct_types = enclosing_struct_types;
6214 /* If this struct is defined inside a struct, add it to
6216 if (in_struct && !in_sizeof && !in_typeof && !in_alignof)
6217 VEC_safe_push (tree, heap, struct_types, t);
6222 /* Lay out the type T, and its element type, and so on. */
6225 layout_array_type (tree t)
6227 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6228 layout_array_type (TREE_TYPE (t));
6232 /* Begin compiling the definition of an enumeration type.
6233 NAME is its name (or null if anonymous).
6234 Returns the type object, as yet incomplete.
6235 Also records info about it so that build_enumerator
6236 may be used to declare the individual values as they are read. */
6239 start_enum (struct c_enum_contents *the_enum, tree name, location_t loc)
6241 tree enumtype = NULL_TREE;
6242 location_t enumloc = UNKNOWN_LOCATION;
6244 /* If this is the real definition for a previous forward reference,
6245 fill in the contents in the same object that used to be the
6246 forward reference. */
6248 if (name != NULL_TREE)
6249 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1, &enumloc);
6251 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
6253 enumtype = make_node (ENUMERAL_TYPE);
6254 pushtag (name, enumtype, loc);
6257 if (C_TYPE_BEING_DEFINED (enumtype))
6258 error_at (loc, "nested redefinition of %<enum %E%>", name);
6260 C_TYPE_BEING_DEFINED (enumtype) = 1;
6262 if (TYPE_VALUES (enumtype) != 0)
6264 /* This enum is a named one that has been declared already. */
6265 error_at (loc, "redeclaration of %<enum %E%>", name);
6266 if (enumloc != UNKNOWN_LOCATION)
6267 inform (enumloc, "originally defined here");
6269 /* Completely replace its old definition.
6270 The old enumerators remain defined, however. */
6271 TYPE_VALUES (enumtype) = 0;
6274 the_enum->enum_next_value = integer_zero_node;
6275 the_enum->enum_overflow = 0;
6277 if (flag_short_enums)
6278 TYPE_PACKED (enumtype) = 1;
6280 /* FIXME: This will issue a warning for a use of a type defined
6281 within sizeof in a statement expr. This is not terribly serious
6282 as C++ doesn't permit statement exprs within sizeof anyhow. */
6283 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
6284 warning_at (loc, OPT_Wc___compat,
6285 "defining type in %qs expression is invalid in C++",
6288 : (in_typeof ? "typeof" : "alignof")));
6293 /* After processing and defining all the values of an enumeration type,
6294 install their decls in the enumeration type and finish it off.
6295 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
6296 and ATTRIBUTES are the specified attributes.
6297 Returns ENUMTYPE. */
6300 finish_enum (tree enumtype, tree values, tree attributes)
6303 tree minnode = 0, maxnode = 0;
6304 int precision, unsign;
6305 bool toplevel = (file_scope == current_scope);
6306 struct lang_type *lt;
6308 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6310 /* Calculate the maximum value of any enumerator in this type. */
6312 if (values == error_mark_node)
6313 minnode = maxnode = integer_zero_node;
6316 minnode = maxnode = TREE_VALUE (values);
6317 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
6319 tree value = TREE_VALUE (pair);
6320 if (tree_int_cst_lt (maxnode, value))
6322 if (tree_int_cst_lt (value, minnode))
6327 /* Construct the final type of this enumeration. It is the same
6328 as one of the integral types - the narrowest one that fits, except
6329 that normally we only go as narrow as int - and signed iff any of
6330 the values are negative. */
6331 unsign = (tree_int_cst_sgn (minnode) >= 0);
6332 precision = MAX (tree_int_cst_min_precision (minnode, unsign),
6333 tree_int_cst_min_precision (maxnode, unsign));
6335 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
6337 tem = c_common_type_for_size (precision, unsign);
6340 warning (0, "enumeration values exceed range of largest integer");
6341 tem = long_long_integer_type_node;
6345 tem = unsign ? unsigned_type_node : integer_type_node;
6347 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
6348 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
6349 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
6350 TYPE_SIZE (enumtype) = 0;
6352 /* If the precision of the type was specific with an attribute and it
6353 was too small, give an error. Otherwise, use it. */
6354 if (TYPE_PRECISION (enumtype))
6356 if (precision > TYPE_PRECISION (enumtype))
6357 error ("specified mode too small for enumeral values");
6360 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
6362 layout_type (enumtype);
6364 if (values != error_mark_node)
6366 /* Change the type of the enumerators to be the enum type. We
6367 need to do this irrespective of the size of the enum, for
6368 proper type checking. Replace the DECL_INITIALs of the
6369 enumerators, and the value slots of the list, with copies
6370 that have the enum type; they cannot be modified in place
6371 because they may be shared (e.g. integer_zero_node) Finally,
6372 change the purpose slots to point to the names of the decls. */
6373 for (pair = values; pair; pair = TREE_CHAIN (pair))
6375 tree enu = TREE_PURPOSE (pair);
6376 tree ini = DECL_INITIAL (enu);
6378 TREE_TYPE (enu) = enumtype;
6380 /* The ISO C Standard mandates enumerators to have type int,
6381 even though the underlying type of an enum type is
6382 unspecified. However, GCC allows enumerators of any
6383 integer type as an extensions. build_enumerator()
6384 converts any enumerators that fit in an int to type int,
6385 to avoid promotions to unsigned types when comparing
6386 integers with enumerators that fit in the int range.
6387 When -pedantic is given, build_enumerator() would have
6388 already warned about those that don't fit. Here we
6389 convert the rest to the enumerator type. */
6390 if (TREE_TYPE (ini) != integer_type_node)
6391 ini = convert (enumtype, ini);
6393 DECL_INITIAL (enu) = ini;
6394 TREE_PURPOSE (pair) = DECL_NAME (enu);
6395 TREE_VALUE (pair) = ini;
6398 TYPE_VALUES (enumtype) = values;
6401 /* Record the min/max values so that we can warn about bit-field
6402 enumerations that are too small for the values. */
6403 lt = GGC_CNEW (struct lang_type);
6404 lt->enum_min = minnode;
6405 lt->enum_max = maxnode;
6406 TYPE_LANG_SPECIFIC (enumtype) = lt;
6408 /* Fix up all variant types of this enum type. */
6409 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
6411 if (tem == enumtype)
6413 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
6414 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
6415 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
6416 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
6417 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
6418 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
6419 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
6420 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
6421 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
6422 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
6423 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
6426 /* Finish debugging output for this type. */
6427 rest_of_type_compilation (enumtype, toplevel);
6429 /* If this enum is defined inside a struct, add it to
6431 if (in_struct && !in_sizeof && !in_typeof && !in_alignof)
6432 VEC_safe_push (tree, heap, struct_types, enumtype);
6437 /* Build and install a CONST_DECL for one value of the
6438 current enumeration type (one that was begun with start_enum).
6439 Return a tree-list containing the CONST_DECL and its value.
6440 Assignment of sequential values by default is handled here. */
6443 build_enumerator (struct c_enum_contents *the_enum, tree name, tree value,
6444 location_t value_loc)
6448 /* Validate and default VALUE. */
6452 /* Don't issue more errors for error_mark_node (i.e. an
6453 undeclared identifier) - just ignore the value expression. */
6454 if (value == error_mark_node)
6456 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
6458 error ("enumerator value for %qE is not an integer constant", name);
6463 if (TREE_CODE (value) != INTEGER_CST)
6465 value = c_fully_fold (value, false, NULL);
6466 if (TREE_CODE (value) == INTEGER_CST)
6467 pedwarn (value_loc, OPT_pedantic,
6468 "enumerator value for %qE is not an integer "
6469 "constant expression", name);
6471 if (TREE_CODE (value) != INTEGER_CST)
6473 error ("enumerator value for %qE is not an integer constant",
6479 value = default_conversion (value);
6480 constant_expression_warning (value);
6485 /* Default based on previous value. */
6486 /* It should no longer be possible to have NON_LVALUE_EXPR
6490 value = the_enum->enum_next_value;
6491 if (the_enum->enum_overflow)
6492 error ("overflow in enumeration values");
6494 /* Even though the underlying type of an enum is unspecified, the
6495 type of enumeration constants is explicitly defined as int
6496 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
6498 else if (!int_fits_type_p (value, integer_type_node))
6499 pedwarn (value_loc, OPT_pedantic,
6500 "ISO C restricts enumerator values to range of %<int%>");
6502 /* The ISO C Standard mandates enumerators to have type int, even
6503 though the underlying type of an enum type is unspecified.
6504 However, GCC allows enumerators of any integer type as an
6505 extensions. Here we convert any enumerators that fit in an int
6506 to type int, to avoid promotions to unsigned types when comparing
6507 integers with enumerators that fit in the int range. When
6508 -pedantic is given, we would have already warned about those that
6509 don't fit. We have to do this here rather than in finish_enum
6510 because this value may be used to define more enumerators. */
6511 if (int_fits_type_p (value, integer_type_node))
6512 value = convert (integer_type_node, value);
6514 /* Set basis for default for next value. */
6515 the_enum->enum_next_value
6517 (EXPR_HAS_LOCATION (value) ? EXPR_LOCATION (value) : input_location,
6518 PLUS_EXPR, value, integer_one_node, 0);
6519 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
6521 /* Now create a declaration for the enum value name. */
6523 type = TREE_TYPE (value);
6524 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6525 TYPE_PRECISION (integer_type_node)),
6526 (TYPE_PRECISION (type)
6527 >= TYPE_PRECISION (integer_type_node)
6528 && TYPE_UNSIGNED (type)));
6530 decl = build_decl (CONST_DECL, name, type);
6531 DECL_INITIAL (decl) = convert (type, value);
6534 return tree_cons (decl, value, NULL_TREE);
6538 /* Create the FUNCTION_DECL for a function definition.
6539 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6540 the declaration; they describe the function's name and the type it returns,
6541 but twisted together in a fashion that parallels the syntax of C.
6543 This function creates a binding context for the function body
6544 as well as setting up the FUNCTION_DECL in current_function_decl.
6546 Returns 1 on success. If the DECLARATOR is not suitable for a function
6547 (it defines a datum instead), we return 0, which tells
6548 yyparse to report a parse error. */
6551 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6554 tree decl1, old_decl;
6555 tree restype, resdecl;
6556 struct c_label_context_se *nstack_se;
6557 struct c_label_context_vm *nstack_vm;
6559 current_function_returns_value = 0; /* Assume, until we see it does. */
6560 current_function_returns_null = 0;
6561 current_function_returns_abnormally = 0;
6562 warn_about_return_type = 0;
6563 c_switch_stack = NULL;
6565 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6566 nstack_se->labels_def = NULL;
6567 nstack_se->labels_used = NULL;
6568 nstack_se->next = label_context_stack_se;
6569 label_context_stack_se = nstack_se;
6571 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6572 nstack_vm->labels_def = NULL;
6573 nstack_vm->labels_used = NULL;
6574 nstack_vm->scope = 0;
6575 nstack_vm->next = label_context_stack_vm;
6576 label_context_stack_vm = nstack_vm;
6578 /* Indicate no valid break/continue context by setting these variables
6579 to some non-null, non-label value. We'll notice and emit the proper
6580 error message in c_finish_bc_stmt. */
6581 c_break_label = c_cont_label = size_zero_node;
6583 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
6584 &attributes, NULL, NULL, DEPRECATED_NORMAL);
6586 /* If the declarator is not suitable for a function definition,
6587 cause a syntax error. */
6590 label_context_stack_se = label_context_stack_se->next;
6591 label_context_stack_vm = label_context_stack_vm->next;
6595 decl_attributes (&decl1, attributes, 0);
6597 if (DECL_DECLARED_INLINE_P (decl1)
6598 && DECL_UNINLINABLE (decl1)
6599 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6600 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6603 /* Handle gnu_inline attribute. */
6604 if (declspecs->inline_p
6605 && !flag_gnu89_inline
6606 && TREE_CODE (decl1) == FUNCTION_DECL
6607 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
6608 || current_function_decl))
6610 if (declspecs->storage_class != csc_static)
6611 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6614 announce_function (decl1);
6616 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6618 error ("return type is an incomplete type");
6619 /* Make it return void instead. */
6621 = build_function_type (void_type_node,
6622 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6625 if (warn_about_return_type)
6626 pedwarn_c99 (input_location, flag_isoc99 ? 0
6627 : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
6628 "return type defaults to %<int%>");
6630 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6631 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6632 DECL_INITIAL (decl1) = error_mark_node;
6634 /* If this definition isn't a prototype and we had a prototype declaration
6635 before, copy the arg type info from that prototype. */
6636 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6637 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6639 current_function_prototype_locus = UNKNOWN_LOCATION;
6640 current_function_prototype_built_in = false;
6641 current_function_prototype_arg_types = NULL_TREE;
6642 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6644 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6645 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6646 TREE_TYPE (TREE_TYPE (old_decl))))
6648 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6650 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6651 current_function_prototype_built_in
6652 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6653 current_function_prototype_arg_types
6654 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6656 if (TREE_PUBLIC (decl1))
6658 /* If there is an external prototype declaration of this
6659 function, record its location but do not copy information
6660 to this decl. This may be an invisible declaration
6661 (built-in or in a scope which has finished) or simply
6662 have more refined argument types than any declaration
6664 struct c_binding *b;
6665 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6666 if (B_IN_SCOPE (b, external_scope))
6670 tree ext_decl, ext_type;
6672 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6673 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6674 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6675 TREE_TYPE (ext_type)))
6677 current_function_prototype_locus
6678 = DECL_SOURCE_LOCATION (ext_decl);
6679 current_function_prototype_built_in
6680 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6681 current_function_prototype_arg_types
6682 = TYPE_ARG_TYPES (ext_type);
6688 /* Optionally warn of old-fashioned def with no previous prototype. */
6689 if (warn_strict_prototypes
6690 && old_decl != error_mark_node
6691 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6692 && C_DECL_ISNT_PROTOTYPE (old_decl))
6693 warning (OPT_Wstrict_prototypes,
6694 "function declaration isn%'t a prototype");
6695 /* Optionally warn of any global def with no previous prototype. */
6696 else if (warn_missing_prototypes
6697 && old_decl != error_mark_node
6698 && TREE_PUBLIC (decl1)
6699 && !MAIN_NAME_P (DECL_NAME (decl1))
6700 && C_DECL_ISNT_PROTOTYPE (old_decl))
6701 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6702 /* Optionally warn of any def with no previous prototype
6703 if the function has already been used. */
6704 else if (warn_missing_prototypes
6706 && old_decl != error_mark_node
6707 && TREE_USED (old_decl)
6708 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6709 warning (OPT_Wmissing_prototypes,
6710 "%q+D was used with no prototype before its definition", decl1);
6711 /* Optionally warn of any global def with no previous declaration. */
6712 else if (warn_missing_declarations
6713 && TREE_PUBLIC (decl1)
6715 && !MAIN_NAME_P (DECL_NAME (decl1)))
6716 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6718 /* Optionally warn of any def with no previous declaration
6719 if the function has already been used. */
6720 else if (warn_missing_declarations
6722 && old_decl != error_mark_node
6723 && TREE_USED (old_decl)
6724 && C_DECL_IMPLICIT (old_decl))
6725 warning (OPT_Wmissing_declarations,
6726 "%q+D was used with no declaration before its definition", decl1);
6728 /* This function exists in static storage.
6729 (This does not mean `static' in the C sense!) */
6730 TREE_STATIC (decl1) = 1;
6732 /* A nested function is not global. */
6733 if (current_function_decl != 0)
6734 TREE_PUBLIC (decl1) = 0;
6736 /* This is the earliest point at which we might know the assembler
6737 name of the function. Thus, if it's set before this, die horribly. */
6738 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6740 /* If #pragma weak was used, mark the decl weak now. */
6741 if (current_scope == file_scope)
6742 maybe_apply_pragma_weak (decl1);
6744 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6745 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
6747 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6748 != integer_type_node)
6749 pedwarn (input_location, OPT_Wmain, "return type of %q+D is not %<int%>", decl1);
6751 check_main_parameter_types (decl1);
6753 if (!TREE_PUBLIC (decl1))
6754 pedwarn (input_location, OPT_Wmain, "%q+D is normally a non-static function", decl1);
6757 /* Record the decl so that the function name is defined.
6758 If we already have a decl for this name, and it is a FUNCTION_DECL,
6759 use the old decl. */
6761 current_function_decl = pushdecl (decl1);
6764 declare_parm_level ();
6766 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6767 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6768 DECL_ARTIFICIAL (resdecl) = 1;
6769 DECL_IGNORED_P (resdecl) = 1;
6770 DECL_RESULT (current_function_decl) = resdecl;
6772 start_fname_decls ();
6777 /* Subroutine of store_parm_decls which handles new-style function
6778 definitions (prototype format). The parms already have decls, so we
6779 need only record them as in effect and complain if any redundant
6780 old-style parm decls were written. */
6782 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6786 if (current_scope->bindings)
6788 error ("%Jold-style parameter declarations in prototyped "
6789 "function definition", fndecl);
6791 /* Get rid of the old-style declarations. */
6795 /* Don't issue this warning for nested functions, and don't issue this
6796 warning if we got here because ARG_INFO_TYPES was error_mark_node
6797 (this happens when a function definition has just an ellipsis in
6798 its parameter list). */
6799 else if (!in_system_header && !current_function_scope
6800 && arg_info->types != error_mark_node)
6801 warning (OPT_Wtraditional,
6802 "%Jtraditional C rejects ISO C style function definitions",
6805 /* Now make all the parameter declarations visible in the function body.
6806 We can bypass most of the grunt work of pushdecl. */
6807 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6809 DECL_CONTEXT (decl) = current_function_decl;
6810 if (DECL_NAME (decl))
6812 bind (DECL_NAME (decl), decl, current_scope,
6813 /*invisible=*/false, /*nested=*/false,
6815 if (!TREE_USED (decl))
6816 warn_if_shadowing (decl);
6819 error ("%Jparameter name omitted", decl);
6822 /* Record the parameter list in the function declaration. */
6823 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6825 /* Now make all the ancillary declarations visible, likewise. */
6826 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6828 DECL_CONTEXT (decl) = current_function_decl;
6829 if (DECL_NAME (decl))
6830 bind (DECL_NAME (decl), decl, current_scope,
6831 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
6834 /* And all the tag declarations. */
6835 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6836 if (TREE_PURPOSE (decl))
6837 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6838 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
6841 /* Subroutine of store_parm_decls which handles old-style function
6842 definitions (separate parameter list and declarations). */
6845 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6847 struct c_binding *b;
6848 tree parm, decl, last;
6849 tree parmids = arg_info->parms;
6850 struct pointer_set_t *seen_args = pointer_set_create ();
6852 if (!in_system_header)
6853 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6856 /* Match each formal parameter name with its declaration. Save each
6857 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6858 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6860 if (TREE_VALUE (parm) == 0)
6862 error ("%Jparameter name missing from parameter list", fndecl);
6863 TREE_PURPOSE (parm) = 0;
6867 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6868 if (b && B_IN_CURRENT_SCOPE (b))
6871 /* If we got something other than a PARM_DECL it is an error. */
6872 if (TREE_CODE (decl) != PARM_DECL)
6873 error ("%q+D declared as a non-parameter", decl);
6874 /* If the declaration is already marked, we have a duplicate
6875 name. Complain and ignore the duplicate. */
6876 else if (pointer_set_contains (seen_args, decl))
6878 error ("multiple parameters named %q+D", decl);
6879 TREE_PURPOSE (parm) = 0;
6882 /* If the declaration says "void", complain and turn it into
6884 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6886 error ("parameter %q+D declared with void type", decl);
6887 TREE_TYPE (decl) = integer_type_node;
6888 DECL_ARG_TYPE (decl) = integer_type_node;
6889 layout_decl (decl, 0);
6891 warn_if_shadowing (decl);
6893 /* If no declaration found, default to int. */
6896 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6897 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6898 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6900 warn_if_shadowing (decl);
6903 pedwarn (input_location, 0, "type of %q+D defaults to %<int%>", decl);
6905 warning (OPT_Wmissing_parameter_type, "type of %q+D defaults to %<int%>", decl);
6908 TREE_PURPOSE (parm) = decl;
6909 pointer_set_insert (seen_args, decl);
6912 /* Now examine the parms chain for incomplete declarations
6913 and declarations with no corresponding names. */
6915 for (b = current_scope->bindings; b; b = b->prev)
6918 if (TREE_CODE (parm) != PARM_DECL)
6921 if (TREE_TYPE (parm) != error_mark_node
6922 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6924 error ("parameter %q+D has incomplete type", parm);
6925 TREE_TYPE (parm) = error_mark_node;
6928 if (!pointer_set_contains (seen_args, parm))
6930 error ("declaration for parameter %q+D but no such parameter", parm);
6932 /* Pretend the parameter was not missing.
6933 This gets us to a standard state and minimizes
6934 further error messages. */
6935 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6939 /* Chain the declarations together in the order of the list of
6940 names. Store that chain in the function decl, replacing the
6941 list of names. Update the current scope to match. */
6942 DECL_ARGUMENTS (fndecl) = 0;
6944 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6945 if (TREE_PURPOSE (parm))
6947 if (parm && TREE_PURPOSE (parm))
6949 last = TREE_PURPOSE (parm);
6950 DECL_ARGUMENTS (fndecl) = last;
6952 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6953 if (TREE_PURPOSE (parm))
6955 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6956 last = TREE_PURPOSE (parm);
6958 TREE_CHAIN (last) = 0;
6961 pointer_set_destroy (seen_args);
6963 /* If there was a previous prototype,
6964 set the DECL_ARG_TYPE of each argument according to
6965 the type previously specified, and report any mismatches. */
6967 if (current_function_prototype_arg_types)
6970 for (parm = DECL_ARGUMENTS (fndecl),
6971 type = current_function_prototype_arg_types;
6972 parm || (type && TREE_VALUE (type) != error_mark_node
6973 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
6974 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6976 if (parm == 0 || type == 0
6977 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6979 if (current_function_prototype_built_in)
6980 warning (0, "number of arguments doesn%'t match "
6981 "built-in prototype");
6984 error ("number of arguments doesn%'t match prototype");
6985 error ("%Hprototype declaration",
6986 ¤t_function_prototype_locus);
6990 /* Type for passing arg must be consistent with that
6991 declared for the arg. ISO C says we take the unqualified
6992 type for parameters declared with qualified type. */
6993 if (TREE_TYPE (parm) != error_mark_node
6994 && TREE_TYPE (type) != error_mark_node
6995 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6996 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6998 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6999 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
7001 /* Adjust argument to match prototype. E.g. a previous
7002 `int foo(float);' prototype causes
7003 `int foo(x) float x; {...}' to be treated like
7004 `int foo(float x) {...}'. This is particularly
7005 useful for argument types like uid_t. */
7006 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
7008 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
7009 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
7010 && TYPE_PRECISION (TREE_TYPE (parm))
7011 < TYPE_PRECISION (integer_type_node))
7012 DECL_ARG_TYPE (parm) = integer_type_node;
7014 /* ??? Is it possible to get here with a
7015 built-in prototype or will it always have
7016 been diagnosed as conflicting with an
7017 old-style definition and discarded? */
7018 if (current_function_prototype_built_in)
7019 warning (OPT_pedantic, "promoted argument %qD "
7020 "doesn%'t match built-in prototype", parm);
7023 pedwarn (input_location, OPT_pedantic, "promoted argument %qD "
7024 "doesn%'t match prototype", parm);
7025 pedwarn (current_function_prototype_locus, OPT_pedantic,
7026 "prototype declaration");
7031 if (current_function_prototype_built_in)
7032 warning (0, "argument %qD doesn%'t match "
7033 "built-in prototype", parm);
7036 error ("argument %qD doesn%'t match prototype", parm);
7037 error ("%Hprototype declaration",
7038 ¤t_function_prototype_locus);
7043 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
7046 /* Otherwise, create a prototype that would match. */
7050 tree actual = 0, last = 0, type;
7052 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
7054 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
7056 TREE_CHAIN (last) = type;
7061 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
7063 TREE_CHAIN (last) = type;
7067 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
7068 of the type of this function, but we need to avoid having this
7069 affect the types of other similarly-typed functions, so we must
7070 first force the generation of an identical (but separate) type
7071 node for the relevant function type. The new node we create
7072 will be a variant of the main variant of the original function
7075 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
7077 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
7081 /* Store parameter declarations passed in ARG_INFO into the current
7082 function declaration. */
7085 store_parm_decls_from (struct c_arg_info *arg_info)
7087 current_function_arg_info = arg_info;
7088 store_parm_decls ();
7091 /* Store the parameter declarations into the current function declaration.
7092 This is called after parsing the parameter declarations, before
7093 digesting the body of the function.
7095 For an old-style definition, construct a prototype out of the old-style
7096 parameter declarations and inject it into the function's type. */
7099 store_parm_decls (void)
7101 tree fndecl = current_function_decl;
7104 /* The argument information block for FNDECL. */
7105 struct c_arg_info *arg_info = current_function_arg_info;
7106 current_function_arg_info = 0;
7108 /* True if this definition is written with a prototype. Note:
7109 despite C99 6.7.5.3p14, we can *not* treat an empty argument
7110 list in a function definition as equivalent to (void) -- an
7111 empty argument list specifies the function has no parameters,
7112 but only (void) sets up a prototype for future calls. */
7113 proto = arg_info->types != 0;
7116 store_parm_decls_newstyle (fndecl, arg_info);
7118 store_parm_decls_oldstyle (fndecl, arg_info);
7120 /* The next call to push_scope will be a function body. */
7122 next_is_function_body = true;
7124 /* Write a record describing this function definition to the prototypes
7125 file (if requested). */
7127 gen_aux_info_record (fndecl, 1, 0, proto);
7129 /* Initialize the RTL code for the function. */
7130 allocate_struct_function (fndecl, false);
7132 /* Begin the statement tree for this function. */
7133 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
7135 /* ??? Insert the contents of the pending sizes list into the function
7136 to be evaluated. The only reason left to have this is
7137 void foo(int n, int array[n++])
7138 because we throw away the array type in favor of a pointer type, and
7139 thus won't naturally see the SAVE_EXPR containing the increment. All
7140 other pending sizes would be handled by gimplify_parameters. */
7143 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
7144 add_stmt (TREE_VALUE (t));
7147 /* Even though we're inside a function body, we still don't want to
7148 call expand_expr to calculate the size of a variable-sized array.
7149 We haven't necessarily assigned RTL to all variables yet, so it's
7150 not safe to try to expand expressions involving them. */
7151 cfun->dont_save_pending_sizes_p = 1;
7154 /* Emit diagnostics that require gimple input for detection. Operate on
7155 FNDECL and all its nested functions. */
7158 c_gimple_diagnostics_recursively (tree fndecl)
7160 struct cgraph_node *cgn;
7161 gimple_seq body = gimple_body (fndecl);
7163 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
7164 c_warn_unused_result (body);
7166 /* Notice when OpenMP structured block constraints are violated. */
7168 diagnose_omp_structured_block_errors (fndecl);
7170 /* Finalize all nested functions now. */
7171 cgn = cgraph_node (fndecl);
7172 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
7173 c_gimple_diagnostics_recursively (cgn->decl);
7176 /* Finish up a function declaration and compile that function
7177 all the way to assembler language output. The free the storage
7178 for the function definition.
7180 This is called after parsing the body of the function definition. */
7183 finish_function (void)
7185 tree fndecl = current_function_decl;
7187 label_context_stack_se = label_context_stack_se->next;
7188 label_context_stack_vm = label_context_stack_vm->next;
7190 if (TREE_CODE (fndecl) == FUNCTION_DECL
7191 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
7193 tree args = DECL_ARGUMENTS (fndecl);
7194 for (; args; args = TREE_CHAIN (args))
7196 tree type = TREE_TYPE (args);
7197 if (INTEGRAL_TYPE_P (type)
7198 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
7199 DECL_ARG_TYPE (args) = integer_type_node;
7203 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
7204 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7206 /* Must mark the RESULT_DECL as being in this function. */
7208 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
7209 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
7211 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
7212 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
7213 == integer_type_node && flag_isoc99)
7215 tree stmt = c_finish_return (integer_zero_node, NULL_TREE);
7216 /* Hack. We don't want the middle-end to warn that this return
7217 is unreachable, so we mark its location as special. Using
7218 UNKNOWN_LOCATION has the problem that it gets clobbered in
7219 annotate_one_with_locus. A cleaner solution might be to
7220 ensure ! should_carry_locus_p (stmt), but that needs a flag.
7222 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
7225 /* Tie off the statement tree for this function. */
7226 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
7228 finish_fname_decls ();
7230 /* Complain if there's just no return statement. */
7231 if (warn_return_type
7232 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
7233 && !current_function_returns_value && !current_function_returns_null
7234 /* Don't complain if we are no-return. */
7235 && !current_function_returns_abnormally
7236 /* Don't warn for main(). */
7237 && !MAIN_NAME_P (DECL_NAME (fndecl))
7238 /* Or if they didn't actually specify a return type. */
7239 && !C_FUNCTION_IMPLICIT_INT (fndecl)
7240 /* Normally, with -Wreturn-type, flow will complain, but we might
7241 optimize out static functions. */
7242 && !TREE_PUBLIC (fndecl))
7244 warning (OPT_Wreturn_type,
7245 "no return statement in function returning non-void");
7246 TREE_NO_WARNING (fndecl) = 1;
7249 /* Store the end of the function, so that we get good line number
7250 info for the epilogue. */
7251 cfun->function_end_locus = input_location;
7253 /* Finalize the ELF visibility for the function. */
7254 c_determine_visibility (fndecl);
7256 /* For GNU C extern inline functions disregard inline limits. */
7257 if (DECL_EXTERNAL (fndecl)
7258 && DECL_DECLARED_INLINE_P (fndecl))
7259 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
7261 /* Genericize before inlining. Delay genericizing nested functions
7262 until their parent function is genericized. Since finalizing
7263 requires GENERIC, delay that as well. */
7265 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
7266 && !undef_nested_function)
7268 if (!decl_function_context (fndecl))
7270 c_genericize (fndecl);
7271 c_gimple_diagnostics_recursively (fndecl);
7273 /* ??? Objc emits functions after finalizing the compilation unit.
7274 This should be cleaned up later and this conditional removed. */
7275 if (cgraph_global_info_ready)
7277 cgraph_add_new_function (fndecl, false);
7281 cgraph_finalize_function (fndecl, false);
7285 /* Register this function with cgraph just far enough to get it
7286 added to our parent's nested function list. Handy, since the
7287 C front end doesn't have such a list. */
7288 (void) cgraph_node (fndecl);
7292 if (!decl_function_context (fndecl))
7293 undef_nested_function = false;
7295 /* We're leaving the context of this function, so zap cfun.
7296 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
7297 tree_rest_of_compilation. */
7299 current_function_decl = NULL;
7302 /* Check the declarations given in a for-loop for satisfying the C99
7303 constraints. If exactly one such decl is found, return it. */
7306 check_for_loop_decls (void)
7308 struct c_binding *b;
7309 tree one_decl = NULL_TREE;
7314 static bool hint = true;
7315 /* If we get here, declarations have been used in a for loop without
7316 the C99 for loop scope. This doesn't make much sense, so don't
7318 error ("%<for%> loop initial declarations are only allowed in C99 mode");
7321 inform (input_location,
7322 "use option -std=c99 or -std=gnu99 to compile your code");
7327 /* C99 subclause 6.8.5 paragraph 3:
7329 [#3] The declaration part of a for statement shall only
7330 declare identifiers for objects having storage class auto or
7333 It isn't clear whether, in this sentence, "identifiers" binds to
7334 "shall only declare" or to "objects" - that is, whether all identifiers
7335 declared must be identifiers for objects, or whether the restriction
7336 only applies to those that are. (A question on this in comp.std.c
7337 in November 2000 received no answer.) We implement the strictest
7338 interpretation, to avoid creating an extension which later causes
7341 for (b = current_scope->bindings; b; b = b->prev)
7344 tree decl = b->decl;
7349 switch (TREE_CODE (decl))
7352 if (TREE_STATIC (decl))
7353 error ("declaration of static variable %q+D in %<for%> loop "
7354 "initial declaration", decl);
7355 else if (DECL_EXTERNAL (decl))
7356 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
7357 "initial declaration", decl);
7361 error ("%<struct %E%> declared in %<for%> loop initial declaration",
7365 error ("%<union %E%> declared in %<for%> loop initial declaration",
7369 error ("%<enum %E%> declared in %<for%> loop initial declaration",
7373 error ("declaration of non-variable %q+D in %<for%> loop "
7374 "initial declaration", decl);
7381 return n_decls == 1 ? one_decl : NULL_TREE;
7384 /* Save and reinitialize the variables
7385 used during compilation of a C function. */
7388 c_push_function_context (void)
7390 struct language_function *p;
7391 p = GGC_NEW (struct language_function);
7394 p->base.x_stmt_tree = c_stmt_tree;
7395 p->x_break_label = c_break_label;
7396 p->x_cont_label = c_cont_label;
7397 p->x_switch_stack = c_switch_stack;
7398 p->arg_info = current_function_arg_info;
7399 p->returns_value = current_function_returns_value;
7400 p->returns_null = current_function_returns_null;
7401 p->returns_abnormally = current_function_returns_abnormally;
7402 p->warn_about_return_type = warn_about_return_type;
7404 push_function_context ();
7407 /* Restore the variables used during compilation of a C function. */
7410 c_pop_function_context (void)
7412 struct language_function *p;
7414 pop_function_context ();
7416 cfun->language = NULL;
7418 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
7419 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
7421 /* Stop pointing to the local nodes about to be freed. */
7422 /* But DECL_INITIAL must remain nonzero so we know this
7423 was an actual function definition. */
7424 DECL_INITIAL (current_function_decl) = error_mark_node;
7425 DECL_ARGUMENTS (current_function_decl) = 0;
7428 c_stmt_tree = p->base.x_stmt_tree;
7429 c_break_label = p->x_break_label;
7430 c_cont_label = p->x_cont_label;
7431 c_switch_stack = p->x_switch_stack;
7432 current_function_arg_info = p->arg_info;
7433 current_function_returns_value = p->returns_value;
7434 current_function_returns_null = p->returns_null;
7435 current_function_returns_abnormally = p->returns_abnormally;
7436 warn_about_return_type = p->warn_about_return_type;
7439 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
7442 c_dup_lang_specific_decl (tree decl)
7444 struct lang_decl *ld;
7446 if (!DECL_LANG_SPECIFIC (decl))
7449 ld = GGC_NEW (struct lang_decl);
7450 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
7451 DECL_LANG_SPECIFIC (decl) = ld;
7454 /* The functions below are required for functionality of doing
7455 function at once processing in the C front end. Currently these
7456 functions are not called from anywhere in the C front end, but as
7457 these changes continue, that will change. */
7459 /* Returns the stmt_tree (if any) to which statements are currently
7460 being added. If there is no active statement-tree, NULL is
7464 current_stmt_tree (void)
7466 return &c_stmt_tree;
7469 /* Return the global value of T as a symbol. */
7472 identifier_global_value (tree t)
7474 struct c_binding *b;
7476 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
7477 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
7483 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
7484 otherwise the name is found in ridpointers from RID_INDEX. */
7487 record_builtin_type (enum rid rid_index, const char *name, tree type)
7491 id = ridpointers[(int) rid_index];
7493 id = get_identifier (name);
7494 decl = build_decl (TYPE_DECL, id, type);
7496 if (debug_hooks->type_decl)
7497 debug_hooks->type_decl (decl, false);
7500 /* Build the void_list_node (void_type_node having been created). */
7502 build_void_list_node (void)
7504 tree t = build_tree_list (NULL_TREE, void_type_node);
7508 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7511 build_c_parm (struct c_declspecs *specs, tree attrs,
7512 struct c_declarator *declarator)
7514 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7517 ret->declarator = declarator;
7521 /* Return a declarator with nested attributes. TARGET is the inner
7522 declarator to which these attributes apply. ATTRS are the
7525 struct c_declarator *
7526 build_attrs_declarator (tree attrs, struct c_declarator *target)
7528 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7529 ret->kind = cdk_attrs;
7530 ret->declarator = target;
7531 ret->u.attrs = attrs;
7535 /* Return a declarator for a function with arguments specified by ARGS
7536 and return type specified by TARGET. */
7538 struct c_declarator *
7539 build_function_declarator (struct c_arg_info *args,
7540 struct c_declarator *target)
7542 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7543 ret->kind = cdk_function;
7544 ret->declarator = target;
7545 ret->u.arg_info = args;
7549 /* Return a declarator for the identifier IDENT (which may be
7550 NULL_TREE for an abstract declarator). */
7552 struct c_declarator *
7553 build_id_declarator (tree ident)
7555 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7557 ret->declarator = 0;
7559 /* Default value - may get reset to a more precise location. */
7560 ret->id_loc = input_location;
7564 /* Return something to represent absolute declarators containing a *.
7565 TARGET is the absolute declarator that the * contains.
7566 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7567 to apply to the pointer type. */
7569 struct c_declarator *
7570 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7571 struct c_declarator *target)
7575 struct c_declarator *itarget = target;
7576 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7577 if (type_quals_attrs)
7579 attrs = type_quals_attrs->attrs;
7580 quals = quals_from_declspecs (type_quals_attrs);
7581 if (attrs != NULL_TREE)
7582 itarget = build_attrs_declarator (attrs, target);
7584 ret->kind = cdk_pointer;
7585 ret->declarator = itarget;
7586 ret->u.pointer_quals = quals;
7590 /* Return a pointer to a structure for an empty list of declaration
7593 struct c_declspecs *
7594 build_null_declspecs (void)
7596 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7601 ret->typespec_word = cts_none;
7602 ret->storage_class = csc_none;
7603 ret->expr_const_operands = true;
7604 ret->declspecs_seen_p = false;
7605 ret->type_seen_p = false;
7606 ret->non_sc_seen_p = false;
7607 ret->typedef_p = false;
7608 ret->tag_defined_p = false;
7609 ret->explicit_signed_p = false;
7610 ret->deprecated_p = false;
7611 ret->default_int_p = false;
7612 ret->long_p = false;
7613 ret->long_long_p = false;
7614 ret->short_p = false;
7615 ret->signed_p = false;
7616 ret->unsigned_p = false;
7617 ret->complex_p = false;
7618 ret->inline_p = false;
7619 ret->thread_p = false;
7620 ret->const_p = false;
7621 ret->volatile_p = false;
7622 ret->restrict_p = false;
7623 ret->saturating_p = false;
7627 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7630 struct c_declspecs *
7631 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7635 specs->non_sc_seen_p = true;
7636 specs->declspecs_seen_p = true;
7637 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7638 && C_IS_RESERVED_WORD (qual));
7639 i = C_RID_CODE (qual);
7643 dupe = specs->const_p;
7644 specs->const_p = true;
7647 dupe = specs->volatile_p;
7648 specs->volatile_p = true;
7651 dupe = specs->restrict_p;
7652 specs->restrict_p = true;
7657 if (dupe && !flag_isoc99)
7658 pedwarn (input_location, OPT_pedantic, "duplicate %qE", qual);
7662 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7665 struct c_declspecs *
7666 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7668 tree type = spec.spec;
7669 specs->non_sc_seen_p = true;
7670 specs->declspecs_seen_p = true;
7671 specs->type_seen_p = true;
7672 if (TREE_DEPRECATED (type))
7673 specs->deprecated_p = true;
7675 /* Handle type specifier keywords. */
7676 if (TREE_CODE (type) == IDENTIFIER_NODE
7677 && C_IS_RESERVED_WORD (type)
7678 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
7680 enum rid i = C_RID_CODE (type);
7683 error ("two or more data types in declaration specifiers");
7686 if ((int) i <= (int) RID_LAST_MODIFIER)
7688 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
7693 if (specs->long_long_p)
7695 error ("%<long long long%> is too long for GCC");
7700 if (specs->typespec_word == cts_double)
7702 error ("both %<long long%> and %<double%> in "
7703 "declaration specifiers");
7706 pedwarn_c90 (input_location, OPT_Wlong_long,
7707 "ISO C90 does not support %<long long%>");
7708 specs->long_long_p = 1;
7712 error ("both %<long%> and %<short%> in "
7713 "declaration specifiers");
7714 else if (specs->typespec_word == cts_void)
7715 error ("both %<long%> and %<void%> in "
7716 "declaration specifiers");
7717 else if (specs->typespec_word == cts_bool)
7718 error ("both %<long%> and %<_Bool%> in "
7719 "declaration specifiers");
7720 else if (specs->typespec_word == cts_char)
7721 error ("both %<long%> and %<char%> in "
7722 "declaration specifiers");
7723 else if (specs->typespec_word == cts_float)
7724 error ("both %<long%> and %<float%> in "
7725 "declaration specifiers");
7726 else if (specs->typespec_word == cts_dfloat32)
7727 error ("both %<long%> and %<_Decimal32%> in "
7728 "declaration specifiers");
7729 else if (specs->typespec_word == cts_dfloat64)
7730 error ("both %<long%> and %<_Decimal64%> in "
7731 "declaration specifiers");
7732 else if (specs->typespec_word == cts_dfloat128)
7733 error ("both %<long%> and %<_Decimal128%> in "
7734 "declaration specifiers");
7736 specs->long_p = true;
7739 dupe = specs->short_p;
7741 error ("both %<long%> and %<short%> in "
7742 "declaration specifiers");
7743 else if (specs->typespec_word == cts_void)
7744 error ("both %<short%> and %<void%> in "
7745 "declaration specifiers");
7746 else if (specs->typespec_word == cts_bool)
7747 error ("both %<short%> and %<_Bool%> in "
7748 "declaration specifiers");
7749 else if (specs->typespec_word == cts_char)
7750 error ("both %<short%> and %<char%> in "
7751 "declaration specifiers");
7752 else if (specs->typespec_word == cts_float)
7753 error ("both %<short%> and %<float%> in "
7754 "declaration specifiers");
7755 else if (specs->typespec_word == cts_double)
7756 error ("both %<short%> and %<double%> in "
7757 "declaration specifiers");
7758 else if (specs->typespec_word == cts_dfloat32)
7759 error ("both %<short%> and %<_Decimal32%> in "
7760 "declaration specifiers");
7761 else if (specs->typespec_word == cts_dfloat64)
7762 error ("both %<short%> and %<_Decimal64%> in "
7763 "declaration specifiers");
7764 else if (specs->typespec_word == cts_dfloat128)
7765 error ("both %<short%> and %<_Decimal128%> in "
7766 "declaration specifiers");
7768 specs->short_p = true;
7771 dupe = specs->signed_p;
7772 if (specs->unsigned_p)
7773 error ("both %<signed%> and %<unsigned%> in "
7774 "declaration specifiers");
7775 else if (specs->typespec_word == cts_void)
7776 error ("both %<signed%> and %<void%> in "
7777 "declaration specifiers");
7778 else if (specs->typespec_word == cts_bool)
7779 error ("both %<signed%> and %<_Bool%> in "
7780 "declaration specifiers");
7781 else if (specs->typespec_word == cts_float)
7782 error ("both %<signed%> and %<float%> in "
7783 "declaration specifiers");
7784 else if (specs->typespec_word == cts_double)
7785 error ("both %<signed%> and %<double%> in "
7786 "declaration specifiers");
7787 else if (specs->typespec_word == cts_dfloat32)
7788 error ("both %<signed%> and %<_Decimal32%> in "
7789 "declaration specifiers");
7790 else if (specs->typespec_word == cts_dfloat64)
7791 error ("both %<signed%> and %<_Decimal64%> in "
7792 "declaration specifiers");
7793 else if (specs->typespec_word == cts_dfloat128)
7794 error ("both %<signed%> and %<_Decimal128%> in "
7795 "declaration specifiers");
7797 specs->signed_p = true;
7800 dupe = specs->unsigned_p;
7801 if (specs->signed_p)
7802 error ("both %<signed%> and %<unsigned%> in "
7803 "declaration specifiers");
7804 else if (specs->typespec_word == cts_void)
7805 error ("both %<unsigned%> and %<void%> in "
7806 "declaration specifiers");
7807 else if (specs->typespec_word == cts_bool)
7808 error ("both %<unsigned%> and %<_Bool%> in "
7809 "declaration specifiers");
7810 else if (specs->typespec_word == cts_float)
7811 error ("both %<unsigned%> and %<float%> in "
7812 "declaration specifiers");
7813 else if (specs->typespec_word == cts_double)
7814 error ("both %<unsigned%> and %<double%> in "
7815 "declaration specifiers");
7816 else if (specs->typespec_word == cts_dfloat32)
7817 error ("both %<unsigned%> and %<_Decimal32%> in "
7818 "declaration specifiers");
7819 else if (specs->typespec_word == cts_dfloat64)
7820 error ("both %<unsigned%> and %<_Decimal64%> in "
7821 "declaration specifiers");
7822 else if (specs->typespec_word == cts_dfloat128)
7823 error ("both %<unsigned%> and %<_Decimal128%> in "
7824 "declaration specifiers");
7826 specs->unsigned_p = true;
7829 dupe = specs->complex_p;
7830 if (!flag_isoc99 && !in_system_header)
7831 pedwarn (input_location, OPT_pedantic, "ISO C90 does not support complex types");
7832 if (specs->typespec_word == cts_void)
7833 error ("both %<complex%> and %<void%> in "
7834 "declaration specifiers");
7835 else if (specs->typespec_word == cts_bool)
7836 error ("both %<complex%> and %<_Bool%> in "
7837 "declaration specifiers");
7838 else if (specs->typespec_word == cts_dfloat32)
7839 error ("both %<complex%> and %<_Decimal32%> in "
7840 "declaration specifiers");
7841 else if (specs->typespec_word == cts_dfloat64)
7842 error ("both %<complex%> and %<_Decimal64%> in "
7843 "declaration specifiers");
7844 else if (specs->typespec_word == cts_dfloat128)
7845 error ("both %<complex%> and %<_Decimal128%> in "
7846 "declaration specifiers");
7847 else if (specs->typespec_word == cts_fract)
7848 error ("both %<complex%> and %<_Fract%> in "
7849 "declaration specifiers");
7850 else if (specs->typespec_word == cts_accum)
7851 error ("both %<complex%> and %<_Accum%> in "
7852 "declaration specifiers");
7853 else if (specs->saturating_p)
7854 error ("both %<complex%> and %<_Sat%> in "
7855 "declaration specifiers");
7857 specs->complex_p = true;
7860 dupe = specs->saturating_p;
7861 pedwarn (input_location, OPT_pedantic, "ISO C does not support saturating types");
7862 if (specs->typespec_word == cts_void)
7863 error ("both %<_Sat%> and %<void%> in "
7864 "declaration specifiers");
7865 else if (specs->typespec_word == cts_bool)
7866 error ("both %<_Sat%> and %<_Bool%> in "
7867 "declaration specifiers");
7868 else if (specs->typespec_word == cts_char)
7869 error ("both %<_Sat%> and %<char%> in "
7870 "declaration specifiers");
7871 else if (specs->typespec_word == cts_int)
7872 error ("both %<_Sat%> and %<int%> in "
7873 "declaration specifiers");
7874 else if (specs->typespec_word == cts_float)
7875 error ("both %<_Sat%> and %<float%> in "
7876 "declaration specifiers");
7877 else if (specs->typespec_word == cts_double)
7878 error ("both %<_Sat%> and %<double%> in "
7879 "declaration specifiers");
7880 else if (specs->typespec_word == cts_dfloat32)
7881 error ("both %<_Sat%> and %<_Decimal32%> in "
7882 "declaration specifiers");
7883 else if (specs->typespec_word == cts_dfloat64)
7884 error ("both %<_Sat%> and %<_Decimal64%> in "
7885 "declaration specifiers");
7886 else if (specs->typespec_word == cts_dfloat128)
7887 error ("both %<_Sat%> and %<_Decimal128%> in "
7888 "declaration specifiers");
7889 else if (specs->complex_p)
7890 error ("both %<_Sat%> and %<complex%> in "
7891 "declaration specifiers");
7893 specs->saturating_p = true;
7900 error ("duplicate %qE", type);
7906 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
7907 "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
7908 if (specs->typespec_word != cts_none)
7910 error ("two or more data types in declaration specifiers");
7917 error ("both %<long%> and %<void%> in "
7918 "declaration specifiers");
7919 else if (specs->short_p)
7920 error ("both %<short%> and %<void%> in "
7921 "declaration specifiers");
7922 else if (specs->signed_p)
7923 error ("both %<signed%> and %<void%> in "
7924 "declaration specifiers");
7925 else if (specs->unsigned_p)
7926 error ("both %<unsigned%> and %<void%> in "
7927 "declaration specifiers");
7928 else if (specs->complex_p)
7929 error ("both %<complex%> and %<void%> in "
7930 "declaration specifiers");
7931 else if (specs->saturating_p)
7932 error ("both %<_Sat%> and %<void%> in "
7933 "declaration specifiers");
7935 specs->typespec_word = cts_void;
7939 error ("both %<long%> and %<_Bool%> in "
7940 "declaration specifiers");
7941 else if (specs->short_p)
7942 error ("both %<short%> and %<_Bool%> in "
7943 "declaration specifiers");
7944 else if (specs->signed_p)
7945 error ("both %<signed%> and %<_Bool%> in "
7946 "declaration specifiers");
7947 else if (specs->unsigned_p)
7948 error ("both %<unsigned%> and %<_Bool%> in "
7949 "declaration specifiers");
7950 else if (specs->complex_p)
7951 error ("both %<complex%> and %<_Bool%> in "
7952 "declaration specifiers");
7953 else if (specs->saturating_p)
7954 error ("both %<_Sat%> and %<_Bool%> in "
7955 "declaration specifiers");
7957 specs->typespec_word = cts_bool;
7961 error ("both %<long%> and %<char%> in "
7962 "declaration specifiers");
7963 else if (specs->short_p)
7964 error ("both %<short%> and %<char%> in "
7965 "declaration specifiers");
7966 else if (specs->saturating_p)
7967 error ("both %<_Sat%> and %<char%> in "
7968 "declaration specifiers");
7970 specs->typespec_word = cts_char;
7973 if (specs->saturating_p)
7974 error ("both %<_Sat%> and %<int%> in "
7975 "declaration specifiers");
7977 specs->typespec_word = cts_int;
7981 error ("both %<long%> and %<float%> in "
7982 "declaration specifiers");
7983 else if (specs->short_p)
7984 error ("both %<short%> and %<float%> in "
7985 "declaration specifiers");
7986 else if (specs->signed_p)
7987 error ("both %<signed%> and %<float%> in "
7988 "declaration specifiers");
7989 else if (specs->unsigned_p)
7990 error ("both %<unsigned%> and %<float%> in "
7991 "declaration specifiers");
7992 else if (specs->saturating_p)
7993 error ("both %<_Sat%> and %<float%> in "
7994 "declaration specifiers");
7996 specs->typespec_word = cts_float;
7999 if (specs->long_long_p)
8000 error ("both %<long long%> and %<double%> in "
8001 "declaration specifiers");
8002 else if (specs->short_p)
8003 error ("both %<short%> and %<double%> in "
8004 "declaration specifiers");
8005 else if (specs->signed_p)
8006 error ("both %<signed%> and %<double%> in "
8007 "declaration specifiers");
8008 else if (specs->unsigned_p)
8009 error ("both %<unsigned%> and %<double%> in "
8010 "declaration specifiers");
8011 else if (specs->saturating_p)
8012 error ("both %<_Sat%> and %<double%> in "
8013 "declaration specifiers");
8015 specs->typespec_word = cts_double;
8022 if (i == RID_DFLOAT32)
8024 else if (i == RID_DFLOAT64)
8027 str = "_Decimal128";
8028 if (specs->long_long_p)
8029 error ("both %<long long%> and %<%s%> in "
8030 "declaration specifiers", str);
8032 error ("both %<long%> and %<%s%> in "
8033 "declaration specifiers", str);
8034 else if (specs->short_p)
8035 error ("both %<short%> and %<%s%> in "
8036 "declaration specifiers", str);
8037 else if (specs->signed_p)
8038 error ("both %<signed%> and %<%s%> in "
8039 "declaration specifiers", str);
8040 else if (specs->unsigned_p)
8041 error ("both %<unsigned%> and %<%s%> in "
8042 "declaration specifiers", str);
8043 else if (specs->complex_p)
8044 error ("both %<complex%> and %<%s%> in "
8045 "declaration specifiers", str);
8046 else if (specs->saturating_p)
8047 error ("both %<_Sat%> and %<%s%> in "
8048 "declaration specifiers", str);
8049 else if (i == RID_DFLOAT32)
8050 specs->typespec_word = cts_dfloat32;
8051 else if (i == RID_DFLOAT64)
8052 specs->typespec_word = cts_dfloat64;
8054 specs->typespec_word = cts_dfloat128;
8056 if (!targetm.decimal_float_supported_p ())
8057 error ("decimal floating point not supported for this target");
8058 pedwarn (input_location, OPT_pedantic,
8059 "ISO C does not support decimal floating point");
8069 if (specs->complex_p)
8070 error ("both %<complex%> and %<%s%> in "
8071 "declaration specifiers", str);
8072 else if (i == RID_FRACT)
8073 specs->typespec_word = cts_fract;
8075 specs->typespec_word = cts_accum;
8077 if (!targetm.fixed_point_supported_p ())
8078 error ("fixed-point types not supported for this target");
8079 pedwarn (input_location, OPT_pedantic,
8080 "ISO C does not support fixed-point types");
8083 /* ObjC reserved word "id", handled below. */
8089 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
8090 form of ObjC type, cases such as "int" and "long" being handled
8091 above), a TYPE (struct, union, enum and typeof specifiers) or an
8092 ERROR_MARK. In none of these cases may there have previously
8093 been any type specifiers. */
8094 if (specs->type || specs->typespec_word != cts_none
8095 || specs->long_p || specs->short_p || specs->signed_p
8096 || specs->unsigned_p || specs->complex_p)
8097 error ("two or more data types in declaration specifiers");
8098 else if (TREE_CODE (type) == TYPE_DECL)
8100 if (TREE_TYPE (type) == error_mark_node)
8101 ; /* Allow the type to default to int to avoid cascading errors. */
8104 specs->type = TREE_TYPE (type);
8105 specs->decl_attr = DECL_ATTRIBUTES (type);
8106 specs->typedef_p = true;
8107 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
8110 else if (TREE_CODE (type) == IDENTIFIER_NODE)
8112 tree t = lookup_name (type);
8113 if (!t || TREE_CODE (t) != TYPE_DECL)
8114 error ("%qE fails to be a typedef or built in type", type);
8115 else if (TREE_TYPE (t) == error_mark_node)
8118 specs->type = TREE_TYPE (t);
8120 else if (TREE_CODE (type) != ERROR_MARK)
8122 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
8123 specs->tag_defined_p = true;
8124 if (spec.kind == ctsk_typeof)
8126 specs->typedef_p = true;
8130 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
8131 specs->expr, spec.expr);
8133 specs->expr = spec.expr;
8134 specs->expr_const_operands &= spec.expr_const_operands;
8143 /* Add the storage class specifier or function specifier SCSPEC to the
8144 declaration specifiers SPECS, returning SPECS. */
8146 struct c_declspecs *
8147 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
8150 enum c_storage_class n = csc_none;
8152 specs->declspecs_seen_p = true;
8153 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
8154 && C_IS_RESERVED_WORD (scspec));
8155 i = C_RID_CODE (scspec);
8156 if (specs->non_sc_seen_p)
8157 warning (OPT_Wold_style_declaration,
8158 "%qE is not at beginning of declaration", scspec);
8162 /* C99 permits duplicate inline. Although of doubtful utility,
8163 it seems simplest to permit it in gnu89 mode as well, as
8164 there is also little utility in maintaining this as a
8165 difference between gnu89 and C99 inline. */
8167 specs->inline_p = true;
8170 dupe = specs->thread_p;
8171 if (specs->storage_class == csc_auto)
8172 error ("%<__thread%> used with %<auto%>");
8173 else if (specs->storage_class == csc_register)
8174 error ("%<__thread%> used with %<register%>");
8175 else if (specs->storage_class == csc_typedef)
8176 error ("%<__thread%> used with %<typedef%>");
8178 specs->thread_p = true;
8185 /* Diagnose "__thread extern". */
8186 if (specs->thread_p)
8187 error ("%<__thread%> before %<extern%>");
8194 /* Diagnose "__thread static". */
8195 if (specs->thread_p)
8196 error ("%<__thread%> before %<static%>");
8204 if (n != csc_none && n == specs->storage_class)
8207 error ("duplicate %qE", scspec);
8210 if (specs->storage_class != csc_none && n != specs->storage_class)
8212 error ("multiple storage classes in declaration specifiers");
8216 specs->storage_class = n;
8217 if (n != csc_extern && n != csc_static && specs->thread_p)
8219 error ("%<__thread%> used with %qE", scspec);
8220 specs->thread_p = false;
8227 /* Add the attributes ATTRS to the declaration specifiers SPECS,
8230 struct c_declspecs *
8231 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
8233 specs->attrs = chainon (attrs, specs->attrs);
8234 specs->declspecs_seen_p = true;
8238 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
8239 specifiers with any other type specifier to determine the resulting
8240 type. This is where ISO C checks on complex types are made, since
8241 "_Complex long" is a prefix of the valid ISO C type "_Complex long
8244 struct c_declspecs *
8245 finish_declspecs (struct c_declspecs *specs)
8247 /* If a type was specified as a whole, we have no modifiers and are
8249 if (specs->type != NULL_TREE)
8251 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
8252 && !specs->signed_p && !specs->unsigned_p
8253 && !specs->complex_p);
8257 /* If none of "void", "_Bool", "char", "int", "float" or "double"
8258 has been specified, treat it as "int" unless "_Complex" is
8259 present and there are no other specifiers. If we just have
8260 "_Complex", it is equivalent to "_Complex double", but e.g.
8261 "_Complex short" is equivalent to "_Complex short int". */
8262 if (specs->typespec_word == cts_none)
8264 if (specs->saturating_p)
8266 error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
8267 if (!targetm.fixed_point_supported_p ())
8268 error ("fixed-point types not supported for this target");
8269 specs->typespec_word = cts_fract;
8271 else if (specs->long_p || specs->short_p
8272 || specs->signed_p || specs->unsigned_p)
8274 specs->typespec_word = cts_int;
8276 else if (specs->complex_p)
8278 specs->typespec_word = cts_double;
8279 pedwarn (input_location, OPT_pedantic,
8280 "ISO C does not support plain %<complex%> meaning "
8281 "%<double complex%>");
8285 specs->typespec_word = cts_int;
8286 specs->default_int_p = true;
8287 /* We don't diagnose this here because grokdeclarator will
8288 give more specific diagnostics according to whether it is
8289 a function definition. */
8293 /* If "signed" was specified, record this to distinguish "int" and
8294 "signed int" in the case of a bit-field with
8295 -funsigned-bitfields. */
8296 specs->explicit_signed_p = specs->signed_p;
8298 /* Now compute the actual type. */
8299 switch (specs->typespec_word)
8302 gcc_assert (!specs->long_p && !specs->short_p
8303 && !specs->signed_p && !specs->unsigned_p
8304 && !specs->complex_p);
8305 specs->type = void_type_node;
8308 gcc_assert (!specs->long_p && !specs->short_p
8309 && !specs->signed_p && !specs->unsigned_p
8310 && !specs->complex_p);
8311 specs->type = boolean_type_node;
8314 gcc_assert (!specs->long_p && !specs->short_p);
8315 gcc_assert (!(specs->signed_p && specs->unsigned_p));
8316 if (specs->signed_p)
8317 specs->type = signed_char_type_node;
8318 else if (specs->unsigned_p)
8319 specs->type = unsigned_char_type_node;
8321 specs->type = char_type_node;
8322 if (specs->complex_p)
8324 pedwarn (input_location, OPT_pedantic,
8325 "ISO C does not support complex integer types");
8326 specs->type = build_complex_type (specs->type);
8330 gcc_assert (!(specs->long_p && specs->short_p));
8331 gcc_assert (!(specs->signed_p && specs->unsigned_p));
8332 if (specs->long_long_p)
8333 specs->type = (specs->unsigned_p
8334 ? long_long_unsigned_type_node
8335 : long_long_integer_type_node);
8336 else if (specs->long_p)
8337 specs->type = (specs->unsigned_p
8338 ? long_unsigned_type_node
8339 : long_integer_type_node);
8340 else if (specs->short_p)
8341 specs->type = (specs->unsigned_p
8342 ? short_unsigned_type_node
8343 : short_integer_type_node);
8345 specs->type = (specs->unsigned_p
8346 ? unsigned_type_node
8347 : integer_type_node);
8348 if (specs->complex_p)
8350 pedwarn (input_location, OPT_pedantic,
8351 "ISO C does not support complex integer types");
8352 specs->type = build_complex_type (specs->type);
8356 gcc_assert (!specs->long_p && !specs->short_p
8357 && !specs->signed_p && !specs->unsigned_p);
8358 specs->type = (specs->complex_p
8359 ? complex_float_type_node
8363 gcc_assert (!specs->long_long_p && !specs->short_p
8364 && !specs->signed_p && !specs->unsigned_p);
8367 specs->type = (specs->complex_p
8368 ? complex_long_double_type_node
8369 : long_double_type_node);
8373 specs->type = (specs->complex_p
8374 ? complex_double_type_node
8375 : double_type_node);
8381 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
8382 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
8383 if (specs->typespec_word == cts_dfloat32)
8384 specs->type = dfloat32_type_node;
8385 else if (specs->typespec_word == cts_dfloat64)
8386 specs->type = dfloat64_type_node;
8388 specs->type = dfloat128_type_node;
8391 gcc_assert (!specs->complex_p);
8392 if (!targetm.fixed_point_supported_p ())
8393 specs->type = integer_type_node;
8394 else if (specs->saturating_p)
8396 if (specs->long_long_p)
8397 specs->type = specs->unsigned_p
8398 ? sat_unsigned_long_long_fract_type_node
8399 : sat_long_long_fract_type_node;
8400 else if (specs->long_p)
8401 specs->type = specs->unsigned_p
8402 ? sat_unsigned_long_fract_type_node
8403 : sat_long_fract_type_node;
8404 else if (specs->short_p)
8405 specs->type = specs->unsigned_p
8406 ? sat_unsigned_short_fract_type_node
8407 : sat_short_fract_type_node;
8409 specs->type = specs->unsigned_p
8410 ? sat_unsigned_fract_type_node
8411 : sat_fract_type_node;
8415 if (specs->long_long_p)
8416 specs->type = specs->unsigned_p
8417 ? unsigned_long_long_fract_type_node
8418 : long_long_fract_type_node;
8419 else if (specs->long_p)
8420 specs->type = specs->unsigned_p
8421 ? unsigned_long_fract_type_node
8422 : long_fract_type_node;
8423 else if (specs->short_p)
8424 specs->type = specs->unsigned_p
8425 ? unsigned_short_fract_type_node
8426 : short_fract_type_node;
8428 specs->type = specs->unsigned_p
8429 ? unsigned_fract_type_node
8434 gcc_assert (!specs->complex_p);
8435 if (!targetm.fixed_point_supported_p ())
8436 specs->type = integer_type_node;
8437 else if (specs->saturating_p)
8439 if (specs->long_long_p)
8440 specs->type = specs->unsigned_p
8441 ? sat_unsigned_long_long_accum_type_node
8442 : sat_long_long_accum_type_node;
8443 else if (specs->long_p)
8444 specs->type = specs->unsigned_p
8445 ? sat_unsigned_long_accum_type_node
8446 : sat_long_accum_type_node;
8447 else if (specs->short_p)
8448 specs->type = specs->unsigned_p
8449 ? sat_unsigned_short_accum_type_node
8450 : sat_short_accum_type_node;
8452 specs->type = specs->unsigned_p
8453 ? sat_unsigned_accum_type_node
8454 : sat_accum_type_node;
8458 if (specs->long_long_p)
8459 specs->type = specs->unsigned_p
8460 ? unsigned_long_long_accum_type_node
8461 : long_long_accum_type_node;
8462 else if (specs->long_p)
8463 specs->type = specs->unsigned_p
8464 ? unsigned_long_accum_type_node
8465 : long_accum_type_node;
8466 else if (specs->short_p)
8467 specs->type = specs->unsigned_p
8468 ? unsigned_short_accum_type_node
8469 : short_accum_type_node;
8471 specs->type = specs->unsigned_p
8472 ? unsigned_accum_type_node
8483 /* A subroutine of c_write_global_declarations. Perform final processing
8484 on one file scope's declarations (or the external scope's declarations),
8488 c_write_global_declarations_1 (tree globals)
8493 /* Process the decls in the order they were written. */
8494 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8496 /* Check for used but undefined static functions using the C
8497 standard's definition of "used", and set TREE_NO_WARNING so
8498 that check_global_declarations doesn't repeat the check. */
8499 if (TREE_CODE (decl) == FUNCTION_DECL
8500 && DECL_INITIAL (decl) == 0
8501 && DECL_EXTERNAL (decl)
8502 && !TREE_PUBLIC (decl)
8503 && C_DECL_USED (decl))
8505 pedwarn (input_location, 0, "%q+F used but never defined", decl);
8506 TREE_NO_WARNING (decl) = 1;
8509 wrapup_global_declaration_1 (decl);
8515 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8516 reconsider |= wrapup_global_declaration_2 (decl);
8520 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8521 check_global_declaration_1 (decl);
8524 /* A subroutine of c_write_global_declarations Emit debug information for each
8525 of the declarations in GLOBALS. */
8528 c_write_global_declarations_2 (tree globals)
8532 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
8533 debug_hooks->global_decl (decl);
8536 /* Preserve the external declarations scope across a garbage collect. */
8537 static GTY(()) tree ext_block;
8540 c_write_global_declarations (void)
8544 /* We don't want to do this if generating a PCH. */
8548 /* Don't waste time on further processing if -fsyntax-only or we've
8549 encountered errors. */
8550 if (flag_syntax_only || errorcount || sorrycount)
8553 /* Close the external scope. */
8554 ext_block = pop_scope ();
8556 gcc_assert (!current_scope);
8560 tree tmp = BLOCK_VARS (ext_block);
8562 FILE * stream = dump_begin (TDI_tu, &flags);
8565 dump_node (tmp, flags & ~TDF_SLIM, stream);
8566 dump_end (TDI_tu, stream);
8570 /* Process all file scopes in this compilation, and the external_scope,
8571 through wrapup_global_declarations and check_global_declarations. */
8572 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8573 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
8574 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
8576 /* We're done parsing; proceed to optimize and emit assembly.
8577 FIXME: shouldn't be the front end's responsibility to call this. */
8580 /* After cgraph has had a chance to emit everything that's going to
8581 be emitted, output debug information for globals. */
8582 if (errorcount == 0 && sorrycount == 0)
8584 timevar_push (TV_SYMOUT);
8585 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8586 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
8587 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
8588 timevar_pop (TV_SYMOUT);
8594 #include "gt-c-decl.h"