Merged r158907 through r159238 into branch.
[official-gcc.git] / gcc / c-decl.c
blob53fed5545e293642c6cc2b18321531f368a861ca
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
11 version.
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
16 for more details.
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. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "input.h"
33 #include "tm.h"
34 #include "intl.h"
35 #include "tree.h"
36 #include "tree-inline.h"
37 #include "rtl.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "output.h"
41 #include "expr.h"
42 #include "c-tree.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "tm_p.h"
46 #include "cpplib.h"
47 #include "target.h"
48 #include "debug.h"
49 #include "opts.h"
50 #include "timevar.h"
51 #include "c-common.h"
52 #include "c-pragma.h"
53 #include "c-lang.h"
54 #include "langhooks.h"
55 #include "tree-mudflap.h"
56 #include "gimple.h"
57 #include "tree-iterator.h"
58 #include "diagnostic.h"
59 #include "tree-dump.h"
60 #include "cgraph.h"
61 #include "hashtab.h"
62 #include "libfuncs.h"
63 #include "except.h"
64 #include "langhooks-def.h"
65 #include "pointer-set.h"
66 #include "gimple.h"
67 #include "plugin.h"
69 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
70 enum decl_context
71 { NORMAL, /* Ordinary declaration */
72 FUNCDEF, /* Function definition */
73 PARM, /* Declaration of parm before function body */
74 FIELD, /* Declaration inside struct or union */
75 TYPENAME}; /* Typename (inside cast or sizeof) */
77 /* States indicating how grokdeclarator() should handle declspecs marked
78 with __attribute__((deprecated)). An object declared as
79 __attribute__((deprecated)) suppresses warnings of uses of other
80 deprecated items. */
82 enum deprecated_states {
83 DEPRECATED_NORMAL,
84 DEPRECATED_SUPPRESS
88 /* Nonzero if we have seen an invalid cross reference
89 to a struct, union, or enum, but not yet printed the message. */
90 tree pending_invalid_xref;
92 /* File and line to appear in the eventual error message. */
93 location_t pending_invalid_xref_location;
95 /* The file and line that the prototype came from if this is an
96 old-style definition; used for diagnostics in
97 store_parm_decls_oldstyle. */
99 static location_t current_function_prototype_locus;
101 /* Whether this prototype was built-in. */
103 static bool current_function_prototype_built_in;
105 /* The argument type information of this prototype. */
107 static tree current_function_prototype_arg_types;
109 /* The argument information structure for the function currently being
110 defined. */
112 static struct c_arg_info *current_function_arg_info;
114 /* The obstack on which parser and related data structures, which are
115 not live beyond their top-level declaration or definition, are
116 allocated. */
117 struct obstack parser_obstack;
119 /* The current statement tree. */
121 static GTY(()) struct stmt_tree_s c_stmt_tree;
123 /* State saving variables. */
124 tree c_break_label;
125 tree c_cont_label;
127 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
128 included in this invocation. Note that the current translation
129 unit is not included in this list. */
131 static GTY(()) tree all_translation_units;
133 /* A list of decls to be made automatically visible in each file scope. */
134 static GTY(()) tree visible_builtins;
136 /* Set to 0 at beginning of a function definition, set to 1 if
137 a return statement that specifies a return value is seen. */
139 int current_function_returns_value;
141 /* Set to 0 at beginning of a function definition, set to 1 if
142 a return statement with no argument is seen. */
144 int current_function_returns_null;
146 /* Set to 0 at beginning of a function definition, set to 1 if
147 a call to a noreturn function is seen. */
149 int current_function_returns_abnormally;
151 /* Set to nonzero by `grokdeclarator' for a function
152 whose return type is defaulted, if warnings for this are desired. */
154 static int warn_about_return_type;
156 /* Nonzero when the current toplevel function contains a declaration
157 of a nested function which is never defined. */
159 static bool undef_nested_function;
161 /* True means global_bindings_p should return false even if the scope stack
162 says we are in file scope. */
163 bool c_override_global_bindings_to_false;
166 /* Each c_binding structure describes one binding of an identifier to
167 a decl. All the decls in a scope - irrespective of namespace - are
168 chained together by the ->prev field, which (as the name implies)
169 runs in reverse order. All the decls in a given namespace bound to
170 a given identifier are chained by the ->shadowed field, which runs
171 from inner to outer scopes.
173 The ->decl field usually points to a DECL node, but there are two
174 exceptions. In the namespace of type tags, the bound entity is a
175 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
176 identifier is encountered, it is bound to error_mark_node to
177 suppress further errors about that identifier in the current
178 function.
180 The ->u.type field stores the type of the declaration in this scope;
181 if NULL, the type is the type of the ->decl field. This is only of
182 relevance for objects with external or internal linkage which may
183 be redeclared in inner scopes, forming composite types that only
184 persist for the duration of those scopes. In the external scope,
185 this stores the composite of all the types declared for this
186 object, visible or not. The ->inner_comp field (used only at file
187 scope) stores whether an incomplete array type at file scope was
188 completed at an inner scope to an array size other than 1.
190 The ->u.label field is used for labels. It points to a structure
191 which stores additional information used for warnings.
193 The depth field is copied from the scope structure that holds this
194 decl. It is used to preserve the proper ordering of the ->shadowed
195 field (see bind()) and also for a handful of special-case checks.
196 Finally, the invisible bit is true for a decl which should be
197 ignored for purposes of normal name lookup, and the nested bit is
198 true for a decl that's been bound a second time in an inner scope;
199 in all such cases, the binding in the outer scope will have its
200 invisible bit true. */
202 struct GTY((chain_next ("%h.prev"))) c_binding {
203 union GTY(()) { /* first so GTY desc can use decl */
204 tree GTY((tag ("0"))) type; /* the type in this scope */
205 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
206 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
207 tree decl; /* the decl bound */
208 tree id; /* the identifier it's bound to */
209 struct c_binding *prev; /* the previous decl in this scope */
210 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
211 unsigned int depth : 28; /* depth of this scope */
212 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
213 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
214 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
215 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
216 location_t locus; /* location for nested bindings */
218 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
219 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
220 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
221 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
223 #define I_SYMBOL_BINDING(node) \
224 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
225 #define I_SYMBOL_DECL(node) \
226 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
228 #define I_TAG_BINDING(node) \
229 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
230 #define I_TAG_DECL(node) \
231 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
233 #define I_LABEL_BINDING(node) \
234 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
235 #define I_LABEL_DECL(node) \
236 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
238 /* Each C symbol points to three linked lists of c_binding structures.
239 These describe the values of the identifier in the three different
240 namespaces defined by the language. */
242 struct GTY(()) lang_identifier {
243 struct c_common_identifier common_id;
244 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
245 struct c_binding *tag_binding; /* struct/union/enum tags */
246 struct c_binding *label_binding; /* labels */
249 /* Validate c-lang.c's assumptions. */
250 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
251 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
253 /* The resulting tree type. */
255 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
256 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
258 union tree_node GTY ((tag ("0"),
259 desc ("tree_node_structure (&%h)")))
260 generic;
261 struct lang_identifier GTY ((tag ("1"))) identifier;
264 /* Track bindings and other things that matter for goto warnings. For
265 efficiency, we do not gather all the decls at the point of
266 definition. Instead, we point into the bindings structure. As
267 scopes are popped, we update these structures and gather the decls
268 that matter at that time. */
270 struct GTY(()) c_spot_bindings {
271 /* The currently open scope which holds bindings defined when the
272 label was defined or the goto statement was found. */
273 struct c_scope *scope;
274 /* The bindings in the scope field which were defined at the point
275 of the label or goto. This lets us look at older or newer
276 bindings in the scope, as appropriate. */
277 struct c_binding *bindings_in_scope;
278 /* The number of statement expressions that have started since this
279 label or goto statement was defined. This is zero if we are at
280 the same statement expression level. It is positive if we are in
281 a statement expression started since this spot. It is negative
282 if this spot was in a statement expression and we have left
283 it. */
284 int stmt_exprs;
285 /* Whether we started in a statement expression but are no longer in
286 it. This is set to true if stmt_exprs ever goes negative. */
287 bool left_stmt_expr;
290 /* This structure is used to keep track of bindings seen when a goto
291 statement is defined. This is only used if we see the goto
292 statement before we see the label. */
294 struct GTY(()) c_goto_bindings {
295 /* The location of the goto statement. */
296 location_t loc;
297 /* The bindings of the goto statement. */
298 struct c_spot_bindings goto_bindings;
301 typedef struct c_goto_bindings *c_goto_bindings_p;
302 DEF_VEC_P(c_goto_bindings_p);
303 DEF_VEC_ALLOC_P(c_goto_bindings_p,gc);
305 /* The additional information we keep track of for a label binding.
306 These fields are updated as scopes are popped. */
308 struct GTY(()) c_label_vars {
309 /* The shadowed c_label_vars, when one label shadows another (which
310 can only happen using a __label__ declaration). */
311 struct c_label_vars *shadowed;
312 /* The bindings when the label was defined. */
313 struct c_spot_bindings label_bindings;
314 /* A list of decls that we care about: decls about which we should
315 warn if a goto branches to this label from later in the function.
316 Decls are added to this list as scopes are popped. We only add
317 the decls that matter. */
318 VEC(tree,gc) *decls_in_scope;
319 /* A list of goto statements to this label. This is only used for
320 goto statements seen before the label was defined, so that we can
321 issue appropriate warnings for them. */
322 VEC(c_goto_bindings_p,gc) *gotos;
325 /* Each c_scope structure describes the complete contents of one
326 scope. Four scopes are distinguished specially: the innermost or
327 current scope, the innermost function scope, the file scope (always
328 the second to outermost) and the outermost or external scope.
330 Most declarations are recorded in the current scope.
332 All normal label declarations are recorded in the innermost
333 function scope, as are bindings of undeclared identifiers to
334 error_mark_node. (GCC permits nested functions as an extension,
335 hence the 'innermost' qualifier.) Explicitly declared labels
336 (using the __label__ extension) appear in the current scope.
338 Being in the file scope (current_scope == file_scope) causes
339 special behavior in several places below. Also, under some
340 conditions the Objective-C front end records declarations in the
341 file scope even though that isn't the current scope.
343 All declarations with external linkage are recorded in the external
344 scope, even if they aren't visible there; this models the fact that
345 such declarations are visible to the entire program, and (with a
346 bit of cleverness, see pushdecl) allows diagnosis of some violations
347 of C99 6.2.2p7 and 6.2.7p2:
349 If, within the same translation unit, the same identifier appears
350 with both internal and external linkage, the behavior is
351 undefined.
353 All declarations that refer to the same object or function shall
354 have compatible type; otherwise, the behavior is undefined.
356 Initially only the built-in declarations, which describe compiler
357 intrinsic functions plus a subset of the standard library, are in
358 this scope.
360 The order of the blocks list matters, and it is frequently appended
361 to. To avoid having to walk all the way to the end of the list on
362 each insertion, or reverse the list later, we maintain a pointer to
363 the last list entry. (FIXME: It should be feasible to use a reversed
364 list here.)
366 The bindings list is strictly in reverse order of declarations;
367 pop_scope relies on this. */
370 struct GTY((chain_next ("%h.outer"))) c_scope {
371 /* The scope containing this one. */
372 struct c_scope *outer;
374 /* The next outermost function scope. */
375 struct c_scope *outer_function;
377 /* All bindings in this scope. */
378 struct c_binding *bindings;
380 /* For each scope (except the global one), a chain of BLOCK nodes
381 for all the scopes that were entered and exited one level down. */
382 tree blocks;
383 tree blocks_last;
385 /* The depth of this scope. Used to keep the ->shadowed chain of
386 bindings sorted innermost to outermost. */
387 unsigned int depth : 28;
389 /* True if we are currently filling this scope with parameter
390 declarations. */
391 BOOL_BITFIELD parm_flag : 1;
393 /* True if we saw [*] in this scope. Used to give an error messages
394 if these appears in a function definition. */
395 BOOL_BITFIELD had_vla_unspec : 1;
397 /* True if we already complained about forward parameter decls
398 in this scope. This prevents double warnings on
399 foo (int a; int b; ...) */
400 BOOL_BITFIELD warned_forward_parm_decls : 1;
402 /* True if this is the outermost block scope of a function body.
403 This scope contains the parameters, the local variables declared
404 in the outermost block, and all the labels (except those in
405 nested functions, or declared at block scope with __label__). */
406 BOOL_BITFIELD function_body : 1;
408 /* True means make a BLOCK for this scope no matter what. */
409 BOOL_BITFIELD keep : 1;
411 /* True means that an unsuffixed float constant is _Decimal64. */
412 BOOL_BITFIELD float_const_decimal64 : 1;
414 /* True if this scope has any label bindings. This is used to speed
415 up searching for labels when popping scopes, particularly since
416 labels are normally only found at function scope. */
417 BOOL_BITFIELD has_label_bindings : 1;
420 /* The scope currently in effect. */
422 static GTY(()) struct c_scope *current_scope;
424 /* The innermost function scope. Ordinary (not explicitly declared)
425 labels, bindings to error_mark_node, and the lazily-created
426 bindings of __func__ and its friends get this scope. */
428 static GTY(()) struct c_scope *current_function_scope;
430 /* The C file scope. This is reset for each input translation unit. */
432 static GTY(()) struct c_scope *file_scope;
434 /* The outermost scope. This is used for all declarations with
435 external linkage, and only these, hence the name. */
437 static GTY(()) struct c_scope *external_scope;
439 /* A chain of c_scope structures awaiting reuse. */
441 static GTY((deletable)) struct c_scope *scope_freelist;
443 /* A chain of c_binding structures awaiting reuse. */
445 static GTY((deletable)) struct c_binding *binding_freelist;
447 /* Append VAR to LIST in scope SCOPE. */
448 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
449 struct c_scope *s_ = (scope); \
450 tree d_ = (decl); \
451 if (s_->list##_last) \
452 BLOCK_CHAIN (s_->list##_last) = d_; \
453 else \
454 s_->list = d_; \
455 s_->list##_last = d_; \
456 } while (0)
458 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
459 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
460 struct c_scope *t_ = (tscope); \
461 struct c_scope *f_ = (fscope); \
462 if (t_->to##_last) \
463 BLOCK_CHAIN (t_->to##_last) = f_->from; \
464 else \
465 t_->to = f_->from; \
466 t_->to##_last = f_->from##_last; \
467 } while (0)
469 /* A c_inline_static structure stores details of a static identifier
470 referenced in a definition of a function that may be an inline
471 definition if no subsequent declaration of that function uses
472 "extern" or does not use "inline". */
474 struct GTY((chain_next ("%h.next"))) c_inline_static {
475 /* The location for a diagnostic. */
476 location_t location;
478 /* The function that may be an inline definition. */
479 tree function;
481 /* The object or function referenced. */
482 tree static_decl;
484 /* What sort of reference this is. */
485 enum c_inline_static_type type;
487 /* The next such structure or NULL. */
488 struct c_inline_static *next;
491 /* List of static identifiers used or referenced in functions that may
492 be inline definitions. */
493 static GTY(()) struct c_inline_static *c_inline_statics;
495 /* True means unconditionally make a BLOCK for the next scope pushed. */
497 static bool keep_next_level_flag;
499 /* True means the next call to push_scope will be the outermost scope
500 of a function body, so do not push a new scope, merely cease
501 expecting parameter decls. */
503 static bool next_is_function_body;
505 /* A VEC of pointers to c_binding structures. */
507 typedef struct c_binding *c_binding_ptr;
508 DEF_VEC_P(c_binding_ptr);
509 DEF_VEC_ALLOC_P(c_binding_ptr,heap);
511 /* Information that we keep for a struct or union while it is being
512 parsed. */
514 struct c_struct_parse_info
516 /* If warn_cxx_compat, a list of types defined within this
517 struct. */
518 VEC(tree,heap) *struct_types;
519 /* If warn_cxx_compat, a list of field names which have bindings,
520 and which are defined in this struct, but which are not defined
521 in any enclosing struct. This is used to clear the in_struct
522 field of the c_bindings structure. */
523 VEC(c_binding_ptr,heap) *fields;
524 /* If warn_cxx_compat, a list of typedef names used when defining
525 fields in this struct. */
526 VEC(tree,heap) *typedefs_seen;
529 /* Information for the struct or union currently being parsed, or
530 NULL if not parsing a struct or union. */
531 static struct c_struct_parse_info *struct_parse_info;
533 /* Forward declarations. */
534 static tree lookup_name_in_scope (tree, struct c_scope *);
535 static tree c_make_fname_decl (location_t, tree, int);
536 static tree grokdeclarator (const struct c_declarator *,
537 struct c_declspecs *,
538 enum decl_context, bool, tree *, tree *, tree *,
539 bool *, enum deprecated_states);
540 static tree grokparms (struct c_arg_info *, bool);
541 static void layout_array_type (tree);
543 /* T is a statement. Add it to the statement-tree. This is the
544 C/ObjC version--C++ has a slightly different version of this
545 function. */
547 tree
548 add_stmt (tree t)
550 enum tree_code code = TREE_CODE (t);
552 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
554 if (!EXPR_HAS_LOCATION (t))
555 SET_EXPR_LOCATION (t, input_location);
558 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
559 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
561 /* Add T to the statement-tree. Non-side-effect statements need to be
562 recorded during statement expressions. */
563 append_to_statement_list_force (t, &cur_stmt_list);
565 return t;
569 void
570 c_print_identifier (FILE *file, tree node, int indent)
572 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
573 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
574 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
575 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
577 tree rid = ridpointers[C_RID_CODE (node)];
578 indent_to (file, indent + 4);
579 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
580 (void *) rid, IDENTIFIER_POINTER (rid));
584 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
585 which may be any of several kinds of DECL or TYPE or error_mark_node,
586 in the scope SCOPE. */
587 static void
588 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
589 bool nested, location_t locus)
591 struct c_binding *b, **here;
593 if (binding_freelist)
595 b = binding_freelist;
596 binding_freelist = b->prev;
598 else
599 b = GGC_NEW (struct c_binding);
601 b->shadowed = 0;
602 b->decl = decl;
603 b->id = name;
604 b->depth = scope->depth;
605 b->invisible = invisible;
606 b->nested = nested;
607 b->inner_comp = 0;
608 b->in_struct = 0;
609 b->locus = locus;
611 b->u.type = NULL;
613 b->prev = scope->bindings;
614 scope->bindings = b;
616 if (!name)
617 return;
619 switch (TREE_CODE (decl))
621 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
622 case ENUMERAL_TYPE:
623 case UNION_TYPE:
624 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
625 case VAR_DECL:
626 case FUNCTION_DECL:
627 case TYPE_DECL:
628 case CONST_DECL:
629 case PARM_DECL:
630 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
632 default:
633 gcc_unreachable ();
636 /* Locate the appropriate place in the chain of shadowed decls
637 to insert this binding. Normally, scope == current_scope and
638 this does nothing. */
639 while (*here && (*here)->depth > scope->depth)
640 here = &(*here)->shadowed;
642 b->shadowed = *here;
643 *here = b;
646 /* Clear the binding structure B, stick it on the binding_freelist,
647 and return the former value of b->prev. This is used by pop_scope
648 and get_parm_info to iterate destructively over all the bindings
649 from a given scope. */
650 static struct c_binding *
651 free_binding_and_advance (struct c_binding *b)
653 struct c_binding *prev = b->prev;
655 memset (b, 0, sizeof (struct c_binding));
656 b->prev = binding_freelist;
657 binding_freelist = b;
659 return prev;
662 /* Bind a label. Like bind, but skip fields which aren't used for
663 labels, and add the LABEL_VARS value. */
664 static void
665 bind_label (tree name, tree label, struct c_scope *scope,
666 struct c_label_vars *label_vars)
668 struct c_binding *b;
670 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
671 UNKNOWN_LOCATION);
673 scope->has_label_bindings = true;
675 b = scope->bindings;
676 gcc_assert (b->decl == label);
677 label_vars->shadowed = b->u.label;
678 b->u.label = label_vars;
681 /* Hook called at end of compilation to assume 1 elt
682 for a file-scope tentative array defn that wasn't complete before. */
684 void
685 c_finish_incomplete_decl (tree decl)
687 if (TREE_CODE (decl) == VAR_DECL)
689 tree type = TREE_TYPE (decl);
690 if (type != error_mark_node
691 && TREE_CODE (type) == ARRAY_TYPE
692 && !DECL_EXTERNAL (decl)
693 && TYPE_DOMAIN (type) == 0)
695 warning_at (DECL_SOURCE_LOCATION (decl),
696 0, "array %q+D assumed to have one element", decl);
698 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
700 layout_decl (decl, 0);
705 /* Record that inline function FUNC contains a reference (location
706 LOC) to static DECL (file-scope or function-local according to
707 TYPE). */
709 void
710 record_inline_static (location_t loc, tree func, tree decl,
711 enum c_inline_static_type type)
713 struct c_inline_static *csi = GGC_NEW (struct c_inline_static);
714 csi->location = loc;
715 csi->function = func;
716 csi->static_decl = decl;
717 csi->type = type;
718 csi->next = c_inline_statics;
719 c_inline_statics = csi;
722 /* Check for references to static declarations in inline functions at
723 the end of the translation unit and diagnose them if the functions
724 are still inline definitions. */
726 static void
727 check_inline_statics (void)
729 struct c_inline_static *csi;
730 for (csi = c_inline_statics; csi; csi = csi->next)
732 if (DECL_EXTERNAL (csi->function))
733 switch (csi->type)
735 case csi_internal:
736 pedwarn (csi->location, 0,
737 "%qD is static but used in inline function %qD "
738 "which is not static", csi->static_decl, csi->function);
739 break;
740 case csi_modifiable:
741 pedwarn (csi->location, 0,
742 "%q+D is static but declared in inline function %qD "
743 "which is not static", csi->static_decl, csi->function);
744 break;
745 default:
746 gcc_unreachable ();
749 c_inline_statics = NULL;
752 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
753 for the current state, otherwise set it to uninitialized. */
755 static void
756 set_spot_bindings (struct c_spot_bindings *p, bool defining)
758 if (defining)
760 p->scope = current_scope;
761 p->bindings_in_scope = current_scope->bindings;
763 else
765 p->scope = NULL;
766 p->bindings_in_scope = NULL;
768 p->stmt_exprs = 0;
769 p->left_stmt_expr = false;
772 /* Return true if we will want to say something if a goto statement
773 crosses DECL. */
775 static bool
776 decl_jump_unsafe (tree decl)
778 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
779 return false;
781 /* Always warn about crossing variably modified types. */
782 if ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == TYPE_DECL)
783 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
784 return true;
786 /* Otherwise, only warn if -Wgoto-misses-init and this is an
787 initialized automatic decl. */
788 if (warn_jump_misses_init
789 && TREE_CODE (decl) == VAR_DECL
790 && !TREE_STATIC (decl)
791 && DECL_INITIAL (decl) != NULL_TREE)
792 return true;
794 return false;
797 /* Update spot bindings P as we pop out of SCOPE. Return true if we
798 should push decls for a label. */
800 static bool
801 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
803 if (p->scope != scope)
805 /* This label or goto is defined in some other scope, or it is a
806 label which is not yet defined. There is nothing to
807 update. */
808 return false;
811 /* Adjust the spot bindings to refer to the bindings already defined
812 in the enclosing scope. */
813 p->scope = scope->outer;
814 p->bindings_in_scope = p->scope->bindings;
816 return true;
819 /* The Objective-C front-end often needs to determine the current scope. */
821 void *
822 objc_get_current_scope (void)
824 return current_scope;
827 /* The following function is used only by Objective-C. It needs to live here
828 because it accesses the innards of c_scope. */
830 void
831 objc_mark_locals_volatile (void *enclosing_blk)
833 struct c_scope *scope;
834 struct c_binding *b;
836 for (scope = current_scope;
837 scope && scope != enclosing_blk;
838 scope = scope->outer)
840 for (b = scope->bindings; b; b = b->prev)
841 objc_volatilize_decl (b->decl);
843 /* Do not climb up past the current function. */
844 if (scope->function_body)
845 break;
849 /* Nonzero if we are currently in file scope. */
852 global_bindings_p (void)
854 return (current_scope == file_scope && !c_override_global_bindings_to_false
855 ? -1
856 : 0);
859 void
860 keep_next_level (void)
862 keep_next_level_flag = true;
865 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
867 void
868 set_float_const_decimal64 (void)
870 current_scope->float_const_decimal64 = true;
873 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
875 void
876 clear_float_const_decimal64 (void)
878 current_scope->float_const_decimal64 = false;
881 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
883 bool
884 float_const_decimal64_p (void)
886 return current_scope->float_const_decimal64;
889 /* Identify this scope as currently being filled with parameters. */
891 void
892 declare_parm_level (void)
894 current_scope->parm_flag = true;
897 void
898 push_scope (void)
900 if (next_is_function_body)
902 /* This is the transition from the parameters to the top level
903 of the function body. These are the same scope
904 (C99 6.2.1p4,6) so we do not push another scope structure.
905 next_is_function_body is set only by store_parm_decls, which
906 in turn is called when and only when we are about to
907 encounter the opening curly brace for the function body.
909 The outermost block of a function always gets a BLOCK node,
910 because the debugging output routines expect that each
911 function has at least one BLOCK. */
912 current_scope->parm_flag = false;
913 current_scope->function_body = true;
914 current_scope->keep = true;
915 current_scope->outer_function = current_function_scope;
916 current_function_scope = current_scope;
918 keep_next_level_flag = false;
919 next_is_function_body = false;
921 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
922 if (current_scope->outer)
923 current_scope->float_const_decimal64
924 = current_scope->outer->float_const_decimal64;
925 else
926 current_scope->float_const_decimal64 = false;
928 else
930 struct c_scope *scope;
931 if (scope_freelist)
933 scope = scope_freelist;
934 scope_freelist = scope->outer;
936 else
937 scope = GGC_CNEW (struct c_scope);
939 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
940 if (current_scope)
941 scope->float_const_decimal64 = current_scope->float_const_decimal64;
942 else
943 scope->float_const_decimal64 = false;
945 scope->keep = keep_next_level_flag;
946 scope->outer = current_scope;
947 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
949 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
950 possible. */
951 if (current_scope && scope->depth == 0)
953 scope->depth--;
954 sorry ("GCC supports only %u nested scopes", scope->depth);
957 current_scope = scope;
958 keep_next_level_flag = false;
962 /* This is called when we are leaving SCOPE. For each label defined
963 in SCOPE, add any appropriate decls to its decls_in_scope fields.
964 These are the decls whose initialization will be skipped by a goto
965 later in the function. */
967 static void
968 update_label_decls (struct c_scope *scope)
970 struct c_scope *s;
972 s = scope;
973 while (s != NULL)
975 if (s->has_label_bindings)
977 struct c_binding *b;
979 for (b = s->bindings; b != NULL; b = b->prev)
981 struct c_label_vars *label_vars;
982 struct c_binding *b1;
983 unsigned int ix;
984 struct c_goto_bindings *g;
986 if (TREE_CODE (b->decl) != LABEL_DECL)
987 continue;
988 label_vars = b->u.label;
990 b1 = label_vars->label_bindings.bindings_in_scope;
991 if (update_spot_bindings (scope, &label_vars->label_bindings))
993 /* This label is defined in this scope. */
994 for (; b1 != NULL; b1 = b1->prev)
996 /* A goto from later in the function to this
997 label will never see the initialization of
998 B1, if any. Save it to issue a warning if
999 needed. */
1000 if (decl_jump_unsafe (b1->decl))
1001 VEC_safe_push (tree, gc, label_vars->decls_in_scope,
1002 b1->decl);
1006 /* Update the bindings of any goto statements associated
1007 with this label. */
1008 for (ix = 0;
1009 VEC_iterate (c_goto_bindings_p, label_vars->gotos, ix, g);
1010 ++ix)
1011 update_spot_bindings (scope, &g->goto_bindings);
1015 /* Don't search beyond the current function. */
1016 if (s == current_function_scope)
1017 break;
1019 s = s->outer;
1023 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1025 static void
1026 set_type_context (tree type, tree context)
1028 for (type = TYPE_MAIN_VARIANT (type); type;
1029 type = TYPE_NEXT_VARIANT (type))
1030 TYPE_CONTEXT (type) = context;
1033 /* Exit a scope. Restore the state of the identifier-decl mappings
1034 that were in effect when this scope was entered. Return a BLOCK
1035 node containing all the DECLs in this scope that are of interest
1036 to debug info generation. */
1038 tree
1039 pop_scope (void)
1041 struct c_scope *scope = current_scope;
1042 tree block, context, p;
1043 struct c_binding *b;
1045 bool functionbody = scope->function_body;
1046 bool keep = functionbody || scope->keep || scope->bindings;
1048 update_label_decls (scope);
1050 /* If appropriate, create a BLOCK to record the decls for the life
1051 of this function. */
1052 block = 0;
1053 if (keep)
1055 block = make_node (BLOCK);
1056 BLOCK_SUBBLOCKS (block) = scope->blocks;
1057 TREE_USED (block) = 1;
1059 /* In each subblock, record that this is its superior. */
1060 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1061 BLOCK_SUPERCONTEXT (p) = block;
1063 BLOCK_VARS (block) = 0;
1066 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1067 scope must be set so that they point to the appropriate
1068 construct, i.e. either to the current FUNCTION_DECL node, or
1069 else to the BLOCK node we just constructed.
1071 Note that for tagged types whose scope is just the formal
1072 parameter list for some function type specification, we can't
1073 properly set their TYPE_CONTEXTs here, because we don't have a
1074 pointer to the appropriate FUNCTION_TYPE node readily available
1075 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1076 type nodes get set in `grokdeclarator' as soon as we have created
1077 the FUNCTION_TYPE node which will represent the "scope" for these
1078 "parameter list local" tagged types. */
1079 if (scope->function_body)
1080 context = current_function_decl;
1081 else if (scope == file_scope)
1083 tree file_decl = build_decl (UNKNOWN_LOCATION,
1084 TRANSLATION_UNIT_DECL, 0, 0);
1085 TREE_CHAIN (file_decl) = all_translation_units;
1086 all_translation_units = file_decl;
1087 context = file_decl;
1089 else
1090 context = block;
1092 /* Clear all bindings in this scope. */
1093 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1095 p = b->decl;
1096 switch (TREE_CODE (p))
1098 case LABEL_DECL:
1099 /* Warnings for unused labels, errors for undefined labels. */
1100 if (TREE_USED (p) && !DECL_INITIAL (p))
1102 error ("label %q+D used but not defined", p);
1103 DECL_INITIAL (p) = error_mark_node;
1105 else
1106 warn_for_unused_label (p);
1108 /* Labels go in BLOCK_VARS. */
1109 TREE_CHAIN (p) = BLOCK_VARS (block);
1110 BLOCK_VARS (block) = p;
1111 gcc_assert (I_LABEL_BINDING (b->id) == b);
1112 I_LABEL_BINDING (b->id) = b->shadowed;
1114 /* Also pop back to the shadowed label_vars. */
1115 release_tree_vector (b->u.label->decls_in_scope);
1116 b->u.label = b->u.label->shadowed;
1117 break;
1119 case ENUMERAL_TYPE:
1120 case UNION_TYPE:
1121 case RECORD_TYPE:
1122 set_type_context (p, context);
1124 /* Types may not have tag-names, in which case the type
1125 appears in the bindings list with b->id NULL. */
1126 if (b->id)
1128 gcc_assert (I_TAG_BINDING (b->id) == b);
1129 I_TAG_BINDING (b->id) = b->shadowed;
1131 break;
1133 case FUNCTION_DECL:
1134 /* Propagate TREE_ADDRESSABLE from nested functions to their
1135 containing functions. */
1136 if (!TREE_ASM_WRITTEN (p)
1137 && DECL_INITIAL (p) != 0
1138 && TREE_ADDRESSABLE (p)
1139 && DECL_ABSTRACT_ORIGIN (p) != 0
1140 && DECL_ABSTRACT_ORIGIN (p) != p)
1141 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1142 if (!DECL_EXTERNAL (p)
1143 && !DECL_INITIAL (p)
1144 && scope != file_scope
1145 && scope != external_scope)
1147 error ("nested function %q+D declared but never defined", p);
1148 undef_nested_function = true;
1150 else if (DECL_DECLARED_INLINE_P (p)
1151 && TREE_PUBLIC (p)
1152 && !DECL_INITIAL (p))
1154 /* C99 6.7.4p6: "a function with external linkage... declared
1155 with an inline function specifier ... shall also be defined
1156 in the same translation unit." */
1157 if (!flag_gnu89_inline)
1158 pedwarn (input_location, 0,
1159 "inline function %q+D declared but never defined", p);
1160 DECL_EXTERNAL (p) = 1;
1163 goto common_symbol;
1165 case VAR_DECL:
1166 /* Warnings for unused variables. */
1167 if ((!TREE_USED (p) || !DECL_READ_P (p))
1168 && !TREE_NO_WARNING (p)
1169 && !DECL_IN_SYSTEM_HEADER (p)
1170 && DECL_NAME (p)
1171 && !DECL_ARTIFICIAL (p)
1172 && scope != file_scope
1173 && scope != external_scope)
1175 if (!TREE_USED (p))
1176 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1177 else if (DECL_CONTEXT (p) == current_function_decl)
1178 warning_at (DECL_SOURCE_LOCATION (p),
1179 OPT_Wunused_but_set_variable,
1180 "variable %qD set but not used", p);
1183 if (b->inner_comp)
1185 error ("type of array %q+D completed incompatibly with"
1186 " implicit initialization", p);
1189 /* Fall through. */
1190 case TYPE_DECL:
1191 case CONST_DECL:
1192 common_symbol:
1193 /* All of these go in BLOCK_VARS, but only if this is the
1194 binding in the home scope. */
1195 if (!b->nested)
1197 TREE_CHAIN (p) = BLOCK_VARS (block);
1198 BLOCK_VARS (block) = p;
1200 else if (VAR_OR_FUNCTION_DECL_P (p))
1202 /* For block local externs add a special
1203 DECL_EXTERNAL decl for debug info generation. */
1204 tree extp = copy_node (p);
1206 DECL_EXTERNAL (extp) = 1;
1207 TREE_STATIC (extp) = 0;
1208 TREE_PUBLIC (extp) = 1;
1209 DECL_INITIAL (extp) = NULL_TREE;
1210 DECL_LANG_SPECIFIC (extp) = NULL;
1211 DECL_CONTEXT (extp) = current_function_decl;
1212 if (TREE_CODE (p) == FUNCTION_DECL)
1214 DECL_RESULT (extp) = NULL_TREE;
1215 DECL_SAVED_TREE (extp) = NULL_TREE;
1216 DECL_STRUCT_FUNCTION (extp) = NULL;
1218 if (b->locus != UNKNOWN_LOCATION)
1219 DECL_SOURCE_LOCATION (extp) = b->locus;
1220 TREE_CHAIN (extp) = BLOCK_VARS (block);
1221 BLOCK_VARS (block) = extp;
1223 /* If this is the file scope, and we are processing more
1224 than one translation unit in this compilation, set
1225 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
1226 This makes same_translation_unit_p work, and causes
1227 static declarations to be given disambiguating suffixes. */
1228 if (scope == file_scope && num_in_fnames > 1)
1230 DECL_CONTEXT (p) = context;
1231 if (TREE_CODE (p) == TYPE_DECL)
1232 set_type_context (TREE_TYPE (p), context);
1235 /* Fall through. */
1236 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1237 already been put there by store_parm_decls. Unused-
1238 parameter warnings are handled by function.c.
1239 error_mark_node obviously does not go in BLOCK_VARS and
1240 does not get unused-variable warnings. */
1241 case PARM_DECL:
1242 case ERROR_MARK:
1243 /* It is possible for a decl not to have a name. We get
1244 here with b->id NULL in this case. */
1245 if (b->id)
1247 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1248 I_SYMBOL_BINDING (b->id) = b->shadowed;
1249 if (b->shadowed && b->shadowed->u.type)
1250 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1252 break;
1254 default:
1255 gcc_unreachable ();
1260 /* Dispose of the block that we just made inside some higher level. */
1261 if ((scope->function_body || scope == file_scope) && context)
1263 DECL_INITIAL (context) = block;
1264 BLOCK_SUPERCONTEXT (block) = context;
1266 else if (scope->outer)
1268 if (block)
1269 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1270 /* If we did not make a block for the scope just exited, any
1271 blocks made for inner scopes must be carried forward so they
1272 will later become subblocks of something else. */
1273 else if (scope->blocks)
1274 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1277 /* Pop the current scope, and free the structure for reuse. */
1278 current_scope = scope->outer;
1279 if (scope->function_body)
1280 current_function_scope = scope->outer_function;
1282 memset (scope, 0, sizeof (struct c_scope));
1283 scope->outer = scope_freelist;
1284 scope_freelist = scope;
1286 return block;
1289 void
1290 push_file_scope (void)
1292 tree decl;
1294 if (file_scope)
1295 return;
1297 push_scope ();
1298 file_scope = current_scope;
1300 start_fname_decls ();
1302 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
1303 bind (DECL_NAME (decl), decl, file_scope,
1304 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1307 void
1308 pop_file_scope (void)
1310 /* In case there were missing closebraces, get us back to the global
1311 binding level. */
1312 while (current_scope != file_scope)
1313 pop_scope ();
1315 /* __FUNCTION__ is defined at file scope (""). This
1316 call may not be necessary as my tests indicate it
1317 still works without it. */
1318 finish_fname_decls ();
1320 check_inline_statics ();
1322 /* This is the point to write out a PCH if we're doing that.
1323 In that case we do not want to do anything else. */
1324 if (pch_file)
1326 c_common_write_pch ();
1327 return;
1330 /* Pop off the file scope and close this translation unit. */
1331 pop_scope ();
1332 file_scope = 0;
1334 maybe_apply_pending_pragma_weaks ();
1337 /* Adjust the bindings for the start of a statement expression. */
1339 void
1340 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1342 struct c_scope *scope;
1344 for (scope = current_scope; scope != NULL; scope = scope->outer)
1346 struct c_binding *b;
1348 if (!scope->has_label_bindings)
1349 continue;
1351 for (b = scope->bindings; b != NULL; b = b->prev)
1353 struct c_label_vars *label_vars;
1354 unsigned int ix;
1355 struct c_goto_bindings *g;
1357 if (TREE_CODE (b->decl) != LABEL_DECL)
1358 continue;
1359 label_vars = b->u.label;
1360 ++label_vars->label_bindings.stmt_exprs;
1361 for (ix = 0;
1362 VEC_iterate (c_goto_bindings_p, label_vars->gotos, ix, g);
1363 ++ix)
1364 ++g->goto_bindings.stmt_exprs;
1368 if (switch_bindings != NULL)
1369 ++switch_bindings->stmt_exprs;
1372 /* Adjust the bindings for the end of a statement expression. */
1374 void
1375 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1377 struct c_scope *scope;
1379 for (scope = current_scope; scope != NULL; scope = scope->outer)
1381 struct c_binding *b;
1383 if (!scope->has_label_bindings)
1384 continue;
1386 for (b = scope->bindings; b != NULL; b = b->prev)
1388 struct c_label_vars *label_vars;
1389 unsigned int ix;
1390 struct c_goto_bindings *g;
1392 if (TREE_CODE (b->decl) != LABEL_DECL)
1393 continue;
1394 label_vars = b->u.label;
1395 --label_vars->label_bindings.stmt_exprs;
1396 if (label_vars->label_bindings.stmt_exprs < 0)
1398 label_vars->label_bindings.left_stmt_expr = true;
1399 label_vars->label_bindings.stmt_exprs = 0;
1401 for (ix = 0;
1402 VEC_iterate (c_goto_bindings_p, label_vars->gotos, ix, g);
1403 ++ix)
1405 --g->goto_bindings.stmt_exprs;
1406 if (g->goto_bindings.stmt_exprs < 0)
1408 g->goto_bindings.left_stmt_expr = true;
1409 g->goto_bindings.stmt_exprs = 0;
1415 if (switch_bindings != NULL)
1417 --switch_bindings->stmt_exprs;
1418 gcc_assert (switch_bindings->stmt_exprs >= 0);
1422 /* Push a definition or a declaration of struct, union or enum tag "name".
1423 "type" should be the type node.
1424 We assume that the tag "name" is not already defined, and has a location
1425 of LOC.
1427 Note that the definition may really be just a forward reference.
1428 In that case, the TYPE_SIZE will be zero. */
1430 static void
1431 pushtag (location_t loc, tree name, tree type)
1433 /* Record the identifier as the type's name if it has none. */
1434 if (name && !TYPE_NAME (type))
1435 TYPE_NAME (type) = name;
1436 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1438 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1439 tagged type we just added to the current scope. This fake
1440 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1441 to output a representation of a tagged type, and it also gives
1442 us a convenient place to record the "scope start" address for the
1443 tagged type. */
1445 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1446 TYPE_DECL, NULL_TREE, type));
1448 /* An approximation for now, so we can tell this is a function-scope tag.
1449 This will be updated in pop_scope. */
1450 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1452 if (warn_cxx_compat && name != NULL_TREE)
1454 struct c_binding *b = I_SYMBOL_BINDING (name);
1456 if (b != NULL
1457 && b->decl != NULL_TREE
1458 && TREE_CODE (b->decl) == TYPE_DECL
1459 && (B_IN_CURRENT_SCOPE (b)
1460 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1461 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1462 != TYPE_MAIN_VARIANT (type)))
1464 warning_at (loc, OPT_Wc___compat,
1465 ("using %qD as both a typedef and a tag is "
1466 "invalid in C++"),
1467 b->decl);
1468 if (b->locus != UNKNOWN_LOCATION)
1469 inform (b->locus, "originally defined here");
1474 /* Subroutine of compare_decls. Allow harmless mismatches in return
1475 and argument types provided that the type modes match. This function
1476 return a unified type given a suitable match, and 0 otherwise. */
1478 static tree
1479 match_builtin_function_types (tree newtype, tree oldtype)
1481 tree newrettype, oldrettype;
1482 tree newargs, oldargs;
1483 tree trytype, tryargs;
1485 /* Accept the return type of the new declaration if same modes. */
1486 oldrettype = TREE_TYPE (oldtype);
1487 newrettype = TREE_TYPE (newtype);
1489 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1490 return 0;
1492 oldargs = TYPE_ARG_TYPES (oldtype);
1493 newargs = TYPE_ARG_TYPES (newtype);
1494 tryargs = newargs;
1496 while (oldargs || newargs)
1498 if (!oldargs
1499 || !newargs
1500 || !TREE_VALUE (oldargs)
1501 || !TREE_VALUE (newargs)
1502 || TYPE_MODE (TREE_VALUE (oldargs))
1503 != TYPE_MODE (TREE_VALUE (newargs)))
1504 return 0;
1506 oldargs = TREE_CHAIN (oldargs);
1507 newargs = TREE_CHAIN (newargs);
1510 trytype = build_function_type (newrettype, tryargs);
1511 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1514 /* Subroutine of diagnose_mismatched_decls. Check for function type
1515 mismatch involving an empty arglist vs a nonempty one and give clearer
1516 diagnostics. */
1517 static void
1518 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1519 tree newtype, tree oldtype)
1521 tree t;
1523 if (TREE_CODE (olddecl) != FUNCTION_DECL
1524 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1525 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1527 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1528 return;
1530 t = TYPE_ARG_TYPES (oldtype);
1531 if (t == 0)
1532 t = TYPE_ARG_TYPES (newtype);
1533 for (; t; t = TREE_CHAIN (t))
1535 tree type = TREE_VALUE (t);
1537 if (TREE_CHAIN (t) == 0
1538 && TYPE_MAIN_VARIANT (type) != void_type_node)
1540 inform (input_location, "a parameter list with an ellipsis can%'t match "
1541 "an empty parameter name list declaration");
1542 break;
1545 if (c_type_promotes_to (type) != type)
1547 inform (input_location, "an argument type that has a default promotion can%'t match "
1548 "an empty parameter name list declaration");
1549 break;
1554 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1555 old-style function definition, NEWDECL is a prototype declaration.
1556 Diagnose inconsistencies in the argument list. Returns TRUE if
1557 the prototype is compatible, FALSE if not. */
1558 static bool
1559 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1561 tree newargs, oldargs;
1562 int i;
1564 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1566 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1567 newargs = TYPE_ARG_TYPES (newtype);
1568 i = 1;
1570 for (;;)
1572 tree oldargtype = TREE_VALUE (oldargs);
1573 tree newargtype = TREE_VALUE (newargs);
1575 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1576 return false;
1578 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1579 newargtype = TYPE_MAIN_VARIANT (newargtype);
1581 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1582 break;
1584 /* Reaching the end of just one list means the two decls don't
1585 agree on the number of arguments. */
1586 if (END_OF_ARGLIST (oldargtype))
1588 error ("prototype for %q+D declares more arguments "
1589 "than previous old-style definition", newdecl);
1590 return false;
1592 else if (END_OF_ARGLIST (newargtype))
1594 error ("prototype for %q+D declares fewer arguments "
1595 "than previous old-style definition", newdecl);
1596 return false;
1599 /* Type for passing arg must be consistent with that declared
1600 for the arg. */
1601 else if (!comptypes (oldargtype, newargtype))
1603 error ("prototype for %q+D declares argument %d"
1604 " with incompatible type",
1605 newdecl, i);
1606 return false;
1609 oldargs = TREE_CHAIN (oldargs);
1610 newargs = TREE_CHAIN (newargs);
1611 i++;
1614 /* If we get here, no errors were found, but do issue a warning
1615 for this poor-style construct. */
1616 warning (0, "prototype for %q+D follows non-prototype definition",
1617 newdecl);
1618 return true;
1619 #undef END_OF_ARGLIST
1622 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1623 first in a pair of mismatched declarations, using the diagnostic
1624 function DIAG. */
1625 static void
1626 locate_old_decl (tree decl)
1628 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1630 else if (DECL_INITIAL (decl))
1631 inform (input_location, "previous definition of %q+D was here", decl);
1632 else if (C_DECL_IMPLICIT (decl))
1633 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1634 else
1635 inform (input_location, "previous declaration of %q+D was here", decl);
1638 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1639 Returns true if the caller should proceed to merge the two, false
1640 if OLDDECL should simply be discarded. As a side effect, issues
1641 all necessary diagnostics for invalid or poor-style combinations.
1642 If it returns true, writes the types of NEWDECL and OLDDECL to
1643 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1644 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1646 static bool
1647 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1648 tree *newtypep, tree *oldtypep)
1650 tree newtype, oldtype;
1651 bool pedwarned = false;
1652 bool warned = false;
1653 bool retval = true;
1655 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1656 && DECL_EXTERNAL (DECL))
1658 /* If we have error_mark_node for either decl or type, just discard
1659 the previous decl - we're in an error cascade already. */
1660 if (olddecl == error_mark_node || newdecl == error_mark_node)
1661 return false;
1662 *oldtypep = oldtype = TREE_TYPE (olddecl);
1663 *newtypep = newtype = TREE_TYPE (newdecl);
1664 if (oldtype == error_mark_node || newtype == error_mark_node)
1665 return false;
1667 /* Two different categories of symbol altogether. This is an error
1668 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1669 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1671 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1672 && DECL_BUILT_IN (olddecl)
1673 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1675 error ("%q+D redeclared as different kind of symbol", newdecl);
1676 locate_old_decl (olddecl);
1678 else if (TREE_PUBLIC (newdecl))
1679 warning (0, "built-in function %q+D declared as non-function",
1680 newdecl);
1681 else
1682 warning (OPT_Wshadow, "declaration of %q+D shadows "
1683 "a built-in function", newdecl);
1684 return false;
1687 /* Enumerators have no linkage, so may only be declared once in a
1688 given scope. */
1689 if (TREE_CODE (olddecl) == CONST_DECL)
1691 error ("redeclaration of enumerator %q+D", newdecl);
1692 locate_old_decl (olddecl);
1693 return false;
1696 if (!comptypes (oldtype, newtype))
1698 if (TREE_CODE (olddecl) == FUNCTION_DECL
1699 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1701 /* Accept harmless mismatch in function types.
1702 This is for the ffs and fprintf builtins. */
1703 tree trytype = match_builtin_function_types (newtype, oldtype);
1705 if (trytype && comptypes (newtype, trytype))
1706 *oldtypep = oldtype = trytype;
1707 else
1709 /* If types don't match for a built-in, throw away the
1710 built-in. No point in calling locate_old_decl here, it
1711 won't print anything. */
1712 warning (0, "conflicting types for built-in function %q+D",
1713 newdecl);
1714 return false;
1717 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1718 && DECL_IS_BUILTIN (olddecl))
1720 /* A conflicting function declaration for a predeclared
1721 function that isn't actually built in. Objective C uses
1722 these. The new declaration silently overrides everything
1723 but the volatility (i.e. noreturn) indication. See also
1724 below. FIXME: Make Objective C use normal builtins. */
1725 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1726 return false;
1728 /* Permit void foo (...) to match int foo (...) if the latter is
1729 the definition and implicit int was used. See
1730 c-torture/compile/920625-2.c. */
1731 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1732 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1733 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1734 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1736 pedwarned = pedwarn (input_location, 0,
1737 "conflicting types for %q+D", newdecl);
1738 /* Make sure we keep void as the return type. */
1739 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1740 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1742 /* Permit void foo (...) to match an earlier call to foo (...) with
1743 no declared type (thus, implicitly int). */
1744 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1745 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1746 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1747 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1749 pedwarned = pedwarn (input_location, 0,
1750 "conflicting types for %q+D", newdecl);
1751 /* Make sure we keep void as the return type. */
1752 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1754 else
1756 int new_quals = TYPE_QUALS (newtype);
1757 int old_quals = TYPE_QUALS (oldtype);
1759 if (new_quals != old_quals)
1761 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1762 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1763 if (new_addr != old_addr)
1765 if (ADDR_SPACE_GENERIC_P (new_addr))
1766 error ("conflicting named address spaces (generic vs %s) "
1767 "for %q+D",
1768 c_addr_space_name (old_addr), newdecl);
1769 else if (ADDR_SPACE_GENERIC_P (old_addr))
1770 error ("conflicting named address spaces (%s vs generic) "
1771 "for %q+D",
1772 c_addr_space_name (new_addr), newdecl);
1773 else
1774 error ("conflicting named address spaces (%s vs %s) "
1775 "for %q+D",
1776 c_addr_space_name (new_addr),
1777 c_addr_space_name (old_addr),
1778 newdecl);
1781 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1782 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1783 error ("conflicting type qualifiers for %q+D", newdecl);
1785 else
1786 error ("conflicting types for %q+D", newdecl);
1787 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1788 locate_old_decl (olddecl);
1789 return false;
1793 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1794 but silently ignore the redeclaration if either is in a system
1795 header. (Conflicting redeclarations were handled above.) */
1796 if (TREE_CODE (newdecl) == TYPE_DECL)
1798 if (DECL_IN_SYSTEM_HEADER (newdecl)
1799 || DECL_IN_SYSTEM_HEADER (olddecl)
1800 || TREE_NO_WARNING (newdecl)
1801 || TREE_NO_WARNING (olddecl))
1802 return true; /* Allow OLDDECL to continue in use. */
1804 error ("redefinition of typedef %q+D", newdecl);
1805 locate_old_decl (olddecl);
1806 return false;
1809 /* Function declarations can either be 'static' or 'extern' (no
1810 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1811 can never conflict with each other on account of linkage
1812 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1813 gnu89 mode permits two definitions if one is 'extern inline' and
1814 one is not. The non- extern-inline definition supersedes the
1815 extern-inline definition. */
1817 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1819 /* If you declare a built-in function name as static, or
1820 define the built-in with an old-style definition (so we
1821 can't validate the argument list) the built-in definition is
1822 overridden, but optionally warn this was a bad choice of name. */
1823 if (DECL_BUILT_IN (olddecl)
1824 && !C_DECL_DECLARED_BUILTIN (olddecl)
1825 && (!TREE_PUBLIC (newdecl)
1826 || (DECL_INITIAL (newdecl)
1827 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1829 warning (OPT_Wshadow, "declaration of %q+D shadows "
1830 "a built-in function", newdecl);
1831 /* Discard the old built-in function. */
1832 return false;
1835 if (DECL_INITIAL (newdecl))
1837 if (DECL_INITIAL (olddecl))
1839 /* If both decls are in the same TU and the new declaration
1840 isn't overriding an extern inline reject the new decl.
1841 In c99, no overriding is allowed in the same translation
1842 unit. */
1843 if ((!DECL_EXTERN_INLINE (olddecl)
1844 || DECL_EXTERN_INLINE (newdecl)
1845 || (!flag_gnu89_inline
1846 && (!DECL_DECLARED_INLINE_P (olddecl)
1847 || !lookup_attribute ("gnu_inline",
1848 DECL_ATTRIBUTES (olddecl)))
1849 && (!DECL_DECLARED_INLINE_P (newdecl)
1850 || !lookup_attribute ("gnu_inline",
1851 DECL_ATTRIBUTES (newdecl))))
1853 && same_translation_unit_p (newdecl, olddecl))
1855 error ("redefinition of %q+D", newdecl);
1856 locate_old_decl (olddecl);
1857 return false;
1861 /* If we have a prototype after an old-style function definition,
1862 the argument types must be checked specially. */
1863 else if (DECL_INITIAL (olddecl)
1864 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1865 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1866 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1868 locate_old_decl (olddecl);
1869 return false;
1871 /* A non-static declaration (even an "extern") followed by a
1872 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1873 The same is true for a static forward declaration at block
1874 scope followed by a non-static declaration/definition at file
1875 scope. Static followed by non-static at the same scope is
1876 not undefined behavior, and is the most convenient way to get
1877 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1878 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1879 we do diagnose it if -Wtraditional. */
1880 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1882 /* Two exceptions to the rule. If olddecl is an extern
1883 inline, or a predeclared function that isn't actually
1884 built in, newdecl silently overrides olddecl. The latter
1885 occur only in Objective C; see also above. (FIXME: Make
1886 Objective C use normal builtins.) */
1887 if (!DECL_IS_BUILTIN (olddecl)
1888 && !DECL_EXTERN_INLINE (olddecl))
1890 error ("static declaration of %q+D follows "
1891 "non-static declaration", newdecl);
1892 locate_old_decl (olddecl);
1894 return false;
1896 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1898 if (DECL_CONTEXT (olddecl))
1900 error ("non-static declaration of %q+D follows "
1901 "static declaration", newdecl);
1902 locate_old_decl (olddecl);
1903 return false;
1905 else if (warn_traditional)
1907 warned |= warning (OPT_Wtraditional,
1908 "non-static declaration of %q+D "
1909 "follows static declaration", newdecl);
1913 /* Make sure gnu_inline attribute is either not present, or
1914 present on all inline decls. */
1915 if (DECL_DECLARED_INLINE_P (olddecl)
1916 && DECL_DECLARED_INLINE_P (newdecl))
1918 bool newa = lookup_attribute ("gnu_inline",
1919 DECL_ATTRIBUTES (newdecl)) != NULL;
1920 bool olda = lookup_attribute ("gnu_inline",
1921 DECL_ATTRIBUTES (olddecl)) != NULL;
1922 if (newa != olda)
1924 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
1925 newa ? newdecl : olddecl);
1926 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
1927 "but not here");
1931 else if (TREE_CODE (newdecl) == VAR_DECL)
1933 /* Only variables can be thread-local, and all declarations must
1934 agree on this property. */
1935 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1937 /* Nothing to check. Since OLDDECL is marked threadprivate
1938 and NEWDECL does not have a thread-local attribute, we
1939 will merge the threadprivate attribute into NEWDECL. */
1942 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1944 if (DECL_THREAD_LOCAL_P (newdecl))
1945 error ("thread-local declaration of %q+D follows "
1946 "non-thread-local declaration", newdecl);
1947 else
1948 error ("non-thread-local declaration of %q+D follows "
1949 "thread-local declaration", newdecl);
1951 locate_old_decl (olddecl);
1952 return false;
1955 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1956 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1958 error ("redefinition of %q+D", newdecl);
1959 locate_old_decl (olddecl);
1960 return false;
1963 /* Objects declared at file scope: if the first declaration had
1964 external linkage (even if it was an external reference) the
1965 second must have external linkage as well, or the behavior is
1966 undefined. If the first declaration had internal linkage, then
1967 the second must too, or else be an external reference (in which
1968 case the composite declaration still has internal linkage).
1969 As for function declarations, we warn about the static-then-
1970 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1971 if (DECL_FILE_SCOPE_P (newdecl)
1972 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1974 if (DECL_EXTERNAL (newdecl))
1976 if (!DECL_FILE_SCOPE_P (olddecl))
1978 error ("extern declaration of %q+D follows "
1979 "declaration with no linkage", newdecl);
1980 locate_old_decl (olddecl);
1981 return false;
1983 else if (warn_traditional)
1985 warned |= warning (OPT_Wtraditional,
1986 "non-static declaration of %q+D "
1987 "follows static declaration", newdecl);
1990 else
1992 if (TREE_PUBLIC (newdecl))
1993 error ("non-static declaration of %q+D follows "
1994 "static declaration", newdecl);
1995 else
1996 error ("static declaration of %q+D follows "
1997 "non-static declaration", newdecl);
1999 locate_old_decl (olddecl);
2000 return false;
2003 /* Two objects with the same name declared at the same block
2004 scope must both be external references (6.7p3). */
2005 else if (!DECL_FILE_SCOPE_P (newdecl))
2007 if (DECL_EXTERNAL (newdecl))
2009 /* Extern with initializer at block scope, which will
2010 already have received an error. */
2012 else if (DECL_EXTERNAL (olddecl))
2014 error ("declaration of %q+D with no linkage follows "
2015 "extern declaration", newdecl);
2016 locate_old_decl (olddecl);
2018 else
2020 error ("redeclaration of %q+D with no linkage", newdecl);
2021 locate_old_decl (olddecl);
2024 return false;
2027 /* C++ does not permit a decl to appear multiple times at file
2028 scope. */
2029 if (warn_cxx_compat
2030 && DECL_FILE_SCOPE_P (newdecl)
2031 && !DECL_EXTERNAL (newdecl)
2032 && !DECL_EXTERNAL (olddecl))
2033 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2034 OPT_Wc___compat,
2035 ("duplicate declaration of %qD is "
2036 "invalid in C++"),
2037 newdecl);
2040 /* warnings */
2041 /* All decls must agree on a visibility. */
2042 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2043 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2044 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2046 warned |= warning (0, "redeclaration of %q+D with different visibility "
2047 "(old visibility preserved)", newdecl);
2050 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2052 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2053 if (DECL_DECLARED_INLINE_P (newdecl)
2054 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2056 warned |= warning (OPT_Wattributes,
2057 "inline declaration of %qD follows "
2058 "declaration with attribute noinline", newdecl);
2060 else if (DECL_DECLARED_INLINE_P (olddecl)
2061 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2063 warned |= warning (OPT_Wattributes,
2064 "declaration of %q+D with attribute "
2065 "noinline follows inline declaration ", newdecl);
2068 else /* PARM_DECL, VAR_DECL */
2070 /* Redeclaration of a parameter is a constraint violation (this is
2071 not explicitly stated, but follows from C99 6.7p3 [no more than
2072 one declaration of the same identifier with no linkage in the
2073 same scope, except type tags] and 6.2.2p6 [parameters have no
2074 linkage]). We must check for a forward parameter declaration,
2075 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2076 an extension, the mandatory diagnostic for which is handled by
2077 mark_forward_parm_decls. */
2079 if (TREE_CODE (newdecl) == PARM_DECL
2080 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2082 error ("redefinition of parameter %q+D", newdecl);
2083 locate_old_decl (olddecl);
2084 return false;
2088 /* Optional warning for completely redundant decls. */
2089 if (!warned && !pedwarned
2090 && warn_redundant_decls
2091 /* Don't warn about a function declaration followed by a
2092 definition. */
2093 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2094 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2095 /* Don't warn about redundant redeclarations of builtins. */
2096 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2097 && !DECL_BUILT_IN (newdecl)
2098 && DECL_BUILT_IN (olddecl)
2099 && !C_DECL_DECLARED_BUILTIN (olddecl))
2100 /* Don't warn about an extern followed by a definition. */
2101 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2102 /* Don't warn about forward parameter decls. */
2103 && !(TREE_CODE (newdecl) == PARM_DECL
2104 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2105 /* Don't warn about a variable definition following a declaration. */
2106 && !(TREE_CODE (newdecl) == VAR_DECL
2107 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2109 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2110 newdecl);
2113 /* Report location of previous decl/defn. */
2114 if (warned || pedwarned)
2115 locate_old_decl (olddecl);
2117 #undef DECL_EXTERN_INLINE
2119 return retval;
2122 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2123 consistent with OLDDECL, but carries new information. Merge the
2124 new information into OLDDECL. This function issues no
2125 diagnostics. */
2127 static void
2128 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2130 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2131 && DECL_INITIAL (newdecl) != 0);
2132 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2133 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
2134 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2135 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
2136 bool extern_changed = false;
2138 /* For real parm decl following a forward decl, rechain the old decl
2139 in its new location and clear TREE_ASM_WRITTEN (it's not a
2140 forward decl anymore). */
2141 if (TREE_CODE (newdecl) == PARM_DECL
2142 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2144 struct c_binding *b, **here;
2146 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2147 if ((*here)->decl == olddecl)
2148 goto found;
2149 gcc_unreachable ();
2151 found:
2152 b = *here;
2153 *here = b->prev;
2154 b->prev = current_scope->bindings;
2155 current_scope->bindings = b;
2157 TREE_ASM_WRITTEN (olddecl) = 0;
2160 DECL_ATTRIBUTES (newdecl)
2161 = targetm.merge_decl_attributes (olddecl, newdecl);
2163 /* Merge the data types specified in the two decls. */
2164 TREE_TYPE (newdecl)
2165 = TREE_TYPE (olddecl)
2166 = composite_type (newtype, oldtype);
2168 /* Lay the type out, unless already done. */
2169 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2171 if (TREE_TYPE (newdecl) != error_mark_node)
2172 layout_type (TREE_TYPE (newdecl));
2173 if (TREE_CODE (newdecl) != FUNCTION_DECL
2174 && TREE_CODE (newdecl) != TYPE_DECL
2175 && TREE_CODE (newdecl) != CONST_DECL)
2176 layout_decl (newdecl, 0);
2178 else
2180 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2181 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2182 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2183 DECL_MODE (newdecl) = DECL_MODE (olddecl);
2184 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2186 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2187 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2191 /* Keep the old rtl since we can safely use it. */
2192 if (HAS_RTL_P (olddecl))
2193 COPY_DECL_RTL (olddecl, newdecl);
2195 /* Merge the type qualifiers. */
2196 if (TREE_READONLY (newdecl))
2197 TREE_READONLY (olddecl) = 1;
2199 if (TREE_THIS_VOLATILE (newdecl))
2200 TREE_THIS_VOLATILE (olddecl) = 1;
2202 /* Merge deprecatedness. */
2203 if (TREE_DEPRECATED (newdecl))
2204 TREE_DEPRECATED (olddecl) = 1;
2206 /* If a decl is in a system header and the other isn't, keep the one on the
2207 system header. Otherwise, keep source location of definition rather than
2208 declaration and of prototype rather than non-prototype unless that
2209 prototype is built-in. */
2210 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2211 && DECL_IN_SYSTEM_HEADER (olddecl)
2212 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2213 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2214 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2215 && DECL_IN_SYSTEM_HEADER (newdecl)
2216 && !DECL_IN_SYSTEM_HEADER (olddecl))
2217 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2218 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
2219 || (old_is_prototype && !new_is_prototype
2220 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2221 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2223 /* Merge the initialization information. */
2224 if (DECL_INITIAL (newdecl) == 0)
2225 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2227 /* Merge the threadprivate attribute. */
2228 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
2230 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
2231 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2234 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2236 /* Merge the section attribute.
2237 We want to issue an error if the sections conflict but that
2238 must be done later in decl_attributes since we are called
2239 before attributes are assigned. */
2240 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
2241 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
2243 /* Copy the assembler name.
2244 Currently, it can only be defined in the prototype. */
2245 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2247 /* Use visibility of whichever declaration had it specified */
2248 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2250 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2251 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2254 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2256 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2257 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2258 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2259 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2260 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2261 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2262 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2263 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2264 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2265 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2266 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2269 /* Merge the storage class information. */
2270 merge_weak (newdecl, olddecl);
2272 /* For functions, static overrides non-static. */
2273 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2275 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2276 /* This is since we don't automatically
2277 copy the attributes of NEWDECL into OLDDECL. */
2278 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2279 /* If this clears `static', clear it in the identifier too. */
2280 if (!TREE_PUBLIC (olddecl))
2281 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2285 /* In c99, 'extern' declaration before (or after) 'inline' means this
2286 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2287 is present. */
2288 if (TREE_CODE (newdecl) == FUNCTION_DECL
2289 && !flag_gnu89_inline
2290 && (DECL_DECLARED_INLINE_P (newdecl)
2291 || DECL_DECLARED_INLINE_P (olddecl))
2292 && (!DECL_DECLARED_INLINE_P (newdecl)
2293 || !DECL_DECLARED_INLINE_P (olddecl)
2294 || !DECL_EXTERNAL (olddecl))
2295 && DECL_EXTERNAL (newdecl)
2296 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2297 && !current_function_decl)
2298 DECL_EXTERNAL (newdecl) = 0;
2300 if (DECL_EXTERNAL (newdecl))
2302 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2303 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2305 /* An extern decl does not override previous storage class. */
2306 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2307 if (!DECL_EXTERNAL (newdecl))
2309 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2310 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2313 else
2315 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2316 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2319 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2321 /* If we're redefining a function previously defined as extern
2322 inline, make sure we emit debug info for the inline before we
2323 throw it away, in case it was inlined into a function that
2324 hasn't been written out yet. */
2325 if (new_is_definition && DECL_INITIAL (olddecl))
2326 /* The new defn must not be inline. */
2327 DECL_UNINLINABLE (newdecl) = 1;
2328 else
2330 /* If either decl says `inline', this fn is inline, unless
2331 its definition was passed already. */
2332 if (DECL_DECLARED_INLINE_P (newdecl)
2333 || DECL_DECLARED_INLINE_P (olddecl))
2334 DECL_DECLARED_INLINE_P (newdecl) = 1;
2336 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2337 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2339 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2340 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2341 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2342 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2345 if (DECL_BUILT_IN (olddecl))
2347 /* If redeclaring a builtin function, it stays built in.
2348 But it gets tagged as having been declared. */
2349 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2350 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2351 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2352 if (new_is_prototype)
2353 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2354 else
2355 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2356 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2359 /* Preserve function specific target and optimization options */
2360 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2361 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2362 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2363 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2365 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2366 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2367 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2368 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2370 /* Also preserve various other info from the definition. */
2371 if (!new_is_definition)
2373 tree t;
2374 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2375 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2376 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2377 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2378 gimple_set_body (newdecl, gimple_body (olddecl));
2379 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2380 for (t = DECL_ARGUMENTS (newdecl); t ; t = TREE_CHAIN (t))
2381 DECL_CONTEXT (t) = newdecl;
2383 /* See if we've got a function to instantiate from. */
2384 if (DECL_SAVED_TREE (olddecl))
2385 DECL_ABSTRACT_ORIGIN (newdecl)
2386 = DECL_ABSTRACT_ORIGIN (olddecl);
2388 else if (DECL_IS_IFUNC (olddecl)
2389 && !DECL_IS_IFUNC (newdecl))
2391 /* The IFUNC information must be on definition since it
2392 may change the function return type inside the function
2393 body. */
2394 error ("definition of function %q+D conflicts with",
2395 newdecl);
2396 error ("previous declaration of indirect function %q+D here",
2397 olddecl);
2400 /* Merge the IFUNC information. */
2401 if (DECL_IS_IFUNC (olddecl))
2402 DECL_IS_IFUNC (newdecl) = 1;
2403 else if (DECL_IS_IFUNC (newdecl))
2404 DECL_IS_IFUNC (olddecl) = 1;
2407 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
2409 /* Merge the USED information. */
2410 if (TREE_USED (olddecl))
2411 TREE_USED (newdecl) = 1;
2412 else if (TREE_USED (newdecl))
2413 TREE_USED (olddecl) = 1;
2414 if (TREE_CODE (olddecl) == VAR_DECL || TREE_CODE (olddecl) == PARM_DECL)
2415 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2416 if (DECL_PRESERVE_P (olddecl))
2417 DECL_PRESERVE_P (newdecl) = 1;
2418 else if (DECL_PRESERVE_P (newdecl))
2419 DECL_PRESERVE_P (olddecl) = 1;
2421 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2422 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2423 DECL_ARGUMENTS (if appropriate). */
2425 unsigned olddecl_uid = DECL_UID (olddecl);
2426 tree olddecl_context = DECL_CONTEXT (olddecl);
2427 tree olddecl_arguments = NULL;
2428 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2429 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2431 memcpy ((char *) olddecl + sizeof (struct tree_common),
2432 (char *) newdecl + sizeof (struct tree_common),
2433 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2434 switch (TREE_CODE (olddecl))
2436 case FUNCTION_DECL:
2437 gimple_set_body (olddecl, gimple_body (newdecl));
2438 /* fall through */
2440 case FIELD_DECL:
2441 case VAR_DECL:
2442 case PARM_DECL:
2443 case LABEL_DECL:
2444 case RESULT_DECL:
2445 case CONST_DECL:
2446 case TYPE_DECL:
2447 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2448 (char *) newdecl + sizeof (struct tree_decl_common),
2449 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2450 break;
2452 default:
2454 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2455 (char *) newdecl + sizeof (struct tree_decl_common),
2456 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2458 DECL_UID (olddecl) = olddecl_uid;
2459 DECL_CONTEXT (olddecl) = olddecl_context;
2460 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2461 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2464 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2465 so that encode_section_info has a chance to look at the new decl
2466 flags and attributes. */
2467 if (DECL_RTL_SET_P (olddecl)
2468 && (TREE_CODE (olddecl) == FUNCTION_DECL
2469 || (TREE_CODE (olddecl) == VAR_DECL
2470 && TREE_STATIC (olddecl))))
2471 make_decl_rtl (olddecl);
2473 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
2474 and the definition is coming from the old version, cgraph needs
2475 to be called again. */
2476 if (extern_changed && !new_is_definition
2477 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
2478 cgraph_mark_if_needed (olddecl);
2481 /* Handle when a new declaration NEWDECL has the same name as an old
2482 one OLDDECL in the same binding contour. Prints an error message
2483 if appropriate.
2485 If safely possible, alter OLDDECL to look like NEWDECL, and return
2486 true. Otherwise, return false. */
2488 static bool
2489 duplicate_decls (tree newdecl, tree olddecl)
2491 tree newtype = NULL, oldtype = NULL;
2493 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2495 /* Avoid `unused variable' and other warnings for OLDDECL. */
2496 TREE_NO_WARNING (olddecl) = 1;
2497 return false;
2500 merge_decls (newdecl, olddecl, newtype, oldtype);
2501 return true;
2505 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2506 static void
2507 warn_if_shadowing (tree new_decl)
2509 struct c_binding *b;
2511 /* Shadow warnings wanted? */
2512 if (!warn_shadow
2513 /* No shadow warnings for internally generated vars. */
2514 || DECL_IS_BUILTIN (new_decl)
2515 /* No shadow warnings for vars made for inlining. */
2516 || DECL_FROM_INLINE (new_decl))
2517 return;
2519 /* Is anything being shadowed? Invisible decls do not count. */
2520 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2521 if (b->decl && b->decl != new_decl && !b->invisible)
2523 tree old_decl = b->decl;
2525 if (old_decl == error_mark_node)
2527 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2528 "non-variable", new_decl);
2529 break;
2531 else if (TREE_CODE (old_decl) == PARM_DECL)
2532 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2533 new_decl);
2534 else if (DECL_FILE_SCOPE_P (old_decl))
2535 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2536 "declaration", new_decl);
2537 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2538 && DECL_BUILT_IN (old_decl))
2540 warning (OPT_Wshadow, "declaration of %q+D shadows "
2541 "a built-in function", new_decl);
2542 break;
2544 else
2545 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2546 new_decl);
2548 warning_at (DECL_SOURCE_LOCATION (old_decl), OPT_Wshadow,
2549 "shadowed declaration is here");
2551 break;
2555 /* Record a decl-node X as belonging to the current lexical scope.
2556 Check for errors (such as an incompatible declaration for the same
2557 name already seen in the same scope).
2559 Returns either X or an old decl for the same name.
2560 If an old decl is returned, it may have been smashed
2561 to agree with what X says. */
2563 tree
2564 pushdecl (tree x)
2566 tree name = DECL_NAME (x);
2567 struct c_scope *scope = current_scope;
2568 struct c_binding *b;
2569 bool nested = false;
2570 location_t locus = DECL_SOURCE_LOCATION (x);
2572 /* Must set DECL_CONTEXT for everything not at file scope or
2573 DECL_FILE_SCOPE_P won't work. Local externs don't count
2574 unless they have initializers (which generate code). */
2575 if (current_function_decl
2576 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2577 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2578 DECL_CONTEXT (x) = current_function_decl;
2580 /* Anonymous decls are just inserted in the scope. */
2581 if (!name)
2583 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2584 locus);
2585 return x;
2588 /* First, see if there is another declaration with the same name in
2589 the current scope. If there is, duplicate_decls may do all the
2590 work for us. If duplicate_decls returns false, that indicates
2591 two incompatible decls in the same scope; we are to silently
2592 replace the old one (duplicate_decls has issued all appropriate
2593 diagnostics). In particular, we should not consider possible
2594 duplicates in the external scope, or shadowing. */
2595 b = I_SYMBOL_BINDING (name);
2596 if (b && B_IN_SCOPE (b, scope))
2598 struct c_binding *b_ext, *b_use;
2599 tree type = TREE_TYPE (x);
2600 tree visdecl = b->decl;
2601 tree vistype = TREE_TYPE (visdecl);
2602 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2603 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2604 b->inner_comp = false;
2605 b_use = b;
2606 b_ext = b;
2607 /* If this is an external linkage declaration, we should check
2608 for compatibility with the type in the external scope before
2609 setting the type at this scope based on the visible
2610 information only. */
2611 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2613 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2614 b_ext = b_ext->shadowed;
2615 if (b_ext)
2617 b_use = b_ext;
2618 if (b_use->u.type)
2619 TREE_TYPE (b_use->decl) = b_use->u.type;
2622 if (duplicate_decls (x, b_use->decl))
2624 if (b_use != b)
2626 /* Save the updated type in the external scope and
2627 restore the proper type for this scope. */
2628 tree thistype;
2629 if (comptypes (vistype, type))
2630 thistype = composite_type (vistype, type);
2631 else
2632 thistype = TREE_TYPE (b_use->decl);
2633 b_use->u.type = TREE_TYPE (b_use->decl);
2634 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2635 && DECL_BUILT_IN (b_use->decl))
2636 thistype
2637 = build_type_attribute_variant (thistype,
2638 TYPE_ATTRIBUTES
2639 (b_use->u.type));
2640 TREE_TYPE (b_use->decl) = thistype;
2642 return b_use->decl;
2644 else
2645 goto skip_external_and_shadow_checks;
2648 /* All declarations with external linkage, and all external
2649 references, go in the external scope, no matter what scope is
2650 current. However, the binding in that scope is ignored for
2651 purposes of normal name lookup. A separate binding structure is
2652 created in the requested scope; this governs the normal
2653 visibility of the symbol.
2655 The binding in the externals scope is used exclusively for
2656 detecting duplicate declarations of the same object, no matter
2657 what scope they are in; this is what we do here. (C99 6.2.7p2:
2658 All declarations that refer to the same object or function shall
2659 have compatible type; otherwise, the behavior is undefined.) */
2660 if (DECL_EXTERNAL (x) || scope == file_scope)
2662 tree type = TREE_TYPE (x);
2663 tree vistype = 0;
2664 tree visdecl = 0;
2665 bool type_saved = false;
2666 if (b && !B_IN_EXTERNAL_SCOPE (b)
2667 && (TREE_CODE (b->decl) == FUNCTION_DECL
2668 || TREE_CODE (b->decl) == VAR_DECL)
2669 && DECL_FILE_SCOPE_P (b->decl))
2671 visdecl = b->decl;
2672 vistype = TREE_TYPE (visdecl);
2674 if (scope != file_scope
2675 && !DECL_IN_SYSTEM_HEADER (x))
2676 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2678 while (b && !B_IN_EXTERNAL_SCOPE (b))
2680 /* If this decl might be modified, save its type. This is
2681 done here rather than when the decl is first bound
2682 because the type may change after first binding, through
2683 being completed or through attributes being added. If we
2684 encounter multiple such decls, only the first should have
2685 its type saved; the others will already have had their
2686 proper types saved and the types will not have changed as
2687 their scopes will not have been re-entered. */
2688 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2690 b->u.type = TREE_TYPE (b->decl);
2691 type_saved = true;
2693 if (B_IN_FILE_SCOPE (b)
2694 && TREE_CODE (b->decl) == VAR_DECL
2695 && TREE_STATIC (b->decl)
2696 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2697 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2698 && TREE_CODE (type) == ARRAY_TYPE
2699 && TYPE_DOMAIN (type)
2700 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2701 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2703 /* Array type completed in inner scope, which should be
2704 diagnosed if the completion does not have size 1 and
2705 it does not get completed in the file scope. */
2706 b->inner_comp = true;
2708 b = b->shadowed;
2711 /* If a matching external declaration has been found, set its
2712 type to the composite of all the types of that declaration.
2713 After the consistency checks, it will be reset to the
2714 composite of the visible types only. */
2715 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2716 && b->u.type)
2717 TREE_TYPE (b->decl) = b->u.type;
2719 /* The point of the same_translation_unit_p check here is,
2720 we want to detect a duplicate decl for a construct like
2721 foo() { extern bar(); } ... static bar(); but not if
2722 they are in different translation units. In any case,
2723 the static does not go in the externals scope. */
2724 if (b
2725 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2726 && duplicate_decls (x, b->decl))
2728 tree thistype;
2729 if (vistype)
2731 if (comptypes (vistype, type))
2732 thistype = composite_type (vistype, type);
2733 else
2734 thistype = TREE_TYPE (b->decl);
2736 else
2737 thistype = type;
2738 b->u.type = TREE_TYPE (b->decl);
2739 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2740 thistype
2741 = build_type_attribute_variant (thistype,
2742 TYPE_ATTRIBUTES (b->u.type));
2743 TREE_TYPE (b->decl) = thistype;
2744 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2745 locus);
2746 return b->decl;
2748 else if (TREE_PUBLIC (x))
2750 if (visdecl && !b && duplicate_decls (x, visdecl))
2752 /* An external declaration at block scope referring to a
2753 visible entity with internal linkage. The composite
2754 type will already be correct for this scope, so we
2755 just need to fall through to make the declaration in
2756 this scope. */
2757 nested = true;
2758 x = visdecl;
2760 else
2762 bind (name, x, external_scope, /*invisible=*/true,
2763 /*nested=*/false, locus);
2764 nested = true;
2769 if (TREE_CODE (x) != PARM_DECL)
2770 warn_if_shadowing (x);
2772 skip_external_and_shadow_checks:
2773 if (TREE_CODE (x) == TYPE_DECL)
2774 set_underlying_type (x);
2776 bind (name, x, scope, /*invisible=*/false, nested, locus);
2778 /* If x's type is incomplete because it's based on a
2779 structure or union which has not yet been fully declared,
2780 attach it to that structure or union type, so we can go
2781 back and complete the variable declaration later, if the
2782 structure or union gets fully declared.
2784 If the input is erroneous, we can have error_mark in the type
2785 slot (e.g. "f(void a, ...)") - that doesn't count as an
2786 incomplete type. */
2787 if (TREE_TYPE (x) != error_mark_node
2788 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2790 tree element = TREE_TYPE (x);
2792 while (TREE_CODE (element) == ARRAY_TYPE)
2793 element = TREE_TYPE (element);
2794 element = TYPE_MAIN_VARIANT (element);
2796 if ((TREE_CODE (element) == RECORD_TYPE
2797 || TREE_CODE (element) == UNION_TYPE)
2798 && (TREE_CODE (x) != TYPE_DECL
2799 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2800 && !COMPLETE_TYPE_P (element))
2801 C_TYPE_INCOMPLETE_VARS (element)
2802 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2804 return x;
2807 /* Record X as belonging to file scope.
2808 This is used only internally by the Objective-C front end,
2809 and is limited to its needs. duplicate_decls is not called;
2810 if there is any preexisting decl for this identifier, it is an ICE. */
2812 tree
2813 pushdecl_top_level (tree x)
2815 tree name;
2816 bool nested = false;
2817 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2819 name = DECL_NAME (x);
2821 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2823 if (TREE_PUBLIC (x))
2825 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2826 UNKNOWN_LOCATION);
2827 nested = true;
2829 if (file_scope)
2830 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2832 return x;
2835 static void
2836 implicit_decl_warning (tree id, tree olddecl)
2838 if (warn_implicit_function_declaration)
2840 bool warned;
2842 if (flag_isoc99)
2843 warned = pedwarn (input_location, OPT_Wimplicit_function_declaration,
2844 "implicit declaration of function %qE", id);
2845 else
2846 warned = warning (OPT_Wimplicit_function_declaration,
2847 G_("implicit declaration of function %qE"), id);
2848 if (olddecl && warned)
2849 locate_old_decl (olddecl);
2853 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
2854 function of type int (). */
2856 tree
2857 implicitly_declare (location_t loc, tree functionid)
2859 struct c_binding *b;
2860 tree decl = 0;
2861 tree asmspec_tree;
2863 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2865 if (B_IN_SCOPE (b, external_scope))
2867 decl = b->decl;
2868 break;
2872 if (decl)
2874 if (decl == error_mark_node)
2875 return decl;
2877 /* FIXME: Objective-C has weird not-really-builtin functions
2878 which are supposed to be visible automatically. They wind up
2879 in the external scope because they're pushed before the file
2880 scope gets created. Catch this here and rebind them into the
2881 file scope. */
2882 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2884 bind (functionid, decl, file_scope,
2885 /*invisible=*/false, /*nested=*/true,
2886 DECL_SOURCE_LOCATION (decl));
2887 return decl;
2889 else
2891 tree newtype = default_function_type;
2892 if (b->u.type)
2893 TREE_TYPE (decl) = b->u.type;
2894 /* Implicit declaration of a function already declared
2895 (somehow) in a different scope, or as a built-in.
2896 If this is the first time this has happened, warn;
2897 then recycle the old declaration but with the new type. */
2898 if (!C_DECL_IMPLICIT (decl))
2900 implicit_decl_warning (functionid, decl);
2901 C_DECL_IMPLICIT (decl) = 1;
2903 if (DECL_BUILT_IN (decl))
2905 newtype = build_type_attribute_variant (newtype,
2906 TYPE_ATTRIBUTES
2907 (TREE_TYPE (decl)));
2908 if (!comptypes (newtype, TREE_TYPE (decl)))
2910 warning_at (loc, 0, "incompatible implicit declaration of "
2911 "built-in function %qD", decl);
2912 newtype = TREE_TYPE (decl);
2915 else
2917 if (!comptypes (newtype, TREE_TYPE (decl)))
2919 error_at (loc, "incompatible implicit declaration of function %qD", decl);
2920 locate_old_decl (decl);
2923 b->u.type = TREE_TYPE (decl);
2924 TREE_TYPE (decl) = newtype;
2925 bind (functionid, decl, current_scope,
2926 /*invisible=*/false, /*nested=*/true,
2927 DECL_SOURCE_LOCATION (decl));
2928 return decl;
2932 /* Not seen before. */
2933 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
2934 DECL_EXTERNAL (decl) = 1;
2935 TREE_PUBLIC (decl) = 1;
2936 C_DECL_IMPLICIT (decl) = 1;
2937 implicit_decl_warning (functionid, 0);
2938 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2939 if (asmspec_tree)
2940 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2942 /* C89 says implicit declarations are in the innermost block.
2943 So we record the decl in the standard fashion. */
2944 decl = pushdecl (decl);
2946 /* No need to call objc_check_decl here - it's a function type. */
2947 rest_of_decl_compilation (decl, 0, 0);
2949 /* Write a record describing this implicit function declaration
2950 to the prototypes file (if requested). */
2951 gen_aux_info_record (decl, 0, 1, 0);
2953 /* Possibly apply some default attributes to this implicit declaration. */
2954 decl_attributes (&decl, NULL_TREE, 0);
2956 return decl;
2959 /* Issue an error message for a reference to an undeclared variable
2960 ID, including a reference to a builtin outside of function-call
2961 context. Establish a binding of the identifier to error_mark_node
2962 in an appropriate scope, which will suppress further errors for the
2963 same identifier. The error message should be given location LOC. */
2964 void
2965 undeclared_variable (location_t loc, tree id)
2967 static bool already = false;
2968 struct c_scope *scope;
2970 if (current_function_decl == 0)
2972 error_at (loc, "%qE undeclared here (not in a function)", id);
2973 scope = current_scope;
2975 else
2977 error_at (loc, "%qE undeclared (first use in this function)", id);
2978 if (!already)
2980 inform (loc, "each undeclared identifier is reported only"
2981 " once for each function it appears in");
2982 already = true;
2985 /* If we are parsing old-style parameter decls, current_function_decl
2986 will be nonnull but current_function_scope will be null. */
2987 scope = current_function_scope ? current_function_scope : current_scope;
2989 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
2990 UNKNOWN_LOCATION);
2993 /* Subroutine of lookup_label, declare_label, define_label: construct a
2994 LABEL_DECL with all the proper frills. Also create a struct
2995 c_label_vars initialized for the current scope. */
2997 static tree
2998 make_label (location_t location, tree name, bool defining,
2999 struct c_label_vars **p_label_vars)
3001 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3002 struct c_label_vars *label_vars;
3004 DECL_CONTEXT (label) = current_function_decl;
3005 DECL_MODE (label) = VOIDmode;
3007 label_vars = GGC_NEW (struct c_label_vars);
3008 label_vars->shadowed = NULL;
3009 set_spot_bindings (&label_vars->label_bindings, defining);
3010 label_vars->decls_in_scope = make_tree_vector ();
3011 label_vars->gotos = VEC_alloc (c_goto_bindings_p, gc, 0);
3012 *p_label_vars = label_vars;
3014 return label;
3017 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3018 Create one if none exists so far for the current function.
3019 This is called when a label is used in a goto expression or
3020 has its address taken. */
3022 tree
3023 lookup_label (tree name)
3025 tree label;
3026 struct c_label_vars *label_vars;
3028 if (current_function_decl == 0)
3030 error ("label %qE referenced outside of any function", name);
3031 return 0;
3034 /* Use a label already defined or ref'd with this name, but not if
3035 it is inherited from a containing function and wasn't declared
3036 using __label__. */
3037 label = I_LABEL_DECL (name);
3038 if (label && (DECL_CONTEXT (label) == current_function_decl
3039 || C_DECLARED_LABEL_FLAG (label)))
3041 /* If the label has only been declared, update its apparent
3042 location to point here, for better diagnostics if it
3043 turns out not to have been defined. */
3044 if (DECL_INITIAL (label) == NULL_TREE)
3045 DECL_SOURCE_LOCATION (label) = input_location;
3046 return label;
3049 /* No label binding for that identifier; make one. */
3050 label = make_label (input_location, name, false, &label_vars);
3052 /* Ordinary labels go in the current function scope. */
3053 bind_label (name, label, current_function_scope, label_vars);
3055 return label;
3058 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3059 to LABEL. */
3061 static void
3062 warn_about_goto (location_t goto_loc, tree label, tree decl)
3064 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3065 error_at (goto_loc,
3066 "jump into scope of identifier with variably modified type");
3067 else
3068 warning_at (goto_loc, OPT_Wjump_misses_init,
3069 "jump skips variable initialization");
3070 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3071 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3074 /* Look up a label because of a goto statement. This is like
3075 lookup_label, but also issues any appropriate warnings. */
3077 tree
3078 lookup_label_for_goto (location_t loc, tree name)
3080 tree label;
3081 struct c_label_vars *label_vars;
3082 unsigned int ix;
3083 tree decl;
3085 label = lookup_label (name);
3086 if (label == NULL_TREE)
3087 return NULL_TREE;
3089 /* If we are jumping to a different function, we can't issue any
3090 useful warnings. */
3091 if (DECL_CONTEXT (label) != current_function_decl)
3093 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3094 return label;
3097 label_vars = I_LABEL_BINDING (name)->u.label;
3099 /* If the label has not yet been defined, then push this goto on a
3100 list for possible later warnings. */
3101 if (label_vars->label_bindings.scope == NULL)
3103 struct c_goto_bindings *g;
3105 g = GGC_NEW (struct c_goto_bindings);
3106 g->loc = loc;
3107 set_spot_bindings (&g->goto_bindings, true);
3108 VEC_safe_push (c_goto_bindings_p, gc, label_vars->gotos, g);
3109 return label;
3112 /* If there are any decls in label_vars->decls_in_scope, then this
3113 goto has missed the declaration of the decl. This happens for a
3114 case like
3115 int i = 1;
3116 lab:
3118 goto lab;
3119 Issue a warning or error. */
3120 for (ix = 0; VEC_iterate (tree, label_vars->decls_in_scope, ix, decl); ++ix)
3121 warn_about_goto (loc, label, decl);
3123 if (label_vars->label_bindings.left_stmt_expr)
3125 error_at (loc, "jump into statement expression");
3126 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3129 return label;
3132 /* Make a label named NAME in the current function, shadowing silently
3133 any that may be inherited from containing functions or containing
3134 scopes. This is called for __label__ declarations. */
3136 tree
3137 declare_label (tree name)
3139 struct c_binding *b = I_LABEL_BINDING (name);
3140 tree label;
3141 struct c_label_vars *label_vars;
3143 /* Check to make sure that the label hasn't already been declared
3144 at this scope */
3145 if (b && B_IN_CURRENT_SCOPE (b))
3147 error ("duplicate label declaration %qE", name);
3148 locate_old_decl (b->decl);
3150 /* Just use the previous declaration. */
3151 return b->decl;
3154 label = make_label (input_location, name, false, &label_vars);
3155 C_DECLARED_LABEL_FLAG (label) = 1;
3157 /* Declared labels go in the current scope. */
3158 bind_label (name, label, current_scope, label_vars);
3160 return label;
3163 /* When we define a label, issue any appropriate warnings if there are
3164 any gotos earlier in the function which jump to this label. */
3166 static void
3167 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3169 unsigned int ix;
3170 struct c_goto_bindings *g;
3172 for (ix = 0;
3173 VEC_iterate (c_goto_bindings_p, label_vars->gotos, ix, g);
3174 ++ix)
3176 struct c_binding *b;
3177 struct c_scope *scope;
3179 /* We have a goto to this label. The goto is going forward. In
3180 g->scope, the goto is going to skip any binding which was
3181 defined after g->bindings_in_scope. */
3182 for (b = g->goto_bindings.scope->bindings;
3183 b != g->goto_bindings.bindings_in_scope;
3184 b = b->prev)
3186 if (decl_jump_unsafe (b->decl))
3187 warn_about_goto (g->loc, label, b->decl);
3190 /* We also need to warn about decls defined in any scopes
3191 between the scope of the label and the scope of the goto. */
3192 for (scope = label_vars->label_bindings.scope;
3193 scope != g->goto_bindings.scope;
3194 scope = scope->outer)
3196 gcc_assert (scope != NULL);
3197 if (scope == label_vars->label_bindings.scope)
3198 b = label_vars->label_bindings.bindings_in_scope;
3199 else
3200 b = scope->bindings;
3201 for (; b != NULL; b = b->prev)
3203 if (decl_jump_unsafe (b->decl))
3204 warn_about_goto (g->loc, label, b->decl);
3208 if (g->goto_bindings.stmt_exprs > 0)
3210 error_at (g->loc, "jump into statement expression");
3211 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3212 label);
3216 /* Now that the label is defined, we will issue warnings about
3217 subsequent gotos to this label when we see them. */
3218 VEC_truncate (c_goto_bindings_p, label_vars->gotos, 0);
3219 label_vars->gotos = NULL;
3222 /* Define a label, specifying the location in the source file.
3223 Return the LABEL_DECL node for the label, if the definition is valid.
3224 Otherwise return 0. */
3226 tree
3227 define_label (location_t location, tree name)
3229 /* Find any preexisting label with this name. It is an error
3230 if that label has already been defined in this function, or
3231 if there is a containing function with a declared label with
3232 the same name. */
3233 tree label = I_LABEL_DECL (name);
3235 if (label
3236 && ((DECL_CONTEXT (label) == current_function_decl
3237 && DECL_INITIAL (label) != 0)
3238 || (DECL_CONTEXT (label) != current_function_decl
3239 && C_DECLARED_LABEL_FLAG (label))))
3241 error_at (location, "duplicate label %qD", label);
3242 locate_old_decl (label);
3243 return 0;
3245 else if (label && DECL_CONTEXT (label) == current_function_decl)
3247 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3249 /* The label has been used or declared already in this function,
3250 but not defined. Update its location to point to this
3251 definition. */
3252 DECL_SOURCE_LOCATION (label) = location;
3253 set_spot_bindings (&label_vars->label_bindings, true);
3255 /* Issue warnings as required about any goto statements from
3256 earlier in the function. */
3257 check_earlier_gotos (label, label_vars);
3259 else
3261 struct c_label_vars *label_vars;
3263 /* No label binding for that identifier; make one. */
3264 label = make_label (location, name, true, &label_vars);
3266 /* Ordinary labels go in the current function scope. */
3267 bind_label (name, label, current_function_scope, label_vars);
3270 if (!in_system_header && lookup_name (name))
3271 warning_at (location, OPT_Wtraditional,
3272 "traditional C lacks a separate namespace "
3273 "for labels, identifier %qE conflicts", name);
3275 /* Mark label as having been defined. */
3276 DECL_INITIAL (label) = error_mark_node;
3277 return label;
3280 /* Get the bindings for a new switch statement. This is used to issue
3281 warnings as appropriate for jumps from the switch to case or
3282 default labels. */
3284 struct c_spot_bindings *
3285 c_get_switch_bindings (void)
3287 struct c_spot_bindings *switch_bindings;
3289 switch_bindings = XNEW (struct c_spot_bindings);
3290 set_spot_bindings (switch_bindings, true);
3291 return switch_bindings;
3294 void
3295 c_release_switch_bindings (struct c_spot_bindings *bindings)
3297 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3298 XDELETE (bindings);
3301 /* This is called at the point of a case or default label to issue
3302 warnings about decls as needed. It returns true if it found an
3303 error, not just a warning. */
3305 bool
3306 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3307 location_t switch_loc, location_t case_loc)
3309 bool saw_error;
3310 struct c_scope *scope;
3312 saw_error = false;
3313 for (scope = current_scope;
3314 scope != switch_bindings->scope;
3315 scope = scope->outer)
3317 struct c_binding *b;
3319 gcc_assert (scope != NULL);
3320 for (b = scope->bindings; b != NULL; b = b->prev)
3322 if (decl_jump_unsafe (b->decl))
3324 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3326 saw_error = true;
3327 error_at (case_loc,
3328 ("switch jumps into scope of identifier with "
3329 "variably modified type"));
3331 else
3332 warning_at (case_loc, OPT_Wjump_misses_init,
3333 "switch jumps over variable initialization");
3334 inform (switch_loc, "switch starts here");
3335 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3336 b->decl);
3341 if (switch_bindings->stmt_exprs > 0)
3343 saw_error = true;
3344 error_at (case_loc, "switch jumps into statement expression");
3345 inform (switch_loc, "switch starts here");
3348 return saw_error;
3351 /* Given NAME, an IDENTIFIER_NODE,
3352 return the structure (or union or enum) definition for that name.
3353 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3354 CODE says which kind of type the caller wants;
3355 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3356 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3357 location where the tag was defined.
3358 If the wrong kind of type is found, an error is reported. */
3360 static tree
3361 lookup_tag (enum tree_code code, tree name, int thislevel_only,
3362 location_t *ploc)
3364 struct c_binding *b = I_TAG_BINDING (name);
3365 int thislevel = 0;
3367 if (!b || !b->decl)
3368 return 0;
3370 /* We only care about whether it's in this level if
3371 thislevel_only was set or it might be a type clash. */
3372 if (thislevel_only || TREE_CODE (b->decl) != code)
3374 /* For our purposes, a tag in the external scope is the same as
3375 a tag in the file scope. (Primarily relevant to Objective-C
3376 and its builtin structure tags, which get pushed before the
3377 file scope is created.) */
3378 if (B_IN_CURRENT_SCOPE (b)
3379 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3380 thislevel = 1;
3383 if (thislevel_only && !thislevel)
3384 return 0;
3386 if (TREE_CODE (b->decl) != code)
3388 /* Definition isn't the kind we were looking for. */
3389 pending_invalid_xref = name;
3390 pending_invalid_xref_location = input_location;
3392 /* If in the same binding level as a declaration as a tag
3393 of a different type, this must not be allowed to
3394 shadow that tag, so give the error immediately.
3395 (For example, "struct foo; union foo;" is invalid.) */
3396 if (thislevel)
3397 pending_xref_error ();
3400 if (ploc != NULL)
3401 *ploc = b->locus;
3403 return b->decl;
3406 /* Print an error message now
3407 for a recent invalid struct, union or enum cross reference.
3408 We don't print them immediately because they are not invalid
3409 when used in the `struct foo;' construct for shadowing. */
3411 void
3412 pending_xref_error (void)
3414 if (pending_invalid_xref != 0)
3415 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3416 pending_invalid_xref);
3417 pending_invalid_xref = 0;
3421 /* Look up NAME in the current scope and its superiors
3422 in the namespace of variables, functions and typedefs.
3423 Return a ..._DECL node of some kind representing its definition,
3424 or return 0 if it is undefined. */
3426 tree
3427 lookup_name (tree name)
3429 struct c_binding *b = I_SYMBOL_BINDING (name);
3430 if (b && !b->invisible)
3431 return b->decl;
3432 return 0;
3435 /* Similar to `lookup_name' but look only at the indicated scope. */
3437 static tree
3438 lookup_name_in_scope (tree name, struct c_scope *scope)
3440 struct c_binding *b;
3442 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3443 if (B_IN_SCOPE (b, scope))
3444 return b->decl;
3445 return 0;
3448 /* Create the predefined scalar types of C,
3449 and some nodes representing standard constants (0, 1, (void *) 0).
3450 Initialize the global scope.
3451 Make definitions for built-in primitive functions. */
3453 void
3454 c_init_decl_processing (void)
3456 location_t save_loc = input_location;
3458 /* Initialize reserved words for parser. */
3459 c_parse_init ();
3461 current_function_decl = 0;
3463 gcc_obstack_init (&parser_obstack);
3465 /* Make the externals scope. */
3466 push_scope ();
3467 external_scope = current_scope;
3469 /* Declarations from c_common_nodes_and_builtins must not be associated
3470 with this input file, lest we get differences between using and not
3471 using preprocessed headers. */
3472 input_location = BUILTINS_LOCATION;
3474 build_common_tree_nodes (flag_signed_char);
3476 c_common_nodes_and_builtins ();
3478 /* In C, comparisons and TRUTH_* expressions have type int. */
3479 truthvalue_type_node = integer_type_node;
3480 truthvalue_true_node = integer_one_node;
3481 truthvalue_false_node = integer_zero_node;
3483 /* Even in C99, which has a real boolean type. */
3484 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
3485 boolean_type_node));
3487 input_location = save_loc;
3489 pedantic_lvalues = true;
3491 make_fname_decl = c_make_fname_decl;
3492 start_fname_decls ();
3495 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
3496 give the decl, NAME is the initialization string and TYPE_DEP
3497 indicates whether NAME depended on the type of the function. As we
3498 don't yet implement delayed emission of static data, we mark the
3499 decl as emitted so it is not placed in the output. Anything using
3500 it must therefore pull out the STRING_CST initializer directly.
3501 FIXME. */
3503 static tree
3504 c_make_fname_decl (location_t loc, tree id, int type_dep)
3506 const char *name = fname_as_string (type_dep);
3507 tree decl, type, init;
3508 size_t length = strlen (name);
3510 type = build_array_type (char_type_node,
3511 build_index_type (size_int (length)));
3512 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
3514 decl = build_decl (loc, VAR_DECL, id, type);
3516 TREE_STATIC (decl) = 1;
3517 TREE_READONLY (decl) = 1;
3518 DECL_ARTIFICIAL (decl) = 1;
3520 init = build_string (length + 1, name);
3521 free (CONST_CAST (char *, name));
3522 TREE_TYPE (init) = type;
3523 DECL_INITIAL (decl) = init;
3525 TREE_USED (decl) = 1;
3527 if (current_function_decl
3528 /* For invalid programs like this:
3530 void foo()
3531 const char* p = __FUNCTION__;
3533 the __FUNCTION__ is believed to appear in K&R style function
3534 parameter declarator. In that case we still don't have
3535 function_scope. */
3536 && (!errorcount || current_function_scope))
3538 DECL_CONTEXT (decl) = current_function_decl;
3539 bind (id, decl, current_function_scope,
3540 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
3543 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
3545 return decl;
3548 tree
3549 c_builtin_function (tree decl)
3551 tree type = TREE_TYPE (decl);
3552 tree id = DECL_NAME (decl);
3554 const char *name = IDENTIFIER_POINTER (id);
3555 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
3557 /* Should never be called on a symbol with a preexisting meaning. */
3558 gcc_assert (!I_SYMBOL_BINDING (id));
3560 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
3561 UNKNOWN_LOCATION);
3563 /* Builtins in the implementation namespace are made visible without
3564 needing to be explicitly declared. See push_file_scope. */
3565 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3567 TREE_CHAIN (decl) = visible_builtins;
3568 visible_builtins = decl;
3571 return decl;
3574 tree
3575 c_builtin_function_ext_scope (tree decl)
3577 tree type = TREE_TYPE (decl);
3578 tree id = DECL_NAME (decl);
3580 const char *name = IDENTIFIER_POINTER (id);
3581 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
3583 /* Should never be called on a symbol with a preexisting meaning. */
3584 gcc_assert (!I_SYMBOL_BINDING (id));
3586 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
3587 UNKNOWN_LOCATION);
3589 /* Builtins in the implementation namespace are made visible without
3590 needing to be explicitly declared. See push_file_scope. */
3591 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3593 TREE_CHAIN (decl) = visible_builtins;
3594 visible_builtins = decl;
3597 return decl;
3600 /* Called when a declaration is seen that contains no names to declare.
3601 If its type is a reference to a structure, union or enum inherited
3602 from a containing scope, shadow that tag name for the current scope
3603 with a forward reference.
3604 If its type defines a new named structure or union
3605 or defines an enum, it is valid but we need not do anything here.
3606 Otherwise, it is an error. */
3608 void
3609 shadow_tag (const struct c_declspecs *declspecs)
3611 shadow_tag_warned (declspecs, 0);
3614 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
3615 but no pedwarn. */
3616 void
3617 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
3619 bool found_tag = false;
3621 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
3623 tree value = declspecs->type;
3624 enum tree_code code = TREE_CODE (value);
3626 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
3627 /* Used to test also that TYPE_SIZE (value) != 0.
3628 That caused warning for `struct foo;' at top level in the file. */
3630 tree name = TYPE_NAME (value);
3631 tree t;
3633 found_tag = true;
3635 if (declspecs->restrict_p)
3637 error ("invalid use of %<restrict%>");
3638 warned = 1;
3641 if (name == 0)
3643 if (warned != 1 && code != ENUMERAL_TYPE)
3644 /* Empty unnamed enum OK */
3646 pedwarn (input_location, 0,
3647 "unnamed struct/union that defines no instances");
3648 warned = 1;
3651 else if (!declspecs->tag_defined_p
3652 && declspecs->storage_class != csc_none)
3654 if (warned != 1)
3655 pedwarn (input_location, 0,
3656 "empty declaration with storage class specifier "
3657 "does not redeclare tag");
3658 warned = 1;
3659 pending_xref_error ();
3661 else if (!declspecs->tag_defined_p
3662 && (declspecs->const_p
3663 || declspecs->volatile_p
3664 || declspecs->restrict_p
3665 || declspecs->address_space))
3667 if (warned != 1)
3668 pedwarn (input_location, 0,
3669 "empty declaration with type qualifier "
3670 "does not redeclare tag");
3671 warned = 1;
3672 pending_xref_error ();
3674 else
3676 pending_invalid_xref = 0;
3677 t = lookup_tag (code, name, 1, NULL);
3679 if (t == 0)
3681 t = make_node (code);
3682 pushtag (input_location, name, t);
3686 else
3688 if (warned != 1 && !in_system_header)
3690 pedwarn (input_location, 0,
3691 "useless type name in empty declaration");
3692 warned = 1;
3696 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
3698 pedwarn (input_location, 0, "useless type name in empty declaration");
3699 warned = 1;
3702 pending_invalid_xref = 0;
3704 if (declspecs->inline_p)
3706 error ("%<inline%> in empty declaration");
3707 warned = 1;
3710 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3712 error ("%<auto%> in file-scope empty declaration");
3713 warned = 1;
3716 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3718 error ("%<register%> in file-scope empty declaration");
3719 warned = 1;
3722 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3724 warning (0, "useless storage class specifier in empty declaration");
3725 warned = 2;
3728 if (!warned && !in_system_header && declspecs->thread_p)
3730 warning (0, "useless %<__thread%> in empty declaration");
3731 warned = 2;
3734 if (!warned && !in_system_header && (declspecs->const_p
3735 || declspecs->volatile_p
3736 || declspecs->restrict_p
3737 || declspecs->address_space))
3739 warning (0, "useless type qualifier in empty declaration");
3740 warned = 2;
3743 if (warned != 1)
3745 if (!found_tag)
3746 pedwarn (input_location, 0, "empty declaration");
3751 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3752 bits. SPECS represents declaration specifiers that the grammar
3753 only permits to contain type qualifiers and attributes. */
3756 quals_from_declspecs (const struct c_declspecs *specs)
3758 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3759 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3760 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
3761 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
3762 gcc_assert (!specs->type
3763 && !specs->decl_attr
3764 && specs->typespec_word == cts_none
3765 && specs->storage_class == csc_none
3766 && !specs->typedef_p
3767 && !specs->explicit_signed_p
3768 && !specs->deprecated_p
3769 && !specs->long_p
3770 && !specs->long_long_p
3771 && !specs->short_p
3772 && !specs->signed_p
3773 && !specs->unsigned_p
3774 && !specs->complex_p
3775 && !specs->inline_p
3776 && !specs->thread_p);
3777 return quals;
3780 /* Construct an array declarator. LOC is the location of the
3781 beginning of the array (usually the opening brace). EXPR is the
3782 expression inside [], or NULL_TREE. QUALS are the type qualifiers
3783 inside the [] (to be applied to the pointer to which a parameter
3784 array is converted). STATIC_P is true if "static" is inside the
3785 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
3786 VLA of unspecified length which is nevertheless a complete type,
3787 false otherwise. The field for the contained declarator is left to
3788 be filled in by set_array_declarator_inner. */
3790 struct c_declarator *
3791 build_array_declarator (location_t loc,
3792 tree expr, struct c_declspecs *quals, bool static_p,
3793 bool vla_unspec_p)
3795 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3796 struct c_declarator);
3797 declarator->id_loc = loc;
3798 declarator->kind = cdk_array;
3799 declarator->declarator = 0;
3800 declarator->u.array.dimen = expr;
3801 if (quals)
3803 declarator->u.array.attrs = quals->attrs;
3804 declarator->u.array.quals = quals_from_declspecs (quals);
3806 else
3808 declarator->u.array.attrs = NULL_TREE;
3809 declarator->u.array.quals = 0;
3811 declarator->u.array.static_p = static_p;
3812 declarator->u.array.vla_unspec_p = vla_unspec_p;
3813 if (!flag_isoc99)
3815 if (static_p || quals != NULL)
3816 pedwarn (loc, OPT_pedantic,
3817 "ISO C90 does not support %<static%> or type "
3818 "qualifiers in parameter array declarators");
3819 if (vla_unspec_p)
3820 pedwarn (loc, OPT_pedantic,
3821 "ISO C90 does not support %<[*]%> array declarators");
3823 if (vla_unspec_p)
3825 if (!current_scope->parm_flag)
3827 /* C99 6.7.5.2p4 */
3828 error_at (loc, "%<[*]%> not allowed in other than "
3829 "function prototype scope");
3830 declarator->u.array.vla_unspec_p = false;
3831 return NULL;
3833 current_scope->had_vla_unspec = true;
3835 return declarator;
3838 /* Set the contained declarator of an array declarator. DECL is the
3839 declarator, as constructed by build_array_declarator; INNER is what
3840 appears on the left of the []. */
3842 struct c_declarator *
3843 set_array_declarator_inner (struct c_declarator *decl,
3844 struct c_declarator *inner)
3846 decl->declarator = inner;
3847 return decl;
3850 /* INIT is a constructor that forms DECL's initializer. If the final
3851 element initializes a flexible array field, add the size of that
3852 initializer to DECL's size. */
3854 static void
3855 add_flexible_array_elts_to_size (tree decl, tree init)
3857 tree elt, type;
3859 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3860 return;
3862 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3863 type = TREE_TYPE (elt);
3864 if (TREE_CODE (type) == ARRAY_TYPE
3865 && TYPE_SIZE (type) == NULL_TREE
3866 && TYPE_DOMAIN (type) != NULL_TREE
3867 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3869 complete_array_type (&type, elt, false);
3870 DECL_SIZE (decl)
3871 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3872 DECL_SIZE_UNIT (decl)
3873 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3877 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3878 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3879 before the type name, and set *EXPR_CONST_OPERANDS, if
3880 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3881 appear in a constant expression. */
3883 tree
3884 groktypename (struct c_type_name *type_name, tree *expr,
3885 bool *expr_const_operands)
3887 tree type;
3888 tree attrs = type_name->specs->attrs;
3890 type_name->specs->attrs = NULL_TREE;
3892 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3893 false, NULL, &attrs, expr, expr_const_operands,
3894 DEPRECATED_NORMAL);
3896 /* Apply attributes. */
3897 decl_attributes (&type, attrs, 0);
3899 return type;
3902 /* Decode a declarator in an ordinary declaration or data definition.
3903 This is called as soon as the type information and variable name
3904 have been parsed, before parsing the initializer if any.
3905 Here we create the ..._DECL node, fill in its type,
3906 and put it on the list of decls for the current context.
3907 The ..._DECL node is returned as the value.
3909 Exception: for arrays where the length is not specified,
3910 the type is left null, to be filled in by `finish_decl'.
3912 Function definitions do not come here; they go to start_function
3913 instead. However, external and forward declarations of functions
3914 do go through here. Structure field declarations are done by
3915 grokfield and not through here. */
3917 tree
3918 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3919 bool initialized, tree attributes)
3921 tree decl;
3922 tree tem;
3923 tree expr = NULL_TREE;
3924 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3926 /* An object declared as __attribute__((deprecated)) suppresses
3927 warnings of uses of other deprecated items. */
3928 if (lookup_attribute ("deprecated", attributes))
3929 deprecated_state = DEPRECATED_SUPPRESS;
3931 decl = grokdeclarator (declarator, declspecs,
3932 NORMAL, initialized, NULL, &attributes, &expr, NULL,
3933 deprecated_state);
3934 if (!decl)
3935 return 0;
3937 if (expr)
3938 add_stmt (expr);
3940 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
3941 warning (OPT_Wmain, "%q+D is usually a function", decl);
3943 if (initialized)
3944 /* Is it valid for this decl to have an initializer at all?
3945 If not, set INITIALIZED to zero, which will indirectly
3946 tell 'finish_decl' to ignore the initializer once it is parsed. */
3947 switch (TREE_CODE (decl))
3949 case TYPE_DECL:
3950 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3951 initialized = 0;
3952 break;
3954 case FUNCTION_DECL:
3955 error ("function %qD is initialized like a variable", decl);
3956 initialized = 0;
3957 break;
3959 case PARM_DECL:
3960 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3961 error ("parameter %qD is initialized", decl);
3962 initialized = 0;
3963 break;
3965 default:
3966 /* Don't allow initializations for incomplete types except for
3967 arrays which might be completed by the initialization. */
3969 /* This can happen if the array size is an undefined macro.
3970 We already gave a warning, so we don't need another one. */
3971 if (TREE_TYPE (decl) == error_mark_node)
3972 initialized = 0;
3973 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3975 /* A complete type is ok if size is fixed. */
3977 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3978 || C_DECL_VARIABLE_SIZE (decl))
3980 error ("variable-sized object may not be initialized");
3981 initialized = 0;
3984 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3986 error ("variable %qD has initializer but incomplete type", decl);
3987 initialized = 0;
3989 else if (C_DECL_VARIABLE_SIZE (decl))
3991 /* Although C99 is unclear about whether incomplete arrays
3992 of VLAs themselves count as VLAs, it does not make
3993 sense to permit them to be initialized given that
3994 ordinary VLAs may not be initialized. */
3995 error ("variable-sized object may not be initialized");
3996 initialized = 0;
4000 if (initialized)
4002 if (current_scope == file_scope)
4003 TREE_STATIC (decl) = 1;
4005 /* Tell 'pushdecl' this is an initialized decl
4006 even though we don't yet have the initializer expression.
4007 Also tell 'finish_decl' it may store the real initializer. */
4008 DECL_INITIAL (decl) = error_mark_node;
4011 /* If this is a function declaration, write a record describing it to the
4012 prototypes file (if requested). */
4014 if (TREE_CODE (decl) == FUNCTION_DECL)
4015 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
4017 /* ANSI specifies that a tentative definition which is not merged with
4018 a non-tentative definition behaves exactly like a definition with an
4019 initializer equal to zero. (Section 3.7.2)
4021 -fno-common gives strict ANSI behavior, though this tends to break
4022 a large body of code that grew up without this rule.
4024 Thread-local variables are never common, since there's no entrenched
4025 body of code to break, and it allows more efficient variable references
4026 in the presence of dynamic linking. */
4028 if (TREE_CODE (decl) == VAR_DECL
4029 && !initialized
4030 && TREE_PUBLIC (decl)
4031 && !DECL_THREAD_LOCAL_P (decl)
4032 && !flag_no_common)
4033 DECL_COMMON (decl) = 1;
4035 /* Set attributes here so if duplicate decl, will have proper attributes. */
4036 decl_attributes (&decl, attributes, 0);
4038 /* Handle gnu_inline attribute. */
4039 if (declspecs->inline_p
4040 && !flag_gnu89_inline
4041 && TREE_CODE (decl) == FUNCTION_DECL
4042 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4043 || current_function_decl))
4045 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4047 else if (declspecs->storage_class != csc_static)
4048 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4051 if (TREE_CODE (decl) == FUNCTION_DECL
4052 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4054 struct c_declarator *ce = declarator;
4056 if (ce->kind == cdk_pointer)
4057 ce = declarator->declarator;
4058 if (ce->kind == cdk_function)
4060 tree args = ce->u.arg_info->parms;
4061 for (; args; args = TREE_CHAIN (args))
4063 tree type = TREE_TYPE (args);
4064 if (type && INTEGRAL_TYPE_P (type)
4065 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4066 DECL_ARG_TYPE (args) = integer_type_node;
4071 if (TREE_CODE (decl) == FUNCTION_DECL
4072 && DECL_DECLARED_INLINE_P (decl)
4073 && DECL_UNINLINABLE (decl)
4074 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4075 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4076 decl);
4078 /* C99 6.7.4p3: An inline definition of a function with external
4079 linkage shall not contain a definition of a modifiable object
4080 with static storage duration... */
4081 if (TREE_CODE (decl) == VAR_DECL
4082 && current_scope != file_scope
4083 && TREE_STATIC (decl)
4084 && !TREE_READONLY (decl)
4085 && DECL_DECLARED_INLINE_P (current_function_decl)
4086 && DECL_EXTERNAL (current_function_decl))
4087 record_inline_static (input_location, current_function_decl,
4088 decl, csi_modifiable);
4090 /* Add this decl to the current scope.
4091 TEM may equal DECL or it may be a previous decl of the same name. */
4092 tem = pushdecl (decl);
4094 if (initialized && DECL_EXTERNAL (tem))
4096 DECL_EXTERNAL (tem) = 0;
4097 TREE_STATIC (tem) = 1;
4100 return tem;
4103 /* Finish processing of a declaration;
4104 install its initial value.
4105 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4106 If the length of an array type is not known before,
4107 it must be determined now, from the initial value, or it is an error.
4109 INIT_LOC is the location of the initial value. */
4111 void
4112 finish_decl (tree decl, location_t init_loc, tree init,
4113 tree origtype, tree asmspec_tree)
4115 tree type;
4116 bool was_incomplete = (DECL_SIZE (decl) == 0);
4117 const char *asmspec = 0;
4119 /* If a name was specified, get the string. */
4120 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
4121 && DECL_FILE_SCOPE_P (decl))
4122 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4123 if (asmspec_tree)
4124 asmspec = TREE_STRING_POINTER (asmspec_tree);
4126 if (TREE_CODE (decl) == VAR_DECL
4127 && TREE_STATIC (decl)
4128 && global_bindings_p ())
4129 /* So decl is a global variable. Record the types it uses
4130 so that we can decide later to emit debug info for them. */
4131 record_types_used_by_current_var_decl (decl);
4133 /* If `start_decl' didn't like having an initialization, ignore it now. */
4134 if (init != 0 && DECL_INITIAL (decl) == 0)
4135 init = 0;
4137 /* Don't crash if parm is initialized. */
4138 if (TREE_CODE (decl) == PARM_DECL)
4139 init = 0;
4141 if (init)
4142 store_init_value (init_loc, decl, init, origtype);
4144 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
4145 || TREE_CODE (decl) == FUNCTION_DECL
4146 || TREE_CODE (decl) == FIELD_DECL))
4147 objc_check_decl (decl);
4149 type = TREE_TYPE (decl);
4151 /* Deduce size of array from initialization, if not already known. */
4152 if (TREE_CODE (type) == ARRAY_TYPE
4153 && TYPE_DOMAIN (type) == 0
4154 && TREE_CODE (decl) != TYPE_DECL)
4156 bool do_default
4157 = (TREE_STATIC (decl)
4158 /* Even if pedantic, an external linkage array
4159 may have incomplete type at first. */
4160 ? pedantic && !TREE_PUBLIC (decl)
4161 : !DECL_EXTERNAL (decl));
4162 int failure
4163 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4164 do_default);
4166 /* Get the completed type made by complete_array_type. */
4167 type = TREE_TYPE (decl);
4169 switch (failure)
4171 case 1:
4172 error ("initializer fails to determine size of %q+D", decl);
4173 break;
4175 case 2:
4176 if (do_default)
4177 error ("array size missing in %q+D", decl);
4178 /* If a `static' var's size isn't known,
4179 make it extern as well as static, so it does not get
4180 allocated.
4181 If it is not `static', then do not mark extern;
4182 finish_incomplete_decl will give it a default size
4183 and it will get allocated. */
4184 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4185 DECL_EXTERNAL (decl) = 1;
4186 break;
4188 case 3:
4189 error ("zero or negative size array %q+D", decl);
4190 break;
4192 case 0:
4193 /* For global variables, update the copy of the type that
4194 exists in the binding. */
4195 if (TREE_PUBLIC (decl))
4197 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4198 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4199 b_ext = b_ext->shadowed;
4200 if (b_ext)
4202 if (b_ext->u.type)
4203 b_ext->u.type = composite_type (b_ext->u.type, type);
4204 else
4205 b_ext->u.type = type;
4208 break;
4210 default:
4211 gcc_unreachable ();
4214 if (DECL_INITIAL (decl))
4215 TREE_TYPE (DECL_INITIAL (decl)) = type;
4217 layout_decl (decl, 0);
4220 if (TREE_CODE (decl) == VAR_DECL)
4222 if (init && TREE_CODE (init) == CONSTRUCTOR)
4223 add_flexible_array_elts_to_size (decl, init);
4225 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
4226 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4227 layout_decl (decl, 0);
4229 if (DECL_SIZE (decl) == 0
4230 /* Don't give an error if we already gave one earlier. */
4231 && TREE_TYPE (decl) != error_mark_node
4232 && (TREE_STATIC (decl)
4233 /* A static variable with an incomplete type
4234 is an error if it is initialized.
4235 Also if it is not file scope.
4236 Otherwise, let it through, but if it is not `extern'
4237 then it may cause an error message later. */
4238 ? (DECL_INITIAL (decl) != 0
4239 || !DECL_FILE_SCOPE_P (decl))
4240 /* An automatic variable with an incomplete type
4241 is an error. */
4242 : !DECL_EXTERNAL (decl)))
4244 error ("storage size of %q+D isn%'t known", decl);
4245 TREE_TYPE (decl) = error_mark_node;
4248 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4249 && DECL_SIZE (decl) != 0)
4251 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4252 constant_expression_warning (DECL_SIZE (decl));
4253 else
4255 error ("storage size of %q+D isn%'t constant", decl);
4256 TREE_TYPE (decl) = error_mark_node;
4260 if (TREE_USED (type))
4262 TREE_USED (decl) = 1;
4263 DECL_READ_P (decl) = 1;
4267 /* If this is a function and an assembler name is specified, reset DECL_RTL
4268 so we can give it its new name. Also, update built_in_decls if it
4269 was a normal built-in. */
4270 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
4272 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4273 set_builtin_user_assembler_name (decl, asmspec);
4274 set_user_assembler_name (decl, asmspec);
4277 /* If #pragma weak was used, mark the decl weak now. */
4278 maybe_apply_pragma_weak (decl);
4280 /* Output the assembler code and/or RTL code for variables and functions,
4281 unless the type is an undefined structure or union.
4282 If not, it will get done when the type is completed. */
4284 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
4286 /* Determine the ELF visibility. */
4287 if (TREE_PUBLIC (decl))
4288 c_determine_visibility (decl);
4290 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4291 if (c_dialect_objc ())
4292 objc_check_decl (decl);
4294 if (asmspec)
4296 /* If this is not a static variable, issue a warning.
4297 It doesn't make any sense to give an ASMSPEC for an
4298 ordinary, non-register local variable. Historically,
4299 GCC has accepted -- but ignored -- the ASMSPEC in
4300 this case. */
4301 if (!DECL_FILE_SCOPE_P (decl)
4302 && TREE_CODE (decl) == VAR_DECL
4303 && !C_DECL_REGISTER (decl)
4304 && !TREE_STATIC (decl))
4305 warning (0, "ignoring asm-specifier for non-static local "
4306 "variable %q+D", decl);
4307 else
4308 set_user_assembler_name (decl, asmspec);
4311 if (DECL_FILE_SCOPE_P (decl))
4313 if (DECL_INITIAL (decl) == NULL_TREE
4314 || DECL_INITIAL (decl) == error_mark_node)
4315 /* Don't output anything
4316 when a tentative file-scope definition is seen.
4317 But at end of compilation, do output code for them. */
4318 DECL_DEFER_OUTPUT (decl) = 1;
4319 rest_of_decl_compilation (decl, true, 0);
4321 else
4323 /* In conjunction with an ASMSPEC, the `register'
4324 keyword indicates that we should place the variable
4325 in a particular register. */
4326 if (asmspec && C_DECL_REGISTER (decl))
4328 DECL_HARD_REGISTER (decl) = 1;
4329 /* This cannot be done for a structure with volatile
4330 fields, on which DECL_REGISTER will have been
4331 reset. */
4332 if (!DECL_REGISTER (decl))
4333 error ("cannot put object with volatile field into register");
4336 if (TREE_CODE (decl) != FUNCTION_DECL)
4338 /* If we're building a variable sized type, and we might be
4339 reachable other than via the top of the current binding
4340 level, then create a new BIND_EXPR so that we deallocate
4341 the object at the right time. */
4342 /* Note that DECL_SIZE can be null due to errors. */
4343 if (DECL_SIZE (decl)
4344 && !TREE_CONSTANT (DECL_SIZE (decl))
4345 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
4347 tree bind;
4348 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4349 TREE_SIDE_EFFECTS (bind) = 1;
4350 add_stmt (bind);
4351 BIND_EXPR_BODY (bind) = push_stmt_list ();
4353 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
4354 DECL_EXPR, decl));
4359 if (!DECL_FILE_SCOPE_P (decl))
4361 /* Recompute the RTL of a local array now
4362 if it used to be an incomplete type. */
4363 if (was_incomplete
4364 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
4366 /* If we used it already as memory, it must stay in memory. */
4367 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4368 /* If it's still incomplete now, no init will save it. */
4369 if (DECL_SIZE (decl) == 0)
4370 DECL_INITIAL (decl) = 0;
4375 if (TREE_CODE (decl) == TYPE_DECL)
4377 if (!DECL_FILE_SCOPE_P (decl)
4378 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
4379 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
4381 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
4384 /* At the end of a declaration, throw away any variable type sizes
4385 of types defined inside that declaration. There is no use
4386 computing them in the following function definition. */
4387 if (current_scope == file_scope)
4388 get_pending_sizes ();
4390 /* Install a cleanup (aka destructor) if one was given. */
4391 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
4393 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
4394 if (attr)
4396 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
4397 tree cleanup_decl = lookup_name (cleanup_id);
4398 tree cleanup;
4399 VEC(tree,gc) *vec;
4401 /* Build "cleanup(&decl)" for the destructor. */
4402 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
4403 vec = VEC_alloc (tree, gc, 1);
4404 VEC_quick_push (tree, vec, cleanup);
4405 cleanup = build_function_call_vec (DECL_SOURCE_LOCATION (decl),
4406 cleanup_decl, vec, NULL);
4407 VEC_free (tree, gc, vec);
4409 /* Don't warn about decl unused; the cleanup uses it. */
4410 TREE_USED (decl) = 1;
4411 TREE_USED (cleanup_decl) = 1;
4412 DECL_READ_P (decl) = 1;
4414 push_cleanup (decl, cleanup, false);
4418 if (warn_cxx_compat
4419 && TREE_CODE (decl) == VAR_DECL
4420 && TREE_READONLY (decl)
4421 && !DECL_EXTERNAL (decl)
4422 && DECL_INITIAL (decl) == NULL_TREE)
4423 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4424 "uninitialized const %qD is invalid in C++", decl);
4427 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
4429 tree
4430 grokparm (const struct c_parm *parm)
4432 tree attrs = parm->attrs;
4433 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
4434 NULL, &attrs, NULL, NULL, DEPRECATED_NORMAL);
4436 decl_attributes (&decl, attrs, 0);
4438 return decl;
4441 /* Given a parsed parameter declaration, decode it into a PARM_DECL
4442 and push that on the current scope. */
4444 void
4445 push_parm_decl (const struct c_parm *parm)
4447 tree attrs = parm->attrs;
4448 tree decl;
4450 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
4451 &attrs, NULL, NULL, DEPRECATED_NORMAL);
4452 decl_attributes (&decl, attrs, 0);
4454 decl = pushdecl (decl);
4456 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
4459 /* Mark all the parameter declarations to date as forward decls.
4460 Also diagnose use of this extension. */
4462 void
4463 mark_forward_parm_decls (void)
4465 struct c_binding *b;
4467 if (pedantic && !current_scope->warned_forward_parm_decls)
4469 pedwarn (input_location, OPT_pedantic,
4470 "ISO C forbids forward parameter declarations");
4471 current_scope->warned_forward_parm_decls = true;
4474 for (b = current_scope->bindings; b; b = b->prev)
4475 if (TREE_CODE (b->decl) == PARM_DECL)
4476 TREE_ASM_WRITTEN (b->decl) = 1;
4479 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
4480 literal, which may be an incomplete array type completed by the
4481 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
4482 literal. NON_CONST is true if the initializers contain something
4483 that cannot occur in a constant expression. */
4485 tree
4486 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
4488 /* We do not use start_decl here because we have a type, not a declarator;
4489 and do not use finish_decl because the decl should be stored inside
4490 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
4491 tree decl;
4492 tree complit;
4493 tree stmt;
4495 if (type == error_mark_node
4496 || init == error_mark_node)
4497 return error_mark_node;
4499 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
4500 DECL_EXTERNAL (decl) = 0;
4501 TREE_PUBLIC (decl) = 0;
4502 TREE_STATIC (decl) = (current_scope == file_scope);
4503 DECL_CONTEXT (decl) = current_function_decl;
4504 TREE_USED (decl) = 1;
4505 DECL_READ_P (decl) = 1;
4506 TREE_TYPE (decl) = type;
4507 TREE_READONLY (decl) = TYPE_READONLY (type);
4508 store_init_value (loc, decl, init, NULL_TREE);
4510 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
4512 int failure = complete_array_type (&TREE_TYPE (decl),
4513 DECL_INITIAL (decl), true);
4514 gcc_assert (!failure);
4516 type = TREE_TYPE (decl);
4517 TREE_TYPE (DECL_INITIAL (decl)) = type;
4520 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4521 return error_mark_node;
4523 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
4524 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
4525 TREE_SIDE_EFFECTS (complit) = 1;
4527 layout_decl (decl, 0);
4529 if (TREE_STATIC (decl))
4531 /* This decl needs a name for the assembler output. */
4532 set_compound_literal_name (decl);
4533 DECL_DEFER_OUTPUT (decl) = 1;
4534 DECL_COMDAT (decl) = 1;
4535 DECL_ARTIFICIAL (decl) = 1;
4536 DECL_IGNORED_P (decl) = 1;
4537 pushdecl (decl);
4538 rest_of_decl_compilation (decl, 1, 0);
4541 if (non_const)
4543 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
4544 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
4547 return complit;
4550 /* Check the type of a compound literal. Here we just check that it
4551 is valid for C++. */
4553 void
4554 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
4556 if (warn_cxx_compat && type_name->specs->tag_defined_p)
4557 warning_at (loc, OPT_Wc___compat,
4558 "defining a type in a compound literal is invalid in C++");
4561 /* Determine whether TYPE is a structure with a flexible array member,
4562 or a union containing such a structure (possibly recursively). */
4564 static bool
4565 flexible_array_type_p (tree type)
4567 tree x;
4568 switch (TREE_CODE (type))
4570 case RECORD_TYPE:
4571 x = TYPE_FIELDS (type);
4572 if (x == NULL_TREE)
4573 return false;
4574 while (TREE_CHAIN (x) != NULL_TREE)
4575 x = TREE_CHAIN (x);
4576 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4577 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4578 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
4579 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
4580 return true;
4581 return false;
4582 case UNION_TYPE:
4583 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
4585 if (flexible_array_type_p (TREE_TYPE (x)))
4586 return true;
4588 return false;
4589 default:
4590 return false;
4594 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
4595 replacing with appropriate values if they are invalid. */
4596 static void
4597 check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
4599 tree type_mv;
4600 unsigned int max_width;
4601 unsigned HOST_WIDE_INT w;
4602 const char *name = (orig_name
4603 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
4604 : _("<anonymous>"));
4606 /* Detect and ignore out of range field width and process valid
4607 field widths. */
4608 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
4610 error ("bit-field %qs width not an integer constant", name);
4611 *width = integer_one_node;
4613 else
4615 if (TREE_CODE (*width) != INTEGER_CST)
4617 *width = c_fully_fold (*width, false, NULL);
4618 if (TREE_CODE (*width) == INTEGER_CST)
4619 pedwarn (input_location, OPT_pedantic,
4620 "bit-field %qs width not an integer constant expression",
4621 name);
4623 if (TREE_CODE (*width) != INTEGER_CST)
4625 error ("bit-field %qs width not an integer constant", name);
4626 *width = integer_one_node;
4628 constant_expression_warning (*width);
4629 if (tree_int_cst_sgn (*width) < 0)
4631 error ("negative width in bit-field %qs", name);
4632 *width = integer_one_node;
4634 else if (integer_zerop (*width) && orig_name)
4636 error ("zero width for bit-field %qs", name);
4637 *width = integer_one_node;
4641 /* Detect invalid bit-field type. */
4642 if (TREE_CODE (*type) != INTEGER_TYPE
4643 && TREE_CODE (*type) != BOOLEAN_TYPE
4644 && TREE_CODE (*type) != ENUMERAL_TYPE)
4646 error ("bit-field %qs has invalid type", name);
4647 *type = unsigned_type_node;
4650 type_mv = TYPE_MAIN_VARIANT (*type);
4651 if (!in_system_header
4652 && type_mv != integer_type_node
4653 && type_mv != unsigned_type_node
4654 && type_mv != boolean_type_node)
4655 pedwarn (input_location, OPT_pedantic,
4656 "type of bit-field %qs is a GCC extension", name);
4658 max_width = TYPE_PRECISION (*type);
4660 if (0 < compare_tree_int (*width, max_width))
4662 error ("width of %qs exceeds its type", name);
4663 w = max_width;
4664 *width = build_int_cst (NULL_TREE, w);
4666 else
4667 w = tree_low_cst (*width, 1);
4669 if (TREE_CODE (*type) == ENUMERAL_TYPE)
4671 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
4672 if (!lt
4673 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
4674 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
4675 warning (0, "%qs is narrower than values of its type", name);
4681 /* Print warning about variable length array if necessary. */
4683 static void
4684 warn_variable_length_array (tree name, tree size)
4686 int const_size = TREE_CONSTANT (size);
4688 if (!flag_isoc99 && pedantic && warn_vla != 0)
4690 if (const_size)
4692 if (name)
4693 pedwarn (input_location, OPT_Wvla,
4694 "ISO C90 forbids array %qE whose size "
4695 "can%'t be evaluated",
4696 name);
4697 else
4698 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array whose size "
4699 "can%'t be evaluated");
4701 else
4703 if (name)
4704 pedwarn (input_location, OPT_Wvla,
4705 "ISO C90 forbids variable length array %qE",
4706 name);
4707 else
4708 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array");
4711 else if (warn_vla > 0)
4713 if (const_size)
4715 if (name)
4716 warning (OPT_Wvla,
4717 "the size of array %qE can"
4718 "%'t be evaluated", name);
4719 else
4720 warning (OPT_Wvla,
4721 "the size of array can %'t be evaluated");
4723 else
4725 if (name)
4726 warning (OPT_Wvla,
4727 "variable length array %qE is used",
4728 name);
4729 else
4730 warning (OPT_Wvla,
4731 "variable length array is used");
4736 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR to
4737 serve as the actual size-expression for a type or decl. This is
4738 like variable_size in stor-layout.c, but we make global_bindings_p
4739 return negative to avoid calls to that function from outside the
4740 front end resulting in errors at file scope, then call this version
4741 instead from front-end code. */
4743 static tree
4744 c_variable_size (tree size)
4746 tree save;
4748 if (TREE_CONSTANT (size))
4749 return size;
4751 size = save_expr (size);
4753 save = skip_simple_arithmetic (size);
4755 if (cfun && cfun->dont_save_pending_sizes_p)
4756 return size;
4758 if (!global_bindings_p ())
4759 put_pending_size (save);
4761 return size;
4764 /* Given declspecs and a declarator,
4765 determine the name and type of the object declared
4766 and construct a ..._DECL node for it.
4767 (In one case we can return a ..._TYPE node instead.
4768 For invalid input we sometimes return 0.)
4770 DECLSPECS is a c_declspecs structure for the declaration specifiers.
4772 DECL_CONTEXT says which syntactic context this declaration is in:
4773 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4774 FUNCDEF for a function definition. Like NORMAL but a few different
4775 error messages in each case. Return value may be zero meaning
4776 this definition is too screwy to try to parse.
4777 PARM for a parameter declaration (either within a function prototype
4778 or before a function body). Make a PARM_DECL, or return void_type_node.
4779 TYPENAME if for a typename (in a cast or sizeof).
4780 Don't make a DECL node; just return the ..._TYPE node.
4781 FIELD for a struct or union field; make a FIELD_DECL.
4782 INITIALIZED is true if the decl has an initializer.
4783 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4784 representing the width of the bit-field.
4785 DECL_ATTRS points to the list of attributes that should be added to this
4786 decl. Any nested attributes that belong on the decl itself will be
4787 added to this list.
4788 If EXPR is not NULL, any expressions that need to be evaluated as
4789 part of evaluating variably modified types will be stored in *EXPR.
4790 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
4791 set to indicate whether operands in *EXPR can be used in constant
4792 expressions.
4793 DEPRECATED_STATE is a deprecated_states value indicating whether
4794 deprecation warnings should be suppressed.
4796 In the TYPENAME case, DECLARATOR is really an absolute declarator.
4797 It may also be so in the PARM case, for a prototype where the
4798 argument type is specified but not the name.
4800 This function is where the complicated C meanings of `static'
4801 and `extern' are interpreted. */
4803 static tree
4804 grokdeclarator (const struct c_declarator *declarator,
4805 struct c_declspecs *declspecs,
4806 enum decl_context decl_context, bool initialized, tree *width,
4807 tree *decl_attrs, tree *expr, bool *expr_const_operands,
4808 enum deprecated_states deprecated_state)
4810 tree type = declspecs->type;
4811 bool threadp = declspecs->thread_p;
4812 enum c_storage_class storage_class = declspecs->storage_class;
4813 int constp;
4814 int restrictp;
4815 int volatilep;
4816 int type_quals = TYPE_UNQUALIFIED;
4817 tree name = NULL_TREE;
4818 bool funcdef_flag = false;
4819 bool funcdef_syntax = false;
4820 bool size_varies = false;
4821 tree decl_attr = declspecs->decl_attr;
4822 int array_ptr_quals = TYPE_UNQUALIFIED;
4823 tree array_ptr_attrs = NULL_TREE;
4824 int array_parm_static = 0;
4825 bool array_parm_vla_unspec_p = false;
4826 tree returned_attrs = NULL_TREE;
4827 bool bitfield = width != NULL;
4828 tree element_type;
4829 struct c_arg_info *arg_info = 0;
4830 addr_space_t as1, as2, address_space;
4831 location_t loc = UNKNOWN_LOCATION;
4832 const char *errmsg;
4833 tree expr_dummy;
4834 bool expr_const_operands_dummy;
4836 if (expr == NULL)
4837 expr = &expr_dummy;
4838 if (expr_const_operands == NULL)
4839 expr_const_operands = &expr_const_operands_dummy;
4841 *expr = declspecs->expr;
4842 *expr_const_operands = declspecs->expr_const_operands;
4844 if (decl_context == FUNCDEF)
4845 funcdef_flag = true, decl_context = NORMAL;
4847 /* Look inside a declarator for the name being declared
4848 and get it as an IDENTIFIER_NODE, for an error message. */
4850 const struct c_declarator *decl = declarator;
4852 while (decl)
4853 switch (decl->kind)
4855 case cdk_array:
4856 loc = decl->id_loc;
4857 /* FALL THRU. */
4859 case cdk_function:
4860 case cdk_pointer:
4861 funcdef_syntax = (decl->kind == cdk_function);
4862 decl = decl->declarator;
4863 break;
4865 case cdk_attrs:
4866 decl = decl->declarator;
4867 break;
4869 case cdk_id:
4870 loc = decl->id_loc;
4871 if (decl->u.id)
4872 name = decl->u.id;
4873 decl = 0;
4874 break;
4876 default:
4877 gcc_unreachable ();
4879 if (name == 0)
4881 gcc_assert (decl_context == PARM
4882 || decl_context == TYPENAME
4883 || (decl_context == FIELD
4884 && declarator->kind == cdk_id));
4885 gcc_assert (!initialized);
4889 /* A function definition's declarator must have the form of
4890 a function declarator. */
4892 if (funcdef_flag && !funcdef_syntax)
4893 return 0;
4895 /* If this looks like a function definition, make it one,
4896 even if it occurs where parms are expected.
4897 Then store_parm_decls will reject it and not use it as a parm. */
4898 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4899 decl_context = PARM;
4901 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4902 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
4904 if ((decl_context == NORMAL || decl_context == FIELD)
4905 && current_scope == file_scope
4906 && variably_modified_type_p (type, NULL_TREE))
4908 if (name)
4909 error_at (loc, "variably modified %qE at file scope", name);
4910 else
4911 error_at (loc, "variably modified field at file scope");
4912 type = integer_type_node;
4915 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
4917 /* Diagnose defaulting to "int". */
4919 if (declspecs->default_int_p && !in_system_header)
4921 /* Issue a warning if this is an ISO C 99 program or if
4922 -Wreturn-type and this is a function, or if -Wimplicit;
4923 prefer the former warning since it is more explicit. */
4924 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4925 && funcdef_flag)
4926 warn_about_return_type = 1;
4927 else
4929 if (name)
4930 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wimplicit_int,
4931 "type defaults to %<int%> in declaration of %qE",
4932 name);
4933 else
4934 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int,
4935 "type defaults to %<int%> in type name");
4939 /* Adjust the type if a bit-field is being declared,
4940 -funsigned-bitfields applied and the type is not explicitly
4941 "signed". */
4942 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4943 && TREE_CODE (type) == INTEGER_TYPE)
4944 type = unsigned_type_for (type);
4946 /* Figure out the type qualifiers for the declaration. There are
4947 two ways a declaration can become qualified. One is something
4948 like `const int i' where the `const' is explicit. Another is
4949 something like `typedef const int CI; CI i' where the type of the
4950 declaration contains the `const'. A third possibility is that
4951 there is a type qualifier on the element type of a typedefed
4952 array type, in which case we should extract that qualifier so
4953 that c_apply_type_quals_to_decl receives the full list of
4954 qualifiers to work with (C90 is not entirely clear about whether
4955 duplicate qualifiers should be diagnosed in this case, but it
4956 seems most appropriate to do so). */
4957 element_type = strip_array_types (type);
4958 constp = declspecs->const_p + TYPE_READONLY (element_type);
4959 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4960 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4961 as1 = declspecs->address_space;
4962 as2 = TYPE_ADDR_SPACE (element_type);
4963 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
4965 if (pedantic && !flag_isoc99)
4967 if (constp > 1)
4968 pedwarn (loc, OPT_pedantic, "duplicate %<const%>");
4969 if (restrictp > 1)
4970 pedwarn (loc, OPT_pedantic, "duplicate %<restrict%>");
4971 if (volatilep > 1)
4972 pedwarn (loc, OPT_pedantic, "duplicate %<volatile%>");
4975 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
4976 error_at (loc, "conflicting named address spaces (%s vs %s)",
4977 c_addr_space_name (as1), c_addr_space_name (as2));
4979 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4980 type = TYPE_MAIN_VARIANT (type);
4981 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4982 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4983 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
4984 | ENCODE_QUAL_ADDR_SPACE (address_space));
4986 /* Warn about storage classes that are invalid for certain
4987 kinds of declarations (parameters, typenames, etc.). */
4989 if (funcdef_flag
4990 && (threadp
4991 || storage_class == csc_auto
4992 || storage_class == csc_register
4993 || storage_class == csc_typedef))
4995 if (storage_class == csc_auto)
4996 pedwarn (loc,
4997 (current_scope == file_scope) ? 0 : OPT_pedantic,
4998 "function definition declared %<auto%>");
4999 if (storage_class == csc_register)
5000 error_at (loc, "function definition declared %<register%>");
5001 if (storage_class == csc_typedef)
5002 error_at (loc, "function definition declared %<typedef%>");
5003 if (threadp)
5004 error_at (loc, "function definition declared %<__thread%>");
5005 threadp = false;
5006 if (storage_class == csc_auto
5007 || storage_class == csc_register
5008 || storage_class == csc_typedef)
5009 storage_class = csc_none;
5011 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5013 if (decl_context == PARM && storage_class == csc_register)
5015 else
5017 switch (decl_context)
5019 case FIELD:
5020 if (name)
5021 error_at (loc, "storage class specified for structure "
5022 "field %qE", name);
5023 else
5024 error_at (loc, "storage class specified for structure field");
5025 break;
5026 case PARM:
5027 if (name)
5028 error_at (loc, "storage class specified for parameter %qE",
5029 name);
5030 else
5031 error_at (loc, "storage class specified for unnamed parameter");
5032 break;
5033 default:
5034 error_at (loc, "storage class specified for typename");
5035 break;
5037 storage_class = csc_none;
5038 threadp = false;
5041 else if (storage_class == csc_extern
5042 && initialized
5043 && !funcdef_flag)
5045 /* 'extern' with initialization is invalid if not at file scope. */
5046 if (current_scope == file_scope)
5048 /* It is fine to have 'extern const' when compiling at C
5049 and C++ intersection. */
5050 if (!(warn_cxx_compat && constp))
5051 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5052 name);
5054 else
5055 error_at (loc, "%qE has both %<extern%> and initializer", name);
5057 else if (current_scope == file_scope)
5059 if (storage_class == csc_auto)
5060 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5061 name);
5062 if (pedantic && storage_class == csc_register)
5063 pedwarn (input_location, OPT_pedantic,
5064 "file-scope declaration of %qE specifies %<register%>", name);
5066 else
5068 if (storage_class == csc_extern && funcdef_flag)
5069 error_at (loc, "nested function %qE declared %<extern%>", name);
5070 else if (threadp && storage_class == csc_none)
5072 error_at (loc, "function-scope %qE implicitly auto and declared "
5073 "%<__thread%>",
5074 name);
5075 threadp = false;
5079 /* Now figure out the structure of the declarator proper.
5080 Descend through it, creating more complex types, until we reach
5081 the declared identifier (or NULL_TREE, in an absolute declarator).
5082 At each stage we maintain an unqualified version of the type
5083 together with any qualifiers that should be applied to it with
5084 c_build_qualified_type; this way, array types including
5085 multidimensional array types are first built up in unqualified
5086 form and then the qualified form is created with
5087 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5089 while (declarator && declarator->kind != cdk_id)
5091 if (type == error_mark_node)
5093 declarator = declarator->declarator;
5094 continue;
5097 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5098 a cdk_pointer (for *...),
5099 a cdk_function (for ...(...)),
5100 a cdk_attrs (for nested attributes),
5101 or a cdk_id (for the name being declared
5102 or the place in an absolute declarator
5103 where the name was omitted).
5104 For the last case, we have just exited the loop.
5106 At this point, TYPE is the type of elements of an array,
5107 or for a function to return, or for a pointer to point to.
5108 After this sequence of ifs, TYPE is the type of the
5109 array or function or pointer, and DECLARATOR has had its
5110 outermost layer removed. */
5112 if (array_ptr_quals != TYPE_UNQUALIFIED
5113 || array_ptr_attrs != NULL_TREE
5114 || array_parm_static)
5116 /* Only the innermost declarator (making a parameter be of
5117 array type which is converted to pointer type)
5118 may have static or type qualifiers. */
5119 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5120 array_ptr_quals = TYPE_UNQUALIFIED;
5121 array_ptr_attrs = NULL_TREE;
5122 array_parm_static = 0;
5125 switch (declarator->kind)
5127 case cdk_attrs:
5129 /* A declarator with embedded attributes. */
5130 tree attrs = declarator->u.attrs;
5131 const struct c_declarator *inner_decl;
5132 int attr_flags = 0;
5133 declarator = declarator->declarator;
5134 inner_decl = declarator;
5135 while (inner_decl->kind == cdk_attrs)
5136 inner_decl = inner_decl->declarator;
5137 if (inner_decl->kind == cdk_id)
5138 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5139 else if (inner_decl->kind == cdk_function)
5140 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5141 else if (inner_decl->kind == cdk_array)
5142 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5143 returned_attrs = decl_attributes (&type,
5144 chainon (returned_attrs, attrs),
5145 attr_flags);
5146 break;
5148 case cdk_array:
5150 tree itype = NULL_TREE;
5151 tree size = declarator->u.array.dimen;
5152 /* The index is a signed object `sizetype' bits wide. */
5153 tree index_type = c_common_signed_type (sizetype);
5155 array_ptr_quals = declarator->u.array.quals;
5156 array_ptr_attrs = declarator->u.array.attrs;
5157 array_parm_static = declarator->u.array.static_p;
5158 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5160 declarator = declarator->declarator;
5162 /* Check for some types that there cannot be arrays of. */
5164 if (VOID_TYPE_P (type))
5166 if (name)
5167 error_at (loc, "declaration of %qE as array of voids", name);
5168 else
5169 error_at (loc, "declaration of type name as array of voids");
5170 type = error_mark_node;
5173 if (TREE_CODE (type) == FUNCTION_TYPE)
5175 if (name)
5176 error_at (loc, "declaration of %qE as array of functions",
5177 name);
5178 else
5179 error_at (loc, "declaration of type name as array of "
5180 "functions");
5181 type = error_mark_node;
5184 if (pedantic && !in_system_header && flexible_array_type_p (type))
5185 pedwarn (loc, OPT_pedantic,
5186 "invalid use of structure with flexible array member");
5188 if (size == error_mark_node)
5189 type = error_mark_node;
5191 if (type == error_mark_node)
5192 continue;
5194 /* If size was specified, set ITYPE to a range-type for
5195 that size. Otherwise, ITYPE remains null. finish_decl
5196 may figure it out from an initial value. */
5198 if (size)
5200 bool size_maybe_const = true;
5201 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5202 && !TREE_OVERFLOW (size));
5203 bool this_size_varies = false;
5205 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5206 lvalue. */
5207 STRIP_TYPE_NOPS (size);
5209 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5211 if (name)
5212 error_at (loc, "size of array %qE has non-integer type",
5213 name);
5214 else
5215 error_at (loc,
5216 "size of unnamed array has non-integer type");
5217 size = integer_one_node;
5220 size = c_fully_fold (size, false, &size_maybe_const);
5222 if (pedantic && size_maybe_const && integer_zerop (size))
5224 if (name)
5225 pedwarn (loc, OPT_pedantic,
5226 "ISO C forbids zero-size array %qE", name);
5227 else
5228 pedwarn (loc, OPT_pedantic,
5229 "ISO C forbids zero-size array");
5232 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
5234 constant_expression_warning (size);
5235 if (tree_int_cst_sgn (size) < 0)
5237 if (name)
5238 error_at (loc, "size of array %qE is negative", name);
5239 else
5240 error_at (loc, "size of unnamed array is negative");
5241 size = integer_one_node;
5243 /* Handle a size folded to an integer constant but
5244 not an integer constant expression. */
5245 if (!size_int_const)
5247 /* If this is a file scope declaration of an
5248 ordinary identifier, this is invalid code;
5249 diagnosing it here and not subsequently
5250 treating the type as variable-length avoids
5251 more confusing diagnostics later. */
5252 if ((decl_context == NORMAL || decl_context == FIELD)
5253 && current_scope == file_scope)
5254 pedwarn (input_location, 0,
5255 "variably modified %qE at file scope",
5256 name);
5257 else
5258 this_size_varies = size_varies = true;
5259 warn_variable_length_array (name, size);
5262 else if ((decl_context == NORMAL || decl_context == FIELD)
5263 && current_scope == file_scope)
5265 error_at (loc, "variably modified %qE at file scope", name);
5266 size = integer_one_node;
5268 else
5270 /* Make sure the array size remains visibly
5271 nonconstant even if it is (eg) a const variable
5272 with known value. */
5273 this_size_varies = size_varies = true;
5274 warn_variable_length_array (name, size);
5277 if (integer_zerop (size) && !this_size_varies)
5279 /* A zero-length array cannot be represented with
5280 an unsigned index type, which is what we'll
5281 get with build_index_type. Create an
5282 open-ended range instead. */
5283 itype = build_range_type (sizetype, size, NULL_TREE);
5285 else
5287 /* Arrange for the SAVE_EXPR on the inside of the
5288 MINUS_EXPR, which allows the -1 to get folded
5289 with the +1 that happens when building TYPE_SIZE. */
5290 if (size_varies)
5291 size = c_variable_size (size);
5292 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
5293 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5294 integer_zero_node, size);
5296 /* Compute the maximum valid index, that is, size
5297 - 1. Do the calculation in index_type, so that
5298 if it is a variable the computations will be
5299 done in the proper mode. */
5300 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
5301 convert (index_type, size),
5302 convert (index_type,
5303 size_one_node));
5305 /* If that overflowed, the array is too big. ???
5306 While a size of INT_MAX+1 technically shouldn't
5307 cause an overflow (because we subtract 1), the
5308 overflow is recorded during the conversion to
5309 index_type, before the subtraction. Handling
5310 this case seems like an unnecessary
5311 complication. */
5312 if (TREE_CODE (itype) == INTEGER_CST
5313 && TREE_OVERFLOW (itype))
5315 if (name)
5316 error_at (loc, "size of array %qE is too large",
5317 name);
5318 else
5319 error_at (loc, "size of unnamed array is too large");
5320 type = error_mark_node;
5321 continue;
5324 itype = build_index_type (itype);
5326 if (this_size_varies)
5328 if (*expr)
5329 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5330 *expr, size);
5331 else
5332 *expr = size;
5333 *expr_const_operands &= size_maybe_const;
5336 else if (decl_context == FIELD)
5338 bool flexible_array_member = false;
5339 if (array_parm_vla_unspec_p)
5340 /* Field names can in fact have function prototype
5341 scope so [*] is disallowed here through making
5342 the field variably modified, not through being
5343 something other than a declaration with function
5344 prototype scope. */
5345 size_varies = true;
5346 else
5348 const struct c_declarator *t = declarator;
5349 while (t->kind == cdk_attrs)
5350 t = t->declarator;
5351 flexible_array_member = (t->kind == cdk_id);
5353 if (flexible_array_member
5354 && pedantic && !flag_isoc99 && !in_system_header)
5355 pedwarn (loc, OPT_pedantic,
5356 "ISO C90 does not support flexible array members");
5358 /* ISO C99 Flexible array members are effectively
5359 identical to GCC's zero-length array extension. */
5360 if (flexible_array_member || array_parm_vla_unspec_p)
5361 itype = build_range_type (sizetype, size_zero_node,
5362 NULL_TREE);
5364 else if (decl_context == PARM)
5366 if (array_parm_vla_unspec_p)
5368 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
5369 size_varies = true;
5372 else if (decl_context == TYPENAME)
5374 if (array_parm_vla_unspec_p)
5376 /* C99 6.7.5.2p4 */
5377 warning (0, "%<[*]%> not in a declaration");
5378 /* We use this to avoid messing up with incomplete
5379 array types of the same type, that would
5380 otherwise be modified below. */
5381 itype = build_range_type (sizetype, size_zero_node,
5382 NULL_TREE);
5383 size_varies = true;
5387 /* Complain about arrays of incomplete types. */
5388 if (!COMPLETE_TYPE_P (type))
5390 error_at (loc, "array type has incomplete element type");
5391 type = error_mark_node;
5393 else
5394 /* When itype is NULL, a shared incomplete array type is
5395 returned for all array of a given type. Elsewhere we
5396 make sure we don't complete that type before copying
5397 it, but here we want to make sure we don't ever
5398 modify the shared type, so we gcc_assert (itype)
5399 below. */
5401 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
5402 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
5403 type = build_qualified_type (type,
5404 ENCODE_QUAL_ADDR_SPACE (as));
5406 type = build_array_type (type, itype);
5409 if (type != error_mark_node)
5411 if (size_varies)
5413 /* It is ok to modify type here even if itype is
5414 NULL: if size_varies, we're in a
5415 multi-dimensional array and the inner type has
5416 variable size, so the enclosing shared array type
5417 must too. */
5418 if (size && TREE_CODE (size) == INTEGER_CST)
5419 type
5420 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5421 C_TYPE_VARIABLE_SIZE (type) = 1;
5424 /* The GCC extension for zero-length arrays differs from
5425 ISO flexible array members in that sizeof yields
5426 zero. */
5427 if (size && integer_zerop (size))
5429 gcc_assert (itype);
5430 TYPE_SIZE (type) = bitsize_zero_node;
5431 TYPE_SIZE_UNIT (type) = size_zero_node;
5432 SET_TYPE_STRUCTURAL_EQUALITY (type);
5434 if (array_parm_vla_unspec_p)
5436 gcc_assert (itype);
5437 /* The type is complete. C99 6.7.5.2p4 */
5438 TYPE_SIZE (type) = bitsize_zero_node;
5439 TYPE_SIZE_UNIT (type) = size_zero_node;
5440 SET_TYPE_STRUCTURAL_EQUALITY (type);
5444 if (decl_context != PARM
5445 && (array_ptr_quals != TYPE_UNQUALIFIED
5446 || array_ptr_attrs != NULL_TREE
5447 || array_parm_static))
5449 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5450 array_ptr_quals = TYPE_UNQUALIFIED;
5451 array_ptr_attrs = NULL_TREE;
5452 array_parm_static = 0;
5454 break;
5456 case cdk_function:
5458 /* Say it's a definition only for the declarator closest
5459 to the identifier, apart possibly from some
5460 attributes. */
5461 bool really_funcdef = false;
5462 tree arg_types;
5463 if (funcdef_flag)
5465 const struct c_declarator *t = declarator->declarator;
5466 while (t->kind == cdk_attrs)
5467 t = t->declarator;
5468 really_funcdef = (t->kind == cdk_id);
5471 /* Declaring a function type. Make sure we have a valid
5472 type for the function to return. */
5473 if (type == error_mark_node)
5474 continue;
5476 size_varies = false;
5478 /* Warn about some types functions can't return. */
5479 if (TREE_CODE (type) == FUNCTION_TYPE)
5481 if (name)
5482 error_at (loc, "%qE declared as function returning a "
5483 "function", name);
5484 else
5485 error_at (loc, "type name declared as function "
5486 "returning a function");
5487 type = integer_type_node;
5489 if (TREE_CODE (type) == ARRAY_TYPE)
5491 if (name)
5492 error_at (loc, "%qE declared as function returning an array",
5493 name);
5494 else
5495 error_at (loc, "type name declared as function returning "
5496 "an array");
5497 type = integer_type_node;
5499 errmsg = targetm.invalid_return_type (type);
5500 if (errmsg)
5502 error (errmsg);
5503 type = integer_type_node;
5506 /* Construct the function type and go to the next
5507 inner layer of declarator. */
5508 arg_info = declarator->u.arg_info;
5509 arg_types = grokparms (arg_info, really_funcdef);
5510 if (really_funcdef)
5511 put_pending_sizes (arg_info->pending_sizes);
5513 /* Type qualifiers before the return type of the function
5514 qualify the return type, not the function type. */
5515 if (type_quals)
5517 /* Type qualifiers on a function return type are
5518 normally permitted by the standard but have no
5519 effect, so give a warning at -Wreturn-type.
5520 Qualifiers on a void return type are banned on
5521 function definitions in ISO C; GCC used to used
5522 them for noreturn functions. */
5523 if (VOID_TYPE_P (type) && really_funcdef)
5524 pedwarn (loc, 0,
5525 "function definition has qualified void return type");
5526 else
5527 warning_at (loc, OPT_Wignored_qualifiers,
5528 "type qualifiers ignored on function return type");
5530 type = c_build_qualified_type (type, type_quals);
5532 type_quals = TYPE_UNQUALIFIED;
5534 type = build_function_type (type, arg_types);
5535 declarator = declarator->declarator;
5537 /* Set the TYPE_CONTEXTs for each tagged type which is local to
5538 the formal parameter list of this FUNCTION_TYPE to point to
5539 the FUNCTION_TYPE node itself. */
5541 tree link;
5543 for (link = arg_info->tags;
5544 link;
5545 link = TREE_CHAIN (link))
5546 TYPE_CONTEXT (TREE_VALUE (link)) = type;
5548 break;
5550 case cdk_pointer:
5552 /* Merge any constancy or volatility into the target type
5553 for the pointer. */
5555 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5556 && type_quals)
5557 pedwarn (loc, OPT_pedantic,
5558 "ISO C forbids qualified function types");
5559 if (type_quals)
5560 type = c_build_qualified_type (type, type_quals);
5561 size_varies = false;
5563 /* When the pointed-to type involves components of variable size,
5564 care must be taken to ensure that the size evaluation code is
5565 emitted early enough to dominate all the possible later uses
5566 and late enough for the variables on which it depends to have
5567 been assigned.
5569 This is expected to happen automatically when the pointed-to
5570 type has a name/declaration of it's own, but special attention
5571 is required if the type is anonymous.
5573 We handle the NORMAL and FIELD contexts here by attaching an
5574 artificial TYPE_DECL to such pointed-to type. This forces the
5575 sizes evaluation at a safe point and ensures it is not deferred
5576 until e.g. within a deeper conditional context.
5578 We expect nothing to be needed here for PARM or TYPENAME.
5579 Pushing a TYPE_DECL at this point for TYPENAME would actually
5580 be incorrect, as we might be in the middle of an expression
5581 with side effects on the pointed-to type size "arguments" prior
5582 to the pointer declaration point and the fake TYPE_DECL in the
5583 enclosing context would force the size evaluation prior to the
5584 side effects. */
5586 if (!TYPE_NAME (type)
5587 && (decl_context == NORMAL || decl_context == FIELD)
5588 && variably_modified_type_p (type, NULL_TREE))
5590 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
5591 DECL_ARTIFICIAL (decl) = 1;
5592 pushdecl (decl);
5593 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
5594 TYPE_NAME (type) = decl;
5597 type = build_pointer_type (type);
5599 /* Process type qualifiers (such as const or volatile)
5600 that were given inside the `*'. */
5601 type_quals = declarator->u.pointer_quals;
5603 declarator = declarator->declarator;
5604 break;
5606 default:
5607 gcc_unreachable ();
5610 *decl_attrs = chainon (returned_attrs, *decl_attrs);
5612 /* Now TYPE has the actual type, apart from any qualifiers in
5613 TYPE_QUALS. */
5615 /* Warn about address space used for things other than static memory or
5616 pointers. */
5617 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
5618 if (!ADDR_SPACE_GENERIC_P (address_space))
5620 if (decl_context == NORMAL)
5622 switch (storage_class)
5624 case csc_auto:
5625 error ("%qs combined with %<auto%> qualifier for %qE",
5626 c_addr_space_name (address_space), name);
5627 break;
5628 case csc_register:
5629 error ("%qs combined with %<register%> qualifier for %qE",
5630 c_addr_space_name (address_space), name);
5631 break;
5632 case csc_none:
5633 if (current_function_scope)
5635 error ("%qs specified for auto variable %qE",
5636 c_addr_space_name (address_space), name);
5637 break;
5639 break;
5640 case csc_static:
5641 case csc_extern:
5642 case csc_typedef:
5643 break;
5644 default:
5645 gcc_unreachable ();
5648 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
5650 if (name)
5651 error ("%qs specified for parameter %qE",
5652 c_addr_space_name (address_space), name);
5653 else
5654 error ("%qs specified for unnamed parameter",
5655 c_addr_space_name (address_space));
5657 else if (decl_context == FIELD)
5659 if (name)
5660 error ("%qs specified for structure field %qE",
5661 c_addr_space_name (address_space), name);
5662 else
5663 error ("%qs specified for structure field",
5664 c_addr_space_name (address_space));
5668 /* Check the type and width of a bit-field. */
5669 if (bitfield)
5670 check_bitfield_type_and_width (&type, width, name);
5672 /* Did array size calculations overflow? */
5674 if (TREE_CODE (type) == ARRAY_TYPE
5675 && COMPLETE_TYPE_P (type)
5676 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
5677 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
5679 if (name)
5680 error_at (loc, "size of array %qE is too large", name);
5681 else
5682 error_at (loc, "size of unnamed array is too large");
5683 /* If we proceed with the array type as it is, we'll eventually
5684 crash in tree_low_cst(). */
5685 type = error_mark_node;
5688 /* If this is declaring a typedef name, return a TYPE_DECL. */
5690 if (storage_class == csc_typedef)
5692 tree decl;
5693 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5694 && type_quals)
5695 pedwarn (loc, OPT_pedantic,
5696 "ISO C forbids qualified function types");
5697 if (type_quals)
5698 type = c_build_qualified_type (type, type_quals);
5699 decl = build_decl (declarator->id_loc,
5700 TYPE_DECL, declarator->u.id, type);
5701 if (declspecs->explicit_signed_p)
5702 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
5703 if (declspecs->inline_p)
5704 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
5706 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
5708 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
5710 if (b != NULL
5711 && b->decl != NULL_TREE
5712 && (B_IN_CURRENT_SCOPE (b)
5713 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
5714 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
5716 warning_at (declarator->id_loc, OPT_Wc___compat,
5717 ("using %qD as both a typedef and a tag is "
5718 "invalid in C++"),
5719 decl);
5720 if (b->locus != UNKNOWN_LOCATION)
5721 inform (b->locus, "originally defined here");
5725 return decl;
5728 /* If this is a type name (such as, in a cast or sizeof),
5729 compute the type and return it now. */
5731 if (decl_context == TYPENAME)
5733 /* Note that the grammar rejects storage classes in typenames
5734 and fields. */
5735 gcc_assert (storage_class == csc_none && !threadp
5736 && !declspecs->inline_p);
5737 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5738 && type_quals)
5739 pedwarn (loc, OPT_pedantic,
5740 "ISO C forbids const or volatile function types");
5741 if (type_quals)
5742 type = c_build_qualified_type (type, type_quals);
5743 return type;
5746 if (pedantic && decl_context == FIELD
5747 && variably_modified_type_p (type, NULL_TREE))
5749 /* C99 6.7.2.1p8 */
5750 pedwarn (loc, OPT_pedantic, "a member of a structure or union cannot "
5751 "have a variably modified type");
5754 /* Aside from typedefs and type names (handle above),
5755 `void' at top level (not within pointer)
5756 is allowed only in public variables.
5757 We don't complain about parms either, but that is because
5758 a better error message can be made later. */
5760 if (VOID_TYPE_P (type) && decl_context != PARM
5761 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
5762 && (storage_class == csc_extern
5763 || (current_scope == file_scope
5764 && !(storage_class == csc_static
5765 || storage_class == csc_register)))))
5767 error_at (loc, "variable or field %qE declared void", name);
5768 type = integer_type_node;
5771 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
5772 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
5775 tree decl;
5777 if (decl_context == PARM)
5779 tree promoted_type;
5781 /* A parameter declared as an array of T is really a pointer to T.
5782 One declared as a function is really a pointer to a function. */
5784 if (TREE_CODE (type) == ARRAY_TYPE)
5786 /* Transfer const-ness of array into that of type pointed to. */
5787 type = TREE_TYPE (type);
5788 if (type_quals)
5789 type = c_build_qualified_type (type, type_quals);
5790 type = build_pointer_type (type);
5791 type_quals = array_ptr_quals;
5792 if (type_quals)
5793 type = c_build_qualified_type (type, type_quals);
5795 /* We don't yet implement attributes in this context. */
5796 if (array_ptr_attrs != NULL_TREE)
5797 warning_at (loc, OPT_Wattributes,
5798 "attributes in parameter array declarator ignored");
5800 size_varies = false;
5802 else if (TREE_CODE (type) == FUNCTION_TYPE)
5804 if (type_quals)
5805 pedwarn (loc, OPT_pedantic,
5806 "ISO C forbids qualified function types");
5807 if (type_quals)
5808 type = c_build_qualified_type (type, type_quals);
5809 type = build_pointer_type (type);
5810 type_quals = TYPE_UNQUALIFIED;
5812 else if (type_quals)
5813 type = c_build_qualified_type (type, type_quals);
5815 decl = build_decl (declarator->id_loc,
5816 PARM_DECL, declarator->u.id, type);
5817 if (size_varies)
5818 C_DECL_VARIABLE_SIZE (decl) = 1;
5820 /* Compute the type actually passed in the parmlist,
5821 for the case where there is no prototype.
5822 (For example, shorts and chars are passed as ints.)
5823 When there is a prototype, this is overridden later. */
5825 if (type == error_mark_node)
5826 promoted_type = type;
5827 else
5828 promoted_type = c_type_promotes_to (type);
5830 DECL_ARG_TYPE (decl) = promoted_type;
5831 if (declspecs->inline_p)
5832 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
5834 else if (decl_context == FIELD)
5836 /* Note that the grammar rejects storage classes in typenames
5837 and fields. */
5838 gcc_assert (storage_class == csc_none && !threadp
5839 && !declspecs->inline_p);
5841 /* Structure field. It may not be a function. */
5843 if (TREE_CODE (type) == FUNCTION_TYPE)
5845 error_at (loc, "field %qE declared as a function", name);
5846 type = build_pointer_type (type);
5848 else if (TREE_CODE (type) != ERROR_MARK
5849 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
5851 if (name)
5852 error_at (loc, "field %qE has incomplete type", name);
5853 else
5854 error_at (loc, "unnamed field has incomplete type");
5855 type = error_mark_node;
5857 type = c_build_qualified_type (type, type_quals);
5858 decl = build_decl (declarator->id_loc,
5859 FIELD_DECL, declarator->u.id, type);
5860 DECL_NONADDRESSABLE_P (decl) = bitfield;
5861 if (bitfield && !declarator->u.id)
5862 TREE_NO_WARNING (decl) = 1;
5864 if (size_varies)
5865 C_DECL_VARIABLE_SIZE (decl) = 1;
5867 else if (TREE_CODE (type) == FUNCTION_TYPE)
5869 if (storage_class == csc_register || threadp)
5871 error_at (loc, "invalid storage class for function %qE", name);
5873 else if (current_scope != file_scope)
5875 /* Function declaration not at file scope. Storage
5876 classes other than `extern' are not allowed, C99
5877 6.7.1p5, and `extern' makes no difference. However,
5878 GCC allows 'auto', perhaps with 'inline', to support
5879 nested functions. */
5880 if (storage_class == csc_auto)
5881 pedwarn (loc, OPT_pedantic,
5882 "invalid storage class for function %qE", name);
5883 else if (storage_class == csc_static)
5885 error_at (loc, "invalid storage class for function %qE", name);
5886 if (funcdef_flag)
5887 storage_class = declspecs->storage_class = csc_none;
5888 else
5889 return 0;
5893 decl = build_decl (declarator->id_loc,
5894 FUNCTION_DECL, declarator->u.id, type);
5895 decl = build_decl_attribute_variant (decl, decl_attr);
5897 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
5898 pedwarn (loc, OPT_pedantic,
5899 "ISO C forbids qualified function types");
5901 /* GNU C interprets a volatile-qualified function type to indicate
5902 that the function does not return. */
5903 if ((type_quals & TYPE_QUAL_VOLATILE)
5904 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5905 warning_at (loc, 0, "%<noreturn%> function returns non-void value");
5907 /* Every function declaration is an external reference
5908 (DECL_EXTERNAL) except for those which are not at file
5909 scope and are explicitly declared "auto". This is
5910 forbidden by standard C (C99 6.7.1p5) and is interpreted by
5911 GCC to signify a forward declaration of a nested function. */
5912 if (storage_class == csc_auto && current_scope != file_scope)
5913 DECL_EXTERNAL (decl) = 0;
5914 /* In C99, a function which is declared 'inline' with 'extern'
5915 is not an external reference (which is confusing). It
5916 means that the later definition of the function must be output
5917 in this file, C99 6.7.4p6. In GNU C89, a function declared
5918 'extern inline' is an external reference. */
5919 else if (declspecs->inline_p && storage_class != csc_static)
5920 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
5921 == flag_gnu89_inline);
5922 else
5923 DECL_EXTERNAL (decl) = !initialized;
5925 /* Record absence of global scope for `static' or `auto'. */
5926 TREE_PUBLIC (decl)
5927 = !(storage_class == csc_static || storage_class == csc_auto);
5929 /* For a function definition, record the argument information
5930 block where store_parm_decls will look for it. */
5931 if (funcdef_flag)
5932 current_function_arg_info = arg_info;
5934 if (declspecs->default_int_p)
5935 C_FUNCTION_IMPLICIT_INT (decl) = 1;
5937 /* Record presence of `inline', if it is reasonable. */
5938 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
5940 if (declspecs->inline_p)
5941 pedwarn (loc, 0, "cannot inline function %<main%>");
5943 else if (declspecs->inline_p)
5944 /* Record that the function is declared `inline'. */
5945 DECL_DECLARED_INLINE_P (decl) = 1;
5947 else
5949 /* It's a variable. */
5950 /* An uninitialized decl with `extern' is a reference. */
5951 int extern_ref = !initialized && storage_class == csc_extern;
5953 type = c_build_qualified_type (type, type_quals);
5955 /* C99 6.2.2p7: It is invalid (compile-time undefined
5956 behavior) to create an 'extern' declaration for a
5957 variable if there is a global declaration that is
5958 'static' and the global declaration is not visible.
5959 (If the static declaration _is_ currently visible,
5960 the 'extern' declaration is taken to refer to that decl.) */
5961 if (extern_ref && current_scope != file_scope)
5963 tree global_decl = identifier_global_value (declarator->u.id);
5964 tree visible_decl = lookup_name (declarator->u.id);
5966 if (global_decl
5967 && global_decl != visible_decl
5968 && TREE_CODE (global_decl) == VAR_DECL
5969 && !TREE_PUBLIC (global_decl))
5970 error_at (loc, "variable previously declared %<static%> "
5971 "redeclared %<extern%>");
5974 decl = build_decl (declarator->id_loc,
5975 VAR_DECL, declarator->u.id, type);
5976 if (size_varies)
5977 C_DECL_VARIABLE_SIZE (decl) = 1;
5979 if (declspecs->inline_p)
5980 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
5982 /* At file scope, an initialized extern declaration may follow
5983 a static declaration. In that case, DECL_EXTERNAL will be
5984 reset later in start_decl. */
5985 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
5987 /* At file scope, the presence of a `static' or `register' storage
5988 class specifier, or the absence of all storage class specifiers
5989 makes this declaration a definition (perhaps tentative). Also,
5990 the absence of `static' makes it public. */
5991 if (current_scope == file_scope)
5993 TREE_PUBLIC (decl) = storage_class != csc_static;
5994 TREE_STATIC (decl) = !extern_ref;
5996 /* Not at file scope, only `static' makes a static definition. */
5997 else
5999 TREE_STATIC (decl) = (storage_class == csc_static);
6000 TREE_PUBLIC (decl) = extern_ref;
6003 if (threadp)
6004 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
6007 if ((storage_class == csc_extern
6008 || (storage_class == csc_none
6009 && TREE_CODE (type) == FUNCTION_TYPE
6010 && !funcdef_flag))
6011 && variably_modified_type_p (type, NULL_TREE))
6013 /* C99 6.7.5.2p2 */
6014 if (TREE_CODE (type) == FUNCTION_TYPE)
6015 error_at (loc, "non-nested function with variably modified type");
6016 else
6017 error_at (loc, "object with variably modified type must have "
6018 "no linkage");
6021 /* Record `register' declaration for warnings on &
6022 and in case doing stupid register allocation. */
6024 if (storage_class == csc_register)
6026 C_DECL_REGISTER (decl) = 1;
6027 DECL_REGISTER (decl) = 1;
6030 /* Record constancy and volatility. */
6031 c_apply_type_quals_to_decl (type_quals, decl);
6033 /* If a type has volatile components, it should be stored in memory.
6034 Otherwise, the fact that those components are volatile
6035 will be ignored, and would even crash the compiler.
6036 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6037 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6038 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
6039 || TREE_CODE (decl) == RESULT_DECL))
6041 /* It is not an error for a structure with volatile fields to
6042 be declared register, but reset DECL_REGISTER since it
6043 cannot actually go in a register. */
6044 int was_reg = C_DECL_REGISTER (decl);
6045 C_DECL_REGISTER (decl) = 0;
6046 DECL_REGISTER (decl) = 0;
6047 c_mark_addressable (decl);
6048 C_DECL_REGISTER (decl) = was_reg;
6051 /* This is the earliest point at which we might know the assembler
6052 name of a variable. Thus, if it's known before this, die horribly. */
6053 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
6055 if (warn_cxx_compat
6056 && TREE_CODE (decl) == VAR_DECL
6057 && TREE_PUBLIC (decl)
6058 && TREE_STATIC (decl)
6059 && (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6060 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6061 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6062 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
6063 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6064 ("non-local variable %qD with anonymous type is "
6065 "questionable in C++"),
6066 decl);
6068 return decl;
6072 /* Decode the parameter-list info for a function type or function definition.
6073 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6074 if there is an identifier list instead of a parameter decl list).
6075 These two functions are separate because when a function returns
6076 or receives functions then each is called multiple times but the order
6077 of calls is different. The last call to `grokparms' is always the one
6078 that contains the formal parameter names of a function definition.
6080 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6082 FUNCDEF_FLAG is true for a function definition, false for
6083 a mere declaration. A nonempty identifier-list gets an error message
6084 when FUNCDEF_FLAG is false. */
6086 static tree
6087 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
6089 tree arg_types = arg_info->types;
6091 if (funcdef_flag && arg_info->had_vla_unspec)
6093 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6094 /* C99 6.7.5.2p4 */
6095 error ("%<[*]%> not allowed in other than function prototype scope");
6098 if (arg_types == 0 && !funcdef_flag && !in_system_header)
6099 warning (OPT_Wstrict_prototypes,
6100 "function declaration isn%'t a prototype");
6102 if (arg_types == error_mark_node)
6103 return 0; /* don't set TYPE_ARG_TYPES in this case */
6105 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
6107 if (!funcdef_flag)
6108 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
6110 arg_info->parms = arg_info->types;
6111 arg_info->types = 0;
6112 return 0;
6114 else
6116 tree parm, type, typelt;
6117 unsigned int parmno;
6118 const char *errmsg;
6120 /* If there is a parameter of incomplete type in a definition,
6121 this is an error. In a declaration this is valid, and a
6122 struct or union type may be completed later, before any calls
6123 or definition of the function. In the case where the tag was
6124 first declared within the parameter list, a warning has
6125 already been given. If a parameter has void type, then
6126 however the function cannot be defined or called, so
6127 warn. */
6129 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
6130 parm;
6131 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
6133 type = TREE_VALUE (typelt);
6134 if (type == error_mark_node)
6135 continue;
6137 if (!COMPLETE_TYPE_P (type))
6139 if (funcdef_flag)
6141 if (DECL_NAME (parm))
6142 error_at (input_location,
6143 "parameter %u (%q+D) has incomplete type",
6144 parmno, parm);
6145 else
6146 error_at (DECL_SOURCE_LOCATION (parm),
6147 "parameter %u has incomplete type",
6148 parmno);
6150 TREE_VALUE (typelt) = error_mark_node;
6151 TREE_TYPE (parm) = error_mark_node;
6152 arg_types = NULL_TREE;
6154 else if (VOID_TYPE_P (type))
6156 if (DECL_NAME (parm))
6157 warning_at (input_location, 0,
6158 "parameter %u (%q+D) has void type",
6159 parmno, parm);
6160 else
6161 warning_at (DECL_SOURCE_LOCATION (parm), 0,
6162 "parameter %u has void type",
6163 parmno);
6167 errmsg = targetm.invalid_parameter_type (type);
6168 if (errmsg)
6170 error (errmsg);
6171 TREE_VALUE (typelt) = error_mark_node;
6172 TREE_TYPE (parm) = error_mark_node;
6173 arg_types = NULL_TREE;
6176 if (DECL_NAME (parm) && TREE_USED (parm))
6177 warn_if_shadowing (parm);
6179 return arg_types;
6183 /* Take apart the current scope and return a c_arg_info structure with
6184 info on a parameter list just parsed.
6186 This structure is later fed to 'grokparms' and 'store_parm_decls'.
6188 ELLIPSIS being true means the argument list ended in '...' so don't
6189 append a sentinel (void_list_node) to the end of the type-list. */
6191 struct c_arg_info *
6192 get_parm_info (bool ellipsis)
6194 struct c_binding *b = current_scope->bindings;
6195 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
6196 struct c_arg_info);
6197 tree parms = 0;
6198 tree tags = 0;
6199 tree types = 0;
6200 tree others = 0;
6202 static bool explained_incomplete_types = false;
6203 bool gave_void_only_once_err = false;
6205 arg_info->parms = 0;
6206 arg_info->tags = 0;
6207 arg_info->types = 0;
6208 arg_info->others = 0;
6209 arg_info->pending_sizes = 0;
6210 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
6212 /* The bindings in this scope must not get put into a block.
6213 We will take care of deleting the binding nodes. */
6214 current_scope->bindings = 0;
6216 /* This function is only called if there was *something* on the
6217 parameter list. */
6218 gcc_assert (b);
6220 /* A parameter list consisting solely of 'void' indicates that the
6221 function takes no arguments. But if the 'void' is qualified
6222 (by 'const' or 'volatile'), or has a storage class specifier
6223 ('register'), then the behavior is undefined; issue an error.
6224 Typedefs for 'void' are OK (see DR#157). */
6225 if (b->prev == 0 /* one binding */
6226 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
6227 && !DECL_NAME (b->decl) /* anonymous */
6228 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
6230 if (TREE_THIS_VOLATILE (b->decl)
6231 || TREE_READONLY (b->decl)
6232 || C_DECL_REGISTER (b->decl))
6233 error ("%<void%> as only parameter may not be qualified");
6235 /* There cannot be an ellipsis. */
6236 if (ellipsis)
6237 error ("%<void%> must be the only parameter");
6239 arg_info->types = void_list_node;
6240 return arg_info;
6243 if (!ellipsis)
6244 types = void_list_node;
6246 /* Break up the bindings list into parms, tags, types, and others;
6247 apply sanity checks; purge the name-to-decl bindings. */
6248 while (b)
6250 tree decl = b->decl;
6251 tree type = TREE_TYPE (decl);
6252 const char *keyword;
6254 switch (TREE_CODE (decl))
6256 case PARM_DECL:
6257 if (b->id)
6259 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6260 I_SYMBOL_BINDING (b->id) = b->shadowed;
6263 /* Check for forward decls that never got their actual decl. */
6264 if (TREE_ASM_WRITTEN (decl))
6265 error ("parameter %q+D has just a forward declaration", decl);
6266 /* Check for (..., void, ...) and issue an error. */
6267 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
6269 if (!gave_void_only_once_err)
6271 error ("%<void%> must be the only parameter");
6272 gave_void_only_once_err = true;
6275 else
6277 /* Valid parameter, add it to the list. */
6278 TREE_CHAIN (decl) = parms;
6279 parms = decl;
6281 /* Since there is a prototype, args are passed in their
6282 declared types. The back end may override this later. */
6283 DECL_ARG_TYPE (decl) = type;
6284 types = tree_cons (0, type, types);
6286 break;
6288 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
6289 case UNION_TYPE: keyword = "union"; goto tag;
6290 case RECORD_TYPE: keyword = "struct"; goto tag;
6291 tag:
6292 /* Types may not have tag-names, in which case the type
6293 appears in the bindings list with b->id NULL. */
6294 if (b->id)
6296 gcc_assert (I_TAG_BINDING (b->id) == b);
6297 I_TAG_BINDING (b->id) = b->shadowed;
6300 /* Warn about any struct, union or enum tags defined in a
6301 parameter list. The scope of such types is limited to
6302 the parameter list, which is rarely if ever desirable
6303 (it's impossible to call such a function with type-
6304 correct arguments). An anonymous union parm type is
6305 meaningful as a GNU extension, so don't warn for that. */
6306 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
6308 if (b->id)
6309 /* The %s will be one of 'struct', 'union', or 'enum'. */
6310 warning (0, "%<%s %E%> declared inside parameter list",
6311 keyword, b->id);
6312 else
6313 /* The %s will be one of 'struct', 'union', or 'enum'. */
6314 warning (0, "anonymous %s declared inside parameter list",
6315 keyword);
6317 if (!explained_incomplete_types)
6319 warning (0, "its scope is only this definition or declaration,"
6320 " which is probably not what you want");
6321 explained_incomplete_types = true;
6325 tags = tree_cons (b->id, decl, tags);
6326 break;
6328 case CONST_DECL:
6329 case TYPE_DECL:
6330 case FUNCTION_DECL:
6331 /* CONST_DECLs appear here when we have an embedded enum,
6332 and TYPE_DECLs appear here when we have an embedded struct
6333 or union. No warnings for this - we already warned about the
6334 type itself. FUNCTION_DECLs appear when there is an implicit
6335 function declaration in the parameter list. */
6337 /* When we reinsert this decl in the function body, we need
6338 to reconstruct whether it was marked as nested. */
6339 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
6340 ? b->nested
6341 : !b->nested);
6342 TREE_CHAIN (decl) = others;
6343 others = decl;
6344 /* fall through */
6346 case ERROR_MARK:
6347 /* error_mark_node appears here when we have an undeclared
6348 variable. Just throw it away. */
6349 if (b->id)
6351 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6352 I_SYMBOL_BINDING (b->id) = b->shadowed;
6354 break;
6356 /* Other things that might be encountered. */
6357 case LABEL_DECL:
6358 case VAR_DECL:
6359 default:
6360 gcc_unreachable ();
6363 b = free_binding_and_advance (b);
6366 arg_info->parms = parms;
6367 arg_info->tags = tags;
6368 arg_info->types = types;
6369 arg_info->others = others;
6370 arg_info->pending_sizes = get_pending_sizes ();
6371 return arg_info;
6374 /* Get the struct, enum or union (CODE says which) with tag NAME.
6375 Define the tag as a forward-reference with location LOC if it is
6376 not defined. Return a c_typespec structure for the type
6377 specifier. */
6379 struct c_typespec
6380 parser_xref_tag (location_t loc, enum tree_code code, tree name)
6382 struct c_typespec ret;
6383 tree ref;
6384 location_t refloc;
6386 ret.expr = NULL_TREE;
6387 ret.expr_const_operands = true;
6389 /* If a cross reference is requested, look up the type
6390 already defined for this tag and return it. */
6392 ref = lookup_tag (code, name, 0, &refloc);
6393 /* If this is the right type of tag, return what we found.
6394 (This reference will be shadowed by shadow_tag later if appropriate.)
6395 If this is the wrong type of tag, do not return it. If it was the
6396 wrong type in the same scope, we will have had an error
6397 message already; if in a different scope and declaring
6398 a name, pending_xref_error will give an error message; but if in a
6399 different scope and not declaring a name, this tag should
6400 shadow the previous declaration of a different type of tag, and
6401 this would not work properly if we return the reference found.
6402 (For example, with "struct foo" in an outer scope, "union foo;"
6403 must shadow that tag with a new one of union type.) */
6404 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
6405 if (ref && TREE_CODE (ref) == code)
6407 if (C_TYPE_DEFINED_IN_STRUCT (ref)
6408 && loc != UNKNOWN_LOCATION
6409 && warn_cxx_compat)
6411 switch (code)
6413 case ENUMERAL_TYPE:
6414 warning_at (loc, OPT_Wc___compat,
6415 ("enum type defined in struct or union "
6416 "is not visible in C++"));
6417 inform (refloc, "enum type defined here");
6418 break;
6419 case RECORD_TYPE:
6420 warning_at (loc, OPT_Wc___compat,
6421 ("struct defined in struct or union "
6422 "is not visible in C++"));
6423 inform (refloc, "struct defined here");
6424 break;
6425 case UNION_TYPE:
6426 warning_at (loc, OPT_Wc___compat,
6427 ("union defined in struct or union "
6428 "is not visible in C++"));
6429 inform (refloc, "union defined here");
6430 break;
6431 default:
6432 gcc_unreachable();
6436 ret.spec = ref;
6437 return ret;
6440 /* If no such tag is yet defined, create a forward-reference node
6441 and record it as the "definition".
6442 When a real declaration of this type is found,
6443 the forward-reference will be altered into a real type. */
6445 ref = make_node (code);
6446 if (code == ENUMERAL_TYPE)
6448 /* Give the type a default layout like unsigned int
6449 to avoid crashing if it does not get defined. */
6450 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
6451 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
6452 TYPE_USER_ALIGN (ref) = 0;
6453 TYPE_UNSIGNED (ref) = 1;
6454 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
6455 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
6456 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
6459 pushtag (loc, name, ref);
6461 ret.spec = ref;
6462 return ret;
6465 /* Get the struct, enum or union (CODE says which) with tag NAME.
6466 Define the tag as a forward-reference if it is not defined.
6467 Return a tree for the type. */
6469 tree
6470 xref_tag (enum tree_code code, tree name)
6472 return parser_xref_tag (input_location, code, name).spec;
6475 /* Make sure that the tag NAME is defined *in the current scope*
6476 at least as a forward reference.
6477 LOC is the location of the struct's definition.
6478 CODE says which kind of tag NAME ought to be.
6480 This stores the current value of the file static STRUCT_PARSE_INFO
6481 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
6482 new c_struct_parse_info structure. The old value of
6483 STRUCT_PARSE_INFO is restored in finish_struct. */
6485 tree
6486 start_struct (location_t loc, enum tree_code code, tree name,
6487 struct c_struct_parse_info **enclosing_struct_parse_info)
6489 /* If there is already a tag defined at this scope
6490 (as a forward reference), just return it. */
6492 tree ref = NULL_TREE;
6493 location_t refloc = UNKNOWN_LOCATION;
6495 if (name != NULL_TREE)
6496 ref = lookup_tag (code, name, 1, &refloc);
6497 if (ref && TREE_CODE (ref) == code)
6499 if (TYPE_SIZE (ref))
6501 if (code == UNION_TYPE)
6502 error_at (loc, "redefinition of %<union %E%>", name);
6503 else
6504 error_at (loc, "redefinition of %<struct %E%>", name);
6505 if (refloc != UNKNOWN_LOCATION)
6506 inform (refloc, "originally defined here");
6507 /* Don't create structures using a name already in use. */
6508 ref = NULL_TREE;
6510 else if (C_TYPE_BEING_DEFINED (ref))
6512 if (code == UNION_TYPE)
6513 error_at (loc, "nested redefinition of %<union %E%>", name);
6514 else
6515 error_at (loc, "nested redefinition of %<struct %E%>", name);
6516 /* Don't bother to report "originally defined here" for a
6517 nested redefinition; the original definition should be
6518 obvious. */
6519 /* Don't create structures that contain themselves. */
6520 ref = NULL_TREE;
6524 /* Otherwise create a forward-reference just so the tag is in scope. */
6526 if (ref == NULL_TREE || TREE_CODE (ref) != code)
6528 ref = make_node (code);
6529 pushtag (loc, name, ref);
6532 C_TYPE_BEING_DEFINED (ref) = 1;
6533 TYPE_PACKED (ref) = flag_pack_struct;
6535 *enclosing_struct_parse_info = struct_parse_info;
6536 struct_parse_info = XNEW (struct c_struct_parse_info);
6537 struct_parse_info->struct_types = VEC_alloc (tree, heap, 0);
6538 struct_parse_info->fields = VEC_alloc (c_binding_ptr, heap, 0);
6539 struct_parse_info->typedefs_seen = VEC_alloc (tree, heap, 0);
6541 /* FIXME: This will issue a warning for a use of a type defined
6542 within a statement expr used within sizeof, et. al. This is not
6543 terribly serious as C++ doesn't permit statement exprs within
6544 sizeof anyhow. */
6545 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
6546 warning_at (loc, OPT_Wc___compat,
6547 "defining type in %qs expression is invalid in C++",
6548 (in_sizeof
6549 ? "sizeof"
6550 : (in_typeof ? "typeof" : "alignof")));
6552 return ref;
6555 /* Process the specs, declarator and width (NULL if omitted)
6556 of a structure component, returning a FIELD_DECL node.
6557 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
6558 DECL_ATTRS is as for grokdeclarator.
6560 LOC is the location of the structure component.
6562 This is done during the parsing of the struct declaration.
6563 The FIELD_DECL nodes are chained together and the lot of them
6564 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
6566 tree
6567 grokfield (location_t loc,
6568 struct c_declarator *declarator, struct c_declspecs *declspecs,
6569 tree width, tree *decl_attrs)
6571 tree value;
6573 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
6574 && width == NULL_TREE)
6576 /* This is an unnamed decl.
6578 If we have something of the form "union { list } ;" then this
6579 is the anonymous union extension. Similarly for struct.
6581 If this is something of the form "struct foo;", then
6582 If MS extensions are enabled, this is handled as an
6583 anonymous struct.
6584 Otherwise this is a forward declaration of a structure tag.
6586 If this is something of the form "foo;" and foo is a TYPE_DECL, then
6587 If MS extensions are enabled and foo names a structure, then
6588 again this is an anonymous struct.
6589 Otherwise this is an error.
6591 Oh what a horrid tangled web we weave. I wonder if MS consciously
6592 took this from Plan 9 or if it was an accident of implementation
6593 that took root before someone noticed the bug... */
6595 tree type = declspecs->type;
6596 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
6597 || TREE_CODE (type) == UNION_TYPE);
6598 bool ok = false;
6600 if (type_ok
6601 && (flag_ms_extensions || !declspecs->typedef_p))
6603 if (flag_ms_extensions)
6604 ok = true;
6605 else if (flag_iso)
6606 ok = false;
6607 else if (TYPE_NAME (type) == NULL)
6608 ok = true;
6609 else
6610 ok = false;
6612 if (!ok)
6614 pedwarn (loc, 0, "declaration does not declare anything");
6615 return NULL_TREE;
6617 pedwarn (loc, OPT_pedantic, "ISO C doesn%'t support unnamed structs/unions");
6620 value = grokdeclarator (declarator, declspecs, FIELD, false,
6621 width ? &width : NULL, decl_attrs, NULL, NULL,
6622 DEPRECATED_NORMAL);
6624 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6625 DECL_INITIAL (value) = width;
6627 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
6629 /* If we currently have a binding for this field, set the
6630 in_struct field in the binding, so that we warn about lookups
6631 which find it. */
6632 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
6633 if (b != NULL)
6635 /* If the in_struct field is not yet set, push it on a list
6636 to be cleared when this struct is finished. */
6637 if (!b->in_struct)
6639 VEC_safe_push (c_binding_ptr, heap,
6640 struct_parse_info->fields, b);
6641 b->in_struct = 1;
6646 return value;
6649 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
6650 to HTAB, giving errors for any duplicates. */
6652 static void
6653 detect_field_duplicates_hash (tree fieldlist, htab_t htab)
6655 tree x, y;
6656 void **slot;
6658 for (x = fieldlist; x ; x = TREE_CHAIN (x))
6659 if ((y = DECL_NAME (x)) != 0)
6661 slot = htab_find_slot (htab, y, INSERT);
6662 if (*slot)
6664 error ("duplicate member %q+D", x);
6665 DECL_NAME (x) = NULL_TREE;
6667 *slot = y;
6669 else if (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6670 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
6671 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
6674 /* Generate an error for any duplicate field names in FIELDLIST. Munge
6675 the list such that this does not present a problem later. */
6677 static void
6678 detect_field_duplicates (tree fieldlist)
6680 tree x, y;
6681 int timeout = 10;
6683 /* First, see if there are more than "a few" fields.
6684 This is trivially true if there are zero or one fields. */
6685 if (!fieldlist)
6686 return;
6687 x = TREE_CHAIN (fieldlist);
6688 if (!x)
6689 return;
6690 do {
6691 timeout--;
6692 if (DECL_NAME (x) == NULL_TREE
6693 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6694 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
6695 timeout = 0;
6696 x = TREE_CHAIN (x);
6697 } while (timeout > 0 && x);
6699 /* If there were "few" fields and no anonymous structures or unions,
6700 avoid the overhead of allocating a hash table. Instead just do
6701 the nested traversal thing. */
6702 if (timeout > 0)
6704 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
6705 if (DECL_NAME (x))
6707 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
6708 if (DECL_NAME (y) == DECL_NAME (x))
6710 error ("duplicate member %q+D", x);
6711 DECL_NAME (x) = NULL_TREE;
6715 else
6717 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
6719 detect_field_duplicates_hash (fieldlist, htab);
6720 htab_delete (htab);
6724 /* Finish up struct info used by -Wc++-compat. */
6726 static void
6727 warn_cxx_compat_finish_struct (tree fieldlist)
6729 unsigned int ix;
6730 tree x;
6731 struct c_binding *b;
6733 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
6734 the current struct. We do this now at the end of the struct
6735 because the flag is used to issue visibility warnings, and we
6736 only want to issue those warnings if the type is referenced
6737 outside of the struct declaration. */
6738 for (ix = 0; VEC_iterate (tree, struct_parse_info->struct_types, ix, x); ++ix)
6739 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
6741 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
6742 typedefs used when declaring fields in this struct. If the name
6743 of any of the fields is also a typedef name then the struct would
6744 not parse in C++, because the C++ lookup rules say that the
6745 typedef name would be looked up in the context of the struct, and
6746 would thus be the field rather than the typedef. */
6747 if (!VEC_empty (tree, struct_parse_info->typedefs_seen)
6748 && fieldlist != NULL_TREE)
6750 /* Use a pointer_set using the name of the typedef. We can use
6751 a pointer_set because identifiers are interned. */
6752 struct pointer_set_t *tset = pointer_set_create ();
6754 for (ix = 0;
6755 VEC_iterate (tree, struct_parse_info->typedefs_seen, ix, x);
6756 ++ix)
6757 pointer_set_insert (tset, DECL_NAME (x));
6759 for (x = fieldlist; x != NULL_TREE; x = TREE_CHAIN (x))
6761 if (pointer_set_contains (tset, DECL_NAME (x)))
6763 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
6764 ("using %qD as both field and typedef name is "
6765 "invalid in C++"),
6767 /* FIXME: It would be nice to report the location where
6768 the typedef name is used. */
6772 pointer_set_destroy (tset);
6775 /* For each field which has a binding and which was not defined in
6776 an enclosing struct, clear the in_struct field. */
6777 for (ix = 0;
6778 VEC_iterate (c_binding_ptr, struct_parse_info->fields, ix, b);
6779 ++ix)
6780 b->in_struct = 0;
6783 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
6784 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
6785 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
6786 ATTRIBUTES are attributes to be applied to the structure.
6788 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
6789 the struct was started. */
6791 tree
6792 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
6793 struct c_struct_parse_info *enclosing_struct_parse_info)
6795 tree x;
6796 bool toplevel = file_scope == current_scope;
6797 int saw_named_field;
6799 /* If this type was previously laid out as a forward reference,
6800 make sure we lay it out again. */
6802 TYPE_SIZE (t) = 0;
6804 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6806 if (pedantic)
6808 for (x = fieldlist; x; x = TREE_CHAIN (x))
6809 if (DECL_NAME (x) != 0)
6810 break;
6812 if (x == 0)
6814 if (TREE_CODE (t) == UNION_TYPE)
6816 if (fieldlist)
6817 pedwarn (loc, OPT_pedantic, "union has no named members");
6818 else
6819 pedwarn (loc, OPT_pedantic, "union has no members");
6821 else
6823 if (fieldlist)
6824 pedwarn (loc, OPT_pedantic, "struct has no named members");
6825 else
6826 pedwarn (loc, OPT_pedantic, "struct has no members");
6831 /* Install struct as DECL_CONTEXT of each field decl.
6832 Also process specified field sizes, found in the DECL_INITIAL,
6833 storing 0 there after the type has been changed to precision equal
6834 to its width, rather than the precision of the specified standard
6835 type. (Correct layout requires the original type to have been preserved
6836 until now.) */
6838 saw_named_field = 0;
6839 for (x = fieldlist; x; x = TREE_CHAIN (x))
6841 if (TREE_TYPE (x) == error_mark_node)
6842 continue;
6844 DECL_CONTEXT (x) = t;
6846 /* If any field is const, the structure type is pseudo-const. */
6847 if (TREE_READONLY (x))
6848 C_TYPE_FIELDS_READONLY (t) = 1;
6849 else
6851 /* A field that is pseudo-const makes the structure likewise. */
6852 tree t1 = TREE_TYPE (x);
6853 while (TREE_CODE (t1) == ARRAY_TYPE)
6854 t1 = TREE_TYPE (t1);
6855 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
6856 && C_TYPE_FIELDS_READONLY (t1))
6857 C_TYPE_FIELDS_READONLY (t) = 1;
6860 /* Any field that is volatile means variables of this type must be
6861 treated in some ways as volatile. */
6862 if (TREE_THIS_VOLATILE (x))
6863 C_TYPE_FIELDS_VOLATILE (t) = 1;
6865 /* Any field of nominal variable size implies structure is too. */
6866 if (C_DECL_VARIABLE_SIZE (x))
6867 C_TYPE_VARIABLE_SIZE (t) = 1;
6869 if (DECL_INITIAL (x))
6871 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
6872 DECL_SIZE (x) = bitsize_int (width);
6873 DECL_BIT_FIELD (x) = 1;
6874 SET_DECL_C_BIT_FIELD (x);
6877 if (TYPE_PACKED (t)
6878 && (DECL_BIT_FIELD (x)
6879 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
6880 DECL_PACKED (x) = 1;
6882 /* Detect flexible array member in an invalid context. */
6883 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6884 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
6885 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
6886 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
6888 if (TREE_CODE (t) == UNION_TYPE)
6890 error_at (DECL_SOURCE_LOCATION (x),
6891 "flexible array member in union");
6892 TREE_TYPE (x) = error_mark_node;
6894 else if (TREE_CHAIN (x) != NULL_TREE)
6896 error_at (DECL_SOURCE_LOCATION (x),
6897 "flexible array member not at end of struct");
6898 TREE_TYPE (x) = error_mark_node;
6900 else if (!saw_named_field)
6902 error_at (DECL_SOURCE_LOCATION (x),
6903 "flexible array member in otherwise empty struct");
6904 TREE_TYPE (x) = error_mark_node;
6908 if (pedantic && TREE_CODE (t) == RECORD_TYPE
6909 && flexible_array_type_p (TREE_TYPE (x)))
6910 pedwarn (DECL_SOURCE_LOCATION (x), OPT_pedantic,
6911 "invalid use of structure with flexible array member");
6913 if (DECL_NAME (x))
6914 saw_named_field = 1;
6917 detect_field_duplicates (fieldlist);
6919 /* Now we have the nearly final fieldlist. Record it,
6920 then lay out the structure or union (including the fields). */
6922 TYPE_FIELDS (t) = fieldlist;
6924 layout_type (t);
6926 /* Give bit-fields their proper types. */
6928 tree *fieldlistp = &fieldlist;
6929 while (*fieldlistp)
6930 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
6931 && TREE_TYPE (*fieldlistp) != error_mark_node)
6933 unsigned HOST_WIDE_INT width
6934 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
6935 tree type = TREE_TYPE (*fieldlistp);
6936 if (width != TYPE_PRECISION (type))
6938 TREE_TYPE (*fieldlistp)
6939 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
6940 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
6942 DECL_INITIAL (*fieldlistp) = 0;
6944 else
6945 fieldlistp = &TREE_CHAIN (*fieldlistp);
6948 /* Now we have the truly final field list.
6949 Store it in this type and in the variants. */
6951 TYPE_FIELDS (t) = fieldlist;
6953 /* If there are lots of fields, sort so we can look through them fast.
6954 We arbitrarily consider 16 or more elts to be "a lot". */
6957 int len = 0;
6959 for (x = fieldlist; x; x = TREE_CHAIN (x))
6961 if (len > 15 || DECL_NAME (x) == NULL)
6962 break;
6963 len += 1;
6966 if (len > 15)
6968 tree *field_array;
6969 struct lang_type *space;
6970 struct sorted_fields_type *space2;
6972 len += list_length (x);
6974 /* Use the same allocation policy here that make_node uses, to
6975 ensure that this lives as long as the rest of the struct decl.
6976 All decls in an inline function need to be saved. */
6978 space = GGC_CNEW (struct lang_type);
6979 space2 = GGC_NEWVAR (struct sorted_fields_type,
6980 sizeof (struct sorted_fields_type) + len * sizeof (tree));
6982 len = 0;
6983 space->s = space2;
6984 field_array = &space2->elts[0];
6985 for (x = fieldlist; x; x = TREE_CHAIN (x))
6987 field_array[len++] = x;
6989 /* If there is anonymous struct or union, break out of the loop. */
6990 if (DECL_NAME (x) == NULL)
6991 break;
6993 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
6994 if (x == NULL)
6996 TYPE_LANG_SPECIFIC (t) = space;
6997 TYPE_LANG_SPECIFIC (t)->s->len = len;
6998 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
6999 qsort (field_array, len, sizeof (tree), field_decl_cmp);
7004 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7006 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7007 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
7008 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
7009 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
7010 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
7013 /* If this was supposed to be a transparent union, but we can't
7014 make it one, warn and turn off the flag. */
7015 if (TREE_CODE (t) == UNION_TYPE
7016 && TYPE_TRANSPARENT_AGGR (t)
7017 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
7019 TYPE_TRANSPARENT_AGGR (t) = 0;
7020 warning_at (loc, 0, "union cannot be made transparent");
7023 /* If this structure or union completes the type of any previous
7024 variable declaration, lay it out and output its rtl. */
7025 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
7027 x = TREE_CHAIN (x))
7029 tree decl = TREE_VALUE (x);
7030 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7031 layout_array_type (TREE_TYPE (decl));
7032 if (TREE_CODE (decl) != TYPE_DECL)
7034 layout_decl (decl, 0);
7035 if (c_dialect_objc ())
7036 objc_check_decl (decl);
7037 rest_of_decl_compilation (decl, toplevel, 0);
7038 if (!toplevel)
7039 expand_decl (decl);
7042 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
7044 /* Update type location to the one of the definition, instead of e.g.
7045 a forward declaration. */
7046 if (TYPE_STUB_DECL (t))
7047 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
7049 /* Finish debugging output for this type. */
7050 rest_of_type_compilation (t, toplevel);
7052 /* If we're inside a function proper, i.e. not file-scope and not still
7053 parsing parameters, then arrange for the size of a variable sized type
7054 to be bound now. */
7055 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
7056 add_stmt (build_stmt (loc,
7057 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
7059 if (warn_cxx_compat)
7060 warn_cxx_compat_finish_struct (fieldlist);
7062 VEC_free (tree, heap, struct_parse_info->struct_types);
7063 VEC_free (c_binding_ptr, heap, struct_parse_info->fields);
7064 VEC_free (tree, heap, struct_parse_info->typedefs_seen);
7065 XDELETE (struct_parse_info);
7067 struct_parse_info = enclosing_struct_parse_info;
7069 /* If this struct is defined inside a struct, add it to
7070 struct_types. */
7071 if (warn_cxx_compat
7072 && struct_parse_info != NULL
7073 && !in_sizeof && !in_typeof && !in_alignof)
7074 VEC_safe_push (tree, heap, struct_parse_info->struct_types, t);
7076 return t;
7079 /* Lay out the type T, and its element type, and so on. */
7081 static void
7082 layout_array_type (tree t)
7084 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7085 layout_array_type (TREE_TYPE (t));
7086 layout_type (t);
7089 /* Begin compiling the definition of an enumeration type.
7090 NAME is its name (or null if anonymous).
7091 LOC is the enum's location.
7092 Returns the type object, as yet incomplete.
7093 Also records info about it so that build_enumerator
7094 may be used to declare the individual values as they are read. */
7096 tree
7097 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
7099 tree enumtype = NULL_TREE;
7100 location_t enumloc = UNKNOWN_LOCATION;
7102 /* If this is the real definition for a previous forward reference,
7103 fill in the contents in the same object that used to be the
7104 forward reference. */
7106 if (name != NULL_TREE)
7107 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1, &enumloc);
7109 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
7111 enumtype = make_node (ENUMERAL_TYPE);
7112 pushtag (loc, name, enumtype);
7115 if (C_TYPE_BEING_DEFINED (enumtype))
7116 error_at (loc, "nested redefinition of %<enum %E%>", name);
7118 C_TYPE_BEING_DEFINED (enumtype) = 1;
7120 if (TYPE_VALUES (enumtype) != 0)
7122 /* This enum is a named one that has been declared already. */
7123 error_at (loc, "redeclaration of %<enum %E%>", name);
7124 if (enumloc != UNKNOWN_LOCATION)
7125 inform (enumloc, "originally defined here");
7127 /* Completely replace its old definition.
7128 The old enumerators remain defined, however. */
7129 TYPE_VALUES (enumtype) = 0;
7132 the_enum->enum_next_value = integer_zero_node;
7133 the_enum->enum_overflow = 0;
7135 if (flag_short_enums)
7136 TYPE_PACKED (enumtype) = 1;
7138 /* FIXME: This will issue a warning for a use of a type defined
7139 within sizeof in a statement expr. This is not terribly serious
7140 as C++ doesn't permit statement exprs within sizeof anyhow. */
7141 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7142 warning_at (loc, OPT_Wc___compat,
7143 "defining type in %qs expression is invalid in C++",
7144 (in_sizeof
7145 ? "sizeof"
7146 : (in_typeof ? "typeof" : "alignof")));
7148 return enumtype;
7151 /* After processing and defining all the values of an enumeration type,
7152 install their decls in the enumeration type and finish it off.
7153 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
7154 and ATTRIBUTES are the specified attributes.
7155 Returns ENUMTYPE. */
7157 tree
7158 finish_enum (tree enumtype, tree values, tree attributes)
7160 tree pair, tem;
7161 tree minnode = 0, maxnode = 0;
7162 int precision, unsign;
7163 bool toplevel = (file_scope == current_scope);
7164 struct lang_type *lt;
7166 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7168 /* Calculate the maximum value of any enumerator in this type. */
7170 if (values == error_mark_node)
7171 minnode = maxnode = integer_zero_node;
7172 else
7174 minnode = maxnode = TREE_VALUE (values);
7175 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
7177 tree value = TREE_VALUE (pair);
7178 if (tree_int_cst_lt (maxnode, value))
7179 maxnode = value;
7180 if (tree_int_cst_lt (value, minnode))
7181 minnode = value;
7185 /* Construct the final type of this enumeration. It is the same
7186 as one of the integral types - the narrowest one that fits, except
7187 that normally we only go as narrow as int - and signed iff any of
7188 the values are negative. */
7189 unsign = (tree_int_cst_sgn (minnode) >= 0);
7190 precision = MAX (tree_int_cst_min_precision (minnode, unsign),
7191 tree_int_cst_min_precision (maxnode, unsign));
7193 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
7195 tem = c_common_type_for_size (precision, unsign);
7196 if (tem == NULL)
7198 warning (0, "enumeration values exceed range of largest integer");
7199 tem = long_long_integer_type_node;
7202 else
7203 tem = unsign ? unsigned_type_node : integer_type_node;
7205 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
7206 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
7207 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
7208 TYPE_SIZE (enumtype) = 0;
7210 /* If the precision of the type was specific with an attribute and it
7211 was too small, give an error. Otherwise, use it. */
7212 if (TYPE_PRECISION (enumtype))
7214 if (precision > TYPE_PRECISION (enumtype))
7215 error ("specified mode too small for enumeral values");
7217 else
7218 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
7220 layout_type (enumtype);
7222 if (values != error_mark_node)
7224 /* Change the type of the enumerators to be the enum type. We
7225 need to do this irrespective of the size of the enum, for
7226 proper type checking. Replace the DECL_INITIALs of the
7227 enumerators, and the value slots of the list, with copies
7228 that have the enum type; they cannot be modified in place
7229 because they may be shared (e.g. integer_zero_node) Finally,
7230 change the purpose slots to point to the names of the decls. */
7231 for (pair = values; pair; pair = TREE_CHAIN (pair))
7233 tree enu = TREE_PURPOSE (pair);
7234 tree ini = DECL_INITIAL (enu);
7236 TREE_TYPE (enu) = enumtype;
7238 /* The ISO C Standard mandates enumerators to have type int,
7239 even though the underlying type of an enum type is
7240 unspecified. However, GCC allows enumerators of any
7241 integer type as an extensions. build_enumerator()
7242 converts any enumerators that fit in an int to type int,
7243 to avoid promotions to unsigned types when comparing
7244 integers with enumerators that fit in the int range.
7245 When -pedantic is given, build_enumerator() would have
7246 already warned about those that don't fit. Here we
7247 convert the rest to the enumerator type. */
7248 if (TREE_TYPE (ini) != integer_type_node)
7249 ini = convert (enumtype, ini);
7251 DECL_INITIAL (enu) = ini;
7252 TREE_PURPOSE (pair) = DECL_NAME (enu);
7253 TREE_VALUE (pair) = ini;
7256 TYPE_VALUES (enumtype) = values;
7259 /* Record the min/max values so that we can warn about bit-field
7260 enumerations that are too small for the values. */
7261 lt = GGC_CNEW (struct lang_type);
7262 lt->enum_min = minnode;
7263 lt->enum_max = maxnode;
7264 TYPE_LANG_SPECIFIC (enumtype) = lt;
7266 /* Fix up all variant types of this enum type. */
7267 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
7269 if (tem == enumtype)
7270 continue;
7271 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
7272 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
7273 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
7274 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
7275 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
7276 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
7277 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
7278 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
7279 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
7280 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
7281 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
7284 /* Finish debugging output for this type. */
7285 rest_of_type_compilation (enumtype, toplevel);
7287 /* If this enum is defined inside a struct, add it to
7288 struct_types. */
7289 if (warn_cxx_compat
7290 && struct_parse_info != NULL
7291 && !in_sizeof && !in_typeof && !in_alignof)
7292 VEC_safe_push (tree, heap, struct_parse_info->struct_types, enumtype);
7294 return enumtype;
7297 /* Build and install a CONST_DECL for one value of the
7298 current enumeration type (one that was begun with start_enum).
7299 LOC is the location of the enumerator.
7300 Return a tree-list containing the CONST_DECL and its value.
7301 Assignment of sequential values by default is handled here. */
7303 tree
7304 build_enumerator (location_t loc,
7305 struct c_enum_contents *the_enum, tree name, tree value)
7307 tree decl, type;
7309 /* Validate and default VALUE. */
7311 if (value != 0)
7313 /* Don't issue more errors for error_mark_node (i.e. an
7314 undeclared identifier) - just ignore the value expression. */
7315 if (value == error_mark_node)
7316 value = 0;
7317 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
7319 error_at (loc, "enumerator value for %qE is not an integer constant",
7320 name);
7321 value = 0;
7323 else
7325 if (TREE_CODE (value) != INTEGER_CST)
7327 value = c_fully_fold (value, false, NULL);
7328 if (TREE_CODE (value) == INTEGER_CST)
7329 pedwarn (loc, OPT_pedantic,
7330 "enumerator value for %qE is not an integer "
7331 "constant expression", name);
7333 if (TREE_CODE (value) != INTEGER_CST)
7335 error ("enumerator value for %qE is not an integer constant",
7336 name);
7337 value = 0;
7339 else
7341 value = default_conversion (value);
7342 constant_expression_warning (value);
7347 /* Default based on previous value. */
7348 /* It should no longer be possible to have NON_LVALUE_EXPR
7349 in the default. */
7350 if (value == 0)
7352 value = the_enum->enum_next_value;
7353 if (the_enum->enum_overflow)
7354 error_at (loc, "overflow in enumeration values");
7356 /* Even though the underlying type of an enum is unspecified, the
7357 type of enumeration constants is explicitly defined as int
7358 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
7359 an extension. */
7360 else if (!int_fits_type_p (value, integer_type_node))
7361 pedwarn (loc, OPT_pedantic,
7362 "ISO C restricts enumerator values to range of %<int%>");
7364 /* The ISO C Standard mandates enumerators to have type int, even
7365 though the underlying type of an enum type is unspecified.
7366 However, GCC allows enumerators of any integer type as an
7367 extensions. Here we convert any enumerators that fit in an int
7368 to type int, to avoid promotions to unsigned types when comparing
7369 integers with enumerators that fit in the int range. When
7370 -pedantic is given, we would have already warned about those that
7371 don't fit. We have to do this here rather than in finish_enum
7372 because this value may be used to define more enumerators. */
7373 if (int_fits_type_p (value, integer_type_node))
7374 value = convert (integer_type_node, value);
7376 /* Set basis for default for next value. */
7377 the_enum->enum_next_value
7378 = build_binary_op
7379 (EXPR_HAS_LOCATION (value) ? EXPR_LOCATION (value) : input_location,
7380 PLUS_EXPR, value, integer_one_node, 0);
7381 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
7383 /* Now create a declaration for the enum value name. */
7385 type = TREE_TYPE (value);
7386 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
7387 TYPE_PRECISION (integer_type_node)),
7388 (TYPE_PRECISION (type)
7389 >= TYPE_PRECISION (integer_type_node)
7390 && TYPE_UNSIGNED (type)));
7392 decl = build_decl (loc, CONST_DECL, name, type);
7393 DECL_INITIAL (decl) = convert (type, value);
7394 pushdecl (decl);
7396 return tree_cons (decl, value, NULL_TREE);
7400 /* Create the FUNCTION_DECL for a function definition.
7401 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
7402 the declaration; they describe the function's name and the type it returns,
7403 but twisted together in a fashion that parallels the syntax of C.
7405 This function creates a binding context for the function body
7406 as well as setting up the FUNCTION_DECL in current_function_decl.
7408 Returns 1 on success. If the DECLARATOR is not suitable for a function
7409 (it defines a datum instead), we return 0, which tells
7410 yyparse to report a parse error. */
7413 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
7414 tree attributes)
7416 tree decl1, old_decl;
7417 tree restype, resdecl;
7418 location_t loc;
7420 current_function_returns_value = 0; /* Assume, until we see it does. */
7421 current_function_returns_null = 0;
7422 current_function_returns_abnormally = 0;
7423 warn_about_return_type = 0;
7424 c_switch_stack = NULL;
7426 /* Indicate no valid break/continue context by setting these variables
7427 to some non-null, non-label value. We'll notice and emit the proper
7428 error message in c_finish_bc_stmt. */
7429 c_break_label = c_cont_label = size_zero_node;
7431 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
7432 &attributes, NULL, NULL, DEPRECATED_NORMAL);
7434 /* If the declarator is not suitable for a function definition,
7435 cause a syntax error. */
7436 if (decl1 == 0)
7437 return 0;
7439 loc = DECL_SOURCE_LOCATION (decl1);
7441 decl_attributes (&decl1, attributes, 0);
7443 if (DECL_DECLARED_INLINE_P (decl1)
7444 && DECL_UNINLINABLE (decl1)
7445 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
7446 warning_at (loc, OPT_Wattributes,
7447 "inline function %qD given attribute noinline",
7448 decl1);
7450 /* Handle gnu_inline attribute. */
7451 if (declspecs->inline_p
7452 && !flag_gnu89_inline
7453 && TREE_CODE (decl1) == FUNCTION_DECL
7454 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
7455 || current_function_decl))
7457 if (declspecs->storage_class != csc_static)
7458 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
7461 announce_function (decl1);
7463 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
7465 error_at (loc, "return type is an incomplete type");
7466 /* Make it return void instead. */
7467 TREE_TYPE (decl1)
7468 = build_function_type (void_type_node,
7469 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
7472 if (warn_about_return_type)
7473 pedwarn_c99 (loc, flag_isoc99 ? 0
7474 : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
7475 "return type defaults to %<int%>");
7477 /* Make the init_value nonzero so pushdecl knows this is not tentative.
7478 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
7479 DECL_INITIAL (decl1) = error_mark_node;
7481 /* A nested function is not global. */
7482 if (current_function_decl != 0)
7483 TREE_PUBLIC (decl1) = 0;
7485 /* If this definition isn't a prototype and we had a prototype declaration
7486 before, copy the arg type info from that prototype. */
7487 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
7488 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
7489 old_decl = 0;
7490 current_function_prototype_locus = UNKNOWN_LOCATION;
7491 current_function_prototype_built_in = false;
7492 current_function_prototype_arg_types = NULL_TREE;
7493 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
7495 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
7496 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7497 TREE_TYPE (TREE_TYPE (old_decl))))
7499 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
7500 TREE_TYPE (decl1));
7501 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
7502 current_function_prototype_built_in
7503 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
7504 current_function_prototype_arg_types
7505 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
7507 if (TREE_PUBLIC (decl1))
7509 /* If there is an external prototype declaration of this
7510 function, record its location but do not copy information
7511 to this decl. This may be an invisible declaration
7512 (built-in or in a scope which has finished) or simply
7513 have more refined argument types than any declaration
7514 found above. */
7515 struct c_binding *b;
7516 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
7517 if (B_IN_SCOPE (b, external_scope))
7518 break;
7519 if (b)
7521 tree ext_decl, ext_type;
7522 ext_decl = b->decl;
7523 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
7524 if (TREE_CODE (ext_type) == FUNCTION_TYPE
7525 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7526 TREE_TYPE (ext_type)))
7528 current_function_prototype_locus
7529 = DECL_SOURCE_LOCATION (ext_decl);
7530 current_function_prototype_built_in
7531 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
7532 current_function_prototype_arg_types
7533 = TYPE_ARG_TYPES (ext_type);
7539 /* Optionally warn of old-fashioned def with no previous prototype. */
7540 if (warn_strict_prototypes
7541 && old_decl != error_mark_node
7542 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
7543 && C_DECL_ISNT_PROTOTYPE (old_decl))
7544 warning_at (loc, OPT_Wstrict_prototypes,
7545 "function declaration isn%'t a prototype");
7546 /* Optionally warn of any global def with no previous prototype. */
7547 else if (warn_missing_prototypes
7548 && old_decl != error_mark_node
7549 && TREE_PUBLIC (decl1)
7550 && !MAIN_NAME_P (DECL_NAME (decl1))
7551 && C_DECL_ISNT_PROTOTYPE (old_decl))
7552 warning_at (loc, OPT_Wmissing_prototypes,
7553 "no previous prototype for %qD", decl1);
7554 /* Optionally warn of any def with no previous prototype
7555 if the function has already been used. */
7556 else if (warn_missing_prototypes
7557 && old_decl != 0
7558 && old_decl != error_mark_node
7559 && TREE_USED (old_decl)
7560 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
7561 warning_at (loc, OPT_Wmissing_prototypes,
7562 "%qD was used with no prototype before its definition", decl1);
7563 /* Optionally warn of any global def with no previous declaration. */
7564 else if (warn_missing_declarations
7565 && TREE_PUBLIC (decl1)
7566 && old_decl == 0
7567 && !MAIN_NAME_P (DECL_NAME (decl1)))
7568 warning_at (loc, OPT_Wmissing_declarations,
7569 "no previous declaration for %qD",
7570 decl1);
7571 /* Optionally warn of any def with no previous declaration
7572 if the function has already been used. */
7573 else if (warn_missing_declarations
7574 && old_decl != 0
7575 && old_decl != error_mark_node
7576 && TREE_USED (old_decl)
7577 && C_DECL_IMPLICIT (old_decl))
7578 warning_at (loc, OPT_Wmissing_declarations,
7579 "%qD was used with no declaration before its definition", decl1);
7581 /* This function exists in static storage.
7582 (This does not mean `static' in the C sense!) */
7583 TREE_STATIC (decl1) = 1;
7585 /* This is the earliest point at which we might know the assembler
7586 name of the function. Thus, if it's set before this, die horribly. */
7587 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
7589 /* If #pragma weak was used, mark the decl weak now. */
7590 if (current_scope == file_scope)
7591 maybe_apply_pragma_weak (decl1);
7593 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
7594 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
7596 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
7597 != integer_type_node)
7598 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
7600 check_main_parameter_types (decl1);
7602 if (!TREE_PUBLIC (decl1))
7603 pedwarn (loc, OPT_Wmain,
7604 "%qD is normally a non-static function", decl1);
7607 /* Record the decl so that the function name is defined.
7608 If we already have a decl for this name, and it is a FUNCTION_DECL,
7609 use the old decl. */
7611 current_function_decl = pushdecl (decl1);
7613 push_scope ();
7614 declare_parm_level ();
7616 restype = function_return_type (current_function_decl);
7617 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
7618 DECL_ARTIFICIAL (resdecl) = 1;
7619 DECL_IGNORED_P (resdecl) = 1;
7620 DECL_RESULT (current_function_decl) = resdecl;
7622 start_fname_decls ();
7624 return 1;
7627 /* Subroutine of store_parm_decls which handles new-style function
7628 definitions (prototype format). The parms already have decls, so we
7629 need only record them as in effect and complain if any redundant
7630 old-style parm decls were written. */
7631 static void
7632 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
7634 tree decl;
7636 if (current_scope->bindings)
7638 error_at (DECL_SOURCE_LOCATION (fndecl),
7639 "old-style parameter declarations in prototyped "
7640 "function definition");
7642 /* Get rid of the old-style declarations. */
7643 pop_scope ();
7644 push_scope ();
7646 /* Don't issue this warning for nested functions, and don't issue this
7647 warning if we got here because ARG_INFO_TYPES was error_mark_node
7648 (this happens when a function definition has just an ellipsis in
7649 its parameter list). */
7650 else if (!in_system_header && !current_function_scope
7651 && arg_info->types != error_mark_node)
7652 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
7653 "traditional C rejects ISO C style function definitions");
7655 /* Now make all the parameter declarations visible in the function body.
7656 We can bypass most of the grunt work of pushdecl. */
7657 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
7659 DECL_CONTEXT (decl) = current_function_decl;
7660 if (DECL_NAME (decl))
7662 bind (DECL_NAME (decl), decl, current_scope,
7663 /*invisible=*/false, /*nested=*/false,
7664 UNKNOWN_LOCATION);
7665 if (!TREE_USED (decl))
7666 warn_if_shadowing (decl);
7668 else
7670 /* Parameters in IFUNC function are never used. */
7671 if (!DECL_IS_IFUNC (current_function_decl))
7672 error_at (DECL_SOURCE_LOCATION (decl),
7673 "parameter name omitted");
7677 /* Record the parameter list in the function declaration. */
7678 DECL_ARGUMENTS (fndecl) = arg_info->parms;
7680 /* Now make all the ancillary declarations visible, likewise. */
7681 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
7683 DECL_CONTEXT (decl) = current_function_decl;
7684 if (DECL_NAME (decl))
7685 bind (DECL_NAME (decl), decl, current_scope,
7686 /*invisible=*/false,
7687 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
7688 UNKNOWN_LOCATION);
7691 /* And all the tag declarations. */
7692 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
7693 if (TREE_PURPOSE (decl))
7694 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
7695 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
7698 /* Subroutine of store_parm_decls which handles old-style function
7699 definitions (separate parameter list and declarations). */
7701 static void
7702 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
7704 struct c_binding *b;
7705 tree parm, decl, last;
7706 tree parmids = arg_info->parms;
7707 struct pointer_set_t *seen_args = pointer_set_create ();
7709 if (!in_system_header)
7710 warning_at (DECL_SOURCE_LOCATION (fndecl),
7711 OPT_Wold_style_definition, "old-style function definition");
7713 /* Match each formal parameter name with its declaration. Save each
7714 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
7715 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
7717 if (TREE_VALUE (parm) == 0)
7719 error_at (DECL_SOURCE_LOCATION (fndecl),
7720 "parameter name missing from parameter list");
7721 TREE_PURPOSE (parm) = 0;
7722 continue;
7725 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
7726 if (b && B_IN_CURRENT_SCOPE (b))
7728 decl = b->decl;
7729 /* If we got something other than a PARM_DECL it is an error. */
7730 if (TREE_CODE (decl) != PARM_DECL)
7731 error_at (DECL_SOURCE_LOCATION (decl),
7732 "%qD declared as a non-parameter", decl);
7733 /* If the declaration is already marked, we have a duplicate
7734 name. Complain and ignore the duplicate. */
7735 else if (pointer_set_contains (seen_args, decl))
7737 error_at (DECL_SOURCE_LOCATION (decl),
7738 "multiple parameters named %qD", decl);
7739 TREE_PURPOSE (parm) = 0;
7740 continue;
7742 /* If the declaration says "void", complain and turn it into
7743 an int. */
7744 else if (VOID_TYPE_P (TREE_TYPE (decl)))
7746 error_at (DECL_SOURCE_LOCATION (decl),
7747 "parameter %qD declared with void type", decl);
7748 TREE_TYPE (decl) = integer_type_node;
7749 DECL_ARG_TYPE (decl) = integer_type_node;
7750 layout_decl (decl, 0);
7752 warn_if_shadowing (decl);
7754 /* If no declaration found, default to int. */
7755 else
7757 /* FIXME diagnostics: This should be the location of the argument,
7758 not the FNDECL. E.g., for an old-style declaration
7760 int f10(v) { blah; }
7762 We should use the location of the V, not the F10.
7763 Unfortunately, the V is an IDENTIFIER_NODE which has no
7764 location. In the future we need locations for c_arg_info
7765 entries.
7767 See gcc.dg/Wshadow-3.c for an example of this problem. */
7768 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
7769 PARM_DECL, TREE_VALUE (parm), integer_type_node);
7770 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
7771 pushdecl (decl);
7772 warn_if_shadowing (decl);
7774 if (flag_isoc99)
7775 pedwarn (DECL_SOURCE_LOCATION (decl),
7776 0, "type of %qD defaults to %<int%>", decl);
7777 else
7778 warning_at (DECL_SOURCE_LOCATION (decl),
7779 OPT_Wmissing_parameter_type,
7780 "type of %qD defaults to %<int%>", decl);
7783 TREE_PURPOSE (parm) = decl;
7784 pointer_set_insert (seen_args, decl);
7787 /* Now examine the parms chain for incomplete declarations
7788 and declarations with no corresponding names. */
7790 for (b = current_scope->bindings; b; b = b->prev)
7792 parm = b->decl;
7793 if (TREE_CODE (parm) != PARM_DECL)
7794 continue;
7796 if (TREE_TYPE (parm) != error_mark_node
7797 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
7799 error_at (DECL_SOURCE_LOCATION (parm),
7800 "parameter %qD has incomplete type", parm);
7801 TREE_TYPE (parm) = error_mark_node;
7804 if (!pointer_set_contains (seen_args, parm))
7806 error_at (DECL_SOURCE_LOCATION (parm),
7807 "declaration for parameter %qD but no such parameter",
7808 parm);
7810 /* Pretend the parameter was not missing.
7811 This gets us to a standard state and minimizes
7812 further error messages. */
7813 parmids = chainon (parmids, tree_cons (parm, 0, 0));
7817 /* Chain the declarations together in the order of the list of
7818 names. Store that chain in the function decl, replacing the
7819 list of names. Update the current scope to match. */
7820 DECL_ARGUMENTS (fndecl) = 0;
7822 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
7823 if (TREE_PURPOSE (parm))
7824 break;
7825 if (parm && TREE_PURPOSE (parm))
7827 last = TREE_PURPOSE (parm);
7828 DECL_ARGUMENTS (fndecl) = last;
7830 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
7831 if (TREE_PURPOSE (parm))
7833 TREE_CHAIN (last) = TREE_PURPOSE (parm);
7834 last = TREE_PURPOSE (parm);
7836 TREE_CHAIN (last) = 0;
7839 pointer_set_destroy (seen_args);
7841 /* If there was a previous prototype,
7842 set the DECL_ARG_TYPE of each argument according to
7843 the type previously specified, and report any mismatches. */
7845 if (current_function_prototype_arg_types)
7847 tree type;
7848 for (parm = DECL_ARGUMENTS (fndecl),
7849 type = current_function_prototype_arg_types;
7850 parm || (type && TREE_VALUE (type) != error_mark_node
7851 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
7852 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
7854 if (parm == 0 || type == 0
7855 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
7857 if (current_function_prototype_built_in)
7858 warning_at (DECL_SOURCE_LOCATION (fndecl),
7859 0, "number of arguments doesn%'t match "
7860 "built-in prototype");
7861 else
7863 /* FIXME diagnostics: This should be the location of
7864 FNDECL, but there is bug when a prototype is
7865 declared inside function context, but defined
7866 outside of it (e.g., gcc.dg/pr15698-2.c). In
7867 which case FNDECL gets the location of the
7868 prototype, not the definition. */
7869 error_at (input_location,
7870 "number of arguments doesn%'t match prototype");
7872 error_at (current_function_prototype_locus,
7873 "prototype declaration");
7875 break;
7877 /* Type for passing arg must be consistent with that
7878 declared for the arg. ISO C says we take the unqualified
7879 type for parameters declared with qualified type. */
7880 if (TREE_TYPE (parm) != error_mark_node
7881 && TREE_TYPE (type) != error_mark_node
7882 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
7883 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
7885 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
7886 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
7888 /* Adjust argument to match prototype. E.g. a previous
7889 `int foo(float);' prototype causes
7890 `int foo(x) float x; {...}' to be treated like
7891 `int foo(float x) {...}'. This is particularly
7892 useful for argument types like uid_t. */
7893 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
7895 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
7896 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
7897 && TYPE_PRECISION (TREE_TYPE (parm))
7898 < TYPE_PRECISION (integer_type_node))
7899 DECL_ARG_TYPE (parm) = integer_type_node;
7901 /* ??? Is it possible to get here with a
7902 built-in prototype or will it always have
7903 been diagnosed as conflicting with an
7904 old-style definition and discarded? */
7905 if (current_function_prototype_built_in)
7906 warning_at (DECL_SOURCE_LOCATION (parm),
7907 OPT_pedantic, "promoted argument %qD "
7908 "doesn%'t match built-in prototype", parm);
7909 else
7911 pedwarn (DECL_SOURCE_LOCATION (parm),
7912 OPT_pedantic, "promoted argument %qD "
7913 "doesn%'t match prototype", parm);
7914 pedwarn (current_function_prototype_locus, OPT_pedantic,
7915 "prototype declaration");
7918 else
7920 if (current_function_prototype_built_in)
7921 warning_at (DECL_SOURCE_LOCATION (parm),
7922 0, "argument %qD doesn%'t match "
7923 "built-in prototype", parm);
7924 else
7926 error_at (DECL_SOURCE_LOCATION (parm),
7927 "argument %qD doesn%'t match prototype", parm);
7928 error_at (current_function_prototype_locus,
7929 "prototype declaration");
7934 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
7937 /* Otherwise, create a prototype that would match. */
7939 else
7941 tree actual = 0, last = 0, type;
7943 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
7945 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
7946 if (last)
7947 TREE_CHAIN (last) = type;
7948 else
7949 actual = type;
7950 last = type;
7952 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
7953 if (last)
7954 TREE_CHAIN (last) = type;
7955 else
7956 actual = type;
7958 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
7959 of the type of this function, but we need to avoid having this
7960 affect the types of other similarly-typed functions, so we must
7961 first force the generation of an identical (but separate) type
7962 node for the relevant function type. The new node we create
7963 will be a variant of the main variant of the original function
7964 type. */
7966 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
7968 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
7972 /* Store parameter declarations passed in ARG_INFO into the current
7973 function declaration. */
7975 void
7976 store_parm_decls_from (struct c_arg_info *arg_info)
7978 current_function_arg_info = arg_info;
7979 store_parm_decls ();
7982 /* Store the parameter declarations into the current function declaration.
7983 This is called after parsing the parameter declarations, before
7984 digesting the body of the function.
7986 For an old-style definition, construct a prototype out of the old-style
7987 parameter declarations and inject it into the function's type. */
7989 void
7990 store_parm_decls (void)
7992 tree fndecl = current_function_decl;
7993 bool proto;
7995 /* The argument information block for FNDECL. */
7996 struct c_arg_info *arg_info = current_function_arg_info;
7997 current_function_arg_info = 0;
7999 /* True if this definition is written with a prototype. Note:
8000 despite C99 6.7.5.3p14, we can *not* treat an empty argument
8001 list in a function definition as equivalent to (void) -- an
8002 empty argument list specifies the function has no parameters,
8003 but only (void) sets up a prototype for future calls. */
8004 proto = arg_info->types != 0;
8006 if (proto)
8007 store_parm_decls_newstyle (fndecl, arg_info);
8008 else
8009 store_parm_decls_oldstyle (fndecl, arg_info);
8011 /* The next call to push_scope will be a function body. */
8013 next_is_function_body = true;
8015 /* Write a record describing this function definition to the prototypes
8016 file (if requested). */
8018 gen_aux_info_record (fndecl, 1, 0, proto);
8020 /* Initialize the RTL code for the function. */
8021 allocate_struct_function (fndecl, false);
8023 /* Begin the statement tree for this function. */
8024 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
8026 /* ??? Insert the contents of the pending sizes list into the function
8027 to be evaluated. The only reason left to have this is
8028 void foo(int n, int array[n++])
8029 because we throw away the array type in favor of a pointer type, and
8030 thus won't naturally see the SAVE_EXPR containing the increment. All
8031 other pending sizes would be handled by gimplify_parameters. */
8033 VEC(tree,gc) *pending_sizes = get_pending_sizes ();
8034 tree t;
8035 int i;
8037 for (i = 0; VEC_iterate (tree, pending_sizes, i, t); i++)
8038 add_stmt (t);
8041 /* Even though we're inside a function body, we still don't want to
8042 call expand_expr to calculate the size of a variable-sized array.
8043 We haven't necessarily assigned RTL to all variables yet, so it's
8044 not safe to try to expand expressions involving them. */
8045 cfun->dont_save_pending_sizes_p = 1;
8049 /* Finish up a function declaration and compile that function
8050 all the way to assembler language output. The free the storage
8051 for the function definition.
8053 This is called after parsing the body of the function definition. */
8055 void
8056 finish_function (void)
8058 tree fndecl = current_function_decl;
8060 if (TREE_CODE (fndecl) == FUNCTION_DECL
8061 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
8063 tree args = DECL_ARGUMENTS (fndecl);
8064 for (; args; args = TREE_CHAIN (args))
8066 tree type = TREE_TYPE (args);
8067 if (INTEGRAL_TYPE_P (type)
8068 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8069 DECL_ARG_TYPE (args) = integer_type_node;
8073 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
8074 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
8076 /* Must mark the RESULT_DECL as being in this function. */
8078 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
8079 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
8081 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
8082 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
8083 == integer_type_node && flag_isoc99)
8085 /* Hack. We don't want the middle-end to warn that this return
8086 is unreachable, so we mark its location as special. Using
8087 UNKNOWN_LOCATION has the problem that it gets clobbered in
8088 annotate_one_with_locus. A cleaner solution might be to
8089 ensure ! should_carry_locus_p (stmt), but that needs a flag.
8091 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
8094 /* Tie off the statement tree for this function. */
8095 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
8097 finish_fname_decls ();
8099 /* Complain if there's just no return statement. */
8100 if ((warn_return_type
8101 || DECL_IS_IFUNC (fndecl))
8102 && TREE_CODE (function_return_type (fndecl)) != VOID_TYPE
8103 && !current_function_returns_value && !current_function_returns_null
8104 /* Don't complain if we are no-return. */
8105 && !current_function_returns_abnormally
8106 /* Don't complain if we are declared noreturn. */
8107 && !TREE_THIS_VOLATILE (fndecl)
8108 /* Don't warn for main(). */
8109 && !MAIN_NAME_P (DECL_NAME (fndecl))
8110 /* Or if they didn't actually specify a return type. */
8111 && !C_FUNCTION_IMPLICIT_INT (fndecl)
8112 /* Normally, with -Wreturn-type, flow will complain, but we might
8113 optimize out static functions. */
8114 && !TREE_PUBLIC (fndecl))
8116 if (DECL_IS_IFUNC (fndecl))
8117 error ("control reaches end of indirect function");
8118 else
8119 warning (OPT_Wreturn_type,
8120 "no return statement in function returning non-void");
8121 TREE_NO_WARNING (fndecl) = 1;
8124 /* Complain about parameters that are only set, but never otherwise used. */
8125 if (warn_unused_but_set_parameter)
8127 tree decl;
8129 for (decl = DECL_ARGUMENTS (fndecl);
8130 decl;
8131 decl = TREE_CHAIN (decl))
8132 if (TREE_USED (decl)
8133 && TREE_CODE (decl) == PARM_DECL
8134 && !DECL_READ_P (decl)
8135 && DECL_NAME (decl)
8136 && !DECL_ARTIFICIAL (decl)
8137 && !TREE_NO_WARNING (decl))
8138 warning_at (DECL_SOURCE_LOCATION (decl),
8139 OPT_Wunused_but_set_parameter,
8140 "parameter %qD set but not used", decl);
8143 /* Store the end of the function, so that we get good line number
8144 info for the epilogue. */
8145 cfun->function_end_locus = input_location;
8147 /* Finalize the ELF visibility for the function. */
8148 c_determine_visibility (fndecl);
8150 /* For GNU C extern inline functions disregard inline limits. */
8151 if (DECL_EXTERNAL (fndecl)
8152 && DECL_DECLARED_INLINE_P (fndecl))
8153 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
8155 /* Genericize before inlining. Delay genericizing nested functions
8156 until their parent function is genericized. Since finalizing
8157 requires GENERIC, delay that as well. */
8159 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
8160 && !undef_nested_function)
8162 if (!decl_function_context (fndecl))
8164 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
8165 c_genericize (fndecl);
8167 /* ??? Objc emits functions after finalizing the compilation unit.
8168 This should be cleaned up later and this conditional removed. */
8169 if (cgraph_global_info_ready)
8171 cgraph_add_new_function (fndecl, false);
8172 return;
8174 cgraph_finalize_function (fndecl, false);
8176 else
8178 /* Register this function with cgraph just far enough to get it
8179 added to our parent's nested function list. Handy, since the
8180 C front end doesn't have such a list. */
8181 (void) cgraph_node (fndecl);
8185 if (!decl_function_context (fndecl))
8186 undef_nested_function = false;
8188 /* We're leaving the context of this function, so zap cfun.
8189 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
8190 tree_rest_of_compilation. */
8191 set_cfun (NULL);
8192 current_function_decl = NULL;
8195 /* Check the declarations given in a for-loop for satisfying the C99
8196 constraints. If exactly one such decl is found, return it. LOC is
8197 the location of the opening parenthesis of the for loop. */
8199 tree
8200 check_for_loop_decls (location_t loc)
8202 struct c_binding *b;
8203 tree one_decl = NULL_TREE;
8204 int n_decls = 0;
8206 if (!flag_isoc99)
8208 static bool hint = true;
8209 /* If we get here, declarations have been used in a for loop without
8210 the C99 for loop scope. This doesn't make much sense, so don't
8211 allow it. */
8212 error_at (loc, "%<for%> loop initial declarations "
8213 "are only allowed in C99 mode");
8214 if (hint)
8216 inform (loc,
8217 "use option -std=c99 or -std=gnu99 to compile your code");
8218 hint = false;
8220 return NULL_TREE;
8222 /* C99 subclause 6.8.5 paragraph 3:
8224 [#3] The declaration part of a for statement shall only
8225 declare identifiers for objects having storage class auto or
8226 register.
8228 It isn't clear whether, in this sentence, "identifiers" binds to
8229 "shall only declare" or to "objects" - that is, whether all identifiers
8230 declared must be identifiers for objects, or whether the restriction
8231 only applies to those that are. (A question on this in comp.std.c
8232 in November 2000 received no answer.) We implement the strictest
8233 interpretation, to avoid creating an extension which later causes
8234 problems. */
8236 for (b = current_scope->bindings; b; b = b->prev)
8238 tree id = b->id;
8239 tree decl = b->decl;
8241 if (!id)
8242 continue;
8244 switch (TREE_CODE (decl))
8246 case VAR_DECL:
8248 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
8249 if (TREE_STATIC (decl))
8250 error_at (decl_loc,
8251 "declaration of static variable %qD in %<for%> loop "
8252 "initial declaration", decl);
8253 else if (DECL_EXTERNAL (decl))
8254 error_at (decl_loc,
8255 "declaration of %<extern%> variable %qD in %<for%> loop "
8256 "initial declaration", decl);
8258 break;
8260 case RECORD_TYPE:
8261 error_at (loc,
8262 "%<struct %E%> declared in %<for%> loop initial "
8263 "declaration", id);
8264 break;
8265 case UNION_TYPE:
8266 error_at (loc,
8267 "%<union %E%> declared in %<for%> loop initial declaration",
8268 id);
8269 break;
8270 case ENUMERAL_TYPE:
8271 error_at (loc, "%<enum %E%> declared in %<for%> loop "
8272 "initial declaration", id);
8273 break;
8274 default:
8275 error_at (loc, "declaration of non-variable "
8276 "%qD in %<for%> loop initial declaration", decl);
8279 n_decls++;
8280 one_decl = decl;
8283 return n_decls == 1 ? one_decl : NULL_TREE;
8286 /* Save and reinitialize the variables
8287 used during compilation of a C function. */
8289 void
8290 c_push_function_context (void)
8292 struct language_function *p;
8293 p = GGC_NEW (struct language_function);
8294 cfun->language = p;
8296 p->base.x_stmt_tree = c_stmt_tree;
8297 p->x_break_label = c_break_label;
8298 p->x_cont_label = c_cont_label;
8299 p->x_switch_stack = c_switch_stack;
8300 p->arg_info = current_function_arg_info;
8301 p->returns_value = current_function_returns_value;
8302 p->returns_null = current_function_returns_null;
8303 p->returns_abnormally = current_function_returns_abnormally;
8304 p->warn_about_return_type = warn_about_return_type;
8306 push_function_context ();
8309 /* Restore the variables used during compilation of a C function. */
8311 void
8312 c_pop_function_context (void)
8314 struct language_function *p;
8316 pop_function_context ();
8317 p = cfun->language;
8318 cfun->language = NULL;
8320 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
8321 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
8323 /* Stop pointing to the local nodes about to be freed. */
8324 /* But DECL_INITIAL must remain nonzero so we know this
8325 was an actual function definition. */
8326 DECL_INITIAL (current_function_decl) = error_mark_node;
8327 DECL_ARGUMENTS (current_function_decl) = 0;
8330 c_stmt_tree = p->base.x_stmt_tree;
8331 c_break_label = p->x_break_label;
8332 c_cont_label = p->x_cont_label;
8333 c_switch_stack = p->x_switch_stack;
8334 current_function_arg_info = p->arg_info;
8335 current_function_returns_value = p->returns_value;
8336 current_function_returns_null = p->returns_null;
8337 current_function_returns_abnormally = p->returns_abnormally;
8338 warn_about_return_type = p->warn_about_return_type;
8341 /* The functions below are required for functionality of doing
8342 function at once processing in the C front end. Currently these
8343 functions are not called from anywhere in the C front end, but as
8344 these changes continue, that will change. */
8346 /* Returns the stmt_tree (if any) to which statements are currently
8347 being added. If there is no active statement-tree, NULL is
8348 returned. */
8350 stmt_tree
8351 current_stmt_tree (void)
8353 return &c_stmt_tree;
8356 /* Return the global value of T as a symbol. */
8358 tree
8359 identifier_global_value (tree t)
8361 struct c_binding *b;
8363 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
8364 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
8365 return b->decl;
8367 return 0;
8370 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
8371 otherwise the name is found in ridpointers from RID_INDEX. */
8373 void
8374 record_builtin_type (enum rid rid_index, const char *name, tree type)
8376 tree id, decl;
8377 if (name == 0)
8378 id = ridpointers[(int) rid_index];
8379 else
8380 id = get_identifier (name);
8381 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
8382 pushdecl (decl);
8383 if (debug_hooks->type_decl)
8384 debug_hooks->type_decl (decl, false);
8387 /* Build the void_list_node (void_type_node having been created). */
8388 tree
8389 build_void_list_node (void)
8391 tree t = build_tree_list (NULL_TREE, void_type_node);
8392 return t;
8395 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
8397 struct c_parm *
8398 build_c_parm (struct c_declspecs *specs, tree attrs,
8399 struct c_declarator *declarator)
8401 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
8402 ret->specs = specs;
8403 ret->attrs = attrs;
8404 ret->declarator = declarator;
8405 return ret;
8408 /* Return a declarator with nested attributes. TARGET is the inner
8409 declarator to which these attributes apply. ATTRS are the
8410 attributes. */
8412 struct c_declarator *
8413 build_attrs_declarator (tree attrs, struct c_declarator *target)
8415 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8416 ret->kind = cdk_attrs;
8417 ret->declarator = target;
8418 ret->u.attrs = attrs;
8419 return ret;
8422 /* Return a declarator for a function with arguments specified by ARGS
8423 and return type specified by TARGET. */
8425 struct c_declarator *
8426 build_function_declarator (struct c_arg_info *args,
8427 struct c_declarator *target)
8429 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8430 ret->kind = cdk_function;
8431 ret->declarator = target;
8432 ret->u.arg_info = args;
8433 return ret;
8436 /* Return a declarator for the identifier IDENT (which may be
8437 NULL_TREE for an abstract declarator). */
8439 struct c_declarator *
8440 build_id_declarator (tree ident)
8442 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8443 ret->kind = cdk_id;
8444 ret->declarator = 0;
8445 ret->u.id = ident;
8446 /* Default value - may get reset to a more precise location. */
8447 ret->id_loc = input_location;
8448 return ret;
8451 /* Return something to represent absolute declarators containing a *.
8452 TARGET is the absolute declarator that the * contains.
8453 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
8454 to apply to the pointer type. */
8456 struct c_declarator *
8457 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
8458 struct c_declarator *target)
8460 tree attrs;
8461 int quals = 0;
8462 struct c_declarator *itarget = target;
8463 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8464 if (type_quals_attrs)
8466 attrs = type_quals_attrs->attrs;
8467 quals = quals_from_declspecs (type_quals_attrs);
8468 if (attrs != NULL_TREE)
8469 itarget = build_attrs_declarator (attrs, target);
8471 ret->kind = cdk_pointer;
8472 ret->declarator = itarget;
8473 ret->u.pointer_quals = quals;
8474 return ret;
8477 /* Return a pointer to a structure for an empty list of declaration
8478 specifiers. */
8480 struct c_declspecs *
8481 build_null_declspecs (void)
8483 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
8484 ret->type = 0;
8485 ret->expr = 0;
8486 ret->decl_attr = 0;
8487 ret->attrs = 0;
8488 ret->typespec_word = cts_none;
8489 ret->storage_class = csc_none;
8490 ret->expr_const_operands = true;
8491 ret->declspecs_seen_p = false;
8492 ret->type_seen_p = false;
8493 ret->non_sc_seen_p = false;
8494 ret->typedef_p = false;
8495 ret->tag_defined_p = false;
8496 ret->explicit_signed_p = false;
8497 ret->deprecated_p = false;
8498 ret->default_int_p = false;
8499 ret->long_p = false;
8500 ret->long_long_p = false;
8501 ret->short_p = false;
8502 ret->signed_p = false;
8503 ret->unsigned_p = false;
8504 ret->complex_p = false;
8505 ret->inline_p = false;
8506 ret->thread_p = false;
8507 ret->const_p = false;
8508 ret->volatile_p = false;
8509 ret->restrict_p = false;
8510 ret->saturating_p = false;
8511 ret->address_space = ADDR_SPACE_GENERIC;
8512 return ret;
8515 /* Add the address space ADDRSPACE to the declaration specifiers
8516 SPECS, returning SPECS. */
8518 struct c_declspecs *
8519 declspecs_add_addrspace (struct c_declspecs *specs, addr_space_t as)
8521 specs->non_sc_seen_p = true;
8522 specs->declspecs_seen_p = true;
8524 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
8525 && specs->address_space != as)
8526 error ("incompatible address space qualifiers %qs and %qs",
8527 c_addr_space_name (as),
8528 c_addr_space_name (specs->address_space));
8529 else
8530 specs->address_space = as;
8531 return specs;
8534 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
8535 returning SPECS. */
8537 struct c_declspecs *
8538 declspecs_add_qual (struct c_declspecs *specs, tree qual)
8540 enum rid i;
8541 bool dupe = false;
8542 specs->non_sc_seen_p = true;
8543 specs->declspecs_seen_p = true;
8544 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
8545 && C_IS_RESERVED_WORD (qual));
8546 i = C_RID_CODE (qual);
8547 switch (i)
8549 case RID_CONST:
8550 dupe = specs->const_p;
8551 specs->const_p = true;
8552 break;
8553 case RID_VOLATILE:
8554 dupe = specs->volatile_p;
8555 specs->volatile_p = true;
8556 break;
8557 case RID_RESTRICT:
8558 dupe = specs->restrict_p;
8559 specs->restrict_p = true;
8560 break;
8561 default:
8562 gcc_unreachable ();
8564 if (dupe && !flag_isoc99)
8565 pedwarn (input_location, OPT_pedantic, "duplicate %qE", qual);
8566 return specs;
8569 /* Add the type specifier TYPE to the declaration specifiers SPECS,
8570 returning SPECS. */
8572 struct c_declspecs *
8573 declspecs_add_type (location_t loc, struct c_declspecs *specs,
8574 struct c_typespec spec)
8576 tree type = spec.spec;
8577 specs->non_sc_seen_p = true;
8578 specs->declspecs_seen_p = true;
8579 specs->type_seen_p = true;
8580 if (TREE_DEPRECATED (type))
8581 specs->deprecated_p = true;
8583 /* Handle type specifier keywords. */
8584 if (TREE_CODE (type) == IDENTIFIER_NODE
8585 && C_IS_RESERVED_WORD (type)
8586 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
8588 enum rid i = C_RID_CODE (type);
8589 if (specs->type)
8591 error_at (loc, "two or more data types in declaration specifiers");
8592 return specs;
8594 if ((int) i <= (int) RID_LAST_MODIFIER)
8596 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
8597 bool dupe = false;
8598 switch (i)
8600 case RID_LONG:
8601 if (specs->long_long_p)
8603 error_at (loc, "%<long long long%> is too long for GCC");
8604 break;
8606 if (specs->long_p)
8608 if (specs->typespec_word == cts_double)
8610 error_at (loc,
8611 ("both %<long long%> and %<double%> in "
8612 "declaration specifiers"));
8613 break;
8615 pedwarn_c90 (loc, OPT_Wlong_long,
8616 "ISO C90 does not support %<long long%>");
8617 specs->long_long_p = 1;
8618 break;
8620 if (specs->short_p)
8621 error_at (loc,
8622 ("both %<long%> and %<short%> in "
8623 "declaration specifiers"));
8624 else if (specs->typespec_word == cts_void)
8625 error_at (loc,
8626 ("both %<long%> and %<void%> in "
8627 "declaration specifiers"));
8628 else if (specs->typespec_word == cts_bool)
8629 error_at (loc,
8630 ("both %<long%> and %<_Bool%> in "
8631 "declaration specifiers"));
8632 else if (specs->typespec_word == cts_char)
8633 error_at (loc,
8634 ("both %<long%> and %<char%> in "
8635 "declaration specifiers"));
8636 else if (specs->typespec_word == cts_float)
8637 error_at (loc,
8638 ("both %<long%> and %<float%> in "
8639 "declaration specifiers"));
8640 else if (specs->typespec_word == cts_dfloat32)
8641 error_at (loc,
8642 ("both %<long%> and %<_Decimal32%> in "
8643 "declaration specifiers"));
8644 else if (specs->typespec_word == cts_dfloat64)
8645 error_at (loc,
8646 ("both %<long%> and %<_Decimal64%> in "
8647 "declaration specifiers"));
8648 else if (specs->typespec_word == cts_dfloat128)
8649 error_at (loc,
8650 ("both %<long%> and %<_Decimal128%> in "
8651 "declaration specifiers"));
8652 else
8653 specs->long_p = true;
8654 break;
8655 case RID_SHORT:
8656 dupe = specs->short_p;
8657 if (specs->long_p)
8658 error_at (loc,
8659 ("both %<long%> and %<short%> in "
8660 "declaration specifiers"));
8661 else if (specs->typespec_word == cts_void)
8662 error_at (loc,
8663 ("both %<short%> and %<void%> in "
8664 "declaration specifiers"));
8665 else if (specs->typespec_word == cts_bool)
8666 error_at (loc,
8667 ("both %<short%> and %<_Bool%> in "
8668 "declaration specifiers"));
8669 else if (specs->typespec_word == cts_char)
8670 error_at (loc,
8671 ("both %<short%> and %<char%> in "
8672 "declaration specifiers"));
8673 else if (specs->typespec_word == cts_float)
8674 error_at (loc,
8675 ("both %<short%> and %<float%> in "
8676 "declaration specifiers"));
8677 else if (specs->typespec_word == cts_double)
8678 error_at (loc,
8679 ("both %<short%> and %<double%> in "
8680 "declaration specifiers"));
8681 else if (specs->typespec_word == cts_dfloat32)
8682 error_at (loc,
8683 ("both %<short%> and %<_Decimal32%> in "
8684 "declaration specifiers"));
8685 else if (specs->typespec_word == cts_dfloat64)
8686 error_at (loc,
8687 ("both %<short%> and %<_Decimal64%> in "
8688 "declaration specifiers"));
8689 else if (specs->typespec_word == cts_dfloat128)
8690 error_at (loc,
8691 ("both %<short%> and %<_Decimal128%> in "
8692 "declaration specifiers"));
8693 else
8694 specs->short_p = true;
8695 break;
8696 case RID_SIGNED:
8697 dupe = specs->signed_p;
8698 if (specs->unsigned_p)
8699 error_at (loc,
8700 ("both %<signed%> and %<unsigned%> in "
8701 "declaration specifiers"));
8702 else if (specs->typespec_word == cts_void)
8703 error_at (loc,
8704 ("both %<signed%> and %<void%> in "
8705 "declaration specifiers"));
8706 else if (specs->typespec_word == cts_bool)
8707 error_at (loc,
8708 ("both %<signed%> and %<_Bool%> in "
8709 "declaration specifiers"));
8710 else if (specs->typespec_word == cts_float)
8711 error_at (loc,
8712 ("both %<signed%> and %<float%> in "
8713 "declaration specifiers"));
8714 else if (specs->typespec_word == cts_double)
8715 error_at (loc,
8716 ("both %<signed%> and %<double%> in "
8717 "declaration specifiers"));
8718 else if (specs->typespec_word == cts_dfloat32)
8719 error_at (loc,
8720 ("both %<signed%> and %<_Decimal32%> in "
8721 "declaration specifiers"));
8722 else if (specs->typespec_word == cts_dfloat64)
8723 error_at (loc,
8724 ("both %<signed%> and %<_Decimal64%> in "
8725 "declaration specifiers"));
8726 else if (specs->typespec_word == cts_dfloat128)
8727 error_at (loc,
8728 ("both %<signed%> and %<_Decimal128%> in "
8729 "declaration specifiers"));
8730 else
8731 specs->signed_p = true;
8732 break;
8733 case RID_UNSIGNED:
8734 dupe = specs->unsigned_p;
8735 if (specs->signed_p)
8736 error_at (loc,
8737 ("both %<signed%> and %<unsigned%> in "
8738 "declaration specifiers"));
8739 else if (specs->typespec_word == cts_void)
8740 error_at (loc,
8741 ("both %<unsigned%> and %<void%> in "
8742 "declaration specifiers"));
8743 else if (specs->typespec_word == cts_bool)
8744 error_at (loc,
8745 ("both %<unsigned%> and %<_Bool%> in "
8746 "declaration specifiers"));
8747 else if (specs->typespec_word == cts_float)
8748 error_at (loc,
8749 ("both %<unsigned%> and %<float%> in "
8750 "declaration specifiers"));
8751 else if (specs->typespec_word == cts_double)
8752 error_at (loc,
8753 ("both %<unsigned%> and %<double%> in "
8754 "declaration specifiers"));
8755 else if (specs->typespec_word == cts_dfloat32)
8756 error_at (loc,
8757 ("both %<unsigned%> and %<_Decimal32%> in "
8758 "declaration specifiers"));
8759 else if (specs->typespec_word == cts_dfloat64)
8760 error_at (loc,
8761 ("both %<unsigned%> and %<_Decimal64%> in "
8762 "declaration specifiers"));
8763 else if (specs->typespec_word == cts_dfloat128)
8764 error_at (loc,
8765 ("both %<unsigned%> and %<_Decimal128%> in "
8766 "declaration specifiers"));
8767 else
8768 specs->unsigned_p = true;
8769 break;
8770 case RID_COMPLEX:
8771 dupe = specs->complex_p;
8772 if (!flag_isoc99 && !in_system_header)
8773 pedwarn (loc, OPT_pedantic,
8774 "ISO C90 does not support complex types");
8775 if (specs->typespec_word == cts_void)
8776 error_at (loc,
8777 ("both %<complex%> and %<void%> in "
8778 "declaration specifiers"));
8779 else if (specs->typespec_word == cts_bool)
8780 error_at (loc,
8781 ("both %<complex%> and %<_Bool%> in "
8782 "declaration specifiers"));
8783 else if (specs->typespec_word == cts_dfloat32)
8784 error_at (loc,
8785 ("both %<complex%> and %<_Decimal32%> in "
8786 "declaration specifiers"));
8787 else if (specs->typespec_word == cts_dfloat64)
8788 error_at (loc,
8789 ("both %<complex%> and %<_Decimal64%> in "
8790 "declaration specifiers"));
8791 else if (specs->typespec_word == cts_dfloat128)
8792 error_at (loc,
8793 ("both %<complex%> and %<_Decimal128%> in "
8794 "declaration specifiers"));
8795 else if (specs->typespec_word == cts_fract)
8796 error_at (loc,
8797 ("both %<complex%> and %<_Fract%> in "
8798 "declaration specifiers"));
8799 else if (specs->typespec_word == cts_accum)
8800 error_at (loc,
8801 ("both %<complex%> and %<_Accum%> in "
8802 "declaration specifiers"));
8803 else if (specs->saturating_p)
8804 error_at (loc,
8805 ("both %<complex%> and %<_Sat%> in "
8806 "declaration specifiers"));
8807 else
8808 specs->complex_p = true;
8809 break;
8810 case RID_SAT:
8811 dupe = specs->saturating_p;
8812 pedwarn (loc, OPT_pedantic,
8813 "ISO C does not support saturating types");
8814 if (specs->typespec_word == cts_void)
8815 error_at (loc,
8816 ("both %<_Sat%> and %<void%> in "
8817 "declaration specifiers"));
8818 else if (specs->typespec_word == cts_bool)
8819 error_at (loc,
8820 ("both %<_Sat%> and %<_Bool%> in "
8821 "declaration specifiers"));
8822 else if (specs->typespec_word == cts_char)
8823 error_at (loc,
8824 ("both %<_Sat%> and %<char%> in "
8825 "declaration specifiers"));
8826 else if (specs->typespec_word == cts_int)
8827 error_at (loc,
8828 ("both %<_Sat%> and %<int%> in "
8829 "declaration specifiers"));
8830 else if (specs->typespec_word == cts_float)
8831 error_at (loc,
8832 ("both %<_Sat%> and %<float%> in "
8833 "declaration specifiers"));
8834 else if (specs->typespec_word == cts_double)
8835 error_at (loc,
8836 ("both %<_Sat%> and %<double%> in "
8837 "declaration specifiers"));
8838 else if (specs->typespec_word == cts_dfloat32)
8839 error_at (loc,
8840 ("both %<_Sat%> and %<_Decimal32%> in "
8841 "declaration specifiers"));
8842 else if (specs->typespec_word == cts_dfloat64)
8843 error_at (loc,
8844 ("both %<_Sat%> and %<_Decimal64%> in "
8845 "declaration specifiers"));
8846 else if (specs->typespec_word == cts_dfloat128)
8847 error_at (loc,
8848 ("both %<_Sat%> and %<_Decimal128%> in "
8849 "declaration specifiers"));
8850 else if (specs->complex_p)
8851 error_at (loc,
8852 ("both %<_Sat%> and %<complex%> in "
8853 "declaration specifiers"));
8854 else
8855 specs->saturating_p = true;
8856 break;
8857 default:
8858 gcc_unreachable ();
8861 if (dupe)
8862 error_at (loc, "duplicate %qE", type);
8864 return specs;
8866 else
8868 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
8869 "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
8870 if (specs->typespec_word != cts_none)
8872 error_at (loc,
8873 "two or more data types in declaration specifiers");
8874 return specs;
8876 switch (i)
8878 case RID_VOID:
8879 if (specs->long_p)
8880 error_at (loc,
8881 ("both %<long%> and %<void%> in "
8882 "declaration specifiers"));
8883 else if (specs->short_p)
8884 error_at (loc,
8885 ("both %<short%> and %<void%> in "
8886 "declaration specifiers"));
8887 else if (specs->signed_p)
8888 error_at (loc,
8889 ("both %<signed%> and %<void%> in "
8890 "declaration specifiers"));
8891 else if (specs->unsigned_p)
8892 error_at (loc,
8893 ("both %<unsigned%> and %<void%> in "
8894 "declaration specifiers"));
8895 else if (specs->complex_p)
8896 error_at (loc,
8897 ("both %<complex%> and %<void%> in "
8898 "declaration specifiers"));
8899 else if (specs->saturating_p)
8900 error_at (loc,
8901 ("both %<_Sat%> and %<void%> in "
8902 "declaration specifiers"));
8903 else
8904 specs->typespec_word = cts_void;
8905 return specs;
8906 case RID_BOOL:
8907 if (specs->long_p)
8908 error_at (loc,
8909 ("both %<long%> and %<_Bool%> in "
8910 "declaration specifiers"));
8911 else if (specs->short_p)
8912 error_at (loc,
8913 ("both %<short%> and %<_Bool%> in "
8914 "declaration specifiers"));
8915 else if (specs->signed_p)
8916 error_at (loc,
8917 ("both %<signed%> and %<_Bool%> in "
8918 "declaration specifiers"));
8919 else if (specs->unsigned_p)
8920 error_at (loc,
8921 ("both %<unsigned%> and %<_Bool%> in "
8922 "declaration specifiers"));
8923 else if (specs->complex_p)
8924 error_at (loc,
8925 ("both %<complex%> and %<_Bool%> in "
8926 "declaration specifiers"));
8927 else if (specs->saturating_p)
8928 error_at (loc,
8929 ("both %<_Sat%> and %<_Bool%> in "
8930 "declaration specifiers"));
8931 else
8932 specs->typespec_word = cts_bool;
8933 return specs;
8934 case RID_CHAR:
8935 if (specs->long_p)
8936 error_at (loc,
8937 ("both %<long%> and %<char%> in "
8938 "declaration specifiers"));
8939 else if (specs->short_p)
8940 error_at (loc,
8941 ("both %<short%> and %<char%> in "
8942 "declaration specifiers"));
8943 else if (specs->saturating_p)
8944 error_at (loc,
8945 ("both %<_Sat%> and %<char%> in "
8946 "declaration specifiers"));
8947 else
8948 specs->typespec_word = cts_char;
8949 return specs;
8950 case RID_INT:
8951 if (specs->saturating_p)
8952 error_at (loc,
8953 ("both %<_Sat%> and %<int%> in "
8954 "declaration specifiers"));
8955 else
8956 specs->typespec_word = cts_int;
8957 return specs;
8958 case RID_FLOAT:
8959 if (specs->long_p)
8960 error_at (loc,
8961 ("both %<long%> and %<float%> in "
8962 "declaration specifiers"));
8963 else if (specs->short_p)
8964 error_at (loc,
8965 ("both %<short%> and %<float%> in "
8966 "declaration specifiers"));
8967 else if (specs->signed_p)
8968 error_at (loc,
8969 ("both %<signed%> and %<float%> in "
8970 "declaration specifiers"));
8971 else if (specs->unsigned_p)
8972 error_at (loc,
8973 ("both %<unsigned%> and %<float%> in "
8974 "declaration specifiers"));
8975 else if (specs->saturating_p)
8976 error_at (loc,
8977 ("both %<_Sat%> and %<float%> in "
8978 "declaration specifiers"));
8979 else
8980 specs->typespec_word = cts_float;
8981 return specs;
8982 case RID_DOUBLE:
8983 if (specs->long_long_p)
8984 error_at (loc,
8985 ("both %<long long%> and %<double%> in "
8986 "declaration specifiers"));
8987 else if (specs->short_p)
8988 error_at (loc,
8989 ("both %<short%> and %<double%> in "
8990 "declaration specifiers"));
8991 else if (specs->signed_p)
8992 error_at (loc,
8993 ("both %<signed%> and %<double%> in "
8994 "declaration specifiers"));
8995 else if (specs->unsigned_p)
8996 error_at (loc,
8997 ("both %<unsigned%> and %<double%> in "
8998 "declaration specifiers"));
8999 else if (specs->saturating_p)
9000 error_at (loc,
9001 ("both %<_Sat%> and %<double%> in "
9002 "declaration specifiers"));
9003 else
9004 specs->typespec_word = cts_double;
9005 return specs;
9006 case RID_DFLOAT32:
9007 case RID_DFLOAT64:
9008 case RID_DFLOAT128:
9010 const char *str;
9011 if (i == RID_DFLOAT32)
9012 str = "_Decimal32";
9013 else if (i == RID_DFLOAT64)
9014 str = "_Decimal64";
9015 else
9016 str = "_Decimal128";
9017 if (specs->long_long_p)
9018 error_at (loc,
9019 ("both %<long long%> and %<%s%> in "
9020 "declaration specifiers"),
9021 str);
9022 if (specs->long_p)
9023 error_at (loc,
9024 ("both %<long%> and %<%s%> in "
9025 "declaration specifiers"),
9026 str);
9027 else if (specs->short_p)
9028 error_at (loc,
9029 ("both %<short%> and %<%s%> in "
9030 "declaration specifiers"),
9031 str);
9032 else if (specs->signed_p)
9033 error_at (loc,
9034 ("both %<signed%> and %<%s%> in "
9035 "declaration specifiers"),
9036 str);
9037 else if (specs->unsigned_p)
9038 error_at (loc,
9039 ("both %<unsigned%> and %<%s%> in "
9040 "declaration specifiers"),
9041 str);
9042 else if (specs->complex_p)
9043 error_at (loc,
9044 ("both %<complex%> and %<%s%> in "
9045 "declaration specifiers"),
9046 str);
9047 else if (specs->saturating_p)
9048 error_at (loc,
9049 ("both %<_Sat%> and %<%s%> in "
9050 "declaration specifiers"),
9051 str);
9052 else if (i == RID_DFLOAT32)
9053 specs->typespec_word = cts_dfloat32;
9054 else if (i == RID_DFLOAT64)
9055 specs->typespec_word = cts_dfloat64;
9056 else
9057 specs->typespec_word = cts_dfloat128;
9059 if (!targetm.decimal_float_supported_p ())
9060 error_at (loc,
9061 ("decimal floating point not supported "
9062 "for this target"));
9063 pedwarn (loc, OPT_pedantic,
9064 "ISO C does not support decimal floating point");
9065 return specs;
9066 case RID_FRACT:
9067 case RID_ACCUM:
9069 const char *str;
9070 if (i == RID_FRACT)
9071 str = "_Fract";
9072 else
9073 str = "_Accum";
9074 if (specs->complex_p)
9075 error_at (loc,
9076 ("both %<complex%> and %<%s%> in "
9077 "declaration specifiers"),
9078 str);
9079 else if (i == RID_FRACT)
9080 specs->typespec_word = cts_fract;
9081 else
9082 specs->typespec_word = cts_accum;
9084 if (!targetm.fixed_point_supported_p ())
9085 error_at (loc,
9086 "fixed-point types not supported for this target");
9087 pedwarn (loc, OPT_pedantic,
9088 "ISO C does not support fixed-point types");
9089 return specs;
9090 default:
9091 /* ObjC reserved word "id", handled below. */
9092 break;
9097 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
9098 form of ObjC type, cases such as "int" and "long" being handled
9099 above), a TYPE (struct, union, enum and typeof specifiers) or an
9100 ERROR_MARK. In none of these cases may there have previously
9101 been any type specifiers. */
9102 if (specs->type || specs->typespec_word != cts_none
9103 || specs->long_p || specs->short_p || specs->signed_p
9104 || specs->unsigned_p || specs->complex_p)
9105 error_at (loc, "two or more data types in declaration specifiers");
9106 else if (TREE_CODE (type) == TYPE_DECL)
9108 if (TREE_TYPE (type) == error_mark_node)
9109 ; /* Allow the type to default to int to avoid cascading errors. */
9110 else
9112 specs->type = TREE_TYPE (type);
9113 specs->decl_attr = DECL_ATTRIBUTES (type);
9114 specs->typedef_p = true;
9115 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
9117 /* If this typedef name is defined in a struct, then a C++
9118 lookup would return a different value. */
9119 if (warn_cxx_compat
9120 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
9121 warning_at (loc, OPT_Wc___compat,
9122 "C++ lookup of %qD would return a field, not a type",
9123 type);
9125 /* If we are parsing a struct, record that a struct field
9126 used a typedef. */
9127 if (warn_cxx_compat && struct_parse_info != NULL)
9128 VEC_safe_push (tree, heap, struct_parse_info->typedefs_seen, type);
9131 else if (TREE_CODE (type) == IDENTIFIER_NODE)
9133 tree t = lookup_name (type);
9134 if (!t || TREE_CODE (t) != TYPE_DECL)
9135 error_at (loc, "%qE fails to be a typedef or built in type", type);
9136 else if (TREE_TYPE (t) == error_mark_node)
9138 else
9139 specs->type = TREE_TYPE (t);
9141 else if (TREE_CODE (type) != ERROR_MARK)
9143 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
9144 specs->tag_defined_p = true;
9145 if (spec.kind == ctsk_typeof)
9147 specs->typedef_p = true;
9148 if (spec.expr)
9150 if (specs->expr)
9151 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
9152 specs->expr, spec.expr);
9153 else
9154 specs->expr = spec.expr;
9155 specs->expr_const_operands &= spec.expr_const_operands;
9158 specs->type = type;
9161 return specs;
9164 /* Add the storage class specifier or function specifier SCSPEC to the
9165 declaration specifiers SPECS, returning SPECS. */
9167 struct c_declspecs *
9168 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
9170 enum rid i;
9171 enum c_storage_class n = csc_none;
9172 bool dupe = false;
9173 specs->declspecs_seen_p = true;
9174 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
9175 && C_IS_RESERVED_WORD (scspec));
9176 i = C_RID_CODE (scspec);
9177 if (specs->non_sc_seen_p)
9178 warning (OPT_Wold_style_declaration,
9179 "%qE is not at beginning of declaration", scspec);
9180 switch (i)
9182 case RID_INLINE:
9183 /* C99 permits duplicate inline. Although of doubtful utility,
9184 it seems simplest to permit it in gnu89 mode as well, as
9185 there is also little utility in maintaining this as a
9186 difference between gnu89 and C99 inline. */
9187 dupe = false;
9188 specs->inline_p = true;
9189 break;
9190 case RID_THREAD:
9191 dupe = specs->thread_p;
9192 if (specs->storage_class == csc_auto)
9193 error ("%<__thread%> used with %<auto%>");
9194 else if (specs->storage_class == csc_register)
9195 error ("%<__thread%> used with %<register%>");
9196 else if (specs->storage_class == csc_typedef)
9197 error ("%<__thread%> used with %<typedef%>");
9198 else
9199 specs->thread_p = true;
9200 break;
9201 case RID_AUTO:
9202 n = csc_auto;
9203 break;
9204 case RID_EXTERN:
9205 n = csc_extern;
9206 /* Diagnose "__thread extern". */
9207 if (specs->thread_p)
9208 error ("%<__thread%> before %<extern%>");
9209 break;
9210 case RID_REGISTER:
9211 n = csc_register;
9212 break;
9213 case RID_STATIC:
9214 n = csc_static;
9215 /* Diagnose "__thread static". */
9216 if (specs->thread_p)
9217 error ("%<__thread%> before %<static%>");
9218 break;
9219 case RID_TYPEDEF:
9220 n = csc_typedef;
9221 break;
9222 default:
9223 gcc_unreachable ();
9225 if (n != csc_none && n == specs->storage_class)
9226 dupe = true;
9227 if (dupe)
9228 error ("duplicate %qE", scspec);
9229 if (n != csc_none)
9231 if (specs->storage_class != csc_none && n != specs->storage_class)
9233 error ("multiple storage classes in declaration specifiers");
9235 else
9237 specs->storage_class = n;
9238 if (n != csc_extern && n != csc_static && specs->thread_p)
9240 error ("%<__thread%> used with %qE", scspec);
9241 specs->thread_p = false;
9245 return specs;
9248 /* Add the attributes ATTRS to the declaration specifiers SPECS,
9249 returning SPECS. */
9251 struct c_declspecs *
9252 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
9254 specs->attrs = chainon (attrs, specs->attrs);
9255 specs->declspecs_seen_p = true;
9256 return specs;
9259 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
9260 specifiers with any other type specifier to determine the resulting
9261 type. This is where ISO C checks on complex types are made, since
9262 "_Complex long" is a prefix of the valid ISO C type "_Complex long
9263 double". */
9265 struct c_declspecs *
9266 finish_declspecs (struct c_declspecs *specs)
9268 /* If a type was specified as a whole, we have no modifiers and are
9269 done. */
9270 if (specs->type != NULL_TREE)
9272 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
9273 && !specs->signed_p && !specs->unsigned_p
9274 && !specs->complex_p);
9275 return specs;
9278 /* If none of "void", "_Bool", "char", "int", "float" or "double"
9279 has been specified, treat it as "int" unless "_Complex" is
9280 present and there are no other specifiers. If we just have
9281 "_Complex", it is equivalent to "_Complex double", but e.g.
9282 "_Complex short" is equivalent to "_Complex short int". */
9283 if (specs->typespec_word == cts_none)
9285 if (specs->saturating_p)
9287 error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
9288 if (!targetm.fixed_point_supported_p ())
9289 error ("fixed-point types not supported for this target");
9290 specs->typespec_word = cts_fract;
9292 else if (specs->long_p || specs->short_p
9293 || specs->signed_p || specs->unsigned_p)
9295 specs->typespec_word = cts_int;
9297 else if (specs->complex_p)
9299 specs->typespec_word = cts_double;
9300 pedwarn (input_location, OPT_pedantic,
9301 "ISO C does not support plain %<complex%> meaning "
9302 "%<double complex%>");
9304 else
9306 specs->typespec_word = cts_int;
9307 specs->default_int_p = true;
9308 /* We don't diagnose this here because grokdeclarator will
9309 give more specific diagnostics according to whether it is
9310 a function definition. */
9314 /* If "signed" was specified, record this to distinguish "int" and
9315 "signed int" in the case of a bit-field with
9316 -funsigned-bitfields. */
9317 specs->explicit_signed_p = specs->signed_p;
9319 /* Now compute the actual type. */
9320 switch (specs->typespec_word)
9322 case cts_void:
9323 gcc_assert (!specs->long_p && !specs->short_p
9324 && !specs->signed_p && !specs->unsigned_p
9325 && !specs->complex_p);
9326 specs->type = void_type_node;
9327 break;
9328 case cts_bool:
9329 gcc_assert (!specs->long_p && !specs->short_p
9330 && !specs->signed_p && !specs->unsigned_p
9331 && !specs->complex_p);
9332 specs->type = boolean_type_node;
9333 break;
9334 case cts_char:
9335 gcc_assert (!specs->long_p && !specs->short_p);
9336 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9337 if (specs->signed_p)
9338 specs->type = signed_char_type_node;
9339 else if (specs->unsigned_p)
9340 specs->type = unsigned_char_type_node;
9341 else
9342 specs->type = char_type_node;
9343 if (specs->complex_p)
9345 pedwarn (input_location, OPT_pedantic,
9346 "ISO C does not support complex integer types");
9347 specs->type = build_complex_type (specs->type);
9349 break;
9350 case cts_int:
9351 gcc_assert (!(specs->long_p && specs->short_p));
9352 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9353 if (specs->long_long_p)
9354 specs->type = (specs->unsigned_p
9355 ? long_long_unsigned_type_node
9356 : long_long_integer_type_node);
9357 else if (specs->long_p)
9358 specs->type = (specs->unsigned_p
9359 ? long_unsigned_type_node
9360 : long_integer_type_node);
9361 else if (specs->short_p)
9362 specs->type = (specs->unsigned_p
9363 ? short_unsigned_type_node
9364 : short_integer_type_node);
9365 else
9366 specs->type = (specs->unsigned_p
9367 ? unsigned_type_node
9368 : integer_type_node);
9369 if (specs->complex_p)
9371 pedwarn (input_location, OPT_pedantic,
9372 "ISO C does not support complex integer types");
9373 specs->type = build_complex_type (specs->type);
9375 break;
9376 case cts_float:
9377 gcc_assert (!specs->long_p && !specs->short_p
9378 && !specs->signed_p && !specs->unsigned_p);
9379 specs->type = (specs->complex_p
9380 ? complex_float_type_node
9381 : float_type_node);
9382 break;
9383 case cts_double:
9384 gcc_assert (!specs->long_long_p && !specs->short_p
9385 && !specs->signed_p && !specs->unsigned_p);
9386 if (specs->long_p)
9388 specs->type = (specs->complex_p
9389 ? complex_long_double_type_node
9390 : long_double_type_node);
9392 else
9394 specs->type = (specs->complex_p
9395 ? complex_double_type_node
9396 : double_type_node);
9398 break;
9399 case cts_dfloat32:
9400 case cts_dfloat64:
9401 case cts_dfloat128:
9402 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
9403 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
9404 if (specs->typespec_word == cts_dfloat32)
9405 specs->type = dfloat32_type_node;
9406 else if (specs->typespec_word == cts_dfloat64)
9407 specs->type = dfloat64_type_node;
9408 else
9409 specs->type = dfloat128_type_node;
9410 break;
9411 case cts_fract:
9412 gcc_assert (!specs->complex_p);
9413 if (!targetm.fixed_point_supported_p ())
9414 specs->type = integer_type_node;
9415 else if (specs->saturating_p)
9417 if (specs->long_long_p)
9418 specs->type = specs->unsigned_p
9419 ? sat_unsigned_long_long_fract_type_node
9420 : sat_long_long_fract_type_node;
9421 else if (specs->long_p)
9422 specs->type = specs->unsigned_p
9423 ? sat_unsigned_long_fract_type_node
9424 : sat_long_fract_type_node;
9425 else if (specs->short_p)
9426 specs->type = specs->unsigned_p
9427 ? sat_unsigned_short_fract_type_node
9428 : sat_short_fract_type_node;
9429 else
9430 specs->type = specs->unsigned_p
9431 ? sat_unsigned_fract_type_node
9432 : sat_fract_type_node;
9434 else
9436 if (specs->long_long_p)
9437 specs->type = specs->unsigned_p
9438 ? unsigned_long_long_fract_type_node
9439 : long_long_fract_type_node;
9440 else if (specs->long_p)
9441 specs->type = specs->unsigned_p
9442 ? unsigned_long_fract_type_node
9443 : long_fract_type_node;
9444 else if (specs->short_p)
9445 specs->type = specs->unsigned_p
9446 ? unsigned_short_fract_type_node
9447 : short_fract_type_node;
9448 else
9449 specs->type = specs->unsigned_p
9450 ? unsigned_fract_type_node
9451 : fract_type_node;
9453 break;
9454 case cts_accum:
9455 gcc_assert (!specs->complex_p);
9456 if (!targetm.fixed_point_supported_p ())
9457 specs->type = integer_type_node;
9458 else if (specs->saturating_p)
9460 if (specs->long_long_p)
9461 specs->type = specs->unsigned_p
9462 ? sat_unsigned_long_long_accum_type_node
9463 : sat_long_long_accum_type_node;
9464 else if (specs->long_p)
9465 specs->type = specs->unsigned_p
9466 ? sat_unsigned_long_accum_type_node
9467 : sat_long_accum_type_node;
9468 else if (specs->short_p)
9469 specs->type = specs->unsigned_p
9470 ? sat_unsigned_short_accum_type_node
9471 : sat_short_accum_type_node;
9472 else
9473 specs->type = specs->unsigned_p
9474 ? sat_unsigned_accum_type_node
9475 : sat_accum_type_node;
9477 else
9479 if (specs->long_long_p)
9480 specs->type = specs->unsigned_p
9481 ? unsigned_long_long_accum_type_node
9482 : long_long_accum_type_node;
9483 else if (specs->long_p)
9484 specs->type = specs->unsigned_p
9485 ? unsigned_long_accum_type_node
9486 : long_accum_type_node;
9487 else if (specs->short_p)
9488 specs->type = specs->unsigned_p
9489 ? unsigned_short_accum_type_node
9490 : short_accum_type_node;
9491 else
9492 specs->type = specs->unsigned_p
9493 ? unsigned_accum_type_node
9494 : accum_type_node;
9496 break;
9497 default:
9498 gcc_unreachable ();
9501 return specs;
9504 /* A subroutine of c_write_global_declarations. Perform final processing
9505 on one file scope's declarations (or the external scope's declarations),
9506 GLOBALS. */
9508 static void
9509 c_write_global_declarations_1 (tree globals, bool ext_scope)
9511 tree decl;
9512 bool reconsider;
9514 /* Process the decls in the order they were written. */
9515 for (decl = globals; decl; decl = TREE_CHAIN (decl))
9517 /* Check for used but undefined static functions using the C
9518 standard's definition of "used", and set TREE_NO_WARNING so
9519 that check_global_declarations doesn't repeat the check.
9521 Issue an error if an IFUNC function is undefined. */
9522 if (TREE_CODE (decl) == FUNCTION_DECL
9523 && DECL_INITIAL (decl) == 0
9524 && DECL_EXTERNAL (decl))
9526 if (DECL_IS_IFUNC (decl))
9528 if (ext_scope || !TREE_PUBLIC (decl))
9530 error_at (input_location,
9531 "indirect function %q+F never defined",
9532 decl);
9533 TREE_NO_WARNING (decl) = 1;
9536 else if (!TREE_PUBLIC (decl)
9537 && C_DECL_USED (decl))
9539 pedwarn (input_location, 0, "%q+F used but never defined",
9540 decl);
9541 TREE_NO_WARNING (decl) = 1;
9545 wrapup_global_declaration_1 (decl);
9550 reconsider = false;
9551 for (decl = globals; decl; decl = TREE_CHAIN (decl))
9552 reconsider |= wrapup_global_declaration_2 (decl);
9554 while (reconsider);
9556 for (decl = globals; decl; decl = TREE_CHAIN (decl))
9557 check_global_declaration_1 (decl);
9560 /* A subroutine of c_write_global_declarations Emit debug information for each
9561 of the declarations in GLOBALS. */
9563 static void
9564 c_write_global_declarations_2 (tree globals)
9566 tree decl;
9568 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
9569 debug_hooks->global_decl (decl);
9572 /* Preserve the external declarations scope across a garbage collect. */
9573 static GTY(()) tree ext_block;
9575 void
9576 c_write_global_declarations (void)
9578 tree t;
9580 /* We don't want to do this if generating a PCH. */
9581 if (pch_file)
9582 return;
9584 /* Don't waste time on further processing if -fsyntax-only.
9585 Continue for warning and errors issued during lowering though. */
9586 if (flag_syntax_only)
9587 return;
9589 /* Close the external scope. */
9590 ext_block = pop_scope ();
9591 external_scope = 0;
9592 gcc_assert (!current_scope);
9594 if (ext_block)
9596 tree tmp = BLOCK_VARS (ext_block);
9597 int flags;
9598 FILE * stream = dump_begin (TDI_tu, &flags);
9599 if (stream && tmp)
9601 dump_node (tmp, flags & ~TDF_SLIM, stream);
9602 dump_end (TDI_tu, stream);
9606 /* Process all file scopes in this compilation, and the external_scope,
9607 through wrapup_global_declarations and check_global_declarations. */
9608 for (t = all_translation_units; t; t = TREE_CHAIN (t))
9609 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)), false);
9610 c_write_global_declarations_1 (BLOCK_VARS (ext_block), true);
9612 /* We're done parsing; proceed to optimize and emit assembly.
9613 FIXME: shouldn't be the front end's responsibility to call this. */
9614 cgraph_finalize_compilation_unit ();
9616 /* After cgraph has had a chance to emit everything that's going to
9617 be emitted, output debug information for globals. */
9618 if (errorcount == 0 && sorrycount == 0)
9620 timevar_push (TV_SYMOUT);
9621 for (t = all_translation_units; t; t = TREE_CHAIN (t))
9622 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
9623 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
9624 timevar_pop (TV_SYMOUT);
9627 ext_block = NULL;
9630 #include "gt-c-decl.h"