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 /* Set this to 1 if you want the standard ISO C99 semantics of 'inline'
66 when you specify -std=c99 or -std=gnu99, and to 0 if you want
67 behavior compatible with the nonstandard semantics implemented by
68 GCC 2.95 through 4.2. */
69 #define WANT_C99_INLINE_SEMANTICS 1
71 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
73 { NORMAL, /* Ordinary declaration */
74 FUNCDEF, /* Function definition */
75 PARM, /* Declaration of parm before function body */
76 FIELD, /* Declaration inside struct or union */
77 TYPENAME}; /* Typename (inside cast or sizeof) */
80 /* Nonzero if we have seen an invalid cross reference
81 to a struct, union, or enum, but not yet printed the message. */
82 tree pending_invalid_xref;
84 /* File and line to appear in the eventual error message. */
85 location_t pending_invalid_xref_location;
87 /* True means we've initialized exception handling. */
88 bool c_eh_initialized_p;
90 /* While defining an enum type, this is 1 plus the last enumerator
91 constant value. Note that will do not have to save this or `enum_overflow'
92 around nested function definition since such a definition could only
93 occur in an enum value expression and we don't use these variables in
96 static tree enum_next_value;
98 /* Nonzero means that there was overflow computing enum_next_value. */
100 static int enum_overflow;
102 /* The file and line that the prototype came from if this is an
103 old-style definition; used for diagnostics in
104 store_parm_decls_oldstyle. */
106 static location_t current_function_prototype_locus;
108 /* Whether this prototype was built-in. */
110 static bool current_function_prototype_built_in;
112 /* The argument type information of this prototype. */
114 static tree current_function_prototype_arg_types;
116 /* The argument information structure for the function currently being
119 static struct c_arg_info *current_function_arg_info;
121 /* The obstack on which parser and related data structures, which are
122 not live beyond their top-level declaration or definition, are
124 struct obstack parser_obstack;
126 /* The current statement tree. */
128 static GTY(()) struct stmt_tree_s c_stmt_tree;
130 /* State saving variables. */
134 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
135 included in this invocation. Note that the current translation
136 unit is not included in this list. */
138 static GTY(()) tree all_translation_units;
140 /* A list of decls to be made automatically visible in each file scope. */
141 static GTY(()) tree visible_builtins;
143 /* Set to 0 at beginning of a function definition, set to 1 if
144 a return statement that specifies a return value is seen. */
146 int current_function_returns_value;
148 /* Set to 0 at beginning of a function definition, set to 1 if
149 a return statement with no argument is seen. */
151 int current_function_returns_null;
153 /* Set to 0 at beginning of a function definition, set to 1 if
154 a call to a noreturn function is seen. */
156 int current_function_returns_abnormally;
158 /* Set to nonzero by `grokdeclarator' for a function
159 whose return type is defaulted, if warnings for this are desired. */
161 static int warn_about_return_type;
163 /* Nonzero when the current toplevel function contains a declaration
164 of a nested function which is never defined. */
166 static bool undef_nested_function;
168 /* True means global_bindings_p should return false even if the scope stack
169 says we are in file scope. */
170 bool c_override_global_bindings_to_false;
173 /* Each c_binding structure describes one binding of an identifier to
174 a decl. All the decls in a scope - irrespective of namespace - are
175 chained together by the ->prev field, which (as the name implies)
176 runs in reverse order. All the decls in a given namespace bound to
177 a given identifier are chained by the ->shadowed field, which runs
178 from inner to outer scopes.
180 The ->decl field usually points to a DECL node, but there are two
181 exceptions. In the namespace of type tags, the bound entity is a
182 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
183 identifier is encountered, it is bound to error_mark_node to
184 suppress further errors about that identifier in the current
187 The ->type field stores the type of the declaration in this scope;
188 if NULL, the type is the type of the ->decl field. This is only of
189 relevance for objects with external or internal linkage which may
190 be redeclared in inner scopes, forming composite types that only
191 persist for the duration of those scopes. In the external scope,
192 this stores the composite of all the types declared for this
193 object, visible or not. The ->inner_comp field (used only at file
194 scope) stores whether an incomplete array type at file scope was
195 completed at an inner scope to an array size other than 1.
197 The depth field is copied from the scope structure that holds this
198 decl. It is used to preserve the proper ordering of the ->shadowed
199 field (see bind()) and also for a handful of special-case checks.
200 Finally, the invisible bit is true for a decl which should be
201 ignored for purposes of normal name lookup, and the nested bit is
202 true for a decl that's been bound a second time in an inner scope;
203 in all such cases, the binding in the outer scope will have its
204 invisible bit true. */
206 struct c_binding GTY((chain_next ("%h.prev")))
208 tree decl; /* the decl bound */
209 tree type; /* the type in this scope */
210 tree id; /* the identifier it's bound to */
211 struct c_binding *prev; /* the previous decl in this scope */
212 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
213 unsigned int depth : 28; /* depth of this scope */
214 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
215 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
216 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
219 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
220 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
221 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
222 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
224 #define I_SYMBOL_BINDING(node) \
225 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
226 #define I_SYMBOL_DECL(node) \
227 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
229 #define I_TAG_BINDING(node) \
230 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
231 #define I_TAG_DECL(node) \
232 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
234 #define I_LABEL_BINDING(node) \
235 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
236 #define I_LABEL_DECL(node) \
237 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
239 /* Each C symbol points to three linked lists of c_binding structures.
240 These describe the values of the identifier in the three different
241 namespaces defined by the language. */
243 struct lang_identifier GTY(())
245 struct c_common_identifier common_id;
246 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
247 struct c_binding *tag_binding; /* struct/union/enum tags */
248 struct c_binding *label_binding; /* labels */
251 /* Validate c-lang.c's assumptions. */
252 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
253 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
255 /* The resulting tree type. */
258 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
259 chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *) TYPE_NEXT_VARIANT (&%h.generic) : (GIMPLE_TUPLE_P (&%h.generic) ? (union lang_tree_node *) 0 : (union lang_tree_node *) TREE_CHAIN (&%h.generic))")))
261 union tree_node GTY ((tag ("0"),
262 desc ("tree_node_structure (&%h)")))
264 struct lang_identifier GTY ((tag ("1"))) identifier;
267 /* Each c_scope structure describes the complete contents of one
268 scope. Four scopes are distinguished specially: the innermost or
269 current scope, the innermost function scope, the file scope (always
270 the second to outermost) and the outermost or external scope.
272 Most declarations are recorded in the current scope.
274 All normal label declarations are recorded in the innermost
275 function scope, as are bindings of undeclared identifiers to
276 error_mark_node. (GCC permits nested functions as an extension,
277 hence the 'innermost' qualifier.) Explicitly declared labels
278 (using the __label__ extension) appear in the current scope.
280 Being in the file scope (current_scope == file_scope) causes
281 special behavior in several places below. Also, under some
282 conditions the Objective-C front end records declarations in the
283 file scope even though that isn't the current scope.
285 All declarations with external linkage are recorded in the external
286 scope, even if they aren't visible there; this models the fact that
287 such declarations are visible to the entire program, and (with a
288 bit of cleverness, see pushdecl) allows diagnosis of some violations
289 of C99 6.2.2p7 and 6.2.7p2:
291 If, within the same translation unit, the same identifier appears
292 with both internal and external linkage, the behavior is
295 All declarations that refer to the same object or function shall
296 have compatible type; otherwise, the behavior is undefined.
298 Initially only the built-in declarations, which describe compiler
299 intrinsic functions plus a subset of the standard library, are in
302 The order of the blocks list matters, and it is frequently appended
303 to. To avoid having to walk all the way to the end of the list on
304 each insertion, or reverse the list later, we maintain a pointer to
305 the last list entry. (FIXME: It should be feasible to use a reversed
308 The bindings list is strictly in reverse order of declarations;
309 pop_scope relies on this. */
312 struct c_scope GTY((chain_next ("%h.outer")))
314 /* The scope containing this one. */
315 struct c_scope *outer;
317 /* The next outermost function scope. */
318 struct c_scope *outer_function;
320 /* All bindings in this scope. */
321 struct c_binding *bindings;
323 /* For each scope (except the global one), a chain of BLOCK nodes
324 for all the scopes that were entered and exited one level down. */
328 /* The depth of this scope. Used to keep the ->shadowed chain of
329 bindings sorted innermost to outermost. */
330 unsigned int depth : 28;
332 /* True if we are currently filling this scope with parameter
334 BOOL_BITFIELD parm_flag : 1;
336 /* True if we saw [*] in this scope. Used to give an error messages
337 if these appears in a function definition. */
338 BOOL_BITFIELD had_vla_unspec : 1;
340 /* True if we already complained about forward parameter decls
341 in this scope. This prevents double warnings on
342 foo (int a; int b; ...) */
343 BOOL_BITFIELD warned_forward_parm_decls : 1;
345 /* True if this is the outermost block scope of a function body.
346 This scope contains the parameters, the local variables declared
347 in the outermost block, and all the labels (except those in
348 nested functions, or declared at block scope with __label__). */
349 BOOL_BITFIELD function_body : 1;
351 /* True means make a BLOCK for this scope no matter what. */
352 BOOL_BITFIELD keep : 1;
355 /* The scope currently in effect. */
357 static GTY(()) struct c_scope *current_scope;
359 /* The innermost function scope. Ordinary (not explicitly declared)
360 labels, bindings to error_mark_node, and the lazily-created
361 bindings of __func__ and its friends get this scope. */
363 static GTY(()) struct c_scope *current_function_scope;
365 /* The C file scope. This is reset for each input translation unit. */
367 static GTY(()) struct c_scope *file_scope;
369 /* The outermost scope. This is used for all declarations with
370 external linkage, and only these, hence the name. */
372 static GTY(()) struct c_scope *external_scope;
374 /* A chain of c_scope structures awaiting reuse. */
376 static GTY((deletable)) struct c_scope *scope_freelist;
378 /* A chain of c_binding structures awaiting reuse. */
380 static GTY((deletable)) struct c_binding *binding_freelist;
382 /* Append VAR to LIST in scope SCOPE. */
383 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
384 struct c_scope *s_ = (scope); \
386 if (s_->list##_last) \
387 TREE_CHAIN (s_->list##_last) = d_; \
390 s_->list##_last = d_; \
393 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
394 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
395 struct c_scope *t_ = (tscope); \
396 struct c_scope *f_ = (fscope); \
398 TREE_CHAIN (t_->to##_last) = f_->from; \
401 t_->to##_last = f_->from##_last; \
404 /* True means unconditionally make a BLOCK for the next scope pushed. */
406 static bool keep_next_level_flag;
408 /* True means the next call to push_scope will be the outermost scope
409 of a function body, so do not push a new scope, merely cease
410 expecting parameter decls. */
412 static bool next_is_function_body;
414 /* Functions called automatically at the beginning and end of execution. */
416 static GTY(()) tree static_ctors;
417 static GTY(()) tree static_dtors;
419 /* Forward declarations. */
420 static tree lookup_name_in_scope (tree, struct c_scope *);
421 static tree c_make_fname_decl (tree, int);
422 static tree grokdeclarator (const struct c_declarator *,
423 struct c_declspecs *,
424 enum decl_context, bool, tree *);
425 static tree grokparms (struct c_arg_info *, bool);
426 static void layout_array_type (tree);
428 /* T is a statement. Add it to the statement-tree. This is the
429 C/ObjC version--C++ has a slightly different version of this
435 enum tree_code code = TREE_CODE (t);
437 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
439 if (!EXPR_HAS_LOCATION (t))
440 SET_EXPR_LOCATION (t, input_location);
443 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
444 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
446 /* Add T to the statement-tree. Non-side-effect statements need to be
447 recorded during statement expressions. */
448 append_to_statement_list_force (t, &cur_stmt_list);
453 /* States indicating how grokdeclarator() should handle declspecs marked
454 with __attribute__((deprecated)). An object declared as
455 __attribute__((deprecated)) suppresses warnings of uses of other
458 enum deprecated_states {
463 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
466 c_print_identifier (FILE *file, tree node, int indent)
468 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
469 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
470 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
471 if (C_IS_RESERVED_WORD (node))
473 tree rid = ridpointers[C_RID_CODE (node)];
474 indent_to (file, indent + 4);
475 fprintf (file, "rid %p \"%s\"",
476 (void *) rid, IDENTIFIER_POINTER (rid));
480 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
481 which may be any of several kinds of DECL or TYPE or error_mark_node,
482 in the scope SCOPE. */
484 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
486 struct c_binding *b, **here;
488 if (binding_freelist)
490 b = binding_freelist;
491 binding_freelist = b->prev;
494 b = GGC_NEW (struct c_binding);
499 b->depth = scope->depth;
500 b->invisible = invisible;
506 b->prev = scope->bindings;
512 switch (TREE_CODE (decl))
514 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
517 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
523 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
529 /* Locate the appropriate place in the chain of shadowed decls
530 to insert this binding. Normally, scope == current_scope and
531 this does nothing. */
532 while (*here && (*here)->depth > scope->depth)
533 here = &(*here)->shadowed;
539 /* Clear the binding structure B, stick it on the binding_freelist,
540 and return the former value of b->prev. This is used by pop_scope
541 and get_parm_info to iterate destructively over all the bindings
542 from a given scope. */
543 static struct c_binding *
544 free_binding_and_advance (struct c_binding *b)
546 struct c_binding *prev = b->prev;
548 memset (b, 0, sizeof (struct c_binding));
549 b->prev = binding_freelist;
550 binding_freelist = b;
556 /* Hook called at end of compilation to assume 1 elt
557 for a file-scope tentative array defn that wasn't complete before. */
560 c_finish_incomplete_decl (tree decl)
562 if (TREE_CODE (decl) == VAR_DECL)
564 tree type = TREE_TYPE (decl);
565 if (type != error_mark_node
566 && TREE_CODE (type) == ARRAY_TYPE
567 && !DECL_EXTERNAL (decl)
568 && TYPE_DOMAIN (type) == 0)
570 warning (0, "array %q+D assumed to have one element", decl);
572 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
574 layout_decl (decl, 0);
579 /* The Objective-C front-end often needs to determine the current scope. */
582 objc_get_current_scope (void)
584 return current_scope;
587 /* The following function is used only by Objective-C. It needs to live here
588 because it accesses the innards of c_scope. */
591 objc_mark_locals_volatile (void *enclosing_blk)
593 struct c_scope *scope;
596 for (scope = current_scope;
597 scope && scope != enclosing_blk;
598 scope = scope->outer)
600 for (b = scope->bindings; b; b = b->prev)
601 objc_volatilize_decl (b->decl);
603 /* Do not climb up past the current function. */
604 if (scope->function_body)
609 /* Nonzero if we are currently in file scope. */
612 global_bindings_p (void)
614 return current_scope == file_scope && !c_override_global_bindings_to_false;
618 keep_next_level (void)
620 keep_next_level_flag = true;
623 /* Identify this scope as currently being filled with parameters. */
626 declare_parm_level (void)
628 current_scope->parm_flag = true;
634 if (next_is_function_body)
636 /* This is the transition from the parameters to the top level
637 of the function body. These are the same scope
638 (C99 6.2.1p4,6) so we do not push another scope structure.
639 next_is_function_body is set only by store_parm_decls, which
640 in turn is called when and only when we are about to
641 encounter the opening curly brace for the function body.
643 The outermost block of a function always gets a BLOCK node,
644 because the debugging output routines expect that each
645 function has at least one BLOCK. */
646 current_scope->parm_flag = false;
647 current_scope->function_body = true;
648 current_scope->keep = true;
649 current_scope->outer_function = current_function_scope;
650 current_function_scope = current_scope;
652 keep_next_level_flag = false;
653 next_is_function_body = false;
657 struct c_scope *scope;
660 scope = scope_freelist;
661 scope_freelist = scope->outer;
664 scope = GGC_CNEW (struct c_scope);
666 scope->keep = keep_next_level_flag;
667 scope->outer = current_scope;
668 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
670 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
672 if (current_scope && scope->depth == 0)
675 sorry ("GCC supports only %u nested scopes", scope->depth);
678 current_scope = scope;
679 keep_next_level_flag = false;
683 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
686 set_type_context (tree type, tree context)
688 for (type = TYPE_MAIN_VARIANT (type); type;
689 type = TYPE_NEXT_VARIANT (type))
690 TYPE_CONTEXT (type) = context;
693 /* Exit a scope. Restore the state of the identifier-decl mappings
694 that were in effect when this scope was entered. Return a BLOCK
695 node containing all the DECLs in this scope that are of interest
696 to debug info generation. */
701 struct c_scope *scope = current_scope;
702 tree block, context, p;
705 bool functionbody = scope->function_body;
706 bool keep = functionbody || scope->keep || scope->bindings;
708 c_end_vm_scope (scope->depth);
710 /* If appropriate, create a BLOCK to record the decls for the life
715 block = make_node (BLOCK);
716 BLOCK_SUBBLOCKS (block) = scope->blocks;
717 TREE_USED (block) = 1;
719 /* In each subblock, record that this is its superior. */
720 for (p = scope->blocks; p; p = TREE_CHAIN (p))
721 BLOCK_SUPERCONTEXT (p) = block;
723 BLOCK_VARS (block) = 0;
726 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
727 scope must be set so that they point to the appropriate
728 construct, i.e. either to the current FUNCTION_DECL node, or
729 else to the BLOCK node we just constructed.
731 Note that for tagged types whose scope is just the formal
732 parameter list for some function type specification, we can't
733 properly set their TYPE_CONTEXTs here, because we don't have a
734 pointer to the appropriate FUNCTION_TYPE node readily available
735 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
736 type nodes get set in `grokdeclarator' as soon as we have created
737 the FUNCTION_TYPE node which will represent the "scope" for these
738 "parameter list local" tagged types. */
739 if (scope->function_body)
740 context = current_function_decl;
741 else if (scope == file_scope)
743 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
744 TREE_CHAIN (file_decl) = all_translation_units;
745 all_translation_units = file_decl;
751 /* Clear all bindings in this scope. */
752 for (b = scope->bindings; b; b = free_binding_and_advance (b))
755 switch (TREE_CODE (p))
758 /* Warnings for unused labels, errors for undefined labels. */
759 if (TREE_USED (p) && !DECL_INITIAL (p))
761 error ("label %q+D used but not defined", p);
762 DECL_INITIAL (p) = error_mark_node;
765 warn_for_unused_label (p);
767 /* Labels go in BLOCK_VARS. */
768 TREE_CHAIN (p) = BLOCK_VARS (block);
769 BLOCK_VARS (block) = p;
770 gcc_assert (I_LABEL_BINDING (b->id) == b);
771 I_LABEL_BINDING (b->id) = b->shadowed;
777 set_type_context (p, context);
779 /* Types may not have tag-names, in which case the type
780 appears in the bindings list with b->id NULL. */
783 gcc_assert (I_TAG_BINDING (b->id) == b);
784 I_TAG_BINDING (b->id) = b->shadowed;
789 /* Propagate TREE_ADDRESSABLE from nested functions to their
790 containing functions. */
791 if (!TREE_ASM_WRITTEN (p)
792 && DECL_INITIAL (p) != 0
793 && TREE_ADDRESSABLE (p)
794 && DECL_ABSTRACT_ORIGIN (p) != 0
795 && DECL_ABSTRACT_ORIGIN (p) != p)
796 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
797 if (!DECL_EXTERNAL (p)
799 && scope != file_scope
800 && scope != external_scope)
802 error ("nested function %q+D declared but never defined", p);
803 undef_nested_function = true;
805 /* C99 6.7.4p6: "a function with external linkage... declared
806 with an inline function specifier ... shall also be defined in the
807 same translation unit." */
808 else if (DECL_DECLARED_INLINE_P (p)
812 pedwarn ("inline function %q+D declared but never defined", p);
817 /* Warnings for unused variables. */
819 && !TREE_NO_WARNING (p)
820 && !DECL_IN_SYSTEM_HEADER (p)
822 && !DECL_ARTIFICIAL (p)
823 && scope != file_scope
824 && scope != external_scope)
825 warning (OPT_Wunused_variable, "unused variable %q+D", p);
829 error ("type of array %q+D completed incompatibly with"
830 " implicit initialization", p);
837 /* All of these go in BLOCK_VARS, but only if this is the
838 binding in the home scope. */
841 TREE_CHAIN (p) = BLOCK_VARS (block);
842 BLOCK_VARS (block) = p;
844 /* If this is the file scope, and we are processing more
845 than one translation unit in this compilation, set
846 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
847 This makes same_translation_unit_p work, and causes
848 static declarations to be given disambiguating suffixes. */
849 if (scope == file_scope && num_in_fnames > 1)
851 DECL_CONTEXT (p) = context;
852 if (TREE_CODE (p) == TYPE_DECL)
853 set_type_context (TREE_TYPE (p), context);
857 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
858 already been put there by store_parm_decls. Unused-
859 parameter warnings are handled by function.c.
860 error_mark_node obviously does not go in BLOCK_VARS and
861 does not get unused-variable warnings. */
864 /* It is possible for a decl not to have a name. We get
865 here with b->id NULL in this case. */
868 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
869 I_SYMBOL_BINDING (b->id) = b->shadowed;
870 if (b->shadowed && b->shadowed->type)
871 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
881 /* Dispose of the block that we just made inside some higher level. */
882 if ((scope->function_body || scope == file_scope) && context)
884 DECL_INITIAL (context) = block;
885 BLOCK_SUPERCONTEXT (block) = context;
887 else if (scope->outer)
890 SCOPE_LIST_APPEND (scope->outer, blocks, block);
891 /* If we did not make a block for the scope just exited, any
892 blocks made for inner scopes must be carried forward so they
893 will later become subblocks of something else. */
894 else if (scope->blocks)
895 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
898 /* Pop the current scope, and free the structure for reuse. */
899 current_scope = scope->outer;
900 if (scope->function_body)
901 current_function_scope = scope->outer_function;
903 memset (scope, 0, sizeof (struct c_scope));
904 scope->outer = scope_freelist;
905 scope_freelist = scope;
911 push_file_scope (void)
919 file_scope = current_scope;
921 start_fname_decls ();
923 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
924 bind (DECL_NAME (decl), decl, file_scope,
925 /*invisible=*/false, /*nested=*/true);
929 pop_file_scope (void)
931 /* In case there were missing closebraces, get us back to the global
933 while (current_scope != file_scope)
936 /* __FUNCTION__ is defined at file scope (""). This
937 call may not be necessary as my tests indicate it
938 still works without it. */
939 finish_fname_decls ();
941 /* This is the point to write out a PCH if we're doing that.
942 In that case we do not want to do anything else. */
945 c_common_write_pch ();
949 /* Pop off the file scope and close this translation unit. */
953 maybe_apply_pending_pragma_weaks ();
954 cgraph_finalize_compilation_unit ();
957 /* Insert BLOCK at the end of the list of subblocks of the current
958 scope. This is used when a BIND_EXPR is expanded, to handle the
959 BLOCK node inside the BIND_EXPR. */
962 insert_block (tree block)
964 TREE_USED (block) = 1;
965 SCOPE_LIST_APPEND (current_scope, blocks, block);
968 /* Push a definition or a declaration of struct, union or enum tag "name".
969 "type" should be the type node.
970 We assume that the tag "name" is not already defined.
972 Note that the definition may really be just a forward reference.
973 In that case, the TYPE_SIZE will be zero. */
976 pushtag (tree name, tree type)
978 /* Record the identifier as the type's name if it has none. */
979 if (name && !TYPE_NAME (type))
980 TYPE_NAME (type) = name;
981 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
983 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
984 tagged type we just added to the current scope. This fake
985 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
986 to output a representation of a tagged type, and it also gives
987 us a convenient place to record the "scope start" address for the
990 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
992 /* An approximation for now, so we can tell this is a function-scope tag.
993 This will be updated in pop_scope. */
994 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
997 /* Subroutine of compare_decls. Allow harmless mismatches in return
998 and argument types provided that the type modes match. This function
999 return a unified type given a suitable match, and 0 otherwise. */
1002 match_builtin_function_types (tree newtype, tree oldtype)
1004 tree newrettype, oldrettype;
1005 tree newargs, oldargs;
1006 tree trytype, tryargs;
1008 /* Accept the return type of the new declaration if same modes. */
1009 oldrettype = TREE_TYPE (oldtype);
1010 newrettype = TREE_TYPE (newtype);
1012 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1015 oldargs = TYPE_ARG_TYPES (oldtype);
1016 newargs = TYPE_ARG_TYPES (newtype);
1019 while (oldargs || newargs)
1023 || !TREE_VALUE (oldargs)
1024 || !TREE_VALUE (newargs)
1025 || TYPE_MODE (TREE_VALUE (oldargs))
1026 != TYPE_MODE (TREE_VALUE (newargs)))
1029 oldargs = TREE_CHAIN (oldargs);
1030 newargs = TREE_CHAIN (newargs);
1033 trytype = build_function_type (newrettype, tryargs);
1034 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1037 /* Subroutine of diagnose_mismatched_decls. Check for function type
1038 mismatch involving an empty arglist vs a nonempty one and give clearer
1041 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1042 tree newtype, tree oldtype)
1046 if (TREE_CODE (olddecl) != FUNCTION_DECL
1047 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1048 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1050 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1053 t = TYPE_ARG_TYPES (oldtype);
1055 t = TYPE_ARG_TYPES (newtype);
1056 for (; t; t = TREE_CHAIN (t))
1058 tree type = TREE_VALUE (t);
1060 if (TREE_CHAIN (t) == 0
1061 && TYPE_MAIN_VARIANT (type) != void_type_node)
1063 inform ("a parameter list with an ellipsis can%'t match "
1064 "an empty parameter name list declaration");
1068 if (c_type_promotes_to (type) != type)
1070 inform ("an argument type that has a default promotion can%'t match "
1071 "an empty parameter name list declaration");
1077 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1078 old-style function definition, NEWDECL is a prototype declaration.
1079 Diagnose inconsistencies in the argument list. Returns TRUE if
1080 the prototype is compatible, FALSE if not. */
1082 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1084 tree newargs, oldargs;
1087 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1089 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1090 newargs = TYPE_ARG_TYPES (newtype);
1095 tree oldargtype = TREE_VALUE (oldargs);
1096 tree newargtype = TREE_VALUE (newargs);
1098 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1101 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1102 newargtype = TYPE_MAIN_VARIANT (newargtype);
1104 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1107 /* Reaching the end of just one list means the two decls don't
1108 agree on the number of arguments. */
1109 if (END_OF_ARGLIST (oldargtype))
1111 error ("prototype for %q+D declares more arguments "
1112 "than previous old-style definition", newdecl);
1115 else if (END_OF_ARGLIST (newargtype))
1117 error ("prototype for %q+D declares fewer arguments "
1118 "than previous old-style definition", newdecl);
1122 /* Type for passing arg must be consistent with that declared
1124 else if (!comptypes (oldargtype, newargtype))
1126 error ("prototype for %q+D declares argument %d"
1127 " with incompatible type",
1132 oldargs = TREE_CHAIN (oldargs);
1133 newargs = TREE_CHAIN (newargs);
1137 /* If we get here, no errors were found, but do issue a warning
1138 for this poor-style construct. */
1139 warning (0, "prototype for %q+D follows non-prototype definition",
1142 #undef END_OF_ARGLIST
1145 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1146 first in a pair of mismatched declarations, using the diagnostic
1149 locate_old_decl (tree decl, void (*diag)(const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2))
1151 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1153 else if (DECL_INITIAL (decl))
1154 diag (G_("previous definition of %q+D was here"), decl);
1155 else if (C_DECL_IMPLICIT (decl))
1156 diag (G_("previous implicit declaration of %q+D was here"), decl);
1158 diag (G_("previous declaration of %q+D was here"), decl);
1161 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1162 Returns true if the caller should proceed to merge the two, false
1163 if OLDDECL should simply be discarded. As a side effect, issues
1164 all necessary diagnostics for invalid or poor-style combinations.
1165 If it returns true, writes the types of NEWDECL and OLDDECL to
1166 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1167 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1170 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1171 tree *newtypep, tree *oldtypep)
1173 tree newtype, oldtype;
1174 bool pedwarned = false;
1175 bool warned = false;
1178 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1179 && DECL_EXTERNAL (DECL))
1181 /* If we have error_mark_node for either decl or type, just discard
1182 the previous decl - we're in an error cascade already. */
1183 if (olddecl == error_mark_node || newdecl == error_mark_node)
1185 *oldtypep = oldtype = TREE_TYPE (olddecl);
1186 *newtypep = newtype = TREE_TYPE (newdecl);
1187 if (oldtype == error_mark_node || newtype == error_mark_node)
1190 /* Two different categories of symbol altogether. This is an error
1191 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1192 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1194 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1195 && DECL_BUILT_IN (olddecl)
1196 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1198 error ("%q+D redeclared as different kind of symbol", newdecl);
1199 locate_old_decl (olddecl, error);
1201 else if (TREE_PUBLIC (newdecl))
1202 warning (0, "built-in function %q+D declared as non-function",
1205 warning (OPT_Wshadow, "declaration of %q+D shadows "
1206 "a built-in function", newdecl);
1210 /* Enumerators have no linkage, so may only be declared once in a
1212 if (TREE_CODE (olddecl) == CONST_DECL)
1214 error ("redeclaration of enumerator %q+D", newdecl);
1215 locate_old_decl (olddecl, error);
1219 if (!comptypes (oldtype, newtype))
1221 if (TREE_CODE (olddecl) == FUNCTION_DECL
1222 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1224 /* Accept harmless mismatch in function types.
1225 This is for the ffs and fprintf builtins. */
1226 tree trytype = match_builtin_function_types (newtype, oldtype);
1228 if (trytype && comptypes (newtype, trytype))
1229 *oldtypep = oldtype = trytype;
1232 /* If types don't match for a built-in, throw away the
1233 built-in. No point in calling locate_old_decl here, it
1234 won't print anything. */
1235 warning (0, "conflicting types for built-in function %q+D",
1240 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1241 && DECL_IS_BUILTIN (olddecl))
1243 /* A conflicting function declaration for a predeclared
1244 function that isn't actually built in. Objective C uses
1245 these. The new declaration silently overrides everything
1246 but the volatility (i.e. noreturn) indication. See also
1247 below. FIXME: Make Objective C use normal builtins. */
1248 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1251 /* Permit void foo (...) to match int foo (...) if the latter is
1252 the definition and implicit int was used. See
1253 c-torture/compile/920625-2.c. */
1254 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1255 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1256 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1257 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1259 pedwarn ("conflicting types for %q+D", newdecl);
1260 /* Make sure we keep void as the return type. */
1261 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1262 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1265 /* Permit void foo (...) to match an earlier call to foo (...) with
1266 no declared type (thus, implicitly int). */
1267 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1268 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1269 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1270 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1272 pedwarn ("conflicting types for %q+D", newdecl);
1273 /* Make sure we keep void as the return type. */
1274 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1279 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1280 error ("conflicting type qualifiers for %q+D", newdecl);
1282 error ("conflicting types for %q+D", newdecl);
1283 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1284 locate_old_decl (olddecl, error);
1289 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1290 but silently ignore the redeclaration if either is in a system
1291 header. (Conflicting redeclarations were handled above.) */
1292 if (TREE_CODE (newdecl) == TYPE_DECL)
1294 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1295 return true; /* Allow OLDDECL to continue in use. */
1297 error ("redefinition of typedef %q+D", newdecl);
1298 locate_old_decl (olddecl, error);
1302 /* Function declarations can either be 'static' or 'extern' (no
1303 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1304 can never conflict with each other on account of linkage
1305 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1306 gnu89 mode permits two definitions if one is 'extern inline' and
1307 one is not. The non- extern-inline definition supersedes the
1308 extern-inline definition. */
1310 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1312 /* If you declare a built-in function name as static, or
1313 define the built-in with an old-style definition (so we
1314 can't validate the argument list) the built-in definition is
1315 overridden, but optionally warn this was a bad choice of name. */
1316 if (DECL_BUILT_IN (olddecl)
1317 && !C_DECL_DECLARED_BUILTIN (olddecl)
1318 && (!TREE_PUBLIC (newdecl)
1319 || (DECL_INITIAL (newdecl)
1320 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1322 warning (OPT_Wshadow, "declaration of %q+D shadows "
1323 "a built-in function", newdecl);
1324 /* Discard the old built-in function. */
1328 if (DECL_INITIAL (newdecl))
1330 if (DECL_INITIAL (olddecl))
1332 /* If both decls are in the same TU and the new declaration
1333 isn't overriding an extern inline reject the new decl.
1334 In c99, no overriding is allowed in the same translation
1336 if ((!DECL_EXTERN_INLINE (olddecl)
1337 || DECL_EXTERN_INLINE (newdecl)
1338 #if WANT_C99_INLINE_SEMANTICS
1340 && (!DECL_DECLARED_INLINE_P (olddecl)
1341 || !lookup_attribute ("gnu_inline",
1342 DECL_ATTRIBUTES (olddecl)))
1343 && (!DECL_DECLARED_INLINE_P (newdecl)
1344 || !lookup_attribute ("gnu_inline",
1345 DECL_ATTRIBUTES (newdecl))))
1346 #endif /* WANT_C99_INLINE_SEMANTICS */
1348 && same_translation_unit_p (newdecl, olddecl))
1350 error ("redefinition of %q+D", newdecl);
1351 locate_old_decl (olddecl, error);
1356 /* If we have a prototype after an old-style function definition,
1357 the argument types must be checked specially. */
1358 else if (DECL_INITIAL (olddecl)
1359 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1360 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1361 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1363 locate_old_decl (olddecl, error);
1366 /* A non-static declaration (even an "extern") followed by a
1367 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1368 The same is true for a static forward declaration at block
1369 scope followed by a non-static declaration/definition at file
1370 scope. Static followed by non-static at the same scope is
1371 not undefined behavior, and is the most convenient way to get
1372 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1373 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1374 we do diagnose it if -Wtraditional. */
1375 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1377 /* Two exceptions to the rule. If olddecl is an extern
1378 inline, or a predeclared function that isn't actually
1379 built in, newdecl silently overrides olddecl. The latter
1380 occur only in Objective C; see also above. (FIXME: Make
1381 Objective C use normal builtins.) */
1382 if (!DECL_IS_BUILTIN (olddecl)
1383 && !DECL_EXTERN_INLINE (olddecl))
1385 error ("static declaration of %q+D follows "
1386 "non-static declaration", newdecl);
1387 locate_old_decl (olddecl, error);
1391 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1393 if (DECL_CONTEXT (olddecl))
1395 error ("non-static declaration of %q+D follows "
1396 "static declaration", newdecl);
1397 locate_old_decl (olddecl, error);
1400 else if (warn_traditional)
1402 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1403 "follows static declaration", newdecl);
1408 /* Make sure gnu_inline attribute is either not present, or
1409 present on all inline decls. */
1410 if (DECL_DECLARED_INLINE_P (olddecl)
1411 && DECL_DECLARED_INLINE_P (newdecl))
1413 bool newa = lookup_attribute ("gnu_inline",
1414 DECL_ATTRIBUTES (newdecl)) != NULL;
1415 bool olda = lookup_attribute ("gnu_inline",
1416 DECL_ATTRIBUTES (olddecl)) != NULL;
1419 error ("%<gnu_inline%> attribute present on %q+D",
1420 newa ? newdecl : olddecl);
1421 error ("%Jbut not here", newa ? olddecl : newdecl);
1425 else if (TREE_CODE (newdecl) == VAR_DECL)
1427 /* Only variables can be thread-local, and all declarations must
1428 agree on this property. */
1429 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1431 /* Nothing to check. Since OLDDECL is marked threadprivate
1432 and NEWDECL does not have a thread-local attribute, we
1433 will merge the threadprivate attribute into NEWDECL. */
1436 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1438 if (DECL_THREAD_LOCAL_P (newdecl))
1439 error ("thread-local declaration of %q+D follows "
1440 "non-thread-local declaration", newdecl);
1442 error ("non-thread-local declaration of %q+D follows "
1443 "thread-local declaration", newdecl);
1445 locate_old_decl (olddecl, error);
1449 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1450 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1452 error ("redefinition of %q+D", newdecl);
1453 locate_old_decl (olddecl, error);
1457 /* Objects declared at file scope: if the first declaration had
1458 external linkage (even if it was an external reference) the
1459 second must have external linkage as well, or the behavior is
1460 undefined. If the first declaration had internal linkage, then
1461 the second must too, or else be an external reference (in which
1462 case the composite declaration still has internal linkage).
1463 As for function declarations, we warn about the static-then-
1464 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1465 if (DECL_FILE_SCOPE_P (newdecl)
1466 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1468 if (DECL_EXTERNAL (newdecl))
1470 if (!DECL_FILE_SCOPE_P (olddecl))
1472 error ("extern declaration of %q+D follows "
1473 "declaration with no linkage", newdecl);
1474 locate_old_decl (olddecl, error);
1477 else if (warn_traditional)
1479 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1480 "follows static declaration", newdecl);
1486 if (TREE_PUBLIC (newdecl))
1487 error ("non-static declaration of %q+D follows "
1488 "static declaration", newdecl);
1490 error ("static declaration of %q+D follows "
1491 "non-static declaration", newdecl);
1493 locate_old_decl (olddecl, error);
1497 /* Two objects with the same name declared at the same block
1498 scope must both be external references (6.7p3). */
1499 else if (!DECL_FILE_SCOPE_P (newdecl))
1501 if (DECL_EXTERNAL (newdecl))
1503 /* Extern with initializer at block scope, which will
1504 already have received an error. */
1506 else if (DECL_EXTERNAL (olddecl))
1508 error ("declaration of %q+D with no linkage follows "
1509 "extern declaration", newdecl);
1510 locate_old_decl (olddecl, error);
1514 error ("redeclaration of %q+D with no linkage", newdecl);
1515 locate_old_decl (olddecl, error);
1523 /* All decls must agree on a visibility. */
1524 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1525 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1526 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1528 warning (0, "redeclaration of %q+D with different visibility "
1529 "(old visibility preserved)", newdecl);
1533 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1535 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1536 if (DECL_DECLARED_INLINE_P (newdecl)
1537 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1539 warning (OPT_Wattributes, "inline declaration of %qD follows "
1540 "declaration with attribute noinline", newdecl);
1543 else if (DECL_DECLARED_INLINE_P (olddecl)
1544 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1546 warning (OPT_Wattributes, "declaration of %q+D with attribute "
1547 "noinline follows inline declaration ", newdecl);
1551 /* Inline declaration after use or definition.
1552 ??? Should we still warn about this now we have unit-at-a-time
1553 mode and can get it right?
1554 Definitely don't complain if the decls are in different translation
1556 C99 permits this, so don't warn in that case. (The function
1557 may not be inlined everywhere in function-at-a-time mode, but
1558 we still shouldn't warn.) */
1559 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1560 && same_translation_unit_p (olddecl, newdecl)
1563 if (TREE_USED (olddecl))
1565 warning (0, "%q+D declared inline after being called", olddecl);
1568 else if (DECL_INITIAL (olddecl))
1570 warning (0, "%q+D declared inline after its definition", olddecl);
1575 else /* PARM_DECL, VAR_DECL */
1577 /* Redeclaration of a parameter is a constraint violation (this is
1578 not explicitly stated, but follows from C99 6.7p3 [no more than
1579 one declaration of the same identifier with no linkage in the
1580 same scope, except type tags] and 6.2.2p6 [parameters have no
1581 linkage]). We must check for a forward parameter declaration,
1582 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1583 an extension, the mandatory diagnostic for which is handled by
1584 mark_forward_parm_decls. */
1586 if (TREE_CODE (newdecl) == PARM_DECL
1587 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1589 error ("redefinition of parameter %q+D", newdecl);
1590 locate_old_decl (olddecl, error);
1595 /* Optional warning for completely redundant decls. */
1596 if (!warned && !pedwarned
1597 && warn_redundant_decls
1598 /* Don't warn about a function declaration followed by a
1600 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1601 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1602 /* Don't warn about redundant redeclarations of builtins. */
1603 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1604 && !DECL_BUILT_IN (newdecl)
1605 && DECL_BUILT_IN (olddecl)
1606 && !C_DECL_DECLARED_BUILTIN (olddecl))
1607 /* Don't warn about an extern followed by a definition. */
1608 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1609 /* Don't warn about forward parameter decls. */
1610 && !(TREE_CODE (newdecl) == PARM_DECL
1611 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1612 /* Don't warn about a variable definition following a declaration. */
1613 && !(TREE_CODE (newdecl) == VAR_DECL
1614 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1616 warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1621 /* Report location of previous decl/defn in a consistent manner. */
1622 if (warned || pedwarned)
1623 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1625 #undef DECL_EXTERN_INLINE
1630 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1631 consistent with OLDDECL, but carries new information. Merge the
1632 new information into OLDDECL. This function issues no
1636 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1638 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1639 && DECL_INITIAL (newdecl) != 0);
1640 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1641 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1642 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1643 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1644 bool extern_changed = false;
1646 /* For real parm decl following a forward decl, rechain the old decl
1647 in its new location and clear TREE_ASM_WRITTEN (it's not a
1648 forward decl anymore). */
1649 if (TREE_CODE (newdecl) == PARM_DECL
1650 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1652 struct c_binding *b, **here;
1654 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
1655 if ((*here)->decl == olddecl)
1662 b->prev = current_scope->bindings;
1663 current_scope->bindings = b;
1665 TREE_ASM_WRITTEN (olddecl) = 0;
1668 DECL_ATTRIBUTES (newdecl)
1669 = targetm.merge_decl_attributes (olddecl, newdecl);
1671 /* Merge the data types specified in the two decls. */
1673 = TREE_TYPE (olddecl)
1674 = composite_type (newtype, oldtype);
1676 /* Lay the type out, unless already done. */
1677 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1679 if (TREE_TYPE (newdecl) != error_mark_node)
1680 layout_type (TREE_TYPE (newdecl));
1681 if (TREE_CODE (newdecl) != FUNCTION_DECL
1682 && TREE_CODE (newdecl) != TYPE_DECL
1683 && TREE_CODE (newdecl) != CONST_DECL)
1684 layout_decl (newdecl, 0);
1688 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1689 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1690 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1691 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1692 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1693 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1695 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1696 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1701 /* Merge the type qualifiers. */
1702 if (TREE_READONLY (newdecl))
1703 TREE_READONLY (olddecl) = 1;
1705 if (TREE_THIS_VOLATILE (newdecl))
1707 TREE_THIS_VOLATILE (olddecl) = 1;
1708 if (TREE_CODE (newdecl) == VAR_DECL)
1709 make_var_volatile (newdecl);
1712 /* Merge deprecatedness. */
1713 if (TREE_DEPRECATED (newdecl))
1714 TREE_DEPRECATED (olddecl) = 1;
1716 /* Keep source location of definition rather than declaration and of
1717 prototype rather than non-prototype unless that prototype is
1719 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1720 || (old_is_prototype && !new_is_prototype
1721 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1722 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1724 /* Merge the initialization information. */
1725 if (DECL_INITIAL (newdecl) == 0)
1726 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1728 /* Merge the threadprivate attribute. */
1729 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1731 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1732 C_DECL_THREADPRIVATE_P (newdecl) = 1;
1735 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1737 /* Merge the unused-warning information. */
1738 if (DECL_IN_SYSTEM_HEADER (olddecl))
1739 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1740 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1741 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1743 /* Merge the section attribute.
1744 We want to issue an error if the sections conflict but that
1745 must be done later in decl_attributes since we are called
1746 before attributes are assigned. */
1747 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1748 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1750 /* Copy the assembler name.
1751 Currently, it can only be defined in the prototype. */
1752 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1754 /* Use visibility of whichever declaration had it specified */
1755 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1757 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1758 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1761 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1763 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1764 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1765 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1766 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1767 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1768 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1769 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1770 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1771 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1772 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1775 /* Merge the storage class information. */
1776 merge_weak (newdecl, olddecl);
1778 /* For functions, static overrides non-static. */
1779 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1781 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1782 /* This is since we don't automatically
1783 copy the attributes of NEWDECL into OLDDECL. */
1784 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1785 /* If this clears `static', clear it in the identifier too. */
1786 if (!TREE_PUBLIC (olddecl))
1787 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1791 #if WANT_C99_INLINE_SEMANTICS
1792 /* In c99, 'extern' declaration before (or after) 'inline' means this
1793 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1795 if (TREE_CODE (newdecl) == FUNCTION_DECL
1797 && (DECL_DECLARED_INLINE_P (newdecl)
1798 || DECL_DECLARED_INLINE_P (olddecl))
1799 && (!DECL_DECLARED_INLINE_P (newdecl)
1800 || !DECL_DECLARED_INLINE_P (olddecl)
1801 || !DECL_EXTERNAL (olddecl))
1802 && DECL_EXTERNAL (newdecl)
1803 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl)))
1804 DECL_EXTERNAL (newdecl) = 0;
1805 #endif /* WANT_C99_INLINE_SEMANTICS */
1807 if (DECL_EXTERNAL (newdecl))
1809 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1810 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1812 /* An extern decl does not override previous storage class. */
1813 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1814 if (!DECL_EXTERNAL (newdecl))
1816 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1817 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1822 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1823 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1826 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1828 /* If we're redefining a function previously defined as extern
1829 inline, make sure we emit debug info for the inline before we
1830 throw it away, in case it was inlined into a function that
1831 hasn't been written out yet. */
1832 if (new_is_definition && DECL_INITIAL (olddecl))
1834 if (TREE_USED (olddecl)
1835 /* In unit-at-a-time mode we never inline re-defined extern
1836 inline functions. */
1837 && !flag_unit_at_a_time
1838 && cgraph_function_possibly_inlined_p (olddecl))
1839 (*debug_hooks->outlining_inline_function) (olddecl);
1841 /* The new defn must not be inline. */
1842 DECL_INLINE (newdecl) = 0;
1843 DECL_UNINLINABLE (newdecl) = 1;
1847 /* If either decl says `inline', this fn is inline, unless
1848 its definition was passed already. */
1849 if (DECL_DECLARED_INLINE_P (newdecl)
1850 || DECL_DECLARED_INLINE_P (olddecl))
1851 DECL_DECLARED_INLINE_P (newdecl) = 1;
1853 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1854 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1857 if (DECL_BUILT_IN (olddecl))
1859 /* If redeclaring a builtin function, it stays built in.
1860 But it gets tagged as having been declared. */
1861 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1862 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1863 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1864 if (new_is_prototype)
1865 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1867 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1868 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1871 /* Also preserve various other info from the definition. */
1872 if (!new_is_definition)
1874 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1875 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1876 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1877 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1878 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1880 /* Set DECL_INLINE on the declaration if we've got a body
1881 from which to instantiate. */
1882 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1884 DECL_INLINE (newdecl) = 1;
1885 DECL_ABSTRACT_ORIGIN (newdecl)
1886 = DECL_ABSTRACT_ORIGIN (olddecl);
1891 /* If a previous declaration said inline, mark the
1892 definition as inlinable. */
1893 if (DECL_DECLARED_INLINE_P (newdecl)
1894 && !DECL_UNINLINABLE (newdecl))
1895 DECL_INLINE (newdecl) = 1;
1899 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
1901 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1902 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1904 unsigned olddecl_uid = DECL_UID (olddecl);
1905 tree olddecl_context = DECL_CONTEXT (olddecl);
1907 memcpy ((char *) olddecl + sizeof (struct tree_common),
1908 (char *) newdecl + sizeof (struct tree_common),
1909 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1910 switch (TREE_CODE (olddecl))
1920 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1921 (char *) newdecl + sizeof (struct tree_decl_common),
1922 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1927 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1928 (char *) newdecl + sizeof (struct tree_decl_common),
1929 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1931 DECL_UID (olddecl) = olddecl_uid;
1932 DECL_CONTEXT (olddecl) = olddecl_context;
1935 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1936 so that encode_section_info has a chance to look at the new decl
1937 flags and attributes. */
1938 if (DECL_RTL_SET_P (olddecl)
1939 && (TREE_CODE (olddecl) == FUNCTION_DECL
1940 || (TREE_CODE (olddecl) == VAR_DECL
1941 && TREE_STATIC (olddecl))))
1942 make_decl_rtl (olddecl);
1944 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
1945 and the definition is coming from the old version, cgraph needs
1946 to be called again. */
1947 if (extern_changed && !new_is_definition
1948 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
1949 cgraph_finalize_function (olddecl, false);
1952 /* Handle when a new declaration NEWDECL has the same name as an old
1953 one OLDDECL in the same binding contour. Prints an error message
1956 If safely possible, alter OLDDECL to look like NEWDECL, and return
1957 true. Otherwise, return false. */
1960 duplicate_decls (tree newdecl, tree olddecl)
1962 tree newtype = NULL, oldtype = NULL;
1964 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1966 /* Avoid `unused variable' and other warnings warnings for OLDDECL. */
1967 TREE_NO_WARNING (olddecl) = 1;
1971 merge_decls (newdecl, olddecl, newtype, oldtype);
1976 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1978 warn_if_shadowing (tree new_decl)
1980 struct c_binding *b;
1982 /* Shadow warnings wanted? */
1984 /* No shadow warnings for internally generated vars. */
1985 || DECL_IS_BUILTIN (new_decl)
1986 /* No shadow warnings for vars made for inlining. */
1987 || DECL_FROM_INLINE (new_decl))
1990 /* Is anything being shadowed? Invisible decls do not count. */
1991 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1992 if (b->decl && b->decl != new_decl && !b->invisible)
1994 tree old_decl = b->decl;
1996 if (old_decl == error_mark_node)
1998 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
1999 "non-variable", new_decl);
2002 else if (TREE_CODE (old_decl) == PARM_DECL)
2003 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2005 else if (DECL_FILE_SCOPE_P (old_decl))
2006 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2007 "declaration", new_decl);
2008 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2009 && DECL_BUILT_IN (old_decl))
2011 warning (OPT_Wshadow, "declaration of %q+D shadows "
2012 "a built-in function", new_decl);
2016 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2019 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
2026 /* Subroutine of pushdecl.
2028 X is a TYPE_DECL for a typedef statement. Create a brand new
2029 ..._TYPE node (which will be just a variant of the existing
2030 ..._TYPE node with identical properties) and then install X
2031 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
2033 The whole point here is to end up with a situation where each
2034 and every ..._TYPE node the compiler creates will be uniquely
2035 associated with AT MOST one node representing a typedef name.
2036 This way, even though the compiler substitutes corresponding
2037 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
2038 early on, later parts of the compiler can always do the reverse
2039 translation and get back the corresponding typedef name. For
2042 typedef struct S MY_TYPE;
2045 Later parts of the compiler might only know that `object' was of
2046 type `struct S' if it were not for code just below. With this
2047 code however, later parts of the compiler see something like:
2049 struct S' == struct S
2050 typedef struct S' MY_TYPE;
2053 And they can then deduce (from the node for type struct S') that
2054 the original object declaration was:
2058 Being able to do this is important for proper support of protoize,
2059 and also for generating precise symbolic debugging information
2060 which takes full account of the programmer's (typedef) vocabulary.
2062 Obviously, we don't want to generate a duplicate ..._TYPE node if
2063 the TYPE_DECL node that we are now processing really represents a
2064 standard built-in type.
2066 Since all standard types are effectively declared at line zero
2067 in the source file, we can easily check to see if we are working
2068 on a standard type by checking the current value of lineno. */
2071 clone_underlying_type (tree x)
2073 if (DECL_IS_BUILTIN (x))
2075 if (TYPE_NAME (TREE_TYPE (x)) == 0)
2076 TYPE_NAME (TREE_TYPE (x)) = x;
2078 else if (TREE_TYPE (x) != error_mark_node
2079 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
2081 tree tt = TREE_TYPE (x);
2082 DECL_ORIGINAL_TYPE (x) = tt;
2083 tt = build_variant_type_copy (tt);
2085 TREE_USED (tt) = TREE_USED (x);
2090 /* Record a decl-node X as belonging to the current lexical scope.
2091 Check for errors (such as an incompatible declaration for the same
2092 name already seen in the same scope).
2094 Returns either X or an old decl for the same name.
2095 If an old decl is returned, it may have been smashed
2096 to agree with what X says. */
2101 tree name = DECL_NAME (x);
2102 struct c_scope *scope = current_scope;
2103 struct c_binding *b;
2104 bool nested = false;
2106 /* Functions need the lang_decl data. */
2107 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
2108 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
2110 /* Must set DECL_CONTEXT for everything not at file scope or
2111 DECL_FILE_SCOPE_P won't work. Local externs don't count
2112 unless they have initializers (which generate code). */
2113 if (current_function_decl
2114 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2115 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2116 DECL_CONTEXT (x) = current_function_decl;
2118 /* If this is of variably modified type, prevent jumping into its
2120 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2121 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2122 c_begin_vm_scope (scope->depth);
2124 /* Anonymous decls are just inserted in the scope. */
2127 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2131 /* First, see if there is another declaration with the same name in
2132 the current scope. If there is, duplicate_decls may do all the
2133 work for us. If duplicate_decls returns false, that indicates
2134 two incompatible decls in the same scope; we are to silently
2135 replace the old one (duplicate_decls has issued all appropriate
2136 diagnostics). In particular, we should not consider possible
2137 duplicates in the external scope, or shadowing. */
2138 b = I_SYMBOL_BINDING (name);
2139 if (b && B_IN_SCOPE (b, scope))
2141 struct c_binding *b_ext, *b_use;
2142 tree type = TREE_TYPE (x);
2143 tree visdecl = b->decl;
2144 tree vistype = TREE_TYPE (visdecl);
2145 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2146 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2147 b->inner_comp = false;
2150 /* If this is an external linkage declaration, we should check
2151 for compatibility with the type in the external scope before
2152 setting the type at this scope based on the visible
2153 information only. */
2154 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2156 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2157 b_ext = b_ext->shadowed;
2162 TREE_TYPE (b_use->decl) = b_use->type;
2165 if (duplicate_decls (x, b_use->decl))
2169 /* Save the updated type in the external scope and
2170 restore the proper type for this scope. */
2172 if (comptypes (vistype, type))
2173 thistype = composite_type (vistype, type);
2175 thistype = TREE_TYPE (b_use->decl);
2176 b_use->type = TREE_TYPE (b_use->decl);
2177 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2178 && DECL_BUILT_IN (b_use->decl))
2180 = build_type_attribute_variant (thistype,
2183 TREE_TYPE (b_use->decl) = thistype;
2188 goto skip_external_and_shadow_checks;
2191 /* All declarations with external linkage, and all external
2192 references, go in the external scope, no matter what scope is
2193 current. However, the binding in that scope is ignored for
2194 purposes of normal name lookup. A separate binding structure is
2195 created in the requested scope; this governs the normal
2196 visibility of the symbol.
2198 The binding in the externals scope is used exclusively for
2199 detecting duplicate declarations of the same object, no matter
2200 what scope they are in; this is what we do here. (C99 6.2.7p2:
2201 All declarations that refer to the same object or function shall
2202 have compatible type; otherwise, the behavior is undefined.) */
2203 if (DECL_EXTERNAL (x) || scope == file_scope)
2205 tree type = TREE_TYPE (x);
2208 bool type_saved = false;
2209 if (b && !B_IN_EXTERNAL_SCOPE (b)
2210 && (TREE_CODE (b->decl) == FUNCTION_DECL
2211 || TREE_CODE (b->decl) == VAR_DECL)
2212 && DECL_FILE_SCOPE_P (b->decl))
2215 vistype = TREE_TYPE (visdecl);
2217 if (scope != file_scope
2218 && !DECL_IN_SYSTEM_HEADER (x))
2219 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2221 while (b && !B_IN_EXTERNAL_SCOPE (b))
2223 /* If this decl might be modified, save its type. This is
2224 done here rather than when the decl is first bound
2225 because the type may change after first binding, through
2226 being completed or through attributes being added. If we
2227 encounter multiple such decls, only the first should have
2228 its type saved; the others will already have had their
2229 proper types saved and the types will not have changed as
2230 their scopes will not have been re-entered. */
2231 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2233 b->type = TREE_TYPE (b->decl);
2236 if (B_IN_FILE_SCOPE (b)
2237 && TREE_CODE (b->decl) == VAR_DECL
2238 && TREE_STATIC (b->decl)
2239 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2240 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2241 && TREE_CODE (type) == ARRAY_TYPE
2242 && TYPE_DOMAIN (type)
2243 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2244 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2246 /* Array type completed in inner scope, which should be
2247 diagnosed if the completion does not have size 1 and
2248 it does not get completed in the file scope. */
2249 b->inner_comp = true;
2254 /* If a matching external declaration has been found, set its
2255 type to the composite of all the types of that declaration.
2256 After the consistency checks, it will be reset to the
2257 composite of the visible types only. */
2258 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2260 TREE_TYPE (b->decl) = b->type;
2262 /* The point of the same_translation_unit_p check here is,
2263 we want to detect a duplicate decl for a construct like
2264 foo() { extern bar(); } ... static bar(); but not if
2265 they are in different translation units. In any case,
2266 the static does not go in the externals scope. */
2268 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2269 && duplicate_decls (x, b->decl))
2274 if (comptypes (vistype, type))
2275 thistype = composite_type (vistype, type);
2277 thistype = TREE_TYPE (b->decl);
2281 b->type = TREE_TYPE (b->decl);
2282 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2284 = build_type_attribute_variant (thistype,
2285 TYPE_ATTRIBUTES (b->type));
2286 TREE_TYPE (b->decl) = thistype;
2287 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2290 else if (TREE_PUBLIC (x))
2292 if (visdecl && !b && duplicate_decls (x, visdecl))
2294 /* An external declaration at block scope referring to a
2295 visible entity with internal linkage. The composite
2296 type will already be correct for this scope, so we
2297 just need to fall through to make the declaration in
2304 bind (name, x, external_scope, /*invisible=*/true,
2311 if (TREE_CODE (x) != PARM_DECL)
2312 warn_if_shadowing (x);
2314 skip_external_and_shadow_checks:
2315 if (TREE_CODE (x) == TYPE_DECL)
2316 clone_underlying_type (x);
2318 bind (name, x, scope, /*invisible=*/false, nested);
2320 /* If x's type is incomplete because it's based on a
2321 structure or union which has not yet been fully declared,
2322 attach it to that structure or union type, so we can go
2323 back and complete the variable declaration later, if the
2324 structure or union gets fully declared.
2326 If the input is erroneous, we can have error_mark in the type
2327 slot (e.g. "f(void a, ...)") - that doesn't count as an
2329 if (TREE_TYPE (x) != error_mark_node
2330 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2332 tree element = TREE_TYPE (x);
2334 while (TREE_CODE (element) == ARRAY_TYPE)
2335 element = TREE_TYPE (element);
2336 element = TYPE_MAIN_VARIANT (element);
2338 if ((TREE_CODE (element) == RECORD_TYPE
2339 || TREE_CODE (element) == UNION_TYPE)
2340 && (TREE_CODE (x) != TYPE_DECL
2341 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2342 && !COMPLETE_TYPE_P (element))
2343 C_TYPE_INCOMPLETE_VARS (element)
2344 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2349 /* Record X as belonging to file scope.
2350 This is used only internally by the Objective-C front end,
2351 and is limited to its needs. duplicate_decls is not called;
2352 if there is any preexisting decl for this identifier, it is an ICE. */
2355 pushdecl_top_level (tree x)
2358 bool nested = false;
2359 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2361 name = DECL_NAME (x);
2363 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2365 if (TREE_PUBLIC (x))
2367 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2371 bind (name, x, file_scope, /*invisible=*/false, nested);
2377 implicit_decl_warning (tree id, tree olddecl)
2379 if (warn_implicit_function_declaration)
2382 pedwarn (G_("implicit declaration of function %qE"), id);
2384 warning (OPT_Wimplicit_function_declaration,
2385 G_("implicit declaration of function %qE"), id);
2387 locate_old_decl (olddecl, inform);
2391 /* Generate an implicit declaration for identifier FUNCTIONID as a
2392 function of type int (). */
2395 implicitly_declare (tree functionid)
2397 struct c_binding *b;
2401 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2403 if (B_IN_SCOPE (b, external_scope))
2412 if (decl == error_mark_node)
2415 /* FIXME: Objective-C has weird not-really-builtin functions
2416 which are supposed to be visible automatically. They wind up
2417 in the external scope because they're pushed before the file
2418 scope gets created. Catch this here and rebind them into the
2420 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2422 bind (functionid, decl, file_scope,
2423 /*invisible=*/false, /*nested=*/true);
2428 tree newtype = default_function_type;
2430 TREE_TYPE (decl) = b->type;
2431 /* Implicit declaration of a function already declared
2432 (somehow) in a different scope, or as a built-in.
2433 If this is the first time this has happened, warn;
2434 then recycle the old declaration but with the new type. */
2435 if (!C_DECL_IMPLICIT (decl))
2437 implicit_decl_warning (functionid, decl);
2438 C_DECL_IMPLICIT (decl) = 1;
2440 if (DECL_BUILT_IN (decl))
2442 newtype = build_type_attribute_variant (newtype,
2444 (TREE_TYPE (decl)));
2445 if (!comptypes (newtype, TREE_TYPE (decl)))
2447 warning (0, "incompatible implicit declaration of built-in"
2448 " function %qD", decl);
2449 newtype = TREE_TYPE (decl);
2454 if (!comptypes (newtype, TREE_TYPE (decl)))
2456 error ("incompatible implicit declaration of function %qD",
2458 locate_old_decl (decl, error);
2461 b->type = TREE_TYPE (decl);
2462 TREE_TYPE (decl) = newtype;
2463 bind (functionid, decl, current_scope,
2464 /*invisible=*/false, /*nested=*/true);
2469 /* Not seen before. */
2470 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2471 DECL_EXTERNAL (decl) = 1;
2472 TREE_PUBLIC (decl) = 1;
2473 C_DECL_IMPLICIT (decl) = 1;
2474 implicit_decl_warning (functionid, 0);
2475 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2477 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2479 /* C89 says implicit declarations are in the innermost block.
2480 So we record the decl in the standard fashion. */
2481 decl = pushdecl (decl);
2483 /* No need to call objc_check_decl here - it's a function type. */
2484 rest_of_decl_compilation (decl, 0, 0);
2486 /* Write a record describing this implicit function declaration
2487 to the prototypes file (if requested). */
2488 gen_aux_info_record (decl, 0, 1, 0);
2490 /* Possibly apply some default attributes to this implicit declaration. */
2491 decl_attributes (&decl, NULL_TREE, 0);
2496 /* Issue an error message for a reference to an undeclared variable
2497 ID, including a reference to a builtin outside of function-call
2498 context. Establish a binding of the identifier to error_mark_node
2499 in an appropriate scope, which will suppress further errors for the
2500 same identifier. The error message should be given location LOC. */
2502 undeclared_variable (tree id, location_t loc)
2504 static bool already = false;
2505 struct c_scope *scope;
2507 if (current_function_decl == 0)
2509 error ("%H%qE undeclared here (not in a function)", &loc, id);
2510 scope = current_scope;
2514 error ("%H%qE undeclared (first use in this function)", &loc, id);
2518 error ("%H(Each undeclared identifier is reported only once", &loc);
2519 error ("%Hfor each function it appears in.)", &loc);
2523 /* If we are parsing old-style parameter decls, current_function_decl
2524 will be nonnull but current_function_scope will be null. */
2525 scope = current_function_scope ? current_function_scope : current_scope;
2527 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2530 /* Subroutine of lookup_label, declare_label, define_label: construct a
2531 LABEL_DECL with all the proper frills. */
2534 make_label (tree name, location_t location)
2536 tree label = build_decl (LABEL_DECL, name, void_type_node);
2538 DECL_CONTEXT (label) = current_function_decl;
2539 DECL_MODE (label) = VOIDmode;
2540 DECL_SOURCE_LOCATION (label) = location;
2545 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2546 Create one if none exists so far for the current function.
2547 This is called when a label is used in a goto expression or
2548 has its address taken. */
2551 lookup_label (tree name)
2555 if (current_function_decl == 0)
2557 error ("label %qE referenced outside of any function", name);
2561 /* Use a label already defined or ref'd with this name, but not if
2562 it is inherited from a containing function and wasn't declared
2564 label = I_LABEL_DECL (name);
2565 if (label && (DECL_CONTEXT (label) == current_function_decl
2566 || C_DECLARED_LABEL_FLAG (label)))
2568 /* If the label has only been declared, update its apparent
2569 location to point here, for better diagnostics if it
2570 turns out not to have been defined. */
2571 if (!TREE_USED (label))
2572 DECL_SOURCE_LOCATION (label) = input_location;
2576 /* No label binding for that identifier; make one. */
2577 label = make_label (name, input_location);
2579 /* Ordinary labels go in the current function scope. */
2580 bind (name, label, current_function_scope,
2581 /*invisible=*/false, /*nested=*/false);
2585 /* Make a label named NAME in the current function, shadowing silently
2586 any that may be inherited from containing functions or containing
2587 scopes. This is called for __label__ declarations. */
2590 declare_label (tree name)
2592 struct c_binding *b = I_LABEL_BINDING (name);
2595 /* Check to make sure that the label hasn't already been declared
2597 if (b && B_IN_CURRENT_SCOPE (b))
2599 error ("duplicate label declaration %qE", name);
2600 locate_old_decl (b->decl, error);
2602 /* Just use the previous declaration. */
2606 label = make_label (name, input_location);
2607 C_DECLARED_LABEL_FLAG (label) = 1;
2609 /* Declared labels go in the current scope. */
2610 bind (name, label, current_scope,
2611 /*invisible=*/false, /*nested=*/false);
2615 /* Define a label, specifying the location in the source file.
2616 Return the LABEL_DECL node for the label, if the definition is valid.
2617 Otherwise return 0. */
2620 define_label (location_t location, tree name)
2622 /* Find any preexisting label with this name. It is an error
2623 if that label has already been defined in this function, or
2624 if there is a containing function with a declared label with
2626 tree label = I_LABEL_DECL (name);
2627 struct c_label_list *nlist_se, *nlist_vm;
2630 && ((DECL_CONTEXT (label) == current_function_decl
2631 && DECL_INITIAL (label) != 0)
2632 || (DECL_CONTEXT (label) != current_function_decl
2633 && C_DECLARED_LABEL_FLAG (label))))
2635 error ("%Hduplicate label %qD", &location, label);
2636 locate_old_decl (label, error);
2639 else if (label && DECL_CONTEXT (label) == current_function_decl)
2641 /* The label has been used or declared already in this function,
2642 but not defined. Update its location to point to this
2644 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2645 error ("%Jjump into statement expression", label);
2646 if (C_DECL_UNDEFINABLE_VM (label))
2647 error ("%Jjump into scope of identifier with variably modified type",
2649 DECL_SOURCE_LOCATION (label) = location;
2653 /* No label binding for that identifier; make one. */
2654 label = make_label (name, location);
2656 /* Ordinary labels go in the current function scope. */
2657 bind (name, label, current_function_scope,
2658 /*invisible=*/false, /*nested=*/false);
2661 if (!in_system_header && lookup_name (name))
2662 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2663 "for labels, identifier %qE conflicts", &location, name);
2665 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2666 nlist_se->next = label_context_stack_se->labels_def;
2667 nlist_se->label = label;
2668 label_context_stack_se->labels_def = nlist_se;
2670 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2671 nlist_vm->next = label_context_stack_vm->labels_def;
2672 nlist_vm->label = label;
2673 label_context_stack_vm->labels_def = nlist_vm;
2675 /* Mark label as having been defined. */
2676 DECL_INITIAL (label) = error_mark_node;
2680 /* Given NAME, an IDENTIFIER_NODE,
2681 return the structure (or union or enum) definition for that name.
2682 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2683 CODE says which kind of type the caller wants;
2684 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2685 If the wrong kind of type is found, an error is reported. */
2688 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2690 struct c_binding *b = I_TAG_BINDING (name);
2696 /* We only care about whether it's in this level if
2697 thislevel_only was set or it might be a type clash. */
2698 if (thislevel_only || TREE_CODE (b->decl) != code)
2700 /* For our purposes, a tag in the external scope is the same as
2701 a tag in the file scope. (Primarily relevant to Objective-C
2702 and its builtin structure tags, which get pushed before the
2703 file scope is created.) */
2704 if (B_IN_CURRENT_SCOPE (b)
2705 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2709 if (thislevel_only && !thislevel)
2712 if (TREE_CODE (b->decl) != code)
2714 /* Definition isn't the kind we were looking for. */
2715 pending_invalid_xref = name;
2716 pending_invalid_xref_location = input_location;
2718 /* If in the same binding level as a declaration as a tag
2719 of a different type, this must not be allowed to
2720 shadow that tag, so give the error immediately.
2721 (For example, "struct foo; union foo;" is invalid.) */
2723 pending_xref_error ();
2728 /* Print an error message now
2729 for a recent invalid struct, union or enum cross reference.
2730 We don't print them immediately because they are not invalid
2731 when used in the `struct foo;' construct for shadowing. */
2734 pending_xref_error (void)
2736 if (pending_invalid_xref != 0)
2737 error ("%H%qE defined as wrong kind of tag",
2738 &pending_invalid_xref_location, pending_invalid_xref);
2739 pending_invalid_xref = 0;
2743 /* Look up NAME in the current scope and its superiors
2744 in the namespace of variables, functions and typedefs.
2745 Return a ..._DECL node of some kind representing its definition,
2746 or return 0 if it is undefined. */
2749 lookup_name (tree name)
2751 struct c_binding *b = I_SYMBOL_BINDING (name);
2752 if (b && !b->invisible)
2757 /* Similar to `lookup_name' but look only at the indicated scope. */
2760 lookup_name_in_scope (tree name, struct c_scope *scope)
2762 struct c_binding *b;
2764 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2765 if (B_IN_SCOPE (b, scope))
2770 /* Create the predefined scalar types of C,
2771 and some nodes representing standard constants (0, 1, (void *) 0).
2772 Initialize the global scope.
2773 Make definitions for built-in primitive functions. */
2776 c_init_decl_processing (void)
2778 location_t save_loc = input_location;
2780 /* Initialize reserved words for parser. */
2783 current_function_decl = 0;
2785 gcc_obstack_init (&parser_obstack);
2787 /* Make the externals scope. */
2789 external_scope = current_scope;
2791 /* Declarations from c_common_nodes_and_builtins must not be associated
2792 with this input file, lest we get differences between using and not
2793 using preprocessed headers. */
2794 #ifdef USE_MAPPED_LOCATION
2795 input_location = BUILTINS_LOCATION;
2797 input_location.file = "<built-in>";
2798 input_location.line = 0;
2801 build_common_tree_nodes (flag_signed_char, false);
2803 c_common_nodes_and_builtins ();
2805 /* In C, comparisons and TRUTH_* expressions have type int. */
2806 truthvalue_type_node = integer_type_node;
2807 truthvalue_true_node = integer_one_node;
2808 truthvalue_false_node = integer_zero_node;
2810 /* Even in C99, which has a real boolean type. */
2811 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2812 boolean_type_node));
2814 input_location = save_loc;
2816 pedantic_lvalues = true;
2818 make_fname_decl = c_make_fname_decl;
2819 start_fname_decls ();
2822 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2823 decl, NAME is the initialization string and TYPE_DEP indicates whether
2824 NAME depended on the type of the function. As we don't yet implement
2825 delayed emission of static data, we mark the decl as emitted
2826 so it is not placed in the output. Anything using it must therefore pull
2827 out the STRING_CST initializer directly. FIXME. */
2830 c_make_fname_decl (tree id, int type_dep)
2832 const char *name = fname_as_string (type_dep);
2833 tree decl, type, init;
2834 size_t length = strlen (name);
2836 type = build_array_type (char_type_node,
2837 build_index_type (size_int (length)));
2838 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2840 decl = build_decl (VAR_DECL, id, type);
2842 TREE_STATIC (decl) = 1;
2843 TREE_READONLY (decl) = 1;
2844 DECL_ARTIFICIAL (decl) = 1;
2846 init = build_string (length + 1, name);
2847 free ((char *) name);
2848 TREE_TYPE (init) = type;
2849 DECL_INITIAL (decl) = init;
2851 TREE_USED (decl) = 1;
2853 if (current_function_decl
2854 /* For invalid programs like this:
2857 const char* p = __FUNCTION__;
2859 the __FUNCTION__ is believed to appear in K&R style function
2860 parameter declarator. In that case we still don't have
2862 && (!errorcount || current_function_scope))
2864 DECL_CONTEXT (decl) = current_function_decl;
2865 bind (id, decl, current_function_scope,
2866 /*invisible=*/false, /*nested=*/false);
2869 finish_decl (decl, init, NULL_TREE);
2875 c_builtin_function (tree decl)
2877 tree type = TREE_TYPE (decl);
2878 tree id = DECL_NAME (decl);
2880 const char *name = IDENTIFIER_POINTER (id);
2881 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2882 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2884 /* Should never be called on a symbol with a preexisting meaning. */
2885 gcc_assert (!I_SYMBOL_BINDING (id));
2887 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2889 /* Builtins in the implementation namespace are made visible without
2890 needing to be explicitly declared. See push_file_scope. */
2891 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2893 TREE_CHAIN (decl) = visible_builtins;
2894 visible_builtins = decl;
2900 /* Called when a declaration is seen that contains no names to declare.
2901 If its type is a reference to a structure, union or enum inherited
2902 from a containing scope, shadow that tag name for the current scope
2903 with a forward reference.
2904 If its type defines a new named structure or union
2905 or defines an enum, it is valid but we need not do anything here.
2906 Otherwise, it is an error. */
2909 shadow_tag (const struct c_declspecs *declspecs)
2911 shadow_tag_warned (declspecs, 0);
2914 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2917 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2919 bool found_tag = false;
2921 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2923 tree value = declspecs->type;
2924 enum tree_code code = TREE_CODE (value);
2926 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2927 /* Used to test also that TYPE_SIZE (value) != 0.
2928 That caused warning for `struct foo;' at top level in the file. */
2930 tree name = TYPE_NAME (value);
2937 if (warned != 1 && code != ENUMERAL_TYPE)
2938 /* Empty unnamed enum OK */
2940 pedwarn ("unnamed struct/union that defines no instances");
2944 else if (!declspecs->tag_defined_p
2945 && declspecs->storage_class != csc_none)
2948 pedwarn ("empty declaration with storage class specifier "
2949 "does not redeclare tag");
2951 pending_xref_error ();
2953 else if (!declspecs->tag_defined_p
2954 && (declspecs->const_p
2955 || declspecs->volatile_p
2956 || declspecs->restrict_p))
2959 pedwarn ("empty declaration with type qualifier "
2960 "does not redeclare tag");
2962 pending_xref_error ();
2966 pending_invalid_xref = 0;
2967 t = lookup_tag (code, name, 1);
2971 t = make_node (code);
2978 if (warned != 1 && !in_system_header)
2980 pedwarn ("useless type name in empty declaration");
2985 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2987 pedwarn ("useless type name in empty declaration");
2991 pending_invalid_xref = 0;
2993 if (declspecs->inline_p)
2995 error ("%<inline%> in empty declaration");
2999 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3001 error ("%<auto%> in file-scope empty declaration");
3005 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3007 error ("%<register%> in file-scope empty declaration");
3011 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3013 warning (0, "useless storage class specifier in empty declaration");
3017 if (!warned && !in_system_header && declspecs->thread_p)
3019 warning (0, "useless %<__thread%> in empty declaration");
3023 if (!warned && !in_system_header && (declspecs->const_p
3024 || declspecs->volatile_p
3025 || declspecs->restrict_p))
3027 warning (0, "useless type qualifier in empty declaration");
3034 pedwarn ("empty declaration");
3039 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3040 bits. SPECS represents declaration specifiers that the grammar
3041 only permits to contain type qualifiers and attributes. */
3044 quals_from_declspecs (const struct c_declspecs *specs)
3046 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3047 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3048 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3049 gcc_assert (!specs->type
3050 && !specs->decl_attr
3051 && specs->typespec_word == cts_none
3052 && specs->storage_class == csc_none
3053 && !specs->typedef_p
3054 && !specs->explicit_signed_p
3055 && !specs->deprecated_p
3057 && !specs->long_long_p
3060 && !specs->unsigned_p
3061 && !specs->complex_p
3063 && !specs->thread_p);
3067 /* Construct an array declarator. EXPR is the expression inside [],
3068 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3069 applied to the pointer to which a parameter array is converted).
3070 STATIC_P is true if "static" is inside the [], false otherwise.
3071 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3072 length which is nevertheless a complete type, false otherwise. The
3073 field for the contained declarator is left to be filled in by
3074 set_array_declarator_inner. */
3076 struct c_declarator *
3077 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3080 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3081 struct c_declarator);
3082 declarator->kind = cdk_array;
3083 declarator->declarator = 0;
3084 declarator->u.array.dimen = expr;
3087 declarator->u.array.attrs = quals->attrs;
3088 declarator->u.array.quals = quals_from_declspecs (quals);
3092 declarator->u.array.attrs = NULL_TREE;
3093 declarator->u.array.quals = 0;
3095 declarator->u.array.static_p = static_p;
3096 declarator->u.array.vla_unspec_p = vla_unspec_p;
3097 if (pedantic && !flag_isoc99)
3099 if (static_p || quals != NULL)
3100 pedwarn ("ISO C90 does not support %<static%> or type "
3101 "qualifiers in parameter array declarators");
3103 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3107 if (!current_scope->parm_flag)
3110 error ("%<[*]%> not allowed in other than function prototype scope");
3111 declarator->u.array.vla_unspec_p = false;
3114 current_scope->had_vla_unspec = true;
3119 /* Set the contained declarator of an array declarator. DECL is the
3120 declarator, as constructed by build_array_declarator; INNER is what
3121 appears on the left of the []. ABSTRACT_P is true if it is an
3122 abstract declarator, false otherwise; this is used to reject static
3123 and type qualifiers in abstract declarators, where they are not in
3124 the C99 grammar (subject to possible change in DR#289). */
3126 struct c_declarator *
3127 set_array_declarator_inner (struct c_declarator *decl,
3128 struct c_declarator *inner, bool abstract_p)
3130 decl->declarator = inner;
3131 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3132 || decl->u.array.attrs != NULL_TREE
3133 || decl->u.array.static_p))
3134 error ("static or type qualifiers in abstract declarator");
3138 /* INIT is a constructor that forms DECL's initializer. If the final
3139 element initializes a flexible array field, add the size of that
3140 initializer to DECL's size. */
3143 add_flexible_array_elts_to_size (tree decl, tree init)
3147 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3150 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3151 type = TREE_TYPE (elt);
3152 if (TREE_CODE (type) == ARRAY_TYPE
3153 && TYPE_SIZE (type) == NULL_TREE
3154 && TYPE_DOMAIN (type) != NULL_TREE
3155 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3157 complete_array_type (&type, elt, false);
3159 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3160 DECL_SIZE_UNIT (decl)
3161 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3165 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3168 groktypename (struct c_type_name *type_name)
3171 tree attrs = type_name->specs->attrs;
3173 type_name->specs->attrs = NULL_TREE;
3175 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3178 /* Apply attributes. */
3179 decl_attributes (&type, attrs, 0);
3184 /* Decode a declarator in an ordinary declaration or data definition.
3185 This is called as soon as the type information and variable name
3186 have been parsed, before parsing the initializer if any.
3187 Here we create the ..._DECL node, fill in its type,
3188 and put it on the list of decls for the current context.
3189 The ..._DECL node is returned as the value.
3191 Exception: for arrays where the length is not specified,
3192 the type is left null, to be filled in by `finish_decl'.
3194 Function definitions do not come here; they go to start_function
3195 instead. However, external and forward declarations of functions
3196 do go through here. Structure field declarations are done by
3197 grokfield and not through here. */
3200 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3201 bool initialized, tree attributes)
3206 /* An object declared as __attribute__((deprecated)) suppresses
3207 warnings of uses of other deprecated items. */
3208 if (lookup_attribute ("deprecated", attributes))
3209 deprecated_state = DEPRECATED_SUPPRESS;
3211 decl = grokdeclarator (declarator, declspecs,
3212 NORMAL, initialized, NULL);
3216 deprecated_state = DEPRECATED_NORMAL;
3218 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3219 && MAIN_NAME_P (DECL_NAME (decl)))
3220 warning (OPT_Wmain, "%q+D is usually a function", decl);
3223 /* Is it valid for this decl to have an initializer at all?
3224 If not, set INITIALIZED to zero, which will indirectly
3225 tell 'finish_decl' to ignore the initializer once it is parsed. */
3226 switch (TREE_CODE (decl))
3229 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3234 error ("function %qD is initialized like a variable", decl);
3239 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3240 error ("parameter %qD is initialized", decl);
3245 /* Don't allow initializations for incomplete types except for
3246 arrays which might be completed by the initialization. */
3248 /* This can happen if the array size is an undefined macro.
3249 We already gave a warning, so we don't need another one. */
3250 if (TREE_TYPE (decl) == error_mark_node)
3252 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3254 /* A complete type is ok if size is fixed. */
3256 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3257 || C_DECL_VARIABLE_SIZE (decl))
3259 error ("variable-sized object may not be initialized");
3263 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3265 error ("variable %qD has initializer but incomplete type", decl);
3268 else if (C_DECL_VARIABLE_SIZE (decl))
3270 /* Although C99 is unclear about whether incomplete arrays
3271 of VLAs themselves count as VLAs, it does not make
3272 sense to permit them to be initialized given that
3273 ordinary VLAs may not be initialized. */
3274 error ("variable-sized object may not be initialized");
3281 if (current_scope == file_scope)
3282 TREE_STATIC (decl) = 1;
3284 /* Tell 'pushdecl' this is an initialized decl
3285 even though we don't yet have the initializer expression.
3286 Also tell 'finish_decl' it may store the real initializer. */
3287 DECL_INITIAL (decl) = error_mark_node;
3290 /* If this is a function declaration, write a record describing it to the
3291 prototypes file (if requested). */
3293 if (TREE_CODE (decl) == FUNCTION_DECL)
3294 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3296 /* ANSI specifies that a tentative definition which is not merged with
3297 a non-tentative definition behaves exactly like a definition with an
3298 initializer equal to zero. (Section 3.7.2)
3300 -fno-common gives strict ANSI behavior, though this tends to break
3301 a large body of code that grew up without this rule.
3303 Thread-local variables are never common, since there's no entrenched
3304 body of code to break, and it allows more efficient variable references
3305 in the presence of dynamic linking. */
3307 if (TREE_CODE (decl) == VAR_DECL
3309 && TREE_PUBLIC (decl)
3310 && !DECL_THREAD_LOCAL_P (decl)
3312 DECL_COMMON (decl) = 1;
3314 /* Set attributes here so if duplicate decl, will have proper attributes. */
3315 decl_attributes (&decl, attributes, 0);
3317 #if WANT_C99_INLINE_SEMANTICS
3318 /* Handle gnu_inline attribute. */
3319 if (declspecs->inline_p
3321 && TREE_CODE (decl) == FUNCTION_DECL
3322 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
3324 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3326 else if (declspecs->storage_class != csc_static)
3327 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3329 #endif /* WANT_C99_INLINE_SEMANTICS */
3331 if (TREE_CODE (decl) == FUNCTION_DECL
3332 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3334 struct c_declarator *ce = declarator;
3336 if (ce->kind == cdk_pointer)
3337 ce = declarator->declarator;
3338 if (ce->kind == cdk_function)
3340 tree args = ce->u.arg_info->parms;
3341 for (; args; args = TREE_CHAIN (args))
3343 tree type = TREE_TYPE (args);
3344 if (type && INTEGRAL_TYPE_P (type)
3345 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3346 DECL_ARG_TYPE (args) = integer_type_node;
3351 if (TREE_CODE (decl) == FUNCTION_DECL
3352 && DECL_DECLARED_INLINE_P (decl)
3353 && DECL_UNINLINABLE (decl)
3354 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3355 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3358 /* C99 6.7.4p3: An inline definition of a function with external
3359 linkage shall not contain a definition of a modifiable object
3360 with static storage duration... */
3361 if (TREE_CODE (decl) == VAR_DECL
3362 && current_scope != file_scope
3363 && TREE_STATIC (decl)
3364 && DECL_DECLARED_INLINE_P (current_function_decl)
3365 && DECL_EXTERNAL (current_function_decl))
3366 pedwarn ("%q+D is static but declared in inline function %qD "
3367 "which is not static", decl, current_function_decl);
3369 /* Add this decl to the current scope.
3370 TEM may equal DECL or it may be a previous decl of the same name. */
3371 tem = pushdecl (decl);
3373 if (initialized && DECL_EXTERNAL (tem))
3375 DECL_EXTERNAL (tem) = 0;
3376 TREE_STATIC (tem) = 1;
3382 /* Initialize EH if not initialized yet and exceptions are enabled. */
3385 c_maybe_initialize_eh (void)
3387 if (!flag_exceptions || c_eh_initialized_p)
3390 c_eh_initialized_p = true;
3391 eh_personality_libfunc
3392 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3393 ? "__gcc_personality_sj0"
3394 : "__gcc_personality_v0");
3395 default_init_unwind_resume_libfunc ();
3396 using_eh_for_cleanups ();
3399 /* Finish processing of a declaration;
3400 install its initial value.
3401 If the length of an array type is not known before,
3402 it must be determined now, from the initial value, or it is an error. */
3405 finish_decl (tree decl, tree init, tree asmspec_tree)
3407 tree type = TREE_TYPE (decl);
3408 int was_incomplete = (DECL_SIZE (decl) == 0);
3409 const char *asmspec = 0;
3411 /* If a name was specified, get the string. */
3412 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3413 && DECL_FILE_SCOPE_P (decl))
3414 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3416 asmspec = TREE_STRING_POINTER (asmspec_tree);
3418 /* If `start_decl' didn't like having an initialization, ignore it now. */
3419 if (init != 0 && DECL_INITIAL (decl) == 0)
3422 /* Don't crash if parm is initialized. */
3423 if (TREE_CODE (decl) == PARM_DECL)
3427 store_init_value (decl, init);
3429 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3430 || TREE_CODE (decl) == FUNCTION_DECL
3431 || TREE_CODE (decl) == FIELD_DECL))
3432 objc_check_decl (decl);
3434 /* Deduce size of array from initialization, if not already known. */
3435 if (TREE_CODE (type) == ARRAY_TYPE
3436 && TYPE_DOMAIN (type) == 0
3437 && TREE_CODE (decl) != TYPE_DECL)
3440 = (TREE_STATIC (decl)
3441 /* Even if pedantic, an external linkage array
3442 may have incomplete type at first. */
3443 ? pedantic && !TREE_PUBLIC (decl)
3444 : !DECL_EXTERNAL (decl));
3446 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3449 /* Get the completed type made by complete_array_type. */
3450 type = TREE_TYPE (decl);
3455 error ("initializer fails to determine size of %q+D", decl);
3460 error ("array size missing in %q+D", decl);
3461 /* If a `static' var's size isn't known,
3462 make it extern as well as static, so it does not get
3464 If it is not `static', then do not mark extern;
3465 finish_incomplete_decl will give it a default size
3466 and it will get allocated. */
3467 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3468 DECL_EXTERNAL (decl) = 1;
3472 error ("zero or negative size array %q+D", decl);
3476 /* For global variables, update the copy of the type that
3477 exists in the binding. */
3478 if (TREE_PUBLIC (decl))
3480 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3481 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3482 b_ext = b_ext->shadowed;
3486 b_ext->type = composite_type (b_ext->type, type);
3497 if (DECL_INITIAL (decl))
3498 TREE_TYPE (DECL_INITIAL (decl)) = type;