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
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"
52 #include "langhooks.h"
53 #include "tree-mudflap.h"
54 #include "gimple.h" /* FIXME: For gimple_set_body and gimple_body, but why? */
55 #include "tree-iterator.h"
56 #include "diagnostic.h"
57 #include "tree-dump.h"
60 #include "langhooks-def.h"
61 #include "pointer-set.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 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
123 included in this invocation. Note that the current translation
124 unit is not included in this list. */
126 static GTY(()) tree all_translation_units;
128 /* A list of decls to be made automatically visible in each file scope. */
129 static GTY(()) tree visible_builtins;
131 /* Set to 0 at beginning of a function definition, set to 1 if
132 a return statement that specifies a return value is seen. */
134 int current_function_returns_value;
136 /* Set to 0 at beginning of a function definition, set to 1 if
137 a return statement with no argument is seen. */
139 int current_function_returns_null;
141 /* Set to 0 at beginning of a function definition, set to 1 if
142 a call to a noreturn function is seen. */
144 int current_function_returns_abnormally;
146 /* Set to nonzero by `grokdeclarator' for a function
147 whose return type is defaulted, if warnings for this are desired. */
149 static int warn_about_return_type;
151 /* Nonzero when the current toplevel function contains a declaration
152 of a nested function which is never defined. */
154 static bool undef_nested_function;
156 /* True means global_bindings_p should return false even if the scope stack
157 says we are in file scope. */
158 bool c_override_global_bindings_to_false;
161 /* Each c_binding structure describes one binding of an identifier to
162 a decl. All the decls in a scope - irrespective of namespace - are
163 chained together by the ->prev field, which (as the name implies)
164 runs in reverse order. All the decls in a given namespace bound to
165 a given identifier are chained by the ->shadowed field, which runs
166 from inner to outer scopes.
168 The ->decl field usually points to a DECL node, but there are two
169 exceptions. In the namespace of type tags, the bound entity is a
170 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
171 identifier is encountered, it is bound to error_mark_node to
172 suppress further errors about that identifier in the current
175 The ->u.type field stores the type of the declaration in this scope;
176 if NULL, the type is the type of the ->decl field. This is only of
177 relevance for objects with external or internal linkage which may
178 be redeclared in inner scopes, forming composite types that only
179 persist for the duration of those scopes. In the external scope,
180 this stores the composite of all the types declared for this
181 object, visible or not. The ->inner_comp field (used only at file
182 scope) stores whether an incomplete array type at file scope was
183 completed at an inner scope to an array size other than 1.
185 The ->u.label field is used for labels. It points to a structure
186 which stores additional information used for warnings.
188 The depth field is copied from the scope structure that holds this
189 decl. It is used to preserve the proper ordering of the ->shadowed
190 field (see bind()) and also for a handful of special-case checks.
191 Finally, the invisible bit is true for a decl which should be
192 ignored for purposes of normal name lookup, and the nested bit is
193 true for a decl that's been bound a second time in an inner scope;
194 in all such cases, the binding in the outer scope will have its
195 invisible bit true. */
197 struct GTY((chain_next ("%h.prev"))) c_binding {
198 union GTY(()) { /* first so GTY desc can use decl */
199 tree GTY((tag ("0"))) type; /* the type in this scope */
200 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
201 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
202 tree decl; /* the decl bound */
203 tree id; /* the identifier it's bound to */
204 struct c_binding *prev; /* the previous decl in this scope */
205 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
206 unsigned int depth : 28; /* depth of this scope */
207 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
208 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
209 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
210 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
211 location_t locus; /* location for nested bindings */
213 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
214 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
215 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
216 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
218 #define I_SYMBOL_BINDING(node) \
219 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
220 #define I_SYMBOL_DECL(node) \
221 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
223 #define I_TAG_BINDING(node) \
224 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
225 #define I_TAG_DECL(node) \
226 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
228 #define I_LABEL_BINDING(node) \
229 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
230 #define I_LABEL_DECL(node) \
231 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
233 /* Each C symbol points to three linked lists of c_binding structures.
234 These describe the values of the identifier in the three different
235 namespaces defined by the language. */
237 struct GTY(()) lang_identifier {
238 struct c_common_identifier common_id;
239 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
240 struct c_binding *tag_binding; /* struct/union/enum tags */
241 struct c_binding *label_binding; /* labels */
244 /* Validate c-lang.c's assumptions. */
245 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
246 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
248 /* The resulting tree type. */
250 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
251 chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *) TYPE_NEXT_VARIANT (&%h.generic) : ((union lang_tree_node *) TREE_CHAIN (&%h.generic))"))) lang_tree_node
253 union tree_node GTY ((tag ("0"),
254 desc ("tree_node_structure (&%h)")))
256 struct lang_identifier GTY ((tag ("1"))) identifier;
259 /* Track bindings and other things that matter for goto warnings. For
260 efficiency, we do not gather all the decls at the point of
261 definition. Instead, we point into the bindings structure. As
262 scopes are popped, we update these structures and gather the decls
263 that matter at that time. */
265 struct GTY(()) c_spot_bindings {
266 /* The currently open scope which holds bindings defined when the
267 label was defined or the goto statement was found. */
268 struct c_scope *scope;
269 /* The bindings in the scope field which were defined at the point
270 of the label or goto. This lets us look at older or newer
271 bindings in the scope, as appropriate. */
272 struct c_binding *bindings_in_scope;
273 /* The number of statement expressions that have started since this
274 label or goto statement was defined. This is zero if we are at
275 the same statement expression level. It is positive if we are in
276 a statement expression started since this spot. It is negative
277 if this spot was in a statement expression and we have left
280 /* Whether we started in a statement expression but are no longer in
281 it. This is set to true if stmt_exprs ever goes negative. */
285 /* This structure is used to keep track of bindings seen when a goto
286 statement is defined. This is only used if we see the goto
287 statement before we see the label. */
289 struct GTY(()) c_goto_bindings {
290 /* The location of the goto statement. */
292 /* The bindings of the goto statement. */
293 struct c_spot_bindings goto_bindings;
296 typedef struct c_goto_bindings *c_goto_bindings_p;
297 DEF_VEC_P(c_goto_bindings_p);
298 DEF_VEC_ALLOC_P(c_goto_bindings_p,gc);
300 /* The additional information we keep track of for a label binding.
301 These fields are updated as scopes are popped. */
303 struct GTY(()) c_label_vars {
304 /* The shadowed c_label_vars, when one label shadows another (which
305 can only happen using a __label__ declaration). */
306 struct c_label_vars *shadowed;
307 /* The bindings when the label was defined. */
308 struct c_spot_bindings label_bindings;
309 /* A list of decls that we care about: decls about which we should
310 warn if a goto branches to this label from later in the function.
311 Decls are added to this list as scopes are popped. We only add
312 the decls that matter. */
313 VEC(tree,gc) *decls_in_scope;
314 /* A list of goto statements to this label. This is only used for
315 goto statements seen before the label was defined, so that we can
316 issue appropriate warnings for them. */
317 VEC(c_goto_bindings_p,gc) *gotos;
320 /* Each c_scope structure describes the complete contents of one
321 scope. Four scopes are distinguished specially: the innermost or
322 current scope, the innermost function scope, the file scope (always
323 the second to outermost) and the outermost or external scope.
325 Most declarations are recorded in the current scope.
327 All normal label declarations are recorded in the innermost
328 function scope, as are bindings of undeclared identifiers to
329 error_mark_node. (GCC permits nested functions as an extension,
330 hence the 'innermost' qualifier.) Explicitly declared labels
331 (using the __label__ extension) appear in the current scope.
333 Being in the file scope (current_scope == file_scope) causes
334 special behavior in several places below. Also, under some
335 conditions the Objective-C front end records declarations in the
336 file scope even though that isn't the current scope.
338 All declarations with external linkage are recorded in the external
339 scope, even if they aren't visible there; this models the fact that
340 such declarations are visible to the entire program, and (with a
341 bit of cleverness, see pushdecl) allows diagnosis of some violations
342 of C99 6.2.2p7 and 6.2.7p2:
344 If, within the same translation unit, the same identifier appears
345 with both internal and external linkage, the behavior is
348 All declarations that refer to the same object or function shall
349 have compatible type; otherwise, the behavior is undefined.
351 Initially only the built-in declarations, which describe compiler
352 intrinsic functions plus a subset of the standard library, are in
355 The order of the blocks list matters, and it is frequently appended
356 to. To avoid having to walk all the way to the end of the list on
357 each insertion, or reverse the list later, we maintain a pointer to
358 the last list entry. (FIXME: It should be feasible to use a reversed
361 The bindings list is strictly in reverse order of declarations;
362 pop_scope relies on this. */
365 struct GTY((chain_next ("%h.outer"))) c_scope {
366 /* The scope containing this one. */
367 struct c_scope *outer;
369 /* The next outermost function scope. */
370 struct c_scope *outer_function;
372 /* All bindings in this scope. */
373 struct c_binding *bindings;
375 /* For each scope (except the global one), a chain of BLOCK nodes
376 for all the scopes that were entered and exited one level down. */
380 /* The depth of this scope. Used to keep the ->shadowed chain of
381 bindings sorted innermost to outermost. */
382 unsigned int depth : 28;
384 /* True if we are currently filling this scope with parameter
386 BOOL_BITFIELD parm_flag : 1;
388 /* True if we saw [*] in this scope. Used to give an error messages
389 if these appears in a function definition. */
390 BOOL_BITFIELD had_vla_unspec : 1;
392 /* True if we already complained about forward parameter decls
393 in this scope. This prevents double warnings on
394 foo (int a; int b; ...) */
395 BOOL_BITFIELD warned_forward_parm_decls : 1;
397 /* True if this is the outermost block scope of a function body.
398 This scope contains the parameters, the local variables declared
399 in the outermost block, and all the labels (except those in
400 nested functions, or declared at block scope with __label__). */
401 BOOL_BITFIELD function_body : 1;
403 /* True means make a BLOCK for this scope no matter what. */
404 BOOL_BITFIELD keep : 1;
406 /* True means that an unsuffixed float constant is _Decimal64. */
407 BOOL_BITFIELD float_const_decimal64 : 1;
409 /* True if this scope has any label bindings. This is used to speed
410 up searching for labels when popping scopes, particularly since
411 labels are normally only found at function scope. */
412 BOOL_BITFIELD has_label_bindings : 1;
415 /* The scope currently in effect. */
417 static GTY(()) struct c_scope *current_scope;
419 /* The innermost function scope. Ordinary (not explicitly declared)
420 labels, bindings to error_mark_node, and the lazily-created
421 bindings of __func__ and its friends get this scope. */
423 static GTY(()) struct c_scope *current_function_scope;
425 /* The C file scope. This is reset for each input translation unit. */
427 static GTY(()) struct c_scope *file_scope;
429 /* The outermost scope. This is used for all declarations with
430 external linkage, and only these, hence the name. */
432 static GTY(()) struct c_scope *external_scope;
434 /* A chain of c_scope structures awaiting reuse. */
436 static GTY((deletable)) struct c_scope *scope_freelist;
438 /* A chain of c_binding structures awaiting reuse. */
440 static GTY((deletable)) struct c_binding *binding_freelist;
442 /* Append VAR to LIST in scope SCOPE. */
443 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
444 struct c_scope *s_ = (scope); \
446 if (s_->list##_last) \
447 BLOCK_CHAIN (s_->list##_last) = d_; \
450 s_->list##_last = d_; \
453 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
454 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
455 struct c_scope *t_ = (tscope); \
456 struct c_scope *f_ = (fscope); \
458 BLOCK_CHAIN (t_->to##_last) = f_->from; \
461 t_->to##_last = f_->from##_last; \
464 /* A c_inline_static structure stores details of a static identifier
465 referenced in a definition of a function that may be an inline
466 definition if no subsequent declaration of that function uses
467 "extern" or does not use "inline". */
469 struct GTY((chain_next ("%h.next"))) c_inline_static {
470 /* The location for a diagnostic. */
473 /* The function that may be an inline definition. */
476 /* The object or function referenced. */
479 /* What sort of reference this is. */
480 enum c_inline_static_type type;
482 /* The next such structure or NULL. */
483 struct c_inline_static *next;
486 /* List of static identifiers used or referenced in functions that may
487 be inline definitions. */
488 static GTY(()) struct c_inline_static *c_inline_statics;
490 /* True means unconditionally make a BLOCK for the next scope pushed. */
492 static bool keep_next_level_flag;
494 /* True means the next call to push_scope will be the outermost scope
495 of a function body, so do not push a new scope, merely cease
496 expecting parameter decls. */
498 static bool next_is_function_body;
500 /* A VEC of pointers to c_binding structures. */
502 typedef struct c_binding *c_binding_ptr;
503 DEF_VEC_P(c_binding_ptr);
504 DEF_VEC_ALLOC_P(c_binding_ptr,heap);
506 /* Information that we keep for a struct or union while it is being
509 struct c_struct_parse_info
511 /* If warn_cxx_compat, a list of types defined within this
513 VEC(tree,heap) *struct_types;
514 /* If warn_cxx_compat, a list of field names which have bindings,
515 and which are defined in this struct, but which are not defined
516 in any enclosing struct. This is used to clear the in_struct
517 field of the c_bindings structure. */
518 VEC(c_binding_ptr,heap) *fields;
519 /* If warn_cxx_compat, a list of typedef names used when defining
520 fields in this struct. */
521 VEC(tree,heap) *typedefs_seen;
524 /* Information for the struct or union currently being parsed, or
525 NULL if not parsing a struct or union. */
526 static struct c_struct_parse_info *struct_parse_info;
528 /* Forward declarations. */
529 static tree lookup_name_in_scope (tree, struct c_scope *);
530 static tree c_make_fname_decl (location_t, tree, int);
531 static tree grokdeclarator (const struct c_declarator *,
532 struct c_declspecs *,
533 enum decl_context, bool, tree *, tree *, tree *,
534 bool *, enum deprecated_states);
535 static tree grokparms (struct c_arg_info *, bool);
536 static void layout_array_type (tree);
538 /* T is a statement. Add it to the statement-tree. This is the
539 C/ObjC version--C++ has a slightly different version of this
545 enum tree_code code = TREE_CODE (t);
547 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
549 if (!EXPR_HAS_LOCATION (t))
550 SET_EXPR_LOCATION (t, input_location);
553 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
554 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
556 /* Add T to the statement-tree. Non-side-effect statements need to be
557 recorded during statement expressions. */
558 append_to_statement_list_force (t, &cur_stmt_list);
565 c_print_identifier (FILE *file, tree node, int indent)
567 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
568 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
569 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
570 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
572 tree rid = ridpointers[C_RID_CODE (node)];
573 indent_to (file, indent + 4);
574 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
575 (void *) rid, IDENTIFIER_POINTER (rid));
579 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
580 which may be any of several kinds of DECL or TYPE or error_mark_node,
581 in the scope SCOPE. */
583 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
584 bool nested, location_t locus)
586 struct c_binding *b, **here;
588 if (binding_freelist)
590 b = binding_freelist;
591 binding_freelist = b->prev;
594 b = GGC_NEW (struct c_binding);
599 b->depth = scope->depth;
600 b->invisible = invisible;
608 b->prev = scope->bindings;
614 switch (TREE_CODE (decl))
616 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
619 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
625 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
631 /* Locate the appropriate place in the chain of shadowed decls
632 to insert this binding. Normally, scope == current_scope and
633 this does nothing. */
634 while (*here && (*here)->depth > scope->depth)
635 here = &(*here)->shadowed;
641 /* Clear the binding structure B, stick it on the binding_freelist,
642 and return the former value of b->prev. This is used by pop_scope
643 and get_parm_info to iterate destructively over all the bindings
644 from a given scope. */
645 static struct c_binding *
646 free_binding_and_advance (struct c_binding *b)
648 struct c_binding *prev = b->prev;
650 memset (b, 0, sizeof (struct c_binding));
651 b->prev = binding_freelist;
652 binding_freelist = b;
657 /* Bind a label. Like bind, but skip fields which aren't used for
658 labels, and add the LABEL_VARS value. */
660 bind_label (tree name, tree label, struct c_scope *scope,
661 struct c_label_vars *label_vars)
665 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
668 scope->has_label_bindings = true;
671 gcc_assert (b->decl == label);
672 label_vars->shadowed = b->u.label;
673 b->u.label = label_vars;
676 /* Hook called at end of compilation to assume 1 elt
677 for a file-scope tentative array defn that wasn't complete before. */
680 c_finish_incomplete_decl (tree decl)
682 if (TREE_CODE (decl) == VAR_DECL)
684 tree type = TREE_TYPE (decl);
685 if (type != error_mark_node
686 && TREE_CODE (type) == ARRAY_TYPE
687 && !DECL_EXTERNAL (decl)
688 && TYPE_DOMAIN (type) == 0)
690 warning_at (DECL_SOURCE_LOCATION (decl),
691 0, "array %q+D assumed to have one element", decl);
693 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
695 layout_decl (decl, 0);
700 /* Record that inline function FUNC contains a reference (location
701 LOC) to static DECL (file-scope or function-local according to
705 record_inline_static (location_t loc, tree func, tree decl,
706 enum c_inline_static_type type)
708 struct c_inline_static *csi = GGC_NEW (struct c_inline_static);
710 csi->function = func;
711 csi->static_decl = decl;
713 csi->next = c_inline_statics;
714 c_inline_statics = csi;
717 /* Check for references to static declarations in inline functions at
718 the end of the translation unit and diagnose them if the functions
719 are still inline definitions. */
722 check_inline_statics (void)
724 struct c_inline_static *csi;
725 for (csi = c_inline_statics; csi; csi = csi->next)
727 if (DECL_EXTERNAL (csi->function))
731 pedwarn (csi->location, 0,
732 "%qD is static but used in inline function %qD "
733 "which is not static", csi->static_decl, csi->function);
736 pedwarn (csi->location, 0,
737 "%q+D is static but declared in inline function %qD "
738 "which is not static", csi->static_decl, csi->function);
744 c_inline_statics = NULL;
747 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
748 for the current state, otherwise set it to uninitialized. */
751 set_spot_bindings (struct c_spot_bindings *p, bool defining)
755 p->scope = current_scope;
756 p->bindings_in_scope = current_scope->bindings;
761 p->bindings_in_scope = NULL;
764 p->left_stmt_expr = false;
767 /* Return true if we will want to say something if a goto statement
771 decl_jump_unsafe (tree decl)
773 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
776 /* Always warn about crossing variably modified types. */
777 if ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == TYPE_DECL)
778 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
781 /* Otherwise, only warn if -Wgoto-misses-init and this is an
782 initialized automatic decl. */
783 if (warn_jump_misses_init
784 && TREE_CODE (decl) == VAR_DECL
785 && !TREE_STATIC (decl)
786 && DECL_INITIAL (decl) != NULL_TREE)
792 /* Update spot bindings P as we pop out of SCOPE. Return true if we
793 should push decls for a label. */
796 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
798 if (p->scope != scope)
800 /* This label or goto is defined in some other scope, or it is a
801 label which is not yet defined. There is nothing to
806 /* Adjust the spot bindings to refer to the bindings already defined
807 in the enclosing scope. */
808 p->scope = scope->outer;
809 p->bindings_in_scope = p->scope->bindings;
814 /* The Objective-C front-end often needs to determine the current scope. */
817 objc_get_current_scope (void)
819 return current_scope;
822 /* The following function is used only by Objective-C. It needs to live here
823 because it accesses the innards of c_scope. */
826 objc_mark_locals_volatile (void *enclosing_blk)
828 struct c_scope *scope;
831 for (scope = current_scope;
832 scope && scope != enclosing_blk;
833 scope = scope->outer)
835 for (b = scope->bindings; b; b = b->prev)
836 objc_volatilize_decl (b->decl);
838 /* Do not climb up past the current function. */
839 if (scope->function_body)
844 /* Nonzero if we are currently in file scope. */
847 global_bindings_p (void)
849 return (current_scope == file_scope && !c_override_global_bindings_to_false
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_CNEW (struct 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;
979 struct c_goto_bindings *g;
981 if (TREE_CODE (b->decl) != LABEL_DECL)
983 label_vars = b->u.label;
985 b1 = label_vars->label_bindings.bindings_in_scope;
986 if (update_spot_bindings (scope, &label_vars->label_bindings))
988 /* This label is defined in this scope. */
989 for (; b1 != NULL; b1 = b1->prev)
991 /* A goto from later in the function to this
992 label will never see the initialization of
993 B1, if any. Save it to issue a warning if
995 if (decl_jump_unsafe (b1->decl))
996 VEC_safe_push (tree, gc, label_vars->decls_in_scope,
1001 /* Update the bindings of any goto statements associated
1004 VEC_iterate (c_goto_bindings_p, label_vars->gotos, ix, g);
1006 update_spot_bindings (scope, &g->goto_bindings);
1010 /* Don't search beyond the current function. */
1011 if (s == current_function_scope)
1018 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1021 set_type_context (tree type, tree context)
1023 for (type = TYPE_MAIN_VARIANT (type); type;
1024 type = TYPE_NEXT_VARIANT (type))
1025 TYPE_CONTEXT (type) = context;
1028 /* Exit a scope. Restore the state of the identifier-decl mappings
1029 that were in effect when this scope was entered. Return a BLOCK
1030 node containing all the DECLs in this scope that are of interest
1031 to debug info generation. */
1036 struct c_scope *scope = current_scope;
1037 tree block, context, p;
1038 struct c_binding *b;
1040 bool functionbody = scope->function_body;
1041 bool keep = functionbody || scope->keep || scope->bindings;
1043 update_label_decls (scope);
1045 /* If appropriate, create a BLOCK to record the decls for the life
1046 of this function. */
1050 block = make_node (BLOCK);
1051 BLOCK_SUBBLOCKS (block) = scope->blocks;
1052 TREE_USED (block) = 1;
1054 /* In each subblock, record that this is its superior. */
1055 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1056 BLOCK_SUPERCONTEXT (p) = block;
1058 BLOCK_VARS (block) = 0;
1061 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1062 scope must be set so that they point to the appropriate
1063 construct, i.e. either to the current FUNCTION_DECL node, or
1064 else to the BLOCK node we just constructed.
1066 Note that for tagged types whose scope is just the formal
1067 parameter list for some function type specification, we can't
1068 properly set their TYPE_CONTEXTs here, because we don't have a
1069 pointer to the appropriate FUNCTION_TYPE node readily available
1070 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1071 type nodes get set in `grokdeclarator' as soon as we have created
1072 the FUNCTION_TYPE node which will represent the "scope" for these
1073 "parameter list local" tagged types. */
1074 if (scope->function_body)
1075 context = current_function_decl;
1076 else if (scope == file_scope)
1078 tree file_decl = build_decl (UNKNOWN_LOCATION,
1079 TRANSLATION_UNIT_DECL, 0, 0);
1080 TREE_CHAIN (file_decl) = all_translation_units;
1081 all_translation_units = file_decl;
1082 context = file_decl;
1087 /* Clear all bindings in this scope. */
1088 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1091 switch (TREE_CODE (p))
1094 /* Warnings for unused labels, errors for undefined labels. */
1095 if (TREE_USED (p) && !DECL_INITIAL (p))
1097 error ("label %q+D used but not defined", p);
1098 DECL_INITIAL (p) = error_mark_node;
1101 warn_for_unused_label (p);
1103 /* Labels go in BLOCK_VARS. */
1104 TREE_CHAIN (p) = BLOCK_VARS (block);
1105 BLOCK_VARS (block) = p;
1106 gcc_assert (I_LABEL_BINDING (b->id) == b);
1107 I_LABEL_BINDING (b->id) = b->shadowed;
1109 /* Also pop back to the shadowed label_vars. */
1110 release_tree_vector (b->u.label->decls_in_scope);
1111 b->u.label = b->u.label->shadowed;
1117 set_type_context (p, context);
1119 /* Types may not have tag-names, in which case the type
1120 appears in the bindings list with b->id NULL. */
1123 gcc_assert (I_TAG_BINDING (b->id) == b);
1124 I_TAG_BINDING (b->id) = b->shadowed;
1129 /* Propagate TREE_ADDRESSABLE from nested functions to their
1130 containing functions. */
1131 if (!TREE_ASM_WRITTEN (p)
1132 && DECL_INITIAL (p) != 0
1133 && TREE_ADDRESSABLE (p)
1134 && DECL_ABSTRACT_ORIGIN (p) != 0
1135 && DECL_ABSTRACT_ORIGIN (p) != p)
1136 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1137 if (!DECL_EXTERNAL (p)
1138 && !DECL_INITIAL (p)
1139 && scope != file_scope
1140 && scope != external_scope)
1142 error ("nested function %q+D declared but never defined", p);
1143 undef_nested_function = true;
1145 else if (DECL_DECLARED_INLINE_P (p)
1147 && !DECL_INITIAL (p))
1149 /* C99 6.7.4p6: "a function with external linkage... declared
1150 with an inline function specifier ... shall also be defined
1151 in the same translation unit." */
1152 if (!flag_gnu89_inline)
1153 pedwarn (input_location, 0,
1154 "inline function %q+D declared but never defined", p);
1155 DECL_EXTERNAL (p) = 1;
1161 /* Warnings for unused variables. */
1162 if ((!TREE_USED (p) || !DECL_READ_P (p))
1163 && !TREE_NO_WARNING (p)
1164 && !DECL_IN_SYSTEM_HEADER (p)
1166 && !DECL_ARTIFICIAL (p)
1167 && scope != file_scope
1168 && scope != external_scope)
1171 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1172 else if (DECL_CONTEXT (p) == current_function_decl)
1173 warning_at (DECL_SOURCE_LOCATION (p),
1174 OPT_Wunused_but_set_variable,
1175 "variable %qD set but not used", p);
1180 error ("type of array %q+D completed incompatibly with"
1181 " implicit initialization", p);
1188 /* All of these go in BLOCK_VARS, but only if this is the
1189 binding in the home scope. */
1192 TREE_CHAIN (p) = BLOCK_VARS (block);
1193 BLOCK_VARS (block) = p;
1195 else if (VAR_OR_FUNCTION_DECL_P (p))
1197 /* For block local externs add a special
1198 DECL_EXTERNAL decl for debug info generation. */
1199 tree extp = copy_node (p);
1201 DECL_EXTERNAL (extp) = 1;
1202 TREE_STATIC (extp) = 0;
1203 TREE_PUBLIC (extp) = 1;
1204 DECL_INITIAL (extp) = NULL_TREE;
1205 DECL_LANG_SPECIFIC (extp) = NULL;
1206 DECL_CONTEXT (extp) = current_function_decl;
1207 if (TREE_CODE (p) == FUNCTION_DECL)
1209 DECL_RESULT (extp) = NULL_TREE;
1210 DECL_SAVED_TREE (extp) = NULL_TREE;
1211 DECL_STRUCT_FUNCTION (extp) = NULL;
1213 if (b->locus != UNKNOWN_LOCATION)
1214 DECL_SOURCE_LOCATION (extp) = b->locus;
1215 TREE_CHAIN (extp) = BLOCK_VARS (block);
1216 BLOCK_VARS (block) = extp;
1218 /* If this is the file scope, and we are processing more
1219 than one translation unit in this compilation, set
1220 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
1221 This makes same_translation_unit_p work, and causes
1222 static declarations to be given disambiguating suffixes. */
1223 if (scope == file_scope && num_in_fnames > 1)
1225 DECL_CONTEXT (p) = context;
1226 if (TREE_CODE (p) == TYPE_DECL)
1227 set_type_context (TREE_TYPE (p), context);
1231 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1232 already been put there by store_parm_decls. Unused-
1233 parameter warnings are handled by function.c.
1234 error_mark_node obviously does not go in BLOCK_VARS and
1235 does not get unused-variable warnings. */
1238 /* It is possible for a decl not to have a name. We get
1239 here with b->id NULL in this case. */
1242 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1243 I_SYMBOL_BINDING (b->id) = b->shadowed;
1244 if (b->shadowed && b->shadowed->u.type)
1245 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1255 /* Dispose of the block that we just made inside some higher level. */
1256 if ((scope->function_body || scope == file_scope) && context)
1258 DECL_INITIAL (context) = block;
1259 BLOCK_SUPERCONTEXT (block) = context;
1261 else if (scope->outer)
1264 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1265 /* If we did not make a block for the scope just exited, any
1266 blocks made for inner scopes must be carried forward so they
1267 will later become subblocks of something else. */
1268 else if (scope->blocks)
1269 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1272 /* Pop the current scope, and free the structure for reuse. */
1273 current_scope = scope->outer;
1274 if (scope->function_body)
1275 current_function_scope = scope->outer_function;
1277 memset (scope, 0, sizeof (struct c_scope));
1278 scope->outer = scope_freelist;
1279 scope_freelist = scope;
1285 push_file_scope (void)
1293 file_scope = current_scope;
1295 start_fname_decls ();
1297 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
1298 bind (DECL_NAME (decl), decl, file_scope,
1299 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1303 pop_file_scope (void)
1305 /* In case there were missing closebraces, get us back to the global
1307 while (current_scope != file_scope)
1310 /* __FUNCTION__ is defined at file scope (""). This
1311 call may not be necessary as my tests indicate it
1312 still works without it. */
1313 finish_fname_decls ();
1315 check_inline_statics ();
1317 /* This is the point to write out a PCH if we're doing that.
1318 In that case we do not want to do anything else. */
1321 c_common_write_pch ();
1325 /* Pop off the file scope and close this translation unit. */
1329 maybe_apply_pending_pragma_weaks ();
1332 /* Adjust the bindings for the start of a statement expression. */
1335 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1337 struct c_scope *scope;
1339 for (scope = current_scope; scope != NULL; scope = scope->outer)
1341 struct c_binding *b;
1343 if (!scope->has_label_bindings)
1346 for (b = scope->bindings; b != NULL; b = b->prev)
1348 struct c_label_vars *label_vars;
1350 struct c_goto_bindings *g;
1352 if (TREE_CODE (b->decl) != LABEL_DECL)
1354 label_vars = b->u.label;
1355 ++label_vars->label_bindings.stmt_exprs;
1357 VEC_iterate (c_goto_bindings_p, label_vars->gotos, ix, g);
1359 ++g->goto_bindings.stmt_exprs;
1363 if (switch_bindings != NULL)
1364 ++switch_bindings->stmt_exprs;
1367 /* Adjust the bindings for the end of a statement expression. */
1370 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1372 struct c_scope *scope;
1374 for (scope = current_scope; scope != NULL; scope = scope->outer)
1376 struct c_binding *b;
1378 if (!scope->has_label_bindings)
1381 for (b = scope->bindings; b != NULL; b = b->prev)
1383 struct c_label_vars *label_vars;
1385 struct c_goto_bindings *g;
1387 if (TREE_CODE (b->decl) != LABEL_DECL)
1389 label_vars = b->u.label;
1390 --label_vars->label_bindings.stmt_exprs;
1391 if (label_vars->label_bindings.stmt_exprs < 0)
1393 label_vars->label_bindings.left_stmt_expr = true;
1394 label_vars->label_bindings.stmt_exprs = 0;
1397 VEC_iterate (c_goto_bindings_p, label_vars->gotos, ix, g);
1400 --g->goto_bindings.stmt_exprs;
1401 if (g->goto_bindings.stmt_exprs < 0)
1403 g->goto_bindings.left_stmt_expr = true;
1404 g->goto_bindings.stmt_exprs = 0;
1410 if (switch_bindings != NULL)
1412 --switch_bindings->stmt_exprs;
1413 gcc_assert (switch_bindings->stmt_exprs >= 0);
1417 /* Push a definition or a declaration of struct, union or enum tag "name".
1418 "type" should be the type node.
1419 We assume that the tag "name" is not already defined, and has a location
1422 Note that the definition may really be just a forward reference.
1423 In that case, the TYPE_SIZE will be zero. */
1426 pushtag (location_t loc, tree name, tree type)
1428 /* Record the identifier as the type's name if it has none. */
1429 if (name && !TYPE_NAME (type))
1430 TYPE_NAME (type) = name;
1431 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1433 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1434 tagged type we just added to the current scope. This fake
1435 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1436 to output a representation of a tagged type, and it also gives
1437 us a convenient place to record the "scope start" address for the
1440 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1441 TYPE_DECL, NULL_TREE, type));
1443 /* An approximation for now, so we can tell this is a function-scope tag.
1444 This will be updated in pop_scope. */
1445 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1447 if (warn_cxx_compat && name != NULL_TREE)
1449 struct c_binding *b = I_SYMBOL_BINDING (name);
1452 && b->decl != NULL_TREE
1453 && TREE_CODE (b->decl) == TYPE_DECL
1454 && (B_IN_CURRENT_SCOPE (b)
1455 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1456 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1457 != TYPE_MAIN_VARIANT (type)))
1459 warning_at (loc, OPT_Wc___compat,
1460 ("using %qD as both a typedef and a tag is "
1463 if (b->locus != UNKNOWN_LOCATION)
1464 inform (b->locus, "originally defined here");
1469 /* Subroutine of compare_decls. Allow harmless mismatches in return
1470 and argument types provided that the type modes match. This function
1471 return a unified type given a suitable match, and 0 otherwise. */
1474 match_builtin_function_types (tree newtype, tree oldtype)
1476 tree newrettype, oldrettype;
1477 tree newargs, oldargs;
1478 tree trytype, tryargs;
1480 /* Accept the return type of the new declaration if same modes. */
1481 oldrettype = TREE_TYPE (oldtype);
1482 newrettype = TREE_TYPE (newtype);
1484 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1487 oldargs = TYPE_ARG_TYPES (oldtype);
1488 newargs = TYPE_ARG_TYPES (newtype);
1491 while (oldargs || newargs)
1495 || !TREE_VALUE (oldargs)
1496 || !TREE_VALUE (newargs)
1497 || TYPE_MODE (TREE_VALUE (oldargs))
1498 != TYPE_MODE (TREE_VALUE (newargs)))
1501 oldargs = TREE_CHAIN (oldargs);
1502 newargs = TREE_CHAIN (newargs);
1505 trytype = build_function_type (newrettype, tryargs);
1506 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1509 /* Subroutine of diagnose_mismatched_decls. Check for function type
1510 mismatch involving an empty arglist vs a nonempty one and give clearer
1513 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1514 tree newtype, tree oldtype)
1518 if (TREE_CODE (olddecl) != FUNCTION_DECL
1519 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1520 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1522 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1525 t = TYPE_ARG_TYPES (oldtype);
1527 t = TYPE_ARG_TYPES (newtype);
1528 for (; t; t = TREE_CHAIN (t))
1530 tree type = TREE_VALUE (t);
1532 if (TREE_CHAIN (t) == 0
1533 && TYPE_MAIN_VARIANT (type) != void_type_node)
1535 inform (input_location, "a parameter list with an ellipsis can%'t match "
1536 "an empty parameter name list declaration");
1540 if (c_type_promotes_to (type) != type)
1542 inform (input_location, "an argument type that has a default promotion can%'t match "
1543 "an empty parameter name list declaration");
1549 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1550 old-style function definition, NEWDECL is a prototype declaration.
1551 Diagnose inconsistencies in the argument list. Returns TRUE if
1552 the prototype is compatible, FALSE if not. */
1554 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1556 tree newargs, oldargs;
1559 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1561 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1562 newargs = TYPE_ARG_TYPES (newtype);
1567 tree oldargtype = TREE_VALUE (oldargs);
1568 tree newargtype = TREE_VALUE (newargs);
1570 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1573 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1574 newargtype = TYPE_MAIN_VARIANT (newargtype);
1576 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1579 /* Reaching the end of just one list means the two decls don't
1580 agree on the number of arguments. */
1581 if (END_OF_ARGLIST (oldargtype))
1583 error ("prototype for %q+D declares more arguments "
1584 "than previous old-style definition", newdecl);
1587 else if (END_OF_ARGLIST (newargtype))
1589 error ("prototype for %q+D declares fewer arguments "
1590 "than previous old-style definition", newdecl);
1594 /* Type for passing arg must be consistent with that declared
1596 else if (!comptypes (oldargtype, newargtype))
1598 error ("prototype for %q+D declares argument %d"
1599 " with incompatible type",
1604 oldargs = TREE_CHAIN (oldargs);
1605 newargs = TREE_CHAIN (newargs);
1609 /* If we get here, no errors were found, but do issue a warning
1610 for this poor-style construct. */
1611 warning (0, "prototype for %q+D follows non-prototype definition",
1614 #undef END_OF_ARGLIST
1617 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1618 first in a pair of mismatched declarations, using the diagnostic
1621 locate_old_decl (tree decl)
1623 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1625 else if (DECL_INITIAL (decl))
1626 inform (input_location, "previous definition of %q+D was here", decl);
1627 else if (C_DECL_IMPLICIT (decl))
1628 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1630 inform (input_location, "previous declaration of %q+D was here", decl);
1633 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1634 Returns true if the caller should proceed to merge the two, false
1635 if OLDDECL should simply be discarded. As a side effect, issues
1636 all necessary diagnostics for invalid or poor-style combinations.
1637 If it returns true, writes the types of NEWDECL and OLDDECL to
1638 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1639 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1642 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1643 tree *newtypep, tree *oldtypep)
1645 tree newtype, oldtype;
1646 bool pedwarned = false;
1647 bool warned = false;
1650 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1651 && DECL_EXTERNAL (DECL))
1653 /* If we have error_mark_node for either decl or type, just discard
1654 the previous decl - we're in an error cascade already. */
1655 if (olddecl == error_mark_node || newdecl == error_mark_node)
1657 *oldtypep = oldtype = TREE_TYPE (olddecl);
1658 *newtypep = newtype = TREE_TYPE (newdecl);
1659 if (oldtype == error_mark_node || newtype == error_mark_node)
1662 /* Two different categories of symbol altogether. This is an error
1663 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1664 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1666 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1667 && DECL_BUILT_IN (olddecl)
1668 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1670 error ("%q+D redeclared as different kind of symbol", newdecl);
1671 locate_old_decl (olddecl);
1673 else if (TREE_PUBLIC (newdecl))
1674 warning (0, "built-in function %q+D declared as non-function",
1677 warning (OPT_Wshadow, "declaration of %q+D shadows "
1678 "a built-in function", newdecl);
1682 /* Enumerators have no linkage, so may only be declared once in a
1684 if (TREE_CODE (olddecl) == CONST_DECL)
1686 error ("redeclaration of enumerator %q+D", newdecl);
1687 locate_old_decl (olddecl);
1691 if (!comptypes (oldtype, newtype))
1693 if (TREE_CODE (olddecl) == FUNCTION_DECL
1694 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1696 /* Accept harmless mismatch in function types.
1697 This is for the ffs and fprintf builtins. */
1698 tree trytype = match_builtin_function_types (newtype, oldtype);
1700 if (trytype && comptypes (newtype, trytype))
1701 *oldtypep = oldtype = trytype;
1704 /* If types don't match for a built-in, throw away the
1705 built-in. No point in calling locate_old_decl here, it
1706 won't print anything. */
1707 warning (0, "conflicting types for built-in function %q+D",
1712 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1713 && DECL_IS_BUILTIN (olddecl))
1715 /* A conflicting function declaration for a predeclared
1716 function that isn't actually built in. Objective C uses
1717 these. The new declaration silently overrides everything
1718 but the volatility (i.e. noreturn) indication. See also
1719 below. FIXME: Make Objective C use normal builtins. */
1720 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1723 /* Permit void foo (...) to match int foo (...) if the latter is
1724 the definition and implicit int was used. See
1725 c-torture/compile/920625-2.c. */
1726 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1727 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1728 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1729 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1731 pedwarned = pedwarn (input_location, 0,
1732 "conflicting types for %q+D", newdecl);
1733 /* Make sure we keep void as the return type. */
1734 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1735 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1737 /* Permit void foo (...) to match an earlier call to foo (...) with
1738 no declared type (thus, implicitly int). */
1739 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1740 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1741 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1742 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1744 pedwarned = pedwarn (input_location, 0,
1745 "conflicting types for %q+D", newdecl);
1746 /* Make sure we keep void as the return type. */
1747 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1751 int new_quals = TYPE_QUALS (newtype);
1752 int old_quals = TYPE_QUALS (oldtype);
1754 if (new_quals != old_quals)
1756 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1757 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1758 if (new_addr != old_addr)
1760 if (ADDR_SPACE_GENERIC_P (new_addr))
1761 error ("conflicting named address spaces (generic vs %s) "
1763 c_addr_space_name (old_addr), newdecl);
1764 else if (ADDR_SPACE_GENERIC_P (old_addr))
1765 error ("conflicting named address spaces (%s vs generic) "
1767 c_addr_space_name (new_addr), newdecl);
1769 error ("conflicting named address spaces (%s vs %s) "
1771 c_addr_space_name (new_addr),
1772 c_addr_space_name (old_addr),
1776 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1777 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1778 error ("conflicting type qualifiers for %q+D", newdecl);
1781 error ("conflicting types for %q+D", newdecl);
1782 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1783 locate_old_decl (olddecl);
1788 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1789 but silently ignore the redeclaration if either is in a system
1790 header. (Conflicting redeclarations were handled above.) */
1791 if (TREE_CODE (newdecl) == TYPE_DECL)
1793 if (DECL_IN_SYSTEM_HEADER (newdecl)
1794 || DECL_IN_SYSTEM_HEADER (olddecl)
1795 || TREE_NO_WARNING (newdecl)
1796 || TREE_NO_WARNING (olddecl))
1797 return true; /* Allow OLDDECL to continue in use. */
1799 error ("redefinition of typedef %q+D", newdecl);
1800 locate_old_decl (olddecl);
1804 /* Function declarations can either be 'static' or 'extern' (no
1805 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1806 can never conflict with each other on account of linkage
1807 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1808 gnu89 mode permits two definitions if one is 'extern inline' and
1809 one is not. The non- extern-inline definition supersedes the
1810 extern-inline definition. */
1812 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1814 /* If you declare a built-in function name as static, or
1815 define the built-in with an old-style definition (so we
1816 can't validate the argument list) the built-in definition is
1817 overridden, but optionally warn this was a bad choice of name. */
1818 if (DECL_BUILT_IN (olddecl)
1819 && !C_DECL_DECLARED_BUILTIN (olddecl)
1820 && (!TREE_PUBLIC (newdecl)
1821 || (DECL_INITIAL (newdecl)
1822 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1824 warning (OPT_Wshadow, "declaration of %q+D shadows "
1825 "a built-in function", newdecl);
1826 /* Discard the old built-in function. */
1830 if (DECL_INITIAL (newdecl))
1832 if (DECL_INITIAL (olddecl))
1834 /* If both decls are in the same TU and the new declaration
1835 isn't overriding an extern inline reject the new decl.
1836 In c99, no overriding is allowed in the same translation
1838 if ((!DECL_EXTERN_INLINE (olddecl)
1839 || DECL_EXTERN_INLINE (newdecl)
1840 || (!flag_gnu89_inline
1841 && (!DECL_DECLARED_INLINE_P (olddecl)
1842 || !lookup_attribute ("gnu_inline",
1843 DECL_ATTRIBUTES (olddecl)))
1844 && (!DECL_DECLARED_INLINE_P (newdecl)
1845 || !lookup_attribute ("gnu_inline",
1846 DECL_ATTRIBUTES (newdecl))))
1848 && same_translation_unit_p (newdecl, olddecl))
1850 error ("redefinition of %q+D", newdecl);
1851 locate_old_decl (olddecl);
1856 /* If we have a prototype after an old-style function definition,
1857 the argument types must be checked specially. */
1858 else if (DECL_INITIAL (olddecl)
1859 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1860 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1861 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1863 locate_old_decl (olddecl);
1866 /* A non-static declaration (even an "extern") followed by a
1867 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1868 The same is true for a static forward declaration at block
1869 scope followed by a non-static declaration/definition at file
1870 scope. Static followed by non-static at the same scope is
1871 not undefined behavior, and is the most convenient way to get
1872 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1873 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1874 we do diagnose it if -Wtraditional. */
1875 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1877 /* Two exceptions to the rule. If olddecl is an extern
1878 inline, or a predeclared function that isn't actually
1879 built in, newdecl silently overrides olddecl. The latter
1880 occur only in Objective C; see also above. (FIXME: Make
1881 Objective C use normal builtins.) */
1882 if (!DECL_IS_BUILTIN (olddecl)
1883 && !DECL_EXTERN_INLINE (olddecl))
1885 error ("static declaration of %q+D follows "
1886 "non-static declaration", newdecl);
1887 locate_old_decl (olddecl);
1891 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1893 if (DECL_CONTEXT (olddecl))
1895 error ("non-static declaration of %q+D follows "
1896 "static declaration", newdecl);
1897 locate_old_decl (olddecl);
1900 else if (warn_traditional)
1902 warned |= warning (OPT_Wtraditional,
1903 "non-static declaration of %q+D "
1904 "follows static declaration", newdecl);
1908 /* Make sure gnu_inline attribute is either not present, or
1909 present on all inline decls. */
1910 if (DECL_DECLARED_INLINE_P (olddecl)
1911 && DECL_DECLARED_INLINE_P (newdecl))
1913 bool newa = lookup_attribute ("gnu_inline",
1914 DECL_ATTRIBUTES (newdecl)) != NULL;
1915 bool olda = lookup_attribute ("gnu_inline",
1916 DECL_ATTRIBUTES (olddecl)) != NULL;
1919 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
1920 newa ? newdecl : olddecl);
1921 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
1926 else if (TREE_CODE (newdecl) == VAR_DECL)
1928 /* Only variables can be thread-local, and all declarations must
1929 agree on this property. */
1930 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1932 /* Nothing to check. Since OLDDECL is marked threadprivate
1933 and NEWDECL does not have a thread-local attribute, we
1934 will merge the threadprivate attribute into NEWDECL. */
1937 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1939 if (DECL_THREAD_LOCAL_P (newdecl))
1940 error ("thread-local declaration of %q+D follows "
1941 "non-thread-local declaration", newdecl);
1943 error ("non-thread-local declaration of %q+D follows "
1944 "thread-local declaration", newdecl);
1946 locate_old_decl (olddecl);
1950 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1951 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1953 error ("redefinition of %q+D", newdecl);
1954 locate_old_decl (olddecl);
1958 /* Objects declared at file scope: if the first declaration had
1959 external linkage (even if it was an external reference) the
1960 second must have external linkage as well, or the behavior is
1961 undefined. If the first declaration had internal linkage, then
1962 the second must too, or else be an external reference (in which
1963 case the composite declaration still has internal linkage).
1964 As for function declarations, we warn about the static-then-
1965 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1966 if (DECL_FILE_SCOPE_P (newdecl)
1967 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1969 if (DECL_EXTERNAL (newdecl))
1971 if (!DECL_FILE_SCOPE_P (olddecl))
1973 error ("extern declaration of %q+D follows "
1974 "declaration with no linkage", newdecl);
1975 locate_old_decl (olddecl);
1978 else if (warn_traditional)
1980 warned |= warning (OPT_Wtraditional,
1981 "non-static declaration of %q+D "
1982 "follows static declaration", newdecl);
1987 if (TREE_PUBLIC (newdecl))
1988 error ("non-static declaration of %q+D follows "
1989 "static declaration", newdecl);
1991 error ("static declaration of %q+D follows "
1992 "non-static declaration", newdecl);
1994 locate_old_decl (olddecl);
1998 /* Two objects with the same name declared at the same block
1999 scope must both be external references (6.7p3). */
2000 else if (!DECL_FILE_SCOPE_P (newdecl))
2002 if (DECL_EXTERNAL (newdecl))
2004 /* Extern with initializer at block scope, which will
2005 already have received an error. */
2007 else if (DECL_EXTERNAL (olddecl))
2009 error ("declaration of %q+D with no linkage follows "
2010 "extern declaration", newdecl);
2011 locate_old_decl (olddecl);
2015 error ("redeclaration of %q+D with no linkage", newdecl);
2016 locate_old_decl (olddecl);
2022 /* C++ does not permit a decl to appear multiple times at file
2025 && DECL_FILE_SCOPE_P (newdecl)
2026 && !DECL_EXTERNAL (newdecl)
2027 && !DECL_EXTERNAL (olddecl))
2028 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2030 ("duplicate declaration of %qD is "
2036 /* All decls must agree on a visibility. */
2037 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2038 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2039 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2041 warned |= warning (0, "redeclaration of %q+D with different visibility "
2042 "(old visibility preserved)", newdecl);
2045 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2047 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2048 if (DECL_DECLARED_INLINE_P (newdecl)
2049 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2051 warned |= warning (OPT_Wattributes,
2052 "inline declaration of %qD follows "
2053 "declaration with attribute noinline", newdecl);
2055 else if (DECL_DECLARED_INLINE_P (olddecl)
2056 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2058 warned |= warning (OPT_Wattributes,
2059 "declaration of %q+D with attribute "
2060 "noinline follows inline declaration ", newdecl);
2063 else /* PARM_DECL, VAR_DECL */
2065 /* Redeclaration of a parameter is a constraint violation (this is
2066 not explicitly stated, but follows from C99 6.7p3 [no more than
2067 one declaration of the same identifier with no linkage in the
2068 same scope, except type tags] and 6.2.2p6 [parameters have no
2069 linkage]). We must check for a forward parameter declaration,
2070 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2071 an extension, the mandatory diagnostic for which is handled by
2072 mark_forward_parm_decls. */
2074 if (TREE_CODE (newdecl) == PARM_DECL
2075 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2077 error ("redefinition of parameter %q+D", newdecl);
2078 locate_old_decl (olddecl);
2083 /* Optional warning for completely redundant decls. */
2084 if (!warned && !pedwarned
2085 && warn_redundant_decls
2086 /* Don't warn about a function declaration followed by a
2088 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2089 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2090 /* Don't warn about redundant redeclarations of builtins. */
2091 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2092 && !DECL_BUILT_IN (newdecl)
2093 && DECL_BUILT_IN (olddecl)
2094 && !C_DECL_DECLARED_BUILTIN (olddecl))
2095 /* Don't warn about an extern followed by a definition. */
2096 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2097 /* Don't warn about forward parameter decls. */
2098 && !(TREE_CODE (newdecl) == PARM_DECL
2099 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2100 /* Don't warn about a variable definition following a declaration. */
2101 && !(TREE_CODE (newdecl) == VAR_DECL
2102 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2104 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2108 /* Report location of previous decl/defn. */
2109 if (warned || pedwarned)
2110 locate_old_decl (olddecl);
2112 #undef DECL_EXTERN_INLINE
2117 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2118 consistent with OLDDECL, but carries new information. Merge the
2119 new information into OLDDECL. This function issues no
2123 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2125 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2126 && DECL_INITIAL (newdecl) != 0);
2127 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2128 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
2129 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2130 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
2131 bool extern_changed = false;
2133 /* For real parm decl following a forward decl, rechain the old decl
2134 in its new location and clear TREE_ASM_WRITTEN (it's not a
2135 forward decl anymore). */
2136 if (TREE_CODE (newdecl) == PARM_DECL
2137 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2139 struct c_binding *b, **here;
2141 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
2142 if ((*here)->decl == olddecl)
2149 b->prev = current_scope->bindings;
2150 current_scope->bindings = b;
2152 TREE_ASM_WRITTEN (olddecl) = 0;
2155 DECL_ATTRIBUTES (newdecl)
2156 = targetm.merge_decl_attributes (olddecl, newdecl);
2158 /* Merge the data types specified in the two decls. */
2160 = TREE_TYPE (olddecl)
2161 = composite_type (newtype, oldtype);
2163 /* Lay the type out, unless already done. */
2164 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2166 if (TREE_TYPE (newdecl) != error_mark_node)
2167 layout_type (TREE_TYPE (newdecl));
2168 if (TREE_CODE (newdecl) != FUNCTION_DECL
2169 && TREE_CODE (newdecl) != TYPE_DECL
2170 && TREE_CODE (newdecl) != CONST_DECL)
2171 layout_decl (newdecl, 0);
2175 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2176 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2177 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2178 DECL_MODE (newdecl) = DECL_MODE (olddecl);
2179 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2181 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2182 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2186 /* Keep the old rtl since we can safely use it. */
2187 if (HAS_RTL_P (olddecl))
2188 COPY_DECL_RTL (olddecl, newdecl);
2190 /* Merge the type qualifiers. */
2191 if (TREE_READONLY (newdecl))
2192 TREE_READONLY (olddecl) = 1;
2194 if (TREE_THIS_VOLATILE (newdecl))
2195 TREE_THIS_VOLATILE (olddecl) = 1;
2197 /* Merge deprecatedness. */
2198 if (TREE_DEPRECATED (newdecl))
2199 TREE_DEPRECATED (olddecl) = 1;
2201 /* If a decl is in a system header and the other isn't, keep the one on the
2202 system header. Otherwise, keep source location of definition rather than
2203 declaration and of prototype rather than non-prototype unless that
2204 prototype is built-in. */
2205 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2206 && DECL_IN_SYSTEM_HEADER (olddecl)
2207 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2208 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2209 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2210 && DECL_IN_SYSTEM_HEADER (newdecl)
2211 && !DECL_IN_SYSTEM_HEADER (olddecl))
2212 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2213 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
2214 || (old_is_prototype && !new_is_prototype
2215 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2216 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2218 /* Merge the initialization information. */
2219 if (DECL_INITIAL (newdecl) == 0)
2220 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2222 /* Merge the threadprivate attribute. */
2223 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
2225 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
2226 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2229 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2231 /* Merge the section attribute.
2232 We want to issue an error if the sections conflict but that
2233 must be done later in decl_attributes since we are called
2234 before attributes are assigned. */
2235 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
2236 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
2238 /* Copy the assembler name.
2239 Currently, it can only be defined in the prototype. */
2240 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2242 /* Use visibility of whichever declaration had it specified */
2243 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2245 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2246 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2249 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2251 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2252 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2253 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2254 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2255 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2256 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2257 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2258 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2259 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2260 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2261 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2264 /* Merge the storage class information. */
2265 merge_weak (newdecl, olddecl);
2267 /* For functions, static overrides non-static. */
2268 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2270 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2271 /* This is since we don't automatically
2272 copy the attributes of NEWDECL into OLDDECL. */
2273 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2274 /* If this clears `static', clear it in the identifier too. */
2275 if (!TREE_PUBLIC (olddecl))
2276 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2280 /* In c99, 'extern' declaration before (or after) 'inline' means this
2281 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2283 if (TREE_CODE (newdecl) == FUNCTION_DECL
2284 && !flag_gnu89_inline
2285 && (DECL_DECLARED_INLINE_P (newdecl)
2286 || DECL_DECLARED_INLINE_P (olddecl))
2287 && (!DECL_DECLARED_INLINE_P (newdecl)
2288 || !DECL_DECLARED_INLINE_P (olddecl)
2289 || !DECL_EXTERNAL (olddecl))
2290 && DECL_EXTERNAL (newdecl)
2291 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2292 && !current_function_decl)
2293 DECL_EXTERNAL (newdecl) = 0;
2295 if (DECL_EXTERNAL (newdecl))
2297 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2298 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2300 /* An extern decl does not override previous storage class. */
2301 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2302 if (!DECL_EXTERNAL (newdecl))
2304 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2305 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2310 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2311 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2314 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2316 /* If we're redefining a function previously defined as extern
2317 inline, make sure we emit debug info for the inline before we
2318 throw it away, in case it was inlined into a function that
2319 hasn't been written out yet. */
2320 if (new_is_definition && DECL_INITIAL (olddecl))
2321 /* The new defn must not be inline. */
2322 DECL_UNINLINABLE (newdecl) = 1;
2325 /* If either decl says `inline', this fn is inline, unless
2326 its definition was passed already. */
2327 if (DECL_DECLARED_INLINE_P (newdecl)
2328 || DECL_DECLARED_INLINE_P (olddecl))
2329 DECL_DECLARED_INLINE_P (newdecl) = 1;
2331 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2332 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2334 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2335 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2336 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2337 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2340 if (DECL_BUILT_IN (olddecl))
2342 /* If redeclaring a builtin function, it stays built in.
2343 But it gets tagged as having been declared. */
2344 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2345 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2346 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2347 if (new_is_prototype)
2348 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2350 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2351 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2354 /* Preserve function specific target and optimization options */
2355 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2356 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2357 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2358 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2360 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2361 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2362 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2363 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2365 /* Also preserve various other info from the definition. */
2366 if (!new_is_definition)
2369 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2370 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2371 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2372 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2373 gimple_set_body (newdecl, gimple_body (olddecl));
2374 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2375 for (t = DECL_ARGUMENTS (newdecl); t ; t = TREE_CHAIN (t))
2376 DECL_CONTEXT (t) = newdecl;
2378 /* See if we've got a function to instantiate from. */
2379 if (DECL_SAVED_TREE (olddecl))
2380 DECL_ABSTRACT_ORIGIN (newdecl)
2381 = DECL_ABSTRACT_ORIGIN (olddecl);
2385 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
2387 /* Merge the USED information. */
2388 if (TREE_USED (olddecl))
2389 TREE_USED (newdecl) = 1;
2390 else if (TREE_USED (newdecl))
2391 TREE_USED (olddecl) = 1;
2392 if (TREE_CODE (olddecl) == VAR_DECL || TREE_CODE (olddecl) == PARM_DECL)
2393 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2394 if (DECL_PRESERVE_P (olddecl))
2395 DECL_PRESERVE_P (newdecl) = 1;
2396 else if (DECL_PRESERVE_P (newdecl))
2397 DECL_PRESERVE_P (olddecl) = 1;
2399 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2400 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2401 DECL_ARGUMENTS (if appropriate). */
2403 unsigned olddecl_uid = DECL_UID (olddecl);
2404 tree olddecl_context = DECL_CONTEXT (olddecl);
2405 tree olddecl_arguments = NULL;
2406 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2407 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2409 memcpy ((char *) olddecl + sizeof (struct tree_common),
2410 (char *) newdecl + sizeof (struct tree_common),
2411 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2412 switch (TREE_CODE (olddecl))
2415 gimple_set_body (olddecl, gimple_body (newdecl));
2425 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2426 (char *) newdecl + sizeof (struct tree_decl_common),
2427 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2432 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2433 (char *) newdecl + sizeof (struct tree_decl_common),
2434 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2436 DECL_UID (olddecl) = olddecl_uid;
2437 DECL_CONTEXT (olddecl) = olddecl_context;
2438 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2439 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2442 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2443 so that encode_section_info has a chance to look at the new decl
2444 flags and attributes. */
2445 if (DECL_RTL_SET_P (olddecl)
2446 && (TREE_CODE (olddecl) == FUNCTION_DECL
2447 || (TREE_CODE (olddecl) == VAR_DECL
2448 && TREE_STATIC (olddecl))))
2449 make_decl_rtl (olddecl);
2451 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
2452 and the definition is coming from the old version, cgraph needs
2453 to be called again. */
2454 if (extern_changed && !new_is_definition
2455 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
2456 cgraph_mark_if_needed (olddecl);
2459 /* Handle when a new declaration NEWDECL has the same name as an old
2460 one OLDDECL in the same binding contour. Prints an error message
2463 If safely possible, alter OLDDECL to look like NEWDECL, and return
2464 true. Otherwise, return false. */
2467 duplicate_decls (tree newdecl, tree olddecl)
2469 tree newtype = NULL, oldtype = NULL;
2471 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2473 /* Avoid `unused variable' and other warnings for OLDDECL. */
2474 TREE_NO_WARNING (olddecl) = 1;
2478 merge_decls (newdecl, olddecl, newtype, oldtype);
2483 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2485 warn_if_shadowing (tree new_decl)
2487 struct c_binding *b;
2489 /* Shadow warnings wanted? */
2491 /* No shadow warnings for internally generated vars. */
2492 || DECL_IS_BUILTIN (new_decl)
2493 /* No shadow warnings for vars made for inlining. */
2494 || DECL_FROM_INLINE (new_decl))
2497 /* Is anything being shadowed? Invisible decls do not count. */
2498 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2499 if (b->decl && b->decl != new_decl && !b->invisible)
2501 tree old_decl = b->decl;
2503 if (old_decl == error_mark_node)
2505 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2506 "non-variable", new_decl);
2509 else if (TREE_CODE (old_decl) == PARM_DECL)
2510 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2512 else if (DECL_FILE_SCOPE_P (old_decl))
2513 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2514 "declaration", new_decl);
2515 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2516 && DECL_BUILT_IN (old_decl))
2518 warning (OPT_Wshadow, "declaration of %q+D shadows "
2519 "a built-in function", new_decl);
2523 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2526 warning_at (DECL_SOURCE_LOCATION (old_decl), OPT_Wshadow,
2527 "shadowed declaration is here");
2533 /* Record a decl-node X as belonging to the current lexical scope.
2534 Check for errors (such as an incompatible declaration for the same
2535 name already seen in the same scope).
2537 Returns either X or an old decl for the same name.
2538 If an old decl is returned, it may have been smashed
2539 to agree with what X says. */
2544 tree name = DECL_NAME (x);
2545 struct c_scope *scope = current_scope;
2546 struct c_binding *b;
2547 bool nested = false;
2548 location_t locus = DECL_SOURCE_LOCATION (x);
2550 /* Must set DECL_CONTEXT for everything not at file scope or
2551 DECL_FILE_SCOPE_P won't work. Local externs don't count
2552 unless they have initializers (which generate code). */
2553 if (current_function_decl
2554 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2555 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2556 DECL_CONTEXT (x) = current_function_decl;
2558 /* Anonymous decls are just inserted in the scope. */
2561 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2566 /* First, see if there is another declaration with the same name in
2567 the current scope. If there is, duplicate_decls may do all the
2568 work for us. If duplicate_decls returns false, that indicates
2569 two incompatible decls in the same scope; we are to silently
2570 replace the old one (duplicate_decls has issued all appropriate
2571 diagnostics). In particular, we should not consider possible
2572 duplicates in the external scope, or shadowing. */
2573 b = I_SYMBOL_BINDING (name);
2574 if (b && B_IN_SCOPE (b, scope))
2576 struct c_binding *b_ext, *b_use;
2577 tree type = TREE_TYPE (x);
2578 tree visdecl = b->decl;
2579 tree vistype = TREE_TYPE (visdecl);
2580 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2581 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2582 b->inner_comp = false;
2585 /* If this is an external linkage declaration, we should check
2586 for compatibility with the type in the external scope before
2587 setting the type at this scope based on the visible
2588 information only. */
2589 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2591 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2592 b_ext = b_ext->shadowed;
2597 TREE_TYPE (b_use->decl) = b_use->u.type;
2600 if (duplicate_decls (x, b_use->decl))
2604 /* Save the updated type in the external scope and
2605 restore the proper type for this scope. */
2607 if (comptypes (vistype, type))
2608 thistype = composite_type (vistype, type);
2610 thistype = TREE_TYPE (b_use->decl);
2611 b_use->u.type = TREE_TYPE (b_use->decl);
2612 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2613 && DECL_BUILT_IN (b_use->decl))
2615 = build_type_attribute_variant (thistype,
2618 TREE_TYPE (b_use->decl) = thistype;
2623 goto skip_external_and_shadow_checks;
2626 /* All declarations with external linkage, and all external
2627 references, go in the external scope, no matter what scope is
2628 current. However, the binding in that scope is ignored for
2629 purposes of normal name lookup. A separate binding structure is
2630 created in the requested scope; this governs the normal
2631 visibility of the symbol.
2633 The binding in the externals scope is used exclusively for
2634 detecting duplicate declarations of the same object, no matter
2635 what scope they are in; this is what we do here. (C99 6.2.7p2:
2636 All declarations that refer to the same object or function shall
2637 have compatible type; otherwise, the behavior is undefined.) */
2638 if (DECL_EXTERNAL (x) || scope == file_scope)
2640 tree type = TREE_TYPE (x);
2643 bool type_saved = false;
2644 if (b && !B_IN_EXTERNAL_SCOPE (b)
2645 && (TREE_CODE (b->decl) == FUNCTION_DECL
2646 || TREE_CODE (b->decl) == VAR_DECL)
2647 && DECL_FILE_SCOPE_P (b->decl))
2650 vistype = TREE_TYPE (visdecl);
2652 if (scope != file_scope
2653 && !DECL_IN_SYSTEM_HEADER (x))
2654 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2656 while (b && !B_IN_EXTERNAL_SCOPE (b))
2658 /* If this decl might be modified, save its type. This is
2659 done here rather than when the decl is first bound
2660 because the type may change after first binding, through
2661 being completed or through attributes being added. If we
2662 encounter multiple such decls, only the first should have
2663 its type saved; the others will already have had their
2664 proper types saved and the types will not have changed as
2665 their scopes will not have been re-entered. */
2666 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2668 b->u.type = TREE_TYPE (b->decl);
2671 if (B_IN_FILE_SCOPE (b)
2672 && TREE_CODE (b->decl) == VAR_DECL
2673 && TREE_STATIC (b->decl)
2674 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2675 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2676 && TREE_CODE (type) == ARRAY_TYPE
2677 && TYPE_DOMAIN (type)
2678 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2679 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2681 /* Array type completed in inner scope, which should be
2682 diagnosed if the completion does not have size 1 and
2683 it does not get completed in the file scope. */
2684 b->inner_comp = true;
2689 /* If a matching external declaration has been found, set its
2690 type to the composite of all the types of that declaration.
2691 After the consistency checks, it will be reset to the
2692 composite of the visible types only. */
2693 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2695 TREE_TYPE (b->decl) = b->u.type;
2697 /* The point of the same_translation_unit_p check here is,
2698 we want to detect a duplicate decl for a construct like
2699 foo() { extern bar(); } ... static bar(); but not if
2700 they are in different translation units. In any case,
2701 the static does not go in the externals scope. */
2703 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2704 && duplicate_decls (x, b->decl))
2709 if (comptypes (vistype, type))
2710 thistype = composite_type (vistype, type);
2712 thistype = TREE_TYPE (b->decl);
2716 b->u.type = TREE_TYPE (b->decl);
2717 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2719 = build_type_attribute_variant (thistype,
2720 TYPE_ATTRIBUTES (b->u.type));
2721 TREE_TYPE (b->decl) = thistype;
2722 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2726 else if (TREE_PUBLIC (x))
2728 if (visdecl && !b && duplicate_decls (x, visdecl))
2730 /* An external declaration at block scope referring to a
2731 visible entity with internal linkage. The composite
2732 type will already be correct for this scope, so we
2733 just need to fall through to make the declaration in
2740 bind (name, x, external_scope, /*invisible=*/true,
2741 /*nested=*/false, locus);
2747 if (TREE_CODE (x) != PARM_DECL)
2748 warn_if_shadowing (x);
2750 skip_external_and_shadow_checks:
2751 if (TREE_CODE (x) == TYPE_DECL)
2752 set_underlying_type (x);
2754 bind (name, x, scope, /*invisible=*/false, nested, locus);
2756 /* If x's type is incomplete because it's based on a
2757 structure or union which has not yet been fully declared,
2758 attach it to that structure or union type, so we can go
2759 back and complete the variable declaration later, if the
2760 structure or union gets fully declared.
2762 If the input is erroneous, we can have error_mark in the type
2763 slot (e.g. "f(void a, ...)") - that doesn't count as an
2765 if (TREE_TYPE (x) != error_mark_node
2766 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2768 tree element = TREE_TYPE (x);
2770 while (TREE_CODE (element) == ARRAY_TYPE)
2771 element = TREE_TYPE (element);
2772 element = TYPE_MAIN_VARIANT (element);
2774 if ((TREE_CODE (element) == RECORD_TYPE
2775 || TREE_CODE (element) == UNION_TYPE)
2776 && (TREE_CODE (x) != TYPE_DECL
2777 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2778 && !COMPLETE_TYPE_P (element))
2779 C_TYPE_INCOMPLETE_VARS (element)
2780 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2785 /* Record X as belonging to file scope.
2786 This is used only internally by the Objective-C front end,
2787 and is limited to its needs. duplicate_decls is not called;
2788 if there is any preexisting decl for this identifier, it is an ICE. */
2791 pushdecl_top_level (tree x)
2794 bool nested = false;
2795 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2797 name = DECL_NAME (x);
2799 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2801 if (TREE_PUBLIC (x))
2803 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2808 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2814 implicit_decl_warning (tree id, tree olddecl)
2816 if (warn_implicit_function_declaration)
2821 warned = pedwarn (input_location, OPT_Wimplicit_function_declaration,
2822 "implicit declaration of function %qE", id);
2824 warned = warning (OPT_Wimplicit_function_declaration,
2825 G_("implicit declaration of function %qE"), id);
2826 if (olddecl && warned)
2827 locate_old_decl (olddecl);
2831 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
2832 function of type int (). */
2835 implicitly_declare (location_t loc, tree functionid)
2837 struct c_binding *b;
2841 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2843 if (B_IN_SCOPE (b, external_scope))
2852 if (decl == error_mark_node)
2855 /* FIXME: Objective-C has weird not-really-builtin functions
2856 which are supposed to be visible automatically. They wind up
2857 in the external scope because they're pushed before the file
2858 scope gets created. Catch this here and rebind them into the
2860 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2862 bind (functionid, decl, file_scope,
2863 /*invisible=*/false, /*nested=*/true,
2864 DECL_SOURCE_LOCATION (decl));
2869 tree newtype = default_function_type;
2871 TREE_TYPE (decl) = b->u.type;
2872 /* Implicit declaration of a function already declared
2873 (somehow) in a different scope, or as a built-in.
2874 If this is the first time this has happened, warn;
2875 then recycle the old declaration but with the new type. */
2876 if (!C_DECL_IMPLICIT (decl))
2878 implicit_decl_warning (functionid, decl);
2879 C_DECL_IMPLICIT (decl) = 1;
2881 if (DECL_BUILT_IN (decl))
2883 newtype = build_type_attribute_variant (newtype,
2885 (TREE_TYPE (decl)));
2886 if (!comptypes (newtype, TREE_TYPE (decl)))
2888 warning_at (loc, 0, "incompatible implicit declaration of "
2889 "built-in function %qD", decl);
2890 newtype = TREE_TYPE (decl);
2895 if (!comptypes (newtype, TREE_TYPE (decl)))
2897 error_at (loc, "incompatible implicit declaration of function %qD", decl);
2898 locate_old_decl (decl);
2901 b->u.type = TREE_TYPE (decl);
2902 TREE_TYPE (decl) = newtype;
2903 bind (functionid, decl, current_scope,
2904 /*invisible=*/false, /*nested=*/true,
2905 DECL_SOURCE_LOCATION (decl));
2910 /* Not seen before. */
2911 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
2912 DECL_EXTERNAL (decl) = 1;
2913 TREE_PUBLIC (decl) = 1;
2914 C_DECL_IMPLICIT (decl) = 1;
2915 implicit_decl_warning (functionid, 0);
2916 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2918 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2920 /* C89 says implicit declarations are in the innermost block.
2921 So we record the decl in the standard fashion. */
2922 decl = pushdecl (decl);
2924 /* No need to call objc_check_decl here - it's a function type. */
2925 rest_of_decl_compilation (decl, 0, 0);
2927 /* Write a record describing this implicit function declaration
2928 to the prototypes file (if requested). */
2929 gen_aux_info_record (decl, 0, 1, 0);
2931 /* Possibly apply some default attributes to this implicit declaration. */
2932 decl_attributes (&decl, NULL_TREE, 0);
2937 /* Issue an error message for a reference to an undeclared variable
2938 ID, including a reference to a builtin outside of function-call
2939 context. Establish a binding of the identifier to error_mark_node
2940 in an appropriate scope, which will suppress further errors for the
2941 same identifier. The error message should be given location LOC. */
2943 undeclared_variable (location_t loc, tree id)
2945 static bool already = false;
2946 struct c_scope *scope;
2948 if (current_function_decl == 0)
2950 error_at (loc, "%qE undeclared here (not in a function)", id);
2951 scope = current_scope;
2955 error_at (loc, "%qE undeclared (first use in this function)", id);
2958 inform (loc, "each undeclared identifier is reported only"
2959 " once for each function it appears in");
2963 /* If we are parsing old-style parameter decls, current_function_decl
2964 will be nonnull but current_function_scope will be null. */
2965 scope = current_function_scope ? current_function_scope : current_scope;
2967 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
2971 /* Subroutine of lookup_label, declare_label, define_label: construct a
2972 LABEL_DECL with all the proper frills. Also create a struct
2973 c_label_vars initialized for the current scope. */
2976 make_label (location_t location, tree name, bool defining,
2977 struct c_label_vars **p_label_vars)
2979 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
2980 struct c_label_vars *label_vars;
2982 DECL_CONTEXT (label) = current_function_decl;
2983 DECL_MODE (label) = VOIDmode;
2985 label_vars = GGC_NEW (struct c_label_vars);
2986 label_vars->shadowed = NULL;
2987 set_spot_bindings (&label_vars->label_bindings, defining);
2988 label_vars->decls_in_scope = make_tree_vector ();
2989 label_vars->gotos = VEC_alloc (c_goto_bindings_p, gc, 0);
2990 *p_label_vars = label_vars;
2995 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2996 Create one if none exists so far for the current function.
2997 This is called when a label is used in a goto expression or
2998 has its address taken. */
3001 lookup_label (tree name)
3004 struct c_label_vars *label_vars;
3006 if (current_function_decl == 0)
3008 error ("label %qE referenced outside of any function", name);
3012 /* Use a label already defined or ref'd with this name, but not if
3013 it is inherited from a containing function and wasn't declared
3015 label = I_LABEL_DECL (name);
3016 if (label && (DECL_CONTEXT (label) == current_function_decl
3017 || C_DECLARED_LABEL_FLAG (label)))
3019 /* If the label has only been declared, update its apparent
3020 location to point here, for better diagnostics if it
3021 turns out not to have been defined. */
3022 if (DECL_INITIAL (label) == NULL_TREE)
3023 DECL_SOURCE_LOCATION (label) = input_location;
3027 /* No label binding for that identifier; make one. */
3028 label = make_label (input_location, name, false, &label_vars);
3030 /* Ordinary labels go in the current function scope. */
3031 bind_label (name, label, current_function_scope, label_vars);
3036 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3040 warn_about_goto (location_t goto_loc, tree label, tree decl)
3042 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3044 "jump into scope of identifier with variably modified type");
3046 warning_at (goto_loc, OPT_Wjump_misses_init,
3047 "jump skips variable initialization");
3048 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3049 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3052 /* Look up a label because of a goto statement. This is like
3053 lookup_label, but also issues any appropriate warnings. */
3056 lookup_label_for_goto (location_t loc, tree name)
3059 struct c_label_vars *label_vars;
3063 label = lookup_label (name);
3064 if (label == NULL_TREE)
3067 /* If we are jumping to a different function, we can't issue any
3069 if (DECL_CONTEXT (label) != current_function_decl)
3071 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3075 label_vars = I_LABEL_BINDING (name)->u.label;
3077 /* If the label has not yet been defined, then push this goto on a
3078 list for possible later warnings. */
3079 if (label_vars->label_bindings.scope == NULL)
3081 struct c_goto_bindings *g;
3083 g = GGC_NEW (struct c_goto_bindings);
3085 set_spot_bindings (&g->goto_bindings, true);
3086 VEC_safe_push (c_goto_bindings_p, gc, label_vars->gotos, g);
3090 /* If there are any decls in label_vars->decls_in_scope, then this
3091 goto has missed the declaration of the decl. This happens for a
3097 Issue a warning or error. */
3098 for (ix = 0; VEC_iterate (tree, label_vars->decls_in_scope, ix, decl); ++ix)
3099 warn_about_goto (loc, label, decl);
3101 if (label_vars->label_bindings.left_stmt_expr)
3103 error_at (loc, "jump into statement expression");
3104 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3110 /* Make a label named NAME in the current function, shadowing silently
3111 any that may be inherited from containing functions or containing
3112 scopes. This is called for __label__ declarations. */
3115 declare_label (tree name)
3117 struct c_binding *b = I_LABEL_BINDING (name);
3119 struct c_label_vars *label_vars;
3121 /* Check to make sure that the label hasn't already been declared
3123 if (b && B_IN_CURRENT_SCOPE (b))
3125 error ("duplicate label declaration %qE", name);
3126 locate_old_decl (b->decl);
3128 /* Just use the previous declaration. */
3132 label = make_label (input_location, name, false, &label_vars);
3133 C_DECLARED_LABEL_FLAG (label) = 1;
3135 /* Declared labels go in the current scope. */
3136 bind_label (name, label, current_scope, label_vars);
3141 /* When we define a label, issue any appropriate warnings if there are
3142 any gotos earlier in the function which jump to this label. */
3145 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3148 struct c_goto_bindings *g;
3151 VEC_iterate (c_goto_bindings_p, label_vars->gotos, ix, g);
3154 struct c_binding *b;
3155 struct c_scope *scope;
3157 /* We have a goto to this label. The goto is going forward. In
3158 g->scope, the goto is going to skip any binding which was
3159 defined after g->bindings_in_scope. */
3160 for (b = g->goto_bindings.scope->bindings;
3161 b != g->goto_bindings.bindings_in_scope;
3164 if (decl_jump_unsafe (b->decl))
3165 warn_about_goto (g->loc, label, b->decl);
3168 /* We also need to warn about decls defined in any scopes
3169 between the scope of the label and the scope of the goto. */
3170 for (scope = label_vars->label_bindings.scope;
3171 scope != g->goto_bindings.scope;
3172 scope = scope->outer)
3174 gcc_assert (scope != NULL);
3175 if (scope == label_vars->label_bindings.scope)
3176 b = label_vars->label_bindings.bindings_in_scope;
3178 b = scope->bindings;
3179 for (; b != NULL; b = b->prev)
3181 if (decl_jump_unsafe (b->decl))
3182 warn_about_goto (g->loc, label, b->decl);
3186 if (g->goto_bindings.stmt_exprs > 0)
3188 error_at (g->loc, "jump into statement expression");
3189 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3194 /* Now that the label is defined, we will issue warnings about
3195 subsequent gotos to this label when we see them. */
3196 VEC_truncate (c_goto_bindings_p, label_vars->gotos, 0);
3197 label_vars->gotos = NULL;
3200 /* Define a label, specifying the location in the source file.
3201 Return the LABEL_DECL node for the label, if the definition is valid.
3202 Otherwise return 0. */
3205 define_label (location_t location, tree name)
3207 /* Find any preexisting label with this name. It is an error
3208 if that label has already been defined in this function, or
3209 if there is a containing function with a declared label with
3211 tree label = I_LABEL_DECL (name);
3214 && ((DECL_CONTEXT (label) == current_function_decl
3215 && DECL_INITIAL (label) != 0)
3216 || (DECL_CONTEXT (label) != current_function_decl
3217 && C_DECLARED_LABEL_FLAG (label))))
3219 error_at (location, "duplicate label %qD", label);
3220 locate_old_decl (label);
3223 else if (label && DECL_CONTEXT (label) == current_function_decl)
3225 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3227 /* The label has been used or declared already in this function,
3228 but not defined. Update its location to point to this
3230 DECL_SOURCE_LOCATION (label) = location;
3231 set_spot_bindings (&label_vars->label_bindings, true);
3233 /* Issue warnings as required about any goto statements from
3234 earlier in the function. */
3235 check_earlier_gotos (label, label_vars);
3239 struct c_label_vars *label_vars;
3241 /* No label binding for that identifier; make one. */
3242 label = make_label (location, name, true, &label_vars);
3244 /* Ordinary labels go in the current function scope. */
3245 bind_label (name, label, current_function_scope, label_vars);
3248 if (!in_system_header && lookup_name (name))
3249 warning_at (location, OPT_Wtraditional,
3250 "traditional C lacks a separate namespace "
3251 "for labels, identifier %qE conflicts", name);
3253 /* Mark label as having been defined. */
3254 DECL_INITIAL (label) = error_mark_node;
3258 /* Get the bindings for a new switch statement. This is used to issue
3259 warnings as appropriate for jumps from the switch to case or
3262 struct c_spot_bindings *
3263 c_get_switch_bindings (void)
3265 struct c_spot_bindings *switch_bindings;
3267 switch_bindings = XNEW (struct c_spot_bindings);
3268 set_spot_bindings (switch_bindings, true);
3269 return switch_bindings;
3273 c_release_switch_bindings (struct c_spot_bindings *bindings)
3275 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3279 /* This is called at the point of a case or default label to issue
3280 warnings about decls as needed. It returns true if it found an
3281 error, not just a warning. */
3284 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3285 location_t switch_loc, location_t case_loc)
3288 struct c_scope *scope;
3291 for (scope = current_scope;
3292 scope != switch_bindings->scope;
3293 scope = scope->outer)
3295 struct c_binding *b;
3297 gcc_assert (scope != NULL);
3298 for (b = scope->bindings; b != NULL; b = b->prev)
3300 if (decl_jump_unsafe (b->decl))
3302 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3306 ("switch jumps into scope of identifier with "
3307 "variably modified type"));
3310 warning_at (case_loc, OPT_Wjump_misses_init,
3311 "switch jumps over variable initialization");
3312 inform (switch_loc, "switch starts here");
3313 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3319 if (switch_bindings->stmt_exprs > 0)
3322 error_at (case_loc, "switch jumps into statement expression");
3323 inform (switch_loc, "switch starts here");
3329 /* Given NAME, an IDENTIFIER_NODE,
3330 return the structure (or union or enum) definition for that name.
3331 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3332 CODE says which kind of type the caller wants;
3333 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3334 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3335 location where the tag was defined.
3336 If the wrong kind of type is found, an error is reported. */
3339 lookup_tag (enum tree_code code, tree name, int thislevel_only,
3342 struct c_binding *b = I_TAG_BINDING (name);
3348 /* We only care about whether it's in this level if
3349 thislevel_only was set or it might be a type clash. */
3350 if (thislevel_only || TREE_CODE (b->decl) != code)
3352 /* For our purposes, a tag in the external scope is the same as
3353 a tag in the file scope. (Primarily relevant to Objective-C
3354 and its builtin structure tags, which get pushed before the
3355 file scope is created.) */
3356 if (B_IN_CURRENT_SCOPE (b)
3357 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3361 if (thislevel_only && !thislevel)
3364 if (TREE_CODE (b->decl) != code)
3366 /* Definition isn't the kind we were looking for. */
3367 pending_invalid_xref = name;
3368 pending_invalid_xref_location = input_location;
3370 /* If in the same binding level as a declaration as a tag
3371 of a different type, this must not be allowed to
3372 shadow that tag, so give the error immediately.
3373 (For example, "struct foo; union foo;" is invalid.) */
3375 pending_xref_error ();
3384 /* Print an error message now
3385 for a recent invalid struct, union or enum cross reference.
3386 We don't print them immediately because they are not invalid
3387 when used in the `struct foo;' construct for shadowing. */
3390 pending_xref_error (void)
3392 if (pending_invalid_xref != 0)
3393 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3394 pending_invalid_xref);
3395 pending_invalid_xref = 0;
3399 /* Look up NAME in the current scope and its superiors
3400 in the namespace of variables, functions and typedefs.
3401 Return a ..._DECL node of some kind representing its definition,
3402 or return 0 if it is undefined. */
3405 lookup_name (tree name)
3407 struct c_binding *b = I_SYMBOL_BINDING (name);
3408 if (b && !b->invisible)
3413 /* Similar to `lookup_name' but look only at the indicated scope. */
3416 lookup_name_in_scope (tree name, struct c_scope *scope)
3418 struct c_binding *b;
3420 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3421 if (B_IN_SCOPE (b, scope))
3426 /* Create the predefined scalar types of C,
3427 and some nodes representing standard constants (0, 1, (void *) 0).
3428 Initialize the global scope.
3429 Make definitions for built-in primitive functions. */
3432 c_init_decl_processing (void)
3434 location_t save_loc = input_location;
3436 /* Initialize reserved words for parser. */
3439 current_function_decl = 0;
3441 gcc_obstack_init (&parser_obstack);
3443 /* Make the externals scope. */
3445 external_scope = current_scope;
3447 /* Declarations from c_common_nodes_and_builtins must not be associated
3448 with this input file, lest we get differences between using and not
3449 using preprocessed headers. */
3450 input_location = BUILTINS_LOCATION;
3452 build_common_tree_nodes (flag_signed_char);
3454 c_common_nodes_and_builtins ();
3456 /* In C, comparisons and TRUTH_* expressions have type int. */
3457 truthvalue_type_node = integer_type_node;
3458 truthvalue_true_node = integer_one_node;
3459 truthvalue_false_node = integer_zero_node;
3461 /* Even in C99, which has a real boolean type. */
3462 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
3463 boolean_type_node));
3465 input_location = save_loc;
3467 pedantic_lvalues = true;
3469 make_fname_decl = c_make_fname_decl;
3470 start_fname_decls ();
3473 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
3474 give the decl, NAME is the initialization string and TYPE_DEP
3475 indicates whether NAME depended on the type of the function. As we
3476 don't yet implement delayed emission of static data, we mark the
3477 decl as emitted so it is not placed in the output. Anything using
3478 it must therefore pull out the STRING_CST initializer directly.
3482 c_make_fname_decl (location_t loc, tree id, int type_dep)
3484 const char *name = fname_as_string (type_dep);
3485 tree decl, type, init;
3486 size_t length = strlen (name);
3488 type = build_array_type (char_type_node,
3489 build_index_type (size_int (length)));
3490 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
3492 decl = build_decl (loc, VAR_DECL, id, type);
3494 TREE_STATIC (decl) = 1;
3495 TREE_READONLY (decl) = 1;
3496 DECL_ARTIFICIAL (decl) = 1;
3498 init = build_string (length + 1, name);
3499 free (CONST_CAST (char *, name));
3500 TREE_TYPE (init) = type;
3501 DECL_INITIAL (decl) = init;
3503 TREE_USED (decl) = 1;
3505 if (current_function_decl
3506 /* For invalid programs like this:
3509 const char* p = __FUNCTION__;
3511 the __FUNCTION__ is believed to appear in K&R style function
3512 parameter declarator. In that case we still don't have
3514 && (!errorcount || current_function_scope))
3516 DECL_CONTEXT (decl) = current_function_decl;
3517 bind (id, decl, current_function_scope,
3518 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
3521 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
3527 c_builtin_function (tree decl)
3529 tree type = TREE_TYPE (decl);
3530 tree id = DECL_NAME (decl);
3532 const char *name = IDENTIFIER_POINTER (id);
3533 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
3535 /* Should never be called on a symbol with a preexisting meaning. */
3536 gcc_assert (!I_SYMBOL_BINDING (id));
3538 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
3541 /* Builtins in the implementation namespace are made visible without
3542 needing to be explicitly declared. See push_file_scope. */
3543 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3545 TREE_CHAIN (decl) = visible_builtins;
3546 visible_builtins = decl;
3553 c_builtin_function_ext_scope (tree decl)
3555 tree type = TREE_TYPE (decl);
3556 tree id = DECL_NAME (decl);
3558 const char *name = IDENTIFIER_POINTER (id);
3559 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
3561 /* Should never be called on a symbol with a preexisting meaning. */
3562 gcc_assert (!I_SYMBOL_BINDING (id));
3564 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
3567 /* Builtins in the implementation namespace are made visible without
3568 needing to be explicitly declared. See push_file_scope. */
3569 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3571 TREE_CHAIN (decl) = visible_builtins;
3572 visible_builtins = decl;
3578 /* Called when a declaration is seen that contains no names to declare.
3579 If its type is a reference to a structure, union or enum inherited
3580 from a containing scope, shadow that tag name for the current scope
3581 with a forward reference.
3582 If its type defines a new named structure or union
3583 or defines an enum, it is valid but we need not do anything here.
3584 Otherwise, it is an error. */
3587 shadow_tag (const struct c_declspecs *declspecs)
3589 shadow_tag_warned (declspecs, 0);
3592 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
3595 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
3597 bool found_tag = false;
3599 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
3601 tree value = declspecs->type;
3602 enum tree_code code = TREE_CODE (value);
3604 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
3605 /* Used to test also that TYPE_SIZE (value) != 0.
3606 That caused warning for `struct foo;' at top level in the file. */
3608 tree name = TYPE_NAME (value);
3613 if (declspecs->restrict_p)
3615 error ("invalid use of %<restrict%>");
3621 if (warned != 1 && code != ENUMERAL_TYPE)
3622 /* Empty unnamed enum OK */
3624 pedwarn (input_location, 0,
3625 "unnamed struct/union that defines no instances");
3629 else if (!declspecs->tag_defined_p
3630 && declspecs->storage_class != csc_none)
3633 pedwarn (input_location, 0,
3634 "empty declaration with storage class specifier "
3635 "does not redeclare tag");
3637 pending_xref_error ();
3639 else if (!declspecs->tag_defined_p
3640 && (declspecs->const_p
3641 || declspecs->volatile_p
3642 || declspecs->restrict_p
3643 || declspecs->address_space))
3646 pedwarn (input_location, 0,
3647 "empty declaration with type qualifier "
3648 "does not redeclare tag");
3650 pending_xref_error ();
3654 pending_invalid_xref = 0;
3655 t = lookup_tag (code, name, 1, NULL);
3659 t = make_node (code);
3660 pushtag (input_location, name, t);
3666 if (warned != 1 && !in_system_header)
3668 pedwarn (input_location, 0,
3669 "useless type name in empty declaration");
3674 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
3676 pedwarn (input_location, 0, "useless type name in empty declaration");
3680 pending_invalid_xref = 0;
3682 if (declspecs->inline_p)
3684 error ("%<inline%> in empty declaration");
3688 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3690 error ("%<auto%> in file-scope empty declaration");
3694 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3696 error ("%<register%> in file-scope empty declaration");
3700 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3702 warning (0, "useless storage class specifier in empty declaration");
3706 if (!warned && !in_system_header && declspecs->thread_p)
3708 warning (0, "useless %<__thread%> in empty declaration");
3712 if (!warned && !in_system_header && (declspecs->const_p
3713 || declspecs->volatile_p
3714 || declspecs->restrict_p
3715 || declspecs->address_space))
3717 warning (0, "useless type qualifier in empty declaration");
3724 pedwarn (input_location, 0, "empty declaration");
3729 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3730 bits. SPECS represents declaration specifiers that the grammar
3731 only permits to contain type qualifiers and attributes. */
3734 quals_from_declspecs (const struct c_declspecs *specs)
3736 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3737 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3738 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
3739 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
3740 gcc_assert (!specs->type
3741 && !specs->decl_attr
3742 && specs->typespec_word == cts_none
3743 && specs->storage_class == csc_none
3744 && !specs->typedef_p
3745 && !specs->explicit_signed_p
3746 && !specs->deprecated_p
3748 && !specs->long_long_p
3751 && !specs->unsigned_p
3752 && !specs->complex_p
3754 && !specs->thread_p);
3758 /* Construct an array declarator. LOC is the location of the
3759 beginning of the array (usually the opening brace). EXPR is the
3760 expression inside [], or NULL_TREE. QUALS are the type qualifiers
3761 inside the [] (to be applied to the pointer to which a parameter
3762 array is converted). STATIC_P is true if "static" is inside the
3763 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
3764 VLA of unspecified length which is nevertheless a complete type,
3765 false otherwise. The field for the contained declarator is left to
3766 be filled in by set_array_declarator_inner. */
3768 struct c_declarator *
3769 build_array_declarator (location_t loc,
3770 tree expr, struct c_declspecs *quals, bool static_p,
3773 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3774 struct c_declarator);
3775 declarator->id_loc = loc;
3776 declarator->kind = cdk_array;
3777 declarator->declarator = 0;
3778 declarator->u.array.dimen = expr;
3781 declarator->u.array.attrs = quals->attrs;
3782 declarator->u.array.quals = quals_from_declspecs (quals);
3786 declarator->u.array.attrs = NULL_TREE;
3787 declarator->u.array.quals = 0;
3789 declarator->u.array.static_p = static_p;
3790 declarator->u.array.vla_unspec_p = vla_unspec_p;
3793 if (static_p || quals != NULL)
3794 pedwarn (loc, OPT_pedantic,
3795 "ISO C90 does not support %<static%> or type "
3796 "qualifiers in parameter array declarators");
3798 pedwarn (loc, OPT_pedantic,
3799 "ISO C90 does not support %<[*]%> array declarators");
3803 if (!current_scope->parm_flag)
3806 error_at (loc, "%<[*]%> not allowed in other than "
3807 "function prototype scope");
3808 declarator->u.array.vla_unspec_p = false;
3811 current_scope->had_vla_unspec = true;
3816 /* Set the contained declarator of an array declarator. DECL is the
3817 declarator, as constructed by build_array_declarator; INNER is what
3818 appears on the left of the []. */
3820 struct c_declarator *
3821 set_array_declarator_inner (struct c_declarator *decl,
3822 struct c_declarator *inner)
3824 decl->declarator = inner;
3828 /* INIT is a constructor that forms DECL's initializer. If the final
3829 element initializes a flexible array field, add the size of that
3830 initializer to DECL's size. */
3833 add_flexible_array_elts_to_size (tree decl, tree init)
3837 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3840 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3841 type = TREE_TYPE (elt);
3842 if (TREE_CODE (type) == ARRAY_TYPE
3843 && TYPE_SIZE (type) == NULL_TREE
3844 && TYPE_DOMAIN (type) != NULL_TREE
3845 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3847 complete_array_type (&type, elt, false);
3849 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3850 DECL_SIZE_UNIT (decl)
3851 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3855 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3856 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3857 before the type name, and set *EXPR_CONST_OPERANDS, if
3858 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3859 appear in a constant expression. */
3862 groktypename (struct c_type_name *type_name, tree *expr,
3863 bool *expr_const_operands)
3866 tree attrs = type_name->specs->attrs;
3868 type_name->specs->attrs = NULL_TREE;
3870 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3871 false, NULL, &attrs, expr, expr_const_operands,
3874 /* Apply attributes. */
3875 decl_attributes (&type, attrs, 0);
3880 /* Decode a declarator in an ordinary declaration or data definition.
3881 This is called as soon as the type information and variable name
3882 have been parsed, before parsing the initializer if any.
3883 Here we create the ..._DECL node, fill in its type,
3884 and put it on the list of decls for the current context.
3885 The ..._DECL node is returned as the value.
3887 Exception: for arrays where the length is not specified,
3888 the type is left null, to be filled in by `finish_decl'.
3890 Function definitions do not come here; they go to start_function
3891 instead. However, external and forward declarations of functions
3892 do go through here. Structure field declarations are done by
3893 grokfield and not through here. */
3896 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3897 bool initialized, tree attributes)
3901 tree expr = NULL_TREE;
3902 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3904 /* An object declared as __attribute__((deprecated)) suppresses
3905 warnings of uses of other deprecated items. */
3906 if (lookup_attribute ("deprecated", attributes))
3907 deprecated_state = DEPRECATED_SUPPRESS;
3909 decl = grokdeclarator (declarator, declspecs,
3910 NORMAL, initialized, NULL, &attributes, &expr, NULL,
3918 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
3919 warning (OPT_Wmain, "%q+D is usually a function", decl);
3922 /* Is it valid for this decl to have an initializer at all?
3923 If not, set INITIALIZED to zero, which will indirectly
3924 tell 'finish_decl' to ignore the initializer once it is parsed. */
3925 switch (TREE_CODE (decl))
3928 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3933 error ("function %qD is initialized like a variable", decl);
3938 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3939 error ("parameter %qD is initialized", decl);
3944 /* Don't allow initializations for incomplete types except for
3945 arrays which might be completed by the initialization. */
3947 /* This can happen if the array size is an undefined macro.
3948 We already gave a warning, so we don't need another one. */
3949 if (TREE_TYPE (decl) == error_mark_node)
3951 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3953 /* A complete type is ok if size is fixed. */
3955 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3956 || C_DECL_VARIABLE_SIZE (decl))
3958 error ("variable-sized object may not be initialized");
3962 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3964 error ("variable %qD has initializer but incomplete type", decl);
3967 else if (C_DECL_VARIABLE_SIZE (decl))
3969 /* Although C99 is unclear about whether incomplete arrays
3970 of VLAs themselves count as VLAs, it does not make
3971 sense to permit them to be initialized given that
3972 ordinary VLAs may not be initialized. */
3973 error ("variable-sized object may not be initialized");
3980 if (current_scope == file_scope)
3981 TREE_STATIC (decl) = 1;
3983 /* Tell 'pushdecl' this is an initialized decl
3984 even though we don't yet have the initializer expression.
3985 Also tell 'finish_decl' it may store the real initializer. */
3986 DECL_INITIAL (decl) = error_mark_node;
3989 /* If this is a function declaration, write a record describing it to the
3990 prototypes file (if requested). */
3992 if (TREE_CODE (decl) == FUNCTION_DECL)
3993 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3995 /* ANSI specifies that a tentative definition which is not merged with
3996 a non-tentative definition behaves exactly like a definition with an
3997 initializer equal to zero. (Section 3.7.2)
3999 -fno-common gives strict ANSI behavior, though this tends to break
4000 a large body of code that grew up without this rule.
4002 Thread-local variables are never common, since there's no entrenched
4003 body of code to break, and it allows more efficient variable references
4004 in the presence of dynamic linking. */
4006 if (TREE_CODE (decl) == VAR_DECL
4008 && TREE_PUBLIC (decl)
4009 && !DECL_THREAD_LOCAL_P (decl)
4011 DECL_COMMON (decl) = 1;
4013 /* Set attributes here so if duplicate decl, will have proper attributes. */
4014 decl_attributes (&decl, attributes, 0);
4016 /* Handle gnu_inline attribute. */
4017 if (declspecs->inline_p
4018 && !flag_gnu89_inline
4019 && TREE_CODE (decl) == FUNCTION_DECL
4020 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4021 || current_function_decl))
4023 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4025 else if (declspecs->storage_class != csc_static)
4026 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4029 if (TREE_CODE (decl) == FUNCTION_DECL
4030 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4032 struct c_declarator *ce = declarator;
4034 if (ce->kind == cdk_pointer)
4035 ce = declarator->declarator;
4036 if (ce->kind == cdk_function)
4038 tree args = ce->u.arg_info->parms;
4039 for (; args; args = TREE_CHAIN (args))
4041 tree type = TREE_TYPE (args);
4042 if (type && INTEGRAL_TYPE_P (type)
4043 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4044 DECL_ARG_TYPE (args) = integer_type_node;
4049 if (TREE_CODE (decl) == FUNCTION_DECL
4050 && DECL_DECLARED_INLINE_P (decl)
4051 && DECL_UNINLINABLE (decl)
4052 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4053 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4056 /* C99 6.7.4p3: An inline definition of a function with external
4057 linkage shall not contain a definition of a modifiable object
4058 with static storage duration... */
4059 if (TREE_CODE (decl) == VAR_DECL
4060 && current_scope != file_scope
4061 && TREE_STATIC (decl)
4062 && !TREE_READONLY (decl)
4063 && DECL_DECLARED_INLINE_P (current_function_decl)
4064 && DECL_EXTERNAL (current_function_decl))
4065 record_inline_static (input_location, current_function_decl,
4066 decl, csi_modifiable);
4068 /* Add this decl to the current scope.
4069 TEM may equal DECL or it may be a previous decl of the same name. */
4070 tem = pushdecl (decl);
4072 if (initialized && DECL_EXTERNAL (tem))
4074 DECL_EXTERNAL (tem) = 0;
4075 TREE_STATIC (tem) = 1;
4081 /* Finish processing of a declaration;
4082 install its initial value.
4083 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4084 If the length of an array type is not known before,
4085 it must be determined now, from the initial value, or it is an error.
4087 INIT_LOC is the location of the initial value. */
4090 finish_decl (tree decl, location_t init_loc, tree init,
4091 tree origtype, tree asmspec_tree)
4094 bool was_incomplete = (DECL_SIZE (decl) == 0);
4095 const char *asmspec = 0;
4097 /* If a name was specified, get the string. */
4098 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
4099 && DECL_FILE_SCOPE_P (decl))
4100 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4102 asmspec = TREE_STRING_POINTER (asmspec_tree);
4104 if (TREE_CODE (decl) == VAR_DECL
4105 && TREE_STATIC (decl)
4106 && global_bindings_p ())
4107 /* So decl is a global variable. Record the types it uses
4108 so that we can decide later to emit debug info for them. */
4109 record_types_used_by_current_var_decl (decl);
4111 /* If `start_decl' didn't like having an initialization, ignore it now. */
4112 if (init != 0 && DECL_INITIAL (decl) == 0)
4115 /* Don't crash if parm is initialized. */
4116 if (TREE_CODE (decl) == PARM_DECL)
4120 store_init_value (init_loc, decl, init, origtype);
4122 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
4123 || TREE_CODE (decl) == FUNCTION_DECL
4124 || TREE_CODE (decl) == FIELD_DECL))
4125 objc_check_decl (decl);
4127 type = TREE_TYPE (decl);
4129 /* Deduce size of array from initialization, if not already known. */
4130 if (TREE_CODE (type) == ARRAY_TYPE
4131 && TYPE_DOMAIN (type) == 0
4132 && TREE_CODE (decl) != TYPE_DECL)
4135 = (TREE_STATIC (decl)
4136 /* Even if pedantic, an external linkage array
4137 may have incomplete type at first. */
4138 ? pedantic && !TREE_PUBLIC (decl)
4139 : !DECL_EXTERNAL (decl));
4141 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4144 /* Get the completed type made by complete_array_type. */
4145 type = TREE_TYPE (decl);
4150 error ("initializer fails to determine size of %q+D", decl);
4155 error ("array size missing in %q+D", decl);
4156 /* If a `static' var's size isn't known,
4157 make it extern as well as static, so it does not get
4159 If it is not `static', then do not mark extern;
4160 finish_incomplete_decl will give it a default size
4161 and it will get allocated. */
4162 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4163 DECL_EXTERNAL (decl) = 1;
4167 error ("zero or negative size array %q+D", decl);
4171 /* For global variables, update the copy of the type that
4172 exists in the binding. */
4173 if (TREE_PUBLIC (decl))
4175 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4176 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4177 b_ext = b_ext->shadowed;
4181 b_ext->u.type = composite_type (b_ext->u.type, type);
4183 b_ext->u.type = type;
4192 if (DECL_INITIAL (decl))
4193 TREE_TYPE (DECL_INITIAL (decl)) = type;
4195 layout_decl (decl, 0);
4198 if (TREE_CODE (decl) == VAR_DECL)
4200 if (init && TREE_CODE (init) == CONSTRUCTOR)
4201 add_flexible_array_elts_to_size (decl, init);
4203 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
4204 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4205 layout_decl (decl, 0);
4207 if (DECL_SIZE (decl) == 0
4208 /* Don't give an error if we already gave one earlier. */
4209 && TREE_TYPE (decl) != error_mark_node
4210 && (TREE_STATIC (decl)
4211 /* A static variable with an incomplete type
4212 is an error if it is initialized.
4213 Also if it is not file scope.
4214 Otherwise, let it through, but if it is not `extern'
4215 then it may cause an error message later. */
4216 ? (DECL_INITIAL (decl) != 0
4217 || !DECL_FILE_SCOPE_P (decl))
4218 /* An automatic variable with an incomplete type
4220 : !DECL_EXTERNAL (decl)))
4222 error ("storage size of %q+D isn%'t known", decl);
4223 TREE_TYPE (decl) = error_mark_node;
4226 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4227 && DECL_SIZE (decl) != 0)
4229 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4230 constant_expression_warning (DECL_SIZE (decl));
4233 error ("storage size of %q+D isn%'t constant", decl);
4234 TREE_TYPE (decl) = error_mark_node;
4238 if (TREE_USED (type))
4240 TREE_USED (decl) = 1;
4241 DECL_READ_P (decl) = 1;
4245 /* If this is a function and an assembler name is specified, reset DECL_RTL
4246 so we can give it its new name. Also, update built_in_decls if it
4247 was a normal built-in. */
4248 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
4250 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4251 set_builtin_user_assembler_name (decl, asmspec);
4252 set_user_assembler_name (decl, asmspec);
4255 /* If #pragma weak was used, mark the decl weak now. */
4256 maybe_apply_pragma_weak (decl);
4258 /* Output the assembler code and/or RTL code for variables and functions,
4259 unless the type is an undefined structure or union.
4260 If not, it will get done when the type is completed. */
4262 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
4264 /* Determine the ELF visibility. */
4265 if (TREE_PUBLIC (decl))
4266 c_determine_visibility (decl);
4268 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4269 if (c_dialect_objc ())
4270 objc_check_decl (decl);
4274 /* If this is not a static variable, issue a warning.
4275 It doesn't make any sense to give an ASMSPEC for an
4276 ordinary, non-register local variable. Historically,
4277 GCC has accepted -- but ignored -- the ASMSPEC in
4279 if (!DECL_FILE_SCOPE_P (decl)
4280 && TREE_CODE (decl) == VAR_DECL
4281 && !C_DECL_REGISTER (decl)
4282 && !TREE_STATIC (decl))
4283 warning (0, "ignoring asm-specifier for non-static local "
4284 "variable %q+D", decl);
4286 set_user_assembler_name (decl, asmspec);
4289 if (DECL_FILE_SCOPE_P (decl))
4291 if (DECL_INITIAL (decl) == NULL_TREE
4292 || DECL_INITIAL (decl) == error_mark_node)
4293 /* Don't output anything
4294 when a tentative file-scope definition is seen.
4295 But at end of compilation, do output code for them. */
4296 DECL_DEFER_OUTPUT (decl) = 1;
4297 rest_of_decl_compilation (decl, true, 0);
4301 /* In conjunction with an ASMSPEC, the `register'
4302 keyword indicates that we should place the variable
4303 in a particular register. */
4304 if (asmspec && C_DECL_REGISTER (decl))
4306 DECL_HARD_REGISTER (decl) = 1;
4307 /* This cannot be done for a structure with volatile
4308 fields, on which DECL_REGISTER will have been
4310 if (!DECL_REGISTER (decl))
4311 error ("cannot put object with volatile field into register");
4314 if (TREE_CODE (decl) != FUNCTION_DECL)
4316 /* If we're building a variable sized type, and we might be
4317 reachable other than via the top of the current binding
4318 level, then create a new BIND_EXPR so that we deallocate
4319 the object at the right time. */
4320 /* Note that DECL_SIZE can be null due to errors. */
4321 if (DECL_SIZE (decl)
4322 && !TREE_CONSTANT (DECL_SIZE (decl))
4323 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
4326 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4327 TREE_SIDE_EFFECTS (bind) = 1;
4329 BIND_EXPR_BODY (bind) = push_stmt_list ();
4331 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
4337 if (!DECL_FILE_SCOPE_P (decl))
4339 /* Recompute the RTL of a local array now
4340 if it used to be an incomplete type. */
4342 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
4344 /* If we used it already as memory, it must stay in memory. */
4345 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4346 /* If it's still incomplete now, no init will save it. */
4347 if (DECL_SIZE (decl) == 0)
4348 DECL_INITIAL (decl) = 0;
4353 if (TREE_CODE (decl) == TYPE_DECL)
4355 if (!DECL_FILE_SCOPE_P (decl)
4356 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
4357 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
4359 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
4362 /* At the end of a declaration, throw away any variable type sizes
4363 of types defined inside that declaration. There is no use
4364 computing them in the following function definition. */
4365 if (current_scope == file_scope)
4366 get_pending_sizes ();
4368 /* Install a cleanup (aka destructor) if one was given. */
4369 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
4371 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
4374 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
4375 tree cleanup_decl = lookup_name (cleanup_id);
4379 /* Build "cleanup(&decl)" for the destructor. */
4380 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
4381 vec = VEC_alloc (tree, gc, 1);
4382 VEC_quick_push (tree, vec, cleanup);
4383 cleanup = build_function_call_vec (DECL_SOURCE_LOCATION (decl),
4384 cleanup_decl, vec, NULL);
4385 VEC_free (tree, gc, vec);
4387 /* Don't warn about decl unused; the cleanup uses it. */
4388 TREE_USED (decl) = 1;
4389 TREE_USED (cleanup_decl) = 1;
4390 DECL_READ_P (decl) = 1;
4392 push_cleanup (decl, cleanup, false);
4397 && TREE_CODE (decl) == VAR_DECL
4398 && TREE_READONLY (decl)
4399 && !DECL_EXTERNAL (decl)
4400 && DECL_INITIAL (decl) == NULL_TREE)
4401 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4402 "uninitialized const %qD is invalid in C++", decl);
4405 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
4408 grokparm (const struct c_parm *parm)
4410 tree attrs = parm->attrs;
4411 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
4412 NULL, &attrs, NULL, NULL, DEPRECATED_NORMAL);
4414 decl_attributes (&decl, attrs, 0);
4419 /* Given a parsed parameter declaration, decode it into a PARM_DECL
4420 and push that on the current scope. */
4423 push_parm_decl (const struct c_parm *parm)
4425 tree attrs = parm->attrs;
4428 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
4429 &attrs, NULL, NULL, DEPRECATED_NORMAL);
4430 decl_attributes (&decl, attrs, 0);
4432 decl = pushdecl (decl);
4434 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
4437 /* Mark all the parameter declarations to date as forward decls.
4438 Also diagnose use of this extension. */
4441 mark_forward_parm_decls (void)
4443 struct c_binding *b;
4445 if (pedantic && !current_scope->warned_forward_parm_decls)
4447 pedwarn (input_location, OPT_pedantic,
4448 "ISO C forbids forward parameter declarations");
4449 current_scope->warned_forward_parm_decls = true;
4452 for (b = current_scope->bindings; b; b = b->prev)
4453 if (TREE_CODE (b->decl) == PARM_DECL)
4454 TREE_ASM_WRITTEN (b->decl) = 1;
4457 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
4458 literal, which may be an incomplete array type completed by the
4459 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
4460 literal. NON_CONST is true if the initializers contain something
4461 that cannot occur in a constant expression. */
4464 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
4466 /* We do not use start_decl here because we have a type, not a declarator;
4467 and do not use finish_decl because the decl should be stored inside
4468 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
4473 if (type == error_mark_node
4474 || init == error_mark_node)
4475 return error_mark_node;
4477 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
4478 DECL_EXTERNAL (decl) = 0;
4479 TREE_PUBLIC (decl) = 0;
4480 TREE_STATIC (decl) = (current_scope == file_scope);
4481 DECL_CONTEXT (decl) = current_function_decl;
4482 TREE_USED (decl) = 1;
4483 DECL_READ_P (decl) = 1;
4484 TREE_TYPE (decl) = type;
4485 TREE_READONLY (decl) = TYPE_READONLY (type);
4486 store_init_value (loc, decl, init, NULL_TREE);
4488 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
4490 int failure = complete_array_type (&TREE_TYPE (decl),
4491 DECL_INITIAL (decl), true);
4492 gcc_assert (!failure);
4494 type = TREE_TYPE (decl);
4495 TREE_TYPE (DECL_INITIAL (decl)) = type;
4498 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4499 return error_mark_node;
4501 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
4502 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
4503 TREE_SIDE_EFFECTS (complit) = 1;
4505 layout_decl (decl, 0);
4507 if (TREE_STATIC (decl))
4509 /* This decl needs a name for the assembler output. */
4510 set_compound_literal_name (decl);
4511 DECL_DEFER_OUTPUT (decl) = 1;
4512 DECL_COMDAT (decl) = 1;
4513 DECL_ARTIFICIAL (decl) = 1;
4514 DECL_IGNORED_P (decl) = 1;
4516 rest_of_decl_compilation (decl, 1, 0);
4521 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
4522 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
4528 /* Check the type of a compound literal. Here we just check that it
4529 is valid for C++. */
4532 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
4534 if (warn_cxx_compat && type_name->specs->tag_defined_p)
4535 warning_at (loc, OPT_Wc___compat,
4536 "defining a type in a compound literal is invalid in C++");
4539 /* Determine whether TYPE is a structure with a flexible array member,
4540 or a union containing such a structure (possibly recursively). */
4543 flexible_array_type_p (tree type)
4546 switch (TREE_CODE (type))
4549 x = TYPE_FIELDS (type);
4552 while (TREE_CHAIN (x) != NULL_TREE)
4554 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4555 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4556 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
4557 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
4561 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
4563 if (flexible_array_type_p (TREE_TYPE (x)))
4572 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
4573 replacing with appropriate values if they are invalid. */
4575 check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
4578 unsigned int max_width;
4579 unsigned HOST_WIDE_INT w;
4580 const char *name = (orig_name
4581 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
4582 : _("<anonymous>"));
4584 /* Detect and ignore out of range field width and process valid
4586 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
4588 error ("bit-field %qs width not an integer constant", name);
4589 *width = integer_one_node;
4593 if (TREE_CODE (*width) != INTEGER_CST)
4595 *width = c_fully_fold (*width, false, NULL);
4596 if (TREE_CODE (*width) == INTEGER_CST)
4597 pedwarn (input_location, OPT_pedantic,
4598 "bit-field %qs width not an integer constant expression",
4601 if (TREE_CODE (*width) != INTEGER_CST)
4603 error ("bit-field %qs width not an integer constant", name);
4604 *width = integer_one_node;
4606 constant_expression_warning (*width);
4607 if (tree_int_cst_sgn (*width) < 0)
4609 error ("negative width in bit-field %qs", name);
4610 *width = integer_one_node;
4612 else if (integer_zerop (*width) && orig_name)
4614 error ("zero width for bit-field %qs", name);
4615 *width = integer_one_node;
4619 /* Detect invalid bit-field type. */
4620 if (TREE_CODE (*type) != INTEGER_TYPE
4621 && TREE_CODE (*type) != BOOLEAN_TYPE
4622 && TREE_CODE (*type) != ENUMERAL_TYPE)
4624 error ("bit-field %qs has invalid type", name);
4625 *type = unsigned_type_node;
4628 type_mv = TYPE_MAIN_VARIANT (*type);
4629 if (!in_system_header
4630 && type_mv != integer_type_node
4631 && type_mv != unsigned_type_node
4632 && type_mv != boolean_type_node)
4633 pedwarn (input_location, OPT_pedantic,
4634 "type of bit-field %qs is a GCC extension", name);
4636 max_width = TYPE_PRECISION (*type);
4638 if (0 < compare_tree_int (*width, max_width))
4640 error ("width of %qs exceeds its type", name);
4642 *width = build_int_cst (NULL_TREE, w);
4645 w = tree_low_cst (*width, 1);
4647 if (TREE_CODE (*type) == ENUMERAL_TYPE)
4649 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
4651 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
4652 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
4653 warning (0, "%qs is narrower than values of its type", name);
4659 /* Print warning about variable length array if necessary. */
4662 warn_variable_length_array (tree name, tree size)
4664 int const_size = TREE_CONSTANT (size);
4666 if (!flag_isoc99 && pedantic && warn_vla != 0)
4671 pedwarn (input_location, OPT_Wvla,
4672 "ISO C90 forbids array %qE whose size "
4673 "can%'t be evaluated",
4676 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array whose size "
4677 "can%'t be evaluated");
4682 pedwarn (input_location, OPT_Wvla,
4683 "ISO C90 forbids variable length array %qE",
4686 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array");
4689 else if (warn_vla > 0)
4695 "the size of array %qE can"
4696 "%'t be evaluated", name);
4699 "the size of array can %'t be evaluated");
4705 "variable length array %qE is used",
4709 "variable length array is used");
4714 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR to
4715 serve as the actual size-expression for a type or decl. This is
4716 like variable_size in stor-layout.c, but we make global_bindings_p
4717 return negative to avoid calls to that function from outside the
4718 front end resulting in errors at file scope, then call this version
4719 instead from front-end code. */
4722 c_variable_size (tree size)
4726 if (TREE_CONSTANT (size))
4729 size = save_expr (size);
4731 save = skip_simple_arithmetic (size);
4733 if (cfun && cfun->dont_save_pending_sizes_p)
4736 if (!global_bindings_p ())
4737 put_pending_size (save);
4742 /* Given declspecs and a declarator,
4743 determine the name and type of the object declared
4744 and construct a ..._DECL node for it.
4745 (In one case we can return a ..._TYPE node instead.
4746 For invalid input we sometimes return 0.)
4748 DECLSPECS is a c_declspecs structure for the declaration specifiers.
4750 DECL_CONTEXT says which syntactic context this declaration is in:
4751 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4752 FUNCDEF for a function definition. Like NORMAL but a few different
4753 error messages in each case. Return value may be zero meaning
4754 this definition is too screwy to try to parse.
4755 PARM for a parameter declaration (either within a function prototype
4756 or before a function body). Make a PARM_DECL, or return void_type_node.
4757 TYPENAME if for a typename (in a cast or sizeof).
4758 Don't make a DECL node; just return the ..._TYPE node.
4759 FIELD for a struct or union field; make a FIELD_DECL.
4760 INITIALIZED is true if the decl has an initializer.
4761 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4762 representing the width of the bit-field.
4763 DECL_ATTRS points to the list of attributes that should be added to this
4764 decl. Any nested attributes that belong on the decl itself will be
4766 If EXPR is not NULL, any expressions that need to be evaluated as
4767 part of evaluating variably modified types will be stored in *EXPR.
4768 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
4769 set to indicate whether operands in *EXPR can be used in constant
4771 DEPRECATED_STATE is a deprecated_states value indicating whether
4772 deprecation warnings should be suppressed.
4774 In the TYPENAME case, DECLARATOR is really an absolute declarator.
4775 It may also be so in the PARM case, for a prototype where the
4776 argument type is specified but not the name.
4778 This function is where the complicated C meanings of `static'
4779 and `extern' are interpreted. */
4782 grokdeclarator (const struct c_declarator *declarator,
4783 struct c_declspecs *declspecs,
4784 enum decl_context decl_context, bool initialized, tree *width,
4785 tree *decl_attrs, tree *expr, bool *expr_const_operands,
4786 enum deprecated_states deprecated_state)
4788 tree type = declspecs->type;
4789 bool threadp = declspecs->thread_p;
4790 enum c_storage_class storage_class = declspecs->storage_class;
4794 int type_quals = TYPE_UNQUALIFIED;
4795 tree name = NULL_TREE;
4796 bool funcdef_flag = false;
4797 bool funcdef_syntax = false;
4798 bool size_varies = false;
4799 tree decl_attr = declspecs->decl_attr;
4800 int array_ptr_quals = TYPE_UNQUALIFIED;
4801 tree array_ptr_attrs = NULL_TREE;
4802 int array_parm_static = 0;
4803 bool array_parm_vla_unspec_p = false;
4804 tree returned_attrs = NULL_TREE;
4805 bool bitfield = width != NULL;
4807 struct c_arg_info *arg_info = 0;
4808 addr_space_t as1, as2, address_space;
4809 location_t loc = UNKNOWN_LOCATION;
4812 bool expr_const_operands_dummy;
4816 if (expr_const_operands == NULL)
4817 expr_const_operands = &expr_const_operands_dummy;
4819 *expr = declspecs->expr;
4820 *expr_const_operands = declspecs->expr_const_operands;
4822 if (decl_context == FUNCDEF)
4823 funcdef_flag = true, decl_context = NORMAL;
4825 /* Look inside a declarator for the name being declared
4826 and get it as an IDENTIFIER_NODE, for an error message. */
4828 const struct c_declarator *decl = declarator;
4839 funcdef_syntax = (decl->kind == cdk_function);
4840 decl = decl->declarator;
4844 decl = decl->declarator;
4859 gcc_assert (decl_context == PARM
4860 || decl_context == TYPENAME
4861 || (decl_context == FIELD
4862 && declarator->kind == cdk_id));
4863 gcc_assert (!initialized);
4867 /* A function definition's declarator must have the form of
4868 a function declarator. */
4870 if (funcdef_flag && !funcdef_syntax)
4873 /* If this looks like a function definition, make it one,
4874 even if it occurs where parms are expected.
4875 Then store_parm_decls will reject it and not use it as a parm. */
4876 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4877 decl_context = PARM;
4879 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4880 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
4882 if ((decl_context == NORMAL || decl_context == FIELD)
4883 && current_scope == file_scope
4884 && variably_modified_type_p (type, NULL_TREE))
4887 error_at (loc, "variably modified %qE at file scope", name);
4889 error_at (loc, "variably modified field at file scope");
4890 type = integer_type_node;
4893 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
4895 /* Diagnose defaulting to "int". */
4897 if (declspecs->default_int_p && !in_system_header)
4899 /* Issue a warning if this is an ISO C 99 program or if
4900 -Wreturn-type and this is a function, or if -Wimplicit;
4901 prefer the former warning since it is more explicit. */
4902 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4904 warn_about_return_type = 1;
4908 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wimplicit_int,
4909 "type defaults to %<int%> in declaration of %qE",
4912 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int,
4913 "type defaults to %<int%> in type name");
4917 /* Adjust the type if a bit-field is being declared,
4918 -funsigned-bitfields applied and the type is not explicitly
4920 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4921 && TREE_CODE (type) == INTEGER_TYPE)
4922 type = unsigned_type_for (type);
4924 /* Figure out the type qualifiers for the declaration. There are
4925 two ways a declaration can become qualified. One is something
4926 like `const int i' where the `const' is explicit. Another is
4927 something like `typedef const int CI; CI i' where the type of the
4928 declaration contains the `const'. A third possibility is that
4929 there is a type qualifier on the element type of a typedefed
4930 array type, in which case we should extract that qualifier so
4931 that c_apply_type_quals_to_decl receives the full list of
4932 qualifiers to work with (C90 is not entirely clear about whether
4933 duplicate qualifiers should be diagnosed in this case, but it
4934 seems most appropriate to do so). */
4935 element_type = strip_array_types (type);
4936 constp = declspecs->const_p + TYPE_READONLY (element_type);
4937 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4938 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4939 as1 = declspecs->address_space;
4940 as2 = TYPE_ADDR_SPACE (element_type);
4941 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
4943 if (pedantic && !flag_isoc99)
4946 pedwarn (loc, OPT_pedantic, "duplicate %<const%>");
4948 pedwarn (loc, OPT_pedantic, "duplicate %<restrict%>");
4950 pedwarn (loc, OPT_pedantic, "duplicate %<volatile%>");
4953 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
4954 error_at (loc, "conflicting named address spaces (%s vs %s)",
4955 c_addr_space_name (as1), c_addr_space_name (as2));
4957 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4958 type = TYPE_MAIN_VARIANT (type);
4959 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4960 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4961 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
4962 | ENCODE_QUAL_ADDR_SPACE (address_space));
4964 /* Warn about storage classes that are invalid for certain
4965 kinds of declarations (parameters, typenames, etc.). */
4969 || storage_class == csc_auto
4970 || storage_class == csc_register
4971 || storage_class == csc_typedef))
4973 if (storage_class == csc_auto)
4975 (current_scope == file_scope) ? 0 : OPT_pedantic,
4976 "function definition declared %<auto%>");
4977 if (storage_class == csc_register)
4978 error_at (loc, "function definition declared %<register%>");
4979 if (storage_class == csc_typedef)
4980 error_at (loc, "function definition declared %<typedef%>");
4982 error_at (loc, "function definition declared %<__thread%>");
4984 if (storage_class == csc_auto
4985 || storage_class == csc_register
4986 || storage_class == csc_typedef)
4987 storage_class = csc_none;
4989 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4991 if (decl_context == PARM && storage_class == csc_register)
4995 switch (decl_context)
4999 error_at (loc, "storage class specified for structure "
5002 error_at (loc, "storage class specified for structure field");
5006 error_at (loc, "storage class specified for parameter %qE",
5009 error_at (loc, "storage class specified for unnamed parameter");
5012 error_at (loc, "storage class specified for typename");
5015 storage_class = csc_none;
5019 else if (storage_class == csc_extern
5023 /* 'extern' with initialization is invalid if not at file scope. */
5024 if (current_scope == file_scope)
5026 /* It is fine to have 'extern const' when compiling at C
5027 and C++ intersection. */
5028 if (!(warn_cxx_compat && constp))
5029 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5033 error_at (loc, "%qE has both %<extern%> and initializer", name);
5035 else if (current_scope == file_scope)
5037 if (storage_class == csc_auto)
5038 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5040 if (pedantic && storage_class == csc_register)
5041 pedwarn (input_location, OPT_pedantic,
5042 "file-scope declaration of %qE specifies %<register%>", name);
5046 if (storage_class == csc_extern && funcdef_flag)
5047 error_at (loc, "nested function %qE declared %<extern%>", name);
5048 else if (threadp && storage_class == csc_none)
5050 error_at (loc, "function-scope %qE implicitly auto and declared "
5057 /* Now figure out the structure of the declarator proper.
5058 Descend through it, creating more complex types, until we reach
5059 the declared identifier (or NULL_TREE, in an absolute declarator).
5060 At each stage we maintain an unqualified version of the type
5061 together with any qualifiers that should be applied to it with
5062 c_build_qualified_type; this way, array types including
5063 multidimensional array types are first built up in unqualified
5064 form and then the qualified form is created with
5065 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5067 while (declarator && declarator->kind != cdk_id)
5069 if (type == error_mark_node)
5071 declarator = declarator->declarator;
5075 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5076 a cdk_pointer (for *...),
5077 a cdk_function (for ...(...)),
5078 a cdk_attrs (for nested attributes),
5079 or a cdk_id (for the name being declared
5080 or the place in an absolute declarator
5081 where the name was omitted).
5082 For the last case, we have just exited the loop.
5084 At this point, TYPE is the type of elements of an array,
5085 or for a function to return, or for a pointer to point to.
5086 After this sequence of ifs, TYPE is the type of the
5087 array or function or pointer, and DECLARATOR has had its
5088 outermost layer removed. */
5090 if (array_ptr_quals != TYPE_UNQUALIFIED
5091 || array_ptr_attrs != NULL_TREE
5092 || array_parm_static)
5094 /* Only the innermost declarator (making a parameter be of
5095 array type which is converted to pointer type)
5096 may have static or type qualifiers. */
5097 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5098 array_ptr_quals = TYPE_UNQUALIFIED;
5099 array_ptr_attrs = NULL_TREE;
5100 array_parm_static = 0;
5103 switch (declarator->kind)
5107 /* A declarator with embedded attributes. */
5108 tree attrs = declarator->u.attrs;
5109 const struct c_declarator *inner_decl;
5111 declarator = declarator->declarator;
5112 inner_decl = declarator;
5113 while (inner_decl->kind == cdk_attrs)
5114 inner_decl = inner_decl->declarator;
5115 if (inner_decl->kind == cdk_id)
5116 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5117 else if (inner_decl->kind == cdk_function)
5118 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5119 else if (inner_decl->kind == cdk_array)
5120 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5121 returned_attrs = decl_attributes (&type,
5122 chainon (returned_attrs, attrs),
5128 tree itype = NULL_TREE;
5129 tree size = declarator->u.array.dimen;
5130 /* The index is a signed object `sizetype' bits wide. */
5131 tree index_type = c_common_signed_type (sizetype);
5133 array_ptr_quals = declarator->u.array.quals;
5134 array_ptr_attrs = declarator->u.array.attrs;
5135 array_parm_static = declarator->u.array.static_p;
5136 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5138 declarator = declarator->declarator;
5140 /* Check for some types that there cannot be arrays of. */
5142 if (VOID_TYPE_P (type))
5145 error_at (loc, "declaration of %qE as array of voids", name);
5147 error_at (loc, "declaration of type name as array of voids");
5148 type = error_mark_node;
5151 if (TREE_CODE (type) == FUNCTION_TYPE)
5154 error_at (loc, "declaration of %qE as array of functions",
5157 error_at (loc, "declaration of type name as array of "
5159 type = error_mark_node;
5162 if (pedantic && !in_system_header && flexible_array_type_p (type))
5163 pedwarn (loc, OPT_pedantic,
5164 "invalid use of structure with flexible array member");
5166 if (size == error_mark_node)
5167 type = error_mark_node;
5169 if (type == error_mark_node)
5172 /* If size was specified, set ITYPE to a range-type for
5173 that size. Otherwise, ITYPE remains null. finish_decl
5174 may figure it out from an initial value. */
5178 bool size_maybe_const = true;
5179 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5180 && !TREE_OVERFLOW (size));
5181 bool this_size_varies = false;
5183 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5185 STRIP_TYPE_NOPS (size);
5187 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5190 error_at (loc, "size of array %qE has non-integer type",
5194 "size of unnamed array has non-integer type");
5195 size = integer_one_node;
5198 size = c_fully_fold (size, false, &size_maybe_const);
5200 if (pedantic && size_maybe_const && integer_zerop (size))
5203 pedwarn (loc, OPT_pedantic,
5204 "ISO C forbids zero-size array %qE", name);
5206 pedwarn (loc, OPT_pedantic,
5207 "ISO C forbids zero-size array");
5210 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
5212 constant_expression_warning (size);
5213 if (tree_int_cst_sgn (size) < 0)
5216 error_at (loc, "size of array %qE is negative", name);
5218 error_at (loc, "size of unnamed array is negative");
5219 size = integer_one_node;
5221 /* Handle a size folded to an integer constant but
5222 not an integer constant expression. */
5223 if (!size_int_const)
5225 /* If this is a file scope declaration of an
5226 ordinary identifier, this is invalid code;
5227 diagnosing it here and not subsequently
5228 treating the type as variable-length avoids
5229 more confusing diagnostics later. */
5230 if ((decl_context == NORMAL || decl_context == FIELD)
5231 && current_scope == file_scope)
5232 pedwarn (input_location, 0,
5233 "variably modified %qE at file scope",
5236 this_size_varies = size_varies = true;
5237 warn_variable_length_array (name, size);
5240 else if ((decl_context == NORMAL || decl_context == FIELD)
5241 && current_scope == file_scope)
5243 error_at (loc, "variably modified %qE at file scope", name);
5244 size = integer_one_node;
5248 /* Make sure the array size remains visibly
5249 nonconstant even if it is (eg) a const variable
5250 with known value. */
5251 this_size_varies = size_varies = true;
5252 warn_variable_length_array (name, size);
5255 if (integer_zerop (size) && !this_size_varies)
5257 /* A zero-length array cannot be represented with
5258 an unsigned index type, which is what we'll
5259 get with build_index_type. Create an
5260 open-ended range instead. */
5261 itype = build_range_type (sizetype, size, NULL_TREE);
5265 /* Arrange for the SAVE_EXPR on the inside of the
5266 MINUS_EXPR, which allows the -1 to get folded
5267 with the +1 that happens when building TYPE_SIZE. */
5269 size = c_variable_size (size);
5270 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
5271 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5272 integer_zero_node, size);
5274 /* Compute the maximum valid index, that is, size
5275 - 1. Do the calculation in index_type, so that
5276 if it is a variable the computations will be
5277 done in the proper mode. */
5278 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
5279 convert (index_type, size),
5280 convert (index_type,
5283 /* If that overflowed, the array is too big. ???
5284 While a size of INT_MAX+1 technically shouldn't
5285 cause an overflow (because we subtract 1), the
5286 overflow is recorded during the conversion to
5287 index_type, before the subtraction. Handling
5288 this case seems like an unnecessary
5290 if (TREE_CODE (itype) == INTEGER_CST
5291 && TREE_OVERFLOW (itype))
5294 error_at (loc, "size of array %qE is too large",
5297 error_at (loc, "size of unnamed array is too large");
5298 type = error_mark_node;
5302 itype = build_index_type (itype);
5304 if (this_size_varies)
5307 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5311 *expr_const_operands &= size_maybe_const;
5314 else if (decl_context == FIELD)
5316 bool flexible_array_member = false;
5317 if (array_parm_vla_unspec_p)
5318 /* Field names can in fact have function prototype
5319 scope so [*] is disallowed here through making
5320 the field variably modified, not through being
5321 something other than a declaration with function
5326 const struct c_declarator *t = declarator;
5327 while (t->kind == cdk_attrs)
5329 flexible_array_member = (t->kind == cdk_id);
5331 if (flexible_array_member
5332 && pedantic && !flag_isoc99 && !in_system_header)
5333 pedwarn (loc, OPT_pedantic,
5334 "ISO C90 does not support flexible array members");
5336 /* ISO C99 Flexible array members are effectively
5337 identical to GCC's zero-length array extension. */
5338 if (flexible_array_member || array_parm_vla_unspec_p)
5339 itype = build_range_type (sizetype, size_zero_node,
5342 else if (decl_context == PARM)
5344 if (array_parm_vla_unspec_p)
5346 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
5350 else if (decl_context == TYPENAME)
5352 if (array_parm_vla_unspec_p)
5355 warning (0, "%<[*]%> not in a declaration");
5356 /* We use this to avoid messing up with incomplete
5357 array types of the same type, that would
5358 otherwise be modified below. */
5359 itype = build_range_type (sizetype, size_zero_node,
5365 /* Complain about arrays of incomplete types. */
5366 if (!COMPLETE_TYPE_P (type))
5368 error_at (loc, "array type has incomplete element type");
5369 type = error_mark_node;
5372 /* When itype is NULL, a shared incomplete array type is
5373 returned for all array of a given type. Elsewhere we
5374 make sure we don't complete that type before copying
5375 it, but here we want to make sure we don't ever
5376 modify the shared type, so we gcc_assert (itype)
5379 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
5380 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
5381 type = build_qualified_type (type,
5382 ENCODE_QUAL_ADDR_SPACE (as));
5384 type = build_array_type (type, itype);
5387 if (type != error_mark_node)
5391 /* It is ok to modify type here even if itype is
5392 NULL: if size_varies, we're in a
5393 multi-dimensional array and the inner type has
5394 variable size, so the enclosing shared array type
5396 if (size && TREE_CODE (size) == INTEGER_CST)
5398 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5399 C_TYPE_VARIABLE_SIZE (type) = 1;
5402 /* The GCC extension for zero-length arrays differs from
5403 ISO flexible array members in that sizeof yields
5405 if (size && integer_zerop (size))
5408 TYPE_SIZE (type) = bitsize_zero_node;
5409 TYPE_SIZE_UNIT (type) = size_zero_node;
5410 SET_TYPE_STRUCTURAL_EQUALITY (type);
5412 if (array_parm_vla_unspec_p)
5415 /* The type is complete. C99 6.7.5.2p4 */
5416 TYPE_SIZE (type) = bitsize_zero_node;
5417 TYPE_SIZE_UNIT (type) = size_zero_node;
5418 SET_TYPE_STRUCTURAL_EQUALITY (type);
5422 if (decl_context != PARM
5423 && (array_ptr_quals != TYPE_UNQUALIFIED
5424 || array_ptr_attrs != NULL_TREE
5425 || array_parm_static))
5427 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5428 array_ptr_quals = TYPE_UNQUALIFIED;
5429 array_ptr_attrs = NULL_TREE;
5430 array_parm_static = 0;
5436 /* Say it's a definition only for the declarator closest
5437 to the identifier, apart possibly from some
5439 bool really_funcdef = false;
5443 const struct c_declarator *t = declarator->declarator;
5444 while (t->kind == cdk_attrs)
5446 really_funcdef = (t->kind == cdk_id);
5449 /* Declaring a function type. Make sure we have a valid
5450 type for the function to return. */
5451 if (type == error_mark_node)
5454 size_varies = false;
5456 /* Warn about some types functions can't return. */
5457 if (TREE_CODE (type) == FUNCTION_TYPE)
5460 error_at (loc, "%qE declared as function returning a "
5463 error_at (loc, "type name declared as function "
5464 "returning a function");
5465 type = integer_type_node;
5467 if (TREE_CODE (type) == ARRAY_TYPE)
5470 error_at (loc, "%qE declared as function returning an array",
5473 error_at (loc, "type name declared as function returning "
5475 type = integer_type_node;
5477 errmsg = targetm.invalid_return_type (type);
5481 type = integer_type_node;
5484 /* Construct the function type and go to the next
5485 inner layer of declarator. */
5486 arg_info = declarator->u.arg_info;
5487 arg_types = grokparms (arg_info, really_funcdef);
5489 put_pending_sizes (arg_info->pending_sizes);
5491 /* Type qualifiers before the return type of the function
5492 qualify the return type, not the function type. */
5495 /* Type qualifiers on a function return type are
5496 normally permitted by the standard but have no
5497 effect, so give a warning at -Wreturn-type.
5498 Qualifiers on a void return type are banned on
5499 function definitions in ISO C; GCC used to used
5500 them for noreturn functions. */
5501 if (VOID_TYPE_P (type) && really_funcdef)
5503 "function definition has qualified void return type");
5505 warning_at (loc, OPT_Wignored_qualifiers,
5506 "type qualifiers ignored on function return type");
5508 type = c_build_qualified_type (type, type_quals);
5510 type_quals = TYPE_UNQUALIFIED;
5512 type = build_function_type (type, arg_types);
5513 declarator = declarator->declarator;
5515 /* Set the TYPE_CONTEXTs for each tagged type which is local to
5516 the formal parameter list of this FUNCTION_TYPE to point to
5517 the FUNCTION_TYPE node itself. */
5521 for (link = arg_info->tags;
5523 link = TREE_CHAIN (link))
5524 TYPE_CONTEXT (TREE_VALUE (link)) = type;
5530 /* Merge any constancy or volatility into the target type
5533 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5535 pedwarn (loc, OPT_pedantic,
5536 "ISO C forbids qualified function types");
5538 type = c_build_qualified_type (type, type_quals);
5539 size_varies = false;
5541 /* When the pointed-to type involves components of variable size,
5542 care must be taken to ensure that the size evaluation code is
5543 emitted early enough to dominate all the possible later uses
5544 and late enough for the variables on which it depends to have
5547 This is expected to happen automatically when the pointed-to
5548 type has a name/declaration of it's own, but special attention
5549 is required if the type is anonymous.
5551 We handle the NORMAL and FIELD contexts here by attaching an
5552 artificial TYPE_DECL to such pointed-to type. This forces the
5553 sizes evaluation at a safe point and ensures it is not deferred
5554 until e.g. within a deeper conditional context.
5556 We expect nothing to be needed here for PARM or TYPENAME.
5557 Pushing a TYPE_DECL at this point for TYPENAME would actually
5558 be incorrect, as we might be in the middle of an expression
5559 with side effects on the pointed-to type size "arguments" prior
5560 to the pointer declaration point and the fake TYPE_DECL in the
5561 enclosing context would force the size evaluation prior to the
5564 if (!TYPE_NAME (type)
5565 && (decl_context == NORMAL || decl_context == FIELD)
5566 && variably_modified_type_p (type, NULL_TREE))
5568 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
5569 DECL_ARTIFICIAL (decl) = 1;
5571 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
5572 TYPE_NAME (type) = decl;
5575 type = build_pointer_type (type);
5577 /* Process type qualifiers (such as const or volatile)
5578 that were given inside the `*'. */
5579 type_quals = declarator->u.pointer_quals;
5581 declarator = declarator->declarator;
5588 *decl_attrs = chainon (returned_attrs, *decl_attrs);
5590 /* Now TYPE has the actual type, apart from any qualifiers in
5593 /* Warn about address space used for things other than static memory or
5595 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
5596 if (!ADDR_SPACE_GENERIC_P (address_space))
5598 if (decl_context == NORMAL)
5600 switch (storage_class)
5603 error ("%qs combined with %<auto%> qualifier for %qE",
5604 c_addr_space_name (address_space), name);
5607 error ("%qs combined with %<register%> qualifier for %qE",
5608 c_addr_space_name (address_space), name);
5611 if (current_function_scope)
5613 error ("%qs specified for auto variable %qE",
5614 c_addr_space_name (address_space), name);
5626 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
5629 error ("%qs specified for parameter %qE",
5630 c_addr_space_name (address_space), name);
5632 error ("%qs specified for unnamed parameter",
5633 c_addr_space_name (address_space));
5635 else if (decl_context == FIELD)
5638 error ("%qs specified for structure field %qE",
5639 c_addr_space_name (address_space), name);
5641 error ("%qs specified for structure field",
5642 c_addr_space_name (address_space));
5646 /* Check the type and width of a bit-field. */
5648 check_bitfield_type_and_width (&type, width, name);
5650 /* Did array size calculations overflow? */
5652 if (TREE_CODE (type) == ARRAY_TYPE
5653 && COMPLETE_TYPE_P (type)
5654 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
5655 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
5658 error_at (loc, "size of array %qE is too large", name);
5660 error_at (loc, "size of unnamed array is too large");
5661 /* If we proceed with the array type as it is, we'll eventually
5662 crash in tree_low_cst(). */
5663 type = error_mark_node;
5666 /* If this is declaring a typedef name, return a TYPE_DECL. */
5668 if (storage_class == csc_typedef)
5671 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5673 pedwarn (loc, OPT_pedantic,
5674 "ISO C forbids qualified function types");
5676 type = c_build_qualified_type (type, type_quals);
5677 decl = build_decl (declarator->id_loc,
5678 TYPE_DECL, declarator->u.id, type);
5679 if (declspecs->explicit_signed_p)
5680 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
5681 if (declspecs->inline_p)
5682 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
5684 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
5686 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
5689 && b->decl != NULL_TREE
5690 && (B_IN_CURRENT_SCOPE (b)
5691 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
5692 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
5694 warning_at (declarator->id_loc, OPT_Wc___compat,
5695 ("using %qD as both a typedef and a tag is "
5698 if (b->locus != UNKNOWN_LOCATION)
5699 inform (b->locus, "originally defined here");
5706 /* If this is a type name (such as, in a cast or sizeof),
5707 compute the type and return it now. */
5709 if (decl_context == TYPENAME)
5711 /* Note that the grammar rejects storage classes in typenames
5713 gcc_assert (storage_class == csc_none && !threadp
5714 && !declspecs->inline_p);
5715 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5717 pedwarn (loc, OPT_pedantic,
5718 "ISO C forbids const or volatile function types");
5720 type = c_build_qualified_type (type, type_quals);
5724 if (pedantic && decl_context == FIELD
5725 && variably_modified_type_p (type, NULL_TREE))
5728 pedwarn (loc, OPT_pedantic, "a member of a structure or union cannot "
5729 "have a variably modified type");
5732 /* Aside from typedefs and type names (handle above),
5733 `void' at top level (not within pointer)
5734 is allowed only in public variables.
5735 We don't complain about parms either, but that is because
5736 a better error message can be made later. */
5738 if (VOID_TYPE_P (type) && decl_context != PARM
5739 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
5740 && (storage_class == csc_extern
5741 || (current_scope == file_scope
5742 && !(storage_class == csc_static
5743 || storage_class == csc_register)))))
5745 error_at (loc, "variable or field %qE declared void", name);
5746 type = integer_type_node;
5749 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
5750 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
5755 if (decl_context == PARM)
5759 /* A parameter declared as an array of T is really a pointer to T.
5760 One declared as a function is really a pointer to a function. */
5762 if (TREE_CODE (type) == ARRAY_TYPE)
5764 /* Transfer const-ness of array into that of type pointed to. */
5765 type = TREE_TYPE (type);
5767 type = c_build_qualified_type (type, type_quals);
5768 type = build_pointer_type (type);
5769 type_quals = array_ptr_quals;
5771 type = c_build_qualified_type (type, type_quals);
5773 /* We don't yet implement attributes in this context. */
5774 if (array_ptr_attrs != NULL_TREE)
5775 warning_at (loc, OPT_Wattributes,
5776 "attributes in parameter array declarator ignored");
5778 size_varies = false;
5780 else if (TREE_CODE (type) == FUNCTION_TYPE)
5783 pedwarn (loc, OPT_pedantic,
5784 "ISO C forbids qualified function types");
5786 type = c_build_qualified_type (type, type_quals);
5787 type = build_pointer_type (type);
5788 type_quals = TYPE_UNQUALIFIED;
5790 else if (type_quals)
5791 type = c_build_qualified_type (type, type_quals);
5793 decl = build_decl (declarator->id_loc,
5794 PARM_DECL, declarator->u.id, type);
5796 C_DECL_VARIABLE_SIZE (decl) = 1;
5798 /* Compute the type actually passed in the parmlist,
5799 for the case where there is no prototype.
5800 (For example, shorts and chars are passed as ints.)
5801 When there is a prototype, this is overridden later. */
5803 if (type == error_mark_node)
5804 promoted_type = type;
5806 promoted_type = c_type_promotes_to (type);
5808 DECL_ARG_TYPE (decl) = promoted_type;
5809 if (declspecs->inline_p)
5810 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
5812 else if (decl_context == FIELD)
5814 /* Note that the grammar rejects storage classes in typenames
5816 gcc_assert (storage_class == csc_none && !threadp
5817 && !declspecs->inline_p);
5819 /* Structure field. It may not be a function. */
5821 if (TREE_CODE (type) == FUNCTION_TYPE)
5823 error_at (loc, "field %qE declared as a function", name);
5824 type = build_pointer_type (type);
5826 else if (TREE_CODE (type) != ERROR_MARK
5827 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
5830 error_at (loc, "field %qE has incomplete type", name);
5832 error_at (loc, "unnamed field has incomplete type");
5833 type = error_mark_node;
5835 type = c_build_qualified_type (type, type_quals);
5836 decl = build_decl (declarator->id_loc,
5837 FIELD_DECL, declarator->u.id, type);
5838 DECL_NONADDRESSABLE_P (decl) = bitfield;
5839 if (bitfield && !declarator->u.id)
5840 TREE_NO_WARNING (decl) = 1;
5843 C_DECL_VARIABLE_SIZE (decl) = 1;
5845 else if (TREE_CODE (type) == FUNCTION_TYPE)
5847 if (storage_class == csc_register || threadp)
5849 error_at (loc, "invalid storage class for function %qE", name);
5851 else if (current_scope != file_scope)
5853 /* Function declaration not at file scope. Storage
5854 classes other than `extern' are not allowed, C99
5855 6.7.1p5, and `extern' makes no difference. However,
5856 GCC allows 'auto', perhaps with 'inline', to support
5857 nested functions. */
5858 if (storage_class == csc_auto)
5859 pedwarn (loc, OPT_pedantic,
5860 "invalid storage class for function %qE", name);
5861 else if (storage_class == csc_static)
5863 error_at (loc, "invalid storage class for function %qE", name);
5865 storage_class = declspecs->storage_class = csc_none;
5871 decl = build_decl (declarator->id_loc,
5872 FUNCTION_DECL, declarator->u.id, type);
5873 decl = build_decl_attribute_variant (decl, decl_attr);
5875 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
5876 pedwarn (loc, OPT_pedantic,
5877 "ISO C forbids qualified function types");
5879 /* GNU C interprets a volatile-qualified function type to indicate
5880 that the function does not return. */
5881 if ((type_quals & TYPE_QUAL_VOLATILE)
5882 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5883 warning_at (loc, 0, "%<noreturn%> function returns non-void value");
5885 /* Every function declaration is an external reference
5886 (DECL_EXTERNAL) except for those which are not at file
5887 scope and are explicitly declared "auto". This is
5888 forbidden by standard C (C99 6.7.1p5) and is interpreted by
5889 GCC to signify a forward declaration of a nested function. */
5890 if (storage_class == csc_auto && current_scope != file_scope)
5891 DECL_EXTERNAL (decl) = 0;
5892 /* In C99, a function which is declared 'inline' with 'extern'
5893 is not an external reference (which is confusing). It
5894 means that the later definition of the function must be output
5895 in this file, C99 6.7.4p6. In GNU C89, a function declared
5896 'extern inline' is an external reference. */
5897 else if (declspecs->inline_p && storage_class != csc_static)
5898 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
5899 == flag_gnu89_inline);
5901 DECL_EXTERNAL (decl) = !initialized;
5903 /* Record absence of global scope for `static' or `auto'. */
5905 = !(storage_class == csc_static || storage_class == csc_auto);
5907 /* For a function definition, record the argument information
5908 block where store_parm_decls will look for it. */
5910 current_function_arg_info = arg_info;
5912 if (declspecs->default_int_p)
5913 C_FUNCTION_IMPLICIT_INT (decl) = 1;
5915 /* Record presence of `inline', if it is reasonable. */
5916 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
5918 if (declspecs->inline_p)
5919 pedwarn (loc, 0, "cannot inline function %<main%>");
5921 else if (declspecs->inline_p)
5922 /* Record that the function is declared `inline'. */
5923 DECL_DECLARED_INLINE_P (decl) = 1;
5927 /* It's a variable. */
5928 /* An uninitialized decl with `extern' is a reference. */
5929 int extern_ref = !initialized && storage_class == csc_extern;
5931 type = c_build_qualified_type (type, type_quals);
5933 /* C99 6.2.2p7: It is invalid (compile-time undefined
5934 behavior) to create an 'extern' declaration for a
5935 variable if there is a global declaration that is
5936 'static' and the global declaration is not visible.
5937 (If the static declaration _is_ currently visible,
5938 the 'extern' declaration is taken to refer to that decl.) */
5939 if (extern_ref && current_scope != file_scope)
5941 tree global_decl = identifier_global_value (declarator->u.id);
5942 tree visible_decl = lookup_name (declarator->u.id);
5945 && global_decl != visible_decl
5946 && TREE_CODE (global_decl) == VAR_DECL
5947 && !TREE_PUBLIC (global_decl))
5948 error_at (loc, "variable previously declared %<static%> "
5949 "redeclared %<extern%>");
5952 decl = build_decl (declarator->id_loc,
5953 VAR_DECL, declarator->u.id, type);
5955 C_DECL_VARIABLE_SIZE (decl) = 1;
5957 if (declspecs->inline_p)
5958 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
5960 /* At file scope, an initialized extern declaration may follow
5961 a static declaration. In that case, DECL_EXTERNAL will be
5962 reset later in start_decl. */
5963 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
5965 /* At file scope, the presence of a `static' or `register' storage
5966 class specifier, or the absence of all storage class specifiers
5967 makes this declaration a definition (perhaps tentative). Also,
5968 the absence of `static' makes it public. */
5969 if (current_scope == file_scope)
5971 TREE_PUBLIC (decl) = storage_class != csc_static;
5972 TREE_STATIC (decl) = !extern_ref;
5974 /* Not at file scope, only `static' makes a static definition. */
5977 TREE_STATIC (decl) = (storage_class == csc_static);
5978 TREE_PUBLIC (decl) = extern_ref;
5982 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
5985 if ((storage_class == csc_extern
5986 || (storage_class == csc_none
5987 && TREE_CODE (type) == FUNCTION_TYPE
5989 && variably_modified_type_p (type, NULL_TREE))
5992 if (TREE_CODE (type) == FUNCTION_TYPE)
5993 error_at (loc, "non-nested function with variably modified type");
5995 error_at (loc, "object with variably modified type must have "
5999 /* Record `register' declaration for warnings on &
6000 and in case doing stupid register allocation. */
6002 if (storage_class == csc_register)
6004 C_DECL_REGISTER (decl) = 1;
6005 DECL_REGISTER (decl) = 1;
6008 /* Record constancy and volatility. */
6009 c_apply_type_quals_to_decl (type_quals, decl);
6011 /* If a type has volatile components, it should be stored in memory.
6012 Otherwise, the fact that those components are volatile
6013 will be ignored, and would even crash the compiler.
6014 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6015 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6016 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
6017 || TREE_CODE (decl) == RESULT_DECL))
6019 /* It is not an error for a structure with volatile fields to
6020 be declared register, but reset DECL_REGISTER since it
6021 cannot actually go in a register. */
6022 int was_reg = C_DECL_REGISTER (decl);
6023 C_DECL_REGISTER (decl) = 0;
6024 DECL_REGISTER (decl) = 0;
6025 c_mark_addressable (decl);
6026 C_DECL_REGISTER (decl) = was_reg;
6029 /* This is the earliest point at which we might know the assembler
6030 name of a variable. Thus, if it's known before this, die horribly. */
6031 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
6034 && TREE_CODE (decl) == VAR_DECL
6035 && TREE_PUBLIC (decl)
6036 && TREE_STATIC (decl)
6037 && (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6038 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6039 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6040 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
6041 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6042 ("non-local variable %qD with anonymous type is "
6043 "questionable in C++"),
6050 /* Decode the parameter-list info for a function type or function definition.
6051 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6052 if there is an identifier list instead of a parameter decl list).
6053 These two functions are separate because when a function returns
6054 or receives functions then each is called multiple times but the order
6055 of calls is different. The last call to `grokparms' is always the one
6056 that contains the formal parameter names of a function definition.
6058 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6060 FUNCDEF_FLAG is true for a function definition, false for
6061 a mere declaration. A nonempty identifier-list gets an error message
6062 when FUNCDEF_FLAG is false. */
6065 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
6067 tree arg_types = arg_info->types;
6069 if (funcdef_flag && arg_info->had_vla_unspec)
6071 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6073 error ("%<[*]%> not allowed in other than function prototype scope");
6076 if (arg_types == 0 && !funcdef_flag && !in_system_header)
6077 warning (OPT_Wstrict_prototypes,
6078 "function declaration isn%'t a prototype");
6080 if (arg_types == error_mark_node)
6081 return 0; /* don't set TYPE_ARG_TYPES in this case */
6083 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
6086 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
6088 arg_info->parms = arg_info->types;
6089 arg_info->types = 0;
6094 tree parm, type, typelt;
6095 unsigned int parmno;
6098 /* If there is a parameter of incomplete type in a definition,
6099 this is an error. In a declaration this is valid, and a
6100 struct or union type may be completed later, before any calls
6101 or definition of the function. In the case where the tag was
6102 first declared within the parameter list, a warning has
6103 already been given. If a parameter has void type, then
6104 however the function cannot be defined or called, so
6107 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
6109 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
6111 type = TREE_VALUE (typelt);
6112 if (type == error_mark_node)
6115 if (!COMPLETE_TYPE_P (type))
6119 if (DECL_NAME (parm))
6120 error_at (input_location,
6121 "parameter %u (%q+D) has incomplete type",
6124 error_at (DECL_SOURCE_LOCATION (parm),
6125 "parameter %u has incomplete type",
6128 TREE_VALUE (typelt) = error_mark_node;
6129 TREE_TYPE (parm) = error_mark_node;
6130 arg_types = NULL_TREE;
6132 else if (VOID_TYPE_P (type))
6134 if (DECL_NAME (parm))
6135 warning_at (input_location, 0,
6136 "parameter %u (%q+D) has void type",
6139 warning_at (DECL_SOURCE_LOCATION (parm), 0,
6140 "parameter %u has void type",
6145 errmsg = targetm.invalid_parameter_type (type);
6149 TREE_VALUE (typelt) = error_mark_node;
6150 TREE_TYPE (parm) = error_mark_node;
6151 arg_types = NULL_TREE;
6154 if (DECL_NAME (parm) && TREE_USED (parm))
6155 warn_if_shadowing (parm);
6161 /* Take apart the current scope and return a c_arg_info structure with
6162 info on a parameter list just parsed.
6164 This structure is later fed to 'grokparms' and 'store_parm_decls'.
6166 ELLIPSIS being true means the argument list ended in '...' so don't
6167 append a sentinel (void_list_node) to the end of the type-list. */
6170 get_parm_info (bool ellipsis)
6172 struct c_binding *b = current_scope->bindings;
6173 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
6180 static bool explained_incomplete_types = false;
6181 bool gave_void_only_once_err = false;
6183 arg_info->parms = 0;
6185 arg_info->types = 0;
6186 arg_info->others = 0;
6187 arg_info->pending_sizes = 0;
6188 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
6190 /* The bindings in this scope must not get put into a block.
6191 We will take care of deleting the binding nodes. */
6192 current_scope->bindings = 0;
6194 /* This function is only called if there was *something* on the
6198 /* A parameter list consisting solely of 'void' indicates that the
6199 function takes no arguments. But if the 'void' is qualified
6200 (by 'const' or 'volatile'), or has a storage class specifier
6201 ('register'), then the behavior is undefined; issue an error.
6202 Typedefs for 'void' are OK (see DR#157). */
6203 if (b->prev == 0 /* one binding */
6204 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
6205 && !DECL_NAME (b->decl) /* anonymous */
6206 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
6208 if (TREE_THIS_VOLATILE (b->decl)
6209 || TREE_READONLY (b->decl)
6210 || C_DECL_REGISTER (b->decl))
6211 error ("%<void%> as only parameter may not be qualified");
6213 /* There cannot be an ellipsis. */
6215 error ("%<void%> must be the only parameter");
6217 arg_info->types = void_list_node;
6222 types = void_list_node;
6224 /* Break up the bindings list into parms, tags, types, and others;
6225 apply sanity checks; purge the name-to-decl bindings. */
6228 tree decl = b->decl;
6229 tree type = TREE_TYPE (decl);
6230 const char *keyword;
6232 switch (TREE_CODE (decl))
6237 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6238 I_SYMBOL_BINDING (b->id) = b->shadowed;
6241 /* Check for forward decls that never got their actual decl. */
6242 if (TREE_ASM_WRITTEN (decl))
6243 error ("parameter %q+D has just a forward declaration", decl);
6244 /* Check for (..., void, ...) and issue an error. */
6245 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
6247 if (!gave_void_only_once_err)
6249 error ("%<void%> must be the only parameter");
6250 gave_void_only_once_err = true;
6255 /* Valid parameter, add it to the list. */
6256 TREE_CHAIN (decl) = parms;
6259 /* Since there is a prototype, args are passed in their
6260 declared types. The back end may override this later. */
6261 DECL_ARG_TYPE (decl) = type;
6262 types = tree_cons (0, type, types);
6266 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
6267 case UNION_TYPE: keyword = "union"; goto tag;
6268 case RECORD_TYPE: keyword = "struct"; goto tag;
6270 /* Types may not have tag-names, in which case the type
6271 appears in the bindings list with b->id NULL. */
6274 gcc_assert (I_TAG_BINDING (b->id) == b);
6275 I_TAG_BINDING (b->id) = b->shadowed;
6278 /* Warn about any struct, union or enum tags defined in a
6279 parameter list. The scope of such types is limited to
6280 the parameter list, which is rarely if ever desirable
6281 (it's impossible to call such a function with type-
6282 correct arguments). An anonymous union parm type is
6283 meaningful as a GNU extension, so don't warn for that. */
6284 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
6287 /* The %s will be one of 'struct', 'union', or 'enum'. */
6288 warning (0, "%<%s %E%> declared inside parameter list",
6291 /* The %s will be one of 'struct', 'union', or 'enum'. */
6292 warning (0, "anonymous %s declared inside parameter list",
6295 if (!explained_incomplete_types)
6297 warning (0, "its scope is only this definition or declaration,"
6298 " which is probably not what you want");
6299 explained_incomplete_types = true;
6303 tags = tree_cons (b->id, decl, tags);
6309 /* CONST_DECLs appear here when we have an embedded enum,
6310 and TYPE_DECLs appear here when we have an embedded struct
6311 or union. No warnings for this - we already warned about the
6312 type itself. FUNCTION_DECLs appear when there is an implicit
6313 function declaration in the parameter list. */
6315 /* When we reinsert this decl in the function body, we need
6316 to reconstruct whether it was marked as nested. */
6317 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
6320 TREE_CHAIN (decl) = others;
6325 /* error_mark_node appears here when we have an undeclared
6326 variable. Just throw it away. */
6329 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6330 I_SYMBOL_BINDING (b->id) = b->shadowed;
6334 /* Other things that might be encountered. */
6341 b = free_binding_and_advance (b);
6344 arg_info->parms = parms;
6345 arg_info->tags = tags;
6346 arg_info->types = types;
6347 arg_info->others = others;
6348 arg_info->pending_sizes = get_pending_sizes ();
6352 /* Get the struct, enum or union (CODE says which) with tag NAME.
6353 Define the tag as a forward-reference with location LOC if it is
6354 not defined. Return a c_typespec structure for the type
6358 parser_xref_tag (location_t loc, enum tree_code code, tree name)
6360 struct c_typespec ret;
6364 ret.expr = NULL_TREE;
6365 ret.expr_const_operands = true;
6367 /* If a cross reference is requested, look up the type
6368 already defined for this tag and return it. */
6370 ref = lookup_tag (code, name, 0, &refloc);
6371 /* If this is the right type of tag, return what we found.
6372 (This reference will be shadowed by shadow_tag later if appropriate.)
6373 If this is the wrong type of tag, do not return it. If it was the
6374 wrong type in the same scope, we will have had an error
6375 message already; if in a different scope and declaring
6376 a name, pending_xref_error will give an error message; but if in a
6377 different scope and not declaring a name, this tag should
6378 shadow the previous declaration of a different type of tag, and
6379 this would not work properly if we return the reference found.
6380 (For example, with "struct foo" in an outer scope, "union foo;"
6381 must shadow that tag with a new one of union type.) */
6382 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
6383 if (ref && TREE_CODE (ref) == code)
6385 if (C_TYPE_DEFINED_IN_STRUCT (ref)
6386 && loc != UNKNOWN_LOCATION
6392 warning_at (loc, OPT_Wc___compat,
6393 ("enum type defined in struct or union "
6394 "is not visible in C++"));
6395 inform (refloc, "enum type defined here");
6398 warning_at (loc, OPT_Wc___compat,
6399 ("struct defined in struct or union "
6400 "is not visible in C++"));
6401 inform (refloc, "struct defined here");
6404 warning_at (loc, OPT_Wc___compat,
6405 ("union defined in struct or union "
6406 "is not visible in C++"));
6407 inform (refloc, "union defined here");
6418 /* If no such tag is yet defined, create a forward-reference node
6419 and record it as the "definition".
6420 When a real declaration of this type is found,
6421 the forward-reference will be altered into a real type. */
6423 ref = make_node (code);
6424 if (code == ENUMERAL_TYPE)
6426 /* Give the type a default layout like unsigned int
6427 to avoid crashing if it does not get defined. */
6428 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
6429 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
6430 TYPE_USER_ALIGN (ref) = 0;
6431 TYPE_UNSIGNED (ref) = 1;
6432 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
6433 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
6434 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
6437 pushtag (loc, name, ref);
6443 /* Get the struct, enum or union (CODE says which) with tag NAME.
6444 Define the tag as a forward-reference if it is not defined.
6445 Return a tree for the type. */
6448 xref_tag (enum tree_code code, tree name)
6450 return parser_xref_tag (input_location, code, name).spec;
6453 /* Make sure that the tag NAME is defined *in the current scope*
6454 at least as a forward reference.
6455 LOC is the location of the struct's definition.
6456 CODE says which kind of tag NAME ought to be.
6458 This stores the current value of the file static STRUCT_PARSE_INFO
6459 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
6460 new c_struct_parse_info structure. The old value of
6461 STRUCT_PARSE_INFO is restored in finish_struct. */
6464 start_struct (location_t loc, enum tree_code code, tree name,
6465 struct c_struct_parse_info **enclosing_struct_parse_info)
6467 /* If there is already a tag defined at this scope
6468 (as a forward reference), just return it. */
6470 tree ref = NULL_TREE;
6471 location_t refloc = UNKNOWN_LOCATION;
6473 if (name != NULL_TREE)
6474 ref = lookup_tag (code, name, 1, &refloc);
6475 if (ref && TREE_CODE (ref) == code)
6477 if (TYPE_SIZE (ref))
6479 if (code == UNION_TYPE)
6480 error_at (loc, "redefinition of %<union %E%>", name);
6482 error_at (loc, "redefinition of %<struct %E%>", name);
6483 if (refloc != UNKNOWN_LOCATION)
6484 inform (refloc, "originally defined here");
6485 /* Don't create structures using a name already in use. */
6488 else if (C_TYPE_BEING_DEFINED (ref))
6490 if (code == UNION_TYPE)
6491 error_at (loc, "nested redefinition of %<union %E%>", name);
6493 error_at (loc, "nested redefinition of %<struct %E%>", name);
6494 /* Don't bother to report "originally defined here" for a
6495 nested redefinition; the original definition should be
6497 /* Don't create structures that contain themselves. */
6502 /* Otherwise create a forward-reference just so the tag is in scope. */
6504 if (ref == NULL_TREE || TREE_CODE (ref) != code)
6506 ref = make_node (code);
6507 pushtag (loc, name, ref);
6510 C_TYPE_BEING_DEFINED (ref) = 1;
6511 TYPE_PACKED (ref) = flag_pack_struct;
6513 *enclosing_struct_parse_info = struct_parse_info;
6514 struct_parse_info = XNEW (struct c_struct_parse_info);
6515 struct_parse_info->struct_types = VEC_alloc (tree, heap, 0);
6516 struct_parse_info->fields = VEC_alloc (c_binding_ptr, heap, 0);
6517 struct_parse_info->typedefs_seen = VEC_alloc (tree, heap, 0);
6519 /* FIXME: This will issue a warning for a use of a type defined
6520 within a statement expr used within sizeof, et. al. This is not
6521 terribly serious as C++ doesn't permit statement exprs within
6523 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
6524 warning_at (loc, OPT_Wc___compat,
6525 "defining type in %qs expression is invalid in C++",
6528 : (in_typeof ? "typeof" : "alignof")));
6533 /* Process the specs, declarator and width (NULL if omitted)
6534 of a structure component, returning a FIELD_DECL node.
6535 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
6536 DECL_ATTRS is as for grokdeclarator.
6538 LOC is the location of the structure component.
6540 This is done during the parsing of the struct declaration.
6541 The FIELD_DECL nodes are chained together and the lot of them
6542 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
6545 grokfield (location_t loc,
6546 struct c_declarator *declarator, struct c_declspecs *declspecs,
6547 tree width, tree *decl_attrs)
6551 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
6552 && width == NULL_TREE)
6554 /* This is an unnamed decl.
6556 If we have something of the form "union { list } ;" then this
6557 is the anonymous union extension. Similarly for struct.
6559 If this is something of the form "struct foo;", then
6560 If MS extensions are enabled, this is handled as an
6562 Otherwise this is a forward declaration of a structure tag.
6564 If this is something of the form "foo;" and foo is a TYPE_DECL, then
6565 If foo names a structure or union without a tag, then this
6566 is an anonymous struct (this is permitted by C1X).
6567 If MS extensions are enabled and foo names a structure, then
6568 again this is an anonymous struct.
6569 Otherwise this is an error.
6571 Oh what a horrid tangled web we weave. I wonder if MS consciously
6572 took this from Plan 9 or if it was an accident of implementation
6573 that took root before someone noticed the bug... */
6575 tree type = declspecs->type;
6576 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
6577 || TREE_CODE (type) == UNION_TYPE);
6582 if (flag_ms_extensions)
6584 else if (TYPE_NAME (TYPE_MAIN_VARIANT (type)) == NULL)
6591 pedwarn (loc, 0, "declaration does not declare anything");
6597 pedwarn (loc, OPT_pedantic,
6598 "ISO C99 doesn%'t support unnamed structs/unions");
6600 pedwarn (loc, OPT_pedantic,
6601 "ISO C90 doesn%'t support unnamed structs/unions");
6605 value = grokdeclarator (declarator, declspecs, FIELD, false,
6606 width ? &width : NULL, decl_attrs, NULL, NULL,
6609 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6610 DECL_INITIAL (value) = width;
6612 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
6614 /* If we currently have a binding for this field, set the
6615 in_struct field in the binding, so that we warn about lookups
6617 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
6620 /* If the in_struct field is not yet set, push it on a list
6621 to be cleared when this struct is finished. */
6624 VEC_safe_push (c_binding_ptr, heap,
6625 struct_parse_info->fields, b);
6634 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
6635 to HTAB, giving errors for any duplicates. */
6638 detect_field_duplicates_hash (tree fieldlist, htab_t htab)
6643 for (x = fieldlist; x ; x = TREE_CHAIN (x))
6644 if ((y = DECL_NAME (x)) != 0)
6646 slot = htab_find_slot (htab, y, INSERT);
6649 error ("duplicate member %q+D", x);
6650 DECL_NAME (x) = NULL_TREE;
6654 else if (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6655 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
6656 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
6659 /* Generate an error for any duplicate field names in FIELDLIST. Munge
6660 the list such that this does not present a problem later. */
6663 detect_field_duplicates (tree fieldlist)
6668 /* First, see if there are more than "a few" fields.
6669 This is trivially true if there are zero or one fields. */
6672 x = TREE_CHAIN (fieldlist);
6677 if (DECL_NAME (x) == NULL_TREE
6678 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6679 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
6682 } while (timeout > 0 && x);
6684 /* If there were "few" fields and no anonymous structures or unions,
6685 avoid the overhead of allocating a hash table. Instead just do
6686 the nested traversal thing. */
6689 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
6692 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
6693 if (DECL_NAME (y) == DECL_NAME (x))
6695 error ("duplicate member %q+D", x);
6696 DECL_NAME (x) = NULL_TREE;
6702 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
6704 detect_field_duplicates_hash (fieldlist, htab);
6709 /* Finish up struct info used by -Wc++-compat. */
6712 warn_cxx_compat_finish_struct (tree fieldlist)
6716 struct c_binding *b;
6718 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
6719 the current struct. We do this now at the end of the struct
6720 because the flag is used to issue visibility warnings, and we
6721 only want to issue those warnings if the type is referenced
6722 outside of the struct declaration. */
6723 for (ix = 0; VEC_iterate (tree, struct_parse_info->struct_types, ix, x); ++ix)
6724 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
6726 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
6727 typedefs used when declaring fields in this struct. If the name
6728 of any of the fields is also a typedef name then the struct would
6729 not parse in C++, because the C++ lookup rules say that the
6730 typedef name would be looked up in the context of the struct, and
6731 would thus be the field rather than the typedef. */
6732 if (!VEC_empty (tree, struct_parse_info->typedefs_seen)
6733 && fieldlist != NULL_TREE)
6735 /* Use a pointer_set using the name of the typedef. We can use
6736 a pointer_set because identifiers are interned. */
6737 struct pointer_set_t *tset = pointer_set_create ();
6740 VEC_iterate (tree, struct_parse_info->typedefs_seen, ix, x);
6742 pointer_set_insert (tset, DECL_NAME (x));
6744 for (x = fieldlist; x != NULL_TREE; x = TREE_CHAIN (x))
6746 if (pointer_set_contains (tset, DECL_NAME (x)))
6748 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
6749 ("using %qD as both field and typedef name is "
6752 /* FIXME: It would be nice to report the location where
6753 the typedef name is used. */
6757 pointer_set_destroy (tset);
6760 /* For each field which has a binding and which was not defined in
6761 an enclosing struct, clear the in_struct field. */
6763 VEC_iterate (c_binding_ptr, struct_parse_info->fields, ix, b);
6768 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
6769 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
6770 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
6771 ATTRIBUTES are attributes to be applied to the structure.
6773 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
6774 the struct was started. */
6777 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
6778 struct c_struct_parse_info *enclosing_struct_parse_info)
6781 bool toplevel = file_scope == current_scope;
6782 int saw_named_field;
6784 /* If this type was previously laid out as a forward reference,
6785 make sure we lay it out again. */
6789 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6793 for (x = fieldlist; x; x = TREE_CHAIN (x))
6795 if (DECL_NAME (x) != 0)
6798 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6799 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
6805 if (TREE_CODE (t) == UNION_TYPE)
6808 pedwarn (loc, OPT_pedantic, "union has no named members");
6810 pedwarn (loc, OPT_pedantic, "union has no members");
6815 pedwarn (loc, OPT_pedantic, "struct has no named members");
6817 pedwarn (loc, OPT_pedantic, "struct has no members");
6822 /* Install struct as DECL_CONTEXT of each field decl.
6823 Also process specified field sizes, found in the DECL_INITIAL,
6824 storing 0 there after the type has been changed to precision equal
6825 to its width, rather than the precision of the specified standard
6826 type. (Correct layout requires the original type to have been preserved
6829 saw_named_field = 0;
6830 for (x = fieldlist; x; x = TREE_CHAIN (x))
6832 if (TREE_TYPE (x) == error_mark_node)
6835 DECL_CONTEXT (x) = t;
6837 /* If any field is const, the structure type is pseudo-const. */
6838 if (TREE_READONLY (x))
6839 C_TYPE_FIELDS_READONLY (t) = 1;
6842 /* A field that is pseudo-const makes the structure likewise. */
6843 tree t1 = TREE_TYPE (x);
6844 while (TREE_CODE (t1) == ARRAY_TYPE)
6845 t1 = TREE_TYPE (t1);
6846 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
6847 && C_TYPE_FIELDS_READONLY (t1))
6848 C_TYPE_FIELDS_READONLY (t) = 1;
6851 /* Any field that is volatile means variables of this type must be
6852 treated in some ways as volatile. */
6853 if (TREE_THIS_VOLATILE (x))
6854 C_TYPE_FIELDS_VOLATILE (t) = 1;
6856 /* Any field of nominal variable size implies structure is too. */
6857 if (C_DECL_VARIABLE_SIZE (x))
6858 C_TYPE_VARIABLE_SIZE (t) = 1;
6860 if (DECL_INITIAL (x))
6862 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
6863 DECL_SIZE (x) = bitsize_int (width);
6864 DECL_BIT_FIELD (x) = 1;
6865 SET_DECL_C_BIT_FIELD (x);
6869 && (DECL_BIT_FIELD (x)
6870 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
6871 DECL_PACKED (x) = 1;
6873 /* Detect flexible array member in an invalid context. */
6874 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6875 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
6876 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
6877 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
6879 if (TREE_CODE (t) == UNION_TYPE)
6881 error_at (DECL_SOURCE_LOCATION (x),
6882 "flexible array member in union");
6883 TREE_TYPE (x) = error_mark_node;
6885 else if (TREE_CHAIN (x) != NULL_TREE)
6887 error_at (DECL_SOURCE_LOCATION (x),
6888 "flexible array member not at end of struct");
6889 TREE_TYPE (x) = error_mark_node;
6891 else if (!saw_named_field)
6893 error_at (DECL_SOURCE_LOCATION (x),
6894 "flexible array member in otherwise empty struct");
6895 TREE_TYPE (x) = error_mark_node;
6899 if (pedantic && TREE_CODE (t) == RECORD_TYPE
6900 && flexible_array_type_p (TREE_TYPE (x)))
6901 pedwarn (DECL_SOURCE_LOCATION (x), OPT_pedantic,
6902 "invalid use of structure with flexible array member");
6905 || TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6906 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
6907 saw_named_field = 1;
6910 detect_field_duplicates (fieldlist);
6912 /* Now we have the nearly final fieldlist. Record it,
6913 then lay out the structure or union (including the fields). */
6915 TYPE_FIELDS (t) = fieldlist;
6919 /* Give bit-fields their proper types. */
6921 tree *fieldlistp = &fieldlist;
6923 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
6924 && TREE_TYPE (*fieldlistp) != error_mark_node)
6926 unsigned HOST_WIDE_INT width
6927 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
6928 tree type = TREE_TYPE (*fieldlistp);
6929 if (width != TYPE_PRECISION (type))
6931 TREE_TYPE (*fieldlistp)
6932 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
6933 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
6935 DECL_INITIAL (*fieldlistp) = 0;
6938 fieldlistp = &TREE_CHAIN (*fieldlistp);
6941 /* Now we have the truly final field list.
6942 Store it in this type and in the variants. */
6944 TYPE_FIELDS (t) = fieldlist;
6946 /* If there are lots of fields, sort so we can look through them fast.
6947 We arbitrarily consider 16 or more elts to be "a lot". */
6952 for (x = fieldlist; x; x = TREE_CHAIN (x))
6954 if (len > 15 || DECL_NAME (x) == NULL)
6962 struct lang_type *space;
6963 struct sorted_fields_type *space2;
6965 len += list_length (x);
6967 /* Use the same allocation policy here that make_node uses, to
6968 ensure that this lives as long as the rest of the struct decl.
6969 All decls in an inline function need to be saved. */
6971 space = GGC_CNEW (struct lang_type);
6972 space2 = GGC_NEWVAR (struct sorted_fields_type,
6973 sizeof (struct sorted_fields_type) + len * sizeof (tree));
6977 field_array = &space2->elts[0];
6978 for (x = fieldlist; x; x = TREE_CHAIN (x))
6980 field_array[len++] = x;
6982 /* If there is anonymous struct or union, break out of the loop. */
6983 if (DECL_NAME (x) == NULL)
6986 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
6989 TYPE_LANG_SPECIFIC (t) = space;
6990 TYPE_LANG_SPECIFIC (t)->s->len = len;
6991 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
6992 qsort (field_array, len, sizeof (tree), field_decl_cmp);
6997 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6999 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7000 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
7001 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
7002 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
7003 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
7006 /* If this was supposed to be a transparent union, but we can't
7007 make it one, warn and turn off the flag. */
7008 if (TREE_CODE (t) == UNION_TYPE
7009 && TYPE_TRANSPARENT_AGGR (t)
7010 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
7012 TYPE_TRANSPARENT_AGGR (t) = 0;
7013 warning_at (loc, 0, "union cannot be made transparent");
7016 /* If this structure or union completes the type of any previous
7017 variable declaration, lay it out and output its rtl. */
7018 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
7022 tree decl = TREE_VALUE (x);
7023 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7024 layout_array_type (TREE_TYPE (decl));
7025 if (TREE_CODE (decl) != TYPE_DECL)
7027 layout_decl (decl, 0);
7028 if (c_dialect_objc ())
7029 objc_check_decl (decl);
7030 rest_of_decl_compilation (decl, toplevel, 0);
7035 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
7037 /* Update type location to the one of the definition, instead of e.g.
7038 a forward declaration. */
7039 if (TYPE_STUB_DECL (t))
7040 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
7042 /* Finish debugging output for this type. */
7043 rest_of_type_compilation (t, toplevel);
7045 /* If we're inside a function proper, i.e. not file-scope and not still
7046 parsing parameters, then arrange for the size of a variable sized type
7048 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
7049 add_stmt (build_stmt (loc,
7050 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
7052 if (warn_cxx_compat)
7053 warn_cxx_compat_finish_struct (fieldlist);
7055 VEC_free (tree, heap, struct_parse_info->struct_types);
7056 VEC_free (c_binding_ptr, heap, struct_parse_info->fields);
7057 VEC_free (tree, heap, struct_parse_info->typedefs_seen);
7058 XDELETE (struct_parse_info);
7060 struct_parse_info = enclosing_struct_parse_info;
7062 /* If this struct is defined inside a struct, add it to
7065 && struct_parse_info != NULL
7066 && !in_sizeof && !in_typeof && !in_alignof)
7067 VEC_safe_push (tree, heap, struct_parse_info->struct_types, t);
7072 /* Lay out the type T, and its element type, and so on. */
7075 layout_array_type (tree t)
7077 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7078 layout_array_type (TREE_TYPE (t));
7082 /* Begin compiling the definition of an enumeration type.
7083 NAME is its name (or null if anonymous).
7084 LOC is the enum's location.
7085 Returns the type object, as yet incomplete.
7086 Also records info about it so that build_enumerator
7087 may be used to declare the individual values as they are read. */
7090 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
7092 tree enumtype = NULL_TREE;
7093 location_t enumloc = UNKNOWN_LOCATION;
7095 /* If this is the real definition for a previous forward reference,
7096 fill in the contents in the same object that used to be the
7097 forward reference. */
7099 if (name != NULL_TREE)
7100 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1, &enumloc);
7102 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
7104 enumtype = make_node (ENUMERAL_TYPE);
7105 pushtag (loc, name, enumtype);
7108 if (C_TYPE_BEING_DEFINED (enumtype))
7109 error_at (loc, "nested redefinition of %<enum %E%>", name);
7111 C_TYPE_BEING_DEFINED (enumtype) = 1;
7113 if (TYPE_VALUES (enumtype) != 0)
7115 /* This enum is a named one that has been declared already. */
7116 error_at (loc, "redeclaration of %<enum %E%>", name);
7117 if (enumloc != UNKNOWN_LOCATION)
7118 inform (enumloc, "originally defined here");
7120 /* Completely replace its old definition.
7121 The old enumerators remain defined, however. */
7122 TYPE_VALUES (enumtype) = 0;
7125 the_enum->enum_next_value = integer_zero_node;
7126 the_enum->enum_overflow = 0;
7128 if (flag_short_enums)
7129 TYPE_PACKED (enumtype) = 1;
7131 /* FIXME: This will issue a warning for a use of a type defined
7132 within sizeof in a statement expr. This is not terribly serious
7133 as C++ doesn't permit statement exprs within sizeof anyhow. */
7134 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7135 warning_at (loc, OPT_Wc___compat,
7136 "defining type in %qs expression is invalid in C++",
7139 : (in_typeof ? "typeof" : "alignof")));
7144 /* After processing and defining all the values of an enumeration type,
7145 install their decls in the enumeration type and finish it off.
7146 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
7147 and ATTRIBUTES are the specified attributes.
7148 Returns ENUMTYPE. */
7151 finish_enum (tree enumtype, tree values, tree attributes)
7154 tree minnode = 0, maxnode = 0;
7155 int precision, unsign;
7156 bool toplevel = (file_scope == current_scope);
7157 struct lang_type *lt;
7159 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7161 /* Calculate the maximum value of any enumerator in this type. */
7163 if (values == error_mark_node)
7164 minnode = maxnode = integer_zero_node;
7167 minnode = maxnode = TREE_VALUE (values);
7168 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
7170 tree value = TREE_VALUE (pair);
7171 if (tree_int_cst_lt (maxnode, value))
7173 if (tree_int_cst_lt (value, minnode))
7178 /* Construct the final type of this enumeration. It is the same
7179 as one of the integral types - the narrowest one that fits, except
7180 that normally we only go as narrow as int - and signed iff any of
7181 the values are negative. */
7182 unsign = (tree_int_cst_sgn (minnode) >= 0);
7183 precision = MAX (tree_int_cst_min_precision (minnode, unsign),
7184 tree_int_cst_min_precision (maxnode, unsign));
7186 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
7188 tem = c_common_type_for_size (precision, unsign);
7191 warning (0, "enumeration values exceed range of largest integer");
7192 tem = long_long_integer_type_node;
7196 tem = unsign ? unsigned_type_node : integer_type_node;
7198 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
7199 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
7200 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
7201 TYPE_SIZE (enumtype) = 0;
7203 /* If the precision of the type was specific with an attribute and it
7204 was too small, give an error. Otherwise, use it. */
7205 if (TYPE_PRECISION (enumtype))
7207 if (precision > TYPE_PRECISION (enumtype))
7208 error ("specified mode too small for enumeral values");
7211 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
7213 layout_type (enumtype);
7215 if (values != error_mark_node)
7217 /* Change the type of the enumerators to be the enum type. We
7218 need to do this irrespective of the size of the enum, for
7219 proper type checking. Replace the DECL_INITIALs of the
7220 enumerators, and the value slots of the list, with copies
7221 that have the enum type; they cannot be modified in place
7222 because they may be shared (e.g. integer_zero_node) Finally,
7223 change the purpose slots to point to the names of the decls. */
7224 for (pair = values; pair; pair = TREE_CHAIN (pair))
7226 tree enu = TREE_PURPOSE (pair);
7227 tree ini = DECL_INITIAL (enu);
7229 TREE_TYPE (enu) = enumtype;
7231 /* The ISO C Standard mandates enumerators to have type int,
7232 even though the underlying type of an enum type is
7233 unspecified. However, GCC allows enumerators of any
7234 integer type as an extensions. build_enumerator()
7235 converts any enumerators that fit in an int to type int,
7236 to avoid promotions to unsigned types when comparing
7237 integers with enumerators that fit in the int range.
7238 When -pedantic is given, build_enumerator() would have
7239 already warned about those that don't fit. Here we
7240 convert the rest to the enumerator type. */
7241 if (TREE_TYPE (ini) != integer_type_node)
7242 ini = convert (enumtype, ini);
7244 DECL_INITIAL (enu) = ini;
7245 TREE_PURPOSE (pair) = DECL_NAME (enu);
7246 TREE_VALUE (pair) = ini;
7249 TYPE_VALUES (enumtype) = values;
7252 /* Record the min/max values so that we can warn about bit-field
7253 enumerations that are too small for the values. */
7254 lt = GGC_CNEW (struct lang_type);
7255 lt->enum_min = minnode;
7256 lt->enum_max = maxnode;
7257 TYPE_LANG_SPECIFIC (enumtype) = lt;
7259 /* Fix up all variant types of this enum type. */
7260 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
7262 if (tem == enumtype)
7264 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
7265 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
7266 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
7267 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
7268 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
7269 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
7270 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
7271 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
7272 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
7273 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
7274 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
7277 /* Finish debugging output for this type. */
7278 rest_of_type_compilation (enumtype, toplevel);
7280 /* If this enum is defined inside a struct, add it to
7283 && struct_parse_info != NULL
7284 && !in_sizeof && !in_typeof && !in_alignof)
7285 VEC_safe_push (tree, heap, struct_parse_info->struct_types, enumtype);
7290 /* Build and install a CONST_DECL for one value of the
7291 current enumeration type (one that was begun with start_enum).
7292 LOC is the location of the enumerator.
7293 Return a tree-list containing the CONST_DECL and its value.
7294 Assignment of sequential values by default is handled here. */
7297 build_enumerator (location_t loc,
7298 struct c_enum_contents *the_enum, tree name, tree value)
7302 /* Validate and default VALUE. */
7306 /* Don't issue more errors for error_mark_node (i.e. an
7307 undeclared identifier) - just ignore the value expression. */
7308 if (value == error_mark_node)
7310 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
7312 error_at (loc, "enumerator value for %qE is not an integer constant",
7318 if (TREE_CODE (value) != INTEGER_CST)
7320 value = c_fully_fold (value, false, NULL);
7321 if (TREE_CODE (value) == INTEGER_CST)
7322 pedwarn (loc, OPT_pedantic,
7323 "enumerator value for %qE is not an integer "
7324 "constant expression", name);
7326 if (TREE_CODE (value) != INTEGER_CST)
7328 error ("enumerator value for %qE is not an integer constant",
7334 value = default_conversion (value);
7335 constant_expression_warning (value);
7340 /* Default based on previous value. */
7341 /* It should no longer be possible to have NON_LVALUE_EXPR
7345 value = the_enum->enum_next_value;
7346 if (the_enum->enum_overflow)
7347 error_at (loc, "overflow in enumeration values");
7349 /* Even though the underlying type of an enum is unspecified, the
7350 type of enumeration constants is explicitly defined as int
7351 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
7353 else if (!int_fits_type_p (value, integer_type_node))
7354 pedwarn (loc, OPT_pedantic,
7355 "ISO C restricts enumerator values to range of %<int%>");
7357 /* The ISO C Standard mandates enumerators to have type int, even
7358 though the underlying type of an enum type is unspecified.
7359 However, GCC allows enumerators of any integer type as an
7360 extensions. Here we convert any enumerators that fit in an int
7361 to type int, to avoid promotions to unsigned types when comparing
7362 integers with enumerators that fit in the int range. When
7363 -pedantic is given, we would have already warned about those that
7364 don't fit. We have to do this here rather than in finish_enum
7365 because this value may be used to define more enumerators. */
7366 if (int_fits_type_p (value, integer_type_node))
7367 value = convert (integer_type_node, value);
7369 /* Set basis for default for next value. */
7370 the_enum->enum_next_value
7372 (EXPR_HAS_LOCATION (value) ? EXPR_LOCATION (value) : input_location,
7373 PLUS_EXPR, value, integer_one_node, 0);
7374 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
7376 /* Now create a declaration for the enum value name. */
7378 type = TREE_TYPE (value);
7379 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
7380 TYPE_PRECISION (integer_type_node)),
7381 (TYPE_PRECISION (type)
7382 >= TYPE_PRECISION (integer_type_node)
7383 && TYPE_UNSIGNED (type)));
7385 decl = build_decl (loc, CONST_DECL, name, type);
7386 DECL_INITIAL (decl) = convert (type, value);
7389 return tree_cons (decl, value, NULL_TREE);
7393 /* Create the FUNCTION_DECL for a function definition.
7394 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
7395 the declaration; they describe the function's name and the type it returns,
7396 but twisted together in a fashion that parallels the syntax of C.
7398 This function creates a binding context for the function body
7399 as well as setting up the FUNCTION_DECL in current_function_decl.
7401 Returns 1 on success. If the DECLARATOR is not suitable for a function
7402 (it defines a datum instead), we return 0, which tells
7403 yyparse to report a parse error. */
7406 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
7409 tree decl1, old_decl;
7410 tree restype, resdecl;
7413 current_function_returns_value = 0; /* Assume, until we see it does. */
7414 current_function_returns_null = 0;
7415 current_function_returns_abnormally = 0;
7416 warn_about_return_type = 0;
7417 c_switch_stack = NULL;
7419 /* Indicate no valid break/continue context by setting these variables
7420 to some non-null, non-label value. We'll notice and emit the proper
7421 error message in c_finish_bc_stmt. */
7422 c_break_label = c_cont_label = size_zero_node;
7424 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
7425 &attributes, NULL, NULL, DEPRECATED_NORMAL);
7427 /* If the declarator is not suitable for a function definition,
7428 cause a syntax error. */
7432 loc = DECL_SOURCE_LOCATION (decl1);
7434 decl_attributes (&decl1, attributes, 0);
7436 if (DECL_DECLARED_INLINE_P (decl1)
7437 && DECL_UNINLINABLE (decl1)
7438 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
7439 warning_at (loc, OPT_Wattributes,
7440 "inline function %qD given attribute noinline",
7443 /* Handle gnu_inline attribute. */
7444 if (declspecs->inline_p
7445 && !flag_gnu89_inline
7446 && TREE_CODE (decl1) == FUNCTION_DECL
7447 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
7448 || current_function_decl))
7450 if (declspecs->storage_class != csc_static)
7451 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
7454 announce_function (decl1);
7456 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
7458 error_at (loc, "return type is an incomplete type");
7459 /* Make it return void instead. */
7461 = build_function_type (void_type_node,
7462 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
7465 if (warn_about_return_type)
7466 pedwarn_c99 (loc, flag_isoc99 ? 0
7467 : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
7468 "return type defaults to %<int%>");
7470 /* Make the init_value nonzero so pushdecl knows this is not tentative.
7471 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
7472 DECL_INITIAL (decl1) = error_mark_node;
7474 /* A nested function is not global. */
7475 if (current_function_decl != 0)
7476 TREE_PUBLIC (decl1) = 0;
7478 /* If this definition isn't a prototype and we had a prototype declaration
7479 before, copy the arg type info from that prototype. */
7480 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
7481 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
7483 current_function_prototype_locus = UNKNOWN_LOCATION;
7484 current_function_prototype_built_in = false;
7485 current_function_prototype_arg_types = NULL_TREE;
7486 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
7488 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
7489 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7490 TREE_TYPE (TREE_TYPE (old_decl))))
7492 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
7494 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
7495 current_function_prototype_built_in
7496 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
7497 current_function_prototype_arg_types
7498 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
7500 if (TREE_PUBLIC (decl1))
7502 /* If there is an external prototype declaration of this
7503 function, record its location but do not copy information
7504 to this decl. This may be an invisible declaration
7505 (built-in or in a scope which has finished) or simply
7506 have more refined argument types than any declaration
7508 struct c_binding *b;
7509 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
7510 if (B_IN_SCOPE (b, external_scope))
7514 tree ext_decl, ext_type;
7516 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
7517 if (TREE_CODE (ext_type) == FUNCTION_TYPE
7518 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7519 TREE_TYPE (ext_type)))
7521 current_function_prototype_locus
7522 = DECL_SOURCE_LOCATION (ext_decl);
7523 current_function_prototype_built_in
7524 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
7525 current_function_prototype_arg_types
7526 = TYPE_ARG_TYPES (ext_type);
7532 /* Optionally warn of old-fashioned def with no previous prototype. */
7533 if (warn_strict_prototypes
7534 && old_decl != error_mark_node
7535 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
7536 && C_DECL_ISNT_PROTOTYPE (old_decl))
7537 warning_at (loc, OPT_Wstrict_prototypes,
7538 "function declaration isn%'t a prototype");
7539 /* Optionally warn of any global def with no previous prototype. */
7540 else if (warn_missing_prototypes
7541 && old_decl != error_mark_node
7542 && TREE_PUBLIC (decl1)
7543 && !MAIN_NAME_P (DECL_NAME (decl1))
7544 && C_DECL_ISNT_PROTOTYPE (old_decl))
7545 warning_at (loc, OPT_Wmissing_prototypes,
7546 "no previous prototype for %qD", decl1);
7547 /* Optionally warn of any def with no previous prototype
7548 if the function has already been used. */
7549 else if (warn_missing_prototypes
7551 && old_decl != error_mark_node
7552 && TREE_USED (old_decl)
7553 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
7554 warning_at (loc, OPT_Wmissing_prototypes,
7555 "%qD was used with no prototype before its definition", decl1);
7556 /* Optionally warn of any global def with no previous declaration. */
7557 else if (warn_missing_declarations
7558 && TREE_PUBLIC (decl1)
7560 && !MAIN_NAME_P (DECL_NAME (decl1)))
7561 warning_at (loc, OPT_Wmissing_declarations,
7562 "no previous declaration for %qD",
7564 /* Optionally warn of any def with no previous declaration
7565 if the function has already been used. */
7566 else if (warn_missing_declarations
7568 && old_decl != error_mark_node
7569 && TREE_USED (old_decl)
7570 && C_DECL_IMPLICIT (old_decl))
7571 warning_at (loc, OPT_Wmissing_declarations,
7572 "%qD was used with no declaration before its definition", decl1);
7574 /* This function exists in static storage.
7575 (This does not mean `static' in the C sense!) */
7576 TREE_STATIC (decl1) = 1;
7578 /* This is the earliest point at which we might know the assembler
7579 name of the function. Thus, if it's set before this, die horribly. */
7580 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
7582 /* If #pragma weak was used, mark the decl weak now. */
7583 if (current_scope == file_scope)
7584 maybe_apply_pragma_weak (decl1);
7586 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
7587 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
7589 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
7590 != integer_type_node)
7591 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
7593 check_main_parameter_types (decl1);
7595 if (!TREE_PUBLIC (decl1))
7596 pedwarn (loc, OPT_Wmain,
7597 "%qD is normally a non-static function", decl1);
7600 /* Record the decl so that the function name is defined.
7601 If we already have a decl for this name, and it is a FUNCTION_DECL,
7602 use the old decl. */
7604 current_function_decl = pushdecl (decl1);
7607 declare_parm_level ();
7609 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
7610 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
7611 DECL_ARTIFICIAL (resdecl) = 1;
7612 DECL_IGNORED_P (resdecl) = 1;
7613 DECL_RESULT (current_function_decl) = resdecl;
7615 start_fname_decls ();
7620 /* Subroutine of store_parm_decls which handles new-style function
7621 definitions (prototype format). The parms already have decls, so we
7622 need only record them as in effect and complain if any redundant
7623 old-style parm decls were written. */
7625 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
7629 if (current_scope->bindings)
7631 error_at (DECL_SOURCE_LOCATION (fndecl),
7632 "old-style parameter declarations in prototyped "
7633 "function definition");
7635 /* Get rid of the old-style declarations. */
7639 /* Don't issue this warning for nested functions, and don't issue this
7640 warning if we got here because ARG_INFO_TYPES was error_mark_node
7641 (this happens when a function definition has just an ellipsis in
7642 its parameter list). */
7643 else if (!in_system_header && !current_function_scope
7644 && arg_info->types != error_mark_node)
7645 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
7646 "traditional C rejects ISO C style function definitions");
7648 /* Now make all the parameter declarations visible in the function body.
7649 We can bypass most of the grunt work of pushdecl. */
7650 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
7652 DECL_CONTEXT (decl) = current_function_decl;
7653 if (DECL_NAME (decl))
7655 bind (DECL_NAME (decl), decl, current_scope,
7656 /*invisible=*/false, /*nested=*/false,
7658 if (!TREE_USED (decl))
7659 warn_if_shadowing (decl);
7662 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
7665 /* Record the parameter list in the function declaration. */
7666 DECL_ARGUMENTS (fndecl) = arg_info->parms;
7668 /* Now make all the ancillary declarations visible, likewise. */
7669 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
7671 DECL_CONTEXT (decl) = current_function_decl;
7672 if (DECL_NAME (decl))
7673 bind (DECL_NAME (decl), decl, current_scope,
7674 /*invisible=*/false,
7675 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
7679 /* And all the tag declarations. */
7680 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
7681 if (TREE_PURPOSE (decl))
7682 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
7683 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
7686 /* Subroutine of store_parm_decls which handles old-style function
7687 definitions (separate parameter list and declarations). */
7690 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
7692 struct c_binding *b;
7693 tree parm, decl, last;
7694 tree parmids = arg_info->parms;
7695 struct pointer_set_t *seen_args = pointer_set_create ();
7697 if (!in_system_header)
7698 warning_at (DECL_SOURCE_LOCATION (fndecl),
7699 OPT_Wold_style_definition, "old-style function definition");
7701 /* Match each formal parameter name with its declaration. Save each
7702 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
7703 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
7705 if (TREE_VALUE (parm) == 0)
7707 error_at (DECL_SOURCE_LOCATION (fndecl),
7708 "parameter name missing from parameter list");
7709 TREE_PURPOSE (parm) = 0;
7713 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
7714 if (b && B_IN_CURRENT_SCOPE (b))
7717 /* If we got something other than a PARM_DECL it is an error. */
7718 if (TREE_CODE (decl) != PARM_DECL)
7719 error_at (DECL_SOURCE_LOCATION (decl),
7720 "%qD declared as a non-parameter", decl);
7721 /* If the declaration is already marked, we have a duplicate
7722 name. Complain and ignore the duplicate. */
7723 else if (pointer_set_contains (seen_args, decl))
7725 error_at (DECL_SOURCE_LOCATION (decl),
7726 "multiple parameters named %qD", decl);
7727 TREE_PURPOSE (parm) = 0;
7730 /* If the declaration says "void", complain and turn it into
7732 else if (VOID_TYPE_P (TREE_TYPE (decl)))
7734 error_at (DECL_SOURCE_LOCATION (decl),
7735 "parameter %qD declared with void type", decl);
7736 TREE_TYPE (decl) = integer_type_node;
7737 DECL_ARG_TYPE (decl) = integer_type_node;
7738 layout_decl (decl, 0);
7740 warn_if_shadowing (decl);
7742 /* If no declaration found, default to int. */
7745 /* FIXME diagnostics: This should be the location of the argument,
7746 not the FNDECL. E.g., for an old-style declaration
7748 int f10(v) { blah; }
7750 We should use the location of the V, not the F10.
7751 Unfortunately, the V is an IDENTIFIER_NODE which has no
7752 location. In the future we need locations for c_arg_info
7755 See gcc.dg/Wshadow-3.c for an example of this problem. */
7756 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
7757 PARM_DECL, TREE_VALUE (parm), integer_type_node);
7758 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
7760 warn_if_shadowing (decl);
7763 pedwarn (DECL_SOURCE_LOCATION (decl),
7764 0, "type of %qD defaults to %<int%>", decl);
7766 warning_at (DECL_SOURCE_LOCATION (decl),
7767 OPT_Wmissing_parameter_type,
7768 "type of %qD defaults to %<int%>", decl);
7771 TREE_PURPOSE (parm) = decl;
7772 pointer_set_insert (seen_args, decl);
7775 /* Now examine the parms chain for incomplete declarations
7776 and declarations with no corresponding names. */
7778 for (b = current_scope->bindings; b; b = b->prev)
7781 if (TREE_CODE (parm) != PARM_DECL)
7784 if (TREE_TYPE (parm) != error_mark_node
7785 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
7787 error_at (DECL_SOURCE_LOCATION (parm),
7788 "parameter %qD has incomplete type", parm);
7789 TREE_TYPE (parm) = error_mark_node;
7792 if (!pointer_set_contains (seen_args, parm))
7794 error_at (DECL_SOURCE_LOCATION (parm),
7795 "declaration for parameter %qD but no such parameter",
7798 /* Pretend the parameter was not missing.
7799 This gets us to a standard state and minimizes
7800 further error messages. */
7801 parmids = chainon (parmids, tree_cons (parm, 0, 0));
7805 /* Chain the declarations together in the order of the list of
7806 names. Store that chain in the function decl, replacing the
7807 list of names. Update the current scope to match. */
7808 DECL_ARGUMENTS (fndecl) = 0;
7810 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
7811 if (TREE_PURPOSE (parm))
7813 if (parm && TREE_PURPOSE (parm))
7815 last = TREE_PURPOSE (parm);
7816 DECL_ARGUMENTS (fndecl) = last;
7818 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
7819 if (TREE_PURPOSE (parm))
7821 TREE_CHAIN (last) = TREE_PURPOSE (parm);
7822 last = TREE_PURPOSE (parm);
7824 TREE_CHAIN (last) = 0;
7827 pointer_set_destroy (seen_args);
7829 /* If there was a previous prototype,
7830 set the DECL_ARG_TYPE of each argument according to
7831 the type previously specified, and report any mismatches. */
7833 if (current_function_prototype_arg_types)
7836 for (parm = DECL_ARGUMENTS (fndecl),
7837 type = current_function_prototype_arg_types;
7838 parm || (type && TREE_VALUE (type) != error_mark_node
7839 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
7840 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
7842 if (parm == 0 || type == 0
7843 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
7845 if (current_function_prototype_built_in)
7846 warning_at (DECL_SOURCE_LOCATION (fndecl),
7847 0, "number of arguments doesn%'t match "
7848 "built-in prototype");
7851 /* FIXME diagnostics: This should be the location of
7852 FNDECL, but there is bug when a prototype is
7853 declared inside function context, but defined
7854 outside of it (e.g., gcc.dg/pr15698-2.c). In
7855 which case FNDECL gets the location of the
7856 prototype, not the definition. */
7857 error_at (input_location,
7858 "number of arguments doesn%'t match prototype");
7860 error_at (current_function_prototype_locus,
7861 "prototype declaration");
7865 /* Type for passing arg must be consistent with that
7866 declared for the arg. ISO C says we take the unqualified
7867 type for parameters declared with qualified type. */
7868 if (TREE_TYPE (parm) != error_mark_node
7869 && TREE_TYPE (type) != error_mark_node
7870 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
7871 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
7873 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
7874 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
7876 /* Adjust argument to match prototype. E.g. a previous
7877 `int foo(float);' prototype causes
7878 `int foo(x) float x; {...}' to be treated like
7879 `int foo(float x) {...}'. This is particularly
7880 useful for argument types like uid_t. */
7881 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
7883 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
7884 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
7885 && TYPE_PRECISION (TREE_TYPE (parm))
7886 < TYPE_PRECISION (integer_type_node))
7887 DECL_ARG_TYPE (parm) = integer_type_node;
7889 /* ??? Is it possible to get here with a
7890 built-in prototype or will it always have
7891 been diagnosed as conflicting with an
7892 old-style definition and discarded? */
7893 if (current_function_prototype_built_in)
7894 warning_at (DECL_SOURCE_LOCATION (parm),
7895 OPT_pedantic, "promoted argument %qD "
7896 "doesn%'t match built-in prototype", parm);
7899 pedwarn (DECL_SOURCE_LOCATION (parm),
7900 OPT_pedantic, "promoted argument %qD "
7901 "doesn%'t match prototype", parm);
7902 pedwarn (current_function_prototype_locus, OPT_pedantic,
7903 "prototype declaration");
7908 if (current_function_prototype_built_in)
7909 warning_at (DECL_SOURCE_LOCATION (parm),
7910 0, "argument %qD doesn%'t match "
7911 "built-in prototype", parm);
7914 error_at (DECL_SOURCE_LOCATION (parm),
7915 "argument %qD doesn%'t match prototype", parm);
7916 error_at (current_function_prototype_locus,
7917 "prototype declaration");
7922 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
7925 /* Otherwise, create a prototype that would match. */
7929 tree actual = 0, last = 0, type;
7931 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
7933 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
7935 TREE_CHAIN (last) = type;
7940 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
7942 TREE_CHAIN (last) = type;
7946 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
7947 of the type of this function, but we need to avoid having this
7948 affect the types of other similarly-typed functions, so we must
7949 first force the generation of an identical (but separate) type
7950 node for the relevant function type. The new node we create
7951 will be a variant of the main variant of the original function
7954 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
7956 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
7960 /* Store parameter declarations passed in ARG_INFO into the current
7961 function declaration. */
7964 store_parm_decls_from (struct c_arg_info *arg_info)
7966 current_function_arg_info = arg_info;
7967 store_parm_decls ();
7970 /* Store the parameter declarations into the current function declaration.
7971 This is called after parsing the parameter declarations, before
7972 digesting the body of the function.
7974 For an old-style definition, construct a prototype out of the old-style
7975 parameter declarations and inject it into the function's type. */
7978 store_parm_decls (void)
7980 tree fndecl = current_function_decl;
7983 /* The argument information block for FNDECL. */
7984 struct c_arg_info *arg_info = current_function_arg_info;
7985 current_function_arg_info = 0;
7987 /* True if this definition is written with a prototype. Note:
7988 despite C99 6.7.5.3p14, we can *not* treat an empty argument
7989 list in a function definition as equivalent to (void) -- an
7990 empty argument list specifies the function has no parameters,
7991 but only (void) sets up a prototype for future calls. */
7992 proto = arg_info->types != 0;
7995 store_parm_decls_newstyle (fndecl, arg_info);
7997 store_parm_decls_oldstyle (fndecl, arg_info);
7999 /* The next call to push_scope will be a function body. */
8001 next_is_function_body = true;
8003 /* Write a record describing this function definition to the prototypes
8004 file (if requested). */
8006 gen_aux_info_record (fndecl, 1, 0, proto);
8008 /* Initialize the RTL code for the function. */
8009 allocate_struct_function (fndecl, false);
8011 /* Begin the statement tree for this function. */
8012 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
8014 /* ??? Insert the contents of the pending sizes list into the function
8015 to be evaluated. The only reason left to have this is
8016 void foo(int n, int array[n++])
8017 because we throw away the array type in favor of a pointer type, and
8018 thus won't naturally see the SAVE_EXPR containing the increment. All
8019 other pending sizes would be handled by gimplify_parameters. */
8021 VEC(tree,gc) *pending_sizes = get_pending_sizes ();
8025 for (i = 0; VEC_iterate (tree, pending_sizes, i, t); i++)
8029 /* Even though we're inside a function body, we still don't want to
8030 call expand_expr to calculate the size of a variable-sized array.
8031 We haven't necessarily assigned RTL to all variables yet, so it's
8032 not safe to try to expand expressions involving them. */
8033 cfun->dont_save_pending_sizes_p = 1;
8037 /* Finish up a function declaration and compile that function
8038 all the way to assembler language output. The free the storage
8039 for the function definition.
8041 This is called after parsing the body of the function definition. */
8044 finish_function (void)
8046 tree fndecl = current_function_decl;
8048 if (TREE_CODE (fndecl) == FUNCTION_DECL
8049 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
8051 tree args = DECL_ARGUMENTS (fndecl);
8052 for (; args; args = TREE_CHAIN (args))
8054 tree type = TREE_TYPE (args);
8055 if (INTEGRAL_TYPE_P (type)
8056 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8057 DECL_ARG_TYPE (args) = integer_type_node;
8061 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
8062 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
8064 /* Must mark the RESULT_DECL as being in this function. */
8066 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
8067 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
8069 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
8070 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
8071 == integer_type_node && flag_isoc99)
8073 /* Hack. We don't want the middle-end to warn that this return
8074 is unreachable, so we mark its location as special. Using
8075 UNKNOWN_LOCATION has the problem that it gets clobbered in
8076 annotate_one_with_locus. A cleaner solution might be to
8077 ensure ! should_carry_locus_p (stmt), but that needs a flag.
8079 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
8082 /* Tie off the statement tree for this function. */
8083 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
8085 finish_fname_decls ();
8087 /* Complain if there's just no return statement. */
8088 if (warn_return_type
8089 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
8090 && !current_function_returns_value && !current_function_returns_null
8091 /* Don't complain if we are no-return. */
8092 && !current_function_returns_abnormally
8093 /* Don't complain if we are declared noreturn. */
8094 && !TREE_THIS_VOLATILE (fndecl)
8095 /* Don't warn for main(). */
8096 && !MAIN_NAME_P (DECL_NAME (fndecl))
8097 /* Or if they didn't actually specify a return type. */
8098 && !C_FUNCTION_IMPLICIT_INT (fndecl)
8099 /* Normally, with -Wreturn-type, flow will complain, but we might
8100 optimize out static functions. */
8101 && !TREE_PUBLIC (fndecl))
8103 warning (OPT_Wreturn_type,
8104 "no return statement in function returning non-void");
8105 TREE_NO_WARNING (fndecl) = 1;
8108 /* Complain about parameters that are only set, but never otherwise used. */
8109 if (warn_unused_but_set_parameter)
8113 for (decl = DECL_ARGUMENTS (fndecl);
8115 decl = TREE_CHAIN (decl))
8116 if (TREE_USED (decl)
8117 && TREE_CODE (decl) == PARM_DECL
8118 && !DECL_READ_P (decl)
8120 && !DECL_ARTIFICIAL (decl)
8121 && !TREE_NO_WARNING (decl))
8122 warning_at (DECL_SOURCE_LOCATION (decl),
8123 OPT_Wunused_but_set_parameter,
8124 "parameter %qD set but not used", decl);
8127 /* Store the end of the function, so that we get good line number
8128 info for the epilogue. */
8129 cfun->function_end_locus = input_location;
8131 /* Finalize the ELF visibility for the function. */
8132 c_determine_visibility (fndecl);
8134 /* For GNU C extern inline functions disregard inline limits. */
8135 if (DECL_EXTERNAL (fndecl)
8136 && DECL_DECLARED_INLINE_P (fndecl))
8137 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
8139 /* Genericize before inlining. Delay genericizing nested functions
8140 until their parent function is genericized. Since finalizing
8141 requires GENERIC, delay that as well. */
8143 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
8144 && !undef_nested_function)
8146 if (!decl_function_context (fndecl))
8148 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
8149 c_genericize (fndecl);
8151 /* ??? Objc emits functions after finalizing the compilation unit.
8152 This should be cleaned up later and this conditional removed. */
8153 if (cgraph_global_info_ready)
8155 cgraph_add_new_function (fndecl, false);
8158 cgraph_finalize_function (fndecl, false);
8162 /* Register this function with cgraph just far enough to get it
8163 added to our parent's nested function list. Handy, since the
8164 C front end doesn't have such a list. */
8165 (void) cgraph_node (fndecl);
8169 if (!decl_function_context (fndecl))
8170 undef_nested_function = false;
8172 /* We're leaving the context of this function, so zap cfun.
8173 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
8174 tree_rest_of_compilation. */
8176 current_function_decl = NULL;
8179 /* Check the declarations given in a for-loop for satisfying the C99
8180 constraints. If exactly one such decl is found, return it. LOC is
8181 the location of the opening parenthesis of the for loop. */
8184 check_for_loop_decls (location_t loc)
8186 struct c_binding *b;
8187 tree one_decl = NULL_TREE;
8192 static bool hint = true;
8193 /* If we get here, declarations have been used in a for loop without
8194 the C99 for loop scope. This doesn't make much sense, so don't
8196 error_at (loc, "%<for%> loop initial declarations "
8197 "are only allowed in C99 mode");
8201 "use option -std=c99 or -std=gnu99 to compile your code");
8206 /* C99 subclause 6.8.5 paragraph 3:
8208 [#3] The declaration part of a for statement shall only
8209 declare identifiers for objects having storage class auto or
8212 It isn't clear whether, in this sentence, "identifiers" binds to
8213 "shall only declare" or to "objects" - that is, whether all identifiers
8214 declared must be identifiers for objects, or whether the restriction
8215 only applies to those that are. (A question on this in comp.std.c
8216 in November 2000 received no answer.) We implement the strictest
8217 interpretation, to avoid creating an extension which later causes
8220 for (b = current_scope->bindings; b; b = b->prev)
8223 tree decl = b->decl;
8228 switch (TREE_CODE (decl))
8232 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
8233 if (TREE_STATIC (decl))
8235 "declaration of static variable %qD in %<for%> loop "
8236 "initial declaration", decl);
8237 else if (DECL_EXTERNAL (decl))
8239 "declaration of %<extern%> variable %qD in %<for%> loop "
8240 "initial declaration", decl);
8246 "%<struct %E%> declared in %<for%> loop initial "
8251 "%<union %E%> declared in %<for%> loop initial declaration",
8255 error_at (loc, "%<enum %E%> declared in %<for%> loop "
8256 "initial declaration", id);
8259 error_at (loc, "declaration of non-variable "
8260 "%qD in %<for%> loop initial declaration", decl);
8267 return n_decls == 1 ? one_decl : NULL_TREE;
8270 /* Save and reinitialize the variables
8271 used during compilation of a C function. */
8274 c_push_function_context (void)
8276 struct language_function *p;
8277 p = GGC_NEW (struct language_function);
8280 p->base.x_stmt_tree = c_stmt_tree;
8281 p->x_break_label = c_break_label;
8282 p->x_cont_label = c_cont_label;
8283 p->x_switch_stack = c_switch_stack;
8284 p->arg_info = current_function_arg_info;
8285 p->returns_value = current_function_returns_value;
8286 p->returns_null = current_function_returns_null;
8287 p->returns_abnormally = current_function_returns_abnormally;
8288 p->warn_about_return_type = warn_about_return_type;
8290 push_function_context ();
8293 /* Restore the variables used during compilation of a C function. */
8296 c_pop_function_context (void)
8298 struct language_function *p;
8300 pop_function_context ();
8302 cfun->language = NULL;
8304 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
8305 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
8307 /* Stop pointing to the local nodes about to be freed. */
8308 /* But DECL_INITIAL must remain nonzero so we know this
8309 was an actual function definition. */
8310 DECL_INITIAL (current_function_decl) = error_mark_node;
8311 DECL_ARGUMENTS (current_function_decl) = 0;
8314 c_stmt_tree = p->base.x_stmt_tree;
8315 c_break_label = p->x_break_label;
8316 c_cont_label = p->x_cont_label;
8317 c_switch_stack = p->x_switch_stack;
8318 current_function_arg_info = p->arg_info;
8319 current_function_returns_value = p->returns_value;
8320 current_function_returns_null = p->returns_null;
8321 current_function_returns_abnormally = p->returns_abnormally;
8322 warn_about_return_type = p->warn_about_return_type;
8325 /* The functions below are required for functionality of doing
8326 function at once processing in the C front end. Currently these
8327 functions are not called from anywhere in the C front end, but as
8328 these changes continue, that will change. */
8330 /* Returns the stmt_tree (if any) to which statements are currently
8331 being added. If there is no active statement-tree, NULL is
8335 current_stmt_tree (void)
8337 return &c_stmt_tree;
8340 /* Return the global value of T as a symbol. */
8343 identifier_global_value (tree t)
8345 struct c_binding *b;
8347 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
8348 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
8354 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
8355 otherwise the name is found in ridpointers from RID_INDEX. */
8358 record_builtin_type (enum rid rid_index, const char *name, tree type)
8362 id = ridpointers[(int) rid_index];
8364 id = get_identifier (name);
8365 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
8367 if (debug_hooks->type_decl)
8368 debug_hooks->type_decl (decl, false);
8371 /* Build the void_list_node (void_type_node having been created). */
8373 build_void_list_node (void)
8375 tree t = build_tree_list (NULL_TREE, void_type_node);
8379 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
8382 build_c_parm (struct c_declspecs *specs, tree attrs,
8383 struct c_declarator *declarator)
8385 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
8388 ret->declarator = declarator;
8392 /* Return a declarator with nested attributes. TARGET is the inner
8393 declarator to which these attributes apply. ATTRS are the
8396 struct c_declarator *
8397 build_attrs_declarator (tree attrs, struct c_declarator *target)
8399 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8400 ret->kind = cdk_attrs;
8401 ret->declarator = target;
8402 ret->u.attrs = attrs;
8406 /* Return a declarator for a function with arguments specified by ARGS
8407 and return type specified by TARGET. */
8409 struct c_declarator *
8410 build_function_declarator (struct c_arg_info *args,
8411 struct c_declarator *target)
8413 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8414 ret->kind = cdk_function;
8415 ret->declarator = target;
8416 ret->u.arg_info = args;
8420 /* Return a declarator for the identifier IDENT (which may be
8421 NULL_TREE for an abstract declarator). */
8423 struct c_declarator *
8424 build_id_declarator (tree ident)
8426 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8428 ret->declarator = 0;
8430 /* Default value - may get reset to a more precise location. */
8431 ret->id_loc = input_location;
8435 /* Return something to represent absolute declarators containing a *.
8436 TARGET is the absolute declarator that the * contains.
8437 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
8438 to apply to the pointer type. */
8440 struct c_declarator *
8441 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
8442 struct c_declarator *target)
8446 struct c_declarator *itarget = target;
8447 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8448 if (type_quals_attrs)
8450 attrs = type_quals_attrs->attrs;
8451 quals = quals_from_declspecs (type_quals_attrs);
8452 if (attrs != NULL_TREE)
8453 itarget = build_attrs_declarator (attrs, target);
8455 ret->kind = cdk_pointer;
8456 ret->declarator = itarget;
8457 ret->u.pointer_quals = quals;
8461 /* Return a pointer to a structure for an empty list of declaration
8464 struct c_declspecs *
8465 build_null_declspecs (void)
8467 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
8472 ret->typespec_word = cts_none;
8473 ret->storage_class = csc_none;
8474 ret->expr_const_operands = true;
8475 ret->declspecs_seen_p = false;
8476 ret->type_seen_p = false;
8477 ret->non_sc_seen_p = false;
8478 ret->typedef_p = false;
8479 ret->tag_defined_p = false;
8480 ret->explicit_signed_p = false;
8481 ret->deprecated_p = false;
8482 ret->default_int_p = false;
8483 ret->long_p = false;
8484 ret->long_long_p = false;
8485 ret->short_p = false;
8486 ret->signed_p = false;
8487 ret->unsigned_p = false;
8488 ret->complex_p = false;
8489 ret->inline_p = false;
8490 ret->thread_p = false;
8491 ret->const_p = false;
8492 ret->volatile_p = false;
8493 ret->restrict_p = false;
8494 ret->saturating_p = false;
8495 ret->address_space = ADDR_SPACE_GENERIC;
8499 /* Add the address space ADDRSPACE to the declaration specifiers
8500 SPECS, returning SPECS. */
8502 struct c_declspecs *
8503 declspecs_add_addrspace (struct c_declspecs *specs, addr_space_t as)
8505 specs->non_sc_seen_p = true;
8506 specs->declspecs_seen_p = true;
8508 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
8509 && specs->address_space != as)
8510 error ("incompatible address space qualifiers %qs and %qs",
8511 c_addr_space_name (as),
8512 c_addr_space_name (specs->address_space));
8514 specs->address_space = as;
8518 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
8521 struct c_declspecs *
8522 declspecs_add_qual (struct c_declspecs *specs, tree qual)
8526 specs->non_sc_seen_p = true;
8527 specs->declspecs_seen_p = true;
8528 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
8529 && C_IS_RESERVED_WORD (qual));
8530 i = C_RID_CODE (qual);
8534 dupe = specs->const_p;
8535 specs->const_p = true;
8538 dupe = specs->volatile_p;
8539 specs->volatile_p = true;
8542 dupe = specs->restrict_p;
8543 specs->restrict_p = true;
8548 if (dupe && !flag_isoc99)
8549 pedwarn (input_location, OPT_pedantic, "duplicate %qE", qual);
8553 /* Add the type specifier TYPE to the declaration specifiers SPECS,
8556 struct c_declspecs *
8557 declspecs_add_type (location_t loc, struct c_declspecs *specs,
8558 struct c_typespec spec)
8560 tree type = spec.spec;
8561 specs->non_sc_seen_p = true;
8562 specs->declspecs_seen_p = true;
8563 specs->type_seen_p = true;
8564 if (TREE_DEPRECATED (type))
8565 specs->deprecated_p = true;
8567 /* Handle type specifier keywords. */
8568 if (TREE_CODE (type) == IDENTIFIER_NODE
8569 && C_IS_RESERVED_WORD (type)
8570 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
8572 enum rid i = C_RID_CODE (type);
8575 error_at (loc, "two or more data types in declaration specifiers");
8578 if ((int) i <= (int) RID_LAST_MODIFIER)
8580 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
8585 if (specs->long_long_p)
8587 error_at (loc, "%<long long long%> is too long for GCC");
8592 if (specs->typespec_word == cts_double)
8595 ("both %<long long%> and %<double%> in "
8596 "declaration specifiers"));
8599 pedwarn_c90 (loc, OPT_Wlong_long,
8600 "ISO C90 does not support %<long long%>");
8601 specs->long_long_p = 1;
8606 ("both %<long%> and %<short%> in "
8607 "declaration specifiers"));
8608 else if (specs->typespec_word == cts_void)
8610 ("both %<long%> and %<void%> in "
8611 "declaration specifiers"));
8612 else if (specs->typespec_word == cts_bool)
8614 ("both %<long%> and %<_Bool%> in "
8615 "declaration specifiers"));
8616 else if (specs->typespec_word == cts_char)
8618 ("both %<long%> and %<char%> in "
8619 "declaration specifiers"));
8620 else if (specs->typespec_word == cts_float)
8622 ("both %<long%> and %<float%> in "
8623 "declaration specifiers"));
8624 else if (specs->typespec_word == cts_dfloat32)
8626 ("both %<long%> and %<_Decimal32%> in "
8627 "declaration specifiers"));
8628 else if (specs->typespec_word == cts_dfloat64)
8630 ("both %<long%> and %<_Decimal64%> in "
8631 "declaration specifiers"));
8632 else if (specs->typespec_word == cts_dfloat128)
8634 ("both %<long%> and %<_Decimal128%> in "
8635 "declaration specifiers"));
8637 specs->long_p = true;
8640 dupe = specs->short_p;
8643 ("both %<long%> and %<short%> in "
8644 "declaration specifiers"));
8645 else if (specs->typespec_word == cts_void)
8647 ("both %<short%> and %<void%> in "
8648 "declaration specifiers"));
8649 else if (specs->typespec_word == cts_bool)
8651 ("both %<short%> and %<_Bool%> in "
8652 "declaration specifiers"));
8653 else if (specs->typespec_word == cts_char)
8655 ("both %<short%> and %<char%> in "
8656 "declaration specifiers"));
8657 else if (specs->typespec_word == cts_float)
8659 ("both %<short%> and %<float%> in "
8660 "declaration specifiers"));
8661 else if (specs->typespec_word == cts_double)
8663 ("both %<short%> and %<double%> in "
8664 "declaration specifiers"));
8665 else if (specs->typespec_word == cts_dfloat32)
8667 ("both %<short%> and %<_Decimal32%> in "
8668 "declaration specifiers"));
8669 else if (specs->typespec_word == cts_dfloat64)
8671 ("both %<short%> and %<_Decimal64%> in "
8672 "declaration specifiers"));
8673 else if (specs->typespec_word == cts_dfloat128)
8675 ("both %<short%> and %<_Decimal128%> in "
8676 "declaration specifiers"));
8678 specs->short_p = true;
8681 dupe = specs->signed_p;
8682 if (specs->unsigned_p)
8684 ("both %<signed%> and %<unsigned%> in "
8685 "declaration specifiers"));
8686 else if (specs->typespec_word == cts_void)
8688 ("both %<signed%> and %<void%> in "
8689 "declaration specifiers"));
8690 else if (specs->typespec_word == cts_bool)
8692 ("both %<signed%> and %<_Bool%> in "
8693 "declaration specifiers"));
8694 else if (specs->typespec_word == cts_float)
8696 ("both %<signed%> and %<float%> in "
8697 "declaration specifiers"));
8698 else if (specs->typespec_word == cts_double)
8700 ("both %<signed%> and %<double%> in "
8701 "declaration specifiers"));
8702 else if (specs->typespec_word == cts_dfloat32)
8704 ("both %<signed%> and %<_Decimal32%> in "
8705 "declaration specifiers"));
8706 else if (specs->typespec_word == cts_dfloat64)
8708 ("both %<signed%> and %<_Decimal64%> in "
8709 "declaration specifiers"));
8710 else if (specs->typespec_word == cts_dfloat128)
8712 ("both %<signed%> and %<_Decimal128%> in "
8713 "declaration specifiers"));
8715 specs->signed_p = true;
8718 dupe = specs->unsigned_p;
8719 if (specs->signed_p)
8721 ("both %<signed%> and %<unsigned%> in "
8722 "declaration specifiers"));
8723 else if (specs->typespec_word == cts_void)
8725 ("both %<unsigned%> and %<void%> in "
8726 "declaration specifiers"));
8727 else if (specs->typespec_word == cts_bool)
8729 ("both %<unsigned%> and %<_Bool%> in "
8730 "declaration specifiers"));
8731 else if (specs->typespec_word == cts_float)
8733 ("both %<unsigned%> and %<float%> in "
8734 "declaration specifiers"));
8735 else if (specs->typespec_word == cts_double)
8737 ("both %<unsigned%> and %<double%> in "
8738 "declaration specifiers"));
8739 else if (specs->typespec_word == cts_dfloat32)
8741 ("both %<unsigned%> and %<_Decimal32%> in "
8742 "declaration specifiers"));
8743 else if (specs->typespec_word == cts_dfloat64)
8745 ("both %<unsigned%> and %<_Decimal64%> in "
8746 "declaration specifiers"));
8747 else if (specs->typespec_word == cts_dfloat128)
8749 ("both %<unsigned%> and %<_Decimal128%> in "
8750 "declaration specifiers"));
8752 specs->unsigned_p = true;
8755 dupe = specs->complex_p;
8756 if (!flag_isoc99 && !in_system_header)
8757 pedwarn (loc, OPT_pedantic,
8758 "ISO C90 does not support complex types");
8759 if (specs->typespec_word == cts_void)
8761 ("both %<complex%> and %<void%> in "
8762 "declaration specifiers"));
8763 else if (specs->typespec_word == cts_bool)
8765 ("both %<complex%> and %<_Bool%> in "
8766 "declaration specifiers"));
8767 else if (specs->typespec_word == cts_dfloat32)
8769 ("both %<complex%> and %<_Decimal32%> in "
8770 "declaration specifiers"));
8771 else if (specs->typespec_word == cts_dfloat64)
8773 ("both %<complex%> and %<_Decimal64%> in "
8774 "declaration specifiers"));
8775 else if (specs->typespec_word == cts_dfloat128)
8777 ("both %<complex%> and %<_Decimal128%> in "
8778 "declaration specifiers"));
8779 else if (specs->typespec_word == cts_fract)
8781 ("both %<complex%> and %<_Fract%> in "
8782 "declaration specifiers"));
8783 else if (specs->typespec_word == cts_accum)
8785 ("both %<complex%> and %<_Accum%> in "
8786 "declaration specifiers"));
8787 else if (specs->saturating_p)
8789 ("both %<complex%> and %<_Sat%> in "
8790 "declaration specifiers"));
8792 specs->complex_p = true;
8795 dupe = specs->saturating_p;
8796 pedwarn (loc, OPT_pedantic,
8797 "ISO C does not support saturating types");
8798 if (specs->typespec_word == cts_void)
8800 ("both %<_Sat%> and %<void%> in "
8801 "declaration specifiers"));
8802 else if (specs->typespec_word == cts_bool)
8804 ("both %<_Sat%> and %<_Bool%> in "
8805 "declaration specifiers"));
8806 else if (specs->typespec_word == cts_char)
8808 ("both %<_Sat%> and %<char%> in "
8809 "declaration specifiers"));
8810 else if (specs->typespec_word == cts_int)
8812 ("both %<_Sat%> and %<int%> in "
8813 "declaration specifiers"));
8814 else if (specs->typespec_word == cts_float)
8816 ("both %<_Sat%> and %<float%> in "
8817 "declaration specifiers"));
8818 else if (specs->typespec_word == cts_double)
8820 ("both %<_Sat%> and %<double%> in "
8821 "declaration specifiers"));
8822 else if (specs->typespec_word == cts_dfloat32)
8824 ("both %<_Sat%> and %<_Decimal32%> in "
8825 "declaration specifiers"));
8826 else if (specs->typespec_word == cts_dfloat64)
8828 ("both %<_Sat%> and %<_Decimal64%> in "
8829 "declaration specifiers"));
8830 else if (specs->typespec_word == cts_dfloat128)
8832 ("both %<_Sat%> and %<_Decimal128%> in "
8833 "declaration specifiers"));
8834 else if (specs->complex_p)
8836 ("both %<_Sat%> and %<complex%> in "
8837 "declaration specifiers"));
8839 specs->saturating_p = true;
8846 error_at (loc, "duplicate %qE", type);
8852 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
8853 "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
8854 if (specs->typespec_word != cts_none)
8857 "two or more data types in declaration specifiers");
8865 ("both %<long%> and %<void%> in "
8866 "declaration specifiers"));
8867 else if (specs->short_p)
8869 ("both %<short%> and %<void%> in "
8870 "declaration specifiers"));
8871 else if (specs->signed_p)
8873 ("both %<signed%> and %<void%> in "
8874 "declaration specifiers"));
8875 else if (specs->unsigned_p)
8877 ("both %<unsigned%> and %<void%> in "
8878 "declaration specifiers"));
8879 else if (specs->complex_p)
8881 ("both %<complex%> and %<void%> in "
8882 "declaration specifiers"));
8883 else if (specs->saturating_p)
8885 ("both %<_Sat%> and %<void%> in "
8886 "declaration specifiers"));
8888 specs->typespec_word = cts_void;
8893 ("both %<long%> and %<_Bool%> in "
8894 "declaration specifiers"));
8895 else if (specs->short_p)
8897 ("both %<short%> and %<_Bool%> in "
8898 "declaration specifiers"));
8899 else if (specs->signed_p)
8901 ("both %<signed%> and %<_Bool%> in "
8902 "declaration specifiers"));
8903 else if (specs->unsigned_p)
8905 ("both %<unsigned%> and %<_Bool%> in "
8906 "declaration specifiers"));
8907 else if (specs->complex_p)
8909 ("both %<complex%> and %<_Bool%> in "
8910 "declaration specifiers"));
8911 else if (specs->saturating_p)
8913 ("both %<_Sat%> and %<_Bool%> in "
8914 "declaration specifiers"));
8916 specs->typespec_word = cts_bool;
8921 ("both %<long%> and %<char%> in "
8922 "declaration specifiers"));
8923 else if (specs->short_p)
8925 ("both %<short%> and %<char%> in "
8926 "declaration specifiers"));
8927 else if (specs->saturating_p)
8929 ("both %<_Sat%> and %<char%> in "
8930 "declaration specifiers"));
8932 specs->typespec_word = cts_char;
8935 if (specs->saturating_p)
8937 ("both %<_Sat%> and %<int%> in "
8938 "declaration specifiers"));
8940 specs->typespec_word = cts_int;
8945 ("both %<long%> and %<float%> in "
8946 "declaration specifiers"));
8947 else if (specs->short_p)
8949 ("both %<short%> and %<float%> in "
8950 "declaration specifiers"));
8951 else if (specs->signed_p)
8953 ("both %<signed%> and %<float%> in "
8954 "declaration specifiers"));
8955 else if (specs->unsigned_p)
8957 ("both %<unsigned%> and %<float%> in "
8958 "declaration specifiers"));
8959 else if (specs->saturating_p)
8961 ("both %<_Sat%> and %<float%> in "
8962 "declaration specifiers"));
8964 specs->typespec_word = cts_float;
8967 if (specs->long_long_p)
8969 ("both %<long long%> and %<double%> in "
8970 "declaration specifiers"));
8971 else if (specs->short_p)
8973 ("both %<short%> and %<double%> in "
8974 "declaration specifiers"));
8975 else if (specs->signed_p)
8977 ("both %<signed%> and %<double%> in "
8978 "declaration specifiers"));
8979 else if (specs->unsigned_p)
8981 ("both %<unsigned%> and %<double%> in "
8982 "declaration specifiers"));
8983 else if (specs->saturating_p)
8985 ("both %<_Sat%> and %<double%> in "
8986 "declaration specifiers"));
8988 specs->typespec_word = cts_double;
8995 if (i == RID_DFLOAT32)
8997 else if (i == RID_DFLOAT64)
9000 str = "_Decimal128";
9001 if (specs->long_long_p)
9003 ("both %<long long%> and %<%s%> in "
9004 "declaration specifiers"),
9008 ("both %<long%> and %<%s%> in "
9009 "declaration specifiers"),
9011 else if (specs->short_p)
9013 ("both %<short%> and %<%s%> in "
9014 "declaration specifiers"),
9016 else if (specs->signed_p)
9018 ("both %<signed%> and %<%s%> in "
9019 "declaration specifiers"),
9021 else if (specs->unsigned_p)
9023 ("both %<unsigned%> and %<%s%> in "
9024 "declaration specifiers"),
9026 else if (specs->complex_p)
9028 ("both %<complex%> and %<%s%> in "
9029 "declaration specifiers"),
9031 else if (specs->saturating_p)
9033 ("both %<_Sat%> and %<%s%> in "
9034 "declaration specifiers"),
9036 else if (i == RID_DFLOAT32)
9037 specs->typespec_word = cts_dfloat32;
9038 else if (i == RID_DFLOAT64)
9039 specs->typespec_word = cts_dfloat64;
9041 specs->typespec_word = cts_dfloat128;
9043 if (!targetm.decimal_float_supported_p ())
9045 ("decimal floating point not supported "
9046 "for this target"));
9047 pedwarn (loc, OPT_pedantic,
9048 "ISO C does not support decimal floating point");
9058 if (specs->complex_p)
9060 ("both %<complex%> and %<%s%> in "
9061 "declaration specifiers"),
9063 else if (i == RID_FRACT)
9064 specs->typespec_word = cts_fract;
9066 specs->typespec_word = cts_accum;
9068 if (!targetm.fixed_point_supported_p ())
9070 "fixed-point types not supported for this target");
9071 pedwarn (loc, OPT_pedantic,
9072 "ISO C does not support fixed-point types");
9075 /* ObjC reserved word "id", handled below. */
9081 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
9082 form of ObjC type, cases such as "int" and "long" being handled
9083 above), a TYPE (struct, union, enum and typeof specifiers) or an
9084 ERROR_MARK. In none of these cases may there have previously
9085 been any type specifiers. */
9086 if (specs->type || specs->typespec_word != cts_none
9087 || specs->long_p || specs->short_p || specs->signed_p
9088 || specs->unsigned_p || specs->complex_p)
9089 error_at (loc, "two or more data types in declaration specifiers");
9090 else if (TREE_CODE (type) == TYPE_DECL)
9092 if (TREE_TYPE (type) == error_mark_node)
9093 ; /* Allow the type to default to int to avoid cascading errors. */
9096 specs->type = TREE_TYPE (type);
9097 specs->decl_attr = DECL_ATTRIBUTES (type);
9098 specs->typedef_p = true;
9099 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
9101 /* If this typedef name is defined in a struct, then a C++
9102 lookup would return a different value. */
9104 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
9105 warning_at (loc, OPT_Wc___compat,
9106 "C++ lookup of %qD would return a field, not a type",
9109 /* If we are parsing a struct, record that a struct field
9111 if (warn_cxx_compat && struct_parse_info != NULL)
9112 VEC_safe_push (tree, heap, struct_parse_info->typedefs_seen, type);
9115 else if (TREE_CODE (type) == IDENTIFIER_NODE)
9117 tree t = lookup_name (type);
9118 if (!t || TREE_CODE (t) != TYPE_DECL)
9119 error_at (loc, "%qE fails to be a typedef or built in type", type);
9120 else if (TREE_TYPE (t) == error_mark_node)
9123 specs->type = TREE_TYPE (t);
9125 else if (TREE_CODE (type) != ERROR_MARK)
9127 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
9128 specs->tag_defined_p = true;
9129 if (spec.kind == ctsk_typeof)
9131 specs->typedef_p = true;
9135 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
9136 specs->expr, spec.expr);
9138 specs->expr = spec.expr;
9139 specs->expr_const_operands &= spec.expr_const_operands;
9148 /* Add the storage class specifier or function specifier SCSPEC to the
9149 declaration specifiers SPECS, returning SPECS. */
9151 struct c_declspecs *
9152 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
9155 enum c_storage_class n = csc_none;
9157 specs->declspecs_seen_p = true;
9158 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
9159 && C_IS_RESERVED_WORD (scspec));
9160 i = C_RID_CODE (scspec);
9161 if (specs->non_sc_seen_p)
9162 warning (OPT_Wold_style_declaration,
9163 "%qE is not at beginning of declaration", scspec);
9167 /* C99 permits duplicate inline. Although of doubtful utility,
9168 it seems simplest to permit it in gnu89 mode as well, as
9169 there is also little utility in maintaining this as a
9170 difference between gnu89 and C99 inline. */
9172 specs->inline_p = true;
9175 dupe = specs->thread_p;
9176 if (specs->storage_class == csc_auto)
9177 error ("%<__thread%> used with %<auto%>");
9178 else if (specs->storage_class == csc_register)
9179 error ("%<__thread%> used with %<register%>");
9180 else if (specs->storage_class == csc_typedef)
9181 error ("%<__thread%> used with %<typedef%>");
9183 specs->thread_p = true;
9190 /* Diagnose "__thread extern". */
9191 if (specs->thread_p)
9192 error ("%<__thread%> before %<extern%>");
9199 /* Diagnose "__thread static". */
9200 if (specs->thread_p)
9201 error ("%<__thread%> before %<static%>");
9209 if (n != csc_none && n == specs->storage_class)
9212 error ("duplicate %qE", scspec);
9215 if (specs->storage_class != csc_none && n != specs->storage_class)
9217 error ("multiple storage classes in declaration specifiers");
9221 specs->storage_class = n;
9222 if (n != csc_extern && n != csc_static && specs->thread_p)
9224 error ("%<__thread%> used with %qE", scspec);
9225 specs->thread_p = false;
9232 /* Add the attributes ATTRS to the declaration specifiers SPECS,
9235 struct c_declspecs *
9236 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
9238 specs->attrs = chainon (attrs, specs->attrs);
9239 specs->declspecs_seen_p = true;
9243 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
9244 specifiers with any other type specifier to determine the resulting
9245 type. This is where ISO C checks on complex types are made, since
9246 "_Complex long" is a prefix of the valid ISO C type "_Complex long
9249 struct c_declspecs *
9250 finish_declspecs (struct c_declspecs *specs)
9252 /* If a type was specified as a whole, we have no modifiers and are
9254 if (specs->type != NULL_TREE)
9256 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
9257 && !specs->signed_p && !specs->unsigned_p
9258 && !specs->complex_p);
9262 /* If none of "void", "_Bool", "char", "int", "float" or "double"
9263 has been specified, treat it as "int" unless "_Complex" is
9264 present and there are no other specifiers. If we just have
9265 "_Complex", it is equivalent to "_Complex double", but e.g.
9266 "_Complex short" is equivalent to "_Complex short int". */
9267 if (specs->typespec_word == cts_none)
9269 if (specs->saturating_p)
9271 error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
9272 if (!targetm.fixed_point_supported_p ())
9273 error ("fixed-point types not supported for this target");
9274 specs->typespec_word = cts_fract;
9276 else if (specs->long_p || specs->short_p
9277 || specs->signed_p || specs->unsigned_p)
9279 specs->typespec_word = cts_int;
9281 else if (specs->complex_p)
9283 specs->typespec_word = cts_double;
9284 pedwarn (input_location, OPT_pedantic,
9285 "ISO C does not support plain %<complex%> meaning "
9286 "%<double complex%>");
9290 specs->typespec_word = cts_int;
9291 specs->default_int_p = true;
9292 /* We don't diagnose this here because grokdeclarator will
9293 give more specific diagnostics according to whether it is
9294 a function definition. */
9298 /* If "signed" was specified, record this to distinguish "int" and
9299 "signed int" in the case of a bit-field with
9300 -funsigned-bitfields. */
9301 specs->explicit_signed_p = specs->signed_p;
9303 /* Now compute the actual type. */
9304 switch (specs->typespec_word)
9307 gcc_assert (!specs->long_p && !specs->short_p
9308 && !specs->signed_p && !specs->unsigned_p
9309 && !specs->complex_p);
9310 specs->type = void_type_node;
9313 gcc_assert (!specs->long_p && !specs->short_p
9314 && !specs->signed_p && !specs->unsigned_p
9315 && !specs->complex_p);
9316 specs->type = boolean_type_node;
9319 gcc_assert (!specs->long_p && !specs->short_p);
9320 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9321 if (specs->signed_p)
9322 specs->type = signed_char_type_node;
9323 else if (specs->unsigned_p)
9324 specs->type = unsigned_char_type_node;
9326 specs->type = char_type_node;
9327 if (specs->complex_p)
9329 pedwarn (input_location, OPT_pedantic,
9330 "ISO C does not support complex integer types");
9331 specs->type = build_complex_type (specs->type);
9335 gcc_assert (!(specs->long_p && specs->short_p));
9336 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9337 if (specs->long_long_p)
9338 specs->type = (specs->unsigned_p
9339 ? long_long_unsigned_type_node
9340 : long_long_integer_type_node);
9341 else if (specs->long_p)
9342 specs->type = (specs->unsigned_p
9343 ? long_unsigned_type_node
9344 : long_integer_type_node);
9345 else if (specs->short_p)
9346 specs->type = (specs->unsigned_p
9347 ? short_unsigned_type_node
9348 : short_integer_type_node);
9350 specs->type = (specs->unsigned_p
9351 ? unsigned_type_node
9352 : integer_type_node);
9353 if (specs->complex_p)
9355 pedwarn (input_location, OPT_pedantic,
9356 "ISO C does not support complex integer types");
9357 specs->type = build_complex_type (specs->type);
9361 gcc_assert (!specs->long_p && !specs->short_p
9362 && !specs->signed_p && !specs->unsigned_p);
9363 specs->type = (specs->complex_p
9364 ? complex_float_type_node
9368 gcc_assert (!specs->long_long_p && !specs->short_p
9369 && !specs->signed_p && !specs->unsigned_p);
9372 specs->type = (specs->complex_p
9373 ? complex_long_double_type_node
9374 : long_double_type_node);
9378 specs->type = (specs->complex_p
9379 ? complex_double_type_node
9380 : double_type_node);
9386 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
9387 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
9388 if (specs->typespec_word == cts_dfloat32)
9389 specs->type = dfloat32_type_node;
9390 else if (specs->typespec_word == cts_dfloat64)
9391 specs->type = dfloat64_type_node;
9393 specs->type = dfloat128_type_node;
9396 gcc_assert (!specs->complex_p);
9397 if (!targetm.fixed_point_supported_p ())
9398 specs->type = integer_type_node;
9399 else if (specs->saturating_p)
9401 if (specs->long_long_p)
9402 specs->type = specs->unsigned_p
9403 ? sat_unsigned_long_long_fract_type_node
9404 : sat_long_long_fract_type_node;
9405 else if (specs->long_p)
9406 specs->type = specs->unsigned_p
9407 ? sat_unsigned_long_fract_type_node
9408 : sat_long_fract_type_node;
9409 else if (specs->short_p)
9410 specs->type = specs->unsigned_p
9411 ? sat_unsigned_short_fract_type_node
9412 : sat_short_fract_type_node;
9414 specs->type = specs->unsigned_p
9415 ? sat_unsigned_fract_type_node
9416 : sat_fract_type_node;
9420 if (specs->long_long_p)
9421 specs->type = specs->unsigned_p
9422 ? unsigned_long_long_fract_type_node
9423 : long_long_fract_type_node;
9424 else if (specs->long_p)
9425 specs->type = specs->unsigned_p
9426 ? unsigned_long_fract_type_node
9427 : long_fract_type_node;
9428 else if (specs->short_p)
9429 specs->type = specs->unsigned_p
9430 ? unsigned_short_fract_type_node
9431 : short_fract_type_node;
9433 specs->type = specs->unsigned_p
9434 ? unsigned_fract_type_node
9439 gcc_assert (!specs->complex_p);
9440 if (!targetm.fixed_point_supported_p ())
9441 specs->type = integer_type_node;
9442 else if (specs->saturating_p)
9444 if (specs->long_long_p)
9445 specs->type = specs->unsigned_p
9446 ? sat_unsigned_long_long_accum_type_node
9447 : sat_long_long_accum_type_node;
9448 else if (specs->long_p)
9449 specs->type = specs->unsigned_p
9450 ? sat_unsigned_long_accum_type_node
9451 : sat_long_accum_type_node;
9452 else if (specs->short_p)
9453 specs->type = specs->unsigned_p
9454 ? sat_unsigned_short_accum_type_node
9455 : sat_short_accum_type_node;
9457 specs->type = specs->unsigned_p
9458 ? sat_unsigned_accum_type_node
9459 : sat_accum_type_node;
9463 if (specs->long_long_p)
9464 specs->type = specs->unsigned_p
9465 ? unsigned_long_long_accum_type_node
9466 : long_long_accum_type_node;
9467 else if (specs->long_p)
9468 specs->type = specs->unsigned_p
9469 ? unsigned_long_accum_type_node
9470 : long_accum_type_node;
9471 else if (specs->short_p)
9472 specs->type = specs->unsigned_p
9473 ? unsigned_short_accum_type_node
9474 : short_accum_type_node;
9476 specs->type = specs->unsigned_p
9477 ? unsigned_accum_type_node
9488 /* A subroutine of c_write_global_declarations. Perform final processing
9489 on one file scope's declarations (or the external scope's declarations),
9493 c_write_global_declarations_1 (tree globals)
9498 /* Process the decls in the order they were written. */
9499 for (decl = globals; decl; decl = TREE_CHAIN (decl))
9501 /* Check for used but undefined static functions using the C
9502 standard's definition of "used", and set TREE_NO_WARNING so
9503 that check_global_declarations doesn't repeat the check. */
9504 if (TREE_CODE (decl) == FUNCTION_DECL
9505 && DECL_INITIAL (decl) == 0
9506 && DECL_EXTERNAL (decl)
9507 && !TREE_PUBLIC (decl)
9508 && C_DECL_USED (decl))
9510 pedwarn (input_location, 0, "%q+F used but never defined", decl);
9511 TREE_NO_WARNING (decl) = 1;
9514 wrapup_global_declaration_1 (decl);
9520 for (decl = globals; decl; decl = TREE_CHAIN (decl))
9521 reconsider |= wrapup_global_declaration_2 (decl);
9525 for (decl = globals; decl; decl = TREE_CHAIN (decl))
9526 check_global_declaration_1 (decl);
9529 /* A subroutine of c_write_global_declarations Emit debug information for each
9530 of the declarations in GLOBALS. */
9533 c_write_global_declarations_2 (tree globals)
9537 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
9538 debug_hooks->global_decl (decl);
9541 /* Preserve the external declarations scope across a garbage collect. */
9542 static GTY(()) tree ext_block;
9545 c_write_global_declarations (void)
9549 /* We don't want to do this if generating a PCH. */
9553 /* Don't waste time on further processing if -fsyntax-only.
9554 Continue for warning and errors issued during lowering though. */
9555 if (flag_syntax_only)
9558 /* Close the external scope. */
9559 ext_block = pop_scope ();
9561 gcc_assert (!current_scope);
9565 tree tmp = BLOCK_VARS (ext_block);
9567 FILE * stream = dump_begin (TDI_tu, &flags);
9570 dump_node (tmp, flags & ~TDF_SLIM, stream);
9571 dump_end (TDI_tu, stream);
9575 /* Process all file scopes in this compilation, and the external_scope,
9576 through wrapup_global_declarations and check_global_declarations. */
9577 for (t = all_translation_units; t; t = TREE_CHAIN (t))
9578 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
9579 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
9581 /* We're done parsing; proceed to optimize and emit assembly.
9582 FIXME: shouldn't be the front end's responsibility to call this. */
9583 cgraph_finalize_compilation_unit ();
9585 /* After cgraph has had a chance to emit everything that's going to
9586 be emitted, output debug information for globals. */
9587 if (errorcount == 0 && sorrycount == 0)
9589 timevar_push (TV_SYMOUT);
9590 for (t = all_translation_units; t; t = TREE_CHAIN (t))
9591 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
9592 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
9593 timevar_pop (TV_SYMOUT);
9599 #include "gt-c-decl.h"