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)
2539 tree old_decl = b->decl;
2541 if (old_decl == error_mark_node)
2543 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2544 "non-variable", new_decl);
2547 else if (TREE_CODE (old_decl) == PARM_DECL)
2548 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2550 else if (DECL_FILE_SCOPE_P (old_decl))
2551 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2552 "declaration", new_decl);
2553 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2554 && DECL_BUILT_IN (old_decl))
2556 warning (OPT_Wshadow, "declaration of %q+D shadows "
2557 "a built-in function", new_decl);
2561 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2564 warning_at (DECL_SOURCE_LOCATION (old_decl), OPT_Wshadow,
2565 "shadowed declaration is here");
2571 /* Record a decl-node X as belonging to the current lexical scope.
2572 Check for errors (such as an incompatible declaration for the same
2573 name already seen in the same scope).
2575 Returns either X or an old decl for the same name.
2576 If an old decl is returned, it may have been smashed
2577 to agree with what X says. */
2582 tree name = DECL_NAME (x);
2583 struct c_scope *scope = current_scope;
2584 struct c_binding *b;
2585 bool nested = false;
2586 location_t locus = DECL_SOURCE_LOCATION (x);
2588 /* Must set DECL_CONTEXT for everything not at file scope or
2589 DECL_FILE_SCOPE_P won't work. Local externs don't count
2590 unless they have initializers (which generate code). */
2591 if (current_function_decl
2592 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2593 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2594 DECL_CONTEXT (x) = current_function_decl;
2596 /* Anonymous decls are just inserted in the scope. */
2599 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2604 /* First, see if there is another declaration with the same name in
2605 the current scope. If there is, duplicate_decls may do all the
2606 work for us. If duplicate_decls returns false, that indicates
2607 two incompatible decls in the same scope; we are to silently
2608 replace the old one (duplicate_decls has issued all appropriate
2609 diagnostics). In particular, we should not consider possible
2610 duplicates in the external scope, or shadowing. */
2611 b = I_SYMBOL_BINDING (name);
2612 if (b && B_IN_SCOPE (b, scope))
2614 struct c_binding *b_ext, *b_use;
2615 tree type = TREE_TYPE (x);
2616 tree visdecl = b->decl;
2617 tree vistype = TREE_TYPE (visdecl);
2618 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2619 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2620 b->inner_comp = false;
2623 /* If this is an external linkage declaration, we should check
2624 for compatibility with the type in the external scope before
2625 setting the type at this scope based on the visible
2626 information only. */
2627 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2629 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2630 b_ext = b_ext->shadowed;
2635 TREE_TYPE (b_use->decl) = b_use->u.type;
2638 if (duplicate_decls (x, b_use->decl))
2642 /* Save the updated type in the external scope and
2643 restore the proper type for this scope. */
2645 if (comptypes (vistype, type))
2646 thistype = composite_type (vistype, type);
2648 thistype = TREE_TYPE (b_use->decl);
2649 b_use->u.type = TREE_TYPE (b_use->decl);
2650 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2651 && DECL_BUILT_IN (b_use->decl))
2653 = build_type_attribute_variant (thistype,
2656 TREE_TYPE (b_use->decl) = thistype;
2661 goto skip_external_and_shadow_checks;
2664 /* All declarations with external linkage, and all external
2665 references, go in the external scope, no matter what scope is
2666 current. However, the binding in that scope is ignored for
2667 purposes of normal name lookup. A separate binding structure is
2668 created in the requested scope; this governs the normal
2669 visibility of the symbol.
2671 The binding in the externals scope is used exclusively for
2672 detecting duplicate declarations of the same object, no matter
2673 what scope they are in; this is what we do here. (C99 6.2.7p2:
2674 All declarations that refer to the same object or function shall
2675 have compatible type; otherwise, the behavior is undefined.) */
2676 if (DECL_EXTERNAL (x) || scope == file_scope)
2678 tree type = TREE_TYPE (x);
2681 bool type_saved = false;
2682 if (b && !B_IN_EXTERNAL_SCOPE (b)
2683 && (TREE_CODE (b->decl) == FUNCTION_DECL
2684 || TREE_CODE (b->decl) == VAR_DECL)
2685 && DECL_FILE_SCOPE_P (b->decl))
2688 vistype = TREE_TYPE (visdecl);
2690 if (scope != file_scope
2691 && !DECL_IN_SYSTEM_HEADER (x))
2692 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2694 while (b && !B_IN_EXTERNAL_SCOPE (b))
2696 /* If this decl might be modified, save its type. This is
2697 done here rather than when the decl is first bound
2698 because the type may change after first binding, through
2699 being completed or through attributes being added. If we
2700 encounter multiple such decls, only the first should have
2701 its type saved; the others will already have had their
2702 proper types saved and the types will not have changed as
2703 their scopes will not have been re-entered. */
2704 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2706 b->u.type = TREE_TYPE (b->decl);
2709 if (B_IN_FILE_SCOPE (b)
2710 && TREE_CODE (b->decl) == VAR_DECL
2711 && TREE_STATIC (b->decl)
2712 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2713 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2714 && TREE_CODE (type) == ARRAY_TYPE
2715 && TYPE_DOMAIN (type)
2716 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2717 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2719 /* Array type completed in inner scope, which should be
2720 diagnosed if the completion does not have size 1 and
2721 it does not get completed in the file scope. */
2722 b->inner_comp = true;
2727 /* If a matching external declaration has been found, set its
2728 type to the composite of all the types of that declaration.
2729 After the consistency checks, it will be reset to the
2730 composite of the visible types only. */
2731 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2733 TREE_TYPE (b->decl) = b->u.type;
2735 /* The point of the same_translation_unit_p check here is,
2736 we want to detect a duplicate decl for a construct like
2737 foo() { extern bar(); } ... static bar(); but not if
2738 they are in different translation units. In any case,
2739 the static does not go in the externals scope. */
2741 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2742 && duplicate_decls (x, b->decl))
2747 if (comptypes (vistype, type))
2748 thistype = composite_type (vistype, type);
2750 thistype = TREE_TYPE (b->decl);
2754 b->u.type = TREE_TYPE (b->decl);
2755 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2757 = build_type_attribute_variant (thistype,
2758 TYPE_ATTRIBUTES (b->u.type));
2759 TREE_TYPE (b->decl) = thistype;
2760 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2764 else if (TREE_PUBLIC (x))
2766 if (visdecl && !b && duplicate_decls (x, visdecl))
2768 /* An external declaration at block scope referring to a
2769 visible entity with internal linkage. The composite
2770 type will already be correct for this scope, so we
2771 just need to fall through to make the declaration in
2778 bind (name, x, external_scope, /*invisible=*/true,
2779 /*nested=*/false, locus);
2785 if (TREE_CODE (x) != PARM_DECL)
2786 warn_if_shadowing (x);
2788 skip_external_and_shadow_checks:
2789 if (TREE_CODE (x) == TYPE_DECL)
2791 /* So this is a typedef, set its underlying type. */
2792 set_underlying_type (x);
2794 /* If X is a typedef defined in the current function, record it
2795 for the purpose of implementing the -Wunused-local-typedefs
2797 record_locally_defined_typedef (x);
2800 bind (name, x, scope, /*invisible=*/false, nested, locus);
2802 /* If x's type is incomplete because it's based on a
2803 structure or union which has not yet been fully declared,
2804 attach it to that structure or union type, so we can go
2805 back and complete the variable declaration later, if the
2806 structure or union gets fully declared.
2808 If the input is erroneous, we can have error_mark in the type
2809 slot (e.g. "f(void a, ...)") - that doesn't count as an
2811 if (TREE_TYPE (x) != error_mark_node
2812 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2814 tree element = TREE_TYPE (x);
2816 while (TREE_CODE (element) == ARRAY_TYPE)
2817 element = TREE_TYPE (element);
2818 element = TYPE_MAIN_VARIANT (element);
2820 if ((TREE_CODE (element) == RECORD_TYPE
2821 || TREE_CODE (element) == UNION_TYPE)
2822 && (TREE_CODE (x) != TYPE_DECL
2823 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2824 && !COMPLETE_TYPE_P (element))
2825 C_TYPE_INCOMPLETE_VARS (element)
2826 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2831 /* Record X as belonging to file scope.
2832 This is used only internally by the Objective-C front end,
2833 and is limited to its needs. duplicate_decls is not called;
2834 if there is any preexisting decl for this identifier, it is an ICE. */
2837 pushdecl_top_level (tree x)
2840 bool nested = false;
2841 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2843 name = DECL_NAME (x);
2845 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2847 if (TREE_PUBLIC (x))
2849 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2854 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2860 implicit_decl_warning (tree id, tree olddecl)
2862 if (warn_implicit_function_declaration)
2867 warned = pedwarn (input_location, OPT_Wimplicit_function_declaration,
2868 "implicit declaration of function %qE", id);
2870 warned = warning (OPT_Wimplicit_function_declaration,
2871 G_("implicit declaration of function %qE"), id);
2872 if (olddecl && warned)
2873 locate_old_decl (olddecl);
2877 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
2878 function of type int (). */
2881 implicitly_declare (location_t loc, tree functionid)
2883 struct c_binding *b;
2887 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2889 if (B_IN_SCOPE (b, external_scope))
2898 if (decl == error_mark_node)
2901 /* FIXME: Objective-C has weird not-really-builtin functions
2902 which are supposed to be visible automatically. They wind up
2903 in the external scope because they're pushed before the file
2904 scope gets created. Catch this here and rebind them into the
2906 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2908 bind (functionid, decl, file_scope,
2909 /*invisible=*/false, /*nested=*/true,
2910 DECL_SOURCE_LOCATION (decl));
2915 tree newtype = default_function_type;
2917 TREE_TYPE (decl) = b->u.type;
2918 /* Implicit declaration of a function already declared
2919 (somehow) in a different scope, or as a built-in.
2920 If this is the first time this has happened, warn;
2921 then recycle the old declaration but with the new type. */
2922 if (!C_DECL_IMPLICIT (decl))
2924 implicit_decl_warning (functionid, decl);
2925 C_DECL_IMPLICIT (decl) = 1;
2927 if (DECL_BUILT_IN (decl))
2929 newtype = build_type_attribute_variant (newtype,
2931 (TREE_TYPE (decl)));
2932 if (!comptypes (newtype, TREE_TYPE (decl)))
2934 warning_at (loc, 0, "incompatible implicit declaration of "
2935 "built-in function %qD", decl);
2936 newtype = TREE_TYPE (decl);
2941 if (!comptypes (newtype, TREE_TYPE (decl)))
2943 error_at (loc, "incompatible implicit declaration of function %qD", decl);
2944 locate_old_decl (decl);
2947 b->u.type = TREE_TYPE (decl);
2948 TREE_TYPE (decl) = newtype;
2949 bind (functionid, decl, current_scope,
2950 /*invisible=*/false, /*nested=*/true,
2951 DECL_SOURCE_LOCATION (decl));
2956 /* Not seen before. */
2957 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
2958 DECL_EXTERNAL (decl) = 1;
2959 TREE_PUBLIC (decl) = 1;
2960 C_DECL_IMPLICIT (decl) = 1;
2961 implicit_decl_warning (functionid, 0);
2962 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2964 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2966 /* C89 says implicit declarations are in the innermost block.
2967 So we record the decl in the standard fashion. */
2968 decl = pushdecl (decl);
2970 /* No need to call objc_check_decl here - it's a function type. */
2971 rest_of_decl_compilation (decl, 0, 0);
2973 /* Write a record describing this implicit function declaration
2974 to the prototypes file (if requested). */
2975 gen_aux_info_record (decl, 0, 1, 0);
2977 /* Possibly apply some default attributes to this implicit declaration. */
2978 decl_attributes (&decl, NULL_TREE, 0);
2983 /* Issue an error message for a reference to an undeclared variable
2984 ID, including a reference to a builtin outside of function-call
2985 context. Establish a binding of the identifier to error_mark_node
2986 in an appropriate scope, which will suppress further errors for the
2987 same identifier. The error message should be given location LOC. */
2989 undeclared_variable (location_t loc, tree id)
2991 static bool already = false;
2992 struct c_scope *scope;
2994 if (current_function_decl == 0)
2996 error_at (loc, "%qE undeclared here (not in a function)", id);
2997 scope = current_scope;
3001 if (!objc_diagnose_private_ivar (id))
3002 error_at (loc, "%qE undeclared (first use in this function)", id);
3005 inform (loc, "each undeclared identifier is reported only"
3006 " once for each function it appears in");
3010 /* If we are parsing old-style parameter decls, current_function_decl
3011 will be nonnull but current_function_scope will be null. */
3012 scope = current_function_scope ? current_function_scope : current_scope;
3014 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3018 /* Subroutine of lookup_label, declare_label, define_label: construct a
3019 LABEL_DECL with all the proper frills. Also create a struct
3020 c_label_vars initialized for the current scope. */
3023 make_label (location_t location, tree name, bool defining,
3024 struct c_label_vars **p_label_vars)
3026 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3027 struct c_label_vars *label_vars;
3029 DECL_CONTEXT (label) = current_function_decl;
3030 DECL_MODE (label) = VOIDmode;
3032 label_vars = ggc_alloc_c_label_vars ();
3033 label_vars->shadowed = NULL;
3034 set_spot_bindings (&label_vars->label_bindings, defining);
3035 label_vars->decls_in_scope = make_tree_vector ();
3036 label_vars->gotos = VEC_alloc (c_goto_bindings_p, gc, 0);
3037 *p_label_vars = label_vars;
3042 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3043 Create one if none exists so far for the current function.
3044 This is called when a label is used in a goto expression or
3045 has its address taken. */
3048 lookup_label (tree name)
3051 struct c_label_vars *label_vars;
3053 if (current_function_scope == 0)
3055 error ("label %qE referenced outside of any function", name);
3059 /* Use a label already defined or ref'd with this name, but not if
3060 it is inherited from a containing function and wasn't declared
3062 label = I_LABEL_DECL (name);
3063 if (label && (DECL_CONTEXT (label) == current_function_decl
3064 || C_DECLARED_LABEL_FLAG (label)))
3066 /* If the label has only been declared, update its apparent
3067 location to point here, for better diagnostics if it
3068 turns out not to have been defined. */
3069 if (DECL_INITIAL (label) == NULL_TREE)
3070 DECL_SOURCE_LOCATION (label) = input_location;
3074 /* No label binding for that identifier; make one. */
3075 label = make_label (input_location, name, false, &label_vars);
3077 /* Ordinary labels go in the current function scope. */
3078 bind_label (name, label, current_function_scope, label_vars);
3083 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3087 warn_about_goto (location_t goto_loc, tree label, tree decl)
3089 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3091 "jump into scope of identifier with variably modified type");
3093 warning_at (goto_loc, OPT_Wjump_misses_init,
3094 "jump skips variable initialization");
3095 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3096 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3099 /* Look up a label because of a goto statement. This is like
3100 lookup_label, but also issues any appropriate warnings. */
3103 lookup_label_for_goto (location_t loc, tree name)
3106 struct c_label_vars *label_vars;
3110 label = lookup_label (name);
3111 if (label == NULL_TREE)
3114 /* If we are jumping to a different function, we can't issue any
3116 if (DECL_CONTEXT (label) != current_function_decl)
3118 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3122 label_vars = I_LABEL_BINDING (name)->u.label;
3124 /* If the label has not yet been defined, then push this goto on a
3125 list for possible later warnings. */
3126 if (label_vars->label_bindings.scope == NULL)
3128 struct c_goto_bindings *g;
3130 g = ggc_alloc_c_goto_bindings ();
3132 set_spot_bindings (&g->goto_bindings, true);
3133 VEC_safe_push (c_goto_bindings_p, gc, label_vars->gotos, g);
3137 /* If there are any decls in label_vars->decls_in_scope, then this
3138 goto has missed the declaration of the decl. This happens for a
3144 Issue a warning or error. */
3145 FOR_EACH_VEC_ELT (tree, label_vars->decls_in_scope, ix, decl)
3146 warn_about_goto (loc, label, decl);
3148 if (label_vars->label_bindings.left_stmt_expr)
3150 error_at (loc, "jump into statement expression");
3151 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3157 /* Make a label named NAME in the current function, shadowing silently
3158 any that may be inherited from containing functions or containing
3159 scopes. This is called for __label__ declarations. */
3162 declare_label (tree name)
3164 struct c_binding *b = I_LABEL_BINDING (name);
3166 struct c_label_vars *label_vars;
3168 /* Check to make sure that the label hasn't already been declared
3170 if (b && B_IN_CURRENT_SCOPE (b))
3172 error ("duplicate label declaration %qE", name);
3173 locate_old_decl (b->decl);
3175 /* Just use the previous declaration. */
3179 label = make_label (input_location, name, false, &label_vars);
3180 C_DECLARED_LABEL_FLAG (label) = 1;
3182 /* Declared labels go in the current scope. */
3183 bind_label (name, label, current_scope, label_vars);
3188 /* When we define a label, issue any appropriate warnings if there are
3189 any gotos earlier in the function which jump to this label. */
3192 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3195 struct c_goto_bindings *g;
3197 FOR_EACH_VEC_ELT (c_goto_bindings_p, label_vars->gotos, ix, g)
3199 struct c_binding *b;
3200 struct c_scope *scope;
3202 /* We have a goto to this label. The goto is going forward. In
3203 g->scope, the goto is going to skip any binding which was
3204 defined after g->bindings_in_scope. */
3205 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3207 for (b = g->goto_bindings.scope->bindings;
3208 b != g->goto_bindings.bindings_in_scope;
3211 if (decl_jump_unsafe (b->decl))
3212 warn_about_goto (g->loc, label, b->decl);
3216 /* We also need to warn about decls defined in any scopes
3217 between the scope of the label and the scope of the goto. */
3218 for (scope = label_vars->label_bindings.scope;
3219 scope != g->goto_bindings.scope;
3220 scope = scope->outer)
3222 gcc_assert (scope != NULL);
3223 if (scope->has_jump_unsafe_decl)
3225 if (scope == label_vars->label_bindings.scope)
3226 b = label_vars->label_bindings.bindings_in_scope;
3228 b = scope->bindings;
3229 for (; b != NULL; b = b->prev)
3231 if (decl_jump_unsafe (b->decl))
3232 warn_about_goto (g->loc, label, b->decl);
3237 if (g->goto_bindings.stmt_exprs > 0)
3239 error_at (g->loc, "jump into statement expression");
3240 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3245 /* Now that the label is defined, we will issue warnings about
3246 subsequent gotos to this label when we see them. */
3247 VEC_truncate (c_goto_bindings_p, label_vars->gotos, 0);
3248 label_vars->gotos = NULL;
3251 /* Define a label, specifying the location in the source file.
3252 Return the LABEL_DECL node for the label, if the definition is valid.
3253 Otherwise return 0. */
3256 define_label (location_t location, tree name)
3258 /* Find any preexisting label with this name. It is an error
3259 if that label has already been defined in this function, or
3260 if there is a containing function with a declared label with
3262 tree label = I_LABEL_DECL (name);
3265 && ((DECL_CONTEXT (label) == current_function_decl
3266 && DECL_INITIAL (label) != 0)
3267 || (DECL_CONTEXT (label) != current_function_decl
3268 && C_DECLARED_LABEL_FLAG (label))))
3270 error_at (location, "duplicate label %qD", label);
3271 locate_old_decl (label);
3274 else if (label && DECL_CONTEXT (label) == current_function_decl)
3276 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3278 /* The label has been used or declared already in this function,
3279 but not defined. Update its location to point to this
3281 DECL_SOURCE_LOCATION (label) = location;
3282 set_spot_bindings (&label_vars->label_bindings, true);
3284 /* Issue warnings as required about any goto statements from
3285 earlier in the function. */
3286 check_earlier_gotos (label, label_vars);
3290 struct c_label_vars *label_vars;
3292 /* No label binding for that identifier; make one. */
3293 label = make_label (location, name, true, &label_vars);
3295 /* Ordinary labels go in the current function scope. */
3296 bind_label (name, label, current_function_scope, label_vars);
3299 if (!in_system_header && lookup_name (name))
3300 warning_at (location, OPT_Wtraditional,
3301 "traditional C lacks a separate namespace "
3302 "for labels, identifier %qE conflicts", name);
3304 /* Mark label as having been defined. */
3305 DECL_INITIAL (label) = error_mark_node;
3309 /* Get the bindings for a new switch statement. This is used to issue
3310 warnings as appropriate for jumps from the switch to case or
3313 struct c_spot_bindings *
3314 c_get_switch_bindings (void)
3316 struct c_spot_bindings *switch_bindings;
3318 switch_bindings = XNEW (struct c_spot_bindings);
3319 set_spot_bindings (switch_bindings, true);
3320 return switch_bindings;
3324 c_release_switch_bindings (struct c_spot_bindings *bindings)
3326 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3330 /* This is called at the point of a case or default label to issue
3331 warnings about decls as needed. It returns true if it found an
3332 error, not just a warning. */
3335 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3336 location_t switch_loc, location_t case_loc)
3339 struct c_scope *scope;
3342 for (scope = current_scope;
3343 scope != switch_bindings->scope;
3344 scope = scope->outer)
3346 struct c_binding *b;
3348 gcc_assert (scope != NULL);
3350 if (!scope->has_jump_unsafe_decl)
3353 for (b = scope->bindings; b != NULL; b = b->prev)
3355 if (decl_jump_unsafe (b->decl))
3357 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3361 ("switch jumps into scope of identifier with "
3362 "variably modified type"));
3365 warning_at (case_loc, OPT_Wjump_misses_init,
3366 "switch jumps over variable initialization");
3367 inform (switch_loc, "switch starts here");
3368 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3374 if (switch_bindings->stmt_exprs > 0)
3377 error_at (case_loc, "switch jumps into statement expression");
3378 inform (switch_loc, "switch starts here");
3384 /* Given NAME, an IDENTIFIER_NODE,
3385 return the structure (or union or enum) definition for that name.
3386 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3387 CODE says which kind of type the caller wants;
3388 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3389 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3390 location where the tag was defined.
3391 If the wrong kind of type is found, an error is reported. */
3394 lookup_tag (enum tree_code code, tree name, int thislevel_only,
3397 struct c_binding *b = I_TAG_BINDING (name);
3403 /* We only care about whether it's in this level if
3404 thislevel_only was set or it might be a type clash. */
3405 if (thislevel_only || TREE_CODE (b->decl) != code)
3407 /* For our purposes, a tag in the external scope is the same as
3408 a tag in the file scope. (Primarily relevant to Objective-C
3409 and its builtin structure tags, which get pushed before the
3410 file scope is created.) */
3411 if (B_IN_CURRENT_SCOPE (b)
3412 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3416 if (thislevel_only && !thislevel)
3419 if (TREE_CODE (b->decl) != code)
3421 /* Definition isn't the kind we were looking for. */
3422 pending_invalid_xref = name;
3423 pending_invalid_xref_location = input_location;
3425 /* If in the same binding level as a declaration as a tag
3426 of a different type, this must not be allowed to
3427 shadow that tag, so give the error immediately.
3428 (For example, "struct foo; union foo;" is invalid.) */
3430 pending_xref_error ();
3439 /* Print an error message now
3440 for a recent invalid struct, union or enum cross reference.
3441 We don't print them immediately because they are not invalid
3442 when used in the `struct foo;' construct for shadowing. */
3445 pending_xref_error (void)
3447 if (pending_invalid_xref != 0)
3448 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3449 pending_invalid_xref);
3450 pending_invalid_xref = 0;
3454 /* Look up NAME in the current scope and its superiors
3455 in the namespace of variables, functions and typedefs.
3456 Return a ..._DECL node of some kind representing its definition,
3457 or return 0 if it is undefined. */
3460 lookup_name (tree name)
3462 struct c_binding *b = I_SYMBOL_BINDING (name);
3463 if (b && !b->invisible)
3465 maybe_record_typedef_use (b->decl);
3471 /* Similar to `lookup_name' but look only at the indicated scope. */
3474 lookup_name_in_scope (tree name, struct c_scope *scope)
3476 struct c_binding *b;
3478 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3479 if (B_IN_SCOPE (b, scope))
3484 /* Create the predefined scalar types of C,
3485 and some nodes representing standard constants (0, 1, (void *) 0).
3486 Initialize the global scope.
3487 Make definitions for built-in primitive functions. */
3490 c_init_decl_processing (void)
3492 location_t save_loc = input_location;
3494 /* Initialize reserved words for parser. */
3497 current_function_decl = 0;
3499 gcc_obstack_init (&parser_obstack);
3501 /* Make the externals scope. */
3503 external_scope = current_scope;
3505 /* Declarations from c_common_nodes_and_builtins must not be associated
3506 with this input file, lest we get differences between using and not
3507 using preprocessed headers. */
3508 input_location = BUILTINS_LOCATION;
3510 c_common_nodes_and_builtins ();
3512 /* In C, comparisons and TRUTH_* expressions have type int. */
3513 truthvalue_type_node = integer_type_node;
3514 truthvalue_true_node = integer_one_node;
3515 truthvalue_false_node = integer_zero_node;
3517 /* Even in C99, which has a real boolean type. */
3518 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
3519 boolean_type_node));
3521 input_location = save_loc;
3523 pedantic_lvalues = true;
3525 make_fname_decl = c_make_fname_decl;
3526 start_fname_decls ();
3529 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
3530 give the decl, NAME is the initialization string and TYPE_DEP
3531 indicates whether NAME depended on the type of the function. As we
3532 don't yet implement delayed emission of static data, we mark the
3533 decl as emitted so it is not placed in the output. Anything using
3534 it must therefore pull out the STRING_CST initializer directly.
3538 c_make_fname_decl (location_t loc, tree id, int type_dep)
3540 const char *name = fname_as_string (type_dep);
3541 tree decl, type, init;
3542 size_t length = strlen (name);
3544 type = build_array_type (char_type_node,
3545 build_index_type (size_int (length)));
3546 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
3548 decl = build_decl (loc, VAR_DECL, id, type);
3550 TREE_STATIC (decl) = 1;
3551 TREE_READONLY (decl) = 1;
3552 DECL_ARTIFICIAL (decl) = 1;
3554 init = build_string (length + 1, name);
3555 free (CONST_CAST (char *, name));
3556 TREE_TYPE (init) = type;
3557 DECL_INITIAL (decl) = init;
3559 TREE_USED (decl) = 1;
3561 if (current_function_decl
3562 /* For invalid programs like this:
3565 const char* p = __FUNCTION__;
3567 the __FUNCTION__ is believed to appear in K&R style function
3568 parameter declarator. In that case we still don't have
3570 && (!seen_error () || current_function_scope))
3572 DECL_CONTEXT (decl) = current_function_decl;
3573 bind (id, decl, current_function_scope,
3574 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
3577 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
3583 c_builtin_function (tree decl)
3585 tree type = TREE_TYPE (decl);
3586 tree id = DECL_NAME (decl);
3588 const char *name = IDENTIFIER_POINTER (id);
3589 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
3591 /* Should never be called on a symbol with a preexisting meaning. */
3592 gcc_assert (!I_SYMBOL_BINDING (id));
3594 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
3597 /* Builtins in the implementation namespace are made visible without
3598 needing to be explicitly declared. See push_file_scope. */
3599 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3601 DECL_CHAIN (decl) = visible_builtins;
3602 visible_builtins = decl;
3609 c_builtin_function_ext_scope (tree decl)
3611 tree type = TREE_TYPE (decl);
3612 tree id = DECL_NAME (decl);
3614 const char *name = IDENTIFIER_POINTER (id);
3615 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
3617 /* Should never be called on a symbol with a preexisting meaning. */
3618 gcc_assert (!I_SYMBOL_BINDING (id));
3620 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
3623 /* Builtins in the implementation namespace are made visible without
3624 needing to be explicitly declared. See push_file_scope. */
3625 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3627 DECL_CHAIN (decl) = visible_builtins;
3628 visible_builtins = decl;
3634 /* Called when a declaration is seen that contains no names to declare.
3635 If its type is a reference to a structure, union or enum inherited
3636 from a containing scope, shadow that tag name for the current scope
3637 with a forward reference.
3638 If its type defines a new named structure or union
3639 or defines an enum, it is valid but we need not do anything here.
3640 Otherwise, it is an error. */
3643 shadow_tag (const struct c_declspecs *declspecs)
3645 shadow_tag_warned (declspecs, 0);
3648 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
3651 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
3653 bool found_tag = false;
3655 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
3657 tree value = declspecs->type;
3658 enum tree_code code = TREE_CODE (value);
3660 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
3661 /* Used to test also that TYPE_SIZE (value) != 0.
3662 That caused warning for `struct foo;' at top level in the file. */
3664 tree name = TYPE_NAME (value);
3669 if (declspecs->restrict_p)
3671 error ("invalid use of %<restrict%>");
3677 if (warned != 1 && code != ENUMERAL_TYPE)
3678 /* Empty unnamed enum OK */
3680 pedwarn (input_location, 0,
3681 "unnamed struct/union that defines no instances");
3685 else if (declspecs->typespec_kind != ctsk_tagdef
3686 && declspecs->typespec_kind != ctsk_tagfirstref
3687 && declspecs->storage_class != csc_none)
3690 pedwarn (input_location, 0,
3691 "empty declaration with storage class specifier "
3692 "does not redeclare tag");
3694 pending_xref_error ();
3696 else if (declspecs->typespec_kind != ctsk_tagdef
3697 && declspecs->typespec_kind != ctsk_tagfirstref
3698 && (declspecs->const_p
3699 || declspecs->volatile_p
3700 || declspecs->restrict_p
3701 || declspecs->address_space))
3704 pedwarn (input_location, 0,
3705 "empty declaration with type qualifier "
3706 "does not redeclare tag");
3708 pending_xref_error ();
3710 else if (declspecs->typespec_kind != ctsk_tagdef
3711 && declspecs->typespec_kind != ctsk_tagfirstref
3712 && declspecs->alignas_p)
3715 pedwarn (input_location, 0,
3716 "empty declaration with %<_Alignas%> "
3717 "does not redeclare tag");
3719 pending_xref_error ();
3723 pending_invalid_xref = 0;
3724 t = lookup_tag (code, name, 1, NULL);
3728 t = make_node (code);
3729 pushtag (input_location, name, t);
3735 if (warned != 1 && !in_system_header)
3737 pedwarn (input_location, 0,
3738 "useless type name in empty declaration");
3743 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
3745 pedwarn (input_location, 0, "useless type name in empty declaration");
3749 pending_invalid_xref = 0;
3751 if (declspecs->inline_p)
3753 error ("%<inline%> in empty declaration");
3757 if (declspecs->noreturn_p)
3759 error ("%<_Noreturn%> in empty declaration");
3763 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3765 error ("%<auto%> in file-scope empty declaration");
3769 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3771 error ("%<register%> in file-scope empty declaration");
3775 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3777 warning (0, "useless storage class specifier in empty declaration");
3781 if (!warned && !in_system_header && declspecs->thread_p)
3783 warning (0, "useless %<__thread%> in empty declaration");
3787 if (!warned && !in_system_header && (declspecs->const_p
3788 || declspecs->volatile_p
3789 || declspecs->restrict_p
3790 || declspecs->address_space))
3792 warning (0, "useless type qualifier in empty declaration");
3796 if (!warned && !in_system_header && declspecs->alignas_p)
3798 warning (0, "useless %<_Alignas%> in empty declaration");
3805 pedwarn (input_location, 0, "empty declaration");
3810 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3811 bits. SPECS represents declaration specifiers that the grammar
3812 only permits to contain type qualifiers and attributes. */
3815 quals_from_declspecs (const struct c_declspecs *specs)
3817 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3818 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3819 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
3820 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
3821 gcc_assert (!specs->type
3822 && !specs->decl_attr
3823 && specs->typespec_word == cts_none
3824 && specs->storage_class == csc_none
3825 && !specs->typedef_p
3826 && !specs->explicit_signed_p
3827 && !specs->deprecated_p
3829 && !specs->long_long_p
3832 && !specs->unsigned_p
3833 && !specs->complex_p
3835 && !specs->noreturn_p
3836 && !specs->thread_p);
3840 /* Construct an array declarator. LOC is the location of the
3841 beginning of the array (usually the opening brace). EXPR is the
3842 expression inside [], or NULL_TREE. QUALS are the type qualifiers
3843 inside the [] (to be applied to the pointer to which a parameter
3844 array is converted). STATIC_P is true if "static" is inside the
3845 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
3846 VLA of unspecified length which is nevertheless a complete type,
3847 false otherwise. The field for the contained declarator is left to
3848 be filled in by set_array_declarator_inner. */
3850 struct c_declarator *
3851 build_array_declarator (location_t loc,
3852 tree expr, struct c_declspecs *quals, bool static_p,
3855 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3856 struct c_declarator);
3857 declarator->id_loc = loc;
3858 declarator->kind = cdk_array;
3859 declarator->declarator = 0;
3860 declarator->u.array.dimen = expr;
3863 declarator->u.array.attrs = quals->attrs;
3864 declarator->u.array.quals = quals_from_declspecs (quals);
3868 declarator->u.array.attrs = NULL_TREE;
3869 declarator->u.array.quals = 0;
3871 declarator->u.array.static_p = static_p;
3872 declarator->u.array.vla_unspec_p = vla_unspec_p;
3875 if (static_p || quals != NULL)
3876 pedwarn (loc, OPT_pedantic,
3877 "ISO C90 does not support %<static%> or type "
3878 "qualifiers in parameter array declarators");
3880 pedwarn (loc, OPT_pedantic,
3881 "ISO C90 does not support %<[*]%> array declarators");
3885 if (!current_scope->parm_flag)
3888 error_at (loc, "%<[*]%> not allowed in other than "
3889 "function prototype scope");
3890 declarator->u.array.vla_unspec_p = false;
3893 current_scope->had_vla_unspec = true;
3898 /* Set the contained declarator of an array declarator. DECL is the
3899 declarator, as constructed by build_array_declarator; INNER is what
3900 appears on the left of the []. */
3902 struct c_declarator *
3903 set_array_declarator_inner (struct c_declarator *decl,
3904 struct c_declarator *inner)
3906 decl->declarator = inner;
3910 /* INIT is a constructor that forms DECL's initializer. If the final
3911 element initializes a flexible array field, add the size of that
3912 initializer to DECL's size. */
3915 add_flexible_array_elts_to_size (tree decl, tree init)
3919 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3922 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3923 type = TREE_TYPE (elt);
3924 if (TREE_CODE (type) == ARRAY_TYPE
3925 && TYPE_SIZE (type) == NULL_TREE
3926 && TYPE_DOMAIN (type) != NULL_TREE
3927 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3929 complete_array_type (&type, elt, false);
3931 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3932 DECL_SIZE_UNIT (decl)
3933 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3937 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3938 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3939 before the type name, and set *EXPR_CONST_OPERANDS, if
3940 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3941 appear in a constant expression. */
3944 groktypename (struct c_type_name *type_name, tree *expr,
3945 bool *expr_const_operands)
3948 tree attrs = type_name->specs->attrs;
3950 type_name->specs->attrs = NULL_TREE;
3952 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3953 false, NULL, &attrs, expr, expr_const_operands,
3956 /* Apply attributes. */
3957 decl_attributes (&type, attrs, 0);
3962 /* Decode a declarator in an ordinary declaration or data definition.
3963 This is called as soon as the type information and variable name
3964 have been parsed, before parsing the initializer if any.
3965 Here we create the ..._DECL node, fill in its type,
3966 and put it on the list of decls for the current context.
3967 The ..._DECL node is returned as the value.
3969 Exception: for arrays where the length is not specified,
3970 the type is left null, to be filled in by `finish_decl'.
3972 Function definitions do not come here; they go to start_function
3973 instead. However, external and forward declarations of functions
3974 do go through here. Structure field declarations are done by
3975 grokfield and not through here. */
3978 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3979 bool initialized, tree attributes)
3983 tree expr = NULL_TREE;
3984 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3986 /* An object declared as __attribute__((deprecated)) suppresses
3987 warnings of uses of other deprecated items. */
3988 if (lookup_attribute ("deprecated", attributes))
3989 deprecated_state = DEPRECATED_SUPPRESS;
3991 decl = grokdeclarator (declarator, declspecs,
3992 NORMAL, initialized, NULL, &attributes, &expr, NULL,
3998 add_stmt (fold_convert (void_type_node, expr));
4000 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4001 warning (OPT_Wmain, "%q+D is usually a function", decl);
4004 /* Is it valid for this decl to have an initializer at all?
4005 If not, set INITIALIZED to zero, which will indirectly
4006 tell 'finish_decl' to ignore the initializer once it is parsed. */
4007 switch (TREE_CODE (decl))
4010 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4015 error ("function %qD is initialized like a variable", decl);
4020 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4021 error ("parameter %qD is initialized", decl);
4026 /* Don't allow initializations for incomplete types except for
4027 arrays which might be completed by the initialization. */
4029 /* This can happen if the array size is an undefined macro.
4030 We already gave a warning, so we don't need another one. */
4031 if (TREE_TYPE (decl) == error_mark_node)
4033 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4035 /* A complete type is ok if size is fixed. */
4037 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4038 || C_DECL_VARIABLE_SIZE (decl))
4040 error ("variable-sized object may not be initialized");
4044 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4046 error ("variable %qD has initializer but incomplete type", decl);
4049 else if (C_DECL_VARIABLE_SIZE (decl))
4051 /* Although C99 is unclear about whether incomplete arrays
4052 of VLAs themselves count as VLAs, it does not make
4053 sense to permit them to be initialized given that
4054 ordinary VLAs may not be initialized. */
4055 error ("variable-sized object may not be initialized");
4062 if (current_scope == file_scope)
4063 TREE_STATIC (decl) = 1;
4065 /* Tell 'pushdecl' this is an initialized decl
4066 even though we don't yet have the initializer expression.
4067 Also tell 'finish_decl' it may store the real initializer. */
4068 DECL_INITIAL (decl) = error_mark_node;
4071 /* If this is a function declaration, write a record describing it to the
4072 prototypes file (if requested). */
4074 if (TREE_CODE (decl) == FUNCTION_DECL)
4075 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4077 /* ANSI specifies that a tentative definition which is not merged with
4078 a non-tentative definition behaves exactly like a definition with an
4079 initializer equal to zero. (Section 3.7.2)
4081 -fno-common gives strict ANSI behavior, though this tends to break
4082 a large body of code that grew up without this rule.
4084 Thread-local variables are never common, since there's no entrenched
4085 body of code to break, and it allows more efficient variable references
4086 in the presence of dynamic linking. */
4088 if (TREE_CODE (decl) == VAR_DECL
4090 && TREE_PUBLIC (decl)
4091 && !DECL_THREAD_LOCAL_P (decl)
4093 DECL_COMMON (decl) = 1;
4095 /* Set attributes here so if d