Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gcc4 / gcc / c-decl.c
blobeb69acace4a0bdb8c4e286362ebc24c39ada95ff
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 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
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 "tree-gimple.h"
56 #include "diagnostic.h"
57 #include "tree-dump.h"
58 #include "cgraph.h"
59 #include "hashtab.h"
60 #include "libfuncs.h"
61 #include "except.h"
62 #include "langhooks-def.h"
63 #include "pointer-set.h"
65 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
66 enum decl_context
67 { NORMAL, /* Ordinary declaration */
68 FUNCDEF, /* Function definition */
69 PARM, /* Declaration of parm before function body */
70 FIELD, /* Declaration inside struct or union */
71 TYPENAME}; /* Typename (inside cast or sizeof) */
74 /* Nonzero if we have seen an invalid cross reference
75 to a struct, union, or enum, but not yet printed the message. */
76 tree pending_invalid_xref;
78 /* File and line to appear in the eventual error message. */
79 location_t pending_invalid_xref_location;
81 /* True means we've initialized exception handling. */
82 bool c_eh_initialized_p;
84 /* While defining an enum type, this is 1 plus the last enumerator
85 constant value. Note that will do not have to save this or `enum_overflow'
86 around nested function definition since such a definition could only
87 occur in an enum value expression and we don't use these variables in
88 that case. */
90 static tree enum_next_value;
92 /* Nonzero means that there was overflow computing enum_next_value. */
94 static int enum_overflow;
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 starting a function declared `extern inline'. */
159 static int current_extern_inline;
161 /* Nonzero when the current toplevel function contains a declaration
162 of a nested function which is never defined. */
164 static bool undef_nested_function;
166 /* True means global_bindings_p should return false even if the scope stack
167 says we are in file scope. */
168 bool c_override_global_bindings_to_false;
171 /* Each c_binding structure describes one binding of an identifier to
172 a decl. All the decls in a scope - irrespective of namespace - are
173 chained together by the ->prev field, which (as the name implies)
174 runs in reverse order. All the decls in a given namespace bound to
175 a given identifier are chained by the ->shadowed field, which runs
176 from inner to outer scopes.
178 The ->decl field usually points to a DECL node, but there are two
179 exceptions. In the namespace of type tags, the bound entity is a
180 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
181 identifier is encountered, it is bound to error_mark_node to
182 suppress further errors about that identifier in the current
183 function.
185 The ->type field stores the type of the declaration in this scope;
186 if NULL, the type is the type of the ->decl field. This is only of
187 relevance for objects with external or internal linkage which may
188 be redeclared in inner scopes, forming composite types that only
189 persist for the duration of those scopes. In the external scope,
190 this stores the composite of all the types declared for this
191 object, visible or not. The ->inner_comp field (used only at file
192 scope) stores whether an incomplete array type at file scope was
193 completed at an inner scope to an array size other than 1.
195 The depth field is copied from the scope structure that holds this
196 decl. It is used to preserve the proper ordering of the ->shadowed
197 field (see bind()) and also for a handful of special-case checks.
198 Finally, the invisible bit is true for a decl which should be
199 ignored for purposes of normal name lookup, and the nested bit is
200 true for a decl that's been bound a second time in an inner scope;
201 in all such cases, the binding in the outer scope will have its
202 invisible bit true. */
204 struct c_binding GTY((chain_next ("%h.prev")))
206 tree decl; /* the decl bound */
207 tree type; /* the type in this scope */
208 tree id; /* the identifier it's bound to */
209 struct c_binding *prev; /* the previous decl in this scope */
210 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
211 unsigned int depth : 28; /* depth of this scope */
212 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
213 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
214 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
215 /* one free bit */
217 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
218 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
219 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
220 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
222 #define I_SYMBOL_BINDING(node) \
223 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
224 #define I_SYMBOL_DECL(node) \
225 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
227 #define I_TAG_BINDING(node) \
228 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
229 #define I_TAG_DECL(node) \
230 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
232 #define I_LABEL_BINDING(node) \
233 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
234 #define I_LABEL_DECL(node) \
235 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
237 /* Each C symbol points to three linked lists of c_binding structures.
238 These describe the values of the identifier in the three different
239 namespaces defined by the language. */
241 struct lang_identifier GTY(())
243 struct c_common_identifier common_id;
244 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
245 struct c_binding *tag_binding; /* struct/union/enum tags */
246 struct c_binding *label_binding; /* labels */
249 /* Validate c-lang.c's assumptions. */
250 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
251 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
253 /* The resulting tree type. */
255 union lang_tree_node
256 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
257 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)")))
259 union tree_node GTY ((tag ("0"),
260 desc ("tree_node_structure (&%h)")))
261 generic;
262 struct lang_identifier GTY ((tag ("1"))) identifier;
265 /* Each c_scope structure describes the complete contents of one
266 scope. Four scopes are distinguished specially: the innermost or
267 current scope, the innermost function scope, the file scope (always
268 the second to outermost) and the outermost or external scope.
270 Most declarations are recorded in the current scope.
272 All normal label declarations are recorded in the innermost
273 function scope, as are bindings of undeclared identifiers to
274 error_mark_node. (GCC permits nested functions as an extension,
275 hence the 'innermost' qualifier.) Explicitly declared labels
276 (using the __label__ extension) appear in the current scope.
278 Being in the file scope (current_scope == file_scope) causes
279 special behavior in several places below. Also, under some
280 conditions the Objective-C front end records declarations in the
281 file scope even though that isn't the current scope.
283 All declarations with external linkage are recorded in the external
284 scope, even if they aren't visible there; this models the fact that
285 such declarations are visible to the entire program, and (with a
286 bit of cleverness, see pushdecl) allows diagnosis of some violations
287 of C99 6.2.2p7 and 6.2.7p2:
289 If, within the same translation unit, the same identifier appears
290 with both internal and external linkage, the behavior is
291 undefined.
293 All declarations that refer to the same object or function shall
294 have compatible type; otherwise, the behavior is undefined.
296 Initially only the built-in declarations, which describe compiler
297 intrinsic functions plus a subset of the standard library, are in
298 this scope.
300 The order of the blocks list matters, and it is frequently appended
301 to. To avoid having to walk all the way to the end of the list on
302 each insertion, or reverse the list later, we maintain a pointer to
303 the last list entry. (FIXME: It should be feasible to use a reversed
304 list here.)
306 The bindings list is strictly in reverse order of declarations;
307 pop_scope relies on this. */
310 struct c_scope GTY((chain_next ("%h.outer")))
312 /* The scope containing this one. */
313 struct c_scope *outer;
315 /* The next outermost function scope. */
316 struct c_scope *outer_function;
318 /* All bindings in this scope. */
319 struct c_binding *bindings;
321 /* For each scope (except the global one), a chain of BLOCK nodes
322 for all the scopes that were entered and exited one level down. */
323 tree blocks;
324 tree blocks_last;
326 /* The depth of this scope. Used to keep the ->shadowed chain of
327 bindings sorted innermost to outermost. */
328 unsigned int depth : 28;
330 /* True if we are currently filling this scope with parameter
331 declarations. */
332 BOOL_BITFIELD parm_flag : 1;
334 /* True if we already complained about forward parameter decls
335 in this scope. This prevents double warnings on
336 foo (int a; int b; ...) */
337 BOOL_BITFIELD warned_forward_parm_decls : 1;
339 /* True if this is the outermost block scope of a function body.
340 This scope contains the parameters, the local variables declared
341 in the outermost block, and all the labels (except those in
342 nested functions, or declared at block scope with __label__). */
343 BOOL_BITFIELD function_body : 1;
345 /* True means make a BLOCK for this scope no matter what. */
346 BOOL_BITFIELD keep : 1;
349 /* The scope currently in effect. */
351 static GTY(()) struct c_scope *current_scope;
353 /* The innermost function scope. Ordinary (not explicitly declared)
354 labels, bindings to error_mark_node, and the lazily-created
355 bindings of __func__ and its friends get this scope. */
357 static GTY(()) struct c_scope *current_function_scope;
359 /* The C file scope. This is reset for each input translation unit. */
361 static GTY(()) struct c_scope *file_scope;
363 /* The outermost scope. This is used for all declarations with
364 external linkage, and only these, hence the name. */
366 static GTY(()) struct c_scope *external_scope;
368 /* A chain of c_scope structures awaiting reuse. */
370 static GTY((deletable)) struct c_scope *scope_freelist;
372 /* A chain of c_binding structures awaiting reuse. */
374 static GTY((deletable)) struct c_binding *binding_freelist;
376 /* Append VAR to LIST in scope SCOPE. */
377 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
378 struct c_scope *s_ = (scope); \
379 tree d_ = (decl); \
380 if (s_->list##_last) \
381 TREE_CHAIN (s_->list##_last) = d_; \
382 else \
383 s_->list = d_; \
384 s_->list##_last = d_; \
385 } while (0)
387 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
388 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
389 struct c_scope *t_ = (tscope); \
390 struct c_scope *f_ = (fscope); \
391 if (t_->to##_last) \
392 TREE_CHAIN (t_->to##_last) = f_->from; \
393 else \
394 t_->to = f_->from; \
395 t_->to##_last = f_->from##_last; \
396 } while (0)
398 /* True means unconditionally make a BLOCK for the next scope pushed. */
400 static bool keep_next_level_flag;
402 /* True means the next call to push_scope will be the outermost scope
403 of a function body, so do not push a new scope, merely cease
404 expecting parameter decls. */
406 static bool next_is_function_body;
408 /* Functions called automatically at the beginning and end of execution. */
410 static GTY(()) tree static_ctors;
411 static GTY(()) tree static_dtors;
413 /* Forward declarations. */
414 static tree lookup_name_in_scope (tree, struct c_scope *);
415 static tree c_make_fname_decl (tree, int);
416 static tree grokdeclarator (const struct c_declarator *,
417 struct c_declspecs *,
418 enum decl_context, bool, tree *);
419 static tree grokparms (struct c_arg_info *, bool);
420 static void layout_array_type (tree);
422 /* T is a statement. Add it to the statement-tree. This is the
423 C/ObjC version--C++ has a slightly different version of this
424 function. */
426 tree
427 add_stmt (tree t)
429 enum tree_code code = TREE_CODE (t);
431 if (EXPR_P (t) && code != LABEL_EXPR)
433 if (!EXPR_HAS_LOCATION (t))
434 SET_EXPR_LOCATION (t, input_location);
437 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
438 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
440 /* Add T to the statement-tree. Non-side-effect statements need to be
441 recorded during statement expressions. */
442 append_to_statement_list_force (t, &cur_stmt_list);
444 return t;
447 /* States indicating how grokdeclarator() should handle declspecs marked
448 with __attribute__((deprecated)). An object declared as
449 __attribute__((deprecated)) suppresses warnings of uses of other
450 deprecated items. */
452 enum deprecated_states {
453 DEPRECATED_NORMAL,
454 DEPRECATED_SUPPRESS
457 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
459 void
460 c_print_identifier (FILE *file, tree node, int indent)
462 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
463 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
464 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
465 if (C_IS_RESERVED_WORD (node))
467 tree rid = ridpointers[C_RID_CODE (node)];
468 indent_to (file, indent + 4);
469 fprintf (file, "rid %p \"%s\"",
470 (void *) rid, IDENTIFIER_POINTER (rid));
474 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
475 which may be any of several kinds of DECL or TYPE or error_mark_node,
476 in the scope SCOPE. */
477 static void
478 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
480 struct c_binding *b, **here;
482 if (binding_freelist)
484 b = binding_freelist;
485 binding_freelist = b->prev;
487 else
488 b = GGC_NEW (struct c_binding);
490 b->shadowed = 0;
491 b->decl = decl;
492 b->id = name;
493 b->depth = scope->depth;
494 b->invisible = invisible;
495 b->nested = nested;
496 b->inner_comp = 0;
498 b->type = 0;
500 b->prev = scope->bindings;
501 scope->bindings = b;
503 if (!name)
504 return;
506 switch (TREE_CODE (decl))
508 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
509 case ENUMERAL_TYPE:
510 case UNION_TYPE:
511 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
512 case VAR_DECL:
513 case FUNCTION_DECL:
514 case TYPE_DECL:
515 case CONST_DECL:
516 case PARM_DECL:
517 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
519 default:
520 gcc_unreachable ();
523 /* Locate the appropriate place in the chain of shadowed decls
524 to insert this binding. Normally, scope == current_scope and
525 this does nothing. */
526 while (*here && (*here)->depth > scope->depth)
527 here = &(*here)->shadowed;
529 b->shadowed = *here;
530 *here = b;
533 /* Clear the binding structure B, stick it on the binding_freelist,
534 and return the former value of b->prev. This is used by pop_scope
535 and get_parm_info to iterate destructively over all the bindings
536 from a given scope. */
537 static struct c_binding *
538 free_binding_and_advance (struct c_binding *b)
540 struct c_binding *prev = b->prev;
542 memset (b, 0, sizeof (struct c_binding));
543 b->prev = binding_freelist;
544 binding_freelist = b;
546 return prev;
550 /* Hook called at end of compilation to assume 1 elt
551 for a file-scope tentative array defn that wasn't complete before. */
553 void
554 c_finish_incomplete_decl (tree decl)
556 if (TREE_CODE (decl) == VAR_DECL)
558 tree type = TREE_TYPE (decl);
559 if (type != error_mark_node
560 && TREE_CODE (type) == ARRAY_TYPE
561 && !DECL_EXTERNAL (decl)
562 && TYPE_DOMAIN (type) == 0)
564 warning (0, "array %q+D assumed to have one element", decl);
566 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
568 layout_decl (decl, 0);
573 /* The Objective-C front-end often needs to determine the current scope. */
575 void *
576 objc_get_current_scope (void)
578 return current_scope;
581 /* The following function is used only by Objective-C. It needs to live here
582 because it accesses the innards of c_scope. */
584 void
585 objc_mark_locals_volatile (void *enclosing_blk)
587 struct c_scope *scope;
588 struct c_binding *b;
590 for (scope = current_scope;
591 scope && scope != enclosing_blk;
592 scope = scope->outer)
594 for (b = scope->bindings; b; b = b->prev)
595 objc_volatilize_decl (b->decl);
597 /* Do not climb up past the current function. */
598 if (scope->function_body)
599 break;
603 /* Nonzero if we are currently in file scope. */
606 global_bindings_p (void)
608 return current_scope == file_scope && !c_override_global_bindings_to_false;
611 void
612 keep_next_level (void)
614 keep_next_level_flag = true;
617 /* Identify this scope as currently being filled with parameters. */
619 void
620 declare_parm_level (void)
622 current_scope->parm_flag = true;
625 void
626 push_scope (void)
628 if (next_is_function_body)
630 /* This is the transition from the parameters to the top level
631 of the function body. These are the same scope
632 (C99 6.2.1p4,6) so we do not push another scope structure.
633 next_is_function_body is set only by store_parm_decls, which
634 in turn is called when and only when we are about to
635 encounter the opening curly brace for the function body.
637 The outermost block of a function always gets a BLOCK node,
638 because the debugging output routines expect that each
639 function has at least one BLOCK. */
640 current_scope->parm_flag = false;
641 current_scope->function_body = true;
642 current_scope->keep = true;
643 current_scope->outer_function = current_function_scope;
644 current_function_scope = current_scope;
646 keep_next_level_flag = false;
647 next_is_function_body = false;
649 else
651 struct c_scope *scope;
652 if (scope_freelist)
654 scope = scope_freelist;
655 scope_freelist = scope->outer;
657 else
658 scope = GGC_CNEW (struct c_scope);
660 scope->keep = keep_next_level_flag;
661 scope->outer = current_scope;
662 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
664 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
665 possible. */
666 if (current_scope && scope->depth == 0)
668 scope->depth--;
669 sorry ("GCC supports only %u nested scopes", scope->depth);
672 current_scope = scope;
673 keep_next_level_flag = false;
677 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
679 static void
680 set_type_context (tree type, tree context)
682 for (type = TYPE_MAIN_VARIANT (type); type;
683 type = TYPE_NEXT_VARIANT (type))
684 TYPE_CONTEXT (type) = context;
687 /* Exit a scope. Restore the state of the identifier-decl mappings
688 that were in effect when this scope was entered. Return a BLOCK
689 node containing all the DECLs in this scope that are of interest
690 to debug info generation. */
692 tree
693 pop_scope (void)
695 struct c_scope *scope = current_scope;
696 tree block, context, p;
697 struct c_binding *b;
699 bool functionbody = scope->function_body;
700 bool keep = functionbody || scope->keep || scope->bindings;
702 c_end_vm_scope (scope->depth);
704 /* If appropriate, create a BLOCK to record the decls for the life
705 of this function. */
706 block = 0;
707 if (keep)
709 block = make_node (BLOCK);
710 BLOCK_SUBBLOCKS (block) = scope->blocks;
711 TREE_USED (block) = 1;
713 /* In each subblock, record that this is its superior. */
714 for (p = scope->blocks; p; p = TREE_CHAIN (p))
715 BLOCK_SUPERCONTEXT (p) = block;
717 BLOCK_VARS (block) = 0;
720 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
721 scope must be set so that they point to the appropriate
722 construct, i.e. either to the current FUNCTION_DECL node, or
723 else to the BLOCK node we just constructed.
725 Note that for tagged types whose scope is just the formal
726 parameter list for some function type specification, we can't
727 properly set their TYPE_CONTEXTs here, because we don't have a
728 pointer to the appropriate FUNCTION_TYPE node readily available
729 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
730 type nodes get set in `grokdeclarator' as soon as we have created
731 the FUNCTION_TYPE node which will represent the "scope" for these
732 "parameter list local" tagged types. */
733 if (scope->function_body)
734 context = current_function_decl;
735 else if (scope == file_scope)
737 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
738 TREE_CHAIN (file_decl) = all_translation_units;
739 all_translation_units = file_decl;
740 context = file_decl;
742 else
743 context = block;
745 /* Clear all bindings in this scope. */
746 for (b = scope->bindings; b; b = free_binding_and_advance (b))
748 p = b->decl;
749 switch (TREE_CODE (p))
751 case LABEL_DECL:
752 /* Warnings for unused labels, errors for undefined labels. */
753 if (TREE_USED (p) && !DECL_INITIAL (p))
755 error ("label %q+D used but not defined", p);
756 DECL_INITIAL (p) = error_mark_node;
758 else if (!TREE_USED (p) && warn_unused_label)
760 if (DECL_INITIAL (p))
761 warning (0, "label %q+D defined but not used", p);
762 else
763 warning (0, "label %q+D declared but not defined", p);
765 /* Labels go in BLOCK_VARS. */
766 TREE_CHAIN (p) = BLOCK_VARS (block);
767 BLOCK_VARS (block) = p;
768 gcc_assert (I_LABEL_BINDING (b->id) == b);
769 I_LABEL_BINDING (b->id) = b->shadowed;
770 break;
772 case ENUMERAL_TYPE:
773 case UNION_TYPE:
774 case RECORD_TYPE:
775 set_type_context (p, context);
777 /* Types may not have tag-names, in which case the type
778 appears in the bindings list with b->id NULL. */
779 if (b->id)
781 gcc_assert (I_TAG_BINDING (b->id) == b);
782 I_TAG_BINDING (b->id) = b->shadowed;
784 break;
786 case FUNCTION_DECL:
787 /* Propagate TREE_ADDRESSABLE from nested functions to their
788 containing functions. */
789 if (!TREE_ASM_WRITTEN (p)
790 && DECL_INITIAL (p) != 0
791 && TREE_ADDRESSABLE (p)
792 && DECL_ABSTRACT_ORIGIN (p) != 0
793 && DECL_ABSTRACT_ORIGIN (p) != p)
794 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
795 if (!DECL_EXTERNAL (p)
796 && DECL_INITIAL (p) == 0)
798 error ("nested function %q+D declared but never defined", p);
799 undef_nested_function = true;
801 goto common_symbol;
803 case VAR_DECL:
804 /* Warnings for unused variables. */
805 if (!TREE_USED (p)
806 && !TREE_NO_WARNING (p)
807 && !DECL_IN_SYSTEM_HEADER (p)
808 && DECL_NAME (p)
809 && !DECL_ARTIFICIAL (p)
810 && scope != file_scope
811 && scope != external_scope)
812 warning (OPT_Wunused_variable, "unused variable %q+D", p);
814 if (b->inner_comp)
816 error ("type of array %q+D completed incompatibly with"
817 " implicit initialization", p);
820 /* Fall through. */
821 case TYPE_DECL:
822 case CONST_DECL:
823 common_symbol:
824 /* All of these go in BLOCK_VARS, but only if this is the
825 binding in the home scope. */
826 if (!b->nested)
828 TREE_CHAIN (p) = BLOCK_VARS (block);
829 BLOCK_VARS (block) = p;
831 /* If this is the file scope, and we are processing more
832 than one translation unit in this compilation, set
833 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
834 This makes same_translation_unit_p work, and causes
835 static declarations to be given disambiguating suffixes. */
836 if (scope == file_scope && num_in_fnames > 1)
838 DECL_CONTEXT (p) = context;
839 if (TREE_CODE (p) == TYPE_DECL)
840 set_type_context (TREE_TYPE (p), context);
843 /* Fall through. */
844 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
845 already been put there by store_parm_decls. Unused-
846 parameter warnings are handled by function.c.
847 error_mark_node obviously does not go in BLOCK_VARS and
848 does not get unused-variable warnings. */
849 case PARM_DECL:
850 case ERROR_MARK:
851 /* It is possible for a decl not to have a name. We get
852 here with b->id NULL in this case. */
853 if (b->id)
855 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
856 I_SYMBOL_BINDING (b->id) = b->shadowed;
857 if (b->shadowed && b->shadowed->type)
858 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
860 break;
862 default:
863 gcc_unreachable ();
868 /* Dispose of the block that we just made inside some higher level. */
869 if ((scope->function_body || scope == file_scope) && context)
871 DECL_INITIAL (context) = block;
872 BLOCK_SUPERCONTEXT (block) = context;
874 else if (scope->outer)
876 if (block)
877 SCOPE_LIST_APPEND (scope->outer, blocks, block);
878 /* If we did not make a block for the scope just exited, any
879 blocks made for inner scopes must be carried forward so they
880 will later become subblocks of something else. */
881 else if (scope->blocks)
882 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
885 /* Pop the current scope, and free the structure for reuse. */
886 current_scope = scope->outer;
887 if (scope->function_body)
888 current_function_scope = scope->outer_function;
890 memset (scope, 0, sizeof (struct c_scope));
891 scope->outer = scope_freelist;
892 scope_freelist = scope;
894 return block;
897 void
898 push_file_scope (void)
900 tree decl;
902 if (file_scope)
903 return;
905 push_scope ();
906 file_scope = current_scope;
908 start_fname_decls ();
910 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
911 bind (DECL_NAME (decl), decl, file_scope,
912 /*invisible=*/false, /*nested=*/true);
915 void
916 pop_file_scope (void)
918 /* In case there were missing closebraces, get us back to the global
919 binding level. */
920 while (current_scope != file_scope)
921 pop_scope ();
923 /* __FUNCTION__ is defined at file scope (""). This
924 call may not be necessary as my tests indicate it
925 still works without it. */
926 finish_fname_decls ();
928 /* This is the point to write out a PCH if we're doing that.
929 In that case we do not want to do anything else. */
930 if (pch_file)
932 c_common_write_pch ();
933 return;
936 /* Pop off the file scope and close this translation unit. */
937 pop_scope ();
938 file_scope = 0;
940 maybe_apply_pending_pragma_weaks ();
941 cgraph_finalize_compilation_unit ();
944 /* Insert BLOCK at the end of the list of subblocks of the current
945 scope. This is used when a BIND_EXPR is expanded, to handle the
946 BLOCK node inside the BIND_EXPR. */
948 void
949 insert_block (tree block)
951 TREE_USED (block) = 1;
952 SCOPE_LIST_APPEND (current_scope, blocks, block);
955 /* Push a definition or a declaration of struct, union or enum tag "name".
956 "type" should be the type node.
957 We assume that the tag "name" is not already defined.
959 Note that the definition may really be just a forward reference.
960 In that case, the TYPE_SIZE will be zero. */
962 static void
963 pushtag (tree name, tree type)
965 /* Record the identifier as the type's name if it has none. */
966 if (name && !TYPE_NAME (type))
967 TYPE_NAME (type) = name;
968 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
970 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
971 tagged type we just added to the current scope. This fake
972 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
973 to output a representation of a tagged type, and it also gives
974 us a convenient place to record the "scope start" address for the
975 tagged type. */
977 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
979 /* An approximation for now, so we can tell this is a function-scope tag.
980 This will be updated in pop_scope. */
981 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
984 /* Subroutine of compare_decls. Allow harmless mismatches in return
985 and argument types provided that the type modes match. This function
986 return a unified type given a suitable match, and 0 otherwise. */
988 static tree
989 match_builtin_function_types (tree newtype, tree oldtype)
991 tree newrettype, oldrettype;
992 tree newargs, oldargs;
993 tree trytype, tryargs;
995 /* Accept the return type of the new declaration if same modes. */
996 oldrettype = TREE_TYPE (oldtype);
997 newrettype = TREE_TYPE (newtype);
999 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1000 return 0;
1002 oldargs = TYPE_ARG_TYPES (oldtype);
1003 newargs = TYPE_ARG_TYPES (newtype);
1004 tryargs = newargs;
1006 while (oldargs || newargs)
1008 if (!oldargs
1009 || !newargs
1010 || !TREE_VALUE (oldargs)
1011 || !TREE_VALUE (newargs)
1012 || TYPE_MODE (TREE_VALUE (oldargs))
1013 != TYPE_MODE (TREE_VALUE (newargs)))
1014 return 0;
1016 oldargs = TREE_CHAIN (oldargs);
1017 newargs = TREE_CHAIN (newargs);
1020 trytype = build_function_type (newrettype, tryargs);
1021 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1024 /* Subroutine of diagnose_mismatched_decls. Check for function type
1025 mismatch involving an empty arglist vs a nonempty one and give clearer
1026 diagnostics. */
1027 static void
1028 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1029 tree newtype, tree oldtype)
1031 tree t;
1033 if (TREE_CODE (olddecl) != FUNCTION_DECL
1034 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1035 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1037 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1038 return;
1040 t = TYPE_ARG_TYPES (oldtype);
1041 if (t == 0)
1042 t = TYPE_ARG_TYPES (newtype);
1043 for (; t; t = TREE_CHAIN (t))
1045 tree type = TREE_VALUE (t);
1047 if (TREE_CHAIN (t) == 0
1048 && TYPE_MAIN_VARIANT (type) != void_type_node)
1050 inform ("a parameter list with an ellipsis can%'t match "
1051 "an empty parameter name list declaration");
1052 break;
1055 if (c_type_promotes_to (type) != type)
1057 inform ("an argument type that has a default promotion can%'t match "
1058 "an empty parameter name list declaration");
1059 break;
1064 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1065 old-style function definition, NEWDECL is a prototype declaration.
1066 Diagnose inconsistencies in the argument list. Returns TRUE if
1067 the prototype is compatible, FALSE if not. */
1068 static bool
1069 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1071 tree newargs, oldargs;
1072 int i;
1074 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1076 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1077 newargs = TYPE_ARG_TYPES (newtype);
1078 i = 1;
1080 for (;;)
1082 tree oldargtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1083 tree newargtype = TYPE_MAIN_VARIANT (TREE_VALUE (newargs));
1085 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1086 break;
1088 /* Reaching the end of just one list means the two decls don't
1089 agree on the number of arguments. */
1090 if (END_OF_ARGLIST (oldargtype))
1092 error ("prototype for %q+D declares more arguments "
1093 "than previous old-style definition", newdecl);
1094 return false;
1096 else if (END_OF_ARGLIST (newargtype))
1098 error ("prototype for %q+D declares fewer arguments "
1099 "than previous old-style definition", newdecl);
1100 return false;
1103 /* Type for passing arg must be consistent with that declared
1104 for the arg. */
1105 else if (!comptypes (oldargtype, newargtype))
1107 error ("prototype for %q+D declares argument %d"
1108 " with incompatible type",
1109 newdecl, i);
1110 return false;
1113 oldargs = TREE_CHAIN (oldargs);
1114 newargs = TREE_CHAIN (newargs);
1115 i++;
1118 /* If we get here, no errors were found, but do issue a warning
1119 for this poor-style construct. */
1120 warning (0, "prototype for %q+D follows non-prototype definition",
1121 newdecl);
1122 return true;
1123 #undef END_OF_ARGLIST
1126 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1127 first in a pair of mismatched declarations, using the diagnostic
1128 function DIAG. */
1129 static void
1130 locate_old_decl (tree decl, void (*diag)(const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2))
1132 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1134 else if (DECL_INITIAL (decl))
1135 diag (G_("previous definition of %q+D was here"), decl);
1136 else if (C_DECL_IMPLICIT (decl))
1137 diag (G_("previous implicit declaration of %q+D was here"), decl);
1138 else
1139 diag (G_("previous declaration of %q+D was here"), decl);
1142 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1143 Returns true if the caller should proceed to merge the two, false
1144 if OLDDECL should simply be discarded. As a side effect, issues
1145 all necessary diagnostics for invalid or poor-style combinations.
1146 If it returns true, writes the types of NEWDECL and OLDDECL to
1147 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1148 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1150 static bool
1151 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1152 tree *newtypep, tree *oldtypep)
1154 tree newtype, oldtype;
1155 bool pedwarned = false;
1156 bool warned = false;
1157 bool retval = true;
1159 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1160 && DECL_EXTERNAL (DECL))
1162 /* If we have error_mark_node for either decl or type, just discard
1163 the previous decl - we're in an error cascade already. */
1164 if (olddecl == error_mark_node || newdecl == error_mark_node)
1165 return false;
1166 *oldtypep = oldtype = TREE_TYPE (olddecl);
1167 *newtypep = newtype = TREE_TYPE (newdecl);
1168 if (oldtype == error_mark_node || newtype == error_mark_node)
1169 return false;
1171 /* Two different categories of symbol altogether. This is an error
1172 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1173 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1175 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1176 && DECL_BUILT_IN (olddecl)
1177 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1179 error ("%q+D redeclared as different kind of symbol", newdecl);
1180 locate_old_decl (olddecl, error);
1182 else if (TREE_PUBLIC (newdecl))
1183 warning (0, "built-in function %q+D declared as non-function",
1184 newdecl);
1185 else
1186 warning (OPT_Wshadow, "declaration of %q+D shadows "
1187 "a built-in function", newdecl);
1188 return false;
1191 /* Enumerators have no linkage, so may only be declared once in a
1192 given scope. */
1193 if (TREE_CODE (olddecl) == CONST_DECL)
1195 error ("redeclaration of enumerator %q+D", newdecl);
1196 locate_old_decl (olddecl, error);
1197 return false;
1200 if (!comptypes (oldtype, newtype))
1202 if (TREE_CODE (olddecl) == FUNCTION_DECL
1203 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1205 /* Accept harmless mismatch in function types.
1206 This is for the ffs and fprintf builtins. */
1207 tree trytype = match_builtin_function_types (newtype, oldtype);
1209 if (trytype && comptypes (newtype, trytype))
1210 *oldtypep = oldtype = trytype;
1211 else
1213 /* If types don't match for a built-in, throw away the
1214 built-in. No point in calling locate_old_decl here, it
1215 won't print anything. */
1216 warning (0, "conflicting types for built-in function %q+D",
1217 newdecl);
1218 return false;
1221 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1222 && DECL_IS_BUILTIN (olddecl))
1224 /* A conflicting function declaration for a predeclared
1225 function that isn't actually built in. Objective C uses
1226 these. The new declaration silently overrides everything
1227 but the volatility (i.e. noreturn) indication. See also
1228 below. FIXME: Make Objective C use normal builtins. */
1229 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1230 return false;
1232 /* Permit void foo (...) to match int foo (...) if the latter is
1233 the definition and implicit int was used. See
1234 c-torture/compile/920625-2.c. */
1235 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1236 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1237 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1238 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1240 pedwarn ("conflicting types for %q+D", newdecl);
1241 /* Make sure we keep void as the return type. */
1242 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1243 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1244 pedwarned = true;
1246 /* Permit void foo (...) to match an earlier call to foo (...) with
1247 no declared type (thus, implicitly int). */
1248 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1249 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1250 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1251 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1253 pedwarn ("conflicting types for %q+D", newdecl);
1254 /* Make sure we keep void as the return type. */
1255 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1256 pedwarned = true;
1258 else
1260 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1261 error ("conflicting type qualifiers for %q+D", newdecl);
1262 else
1263 error ("conflicting types for %q+D", newdecl);
1264 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1265 locate_old_decl (olddecl, error);
1266 return false;
1270 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1271 but silently ignore the redeclaration if either is in a system
1272 header. (Conflicting redeclarations were handled above.) */
1273 if (TREE_CODE (newdecl) == TYPE_DECL)
1275 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1276 return true; /* Allow OLDDECL to continue in use. */
1278 error ("redefinition of typedef %q+D", newdecl);
1279 locate_old_decl (olddecl, error);
1280 return false;
1283 /* Function declarations can either be 'static' or 'extern' (no
1284 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1285 can never conflict with each other on account of linkage (6.2.2p4).
1286 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1287 two definitions if one is 'extern inline' and one is not. The non-
1288 extern-inline definition supersedes the extern-inline definition. */
1290 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1292 /* If you declare a built-in function name as static, or
1293 define the built-in with an old-style definition (so we
1294 can't validate the argument list) the built-in definition is
1295 overridden, but optionally warn this was a bad choice of name. */
1296 if (DECL_BUILT_IN (olddecl)
1297 && !C_DECL_DECLARED_BUILTIN (olddecl)
1298 && (!TREE_PUBLIC (newdecl)
1299 || (DECL_INITIAL (newdecl)
1300 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1302 warning (OPT_Wshadow, "declaration of %q+D shadows "
1303 "a built-in function", newdecl);
1304 /* Discard the old built-in function. */
1305 return false;
1308 if (DECL_INITIAL (newdecl))
1310 if (DECL_INITIAL (olddecl))
1312 /* If both decls are in the same TU and the new declaration
1313 isn't overriding an extern inline reject the new decl.
1314 When we handle c99 style inline rules we'll want to reject
1315 the following:
1317 DECL_EXTERN_INLINE (olddecl)
1318 && !DECL_EXTERN_INLINE (newdecl)
1320 if they're in the same translation unit. Until we implement
1321 the full semantics we accept the construct. */
1322 if (!(DECL_EXTERN_INLINE (olddecl)
1323 && !DECL_EXTERN_INLINE (newdecl))
1324 && same_translation_unit_p (newdecl, olddecl))
1326 error ("redefinition of %q+D", newdecl);
1327 locate_old_decl (olddecl, error);
1328 return false;
1332 /* If we have a prototype after an old-style function definition,
1333 the argument types must be checked specially. */
1334 else if (DECL_INITIAL (olddecl)
1335 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1336 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1337 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1339 locate_old_decl (olddecl, error);
1340 return false;
1342 /* A non-static declaration (even an "extern") followed by a
1343 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1344 The same is true for a static forward declaration at block
1345 scope followed by a non-static declaration/definition at file
1346 scope. Static followed by non-static at the same scope is
1347 not undefined behavior, and is the most convenient way to get
1348 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1349 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1350 we do diagnose it if -Wtraditional. */
1351 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1353 /* Two exceptions to the rule. If olddecl is an extern
1354 inline, or a predeclared function that isn't actually
1355 built in, newdecl silently overrides olddecl. The latter
1356 occur only in Objective C; see also above. (FIXME: Make
1357 Objective C use normal builtins.) */
1358 if (!DECL_IS_BUILTIN (olddecl)
1359 && !DECL_EXTERN_INLINE (olddecl))
1361 error ("static declaration of %q+D follows "
1362 "non-static declaration", newdecl);
1363 locate_old_decl (olddecl, error);
1365 return false;
1367 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1369 if (DECL_CONTEXT (olddecl))
1371 error ("non-static declaration of %q+D follows "
1372 "static declaration", newdecl);
1373 locate_old_decl (olddecl, error);
1374 return false;
1376 else if (warn_traditional)
1378 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1379 "follows static declaration", newdecl);
1380 warned = true;
1384 else if (TREE_CODE (newdecl) == VAR_DECL)
1386 /* Only variables can be thread-local, and all declarations must
1387 agree on this property. */
1388 if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1390 if (DECL_THREAD_LOCAL_P (newdecl))
1391 error ("thread-local declaration of %q+D follows "
1392 "non-thread-local declaration", newdecl);
1393 else
1394 error ("non-thread-local declaration of %q+D follows "
1395 "thread-local declaration", newdecl);
1397 locate_old_decl (olddecl, error);
1398 return false;
1401 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1402 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1404 error ("redefinition of %q+D", newdecl);
1405 locate_old_decl (olddecl, error);
1406 return false;
1409 /* Objects declared at file scope: if the first declaration had
1410 external linkage (even if it was an external reference) the
1411 second must have external linkage as well, or the behavior is
1412 undefined. If the first declaration had internal linkage, then
1413 the second must too, or else be an external reference (in which
1414 case the composite declaration still has internal linkage).
1415 As for function declarations, we warn about the static-then-
1416 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1417 if (DECL_FILE_SCOPE_P (newdecl)
1418 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1420 if (DECL_EXTERNAL (newdecl))
1422 if (!DECL_FILE_SCOPE_P (olddecl))
1424 error ("extern declaration of %q+D follows "
1425 "declaration with no linkage", newdecl);
1426 locate_old_decl (olddecl, error);
1427 return false;
1429 else if (warn_traditional)
1431 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1432 "follows static declaration", newdecl);
1433 warned = true;
1436 else
1438 if (TREE_PUBLIC (newdecl))
1439 error ("non-static declaration of %q+D follows "
1440 "static declaration", newdecl);
1441 else
1442 error ("static declaration of %q+D follows "
1443 "non-static declaration", newdecl);
1445 locate_old_decl (olddecl, error);
1446 return false;
1449 /* Two objects with the same name declared at the same block
1450 scope must both be external references (6.7p3). */
1451 else if (!DECL_FILE_SCOPE_P (newdecl))
1453 if (DECL_EXTERNAL (newdecl))
1455 /* Extern with initializer at block scope, which will
1456 already have received an error. */
1458 else if (DECL_EXTERNAL (olddecl))
1460 error ("declaration of %q+D with no linkage follows "
1461 "extern declaration", newdecl);
1462 locate_old_decl (olddecl, error);
1464 else
1466 error ("redeclaration of %q+D with no linkage", newdecl);
1467 locate_old_decl (olddecl, error);
1470 return false;
1474 /* warnings */
1475 /* All decls must agree on a visibility. */
1476 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1477 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1478 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1480 warning (0, "redeclaration of %q+D with different visibility "
1481 "(old visibility preserved)", newdecl);
1482 warned = true;
1485 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1487 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1488 if (DECL_DECLARED_INLINE_P (newdecl)
1489 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1491 warning (OPT_Wattributes, "inline declaration of %qD follows "
1492 "declaration with attribute noinline", newdecl);
1493 warned = true;
1495 else if (DECL_DECLARED_INLINE_P (olddecl)
1496 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1498 warning (OPT_Wattributes, "declaration of %q+D with attribute "
1499 "noinline follows inline declaration ", newdecl);
1500 warned = true;
1503 /* Inline declaration after use or definition.
1504 ??? Should we still warn about this now we have unit-at-a-time
1505 mode and can get it right?
1506 Definitely don't complain if the decls are in different translation
1507 units. */
1508 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1509 && same_translation_unit_p (olddecl, newdecl))
1511 if (TREE_USED (olddecl))
1513 warning (0, "%q+D declared inline after being called", olddecl);
1514 warned = true;
1516 else if (DECL_INITIAL (olddecl))
1518 warning (0, "%q+D declared inline after its definition", olddecl);
1519 warned = true;
1523 else /* PARM_DECL, VAR_DECL */
1525 /* Redeclaration of a parameter is a constraint violation (this is
1526 not explicitly stated, but follows from C99 6.7p3 [no more than
1527 one declaration of the same identifier with no linkage in the
1528 same scope, except type tags] and 6.2.2p6 [parameters have no
1529 linkage]). We must check for a forward parameter declaration,
1530 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1531 an extension, the mandatory diagnostic for which is handled by
1532 mark_forward_parm_decls. */
1534 if (TREE_CODE (newdecl) == PARM_DECL
1535 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1537 error ("redefinition of parameter %q+D", newdecl);
1538 locate_old_decl (olddecl, error);
1539 return false;
1543 /* Optional warning for completely redundant decls. */
1544 if (!warned && !pedwarned
1545 && warn_redundant_decls
1546 /* Don't warn about a function declaration followed by a
1547 definition. */
1548 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1549 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1550 /* Don't warn about redundant redeclarations of builtins. */
1551 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1552 && !DECL_BUILT_IN (newdecl)
1553 && DECL_BUILT_IN (olddecl)
1554 && !C_DECL_DECLARED_BUILTIN (olddecl))
1555 /* Don't warn about an extern followed by a definition. */
1556 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1557 /* Don't warn about forward parameter decls. */
1558 && !(TREE_CODE (newdecl) == PARM_DECL
1559 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1560 /* Don't warn about a variable definition following a declaration. */
1561 && !(TREE_CODE (newdecl) == VAR_DECL
1562 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1564 warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1565 newdecl);
1566 warned = true;
1569 /* Report location of previous decl/defn in a consistent manner. */
1570 if (warned || pedwarned)
1571 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1573 #undef DECL_EXTERN_INLINE
1575 return retval;
1578 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1579 consistent with OLDDECL, but carries new information. Merge the
1580 new information into OLDDECL. This function issues no
1581 diagnostics. */
1583 static void
1584 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1586 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1587 && DECL_INITIAL (newdecl) != 0);
1588 int new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1589 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1590 int old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1591 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1593 /* For real parm decl following a forward decl, rechain the old decl
1594 in its new location and clear TREE_ASM_WRITTEN (it's not a
1595 forward decl anymore). */
1596 if (TREE_CODE (newdecl) == PARM_DECL
1597 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1599 struct c_binding *b, **here;
1601 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1602 if ((*here)->decl == olddecl)
1603 goto found;
1604 gcc_unreachable ();
1606 found:
1607 b = *here;
1608 *here = b->prev;
1609 b->prev = current_scope->bindings;
1610 current_scope->bindings = b;
1612 TREE_ASM_WRITTEN (olddecl) = 0;
1615 DECL_ATTRIBUTES (newdecl)
1616 = targetm.merge_decl_attributes (olddecl, newdecl);
1618 /* Merge the data types specified in the two decls. */
1619 TREE_TYPE (newdecl)
1620 = TREE_TYPE (olddecl)
1621 = composite_type (newtype, oldtype);
1623 /* Lay the type out, unless already done. */
1624 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1626 if (TREE_TYPE (newdecl) != error_mark_node)
1627 layout_type (TREE_TYPE (newdecl));
1628 if (TREE_CODE (newdecl) != FUNCTION_DECL
1629 && TREE_CODE (newdecl) != TYPE_DECL
1630 && TREE_CODE (newdecl) != CONST_DECL)
1631 layout_decl (newdecl, 0);
1633 else
1635 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1636 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1637 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1638 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1639 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1640 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1642 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1643 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1647 /* Keep the old rtl since we can safely use it. */
1648 if (HAS_RTL_P (olddecl))
1649 COPY_DECL_RTL (olddecl, newdecl);
1651 /* Merge the type qualifiers. */
1652 if (TREE_READONLY (newdecl))
1653 TREE_READONLY (olddecl) = 1;
1655 if (TREE_THIS_VOLATILE (newdecl))
1657 TREE_THIS_VOLATILE (olddecl) = 1;
1658 if (TREE_CODE (newdecl) == VAR_DECL)
1659 make_var_volatile (newdecl);
1662 /* Merge deprecatedness. */
1663 if (TREE_DEPRECATED (newdecl))
1664 TREE_DEPRECATED (olddecl) = 1;
1666 /* Keep source location of definition rather than declaration and of
1667 prototype rather than non-prototype unless that prototype is
1668 built-in. */
1669 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1670 || (old_is_prototype && !new_is_prototype
1671 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1672 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1674 /* Merge the initialization information. */
1675 if (DECL_INITIAL (newdecl) == 0)
1676 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1678 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1680 /* Merge the unused-warning information. */
1681 if (DECL_IN_SYSTEM_HEADER (olddecl))
1682 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1683 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1684 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1686 /* Merge the section attribute.
1687 We want to issue an error if the sections conflict but that must be
1688 done later in decl_attributes since we are called before attributes
1689 are assigned. */
1690 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1691 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1693 /* Copy the assembler name.
1694 Currently, it can only be defined in the prototype. */
1695 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1697 /* Use visibility of whichever declaration had it specified */
1698 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1700 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1701 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1704 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1706 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1707 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1708 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1709 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1710 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1711 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1712 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1713 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1714 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1715 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1718 /* Merge the storage class information. */
1719 merge_weak (newdecl, olddecl);
1721 /* For functions, static overrides non-static. */
1722 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1724 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1725 /* This is since we don't automatically
1726 copy the attributes of NEWDECL into OLDDECL. */
1727 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1728 /* If this clears `static', clear it in the identifier too. */
1729 if (!TREE_PUBLIC (olddecl))
1730 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1734 if (DECL_EXTERNAL (newdecl))
1736 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1737 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1739 /* An extern decl does not override previous storage class. */
1740 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1741 if (!DECL_EXTERNAL (newdecl))
1743 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1744 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1747 else
1749 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1750 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1753 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1755 /* If we're redefining a function previously defined as extern
1756 inline, make sure we emit debug info for the inline before we
1757 throw it away, in case it was inlined into a function that hasn't
1758 been written out yet. */
1759 if (new_is_definition && DECL_INITIAL (olddecl))
1761 if (TREE_USED (olddecl)
1762 /* In unit-at-a-time mode we never inline re-defined extern
1763 inline functions. */
1764 && !flag_unit_at_a_time
1765 && cgraph_function_possibly_inlined_p (olddecl))
1766 (*debug_hooks->outlining_inline_function) (olddecl);
1768 /* The new defn must not be inline. */
1769 DECL_INLINE (newdecl) = 0;
1770 DECL_UNINLINABLE (newdecl) = 1;
1772 else
1774 /* If either decl says `inline', this fn is inline,
1775 unless its definition was passed already. */
1776 if (DECL_DECLARED_INLINE_P (newdecl)
1777 || DECL_DECLARED_INLINE_P (olddecl))
1778 DECL_DECLARED_INLINE_P (newdecl) = 1;
1780 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1781 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1784 if (DECL_BUILT_IN (olddecl))
1786 /* If redeclaring a builtin function, it stays built in.
1787 But it gets tagged as having been declared. */
1788 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1789 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1790 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1791 if (new_is_prototype)
1792 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1793 else
1794 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1795 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1798 /* Also preserve various other info from the definition. */
1799 if (!new_is_definition)
1801 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1802 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1803 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1804 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1805 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1807 /* Set DECL_INLINE on the declaration if we've got a body
1808 from which to instantiate. */
1809 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1811 DECL_INLINE (newdecl) = 1;
1812 DECL_ABSTRACT_ORIGIN (newdecl)
1813 = DECL_ABSTRACT_ORIGIN (olddecl);
1816 else
1818 /* If a previous declaration said inline, mark the
1819 definition as inlinable. */
1820 if (DECL_DECLARED_INLINE_P (newdecl)
1821 && !DECL_UNINLINABLE (newdecl))
1822 DECL_INLINE (newdecl) = 1;
1826 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1827 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1829 unsigned olddecl_uid = DECL_UID (olddecl);
1830 tree olddecl_context = DECL_CONTEXT (olddecl);
1832 memcpy ((char *) olddecl + sizeof (struct tree_common),
1833 (char *) newdecl + sizeof (struct tree_common),
1834 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1835 switch (TREE_CODE (olddecl))
1837 case FIELD_DECL:
1838 case VAR_DECL:
1839 case PARM_DECL:
1840 case LABEL_DECL:
1841 case RESULT_DECL:
1842 case CONST_DECL:
1843 case TYPE_DECL:
1844 case FUNCTION_DECL:
1845 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1846 (char *) newdecl + sizeof (struct tree_decl_common),
1847 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1848 break;
1850 default:
1852 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1853 (char *) newdecl + sizeof (struct tree_decl_common),
1854 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1856 DECL_UID (olddecl) = olddecl_uid;
1857 DECL_CONTEXT (olddecl) = olddecl_context;
1860 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1861 so that encode_section_info has a chance to look at the new decl
1862 flags and attributes. */
1863 if (DECL_RTL_SET_P (olddecl)
1864 && (TREE_CODE (olddecl) == FUNCTION_DECL
1865 || (TREE_CODE (olddecl) == VAR_DECL
1866 && TREE_STATIC (olddecl))))
1867 make_decl_rtl (olddecl);
1870 /* Handle when a new declaration NEWDECL has the same name as an old
1871 one OLDDECL in the same binding contour. Prints an error message
1872 if appropriate.
1874 If safely possible, alter OLDDECL to look like NEWDECL, and return
1875 true. Otherwise, return false. */
1877 static bool
1878 duplicate_decls (tree newdecl, tree olddecl)
1880 tree newtype = NULL, oldtype = NULL;
1882 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1884 /* Avoid `unused variable' and other warnings warnings for OLDDECL. */
1885 TREE_NO_WARNING (olddecl) = 1;
1886 return false;
1889 merge_decls (newdecl, olddecl, newtype, oldtype);
1890 return true;
1894 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1895 static void
1896 warn_if_shadowing (tree new_decl)
1898 struct c_binding *b;
1900 /* Shadow warnings wanted? */
1901 if (!warn_shadow
1902 /* No shadow warnings for internally generated vars. */
1903 || DECL_IS_BUILTIN (new_decl)
1904 /* No shadow warnings for vars made for inlining. */
1905 || DECL_FROM_INLINE (new_decl))
1906 return;
1908 /* Is anything being shadowed? Invisible decls do not count. */
1909 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1910 if (b->decl && b->decl != new_decl && !b->invisible)
1912 tree old_decl = b->decl;
1914 if (old_decl == error_mark_node)
1916 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
1917 "non-variable", new_decl);
1918 break;
1920 else if (TREE_CODE (old_decl) == PARM_DECL)
1921 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
1922 new_decl);
1923 else if (DECL_FILE_SCOPE_P (old_decl))
1924 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
1925 "declaration", new_decl);
1926 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1927 && DECL_BUILT_IN (old_decl))
1929 warning (OPT_Wshadow, "declaration of %q+D shadows "
1930 "a built-in function", new_decl);
1931 break;
1933 else
1934 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
1935 new_decl);
1937 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
1939 break;
1944 /* Subroutine of pushdecl.
1946 X is a TYPE_DECL for a typedef statement. Create a brand new
1947 ..._TYPE node (which will be just a variant of the existing
1948 ..._TYPE node with identical properties) and then install X
1949 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1951 The whole point here is to end up with a situation where each
1952 and every ..._TYPE node the compiler creates will be uniquely
1953 associated with AT MOST one node representing a typedef name.
1954 This way, even though the compiler substitutes corresponding
1955 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1956 early on, later parts of the compiler can always do the reverse
1957 translation and get back the corresponding typedef name. For
1958 example, given:
1960 typedef struct S MY_TYPE;
1961 MY_TYPE object;
1963 Later parts of the compiler might only know that `object' was of
1964 type `struct S' if it were not for code just below. With this
1965 code however, later parts of the compiler see something like:
1967 struct S' == struct S
1968 typedef struct S' MY_TYPE;
1969 struct S' object;
1971 And they can then deduce (from the node for type struct S') that
1972 the original object declaration was:
1974 MY_TYPE object;
1976 Being able to do this is important for proper support of protoize,
1977 and also for generating precise symbolic debugging information
1978 which takes full account of the programmer's (typedef) vocabulary.
1980 Obviously, we don't want to generate a duplicate ..._TYPE node if
1981 the TYPE_DECL node that we are now processing really represents a
1982 standard built-in type.
1984 Since all standard types are effectively declared at line zero
1985 in the source file, we can easily check to see if we are working
1986 on a standard type by checking the current value of lineno. */
1988 static void
1989 clone_underlying_type (tree x)
1991 if (DECL_IS_BUILTIN (x))
1993 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1994 TYPE_NAME (TREE_TYPE (x)) = x;
1996 else if (TREE_TYPE (x) != error_mark_node
1997 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1999 tree tt = TREE_TYPE (x);
2000 DECL_ORIGINAL_TYPE (x) = tt;
2001 tt = build_variant_type_copy (tt);
2002 TYPE_NAME (tt) = x;
2003 TREE_USED (tt) = TREE_USED (x);
2004 TREE_TYPE (x) = tt;
2008 /* Record a decl-node X as belonging to the current lexical scope.
2009 Check for errors (such as an incompatible declaration for the same
2010 name already seen in the same scope).
2012 Returns either X or an old decl for the same name.
2013 If an old decl is returned, it may have been smashed
2014 to agree with what X says. */
2016 tree
2017 pushdecl (tree x)
2019 tree name = DECL_NAME (x);
2020 struct c_scope *scope = current_scope;
2021 struct c_binding *b;
2022 bool nested = false;
2024 /* Functions need the lang_decl data. */
2025 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
2026 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
2028 /* Must set DECL_CONTEXT for everything not at file scope or
2029 DECL_FILE_SCOPE_P won't work. Local externs don't count
2030 unless they have initializers (which generate code). */
2031 if (current_function_decl
2032 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2033 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2034 DECL_CONTEXT (x) = current_function_decl;
2036 /* If this is of variably modified type, prevent jumping into its
2037 scope. */
2038 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2039 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2040 c_begin_vm_scope (scope->depth);
2042 /* Anonymous decls are just inserted in the scope. */
2043 if (!name)
2045 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2046 return x;
2049 /* First, see if there is another declaration with the same name in
2050 the current scope. If there is, duplicate_decls may do all the
2051 work for us. If duplicate_decls returns false, that indicates
2052 two incompatible decls in the same scope; we are to silently
2053 replace the old one (duplicate_decls has issued all appropriate
2054 diagnostics). In particular, we should not consider possible
2055 duplicates in the external scope, or shadowing. */
2056 b = I_SYMBOL_BINDING (name);
2057 if (b && B_IN_SCOPE (b, scope))
2059 struct c_binding *b_ext, *b_use;
2060 tree type = TREE_TYPE (x);
2061 tree visdecl = b->decl;
2062 tree vistype = TREE_TYPE (visdecl);
2063 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2064 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2065 b->inner_comp = false;
2066 b_use = b;
2067 b_ext = b;
2068 /* If this is an external linkage declaration, we should check
2069 for compatibility with the type in the external scope before
2070 setting the type at this scope based on the visible
2071 information only. */
2072 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2074 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2075 b_ext = b_ext->shadowed;
2076 if (b_ext)
2078 b_use = b_ext;
2079 if (b_use->type)
2080 TREE_TYPE (b_use->decl) = b_use->type;
2083 if (duplicate_decls (x, b_use->decl))
2085 if (b_use != b)
2087 /* Save the updated type in the external scope and
2088 restore the proper type for this scope. */
2089 tree thistype;
2090 if (comptypes (vistype, type))
2091 thistype = composite_type (vistype, type);
2092 else
2093 thistype = TREE_TYPE (b_use->decl);
2094 b_use->type = TREE_TYPE (b_use->decl);
2095 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2096 && DECL_BUILT_IN (b_use->decl))
2097 thistype
2098 = build_type_attribute_variant (thistype,
2099 TYPE_ATTRIBUTES
2100 (b_use->type));
2101 TREE_TYPE (b_use->decl) = thistype;
2103 return b_use->decl;
2105 else
2106 goto skip_external_and_shadow_checks;
2109 /* All declarations with external linkage, and all external
2110 references, go in the external scope, no matter what scope is
2111 current. However, the binding in that scope is ignored for
2112 purposes of normal name lookup. A separate binding structure is
2113 created in the requested scope; this governs the normal
2114 visibility of the symbol.
2116 The binding in the externals scope is used exclusively for
2117 detecting duplicate declarations of the same object, no matter
2118 what scope they are in; this is what we do here. (C99 6.2.7p2:
2119 All declarations that refer to the same object or function shall
2120 have compatible type; otherwise, the behavior is undefined.) */
2121 if (DECL_EXTERNAL (x) || scope == file_scope)
2123 tree type = TREE_TYPE (x);
2124 tree vistype = 0;
2125 tree visdecl = 0;
2126 bool type_saved = false;
2127 if (b && !B_IN_EXTERNAL_SCOPE (b)
2128 && (TREE_CODE (b->decl) == FUNCTION_DECL
2129 || TREE_CODE (b->decl) == VAR_DECL)
2130 && DECL_FILE_SCOPE_P (b->decl))
2132 visdecl = b->decl;
2133 vistype = TREE_TYPE (visdecl);
2135 if (scope != file_scope
2136 && !DECL_IN_SYSTEM_HEADER (x))
2137 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2139 while (b && !B_IN_EXTERNAL_SCOPE (b))
2141 /* If this decl might be modified, save its type. This is
2142 done here rather than when the decl is first bound
2143 because the type may change after first binding, through
2144 being completed or through attributes being added. If we
2145 encounter multiple such decls, only the first should have
2146 its type saved; the others will already have had their
2147 proper types saved and the types will not have changed as
2148 their scopes will not have been re-entered. */
2149 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2151 b->type = TREE_TYPE (b->decl);
2152 type_saved = true;
2154 if (B_IN_FILE_SCOPE (b)
2155 && TREE_CODE (b->decl) == VAR_DECL
2156 && TREE_STATIC (b->decl)
2157 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2158 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2159 && TREE_CODE (type) == ARRAY_TYPE
2160 && TYPE_DOMAIN (type)
2161 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2162 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2164 /* Array type completed in inner scope, which should be
2165 diagnosed if the completion does not have size 1 and
2166 it does not get completed in the file scope. */
2167 b->inner_comp = true;
2169 b = b->shadowed;
2172 /* If a matching external declaration has been found, set its
2173 type to the composite of all the types of that declaration.
2174 After the consistency checks, it will be reset to the
2175 composite of the visible types only. */
2176 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2177 && b->type)
2178 TREE_TYPE (b->decl) = b->type;
2180 /* The point of the same_translation_unit_p check here is,
2181 we want to detect a duplicate decl for a construct like
2182 foo() { extern bar(); } ... static bar(); but not if
2183 they are in different translation units. In any case,
2184 the static does not go in the externals scope. */
2185 if (b
2186 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2187 && duplicate_decls (x, b->decl))
2189 tree thistype;
2190 if (vistype)
2192 if (comptypes (vistype, type))
2193 thistype = composite_type (vistype, type);
2194 else
2195 thistype = TREE_TYPE (b->decl);
2197 else
2198 thistype = type;
2199 b->type = TREE_TYPE (b->decl);
2200 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2201 thistype
2202 = build_type_attribute_variant (thistype,
2203 TYPE_ATTRIBUTES (b->type));
2204 TREE_TYPE (b->decl) = thistype;
2205 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2206 return b->decl;
2208 else if (TREE_PUBLIC (x))
2210 if (visdecl && !b && duplicate_decls (x, visdecl))
2212 /* An external declaration at block scope referring to a
2213 visible entity with internal linkage. The composite
2214 type will already be correct for this scope, so we
2215 just need to fall through to make the declaration in
2216 this scope. */
2217 nested = true;
2218 x = visdecl;
2220 else
2222 bind (name, x, external_scope, /*invisible=*/true,
2223 /*nested=*/false);
2224 nested = true;
2229 if (TREE_CODE (x) != PARM_DECL)
2230 warn_if_shadowing (x);
2232 skip_external_and_shadow_checks:
2233 if (TREE_CODE (x) == TYPE_DECL)
2234 clone_underlying_type (x);
2236 bind (name, x, scope, /*invisible=*/false, nested);
2238 /* If x's type is incomplete because it's based on a
2239 structure or union which has not yet been fully declared,
2240 attach it to that structure or union type, so we can go
2241 back and complete the variable declaration later, if the
2242 structure or union gets fully declared.
2244 If the input is erroneous, we can have error_mark in the type
2245 slot (e.g. "f(void a, ...)") - that doesn't count as an
2246 incomplete type. */
2247 if (TREE_TYPE (x) != error_mark_node
2248 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2250 tree element = TREE_TYPE (x);
2252 while (TREE_CODE (element) == ARRAY_TYPE)
2253 element = TREE_TYPE (element);
2254 element = TYPE_MAIN_VARIANT (element);
2256 if ((TREE_CODE (element) == RECORD_TYPE
2257 || TREE_CODE (element) == UNION_TYPE)
2258 && (TREE_CODE (x) != TYPE_DECL
2259 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2260 && !COMPLETE_TYPE_P (element))
2261 C_TYPE_INCOMPLETE_VARS (element)
2262 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2264 return x;
2267 /* Record X as belonging to file scope.
2268 This is used only internally by the Objective-C front end,
2269 and is limited to its needs. duplicate_decls is not called;
2270 if there is any preexisting decl for this identifier, it is an ICE. */
2272 tree
2273 pushdecl_top_level (tree x)
2275 tree name;
2276 bool nested = false;
2277 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2279 name = DECL_NAME (x);
2281 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2283 if (TREE_PUBLIC (x))
2285 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2286 nested = true;
2288 if (file_scope)
2289 bind (name, x, file_scope, /*invisible=*/false, nested);
2291 return x;
2294 static void
2295 implicit_decl_warning (tree id, tree olddecl)
2297 void (*diag) (const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2);
2298 switch (mesg_implicit_function_declaration)
2300 case 0: return;
2301 case 1: diag = warning0; break;
2302 case 2: diag = error; break;
2303 default: gcc_unreachable ();
2306 diag (G_("implicit declaration of function %qE"), id);
2307 if (olddecl)
2308 locate_old_decl (olddecl, diag);
2311 /* Generate an implicit declaration for identifier FUNCTIONID as a
2312 function of type int (). */
2314 tree
2315 implicitly_declare (tree functionid)
2317 struct c_binding *b;
2318 tree decl = 0;
2319 tree asmspec_tree;
2321 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2323 if (B_IN_SCOPE (b, external_scope))
2325 decl = b->decl;
2326 break;
2330 if (decl)
2332 if (decl == error_mark_node)
2333 return decl;
2335 /* FIXME: Objective-C has weird not-really-builtin functions
2336 which are supposed to be visible automatically. They wind up
2337 in the external scope because they're pushed before the file
2338 scope gets created. Catch this here and rebind them into the
2339 file scope. */
2340 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2342 bind (functionid, decl, file_scope,
2343 /*invisible=*/false, /*nested=*/true);
2344 return decl;
2346 else
2348 tree newtype = default_function_type;
2349 if (b->type)
2350 TREE_TYPE (decl) = b->type;
2351 /* Implicit declaration of a function already declared
2352 (somehow) in a different scope, or as a built-in.
2353 If this is the first time this has happened, warn;
2354 then recycle the old declaration but with the new type. */
2355 if (!C_DECL_IMPLICIT (decl))
2357 implicit_decl_warning (functionid, decl);
2358 C_DECL_IMPLICIT (decl) = 1;
2360 if (DECL_BUILT_IN (decl))
2362 newtype = build_type_attribute_variant (newtype,
2363 TYPE_ATTRIBUTES
2364 (TREE_TYPE (decl)));
2365 if (!comptypes (newtype, TREE_TYPE (decl)))
2367 warning (0, "incompatible implicit declaration of built-in"
2368 " function %qD", decl);
2369 newtype = TREE_TYPE (decl);
2372 else
2374 if (!comptypes (newtype, TREE_TYPE (decl)))
2376 error ("incompatible implicit declaration of function %qD",
2377 decl);
2378 locate_old_decl (decl, error);
2381 b->type = TREE_TYPE (decl);
2382 TREE_TYPE (decl) = newtype;
2383 bind (functionid, decl, current_scope,
2384 /*invisible=*/false, /*nested=*/true);
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);
2450 /* Subroutine of lookup_label, declare_label, define_label: construct a
2451 LABEL_DECL with all the proper frills. */
2453 static tree
2454 make_label (tree name, location_t location)
2456 tree label = build_decl (LABEL_DECL, name, void_type_node);
2458 DECL_CONTEXT (label) = current_function_decl;
2459 DECL_MODE (label) = VOIDmode;
2460 DECL_SOURCE_LOCATION (label) = location;
2462 return label;
2465 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2466 Create one if none exists so far for the current function.
2467 This is called when a label is used in a goto expression or
2468 has its address taken. */
2470 tree
2471 lookup_label (tree name)
2473 tree label;
2475 if (current_function_decl == 0)
2477 error ("label %qE referenced outside of any function", name);
2478 return 0;
2481 /* Use a label already defined or ref'd with this name, but not if
2482 it is inherited from a containing function and wasn't declared
2483 using __label__. */
2484 label = I_LABEL_DECL (name);
2485 if (label && (DECL_CONTEXT (label) == current_function_decl
2486 || C_DECLARED_LABEL_FLAG (label)))
2488 /* If the label has only been declared, update its apparent
2489 location to point here, for better diagnostics if it
2490 turns out not to have been defined. */
2491 if (!TREE_USED (label))
2492 DECL_SOURCE_LOCATION (label) = input_location;
2493 return label;
2496 /* No label binding for that identifier; make one. */
2497 label = make_label (name, input_location);
2499 /* Ordinary labels go in the current function scope. */
2500 bind (name, label, current_function_scope,
2501 /*invisible=*/false, /*nested=*/false);
2502 return label;
2505 /* Make a label named NAME in the current function, shadowing silently
2506 any that may be inherited from containing functions or containing
2507 scopes. This is called for __label__ declarations. */
2509 tree
2510 declare_label (tree name)
2512 struct c_binding *b = I_LABEL_BINDING (name);
2513 tree label;
2515 /* Check to make sure that the label hasn't already been declared
2516 at this scope */
2517 if (b && B_IN_CURRENT_SCOPE (b))
2519 error ("duplicate label declaration %qE", name);
2520 locate_old_decl (b->decl, error);
2522 /* Just use the previous declaration. */
2523 return b->decl;
2526 label = make_label (name, input_location);
2527 C_DECLARED_LABEL_FLAG (label) = 1;
2529 /* Declared labels go in the current scope. */
2530 bind (name, label, current_scope,
2531 /*invisible=*/false, /*nested=*/false);
2532 return label;
2535 /* Define a label, specifying the location in the source file.
2536 Return the LABEL_DECL node for the label, if the definition is valid.
2537 Otherwise return 0. */
2539 tree
2540 define_label (location_t location, tree name)
2542 /* Find any preexisting label with this name. It is an error
2543 if that label has already been defined in this function, or
2544 if there is a containing function with a declared label with
2545 the same name. */
2546 tree label = I_LABEL_DECL (name);
2547 struct c_label_list *nlist_se, *nlist_vm;
2549 if (label
2550 && ((DECL_CONTEXT (label) == current_function_decl
2551 && DECL_INITIAL (label) != 0)
2552 || (DECL_CONTEXT (label) != current_function_decl
2553 && C_DECLARED_LABEL_FLAG (label))))
2555 error ("%Hduplicate label %qD", &location, label);
2556 locate_old_decl (label, error);
2557 return 0;
2559 else if (label && DECL_CONTEXT (label) == current_function_decl)
2561 /* The label has been used or declared already in this function,
2562 but not defined. Update its location to point to this
2563 definition. */
2564 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2565 error ("%Jjump into statement expression", label);
2566 if (C_DECL_UNDEFINABLE_VM (label))
2567 error ("%Jjump into scope of identifier with variably modified type",
2568 label);
2569 DECL_SOURCE_LOCATION (label) = location;
2571 else
2573 /* No label binding for that identifier; make one. */
2574 label = make_label (name, location);
2576 /* Ordinary labels go in the current function scope. */
2577 bind (name, label, current_function_scope,
2578 /*invisible=*/false, /*nested=*/false);
2581 if (!in_system_header && lookup_name (name))
2582 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2583 "for labels, identifier %qE conflicts", &location, name);
2585 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2586 nlist_se->next = label_context_stack_se->labels_def;
2587 nlist_se->label = label;
2588 label_context_stack_se->labels_def = nlist_se;
2590 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2591 nlist_vm->next = label_context_stack_vm->labels_def;
2592 nlist_vm->label = label;
2593 label_context_stack_vm->labels_def = nlist_vm;
2595 /* Mark label as having been defined. */
2596 DECL_INITIAL (label) = error_mark_node;
2597 return label;
2600 /* Given NAME, an IDENTIFIER_NODE,
2601 return the structure (or union or enum) definition for that name.
2602 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2603 CODE says which kind of type the caller wants;
2604 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2605 If the wrong kind of type is found, an error is reported. */
2607 static tree
2608 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2610 struct c_binding *b = I_TAG_BINDING (name);
2611 int thislevel = 0;
2613 if (!b || !b->decl)
2614 return 0;
2616 /* We only care about whether it's in this level if
2617 thislevel_only was set or it might be a type clash. */
2618 if (thislevel_only || TREE_CODE (b->decl) != code)
2620 /* For our purposes, a tag in the external scope is the same as
2621 a tag in the file scope. (Primarily relevant to Objective-C
2622 and its builtin structure tags, which get pushed before the
2623 file scope is created.) */
2624 if (B_IN_CURRENT_SCOPE (b)
2625 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2626 thislevel = 1;
2629 if (thislevel_only && !thislevel)
2630 return 0;
2632 if (TREE_CODE (b->decl) != code)
2634 /* Definition isn't the kind we were looking for. */
2635 pending_invalid_xref = name;
2636 pending_invalid_xref_location = input_location;
2638 /* If in the same binding level as a declaration as a tag
2639 of a different type, this must not be allowed to
2640 shadow that tag, so give the error immediately.
2641 (For example, "struct foo; union foo;" is invalid.) */
2642 if (thislevel)
2643 pending_xref_error ();
2645 return b->decl;
2648 /* Print an error message now
2649 for a recent invalid struct, union or enum cross reference.
2650 We don't print them immediately because they are not invalid
2651 when used in the `struct foo;' construct for shadowing. */
2653 void
2654 pending_xref_error (void)
2656 if (pending_invalid_xref != 0)
2657 error ("%H%qE defined as wrong kind of tag",
2658 &pending_invalid_xref_location, pending_invalid_xref);
2659 pending_invalid_xref = 0;
2663 /* Look up NAME in the current scope and its superiors
2664 in the namespace of variables, functions and typedefs.
2665 Return a ..._DECL node of some kind representing its definition,
2666 or return 0 if it is undefined. */
2668 tree
2669 lookup_name (tree name)
2671 struct c_binding *b = I_SYMBOL_BINDING (name);
2672 if (b && !b->invisible)
2673 return b->decl;
2674 return 0;
2677 /* Similar to `lookup_name' but look only at the indicated scope. */
2679 static tree
2680 lookup_name_in_scope (tree name, struct c_scope *scope)
2682 struct c_binding *b;
2684 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2685 if (B_IN_SCOPE (b, scope))
2686 return b->decl;
2687 return 0;
2690 /* Create the predefined scalar types of C,
2691 and some nodes representing standard constants (0, 1, (void *) 0).
2692 Initialize the global scope.
2693 Make definitions for built-in primitive functions. */
2695 void
2696 c_init_decl_processing (void)
2698 location_t save_loc = input_location;
2700 /* Initialize reserved words for parser. */
2701 c_parse_init ();
2703 current_function_decl = 0;
2705 gcc_obstack_init (&parser_obstack);
2707 /* Make the externals scope. */
2708 push_scope ();
2709 external_scope = current_scope;
2711 /* Declarations from c_common_nodes_and_builtins must not be associated
2712 with this input file, lest we get differences between using and not
2713 using preprocessed headers. */
2714 #ifdef USE_MAPPED_LOCATION
2715 input_location = BUILTINS_LOCATION;
2716 #else
2717 input_location.file = "<built-in>";
2718 input_location.line = 0;
2719 #endif
2721 build_common_tree_nodes (flag_signed_char, false);
2723 c_common_nodes_and_builtins ();
2725 /* In C, comparisons and TRUTH_* expressions have type int. */
2726 truthvalue_type_node = integer_type_node;
2727 truthvalue_true_node = integer_one_node;
2728 truthvalue_false_node = integer_zero_node;
2730 /* Even in C99, which has a real boolean type. */
2731 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2732 boolean_type_node));
2734 input_location = save_loc;
2736 pedantic_lvalues = true;
2738 make_fname_decl = c_make_fname_decl;
2739 start_fname_decls ();
2742 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2743 decl, NAME is the initialization string and TYPE_DEP indicates whether
2744 NAME depended on the type of the function. As we don't yet implement
2745 delayed emission of static data, we mark the decl as emitted
2746 so it is not placed in the output. Anything using it must therefore pull
2747 out the STRING_CST initializer directly. FIXME. */
2749 static tree
2750 c_make_fname_decl (tree id, int type_dep)
2752 const char *name = fname_as_string (type_dep);
2753 tree decl, type, init;
2754 size_t length = strlen (name);
2756 type = build_array_type (char_type_node,
2757 build_index_type (size_int (length)));
2758 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2760 decl = build_decl (VAR_DECL, id, type);
2762 TREE_STATIC (decl) = 1;
2763 TREE_READONLY (decl) = 1;
2764 DECL_ARTIFICIAL (decl) = 1;
2766 init = build_string (length + 1, name);
2767 free ((char *) name);
2768 TREE_TYPE (init) = type;
2769 DECL_INITIAL (decl) = init;
2771 TREE_USED (decl) = 1;
2773 if (current_function_decl)
2775 DECL_CONTEXT (decl) = current_function_decl;
2776 bind (id, decl, current_function_scope,
2777 /*invisible=*/false, /*nested=*/false);
2780 finish_decl (decl, init, NULL_TREE);
2782 return decl;
2785 /* Return a definition for a builtin function named NAME and whose data type
2786 is TYPE. TYPE should be a function type with argument types.
2787 FUNCTION_CODE tells later passes how to compile calls to this function.
2788 See tree.h for its possible values.
2790 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2791 the name to be called if we can't opencode the function. If
2792 ATTRS is nonzero, use that for the function's attribute list. */
2794 tree
2795 builtin_function (const char *name, tree type, int function_code,
2796 enum built_in_class cl, const char *library_name,
2797 tree attrs)
2799 tree id = get_identifier (name);
2800 tree decl = build_decl (FUNCTION_DECL, id, type);
2801 TREE_PUBLIC (decl) = 1;
2802 DECL_EXTERNAL (decl) = 1;
2803 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2804 DECL_BUILT_IN_CLASS (decl) = cl;
2805 DECL_FUNCTION_CODE (decl) = function_code;
2806 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2807 if (library_name)
2808 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2810 /* Should never be called on a symbol with a preexisting meaning. */
2811 gcc_assert (!I_SYMBOL_BINDING (id));
2813 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2815 /* Builtins in the implementation namespace are made visible without
2816 needing to be explicitly declared. See push_file_scope. */
2817 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2819 TREE_CHAIN (decl) = visible_builtins;
2820 visible_builtins = decl;
2823 /* Possibly apply some default attributes to this built-in function. */
2824 if (attrs)
2825 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2826 else
2827 decl_attributes (&decl, NULL_TREE, 0);
2829 return decl;
2832 /* Called when a declaration is seen that contains no names to declare.
2833 If its type is a reference to a structure, union or enum inherited
2834 from a containing scope, shadow that tag name for the current scope
2835 with a forward reference.
2836 If its type defines a new named structure or union
2837 or defines an enum, it is valid but we need not do anything here.
2838 Otherwise, it is an error. */
2840 void
2841 shadow_tag (const struct c_declspecs *declspecs)
2843 shadow_tag_warned (declspecs, 0);
2846 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2847 but no pedwarn. */
2848 void
2849 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2851 bool found_tag = false;
2853 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2855 tree value = declspecs->type;
2856 enum tree_code code = TREE_CODE (value);
2858 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2859 /* Used to test also that TYPE_SIZE (value) != 0.
2860 That caused warning for `struct foo;' at top level in the file. */
2862 tree name = TYPE_NAME (value);
2863 tree t;
2865 found_tag = true;
2867 if (name == 0)
2869 if (warned != 1 && code != ENUMERAL_TYPE)
2870 /* Empty unnamed enum OK */
2872 pedwarn ("unnamed struct/union that defines no instances");
2873 warned = 1;
2876 else if (!declspecs->tag_defined_p
2877 && declspecs->storage_class != csc_none)
2879 if (warned != 1)
2880 pedwarn ("empty declaration with storage class specifier "
2881 "does not redeclare tag");
2882 warned = 1;
2883 pending_xref_error ();
2885 else if (!declspecs->tag_defined_p
2886 && (declspecs->const_p
2887 || declspecs->volatile_p
2888 || declspecs->restrict_p))
2890 if (warned != 1)
2891 pedwarn ("empty declaration with type qualifier "
2892 "does not redeclare tag");
2893 warned = 1;
2894 pending_xref_error ();
2896 else
2898 pending_invalid_xref = 0;
2899 t = lookup_tag (code, name, 1);
2901 if (t == 0)
2903 t = make_node (code);
2904 pushtag (name, t);
2908 else
2910 if (warned != 1 && !in_system_header)
2912 pedwarn ("useless type name in empty declaration");
2913 warned = 1;
2917 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2919 pedwarn ("useless type name in empty declaration");
2920 warned = 1;
2923 pending_invalid_xref = 0;
2925 if (declspecs->inline_p)
2927 error ("%<inline%> in empty declaration");
2928 warned = 1;
2931 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2933 error ("%<auto%> in file-scope empty declaration");
2934 warned = 1;
2937 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2939 error ("%<register%> in file-scope empty declaration");
2940 warned = 1;
2943 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2945 warning (0, "useless storage class specifier in empty declaration");
2946 warned = 2;
2949 if (!warned && !in_system_header && declspecs->thread_p)
2951 warning (0, "useless %<__thread%> in empty declaration");
2952 warned = 2;
2955 if (!warned && !in_system_header && (declspecs->const_p
2956 || declspecs->volatile_p
2957 || declspecs->restrict_p))
2959 warning (0, "useless type qualifier in empty declaration");
2960 warned = 2;
2963 if (warned != 1)
2965 if (!found_tag)
2966 pedwarn ("empty declaration");
2971 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2972 bits. SPECS represents declaration specifiers that the grammar
2973 only permits to contain type qualifiers and attributes. */
2976 quals_from_declspecs (const struct c_declspecs *specs)
2978 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
2979 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
2980 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
2981 gcc_assert (!specs->type
2982 && !specs->decl_attr
2983 && specs->typespec_word == cts_none
2984 && specs->storage_class == csc_none
2985 && !specs->typedef_p
2986 && !specs->explicit_signed_p
2987 && !specs->deprecated_p
2988 && !specs->long_p
2989 && !specs->long_long_p
2990 && !specs->short_p
2991 && !specs->signed_p
2992 && !specs->unsigned_p
2993 && !specs->complex_p
2994 && !specs->inline_p
2995 && !specs->thread_p);
2996 return quals;
2999 /* Construct an array declarator. EXPR is the expression inside [], or
3000 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
3001 to the pointer to which a parameter array is converted). STATIC_P is
3002 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
3003 is true if the array is [*], a VLA of unspecified length which is
3004 nevertheless a complete type (not currently implemented by GCC),
3005 false otherwise. The field for the contained declarator is left to be
3006 filled in by set_array_declarator_inner. */
3008 struct c_declarator *
3009 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3010 bool vla_unspec_p)
3012 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3013 struct c_declarator);
3014 declarator->kind = cdk_array;
3015 declarator->declarator = 0;
3016 declarator->u.array.dimen = expr;
3017 if (quals)
3019 declarator->u.array.attrs = quals->attrs;
3020 declarator->u.array.quals = quals_from_declspecs (quals);
3022 else
3024 declarator->u.array.attrs = NULL_TREE;
3025 declarator->u.array.quals = 0;
3027 declarator->u.array.static_p = static_p;
3028 declarator->u.array.vla_unspec_p = vla_unspec_p;
3029 if (pedantic && !flag_isoc99)
3031 if (static_p || quals != NULL)
3032 pedwarn ("ISO C90 does not support %<static%> or type "
3033 "qualifiers in parameter array declarators");
3034 if (vla_unspec_p)
3035 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3037 if (vla_unspec_p)
3038 warning (0, "GCC does not yet properly implement %<[*]%> array declarators");
3039 return declarator;
3042 /* Set the contained declarator of an array declarator. DECL is the
3043 declarator, as constructed by build_array_declarator; INNER is what
3044 appears on the left of the []. ABSTRACT_P is true if it is an
3045 abstract declarator, false otherwise; this is used to reject static
3046 and type qualifiers in abstract declarators, where they are not in
3047 the C99 grammar (subject to possible change in DR#289). */
3049 struct c_declarator *
3050 set_array_declarator_inner (struct c_declarator *decl,
3051 struct c_declarator *inner, bool abstract_p)
3053 decl->declarator = inner;
3054 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3055 || decl->u.array.attrs != NULL_TREE
3056 || decl->u.array.static_p))
3057 error ("static or type qualifiers in abstract declarator");
3058 return decl;
3061 /* INIT is a constructor that forms DECL's initializer. If the final
3062 element initializes a flexible array field, add the size of that
3063 initializer to DECL's size. */
3065 static void
3066 add_flexible_array_elts_to_size (tree decl, tree init)
3068 unsigned int size;
3069 tree elt, type;
3071 size = VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init));
3072 if (size == 0)
3073 return;
3075 elt = VEC_index (constructor_elt, CONSTRUCTOR_ELTS (init), size - 1)->value;
3076 type = TREE_TYPE (elt);
3077 if (TREE_CODE (type) == ARRAY_TYPE
3078 && TYPE_SIZE (type) == NULL_TREE
3079 && TYPE_DOMAIN (type) != NULL_TREE
3080 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3082 complete_array_type (&type, elt, false);
3083 DECL_SIZE (decl)
3084 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3085 DECL_SIZE_UNIT (decl)
3086 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3090 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3092 tree
3093 groktypename (struct c_type_name *type_name)
3095 tree type;
3096 tree attrs = type_name->specs->attrs;
3098 type_name->specs->attrs = NULL_TREE;
3100 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3101 false, NULL);
3103 /* Apply attributes. */
3104 decl_attributes (&type, attrs, 0);
3106 return type;
3109 /* Decode a declarator in an ordinary declaration or data definition.
3110 This is called as soon as the type information and variable name
3111 have been parsed, before parsing the initializer if any.
3112 Here we create the ..._DECL node, fill in its type,
3113 and put it on the list of decls for the current context.
3114 The ..._DECL node is returned as the value.
3116 Exception: for arrays where the length is not specified,
3117 the type is left null, to be filled in by `finish_decl'.
3119 Function definitions do not come here; they go to start_function
3120 instead. However, external and forward declarations of functions
3121 do go through here. Structure field declarations are done by
3122 grokfield and not through here. */
3124 tree
3125 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3126 bool initialized, tree attributes)
3128 tree decl;
3129 tree tem;
3131 /* An object declared as __attribute__((deprecated)) suppresses
3132 warnings of uses of other deprecated items. */
3133 if (lookup_attribute ("deprecated", attributes))
3134 deprecated_state = DEPRECATED_SUPPRESS;
3136 decl = grokdeclarator (declarator, declspecs,
3137 NORMAL, initialized, NULL);
3138 if (!decl)
3139 return 0;
3141 deprecated_state = DEPRECATED_NORMAL;
3143 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3144 && MAIN_NAME_P (DECL_NAME (decl)))
3145 warning (OPT_Wmain, "%q+D is usually a function", decl);
3147 if (initialized)
3148 /* Is it valid for this decl to have an initializer at all?
3149 If not, set INITIALIZED to zero, which will indirectly
3150 tell 'finish_decl' to ignore the initializer once it is parsed. */
3151 switch (TREE_CODE (decl))
3153 case TYPE_DECL:
3154 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3155 initialized = 0;
3156 break;
3158 case FUNCTION_DECL:
3159 error ("function %qD is initialized like a variable", decl);
3160 initialized = 0;
3161 break;
3163 case PARM_DECL:
3164 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3165 error ("parameter %qD is initialized", decl);
3166 initialized = 0;
3167 break;
3169 default:
3170 /* Don't allow initializations for incomplete types except for
3171 arrays which might be completed by the initialization. */
3173 /* This can happen if the array size is an undefined macro.
3174 We already gave a warning, so we don't need another one. */
3175 if (TREE_TYPE (decl) == error_mark_node)
3176 initialized = 0;
3177 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3179 /* A complete type is ok if size is fixed. */
3181 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3182 || C_DECL_VARIABLE_SIZE (decl))
3184 error ("variable-sized object may not be initialized");
3185 initialized = 0;
3188 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3190 error ("variable %qD has initializer but incomplete type", decl);
3191 initialized = 0;
3193 else if (C_DECL_VARIABLE_SIZE (decl))
3195 /* Although C99 is unclear about whether incomplete arrays
3196 of VLAs themselves count as VLAs, it does not make
3197 sense to permit them to be initialized given that
3198 ordinary VLAs may not be initialized. */
3199 error ("variable-sized object may not be initialized");
3200 initialized = 0;
3204 if (initialized)
3206 if (current_scope == file_scope)
3207 TREE_STATIC (decl) = 1;
3209 /* Tell 'pushdecl' this is an initialized decl
3210 even though we don't yet have the initializer expression.
3211 Also tell 'finish_decl' it may store the real initializer. */
3212 DECL_INITIAL (decl) = error_mark_node;
3215 /* If this is a function declaration, write a record describing it to the
3216 prototypes file (if requested). */
3218 if (TREE_CODE (decl) == FUNCTION_DECL)
3219 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3221 /* ANSI specifies that a tentative definition which is not merged with
3222 a non-tentative definition behaves exactly like a definition with an
3223 initializer equal to zero. (Section 3.7.2)
3225 -fno-common gives strict ANSI behavior, though this tends to break
3226 a large body of code that grew up without this rule.
3228 Thread-local variables are never common, since there's no entrenched
3229 body of code to break, and it allows more efficient variable references
3230 in the presence of dynamic linking. */
3232 if (TREE_CODE (decl) == VAR_DECL
3233 && !initialized
3234 && TREE_PUBLIC (decl)
3235 && !DECL_THREAD_LOCAL_P (decl)
3236 && !flag_no_common)
3237 DECL_COMMON (decl) = 1;
3239 /* Set attributes here so if duplicate decl, will have proper attributes. */
3240 decl_attributes (&decl, attributes, 0);
3242 if (TREE_CODE (decl) == FUNCTION_DECL
3243 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3245 struct c_declarator *ce = declarator;
3247 if (ce->kind == cdk_pointer)
3248 ce = declarator->declarator;
3249 if (ce->kind == cdk_function)
3251 tree args = ce->u.arg_info->parms;
3252 for (; args; args = TREE_CHAIN (args))
3254 tree type = TREE_TYPE (args);
3255 if (type && INTEGRAL_TYPE_P (type)
3256 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3257 DECL_ARG_TYPE (args) = integer_type_node;
3262 if (TREE_CODE (decl) == FUNCTION_DECL
3263 && DECL_DECLARED_INLINE_P (decl)
3264 && DECL_UNINLINABLE (decl)
3265 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3266 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3267 decl);
3269 /* Add this decl to the current scope.
3270 TEM may equal DECL or it may be a previous decl of the same name. */
3271 tem = pushdecl (decl);
3273 if (initialized && DECL_EXTERNAL (tem))
3275 DECL_EXTERNAL (tem) = 0;
3276 TREE_STATIC (tem) = 1;
3279 return tem;
3282 /* Finish processing of a declaration;
3283 install its initial value.
3284 If the length of an array type is not known before,
3285 it must be determined now, from the initial value, or it is an error. */
3287 void
3288 finish_decl (tree decl, tree init, tree asmspec_tree)
3290 tree type;
3291 int was_incomplete = (DECL_SIZE (decl) == 0);
3292 const char *asmspec = 0;
3294 /* If a name was specified, get the string. */
3295 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3296 && DECL_FILE_SCOPE_P (decl))
3297 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3298 if (asmspec_tree)
3299 asmspec = TREE_STRING_POINTER (asmspec_tree);
3301 /* If `start_decl' didn't like having an initialization, ignore it now. */
3302 if (init != 0 && DECL_INITIAL (decl) == 0)
3303 init = 0;
3305 /* Don't crash if parm is initialized. */
3306 if (TREE_CODE (decl) == PARM_DECL)
3307 init = 0;
3309 if (init)
3310 store_init_value (decl, init);
3312 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3313 || TREE_CODE (decl) == FUNCTION_DECL
3314 || TREE_CODE (decl) == FIELD_DECL))
3315 objc_check_decl (decl);
3317 type = TREE_TYPE (decl);
3319 /* Deduce size of array from initialization, if not already known. */
3320 if (TREE_CODE (type) == ARRAY_TYPE
3321 && TYPE_DOMAIN (type) == 0
3322 && TREE_CODE (decl) != TYPE_DECL)
3324 bool do_default
3325 = (TREE_STATIC (decl)
3326 /* Even if pedantic, an external linkage array
3327 may have incomplete type at first. */
3328 ? pedantic && !TREE_PUBLIC (decl)
3329 : !DECL_EXTERNAL (decl));
3330 int failure
3331 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3332 do_default);
3334 /* Get the completed type made by complete_array_type. */
3335 type = TREE_TYPE (decl);
3337 switch (failure)
3339 case 1:
3340 error ("initializer fails to determine size of %q+D", decl);
3341 break;
3343 case 2:
3344 if (do_default)
3345 error ("array size missing in %q+D", decl);
3346 /* If a `static' var's size isn't known,
3347 make it extern as well as static, so it does not get
3348 allocated.
3349 If it is not `static', then do not mark extern;
3350 finish_incomplete_decl will give it a default size
3351 and it will get allocated. */
3352 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3353 DECL_EXTERNAL (decl) = 1;
3354 break;
3356 case 3:
3357 error ("zero or negative size array %q+D", decl);
3358 break;
3360 case 0:
3361 /* For global variables, update the copy of the type that
3362 exists in the binding. */
3363 if (TREE_PUBLIC (decl))
3365 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3366 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3367 b_ext = b_ext->shadowed;
3368 if (b_ext)
3370 if (b_ext->type)
3371 b_ext->type = composite_type (b_ext->type, type);
3372 else
3373 b_ext->type = type;
3376 break;
3378 default:
3379 gcc_unreachable ();
3382 if (DECL_INITIAL (decl))
3383 TREE_TYPE (DECL_INITIAL (decl)) = type;
3385 layout_decl (decl, 0);
3388 if (TREE_CODE (decl) == VAR_DECL)
3390 if (init && TREE_CODE (init) == CONSTRUCTOR)
3391 add_flexible_array_elts_to_size (decl, init);
3393 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3394 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3395 layout_decl (decl, 0);
3397 if (DECL_SIZE (decl) == 0
3398 /* Don't give an error if we already gave one earlier. */
3399 && TREE_TYPE (decl) != error_mark_node
3400 && (TREE_STATIC (decl)
3401 /* A static variable with an incomplete type
3402 is an error if it is initialized.
3403 Also if it is not file scope.
3404 Otherwise, let it through, but if it is not `extern'
3405 then it may cause an error message later. */
3406 ? (DECL_INITIAL (decl) != 0
3407 || !DECL_FILE_SCOPE_P (decl))
3408 /* An automatic variable with an incomplete type
3409 is an error. */
3410 : !DECL_EXTERNAL (decl)))
3412 error ("storage size of %q+D isn%'t known", decl);
3413 TREE_TYPE (decl) = error_mark_node;
3416 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3417 && DECL_SIZE (decl) != 0)
3419 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3420 constant_expression_warning (DECL_SIZE (decl));
3421 else
3422 error ("storage size of %q+D isn%'t constant", decl);
3425 if (TREE_USED (type))
3426 TREE_USED (decl) = 1;
3429 /* If this is a function and an assembler name is specified, reset DECL_RTL
3430 so we can give it its new name. Also, update built_in_decls if it
3431 was a normal built-in. */
3432 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3434 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3435 set_builtin_user_assembler_name (decl, asmspec);
3436 set_user_assembler_name (decl, asmspec);
3439 /* If #pragma weak was used, mark the decl weak now. */
3440 maybe_apply_pragma_weak (decl);
3442 /* If this is a variable definition, determine its ELF visibility. */
3443 if (TREE_CODE (decl) == VAR_DECL
3444 && TREE_STATIC (decl)
3445 && !DECL_EXTERNAL (decl))
3446 c_determine_visibility (decl);
3448 /* Output the assembler code and/or RTL code for variables and functions,
3449 unless the type is an undefined structure or union.
3450 If not, it will get done when the type is completed. */
3452 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3454 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3455 if (c_dialect_objc ())
3456 objc_check_decl (decl);
3458 if (asmspec)
3460 /* If this is not a static variable, issue a warning.
3461 It doesn't make any sense to give an ASMSPEC for an
3462 ordinary, non-register local variable. Historically,
3463 GCC has accepted -- but ignored -- the ASMSPEC in
3464 this case. */
3465 if (!DECL_FILE_SCOPE_P (decl)
3466 && TREE_CODE (decl) == VAR_DECL
3467 && !C_DECL_REGISTER (decl)
3468 && !TREE_STATIC (decl))
3469 warning (0, "ignoring asm-specifier for non-static local "
3470 "variable %q+D", decl);
3471 else
3472 set_user_assembler_name (decl, asmspec);
3475 if (DECL_FILE_SCOPE_P (decl))
3477 if (DECL_INITIAL (decl) == NULL_TREE
3478 || DECL_INITIAL (decl) == error_mark_node)
3479 /* Don't output anything
3480 when a tentative file-scope definition is seen.
3481 But at end of compilation, do output code for them. */
3482 DECL_DEFER_OUTPUT (decl) = 1;
3483 rest_of_decl_compilation (decl, true, 0);
3485 else
3487 /* In conjunction with an ASMSPEC, the `register'
3488 keyword indicates that we should place the variable
3489 in a particular register. */
3490 if (asmspec && C_DECL_REGISTER (decl))
3492 DECL_HARD_REGISTER (decl) = 1;
3493 /* This cannot be done for a structure with volatile
3494 fields, on which DECL_REGISTER will have been
3495 reset. */
3496 if (!DECL_REGISTER (decl))
3497 error ("cannot put object with volatile field into register");
3500 if (TREE_CODE (decl) != FUNCTION_DECL)
3502 /* If we're building a variable sized type, and we might be
3503 reachable other than via the top of the current binding
3504 level, then create a new BIND_EXPR so that we deallocate
3505 the object at the right time. */
3506 /* Note that DECL_SIZE can be null due to errors. */
3507 if (DECL_SIZE (decl)
3508 && !TREE_CONSTANT (DECL_SIZE (decl))
3509 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3511 tree bind;
3512 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3513 TREE_SIDE_EFFECTS (bind) = 1;
3514 add_stmt (bind);
3515 BIND_EXPR_BODY (bind) = push_stmt_list ();
3517 add_stmt (build_stmt (DECL_EXPR, decl));
3522 if (!DECL_FILE_SCOPE_P (decl))
3524 /* Recompute the RTL of a local array now
3525 if it used to be an incomplete type. */
3526 if (was_incomplete
3527 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3529 /* If we used it already as memory, it must stay in memory. */
3530 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3531 /* If it's still incomplete now, no init will save it. */
3532 if (DECL_SIZE (decl) == 0)
3533 DECL_INITIAL (decl) = 0;
3538 /* If this was marked 'used', be sure it will be output. */
3539 if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3540 mark_decl_referenced (decl);
3542 if (TREE_CODE (decl) == TYPE_DECL)
3544 if (!DECL_FILE_SCOPE_P (decl)
3545 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3546 add_stmt (build_stmt (DECL_EXPR, decl));
3548 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3551 /* At the end of a declaration, throw away any variable type sizes
3552 of types defined inside that declaration. There is no use
3553 computing them in the following function definition. */
3554 if (current_scope == file_scope)
3555 get_pending_sizes ();
3557 /* Install a cleanup (aka destructor) if one was given. */
3558 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3560 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3561 if (attr)
3563 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3564 tree cleanup_decl = lookup_name (cleanup_id);
3565 tree cleanup;
3567 /* Build "cleanup(&decl)" for the destructor. */
3568 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3569 cleanup = build_tree_list (NULL_TREE, cleanup);
3570 cleanup = build_function_call (cleanup_decl, cleanup);
3572 /* Don't warn about decl unused; the cleanup uses it. */
3573 TREE_USED (decl) = 1;
3574 TREE_USED (cleanup_decl) = 1;
3576 /* Initialize EH, if we've been told to do so. */
3577 if (flag_exceptions && !c_eh_initialized_p)
3579 c_eh_initialized_p = true;
3580 eh_personality_libfunc
3581 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3582 ? "__gcc_personality_sj0"
3583 : "__gcc_personality_v0");
3584 default_init_unwind_resume_libfunc ();
3585 using_eh_for_cleanups ();
3588 push_cleanup (decl, cleanup, false);
3593 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3595 tree
3596 grokparm (const struct c_parm *parm)
3598 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3599 NULL);
3601 decl_attributes (&decl, parm->attrs, 0);
3603 return decl;
3606 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3607 and push that on the current scope. */
3609 void
3610 push_parm_decl (const struct c_parm *parm)
3612 tree decl;
3614 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3615 decl_attributes (&decl, parm->attrs, 0);
3617 decl = pushdecl (decl);
3619 finish_decl (decl, NULL_TREE, NULL_TREE);
3622 /* Mark all the parameter declarations to date as forward decls.
3623 Also diagnose use of this extension. */
3625 void
3626 mark_forward_parm_decls (void)
3628 struct c_binding *b;
3630 if (pedantic && !current_scope->warned_forward_parm_decls)
3632 pedwarn ("ISO C forbids forward parameter declarations");
3633 current_scope->warned_forward_parm_decls = true;
3636 for (b = current_scope->bindings; b; b = b->prev)
3637 if (TREE_CODE (b->decl) == PARM_DECL)
3638 TREE_ASM_WRITTEN (b->decl) = 1;
3641 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3642 literal, which may be an incomplete array type completed by the
3643 initializer; INIT is a CONSTRUCTOR that initializes the compound
3644 literal. */
3646 tree
3647 build_compound_literal (tree type, tree init)
3649 /* We do not use start_decl here because we have a type, not a declarator;
3650 and do not use finish_decl because the decl should be stored inside
3651 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3652 tree decl;
3653 tree complit;
3654 tree stmt;
3656 if (type == error_mark_node)
3657 return error_mark_node;
3659 decl = build_decl (VAR_DECL, NULL_TREE, type);
3660 DECL_EXTERNAL (decl) = 0;
3661 TREE_PUBLIC (decl) = 0;
3662 TREE_STATIC (decl) = (current_scope == file_scope);
3663 DECL_CONTEXT (decl) = current_function_decl;
3664 TREE_USED (decl) = 1;
3665 TREE_TYPE (decl) = type;
3666 TREE_READONLY (decl) = TYPE_READONLY (type);
3667 store_init_value (decl, init);
3669 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3671 int failure = complete_array_type (&TREE_TYPE (decl),
3672 DECL_INITIAL (decl), true);
3673 gcc_assert (!failure);
3675 type = TREE_TYPE (decl);
3676 TREE_TYPE (DECL_INITIAL (decl)) = type;
3679 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3680 return error_mark_node;
3682 stmt = build_stmt (DECL_EXPR, decl);
3683 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3684 TREE_SIDE_EFFECTS (complit) = 1;
3686 layout_decl (decl, 0);
3688 if (TREE_STATIC (decl))
3690 /* This decl needs a name for the assembler output. */
3691 set_compound_literal_name (decl);
3692 DECL_DEFER_OUTPUT (decl) = 1;
3693 DECL_COMDAT (decl) = 1;
3694 DECL_ARTIFICIAL (decl) = 1;
3695 DECL_IGNORED_P (decl) = 1;
3696 pushdecl (decl);
3697 rest_of_decl_compilation (decl, 1, 0);
3700 return complit;
3703 /* Determine whether TYPE is a structure with a flexible array member,
3704 or a union containing such a structure (possibly recursively). */
3706 static bool
3707 flexible_array_type_p (tree type)
3709 tree x;
3710 switch (TREE_CODE (type))
3712 case RECORD_TYPE:
3713 x = TYPE_FIELDS (type);
3714 if (x == NULL_TREE)
3715 return false;
3716 while (TREE_CHAIN (x) != NULL_TREE)
3717 x = TREE_CHAIN (x);
3718 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3719 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3720 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3721 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3722 return true;
3723 return false;
3724 case UNION_TYPE:
3725 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3727 if (flexible_array_type_p (TREE_TYPE (x)))
3728 return true;
3730 return false;
3731 default:
3732 return false;
3736 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3737 replacing with appropriate values if they are invalid. */
3738 static void
3739 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3741 tree type_mv;
3742 unsigned int max_width;
3743 unsigned HOST_WIDE_INT w;
3744 const char *name = orig_name ? orig_name: _("<anonymous>");
3746 /* Detect and ignore out of range field width and process valid
3747 field widths. */
3748 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3749 || TREE_CODE (*width) != INTEGER_CST)
3751 error ("bit-field %qs width not an integer constant", name);
3752 *width = integer_one_node;
3754 else
3756 constant_expression_warning (*width);
3757 if (tree_int_cst_sgn (*width) < 0)
3759 error ("negative width in bit-field %qs", name);
3760 *width = integer_one_node;
3762 else if (integer_zerop (*width) && orig_name)
3764 error ("zero width for bit-field %qs", name);
3765 *width = integer_one_node;
3769 /* Detect invalid bit-field type. */
3770 if (TREE_CODE (*type) != INTEGER_TYPE
3771 && TREE_CODE (*type) != BOOLEAN_TYPE
3772 && TREE_CODE (*type) != ENUMERAL_TYPE)
3774 error ("bit-field %qs has invalid type", name);
3775 *type = unsigned_type_node;
3778 type_mv = TYPE_MAIN_VARIANT (*type);
3779 if (pedantic
3780 && !in_system_header
3781 && type_mv != integer_type_node
3782 && type_mv != unsigned_type_node
3783 && type_mv != boolean_type_node)
3784 pedwarn ("type of bit-field %qs is a GCC extension", name);
3786 if (type_mv == boolean_type_node)
3787 max_width = CHAR_TYPE_SIZE;
3788 else
3789 max_width = TYPE_PRECISION (*type);
3791 if (0 < compare_tree_int (*width, max_width))
3793 error ("width of %qs exceeds its type", name);
3794 w = max_width;
3795 *width = build_int_cst (NULL_TREE, w);
3797 else
3798 w = tree_low_cst (*width, 1);
3800 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3802 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3803 if (!lt
3804 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3805 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3806 warning (0, "%qs is narrower than values of its type", name);
3810 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3811 static tree
3812 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3814 /* Extended integer types of the same width as a standard type have
3815 lesser rank, so those of the same width as int promote to int or
3816 unsigned int and are valid for printf formats expecting int or
3817 unsigned int. To avoid such special cases, avoid creating
3818 extended integer types for bit-fields if a standard integer type
3819 is available. */
3820 if (width == TYPE_PRECISION (integer_type_node))
3821 return unsignedp ? unsigned_type_node : integer_type_node;
3822 if (width == TYPE_PRECISION (signed_char_type_node))
3823 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3824 if (width == TYPE_PRECISION (short_integer_type_node))
3825 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3826 if (width == TYPE_PRECISION (long_integer_type_node))
3827 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3828 if (width == TYPE_PRECISION (long_long_integer_type_node))
3829 return (unsignedp ? long_long_unsigned_type_node
3830 : long_long_integer_type_node);
3831 return build_nonstandard_integer_type (width, unsignedp);
3834 /* Given declspecs and a declarator,
3835 determine the name and type of the object declared
3836 and construct a ..._DECL node for it.
3837 (In one case we can return a ..._TYPE node instead.
3838 For invalid input we sometimes return 0.)
3840 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3842 DECL_CONTEXT says which syntactic context this declaration is in:
3843 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3844 FUNCDEF for a function definition. Like NORMAL but a few different
3845 error messages in each case. Return value may be zero meaning
3846 this definition is too screwy to try to parse.
3847 PARM for a parameter declaration (either within a function prototype
3848 or before a function body). Make a PARM_DECL, or return void_type_node.
3849 TYPENAME if for a typename (in a cast or sizeof).
3850 Don't make a DECL node; just return the ..._TYPE node.
3851 FIELD for a struct or union field; make a FIELD_DECL.
3852 INITIALIZED is true if the decl has an initializer.
3853 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3854 representing the width of the bit-field.
3856 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3857 It may also be so in the PARM case, for a prototype where the
3858 argument type is specified but not the name.
3860 This function is where the complicated C meanings of `static'
3861 and `extern' are interpreted. */
3863 static tree
3864 grokdeclarator (const struct c_declarator *declarator,
3865 struct c_declspecs *declspecs,
3866 enum decl_context decl_context, bool initialized, tree *width)
3868 tree type = declspecs->type;
3869 bool threadp = declspecs->thread_p;
3870 enum c_storage_class storage_class = declspecs->storage_class;
3871 int constp;
3872 int restrictp;
3873 int volatilep;
3874 int type_quals = TYPE_UNQUALIFIED;
3875 const char *name, *orig_name;
3876 tree typedef_type = 0;
3877 int funcdef_flag = 0;
3878 bool funcdef_syntax = false;
3879 int size_varies = 0;
3880 tree decl_attr = declspecs->decl_attr;
3881 int array_ptr_quals = TYPE_UNQUALIFIED;
3882 tree array_ptr_attrs = NULL_TREE;
3883 int array_parm_static = 0;
3884 tree returned_attrs = NULL_TREE;
3885 bool bitfield = width != NULL;
3886 tree element_type;
3887 struct c_arg_info *arg_info = 0;
3889 if (decl_context == FUNCDEF)
3890 funcdef_flag = 1, decl_context = NORMAL;
3892 /* Look inside a declarator for the name being declared
3893 and get it as a string, for an error message. */
3895 const struct c_declarator *decl = declarator;
3896 name = 0;
3898 while (decl)
3899 switch (decl->kind)
3901 case cdk_function:
3902 case cdk_array:
3903 case cdk_pointer:
3904 funcdef_syntax = (decl->kind == cdk_function);
3905 decl = decl->declarator;
3906 break;
3908 case cdk_attrs:
3909 decl = decl->declarator;
3910 break;
3912 case cdk_id:
3913 if (decl->u.id)
3914 name = IDENTIFIER_POINTER (decl->u.id);
3915 decl = 0;
3916 break;
3918 default:
3919 gcc_unreachable ();
3921 orig_name = name;
3922 if (name == 0)
3923 name = "type name";
3926 /* A function definition's declarator must have the form of
3927 a function declarator. */
3929 if (funcdef_flag && !funcdef_syntax)
3930 return 0;
3932 /* If this looks like a function definition, make it one,
3933 even if it occurs where parms are expected.
3934 Then store_parm_decls will reject it and not use it as a parm. */
3935 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3936 decl_context = PARM;
3938 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3939 warn_deprecated_use (declspecs->type);
3941 if ((decl_context == NORMAL || decl_context == FIELD)
3942 && current_scope == file_scope
3943 && variably_modified_type_p (type, NULL_TREE))
3945 error ("variably modified %qs at file scope", name);
3946 type = integer_type_node;
3949 typedef_type = type;
3950 size_varies = C_TYPE_VARIABLE_SIZE (type);
3952 /* Diagnose defaulting to "int". */
3954 if (declspecs->default_int_p && !in_system_header)
3956 /* Issue a warning if this is an ISO C 99 program or if
3957 -Wreturn-type and this is a function, or if -Wimplicit;
3958 prefer the former warning since it is more explicit. */
3959 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3960 && funcdef_flag)
3961 warn_about_return_type = 1;
3962 else if (warn_implicit_int || flag_isoc99)
3963 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3966 /* Adjust the type if a bit-field is being declared,
3967 -funsigned-bitfields applied and the type is not explicitly
3968 "signed". */
3969 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3970 && TREE_CODE (type) == INTEGER_TYPE)
3971 type = c_common_unsigned_type (type);
3973 /* Figure out the type qualifiers for the declaration. There are
3974 two ways a declaration can become qualified. One is something
3975 like `const int i' where the `const' is explicit. Another is
3976 something like `typedef const int CI; CI i' where the type of the
3977 declaration contains the `const'. A third possibility is that
3978 there is a type qualifier on the element type of a typedefed
3979 array type, in which case we should extract that qualifier so
3980 that c_apply_type_quals_to_decls receives the full list of
3981 qualifiers to work with (C90 is not entirely clear about whether
3982 duplicate qualifiers should be diagnosed in this case, but it
3983 seems most appropriate to do so). */
3984 element_type = strip_array_types (type);
3985 constp = declspecs->const_p + TYPE_READONLY (element_type);
3986 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3987 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3988 if (pedantic && !flag_isoc99)
3990 if (constp > 1)
3991 pedwarn ("duplicate %<const%>");
3992 if (restrictp > 1)
3993 pedwarn ("duplicate %<restrict%>");
3994 if (volatilep > 1)
3995 pedwarn ("duplicate %<volatile%>");
3997 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
3998 type = TYPE_MAIN_VARIANT (type);
3999 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4000 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4001 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4003 /* Warn about storage classes that are invalid for certain
4004 kinds of declarations (parameters, typenames, etc.). */
4006 if (funcdef_flag
4007 && (threadp
4008 || storage_class == csc_auto
4009 || storage_class == csc_register
4010 || storage_class == csc_typedef))
4012 if (storage_class == csc_auto
4013 && (pedantic || current_scope == file_scope))
4014 pedwarn ("function definition declared %<auto%>");
4015 if (storage_class == csc_register)
4016 error ("function definition declared %<register%>");
4017 if (storage_class == csc_typedef)
4018 error ("function definition declared %<typedef%>");
4019 if (threadp)
4020 error ("function definition declared %<__thread%>");
4021 threadp = false;
4022 if (storage_class == csc_auto
4023 || storage_class == csc_register
4024 || storage_class == csc_typedef)
4025 storage_class = csc_none;
4027 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4029 if (decl_context == PARM && storage_class == csc_register)
4031 else
4033 switch (decl_context)
4035 case FIELD:
4036 error ("storage class specified for structure field %qs",
4037 name);
4038 break;
4039 case PARM:
4040 error ("storage class specified for parameter %qs", name);
4041 break;
4042 default:
4043 error ("storage class specified for typename");
4044 break;
4046 storage_class = csc_none;
4047 threadp = false;
4050 else if (storage_class == csc_extern
4051 && initialized
4052 && !funcdef_flag)
4054 /* 'extern' with initialization is invalid if not at file scope. */
4055 if (current_scope == file_scope)
4056 warning (0, "%qs initialized and declared %<extern%>", name);
4057 else
4058 error ("%qs has both %<extern%> and initializer", name);
4060 else if (current_scope == file_scope)
4062 if (storage_class == csc_auto)
4063 error ("file-scope declaration of %qs specifies %<auto%>", name);
4064 if (pedantic && storage_class == csc_register)
4065 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
4067 else
4069 if (storage_class == csc_extern && funcdef_flag)
4070 error ("nested function %qs declared %<extern%>", name);
4071 else if (threadp && storage_class == csc_none)
4073 error ("function-scope %qs implicitly auto and declared "
4074 "%<__thread%>",
4075 name);
4076 threadp = false;
4080 /* Now figure out the structure of the declarator proper.
4081 Descend through it, creating more complex types, until we reach
4082 the declared identifier (or NULL_TREE, in an absolute declarator).
4083 At each stage we maintain an unqualified version of the type
4084 together with any qualifiers that should be applied to it with
4085 c_build_qualified_type; this way, array types including
4086 multidimensional array types are first built up in unqualified
4087 form and then the qualified form is created with
4088 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4090 while (declarator && declarator->kind != cdk_id)
4092 if (type == error_mark_node)
4094 declarator = declarator->declarator;
4095 continue;
4098 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4099 a cdk_pointer (for *...),
4100 a cdk_function (for ...(...)),
4101 a cdk_attrs (for nested attributes),
4102 or a cdk_id (for the name being declared
4103 or the place in an absolute declarator
4104 where the name was omitted).
4105 For the last case, we have just exited the loop.
4107 At this point, TYPE is the type of elements of an array,
4108 or for a function to return, or for a pointer to point to.
4109 After this sequence of ifs, TYPE is the type of the
4110 array or function or pointer, and DECLARATOR has had its
4111 outermost layer removed. */
4113 if (array_ptr_quals != TYPE_UNQUALIFIED
4114 || array_ptr_attrs != NULL_TREE
4115 || array_parm_static)
4117 /* Only the innermost declarator (making a parameter be of
4118 array type which is converted to pointer type)
4119 may have static or type qualifiers. */
4120 error ("static or type qualifiers in non-parameter array declarator");
4121 array_ptr_quals = TYPE_UNQUALIFIED;
4122 array_ptr_attrs = NULL_TREE;
4123 array_parm_static = 0;
4126 switch (declarator->kind)
4128 case cdk_attrs:
4130 /* A declarator with embedded attributes. */
4131 tree attrs = declarator->u.attrs;
4132 const struct c_declarator *inner_decl;
4133 int attr_flags = 0;
4134 declarator = declarator->declarator;
4135 inner_decl = declarator;
4136 while (inner_decl->kind == cdk_attrs)
4137 inner_decl = inner_decl->declarator;
4138 if (inner_decl->kind == cdk_id)
4139 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4140 else if (inner_decl->kind == cdk_function)
4141 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4142 else if (inner_decl->kind == cdk_array)
4143 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4144 returned_attrs = decl_attributes (&type,
4145 chainon (returned_attrs, attrs),
4146 attr_flags);
4147 break;
4149 case cdk_array:
4151 tree itype = NULL_TREE;
4152 tree size = declarator->u.array.dimen;
4153 /* The index is a signed object `sizetype' bits wide. */
4154 tree index_type = c_common_signed_type (sizetype);
4156 array_ptr_quals = declarator->u.array.quals;
4157 array_ptr_attrs = declarator->u.array.attrs;
4158 array_parm_static = declarator->u.array.static_p;
4160 declarator = declarator->declarator;
4162 /* Check for some types that there cannot be arrays of. */
4164 if (VOID_TYPE_P (type))
4166 error ("declaration of %qs as array of voids", name);
4167 type = error_mark_node;
4170 if (TREE_CODE (type) == FUNCTION_TYPE)
4172 error ("declaration of %qs as array of functions", name);
4173 type = error_mark_node;
4176 if (pedantic && !in_system_header && flexible_array_type_p (type))
4177 pedwarn ("invalid use of structure with flexible array member");
4179 if (size == error_mark_node)
4180 type = error_mark_node;
4182 if (type == error_mark_node)
4183 continue;
4185 /* If size was specified, set ITYPE to a range-type for
4186 that size. Otherwise, ITYPE remains null. finish_decl
4187 may figure it out from an initial value. */
4189 if (size)
4191 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4192 lvalue. */
4193 STRIP_TYPE_NOPS (size);
4195 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4197 error ("size of array %qs has non-integer type", name);
4198 size = integer_one_node;
4201 if (pedantic && integer_zerop (size))
4202 pedwarn ("ISO C forbids zero-size array %qs", name);
4204 if (TREE_CODE (size) == INTEGER_CST)
4206 constant_expression_warning (size);
4207 if (tree_int_cst_sgn (size) < 0)
4209 error ("size of array %qs is negative", name);
4210 size = integer_one_node;
4213 else if ((decl_context == NORMAL || decl_context == FIELD)
4214 && current_scope == file_scope)
4216 error ("variably modified %qs at file scope", name);
4217 size = integer_one_node;
4219 else
4221 /* Make sure the array size remains visibly
4222 nonconstant even if it is (eg) a const variable
4223 with known value. */
4224 size_varies = 1;
4226 if (!flag_isoc99 && pedantic)
4228 if (TREE_CONSTANT (size))
4229 pedwarn ("ISO C90 forbids array %qs whose size "
4230 "can%'t be evaluated",
4231 name);
4232 else
4233 pedwarn ("ISO C90 forbids variable-size array %qs",
4234 name);
4238 if (integer_zerop (size))
4240 /* A zero-length array cannot be represented with
4241 an unsigned index type, which is what we'll
4242 get with build_index_type. Create an
4243 open-ended range instead. */
4244 itype = build_range_type (sizetype, size, NULL_TREE);
4246 else
4248 /* Arrange for the SAVE_EXPR on the inside of the
4249 MINUS_EXPR, which allows the -1 to get folded
4250 with the +1 that happens when building TYPE_SIZE. */
4251 if (size_varies)
4252 size = variable_size (size);
4254 /* Compute the maximum valid index, that is, size
4255 - 1. Do the calculation in index_type, so that
4256 if it is a variable the computations will be
4257 done in the proper mode. */
4258 itype = fold_build2 (MINUS_EXPR, index_type,
4259 convert (index_type, size),
4260 convert (index_type,
4261 size_one_node));
4263 /* If that overflowed, the array is too big. ???
4264 While a size of INT_MAX+1 technically shouldn't
4265 cause an overflow (because we subtract 1), the
4266 overflow is recorded during the conversion to
4267 index_type, before the subtraction. Handling
4268 this case seems like an unnecessary
4269 complication. */
4270 if (TREE_CODE (itype) == INTEGER_CST
4271 && TREE_OVERFLOW (itype))
4273 error ("size of array %qs is too large", name);
4274 type = error_mark_node;
4275 continue;
4278 itype = build_index_type (itype);
4281 else if (decl_context == FIELD)
4283 if (pedantic && !flag_isoc99 && !in_system_header)
4284 pedwarn ("ISO C90 does not support flexible array members");
4286 /* ISO C99 Flexible array members are effectively
4287 identical to GCC's zero-length array extension. */
4288 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4291 /* Complain about arrays of incomplete types. */
4292 if (!COMPLETE_TYPE_P (type))
4294 error ("array type has incomplete element type");
4295 type = error_mark_node;
4297 else
4298 /* When itype is NULL, a shared incomplete array type is
4299 returned for all array of a given type. Elsewhere we
4300 make sure we don't complete that type before copying
4301 it, but here we want to make sure we don't ever
4302 modify the shared type, so we gcc_assert (itype)
4303 below. */
4304 type = build_array_type (type, itype);
4306 if (type != error_mark_node)
4308 if (size_varies)
4310 /* It is ok to modify type here even if itype is
4311 NULL: if size_varies, we're in a
4312 multi-dimentional array and the inner type has
4313 variable size, so the enclosing shared array type
4314 must too. */
4315 if (size && TREE_CODE (size) == INTEGER_CST)
4316 type
4317 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4318 C_TYPE_VARIABLE_SIZE (type) = 1;
4321 /* The GCC extension for zero-length arrays differs from
4322 ISO flexible array members in that sizeof yields
4323 zero. */
4324 if (size && integer_zerop (size))
4326 gcc_assert (itype);
4327 TYPE_SIZE (type) = bitsize_zero_node;
4328 TYPE_SIZE_UNIT (type) = size_zero_node;
4332 if (decl_context != PARM
4333 && (array_ptr_quals != TYPE_UNQUALIFIED
4334 || array_ptr_attrs != NULL_TREE
4335 || array_parm_static))
4337 error ("static or type qualifiers in non-parameter array declarator");
4338 array_ptr_quals = TYPE_UNQUALIFIED;
4339 array_ptr_attrs = NULL_TREE;
4340 array_parm_static = 0;
4342 break;
4344 case cdk_function:
4346 /* Say it's a definition only for the declarator closest
4347 to the identifier, apart possibly from some
4348 attributes. */
4349 bool really_funcdef = false;
4350 tree arg_types;
4351 if (funcdef_flag)
4353 const struct c_declarator *t = declarator->declarator;
4354 while (t->kind == cdk_attrs)
4355 t = t->declarator;
4356 really_funcdef = (t->kind == cdk_id);
4359 /* Declaring a function type. Make sure we have a valid
4360 type for the function to return. */
4361 if (type == error_mark_node)
4362 continue;
4364 size_varies = 0;
4366 /* Warn about some types functions can't return. */
4367 if (TREE_CODE (type) == FUNCTION_TYPE)
4369 error ("%qs declared as function returning a function", name);
4370 type = integer_type_node;
4372 if (TREE_CODE (type) == ARRAY_TYPE)
4374 error ("%qs declared as function returning an array", name);
4375 type = integer_type_node;
4378 /* Construct the function type and go to the next
4379 inner layer of declarator. */
4380 arg_info = declarator->u.arg_info;
4381 arg_types = grokparms (arg_info, really_funcdef);
4383 /* Type qualifiers before the return type of the function
4384 qualify the return type, not the function type. */
4385 if (type_quals)
4387 /* Type qualifiers on a function return type are
4388 normally permitted by the standard but have no
4389 effect, so give a warning at -Wreturn-type.
4390 Qualifiers on a void return type are banned on
4391 function definitions in ISO C; GCC used to used
4392 them for noreturn functions. */
4393 if (VOID_TYPE_P (type) && really_funcdef)
4394 pedwarn ("function definition has qualified void return type");
4395 else
4396 warning (OPT_Wreturn_type,
4397 "type qualifiers ignored on function return type");
4399 type = c_build_qualified_type (type, type_quals);
4401 type_quals = TYPE_UNQUALIFIED;
4403 type = build_function_type (type, arg_types);
4404 declarator = declarator->declarator;
4406 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4407 the formal parameter list of this FUNCTION_TYPE to point to
4408 the FUNCTION_TYPE node itself. */
4410 tree link;
4412 for (link = arg_info->tags;
4413 link;
4414 link = TREE_CHAIN (link))
4415 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4417 break;
4419 case cdk_pointer:
4421 /* Merge any constancy or volatility into the target type
4422 for the pointer. */
4424 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4425 && type_quals)
4426 pedwarn ("ISO C forbids qualified function types");
4427 if (type_quals)
4428 type = c_build_qualified_type (type, type_quals);
4429 size_varies = 0;
4431 type = build_pointer_type (type);
4433 /* Process type qualifiers (such as const or volatile)
4434 that were given inside the `*'. */
4435 type_quals = declarator->u.pointer_quals;
4437 declarator = declarator->declarator;
4438 break;
4440 default:
4441 gcc_unreachable ();
4445 /* Now TYPE has the actual type, apart from any qualifiers in
4446 TYPE_QUALS. */
4448 /* Check the type and width of a bit-field. */
4449 if (bitfield)
4450 check_bitfield_type_and_width (&type, width, orig_name);
4452 /* Did array size calculations overflow? */
4454 if (TREE_CODE (type) == ARRAY_TYPE
4455 && COMPLETE_TYPE_P (type)
4456 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4457 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4459 error ("size of array %qs is too large", name);
4460 /* If we proceed with the array type as it is, we'll eventually
4461 crash in tree_low_cst(). */
4462 type = error_mark_node;
4465 /* If this is declaring a typedef name, return a TYPE_DECL. */
4467 if (storage_class == csc_typedef)
4469 tree decl;
4470 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4471 && type_quals)
4472 pedwarn ("ISO C forbids qualified function types");
4473 if (type_quals)
4474 type = c_build_qualified_type (type, type_quals);
4475 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4476 if (declspecs->explicit_signed_p)
4477 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4478 decl_attributes (&decl, returned_attrs, 0);
4479 if (declspecs->inline_p)
4480 pedwarn ("typedef %q+D declared %<inline%>", decl);
4481 return decl;
4484 /* If this is a type name (such as, in a cast or sizeof),
4485 compute the type and return it now. */
4487 if (decl_context == TYPENAME)
4489 /* Note that the grammar rejects storage classes in typenames
4490 and fields. */
4491 gcc_assert (storage_class == csc_none && !threadp
4492 && !declspecs->inline_p);
4493 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4494 && type_quals)
4495 pedwarn ("ISO C forbids const or volatile function types");
4496 if (type_quals)
4497 type = c_build_qualified_type (type, type_quals);
4498 decl_attributes (&type, returned_attrs, 0);
4499 return type;
4502 /* Aside from typedefs and type names (handle above),
4503 `void' at top level (not within pointer)
4504 is allowed only in public variables.
4505 We don't complain about parms either, but that is because
4506 a better error message can be made later. */
4508 if (VOID_TYPE_P (type) && decl_context != PARM
4509 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4510 && (storage_class == csc_extern
4511 || (current_scope == file_scope
4512 && !(storage_class == csc_static
4513 || storage_class == csc_register)))))
4515 error ("variable or field %qs declared void", name);
4516 type = integer_type_node;
4519 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4520 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4523 tree decl;
4525 if (decl_context == PARM)
4527 tree type_as_written;
4528 tree promoted_type;
4530 /* A parameter declared as an array of T is really a pointer to T.
4531 One declared as a function is really a pointer to a function. */
4533 if (TREE_CODE (type) == ARRAY_TYPE)
4535 /* Transfer const-ness of array into that of type pointed to. */
4536 type = TREE_TYPE (type);
4537 if (type_quals)
4538 type = c_build_qualified_type (type, type_quals);
4539 type = build_pointer_type (type);
4540 type_quals = array_ptr_quals;
4542 /* We don't yet implement attributes in this context. */
4543 if (array_ptr_attrs != NULL_TREE)
4544 warning (OPT_Wattributes,
4545 "attributes in parameter array declarator ignored");
4547 size_varies = 0;
4549 else if (TREE_CODE (type) == FUNCTION_TYPE)
4551 if (pedantic && type_quals)
4552 pedwarn ("ISO C forbids qualified function types");
4553 if (type_quals)
4554 type = c_build_qualified_type (type, type_quals);
4555 type = build_pointer_type (type);
4556 type_quals = TYPE_UNQUALIFIED;
4558 else if (type_quals)
4559 type = c_build_qualified_type (type, type_quals);
4561 type_as_written = type;
4563 decl = build_decl (PARM_DECL, declarator->u.id, type);
4564 if (size_varies)
4565 C_DECL_VARIABLE_SIZE (decl) = 1;
4567 /* Compute the type actually passed in the parmlist,
4568 for the case where there is no prototype.
4569 (For example, shorts and chars are passed as ints.)
4570 When there is a prototype, this is overridden later. */
4572 if (type == error_mark_node)
4573 promoted_type = type;
4574 else
4575 promoted_type = c_type_promotes_to (type);
4577 DECL_ARG_TYPE (decl) = promoted_type;
4578 if (declspecs->inline_p)
4579 pedwarn ("parameter %q+D declared %<inline%>", decl);
4581 else if (decl_context == FIELD)
4583 /* Note that the grammar rejects storage classes in typenames
4584 and fields. */
4585 gcc_assert (storage_class == csc_none && !threadp
4586 && !declspecs->inline_p);
4588 /* Structure field. It may not be a function. */
4590 if (TREE_CODE (type) == FUNCTION_TYPE)
4592 error ("field %qs declared as a function", name);
4593 type = build_pointer_type (type);
4595 else if (TREE_CODE (type) != ERROR_MARK
4596 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4598 error ("field %qs has incomplete type", name);
4599 type = error_mark_node;
4601 type = c_build_qualified_type (type, type_quals);
4602 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4603 DECL_NONADDRESSABLE_P (decl) = bitfield;
4605 if (size_varies)
4606 C_DECL_VARIABLE_SIZE (decl) = 1;
4608 else if (TREE_CODE (type) == FUNCTION_TYPE)
4610 if (storage_class == csc_register || threadp)
4612 error ("invalid storage class for function %qs", name);
4614 else if (current_scope != file_scope)
4616 /* Function declaration not at file scope. Storage
4617 classes other than `extern' are not allowed, C99
4618 6.7.1p5, and `extern' makes no difference. However,
4619 GCC allows 'auto', perhaps with 'inline', to support
4620 nested functions. */
4621 if (storage_class == csc_auto)
4623 if (pedantic)
4624 pedwarn ("invalid storage class for function %qs", name);
4626 else if (storage_class == csc_static)
4628 error ("invalid storage class for function %qs", name);
4629 if (funcdef_flag)
4630 storage_class = declspecs->storage_class = csc_none;
4631 else
4632 return 0;
4636 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4637 decl = build_decl_attribute_variant (decl, decl_attr);
4639 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4641 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4642 pedwarn ("ISO C forbids qualified function types");
4644 /* GNU C interprets a volatile-qualified function type to indicate
4645 that the function does not return. */
4646 if ((type_quals & TYPE_QUAL_VOLATILE)
4647 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4648 warning (0, "%<noreturn%> function returns non-void value");
4650 /* Every function declaration is an external reference
4651 (DECL_EXTERNAL) except for those which are not at file
4652 scope and are explicitly declared "auto". This is
4653 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4654 GCC to signify a forward declaration of a nested function. */
4655 if (storage_class == csc_auto && current_scope != file_scope)
4656 DECL_EXTERNAL (decl) = 0;
4657 else
4658 DECL_EXTERNAL (decl) = 1;
4660 /* Record absence of global scope for `static' or `auto'. */
4661 TREE_PUBLIC (decl)
4662 = !(storage_class == csc_static || storage_class == csc_auto);
4664 /* For a function definition, record the argument information
4665 block where store_parm_decls will look for it. */
4666 if (funcdef_flag)
4667 current_function_arg_info = arg_info;
4669 if (declspecs->default_int_p)
4670 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4672 /* Record presence of `inline', if it is reasonable. */
4673 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4675 if (declspecs->inline_p)
4676 pedwarn ("cannot inline function %<main%>");
4678 else if (declspecs->inline_p)
4680 /* Record that the function is declared `inline'. */
4681 DECL_DECLARED_INLINE_P (decl) = 1;
4683 /* Do not mark bare declarations as DECL_INLINE. Doing so
4684 in the presence of multiple declarations can result in
4685 the abstract origin pointing between the declarations,
4686 which will confuse dwarf2out. */
4687 if (initialized)
4689 DECL_INLINE (decl) = 1;
4690 if (storage_class == csc_extern)
4691 current_extern_inline = 1;
4694 /* If -finline-functions, assume it can be inlined. This does
4695 two things: let the function be deferred until it is actually
4696 needed, and let dwarf2 know that the function is inlinable. */
4697 else if (flag_inline_trees == 2 && initialized)
4698 DECL_INLINE (decl) = 1;
4700 else
4702 /* It's a variable. */
4703 /* An uninitialized decl with `extern' is a reference. */
4704 int extern_ref = !initialized && storage_class == csc_extern;
4706 type = c_build_qualified_type (type, type_quals);
4708 /* C99 6.2.2p7: It is invalid (compile-time undefined
4709 behavior) to create an 'extern' declaration for a
4710 variable if there is a global declaration that is
4711 'static' and the global declaration is not visible.
4712 (If the static declaration _is_ currently visible,
4713 the 'extern' declaration is taken to refer to that decl.) */
4714 if (extern_ref && current_scope != file_scope)
4716 tree global_decl = identifier_global_value (declarator->u.id);
4717 tree visible_decl = lookup_name (declarator->u.id);
4719 if (global_decl
4720 && global_decl != visible_decl
4721 && TREE_CODE (global_decl) == VAR_DECL
4722 && !TREE_PUBLIC (global_decl))
4723 error ("variable previously declared %<static%> redeclared "
4724 "%<extern%>");
4727 decl = build_decl (VAR_DECL, declarator->u.id, type);
4728 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4729 if (size_varies)
4730 C_DECL_VARIABLE_SIZE (decl) = 1;
4732 if (declspecs->inline_p)
4733 pedwarn ("variable %q+D declared %<inline%>", decl);
4735 /* At file scope, an initialized extern declaration may follow
4736 a static declaration. In that case, DECL_EXTERNAL will be
4737 reset later in start_decl. */
4738 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4740 /* At file scope, the presence of a `static' or `register' storage
4741 class specifier, or the absence of all storage class specifiers
4742 makes this declaration a definition (perhaps tentative). Also,
4743 the absence of `static' makes it public. */
4744 if (current_scope == file_scope)
4746 TREE_PUBLIC (decl) = storage_class != csc_static;
4747 TREE_STATIC (decl) = !extern_ref;
4749 /* Not at file scope, only `static' makes a static definition. */
4750 else
4752 TREE_STATIC (decl) = (storage_class == csc_static);
4753 TREE_PUBLIC (decl) = extern_ref;
4756 if (threadp)
4758 if (targetm.have_tls)
4759 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
4760 else
4761 /* A mere warning is sure to result in improper semantics
4762 at runtime. Don't bother to allow this to compile. */
4763 error ("thread-local storage not supported for this target");
4767 /* Record `register' declaration for warnings on &
4768 and in case doing stupid register allocation. */
4770 if (storage_class == csc_register)
4772 C_DECL_REGISTER (decl) = 1;
4773 DECL_REGISTER (decl) = 1;
4776 /* Record constancy and volatility. */
4777 c_apply_type_quals_to_decl (type_quals, decl);
4779 /* If a type has volatile components, it should be stored in memory.
4780 Otherwise, the fact that those components are volatile
4781 will be ignored, and would even crash the compiler.
4782 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
4783 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
4784 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
4785 || TREE_CODE (decl) == RESULT_DECL))
4787 /* It is not an error for a structure with volatile fields to
4788 be declared register, but reset DECL_REGISTER since it
4789 cannot actually go in a register. */
4790 int was_reg = C_DECL_REGISTER (decl);
4791 C_DECL_REGISTER (decl) = 0;
4792 DECL_REGISTER (decl) = 0;
4793 c_mark_addressable (decl);
4794 C_DECL_REGISTER (decl) = was_reg;
4797 /* This is the earliest point at which we might know the assembler
4798 name of a variable. Thus, if it's known before this, die horribly. */
4799 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4801 decl_attributes (&decl, returned_attrs, 0);
4803 return decl;
4807 /* Decode the parameter-list info for a function type or function definition.
4808 The argument is the value returned by `get_parm_info' (or made in parse.y
4809 if there is an identifier list instead of a parameter decl list).
4810 These two functions are separate because when a function returns
4811 or receives functions then each is called multiple times but the order
4812 of calls is different. The last call to `grokparms' is always the one
4813 that contains the formal parameter names of a function definition.
4815 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4817 FUNCDEF_FLAG is true for a function definition, false for
4818 a mere declaration. A nonempty identifier-list gets an error message
4819 when FUNCDEF_FLAG is false. */
4821 static tree
4822 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4824 tree arg_types = arg_info->types;
4826 if (arg_types == 0 && !funcdef_flag && !in_system_header)
4827 warning (OPT_Wstrict_prototypes,
4828 "function declaration isn%'t a prototype");
4830 if (arg_types == error_mark_node)
4831 return 0; /* don't set TYPE_ARG_TYPES in this case */
4833 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4835 if (!funcdef_flag)
4836 pedwarn ("parameter names (without types) in function declaration");
4838 arg_info->parms = arg_info->types;
4839 arg_info->types = 0;
4840 return 0;
4842 else
4844 tree parm, type, typelt;
4845 unsigned int parmno;
4847 /* If there is a parameter of incomplete type in a definition,
4848 this is an error. In a declaration this is valid, and a
4849 struct or union type may be completed later, before any calls
4850 or definition of the function. In the case where the tag was
4851 first declared within the parameter list, a warning has
4852 already been given. If a parameter has void type, then
4853 however the function cannot be defined or called, so
4854 warn. */
4856 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4857 parm;
4858 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4860 type = TREE_VALUE (typelt);
4861 if (type == error_mark_node)
4862 continue;
4864 if (!COMPLETE_TYPE_P (type))
4866 if (funcdef_flag)
4868 if (DECL_NAME (parm))
4869 error ("parameter %u (%q+D) has incomplete type",
4870 parmno, parm);
4871 else
4872 error ("%Jparameter %u has incomplete type",
4873 parm, parmno);
4875 TREE_VALUE (typelt) = error_mark_node;
4876 TREE_TYPE (parm) = error_mark_node;
4878 else if (VOID_TYPE_P (type))
4880 if (DECL_NAME (parm))
4881 warning (0, "parameter %u (%q+D) has void type",
4882 parmno, parm);
4883 else
4884 warning (0, "%Jparameter %u has void type",
4885 parm, parmno);
4889 if (DECL_NAME (parm) && TREE_USED (parm))
4890 warn_if_shadowing (parm);
4892 return arg_types;
4896 /* Take apart the current scope and return a c_arg_info structure with
4897 info on a parameter list just parsed.
4899 This structure is later fed to 'grokparms' and 'store_parm_decls'.
4901 ELLIPSIS being true means the argument list ended in '...' so don't
4902 append a sentinel (void_list_node) to the end of the type-list. */
4904 struct c_arg_info *
4905 get_parm_info (bool ellipsis)
4907 struct c_binding *b = current_scope->bindings;
4908 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
4909 struct c_arg_info);
4910 tree parms = 0;
4911 tree tags = 0;
4912 tree types = 0;
4913 tree others = 0;
4915 static bool explained_incomplete_types = false;
4916 bool gave_void_only_once_err = false;
4918 arg_info->parms = 0;
4919 arg_info->tags = 0;
4920 arg_info->types = 0;
4921 arg_info->others = 0;
4923 /* The bindings in this scope must not get put into a block.
4924 We will take care of deleting the binding nodes. */
4925 current_scope->bindings = 0;
4927 /* This function is only called if there was *something* on the
4928 parameter list. */
4929 gcc_assert (b);
4931 /* A parameter list consisting solely of 'void' indicates that the
4932 function takes no arguments. But if the 'void' is qualified
4933 (by 'const' or 'volatile'), or has a storage class specifier
4934 ('register'), then the behavior is undefined; issue an error.
4935 Typedefs for 'void' are OK (see DR#157). */
4936 if (b->prev == 0 /* one binding */
4937 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4938 && !DECL_NAME (b->decl) /* anonymous */
4939 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4941 if (TREE_THIS_VOLATILE (b->decl)
4942 || TREE_READONLY (b->decl)
4943 || C_DECL_REGISTER (b->decl))
4944 error ("%<void%> as only parameter may not be qualified");
4946 /* There cannot be an ellipsis. */
4947 if (ellipsis)
4948 error ("%<void%> must be the only parameter");
4950 arg_info->types = void_list_node;
4951 return arg_info;
4954 if (!ellipsis)
4955 types = void_list_node;
4957 /* Break up the bindings list into parms, tags, types, and others;
4958 apply sanity checks; purge the name-to-decl bindings. */
4959 while (b)
4961 tree decl = b->decl;
4962 tree type = TREE_TYPE (decl);
4963 const char *keyword;
4965 switch (TREE_CODE (decl))
4967 case PARM_DECL:
4968 if (b->id)
4970 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4971 I_SYMBOL_BINDING (b->id) = b->shadowed;
4974 /* Check for forward decls that never got their actual decl. */
4975 if (TREE_ASM_WRITTEN (decl))
4976 error ("parameter %q+D has just a forward declaration", decl);
4977 /* Check for (..., void, ...) and issue an error. */
4978 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4980 if (!gave_void_only_once_err)
4982 error ("%<void%> must be the only parameter");
4983 gave_void_only_once_err = true;
4986 else
4988 /* Valid parameter, add it to the list. */
4989 TREE_CHAIN (decl) = parms;
4990 parms = decl;
4992 /* Since there is a prototype, args are passed in their
4993 declared types. The back end may override this later. */
4994 DECL_ARG_TYPE (decl) = type;
4995 types = tree_cons (0, type, types);
4997 break;
4999 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5000 case UNION_TYPE: keyword = "union"; goto tag;
5001 case RECORD_TYPE: keyword = "struct"; goto tag;
5002 tag:
5003 /* Types may not have tag-names, in which case the type
5004 appears in the bindings list with b->id NULL. */
5005 if (b->id)
5007 gcc_assert (I_TAG_BINDING (b->id) == b);
5008 I_TAG_BINDING (b->id) = b->shadowed;
5011 /* Warn about any struct, union or enum tags defined in a
5012 parameter list. The scope of such types is limited to
5013 the parameter list, which is rarely if ever desirable
5014 (it's impossible to call such a function with type-
5015 correct arguments). An anonymous union parm type is
5016 meaningful as a GNU extension, so don't warn for that. */
5017 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5019 if (b->id)
5020 /* The %s will be one of 'struct', 'union', or 'enum'. */
5021 warning (0, "%<%s %E%> declared inside parameter list",
5022 keyword, b->id);
5023 else
5024 /* The %s will be one of 'struct', 'union', or 'enum'. */
5025 warning (0, "anonymous %s declared inside parameter list",
5026 keyword);
5028 if (!explained_incomplete_types)
5030 warning (0, "its scope is only this definition or declaration,"
5031 " which is probably not what you want");
5032 explained_incomplete_types = true;
5036 tags = tree_cons (b->id, decl, tags);
5037 break;
5039 case CONST_DECL:
5040 case TYPE_DECL:
5041 case FUNCTION_DECL:
5042 /* CONST_DECLs appear here when we have an embedded enum,
5043 and TYPE_DECLs appear here when we have an embedded struct
5044 or union. No warnings for this - we already warned about the
5045 type itself. FUNCTION_DECLs appear when there is an implicit
5046 function declaration in the parameter list. */
5048 TREE_CHAIN (decl) = others;
5049 others = decl;
5050 /* fall through */
5052 case ERROR_MARK:
5053 /* error_mark_node appears here when we have an undeclared
5054 variable. Just throw it away. */
5055 if (b->id)
5057 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5058 I_SYMBOL_BINDING (b->id) = b->shadowed;
5060 break;
5062 /* Other things that might be encountered. */
5063 case LABEL_DECL:
5064 case VAR_DECL:
5065 default:
5066 gcc_unreachable ();
5069 b = free_binding_and_advance (b);
5072 arg_info->parms = parms;
5073 arg_info->tags = tags;
5074 arg_info->types = types;
5075 arg_info->others = others;
5076 return arg_info;
5079 /* Get the struct, enum or union (CODE says which) with tag NAME.
5080 Define the tag as a forward-reference if it is not defined.
5081 Return a c_typespec structure for the type specifier. */
5083 struct c_typespec
5084 parser_xref_tag (enum tree_code code, tree name)
5086 struct c_typespec ret;
5087 /* If a cross reference is requested, look up the type
5088 already defined for this tag and return it. */
5090 tree ref = lookup_tag (code, name, 0);
5091 /* If this is the right type of tag, return what we found.
5092 (This reference will be shadowed by shadow_tag later if appropriate.)
5093 If this is the wrong type of tag, do not return it. If it was the
5094 wrong type in the same scope, we will have had an error
5095 message already; if in a different scope and declaring
5096 a name, pending_xref_error will give an error message; but if in a
5097 different scope and not declaring a name, this tag should
5098 shadow the previous declaration of a different type of tag, and
5099 this would not work properly if we return the reference found.
5100 (For example, with "struct foo" in an outer scope, "union foo;"
5101 must shadow that tag with a new one of union type.) */
5102 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5103 if (ref && TREE_CODE (ref) == code)
5105 ret.spec = ref;
5106 return ret;
5109 /* If no such tag is yet defined, create a forward-reference node
5110 and record it as the "definition".
5111 When a real declaration of this type is found,
5112 the forward-reference will be altered into a real type. */
5114 ref = make_node (code);
5115 if (code == ENUMERAL_TYPE)
5117 /* Give the type a default layout like unsigned int
5118 to avoid crashing if it does not get defined. */
5119 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5120 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5121 TYPE_USER_ALIGN (ref) = 0;
5122 TYPE_UNSIGNED (ref) = 1;
5123 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5124 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5125 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5128 pushtag (name, ref);
5130 ret.spec = ref;
5131 return ret;
5134 /* Get the struct, enum or union (CODE says which) with tag NAME.
5135 Define the tag as a forward-reference if it is not defined.
5136 Return a tree for the type. */
5138 tree
5139 xref_tag (enum tree_code code, tree name)
5141 return parser_xref_tag (code, name).spec;
5144 /* Make sure that the tag NAME is defined *in the current scope*
5145 at least as a forward reference.
5146 CODE says which kind of tag NAME ought to be. */
5148 tree
5149 start_struct (enum tree_code code, tree name)
5151 /* If there is already a tag defined at this scope
5152 (as a forward reference), just return it. */
5154 tree ref = 0;
5156 if (name != 0)
5157 ref = lookup_tag (code, name, 1);
5158 if (ref && TREE_CODE (ref) == code)
5160 if (TYPE_SIZE (ref))
5162 if (code == UNION_TYPE)
5163 error ("redefinition of %<union %E%>", name);
5164 else
5165 error ("redefinition of %<struct %E%>", name);
5167 else if (C_TYPE_BEING_DEFINED (ref))
5169 if (code == UNION_TYPE)
5170 error ("nested redefinition of %<union %E%>", name);
5171 else
5172 error ("nested redefinition of %<struct %E%>", name);
5175 else
5177 /* Otherwise create a forward-reference just so the tag is in scope. */
5179 ref = make_node (code);
5180 pushtag (name, ref);
5183 C_TYPE_BEING_DEFINED (ref) = 1;
5184 TYPE_PACKED (ref) = flag_pack_struct;
5185 return ref;
5188 /* Process the specs, declarator and width (NULL if omitted)
5189 of a structure component, returning a FIELD_DECL node.
5190 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5192 This is done during the parsing of the struct declaration.
5193 The FIELD_DECL nodes are chained together and the lot of them
5194 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5196 tree
5197 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5198 tree width)
5200 tree value;
5202 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5203 && width == NULL_TREE)
5205 /* This is an unnamed decl.
5207 If we have something of the form "union { list } ;" then this
5208 is the anonymous union extension. Similarly for struct.
5210 If this is something of the form "struct foo;", then
5211 If MS extensions are enabled, this is handled as an
5212 anonymous struct.
5213 Otherwise this is a forward declaration of a structure tag.
5215 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5216 If MS extensions are enabled and foo names a structure, then
5217 again this is an anonymous struct.
5218 Otherwise this is an error.
5220 Oh what a horrid tangled web we weave. I wonder if MS consciously
5221 took this from Plan 9 or if it was an accident of implementation
5222 that took root before someone noticed the bug... */
5224 tree type = declspecs->type;
5225 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5226 || TREE_CODE (type) == UNION_TYPE);
5227 bool ok = false;
5229 if (type_ok
5230 && (flag_ms_extensions || !declspecs->typedef_p))
5232 if (flag_ms_extensions)
5233 ok = true;
5234 else if (flag_iso)
5235 ok = false;
5236 else if (TYPE_NAME (type) == NULL)
5237 ok = true;
5238 else
5239 ok = false;
5241 if (!ok)
5243 pedwarn ("declaration does not declare anything");
5244 return NULL_TREE;
5246 if (pedantic)
5247 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5250 value = grokdeclarator (declarator, declspecs, FIELD, false,
5251 width ? &width : NULL);
5253 finish_decl (value, NULL_TREE, NULL_TREE);
5254 DECL_INITIAL (value) = width;
5256 return value;
5259 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5260 the list such that this does not present a problem later. */
5262 static void
5263 detect_field_duplicates (tree fieldlist)
5265 tree x, y;
5266 int timeout = 10;
5268 /* First, see if there are more than "a few" fields.
5269 This is trivially true if there are zero or one fields. */
5270 if (!fieldlist)
5271 return;
5272 x = TREE_CHAIN (fieldlist);
5273 if (!x)
5274 return;
5275 do {
5276 timeout--;
5277 x = TREE_CHAIN (x);
5278 } while (timeout > 0 && x);
5280 /* If there were "few" fields, avoid the overhead of allocating
5281 a hash table. Instead just do the nested traversal thing. */
5282 if (timeout > 0)
5284 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5285 if (DECL_NAME (x))
5287 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5288 if (DECL_NAME (y) == DECL_NAME (x))
5290 error ("duplicate member %q+D", x);
5291 DECL_NAME (x) = NULL_TREE;
5295 else
5297 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5298 void **slot;
5300 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5301 if ((y = DECL_NAME (x)) != 0)
5303 slot = htab_find_slot (htab, y, INSERT);
5304 if (*slot)
5306 error ("duplicate member %q+D", x);
5307 DECL_NAME (x) = NULL_TREE;
5309 *slot = y;
5312 htab_delete (htab);
5316 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5317 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5318 ATTRIBUTES are attributes to be applied to the structure. */
5320 tree
5321 finish_struct (tree t, tree fieldlist, tree attributes)
5323 tree x;
5324 bool toplevel = file_scope == current_scope;
5325 int saw_named_field;
5327 /* If this type was previously laid out as a forward reference,
5328 make sure we lay it out again. */
5330 TYPE_SIZE (t) = 0;
5332 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5334 if (pedantic)
5336 for (x = fieldlist; x; x = TREE_CHAIN (x))
5337 if (DECL_NAME (x) != 0)
5338 break;
5340 if (x == 0)
5342 if (TREE_CODE (t) == UNION_TYPE)
5344 if (fieldlist)
5345 pedwarn ("union has no named members");
5346 else
5347 pedwarn ("union has no members");
5349 else
5351 if (fieldlist)
5352 pedwarn ("struct has no named members");
5353 else
5354 pedwarn ("struct has no members");
5359 /* Install struct as DECL_CONTEXT of each field decl.
5360 Also process specified field sizes, found in the DECL_INITIAL,
5361 storing 0 there after the type has been changed to precision equal
5362 to its width, rather than the precision of the specified standard
5363 type. (Correct layout requires the original type to have been preserved
5364 until now.) */
5366 saw_named_field = 0;
5367 for (x = fieldlist; x; x = TREE_CHAIN (x))
5369 if (TREE_TYPE (x) == error_mark_node)
5370 continue;
5372 DECL_CONTEXT (x) = t;
5374 if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
5375 DECL_PACKED (x) = 1;
5377 /* If any field is const, the structure type is pseudo-const. */
5378 if (TREE_READONLY (x))
5379 C_TYPE_FIELDS_READONLY (t) = 1;
5380 else
5382 /* A field that is pseudo-const makes the structure likewise. */
5383 tree t1 = TREE_TYPE (x);
5384 while (TREE_CODE (t1) == ARRAY_TYPE)
5385 t1 = TREE_TYPE (t1);
5386 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5387 && C_TYPE_FIELDS_READONLY (t1))
5388 C_TYPE_FIELDS_READONLY (t) = 1;
5391 /* Any field that is volatile means variables of this type must be
5392 treated in some ways as volatile. */
5393 if (TREE_THIS_VOLATILE (x))
5394 C_TYPE_FIELDS_VOLATILE (t) = 1;
5396 /* Any field of nominal variable size implies structure is too. */
5397 if (C_DECL_VARIABLE_SIZE (x))
5398 C_TYPE_VARIABLE_SIZE (t) = 1;
5400 if (DECL_INITIAL (x))
5402 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5403 DECL_SIZE (x) = bitsize_int (width);
5404 DECL_BIT_FIELD (x) = 1;
5405 SET_DECL_C_BIT_FIELD (x);
5408 /* Detect flexible array member in an invalid context. */
5409 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5410 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5411 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5412 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5414 if (TREE_CODE (t) == UNION_TYPE)
5416 error ("%Jflexible array member in union", x);
5417 TREE_TYPE (x) = error_mark_node;
5419 else if (TREE_CHAIN (x) != NULL_TREE)
5421 error ("%Jflexible array member not at end of struct", x);
5422 TREE_TYPE (x) = error_mark_node;
5424 else if (!saw_named_field)
5426 error ("%Jflexible array member in otherwise empty struct", x);
5427 TREE_TYPE (x) = error_mark_node;
5431 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5432 && flexible_array_type_p (TREE_TYPE (x)))
5433 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5435 if (DECL_NAME (x))
5436 saw_named_field = 1;
5439 detect_field_duplicates (fieldlist);
5441 /* Now we have the nearly final fieldlist. Record it,
5442 then lay out the structure or union (including the fields). */
5444 TYPE_FIELDS (t) = fieldlist;
5446 layout_type (t);
5448 /* Give bit-fields their proper types. */
5450 tree *fieldlistp = &fieldlist;
5451 while (*fieldlistp)
5452 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5453 && TREE_TYPE (*fieldlistp) != error_mark_node)
5455 unsigned HOST_WIDE_INT width
5456 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5457 tree type = TREE_TYPE (*fieldlistp);
5458 if (width != TYPE_PRECISION (type))
5460 TREE_TYPE (*fieldlistp)
5461 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5462 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5464 DECL_INITIAL (*fieldlistp) = 0;
5466 else
5467 fieldlistp = &TREE_CHAIN (*fieldlistp);
5470 /* Now we have the truly final field list.
5471 Store it in this type and in the variants. */
5473 TYPE_FIELDS (t) = fieldlist;
5475 /* If there are lots of fields, sort so we can look through them fast.
5476 We arbitrarily consider 16 or more elts to be "a lot". */
5479 int len = 0;
5481 for (x = fieldlist; x; x = TREE_CHAIN (x))
5483 if (len > 15 || DECL_NAME (x) == NULL)
5484 break;
5485 len += 1;
5488 if (len > 15)
5490 tree *field_array;
5491 struct lang_type *space;
5492 struct sorted_fields_type *space2;
5494 len += list_length (x);
5496 /* Use the same allocation policy here that make_node uses, to
5497 ensure that this lives as long as the rest of the struct decl.
5498 All decls in an inline function need to be saved. */
5500 space = GGC_CNEW (struct lang_type);
5501 space2 = GGC_NEWVAR (struct sorted_fields_type,
5502 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5504 len = 0;
5505 space->s = space2;
5506 field_array = &space2->elts[0];
5507 for (x = fieldlist; x; x = TREE_CHAIN (x))
5509 field_array[len++] = x;
5511 /* If there is anonymous struct or union, break out of the loop. */
5512 if (DECL_NAME (x) == NULL)
5513 break;
5515 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5516 if (x == NULL)
5518 TYPE_LANG_SPECIFIC (t) = space;
5519 TYPE_LANG_SPECIFIC (t)->s->len = len;
5520 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5521 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5526 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5528 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5529 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5530 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5531 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5532 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5533 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5534 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5537 /* If this was supposed to be a transparent union, but we can't
5538 make it one, warn and turn off the flag. */
5539 if (TREE_CODE (t) == UNION_TYPE
5540 && TYPE_TRANSPARENT_UNION (t)
5541 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5543 TYPE_TRANSPARENT_UNION (t) = 0;
5544 warning (0, "union cannot be made transparent");
5547 /* If this structure or union completes the type of any previous
5548 variable declaration, lay it out and output its rtl. */
5549 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5551 x = TREE_CHAIN (x))
5553 tree decl = TREE_VALUE (x);
5554 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5555 layout_array_type (TREE_TYPE (decl));
5556 if (TREE_CODE (decl) != TYPE_DECL)
5558 layout_decl (decl, 0);
5559 if (c_dialect_objc ())
5560 objc_check_decl (decl);
5561 rest_of_decl_compilation (decl, toplevel, 0);
5562 if (!toplevel)
5563 expand_decl (decl);
5566 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5568 /* Finish debugging output for this type. */
5569 rest_of_type_compilation (t, toplevel);
5571 /* If we're inside a function proper, i.e. not file-scope and not still
5572 parsing parameters, then arrange for the size of a variable sized type
5573 to be bound now. */
5574 if (cur_stmt_list && variably_modified_type_p (t, NULL))
5575 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5577 return t;
5580 /* Lay out the type T, and its element type, and so on. */
5582 static void
5583 layout_array_type (tree t)
5585 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5586 layout_array_type (TREE_TYPE (t));
5587 layout_type (t);
5590 /* Begin compiling the definition of an enumeration type.
5591 NAME is its name (or null if anonymous).
5592 Returns the type object, as yet incomplete.
5593 Also records info about it so that build_enumerator
5594 may be used to declare the individual values as they are read. */
5596 tree
5597 start_enum (tree name)
5599 tree enumtype = 0;
5601 /* If this is the real definition for a previous forward reference,
5602 fill in the contents in the same object that used to be the
5603 forward reference. */
5605 if (name != 0)
5606 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5608 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5610 enumtype = make_node (ENUMERAL_TYPE);
5611 pushtag (name, enumtype);
5614 if (C_TYPE_BEING_DEFINED (enumtype))
5615 error ("nested redefinition of %<enum %E%>", name);
5617 C_TYPE_BEING_DEFINED (enumtype) = 1;
5619 if (TYPE_VALUES (enumtype) != 0)
5621 /* This enum is a named one that has been declared already. */
5622 error ("redeclaration of %<enum %E%>", name);
5624 /* Completely replace its old definition.
5625 The old enumerators remain defined, however. */
5626 TYPE_VALUES (enumtype) = 0;
5629 enum_next_value = integer_zero_node;
5630 enum_overflow = 0;
5632 if (flag_short_enums)
5633 TYPE_PACKED (enumtype) = 1;
5635 return enumtype;
5638 /* After processing and defining all the values of an enumeration type,
5639 install their decls in the enumeration type and finish it off.
5640 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5641 and ATTRIBUTES are the specified attributes.
5642 Returns ENUMTYPE. */
5644 tree
5645 finish_enum (tree enumtype, tree values, tree attributes)
5647 tree pair, tem;
5648 tree minnode = 0, maxnode = 0;
5649 int precision, unsign;
5650 bool toplevel = (file_scope == current_scope);
5651 struct lang_type *lt;
5653 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5655 /* Calculate the maximum value of any enumerator in this type. */
5657 if (values == error_mark_node)
5658 minnode = maxnode = integer_zero_node;
5659 else
5661 minnode = maxnode = TREE_VALUE (values);
5662 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5664 tree value = TREE_VALUE (pair);
5665 if (tree_int_cst_lt (maxnode, value))
5666 maxnode = value;
5667 if (tree_int_cst_lt (value, minnode))
5668 minnode = value;
5672 /* Construct the final type of this enumeration. It is the same
5673 as one of the integral types - the narrowest one that fits, except
5674 that normally we only go as narrow as int - and signed iff any of
5675 the values are negative. */
5676 unsign = (tree_int_cst_sgn (minnode) >= 0);
5677 precision = MAX (min_precision (minnode, unsign),
5678 min_precision (maxnode, unsign));
5680 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5682 tem = c_common_type_for_size (precision, unsign);
5683 if (tem == NULL)
5685 warning (0, "enumeration values exceed range of largest integer");
5686 tem = long_long_integer_type_node;
5689 else
5690 tem = unsign ? unsigned_type_node : integer_type_node;
5692 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5693 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5694 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5695 TYPE_SIZE (enumtype) = 0;
5697 /* If the precision of the type was specific with an attribute and it
5698 was too small, give an error. Otherwise, use it. */
5699 if (TYPE_PRECISION (enumtype))
5701 if (precision > TYPE_PRECISION (enumtype))
5702 error ("specified mode too small for enumeral values");
5704 else
5705 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5707 layout_type (enumtype);
5709 if (values != error_mark_node)
5711 /* Change the type of the enumerators to be the enum type. We
5712 need to do this irrespective of the size of the enum, for
5713 proper type checking. Replace the DECL_INITIALs of the
5714 enumerators, and the value slots of the list, with copies
5715 that have the enum type; they cannot be modified in place
5716 because they may be shared (e.g. integer_zero_node) Finally,
5717 change the purpose slots to point to the names of the decls. */
5718 for (pair = values; pair; pair = TREE_CHAIN (pair))
5720 tree enu = TREE_PURPOSE (pair);
5721 tree ini = DECL_INITIAL (enu);
5723 TREE_TYPE (enu) = enumtype;
5725 /* The ISO C Standard mandates enumerators to have type int,
5726 even though the underlying type of an enum type is
5727 unspecified. Here we convert any enumerators that fit in
5728 an int to type int, to avoid promotions to unsigned types
5729 when comparing integers with enumerators that fit in the
5730 int range. When -pedantic is given, build_enumerator()
5731 would have already taken care of those that don't fit. */
5732 if (int_fits_type_p (ini, integer_type_node))
5733 tem = integer_type_node;
5734 else
5735 tem = enumtype;
5736 ini = convert (tem, ini);
5738 DECL_INITIAL (enu) = ini;
5739 TREE_PURPOSE (pair) = DECL_NAME (enu);
5740 TREE_VALUE (pair) = ini;
5743 TYPE_VALUES (enumtype) = values;
5746 /* Record the min/max values so that we can warn about bit-field
5747 enumerations that are too small for the values. */
5748 lt = GGC_CNEW (struct lang_type);
5749 lt->enum_min = minnode;
5750 lt->enum_max = maxnode;
5751 TYPE_LANG_SPECIFIC (enumtype) = lt;
5753 /* Fix up all variant types of this enum type. */
5754 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5756 if (tem == enumtype)
5757 continue;
5758 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5759 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5760 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5761 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5762 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5763 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5764 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5765 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5766 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5767 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5768 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5771 /* Finish debugging output for this type. */
5772 rest_of_type_compilation (enumtype, toplevel);
5774 return enumtype;
5777 /* Build and install a CONST_DECL for one value of the
5778 current enumeration type (one that was begun with start_enum).
5779 Return a tree-list containing the CONST_DECL and its value.
5780 Assignment of sequential values by default is handled here. */
5782 tree
5783 build_enumerator (tree name, tree value)
5785 tree decl, type;
5787 /* Validate and default VALUE. */
5789 if (value != 0)
5791 /* Don't issue more errors for error_mark_node (i.e. an
5792 undeclared identifier) - just ignore the value expression. */
5793 if (value == error_mark_node)
5794 value = 0;
5795 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5796 || TREE_CODE (value) != INTEGER_CST)
5798 error ("enumerator value for %qE is not an integer constant", name);
5799 value = 0;
5801 else
5803 value = default_conversion (value);
5804 constant_expression_warning (value);
5808 /* Default based on previous value. */
5809 /* It should no longer be possible to have NON_LVALUE_EXPR
5810 in the default. */
5811 if (value == 0)
5813 value = enum_next_value;
5814 if (enum_overflow)
5815 error ("overflow in enumeration values");
5818 if (pedantic && !int_fits_type_p (value, integer_type_node))
5820 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5821 /* XXX This causes -pedantic to change the meaning of the program.
5822 Remove? -zw 2004-03-15 */
5823 value = convert (integer_type_node, value);
5826 /* Set basis for default for next value. */
5827 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5828 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5830 /* Now create a declaration for the enum value name. */
5832 type = TREE_TYPE (value);
5833 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5834 TYPE_PRECISION (integer_type_node)),
5835 (TYPE_PRECISION (type)
5836 >= TYPE_PRECISION (integer_type_node)
5837 && TYPE_UNSIGNED (type)));
5839 decl = build_decl (CONST_DECL, name, type);
5840 DECL_INITIAL (decl) = convert (type, value);
5841 pushdecl (decl);
5843 return tree_cons (decl, value, NULL_TREE);
5847 /* Create the FUNCTION_DECL for a function definition.
5848 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5849 the declaration; they describe the function's name and the type it returns,
5850 but twisted together in a fashion that parallels the syntax of C.
5852 This function creates a binding context for the function body
5853 as well as setting up the FUNCTION_DECL in current_function_decl.
5855 Returns 1 on success. If the DECLARATOR is not suitable for a function
5856 (it defines a datum instead), we return 0, which tells
5857 yyparse to report a parse error. */
5860 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5861 tree attributes)
5863 tree decl1, old_decl;
5864 tree restype, resdecl;
5865 struct c_label_context_se *nstack_se;
5866 struct c_label_context_vm *nstack_vm;
5868 current_function_returns_value = 0; /* Assume, until we see it does. */
5869 current_function_returns_null = 0;
5870 current_function_returns_abnormally = 0;
5871 warn_about_return_type = 0;
5872 current_extern_inline = 0;
5873 c_switch_stack = NULL;
5875 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
5876 nstack_se->labels_def = NULL;
5877 nstack_se->labels_used = NULL;
5878 nstack_se->next = label_context_stack_se;
5879 label_context_stack_se = nstack_se;
5881 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
5882 nstack_vm->labels_def = NULL;
5883 nstack_vm->labels_used = NULL;
5884 nstack_vm->scope = 0;
5885 nstack_vm->next = label_context_stack_vm;
5886 label_context_stack_vm = nstack_vm;
5888 /* Indicate no valid break/continue context by setting these variables
5889 to some non-null, non-label value. We'll notice and emit the proper
5890 error message in c_finish_bc_stmt. */
5891 c_break_label = c_cont_label = size_zero_node;
5893 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5895 /* If the declarator is not suitable for a function definition,
5896 cause a syntax error. */
5897 if (decl1 == 0)
5899 label_context_stack_se = label_context_stack_se->next;
5900 label_context_stack_vm = label_context_stack_vm->next;
5901 return 0;
5904 decl_attributes (&decl1, attributes, 0);
5906 if (DECL_DECLARED_INLINE_P (decl1)
5907 && DECL_UNINLINABLE (decl1)
5908 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5909 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
5910 decl1);
5912 announce_function (decl1);
5914 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5916 error ("return type is an incomplete type");
5917 /* Make it return void instead. */
5918 TREE_TYPE (decl1)
5919 = build_function_type (void_type_node,
5920 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5923 if (warn_about_return_type)
5924 pedwarn_c99 ("return type defaults to %<int%>");
5926 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5927 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5928 DECL_INITIAL (decl1) = error_mark_node;
5930 /* If this definition isn't a prototype and we had a prototype declaration
5931 before, copy the arg type info from that prototype. */
5932 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5933 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
5934 old_decl = 0;
5935 current_function_prototype_locus = UNKNOWN_LOCATION;
5936 current_function_prototype_built_in = false;
5937 current_function_prototype_arg_types = NULL_TREE;
5938 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5940 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5941 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5942 TREE_TYPE (TREE_TYPE (old_decl))))
5944 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5945 TREE_TYPE (decl1));
5946 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5947 current_function_prototype_built_in
5948 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
5949 current_function_prototype_arg_types
5950 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
5952 if (TREE_PUBLIC (decl1))
5954 /* If there is an external prototype declaration of this
5955 function, record its location but do not copy information
5956 to this decl. This may be an invisible declaration
5957 (built-in or in a scope which has finished) or simply
5958 have more refined argument types than any declaration
5959 found above. */
5960 struct c_binding *b;
5961 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
5962 if (B_IN_SCOPE (b, external_scope))
5963 break;
5964 if (b)
5966 tree ext_decl, ext_type;
5967 ext_decl = b->decl;
5968 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
5969 if (TREE_CODE (ext_type) == FUNCTION_TYPE
5970 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5971 TREE_TYPE (ext_type)))
5973 current_function_prototype_locus
5974 = DECL_SOURCE_LOCATION (ext_decl);
5975 current_function_prototype_built_in
5976 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
5977 current_function_prototype_arg_types
5978 = TYPE_ARG_TYPES (ext_type);
5984 /* Optionally warn of old-fashioned def with no previous prototype. */
5985 if (warn_strict_prototypes
5986 && old_decl != error_mark_node
5987 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5988 && C_DECL_ISNT_PROTOTYPE (old_decl))
5989 warning (OPT_Wstrict_prototypes,
5990 "function declaration isn%'t a prototype");
5991 /* Optionally warn of any global def with no previous prototype. */
5992 else if (warn_missing_prototypes
5993 && old_decl != error_mark_node
5994 && TREE_PUBLIC (decl1)
5995 && !MAIN_NAME_P (DECL_NAME (decl1))
5996 && C_DECL_ISNT_PROTOTYPE (old_decl))
5997 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
5998 /* Optionally warn of any def with no previous prototype
5999 if the function has already been used. */
6000 else if (warn_missing_prototypes
6001 && old_decl != 0
6002 && old_decl != error_mark_node
6003 && TREE_USED (old_decl)
6004 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6005 warning (OPT_Wmissing_prototypes,
6006 "%q+D was used with no prototype before its definition", decl1);
6007 /* Optionally warn of any global def with no previous declaration. */
6008 else if (warn_missing_declarations
6009 && TREE_PUBLIC (decl1)
6010 && old_decl == 0
6011 && !MAIN_NAME_P (DECL_NAME (decl1)))
6012 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6013 decl1);
6014 /* Optionally warn of any def with no previous declaration
6015 if the function has already been used. */
6016 else if (warn_missing_declarations
6017 && old_decl != 0
6018 && old_decl != error_mark_node
6019 && TREE_USED (old_decl)
6020 && C_DECL_IMPLICIT (old_decl))
6021 warning (OPT_Wmissing_declarations,
6022 "%q+D was used with no declaration before its definition", decl1);
6024 /* This is a definition, not a reference.
6025 So normally clear DECL_EXTERNAL.
6026 However, `extern inline' acts like a declaration
6027 except for defining how to inline. So set DECL_EXTERNAL in that case. */
6028 DECL_EXTERNAL (decl1) = current_extern_inline;
6030 /* This function exists in static storage.
6031 (This does not mean `static' in the C sense!) */
6032 TREE_STATIC (decl1) = 1;
6034 /* A nested function is not global. */
6035 if (current_function_decl != 0)
6036 TREE_PUBLIC (decl1) = 0;
6038 /* This is the earliest point at which we might know the assembler
6039 name of the function. Thus, if it's set before this, die horribly. */
6040 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6042 /* If #pragma weak was used, mark the decl weak now. */
6043 if (current_scope == file_scope)
6044 maybe_apply_pragma_weak (decl1);
6046 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6047 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6049 tree args;
6050 int argct = 0;
6052 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6053 != integer_type_node)
6054 pedwarn ("return type of %q+D is not %<int%>", decl1);
6056 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
6057 args = TREE_CHAIN (args))
6059 tree type = args ? TREE_VALUE (args) : 0;
6061 if (type == void_type_node)
6062 break;
6064 ++argct;
6065 switch (argct)
6067 case 1:
6068 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
6069 pedwarn ("first argument of %q+D should be %<int%>", decl1);
6070 break;
6072 case 2:
6073 if (TREE_CODE (type) != POINTER_TYPE
6074 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6075 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6076 != char_type_node))
6077 pedwarn ("second argument of %q+D should be %<char **%>",
6078 decl1);
6079 break;
6081 case 3:
6082 if (TREE_CODE (type) != POINTER_TYPE
6083 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6084 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6085 != char_type_node))
6086 pedwarn ("third argument of %q+D should probably be "
6087 "%<char **%>", decl1);
6088 break;
6092 /* It is intentional that this message does not mention the third
6093 argument because it's only mentioned in an appendix of the
6094 standard. */
6095 if (argct > 0 && (argct < 2 || argct > 3))
6096 pedwarn ("%q+D takes only zero or two arguments", decl1);
6098 if (!TREE_PUBLIC (decl1))
6099 pedwarn ("%q+D is normally a non-static function", decl1);
6102 /* Record the decl so that the function name is defined.
6103 If we already have a decl for this name, and it is a FUNCTION_DECL,
6104 use the old decl. */
6106 current_function_decl = pushdecl (decl1);
6108 push_scope ();
6109 declare_parm_level ();
6111 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6112 /* Promote the value to int before returning it. */
6113 if (c_promoting_integer_type_p (restype))
6115 /* It retains unsignedness if not really getting wider. */
6116 if (TYPE_UNSIGNED (restype)
6117 && (TYPE_PRECISION (restype)
6118 == TYPE_PRECISION (integer_type_node)))
6119 restype = unsigned_type_node;
6120 else
6121 restype = integer_type_node;
6124 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6125 DECL_ARTIFICIAL (resdecl) = 1;
6126 DECL_IGNORED_P (resdecl) = 1;
6127 DECL_RESULT (current_function_decl) = resdecl;
6129 start_fname_decls ();
6131 return 1;
6134 /* Subroutine of store_parm_decls which handles new-style function
6135 definitions (prototype format). The parms already have decls, so we
6136 need only record them as in effect and complain if any redundant
6137 old-style parm decls were written. */
6138 static void
6139 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6141 tree decl;
6143 if (current_scope->bindings)
6145 error ("%Jold-style parameter declarations in prototyped "
6146 "function definition", fndecl);
6148 /* Get rid of the old-style declarations. */
6149 pop_scope ();
6150 push_scope ();
6152 /* Don't issue this warning for nested functions, and don't issue this
6153 warning if we got here because ARG_INFO_TYPES was error_mark_node
6154 (this happens when a function definition has just an ellipsis in
6155 its parameter list). */
6156 else if (!in_system_header && !current_function_scope
6157 && arg_info->types != error_mark_node)
6158 warning (OPT_Wtraditional,
6159 "%Jtraditional C rejects ISO C style function definitions",
6160 fndecl);
6162 /* Now make all the parameter declarations visible in the function body.
6163 We can bypass most of the grunt work of pushdecl. */
6164 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6166 DECL_CONTEXT (decl) = current_function_decl;
6167 if (DECL_NAME (decl))
6169 bind (DECL_NAME (decl), decl, current_scope,
6170 /*invisible=*/false, /*nested=*/false);
6171 if (!TREE_USED (decl))
6172 warn_if_shadowing (decl);
6174 else
6175 error ("%Jparameter name omitted", decl);
6178 /* Record the parameter list in the function declaration. */
6179 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6181 /* Now make all the ancillary declarations visible, likewise. */
6182 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6184 DECL_CONTEXT (decl) = current_function_decl;
6185 if (DECL_NAME (decl))
6186 bind (DECL_NAME (decl), decl, current_scope,
6187 /*invisible=*/false, /*nested=*/false);
6190 /* And all the tag declarations. */
6191 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6192 if (TREE_PURPOSE (decl))
6193 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6194 /*invisible=*/false, /*nested=*/false);
6197 /* Subroutine of store_parm_decls which handles old-style function
6198 definitions (separate parameter list and declarations). */
6200 static void
6201 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6203 struct c_binding *b;
6204 tree parm, decl, last;
6205 tree parmids = arg_info->parms;
6206 struct pointer_set_t *seen_args = pointer_set_create ();
6208 if (!in_system_header)
6209 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6210 fndecl);
6212 /* Match each formal parameter name with its declaration. Save each
6213 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6214 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6216 if (TREE_VALUE (parm) == 0)
6218 error ("%Jparameter name missing from parameter list", fndecl);
6219 TREE_PURPOSE (parm) = 0;
6220 continue;
6223 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6224 if (b && B_IN_CURRENT_SCOPE (b))
6226 decl = b->decl;
6227 /* If we got something other than a PARM_DECL it is an error. */
6228 if (TREE_CODE (decl) != PARM_DECL)
6229 error ("%q+D declared as a non-parameter", decl);
6230 /* If the declaration is already marked, we have a duplicate
6231 name. Complain and ignore the duplicate. */
6232 else if (pointer_set_contains (seen_args, decl))
6234 error ("multiple parameters named %q+D", decl);
6235 TREE_PURPOSE (parm) = 0;
6236 continue;
6238 /* If the declaration says "void", complain and turn it into
6239 an int. */
6240 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6242 error ("parameter %q+D declared with void type", decl);
6243 TREE_TYPE (decl) = integer_type_node;
6244 DECL_ARG_TYPE (decl) = integer_type_node;
6245 layout_decl (decl, 0);
6247 warn_if_shadowing (decl);
6249 /* If no declaration found, default to int. */
6250 else
6252 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6253 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6254 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6255 pushdecl (decl);
6256 warn_if_shadowing (decl);
6258 if (flag_isoc99)
6259 pedwarn ("type of %q+D defaults to %<int%>", decl);
6260 else if (extra_warnings)
6261 warning (OPT_Wextra, "type of %q+D defaults to %<int%>", decl);
6264 TREE_PURPOSE (parm) = decl;
6265 pointer_set_insert (seen_args, decl);
6268 /* Now examine the parms chain for incomplete declarations
6269 and declarations with no corresponding names. */
6271 for (b = current_scope->bindings; b; b = b->prev)
6273 parm = b->decl;
6274 if (TREE_CODE (parm) != PARM_DECL)
6275 continue;
6277 if (TREE_TYPE (parm) != error_mark_node
6278 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6280 error ("parameter %q+D has incomplete type", parm);
6281 TREE_TYPE (parm) = error_mark_node;
6284 if (!pointer_set_contains (seen_args, parm))
6286 error ("declaration for parameter %q+D but no such parameter", parm);
6288 /* Pretend the parameter was not missing.
6289 This gets us to a standard state and minimizes
6290 further error messages. */
6291 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6295 /* Chain the declarations together in the order of the list of
6296 names. Store that chain in the function decl, replacing the
6297 list of names. Update the current scope to match. */
6298 DECL_ARGUMENTS (fndecl) = 0;
6300 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6301 if (TREE_PURPOSE (parm))
6302 break;
6303 if (parm && TREE_PURPOSE (parm))
6305 last = TREE_PURPOSE (parm);
6306 DECL_ARGUMENTS (fndecl) = last;
6308 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6309 if (TREE_PURPOSE (parm))
6311 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6312 last = TREE_PURPOSE (parm);
6314 TREE_CHAIN (last) = 0;
6317 pointer_set_destroy (seen_args);
6319 /* If there was a previous prototype,
6320 set the DECL_ARG_TYPE of each argument according to
6321 the type previously specified, and report any mismatches. */
6323 if (current_function_prototype_arg_types)
6325 tree type;
6326 for (parm = DECL_ARGUMENTS (fndecl),
6327 type = current_function_prototype_arg_types;
6328 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6329 != void_type_node));
6330 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6332 if (parm == 0 || type == 0
6333 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6335 if (current_function_prototype_built_in)
6336 warning (0, "number of arguments doesn%'t match "
6337 "built-in prototype");
6338 else
6340 error ("number of arguments doesn%'t match prototype");
6341 error ("%Hprototype declaration",
6342 &current_function_prototype_locus);
6344 break;
6346 /* Type for passing arg must be consistent with that
6347 declared for the arg. ISO C says we take the unqualified
6348 type for parameters declared with qualified type. */
6349 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6350 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6352 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6353 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6355 /* Adjust argument to match prototype. E.g. a previous
6356 `int foo(float);' prototype causes
6357 `int foo(x) float x; {...}' to be treated like
6358 `int foo(float x) {...}'. This is particularly
6359 useful for argument types like uid_t. */
6360 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6362 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6363 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6364 && TYPE_PRECISION (TREE_TYPE (parm))
6365 < TYPE_PRECISION (integer_type_node))
6366 DECL_ARG_TYPE (parm) = integer_type_node;
6368 if (pedantic)
6370 /* ??? Is it possible to get here with a
6371 built-in prototype or will it always have
6372 been diagnosed as conflicting with an
6373 old-style definition and discarded? */
6374 if (current_function_prototype_built_in)
6375 warning (0, "promoted argument %qD "
6376 "doesn%'t match built-in prototype", parm);
6377 else
6379 pedwarn ("promoted argument %qD "
6380 "doesn%'t match prototype", parm);
6381 pedwarn ("%Hprototype declaration",
6382 &current_function_prototype_locus);
6386 else
6388 if (current_function_prototype_built_in)
6389 warning (0, "argument %qD doesn%'t match "
6390 "built-in prototype", parm);
6391 else
6393 error ("argument %qD doesn%'t match prototype", parm);
6394 error ("%Hprototype declaration",
6395 &current_function_prototype_locus);
6400 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6403 /* Otherwise, create a prototype that would match. */
6405 else
6407 tree actual = 0, last = 0, type;
6409 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6411 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6412 if (last)
6413 TREE_CHAIN (last) = type;
6414 else
6415 actual = type;
6416 last = type;
6418 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6419 if (last)
6420 TREE_CHAIN (last) = type;
6421 else
6422 actual = type;
6424 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6425 of the type of this function, but we need to avoid having this
6426 affect the types of other similarly-typed functions, so we must
6427 first force the generation of an identical (but separate) type
6428 node for the relevant function type. The new node we create
6429 will be a variant of the main variant of the original function
6430 type. */
6432 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6434 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6438 /* Store parameter declarations passed in ARG_INFO into the current
6439 function declaration. */
6441 void
6442 store_parm_decls_from (struct c_arg_info *arg_info)
6444 current_function_arg_info = arg_info;
6445 store_parm_decls ();
6448 /* Store the parameter declarations into the current function declaration.
6449 This is called after parsing the parameter declarations, before
6450 digesting the body of the function.
6452 For an old-style definition, construct a prototype out of the old-style
6453 parameter declarations and inject it into the function's type. */
6455 void
6456 store_parm_decls (void)
6458 tree fndecl = current_function_decl;
6459 bool proto;
6461 /* The argument information block for FNDECL. */
6462 struct c_arg_info *arg_info = current_function_arg_info;
6463 current_function_arg_info = 0;
6465 /* True if this definition is written with a prototype. Note:
6466 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6467 list in a function definition as equivalent to (void) -- an
6468 empty argument list specifies the function has no parameters,
6469 but only (void) sets up a prototype for future calls. */
6470 proto = arg_info->types != 0;
6472 if (proto)
6473 store_parm_decls_newstyle (fndecl, arg_info);
6474 else
6475 store_parm_decls_oldstyle (fndecl, arg_info);
6477 /* The next call to push_scope will be a function body. */
6479 next_is_function_body = true;
6481 /* Write a record describing this function definition to the prototypes
6482 file (if requested). */
6484 gen_aux_info_record (fndecl, 1, 0, proto);
6486 /* Initialize the RTL code for the function. */
6487 allocate_struct_function (fndecl);
6489 /* Begin the statement tree for this function. */
6490 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6492 /* ??? Insert the contents of the pending sizes list into the function
6493 to be evaluated. The only reason left to have this is
6494 void foo(int n, int array[n++])
6495 because we throw away the array type in favor of a pointer type, and
6496 thus won't naturally see the SAVE_EXPR containing the increment. All
6497 other pending sizes would be handled by gimplify_parameters. */
6499 tree t;
6500 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6501 add_stmt (TREE_VALUE (t));
6504 /* Even though we're inside a function body, we still don't want to
6505 call expand_expr to calculate the size of a variable-sized array.
6506 We haven't necessarily assigned RTL to all variables yet, so it's
6507 not safe to try to expand expressions involving them. */
6508 cfun->x_dont_save_pending_sizes_p = 1;
6511 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6512 functions. */
6514 static void
6515 c_warn_unused_result_recursively (tree fndecl)
6517 struct cgraph_node *cgn;
6519 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6520 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6522 /* Finalize all nested functions now. */
6523 cgn = cgraph_node (fndecl);
6524 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6525 c_warn_unused_result_recursively (cgn->decl);
6528 /* Finish up a function declaration and compile that function
6529 all the way to assembler language output. The free the storage
6530 for the function definition.
6532 This is called after parsing the body of the function definition. */
6534 void
6535 finish_function (void)
6537 tree fndecl = current_function_decl;
6539 label_context_stack_se = label_context_stack_se->next;
6540 label_context_stack_vm = label_context_stack_vm->next;
6542 if (TREE_CODE (fndecl) == FUNCTION_DECL
6543 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6545 tree args = DECL_ARGUMENTS (fndecl);
6546 for (; args; args = TREE_CHAIN (args))
6548 tree type = TREE_TYPE (args);
6549 if (INTEGRAL_TYPE_P (type)
6550 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6551 DECL_ARG_TYPE (args) = integer_type_node;
6555 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6556 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6558 /* Must mark the RESULT_DECL as being in this function. */
6560 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6561 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6563 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6565 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6566 != integer_type_node)
6568 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6569 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6570 if (!warn_main)
6571 pedwarn ("return type of %q+D is not %<int%>", fndecl);
6573 else
6575 if (flag_isoc99)
6577 tree stmt = c_finish_return (integer_zero_node);
6578 #ifdef USE_MAPPED_LOCATION
6579 /* Hack. We don't want the middle-end to warn that this return
6580 is unreachable, so we mark its location as special. Using
6581 UNKNOWN_LOCATION has the problem that it gets clobbered in
6582 annotate_one_with_locus. A cleaner solution might be to
6583 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6585 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6586 #else
6587 /* Hack. We don't want the middle-end to warn that this
6588 return is unreachable, so put the statement on the
6589 special line 0. */
6590 annotate_with_file_line (stmt, input_filename, 0);
6591 #endif
6596 /* Tie off the statement tree for this function. */
6597 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6599 finish_fname_decls ();
6601 /* Complain if there's just no return statement. */
6602 if (warn_return_type
6603 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6604 && !current_function_returns_value && !current_function_returns_null
6605 /* Don't complain if we are no-return. */
6606 && !current_function_returns_abnormally
6607 /* Don't warn for main(). */
6608 && !MAIN_NAME_P (DECL_NAME (fndecl))
6609 /* Or if they didn't actually specify a return type. */
6610 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6611 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6612 inline function, as we might never be compiled separately. */
6613 && DECL_INLINE (fndecl))
6615 warning (OPT_Wreturn_type,
6616 "no return statement in function returning non-void");
6617 TREE_NO_WARNING (fndecl) = 1;
6620 /* With just -Wextra, complain only if function returns both with
6621 and without a value. */
6622 if (extra_warnings
6623 && current_function_returns_value
6624 && current_function_returns_null)
6625 warning (OPT_Wextra, "this function may return with or without a value");
6627 /* Store the end of the function, so that we get good line number
6628 info for the epilogue. */
6629 cfun->function_end_locus = input_location;
6631 /* If we don't have ctors/dtors sections, and this is a static
6632 constructor or destructor, it must be recorded now. */
6633 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6634 && !targetm.have_ctors_dtors)
6635 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6636 if (DECL_STATIC_DESTRUCTOR (fndecl)
6637 && !targetm.have_ctors_dtors)
6638 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6640 /* Finalize the ELF visibility for the function. */
6641 c_determine_visibility (fndecl);
6643 /* Genericize before inlining. Delay genericizing nested functions
6644 until their parent function is genericized. Since finalizing
6645 requires GENERIC, delay that as well. */
6647 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6648 && !undef_nested_function)
6650 if (!decl_function_context (fndecl))
6652 c_genericize (fndecl);
6653 c_warn_unused_result_recursively (fndecl);
6655 /* ??? Objc emits functions after finalizing the compilation unit.
6656 This should be cleaned up later and this conditional removed. */
6657 if (cgraph_global_info_ready)
6659 c_expand_body (fndecl);
6660 return;
6663 cgraph_finalize_function (fndecl, false);
6665 else
6667 /* Register this function with cgraph just far enough to get it
6668 added to our parent's nested function list. Handy, since the
6669 C front end doesn't have such a list. */
6670 (void) cgraph_node (fndecl);
6674 if (!decl_function_context (fndecl))
6675 undef_nested_function = false;
6677 /* We're leaving the context of this function, so zap cfun.
6678 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6679 tree_rest_of_compilation. */
6680 cfun = NULL;
6681 current_function_decl = NULL;
6684 /* Generate the RTL for the body of FNDECL. */
6686 void
6687 c_expand_body (tree fndecl)
6690 if (!DECL_INITIAL (fndecl)
6691 || DECL_INITIAL (fndecl) == error_mark_node)
6692 return;
6694 tree_rest_of_compilation (fndecl);
6696 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6697 && targetm.have_ctors_dtors)
6698 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6699 DEFAULT_INIT_PRIORITY);
6700 if (DECL_STATIC_DESTRUCTOR (fndecl)
6701 && targetm.have_ctors_dtors)
6702 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6703 DEFAULT_INIT_PRIORITY);
6706 /* Check the declarations given in a for-loop for satisfying the C99
6707 constraints. */
6708 void
6709 check_for_loop_decls (void)
6711 struct c_binding *b;
6713 if (!flag_isoc99)
6715 /* If we get here, declarations have been used in a for loop without
6716 the C99 for loop scope. This doesn't make much sense, so don't
6717 allow it. */
6718 error ("%<for%> loop initial declaration used outside C99 mode");
6719 return;
6721 /* C99 subclause 6.8.5 paragraph 3:
6723 [#3] The declaration part of a for statement shall only
6724 declare identifiers for objects having storage class auto or
6725 register.
6727 It isn't clear whether, in this sentence, "identifiers" binds to
6728 "shall only declare" or to "objects" - that is, whether all identifiers
6729 declared must be identifiers for objects, or whether the restriction
6730 only applies to those that are. (A question on this in comp.std.c
6731 in November 2000 received no answer.) We implement the strictest
6732 interpretation, to avoid creating an extension which later causes
6733 problems. */
6735 for (b = current_scope->bindings; b; b = b->prev)
6737 tree id = b->id;
6738 tree decl = b->decl;
6740 if (!id)
6741 continue;
6743 switch (TREE_CODE (decl))
6745 case VAR_DECL:
6746 if (TREE_STATIC (decl))
6747 error ("declaration of static variable %q+D in %<for%> loop "
6748 "initial declaration", decl);
6749 else if (DECL_EXTERNAL (decl))
6750 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
6751 "initial declaration", decl);
6752 break;
6754 case RECORD_TYPE:
6755 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6756 id);
6757 break;
6758 case UNION_TYPE:
6759 error ("%<union %E%> declared in %<for%> loop initial declaration",
6760 id);
6761 break;
6762 case ENUMERAL_TYPE:
6763 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6764 id);
6765 break;
6766 default:
6767 error ("declaration of non-variable %q+D in %<for%> loop "
6768 "initial declaration", decl);
6773 /* Save and reinitialize the variables
6774 used during compilation of a C function. */
6776 void
6777 c_push_function_context (struct function *f)
6779 struct language_function *p;
6780 p = GGC_NEW (struct language_function);
6781 f->language = p;
6783 p->base.x_stmt_tree = c_stmt_tree;
6784 p->x_break_label = c_break_label;
6785 p->x_cont_label = c_cont_label;
6786 p->x_switch_stack = c_switch_stack;
6787 p->arg_info = current_function_arg_info;
6788 p->returns_value = current_function_returns_value;
6789 p->returns_null = current_function_returns_null;
6790 p->returns_abnormally = current_function_returns_abnormally;
6791 p->warn_about_return_type = warn_about_return_type;
6792 p->extern_inline = current_extern_inline;
6795 /* Restore the variables used during compilation of a C function. */
6797 void
6798 c_pop_function_context (struct function *f)
6800 struct language_function *p = f->language;
6802 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6803 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6805 /* Stop pointing to the local nodes about to be freed. */
6806 /* But DECL_INITIAL must remain nonzero so we know this
6807 was an actual function definition. */
6808 DECL_INITIAL (current_function_decl) = error_mark_node;
6809 DECL_ARGUMENTS (current_function_decl) = 0;
6812 c_stmt_tree = p->base.x_stmt_tree;
6813 c_break_label = p->x_break_label;
6814 c_cont_label = p->x_cont_label;
6815 c_switch_stack = p->x_switch_stack;
6816 current_function_arg_info = p->arg_info;
6817 current_function_returns_value = p->returns_value;
6818 current_function_returns_null = p->returns_null;
6819 current_function_returns_abnormally = p->returns_abnormally;
6820 warn_about_return_type = p->warn_about_return_type;
6821 current_extern_inline = p->extern_inline;
6823 f->language = NULL;
6826 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6828 void
6829 c_dup_lang_specific_decl (tree decl)
6831 struct lang_decl *ld;
6833 if (!DECL_LANG_SPECIFIC (decl))
6834 return;
6836 ld = GGC_NEW (struct lang_decl);
6837 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6838 DECL_LANG_SPECIFIC (decl) = ld;
6841 /* The functions below are required for functionality of doing
6842 function at once processing in the C front end. Currently these
6843 functions are not called from anywhere in the C front end, but as
6844 these changes continue, that will change. */
6846 /* Returns the stmt_tree (if any) to which statements are currently
6847 being added. If there is no active statement-tree, NULL is
6848 returned. */
6850 stmt_tree
6851 current_stmt_tree (void)
6853 return &c_stmt_tree;
6856 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6857 C. */
6860 anon_aggr_type_p (tree ARG_UNUSED (node))
6862 return 0;
6865 /* Return the global value of T as a symbol. */
6867 tree
6868 identifier_global_value (tree t)
6870 struct c_binding *b;
6872 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6873 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6874 return b->decl;
6876 return 0;
6879 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6880 otherwise the name is found in ridpointers from RID_INDEX. */
6882 void
6883 record_builtin_type (enum rid rid_index, const char *name, tree type)
6885 tree id, decl;
6886 if (name == 0)
6887 id = ridpointers[(int) rid_index];
6888 else
6889 id = get_identifier (name);
6890 decl = build_decl (TYPE_DECL, id, type);
6891 pushdecl (decl);
6892 if (debug_hooks->type_decl)
6893 debug_hooks->type_decl (decl, false);
6896 /* Build the void_list_node (void_type_node having been created). */
6897 tree
6898 build_void_list_node (void)
6900 tree t = build_tree_list (NULL_TREE, void_type_node);
6901 return t;
6904 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6906 struct c_parm *
6907 build_c_parm (struct c_declspecs *specs, tree attrs,
6908 struct c_declarator *declarator)
6910 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6911 ret->specs = specs;
6912 ret->attrs = attrs;
6913 ret->declarator = declarator;
6914 return ret;
6917 /* Return a declarator with nested attributes. TARGET is the inner
6918 declarator to which these attributes apply. ATTRS are the
6919 attributes. */
6921 struct c_declarator *
6922 build_attrs_declarator (tree attrs, struct c_declarator *target)
6924 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6925 ret->kind = cdk_attrs;
6926 ret->declarator = target;
6927 ret->u.attrs = attrs;
6928 return ret;
6931 /* Return a declarator for a function with arguments specified by ARGS
6932 and return type specified by TARGET. */
6934 struct c_declarator *
6935 build_function_declarator (struct c_arg_info *args,
6936 struct c_declarator *target)
6938 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6939 ret->kind = cdk_function;
6940 ret->declarator = target;
6941 ret->u.arg_info = args;
6942 return ret;
6945 /* Return a declarator for the identifier IDENT (which may be
6946 NULL_TREE for an abstract declarator). */
6948 struct c_declarator *
6949 build_id_declarator (tree ident)
6951 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6952 ret->kind = cdk_id;
6953 ret->declarator = 0;
6954 ret->u.id = ident;
6955 /* Default value - may get reset to a more precise location. */
6956 ret->id_loc = input_location;
6957 return ret;
6960 /* Return something to represent absolute declarators containing a *.
6961 TARGET is the absolute declarator that the * contains.
6962 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6963 to apply to the pointer type. */
6965 struct c_declarator *
6966 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6967 struct c_declarator *target)
6969 tree attrs;
6970 int quals = 0;
6971 struct c_declarator *itarget = target;
6972 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6973 if (type_quals_attrs)
6975 attrs = type_quals_attrs->attrs;
6976 quals = quals_from_declspecs (type_quals_attrs);
6977 if (attrs != NULL_TREE)
6978 itarget = build_attrs_declarator (attrs, target);
6980 ret->kind = cdk_pointer;
6981 ret->declarator = itarget;
6982 ret->u.pointer_quals = quals;
6983 return ret;
6986 /* Return a pointer to a structure for an empty list of declaration
6987 specifiers. */
6989 struct c_declspecs *
6990 build_null_declspecs (void)
6992 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6993 ret->type = 0;
6994 ret->decl_attr = 0;
6995 ret->attrs = 0;
6996 ret->typespec_word = cts_none;
6997 ret->storage_class = csc_none;
6998 ret->declspecs_seen_p = false;
6999 ret->type_seen_p = false;
7000 ret->non_sc_seen_p = false;
7001 ret->typedef_p = false;
7002 ret->tag_defined_p = false;
7003 ret->explicit_signed_p = false;
7004 ret->deprecated_p = false;
7005 ret->default_int_p = false;
7006 ret->long_p = false;
7007 ret->long_long_p = false;
7008 ret->short_p = false;
7009 ret->signed_p = false;
7010 ret->unsigned_p = false;
7011 ret->complex_p = false;
7012 ret->inline_p = false;
7013 ret->thread_p = false;
7014 ret->const_p = false;
7015 ret->volatile_p = false;
7016 ret->restrict_p = false;
7017 return ret;
7020 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7021 returning SPECS. */
7023 struct c_declspecs *
7024 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7026 enum rid i;
7027 bool dupe = false;
7028 specs->non_sc_seen_p = true;
7029 specs->declspecs_seen_p = true;
7030 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7031 && C_IS_RESERVED_WORD (qual));
7032 i = C_RID_CODE (qual);
7033 switch (i)
7035 case RID_CONST:
7036 dupe = specs->const_p;
7037 specs->const_p = true;
7038 break;
7039 case RID_VOLATILE:
7040 dupe = specs->volatile_p;
7041 specs->volatile_p = true;
7042 break;
7043 case RID_RESTRICT:
7044 dupe = specs->restrict_p;
7045 specs->restrict_p = true;
7046 break;
7047 default:
7048 gcc_unreachable ();
7050 if (dupe && pedantic && !flag_isoc99)
7051 pedwarn ("duplicate %qE", qual);
7052 return specs;
7055 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7056 returning SPECS. */
7058 struct c_declspecs *
7059 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7061 tree type = spec.spec;
7062 specs->non_sc_seen_p = true;
7063 specs->declspecs_seen_p = true;
7064 specs->type_seen_p = true;
7065 if (TREE_DEPRECATED (type))
7066 specs->deprecated_p = true;
7068 /* Handle type specifier keywords. */
7069 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
7071 enum rid i = C_RID_CODE (type);
7072 if (specs->type)
7074 error ("two or more data types in declaration specifiers");
7075 return specs;
7077 if ((int) i <= (int) RID_LAST_MODIFIER)
7079 /* "long", "short", "signed", "unsigned" or "_Complex". */
7080 bool dupe = false;
7081 switch (i)
7083 case RID_LONG:
7084 if (specs->long_long_p)
7086 error ("%<long long long%> is too long for GCC");
7087 break;
7089 if (specs->long_p)
7091 if (specs->typespec_word == cts_double)
7093 error ("both %<long long%> and %<double%> in "
7094 "declaration specifiers");
7095 break;
7097 if (pedantic && !flag_isoc99 && !in_system_header
7098 && warn_long_long)
7099 pedwarn ("ISO C90 does not support %<long long%>");
7100 specs->long_long_p = 1;
7101 break;
7103 if (specs->short_p)
7104 error ("both %<long%> and %<short%> in "
7105 "declaration specifiers");
7106 else if (specs->typespec_word == cts_void)
7107 error ("both %<long%> and %<void%> in "
7108 "declaration specifiers");
7109 else if (specs->typespec_word == cts_bool)
7110 error ("both %<long%> and %<_Bool%> in "
7111 "declaration specifiers");
7112 else if (specs->typespec_word == cts_char)
7113 error ("both %<long%> and %<char%> in "
7114 "declaration specifiers");
7115 else if (specs->typespec_word == cts_float)
7116 error ("both %<long%> and %<float%> in "
7117 "declaration specifiers");
7118 else
7119 specs->long_p = true;
7120 break;
7121 case RID_SHORT:
7122 dupe = specs->short_p;
7123 if (specs->long_p)
7124 error ("both %<long%> and %<short%> in "
7125 "declaration specifiers");
7126 else if (specs->typespec_word == cts_void)
7127 error ("both %<short%> and %<void%> in "
7128 "declaration specifiers");
7129 else if (specs->typespec_word == cts_bool)
7130 error ("both %<short%> and %<_Bool%> in "
7131 "declaration specifiers");
7132 else if (specs->typespec_word == cts_char)
7133 error ("both %<short%> and %<char%> in "
7134 "declaration specifiers");
7135 else if (specs->typespec_word == cts_float)
7136 error ("both %<short%> and %<float%> in "
7137 "declaration specifiers");
7138 else if (specs->typespec_word == cts_double)
7139 error ("both %<short%> and %<double%> in "
7140 "declaration specifiers");
7141 else
7142 specs->short_p = true;
7143 break;
7144 case RID_SIGNED:
7145 dupe = specs->signed_p;
7146 if (specs->unsigned_p)
7147 error ("both %<signed%> and %<unsigned%> in "
7148 "declaration specifiers");
7149 else if (specs->typespec_word == cts_void)
7150 error ("both %<signed%> and %<void%> in "
7151 "declaration specifiers");
7152 else if (specs->typespec_word == cts_bool)
7153 error ("both %<signed%> and %<_Bool%> in "
7154 "declaration specifiers");
7155 else if (specs->typespec_word == cts_float)
7156 error ("both %<signed%> and %<float%> in "
7157 "declaration specifiers");
7158 else if (specs->typespec_word == cts_double)
7159 error ("both %<signed%> and %<double%> in "
7160 "declaration specifiers");
7161 else
7162 specs->signed_p = true;
7163 break;
7164 case RID_UNSIGNED:
7165 dupe = specs->unsigned_p;
7166 if (specs->signed_p)
7167 error ("both %<signed%> and %<unsigned%> in "
7168 "declaration specifiers");
7169 else if (specs->typespec_word == cts_void)
7170 error ("both %<unsigned%> and %<void%> in "
7171 "declaration specifiers");
7172 else if (specs->typespec_word == cts_bool)
7173 error ("both %<unsigned%> and %<_Bool%> in "
7174 "declaration specifiers");
7175 else if (specs->typespec_word == cts_float)
7176 error ("both %<unsigned%> and %<float%> in "
7177 "declaration specifiers");
7178 else if (specs->typespec_word == cts_double)
7179 error ("both %<unsigned%> and %<double%> in "
7180 "declaration specifiers");
7181 else
7182 specs->unsigned_p = true;
7183 break;
7184 case RID_COMPLEX:
7185 dupe = specs->complex_p;
7186 if (pedantic && !flag_isoc99 && !in_system_header)
7187 pedwarn ("ISO C90 does not support complex types");
7188 if (specs->typespec_word == cts_void)
7189 error ("both %<complex%> and %<void%> in "
7190 "declaration specifiers");
7191 else if (specs->typespec_word == cts_bool)
7192 error ("both %<complex%> and %<_Bool%> in "
7193 "declaration specifiers");
7194 else
7195 specs->complex_p = true;
7196 break;
7197 default:
7198 gcc_unreachable ();
7201 if (dupe)
7202 error ("duplicate %qE", type);
7204 return specs;
7206 else
7208 /* "void", "_Bool", "char", "int", "float" or "double". */
7209 if (specs->typespec_word != cts_none)
7211 error ("two or more data types in declaration specifiers");
7212 return specs;
7214 switch (i)
7216 case RID_VOID:
7217 if (specs->long_p)
7218 error ("both %<long%> and %<void%> in "
7219 "declaration specifiers");
7220 else if (specs->short_p)
7221 error ("both %<short%> and %<void%> in "
7222 "declaration specifiers");
7223 else if (specs->signed_p)
7224 error ("both %<signed%> and %<void%> in "
7225 "declaration specifiers");
7226 else if (specs->unsigned_p)
7227 error ("both %<unsigned%> and %<void%> in "
7228 "declaration specifiers");
7229 else if (specs->complex_p)
7230 error ("both %<complex%> and %<void%> in "
7231 "declaration specifiers");
7232 else
7233 specs->typespec_word = cts_void;
7234 return specs;
7235 case RID_BOOL:
7236 if (specs->long_p)
7237 error ("both %<long%> and %<_Bool%> in "
7238 "declaration specifiers");
7239 else if (specs->short_p)
7240 error ("both %<short%> and %<_Bool%> in "
7241 "declaration specifiers");
7242 else if (specs->signed_p)
7243 error ("both %<signed%> and %<_Bool%> in "
7244 "declaration specifiers");
7245 else if (specs->unsigned_p)
7246 error ("both %<unsigned%> and %<_Bool%> in "
7247 "declaration specifiers");
7248 else if (specs->complex_p)
7249 error ("both %<complex%> and %<_Bool%> in "
7250 "declaration specifiers");
7251 else
7252 specs->typespec_word = cts_bool;
7253 return specs;
7254 case RID_CHAR:
7255 if (specs->long_p)
7256 error ("both %<long%> and %<char%> in "
7257 "declaration specifiers");
7258 else if (specs->short_p)
7259 error ("both %<short%> and %<char%> in "
7260 "declaration specifiers");
7261 else
7262 specs->typespec_word = cts_char;
7263 return specs;
7264 case RID_INT:
7265 specs->typespec_word = cts_int;
7266 return specs;
7267 case RID_FLOAT:
7268 if (specs->long_p)
7269 error ("both %<long%> and %<float%> in "
7270 "declaration specifiers");
7271 else if (specs->short_p)
7272 error ("both %<short%> and %<float%> in "
7273 "declaration specifiers");
7274 else if (specs->signed_p)
7275 error ("both %<signed%> and %<float%> in "
7276 "declaration specifiers");
7277 else if (specs->unsigned_p)
7278 error ("both %<unsigned%> and %<float%> in "
7279 "declaration specifiers");
7280 else
7281 specs->typespec_word = cts_float;
7282 return specs;
7283 case RID_DOUBLE:
7284 if (specs->long_long_p)
7285 error ("both %<long long%> and %<double%> in "
7286 "declaration specifiers");
7287 else if (specs->short_p)
7288 error ("both %<short%> and %<double%> in "
7289 "declaration specifiers");
7290 else if (specs->signed_p)
7291 error ("both %<signed%> and %<double%> in "
7292 "declaration specifiers");
7293 else if (specs->unsigned_p)
7294 error ("both %<unsigned%> and %<double%> in "
7295 "declaration specifiers");
7296 else
7297 specs->typespec_word = cts_double;
7298 return specs;
7299 default:
7300 /* ObjC reserved word "id", handled below. */
7301 break;
7306 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7307 form of ObjC type, cases such as "int" and "long" being handled
7308 above), a TYPE (struct, union, enum and typeof specifiers) or an
7309 ERROR_MARK. In none of these cases may there have previously
7310 been any type specifiers. */
7311 if (specs->type || specs->typespec_word != cts_none
7312 || specs->long_p || specs->short_p || specs->signed_p
7313 || specs->unsigned_p || specs->complex_p)
7314 error ("two or more data types in declaration specifiers");
7315 else if (TREE_CODE (type) == TYPE_DECL)
7317 if (TREE_TYPE (type) == error_mark_node)
7318 ; /* Allow the type to default to int to avoid cascading errors. */
7319 else
7321 specs->type = TREE_TYPE (type);
7322 specs->decl_attr = DECL_ATTRIBUTES (type);
7323 specs->typedef_p = true;
7324 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7327 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7329 tree t = lookup_name (type);
7330 if (!t || TREE_CODE (t) != TYPE_DECL)
7331 error ("%qE fails to be a typedef or built in type", type);
7332 else if (TREE_TYPE (t) == error_mark_node)
7334 else
7335 specs->type = TREE_TYPE (t);
7337 else if (TREE_CODE (type) != ERROR_MARK)
7339 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7340 specs->tag_defined_p = true;
7341 if (spec.kind == ctsk_typeof)
7342 specs->typedef_p = true;
7343 specs->type = type;
7346 return specs;
7349 /* Add the storage class specifier or function specifier SCSPEC to the
7350 declaration specifiers SPECS, returning SPECS. */
7352 struct c_declspecs *
7353 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7355 enum rid i;
7356 enum c_storage_class n = csc_none;
7357 bool dupe = false;
7358 specs->declspecs_seen_p = true;
7359 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7360 && C_IS_RESERVED_WORD (scspec));
7361 i = C_RID_CODE (scspec);
7362 if (extra_warnings && specs->non_sc_seen_p)
7363 warning (OPT_Wextra, "%qE is not at beginning of declaration", scspec);
7364 switch (i)
7366 case RID_INLINE:
7367 /* C99 permits duplicate inline. Although of doubtful utility,
7368 it seems simplest to permit it in gnu89 mode as well, as
7369 there is also little utility in maintaining this as a
7370 difference between gnu89 and C99 inline. */
7371 dupe = false;
7372 specs->inline_p = true;
7373 break;
7374 case RID_THREAD:
7375 dupe = specs->thread_p;
7376 if (specs->storage_class == csc_auto)
7377 error ("%<__thread%> used with %<auto%>");
7378 else if (specs->storage_class == csc_register)
7379 error ("%<__thread%> used with %<register%>");
7380 else if (specs->storage_class == csc_typedef)
7381 error ("%<__thread%> used with %<typedef%>");
7382 else
7383 specs->thread_p = true;
7384 break;
7385 case RID_AUTO:
7386 n = csc_auto;
7387 break;
7388 case RID_EXTERN:
7389 n = csc_extern;
7390 /* Diagnose "__thread extern". */
7391 if (specs->thread_p)
7392 error ("%<__thread%> before %<extern%>");
7393 break;
7394 case RID_REGISTER:
7395 n = csc_register;
7396 break;
7397 case RID_STATIC:
7398 n = csc_static;
7399 /* Diagnose "__thread static". */
7400 if (specs->thread_p)
7401 error ("%<__thread%> before %<static%>");
7402 break;
7403 case RID_TYPEDEF:
7404 n = csc_typedef;
7405 break;
7406 default:
7407 gcc_unreachable ();
7409 if (n != csc_none && n == specs->storage_class)
7410 dupe = true;
7411 if (dupe)
7412 error ("duplicate %qE", scspec);
7413 if (n != csc_none)
7415 if (specs->storage_class != csc_none && n != specs->storage_class)
7417 error ("multiple storage classes in declaration specifiers");
7419 else
7421 specs->storage_class = n;
7422 if (n != csc_extern && n != csc_static && specs->thread_p)
7424 error ("%<__thread%> used with %qE", scspec);
7425 specs->thread_p = false;
7429 return specs;
7432 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7433 returning SPECS. */
7435 struct c_declspecs *
7436 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7438 specs->attrs = chainon (attrs, specs->attrs);
7439 specs->declspecs_seen_p = true;
7440 return specs;
7443 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7444 specifiers with any other type specifier to determine the resulting
7445 type. This is where ISO C checks on complex types are made, since
7446 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7447 double". */
7449 struct c_declspecs *
7450 finish_declspecs (struct c_declspecs *specs)
7452 /* If a type was specified as a whole, we have no modifiers and are
7453 done. */
7454 if (specs->type != NULL_TREE)
7456 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7457 && !specs->signed_p && !specs->unsigned_p
7458 && !specs->complex_p);
7459 return specs;
7462 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7463 has been specified, treat it as "int" unless "_Complex" is
7464 present and there are no other specifiers. If we just have
7465 "_Complex", it is equivalent to "_Complex double", but e.g.
7466 "_Complex short" is equivalent to "_Complex short int". */
7467 if (specs->typespec_word == cts_none)
7469 if (specs->long_p || specs->short_p
7470 || specs->signed_p || specs->unsigned_p)
7472 specs->typespec_word = cts_int;
7474 else if (specs->complex_p)
7476 specs->typespec_word = cts_double;
7477 if (pedantic)
7478 pedwarn ("ISO C does not support plain %<complex%> meaning "
7479 "%<double complex%>");
7481 else
7483 specs->typespec_word = cts_int;
7484 specs->default_int_p = true;
7485 /* We don't diagnose this here because grokdeclarator will
7486 give more specific diagnostics according to whether it is
7487 a function definition. */
7491 /* If "signed" was specified, record this to distinguish "int" and
7492 "signed int" in the case of a bit-field with
7493 -funsigned-bitfields. */
7494 specs->explicit_signed_p = specs->signed_p;
7496 /* Now compute the actual type. */
7497 switch (specs->typespec_word)
7499 case cts_void:
7500 gcc_assert (!specs->long_p && !specs->short_p
7501 && !specs->signed_p && !specs->unsigned_p
7502 && !specs->complex_p);
7503 specs->type = void_type_node;
7504 break;
7505 case cts_bool:
7506 gcc_assert (!specs->long_p && !specs->short_p
7507 && !specs->signed_p && !specs->unsigned_p
7508 && !specs->complex_p);
7509 specs->type = boolean_type_node;
7510 break;
7511 case cts_char:
7512 gcc_assert (!specs->long_p && !specs->short_p);
7513 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7514 if (specs->signed_p)
7515 specs->type = signed_char_type_node;
7516 else if (specs->unsigned_p)
7517 specs->type = unsigned_char_type_node;
7518 else
7519 specs->type = char_type_node;
7520 if (specs->complex_p)
7522 if (pedantic)
7523 pedwarn ("ISO C does not support complex integer types");
7524 specs->type = build_complex_type (specs->type);
7526 break;
7527 case cts_int:
7528 gcc_assert (!(specs->long_p && specs->short_p));
7529 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7530 if (specs->long_long_p)
7531 specs->type = (specs->unsigned_p
7532 ? long_long_unsigned_type_node
7533 : long_long_integer_type_node);
7534 else if (specs->long_p)
7535 specs->type = (specs->unsigned_p
7536 ? long_unsigned_type_node
7537 : long_integer_type_node);
7538 else if (specs->short_p)
7539 specs->type = (specs->unsigned_p
7540 ? short_unsigned_type_node
7541 : short_integer_type_node);
7542 else
7543 specs->type = (specs->unsigned_p
7544 ? unsigned_type_node
7545 : integer_type_node);
7546 if (specs->complex_p)
7548 if (pedantic)
7549 pedwarn ("ISO C does not support complex integer types");
7550 specs->type = build_complex_type (specs->type);
7552 break;
7553 case cts_float:
7554 gcc_assert (!specs->long_p && !specs->short_p
7555 && !specs->signed_p && !specs->unsigned_p);
7556 specs->type = (specs->complex_p
7557 ? complex_float_type_node
7558 : float_type_node);
7559 break;
7560 case cts_double:
7561 gcc_assert (!specs->long_long_p && !specs->short_p
7562 && !specs->signed_p && !specs->unsigned_p);
7563 if (specs->long_p)
7565 specs->type = (specs->complex_p
7566 ? complex_long_double_type_node
7567 : long_double_type_node);
7569 else
7571 specs->type = (specs->complex_p
7572 ? complex_double_type_node
7573 : double_type_node);
7575 break;
7576 default:
7577 gcc_unreachable ();
7580 return specs;
7583 /* Synthesize a function which calls all the global ctors or global
7584 dtors in this file. This is only used for targets which do not
7585 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7586 static void
7587 build_cdtor (int method_type, tree cdtors)
7589 tree body = 0;
7591 if (!cdtors)
7592 return;
7594 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7595 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7596 &body);
7598 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7601 /* A subroutine of c_write_global_declarations. Perform final processing
7602 on one file scope's declarations (or the external scope's declarations),
7603 GLOBALS. */
7605 static void
7606 c_write_global_declarations_1 (tree globals)
7608 tree decl;
7609 bool reconsider;
7611 /* Process the decls in the order they were written. */
7612 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7614 /* Check for used but undefined static functions using the C
7615 standard's definition of "used", and set TREE_NO_WARNING so
7616 that check_global_declarations doesn't repeat the check. */
7617 if (TREE_CODE (decl) == FUNCTION_DECL
7618 && DECL_INITIAL (decl) == 0
7619 && DECL_EXTERNAL (decl)
7620 && !TREE_PUBLIC (decl)
7621 && C_DECL_USED (decl))
7623 pedwarn ("%q+F used but never defined", decl);
7624 TREE_NO_WARNING (decl) = 1;
7627 wrapup_global_declaration_1 (decl);
7632 reconsider = false;
7633 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7634 reconsider |= wrapup_global_declaration_2 (decl);
7636 while (reconsider);
7638 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7639 check_global_declaration_1 (decl);
7642 /* A subroutine of c_write_global_declarations Emit debug information for each
7643 of the declarations in GLOBALS. */
7645 static void
7646 c_write_global_declarations_2 (tree globals)
7648 tree decl;
7650 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
7651 debug_hooks->global_decl (decl);
7654 /* Preserve the external declarations scope across a garbage collect. */
7655 static GTY(()) tree ext_block;
7657 void
7658 c_write_global_declarations (void)
7660 tree t;
7662 /* We don't want to do this if generating a PCH. */
7663 if (pch_file)
7664 return;
7666 /* Don't waste time on further processing if -fsyntax-only or we've
7667 encountered errors. */
7668 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7669 return;
7671 /* Close the external scope. */
7672 ext_block = pop_scope ();
7673 external_scope = 0;
7674 gcc_assert (!current_scope);
7676 if (ext_block)
7678 tree tmp = BLOCK_VARS (ext_block);
7679 int flags;
7680 FILE * stream = dump_begin (TDI_tu, &flags);
7681 if (stream && tmp)
7683 dump_node (tmp, flags & ~TDF_SLIM, stream);
7684 dump_end (TDI_tu, stream);
7688 /* Process all file scopes in this compilation, and the external_scope,
7689 through wrapup_global_declarations and check_global_declarations. */
7690 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7691 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7692 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7694 /* Generate functions to call static constructors and destructors
7695 for targets that do not support .ctors/.dtors sections. These
7696 functions have magic names which are detected by collect2. */
7697 build_cdtor ('I', static_ctors); static_ctors = 0;
7698 build_cdtor ('D', static_dtors); static_dtors = 0;
7700 /* We're done parsing; proceed to optimize and emit assembly.
7701 FIXME: shouldn't be the front end's responsibility to call this. */
7702 cgraph_optimize ();
7704 /* After cgraph has had a chance to emit everything that's going to
7705 be emitted, output debug information for globals. */
7706 if (errorcount == 0 && sorrycount == 0)
7708 timevar_push (TV_SYMOUT);
7709 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7710 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
7711 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
7712 timevar_pop (TV_SYMOUT);
7715 ext_block = NULL;
7718 #include "gt-c-decl.h"