1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
31 #include "coretypes.h"
36 #include "tree-inline.h"
48 #include "c-family/c-common.h"
49 #include "c-family/c-objc.h"
50 #include "c-family/c-pragma.h"
52 #include "langhooks.h"
53 #include "tree-mudflap.h"
54 #include "tree-iterator.h"
55 #include "diagnostic-core.h"
56 #include "tree-dump.h"
59 #include "langhooks-def.h"
60 #include "pointer-set.h"
62 #include "c-family/c-ada-spec.h"
64 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
66 { NORMAL, /* Ordinary declaration */
67 FUNCDEF, /* Function definition */
68 PARM, /* Declaration of parm before function body */
69 FIELD, /* Declaration inside struct or union */
70 TYPENAME}; /* Typename (inside cast or sizeof) */
72 /* States indicating how grokdeclarator() should handle declspecs marked
73 with __attribute__((deprecated)). An object declared as
74 __attribute__((deprecated)) suppresses warnings of uses of other
77 enum deprecated_states {
83 /* Nonzero if we have seen an invalid cross reference
84 to a struct, union, or enum, but not yet printed the message. */
85 tree pending_invalid_xref;
87 /* File and line to appear in the eventual error message. */
88 location_t pending_invalid_xref_location;
90 /* The file and line that the prototype came from if this is an
91 old-style definition; used for diagnostics in
92 store_parm_decls_oldstyle. */
94 static location_t current_function_prototype_locus;
96 /* Whether this prototype was built-in. */
98 static bool current_function_prototype_built_in;
100 /* The argument type information of this prototype. */
102 static tree current_function_prototype_arg_types;
104 /* The argument information structure for the function currently being
107 static struct c_arg_info *current_function_arg_info;
109 /* The obstack on which parser and related data structures, which are
110 not live beyond their top-level declaration or definition, are
112 struct obstack parser_obstack;
114 /* The current statement tree. */
116 static GTY(()) struct stmt_tree_s c_stmt_tree;
118 /* State saving variables. */
122 /* A list of decls to be made automatically visible in each file scope. */
123 static GTY(()) tree visible_builtins;
125 /* Set to 0 at beginning of a function definition, set to 1 if
126 a return statement that specifies a return value is seen. */
128 int current_function_returns_value;
130 /* Set to 0 at beginning of a function definition, set to 1 if
131 a return statement with no argument is seen. */
133 int current_function_returns_null;
135 /* Set to 0 at beginning of a function definition, set to 1 if
136 a call to a noreturn function is seen. */
138 int current_function_returns_abnormally;
140 /* Set to nonzero by `grokdeclarator' for a function
141 whose return type is defaulted, if warnings for this are desired. */
143 static int warn_about_return_type;
145 /* Nonzero when the current toplevel function contains a declaration
146 of a nested function which is never defined. */
148 static bool undef_nested_function;
151 /* Each c_binding structure describes one binding of an identifier to
152 a decl. All the decls in a scope - irrespective of namespace - are
153 chained together by the ->prev field, which (as the name implies)
154 runs in reverse order. All the decls in a given namespace bound to
155 a given identifier are chained by the ->shadowed field, which runs
156 from inner to outer scopes.
158 The ->decl field usually points to a DECL node, but there are two
159 exceptions. In the namespace of type tags, the bound entity is a
160 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
161 identifier is encountered, it is bound to error_mark_node to
162 suppress further errors about that identifier in the current
165 The ->u.type field stores the type of the declaration in this scope;
166 if NULL, the type is the type of the ->decl field. This is only of
167 relevance for objects with external or internal linkage which may
168 be redeclared in inner scopes, forming composite types that only
169 persist for the duration of those scopes. In the external scope,
170 this stores the composite of all the types declared for this
171 object, visible or not. The ->inner_comp field (used only at file
172 scope) stores whether an incomplete array type at file scope was
173 completed at an inner scope to an array size other than 1.
175 The ->u.label field is used for labels. It points to a structure
176 which stores additional information used for warnings.
178 The depth field is copied from the scope structure that holds this
179 decl. It is used to preserve the proper ordering of the ->shadowed
180 field (see bind()) and also for a handful of special-case checks.
181 Finally, the invisible bit is true for a decl which should be
182 ignored for purposes of normal name lookup, and the nested bit is
183 true for a decl that's been bound a second time in an inner scope;
184 in all such cases, the binding in the outer scope will have its
185 invisible bit true. */
187 struct GTY((chain_next ("%h.prev"))) c_binding {
188 union GTY(()) { /* first so GTY desc can use decl */
189 tree GTY((tag ("0"))) type; /* the type in this scope */
190 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
191 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
192 tree decl; /* the decl bound */
193 tree id; /* the identifier it's bound to */
194 struct c_binding *prev; /* the previous decl in this scope */
195 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
196 unsigned int depth : 28; /* depth of this scope */
197 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
198 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
199 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
200 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
201 location_t locus; /* location for nested bindings */
203 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
204 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
205 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
206 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
208 #define I_SYMBOL_BINDING(node) \
209 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
210 #define I_SYMBOL_DECL(node) \
211 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
213 #define I_TAG_BINDING(node) \
214 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
215 #define I_TAG_DECL(node) \
216 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
218 #define I_LABEL_BINDING(node) \
219 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
220 #define I_LABEL_DECL(node) \
221 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
223 /* Each C symbol points to three linked lists of c_binding structures.
224 These describe the values of the identifier in the three different
225 namespaces defined by the language. */
227 struct GTY(()) lang_identifier {
228 struct c_common_identifier common_id;
229 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
230 struct c_binding *tag_binding; /* struct/union/enum tags */
231 struct c_binding *label_binding; /* labels */
234 /* Validate c-lang.c's assumptions. */
235 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
236 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
238 /* The resulting tree type. */
240 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
241 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
243 union tree_node GTY ((tag ("0"),
244 desc ("tree_node_structure (&%h)")))
246 struct lang_identifier GTY ((tag ("1"))) identifier;
249 /* Track bindings and other things that matter for goto warnings. For
250 efficiency, we do not gather all the decls at the point of
251 definition. Instead, we point into the bindings structure. As
252 scopes are popped, we update these structures and gather the decls
253 that matter at that time. */
255 struct GTY(()) c_spot_bindings {
256 /* The currently open scope which holds bindings defined when the
257 label was defined or the goto statement was found. */
258 struct c_scope *scope;
259 /* The bindings in the scope field which were defined at the point
260 of the label or goto. This lets us look at older or newer
261 bindings in the scope, as appropriate. */
262 struct c_binding *bindings_in_scope;
263 /* The number of statement expressions that have started since this
264 label or goto statement was defined. This is zero if we are at
265 the same statement expression level. It is positive if we are in
266 a statement expression started since this spot. It is negative
267 if this spot was in a statement expression and we have left
270 /* Whether we started in a statement expression but are no longer in
271 it. This is set to true if stmt_exprs ever goes negative. */
275 /* This structure is used to keep track of bindings seen when a goto
276 statement is defined. This is only used if we see the goto
277 statement before we see the label. */
279 struct GTY(()) c_goto_bindings {
280 /* The location of the goto statement. */
282 /* The bindings of the goto statement. */
283 struct c_spot_bindings goto_bindings;
286 typedef struct c_goto_bindings *c_goto_bindings_p;
287 DEF_VEC_P(c_goto_bindings_p);
288 DEF_VEC_ALLOC_P(c_goto_bindings_p,gc);
290 /* The additional information we keep track of for a label binding.
291 These fields are updated as scopes are popped. */
293 struct GTY(()) c_label_vars {
294 /* The shadowed c_label_vars, when one label shadows another (which
295 can only happen using a __label__ declaration). */
296 struct c_label_vars *shadowed;
297 /* The bindings when the label was defined. */
298 struct c_spot_bindings label_bindings;
299 /* A list of decls that we care about: decls about which we should
300 warn if a goto branches to this label from later in the function.
301 Decls are added to this list as scopes are popped. We only add
302 the decls that matter. */
303 VEC(tree,gc) *decls_in_scope;
304 /* A list of goto statements to this label. This is only used for
305 goto statements seen before the label was defined, so that we can
306 issue appropriate warnings for them. */
307 VEC(c_goto_bindings_p,gc) *gotos;
310 /* Each c_scope structure describes the complete contents of one
311 scope. Four scopes are distinguished specially: the innermost or
312 current scope, the innermost function scope, the file scope (always
313 the second to outermost) and the outermost or external scope.
315 Most declarations are recorded in the current scope.
317 All normal label declarations are recorded in the innermost
318 function scope, as are bindings of undeclared identifiers to
319 error_mark_node. (GCC permits nested functions as an extension,
320 hence the 'innermost' qualifier.) Explicitly declared labels
321 (using the __label__ extension) appear in the current scope.
323 Being in the file scope (current_scope == file_scope) causes
324 special behavior in several places below. Also, under some
325 conditions the Objective-C front end records declarations in the
326 file scope even though that isn't the current scope.
328 All declarations with external linkage are recorded in the external
329 scope, even if they aren't visible there; this models the fact that
330 such declarations are visible to the entire program, and (with a
331 bit of cleverness, see pushdecl) allows diagnosis of some violations
332 of C99 6.2.2p7 and 6.2.7p2:
334 If, within the same translation unit, the same identifier appears
335 with both internal and external linkage, the behavior is
338 All declarations that refer to the same object or function shall
339 have compatible type; otherwise, the behavior is undefined.
341 Initially only the built-in declarations, which describe compiler
342 intrinsic functions plus a subset of the standard library, are in
345 The order of the blocks list matters, and it is frequently appended
346 to. To avoid having to walk all the way to the end of the list on
347 each insertion, or reverse the list later, we maintain a pointer to
348 the last list entry. (FIXME: It should be feasible to use a reversed
351 The bindings list is strictly in reverse order of declarations;
352 pop_scope relies on this. */
355 struct GTY((chain_next ("%h.outer"))) c_scope {
356 /* The scope containing this one. */
357 struct c_scope *outer;
359 /* The next outermost function scope. */
360 struct c_scope *outer_function;
362 /* All bindings in this scope. */
363 struct c_binding *bindings;
365 /* For each scope (except the global one), a chain of BLOCK nodes
366 for all the scopes that were entered and exited one level down. */
370 /* The depth of this scope. Used to keep the ->shadowed chain of
371 bindings sorted innermost to outermost. */
372 unsigned int depth : 28;
374 /* True if we are currently filling this scope with parameter
376 BOOL_BITFIELD parm_flag : 1;
378 /* True if we saw [*] in this scope. Used to give an error messages
379 if these appears in a function definition. */
380 BOOL_BITFIELD had_vla_unspec : 1;
382 /* True if we already complained about forward parameter decls
383 in this scope. This prevents double warnings on
384 foo (int a; int b; ...) */
385 BOOL_BITFIELD warned_forward_parm_decls : 1;
387 /* True if this is the outermost block scope of a function body.
388 This scope contains the parameters, the local variables declared
389 in the outermost block, and all the labels (except those in
390 nested functions, or declared at block scope with __label__). */
391 BOOL_BITFIELD function_body : 1;
393 /* True means make a BLOCK for this scope no matter what. */
394 BOOL_BITFIELD keep : 1;
396 /* True means that an unsuffixed float constant is _Decimal64. */
397 BOOL_BITFIELD float_const_decimal64 : 1;
399 /* True if this scope has any label bindings. This is used to speed
400 up searching for labels when popping scopes, particularly since
401 labels are normally only found at function scope. */
402 BOOL_BITFIELD has_label_bindings : 1;
404 /* True if we should issue a warning if a goto statement crosses any
405 of the bindings. We still need to check the list of bindings to
406 find the specific ones we need to warn about. This is true if
407 decl_jump_unsafe would return true for any of the bindings. This
408 is used to avoid looping over all the bindings unnecessarily. */
409 BOOL_BITFIELD has_jump_unsafe_decl : 1;
412 /* The scope currently in effect. */
414 static GTY(()) struct c_scope *current_scope;
416 /* The innermost function scope. Ordinary (not explicitly declared)
417 labels, bindings to error_mark_node, and the lazily-created
418 bindings of __func__ and its friends get this scope. */
420 static GTY(()) struct c_scope *current_function_scope;
422 /* The C file scope. This is reset for each input translation unit. */
424 static GTY(()) struct c_scope *file_scope;
426 /* The outermost scope. This is used for all declarations with
427 external linkage, and only these, hence the name. */
429 static GTY(()) struct c_scope *external_scope;
431 /* A chain of c_scope structures awaiting reuse. */
433 static GTY((deletable)) struct c_scope *scope_freelist;
435 /* A chain of c_binding structures awaiting reuse. */
437 static GTY((deletable)) struct c_binding *binding_freelist;
439 /* Append VAR to LIST in scope SCOPE. */
440 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
441 struct c_scope *s_ = (scope); \
443 if (s_->list##_last) \
444 BLOCK_CHAIN (s_->list##_last) = d_; \
447 s_->list##_last = d_; \
450 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
451 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
452 struct c_scope *t_ = (tscope); \
453 struct c_scope *f_ = (fscope); \
455 BLOCK_CHAIN (t_->to##_last) = f_->from; \
458 t_->to##_last = f_->from##_last; \
461 /* A c_inline_static structure stores details of a static identifier
462 referenced in a definition of a function that may be an inline
463 definition if no subsequent declaration of that function uses
464 "extern" or does not use "inline". */
466 struct GTY((chain_next ("%h.next"))) c_inline_static {
467 /* The location for a diagnostic. */
470 /* The function that may be an inline definition. */
473 /* The object or function referenced. */
476 /* What sort of reference this is. */
477 enum c_inline_static_type type;
479 /* The next such structure or NULL. */
480 struct c_inline_static *next;
483 /* List of static identifiers used or referenced in functions that may
484 be inline definitions. */
485 static GTY(()) struct c_inline_static *c_inline_statics;
487 /* True means unconditionally make a BLOCK for the next scope pushed. */
489 static bool keep_next_level_flag;
491 /* True means the next call to push_scope will be the outermost scope
492 of a function body, so do not push a new scope, merely cease
493 expecting parameter decls. */
495 static bool next_is_function_body;
497 /* A VEC of pointers to c_binding structures. */
499 typedef struct c_binding *c_binding_ptr;
500 DEF_VEC_P(c_binding_ptr);
501 DEF_VEC_ALLOC_P(c_binding_ptr,heap);
503 /* Information that we keep for a struct or union while it is being
506 struct c_struct_parse_info
508 /* If warn_cxx_compat, a list of types defined within this
510 VEC(tree,heap) *struct_types;
511 /* If warn_cxx_compat, a list of field names which have bindings,
512 and which are defined in this struct, but which are not defined
513 in any enclosing struct. This is used to clear the in_struct
514 field of the c_bindings structure. */
515 VEC(c_binding_ptr,heap) *fields;
516 /* If warn_cxx_compat, a list of typedef names used when defining
517 fields in this struct. */
518 VEC(tree,heap) *typedefs_seen;
521 /* Information for the struct or union currently being parsed, or
522 NULL if not parsing a struct or union. */
523 static struct c_struct_parse_info *struct_parse_info;
525 /* Forward declarations. */
526 static tree lookup_name_in_scope (tree, struct c_scope *);
527 static tree c_make_fname_decl (location_t, tree, int);
528 static tree grokdeclarator (const struct c_declarator *,
529 struct c_declspecs *,
530 enum decl_context, bool, tree *, tree *, tree *,
531 bool *, enum deprecated_states);
532 static tree grokparms (struct c_arg_info *, bool);
533 static void layout_array_type (tree);
535 /* T is a statement. Add it to the statement-tree. This is the
536 C/ObjC version--C++ has a slightly different version of this
542 enum tree_code code = TREE_CODE (t);
544 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
546 if (!EXPR_HAS_LOCATION (t))
547 SET_EXPR_LOCATION (t, input_location);
550 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
551 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
553 /* Add T to the statement-tree. Non-side-effect statements need to be
554 recorded during statement expressions. */
555 if (!building_stmt_list_p ())
557 append_to_statement_list_force (t, &cur_stmt_list);
562 /* Return true if we will want to say something if a goto statement
566 decl_jump_unsafe (tree decl)
568 if (error_operand_p (decl))
571 /* Always warn about crossing variably modified types. */
572 if ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == TYPE_DECL)
573 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
576 /* Otherwise, only warn if -Wgoto-misses-init and this is an
577 initialized automatic decl. */
578 if (warn_jump_misses_init
579 && TREE_CODE (decl) == VAR_DECL
580 && !TREE_STATIC (decl)
581 && DECL_INITIAL (decl) != NULL_TREE)
589 c_print_identifier (FILE *file, tree node, int indent)
591 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
592 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
593 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
594 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
596 tree rid = ridpointers[C_RID_CODE (node)];
597 indent_to (file, indent + 4);
598 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
599 (void *) rid, IDENTIFIER_POINTER (rid));
603 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
604 which may be any of several kinds of DECL or TYPE or error_mark_node,
605 in the scope SCOPE. */
607 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
608 bool nested, location_t locus)
610 struct c_binding *b, **here;
612 if (binding_freelist)
614 b = binding_freelist;
615 binding_freelist = b->prev;
618 b = ggc_alloc_c_binding ();
623 b->depth = scope->depth;
624 b->invisible = invisible;
632 b->prev = scope->bindings;
635 if (decl_jump_unsafe (decl))
636 scope->has_jump_unsafe_decl = 1;
641 switch (TREE_CODE (decl))
643 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
646 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
652 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
658 /* Locate the appropriate place in the chain of shadowed decls
659 to insert this binding. Normally, scope == current_scope and
660 this does nothing. */
661 while (*here && (*here)->depth > scope->depth)
662 here = &(*here)->shadowed;
668 /* Clear the binding structure B, stick it on the binding_freelist,
669 and return the former value of b->prev. This is used by pop_scope
670 and get_parm_info to iterate destructively over all the bindings
671 from a given scope. */
672 static struct c_binding *
673 free_binding_and_advance (struct c_binding *b)
675 struct c_binding *prev = b->prev;
677 memset (b, 0, sizeof (struct c_binding));
678 b->prev = binding_freelist;
679 binding_freelist = b;
684 /* Bind a label. Like bind, but skip fields which aren't used for
685 labels, and add the LABEL_VARS value. */
687 bind_label (tree name, tree label, struct c_scope *scope,
688 struct c_label_vars *label_vars)
692 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
695 scope->has_label_bindings = true;
698 gcc_assert (b->decl == label);
699 label_vars->shadowed = b->u.label;
700 b->u.label = label_vars;
703 /* Hook called at end of compilation to assume 1 elt
704 for a file-scope tentative array defn that wasn't complete before. */
707 c_finish_incomplete_decl (tree decl)
709 if (TREE_CODE (decl) == VAR_DECL)
711 tree type = TREE_TYPE (decl);
712 if (type != error_mark_node
713 && TREE_CODE (type) == ARRAY_TYPE
714 && !DECL_EXTERNAL (decl)
715 && TYPE_DOMAIN (type) == 0)
717 warning_at (DECL_SOURCE_LOCATION (decl),
718 0, "array %q+D assumed to have one element", decl);
720 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
722 layout_decl (decl, 0);
727 /* Record that inline function FUNC contains a reference (location
728 LOC) to static DECL (file-scope or function-local according to
732 record_inline_static (location_t loc, tree func, tree decl,
733 enum c_inline_static_type type)
735 struct c_inline_static *csi = ggc_alloc_c_inline_static ();
737 csi->function = func;
738 csi->static_decl = decl;
740 csi->next = c_inline_statics;
741 c_inline_statics = csi;
744 /* Check for references to static declarations in inline functions at
745 the end of the translation unit and diagnose them if the functions
746 are still inline definitions. */
749 check_inline_statics (void)
751 struct c_inline_static *csi;
752 for (csi = c_inline_statics; csi; csi = csi->next)
754 if (DECL_EXTERNAL (csi->function))
758 pedwarn (csi->location, 0,
759 "%qD is static but used in inline function %qD "
760 "which is not static", csi->static_decl, csi->function);
763 pedwarn (csi->location, 0,
764 "%q+D is static but declared in inline function %qD "
765 "which is not static", csi->static_decl, csi->function);
771 c_inline_statics = NULL;
774 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
775 for the current state, otherwise set it to uninitialized. */
778 set_spot_bindings (struct c_spot_bindings *p, bool defining)
782 p->scope = current_scope;
783 p->bindings_in_scope = current_scope->bindings;
788 p->bindings_in_scope = NULL;
791 p->left_stmt_expr = false;
794 /* Update spot bindings P as we pop out of SCOPE. Return true if we
795 should push decls for a label. */
798 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
800 if (p->scope != scope)
802 /* This label or goto is defined in some other scope, or it is a
803 label which is not yet defined. There is nothing to
808 /* Adjust the spot bindings to refer to the bindings already defined
809 in the enclosing scope. */
810 p->scope = scope->outer;
811 p->bindings_in_scope = p->scope->bindings;
816 /* The Objective-C front-end often needs to determine the current scope. */
819 objc_get_current_scope (void)
821 return current_scope;
824 /* The following function is used only by Objective-C. It needs to live here
825 because it accesses the innards of c_scope. */
828 objc_mark_locals_volatile (void *enclosing_blk)
830 struct c_scope *scope;
833 for (scope = current_scope;
834 scope && scope != enclosing_blk;
835 scope = scope->outer)
837 for (b = scope->bindings; b; b = b->prev)
838 objc_volatilize_decl (b->decl);
840 /* Do not climb up past the current function. */
841 if (scope->function_body)
846 /* Return true if we are in the global binding level. */
849 global_bindings_p (void)
851 return current_scope == file_scope;
855 keep_next_level (void)
857 keep_next_level_flag = true;
860 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
863 set_float_const_decimal64 (void)
865 current_scope->float_const_decimal64 = true;
868 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
871 clear_float_const_decimal64 (void)
873 current_scope->float_const_decimal64 = false;
876 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
879 float_const_decimal64_p (void)
881 return current_scope->float_const_decimal64;
884 /* Identify this scope as currently being filled with parameters. */
887 declare_parm_level (void)
889 current_scope->parm_flag = true;
895 if (next_is_function_body)
897 /* This is the transition from the parameters to the top level
898 of the function body. These are the same scope
899 (C99 6.2.1p4,6) so we do not push another scope structure.
900 next_is_function_body is set only by store_parm_decls, which
901 in turn is called when and only when we are about to
902 encounter the opening curly brace for the function body.
904 The outermost block of a function always gets a BLOCK node,
905 because the debugging output routines expect that each
906 function has at least one BLOCK. */
907 current_scope->parm_flag = false;
908 current_scope->function_body = true;
909 current_scope->keep = true;
910 current_scope->outer_function = current_function_scope;
911 current_function_scope = current_scope;
913 keep_next_level_flag = false;
914 next_is_function_body = false;
916 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
917 if (current_scope->outer)
918 current_scope->float_const_decimal64
919 = current_scope->outer->float_const_decimal64;
921 current_scope->float_const_decimal64 = false;
925 struct c_scope *scope;
928 scope = scope_freelist;
929 scope_freelist = scope->outer;
932 scope = ggc_alloc_cleared_c_scope ();
934 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
936 scope->float_const_decimal64 = current_scope->float_const_decimal64;
938 scope->float_const_decimal64 = false;
940 scope->keep = keep_next_level_flag;
941 scope->outer = current_scope;
942 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
944 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
946 if (current_scope && scope->depth == 0)
949 sorry ("GCC supports only %u nested scopes", scope->depth);
952 current_scope = scope;
953 keep_next_level_flag = false;
957 /* This is called when we are leaving SCOPE. For each label defined
958 in SCOPE, add any appropriate decls to its decls_in_scope fields.
959 These are the decls whose initialization will be skipped by a goto
960 later in the function. */
963 update_label_decls (struct c_scope *scope)
970 if (s->has_label_bindings)
974 for (b = s->bindings; b != NULL; b = b->prev)
976 struct c_label_vars *label_vars;
977 struct c_binding *b1;
980 struct c_goto_bindings *g;
982 if (TREE_CODE (b->decl) != LABEL_DECL)
984 label_vars = b->u.label;
986 b1 = label_vars->label_bindings.bindings_in_scope;
987 if (label_vars->label_bindings.scope == NULL)
990 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
991 if (update_spot_bindings (scope, &label_vars->label_bindings))
993 /* This label is defined in this scope. */
996 for (; b1 != NULL; b1 = b1->prev)
998 /* A goto from later in the function to this
999 label will never see the initialization
1000 of B1, if any. Save it to issue a
1001 warning if needed. */
1002 if (decl_jump_unsafe (b1->decl))
1003 VEC_safe_push (tree, gc,
1004 label_vars->decls_in_scope,
1010 /* Update the bindings of any goto statements associated
1012 FOR_EACH_VEC_ELT (c_goto_bindings_p, label_vars->gotos, ix, g)
1013 update_spot_bindings (scope, &g->goto_bindings);
1017 /* Don't search beyond the current function. */
1018 if (s == current_function_scope)
1025 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1028 set_type_context (tree type, tree context)
1030 for (type = TYPE_MAIN_VARIANT (type); type;
1031 type = TYPE_NEXT_VARIANT (type))
1032 TYPE_CONTEXT (type) = context;
1035 /* Exit a scope. Restore the state of the identifier-decl mappings
1036 that were in effect when this scope was entered. Return a BLOCK
1037 node containing all the DECLs in this scope that are of interest
1038 to debug info generation. */
1043 struct c_scope *scope = current_scope;
1044 tree block, context, p;
1045 struct c_binding *b;
1047 bool functionbody = scope->function_body;
1048 bool keep = functionbody || scope->keep || scope->bindings;
1050 update_label_decls (scope);
1052 /* If appropriate, create a BLOCK to record the decls for the life
1053 of this function. */
1057 block = make_node (BLOCK);
1058 BLOCK_SUBBLOCKS (block) = scope->blocks;
1059 TREE_USED (block) = 1;
1061 /* In each subblock, record that this is its superior. */
1062 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1063 BLOCK_SUPERCONTEXT (p) = block;
1065 BLOCK_VARS (block) = 0;
1068 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1069 scope must be set so that they point to the appropriate
1070 construct, i.e. either to the current FUNCTION_DECL node, or
1071 else to the BLOCK node we just constructed.
1073 Note that for tagged types whose scope is just the formal
1074 parameter list for some function type specification, we can't
1075 properly set their TYPE_CONTEXTs here, because we don't have a
1076 pointer to the appropriate FUNCTION_TYPE node readily available
1077 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1078 type nodes get set in `grokdeclarator' as soon as we have created
1079 the FUNCTION_TYPE node which will represent the "scope" for these
1080 "parameter list local" tagged types. */
1081 if (scope->function_body)
1082 context = current_function_decl;
1083 else if (scope == file_scope)
1085 tree file_decl = build_translation_unit_decl (NULL_TREE);
1086 context = file_decl;
1091 /* Clear all bindings in this scope. */
1092 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1095 switch (TREE_CODE (p))
1098 /* Warnings for unused labels, errors for undefined labels. */
1099 if (TREE_USED (p) && !DECL_INITIAL (p))
1101 error ("label %q+D used but not defined", p);
1102 DECL_INITIAL (p) = error_mark_node;
1105 warn_for_unused_label (p);
1107 /* Labels go in BLOCK_VARS. */
1108 DECL_CHAIN (p) = BLOCK_VARS (block);
1109 BLOCK_VARS (block) = p;
1110 gcc_assert (I_LABEL_BINDING (b->id) == b);
1111 I_LABEL_BINDING (b->id) = b->shadowed;
1113 /* Also pop back to the shadowed label_vars. */
1114 release_tree_vector (b->u.label->decls_in_scope);
1115 b->u.label = b->u.label->shadowed;
1121 set_type_context (p, context);
1123 /* Types may not have tag-names, in which case the type
1124 appears in the bindings list with b->id NULL. */
1127 gcc_assert (I_TAG_BINDING (b->id) == b);
1128 I_TAG_BINDING (b->id) = b->shadowed;
1133 /* Propagate TREE_ADDRESSABLE from nested functions to their
1134 containing functions. */
1135 if (!TREE_ASM_WRITTEN (p)
1136 && DECL_INITIAL (p) != 0
1137 && TREE_ADDRESSABLE (p)
1138 && DECL_ABSTRACT_ORIGIN (p) != 0
1139 && DECL_ABSTRACT_ORIGIN (p) != p)
1140 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1141 if (!DECL_EXTERNAL (p)
1142 && !DECL_INITIAL (p)
1143 && scope != file_scope
1144 && scope != external_scope)
1146 error ("nested function %q+D declared but never defined", p);
1147 undef_nested_function = true;
1149 else if (DECL_DECLARED_INLINE_P (p)
1151 && !DECL_INITIAL (p))
1153 /* C99 6.7.4p6: "a function with external linkage... declared
1154 with an inline function specifier ... shall also be defined
1155 in the same translation unit." */
1156 if (!flag_gnu89_inline)
1157 pedwarn (input_location, 0,
1158 "inline function %q+D declared but never defined", p);
1159 DECL_EXTERNAL (p) = 1;
1165 /* Warnings for unused variables. */
1166 if ((!TREE_USED (p) || !DECL_READ_P (p))
1167 && !TREE_NO_WARNING (p)
1168 && !DECL_IN_SYSTEM_HEADER (p)
1170 && !DECL_ARTIFICIAL (p)
1171 && scope != file_scope
1172 && scope != external_scope)
1175 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1176 else if (DECL_CONTEXT (p) == current_function_decl)
1177 warning_at (DECL_SOURCE_LOCATION (p),
1178 OPT_Wunused_but_set_variable,
1179 "variable %qD set but not used", p);
1184 error ("type of array %q+D completed incompatibly with"
1185 " implicit initialization", p);
1192 /* All of these go in BLOCK_VARS, but only if this is the
1193 binding in the home scope. */
1196 DECL_CHAIN (p) = BLOCK_VARS (block);
1197 BLOCK_VARS (block) = p;
1199 else if (VAR_OR_FUNCTION_DECL_P (p))
1201 /* For block local externs add a special
1202 DECL_EXTERNAL decl for debug info generation. */
1203 tree extp = copy_node (p);
1205 DECL_EXTERNAL (extp) = 1;
1206 TREE_STATIC (extp) = 0;
1207 TREE_PUBLIC (extp) = 1;
1208 DECL_INITIAL (extp) = NULL_TREE;
1209 DECL_LANG_SPECIFIC (extp) = NULL;
1210 DECL_CONTEXT (extp) = current_function_decl;
1211 if (TREE_CODE (p) == FUNCTION_DECL)
1213 DECL_RESULT (extp) = NULL_TREE;
1214 DECL_SAVED_TREE (extp) = NULL_TREE;
1215 DECL_STRUCT_FUNCTION (extp) = NULL;
1217 if (b->locus != UNKNOWN_LOCATION)
1218 DECL_SOURCE_LOCATION (extp) = b->locus;
1219 DECL_CHAIN (extp) = BLOCK_VARS (block);
1220 BLOCK_VARS (block) = extp;
1222 /* If this is the file scope set DECL_CONTEXT of each decl to
1223 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1225 if (scope == file_scope)
1227 DECL_CONTEXT (p) = context;
1228 if (TREE_CODE (p) == TYPE_DECL
1229 && TREE_TYPE (p) != error_mark_node)
1230 set_type_context (TREE_TYPE (p), context);
1234 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1235 already been put there by store_parm_decls. Unused-
1236 parameter warnings are handled by function.c.
1237 error_mark_node obviously does not go in BLOCK_VARS and
1238 does not get unused-variable warnings. */
1241 /* It is possible for a decl not to have a name. We get
1242 here with b->id NULL in this case. */
1245 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1246 I_SYMBOL_BINDING (b->id) = b->shadowed;
1247 if (b->shadowed && b->shadowed->u.type)
1248 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1258 /* Dispose of the block that we just made inside some higher level. */
1259 if ((scope->function_body || scope == file_scope) && context)
1261 DECL_INITIAL (context) = block;
1262 BLOCK_SUPERCONTEXT (block) = context;
1264 else if (scope->outer)
1267 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1268 /* If we did not make a block for the scope just exited, any
1269 blocks made for inner scopes must be carried forward so they
1270 will later become subblocks of something else. */
1271 else if (scope->blocks)
1272 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1275 /* Pop the current scope, and free the structure for reuse. */
1276 current_scope = scope->outer;
1277 if (scope->function_body)
1278 current_function_scope = scope->outer_function;
1280 memset (scope, 0, sizeof (struct c_scope));
1281 scope->outer = scope_freelist;
1282 scope_freelist = scope;
1288 push_file_scope (void)
1296 file_scope = current_scope;
1298 start_fname_decls ();
1300 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1301 bind (DECL_NAME (decl), decl, file_scope,
1302 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1306 pop_file_scope (void)
1308 /* In case there were missing closebraces, get us back to the global
1310 while (current_scope != file_scope)
1313 /* __FUNCTION__ is defined at file scope (""). This
1314 call may not be necessary as my tests indicate it
1315 still works without it. */
1316 finish_fname_decls ();
1318 check_inline_statics ();
1320 /* This is the point to write out a PCH if we're doing that.
1321 In that case we do not want to do anything else. */
1324 c_common_write_pch ();
1328 /* Pop off the file scope and close this translation unit. */
1332 maybe_apply_pending_pragma_weaks ();
1335 /* Adjust the bindings for the start of a statement expression. */
1338 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1340 struct c_scope *scope;
1342 for (scope = current_scope; scope != NULL; scope = scope->outer)
1344 struct c_binding *b;
1346 if (!scope->has_label_bindings)
1349 for (b = scope->bindings; b != NULL; b = b->prev)
1351 struct c_label_vars *label_vars;
1353 struct c_goto_bindings *g;
1355 if (TREE_CODE (b->decl) != LABEL_DECL)
1357 label_vars = b->u.label;
1358 ++label_vars->label_bindings.stmt_exprs;
1359 FOR_EACH_VEC_ELT (c_goto_bindings_p, label_vars->gotos, ix, g)
1360 ++g->goto_bindings.stmt_exprs;
1364 if (switch_bindings != NULL)
1365 ++switch_bindings->stmt_exprs;
1368 /* Adjust the bindings for the end of a statement expression. */
1371 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1373 struct c_scope *scope;
1375 for (scope = current_scope; scope != NULL; scope = scope->outer)
1377 struct c_binding *b;
1379 if (!scope->has_label_bindings)
1382 for (b = scope->bindings; b != NULL; b = b->prev)
1384 struct c_label_vars *label_vars;
1386 struct c_goto_bindings *g;
1388 if (TREE_CODE (b->decl) != LABEL_DECL)
1390 label_vars = b->u.label;
1391 --label_vars->label_bindings.stmt_exprs;
1392 if (label_vars->label_bindings.stmt_exprs < 0)
1394 label_vars->label_bindings.left_stmt_expr = true;
1395 label_vars->label_bindings.stmt_exprs = 0;
1397 FOR_EACH_VEC_ELT (c_goto_bindings_p, label_vars->gotos, ix, g)
1399 --g->goto_bindings.stmt_exprs;
1400 if (g->goto_bindings.stmt_exprs < 0)
1402 g->goto_bindings.left_stmt_expr = true;
1403 g->goto_bindings.stmt_exprs = 0;
1409 if (switch_bindings != NULL)
1411 --switch_bindings->stmt_exprs;
1412 gcc_assert (switch_bindings->stmt_exprs >= 0);
1416 /* Push a definition or a declaration of struct, union or enum tag "name".
1417 "type" should be the type node.
1418 We assume that the tag "name" is not already defined, and has a location
1421 Note that the definition may really be just a forward reference.
1422 In that case, the TYPE_SIZE will be zero. */
1425 pushtag (location_t loc, tree name, tree type)
1427 /* Record the identifier as the type's name if it has none. */
1428 if (name && !TYPE_NAME (type))
1429 TYPE_NAME (type) = name;
1430 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1432 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1433 tagged type we just added to the current scope. This fake
1434 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1435 to output a representation of a tagged type, and it also gives
1436 us a convenient place to record the "scope start" address for the
1439 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1440 TYPE_DECL, NULL_TREE, type));
1442 /* An approximation for now, so we can tell this is a function-scope tag.
1443 This will be updated in pop_scope. */
1444 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1446 if (warn_cxx_compat && name != NULL_TREE)
1448 struct c_binding *b = I_SYMBOL_BINDING (name);
1451 && b->decl != NULL_TREE
1452 && TREE_CODE (b->decl) == TYPE_DECL
1453 && (B_IN_CURRENT_SCOPE (b)
1454 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1455 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1456 != TYPE_MAIN_VARIANT (type)))
1458 warning_at (loc, OPT_Wc___compat,
1459 ("using %qD as both a typedef and a tag is "
1462 if (b->locus != UNKNOWN_LOCATION)
1463 inform (b->locus, "originally defined here");
1468 /* Subroutine of compare_decls. Allow harmless mismatches in return
1469 and argument types provided that the type modes match. This function
1470 return a unified type given a suitable match, and 0 otherwise. */
1473 match_builtin_function_types (tree newtype, tree oldtype)
1475 tree newrettype, oldrettype;
1476 tree newargs, oldargs;
1477 tree trytype, tryargs;
1479 /* Accept the return type of the new declaration if same modes. */
1480 oldrettype = TREE_TYPE (oldtype);
1481 newrettype = TREE_TYPE (newtype);
1483 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1486 oldargs = TYPE_ARG_TYPES (oldtype);
1487 newargs = TYPE_ARG_TYPES (newtype);
1490 while (oldargs || newargs)
1494 || !TREE_VALUE (oldargs)
1495 || !TREE_VALUE (newargs)
1496 || TYPE_MODE (TREE_VALUE (oldargs))
1497 != TYPE_MODE (TREE_VALUE (newargs)))
1500 oldargs = TREE_CHAIN (oldargs);
1501 newargs = TREE_CHAIN (newargs);
1504 trytype = build_function_type (newrettype, tryargs);
1505 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1508 /* Subroutine of diagnose_mismatched_decls. Check for function type
1509 mismatch involving an empty arglist vs a nonempty one and give clearer
1512 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1513 tree newtype, tree oldtype)
1517 if (TREE_CODE (olddecl) != FUNCTION_DECL
1518 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1519 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == 0)
1520 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == 0)))
1523 t = TYPE_ARG_TYPES (oldtype);
1525 t = TYPE_ARG_TYPES (newtype);
1526 for (; t; t = TREE_CHAIN (t))
1528 tree type = TREE_VALUE (t);
1530 if (TREE_CHAIN (t) == 0
1531 && TYPE_MAIN_VARIANT (type) != void_type_node)
1533 inform (input_location, "a parameter list with an ellipsis can%'t match "
1534 "an empty parameter name list declaration");
1538 if (c_type_promotes_to (type) != type)
1540 inform (input_location, "an argument type that has a default promotion can%'t match "
1541 "an empty parameter name list declaration");
1547 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1548 old-style function definition, NEWDECL is a prototype declaration.
1549 Diagnose inconsistencies in the argument list. Returns TRUE if
1550 the prototype is compatible, FALSE if not. */
1552 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1554 tree newargs, oldargs;
1557 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1559 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1560 newargs = TYPE_ARG_TYPES (newtype);
1565 tree oldargtype = TREE_VALUE (oldargs);
1566 tree newargtype = TREE_VALUE (newargs);
1568 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1571 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1572 newargtype = TYPE_MAIN_VARIANT (newargtype);
1574 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1577 /* Reaching the end of just one list means the two decls don't
1578 agree on the number of arguments. */
1579 if (END_OF_ARGLIST (oldargtype))
1581 error ("prototype for %q+D declares more arguments "
1582 "than previous old-style definition", newdecl);
1585 else if (END_OF_ARGLIST (newargtype))
1587 error ("prototype for %q+D declares fewer arguments "
1588 "than previous old-style definition", newdecl);
1592 /* Type for passing arg must be consistent with that declared
1594 else if (!comptypes (oldargtype, newargtype))
1596 error ("prototype for %q+D declares argument %d"
1597 " with incompatible type",
1602 oldargs = TREE_CHAIN (oldargs);
1603 newargs = TREE_CHAIN (newargs);
1607 /* If we get here, no errors were found, but do issue a warning
1608 for this poor-style construct. */
1609 warning (0, "prototype for %q+D follows non-prototype definition",
1612 #undef END_OF_ARGLIST
1615 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1616 first in a pair of mismatched declarations, using the diagnostic
1619 locate_old_decl (tree decl)
1621 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1623 else if (DECL_INITIAL (decl))
1624 inform (input_location, "previous definition of %q+D was here", decl);
1625 else if (C_DECL_IMPLICIT (decl))
1626 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1628 inform (input_location, "previous declaration of %q+D was here", decl);
1631 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1632 Returns true if the caller should proceed to merge the two, false
1633 if OLDDECL should simply be discarded. As a side effect, issues
1634 all necessary diagnostics for invalid or poor-style combinations.
1635 If it returns true, writes the types of NEWDECL and OLDDECL to
1636 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1637 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1640 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1641 tree *newtypep, tree *oldtypep)
1643 tree newtype, oldtype;
1644 bool pedwarned = false;
1645 bool warned = false;
1648 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1649 && DECL_EXTERNAL (DECL))
1651 /* If we have error_mark_node for either decl or type, just discard
1652 the previous decl - we're in an error cascade already. */
1653 if (olddecl == error_mark_node || newdecl == error_mark_node)
1655 *oldtypep = oldtype = TREE_TYPE (olddecl);
1656 *newtypep = newtype = TREE_TYPE (newdecl);
1657 if (oldtype == error_mark_node || newtype == error_mark_node)
1660 /* Two different categories of symbol altogether. This is an error
1661 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1662 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1664 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1665 && DECL_BUILT_IN (olddecl)
1666 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1668 error ("%q+D redeclared as different kind of symbol", newdecl);
1669 locate_old_decl (olddecl);
1671 else if (TREE_PUBLIC (newdecl))
1672 warning (0, "built-in function %q+D declared as non-function",
1675 warning (OPT_Wshadow, "declaration of %q+D shadows "
1676 "a built-in function", newdecl);
1680 /* Enumerators have no linkage, so may only be declared once in a
1682 if (TREE_CODE (olddecl) == CONST_DECL)
1684 error ("redeclaration of enumerator %q+D", newdecl);
1685 locate_old_decl (olddecl);
1689 if (!comptypes (oldtype, newtype))
1691 if (TREE_CODE (olddecl) == FUNCTION_DECL
1692 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1694 /* Accept harmless mismatch in function types.
1695 This is for the ffs and fprintf builtins. */
1696 tree trytype = match_builtin_function_types (newtype, oldtype);
1698 if (trytype && comptypes (newtype, trytype))
1699 *oldtypep = oldtype = trytype;
1702 /* If types don't match for a built-in, throw away the
1703 built-in. No point in calling locate_old_decl here, it
1704 won't print anything. */
1705 warning (0, "conflicting types for built-in function %q+D",
1710 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1711 && DECL_IS_BUILTIN (olddecl))
1713 /* A conflicting function declaration for a predeclared
1714 function that isn't actually built in. Objective C uses
1715 these. The new declaration silently overrides everything
1716 but the volatility (i.e. noreturn) indication. See also
1717 below. FIXME: Make Objective C use normal builtins. */
1718 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1721 /* Permit void foo (...) to match int foo (...) if the latter is
1722 the definition and implicit int was used. See
1723 c-torture/compile/920625-2.c. */
1724 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1725 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1726 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1727 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1729 pedwarned = pedwarn (input_location, 0,
1730 "conflicting types for %q+D", newdecl);
1731 /* Make sure we keep void as the return type. */
1732 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1733 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1735 /* Permit void foo (...) to match an earlier call to foo (...) with
1736 no declared type (thus, implicitly int). */
1737 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1738 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1739 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1740 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1742 pedwarned = pedwarn (input_location, 0,
1743 "conflicting types for %q+D", newdecl);
1744 /* Make sure we keep void as the return type. */
1745 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1749 int new_quals = TYPE_QUALS (newtype);
1750 int old_quals = TYPE_QUALS (oldtype);
1752 if (new_quals != old_quals)
1754 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1755 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1756 if (new_addr != old_addr)
1758 if (ADDR_SPACE_GENERIC_P (new_addr))
1759 error ("conflicting named address spaces (generic vs %s) "
1761 c_addr_space_name (old_addr), newdecl);
1762 else if (ADDR_SPACE_GENERIC_P (old_addr))
1763 error ("conflicting named address spaces (%s vs generic) "
1765 c_addr_space_name (new_addr), newdecl);
1767 error ("conflicting named address spaces (%s vs %s) "
1769 c_addr_space_name (new_addr),
1770 c_addr_space_name (old_addr),
1774 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1775 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1776 error ("conflicting type qualifiers for %q+D", newdecl);
1779 error ("conflicting types for %q+D", newdecl);
1780 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1781 locate_old_decl (olddecl);
1786 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1787 but silently ignore the redeclaration if either is in a system
1788 header. (Conflicting redeclarations were handled above.) This
1789 is allowed for C1X if the types are the same, not just
1791 if (TREE_CODE (newdecl) == TYPE_DECL)
1793 bool types_different = false;
1794 int comptypes_result;
1797 = comptypes_check_different_types (oldtype, newtype, &types_different);
1799 if (comptypes_result != 1 || types_different)
1801 error ("redefinition of typedef %q+D with different type", newdecl);
1802 locate_old_decl (olddecl);
1806 if (DECL_IN_SYSTEM_HEADER (newdecl)
1807 || DECL_IN_SYSTEM_HEADER (olddecl)
1808 || TREE_NO_WARNING (newdecl)
1809 || TREE_NO_WARNING (olddecl))
1810 return true; /* Allow OLDDECL to continue in use. */
1812 if (variably_modified_type_p (newtype, NULL))
1814 error ("redefinition of typedef %q+D with variably modified type",
1816 locate_old_decl (olddecl);
1818 else if (pedantic && !flag_isoc1x)
1820 pedwarn (input_location, OPT_pedantic,
1821 "redefinition of typedef %q+D", newdecl);
1822 locate_old_decl (olddecl);
1828 /* Function declarations can either be 'static' or 'extern' (no
1829 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1830 can never conflict with each other on account of linkage
1831 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1832 gnu89 mode permits two definitions if one is 'extern inline' and
1833 one is not. The non- extern-inline definition supersedes the
1834 extern-inline definition. */
1836 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1838 /* If you declare a built-in function name as static, or
1839 define the built-in with an old-style definition (so we
1840 can't validate the argument list) the built-in definition is
1841 overridden, but optionally warn this was a bad choice of name. */
1842 if (DECL_BUILT_IN (olddecl)
1843 && !C_DECL_DECLARED_BUILTIN (olddecl)
1844 && (!TREE_PUBLIC (newdecl)
1845 || (DECL_INITIAL (newdecl)
1846 && !prototype_p (TREE_TYPE (newdecl)))))
1848 warning (OPT_Wshadow, "declaration of %q+D shadows "
1849 "a built-in function", newdecl);
1850 /* Discard the old built-in function. */
1854 if (DECL_INITIAL (newdecl))
1856 if (DECL_INITIAL (olddecl))
1858 /* If both decls are in the same TU and the new declaration
1859 isn't overriding an extern inline reject the new decl.
1860 In c99, no overriding is allowed in the same translation
1862 if ((!DECL_EXTERN_INLINE (olddecl)
1863 || DECL_EXTERN_INLINE (newdecl)
1864 || (!flag_gnu89_inline
1865 && (!DECL_DECLARED_INLINE_P (olddecl)
1866 || !lookup_attribute ("gnu_inline",
1867 DECL_ATTRIBUTES (olddecl)))
1868 && (!DECL_DECLARED_INLINE_P (newdecl)
1869 || !lookup_attribute ("gnu_inline",
1870 DECL_ATTRIBUTES (newdecl))))
1872 && same_translation_unit_p (newdecl, olddecl))
1874 error ("redefinition of %q+D", newdecl);
1875 locate_old_decl (olddecl);
1880 /* If we have a prototype after an old-style function definition,
1881 the argument types must be checked specially. */
1882 else if (DECL_INITIAL (olddecl)
1883 && !prototype_p (oldtype) && prototype_p (newtype)
1884 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1885 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1887 locate_old_decl (olddecl);
1890 /* A non-static declaration (even an "extern") followed by a
1891 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1892 The same is true for a static forward declaration at block
1893 scope followed by a non-static declaration/definition at file
1894 scope. Static followed by non-static at the same scope is
1895 not undefined behavior, and is the most convenient way to get
1896 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1897 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1898 we do diagnose it if -Wtraditional. */
1899 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1901 /* Two exceptions to the rule. If olddecl is an extern
1902 inline, or a predeclared function that isn't actually
1903 built in, newdecl silently overrides olddecl. The latter
1904 occur only in Objective C; see also above. (FIXME: Make
1905 Objective C use normal builtins.) */
1906 if (!DECL_IS_BUILTIN (olddecl)
1907 && !DECL_EXTERN_INLINE (olddecl))
1909 error ("static declaration of %q+D follows "
1910 "non-static declaration", newdecl);
1911 locate_old_decl (olddecl);
1915 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1917 if (DECL_CONTEXT (olddecl))
1919 error ("non-static declaration of %q+D follows "
1920 "static declaration", newdecl);
1921 locate_old_decl (olddecl);
1924 else if (warn_traditional)
1926 warned |= warning (OPT_Wtraditional,
1927 "non-static declaration of %q+D "
1928 "follows static declaration", newdecl);
1932 /* Make sure gnu_inline attribute is either not present, or
1933 present on all inline decls. */
1934 if (DECL_DECLARED_INLINE_P (olddecl)
1935 && DECL_DECLARED_INLINE_P (newdecl))
1937 bool newa = lookup_attribute ("gnu_inline",
1938 DECL_ATTRIBUTES (newdecl)) != NULL;
1939 bool olda = lookup_attribute ("gnu_inline",
1940 DECL_ATTRIBUTES (olddecl)) != NULL;
1943 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
1944 newa ? newdecl : olddecl);
1945 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
1950 else if (TREE_CODE (newdecl) == VAR_DECL)
1952 /* Only variables can be thread-local, and all declarations must
1953 agree on this property. */
1954 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1956 /* Nothing to check. Since OLDDECL is marked threadprivate
1957 and NEWDECL does not have a thread-local attribute, we
1958 will merge the threadprivate attribute into NEWDECL. */
1961 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1963 if (DECL_THREAD_LOCAL_P (newdecl))
1964 error ("thread-local declaration of %q+D follows "
1965 "non-thread-local declaration", newdecl);
1967 error ("non-thread-local declaration of %q+D follows "
1968 "thread-local declaration", newdecl);
1970 locate_old_decl (olddecl);
1974 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1975 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1977 error ("redefinition of %q+D", newdecl);
1978 locate_old_decl (olddecl);
1982 /* Objects declared at file scope: if the first declaration had
1983 external linkage (even if it was an external reference) the
1984 second must have external linkage as well, or the behavior is
1985 undefined. If the first declaration had internal linkage, then
1986 the second must too, or else be an external reference (in which
1987 case the composite declaration still has internal linkage).
1988 As for function declarations, we warn about the static-then-
1989 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1990 if (DECL_FILE_SCOPE_P (newdecl)
1991 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1993 if (DECL_EXTERNAL (newdecl))
1995 if (!DECL_FILE_SCOPE_P (olddecl))
1997 error ("extern declaration of %q+D follows "
1998 "declaration with no linkage", newdecl);
1999 locate_old_decl (olddecl);
2002 else if (warn_traditional)
2004 warned |= warning (OPT_Wtraditional,
2005 "non-static declaration of %q+D "
2006 "follows static declaration", newdecl);
2011 if (TREE_PUBLIC (newdecl))
2012 error ("non-static declaration of %q+D follows "
2013 "static declaration", newdecl);
2015 error ("static declaration of %q+D follows "
2016 "non-static declaration", newdecl);
2018 locate_old_decl (olddecl);
2022 /* Two objects with the same name declared at the same block
2023 scope must both be external references (6.7p3). */
2024 else if (!DECL_FILE_SCOPE_P (newdecl))
2026 if (DECL_EXTERNAL (newdecl))
2028 /* Extern with initializer at block scope, which will
2029 already have received an error. */
2031 else if (DECL_EXTERNAL (olddecl))
2033 error ("declaration of %q+D with no linkage follows "
2034 "extern declaration", newdecl);
2035 locate_old_decl (olddecl);
2039 error ("redeclaration of %q+D with no linkage", newdecl);
2040 locate_old_decl (olddecl);
2046 /* C++ does not permit a decl to appear multiple times at file
2049 && DECL_FILE_SCOPE_P (newdecl)
2050 && !DECL_EXTERNAL (newdecl)
2051 && !DECL_EXTERNAL (olddecl))
2052 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2054 ("duplicate declaration of %qD is "
2060 /* All decls must agree on a visibility. */
2061 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2062 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2063 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2065 warned |= warning (0, "redeclaration of %q+D with different visibility "
2066 "(old visibility preserved)", newdecl);
2069 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2071 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2072 if (DECL_DECLARED_INLINE_P (newdecl)
2073 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2075 warned |= warning (OPT_Wattributes,
2076 "inline declaration of %qD follows "
2077 "declaration with attribute noinline", newdecl);
2079 else if (DECL_DECLARED_INLINE_P (olddecl)
2080 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2082 warned |= warning (OPT_Wattributes,
2083 "declaration of %q+D with attribute "
2084 "noinline follows inline declaration ", newdecl);
2087 else /* PARM_DECL, VAR_DECL */
2089 /* Redeclaration of a parameter is a constraint violation (this is
2090 not explicitly stated, but follows from C99 6.7p3 [no more than
2091 one declaration of the same identifier with no linkage in the
2092 same scope, except type tags] and 6.2.2p6 [parameters have no
2093 linkage]). We must check for a forward parameter declaration,
2094 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2095 an extension, the mandatory diagnostic for which is handled by
2096 mark_forward_parm_decls. */
2098 if (TREE_CODE (newdecl) == PARM_DECL
2099 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2101 error ("redefinition of parameter %q+D", newdecl);
2102 locate_old_decl (olddecl);
2107 /* Optional warning for completely redundant decls. */
2108 if (!warned && !pedwarned
2109 && warn_redundant_decls
2110 /* Don't warn about a function declaration followed by a
2112 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2113 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2114 /* Don't warn about redundant redeclarations of builtins. */
2115 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2116 && !DECL_BUILT_IN (newdecl)
2117 && DECL_BUILT_IN (olddecl)
2118 && !C_DECL_DECLARED_BUILTIN (olddecl))
2119 /* Don't warn about an extern followed by a definition. */
2120 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2121 /* Don't warn about forward parameter decls. */
2122 && !(TREE_CODE (newdecl) == PARM_DECL
2123 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2124 /* Don't warn about a variable definition following a declaration. */
2125 && !(TREE_CODE (newdecl) == VAR_DECL
2126 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2128 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2132 /* Report location of previous decl/defn. */
2133 if (warned || pedwarned)
2134 locate_old_decl (olddecl);
2136 #undef DECL_EXTERN_INLINE
2141 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2142 consistent with OLDDECL, but carries new information. Merge the
2143 new information into OLDDECL. This function issues no
2147 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2149 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2150 && DECL_INITIAL (newdecl) != 0);
2151 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2152 && prototype_p (TREE_TYPE (newdecl)));
2153 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2154 && prototype_p (TREE_TYPE (olddecl)));
2155 bool extern_changed = false;
2157 /* For real parm decl following a forward decl, rechain the old decl
2158 in its new location and clear TREE_ASM_WRITTEN (it's not a
2159 forward decl anymore). */
2160 if (TREE_CODE (newdecl) == PARM_DECL
2161 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2163 struct c_binding *b, **here;
2165 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
2166 if ((*here)->decl == olddecl)
2173 b->prev = current_scope->bindings;
2174 current_scope->bindings = b;
2176 TREE_ASM_WRITTEN (olddecl) = 0;
2179 DECL_ATTRIBUTES (newdecl)
2180 = targetm.merge_decl_attributes (olddecl, newdecl);
2182 /* Merge the data types specified in the two decls. */
2184 = TREE_TYPE (olddecl)
2185 = composite_type (newtype, oldtype);
2187 /* Lay the type out, unless already done. */
2188 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2190 if (TREE_TYPE (newdecl) != error_mark_node)
2191 layout_type (TREE_TYPE (newdecl));
2192 if (TREE_CODE (newdecl) != FUNCTION_DECL
2193 && TREE_CODE (newdecl) != TYPE_DECL
2194 && TREE_CODE (newdecl) != CONST_DECL)
2195 layout_decl (newdecl, 0);
2199 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2200 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2201 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2202 DECL_MODE (newdecl) = DECL_MODE (olddecl);
2203 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2205 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2206 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2210 /* Keep the old rtl since we can safely use it. */
2211 if (HAS_RTL_P (olddecl))
2212 COPY_DECL_RTL (olddecl, newdecl);
2214 /* Merge the type qualifiers. */
2215 if (TREE_READONLY (newdecl))
2216 TREE_READONLY (olddecl) = 1;
2218 if (TREE_THIS_VOLATILE (newdecl))
2219 TREE_THIS_VOLATILE (olddecl) = 1;
2221 /* Merge deprecatedness. */
2222 if (TREE_DEPRECATED (newdecl))
2223 TREE_DEPRECATED (olddecl) = 1;
2225 /* If a decl is in a system header and the other isn't, keep the one on the
2226 system header. Otherwise, keep source location of definition rather than
2227 declaration and of prototype rather than non-prototype unless that
2228 prototype is built-in. */
2229 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2230 && DECL_IN_SYSTEM_HEADER (olddecl)
2231 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2232 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2233 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2234 && DECL_IN_SYSTEM_HEADER (newdecl)
2235 && !DECL_IN_SYSTEM_HEADER (olddecl))
2236 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2237 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
2238 || (old_is_prototype && !new_is_prototype
2239 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2240 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2242 /* Merge the initialization information. */
2243 if (DECL_INITIAL (newdecl) == 0)
2244 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2246 /* Merge the threadprivate attribute. */
2247 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
2249 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
2250 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2253 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2255 /* Merge the section attribute.
2256 We want to issue an error if the sections conflict but that
2257 must be done later in decl_attributes since we are called
2258 before attributes are assigned. */
2259 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
2260 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
2262 /* Copy the assembler name.
2263 Currently, it can only be defined in the prototype. */
2264 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2266 /* Use visibility of whichever declaration had it specified */
2267 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2269 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2270 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2273 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2275 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2276 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2277 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2278 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2279 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2280 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2281 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2282 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2283 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2284 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2285 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2288 /* Merge the storage class information. */
2289 merge_weak (newdecl, olddecl);
2291 /* For functions, static overrides non-static. */
2292 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2294 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2295 /* This is since we don't automatically
2296 copy the attributes of NEWDECL into OLDDECL. */
2297 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2298 /* If this clears `static', clear it in the identifier too. */
2299 if (!TREE_PUBLIC (olddecl))
2300 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2304 /* In c99, 'extern' declaration before (or after) 'inline' means this
2305 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2307 if (TREE_CODE (newdecl) == FUNCTION_DECL
2308 && !flag_gnu89_inline
2309 && (DECL_DECLARED_INLINE_P (newdecl)
2310 || DECL_DECLARED_INLINE_P (olddecl))
2311 && (!DECL_DECLARED_INLINE_P (newdecl)
2312 || !DECL_DECLARED_INLINE_P (olddecl)
2313 || !DECL_EXTERNAL (olddecl))
2314 && DECL_EXTERNAL (newdecl)
2315 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2316 && !current_function_decl)
2317 DECL_EXTERNAL (newdecl) = 0;
2319 if (DECL_EXTERNAL (newdecl))
2321 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2322 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2324 /* An extern decl does not override previous storage class. */
2325 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2326 if (!DECL_EXTERNAL (newdecl))
2328 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2329 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2334 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2335 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2338 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2340 /* If we're redefining a function previously defined as extern
2341 inline, make sure we emit debug info for the inline before we
2342 throw it away, in case it was inlined into a function that
2343 hasn't been written out yet. */
2344 if (new_is_definition && DECL_INITIAL (olddecl))
2345 /* The new defn must not be inline. */
2346 DECL_UNINLINABLE (newdecl) = 1;
2349 /* If either decl says `inline', this fn is inline, unless
2350 its definition was passed already. */
2351 if (DECL_DECLARED_INLINE_P (newdecl)
2352 || DECL_DECLARED_INLINE_P (olddecl))
2353 DECL_DECLARED_INLINE_P (newdecl) = 1;
2355 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2356 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2358 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2359 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2360 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2361 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2364 if (DECL_BUILT_IN (olddecl))
2366 /* If redeclaring a builtin function, it stays built in.
2367 But it gets tagged as having been declared. */
2368 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2369 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2370 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2371 if (new_is_prototype)
2373 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2374 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2376 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2379 /* If a compatible prototype of these builtin functions
2380 is seen, assume the runtime implements it with the
2381 expected semantics. */
2382 case BUILT_IN_STPCPY:
2383 if (builtin_decl_explicit_p (fncode))
2384 set_builtin_decl_implicit_p (fncode, true);
2392 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2393 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2396 /* Preserve function specific target and optimization options */
2397 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2398 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2399 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2400 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2402 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2403 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2404 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2405 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2407 /* Also preserve various other info from the definition. */
2408 if (!new_is_definition)
2411 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2412 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2413 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2414 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2415 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2416 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2417 DECL_CONTEXT (t) = newdecl;
2419 /* See if we've got a function to instantiate from. */
2420 if (DECL_SAVED_TREE (olddecl))
2421 DECL_ABSTRACT_ORIGIN (newdecl)
2422 = DECL_ABSTRACT_ORIGIN (olddecl);
2426 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
2428 /* Merge the USED information. */
2429 if (TREE_USED (olddecl))
2430 TREE_USED (newdecl) = 1;
2431 else if (TREE_USED (newdecl))
2432 TREE_USED (olddecl) = 1;
2433 if (TREE_CODE (olddecl) == VAR_DECL || TREE_CODE (olddecl) == PARM_DECL)
2434 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2435 if (DECL_PRESERVE_P (olddecl))
2436 DECL_PRESERVE_P (newdecl) = 1;
2437 else if (DECL_PRESERVE_P (newdecl))
2438 DECL_PRESERVE_P (olddecl) = 1;
2440 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2441 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2442 DECL_ARGUMENTS (if appropriate). */
2444 unsigned olddecl_uid = DECL_UID (olddecl);
2445 tree olddecl_context = DECL_CONTEXT (olddecl);
2446 tree olddecl_arguments = NULL;
2447 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2448 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2450 memcpy ((char *) olddecl + sizeof (struct tree_common),
2451 (char *) newdecl + sizeof (struct tree_common),
2452 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2453 switch (TREE_CODE (olddecl))
2463 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2464 (char *) newdecl + sizeof (struct tree_decl_common),
2465 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2470 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2471 (char *) newdecl + sizeof (struct tree_decl_common),
2472 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2474 DECL_UID (olddecl) = olddecl_uid;
2475 DECL_CONTEXT (olddecl) = olddecl_context;
2476 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2477 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2480 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2481 so that encode_section_info has a chance to look at the new decl
2482 flags and attributes. */
2483 if (DECL_RTL_SET_P (olddecl)
2484 && (TREE_CODE (olddecl) == FUNCTION_DECL
2485 || (TREE_CODE (olddecl) == VAR_DECL
2486 && TREE_STATIC (olddecl))))
2487 make_decl_rtl (olddecl);
2489 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
2490 and the definition is coming from the old version, cgraph needs
2491 to be called again. */
2492 if (extern_changed && !new_is_definition
2493 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
2494 cgraph_mark_if_needed (olddecl);
2497 /* Handle when a new declaration NEWDECL has the same name as an old
2498 one OLDDECL in the same binding contour. Prints an error message
2501 If safely possible, alter OLDDECL to look like NEWDECL, and return
2502 true. Otherwise, return false. */
2505 duplicate_decls (tree newdecl, tree olddecl)
2507 tree newtype = NULL, oldtype = NULL;
2509 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2511 /* Avoid `unused variable' and other warnings for OLDDECL. */
2512 TREE_NO_WARNING (olddecl) = 1;
2516 merge_decls (newdecl, olddecl, newtype, oldtype);
2521 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2523 warn_if_shadowing (tree new_decl)
2525 struct c_binding *b;
2527 /* Shadow warnings wanted? */
2529 /* No shadow warnings for internally generated vars. */
2530 || DECL_IS_BUILTIN (new_decl)
2531 /* No shadow warnings for vars made for inlining. */
2532 || DECL_FROM_INLINE (new_decl))
2535 /* Is anything being shadowed? Invisible decls do not count. */
2536 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2537 if (b->decl && b->decl != new_decl && !b->invisible
2538 && (b->decl == error_mark_node
2539 || diagnostic_report_warnings_p (global_dc,
2540 DECL_SOURCE_LOCATION (b->decl))))
2542 tree old_decl = b->decl;
2544 if (old_decl == error_mark_node)
2546 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2547 "non-variable", new_decl);
2550 else if (TREE_CODE (old_decl) == PARM_DECL)
2551 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2553 else if (DECL_FILE_SCOPE_P (old_decl))
2554 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2555 "declaration", new_decl);
2556 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2557 && DECL_BUILT_IN (old_decl))
2559 warning (OPT_Wshadow, "declaration of %q+D shadows "
2560 "a built-in function", new_decl);
2564 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2567 warning_at (DECL_SOURCE_LOCATION (old_decl), OPT_Wshadow,
2568 "shadowed declaration is here");
2574 /* Record a decl-node X as belonging to the current lexical scope.
2575 Check for errors (such as an incompatible declaration for the same
2576 name already seen in the same scope).
2578 Returns either X or an old decl for the same name.
2579 If an old decl is returned, it may have been smashed
2580 to agree with what X says. */
2585 tree name = DECL_NAME (x);
2586 struct c_scope *scope = current_scope;
2587 struct c_binding *b;
2588 bool nested = false;
2589 location_t locus = DECL_SOURCE_LOCATION (x);
2591 /* Must set DECL_CONTEXT for everything not at file scope or
2592 DECL_FILE_SCOPE_P won't work. Local externs don't count
2593 unless they have initializers (which generate code). */
2594 if (current_function_decl
2595 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2596 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2597 DECL_CONTEXT (x) = current_function_decl;
2599 /* Anonymous decls are just inserted in the scope. */
2602 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2607 /* First, see if there is another declaration with the same name in
2608 the current scope. If there is, duplicate_decls may do all the
2609 work for us. If duplicate_decls returns false, that indicates
2610 two incompatible decls in the same scope; we are to silently
2611 replace the old one (duplicate_decls has issued all appropriate
2612 diagnostics). In particular, we should not consider possible
2613 duplicates in the external scope, or shadowing. */
2614 b = I_SYMBOL_BINDING (name);
2615 if (b && B_IN_SCOPE (b, scope))
2617 struct c_binding *b_ext, *b_use;
2618 tree type = TREE_TYPE (x);
2619 tree visdecl = b->decl;
2620 tree vistype = TREE_TYPE (visdecl);
2621 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2622 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2623 b->inner_comp = false;
2626 /* If this is an external linkage declaration, we should check
2627 for compatibility with the type in the external scope before
2628 setting the type at this scope based on the visible
2629 information only. */
2630 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2632 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2633 b_ext = b_ext->shadowed;
2638 TREE_TYPE (b_use->decl) = b_use->u.type;
2641 if (duplicate_decls (x, b_use->decl))
2645 /* Save the updated type in the external scope and
2646 restore the proper type for this scope. */
2648 if (comptypes (vistype, type))
2649 thistype = composite_type (vistype, type);
2651 thistype = TREE_TYPE (b_use->decl);
2652 b_use->u.type = TREE_TYPE (b_use->decl);
2653 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2654 && DECL_BUILT_IN (b_use->decl))
2656 = build_type_attribute_variant (thistype,
2659 TREE_TYPE (b_use->decl) = thistype;
2664 goto skip_external_and_shadow_checks;
2667 /* All declarations with external linkage, and all external
2668 references, go in the external scope, no matter what scope is
2669 current. However, the binding in that scope is ignored for
2670 purposes of normal name lookup. A separate binding structure is
2671 created in the requested scope; this governs the normal
2672 visibility of the symbol.
2674 The binding in the externals scope is used exclusively for
2675 detecting duplicate declarations of the same object, no matter
2676 what scope they are in; this is what we do here. (C99 6.2.7p2:
2677 All declarations that refer to the same object or function shall
2678 have compatible type; otherwise, the behavior is undefined.) */
2679 if (DECL_EXTERNAL (x) || scope == file_scope)
2681 tree type = TREE_TYPE (x);
2684 bool type_saved = false;
2685 if (b && !B_IN_EXTERNAL_SCOPE (b)
2686 && (TREE_CODE (b->decl) == FUNCTION_DECL
2687 || TREE_CODE (b->decl) == VAR_DECL)
2688 && DECL_FILE_SCOPE_P (b->decl))
2691 vistype = TREE_TYPE (visdecl);
2693 if (scope != file_scope
2694 && !DECL_IN_SYSTEM_HEADER (x))
2695 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2697 while (b && !B_IN_EXTERNAL_SCOPE (b))
2699 /* If this decl might be modified, save its type. This is
2700 done here rather than when the decl is first bound
2701 because the type may change after first binding, through
2702 being completed or through attributes being added. If we
2703 encounter multiple such decls, only the first should have
2704 its type saved; the others will already have had their
2705 proper types saved and the types will not have changed as
2706 their scopes will not have been re-entered. */
2707 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2709 b->u.type = TREE_TYPE (b->decl);
2712 if (B_IN_FILE_SCOPE (b)
2713 && TREE_CODE (b->decl) == VAR_DECL
2714 && TREE_STATIC (b->decl)
2715 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2716 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2717 && TREE_CODE (type) == ARRAY_TYPE
2718 && TYPE_DOMAIN (type)
2719 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2720 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2722 /* Array type completed in inner scope, which should be
2723 diagnosed if the completion does not have size 1 and
2724 it does not get completed in the file scope. */
2725 b->inner_comp = true;
2730 /* If a matching external declaration has been found, set its
2731 type to the composite of all the types of that declaration.
2732 After the consistency checks, it will be reset to the
2733 composite of the visible types only. */
2734 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2736 TREE_TYPE (b->decl) = b->u.type;
2738 /* The point of the same_translation_unit_p check here is,
2739 we want to detect a duplicate decl for a construct like
2740 foo() { extern bar(); } ... static bar(); but not if
2741 they are in different translation units. In any case,
2742 the static does not go in the externals scope. */
2744 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2745 && duplicate_decls (x, b->decl))
2750 if (comptypes (vistype, type))
2751 thistype = composite_type (vistype, type);
2753 thistype = TREE_TYPE (b->decl);
2757 b->u.type = TREE_TYPE (b->decl);
2758 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2760 = build_type_attribute_variant (thistype,
2761 TYPE_ATTRIBUTES (b->u.type));
2762 TREE_TYPE (b->decl) = thistype;
2763 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2767 else if (TREE_PUBLIC (x))
2769 if (visdecl && !b && duplicate_decls (x, visdecl))
2771 /* An external declaration at block scope referring to a
2772 visible entity with internal linkage. The composite
2773 type will already be correct for this scope, so we
2774 just need to fall through to make the declaration in
2781 bind (name, x, external_scope, /*invisible=*/true,
2782 /*nested=*/false, locus);
2788 if (TREE_CODE (x) != PARM_DECL)
2789 warn_if_shadowing (x);
2791 skip_external_and_shadow_checks:
2792 if (TREE_CODE (x) == TYPE_DECL)
2794 /* So this is a typedef, set its underlying type. */
2795 set_underlying_type (x);
2797 /* If X is a typedef defined in the current function, record it
2798 for the purpose of implementing the -Wunused-local-typedefs
2800 record_locally_defined_typedef (x);
2803 bind (name, x, scope, /*invisible=*/false, nested, locus);
2805 /* If x's type is incomplete because it's based on a
2806 structure or union which has not yet been fully declared,
2807 attach it to that structure or union type, so we can go
2808 back and complete the variable declaration later, if the
2809 structure or union gets fully declared.
2811 If the input is erroneous, we can have error_mark in the type
2812 slot (e.g. "f(void a, ...)") - that doesn't count as an
2814 if (TREE_TYPE (x) != error_mark_node
2815 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2817 tree element = TREE_TYPE (x);
2819 while (TREE_CODE (element) == ARRAY_TYPE)
2820 element = TREE_TYPE (element);
2821 element = TYPE_MAIN_VARIANT (element);
2823 if ((TREE_CODE (element) == RECORD_TYPE
2824 || TREE_CODE (element) == UNION_TYPE)
2825 && (TREE_CODE (x) != TYPE_DECL
2826 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2827 && !COMPLETE_TYPE_P (element))
2828 C_TYPE_INCOMPLETE_VARS (element)
2829 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2834 /* Record X as belonging to file scope.
2835 This is used only internally by the Objective-C front end,
2836 and is limited to its needs. duplicate_decls is not called;
2837 if there is any preexisting decl for this identifier, it is an ICE. */
2840 pushdecl_top_level (tree x)
2843 bool nested = false;
2844 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2846 name = DECL_NAME (x);
2848 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2850 if (TREE_PUBLIC (x))
2852 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2857 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2863 implicit_decl_warning (tree id, tree olddecl)
2865 if (warn_implicit_function_declaration)
2870 warned = pedwarn (input_location, OPT_Wimplicit_function_declaration,
2871 "implicit declaration of function %qE", id);
2873 warned = warning (OPT_Wimplicit_function_declaration,
2874 G_("implicit declaration of function %qE"), id);
2875 if (olddecl && warned)
2876 locate_old_decl (olddecl);
2880 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
2881 function of type int (). */
2884 implicitly_declare (location_t loc, tree functionid)
2886 struct c_binding *b;
2890 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2892 if (B_IN_SCOPE (b, external_scope))
2901 if (decl == error_mark_node)
2904 /* FIXME: Objective-C has weird not-really-builtin functions
2905 which are supposed to be visible automatically. They wind up
2906 in the external scope because they're pushed before the file
2907 scope gets created. Catch this here and rebind them into the
2909 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2911 bind (functionid, decl, file_scope,
2912 /*invisible=*/false, /*nested=*/true,
2913 DECL_SOURCE_LOCATION (decl));
2918 tree newtype = default_function_type;
2920 TREE_TYPE (decl) = b->u.type;
2921 /* Implicit declaration of a function already declared
2922 (somehow) in a different scope, or as a built-in.
2923 If this is the first time this has happened, warn;
2924 then recycle the old declaration but with the new type. */
2925 if (!C_DECL_IMPLICIT (decl))
2927 implicit_decl_warning (functionid, decl);
2928 C_DECL_IMPLICIT (decl) = 1;
2930 if (DECL_BUILT_IN (decl))
2932 newtype = build_type_attribute_variant (newtype,
2934 (TREE_TYPE (decl)));
2935 if (!comptypes (newtype, TREE_TYPE (decl)))
2937 warning_at (loc, 0, "incompatible implicit declaration of "
2938 "built-in function %qD", decl);
2939 newtype = TREE_TYPE (decl);
2944 if (!comptypes (newtype, TREE_TYPE (decl)))
2946 error_at (loc, "incompatible implicit declaration of function %qD", decl);
2947 locate_old_decl (decl);
2950 b->u.type = TREE_TYPE (decl);
2951 TREE_TYPE (decl) = newtype;
2952 bind (functionid, decl, current_scope,
2953 /*invisible=*/false, /*nested=*/true,
2954 DECL_SOURCE_LOCATION (decl));
2959 /* Not seen before. */
2960 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
2961 DECL_EXTERNAL (decl) = 1;
2962 TREE_PUBLIC (decl) = 1;
2963 C_DECL_IMPLICIT (decl) = 1;
2964 implicit_decl_warning (functionid, 0);
2965 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2967 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2969 /* C89 says implicit declarations are in the innermost block.
2970 So we record the decl in the standard fashion. */
2971 decl = pushdecl (decl);
2973 /* No need to call objc_check_decl here - it's a function type. */
2974 rest_of_decl_compilation (decl, 0, 0);
2976 /* Write a record describing this implicit function declaration
2977 to the prototypes file (if requested). */
2978 gen_aux_info_record (decl, 0, 1, 0);
2980 /* Possibly apply some default attributes to this implicit declaration. */
2981 decl_attributes (&decl, NULL_TREE, 0);
2986 /* Issue an error message for a reference to an undeclared variable
2987 ID, including a reference to a builtin outside of function-call
2988 context. Establish a binding of the identifier to error_mark_node
2989 in an appropriate scope, which will suppress further errors for the
2990 same identifier. The error message should be given location LOC. */
2992 undeclared_variable (location_t loc, tree id)
2994 static bool already = false;
2995 struct c_scope *scope;
2997 if (current_function_decl == 0)
2999 error_at (loc, "%qE undeclared here (not in a function)", id);
3000 scope = current_scope;
3004 if (!objc_diagnose_private_ivar (id))
3005 error_at (loc, "%qE undeclared (first use in this function)", id);
3008 inform (loc, "each undeclared identifier is reported only"
3009 " once for each function it appears in");
3013 /* If we are parsing old-style parameter decls, current_function_decl
3014 will be nonnull but current_function_scope will be null. */
3015 scope = current_function_scope ? current_function_scope : current_scope;
3017 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3021 /* Subroutine of lookup_label, declare_label, define_label: construct a
3022 LABEL_DECL with all the proper frills. Also create a struct
3023 c_label_vars initialized for the current scope. */
3026 make_label (location_t location, tree name, bool defining,
3027 struct c_label_vars **p_label_vars)
3029 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3030 struct c_label_vars *label_vars;
3032 DECL_CONTEXT (label) = current_function_decl;
3033 DECL_MODE (label) = VOIDmode;
3035 label_vars = ggc_alloc_c_label_vars ();
3036 label_vars->shadowed = NULL;
3037 set_spot_bindings (&label_vars->label_bindings, defining);
3038 label_vars->decls_in_scope = make_tree_vector ();
3039 label_vars->gotos = VEC_alloc (c_goto_bindings_p, gc, 0);
3040 *p_label_vars = label_vars;
3045 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3046 Create one if none exists so far for the current function.
3047 This is called when a label is used in a goto expression or
3048 has its address taken. */
3051 lookup_label (tree name)
3054 struct c_label_vars *label_vars;
3056 if (current_function_scope == 0)
3058 error ("label %qE referenced outside of any function", name);
3062 /* Use a label already defined or ref'd with this name, but not if
3063 it is inherited from a containing function and wasn't declared
3065 label = I_LABEL_DECL (name);
3066 if (label && (DECL_CONTEXT (label) == current_function_decl
3067 || C_DECLARED_LABEL_FLAG (label)))
3069 /* If the label has only been declared, update its apparent
3070 location to point here, for better diagnostics if it
3071 turns out not to have been defined. */
3072 if (DECL_INITIAL (label) == NULL_TREE)
3073 DECL_SOURCE_LOCATION (label) = input_location;
3077 /* No label binding for that identifier; make one. */
3078 label = make_label (input_location, name, false, &label_vars);
3080 /* Ordinary labels go in the current function scope. */
3081 bind_label (name, label, current_function_scope, label_vars);
3086 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3090 warn_about_goto (location_t goto_loc, tree label, tree decl)
3092 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3094 "jump into scope of identifier with variably modified type");
3096 warning_at (goto_loc, OPT_Wjump_misses_init,
3097 "jump skips variable initialization");
3098 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3099 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3102 /* Look up a label because of a goto statement. This is like
3103 lookup_label, but also issues any appropriate warnings. */
3106 lookup_label_for_goto (location_t loc, tree name)
3109 struct c_label_vars *label_vars;
3113 label = lookup_label (name);
3114 if (label == NULL_TREE)
3117 /* If we are jumping to a different function, we can't issue any
3119 if (DECL_CONTEXT (label) != current_function_decl)
3121 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3125 label_vars = I_LABEL_BINDING (name)->u.label;
3127 /* If the label has not yet been defined, then push this goto on a
3128 list for possible later warnings. */
3129 if (label_vars->label_bindings.scope == NULL)
3131 struct c_goto_bindings *g;
3133 g = ggc_alloc_c_goto_bindings ();
3135 set_spot_bindings (&g->goto_bindings, true);
3136 VEC_safe_push (c_goto_bindings_p, gc, label_vars->gotos, g);
3140 /* If there are any decls in label_vars->decls_in_scope, then this
3141 goto has missed the declaration of the decl. This happens for a
3147 Issue a warning or error. */
3148 FOR_EACH_VEC_ELT (tree, label_vars->decls_in_scope, ix, decl)
3149 warn_about_goto (loc, label, decl);
3151 if (label_vars->label_bindings.left_stmt_expr)
3153 error_at (loc, "jump into statement expression");
3154 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3160 /* Make a label named NAME in the current function, shadowing silently
3161 any that may be inherited from containing functions or containing
3162 scopes. This is called for __label__ declarations. */
3165 declare_label (tree name)
3167 struct c_binding *b = I_LABEL_BINDING (name);
3169 struct c_label_vars *label_vars;
3171 /* Check to make sure that the label hasn't already been declared
3173 if (b && B_IN_CURRENT_SCOPE (b))
3175 error ("duplicate label declaration %qE", name);
3176 locate_old_decl (b->decl);
3178 /* Just use the previous declaration. */
3182 label = make_label (input_location, name, false, &label_vars);
3183 C_DECLARED_LABEL_FLAG (label) = 1;
3185 /* Declared labels go in the current scope. */
3186 bind_label (name, label, current_scope, label_vars);
3191 /* When we define a label, issue any appropriate warnings if there are
3192 any gotos earlier in the function which jump to this label. */
3195 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3198 struct c_goto_bindings *g;
3200 FOR_EACH_VEC_ELT (c_goto_bindings_p, label_vars->gotos, ix, g)
3202 struct c_binding *b;
3203 struct c_scope *scope;
3205 /* We have a goto to this label. The goto is going forward. In
3206 g->scope, the goto is going to skip any binding which was
3207 defined after g->bindings_in_scope. */
3208 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3210 for (b = g->goto_bindings.scope->bindings;
3211 b != g->goto_bindings.bindings_in_scope;
3214 if (decl_jump_unsafe (b->decl))
3215 warn_about_goto (g->loc, label, b->decl);
3219 /* We also need to warn about decls defined in any scopes
3220 between the scope of the label and the scope of the goto. */
3221 for (scope = label_vars->label_bindings.scope;
3222 scope != g->goto_bindings.scope;
3223 scope = scope->outer)
3225 gcc_assert (scope != NULL);
3226 if (scope->has_jump_unsafe_decl)
3228 if (scope == label_vars->label_bindings.scope)
3229 b = label_vars->label_bindings.bindings_in_scope;
3231 b = scope->bindings;
3232 for (; b != NULL; b = b->prev)
3234 if (decl_jump_unsafe (b->decl))
3235 warn_about_goto (g->loc, label, b->decl);
3240 if (g->goto_bindings.stmt_exprs > 0)
3242 error_at (g->loc, "jump into statement expression");
3243 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3248 /* Now that the label is defined, we will issue warnings about
3249 subsequent gotos to this label when we see them. */
3250 VEC_truncate (c_goto_bindings_p, label_vars->gotos, 0);
3251 label_vars->gotos = NULL;
3254 /* Define a label, specifying the location in the source file.
3255 Return the LABEL_DECL node for the label, if the definition is valid.
3256 Otherwise return 0. */
3259 define_label (location_t location, tree name)
3261 /* Find any preexisting label with this name. It is an error
3262 if that label has already been defined in this function, or
3263 if there is a containing function with a declared label with
3265 tree label = I_LABEL_DECL (name);
3268 && ((DECL_CONTEXT (label) == current_function_decl
3269 && DECL_INITIAL (label) != 0)
3270 || (DECL_CONTEXT (label) != current_function_decl
3271 && C_DECLARED_LABEL_FLAG (label))))
3273 error_at (location, "duplicate label %qD", label);
3274 locate_old_decl (label);
3277 else if (label && DECL_CONTEXT (label) == current_function_decl)
3279 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3281 /* The label has been used or declared already in this function,
3282 but not defined. Update its location to point to this
3284 DECL_SOURCE_LOCATION (label) = location;
3285 set_spot_bindings (&label_vars->label_bindings, true);
3287 /* Issue warnings as required about any goto statements from
3288 earlier in the function. */
3289 check_earlier_gotos (label, label_vars);
3293 struct c_label_vars *label_vars;
3295 /* No label binding for that identifier; make one. */
3296 label = make_label (location, name, true, &label_vars);
3298 /* Ordinary labels go in the current function scope. */
3299 bind_label (name, label, current_function_scope, label_vars);
3302 if (!in_system_header && lookup_name (name))
3303 warning_at (location, OPT_Wtraditional,
3304 "traditional C lacks a separate namespace "
3305 "for labels, identifier %qE conflicts", name);
3307 /* Mark label as having been defined. */
3308 DECL_INITIAL (label) = error_mark_node;
3312 /* Get the bindings for a new switch statement. This is used to issue
3313 warnings as appropriate for jumps from the switch to case or
3316 struct c_spot_bindings *
3317 c_get_switch_bindings (void)
3319 struct c_spot_bindings *switch_bindings;
3321 switch_bindings = XNEW (struct c_spot_bindings);
3322 set_spot_bindings (switch_bindings, true);
3323 return switch_bindings;
3327 c_release_switch_bindings (struct c_spot_bindings *bindings)
3329 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3333 /* This is called at the point of a case or default label to issue
3334 warnings about decls as needed. It returns true if it found an
3335 error, not just a warning. */
3338 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3339 location_t switch_loc, location_t case_loc)
3342 struct c_scope *scope;
3345 for (scope = current_scope;
3346 scope != switch_bindings->scope;
3347 scope = scope->outer)
3349 struct c_binding *b;
3351 gcc_assert (scope != NULL);
3353 if (!scope->has_jump_unsafe_decl)
3356 for (b = scope->bindings; b != NULL; b = b->prev)
3358 if (decl_jump_unsafe (b->decl))
3360 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3364 ("switch jumps into scope of identifier with "
3365 "variably modified type"));
3368 warning_at (case_loc, OPT_Wjump_misses_init,
3369 "switch jumps over variable initialization");
3370 inform (switch_loc, "switch starts here");
3371 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3377 if (switch_bindings->stmt_exprs > 0)
3380 error_at (case_loc, "switch jumps into statement expression");
3381 inform (switch_loc, "switch starts here");
3387 /* Given NAME, an IDENTIFIER_NODE,
3388 return the structure (or union or enum) definition for that name.
3389 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3390 CODE says which kind of type the caller wants;
3391 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3392 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3393 location where the tag was defined.
3394 If the wrong kind of type is found, an error is reported. */
3397 lookup_tag (enum tree_code code, tree name, int thislevel_only,
3400 struct c_binding *b = I_TAG_BINDING (name);
3406 /* We only care about whether it's in this level if
3407 thislevel_only was set or it might be a type clash. */
3408 if (thislevel_only || TREE_CODE (b->decl) != code)
3410 /* For our purposes, a tag in the external scope is the same as
3411 a tag in the file scope. (Primarily relevant to Objective-C
3412 and its builtin structure tags, which get pushed before the
3413 file scope is created.) */
3414 if (B_IN_CURRENT_SCOPE (b)
3415 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3419 if (thislevel_only && !thislevel)
3422 if (TREE_CODE (b->decl) != code)
3424 /* Definition isn't the kind we were looking for. */
3425 pending_invalid_xref = name;
3426 pending_invalid_xref_location = input_location;
3428 /* If in the same binding level as a declaration as a tag
3429 of a different type, this must not be allowed to
3430 shadow that tag, so give the error immediately.
3431 (For example, "struct foo; union foo;" is invalid.) */
3433 pending_xref_error ();
3442 /* Print an error message now
3443 for a recent invalid struct, union or enum cross reference.
3444 We don't print them immediately because they are not invalid
3445 when used in the `struct foo;' construct for shadowing. */
3448 pending_xref_error (void)
3450 if (pending_invalid_xref != 0)
3451 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3452 pending_invalid_xref);
3453 pending_invalid_xref = 0;
3457 /* Look up NAME in the current scope and its superiors
3458 in the namespace of variables, functions and typedefs.
3459 Return a ..._DECL node of some kind representing its definition,
3460 or return 0 if it is undefined. */
3463 lookup_name (tree name)
3465 struct c_binding *b = I_SYMBOL_BINDING (name);
3466 if (b && !b->invisible)
3468 maybe_record_typedef_use (b->decl);
3474 /* Similar to `lookup_name' but look only at the indicated scope. */
3477 lookup_name_in_scope (tree name, struct c_scope *scope)
3479 struct c_binding *b;