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 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
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"
55 #include "tree-gimple.h"
56 #include "diagnostic.h"
57 #include "tree-dump.h"
62 #include "langhooks-def.h"
63 #include "pointer-set.h"
65 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
67 { NORMAL, /* Ordinary declaration */
68 FUNCDEF, /* Function definition */
69 PARM, /* Declaration of parm before function body */
70 FIELD, /* Declaration inside struct or union */
71 TYPENAME}; /* Typename (inside cast or sizeof) */
74 /* Nonzero if we have seen an invalid cross reference
75 to a struct, union, or enum, but not yet printed the message. */
76 tree pending_invalid_xref;
78 /* File and line to appear in the eventual error message. */
79 location_t pending_invalid_xref_location;
81 /* True means we've initialized exception handling. */
82 bool c_eh_initialized_p;
84 /* While defining an enum type, this is 1 plus the last enumerator
85 constant value. Note that will do not have to save this or `enum_overflow'
86 around nested function definition since such a definition could only
87 occur in an enum value expression and we don't use these variables in
90 static tree enum_next_value;
92 /* Nonzero means that there was overflow computing enum_next_value. */
94 static int enum_overflow;
96 /* The file and line that the prototype came from if this is an
97 old-style definition; used for diagnostics in
98 store_parm_decls_oldstyle. */
100 static location_t current_function_prototype_locus;
102 /* Whether this prototype was built-in. */
104 static bool current_function_prototype_built_in;
106 /* The argument type information of this prototype. */
108 static tree current_function_prototype_arg_types;
110 /* The argument information structure for the function currently being
113 static struct c_arg_info *current_function_arg_info;
115 /* The obstack on which parser and related data structures, which are
116 not live beyond their top-level declaration or definition, are
118 struct obstack parser_obstack;
120 /* The current statement tree. */
122 static GTY(()) struct stmt_tree_s c_stmt_tree;
124 /* State saving variables. */
128 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
129 included in this invocation. Note that the current translation
130 unit is not included in this list. */
132 static GTY(()) tree all_translation_units;
134 /* A list of decls to be made automatically visible in each file scope. */
135 static GTY(()) tree visible_builtins;
137 /* Set to 0 at beginning of a function definition, set to 1 if
138 a return statement that specifies a return value is seen. */
140 int current_function_returns_value;
142 /* Set to 0 at beginning of a function definition, set to 1 if
143 a return statement with no argument is seen. */
145 int current_function_returns_null;
147 /* Set to 0 at beginning of a function definition, set to 1 if
148 a call to a noreturn function is seen. */
150 int current_function_returns_abnormally;
152 /* Set to nonzero by `grokdeclarator' for a function
153 whose return type is defaulted, if warnings for this are desired. */
155 static int warn_about_return_type;
157 /* Nonzero when starting a function declared `extern inline'. */
159 static int current_extern_inline;
161 /* Nonzero when the current toplevel function contains a declaration
162 of a nested function which is never defined. */
164 static bool undef_nested_function;
166 /* True means global_bindings_p should return false even if the scope stack
167 says we are in file scope. */
168 bool c_override_global_bindings_to_false;
171 /* Each c_binding structure describes one binding of an identifier to
172 a decl. All the decls in a scope - irrespective of namespace - are
173 chained together by the ->prev field, which (as the name implies)
174 runs in reverse order. All the decls in a given namespace bound to
175 a given identifier are chained by the ->shadowed field, which runs
176 from inner to outer scopes.
178 The ->decl field usually points to a DECL node, but there are two
179 exceptions. In the namespace of type tags, the bound entity is a
180 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
181 identifier is encountered, it is bound to error_mark_node to
182 suppress further errors about that identifier in the current
185 The ->type field stores the type of the declaration in this scope;
186 if NULL, the type is the type of the ->decl field. This is only of
187 relevance for objects with external or internal linkage which may
188 be redeclared in inner scopes, forming composite types that only
189 persist for the duration of those scopes. In the external scope,
190 this stores the composite of all the types declared for this
191 object, visible or not. The ->inner_comp field (used only at file
192 scope) stores whether an incomplete array type at file scope was
193 completed at an inner scope to an array size other than 1.
195 The depth field is copied from the scope structure that holds this
196 decl. It is used to preserve the proper ordering of the ->shadowed
197 field (see bind()) and also for a handful of special-case checks.
198 Finally, the invisible bit is true for a decl which should be
199 ignored for purposes of normal name lookup, and the nested bit is
200 true for a decl that's been bound a second time in an inner scope;
201 in all such cases, the binding in the outer scope will have its
202 invisible bit true. */
204 struct c_binding GTY((chain_next ("%h.prev")))
206 tree decl; /* the decl bound */
207 tree type; /* the type in this scope */
208 tree id; /* the identifier it's bound to */
209 struct c_binding *prev; /* the previous decl in this scope */
210 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
211 unsigned int depth : 28; /* depth of this scope */
212 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
213 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
214 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
217 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
218 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
219 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
220 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
222 #define I_SYMBOL_BINDING(node) \
223 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
224 #define I_SYMBOL_DECL(node) \
225 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
227 #define I_TAG_BINDING(node) \
228 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
229 #define I_TAG_DECL(node) \
230 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
232 #define I_LABEL_BINDING(node) \
233 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
234 #define I_LABEL_DECL(node) \
235 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
237 /* Each C symbol points to three linked lists of c_binding structures.
238 These describe the values of the identifier in the three different
239 namespaces defined by the language. */
241 struct lang_identifier GTY(())
243 struct c_common_identifier common_id;
244 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
245 struct c_binding *tag_binding; /* struct/union/enum tags */
246 struct c_binding *label_binding; /* labels */
249 /* Validate c-lang.c's assumptions. */
250 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
251 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
253 /* The resulting tree type. */
256 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
257 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)")))
259 union tree_node GTY ((tag ("0"),
260 desc ("tree_node_structure (&%h)")))
262 struct lang_identifier GTY ((tag ("1"))) identifier;
265 /* Each c_scope structure describes the complete contents of one
266 scope. Four scopes are distinguished specially: the innermost or
267 current scope, the innermost function scope, the file scope (always
268 the second to outermost) and the outermost or external scope.
270 Most declarations are recorded in the current scope.
272 All normal label declarations are recorded in the innermost
273 function scope, as are bindings of undeclared identifiers to
274 error_mark_node. (GCC permits nested functions as an extension,
275 hence the 'innermost' qualifier.) Explicitly declared labels
276 (using the __label__ extension) appear in the current scope.
278 Being in the file scope (current_scope == file_scope) causes
279 special behavior in several places below. Also, under some
280 conditions the Objective-C front end records declarations in the
281 file scope even though that isn't the current scope.
283 All declarations with external linkage are recorded in the external
284 scope, even if they aren't visible there; this models the fact that
285 such declarations are visible to the entire program, and (with a
286 bit of cleverness, see pushdecl) allows diagnosis of some violations
287 of C99 6.2.2p7 and 6.2.7p2:
289 If, within the same translation unit, the same identifier appears
290 with both internal and external linkage, the behavior is
293 All declarations that refer to the same object or function shall
294 have compatible type; otherwise, the behavior is undefined.
296 Initially only the built-in declarations, which describe compiler
297 intrinsic functions plus a subset of the standard library, are in
300 The order of the blocks list matters, and it is frequently appended
301 to. To avoid having to walk all the way to the end of the list on
302 each insertion, or reverse the list later, we maintain a pointer to
303 the last list entry. (FIXME: It should be feasible to use a reversed
306 The bindings list is strictly in reverse order of declarations;
307 pop_scope relies on this. */
310 struct c_scope GTY((chain_next ("%h.outer")))
312 /* The scope containing this one. */
313 struct c_scope *outer;
315 /* The next outermost function scope. */
316 struct c_scope *outer_function;
318 /* All bindings in this scope. */
319 struct c_binding *bindings;
321 /* For each scope (except the global one), a chain of BLOCK nodes
322 for all the scopes that were entered and exited one level down. */
326 /* The depth of this scope. Used to keep the ->shadowed chain of
327 bindings sorted innermost to outermost. */
328 unsigned int depth : 28;
330 /* True if we are currently filling this scope with parameter
332 BOOL_BITFIELD parm_flag : 1;
334 /* True if we already complained about forward parameter decls
335 in this scope. This prevents double warnings on
336 foo (int a; int b; ...) */
337 BOOL_BITFIELD warned_forward_parm_decls : 1;
339 /* True if this is the outermost block scope of a function body.
340 This scope contains the parameters, the local variables declared
341 in the outermost block, and all the labels (except those in
342 nested functions, or declared at block scope with __label__). */
343 BOOL_BITFIELD function_body : 1;
345 /* True means make a BLOCK for this scope no matter what. */
346 BOOL_BITFIELD keep : 1;
349 /* The scope currently in effect. */
351 static GTY(()) struct c_scope *current_scope;
353 /* The innermost function scope. Ordinary (not explicitly declared)
354 labels, bindings to error_mark_node, and the lazily-created
355 bindings of __func__ and its friends get this scope. */
357 static GTY(()) struct c_scope *current_function_scope;
359 /* The C file scope. This is reset for each input translation unit. */
361 static GTY(()) struct c_scope *file_scope;
363 /* The outermost scope. This is used for all declarations with
364 external linkage, and only these, hence the name. */
366 static GTY(()) struct c_scope *external_scope;
368 /* A chain of c_scope structures awaiting reuse. */
370 static GTY((deletable)) struct c_scope *scope_freelist;
372 /* A chain of c_binding structures awaiting reuse. */
374 static GTY((deletable)) struct c_binding *binding_freelist;
376 /* Append VAR to LIST in scope SCOPE. */
377 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
378 struct c_scope *s_ = (scope); \
380 if (s_->list##_last) \
381 TREE_CHAIN (s_->list##_last) = d_; \
384 s_->list##_last = d_; \
387 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
388 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
389 struct c_scope *t_ = (tscope); \
390 struct c_scope *f_ = (fscope); \
392 TREE_CHAIN (t_->to##_last) = f_->from; \
395 t_->to##_last = f_->from##_last; \
398 /* True means unconditionally make a BLOCK for the next scope pushed. */
400 static bool keep_next_level_flag;
402 /* True means the next call to push_scope will be the outermost scope
403 of a function body, so do not push a new scope, merely cease
404 expecting parameter decls. */
406 static bool next_is_function_body;
408 /* Functions called automatically at the beginning and end of execution. */
410 static GTY(()) tree static_ctors;
411 static GTY(()) tree static_dtors;
413 /* Forward declarations. */
414 static tree lookup_name_in_scope (tree, struct c_scope *);
415 static tree c_make_fname_decl (tree, int);
416 static tree grokdeclarator (const struct c_declarator *,
417 struct c_declspecs *,
418 enum decl_context, bool, tree *);
419 static tree grokparms (struct c_arg_info *, bool);
420 static void layout_array_type (tree);
422 /* T is a statement. Add it to the statement-tree. This is the
423 C/ObjC version--C++ has a slightly different version of this
429 enum tree_code code = TREE_CODE (t);
431 if (EXPR_P (t) && code != LABEL_EXPR)
433 if (!EXPR_HAS_LOCATION (t))
434 SET_EXPR_LOCATION (t, input_location);
437 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
438 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
440 /* Add T to the statement-tree. Non-side-effect statements need to be
441 recorded during statement expressions. */
442 append_to_statement_list_force (t, &cur_stmt_list);
447 /* States indicating how grokdeclarator() should handle declspecs marked
448 with __attribute__((deprecated)). An object declared as
449 __attribute__((deprecated)) suppresses warnings of uses of other
452 enum deprecated_states {
457 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
460 c_print_identifier (FILE *file, tree node, int indent)
462 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
463 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
464 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
465 if (C_IS_RESERVED_WORD (node))
467 tree rid = ridpointers[C_RID_CODE (node)];
468 indent_to (file, indent + 4);
469 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
470 (void *) rid, IDENTIFIER_POINTER (rid));
474 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
475 which may be any of several kinds of DECL or TYPE or error_mark_node,
476 in the scope SCOPE. */
478 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
480 struct c_binding *b, **here;
482 if (binding_freelist)
484 b = binding_freelist;
485 binding_freelist = b->prev;
488 b = GGC_NEW (struct c_binding);
493 b->depth = scope->depth;
494 b->invisible = invisible;
500 b->prev = scope->bindings;
506 switch (TREE_CODE (decl))
508 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
511 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
517 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
523 /* Locate the appropriate place in the chain of shadowed decls
524 to insert this binding. Normally, scope == current_scope and
525 this does nothing. */
526 while (*here && (*here)->depth > scope->depth)
527 here = &(*here)->shadowed;
533 /* Clear the binding structure B, stick it on the binding_freelist,
534 and return the former value of b->prev. This is used by pop_scope
535 and get_parm_info to iterate destructively over all the bindings
536 from a given scope. */
537 static struct c_binding *
538 free_binding_and_advance (struct c_binding *b)
540 struct c_binding *prev = b->prev;
542 memset (b, 0, sizeof (struct c_binding));
543 b->prev = binding_freelist;
544 binding_freelist = b;
550 /* Hook called at end of compilation to assume 1 elt
551 for a file-scope tentative array defn that wasn't complete before. */
554 c_finish_incomplete_decl (tree decl)
556 if (TREE_CODE (decl) == VAR_DECL)
558 tree type = TREE_TYPE (decl);
559 if (type != error_mark_node
560 && TREE_CODE (type) == ARRAY_TYPE
561 && !DECL_EXTERNAL (decl)
562 && TYPE_DOMAIN (type) == 0)
564 warning (0, "array %q+D assumed to have one element", decl);
566 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
568 layout_decl (decl, 0);
573 /* The Objective-C front-end often needs to determine the current scope. */
576 objc_get_current_scope (void)
578 return current_scope;
581 /* The following function is used only by Objective-C. It needs to live here
582 because it accesses the innards of c_scope. */
585 objc_mark_locals_volatile (void *enclosing_blk)
587 struct c_scope *scope;
590 for (scope = current_scope;
591 scope && scope != enclosing_blk;
592 scope = scope->outer)
594 for (b = scope->bindings; b; b = b->prev)
595 objc_volatilize_decl (b->decl);
597 /* Do not climb up past the current function. */
598 if (scope->function_body)
603 /* Nonzero if we are currently in file scope. */
606 global_bindings_p (void)
608 return current_scope == file_scope && !c_override_global_bindings_to_false;
612 keep_next_level (void)
614 keep_next_level_flag = true;
617 /* Identify this scope as currently being filled with parameters. */
620 declare_parm_level (void)
622 current_scope->parm_flag = true;
628 if (next_is_function_body)
630 /* This is the transition from the parameters to the top level
631 of the function body. These are the same scope
632 (C99 6.2.1p4,6) so we do not push another scope structure.
633 next_is_function_body is set only by store_parm_decls, which
634 in turn is called when and only when we are about to
635 encounter the opening curly brace for the function body.
637 The outermost block of a function always gets a BLOCK node,
638 because the debugging output routines expect that each
639 function has at least one BLOCK. */
640 current_scope->parm_flag = false;
641 current_scope->function_body = true;
642 current_scope->keep = true;
643 current_scope->outer_function = current_function_scope;
644 current_function_scope = current_scope;
646 keep_next_level_flag = false;
647 next_is_function_body = false;
651 struct c_scope *scope;
654 scope = scope_freelist;
655 scope_freelist = scope->outer;
658 scope = GGC_CNEW (struct c_scope);
660 scope->keep = keep_next_level_flag;
661 scope->outer = current_scope;
662 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
664 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
666 if (current_scope && scope->depth == 0)
669 sorry ("GCC supports only %u nested scopes", scope->depth);
672 current_scope = scope;
673 keep_next_level_flag = false;
677 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
680 set_type_context (tree type, tree context)
682 for (type = TYPE_MAIN_VARIANT (type); type;
683 type = TYPE_NEXT_VARIANT (type))
684 TYPE_CONTEXT (type) = context;
687 /* Exit a scope. Restore the state of the identifier-decl mappings
688 that were in effect when this scope was entered. Return a BLOCK
689 node containing all the DECLs in this scope that are of interest
690 to debug info generation. */
695 struct c_scope *scope = current_scope;
696 tree block, context, p;
699 bool functionbody = scope->function_body;
700 bool keep = functionbody || scope->keep || scope->bindings;
702 c_end_vm_scope (scope->depth);
704 /* If appropriate, create a BLOCK to record the decls for the life
709 block = make_node (BLOCK);
710 BLOCK_SUBBLOCKS (block) = scope->blocks;
711 TREE_USED (block) = 1;
713 /* In each subblock, record that this is its superior. */
714 for (p = scope->blocks; p; p = TREE_CHAIN (p))
715 BLOCK_SUPERCONTEXT (p) = block;
717 BLOCK_VARS (block) = 0;
720 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
721 scope must be set so that they point to the appropriate
722 construct, i.e. either to the current FUNCTION_DECL node, or
723 else to the BLOCK node we just constructed.
725 Note that for tagged types whose scope is just the formal
726 parameter list for some function type specification, we can't
727 properly set their TYPE_CONTEXTs here, because we don't have a
728 pointer to the appropriate FUNCTION_TYPE node readily available
729 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
730 type nodes get set in `grokdeclarator' as soon as we have created
731 the FUNCTION_TYPE node which will represent the "scope" for these
732 "parameter list local" tagged types. */
733 if (scope->function_body)
734 context = current_function_decl;
735 else if (scope == file_scope)
737 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
738 TREE_CHAIN (file_decl) = all_translation_units;
739 all_translation_units = file_decl;
745 /* Clear all bindings in this scope. */
746 for (b = scope->bindings; b; b = free_binding_and_advance (b))
749 switch (TREE_CODE (p))
752 /* Warnings for unused labels, errors for undefined labels. */
753 if (TREE_USED (p) && !DECL_INITIAL (p))
755 error ("label %q+D used but not defined", p);
756 DECL_INITIAL (p) = error_mark_node;
758 else if (!TREE_USED (p) && warn_unused_label)
760 if (DECL_INITIAL (p))
761 warning (0, "label %q+D defined but not used", p);
763 warning (0, "label %q+D declared but not defined", p);
765 /* Labels go in BLOCK_VARS. */
766 TREE_CHAIN (p) = BLOCK_VARS (block);
767 BLOCK_VARS (block) = p;
768 gcc_assert (I_LABEL_BINDING (b->id) == b);
769 I_LABEL_BINDING (b->id) = b->shadowed;
775 set_type_context (p, context);
777 /* Types may not have tag-names, in which case the type
778 appears in the bindings list with b->id NULL. */
781 gcc_assert (I_TAG_BINDING (b->id) == b);
782 I_TAG_BINDING (b->id) = b->shadowed;
787 /* Propagate TREE_ADDRESSABLE from nested functions to their
788 containing functions. */
789 if (!TREE_ASM_WRITTEN (p)
790 && DECL_INITIAL (p) != 0
791 && TREE_ADDRESSABLE (p)
792 && DECL_ABSTRACT_ORIGIN (p) != 0
793 && DECL_ABSTRACT_ORIGIN (p) != p)
794 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
795 if (!DECL_EXTERNAL (p)
796 && DECL_INITIAL (p) == 0)
798 error ("nested function %q+D declared but never defined", p);
799 undef_nested_function = true;
804 /* Warnings for unused variables. */
806 && !DECL_IN_SYSTEM_HEADER (p)
808 && !DECL_ARTIFICIAL (p)
809 && scope != file_scope
810 && scope != external_scope)
811 warning (OPT_Wunused_variable, "unused variable %q+D", p);
815 error ("type of array %q+D completed incompatibly with"
816 " implicit initialization", p);
823 /* All of these go in BLOCK_VARS, but only if this is the
824 binding in the home scope. */
827 TREE_CHAIN (p) = BLOCK_VARS (block);
828 BLOCK_VARS (block) = p;
830 /* If this is the file scope, and we are processing more
831 than one translation unit in this compilation, set
832 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
833 This makes same_translation_unit_p work, and causes
834 static declarations to be given disambiguating suffixes. */
835 if (scope == file_scope && num_in_fnames > 1)
837 DECL_CONTEXT (p) = context;
838 if (TREE_CODE (p) == TYPE_DECL)
839 set_type_context (TREE_TYPE (p), context);
843 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
844 already been put there by store_parm_decls. Unused-
845 parameter warnings are handled by function.c.
846 error_mark_node obviously does not go in BLOCK_VARS and
847 does not get unused-variable warnings. */
850 /* It is possible for a decl not to have a name. We get
851 here with b->id NULL in this case. */
854 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
855 I_SYMBOL_BINDING (b->id) = b->shadowed;
856 if (b->shadowed && b->shadowed->type)
857 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
867 /* Dispose of the block that we just made inside some higher level. */
868 if ((scope->function_body || scope == file_scope) && context)
870 DECL_INITIAL (context) = block;
871 BLOCK_SUPERCONTEXT (block) = context;
873 else if (scope->outer)
876 SCOPE_LIST_APPEND (scope->outer, blocks, block);
877 /* If we did not make a block for the scope just exited, any
878 blocks made for inner scopes must be carried forward so they
879 will later become subblocks of something else. */
880 else if (scope->blocks)
881 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
884 /* Pop the current scope, and free the structure for reuse. */
885 current_scope = scope->outer;
886 if (scope->function_body)
887 current_function_scope = scope->outer_function;
889 memset (scope, 0, sizeof (struct c_scope));
890 scope->outer = scope_freelist;
891 scope_freelist = scope;
897 push_file_scope (void)
905 file_scope = current_scope;
907 start_fname_decls ();
909 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
910 bind (DECL_NAME (decl), decl, file_scope,
911 /*invisible=*/false, /*nested=*/true);
915 pop_file_scope (void)
917 /* In case there were missing closebraces, get us back to the global
919 while (current_scope != file_scope)
922 /* __FUNCTION__ is defined at file scope (""). This
923 call may not be necessary as my tests indicate it
924 still works without it. */
925 finish_fname_decls ();
927 /* This is the point to write out a PCH if we're doing that.
928 In that case we do not want to do anything else. */
931 c_common_write_pch ();
935 /* Pop off the file scope and close this translation unit. */
939 maybe_apply_pending_pragma_weaks ();
940 cgraph_finalize_compilation_unit ();
943 /* Insert BLOCK at the end of the list of subblocks of the current
944 scope. This is used when a BIND_EXPR is expanded, to handle the
945 BLOCK node inside the BIND_EXPR. */
948 insert_block (tree block)
950 TREE_USED (block) = 1;
951 SCOPE_LIST_APPEND (current_scope, blocks, block);
954 /* Push a definition or a declaration of struct, union or enum tag "name".
955 "type" should be the type node.
956 We assume that the tag "name" is not already defined.
958 Note that the definition may really be just a forward reference.
959 In that case, the TYPE_SIZE will be zero. */
962 pushtag (tree name, tree type)
964 /* Record the identifier as the type's name if it has none. */
965 if (name && !TYPE_NAME (type))
966 TYPE_NAME (type) = name;
967 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
969 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
970 tagged type we just added to the current scope. This fake
971 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
972 to output a representation of a tagged type, and it also gives
973 us a convenient place to record the "scope start" address for the
976 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
978 /* An approximation for now, so we can tell this is a function-scope tag.
979 This will be updated in pop_scope. */
980 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
983 /* Subroutine of compare_decls. Allow harmless mismatches in return
984 and argument types provided that the type modes match. This function
985 return a unified type given a suitable match, and 0 otherwise. */
988 match_builtin_function_types (tree newtype, tree oldtype)
990 tree newrettype, oldrettype;
991 tree newargs, oldargs;
992 tree trytype, tryargs;
994 /* Accept the return type of the new declaration if same modes. */
995 oldrettype = TREE_TYPE (oldtype);
996 newrettype = TREE_TYPE (newtype);
998 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1001 oldargs = TYPE_ARG_TYPES (oldtype);
1002 newargs = TYPE_ARG_TYPES (newtype);
1005 while (oldargs || newargs)
1009 || !TREE_VALUE (oldargs)
1010 || !TREE_VALUE (newargs)
1011 || TYPE_MODE (TREE_VALUE (oldargs))
1012 != TYPE_MODE (TREE_VALUE (newargs)))
1015 oldargs = TREE_CHAIN (oldargs);
1016 newargs = TREE_CHAIN (newargs);
1019 trytype = build_function_type (newrettype, tryargs);
1020 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1023 /* Subroutine of diagnose_mismatched_decls. Check for function type
1024 mismatch involving an empty arglist vs a nonempty one and give clearer
1027 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1028 tree newtype, tree oldtype)
1032 if (TREE_CODE (olddecl) != FUNCTION_DECL
1033 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1034 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1036 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1039 t = TYPE_ARG_TYPES (oldtype);
1041 t = TYPE_ARG_TYPES (newtype);
1042 for (; t; t = TREE_CHAIN (t))
1044 tree type = TREE_VALUE (t);
1046 if (TREE_CHAIN (t) == 0
1047 && TYPE_MAIN_VARIANT (type) != void_type_node)
1049 inform ("a parameter list with an ellipsis can%'t match "
1050 "an empty parameter name list declaration");
1054 if (c_type_promotes_to (type) != type)
1056 inform ("an argument type that has a default promotion can%'t match "
1057 "an empty parameter name list declaration");
1063 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1064 old-style function definition, NEWDECL is a prototype declaration.
1065 Diagnose inconsistencies in the argument list. Returns TRUE if
1066 the prototype is compatible, FALSE if not. */
1068 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1070 tree newargs, oldargs;
1073 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1075 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1076 newargs = TYPE_ARG_TYPES (newtype);
1081 tree oldargtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1082 tree newargtype = TYPE_MAIN_VARIANT (TREE_VALUE (newargs));
1084 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1087 /* Reaching the end of just one list means the two decls don't
1088 agree on the number of arguments. */
1089 if (END_OF_ARGLIST (oldargtype))
1091 error ("prototype for %q+D declares more arguments "
1092 "than previous old-style definition", newdecl);
1095 else if (END_OF_ARGLIST (newargtype))
1097 error ("prototype for %q+D declares fewer arguments "
1098 "than previous old-style definition", newdecl);
1102 /* Type for passing arg must be consistent with that declared
1104 else if (!comptypes (oldargtype, newargtype))
1106 error ("prototype for %q+D declares argument %d"
1107 " with incompatible type",
1112 oldargs = TREE_CHAIN (oldargs);
1113 newargs = TREE_CHAIN (newargs);
1117 /* If we get here, no errors were found, but do issue a warning
1118 for this poor-style construct. */
1119 warning (0, "prototype for %q+D follows non-prototype definition",
1122 #undef END_OF_ARGLIST
1125 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1126 first in a pair of mismatched declarations, using the diagnostic
1129 locate_old_decl (tree decl, void (*diag)(const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2))
1131 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1133 else if (DECL_INITIAL (decl))
1134 diag (G_("previous definition of %q+D was here"), decl);
1135 else if (C_DECL_IMPLICIT (decl))
1136 diag (G_("previous implicit declaration of %q+D was here"), decl);
1138 diag (G_("previous declaration of %q+D was here"), decl);
1141 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1142 Returns true if the caller should proceed to merge the two, false
1143 if OLDDECL should simply be discarded. As a side effect, issues
1144 all necessary diagnostics for invalid or poor-style combinations.
1145 If it returns true, writes the types of NEWDECL and OLDDECL to
1146 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1147 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1150 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1151 tree *newtypep, tree *oldtypep)
1153 tree newtype, oldtype;
1154 bool pedwarned = false;
1155 bool warned = false;
1158 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1159 && DECL_EXTERNAL (DECL))
1161 /* If we have error_mark_node for either decl or type, just discard
1162 the previous decl - we're in an error cascade already. */
1163 if (olddecl == error_mark_node || newdecl == error_mark_node)
1165 *oldtypep = oldtype = TREE_TYPE (olddecl);
1166 *newtypep = newtype = TREE_TYPE (newdecl);
1167 if (oldtype == error_mark_node || newtype == error_mark_node)
1170 /* Two different categories of symbol altogether. This is an error
1171 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1172 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1174 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1175 && DECL_BUILT_IN (olddecl)
1176 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1178 error ("%q+D redeclared as different kind of symbol", newdecl);
1179 locate_old_decl (olddecl, error);
1181 else if (TREE_PUBLIC (newdecl))
1182 warning (0, "built-in function %q+D declared as non-function",
1185 warning (OPT_Wshadow, "declaration of %q+D shadows "
1186 "a built-in function", newdecl);
1190 /* Enumerators have no linkage, so may only be declared once in a
1192 if (TREE_CODE (olddecl) == CONST_DECL)
1194 error ("redeclaration of enumerator %q+D", newdecl);
1195 locate_old_decl (olddecl, error);
1199 if (!comptypes (oldtype, newtype))
1201 if (TREE_CODE (olddecl) == FUNCTION_DECL
1202 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1204 /* Accept harmless mismatch in function types.
1205 This is for the ffs and fprintf builtins. */
1206 tree trytype = match_builtin_function_types (newtype, oldtype);
1208 if (trytype && comptypes (newtype, trytype))
1209 *oldtypep = oldtype = trytype;
1212 /* If types don't match for a built-in, throw away the
1213 built-in. No point in calling locate_old_decl here, it
1214 won't print anything. */
1215 warning (0, "conflicting types for built-in function %q+D",
1220 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1221 && DECL_IS_BUILTIN (olddecl))
1223 /* A conflicting function declaration for a predeclared
1224 function that isn't actually built in. Objective C uses
1225 these. The new declaration silently overrides everything
1226 but the volatility (i.e. noreturn) indication. See also
1227 below. FIXME: Make Objective C use normal builtins. */
1228 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1231 /* Permit void foo (...) to match int foo (...) if the latter is
1232 the definition and implicit int was used. See
1233 c-torture/compile/920625-2.c. */
1234 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1235 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1236 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1237 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1239 pedwarn ("conflicting types for %q+D", newdecl);
1240 /* Make sure we keep void as the return type. */
1241 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1242 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1245 /* Permit void foo (...) to match an earlier call to foo (...) with
1246 no declared type (thus, implicitly int). */
1247 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1248 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1249 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1250 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1252 pedwarn ("conflicting types for %q+D", newdecl);
1253 /* Make sure we keep void as the return type. */
1254 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1259 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1260 error ("conflicting type qualifiers for %q+D", newdecl);
1262 error ("conflicting types for %q+D", newdecl);
1263 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1264 locate_old_decl (olddecl, error);
1269 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1270 but silently ignore the redeclaration if either is in a system
1271 header. (Conflicting redeclarations were handled above.) */
1272 if (TREE_CODE (newdecl) == TYPE_DECL)
1274 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1275 return true; /* Allow OLDDECL to continue in use. */
1277 error ("redefinition of typedef %q+D", newdecl);
1278 locate_old_decl (olddecl, error);
1282 /* Function declarations can either be 'static' or 'extern' (no
1283 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1284 can never conflict with each other on account of linkage (6.2.2p4).
1285 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1286 two definitions if one is 'extern inline' and one is not. The non-
1287 extern-inline definition supersedes the extern-inline definition. */
1289 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1291 /* If you declare a built-in function name as static, or
1292 define the built-in with an old-style definition (so we
1293 can't validate the argument list) the built-in definition is
1294 overridden, but optionally warn this was a bad choice of name. */
1295 if (DECL_BUILT_IN (olddecl)
1296 && !C_DECL_DECLARED_BUILTIN (olddecl)
1297 && (!TREE_PUBLIC (newdecl)
1298 || (DECL_INITIAL (newdecl)
1299 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1301 warning (OPT_Wshadow, "declaration of %q+D shadows "
1302 "a built-in function", newdecl);
1303 /* Discard the old built-in function. */
1307 if (DECL_INITIAL (newdecl))
1309 if (DECL_INITIAL (olddecl))
1311 /* If both decls are in the same TU and the new declaration
1312 isn't overriding an extern inline reject the new decl.
1313 When we handle c99 style inline rules we'll want to reject
1316 DECL_EXTERN_INLINE (olddecl)
1317 && !DECL_EXTERN_INLINE (newdecl)
1319 if they're in the same translation unit. Until we implement
1320 the full semantics we accept the construct. */
1321 if (!(DECL_EXTERN_INLINE (olddecl)
1322 && !DECL_EXTERN_INLINE (newdecl))
1323 && same_translation_unit_p (newdecl, olddecl))
1325 error ("redefinition of %q+D", newdecl);
1326 locate_old_decl (olddecl, error);
1331 /* If we have a prototype after an old-style function definition,
1332 the argument types must be checked specially. */
1333 else if (DECL_INITIAL (olddecl)
1334 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1335 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1336 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1338 locate_old_decl (olddecl, error);
1341 /* A non-static declaration (even an "extern") followed by a
1342 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1343 The same is true for a static forward declaration at block
1344 scope followed by a non-static declaration/definition at file
1345 scope. Static followed by non-static at the same scope is
1346 not undefined behavior, and is the most convenient way to get
1347 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1348 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1349 we do diagnose it if -Wtraditional. */
1350 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1352 /* Two exceptions to the rule. If olddecl is an extern
1353 inline, or a predeclared function that isn't actually
1354 built in, newdecl silently overrides olddecl. The latter
1355 occur only in Objective C; see also above. (FIXME: Make
1356 Objective C use normal builtins.) */
1357 if (!DECL_IS_BUILTIN (olddecl)
1358 && !DECL_EXTERN_INLINE (olddecl))
1360 error ("static declaration of %q+D follows "
1361 "non-static declaration", newdecl);
1362 locate_old_decl (olddecl, error);
1366 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1368 if (DECL_CONTEXT (olddecl))
1370 error ("non-static declaration of %q+D follows "
1371 "static declaration", newdecl);
1372 locate_old_decl (olddecl, error);
1375 else if (warn_traditional)
1377 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1378 "follows static declaration", newdecl);
1383 else if (TREE_CODE (newdecl) == VAR_DECL)
1385 /* Only variables can be thread-local, and all declarations must
1386 agree on this property. */
1387 if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1389 if (DECL_THREAD_LOCAL_P (newdecl))
1390 error ("thread-local declaration of %q+D follows "
1391 "non-thread-local declaration", newdecl);
1393 error ("non-thread-local declaration of %q+D follows "
1394 "thread-local declaration", newdecl);
1396 locate_old_decl (olddecl, error);
1400 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1401 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1403 error ("redefinition of %q+D", newdecl);
1404 locate_old_decl (olddecl, error);
1408 /* Objects declared at file scope: if the first declaration had
1409 external linkage (even if it was an external reference) the
1410 second must have external linkage as well, or the behavior is
1411 undefined. If the first declaration had internal linkage, then
1412 the second must too, or else be an external reference (in which
1413 case the composite declaration still has internal linkage).
1414 As for function declarations, we warn about the static-then-
1415 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1416 if (DECL_FILE_SCOPE_P (newdecl)
1417 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1419 if (DECL_EXTERNAL (newdecl))
1421 if (!DECL_FILE_SCOPE_P (olddecl))
1423 error ("extern declaration of %q+D follows "
1424 "declaration with no linkage", newdecl);
1425 locate_old_decl (olddecl, error);
1428 else if (warn_traditional)
1430 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1431 "follows static declaration", newdecl);
1437 if (TREE_PUBLIC (newdecl))
1438 error ("non-static declaration of %q+D follows "
1439 "static declaration", newdecl);
1441 error ("static declaration of %q+D follows "
1442 "non-static declaration", newdecl);
1444 locate_old_decl (olddecl, error);
1448 /* Two objects with the same name declared at the same block
1449 scope must both be external references (6.7p3). */
1450 else if (!DECL_FILE_SCOPE_P (newdecl))
1452 if (DECL_EXTERNAL (newdecl))
1454 /* Extern with initializer at block scope, which will
1455 already have received an error. */
1457 else if (DECL_EXTERNAL (olddecl))
1459 error ("declaration of %q+D with no linkage follows "
1460 "extern declaration", newdecl);
1461 locate_old_decl (olddecl, error);
1465 error ("redeclaration of %q+D with no linkage", newdecl);
1466 locate_old_decl (olddecl, error);
1474 /* All decls must agree on a visibility. */
1475 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1476 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1477 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1479 warning (0, "redeclaration of %q+D with different visibility "
1480 "(old visibility preserved)", newdecl);
1484 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1486 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1487 if (DECL_DECLARED_INLINE_P (newdecl)
1488 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1490 warning (OPT_Wattributes, "inline declaration of %qD follows "
1491 "declaration with attribute noinline", newdecl);
1494 else if (DECL_DECLARED_INLINE_P (olddecl)
1495 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1497 warning (OPT_Wattributes, "declaration of %q+D with attribute "
1498 "noinline follows inline declaration ", newdecl);
1502 /* Inline declaration after use or definition.
1503 ??? Should we still warn about this now we have unit-at-a-time
1504 mode and can get it right?
1505 Definitely don't complain if the decls are in different translation
1507 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1508 && same_translation_unit_p (olddecl, newdecl))
1510 if (TREE_USED (olddecl))
1512 warning (0, "%q+D declared inline after being called", olddecl);
1515 else if (DECL_INITIAL (olddecl))
1517 warning (0, "%q+D declared inline after its definition", olddecl);
1522 else /* PARM_DECL, VAR_DECL */
1524 /* Redeclaration of a parameter is a constraint violation (this is
1525 not explicitly stated, but follows from C99 6.7p3 [no more than
1526 one declaration of the same identifier with no linkage in the
1527 same scope, except type tags] and 6.2.2p6 [parameters have no
1528 linkage]). We must check for a forward parameter declaration,
1529 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1530 an extension, the mandatory diagnostic for which is handled by
1531 mark_forward_parm_decls. */
1533 if (TREE_CODE (newdecl) == PARM_DECL
1534 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1536 error ("redefinition of parameter %q+D", newdecl);
1537 locate_old_decl (olddecl, error);
1542 /* Optional warning for completely redundant decls. */
1543 if (!warned && !pedwarned
1544 && warn_redundant_decls
1545 /* Don't warn about a function declaration followed by a
1547 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1548 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1549 /* Don't warn about redundant redeclarations of builtins. */
1550 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1551 && !DECL_BUILT_IN (newdecl)
1552 && DECL_BUILT_IN (olddecl)
1553 && !C_DECL_DECLARED_BUILTIN (olddecl))
1554 /* Don't warn about an extern followed by a definition. */
1555 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1556 /* Don't warn about forward parameter decls. */
1557 && !(TREE_CODE (newdecl) == PARM_DECL
1558 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1560 warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1565 /* Report location of previous decl/defn in a consistent manner. */
1566 if (warned || pedwarned)
1567 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1569 #undef DECL_EXTERN_INLINE
1574 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1575 consistent with OLDDECL, but carries new information. Merge the
1576 new information into OLDDECL. This function issues no
1580 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1582 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1583 && DECL_INITIAL (newdecl) != 0);
1584 int new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1585 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1586 int old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1587 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1589 /* For real parm decl following a forward decl, rechain the old decl
1590 in its new location and clear TREE_ASM_WRITTEN (it's not a
1591 forward decl anymore). */
1592 if (TREE_CODE (newdecl) == PARM_DECL
1593 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1595 struct c_binding *b, **here;
1597 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
1598 if ((*here)->decl == olddecl)
1605 b->prev = current_scope->bindings;
1606 current_scope->bindings = b;
1608 TREE_ASM_WRITTEN (olddecl) = 0;
1611 DECL_ATTRIBUTES (newdecl)
1612 = targetm.merge_decl_attributes (olddecl, newdecl);
1614 /* Merge the data types specified in the two decls. */
1616 = TREE_TYPE (olddecl)
1617 = composite_type (newtype, oldtype);
1619 /* Lay the type out, unless already done. */
1620 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1622 if (TREE_TYPE (newdecl) != error_mark_node)
1623 layout_type (TREE_TYPE (newdecl));
1624 if (TREE_CODE (newdecl) != FUNCTION_DECL
1625 && TREE_CODE (newdecl) != TYPE_DECL
1626 && TREE_CODE (newdecl) != CONST_DECL)
1627 layout_decl (newdecl, 0);
1631 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1632 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1633 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1634 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1635 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1636 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1638 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1639 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1644 /* Merge the type qualifiers. */
1645 if (TREE_READONLY (newdecl))
1646 TREE_READONLY (olddecl) = 1;
1648 if (TREE_THIS_VOLATILE (newdecl))
1650 TREE_THIS_VOLATILE (olddecl) = 1;
1651 if (TREE_CODE (newdecl) == VAR_DECL)
1652 make_var_volatile (newdecl);
1655 /* Merge deprecatedness. */
1656 if (TREE_DEPRECATED (newdecl))
1657 TREE_DEPRECATED (olddecl) = 1;
1659 /* Keep source location of definition rather than declaration and of
1660 prototype rather than non-prototype unless that prototype is
1662 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1663 || (old_is_prototype && !new_is_prototype
1664 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1665 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1667 /* Merge the unused-warning information. */
1668 if (DECL_IN_SYSTEM_HEADER (olddecl))
1669 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1670 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1671 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1673 /* Merge the initialization information. */
1674 if (DECL_INITIAL (newdecl) == 0)
1675 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1677 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1679 /* Merge the section attribute.
1680 We want to issue an error if the sections conflict but that must be
1681 done later in decl_attributes since we are called before attributes
1683 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1684 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1686 /* Copy the assembler name.
1687 Currently, it can only be defined in the prototype. */
1688 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1690 /* Use visibility of whichever declaration had it specified */
1691 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1693 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1694 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1697 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1699 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1700 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1701 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1702 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1703 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1704 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1705 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1706 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1707 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1708 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1711 /* Merge the storage class information. */
1712 merge_weak (newdecl, olddecl);
1714 /* For functions, static overrides non-static. */
1715 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1717 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1718 /* This is since we don't automatically
1719 copy the attributes of NEWDECL into OLDDECL. */
1720 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1721 /* If this clears `static', clear it in the identifier too. */
1722 if (!TREE_PUBLIC (olddecl))
1723 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1727 if (DECL_EXTERNAL (newdecl))
1729 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1730 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1732 /* An extern decl does not override previous storage class. */
1733 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1734 if (!DECL_EXTERNAL (newdecl))
1736 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1737 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1742 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1743 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1746 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1748 /* If we're redefining a function previously defined as extern
1749 inline, make sure we emit debug info for the inline before we
1750 throw it away, in case it was inlined into a function that hasn't
1751 been written out yet. */
1752 if (new_is_definition && DECL_INITIAL (olddecl))
1754 if (TREE_USED (olddecl)
1755 /* In unit-at-a-time mode we never inline re-defined extern
1756 inline functions. */
1757 && !flag_unit_at_a_time
1758 && cgraph_function_possibly_inlined_p (olddecl))
1759 (*debug_hooks->outlining_inline_function) (olddecl);
1761 /* The new defn must not be inline. */
1762 DECL_INLINE (newdecl) = 0;
1763 DECL_UNINLINABLE (newdecl) = 1;
1767 /* If either decl says `inline', this fn is inline,
1768 unless its definition was passed already. */
1769 if (DECL_DECLARED_INLINE_P (newdecl)
1770 || DECL_DECLARED_INLINE_P (olddecl))
1771 DECL_DECLARED_INLINE_P (newdecl) = 1;
1773 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1774 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1777 if (DECL_BUILT_IN (olddecl))
1779 /* If redeclaring a builtin function, it stays built in.
1780 But it gets tagged as having been declared. */
1781 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1782 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1783 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1784 if (new_is_prototype)
1785 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1787 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1788 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1791 /* Also preserve various other info from the definition. */
1792 if (!new_is_definition)
1794 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1795 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1796 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1797 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1798 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1800 /* Set DECL_INLINE on the declaration if we've got a body
1801 from which to instantiate. */
1802 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1804 DECL_INLINE (newdecl) = 1;
1805 DECL_ABSTRACT_ORIGIN (newdecl)
1806 = DECL_ABSTRACT_ORIGIN (olddecl);
1811 /* If a previous declaration said inline, mark the
1812 definition as inlinable. */
1813 if (DECL_DECLARED_INLINE_P (newdecl)
1814 && !DECL_UNINLINABLE (newdecl))
1815 DECL_INLINE (newdecl) = 1;
1819 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1820 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1822 unsigned olddecl_uid = DECL_UID (olddecl);
1823 tree olddecl_context = DECL_CONTEXT (olddecl);
1825 memcpy ((char *) olddecl + sizeof (struct tree_common),
1826 (char *) newdecl + sizeof (struct tree_common),
1827 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1828 switch (TREE_CODE (olddecl))
1838 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1839 (char *) newdecl + sizeof (struct tree_decl_common),
1840 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1845 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1846 (char *) newdecl + sizeof (struct tree_decl_common),
1847 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1849 DECL_UID (olddecl) = olddecl_uid;
1850 DECL_CONTEXT (olddecl) = olddecl_context;
1853 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1854 so that encode_section_info has a chance to look at the new decl
1855 flags and attributes. */
1856 if (DECL_RTL_SET_P (olddecl)
1857 && (TREE_CODE (olddecl) == FUNCTION_DECL
1858 || (TREE_CODE (olddecl) == VAR_DECL
1859 && TREE_STATIC (olddecl))))
1860 make_decl_rtl (olddecl);
1863 /* Handle when a new declaration NEWDECL has the same name as an old
1864 one OLDDECL in the same binding contour. Prints an error message
1867 If safely possible, alter OLDDECL to look like NEWDECL, and return
1868 true. Otherwise, return false. */
1871 duplicate_decls (tree newdecl, tree olddecl)
1873 tree newtype = NULL, oldtype = NULL;
1875 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1878 merge_decls (newdecl, olddecl, newtype, oldtype);
1883 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1885 warn_if_shadowing (tree new_decl)
1887 struct c_binding *b;
1889 /* Shadow warnings wanted? */
1891 /* No shadow warnings for internally generated vars. */
1892 || DECL_IS_BUILTIN (new_decl)
1893 /* No shadow warnings for vars made for inlining. */
1894 || DECL_FROM_INLINE (new_decl))
1897 /* Is anything being shadowed? Invisible decls do not count. */
1898 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1899 if (b->decl && b->decl != new_decl && !b->invisible)
1901 tree old_decl = b->decl;
1903 if (old_decl == error_mark_node)
1905 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
1906 "non-variable", new_decl);
1909 else if (TREE_CODE (old_decl) == PARM_DECL)
1910 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
1912 else if (DECL_FILE_SCOPE_P (old_decl))
1913 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
1914 "declaration", new_decl);
1915 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1916 && DECL_BUILT_IN (old_decl))
1918 warning (OPT_Wshadow, "declaration of %q+D shadows "
1919 "a built-in function", new_decl);
1923 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
1926 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
1933 /* Subroutine of pushdecl.
1935 X is a TYPE_DECL for a typedef statement. Create a brand new
1936 ..._TYPE node (which will be just a variant of the existing
1937 ..._TYPE node with identical properties) and then install X
1938 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1940 The whole point here is to end up with a situation where each
1941 and every ..._TYPE node the compiler creates will be uniquely
1942 associated with AT MOST one node representing a typedef name.
1943 This way, even though the compiler substitutes corresponding
1944 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1945 early on, later parts of the compiler can always do the reverse
1946 translation and get back the corresponding typedef name. For
1949 typedef struct S MY_TYPE;
1952 Later parts of the compiler might only know that `object' was of
1953 type `struct S' if it were not for code just below. With this
1954 code however, later parts of the compiler see something like:
1956 struct S' == struct S
1957 typedef struct S' MY_TYPE;
1960 And they can then deduce (from the node for type struct S') that
1961 the original object declaration was:
1965 Being able to do this is important for proper support of protoize,
1966 and also for generating precise symbolic debugging information
1967 which takes full account of the programmer's (typedef) vocabulary.
1969 Obviously, we don't want to generate a duplicate ..._TYPE node if
1970 the TYPE_DECL node that we are now processing really represents a
1971 standard built-in type.
1973 Since all standard types are effectively declared at line zero
1974 in the source file, we can easily check to see if we are working
1975 on a standard type by checking the current value of lineno. */
1978 clone_underlying_type (tree x)
1980 if (DECL_IS_BUILTIN (x))
1982 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1983 TYPE_NAME (TREE_TYPE (x)) = x;
1985 else if (TREE_TYPE (x) != error_mark_node
1986 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1988 tree tt = TREE_TYPE (x);
1989 DECL_ORIGINAL_TYPE (x) = tt;
1990 tt = build_variant_type_copy (tt);
1992 TREE_USED (tt) = TREE_USED (x);
1997 /* Record a decl-node X as belonging to the current lexical scope.
1998 Check for errors (such as an incompatible declaration for the same
1999 name already seen in the same scope).
2001 Returns either X or an old decl for the same name.
2002 If an old decl is returned, it may have been smashed
2003 to agree with what X says. */
2008 tree name = DECL_NAME (x);
2009 struct c_scope *scope = current_scope;
2010 struct c_binding *b;
2011 bool nested = false;
2013 /* Functions need the lang_decl data. */
2014 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
2015 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
2017 /* Must set DECL_CONTEXT for everything not at file scope or
2018 DECL_FILE_SCOPE_P won't work. Local externs don't count
2019 unless they have initializers (which generate code). */
2020 if (current_function_decl
2021 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2022 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2023 DECL_CONTEXT (x) = current_function_decl;
2025 /* If this is of variably modified type, prevent jumping into its
2027 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2028 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2029 c_begin_vm_scope (scope->depth);
2031 /* Anonymous decls are just inserted in the scope. */
2034 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2038 /* First, see if there is another declaration with the same name in
2039 the current scope. If there is, duplicate_decls may do all the
2040 work for us. If duplicate_decls returns false, that indicates
2041 two incompatible decls in the same scope; we are to silently
2042 replace the old one (duplicate_decls has issued all appropriate
2043 diagnostics). In particular, we should not consider possible
2044 duplicates in the external scope, or shadowing. */
2045 b = I_SYMBOL_BINDING (name);
2046 if (b && B_IN_SCOPE (b, scope))
2048 struct c_binding *b_ext, *b_use;
2049 tree type = TREE_TYPE (x);
2050 tree visdecl = b->decl;
2051 tree vistype = TREE_TYPE (visdecl);
2052 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2053 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2054 b->inner_comp = false;
2057 /* If this is an external linkage declaration, we should check
2058 for compatibility with the type in the external scope before
2059 setting the type at this scope based on the visible
2060 information only. */
2061 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2063 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2064 b_ext = b_ext->shadowed;
2069 TREE_TYPE (b_use->decl) = b_use->type;
2072 if (duplicate_decls (x, b_use->decl))
2076 /* Save the updated type in the external scope and
2077 restore the proper type for this scope. */
2079 if (comptypes (vistype, type))
2080 thistype = composite_type (vistype, type);
2082 thistype = TREE_TYPE (b_use->decl);
2083 b_use->type = TREE_TYPE (b_use->decl);
2084 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2085 && DECL_BUILT_IN (b_use->decl))
2087 = build_type_attribute_variant (thistype,
2090 TREE_TYPE (b_use->decl) = thistype;
2095 goto skip_external_and_shadow_checks;
2098 /* All declarations with external linkage, and all external
2099 references, go in the external scope, no matter what scope is
2100 current. However, the binding in that scope is ignored for
2101 purposes of normal name lookup. A separate binding structure is
2102 created in the requested scope; this governs the normal
2103 visibility of the symbol.
2105 The binding in the externals scope is used exclusively for
2106 detecting duplicate declarations of the same object, no matter
2107 what scope they are in; this is what we do here. (C99 6.2.7p2:
2108 All declarations that refer to the same object or function shall
2109 have compatible type; otherwise, the behavior is undefined.) */
2110 if (DECL_EXTERNAL (x) || scope == file_scope)
2112 tree type = TREE_TYPE (x);
2115 bool type_saved = false;
2116 if (b && !B_IN_EXTERNAL_SCOPE (b)
2117 && (TREE_CODE (b->decl) == FUNCTION_DECL
2118 || TREE_CODE (b->decl) == VAR_DECL)
2119 && DECL_FILE_SCOPE_P (b->decl))
2122 vistype = TREE_TYPE (visdecl);
2124 if (scope != file_scope
2125 && !DECL_IN_SYSTEM_HEADER (x))
2126 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2128 while (b && !B_IN_EXTERNAL_SCOPE (b))
2130 /* If this decl might be modified, save its type. This is
2131 done here rather than when the decl is first bound
2132 because the type may change after first binding, through
2133 being completed or through attributes being added. If we
2134 encounter multiple such decls, only the first should have
2135 its type saved; the others will already have had their
2136 proper types saved and the types will not have changed as
2137 their scopes will not have been re-entered. */
2138 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2140 b->type = TREE_TYPE (b->decl);
2143 if (B_IN_FILE_SCOPE (b)
2144 && TREE_CODE (b->decl) == VAR_DECL
2145 && TREE_STATIC (b->decl)
2146 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2147 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2148 && TREE_CODE (type) == ARRAY_TYPE
2149 && TYPE_DOMAIN (type)
2150 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2151 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2153 /* Array type completed in inner scope, which should be
2154 diagnosed if the completion does not have size 1 and
2155 it does not get completed in the file scope. */
2156 b->inner_comp = true;
2161 /* If a matching external declaration has been found, set its
2162 type to the composite of all the types of that declaration.
2163 After the consistency checks, it will be reset to the
2164 composite of the visible types only. */
2165 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2167 TREE_TYPE (b->decl) = b->type;
2169 /* The point of the same_translation_unit_p check here is,
2170 we want to detect a duplicate decl for a construct like
2171 foo() { extern bar(); } ... static bar(); but not if
2172 they are in different translation units. In any case,
2173 the static does not go in the externals scope. */
2175 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2176 && duplicate_decls (x, b->decl))
2181 if (comptypes (vistype, type))
2182 thistype = composite_type (vistype, type);
2184 thistype = TREE_TYPE (b->decl);
2188 b->type = TREE_TYPE (b->decl);
2189 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2191 = build_type_attribute_variant (thistype,
2192 TYPE_ATTRIBUTES (b->type));
2193 TREE_TYPE (b->decl) = thistype;
2194 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2197 else if (TREE_PUBLIC (x))
2199 if (visdecl && !b && duplicate_decls (x, visdecl))
2201 /* An external declaration at block scope referring to a
2202 visible entity with internal linkage. The composite
2203 type will already be correct for this scope, so we
2204 just need to fall through to make the declaration in
2211 bind (name, x, external_scope, /*invisible=*/true,
2218 if (TREE_CODE (x) != PARM_DECL)
2219 warn_if_shadowing (x);
2221 skip_external_and_shadow_checks:
2222 if (TREE_CODE (x) == TYPE_DECL)
2223 clone_underlying_type (x);
2225 bind (name, x, scope, /*invisible=*/false, nested);
2227 /* If x's type is incomplete because it's based on a
2228 structure or union which has not yet been fully declared,
2229 attach it to that structure or union type, so we can go
2230 back and complete the variable declaration later, if the
2231 structure or union gets fully declared.
2233 If the input is erroneous, we can have error_mark in the type
2234 slot (e.g. "f(void a, ...)") - that doesn't count as an
2236 if (TREE_TYPE (x) != error_mark_node
2237 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2239 tree element = TREE_TYPE (x);
2241 while (TREE_CODE (element) == ARRAY_TYPE)
2242 element = TREE_TYPE (element);
2243 element = TYPE_MAIN_VARIANT (element);
2245 if ((TREE_CODE (element) == RECORD_TYPE
2246 || TREE_CODE (element) == UNION_TYPE)
2247 && (TREE_CODE (x) != TYPE_DECL
2248 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2249 && !COMPLETE_TYPE_P (element))
2250 C_TYPE_INCOMPLETE_VARS (element)
2251 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2256 /* Record X as belonging to file scope.
2257 This is used only internally by the Objective-C front end,
2258 and is limited to its needs. duplicate_decls is not called;
2259 if there is any preexisting decl for this identifier, it is an ICE. */
2262 pushdecl_top_level (tree x)
2265 bool nested = false;
2266 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2268 name = DECL_NAME (x);
2270 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2272 if (TREE_PUBLIC (x))
2274 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2278 bind (name, x, file_scope, /*invisible=*/false, nested);
2284 implicit_decl_warning (tree id, tree olddecl)
2286 void (*diag) (const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2);
2287 switch (mesg_implicit_function_declaration)
2290 case 1: diag = warning0; break;
2291 case 2: diag = error; break;
2292 default: gcc_unreachable ();
2295 diag (G_("implicit declaration of function %qE"), id);
2297 locate_old_decl (olddecl, diag);
2300 /* Generate an implicit declaration for identifier FUNCTIONID as a
2301 function of type int (). */
2304 implicitly_declare (tree functionid)
2306 struct c_binding *b;
2310 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2312 if (B_IN_SCOPE (b, external_scope))
2321 if (decl == error_mark_node)
2324 /* FIXME: Objective-C has weird not-really-builtin functions
2325 which are supposed to be visible automatically. They wind up
2326 in the external scope because they're pushed before the file
2327 scope gets created. Catch this here and rebind them into the
2329 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2331 bind (functionid, decl, file_scope,
2332 /*invisible=*/false, /*nested=*/true);
2337 tree newtype = default_function_type;
2339 TREE_TYPE (decl) = b->type;
2340 /* Implicit declaration of a function already declared
2341 (somehow) in a different scope, or as a built-in.
2342 If this is the first time this has happened, warn;
2343 then recycle the old declaration but with the new type. */
2344 if (!C_DECL_IMPLICIT (decl))
2346 implicit_decl_warning (functionid, decl);
2347 C_DECL_IMPLICIT (decl) = 1;
2349 if (DECL_BUILT_IN (decl))
2351 newtype = build_type_attribute_variant (newtype,
2353 (TREE_TYPE (decl)));
2354 if (!comptypes (newtype, TREE_TYPE (decl)))
2356 warning (0, "incompatible implicit declaration of built-in"
2357 " function %qD", decl);
2358 newtype = TREE_TYPE (decl);
2363 if (!comptypes (newtype, TREE_TYPE (decl)))
2365 error ("incompatible implicit declaration of function %qD",
2367 locate_old_decl (decl, error);
2370 b->type = TREE_TYPE (decl);
2371 TREE_TYPE (decl) = newtype;
2372 bind (functionid, decl, current_scope,
2373 /*invisible=*/false, /*nested=*/true);
2378 /* Not seen before. */
2379 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2380 DECL_EXTERNAL (decl) = 1;
2381 TREE_PUBLIC (decl) = 1;
2382 C_DECL_IMPLICIT (decl) = 1;
2383 implicit_decl_warning (functionid, 0);
2384 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2386 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2388 /* C89 says implicit declarations are in the innermost block.
2389 So we record the decl in the standard fashion. */
2390 decl = pushdecl (decl);
2392 /* No need to call objc_check_decl here - it's a function type. */
2393 rest_of_decl_compilation (decl, 0, 0);
2395 /* Write a record describing this implicit function declaration
2396 to the prototypes file (if requested). */
2397 gen_aux_info_record (decl, 0, 1, 0);
2399 /* Possibly apply some default attributes to this implicit declaration. */
2400 decl_attributes (&decl, NULL_TREE, 0);
2405 /* Issue an error message for a reference to an undeclared variable
2406 ID, including a reference to a builtin outside of function-call
2407 context. Establish a binding of the identifier to error_mark_node
2408 in an appropriate scope, which will suppress further errors for the
2409 same identifier. The error message should be given location LOC. */
2411 undeclared_variable (tree id, location_t loc)
2413 static bool already = false;
2414 struct c_scope *scope;
2416 if (current_function_decl == 0)
2418 error ("%H%qE undeclared here (not in a function)", &loc, id);
2419 scope = current_scope;
2423 error ("%H%qE undeclared (first use in this function)", &loc, id);
2427 error ("%H(Each undeclared identifier is reported only once", &loc);
2428 error ("%Hfor each function it appears in.)", &loc);
2432 /* If we are parsing old-style parameter decls, current_function_decl
2433 will be nonnull but current_function_scope will be null. */
2434 scope = current_function_scope ? current_function_scope : current_scope;
2436 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2439 /* Subroutine of lookup_label, declare_label, define_label: construct a
2440 LABEL_DECL with all the proper frills. */
2443 make_label (tree name, location_t location)
2445 tree label = build_decl (LABEL_DECL, name, void_type_node);
2447 DECL_CONTEXT (label) = current_function_decl;
2448 DECL_MODE (label) = VOIDmode;
2449 DECL_SOURCE_LOCATION (label) = location;
2454 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2455 Create one if none exists so far for the current function.
2456 This is called when a label is used in a goto expression or
2457 has its address taken. */
2460 lookup_label (tree name)
2464 if (current_function_decl == 0)
2466 error ("label %qE referenced outside of any function", name);
2470 /* Use a label already defined or ref'd with this name, but not if
2471 it is inherited from a containing function and wasn't declared
2473 label = I_LABEL_DECL (name);
2474 if (label && (DECL_CONTEXT (label) == current_function_decl
2475 || C_DECLARED_LABEL_FLAG (label)))
2477 /* If the label has only been declared, update its apparent
2478 location to point here, for better diagnostics if it
2479 turns out not to have been defined. */
2480 if (!TREE_USED (label))
2481 DECL_SOURCE_LOCATION (label) = input_location;
2485 /* No label binding for that identifier; make one. */
2486 label = make_label (name, input_location);
2488 /* Ordinary labels go in the current function scope. */
2489 bind (name, label, current_function_scope,
2490 /*invisible=*/false, /*nested=*/false);
2494 /* Make a label named NAME in the current function, shadowing silently
2495 any that may be inherited from containing functions or containing
2496 scopes. This is called for __label__ declarations. */
2499 declare_label (tree name)
2501 struct c_binding *b = I_LABEL_BINDING (name);
2504 /* Check to make sure that the label hasn't already been declared
2506 if (b && B_IN_CURRENT_SCOPE (b))
2508 error ("duplicate label declaration %qE", name);
2509 locate_old_decl (b->decl, error);
2511 /* Just use the previous declaration. */
2515 label = make_label (name, input_location);
2516 C_DECLARED_LABEL_FLAG (label) = 1;
2518 /* Declared labels go in the current scope. */
2519 bind (name, label, current_scope,
2520 /*invisible=*/false, /*nested=*/false);
2524 /* Define a label, specifying the location in the source file.
2525 Return the LABEL_DECL node for the label, if the definition is valid.
2526 Otherwise return 0. */
2529 define_label (location_t location, tree name)
2531 /* Find any preexisting label with this name. It is an error
2532 if that label has already been defined in this function, or
2533 if there is a containing function with a declared label with
2535 tree label = I_LABEL_DECL (name);
2536 struct c_label_list *nlist_se, *nlist_vm;
2539 && ((DECL_CONTEXT (label) == current_function_decl
2540 && DECL_INITIAL (label) != 0)
2541 || (DECL_CONTEXT (label) != current_function_decl
2542 && C_DECLARED_LABEL_FLAG (label))))
2544 error ("%Hduplicate label %qD", &location, label);
2545 locate_old_decl (label, error);
2548 else if (label && DECL_CONTEXT (label) == current_function_decl)
2550 /* The label has been used or declared already in this function,
2551 but not defined. Update its location to point to this
2553 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2554 error ("%Jjump into statement expression", label);
2555 if (C_DECL_UNDEFINABLE_VM (label))
2556 error ("%Jjump into scope of identifier with variably modified type",
2558 DECL_SOURCE_LOCATION (label) = location;
2562 /* No label binding for that identifier; make one. */
2563 label = make_label (name, location);
2565 /* Ordinary labels go in the current function scope. */
2566 bind (name, label, current_function_scope,
2567 /*invisible=*/false, /*nested=*/false);
2570 if (!in_system_header && lookup_name (name))
2571 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2572 "for labels, identifier %qE conflicts", &location, name);
2574 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2575 nlist_se->next = label_context_stack_se->labels_def;
2576 nlist_se->label = label;
2577 label_context_stack_se->labels_def = nlist_se;
2579 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2580 nlist_vm->next = label_context_stack_vm->labels_def;
2581 nlist_vm->label = label;
2582 label_context_stack_vm->labels_def = nlist_vm;
2584 /* Mark label as having been defined. */
2585 DECL_INITIAL (label) = error_mark_node;
2589 /* Given NAME, an IDENTIFIER_NODE,
2590 return the structure (or union or enum) definition for that name.
2591 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2592 CODE says which kind of type the caller wants;
2593 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2594 If the wrong kind of type is found, an error is reported. */
2597 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2599 struct c_binding *b = I_TAG_BINDING (name);
2605 /* We only care about whether it's in this level if
2606 thislevel_only was set or it might be a type clash. */
2607 if (thislevel_only || TREE_CODE (b->decl) != code)
2609 /* For our purposes, a tag in the external scope is the same as
2610 a tag in the file scope. (Primarily relevant to Objective-C
2611 and its builtin structure tags, which get pushed before the
2612 file scope is created.) */
2613 if (B_IN_CURRENT_SCOPE (b)
2614 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2618 if (thislevel_only && !thislevel)
2621 if (TREE_CODE (b->decl) != code)
2623 /* Definition isn't the kind we were looking for. */
2624 pending_invalid_xref = name;
2625 pending_invalid_xref_location = input_location;
2627 /* If in the same binding level as a declaration as a tag
2628 of a different type, this must not be allowed to
2629 shadow that tag, so give the error immediately.
2630 (For example, "struct foo; union foo;" is invalid.) */
2632 pending_xref_error ();
2637 /* Print an error message now
2638 for a recent invalid struct, union or enum cross reference.
2639 We don't print them immediately because they are not invalid
2640 when used in the `struct foo;' construct for shadowing. */
2643 pending_xref_error (void)
2645 if (pending_invalid_xref != 0)
2646 error ("%H%qE defined as wrong kind of tag",
2647 &pending_invalid_xref_location, pending_invalid_xref);
2648 pending_invalid_xref = 0;
2652 /* Look up NAME in the current scope and its superiors
2653 in the namespace of variables, functions and typedefs.
2654 Return a ..._DECL node of some kind representing its definition,
2655 or return 0 if it is undefined. */
2658 lookup_name (tree name)
2660 struct c_binding *b = I_SYMBOL_BINDING (name);
2661 if (b && !b->invisible)
2666 /* Similar to `lookup_name' but look only at the indicated scope. */
2669 lookup_name_in_scope (tree name, struct c_scope *scope)
2671 struct c_binding *b;
2673 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2674 if (B_IN_SCOPE (b, scope))
2679 /* Create the predefined scalar types of C,
2680 and some nodes representing standard constants (0, 1, (void *) 0).
2681 Initialize the global scope.
2682 Make definitions for built-in primitive functions. */
2685 c_init_decl_processing (void)
2687 location_t save_loc = input_location;
2689 /* Initialize reserved words for parser. */
2692 current_function_decl = 0;
2694 gcc_obstack_init (&parser_obstack);
2696 /* Make the externals scope. */
2698 external_scope = current_scope;
2700 /* Declarations from c_common_nodes_and_builtins must not be associated
2701 with this input file, lest we get differences between using and not
2702 using preprocessed headers. */
2703 #ifdef USE_MAPPED_LOCATION
2704 input_location = BUILTINS_LOCATION;
2706 input_location.file = "<built-in>";
2707 input_location.line = 0;
2710 build_common_tree_nodes (flag_signed_char, false);
2712 c_common_nodes_and_builtins ();
2714 /* In C, comparisons and TRUTH_* expressions have type int. */
2715 truthvalue_type_node = integer_type_node;
2716 truthvalue_true_node = integer_one_node;
2717 truthvalue_false_node = integer_zero_node;
2719 /* Even in C99, which has a real boolean type. */
2720 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2721 boolean_type_node));
2723 input_location = save_loc;
2725 pedantic_lvalues = true;
2727 make_fname_decl = c_make_fname_decl;
2728 start_fname_decls ();
2731 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2732 decl, NAME is the initialization string and TYPE_DEP indicates whether
2733 NAME depended on the type of the function. As we don't yet implement
2734 delayed emission of static data, we mark the decl as emitted
2735 so it is not placed in the output. Anything using it must therefore pull
2736 out the STRING_CST initializer directly. FIXME. */
2739 c_make_fname_decl (tree id, int type_dep)
2741 const char *name = fname_as_string (type_dep);
2742 tree decl, type, init;
2743 size_t length = strlen (name);
2745 type = build_array_type (char_type_node,
2746 build_index_type (size_int (length)));
2747 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2749 decl = build_decl (VAR_DECL, id, type);
2751 TREE_STATIC (decl) = 1;
2752 TREE_READONLY (decl) = 1;
2753 DECL_ARTIFICIAL (decl) = 1;
2755 init = build_string (length + 1, name);
2756 free ((char *) name);
2757 TREE_TYPE (init) = type;
2758 DECL_INITIAL (decl) = init;
2760 TREE_USED (decl) = 1;
2762 if (current_function_decl)
2764 DECL_CONTEXT (decl) = current_function_decl;
2765 bind (id, decl, current_function_scope,
2766 /*invisible=*/false, /*nested=*/false);
2769 finish_decl (decl, init, NULL_TREE);
2774 /* Return a definition for a builtin function named NAME and whose data type
2775 is TYPE. TYPE should be a function type with argument types.
2776 FUNCTION_CODE tells later passes how to compile calls to this function.
2777 See tree.h for its possible values.
2779 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2780 the name to be called if we can't opencode the function. If
2781 ATTRS is nonzero, use that for the function's attribute list. */
2784 builtin_function (const char *name, tree type, int function_code,
2785 enum built_in_class cl, const char *library_name,
2788 tree id = get_identifier (name);
2789 tree decl = build_decl (FUNCTION_DECL, id, type);
2790 TREE_PUBLIC (decl) = 1;
2791 DECL_EXTERNAL (decl) = 1;
2792 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2793 DECL_BUILT_IN_CLASS (decl) = cl;
2794 DECL_FUNCTION_CODE (decl) = function_code;
2795 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2797 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2799 /* Should never be called on a symbol with a preexisting meaning. */
2800 gcc_assert (!I_SYMBOL_BINDING (id));
2802 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2804 /* Builtins in the implementation namespace are made visible without
2805 needing to be explicitly declared. See push_file_scope. */
2806 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2808 TREE_CHAIN (decl) = visible_builtins;
2809 visible_builtins = decl;
2812 /* Possibly apply some default attributes to this built-in function. */
2814 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2816 decl_attributes (&decl, NULL_TREE, 0);
2821 /* Called when a declaration is seen that contains no names to declare.
2822 If its type is a reference to a structure, union or enum inherited
2823 from a containing scope, shadow that tag name for the current scope
2824 with a forward reference.
2825 If its type defines a new named structure or union
2826 or defines an enum, it is valid but we need not do anything here.
2827 Otherwise, it is an error. */
2830 shadow_tag (const struct c_declspecs *declspecs)
2832 shadow_tag_warned (declspecs, 0);
2835 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2838 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2840 bool found_tag = false;
2842 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2844 tree value = declspecs->type;
2845 enum tree_code code = TREE_CODE (value);
2847 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2848 /* Used to test also that TYPE_SIZE (value) != 0.
2849 That caused warning for `struct foo;' at top level in the file. */
2851 tree name = TYPE_NAME (value);
2858 if (warned != 1 && code != ENUMERAL_TYPE)
2859 /* Empty unnamed enum OK */
2861 pedwarn ("unnamed struct/union that defines no instances");
2865 else if (!declspecs->tag_defined_p
2866 && declspecs->storage_class != csc_none)
2869 pedwarn ("empty declaration with storage class specifier "
2870 "does not redeclare tag");
2872 pending_xref_error ();
2874 else if (!declspecs->tag_defined_p
2875 && (declspecs->const_p
2876 || declspecs->volatile_p
2877 || declspecs->restrict_p))
2880 pedwarn ("empty declaration with type qualifier "
2881 "does not redeclare tag");
2883 pending_xref_error ();
2887 pending_invalid_xref = 0;
2888 t = lookup_tag (code, name, 1);
2892 t = make_node (code);
2899 if (warned != 1 && !in_system_header)
2901 pedwarn ("useless type name in empty declaration");
2906 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2908 pedwarn ("useless type name in empty declaration");
2912 pending_invalid_xref = 0;
2914 if (declspecs->inline_p)
2916 error ("%<inline%> in empty declaration");
2920 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2922 error ("%<auto%> in file-scope empty declaration");
2926 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2928 error ("%<register%> in file-scope empty declaration");
2932 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2934 warning (0, "useless storage class specifier in empty declaration");
2938 if (!warned && !in_system_header && declspecs->thread_p)
2940 warning (0, "useless %<__thread%> in empty declaration");
2944 if (!warned && !in_system_header && (declspecs->const_p
2945 || declspecs->volatile_p
2946 || declspecs->restrict_p))
2948 warning (0, "useless type qualifier in empty declaration");
2955 pedwarn ("empty declaration");
2960 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2961 bits. SPECS represents declaration specifiers that the grammar
2962 only permits to contain type qualifiers and attributes. */
2965 quals_from_declspecs (const struct c_declspecs *specs)
2967 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
2968 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
2969 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
2970 gcc_assert (!specs->type
2971 && !specs->decl_attr
2972 && specs->typespec_word == cts_none
2973 && specs->storage_class == csc_none
2974 && !specs->typedef_p
2975 && !specs->explicit_signed_p
2976 && !specs->deprecated_p
2978 && !specs->long_long_p
2981 && !specs->unsigned_p
2982 && !specs->complex_p
2984 && !specs->thread_p);
2988 /* Construct an array declarator. EXPR is the expression inside [], or
2989 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2990 to the pointer to which a parameter array is converted). STATIC_P is
2991 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
2992 is true if the array is [*], a VLA of unspecified length which is
2993 nevertheless a complete type (not currently implemented by GCC),
2994 false otherwise. The field for the contained declarator is left to be
2995 filled in by set_array_declarator_inner. */
2997 struct c_declarator *
2998 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3001 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3002 struct c_declarator);
3003 declarator->kind = cdk_array;
3004 declarator->declarator = 0;
3005 declarator->u.array.dimen = expr;
3008 declarator->u.array.attrs = quals->attrs;
3009 declarator->u.array.quals = quals_from_declspecs (quals);
3013 declarator->u.array.attrs = NULL_TREE;
3014 declarator->u.array.quals = 0;
3016 declarator->u.array.static_p = static_p;
3017 declarator->u.array.vla_unspec_p = vla_unspec_p;
3018 if (pedantic && !flag_isoc99)
3020 if (static_p || quals != NULL)
3021 pedwarn ("ISO C90 does not support %<static%> or type "
3022 "qualifiers in parameter array declarators");
3024 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3027 warning (0, "GCC does not yet properly implement %<[*]%> array declarators");
3031 /* Set the contained declarator of an array declarator. DECL is the
3032 declarator, as constructed by build_array_declarator; INNER is what
3033 appears on the left of the []. ABSTRACT_P is true if it is an
3034 abstract declarator, false otherwise; this is used to reject static
3035 and type qualifiers in abstract declarators, where they are not in
3036 the C99 grammar (subject to possible change in DR#289). */
3038 struct c_declarator *
3039 set_array_declarator_inner (struct c_declarator *decl,
3040 struct c_declarator *inner, bool abstract_p)
3042 decl->declarator = inner;
3043 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3044 || decl->u.array.attrs != NULL_TREE
3045 || decl->u.array.static_p))
3046 error ("static or type qualifiers in abstract declarator");
3050 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3053 groktypename (struct c_type_name *type_name)
3056 tree attrs = type_name->specs->attrs;
3058 type_name->specs->attrs = NULL_TREE;
3060 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3063 /* Apply attributes. */
3064 decl_attributes (&type, attrs, 0);
3069 /* Decode a declarator in an ordinary declaration or data definition.
3070 This is called as soon as the type information and variable name
3071 have been parsed, before parsing the initializer if any.
3072 Here we create the ..._DECL node, fill in its type,
3073 and put it on the list of decls for the current context.
3074 The ..._DECL node is returned as the value.
3076 Exception: for arrays where the length is not specified,
3077 the type is left null, to be filled in by `finish_decl'.
3079 Function definitions do not come here; they go to start_function
3080 instead. However, external and forward declarations of functions
3081 do go through here. Structure field declarations are done by
3082 grokfield and not through here. */
3085 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3086 bool initialized, tree attributes)
3091 /* An object declared as __attribute__((deprecated)) suppresses
3092 warnings of uses of other deprecated items. */
3093 if (lookup_attribute ("deprecated", attributes))
3094 deprecated_state = DEPRECATED_SUPPRESS;
3096 decl = grokdeclarator (declarator, declspecs,
3097 NORMAL, initialized, NULL);
3101 deprecated_state = DEPRECATED_NORMAL;
3103 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3104 && MAIN_NAME_P (DECL_NAME (decl)))
3105 warning (OPT_Wmain, "%q+D is usually a function", decl);
3108 /* Is it valid for this decl to have an initializer at all?
3109 If not, set INITIALIZED to zero, which will indirectly
3110 tell 'finish_decl' to ignore the initializer once it is parsed. */
3111 switch (TREE_CODE (decl))
3114 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3119 error ("function %qD is initialized like a variable", decl);
3124 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3125 error ("parameter %qD is initialized", decl);
3130 /* Don't allow initializations for incomplete types except for
3131 arrays which might be completed by the initialization. */
3133 /* This can happen if the array size is an undefined macro.
3134 We already gave a warning, so we don't need another one. */
3135 if (TREE_TYPE (decl) == error_mark_node)
3137 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3139 /* A complete type is ok if size is fixed. */
3141 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3142 || C_DECL_VARIABLE_SIZE (decl))
3144 error ("variable-sized object may not be initialized");
3148 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3150 error ("variable %qD has initializer but incomplete type", decl);
3153 else if (C_DECL_VARIABLE_SIZE (decl))
3155 /* Although C99 is unclear about whether incomplete arrays
3156 of VLAs themselves count as VLAs, it does not make
3157 sense to permit them to be initialized given that
3158 ordinary VLAs may not be initialized. */
3159 error ("variable-sized object may not be initialized");
3166 if (current_scope == file_scope)
3167 TREE_STATIC (decl) = 1;
3169 /* Tell 'pushdecl' this is an initialized decl
3170 even though we don't yet have the initializer expression.
3171 Also tell 'finish_decl' it may store the real initializer. */
3172 DECL_INITIAL (decl) = error_mark_node;
3175 /* If this is a function declaration, write a record describing it to the
3176 prototypes file (if requested). */
3178 if (TREE_CODE (decl) == FUNCTION_DECL)
3179 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3181 /* ANSI specifies that a tentative definition which is not merged with
3182 a non-tentative definition behaves exactly like a definition with an
3183 initializer equal to zero. (Section 3.7.2)
3185 -fno-common gives strict ANSI behavior, though this tends to break
3186 a large body of code that grew up without this rule.
3188 Thread-local variables are never common, since there's no entrenched
3189 body of code to break, and it allows more efficient variable references
3190 in the presence of dynamic linking. */
3192 if (TREE_CODE (decl) == VAR_DECL
3194 && TREE_PUBLIC (decl)
3195 && !DECL_THREAD_LOCAL_P (decl)
3197 DECL_COMMON (decl) = 1;
3199 /* Set attributes here so if duplicate decl, will have proper attributes. */
3200 decl_attributes (&decl, attributes, 0);
3202 if (TREE_CODE (decl) == FUNCTION_DECL
3203 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3205 struct c_declarator *ce = declarator;
3207 if (ce->kind == cdk_pointer)
3208 ce = declarator->declarator;
3209 if (ce->kind == cdk_function)
3211 tree args = ce->u.arg_info->parms;
3212 for (; args; args = TREE_CHAIN (args))
3214 tree type = TREE_TYPE (args);
3215 if (type && INTEGRAL_TYPE_P (type)
3216 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3217 DECL_ARG_TYPE (args) = integer_type_node;
3222 if (TREE_CODE (decl) == FUNCTION_DECL
3223 && DECL_DECLARED_INLINE_P (decl)
3224 && DECL_UNINLINABLE (decl)
3225 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3226 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3229 /* Add this decl to the current scope.
3230 TEM may equal DECL or it may be a previous decl of the same name. */
3231 tem = pushdecl (decl);
3233 if (initialized && DECL_EXTERNAL (tem))
3235 DECL_EXTERNAL (tem) = 0;
3236 TREE_STATIC (tem) = 1;
3242 /* Finish processing of a declaration;
3243 install its initial value.
3244 If the length of an array type is not known before,
3245 it must be determined now, from the initial value, or it is an error. */
3248 finish_decl (tree decl, tree init, tree asmspec_tree)
3250 tree type = TREE_TYPE (decl);
3251 int was_incomplete = (DECL_SIZE (decl) == 0);
3252 const char *asmspec = 0;
3254 /* If a name was specified, get the string. */
3255 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3256 && DECL_FILE_SCOPE_P (decl))
3257 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3259 asmspec = TREE_STRING_POINTER (asmspec_tree);
3261 /* If `start_decl' didn't like having an initialization, ignore it now. */
3262 if (init != 0 && DECL_INITIAL (decl) == 0)
3265 /* Don't crash if parm is initialized. */
3266 if (TREE_CODE (decl) == PARM_DECL)
3270 store_init_value (decl, init);
3272 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3273 || TREE_CODE (decl) == FUNCTION_DECL
3274 || TREE_CODE (decl) == FIELD_DECL))
3275 objc_check_decl (decl);
3277 /* Deduce size of array from initialization, if not already known. */
3278 if (TREE_CODE (type) == ARRAY_TYPE
3279 && TYPE_DOMAIN (type) == 0
3280 && TREE_CODE (decl) != TYPE_DECL)
3283 = (TREE_STATIC (decl)
3284 /* Even if pedantic, an external linkage array
3285 may have incomplete type at first. */
3286 ? pedantic && !TREE_PUBLIC (decl)
3287 : !DECL_EXTERNAL (decl));
3289 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3292 /* Get the completed type made by complete_array_type. */
3293 type = TREE_TYPE (decl);
3298 error ("initializer fails to determine size of %q+D", decl);
3303 error ("array size missing in %q+D", decl);
3304 /* If a `static' var's size isn't known,
3305 make it extern as well as static, so it does not get
3307 If it is not `static', then do not mark extern;
3308 finish_incomplete_decl will give it a default size
3309 and it will get allocated. */
3310 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3311 DECL_EXTERNAL (decl) = 1;
3315 error ("zero or negative size array %q+D", decl);
3319 /* For global variables, update the copy of the type that
3320 exists in the binding. */
3321 if (TREE_PUBLIC (decl))
3323 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3324 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3325 b_ext = b_ext->shadowed;
3329 b_ext->type = composite_type (b_ext->type, type);
3340 if (DECL_INITIAL (decl))
3341 TREE_TYPE (DECL_INITIAL (decl)) = type;
3343 layout_decl (decl, 0);
3346 if (TREE_CODE (decl) == VAR_DECL)
3348 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3349 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3350 layout_decl (decl, 0);
3352 if (DECL_SIZE (decl) == 0
3353 /* Don't give an error if we already gave one earlier. */
3354 && TREE_TYPE (decl) != error_mark_node
3355 && (TREE_STATIC (decl)
3356 /* A static variable with an incomplete type
3357 is an error if it is initialized.
3358 Also if it is not file scope.
3359 Otherwise, let it through, but if it is not `extern'
3360 then it may cause an error message later. */
3361 ? (DECL_INITIAL (decl) != 0
3362 || !DECL_FILE_SCOPE_P (decl))
3363 /* An automatic variable with an incomplete type
3365 : !DECL_EXTERNAL (decl)))
3367 error ("storage size of %q+D isn%'t known", decl);
3368 TREE_TYPE (decl) = error_mark_node;
3371 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3372 && DECL_SIZE (decl) != 0)
3374 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3375 constant_expression_warning (DECL_SIZE (decl));
3377 error ("storage size of %q+D isn%'t constant", decl);
3380 if (TREE_USED (type))
3381 TREE_USED (decl) = 1;
3384 /* If this is a function and an assembler name is specified, reset DECL_RTL
3385 so we can give it its new name. Also, update built_in_decls if it
3386 was a normal built-in. */
3387 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3389 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3390 set_builtin_user_assembler_name (decl, asmspec);
3391 set_user_assembler_name (decl, asmspec);
3394 /* If #pragma weak was used, mark the decl weak now. */
3395 maybe_apply_pragma_weak (decl);
3397 /* If this is a variable definition, determine its ELF visibility. */
3398 if (TREE_CODE (decl) == VAR_DECL
3399 && TREE_STATIC (decl)
3400 && !DECL_EXTERNAL (decl))
3401 c_determine_visibility (decl);
3403 /* Output the assembler code and/or RTL code for variables and functions,
3404 unless the type is an undefined structure or union.
3405 If not, it will get done when the type is completed. */
3407 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3409 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3410 if (c_dialect_objc ())
3411 objc_check_decl (decl);
3415 /* If this is not a static variable, issue a warning.
3416 It doesn't make any sense to give an ASMSPEC for an
3417 ordinary, non-register local variable. Historically,
3418 GCC has accepted -- but ignored -- the ASMSPEC in
3420 if (!DECL_FILE_SCOPE_P (decl)
3421 && TREE_CODE (decl) == VAR_DECL
3422 && !C_DECL_REGISTER (decl)
3423 && !TREE_STATIC (decl))
3424 warning (0, "ignoring asm-specifier for non-static local "
3425 "variable %q+D", decl);
3426 else if (C_DECL_REGISTER (decl))
3427 change_decl_assembler_name (decl, get_identifier (asmspec));
3429 set_user_assembler_name (decl, asmspec);
3432 if (DECL_FILE_SCOPE_P (decl))
3434 if (DECL_INITIAL (decl) == NULL_TREE
3435 || DECL_INITIAL (decl) == error_mark_node)
3436 /* Don't output anything
3437 when a tentative file-scope definition is seen.
3438 But at end of compilation, do output code for them. */
3439 DECL_DEFER_OUTPUT (decl) = 1;
3440 rest_of_decl_compilation (decl, true, 0);
3444 /* In conjunction with an ASMSPEC, the `register'
3445 keyword indicates that we should place the variable
3446 in a particular register. */
3447 if (asmspec && C_DECL_REGISTER (decl))
3449 DECL_HARD_REGISTER (decl) = 1;
3450 /* This cannot be done for a structure with volatile
3451 fields, on which DECL_REGISTER will have been
3453 if (!DECL_REGISTER (decl))
3454 error ("cannot put object with volatile field into register");
3457 if (TREE_CODE (decl) != FUNCTION_DECL)
3459 /* If we're building a variable sized type, and we might be
3460 reachable other than via the top of the current binding
3461 level, then create a new BIND_EXPR so that we deallocate
3462 the object at the right time. */
3463 /* Note that DECL_SIZE can be null due to errors. */
3464 if (DECL_SIZE (decl)
3465 && !TREE_CONSTANT (DECL_SIZE (decl))
3466 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3469 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3470 TREE_SIDE_EFFECTS (bind) = 1;
3472 BIND_EXPR_BODY (bind) = push_stmt_list ();
3474 add_stmt (build_stmt (DECL_EXPR, decl));
3479 if (!DECL_FILE_SCOPE_P (decl))
3481 /* Recompute the RTL of a local array now
3482 if it used to be an incomplete type. */
3484 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3486 /* If we used it already as memory, it must stay in memory. */
3487 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3488 /* If it's still incomplete now, no init will save it. */