* passes.c (init_optimization_passes): Remove two copies of ehcleanup
[official-gcc/constexpr.git] / gcc / c-decl.c
blob8930d93c0cda2d3745624d7309205bb655da36f6
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
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 "langhooks.h"
54 #include "tree-mudflap.h"
55 #include "gimple.h"
56 #include "tree-iterator.h"
57 #include "diagnostic.h"
58 #include "tree-dump.h"
59 #include "cgraph.h"
60 #include "hashtab.h"
61 #include "libfuncs.h"
62 #include "except.h"
63 #include "langhooks-def.h"
64 #include "pointer-set.h"
65 #include "gimple.h"
67 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
68 enum decl_context
69 { NORMAL, /* Ordinary declaration */
70 FUNCDEF, /* Function definition */
71 PARM, /* Declaration of parm before function body */
72 FIELD, /* Declaration inside struct or union */
73 TYPENAME}; /* Typename (inside cast or sizeof) */
75 /* States indicating how grokdeclarator() should handle declspecs marked
76 with __attribute__((deprecated)). An object declared as
77 __attribute__((deprecated)) suppresses warnings of uses of other
78 deprecated items. */
80 enum deprecated_states {
81 DEPRECATED_NORMAL,
82 DEPRECATED_SUPPRESS
86 /* Nonzero if we have seen an invalid cross reference
87 to a struct, union, or enum, but not yet printed the message. */
88 tree pending_invalid_xref;
90 /* File and line to appear in the eventual error message. */
91 location_t pending_invalid_xref_location;
93 /* True means we've initialized exception handling. */
94 bool c_eh_initialized_p;
96 /* The file and line that the prototype came from if this is an
97 old-style definition; used for diagnostics in
98 store_parm_decls_oldstyle. */
100 static location_t current_function_prototype_locus;
102 /* Whether this prototype was built-in. */
104 static bool current_function_prototype_built_in;
106 /* The argument type information of this prototype. */
108 static tree current_function_prototype_arg_types;
110 /* The argument information structure for the function currently being
111 defined. */
113 static struct c_arg_info *current_function_arg_info;
115 /* The obstack on which parser and related data structures, which are
116 not live beyond their top-level declaration or definition, are
117 allocated. */
118 struct obstack parser_obstack;
120 /* The current statement tree. */
122 static GTY(()) struct stmt_tree_s c_stmt_tree;
124 /* State saving variables. */
125 tree c_break_label;
126 tree c_cont_label;
128 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
129 included in this invocation. Note that the current translation
130 unit is not included in this list. */
132 static GTY(()) tree all_translation_units;
134 /* A list of decls to be made automatically visible in each file scope. */
135 static GTY(()) tree visible_builtins;
137 /* Set to 0 at beginning of a function definition, set to 1 if
138 a return statement that specifies a return value is seen. */
140 int current_function_returns_value;
142 /* Set to 0 at beginning of a function definition, set to 1 if
143 a return statement with no argument is seen. */
145 int current_function_returns_null;
147 /* Set to 0 at beginning of a function definition, set to 1 if
148 a call to a noreturn function is seen. */
150 int current_function_returns_abnormally;
152 /* Set to nonzero by `grokdeclarator' for a function
153 whose return type is defaulted, if warnings for this are desired. */
155 static int warn_about_return_type;
157 /* Nonzero when the current toplevel function contains a declaration
158 of a nested function which is never defined. */
160 static bool undef_nested_function;
162 /* True means global_bindings_p should return false even if the scope stack
163 says we are in file scope. */
164 bool c_override_global_bindings_to_false;
167 /* Each c_binding structure describes one binding of an identifier to
168 a decl. All the decls in a scope - irrespective of namespace - are
169 chained together by the ->prev field, which (as the name implies)
170 runs in reverse order. All the decls in a given namespace bound to
171 a given identifier are chained by the ->shadowed field, which runs
172 from inner to outer scopes.
174 The ->decl field usually points to a DECL node, but there are two
175 exceptions. In the namespace of type tags, the bound entity is a
176 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
177 identifier is encountered, it is bound to error_mark_node to
178 suppress further errors about that identifier in the current
179 function.
181 The ->type field stores the type of the declaration in this scope;
182 if NULL, the type is the type of the ->decl field. This is only of
183 relevance for objects with external or internal linkage which may
184 be redeclared in inner scopes, forming composite types that only
185 persist for the duration of those scopes. In the external scope,
186 this stores the composite of all the types declared for this
187 object, visible or not. The ->inner_comp field (used only at file
188 scope) stores whether an incomplete array type at file scope was
189 completed at an inner scope to an array size other than 1.
191 The depth field is copied from the scope structure that holds this
192 decl. It is used to preserve the proper ordering of the ->shadowed
193 field (see bind()) and also for a handful of special-case checks.
194 Finally, the invisible bit is true for a decl which should be
195 ignored for purposes of normal name lookup, and the nested bit is
196 true for a decl that's been bound a second time in an inner scope;
197 in all such cases, the binding in the outer scope will have its
198 invisible bit true. */
200 struct c_binding GTY((chain_next ("%h.prev")))
202 tree decl; /* the decl bound */
203 tree type; /* the type in this scope */
204 tree id; /* the identifier it's bound to */
205 struct c_binding *prev; /* the previous decl in this scope */
206 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
207 unsigned int depth : 28; /* depth of this scope */
208 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
209 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
210 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
211 /* one free bit */
212 location_t locus; /* location for nested bindings */
214 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
215 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
216 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
217 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
219 #define I_SYMBOL_BINDING(node) \
220 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
221 #define I_SYMBOL_DECL(node) \
222 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
224 #define I_TAG_BINDING(node) \
225 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
226 #define I_TAG_DECL(node) \
227 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
229 #define I_LABEL_BINDING(node) \
230 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
231 #define I_LABEL_DECL(node) \
232 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
234 /* Each C symbol points to three linked lists of c_binding structures.
235 These describe the values of the identifier in the three different
236 namespaces defined by the language. */
238 struct lang_identifier GTY(())
240 struct c_common_identifier common_id;
241 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
242 struct c_binding *tag_binding; /* struct/union/enum tags */
243 struct c_binding *label_binding; /* labels */
246 /* Validate c-lang.c's assumptions. */
247 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
248 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
250 /* The resulting tree type. */
252 union lang_tree_node
253 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
254 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))")))
256 union tree_node GTY ((tag ("0"),
257 desc ("tree_node_structure (&%h)")))
258 generic;
259 struct lang_identifier GTY ((tag ("1"))) identifier;
262 /* Each c_scope structure describes the complete contents of one
263 scope. Four scopes are distinguished specially: the innermost or
264 current scope, the innermost function scope, the file scope (always
265 the second to outermost) and the outermost or external scope.
267 Most declarations are recorded in the current scope.
269 All normal label declarations are recorded in the innermost
270 function scope, as are bindings of undeclared identifiers to
271 error_mark_node. (GCC permits nested functions as an extension,
272 hence the 'innermost' qualifier.) Explicitly declared labels
273 (using the __label__ extension) appear in the current scope.
275 Being in the file scope (current_scope == file_scope) causes
276 special behavior in several places below. Also, under some
277 conditions the Objective-C front end records declarations in the
278 file scope even though that isn't the current scope.
280 All declarations with external linkage are recorded in the external
281 scope, even if they aren't visible there; this models the fact that
282 such declarations are visible to the entire program, and (with a
283 bit of cleverness, see pushdecl) allows diagnosis of some violations
284 of C99 6.2.2p7 and 6.2.7p2:
286 If, within the same translation unit, the same identifier appears
287 with both internal and external linkage, the behavior is
288 undefined.
290 All declarations that refer to the same object or function shall
291 have compatible type; otherwise, the behavior is undefined.
293 Initially only the built-in declarations, which describe compiler
294 intrinsic functions plus a subset of the standard library, are in
295 this scope.
297 The order of the blocks list matters, and it is frequently appended
298 to. To avoid having to walk all the way to the end of the list on
299 each insertion, or reverse the list later, we maintain a pointer to
300 the last list entry. (FIXME: It should be feasible to use a reversed
301 list here.)
303 The bindings list is strictly in reverse order of declarations;
304 pop_scope relies on this. */
307 struct c_scope GTY((chain_next ("%h.outer")))
309 /* The scope containing this one. */
310 struct c_scope *outer;
312 /* The next outermost function scope. */
313 struct c_scope *outer_function;
315 /* All bindings in this scope. */
316 struct c_binding *bindings;
318 /* For each scope (except the global one), a chain of BLOCK nodes
319 for all the scopes that were entered and exited one level down. */
320 tree blocks;
321 tree blocks_last;
323 /* The depth of this scope. Used to keep the ->shadowed chain of
324 bindings sorted innermost to outermost. */
325 unsigned int depth : 28;
327 /* True if we are currently filling this scope with parameter
328 declarations. */
329 BOOL_BITFIELD parm_flag : 1;
331 /* True if we saw [*] in this scope. Used to give an error messages
332 if these appears in a function definition. */
333 BOOL_BITFIELD had_vla_unspec : 1;
335 /* True if we already complained about forward parameter decls
336 in this scope. This prevents double warnings on
337 foo (int a; int b; ...) */
338 BOOL_BITFIELD warned_forward_parm_decls : 1;
340 /* True if this is the outermost block scope of a function body.
341 This scope contains the parameters, the local variables declared
342 in the outermost block, and all the labels (except those in
343 nested functions, or declared at block scope with __label__). */
344 BOOL_BITFIELD function_body : 1;
346 /* True means make a BLOCK for this scope no matter what. */
347 BOOL_BITFIELD keep : 1;
350 /* The scope currently in effect. */
352 static GTY(()) struct c_scope *current_scope;
354 /* The innermost function scope. Ordinary (not explicitly declared)
355 labels, bindings to error_mark_node, and the lazily-created
356 bindings of __func__ and its friends get this scope. */
358 static GTY(()) struct c_scope *current_function_scope;
360 /* The C file scope. This is reset for each input translation unit. */
362 static GTY(()) struct c_scope *file_scope;
364 /* The outermost scope. This is used for all declarations with
365 external linkage, and only these, hence the name. */
367 static GTY(()) struct c_scope *external_scope;
369 /* A chain of c_scope structures awaiting reuse. */
371 static GTY((deletable)) struct c_scope *scope_freelist;
373 /* A chain of c_binding structures awaiting reuse. */
375 static GTY((deletable)) struct c_binding *binding_freelist;
377 /* Append VAR to LIST in scope SCOPE. */
378 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
379 struct c_scope *s_ = (scope); \
380 tree d_ = (decl); \
381 if (s_->list##_last) \
382 BLOCK_CHAIN (s_->list##_last) = d_; \
383 else \
384 s_->list = d_; \
385 s_->list##_last = d_; \
386 } while (0)
388 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
389 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
390 struct c_scope *t_ = (tscope); \
391 struct c_scope *f_ = (fscope); \
392 if (t_->to##_last) \
393 BLOCK_CHAIN (t_->to##_last) = f_->from; \
394 else \
395 t_->to = f_->from; \
396 t_->to##_last = f_->from##_last; \
397 } while (0)
399 /* True means unconditionally make a BLOCK for the next scope pushed. */
401 static bool keep_next_level_flag;
403 /* True means the next call to push_scope will be the outermost scope
404 of a function body, so do not push a new scope, merely cease
405 expecting parameter decls. */
407 static bool next_is_function_body;
409 /* Forward declarations. */
410 static tree lookup_name_in_scope (tree, struct c_scope *);
411 static tree c_make_fname_decl (tree, int);
412 static tree grokdeclarator (const struct c_declarator *,
413 struct c_declspecs *,
414 enum decl_context, bool, tree *, tree *, tree *,
415 bool *, enum deprecated_states);
416 static tree grokparms (struct c_arg_info *, bool);
417 static void layout_array_type (tree);
419 /* T is a statement. Add it to the statement-tree. This is the
420 C/ObjC version--C++ has a slightly different version of this
421 function. */
423 tree
424 add_stmt (tree t)
426 enum tree_code code = TREE_CODE (t);
428 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
430 if (!EXPR_HAS_LOCATION (t))
431 SET_EXPR_LOCATION (t, input_location);
434 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
435 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
437 /* Add T to the statement-tree. Non-side-effect statements need to be
438 recorded during statement expressions. */
439 append_to_statement_list_force (t, &cur_stmt_list);
441 return t;
445 void
446 c_print_identifier (FILE *file, tree node, int indent)
448 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
449 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
450 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
451 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
453 tree rid = ridpointers[C_RID_CODE (node)];
454 indent_to (file, indent + 4);
455 fprintf (file, "rid %p \"%s\"",
456 (void *) rid, IDENTIFIER_POINTER (rid));
460 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
461 which may be any of several kinds of DECL or TYPE or error_mark_node,
462 in the scope SCOPE. */
463 static void
464 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
465 bool nested, location_t locus)
467 struct c_binding *b, **here;
469 if (binding_freelist)
471 b = binding_freelist;
472 binding_freelist = b->prev;
474 else
475 b = GGC_NEW (struct c_binding);
477 b->shadowed = 0;
478 b->decl = decl;
479 b->id = name;
480 b->depth = scope->depth;
481 b->invisible = invisible;
482 b->nested = nested;
483 b->inner_comp = 0;
484 b->locus = locus;
486 b->type = 0;
488 b->prev = scope->bindings;
489 scope->bindings = b;
491 if (!name)
492 return;
494 switch (TREE_CODE (decl))
496 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
497 case ENUMERAL_TYPE:
498 case UNION_TYPE:
499 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
500 case VAR_DECL:
501 case FUNCTION_DECL:
502 case TYPE_DECL:
503 case CONST_DECL:
504 case PARM_DECL:
505 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
507 default:
508 gcc_unreachable ();
511 /* Locate the appropriate place in the chain of shadowed decls
512 to insert this binding. Normally, scope == current_scope and
513 this does nothing. */
514 while (*here && (*here)->depth > scope->depth)
515 here = &(*here)->shadowed;
517 b->shadowed = *here;
518 *here = b;
521 /* Clear the binding structure B, stick it on the binding_freelist,
522 and return the former value of b->prev. This is used by pop_scope
523 and get_parm_info to iterate destructively over all the bindings
524 from a given scope. */
525 static struct c_binding *
526 free_binding_and_advance (struct c_binding *b)
528 struct c_binding *prev = b->prev;
530 memset (b, 0, sizeof (struct c_binding));
531 b->prev = binding_freelist;
532 binding_freelist = b;
534 return prev;
538 /* Hook called at end of compilation to assume 1 elt
539 for a file-scope tentative array defn that wasn't complete before. */
541 void
542 c_finish_incomplete_decl (tree decl)
544 if (TREE_CODE (decl) == VAR_DECL)
546 tree type = TREE_TYPE (decl);
547 if (type != error_mark_node
548 && TREE_CODE (type) == ARRAY_TYPE
549 && !DECL_EXTERNAL (decl)
550 && TYPE_DOMAIN (type) == 0)
552 warning (0, "array %q+D assumed to have one element", decl);
554 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
556 layout_decl (decl, 0);
561 /* The Objective-C front-end often needs to determine the current scope. */
563 void *
564 objc_get_current_scope (void)
566 return current_scope;
569 /* The following function is used only by Objective-C. It needs to live here
570 because it accesses the innards of c_scope. */
572 void
573 objc_mark_locals_volatile (void *enclosing_blk)
575 struct c_scope *scope;
576 struct c_binding *b;
578 for (scope = current_scope;
579 scope && scope != enclosing_blk;
580 scope = scope->outer)
582 for (b = scope->bindings; b; b = b->prev)
583 objc_volatilize_decl (b->decl);
585 /* Do not climb up past the current function. */
586 if (scope->function_body)
587 break;
591 /* Nonzero if we are currently in file scope. */
594 global_bindings_p (void)
596 return current_scope == file_scope && !c_override_global_bindings_to_false;
599 void
600 keep_next_level (void)
602 keep_next_level_flag = true;
605 /* Identify this scope as currently being filled with parameters. */
607 void
608 declare_parm_level (void)
610 current_scope->parm_flag = true;
613 void
614 push_scope (void)
616 if (next_is_function_body)
618 /* This is the transition from the parameters to the top level
619 of the function body. These are the same scope
620 (C99 6.2.1p4,6) so we do not push another scope structure.
621 next_is_function_body is set only by store_parm_decls, which
622 in turn is called when and only when we are about to
623 encounter the opening curly brace for the function body.
625 The outermost block of a function always gets a BLOCK node,
626 because the debugging output routines expect that each
627 function has at least one BLOCK. */
628 current_scope->parm_flag = false;
629 current_scope->function_body = true;
630 current_scope->keep = true;
631 current_scope->outer_function = current_function_scope;
632 current_function_scope = current_scope;
634 keep_next_level_flag = false;
635 next_is_function_body = false;
637 else
639 struct c_scope *scope;
640 if (scope_freelist)
642 scope = scope_freelist;
643 scope_freelist = scope->outer;
645 else
646 scope = GGC_CNEW (struct c_scope);
648 scope->keep = keep_next_level_flag;
649 scope->outer = current_scope;
650 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
652 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
653 possible. */
654 if (current_scope && scope->depth == 0)
656 scope->depth--;
657 sorry ("GCC supports only %u nested scopes", scope->depth);
660 current_scope = scope;
661 keep_next_level_flag = false;
665 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
667 static void
668 set_type_context (tree type, tree context)
670 for (type = TYPE_MAIN_VARIANT (type); type;
671 type = TYPE_NEXT_VARIANT (type))
672 TYPE_CONTEXT (type) = context;
675 /* Exit a scope. Restore the state of the identifier-decl mappings
676 that were in effect when this scope was entered. Return a BLOCK
677 node containing all the DECLs in this scope that are of interest
678 to debug info generation. */
680 tree
681 pop_scope (void)
683 struct c_scope *scope = current_scope;
684 tree block, context, p;
685 struct c_binding *b;
687 bool functionbody = scope->function_body;
688 bool keep = functionbody || scope->keep || scope->bindings;
690 c_end_vm_scope (scope->depth);
692 /* If appropriate, create a BLOCK to record the decls for the life
693 of this function. */
694 block = 0;
695 if (keep)
697 block = make_node (BLOCK);
698 BLOCK_SUBBLOCKS (block) = scope->blocks;
699 TREE_USED (block) = 1;
701 /* In each subblock, record that this is its superior. */
702 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
703 BLOCK_SUPERCONTEXT (p) = block;
705 BLOCK_VARS (block) = 0;
708 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
709 scope must be set so that they point to the appropriate
710 construct, i.e. either to the current FUNCTION_DECL node, or
711 else to the BLOCK node we just constructed.
713 Note that for tagged types whose scope is just the formal
714 parameter list for some function type specification, we can't
715 properly set their TYPE_CONTEXTs here, because we don't have a
716 pointer to the appropriate FUNCTION_TYPE node readily available
717 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
718 type nodes get set in `grokdeclarator' as soon as we have created
719 the FUNCTION_TYPE node which will represent the "scope" for these
720 "parameter list local" tagged types. */
721 if (scope->function_body)
722 context = current_function_decl;
723 else if (scope == file_scope)
725 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
726 TREE_CHAIN (file_decl) = all_translation_units;
727 all_translation_units = file_decl;
728 context = file_decl;
730 else
731 context = block;
733 /* Clear all bindings in this scope. */
734 for (b = scope->bindings; b; b = free_binding_and_advance (b))
736 p = b->decl;
737 switch (TREE_CODE (p))
739 case LABEL_DECL:
740 /* Warnings for unused labels, errors for undefined labels. */
741 if (TREE_USED (p) && !DECL_INITIAL (p))
743 error ("label %q+D used but not defined", p);
744 DECL_INITIAL (p) = error_mark_node;
746 else
747 warn_for_unused_label (p);
749 /* Labels go in BLOCK_VARS. */
750 TREE_CHAIN (p) = BLOCK_VARS (block);
751 BLOCK_VARS (block) = p;
752 gcc_assert (I_LABEL_BINDING (b->id) == b);
753 I_LABEL_BINDING (b->id) = b->shadowed;
754 break;
756 case ENUMERAL_TYPE:
757 case UNION_TYPE:
758 case RECORD_TYPE:
759 set_type_context (p, context);
761 /* Types may not have tag-names, in which case the type
762 appears in the bindings list with b->id NULL. */
763 if (b->id)
765 gcc_assert (I_TAG_BINDING (b->id) == b);
766 I_TAG_BINDING (b->id) = b->shadowed;
768 break;
770 case FUNCTION_DECL:
771 /* Propagate TREE_ADDRESSABLE from nested functions to their
772 containing functions. */
773 if (!TREE_ASM_WRITTEN (p)
774 && DECL_INITIAL (p) != 0
775 && TREE_ADDRESSABLE (p)
776 && DECL_ABSTRACT_ORIGIN (p) != 0
777 && DECL_ABSTRACT_ORIGIN (p) != p)
778 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
779 if (!DECL_EXTERNAL (p)
780 && !DECL_INITIAL (p)
781 && scope != file_scope
782 && scope != external_scope)
784 error ("nested function %q+D declared but never defined", p);
785 undef_nested_function = true;
787 else if (DECL_DECLARED_INLINE_P (p)
788 && TREE_PUBLIC (p)
789 && !DECL_INITIAL (p))
791 /* C99 6.7.4p6: "a function with external linkage... declared
792 with an inline function specifier ... shall also be defined
793 in the same translation unit." */
794 if (!flag_gnu89_inline)
795 pedwarn (input_location, 0,
796 "inline function %q+D declared but never defined", p);
797 DECL_EXTERNAL (p) = 1;
800 goto common_symbol;
802 case VAR_DECL:
803 /* Warnings for unused variables. */
804 if (!TREE_USED (p)
805 && !TREE_NO_WARNING (p)
806 && !DECL_IN_SYSTEM_HEADER (p)
807 && DECL_NAME (p)
808 && !DECL_ARTIFICIAL (p)
809 && scope != file_scope
810 && scope != external_scope)
811 warning (OPT_Wunused_variable, "unused variable %q+D", p);
813 if (b->inner_comp)
815 error ("type of array %q+D completed incompatibly with"
816 " implicit initialization", p);
819 /* Fall through. */
820 case TYPE_DECL:
821 case CONST_DECL:
822 common_symbol:
823 /* All of these go in BLOCK_VARS, but only if this is the
824 binding in the home scope. */
825 if (!b->nested)
827 TREE_CHAIN (p) = BLOCK_VARS (block);
828 BLOCK_VARS (block) = p;
830 else if (VAR_OR_FUNCTION_DECL_P (p))
832 /* For block local externs add a special
833 DECL_EXTERNAL decl for debug info generation. */
834 tree extp = copy_node (p);
836 DECL_EXTERNAL (extp) = 1;
837 TREE_STATIC (extp) = 0;
838 TREE_PUBLIC (extp) = 1;
839 DECL_INITIAL (extp) = NULL_TREE;
840 DECL_LANG_SPECIFIC (extp) = NULL;
841 DECL_CONTEXT (extp) = current_function_decl;
842 if (TREE_CODE (p) == FUNCTION_DECL)
844 DECL_RESULT (extp) = NULL_TREE;
845 DECL_SAVED_TREE (extp) = NULL_TREE;
846 DECL_STRUCT_FUNCTION (extp) = NULL;
848 if (b->locus != UNKNOWN_LOCATION)
849 DECL_SOURCE_LOCATION (extp) = b->locus;
850 TREE_CHAIN (extp) = BLOCK_VARS (block);
851 BLOCK_VARS (block) = extp;
853 /* If this is the file scope, and we are processing more
854 than one translation unit in this compilation, set
855 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
856 This makes same_translation_unit_p work, and causes
857 static declarations to be given disambiguating suffixes. */
858 if (scope == file_scope && num_in_fnames > 1)
860 DECL_CONTEXT (p) = context;
861 if (TREE_CODE (p) == TYPE_DECL)
862 set_type_context (TREE_TYPE (p), context);
865 /* Fall through. */
866 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
867 already been put there by store_parm_decls. Unused-
868 parameter warnings are handled by function.c.
869 error_mark_node obviously does not go in BLOCK_VARS and
870 does not get unused-variable warnings. */
871 case PARM_DECL:
872 case ERROR_MARK:
873 /* It is possible for a decl not to have a name. We get
874 here with b->id NULL in this case. */
875 if (b->id)
877 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
878 I_SYMBOL_BINDING (b->id) = b->shadowed;
879 if (b->shadowed && b->shadowed->type)
880 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
882 break;
884 default:
885 gcc_unreachable ();
890 /* Dispose of the block that we just made inside some higher level. */
891 if ((scope->function_body || scope == file_scope) && context)
893 DECL_INITIAL (context) = block;
894 BLOCK_SUPERCONTEXT (block) = context;
896 else if (scope->outer)
898 if (block)
899 SCOPE_LIST_APPEND (scope->outer, blocks, block);
900 /* If we did not make a block for the scope just exited, any
901 blocks made for inner scopes must be carried forward so they
902 will later become subblocks of something else. */
903 else if (scope->blocks)
904 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
907 /* Pop the current scope, and free the structure for reuse. */
908 current_scope = scope->outer;
909 if (scope->function_body)
910 current_function_scope = scope->outer_function;
912 memset (scope, 0, sizeof (struct c_scope));
913 scope->outer = scope_freelist;
914 scope_freelist = scope;
916 return block;
919 void
920 push_file_scope (void)
922 tree decl;
924 if (file_scope)
925 return;
927 push_scope ();
928 file_scope = current_scope;
930 start_fname_decls ();
932 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
933 bind (DECL_NAME (decl), decl, file_scope,
934 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
937 void
938 pop_file_scope (void)
940 /* In case there were missing closebraces, get us back to the global
941 binding level. */
942 while (current_scope != file_scope)
943 pop_scope ();
945 /* __FUNCTION__ is defined at file scope (""). This
946 call may not be necessary as my tests indicate it
947 still works without it. */
948 finish_fname_decls ();
950 /* This is the point to write out a PCH if we're doing that.
951 In that case we do not want to do anything else. */
952 if (pch_file)
954 c_common_write_pch ();
955 return;
958 /* Pop off the file scope and close this translation unit. */
959 pop_scope ();
960 file_scope = 0;
962 maybe_apply_pending_pragma_weaks ();
963 cgraph_finalize_compilation_unit ();
967 /* Push a definition or a declaration of struct, union or enum tag "name".
968 "type" should be the type node.
969 We assume that the tag "name" is not already defined.
971 Note that the definition may really be just a forward reference.
972 In that case, the TYPE_SIZE will be zero. */
974 static void
975 pushtag (tree name, tree type)
977 /* Record the identifier as the type's name if it has none. */
978 if (name && !TYPE_NAME (type))
979 TYPE_NAME (type) = name;
980 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false,
981 UNKNOWN_LOCATION);
983 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
984 tagged type we just added to the current scope. This fake
985 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
986 to output a representation of a tagged type, and it also gives
987 us a convenient place to record the "scope start" address for the
988 tagged type. */
990 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
992 /* An approximation for now, so we can tell this is a function-scope tag.
993 This will be updated in pop_scope. */
994 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
997 /* Subroutine of compare_decls. Allow harmless mismatches in return
998 and argument types provided that the type modes match. This function
999 return a unified type given a suitable match, and 0 otherwise. */
1001 static tree
1002 match_builtin_function_types (tree newtype, tree oldtype)
1004 tree newrettype, oldrettype;
1005 tree newargs, oldargs;
1006 tree trytype, tryargs;
1008 /* Accept the return type of the new declaration if same modes. */
1009 oldrettype = TREE_TYPE (oldtype);
1010 newrettype = TREE_TYPE (newtype);
1012 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1013 return 0;
1015 oldargs = TYPE_ARG_TYPES (oldtype);
1016 newargs = TYPE_ARG_TYPES (newtype);
1017 tryargs = newargs;
1019 while (oldargs || newargs)
1021 if (!oldargs
1022 || !newargs
1023 || !TREE_VALUE (oldargs)
1024 || !TREE_VALUE (newargs)
1025 || TYPE_MODE (TREE_VALUE (oldargs))
1026 != TYPE_MODE (TREE_VALUE (newargs)))
1027 return 0;
1029 oldargs = TREE_CHAIN (oldargs);
1030 newargs = TREE_CHAIN (newargs);
1033 trytype = build_function_type (newrettype, tryargs);
1034 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1037 /* Subroutine of diagnose_mismatched_decls. Check for function type
1038 mismatch involving an empty arglist vs a nonempty one and give clearer
1039 diagnostics. */
1040 static void
1041 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1042 tree newtype, tree oldtype)
1044 tree t;
1046 if (TREE_CODE (olddecl) != FUNCTION_DECL
1047 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1048 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1050 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1051 return;
1053 t = TYPE_ARG_TYPES (oldtype);
1054 if (t == 0)
1055 t = TYPE_ARG_TYPES (newtype);
1056 for (; t; t = TREE_CHAIN (t))
1058 tree type = TREE_VALUE (t);
1060 if (TREE_CHAIN (t) == 0
1061 && TYPE_MAIN_VARIANT (type) != void_type_node)
1063 inform (input_location, "a parameter list with an ellipsis can%'t match "
1064 "an empty parameter name list declaration");
1065 break;
1068 if (c_type_promotes_to (type) != type)
1070 inform (input_location, "an argument type that has a default promotion can%'t match "
1071 "an empty parameter name list declaration");
1072 break;
1077 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1078 old-style function definition, NEWDECL is a prototype declaration.
1079 Diagnose inconsistencies in the argument list. Returns TRUE if
1080 the prototype is compatible, FALSE if not. */
1081 static bool
1082 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1084 tree newargs, oldargs;
1085 int i;
1087 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1089 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1090 newargs = TYPE_ARG_TYPES (newtype);
1091 i = 1;
1093 for (;;)
1095 tree oldargtype = TREE_VALUE (oldargs);
1096 tree newargtype = TREE_VALUE (newargs);
1098 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1099 return false;
1101 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1102 newargtype = TYPE_MAIN_VARIANT (newargtype);
1104 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1105 break;
1107 /* Reaching the end of just one list means the two decls don't
1108 agree on the number of arguments. */
1109 if (END_OF_ARGLIST (oldargtype))
1111 error ("prototype for %q+D declares more arguments "
1112 "than previous old-style definition", newdecl);
1113 return false;
1115 else if (END_OF_ARGLIST (newargtype))
1117 error ("prototype for %q+D declares fewer arguments "
1118 "than previous old-style definition", newdecl);
1119 return false;
1122 /* Type for passing arg must be consistent with that declared
1123 for the arg. */
1124 else if (!comptypes (oldargtype, newargtype))
1126 error ("prototype for %q+D declares argument %d"
1127 " with incompatible type",
1128 newdecl, i);
1129 return false;
1132 oldargs = TREE_CHAIN (oldargs);
1133 newargs = TREE_CHAIN (newargs);
1134 i++;
1137 /* If we get here, no errors were found, but do issue a warning
1138 for this poor-style construct. */
1139 warning (0, "prototype for %q+D follows non-prototype definition",
1140 newdecl);
1141 return true;
1142 #undef END_OF_ARGLIST
1145 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1146 first in a pair of mismatched declarations, using the diagnostic
1147 function DIAG. */
1148 static void
1149 locate_old_decl (tree decl)
1151 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1153 else if (DECL_INITIAL (decl))
1154 inform (input_location, "previous definition of %q+D was here", decl);
1155 else if (C_DECL_IMPLICIT (decl))
1156 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1157 else
1158 inform (input_location, "previous declaration of %q+D was here", decl);
1161 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1162 Returns true if the caller should proceed to merge the two, false
1163 if OLDDECL should simply be discarded. As a side effect, issues
1164 all necessary diagnostics for invalid or poor-style combinations.
1165 If it returns true, writes the types of NEWDECL and OLDDECL to
1166 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1167 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1169 static bool
1170 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1171 tree *newtypep, tree *oldtypep)
1173 tree newtype, oldtype;
1174 bool pedwarned = false;
1175 bool warned = false;
1176 bool retval = true;
1178 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1179 && DECL_EXTERNAL (DECL))
1181 /* If we have error_mark_node for either decl or type, just discard
1182 the previous decl - we're in an error cascade already. */
1183 if (olddecl == error_mark_node || newdecl == error_mark_node)
1184 return false;
1185 *oldtypep = oldtype = TREE_TYPE (olddecl);
1186 *newtypep = newtype = TREE_TYPE (newdecl);
1187 if (oldtype == error_mark_node || newtype == error_mark_node)
1188 return false;
1190 /* Two different categories of symbol altogether. This is an error
1191 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1192 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1194 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1195 && DECL_BUILT_IN (olddecl)
1196 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1198 error ("%q+D redeclared as different kind of symbol", newdecl);
1199 locate_old_decl (olddecl);
1201 else if (TREE_PUBLIC (newdecl))
1202 warning (0, "built-in function %q+D declared as non-function",
1203 newdecl);
1204 else
1205 warning (OPT_Wshadow, "declaration of %q+D shadows "
1206 "a built-in function", newdecl);
1207 return false;
1210 /* Enumerators have no linkage, so may only be declared once in a
1211 given scope. */
1212 if (TREE_CODE (olddecl) == CONST_DECL)
1214 error ("redeclaration of enumerator %q+D", newdecl);
1215 locate_old_decl (olddecl);
1216 return false;
1219 if (!comptypes (oldtype, newtype))
1221 if (TREE_CODE (olddecl) == FUNCTION_DECL
1222 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1224 /* Accept harmless mismatch in function types.
1225 This is for the ffs and fprintf builtins. */
1226 tree trytype = match_builtin_function_types (newtype, oldtype);
1228 if (trytype && comptypes (newtype, trytype))
1229 *oldtypep = oldtype = trytype;
1230 else
1232 /* If types don't match for a built-in, throw away the
1233 built-in. No point in calling locate_old_decl here, it
1234 won't print anything. */
1235 warning (0, "conflicting types for built-in function %q+D",
1236 newdecl);
1237 return false;
1240 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1241 && DECL_IS_BUILTIN (olddecl))
1243 /* A conflicting function declaration for a predeclared
1244 function that isn't actually built in. Objective C uses
1245 these. The new declaration silently overrides everything
1246 but the volatility (i.e. noreturn) indication. See also
1247 below. FIXME: Make Objective C use normal builtins. */
1248 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1249 return false;
1251 /* Permit void foo (...) to match int foo (...) if the latter is
1252 the definition and implicit int was used. See
1253 c-torture/compile/920625-2.c. */
1254 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1255 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1256 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1257 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1259 pedwarned = pedwarn (input_location, 0,
1260 "conflicting types for %q+D", newdecl);
1261 /* Make sure we keep void as the return type. */
1262 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1263 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1265 /* Permit void foo (...) to match an earlier call to foo (...) with
1266 no declared type (thus, implicitly int). */
1267 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1268 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1269 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1270 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1272 pedwarned = pedwarn (input_location, 0,
1273 "conflicting types for %q+D", newdecl);
1274 /* Make sure we keep void as the return type. */
1275 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1277 else
1279 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1280 error ("conflicting type qualifiers for %q+D", newdecl);
1281 else
1282 error ("conflicting types for %q+D", newdecl);
1283 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1284 locate_old_decl (olddecl);
1285 return false;
1289 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1290 but silently ignore the redeclaration if either is in a system
1291 header. (Conflicting redeclarations were handled above.) */
1292 if (TREE_CODE (newdecl) == TYPE_DECL)
1294 if (DECL_IN_SYSTEM_HEADER (newdecl)
1295 || DECL_IN_SYSTEM_HEADER (olddecl)
1296 || TREE_NO_WARNING (newdecl)
1297 || TREE_NO_WARNING (olddecl))
1298 return true; /* Allow OLDDECL to continue in use. */
1300 error ("redefinition of typedef %q+D", newdecl);
1301 locate_old_decl (olddecl);
1302 return false;
1305 /* Function declarations can either be 'static' or 'extern' (no
1306 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1307 can never conflict with each other on account of linkage
1308 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1309 gnu89 mode permits two definitions if one is 'extern inline' and
1310 one is not. The non- extern-inline definition supersedes the
1311 extern-inline definition. */
1313 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1315 /* If you declare a built-in function name as static, or
1316 define the built-in with an old-style definition (so we
1317 can't validate the argument list) the built-in definition is
1318 overridden, but optionally warn this was a bad choice of name. */
1319 if (DECL_BUILT_IN (olddecl)
1320 && !C_DECL_DECLARED_BUILTIN (olddecl)
1321 && (!TREE_PUBLIC (newdecl)
1322 || (DECL_INITIAL (newdecl)
1323 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1325 warning (OPT_Wshadow, "declaration of %q+D shadows "
1326 "a built-in function", newdecl);
1327 /* Discard the old built-in function. */
1328 return false;
1331 if (DECL_INITIAL (newdecl))
1333 if (DECL_INITIAL (olddecl))
1335 /* If both decls are in the same TU and the new declaration
1336 isn't overriding an extern inline reject the new decl.
1337 In c99, no overriding is allowed in the same translation
1338 unit. */
1339 if ((!DECL_EXTERN_INLINE (olddecl)
1340 || DECL_EXTERN_INLINE (newdecl)
1341 || (!flag_gnu89_inline
1342 && (!DECL_DECLARED_INLINE_P (olddecl)
1343 || !lookup_attribute ("gnu_inline",
1344 DECL_ATTRIBUTES (olddecl)))
1345 && (!DECL_DECLARED_INLINE_P (newdecl)
1346 || !lookup_attribute ("gnu_inline",
1347 DECL_ATTRIBUTES (newdecl))))
1349 && same_translation_unit_p (newdecl, olddecl))
1351 error ("redefinition of %q+D", newdecl);
1352 locate_old_decl (olddecl);
1353 return false;
1357 /* If we have a prototype after an old-style function definition,
1358 the argument types must be checked specially. */
1359 else if (DECL_INITIAL (olddecl)
1360 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1361 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1362 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1364 locate_old_decl (olddecl);
1365 return false;
1367 /* A non-static declaration (even an "extern") followed by a
1368 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1369 The same is true for a static forward declaration at block
1370 scope followed by a non-static declaration/definition at file
1371 scope. Static followed by non-static at the same scope is
1372 not undefined behavior, and is the most convenient way to get
1373 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1374 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1375 we do diagnose it if -Wtraditional. */
1376 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1378 /* Two exceptions to the rule. If olddecl is an extern
1379 inline, or a predeclared function that isn't actually
1380 built in, newdecl silently overrides olddecl. The latter
1381 occur only in Objective C; see also above. (FIXME: Make
1382 Objective C use normal builtins.) */
1383 if (!DECL_IS_BUILTIN (olddecl)
1384 && !DECL_EXTERN_INLINE (olddecl))
1386 error ("static declaration of %q+D follows "
1387 "non-static declaration", newdecl);
1388 locate_old_decl (olddecl);
1390 return false;
1392 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1394 if (DECL_CONTEXT (olddecl))
1396 error ("non-static declaration of %q+D follows "
1397 "static declaration", newdecl);
1398 locate_old_decl (olddecl);
1399 return false;
1401 else if (warn_traditional)
1403 warned |= warning (OPT_Wtraditional,
1404 "non-static declaration of %q+D "
1405 "follows static declaration", newdecl);
1409 /* Make sure gnu_inline attribute is either not present, or
1410 present on all inline decls. */
1411 if (DECL_DECLARED_INLINE_P (olddecl)
1412 && DECL_DECLARED_INLINE_P (newdecl))
1414 bool newa = lookup_attribute ("gnu_inline",
1415 DECL_ATTRIBUTES (newdecl)) != NULL;
1416 bool olda = lookup_attribute ("gnu_inline",
1417 DECL_ATTRIBUTES (olddecl)) != NULL;
1418 if (newa != olda)
1420 error ("%<gnu_inline%> attribute present on %q+D",
1421 newa ? newdecl : olddecl);
1422 error ("%Jbut not here", newa ? olddecl : newdecl);
1426 else if (TREE_CODE (newdecl) == VAR_DECL)
1428 /* Only variables can be thread-local, and all declarations must
1429 agree on this property. */
1430 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1432 /* Nothing to check. Since OLDDECL is marked threadprivate
1433 and NEWDECL does not have a thread-local attribute, we
1434 will merge the threadprivate attribute into NEWDECL. */
1437 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1439 if (DECL_THREAD_LOCAL_P (newdecl))
1440 error ("thread-local declaration of %q+D follows "
1441 "non-thread-local declaration", newdecl);
1442 else
1443 error ("non-thread-local declaration of %q+D follows "
1444 "thread-local declaration", newdecl);
1446 locate_old_decl (olddecl);
1447 return false;
1450 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1451 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1453 error ("redefinition of %q+D", newdecl);
1454 locate_old_decl (olddecl);
1455 return false;
1458 /* Objects declared at file scope: if the first declaration had
1459 external linkage (even if it was an external reference) the
1460 second must have external linkage as well, or the behavior is
1461 undefined. If the first declaration had internal linkage, then
1462 the second must too, or else be an external reference (in which
1463 case the composite declaration still has internal linkage).
1464 As for function declarations, we warn about the static-then-
1465 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1466 if (DECL_FILE_SCOPE_P (newdecl)
1467 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1469 if (DECL_EXTERNAL (newdecl))
1471 if (!DECL_FILE_SCOPE_P (olddecl))
1473 error ("extern declaration of %q+D follows "
1474 "declaration with no linkage", newdecl);
1475 locate_old_decl (olddecl);
1476 return false;
1478 else if (warn_traditional)
1480 warned |= warning (OPT_Wtraditional,
1481 "non-static declaration of %q+D "
1482 "follows static declaration", newdecl);
1485 else
1487 if (TREE_PUBLIC (newdecl))
1488 error ("non-static declaration of %q+D follows "
1489 "static declaration", newdecl);
1490 else
1491 error ("static declaration of %q+D follows "
1492 "non-static declaration", newdecl);
1494 locate_old_decl (olddecl);
1495 return false;
1498 /* Two objects with the same name declared at the same block
1499 scope must both be external references (6.7p3). */
1500 else if (!DECL_FILE_SCOPE_P (newdecl))
1502 if (DECL_EXTERNAL (newdecl))
1504 /* Extern with initializer at block scope, which will
1505 already have received an error. */
1507 else if (DECL_EXTERNAL (olddecl))
1509 error ("declaration of %q+D with no linkage follows "
1510 "extern declaration", newdecl);
1511 locate_old_decl (olddecl);
1513 else
1515 error ("redeclaration of %q+D with no linkage", newdecl);
1516 locate_old_decl (olddecl);
1519 return false;
1523 /* warnings */
1524 /* All decls must agree on a visibility. */
1525 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1526 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1527 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1529 warned |= warning (0, "redeclaration of %q+D with different visibility "
1530 "(old visibility preserved)", newdecl);
1533 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1535 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1536 if (DECL_DECLARED_INLINE_P (newdecl)
1537 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1539 warned |= warning (OPT_Wattributes,
1540 "inline declaration of %qD follows "
1541 "declaration with attribute noinline", newdecl);
1543 else if (DECL_DECLARED_INLINE_P (olddecl)
1544 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1546 warned |= warning (OPT_Wattributes,
1547 "declaration of %q+D with attribute "
1548 "noinline follows inline declaration ", newdecl);
1551 else /* PARM_DECL, VAR_DECL */
1553 /* Redeclaration of a parameter is a constraint violation (this is
1554 not explicitly stated, but follows from C99 6.7p3 [no more than
1555 one declaration of the same identifier with no linkage in the
1556 same scope, except type tags] and 6.2.2p6 [parameters have no
1557 linkage]). We must check for a forward parameter declaration,
1558 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1559 an extension, the mandatory diagnostic for which is handled by
1560 mark_forward_parm_decls. */
1562 if (TREE_CODE (newdecl) == PARM_DECL
1563 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1565 error ("redefinition of parameter %q+D", newdecl);
1566 locate_old_decl (olddecl);
1567 return false;
1571 /* Optional warning for completely redundant decls. */
1572 if (!warned && !pedwarned
1573 && warn_redundant_decls
1574 /* Don't warn about a function declaration followed by a
1575 definition. */
1576 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1577 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1578 /* Don't warn about redundant redeclarations of builtins. */
1579 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1580 && !DECL_BUILT_IN (newdecl)
1581 && DECL_BUILT_IN (olddecl)
1582 && !C_DECL_DECLARED_BUILTIN (olddecl))
1583 /* Don't warn about an extern followed by a definition. */
1584 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1585 /* Don't warn about forward parameter decls. */
1586 && !(TREE_CODE (newdecl) == PARM_DECL
1587 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1588 /* Don't warn about a variable definition following a declaration. */
1589 && !(TREE_CODE (newdecl) == VAR_DECL
1590 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1592 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1593 newdecl);
1596 /* Report location of previous decl/defn. */
1597 if (warned || pedwarned)
1598 locate_old_decl (olddecl);
1600 #undef DECL_EXTERN_INLINE
1602 return retval;
1605 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1606 consistent with OLDDECL, but carries new information. Merge the
1607 new information into OLDDECL. This function issues no
1608 diagnostics. */
1610 static void
1611 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1613 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1614 && DECL_INITIAL (newdecl) != 0);
1615 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1616 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1617 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1618 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1619 bool extern_changed = false;
1621 /* For real parm decl following a forward decl, rechain the old decl
1622 in its new location and clear TREE_ASM_WRITTEN (it's not a
1623 forward decl anymore). */
1624 if (TREE_CODE (newdecl) == PARM_DECL
1625 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1627 struct c_binding *b, **here;
1629 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1630 if ((*here)->decl == olddecl)
1631 goto found;
1632 gcc_unreachable ();
1634 found:
1635 b = *here;
1636 *here = b->prev;
1637 b->prev = current_scope->bindings;
1638 current_scope->bindings = b;
1640 TREE_ASM_WRITTEN (olddecl) = 0;
1643 DECL_ATTRIBUTES (newdecl)
1644 = targetm.merge_decl_attributes (olddecl, newdecl);
1646 /* Merge the data types specified in the two decls. */
1647 TREE_TYPE (newdecl)
1648 = TREE_TYPE (olddecl)
1649 = composite_type (newtype, oldtype);
1651 /* Lay the type out, unless already done. */
1652 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1654 if (TREE_TYPE (newdecl) != error_mark_node)
1655 layout_type (TREE_TYPE (newdecl));
1656 if (TREE_CODE (newdecl) != FUNCTION_DECL
1657 && TREE_CODE (newdecl) != TYPE_DECL
1658 && TREE_CODE (newdecl) != CONST_DECL)
1659 layout_decl (newdecl, 0);
1661 else
1663 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1664 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1665 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1666 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1667 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1669 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1670 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
1674 /* Keep the old rtl since we can safely use it. */
1675 if (HAS_RTL_P (olddecl))
1676 COPY_DECL_RTL (olddecl, newdecl);
1678 /* Merge the type qualifiers. */
1679 if (TREE_READONLY (newdecl))
1680 TREE_READONLY (olddecl) = 1;
1682 if (TREE_THIS_VOLATILE (newdecl))
1683 TREE_THIS_VOLATILE (olddecl) = 1;
1685 /* Merge deprecatedness. */
1686 if (TREE_DEPRECATED (newdecl))
1687 TREE_DEPRECATED (olddecl) = 1;
1689 /* If a decl is in a system header and the other isn't, keep the one on the
1690 system header. Otherwise, keep source location of definition rather than
1691 declaration and of prototype rather than non-prototype unless that
1692 prototype is built-in. */
1693 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
1694 && DECL_IN_SYSTEM_HEADER (olddecl)
1695 && !DECL_IN_SYSTEM_HEADER (newdecl) )
1696 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1697 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
1698 && DECL_IN_SYSTEM_HEADER (newdecl)
1699 && !DECL_IN_SYSTEM_HEADER (olddecl))
1700 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
1701 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1702 || (old_is_prototype && !new_is_prototype
1703 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1704 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1706 /* Merge the initialization information. */
1707 if (DECL_INITIAL (newdecl) == 0)
1708 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1710 /* Merge the threadprivate attribute. */
1711 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1713 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1714 C_DECL_THREADPRIVATE_P (newdecl) = 1;
1717 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1719 /* Merge the section attribute.
1720 We want to issue an error if the sections conflict but that
1721 must be done later in decl_attributes since we are called
1722 before attributes are assigned. */
1723 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1724 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1726 /* Copy the assembler name.
1727 Currently, it can only be defined in the prototype. */
1728 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1730 /* Use visibility of whichever declaration had it specified */
1731 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1733 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1734 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1737 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1739 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1740 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1741 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1742 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1743 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1744 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1745 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1746 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
1747 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1748 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1749 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1752 /* Merge the storage class information. */
1753 merge_weak (newdecl, olddecl);
1755 /* For functions, static overrides non-static. */
1756 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1758 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1759 /* This is since we don't automatically
1760 copy the attributes of NEWDECL into OLDDECL. */
1761 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1762 /* If this clears `static', clear it in the identifier too. */
1763 if (!TREE_PUBLIC (olddecl))
1764 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1768 /* In c99, 'extern' declaration before (or after) 'inline' means this
1769 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1770 is present. */
1771 if (TREE_CODE (newdecl) == FUNCTION_DECL
1772 && !flag_gnu89_inline
1773 && (DECL_DECLARED_INLINE_P (newdecl)
1774 || DECL_DECLARED_INLINE_P (olddecl))
1775 && (!DECL_DECLARED_INLINE_P (newdecl)
1776 || !DECL_DECLARED_INLINE_P (olddecl)
1777 || !DECL_EXTERNAL (olddecl))
1778 && DECL_EXTERNAL (newdecl)
1779 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
1780 && !current_function_decl)
1781 DECL_EXTERNAL (newdecl) = 0;
1783 if (DECL_EXTERNAL (newdecl))
1785 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1786 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1788 /* An extern decl does not override previous storage class. */
1789 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1790 if (!DECL_EXTERNAL (newdecl))
1792 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1793 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1796 else
1798 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1799 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1802 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1804 /* If we're redefining a function previously defined as extern
1805 inline, make sure we emit debug info for the inline before we
1806 throw it away, in case it was inlined into a function that
1807 hasn't been written out yet. */
1808 if (new_is_definition && DECL_INITIAL (olddecl))
1809 /* The new defn must not be inline. */
1810 DECL_UNINLINABLE (newdecl) = 1;
1811 else
1813 /* If either decl says `inline', this fn is inline, unless
1814 its definition was passed already. */
1815 if (DECL_DECLARED_INLINE_P (newdecl)
1816 || DECL_DECLARED_INLINE_P (olddecl))
1817 DECL_DECLARED_INLINE_P (newdecl) = 1;
1819 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1820 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1822 DECL_DISREGARD_INLINE_LIMITS (newdecl)
1823 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
1824 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
1825 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
1828 if (DECL_BUILT_IN (olddecl))
1830 /* If redeclaring a builtin function, it stays built in.
1831 But it gets tagged as having been declared. */
1832 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1833 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1834 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1835 if (new_is_prototype)
1836 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1837 else
1838 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1839 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1842 /* Preserve function specific target and optimization options */
1843 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
1844 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
1845 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
1846 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
1848 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
1849 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
1850 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
1851 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
1853 /* Also preserve various other info from the definition. */
1854 if (!new_is_definition)
1856 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1857 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1858 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1859 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1860 gimple_set_body (newdecl, gimple_body (olddecl));
1861 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1863 /* See if we've got a function to instantiate from. */
1864 if (DECL_SAVED_TREE (olddecl))
1865 DECL_ABSTRACT_ORIGIN (newdecl)
1866 = DECL_ABSTRACT_ORIGIN (olddecl);
1870 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
1872 /* Merge the USED information. */
1873 if (TREE_USED (olddecl))
1874 TREE_USED (newdecl) = 1;
1875 else if (TREE_USED (newdecl))
1876 TREE_USED (olddecl) = 1;
1878 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1879 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1881 unsigned olddecl_uid = DECL_UID (olddecl);
1882 tree olddecl_context = DECL_CONTEXT (olddecl);
1884 memcpy ((char *) olddecl + sizeof (struct tree_common),
1885 (char *) newdecl + sizeof (struct tree_common),
1886 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1887 switch (TREE_CODE (olddecl))
1889 case FUNCTION_DECL:
1890 gimple_set_body (olddecl, gimple_body (newdecl));
1891 /* fall through */
1893 case FIELD_DECL:
1894 case VAR_DECL:
1895 case PARM_DECL:
1896 case LABEL_DECL:
1897 case RESULT_DECL:
1898 case CONST_DECL:
1899 case TYPE_DECL:
1900 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1901 (char *) newdecl + sizeof (struct tree_decl_common),
1902 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1903 break;
1905 default:
1907 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1908 (char *) newdecl + sizeof (struct tree_decl_common),
1909 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1911 DECL_UID (olddecl) = olddecl_uid;
1912 DECL_CONTEXT (olddecl) = olddecl_context;
1915 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1916 so that encode_section_info has a chance to look at the new decl
1917 flags and attributes. */
1918 if (DECL_RTL_SET_P (olddecl)
1919 && (TREE_CODE (olddecl) == FUNCTION_DECL
1920 || (TREE_CODE (olddecl) == VAR_DECL
1921 && TREE_STATIC (olddecl))))
1922 make_decl_rtl (olddecl);
1924 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
1925 and the definition is coming from the old version, cgraph needs
1926 to be called again. */
1927 if (extern_changed && !new_is_definition
1928 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
1929 cgraph_mark_if_needed (olddecl);
1932 /* Handle when a new declaration NEWDECL has the same name as an old
1933 one OLDDECL in the same binding contour. Prints an error message
1934 if appropriate.
1936 If safely possible, alter OLDDECL to look like NEWDECL, and return
1937 true. Otherwise, return false. */
1939 static bool
1940 duplicate_decls (tree newdecl, tree olddecl)
1942 tree newtype = NULL, oldtype = NULL;
1944 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1946 /* Avoid `unused variable' and other warnings for OLDDECL. */
1947 TREE_NO_WARNING (olddecl) = 1;
1948 return false;
1951 merge_decls (newdecl, olddecl, newtype, oldtype);
1952 return true;
1956 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1957 static void
1958 warn_if_shadowing (tree new_decl)
1960 struct c_binding *b;
1962 /* Shadow warnings wanted? */
1963 if (!warn_shadow
1964 /* No shadow warnings for internally generated vars. */
1965 || DECL_IS_BUILTIN (new_decl)
1966 /* No shadow warnings for vars made for inlining. */
1967 || DECL_FROM_INLINE (new_decl))
1968 return;
1970 /* Is anything being shadowed? Invisible decls do not count. */
1971 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1972 if (b->decl && b->decl != new_decl && !b->invisible)
1974 tree old_decl = b->decl;
1976 if (old_decl == error_mark_node)
1978 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
1979 "non-variable", new_decl);
1980 break;
1982 else if (TREE_CODE (old_decl) == PARM_DECL)
1983 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
1984 new_decl);
1985 else if (DECL_FILE_SCOPE_P (old_decl))
1986 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
1987 "declaration", new_decl);
1988 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1989 && DECL_BUILT_IN (old_decl))
1991 warning (OPT_Wshadow, "declaration of %q+D shadows "
1992 "a built-in function", new_decl);
1993 break;
1995 else
1996 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
1997 new_decl);
1999 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
2001 break;
2005 /* Record a decl-node X as belonging to the current lexical scope.
2006 Check for errors (such as an incompatible declaration for the same
2007 name already seen in the same scope).
2009 Returns either X or an old decl for the same name.
2010 If an old decl is returned, it may have been smashed
2011 to agree with what X says. */
2013 tree
2014 pushdecl (tree x)
2016 tree name = DECL_NAME (x);
2017 struct c_scope *scope = current_scope;
2018 struct c_binding *b;
2019 bool nested = false;
2020 location_t locus = DECL_SOURCE_LOCATION (x);
2022 /* Must set DECL_CONTEXT for everything not at file scope or
2023 DECL_FILE_SCOPE_P won't work. Local externs don't count
2024 unless they have initializers (which generate code). */
2025 if (current_function_decl
2026 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2027 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2028 DECL_CONTEXT (x) = current_function_decl;
2030 /* If this is of variably modified type, prevent jumping into its
2031 scope. */
2032 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2033 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2034 c_begin_vm_scope (scope->depth);
2036 /* Anonymous decls are just inserted in the scope. */
2037 if (!name)
2039 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2040 locus);
2041 return x;
2044 /* First, see if there is another declaration with the same name in
2045 the current scope. If there is, duplicate_decls may do all the
2046 work for us. If duplicate_decls returns false, that indicates
2047 two incompatible decls in the same scope; we are to silently
2048 replace the old one (duplicate_decls has issued all appropriate
2049 diagnostics). In particular, we should not consider possible
2050 duplicates in the external scope, or shadowing. */
2051 b = I_SYMBOL_BINDING (name);
2052 if (b && B_IN_SCOPE (b, scope))
2054 struct c_binding *b_ext, *b_use;
2055 tree type = TREE_TYPE (x);
2056 tree visdecl = b->decl;
2057 tree vistype = TREE_TYPE (visdecl);
2058 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2059 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2060 b->inner_comp = false;
2061 b_use = b;
2062 b_ext = b;
2063 /* If this is an external linkage declaration, we should check
2064 for compatibility with the type in the external scope before
2065 setting the type at this scope based on the visible
2066 information only. */
2067 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2069 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2070 b_ext = b_ext->shadowed;
2071 if (b_ext)
2073 b_use = b_ext;
2074 if (b_use->type)
2075 TREE_TYPE (b_use->decl) = b_use->type;
2078 if (duplicate_decls (x, b_use->decl))
2080 if (b_use != b)
2082 /* Save the updated type in the external scope and
2083 restore the proper type for this scope. */
2084 tree thistype;
2085 if (comptypes (vistype, type))
2086 thistype = composite_type (vistype, type);
2087 else
2088 thistype = TREE_TYPE (b_use->decl);
2089 b_use->type = TREE_TYPE (b_use->decl);
2090 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2091 && DECL_BUILT_IN (b_use->decl))
2092 thistype
2093 = build_type_attribute_variant (thistype,
2094 TYPE_ATTRIBUTES
2095 (b_use->type));
2096 TREE_TYPE (b_use->decl) = thistype;
2098 return b_use->decl;
2100 else
2101 goto skip_external_and_shadow_checks;
2104 /* All declarations with external linkage, and all external
2105 references, go in the external scope, no matter what scope is
2106 current. However, the binding in that scope is ignored for
2107 purposes of normal name lookup. A separate binding structure is
2108 created in the requested scope; this governs the normal
2109 visibility of the symbol.
2111 The binding in the externals scope is used exclusively for
2112 detecting duplicate declarations of the same object, no matter
2113 what scope they are in; this is what we do here. (C99 6.2.7p2:
2114 All declarations that refer to the same object or function shall
2115 have compatible type; otherwise, the behavior is undefined.) */
2116 if (DECL_EXTERNAL (x) || scope == file_scope)
2118 tree type = TREE_TYPE (x);
2119 tree vistype = 0;
2120 tree visdecl = 0;
2121 bool type_saved = false;
2122 if (b && !B_IN_EXTERNAL_SCOPE (b)
2123 && (TREE_CODE (b->decl) == FUNCTION_DECL
2124 || TREE_CODE (b->decl) == VAR_DECL)
2125 && DECL_FILE_SCOPE_P (b->decl))
2127 visdecl = b->decl;
2128 vistype = TREE_TYPE (visdecl);
2130 if (scope != file_scope
2131 && !DECL_IN_SYSTEM_HEADER (x))
2132 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2134 while (b && !B_IN_EXTERNAL_SCOPE (b))
2136 /* If this decl might be modified, save its type. This is
2137 done here rather than when the decl is first bound
2138 because the type may change after first binding, through
2139 being completed or through attributes being added. If we
2140 encounter multiple such decls, only the first should have
2141 its type saved; the others will already have had their
2142 proper types saved and the types will not have changed as
2143 their scopes will not have been re-entered. */
2144 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2146 b->type = TREE_TYPE (b->decl);
2147 type_saved = true;
2149 if (B_IN_FILE_SCOPE (b)
2150 && TREE_CODE (b->decl) == VAR_DECL
2151 && TREE_STATIC (b->decl)
2152 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2153 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2154 && TREE_CODE (type) == ARRAY_TYPE
2155 && TYPE_DOMAIN (type)
2156 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2157 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2159 /* Array type completed in inner scope, which should be
2160 diagnosed if the completion does not have size 1 and
2161 it does not get completed in the file scope. */
2162 b->inner_comp = true;
2164 b = b->shadowed;
2167 /* If a matching external declaration has been found, set its
2168 type to the composite of all the types of that declaration.
2169 After the consistency checks, it will be reset to the
2170 composite of the visible types only. */
2171 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2172 && b->type)
2173 TREE_TYPE (b->decl) = b->type;
2175 /* The point of the same_translation_unit_p check here is,
2176 we want to detect a duplicate decl for a construct like
2177 foo() { extern bar(); } ... static bar(); but not if
2178 they are in different translation units. In any case,
2179 the static does not go in the externals scope. */
2180 if (b
2181 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2182 && duplicate_decls (x, b->decl))
2184 tree thistype;
2185 if (vistype)
2187 if (comptypes (vistype, type))
2188 thistype = composite_type (vistype, type);
2189 else
2190 thistype = TREE_TYPE (b->decl);
2192 else
2193 thistype = type;
2194 b->type = TREE_TYPE (b->decl);
2195 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2196 thistype
2197 = build_type_attribute_variant (thistype,
2198 TYPE_ATTRIBUTES (b->type));
2199 TREE_TYPE (b->decl) = thistype;
2200 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2201 locus);
2202 return b->decl;
2204 else if (TREE_PUBLIC (x))
2206 if (visdecl && !b && duplicate_decls (x, visdecl))
2208 /* An external declaration at block scope referring to a
2209 visible entity with internal linkage. The composite
2210 type will already be correct for this scope, so we
2211 just need to fall through to make the declaration in
2212 this scope. */
2213 nested = true;
2214 x = visdecl;
2216 else
2218 bind (name, x, external_scope, /*invisible=*/true,
2219 /*nested=*/false, locus);
2220 nested = true;
2225 if (TREE_CODE (x) != PARM_DECL)
2226 warn_if_shadowing (x);
2228 skip_external_and_shadow_checks:
2229 if (TREE_CODE (x) == TYPE_DECL)
2230 set_underlying_type (x);
2232 bind (name, x, scope, /*invisible=*/false, nested, locus);
2234 /* If x's type is incomplete because it's based on a
2235 structure or union which has not yet been fully declared,
2236 attach it to that structure or union type, so we can go
2237 back and complete the variable declaration later, if the
2238 structure or union gets fully declared.
2240 If the input is erroneous, we can have error_mark in the type
2241 slot (e.g. "f(void a, ...)") - that doesn't count as an
2242 incomplete type. */
2243 if (TREE_TYPE (x) != error_mark_node
2244 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2246 tree element = TREE_TYPE (x);
2248 while (TREE_CODE (element) == ARRAY_TYPE)
2249 element = TREE_TYPE (element);
2250 element = TYPE_MAIN_VARIANT (element);
2252 if ((TREE_CODE (element) == RECORD_TYPE
2253 || TREE_CODE (element) == UNION_TYPE)
2254 && (TREE_CODE (x) != TYPE_DECL
2255 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2256 && !COMPLETE_TYPE_P (element))
2257 C_TYPE_INCOMPLETE_VARS (element)
2258 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2260 return x;
2263 /* Record X as belonging to file scope.
2264 This is used only internally by the Objective-C front end,
2265 and is limited to its needs. duplicate_decls is not called;
2266 if there is any preexisting decl for this identifier, it is an ICE. */
2268 tree
2269 pushdecl_top_level (tree x)
2271 tree name;
2272 bool nested = false;
2273 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2275 name = DECL_NAME (x);
2277 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2279 if (TREE_PUBLIC (x))
2281 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2282 UNKNOWN_LOCATION);
2283 nested = true;
2285 if (file_scope)
2286 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2288 return x;
2291 static void
2292 implicit_decl_warning (tree id, tree olddecl)
2294 if (warn_implicit_function_declaration)
2296 bool warned;
2298 if (flag_isoc99)
2299 warned = pedwarn (input_location, OPT_Wimplicit_function_declaration,
2300 "implicit declaration of function %qE", id);
2301 else
2302 warned = warning (OPT_Wimplicit_function_declaration,
2303 G_("implicit declaration of function %qE"), id);
2304 if (olddecl && warned)
2305 locate_old_decl (olddecl);
2309 /* Generate an implicit declaration for identifier FUNCTIONID as a
2310 function of type int (). */
2312 tree
2313 implicitly_declare (tree functionid)
2315 struct c_binding *b;
2316 tree decl = 0;
2317 tree asmspec_tree;
2319 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2321 if (B_IN_SCOPE (b, external_scope))
2323 decl = b->decl;
2324 break;
2328 if (decl)
2330 if (decl == error_mark_node)
2331 return decl;
2333 /* FIXME: Objective-C has weird not-really-builtin functions
2334 which are supposed to be visible automatically. They wind up
2335 in the external scope because they're pushed before the file
2336 scope gets created. Catch this here and rebind them into the
2337 file scope. */
2338 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2340 bind (functionid, decl, file_scope,
2341 /*invisible=*/false, /*nested=*/true,
2342 DECL_SOURCE_LOCATION (decl));
2343 return decl;
2345 else
2347 tree newtype = default_function_type;
2348 if (b->type)
2349 TREE_TYPE (decl) = b->type;
2350 /* Implicit declaration of a function already declared
2351 (somehow) in a different scope, or as a built-in.
2352 If this is the first time this has happened, warn;
2353 then recycle the old declaration but with the new type. */
2354 if (!C_DECL_IMPLICIT (decl))
2356 implicit_decl_warning (functionid, decl);
2357 C_DECL_IMPLICIT (decl) = 1;
2359 if (DECL_BUILT_IN (decl))
2361 newtype = build_type_attribute_variant (newtype,
2362 TYPE_ATTRIBUTES
2363 (TREE_TYPE (decl)));
2364 if (!comptypes (newtype, TREE_TYPE (decl)))
2366 warning (0, "incompatible implicit declaration of built-in"
2367 " function %qD", decl);
2368 newtype = TREE_TYPE (decl);
2371 else
2373 if (!comptypes (newtype, TREE_TYPE (decl)))
2375 error ("incompatible implicit declaration of function %qD",
2376 decl);
2377 locate_old_decl (decl);
2380 b->type = TREE_TYPE (decl);
2381 TREE_TYPE (decl) = newtype;
2382 bind (functionid, decl, current_scope,
2383 /*invisible=*/false, /*nested=*/true,
2384 DECL_SOURCE_LOCATION (decl));
2385 return decl;
2389 /* Not seen before. */
2390 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2391 DECL_EXTERNAL (decl) = 1;
2392 TREE_PUBLIC (decl) = 1;
2393 C_DECL_IMPLICIT (decl) = 1;
2394 implicit_decl_warning (functionid, 0);
2395 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2396 if (asmspec_tree)
2397 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2399 /* C89 says implicit declarations are in the innermost block.
2400 So we record the decl in the standard fashion. */
2401 decl = pushdecl (decl);
2403 /* No need to call objc_check_decl here - it's a function type. */
2404 rest_of_decl_compilation (decl, 0, 0);
2406 /* Write a record describing this implicit function declaration
2407 to the prototypes file (if requested). */
2408 gen_aux_info_record (decl, 0, 1, 0);
2410 /* Possibly apply some default attributes to this implicit declaration. */
2411 decl_attributes (&decl, NULL_TREE, 0);
2413 return decl;
2416 /* Issue an error message for a reference to an undeclared variable
2417 ID, including a reference to a builtin outside of function-call
2418 context. Establish a binding of the identifier to error_mark_node
2419 in an appropriate scope, which will suppress further errors for the
2420 same identifier. The error message should be given location LOC. */
2421 void
2422 undeclared_variable (tree id, location_t loc)
2424 static bool already = false;
2425 struct c_scope *scope;
2427 if (current_function_decl == 0)
2429 error ("%H%qE undeclared here (not in a function)", &loc, id);
2430 scope = current_scope;
2432 else
2434 error ("%H%qE undeclared (first use in this function)", &loc, id);
2436 if (!already)
2438 error ("%H(Each undeclared identifier is reported only once", &loc);
2439 error ("%Hfor each function it appears in.)", &loc);
2440 already = true;
2443 /* If we are parsing old-style parameter decls, current_function_decl
2444 will be nonnull but current_function_scope will be null. */
2445 scope = current_function_scope ? current_function_scope : current_scope;
2447 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
2448 UNKNOWN_LOCATION);
2451 /* Subroutine of lookup_label, declare_label, define_label: construct a
2452 LABEL_DECL with all the proper frills. */
2454 static tree
2455 make_label (tree name, location_t location)
2457 tree label = build_decl (LABEL_DECL, name, void_type_node);
2459 DECL_CONTEXT (label) = current_function_decl;
2460 DECL_MODE (label) = VOIDmode;
2461 DECL_SOURCE_LOCATION (label) = location;
2463 return label;
2466 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2467 Create one if none exists so far for the current function.
2468 This is called when a label is used in a goto expression or
2469 has its address taken. */
2471 tree
2472 lookup_label (tree name)
2474 tree label;
2476 if (current_function_decl == 0)
2478 error ("label %qE referenced outside of any function", name);
2479 return 0;
2482 /* Use a label already defined or ref'd with this name, but not if
2483 it is inherited from a containing function and wasn't declared
2484 using __label__. */
2485 label = I_LABEL_DECL (name);
2486 if (label && (DECL_CONTEXT (label) == current_function_decl
2487 || C_DECLARED_LABEL_FLAG (label)))
2489 /* If the label has only been declared, update its apparent
2490 location to point here, for better diagnostics if it
2491 turns out not to have been defined. */
2492 if (!TREE_USED (label))
2493 DECL_SOURCE_LOCATION (label) = input_location;
2494 return label;
2497 /* No label binding for that identifier; make one. */
2498 label = make_label (name, input_location);
2500 /* Ordinary labels go in the current function scope. */
2501 bind (name, label, current_function_scope,
2502 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2503 return label;
2506 /* Make a label named NAME in the current function, shadowing silently
2507 any that may be inherited from containing functions or containing
2508 scopes. This is called for __label__ declarations. */
2510 tree
2511 declare_label (tree name)
2513 struct c_binding *b = I_LABEL_BINDING (name);
2514 tree label;
2516 /* Check to make sure that the label hasn't already been declared
2517 at this scope */
2518 if (b && B_IN_CURRENT_SCOPE (b))
2520 error ("duplicate label declaration %qE", name);
2521 locate_old_decl (b->decl);
2523 /* Just use the previous declaration. */
2524 return b->decl;
2527 label = make_label (name, input_location);
2528 C_DECLARED_LABEL_FLAG (label) = 1;
2530 /* Declared labels go in the current scope. */
2531 bind (name, label, current_scope,
2532 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2533 return label;
2536 /* Define a label, specifying the location in the source file.
2537 Return the LABEL_DECL node for the label, if the definition is valid.
2538 Otherwise return 0. */
2540 tree
2541 define_label (location_t location, tree name)
2543 /* Find any preexisting label with this name. It is an error
2544 if that label has already been defined in this function, or
2545 if there is a containing function with a declared label with
2546 the same name. */
2547 tree label = I_LABEL_DECL (name);
2548 struct c_label_list *nlist_se, *nlist_vm;
2550 if (label
2551 && ((DECL_CONTEXT (label) == current_function_decl
2552 && DECL_INITIAL (label) != 0)
2553 || (DECL_CONTEXT (label) != current_function_decl
2554 && C_DECLARED_LABEL_FLAG (label))))
2556 error ("%Hduplicate label %qD", &location, label);
2557 locate_old_decl (label);
2558 return 0;
2560 else if (label && DECL_CONTEXT (label) == current_function_decl)
2562 /* The label has been used or declared already in this function,
2563 but not defined. Update its location to point to this
2564 definition. */
2565 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2566 error ("%Jjump into statement expression", label);
2567 if (C_DECL_UNDEFINABLE_VM (label))
2568 error ("%Jjump into scope of identifier with variably modified type",
2569 label);
2570 DECL_SOURCE_LOCATION (label) = location;
2572 else
2574 /* No label binding for that identifier; make one. */
2575 label = make_label (name, location);
2577 /* Ordinary labels go in the current function scope. */
2578 bind (name, label, current_function_scope,
2579 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2582 if (!in_system_header && lookup_name (name))
2583 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2584 "for labels, identifier %qE conflicts", &location, name);
2586 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2587 nlist_se->next = label_context_stack_se->labels_def;
2588 nlist_se->label = label;
2589 label_context_stack_se->labels_def = nlist_se;
2591 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2592 nlist_vm->next = label_context_stack_vm->labels_def;
2593 nlist_vm->label = label;
2594 label_context_stack_vm->labels_def = nlist_vm;
2596 /* Mark label as having been defined. */
2597 DECL_INITIAL (label) = error_mark_node;
2598 return label;
2601 /* Given NAME, an IDENTIFIER_NODE,
2602 return the structure (or union or enum) definition for that name.
2603 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2604 CODE says which kind of type the caller wants;
2605 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2606 If the wrong kind of type is found, an error is reported. */
2608 static tree
2609 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2611 struct c_binding *b = I_TAG_BINDING (name);
2612 int thislevel = 0;
2614 if (!b || !b->decl)
2615 return 0;
2617 /* We only care about whether it's in this level if
2618 thislevel_only was set or it might be a type clash. */
2619 if (thislevel_only || TREE_CODE (b->decl) != code)
2621 /* For our purposes, a tag in the external scope is the same as
2622 a tag in the file scope. (Primarily relevant to Objective-C
2623 and its builtin structure tags, which get pushed before the
2624 file scope is created.) */
2625 if (B_IN_CURRENT_SCOPE (b)
2626 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2627 thislevel = 1;
2630 if (thislevel_only && !thislevel)
2631 return 0;
2633 if (TREE_CODE (b->decl) != code)
2635 /* Definition isn't the kind we were looking for. */
2636 pending_invalid_xref = name;
2637 pending_invalid_xref_location = input_location;
2639 /* If in the same binding level as a declaration as a tag
2640 of a different type, this must not be allowed to
2641 shadow that tag, so give the error immediately.
2642 (For example, "struct foo; union foo;" is invalid.) */
2643 if (thislevel)
2644 pending_xref_error ();
2646 return b->decl;
2649 /* Print an error message now
2650 for a recent invalid struct, union or enum cross reference.
2651 We don't print them immediately because they are not invalid
2652 when used in the `struct foo;' construct for shadowing. */
2654 void
2655 pending_xref_error (void)
2657 if (pending_invalid_xref != 0)
2658 error ("%H%qE defined as wrong kind of tag",
2659 &pending_invalid_xref_location, pending_invalid_xref);
2660 pending_invalid_xref = 0;
2664 /* Look up NAME in the current scope and its superiors
2665 in the namespace of variables, functions and typedefs.
2666 Return a ..._DECL node of some kind representing its definition,
2667 or return 0 if it is undefined. */
2669 tree
2670 lookup_name (tree name)
2672 struct c_binding *b = I_SYMBOL_BINDING (name);
2673 if (b && !b->invisible)
2674 return b->decl;
2675 return 0;
2678 /* Similar to `lookup_name' but look only at the indicated scope. */
2680 static tree
2681 lookup_name_in_scope (tree name, struct c_scope *scope)
2683 struct c_binding *b;
2685 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2686 if (B_IN_SCOPE (b, scope))
2687 return b->decl;
2688 return 0;
2691 /* Create the predefined scalar types of C,
2692 and some nodes representing standard constants (0, 1, (void *) 0).
2693 Initialize the global scope.
2694 Make definitions for built-in primitive functions. */
2696 void
2697 c_init_decl_processing (void)
2699 location_t save_loc = input_location;
2701 /* Initialize reserved words for parser. */
2702 c_parse_init ();
2704 current_function_decl = 0;
2706 gcc_obstack_init (&parser_obstack);
2708 /* Make the externals scope. */
2709 push_scope ();
2710 external_scope = current_scope;
2712 /* Declarations from c_common_nodes_and_builtins must not be associated
2713 with this input file, lest we get differences between using and not
2714 using preprocessed headers. */
2715 input_location = BUILTINS_LOCATION;
2717 build_common_tree_nodes (flag_signed_char, false);
2719 c_common_nodes_and_builtins ();
2721 /* In C, comparisons and TRUTH_* expressions have type int. */
2722 truthvalue_type_node = integer_type_node;
2723 truthvalue_true_node = integer_one_node;
2724 truthvalue_false_node = integer_zero_node;
2726 /* Even in C99, which has a real boolean type. */
2727 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2728 boolean_type_node));
2730 input_location = save_loc;
2732 pedantic_lvalues = true;
2734 make_fname_decl = c_make_fname_decl;
2735 start_fname_decls ();
2738 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2739 decl, NAME is the initialization string and TYPE_DEP indicates whether
2740 NAME depended on the type of the function. As we don't yet implement
2741 delayed emission of static data, we mark the decl as emitted
2742 so it is not placed in the output. Anything using it must therefore pull
2743 out the STRING_CST initializer directly. FIXME. */
2745 static tree
2746 c_make_fname_decl (tree id, int type_dep)
2748 const char *name = fname_as_string (type_dep);
2749 tree decl, type, init;
2750 size_t length = strlen (name);
2752 type = build_array_type (char_type_node,
2753 build_index_type (size_int (length)));
2754 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2756 decl = build_decl (VAR_DECL, id, type);
2758 TREE_STATIC (decl) = 1;
2759 TREE_READONLY (decl) = 1;
2760 DECL_ARTIFICIAL (decl) = 1;
2762 init = build_string (length + 1, name);
2763 free (CONST_CAST (char *, name));
2764 TREE_TYPE (init) = type;
2765 DECL_INITIAL (decl) = init;
2767 TREE_USED (decl) = 1;
2769 if (current_function_decl
2770 /* For invalid programs like this:
2772 void foo()
2773 const char* p = __FUNCTION__;
2775 the __FUNCTION__ is believed to appear in K&R style function
2776 parameter declarator. In that case we still don't have
2777 function_scope. */
2778 && (!errorcount || current_function_scope))
2780 DECL_CONTEXT (decl) = current_function_decl;
2781 bind (id, decl, current_function_scope,
2782 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2785 finish_decl (decl, init, NULL_TREE);
2787 return decl;
2790 tree
2791 c_builtin_function (tree decl)
2793 tree type = TREE_TYPE (decl);
2794 tree id = DECL_NAME (decl);
2796 const char *name = IDENTIFIER_POINTER (id);
2797 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2799 /* Should never be called on a symbol with a preexisting meaning. */
2800 gcc_assert (!I_SYMBOL_BINDING (id));
2802 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
2803 UNKNOWN_LOCATION);
2805 /* Builtins in the implementation namespace are made visible without
2806 needing to be explicitly declared. See push_file_scope. */
2807 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2809 TREE_CHAIN (decl) = visible_builtins;
2810 visible_builtins = decl;
2813 return decl;
2816 tree
2817 c_builtin_function_ext_scope (tree decl)
2819 tree type = TREE_TYPE (decl);
2820 tree id = DECL_NAME (decl);
2822 const char *name = IDENTIFIER_POINTER (id);
2823 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2825 /* Should never be called on a symbol with a preexisting meaning. */
2826 gcc_assert (!I_SYMBOL_BINDING (id));
2828 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
2829 UNKNOWN_LOCATION);
2831 /* Builtins in the implementation namespace are made visible without
2832 needing to be explicitly declared. See push_file_scope. */
2833 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2835 TREE_CHAIN (decl) = visible_builtins;
2836 visible_builtins = decl;
2839 return decl;
2842 /* Called when a declaration is seen that contains no names to declare.
2843 If its type is a reference to a structure, union or enum inherited
2844 from a containing scope, shadow that tag name for the current scope
2845 with a forward reference.
2846 If its type defines a new named structure or union
2847 or defines an enum, it is valid but we need not do anything here.
2848 Otherwise, it is an error. */
2850 void
2851 shadow_tag (const struct c_declspecs *declspecs)
2853 shadow_tag_warned (declspecs, 0);
2856 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2857 but no pedwarn. */
2858 void
2859 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2861 bool found_tag = false;
2863 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2865 tree value = declspecs->type;
2866 enum tree_code code = TREE_CODE (value);
2868 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2869 /* Used to test also that TYPE_SIZE (value) != 0.
2870 That caused warning for `struct foo;' at top level in the file. */
2872 tree name = TYPE_NAME (value);
2873 tree t;
2875 found_tag = true;
2877 if (name == 0)
2879 if (warned != 1 && code != ENUMERAL_TYPE)
2880 /* Empty unnamed enum OK */
2882 pedwarn (input_location, 0,
2883 "unnamed struct/union that defines no instances");
2884 warned = 1;
2887 else if (!declspecs->tag_defined_p
2888 && declspecs->storage_class != csc_none)
2890 if (warned != 1)
2891 pedwarn (input_location, 0,
2892 "empty declaration with storage class specifier "
2893 "does not redeclare tag");
2894 warned = 1;
2895 pending_xref_error ();
2897 else if (!declspecs->tag_defined_p
2898 && (declspecs->const_p
2899 || declspecs->volatile_p
2900 || declspecs->restrict_p))
2902 if (warned != 1)
2903 pedwarn (input_location, 0,
2904 "empty declaration with type qualifier "
2905 "does not redeclare tag");
2906 warned = 1;
2907 pending_xref_error ();
2909 else
2911 pending_invalid_xref = 0;
2912 t = lookup_tag (code, name, 1);
2914 if (t == 0)
2916 t = make_node (code);
2917 pushtag (name, t);
2921 else
2923 if (warned != 1 && !in_system_header)
2925 pedwarn (input_location, 0,
2926 "useless type name in empty declaration");
2927 warned = 1;
2931 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2933 pedwarn (input_location, 0, "useless type name in empty declaration");
2934 warned = 1;
2937 pending_invalid_xref = 0;
2939 if (declspecs->inline_p)
2941 error ("%<inline%> in empty declaration");
2942 warned = 1;
2945 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2947 error ("%<auto%> in file-scope empty declaration");
2948 warned = 1;
2951 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2953 error ("%<register%> in file-scope empty declaration");
2954 warned = 1;
2957 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2959 warning (0, "useless storage class specifier in empty declaration");
2960 warned = 2;
2963 if (!warned && !in_system_header && declspecs->thread_p)
2965 warning (0, "useless %<__thread%> in empty declaration");
2966 warned = 2;
2969 if (!warned && !in_system_header && (declspecs->const_p
2970 || declspecs->volatile_p
2971 || declspecs->restrict_p))
2973 warning (0, "useless type qualifier in empty declaration");
2974 warned = 2;
2977 if (warned != 1)
2979 if (!found_tag)
2980 pedwarn (input_location, 0, "empty declaration");
2985 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2986 bits. SPECS represents declaration specifiers that the grammar
2987 only permits to contain type qualifiers and attributes. */
2990 quals_from_declspecs (const struct c_declspecs *specs)
2992 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
2993 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
2994 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
2995 gcc_assert (!specs->type
2996 && !specs->decl_attr
2997 && specs->typespec_word == cts_none
2998 && specs->storage_class == csc_none
2999 && !specs->typedef_p
3000 && !specs->explicit_signed_p
3001 && !specs->deprecated_p
3002 && !specs->long_p
3003 && !specs->long_long_p
3004 && !specs->short_p
3005 && !specs->signed_p
3006 && !specs->unsigned_p
3007 && !specs->complex_p
3008 && !specs->inline_p
3009 && !specs->thread_p);
3010 return quals;
3013 /* Construct an array declarator. EXPR is the expression inside [],
3014 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3015 applied to the pointer to which a parameter array is converted).
3016 STATIC_P is true if "static" is inside the [], false otherwise.
3017 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3018 length which is nevertheless a complete type, false otherwise. The
3019 field for the contained declarator is left to be filled in by
3020 set_array_declarator_inner. */
3022 struct c_declarator *
3023 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3024 bool vla_unspec_p)
3026 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3027 struct c_declarator);
3028 declarator->kind = cdk_array;
3029 declarator->declarator = 0;
3030 declarator->u.array.dimen = expr;
3031 if (quals)
3033 declarator->u.array.attrs = quals->attrs;
3034 declarator->u.array.quals = quals_from_declspecs (quals);
3036 else
3038 declarator->u.array.attrs = NULL_TREE;
3039 declarator->u.array.quals = 0;
3041 declarator->u.array.static_p = static_p;
3042 declarator->u.array.vla_unspec_p = vla_unspec_p;
3043 if (!flag_isoc99)
3045 if (static_p || quals != NULL)
3046 pedwarn (input_location, OPT_pedantic,
3047 "ISO C90 does not support %<static%> or type "
3048 "qualifiers in parameter array declarators");
3049 if (vla_unspec_p)
3050 pedwarn (input_location, OPT_pedantic,
3051 "ISO C90 does not support %<[*]%> array declarators");
3053 if (vla_unspec_p)
3055 if (!current_scope->parm_flag)
3057 /* C99 6.7.5.2p4 */
3058 error ("%<[*]%> not allowed in other than function prototype scope");
3059 declarator->u.array.vla_unspec_p = false;
3060 return NULL;
3062 current_scope->had_vla_unspec = true;
3064 return declarator;
3067 /* Set the contained declarator of an array declarator. DECL is the
3068 declarator, as constructed by build_array_declarator; INNER is what
3069 appears on the left of the []. */
3071 struct c_declarator *
3072 set_array_declarator_inner (struct c_declarator *decl,
3073 struct c_declarator *inner)
3075 decl->declarator = inner;
3076 return decl;
3079 /* INIT is a constructor that forms DECL's initializer. If the final
3080 element initializes a flexible array field, add the size of that
3081 initializer to DECL's size. */
3083 static void
3084 add_flexible_array_elts_to_size (tree decl, tree init)
3086 tree elt, type;
3088 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3089 return;
3091 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3092 type = TREE_TYPE (elt);
3093 if (TREE_CODE (type) == ARRAY_TYPE
3094 && TYPE_SIZE (type) == NULL_TREE
3095 && TYPE_DOMAIN (type) != NULL_TREE
3096 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3098 complete_array_type (&type, elt, false);
3099 DECL_SIZE (decl)
3100 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3101 DECL_SIZE_UNIT (decl)
3102 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3106 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3107 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3108 before the type name, and set *EXPR_CONST_OPERANDS, if
3109 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3110 appear in a constant expression. */
3112 tree
3113 groktypename (struct c_type_name *type_name, tree *expr,
3114 bool *expr_const_operands)
3116 tree type;
3117 tree attrs = type_name->specs->attrs;
3119 type_name->specs->attrs = NULL_TREE;
3121 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3122 false, NULL, &attrs, expr, expr_const_operands,
3123 DEPRECATED_NORMAL);
3125 /* Apply attributes. */
3126 decl_attributes (&type, attrs, 0);
3128 return type;
3131 /* Decode a declarator in an ordinary declaration or data definition.
3132 This is called as soon as the type information and variable name
3133 have been parsed, before parsing the initializer if any.
3134 Here we create the ..._DECL node, fill in its type,
3135 and put it on the list of decls for the current context.
3136 The ..._DECL node is returned as the value.
3138 Exception: for arrays where the length is not specified,
3139 the type is left null, to be filled in by `finish_decl'.
3141 Function definitions do not come here; they go to start_function
3142 instead. However, external and forward declarations of functions
3143 do go through here. Structure field declarations are done by
3144 grokfield and not through here. */
3146 tree
3147 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3148 bool initialized, tree attributes)
3150 tree decl;
3151 tree tem;
3152 tree expr = NULL_TREE;
3153 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3155 /* An object declared as __attribute__((deprecated)) suppresses
3156 warnings of uses of other deprecated items. */
3157 if (lookup_attribute ("deprecated", attributes))
3158 deprecated_state = DEPRECATED_SUPPRESS;
3160 decl = grokdeclarator (declarator, declspecs,
3161 NORMAL, initialized, NULL, &attributes, &expr, NULL,
3162 deprecated_state);
3163 if (!decl)
3164 return 0;
3166 if (expr)
3167 add_stmt (expr);
3169 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
3170 warning (OPT_Wmain, "%q+D is usually a function", decl);
3172 if (initialized)
3173 /* Is it valid for this decl to have an initializer at all?
3174 If not, set INITIALIZED to zero, which will indirectly
3175 tell 'finish_decl' to ignore the initializer once it is parsed. */
3176 switch (TREE_CODE (decl))
3178 case TYPE_DECL:
3179 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3180 initialized = 0;
3181 break;
3183 case FUNCTION_DECL:
3184 error ("function %qD is initialized like a variable", decl);
3185 initialized = 0;
3186 break;
3188 case PARM_DECL:
3189 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3190 error ("parameter %qD is initialized", decl);
3191 initialized = 0;
3192 break;
3194 default:
3195 /* Don't allow initializations for incomplete types except for
3196 arrays which might be completed by the initialization. */
3198 /* This can happen if the array size is an undefined macro.
3199 We already gave a warning, so we don't need another one. */
3200 if (TREE_TYPE (decl) == error_mark_node)
3201 initialized = 0;
3202 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3204 /* A complete type is ok if size is fixed. */
3206 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3207 || C_DECL_VARIABLE_SIZE (decl))
3209 error ("variable-sized object may not be initialized");
3210 initialized = 0;
3213 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3215 error ("variable %qD has initializer but incomplete type", decl);
3216 initialized = 0;
3218 else if (C_DECL_VARIABLE_SIZE (decl))
3220 /* Although C99 is unclear about whether incomplete arrays
3221 of VLAs themselves count as VLAs, it does not make
3222 sense to permit them to be initialized given that
3223 ordinary VLAs may not be initialized. */
3224 error ("variable-sized object may not be initialized");
3225 initialized = 0;
3229 if (initialized)
3231 if (current_scope == file_scope)
3232 TREE_STATIC (decl) = 1;
3234 /* Tell 'pushdecl' this is an initialized decl
3235 even though we don't yet have the initializer expression.
3236 Also tell 'finish_decl' it may store the real initializer. */
3237 DECL_INITIAL (decl) = error_mark_node;
3240 /* If this is a function declaration, write a record describing it to the
3241 prototypes file (if requested). */
3243 if (TREE_CODE (decl) == FUNCTION_DECL)
3244 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3246 /* ANSI specifies that a tentative definition which is not merged with
3247 a non-tentative definition behaves exactly like a definition with an
3248 initializer equal to zero. (Section 3.7.2)
3250 -fno-common gives strict ANSI behavior, though this tends to break
3251 a large body of code that grew up without this rule.
3253 Thread-local variables are never common, since there's no entrenched
3254 body of code to break, and it allows more efficient variable references
3255 in the presence of dynamic linking. */
3257 if (TREE_CODE (decl) == VAR_DECL
3258 && !initialized
3259 && TREE_PUBLIC (decl)
3260 && !DECL_THREAD_LOCAL_P (decl)
3261 && !flag_no_common)
3262 DECL_COMMON (decl) = 1;
3264 /* Set attributes here so if duplicate decl, will have proper attributes. */
3265 decl_attributes (&decl, attributes, 0);
3267 /* Handle gnu_inline attribute. */
3268 if (declspecs->inline_p
3269 && !flag_gnu89_inline
3270 && TREE_CODE (decl) == FUNCTION_DECL
3271 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
3272 || current_function_decl))
3274 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3276 else if (declspecs->storage_class != csc_static)
3277 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3280 if (TREE_CODE (decl) == FUNCTION_DECL
3281 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3283 struct c_declarator *ce = declarator;
3285 if (ce->kind == cdk_pointer)
3286 ce = declarator->declarator;
3287 if (ce->kind == cdk_function)
3289 tree args = ce->u.arg_info->parms;
3290 for (; args; args = TREE_CHAIN (args))
3292 tree type = TREE_TYPE (args);
3293 if (type && INTEGRAL_TYPE_P (type)
3294 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3295 DECL_ARG_TYPE (args) = integer_type_node;
3300 if (TREE_CODE (decl) == FUNCTION_DECL
3301 && DECL_DECLARED_INLINE_P (decl)
3302 && DECL_UNINLINABLE (decl)
3303 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3304 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3305 decl);
3307 /* C99 6.7.4p3: An inline definition of a function with external
3308 linkage shall not contain a definition of a modifiable object
3309 with static storage duration... */
3310 if (TREE_CODE (decl) == VAR_DECL
3311 && current_scope != file_scope
3312 && TREE_STATIC (decl)
3313 && !TREE_READONLY (decl)
3314 && DECL_DECLARED_INLINE_P (current_function_decl)
3315 && DECL_EXTERNAL (current_function_decl))
3316 pedwarn (input_location, 0,
3317 "%q+D is static but declared in inline function %qD "
3318 "which is not static", decl, current_function_decl);
3320 /* Add this decl to the current scope.
3321 TEM may equal DECL or it may be a previous decl of the same name. */
3322 tem = pushdecl (decl);
3324 if (initialized && DECL_EXTERNAL (tem))
3326 DECL_EXTERNAL (tem) = 0;
3327 TREE_STATIC (tem) = 1;
3330 return tem;
3333 /* Initialize EH if not initialized yet and exceptions are enabled. */
3335 void
3336 c_maybe_initialize_eh (void)
3338 if (!flag_exceptions || c_eh_initialized_p)
3339 return;
3341 c_eh_initialized_p = true;
3342 eh_personality_libfunc
3343 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3344 ? "__gcc_personality_sj0"
3345 : "__gcc_personality_v0");
3346 default_init_unwind_resume_libfunc ();
3347 using_eh_for_cleanups ();
3350 /* Finish processing of a declaration;
3351 install its initial value.
3352 If the length of an array type is not known before,
3353 it must be determined now, from the initial value, or it is an error. */
3355 void
3356 finish_decl (tree decl, tree init, tree asmspec_tree)
3358 tree type;
3359 int was_incomplete = (DECL_SIZE (decl) == 0);
3360 const char *asmspec = 0;
3362 /* If a name was specified, get the string. */
3363 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3364 && DECL_FILE_SCOPE_P (decl))
3365 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3366 if (asmspec_tree)
3367 asmspec = TREE_STRING_POINTER (asmspec_tree);
3369 /* If `start_decl' didn't like having an initialization, ignore it now. */
3370 if (init != 0 && DECL_INITIAL (decl) == 0)
3371 init = 0;
3373 /* Don't crash if parm is initialized. */
3374 if (TREE_CODE (decl) == PARM_DECL)
3375 init = 0;
3377 if (init)
3378 store_init_value (decl, init);
3380 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3381 || TREE_CODE (decl) == FUNCTION_DECL
3382 || TREE_CODE (decl) == FIELD_DECL))
3383 objc_check_decl (decl);
3385 type = TREE_TYPE (decl);
3387 /* Deduce size of array from initialization, if not already known. */
3388 if (TREE_CODE (type) == ARRAY_TYPE
3389 && TYPE_DOMAIN (type) == 0
3390 && TREE_CODE (decl) != TYPE_DECL)
3392 bool do_default
3393 = (TREE_STATIC (decl)
3394 /* Even if pedantic, an external linkage array
3395 may have incomplete type at first. */
3396 ? pedantic && !TREE_PUBLIC (decl)
3397 : !DECL_EXTERNAL (decl));
3398 int failure
3399 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3400 do_default);
3402 /* Get the completed type made by complete_array_type. */
3403 type = TREE_TYPE (decl);
3405 switch (failure)
3407 case 1:
3408 error ("initializer fails to determine size of %q+D", decl);
3409 break;
3411 case 2:
3412 if (do_default)
3413 error ("array size missing in %q+D", decl);
3414 /* If a `static' var's size isn't known,
3415 make it extern as well as static, so it does not get
3416 allocated.
3417 If it is not `static', then do not mark extern;
3418 finish_incomplete_decl will give it a default size
3419 and it will get allocated. */
3420 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3421 DECL_EXTERNAL (decl) = 1;
3422 break;
3424 case 3:
3425 error ("zero or negative size array %q+D", decl);
3426 break;
3428 case 0:
3429 /* For global variables, update the copy of the type that
3430 exists in the binding. */
3431 if (TREE_PUBLIC (decl))
3433 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3434 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3435 b_ext = b_ext->shadowed;
3436 if (b_ext)
3438 if (b_ext->type)
3439 b_ext->type = composite_type (b_ext->type, type);
3440 else
3441 b_ext->type = type;
3444 break;
3446 default:
3447 gcc_unreachable ();
3450 if (DECL_INITIAL (decl))
3451 TREE_TYPE (DECL_INITIAL (decl)) = type;
3453 layout_decl (decl, 0);
3456 if (TREE_CODE (decl) == VAR_DECL)
3458 if (init && TREE_CODE (init) == CONSTRUCTOR)
3459 add_flexible_array_elts_to_size (decl, init);
3461 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3462 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3463 layout_decl (decl, 0);
3465 if (DECL_SIZE (decl) == 0
3466 /* Don't give an error if we already gave one earlier. */
3467 && TREE_TYPE (decl) != error_mark_node
3468 && (TREE_STATIC (decl)
3469 /* A static variable with an incomplete type
3470 is an error if it is initialized.
3471 Also if it is not file scope.
3472 Otherwise, let it through, but if it is not `extern'
3473 then it may cause an error message later. */
3474 ? (DECL_INITIAL (decl) != 0
3475 || !DECL_FILE_SCOPE_P (decl))
3476 /* An automatic variable with an incomplete type
3477 is an error. */
3478 : !DECL_EXTERNAL (decl)))
3480 error ("storage size of %q+D isn%'t known", decl);
3481 TREE_TYPE (decl) = error_mark_node;
3484 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3485 && DECL_SIZE (decl) != 0)
3487 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3488 constant_expression_warning (DECL_SIZE (decl));
3489 else
3491 error ("storage size of %q+D isn%'t constant", decl);
3492 TREE_TYPE (decl) = error_mark_node;
3496 if (TREE_USED (type))
3497 TREE_USED (decl) = 1;
3500 /* If this is a function and an assembler name is specified, reset DECL_RTL
3501 so we can give it its new name. Also, update built_in_decls if it
3502 was a normal built-in. */
3503 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3505 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3506 set_builtin_user_assembler_name (decl, asmspec);
3507 set_user_assembler_name (decl, asmspec);
3510 /* If #pragma weak was used, mark the decl weak now. */
3511 maybe_apply_pragma_weak (decl);
3513 /* Output the assembler code and/or RTL code for variables and functions,
3514 unless the type is an undefined structure or union.
3515 If not, it will get done when the type is completed. */
3517 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3519 /* Determine the ELF visibility. */
3520 if (TREE_PUBLIC (decl))
3521 c_determine_visibility (decl);
3523 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3524 if (c_dialect_objc ())
3525 objc_check_decl (decl);
3527 if (asmspec)
3529 /* If this is not a static variable, issue a warning.
3530 It doesn't make any sense to give an ASMSPEC for an
3531 ordinary, non-register local variable. Historically,
3532 GCC has accepted -- but ignored -- the ASMSPEC in
3533 this case. */
3534 if (!DECL_FILE_SCOPE_P (decl)
3535 && TREE_CODE (decl) == VAR_DECL
3536 && !C_DECL_REGISTER (decl)
3537 && !TREE_STATIC (decl))
3538 warning (0, "ignoring asm-specifier for non-static local "
3539 "variable %q+D", decl);
3540 else
3541 set_user_assembler_name (decl, asmspec);
3544 if (DECL_FILE_SCOPE_P (decl))
3546 if (DECL_INITIAL (decl) == NULL_TREE
3547 || DECL_INITIAL (decl) == error_mark_node)
3548 /* Don't output anything
3549 when a tentative file-scope definition is seen.
3550 But at end of compilation, do output code for them. */
3551 DECL_DEFER_OUTPUT (decl) = 1;
3552 rest_of_decl_compilation (decl, true, 0);
3554 else
3556 /* In conjunction with an ASMSPEC, the `register'
3557 keyword indicates that we should place the variable
3558 in a particular register. */
3559 if (asmspec && C_DECL_REGISTER (decl))
3561 DECL_HARD_REGISTER (decl) = 1;
3562 /* This cannot be done for a structure with volatile
3563 fields, on which DECL_REGISTER will have been
3564 reset. */
3565 if (!DECL_REGISTER (decl))
3566 error ("cannot put object with volatile field into register");
3569 if (TREE_CODE (decl) != FUNCTION_DECL)
3571 /* If we're building a variable sized type, and we might be
3572 reachable other than via the top of the current binding
3573 level, then create a new BIND_EXPR so that we deallocate
3574 the object at the right time. */
3575 /* Note that DECL_SIZE can be null due to errors. */
3576 if (DECL_SIZE (decl)
3577 && !TREE_CONSTANT (DECL_SIZE (decl))
3578 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3580 tree bind;
3581 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3582 TREE_SIDE_EFFECTS (bind) = 1;
3583 add_stmt (bind);
3584 BIND_EXPR_BODY (bind) = push_stmt_list ();
3586 add_stmt (build_stmt (DECL_EXPR, decl));
3591 if (!DECL_FILE_SCOPE_P (decl))
3593 /* Recompute the RTL of a local array now
3594 if it used to be an incomplete type. */
3595 if (was_incomplete
3596 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3598 /* If we used it already as memory, it must stay in memory. */
3599 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3600 /* If it's still incomplete now, no init will save it. */
3601 if (DECL_SIZE (decl) == 0)
3602 DECL_INITIAL (decl) = 0;
3607 if (TREE_CODE (decl) == TYPE_DECL)
3609 if (!DECL_FILE_SCOPE_P (decl)
3610 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3611 add_stmt (build_stmt (DECL_EXPR, decl));
3613 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3616 /* At the end of a declaration, throw away any variable type sizes
3617 of types defined inside that declaration. There is no use
3618 computing them in the following function definition. */
3619 if (current_scope == file_scope)
3620 get_pending_sizes ();
3622 /* Install a cleanup (aka destructor) if one was given. */
3623 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3625 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3626 if (attr)
3628 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3629 tree cleanup_decl = lookup_name (cleanup_id);
3630 tree cleanup;
3632 /* Build "cleanup(&decl)" for the destructor. */
3633 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
3634 cleanup = build_tree_list (NULL_TREE, cleanup);
3635 cleanup = build_function_call (cleanup_decl, cleanup);
3637 /* Don't warn about decl unused; the cleanup uses it. */
3638 TREE_USED (decl) = 1;
3639 TREE_USED (cleanup_decl) = 1;
3641 /* Initialize EH, if we've been told to do so. */
3642 c_maybe_initialize_eh ();
3644 push_cleanup (decl, cleanup, false);
3649 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3651 tree
3652 grokparm (const struct c_parm *parm)
3654 tree attrs = parm->attrs;
3655 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3656 NULL, &attrs, NULL, NULL, DEPRECATED_NORMAL);
3658 decl_attributes (&decl, attrs, 0);
3660 return decl;
3663 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3664 and push that on the current scope. */
3666 void
3667 push_parm_decl (const struct c_parm *parm)
3669 tree attrs = parm->attrs;
3670 tree decl;
3672 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
3673 &attrs, NULL, NULL, DEPRECATED_NORMAL);
3674 decl_attributes (&decl, attrs, 0);
3676 decl = pushdecl (decl);
3678 finish_decl (decl, NULL_TREE, NULL_TREE);
3681 /* Mark all the parameter declarations to date as forward decls.
3682 Also diagnose use of this extension. */
3684 void
3685 mark_forward_parm_decls (void)
3687 struct c_binding *b;
3689 if (pedantic && !current_scope->warned_forward_parm_decls)
3691 pedwarn (input_location, OPT_pedantic,
3692 "ISO C forbids forward parameter declarations");
3693 current_scope->warned_forward_parm_decls = true;
3696 for (b = current_scope->bindings; b; b = b->prev)
3697 if (TREE_CODE (b->decl) == PARM_DECL)
3698 TREE_ASM_WRITTEN (b->decl) = 1;
3701 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3702 literal, which may be an incomplete array type completed by the
3703 initializer; INIT is a CONSTRUCTOR that initializes the compound
3704 literal. NON_CONST is true if the initializers contain something
3705 that cannot occur in a constant expression. */
3707 tree
3708 build_compound_literal (tree type, tree init, bool non_const)
3710 /* We do not use start_decl here because we have a type, not a declarator;
3711 and do not use finish_decl because the decl should be stored inside
3712 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3713 tree decl;
3714 tree complit;
3715 tree stmt;
3717 if (type == error_mark_node)
3718 return error_mark_node;
3720 decl = build_decl (VAR_DECL, NULL_TREE, type);
3721 DECL_EXTERNAL (decl) = 0;
3722 TREE_PUBLIC (decl) = 0;
3723 TREE_STATIC (decl) = (current_scope == file_scope);
3724 DECL_CONTEXT (decl) = current_function_decl;
3725 TREE_USED (decl) = 1;
3726 TREE_TYPE (decl) = type;
3727 TREE_READONLY (decl) = TYPE_READONLY (type);
3728 store_init_value (decl, init);
3730 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3732 int failure = complete_array_type (&TREE_TYPE (decl),
3733 DECL_INITIAL (decl), true);
3734 gcc_assert (!failure);
3736 type = TREE_TYPE (decl);
3737 TREE_TYPE (DECL_INITIAL (decl)) = type;
3740 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3741 return error_mark_node;
3743 stmt = build_stmt (DECL_EXPR, decl);
3744 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3745 TREE_SIDE_EFFECTS (complit) = 1;
3747 layout_decl (decl, 0);
3749 if (TREE_STATIC (decl))
3751 /* This decl needs a name for the assembler output. */
3752 set_compound_literal_name (decl);
3753 DECL_DEFER_OUTPUT (decl) = 1;
3754 DECL_COMDAT (decl) = 1;
3755 DECL_ARTIFICIAL (decl) = 1;
3756 DECL_IGNORED_P (decl) = 1;
3757 pushdecl (decl);
3758 rest_of_decl_compilation (decl, 1, 0);
3761 if (non_const)
3763 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
3764 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
3767 return complit;
3770 /* Determine whether TYPE is a structure with a flexible array member,
3771 or a union containing such a structure (possibly recursively). */
3773 static bool
3774 flexible_array_type_p (tree type)
3776 tree x;
3777 switch (TREE_CODE (type))
3779 case RECORD_TYPE:
3780 x = TYPE_FIELDS (type);
3781 if (x == NULL_TREE)
3782 return false;
3783 while (TREE_CHAIN (x) != NULL_TREE)
3784 x = TREE_CHAIN (x);
3785 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3786 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3787 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3788 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3789 return true;
3790 return false;
3791 case UNION_TYPE:
3792 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3794 if (flexible_array_type_p (TREE_TYPE (x)))
3795 return true;
3797 return false;
3798 default:
3799 return false;
3803 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3804 replacing with appropriate values if they are invalid. */
3805 static void
3806 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3808 tree type_mv;
3809 unsigned int max_width;
3810 unsigned HOST_WIDE_INT w;
3811 const char *name = orig_name ? orig_name: _("<anonymous>");
3813 /* Detect and ignore out of range field width and process valid
3814 field widths. */
3815 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3816 || TREE_CODE (*width) != INTEGER_CST)
3818 error ("bit-field %qs width not an integer constant", name);
3819 *width = integer_one_node;
3821 else
3823 constant_expression_warning (*width);
3824 if (tree_int_cst_sgn (*width) < 0)
3826 error ("negative width in bit-field %qs", name);
3827 *width = integer_one_node;
3829 else if (integer_zerop (*width) && orig_name)
3831 error ("zero width for bit-field %qs", name);
3832 *width = integer_one_node;
3836 /* Detect invalid bit-field type. */
3837 if (TREE_CODE (*type) != INTEGER_TYPE
3838 && TREE_CODE (*type) != BOOLEAN_TYPE
3839 && TREE_CODE (*type) != ENUMERAL_TYPE)
3841 error ("bit-field %qs has invalid type", name);
3842 *type = unsigned_type_node;
3845 type_mv = TYPE_MAIN_VARIANT (*type);
3846 if (!in_system_header
3847 && type_mv != integer_type_node
3848 && type_mv != unsigned_type_node
3849 && type_mv != boolean_type_node)
3850 pedwarn (input_location, OPT_pedantic,
3851 "type of bit-field %qs is a GCC extension", name);
3853 max_width = TYPE_PRECISION (*type);
3855 if (0 < compare_tree_int (*width, max_width))
3857 error ("width of %qs exceeds its type", name);
3858 w = max_width;
3859 *width = build_int_cst (NULL_TREE, w);
3861 else
3862 w = tree_low_cst (*width, 1);
3864 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3866 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3867 if (!lt
3868 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3869 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3870 warning (0, "%qs is narrower than values of its type", name);
3876 /* Print warning about variable length array if necessary. */
3878 static void
3879 warn_variable_length_array (const char *name, tree size)
3881 int const_size = TREE_CONSTANT (size);
3883 if (!flag_isoc99 && pedantic && warn_vla != 0)
3885 if (const_size)
3887 if (name)
3888 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array %qs whose size "
3889 "can%'t be evaluated",
3890 name);
3891 else
3892 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array whose size "
3893 "can%'t be evaluated");
3895 else
3897 if (name)
3898 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array %qs",
3899 name);
3900 else
3901 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array");
3904 else if (warn_vla > 0)
3906 if (const_size)
3908 if (name)
3909 warning (OPT_Wvla,
3910 "the size of array %qs can"
3911 "%'t be evaluated", name);
3912 else
3913 warning (OPT_Wvla,
3914 "the size of array can %'t be evaluated");
3916 else
3918 if (name)
3919 warning (OPT_Wvla,
3920 "variable length array %qs is used",
3921 name);
3922 else
3923 warning (OPT_Wvla,
3924 "variable length array is used");
3929 /* Given declspecs and a declarator,
3930 determine the name and type of the object declared
3931 and construct a ..._DECL node for it.
3932 (In one case we can return a ..._TYPE node instead.
3933 For invalid input we sometimes return 0.)
3935 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3937 DECL_CONTEXT says which syntactic context this declaration is in:
3938 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3939 FUNCDEF for a function definition. Like NORMAL but a few different
3940 error messages in each case. Return value may be zero meaning
3941 this definition is too screwy to try to parse.
3942 PARM for a parameter declaration (either within a function prototype
3943 or before a function body). Make a PARM_DECL, or return void_type_node.
3944 TYPENAME if for a typename (in a cast or sizeof).
3945 Don't make a DECL node; just return the ..._TYPE node.
3946 FIELD for a struct or union field; make a FIELD_DECL.
3947 INITIALIZED is true if the decl has an initializer.
3948 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3949 representing the width of the bit-field.
3950 DECL_ATTRS points to the list of attributes that should be added to this
3951 decl. Any nested attributes that belong on the decl itself will be
3952 added to this list.
3953 If EXPR is not NULL, any expressions that need to be evaluated as
3954 part of evaluating variably modified types will be stored in *EXPR.
3955 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
3956 set to indicate whether operands in *EXPR can be used in constant
3957 expressions.
3958 DEPRECATED_STATE is a deprecated_states value indicating whether
3959 deprecation warnings should be suppressed.
3961 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3962 It may also be so in the PARM case, for a prototype where the
3963 argument type is specified but not the name.
3965 This function is where the complicated C meanings of `static'
3966 and `extern' are interpreted. */
3968 static tree
3969 grokdeclarator (const struct c_declarator *declarator,
3970 struct c_declspecs *declspecs,
3971 enum decl_context decl_context, bool initialized, tree *width,
3972 tree *decl_attrs, tree *expr, bool *expr_const_operands,
3973 enum deprecated_states deprecated_state)
3975 tree type = declspecs->type;
3976 bool threadp = declspecs->thread_p;
3977 enum c_storage_class storage_class = declspecs->storage_class;
3978 int constp;
3979 int restrictp;
3980 int volatilep;
3981 int type_quals = TYPE_UNQUALIFIED;
3982 const char *name, *orig_name;
3983 bool funcdef_flag = false;
3984 bool funcdef_syntax = false;
3985 int size_varies = 0;
3986 tree decl_attr = declspecs->decl_attr;
3987 int array_ptr_quals = TYPE_UNQUALIFIED;
3988 tree array_ptr_attrs = NULL_TREE;
3989 int array_parm_static = 0;
3990 bool array_parm_vla_unspec_p = false;
3991 tree returned_attrs = NULL_TREE;
3992 bool bitfield = width != NULL;
3993 tree element_type;
3994 struct c_arg_info *arg_info = 0;
3995 tree expr_dummy;
3996 bool expr_const_operands_dummy;
3998 if (expr == NULL)
3999 expr = &expr_dummy;
4000 if (expr_const_operands == NULL)
4001 expr_const_operands = &expr_const_operands_dummy;
4003 *expr = declspecs->expr;
4004 *expr_const_operands = declspecs->expr_const_operands;
4006 if (decl_context == FUNCDEF)
4007 funcdef_flag = true, decl_context = NORMAL;
4009 /* Look inside a declarator for the name being declared
4010 and get it as a string, for an error message. */
4012 const struct c_declarator *decl = declarator;
4013 name = 0;
4015 while (decl)
4016 switch (decl->kind)
4018 case cdk_function:
4019 case cdk_array:
4020 case cdk_pointer:
4021 funcdef_syntax = (decl->kind == cdk_function);
4022 decl = decl->declarator;
4023 break;
4025 case cdk_attrs:
4026 decl = decl->declarator;
4027 break;
4029 case cdk_id:
4030 if (decl->u.id)
4031 name = IDENTIFIER_POINTER (decl->u.id);
4032 decl = 0;
4033 break;
4035 default:
4036 gcc_unreachable ();
4038 orig_name = name;
4039 if (name == 0)
4040 name = "type name";
4043 /* A function definition's declarator must have the form of
4044 a function declarator. */
4046 if (funcdef_flag && !funcdef_syntax)
4047 return 0;
4049 /* If this looks like a function definition, make it one,
4050 even if it occurs where parms are expected.
4051 Then store_parm_decls will reject it and not use it as a parm. */
4052 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4053 decl_context = PARM;
4055 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4056 warn_deprecated_use (declspecs->type);
4058 if ((decl_context == NORMAL || decl_context == FIELD)
4059 && current_scope == file_scope
4060 && variably_modified_type_p (type, NULL_TREE))
4062 error ("variably modified %qs at file scope", name);
4063 type = integer_type_node;
4066 size_varies = C_TYPE_VARIABLE_SIZE (type);
4068 /* Diagnose defaulting to "int". */
4070 if (declspecs->default_int_p && !in_system_header)
4072 /* Issue a warning if this is an ISO C 99 program or if
4073 -Wreturn-type and this is a function, or if -Wimplicit;
4074 prefer the former warning since it is more explicit. */
4075 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4076 && funcdef_flag)
4077 warn_about_return_type = 1;
4078 else
4079 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int,
4080 "type defaults to %<int%> in declaration of %qs", name);
4083 /* Adjust the type if a bit-field is being declared,
4084 -funsigned-bitfields applied and the type is not explicitly
4085 "signed". */
4086 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4087 && TREE_CODE (type) == INTEGER_TYPE)
4088 type = unsigned_type_for (type);
4090 /* Figure out the type qualifiers for the declaration. There are
4091 two ways a declaration can become qualified. One is something
4092 like `const int i' where the `const' is explicit. Another is
4093 something like `typedef const int CI; CI i' where the type of the
4094 declaration contains the `const'. A third possibility is that
4095 there is a type qualifier on the element type of a typedefed
4096 array type, in which case we should extract that qualifier so
4097 that c_apply_type_quals_to_decl receives the full list of
4098 qualifiers to work with (C90 is not entirely clear about whether
4099 duplicate qualifiers should be diagnosed in this case, but it
4100 seems most appropriate to do so). */
4101 element_type = strip_array_types (type);
4102 constp = declspecs->const_p + TYPE_READONLY (element_type);
4103 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4104 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4105 if (pedantic && !flag_isoc99)
4107 if (constp > 1)
4108 pedwarn (input_location, OPT_pedantic, "duplicate %<const%>");
4109 if (restrictp > 1)
4110 pedwarn (input_location, OPT_pedantic, "duplicate %<restrict%>");
4111 if (volatilep > 1)
4112 pedwarn (input_location, OPT_pedantic, "duplicate %<volatile%>");
4114 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4115 type = TYPE_MAIN_VARIANT (type);
4116 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4117 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4118 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4120 /* Warn about storage classes that are invalid for certain
4121 kinds of declarations (parameters, typenames, etc.). */
4123 if (funcdef_flag
4124 && (threadp
4125 || storage_class == csc_auto
4126 || storage_class == csc_register
4127 || storage_class == csc_typedef))
4129 if (storage_class == csc_auto)
4130 pedwarn (input_location,
4131 (current_scope == file_scope) ? 0 : OPT_pedantic,
4132 "function definition declared %<auto%>");
4133 if (storage_class == csc_register)
4134 error ("function definition declared %<register%>");
4135 if (storage_class == csc_typedef)
4136 error ("function definition declared %<typedef%>");
4137 if (threadp)
4138 error ("function definition declared %<__thread%>");
4139 threadp = false;
4140 if (storage_class == csc_auto
4141 || storage_class == csc_register
4142 || storage_class == csc_typedef)
4143 storage_class = csc_none;
4145 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4147 if (decl_context == PARM && storage_class == csc_register)
4149 else
4151 switch (decl_context)
4153 case FIELD:
4154 error ("storage class specified for structure field %qs",
4155 name);
4156 break;
4157 case PARM:
4158 error ("storage class specified for parameter %qs", name);
4159 break;
4160 default:
4161 error ("storage class specified for typename");
4162 break;
4164 storage_class = csc_none;
4165 threadp = false;
4168 else if (storage_class == csc_extern
4169 && initialized
4170 && !funcdef_flag)
4172 /* 'extern' with initialization is invalid if not at file scope. */
4173 if (current_scope == file_scope)
4175 /* It is fine to have 'extern const' when compiling at C
4176 and C++ intersection. */
4177 if (!(warn_cxx_compat && constp))
4178 warning (0, "%qs initialized and declared %<extern%>", name);
4180 else
4181 error ("%qs has both %<extern%> and initializer", name);
4183 else if (current_scope == file_scope)
4185 if (storage_class == csc_auto)
4186 error ("file-scope declaration of %qs specifies %<auto%>", name);
4187 if (pedantic && storage_class == csc_register)
4188 pedwarn (input_location, OPT_pedantic,
4189 "file-scope declaration of %qs specifies %<register%>", name);
4191 else
4193 if (storage_class == csc_extern && funcdef_flag)
4194 error ("nested function %qs declared %<extern%>", name);
4195 else if (threadp && storage_class == csc_none)
4197 error ("function-scope %qs implicitly auto and declared "
4198 "%<__thread%>",
4199 name);
4200 threadp = false;
4204 /* Now figure out the structure of the declarator proper.
4205 Descend through it, creating more complex types, until we reach
4206 the declared identifier (or NULL_TREE, in an absolute declarator).
4207 At each stage we maintain an unqualified version of the type
4208 together with any qualifiers that should be applied to it with
4209 c_build_qualified_type; this way, array types including
4210 multidimensional array types are first built up in unqualified
4211 form and then the qualified form is created with
4212 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4214 while (declarator && declarator->kind != cdk_id)
4216 if (type == error_mark_node)
4218 declarator = declarator->declarator;
4219 continue;
4222 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4223 a cdk_pointer (for *...),
4224 a cdk_function (for ...(...)),
4225 a cdk_attrs (for nested attributes),
4226 or a cdk_id (for the name being declared
4227 or the place in an absolute declarator
4228 where the name was omitted).
4229 For the last case, we have just exited the loop.
4231 At this point, TYPE is the type of elements of an array,
4232 or for a function to return, or for a pointer to point to.
4233 After this sequence of ifs, TYPE is the type of the
4234 array or function or pointer, and DECLARATOR has had its
4235 outermost layer removed. */
4237 if (array_ptr_quals != TYPE_UNQUALIFIED
4238 || array_ptr_attrs != NULL_TREE
4239 || array_parm_static)
4241 /* Only the innermost declarator (making a parameter be of
4242 array type which is converted to pointer type)
4243 may have static or type qualifiers. */
4244 error ("static or type qualifiers in non-parameter array declarator");
4245 array_ptr_quals = TYPE_UNQUALIFIED;
4246 array_ptr_attrs = NULL_TREE;
4247 array_parm_static = 0;
4250 switch (declarator->kind)
4252 case cdk_attrs:
4254 /* A declarator with embedded attributes. */
4255 tree attrs = declarator->u.attrs;
4256 const struct c_declarator *inner_decl;
4257 int attr_flags = 0;
4258 declarator = declarator->declarator;
4259 inner_decl = declarator;
4260 while (inner_decl->kind == cdk_attrs)
4261 inner_decl = inner_decl->declarator;
4262 if (inner_decl->kind == cdk_id)
4263 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4264 else if (inner_decl->kind == cdk_function)
4265 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4266 else if (inner_decl->kind == cdk_array)
4267 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4268 returned_attrs = decl_attributes (&type,
4269 chainon (returned_attrs, attrs),
4270 attr_flags);
4271 break;
4273 case cdk_array:
4275 tree itype = NULL_TREE;
4276 tree size = declarator->u.array.dimen;
4277 /* The index is a signed object `sizetype' bits wide. */
4278 tree index_type = c_common_signed_type (sizetype);
4280 array_ptr_quals = declarator->u.array.quals;
4281 array_ptr_attrs = declarator->u.array.attrs;
4282 array_parm_static = declarator->u.array.static_p;
4283 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4285 declarator = declarator->declarator;
4287 /* Check for some types that there cannot be arrays of. */
4289 if (VOID_TYPE_P (type))
4291 error ("declaration of %qs as array of voids", name);
4292 type = error_mark_node;
4295 if (TREE_CODE (type) == FUNCTION_TYPE)
4297 error ("declaration of %qs as array of functions", name);
4298 type = error_mark_node;
4301 if (pedantic && !in_system_header && flexible_array_type_p (type))
4302 pedwarn (input_location, OPT_pedantic,
4303 "invalid use of structure with flexible array member");
4305 if (size == error_mark_node)
4306 type = error_mark_node;
4308 if (type == error_mark_node)
4309 continue;
4311 /* If size was specified, set ITYPE to a range-type for
4312 that size. Otherwise, ITYPE remains null. finish_decl
4313 may figure it out from an initial value. */
4315 if (size)
4317 bool size_maybe_const = true;
4318 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
4319 && !TREE_OVERFLOW (size));
4320 bool this_size_varies = false;
4322 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4323 lvalue. */
4324 STRIP_TYPE_NOPS (size);
4326 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4328 error ("size of array %qs has non-integer type", name);
4329 size = integer_one_node;
4332 size = c_fully_fold (size, false, &size_maybe_const);
4334 if (pedantic && size_maybe_const && integer_zerop (size))
4335 pedwarn (input_location, OPT_pedantic,
4336 "ISO C forbids zero-size array %qs", name);
4338 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
4340 constant_expression_warning (size);
4341 if (tree_int_cst_sgn (size) < 0)
4343 error ("size of array %qs is negative", name);
4344 size = integer_one_node;
4346 /* Handle a size folded to an integer constant but
4347 not an integer constant expression. */
4348 if (!size_int_const)
4350 /* If this is a file scope declaration of an
4351 ordinary identifier, this is invalid code;
4352 diagnosing it here and not subsequently
4353 treating the type as variable-length avoids
4354 more confusing diagnostics later. */
4355 if ((decl_context == NORMAL || decl_context == FIELD)
4356 && current_scope == file_scope)
4357 pedwarn (input_location, 0,
4358 "variably modified %qs at file scope", name);
4359 else
4360 this_size_varies = size_varies = 1;
4361 warn_variable_length_array (orig_name, size);
4364 else if ((decl_context == NORMAL || decl_context == FIELD)
4365 && current_scope == file_scope)
4367 error ("variably modified %qs at file scope", name);
4368 size = integer_one_node;
4370 else
4372 /* Make sure the array size remains visibly
4373 nonconstant even if it is (eg) a const variable
4374 with known value. */
4375 this_size_varies = size_varies = 1;
4376 warn_variable_length_array (orig_name, size);
4379 if (integer_zerop (size) && !this_size_varies)
4381 /* A zero-length array cannot be represented with
4382 an unsigned index type, which is what we'll
4383 get with build_index_type. Create an
4384 open-ended range instead. */
4385 itype = build_range_type (sizetype, size, NULL_TREE);
4387 else
4389 /* Arrange for the SAVE_EXPR on the inside of the
4390 MINUS_EXPR, which allows the -1 to get folded
4391 with the +1 that happens when building TYPE_SIZE. */
4392 if (size_varies)
4393 size = variable_size (size);
4394 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
4395 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
4396 integer_zero_node, size);
4398 /* Compute the maximum valid index, that is, size
4399 - 1. Do the calculation in index_type, so that
4400 if it is a variable the computations will be
4401 done in the proper mode. */
4402 itype = fold_build2 (MINUS_EXPR, index_type,
4403 convert (index_type, size),
4404 convert (index_type,
4405 size_one_node));
4407 /* If that overflowed, the array is too big. ???
4408 While a size of INT_MAX+1 technically shouldn't
4409 cause an overflow (because we subtract 1), the
4410 overflow is recorded during the conversion to
4411 index_type, before the subtraction. Handling
4412 this case seems like an unnecessary
4413 complication. */
4414 if (TREE_CODE (itype) == INTEGER_CST
4415 && TREE_OVERFLOW (itype))
4417 error ("size of array %qs is too large", name);
4418 type = error_mark_node;
4419 continue;
4422 itype = build_index_type (itype);
4424 if (this_size_varies)
4426 if (*expr)
4427 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
4428 *expr, size);
4429 else
4430 *expr = size;
4431 *expr_const_operands &= size_maybe_const;
4434 else if (decl_context == FIELD)
4436 bool flexible_array_member = false;
4437 if (array_parm_vla_unspec_p)
4438 /* Field names can in fact have function prototype
4439 scope so [*] is disallowed here through making
4440 the field variably modified, not through being
4441 something other than a declaration with function
4442 prototype scope. */
4443 size_varies = 1;
4444 else
4446 const struct c_declarator *t = declarator;
4447 while (t->kind == cdk_attrs)
4448 t = t->declarator;
4449 flexible_array_member = (t->kind == cdk_id);
4451 if (flexible_array_member
4452 && pedantic && !flag_isoc99 && !in_system_header)
4453 pedwarn (input_location, OPT_pedantic,
4454 "ISO C90 does not support flexible array members");
4456 /* ISO C99 Flexible array members are effectively
4457 identical to GCC's zero-length array extension. */
4458 if (flexible_array_member || array_parm_vla_unspec_p)
4459 itype = build_range_type (sizetype, size_zero_node,
4460 NULL_TREE);
4462 else if (decl_context == PARM)
4464 if (array_parm_vla_unspec_p)
4466 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4467 size_varies = 1;
4470 else if (decl_context == TYPENAME)
4472 if (array_parm_vla_unspec_p)
4474 /* C99 6.7.5.2p4 */
4475 warning (0, "%<[*]%> not in a declaration");
4476 /* We use this to avoid messing up with incomplete
4477 array types of the same type, that would
4478 otherwise be modified below. */
4479 itype = build_range_type (sizetype, size_zero_node,
4480 NULL_TREE);
4481 size_varies = 1;
4485 /* Complain about arrays of incomplete types. */
4486 if (!COMPLETE_TYPE_P (type))
4488 error ("array type has incomplete element type");
4489 type = error_mark_node;
4491 else
4492 /* When itype is NULL, a shared incomplete array type is
4493 returned for all array of a given type. Elsewhere we
4494 make sure we don't complete that type before copying
4495 it, but here we want to make sure we don't ever
4496 modify the shared type, so we gcc_assert (itype)
4497 below. */
4498 type = build_array_type (type, itype);
4500 if (type != error_mark_node)
4502 if (size_varies)
4504 /* It is ok to modify type here even if itype is
4505 NULL: if size_varies, we're in a
4506 multi-dimensional array and the inner type has
4507 variable size, so the enclosing shared array type
4508 must too. */
4509 if (size && TREE_CODE (size) == INTEGER_CST)
4510 type
4511 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4512 C_TYPE_VARIABLE_SIZE (type) = 1;
4515 /* The GCC extension for zero-length arrays differs from
4516 ISO flexible array members in that sizeof yields
4517 zero. */
4518 if (size && integer_zerop (size))
4520 gcc_assert (itype);
4521 TYPE_SIZE (type) = bitsize_zero_node;
4522 TYPE_SIZE_UNIT (type) = size_zero_node;
4524 if (array_parm_vla_unspec_p)
4526 gcc_assert (itype);
4527 /* The type is complete. C99 6.7.5.2p4 */
4528 TYPE_SIZE (type) = bitsize_zero_node;
4529 TYPE_SIZE_UNIT (type) = size_zero_node;
4533 if (decl_context != PARM
4534 && (array_ptr_quals != TYPE_UNQUALIFIED
4535 || array_ptr_attrs != NULL_TREE
4536 || array_parm_static))
4538 error ("static or type qualifiers in non-parameter array declarator");
4539 array_ptr_quals = TYPE_UNQUALIFIED;
4540 array_ptr_attrs = NULL_TREE;
4541 array_parm_static = 0;
4543 break;
4545 case cdk_function:
4547 /* Say it's a definition only for the declarator closest
4548 to the identifier, apart possibly from some
4549 attributes. */
4550 bool really_funcdef = false;
4551 tree arg_types;
4552 if (funcdef_flag)
4554 const struct c_declarator *t = declarator->declarator;
4555 while (t->kind == cdk_attrs)
4556 t = t->declarator;
4557 really_funcdef = (t->kind == cdk_id);
4560 /* Declaring a function type. Make sure we have a valid
4561 type for the function to return. */
4562 if (type == error_mark_node)
4563 continue;
4565 size_varies = 0;
4567 /* Warn about some types functions can't return. */
4568 if (TREE_CODE (type) == FUNCTION_TYPE)
4570 error ("%qs declared as function returning a function", name);
4571 type = integer_type_node;
4573 if (TREE_CODE (type) == ARRAY_TYPE)
4575 error ("%qs declared as function returning an array", name);
4576 type = integer_type_node;
4579 /* Construct the function type and go to the next
4580 inner layer of declarator. */
4581 arg_info = declarator->u.arg_info;
4582 arg_types = grokparms (arg_info, really_funcdef);
4583 if (really_funcdef)
4584 put_pending_sizes (arg_info->pending_sizes);
4586 /* Type qualifiers before the return type of the function
4587 qualify the return type, not the function type. */
4588 if (type_quals)
4590 /* Type qualifiers on a function return type are
4591 normally permitted by the standard but have no
4592 effect, so give a warning at -Wreturn-type.
4593 Qualifiers on a void return type are banned on
4594 function definitions in ISO C; GCC used to used
4595 them for noreturn functions. */
4596 if (VOID_TYPE_P (type) && really_funcdef)
4597 pedwarn (input_location, 0,
4598 "function definition has qualified void return type");
4599 else
4600 warning (OPT_Wignored_qualifiers,
4601 "type qualifiers ignored on function return type");
4603 type = c_build_qualified_type (type, type_quals);
4605 type_quals = TYPE_UNQUALIFIED;
4607 type = build_function_type (type, arg_types);
4608 declarator = declarator->declarator;
4610 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4611 the formal parameter list of this FUNCTION_TYPE to point to
4612 the FUNCTION_TYPE node itself. */
4614 tree link;
4616 for (link = arg_info->tags;
4617 link;
4618 link = TREE_CHAIN (link))
4619 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4621 break;
4623 case cdk_pointer:
4625 /* Merge any constancy or volatility into the target type
4626 for the pointer. */
4628 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4629 && type_quals)
4630 pedwarn (input_location, OPT_pedantic,
4631 "ISO C forbids qualified function types");
4632 if (type_quals)
4633 type = c_build_qualified_type (type, type_quals);
4634 size_varies = 0;
4636 /* When the pointed-to type involves components of variable size,
4637 care must be taken to ensure that the size evaluation code is
4638 emitted early enough to dominate all the possible later uses
4639 and late enough for the variables on which it depends to have
4640 been assigned.
4642 This is expected to happen automatically when the pointed-to
4643 type has a name/declaration of it's own, but special attention
4644 is required if the type is anonymous.
4646 We handle the NORMAL and FIELD contexts here by attaching an
4647 artificial TYPE_DECL to such pointed-to type. This forces the
4648 sizes evaluation at a safe point and ensures it is not deferred
4649 until e.g. within a deeper conditional context.
4651 We expect nothing to be needed here for PARM or TYPENAME.
4652 Pushing a TYPE_DECL at this point for TYPENAME would actually
4653 be incorrect, as we might be in the middle of an expression
4654 with side effects on the pointed-to type size "arguments" prior
4655 to the pointer declaration point and the fake TYPE_DECL in the
4656 enclosing context would force the size evaluation prior to the
4657 side effects. */
4659 if (!TYPE_NAME (type)
4660 && (decl_context == NORMAL || decl_context == FIELD)
4661 && variably_modified_type_p (type, NULL_TREE))
4663 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4664 DECL_ARTIFICIAL (decl) = 1;
4665 pushdecl (decl);
4666 finish_decl (decl, NULL_TREE, NULL_TREE);
4667 TYPE_NAME (type) = decl;
4670 type = build_pointer_type (type);
4672 /* Process type qualifiers (such as const or volatile)
4673 that were given inside the `*'. */
4674 type_quals = declarator->u.pointer_quals;
4676 declarator = declarator->declarator;
4677 break;
4679 default:
4680 gcc_unreachable ();
4683 *decl_attrs = chainon (returned_attrs, *decl_attrs);
4685 /* Now TYPE has the actual type, apart from any qualifiers in
4686 TYPE_QUALS. */
4688 /* Check the type and width of a bit-field. */
4689 if (bitfield)
4690 check_bitfield_type_and_width (&type, width, orig_name);
4692 /* Did array size calculations overflow? */
4694 if (TREE_CODE (type) == ARRAY_TYPE
4695 && COMPLETE_TYPE_P (type)
4696 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4697 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4699 error ("size of array %qs is too large", name);
4700 /* If we proceed with the array type as it is, we'll eventually
4701 crash in tree_low_cst(). */
4702 type = error_mark_node;
4705 /* If this is declaring a typedef name, return a TYPE_DECL. */
4707 if (storage_class == csc_typedef)
4709 tree decl;
4710 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4711 && type_quals)
4712 pedwarn (input_location, OPT_pedantic,
4713 "ISO C forbids qualified function types");
4714 if (type_quals)
4715 type = c_build_qualified_type (type, type_quals);
4716 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4717 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4718 if (declspecs->explicit_signed_p)
4719 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4720 if (declspecs->inline_p)
4721 pedwarn (input_location, 0,"typedef %q+D declared %<inline%>", decl);
4722 return decl;
4725 /* If this is a type name (such as, in a cast or sizeof),
4726 compute the type and return it now. */
4728 if (decl_context == TYPENAME)
4730 /* Note that the grammar rejects storage classes in typenames
4731 and fields. */
4732 gcc_assert (storage_class == csc_none && !threadp
4733 && !declspecs->inline_p);
4734 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4735 && type_quals)
4736 pedwarn (input_location, OPT_pedantic,
4737 "ISO C forbids const or volatile function types");
4738 if (type_quals)
4739 type = c_build_qualified_type (type, type_quals);
4740 return type;
4743 if (pedantic && decl_context == FIELD
4744 && variably_modified_type_p (type, NULL_TREE))
4746 /* C99 6.7.2.1p8 */
4747 pedwarn (input_location, OPT_pedantic,
4748 "a member of a structure or union cannot have a variably modified type");
4751 /* Aside from typedefs and type names (handle above),
4752 `void' at top level (not within pointer)
4753 is allowed only in public variables.
4754 We don't complain about parms either, but that is because
4755 a better error message can be made later. */
4757 if (VOID_TYPE_P (type) && decl_context != PARM
4758 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4759 && (storage_class == csc_extern
4760 || (current_scope == file_scope
4761 && !(storage_class == csc_static
4762 || storage_class == csc_register)))))
4764 error ("variable or field %qs declared void", name);
4765 type = integer_type_node;
4768 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4769 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4772 tree decl;
4774 if (decl_context == PARM)
4776 tree promoted_type;
4778 /* A parameter declared as an array of T is really a pointer to T.
4779 One declared as a function is really a pointer to a function. */
4781 if (TREE_CODE (type) == ARRAY_TYPE)
4783 /* Transfer const-ness of array into that of type pointed to. */
4784 type = TREE_TYPE (type);
4785 if (type_quals)
4786 type = c_build_qualified_type (type, type_quals);
4787 type = build_pointer_type (type);
4788 type_quals = array_ptr_quals;
4789 if (type_quals)
4790 type = c_build_qualified_type (type, type_quals);
4792 /* We don't yet implement attributes in this context. */
4793 if (array_ptr_attrs != NULL_TREE)
4794 warning (OPT_Wattributes,
4795 "attributes in parameter array declarator ignored");
4797 size_varies = 0;
4799 else if (TREE_CODE (type) == FUNCTION_TYPE)
4801 if (type_quals)
4802 pedwarn (input_location, OPT_pedantic,
4803 "ISO C forbids qualified function types");
4804 if (type_quals)
4805 type = c_build_qualified_type (type, type_quals);
4806 type = build_pointer_type (type);
4807 type_quals = TYPE_UNQUALIFIED;
4809 else if (type_quals)
4810 type = c_build_qualified_type (type, type_quals);
4812 decl = build_decl (PARM_DECL, declarator->u.id, type);
4813 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4814 if (size_varies)
4815 C_DECL_VARIABLE_SIZE (decl) = 1;
4817 /* Compute the type actually passed in the parmlist,
4818 for the case where there is no prototype.
4819 (For example, shorts and chars are passed as ints.)
4820 When there is a prototype, this is overridden later. */
4822 if (type == error_mark_node)
4823 promoted_type = type;
4824 else
4825 promoted_type = c_type_promotes_to (type);
4827 DECL_ARG_TYPE (decl) = promoted_type;
4828 if (declspecs->inline_p)
4829 pedwarn (input_location, 0, "parameter %q+D declared %<inline%>", decl);
4831 else if (decl_context == FIELD)
4833 /* Note that the grammar rejects storage classes in typenames
4834 and fields. */
4835 gcc_assert (storage_class == csc_none && !threadp
4836 && !declspecs->inline_p);
4838 /* Structure field. It may not be a function. */
4840 if (TREE_CODE (type) == FUNCTION_TYPE)
4842 error ("field %qs declared as a function", name);
4843 type = build_pointer_type (type);
4845 else if (TREE_CODE (type) != ERROR_MARK
4846 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4848 error ("field %qs has incomplete type", name);
4849 type = error_mark_node;
4851 type = c_build_qualified_type (type, type_quals);
4852 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4853 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4854 DECL_NONADDRESSABLE_P (decl) = bitfield;
4855 if (bitfield && !declarator->u.id)
4856 TREE_NO_WARNING (decl) = 1;
4858 if (size_varies)
4859 C_DECL_VARIABLE_SIZE (decl) = 1;
4861 else if (TREE_CODE (type) == FUNCTION_TYPE)
4863 if (storage_class == csc_register || threadp)
4865 error ("invalid storage class for function %qs", name);
4867 else if (current_scope != file_scope)
4869 /* Function declaration not at file scope. Storage
4870 classes other than `extern' are not allowed, C99
4871 6.7.1p5, and `extern' makes no difference. However,
4872 GCC allows 'auto', perhaps with 'inline', to support
4873 nested functions. */
4874 if (storage_class == csc_auto)
4875 pedwarn (input_location, OPT_pedantic, "invalid storage class for function %qs", name);
4876 else if (storage_class == csc_static)
4878 error ("invalid storage class for function %qs", name);
4879 if (funcdef_flag)
4880 storage_class = declspecs->storage_class = csc_none;
4881 else
4882 return 0;
4886 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4887 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4888 decl = build_decl_attribute_variant (decl, decl_attr);
4890 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4891 pedwarn (input_location, OPT_pedantic,
4892 "ISO C forbids qualified function types");
4894 /* GNU C interprets a volatile-qualified function type to indicate
4895 that the function does not return. */
4896 if ((type_quals & TYPE_QUAL_VOLATILE)
4897 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4898 warning (0, "%<noreturn%> function returns non-void value");
4900 /* Every function declaration is an external reference
4901 (DECL_EXTERNAL) except for those which are not at file
4902 scope and are explicitly declared "auto". This is
4903 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4904 GCC to signify a forward declaration of a nested function. */
4905 if (storage_class == csc_auto && current_scope != file_scope)
4906 DECL_EXTERNAL (decl) = 0;
4907 /* In C99, a function which is declared 'inline' with 'extern'
4908 is not an external reference (which is confusing). It
4909 means that the later definition of the function must be output
4910 in this file, C99 6.7.4p6. In GNU C89, a function declared
4911 'extern inline' is an external reference. */
4912 else if (declspecs->inline_p && storage_class != csc_static)
4913 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
4914 == flag_gnu89_inline);
4915 else
4916 DECL_EXTERNAL (decl) = !initialized;
4918 /* Record absence of global scope for `static' or `auto'. */
4919 TREE_PUBLIC (decl)
4920 = !(storage_class == csc_static || storage_class == csc_auto);
4922 /* For a function definition, record the argument information
4923 block where store_parm_decls will look for it. */
4924 if (funcdef_flag)
4925 current_function_arg_info = arg_info;
4927 if (declspecs->default_int_p)
4928 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4930 /* Record presence of `inline', if it is reasonable. */
4931 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4933 if (declspecs->inline_p)
4934 pedwarn (input_location, 0, "cannot inline function %<main%>");
4936 else if (declspecs->inline_p)
4937 /* Record that the function is declared `inline'. */
4938 DECL_DECLARED_INLINE_P (decl) = 1;
4940 else
4942 /* It's a variable. */
4943 /* An uninitialized decl with `extern' is a reference. */
4944 int extern_ref = !initialized && storage_class == csc_extern;
4946 type = c_build_qualified_type (type, type_quals);
4948 /* C99 6.2.2p7: It is invalid (compile-time undefined
4949 behavior) to create an 'extern' declaration for a
4950 variable if there is a global declaration that is
4951 'static' and the global declaration is not visible.
4952 (If the static declaration _is_ currently visible,
4953 the 'extern' declaration is taken to refer to that decl.) */
4954 if (extern_ref && current_scope != file_scope)
4956 tree global_decl = identifier_global_value (declarator->u.id);
4957 tree visible_decl = lookup_name (declarator->u.id);
4959 if (global_decl
4960 && global_decl != visible_decl
4961 && TREE_CODE (global_decl) == VAR_DECL
4962 && !TREE_PUBLIC (global_decl))
4963 error ("variable previously declared %<static%> redeclared "
4964 "%<extern%>");
4967 decl = build_decl (VAR_DECL, declarator->u.id, type);
4968 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4969 if (size_varies)
4970 C_DECL_VARIABLE_SIZE (decl) = 1;
4972 if (declspecs->inline_p)
4973 pedwarn (input_location, 0, "variable %q+D declared %<inline%>", decl);
4975 /* At file scope, an initialized extern declaration may follow
4976 a static declaration. In that case, DECL_EXTERNAL will be
4977 reset later in start_decl. */
4978 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4980 /* At file scope, the presence of a `static' or `register' storage
4981 class specifier, or the absence of all storage class specifiers
4982 makes this declaration a definition (perhaps tentative). Also,
4983 the absence of `static' makes it public. */
4984 if (current_scope == file_scope)
4986 TREE_PUBLIC (decl) = storage_class != csc_static;
4987 TREE_STATIC (decl) = !extern_ref;
4989 /* Not at file scope, only `static' makes a static definition. */
4990 else
4992 TREE_STATIC (decl) = (storage_class == csc_static);
4993 TREE_PUBLIC (decl) = extern_ref;
4996 if (threadp)
4997 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
5000 if (storage_class == csc_extern
5001 && variably_modified_type_p (type, NULL_TREE))
5003 /* C99 6.7.5.2p2 */
5004 error ("object with variably modified type must have no linkage");
5007 /* Record `register' declaration for warnings on &
5008 and in case doing stupid register allocation. */
5010 if (storage_class == csc_register)
5012 C_DECL_REGISTER (decl) = 1;
5013 DECL_REGISTER (decl) = 1;
5016 /* Record constancy and volatility. */
5017 c_apply_type_quals_to_decl (type_quals, decl);
5019 /* If a type has volatile components, it should be stored in memory.
5020 Otherwise, the fact that those components are volatile
5021 will be ignored, and would even crash the compiler.
5022 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
5023 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
5024 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
5025 || TREE_CODE (decl) == RESULT_DECL))
5027 /* It is not an error for a structure with volatile fields to
5028 be declared register, but reset DECL_REGISTER since it
5029 cannot actually go in a register. */
5030 int was_reg = C_DECL_REGISTER (decl);
5031 C_DECL_REGISTER (decl) = 0;
5032 DECL_REGISTER (decl) = 0;
5033 c_mark_addressable (decl);
5034 C_DECL_REGISTER (decl) = was_reg;
5037 /* This is the earliest point at which we might know the assembler
5038 name of a variable. Thus, if it's known before this, die horribly. */
5039 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
5041 return decl;
5045 /* Decode the parameter-list info for a function type or function definition.
5046 The argument is the value returned by `get_parm_info' (or made in c-parse.c
5047 if there is an identifier list instead of a parameter decl list).
5048 These two functions are separate because when a function returns
5049 or receives functions then each is called multiple times but the order
5050 of calls is different. The last call to `grokparms' is always the one
5051 that contains the formal parameter names of a function definition.
5053 Return a list of arg types to use in the FUNCTION_TYPE for this function.
5055 FUNCDEF_FLAG is true for a function definition, false for
5056 a mere declaration. A nonempty identifier-list gets an error message
5057 when FUNCDEF_FLAG is false. */
5059 static tree
5060 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
5062 tree arg_types = arg_info->types;
5064 if (funcdef_flag && arg_info->had_vla_unspec)
5066 /* A function definition isn't function prototype scope C99 6.2.1p4. */
5067 /* C99 6.7.5.2p4 */
5068 error ("%<[*]%> not allowed in other than function prototype scope");
5071 if (arg_types == 0 && !funcdef_flag && !in_system_header)
5072 warning (OPT_Wstrict_prototypes,
5073 "function declaration isn%'t a prototype");
5075 if (arg_types == error_mark_node)
5076 return 0; /* don't set TYPE_ARG_TYPES in this case */
5078 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5080 if (!funcdef_flag)
5081 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
5083 arg_info->parms = arg_info->types;
5084 arg_info->types = 0;
5085 return 0;
5087 else
5089 tree parm, type, typelt;
5090 unsigned int parmno;
5092 /* If there is a parameter of incomplete type in a definition,
5093 this is an error. In a declaration this is valid, and a
5094 struct or union type may be completed later, before any calls
5095 or definition of the function. In the case where the tag was
5096 first declared within the parameter list, a warning has
5097 already been given. If a parameter has void type, then
5098 however the function cannot be defined or called, so
5099 warn. */
5101 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5102 parm;
5103 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5105 type = TREE_VALUE (typelt);
5106 if (type == error_mark_node)
5107 continue;
5109 if (!COMPLETE_TYPE_P (type))
5111 if (funcdef_flag)
5113 if (DECL_NAME (parm))
5114 error ("parameter %u (%q+D) has incomplete type",
5115 parmno, parm);
5116 else
5117 error ("%Jparameter %u has incomplete type",
5118 parm, parmno);
5120 TREE_VALUE (typelt) = error_mark_node;
5121 TREE_TYPE (parm) = error_mark_node;
5123 else if (VOID_TYPE_P (type))
5125 if (DECL_NAME (parm))
5126 warning (0, "parameter %u (%q+D) has void type",
5127 parmno, parm);
5128 else
5129 warning (0, "%Jparameter %u has void type",
5130 parm, parmno);
5134 if (DECL_NAME (parm) && TREE_USED (parm))
5135 warn_if_shadowing (parm);
5137 return arg_types;
5141 /* Take apart the current scope and return a c_arg_info structure with
5142 info on a parameter list just parsed.
5144 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5146 ELLIPSIS being true means the argument list ended in '...' so don't
5147 append a sentinel (void_list_node) to the end of the type-list. */
5149 struct c_arg_info *
5150 get_parm_info (bool ellipsis)
5152 struct c_binding *b = current_scope->bindings;
5153 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5154 struct c_arg_info);
5155 tree parms = 0;
5156 tree tags = 0;
5157 tree types = 0;
5158 tree others = 0;
5160 static bool explained_incomplete_types = false;
5161 bool gave_void_only_once_err = false;
5163 arg_info->parms = 0;
5164 arg_info->tags = 0;
5165 arg_info->types = 0;
5166 arg_info->others = 0;
5167 arg_info->pending_sizes = 0;
5168 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5170 /* The bindings in this scope must not get put into a block.
5171 We will take care of deleting the binding nodes. */
5172 current_scope->bindings = 0;
5174 /* This function is only called if there was *something* on the
5175 parameter list. */
5176 gcc_assert (b);
5178 /* A parameter list consisting solely of 'void' indicates that the
5179 function takes no arguments. But if the 'void' is qualified
5180 (by 'const' or 'volatile'), or has a storage class specifier
5181 ('register'), then the behavior is undefined; issue an error.
5182 Typedefs for 'void' are OK (see DR#157). */
5183 if (b->prev == 0 /* one binding */
5184 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5185 && !DECL_NAME (b->decl) /* anonymous */
5186 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5188 if (TREE_THIS_VOLATILE (b->decl)
5189 || TREE_READONLY (b->decl)
5190 || C_DECL_REGISTER (b->decl))
5191 error ("%<void%> as only parameter may not be qualified");
5193 /* There cannot be an ellipsis. */
5194 if (ellipsis)
5195 error ("%<void%> must be the only parameter");
5197 arg_info->types = void_list_node;
5198 return arg_info;
5201 if (!ellipsis)
5202 types = void_list_node;
5204 /* Break up the bindings list into parms, tags, types, and others;
5205 apply sanity checks; purge the name-to-decl bindings. */
5206 while (b)
5208 tree decl = b->decl;
5209 tree type = TREE_TYPE (decl);
5210 const char *keyword;
5212 switch (TREE_CODE (decl))
5214 case PARM_DECL:
5215 if (b->id)
5217 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5218 I_SYMBOL_BINDING (b->id) = b->shadowed;
5221 /* Check for forward decls that never got their actual decl. */
5222 if (TREE_ASM_WRITTEN (decl))
5223 error ("parameter %q+D has just a forward declaration", decl);
5224 /* Check for (..., void, ...) and issue an error. */
5225 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5227 if (!gave_void_only_once_err)
5229 error ("%<void%> must be the only parameter");
5230 gave_void_only_once_err = true;
5233 else
5235 /* Valid parameter, add it to the list. */
5236 TREE_CHAIN (decl) = parms;
5237 parms = decl;
5239 /* Since there is a prototype, args are passed in their
5240 declared types. The back end may override this later. */
5241 DECL_ARG_TYPE (decl) = type;
5242 types = tree_cons (0, type, types);
5244 break;
5246 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5247 case UNION_TYPE: keyword = "union"; goto tag;
5248 case RECORD_TYPE: keyword = "struct"; goto tag;
5249 tag:
5250 /* Types may not have tag-names, in which case the type
5251 appears in the bindings list with b->id NULL. */
5252 if (b->id)
5254 gcc_assert (I_TAG_BINDING (b->id) == b);
5255 I_TAG_BINDING (b->id) = b->shadowed;
5258 /* Warn about any struct, union or enum tags defined in a
5259 parameter list. The scope of such types is limited to
5260 the parameter list, which is rarely if ever desirable
5261 (it's impossible to call such a function with type-
5262 correct arguments). An anonymous union parm type is
5263 meaningful as a GNU extension, so don't warn for that. */
5264 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5266 if (b->id)
5267 /* The %s will be one of 'struct', 'union', or 'enum'. */
5268 warning (0, "%<%s %E%> declared inside parameter list",
5269 keyword, b->id);
5270 else
5271 /* The %s will be one of 'struct', 'union', or 'enum'. */
5272 warning (0, "anonymous %s declared inside parameter list",
5273 keyword);
5275 if (!explained_incomplete_types)
5277 warning (0, "its scope is only this definition or declaration,"
5278 " which is probably not what you want");
5279 explained_incomplete_types = true;
5283 tags = tree_cons (b->id, decl, tags);
5284 break;
5286 case CONST_DECL:
5287 case TYPE_DECL:
5288 case FUNCTION_DECL:
5289 /* CONST_DECLs appear here when we have an embedded enum,
5290 and TYPE_DECLs appear here when we have an embedded struct
5291 or union. No warnings for this - we already warned about the
5292 type itself. FUNCTION_DECLs appear when there is an implicit
5293 function declaration in the parameter list. */
5295 TREE_CHAIN (decl) = others;
5296 others = decl;
5297 /* fall through */
5299 case ERROR_MARK:
5300 /* error_mark_node appears here when we have an undeclared
5301 variable. Just throw it away. */
5302 if (b->id)
5304 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5305 I_SYMBOL_BINDING (b->id) = b->shadowed;
5307 break;
5309 /* Other things that might be encountered. */
5310 case LABEL_DECL:
5311 case VAR_DECL:
5312 default:
5313 gcc_unreachable ();
5316 b = free_binding_and_advance (b);
5319 arg_info->parms = parms;
5320 arg_info->tags = tags;
5321 arg_info->types = types;
5322 arg_info->others = others;
5323 arg_info->pending_sizes = get_pending_sizes ();
5324 return arg_info;
5327 /* Get the struct, enum or union (CODE says which) with tag NAME.
5328 Define the tag as a forward-reference if it is not defined.
5329 Return a c_typespec structure for the type specifier. */
5331 struct c_typespec
5332 parser_xref_tag (enum tree_code code, tree name)
5334 struct c_typespec ret;
5335 tree ref;
5337 ret.expr = NULL_TREE;
5338 ret.expr_const_operands = true;
5340 /* If a cross reference is requested, look up the type
5341 already defined for this tag and return it. */
5343 ref = lookup_tag (code, name, 0);
5344 /* If this is the right type of tag, return what we found.
5345 (This reference will be shadowed by shadow_tag later if appropriate.)
5346 If this is the wrong type of tag, do not return it. If it was the
5347 wrong type in the same scope, we will have had an error
5348 message already; if in a different scope and declaring
5349 a name, pending_xref_error will give an error message; but if in a
5350 different scope and not declaring a name, this tag should
5351 shadow the previous declaration of a different type of tag, and
5352 this would not work properly if we return the reference found.
5353 (For example, with "struct foo" in an outer scope, "union foo;"
5354 must shadow that tag with a new one of union type.) */
5355 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5356 if (ref && TREE_CODE (ref) == code)
5358 ret.spec = ref;
5359 return ret;
5362 /* If no such tag is yet defined, create a forward-reference node
5363 and record it as the "definition".
5364 When a real declaration of this type is found,
5365 the forward-reference will be altered into a real type. */
5367 ref = make_node (code);
5368 if (code == ENUMERAL_TYPE)
5370 /* Give the type a default layout like unsigned int
5371 to avoid crashing if it does not get defined. */
5372 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
5373 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5374 TYPE_USER_ALIGN (ref) = 0;
5375 TYPE_UNSIGNED (ref) = 1;
5376 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5377 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5378 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5381 pushtag (name, ref);
5383 ret.spec = ref;
5384 return ret;
5387 /* Get the struct, enum or union (CODE says which) with tag NAME.
5388 Define the tag as a forward-reference if it is not defined.
5389 Return a tree for the type. */
5391 tree
5392 xref_tag (enum tree_code code, tree name)
5394 return parser_xref_tag (code, name).spec;
5397 /* Make sure that the tag NAME is defined *in the current scope*
5398 at least as a forward reference.
5399 CODE says which kind of tag NAME ought to be. */
5401 tree
5402 start_struct (enum tree_code code, tree name)
5404 /* If there is already a tag defined at this scope
5405 (as a forward reference), just return it. */
5407 tree ref = 0;
5409 if (name != 0)
5410 ref = lookup_tag (code, name, 1);
5411 if (ref && TREE_CODE (ref) == code)
5413 if (TYPE_SIZE (ref))
5415 if (code == UNION_TYPE)
5416 error ("redefinition of %<union %E%>", name);
5417 else
5418 error ("redefinition of %<struct %E%>", name);
5419 /* Don't create structures using a name already in use. */
5420 ref = NULL_TREE;
5422 else if (C_TYPE_BEING_DEFINED (ref))
5424 if (code == UNION_TYPE)
5425 error ("nested redefinition of %<union %E%>", name);
5426 else
5427 error ("nested redefinition of %<struct %E%>", name);
5428 /* Don't create structures that contain themselves. */
5429 ref = NULL_TREE;
5433 /* Otherwise create a forward-reference just so the tag is in scope. */
5435 if (ref == NULL_TREE || TREE_CODE (ref) != code)
5437 ref = make_node (code);
5438 pushtag (name, ref);
5441 C_TYPE_BEING_DEFINED (ref) = 1;
5442 TYPE_PACKED (ref) = flag_pack_struct;
5443 return ref;
5446 /* Process the specs, declarator and width (NULL if omitted)
5447 of a structure component, returning a FIELD_DECL node.
5448 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5449 DECL_ATTRS is as for grokdeclarator.
5451 LOC is the location of the structure component.
5453 This is done during the parsing of the struct declaration.
5454 The FIELD_DECL nodes are chained together and the lot of them
5455 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5457 tree
5458 grokfield (location_t loc,
5459 struct c_declarator *declarator, struct c_declspecs *declspecs,
5460 tree width, tree *decl_attrs)
5462 tree value;
5464 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5465 && width == NULL_TREE)
5467 /* This is an unnamed decl.
5469 If we have something of the form "union { list } ;" then this
5470 is the anonymous union extension. Similarly for struct.
5472 If this is something of the form "struct foo;", then
5473 If MS extensions are enabled, this is handled as an
5474 anonymous struct.
5475 Otherwise this is a forward declaration of a structure tag.
5477 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5478 If MS extensions are enabled and foo names a structure, then
5479 again this is an anonymous struct.
5480 Otherwise this is an error.
5482 Oh what a horrid tangled web we weave. I wonder if MS consciously
5483 took this from Plan 9 or if it was an accident of implementation
5484 that took root before someone noticed the bug... */
5486 tree type = declspecs->type;
5487 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5488 || TREE_CODE (type) == UNION_TYPE);
5489 bool ok = false;
5491 if (type_ok
5492 && (flag_ms_extensions || !declspecs->typedef_p))
5494 if (flag_ms_extensions)
5495 ok = true;
5496 else if (flag_iso)
5497 ok = false;
5498 else if (TYPE_NAME (type) == NULL)
5499 ok = true;
5500 else
5501 ok = false;
5503 if (!ok)
5505 pedwarn (loc, 0, "declaration does not declare anything");
5506 return NULL_TREE;
5508 pedwarn (loc, OPT_pedantic, "ISO C doesn%'t support unnamed structs/unions");
5511 value = grokdeclarator (declarator, declspecs, FIELD, false,
5512 width ? &width : NULL, decl_attrs, NULL, NULL,
5513 DEPRECATED_NORMAL);
5515 finish_decl (value, NULL_TREE, NULL_TREE);
5516 DECL_INITIAL (value) = width;
5518 return value;
5521 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5522 the list such that this does not present a problem later. */
5524 static void
5525 detect_field_duplicates (tree fieldlist)
5527 tree x, y;
5528 int timeout = 10;
5530 /* First, see if there are more than "a few" fields.
5531 This is trivially true if there are zero or one fields. */
5532 if (!fieldlist)
5533 return;
5534 x = TREE_CHAIN (fieldlist);
5535 if (!x)
5536 return;
5537 do {
5538 timeout--;
5539 x = TREE_CHAIN (x);
5540 } while (timeout > 0 && x);
5542 /* If there were "few" fields, avoid the overhead of allocating
5543 a hash table. Instead just do the nested traversal thing. */
5544 if (timeout > 0)
5546 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5547 if (DECL_NAME (x))
5549 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5550 if (DECL_NAME (y) == DECL_NAME (x))
5552 error ("duplicate member %q+D", x);
5553 DECL_NAME (x) = NULL_TREE;
5557 else
5559 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5560 void **slot;
5562 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5563 if ((y = DECL_NAME (x)) != 0)
5565 slot = htab_find_slot (htab, y, INSERT);
5566 if (*slot)
5568 error ("duplicate member %q+D", x);
5569 DECL_NAME (x) = NULL_TREE;
5571 *slot = y;
5574 htab_delete (htab);
5578 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5579 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5580 ATTRIBUTES are attributes to be applied to the structure. */
5582 tree
5583 finish_struct (tree t, tree fieldlist, tree attributes)
5585 tree x;
5586 bool toplevel = file_scope == current_scope;
5587 int saw_named_field;
5589 /* If this type was previously laid out as a forward reference,
5590 make sure we lay it out again. */
5592 TYPE_SIZE (t) = 0;
5594 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5596 if (pedantic)
5598 for (x = fieldlist; x; x = TREE_CHAIN (x))
5599 if (DECL_NAME (x) != 0)
5600 break;
5602 if (x == 0)
5604 if (TREE_CODE (t) == UNION_TYPE)
5606 if (fieldlist)
5607 pedwarn (input_location, OPT_pedantic, "union has no named members");
5608 else
5609 pedwarn (input_location, OPT_pedantic, "union has no members");
5611 else
5613 if (fieldlist)
5614 pedwarn (input_location, OPT_pedantic, "struct has no named members");
5615 else
5616 pedwarn (input_location, OPT_pedantic, "struct has no members");
5621 /* Install struct as DECL_CONTEXT of each field decl.
5622 Also process specified field sizes, found in the DECL_INITIAL,
5623 storing 0 there after the type has been changed to precision equal
5624 to its width, rather than the precision of the specified standard
5625 type. (Correct layout requires the original type to have been preserved
5626 until now.) */
5628 saw_named_field = 0;
5629 for (x = fieldlist; x; x = TREE_CHAIN (x))
5631 if (TREE_TYPE (x) == error_mark_node)
5632 continue;
5634 DECL_CONTEXT (x) = t;
5636 /* If any field is const, the structure type is pseudo-const. */
5637 if (TREE_READONLY (x))
5638 C_TYPE_FIELDS_READONLY (t) = 1;
5639 else
5641 /* A field that is pseudo-const makes the structure likewise. */
5642 tree t1 = TREE_TYPE (x);
5643 while (TREE_CODE (t1) == ARRAY_TYPE)
5644 t1 = TREE_TYPE (t1);
5645 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5646 && C_TYPE_FIELDS_READONLY (t1))
5647 C_TYPE_FIELDS_READONLY (t) = 1;
5650 /* Any field that is volatile means variables of this type must be
5651 treated in some ways as volatile. */
5652 if (TREE_THIS_VOLATILE (x))
5653 C_TYPE_FIELDS_VOLATILE (t) = 1;
5655 /* Any field of nominal variable size implies structure is too. */
5656 if (C_DECL_VARIABLE_SIZE (x))
5657 C_TYPE_VARIABLE_SIZE (t) = 1;
5659 if (DECL_INITIAL (x))
5661 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5662 DECL_SIZE (x) = bitsize_int (width);
5663 DECL_BIT_FIELD (x) = 1;
5664 SET_DECL_C_BIT_FIELD (x);
5667 if (TYPE_PACKED (t)
5668 && (DECL_BIT_FIELD (x)
5669 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
5670 DECL_PACKED (x) = 1;
5672 /* Detect flexible array member in an invalid context. */
5673 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5674 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5675 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5676 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5678 if (TREE_CODE (t) == UNION_TYPE)
5680 error ("%Jflexible array member in union", x);
5681 TREE_TYPE (x) = error_mark_node;
5683 else if (TREE_CHAIN (x) != NULL_TREE)
5685 error ("%Jflexible array member not at end of struct", x);
5686 TREE_TYPE (x) = error_mark_node;
5688 else if (!saw_named_field)
5690 error ("%Jflexible array member in otherwise empty struct", x);
5691 TREE_TYPE (x) = error_mark_node;
5695 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5696 && flexible_array_type_p (TREE_TYPE (x)))
5697 pedwarn (input_location, OPT_pedantic,
5698 "%Jinvalid use of structure with flexible array member", x);
5700 if (DECL_NAME (x))
5701 saw_named_field = 1;
5704 detect_field_duplicates (fieldlist);
5706 /* Now we have the nearly final fieldlist. Record it,
5707 then lay out the structure or union (including the fields). */
5709 TYPE_FIELDS (t) = fieldlist;
5711 layout_type (t);
5713 /* Give bit-fields their proper types. */
5715 tree *fieldlistp = &fieldlist;
5716 while (*fieldlistp)
5717 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5718 && TREE_TYPE (*fieldlistp) != error_mark_node)
5720 unsigned HOST_WIDE_INT width
5721 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5722 tree type = TREE_TYPE (*fieldlistp);
5723 if (width != TYPE_PRECISION (type))
5725 TREE_TYPE (*fieldlistp)
5726 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5727 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5729 DECL_INITIAL (*fieldlistp) = 0;
5731 else
5732 fieldlistp = &TREE_CHAIN (*fieldlistp);
5735 /* Now we have the truly final field list.
5736 Store it in this type and in the variants. */
5738 TYPE_FIELDS (t) = fieldlist;
5740 /* If there are lots of fields, sort so we can look through them fast.
5741 We arbitrarily consider 16 or more elts to be "a lot". */
5744 int len = 0;
5746 for (x = fieldlist; x; x = TREE_CHAIN (x))
5748 if (len > 15 || DECL_NAME (x) == NULL)
5749 break;
5750 len += 1;
5753 if (len > 15)
5755 tree *field_array;
5756 struct lang_type *space;
5757 struct sorted_fields_type *space2;
5759 len += list_length (x);
5761 /* Use the same allocation policy here that make_node uses, to
5762 ensure that this lives as long as the rest of the struct decl.
5763 All decls in an inline function need to be saved. */
5765 space = GGC_CNEW (struct lang_type);
5766 space2 = GGC_NEWVAR (struct sorted_fields_type,
5767 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5769 len = 0;
5770 space->s = space2;
5771 field_array = &space2->elts[0];
5772 for (x = fieldlist; x; x = TREE_CHAIN (x))
5774 field_array[len++] = x;
5776 /* If there is anonymous struct or union, break out of the loop. */
5777 if (DECL_NAME (x) == NULL)
5778 break;
5780 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5781 if (x == NULL)
5783 TYPE_LANG_SPECIFIC (t) = space;
5784 TYPE_LANG_SPECIFIC (t)->s->len = len;
5785 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5786 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5791 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5793 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5794 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5795 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5796 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5797 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5800 /* If this was supposed to be a transparent union, but we can't
5801 make it one, warn and turn off the flag. */
5802 if (TREE_CODE (t) == UNION_TYPE
5803 && TYPE_TRANSPARENT_UNION (t)
5804 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5806 TYPE_TRANSPARENT_UNION (t) = 0;
5807 warning (0, "union cannot be made transparent");
5810 /* If this structure or union completes the type of any previous
5811 variable declaration, lay it out and output its rtl. */
5812 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5814 x = TREE_CHAIN (x))
5816 tree decl = TREE_VALUE (x);
5817 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5818 layout_array_type (TREE_TYPE (decl));
5819 if (TREE_CODE (decl) != TYPE_DECL)
5821 layout_decl (decl, 0);
5822 if (c_dialect_objc ())
5823 objc_check_decl (decl);
5824 rest_of_decl_compilation (decl, toplevel, 0);
5825 if (!toplevel)
5826 expand_decl (decl);
5829 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5831 /* Finish debugging output for this type. */
5832 rest_of_type_compilation (t, toplevel);
5834 /* If we're inside a function proper, i.e. not file-scope and not still
5835 parsing parameters, then arrange for the size of a variable sized type
5836 to be bound now. */
5837 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
5838 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5840 return t;
5843 /* Lay out the type T, and its element type, and so on. */
5845 static void
5846 layout_array_type (tree t)
5848 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5849 layout_array_type (TREE_TYPE (t));
5850 layout_type (t);
5853 /* Begin compiling the definition of an enumeration type.
5854 NAME is its name (or null if anonymous).
5855 Returns the type object, as yet incomplete.
5856 Also records info about it so that build_enumerator
5857 may be used to declare the individual values as they are read. */
5859 tree
5860 start_enum (struct c_enum_contents *the_enum, tree name)
5862 tree enumtype = 0;
5864 /* If this is the real definition for a previous forward reference,
5865 fill in the contents in the same object that used to be the
5866 forward reference. */
5868 if (name != 0)
5869 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5871 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5873 enumtype = make_node (ENUMERAL_TYPE);
5874 pushtag (name, enumtype);
5877 if (C_TYPE_BEING_DEFINED (enumtype))
5878 error ("nested redefinition of %<enum %E%>", name);
5880 C_TYPE_BEING_DEFINED (enumtype) = 1;
5882 if (TYPE_VALUES (enumtype) != 0)
5884 /* This enum is a named one that has been declared already. */
5885 error ("redeclaration of %<enum %E%>", name);
5887 /* Completely replace its old definition.
5888 The old enumerators remain defined, however. */
5889 TYPE_VALUES (enumtype) = 0;
5892 the_enum->enum_next_value = integer_zero_node;
5893 the_enum->enum_overflow = 0;
5895 if (flag_short_enums)
5896 TYPE_PACKED (enumtype) = 1;
5898 return enumtype;
5901 /* After processing and defining all the values of an enumeration type,
5902 install their decls in the enumeration type and finish it off.
5903 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5904 and ATTRIBUTES are the specified attributes.
5905 Returns ENUMTYPE. */
5907 tree
5908 finish_enum (tree enumtype, tree values, tree attributes)
5910 tree pair, tem;
5911 tree minnode = 0, maxnode = 0;
5912 int precision, unsign;
5913 bool toplevel = (file_scope == current_scope);
5914 struct lang_type *lt;
5916 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5918 /* Calculate the maximum value of any enumerator in this type. */
5920 if (values == error_mark_node)
5921 minnode = maxnode = integer_zero_node;
5922 else
5924 minnode = maxnode = TREE_VALUE (values);
5925 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5927 tree value = TREE_VALUE (pair);
5928 if (tree_int_cst_lt (maxnode, value))
5929 maxnode = value;
5930 if (tree_int_cst_lt (value, minnode))
5931 minnode = value;
5935 /* Construct the final type of this enumeration. It is the same
5936 as one of the integral types - the narrowest one that fits, except
5937 that normally we only go as narrow as int - and signed iff any of
5938 the values are negative. */
5939 unsign = (tree_int_cst_sgn (minnode) >= 0);
5940 precision = MAX (tree_int_cst_min_precision (minnode, unsign),
5941 tree_int_cst_min_precision (maxnode, unsign));
5943 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5945 tem = c_common_type_for_size (precision, unsign);
5946 if (tem == NULL)
5948 warning (0, "enumeration values exceed range of largest integer");
5949 tem = long_long_integer_type_node;
5952 else
5953 tem = unsign ? unsigned_type_node : integer_type_node;
5955 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5956 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5957 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5958 TYPE_SIZE (enumtype) = 0;
5960 /* If the precision of the type was specific with an attribute and it
5961 was too small, give an error. Otherwise, use it. */
5962 if (TYPE_PRECISION (enumtype))
5964 if (precision > TYPE_PRECISION (enumtype))
5965 error ("specified mode too small for enumeral values");
5967 else
5968 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5970 layout_type (enumtype);
5972 if (values != error_mark_node)
5974 /* Change the type of the enumerators to be the enum type. We
5975 need to do this irrespective of the size of the enum, for
5976 proper type checking. Replace the DECL_INITIALs of the
5977 enumerators, and the value slots of the list, with copies
5978 that have the enum type; they cannot be modified in place
5979 because they may be shared (e.g. integer_zero_node) Finally,
5980 change the purpose slots to point to the names of the decls. */
5981 for (pair = values; pair; pair = TREE_CHAIN (pair))
5983 tree enu = TREE_PURPOSE (pair);
5984 tree ini = DECL_INITIAL (enu);
5986 TREE_TYPE (enu) = enumtype;
5988 /* The ISO C Standard mandates enumerators to have type int,
5989 even though the underlying type of an enum type is
5990 unspecified. However, GCC allows enumerators of any
5991 integer type as an extensions. build_enumerator()
5992 converts any enumerators that fit in an int to type int,
5993 to avoid promotions to unsigned types when comparing
5994 integers with enumerators that fit in the int range.
5995 When -pedantic is given, build_enumerator() would have
5996 already warned about those that don't fit. Here we
5997 convert the rest to the enumerator type. */
5998 if (TREE_TYPE (ini) != integer_type_node)
5999 ini = convert (enumtype, ini);
6001 DECL_INITIAL (enu) = ini;
6002 TREE_PURPOSE (pair) = DECL_NAME (enu);
6003 TREE_VALUE (pair) = ini;
6006 TYPE_VALUES (enumtype) = values;
6009 /* Record the min/max values so that we can warn about bit-field
6010 enumerations that are too small for the values. */
6011 lt = GGC_CNEW (struct lang_type);
6012 lt->enum_min = minnode;
6013 lt->enum_max = maxnode;
6014 TYPE_LANG_SPECIFIC (enumtype) = lt;
6016 /* Fix up all variant types of this enum type. */
6017 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
6019 if (tem == enumtype)
6020 continue;
6021 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
6022 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
6023 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
6024 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
6025 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
6026 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
6027 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
6028 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
6029 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
6030 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
6031 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
6034 /* Finish debugging output for this type. */
6035 rest_of_type_compilation (enumtype, toplevel);
6037 return enumtype;
6040 /* Build and install a CONST_DECL for one value of the
6041 current enumeration type (one that was begun with start_enum).
6042 Return a tree-list containing the CONST_DECL and its value.
6043 Assignment of sequential values by default is handled here. */
6045 tree
6046 build_enumerator (struct c_enum_contents *the_enum, tree name, tree value,
6047 location_t value_loc)
6049 tree decl, type;
6051 /* Validate and default VALUE. */
6053 if (value != 0)
6055 /* Don't issue more errors for error_mark_node (i.e. an
6056 undeclared identifier) - just ignore the value expression. */
6057 if (value == error_mark_node)
6058 value = 0;
6059 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
6060 || TREE_CODE (value) != INTEGER_CST)
6062 error ("enumerator value for %qE is not an integer constant", name);
6063 value = 0;
6065 else
6067 value = default_conversion (value);
6068 constant_expression_warning (value);
6072 /* Default based on previous value. */
6073 /* It should no longer be possible to have NON_LVALUE_EXPR
6074 in the default. */
6075 if (value == 0)
6077 value = the_enum->enum_next_value;
6078 if (the_enum->enum_overflow)
6079 error ("overflow in enumeration values");
6081 /* Even though the underlying type of an enum is unspecified, the
6082 type of enumeration constants is explicitly defined as int
6083 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
6084 an extension. */
6085 else if (!int_fits_type_p (value, integer_type_node))
6086 pedwarn (value_loc, OPT_pedantic,
6087 "ISO C restricts enumerator values to range of %<int%>");
6089 /* The ISO C Standard mandates enumerators to have type int, even
6090 though the underlying type of an enum type is unspecified.
6091 However, GCC allows enumerators of any integer type as an
6092 extensions. Here we convert any enumerators that fit in an int
6093 to type int, to avoid promotions to unsigned types when comparing
6094 integers with enumerators that fit in the int range. When
6095 -pedantic is given, we would have already warned about those that
6096 don't fit. We have to do this here rather than in finish_enum
6097 because this value may be used to define more enumerators. */
6098 if (int_fits_type_p (value, integer_type_node))
6099 value = convert (integer_type_node, value);
6101 /* Set basis for default for next value. */
6102 the_enum->enum_next_value
6103 = build_binary_op
6104 (EXPR_HAS_LOCATION (value) ? EXPR_LOCATION (value) : input_location,
6105 PLUS_EXPR, value, integer_one_node, 0);
6106 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
6108 /* Now create a declaration for the enum value name. */
6110 type = TREE_TYPE (value);
6111 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6112 TYPE_PRECISION (integer_type_node)),
6113 (TYPE_PRECISION (type)
6114 >= TYPE_PRECISION (integer_type_node)
6115 && TYPE_UNSIGNED (type)));
6117 decl = build_decl (CONST_DECL, name, type);
6118 DECL_INITIAL (decl) = convert (type, value);
6119 pushdecl (decl);
6121 return tree_cons (decl, value, NULL_TREE);
6125 /* Create the FUNCTION_DECL for a function definition.
6126 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6127 the declaration; they describe the function's name and the type it returns,
6128 but twisted together in a fashion that parallels the syntax of C.
6130 This function creates a binding context for the function body
6131 as well as setting up the FUNCTION_DECL in current_function_decl.
6133 Returns 1 on success. If the DECLARATOR is not suitable for a function
6134 (it defines a datum instead), we return 0, which tells
6135 yyparse to report a parse error. */
6138 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6139 tree attributes)
6141 tree decl1, old_decl;
6142 tree restype, resdecl;
6143 struct c_label_context_se *nstack_se;
6144 struct c_label_context_vm *nstack_vm;
6146 current_function_returns_value = 0; /* Assume, until we see it does. */
6147 current_function_returns_null = 0;
6148 current_function_returns_abnormally = 0;
6149 warn_about_return_type = 0;
6150 c_switch_stack = NULL;
6152 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6153 nstack_se->labels_def = NULL;
6154 nstack_se->labels_used = NULL;
6155 nstack_se->next = label_context_stack_se;
6156 label_context_stack_se = nstack_se;
6158 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6159 nstack_vm->labels_def = NULL;
6160 nstack_vm->labels_used = NULL;
6161 nstack_vm->scope = 0;
6162 nstack_vm->next = label_context_stack_vm;
6163 label_context_stack_vm = nstack_vm;
6165 /* Indicate no valid break/continue context by setting these variables
6166 to some non-null, non-label value. We'll notice and emit the proper
6167 error message in c_finish_bc_stmt. */
6168 c_break_label = c_cont_label = size_zero_node;
6170 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
6171 &attributes, NULL, NULL, DEPRECATED_NORMAL);
6173 /* If the declarator is not suitable for a function definition,
6174 cause a syntax error. */
6175 if (decl1 == 0)
6177 label_context_stack_se = label_context_stack_se->next;
6178 label_context_stack_vm = label_context_stack_vm->next;
6179 return 0;
6182 decl_attributes (&decl1, attributes, 0);
6184 if (DECL_DECLARED_INLINE_P (decl1)
6185 && DECL_UNINLINABLE (decl1)
6186 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6187 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6188 decl1);
6190 /* Handle gnu_inline attribute. */
6191 if (declspecs->inline_p
6192 && !flag_gnu89_inline
6193 && TREE_CODE (decl1) == FUNCTION_DECL
6194 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
6195 || current_function_decl))
6197 if (declspecs->storage_class != csc_static)
6198 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6201 announce_function (decl1);
6203 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6205 error ("return type is an incomplete type");
6206 /* Make it return void instead. */
6207 TREE_TYPE (decl1)
6208 = build_function_type (void_type_node,
6209 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6212 if (warn_about_return_type)
6213 pedwarn_c99 (input_location, flag_isoc99 ? 0
6214 : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
6215 "return type defaults to %<int%>");
6217 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6218 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6219 DECL_INITIAL (decl1) = error_mark_node;
6221 /* If this definition isn't a prototype and we had a prototype declaration
6222 before, copy the arg type info from that prototype. */
6223 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6224 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6225 old_decl = 0;
6226 current_function_prototype_locus = UNKNOWN_LOCATION;
6227 current_function_prototype_built_in = false;
6228 current_function_prototype_arg_types = NULL_TREE;
6229 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6231 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6232 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6233 TREE_TYPE (TREE_TYPE (old_decl))))
6235 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6236 TREE_TYPE (decl1));
6237 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6238 current_function_prototype_built_in
6239 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6240 current_function_prototype_arg_types
6241 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6243 if (TREE_PUBLIC (decl1))
6245 /* If there is an external prototype declaration of this
6246 function, record its location but do not copy information
6247 to this decl. This may be an invisible declaration
6248 (built-in or in a scope which has finished) or simply
6249 have more refined argument types than any declaration
6250 found above. */
6251 struct c_binding *b;
6252 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6253 if (B_IN_SCOPE (b, external_scope))
6254 break;
6255 if (b)
6257 tree ext_decl, ext_type;
6258 ext_decl = b->decl;
6259 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6260 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6261 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6262 TREE_TYPE (ext_type)))
6264 current_function_prototype_locus
6265 = DECL_SOURCE_LOCATION (ext_decl);
6266 current_function_prototype_built_in
6267 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6268 current_function_prototype_arg_types
6269 = TYPE_ARG_TYPES (ext_type);
6275 /* Optionally warn of old-fashioned def with no previous prototype. */
6276 if (warn_strict_prototypes
6277 && old_decl != error_mark_node
6278 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6279 && C_DECL_ISNT_PROTOTYPE (old_decl))
6280 warning (OPT_Wstrict_prototypes,
6281 "function declaration isn%'t a prototype");
6282 /* Optionally warn of any global def with no previous prototype. */
6283 else if (warn_missing_prototypes
6284 && old_decl != error_mark_node
6285 && TREE_PUBLIC (decl1)
6286 && !MAIN_NAME_P (DECL_NAME (decl1))
6287 && C_DECL_ISNT_PROTOTYPE (old_decl))
6288 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6289 /* Optionally warn of any def with no previous prototype
6290 if the function has already been used. */
6291 else if (warn_missing_prototypes
6292 && old_decl != 0
6293 && old_decl != error_mark_node
6294 && TREE_USED (old_decl)
6295 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6296 warning (OPT_Wmissing_prototypes,
6297 "%q+D was used with no prototype before its definition", decl1);
6298 /* Optionally warn of any global def with no previous declaration. */
6299 else if (warn_missing_declarations
6300 && TREE_PUBLIC (decl1)
6301 && old_decl == 0
6302 && !MAIN_NAME_P (DECL_NAME (decl1)))
6303 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6304 decl1);
6305 /* Optionally warn of any def with no previous declaration
6306 if the function has already been used. */
6307 else if (warn_missing_declarations
6308 && old_decl != 0
6309 && old_decl != error_mark_node
6310 && TREE_USED (old_decl)
6311 && C_DECL_IMPLICIT (old_decl))
6312 warning (OPT_Wmissing_declarations,
6313 "%q+D was used with no declaration before its definition", decl1);
6315 /* This function exists in static storage.
6316 (This does not mean `static' in the C sense!) */
6317 TREE_STATIC (decl1) = 1;
6319 /* A nested function is not global. */
6320 if (current_function_decl != 0)
6321 TREE_PUBLIC (decl1) = 0;
6323 /* This is the earliest point at which we might know the assembler
6324 name of the function. Thus, if it's set before this, die horribly. */
6325 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6327 /* If #pragma weak was used, mark the decl weak now. */
6328 if (current_scope == file_scope)
6329 maybe_apply_pragma_weak (decl1);
6331 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6332 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
6334 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6335 != integer_type_node)
6336 pedwarn (input_location, OPT_Wmain, "return type of %q+D is not %<int%>", decl1);
6338 check_main_parameter_types (decl1);
6340 if (!TREE_PUBLIC (decl1))
6341 pedwarn (input_location, OPT_Wmain, "%q+D is normally a non-static function", decl1);
6344 /* Record the decl so that the function name is defined.
6345 If we already have a decl for this name, and it is a FUNCTION_DECL,
6346 use the old decl. */
6348 current_function_decl = pushdecl (decl1);
6350 push_scope ();
6351 declare_parm_level ();
6353 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6354 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6355 DECL_ARTIFICIAL (resdecl) = 1;
6356 DECL_IGNORED_P (resdecl) = 1;
6357 DECL_RESULT (current_function_decl) = resdecl;
6359 start_fname_decls ();
6361 return 1;
6364 /* Subroutine of store_parm_decls which handles new-style function
6365 definitions (prototype format). The parms already have decls, so we
6366 need only record them as in effect and complain if any redundant
6367 old-style parm decls were written. */
6368 static void
6369 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6371 tree decl;
6373 if (current_scope->bindings)
6375 error ("%Jold-style parameter declarations in prototyped "
6376 "function definition", fndecl);
6378 /* Get rid of the old-style declarations. */
6379 pop_scope ();
6380 push_scope ();
6382 /* Don't issue this warning for nested functions, and don't issue this
6383 warning if we got here because ARG_INFO_TYPES was error_mark_node
6384 (this happens when a function definition has just an ellipsis in
6385 its parameter list). */
6386 else if (!in_system_header && !current_function_scope
6387 && arg_info->types != error_mark_node)
6388 warning (OPT_Wtraditional,
6389 "%Jtraditional C rejects ISO C style function definitions",
6390 fndecl);
6392 /* Now make all the parameter declarations visible in the function body.
6393 We can bypass most of the grunt work of pushdecl. */
6394 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6396 DECL_CONTEXT (decl) = current_function_decl;
6397 if (DECL_NAME (decl))
6399 bind (DECL_NAME (decl), decl, current_scope,
6400 /*invisible=*/false, /*nested=*/false,
6401 UNKNOWN_LOCATION);
6402 if (!TREE_USED (decl))
6403 warn_if_shadowing (decl);
6405 else
6406 error ("%Jparameter name omitted", decl);
6409 /* Record the parameter list in the function declaration. */
6410 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6412 /* Now make all the ancillary declarations visible, likewise. */
6413 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6415 DECL_CONTEXT (decl) = current_function_decl;
6416 if (DECL_NAME (decl))
6417 bind (DECL_NAME (decl), decl, current_scope,
6418 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
6421 /* And all the tag declarations. */
6422 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6423 if (TREE_PURPOSE (decl))
6424 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6425 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
6428 /* Subroutine of store_parm_decls which handles old-style function
6429 definitions (separate parameter list and declarations). */
6431 static void
6432 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6434 struct c_binding *b;
6435 tree parm, decl, last;
6436 tree parmids = arg_info->parms;
6437 struct pointer_set_t *seen_args = pointer_set_create ();
6439 if (!in_system_header)
6440 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6441 fndecl);
6443 /* Match each formal parameter name with its declaration. Save each
6444 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6445 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6447 if (TREE_VALUE (parm) == 0)
6449 error ("%Jparameter name missing from parameter list", fndecl);
6450 TREE_PURPOSE (parm) = 0;
6451 continue;
6454 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6455 if (b && B_IN_CURRENT_SCOPE (b))
6457 decl = b->decl;
6458 /* If we got something other than a PARM_DECL it is an error. */
6459 if (TREE_CODE (decl) != PARM_DECL)
6460 error ("%q+D declared as a non-parameter", decl);
6461 /* If the declaration is already marked, we have a duplicate
6462 name. Complain and ignore the duplicate. */
6463 else if (pointer_set_contains (seen_args, decl))
6465 error ("multiple parameters named %q+D", decl);
6466 TREE_PURPOSE (parm) = 0;
6467 continue;
6469 /* If the declaration says "void", complain and turn it into
6470 an int. */
6471 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6473 error ("parameter %q+D declared with void type", decl);
6474 TREE_TYPE (decl) = integer_type_node;
6475 DECL_ARG_TYPE (decl) = integer_type_node;
6476 layout_decl (decl, 0);
6478 warn_if_shadowing (decl);
6480 /* If no declaration found, default to int. */
6481 else
6483 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6484 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6485 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6486 pushdecl (decl);
6487 warn_if_shadowing (decl);
6489 if (flag_isoc99)
6490 pedwarn (input_location, 0, "type of %q+D defaults to %<int%>", decl);
6491 else
6492 warning (OPT_Wmissing_parameter_type, "type of %q+D defaults to %<int%>", decl);
6495 TREE_PURPOSE (parm) = decl;
6496 pointer_set_insert (seen_args, decl);
6499 /* Now examine the parms chain for incomplete declarations
6500 and declarations with no corresponding names. */
6502 for (b = current_scope->bindings; b; b = b->prev)
6504 parm = b->decl;
6505 if (TREE_CODE (parm) != PARM_DECL)
6506 continue;
6508 if (TREE_TYPE (parm) != error_mark_node
6509 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6511 error ("parameter %q+D has incomplete type", parm);
6512 TREE_TYPE (parm) = error_mark_node;
6515 if (!pointer_set_contains (seen_args, parm))
6517 error ("declaration for parameter %q+D but no such parameter", parm);
6519 /* Pretend the parameter was not missing.
6520 This gets us to a standard state and minimizes
6521 further error messages. */
6522 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6526 /* Chain the declarations together in the order of the list of
6527 names. Store that chain in the function decl, replacing the
6528 list of names. Update the current scope to match. */
6529 DECL_ARGUMENTS (fndecl) = 0;
6531 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6532 if (TREE_PURPOSE (parm))
6533 break;
6534 if (parm && TREE_PURPOSE (parm))
6536 last = TREE_PURPOSE (parm);
6537 DECL_ARGUMENTS (fndecl) = last;
6539 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6540 if (TREE_PURPOSE (parm))
6542 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6543 last = TREE_PURPOSE (parm);
6545 TREE_CHAIN (last) = 0;
6548 pointer_set_destroy (seen_args);
6550 /* If there was a previous prototype,
6551 set the DECL_ARG_TYPE of each argument according to
6552 the type previously specified, and report any mismatches. */
6554 if (current_function_prototype_arg_types)
6556 tree type;
6557 for (parm = DECL_ARGUMENTS (fndecl),
6558 type = current_function_prototype_arg_types;
6559 parm || (type && TREE_VALUE (type) != error_mark_node
6560 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
6561 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6563 if (parm == 0 || type == 0
6564 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6566 if (current_function_prototype_built_in)
6567 warning (0, "number of arguments doesn%'t match "
6568 "built-in prototype");
6569 else
6571 error ("number of arguments doesn%'t match prototype");
6572 error ("%Hprototype declaration",
6573 &current_function_prototype_locus);
6575 break;
6577 /* Type for passing arg must be consistent with that
6578 declared for the arg. ISO C says we take the unqualified
6579 type for parameters declared with qualified type. */
6580 if (TREE_TYPE (parm) != error_mark_node
6581 && TREE_TYPE (type) != error_mark_node
6582 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6583 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6585 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6586 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6588 /* Adjust argument to match prototype. E.g. a previous
6589 `int foo(float);' prototype causes
6590 `int foo(x) float x; {...}' to be treated like
6591 `int foo(float x) {...}'. This is particularly
6592 useful for argument types like uid_t. */
6593 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6595 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6596 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6597 && TYPE_PRECISION (TREE_TYPE (parm))
6598 < TYPE_PRECISION (integer_type_node))
6599 DECL_ARG_TYPE (parm) = integer_type_node;
6601 /* ??? Is it possible to get here with a
6602 built-in prototype or will it always have
6603 been diagnosed as conflicting with an
6604 old-style definition and discarded? */
6605 if (current_function_prototype_built_in)
6606 warning (OPT_pedantic, "promoted argument %qD "
6607 "doesn%'t match built-in prototype", parm);
6608 else
6610 pedwarn (input_location, OPT_pedantic, "promoted argument %qD "
6611 "doesn%'t match prototype", parm);
6612 pedwarn (current_function_prototype_locus, OPT_pedantic,
6613 "prototype declaration");
6616 else
6618 if (current_function_prototype_built_in)
6619 warning (0, "argument %qD doesn%'t match "
6620 "built-in prototype", parm);
6621 else
6623 error ("argument %qD doesn%'t match prototype", parm);
6624 error ("%Hprototype declaration",
6625 &current_function_prototype_locus);
6630 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6633 /* Otherwise, create a prototype that would match. */
6635 else
6637 tree actual = 0, last = 0, type;
6639 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6641 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6642 if (last)
6643 TREE_CHAIN (last) = type;
6644 else
6645 actual = type;
6646 last = type;
6648 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6649 if (last)
6650 TREE_CHAIN (last) = type;
6651 else
6652 actual = type;
6654 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6655 of the type of this function, but we need to avoid having this
6656 affect the types of other similarly-typed functions, so we must
6657 first force the generation of an identical (but separate) type
6658 node for the relevant function type. The new node we create
6659 will be a variant of the main variant of the original function
6660 type. */
6662 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6664 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6668 /* Store parameter declarations passed in ARG_INFO into the current
6669 function declaration. */
6671 void
6672 store_parm_decls_from (struct c_arg_info *arg_info)
6674 current_function_arg_info = arg_info;
6675 store_parm_decls ();
6678 /* Store the parameter declarations into the current function declaration.
6679 This is called after parsing the parameter declarations, before
6680 digesting the body of the function.
6682 For an old-style definition, construct a prototype out of the old-style
6683 parameter declarations and inject it into the function's type. */
6685 void
6686 store_parm_decls (void)
6688 tree fndecl = current_function_decl;
6689 bool proto;
6691 /* The argument information block for FNDECL. */
6692 struct c_arg_info *arg_info = current_function_arg_info;
6693 current_function_arg_info = 0;
6695 /* True if this definition is written with a prototype. Note:
6696 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6697 list in a function definition as equivalent to (void) -- an
6698 empty argument list specifies the function has no parameters,
6699 but only (void) sets up a prototype for future calls. */
6700 proto = arg_info->types != 0;
6702 if (proto)
6703 store_parm_decls_newstyle (fndecl, arg_info);
6704 else
6705 store_parm_decls_oldstyle (fndecl, arg_info);
6707 /* The next call to push_scope will be a function body. */
6709 next_is_function_body = true;
6711 /* Write a record describing this function definition to the prototypes
6712 file (if requested). */
6714 gen_aux_info_record (fndecl, 1, 0, proto);
6716 /* Initialize the RTL code for the function. */
6717 allocate_struct_function (fndecl, false);
6719 /* Begin the statement tree for this function. */
6720 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6722 /* ??? Insert the contents of the pending sizes list into the function
6723 to be evaluated. The only reason left to have this is
6724 void foo(int n, int array[n++])
6725 because we throw away the array type in favor of a pointer type, and
6726 thus won't naturally see the SAVE_EXPR containing the increment. All
6727 other pending sizes would be handled by gimplify_parameters. */
6729 tree t;
6730 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6731 add_stmt (TREE_VALUE (t));
6734 /* Even though we're inside a function body, we still don't want to
6735 call expand_expr to calculate the size of a variable-sized array.
6736 We haven't necessarily assigned RTL to all variables yet, so it's
6737 not safe to try to expand expressions involving them. */
6738 cfun->dont_save_pending_sizes_p = 1;
6741 /* Emit diagnostics that require gimple input for detection. Operate on
6742 FNDECL and all its nested functions. */
6744 static void
6745 c_gimple_diagnostics_recursively (tree fndecl)
6747 struct cgraph_node *cgn;
6748 gimple_seq body = gimple_body (fndecl);
6750 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6751 c_warn_unused_result (body);
6753 /* Notice when OpenMP structured block constraints are violated. */
6754 if (flag_openmp)
6755 diagnose_omp_structured_block_errors (fndecl);
6757 /* Finalize all nested functions now. */
6758 cgn = cgraph_node (fndecl);
6759 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6760 c_gimple_diagnostics_recursively (cgn->decl);
6763 /* Finish up a function declaration and compile that function
6764 all the way to assembler language output. The free the storage
6765 for the function definition.
6767 This is called after parsing the body of the function definition. */
6769 void
6770 finish_function (void)
6772 tree fndecl = current_function_decl;
6774 label_context_stack_se = label_context_stack_se->next;
6775 label_context_stack_vm = label_context_stack_vm->next;
6777 if (TREE_CODE (fndecl) == FUNCTION_DECL
6778 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6780 tree args = DECL_ARGUMENTS (fndecl);
6781 for (; args; args = TREE_CHAIN (args))
6783 tree type = TREE_TYPE (args);
6784 if (INTEGRAL_TYPE_P (type)
6785 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6786 DECL_ARG_TYPE (args) = integer_type_node;
6790 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6791 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6793 /* Must mark the RESULT_DECL as being in this function. */
6795 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6796 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6798 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
6799 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6800 == integer_type_node && flag_isoc99)
6802 tree stmt = c_finish_return (integer_zero_node);
6803 /* Hack. We don't want the middle-end to warn that this return
6804 is unreachable, so we mark its location as special. Using
6805 UNKNOWN_LOCATION has the problem that it gets clobbered in
6806 annotate_one_with_locus. A cleaner solution might be to
6807 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6809 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6812 /* Tie off the statement tree for this function. */
6813 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6815 finish_fname_decls ();
6817 /* Complain if there's just no return statement. */
6818 if (warn_return_type
6819 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6820 && !current_function_returns_value && !current_function_returns_null
6821 /* Don't complain if we are no-return. */
6822 && !current_function_returns_abnormally
6823 /* Don't warn for main(). */
6824 && !MAIN_NAME_P (DECL_NAME (fndecl))
6825 /* Or if they didn't actually specify a return type. */
6826 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6827 /* Normally, with -Wreturn-type, flow will complain, but we might
6828 optimize out static functions. */
6829 && !TREE_PUBLIC (fndecl))
6831 warning (OPT_Wreturn_type,
6832 "no return statement in function returning non-void");
6833 TREE_NO_WARNING (fndecl) = 1;
6836 /* Store the end of the function, so that we get good line number
6837 info for the epilogue. */
6838 cfun->function_end_locus = input_location;
6840 /* Finalize the ELF visibility for the function. */
6841 c_determine_visibility (fndecl);
6843 /* For GNU C extern inline functions disregard inline limits. */
6844 if (DECL_EXTERNAL (fndecl)
6845 && DECL_DECLARED_INLINE_P (fndecl))
6846 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
6848 /* Genericize before inlining. Delay genericizing nested functions
6849 until their parent function is genericized. Since finalizing
6850 requires GENERIC, delay that as well. */
6852 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6853 && !undef_nested_function)
6855 if (!decl_function_context (fndecl))
6857 c_genericize (fndecl);
6858 c_gimple_diagnostics_recursively (fndecl);
6860 /* ??? Objc emits functions after finalizing the compilation unit.
6861 This should be cleaned up later and this conditional removed. */
6862 if (cgraph_global_info_ready)
6864 cgraph_add_new_function (fndecl, false);
6865 return;
6868 cgraph_finalize_function (fndecl, false);
6870 else
6872 /* Register this function with cgraph just far enough to get it
6873 added to our parent's nested function list. Handy, since the
6874 C front end doesn't have such a list. */
6875 (void) cgraph_node (fndecl);
6879 if (!decl_function_context (fndecl))
6880 undef_nested_function = false;
6882 /* We're leaving the context of this function, so zap cfun.
6883 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6884 tree_rest_of_compilation. */
6885 set_cfun (NULL);
6886 current_function_decl = NULL;
6889 /* Check the declarations given in a for-loop for satisfying the C99
6890 constraints. If exactly one such decl is found, return it. */
6892 tree
6893 check_for_loop_decls (void)
6895 struct c_binding *b;
6896 tree one_decl = NULL_TREE;
6897 int n_decls = 0;
6899 if (!flag_isoc99)
6901 static bool hint = true;
6902 /* If we get here, declarations have been used in a for loop without
6903 the C99 for loop scope. This doesn't make much sense, so don't
6904 allow it. */
6905 error ("%<for%> loop initial declarations are only allowed in C99 mode");
6906 if (hint)
6908 inform (input_location,
6909 "use option -std=c99 or -std=gnu99 to compile your code");
6910 hint = false;
6912 return NULL_TREE;
6914 /* C99 subclause 6.8.5 paragraph 3:
6916 [#3] The declaration part of a for statement shall only
6917 declare identifiers for objects having storage class auto or
6918 register.
6920 It isn't clear whether, in this sentence, "identifiers" binds to
6921 "shall only declare" or to "objects" - that is, whether all identifiers
6922 declared must be identifiers for objects, or whether the restriction
6923 only applies to those that are. (A question on this in comp.std.c
6924 in November 2000 received no answer.) We implement the strictest
6925 interpretation, to avoid creating an extension which later causes
6926 problems. */
6928 for (b = current_scope->bindings; b; b = b->prev)
6930 tree id = b->id;
6931 tree decl = b->decl;
6933 if (!id)
6934 continue;
6936 switch (TREE_CODE (decl))
6938 case VAR_DECL:
6939 if (TREE_STATIC (decl))
6940 error ("declaration of static variable %q+D in %<for%> loop "
6941 "initial declaration", decl);
6942 else if (DECL_EXTERNAL (decl))
6943 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
6944 "initial declaration", decl);
6945 break;
6947 case RECORD_TYPE:
6948 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6949 id);
6950 break;
6951 case UNION_TYPE:
6952 error ("%<union %E%> declared in %<for%> loop initial declaration",
6953 id);
6954 break;
6955 case ENUMERAL_TYPE:
6956 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6957 id);
6958 break;
6959 default:
6960 error ("declaration of non-variable %q+D in %<for%> loop "
6961 "initial declaration", decl);
6964 n_decls++;
6965 one_decl = decl;
6968 return n_decls == 1 ? one_decl : NULL_TREE;
6971 /* Save and reinitialize the variables
6972 used during compilation of a C function. */
6974 void
6975 c_push_function_context (void)
6977 struct language_function *p;
6978 p = GGC_NEW (struct language_function);
6979 cfun->language = p;
6981 p->base.x_stmt_tree = c_stmt_tree;
6982 p->x_break_label = c_break_label;
6983 p->x_cont_label = c_cont_label;
6984 p->x_switch_stack = c_switch_stack;
6985 p->arg_info = current_function_arg_info;
6986 p->returns_value = current_function_returns_value;
6987 p->returns_null = current_function_returns_null;
6988 p->returns_abnormally = current_function_returns_abnormally;
6989 p->warn_about_return_type = warn_about_return_type;
6991 push_function_context ();
6994 /* Restore the variables used during compilation of a C function. */
6996 void
6997 c_pop_function_context (void)
6999 struct language_function *p;
7001 pop_function_context ();
7002 p = cfun->language;
7003 cfun->language = NULL;
7005 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
7006 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
7008 /* Stop pointing to the local nodes about to be freed. */
7009 /* But DECL_INITIAL must remain nonzero so we know this
7010 was an actual function definition. */
7011 DECL_INITIAL (current_function_decl) = error_mark_node;
7012 DECL_ARGUMENTS (current_function_decl) = 0;
7015 c_stmt_tree = p->base.x_stmt_tree;
7016 c_break_label = p->x_break_label;
7017 c_cont_label = p->x_cont_label;
7018 c_switch_stack = p->x_switch_stack;
7019 current_function_arg_info = p->arg_info;
7020 current_function_returns_value = p->returns_value;
7021 current_function_returns_null = p->returns_null;
7022 current_function_returns_abnormally = p->returns_abnormally;
7023 warn_about_return_type = p->warn_about_return_type;
7026 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
7028 void
7029 c_dup_lang_specific_decl (tree decl)
7031 struct lang_decl *ld;
7033 if (!DECL_LANG_SPECIFIC (decl))
7034 return;
7036 ld = GGC_NEW (struct lang_decl);
7037 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
7038 DECL_LANG_SPECIFIC (decl) = ld;
7041 /* The functions below are required for functionality of doing
7042 function at once processing in the C front end. Currently these
7043 functions are not called from anywhere in the C front end, but as
7044 these changes continue, that will change. */
7046 /* Returns the stmt_tree (if any) to which statements are currently
7047 being added. If there is no active statement-tree, NULL is
7048 returned. */
7050 stmt_tree
7051 current_stmt_tree (void)
7053 return &c_stmt_tree;
7056 /* Return the global value of T as a symbol. */
7058 tree
7059 identifier_global_value (tree t)
7061 struct c_binding *b;
7063 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
7064 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
7065 return b->decl;
7067 return 0;
7070 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
7071 otherwise the name is found in ridpointers from RID_INDEX. */
7073 void
7074 record_builtin_type (enum rid rid_index, const char *name, tree type)
7076 tree id, decl;
7077 if (name == 0)
7078 id = ridpointers[(int) rid_index];
7079 else
7080 id = get_identifier (name);
7081 decl = build_decl (TYPE_DECL, id, type);
7082 pushdecl (decl);
7083 if (debug_hooks->type_decl)
7084 debug_hooks->type_decl (decl, false);
7087 /* Build the void_list_node (void_type_node having been created). */
7088 tree
7089 build_void_list_node (void)
7091 tree t = build_tree_list (NULL_TREE, void_type_node);
7092 return t;
7095 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7097 struct c_parm *
7098 build_c_parm (struct c_declspecs *specs, tree attrs,
7099 struct c_declarator *declarator)
7101 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7102 ret->specs = specs;
7103 ret->attrs = attrs;
7104 ret->declarator = declarator;
7105 return ret;
7108 /* Return a declarator with nested attributes. TARGET is the inner
7109 declarator to which these attributes apply. ATTRS are the
7110 attributes. */
7112 struct c_declarator *
7113 build_attrs_declarator (tree attrs, struct c_declarator *target)
7115 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7116 ret->kind = cdk_attrs;
7117 ret->declarator = target;
7118 ret->u.attrs = attrs;
7119 return ret;
7122 /* Return a declarator for a function with arguments specified by ARGS
7123 and return type specified by TARGET. */
7125 struct c_declarator *
7126 build_function_declarator (struct c_arg_info *args,
7127 struct c_declarator *target)
7129 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7130 ret->kind = cdk_function;
7131 ret->declarator = target;
7132 ret->u.arg_info = args;
7133 return ret;
7136 /* Return a declarator for the identifier IDENT (which may be
7137 NULL_TREE for an abstract declarator). */
7139 struct c_declarator *
7140 build_id_declarator (tree ident)
7142 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7143 ret->kind = cdk_id;
7144 ret->declarator = 0;
7145 ret->u.id = ident;
7146 /* Default value - may get reset to a more precise location. */
7147 ret->id_loc = input_location;
7148 return ret;
7151 /* Return something to represent absolute declarators containing a *.
7152 TARGET is the absolute declarator that the * contains.
7153 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7154 to apply to the pointer type. */
7156 struct c_declarator *
7157 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7158 struct c_declarator *target)
7160 tree attrs;
7161 int quals = 0;
7162 struct c_declarator *itarget = target;
7163 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7164 if (type_quals_attrs)
7166 attrs = type_quals_attrs->attrs;
7167 quals = quals_from_declspecs (type_quals_attrs);
7168 if (attrs != NULL_TREE)
7169 itarget = build_attrs_declarator (attrs, target);
7171 ret->kind = cdk_pointer;
7172 ret->declarator = itarget;
7173 ret->u.pointer_quals = quals;
7174 return ret;
7177 /* Return a pointer to a structure for an empty list of declaration
7178 specifiers. */
7180 struct c_declspecs *
7181 build_null_declspecs (void)
7183 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7184 ret->type = 0;
7185 ret->expr = 0;
7186 ret->decl_attr = 0;
7187 ret->attrs = 0;
7188 ret->typespec_word = cts_none;
7189 ret->storage_class = csc_none;
7190 ret->expr_const_operands = true;
7191 ret->declspecs_seen_p = false;
7192 ret->type_seen_p = false;
7193 ret->non_sc_seen_p = false;
7194 ret->typedef_p = false;
7195 ret->tag_defined_p = false;
7196 ret->explicit_signed_p = false;
7197 ret->deprecated_p = false;
7198 ret->default_int_p = false;
7199 ret->long_p = false;
7200 ret->long_long_p = false;
7201 ret->short_p = false;
7202 ret->signed_p = false;
7203 ret->unsigned_p = false;
7204 ret->complex_p = false;
7205 ret->inline_p = false;
7206 ret->thread_p = false;
7207 ret->const_p = false;
7208 ret->volatile_p = false;
7209 ret->restrict_p = false;
7210 ret->saturating_p = false;
7211 return ret;
7214 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7215 returning SPECS. */
7217 struct c_declspecs *
7218 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7220 enum rid i;
7221 bool dupe = false;
7222 specs->non_sc_seen_p = true;
7223 specs->declspecs_seen_p = true;
7224 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7225 && C_IS_RESERVED_WORD (qual));
7226 i = C_RID_CODE (qual);
7227 switch (i)
7229 case RID_CONST:
7230 dupe = specs->const_p;
7231 specs->const_p = true;
7232 break;
7233 case RID_VOLATILE:
7234 dupe = specs->volatile_p;
7235 specs->volatile_p = true;
7236 break;
7237 case RID_RESTRICT:
7238 dupe = specs->restrict_p;
7239 specs->restrict_p = true;
7240 break;
7241 default:
7242 gcc_unreachable ();
7244 if (dupe && !flag_isoc99)
7245 pedwarn (input_location, OPT_pedantic, "duplicate %qE", qual);
7246 return specs;
7249 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7250 returning SPECS. */
7252 struct c_declspecs *
7253 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7255 tree type = spec.spec;
7256 specs->non_sc_seen_p = true;
7257 specs->declspecs_seen_p = true;
7258 specs->type_seen_p = true;
7259 if (TREE_DEPRECATED (type))
7260 specs->deprecated_p = true;
7262 /* Handle type specifier keywords. */
7263 if (TREE_CODE (type) == IDENTIFIER_NODE
7264 && C_IS_RESERVED_WORD (type)
7265 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
7267 enum rid i = C_RID_CODE (type);
7268 if (specs->type)
7270 error ("two or more data types in declaration specifiers");
7271 return specs;
7273 if ((int) i <= (int) RID_LAST_MODIFIER)
7275 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
7276 bool dupe = false;
7277 switch (i)
7279 case RID_LONG:
7280 if (specs->long_long_p)
7282 error ("%<long long long%> is too long for GCC");
7283 break;
7285 if (specs->long_p)
7287 if (specs->typespec_word == cts_double)
7289 error ("both %<long long%> and %<double%> in "
7290 "declaration specifiers");
7291 break;
7293 if (pedantic && !flag_isoc99 && !in_system_header)
7294 pedwarn (input_location, OPT_Wlong_long, "ISO C90 does not support %<long long%>");
7295 specs->long_long_p = 1;
7296 break;
7298 if (specs->short_p)
7299 error ("both %<long%> and %<short%> in "
7300 "declaration specifiers");
7301 else if (specs->typespec_word == cts_void)
7302 error ("both %<long%> and %<void%> in "
7303 "declaration specifiers");
7304 else if (specs->typespec_word == cts_bool)
7305 error ("both %<long%> and %<_Bool%> in "
7306 "declaration specifiers");
7307 else if (specs->typespec_word == cts_char)
7308 error ("both %<long%> and %<char%> in "
7309 "declaration specifiers");
7310 else if (specs->typespec_word == cts_float)
7311 error ("both %<long%> and %<float%> in "
7312 "declaration specifiers");
7313 else if (specs->typespec_word == cts_dfloat32)
7314 error ("both %<long%> and %<_Decimal32%> in "
7315 "declaration specifiers");
7316 else if (specs->typespec_word == cts_dfloat64)
7317 error ("both %<long%> and %<_Decimal64%> in "
7318 "declaration specifiers");
7319 else if (specs->typespec_word == cts_dfloat128)
7320 error ("both %<long%> and %<_Decimal128%> in "
7321 "declaration specifiers");
7322 else
7323 specs->long_p = true;
7324 break;
7325 case RID_SHORT:
7326 dupe = specs->short_p;
7327 if (specs->long_p)
7328 error ("both %<long%> and %<short%> in "
7329 "declaration specifiers");
7330 else if (specs->typespec_word == cts_void)
7331 error ("both %<short%> and %<void%> in "
7332 "declaration specifiers");
7333 else if (specs->typespec_word == cts_bool)
7334 error ("both %<short%> and %<_Bool%> in "
7335 "declaration specifiers");
7336 else if (specs->typespec_word == cts_char)
7337 error ("both %<short%> and %<char%> in "
7338 "declaration specifiers");
7339 else if (specs->typespec_word == cts_float)
7340 error ("both %<short%> and %<float%> in "
7341 "declaration specifiers");
7342 else if (specs->typespec_word == cts_double)
7343 error ("both %<short%> and %<double%> in "
7344 "declaration specifiers");
7345 else if (specs->typespec_word == cts_dfloat32)
7346 error ("both %<short%> and %<_Decimal32%> in "
7347 "declaration specifiers");
7348 else if (specs->typespec_word == cts_dfloat64)
7349 error ("both %<short%> and %<_Decimal64%> in "
7350 "declaration specifiers");
7351 else if (specs->typespec_word == cts_dfloat128)
7352 error ("both %<short%> and %<_Decimal128%> in "
7353 "declaration specifiers");
7354 else
7355 specs->short_p = true;
7356 break;
7357 case RID_SIGNED:
7358 dupe = specs->signed_p;
7359 if (specs->unsigned_p)
7360 error ("both %<signed%> and %<unsigned%> in "
7361 "declaration specifiers");
7362 else if (specs->typespec_word == cts_void)
7363 error ("both %<signed%> and %<void%> in "
7364 "declaration specifiers");
7365 else if (specs->typespec_word == cts_bool)
7366 error ("both %<signed%> and %<_Bool%> in "
7367 "declaration specifiers");
7368 else if (specs->typespec_word == cts_float)
7369 error ("both %<signed%> and %<float%> in "
7370 "declaration specifiers");
7371 else if (specs->typespec_word == cts_double)
7372 error ("both %<signed%> and %<double%> in "
7373 "declaration specifiers");
7374 else if (specs->typespec_word == cts_dfloat32)
7375 error ("both %<signed%> and %<_Decimal32%> in "
7376 "declaration specifiers");
7377 else if (specs->typespec_word == cts_dfloat64)
7378 error ("both %<signed%> and %<_Decimal64%> in "
7379 "declaration specifiers");
7380 else if (specs->typespec_word == cts_dfloat128)
7381 error ("both %<signed%> and %<_Decimal128%> in "
7382 "declaration specifiers");
7383 else
7384 specs->signed_p = true;
7385 break;
7386 case RID_UNSIGNED:
7387 dupe = specs->unsigned_p;
7388 if (specs->signed_p)
7389 error ("both %<signed%> and %<unsigned%> in "
7390 "declaration specifiers");
7391 else if (specs->typespec_word == cts_void)
7392 error ("both %<unsigned%> and %<void%> in "
7393 "declaration specifiers");
7394 else if (specs->typespec_word == cts_bool)
7395 error ("both %<unsigned%> and %<_Bool%> in "
7396 "declaration specifiers");
7397 else if (specs->typespec_word == cts_float)
7398 error ("both %<unsigned%> and %<float%> in "
7399 "declaration specifiers");
7400 else if (specs->typespec_word == cts_double)
7401 error ("both %<unsigned%> and %<double%> in "
7402 "declaration specifiers");
7403 else if (specs->typespec_word == cts_dfloat32)
7404 error ("both %<unsigned%> and %<_Decimal32%> in "
7405 "declaration specifiers");
7406 else if (specs->typespec_word == cts_dfloat64)
7407 error ("both %<unsigned%> and %<_Decimal64%> in "
7408 "declaration specifiers");
7409 else if (specs->typespec_word == cts_dfloat128)
7410 error ("both %<unsigned%> and %<_Decimal128%> in "
7411 "declaration specifiers");
7412 else
7413 specs->unsigned_p = true;
7414 break;
7415 case RID_COMPLEX:
7416 dupe = specs->complex_p;
7417 if (!flag_isoc99 && !in_system_header)
7418 pedwarn (input_location, OPT_pedantic, "ISO C90 does not support complex types");
7419 if (specs->typespec_word == cts_void)
7420 error ("both %<complex%> and %<void%> in "
7421 "declaration specifiers");
7422 else if (specs->typespec_word == cts_bool)
7423 error ("both %<complex%> and %<_Bool%> in "
7424 "declaration specifiers");
7425 else if (specs->typespec_word == cts_dfloat32)
7426 error ("both %<complex%> and %<_Decimal32%> in "
7427 "declaration specifiers");
7428 else if (specs->typespec_word == cts_dfloat64)
7429 error ("both %<complex%> and %<_Decimal64%> in "
7430 "declaration specifiers");
7431 else if (specs->typespec_word == cts_dfloat128)
7432 error ("both %<complex%> and %<_Decimal128%> in "
7433 "declaration specifiers");
7434 else if (specs->typespec_word == cts_fract)
7435 error ("both %<complex%> and %<_Fract%> in "
7436 "declaration specifiers");
7437 else if (specs->typespec_word == cts_accum)
7438 error ("both %<complex%> and %<_Accum%> in "
7439 "declaration specifiers");
7440 else if (specs->saturating_p)
7441 error ("both %<complex%> and %<_Sat%> in "
7442 "declaration specifiers");
7443 else
7444 specs->complex_p = true;
7445 break;
7446 case RID_SAT:
7447 dupe = specs->saturating_p;
7448 pedwarn (input_location, OPT_pedantic, "ISO C does not support saturating types");
7449 if (specs->typespec_word == cts_void)
7450 error ("both %<_Sat%> and %<void%> in "
7451 "declaration specifiers");
7452 else if (specs->typespec_word == cts_bool)
7453 error ("both %<_Sat%> and %<_Bool%> in "
7454 "declaration specifiers");
7455 else if (specs->typespec_word == cts_char)
7456 error ("both %<_Sat%> and %<char%> in "
7457 "declaration specifiers");
7458 else if (specs->typespec_word == cts_int)
7459 error ("both %<_Sat%> and %<int%> in "
7460 "declaration specifiers");
7461 else if (specs->typespec_word == cts_float)
7462 error ("both %<_Sat%> and %<float%> in "
7463 "declaration specifiers");
7464 else if (specs->typespec_word == cts_double)
7465 error ("both %<_Sat%> and %<double%> in "
7466 "declaration specifiers");
7467 else if (specs->typespec_word == cts_dfloat32)
7468 error ("both %<_Sat%> and %<_Decimal32%> in "
7469 "declaration specifiers");
7470 else if (specs->typespec_word == cts_dfloat64)
7471 error ("both %<_Sat%> and %<_Decimal64%> in "
7472 "declaration specifiers");
7473 else if (specs->typespec_word == cts_dfloat128)
7474 error ("both %<_Sat%> and %<_Decimal128%> in "
7475 "declaration specifiers");
7476 else if (specs->complex_p)
7477 error ("both %<_Sat%> and %<complex%> in "
7478 "declaration specifiers");
7479 else
7480 specs->saturating_p = true;
7481 break;
7482 default:
7483 gcc_unreachable ();
7486 if (dupe)
7487 error ("duplicate %qE", type);
7489 return specs;
7491 else
7493 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
7494 "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
7495 if (specs->typespec_word != cts_none)
7497 error ("two or more data types in declaration specifiers");
7498 return specs;
7500 switch (i)
7502 case RID_VOID:
7503 if (specs->long_p)
7504 error ("both %<long%> and %<void%> in "
7505 "declaration specifiers");
7506 else if (specs->short_p)
7507 error ("both %<short%> and %<void%> in "
7508 "declaration specifiers");
7509 else if (specs->signed_p)
7510 error ("both %<signed%> and %<void%> in "
7511 "declaration specifiers");
7512 else if (specs->unsigned_p)
7513 error ("both %<unsigned%> and %<void%> in "
7514 "declaration specifiers");
7515 else if (specs->complex_p)
7516 error ("both %<complex%> and %<void%> in "
7517 "declaration specifiers");
7518 else if (specs->saturating_p)
7519 error ("both %<_Sat%> and %<void%> in "
7520 "declaration specifiers");
7521 else
7522 specs->typespec_word = cts_void;
7523 return specs;
7524 case RID_BOOL:
7525 if (specs->long_p)
7526 error ("both %<long%> and %<_Bool%> in "
7527 "declaration specifiers");
7528 else if (specs->short_p)
7529 error ("both %<short%> and %<_Bool%> in "
7530 "declaration specifiers");
7531 else if (specs->signed_p)
7532 error ("both %<signed%> and %<_Bool%> in "
7533 "declaration specifiers");
7534 else if (specs->unsigned_p)
7535 error ("both %<unsigned%> and %<_Bool%> in "
7536 "declaration specifiers");
7537 else if (specs->complex_p)
7538 error ("both %<complex%> and %<_Bool%> in "
7539 "declaration specifiers");
7540 else if (specs->saturating_p)
7541 error ("both %<_Sat%> and %<_Bool%> in "
7542 "declaration specifiers");
7543 else
7544 specs->typespec_word = cts_bool;
7545 return specs;
7546 case RID_CHAR:
7547 if (specs->long_p)
7548 error ("both %<long%> and %<char%> in "
7549 "declaration specifiers");
7550 else if (specs->short_p)
7551 error ("both %<short%> and %<char%> in "
7552 "declaration specifiers");
7553 else if (specs->saturating_p)
7554 error ("both %<_Sat%> and %<char%> in "
7555 "declaration specifiers");
7556 else
7557 specs->typespec_word = cts_char;
7558 return specs;
7559 case RID_INT:
7560 if (specs->saturating_p)
7561 error ("both %<_Sat%> and %<int%> in "
7562 "declaration specifiers");
7563 else
7564 specs->typespec_word = cts_int;
7565 return specs;
7566 case RID_FLOAT:
7567 if (specs->long_p)
7568 error ("both %<long%> and %<float%> in "
7569 "declaration specifiers");
7570 else if (specs->short_p)
7571 error ("both %<short%> and %<float%> in "
7572 "declaration specifiers");
7573 else if (specs->signed_p)
7574 error ("both %<signed%> and %<float%> in "
7575 "declaration specifiers");
7576 else if (specs->unsigned_p)
7577 error ("both %<unsigned%> and %<float%> in "
7578 "declaration specifiers");
7579 else if (specs->saturating_p)
7580 error ("both %<_Sat%> and %<float%> in "
7581 "declaration specifiers");
7582 else
7583 specs->typespec_word = cts_float;
7584 return specs;
7585 case RID_DOUBLE:
7586 if (specs->long_long_p)
7587 error ("both %<long long%> and %<double%> in "
7588 "declaration specifiers");
7589 else if (specs->short_p)
7590 error ("both %<short%> and %<double%> in "
7591 "declaration specifiers");
7592 else if (specs->signed_p)
7593 error ("both %<signed%> and %<double%> in "
7594 "declaration specifiers");
7595 else if (specs->unsigned_p)
7596 error ("both %<unsigned%> and %<double%> in "
7597 "declaration specifiers");
7598 else if (specs->saturating_p)
7599 error ("both %<_Sat%> and %<double%> in "
7600 "declaration specifiers");
7601 else
7602 specs->typespec_word = cts_double;
7603 return specs;
7604 case RID_DFLOAT32:
7605 case RID_DFLOAT64:
7606 case RID_DFLOAT128:
7608 const char *str;
7609 if (i == RID_DFLOAT32)
7610 str = "_Decimal32";
7611 else if (i == RID_DFLOAT64)
7612 str = "_Decimal64";
7613 else
7614 str = "_Decimal128";
7615 if (specs->long_long_p)
7616 error ("both %<long long%> and %<%s%> in "
7617 "declaration specifiers", str);
7618 if (specs->long_p)
7619 error ("both %<long%> and %<%s%> in "
7620 "declaration specifiers", str);
7621 else if (specs->short_p)
7622 error ("both %<short%> and %<%s%> in "
7623 "declaration specifiers", str);
7624 else if (specs->signed_p)
7625 error ("both %<signed%> and %<%s%> in "
7626 "declaration specifiers", str);
7627 else if (specs->unsigned_p)
7628 error ("both %<unsigned%> and %<%s%> in "
7629 "declaration specifiers", str);
7630 else if (specs->complex_p)
7631 error ("both %<complex%> and %<%s%> in "
7632 "declaration specifiers", str);
7633 else if (specs->saturating_p)
7634 error ("both %<_Sat%> and %<%s%> in "
7635 "declaration specifiers", str);
7636 else if (i == RID_DFLOAT32)
7637 specs->typespec_word = cts_dfloat32;
7638 else if (i == RID_DFLOAT64)
7639 specs->typespec_word = cts_dfloat64;
7640 else
7641 specs->typespec_word = cts_dfloat128;
7643 if (!targetm.decimal_float_supported_p ())
7644 error ("decimal floating point not supported for this target");
7645 pedwarn (input_location, OPT_pedantic,
7646 "ISO C does not support decimal floating point");
7647 return specs;
7648 case RID_FRACT:
7649 case RID_ACCUM:
7651 const char *str;
7652 if (i == RID_FRACT)
7653 str = "_Fract";
7654 else
7655 str = "_Accum";
7656 if (specs->complex_p)
7657 error ("both %<complex%> and %<%s%> in "
7658 "declaration specifiers", str);
7659 else if (i == RID_FRACT)
7660 specs->typespec_word = cts_fract;
7661 else
7662 specs->typespec_word = cts_accum;
7664 if (!targetm.fixed_point_supported_p ())
7665 error ("fixed-point types not supported for this target");
7666 pedwarn (input_location, OPT_pedantic,
7667 "ISO C does not support fixed-point types");
7668 return specs;
7669 default:
7670 /* ObjC reserved word "id", handled below. */
7671 break;
7676 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7677 form of ObjC type, cases such as "int" and "long" being handled
7678 above), a TYPE (struct, union, enum and typeof specifiers) or an
7679 ERROR_MARK. In none of these cases may there have previously
7680 been any type specifiers. */
7681 if (specs->type || specs->typespec_word != cts_none
7682 || specs->long_p || specs->short_p || specs->signed_p
7683 || specs->unsigned_p || specs->complex_p)
7684 error ("two or more data types in declaration specifiers");
7685 else if (TREE_CODE (type) == TYPE_DECL)
7687 if (TREE_TYPE (type) == error_mark_node)
7688 ; /* Allow the type to default to int to avoid cascading errors. */
7689 else
7691 specs->type = TREE_TYPE (type);
7692 specs->decl_attr = DECL_ATTRIBUTES (type);
7693 specs->typedef_p = true;
7694 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7697 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7699 tree t = lookup_name (type);
7700 if (!t || TREE_CODE (t) != TYPE_DECL)
7701 error ("%qE fails to be a typedef or built in type", type);
7702 else if (TREE_TYPE (t) == error_mark_node)
7704 else
7705 specs->type = TREE_TYPE (t);
7707 else if (TREE_CODE (type) != ERROR_MARK)
7709 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7710 specs->tag_defined_p = true;
7711 if (spec.kind == ctsk_typeof)
7713 specs->typedef_p = true;
7714 if (spec.expr)
7716 if (specs->expr)
7717 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
7718 specs->expr, spec.expr);
7719 else
7720 specs->expr = spec.expr;
7721 specs->expr_const_operands &= spec.expr_const_operands;
7724 specs->type = type;
7727 return specs;
7730 /* Add the storage class specifier or function specifier SCSPEC to the
7731 declaration specifiers SPECS, returning SPECS. */
7733 struct c_declspecs *
7734 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7736 enum rid i;
7737 enum c_storage_class n = csc_none;
7738 bool dupe = false;
7739 specs->declspecs_seen_p = true;
7740 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7741 && C_IS_RESERVED_WORD (scspec));
7742 i = C_RID_CODE (scspec);
7743 if (specs->non_sc_seen_p)
7744 warning (OPT_Wold_style_declaration,
7745 "%qE is not at beginning of declaration", scspec);
7746 switch (i)
7748 case RID_INLINE:
7749 /* C99 permits duplicate inline. Although of doubtful utility,
7750 it seems simplest to permit it in gnu89 mode as well, as
7751 there is also little utility in maintaining this as a
7752 difference between gnu89 and C99 inline. */
7753 dupe = false;
7754 specs->inline_p = true;
7755 break;
7756 case RID_THREAD:
7757 dupe = specs->thread_p;
7758 if (specs->storage_class == csc_auto)
7759 error ("%<__thread%> used with %<auto%>");
7760 else if (specs->storage_class == csc_register)
7761 error ("%<__thread%> used with %<register%>");
7762 else if (specs->storage_class == csc_typedef)
7763 error ("%<__thread%> used with %<typedef%>");
7764 else
7765 specs->thread_p = true;
7766 break;
7767 case RID_AUTO:
7768 n = csc_auto;
7769 break;
7770 case RID_EXTERN:
7771 n = csc_extern;
7772 /* Diagnose "__thread extern". */
7773 if (specs->thread_p)
7774 error ("%<__thread%> before %<extern%>");
7775 break;
7776 case RID_REGISTER:
7777 n = csc_register;
7778 break;
7779 case RID_STATIC:
7780 n = csc_static;
7781 /* Diagnose "__thread static". */
7782 if (specs->thread_p)
7783 error ("%<__thread%> before %<static%>");
7784 break;
7785 case RID_TYPEDEF:
7786 n = csc_typedef;
7787 break;
7788 default:
7789 gcc_unreachable ();
7791 if (n != csc_none && n == specs->storage_class)
7792 dupe = true;
7793 if (dupe)
7794 error ("duplicate %qE", scspec);
7795 if (n != csc_none)
7797 if (specs->storage_class != csc_none && n != specs->storage_class)
7799 error ("multiple storage classes in declaration specifiers");
7801 else
7803 specs->storage_class = n;
7804 if (n != csc_extern && n != csc_static && specs->thread_p)
7806 error ("%<__thread%> used with %qE", scspec);
7807 specs->thread_p = false;
7811 return specs;
7814 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7815 returning SPECS. */
7817 struct c_declspecs *
7818 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7820 specs->attrs = chainon (attrs, specs->attrs);
7821 specs->declspecs_seen_p = true;
7822 return specs;
7825 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7826 specifiers with any other type specifier to determine the resulting
7827 type. This is where ISO C checks on complex types are made, since
7828 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7829 double". */
7831 struct c_declspecs *
7832 finish_declspecs (struct c_declspecs *specs)
7834 /* If a type was specified as a whole, we have no modifiers and are
7835 done. */
7836 if (specs->type != NULL_TREE)
7838 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7839 && !specs->signed_p && !specs->unsigned_p
7840 && !specs->complex_p);
7841 return specs;
7844 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7845 has been specified, treat it as "int" unless "_Complex" is
7846 present and there are no other specifiers. If we just have
7847 "_Complex", it is equivalent to "_Complex double", but e.g.
7848 "_Complex short" is equivalent to "_Complex short int". */
7849 if (specs->typespec_word == cts_none)
7851 if (specs->saturating_p)
7853 error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
7854 if (!targetm.fixed_point_supported_p ())
7855 error ("fixed-point types not supported for this target");
7856 specs->typespec_word = cts_fract;
7858 else if (specs->long_p || specs->short_p
7859 || specs->signed_p || specs->unsigned_p)
7861 specs->typespec_word = cts_int;
7863 else if (specs->complex_p)
7865 specs->typespec_word = cts_double;
7866 pedwarn (input_location, OPT_pedantic,
7867 "ISO C does not support plain %<complex%> meaning "
7868 "%<double complex%>");
7870 else
7872 specs->typespec_word = cts_int;
7873 specs->default_int_p = true;
7874 /* We don't diagnose this here because grokdeclarator will
7875 give more specific diagnostics according to whether it is
7876 a function definition. */
7880 /* If "signed" was specified, record this to distinguish "int" and
7881 "signed int" in the case of a bit-field with
7882 -funsigned-bitfields. */
7883 specs->explicit_signed_p = specs->signed_p;
7885 /* Now compute the actual type. */
7886 switch (specs->typespec_word)
7888 case cts_void:
7889 gcc_assert (!specs->long_p && !specs->short_p
7890 && !specs->signed_p && !specs->unsigned_p
7891 && !specs->complex_p);
7892 specs->type = void_type_node;
7893 break;
7894 case cts_bool:
7895 gcc_assert (!specs->long_p && !specs->short_p
7896 && !specs->signed_p && !specs->unsigned_p
7897 && !specs->complex_p);
7898 specs->type = boolean_type_node;
7899 break;
7900 case cts_char:
7901 gcc_assert (!specs->long_p && !specs->short_p);
7902 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7903 if (specs->signed_p)
7904 specs->type = signed_char_type_node;
7905 else if (specs->unsigned_p)
7906 specs->type = unsigned_char_type_node;
7907 else
7908 specs->type = char_type_node;
7909 if (specs->complex_p)
7911 pedwarn (input_location, OPT_pedantic,
7912 "ISO C does not support complex integer types");
7913 specs->type = build_complex_type (specs->type);
7915 break;
7916 case cts_int:
7917 gcc_assert (!(specs->long_p && specs->short_p));
7918 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7919 if (specs->long_long_p)
7920 specs->type = (specs->unsigned_p
7921 ? long_long_unsigned_type_node
7922 : long_long_integer_type_node);
7923 else if (specs->long_p)
7924 specs->type = (specs->unsigned_p
7925 ? long_unsigned_type_node
7926 : long_integer_type_node);
7927 else if (specs->short_p)
7928 specs->type = (specs->unsigned_p
7929 ? short_unsigned_type_node
7930 : short_integer_type_node);
7931 else
7932 specs->type = (specs->unsigned_p
7933 ? unsigned_type_node
7934 : integer_type_node);
7935 if (specs->complex_p)
7937 pedwarn (input_location, OPT_pedantic,
7938 "ISO C does not support complex integer types");
7939 specs->type = build_complex_type (specs->type);
7941 break;
7942 case cts_float:
7943 gcc_assert (!specs->long_p && !specs->short_p
7944 && !specs->signed_p && !specs->unsigned_p);
7945 specs->type = (specs->complex_p
7946 ? complex_float_type_node
7947 : float_type_node);
7948 break;
7949 case cts_double:
7950 gcc_assert (!specs->long_long_p && !specs->short_p
7951 && !specs->signed_p && !specs->unsigned_p);
7952 if (specs->long_p)
7954 specs->type = (specs->complex_p
7955 ? complex_long_double_type_node
7956 : long_double_type_node);
7958 else
7960 specs->type = (specs->complex_p
7961 ? complex_double_type_node
7962 : double_type_node);
7964 break;
7965 case cts_dfloat32:
7966 case cts_dfloat64:
7967 case cts_dfloat128:
7968 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7969 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
7970 if (specs->typespec_word == cts_dfloat32)
7971 specs->type = dfloat32_type_node;
7972 else if (specs->typespec_word == cts_dfloat64)
7973 specs->type = dfloat64_type_node;
7974 else
7975 specs->type = dfloat128_type_node;
7976 break;
7977 case cts_fract:
7978 gcc_assert (!specs->complex_p);
7979 if (!targetm.fixed_point_supported_p ())
7980 specs->type = integer_type_node;
7981 else if (specs->saturating_p)
7983 if (specs->long_long_p)
7984 specs->type = specs->unsigned_p
7985 ? sat_unsigned_long_long_fract_type_node
7986 : sat_long_long_fract_type_node;
7987 else if (specs->long_p)
7988 specs->type = specs->unsigned_p
7989 ? sat_unsigned_long_fract_type_node
7990 : sat_long_fract_type_node;
7991 else if (specs->short_p)
7992 specs->type = specs->unsigned_p
7993 ? sat_unsigned_short_fract_type_node
7994 : sat_short_fract_type_node;
7995 else
7996 specs->type = specs->unsigned_p
7997 ? sat_unsigned_fract_type_node
7998 : sat_fract_type_node;
8000 else
8002 if (specs->long_long_p)
8003 specs->type = specs->unsigned_p
8004 ? unsigned_long_long_fract_type_node
8005 : long_long_fract_type_node;
8006 else if (specs->long_p)
8007 specs->type = specs->unsigned_p
8008 ? unsigned_long_fract_type_node
8009 : long_fract_type_node;
8010 else if (specs->short_p)
8011 specs->type = specs->unsigned_p
8012 ? unsigned_short_fract_type_node
8013 : short_fract_type_node;
8014 else
8015 specs->type = specs->unsigned_p
8016 ? unsigned_fract_type_node
8017 : fract_type_node;
8019 break;
8020 case cts_accum:
8021 gcc_assert (!specs->complex_p);
8022 if (!targetm.fixed_point_supported_p ())
8023 specs->type = integer_type_node;
8024 else if (specs->saturating_p)
8026 if (specs->long_long_p)
8027 specs->type = specs->unsigned_p
8028 ? sat_unsigned_long_long_accum_type_node
8029 : sat_long_long_accum_type_node;
8030 else if (specs->long_p)
8031 specs->type = specs->unsigned_p
8032 ? sat_unsigned_long_accum_type_node
8033 : sat_long_accum_type_node;
8034 else if (specs->short_p)
8035 specs->type = specs->unsigned_p
8036 ? sat_unsigned_short_accum_type_node
8037 : sat_short_accum_type_node;
8038 else
8039 specs->type = specs->unsigned_p
8040 ? sat_unsigned_accum_type_node
8041 : sat_accum_type_node;
8043 else
8045 if (specs->long_long_p)
8046 specs->type = specs->unsigned_p
8047 ? unsigned_long_long_accum_type_node
8048 : long_long_accum_type_node;
8049 else if (specs->long_p)
8050 specs->type = specs->unsigned_p
8051 ? unsigned_long_accum_type_node
8052 : long_accum_type_node;
8053 else if (specs->short_p)
8054 specs->type = specs->unsigned_p
8055 ? unsigned_short_accum_type_node
8056 : short_accum_type_node;
8057 else
8058 specs->type = specs->unsigned_p
8059 ? unsigned_accum_type_node
8060 : accum_type_node;
8062 break;
8063 default:
8064 gcc_unreachable ();
8067 return specs;
8070 /* A subroutine of c_write_global_declarations. Perform final processing
8071 on one file scope's declarations (or the external scope's declarations),
8072 GLOBALS. */
8074 static void
8075 c_write_global_declarations_1 (tree globals)
8077 tree decl;
8078 bool reconsider;
8080 /* Process the decls in the order they were written. */
8081 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8083 /* Check for used but undefined static functions using the C
8084 standard's definition of "used", and set TREE_NO_WARNING so
8085 that check_global_declarations doesn't repeat the check. */
8086 if (TREE_CODE (decl) == FUNCTION_DECL
8087 && DECL_INITIAL (decl) == 0
8088 && DECL_EXTERNAL (decl)
8089 && !TREE_PUBLIC (decl)
8090 && C_DECL_USED (decl))
8092 pedwarn (input_location, 0, "%q+F used but never defined", decl);
8093 TREE_NO_WARNING (decl) = 1;
8096 wrapup_global_declaration_1 (decl);
8101 reconsider = false;
8102 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8103 reconsider |= wrapup_global_declaration_2 (decl);
8105 while (reconsider);
8107 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8108 check_global_declaration_1 (decl);
8111 /* A subroutine of c_write_global_declarations Emit debug information for each
8112 of the declarations in GLOBALS. */
8114 static void
8115 c_write_global_declarations_2 (tree globals)
8117 tree decl;
8119 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
8120 debug_hooks->global_decl (decl);
8123 /* Preserve the external declarations scope across a garbage collect. */
8124 static GTY(()) tree ext_block;
8126 void
8127 c_write_global_declarations (void)
8129 tree t;
8131 /* We don't want to do this if generating a PCH. */
8132 if (pch_file)
8133 return;
8135 /* Don't waste time on further processing if -fsyntax-only or we've
8136 encountered errors. */
8137 if (flag_syntax_only || errorcount || sorrycount)
8138 return;
8140 /* Close the external scope. */
8141 ext_block = pop_scope ();
8142 external_scope = 0;
8143 gcc_assert (!current_scope);
8145 if (ext_block)
8147 tree tmp = BLOCK_VARS (ext_block);
8148 int flags;
8149 FILE * stream = dump_begin (TDI_tu, &flags);
8150 if (stream && tmp)
8152 dump_node (tmp, flags & ~TDF_SLIM, stream);
8153 dump_end (TDI_tu, stream);
8157 /* Process all file scopes in this compilation, and the external_scope,
8158 through wrapup_global_declarations and check_global_declarations. */
8159 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8160 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
8161 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
8163 /* We're done parsing; proceed to optimize and emit assembly.
8164 FIXME: shouldn't be the front end's responsibility to call this. */
8165 cgraph_optimize ();
8167 /* After cgraph has had a chance to emit everything that's going to
8168 be emitted, output debug information for globals. */
8169 if (errorcount == 0 && sorrycount == 0)
8171 timevar_push (TV_SYMOUT);
8172 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8173 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
8174 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
8175 timevar_pop (TV_SYMOUT);
8178 ext_block = NULL;
8181 #include "gt-c-decl.h"