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 (location_t, 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_at (DECL_SOURCE_LOCATION (decl),
588 0, "array %q+D assumed to have one element", decl);
590 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
592 layout_decl (decl, 0);
597 /* Record that inline function FUNC contains a reference (location
598 LOC) to static DECL (file-scope or function-local according to
602 record_inline_static (location_t loc, tree func, tree decl,
603 enum c_inline_static_type type)
605 struct c_inline_static *csi = GGC_NEW (struct c_inline_static);
607 csi->function = func;
608 csi->static_decl = decl;
610 csi->next = c_inline_statics;
611 c_inline_statics = csi;
614 /* Check for references to static declarations in inline functions at
615 the end of the translation unit and diagnose them if the functions
616 are still inline definitions. */
619 check_inline_statics (void)
621 struct c_inline_static *csi;
622 for (csi = c_inline_statics; csi; csi = csi->next)
624 if (DECL_EXTERNAL (csi->function))
628 pedwarn (csi->location, 0,
629 "%qD is static but used in inline function %qD "
630 "which is not static", csi->static_decl, csi->function);
633 pedwarn (csi->location, 0,
634 "%q+D is static but declared in inline function %qD "
635 "which is not static", csi->static_decl, csi->function);
641 c_inline_statics = NULL;
644 /* The Objective-C front-end often needs to determine the current scope. */
647 objc_get_current_scope (void)
649 return current_scope;
652 /* The following function is used only by Objective-C. It needs to live here
653 because it accesses the innards of c_scope. */
656 objc_mark_locals_volatile (void *enclosing_blk)
658 struct c_scope *scope;
661 for (scope = current_scope;
662 scope && scope != enclosing_blk;
663 scope = scope->outer)
665 for (b = scope->bindings; b; b = b->prev)
666 objc_volatilize_decl (b->decl);
668 /* Do not climb up past the current function. */
669 if (scope->function_body)
674 /* Nonzero if we are currently in file scope. */
677 global_bindings_p (void)
679 return (current_scope == file_scope && !c_override_global_bindings_to_false
685 keep_next_level (void)
687 keep_next_level_flag = true;
690 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
693 set_float_const_decimal64 (void)
695 current_scope->float_const_decimal64 = true;
698 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
701 clear_float_const_decimal64 (void)
703 current_scope->float_const_decimal64 = false;
706 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
709 float_const_decimal64_p (void)
711 return current_scope->float_const_decimal64;
714 /* Identify this scope as currently being filled with parameters. */
717 declare_parm_level (void)
719 current_scope->parm_flag = true;
725 if (next_is_function_body)
727 /* This is the transition from the parameters to the top level
728 of the function body. These are the same scope
729 (C99 6.2.1p4,6) so we do not push another scope structure.
730 next_is_function_body is set only by store_parm_decls, which
731 in turn is called when and only when we are about to
732 encounter the opening curly brace for the function body.
734 The outermost block of a function always gets a BLOCK node,
735 because the debugging output routines expect that each
736 function has at least one BLOCK. */
737 current_scope->parm_flag = false;
738 current_scope->function_body = true;
739 current_scope->keep = true;
740 current_scope->outer_function = current_function_scope;
741 current_function_scope = current_scope;
743 keep_next_level_flag = false;
744 next_is_function_body = false;
746 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
747 if (current_scope->outer)
748 current_scope->float_const_decimal64
749 = current_scope->outer->float_const_decimal64;
751 current_scope->float_const_decimal64 = false;
755 struct c_scope *scope;
758 scope = scope_freelist;
759 scope_freelist = scope->outer;
762 scope = GGC_CNEW (struct c_scope);
764 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
766 scope->float_const_decimal64 = current_scope->float_const_decimal64;
768 scope->float_const_decimal64 = false;
770 scope->keep = keep_next_level_flag;
771 scope->outer = current_scope;
772 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
774 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
776 if (current_scope && scope->depth == 0)
779 sorry ("GCC supports only %u nested scopes", scope->depth);
782 current_scope = scope;
783 keep_next_level_flag = false;
787 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
790 set_type_context (tree type, tree context)
792 for (type = TYPE_MAIN_VARIANT (type); type;
793 type = TYPE_NEXT_VARIANT (type))
794 TYPE_CONTEXT (type) = context;
797 /* Exit a scope. Restore the state of the identifier-decl mappings
798 that were in effect when this scope was entered. Return a BLOCK
799 node containing all the DECLs in this scope that are of interest
800 to debug info generation. */
805 struct c_scope *scope = current_scope;
806 tree block, context, p;
809 bool functionbody = scope->function_body;
810 bool keep = functionbody || scope->keep || scope->bindings;
812 c_end_vm_scope (scope->depth);
814 /* If appropriate, create a BLOCK to record the decls for the life
819 block = make_node (BLOCK);
820 BLOCK_SUBBLOCKS (block) = scope->blocks;
821 TREE_USED (block) = 1;
823 /* In each subblock, record that this is its superior. */
824 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
825 BLOCK_SUPERCONTEXT (p) = block;
827 BLOCK_VARS (block) = 0;
830 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
831 scope must be set so that they point to the appropriate
832 construct, i.e. either to the current FUNCTION_DECL node, or
833 else to the BLOCK node we just constructed.
835 Note that for tagged types whose scope is just the formal
836 parameter list for some function type specification, we can't
837 properly set their TYPE_CONTEXTs here, because we don't have a
838 pointer to the appropriate FUNCTION_TYPE node readily available
839 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
840 type nodes get set in `grokdeclarator' as soon as we have created
841 the FUNCTION_TYPE node which will represent the "scope" for these
842 "parameter list local" tagged types. */
843 if (scope->function_body)
844 context = current_function_decl;
845 else if (scope == file_scope)
847 tree file_decl = build_decl (UNKNOWN_LOCATION,
848 TRANSLATION_UNIT_DECL, 0, 0);
849 TREE_CHAIN (file_decl) = all_translation_units;
850 all_translation_units = file_decl;
856 /* Clear all bindings in this scope. */
857 for (b = scope->bindings; b; b = free_binding_and_advance (b))
860 switch (TREE_CODE (p))
863 /* Warnings for unused labels, errors for undefined labels. */
864 if (TREE_USED (p) && !DECL_INITIAL (p))
866 error ("label %q+D used but not defined", p);
867 DECL_INITIAL (p) = error_mark_node;
870 warn_for_unused_label (p);
872 /* Labels go in BLOCK_VARS. */
873 TREE_CHAIN (p) = BLOCK_VARS (block);
874 BLOCK_VARS (block) = p;
875 gcc_assert (I_LABEL_BINDING (b->id) == b);
876 I_LABEL_BINDING (b->id) = b->shadowed;
882 set_type_context (p, context);
884 /* Types may not have tag-names, in which case the type
885 appears in the bindings list with b->id NULL. */
888 gcc_assert (I_TAG_BINDING (b->id) == b);
889 I_TAG_BINDING (b->id) = b->shadowed;
894 /* Propagate TREE_ADDRESSABLE from nested functions to their
895 containing functions. */
896 if (!TREE_ASM_WRITTEN (p)
897 && DECL_INITIAL (p) != 0
898 && TREE_ADDRESSABLE (p)
899 && DECL_ABSTRACT_ORIGIN (p) != 0
900 && DECL_ABSTRACT_ORIGIN (p) != p)
901 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
902 if (!DECL_EXTERNAL (p)
904 && scope != file_scope
905 && scope != external_scope)
907 error ("nested function %q+D declared but never defined", p);
908 undef_nested_function = true;
910 else if (DECL_DECLARED_INLINE_P (p)
912 && !DECL_INITIAL (p))
914 /* C99 6.7.4p6: "a function with external linkage... declared
915 with an inline function specifier ... shall also be defined
916 in the same translation unit." */
917 if (!flag_gnu89_inline)
918 pedwarn (input_location, 0,
919 "inline function %q+D declared but never defined", p);
920 DECL_EXTERNAL (p) = 1;
926 /* Warnings for unused variables. */
928 && !TREE_NO_WARNING (p)
929 && !DECL_IN_SYSTEM_HEADER (p)
931 && !DECL_ARTIFICIAL (p)
932 && scope != file_scope
933 && scope != external_scope)
934 warning (OPT_Wunused_variable, "unused variable %q+D", p);
938 error ("type of array %q+D completed incompatibly with"
939 " implicit initialization", p);
946 /* All of these go in BLOCK_VARS, but only if this is the
947 binding in the home scope. */
950 TREE_CHAIN (p) = BLOCK_VARS (block);
951 BLOCK_VARS (block) = p;
953 else if (VAR_OR_FUNCTION_DECL_P (p))
955 /* For block local externs add a special
956 DECL_EXTERNAL decl for debug info generation. */
957 tree extp = copy_node (p);
959 DECL_EXTERNAL (extp) = 1;
960 TREE_STATIC (extp) = 0;
961 TREE_PUBLIC (extp) = 1;
962 DECL_INITIAL (extp) = NULL_TREE;
963 DECL_LANG_SPECIFIC (extp) = NULL;
964 DECL_CONTEXT (extp) = current_function_decl;
965 if (TREE_CODE (p) == FUNCTION_DECL)
967 DECL_RESULT (extp) = NULL_TREE;
968 DECL_SAVED_TREE (extp) = NULL_TREE;
969 DECL_STRUCT_FUNCTION (extp) = NULL;
971 if (b->locus != UNKNOWN_LOCATION)
972 DECL_SOURCE_LOCATION (extp) = b->locus;
973 TREE_CHAIN (extp) = BLOCK_VARS (block);
974 BLOCK_VARS (block) = extp;
976 /* If this is the file scope, and we are processing more
977 than one translation unit in this compilation, set
978 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
979 This makes same_translation_unit_p work, and causes
980 static declarations to be given disambiguating suffixes. */
981 if (scope == file_scope && num_in_fnames > 1)
983 DECL_CONTEXT (p) = context;
984 if (TREE_CODE (p) == TYPE_DECL)
985 set_type_context (TREE_TYPE (p), context);
989 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
990 already been put there by store_parm_decls. Unused-
991 parameter warnings are handled by function.c.
992 error_mark_node obviously does not go in BLOCK_VARS and
993 does not get unused-variable warnings. */
996 /* It is possible for a decl not to have a name. We get
997 here with b->id NULL in this case. */
1000 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1001 I_SYMBOL_BINDING (b->id) = b->shadowed;
1002 if (b->shadowed && b->shadowed->type)
1003 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
1013 /* Dispose of the block that we just made inside some higher level. */
1014 if ((scope->function_body || scope == file_scope) && context)
1016 DECL_INITIAL (context) = block;
1017 BLOCK_SUPERCONTEXT (block) = context;
1019 else if (scope->outer)
1022 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1023 /* If we did not make a block for the scope just exited, any
1024 blocks made for inner scopes must be carried forward so they
1025 will later become subblocks of something else. */
1026 else if (scope->blocks)
1027 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1030 /* Pop the current scope, and free the structure for reuse. */
1031 current_scope = scope->outer;
1032 if (scope->function_body)
1033 current_function_scope = scope->outer_function;
1035 memset (scope, 0, sizeof (struct c_scope));
1036 scope->outer = scope_freelist;
1037 scope_freelist = scope;
1043 push_file_scope (void)
1051 file_scope = current_scope;
1053 start_fname_decls ();
1055 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
1056 bind (DECL_NAME (decl), decl, file_scope,
1057 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1061 pop_file_scope (void)
1063 /* In case there were missing closebraces, get us back to the global
1065 while (current_scope != file_scope)
1068 /* __FUNCTION__ is defined at file scope (""). This
1069 call may not be necessary as my tests indicate it
1070 still works without it. */
1071 finish_fname_decls ();
1073 check_inline_statics ();
1075 /* This is the point to write out a PCH if we're doing that.
1076 In that case we do not want to do anything else. */
1079 c_common_write_pch ();
1083 /* Pop off the file scope and close this translation unit. */
1087 maybe_apply_pending_pragma_weaks ();
1088 cgraph_finalize_compilation_unit ();
1092 /* Push a definition or a declaration of struct, union or enum tag "name".
1093 "type" should be the type node.
1094 We assume that the tag "name" is not already defined, and has a location
1097 Note that the definition may really be just a forward reference.
1098 In that case, the TYPE_SIZE will be zero. */
1101 pushtag (location_t loc, tree name, tree type)
1103 /* Record the identifier as the type's name if it has none. */
1104 if (name && !TYPE_NAME (type))
1105 TYPE_NAME (type) = name;
1106 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1108 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1109 tagged type we just added to the current scope. This fake
1110 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1111 to output a representation of a tagged type, and it also gives
1112 us a convenient place to record the "scope start" address for the
1115 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1116 TYPE_DECL, NULL_TREE, type));
1118 /* An approximation for now, so we can tell this is a function-scope tag.
1119 This will be updated in pop_scope. */
1120 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1122 if (warn_cxx_compat && name != NULL_TREE)
1124 struct c_binding *b = I_SYMBOL_BINDING (name);
1127 && b->decl != NULL_TREE
1128 && TREE_CODE (b->decl) == TYPE_DECL
1129 && (B_IN_CURRENT_SCOPE (b)
1130 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1131 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1132 != TYPE_MAIN_VARIANT (type)))
1134 warning_at (loc, OPT_Wc___compat,
1135 ("using %qD as both a typedef and a tag is "
1138 if (b->locus != UNKNOWN_LOCATION)
1139 inform (b->locus, "originally defined here");
1144 /* Subroutine of compare_decls. Allow harmless mismatches in return
1145 and argument types provided that the type modes match. This function
1146 return a unified type given a suitable match, and 0 otherwise. */
1149 match_builtin_function_types (tree newtype, tree oldtype)
1151 tree newrettype, oldrettype;
1152 tree newargs, oldargs;
1153 tree trytype, tryargs;
1155 /* Accept the return type of the new declaration if same modes. */
1156 oldrettype = TREE_TYPE (oldtype);
1157 newrettype = TREE_TYPE (newtype);
1159 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1162 oldargs = TYPE_ARG_TYPES (oldtype);
1163 newargs = TYPE_ARG_TYPES (newtype);
1166 while (oldargs || newargs)
1170 || !TREE_VALUE (oldargs)
1171 || !TREE_VALUE (newargs)
1172 || TYPE_MODE (TREE_VALUE (oldargs))
1173 != TYPE_MODE (TREE_VALUE (newargs)))
1176 oldargs = TREE_CHAIN (oldargs);
1177 newargs = TREE_CHAIN (newargs);
1180 trytype = build_function_type (newrettype, tryargs);
1181 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1184 /* Subroutine of diagnose_mismatched_decls. Check for function type
1185 mismatch involving an empty arglist vs a nonempty one and give clearer
1188 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1189 tree newtype, tree oldtype)
1193 if (TREE_CODE (olddecl) != FUNCTION_DECL
1194 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1195 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1197 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1200 t = TYPE_ARG_TYPES (oldtype);
1202 t = TYPE_ARG_TYPES (newtype);
1203 for (; t; t = TREE_CHAIN (t))
1205 tree type = TREE_VALUE (t);
1207 if (TREE_CHAIN (t) == 0
1208 && TYPE_MAIN_VARIANT (type) != void_type_node)
1210 inform (input_location, "a parameter list with an ellipsis can%'t match "
1211 "an empty parameter name list declaration");
1215 if (c_type_promotes_to (type) != type)
1217 inform (input_location, "an argument type that has a default promotion can%'t match "
1218 "an empty parameter name list declaration");
1224 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1225 old-style function definition, NEWDECL is a prototype declaration.
1226 Diagnose inconsistencies in the argument list. Returns TRUE if
1227 the prototype is compatible, FALSE if not. */
1229 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1231 tree newargs, oldargs;
1234 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1236 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1237 newargs = TYPE_ARG_TYPES (newtype);
1242 tree oldargtype = TREE_VALUE (oldargs);
1243 tree newargtype = TREE_VALUE (newargs);
1245 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1248 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1249 newargtype = TYPE_MAIN_VARIANT (newargtype);
1251 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1254 /* Reaching the end of just one list means the two decls don't
1255 agree on the number of arguments. */
1256 if (END_OF_ARGLIST (oldargtype))
1258 error ("prototype for %q+D declares more arguments "
1259 "than previous old-style definition", newdecl);
1262 else if (END_OF_ARGLIST (newargtype))
1264 error ("prototype for %q+D declares fewer arguments "
1265 "than previous old-style definition", newdecl);
1269 /* Type for passing arg must be consistent with that declared
1271 else if (!comptypes (oldargtype, newargtype))
1273 error ("prototype for %q+D declares argument %d"
1274 " with incompatible type",
1279 oldargs = TREE_CHAIN (oldargs);
1280 newargs = TREE_CHAIN (newargs);
1284 /* If we get here, no errors were found, but do issue a warning
1285 for this poor-style construct. */
1286 warning (0, "prototype for %q+D follows non-prototype definition",
1289 #undef END_OF_ARGLIST
1292 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1293 first in a pair of mismatched declarations, using the diagnostic
1296 locate_old_decl (tree decl)
1298 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1300 else if (DECL_INITIAL (decl))
1301 inform (input_location, "previous definition of %q+D was here", decl);
1302 else if (C_DECL_IMPLICIT (decl))
1303 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1305 inform (input_location, "previous declaration of %q+D was here", decl);
1308 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1309 Returns true if the caller should proceed to merge the two, false
1310 if OLDDECL should simply be discarded. As a side effect, issues
1311 all necessary diagnostics for invalid or poor-style combinations.
1312 If it returns true, writes the types of NEWDECL and OLDDECL to
1313 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1314 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1317 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1318 tree *newtypep, tree *oldtypep)
1320 tree newtype, oldtype;
1321 bool pedwarned = false;
1322 bool warned = false;
1325 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1326 && DECL_EXTERNAL (DECL))
1328 /* If we have error_mark_node for either decl or type, just discard
1329 the previous decl - we're in an error cascade already. */
1330 if (olddecl == error_mark_node || newdecl == error_mark_node)
1332 *oldtypep = oldtype = TREE_TYPE (olddecl);
1333 *newtypep = newtype = TREE_TYPE (newdecl);
1334 if (oldtype == error_mark_node || newtype == error_mark_node)
1337 /* Two different categories of symbol altogether. This is an error
1338 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1339 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1341 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1342 && DECL_BUILT_IN (olddecl)
1343 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1345 error ("%q+D redeclared as different kind of symbol", newdecl);
1346 locate_old_decl (olddecl);
1348 else if (TREE_PUBLIC (newdecl))
1349 warning (0, "built-in function %q+D declared as non-function",
1352 warning (OPT_Wshadow, "declaration of %q+D shadows "
1353 "a built-in function", newdecl);
1357 /* Enumerators have no linkage, so may only be declared once in a
1359 if (TREE_CODE (olddecl) == CONST_DECL)
1361 error ("redeclaration of enumerator %q+D", newdecl);
1362 locate_old_decl (olddecl);
1366 if (!comptypes (oldtype, newtype))
1368 if (TREE_CODE (olddecl) == FUNCTION_DECL
1369 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1371 /* Accept harmless mismatch in function types.
1372 This is for the ffs and fprintf builtins. */
1373 tree trytype = match_builtin_function_types (newtype, oldtype);
1375 if (trytype && comptypes (newtype, trytype))
1376 *oldtypep = oldtype = trytype;
1379 /* If types don't match for a built-in, throw away the
1380 built-in. No point in calling locate_old_decl here, it
1381 won't print anything. */
1382 warning (0, "conflicting types for built-in function %q+D",
1387 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1388 && DECL_IS_BUILTIN (olddecl))
1390 /* A conflicting function declaration for a predeclared
1391 function that isn't actually built in. Objective C uses
1392 these. The new declaration silently overrides everything
1393 but the volatility (i.e. noreturn) indication. See also
1394 below. FIXME: Make Objective C use normal builtins. */
1395 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1398 /* Permit void foo (...) to match int foo (...) if the latter is
1399 the definition and implicit int was used. See
1400 c-torture/compile/920625-2.c. */
1401 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1402 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1403 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1404 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1406 pedwarned = pedwarn (input_location, 0,
1407 "conflicting types for %q+D", newdecl);
1408 /* Make sure we keep void as the return type. */
1409 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1410 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1412 /* Permit void foo (...) to match an earlier call to foo (...) with
1413 no declared type (thus, implicitly int). */
1414 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1415 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1416 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1417 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1419 pedwarned = pedwarn (input_location, 0,
1420 "conflicting types for %q+D", newdecl);
1421 /* Make sure we keep void as the return type. */
1422 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1426 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1427 error ("conflicting type qualifiers for %q+D", newdecl);
1429 error ("conflicting types for %q+D", newdecl);
1430 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1431 locate_old_decl (olddecl);
1436 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1437 but silently ignore the redeclaration if either is in a system
1438 header. (Conflicting redeclarations were handled above.) */
1439 if (TREE_CODE (newdecl) == TYPE_DECL)
1441 if (DECL_IN_SYSTEM_HEADER (newdecl)
1442 || DECL_IN_SYSTEM_HEADER (olddecl)
1443 || TREE_NO_WARNING (newdecl)
1444 || TREE_NO_WARNING (olddecl))
1445 return true; /* Allow OLDDECL to continue in use. */
1447 error ("redefinition of typedef %q+D", newdecl);
1448 locate_old_decl (olddecl);
1452 /* Function declarations can either be 'static' or 'extern' (no
1453 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1454 can never conflict with each other on account of linkage
1455 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1456 gnu89 mode permits two definitions if one is 'extern inline' and
1457 one is not. The non- extern-inline definition supersedes the
1458 extern-inline definition. */
1460 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1462 /* If you declare a built-in function name as static, or
1463 define the built-in with an old-style definition (so we
1464 can't validate the argument list) the built-in definition is
1465 overridden, but optionally warn this was a bad choice of name. */
1466 if (DECL_BUILT_IN (olddecl)
1467 && !C_DECL_DECLARED_BUILTIN (olddecl)
1468 && (!TREE_PUBLIC (newdecl)
1469 || (DECL_INITIAL (newdecl)
1470 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1472 warning (OPT_Wshadow, "declaration of %q+D shadows "
1473 "a built-in function", newdecl);
1474 /* Discard the old built-in function. */
1478 if (DECL_INITIAL (newdecl))
1480 if (DECL_INITIAL (olddecl))
1482 /* If both decls are in the same TU and the new declaration
1483 isn't overriding an extern inline reject the new decl.
1484 In c99, no overriding is allowed in the same translation
1486 if ((!DECL_EXTERN_INLINE (olddecl)
1487 || DECL_EXTERN_INLINE (newdecl)
1488 || (!flag_gnu89_inline
1489 && (!DECL_DECLARED_INLINE_P (olddecl)
1490 || !lookup_attribute ("gnu_inline",
1491 DECL_ATTRIBUTES (olddecl)))
1492 && (!DECL_DECLARED_INLINE_P (newdecl)
1493 || !lookup_attribute ("gnu_inline",
1494 DECL_ATTRIBUTES (newdecl))))
1496 && same_translation_unit_p (newdecl, olddecl))
1498 error ("redefinition of %q+D", newdecl);
1499 locate_old_decl (olddecl);
1504 /* If we have a prototype after an old-style function definition,
1505 the argument types must be checked specially. */
1506 else if (DECL_INITIAL (olddecl)
1507 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1508 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1509 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1511 locate_old_decl (olddecl);
1514 /* A non-static declaration (even an "extern") followed by a
1515 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1516 The same is true for a static forward declaration at block
1517 scope followed by a non-static declaration/definition at file
1518 scope. Static followed by non-static at the same scope is
1519 not undefined behavior, and is the most convenient way to get
1520 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1521 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1522 we do diagnose it if -Wtraditional. */
1523 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1525 /* Two exceptions to the rule. If olddecl is an extern
1526 inline, or a predeclared function that isn't actually
1527 built in, newdecl silently overrides olddecl. The latter
1528 occur only in Objective C; see also above. (FIXME: Make
1529 Objective C use normal builtins.) */
1530 if (!DECL_IS_BUILTIN (olddecl)
1531 && !DECL_EXTERN_INLINE (olddecl))
1533 error ("static declaration of %q+D follows "
1534 "non-static declaration", newdecl);
1535 locate_old_decl (olddecl);
1539 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1541 if (DECL_CONTEXT (olddecl))
1543 error ("non-static declaration of %q+D follows "
1544 "static declaration", newdecl);
1545 locate_old_decl (olddecl);
1548 else if (warn_traditional)
1550 warned |= warning (OPT_Wtraditional,
1551 "non-static declaration of %q+D "
1552 "follows static declaration", newdecl);
1556 /* Make sure gnu_inline attribute is either not present, or
1557 present on all inline decls. */
1558 if (DECL_DECLARED_INLINE_P (olddecl)
1559 && DECL_DECLARED_INLINE_P (newdecl))
1561 bool newa = lookup_attribute ("gnu_inline",
1562 DECL_ATTRIBUTES (newdecl)) != NULL;
1563 bool olda = lookup_attribute ("gnu_inline",
1564 DECL_ATTRIBUTES (olddecl)) != NULL;
1567 error ("%<gnu_inline%> attribute present on %q+D",
1568 newa ? newdecl : olddecl);
1569 error ("%Jbut not here", newa ? olddecl : newdecl);
1573 else if (TREE_CODE (newdecl) == VAR_DECL)
1575 /* Only variables can be thread-local, and all declarations must
1576 agree on this property. */
1577 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1579 /* Nothing to check. Since OLDDECL is marked threadprivate
1580 and NEWDECL does not have a thread-local attribute, we
1581 will merge the threadprivate attribute into NEWDECL. */
1584 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1586 if (DECL_THREAD_LOCAL_P (newdecl))
1587 error ("thread-local declaration of %q+D follows "
1588 "non-thread-local declaration", newdecl);
1590 error ("non-thread-local declaration of %q+D follows "
1591 "thread-local declaration", newdecl);
1593 locate_old_decl (olddecl);
1597 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1598 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1600 error ("redefinition of %q+D", newdecl);
1601 locate_old_decl (olddecl);
1605 /* Objects declared at file scope: if the first declaration had
1606 external linkage (even if it was an external reference) the
1607 second must have external linkage as well, or the behavior is
1608 undefined. If the first declaration had internal linkage, then
1609 the second must too, or else be an external reference (in which
1610 case the composite declaration still has internal linkage).
1611 As for function declarations, we warn about the static-then-
1612 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1613 if (DECL_FILE_SCOPE_P (newdecl)
1614 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1616 if (DECL_EXTERNAL (newdecl))
1618 if (!DECL_FILE_SCOPE_P (olddecl))
1620 error ("extern declaration of %q+D follows "
1621 "declaration with no linkage", newdecl);
1622 locate_old_decl (olddecl);
1625 else if (warn_traditional)
1627 warned |= warning (OPT_Wtraditional,
1628 "non-static declaration of %q+D "
1629 "follows static declaration", newdecl);
1634 if (TREE_PUBLIC (newdecl))
1635 error ("non-static declaration of %q+D follows "
1636 "static declaration", newdecl);
1638 error ("static declaration of %q+D follows "
1639 "non-static declaration", newdecl);
1641 locate_old_decl (olddecl);
1645 /* Two objects with the same name declared at the same block
1646 scope must both be external references (6.7p3). */
1647 else if (!DECL_FILE_SCOPE_P (newdecl))
1649 if (DECL_EXTERNAL (newdecl))
1651 /* Extern with initializer at block scope, which will
1652 already have received an error. */
1654 else if (DECL_EXTERNAL (olddecl))
1656 error ("declaration of %q+D with no linkage follows "
1657 "extern declaration", newdecl);
1658 locate_old_decl (olddecl);
1662 error ("redeclaration of %q+D with no linkage", newdecl);
1663 locate_old_decl (olddecl);
1671 /* All decls must agree on a visibility. */
1672 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1673 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1674 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1676 warned |= warning (0, "redeclaration of %q+D with different visibility "
1677 "(old visibility preserved)", newdecl);
1680 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1682 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1683 if (DECL_DECLARED_INLINE_P (newdecl)
1684 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1686 warned |= warning (OPT_Wattributes,
1687 "inline declaration of %qD follows "
1688 "declaration with attribute noinline", newdecl);
1690 else if (DECL_DECLARED_INLINE_P (olddecl)
1691 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1693 warned |= warning (OPT_Wattributes,
1694 "declaration of %q+D with attribute "
1695 "noinline follows inline declaration ", newdecl);
1698 else /* PARM_DECL, VAR_DECL */
1700 /* Redeclaration of a parameter is a constraint violation (this is
1701 not explicitly stated, but follows from C99 6.7p3 [no more than
1702 one declaration of the same identifier with no linkage in the
1703 same scope, except type tags] and 6.2.2p6 [parameters have no
1704 linkage]). We must check for a forward parameter declaration,
1705 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1706 an extension, the mandatory diagnostic for which is handled by
1707 mark_forward_parm_decls. */
1709 if (TREE_CODE (newdecl) == PARM_DECL
1710 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1712 error ("redefinition of parameter %q+D", newdecl);
1713 locate_old_decl (olddecl);
1718 /* Optional warning for completely redundant decls. */
1719 if (!warned && !pedwarned
1720 && warn_redundant_decls
1721 /* Don't warn about a function declaration followed by a
1723 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1724 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1725 /* Don't warn about redundant redeclarations of builtins. */
1726 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1727 && !DECL_BUILT_IN (newdecl)
1728 && DECL_BUILT_IN (olddecl)
1729 && !C_DECL_DECLARED_BUILTIN (olddecl))
1730 /* Don't warn about an extern followed by a definition. */
1731 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1732 /* Don't warn about forward parameter decls. */
1733 && !(TREE_CODE (newdecl) == PARM_DECL
1734 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1735 /* Don't warn about a variable definition following a declaration. */
1736 && !(TREE_CODE (newdecl) == VAR_DECL
1737 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1739 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1743 /* Report location of previous decl/defn. */
1744 if (warned || pedwarned)
1745 locate_old_decl (olddecl);
1747 #undef DECL_EXTERN_INLINE
1752 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1753 consistent with OLDDECL, but carries new information. Merge the
1754 new information into OLDDECL. This function issues no
1758 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1760 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1761 && DECL_INITIAL (newdecl) != 0);
1762 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1763 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1764 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1765 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1766 bool extern_changed = false;
1768 /* For real parm decl following a forward decl, rechain the old decl
1769 in its new location and clear TREE_ASM_WRITTEN (it's not a
1770 forward decl anymore). */
1771 if (TREE_CODE (newdecl) == PARM_DECL
1772 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1774 struct c_binding *b, **here;
1776 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
1777 if ((*here)->decl == olddecl)
1784 b->prev = current_scope->bindings;
1785 current_scope->bindings = b;
1787 TREE_ASM_WRITTEN (olddecl) = 0;
1790 DECL_ATTRIBUTES (newdecl)
1791 = targetm.merge_decl_attributes (olddecl, newdecl);
1793 /* Merge the data types specified in the two decls. */
1795 = TREE_TYPE (olddecl)
1796 = composite_type (newtype, oldtype);
1798 /* Lay the type out, unless already done. */
1799 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1801 if (TREE_TYPE (newdecl) != error_mark_node)
1802 layout_type (TREE_TYPE (newdecl));
1803 if (TREE_CODE (newdecl) != FUNCTION_DECL
1804 && TREE_CODE (newdecl) != TYPE_DECL
1805 && TREE_CODE (newdecl) != CONST_DECL)
1806 layout_decl (newdecl, 0);
1810 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1811 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1812 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1813 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1814 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1816 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1817 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
1821 /* Keep the old rtl since we can safely use it. */
1822 if (HAS_RTL_P (olddecl))
1823 COPY_DECL_RTL (olddecl, newdecl);
1825 /* Merge the type qualifiers. */
1826 if (TREE_READONLY (newdecl))
1827 TREE_READONLY (olddecl) = 1;
1829 if (TREE_THIS_VOLATILE (newdecl))
1830 TREE_THIS_VOLATILE (olddecl) = 1;
1832 /* Merge deprecatedness. */
1833 if (TREE_DEPRECATED (newdecl))
1834 TREE_DEPRECATED (olddecl) = 1;
1836 /* If a decl is in a system header and the other isn't, keep the one on the
1837 system header. Otherwise, keep source location of definition rather than
1838 declaration and of prototype rather than non-prototype unless that
1839 prototype is built-in. */
1840 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
1841 && DECL_IN_SYSTEM_HEADER (olddecl)
1842 && !DECL_IN_SYSTEM_HEADER (newdecl) )
1843 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1844 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
1845 && DECL_IN_SYSTEM_HEADER (newdecl)
1846 && !DECL_IN_SYSTEM_HEADER (olddecl))
1847 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
1848 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1849 || (old_is_prototype && !new_is_prototype
1850 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1851 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1853 /* Merge the initialization information. */
1854 if (DECL_INITIAL (newdecl) == 0)
1855 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1857 /* Merge the threadprivate attribute. */
1858 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1860 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1861 C_DECL_THREADPRIVATE_P (newdecl) = 1;
1864 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1866 /* Merge the section attribute.
1867 We want to issue an error if the sections conflict but that
1868 must be done later in decl_attributes since we are called
1869 before attributes are assigned. */
1870 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1871 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1873 /* Copy the assembler name.
1874 Currently, it can only be defined in the prototype. */
1875 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1877 /* Use visibility of whichever declaration had it specified */
1878 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1880 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1881 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1884 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1886 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1887 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1888 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1889 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1890 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1891 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1892 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1893 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
1894 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1895 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1896 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1899 /* Merge the storage class information. */
1900 merge_weak (newdecl, olddecl);
1902 /* For functions, static overrides non-static. */
1903 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1905 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1906 /* This is since we don't automatically
1907 copy the attributes of NEWDECL into OLDDECL. */
1908 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1909 /* If this clears `static', clear it in the identifier too. */
1910 if (!TREE_PUBLIC (olddecl))
1911 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1915 /* In c99, 'extern' declaration before (or after) 'inline' means this
1916 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1918 if (TREE_CODE (newdecl) == FUNCTION_DECL
1919 && !flag_gnu89_inline
1920 && (DECL_DECLARED_INLINE_P (newdecl)
1921 || DECL_DECLARED_INLINE_P (olddecl))
1922 && (!DECL_DECLARED_INLINE_P (newdecl)
1923 || !DECL_DECLARED_INLINE_P (olddecl)
1924 || !DECL_EXTERNAL (olddecl))
1925 && DECL_EXTERNAL (newdecl)
1926 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
1927 && !current_function_decl)
1928 DECL_EXTERNAL (newdecl) = 0;
1930 if (DECL_EXTERNAL (newdecl))
1932 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1933 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1935 /* An extern decl does not override previous storage class. */
1936 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1937 if (!DECL_EXTERNAL (newdecl))
1939 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1940 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1945 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1946 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1949 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1951 /* If we're redefining a function previously defined as extern
1952 inline, make sure we emit debug info for the inline before we
1953 throw it away, in case it was inlined into a function that
1954 hasn't been written out yet. */
1955 if (new_is_definition && DECL_INITIAL (olddecl))
1956 /* The new defn must not be inline. */
1957 DECL_UNINLINABLE (newdecl) = 1;
1960 /* If either decl says `inline', this fn is inline, unless
1961 its definition was passed already. */
1962 if (DECL_DECLARED_INLINE_P (newdecl)
1963 || DECL_DECLARED_INLINE_P (olddecl))
1964 DECL_DECLARED_INLINE_P (newdecl) = 1;
1966 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1967 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1969 DECL_DISREGARD_INLINE_LIMITS (newdecl)
1970 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
1971 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
1972 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
1975 if (DECL_BUILT_IN (olddecl))
1977 /* If redeclaring a builtin function, it stays built in.
1978 But it gets tagged as having been declared. */
1979 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1980 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1981 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1982 if (new_is_prototype)
1983 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1985 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1986 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1989 /* Preserve function specific target and optimization options */
1990 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
1991 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
1992 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
1993 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
1995 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
1996 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
1997 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
1998 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2000 /* Also preserve various other info from the definition. */
2001 if (!new_is_definition)
2004 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2005 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2006 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2007 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2008 gimple_set_body (newdecl, gimple_body (olddecl));
2009 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2010 for (t = DECL_ARGUMENTS (newdecl); t ; t = TREE_CHAIN (t))
2011 DECL_CONTEXT (t) = newdecl;
2013 /* See if we've got a function to instantiate from. */
2014 if (DECL_SAVED_TREE (olddecl))
2015 DECL_ABSTRACT_ORIGIN (newdecl)
2016 = DECL_ABSTRACT_ORIGIN (olddecl);
2020 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
2022 /* Merge the USED information. */
2023 if (TREE_USED (olddecl))
2024 TREE_USED (newdecl) = 1;
2025 else if (TREE_USED (newdecl))
2026 TREE_USED (olddecl) = 1;
2028 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2029 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
2031 unsigned olddecl_uid = DECL_UID (olddecl);
2032 tree olddecl_context = DECL_CONTEXT (olddecl);
2033 tree olddecl_arguments = NULL;
2034 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2035 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2037 memcpy ((char *) olddecl + sizeof (struct tree_common),
2038 (char *) newdecl + sizeof (struct tree_common),
2039 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2040 switch (TREE_CODE (olddecl))
2043 gimple_set_body (olddecl, gimple_body (newdecl));
2053 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2054 (char *) newdecl + sizeof (struct tree_decl_common),
2055 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2060 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2061 (char *) newdecl + sizeof (struct tree_decl_common),
2062 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2064 DECL_UID (olddecl) = olddecl_uid;
2065 DECL_CONTEXT (olddecl) = olddecl_context;
2066 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2067 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2070 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2071 so that encode_section_info has a chance to look at the new decl
2072 flags and attributes. */
2073 if (DECL_RTL_SET_P (olddecl)
2074 && (TREE_CODE (olddecl) == FUNCTION_DECL
2075 || (TREE_CODE (olddecl) == VAR_DECL
2076 && TREE_STATIC (olddecl))))
2077 make_decl_rtl (olddecl);
2079 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
2080 and the definition is coming from the old version, cgraph needs
2081 to be called again. */
2082 if (extern_changed && !new_is_definition
2083 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
2084 cgraph_mark_if_needed (olddecl);
2087 /* Handle when a new declaration NEWDECL has the same name as an old
2088 one OLDDECL in the same binding contour. Prints an error message
2091 If safely possible, alter OLDDECL to look like NEWDECL, and return
2092 true. Otherwise, return false. */
2095 duplicate_decls (tree newdecl, tree olddecl)
2097 tree newtype = NULL, oldtype = NULL;
2099 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2101 /* Avoid `unused variable' and other warnings for OLDDECL. */
2102 TREE_NO_WARNING (olddecl) = 1;
2106 merge_decls (newdecl, olddecl, newtype, oldtype);
2111 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2113 warn_if_shadowing (tree new_decl)
2115 struct c_binding *b;
2117 /* Shadow warnings wanted? */
2119 /* No shadow warnings for internally generated vars. */
2120 || DECL_IS_BUILTIN (new_decl)
2121 /* No shadow warnings for vars made for inlining. */
2122 || DECL_FROM_INLINE (new_decl))
2125 /* Is anything being shadowed? Invisible decls do not count. */
2126 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2127 if (b->decl && b->decl != new_decl && !b->invisible)
2129 tree old_decl = b->decl;
2131 if (old_decl == error_mark_node)
2133 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2134 "non-variable", new_decl);
2137 else if (TREE_CODE (old_decl) == PARM_DECL)
2138 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2140 else if (DECL_FILE_SCOPE_P (old_decl))
2141 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2142 "declaration", new_decl);
2143 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2144 && DECL_BUILT_IN (old_decl))
2146 warning (OPT_Wshadow, "declaration of %q+D shadows "
2147 "a built-in function", new_decl);
2151 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2154 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
2160 /* Record a decl-node X as belonging to the current lexical scope.
2161 Check for errors (such as an incompatible declaration for the same
2162 name already seen in the same scope).
2164 Returns either X or an old decl for the same name.
2165 If an old decl is returned, it may have been smashed
2166 to agree with what X says. */
2171 tree name = DECL_NAME (x);
2172 struct c_scope *scope = current_scope;
2173 struct c_binding *b;
2174 bool nested = false;
2175 location_t locus = DECL_SOURCE_LOCATION (x);
2177 /* Must set DECL_CONTEXT for everything not at file scope or
2178 DECL_FILE_SCOPE_P won't work. Local externs don't count
2179 unless they have initializers (which generate code). */
2180 if (current_function_decl
2181 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2182 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2183 DECL_CONTEXT (x) = current_function_decl;
2185 /* If this is of variably modified type, prevent jumping into its
2187 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2188 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2189 c_begin_vm_scope (scope->depth);
2191 /* Anonymous decls are just inserted in the scope. */
2194 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2199 /* First, see if there is another declaration with the same name in
2200 the current scope. If there is, duplicate_decls may do all the
2201 work for us. If duplicate_decls returns false, that indicates
2202 two incompatible decls in the same scope; we are to silently
2203 replace the old one (duplicate_decls has issued all appropriate
2204 diagnostics). In particular, we should not consider possible
2205 duplicates in the external scope, or shadowing. */
2206 b = I_SYMBOL_BINDING (name);
2207 if (b && B_IN_SCOPE (b, scope))
2209 struct c_binding *b_ext, *b_use;
2210 tree type = TREE_TYPE (x);
2211 tree visdecl = b->decl;
2212 tree vistype = TREE_TYPE (visdecl);
2213 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2214 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2215 b->inner_comp = false;
2218 /* If this is an external linkage declaration, we should check
2219 for compatibility with the type in the external scope before
2220 setting the type at this scope based on the visible
2221 information only. */
2222 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2224 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2225 b_ext = b_ext->shadowed;
2230 TREE_TYPE (b_use->decl) = b_use->type;
2233 if (duplicate_decls (x, b_use->decl))
2237 /* Save the updated type in the external scope and
2238 restore the proper type for this scope. */
2240 if (comptypes (vistype, type))
2241 thistype = composite_type (vistype, type);
2243 thistype = TREE_TYPE (b_use->decl);
2244 b_use->type = TREE_TYPE (b_use->decl);
2245 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2246 && DECL_BUILT_IN (b_use->decl))
2248 = build_type_attribute_variant (thistype,
2251 TREE_TYPE (b_use->decl) = thistype;
2256 goto skip_external_and_shadow_checks;
2259 /* All declarations with external linkage, and all external
2260 references, go in the external scope, no matter what scope is
2261 current. However, the binding in that scope is ignored for
2262 purposes of normal name lookup. A separate binding structure is
2263 created in the requested scope; this governs the normal
2264 visibility of the symbol.
2266 The binding in the externals scope is used exclusively for
2267 detecting duplicate declarations of the same object, no matter
2268 what scope they are in; this is what we do here. (C99 6.2.7p2:
2269 All declarations that refer to the same object or function shall
2270 have compatible type; otherwise, the behavior is undefined.) */
2271 if (DECL_EXTERNAL (x) || scope == file_scope)
2273 tree type = TREE_TYPE (x);
2276 bool type_saved = false;
2277 if (b && !B_IN_EXTERNAL_SCOPE (b)
2278 && (TREE_CODE (b->decl) == FUNCTION_DECL
2279 || TREE_CODE (b->decl) == VAR_DECL)
2280 && DECL_FILE_SCOPE_P (b->decl))
2283 vistype = TREE_TYPE (visdecl);
2285 if (scope != file_scope
2286 && !DECL_IN_SYSTEM_HEADER (x))
2287 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2289 while (b && !B_IN_EXTERNAL_SCOPE (b))
2291 /* If this decl might be modified, save its type. This is
2292 done here rather than when the decl is first bound
2293 because the type may change after first binding, through
2294 being completed or through attributes being added. If we
2295 encounter multiple such decls, only the first should have
2296 its type saved; the others will already have had their
2297 proper types saved and the types will not have changed as
2298 their scopes will not have been re-entered. */
2299 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2301 b->type = TREE_TYPE (b->decl);
2304 if (B_IN_FILE_SCOPE (b)
2305 && TREE_CODE (b->decl) == VAR_DECL
2306 && TREE_STATIC (b->decl)
2307 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2308 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2309 && TREE_CODE (type) == ARRAY_TYPE
2310 && TYPE_DOMAIN (type)
2311 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2312 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2314 /* Array type completed in inner scope, which should be
2315 diagnosed if the completion does not have size 1 and
2316 it does not get completed in the file scope. */
2317 b->inner_comp = true;
2322 /* If a matching external declaration has been found, set its
2323 type to the composite of all the types of that declaration.
2324 After the consistency checks, it will be reset to the
2325 composite of the visible types only. */
2326 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2328 TREE_TYPE (b->decl) = b->type;
2330 /* The point of the same_translation_unit_p check here is,
2331 we want to detect a duplicate decl for a construct like
2332 foo() { extern bar(); } ... static bar(); but not if
2333 they are in different translation units. In any case,
2334 the static does not go in the externals scope. */
2336 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2337 && duplicate_decls (x, b->decl))
2342 if (comptypes (vistype, type))
2343 thistype = composite_type (vistype, type);
2345 thistype = TREE_TYPE (b->decl);
2349 b->type = TREE_TYPE (b->decl);
2350 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2352 = build_type_attribute_variant (thistype,
2353 TYPE_ATTRIBUTES (b->type));
2354 TREE_TYPE (b->decl) = thistype;
2355 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2359 else if (TREE_PUBLIC (x))
2361 if (visdecl && !b && duplicate_decls (x, visdecl))
2363 /* An external declaration at block scope referring to a
2364 visible entity with internal linkage. The composite
2365 type will already be correct for this scope, so we
2366 just need to fall through to make the declaration in
2373 bind (name, x, external_scope, /*invisible=*/true,
2374 /*nested=*/false, locus);
2380 if (TREE_CODE (x) != PARM_DECL)
2381 warn_if_shadowing (x);
2383 skip_external_and_shadow_checks:
2384 if (TREE_CODE (x) == TYPE_DECL)
2385 set_underlying_type (x);
2387 bind (name, x, scope, /*invisible=*/false, nested, locus);
2389 /* If x's type is incomplete because it's based on a
2390 structure or union which has not yet been fully declared,
2391 attach it to that structure or union type, so we can go
2392 back and complete the variable declaration later, if the
2393 structure or union gets fully declared.
2395 If the input is erroneous, we can have error_mark in the type
2396 slot (e.g. "f(void a, ...)") - that doesn't count as an
2398 if (TREE_TYPE (x) != error_mark_node
2399 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2401 tree element = TREE_TYPE (x);
2403 while (TREE_CODE (element) == ARRAY_TYPE)
2404 element = TREE_TYPE (element);
2405 element = TYPE_MAIN_VARIANT (element);
2407 if ((TREE_CODE (element) == RECORD_TYPE
2408 || TREE_CODE (element) == UNION_TYPE)
2409 && (TREE_CODE (x) != TYPE_DECL
2410 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2411 && !COMPLETE_TYPE_P (element))
2412 C_TYPE_INCOMPLETE_VARS (element)
2413 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2418 /* Record X as belonging to file scope.
2419 This is used only internally by the Objective-C front end,
2420 and is limited to its needs. duplicate_decls is not called;
2421 if there is any preexisting decl for this identifier, it is an ICE. */
2424 pushdecl_top_level (tree x)
2427 bool nested = false;
2428 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2430 name = DECL_NAME (x);
2432 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2434 if (TREE_PUBLIC (x))
2436 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2441 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2447 implicit_decl_warning (tree id, tree olddecl)
2449 if (warn_implicit_function_declaration)
2454 warned = pedwarn (input_location, OPT_Wimplicit_function_declaration,
2455 "implicit declaration of function %qE", id);
2457 warned = warning (OPT_Wimplicit_function_declaration,
2458 G_("implicit declaration of function %qE"), id);
2459 if (olddecl && warned)
2460 locate_old_decl (olddecl);
2464 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
2465 function of type int (). */
2468 implicitly_declare (location_t loc, tree functionid)
2470 struct c_binding *b;
2474 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2476 if (B_IN_SCOPE (b, external_scope))
2485 if (decl == error_mark_node)
2488 /* FIXME: Objective-C has weird not-really-builtin functions
2489 which are supposed to be visible automatically. They wind up
2490 in the external scope because they're pushed before the file
2491 scope gets created. Catch this here and rebind them into the
2493 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2495 bind (functionid, decl, file_scope,
2496 /*invisible=*/false, /*nested=*/true,
2497 DECL_SOURCE_LOCATION (decl));
2502 tree newtype = default_function_type;
2504 TREE_TYPE (decl) = b->type;
2505 /* Implicit declaration of a function already declared
2506 (somehow) in a different scope, or as a built-in.
2507 If this is the first time this has happened, warn;
2508 then recycle the old declaration but with the new type. */
2509 if (!C_DECL_IMPLICIT (decl))
2511 implicit_decl_warning (functionid, decl);
2512 C_DECL_IMPLICIT (decl) = 1;
2514 if (DECL_BUILT_IN (decl))
2516 newtype = build_type_attribute_variant (newtype,
2518 (TREE_TYPE (decl)));
2519 if (!comptypes (newtype, TREE_TYPE (decl)))
2521 warning_at (loc, 0, "incompatible implicit declaration of "
2522 "built-in function %qD", decl);
2523 newtype = TREE_TYPE (decl);
2528 if (!comptypes (newtype, TREE_TYPE (decl)))
2530 error_at (loc, "incompatible implicit declaration of function %qD", decl);
2531 locate_old_decl (decl);
2534 b->type = TREE_TYPE (decl);
2535 TREE_TYPE (decl) = newtype;
2536 bind (functionid, decl, current_scope,
2537 /*invisible=*/false, /*nested=*/true,
2538 DECL_SOURCE_LOCATION (decl));
2543 /* Not seen before. */
2544 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
2545 DECL_EXTERNAL (decl) = 1;
2546 TREE_PUBLIC (decl) = 1;
2547 C_DECL_IMPLICIT (decl) = 1;
2548 implicit_decl_warning (functionid, 0);
2549 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2551 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2553 /* C89 says implicit declarations are in the innermost block.
2554 So we record the decl in the standard fashion. */
2555 decl = pushdecl (decl);
2557 /* No need to call objc_check_decl here - it's a function type. */
2558 rest_of_decl_compilation (decl, 0, 0);
2560 /* Write a record describing this implicit function declaration
2561 to the prototypes file (if requested). */
2562 gen_aux_info_record (decl, 0, 1, 0);
2564 /* Possibly apply some default attributes to this implicit declaration. */
2565 decl_attributes (&decl, NULL_TREE, 0);
2570 /* Issue an error message for a reference to an undeclared variable
2571 ID, including a reference to a builtin outside of function-call
2572 context. Establish a binding of the identifier to error_mark_node
2573 in an appropriate scope, which will suppress further errors for the
2574 same identifier. The error message should be given location LOC. */
2576 undeclared_variable (location_t loc, tree id)
2578 static bool already = false;
2579 struct c_scope *scope;
2581 if (current_function_decl == 0)
2583 error ("%H%qE undeclared here (not in a function)", &loc, id);
2584 scope = current_scope;
2588 error ("%H%qE undeclared (first use in this function)", &loc, id);
2592 error ("%H(Each undeclared identifier is reported only once", &loc);
2593 error ("%Hfor each function it appears in.)", &loc);
2597 /* If we are parsing old-style parameter decls, current_function_decl
2598 will be nonnull but current_function_scope will be null. */
2599 scope = current_function_scope ? current_function_scope : current_scope;
2601 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
2605 /* Subroutine of lookup_label, declare_label, define_label: construct a
2606 LABEL_DECL with all the proper frills. */
2609 make_label (location_t location, tree name)
2611 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
2613 DECL_CONTEXT (label) = current_function_decl;
2614 DECL_MODE (label) = VOIDmode;
2619 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2620 Create one if none exists so far for the current function.
2621 This is called when a label is used in a goto expression or
2622 has its address taken. */
2625 lookup_label (tree name)
2629 if (current_function_decl == 0)
2631 error ("label %qE referenced outside of any function", name);
2635 /* Use a label already defined or ref'd with this name, but not if
2636 it is inherited from a containing function and wasn't declared
2638 label = I_LABEL_DECL (name);
2639 if (label && (DECL_CONTEXT (label) == current_function_decl
2640 || C_DECLARED_LABEL_FLAG (label)))
2642 /* If the label has only been declared, update its apparent
2643 location to point here, for better diagnostics if it
2644 turns out not to have been defined. */
2645 if (!TREE_USED (label))
2646 DECL_SOURCE_LOCATION (label) = input_location;
2650 /* No label binding for that identifier; make one. */
2651 label = make_label (input_location, name);
2653 /* Ordinary labels go in the current function scope. */
2654 bind (name, label, current_function_scope,
2655 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2659 /* Make a label named NAME in the current function, shadowing silently
2660 any that may be inherited from containing functions or containing
2661 scopes. This is called for __label__ declarations. */
2664 declare_label (tree name)
2666 struct c_binding *b = I_LABEL_BINDING (name);
2669 /* Check to make sure that the label hasn't already been declared
2671 if (b && B_IN_CURRENT_SCOPE (b))
2673 error ("duplicate label declaration %qE", name);
2674 locate_old_decl (b->decl);
2676 /* Just use the previous declaration. */
2680 label = make_label (input_location, name);
2681 C_DECLARED_LABEL_FLAG (label) = 1;
2683 /* Declared labels go in the current scope. */
2684 bind (name, label, current_scope,
2685 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2689 /* Define a label, specifying the location in the source file.
2690 Return the LABEL_DECL node for the label, if the definition is valid.
2691 Otherwise return 0. */
2694 define_label (location_t location, tree name)
2696 /* Find any preexisting label with this name. It is an error
2697 if that label has already been defined in this function, or
2698 if there is a containing function with a declared label with
2700 tree label = I_LABEL_DECL (name);
2701 struct c_label_list *nlist_se, *nlist_vm;
2704 && ((DECL_CONTEXT (label) == current_function_decl
2705 && DECL_INITIAL (label) != 0)
2706 || (DECL_CONTEXT (label) != current_function_decl
2707 && C_DECLARED_LABEL_FLAG (label))))
2709 error_at (location, "duplicate label %qD", label);
2710 locate_old_decl (label);
2713 else if (label && DECL_CONTEXT (label) == current_function_decl)
2715 /* The label has been used or declared already in this function,
2716 but not defined. Update its location to point to this
2718 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2719 error_at (location, "jump into statement expression");
2720 if (C_DECL_UNDEFINABLE_VM (label))
2722 "jump into scope of identifier with variably modified type");
2723 DECL_SOURCE_LOCATION (label) = location;
2727 /* No label binding for that identifier; make one. */
2728 label = make_label (location, name);
2730 /* Ordinary labels go in the current function scope. */
2731 bind (name, label, current_function_scope,
2732 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2735 if (!in_system_header && lookup_name (name))
2736 warning_at (location, OPT_Wtraditional,
2737 "traditional C lacks a separate namespace "
2738 "for labels, identifier %qE conflicts", name);
2740 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2741 nlist_se->next = label_context_stack_se->labels_def;
2742 nlist_se->label = label;
2743 label_context_stack_se->labels_def = nlist_se;
2745 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2746 nlist_vm->next = label_context_stack_vm->labels_def;
2747 nlist_vm->label = label;
2748 label_context_stack_vm->labels_def = nlist_vm;
2750 /* Mark label as having been defined. */
2751 DECL_INITIAL (label) = error_mark_node;
2755 /* Given NAME, an IDENTIFIER_NODE,
2756 return the structure (or union or enum) definition for that name.
2757 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2758 CODE says which kind of type the caller wants;
2759 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2760 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
2761 location where the tag was defined.
2762 If the wrong kind of type is found, an error is reported. */
2765 lookup_tag (enum tree_code code, tree name, int thislevel_only,
2768 struct c_binding *b = I_TAG_BINDING (name);
2774 /* We only care about whether it's in this level if
2775 thislevel_only was set or it might be a type clash. */
2776 if (thislevel_only || TREE_CODE (b->decl) != code)
2778 /* For our purposes, a tag in the external scope is the same as
2779 a tag in the file scope. (Primarily relevant to Objective-C
2780 and its builtin structure tags, which get pushed before the
2781 file scope is created.) */
2782 if (B_IN_CURRENT_SCOPE (b)
2783 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2787 if (thislevel_only && !thislevel)
2790 if (TREE_CODE (b->decl) != code)
2792 /* Definition isn't the kind we were looking for. */
2793 pending_invalid_xref = name;
2794 pending_invalid_xref_location = input_location;
2796 /* If in the same binding level as a declaration as a tag
2797 of a different type, this must not be allowed to
2798 shadow that tag, so give the error immediately.
2799 (For example, "struct foo; union foo;" is invalid.) */
2801 pending_xref_error ();
2810 /* Print an error message now
2811 for a recent invalid struct, union or enum cross reference.
2812 We don't print them immediately because they are not invalid
2813 when used in the `struct foo;' construct for shadowing. */
2816 pending_xref_error (void)
2818 if (pending_invalid_xref != 0)
2819 error ("%H%qE defined as wrong kind of tag",
2820 &pending_invalid_xref_location, pending_invalid_xref);
2821 pending_invalid_xref = 0;
2825 /* Look up NAME in the current scope and its superiors
2826 in the namespace of variables, functions and typedefs.
2827 Return a ..._DECL node of some kind representing its definition,
2828 or return 0 if it is undefined. */
2831 lookup_name (tree name)
2833 struct c_binding *b = I_SYMBOL_BINDING (name);
2834 if (b && !b->invisible)
2839 /* Similar to `lookup_name' but look only at the indicated scope. */
2842 lookup_name_in_scope (tree name, struct c_scope *scope)
2844 struct c_binding *b;
2846 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2847 if (B_IN_SCOPE (b, scope))
2852 /* Create the predefined scalar types of C,
2853 and some nodes representing standard constants (0, 1, (void *) 0).
2854 Initialize the global scope.
2855 Make definitions for built-in primitive functions. */
2858 c_init_decl_processing (void)
2860 location_t save_loc = input_location;
2862 /* Initialize reserved words for parser. */
2865 current_function_decl = 0;
2867 gcc_obstack_init (&parser_obstack);
2869 /* Make the externals scope. */
2871 external_scope = current_scope;
2873 /* Declarations from c_common_nodes_and_builtins must not be associated
2874 with this input file, lest we get differences between using and not
2875 using preprocessed headers. */
2876 input_location = BUILTINS_LOCATION;
2878 build_common_tree_nodes (flag_signed_char, false);
2880 c_common_nodes_and_builtins ();
2882 /* In C, comparisons and TRUTH_* expressions have type int. */
2883 truthvalue_type_node = integer_type_node;
2884 truthvalue_true_node = integer_one_node;
2885 truthvalue_false_node = integer_zero_node;
2887 /* Even in C99, which has a real boolean type. */
2888 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
2889 boolean_type_node));
2891 input_location = save_loc;
2893 pedantic_lvalues = true;
2895 make_fname_decl = c_make_fname_decl;
2896 start_fname_decls ();
2899 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
2900 give the decl, NAME is the initialization string and TYPE_DEP
2901 indicates whether NAME depended on the type of the function. As we
2902 don't yet implement delayed emission of static data, we mark the
2903 decl as emitted so it is not placed in the output. Anything using
2904 it must therefore pull out the STRING_CST initializer directly.
2908 c_make_fname_decl (location_t loc, tree id, int type_dep)
2910 const char *name = fname_as_string (type_dep);
2911 tree decl, type, init;
2912 size_t length = strlen (name);
2914 type = build_array_type (char_type_node,
2915 build_index_type (size_int (length)));
2916 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2918 decl = build_decl (loc, VAR_DECL, id, type);
2920 TREE_STATIC (decl) = 1;
2921 TREE_READONLY (decl) = 1;
2922 DECL_ARTIFICIAL (decl) = 1;
2924 init = build_string (length + 1, name);
2925 free (CONST_CAST (char *, name));
2926 TREE_TYPE (init) = type;
2927 DECL_INITIAL (decl) = init;
2929 TREE_USED (decl) = 1;
2931 if (current_function_decl
2932 /* For invalid programs like this:
2935 const char* p = __FUNCTION__;
2937 the __FUNCTION__ is believed to appear in K&R style function
2938 parameter declarator. In that case we still don't have
2940 && (!errorcount || current_function_scope))
2942 DECL_CONTEXT (decl) = current_function_decl;
2943 bind (id, decl, current_function_scope,
2944 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2947 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
2953 c_builtin_function (tree decl)
2955 tree type = TREE_TYPE (decl);
2956 tree id = DECL_NAME (decl);
2958 const char *name = IDENTIFIER_POINTER (id);
2959 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2961 /* Should never be called on a symbol with a preexisting meaning. */
2962 gcc_assert (!I_SYMBOL_BINDING (id));
2964 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
2967 /* Builtins in the implementation namespace are made visible without
2968 needing to be explicitly declared. See push_file_scope. */
2969 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2971 TREE_CHAIN (decl) = visible_builtins;
2972 visible_builtins = decl;
2979 c_builtin_function_ext_scope (tree decl)
2981 tree type = TREE_TYPE (decl);
2982 tree id = DECL_NAME (decl);
2984 const char *name = IDENTIFIER_POINTER (id);
2985 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2987 /* Should never be called on a symbol with a preexisting meaning. */
2988 gcc_assert (!I_SYMBOL_BINDING (id));
2990 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
2993 /* Builtins in the implementation namespace are made visible without
2994 needing to be explicitly declared. See push_file_scope. */
2995 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2997 TREE_CHAIN (decl) = visible_builtins;
2998 visible_builtins = decl;
3004 /* Called when a declaration is seen that contains no names to declare.
3005 If its type is a reference to a structure, union or enum inherited
3006 from a containing scope, shadow that tag name for the current scope
3007 with a forward reference.
3008 If its type defines a new named structure or union
3009 or defines an enum, it is valid but we need not do anything here.
3010 Otherwise, it is an error. */
3013 shadow_tag (const struct c_declspecs *declspecs)
3015 shadow_tag_warned (declspecs, 0);
3018 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
3021 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
3023 bool found_tag = false;
3025 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
3027 tree value = declspecs->type;
3028 enum tree_code code = TREE_CODE (value);
3030 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
3031 /* Used to test also that TYPE_SIZE (value) != 0.
3032 That caused warning for `struct foo;' at top level in the file. */
3034 tree name = TYPE_NAME (value);
3039 if (declspecs->restrict_p)
3041 error ("invalid use of %<restrict%>");
3047 if (warned != 1 && code != ENUMERAL_TYPE)
3048 /* Empty unnamed enum OK */
3050 pedwarn (input_location, 0,
3051 "unnamed struct/union that defines no instances");
3055 else if (!declspecs->tag_defined_p
3056 && declspecs->storage_class != csc_none)
3059 pedwarn (input_location, 0,
3060 "empty declaration with storage class specifier "
3061 "does not redeclare tag");
3063 pending_xref_error ();
3065 else if (!declspecs->tag_defined_p
3066 && (declspecs->const_p
3067 || declspecs->volatile_p
3068 || declspecs->restrict_p))
3071 pedwarn (input_location, 0,
3072 "empty declaration with type qualifier "
3073 "does not redeclare tag");
3075 pending_xref_error ();
3079 pending_invalid_xref = 0;
3080 t = lookup_tag (code, name, 1, NULL);
3084 t = make_node (code);
3085 pushtag (input_location, name, t);
3091 if (warned != 1 && !in_system_header)
3093 pedwarn (input_location, 0,
3094 "useless type name in empty declaration");
3099 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
3101 pedwarn (input_location, 0, "useless type name in empty declaration");
3105 pending_invalid_xref = 0;
3107 if (declspecs->inline_p)
3109 error ("%<inline%> in empty declaration");
3113 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3115 error ("%<auto%> in file-scope empty declaration");
3119 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3121 error ("%<register%> in file-scope empty declaration");
3125 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3127 warning (0, "useless storage class specifier in empty declaration");
3131 if (!warned && !in_system_header && declspecs->thread_p)
3133 warning (0, "useless %<__thread%> in empty declaration");
3137 if (!warned && !in_system_header && (declspecs->const_p
3138 || declspecs->volatile_p
3139 || declspecs->restrict_p))
3141 warning (0, "useless type qualifier in empty declaration");
3148 pedwarn (input_location, 0, "empty declaration");
3153 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3154 bits. SPECS represents declaration specifiers that the grammar
3155 only permits to contain type qualifiers and attributes. */
3158 quals_from_declspecs (const struct c_declspecs *specs)
3160 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3161 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3162 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3163 gcc_assert (!specs->type
3164 && !specs->decl_attr
3165 && specs->typespec_word == cts_none
3166 && specs->storage_class == csc_none
3167 && !specs->typedef_p
3168 && !specs->explicit_signed_p
3169 && !specs->deprecated_p
3171 && !specs->long_long_p
3174 && !specs->unsigned_p
3175 && !specs->complex_p
3177 && !specs->thread_p);
3181 /* Construct an array declarator. LOC is the location of the
3182 beginning of the array (usually the opening brace). EXPR is the
3183 expression inside [], or NULL_TREE. QUALS are the type qualifiers
3184 inside the [] (to be applied to the pointer to which a parameter
3185 array is converted). STATIC_P is true if "static" is inside the
3186 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
3187 VLA of unspecified length which is nevertheless a complete type,
3188 false otherwise. The field for the contained declarator is left to
3189 be filled in by set_array_declarator_inner. */
3191 struct c_declarator *
3192 build_array_declarator (location_t loc,
3193 tree expr, struct c_declspecs *quals, bool static_p,
3196 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3197 struct c_declarator);
3198 declarator->id_loc = loc;
3199 declarator->kind = cdk_array;
3200 declarator->declarator = 0;
3201 declarator->u.array.dimen = expr;
3204 declarator->u.array.attrs = quals->attrs;
3205 declarator->u.array.quals = quals_from_declspecs (quals);
3209 declarator->u.array.attrs = NULL_TREE;
3210 declarator->u.array.quals = 0;
3212 declarator->u.array.static_p = static_p;
3213 declarator->u.array.vla_unspec_p = vla_unspec_p;
3216 if (static_p || quals != NULL)
3217 pedwarn (loc, OPT_pedantic,
3218 "ISO C90 does not support %<static%> or type "
3219 "qualifiers in parameter array declarators");
3221 pedwarn (loc, OPT_pedantic,
3222 "ISO C90 does not support %<[*]%> array declarators");
3226 if (!current_scope->parm_flag)
3229 error_at (loc, "%<[*]%> not allowed in other than "
3230 "function prototype scope");
3231 declarator->u.array.vla_unspec_p = false;
3234 current_scope->had_vla_unspec = true;
3239 /* Set the contained declarator of an array declarator. DECL is the
3240 declarator, as constructed by build_array_declarator; INNER is what
3241 appears on the left of the []. */
3243 struct c_declarator *
3244 set_array_declarator_inner (struct c_declarator *decl,
3245 struct c_declarator *inner)
3247 decl->declarator = inner;
3251 /* INIT is a constructor that forms DECL's initializer. If the final
3252 element initializes a flexible array field, add the size of that
3253 initializer to DECL's size. */
3256 add_flexible_array_elts_to_size (tree decl, tree init)
3260 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3263 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3264 type = TREE_TYPE (elt);
3265 if (TREE_CODE (type) == ARRAY_TYPE
3266 && TYPE_SIZE (type) == NULL_TREE
3267 && TYPE_DOMAIN (type) != NULL_TREE
3268 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3270 complete_array_type (&type, elt, false);
3272 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3273 DECL_SIZE_UNIT (decl)
3274 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3278 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3279 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3280 before the type name, and set *EXPR_CONST_OPERANDS, if
3281 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3282 appear in a constant expression. */
3285 groktypename (struct c_type_name *type_name, tree *expr,
3286 bool *expr_const_operands)
3289 tree attrs = type_name->specs->attrs;
3291 type_name->specs->attrs = NULL_TREE;
3293 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3294 false, NULL, &attrs, expr, expr_const_operands,
3297 /* Apply attributes. */
3298 decl_attributes (&type, attrs, 0);
3303 /* Decode a declarator in an ordinary declaration or data definition.
3304 This is called as soon as the type information and variable name
3305 have been parsed, before parsing the initializer if any.
3306 Here we create the ..._DECL node, fill in its type,
3307 and put it on the list of decls for the current context.
3308 The ..._DECL node is returned as the value.
3310 Exception: for arrays where the length is not specified,
3311 the type is left null, to be filled in by `finish_decl'.
3313 Function definitions do not come here; they go to start_function
3314 instead. However, external and forward declarations of functions
3315 do go through here. Structure field declarations are done by
3316 grokfield and not through here. */
3319 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3320 bool initialized, tree attributes)
3324 tree expr = NULL_TREE;
3325 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3327 /* An object declared as __attribute__((deprecated)) suppresses
3328 warnings of uses of other deprecated items. */
3329 if (lookup_attribute ("deprecated", attributes))
3330 deprecated_state = DEPRECATED_SUPPRESS;
3332 decl = grokdeclarator (declarator, declspecs,
3333 NORMAL, initialized, NULL, &attributes, &expr, NULL,
3341 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
3342 warning (OPT_Wmain, "%q+D is usually a function", decl);
3345 /* Is it valid for this decl to have an initializer at all?
3346 If not, set INITIALIZED to zero, which will indirectly
3347 tell 'finish_decl' to ignore the initializer once it is parsed. */
3348 switch (TREE_CODE (decl))
3351 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3356 error ("function %qD is initialized like a variable", decl);
3361 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3362 error ("parameter %qD is initialized", decl);
3367 /* Don't allow initializations for incomplete types except for
3368 arrays which might be completed by the initialization. */
3370 /* This can happen if the array size is an undefined macro.
3371 We already gave a warning, so we don't need another one. */
3372 if (TREE_TYPE (decl) == error_mark_node)
3374 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3376 /* A complete type is ok if size is fixed. */
3378 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3379 || C_DECL_VARIABLE_SIZE (decl))
3381 error ("variable-sized object may not be initialized");
3385 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3387 error ("variable %qD has initializer but incomplete type", decl);
3390 else if (C_DECL_VARIABLE_SIZE (decl))
3392 /* Although C99 is unclear about whether incomplete arrays
3393 of VLAs themselves count as VLAs, it does not make
3394 sense to permit them to be initialized given that
3395 ordinary VLAs may not be initialized. */
3396 error ("variable-sized object may not be initialized");
3403 if (current_scope == file_scope)
3404 TREE_STATIC (decl) = 1;
3406 /* Tell 'pushdecl' this is an initialized decl
3407 even though we don't yet have the initializer expression.
3408 Also tell 'finish_decl' it may store the real initializer. */
3409 DECL_INITIAL (decl) = error_mark_node;
3412 /* If this is a function declaration, write a record describing it to the
3413 prototypes file (if requested). */
3415 if (TREE_CODE (decl) == FUNCTION_DECL)
3416 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3418 /* ANSI specifies that a tentative definition which is not merged with
3419 a non-tentative definition behaves exactly like a definition with an
3420 initializer equal to zero. (Section 3.7.2)
3422 -fno-common gives strict ANSI behavior, though this tends to break
3423 a large body of code that grew up without this rule.
3425 Thread-local variables are never common, since there's no entrenched
3426 body of code to break, and it allows more efficient variable references
3427 in the presence of dynamic linking. */
3429 if (TREE_CODE (decl) == VAR_DECL
3431 && TREE_PUBLIC (decl)
3432 && !DECL_THREAD_LOCAL_P (decl)
3434 DECL_COMMON (decl) = 1;
3436 /* Set attributes here so if duplicate decl, will have proper attributes. */
3437 decl_attributes (&decl, attributes, 0);
3439 /* Handle gnu_inline attribute. */
3440 if (declspecs->inline_p
3441 && !flag_gnu89_inline
3442 && TREE_CODE (decl) == FUNCTION_DECL
3443 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
3444 || current_function_decl))
3446 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3448 else if (declspecs->storage_class != csc_static)
3449 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3452 if (TREE_CODE (decl) == FUNCTION_DECL
3453 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3455 struct c_declarator *ce = declarator;
3457 if (ce->kind == cdk_pointer)
3458 ce = declarator->declarator;
3459 if (ce->kind == cdk_function)
3461 tree args = ce->u.arg_info->parms;
3462 for (; args; args = TREE_CHAIN (args))
3464 tree type = TREE_TYPE (args);
3465 if (type && INTEGRAL_TYPE_P (type)
3466 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3467 DECL_ARG_TYPE (args) = integer_type_node;
3472 if (TREE_CODE (decl) == FUNCTION_DECL
3473 && DECL_DECLARED_INLINE_P (decl)
3474 && DECL_UNINLINABLE (decl)
3475 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3476 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3479 /* C99 6.7.4p3: An inline definition of a function with external
3480 linkage shall not contain a definition of a modifiable object
3481 with static storage duration... */
3482 if (TREE_CODE (decl) == VAR_DECL
3483 && current_scope != file_scope
3484 && TREE_STATIC (decl)
3485 && !TREE_READONLY (decl)
3486 && DECL_DECLARED_INLINE_P (current_function_decl)
3487 && DECL_EXTERNAL (current_function_decl))
3488 record_inline_static (input_location, current_function_decl,
3489 decl, csi_modifiable);
3491 /* Add this decl to the current scope.
3492 TEM may equal DECL or it may be a previous decl of the same name. */
3493 tem = pushdecl (decl);
3495 if (initialized && DECL_EXTERNAL (tem))
3497 DECL_EXTERNAL (tem) = 0;
3498 TREE_STATIC (tem) = 1;
3504 /* Initialize EH if not initialized yet and exceptions are enabled. */
3507 c_maybe_initialize_eh (void)
3509 if (!flag_exceptions || c_eh_initialized_p)
3512 c_eh_initialized_p = true;
3513 eh_personality_libfunc
3514 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3515 ? "__gcc_personality_sj0"
3516 : "__gcc_personality_v0");
3517 default_init_unwind_resume_libfunc ();
3518 using_eh_for_cleanups ();
3521 /* Finish processing of a declaration;
3522 install its initial value.
3523 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
3524 If the length of an array type is not known before,
3525 it must be determined now, from the initial value, or it is an error.
3527 INIT_LOC is the location of the initial value. */
3530 finish_decl (tree decl, location_t init_loc, tree init,
3531 tree origtype, tree asmspec_tree)
3534 bool was_incomplete = (DECL_SIZE (decl) == 0);
3535 const char *asmspec = 0;
3537 /* If a name was specified, get the string. */
3538 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3539 && DECL_FILE_SCOPE_P (decl))
3540 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3542 asmspec = TREE_STRING_POINTER (asmspec_tree);
3544 /* If `start_decl' didn't like having an initialization, ignore it now. */
3545 if (init != 0 && DECL_INITIAL (decl) == 0)
3548 /* Don't crash if parm is initialized. */
3549 if (TREE_CODE (decl) == PARM_DECL)
3553 store_init_value (init_loc, decl, init, origtype);
3555 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3556 || TREE_CODE (decl) == FUNCTION_DECL
3557 || TREE_CODE (decl) == FIELD_DECL))
3558 objc_check_decl (decl);
3560 type = TREE_TYPE (decl);
3562 /* Deduce size of array from initialization, if not already known. */
3563 if (TREE_CODE (type) == ARRAY_TYPE
3564 && TYPE_DOMAIN (type) == 0
3565 && TREE_CODE (decl) != TYPE_DECL)
3568 = (TREE_STATIC (decl)
3569 /* Even if pedantic, an external linkage array
3570 may have incomplete type at first. */
3571 ? pedantic && !TREE_PUBLIC (decl)
3572 : !DECL_EXTERNAL (decl));
3574 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3577 /* Get the completed type made by complete_array_type. */
3578 type = TREE_TYPE (decl);
3583 error ("initializer fails to determine size of %q+D", decl);
3588 error ("array size missing in %q+D", decl);
3589 /* If a `static' var's size isn't known,
3590 make it extern as well as static, so it does not get
3592 If it is not `static', then do not mark extern;
3593 finish_incomplete_decl will give it a default size
3594 and it will get allocated. */
3595 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3596 DECL_EXTERNAL (decl) = 1;
3600 error ("zero or negative size array %q+D", decl);
3604 /* For global variables, update the copy of the type that
3605 exists in the binding. */
3606 if (TREE_PUBLIC (decl))
3608 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3609 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3610 b_ext = b_ext->shadowed;
3614 b_ext->type = composite_type (b_ext->type, type);
3625 if (DECL_INITIAL (decl))
3626 TREE_TYPE (DECL_INITIAL (decl)) = type;
3628 layout_decl (decl, 0);
3631 if (TREE_CODE (decl) == VAR_DECL)
3633 if (init && TREE_CODE (init) == CONSTRUCTOR)
3634 add_flexible_array_elts_to_size (decl, init);
3636 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3637 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3638 layout_decl (decl, 0);
3640 if (DECL_SIZE (decl) == 0
3641 /* Don't give an error if we already gave one earlier. */
3642 && TREE_TYPE (decl) != error_mark_node
3643 && (TREE_STATIC (decl)
3644 /* A static variable with an incomplete type
3645 is an error if it is initialized.
3646 Also if it is not file scope.
3647 Otherwise, let it through, but if it is not `extern'
3648 then it may cause an error message later. */
3649 ? (DECL_INITIAL (decl) != 0
3650 || !DECL_FILE_SCOPE_P (decl))
3651 /* An automatic variable with an incomplete type
3653 : !DECL_EXTERNAL (decl)))
3655 error ("storage size of %q+D isn%'t known", decl);
3656 TREE_TYPE (decl) = error_mark_node;
3659 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3660 && DECL_SIZE (decl) != 0)
3662 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3663 constant_expression_warning (DECL_SIZE (decl));
3666 error ("storage size of %q+D isn%'t constant", decl);
3667 TREE_TYPE (decl) = error_mark_node;
3671 if (TREE_USED (type))
3672 TREE_USED (decl) = 1;
3675 /* If this is a function and an assembler name is specified, reset DECL_RTL
3676 so we can give it its new name. Also, update built_in_decls if it
3677 was a normal built-in. */
3678 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3680 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3681 set_builtin_user_assembler_name (decl, asmspec);
3682 set_user_assembler_name (decl, asmspec);
3685 /* If #pragma weak was used, mark the decl weak now. */
3686 maybe_apply_pragma_weak (decl);
3688 /* Output the assembler code and/or RTL code for variables and functions,
3689 unless the type is an undefined structure or union.
3690 If not, it will get done when the type is completed. */
3692 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3694 /* Determine the ELF visibility. */
3695 if (TREE_PUBLIC (decl))
3696 c_determine_visibility (decl);
3698 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3699 if (c_dialect_objc ())
3700 objc_check_decl (decl);
3704 /* If this is not a static variable, issue a warning.
3705 It doesn't make any sense to give an ASMSPEC for an
3706 ordinary, non-register local variable. Historically,
3707 GCC has accepted -- but ignored -- the ASMSPEC in
3709 if (!DECL_FILE_SCOPE_P (decl)
3710 && TREE_CODE (decl) == VAR_DECL
3711 && !C_DECL_REGISTER (decl)
3712 && !TREE_STATIC (decl))
3713 warning (0, "ignoring asm-specifier for non-static local "
3714 "variable %q+D", decl);
3716 set_user_assembler_name (decl, asmspec);
3719 if (DECL_FILE_SCOPE_P (decl))
3721 if (DECL_INITIAL (decl) == NULL_TREE
3722 || DECL_INITIAL (decl) == error_mark_node)
3723 /* Don't output anything
3724 when a tentative file-scope definition is seen.
3725 But at end of compilation, do output code for them. */
3726 DECL_DEFER_OUTPUT (decl) = 1;
3727 rest_of_decl_compilation (decl, true, 0);
3731 /* In conjunction with an ASMSPEC, the `register'
3732 keyword indicates that we should place the variable
3733 in a particular register. */
3734 if (asmspec && C_DECL_REGISTER (decl))
3736 DECL_HARD_REGISTER (decl) = 1;
3737 /* This cannot be done for a structure with volatile
3738 fields, on which DECL_REGISTER will have been
3740 if (!DECL_REGISTER (decl))
3741 error ("cannot put object with volatile field into register");
3744 if (TREE_CODE (decl) != FUNCTION_DECL)
3746 /* If we're building a variable sized type, and we might be
3747 reachable other than via the top of the current binding
3748 level, then create a new BIND_EXPR so that we deallocate
3749 the object at the right time. */
3750 /* Note that DECL_SIZE can be null due to errors. */
3751 if (DECL_SIZE (decl)
3752 && !TREE_CONSTANT (DECL_SIZE (decl))
3753 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3756 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3757 TREE_SIDE_EFFECTS (bind) = 1;
3759 BIND_EXPR_BODY (bind) = push_stmt_list ();
3761 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
3767 if (!DECL_FILE_SCOPE_P (decl))
3769 /* Recompute the RTL of a local array now
3770 if it used to be an incomplete type. */
3772 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3774 /* If we used it already as memory, it must stay in memory. */
3775 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3776 /* If it's still incomplete now, no init will save it. */
3777 if (DECL_SIZE (decl) == 0)
3778 DECL_INITIAL (decl) = 0;
3783 if (TREE_CODE (decl) == TYPE_DECL)
3785 if (!DECL_FILE_SCOPE_P (decl)
3786 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3787 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
3789 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3792 /* At the end of a declaration, throw away any variable type sizes
3793 of types defined inside that declaration. There is no use
3794 computing them in the following function definition. */
3795 if (current_scope == file_scope)
3796 get_pending_sizes ();
3798 /* Install a cleanup (aka destructor) if one was given. */
3799 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3801 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3804 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3805 tree cleanup_decl = lookup_name (cleanup_id);
3809 /* Build "cleanup(&decl)" for the destructor. */
3810 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
3811 vec = VEC_alloc (tree, gc, 1);
3812 VEC_quick_push (tree, vec, cleanup);
3813 cleanup = build_function_call_vec (DECL_SOURCE_LOCATION (decl),
3814 cleanup_decl, vec, NULL);
3815 VEC_free (tree, gc, vec);
3817 /* Don't warn about decl unused; the cleanup uses it. */
3818 TREE_USED (decl) = 1;
3819 TREE_USED (cleanup_decl) = 1;
3821 /* Initialize EH, if we've been told to do so. */
3822 c_maybe_initialize_eh ();
3824 push_cleanup (decl, cleanup, false);
3829 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3832 grokparm (const struct c_parm *parm)
3834 tree attrs = parm->attrs;
3835 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3836 NULL, &attrs, NULL, NULL, DEPRECATED_NORMAL);
3838 decl_attributes (&decl, attrs, 0);
3843 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3844 and push that on the current scope. */
3847 push_parm_decl (const struct c_parm *parm)
3849 tree attrs = parm->attrs;
3852 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
3853 &attrs, NULL, NULL, DEPRECATED_NORMAL);
3854 decl_attributes (&decl, attrs, 0);
3856 decl = pushdecl (decl);
3858 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
3861 /* Mark all the parameter declarations to date as forward decls.
3862 Also diagnose use of this extension. */
3865 mark_forward_parm_decls (void)
3867 struct c_binding *b;
3869 if (pedantic && !current_scope->warned_forward_parm_decls)
3871 pedwarn (input_location, OPT_pedantic,
3872 "ISO C forbids forward parameter declarations");
3873 current_scope->warned_forward_parm_decls = true;
3876 for (b = current_scope->bindings; b; b = b->prev)
3877 if (TREE_CODE (b->decl) == PARM_DECL)
3878 TREE_ASM_WRITTEN (b->decl) = 1;
3881 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3882 literal, which may be an incomplete array type completed by the
3883 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
3884 literal. NON_CONST is true if the initializers contain something
3885 that cannot occur in a constant expression. */
3888 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
3890 /* We do not use start_decl here because we have a type, not a declarator;
3891 and do not use finish_decl because the decl should be stored inside
3892 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3897 if (type == error_mark_node)
3898 return error_mark_node;
3900 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
3901 DECL_EXTERNAL (decl) = 0;
3902 TREE_PUBLIC (decl) = 0;
3903 TREE_STATIC (decl) = (current_scope == file_scope);
3904 DECL_CONTEXT (decl) = current_function_decl;
3905 TREE_USED (decl) = 1;
3906 TREE_TYPE (decl) = type;
3907 TREE_READONLY (decl) = TYPE_READONLY (type);
3908 store_init_value (loc, decl, init, NULL_TREE);
3910 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3912 int failure = complete_array_type (&TREE_TYPE (decl),
3913 DECL_INITIAL (decl), true);
3914 gcc_assert (!failure);
3916 type = TREE_TYPE (decl);
3917 TREE_TYPE (DECL_INITIAL (decl)) = type;
3920 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3921 return error_mark_node;
3923 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
3924 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3925 TREE_SIDE_EFFECTS (complit) = 1;
3927 layout_decl (decl, 0);
3929 if (TREE_STATIC (decl))
3931 /* This decl needs a name for the assembler output. */
3932 set_compound_literal_name (decl);
3933 DECL_DEFER_OUTPUT (decl) = 1;
3934 DECL_COMDAT (decl) = 1;
3935 DECL_ARTIFICIAL (decl) = 1;
3936 DECL_IGNORED_P (decl) = 1;
3938 rest_of_decl_compilation (decl, 1, 0);
3943 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
3944 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
3950 /* Check the type of a compound literal. Here we just check that it
3951 is valid for C++. */
3954 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
3956 if (warn_cxx_compat && type_name->specs->tag_defined_p)
3957 warning_at (loc, OPT_Wc___compat,
3958 "defining a type in a compound literal is invalid in C++");
3961 /* Determine whether TYPE is a structure with a flexible array member,
3962 or a union containing such a structure (possibly recursively). */
3965 flexible_array_type_p (tree type)
3968 switch (TREE_CODE (type))
3971 x = TYPE_FIELDS (type);
3974 while (TREE_CHAIN (x) != NULL_TREE)
3976 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3977 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3978 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3979 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3983 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3985 if (flexible_array_type_p (TREE_TYPE (x)))
3994 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3995 replacing with appropriate values if they are invalid. */
3997 check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
4000 unsigned int max_width;
4001 unsigned HOST_WIDE_INT w;
4002 const char *name = (orig_name
4003 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
4004 : _("<anonymous>"));
4006 /* Detect and ignore out of range field width and process valid
4008 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
4009 || TREE_CODE (*width) != INTEGER_CST)
4011 error ("bit-field %qs width not an integer constant", name);
4012 *width = integer_one_node;
4016 constant_expression_warning (*width);
4017 if (tree_int_cst_sgn (*width) < 0)
4019 error ("negative width in bit-field %qs", name);
4020 *width = integer_one_node;
4022 else if (integer_zerop (*width) && orig_name)
4024 error ("zero width for bit-field %qs", name);
4025 *width = integer_one_node;
4029 /* Detect invalid bit-field type. */
4030 if (TREE_CODE (*type) != INTEGER_TYPE
4031 && TREE_CODE (*type) != BOOLEAN_TYPE
4032 && TREE_CODE (*type) != ENUMERAL_TYPE)
4034 error ("bit-field %qs has invalid type", name);
4035 *type = unsigned_type_node;
4038 type_mv = TYPE_MAIN_VARIANT (*type);
4039 if (!in_system_header
4040 && type_mv != integer_type_node
4041 && type_mv != unsigned_type_node
4042 && type_mv != boolean_type_node)
4043 pedwarn (input_location, OPT_pedantic,
4044 "type of bit-field %qs is a GCC extension", name);
4046 max_width = TYPE_PRECISION (*type);
4048 if (0 < compare_tree_int (*width, max_width))
4050 error ("width of %qs exceeds its type", name);
4052 *width = build_int_cst (NULL_TREE, w);
4055 w = tree_low_cst (*width, 1);
4057 if (TREE_CODE (*type) == ENUMERAL_TYPE)
4059 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
4061 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
4062 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
4063 warning (0, "%qs is narrower than values of its type", name);
4069 /* Print warning about variable length array if necessary. */
4072 warn_variable_length_array (tree name, tree size)
4074 int const_size = TREE_CONSTANT (size);
4076 if (!flag_isoc99 && pedantic && warn_vla != 0)
4081 pedwarn (input_location, OPT_Wvla,
4082 "ISO C90 forbids array %qE whose size "
4083 "can%'t be evaluated",
4086 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array whose size "
4087 "can%'t be evaluated");
4092 pedwarn (input_location, OPT_Wvla,
4093 "ISO C90 forbids variable length array %qE",
4096 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array");
4099 else if (warn_vla > 0)
4105 "the size of array %qE can"
4106 "%'t be evaluated", name);
4109 "the size of array can %'t be evaluated");
4115 "variable length array %qE is used",
4119 "variable length array is used");
4124 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR to
4125 serve as the actual size-expression for a type or decl. This is
4126 like variable_size in stor-layout.c, but we make global_bindings_p
4127 return negative to avoid calls to that function from outside the
4128 front end resulting in errors at file scope, then call this version
4129 instead from front-end code. */
4132 c_variable_size (tree size)
4136 if (TREE_CONSTANT (size))
4139 size = save_expr (size);
4141 save = skip_simple_arithmetic (size);
4143 if (cfun && cfun->dont_save_pending_sizes_p)
4146 if (!global_bindings_p ())
4147 put_pending_size (save);
4152 /* Given declspecs and a declarator,
4153 determine the name and type of the object declared
4154 and construct a ..._DECL node for it.
4155 (In one case we can return a ..._TYPE node instead.
4156 For invalid input we sometimes return 0.)
4158 DECLSPECS is a c_declspecs structure for the declaration specifiers.
4160 DECL_CONTEXT says which syntactic context this declaration is in:
4161 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4162 FUNCDEF for a function definition. Like NORMAL but a few different
4163 error messages in each case. Return value may be zero meaning
4164 this definition is too screwy to try to parse.
4165 PARM for a parameter declaration (either within a function prototype
4166 or before a function body). Make a PARM_DECL, or return void_type_node.
4167 TYPENAME if for a typename (in a cast or sizeof).
4168 Don't make a DECL node; just return the ..._TYPE node.
4169 FIELD for a struct or union field; make a FIELD_DECL.
4170 INITIALIZED is true if the decl has an initializer.
4171 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4172 representing the width of the bit-field.
4173 DECL_ATTRS points to the list of attributes that should be added to this
4174 decl. Any nested attributes that belong on the decl itself will be
4176 If EXPR is not NULL, any expressions that need to be evaluated as
4177 part of evaluating variably modified types will be stored in *EXPR.
4178 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
4179 set to indicate whether operands in *EXPR can be used in constant
4181 DEPRECATED_STATE is a deprecated_states value indicating whether
4182 deprecation warnings should be suppressed.
4184 In the TYPENAME case, DECLARATOR is really an absolute declarator.
4185 It may also be so in the PARM case, for a prototype where the
4186 argument type is specified but not the name.
4188 This function is where the complicated C meanings of `static'
4189 and `extern' are interpreted. */
4192 grokdeclarator (const struct c_declarator *declarator,
4193 struct c_declspecs *declspecs,
4194 enum decl_context decl_context, bool initialized, tree *width,
4195 tree *decl_attrs, tree *expr, bool *expr_const_operands,
4196 enum deprecated_states deprecated_state)
4198 tree type = declspecs->type;
4199 bool threadp = declspecs->thread_p;
4200 enum c_storage_class storage_class = declspecs->storage_class;
4204 int type_quals = TYPE_UNQUALIFIED;
4205 tree name = NULL_TREE;
4206 bool funcdef_flag = false;
4207 bool funcdef_syntax = false;
4208 int size_varies = 0;
4209 tree decl_attr = declspecs->decl_attr;
4210 int array_ptr_quals = TYPE_UNQUALIFIED;
4211 tree array_ptr_attrs = NULL_TREE;
4212 int array_parm_static = 0;
4213 bool array_parm_vla_unspec_p = false;
4214 tree returned_attrs = NULL_TREE;
4215 bool bitfield = width != NULL;
4217 struct c_arg_info *arg_info = 0;
4218 location_t loc = UNKNOWN_LOCATION;
4221 bool expr_const_operands_dummy;
4225 if (expr_const_operands == NULL)
4226 expr_const_operands = &expr_const_operands_dummy;
4228 *expr = declspecs->expr;
4229 *expr_const_operands = declspecs->expr_const_operands;
4231 if (decl_context == FUNCDEF)
4232 funcdef_flag = true, decl_context = NORMAL;
4234 /* Look inside a declarator for the name being declared
4235 and get it as an IDENTIFIER_NODE, for an error message. */
4237 const struct c_declarator *decl = declarator;
4248 funcdef_syntax = (decl->kind == cdk_function);
4249 decl = decl->declarator;
4253 decl = decl->declarator;
4268 gcc_assert (decl_context == PARM
4269 || decl_context == TYPENAME
4270 || (decl_context == FIELD
4271 && declarator->kind == cdk_id));
4272 gcc_assert (!initialized);
4276 /* A function definition's declarator must have the form of
4277 a function declarator. */
4279 if (funcdef_flag && !funcdef_syntax)
4282 /* If this looks like a function definition, make it one,
4283 even if it occurs where parms are expected.
4284 Then store_parm_decls will reject it and not use it as a parm. */
4285 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4286 decl_context = PARM;
4288 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4289 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
4291 if ((decl_context == NORMAL || decl_context == FIELD)
4292 && current_scope == file_scope
4293 && variably_modified_type_p (type, NULL_TREE))
4296 error_at (loc, "variably modified %qE at file scope", name);
4298 error_at (loc, "variably modified field at file scope");
4299 type = integer_type_node;
4302 size_varies = C_TYPE_VARIABLE_SIZE (type);
4304 /* Diagnose defaulting to "int". */
4306 if (declspecs->default_int_p && !in_system_header)
4308 /* Issue a warning if this is an ISO C 99 program or if
4309 -Wreturn-type and this is a function, or if -Wimplicit;
4310 prefer the former warning since it is more explicit. */
4311 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4313 warn_about_return_type = 1;
4317 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wimplicit_int,
4318 "type defaults to %<int%> in declaration of %qE",
4321 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int,
4322 "type defaults to %<int%> in type name");
4326 /* Adjust the type if a bit-field is being declared,
4327 -funsigned-bitfields applied and the type is not explicitly
4329 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4330 && TREE_CODE (type) == INTEGER_TYPE)
4331 type = unsigned_type_for (type);
4333 /* Figure out the type qualifiers for the declaration. There are
4334 two ways a declaration can become qualified. One is something
4335 like `const int i' where the `const' is explicit. Another is
4336 something like `typedef const int CI; CI i' where the type of the
4337 declaration contains the `const'. A third possibility is that
4338 there is a type qualifier on the element type of a typedefed
4339 array type, in which case we should extract that qualifier so
4340 that c_apply_type_quals_to_decl receives the full list of
4341 qualifiers to work with (C90 is not entirely clear about whether
4342 duplicate qualifiers should be diagnosed in this case, but it
4343 seems most appropriate to do so). */
4344 element_type = strip_array_types (type);
4345 constp = declspecs->const_p + TYPE_READONLY (element_type);
4346 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4347 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4348 if (pedantic && !flag_isoc99)
4351 pedwarn (loc, OPT_pedantic, "duplicate %<const%>");
4353 pedwarn (loc, OPT_pedantic, "duplicate %<restrict%>");
4355 pedwarn (loc, OPT_pedantic, "duplicate %<volatile%>");
4357 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4358 type = TYPE_MAIN_VARIANT (type);
4359 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4360 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4361 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4363 /* Warn about storage classes that are invalid for certain
4364 kinds of declarations (parameters, typenames, etc.). */
4368 || storage_class == csc_auto
4369 || storage_class == csc_register
4370 || storage_class == csc_typedef))
4372 if (storage_class == csc_auto)
4374 (current_scope == file_scope) ? 0 : OPT_pedantic,
4375 "function definition declared %<auto%>");
4376 if (storage_class == csc_register)
4377 error_at (loc, "function definition declared %<register%>");
4378 if (storage_class == csc_typedef)
4379 error_at (loc, "function definition declared %<typedef%>");
4381 error_at (loc, "function definition declared %<__thread%>");
4383 if (storage_class == csc_auto
4384 || storage_class == csc_register
4385 || storage_class == csc_typedef)
4386 storage_class = csc_none;
4388 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4390 if (decl_context == PARM && storage_class == csc_register)
4394 switch (decl_context)
4398 error_at (loc, "storage class specified for structure "
4401 error_at (loc, "storage class specified for structure field");
4405 error_at (loc, "storage class specified for parameter %qE",
4408 error_at (loc, "storage class specified for unnamed parameter");
4411 error_at (loc, "storage class specified for typename");
4414 storage_class = csc_none;
4418 else if (storage_class == csc_extern
4422 /* 'extern' with initialization is invalid if not at file scope. */
4423 if (current_scope == file_scope)
4425 /* It is fine to have 'extern const' when compiling at C
4426 and C++ intersection. */
4427 if (!(warn_cxx_compat && constp))
4428 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
4432 error_at (loc, "%qE has both %<extern%> and initializer", name);
4434 else if (current_scope == file_scope)
4436 if (storage_class == csc_auto)
4437 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
4439 if (pedantic && storage_class == csc_register)
4440 pedwarn (input_location, OPT_pedantic,
4441 "file-scope declaration of %qE specifies %<register%>", name);
4445 if (storage_class == csc_extern && funcdef_flag)
4446 error_at (loc, "nested function %qE declared %<extern%>", name);
4447 else if (threadp && storage_class == csc_none)
4449 error_at (loc, "function-scope %qE implicitly auto and declared "
4456 /* Now figure out the structure of the declarator proper.
4457 Descend through it, creating more complex types, until we reach
4458 the declared identifier (or NULL_TREE, in an absolute declarator).
4459 At each stage we maintain an unqualified version of the type
4460 together with any qualifiers that should be applied to it with
4461 c_build_qualified_type; this way, array types including
4462 multidimensional array types are first built up in unqualified
4463 form and then the qualified form is created with
4464 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4466 while (declarator && declarator->kind != cdk_id)
4468 if (type == error_mark_node)
4470 declarator = declarator->declarator;
4474 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4475 a cdk_pointer (for *...),
4476 a cdk_function (for ...(...)),
4477 a cdk_attrs (for nested attributes),
4478 or a cdk_id (for the name being declared
4479 or the place in an absolute declarator
4480 where the name was omitted).
4481 For the last case, we have just exited the loop.
4483 At this point, TYPE is the type of elements of an array,
4484 or for a function to return, or for a pointer to point to.
4485 After this sequence of ifs, TYPE is the type of the
4486 array or function or pointer, and DECLARATOR has had its
4487 outermost layer removed. */
4489 if (array_ptr_quals != TYPE_UNQUALIFIED
4490 || array_ptr_attrs != NULL_TREE
4491 || array_parm_static)
4493 /* Only the innermost declarator (making a parameter be of
4494 array type which is converted to pointer type)
4495 may have static or type qualifiers. */
4496 error_at (loc, "static or type qualifiers in non-parameter array declarator");
4497 array_ptr_quals = TYPE_UNQUALIFIED;
4498 array_ptr_attrs = NULL_TREE;
4499 array_parm_static = 0;
4502 switch (declarator->kind)
4506 /* A declarator with embedded attributes. */
4507 tree attrs = declarator->u.attrs;
4508 const struct c_declarator *inner_decl;
4510 declarator = declarator->declarator;
4511 inner_decl = declarator;
4512 while (inner_decl->kind == cdk_attrs)
4513 inner_decl = inner_decl->declarator;
4514 if (inner_decl->kind == cdk_id)
4515 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4516 else if (inner_decl->kind == cdk_function)
4517 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4518 else if (inner_decl->kind == cdk_array)
4519 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4520 returned_attrs = decl_attributes (&type,
4521 chainon (returned_attrs, attrs),
4527 tree itype = NULL_TREE;
4528 tree size = declarator->u.array.dimen;
4529 /* The index is a signed object `sizetype' bits wide. */
4530 tree index_type = c_common_signed_type (sizetype);
4532 array_ptr_quals = declarator->u.array.quals;
4533 array_ptr_attrs = declarator->u.array.attrs;
4534 array_parm_static = declarator->u.array.static_p;
4535 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4537 declarator = declarator->declarator;
4539 /* Check for some types that there cannot be arrays of. */
4541 if (VOID_TYPE_P (type))
4544 error_at (loc, "declaration of %qE as array of voids", name);
4546 error_at (loc, "declaration of type name as array of voids");
4547 type = error_mark_node;
4550 if (TREE_CODE (type) == FUNCTION_TYPE)
4553 error_at (loc, "declaration of %qE as array of functions",
4556 error_at (loc, "declaration of type name as array of "
4558 type = error_mark_node;
4561 if (pedantic && !in_system_header && flexible_array_type_p (type))
4562 pedwarn (loc, OPT_pedantic,
4563 "invalid use of structure with flexible array member");
4565 if (size == error_mark_node)
4566 type = error_mark_node;
4568 if (type == error_mark_node)
4571 /* If size was specified, set ITYPE to a range-type for
4572 that size. Otherwise, ITYPE remains null. finish_decl
4573 may figure it out from an initial value. */
4577 bool size_maybe_const = true;
4578 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
4579 && !TREE_OVERFLOW (size));
4580 bool this_size_varies = false;
4582 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4584 STRIP_TYPE_NOPS (size);
4586 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4589 error_at (loc, "size of array %qE has non-integer type",
4593 "size of unnamed array has non-integer type");
4594 size = integer_one_node;
4597 size = c_fully_fold (size, false, &size_maybe_const);
4599 if (pedantic && size_maybe_const && integer_zerop (size))
4602 pedwarn (loc, OPT_pedantic,
4603 "ISO C forbids zero-size array %qE", name);
4605 pedwarn (loc, OPT_pedantic,
4606 "ISO C forbids zero-size array");
4609 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
4611 constant_expression_warning (size);
4612 if (tree_int_cst_sgn (size) < 0)
4615 error_at (loc, "size of array %qE is negative", name);
4617 error_at (loc, "size of unnamed array is negative");
4618 size = integer_one_node;
4620 /* Handle a size folded to an integer constant but
4621 not an integer constant expression. */
4622 if (!size_int_const)
4624 /* If this is a file scope declaration of an
4625 ordinary identifier, this is invalid code;
4626 diagnosing it here and not subsequently
4627 treating the type as variable-length avoids
4628 more confusing diagnostics later. */
4629 if ((decl_context == NORMAL || decl_context == FIELD)
4630 && current_scope == file_scope)
4631 pedwarn (input_location, 0,
4632 "variably modified %qE at file scope",
4635 this_size_varies = size_varies = 1;
4636 warn_variable_length_array (name, size);
4639 else if ((decl_context == NORMAL || decl_context == FIELD)
4640 && current_scope == file_scope)
4642 error_at (loc, "variably modified %qE at file scope", name);
4643 size = integer_one_node;
4647 /* Make sure the array size remains visibly
4648 nonconstant even if it is (eg) a const variable
4649 with known value. */
4650 this_size_varies = size_varies = 1;
4651 warn_variable_length_array (name, size);
4654 if (integer_zerop (size) && !this_size_varies)
4656 /* A zero-length array cannot be represented with
4657 an unsigned index type, which is what we'll
4658 get with build_index_type. Create an
4659 open-ended range instead. */
4660 itype = build_range_type (sizetype, size, NULL_TREE);
4664 /* Arrange for the SAVE_EXPR on the inside of the
4665 MINUS_EXPR, which allows the -1 to get folded
4666 with the +1 that happens when building TYPE_SIZE. */
4668 size = c_variable_size (size);
4669 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
4670 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
4671 integer_zero_node, size);
4673 /* Compute the maximum valid index, that is, size
4674 - 1. Do the calculation in index_type, so that
4675 if it is a variable the computations will be
4676 done in the proper mode. */
4677 itype = fold_build2 (MINUS_EXPR, index_type,
4678 convert (index_type, size),
4679 convert (index_type,
4682 /* If that overflowed, the array is too big. ???
4683 While a size of INT_MAX+1 technically shouldn't
4684 cause an overflow (because we subtract 1), the
4685 overflow is recorded during the conversion to
4686 index_type, before the subtraction. Handling
4687 this case seems like an unnecessary
4689 if (TREE_CODE (itype) == INTEGER_CST
4690 && TREE_OVERFLOW (itype))
4693 error_at (loc, "size of array %qE is too large",
4696 error_at (loc, "size of unnamed array is too large");
4697 type = error_mark_node;
4701 itype = build_index_type (itype);
4703 if (this_size_varies)
4706 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
4710 *expr_const_operands &= size_maybe_const;
4713 else if (decl_context == FIELD)
4715 bool flexible_array_member = false;
4716 if (array_parm_vla_unspec_p)
4717 /* Field names can in fact have function prototype
4718 scope so [*] is disallowed here through making
4719 the field variably modified, not through being
4720 something other than a declaration with function
4725 const struct c_declarator *t = declarator;
4726 while (t->kind == cdk_attrs)
4728 flexible_array_member = (t->kind == cdk_id);
4730 if (flexible_array_member
4731 && pedantic && !flag_isoc99 && !in_system_header)
4732 pedwarn (loc, OPT_pedantic,
4733 "ISO C90 does not support flexible array members");
4735 /* ISO C99 Flexible array members are effectively
4736 identical to GCC's zero-length array extension. */
4737 if (flexible_array_member || array_parm_vla_unspec_p)
4738 itype = build_range_type (sizetype, size_zero_node,
4741 else if (decl_context == PARM)
4743 if (array_parm_vla_unspec_p)
4745 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4749 else if (decl_context == TYPENAME)
4751 if (array_parm_vla_unspec_p)
4754 warning (0, "%<[*]%> not in a declaration");
4755 /* We use this to avoid messing up with incomplete
4756 array types of the same type, that would
4757 otherwise be modified below. */
4758 itype = build_range_type (sizetype, size_zero_node,
4764 /* Complain about arrays of incomplete types. */
4765 if (!COMPLETE_TYPE_P (type))
4767 error_at (loc, "array type has incomplete element type");
4768 type = error_mark_node;
4771 /* When itype is NULL, a shared incomplete array type is
4772 returned for all array of a given type. Elsewhere we
4773 make sure we don't complete that type before copying
4774 it, but here we want to make sure we don't ever
4775 modify the shared type, so we gcc_assert (itype)
4777 type = build_array_type (type, itype);
4779 if (type != error_mark_node)
4783 /* It is ok to modify type here even if itype is
4784 NULL: if size_varies, we're in a
4785 multi-dimensional array and the inner type has
4786 variable size, so the enclosing shared array type
4788 if (size && TREE_CODE (size) == INTEGER_CST)
4790 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4791 C_TYPE_VARIABLE_SIZE (type) = 1;
4794 /* The GCC extension for zero-length arrays differs from
4795 ISO flexible array members in that sizeof yields
4797 if (size && integer_zerop (size))
4800 TYPE_SIZE (type) = bitsize_zero_node;
4801 TYPE_SIZE_UNIT (type) = size_zero_node;
4803 if (array_parm_vla_unspec_p)
4806 /* The type is complete. C99 6.7.5.2p4 */
4807 TYPE_SIZE (type) = bitsize_zero_node;
4808 TYPE_SIZE_UNIT (type) = size_zero_node;
4812 if (decl_context != PARM
4813 && (array_ptr_quals != TYPE_UNQUALIFIED
4814 || array_ptr_attrs != NULL_TREE
4815 || array_parm_static))
4817 error_at (loc, "static or type qualifiers in non-parameter array declarator");
4818 array_ptr_quals = TYPE_UNQUALIFIED;
4819 array_ptr_attrs = NULL_TREE;
4820 array_parm_static = 0;
4826 /* Say it's a definition only for the declarator closest
4827 to the identifier, apart possibly from some
4829 bool really_funcdef = false;
4833 const struct c_declarator *t = declarator->declarator;
4834 while (t->kind == cdk_attrs)
4836 really_funcdef = (t->kind == cdk_id);
4839 /* Declaring a function type. Make sure we have a valid
4840 type for the function to return. */
4841 if (type == error_mark_node)
4846 /* Warn about some types functions can't return. */
4847 if (TREE_CODE (type) == FUNCTION_TYPE)
4850 error_at (loc, "%qE declared as function returning a "
4853 error_at (loc, "type name declared as function "
4854 "returning a function");
4855 type = integer_type_node;
4857 if (TREE_CODE (type) == ARRAY_TYPE)
4860 error_at (loc, "%qE declared as function returning an array",
4863 error_at (loc, "type name declared as function returning "
4865 type = integer_type_node;
4867 errmsg = targetm.invalid_return_type (type);
4871 type = integer_type_node;
4874 /* Construct the function type and go to the next
4875 inner layer of declarator. */
4876 arg_info = declarator->u.arg_info;
4877 arg_types = grokparms (arg_info, really_funcdef);
4879 put_pending_sizes (arg_info->pending_sizes);
4881 /* Type qualifiers before the return type of the function
4882 qualify the return type, not the function type. */
4885 /* Type qualifiers on a function return type are
4886 normally permitted by the standard but have no
4887 effect, so give a warning at -Wreturn-type.
4888 Qualifiers on a void return type are banned on
4889 function definitions in ISO C; GCC used to used
4890 them for noreturn functions. */
4891 if (VOID_TYPE_P (type) && really_funcdef)
4893 "function definition has qualified void return type");
4895 warning_at (loc, OPT_Wignored_qualifiers,
4896 "type qualifiers ignored on function return type");
4898 type = c_build_qualified_type (type, type_quals);
4900 type_quals = TYPE_UNQUALIFIED;
4902 type = build_function_type (type, arg_types);
4903 declarator = declarator->declarator;
4905 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4906 the formal parameter list of this FUNCTION_TYPE to point to
4907 the FUNCTION_TYPE node itself. */
4911 for (link = arg_info->tags;
4913 link = TREE_CHAIN (link))
4914 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4920 /* Merge any constancy or volatility into the target type
4923 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4925 pedwarn (loc, OPT_pedantic,
4926 "ISO C forbids qualified function types");
4928 type = c_build_qualified_type (type, type_quals);
4931 /* When the pointed-to type involves components of variable size,
4932 care must be taken to ensure that the size evaluation code is
4933 emitted early enough to dominate all the possible later uses
4934 and late enough for the variables on which it depends to have
4937 This is expected to happen automatically when the pointed-to
4938 type has a name/declaration of it's own, but special attention
4939 is required if the type is anonymous.
4941 We handle the NORMAL and FIELD contexts here by attaching an
4942 artificial TYPE_DECL to such pointed-to type. This forces the
4943 sizes evaluation at a safe point and ensures it is not deferred
4944 until e.g. within a deeper conditional context.
4946 We expect nothing to be needed here for PARM or TYPENAME.
4947 Pushing a TYPE_DECL at this point for TYPENAME would actually
4948 be incorrect, as we might be in the middle of an expression
4949 with side effects on the pointed-to type size "arguments" prior
4950 to the pointer declaration point and the fake TYPE_DECL in the
4951 enclosing context would force the size evaluation prior to the
4954 if (!TYPE_NAME (type)
4955 && (decl_context == NORMAL || decl_context == FIELD)
4956 && variably_modified_type_p (type, NULL_TREE))
4958 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
4959 DECL_ARTIFICIAL (decl) = 1;
4961 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
4962 TYPE_NAME (type) = decl;
4965 type = build_pointer_type (type);
4967 /* Process type qualifiers (such as const or volatile)
4968 that were given inside the `*'. */
4969 type_quals = declarator->u.pointer_quals;
4971 declarator = declarator->declarator;
4978 *decl_attrs = chainon (returned_attrs, *decl_attrs);
4980 /* Now TYPE has the actual type, apart from any qualifiers in
4983 /* Check the type and width of a bit-field. */
4985 check_bitfield_type_and_width (&type, width, name);
4987 /* Did array size calculations overflow? */
4989 if (TREE_CODE (type) == ARRAY_TYPE
4990 && COMPLETE_TYPE_P (type)
4991 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4992 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4995 error_at (loc, "size of array %qE is too large", name);
4997 error_at (loc, "size of unnamed array is too large");
4998 /* If we proceed with the array type as it is, we'll eventually
4999 crash in tree_low_cst(). */
5000 type = error_mark_node;
5003 /* If this is declaring a typedef name, return a TYPE_DECL. */
5005 if (storage_class == csc_typedef)
5008 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5010 pedwarn (loc, OPT_pedantic,
5011 "ISO C forbids qualified function types");
5013 type = c_build_qualified_type (type, type_quals);
5014 decl = build_decl (declarator->id_loc,
5015 TYPE_DECL, declarator->u.id, type);
5016 if (declspecs->explicit_signed_p)
5017 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
5018 if (declspecs->inline_p)
5019 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
5021 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
5023 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
5026 && b->decl != NULL_TREE
5027 && (B_IN_CURRENT_SCOPE (b)
5028 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
5029 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
5031 warning_at (declarator->id_loc, OPT_Wc___compat,
5032 ("using %qD as both a typedef and a tag is "
5035 if (b->locus != UNKNOWN_LOCATION)
5036 inform (b->locus, "originally defined here");
5043 /* If this is a type name (such as, in a cast or sizeof),
5044 compute the type and return it now. */
5046 if (decl_context == TYPENAME)
5048 /* Note that the grammar rejects storage classes in typenames
5050 gcc_assert (storage_class == csc_none && !threadp
5051 && !declspecs->inline_p);
5052 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5054 pedwarn (loc, OPT_pedantic,
5055 "ISO C forbids const or volatile function types");
5057 type = c_build_qualified_type (type, type_quals);
5061 if (pedantic && decl_context == FIELD
5062 && variably_modified_type_p (type, NULL_TREE))
5065 pedwarn (loc, OPT_pedantic, "a member of a structure or union cannot "
5066 "have a variably modified type");
5069 /* Aside from typedefs and type names (handle above),
5070 `void' at top level (not within pointer)
5071 is allowed only in public variables.
5072 We don't complain about parms either, but that is because
5073 a better error message can be made later. */
5075 if (VOID_TYPE_P (type) && decl_context != PARM
5076 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
5077 && (storage_class == csc_extern
5078 || (current_scope == file_scope
5079 && !(storage_class == csc_static
5080 || storage_class == csc_register)))))
5082 error_at (loc, "variable or field %qE declared void", name);
5083 type = integer_type_node;
5086 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
5087 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
5092 if (decl_context == PARM)
5096 /* A parameter declared as an array of T is really a pointer to T.
5097 One declared as a function is really a pointer to a function. */
5099 if (TREE_CODE (type) == ARRAY_TYPE)
5101 /* Transfer const-ness of array into that of type pointed to. */
5102 type = TREE_TYPE (type);
5104 type = c_build_qualified_type (type, type_quals);
5105 type = build_pointer_type (type);
5106 type_quals = array_ptr_quals;
5108 type = c_build_qualified_type (type, type_quals);
5110 /* We don't yet implement attributes in this context. */
5111 if (array_ptr_attrs != NULL_TREE)
5112 warning_at (loc, OPT_Wattributes,
5113 "attributes in parameter array declarator ignored");
5117 else if (TREE_CODE (type) == FUNCTION_TYPE)
5120 pedwarn (loc, OPT_pedantic,
5121 "ISO C forbids qualified function types");
5123 type = c_build_qualified_type (type, type_quals);
5124 type = build_pointer_type (type);
5125 type_quals = TYPE_UNQUALIFIED;
5127 else if (type_quals)
5128 type = c_build_qualified_type (type, type_quals);
5130 decl = build_decl (declarator->id_loc,
5131 PARM_DECL, declarator->u.id, type);
5133 C_DECL_VARIABLE_SIZE (decl) = 1;
5135 /* Compute the type actually passed in the parmlist,
5136 for the case where there is no prototype.
5137 (For example, shorts and chars are passed as ints.)
5138 When there is a prototype, this is overridden later. */
5140 if (type == error_mark_node)
5141 promoted_type = type;
5143 promoted_type = c_type_promotes_to (type);
5145 DECL_ARG_TYPE (decl) = promoted_type;
5146 if (declspecs->inline_p)
5147 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
5149 else if (decl_context == FIELD)
5151 /* Note that the grammar rejects storage classes in typenames
5153 gcc_assert (storage_class == csc_none && !threadp
5154 && !declspecs->inline_p);
5156 /* Structure field. It may not be a function. */
5158 if (TREE_CODE (type) == FUNCTION_TYPE)
5160 error_at (loc, "field %qE declared as a function", name);
5161 type = build_pointer_type (type);
5163 else if (TREE_CODE (type) != ERROR_MARK
5164 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
5167 error_at (loc, "field %qE has incomplete type", name);
5169 error_at (loc, "unnamed field has incomplete type");
5170 type = error_mark_node;
5172 type = c_build_qualified_type (type, type_quals);
5173 decl = build_decl (declarator->id_loc,
5174 FIELD_DECL, declarator->u.id, type);
5175 DECL_NONADDRESSABLE_P (decl) = bitfield;
5176 if (bitfield && !declarator->u.id)
5177 TREE_NO_WARNING (decl) = 1;
5180 C_DECL_VARIABLE_SIZE (decl) = 1;
5182 else if (TREE_CODE (type) == FUNCTION_TYPE)
5184 if (storage_class == csc_register || threadp)
5186 error_at (loc, "invalid storage class for function %qE", name);
5188 else if (current_scope != file_scope)
5190 /* Function declaration not at file scope. Storage
5191 classes other than `extern' are not allowed, C99
5192 6.7.1p5, and `extern' makes no difference. However,
5193 GCC allows 'auto', perhaps with 'inline', to support
5194 nested functions. */
5195 if (storage_class == csc_auto)
5196 pedwarn (loc, OPT_pedantic,
5197 "invalid storage class for function %qE", name);
5198 else if (storage_class == csc_static)
5200 error_at (loc, "invalid storage class for function %qE", name);
5202 storage_class = declspecs->storage_class = csc_none;
5208 decl = build_decl (declarator->id_loc,
5209 FUNCTION_DECL, declarator->u.id, type);
5210 decl = build_decl_attribute_variant (decl, decl_attr);
5212 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
5213 pedwarn (loc, OPT_pedantic,
5214 "ISO C forbids qualified function types");
5216 /* GNU C interprets a volatile-qualified function type to indicate
5217 that the function does not return. */
5218 if ((type_quals & TYPE_QUAL_VOLATILE)
5219 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5220 warning_at (loc, 0, "%<noreturn%> function returns non-void value");
5222 /* Every function declaration is an external reference
5223 (DECL_EXTERNAL) except for those which are not at file
5224 scope and are explicitly declared "auto". This is
5225 forbidden by standard C (C99 6.7.1p5) and is interpreted by
5226 GCC to signify a forward declaration of a nested function. */
5227 if (storage_class == csc_auto && current_scope != file_scope)
5228 DECL_EXTERNAL (decl) = 0;
5229 /* In C99, a function which is declared 'inline' with 'extern'
5230 is not an external reference (which is confusing). It
5231 means that the later definition of the function must be output
5232 in this file, C99 6.7.4p6. In GNU C89, a function declared
5233 'extern inline' is an external reference. */
5234 else if (declspecs->inline_p && storage_class != csc_static)
5235 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
5236 == flag_gnu89_inline);
5238 DECL_EXTERNAL (decl) = !initialized;
5240 /* Record absence of global scope for `static' or `auto'. */
5242 = !(storage_class == csc_static || storage_class == csc_auto);
5244 /* For a function definition, record the argument information
5245 block where store_parm_decls will look for it. */
5247 current_function_arg_info = arg_info;
5249 if (declspecs->default_int_p)
5250 C_FUNCTION_IMPLICIT_INT (decl) = 1;
5252 /* Record presence of `inline', if it is reasonable. */
5253 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
5255 if (declspecs->inline_p)
5256 pedwarn (loc, 0, "cannot inline function %<main%>");
5258 else if (declspecs->inline_p)
5259 /* Record that the function is declared `inline'. */
5260 DECL_DECLARED_INLINE_P (decl) = 1;
5264 /* It's a variable. */
5265 /* An uninitialized decl with `extern' is a reference. */
5266 int extern_ref = !initialized && storage_class == csc_extern;
5268 type = c_build_qualified_type (type, type_quals);
5270 /* C99 6.2.2p7: It is invalid (compile-time undefined
5271 behavior) to create an 'extern' declaration for a
5272 variable if there is a global declaration that is
5273 'static' and the global declaration is not visible.
5274 (If the static declaration _is_ currently visible,
5275 the 'extern' declaration is taken to refer to that decl.) */
5276 if (extern_ref && current_scope != file_scope)
5278 tree global_decl = identifier_global_value (declarator->u.id);
5279 tree visible_decl = lookup_name (declarator->u.id);
5282 && global_decl != visible_decl
5283 && TREE_CODE (global_decl) == VAR_DECL
5284 && !TREE_PUBLIC (global_decl))
5285 error_at (loc, "variable previously declared %<static%> "
5286 "redeclared %<extern%>");
5289 decl = build_decl (declarator->id_loc,
5290 VAR_DECL, declarator->u.id, type);
5292 C_DECL_VARIABLE_SIZE (decl) = 1;
5294 if (declspecs->inline_p)
5295 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
5297 /* At file scope, an initialized extern declaration may follow
5298 a static declaration. In that case, DECL_EXTERNAL will be
5299 reset later in start_decl. */
5300 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
5302 /* At file scope, the presence of a `static' or `register' storage
5303 class specifier, or the absence of all storage class specifiers
5304 makes this declaration a definition (perhaps tentative). Also,
5305 the absence of `static' makes it public. */
5306 if (current_scope == file_scope)
5308 TREE_PUBLIC (decl) = storage_class != csc_static;
5309 TREE_STATIC (decl) = !extern_ref;
5311 /* Not at file scope, only `static' makes a static definition. */
5314 TREE_STATIC (decl) = (storage_class == csc_static);
5315 TREE_PUBLIC (decl) = extern_ref;
5319 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
5322 if ((storage_class == csc_extern
5323 || (storage_class == csc_none
5324 && TREE_CODE (type) == FUNCTION_TYPE
5326 && variably_modified_type_p (type, NULL_TREE))
5329 if (TREE_CODE (type) == FUNCTION_TYPE)
5330 error_at (loc, "non-nested function with variably modified type");
5332 error_at (loc, "object with variably modified type must have "
5336 /* Record `register' declaration for warnings on &
5337 and in case doing stupid register allocation. */
5339 if (storage_class == csc_register)
5341 C_DECL_REGISTER (decl) = 1;
5342 DECL_REGISTER (decl) = 1;
5345 /* Record constancy and volatility. */
5346 c_apply_type_quals_to_decl (type_quals, decl);
5348 /* If a type has volatile components, it should be stored in memory.
5349 Otherwise, the fact that those components are volatile
5350 will be ignored, and would even crash the compiler.
5351 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
5352 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
5353 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
5354 || TREE_CODE (decl) == RESULT_DECL))
5356 /* It is not an error for a structure with volatile fields to
5357 be declared register, but reset DECL_REGISTER since it
5358 cannot actually go in a register. */
5359 int was_reg = C_DECL_REGISTER (decl);
5360 C_DECL_REGISTER (decl) = 0;
5361 DECL_REGISTER (decl) = 0;
5362 c_mark_addressable (decl);
5363 C_DECL_REGISTER (decl) = was_reg;
5366 /* This is the earliest point at which we might know the assembler
5367 name of a variable. Thus, if it's known before this, die horribly. */
5368 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
5374 /* Decode the parameter-list info for a function type or function definition.
5375 The argument is the value returned by `get_parm_info' (or made in c-parse.c
5376 if there is an identifier list instead of a parameter decl list).
5377 These two functions are separate because when a function returns
5378 or receives functions then each is called multiple times but the order
5379 of calls is different. The last call to `grokparms' is always the one
5380 that contains the formal parameter names of a function definition.
5382 Return a list of arg types to use in the FUNCTION_TYPE for this function.
5384 FUNCDEF_FLAG is true for a function definition, false for
5385 a mere declaration. A nonempty identifier-list gets an error message
5386 when FUNCDEF_FLAG is false. */
5389 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
5391 tree arg_types = arg_info->types;
5393 if (funcdef_flag && arg_info->had_vla_unspec)
5395 /* A function definition isn't function prototype scope C99 6.2.1p4. */
5397 error ("%<[*]%> not allowed in other than function prototype scope");
5400 if (arg_types == 0 && !funcdef_flag && !in_system_header)
5401 warning (OPT_Wstrict_prototypes,
5402 "function declaration isn%'t a prototype");
5404 if (arg_types == error_mark_node)
5405 return 0; /* don't set TYPE_ARG_TYPES in this case */
5407 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5410 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
5412 arg_info->parms = arg_info->types;
5413 arg_info->types = 0;
5418 tree parm, type, typelt;
5419 unsigned int parmno;
5422 /* If there is a parameter of incomplete type in a definition,
5423 this is an error. In a declaration this is valid, and a
5424 struct or union type may be completed later, before any calls
5425 or definition of the function. In the case where the tag was
5426 first declared within the parameter list, a warning has
5427 already been given. If a parameter has void type, then
5428 however the function cannot be defined or called, so
5431 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5433 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5435 type = TREE_VALUE (typelt);
5436 if (type == error_mark_node)
5439 if (!COMPLETE_TYPE_P (type))
5443 if (DECL_NAME (parm))
5444 error ("parameter %u (%q+D) has incomplete type",
5447 error ("%Jparameter %u has incomplete type",
5450 TREE_VALUE (typelt) = error_mark_node;
5451 TREE_TYPE (parm) = error_mark_node;
5453 else if (VOID_TYPE_P (type))
5455 if (DECL_NAME (parm))
5456 warning (0, "parameter %u (%q+D) has void type",
5459 warning (0, "%Jparameter %u has void type",
5464 errmsg = targetm.invalid_parameter_type (type);
5468 TREE_VALUE (typelt) = error_mark_node;
5469 TREE_TYPE (parm) = error_mark_node;
5472 if (DECL_NAME (parm) && TREE_USED (parm))
5473 warn_if_shadowing (parm);
5479 /* Take apart the current scope and return a c_arg_info structure with
5480 info on a parameter list just parsed.
5482 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5484 ELLIPSIS being true means the argument list ended in '...' so don't
5485 append a sentinel (void_list_node) to the end of the type-list. */
5488 get_parm_info (bool ellipsis)
5490 struct c_binding *b = current_scope->bindings;
5491 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5498 static bool explained_incomplete_types = false;
5499 bool gave_void_only_once_err = false;
5501 arg_info->parms = 0;
5503 arg_info->types = 0;
5504 arg_info->others = 0;
5505 arg_info->pending_sizes = 0;
5506 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5508 /* The bindings in this scope must not get put into a block.
5509 We will take care of deleting the binding nodes. */
5510 current_scope->bindings = 0;
5512 /* This function is only called if there was *something* on the
5516 /* A parameter list consisting solely of 'void' indicates that the
5517 function takes no arguments. But if the 'void' is qualified
5518 (by 'const' or 'volatile'), or has a storage class specifier
5519 ('register'), then the behavior is undefined; issue an error.
5520 Typedefs for 'void' are OK (see DR#157). */
5521 if (b->prev == 0 /* one binding */
5522 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5523 && !DECL_NAME (b->decl) /* anonymous */
5524 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5526 if (TREE_THIS_VOLATILE (b->decl)
5527 || TREE_READONLY (b->decl)
5528 || C_DECL_REGISTER (b->decl))
5529 error ("%<void%> as only parameter may not be qualified");
5531 /* There cannot be an ellipsis. */
5533 error ("%<void%> must be the only parameter");
5535 arg_info->types = void_list_node;
5540 types = void_list_node;
5542 /* Break up the bindings list into parms, tags, types, and others;
5543 apply sanity checks; purge the name-to-decl bindings. */
5546 tree decl = b->decl;
5547 tree type = TREE_TYPE (decl);
5548 const char *keyword;
5550 switch (TREE_CODE (decl))
5555 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5556 I_SYMBOL_BINDING (b->id) = b->shadowed;
5559 /* Check for forward decls that never got their actual decl. */
5560 if (TREE_ASM_WRITTEN (decl))
5561 error ("parameter %q+D has just a forward declaration", decl);
5562 /* Check for (..., void, ...) and issue an error. */
5563 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5565 if (!gave_void_only_once_err)
5567 error ("%<void%> must be the only parameter");
5568 gave_void_only_once_err = true;
5573 /* Valid parameter, add it to the list. */
5574 TREE_CHAIN (decl) = parms;
5577 /* Since there is a prototype, args are passed in their
5578 declared types. The back end may override this later. */
5579 DECL_ARG_TYPE (decl) = type;
5580 types = tree_cons (0, type, types);
5584 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5585 case UNION_TYPE: keyword = "union"; goto tag;
5586 case RECORD_TYPE: keyword = "struct"; goto tag;
5588 /* Types may not have tag-names, in which case the type
5589 appears in the bindings list with b->id NULL. */
5592 gcc_assert (I_TAG_BINDING (b->id) == b);
5593 I_TAG_BINDING (b->id) = b->shadowed;
5596 /* Warn about any struct, union or enum tags defined in a
5597 parameter list. The scope of such types is limited to
5598 the parameter list, which is rarely if ever desirable
5599 (it's impossible to call such a function with type-
5600 correct arguments). An anonymous union parm type is
5601 meaningful as a GNU extension, so don't warn for that. */
5602 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5605 /* The %s will be one of 'struct', 'union', or 'enum'. */
5606 warning (0, "%<%s %E%> declared inside parameter list",
5609 /* The %s will be one of 'struct', 'union', or 'enum'. */
5610 warning (0, "anonymous %s declared inside parameter list",
5613 if (!explained_incomplete_types)
5615 warning (0, "its scope is only this definition or declaration,"
5616 " which is probably not what you want");
5617 explained_incomplete_types = true;
5621 tags = tree_cons (b->id, decl, tags);
5627 /* CONST_DECLs appear here when we have an embedded enum,
5628 and TYPE_DECLs appear here when we have an embedded struct
5629 or union. No warnings for this - we already warned about the
5630 type itself. FUNCTION_DECLs appear when there is an implicit
5631 function declaration in the parameter list. */
5633 TREE_CHAIN (decl) = others;
5638 /* error_mark_node appears here when we have an undeclared
5639 variable. Just throw it away. */
5642 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5643 I_SYMBOL_BINDING (b->id) = b->shadowed;
5647 /* Other things that might be encountered. */
5654 b = free_binding_and_advance (b);
5657 arg_info->parms = parms;
5658 arg_info->tags = tags;
5659 arg_info->types = types;
5660 arg_info->others = others;
5661 arg_info->pending_sizes = get_pending_sizes ();
5665 /* Get the struct, enum or union (CODE says which) with tag NAME.
5666 Define the tag as a forward-reference with location LOC if it is
5667 not defined. Return a c_typespec structure for the type
5671 parser_xref_tag (location_t loc, enum tree_code code, tree name)
5673 struct c_typespec ret;
5677 ret.expr = NULL_TREE;
5678 ret.expr_const_operands = true;
5680 /* If a cross reference is requested, look up the type
5681 already defined for this tag and return it. */
5683 ref = lookup_tag (code, name, 0, &refloc);
5684 /* If this is the right type of tag, return what we found.
5685 (This reference will be shadowed by shadow_tag later if appropriate.)
5686 If this is the wrong type of tag, do not return it. If it was the
5687 wrong type in the same scope, we will have had an error
5688 message already; if in a different scope and declaring
5689 a name, pending_xref_error will give an error message; but if in a
5690 different scope and not declaring a name, this tag should
5691 shadow the previous declaration of a different type of tag, and
5692 this would not work properly if we return the reference found.
5693 (For example, with "struct foo" in an outer scope, "union foo;"
5694 must shadow that tag with a new one of union type.) */
5695 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5696 if (ref && TREE_CODE (ref) == code)
5698 if (C_TYPE_DEFINED_IN_STRUCT (ref)
5699 && loc != UNKNOWN_LOCATION
5705 warning_at (loc, OPT_Wc___compat,
5706 ("enum type defined in struct or union "
5707 "is not visible in C++"));
5708 inform (refloc, "enum type defined here");
5711 warning_at (loc, OPT_Wc___compat,
5712 ("struct defined in struct or union "
5713 "is not visible in C++"));
5714 inform (refloc, "struct defined here");
5717 warning_at (loc, OPT_Wc___compat,
5718 ("union defined in struct or union "
5719 "is not visible in C++"));
5720 inform (refloc, "union defined here");
5731 /* If no such tag is yet defined, create a forward-reference node
5732 and record it as the "definition".
5733 When a real declaration of this type is found,
5734 the forward-reference will be altered into a real type. */
5736 ref = make_node (code);
5737 if (code == ENUMERAL_TYPE)
5739 /* Give the type a default layout like unsigned int
5740 to avoid crashing if it does not get defined. */
5741 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
5742 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5743 TYPE_USER_ALIGN (ref) = 0;
5744 TYPE_UNSIGNED (ref) = 1;
5745 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5746 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5747 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5750 pushtag (loc, name, ref);
5756 /* Get the struct, enum or union (CODE says which) with tag NAME.
5757 Define the tag as a forward-reference if it is not defined.
5758 Return a tree for the type. */
5761 xref_tag (enum tree_code code, tree name)
5763 return parser_xref_tag (input_location, code, name).spec;
5766 /* Make sure that the tag NAME is defined *in the current scope*
5767 at least as a forward reference.
5768 LOC is the location of the struct's definition.
5769 CODE says which kind of tag NAME ought to be.
5771 This stores the current value of the file static IN_STRUCT in
5772 *ENCLOSING_IN_STRUCT, and sets IN_STRUCT to true. Similarly, this
5773 sets STRUCT_TYPES in *ENCLOSING_STRUCT_TYPES, and sets STRUCT_TYPES
5774 to an empty vector. The old values are restored in
5778 start_struct (location_t loc, enum tree_code code, tree name,
5779 bool *enclosing_in_struct,
5780 VEC(tree,heap) **enclosing_struct_types)
5782 /* If there is already a tag defined at this scope
5783 (as a forward reference), just return it. */
5785 tree ref = NULL_TREE;
5786 location_t refloc = UNKNOWN_LOCATION;
5788 if (name != NULL_TREE)
5789 ref = lookup_tag (code, name, 1, &refloc);
5790 if (ref && TREE_CODE (ref) == code)
5792 if (TYPE_SIZE (ref))
5794 if (code == UNION_TYPE)
5795 error_at (loc, "redefinition of %<union %E%>", name);
5797 error_at (loc, "redefinition of %<struct %E%>", name);
5798 if (refloc != UNKNOWN_LOCATION)
5799 inform (refloc, "originally defined here");
5800 /* Don't create structures using a name already in use. */
5803 else if (C_TYPE_BEING_DEFINED (ref))
5805 if (code == UNION_TYPE)
5806 error_at (loc, "nested redefinition of %<union %E%>", name);
5808 error_at (loc, "nested redefinition of %<struct %E%>", name);
5809 /* Don't bother to report "originally defined here" for a
5810 nested redefinition; the original definition should be
5812 /* Don't create structures that contain themselves. */
5817 /* Otherwise create a forward-reference just so the tag is in scope. */
5819 if (ref == NULL_TREE || TREE_CODE (ref) != code)
5821 ref = make_node (code);
5822 pushtag (loc, name, ref);
5825 C_TYPE_BEING_DEFINED (ref) = 1;
5826 TYPE_PACKED (ref) = flag_pack_struct;
5828 *enclosing_in_struct = in_struct;
5829 *enclosing_struct_types = struct_types;
5831 struct_types = VEC_alloc(tree, heap, 0);
5833 /* FIXME: This will issue a warning for a use of a type defined
5834 within a statement expr used within sizeof, et. al. This is not
5835 terribly serious as C++ doesn't permit statement exprs within
5837 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
5838 warning_at (loc, OPT_Wc___compat,
5839 "defining type in %qs expression is invalid in C++",
5842 : (in_typeof ? "typeof" : "alignof")));
5847 /* Process the specs, declarator and width (NULL if omitted)
5848 of a structure component, returning a FIELD_DECL node.
5849 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5850 DECL_ATTRS is as for grokdeclarator.
5852 LOC is the location of the structure component.
5854 This is done during the parsing of the struct declaration.
5855 The FIELD_DECL nodes are chained together and the lot of them
5856 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5859 grokfield (location_t loc,
5860 struct c_declarator *declarator, struct c_declspecs *declspecs,
5861 tree width, tree *decl_attrs)
5865 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5866 && width == NULL_TREE)
5868 /* This is an unnamed decl.
5870 If we have something of the form "union { list } ;" then this
5871 is the anonymous union extension. Similarly for struct.
5873 If this is something of the form "struct foo;", then
5874 If MS extensions are enabled, this is handled as an
5876 Otherwise this is a forward declaration of a structure tag.
5878 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5879 If MS extensions are enabled and foo names a structure, then
5880 again this is an anonymous struct.
5881 Otherwise this is an error.
5883 Oh what a horrid tangled web we weave. I wonder if MS consciously
5884 took this from Plan 9 or if it was an accident of implementation
5885 that took root before someone noticed the bug... */
5887 tree type = declspecs->type;
5888 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5889 || TREE_CODE (type) == UNION_TYPE);
5893 && (flag_ms_extensions || !declspecs->typedef_p))
5895 if (flag_ms_extensions)
5899 else if (TYPE_NAME (type) == NULL)
5906 pedwarn (loc, 0, "declaration does not declare anything");
5909 pedwarn (loc, OPT_pedantic, "ISO C doesn%'t support unnamed structs/unions");
5912 value = grokdeclarator (declarator, declspecs, FIELD, false,
5913 width ? &width : NULL, decl_attrs, NULL, NULL,
5916 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
5917 DECL_INITIAL (value) = width;
5922 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5923 the list such that this does not present a problem later. */
5926 detect_field_duplicates (tree fieldlist)
5931 /* First, see if there are more than "a few" fields.
5932 This is trivially true if there are zero or one fields. */
5935 x = TREE_CHAIN (fieldlist);
5941 } while (timeout > 0 && x);
5943 /* If there were "few" fields, avoid the overhead of allocating
5944 a hash table. Instead just do the nested traversal thing. */
5947 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5950 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5951 if (DECL_NAME (y) == DECL_NAME (x))
5953 error ("duplicate member %q+D", x);
5954 DECL_NAME (x) = NULL_TREE;
5960 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5963 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5964 if ((y = DECL_NAME (x)) != 0)
5966 slot = htab_find_slot (htab, y, INSERT);
5969 error ("duplicate member %q+D", x);
5970 DECL_NAME (x) = NULL_TREE;
5979 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5980 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
5981 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5982 ATTRIBUTES are attributes to be applied to the structure.
5984 ENCLOSING_IN_STRUCT is the value of IN_STRUCT, and
5985 ENCLOSING_STRUCT_TYPES is the value of STRUCT_TYPES, when the
5986 struct was started. This sets the C_TYPE_DEFINED_IN_STRUCT flag
5987 for any type defined in the current struct. */
5990 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
5991 bool enclosing_in_struct,
5992 VEC(tree,heap) *enclosing_struct_types)
5995 bool toplevel = file_scope == current_scope;
5996 int saw_named_field;
5999 /* If this type was previously laid out as a forward reference,
6000 make sure we lay it out again. */
6004 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6008 for (x = fieldlist; x; x = TREE_CHAIN (x))
6009 if (DECL_NAME (x) != 0)
6014 if (TREE_CODE (t) == UNION_TYPE)
6017 pedwarn (loc, OPT_pedantic, "union has no named members");
6019 pedwarn (loc, OPT_pedantic, "union has no members");
6024 pedwarn (loc, OPT_pedantic, "struct has no named members");
6026 pedwarn (loc, OPT_pedantic, "struct has no members");
6031 /* Install struct as DECL_CONTEXT of each field decl.
6032 Also process specified field sizes, found in the DECL_INITIAL,
6033 storing 0 there after the type has been changed to precision equal
6034 to its width, rather than the precision of the specified standard
6035 type. (Correct layout requires the original type to have been preserved
6038 saw_named_field = 0;
6039 for (x = fieldlist; x; x = TREE_CHAIN (x))
6041 if (TREE_TYPE (x) == error_mark_node)
6044 DECL_CONTEXT (x) = t;
6046 /* If any field is const, the structure type is pseudo-const. */
6047 if (TREE_READONLY (x))
6048 C_TYPE_FIELDS_READONLY (t) = 1;
6051 /* A field that is pseudo-const makes the structure likewise. */
6052 tree t1 = TREE_TYPE (x);
6053 while (TREE_CODE (t1) == ARRAY_TYPE)
6054 t1 = TREE_TYPE (t1);
6055 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
6056 && C_TYPE_FIELDS_READONLY (t1))
6057 C_TYPE_FIELDS_READONLY (t) = 1;
6060 /* Any field that is volatile means variables of this type must be
6061 treated in some ways as volatile. */
6062 if (TREE_THIS_VOLATILE (x))
6063 C_TYPE_FIELDS_VOLATILE (t) = 1;
6065 /* Any field of nominal variable size implies structure is too. */
6066 if (C_DECL_VARIABLE_SIZE (x))
6067 C_TYPE_VARIABLE_SIZE (t) = 1;
6069 if (DECL_INITIAL (x))
6071 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
6072 DECL_SIZE (x) = bitsize_int (width);
6073 DECL_BIT_FIELD (x) = 1;
6074 SET_DECL_C_BIT_FIELD (x);
6078 && (DECL_BIT_FIELD (x)
6079 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
6080 DECL_PACKED (x) = 1;
6082 /* Detect flexible array member in an invalid context. */
6083 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6084 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
6085 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
6086 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
6088 if (TREE_CODE (t) == UNION_TYPE)
6090 error_at (DECL_SOURCE_LOCATION (x),
6091 "%Jflexible array member in union", x);
6092 TREE_TYPE (x) = error_mark_node;
6094 else if (TREE_CHAIN (x) != NULL_TREE)
6096 error_at (DECL_SOURCE_LOCATION (x),
6097 "%Jflexible array member not at end of struct", x);
6098 TREE_TYPE (x) = error_mark_node;
6100 else if (!saw_named_field)
6102 error_at (DECL_SOURCE_LOCATION (x),
6103 "%Jflexible array member in otherwise empty struct",
6105 TREE_TYPE (x) = error_mark_node;
6109 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
6110 && flexible_array_type_p (TREE_TYPE (x)))
6111 pedwarn (loc, OPT_pedantic,
6112 "%Jinvalid use of structure with flexible array member", x);
6115 saw_named_field = 1;
6118 detect_field_duplicates (fieldlist);
6120 /* Now we have the nearly final fieldlist. Record it,
6121 then lay out the structure or union (including the fields). */
6123 TYPE_FIELDS (t) = fieldlist;
6127 /* Give bit-fields their proper types. */
6129 tree *fieldlistp = &fieldlist;
6131 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
6132 && TREE_TYPE (*fieldlistp) != error_mark_node)
6134 unsigned HOST_WIDE_INT width
6135 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
6136 tree type = TREE_TYPE (*fieldlistp);
6137 if (width != TYPE_PRECISION (type))
6139 TREE_TYPE (*fieldlistp)
6140 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
6141 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
6143 DECL_INITIAL (*fieldlistp) = 0;
6146 fieldlistp = &TREE_CHAIN (*fieldlistp);
6149 /* Now we have the truly final field list.
6150 Store it in this type and in the variants. */
6152 TYPE_FIELDS (t) = fieldlist;
6154 /* If there are lots of fields, sort so we can look through them fast.
6155 We arbitrarily consider 16 or more elts to be "a lot". */
6160 for (x = fieldlist; x; x = TREE_CHAIN (x))
6162 if (len > 15 || DECL_NAME (x) == NULL)
6170 struct lang_type *space;
6171 struct sorted_fields_type *space2;
6173 len += list_length (x);
6175 /* Use the same allocation policy here that make_node uses, to
6176 ensure that this lives as long as the rest of the struct decl.
6177 All decls in an inline function need to be saved. */
6179 space = GGC_CNEW (struct lang_type);
6180 space2 = GGC_NEWVAR (struct sorted_fields_type,
6181 sizeof (struct sorted_fields_type) + len * sizeof (tree));
6185 field_array = &space2->elts[0];
6186 for (x = fieldlist; x; x = TREE_CHAIN (x))
6188 field_array[len++] = x;
6190 /* If there is anonymous struct or union, break out of the loop. */
6191 if (DECL_NAME (x) == NULL)
6194 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
6197 TYPE_LANG_SPECIFIC (t) = space;
6198 TYPE_LANG_SPECIFIC (t)->s->len = len;
6199 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
6200 qsort (field_array, len, sizeof (tree), field_decl_cmp);
6205 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6207 TYPE_FIELDS (x) = TYPE_FIELDS (t);
6208 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
6209 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
6210 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
6211 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
6214 /* If this was supposed to be a transparent union, but we can't
6215 make it one, warn and turn off the flag. */
6216 if (TREE_CODE (t) == UNION_TYPE
6217 && TYPE_TRANSPARENT_UNION (t)
6218 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
6220 TYPE_TRANSPARENT_UNION (t) = 0;
6221 warning_at (loc, 0, "union cannot be made transparent");
6224 /* If this structure or union completes the type of any previous
6225 variable declaration, lay it out and output its rtl. */
6226 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
6230 tree decl = TREE_VALUE (x);
6231 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6232 layout_array_type (TREE_TYPE (decl));
6233 if (TREE_CODE (decl) != TYPE_DECL)
6235 layout_decl (decl, 0);
6236 if (c_dialect_objc ())
6237 objc_check_decl (decl);
6238 rest_of_decl_compilation (decl, toplevel, 0);
6243 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
6245 /* Finish debugging output for this type. */
6246 rest_of_type_compilation (t, toplevel);
6248 /* If we're inside a function proper, i.e. not file-scope and not still
6249 parsing parameters, then arrange for the size of a variable sized type
6251 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
6252 add_stmt (build_stmt (loc,
6253 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
6255 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
6256 the current struct. We do this now at the end of the struct
6257 because the flag is used to issue visibility warnings when using
6258 -Wc++-compat, and we only want to issue those warnings if the
6259 type is referenced outside of the struct declaration. */
6260 for (ix = 0; VEC_iterate (tree, struct_types, ix, x); ++ix)
6261 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
6263 VEC_free (tree, heap, struct_types);
6265 in_struct = enclosing_in_struct;
6266 struct_types = enclosing_struct_types;
6268 /* If this struct is defined inside a struct, add it to
6270 if (in_struct && !in_sizeof && !in_typeof && !in_alignof)
6271 VEC_safe_push (tree, heap, struct_types, t);
6276 /* Lay out the type T, and its element type, and so on. */
6279 layout_array_type (tree t)
6281 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6282 layout_array_type (TREE_TYPE (t));
6286 /* Begin compiling the definition of an enumeration type.
6287 NAME is its name (or null if anonymous).
6288 LOC is the enum's location.
6289 Returns the type object, as yet incomplete.
6290 Also records info about it so that build_enumerator
6291 may be used to declare the individual values as they are read. */
6294 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
6296 tree enumtype = NULL_TREE;
6297 location_t enumloc = UNKNOWN_LOCATION;
6299 /* If this is the real definition for a previous forward reference,
6300 fill in the contents in the same object that used to be the
6301 forward reference. */
6303 if (name != NULL_TREE)
6304 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1, &enumloc);
6306 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
6308 enumtype = make_node (ENUMERAL_TYPE);
6309 pushtag (loc, name, enumtype);
6312 if (C_TYPE_BEING_DEFINED (enumtype))
6313 error_at (loc, "nested redefinition of %<enum %E%>", name);
6315 C_TYPE_BEING_DEFINED (enumtype) = 1;
6317 if (TYPE_VALUES (enumtype) != 0)
6319 /* This enum is a named one that has been declared already. */
6320 error_at (loc, "redeclaration of %<enum %E%>", name);
6321 if (enumloc != UNKNOWN_LOCATION)
6322 inform (enumloc, "originally defined here");
6324 /* Completely replace its old definition.
6325 The old enumerators remain defined, however. */
6326 TYPE_VALUES (enumtype) = 0;
6329 the_enum->enum_next_value = integer_zero_node;
6330 the_enum->enum_overflow = 0;
6332 if (flag_short_enums)
6333 TYPE_PACKED (enumtype) = 1;
6335 /* FIXME: This will issue a warning for a use of a type defined
6336 within sizeof in a statement expr. This is not terribly serious
6337 as C++ doesn't permit statement exprs within sizeof anyhow. */
6338 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
6339 warning_at (loc, OPT_Wc___compat,
6340 "defining type in %qs expression is invalid in C++",
6343 : (in_typeof ? "typeof" : "alignof")));
6348 /* After processing and defining all the values of an enumeration type,
6349 install their decls in the enumeration type and finish it off.
6350 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
6351 and ATTRIBUTES are the specified attributes.
6352 Returns ENUMTYPE. */
6355 finish_enum (tree enumtype, tree values, tree attributes)
6358 tree minnode = 0, maxnode = 0;
6359 int precision, unsign;
6360 bool toplevel = (file_scope == current_scope);
6361 struct lang_type *lt;
6363 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6365 /* Calculate the maximum value of any enumerator in this type. */
6367 if (values == error_mark_node)
6368 minnode = maxnode = integer_zero_node;
6371 minnode = maxnode = TREE_VALUE (values);
6372 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
6374 tree value = TREE_VALUE (pair);
6375 if (tree_int_cst_lt (maxnode, value))
6377 if (tree_int_cst_lt (value, minnode))
6382 /* Construct the final type of this enumeration. It is the same
6383 as one of the integral types - the narrowest one that fits, except
6384 that normally we only go as narrow as int - and signed iff any of
6385 the values are negative. */
6386 unsign = (tree_int_cst_sgn (minnode) >= 0);
6387 precision = MAX (tree_int_cst_min_precision (minnode, unsign),
6388 tree_int_cst_min_precision (maxnode, unsign));
6390 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
6392 tem = c_common_type_for_size (precision, unsign);
6395 warning (0, "enumeration values exceed range of largest integer");
6396 tem = long_long_integer_type_node;
6400 tem = unsign ? unsigned_type_node : integer_type_node;
6402 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
6403 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
6404 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
6405 TYPE_SIZE (enumtype) = 0;
6407 /* If the precision of the type was specific with an attribute and it
6408 was too small, give an error. Otherwise, use it. */
6409 if (TYPE_PRECISION (enumtype))
6411 if (precision > TYPE_PRECISION (enumtype))
6412 error ("specified mode too small for enumeral values");
6415 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
6417 layout_type (enumtype);
6419 if (values != error_mark_node)
6421 /* Change the type of the enumerators to be the enum type. We
6422 need to do this irrespective of the size of the enum, for
6423 proper type checking. Replace the DECL_INITIALs of the
6424 enumerators, and the value slots of the list, with copies
6425 that have the enum type; they cannot be modified in place
6426 because they may be shared (e.g. integer_zero_node) Finally,
6427 change the purpose slots to point to the names of the decls. */
6428 for (pair = values; pair; pair = TREE_CHAIN (pair))
6430 tree enu = TREE_PURPOSE (pair);
6431 tree ini = DECL_INITIAL (enu);
6433 TREE_TYPE (enu) = enumtype;
6435 /* The ISO C Standard mandates enumerators to have type int,
6436 even though the underlying type of an enum type is
6437 unspecified. However, GCC allows enumerators of any
6438 integer type as an extensions. build_enumerator()
6439 converts any enumerators that fit in an int to type int,
6440 to avoid promotions to unsigned types when comparing
6441 integers with enumerators that fit in the int range.
6442 When -pedantic is given, build_enumerator() would have
6443 already warned about those that don't fit. Here we
6444 convert the rest to the enumerator type. */
6445 if (TREE_TYPE (ini) != integer_type_node)
6446 ini = convert (enumtype, ini);
6448 DECL_INITIAL (enu) = ini;
6449 TREE_PURPOSE (pair) = DECL_NAME (enu);
6450 TREE_VALUE (pair) = ini;
6453 TYPE_VALUES (enumtype) = values;
6456 /* Record the min/max values so that we can warn about bit-field
6457 enumerations that are too small for the values. */
6458 lt = GGC_CNEW (struct lang_type);
6459 lt->enum_min = minnode;
6460 lt->enum_max = maxnode;
6461 TYPE_LANG_SPECIFIC (enumtype) = lt;
6463 /* Fix up all variant types of this enum type. */
6464 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
6466 if (tem == enumtype)
6468 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
6469 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
6470 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
6471 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
6472 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
6473 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
6474 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
6475 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
6476 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
6477 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
6478 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
6481 /* Finish debugging output for this type. */
6482 rest_of_type_compilation (enumtype, toplevel);
6484 /* If this enum is defined inside a struct, add it to
6486 if (in_struct && !in_sizeof && !in_typeof && !in_alignof)
6487 VEC_safe_push (tree, heap, struct_types, enumtype);
6492 /* Build and install a CONST_DECL for one value of the
6493 current enumeration type (one that was begun with start_enum).
6494 LOC is the location of the enumerator.
6495 Return a tree-list containing the CONST_DECL and its value.
6496 Assignment of sequential values by default is handled here. */
6499 build_enumerator (location_t loc,
6500 struct c_enum_contents *the_enum, tree name, tree value)
6504 /* Validate and default VALUE. */
6508 /* Don't issue more errors for error_mark_node (i.e. an
6509 undeclared identifier) - just ignore the value expression. */
6510 if (value == error_mark_node)
6512 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
6514 error_at (loc, "enumerator value for %qE is not an integer constant",
6520 if (TREE_CODE (value) != INTEGER_CST)
6522 value = c_fully_fold (value, false, NULL);
6523 if (TREE_CODE (value) == INTEGER_CST)
6524 pedwarn (loc, OPT_pedantic,
6525 "enumerator value for %qE is not an integer "
6526 "constant expression", name);
6528 if (TREE_CODE (value) != INTEGER_CST)
6530 error ("enumerator value for %qE is not an integer constant",
6536 value = default_conversion (value);
6537 constant_expression_warning (value);
6542 /* Default based on previous value. */
6543 /* It should no longer be possible to have NON_LVALUE_EXPR
6547 value = the_enum->enum_next_value;
6548 if (the_enum->enum_overflow)
6549 error_at (loc, "overflow in enumeration values");
6551 /* Even though the underlying type of an enum is unspecified, the
6552 type of enumeration constants is explicitly defined as int
6553 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
6555 else if (!int_fits_type_p (value, integer_type_node))
6556 pedwarn (loc, OPT_pedantic,
6557 "ISO C restricts enumerator values to range of %<int%>");
6559 /* The ISO C Standard mandates enumerators to have type int, even
6560 though the underlying type of an enum type is unspecified.
6561 However, GCC allows enumerators of any integer type as an
6562 extensions. Here we convert any enumerators that fit in an int
6563 to type int, to avoid promotions to unsigned types when comparing
6564 integers with enumerators that fit in the int range. When
6565 -pedantic is given, we would have already warned about those that
6566 don't fit. We have to do this here rather than in finish_enum
6567 because this value may be used to define more enumerators. */
6568 if (int_fits_type_p (value, integer_type_node))
6569 value = convert (integer_type_node, value);
6571 /* Set basis for default for next value. */
6572 the_enum->enum_next_value
6574 (EXPR_HAS_LOCATION (value) ? EXPR_LOCATION (value) : input_location,
6575 PLUS_EXPR, value, integer_one_node, 0);
6576 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
6578 /* Now create a declaration for the enum value name. */
6580 type = TREE_TYPE (value);
6581 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6582 TYPE_PRECISION (integer_type_node)),
6583 (TYPE_PRECISION (type)
6584 >= TYPE_PRECISION (integer_type_node)
6585 && TYPE_UNSIGNED (type)));
6587 decl = build_decl (loc, CONST_DECL, name, type);
6588 DECL_INITIAL (decl) = convert (type, value);
6591 return tree_cons (decl, value, NULL_TREE);
6595 /* Create the FUNCTION_DECL for a function definition.
6596 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6597 the declaration; they describe the function's name and the type it returns,
6598 but twisted together in a fashion that parallels the syntax of C.
6600 This function creates a binding context for the function body
6601 as well as setting up the FUNCTION_DECL in current_function_decl.
6603 Returns 1 on success. If the DECLARATOR is not suitable for a function
6604 (it defines a datum instead), we return 0, which tells
6605 yyparse to report a parse error. */
6608 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6611 tree decl1, old_decl;
6612 tree restype, resdecl;
6613 struct c_label_context_se *nstack_se;
6614 struct c_label_context_vm *nstack_vm;
6617 current_function_returns_value = 0; /* Assume, until we see it does. */
6618 current_function_returns_null = 0;
6619 current_function_returns_abnormally = 0;
6620 warn_about_return_type = 0;
6621 c_switch_stack = NULL;
6623 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6624 nstack_se->labels_def = NULL;
6625 nstack_se->labels_used = NULL;
6626 nstack_se->next = label_context_stack_se;
6627 label_context_stack_se = nstack_se;
6629 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6630 nstack_vm->labels_def = NULL;
6631 nstack_vm->labels_used = NULL;
6632 nstack_vm->scope = 0;
6633 nstack_vm->next = label_context_stack_vm;
6634 label_context_stack_vm = nstack_vm;
6636 /* Indicate no valid break/continue context by setting these variables
6637 to some non-null, non-label value. We'll notice and emit the proper
6638 error message in c_finish_bc_stmt. */
6639 c_break_label = c_cont_label = size_zero_node;
6641 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
6642 &attributes, NULL, NULL, DEPRECATED_NORMAL);
6644 /* If the declarator is not suitable for a function definition,
6645 cause a syntax error. */
6648 label_context_stack_se = label_context_stack_se->next;
6649 label_context_stack_vm = label_context_stack_vm->next;
6653 loc = DECL_SOURCE_LOCATION (decl1);
6655 decl_attributes (&decl1, attributes, 0);
6657 if (DECL_DECLARED_INLINE_P (decl1)
6658 && DECL_UNINLINABLE (decl1)
6659 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6660 warning_at (loc, OPT_Wattributes,
6661 "inline function %qD given attribute noinline",
6664 /* Handle gnu_inline attribute. */
6665 if (declspecs->inline_p
6666 && !flag_gnu89_inline
6667 && TREE_CODE (decl1) == FUNCTION_DECL
6668 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
6669 || current_function_decl))
6671 if (declspecs->storage_class != csc_static)
6672 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6675 announce_function (decl1);
6677 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6679 error_at (loc, "return type is an incomplete type");
6680 /* Make it return void instead. */
6682 = build_function_type (void_type_node,
6683 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6686 if (warn_about_return_type)
6687 pedwarn_c99 (loc, flag_isoc99 ? 0
6688 : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
6689 "return type defaults to %<int%>");
6691 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6692 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6693 DECL_INITIAL (decl1) = error_mark_node;
6695 /* If this definition isn't a prototype and we had a prototype declaration
6696 before, copy the arg type info from that prototype. */
6697 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6698 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6700 current_function_prototype_locus = UNKNOWN_LOCATION;
6701 current_function_prototype_built_in = false;
6702 current_function_prototype_arg_types = NULL_TREE;
6703 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6705 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6706 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6707 TREE_TYPE (TREE_TYPE (old_decl))))
6709 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6711 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6712 current_function_prototype_built_in
6713 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6714 current_function_prototype_arg_types
6715 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6717 if (TREE_PUBLIC (decl1))
6719 /* If there is an external prototype declaration of this
6720 function, record its location but do not copy information
6721 to this decl. This may be an invisible declaration
6722 (built-in or in a scope which has finished) or simply
6723 have more refined argument types than any declaration
6725 struct c_binding *b;
6726 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6727 if (B_IN_SCOPE (b, external_scope))
6731 tree ext_decl, ext_type;
6733 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6734 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6735 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6736 TREE_TYPE (ext_type)))
6738 current_function_prototype_locus
6739 = DECL_SOURCE_LOCATION (ext_decl);
6740 current_function_prototype_built_in
6741 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6742 current_function_prototype_arg_types
6743 = TYPE_ARG_TYPES (ext_type);
6749 /* Optionally warn of old-fashioned def with no previous prototype. */
6750 if (warn_strict_prototypes
6751 && old_decl != error_mark_node
6752 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6753 && C_DECL_ISNT_PROTOTYPE (old_decl))
6754 warning_at (loc, OPT_Wstrict_prototypes,
6755 "function declaration isn%'t a prototype");
6756 /* Optionally warn of any global def with no previous prototype. */
6757 else if (warn_missing_prototypes
6758 && old_decl != error_mark_node
6759 && TREE_PUBLIC (decl1)
6760 && !MAIN_NAME_P (DECL_NAME (decl1))
6761 && C_DECL_ISNT_PROTOTYPE (old_decl))
6762 warning_at (loc, OPT_Wmissing_prototypes,
6763 "no previous prototype for %qD", decl1);
6764 /* Optionally warn of any def with no previous prototype
6765 if the function has already been used. */
6766 else if (warn_missing_prototypes
6768 && old_decl != error_mark_node
6769 && TREE_USED (old_decl)
6770 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6771 warning_at (loc, OPT_Wmissing_prototypes,
6772 "%qD was used with no prototype before its definition", decl1);
6773 /* Optionally warn of any global def with no previous declaration. */
6774 else if (warn_missing_declarations
6775 && TREE_PUBLIC (decl1)
6777 && !MAIN_NAME_P (DECL_NAME (decl1)))
6778 warning_at (loc, OPT_Wmissing_declarations,
6779 "no previous declaration for %qD",
6781 /* Optionally warn of any def with no previous declaration
6782 if the function has already been used. */
6783 else if (warn_missing_declarations
6785 && old_decl != error_mark_node
6786 && TREE_USED (old_decl)
6787 && C_DECL_IMPLICIT (old_decl))
6788 warning_at (loc, OPT_Wmissing_declarations,
6789 "%qD was used with no declaration before its definition", decl1);
6791 /* This function exists in static storage.
6792 (This does not mean `static' in the C sense!) */
6793 TREE_STATIC (decl1) = 1;
6795 /* A nested function is not global. */
6796 if (current_function_decl != 0)
6797 TREE_PUBLIC (decl1) = 0;
6799 /* This is the earliest point at which we might know the assembler
6800 name of the function. Thus, if it's set before this, die horribly. */
6801 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6803 /* If #pragma weak was used, mark the decl weak now. */
6804 if (current_scope == file_scope)
6805 maybe_apply_pragma_weak (decl1);
6807 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6808 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
6810 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6811 != integer_type_node)
6812 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
6814 check_main_parameter_types (decl1);
6816 if (!TREE_PUBLIC (decl1))
6817 pedwarn (loc, OPT_Wmain,
6818 "%qD is normally a non-static function", decl1);
6821 /* Record the decl so that the function name is defined.
6822 If we already have a decl for this name, and it is a FUNCTION_DECL,
6823 use the old decl. */
6825 current_function_decl = pushdecl (decl1);
6828 declare_parm_level ();
6830 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6831 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
6832 DECL_ARTIFICIAL (resdecl) = 1;
6833 DECL_IGNORED_P (resdecl) = 1;
6834 DECL_RESULT (current_function_decl) = resdecl;
6836 start_fname_decls ();
6841 /* Subroutine of store_parm_decls which handles new-style function
6842 definitions (prototype format). The parms already have decls, so we
6843 need only record them as in effect and complain if any redundant
6844 old-style parm decls were written. */
6846 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6850 if (current_scope->bindings)
6852 error ("%Jold-style parameter declarations in prototyped "
6853 "function definition", fndecl);
6855 /* Get rid of the old-style declarations. */
6859 /* Don't issue this warning for nested functions, and don't issue this
6860 warning if we got here because ARG_INFO_TYPES was error_mark_node
6861 (this happens when a function definition has just an ellipsis in
6862 its parameter list). */
6863 else if (!in_system_header && !current_function_scope
6864 && arg_info->types != error_mark_node)
6865 warning (OPT_Wtraditional,
6866 "%Jtraditional C rejects ISO C style function definitions",
6869 /* Now make all the parameter declarations visible in the function body.
6870 We can bypass most of the grunt work of pushdecl. */
6871 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6873 DECL_CONTEXT (decl) = current_function_decl;
6874 if (DECL_NAME (decl))
6876 bind (DECL_NAME (decl), decl, current_scope,
6877 /*invisible=*/false, /*nested=*/false,
6879 if (!TREE_USED (decl))
6880 warn_if_shadowing (decl);
6883 error ("%Jparameter name omitted", decl);
6886 /* Record the parameter list in the function declaration. */
6887 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6889 /* Now make all the ancillary declarations visible, likewise. */
6890 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6892 DECL_CONTEXT (decl) = current_function_decl;
6893 if (DECL_NAME (decl))
6894 bind (DECL_NAME (decl), decl, current_scope,
6895 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
6898 /* And all the tag declarations. */
6899 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6900 if (TREE_PURPOSE (decl))
6901 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6902 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
6905 /* Subroutine of store_parm_decls which handles old-style function
6906 definitions (separate parameter list and declarations). */
6909 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6911 struct c_binding *b;
6912 tree parm, decl, last;
6913 tree parmids = arg_info->parms;
6914 struct pointer_set_t *seen_args = pointer_set_create ();
6916 if (!in_system_header)
6917 warning_at (DECL_SOURCE_LOCATION (fndecl),
6918 OPT_Wold_style_definition, "old-style function definition");
6920 /* Match each formal parameter name with its declaration. Save each
6921 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6922 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6924 if (TREE_VALUE (parm) == 0)
6926 error_at (DECL_SOURCE_LOCATION (fndecl),
6927 "parameter name missing from parameter list");
6928 TREE_PURPOSE (parm) = 0;
6932 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6933 if (b && B_IN_CURRENT_SCOPE (b))
6936 /* If we got something other than a PARM_DECL it is an error. */
6937 if (TREE_CODE (decl) != PARM_DECL)
6938 error_at (DECL_SOURCE_LOCATION (decl),
6939 "%qD declared as a non-parameter", decl);
6940 /* If the declaration is already marked, we have a duplicate
6941 name. Complain and ignore the duplicate. */
6942 else if (pointer_set_contains (seen_args, decl))
6944 error_at (DECL_SOURCE_LOCATION (decl),
6945 "multiple parameters named %qD", decl);
6946 TREE_PURPOSE (parm) = 0;
6949 /* If the declaration says "void", complain and turn it into
6951 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6953 error_at (DECL_SOURCE_LOCATION (decl),
6954 "parameter %qD declared with void type", decl);
6955 TREE_TYPE (decl) = integer_type_node;
6956 DECL_ARG_TYPE (decl) = integer_type_node;
6957 layout_decl (decl, 0);
6959 warn_if_shadowing (decl);
6961 /* If no declaration found, default to int. */
6964 /* FIXME diagnostics: This should be the location of the argument,
6965 not the FNDECL. E.g., for an old-style declaration
6967 int f10(v) { blah; }
6969 We should use the location of the V, not the F10.
6970 Unfortunately, the V is an IDENTIFIER_NODE which has no
6971 location. In the future we need locations for c_arg_info
6974 See gcc.dg/Wshadow-3.c for an example of this problem. */
6975 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
6976 PARM_DECL, TREE_VALUE (parm), integer_type_node);
6977 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6979 warn_if_shadowing (decl);
6982 pedwarn (DECL_SOURCE_LOCATION (decl),
6983 0, "type of %qD defaults to %<int%>", decl);
6985 warning_at (DECL_SOURCE_LOCATION (decl),
6986 OPT_Wmissing_parameter_type,
6987 "type of %qD defaults to %<int%>", decl);
6990 TREE_PURPOSE (parm) = decl;
6991 pointer_set_insert (seen_args, decl);
6994 /* Now examine the parms chain for incomplete declarations
6995 and declarations with no corresponding names. */
6997 for (b = current_scope->bindings; b; b = b->prev)
7000 if (TREE_CODE (parm) != PARM_DECL)
7003 if (TREE_TYPE (parm) != error_mark_node
7004 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
7006 error_at (DECL_SOURCE_LOCATION (parm),
7007 "parameter %qD has incomplete type", parm);
7008 TREE_TYPE (parm) = error_mark_node;
7011 if (!pointer_set_contains (seen_args, parm))
7013 error_at (DECL_SOURCE_LOCATION (parm),
7014 "declaration for parameter %qD but no such parameter",
7017 /* Pretend the parameter was not missing.
7018 This gets us to a standard state and minimizes
7019 further error messages. */
7020 parmids = chainon (parmids, tree_cons (parm, 0, 0));
7024 /* Chain the declarations together in the order of the list of
7025 names. Store that chain in the function decl, replacing the
7026 list of names. Update the current scope to match. */
7027 DECL_ARGUMENTS (fndecl) = 0;
7029 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
7030 if (TREE_PURPOSE (parm))
7032 if (parm && TREE_PURPOSE (parm))
7034 last = TREE_PURPOSE (parm);
7035 DECL_ARGUMENTS (fndecl) = last;
7037 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
7038 if (TREE_PURPOSE (parm))
7040 TREE_CHAIN (last) = TREE_PURPOSE (parm);
7041 last = TREE_PURPOSE (parm);
7043 TREE_CHAIN (last) = 0;
7046 pointer_set_destroy (seen_args);
7048 /* If there was a previous prototype,
7049 set the DECL_ARG_TYPE of each argument according to
7050 the type previously specified, and report any mismatches. */
7052 if (current_function_prototype_arg_types)
7055 for (parm = DECL_ARGUMENTS (fndecl),
7056 type = current_function_prototype_arg_types;
7057 parm || (type && TREE_VALUE (type) != error_mark_node
7058 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
7059 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
7061 if (parm == 0 || type == 0
7062 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
7064 if (current_function_prototype_built_in)
7065 warning_at (DECL_SOURCE_LOCATION (fndecl),
7066 0, "number of arguments doesn%'t match "
7067 "built-in prototype");
7070 /* FIXME diagnostics: This should be the location of
7071 FNDECL, but there is bug when a prototype is
7072 declared inside function context, but defined
7073 outside of it (e.g., gcc.dg/pr15698-2.c). In
7074 which case FNDECL gets the location of the
7075 prototype, not the definition. */
7076 error_at (input_location,
7077 "number of arguments doesn%'t match prototype");
7079 error_at (current_function_prototype_locus,
7080 "prototype declaration");
7084 /* Type for passing arg must be consistent with that
7085 declared for the arg. ISO C says we take the unqualified
7086 type for parameters declared with qualified type. */
7087 if (TREE_TYPE (parm) != error_mark_node
7088 && TREE_TYPE (type) != error_mark_node
7089 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
7090 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
7092 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
7093 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
7095 /* Adjust argument to match prototype. E.g. a previous
7096 `int foo(float);' prototype causes
7097 `int foo(x) float x; {...}' to be treated like
7098 `int foo(float x) {...}'. This is particularly
7099 useful for argument types like uid_t. */
7100 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
7102 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
7103 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
7104 && TYPE_PRECISION (TREE_TYPE (parm))
7105 < TYPE_PRECISION (integer_type_node))
7106 DECL_ARG_TYPE (parm) = integer_type_node;
7108 /* ??? Is it possible to get here with a
7109 built-in prototype or will it always have
7110 been diagnosed as conflicting with an
7111 old-style definition and discarded? */
7112 if (current_function_prototype_built_in)
7113 warning_at (DECL_SOURCE_LOCATION (parm),
7114 OPT_pedantic, "promoted argument %qD "
7115 "doesn%'t match built-in prototype", parm);
7118 pedwarn (DECL_SOURCE_LOCATION (parm),
7119 OPT_pedantic, "promoted argument %qD "
7120 "doesn%'t match prototype", parm);
7121 pedwarn (current_function_prototype_locus, OPT_pedantic,
7122 "prototype declaration");
7127 if (current_function_prototype_built_in)
7128 warning_at (DECL_SOURCE_LOCATION (parm),
7129 0, "argument %qD doesn%'t match "
7130 "built-in prototype", parm);
7133 error_at (DECL_SOURCE_LOCATION (parm),
7134 "argument %qD doesn%'t match prototype", parm);
7135 error_at (current_function_prototype_locus,
7136 "prototype declaration");
7141 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
7144 /* Otherwise, create a prototype that would match. */
7148 tree actual = 0, last = 0, type;
7150 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
7152 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
7154 TREE_CHAIN (last) = type;
7159 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
7161 TREE_CHAIN (last) = type;
7165 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
7166 of the type of this function, but we need to avoid having this
7167 affect the types of other similarly-typed functions, so we must
7168 first force the generation of an identical (but separate) type
7169 node for the relevant function type. The new node we create
7170 will be a variant of the main variant of the original function
7173 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
7175 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
7179 /* Store parameter declarations passed in ARG_INFO into the current
7180 function declaration. */
7183 store_parm_decls_from (struct c_arg_info *arg_info)
7185 current_function_arg_info = arg_info;
7186 store_parm_decls ();
7189 /* Store the parameter declarations into the current function declaration.
7190 This is called after parsing the parameter declarations, before
7191 digesting the body of the function.
7193 For an old-style definition, construct a prototype out of the old-style
7194 parameter declarations and inject it into the function's type. */
7197 store_parm_decls (void)
7199 tree fndecl = current_function_decl;
7202 /* The argument information block for FNDECL. */
7203 struct c_arg_info *arg_info = current_function_arg_info;
7204 current_function_arg_info = 0;
7206 /* True if this definition is written with a prototype. Note:
7207 despite C99 6.7.5.3p14, we can *not* treat an empty argument
7208 list in a function definition as equivalent to (void) -- an
7209 empty argument list specifies the function has no parameters,
7210 but only (void) sets up a prototype for future calls. */
7211 proto = arg_info->types != 0;
7214 store_parm_decls_newstyle (fndecl, arg_info);
7216 store_parm_decls_oldstyle (fndecl, arg_info);
7218 /* The next call to push_scope will be a function body. */
7220 next_is_function_body = true;
7222 /* Write a record describing this function definition to the prototypes
7223 file (if requested). */
7225 gen_aux_info_record (fndecl, 1, 0, proto);
7227 /* Initialize the RTL code for the function. */
7228 allocate_struct_function (fndecl, false);
7230 /* Begin the statement tree for this function. */
7231 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
7233 /* ??? Insert the contents of the pending sizes list into the function
7234 to be evaluated. The only reason left to have this is
7235 void foo(int n, int array[n++])
7236 because we throw away the array type in favor of a pointer type, and
7237 thus won't naturally see the SAVE_EXPR containing the increment. All
7238 other pending sizes would be handled by gimplify_parameters. */
7241 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
7242 add_stmt (TREE_VALUE (t));
7245 /* Even though we're inside a function body, we still don't want to
7246 call expand_expr to calculate the size of a variable-sized array.
7247 We haven't necessarily assigned RTL to all variables yet, so it's
7248 not safe to try to expand expressions involving them. */
7249 cfun->dont_save_pending_sizes_p = 1;
7252 /* Emit diagnostics that require gimple input for detection. Operate on
7253 FNDECL and all its nested functions. */
7256 c_gimple_diagnostics_recursively (tree fndecl)
7258 struct cgraph_node *cgn;
7259 gimple_seq body = gimple_body (fndecl);
7261 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
7262 c_warn_unused_result (body);
7264 /* Notice when OpenMP structured block constraints are violated. */
7266 diagnose_omp_structured_block_errors (fndecl);
7268 /* Finalize all nested functions now. */
7269 cgn = cgraph_node (fndecl);
7270 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
7271 c_gimple_diagnostics_recursively (cgn->decl);
7274 /* Finish up a function declaration and compile that function
7275 all the way to assembler language output. The free the storage
7276 for the function definition.
7278 This is called after parsing the body of the function definition. */
7281 finish_function (void)
7283 tree fndecl = current_function_decl;
7285 label_context_stack_se = label_context_stack_se->next;
7286 label_context_stack_vm = label_context_stack_vm->next;
7288 if (TREE_CODE (fndecl) == FUNCTION_DECL
7289 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
7291 tree args = DECL_ARGUMENTS (fndecl);
7292 for (; args; args = TREE_CHAIN (args))
7294 tree type = TREE_TYPE (args);
7295 if (INTEGRAL_TYPE_P (type)
7296 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
7297 DECL_ARG_TYPE (args) = integer_type_node;
7301 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
7302 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7304 /* Must mark the RESULT_DECL as being in this function. */
7306 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
7307 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
7309 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
7310 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
7311 == integer_type_node && flag_isoc99)
7313 /* Hack. We don't want the middle-end to warn that this return
7314 is unreachable, so we mark its location as special. Using
7315 UNKNOWN_LOCATION has the problem that it gets clobbered in
7316 annotate_one_with_locus. A cleaner solution might be to
7317 ensure ! should_carry_locus_p (stmt), but that needs a flag.
7319 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
7322 /* Tie off the statement tree for this function. */
7323 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
7325 finish_fname_decls ();
7327 /* Complain if there's just no return statement. */
7328 if (warn_return_type
7329 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
7330 && !current_function_returns_value && !current_function_returns_null
7331 /* Don't complain if we are no-return. */
7332 && !current_function_returns_abnormally
7333 /* Don't warn for main(). */
7334 && !MAIN_NAME_P (DECL_NAME (fndecl))
7335 /* Or if they didn't actually specify a return type. */
7336 && !C_FUNCTION_IMPLICIT_INT (fndecl)
7337 /* Normally, with -Wreturn-type, flow will complain, but we might
7338 optimize out static functions. */
7339 && !TREE_PUBLIC (fndecl))
7341 warning (OPT_Wreturn_type,
7342 "no return statement in function returning non-void");
7343 TREE_NO_WARNING (fndecl) = 1;
7346 /* Store the end of the function, so that we get good line number
7347 info for the epilogue. */
7348 cfun->function_end_locus = input_location;
7350 /* Finalize the ELF visibility for the function. */
7351 c_determine_visibility (fndecl);
7353 /* For GNU C extern inline functions disregard inline limits. */
7354 if (DECL_EXTERNAL (fndecl)
7355 && DECL_DECLARED_INLINE_P (fndecl))
7356 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
7358 /* Genericize before inlining. Delay genericizing nested functions
7359 until their parent function is genericized. Since finalizing
7360 requires GENERIC, delay that as well. */
7362 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
7363 && !undef_nested_function)
7365 if (!decl_function_context (fndecl))
7367 c_genericize (fndecl);
7368 c_gimple_diagnostics_recursively (fndecl);
7370 /* ??? Objc emits functions after finalizing the compilation unit.
7371 This should be cleaned up later and this conditional removed. */
7372 if (cgraph_global_info_ready)
7374 cgraph_add_new_function (fndecl, false);
7377 cgraph_finalize_function (fndecl, false);
7381 /* Register this function with cgraph just far enough to get it
7382 added to our parent's nested function list. Handy, since the
7383 C front end doesn't have such a list. */
7384 (void) cgraph_node (fndecl);
7388 if (!decl_function_context (fndecl))
7389 undef_nested_function = false;
7391 /* We're leaving the context of this function, so zap cfun.
7392 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
7393 tree_rest_of_compilation. */
7395 current_function_decl = NULL;
7398 /* Check the declarations given in a for-loop for satisfying the C99
7399 constraints. If exactly one such decl is found, return it. LOC is
7400 the location of the opening parenthesis of the for loop. */
7403 check_for_loop_decls (location_t loc)
7405 struct c_binding *b;
7406 tree one_decl = NULL_TREE;
7411 static bool hint = true;
7412 /* If we get here, declarations have been used in a for loop without
7413 the C99 for loop scope. This doesn't make much sense, so don't
7415 error_at (loc, "%<for%> loop initial declarations "
7416 "are only allowed in C99 mode");
7420 "use option -std=c99 or -std=gnu99 to compile your code");
7425 /* C99 subclause 6.8.5 paragraph 3:
7427 [#3] The declaration part of a for statement shall only
7428 declare identifiers for objects having storage class auto or
7431 It isn't clear whether, in this sentence, "identifiers" binds to
7432 "shall only declare" or to "objects" - that is, whether all identifiers
7433 declared must be identifiers for objects, or whether the restriction
7434 only applies to those that are. (A question on this in comp.std.c
7435 in November 2000 received no answer.) We implement the strictest
7436 interpretation, to avoid creating an extension which later causes
7439 for (b = current_scope->bindings; b; b = b->prev)
7442 tree decl = b->decl;
7447 switch (TREE_CODE (decl))
7451 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
7452 if (TREE_STATIC (decl))
7454 "declaration of static variable %qD in %<for%> loop "
7455 "initial declaration", decl);
7456 else if (DECL_EXTERNAL (decl))
7458 "declaration of %<extern%> variable %qD in %<for%> loop "
7459 "initial declaration", decl);
7465 "%<struct %E%> declared in %<for%> loop initial "
7470 "%<union %E%> declared in %<for%> loop initial declaration",
7474 error_at (loc, "%<enum %E%> declared in %<for%> loop "
7475 "initial declaration", id);
7478 error_at (loc, "declaration of non-variable "
7479 "%qD in %<for%> loop initial declaration", decl);
7486 return n_decls == 1 ? one_decl : NULL_TREE;
7489 /* Save and reinitialize the variables
7490 used during compilation of a C function. */
7493 c_push_function_context (void)
7495 struct language_function *p;
7496 p = GGC_NEW (struct language_function);
7499 p->base.x_stmt_tree = c_stmt_tree;
7500 p->x_break_label = c_break_label;
7501 p->x_cont_label = c_cont_label;
7502 p->x_switch_stack = c_switch_stack;
7503 p->arg_info = current_function_arg_info;
7504 p->returns_value = current_function_returns_value;
7505 p->returns_null = current_function_returns_null;
7506 p->returns_abnormally = current_function_returns_abnormally;
7507 p->warn_about_return_type = warn_about_return_type;
7509 push_function_context ();
7512 /* Restore the variables used during compilation of a C function. */
7515 c_pop_function_context (void)
7517 struct language_function *p;
7519 pop_function_context ();
7521 cfun->language = NULL;
7523 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
7524 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
7526 /* Stop pointing to the local nodes about to be freed. */
7527 /* But DECL_INITIAL must remain nonzero so we know this
7528 was an actual function definition. */
7529 DECL_INITIAL (current_function_decl) = error_mark_node;
7530 DECL_ARGUMENTS (current_function_decl) = 0;
7533 c_stmt_tree = p->base.x_stmt_tree;
7534 c_break_label = p->x_break_label;
7535 c_cont_label = p->x_cont_label;
7536 c_switch_stack = p->x_switch_stack;
7537 current_function_arg_info = p->arg_info;
7538 current_function_returns_value = p->returns_value;
7539 current_function_returns_null = p->returns_null;
7540 current_function_returns_abnormally = p->returns_abnormally;
7541 warn_about_return_type = p->warn_about_return_type;
7544 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
7547 c_dup_lang_specific_decl (tree decl)
7549 struct lang_decl *ld;
7551 if (!DECL_LANG_SPECIFIC (decl))
7554 ld = GGC_NEW (struct lang_decl);
7555 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
7556 DECL_LANG_SPECIFIC (decl) = ld;
7559 /* The functions below are required for functionality of doing
7560 function at once processing in the C front end. Currently these
7561 functions are not called from anywhere in the C front end, but as
7562 these changes continue, that will change. */
7564 /* Returns the stmt_tree (if any) to which statements are currently
7565 being added. If there is no active statement-tree, NULL is
7569 current_stmt_tree (void)
7571 return &c_stmt_tree;
7574 /* Return the global value of T as a symbol. */
7577 identifier_global_value (tree t)
7579 struct c_binding *b;
7581 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
7582 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
7588 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
7589 otherwise the name is found in ridpointers from RID_INDEX. */
7592 record_builtin_type (enum rid rid_index, const char *name, tree type)
7596 id = ridpointers[(int) rid_index];
7598 id = get_identifier (name);
7599 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
7601 if (debug_hooks->type_decl)
7602 debug_hooks->type_decl (decl, false);
7605 /* Build the void_list_node (void_type_node having been created). */
7607 build_void_list_node (void)
7609 tree t = build_tree_list (NULL_TREE, void_type_node);
7613 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7616 build_c_parm (struct c_declspecs *specs, tree attrs,
7617 struct c_declarator *declarator)
7619 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7622 ret->declarator = declarator;
7626 /* Return a declarator with nested attributes. TARGET is the inner
7627 declarator to which these attributes apply. ATTRS are the
7630 struct c_declarator *
7631 build_attrs_declarator (tree attrs, struct c_declarator *target)
7633 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7634 ret->kind = cdk_attrs;
7635 ret->declarator = target;
7636 ret->u.attrs = attrs;
7640 /* Return a declarator for a function with arguments specified by ARGS
7641 and return type specified by TARGET. */
7643 struct c_declarator *
7644 build_function_declarator (struct c_arg_info *args,
7645 struct c_declarator *target)
7647 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7648 ret->kind = cdk_function;
7649 ret->declarator = target;
7650 ret->u.arg_info = args;
7654 /* Return a declarator for the identifier IDENT (which may be
7655 NULL_TREE for an abstract declarator). */
7657 struct c_declarator *
7658 build_id_declarator (tree ident)
7660 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7662 ret->declarator = 0;
7664 /* Default value - may get reset to a more precise location. */
7665 ret->id_loc = input_location;
7669 /* Return something to represent absolute declarators containing a *.
7670 TARGET is the absolute declarator that the * contains.
7671 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7672 to apply to the pointer type. */
7674 struct c_declarator *
7675 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7676 struct c_declarator *target)
7680 struct c_declarator *itarget = target;
7681 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7682 if (type_quals_attrs)
7684 attrs = type_quals_attrs->attrs;
7685 quals = quals_from_declspecs (type_quals_attrs);
7686 if (attrs != NULL_TREE)
7687 itarget = build_attrs_declarator (attrs, target);
7689 ret->kind = cdk_pointer;
7690 ret->declarator = itarget;
7691 ret->u.pointer_quals = quals;
7695 /* Return a pointer to a structure for an empty list of declaration
7698 struct c_declspecs *
7699 build_null_declspecs (void)
7701 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7706 ret->typespec_word = cts_none;
7707 ret->storage_class = csc_none;
7708 ret->expr_const_operands = true;
7709 ret->declspecs_seen_p = false;
7710 ret->type_seen_p = false;
7711 ret->non_sc_seen_p = false;
7712 ret->typedef_p = false;
7713 ret->tag_defined_p = false;
7714 ret->explicit_signed_p = false;
7715 ret->deprecated_p = false;
7716 ret->default_int_p = false;
7717 ret->long_p = false;
7718 ret->long_long_p = false;
7719 ret->short_p = false;
7720 ret->signed_p = false;
7721 ret->unsigned_p = false;
7722 ret->complex_p = false;
7723 ret->inline_p = false;
7724 ret->thread_p = false;
7725 ret->const_p = false;
7726 ret->volatile_p = false;
7727 ret->restrict_p = false;
7728 ret->saturating_p = false;
7732 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7735 struct c_declspecs *
7736 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7740 specs->non_sc_seen_p = true;
7741 specs->declspecs_seen_p = true;
7742 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7743 && C_IS_RESERVED_WORD (qual));
7744 i = C_RID_CODE (qual);
7748 dupe = specs->const_p;
7749 specs->const_p = true;
7752 dupe = specs->volatile_p;
7753 specs->volatile_p = true;
7756 dupe = specs->restrict_p;
7757 specs->restrict_p = true;
7762 if (dupe && !flag_isoc99)
7763 pedwarn (input_location, OPT_pedantic, "duplicate %qE", qual);
7767 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7770 struct c_declspecs *
7771 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7773 tree type = spec.spec;
7774 specs->non_sc_seen_p = true;
7775 specs->declspecs_seen_p = true;
7776 specs->type_seen_p = true;
7777 if (TREE_DEPRECATED (type))
7778 specs->deprecated_p = true;
7780 /* Handle type specifier keywords. */
7781 if (TREE_CODE (type) == IDENTIFIER_NODE
7782 && C_IS_RESERVED_WORD (type)
7783 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
7785 enum rid i = C_RID_CODE (type);
7788 error ("two or more data types in declaration specifiers");
7791 if ((int) i <= (int) RID_LAST_MODIFIER)
7793 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
7798 if (specs->long_long_p)
7800 error ("%<long long long%> is too long for GCC");
7805 if (specs->typespec_word == cts_double)
7807 error ("both %<long long%> and %<double%> in "
7808 "declaration specifiers");
7811 pedwarn_c90 (input_location, OPT_Wlong_long,
7812 "ISO C90 does not support %<long long%>");
7813 specs->long_long_p = 1;
7817 error ("both %<long%> and %<short%> in "
7818 "declaration specifiers");
7819 else if (specs->typespec_word == cts_void)
7820 error ("both %<long%> and %<void%> in "
7821 "declaration specifiers");
7822 else if (specs->typespec_word == cts_bool)
7823 error ("both %<long%> and %<_Bool%> in "
7824 "declaration specifiers");
7825 else if (specs->typespec_word == cts_char)
7826 error ("both %<long%> and %<char%> in "
7827 "declaration specifiers");
7828 else if (specs->typespec_word == cts_float)
7829 error ("both %<long%> and %<float%> in "
7830 "declaration specifiers");
7831 else if (specs->typespec_word == cts_dfloat32)
7832 error ("both %<long%> and %<_Decimal32%> in "
7833 "declaration specifiers");
7834 else if (specs->typespec_word == cts_dfloat64)
7835 error ("both %<long%> and %<_Decimal64%> in "
7836 "declaration specifiers");
7837 else if (specs->typespec_word == cts_dfloat128)
7838 error ("both %<long%> and %<_Decimal128%> in "
7839 "declaration specifiers");
7841 specs->long_p = true;
7844 dupe = specs->short_p;
7846 error ("both %<long%> and %<short%> in "
7847 "declaration specifiers");
7848 else if (specs->typespec_word == cts_void)
7849 error ("both %<short%> and %<void%> in "
7850 "declaration specifiers");
7851 else if (specs->typespec_word == cts_bool)
7852 error ("both %<short%> and %<_Bool%> in "
7853 "declaration specifiers");
7854 else if (specs->typespec_word == cts_char)
7855 error ("both %<short%> and %<char%> in "
7856 "declaration specifiers");
7857 else if (specs->typespec_word == cts_float)
7858 error ("both %<short%> and %<float%> in "
7859 "declaration specifiers");
7860 else if (specs->typespec_word == cts_double)
7861 error ("both %<short%> and %<double%> in "
7862 "declaration specifiers");
7863 else if (specs->typespec_word == cts_dfloat32)
7864 error ("both %<short%> and %<_Decimal32%> in "
7865 "declaration specifiers");
7866 else if (specs->typespec_word == cts_dfloat64)
7867 error ("both %<short%> and %<_Decimal64%> in "
7868 "declaration specifiers");
7869 else if (specs->typespec_word == cts_dfloat128)
7870 error ("both %<short%> and %<_Decimal128%> in "
7871 "declaration specifiers");
7873 specs->short_p = true;
7876 dupe = specs->signed_p;
7877 if (specs->unsigned_p)
7878 error ("both %<signed%> and %<unsigned%> in "
7879 "declaration specifiers");
7880 else if (specs->typespec_word == cts_void)
7881 error ("both %<signed%> and %<void%> in "
7882 "declaration specifiers");
7883 else if (specs->typespec_word == cts_bool)
7884 error ("both %<signed%> and %<_Bool%> in "
7885 "declaration specifiers");
7886 else if (specs->typespec_word == cts_float)
7887 error ("both %<signed%> and %<float%> in "
7888 "declaration specifiers");
7889 else if (specs->typespec_word == cts_double)
7890 error ("both %<signed%> and %<double%> in "
7891 "declaration specifiers");
7892 else if (specs->typespec_word == cts_dfloat32)
7893 error ("both %<signed%> and %<_Decimal32%> in "
7894 "declaration specifiers");
7895 else if (specs->typespec_word == cts_dfloat64)
7896 error ("both %<signed%> and %<_Decimal64%> in "
7897 "declaration specifiers");
7898 else if (specs->typespec_word == cts_dfloat128)
7899 error ("both %<signed%> and %<_Decimal128%> in "
7900 "declaration specifiers");
7902 specs->signed_p = true;
7905 dupe = specs->unsigned_p;
7906 if (specs->signed_p)
7907 error ("both %<signed%> and %<unsigned%> in "
7908 "declaration specifiers");
7909 else if (specs->typespec_word == cts_void)
7910 error ("both %<unsigned%> and %<void%> in "
7911 "declaration specifiers");
7912 else if (specs->typespec_word == cts_bool)
7913 error ("both %<unsigned%> and %<_Bool%> in "
7914 "declaration specifiers");
7915 else if (specs->typespec_word == cts_float)
7916 error ("both %<unsigned%> and %<float%> in "
7917 "declaration specifiers");
7918 else if (specs->typespec_word == cts_double)
7919 error ("both %<unsigned%> and %<double%> in "
7920 "declaration specifiers");
7921 else if (specs->typespec_word == cts_dfloat32)
7922 error ("both %<unsigned%> and %<_Decimal32%> in "
7923 "declaration specifiers");
7924 else if (specs->typespec_word == cts_dfloat64)
7925 error ("both %<unsigned%> and %<_Decimal64%> in "
7926 "declaration specifiers");
7927 else if (specs->typespec_word == cts_dfloat128)
7928 error ("both %<unsigned%> and %<_Decimal128%> in "
7929 "declaration specifiers");
7931 specs->unsigned_p = true;
7934 dupe = specs->complex_p;
7935 if (!flag_isoc99 && !in_system_header)
7936 pedwarn (input_location, OPT_pedantic, "ISO C90 does not support complex types");
7937 if (specs->typespec_word == cts_void)
7938 error ("both %<complex%> and %<void%> in "
7939 "declaration specifiers");
7940 else if (specs->typespec_word == cts_bool)
7941 error ("both %<complex%> and %<_Bool%> in "
7942 "declaration specifiers");
7943 else if (specs->typespec_word == cts_dfloat32)
7944 error ("both %<complex%> and %<_Decimal32%> in "
7945 "declaration specifiers");
7946 else if (specs->typespec_word == cts_dfloat64)
7947 error ("both %<complex%> and %<_Decimal64%> in "
7948 "declaration specifiers");
7949 else if (specs->typespec_word == cts_dfloat128)
7950 error ("both %<complex%> and %<_Decimal128%> in "
7951 "declaration specifiers");
7952 else if (specs->typespec_word == cts_fract)
7953 error ("both %<complex%> and %<_Fract%> in "
7954 "declaration specifiers");
7955 else if (specs->typespec_word == cts_accum)
7956 error ("both %<complex%> and %<_Accum%> in "
7957 "declaration specifiers");
7958 else if (specs->saturating_p)
7959 error ("both %<complex%> and %<_Sat%> in "
7960 "declaration specifiers");
7962 specs->complex_p = true;
7965 dupe = specs->saturating_p;
7966 pedwarn (input_location, OPT_pedantic, "ISO C does not support saturating types");
7967 if (specs->typespec_word == cts_void)
7968 error ("both %<_Sat%> and %<void%> in "
7969 "declaration specifiers");
7970 else if (specs->typespec_word == cts_bool)
7971 error ("both %<_Sat%> and %<_Bool%> in "
7972 "declaration specifiers");
7973 else if (specs->typespec_word == cts_char)
7974 error ("both %<_Sat%> and %<char%> in "
7975 "declaration specifiers");
7976 else if (specs->typespec_word == cts_int)
7977 error ("both %<_Sat%> and %<int%> in "
7978 "declaration specifiers");
7979 else if (specs->typespec_word == cts_float)
7980 error ("both %<_Sat%> and %<float%> in "
7981 "declaration specifiers");
7982 else if (specs->typespec_word == cts_double)
7983 error ("both %<_Sat%> and %<double%> in "
7984 "declaration specifiers");
7985 else if (specs->typespec_word == cts_dfloat32)
7986 error ("both %<_Sat%> and %<_Decimal32%> in "
7987 "declaration specifiers");
7988 else if (specs->typespec_word == cts_dfloat64)
7989 error ("both %<_Sat%> and %<_Decimal64%> in "
7990 "declaration specifiers");
7991 else if (specs->typespec_word == cts_dfloat128)
7992 error ("both %<_Sat%> and %<_Decimal128%> in "
7993 "declaration specifiers");
7994 else if (specs->complex_p)
7995 error ("both %<_Sat%> and %<complex%> in "
7996 "declaration specifiers");
7998 specs->saturating_p = true;
8005 error ("duplicate %qE", type);
8011 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
8012 "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
8013 if (specs->typespec_word != cts_none)
8015 error ("two or more data types in declaration specifiers");
8022 error ("both %<long%> and %<void%> in "
8023 "declaration specifiers");
8024 else if (specs->short_p)
8025 error ("both %<short%> and %<void%> in "
8026 "declaration specifiers");
8027 else if (specs->signed_p)
8028 error ("both %<signed%> and %<void%> in "
8029 "declaration specifiers");
8030 else if (specs->unsigned_p)
8031 error ("both %<unsigned%> and %<void%> in "
8032 "declaration specifiers");
8033 else if (specs->complex_p)
8034 error ("both %<complex%> and %<void%> in "
8035 "declaration specifiers");
8036 else if (specs->saturating_p)
8037 error ("both %<_Sat%> and %<void%> in "
8038 "declaration specifiers");
8040 specs->typespec_word = cts_void;
8044 error ("both %<long%> and %<_Bool%> in "
8045 "declaration specifiers");
8046 else if (specs->short_p)
8047 error ("both %<short%> and %<_Bool%> in "
8048 "declaration specifiers");
8049 else if (specs->signed_p)
8050 error ("both %<signed%> and %<_Bool%> in "
8051 "declaration specifiers");
8052 else if (specs->unsigned_p)
8053 error ("both %<unsigned%> and %<_Bool%> in "
8054 "declaration specifiers");
8055 else if (specs->complex_p)
8056 error ("both %<complex%> and %<_Bool%> in "
8057 "declaration specifiers");
8058 else if (specs->saturating_p)
8059 error ("both %<_Sat%> and %<_Bool%> in "
8060 "declaration specifiers");
8062 specs->typespec_word = cts_bool;
8066 error ("both %<long%> and %<char%> in "
8067 "declaration specifiers");
8068 else if (specs->short_p)
8069 error ("both %<short%> and %<char%> in "
8070 "declaration specifiers");
8071 else if (specs->saturating_p)
8072 error ("both %<_Sat%> and %<char%> in "
8073 "declaration specifiers");
8075 specs->typespec_word = cts_char;
8078 if (specs->saturating_p)
8079 error ("both %<_Sat%> and %<int%> in "
8080 "declaration specifiers");
8082 specs->typespec_word = cts_int;
8086 error ("both %<long%> and %<float%> in "
8087 "declaration specifiers");
8088 else if (specs->short_p)
8089 error ("both %<short%> and %<float%> in "
8090 "declaration specifiers");
8091 else if (specs->signed_p)
8092 error ("both %<signed%> and %<float%> in "
8093 "declaration specifiers");
8094 else if (specs->unsigned_p)
8095 error ("both %<unsigned%> and %<float%> in "
8096 "declaration specifiers");
8097 else if (specs->saturating_p)
8098 error ("both %<_Sat%> and %<float%> in "
8099 "declaration specifiers");
8101 specs->typespec_word = cts_float;
8104 if (specs->long_long_p)
8105 error ("both %<long long%> and %<double%> in "
8106 "declaration specifiers");
8107 else if (specs->short_p)
8108 error ("both %<short%> and %<double%> in "
8109 "declaration specifiers");
8110 else if (specs->signed_p)
8111 error ("both %<signed%> and %<double%> in "
8112 "declaration specifiers");
8113 else if (specs->unsigned_p)
8114 error ("both %<unsigned%> and %<double%> in "
8115 "declaration specifiers");
8116 else if (specs->saturating_p)
8117 error ("both %<_Sat%> and %<double%> in "
8118 "declaration specifiers");
8120 specs->typespec_word = cts_double;
8127 if (i == RID_DFLOAT32)
8129 else if (i == RID_DFLOAT64)
8132 str = "_Decimal128";
8133 if (specs->long_long_p)
8134 error ("both %<long long%> and %<%s%> in "
8135 "declaration specifiers", str);
8137 error ("both %<long%> and %<%s%> in "
8138 "declaration specifiers", str);
8139 else if (specs->short_p)
8140 error ("both %<short%> and %<%s%> in "
8141 "declaration specifiers", str);
8142 else if (specs->signed_p)
8143 error ("both %<signed%> and %<%s%> in "
8144 "declaration specifiers", str);
8145 else if (specs->unsigned_p)
8146 error ("both %<unsigned%> and %<%s%> in "
8147 "declaration specifiers", str);
8148 else if (specs->complex_p)
8149 error ("both %<complex%> and %<%s%> in "
8150 "declaration specifiers", str);
8151 else if (specs->saturating_p)
8152 error ("both %<_Sat%> and %<%s%> in "
8153 "declaration specifiers", str);
8154 else if (i == RID_DFLOAT32)
8155 specs->typespec_word = cts_dfloat32;
8156 else if (i == RID_DFLOAT64)
8157 specs->typespec_word = cts_dfloat64;
8159 specs->typespec_word = cts_dfloat128;
8161 if (!targetm.decimal_float_supported_p ())
8162 error ("decimal floating point not supported for this target");
8163 pedwarn (input_location, OPT_pedantic,
8164 "ISO C does not support decimal floating point");
8174 if (specs->complex_p)
8175 error ("both %<complex%> and %<%s%> in "
8176 "declaration specifiers", str);
8177 else if (i == RID_FRACT)
8178 specs->typespec_word = cts_fract;
8180 specs->typespec_word = cts_accum;
8182 if (!targetm.fixed_point_supported_p ())
8183 error ("fixed-point types not supported for this target");
8184 pedwarn (input_location, OPT_pedantic,
8185 "ISO C does not support fixed-point types");
8188 /* ObjC reserved word "id", handled below. */
8194 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
8195 form of ObjC type, cases such as "int" and "long" being handled
8196 above), a TYPE (struct, union, enum and typeof specifiers) or an
8197 ERROR_MARK. In none of these cases may there have previously
8198 been any type specifiers. */
8199 if (specs->type || specs->typespec_word != cts_none
8200 || specs->long_p || specs->short_p || specs->signed_p
8201 || specs->unsigned_p || specs->complex_p)
8202 error ("two or more data types in declaration specifiers");
8203 else if (TREE_CODE (type) == TYPE_DECL)
8205 if (TREE_TYPE (type) == error_mark_node)
8206 ; /* Allow the type to default to int to avoid cascading errors. */
8209 specs->type = TREE_TYPE (type);
8210 specs->decl_attr = DECL_ATTRIBUTES (type);
8211 specs->typedef_p = true;
8212 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
8215 else if (TREE_CODE (type) == IDENTIFIER_NODE)
8217 tree t = lookup_name (type);
8218 if (!t || TREE_CODE (t) != TYPE_DECL)
8219 error ("%qE fails to be a typedef or built in type", type);
8220 else if (TREE_TYPE (t) == error_mark_node)
8223 specs->type = TREE_TYPE (t);
8225 else if (TREE_CODE (type) != ERROR_MARK)
8227 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
8228 specs->tag_defined_p = true;
8229 if (spec.kind == ctsk_typeof)
8231 specs->typedef_p = true;
8235 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
8236 specs->expr, spec.expr);
8238 specs->expr = spec.expr;
8239 specs->expr_const_operands &= spec.expr_const_operands;
8248 /* Add the storage class specifier or function specifier SCSPEC to the
8249 declaration specifiers SPECS, returning SPECS. */
8251 struct c_declspecs *
8252 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
8255 enum c_storage_class n = csc_none;
8257 specs->declspecs_seen_p = true;
8258 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
8259 && C_IS_RESERVED_WORD (scspec));
8260 i = C_RID_CODE (scspec);
8261 if (specs->non_sc_seen_p)
8262 warning (OPT_Wold_style_declaration,
8263 "%qE is not at beginning of declaration", scspec);
8267 /* C99 permits duplicate inline. Although of doubtful utility,
8268 it seems simplest to permit it in gnu89 mode as well, as
8269 there is also little utility in maintaining this as a
8270 difference between gnu89 and C99 inline. */
8272 specs->inline_p = true;
8275 dupe = specs->thread_p;
8276 if (specs->storage_class == csc_auto)
8277 error ("%<__thread%> used with %<auto%>");
8278 else if (specs->storage_class == csc_register)
8279 error ("%<__thread%> used with %<register%>");
8280 else if (specs->storage_class == csc_typedef)
8281 error ("%<__thread%> used with %<typedef%>");
8283 specs->thread_p = true;
8290 /* Diagnose "__thread extern". */
8291 if (specs->thread_p)
8292 error ("%<__thread%> before %<extern%>");
8299 /* Diagnose "__thread static". */
8300 if (specs->thread_p)
8301 error ("%<__thread%> before %<static%>");
8309 if (n != csc_none && n == specs->storage_class)
8312 error ("duplicate %qE", scspec);
8315 if (specs->storage_class != csc_none && n != specs->storage_class)
8317 error ("multiple storage classes in declaration specifiers");
8321 specs->storage_class = n;
8322 if (n != csc_extern && n != csc_static && specs->thread_p)
8324 error ("%<__thread%> used with %qE", scspec);
8325 specs->thread_p = false;
8332 /* Add the attributes ATTRS to the declaration specifiers SPECS,
8335 struct c_declspecs *
8336 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
8338 specs->attrs = chainon (attrs, specs->attrs);
8339 specs->declspecs_seen_p = true;
8343 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
8344 specifiers with any other type specifier to determine the resulting
8345 type. This is where ISO C checks on complex types are made, since
8346 "_Complex long" is a prefix of the valid ISO C type "_Complex long
8349 struct c_declspecs *
8350 finish_declspecs (struct c_declspecs *specs)
8352 /* If a type was specified as a whole, we have no modifiers and are
8354 if (specs->type != NULL_TREE)
8356 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
8357 && !specs->signed_p && !specs->unsigned_p
8358 && !specs->complex_p);
8362 /* If none of "void", "_Bool", "char", "int", "float" or "double"
8363 has been specified, treat it as "int" unless "_Complex" is
8364 present and there are no other specifiers. If we just have
8365 "_Complex", it is equivalent to "_Complex double", but e.g.
8366 "_Complex short" is equivalent to "_Complex short int". */
8367 if (specs->typespec_word == cts_none)
8369 if (specs->saturating_p)
8371 error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
8372 if (!targetm.fixed_point_supported_p ())
8373 error ("fixed-point types not supported for this target");
8374 specs->typespec_word = cts_fract;
8376 else if (specs->long_p || specs->short_p
8377 || specs->signed_p || specs->unsigned_p)
8379 specs->typespec_word = cts_int;
8381 else if (specs->complex_p)
8383 specs->typespec_word = cts_double;
8384 pedwarn (input_location, OPT_pedantic,
8385 "ISO C does not support plain %<complex%> meaning "
8386 "%<double complex%>");
8390 specs->typespec_word = cts_int;
8391 specs->default_int_p = true;
8392 /* We don't diagnose this here because grokdeclarator will
8393 give more specific diagnostics according to whether it is
8394 a function definition. */
8398 /* If "signed" was specified, record this to distinguish "int" and
8399 "signed int" in the case of a bit-field with
8400 -funsigned-bitfields. */
8401 specs->explicit_signed_p = specs->signed_p;
8403 /* Now compute the actual type. */
8404 switch (specs->typespec_word)
8407 gcc_assert (!specs->long_p && !specs->short_p
8408 && !specs->signed_p && !specs->unsigned_p
8409 && !specs->complex_p);
8410 specs->type = void_type_node;
8413 gcc_assert (!specs->long_p && !specs->short_p
8414 && !specs->signed_p && !specs->unsigned_p
8415 && !specs->complex_p);
8416 specs->type = boolean_type_node;
8419 gcc_assert (!specs->long_p && !specs->short_p);
8420 gcc_assert (!(specs->signed_p && specs->unsigned_p));
8421 if (specs->signed_p)
8422 specs->type = signed_char_type_node;
8423 else if (specs->unsigned_p)
8424 specs->type = unsigned_char_type_node;
8426 specs->type = char_type_node;
8427 if (specs->complex_p)
8429 pedwarn (input_location, OPT_pedantic,
8430 "ISO C does not support complex integer types");
8431 specs->type = build_complex_type (specs->type);
8435 gcc_assert (!(specs->long_p && specs->short_p));
8436 gcc_assert (!(specs->signed_p && specs->unsigned_p));
8437 if (specs->long_long_p)
8438 specs->type = (specs->unsigned_p
8439 ? long_long_unsigned_type_node
8440 : long_long_integer_type_node);
8441 else if (specs->long_p)
8442 specs->type = (specs->unsigned_p
8443 ? long_unsigned_type_node
8444 : long_integer_type_node);
8445 else if (specs->short_p)
8446 specs->type = (specs->unsigned_p
8447 ? short_unsigned_type_node
8448 : short_integer_type_node);
8450 specs->type = (specs->unsigned_p
8451 ? unsigned_type_node
8452 : integer_type_node);
8453 if (specs->complex_p)
8455 pedwarn (input_location, OPT_pedantic,
8456 "ISO C does not support complex integer types");
8457 specs->type = build_complex_type (specs->type);
8461 gcc_assert (!specs->long_p && !specs->short_p
8462 && !specs->signed_p && !specs->unsigned_p);
8463 specs->type = (specs->complex_p
8464 ? complex_float_type_node
8468 gcc_assert (!specs->long_long_p && !specs->short_p
8469 && !specs->signed_p && !specs->unsigned_p);
8472 specs->type = (specs->complex_p
8473 ? complex_long_double_type_node
8474 : long_double_type_node);
8478 specs->type = (specs->complex_p
8479 ? complex_double_type_node
8480 : double_type_node);
8486 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
8487 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
8488 if (specs->typespec_word == cts_dfloat32)
8489 specs->type = dfloat32_type_node;
8490 else if (specs->typespec_word == cts_dfloat64)
8491 specs->type = dfloat64_type_node;
8493 specs->type = dfloat128_type_node;
8496 gcc_assert (!specs->complex_p);
8497 if (!targetm.fixed_point_supported_p ())
8498 specs->type = integer_type_node;
8499 else if (specs->saturating_p)
8501 if (specs->long_long_p)
8502 specs->type = specs->unsigned_p
8503 ? sat_unsigned_long_long_fract_type_node
8504 : sat_long_long_fract_type_node;
8505 else if (specs->long_p)
8506 specs->type = specs->unsigned_p
8507 ? sat_unsigned_long_fract_type_node
8508 : sat_long_fract_type_node;
8509 else if (specs->short_p)
8510 specs->type = specs->unsigned_p
8511 ? sat_unsigned_short_fract_type_node
8512 : sat_short_fract_type_node;
8514 specs->type = specs->unsigned_p
8515 ? sat_unsigned_fract_type_node
8516 : sat_fract_type_node;
8520 if (specs->long_long_p)
8521 specs->type = specs->unsigned_p
8522 ? unsigned_long_long_fract_type_node
8523 : long_long_fract_type_node;
8524 else if (specs->long_p)
8525 specs->type = specs->unsigned_p
8526 ? unsigned_long_fract_type_node
8527 : long_fract_type_node;
8528 else if (specs->short_p)
8529 specs->type = specs->unsigned_p
8530 ? unsigned_short_fract_type_node
8531 : short_fract_type_node;
8533 specs->type = specs->unsigned_p
8534 ? unsigned_fract_type_node
8539 gcc_assert (!specs->complex_p);
8540 if (!targetm.fixed_point_supported_p ())
8541 specs->type = integer_type_node;
8542 else if (specs->saturating_p)
8544 if (specs->long_long_p)
8545 specs->type = specs->unsigned_p
8546 ? sat_unsigned_long_long_accum_type_node
8547 : sat_long_long_accum_type_node;
8548 else if (specs->long_p)
8549 specs->type = specs->unsigned_p
8550 ? sat_unsigned_long_accum_type_node
8551 : sat_long_accum_type_node;
8552 else if (specs->short_p)
8553 specs->type = specs->unsigned_p
8554 ? sat_unsigned_short_accum_type_node
8555 : sat_short_accum_type_node;
8557 specs->type = specs->unsigned_p
8558 ? sat_unsigned_accum_type_node
8559 : sat_accum_type_node;
8563 if (specs->long_long_p)
8564 specs->type = specs->unsigned_p
8565 ? unsigned_long_long_accum_type_node
8566 : long_long_accum_type_node;
8567 else if (specs->long_p)
8568 specs->type = specs->unsigned_p
8569 ? unsigned_long_accum_type_node
8570 : long_accum_type_node;
8571 else if (specs->short_p)
8572 specs->type = specs->unsigned_p
8573 ? unsigned_short_accum_type_node
8574 : short_accum_type_node;
8576 specs->type = specs->unsigned_p
8577 ? unsigned_accum_type_node
8588 /* A subroutine of c_write_global_declarations. Perform final processing
8589 on one file scope's declarations (or the external scope's declarations),
8593 c_write_global_declarations_1 (tree globals)
8598 /* Process the decls in the order they were written. */
8599 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8601 /* Check for used but undefined static functions using the C
8602 standard's definition of "used", and set TREE_NO_WARNING so
8603 that check_global_declarations doesn't repeat the check. */
8604 if (TREE_CODE (decl) == FUNCTION_DECL
8605 && DECL_INITIAL (decl) == 0
8606 && DECL_EXTERNAL (decl)
8607 && !TREE_PUBLIC (decl)
8608 && C_DECL_USED (decl))
8610 pedwarn (input_location, 0, "%q+F used but never defined", decl);
8611 TREE_NO_WARNING (decl) = 1;
8614 wrapup_global_declaration_1 (decl);
8620 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8621 reconsider |= wrapup_global_declaration_2 (decl);
8625 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8626 check_global_declaration_1 (decl);
8629 /* A subroutine of c_write_global_declarations Emit debug information for each
8630 of the declarations in GLOBALS. */
8633 c_write_global_declarations_2 (tree globals)
8637 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
8638 debug_hooks->global_decl (decl);
8641 /* Preserve the external declarations scope across a garbage collect. */
8642 static GTY(()) tree ext_block;
8645 c_write_global_declarations (void)
8649 /* We don't want to do this if generating a PCH. */
8653 /* Don't waste time on further processing if -fsyntax-only or we've
8654 encountered errors. */
8655 if (flag_syntax_only || errorcount || sorrycount)
8658 /* Close the external scope. */
8659 ext_block = pop_scope ();
8661 gcc_assert (!current_scope);
8665 tree tmp = BLOCK_VARS (ext_block);
8667 FILE * stream = dump_begin (TDI_tu, &flags);
8670 dump_node (tmp, flags & ~TDF_SLIM, stream);
8671 dump_end (TDI_tu, stream);
8675 /* Process all file scopes in this compilation, and the external_scope,
8676 through wrapup_global_declarations and check_global_declarations. */
8677 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8678 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
8679 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
8681 /* We're done parsing; proceed to optimize and emit assembly.
8682 FIXME: shouldn't be the front end's responsibility to call this. */
8685 /* After cgraph has had a chance to emit everything that's going to
8686 be emitted, output debug information for globals. */
8687 if (errorcount == 0 && sorrycount == 0)
8689 timevar_push (TV_SYMOUT);
8690 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8691 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
8692 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
8693 timevar_pop (TV_SYMOUT);
8699 #include "gt-c-decl.h"